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