Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy2/src/network/GamestateClient.cc @ 2441

Last change on this file since 2441 was 2176, checked in by scheusso, 16 years ago

(re)implemented NACKing for gamestates

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