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