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