| 1 | /* |
| 2 | =========================================================================== |
| 3 | blockattack - Block Attack - Rise of the Blocks |
| 4 | Copyright (C) 2005-2012 Poul Sander |
| 5 | |
| 6 | This program is free software: you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation, either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program. If not, see http://www.gnu.org/licenses/ |
| 18 | |
| 19 | Source information and contacts persons can be found at |
| 20 | http://www.blockattack.net |
| 21 | =========================================================================== |
| 22 | */ |
| 23 | |
| 24 | #ifndef BLOCKGAME_HPP |
| 25 | #define BLOCKGAME_HPP 1 |
| 26 | |
| 27 | #include "nlohmann/json.hpp" |
| 28 | #include "stats.h" |
| 29 | #include "common.h" |
| 30 | #include <deque> |
| 31 | |
| 32 | #define NUMBEROFCHAINS 100 |
| 33 | #define BLOCKWAIT 100000 |
| 34 | #define BLOCKHANG 1000 |
| 35 | |
| 36 | enum stageButton {SBdontShow, SBstageClear, SBpuzzleMode}; |
| 37 | |
| 38 | extern stageButton stageButtonStatus; |
| 39 | |
| 40 | //This is the size of the blocks. They are always 50. The internal logic calculates it that way |
| 41 | const int bsize = 50; |
| 42 | |
| 43 | /** |
| 44 | * This struct defines the start conditions of the game |
| 45 | */ |
| 46 | struct BlockGameStartInfo { |
| 47 | unsigned int ticks = 0; |
| 48 | bool timeTrial = false; |
| 49 | ///True means a stage clear game will be started. level must be set too. |
| 50 | bool stageClear = false; |
| 51 | ///True if puzzle mode. level must be set too. |
| 52 | bool puzzleMode = false; |
| 53 | ///Single puzzle is used for the editor only. |
| 54 | bool singlePuzzle = false; |
| 55 | int level = 0; |
| 56 | bool AI = false; |
| 57 | /** |
| 58 | * True means that stats will be recorded. |
| 59 | * If AI is true then this will be overruled to false |
| 60 | */ |
| 61 | bool recordStats = true; |
| 62 | bool vsMode = false; |
| 63 | /** |
| 64 | * Set to true if we are fighting an AI. level should be the AI level we are fighting |
| 65 | */ |
| 66 | bool vsAI = false; |
| 67 | int startBlocks = -1; |
| 68 | int handicap = 0; |
| 69 | int gameSpeed = 0; |
| 70 | int basicBlockVariants = 6; |
| 71 | NLOHMANN_DEFINE_TYPE_INTRUSIVE(BlockGameStartInfo, ticks, timeTrial, stageClear, puzzleMode, singlePuzzle, level, AI, recordStats, vsMode, vsAI, startBlocks, handicap, gameSpeed, basicBlockVariants); |
| 72 | }; |
| 73 | |
| 74 | struct GarbageStruct { |
| 75 | bool greyGarbage = false; |
| 76 | int width = 0; |
| 77 | int height = 0; |
| 78 | void setGarbage(int w, int h, bool g = false) { |
| 79 | greyGarbage = g; |
| 80 | width = w; |
| 81 | height = h; |
| 82 | } |
| 83 | NLOHMANN_DEFINE_TYPE_INTRUSIVE(GarbageStruct, greyGarbage, width, height); |
| 84 | }; |
| 85 | |
| 86 | struct BlockGameAction { |
| 87 | enum class Action {NONE, UPDATE, SET_DRAW, SET_WON, SET_GAME_OVER, MOVE, SWITCH, PUSH, PUSH_GARBAGE, MOUSE_DOWN, MOUSE_UP, MOUSE_MOVE}; |
| 88 | Action action = Action::NONE; |
| 89 | unsigned int tick = 0; //< Used for update |
| 90 | char way = '\0'; //< The direction to move the cursor: 'N', 'E', 'S' or 'W' |
| 91 | int x = 0; |
| 92 | int y = 0; |
| 93 | std::vector<GarbageStruct> garbage; |
| 94 | }; |
| 95 | |
| 96 | static void to_json(nlohmann::json& j, const BlockGameAction& p) { |
| 97 | j = nlohmann::json{ {"action", p.action}, {"tick", p.tick}, {"way", p.way}, {"x", p.x}, {"y", p.y}, {"garbage", p.garbage} }; |
| 98 | } |
| 99 | |
| 100 | static void from_json(const nlohmann::json& j, BlockGameAction& p) { |
| 101 | j.at("action").get_to(p.action); |
| 102 | j.at("tick").get_to(p.tick); |
| 103 | j.at("way").get_to(p.way); |
| 104 | j.at("x").get_to(p.x); |
| 105 | j.at("y").get_to(p.y); |
| 106 | j.at("garbage").get_to(p.garbage); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | struct BlockGameInfoExtra { |
| 111 | std::string name; |
| 112 | int score = 0; |
| 113 | int seconds = 0; |
| 114 | NLOHMANN_DEFINE_TYPE_INTRUSIVE(BlockGameInfoExtra, name, score, seconds); |
| 115 | }; |
| 116 | |
| 117 | struct BlockGameInfo { |
| 118 | BlockGameStartInfo startInfo; |
| 119 | std::deque<BlockGameAction> actions; |
| 120 | BlockGameInfoExtra extra; |
| 121 | NLOHMANN_DEFINE_TYPE_INTRUSIVE(BlockGameInfo, startInfo, actions, extra); |
| 122 | }; |
| 123 | |
| 124 | //////////////////////////////////////////////////////////////////////////////// |
| 125 | //The BloackGame class represents a board, score, time etc. for a single player/ |
| 126 | //////////////////////////////////////////////////////////////////////////////// |
| 127 | class BlockGame { |
| 128 | private: |
| 129 | int prevTowerHeight = 0; |
| 130 | bool bGarbageFallLeft = false; |
| 131 | bool singlePuzzle = false; |
| 132 | |
| 133 | int nextGarbageNumber = 0; |
| 134 | int pushedPixelAt = 0; |
| 135 | int nrPushedPixel = 0; |
| 136 | int nrFellDown = 0; |
| 137 | unsigned int nrStops = 0; |
| 138 | bool garbageToBeCleared[7][30] = {}; |
| 139 | unsigned int lastAImove = 0; |
| 140 | |
| 141 | int AI_LineOffset = 0; //how many lines have changed since command |
| 142 | int hangTicks = 0; //How many times have hang been decreased? |
| 143 | //int the two following index 0 may NOT be used (what the fuck did I meen?) |
| 144 | int chainSize[NUMBEROFCHAINS] {}; //Contains the chains |
| 145 | bool chainUsed[NUMBEROFCHAINS] {}; //True if the chain is used |
| 146 | |
| 147 | unsigned int nextRandomNumber = 0; |
| 148 | int Level = 0; //Only used in stageClear and puzzle (not implemented) |
| 149 | |
| 150 | BlockGameInfo replayInfo; |
| 151 | |
| 152 | int rand2(); |
| 153 | int firstUnusedChain(); |
| 154 | |
| 155 | protected: |
| 156 | int lastCounter = 0; |
| 157 | std::string strHolder; |
| 158 | bool bDraw = false; |
| 159 | unsigned int ticks = 0; |
| 160 | unsigned int gameStartedAt = 0; |
| 161 | unsigned int gameEndedAfter = 0; //How long did the game last? |
| 162 | int linesCleared = 0; |
| 163 | int TowerHeight = 0; |
| 164 | int board[7][30]; |
| 165 | int stop = 0; |
| 166 | int speedLevel = 0; |
| 167 | int pixels = 0; |
| 168 | int MovesLeft = 0; |
| 169 | bool timetrial = false; |
| 170 | bool stageClear = false; |
| 171 | bool vsMode = false; |
| 172 | bool puzzleMode = false; |
| 173 | int stageClearLimit = 0; //stores number of lines user must clear to win |
| 174 | int combo = 0; |
| 175 | int chain = 0; |
| 176 | int cursorx = 2; //stores cursor position |
| 177 | int cursory = 3; // -||- |
| 178 | int mouse_cursorx = -1; //Stores the mouse hold cursor. -1 if nothing is selected. |
| 179 | int mouse_cursory = -1; |
| 180 | double speed = 0.0; |
| 181 | double baseSpeed = 0.0; //factor for speed. Lower value = faster gameplay |
| 182 | int score = 0; |
| 183 | bool bGameOver = false; |
| 184 | bool hasWonTheGame = false; |
| 185 | int AI_MoveSpeed = 0; //How often will the computer move? milliseconds |
| 186 | bool AI_Enabled = false; |
| 187 | bool recordStats = true; |
| 188 | bool vsAI = false; //Set to true for single player vs |
| 189 | |
| 190 | int handicap = 0; |
| 191 | |
| 192 | // Number of different basic blocks. Default is 6. Easy mde is limited t 5. |
| 193 | int basicBlockVariants = 6; |
| 194 | |
| 195 | std::vector<GarbageStruct> garbageSendQueue; |
| 196 | |
| 197 | int AIlineToClear = 0; |
| 198 | |
| 199 | short AIstatus = 0; //Status flags: |
| 200 | //0: nothing, 2: clear tower, 3: clear horisontal, 4: clear vertical |
| 201 | //1: make more lines, 5: make 2 lines, 6: make 1 line |
| 202 | |
| 203 | public: |
| 204 | |
| 205 | std::string name; |
| 206 | |
| 207 | public: |
| 208 | BlockGame(); |
| 209 | virtual ~BlockGame() = default; |
| 210 | |
| 211 | int getAIlevel() const; |
| 212 | |
| 213 | virtual void AddText(int, int, unsigned int, int) const {} |
| 214 | virtual void AddBall(int, int, bool, int) const {} |
| 215 | virtual void AddExplosion(int, int) const {} |
| 216 | virtual void PlayerWonEvent() const {} |
| 217 | virtual void DrawEvent() const {} |
| 218 | virtual void BlockPopEvent() const {} |
| 219 | virtual void LongChainDoneEvent() const {} |
| 220 | virtual void TimeTrialEndEvent() const {} |
| 221 | virtual void EndlessHighscoreEvent() const {} |
| 222 | |
| 223 | void NewGame(const BlockGameStartInfo& s); |
| 224 | void DoAction (const BlockGameAction& action); |
| 225 | /** |
| 226 | * This function returns all the garbage. |
| 227 | * This is actual const in the way that it does not change the games state |
| 228 | * Technically it is not const because it empties the queue that are stored inside the object even if not part of the game state. |
| 229 | * @param poppedData |
| 230 | */ |
| 231 | void PopSendGarbage(std::vector<GarbageStruct>& poppedData); |
| 232 | |
| 233 | int GetScore() const; |
| 234 | int GetHandicap() const; |
| 235 | bool isGameOver() const; |
| 236 | int GetGameStartedAt() const; |
| 237 | int GetGameEndedAt() const; |
| 238 | bool isTimeTrial() const; |
| 239 | bool isStageClear() const; |
| 240 | bool isVsMode() const; |
| 241 | bool isPuzzleMode() const; |
| 242 | int GetLinesCleared() const; |
| 243 | int GetStageClearLimit() const; |
| 244 | int GetChains() const; |
| 245 | int GetPixels() const; |
| 246 | int GetSpeedLevel() const; |
| 247 | int GetTowerHeight() const; |
| 248 | int GetCursorX() const; |
| 249 | int GetCursorY() const; |
| 250 | void GetMouseCursor(bool& pressed, int& x, int& y) const; |
| 251 | bool GetIsWinner() const; |
| 252 | bool isSinglePuzzle() const; |
| 253 | int getLevel() const; |
| 254 | bool GetAIenabled() const; |
| 255 | bool IsNearDeath() const; |
| 256 | double GetBaseSpeed() const { |
| 257 | return baseSpeed; |
| 258 | } |
| 259 | int GetBasicBlockVariants() const { |
| 260 | return basicBlockVariants; |
| 261 | } |
| 262 | const BlockGameInfo& GetBlockGameInfo() { |
| 263 | return replayInfo; |
| 264 | } |
| 265 | void putSampleBlocks(); |
| 266 | private: |
| 267 | void NewGameInternal(unsigned int ticks); |
| 268 | //Test if LineNr is an empty line, returns false otherwise. |
| 269 | bool LineEmpty(int lineNr) const; |
| 270 | //Test if the entire board is empty (used for Puzzles) |
| 271 | bool BoardEmpty() const; |
| 272 | //Anything that the user can't move? In that case Game Over cannot occur |
| 273 | bool hasStaticContent() const; |
| 274 | void putStartBlocks(); |
| 275 | void putStartBlocks(int n); |
| 276 | //decreases hang for all hanging blocks and wait for waiting blocks |
| 277 | void ReduceStuff(); |
| 278 | void setGameSpeed(int globalSpeedLevel); |
| 279 | void setHandicap(int globalHandicap); |
| 280 | //Clears garbage, must take one the lower left corner! |
| 281 | int GarbageClearer(int x, int y, int number, bool aLineToClear, int chain); |
| 282 | //Marks garbage that must be cleared |
| 283 | int GarbageMarker(int x, int y); |
| 284 | int FirstGarbageMarker(int x, int y); |
| 285 | //Clear Blocks if 3 or more is alligned (naive implemented) |
| 286 | void ClearBlocks(); |
| 287 | //Moves all peaces a spot down if possible |
| 288 | int FallBlock(int x, int y, int number); |
| 289 | //Makes all Garbage fall one spot |
| 290 | void GarbageFall(); |
| 291 | //Makes the blocks fall (it doesn't test time, this must be done before hand) |
| 292 | void FallDown(); |
| 293 | //Pushes a single pixel, so it appears to scrool |
| 294 | void PushPixels(); |
| 295 | //See how high the tower is, saved in integer TowerHeight |
| 296 | void FindTowerHeight(); |
| 297 | //Generates a new line and moves the field one block up (restart puzzle mode) |
| 298 | void PushLine(); |
| 299 | //prints "Game Over" and ends game |
| 300 | void SetGameOver(); |
| 301 | //Moves the cursor, receaves N,S,E or W as a char an moves as desired |
| 302 | void MoveCursor(char way); |
| 303 | //switches the two blocks at the cursor position, unless game over |
| 304 | void SwitchAtCursor(); |
| 305 | //Creates garbage using a given wide and height |
| 306 | bool CreateGarbage(int wide, int height); |
| 307 | //Creates garbage using a given wide and height |
| 308 | bool CreateGreyGarbage(); |
| 309 | void MouseDown(int x, int y); //Send then the mouse is pressed |
| 310 | void MouseMove(int x); //Send then the mouse moves |
| 311 | void MouseUp(); //Send then the mouse goes up |
| 312 | void MoveCursorTo(int x, int y); |
| 313 | void FinalizeBlockGameInfo(); |
| 314 | /////////////////////////////////////////////////////////////////////////// |
| 315 | /////////////////////////// AI starts here! /////////////////////////////// |
| 316 | /////////////////////////////////////////////////////////////////////////// |
| 317 | //First the helpet functions: |
| 318 | int nrOfType(int line, int type); |
| 319 | int AIcolorToClear = 0; |
| 320 | //See if a combo can be made in this line |
| 321 | int horiInLine(int line); |
| 322 | bool horiClearPossible(); |
| 323 | //the Line Has Unmoveable Objects witch might stall the AI |
| 324 | bool lineHasGarbage(int line); |
| 325 | //Types 0..6 in line |
| 326 | int nrOfRealTypes(int line); |
| 327 | //See if there is a tower |
| 328 | bool ThereIsATower(); |
| 329 | double firstInLine1(int line); |
| 330 | //returns the first coordinate of the block of type |
| 331 | double firstInLine(int line, int type); |
| 332 | //There in the line shall we move |
| 333 | int closestTo(int line, int place); |
| 334 | //The AI will remove a tower |
| 335 | void AI_ClearTower(); |
| 336 | //The AI will try to clear block horisontally |
| 337 | void AI_ClearHori(); |
| 338 | //Test if vertical clear is possible |
| 339 | bool veriClearPossible(); |
| 340 | //There in the line shall we move |
| 341 | int closestTo(int line, int type, int place); |
| 342 | //The AI will try to clear blocks vertically |
| 343 | void AI_ClearVertical(); |
| 344 | bool firstLineCreated = 0; |
| 345 | void AI_Move(); |
| 346 | ////////////////////////////////////////////////////////////////// |
| 347 | ///////////////////////////// AI ends here! ////////////////////// |
| 348 | ////////////////////////////////////////////////////////////////// |
| 349 | //Set the move speed of the AI based on the aiLevel parameter |
| 350 | void setAIlevel(int aiLevel); |
| 351 | void PushLineInternal(); |
| 352 | //Prints "winner" and ends game |
| 353 | void setPlayerWon(); |
| 354 | //Prints "draw" and ends the game |
| 355 | void setDraw(); |
| 356 | //Updates evrything, if not called nothing happends |
| 357 | void Update(); |
| 358 | void UpdateInternal(unsigned int newtick); |
| 359 | }; |
| 360 | |
| 361 | //Play the next level |
| 362 | void nextLevel(BlockGame& g, unsigned int ticks); |
| 363 | //Replay the current level |
| 364 | void retryLevel(BlockGame& g, unsigned int ticks); |
| 365 | |
| 366 | #endif |