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