Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/network/GamestateClient.cc @ 1767

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

problems of the merge should be solved now (hopefully)

  • Property svn:eol-style set to native
File size: 5.1 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
38
39namespace network
40{
41  struct GameStateItem{
42    packet::Gamestate *state;
43    int id;
44  };
45
46  GamestateClient::GamestateClient() {
47    COUT(5) << "this: " << this << std::endl;
48    last_diff_=0;
49    last_gamestate_=GAMESTATEID_INITIAL-1;
50    tempGamestate_=NULL;
51    myShip_=NULL;
52  }
53
54  GamestateClient::~GamestateClient() {
55  }
56
57  bool GamestateClient::ack(int gamestateID, int clientID){
58    return true;
59  }
60
61  bool GamestateClient::add(packet::Gamestate *gs, int clientID){
62    if(tempGamestate_!=NULL){
63      //delete the obsolete gamestate
64      if(tempGamestate_->getID()>gs->getID())
65        return false;
66      delete tempGamestate_;
67    }
68    tempGamestate_=gs;
69    return true;
70  }
71
72  int GamestateClient::processGamestates(){
73    if(tempGamestate_==NULL)
74      return 0;
75    int id = GAMESTATEID_INITIAL;
76    bool b = saveShipCache();
77    packet::Gamestate *processed = processGamestate(tempGamestate_);
78    if(!processed)
79      return GAMESTATEID_INITIAL;
80//    assert(processed);
81    //successfully loaded data from gamestate. now save gamestate for diff and delete the old gs
82    tempGamestate_=0;
83    gamestateMap_[processed->getID()]=processed;
84    last_diff_ = processed->getID();
85    if(b)
86      loadShipCache();
87    id = processed->getID();
88    cleanup();
89    return id;
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(orxonox::ObjectList<Synchronisable>::iterator &it) {
99    orxonox::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    gs->collectData(0);
107    return gs;
108  }
109
110  void GamestateClient::cleanup(){
111    std::map<int, packet::Gamestate*>::iterator temp, it = gamestateMap_.begin();
112    while(it!=gamestateMap_.end()){
113      if(it->first>=last_diff_)
114        break;
115      // otherwise delete that stuff
116      delete (*it).second;
117      temp=it++;
118      gamestateMap_.erase(temp);
119    }
120    tempGamestate_=NULL;
121  }
122
123  void GamestateClient::printGamestateMap(){
124    std::map<int, packet::Gamestate*>::iterator it;
125    COUT(4) << "gamestates: ";
126    for(it=gamestateMap_.begin(); it!=gamestateMap_.end(); it++){
127      COUT(4) << it->first << ":" << it->second << "|";
128    }
129    COUT(4) << std::endl;
130
131  }
132
133  bool GamestateClient::saveShipCache(){
134    if(myShip_==NULL){
135      myShip_ = orxonox::SpaceShip::getLocalShip();
136//      COUT(2) << "myShip_: " << myShip_ << " getLocalShip(): " << orxonox::SpaceShip::getLocalShip() << std::endl;
137      if(!myShip_)
138        return false;
139    }
140    if(myShip_){
141      //      unsigned char *data = new unsigned char[myShip_->getSize()];
142      int size=myShip_->getSize2(0, 0x1);
143      if(size==0)
144        return false;
145      shipCache_ = new unsigned char [size];
146      unsigned char *temp = shipCache_;
147      if(!myShip_->getData(temp, 0, 0x1))
148        COUT(3) << "could not save shipCache" << std::endl;
149      return true;
150    }else
151      return false;
152  }
153
154  bool GamestateClient::loadShipCache(){
155    myShip_=orxonox::SpaceShip::getLocalShip(); //TODO: remove this (only a hack)
156    if(myShip_ && shipCache_){
157      assert(myShip_->getIdentifier());
158      unsigned char *temp = shipCache_;
159      myShip_->updateData(temp, 0x2);
160      delete shipCache_;
161      return true;
162    }else
163      return false;
164  }
165
166  packet::Gamestate *GamestateClient::processGamestate(packet::Gamestate *gs){
167    if(gs->isCompressed())
168      assert(gs->decompressData());
169    if(gs->isDiffed()){
170      packet::Gamestate *base = gamestateMap_[gs->getBaseID()];
171      if(!base)
172        return 0;
173//      assert(base); //TODO: fix this
174      packet::Gamestate *undiffed = gs->undiff(base);
175      delete gs;
176      gs=undiffed;
177      COUT(3) << "successfully undiffed gamestate id: " << undiffed->getID() << std::endl;
178    }
179    if(gs->spreadData())
180      return gs;
181    else
182      return NULL;
183  }
184
185}
186
Note: See TracBrowser for help on using the repository browser.