Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

network branch merged into trunk

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