From a9026f8c206f49aabc1b00d62acec158f5963134 Mon Sep 17 00:00:00 2001 From: James McCulloch Date: Sat, 27 Mar 2010 07:09:34 +0000 Subject: [PATCH] * added missing #include "leak_dumper.h" to new cpp files * fix for map names not being formatted (and replaced explicit loops to do the same to tileSet & techTree) * fix exploration state for fog-of-war off & multiplayer (note: also turns off for AI, diff. behaviour to before) * added fogOfWar to GameSettings and added to cutom game menu/menu_state_custom_game.cpp --- source/glest_game/game/game.cpp | 5 - source/glest_game/game/game_settings.h | 98 +++++++++++ .../menu/menu_state_custom_game.cpp | 91 ++++++---- .../glest_game/menu/menu_state_custom_game.h | 81 +++++++++ .../glest_game/network/client_interface.cpp | 16 +- source/glest_game/network/client_interface.h | 85 ++++++++++ source/glest_game/network/connection_slot.cpp | 2 - source/glest_game/network/network_interface.h | 1 - source/glest_game/network/network_manager.cpp | 79 +++++++++ source/glest_game/network/network_message.cpp | 8 +- source/glest_game/network/network_message.h | 9 +- .../glest_game/network/server_interface.cpp | 5 - source/glest_game/network/server_interface.h | 85 ++++++++++ source/glest_game/world/minimap.cpp | 157 ++++++++++++++++++ source/glest_game/world/minimap.h | 69 ++++++++ source/glest_game/world/world.cpp | 34 ++-- source/glest_game/world/world.h | 4 +- source/shared_lib/include/util/leak_dumper.h | 121 ++++++++++++++ .../shared_lib/sources/graphics/BMPReader.cpp | 2 + .../sources/graphics/ImageReaders.cpp | 1 + .../shared_lib/sources/graphics/JPGReader.cpp | 2 + .../shared_lib/sources/graphics/PNGReader.cpp | 2 + .../shared_lib/sources/graphics/TGAReader.cpp | 4 +- source/shared_lib/sources/graphics/pixmap.cpp | 5 +- .../shared_lib/sources/util/leak_dumper.cpp | 25 +-- source/shared_lib/sources/util/profiler.cpp | 1 + 26 files changed, 869 insertions(+), 123 deletions(-) create mode 100644 source/glest_game/game/game_settings.h create mode 100644 source/glest_game/menu/menu_state_custom_game.h create mode 100644 source/glest_game/network/client_interface.h create mode 100644 source/glest_game/network/network_manager.cpp create mode 100644 source/glest_game/network/server_interface.h create mode 100644 source/glest_game/world/minimap.cpp create mode 100644 source/glest_game/world/minimap.h create mode 100644 source/shared_lib/include/util/leak_dumper.h diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index d7c5fc6c..8b2fcb2e 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -146,11 +146,6 @@ void Game::init() mainMessageBox.init(lang.get("Yes"), lang.get("No")); mainMessageBox.setEnabled(false); - //check fog of war - if(!Config::getInstance().getBool("FogOfWar") && networkManager.isNetworkGame() ){ - throw runtime_error("Can not play online games with for of war disabled"); - } - //init world, and place camera commander.init(&world); world.init(this, gameSettings.getDefaultUnits()); diff --git a/source/glest_game/game/game_settings.h b/source/glest_game/game/game_settings.h new file mode 100644 index 00000000..84f2c03f --- /dev/null +++ b/source/glest_game/game/game_settings.h @@ -0,0 +1,98 @@ +// ============================================================== +// This file is part of Glest (www.glest.org) +// +// Copyright (C) 2001-2008 Martiņo Figueroa +// +// You can redistribute this code and/or modify it under +// the terms of the GNU General Public License as published +// by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version +// ============================================================== + +#ifndef _GLEST_GAME_GAMESETTINGS_H_ +#define _GLEST_GAME_GAMESETTINGS_H_ + +#include "game_constants.h" + +namespace Glest{ namespace Game{ + +// ===================================================== +// class GameSettings +// ===================================================== + +class GameSettings{ +private: + string description; + string map; + string tileset; + string tech; + string scenario; + string scenarioDir; + string factionTypeNames[GameConstants::maxPlayers]; //faction names + + ControlType factionControls[GameConstants::maxPlayers]; + + int thisFactionIndex; + int factionCount; + int teams[GameConstants::maxPlayers]; + int startLocationIndex[GameConstants::maxPlayers]; + + bool defaultUnits; + bool defaultResources; + bool defaultVictoryConditions; + + bool fogOfWar; + +public: + + + GameSettings() { } + + // default copy constructor will do fine, and will maintain itself ;) + + //get + const string &getDescription() const {return description;} + const string &getMap() const {return map;} + const string &getTileset() const {return tileset;} + const string &getTech() const {return tech;} + const string &getScenario() const {return scenario;} + const string &getScenarioDir() const {return scenarioDir;} + const string &getFactionTypeName(int factionIndex) const {return factionTypeNames[factionIndex];} + ControlType getFactionControl(int factionIndex) const {return factionControls[factionIndex];} + + int getThisFactionIndex() const {return thisFactionIndex;} + int getFactionCount() const {return factionCount;} + int getTeam(int factionIndex) const {return teams[factionIndex];} + int getStartLocationIndex(int factionIndex) const {return startLocationIndex[factionIndex];} + + bool getDefaultUnits() const {return defaultUnits;} + bool getDefaultResources() const {return defaultResources;} + bool getDefaultVictoryConditions() const {return defaultVictoryConditions;} + + bool getFogOfWar() const {return fogOfWar;} + + //set + void setDescription(const string& description) {this->description= description;} + void setMap(const string& map) {this->map= map;} + void setTileset(const string& tileset) {this->tileset= tileset;} + void setTech(const string& tech) {this->tech= tech;} + void setScenario(const string& scenario) {this->scenario= scenario;} + void setScenarioDir(const string& scenarioDir) {this->scenarioDir= scenarioDir;} + + void setFactionTypeName(int factionIndex, const string& factionTypeName) {this->factionTypeNames[factionIndex]= factionTypeName;} + void setFactionControl(int factionIndex, ControlType controller) {this->factionControls[factionIndex]= controller;} + void setThisFactionIndex(int thisFactionIndex) {this->thisFactionIndex= thisFactionIndex;} + void setFactionCount(int factionCount) {this->factionCount= factionCount;} + void setTeam(int factionIndex, int team) {this->teams[factionIndex]= team;} + void setStartLocationIndex(int factionIndex, int startLocationIndex) {this->startLocationIndex[factionIndex]= startLocationIndex;} + + void setDefaultUnits(bool defaultUnits) {this->defaultUnits= defaultUnits;} + void setDefaultResources(bool defaultResources) {this->defaultResources= defaultResources;} + void setDefaultVictoryConditions(bool defaultVictoryConditions) {this->defaultVictoryConditions= defaultVictoryConditions;} + + void setFogOfWar(bool fogOfWar) {this->fogOfWar = fogOfWar;} +}; + +}}//end namespace + +#endif diff --git a/source/glest_game/menu/menu_state_custom_game.cpp b/source/glest_game/menu/menu_state_custom_game.cpp index 140ca978..fffc022d 100644 --- a/source/glest_game/menu/menu_state_custom_game.cpp +++ b/source/glest_game/menu/menu_state_custom_game.cpp @@ -31,6 +31,12 @@ namespace Glest{ namespace Game{ using namespace Shared::Util; +struct FormatString { + void operator()(string &s) { + s = formatString(s); + } +}; + // ===================================================== // class MenuStateCustomGame // ===================================================== @@ -40,67 +46,69 @@ MenuStateCustomGame::MenuStateCustomGame(Program *program, MainMenu *mainMenu, b { Lang &lang= Lang::getInstance(); NetworkManager &networkManager= NetworkManager::getInstance(); + Config &config = Config::getInstance(); needToSetChangedGameSettings = false; lastSetChangedGameSettings = time(NULL);; - vector glestMaps, megaMaps, teamItems, controlItems; + vector teamItems, controlItems, results; //create buttonReturn.init(350, 180, 125); buttonPlayNow.init(525, 180, 125); //map listBox - Config &config = Config::getInstance(); - findAll(config.getPathListForType(ptMaps), "*.gbm", glestMaps, true, false); - findAll(config.getPathListForType(ptMaps), "*.mgm", megaMaps, true, false); - // put them all in a set, to weed out duplicates (gbm & mgm with same name) // will also ensure they are alphabetically listed (rather than how the OS provides them) set allMaps; - if (!glestMaps.empty()) { - copy(glestMaps.begin(), glestMaps.end(), std::inserter(allMaps, allMaps.begin())); - } - if (!megaMaps.empty()) { - copy(megaMaps.begin(), megaMaps.end(), std::inserter(allMaps, allMaps.begin())); - } - if(allMaps.size()==0){ - throw runtime_error("There are no maps"); - } - std::copy(allMaps.begin(), allMaps.end(), std::back_inserter(mapFiles)); - - listBoxMap.init(200, 260, 150); - listBoxMap.setItems(mapFiles); - labelMap.init(200, 290); - labelMapInfo.init(200, 230, 200, 40); + findAll(config.getPathListForType(ptMaps), "*.gbm", results, true, false); + copy(results.begin(), results.end(), std::inserter(allMaps, allMaps.begin())); + results.clear(); + findAll(config.getPathListForType(ptMaps), "*.mgm", results, true, false); + copy(results.begin(), results.end(), std::inserter(allMaps, allMaps.begin())); + results.clear(); - vector results; + if (allMaps.empty()) { + throw runtime_error("No maps were found!"); + } + copy(allMaps.begin(), allMaps.end(), std::back_inserter(results)); + mapFiles = results; + std::for_each(results.begin(), results.end(), FormatString()); + + listBoxMap.init(100, 260, 200); + listBoxMap.setItems(results); + labelMap.init(100, 290); + labelMapInfo.init(100, 230, 200, 40); + + // fog - o - war + // @350 ? 300 ? + labelFogOfWar.init(350, 290, 100); + listBoxFogOfWar.init(350, 260, 100); + listBoxFogOfWar.pushBackItem(lang.get("On")); + listBoxFogOfWar.pushBackItem(lang.get("Off")); + listBoxFogOfWar.setSelectedItemIndex(0); //tileset listBox findDirs(config.getPathListForType(ptTilesets), results); - if(results.size() == 0) { - throw runtime_error("There is no tile set"); + if (results.empty()) { + throw runtime_error("No tile-sets were found!"); } tilesetFiles= results; - for(int i= 0; isetDefaultUnits(true); gameSettings->setDefaultResources(true); gameSettings->setDefaultVictoryConditions(true); + gameSettings->setFogOfWar(listBoxFogOfWar.getSelectedItemIndex() == 0); for(int i=0; i mapFiles; + vector techTreeFiles; + vector tilesetFiles; + vector factionFiles; + GraphicLabel labelPlayers[GameConstants::maxPlayers]; + GraphicListBox listBoxControls[GameConstants::maxPlayers]; + GraphicListBox listBoxFactions[GameConstants::maxPlayers]; + GraphicListBox listBoxTeams[GameConstants::maxPlayers]; + GraphicLabel labelNetStatus[GameConstants::maxPlayers]; + MapInfo mapInfo; + + bool needToSetChangedGameSettings; + time_t lastSetChangedGameSettings; + + Console console; + ChatManager chatManager; + +public: + MenuStateCustomGame(Program *program, MainMenu *mainMenu, bool openNetworkSlots= false); + + void mouseClick(int x, int y, MouseButton mouseButton); + void mouseMove(int x, int y, const MouseState *mouseState); + void render(); + void update(); + + virtual void keyDown(char key); + virtual void keyPress(char c); + +private: + + bool hasNetworkGameSettings(); + void loadGameSettings(GameSettings *gameSettings); + void loadMapInfo(string file, MapInfo *mapInfo); + void reloadFactions(); + void updateControlers(); + void closeUnusedSlots(); + void updateNetworkSlots(); +}; + +}}//end namespace + +#endif diff --git a/source/glest_game/network/client_interface.cpp b/source/glest_game/network/client_interface.cpp index 9cc61edb..863a4ba4 100755 --- a/source/glest_game/network/client_interface.cpp +++ b/source/glest_game/network/client_interface.cpp @@ -218,17 +218,7 @@ void ClientInterface::updateLobby() SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] mapCRC mismatch, local = %d, remote = %d\n", __FILE__,__FUNCTION__,mapCRC,networkMessageSynchNetworkGameData.getMapCRC()); } - - - this->setNetworkGameDataSynchCheckOkFogOfWar((getFogOfWar() == networkMessageSynchNetworkGameData.getFogOfWar())); - - if(this->getNetworkGameDataSynchCheckOkFogOfWar() == false) - { - SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] getFogOfWar mismatch, local = %d, remote = %d\n", - __FILE__,__FUNCTION__,getFogOfWar(),networkMessageSynchNetworkGameData.getFogOfWar()); - } - - NetworkMessageSynchNetworkGameDataStatus sendNetworkMessageSynchNetworkGameDataStatus(mapCRC,tilesetCRC,techCRC,getFogOfWar()); + NetworkMessageSynchNetworkGameDataStatus sendNetworkMessageSynchNetworkGameDataStatus(mapCRC,tilesetCRC,techCRC); sendMessage(&sendNetworkMessageSynchNetworkGameDataStatus); } } @@ -569,10 +559,10 @@ void ClientInterface::close() delete clientSocket; clientSocket= NULL; } - +/* bool ClientInterface::getFogOfWar() { return Config::getInstance().getBool("FogOfWar"); } - +*/ }}//end namespace diff --git a/source/glest_game/network/client_interface.h b/source/glest_game/network/client_interface.h new file mode 100644 index 00000000..f42abbf0 --- /dev/null +++ b/source/glest_game/network/client_interface.h @@ -0,0 +1,85 @@ +// ============================================================== +// This file is part of Glest (www.glest.org) +// +// Copyright (C) 2001-2008 Martiņo Figueroa +// +// You can redistribute this code and/or modify it under +// the terms of the GNU General Public License as published +// by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version +// ============================================================== + +#ifndef _GLEST_GAME_CLIENTINTERFACE_H_ +#define _GLEST_GAME_CLIENTINTERFACE_H_ + +#include + +#include "network_interface.h" +//#include "game_settings.h" + +#include "socket.h" + +using Shared::Platform::Ip; +using Shared::Platform::ClientSocket; +using std::vector; + +namespace Glest{ namespace Game{ + +// ===================================================== +// class ClientInterface +// ===================================================== + +class ClientInterface: public GameNetworkInterface{ +private: + static const int messageWaitTimeout; + static const int waitSleepTime; + +private: + ClientSocket *clientSocket; + //GameSettings gameSettings; + string serverName; + bool introDone; + bool launchGame; + int playerIndex; + + Ip ip; + int port; + +public: + ClientInterface(); + virtual ~ClientInterface(); + + virtual Socket* getSocket() {return clientSocket;} + virtual const Socket* getSocket() const {return clientSocket;} + virtual void close(); + + //message processing + virtual void update(); + virtual void updateLobby(); + virtual void updateKeyframe(int frameCount); + virtual void waitUntilReady(Checksum* checksum); + + // message sending + virtual void sendTextMessage(const string &text, int teamIndex); + virtual void quitGame(bool userManuallyQuit); + + //misc + virtual string getNetworkStatus() const; + + //accessors + string getServerName() const {return serverName;} + bool getLaunchGame() const {return launchGame;} + bool getIntroDone() const {return introDone;} + int getPlayerIndex() const {return playerIndex;} + //const GameSettings *getGameSettings() {return &gameSettings;} + + void connect(const Ip &ip, int port); + void reset(); + +private: + void waitForMessage(); +}; + +}}//end namespace + +#endif diff --git a/source/glest_game/network/connection_slot.cpp b/source/glest_game/network/connection_slot.cpp index 551753d0..300e02a1 100644 --- a/source/glest_game/network/connection_slot.cpp +++ b/source/glest_game/network/connection_slot.cpp @@ -186,7 +186,6 @@ void ConnectionSlot::update(bool checkForNewClients) networkGameDataSynchCheckOkMap = (networkMessageSynchNetworkGameDataStatus.getMapCRC() == mapCRC); networkGameDataSynchCheckOkTile = (networkMessageSynchNetworkGameDataStatus.getTilesetCRC() == tilesetCRC); networkGameDataSynchCheckOkTech = (networkMessageSynchNetworkGameDataStatus.getTechCRC() == techCRC); - networkGameDataSynchCheckOkFogOfWar = (networkMessageSynchNetworkGameDataStatus.getFogOfWar() == serverInterface->getFogOfWar() == true); // For testing //techCRC++; @@ -203,7 +202,6 @@ void ConnectionSlot::update(bool checkForNewClients) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] mapCRC = %d, remote = %d\n",__FILE__,__FUNCTION__,mapCRC,networkMessageSynchNetworkGameDataStatus.getMapCRC()); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] tilesetCRC = %d, remote = %d\n",__FILE__,__FUNCTION__,tilesetCRC,networkMessageSynchNetworkGameDataStatus.getTilesetCRC()); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] techCRC = %d, remote = %d\n",__FILE__,__FUNCTION__,techCRC,networkMessageSynchNetworkGameDataStatus.getTechCRC()); - SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] serverInterface->getFogOfWar() = %d, remote = %d\n",__FILE__,__FUNCTION__,serverInterface->getFogOfWar(),networkMessageSynchNetworkGameDataStatus.getFogOfWar()); if(allowDownloadDataSynch == true) { diff --git a/source/glest_game/network/network_interface.h b/source/glest_game/network/network_interface.h index d8d56240..17e4e41c 100644 --- a/source/glest_game/network/network_interface.h +++ b/source/glest_game/network/network_interface.h @@ -93,7 +93,6 @@ public: virtual bool getNetworkGameDataSynchCheckOkMap() { return networkGameDataSynchCheckOkMap; } virtual bool getNetworkGameDataSynchCheckOkTile() { return networkGameDataSynchCheckOkTile; } virtual bool getNetworkGameDataSynchCheckOkTech() { return networkGameDataSynchCheckOkTech; } - virtual bool getFogOfWar()=0; virtual bool getNetworkGameDataSynchCheckOkFogOfWar() { return networkGameDataSynchCheckOkFogOfWar; } virtual void setNetworkGameDataSynchCheckOkFogOfWar(bool value) { networkGameDataSynchCheckOkFogOfWar = value; } diff --git a/source/glest_game/network/network_manager.cpp b/source/glest_game/network/network_manager.cpp new file mode 100644 index 00000000..dfdff065 --- /dev/null +++ b/source/glest_game/network/network_manager.cpp @@ -0,0 +1,79 @@ +// ============================================================== +// This file is part of Glest (www.glest.org) +// +// Copyright (C) 2001-2008 Martiņo Figueroa +// +// You can redistribute this code and/or modify it under +// the terms of the GNU General Public License as published +// by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version +// ============================================================== + +#include "network_manager.h" +#include "leak_dumper.h" + +namespace Glest{ namespace Game{ + +// ===================================================== +// class NetworkManager +// ===================================================== + +NetworkManager &NetworkManager::getInstance(){ + static NetworkManager networkManager; + return networkManager; +} + +NetworkManager::NetworkManager(){ + gameNetworkInterface= NULL; + networkRole= nrIdle; +} + +void NetworkManager::init(NetworkRole networkRole) +{ + assert(gameNetworkInterface==NULL); + + this->networkRole = networkRole; + + if(networkRole==nrServer){ + gameNetworkInterface = new ServerInterface(); + } + else + { + gameNetworkInterface = new ClientInterface(); + } +} + +void NetworkManager::end(){ + delete gameNetworkInterface; + gameNetworkInterface= NULL; + networkRole= nrIdle; +} + +void NetworkManager::update(){ + if(gameNetworkInterface!=NULL){ + gameNetworkInterface->update(); + } +} + +bool NetworkManager::isNetworkGame(){ + return networkRole==nrClient || getServerInterface()->getConnectedSlotCount()>0; +} + +GameNetworkInterface* NetworkManager::getGameNetworkInterface(){ + assert(gameNetworkInterface!=NULL); + return gameNetworkInterface; +} + +ServerInterface* NetworkManager::getServerInterface(){ + assert(gameNetworkInterface!=NULL); + assert(networkRole==nrServer); + return static_cast(gameNetworkInterface); +} + +ClientInterface* NetworkManager::getClientInterface(){ + assert(gameNetworkInterface!=NULL); + assert(networkRole==nrClient); + return static_cast(gameNetworkInterface); +} + +}}//end namespace diff --git a/source/glest_game/network/network_message.cpp b/source/glest_game/network/network_message.cpp index 59e370c3..8c0953ca 100644 --- a/source/glest_game/network/network_message.cpp +++ b/source/glest_game/network/network_message.cpp @@ -177,6 +177,7 @@ NetworkMessageLaunch::NetworkMessageLaunch(const GameSettings *gameSettings){ data.defaultResources= gameSettings->getDefaultResources(); data.defaultUnits= gameSettings->getDefaultUnits(); data.defaultVictoryConditions= gameSettings->getDefaultVictoryConditions(); + data.fogOfWar = gameSettings->getFogOfWar(); for(int i= 0; igetFactionTypeName(i); @@ -196,6 +197,7 @@ void NetworkMessageLaunch::buildGameSettings(GameSettings *gameSettings) const{ gameSettings->setDefaultResources(data.defaultResources); gameSettings->setDefaultUnits(data.defaultUnits); gameSettings->setDefaultVictoryConditions(data.defaultVictoryConditions); + gameSettings->setFogOfWar(data.fogOfWar); for(int i= 0; isetFactionTypeName(i, data.factionTypeNames[i].getString()); @@ -344,8 +346,6 @@ NetworkMessageSynchNetworkGameData::NetworkMessageSynchNetworkGameData(const Gam checksum.addFile(file); data.mapCRC = checksum.getSum(); //SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] file = [%s] checksum = %d\n",__FILE__,__FUNCTION__,file.c_str(),data.mapCRC); - - data.hasFogOfWar = Config::getInstance().getBool("FogOfWar");; } bool NetworkMessageSynchNetworkGameData::receive(Socket* socket) @@ -364,15 +364,13 @@ void NetworkMessageSynchNetworkGameData::send(Socket* socket) const // class NetworkMessageSynchNetworkGameDataStatus // ===================================================== -NetworkMessageSynchNetworkGameDataStatus::NetworkMessageSynchNetworkGameDataStatus(int32 mapCRC, int32 tilesetCRC, int32 techCRC, int8 hasFogOfWar) +NetworkMessageSynchNetworkGameDataStatus::NetworkMessageSynchNetworkGameDataStatus(int32 mapCRC, int32 tilesetCRC, int32 techCRC) { data.messageType= nmtSynchNetworkGameDataStatus; data.tilesetCRC = tilesetCRC; data.techCRC = techCRC; data.mapCRC = mapCRC; - - data.hasFogOfWar = hasFogOfWar; } bool NetworkMessageSynchNetworkGameDataStatus::receive(Socket* socket) diff --git a/source/glest_game/network/network_message.h b/source/glest_game/network/network_message.h index 4373a646..7f275b51 100644 --- a/source/glest_game/network/network_message.h +++ b/source/glest_game/network/network_message.h @@ -149,6 +149,7 @@ private: int8 defaultResources; int8 defaultUnits; int8 defaultVictoryConditions; + int8 fogOfWar; }; private: @@ -279,7 +280,6 @@ private: int32 mapCRC; int32 tilesetCRC; int32 techCRC; - int8 hasFogOfWar; }; private: @@ -299,8 +299,6 @@ public: int32 getMapCRC() const {return data.mapCRC;} int32 getTilesetCRC() const {return data.tilesetCRC;} int32 getTechCRC() const {return data.techCRC;} - - int8 getFogOfWar() const { return data.hasFogOfWar; } }; // ===================================================== @@ -323,7 +321,6 @@ private: int32 tilesetCRC; int32 techCRC; - int8 hasFogOfWar; }; private: @@ -331,7 +328,7 @@ private: public: NetworkMessageSynchNetworkGameDataStatus() {}; - NetworkMessageSynchNetworkGameDataStatus(int32 mapCRC, int32 tilesetCRC, int32 techCRC, int8 hasFogOfWar); + NetworkMessageSynchNetworkGameDataStatus(int32 mapCRC, int32 tilesetCRC, int32 techCRC); virtual bool receive(Socket* socket); virtual void send(Socket* socket) const; @@ -339,8 +336,6 @@ public: int32 getMapCRC() const {return data.mapCRC;} int32 getTilesetCRC() const {return data.tilesetCRC;} int32 getTechCRC() const {return data.techCRC;} - - int8 getFogOfWar() const { return data.hasFogOfWar; } }; // ===================================================== diff --git a/source/glest_game/network/server_interface.cpp b/source/glest_game/network/server_interface.cpp index 5a70c5dc..bb02ec45 100644 --- a/source/glest_game/network/server_interface.cpp +++ b/source/glest_game/network/server_interface.cpp @@ -571,9 +571,4 @@ void ServerInterface::close() //serverSocket = ServerSocket(); } -bool ServerInterface::getFogOfWar() -{ - return Config::getInstance().getBool("FogOfWar"); -} - }}//end namespace diff --git a/source/glest_game/network/server_interface.h b/source/glest_game/network/server_interface.h new file mode 100644 index 00000000..3b0f16f2 --- /dev/null +++ b/source/glest_game/network/server_interface.h @@ -0,0 +1,85 @@ +// ============================================================== +// This file is part of Glest (www.glest.org) +// +// Copyright (C) 2001-2008 Martiņo Figueroa +// +// You can redistribute this code and/or modify it under +// the terms of the GNU General Public License as published +// by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version +// ============================================================== + +#ifndef _GLEST_GAME_SERVERINTERFACE_H_ +#define _GLEST_GAME_SERVERINTERFACE_H_ + +#include + +#include "game_constants.h" +#include "network_interface.h" +#include "connection_slot.h" +#include "socket.h" + +using std::vector; +using Shared::Platform::ServerSocket; + +namespace Glest{ namespace Game{ + +// ===================================================== +// class ServerInterface +// ===================================================== + +typedef struct +{ + string chatText; + string chatSender; + int chatTeamIndex; + int sourceTeamIndex; + +} TeamMessageData; + +class ServerInterface: public GameNetworkInterface{ +private: + ConnectionSlot* slots[GameConstants::maxPlayers]; + ServerSocket serverSocket; + bool gameHasBeenInitiated; + int gameSettingsUpdateCount; + +public: + ServerInterface(); + virtual ~ServerInterface(); + + virtual Socket* getSocket() {return &serverSocket;} + virtual const Socket* getSocket() const {return &serverSocket;} + virtual void close(); + + //message processing + virtual void update(); + virtual void updateLobby(){}; + virtual void updateKeyframe(int frameCount); + virtual void waitUntilReady(Checksum* checksum); + + // message sending + virtual void sendTextMessage(const string &text, int teamIndex); + virtual void quitGame(bool userManuallyQuit); + + //misc + virtual string getNetworkStatus() const; + + ServerSocket* getServerSocket() {return &serverSocket;} + void addSlot(int playerIndex); + void removeSlot(int playerIndex); + ConnectionSlot* getSlot(int playerIndex); + int getConnectedSlotCount(); + + bool launchGame(const GameSettings* gameSettings); + virtual void setGameSettings(GameSettings *serverGameSettings, bool waitForClientAck = false); + +private: + void broadcastMessage(const NetworkMessage* networkMessage, int excludeSlot= -1); + void updateListen(); + void broadcastMessageToConnectedClients(const NetworkMessage* networkMessage, int excludeSlot = -1); +}; + +}}//end namespace + +#endif diff --git a/source/glest_game/world/minimap.cpp b/source/glest_game/world/minimap.cpp new file mode 100644 index 00000000..350dd230 --- /dev/null +++ b/source/glest_game/world/minimap.cpp @@ -0,0 +1,157 @@ +// ============================================================== +// This file is part of Glest (www.glest.org) +// +// Copyright (C) 2001-2008 Martiņo Figueroa +// +// You can redistribute this code and/or modify it under +// the terms of the GNU General Public License as published +// by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version +// ============================================================== + +#include "minimap.h" + +#include + +#include "world.h" +#include "vec.h" +#include "renderer.h" +#include "config.h" +#include "object.h" +#include "leak_dumper.h" + +using namespace Shared::Graphics; + +namespace Glest{ namespace Game{ + +// ===================================================== +// class Minimap +// ===================================================== + +const float Minimap::exploredAlpha= 0.5f; + +Minimap::Minimap(){ + fowPixmap0= NULL; + fowPixmap1= NULL; + fogOfWar= true;//Config::getInstance().getBool("FogOfWar"); +} + +void Minimap::init(int w, int h, const World *world, bool fogOfWar){ + int scaledW= w/Map::cellScale; + int scaledH= h/Map::cellScale; + + this->fogOfWar = fogOfWar; + Renderer &renderer= Renderer::getInstance(); + + //fow pixmaps + float f= 0.f; + fowPixmap0= new Pixmap2D(next2Power(scaledW), next2Power(scaledH), 1); + fowPixmap1= new Pixmap2D(next2Power(scaledW), next2Power(scaledH), 1); + fowPixmap0->setPixels(&f); + fowPixmap1->setPixels(&f); + + //fow tex + fowTex= renderer.newTexture2D(rsGame); + fowTex->setMipmap(false); + fowTex->setPixmapInit(false); + fowTex->setFormat(Texture::fAlpha); + fowTex->getPixmap()->init(next2Power(scaledW), next2Power(scaledH), 1); + fowTex->getPixmap()->setPixels(&f); + + //tex + tex= renderer.newTexture2D(rsGame); + tex->getPixmap()->init(scaledW, scaledH, 3); + tex->setMipmap(false); + + computeTexture(world); +} + +Minimap::~Minimap(){ + Logger::getInstance().add("Minimap", true); + delete fowPixmap0; + delete fowPixmap1; +} + +// ==================== set ==================== + +void Minimap::incFowTextureAlphaSurface(const Vec2i &sPos, float alpha){ + + assert(sPos.xgetW() && sPos.ygetH()); + + if(fowPixmap1->getPixelf(sPos.x, sPos.y)setPixel(sPos.x, sPos.y, alpha); + } +} + +void Minimap::resetFowTex(){ + Pixmap2D *tmpPixmap= fowPixmap0; + fowPixmap0= fowPixmap1; + fowPixmap1= tmpPixmap; + + for(int i=0; igetPixmap()->getW(); ++i){ + for(int j=0; jgetPixmap()->getH(); ++j){ + if(fogOfWar){ + float p0= fowPixmap0->getPixelf(i, j); + float p1= fowPixmap1->getPixelf(i, j); + + if(p1>exploredAlpha){ + fowPixmap1->setPixel(i, j, exploredAlpha); + } + if(p0>p1){ + fowPixmap1->setPixel(i, j, p0); + } + } + else{ + fowPixmap1->setPixel(i, j, 1.f); + } + } + } +} + +void Minimap::updateFowTex(float t){ + for(int i=0; igetW(); ++i){ + for(int j=0; jgetH(); ++j){ + float p1= fowPixmap1->getPixelf(i, j); + if(p1!=fowTex->getPixmap()->getPixelf(i, j)){ + float p0= fowPixmap0->getPixelf(i, j); + fowTex->getPixmap()->setPixel(i, j, p0+(t*(p1-p0))); + } + } + } +} + +// ==================== PRIVATE ==================== + +void Minimap::computeTexture(const World *world){ + + Vec3f color; + const Map *map= world->getMap(); + + tex->getPixmap()->setPixels(Vec4f(1.f, 1.f, 1.f, 0.1f).ptr()); + + for(int j=0; jgetPixmap()->getH(); ++j){ + for(int i=0; igetPixmap()->getW(); ++i){ + SurfaceCell *sc= map->getSurfaceCell(i, j); + + if(sc->getObject()==NULL || sc->getObject()->getType()==NULL){ + const Pixmap2D *p= world->getTileset()->getSurfPixmap(sc->getSurfaceType(), 0); + color= p->getPixel3f(p->getW()/2, p->getH()/2); + color= color * static_cast(sc->getVertex().y/6.f); + + if(sc->getVertex().y<= world->getMap()->getWaterLevel()){ + color+= Vec3f(0.5f, 0.5f, 1.0f); + } + + if(color.x>1.f) color.x=1.f; + if(color.y>1.f) color.y=1.f; + if(color.z>1.f) color.z=1.f; + } + else{ + color= sc->getObject()->getType()->getColor(); + } + tex->getPixmap()->setPixel(i, j, color); + } + } +} + +}}//end namespace diff --git a/source/glest_game/world/minimap.h b/source/glest_game/world/minimap.h new file mode 100644 index 00000000..8212c2ff --- /dev/null +++ b/source/glest_game/world/minimap.h @@ -0,0 +1,69 @@ +// ============================================================== +// This file is part of Glest (www.glest.org) +// +// Copyright (C) 2001-2008 Martiņo Figueroa +// +// You can redistribute this code and/or modify it under +// the terms of the GNU General Public License as published +// by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version +// ============================================================== + +#ifndef _GLEST_GAME_MINIMAP_H_ +#define _GLEST_GAME_MINIMAP_H_ + +#include "pixmap.h" +#include "texture.h" + +namespace Glest{ namespace Game{ + +using Shared::Graphics::Vec4f; +using Shared::Graphics::Vec3f; +using Shared::Graphics::Vec2i; +using Shared::Graphics::Pixmap2D; +using Shared::Graphics::Texture2D; + +class World; + +enum ExplorationState{ + esNotExplored, + esExplored, + esVisible +}; + +// ===================================================== +// class Minimap +// +/// State of the in-game minimap +// ===================================================== + +class Minimap{ +private: + Pixmap2D *fowPixmap0; + Pixmap2D *fowPixmap1; + Texture2D *tex; + Texture2D *fowTex; //Fog Of War Texture2D + bool fogOfWar; + +private: + static const float exploredAlpha; + +public: + void init(int x, int y, const World *world, bool fogOfWar); + Minimap(); + ~Minimap(); + + const Texture2D *getFowTexture() const {return fowTex;} + const Texture2D *getTexture() const {return tex;} + + void incFowTextureAlphaSurface(const Vec2i &sPos, float alpha); + void resetFowTex(); + void updateFowTex(float t); + +private: + void computeTexture(const World *world); +}; + +}}//end namespace + +#endif diff --git a/source/glest_game/world/world.cpp b/source/glest_game/world/world.cpp index 735df5f2..4af26c50 100644 --- a/source/glest_game/world/world.cpp +++ b/source/glest_game/world/world.cpp @@ -39,7 +39,6 @@ const float World::airHeight= 5.f; World::World(){ Config &config= Config::getInstance(); - fogOfWar= config.getBool("FogOfWar"); fogOfWarSmoothing= config.getBool("FogOfWarSmoothing"); fogOfWarSmoothingFrameSkip= config.getInt("FogOfWarSmoothingFrameSkip"); @@ -70,8 +69,11 @@ void World::init(Game *game, bool createUnits){ unitUpdater.init(game); - initFactionTypes(game->getGameSettings()); - initCells(); //must be done after knowing faction number and dimensions + GameSettings *gs = game->getGameSettings(); + fogOfWar = gs->getFogOfWar(); + + initFactionTypes(gs); + initCells(fogOfWar); //must be done after knowing faction number and dimensions initMap(); initSplattedTextures(); @@ -80,7 +82,7 @@ void World::init(Game *game, bool createUnits){ if(createUnits){ initUnits(); } - initExplorationState(); + //initExplorationState(); ... was only for !fog-of-war, now handled in initCells() computeFow(); } @@ -487,7 +489,7 @@ int World::getUnitCountOfType(int factionIndex, const string &typeName){ // ==================== private init ==================== //init basic cell state -void World::initCells(){ +void World::initCells(bool fogOfWar){ Logger::getInstance().add("State cells", true); for(int i=0; isetExplored(k, false); - sc->setVisible(k, 0); - } + for (int k = 0; k < GameConstants::maxPlayers; k++) { + sc->setExplored(k, !fogOfWar); + sc->setVisible(k, !fogOfWar); + } } } } @@ -558,7 +560,7 @@ void World::initFactionTypes(GameSettings *gs){ } void World::initMinimap(){ - minimap.init(map.getW(), map.getH(), this); + minimap.init(map.getW(), map.getH(), this, game->getGameSettings()->getFogOfWar()); Logger::getInstance().add("Compute minimap surface", true); } @@ -600,18 +602,6 @@ void World::initMap(){ map.init(); } -void World::initExplorationState(){ - if(!fogOfWar){ - for(int i=0; isetVisible(thisTeamIndex, true); - map.getSurfaceCell(i, j)->setExplored(thisTeamIndex, true); - } - } - } -} - - // ==================== exploration ==================== void World::exploreCells(const Vec2i &newPos, int sightRange, int teamIndex){ diff --git a/source/glest_game/world/world.h b/source/glest_game/world/world.h index 646d487d..7a2e8b51 100644 --- a/source/glest_game/world/world.h +++ b/source/glest_game/world/world.h @@ -151,13 +151,13 @@ public: private: - void initCells(); + void initCells(bool fogOfWar); void initSplattedTextures(); void initFactionTypes(GameSettings *gs); void initMinimap(); void initUnits(); void initMap(); - void initExplorationState(); + //void initExplorationState(); //misc void tick(); diff --git a/source/shared_lib/include/util/leak_dumper.h b/source/shared_lib/include/util/leak_dumper.h new file mode 100644 index 00000000..9af034f9 --- /dev/null +++ b/source/shared_lib/include/util/leak_dumper.h @@ -0,0 +1,121 @@ +// ============================================================== +// This file is part of Glest Shared Library (www.glest.org) +// +// Copyright (C) 2001-2008 Martiņo Figueroa +// +// You can redistribute this code and/or modify it under +// the terms of the GNU General Public License as published +// by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version +// ============================================================== + +#ifndef _LEAKDUMPER_H_ +#define _LEAKDUMPER_H_ + +#define SL_LEAK_DUMP +//SL_LEAK_DUMP controls if leak dumping is enabled or not + +#ifdef SL_LEAK_DUMP + +#include +#include + +//including this header in any file of a project will cause all +//leaks to be dumped into leak_dump.txt, but only allocations that +//ocurred in a file where this header is included will have +//file and line number + +struct AllocInfo{ + int line; + const char *file; + size_t bytes; + void *ptr; + bool free; + bool array; + + AllocInfo(); + AllocInfo(void* ptr, const char* file, int line, size_t bytes, bool array); +}; + +// ===================================================== +// class AllocRegistry +// ===================================================== + +class AllocRegistry{ +private: + static const unsigned maxAllocs= 40000; + +private: + AllocRegistry(); + +private: + AllocInfo allocs[maxAllocs]; //array to store allocation info + int allocCount; //allocations + size_t allocBytes; //bytes allocated + int nonMonitoredCount; + size_t nonMonitoredBytes; + +public: + ~AllocRegistry(); + + static AllocRegistry &getInstance(); + + void allocate(AllocInfo info); + void deallocate(void* ptr, bool array); + void reset(); + void dump(const char *path); +}; + +//if an allocation ocurrs in a file where "leaks_dumper.h" is not included +//this operator new is called and file and line will be unknown +inline void * operator new (size_t bytes){ + void *ptr= malloc(bytes); + AllocRegistry::getInstance().allocate(AllocInfo(ptr, "unknown", 0, bytes, false)); + return ptr; +} + +inline void operator delete(void *ptr){ + AllocRegistry::getInstance().deallocate(ptr, false); + free(ptr); +} + +inline void * operator new[](size_t bytes){ + void *ptr= malloc(bytes); + AllocRegistry::getInstance().allocate(AllocInfo(ptr, "unknown", 0, bytes, true)); + return ptr; +} + +inline void operator delete [](void *ptr){ + AllocRegistry::getInstance().deallocate(ptr, true); + free(ptr); +} + +//if an allocation ocurrs in a file where "leaks_dumper.h" is included +//this operator new is called and file and line will be known +inline void * operator new (size_t bytes, char* file, int line){ + void *ptr= malloc(bytes); + AllocRegistry::getInstance().allocate(AllocInfo(ptr, file, line, bytes, false)); + return ptr; +} + +inline void operator delete(void *ptr, char* file, int line){ + AllocRegistry::getInstance().deallocate(ptr, false); + free(ptr); +} + +inline void * operator new[](size_t bytes, char* file, int line){ + void *ptr= malloc(bytes); + AllocRegistry::getInstance().allocate(AllocInfo(ptr, file, line, bytes, true)); + return ptr; +} + +inline void operator delete [](void *ptr, char* file, int line){ + AllocRegistry::getInstance().deallocate(ptr, true); + free(ptr); +} + +#define new new(__FILE__, __LINE__) + +#endif + +#endif diff --git a/source/shared_lib/sources/graphics/BMPReader.cpp b/source/shared_lib/sources/graphics/BMPReader.cpp index a122e412..47a04a2c 100644 --- a/source/shared_lib/sources/graphics/BMPReader.cpp +++ b/source/shared_lib/sources/graphics/BMPReader.cpp @@ -14,6 +14,8 @@ #include "pixmap.h" #include +#include "leak_dumper.h" + using std::runtime_error; namespace Shared{ namespace Graphics{ diff --git a/source/shared_lib/sources/graphics/ImageReaders.cpp b/source/shared_lib/sources/graphics/ImageReaders.cpp index 40eda024..46d3191a 100644 --- a/source/shared_lib/sources/graphics/ImageReaders.cpp +++ b/source/shared_lib/sources/graphics/ImageReaders.cpp @@ -10,6 +10,7 @@ // ============================================================== #include "ImageReaders.h" +#include "leak_dumper.h" namespace Shared{ namespace Graphics{ diff --git a/source/shared_lib/sources/graphics/JPGReader.cpp b/source/shared_lib/sources/graphics/JPGReader.cpp index a8780bb5..1e44ee03 100644 --- a/source/shared_lib/sources/graphics/JPGReader.cpp +++ b/source/shared_lib/sources/graphics/JPGReader.cpp @@ -18,6 +18,8 @@ #include #include +#include "leak_dumper.h" + using std::runtime_error; using std::ios; diff --git a/source/shared_lib/sources/graphics/PNGReader.cpp b/source/shared_lib/sources/graphics/PNGReader.cpp index 22323a94..3aea5aa7 100644 --- a/source/shared_lib/sources/graphics/PNGReader.cpp +++ b/source/shared_lib/sources/graphics/PNGReader.cpp @@ -17,6 +17,8 @@ #include #include +#include "leak_dumper.h" + using std::runtime_error; using std::ios; diff --git a/source/shared_lib/sources/graphics/TGAReader.cpp b/source/shared_lib/sources/graphics/TGAReader.cpp index 00b6adde..fe1cd88d 100644 --- a/source/shared_lib/sources/graphics/TGAReader.cpp +++ b/source/shared_lib/sources/graphics/TGAReader.cpp @@ -15,6 +15,8 @@ #include #include +#include "leak_dumper.h" + using std::runtime_error; namespace Shared{ namespace Graphics{ @@ -146,6 +148,6 @@ Pixmap2D* TGAReader::read(ifstream& in, const string& path, Pixmap2D* ret) const std::cout << " "; }*/ return ret; - } +} }} //end namespace diff --git a/source/shared_lib/sources/graphics/pixmap.cpp b/source/shared_lib/sources/graphics/pixmap.cpp index 462f8dd7..29ec12d0 100644 --- a/source/shared_lib/sources/graphics/pixmap.cpp +++ b/source/shared_lib/sources/graphics/pixmap.cpp @@ -18,13 +18,10 @@ #include "util.h" #include "math_util.h" #include "random.h" -#include "leak_dumper.h" #include "FileReader.h" - -//Readers #include "ImageReaders.h" - +#include "leak_dumper.h" using namespace Shared::Util; using namespace std; diff --git a/source/shared_lib/sources/util/leak_dumper.cpp b/source/shared_lib/sources/util/leak_dumper.cpp index f20cb152..0345a777 100644 --- a/source/shared_lib/sources/util/leak_dumper.cpp +++ b/source/shared_lib/sources/util/leak_dumper.cpp @@ -13,22 +13,12 @@ #ifdef SL_LEAK_DUMP -AllocInfo::AllocInfo(){ - ptr= NULL; - file= ""; - line= -1; - bytes= -1; - array= false; - free= true; +AllocInfo::AllocInfo() + : ptr(0), file(""), line(-1), bytes(-1), array(false), free(false) { } -AllocInfo::AllocInfo(void* ptr, const char* file, int line, size_t bytes, bool array){ - this->ptr= ptr; - this->file= file; - this->line= line; - this->bytes= bytes; - this->array= array; - free= false; +AllocInfo::AllocInfo(void* ptr, const char* file, int line, size_t bytes, bool array) + : ptr(ptr), file(file), line(line), bytes(bytes), array(array), free(false) { } // ===================================================== @@ -53,12 +43,7 @@ AllocRegistry &AllocRegistry::getInstance(){ AllocRegistry::~AllocRegistry(){ - string leakLog = "leak_dump.log"; - if(getGameReadWritePath() != "") { - leakLog = getGameReadWritePath() + leakLog; - } - - dump(leakLog.c_str()); + dump("leak_dump.log"); } void AllocRegistry::allocate(AllocInfo info){ diff --git a/source/shared_lib/sources/util/profiler.cpp b/source/shared_lib/sources/util/profiler.cpp index 93c87840..819654ec 100644 --- a/source/shared_lib/sources/util/profiler.cpp +++ b/source/shared_lib/sources/util/profiler.cpp @@ -14,6 +14,7 @@ #ifdef SL_PROFILE #include +#include "leak_dumper.h" using namespace std;