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