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