Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutorialHS08/src/network/GamestateClient.cc @ 2499

Last change on this file since 2499 was 1769, checked in by scheusso, 16 years ago

sync frequencies do work now

  • Property svn:eol-style set to native
File size: 5.5 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 <zlib.h>
32
33#include "core/CoreIncludes.h"
34#include "core/BaseObject.h"
35#include "core/Iterator.h"
36#include "Synchronisable.h"
37#include "packet/Acknowledgement.h"
38
39
40namespace network
41{
42  struct GameStateItem{
43    packet::Gamestate *state;
44    int id;
45  };
46
47  GamestateClient::GamestateClient() {
48    COUT(5) << "this: " << this << std::endl;
49    last_diff_=0;
50    last_gamestate_=GAMESTATEID_INITIAL-1;
51    tempGamestate_=NULL;
52    myShip_=NULL;
53  }
54
55  GamestateClient::~GamestateClient() {
56  }
57
58  bool GamestateClient::ack(int gamestateID, int clientID){
59    return true;
60  }
61
62  bool GamestateClient::add(packet::Gamestate *gs, 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    bool b = saveShipCache();
78    packet::Gamestate *processed = processGamestate(tempGamestate_);
79    if(!processed){
80      if(b)
81        loadShipCache();
82      return false;
83    }
84//    assert(processed);
85    //successfully loaded data from gamestate. now save gamestate for diff and delete the old gs
86    tempGamestate_=NULL;
87    gamestateMap_[processed->getID()]=processed;
88    last_diff_ = processed->getID();
89    if(b)
90      loadShipCache();
91    id = processed->getID();
92    sendAck(id);
93    return true;
94  }
95
96
97  /**
98  * This function removes a Synchronisable out of the universe
99  * @param it iterator of the list pointing to the object
100  * @return iterator pointing to the next object in the list
101  */
102  void GamestateClient::removeObject(orxonox::ObjectList<Synchronisable>::iterator &it) {
103    orxonox::ObjectList<Synchronisable>::iterator temp=it;
104    ++it;
105    delete  *temp;
106  }
107
108  packet::Gamestate *GamestateClient::getGamestate(){
109    packet::Gamestate *gs = new packet::Gamestate();
110    gs->collectData(0);
111    return gs;
112  }
113
114  void GamestateClient::cleanup(){
115    std::map<int, packet::Gamestate*>::iterator temp, it = gamestateMap_.begin();
116    while(it!=gamestateMap_.end()){
117      if(it->first>=last_diff_)
118        break;
119      // otherwise delete that stuff
120      delete (*it).second;
121      temp=it++;
122      gamestateMap_.erase(temp);
123    }
124    tempGamestate_=NULL;
125  }
126
127  void GamestateClient::printGamestateMap(){
128    std::map<int, packet::Gamestate*>::iterator it;
129    COUT(4) << "gamestates: ";
130    for(it=gamestateMap_.begin(); it!=gamestateMap_.end(); it++){
131      COUT(4) << it->first << ":" << it->second << "|";
132    }
133    COUT(4) << std::endl;
134
135  }
136 
137  bool GamestateClient::sendAck(unsigned int gamestateID){
138    packet::Acknowledgement *ack = new packet::Acknowledgement(gamestateID, 0);
139    if(!ack->send()){
140      COUT(3) << "could not ack gamestate: " << gamestateID << std::endl;
141      return false;
142    }
143    else{
144      COUT(3) << "acked a gamestate: " << gamestateID << std::endl;
145      return true;
146    }
147  }
148
149  bool GamestateClient::saveShipCache(){
150    if(myShip_==NULL){
151      myShip_ = orxonox::SpaceShip::getLocalShip();
152//      COUT(2) << "myShip_: " << myShip_ << " getLocalShip(): " << orxonox::SpaceShip::getLocalShip() << std::endl;
153      if(!myShip_)
154        return false;
155    }
156    if(myShip_){
157      //      unsigned char *data = new unsigned char[myShip_->getSize()];
158      int size=myShip_->getSize2(0, 0x1);
159      if(size==0)
160        return false;
161      shipCache_ = new unsigned char [size];
162      unsigned char *temp = shipCache_;
163      if(!myShip_->getData(temp, 0, 0x1))
164        COUT(3) << "could not save shipCache" << std::endl;
165      return true;
166    }else
167      return false;
168  }
169
170  bool GamestateClient::loadShipCache(){
171    myShip_=orxonox::SpaceShip::getLocalShip(); //TODO: remove this (only a hack)
172    if(myShip_ && shipCache_){
173      assert(myShip_->getIdentifier());
174      unsigned char *temp = shipCache_;
175      myShip_->updateData(temp, 0x2);
176      delete shipCache_;
177      return true;
178    }else
179      return false;
180  }
181
182  packet::Gamestate *GamestateClient::processGamestate(packet::Gamestate *gs){
183    if(gs->isCompressed())
184      assert(gs->decompressData());
185    if(gs->isDiffed()){
186      packet::Gamestate *base = gamestateMap_[gs->getBaseID()];
187      if(!base){
188        delete gs;
189        return 0;
190      }
191//      assert(base); //TODO: fix this
192      packet::Gamestate *undiffed = gs->undiff(base);
193      delete gs;
194      gs=undiffed;
195      COUT(3) << "successfully undiffed gamestate id: " << undiffed->getID() << std::endl;
196    }
197    if(gs->spreadData())
198      return gs;
199    else
200      return NULL;
201  }
202
203}
204
Note: See TracBrowser for help on using the repository browser.