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