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