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