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