Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged network branch back into trunk

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