/* * ORXONOX - the hottest 3D action shooter ever to exist * > www.orxonox.net < * * * License notice: * * This program is free software; you can redistribute it 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. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Author: * Oliver Scheuss * Co-authors: * ... * */ // // C++ Interface: GamestateManager // // Description: // // // Author: Oliver Scheuss, (C) 2007 // // Copyright: See COPYING file that comes with this distribution // // #ifndef _GamestateManager_H__ #define _GamestateManager_H__ #include "NetworkPrereqs.h" #include #include "GamestateHandler.h" #include "core/CorePrereqs.h" #include "packet/Gamestate.h" namespace orxonox { const int KEEP_GAMESTATES = 10; /** * This Class implements a manager for gamestates: * - creating snapshots of gamestates * - writing gamestates to universe * - diffing gamestates * * Inherited by Host * * EN/DECODATION: * a: last Gamestate a client has received * b: new Gamestate * x: diffed and compressed gamestate * x = (a ^ b) * b = (a ^ x) * diff(a, diff(a, x)) = x (hope this is correct) * @author Oliver Scheuss */ class _NetworkExport GamestateManager: public GamestateHandler { struct peerInfo { uint32_t peerID; uint32_t lastReceivedGamestateID; //!< id of the last gamestate which was received (and processed) from the peer uint32_t lastAckedGamestateID; //!< id of the last gamestate on which we received an ack from the peer bool isSynched; std::map gamestates; }; public: GamestateManager(); ~GamestateManager(); virtual bool addGamestate(packet::Gamestate *gs, unsigned int peerID) override; virtual bool ackGamestate(unsigned int gamestateID, unsigned int peerID) override; virtual uint32_t getLastReceivedGamestateID( unsigned int peerID ) override; virtual uint32_t getCurrentGamestateID() override { if(currentGamestate_) return currentGamestate_->getID(); else return GAMESTATEID_INITIAL; } bool processGamestates(); bool sendAck(unsigned int gamestateID, uint32_t peerID); bool update(); std::vector getGamestates(); void finishGamestate( unsigned int peerID, packet::Gamestate*& destgamestate, packet::Gamestate* base, packet::Gamestate* gamestate ); bool getSnapshot(); void addPeer( uint32_t peerID ); void setSynched( uint32_t peerID ) { assert(peerMap_.find(peerID) != peerMap_.end()); peerMap_[peerID].isSynched = true; } void removePeer( uint32_t peerID ); bool hasPeers(){ return this->peerMap_.size() != 0; } protected: virtual bool sendPacket( packet::Packet* packet ) = 0; private: bool processGamestate(packet::Gamestate *gs); // TODO: What is the purpose of the gamestateQueue? std::map gamestateQueue; // Each peerID maps to a peerInfo struct std::map peerMap_; // always contains the latest gamestate packet::Gamestate* currentGamestate_; unsigned int id_; ThreadPool* Pool_; }; } #endif /* _GameStateManager_H__ */