git repos / blockattack-game

blame: source/code/BlockGame.cpp

normal view · raw

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