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