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