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