forked from farteryhr/tetr.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.js
305 lines (291 loc) · 9.41 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
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.grid = cells;
}
/**
* Adds tetro to the stack, and clears lines if they fill up.
*/
Stack.prototype.addPiece = function(tetro) {
var lineClear = 0;
var isSpin = false;
var once = false;
// spin check
if (
!piece.moveValid(-1, 0, piece.tetro) &&
!piece.moveValid( 1, 0, piece.tetro) &&
!piece.moveValid( 0,-1, piece.tetro)
) {
isSpin = true;
}
// Add the piece to the stack.
var range = [];
var valid = false;
for (var x = 0; x < tetro.length; x++) {
for (var y = 0; y < tetro[x].length; y++) {
if (tetro[x][y] && y + piece.y >= 0) {
this.grid[x + piece.x][y + piece.y] = RotSys[settings.RotSys].color[piece.index];
// Get column for finesse
if (!once || x + piece.x < column) {
column = x + piece.x;
once = true;
}
// Check which lines get modified
if (range.indexOf(y + piece.y) === -1) {
range.push(y + piece.y);
// 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 + piece.y >= this.hiddenHeight) valid = true;
}
}
}
}
// Lock out
if (!valid) {
gameState = 9;
$setText(msg,'LOCK OUT!');
menu(3);
sound.playse("gameover");
return;
}
// Check modified lines for full lines.
range = range.sort(function(a,b){return a-b});
for (var row = range[0], len = row + range.length; row < len; 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
var rowInDig = digLines.indexOf(row);
if (rowInDig !== -1) {
for (var y = 0; y < rowInDig; y++) {
digLines[y]++;
}
digLines.splice(rowInDig, 1);
}
for (var y = row; y >= 1; y--) {
for (var x = 0; x < this.width; x++) {
this.grid[x][y] = this.grid[x][y - 1];
}
}
for (var x = 0; x < this.width; x++) {
this.grid[x][0] = void 0;
}
}
}
var scoreAdd = bigInt(level + 1);
if (lineClear !== 0) {
//console.log("C"+combo+" B"+b2b)
if (isSpin) {
scoreAdd = scoreAdd.mul(
bigInt([800,1200,1600,2000][lineClear - 1])
.mul(bigInt(2).pow(b2b + combo))
);
b2b += 1;
sound.playse("tspin",lineClear);
} else if(lineClear === 4) {
scoreAdd = scoreAdd.mul(
bigInt(800)
.mul(bigInt(2).pow(b2b + combo))
);
b2b += 1;
sound.playse("erase",lineClear);
} else {
scoreAdd = scoreAdd.mul(
bigInt([100,300,500,800][lineClear - 1])
.mul(bigInt(2).pow(combo))
);
b2b = 0;
sound.playse("erase",lineClear);
}
combo += 1;
} else {
if (isSpin) {
scoreAdd = scoreAdd.mul(
bigInt(2).pow(bigInt(b2b))
.mul(bigInt(400))
);
sound.playse("tspin",lineClear);
} 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 pc = true;
for (var x = 0; x < this.width; x++)
for (var y = 0; y < this.height; y++)
if (this.grid[x][y])
pc = false;
if (pc) {
score = score.add(bigInt(1000000).mul(bigInt(16).pow(allclear)));
allclear ++;
sound.playse("bravo");
}
//if (scoreAdd.cmp(0) > 0)
//console.log(scoreAdd.toString());
statsFinesse += piece.finesse - finesse[piece.index][piece.pos][column];
piecesSet++; // NOTE Stats
// TODO Might not need this (same for in init)
column = 0;
statisticsStack();
this.draw();
}
/**
* 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(topout) {
gameState = 9;
$setText(msg,'TOP OUT!');
menu(3);
sound.playse("gameover");
}
if(!isEmpty) {
digLines.push(this.height - 1);
}
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
gameState = 9;
$setText(msg,'OOPS!');
menu(3);
sound.playse("gameover");
}
}
piece.dirty = true;
this.draw();
}
/**
* 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);
}
// 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);
}
}
var stack = new Stack();