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