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