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