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