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 {
c53e6443 sago007 2012-04-17 11:04 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
						}
c53e6443 sago007 2012-04-17 11:04 851
					combo=1;
c53e6443 sago007 2012-04-17 11:04 852
					previus = board[j][i]%10000000;
c53e6443 sago007 2012-04-17 11:04 853
				}
c53e6443 sago007 2012-04-17 11:04 854
			} //if board
ecef5838 sago007 2015-11-24 20:01 855
			else {
c53e6443 sago007 2012-04-17 11:04 856
				if (combo>2)
ecef5838 sago007 2015-11-24 20:01 857
					for (int k = j-combo; k<j; k++) {
c53e6443 sago007 2012-04-17 11:04 858
						toBeCleared[k][i] = true;
c53e6443 sago007 2012-04-17 11:04 859
					}
c53e6443 sago007 2012-04-17 11:04 860
				combo = 0;
c53e6443 sago007 2012-04-17 11:04 861
				previus = -1;
c53e6443 sago007 2012-04-17 11:04 862
			}
c53e6443 sago007 2012-04-17 11:04 863
c53e6443 sago007 2012-04-17 11:04 864
		} //for j
c53e6443 sago007 2012-04-17 11:04 865
	} //for i
c53e6443 sago007 2012-04-17 11:04 866
c53e6443 sago007 2012-04-17 11:04 867
	combo = 0;
c53e6443 sago007 2012-04-17 11:04 868
	chain = 0;
c53e6443 sago007 2012-04-17 11:04 869
	int grey = 0;
c53e6443 sago007 2012-04-17 11:04 870
	for (int i=0; i<30; i++)
c53e6443 sago007 2012-04-17 11:04 871
		for (int j=0; j<6; j++)
ecef5838 sago007 2015-11-24 20:01 872
			if (toBeCleared[j][i]) {
c53e6443 sago007 2012-04-17 11:04 873
				//see if any garbage is around:
c53e6443 sago007 2012-04-17 11:04 874
				FirstGarbageMarker(j-1, i);
c53e6443 sago007 2012-04-17 11:04 875
				FirstGarbageMarker(j+1, i);
c53e6443 sago007 2012-04-17 11:04 876
				FirstGarbageMarker(j, i-1);
c53e6443 sago007 2012-04-17 11:04 877
				FirstGarbageMarker(j, i+1);
c53e6443 sago007 2012-04-17 11:04 878
				//that is checked now :-)
ecef5838 sago007 2015-11-24 20:01 879
				if (board[j][i]%10000000==6) {
c53e6443 sago007 2012-04-17 11:04 880
					grey++;
ecef5838 sago007 2015-11-24 20:01 881
				}
ecef5838 sago007 2015-11-24 20:01 882
				if ((vsMode) && (grey>2) && (board[j][i]%10000000==6)) {
c53e6443 sago007 2012-04-17 11:04 883
					garbageTarget->CreateGreyGarbage();
ecef5838 sago007 2015-11-24 20:01 884
				}
ecef5838 sago007 2015-11-24 20:01 885
				if ((board[j][i]>-1)&&(board[j][i]%10000000<7)) {
c53e6443 sago007 2012-04-17 11:04 886
					board[j][i]+=BLOCKWAIT+10*FALLTIME;
ecef5838 sago007 2015-11-24 20:01 887
				}
c53e6443 sago007 2012-04-17 11:04 888
ecef5838 sago007 2015-11-24 20:01 889
				if (chainSize[board[j][i]/10000000]>chainSize[chain]) {
c53e6443 sago007 2012-04-17 11:04 890
					chain=board[j][i]/10000000;
ecef5838 sago007 2015-11-24 20:01 891
				}
c53e6443 sago007 2012-04-17 11:04 892
				combo++;
c53e6443 sago007 2012-04-17 11:04 893
				stop+=140*combo;
c53e6443 sago007 2012-04-17 11:04 894
				score +=10;
ecef5838 sago007 2015-11-24 20:01 895
				if (combo>3) {
ecef5838 sago007 2015-11-24 20:01 896
					score+=3*combo;    //More points if more cleared simontanously
ecef5838 sago007 2015-11-24 20:01 897
				}
c53e6443 sago007 2012-04-17 11:04 898
			}
c53e6443 sago007 2012-04-17 11:04 899
	score+=chainSize[chain]*100;
ecef5838 sago007 2015-11-24 20:01 900
	if (chain==0) {
c53e6443 sago007 2012-04-17 11:04 901
		chain=firstUnusedChain();
c53e6443 sago007 2012-04-17 11:04 902
		chainSize[chain]=0;
c53e6443 sago007 2012-04-17 11:04 903
		chainUsed[chain]=true;
c53e6443 sago007 2012-04-17 11:04 904
	}
c53e6443 sago007 2012-04-17 11:04 905
	chainSize[chain]++;
c53e6443 sago007 2012-04-17 11:04 906
	for (int i=0; i<30; i++)
ecef5838 sago007 2015-11-24 20:01 907
		for (int j=0; j<6; j++) {
c53e6443 sago007 2012-04-17 11:04 908
			//if(board[j][i]/10==(BLOCKWAIT+10*FALLTIME)/10)
ecef5838 sago007 2015-11-24 20:01 909
			if (toBeCleared[j][i]) {
c53e6443 sago007 2012-04-17 11:04 910
				board[j][i]=(board[j][i]%10000000)+chain*10000000;
c53e6443 sago007 2012-04-17 11:04 911
			}
c53e6443 sago007 2012-04-17 11:04 912
		}
c53e6443 sago007 2012-04-17 11:04 913
c53e6443 sago007 2012-04-17 11:04 914
	{
c53e6443 sago007 2012-04-17 11:04 915
		//This is here we add text to screen!
c53e6443 sago007 2012-04-17 11:04 916
		bool dead = false;
c53e6443 sago007 2012-04-17 11:04 917
		for (int i=29; i>=0; i--)
c53e6443 sago007 2012-04-17 11:04 918
			for (int j=0; j<6; j++)
ecef5838 sago007 2015-11-24 20:01 919
				if (toBeCleared[j][i]) {
ecef5838 sago007 2015-11-24 20:01 920
					if (!dead) {
c53e6443 sago007 2012-04-17 11:04 921
						dead=true;
c53e6443 sago007 2012-04-17 11:04 922
						string tempS = itoa(chainSize[chain]);
229e212d sago007 2015-08-22 17:14 923
						if (chainSize[chain]>1) {
229e212d sago007 2015-08-22 17:14 924
							AddText(j, i, tempS, 1000);
229e212d sago007 2015-08-22 17:14 925
						}
c53e6443 sago007 2012-04-17 11:04 926
					}
c53e6443 sago007 2012-04-17 11:04 927
				}
c53e6443 sago007 2012-04-17 11:04 928
	} //This was there text was added
c53e6443 sago007 2012-04-17 11:04 929
c53e6443 sago007 2012-04-17 11:04 930
	if (vsMode)
ecef5838 sago007 2015-11-24 20:01 931
		switch (combo) {
c53e6443 sago007 2012-04-17 11:04 932
		case 0:
c53e6443 sago007 2012-04-17 11:04 933
		case 1:
c53e6443 sago007 2012-04-17 11:04 934
		case 2:
c53e6443 sago007 2012-04-17 11:04 935
		case 3:
c53e6443 sago007 2012-04-17 11:04 936
			break;
c53e6443 sago007 2012-04-17 11:04 937
		case 4:
c53e6443 sago007 2012-04-17 11:04 938
			garbageTarget->CreateGarbage(3, 1);
c53e6443 sago007 2012-04-17 11:04 939
			break;
c53e6443 sago007 2012-04-17 11:04 940
		case 5:
c53e6443 sago007 2012-04-17 11:04 941
			garbageTarget->CreateGarbage(4, 1);
c53e6443 sago007 2012-04-17 11:04 942
			break;
c53e6443 sago007 2012-04-17 11:04 943
		case 6:
c53e6443 sago007 2012-04-17 11:04 944
			garbageTarget->CreateGarbage(5, 1);
c53e6443 sago007 2012-04-17 11:04 945
			break;
c53e6443 sago007 2012-04-17 11:04 946
		case 7:
c53e6443 sago007 2012-04-17 11:04 947
			garbageTarget->CreateGarbage(6, 1);
c53e6443 sago007 2012-04-17 11:04 948
			break;
c53e6443 sago007 2012-04-17 11:04 949
		case 8:
c53e6443 sago007 2012-04-17 11:04 950
			garbageTarget->CreateGarbage(4, 1);
c53e6443 sago007 2012-04-17 11:04 951
			garbageTarget->CreateGarbage(4, 1);
c53e6443 sago007 2012-04-17 11:04 952
			break;
c53e6443 sago007 2012-04-17 11:04 953
		case 9:
c53e6443 sago007 2012-04-17 11:04 954
			garbageTarget->CreateGarbage(5, 1);
c53e6443 sago007 2012-04-17 11:04 955
			garbageTarget->CreateGarbage(4, 1);
c53e6443 sago007 2012-04-17 11:04 956
			break;
c53e6443 sago007 2012-04-17 11:04 957
		case 10:
c53e6443 sago007 2012-04-17 11:04 958
			garbageTarget->CreateGarbage(5, 1);
c53e6443 sago007 2012-04-17 11:04 959
			garbageTarget->CreateGarbage(5, 1);
c53e6443 sago007 2012-04-17 11:04 960
			break;
c53e6443 sago007 2012-04-17 11:04 961
		case 11:
c53e6443 sago007 2012-04-17 11:04 962
			garbageTarget->CreateGarbage(6, 1);
c53e6443 sago007 2012-04-17 11:04 963
			garbageTarget->CreateGarbage(5, 1);
c53e6443 sago007 2012-04-17 11:04 964
			break;
c53e6443 sago007 2012-04-17 11:04 965
		case 12:
c53e6443 sago007 2012-04-17 11:04 966
			garbageTarget->CreateGarbage(6, 1);
c53e6443 sago007 2012-04-17 11:04 967
			garbageTarget->CreateGarbage(6, 1);
c53e6443 sago007 2012-04-17 11:04 968
			break;
c53e6443 sago007 2012-04-17 11:04 969
		case 13:
c53e6443 sago007 2012-04-17 11:04 970
			garbageTarget->CreateGarbage(5, 1);
c53e6443 sago007 2012-04-17 11:04 971
			garbageTarget->CreateGarbage(5, 1);
c53e6443 sago007 2012-04-17 11:04 972
			garbageTarget->CreateGarbage(4, 1);
c53e6443 sago007 2012-04-17 11:04 973
			break;
c53e6443 sago007 2012-04-17 11:04 974
		default:
c53e6443 sago007 2012-04-17 11:04 975
			garbageTarget->CreateGarbage(5, 1);
c53e6443 sago007 2012-04-17 11:04 976
			garbageTarget->CreateGarbage(5, 1);
c53e6443 sago007 2012-04-17 11:04 977
			garbageTarget->CreateGarbage(4, 1);
c53e6443 sago007 2012-04-17 11:04 978
			break;
c53e6443 sago007 2012-04-17 11:04 979
		}
c53e6443 sago007 2012-04-17 11:04 980
	for (int i=0; i<30; i++)
c53e6443 sago007 2012-04-17 11:04 981
		for (int j=0; j<6; j++)
ecef5838 sago007 2015-11-24 20:01 982
			if (garbageToBeCleared[j][i]) {
c53e6443 sago007 2012-04-17 11:04 983
				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 984
			}
c53e6443 sago007 2012-04-17 11:04 985
c53e6443 sago007 2012-04-17 11:04 986
	chain=0;
c53e6443 sago007 2012-04-17 11:04 987
c53e6443 sago007 2012-04-17 11:04 988
	//Break chains (if a block is stable it is resetted to (chain == 0)):
ecef5838 sago007 2015-11-24 20:01 989
	for (int i=0; i<7; i++) {
c53e6443 sago007 2012-04-17 11:04 990
		bool faaling = false;  //In the beginning we are NOT falling
ecef5838 sago007 2015-11-24 20:01 991
		for (int j=0; j<30; j++) {
ecef5838 sago007 2015-11-24 20:01 992
			if ((faaling)&&(board[i][j]>-1)&&(board[i][j]<7)) {
c53e6443 sago007 2012-04-17 11:04 993
				board[i][j]+=BLOCKFALL;
c53e6443 sago007 2012-04-17 11:04 994
			}
ecef5838 sago007 2015-11-24 20:01 995
			if ((!faaling)&&((board[i][j]/BLOCKFALL)%10==1)) {
c53e6443 sago007 2012-04-17 11:04 996
				board[i][j]-=BLOCKFALL;
ecef5838 sago007 2015-11-24 20:01 997
			}
ecef5838 sago007 2015-11-24 20:01 998
			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 999
				if (chainSize[board[i][j]/10000000]>chainSize[chain]) {
c53e6443 sago007 2012-04-17 11:04 1000
					chain=board[i][j]/10000000;
ecef5838 sago007 2015-11-24 20:01 1001
				}
c53e6443 sago007 2012-04-17 11:04 1002
				board[i][j]=board[i][j]%10000000;
c53e6443 sago007 2012-04-17 11:04 1003
			}
ecef5838 sago007 2015-11-24 20:01 1004
			if (!((board[i][j]>-1)&&(board[i][j]<7))) {
c53e6443 sago007 2012-04-17 11:04 1005
				faaling=true;
ecef5838 sago007 2015-11-24 20:01 1006
			}
ecef5838 sago007 2015-11-24 20:01 1007
			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 1008
				faaling = false;
ecef5838 sago007 2015-11-24 20:01 1009
			}
c53e6443 sago007 2012-04-17 11:04 1010
		}
c53e6443 sago007 2012-04-17 11:04 1011
	}
c53e6443 sago007 2012-04-17 11:04 1012
c53e6443 sago007 2012-04-17 11:04 1013
	//Create garbage as a result
c53e6443 sago007 2012-04-17 11:04 1014
	//if((vsMode)&&(chainSize[chain]>1)) garbageTarget->CreateGarbage(6,chainSize[chain]-1);
c53e6443 sago007 2012-04-17 11:04 1015
c53e6443 sago007 2012-04-17 11:04 1016
	//Calculate chain
c53e6443 sago007 2012-04-17 11:04 1017
	chain=0;
c53e6443 sago007 2012-04-17 11:04 1018
	for (int i=0; i<6; i++)
ecef5838 sago007 2015-11-24 20:01 1019
		for (int j=0; j<30; j++) {
ecef5838 sago007 2015-11-24 20:01 1020
			if (chainSize[board[i][j]/10000000]>chain) {
c53e6443 sago007 2012-04-17 11:04 1021
				chain=chainSize[board[i][j]/10000000];
ecef5838 sago007 2015-11-24 20:01 1022
			}
c53e6443 sago007 2012-04-17 11:04 1023
		}
c53e6443 sago007 2012-04-17 11:04 1024
c53e6443 sago007 2012-04-17 11:04 1025
	//Make space in table for more things
c53e6443 sago007 2012-04-17 11:04 1026
	if (chain==0)
c53e6443 sago007 2012-04-17 11:04 1027
		for (int i=0; i<NUMBEROFCHAINS; i++)
ecef5838 sago007 2015-11-24 20:01 1028
			if (chainUsed[i]==true) {
ecef5838 sago007 2015-11-24 20:01 1029
				if ((vsMode)&&(chainSize[i]>1)) {
ecef5838 sago007 2015-11-24 20:01 1030
					garbageTarget->CreateGarbage(6, chainSize[i]-1);
ecef5838 sago007 2015-11-24 20:01 1031
				}
313ecd1b sago007 2015-08-22 20:38 1032
				if (chainSize[i]>4) {
313ecd1b sago007 2015-08-22 20:38 1033
					LongChainDoneEvent();
313ecd1b sago007 2015-08-22 20:38 1034
				}
ecef5838 sago007 2015-11-24 20:01 1035
				if (chainSize[i]>1 && !puzzleMode && !AI_Enabled) {
c53e6443 sago007 2012-04-17 11:04 1036
					Stats::getInstance()->addOne((string)"chainX"+itoa(chainSize[i]));
ecef5838 sago007 2015-11-24 20:01 1037
				}
c53e6443 sago007 2012-04-17 11:04 1038
				chainUsed[i]=false;
c53e6443 sago007 2012-04-17 11:04 1039
			}
c53e6443 sago007 2012-04-17 11:04 1040
} //ClearBlocks
c53e6443 sago007 2012-04-17 11:04 1041
c53e6443 sago007 2012-04-17 11:04 1042
//prints "Game Over" and ends game
ecef5838 sago007 2015-11-24 20:01 1043
void BlockGame::SetGameOver() {
ca486238 sago007 2015-08-23 15:12 1044
	if (!bGameOver) {
c53e6443 sago007 2012-04-17 11:04 1045
		gameEndedAfter = ticks-gameStartedAt; //We game ends now!
ca486238 sago007 2015-08-23 15:12 1046
		if (!AI_Enabled) {
17916a2e sago007 2010-11-07 11:26 1047
			TimeHandler::addTime("playTime",TimeHandler::ms2ct(gameEndedAfter));
17916a2e sago007 2010-11-07 11:26 1048
		}
17916a2e sago007 2010-11-07 11:26 1049
	}
c53e6443 sago007 2012-04-17 11:04 1050
	bGameOver = true;
ecef5838 sago007 2015-11-24 20:01 1051
	if (stageClear) {
c53e6443 sago007 2012-04-17 11:04 1052
		stageButtonStatus = SBstageClear;
ecef5838 sago007 2015-11-24 20:01 1053
	}
c53e6443 sago007 2012-04-17 11:04 1054
}
c53e6443 sago007 2012-04-17 11:04 1055
ecef5838 sago007 2015-11-24 20:01 1056
bool BlockGame::GetAIenabled() {
c53e6443 sago007 2012-04-17 11:04 1057
	return AI_Enabled;
c53e6443 sago007 2012-04-17 11:04 1058
}
c53e6443 sago007 2012-04-17 11:04 1059
c53e6443 sago007 2012-04-17 11:04 1060
c53e6443 sago007 2012-04-17 11:04 1061
//Moves all peaces a spot down if possible
ecef5838 sago007 2015-11-24 20:01 1062
int BlockGame::FallBlock(int x, int y, int number) {
ecef5838 sago007 2015-11-24 20:01 1063
	if (y == 0) {
ecef5838 sago007 2015-11-24 20:01 1064
		return -1;
ecef5838 sago007 2015-11-24 20:01 1065
	}
c53e6443 sago007 2012-04-17 11:04 1066
	if (x>0)
ecef5838 sago007 2015-11-24 20:01 1067
		if (board[x-1][y] == number) {
c53e6443 sago007 2012-04-17 11:04 1068
			return -1;
ecef5838 sago007 2015-11-24 20:01 1069
		}
c53e6443 sago007 2012-04-17 11:04 1070
	int i=x;
c53e6443 sago007 2012-04-17 11:04 1071
	bool canFall = true;
c53e6443 sago007 2012-04-17 11:04 1072
	//checks a line of a garbage block and see if something is under it
ecef5838 sago007 2015-11-24 20:01 1073
	while ((board[i][y] == number)&&(canFall)&&(i<6)) {
ecef5838 sago007 2015-11-24 20:01 1074
		if (board[i][y-1] != -1) {
ecef5838 sago007 2015-11-24 20:01 1075
			canFall = false;
ecef5838 sago007 2015-11-24 20:01 1076
		}
c53e6443 sago007 2012-04-17 11:04 1077
		i++;
c53e6443 sago007 2012-04-17 11:04 1078
	}
ecef5838 sago007 2015-11-24 20:01 1079
	if (canFall) {
c53e6443 sago007 2012-04-17 11:04 1080
		//cout << "Now falling" << endl;
ecef5838 sago007 2015-11-24 20:01 1081
		for (int j = x; j<i; j++) {
c53e6443 sago007 2012-04-17 11:04 1082
			board[j][y-1] = board[j][y];
c53e6443 sago007 2012-04-17 11:04 1083
			board[j][y] = -1;
c53e6443 sago007 2012-04-17 11:04 1084
		}
c53e6443 sago007 2012-04-17 11:04 1085
	}
c53e6443 sago007 2012-04-17 11:04 1086
	return 0;
ecef5838 sago007 2015-11-24 20:01 1087
}   //FallBlock
c53e6443 sago007 2012-04-17 11:04 1088
c53e6443 sago007 2012-04-17 11:04 1089
c53e6443 sago007 2012-04-17 11:04 1090
//Makes all Garbage fall one spot
ecef5838 sago007 2015-11-24 20:01 1091
void BlockGame::GarbageFall() {
c53e6443 sago007 2012-04-17 11:04 1092
	for (int i=0; i<30; i++)
ecef5838 sago007 2015-11-24 20:01 1093
		for (int j=0; j<7; j++) {
ecef5838 sago007 2015-11-24 20:01 1094
			if ((((board[j][i]/1000000)%10) == 1)||(((board[j][i]/1000000)%10) == 2)) {
c53e6443 sago007 2012-04-17 11:04 1095
				FallBlock(j, i, board[j][i]);
ecef5838 sago007 2015-11-24 20:01 1096
			}
c53e6443 sago007 2012-04-17 11:04 1097
		}
c53e6443 sago007 2012-04-17 11:04 1098
}
c53e6443 sago007 2012-04-17 11:04 1099
c53e6443 sago007 2012-04-17 11:04 1100
//Makes the blocks fall (it doesn't test time, this must be done before hand)
ecef5838 sago007 2015-11-24 20:01 1101
void BlockGame::FallDown() {
c53e6443 sago007 2012-04-17 11:04 1102
	bool falling =false;        //nothing is moving unless proven otherwise
c53e6443 sago007 2012-04-17 11:04 1103
	for (int i=0; i<29; i++)
ecef5838 sago007 2015-11-24 20:01 1104
		for (int j=0; j<6; j++) {
ecef5838 sago007 2015-11-24 20:01 1105
			if ((board[j][i]==-1) && (board[j][i+1]!=-1) && (board[j][i+1]%BLOCKFALL<7)) {
c53e6443 sago007 2012-04-17 11:04 1106
				board[j][i] = board[j][i+1];
c53e6443 sago007 2012-04-17 11:04 1107
				board[j][i+1] = -1;
c53e6443 sago007 2012-04-17 11:04 1108
				falling = true;              //something is moving!
c53e6443 sago007 2012-04-17 11:04 1109
			}
ecef5838 sago007 2015-11-24 20:01 1110
			if ((board[j][i]/BLOCKWAIT)%10==1) {
c53e6443 sago007 2012-04-17 11:04 1111
				falling=true;
ecef5838 sago007 2015-11-24 20:01 1112
			}
c53e6443 sago007 2012-04-17 11:04 1113
		}
ecef5838 sago007 2015-11-24 20:01 1114
	if (!falling) { //If nothing is falling
ecef5838 sago007 2015-11-24 20:01 1115
		if ((puzzleMode)&&(!bGameOver)&&(MovesLeft==0)&&(!(BoardEmpty()))) {
c53e6443 sago007 2012-04-17 11:04 1116
			//Puzzle not won
c53e6443 sago007 2012-04-17 11:04 1117
			SetGameOver();
c53e6443 sago007 2012-04-17 11:04 1118
			stageButtonStatus = SBpuzzleMode;
c53e6443 sago007 2012-04-17 11:04 1119
		}
c53e6443 sago007 2012-04-17 11:04 1120
	}
ecef5838 sago007 2015-11-24 20:01 1121
	GarbageFall();      //Makes the garbage fall
c53e6443 sago007 2012-04-17 11:04 1122
	nrFellDown++;      //Sets number of this fall, so we know then the next will occur
c53e6443 sago007 2012-04-17 11:04 1123
}
c53e6443 sago007 2012-04-17 11:04 1124
c53e6443 sago007 2012-04-17 11:04 1125
//Moves the cursor, receaves N,S,E or W as a char an moves as desired
ecef5838 sago007 2015-11-24 20:01 1126
void BlockGame::MoveCursor(char way) {
ecef5838 sago007 2015-11-24 20:01 1127
	if (!bGameOver) {     //If game over nothing happends
ecef5838 sago007 2015-11-24 20:01 1128
		if ((way == 'N') && ((cursory<10)||(TowerHeight>12) ||(((pixels==bsize)||(pixels==0)) && (cursory<11)))) {
c53e6443 sago007 2012-04-17 11:04 1129
			cursory++;
ecef5838 sago007 2015-11-24 20:01 1130
		}
ecef5838 sago007 2015-11-24 20:01 1131
		if ((way == 'S') && (cursory>0)) {
c53e6443 sago007 2012-04-17 11:04 1132
			cursory--;
ecef5838 sago007 2015-11-24 20:01 1133
		}
ecef5838 sago007 2015-11-24 20:01 1134
		if ((way == 'W') && (cursorx>0)) {
c53e6443 sago007 2012-04-17 11:04 1135
			cursorx--;
ecef5838 sago007 2015-11-24 20:01 1136
		}
ecef5838 sago007 2015-11-24 20:01 1137
		if ((way == 'E') && (cursorx<4)) {
c53e6443 sago007 2012-04-17 11:04 1138
			cursorx++;
ecef5838 sago007 2015-11-24 20:01 1139
		}
c53e6443 sago007 2012-04-17 11:04 1140
	}
c53e6443 sago007 2012-04-17 11:04 1141
}
c53e6443 sago007 2012-04-17 11:04 1142
c53e6443 sago007 2012-04-17 11:04 1143
//switches the two blocks at the cursor position, unless game over
ecef5838 sago007 2015-11-24 20:01 1144
void BlockGame::SwitchAtCursor() {
ecef5838 sago007 2015-11-24 20:01 1145
	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 1146
		int temp = board[cursorx][cursory+1];
c53e6443 sago007 2012-04-17 11:04 1147
		board[cursorx][cursory+1] = board[cursorx+1][cursory+1];
c53e6443 sago007 2012-04-17 11:04 1148
		board[cursorx+1][cursory+1] = temp;
c53e6443 sago007 2012-04-17 11:04 1149
	}
ecef5838 sago007 2015-11-24 20:01 1150
	if ((puzzleMode)&&(gameStartedAt<ticks)&&(MovesLeft>0)) {
ecef5838 sago007 2015-11-24 20:01 1151
		MovesLeft--;
ecef5838 sago007 2015-11-24 20:01 1152
	}
c53e6443 sago007 2012-04-17 11:04 1153
}
c53e6443 sago007 2012-04-17 11:04 1154
ecef5838 sago007 2015-11-24 20:01 1155
void BlockGame::PushLine() {
79d3c1d3 sago007 2012-05-05 15:54 1156
	PushLineInternal();
79d3c1d3 sago007 2012-05-05 15:54 1157
}
79d3c1d3 sago007 2012-05-05 15:54 1158
79d3c1d3 sago007 2012-05-05 15:54 1159
//Generates a new line and moves the field one block up (restart puzzle mode)
ecef5838 sago007 2015-11-24 20:01 1160
void BlockGame::PushLineInternal() {
c53e6443 sago007 2012-04-17 11:04 1161
	//If not game over, not high tower and not puzzle mode
ecef5838 sago007 2015-11-24 20:01 1162
	if ((!bGameOver) && TowerHeight<13 && (!puzzleMode) && (gameStartedAt<ticks)&&(chain==0)) {
c53e6443 sago007 2012-04-17 11:04 1163
		for (int i=19; i>0; i--)
ecef5838 sago007 2015-11-24 20:01 1164
			for (int j=0; j<6; j++) {
c53e6443 sago007 2012-04-17 11:04 1165
				board[j][i] = board[j][i-1];
c53e6443 sago007 2012-04-17 11:04 1166
			}
ecef5838 sago007 2015-11-24 20:01 1167
		for (int j=0; j<6; j++) {
c53e6443 sago007 2012-04-17 11:04 1168
			board[j][0] = rand2() % 4;
ecef5838 sago007 2015-11-24 20:01 1169
			if (j > 0) {
ecef5838 sago007 2015-11-24 20:01 1170
				if (board[j][0] == board[j-1][0]) {
c53e6443 sago007 2012-04-17 11:04 1171
					board[j][0] = rand2() % 6;
ecef5838 sago007 2015-11-24 20:01 1172
				}
c53e6443 sago007 2012-04-17 11:04 1173
			}
ecef5838 sago007 2015-11-24 20:01 1174
			if (board[j][0] == board[j][1]) {
c53e6443 sago007 2012-04-17 11:04 1175
				board[j][0] = rand2() % 6;
ecef5838 sago007 2015-11-24 20:01 1176
			}
ecef5838 sago007 2015-11-24 20:01 1177
			if (board[j][0] == board[j][1]) {
c53e6443 sago007 2012-04-17 11:04 1178
				board[j][0] = rand2() % 6;
ecef5838 sago007 2015-11-24 20:01 1179
			}
ecef5838 sago007 2015-11-24 20:01 1180
			while ((j>0)&&(board[j][0]==board[j-1][0])) {
c53e6443 sago007 2012-04-17 11:04 1181
				board[j][0] = rand2() % 6;
ecef5838 sago007 2015-11-24 20:01 1182
			}
c53e6443 sago007 2012-04-17 11:04 1183
		}
c53e6443 sago007 2012-04-17 11:04 1184
		score+=1;
ca486238 sago007 2015-08-23 15:12 1185
		MoveCursor('N'); //Workaround for this being done registred too
ca486238 sago007 2015-08-23 15:12 1186
		if (vsMode) {
ecef5838 sago007 2015-11-24 20:01 1187
			if (rand2()%6==1) {
c53e6443 sago007 2012-04-17 11:04 1188
				board[rand2()%6][0]=6;
ecef5838 sago007 2015-11-24 20:01 1189
			}
c53e6443 sago007 2012-04-17 11:04 1190
		}
c53e6443 sago007 2012-04-17 11:04 1191
		pixels = 0;
c53e6443 sago007 2012-04-17 11:04 1192
		stop=0;
c53e6443 sago007 2012-04-17 11:04 1193
		pushedPixelAt = ticks;
c53e6443 sago007 2012-04-17 11:04 1194
		linesCleared++;
c53e6443 sago007 2012-04-17 11:04 1195
		AI_LineOffset++;
c53e6443 sago007 2012-04-17 11:04 1196
		nrPushedPixel=(int)((double)(pushedPixelAt-gameStartedAt)/(1000.0*speed));
c53e6443 sago007 2012-04-17 11:04 1197
ecef5838 sago007 2015-11-24 20:01 1198
		if (!AI_Enabled) {
c53e6443 sago007 2012-04-17 11:04 1199
			Stats::getInstance()->addOne("linesPushed");
c53e6443 sago007 2012-04-17 11:04 1200
		}
c53e6443 sago007 2012-04-17 11:04 1201
	} //if !bGameOver
c53e6443 sago007 2012-04-17 11:04 1202
c53e6443 sago007 2012-04-17 11:04 1203
	//Restart Puzzle mode
ecef5838 sago007 2015-11-24 20:01 1204
	if (puzzleMode && !bGameOver) {
c53e6443 sago007 2012-04-17 11:04 1205
		//Reloads level
055218be sago007 2015-08-22 18:44 1206
		MovesLeft = PuzzleNumberOfMovesAllowed(Level);
c53e6443 sago007 2012-04-17 11:04 1207
		for (int i=0; i<6; i++)
ecef5838 sago007 2015-11-24 20:01 1208
			for (int j=0; j<12; j++) {
055218be sago007 2015-08-22 18:44 1209
				board[i][j+1] = PuzzleGetBrick(Level,i,j);
c53e6443 sago007 2012-04-17 11:04 1210
			}
c53e6443 sago007 2012-04-17 11:04 1211
		score=0;
c53e6443 sago007 2012-04-17 11:04 1212
		bGameOver=false;
c53e6443 sago007 2012-04-17 11:04 1213
	}
c53e6443 sago007 2012-04-17 11:04 1214
ecef5838 sago007 2015-11-24 20:01 1215
	if ((TowerHeight>12) && (!puzzleMode)&&(!bGameOver)&&(chain==0)) {
c53e6443 sago007 2012-04-17 11:04 1216
		SetGameOver();
c53e6443 sago007 2012-04-17 11:04 1217
	}
c53e6443 sago007 2012-04-17 11:04 1218
c53e6443 sago007 2012-04-17 11:04 1219
c53e6443 sago007 2012-04-17 11:04 1220
}//PushLine
c53e6443 sago007 2012-04-17 11:04 1221
c53e6443 sago007 2012-04-17 11:04 1222
//Pushes a single pixel, so it appears to scrool
ecef5838 sago007 2015-11-24 20:01 1223
void BlockGame::PushPixels() {
c53e6443 sago007 2012-04-17 11:04 1224
	nrPushedPixel++;
ecef5838 sago007 2015-11-24 20:01 1225
	if ((pixels < bsize) && TowerHeight<13) {
c53e6443 sago007 2012-04-17 11:04 1226
		pixels++;
c53e6443 sago007 2012-04-17 11:04 1227
	}
ecef5838 sago007 2015-11-24 20:01 1228
	else {
79d3c1d3 sago007 2012-05-05 15:54 1229
		PushLineInternal();
ecef5838 sago007 2015-11-24 20:01 1230
	}
ecef5838 sago007 2015-11-24 20:01 1231
	if (pixels>bsize) {
c53e6443 sago007 2012-04-17 11:04 1232
		pixels=0;
ecef5838 sago007 2015-11-24 20:01 1233
	}
c53e6443 sago007 2012-04-17 11:04 1234
}
c53e6443 sago007 2012-04-17 11:04 1235
c53e6443 sago007 2012-04-17 11:04 1236
c53e6443 sago007 2012-04-17 11:04 1237
//See how high the tower is, saved in integer TowerHeight
ecef5838 sago007 2015-11-24 20:01 1238
void BlockGame::FindTowerHeight() {
c53e6443 sago007 2012-04-17 11:04 1239
	/*
c53e6443 sago007 2012-04-17 11:04 1240
	 * Old implementation, used until I find the bug in the other.
c53e6443 sago007 2012-04-17 11:04 1241
	 * This function has a bug in stage clear! if an empty line appears.
c53e6443 sago007 2012-04-17 11:04 1242
	 */
c53e6443 sago007 2012-04-17 11:04 1243
	prevTowerHeight = TowerHeight;
c53e6443 sago007 2012-04-17 11:04 1244
	bool found = false;
c53e6443 sago007 2012-04-17 11:04 1245
	TowerHeight = 0;
ecef5838 sago007 2015-11-24 20:01 1246
	while (!found) {
c53e6443 sago007 2012-04-17 11:04 1247
		found = true;
c53e6443 sago007 2012-04-17 11:04 1248
		for (int j=0; j<6; j++)
ecef5838 sago007 2015-11-24 20:01 1249
			if (board[j][TowerHeight] != -1) {
c53e6443 sago007 2012-04-17 11:04 1250
				found = false;
ecef5838 sago007 2015-11-24 20:01 1251
			}
c53e6443 sago007 2012-04-17 11:04 1252
		TowerHeight++;
c53e6443 sago007 2012-04-17 11:04 1253
	}
c53e6443 sago007 2012-04-17 11:04 1254
	TowerHeight--;
c53e6443 sago007 2012-04-17 11:04 1255
}
17916a2e sago007 2010-11-07 11:26 1256
17916a2e sago007 2010-11-07 11:26 1257
///////////////////////////////////////////////////////////////////////////
17916a2e sago007 2010-11-07 11:26 1258
/////////////////////////// AI starts here! ///////////////////////////////
17916a2e sago007 2010-11-07 11:26 1259
///////////////////////////////////////////////////////////////////////////
c53e6443 sago007 2012-04-17 11:04 1260
//First the helpet functions:
ecef5838 sago007 2015-11-24 20:01 1261
int BlockGame::nrOfType(int line, int type) {
c53e6443 sago007 2012-04-17 11:04 1262
	// cout << "Start_ nrOfType" << endl;
c53e6443 sago007 2012-04-17 11:04 1263
	int counter = 0;
c53e6443 sago007 2012-04-17 11:04 1264
	for (int i=0; i<6; i++)
ecef5838 sago007 2015-11-24 20:01 1265
		if (board[i][line]==type) {
ecef5838 sago007 2015-11-24 20:01 1266
			counter++;
ecef5838 sago007 2015-11-24 20:01 1267
		}
c53e6443 sago007 2012-04-17 11:04 1268
	return counter;
c53e6443 sago007 2012-04-17 11:04 1269
}
c53e6443 sago007 2012-04-17 11:04 1270
c53e6443 sago007 2012-04-17 11:04 1271
c53e6443 sago007 2012-04-17 11:04 1272
//See if a combo can be made in this line
ecef5838 sago007 2015-11-24 20:01 1273
int BlockGame::horiInLine(int line) {
c53e6443 sago007 2012-04-17 11:04 1274
	//cout << "Start_ hori in line" << endl;
c53e6443 sago007 2012-04-17 11:04 1275
	int nrOfType[7] = {0, 0, 0, 0, 0, 0, 0};
c53e6443 sago007 2012-04-17 11:04 1276
	int iTemp;
c53e6443 sago007 2012-04-17 11:04 1277
	int max = 0;
ecef5838 sago007 2015-11-24 20:01 1278
	for (int i=0; i<6; i++) {
c53e6443 sago007 2012-04-17 11:04 1279
		iTemp = board[i][line];
ecef5838 sago007 2015-11-24 20:01 1280
		if ((iTemp>-1)&&(iTemp<7)) {
c53e6443 sago007 2012-04-17 11:04 1281
			nrOfType[iTemp]++;
ecef5838 sago007 2015-11-24 20:01 1282
		}
c53e6443 sago007 2012-04-17 11:04 1283
	}
ecef5838 sago007 2015-11-24 20:01 1284
	for (int j=0; j<7; j++) {
ecef5838 sago007 2015-11-24 20:01 1285
		if (nrOfType[j]>max) {
c53e6443 sago007 2012-04-17 11:04 1286
			max = nrOfType[j];
c53e6443 sago007 2012-04-17 11:04 1287
			AIcolorToClear = j;
c53e6443 sago007 2012-04-17 11:04 1288
		}
c53e6443 sago007 2012-04-17 11:04 1289
	}
c53e6443 sago007 2012-04-17 11:04 1290
	return max;
c53e6443 sago007 2012-04-17 11:04 1291
}
c53e6443 sago007 2012-04-17 11:04 1292
ecef5838 sago007 2015-11-24 20:01 1293
bool BlockGame::horiClearPossible() {
c53e6443 sago007 2012-04-17 11:04 1294
	//cout << "Start_ horiclear possible" << endl;
c53e6443 sago007 2012-04-17 11:04 1295
	int i=13;
c53e6443 sago007 2012-04-17 11:04 1296
	bool solutionFound = false;
ecef5838 sago007 2015-11-24 20:01 1297
	do {
ecef5838 sago007 2015-11-24 20:01 1298
		if (horiInLine(i)>2) {
c53e6443 sago007 2012-04-17 11:04 1299
			AI_LineOffset = 0;
c53e6443 sago007 2012-04-17 11:04 1300
			AIlineToClear = i;
c53e6443 sago007 2012-04-17 11:04 1301
			solutionFound = true;
c53e6443 sago007 2012-04-17 11:04 1302
		}
c53e6443 sago007 2012-04-17 11:04 1303
		i--;
c53e6443 sago007 2012-04-17 11:04 1304
	}
c53e6443 sago007 2012-04-17 11:04 1305
	while ((!solutionFound)&&(i>0));
c53e6443 sago007 2012-04-17 11:04 1306
	return solutionFound;
c53e6443 sago007 2012-04-17 11:04 1307
}
c53e6443 sago007 2012-04-17 11:04 1308
c53e6443 sago007 2012-04-17 11:04 1309
//the Line Has Unmoveable Objects witch might stall the AI
ecef5838 sago007 2015-11-24 20:01 1310
bool BlockGame::lineHasGarbage(int line) {
c53e6443 sago007 2012-04-17 11:04 1311
	for (int i=0; i<6; i++)
ecef5838 sago007 2015-11-24 20:01 1312
		if (board[i][line]>1000000) {
c53e6443 sago007 2012-04-17 11:04 1313
			return true;
ecef5838 sago007 2015-11-24 20:01 1314
		}
c53e6443 sago007 2012-04-17 11:04 1315
	return false;
c53e6443 sago007 2012-04-17 11:04 1316
}
c53e6443 sago007 2012-04-17 11:04 1317
c53e6443 sago007 2012-04-17 11:04 1318
//Types 0..6 in line
ecef5838 sago007 2015-11-24 20:01 1319
int BlockGame::nrOfRealTypes(int line) {
c53e6443 sago007 2012-04-17 11:04 1320
	//cout << "Start_ nrOfReal" << endl;
c53e6443 sago007 2012-04-17 11:04 1321
	int counter = 0;
c53e6443 sago007 2012-04-17 11:04 1322
	for (int i=0; i<6; i++)
ecef5838 sago007 2015-11-24 20:01 1323
		if ((board[i][line]>-1)&&(board[i][line]<7)) {
ecef5838 sago007 2015-11-24 20:01 1324
			counter++;
ecef5838 sago007 2015-11-24 20:01 1325
		}
c53e6443 sago007 2012-04-17 11:04 1326
	return counter;
c53e6443 sago007 2012-04-17 11:04 1327
}
c53e6443 sago007 2012-04-17 11:04 1328
c53e6443 sago007 2012-04-17 11:04 1329
//See if there is a tower
ecef5838 sago007 2015-11-24 20:01 1330
bool BlockGame::ThereIsATower() {
c53e6443 sago007 2012-04-17 11:04 1331
	//cout << "Start_ there is a tower" << endl;
c53e6443 sago007 2012-04-17 11:04 1332
	bool bThereIsATower = false; //Unless proven otherwise!
c53e6443 sago007 2012-04-17 11:04 1333
	bool topReached = false; //If we have reached the top
c53e6443 sago007 2012-04-17 11:04 1334
	int lineNumber = 0;
c53e6443 sago007 2012-04-17 11:04 1335
	bool emptySpacesFound = false;
ecef5838 sago007 2015-11-24 20:01 1336
	do {
ecef5838 sago007 2015-11-24 20:01 1337
		if ((emptySpacesFound) && (nrOfRealTypes(lineNumber)>0)&&(nrOfType(lineNumber, -1)>0)) {
c53e6443 sago007 2012-04-17 11:04 1338
			AIlineToClear = lineNumber;
ecef5838 sago007 2015-11-24 20:01 1339
			if (lineHasGarbage(lineNumber)) {
c53e6443 sago007 2012-04-17 11:04 1340
				return false;
ecef5838 sago007 2015-11-24 20:01 1341
			}
ecef5838 sago007 2015-11-24 20:01 1342
			else {
c53e6443 sago007 2012-04-17 11:04 1343
				bThereIsATower = true;
ecef5838 sago007 2015-11-24 20:01 1344
			}
c53e6443 sago007 2012-04-17 11:04 1345
		}
ecef5838 sago007 2015-11-24 20:01 1346
		else {
c53e6443 sago007 2012-04-17 11:04 1347
			emptySpacesFound=false;
ecef5838 sago007 2015-11-24 20:01 1348
		}
ecef5838 sago007 2015-11-24 20:01 1349
		if ((!emptySpacesFound)&&(nrOfType(lineNumber, -1)>0)) {
c53e6443 sago007 2012-04-17 11:04 1350
			emptySpacesFound = true;
ecef5838 sago007 2015-11-24 20:01 1351
		}
ecef5838 sago007 2015-11-24 20:01 1352
		if (lineNumber<12) {
c53e6443 sago007 2012-04-17 11:04 1353
			lineNumber++;
ecef5838 sago007 2015-11-24 20:01 1354
		}
ecef5838 sago007 2015-11-24 20:01 1355
		else {
c53e6443 sago007 2012-04-17 11:04 1356
			topReached = true;
ecef5838 sago007 2015-11-24 20:01 1357
		}
c53e6443 sago007 2012-04-17 11:04 1358
	}
c53e6443 sago007 2012-04-17 11:04 1359
	while ((!bThereIsATower)&&(!topReached));
c53e6443 sago007 2012-04-17 11:04 1360
	//if(bThereIsATower)
c53e6443 sago007 2012-04-17 11:04 1361
	//cout << "There is actually a tower" << endl;
c53e6443 sago007 2012-04-17 11:04 1362
	return bThereIsATower;
c53e6443 sago007 2012-04-17 11:04 1363
}
c53e6443 sago007 2012-04-17 11:04 1364
ecef5838 sago007 2015-11-24 20:01 1365
double BlockGame::firstInLine1(int line) {
ecef5838 sago007 2015-11-24 20:01 1366
	if (line > 20 || line < 0) {
56241205 sago007 2012-08-05 11:15 1367
		cerr << "Warning: first in Line1: " << line << endl;
56241205 sago007 2012-08-05 11:15 1368
		return 3.0;
56241205 sago007 2012-08-05 11:15 1369
	}
c53e6443 sago007 2012-04-17 11:04 1370
	for (int i=0; i<6; i++)
ecef5838 sago007 2015-11-24 20:01 1371
		if ((board[i][line]>-1)&&(board[i][line]<7)) {
c53e6443 sago007 2012-04-17 11:04 1372
			return (double)i;
ecef5838 sago007 2015-11-24 20:01 1373
		}
c53e6443 sago007 2012-04-17 11:04 1374
	return 3.0;
c53e6443 sago007 2012-04-17 11:04 1375
}
c53e6443 sago007 2012-04-17 11:04 1376
c53e6443 sago007 2012-04-17 11:04 1377
//returns the first coordinate of the block of type
ecef5838 sago007 2015-11-24 20:01 1378
double BlockGame::firstInLine(int line, int type) {
ecef5838 sago007 2015-11-24 20:01 1379
	if (line > 20 || line < 0) {
56241205 sago007 2012-08-05 11:15 1380
		cerr << "Warning: first in Line: " << line << endl;
56241205 sago007 2012-08-05 11:15 1381
		return 3.0;
56241205 sago007 2012-08-05 11:15 1382
	}
c53e6443 sago007 2012-04-17 11:04 1383
	for (int i=0; i<6; i++)
ecef5838 sago007 2015-11-24 20:01 1384
		if (board[i][line]==type) {
c53e6443 sago007 2012-04-17 11:04 1385
			return (double)i;
ecef5838 sago007 2015-11-24 20:01 1386
		}
c53e6443 sago007 2012-04-17 11:04 1387
	return 3.0;
c53e6443 sago007 2012-04-17 11:04 1388
}
c53e6443 sago007 2012-04-17 11:04 1389
c53e6443 sago007 2012-04-17 11:04 1390
//There in the line shall we move
ecef5838 sago007 2015-11-24 20:01 1391
int BlockGame::closestTo(int line, int place) {
ecef5838 sago007 2015-11-24 20:01 1392
	if ((int)firstInLine1(line)>place) {
c53e6443 sago007 2012-04-17 11:04 1393
		return (int)firstInLine1(line)-1;
ecef5838 sago007 2015-11-24 20:01 1394
	}
ecef5838 sago007 2015-11-24 20:01 1395
	for (int i=place; i>=0; i--) {
ecef5838 sago007 2015-11-24 20:01 1396
		if ((board[i][line]>-1)&&(board[i][line]<7)) {
c53e6443 sago007 2012-04-17 11:04 1397
			return i;
ecef5838 sago007 2015-11-24 20:01 1398
		}
c53e6443 sago007 2012-04-17 11:04 1399
	}
c53e6443 sago007 2012-04-17 11:04 1400
	AIstatus=0;
c53e6443 sago007 2012-04-17 11:04 1401
	return place;
c53e6443 sago007 2012-04-17 11:04 1402
}
c53e6443 sago007 2012-04-17 11:04 1403
c53e6443 sago007 2012-04-17 11:04 1404
//The AI will remove a tower
ecef5838 sago007 2015-11-24 20:01 1405
void BlockGame::AI_ClearTower() {
c53e6443 sago007 2012-04-17 11:04 1406
	//   cout << "AI: ClearTower, line: " << AIlineToClear << endl;
c53e6443 sago007 2012-04-17 11:04 1407
	int place = (int)firstInLine(AIlineToClear-1, -1); //Find an empty field to frop a brick into
c53e6443 sago007 2012-04-17 11:04 1408
	int xplace = closestTo(AIlineToClear, place); //Find the brick to drop in it
ecef5838 sago007 2015-11-24 20:01 1409
	if (cursory+1<AIlineToClear) {
c53e6443 sago007 2012-04-17 11:04 1410
		MoveCursor('N');
ecef5838 sago007 2015-11-24 20:01 1411
	}
ecef5838 sago007 2015-11-24 20:01 1412
	else if (cursory+1>AIlineToClear) {
c53e6443 sago007 2012-04-17 11:04 1413
		MoveCursor('S');
ecef5838 sago007 2015-11-24 20:01 1414
	}
ecef5838 sago007 2015-11-24 20:01 1415
	else if (cursorx<xplace) {
c53e6443 sago007 2012-04-17 11:04 1416
		MoveCursor('E');
ecef5838 sago007 2015-11-24 20:01 1417
	}
ecef5838 sago007 2015-11-24 20:01 1418
	else if (cursorx>xplace) {
c53e6443 sago007 2012-04-17 11:04 1419
		MoveCursor('W');
ecef5838 sago007 2015-11-24 20:01 1420
	}
ecef5838 sago007 2015-11-24 20:01 1421
	else {
c53e6443 sago007 2012-04-17 11:04 1422
		SwitchAtCursor();
ecef5838 sago007 2015-11-24 20:01 1423
	}
ecef5838 sago007 2015-11-24 20:01 1424
	if (!ThereIsATower()) {
c53e6443 sago007 2012-04-17 11:04 1425
		AIstatus = 0;
ecef5838 sago007 2015-11-24 20:01 1426
	}
c53e6443 sago007 2012-04-17 11:04 1427
}
c53e6443 sago007 2012-04-17 11:04 1428
c53e6443 sago007 2012-04-17 11:04 1429
//The AI will try to clear block horisontally
ecef5838 sago007 2015-11-24 20:01 1430
void BlockGame::AI_ClearHori() {
c53e6443 sago007 2012-04-17 11:04 1431
	//   cout << "AI: ClearHori";
c53e6443 sago007 2012-04-17 11:04 1432
	int lowestLine = AIlineToClear;
c53e6443 sago007 2012-04-17 11:04 1433
	//AIcolorToClear
c53e6443 sago007 2012-04-17 11:04 1434
	bool found =true;
c53e6443 sago007 2012-04-17 11:04 1435
	/*for(int i; (i<12)&&(!found);i++)
c53e6443 sago007 2012-04-17 11:04 1436
	 * {
c53e6443 sago007 2012-04-17 11:04 1437
	 * if(horiInLine(i)>2)
c53e6443 sago007 2012-04-17 11:04 1438
	 * {
c53e6443 sago007 2012-04-17 11:04 1439
	 * int lowestLine = i;
c53e6443 sago007 2012-04-17 11:04 1440
	 * found = true;
c53e6443 sago007 2012-04-17 11:04 1441
	 * }
c53e6443 sago007 2012-04-17 11:04 1442
	 * }*/
ecef5838 sago007 2015-11-24 20:01 1443
	for (int i=0; i<7; i++) {
ecef5838 sago007 2015-11-24 20:01 1444
		if (nrOfType(lowestLine, i)>2) {
c53e6443 sago007 2012-04-17 11:04 1445
			AIcolorToClear = i;
ecef5838 sago007 2015-11-24 20:01 1446
		}
c53e6443 sago007 2012-04-17 11:04 1447
	}
ecef5838 sago007 2015-11-24 20:01 1448
	if (found) {
ecef5838 sago007 2015-11-24 20:01 1449
		if (cursory>lowestLine-1) {
c53e6443 sago007 2012-04-17 11:04 1450
			MoveCursor('S');
ecef5838 sago007 2015-11-24 20:01 1451
		}
ecef5838 sago007 2015-11-24 20:01 1452
		else if (cursory<lowestLine-1) {
c53e6443 sago007 2012-04-17 11:04 1453
			MoveCursor('N');
ecef5838 sago007 2015-11-24 20:01 1454
		}
ecef5838 sago007 2015-11-24 20:01 1455
		else if (nrOfType(lowestLine, AIcolorToClear)>2) {
c53e6443 sago007 2012-04-17 11:04 1456
			int left=0, right=0;
ecef5838 sago007 2015-11-24 20:01 1457
			if (board[0][lowestLine]==AIcolorToClear) {
ecef5838 sago007 2015-11-24 20:01 1458
				left++;
ecef5838 sago007 2015-11-24 20:01 1459
			}
ecef5838 sago007 2015-11-24 20:01 1460
			if (board[1][lowestLine]==AIcolorToClear) {
ecef5838 sago007 2015-11-24 20:01 1461
				left++;
ecef5838 sago007 2015-11-24 20:01 1462
			}
ecef5838 sago007 2015-11-24 20:01 1463
			if (board[2][lowestLine]==AIcolorToClear) {
ecef5838 sago007 2015-11-24 20:01 1464
				left++;
ecef5838 sago007 2015-11-24 20:01 1465
			}
ecef5838 sago007 2015-11-24 20:01 1466
			if (board[3][lowestLine]==AIcolorToClear) {
ecef5838 sago007 2015-11-24 20:01 1467
				right++;
ecef5838 sago007 2015-11-24 20:01 1468
			}
ecef5838 sago007 2015-11-24 20:01 1469
			if (board[4][lowestLine]==AIcolorToClear) {
ecef5838 sago007 2015-11-24 20:01 1470
				right++;
ecef5838 sago007 2015-11-24 20:01 1471
			}
ecef5838 sago007 2015-11-24 20:01 1472
			if (board[5][lowestLine]==AIcolorToClear) {
ecef5838 sago007 2015-11-24 20:01 1473
				right++;
ecef5838 sago007 2015-11-24 20:01 1474
			}
c53e6443 sago007 2012-04-17 11:04 1475
			int xplace = 0;
ecef5838 sago007 2015-11-24 20:01 1476
			if (left<right) {
c53e6443 sago007 2012-04-17 11:04 1477
				//   cout << ", right>left";
c53e6443 sago007 2012-04-17 11:04 1478
				int count=0;
c53e6443 sago007 2012-04-17 11:04 1479
				for (int i=0; (i<4)&&(count<1); i++)
ecef5838 sago007 2015-11-24 20:01 1480
					if ((board[i][lowestLine]==AIcolorToClear)&&((i==0)||(board[i+1][lowestLine]!=AIcolorToClear))) {
c53e6443 sago007 2012-04-17 11:04 1481
						count++;
c53e6443 sago007 2012-04-17 11:04 1482
						xplace = i;
c53e6443 sago007 2012-04-17 11:04 1483
					}
c53e6443 sago007 2012-04-17 11:04 1484
			}
ecef5838 sago007 2015-11-24 20:01 1485
			else {
c53e6443 sago007 2012-04-17 11:04 1486
				//   cout << ", left>=right";
c53e6443 sago007 2012-04-17 11:04 1487
				int count=0;
c53e6443 sago007 2012-04-17 11:04 1488
				for (int i=3; (i<=5)&&(count<1); i++)
ecef5838 sago007 2015-11-24 20:01 1489
					if ((board[i][lowestLine]==AIcolorToClear)&&(board[i-1][lowestLine]!=AIcolorToClear)) {
c53e6443 sago007 2012-04-17 11:04 1490
						count++;
c53e6443 sago007 2012-04-17 11:04 1491
						xplace = --i;
c53e6443 sago007 2012-04-17 11:04 1492
					}
c53e6443 sago007 2012-04-17 11:04 1493
			}
c53e6443 sago007 2012-04-17 11:04 1494
			//cout << ", xplace: " << xplace;
ecef5838 sago007 2015-11-24 20:01 1495
			if (cursorx<xplace) {
c53e6443 sago007 2012-04-17 11:04 1496
				MoveCursor('E');
ecef5838 sago007 2015-11-24 20:01 1497
			}
ecef5838 sago007 2015-11-24 20:01 1498
			else if (cursorx>xplace) {
c53e6443 sago007 2012-04-17 11:04 1499
				MoveCursor('W');
ecef5838 sago007 2015-11-24 20:01 1500
			}
ecef5838 sago007 2015-11-24 20:01 1501
			else if (cursorx==xplace) {
c53e6443 sago007 2012-04-17 11:04 1502
				SwitchAtCursor();
ecef5838 sago007 2015-11-24 20:01 1503
			}
ecef5838 sago007 2015-11-24 20:01 1504
			else {
c53e6443 sago007 2012-04-17 11:04 1505
				AIstatus = 0;
ecef5838 sago007 2015-11-24 20:01 1506
			}
c53e6443 sago007 2012-04-17 11:04 1507
		}
ecef5838 sago007 2015-11-24 20:01 1508
		else {
c53e6443 sago007 2012-04-17 11:04 1509
			AIstatus = 0;
ecef5838 sago007 2015-11-24 20:01 1510
		}
c53e6443 sago007 2012-04-17 11:04 1511
	}
ecef5838 sago007 2015-11-24 20:01 1512
	else {
c53e6443 sago007 2012-04-17 11:04 1513
		AIstatus = 0;
ecef5838 sago007 2015-11-24 20:01 1514
	}
c53e6443 sago007 2012-04-17 11:04 1515
	//cout << endl; //for debugging
c53e6443 sago007 2012-04-17 11:04 1516
}
c53e6443 sago007 2012-04-17 11:04 1517
c53e6443 sago007 2012-04-17 11:04 1518
//Test if vertical clear is possible
ecef5838 sago007 2015-11-24 20:01 1519
bool BlockGame::veriClearPossible() {
c53e6443 sago007 2012-04-17 11:04 1520
	bool found=false;
c53e6443 sago007 2012-04-17 11:04 1521
	int colors[7] = {0, 0, 0, 0, 0, 0, 0};
ecef5838 sago007 2015-11-24 20:01 1522
	for (int i=12; (i>0)&&(!found); i--) {
ecef5838 sago007 2015-11-24 20:01 1523
		for (int j=0; j<7; j++) {
ecef5838 sago007 2015-11-24 20:01 1524
			if (nrOfType(i, j)==0) {
c53e6443 sago007 2012-04-17 11:04 1525
				colors[j]=0;
ecef5838 sago007 2015-11-24 20:01 1526
			}
ecef5838 sago007 2015-11-24 20:01 1527
			else if (++colors[j]>2) {
c53e6443 sago007 2012-04-17 11:04 1528
				AIcolorToClear = j;
c53e6443 sago007 2012-04-17 11:04 1529
				AIlineToClear = i;
c53e6443 sago007 2012-04-17 11:04 1530
				found=true;
c53e6443 sago007 2012-04-17 11:04 1531
			}
c53e6443 sago007 2012-04-17 11:04 1532
c53e6443 sago007 2012-04-17 11:04 1533
		}
c53e6443 sago007 2012-04-17 11:04 1534
	}
c53e6443 sago007 2012-04-17 11:04 1535
	return found;
c53e6443 sago007 2012-04-17 11:04 1536
}
c53e6443 sago007 2012-04-17 11:04 1537
c53e6443 sago007 2012-04-17 11:04 1538
c53e6443 sago007 2012-04-17 11:04 1539
c53e6443 sago007 2012-04-17 11:04 1540
//There in the line shall we move
ecef5838 sago007 2015-11-24 20:01 1541
int BlockGame::closestTo(int line, int type, int place) {
ecef5838 sago007 2015-11-24 20:01 1542
	if ((int)firstInLine(line, type)>place) {
c53e6443 sago007 2012-04-17 11:04 1543
		return (int)firstInLine(line, type)-1;
ecef5838 sago007 2015-11-24 20:01 1544
	}
ecef5838 sago007 2015-11-24 20:01 1545
	for (int i=place; i>=0; i--) {
ecef5838 sago007 2015-11-24 20:01 1546
		if (board[i][line]==type) {
c53e6443 sago007 2012-04-17 11:04 1547
			return i;
ecef5838 sago007 2015-11-24 20:01 1548
		}
c53e6443 sago007 2012-04-17 11:04 1549
	}
c53e6443 sago007 2012-04-17 11:04 1550
	AIstatus=0;
c53e6443 sago007 2012-04-17 11:04 1551
	return place;
c53e6443 sago007 2012-04-17 11:04 1552
}
c53e6443 sago007 2012-04-17 11:04 1553
c53e6443 sago007 2012-04-17 11:04 1554
//The AI will try to clear blocks vertically
ecef5838 sago007 2015-11-24 20:01 1555
void BlockGame::AI_ClearVertical() {
c53e6443 sago007 2012-04-17 11:04 1556
	// cout << "AI: ClearVeri";
c53e6443 sago007 2012-04-17 11:04 1557
	//First we find the place there we will align the bricks
c53e6443 sago007 2012-04-17 11:04 1558
	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 1559
	int unlimitedLoop=0;
ecef5838 sago007 2015-11-24 20:01 1560
	if (AIlineToClear < 0 || AIlineToClear > 20) {
56241205 sago007 2012-08-05 11:15 1561
		cerr << "AIlineToClear out of range: " << AIlineToClear << endl;
56241205 sago007 2012-08-05 11:15 1562
		return;
56241205 sago007 2012-08-05 11:15 1563
	}
ecef5838 sago007 2015-11-24 20:01 1564
	if (placeToCenter<0 || placeToCenter > 5) {
56241205 sago007 2012-08-05 11:15 1565
		cerr << "placeToCenter out of range: " << placeToCenter << endl;
56241205 sago007 2012-08-05 11:15 1566
		return;
56241205 sago007 2012-08-05 11:15 1567
	}
56241205 sago007 2012-08-05 11:15 1568
	//cout << "AI_ClearVertical: " << placeToCenter << ", " << AIlineToClear << endl;
ecef5838 sago007 2015-11-24 20:01 1569
	while (((board[placeToCenter][AIlineToClear]>1000000)||(board[placeToCenter][AIlineToClear+1]>1000000)||(board[placeToCenter][AIlineToClear+2]>1000000))&&(unlimitedLoop<10)) {
c53e6443 sago007 2012-04-17 11:04 1570
		unlimitedLoop++;
c53e6443 sago007 2012-04-17 11:04 1571
		placeToCenter++;
ecef5838 sago007 2015-11-24 20:01 1572
		if (placeToCenter>5) {
c53e6443 sago007 2012-04-17 11:04 1573
			placeToCenter=0;
ecef5838 sago007 2015-11-24 20:01 1574
		}
c53e6443 sago007 2012-04-17 11:04 1575
	}
ecef5838 sago007 2015-11-24 20:01 1576
	if (unlimitedLoop>9) {
c53e6443 sago007 2012-04-17 11:04 1577
		AIstatus = 0;
56241205 sago007 2012-08-05 11:15 1578
		return;
c53e6443 sago007 2012-04-17 11:04 1579
	}
c53e6443 sago007 2012-04-17 11:04 1580
	//cout << ", ptc: " << placeToCenter << ", line: " << AIlineToClear << ", cy: " << cursory;
ecef5838 sago007 2015-11-24 20:01 1581
	if (cursory+1>AIlineToClear+2) {
c53e6443 sago007 2012-04-17 11:04 1582
		// cout << ", cursory>line+2";
c53e6443 sago007 2012-04-17 11:04 1583
		MoveCursor('S');
c53e6443 sago007 2012-04-17 11:04 1584
	}
ecef5838 sago007 2015-11-24 20:01 1585
	if (cursory+1<AIlineToClear) {
c53e6443 sago007 2012-04-17 11:04 1586
		MoveCursor('N');
ecef5838 sago007 2015-11-24 20:01 1587
	}
c53e6443 sago007 2012-04-17 11:04 1588
	bool toAlign[3]= {true, true, true};
ecef5838 sago007 2015-11-24 20:01 1589
	if (board[placeToCenter][AIlineToClear+0]==AIcolorToClear) {
c53e6443 sago007 2012-04-17 11:04 1590
		toAlign[0]=false;
ecef5838 sago007 2015-11-24 20:01 1591
	}
ecef5838 sago007 2015-11-24 20:01 1592
	if (board[placeToCenter][AIlineToClear+1]==AIcolorToClear) {
c53e6443 sago007 2012-04-17 11:04 1593
		toAlign[1]=false;
ecef5838 sago007 2015-11-24 20:01 1594
	}
ecef5838 sago007 2015-11-24 20:01 1595
	if (board[placeToCenter][AIlineToClear+2]==AIcolorToClear) {
c53e6443 sago007 2012-04-17 11:04 1596
		toAlign[2]=false;
ecef5838 sago007 2015-11-24 20:01 1597
	}
c53e6443 sago007 2012-04-17 11:04 1598
	//cout << "status: " << toAlign[0] << " " << toAlign[1] << " " << toAlign[2];
ecef5838 sago007 2015-11-24 20:01 1599
	if (cursory+1==AIlineToClear) {
ecef5838 sago007 2015-11-24 20:01 1600
		if (toAlign[0]==false) {
c53e6443 sago007 2012-04-17 11:04 1601
			MoveCursor('N');
ecef5838 sago007 2015-11-24 20:01 1602
		}
ecef5838 sago007 2015-11-24 20:01 1603
		else {
ecef5838 sago007 2015-11-24 20:01 1604
			if (cursorx>closestTo(AIlineToClear, AIcolorToClear, placeToCenter)) {
c53e6443 sago007 2012-04-17 11:04 1605
				MoveCursor('W');
ecef5838 sago007 2015-11-24 20:01 1606
			}
ecef5838 sago007 2015-11-24 20:01 1607
			else if (cursorx<closestTo(AIlineToClear, AIcolorToClear, placeToCenter)) {
c53e6443 sago007 2012-04-17 11:04 1608
				MoveCursor('E');
ecef5838 sago007 2015-11-24 20:01 1609
			}
ecef5838 sago007 2015-11-24 20:01 1610
			else {
c53e6443 sago007 2012-04-17 11:04 1611
				SwitchAtCursor();
ecef5838 sago007 2015-11-24 20:01 1612
			}
c53e6443 sago007 2012-04-17 11:04 1613
		}
c53e6443 sago007 2012-04-17 11:04 1614
	}
ecef5838 sago007 2015-11-24 20:01 1615
	else if (cursory+1==AIlineToClear+1) {
ecef5838 sago007 2015-11-24 20:01 1616
		if (toAlign[1]==false) {
ecef5838 sago007 2015-11-24 20:01 1617
			if (toAlign[2]) {
c53e6443 sago007 2012-04-17 11:04 1618
				MoveCursor('N');
ecef5838 sago007 2015-11-24 20:01 1619
			}
ecef5838 sago007 2015-11-24 20:01 1620
			else {
c53e6443 sago007 2012-04-17 11:04 1621
				MoveCursor('S');
ecef5838 sago007 2015-11-24 20:01 1622
			}
c53e6443 sago007 2012-04-17 11:04 1623
		}
ecef5838 sago007 2015-11-24 20:01 1624
		else {
ecef5838 sago007 2015-11-24 20:01 1625
			if (cursorx>closestTo(AIlineToClear+1, AIcolorToClear, placeToCenter)) {
c53e6443 sago007 2012-04-17 11:04 1626
				MoveCursor('W');
ecef5838 sago007 2015-11-24 20:01 1627
			}
ecef5838 sago007 2015-11-24 20:01 1628
			else if (cursorx<closestTo(AIlineToClear+1, AIcolorToClear, placeToCenter)) {
c53e6443 sago007 2012-04-17 11:04 1629
				MoveCursor('E');
ecef5838 sago007 2015-11-24 20:01 1630
			}
ecef5838 sago007 2015-11-24 20:01 1631
			else {
c53e6443 sago007 2012-04-17 11:04 1632
				SwitchAtCursor();
ecef5838 sago007 2015-11-24 20:01 1633
			}
c53e6443 sago007 2012-04-17 11:04 1634
		}
c53e6443 sago007 2012-04-17 11:04 1635
	}
ecef5838 sago007 2015-11-24 20:01 1636
	else if (cursory+1==AIlineToClear+2) {
ecef5838 sago007 2015-11-24 20:01 1637
		if (toAlign[2]==false) {
c53e6443 sago007 2012-04-17 11:04 1638
			MoveCursor('S');
ecef5838 sago007 2015-11-24 20:01 1639
		}
ecef5838 sago007 2015-11-24 20:01 1640
		else {
ecef5838 sago007 2015-11-24 20:01 1641
			if (cursorx>closestTo(AIlineToClear+2, AIcolorToClear, placeToCenter)) {
c53e6443 sago007 2012-04-17 11:04 1642
				MoveCursor('W');
ecef5838 sago007 2015-11-24 20:01 1643
			}
ecef5838 sago007 2015-11-24 20:01 1644
			else if (cursorx<closestTo(AIlineToClear+2, AIcolorToClear, placeToCenter)) {
c53e6443 sago007 2012-04-17 11:04 1645
				MoveCursor('E');
ecef5838 sago007 2015-11-24 20:01 1646
			}
ecef5838 sago007 2015-11-24 20:01 1647
			else {
c53e6443 sago007 2012-04-17 11:04 1648
				SwitchAtCursor();
ecef5838 sago007 2015-11-24 20:01 1649
			}
c53e6443 sago007 2012-04-17 11:04 1650
		}
c53e6443 sago007 2012-04-17 11:04 1651
	}
c53e6443 sago007 2012-04-17 11:04 1652
ecef5838 sago007 2015-11-24 20:01 1653
	if ((!toAlign[0])&&(!toAlign[1])&&(!toAlign[2])) {
c53e6443 sago007 2012-04-17 11:04 1654
		AIstatus = 0;
ecef5838 sago007 2015-11-24 20:01 1655
	}
ecef5838 sago007 2015-11-24 20:01 1656
	if ((nrOfType(AIlineToClear, AIcolorToClear)==0)||(nrOfType(AIlineToClear+1, AIcolorToClear)==0)||(nrOfType(AIlineToClear+2, AIcolorToClear)==0)) {
c53e6443 sago007 2012-04-17 11:04 1657
		AIstatus = 0;
ecef5838 sago007 2015-11-24 20:01 1658
	}
c53e6443 sago007 2012-04-17 11:04 1659
	//cout << endl;
c53e6443 sago007 2012-04-17 11:04 1660
}
c53e6443 sago007 2012-04-17 11:04 1661
c53e6443 sago007 2012-04-17 11:04 1662
ecef5838 sago007 2015-11-24 20:01 1663
void BlockGame::AI_Move() {
ecef5838 sago007 2015-11-24 20:01 1664
	switch (AIstatus) {
c53e6443 sago007 2012-04-17 11:04 1665
	case 1:
ecef5838 sago007 2015-11-24 20:01 1666
		if (TowerHeight<8) {
ecef5838 sago007 2015-11-24 20:01 1667
			PushLine();
ecef5838 sago007 2015-11-24 20:01 1668
		}
ecef5838 sago007 2015-11-24 20:01 1669
		else {
ecef5838 sago007 2015-11-24 20:01 1670
			AIstatus = 0;
ecef5838 sago007 2015-11-24 20:01 1671
		}
c53e6443 sago007 2012-04-17 11:04 1672
		break;
c53e6443 sago007 2012-04-17 11:04 1673
	case 2:
c53e6443 sago007 2012-04-17 11:04 1674
		AI_ClearTower();
c53e6443 sago007 2012-04-17 11:04 1675
		break;
c53e6443 sago007 2012-04-17 11:04 1676
	case 3:
c53e6443 sago007 2012-04-17 11:04 1677
		AI_ClearHori();
c53e6443 sago007 2012-04-17 11:04 1678
		break;
c53e6443 sago007 2012-04-17 11:04 1679
	case 4:
c53e6443 sago007 2012-04-17 11:04 1680
		AI_ClearVertical();
c53e6443 sago007 2012-04-17 11:04 1681
		break;
c53e6443 sago007 2012-04-17 11:04 1682
	case 5:
ecef5838 sago007 2015-11-24 20:01 1683
		if (!firstLineCreated) {
c53e6443 sago007 2012-04-17 11:04 1684
			PushLine();
c53e6443 sago007 2012-04-17 11:04 1685
			firstLineCreated = true;
c53e6443 sago007 2012-04-17 11:04 1686
		}
ecef5838 sago007 2015-11-24 20:01 1687
		else {
c53e6443 sago007 2012-04-17 11:04 1688
			PushLine();
c53e6443 sago007 2012-04-17 11:04 1689
			AIstatus = 0;
c53e6443 sago007 2012-04-17 11:04 1690
		}
c53e6443 sago007 2012-04-17 11:04 1691
		break;
c53e6443 sago007 2012-04-17 11:04 1692
	case 6:
c53e6443 sago007 2012-04-17 11:04 1693
		PushLine();
c53e6443 sago007 2012-04-17 11:04 1694
		AIstatus = 0;
c53e6443 sago007 2012-04-17 11:04 1695
		break;
c53e6443 sago007 2012-04-17 11:04 1696
	default:
ecef5838 sago007 2015-11-24 20:01 1697
		if (TowerHeight<6) {
ecef5838 sago007 2015-11-24 20:01 1698
			AIstatus = 1;
ecef5838 sago007 2015-11-24 20:01 1699
		}
ecef5838 sago007 2015-11-24 20:01 1700
		else if (horiClearPossible()) {
ecef5838 sago007 2015-11-24 20:01 1701
			AIstatus = 3;
ecef5838 sago007 2015-11-24 20:01 1702
		}
ecef5838 sago007 2015-11-24 20:01 1703
		else if (veriClearPossible()) {
ecef5838 sago007 2015-11-24 20:01 1704
			AIstatus = 4;
ecef5838 sago007 2015-11-24 20:01 1705
		}
ecef5838 sago007 2015-11-24 20:01 1706
		else if (ThereIsATower()) {
ecef5838 sago007 2015-11-24 20:01 1707
			AIstatus = 2;
ecef5838 sago007 2015-11-24 20:01 1708
		}
ecef5838 sago007 2015-11-24 20:01 1709
		else {
c53e6443 sago007 2012-04-17 11:04 1710
			AIstatus = 5;
ecef5838 sago007 2015-11-24 20:01 1711
		}
c53e6443 sago007 2012-04-17 11:04 1712
		break;
c53e6443 sago007 2012-04-17 11:04 1713
	}
c53e6443 sago007 2012-04-17 11:04 1714
}
17916a2e sago007 2010-11-07 11:26 1715
17916a2e sago007 2010-11-07 11:26 1716
//////////////////////////////////////////////////////////////////////////
17916a2e sago007 2010-11-07 11:26 1717
///////////////////////////// AI ends here! //////////////////////////////
17916a2e sago007 2010-11-07 11:26 1718
//////////////////////////////////////////////////////////////////////////
17916a2e sago007 2010-11-07 11:26 1719
17916a2e sago007 2010-11-07 11:26 1720
c53e6443 sago007 2012-04-17 11:04 1721
//Updates evrything, if not called nothing happends
ecef5838 sago007 2015-11-24 20:01 1722
void BlockGame::Update() {
c53e6443 sago007 2012-04-17 11:04 1723
	Uint32 tempUInt32;
c53e6443 sago007 2012-04-17 11:04 1724
	Uint32 nowTime = ticks; //We remember the time, so it doesn't change during this call
79d3c1d3 sago007 2012-05-05 15:54 1725
c53e6443 sago007 2012-04-17 11:04 1726
	{
c53e6443 sago007 2012-04-17 11:04 1727
		FindTowerHeight();
ca486238 sago007 2015-08-23 15:12 1728
		if ((linesCleared-TowerHeight>stageClearLimit) && (stageClear) && (!bGameOver)) {
c53e6443 sago007 2012-04-17 11:04 1729
			stageCleared[Level] = true;
ecef5838 sago007 2015-11-24 20:01 1730
			if (stageScores[Level]<score) {
c53e6443 sago007 2012-04-17 11:04 1731
				gameEndedAfter = nowTime-gameStartedAt;
c53e6443 sago007 2012-04-17 11:04 1732
				stageScores[Level] = score;
c53e6443 sago007 2012-04-17 11:04 1733
				stageTimes[Level] = gameEndedAfter;
c53e6443 sago007 2012-04-17 11:04 1734
			}
c53e6443 sago007 2012-04-17 11:04 1735
c53e6443 sago007 2012-04-17 11:04 1736
			ofstream outfile;
c53e6443 sago007 2012-04-17 11:04 1737
			outfile.open(stageClearSavePath.c_str(), ios::binary |ios::trunc);
ecef5838 sago007 2015-11-24 20:01 1738
			if (!outfile) {
56241205 sago007 2012-08-05 11:15 1739
				cerr << "Error writing to file: " << stageClearSavePath << endl;
c53e6443 sago007 2012-04-17 11:04 1740
			}
ecef5838 sago007 2015-11-24 20:01 1741
			else {
ecef5838 sago007 2015-11-24 20:01 1742
				for (int i=0; i<nrOfStageLevels; i++) {
c53e6443 sago007 2012-04-17 11:04 1743
					bool tempBool = stageCleared[i];
c53e6443 sago007 2012-04-17 11:04 1744
					outfile.write(reinterpret_cast<char*>(&tempBool), sizeof(bool));
c53e6443 sago007 2012-04-17 11:04 1745
				}
ecef5838 sago007 2015-11-24 20:01 1746
				for (int i=0; i<nrOfStageLevels; i++) {
c53e6443 sago007 2012-04-17 11:04 1747
					tempUInt32 = stageScores[i];
c53e6443 sago007 2012-04-17 11:04 1748
					outfile.write(reinterpret_cast<char*>(&tempUInt32), sizeof(Uint32));
c53e6443 sago007 2012-04-17 11:04 1749
				}
ecef5838 sago007 2015-11-24 20:01 1750
				for (int i=0; i<nrOfStageLevels; i++) {
c53e6443 sago007 2012-04-17 11:04 1751
					tempUInt32 = stageTimes[i];
c53e6443 sago007 2012-04-17 11:04 1752
					outfile.write(reinterpret_cast<char*>(&tempUInt32), sizeof(Uint32));
c53e6443 sago007 2012-04-17 11:04 1753
				}
c53e6443 sago007 2012-04-17 11:04 1754
				outfile.close();
c53e6443 sago007 2012-04-17 11:04 1755
			}
c53e6443 sago007 2012-04-17 11:04 1756
			setPlayerWon();
c53e6443 sago007 2012-04-17 11:04 1757
			stageButtonStatus = SBstageClear;
c53e6443 sago007 2012-04-17 11:04 1758
		}
ecef5838 sago007 2015-11-24 20:01 1759
		if ((TowerHeight>12)&&(prevTowerHeight<13)&&(!puzzleMode)) {
c53e6443 sago007 2012-04-17 11:04 1760
			stop+=1000;
c53e6443 sago007 2012-04-17 11:04 1761
		}
c53e6443 sago007 2012-04-17 11:04 1762
ecef5838 sago007 2015-11-24 20:01 1763
		while (nowTime>nrStops*40+gameStartedAt) { //Increase stops, till we reach nowTime
ecef5838 sago007 2015-11-24 20:01 1764
			if (stop>0) {
c53e6443 sago007 2012-04-17 11:04 1765
				stop = stop-20;
ecef5838 sago007 2015-11-24 20:01 1766
				if (stop<=0) {
ecef5838 sago007 2015-11-24 20:01 1767
					nrPushedPixel=(int)((nowTime-gameStartedAt)/(1000.0*speed));
ecef5838 sago007 2015-11-24 20:01 1768
				}
c53e6443 sago007 2012-04-17 11:04 1769
			}
ecef5838 sago007 2015-11-24 20:01 1770
			if (stop<0) {
c53e6443 sago007 2012-04-17 11:04 1771
				stop = 0;
ecef5838 sago007 2015-11-24 20:01 1772
			}
c53e6443 sago007 2012-04-17 11:04 1773
			nrStops++;
c53e6443 sago007 2012-04-17 11:04 1774
		}
c53e6443 sago007 2012-04-17 11:04 1775
		//If we have static content, we don't raise at all!
055218be sago007 2015-08-22 18:44 1776
		if (hasStaticContent()) {
c53e6443 sago007 2012-04-17 11:04 1777
			stop++;
055218be sago007 2015-08-22 18:44 1778
		}
055218be sago007 2015-08-22 18:44 1779
		if ((puzzleMode)&&(!bGameOver)&&BoardEmpty()) {
055218be sago007 2015-08-22 18:44 1780
			if (!singlePuzzle) {
055218be sago007 2015-08-22 18:44 1781
				PuzzleSetClear(Level);
c53e6443 sago007 2012-04-17 11:04 1782
				stageButtonStatus = SBpuzzleMode;
c53e6443 sago007 2012-04-17 11:04 1783
			}
c53e6443 sago007 2012-04-17 11:04 1784
			setPlayerWon();
c53e6443 sago007 2012-04-17 11:04 1785
		}
c53e6443 sago007 2012-04-17 11:04 1786
c53e6443 sago007 2012-04-17 11:04 1787
		//increse speed:
ecef5838 sago007 2015-11-24 20:01 1788
		if ((nowTime>gameStartedAt+20000*speedLevel)&&(speedLevel <99)&&(!bGameOver)) {
c53e6443 sago007 2012-04-17 11:04 1789
			speed = (baseSpeed*0.9)/((double)speedLevel*0.5);
c53e6443 sago007 2012-04-17 11:04 1790
			speedLevel++;
c53e6443 sago007 2012-04-17 11:04 1791
			nrPushedPixel=(int)((double)(nowTime-gameStartedAt)/(1000.0*speed));
c53e6443 sago007 2012-04-17 11:04 1792
		}
c53e6443 sago007 2012-04-17 11:04 1793
c53e6443 sago007 2012-04-17 11:04 1794
c53e6443 sago007 2012-04-17 11:04 1795
		//To prevent the stack from raising a lot then we stop a chain (doesn't work anymore)
ecef5838 sago007 2015-11-24 20:01 1796
		if (chain>0) {
c53e6443 sago007 2012-04-17 11:04 1797
			stop+=1;
ecef5838 sago007 2015-11-24 20:01 1798
		}
c53e6443 sago007 2012-04-17 11:04 1799
		//Raises the stack
c53e6443 sago007 2012-04-17 11:04 1800
		if ((nowTime>gameStartedAt+nrPushedPixel*1000*speed) && (!bGameOver)&&(!stop))
ecef5838 sago007 2015-11-24 20:01 1801
			while ((nowTime>gameStartedAt+nrPushedPixel*1000*speed)&&(!(puzzleMode))) {
c53e6443 sago007 2012-04-17 11:04 1802
				PushPixels();
ecef5838 sago007 2015-11-24 20:01 1803
			}
ecef5838 sago007 2015-11-24 20:01 1804
		if (!bGameOver) {
ecef5838 sago007 2015-11-24 20:01 1805
			ClearBlocks();
ecef5838 sago007 2015-11-24 20:01 1806
		}
c53e6443 sago007 2012-04-17 11:04 1807
c53e6443 sago007 2012-04-17 11:04 1808
		/*************************************************************
c53e6443 sago007 2012-04-17 11:04 1809
		Ai stuff
c53e6443 sago007 2012-04-17 11:04 1810
		 **************************************************************/
ecef5838 sago007 2015-11-24 20:01 1811
		if (bGameOver) {
c53e6443 sago007 2012-04-17 11:04 1812
			AIstatus = 0;       //Enusres that AI is resetted
c53e6443 sago007 2012-04-17 11:04 1813
		}
c53e6443 sago007 2012-04-17 11:04 1814
		else if (AI_Enabled)
ecef5838 sago007 2015-11-24 20:01 1815
			if (lastAImove+AI_MoveSpeed<ticks) {
c53e6443 sago007 2012-04-17 11:04 1816
				AI_Move();
c53e6443 sago007 2012-04-17 11:04 1817
				lastAImove=ticks;
c53e6443 sago007 2012-04-17 11:04 1818
			}
c53e6443 sago007 2012-04-17 11:04 1819
c53e6443 sago007 2012-04-17 11:04 1820
		/*************************************************************
c53e6443 sago007 2012-04-17 11:04 1821
		Ai stuff ended
c53e6443 sago007 2012-04-17 11:04 1822
		 **************************************************************/
ecef5838 sago007 2015-11-24 20:01 1823
		if ((nowTime>gameStartedAt+nrFellDown*140) && (!bGameOver)) {
ecef5838 sago007 2015-11-24 20:01 1824
			FallDown();
ecef5838 sago007 2015-11-24 20:01 1825
		}
ecef5838 sago007 2015-11-24 20:01 1826
		if ((nowTime<gameStartedAt)&&(puzzleMode)) {
c53e6443 sago007 2012-04-17 11:04 1827
			FallDown();
c53e6443 sago007 2012-04-17 11:04 1828
			nrFellDown--;
c53e6443 sago007 2012-04-17 11:04 1829
		}
c53e6443 sago007 2012-04-17 11:04 1830
		ReduceStuff();
ecef5838 sago007 2015-11-24 20:01 1831
		if ((timetrial) && (!bGameOver) && (nowTime>gameStartedAt+2*60*1000)) {
c53e6443 sago007 2012-04-17 11:04 1832
			SetGameOver();
313ecd1b sago007 2015-08-22 20:38 1833
			TimeTrialEndEvent();
c53e6443 sago007 2012-04-17 11:04 1834
		}
c53e6443 sago007 2012-04-17 11:04 1835
	}
c53e6443 sago007 2012-04-17 11:04 1836
}
c53e6443 sago007 2012-04-17 11:04 1837
ecef5838 sago007 2015-11-24 20:01 1838
bool BlockGame::IsNearDeath() {
ecef5838 sago007 2015-11-24 20:01 1839
	if ((TowerHeight>12)&&(!puzzleMode)&&(!bGameOver)) {
80b830cd sago007 2012-08-19 17:51 1840
		return true;
ecef5838 sago007 2015-11-24 20:01 1841
	}
ecef5838 sago007 2015-11-24 20:01 1842
	else {
80b830cd sago007 2012-08-19 17:51 1843
		return false;
ecef5838 sago007 2015-11-24 20:01 1844
	}
18055aa0 sago007 2012-06-05 19:43 1845
}
18055aa0 sago007 2012-06-05 19:43 1846
ecef5838 sago007 2015-11-24 20:01 1847
void BlockGame::UpdateInternal(unsigned int newtick) {
ecef5838 sago007 2015-11-24 20:01 1848
	while (newtick >= ticks+50) {
6c9b4cd7 sago007 2012-05-01 19:34 1849
		ticks+=50;
d12916ab sago007 2012-04-19 23:30 1850
		Update();
d12916ab sago007 2012-04-19 23:30 1851
	}
d12916ab sago007 2012-04-19 23:30 1852
}
d12916ab sago007 2012-04-19 23:30 1853
ecef5838 sago007 2015-11-24 20:01 1854
void BlockGame::Update(unsigned int newtick) {
ca486238 sago007 2015-08-23 15:12 1855
	UpdateInternal(newtick);
50258544 sago007 2012-05-10 19:11 1856
}
50258544 sago007 2012-05-10 19:11 1857
ecef5838 sago007 2015-11-24 20:01 1858
void BlockGame::PerformAction(unsigned int tick, int action, string param) {
444cec07 sago007 2012-05-05 16:42 1859
	ss.str(std::string());
444cec07 sago007 2012-05-05 16:42 1860
	ss.clear();
444cec07 sago007 2012-05-05 16:42 1861
	ss << param;
79d3c1d3 sago007 2012-05-05 15:54 1862
	int p1,p2;
79d3c1d3 sago007 2012-05-05 15:54 1863
	Uint32 p3;
ecef5838 sago007 2015-11-24 20:01 1864
	switch (action) {
6c9b4cd7 sago007 2012-05-01 19:34 1865
	case ACTION_UPDATE:
79d3c1d3 sago007 2012-05-05 15:54 1866
		UpdateInternal(tick);
6c9b4cd7 sago007 2012-05-01 19:34 1867
		break;
6c9b4cd7 sago007 2012-05-01 19:34 1868
	case ACTION_MOVECURSOR:
50258544 sago007 2012-05-10 19:11 1869
		MoveCursor(param.at(0));
6c9b4cd7 sago007 2012-05-01 19:34 1870
		break;
ecef5838 sago007 2015-11-24 20:01 1871
	case ACTION_MOVECURSORTO: {
80b830cd sago007 2012-08-19 17:51 1872
6c9b4cd7 sago007 2012-05-01 19:34 1873
		int p1,p2;
6c9b4cd7 sago007 2012-05-01 19:34 1874
		ss >> p1 >> p2;
6c9b4cd7 sago007 2012-05-01 19:34 1875
		MoveCursorTo(p1,p2);
6c9b4cd7 sago007 2012-05-01 19:34 1876
	}
80b830cd sago007 2012-08-19 17:51 1877
	break;
6c9b4cd7 sago007 2012-05-01 19:34 1878
	case ACTION_SWITCH:
6c9b4cd7 sago007 2012-05-01 19:34 1879
		SwitchAtCursor();
6c9b4cd7 sago007 2012-05-01 19:34 1880
		break;
6c9b4cd7 sago007 2012-05-01 19:34 1881
	case ACTION_PUSH:
6c9b4cd7 sago007 2012-05-01 19:34 1882
		PushLine();
6c9b4cd7 sago007 2012-05-01 19:34 1883
		break;
ecef5838 sago007 2015-11-24 20:01 1884
	case ACTION_CREATEGARBAGE: {
6c9b4cd7 sago007 2012-05-01 19:34 1885
		ss >> p1 >> p2;
6c9b4cd7 sago007 2012-05-01 19:34 1886
		CreateGarbage(p1,p2);
6c9b4cd7 sago007 2012-05-01 19:34 1887
	}
80b830cd sago007 2012-08-19 17:51 1888
	break;
6c9b4cd7 sago007 2012-05-01 19:34 1889
	case ACTION_CREATEGRAYGARBAGE:
6c9b4cd7 sago007 2012-05-01 19:34 1890
		CreateGreyGarbage();
6c9b4cd7 sago007 2012-05-01 19:34 1891
		break;
6c9b4cd7 sago007 2012-05-01 19:34 1892
	case ACTION_GAMEOVER:
6c9b4cd7 sago007 2012-05-01 19:34 1893
		SetGameOver();
6c9b4cd7 sago007 2012-05-01 19:34 1894
		break;
6c9b4cd7 sago007 2012-05-01 19:34 1895
	case ACTION_WIN:
6c9b4cd7 sago007 2012-05-01 19:34 1896
		setPlayerWon();
6c9b4cd7 sago007 2012-05-01 19:34 1897
		break;
6c9b4cd7 sago007 2012-05-01 19:34 1898
	case ACTION_DRAW:
6c9b4cd7 sago007 2012-05-01 19:34 1899
		setDraw();
6c9b4cd7 sago007 2012-05-01 19:34 1900
		break;
6c9b4cd7 sago007 2012-05-01 19:34 1901
	case ACTION_GAMESPEED:
79d3c1d3 sago007 2012-05-05 15:54 1902
		ss >> p1;
79d3c1d3 sago007 2012-05-05 15:54 1903
		setGameSpeed(p1);
6c9b4cd7 sago007 2012-05-01 19:34 1904
		break;
6c9b4cd7 sago007 2012-05-01 19:34 1905
	case ACTION_HANDICAP:
79d3c1d3 sago007 2012-05-05 15:54 1906
		ss >> p1;
79d3c1d3 sago007 2012-05-05 15:54 1907
		setHandicap(p1);
6c9b4cd7 sago007 2012-05-01 19:34 1908
		break;
6c9b4cd7 sago007 2012-05-01 19:34 1909
	case ACTION_NEW:
bbb533ad sago007 2012-05-08 19:38 1910
		break;
6c9b4cd7 sago007 2012-05-01 19:34 1911
	case ACTION_NEWTT:
bbb533ad sago007 2012-05-08 19:38 1912
		timetrial = true;
bbb533ad sago007 2012-05-08 19:38 1913
		break;
6c9b4cd7 sago007 2012-05-01 19:34 1914
	case ACTION_NEWVS:
6c9b4cd7 sago007 2012-05-01 19:34 1915
		break;
6c9b4cd7 sago007 2012-05-01 19:34 1916
	case ACTION_STARTBLOCKS:
79d3c1d3 sago007 2012-05-05 15:54 1917
		ss >> p3;
79d3c1d3 sago007 2012-05-05 15:54 1918
		putStartBlocks(p3);
6c9b4cd7 sago007 2012-05-01 19:34 1919
		break;
6c9b4cd7 sago007 2012-05-01 19:34 1920
	case ACTION_NOP:
6c9b4cd7 sago007 2012-05-01 19:34 1921
		break;
6c9b4cd7 sago007 2012-05-01 19:34 1922
	default:
6c9b4cd7 sago007 2012-05-01 19:34 1923
		cerr << "Unknown action: " << action << " " << param << endl;
6c9b4cd7 sago007 2012-05-01 19:34 1924
		break;
6c9b4cd7 sago007 2012-05-01 19:34 1925
	};
c53e6443 sago007 2012-04-17 11:04 1926
}
17916a2e sago007 2010-11-07 11:26 1927
////////////////////////////////////////////////////////////////////////////////
17916a2e sago007 2010-11-07 11:26 1928
///////////////////////// BlockAttack class end ////////////////////////////////
17916a2e sago007 2010-11-07 11:26 1929
////////////////////////////////////////////////////////////////////////////////
1970-01-01 00:00 1930