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