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