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