Commit | Line | Data |
---|---|---|
bc2bc396 | 1 | import ChessRules from "/base_rules.js"; |
f3e90e30 BA |
2 | import {ArrayFun} from "/utils/array.js"; |
3 | import {Random} from "/utils/alea.js"; | |
7c038235 | 4 | import {FenUtil} from "/utils/setupPieces.js"; |
8f57fbf2 BA |
5 | import PiPo from "/utils/PiPo.js"; |
6 | import Move from "/utils/Move.js"; | |
f382c57b | 7 | |
bc2bc396 | 8 | export default class ChakartRules extends ChessRules { |
f8b43ef7 BA |
9 | |
10 | static get Options() { | |
11 | return { | |
12 | select: [ | |
13 | { | |
14 | label: "Randomness", | |
15 | variable: "randomness", | |
16 | defaut: 2, | |
17 | options: [ | |
37481d1e BA |
18 | {label: "Deterministic", value: 0}, |
19 | {label: "Symmetric random", value: 1}, | |
20 | {label: "Asymmetric random", value: 2} | |
f8b43ef7 BA |
21 | ] |
22 | } | |
3b641716 BA |
23 | ], |
24 | styles: ["cylinder"] | |
f8b43ef7 BA |
25 | }; |
26 | } | |
27 | ||
8f57fbf2 BA |
28 | get pawnPromotions() { |
29 | return ['q', 'r', 'n', 'b', 'k']; | |
f8b43ef7 | 30 | } |
8f57fbf2 | 31 | |
2b9b90da | 32 | get hasCastle() { |
f8b43ef7 BA |
33 | return false; |
34 | } | |
2b9b90da | 35 | get hasEnpassant() { |
f8b43ef7 BA |
36 | return false; |
37 | } | |
24872b22 BA |
38 | get hasReserve() { |
39 | return true; | |
40 | } | |
41 | get hasReserveFen() { | |
42 | return false; | |
43 | } | |
f8b43ef7 | 44 | |
f8b43ef7 BA |
45 | static get IMMOBILIZE_CODE() { |
46 | return { | |
47 | 'p': 's', | |
48 | 'r': 'u', | |
49 | 'n': 'o', | |
50 | 'b': 'c', | |
51 | 'q': 't', | |
52 | 'k': 'l' | |
53 | }; | |
54 | } | |
55 | ||
56 | static get IMMOBILIZE_DECODE() { | |
57 | return { | |
58 | 's': 'p', | |
59 | 'u': 'r', | |
60 | 'o': 'n', | |
61 | 'c': 'b', | |
62 | 't': 'q', | |
63 | 'l': 'k' | |
64 | }; | |
65 | } | |
66 | ||
f8b43ef7 BA |
67 | // Fictive color 'a', bomb banana mushroom egg |
68 | static get BOMB() { | |
69 | return 'w'; //"Wario" | |
70 | } | |
71 | static get BANANA() { | |
72 | return 'd'; //"Donkey" | |
73 | } | |
74 | static get EGG() { | |
75 | return 'e'; | |
76 | } | |
77 | static get MUSHROOM() { | |
78 | return 'm'; | |
79 | } | |
80 | ||
24872b22 BA |
81 | static get EGG_SURPRISE() { |
82 | return [ | |
83 | "kingboo", "bowser", "daisy", "koopa", | |
84 | "luigi", "waluigi", "toadette", "chomp"]; | |
85 | } | |
86 | ||
3b641716 BA |
87 | canIplay(x, y) { |
88 | if ( | |
89 | this.playerColor != this.turn || | |
90 | Object.keys(V.IMMOBILIZE_DECODE).includes(this.getPiece(x, y)) | |
91 | ) { | |
92 | return false; | |
93 | } | |
94 | return this.egg == "kingboo" || this.getColor(x, y) == this.turn; | |
95 | } | |
96 | ||
24872b22 | 97 | pieces(color, x, y) { |
3b641716 | 98 | const specials = { |
24872b22 | 99 | 'i': {"class": "invisible"}, //queen |
cc9fe4f1 | 100 | '?': {"class": "mystery"}, //...initial square |
24872b22 BA |
101 | 'e': {"class": "egg"}, |
102 | 'm': {"class": "mushroom"}, | |
103 | 'd': {"class": "banana"}, | |
3b641716 BA |
104 | 'w': {"class": "bomb"}, |
105 | 'z': {"class": "remote-capture"} | |
24872b22 | 106 | }; |
3b641716 BA |
107 | const bowsered = { |
108 | 's': {"class": ["immobilized", "pawn"]}, | |
109 | 'u': {"class": ["immobilized", "rook"]}, | |
110 | 'o': {"class": ["immobilized", "knight"]}, | |
111 | 'c': {"class": ["immobilized", "bishop"]}, | |
112 | 't': {"class": ["immobilized", "queen"]}, | |
113 | 'l': {"class": ["immobilized", "king"]} | |
114 | }; | |
f5435757 BA |
115 | return Object.assign( |
116 | { | |
117 | 'y': { | |
118 | // Virtual piece for "king remote shell captures" | |
f5435757 BA |
119 | attack: [ |
120 | { | |
121 | steps: [ | |
122 | [0, 1], [0, -1], [1, 0], [-1, 0], | |
123 | [1, 1], [1, -1], [-1, 1], [-1, -1] | |
124 | ] | |
125 | } | |
126 | ] | |
127 | } | |
128 | }, | |
6b9320bb BA |
129 | specials, bowsered, super.pieces(color, x, y) |
130 | ); | |
24872b22 BA |
131 | } |
132 | ||
130a166f BA |
133 | isKing(x, y, p) { |
134 | if (!p) | |
135 | p = this.getPiece(x, y); | |
136 | return ['k', 'l'].includes(p); | |
137 | } | |
138 | ||
f31de5e4 | 139 | genRandInitBaseFen() { |
7c038235 | 140 | const s = FenUtil.setupPieces( |
10c9010b BA |
141 | ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'], |
142 | { | |
143 | randomness: this.options["randomness"], | |
144 | diffCol: ['b'] | |
145 | } | |
146 | ); | |
7c038235 BA |
147 | return { |
148 | fen: s.b.join("") + "/pppppppp/8/8/8/8/PPPPPPPP/" + | |
149 | s.w.join("").toUpperCase(), | |
150 | o: {flags: "1111"} //Peach + Mario | |
151 | }; | |
91339921 BA |
152 | } |
153 | ||
8f57fbf2 | 154 | fen2board(f) { |
f8b43ef7 BA |
155 | return ( |
156 | f.charCodeAt() <= 90 | |
157 | ? "w" + f.toLowerCase() | |
158 | : (['w', 'd', 'e', 'm'].includes(f) ? "a" : "b") + f | |
159 | ); | |
160 | } | |
161 | ||
f8b43ef7 BA |
162 | setFlags(fenflags) { |
163 | // King can send shell? Queen can be invisible? | |
164 | this.powerFlags = { | |
8f57fbf2 BA |
165 | w: {k: false, q: false}, |
166 | b: {k: false, q: false} | |
f8b43ef7 | 167 | }; |
8f57fbf2 | 168 | for (let c of ['w', 'b']) { |
f8b43ef7 BA |
169 | for (let p of ['k', 'q']) { |
170 | this.powerFlags[c][p] = | |
171 | fenflags.charAt((c == "w" ? 0 : 2) + (p == 'k' ? 0 : 1)) == "1"; | |
172 | } | |
173 | } | |
174 | } | |
175 | ||
176 | aggregateFlags() { | |
177 | return this.powerFlags; | |
178 | } | |
179 | ||
180 | disaggregateFlags(flags) { | |
181 | this.powerFlags = flags; | |
182 | } | |
183 | ||
91339921 BA |
184 | getFlagsFen() { |
185 | return ['w', 'b'].map(c => { | |
186 | return ['k', 'q'].map(p => this.powerFlags[c][p] ? "1" : "0").join(""); | |
187 | }).join(""); | |
188 | } | |
189 | ||
8f57fbf2 | 190 | setOtherVariables(fenParsed) { |
5f08c59b | 191 | super.setOtherVariables(fenParsed); |
bc2bc396 | 192 | this.egg = null; |
3b641716 BA |
193 | // Change seed (after FEN generation!!) |
194 | // so that further calls differ between players: | |
554e3ad3 | 195 | Random.setSeed(Math.floor(19840 * Math.random())); |
f8b43ef7 BA |
196 | } |
197 | ||
5f08c59b BA |
198 | initReserves() { |
199 | this.reserve = {}; //to be filled later | |
200 | } | |
201 | ||
ca8a3993 BA |
202 | canStepOver(i, j) { |
203 | return ( | |
204 | this.board[i][j] == "" || | |
205 | ['i', V.EGG, V.MUSHROOM].includes(this.getPiece(i, j)) | |
206 | ); | |
207 | } | |
208 | ||
c7c2f41c | 209 | // For Toadette bonus |
ca8a3993 BA |
210 | canDrop([c, p], [i, j]) { |
211 | return ( | |
212 | ( | |
213 | this.board[i][j] == "" || | |
214 | this.getColor(i, j) == 'a' || | |
215 | this.getPiece(i, j) == 'i' | |
216 | ) | |
217 | && | |
218 | (p != "p" || (c == 'w' && i < this.size.x - 1) || (c == 'b' && i > 0)) | |
219 | ); | |
f8b43ef7 BA |
220 | } |
221 | ||
bc2bc396 | 222 | getPotentialMovesFrom([x, y]) { |
f8b43ef7 | 223 | let moves = []; |
3b641716 | 224 | const piece = this.getPiece(x, y); |
bc2bc396 | 225 | if (this.egg == "toadette") |
24872b22 BA |
226 | moves = this.getDropMovesFrom([x, y]); |
227 | else if (this.egg == "kingboo") { | |
3b641716 | 228 | const color = this.turn; |
616a8d7a | 229 | const oppCol = C.GetOppTurn(color); |
3b641716 | 230 | // Only allow to swap (non-immobilized!) pieces |
37481d1e BA |
231 | for (let i=0; i<this.size.x; i++) { |
232 | for (let j=0; j<this.size.y; j++) { | |
bc2bc396 BA |
233 | const colIJ = this.getColor(i, j); |
234 | const pieceIJ = this.getPiece(i, j); | |
235 | if ( | |
236 | (i != x || j != y) && | |
237 | ['w', 'b'].includes(colIJ) && | |
3b641716 | 238 | !Object.keys(V.IMMOBILIZE_DECODE).includes(pieceIJ) && |
bc2bc396 BA |
239 | // Next conditions = no pawn on last rank |
240 | ( | |
3b641716 | 241 | piece != 'p' || |
bc2bc396 BA |
242 | ( |
243 | (color != 'w' || i != 0) && | |
244 | (color != 'b' || i != this.size.x - 1) | |
245 | ) | |
246 | ) | |
247 | && | |
248 | ( | |
249 | pieceIJ != 'p' || | |
250 | ( | |
251 | (colIJ != 'w' || x != 0) && | |
252 | (colIJ != 'b' || x != this.size.x - 1) | |
253 | ) | |
254 | ) | |
255 | ) { | |
37481d1e | 256 | let m = this.getBasicMove([x, y], [i, j]); |
3b641716 BA |
257 | m.appear.push(new PiPo({x: x, y: y, p: pieceIJ, c: colIJ})); |
258 | m.kingboo = true; //avoid some side effects (bananas/bombs) | |
37481d1e BA |
259 | moves.push(m); |
260 | } | |
261 | } | |
262 | } | |
be3cb9d1 | 263 | } |
24872b22 BA |
264 | else { |
265 | // Normal case (including bonus daisy) | |
24872b22 BA |
266 | switch (piece) { |
267 | case 'p': | |
268 | moves = this.getPawnMovesFrom([x, y]); //apply promotions | |
269 | break; | |
270 | case 'q': | |
271 | moves = this.getQueenMovesFrom([x, y]); | |
272 | break; | |
273 | case 'k': | |
274 | moves = this.getKingMovesFrom([x, y]); | |
275 | break; | |
276 | case 'n': | |
277 | moves = this.getKnightMovesFrom([x, y]); | |
278 | break; | |
279 | case 'b': | |
280 | case 'r': | |
3b641716 | 281 | // Explicitely listing types to avoid moving immobilized piece |
6b9320bb | 282 | moves = super.getPotentialMovesOf(piece, [x, y]); |
24872b22 BA |
283 | break; |
284 | } | |
285 | } | |
24872b22 BA |
286 | return moves; |
287 | } | |
288 | ||
37481d1e | 289 | getPawnMovesFrom([x, y]) { |
f8b43ef7 | 290 | const color = this.turn; |
616a8d7a | 291 | const oppCol = C.GetOppTurn(color); |
be3cb9d1 BA |
292 | const shiftX = (color == 'w' ? -1 : 1); |
293 | const firstRank = (color == "w" ? this.size.x - 1 : 0); | |
f8b43ef7 | 294 | let moves = []; |
cc9fe4f1 | 295 | const frontPiece = this.getPiece(x + shiftX, y); |
f8b43ef7 | 296 | if ( |
be3cb9d1 | 297 | this.board[x + shiftX][y] == "" || |
f8b43ef7 | 298 | this.getColor(x + shiftX, y) == 'a' || |
cc9fe4f1 | 299 | frontPiece == 'i' |
f8b43ef7 | 300 | ) { |
7562d2c2 | 301 | moves.push(this.getBasicMove([x, y], [x + shiftX, y])); |
f8b43ef7 BA |
302 | if ( |
303 | [firstRank, firstRank + shiftX].includes(x) && | |
cc9fe4f1 | 304 | ![V.BANANA, V.BOMB].includes(frontPiece) && |
f8b43ef7 | 305 | ( |
7562d2c2 | 306 | this.board[x + 2 * shiftX][y] == "" || |
f8b43ef7 | 307 | this.getColor(x + 2 * shiftX, y) == 'a' || |
cc9fe4f1 | 308 | this.getPiece(x + 2 * shiftX, y) == 'i' |
f8b43ef7 BA |
309 | ) |
310 | ) { | |
7562d2c2 | 311 | moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y])); |
f8b43ef7 BA |
312 | } |
313 | } | |
314 | for (let shiftY of [-1, 1]) { | |
f5435757 | 315 | const nextY = this.getY(y + shiftY); |
f8b43ef7 | 316 | if ( |
f5435757 BA |
317 | nextY >= 0 && |
318 | nextY < this.size.y && | |
319 | this.board[x + shiftX][nextY] != "" && | |
f8b43ef7 | 320 | // Pawns cannot capture invisible queen this way! |
f5435757 BA |
321 | this.getPiece(x + shiftX, nextY) != 'i' && |
322 | ['a', oppCol].includes(this.getColor(x + shiftX, nextY)) | |
f8b43ef7 | 323 | ) { |
f5435757 | 324 | moves.push(this.getBasicMove([x, y], [x + shiftX, nextY])); |
f8b43ef7 BA |
325 | } |
326 | } | |
b9877ed2 | 327 | moves = super.pawnPostProcess(moves, color, oppCol); |
f5435757 | 328 | // Add mushroom on before-last square (+ potential segments) |
24872b22 | 329 | moves.forEach(m => { |
f5435757 BA |
330 | let [mx, my] = [x, y]; |
331 | if (Math.abs(m.end.x - m.start.x) == 2) | |
332 | mx = (m.start.x + m.end.x) / 2; | |
333 | m.appear.push(new PiPo({x: mx, y: my, c: 'a', p: 'm'})); | |
334 | if (mx != x && this.board[mx][my] != "") { | |
3b641716 | 335 | m.vanish.push(new PiPo({ |
f5435757 BA |
336 | x: mx, |
337 | y: my, | |
338 | c: this.getColor(mx, my), | |
339 | p: this.getPiece(mx, my) | |
3b641716 | 340 | })); |
24872b22 | 341 | } |
f5435757 BA |
342 | if (Math.abs(m.end.y - m.start.y) > 1) { |
343 | m.segments = [ | |
344 | [[x, y], [x, y]], | |
345 | [[m.end.x, m.end.y], [m.end.x, m.end.y]] | |
346 | ]; | |
347 | } | |
24872b22 | 348 | }); |
3b641716 | 349 | return moves; |
24872b22 BA |
350 | } |
351 | ||
352 | getKnightMovesFrom([x, y]) { | |
353 | // Add egg on initial square: | |
6b9320bb | 354 | return super.getPotentialMovesOf('n', [x, y]).map(m => { |
24872b22 BA |
355 | m.appear.push(new PiPo({p: "e", c: "a", x: x, y: y})); |
356 | return m; | |
357 | }); | |
358 | } | |
359 | ||
37481d1e | 360 | getQueenMovesFrom(sq) { |
6b9320bb | 361 | const normalMoves = super.getPotentialMovesOf('q', sq); |
f8b43ef7 BA |
362 | // If flag allows it, add 'invisible movements' |
363 | let invisibleMoves = []; | |
7562d2c2 | 364 | if (this.powerFlags[this.turn]['q']) { |
f8b43ef7 BA |
365 | normalMoves.forEach(m => { |
366 | if ( | |
367 | m.appear.length == 1 && | |
368 | m.vanish.length == 1 && | |
369 | // Only simple non-capturing moves: | |
370 | m.vanish[0].c != 'a' | |
371 | ) { | |
372 | let im = JSON.parse(JSON.stringify(m)); | |
cc9fe4f1 | 373 | im.appear[0].p = 'i'; |
24872b22 | 374 | im.noAnimate = true; |
f8b43ef7 BA |
375 | invisibleMoves.push(im); |
376 | } | |
377 | }); | |
378 | } | |
379 | return normalMoves.concat(invisibleMoves); | |
380 | } | |
381 | ||
37481d1e | 382 | getKingMovesFrom([x, y]) { |
6b9320bb | 383 | let moves = super.getPotentialMovesOf('k', [x, y]); |
f8b43ef7 | 384 | // If flag allows it, add 'remote shell captures' |
7562d2c2 | 385 | if (this.powerFlags[this.turn]['k']) { |
6b9320bb | 386 | let shellCaptures = super.getPotentialMovesOf('y', [x, y]); |
f5435757 BA |
387 | shellCaptures.forEach(sc => { |
388 | sc.shell = true; //easier play() | |
389 | sc.choice = 'z'; //to display in showChoices() | |
390 | // Fix move (Rifle style): | |
391 | sc.vanish.shift(); | |
392 | sc.appear.shift(); | |
f8b43ef7 | 393 | }); |
f5435757 | 394 | Array.prototype.push.apply(moves, shellCaptures); |
f8b43ef7 BA |
395 | } |
396 | return moves; | |
397 | } | |
398 | ||
7562d2c2 | 399 | play(move) { |
a2bb7e06 | 400 | const color = this.turn; |
616a8d7a | 401 | const oppCol = C.GetOppTurn(color); |
24872b22 | 402 | this.egg = move.egg; |
24872b22 BA |
403 | if (move.egg == "toadette") { |
404 | this.reserve = { w: {}, b: {} }; | |
405 | // Randomly select a piece in pawnPromotions | |
3b641716 BA |
406 | if (!move.toadette) |
407 | move.toadette = Random.sample(this.pawnPromotions); | |
408 | this.reserve[color][move.toadette] = 1; | |
24872b22 | 409 | this.re_drawReserve([color]); |
f8b43ef7 | 410 | } |
24872b22 BA |
411 | else if (Object.keys(this.reserve).length > 0) { |
412 | this.reserve = {}; | |
413 | this.re_drawReserve([color]); | |
f8b43ef7 | 414 | } |
24872b22 BA |
415 | if (move.shell) |
416 | this.powerFlags[color]['k'] = false; | |
cc9fe4f1 | 417 | else if (move.appear.length > 0 && move.appear[0].p == 'i') { |
24872b22 | 418 | this.powerFlags[move.appear[0].c]['q'] = false; |
cc9fe4f1 BA |
419 | if (color == this.playerColor) { |
420 | move.appear.push( | |
421 | new PiPo({x: move.start.x, y: move.start.y, c: color, p: '?'})); | |
422 | } | |
3b641716 BA |
423 | } |
424 | if (color == this.playerColor) { | |
425 | // Look for an immobilized piece of my color: it can now move | |
426 | for (let i=0; i<8; i++) { | |
427 | for (let j=0; j<8; j++) { | |
428 | if ((i != move.end.x || j != move.end.y) && this.board[i][j] != "") { | |
429 | const piece = this.getPiece(i, j); | |
430 | if ( | |
431 | this.getColor(i, j) == color && | |
432 | Object.keys(V.IMMOBILIZE_DECODE).includes(piece) | |
433 | ) { | |
434 | move.vanish.push(new PiPo({ | |
435 | x: i, y: j, c: color, p: piece | |
436 | })); | |
437 | move.appear.push(new PiPo({ | |
438 | x: i, y: j, c: color, p: V.IMMOBILIZE_DECODE[piece] | |
439 | })); | |
440 | } | |
f8b43ef7 BA |
441 | } |
442 | } | |
443 | } | |
3b641716 BA |
444 | // Also make opponent invisible queen visible again, if any |
445 | for (let i=0; i<8; i++) { | |
446 | for (let j=0; j<8; j++) { | |
447 | if ( | |
448 | this.board[i][j] != "" && | |
cc9fe4f1 | 449 | this.getColor(i, j) == oppCol |
3b641716 | 450 | ) { |
cc9fe4f1 | 451 | const pieceIJ = this.getPiece(i, j); |
6b9320bb BA |
452 | if ( |
453 | pieceIJ == 'i' && | |
454 | // Ensure that current move doesn't erase invisible queen | |
455 | move.appear.every(a => a.x != i || a.y != j) | |
456 | ) { | |
cc9fe4f1 BA |
457 | move.vanish.push(new PiPo({x: i, y: j, c: oppCol, p: 'i'})); |
458 | move.appear.push(new PiPo({x: i, y: j, c: oppCol, p: 'q'})); | |
459 | } | |
460 | else if (pieceIJ == '?') | |
461 | move.vanish.push(new PiPo({x: i, y: j, c: oppCol, p: '?'})); | |
3b641716 | 462 | } |
f8b43ef7 BA |
463 | } |
464 | } | |
465 | } | |
5f08c59b BA |
466 | this.playOnBoard(move); |
467 | super.postPlay(move); | |
468 | } | |
469 | ||
470 | playVisual(move, r) { | |
471 | super.playVisual(move, r); | |
bc2bc396 | 472 | if (move.egg) |
cc9fe4f1 | 473 | this.displayBonus(move); |
5f08c59b BA |
474 | } |
475 | ||
9ac67217 BA |
476 | buildMoveStack(move, r) { |
477 | const color = this.turn; | |
478 | if ( | |
479 | move.appear.length > 0 && | |
480 | move.appear[0].p == 'p' && | |
481 | ( | |
482 | (color == 'w' && move.end.x == 0) || | |
483 | (color == 'b' && move.end.x == this.size.x - 1) | |
484 | ) | |
485 | ) { | |
486 | // "Forgotten" promotion, which occurred after some effect | |
616a8d7a | 487 | let moves = super.pawnPostProcess([move], color, C.GetOppTurn(color)); |
9ac67217 BA |
488 | super.showChoices(moves, r); |
489 | } | |
490 | else | |
491 | super.buildMoveStack(move, r); | |
492 | } | |
493 | ||
5f08c59b | 494 | computeNextMove(move) { |
b9877ed2 BA |
495 | if (move.koopa) |
496 | return null; | |
5f08c59b BA |
497 | // Set potential random effects, so that play() is deterministic |
498 | // from opponent viewpoint: | |
499 | const endPiece = this.getPiece(move.end.x, move.end.y); | |
500 | switch (endPiece) { | |
501 | case V.EGG: | |
502 | move.egg = Random.sample(V.EGG_SURPRISE); | |
503 | move.next = this.getEggEffect(move); | |
504 | break; | |
505 | case V.MUSHROOM: | |
506 | move.next = this.getMushroomEffect(move); | |
507 | break; | |
508 | case V.BANANA: | |
509 | case V.BOMB: | |
510 | move.next = this.getBombBananaEffect(move, endPiece); | |
511 | break; | |
512 | } | |
513 | // NOTE: Chakart has also some side-effects: | |
514 | if ( | |
515 | !move.next && move.appear.length > 0 && | |
516 | !move.kingboo && !move.luigiEffect | |
517 | ) { | |
518 | const movingPiece = move.appear[0].p; | |
519 | if (['b', 'r'].includes(movingPiece)) { | |
520 | // Drop a banana or bomb: | |
521 | const bs = | |
522 | this.getRandomSquare([move.end.x, move.end.y], | |
523 | movingPiece == 'r' | |
524 | ? [[1, 1], [1, -1], [-1, 1], [-1, -1]] | |
525 | : [[1, 0], [-1, 0], [0, 1], [0, -1]], | |
526 | "freeSquare"); | |
527 | if (bs) { | |
528 | move.appear.push( | |
529 | new PiPo({ | |
530 | x: bs[0], | |
531 | y: bs[1], | |
532 | c: 'a', | |
533 | p: movingPiece == 'r' ? 'd' : 'w' | |
534 | }) | |
535 | ); | |
536 | if (this.board[bs[0]][bs[1]] != "") { | |
537 | move.vanish.push( | |
538 | new PiPo({ | |
539 | x: bs[0], | |
540 | y: bs[1], | |
541 | c: this.getColor(bs[0], bs[1]), | |
542 | p: this.getPiece(bs[0], bs[1]) | |
543 | }) | |
544 | ); | |
545 | } | |
546 | } | |
547 | } | |
548 | } | |
549 | } | |
550 | ||
551 | isLastMove(move) { | |
552 | return !move.next && !["daisy", "toadette", "kingboo"].includes(move.egg); | |
bc2bc396 BA |
553 | } |
554 | ||
3b641716 BA |
555 | // Helper to set and apply banana/bomb effect |
556 | getRandomSquare([x, y], steps, freeSquare) { | |
557 | let validSteps = steps.filter(s => this.onBoard(x + s[0], y + s[1])); | |
558 | if (freeSquare) { | |
559 | // Square to put banana/bomb cannot be occupied by a piece | |
560 | validSteps = validSteps.filter(s => { | |
561 | return ["", 'a'].includes(this.getColor(x + s[0], y + s[1])) | |
562 | }); | |
563 | } | |
564 | if (validSteps.length == 0) | |
565 | return null; | |
566 | const step = validSteps[Random.randInt(validSteps.length)]; | |
567 | return [x + step[0], y + step[1]]; | |
f8b43ef7 BA |
568 | } |
569 | ||
3b641716 BA |
570 | getEggEffect(move) { |
571 | const getRandomPiece = (c) => { | |
572 | let bagOfPieces = []; | |
573 | for (let i=0; i<this.size.x; i++) { | |
574 | for (let j=0; j<this.size.y; j++) { | |
9ac67217 BA |
575 | const pieceIJ = this.getPiece(i, j); |
576 | if ( | |
577 | this.getColor(i, j) == c && pieceIJ != 'k' && | |
578 | ( | |
579 | // The color will change, so pawns on first rank are ineligible | |
580 | pieceIJ != 'p' || | |
581 | (c == 'w' && i < this.size.x - 1) || (c == 'b' && i > 0) | |
582 | ) | |
583 | ) { | |
3b641716 | 584 | bagOfPieces.push([i, j]); |
9ac67217 | 585 | } |
3b641716 BA |
586 | } |
587 | } | |
588 | if (bagOfPieces.length >= 1) | |
589 | return Random.sample(bagOfPieces); | |
590 | return null; | |
591 | }; | |
592 | const color = this.turn; | |
593 | let em = null; | |
594 | switch (move.egg) { | |
595 | case "luigi": | |
596 | case "waluigi": | |
597 | // Change color of friendly or enemy piece, king excepted | |
616a8d7a BA |
598 | const oldColor = (move.egg == "waluigi" ? color : C.GetOppTurn(color)); |
599 | const newColor = C.GetOppTurn(oldColor); | |
3b641716 BA |
600 | const coords = getRandomPiece(oldColor); |
601 | if (coords) { | |
602 | const piece = this.getPiece(coords[0], coords[1]); | |
727f2e55 BA |
603 | if (coords[0] == move.start.x && coords[1] == move.start.y) { |
604 | // Moving piece change color: fix coords | |
605 | coords = [move.end.x, move.end.y]; | |
606 | } | |
3b641716 BA |
607 | em = new Move({ |
608 | appear: [ | |
609 | new PiPo({x: coords[0], y: coords[1], c: newColor, p: piece}) | |
610 | ], | |
611 | vanish: [ | |
612 | new PiPo({x: coords[0], y: coords[1], c: oldColor, p: piece}) | |
613 | ] | |
614 | }); | |
5f08c59b | 615 | em.luigiEffect = true; //avoid dropping bomb/banana by mistake |
3b641716 BA |
616 | } |
617 | break; | |
618 | case "bowser": | |
619 | em = new Move({ | |
620 | appear: [ | |
621 | new PiPo({ | |
622 | x: move.end.x, | |
623 | y: move.end.y, | |
624 | c: color, | |
625 | p: V.IMMOBILIZE_CODE[move.appear[0].p] | |
626 | }) | |
627 | ], | |
628 | vanish: [ | |
629 | new PiPo({ | |
630 | x: move.end.x, | |
631 | y: move.end.y, | |
632 | c: color, | |
633 | p: move.appear[0].p | |
634 | }) | |
635 | ] | |
636 | }); | |
637 | break; | |
638 | case "koopa": | |
130a166f | 639 | // Reverse move, if possible |
3b641716 | 640 | em = new Move({ |
130a166f | 641 | appear: [], |
3b641716 BA |
642 | vanish: [ |
643 | new PiPo({ | |
644 | x: move.end.x, y: move.end.y, c: color, p: move.appear[0].p | |
645 | }) | |
130a166f BA |
646 | ], |
647 | end: {x: move.start.x, y: move.start.y} //may be irrelevant | |
3b641716 | 648 | }); |
130a166f BA |
649 | em.koopa = true; //avoid applying effect |
650 | if (move.vanish.length == 0) | |
651 | // After toadette+drop, just erase piece | |
652 | break; | |
653 | em.appear.push( | |
654 | new PiPo({ | |
655 | x: move.start.x, y: move.start.y, c: color, p: move.appear[0].p | |
656 | }) | |
657 | ); | |
3b641716 BA |
658 | if (this.board[move.start.x][move.start.y] != "") { |
659 | // Pawn or knight let something on init square | |
660 | em.vanish.push(new PiPo({ | |
661 | x: move.start.x, | |
662 | y: move.start.y, | |
663 | c: 'a', | |
664 | p: this.getPiece(move.start.x, move.start.y) | |
665 | })); | |
666 | } | |
667 | break; | |
668 | case "chomp": | |
669 | // Eat piece | |
670 | em = new Move({ | |
671 | appear: [], | |
672 | vanish: [ | |
673 | new PiPo({ | |
674 | x: move.end.x, y: move.end.y, c: color, p: move.appear[0].p | |
675 | }) | |
676 | ], | |
677 | end: {x: move.end.x, y: move.end.y} | |
678 | }); | |
679 | break; | |
680 | } | |
681 | if (em && move.egg != "koopa") | |
682 | em.noAnimate = true; //static move | |
683 | return em; | |
f8b43ef7 BA |
684 | } |
685 | ||
24872b22 | 686 | getMushroomEffect(move) { |
fc12475f BA |
687 | if ( |
688 | typeof move.start.x == "string" || //drop move (toadette) | |
689 | ['b', 'r', 'q'].includes(move.vanish[0].p) //slider | |
690 | ) { | |
cc9fe4f1 | 691 | return null; |
37481d1e | 692 | } |
fc12475f BA |
693 | let step = [move.end.x - move.start.x, move.end.y - move.start.y]; |
694 | if (Math.abs(step[0]) == 2 && Math.abs(step[1]) == 0) | |
695 | // Pawn initial 2-squares move: normalize step | |
696 | step[0] /= 2; | |
24872b22 | 697 | const nextSquare = [move.end.x + step[0], move.end.y + step[1]]; |
24872b22 | 698 | let nextMove = null; |
fc12475f BA |
699 | if ( |
700 | this.onBoard(nextSquare[0], nextSquare[1]) && | |
701 | ( | |
702 | this.board[nextSquare[0]][nextSquare[1]] == "" || | |
703 | this.getColor(nextSquare[0], nextSquare[1]) == 'a' | |
704 | ) | |
705 | ) { | |
bc5d61a7 | 706 | this.playOnBoard(move); //HACK for getBasicMove() |
24872b22 | 707 | nextMove = this.getBasicMove([move.end.x, move.end.y], nextSquare); |
bc5d61a7 | 708 | this.undoOnBoard(move); |
24872b22 | 709 | } |
24872b22 | 710 | return nextMove; |
37481d1e BA |
711 | } |
712 | ||
3b641716 BA |
713 | getBombBananaEffect(move, item) { |
714 | const steps = item == V.BANANA | |
715 | ? [[1, 0], [-1, 0], [0, 1], [0, -1]] | |
716 | : [[1, 1], [1, -1], [-1, 1], [-1, -1]]; | |
717 | const nextSquare = this.getRandomSquare([move.end.x, move.end.y], steps); | |
718 | this.playOnBoard(move); //HACK for getBasicMove() | |
719 | const res = this.getBasicMove([move.end.x, move.end.y], nextSquare); | |
720 | this.undoOnBoard(move); | |
721 | return res; | |
722 | } | |
723 | ||
cc9fe4f1 | 724 | displayBonus(move) { |
98d14451 | 725 | super.displayMessage(null, move.egg, "bonus-text", 2000); |
3b641716 BA |
726 | } |
727 | ||
728 | atLeastOneMove() { | |
729 | return true; | |
730 | } | |
731 | ||
732 | filterValid(moves) { | |
733 | return moves; | |
734 | } | |
735 | ||
5f08c59b BA |
736 | // Kingboo bonus can be animated better: |
737 | customAnimate(move, segments, cb) { | |
738 | if (!move.kingboo) | |
739 | return 0; | |
740 | super.animateMoving(move.end, move.start, null, | |
741 | segments.reverse().map(s => s.reverse()), cb); | |
742 | return 1; | |
be3cb9d1 | 743 | } |
f8b43ef7 BA |
744 | |
745 | }; |