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