1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */ 2 3 #ifndef _GAME_H 4 #define _GAME_H 5 6 #include <string> 7 #include <map> 8 9 #include "GameController.h" 10 #include "Game/UI/KeySet.h" 11 #include "System/creg/creg_cond.h" 12 #include "System/Misc/SpringTime.h" 13 14 class IWater; 15 class CConsoleHistory; 16 class CInfoConsole; 17 class LuaParser; 18 class LuaInputReceiver; 19 class ILoadSaveHandler; 20 class Action; 21 class ISyncedActionExecutor; 22 class IUnsyncedActionExecutor; 23 class ChatMessage; 24 class SkirmishAIData; 25 class CWorldDrawer; 26 27 28 class CGame : public CGameController 29 { 30 private: 31 CR_DECLARE_STRUCT(CGame) 32 33 public: 34 CGame(const std::string& mapName, const std::string& modName, ILoadSaveHandler* saveFile); 35 virtual ~CGame(); 36 37 public: 38 enum GameDrawMode { 39 gameNotDrawing = 0, 40 gameNormalDraw = 1, 41 gameShadowDraw = 2, 42 gameReflectionDraw = 3, 43 gameRefractionDraw = 4 44 }; 45 46 struct PlayerTrafficInfo { PlayerTrafficInfoPlayerTrafficInfo47 PlayerTrafficInfo() : total(0) {} 48 int total; 49 std::map<int, int> packets; 50 }; 51 52 public: 53 void LoadGame(const std::string& mapName, bool threaded); 54 55 /// show GameEnd-window, calculate mouse movement etc. 56 void GameEnd(const std::vector<unsigned char>& winningAllyTeams, bool timeout = false); 57 58 private: 59 void LoadMap(const std::string& mapName); 60 void LoadDefs(); 61 void PreLoadSimulation(); 62 void PostLoadSimulation(); 63 void PreLoadRendering(); 64 void PostLoadRendering(); 65 void LoadInterface(); 66 void LoadLua(); 67 void InitSkirmishAIs(); 68 void LoadFinalize(); 69 void PostLoad(); 70 71 public: IsFinishedLoading()72 volatile bool IsFinishedLoading() const { return finishedLoading; } IsGameOver()73 bool IsGameOver() const { return gameOver; } 74 bool IsLagging(float maxLatency = 500.0f) const; 75 GetPlayerTraffic()76 const std::map<int, PlayerTrafficInfo>& GetPlayerTraffic() const { 77 return playerTraffic; 78 } 79 void AddTraffic(int playerID, int packetCode, int length); 80 81 /// Send a message to other players (allows prefixed messages with e.g. "a:...") 82 void SendNetChat(std::string message, int destination = -1); 83 84 bool ProcessCommandText(unsigned int key, const std::string& command); 85 bool ProcessKeyPressAction(unsigned int key, const Action& action); 86 bool ProcessAction(const Action& action, unsigned int key = -1, bool isRepeat = false); 87 88 void ReloadCOB(const std::string& msg, int player); 89 void ReloadCEGs(const std::string& tag); 90 91 void StartSkip(int toFrame); 92 void EndSkip(); 93 94 void ParseInputTextGeometry(const std::string& geo); 95 96 void ReloadGame(); 97 void SaveGame(const std::string& filename, bool overwrite); 98 99 void ResizeEvent(); 100 void SetupRenderingParams(); 101 SetDrawMode(GameDrawMode mode)102 void SetDrawMode(GameDrawMode mode) { gameDrawMode = mode; } GetDrawMode()103 GameDrawMode GetDrawMode() const { return gameDrawMode; } 104 105 private: 106 bool Draw(); 107 bool DrawMT(); 108 DrawMTcb(void * c)109 static void DrawMTcb(void* c) { static_cast<CGame*>(c)->DrawMT(); } 110 bool UpdateUnsynced(const spring_time currentTime); 111 112 void DrawSkip(bool blackscreen = true); 113 void DrawInputText(); 114 void UpdateCam(); 115 116 /// Format and display a chat message received over network 117 void HandleChatMsg(const ChatMessage& msg); 118 119 /// Called when a key is released by the user 120 int KeyReleased(int k); 121 /// Called when the key is pressed by the user (can be called several times due to key repeat) 122 int KeyPressed(int k, bool isRepeat); 123 124 bool ActionPressed(unsigned int key, const Action& action, bool isRepeat); 125 bool ActionReleased(const Action& action); 126 /// synced actions (received from server) go in here 127 void ActionReceived(const Action& action, int playerID); 128 129 void ReColorTeams(); 130 131 unsigned int GetNumQueuedSimFrameMessages(unsigned int maxFrames) const; 132 float GetNetMessageProcessingTimeLimit() const; 133 134 void SendClientProcUsage(); 135 void ClientReadNet(); 136 void UpdateNumQueuedSimFrames(); 137 void UpdateNetMessageProcessingTimeLeft(); 138 void SimFrame(); 139 void StartPlaying(); 140 bool Update(); 141 142 public: 143 GameDrawMode gameDrawMode; 144 145 unsigned char gameID[16]; 146 147 int lastSimFrame; 148 int lastNumQueuedSimFrames; 149 150 // number of Draw() calls per 1000ms 151 unsigned int numDrawFrames; 152 153 spring_time frameStartTime; 154 spring_time lastSimFrameTime; 155 spring_time lastDrawFrameTime; 156 spring_time lastFrameTime; 157 spring_time lastReadNetTime; ///< time of previous ClientReadNet() call 158 spring_time lastNetPacketProcessTime; 159 spring_time lastReceivedNetPacketTime; 160 spring_time lastSimFrameNetPacketTime; 161 162 float updateDeltaSeconds; 163 /// Time in seconds, stops at game end 164 float totalGameTime; 165 166 int chatSound; 167 168 bool windowedEdgeMove; 169 bool fullscreenEdgeMove; 170 171 bool hideInterface; 172 bool showFPS; 173 bool showClock; 174 bool showSpeed; 175 int showMTInfo; 176 177 float inputTextPosX; 178 float inputTextPosY; 179 float inputTextSizeX; 180 float inputTextSizeY; 181 182 bool skipping; 183 bool playing; 184 bool chatting; 185 186 /// Prevents spectator msgs from being seen by players 187 bool noSpectatorChat; 188 189 CTimedKeyChain curKeyChain; 190 std::string userInputPrefix; 191 192 /// <playerID, <packetCode, total bytes> > 193 std::map<int, PlayerTrafficInfo> playerTraffic; 194 195 // to smooth out SimFrame calls 196 float msgProcTimeLeft; ///< How many SimFrame() calls we still may do. 197 float consumeSpeedMult; ///< How fast we should eat NETMSG_NEWFRAMEs. 198 199 int skipStartFrame; 200 int skipEndFrame; 201 int skipTotalFrames; 202 float skipSeconds; 203 bool skipSoundmute; 204 float skipOldSpeed; 205 float skipOldUserSpeed; 206 spring_time skipLastDrawTime; 207 208 /** 209 * @see CGameServer#speedControl 210 */ 211 int speedControl; 212 213 CInfoConsole* infoConsole; 214 CConsoleHistory* consoleHistory; 215 216 private: 217 CWorldDrawer* worldDrawer; 218 219 LuaParser* defsParser; 220 221 /// for reloading the savefile 222 ILoadSaveHandler* saveFile; 223 224 volatile bool finishedLoading; 225 bool gameOver; 226 }; 227 228 229 extern CGame* game; 230 231 232 #endif // _GAME_H 233