Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/network/GamestateClient.cc @ 7687

Last change on this file since 7687 was 7401, checked in by landauf, 14 years ago

merged doc branch back to trunk

  • Property svn:eol-style set to native
File size: 5.2 KB
RevLine 
[1707]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Oliver Scheuss
24 *   Co-authors:
25 *      Dumeni Manatschal
26 *
27 */
28
29#include "GamestateClient.h"
30
[3214]31#include "util/Debug.h"
32#include "core/ObjectList.h"
[2662]33#include "synchronisable/Synchronisable.h"
34#include "synchronisable/NetworkCallbackManager.h"
[1769]35#include "packet/Acknowledgement.h"
[3214]36#include "packet/Gamestate.h"
[1707]37
38
[2171]39namespace orxonox
[1707]40{
[2087]41  struct _NetworkExport GameStateItem{
[1707]42    packet::Gamestate *state;
[2087]43    unsigned int id;
[1707]44  };
45
46  GamestateClient::GamestateClient() {
47    COUT(5) << "this: " << this << std::endl;
48    last_diff_=0;
49    last_gamestate_=GAMESTATEID_INITIAL-1;
50    tempGamestate_=NULL;
51  }
52
53  GamestateClient::~GamestateClient() {
[3304]54      std::map<unsigned int, packet::Gamestate *>::iterator it;
55      for ( it = this->gamestateMap_.begin(); it != this->gamestateMap_.end(); ++it )
[6417]56          delete it->second;
[3304]57      if( this->tempGamestate_ )
58          delete this->tempGamestate_;
[1707]59  }
60
[2087]61  bool GamestateClient::ack(unsigned int gamestateID, unsigned int clientID){
[1707]62    return true;
63  }
64
[2087]65  bool GamestateClient::add(packet::Gamestate *gs, unsigned int clientID){
[1707]66    if(tempGamestate_!=NULL){
67      //delete the obsolete gamestate
68      if(tempGamestate_->getID()>gs->getID())
69        return false;
70      delete tempGamestate_;
71    }
72    tempGamestate_=gs;
73    return true;
74  }
[1747]75
[1769]76  bool GamestateClient::processGamestates(){
[1707]77    if(tempGamestate_==NULL)
[1769]78      return false;
[2662]79    bool isDiffed = tempGamestate_->isDiffed();
[1707]80    int id = GAMESTATEID_INITIAL;
[1751]81    packet::Gamestate *processed = processGamestate(tempGamestate_);
[2662]82    assert(processed);
[6417]83
[2662]84    //now call the queued callbacks
85    NetworkCallbackManager::callCallbacks();
[6417]86
[2662]87    if (!processed){
88      sendAck(0);
89      return false;
90    }
[1751]91    //successfully loaded data from gamestate. now save gamestate for diff and delete the old gs
[1769]92    tempGamestate_=NULL;
[1751]93    gamestateMap_[processed->getID()]=processed;
[2662]94    if(isDiffed)
95      last_diff_ = processed->getBaseID();
[1751]96    id = processed->getID();
[1769]97    sendAck(id);
98    return true;
[1707]99  }
100
101
102  /**
103  * This function removes a Synchronisable out of the universe
104  * @param it iterator of the list pointing to the object
105  * @return iterator pointing to the next object in the list
106  */
[7401]107  void GamestateClient::removeObject(ObjectListIterator<Synchronisable> &it) {
108    ObjectListIterator<Synchronisable> temp=it;
[1707]109    ++it;
[5929]110    temp->destroy(); // or delete?
[1707]111  }
112
113  packet::Gamestate *GamestateClient::getGamestate(){
114    packet::Gamestate *gs = new packet::Gamestate();
[3102]115    if(!gs->collectData(0,0x2)){
[2087]116      delete gs;
117      return 0;
118    }
[1707]119    return gs;
120  }
121
122  void GamestateClient::cleanup(){
[2087]123    std::map<unsigned int, packet::Gamestate*>::iterator temp, it = gamestateMap_.begin();
[1707]124    while(it!=gamestateMap_.end()){
125      if(it->first>=last_diff_)
126        break;
127      // otherwise delete that stuff
[6417]128      delete it->second;
[1707]129      temp=it++;
130      gamestateMap_.erase(temp);
131    }
132    tempGamestate_=NULL;
133  }
134
135  void GamestateClient::printGamestateMap(){
[2087]136    std::map<unsigned int, packet::Gamestate*>::iterator it;
[1707]137    COUT(4) << "gamestates: ";
138    for(it=gamestateMap_.begin(); it!=gamestateMap_.end(); it++){
[6417]139      COUT(4) << it->first << ':' << it->second << '|';
[1707]140    }
141    COUT(4) << std::endl;
142
143  }
[2087]144
[1769]145  bool GamestateClient::sendAck(unsigned int gamestateID){
146    packet::Acknowledgement *ack = new packet::Acknowledgement(gamestateID, 0);
147    if(!ack->send()){
148      COUT(3) << "could not ack gamestate: " << gamestateID << std::endl;
149      return false;
150    }
151    else{
[2087]152      COUT(5) << "acked a gamestate: " << gamestateID << std::endl;
[1769]153      return true;
154    }
155  }
[1707]156
[1751]157  packet::Gamestate *GamestateClient::processGamestate(packet::Gamestate *gs){
158    if(gs->isCompressed())
[1907]159    {
160      bool b = gs->decompressData();
161      assert(b);
162    }
[7163]163    if(gs->isDiffed())
164    {
165      assert(0);
166//       packet::Gamestate *base = gamestateMap_[gs->getBaseID()];
167//       if(!base)
168//       {
169//         COUT(0) << "could not find base gamestate id: " << gs->getBaseID() << endl;
170//         assert(0);
171//         delete gs;
172//         return 0;
173//       }
174//       packet::Gamestate *undiffed = gs->undiff(base);
175//       delete gs;
176//       gs=undiffed;
177//       COUT(5) << "successfully undiffed gamestate id: " << undiffed->getID() << std::endl;
[1751]178    }
[3102]179    if(gs->spreadData(0x2))
[1751]180      return gs;
181    else
[2662]182    {
[7163]183      COUT(0) << "could not spread gamestate" << endl;
184      assert(0);
[1751]185      return NULL;
[2662]186    }
[1712]187  }
[1707]188
189}
190
Note: See TracBrowser for help on using the repository browser.