Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gui/src/network/GamestateClient.cc @ 2850

Last change on this file since 2850 was 2662, checked in by rgrieder, 16 years ago

Merged presentation branch back to trunk.

  • Property svn:eol-style set to native
File size: 4.9 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    bool isDiffed = tempGamestate_->isDiffed();
78    int id = GAMESTATEID_INITIAL;
79    packet::Gamestate *processed = processGamestate(tempGamestate_);
80    assert(processed);
81   
82    //now call the queued callbacks
83    NetworkCallbackManager::callCallbacks();
84   
85    if (!processed){
86      sendAck(0);
87      return false;
88    }
89    //successfully loaded data from gamestate. now save gamestate for diff and delete the old gs
90    tempGamestate_=NULL;
91    gamestateMap_[processed->getID()]=processed;
92    if(isDiffed)
93      last_diff_ = processed->getBaseID();
94    id = processed->getID();
95    sendAck(id);
96    return true;
97  }
98
99
100  /**
101  * This function removes a Synchronisable out of the universe
102  * @param it iterator of the list pointing to the object
103  * @return iterator pointing to the next object in the list
104  */
105  void GamestateClient::removeObject(ObjectList<Synchronisable>::iterator &it) {
106    ObjectList<Synchronisable>::iterator temp=it;
107    ++it;
108    delete  *temp;
109  }
110
111  packet::Gamestate *GamestateClient::getGamestate(){
112    packet::Gamestate *gs = new packet::Gamestate();
113    if(!gs->collectData(0)){
114      delete gs;
115      return 0;
116    }
117    return gs;
118  }
119
120  void GamestateClient::cleanup(){
121    std::map<unsigned int, packet::Gamestate*>::iterator temp, it = gamestateMap_.begin();
122    while(it!=gamestateMap_.end()){
123      if(it->first>=last_diff_)
124        break;
125      // otherwise delete that stuff
126      delete (*it).second;
127      temp=it++;
128      gamestateMap_.erase(temp);
129    }
130    tempGamestate_=NULL;
131  }
132
133  void GamestateClient::printGamestateMap(){
134    std::map<unsigned int, packet::Gamestate*>::iterator it;
135    COUT(4) << "gamestates: ";
136    for(it=gamestateMap_.begin(); it!=gamestateMap_.end(); it++){
137      COUT(4) << it->first << ":" << it->second << "|";
138    }
139    COUT(4) << std::endl;
140
141  }
142
143  bool GamestateClient::sendAck(unsigned int gamestateID){
144    packet::Acknowledgement *ack = new packet::Acknowledgement(gamestateID, 0);
145    if(!ack->send()){
146      COUT(3) << "could not ack gamestate: " << gamestateID << std::endl;
147      return false;
148    }
149    else{
150      COUT(5) << "acked a gamestate: " << gamestateID << std::endl;
151      return true;
152    }
153  }
154
155  packet::Gamestate *GamestateClient::processGamestate(packet::Gamestate *gs){
156    if(gs->isCompressed())
157    {
158      bool b = gs->decompressData();
159      assert(b);
160    }
161    if(gs->isDiffed()){
162      packet::Gamestate *base = gamestateMap_[gs->getBaseID()];
163      if(!base){
164        COUT(3) << "could not find base gamestate id: " << gs->getBaseID() << endl;
165        delete gs;
166        return 0;
167      }
168//       assert(base); //TODO: fix this
169      packet::Gamestate *undiffed = gs->undiff(base);
170      delete gs;
171      gs=undiffed;
172      COUT(5) << "successfully undiffed gamestate id: " << undiffed->getID() << std::endl;
173    }
174    if(gs->spreadData())
175      return gs;
176    else
177    {
178      COUT(3) << "could not spread gamestate" << endl;
179      return NULL;
180    }
181  }
182
183}
184
Note: See TracBrowser for help on using the repository browser.