forked from simonlc/tetr.js
-
Notifications
You must be signed in to change notification settings - Fork 27
/
stack.js
590 lines (561 loc) · 19.2 KB
/
stack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
function Stack() {
//this.grid;
}
/**
* Creates a matrix for the playfield.
*/
Stack.prototype.new = function(x, y, hy) {
var cells = new Array(x);
for (var i = 0; i < x; i++) {
cells[i] = new Array(hy + y);
}
this.width = x;
this.height = hy + y;
this.hiddenHeight = hy;
this.toGreyRow = this.height - 1;
this.grid = cells;
this.dirty = true;
this.statisticsDirty = true;
}
/**
* Adds tetro to the stack, and clears lines if they fill up.
*/
Stack.prototype.addPiece = function(piece) {
var tetro = piece.tetro;
var rect = pieces[piece.index].rect[piece.pos];
var lineClear = 0;
var isSpin = false;
var once = false;
var piecesDel = 0; // for whole/symmetry
var bottomRow = []; // for backfire
for (var x = 0; x < this.width; x++) {
bottomRow.push(this.grid[x][this.height - 1]);
}
// spin check
if (
!piece.moveValid(-1, 0, piece.tetro) &&
!piece.moveValid( 1, 0, piece.tetro) &&
!piece.moveValid( 0,-1, piece.tetro)
) {
isSpin = true;
}
do { // for gameover breaking
// for symmetric
var cellToDelWhole = [];
// Add the piece to the stack.
var valid = false;
var doAddPiece = true;
if (gameparams.validPos === 1) { // fixed lite, only allow flat up-facing pose
if (!(piece.index === PieceO.index || piece.pos === 0 || (piece.pos === 2 && (
[PieceI.index, PieceS.index, PieceZ.index].indexOf(piece.index) !== -1
)))){
doAddPiece = false;
}
} else if (gameparams.validPos === 2) { // just flat pose
if (!(piece.index === PieceO.index || piece.pos === 0 || piece.pos === 2)){
doAddPiece = false;
}
}
if (!doAddPiece) {
timePenalty += 10;
if (timePenalty >= 100) {
gameState = 9;
$setText(msg,'RUUAAHHHH!');
menu(3);
sound.playse("gameover");
break;
}
}
for (var tx = 0; tx < tetro.length; tx++) {
for (var ty = 0; ty < tetro[tx].length; ty++) {
var x = tx + piece.x, y = ty + piece.y;
if (tetro[tx][ty] && y >= 0) {
if (doAddPiece) {
this.grid[x][y] =
RotSys[settings.RotSys].color[piece.index] |
(cellFlags.maskConn & tetro[tx][ty]) |
((piece.index + 1) << cellFlags.heightIndex);
}
if (gameparams.symmetry === 1) {
// mark pieces violating symmetry constraint, delete later
// must delete after completely added, because it may be itself!
var symmx = this.width - 1 - x;
var c = this.grid[x][y];
var symmc = this.grid[symmx][y];
if (c !== void 0 && symmc !== void 0) { // in-game, pass if only one
if (!Stack.isMinoSymmetric(c, symmc)){
cellToDelWhole.push([symmx, y]);
}
}
}
if (gameparams.isolation) {
// find piece of the same index nearby (but not myself), delete it
for (var i = 0; i < 4; i++){
var d = fourWays[i];
var cx = x + d.dx, cy = y + d.dy;
if (cx >= 0 && cx < this.width && cy >= 0 && cy < this.height) {
if (!(this.grid[x][y] & d.connFlag) && this.grid[cx][cy] !== void 0 && (
((this.grid[cx][cy] & cellFlags.maskIndex) >> cellFlags.heightIndex) === piece.index + 1
)) {
cellToDelWhole.push([cx, cy]);
if (gameparams.isolation === 2) {
cellToDelWhole.push([x, y]);
}
}
}
}
}
// Get column for finesse
if (!once || x < column) {
column = x;
once = true;
}
// This checks if any cell is in the play field. If there
// isn't any this is called a lock out and the game ends.
if (y >= this.hiddenHeight) {
valid = true;
}
}
}
}
if (gameparams.symmetry === 1 || gameparams.isolation) {
for (var i = 0; i < cellToDelWhole.length; i++){
var xy = cellToDelWhole[i];
var x = xy[0], y = xy[1];
piecesDel += this.delWholePieceAt(x, y);
}
if (piecesDel && !gameparams.isolation) { // isoltion mode, don't add penalty
timePenalty += 10 * piecesDel;
if (timePenalty >= 100) {
gameState = 9;
$setText(msg,'RUUAAHHHH!');
menu(3);
sound.playse("gameover");
break; // may skip line clear, but don't care
}
}
}
// Lock out
if (!valid) {
gameState = 9;
$setText(msg,'LOCK OUT!');
menu(3);
sound.playse("gameover");
break;
}
var rowsDel = [];
if (gameparams.triplet) {
// triplet mode: delete only triplet lines
var fullMask = 0, delMask = 0; // bit, max 31
for (var y = 0; y < this.height; y++) {
var count = 0;
for (var x = 0; x < this.width; x++) {
if (this.grid[x][y]) count++;
}
if (count === this.width) {
fullMask |= (1 << y);
}
}
for (var interval = 1; interval * 2 + 1 < this.height; interval++) {
var triplets = fullMask & (fullMask << interval) & (fullMask << (interval * 2));
delMask |= triplets | (triplets >> interval) | (triplets >> (interval * 2));
}
if(delMask)console.log(delMask);
for (var y = 0; y < this.height; y++) {
if (delMask & (1 << y)) {
lineClear++;
rowsDel.push(y);
}
}
} else {
// ordinary: Check modified lines for full lines.
var yfrom = Math.max(0, piece.y + rect[1]);
var yto = piece.y + rect[3];
for (var row = yfrom; row < yto; row++) {
var count = 0;
for (var x = 0; x < this.width; x++) {
if (this.grid[x][row]) count++;
}
// Clear the line. This basically just moves down the stack.
// TODO Ponder during the day and see if there is a more elegant solution.
if (count === this.width) {
lineClear++; // NOTE stats
rowsDel.push(row);
}
}
}
// Delete the lines
for (var i = 0; i < rowsDel.length; i++) {
var row = rowsDel[i];
var rowInDig = digLines.indexOf(row);
if (rowInDig !== -1) {
for (var y = 0; y < rowInDig; y++) {
digLines[y]++;
}
digLines.splice(rowInDig, 1);
}
if (gameparams.delType === 1){
for (var x = 0; x < this.width; x++) {
this.delWholePieceAt(x, row);
}
} else {
for (var x = 0; x < this.width; x++) {
this.grid[x][row] = void 0;
if (row > 0 && this.grid[x][row - 1] !== void 0) {
this.grid[x][row - 1] &= ~cellFlags.connDown;
}
if (row + 1 < this.height && this.grid[x][row + 1] !== void 0){
this.grid[x][row + 1] &= ~cellFlags.connUp;
}
}
}
if (!gameparams.fallMode) { // 0 normal 1 none 2+ others
for (var y = row; y >= 0; y--) {
for (var x = 0; x < this.width; x++) {
this.grid[x][y] = y === 0 ? void 0 : this.grid[x][y - 1];
}
}
}
}
var scoreAdd = bigInt(level + 1);
var garbage = 0;
if (lineClear !== 0) {
//console.log("C"+combo+" B"+b2b)
if (isSpin) {
scoreAdd = scoreAdd.mul(
bigInt(400 + 400 * lineClear)
.mul(bigInt(2).pow(b2b + combo))
);
garbage = ~~((2 + (lineClear + 3) * lineClear / 2) * (b2b == 0 ? 3 : 4) / 5);
b2b += 1;
sound.playse("tspin",Math.min(lineClear, 3));
} else if (lineClear >= 4) {
scoreAdd = scoreAdd.mul(
bigInt(100 * (lineClear * lineClear / 4 + lineClear) - 25 * (lineClear & 1))
.mul(bigInt(2).pow(b2b + combo))
);
garbage = ~~((lineClear * lineClear) * (b2b == 0 ? 4 : 5) / 16);
b2b += 1;
sound.playse("erase4");
} else {
scoreAdd = scoreAdd.mul(
bigInt([100,300,500,800][lineClear - 1])
.mul(bigInt(2).pow(combo))
);
b2b = 0;
garbage = [0,1,2,4][lineClear - 1];
sound.playse("erase",lineClear);
}
garbage += ~~(combo / 2); //[0,0,1,1,2,2,3,3,4,4,5,5,6,6,...]
combo += 1;
} else {
if (isSpin) {
scoreAdd = scoreAdd.mul(
bigInt(2).pow(bigInt(b2b))
.mul(bigInt(400))
);
sound.playse("tspin0");
} else {
scoreAdd = bigInt(0);
}
combo = 0;
}
lines += lineClear;
if (gametype === 1 || gametype === 6) {
level = ~~(lines / 10);
} else if (gametype === 7) {
level = ~~(lines / 30);
}
score = score.add(scoreAdd.mul(bigInt(16).pow(allclear)));
var isPC = true;
if (piecesDel !== 0 || !doAddPiece) { // self symmetry violation leads to fake PC
isPC = false;
}
for (var x = 0; x < this.width; x++)
for (var y = 0; y < this.height; y++)
if (this.grid[x][y])
isPC = false;
if (isPC) {
score = score.add(bigInt(1000000).mul(bigInt(16).pow(allclear)));
allclear ++;
sound.playse("bravo");
garbage += 10;
}
if(gameparams.backFire){
if(gameparams.backFire === 1){
garbage = ~~(lineClear * 1.5 - 2);
}else if(gameparams.backFire === 3){
garbage *= ~~(lines/2);
}else if(gameparams.backFire === 4){
garbage = +(combo >= 2 || lineClear >= 2 || isSpin || isPC)
}
if(garbage !== 0) {
if(gameparams.backFire === 1){
for(var y = 0; y < garbage; y++){
this.rowRise(bottomRow, piece);
}
}else if(gameparams.backFire === 2 || gameparams.backFire === 3){
var hole = ~~(rng.next() * 10);
var arrRow = [8,8,8,8,8,8,8,8,8,8];
arrRow[hole] = 0;
for(var y = 0; y < garbage; y++){
this.rowRise(arrRow, piece);
}
}else if(gameparams.backFire === 4){
stack.makeSomeArt();
if(isPC) {
$setText(msg,'PAHUEKUTO KURIYAA!');
} else if(isSpin) {
$setText(msg,'SPINNING!');
} else if(combo >= 2) {
$setText(msg,'RAHH COMBO!');
} else if(lineClear >= 2) {
$setText(msg,['DOUBLE!','TRIPLE!','TETR.JS!'][lineClear - 2]);
}
}
}
}
//if (scoreAdd.cmp(0) > 0)
//console.log(scoreAdd.toString());
} while(false); // gameover break
statsFinesse += piece.finesse - finesse[piece.index][piece.pos][column];
piecesSet++; // NOTE Stats
// TODO Might not need this (same for in init)
column = 0;
this.statisticsDirty = true;
this.dirty = true;
}
/**
* Raise a garbage line. farter
*/
Stack.prototype.rowRise = function(arrRow, objPiece) {
var isEmpty = true;
for(var x = 0; x < this.width; x++) {
for(var y = 0; y < this.height - 1; y++) {
this.grid[x][y] = this.grid[x][y+1];
}
if(arrRow[x])
isEmpty = false;
this.grid[x][this.height-1]=arrRow[x];
}
var topout = false;
for(var y = 0; y < digLines.length; y++) {
digLines[y]--;
if(digLines[y] < 0) { // top out, but only detecting added lines
topout = true;
}
}
if(gameState !== 9 && topout) {
gameState = 9;
$setText(msg,'TOP OUT!');
menu(3);
sound.playse("gameover");
}
if(!isEmpty) {
digLines.push(this.height - 1);
}
if (!piece.dead) {
if (!piece.moveValid(0, 0, piece.tetro)) {
piece.y-=1;
if (piece.y + pieces[piece.index].rect[3] <= this.hiddenHeight - 2) {
// the bottom is >=2 cell away from visible part
if(gameState !== 9) { // may happen simutanously
gameState = 9;
$setText(msg,'RUUAAAAA!');
menu(3);
sound.playse("gameover");
}
}
}
piece.dirty = true;
}
this.dirty = true;
}
/**
* Detele whole piece by flood fill according to mino connection flags.
*/
Stack.prototype.delWholePieceAt = function(x, y) {
if (this.grid[x][y] === void 0){
return 0;
}
var queue = [[x,y]], qh = 0;
while (qh < queue.length){
var cxy = queue[qh++];
var cx = cxy[0], cy = cxy[1];
var c = this.grid[cx][cy];
if (c !== void 0) { // may be added into queue twice or more
this.grid[cx][cy] = void 0;
if ((c & cellFlags.connRight) && cx + 1 < this.width && this.grid[cx + 1][cy] !== void 0) {
queue.push([cx + 1, cy]);
}
if ((c & cellFlags.connDown) && cy + 1 < this.height && this.grid[cx][cy + 1] !== void 0) {
queue.push([cx, cy + 1]);
}
if ((c & cellFlags.connLeft) && cx > 0 && this.grid[cx - 1][cy] !== void 0) {
queue.push([cx - 1, cy]);
}
if ((c & cellFlags.connUp) && cy > 0 && this.grid[cx][cy - 1] !== void 0) {
queue.push([cx, cy - 1]);
}
}
}
//console.log(JSON.stringify(queue));
return 1;
}
/**
* Draws the stack.
*/
Stack.prototype.draw = function() {
clear(stackCtx);
if(settings.Outline === 0 || settings.Outline === 1 ||
(settings.Outline === 2 && (gameState === 9 || gameState === 1))
) {
draw(this.grid, 0, -this.hiddenHeight, stackCtx, void 0, 0.3);
}
if(gameparams.symmetry === 1 && timePenalty > 0){
stackCtx.globalAlpha = 0.2;
var tetro = this.grid;
for (var x = 0; x < this.width; x++) {
for (var y = this.hiddenHeight; y < this.height; y++) {
var symmx = this.width - x - 1;
if (!tetro[x][y] && tetro[symmx][y]) {
var index = (tetro[symmx][y] & cellFlags.maskIndex) >> cellFlags.heightIndex;
var color = index === 0 ? 0 : RotSys[settings.RotSys].color[pieceSymmetryIndex[index - 1]];
drawCell(x, y - this.hiddenHeight, color, stackCtx);
}
}
}
stackCtx.globalAlpha = 1;
}
// Darken Stack
// TODO wrap this with an option.
// no fullscreen flush, see above
//stackCtx.globalCompositeOperation = 'source-atop';
//stackCtx.fillStyle = 'rgba(0,0,0,0.3)';
//stackCtx.fillRect(0, 0, stackCanvas.width, stackCanvas.height);
//stackCtx.globalCompositeOperation = 'source-over';
if (settings.Outline === 1 || settings.Outline === 3) {
var b = ~~(cellSize / 8);
var c = cellSize;
var hhc = this.hiddenHeight * c;
var pi = Math.PI;
var lineCanvas = document.createElement('canvas');
lineCanvas.width = stackCanvas.width;
lineCanvas.height = stackCanvas.height;
var lineCtx = lineCanvas.getContext('2d');
lineCtx.fillStyle = 'rgba(255,255,255,0.5)';
lineCtx.beginPath();
for (var x = 0, len = this.width; x < len; x++) {
for (var y = 0, wid = this.height; y < wid; y++) {
if (this.grid[x][y]) {
if (x < this.width - 1 && !this.grid[x + 1][y]) {
lineCtx.fillRect(x * c + c - b, y * c - hhc, b, c);
}
if (x > 0 && !this.grid[x - 1][y]) {
lineCtx.fillRect(x * c, y * c - hhc, b, c);
}
if (y < this.height - 1 && !this.grid[x][y + 1]) {
lineCtx.fillRect(x * c, y * c - hhc + c - b, c, b);
}
if (!this.grid[x][y - 1]) {
lineCtx.fillRect(x * c, y * c - hhc, c, b);
}
// Diags
if (x < this.width - 1 && y < this.height - 1) {
if (!this.grid[x + 1][y] && !this.grid[x][y + 1]) {
lineCtx.clearRect(x * c + c - b, y * c - hhc + c - b, b, b);
lineCtx.fillRect(x * c + c - b, y * c - hhc + c - b, b, b);
} else if (!this.grid[x + 1][y + 1] && this.grid[x + 1][y] && this.grid[x][y + 1]) {
lineCtx.moveTo(x * c + c, y * c - hhc + c - b);
lineCtx.lineTo(x * c + c, y * c - hhc + c);
lineCtx.lineTo(x * c + c - b, y * c - hhc + c);
lineCtx.arc(x * c + c, y * c - hhc + c, b, 3 * pi / 2, pi, true);
}
}
if (x < this.width - 1 && y > -this.hiddenHeight) {
if (!this.grid[x + 1][y] && !this.grid[x][y - 1]) {
lineCtx.clearRect(x * c + c - b, y * c - hhc, b, b);
lineCtx.fillRect(x * c + c - b, y * c - hhc, b, b);
} else if (!this.grid[x + 1][y - 1] && this.grid[x + 1][y] && this.grid[x][y - 1]) {
lineCtx.moveTo(x * c + c - b, y * c - hhc);
lineCtx.lineTo(x * c + c, y * c - hhc);
lineCtx.lineTo(x * c + c, y * c - hhc + b);
lineCtx.arc(x * c + c, y * c - hhc, b, pi / 2, pi, false);
}
}
if (x > 0 && y < this.height - 1) {
if (!this.grid[x - 1][y] && !this.grid[x][y + 1]) {
lineCtx.clearRect(x * c, y * c - hhc + c - b, b, b);
lineCtx.fillRect(x * c, y * c - hhc + c - b, b, b);
} else if (!this.grid[x - 1][y + 1] && this.grid[x - 1][y] && this.grid[x][y + 1]) {
lineCtx.moveTo(x * c, y * c - hhc + c - b);
lineCtx.lineTo(x * c, y * c - hhc + c);
lineCtx.lineTo(x * c + b, y * c - hhc + c);
lineCtx.arc(x * c, y * c - hhc + c, b, pi * 2, 3 * pi / 2, true);
}
}
if (x > 0 && y > -this.hiddenHeight) {
if (!this.grid[x - 1][y] && !this.grid[x][y - 1]) {
lineCtx.clearRect(x * c, y * c - hhc, b, b);
lineCtx.fillRect(x * c, y * c - hhc, b, b);
} else if (!this.grid[x - 1][y - 1] && this.grid[x - 1][y] && this.grid[x][y - 1]) {
lineCtx.moveTo(x * c + b, y * c - hhc);
lineCtx.lineTo(x * c, y * c - hhc);
lineCtx.lineTo(x * c, y * c - hhc + b);
lineCtx.arc(x * c, y * c - hhc, b, pi / 2, pi * 2, true);
}
}
}
}
}
lineCtx.fill();
stackCtx.globalCompositeOperation = 'source-over';
stackCtx.drawImage(lineCanvas, 0, 0);
}
if (this.statisticsDirty)
statisticsStack();
this.dirty = false;
}
Stack.prototype.makeSomeArt = function(piece){
var arrRow = [8,8,8,8,8,8,8,8,8,8];
for(var y = 0; y < this.hiddenHeight + 1; y++){ // ensure dies
this.rowRise(arrRow, piece);
}
var colors = [];
var dc = ~~(rng.next()*rng.next()*6)+1, bc = ~~(rng.next()*7);
for(var c=0;c<7;c++){
colors[c]=[7,3,4,5,1,2,6][(dc*c+bc)%7];
}
var dx = ~~(rng.next()*7), dy = ~~(rng.next()*7),
mod = ~~(rng.next()*7+1), bxy = ~~(rng.next()*mod);
for(var y = 0; y < this.height; y++){
for(var x = 0; x < this.width; x++){
arrRow[x] = colors[(y*dy+x*dx+bxy)%mod];
}
this.rowRise(arrRow, piece);
}
this.toGreyRow = 0;
console.log(dc,bc,dx,dy,mod,bxy);
}
/**
* single mino symmetry test
* strict version: both empty or both mino
*/
Stack.isMinoSymmetric = function(c, symmc) {
var symmIndexData = (symmc & cellFlags.maskIndex) >> cellFlags.heightIndex;
var indexData = (c & cellFlags.maskIndex) >> cellFlags.heightIndex
var conn = symmc & cellFlags.maskConn;
// exchange left&right
conn = (conn & cellFlags.connUp) | (conn & cellFlags.connDown) |
((conn & cellFlags.connLeft) ? cellFlags.connRight : 0) |
((conn & cellFlags.connRight) ? cellFlags.connLeft : 0) ;
// console.log(indexData - 1, symmIndexData - 1, conn, c & cellFlags.maskConn);
if (indexData > 0 && symmIndexData > 0 && (
pieceSymmetryIndex[indexData - 1] !== symmIndexData - 1 ||
(c & cellFlags.maskConn) !== conn
)){
return false;
}
return true;
}
var stack = new Stack();