git repos / blockattack-game

blame: source/code/BlockGame.cpp

normal view · raw

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