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