git repos / blockattack-game

blame: source/code/BlockGame.cpp

normal view · raw

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