Debian Codegraph @

source/code/BlockGame.cpp

raw symbols SCIP
1 /*
2 ===========================================================================
3 blockattack - Block Attack - Rise of the Blocks
4 Copyright (C) 2005-2012 Poul Sander
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see http://www.gnu.org/licenses/
18
19 Source information and contacts persons can be found at
20 http://blockattack.net
21 ===========================================================================
22 */
23
24 //Some definitions
25 //The game is divided in frames. FALLTIME means the blocks will fall one block every FRAMELENGTH*FALLTIME millisecond
26 #define FRAMELENGTH 50
27 #define HANGTIME 40
28 #define FALLTIME 20
29 //Don't change the following, they are fundamental and later some functions are hardcoded
30 #define BLOCKFALL 10000
31 #define GARBAGE 1000000
32 #define CHAINPLACE 10000000
33
34 // The game uses a very special base-10 pack system
35 // int 999999999999
36 // YYYYYGWBHTTC
37 // YYYY = Chain
38 // G = Garbage, 1= NORMAL, 2=GRAY
39 // W = Waiting (A bomb is on it)
40 // B = 1 if the block is falling
41 // H = Block is hanging after garbage (Get Ready text on it)
42 // TT = Time (in steps) until something happens
43 // C = color
44
45 static bool block_isFalling(int block) {
46 return (block/BLOCKFALL)%10;
47 }
48
49 static void block_setFalling(int& block, bool value) {
50 if (value) {
51 if (!block_isFalling(block)) {
52 block += BLOCKFALL;
53 }
54 }
55 else {
56 if (block_isFalling(block)) {
57 block -= BLOCKFALL;
58 }
59 }
60 }
61
62 #include "BlockGame.hpp"
63 #include "puzzlehandler.hpp"
64 #include "stageclearhandler.hpp"
65 #include <sstream>
66 #include <deque>
67
68
69 //Function to convert numbers to string (2 diget)
70 static std::string itoa2(int num) {
71 char buffer[20];
72 snprintf(buffer, sizeof(buffer), "%02i", num);
73 return buffer;
74 }
75
76 /**
77 * This function tells how long the game may pause the rise of the blocks.
78 * This decreases as the game progresses.
79 * @param stops In out variable. If larger than the max allowed
80 * @param ticks Ticks since the game was started
81 * @param gameStartedAt Tick the game was started at.
82 */
83 static void capMaxStops(int& stops, unsigned int ticks, unsigned int gameStartedAt) {
84 if (ticks < gameStartedAt || stops == 0) {
85 //Game not started or not stops, do nothing
86 return;
87 }
88 unsigned int gameTime = gameStartedAt - ticks;
89
90 int maxSeconds = 10-( (gameTime/1000) % 30 );
91 if (maxSeconds < 3) {
92 maxSeconds = 1;
93 }
94 int cappedStops = maxSeconds * 1000;
95 if (stops > cappedStops) {
96 stops = cappedStops;
97 }
98 }
99
100 stageButton stageButtonStatus;
101
102 static std::stringstream ss; //Used for internal formatting
103
104 ////////////////////////////////////////////////////////////////////////////////
105 //The BloackGame class represents a board, score, time etc. for a single player/
106 ////////////////////////////////////////////////////////////////////////////////
107 int BlockGame::rand2() {
108 nextRandomNumber = nextRandomNumber*1103515245 + 12345;
109 return ((int)(nextRandomNumber/65536)) % 32768;
110 }
111
112 int BlockGame::firstUnusedChain() {
113 bool found=false;
114 int i = 0;
115 while (!found) {
116 if (!chainUsed[++i]) {
117 found=true;
118 }
119 if (i>NUMBEROFCHAINS-2) {
120 found=true;
121 }
122 }
123 return i;
124 }
125
126 //Constructor
127 BlockGame::BlockGame() {
128 AI_MoveSpeed=100;
129 baseSpeed = 0.5; //All other speeds are relative to this
130 speed = baseSpeed;
131 speedLevel = 1;
132 ticks = 0;
133 gameStartedAt = ticks;
134 gameEndedAfter = 0;
135 pushedPixelAt = gameStartedAt;
136 nextGarbageNumber = 10;
137 basicBlockVariants = 6;
138 handicap=0;
139 for (int i=0; i<7; i++) {
140 for (int j=0; j<30; j++) {
141 board[i][j] = -1;
142 }
143 }
144 lastCounter = -1; //To prevent the final chunk to be played when stating the program
145 } //Constructor
146
147
148 void BlockGame::setGameSpeed(int globalSpeedLevel) {
149 switch (globalSpeedLevel) {
150 case 0:
151 baseSpeed=0.5;
152 break;
153 case 1:
154 baseSpeed=0.1;
155 break;
156 case 2:
157 baseSpeed=0.07;
158 break;
159 case 3:
160 baseSpeed=0.04;
161 break;
162 case 4:
163 baseSpeed=0.015;
164 break;
165 default:
166 baseSpeed=0.005;
167 break;
168 };
169 }
170
171 void BlockGame::setHandicap(int globalHandicap) {
172 handicap=1000*((int)globalHandicap);
173 }
174
175 //Set the move speed of the AI based on the aiLevel parameter
176 //Also enables AI
177 void BlockGame::setAIlevel(int aiLevel) {
178 AI_MoveSpeed=120-(20*(aiLevel-3));
179 };
180
181 int BlockGame::getAIlevel() const {
182 return (120-AI_MoveSpeed)/20+3;
183 }
184
185 int BlockGame::GetScore() const {
186 return score;
187 }
188
189 int BlockGame::GetHandicap() const {
190 return handicap;
191 }
192
193 bool BlockGame::isGameOver() const {
194 return bGameOver;
195 }
196
197 int BlockGame::GetGameStartedAt() const {
198 return gameStartedAt;
199 }
200
201 int BlockGame::GetGameEndedAt() const {
202 return gameEndedAfter;
203 }
204
205 bool BlockGame::isTimeTrial() const {
206 return timetrial;
207 }
208
209 bool BlockGame::isStageClear() const {
210 return stageClear;
211 }
212
213 bool BlockGame::isVsMode() const {
214 return vsMode;
215 }
216
217 bool BlockGame::isPuzzleMode() const {
218 return puzzleMode;
219 }
220
221 int BlockGame::GetLinesCleared() const {
222 return linesCleared;
223 }
224
225 int BlockGame::GetStageClearLimit() const {
226 return stageClearLimit;
227 }
228
229 int BlockGame::GetChains() const {
230 return chain;
231 }
232
233 int BlockGame::GetPixels() const {
234 return pixels;
235 }
236
237 int BlockGame::GetSpeedLevel() const {
238 return speedLevel;
239 }
240
241 int BlockGame::GetTowerHeight() const {
242 return TowerHeight;
243 }
244
245 int BlockGame::GetCursorX() const {
246 return cursorx;
247 }
248
249 int BlockGame::GetCursorY() const {
250 return cursory;
251 }
252
253 void BlockGame::MoveCursorTo(int x, int y) {
254 cursorx = x;
255 cursory = y;
256 }
257
258 bool BlockGame::GetIsWinner() const {
259 return hasWonTheGame;
260 }
261
262 void BlockGame::NewGame(const BlockGameStartInfo& s) {
263 replayInfo.startInfo = s;
264 replayInfo.actions.clear();
265 this->recordStats = s.recordStats;
266 if (s.AI) {
267 recordStats = false;
268 }
269 NewGameInternal(s.ticks);
270 basicBlockVariants = s.basicBlockVariants;
271 if (s.timeTrial) {
272 timetrial = true;
273 putStartBlocks();
274 }
275 if (s.stageClear) {
276 if (s.level > -1) {
277 stageClear = true;
278 Level = s.level;
279 Stats::getInstance()->addOne("PlayedStageLevel"+itoa2(s.level));
280 stageClearLimit = 30+(Level%6)*10;
281 baseSpeed = 0.5/((double)(Level*0.5)+1.0);
282 speed = baseSpeed;
283 }
284 }
285 if (s.puzzleMode) {
286 if (s.level>-1) {
287 puzzleMode = true;
288 Level = s.level;
289 MovesLeft = PuzzleNumberOfMovesAllowed(Level);
290 for (int i=0; i<6; i++) {
291 for (int j=0; j<12; j++) {
292 board[i][j+1] = PuzzleGetBrick(Level,i,j);
293 }
294 }
295 baseSpeed = 100000;
296 speed = 100000;
297
298 //Now push the blines up
299 for (int i=19; i>0; i--) {
300 for (int j=0; j<6; j++) {
301 board[j][i] = board[j][i-1];
302 }
303 }
304 for (int j=0; j<6; j++) {
305 board[j][0] = 5;
306 }
307 }
308 this->singlePuzzle = s.singlePuzzle;
309 }
310 if (s.vsMode) {
311 vsMode = true;
312 AI_Enabled = s.AI;
313 if (recordStats) {
314 Stats::getInstance()->addOne("VSgamesStarted");
315 }
316 if (s.vsAI) {
317 setAIlevel(s.level);
318 vsAI = true;
319 }
320 if (AI_Enabled) {
321 name = "CPU";
322 setAIlevel(s.level);
323 }
324 putStartBlocks();
325 }
326 if (s.startBlocks >= 0) {
327 putStartBlocks(s.startBlocks);
328 }
329 if (s.handicap > 0) {
330 setHandicap(s.handicap);
331 }
332 if (s.gameSpeed > 0) {
333 setGameSpeed(s.gameSpeed);
334 speed = baseSpeed;
335 }
336 }
337
338 //Instead of creating new object new game is called, to prevent memory leaks
339 void BlockGame::NewGameInternal( unsigned int ticks) {
340 this->ticks = ticks;
341 stageButtonStatus = SBdontShow;
342 nrFellDown = 0;
343 nrPushedPixel = 0;
344 nrStops = 0;
345 cursorx = 2;
346 cursory = 3;
347 stop = 0;
348 pixels = 0;
349 score = 0;
350 bGameOver = false;
351 bDraw = false;
352 timetrial = false;
353 stageClear = false;
354 linesCleared = 0;
355 hasWonTheGame = false;
356 vsMode = false;
357 puzzleMode = false;
358 combo=0;
359 chain=0;
360 AI_Enabled = false;
361 baseSpeed= 0.5;
362 speed = baseSpeed;
363 speedLevel = 1;
364 gameStartedAt = ticks+3000;
365 pushedPixelAt = gameStartedAt;
366 nextGarbageNumber = 10;
367 handicap=0;
368 for (int i=0; i<7; i++) {
369 for (int j=0; j<30; j++) {
370 board[i][j] = -1;
371 }
372 }
373 for (int i=0; i<NUMBEROFCHAINS; i++) {
374 chainUsed[i]=false;
375 chainSize[i] = 0;
376 }
377 lastAImove = ticks+3000;
378 } //NewGame
379
380 void BlockGame::FinalizeBlockGameInfo() {
381 replayInfo.extra.name = name;
382 replayInfo.extra.score = score;
383 replayInfo.extra.seconds = gameEndedAfter;
384 }
385
386 //Prints "winner" and ends game
387 void BlockGame::setPlayerWon() {
388 if (!bGameOver || !hasWonTheGame) {
389 gameEndedAfter = ticks-gameStartedAt; //We game ends now!
390 if (recordStats) {
391 TimeHandler::addTime("playTime",TimeHandler::ms2ct(gameEndedAfter));
392 }
393 bGameOver = true;
394 PlayerWonEvent();
395 if (recordStats) {
396 Stats::getInstance()->addOne("totalWins");
397 if (vsAI) {
398 //We have defeated an AI
399 Stats::getInstance()->addOne("defeatedAI"+std::to_string(getAIlevel()));
400 }
401 }
402 if (AI_Enabled && vsAI) {
403 //The AI have defeated a human player
404 Stats::getInstance()->addOne("defeatedByAI"+std::to_string(getAIlevel()));
405 }
406 FinalizeBlockGameInfo();
407 }
408 hasWonTheGame = true;
409 }
410
411 //Prints "draw" and ends the game
412 void BlockGame::setDraw() {
413 bGameOver = true;
414 if (recordStats) {
415 TimeHandler::addTime("playTime",TimeHandler::ms2ct(gameEndedAfter));
416 }
417 hasWonTheGame = false;
418 bDraw = true;
419 DrawEvent();
420 if (recordStats) {
421 Stats::getInstance()->addOne("totalDraws");
422 }
423 FinalizeBlockGameInfo();
424 }
425
426
427 //Test if LineNr is an empty line, returns false otherwise.
428 bool BlockGame::LineEmpty(int lineNr) const {
429 bool empty = true;
430 for (int i = 0; i <7; i++) {
431 if (board[i][lineNr] != -1) {
432 empty = false;
433 }
434 }
435 return empty;
436 }
437
438 //Test if the entire board is empty (used for Puzzles)
439 bool BlockGame::BoardEmpty() const {
440 bool empty = true;
441 for (int i=0; i<6; i++) {
442 for (int j=1; j<13; j++) {
443 if (board[i][j] != -1) {
444 empty = false;
445 }
446 }
447 }
448 return empty;
449 }
450
451 //Anything that the user can't move? In that case Game Over cannot occur
452 bool BlockGame::hasStaticContent() const {
453 for (int i=0; i<6; i++) {
454 for (int j=1; j<13; j++) {
455 if (board[i][j] >= 10000000) { //Higher than this means combos (garbage is static, but the stack is static but nothing to do about it)
456 return true; //They are static
457 }
458 }
459 }
460 return false; //Return false if no static object found
461 }
462
463 void BlockGame::putSampleBlocks() {
464 //row 0 (hidden):
465 board[0][0]=0;
466 board[1][0]=1;
467 board[2][0]=2;
468 board[3][0]=3;
469 board[4][0]=4;
470 board[5][0]=5;
471 //row 1:
472 board[0][1]=0;
473 board[1][1]=1;
474 board[2][1]=2;
475 board[3][1]=3;
476 board[4][1]=4;
477 board[5][1]=5;
478 //row 2+:
479 board[0][2]=6;
480 board[0][3]=1;
481 board[0][4]=2;
482 for ( size_t i = 0; i < 4; i++) {
483 board[i][5]=GARBAGE;
484 }
485 for (size_t i = 0; i < 6; i++) {
486 for (size_t j = 6; j < 9; j++) {
487 board[i][j] = GARBAGE+1;
488 }
489 for (size_t j = 9; j < 12; j++) {
490 board[i][j] = GARBAGE+2;
491 }
492 }
493 }
494
495 void BlockGame::putStartBlocks() {
496 putStartBlocks(time(0));
497 }
498
499 void BlockGame::putStartBlocks(int n) {
500 for (int i=0; i<7; i++) {
501 for (int j=0; j<30; j++) {
502 board[i][j] = -1;
503 }
504 }
505 nextRandomNumber = n;
506 int choice = rand2()%3; //Pick a random layout
507 switch (choice) {
508 case 0:
509 //row 0:
510 board[0][0]=1;
511 board[1][0]=0;
512 board[2][0]=4;
513 board[3][0]=3;
514 board[4][0]=3;
515 board[5][0]=4;
516 //row 1:
517 board[0][1]=1;
518 board[1][1]=4;
519 board[2][1]=2;
520 board[3][1]=0;
521 board[4][1]=4;
522 board[5][1]=4;
523 //row 2:
524 board[0][2]=2;
525 board[1][2]=3;
526 board[2][2]=0;
527 board[3][2]=4;
528 board[4][2]=1;
529 board[5][2]=1;
530 //row 3:
531 board[0][3]=3;
532 board[1][3]=2;
533 board[2][3]=3;
534 board[3][3]=1;
535 board[4][3]=0;
536 board[5][3]=4;
537 //row 4:
538 board[0][4]=2;
539 board[1][4]=3;
540 board[2][4]=3;
541 board[3][4]=1;
542 board[4][4]=4;
543 board[5][4]=0;
544 //row 5:
545 board[0][5]=-1;
546 board[1][5]=4;
547 board[2][5]=4;
548 board[3][5]=-1;
549 board[4][5]=1;
550 board[5][5]=-1;
551 break;
552 case 1:
553 //row 0:
554 board[0][0]=3;
555 board[1][0]=1;
556 board[2][0]=0;
557 board[3][0]=0;
558 board[4][0]=4;
559 board[5][0]=2;
560 //row 1:
561 board[0][1]=3;
562 board[1][1]=1;
563 board[2][1]=-1;
564 board[3][1]=1;
565 board[4][1]=4;
566 board[5][1]=2;
567 //row 2:
568 board[0][2]=2;
569 board[1][2]=-1;
570 board[2][2]=-1;
571 board[3][2]=4;
572 board[4][2]=0;
573 board[5][2]=3;
574 //row 3:
575 board[0][3]=2;
576 board[5][3]=3;
577 break;
578 default:
579 //row 0:
580 board[0][0]=4;
581 board[1][0]=1;
582 board[2][0]=2;
583 board[3][0]=0;
584 board[4][0]=1;
585 board[5][0]=3;
586 //row 1:
587 board[0][1]=4;
588 board[1][1]=3;
589 board[2][1]=2;
590 board[3][1]=1;
591 board[4][1]=0;
592 board[5][1]=2;
593 //row 2:
594 board[0][2]=2;
595 board[1][2]=4;
596 board[2][2]=-1;
597 board[3][2]=0;
598 board[4][2]=1;
599 board[5][2]=3;
600 //row 3:
601 board[0][3]=4;
602 board[1][3]=2;
603 board[2][3]=-1;
604 board[3][3]=1;
605 board[4][3]=0;
606 board[5][3]=2;
607 //row 4:
608 board[0][4]=4;
609 board[1][4]=2;
610 board[2][4]=-1;
611 board[3][4]=0;
612 board[4][4]=1;
613 board[5][4]=-1;
614 break;
615 };
616 }
617
618 //decreases hang for all hanging blocks and wait for waiting blocks
619 void BlockGame::ReduceStuff() {
620 int howMuchHang = (ticks - FRAMELENGTH*hangTicks)/FRAMELENGTH;
621 if (howMuchHang>0) {
622 for (int i=0; i<7; i++) {
623 for (int j=0; j<30; j++) {
624 if ((board[i][j]/BLOCKHANG)%10==1) {
625 int hangNumber = (board[i][j]/10)%100;
626 if (hangNumber<=howMuchHang) {
627 board[i][j]-=BLOCKHANG;
628 board[i][j]-=hangNumber*10;
629 }
630 else {
631 board[i][j]-=10*howMuchHang;
632 }
633 }
634 if ((board[i][j]/BLOCKWAIT)%10==1) {
635 int hangNumber = (board[i][j]/10)%100;
636 if (hangNumber<=howMuchHang) {
637 //The blocks must be cleared
638 board[i][j]-=hangNumber*10;
639 }
640 else {
641 board[i][j]-=10*howMuchHang;
642 }
643 }
644 }
645 }
646 }
647 hangTicks+=howMuchHang;
648 }
649
650 //Creates garbage using a given wide and height
651 bool BlockGame::CreateGarbage(int wide, int height) {
652 {
653 if (wide>6) {
654 wide = 6;
655 }
656 if (height>12) {
657 height = 12;
658 }
659 int startPosition = 12;
660 while ( startPosition <= 29 && !LineEmpty(startPosition)) {
661 startPosition++;
662 }
663 if (startPosition >= 29) {
664 return false; //failed to place blocks
665 }
666 if (29-startPosition<height) {
667 return false; //not enough space
668 }
669 int start = 0;
670 int end = 6;
671 if (bGarbageFallLeft) {
672 start=0;
673 end=start+wide;
674 bGarbageFallLeft = false;
675 }
676 else {
677 start=6-wide;
678 end = 6;
679 bGarbageFallLeft = true;
680 }
681 for (int i = startPosition; i <startPosition+height; i++) {
682 for (int j = start; j < end; j++) {
683 board[j][i] = 1000000+nextGarbageNumber;
684 }
685 }
686 nextGarbageNumber++;
687 if (nextGarbageNumber>999999) {
688 nextGarbageNumber = 10;
689 }
690 return true;
691 }
692 return false;
693 }
694
695 //Creates garbage using a given wide and height
696 bool BlockGame::CreateGreyGarbage() {
697 int startPosition = 12;
698 while ( startPosition <= 29 && !LineEmpty(startPosition) ) {
699 startPosition++;
700 }
701 if (startPosition >= 29) {
702 return false; //failed to place blocks
703 }
704 int start = 0;
705 int end = 6;
706 for (int i = startPosition; i <startPosition+1; i++) {
707 for (int j = start; j < end; j++) {
708 board[j][i] = 2*1000000+nextGarbageNumber;
709 }
710 }
711 nextGarbageNumber++;
712 if (nextGarbageNumber>999999) {
713 nextGarbageNumber = 10;
714 }
715 return true;
716 }
717
718
719 //Clears garbage, must take one the lower left corner!
720 int BlockGame::GarbageClearer(int x, int y, int number, bool aLineToClear, int chain) {
721 if ((board[x][y])%1000000 != number) {
722 return -1;
723 }
724 if (aLineToClear) {
725 board[x][y] = this->rand2() % basicBlockVariants;
726 board[x][y] += 10*HANGTIME+BLOCKHANG+CHAINPLACE*chain;
727 }
728 garbageToBeCleared[x][y] = false;
729 GarbageClearer(x+1, y, number, aLineToClear, chain);
730 GarbageClearer(x, y+1, number, false, chain);
731 return 1;
732 }
733
734 //Marks garbage that must be cleared
735 int BlockGame::GarbageMarker(int x, int y) {
736 if ((x>6)||(x<0)||(y<0)||(y>29)) {
737 return -1;
738 }
739 if (((board[x][y])/1000000 == 1)&&(garbageToBeCleared[x][y] == false)) {
740 garbageToBeCleared[x][y] = true;
741 //Float fill
742 GarbageMarker(x-1, y);
743 GarbageMarker(x+1, y);
744 GarbageMarker(x, y-1);
745 GarbageMarker(x, y+1);
746 }
747 return 1;
748 }
749
750 int BlockGame::FirstGarbageMarker(int x, int y) {
751 if ((x>6)||(x<0)||(y<0)||(y>29)) {
752 return -1;
753 }
754 if (((board[x][y])/1000000 == 2)&&(garbageToBeCleared[x][y] == false)) {
755 for (int i=0; i<6; i++) {
756 garbageToBeCleared[i][y] = true;
757 }
758 }
759 else if (((board[x][y])/1000000 == 1)&&(garbageToBeCleared[x][y] == false)) {
760 garbageToBeCleared[x][y] = true;
761 //Float fill
762 GarbageMarker(x-1, y);
763 GarbageMarker(x+1, y);
764 GarbageMarker(x, y-1);
765 GarbageMarker(x, y+1);
766 }
767 return 1;
768 }
769
770 //Clear Blocks if 3 or more is alligned (naive implemented)
771 void BlockGame::ClearBlocks() {
772
773 bool toBeCleared[7][30]; //true if blok must be removed
774
775 int previus=-1; //the last block checked
776 int combo=0;
777 for (int i=0; i<30; i++)
778 for (int j=0; j<7; j++) {
779 toBeCleared[j][i] = false;
780 garbageToBeCleared[j][i] = false;
781 }
782 for (int i=0; i<7; i++) {
783 bool faaling = false;
784 for (int j=0; j<30; j++) {
785 if ((faaling)&&(board[i][j]>-1)&&(board[i][j]%10000000<7)) {
786 block_setFalling(board[i][j], true);
787 }
788 if ((!faaling)&&((board[i][j]/BLOCKFALL)%10==1)) {
789 block_setFalling(board[i][j], false);
790 }
791 if (!((board[i][j]>-1)&&(board[i][j]%10000000<7))) {
792 faaling=true;
793 }
794 if (((board[i][j]/1000000)%10==1)||((board[i][j]/1000000)%10==2)||((board[i][j]/BLOCKHANG)%10==1)||((board[i][j]/BLOCKWAIT)%10==1)) {
795 faaling = false;
796 }
797 }
798 }
799
800
801 for (int j=0; j<7; j++) {
802 previus = -1;
803 combo=0;
804
805 for (int i=1; i<30; i++) {
806 if ((board[j][i]>-1)&&(board[j][i]%10000000<7)) {
807 if (board[j][i]%10000000 == previus) {
808 combo++;
809 }
810 else {
811 if (combo>2) {
812 for (int k = i-combo; k<i; k++) {
813 toBeCleared[j][k] = true;
814 }
815 }
816 combo=1;
817 previus = board[j][i]%10000000;
818 }
819 } //if board
820 else {
821 if (combo>2) {
822 for (int k = i-combo; k<i; k++) {
823 toBeCleared[j][k] = true;
824 }
825 }
826 combo = 0;
827 previus = -1;
828 }
829
830 } //for i
831 } //for j
832
833 chain = 0;
834 for (int i=0; i<6; i++) {
835 for (int j=0; j<30; j++) {
836 //Clears blocks marked for clearing
837 int temp=board[i][j];
838 if (1==((temp/BLOCKWAIT)%10)) {
839 if (((temp/10)%100)==0) {
840 if (chainSize[chain]<chainSize[board[i][j]/10000000]) {
841 chain = board[i][j]/10000000;
842 }
843
844 AddBall(i, j, true, board[i][j]%10);
845 AddBall(i, j, false, board[i][j]%10);
846 AddExplosion(i, j);
847 board[i][j]=-2;
848 }
849 }
850 }
851 }
852 for (int i=0; i<7; i++) {
853 bool setChain=false;
854 for (int j=0; j<30; j++) {
855 if (board[i][j]==-1) {
856 setChain=false;
857 }
858 if (board[i][j]==-2) {
859 board[i][j]=-1;
860 setChain=true;
861 BlockPopEvent();
862 }
863 if (board[i][j]!=-1) {
864 if ((setChain)&&((board[i][j]/GARBAGE)%10!=1)&&((board[i][j]/GARBAGE)%10!=2)) {
865 board[i][j]=((board[i][j]%CHAINPLACE)+CHAINPLACE*chain);
866 }
867 }
868 }
869 }
870 int startvalue;
871 if (pixels == 0) {
872 startvalue=1;
873 }
874 else {
875 startvalue=0;
876 }
877 for (int i=startvalue; i<30; i++) {
878 previus=-1;
879 combo=0;
880 for (int j=0; j<7; j++) {
881 if (((board[j][i]>-1)&&(board[j][i]%10000000<7))) {
882 if (board[j][i]%10000000 == previus) {
883 combo++;
884 }
885 else {
886 if (combo>2) {
887 for (int k = j-combo; k<j; k++) {
888 toBeCleared[k][i] = true;
889 }
890 }
891 combo=1;
892 previus = board[j][i]%10000000;
893 }
894 } //if board
895 else {
896 if (combo>2) {
897 for (int k = j-combo; k<j; k++) {
898 toBeCleared[k][i] = true;
899 }
900 }
901 combo = 0;
902 previus = -1;
903 }
904
905 } //for j
906 } //for i
907
908 combo = 0;
909 chain = 0;
910 int grey = 0;
911 for (int i=0; i<30; i++) {
912 for (int j=0; j<6; j++) {
913 if (toBeCleared[j][i]) {
914 //see if any garbage is around:
915 FirstGarbageMarker(j-1, i);
916 FirstGarbageMarker(j+1, i);
917 FirstGarbageMarker(j, i-1);
918 FirstGarbageMarker(j, i+1);
919 //that is checked now :-)
920 if (board[j][i]%10000000==6) {
921 grey++;
922 }
923 if ((vsMode) && (grey>2) && (board[j][i]%10000000==6)) {
924 GarbageStruct s;
925 s.setGarbage(6, 1, true);
926 this->garbageSendQueue.push_back(s);
927 }
928 if ((board[j][i]>-1)&&(board[j][i]%10000000<7)) {
929 board[j][i]+=BLOCKWAIT+10*FALLTIME;
930 }
931
932 if (chainSize[board[j][i]/10000000]>chainSize[chain]) {
933 chain=board[j][i]/10000000;
934 }
935 combo++;
936 stop+=140*combo;
937 score +=10;
938 if (combo>3) {
939 score+=3*combo; //More points if more cleared simontanously
940 }
941 }
942 }
943 }
944 score+=chainSize[chain]*100;
945 if (chain==0) {
946 chain=firstUnusedChain();
947 chainSize[chain]=0;
948 chainUsed[chain]=true;
949 }
950 chainSize[chain]++;
951 for (int i=0; i<30; i++) {
952 for (int j=0; j<6; j++) {
953 if (toBeCleared[j][i]) {
954 board[j][i]=(board[j][i]%10000000)+chain*10000000;
955 }
956 }
957 }
958
959 {
960 //This is here we add text to screen!
961 bool dead = false;
962 for (int i=29; i>=0; i--) {
963 for (int j=0; j<6; j++) {
964 if (toBeCleared[j][i]) {
965 if (!dead) {
966 dead=true;
967 if (chainSize[chain]>1) {
968 AddText(j, i, chainSize[chain], 1000);
969 }
970 }
971 }
972 }
973 }
974 } //This was there text was added
975
976 if (vsMode) {
977 GarbageStruct s;
978 switch (combo) {
979 case 0:
980 case 1:
981 case 2:
982 case 3:
983 break;
984 case 4:
985 s.setGarbage(3,1);
986 break;
987 case 5:
988 s.setGarbage(4,1);
989 break;
990 case 6:
991 s.setGarbage(5,1);
992 break;
993 case 7:
994 s.setGarbage(6,1);
995 break;
996 case 8:
997 s.setGarbage(4,1);
998 s.setGarbage(4,1);
999 break;
1000 case 9:
1001 s.setGarbage(5,1);
1002 s.setGarbage(4,1);
1003 break;
1004 case 10:
1005 s.setGarbage(5,1);
1006 s.setGarbage(5,1);
1007 break;
1008 case 11:
1009 s.setGarbage(6,1);
1010 s.setGarbage(5,1);
1011 break;
1012 case 12:
1013 s.setGarbage(6,1);
1014 s.setGarbage(6,1);
1015 break;
1016 case 13:
1017 s.setGarbage(5,1);
1018 s.setGarbage(5,1);
1019 s.setGarbage(4,1);
1020 break;
1021 default:
1022 s.setGarbage(5,1);
1023 s.setGarbage(5,1);
1024 s.setGarbage(4,1);
1025 break;
1026 }
1027 if (s.width > 0) {
1028 garbageSendQueue.push_back(s);
1029 }
1030 }
1031 for (int i=0; i<30; i++) {
1032 for (int j=0; j<6; j++) {
1033 if (garbageToBeCleared[j][i]) {
1034 GarbageClearer(j, i, board[j][i]%1000000, true, chain); //Clears the blocks and all blocks connected to it.
1035 }
1036 }
1037 }
1038
1039 chain=0;
1040
1041 //Break chains (if a block is stable it is resetted to (chain == 0)):
1042 for (int i=0; i<7; i++) {
1043 bool faaling = false; //In the beginning we are NOT falling
1044 for (int j=0; j<30; j++) {
1045 if ((faaling)&&(board[i][j]>-1)&&(board[i][j]<7)) {
1046 block_setFalling(board[i][j], true);
1047 }
1048 if (!faaling) {
1049 block_setFalling(board[i][j], false);
1050 }
1051 if ((!faaling)&&(board[i][j]>0)&&(board[i][j]/10000000!=0)&&((board[i][j]/BLOCKWAIT)%10!=1)&&((board[i][j]/BLOCKHANG)%10!=1)) {
1052 if (chainSize[board[i][j]/10000000]>chainSize[chain]) {
1053 chain=board[i][j]/10000000;
1054 }
1055 board[i][j]=board[i][j]%10000000;
1056 }
1057 if (!((board[i][j]>-1)&&(board[i][j]<7))) {
1058 faaling=true;
1059 }
1060 if (((board[i][j]/1000000)%10==1)||((board[i][j]/BLOCKHANG)%10==1)||((board[i][j]/BLOCKWAIT)%10==1)) {
1061 faaling = false;
1062 }
1063 }
1064 }
1065
1066 //Calculate chain
1067 chain=0;
1068 for (int i=0; i<6; i++) {
1069 for (int j=0; j<30; j++) {
1070 if (chainSize[board[i][j]/10000000]>chain) {
1071 chain=chainSize[board[i][j]/10000000];
1072 }
1073 }
1074 }
1075
1076 //Make space in table for more things
1077 if (chain==0) {
1078 for (int i=0; i<NUMBEROFCHAINS; i++) {
1079 if (chainUsed[i]==true) {
1080 if ((vsMode)&&(chainSize[i]>1)) {
1081 GarbageStruct s;
1082 s.setGarbage(6,chainSize[i]-1);
1083 this->garbageSendQueue.push_back(s);
1084 }
1085 if (chainSize[i]>4) {
1086 LongChainDoneEvent();
1087 }
1088 if (chainSize[i]>1 && !puzzleMode && recordStats) {
1089 Stats::getInstance()->addOne((std::string)"chainX"+std::to_string(chainSize[i]));
1090 }
1091 chainUsed[i]=false;
1092 }
1093 }
1094 }
1095 } //ClearBlocks
1096
1097 //prints "Game Over" and ends game
1098 void BlockGame::SetGameOver() {
1099 if (!bGameOver) {
1100 gameEndedAfter = ticks-gameStartedAt; //We game ends now!
1101 if (recordStats) {
1102 TimeHandler::addTime("playTime",TimeHandler::ms2ct(gameEndedAfter));
1103 }
1104 FinalizeBlockGameInfo();
1105 }
1106 bGameOver = true;
1107 if (stageClear) {
1108 stageButtonStatus = SBstageClear;
1109 }
1110 }
1111
1112 bool BlockGame::GetAIenabled() const {
1113 return AI_Enabled;
1114 }
1115
1116
1117 //Moves all peaces a spot down if possible
1118 int BlockGame::FallBlock(int x, int y, int number) {
1119 if (y == 0) {
1120 return -1;
1121 }
1122 if (x>0) {
1123 if (board[x-1][y] == number) {
1124 return -1;
1125 }
1126 }
1127 int i=x;
1128 bool canFall = true;
1129 //checks a line of a garbage block and see if something is under it
1130 while ((board[i][y] == number)&&(canFall)&&(i<6)) {
1131 if (board[i][y-1] != -1) {
1132 canFall = false;
1133 }
1134 i++;
1135 }
1136 if (canFall) {
1137 //cout << "Now falling" << "\n";
1138 for (int j = x; j<i; j++) {
1139 board[j][y-1] = board[j][y];
1140 board[j][y] = -1;
1141 }
1142 }
1143 return 0;
1144 } //FallBlock
1145
1146
1147 //Makes all Garbage fall one spot
1148 void BlockGame::GarbageFall() {
1149 for (int i=0; i<30; i++) {
1150 for (int j=0; j<7; j++) {
1151 if ((((board[j][i]/1000000)%10) == 1)||(((board[j][i]/1000000)%10) == 2)) {
1152 FallBlock(j, i, board[j][i]);
1153 }
1154 }
1155 }
1156 }
1157
1158 //Makes the blocks fall (it doesn't test time, this must be done before hand)
1159 void BlockGame::FallDown() {
1160 bool falling =false; //nothing is moving unless proven otherwise
1161 for (int i=0; i<29; i++) {
1162 for (int j=0; j<6; j++) {
1163 if ((board[j][i]==-1) && (board[j][i+1]!=-1) && (board[j][i+1]%BLOCKFALL<7)) {
1164 board[j][i] = board[j][i+1];
1165 board[j][i+1] = -1;
1166 falling = true; //something is moving!
1167 }
1168 if ((board[j][i]/BLOCKWAIT)%10==1) {
1169 falling=true;
1170 }
1171 }
1172 }
1173 if (!falling) { //If nothing is falling
1174 if ((puzzleMode)&&(!bGameOver)&&(MovesLeft==0)&&(!(BoardEmpty()))) {
1175 //Puzzle not won
1176 SetGameOver();
1177 stageButtonStatus = SBpuzzleMode;
1178 }
1179 }
1180 GarbageFall(); //Makes the garbage fall
1181 nrFellDown++; //Sets number of this fall, so we know then the next will occur
1182 }
1183
1184 //Moves the cursor, receaves N,S,E or W as a char an moves as desired
1185 void BlockGame::MoveCursor(char way) {
1186 if (!bGameOver) { //If game over nothing happends
1187 if ((way == 'N') && ((cursory<10)||(TowerHeight>12) ||(((pixels==bsize)||(pixels==0)) && (cursory<11)))) {
1188 cursory++;
1189 }
1190 if ((way == 'S') && (cursory>0)) {
1191 cursory--;
1192 }
1193 if ((way == 'W') && (cursorx>0)) {
1194 cursorx--;
1195 }
1196 if ((way == 'E') && (cursorx<4)) {
1197 cursorx++;
1198 }
1199 }
1200 }
1201
1202 //switches the two blocks at the cursor position, unless game over
1203 void BlockGame::SwitchAtCursor() {
1204 if (bGameOver) {
1205 return;
1206 }
1207 ClearBlocks(); //Ensure that everything that floats are marked as floating
1208 if ((board[cursorx][cursory+1]<7) && (board[cursorx+1][cursory+1]<7) && ((!puzzleMode)||(MovesLeft>0)) && (gameStartedAt<ticks)) {
1209 int temp = board[cursorx][cursory+1];
1210 board[cursorx][cursory+1] = board[cursorx+1][cursory+1];
1211 board[cursorx+1][cursory+1] = temp;
1212 }
1213 if ((puzzleMode)&&(gameStartedAt<ticks)&&(MovesLeft>0)) {
1214 MovesLeft--;
1215 }
1216 }
1217
1218 void BlockGame::PushLine() {
1219 PushLineInternal();
1220 }
1221
1222 //Generates a new line and moves the field one block up (restart puzzle mode)
1223 void BlockGame::PushLineInternal() {
1224 //If not game over, not high tower and not puzzle mode
1225 if ((!bGameOver) && TowerHeight<13 && (!puzzleMode) && (gameStartedAt<ticks)&&(chain==0)) {
1226 for (int i=19; i>0; i--) {
1227 for (int j=0; j<6; j++) {
1228 board[j][i] = board[j][i-1];
1229 }
1230 }
1231 for (int j=0; j<6; j++) {
1232 board[j][0] = rand2() % 4;
1233 if (j > 0) {
1234 if (board[j][0] == board[j-1][0]) {
1235 board[j][0] = rand2() % basicBlockVariants;
1236 }
1237 }
1238 if (board[j][0] == board[j][1]) {
1239 board[j][0] = rand2() % basicBlockVariants;
1240 }
1241 if (board[j][0] == board[j][1]) {
1242 board[j][0] = rand2() % basicBlockVariants;
1243 }
1244 while ((j>0)&&(board[j][0]==board[j-1][0])) {
1245 board[j][0] = rand2() % basicBlockVariants;
1246 }
1247 }
1248 score+=1;
1249 MoveCursor('N'); //Workaround for this being done registred too
1250 if (vsMode) {
1251 if (rand2()%6==1) {
1252 board[rand2()%6][0]=6;
1253 }
1254 }
1255 pixels = 0;
1256 stop=0;
1257 pushedPixelAt = ticks;
1258 linesCleared++;
1259 AI_LineOffset++;
1260 nrPushedPixel=(int)((double)(pushedPixelAt-gameStartedAt)/(1000.0*speed));
1261
1262 if (recordStats) {
1263 Stats::getInstance()->addOne("linesPushed");
1264 }
1265 } //if !bGameOver
1266
1267 //Restart Puzzle mode
1268 if (puzzleMode && !bGameOver) {
1269 //Reloads level
1270 MovesLeft = PuzzleNumberOfMovesAllowed(Level);
1271 for (int i=0; i<6; i++) {
1272 for (int j=0; j<12; j++) {
1273 board[i][j+1] = PuzzleGetBrick(Level,i,j);
1274 }
1275 }
1276 score=0;
1277 bGameOver=false;
1278 }
1279
1280 if ((TowerHeight>12) && (!puzzleMode)&&(!bGameOver)&&(chain==0)) {
1281 SetGameOver();
1282 }
1283
1284
1285 }//PushLine
1286
1287 //Pushes a single pixel, so it appears to scrool
1288 void BlockGame::PushPixels() {
1289 nrPushedPixel++;
1290 if ((pixels < bsize) && TowerHeight<13) {
1291 pixels++;
1292 }
1293 else {
1294 PushLineInternal();
1295 }
1296 if (pixels>bsize) {
1297 pixels=0;
1298 }
1299 }
1300
1301
1302 //See how high the tower is, saved in integer TowerHeight
1303 void BlockGame::FindTowerHeight() {
1304 /*
1305 * Old implementation, used until I find the bug in the other.
1306 * This function has a bug in stage clear! if an empty line appears.
1307 */
1308 prevTowerHeight = TowerHeight;
1309 bool found = false;
1310 TowerHeight = 0;
1311 while (!found) {
1312 found = true;
1313 for (int j=0; j<6; j++)
1314 if (board[j][TowerHeight] != -1) {
1315 found = false;
1316 }
1317 TowerHeight++;
1318 }
1319 TowerHeight--;
1320 }
1321
1322 ///////////////////////////////////////////////////////////////////////////
1323 /////////////////////////// AI starts here! ///////////////////////////////
1324 ///////////////////////////////////////////////////////////////////////////
1325 //First the helpet functions:
1326 int BlockGame::nrOfType(int line, int type) {
1327 int counter = 0;
1328 for (int i=0; i<6; i++)
1329 if (board[i][line]==type) {
1330 counter++;
1331 }
1332 return counter;
1333 }
1334
1335
1336 //See if a combo can be made in this line
1337 int BlockGame::horiInLine(int line) {
1338 int nrOfType[7] = {0, 0, 0, 0, 0, 0, 0};
1339 int max = 0;
1340 for (int i=0; i<6; i++) {
1341 int iTemp = board[i][line];
1342 if ((iTemp>-1)&&(iTemp<7)) {
1343 nrOfType[iTemp]++;
1344 }
1345 }
1346 for (int j=0; j<7; j++) {
1347 if (nrOfType[j]>max) {
1348 max = nrOfType[j];
1349 AIcolorToClear = j;
1350 }
1351 }
1352 return max;
1353 }
1354
1355 bool BlockGame::horiClearPossible() {
1356 int i=13;
1357 bool solutionFound = false;
1358 do {
1359 if (horiInLine(i)>2) {
1360 AI_LineOffset = 0;
1361 AIlineToClear = i;
1362 solutionFound = true;
1363 }
1364 i--;
1365 }
1366 while ((!solutionFound)&&(i>0));
1367 return solutionFound;
1368 }
1369
1370 //the Line Has Unmoveable Objects witch might stall the AI
1371 bool BlockGame::lineHasGarbage(int line) {
1372 for (int i=0; i<6; i++) {
1373 if (board[i][line]>1000000) {
1374 return true;
1375 }
1376 }
1377 return false;
1378 }
1379
1380 //Types 0..6 in line
1381 int BlockGame::nrOfRealTypes(int line) {
1382 int counter = 0;
1383 for (int i=0; i<6; i++) {
1384 if ((board[i][line]>-1)&&(board[i][line]<7)) {
1385 counter++;
1386 }
1387 }
1388 return counter;
1389 }
1390
1391 //See if there is a tower
1392 bool BlockGame::ThereIsATower() {
1393 bool bThereIsATower = false; //Unless proven otherwise!
1394 bool topReached = false; //If we have reached the top
1395 int lineNumber = 0;
1396 bool emptySpacesFound = false;
1397 do {
1398 if ((emptySpacesFound) && (nrOfRealTypes(lineNumber)>0)&&(nrOfType(lineNumber, -1)>0)) {
1399 AIlineToClear = lineNumber;
1400 if (lineHasGarbage(lineNumber)) {
1401 return false;
1402 }
1403 else {
1404 bThereIsATower = true;
1405 }
1406 }
1407 else {
1408 emptySpacesFound=false;
1409 }
1410 if ((!emptySpacesFound)&&(nrOfType(lineNumber, -1)>0)) {
1411 emptySpacesFound = true;
1412 }
1413 if (lineNumber<12) {
1414 lineNumber++;
1415 }
1416 else {
1417 topReached = true;
1418 }
1419 }
1420 while ((!bThereIsATower)&&(!topReached));
1421 return bThereIsATower;
1422 }
1423
1424 double BlockGame::firstInLine1(int line) {
1425 if (line > 20 || line < 0) {
1426 std::cerr << "Warning: first in Line1: " << line << "\n";
1427 return 3.0;
1428 }
1429 for (int i=0; i<6; i++) {
1430 if ((board[i][line]>-1)&&(board[i][line]<7)) {
1431 return (double)i;
1432 }
1433 }
1434 return 3.0;
1435 }
1436
1437 //returns the first coordinate of the block of type
1438 double BlockGame::firstInLine(int line, int type) {
1439 if (line > 20 || line < 0) {
1440 std::cerr << "Warning: first in Line: " << line << "\n";
1441 return 3.0;
1442 }
1443 for (int i=0; i<6; i++) {
1444 if (board[i][line]==type) {
1445 return (double)i;
1446 }
1447 }
1448 return 3.0;
1449 }
1450
1451 //There in the line shall we move
1452 int BlockGame::closestTo(int line, int place) {
1453 if ((int)firstInLine1(line)>place) {
1454 return (int)firstInLine1(line)-1;
1455 }
1456 for (int i=place; i>=0; i--) {
1457 if ((board[i][line]>-1)&&(board[i][line]<7)) {
1458 return i;
1459 }
1460 }
1461 AIstatus=0;
1462 return place;
1463 }
1464
1465 //The AI will remove a tower
1466 void BlockGame::AI_ClearTower() {
1467 int place = (int)firstInLine(AIlineToClear-1, -1); //Find an empty field to frop a brick into
1468 int xplace = closestTo(AIlineToClear, place); //Find the brick to drop in it
1469 if (cursory+1<AIlineToClear) {
1470 MoveCursor('N');
1471 }
1472 else if (cursory+1>AIlineToClear) {
1473 MoveCursor('S');
1474 }
1475 else if (cursorx<xplace) {
1476 MoveCursor('E');
1477 }
1478 else if (cursorx>xplace) {
1479 MoveCursor('W');
1480 }
1481 else {
1482 SwitchAtCursor();
1483 }
1484 if (!ThereIsATower()) {
1485 AIstatus = 0;
1486 }
1487 }
1488
1489 //The AI will try to clear block horisontally
1490 void BlockGame::AI_ClearHori() {
1491 int lowestLine = AIlineToClear;
1492 for (int i=0; i<7; i++) {
1493 if (nrOfType(lowestLine, i)>2) {
1494 AIcolorToClear = i;
1495 }
1496 }
1497 if (cursory>lowestLine-1) {
1498 MoveCursor('S');
1499 }
1500 else if (cursory<lowestLine-1) {
1501 MoveCursor('N');
1502 }
1503 else if (nrOfType(lowestLine, AIcolorToClear)>2) {
1504 int left=0, right=0;
1505 if (board[0][lowestLine]==AIcolorToClear) {
1506 left++;
1507 }
1508 if (board[1][lowestLine]==AIcolorToClear) {
1509 left++;
1510 }
1511 if (board[2][lowestLine]==AIcolorToClear) {
1512 left++;
1513 }
1514 if (board[3][lowestLine]==AIcolorToClear) {
1515 right++;
1516 }
1517 if (board[4][lowestLine]==AIcolorToClear) {
1518 right++;
1519 }
1520 if (board[5][lowestLine]==AIcolorToClear) {
1521 right++;
1522 }
1523 int xplace = 0;
1524 if (left<right) {
1525 int count=0;
1526 for (int i=0; (i<4)&&(count<1); i++) {
1527 if ((board[i][lowestLine]==AIcolorToClear)&&((i==0)||(board[i+1][lowestLine]!=AIcolorToClear))) {
1528 count++;
1529 xplace = i;
1530 }
1531 }
1532 }
1533 else {
1534 int count=0;
1535 for (int i=3; (i<=5)&&(count<1); i++) {
1536 if ((board[i][lowestLine]==AIcolorToClear)&&(board[i-1][lowestLine]!=AIcolorToClear)) {
1537 count++;
1538 xplace = --i;
1539 }
1540 }
1541 }
1542 if (cursorx<xplace) {
1543 MoveCursor('E');
1544 }
1545 else if (cursorx>xplace) {
1546 MoveCursor('W');
1547 }
1548 else if (cursorx==xplace) {
1549 SwitchAtCursor();
1550 }
1551 else {
1552 AIstatus = 0;
1553 }
1554 }
1555 else {
1556 AIstatus = 0;
1557 }
1558 }
1559
1560 //Test if vertical clear is possible
1561 bool BlockGame::veriClearPossible() {
1562 bool found=false;
1563 int colors[7] = {0, 0, 0, 0, 0, 0, 0};
1564 for (int i=12; (i>0)&&(!found); i--) {
1565 for (int j=0; j<7; j++) {
1566 if (nrOfType(i, j)==0) {
1567 colors[j]=0;
1568 }
1569 else if (++colors[j]>2) {
1570 AIcolorToClear = j;
1571 AIlineToClear = i;
1572 found=true;
1573 }
1574 }
1575 }
1576 return found;
1577 }
1578
1579
1580
1581 //There in the line shall we move
1582 int BlockGame::closestTo(int line, int type, int place) {
1583 if ((int)firstInLine(line, type)>place) {
1584 return (int)firstInLine(line, type)-1;
1585 }
1586 for (int i=place; i>=0; i--) {
1587 if (board[i][line]==type) {
1588 return i;
1589 }
1590 }
1591 AIstatus=0;
1592 return place;
1593 }
1594
1595 //The AI will try to clear blocks vertically
1596 void BlockGame::AI_ClearVertical() {
1597 //First we find the place there we will align the bricks
1598 int placeToCenter = (int)(firstInLine(AIlineToClear, AIcolorToClear)/3.0+firstInLine(AIlineToClear+1, AIcolorToClear)/3.0+firstInLine(AIlineToClear+2, AIcolorToClear)/3.0);
1599 int unlimitedLoop=0;
1600 if (AIlineToClear < 0 || AIlineToClear > 20) {
1601 std::cerr << "AIlineToClear out of range: " << AIlineToClear << "\n";
1602 return;
1603 }
1604 if (placeToCenter<0 || placeToCenter > 5) {
1605 std::cerr << "placeToCenter out of range: " << placeToCenter << "\n";
1606 return;
1607 }
1608 while (((board[placeToCenter][AIlineToClear]>1000000)||(board[placeToCenter][AIlineToClear+1]>1000000)||(board[placeToCenter][AIlineToClear+2]>1000000))&&(unlimitedLoop<10)) {
1609 unlimitedLoop++;
1610 placeToCenter++;
1611 if (placeToCenter>5) {
1612 placeToCenter=0;
1613 }
1614 }
1615 if (unlimitedLoop>9) {
1616 AIstatus = 0;
1617 return;
1618 }
1619 if (cursory+1>AIlineToClear+2) {
1620 MoveCursor('S');
1621 }
1622 if (cursory+1<AIlineToClear) {
1623 MoveCursor('N');
1624 }
1625 bool toAlign[3]= {true, true, true};
1626 if (board[placeToCenter][AIlineToClear+0]==AIcolorToClear) {
1627 toAlign[0]=false;
1628 }
1629 if (board[placeToCenter][AIlineToClear+1]==AIcolorToClear) {
1630 toAlign[1]=false;
1631 }
1632 if (board[placeToCenter][AIlineToClear+2]==AIcolorToClear) {
1633 toAlign[2]=false;
1634 }
1635 if (cursory+1==AIlineToClear) {
1636 if (toAlign[0]==false) {
1637 MoveCursor('N');
1638 }
1639 else {
1640 if (cursorx>closestTo(AIlineToClear, AIcolorToClear, placeToCenter)) {
1641 MoveCursor('W');
1642 }
1643 else if (cursorx<closestTo(AIlineToClear, AIcolorToClear, placeToCenter)) {
1644 MoveCursor('E');
1645 }
1646 else {
1647 SwitchAtCursor();
1648 }
1649 }
1650 }
1651 else if (cursory+1==AIlineToClear+1) {
1652 if (toAlign[1]==false) {
1653 if (toAlign[2]) {
1654 MoveCursor('N');
1655 }
1656 else {
1657 MoveCursor('S');
1658 }
1659 }
1660 else {
1661 if (cursorx>closestTo(AIlineToClear+1, AIcolorToClear, placeToCenter)) {
1662 MoveCursor('W');
1663 }
1664 else if (cursorx<closestTo(AIlineToClear+1, AIcolorToClear, placeToCenter)) {
1665 MoveCursor('E');
1666 }
1667 else {
1668 SwitchAtCursor();
1669 }
1670 }
1671 }
1672 else if (cursory+1==AIlineToClear+2) {
1673 if (toAlign[2]==false) {
1674 MoveCursor('S');
1675 }
1676 else {
1677 if (cursorx>closestTo(AIlineToClear+2, AIcolorToClear, placeToCenter)) {
1678 MoveCursor('W');
1679 }
1680 else if (cursorx<closestTo(AIlineToClear+2, AIcolorToClear, placeToCenter)) {
1681 MoveCursor('E');
1682 }
1683 else {
1684 SwitchAtCursor();
1685 }
1686 }
1687 }
1688
1689 if ((!toAlign[0])&&(!toAlign[1])&&(!toAlign[2])) {
1690 AIstatus = 0;
1691 }
1692 if ((nrOfType(AIlineToClear, AIcolorToClear)==0)||(nrOfType(AIlineToClear+1, AIcolorToClear)==0)||(nrOfType(AIlineToClear+2, AIcolorToClear)==0)) {
1693 AIstatus = 0;
1694 }
1695 }
1696
1697
1698 void BlockGame::AI_Move() {
1699 switch (AIstatus) {
1700 case 1:
1701 if (TowerHeight<8) {
1702 PushLine();
1703 }
1704 else {
1705 AIstatus = 0;
1706 }
1707 break;
1708 case 2:
1709 AI_ClearTower();
1710 break;
1711 case 3:
1712 AI_ClearHori();
1713 break;
1714 case 4:
1715 AI_ClearVertical();
1716 break;
1717 case 5:
1718 if (!firstLineCreated) {
1719 PushLine();
1720 firstLineCreated = true;
1721 }
1722 else {
1723 PushLine();
1724 AIstatus = 0;
1725 }
1726 break;
1727 case 6:
1728 PushLine();
1729 AIstatus = 0;
1730 break;
1731 default:
1732 if (TowerHeight<6) {
1733 AIstatus = 1;
1734 }
1735 else if (horiClearPossible()) {
1736 AIstatus = 3;
1737 }
1738 else if (veriClearPossible()) {
1739 AIstatus = 4;
1740 }
1741 else if (ThereIsATower()) {
1742 AIstatus = 2;
1743 }
1744 else {
1745 AIstatus = 5;
1746 }
1747 break;
1748 }
1749 }
1750
1751 //////////////////////////////////////////////////////////////////////////
1752 ///////////////////////////// AI ends here! //////////////////////////////
1753 //////////////////////////////////////////////////////////////////////////
1754
1755
1756 //Updates evrything, if not called nothing happends
1757 void BlockGame::Update() {
1758 unsigned int nowTime = ticks; //We remember the time, so it doesn't change during this call
1759 capMaxStops(stop, nowTime, gameStartedAt);
1760 {
1761 FindTowerHeight();
1762 if ((linesCleared-TowerHeight>stageClearLimit) && (stageClear) && (!bGameOver)) {
1763 StageClearSetClear(Level, score, nowTime-gameStartedAt);
1764 setPlayerWon();
1765 stageButtonStatus = SBstageClear;
1766 }
1767 if ((TowerHeight>12)&&(prevTowerHeight<13)&&(!puzzleMode)) {
1768 stop+=1000;
1769 }
1770
1771 while ( nowTime> nrStops*40+gameStartedAt) { //Increase stops, till we reach nowTime
1772 if (stop>0) {
1773 stop = stop-20;
1774 if (stop<=0) {
1775 nrPushedPixel=(int)((nowTime-gameStartedAt)/(1000.0*speed));
1776 }
1777 }
1778 if (stop<0) {
1779 stop = 0;
1780 }
1781 nrStops++;
1782 }
1783 //If we have static content, we don't raise at all!
1784 if (hasStaticContent()) {
1785 stop++;
1786 }
1787 if ((puzzleMode)&&(!bGameOver)&&BoardEmpty()) {
1788 if (!this->singlePuzzle) {
1789 PuzzleSetClear(Level);
1790 stageButtonStatus = SBpuzzleMode;
1791 }
1792 setPlayerWon();
1793 }
1794
1795 //increse speed:
1796 if ((nowTime>gameStartedAt+20000*speedLevel)&&(speedLevel <99)&&(!bGameOver)) {
1797 speed = (baseSpeed*0.9)/((double)speedLevel*0.8);
1798 speedLevel++;
1799 nrPushedPixel=(int)((double)(nowTime-gameStartedAt)/(1000.0*speed));
1800 }
1801
1802
1803 //To prevent the stack from raising a lot then we stop a chain (doesn't work anymore)
1804 if (chain>0) {
1805 stop+=1;
1806 }
1807 //Raises the stack
1808 if ((nowTime>gameStartedAt+nrPushedPixel*1000*speed) && (!bGameOver)&&(!stop))
1809 while ((nowTime>gameStartedAt+nrPushedPixel*1000*speed)&&(!(puzzleMode))) {
1810 PushPixels();
1811 }
1812 if (!bGameOver) {
1813 ClearBlocks();
1814 }
1815
1816 /*************************************************************
1817 Ai stuff
1818 **************************************************************/
1819 if (bGameOver) {
1820 AIstatus = 0; //Enusres that AI is resetted
1821 }
1822 else if (AI_Enabled) {
1823 if (lastAImove+AI_MoveSpeed<ticks) {
1824 AI_Move();
1825 lastAImove=ticks;
1826 }
1827 }
1828
1829 /*************************************************************
1830 Ai stuff ended
1831 **************************************************************/
1832 if ((nowTime>gameStartedAt+nrFellDown*140) && (!bGameOver)) {
1833 FallDown();
1834 }
1835 if ((nowTime<gameStartedAt)&&(puzzleMode)) {
1836 FallDown();
1837 nrFellDown--;
1838 }
1839 ReduceStuff();
1840 if ((timetrial) && (!bGameOver) && (nowTime>gameStartedAt+2*60*1000)) {
1841 SetGameOver();
1842 TimeTrialEndEvent();
1843 }
1844 }
1845 }
1846
1847 bool BlockGame::IsNearDeath() const {
1848 if ((TowerHeight>12)&&(!puzzleMode)&&(!bGameOver)) {
1849 return true;
1850 }
1851 else {
1852 return false;
1853 }
1854 }
1855
1856 void BlockGame::UpdateInternal(unsigned int newtick) {
1857 while (newtick >= ticks+10) {
1858 ticks+=10;
1859 Update();
1860 }
1861 }
1862
1863 void BlockGame::DoAction (const BlockGameAction& action) {
1864 if (action.action == BlockGameAction::Action::UPDATE && action.tick < ticks+10) {
1865 return; //Ignore if this is an update and not high enough
1866 }
1867 if (action.action == BlockGameAction::Action::UPDATE && replayInfo.actions.size() > 0 && replayInfo.actions.back().action == action.action) {
1868 replayInfo.actions.back() = action;
1869 }
1870 else {
1871 replayInfo.actions.push_back(action);
1872 }
1873 if (action.action == BlockGameAction::Action::UPDATE) {
1874 UpdateInternal(action.tick);
1875 }
1876 if (action.action == BlockGameAction::Action::SET_DRAW) {
1877 setDraw();
1878 }
1879 if (action.action == BlockGameAction::Action::SET_WON) {
1880 setPlayerWon();
1881 }
1882 if (action.action == BlockGameAction::Action::SET_GAME_OVER) {
1883 SetGameOver();
1884 }
1885 if (action.action == BlockGameAction::Action::PUSH) {
1886 PushLine();
1887 }
1888 if (action.action == BlockGameAction::Action::MOVE) {
1889 char direction = action.way;
1890 MoveCursor(direction);
1891 }
1892 if (action.action == BlockGameAction::Action::SWITCH) {
1893 SwitchAtCursor();
1894 }
1895 if (action.action == BlockGameAction::Action::PUSH_GARBAGE) {
1896 for (const GarbageStruct& i : action.garbage) {
1897 if (i.greyGarbage) {
1898 CreateGreyGarbage();
1899 }
1900 else {
1901 CreateGarbage(i.width, i.height);
1902 }
1903 }
1904 }
1905 if (action.action == BlockGameAction::Action::MOUSE_DOWN) {
1906 MouseDown(action.x, action.y);
1907 }
1908 if (action.action == BlockGameAction::Action::MOUSE_UP) {
1909 MouseUp();
1910 }
1911 if (action.action == BlockGameAction::Action::MOUSE_MOVE) {
1912 MouseMove(action.x);
1913 }
1914 }
1915
1916 bool BlockGame::isSinglePuzzle() const {
1917 return singlePuzzle;
1918 }
1919
1920 int BlockGame::getLevel() const {
1921 return Level;
1922 }
1923
1924 void BlockGame::PopSendGarbage(std::vector<GarbageStruct>& poppedData) {
1925 for (const GarbageStruct& g : this->garbageSendQueue) {
1926 poppedData.push_back(g);
1927 }
1928 this->garbageSendQueue.clear();
1929 }
1930
1931 void BlockGame::GetMouseCursor(bool& pressed, int& x, int& y) const {
1932 if (mouse_cursorx < 0 || mouse_cursory < 0 || mouse_cursorx >=6 || mouse_cursory > 13) {
1933 pressed = false;
1934 x = 0;
1935 y = 0;
1936 return;
1937 }
1938 pressed = true;
1939 x = mouse_cursorx;
1940 y = mouse_cursory;
1941 }
1942
1943 void BlockGame::MouseDown(int x, int y) {
1944 if (AI_Enabled) {
1945 //AI may not use mouse move. It must use the controller
1946 return;
1947 }
1948 mouse_cursorx = x;
1949 mouse_cursory = y;
1950 }
1951
1952 void BlockGame::MouseMove(int x) {
1953 if (AI_Enabled) {
1954 //AI may not use mouse move. It must use the controller
1955 return;
1956 }
1957 if (mouse_cursorx < 0) {
1958 return;
1959 }
1960 if (x < 0 || x >= 6) {
1961 return;
1962 }
1963 if (x > mouse_cursorx) {
1964 MoveCursorTo(mouse_cursorx, mouse_cursory);
1965 ++mouse_cursorx;
1966 SwitchAtCursor();
1967 }
1968 if (x < mouse_cursorx) {
1969 --mouse_cursorx;
1970 MoveCursorTo(mouse_cursorx, mouse_cursory);
1971 SwitchAtCursor();
1972 }
1973 }
1974
1975 void BlockGame::MouseUp() {
1976 mouse_cursorx = -1;
1977 mouse_cursory = -1;
1978 }
1979 //Play the next level
1980 void nextLevel(BlockGame& g, unsigned int ticks) {
1981 BlockGameStartInfo s;
1982 s.ticks = ticks;
1983 s.level = g.getLevel()+1;
1984 if (g.isPuzzleMode()) {
1985 if (g.getLevel()<PuzzleGetNumberOfPuzzles()-1) {
1986 s.puzzleMode = true;
1987 g.NewGame(s);
1988 }
1989 }
1990 else if (g.isStageClear()) {
1991 if (g.getLevel() < 50-1) {
1992 s.stageClear = true;
1993 g.NewGame(s);
1994 }
1995 }
1996 }
1997
1998 void retryLevel(BlockGame& g, unsigned int ticks) {
1999 BlockGameStartInfo s;
2000 s.ticks = ticks;
2001 s.level = g.getLevel();
2002 if (g.isPuzzleMode()) {
2003 s.puzzleMode = true;
2004 g.NewGame(s);
2005 }
2006 else if (g.isStageClear()) {
2007 s.stageClear = true;
2008 g.NewGame(s);
2009 }
2010 }