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