git repos / blockattack-game

blame: source/code/BlockGame.cpp

normal view · raw

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