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