git repos / blockattack-game

blame: source/code/BlockGame.cpp

normal view · raw

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