Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/questsystem4/src/network/GamestateClient.cc @ 2908

Last change on this file since 2908 was 2371, checked in by scheusso, 16 years ago

merged network branch to presentation branch

  • Property svn:eol-style set to native
File size: 4.7 KB
Line 
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
31#include <cassert>
32#include <zlib.h>
33
34#include "core/CoreIncludes.h"
35#include "core/BaseObject.h"
36#include "core/Iterator.h"
37#include "synchronisable/Synchronisable.h"
38#include "synchronisable/NetworkCallbackManager.h"
39#include "packet/Acknowledgement.h"
40
41
42namespace orxonox
43{
44  struct _NetworkExport GameStateItem{
45    packet::Gamestate *state;
46    unsigned int id;
47  };
48
49  GamestateClient::GamestateClient() {
50    COUT(5) << "this: " << this << std::endl;
51    last_diff_=0;
52    last_gamestate_=GAMESTATEID_INITIAL-1;
53    tempGamestate_=NULL;
54  }
55
56  GamestateClient::~GamestateClient() {
57  }
58
59  bool GamestateClient::ack(unsigned int gamestateID, unsigned int clientID){
60    return true;
61  }
62
63  bool GamestateClient::add(packet::Gamestate *gs, unsigned int clientID){
64    if(tempGamestate_!=NULL){
65      //delete the obsolete gamestate
66      if(tempGamestate_->getID()>gs->getID())
67        return false;
68      delete tempGamestate_;
69    }
70    tempGamestate_=gs;
71    return true;
72  }
73
74  bool GamestateClient::processGamestates(){
75    if(tempGamestate_==NULL)
76      return false;
77    int id = GAMESTATEID_INITIAL;
78    packet::Gamestate *processed = processGamestate(tempGamestate_);
79//    assert(processed);
80   
81    //now call the queued callbacks
82    NetworkCallbackManager::callCallbacks();
83   
84    if (!processed)
85        return false;
86    //successfully loaded data from gamestate. now save gamestate for diff and delete the old gs
87    tempGamestate_=NULL;
88    gamestateMap_[processed->getID()]=processed;
89    last_diff_ = processed->getID();
90    id = processed->getID();
91    sendAck(id);
92    return true;
93  }
94
95
96  /**
97  * This function removes a Synchronisable out of the universe
98  * @param it iterator of the list pointing to the object
99  * @return iterator pointing to the next object in the list
100  */
101  void GamestateClient::removeObject(ObjectList<Synchronisable>::iterator &it) {
102    ObjectList<Synchronisable>::iterator temp=it;
103    ++it;
104    delete  *temp;
105  }
106
107  packet::Gamestate *GamestateClient::getGamestate(){
108    packet::Gamestate *gs = new packet::Gamestate();
109    if(!gs->collectData(0)){
110      delete gs;
111      return 0;
112    }
113    return gs;
114  }
115
116  void GamestateClient::cleanup(){
117    std::map<unsigned int, packet::Gamestate*>::iterator temp, it = gamestateMap_.begin();
118    while(it!=gamestateMap_.end()){
119      if(it->first>=last_diff_)
120        break;
121      // otherwise delete that stuff
122      delete (*it).second;
123      temp=it++;
124      gamestateMap_.erase(temp);
125    }
126    tempGamestate_=NULL;
127  }
128
129  void GamestateClient::printGamestateMap(){
130    std::map<unsigned int, packet::Gamestate*>::iterator it;
131    COUT(4) << "gamestates: ";
132    for(it=gamestateMap_.begin(); it!=gamestateMap_.end(); it++){
133      COUT(4) << it->first << ":" << it->second << "|";
134    }
135    COUT(4) << std::endl;
136
137  }
138
139  bool GamestateClient::sendAck(unsigned int gamestateID){
140    packet::Acknowledgement *ack = new packet::Acknowledgement(gamestateID, 0);
141    if(!ack->send()){
142      COUT(3) << "could not ack gamestate: " << gamestateID << std::endl;
143      return false;
144    }
145    else{
146      COUT(5) << "acked a gamestate: " << gamestateID << std::endl;
147      return true;
148    }
149  }
150
151  packet::Gamestate *GamestateClient::processGamestate(packet::Gamestate *gs){
152    if(gs->isCompressed())
153    {
154      bool b = gs->decompressData();
155      assert(b);
156    }
157    if(gs->isDiffed()){
158      packet::Gamestate *base = gamestateMap_[gs->getBaseID()];
159      if(!base){
160        delete gs;
161        return 0;
162      }
163//       assert(base); //TODO: fix this
164      packet::Gamestate *undiffed = gs->undiff(base);
165      delete gs;
166      gs=undiffed;
167      COUT(5) << "successfully undiffed gamestate id: " << undiffed->getID() << std::endl;
168    }
169    if(gs->spreadData())
170      return gs;
171    else
172      return NULL;
173  }
174
175}
176
Note: See TracBrowser for help on using the repository browser.