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