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