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