git repos / blockattack-game

blame: source/code/BlockGame.cpp

normal view · raw

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