Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/network/GamestateClient.cc @ 1741

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

compressing doesnt work yet (maybe rather decompressing)

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