| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file game.h |
| 5 | * |
| 6 | * Part of the OpenJazz project |
| 7 | * |
| 8 | * @par History: |
| 9 | * - 23rd August 2005: Created OpenJazz.h |
| 10 | * - 2nd March 2009: Created network.h from parts of OpenJazz.h |
| 11 | * - 9th February 2009: Renamed network.h to game.h |
| 12 | * - 2nd August 2009: Created gamemode.h from parts of game.h |
| 13 | * |
| 14 | * @par Licence: |
| 15 | * Copyright (c) 2005-2017 AJ Thomson |
| 16 | * |
| 17 | * OpenJazz is distributed under the terms of |
| 18 | * the GNU General Public License, version 2.0 |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | |
| 23 | #ifndef _GAME_H |
| 24 | #define _GAME_H |
| 25 | |
| 26 | |
| 27 | #include "gamemode.h" |
| 28 | |
| 29 | #include "io/network.h" |
| 30 | #include "level/level.h" |
| 31 | |
| 32 | |
| 33 | // Constants |
| 34 | |
| 35 | // Time intervals |
| 36 | #define T_SSEND 20 |
| 37 | #define T_SCHECK 1000 |
| 38 | #define T_CSEND 10 |
| 39 | #define T_CCHECK 1000 |
| 40 | |
| 41 | // Message categories and types |
| 42 | #define MCMASK 0xF0 |
| 43 | #define MC_GAME 0x00 |
| 44 | #define MC_LEVEL 0x10 |
| 45 | #define MC_PLAYER 0x20 |
| 46 | |
| 47 | #define MT_G_PROPS 0x00 /* Game properties */ |
| 48 | #define MT_G_PJOIN 0x01 /* New player joined */ |
| 49 | #define MT_G_PQUIT 0x02 /* Player left */ |
| 50 | #define MT_G_LEVEL 0x03 /* Level data */ |
| 51 | #define MT_G_CHECK 0x04 |
| 52 | #define MT_G_SCORE 0x05 /* Team scored a roast/lap/etc. */ |
| 53 | #define MT_G_LTYPE 0x06 /* Level type */ |
| 54 | |
| 55 | #define MT_L_PROP 0x10 /* Level property */ |
| 56 | #define MT_L_GRID 0x11 /* Change to gridElement */ |
| 57 | #define MT_L_STAGE 0x12 /* Change in level stage */ |
| 58 | |
| 59 | #define MT_P_ANIMS 0x20 /* Player animations */ |
| 60 | #define MT_P_TEMP 0x21 /* Temporary player properties, e.g. position */ |
| 61 | |
| 62 | // Minimum message lengths, including header |
| 63 | #define MTL_G_PROPS 8 |
| 64 | #define MTL_G_PJOIN 10 |
| 65 | #define MTL_G_PQUIT 3 |
| 66 | #define MTL_G_LEVEL 4 /* + amount of level data */ |
| 67 | #define MTL_G_CHECK 6 |
| 68 | #define MTL_G_SCORE 3 |
| 69 | #define MTL_G_LTYPE 3 |
| 70 | |
| 71 | #define MTL_L_PROP 5 |
| 72 | #define MTL_L_GRID 8 |
| 73 | #define MTL_L_STAGE 3 |
| 74 | |
| 75 | #define MTL_P_ANIMS 3 /* + PANIMS, BPANIMS, or 1 (for JJ2) */ |
| 76 | #define MTL_P_TEMP 46 |
| 77 | |
| 78 | #define BUFFER_LENGTH 255 /* Should always be big enough to hold any message */ |
| 79 | |
| 80 | |
| 81 | // Classes |
| 82 | |
| 83 | class Anim; |
| 84 | class File; |
| 85 | |
| 86 | /// Base class for game handling classes |
| 87 | class Game { |
| 88 | |
| 89 | private: |
| 90 | int planetId; ///< ID of last planet approach sequence |
| 91 | |
| 92 | bool isFileType (const char *fileName, const char *type, int typeLength); |
| 93 | |
| 94 | protected: |
| 95 | GameMode* mode; ///< Mode-specific management |
| 96 | Level* baseLevel; ///< Current level |
| 97 | char* levelFile; ///< Current level's file name |
| 98 | LevelType levelType; ///< Current level's type |
| 99 | difficultyType difficulty; ///< Difficulty setting (easy, medium, hard, turbo (hard in JJ2 levels)) |
| 100 | unsigned int sendTime; ///< The next time data will be sent |
| 101 | unsigned int checkTime; ///< The next time a connection/disconnection will be dealt with |
| 102 | short int checkX; ///< X-coordinate of the level checkpoint |
| 103 | short int checkY; ///< Y-coordinate of the level checkpoint |
| 104 | |
| 105 | Game (); |
| 106 | |
| 107 | GameMode* createMode (GameModeType modeType); |
| 108 | |
| 109 | LevelType getLevelType (const char* fileName); |
| 110 | int playLevel (char *fileName, bool intro, bool checkpoint); |
| 111 | |
| 112 | void addLevelPlayer (Player *player); |
| 113 | |
| 114 | public: |
| 115 | virtual ~Game (); |
| 116 | |
| 117 | GameMode* getMode (); |
| 118 | difficultyType getDifficulty (); |
| 119 | void setDifficulty (difficultyType diff); |
| 120 | int playLevel (char *fileName); |
| 121 | virtual int setLevel (char *fileName) = 0; |
| 122 | int play (); |
| 123 | void view (int change); |
| 124 | virtual void send (unsigned char *buffer) = 0; |
| 125 | virtual int step (unsigned int ticks) = 0; |
| 126 | virtual void score (unsigned char team) = 0; |
| 127 | virtual void setCheckpoint (int gridX, int gridY) = 0; |
| 128 | void resetPlayer (Player *player); |
| 129 | |
| 130 | }; |
| 131 | |
| 132 | |
| 133 | /// Game handling for single-player local play |
| 134 | class LocalGame : public Game { |
| 135 | |
| 136 | public: |
| 137 | LocalGame (const char *firstLevel, difficultyType gameDifficulty); |
| 138 | ~LocalGame () override; |
| 139 | |
| 140 | int setLevel (char *fileName) override; |
| 141 | void send (unsigned char *buffer) override; |
| 142 | int step (unsigned int ticks) override; |
| 143 | void score (unsigned char team) override; |
| 144 | void setCheckpoint (int gridX, int gridY) override; |
| 145 | |
| 146 | }; |
| 147 | |
| 148 | |
| 149 | /// Game handling for multiplayer servers |
| 150 | class ServerGame : public Game { |
| 151 | |
| 152 | private: |
| 153 | int clientStatus[MAX_CLIENTS]; /**< Array of client statuses |
| 154 | -2: Connected and operational |
| 155 | -1: Not connected |
| 156 | >=0: Number of bytes of the level that have been sent */ |
| 157 | int clientPlayer[MAX_CLIENTS]; ///< Array of client player indexes |
| 158 | int clientSock[MAX_CLIENTS]; ///< Array of client sockets |
| 159 | unsigned char recvBuffers[MAX_CLIENTS][BUFFER_LENGTH]; ///< Array of buffers containing data received from clients |
| 160 | int received[MAX_CLIENTS]; ///< Array containing the amount of data received from each client |
| 161 | unsigned char *levelData; ///< Contents of the current level file |
| 162 | int levelSize; ///< Size of the current level file |
| 163 | int sock; ///< Server socket |
| 164 | |
| 165 | public: |
| 166 | ServerGame (GameModeType mode, char *firstLevel, difficultyType gameDifficulty); |
| 167 | ~ServerGame () override; |
| 168 | |
| 169 | int setLevel (char *fileName) override; |
| 170 | void send (unsigned char *buffer) override; |
| 171 | int step (unsigned int ticks) override; |
| 172 | void score (unsigned char team) override; |
| 173 | void setCheckpoint (int gridX, int gridY) override; |
| 174 | |
| 175 | }; |
| 176 | |
| 177 | |
| 178 | /// Game handling for multiplayer clients |
| 179 | class ClientGame : public Game { |
| 180 | |
| 181 | private: |
| 182 | File *file; ///< File to which the incoming level will be written |
| 183 | unsigned char recvBuffer[BUFFER_LENGTH]; ///< Buffer containing data received from server |
| 184 | int received; ///< Amount of data received from server |
| 185 | int clientID; ///< Client's index on the server |
| 186 | int maxPlayers; ///< The maximum number of players in the game |
| 187 | int sock; ///< Client socket |
| 188 | |
| 189 | public: |
| 190 | explicit ClientGame(char *address); |
| 191 | ~ClientGame() override; |
| 192 | |
| 193 | int setLevel (char *fileName) override; |
| 194 | void send (unsigned char *buffer) override; |
| 195 | int step (unsigned int ticks) override; |
| 196 | void score (unsigned char team) override; |
| 197 | void setCheckpoint (int gridX, int gridY) override; |
| 198 | |
| 199 | }; |
| 200 | |
| 201 | #endif |
| 202 |