Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/bugger/src/network/GamestateClient.cc @ 2532

Last change on this file since 2532 was 2531, checked in by rgrieder, 16 years ago

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