Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/network/GamestateClient.cc @ 1916

Last change on this file since 1916 was 1916, checked in by landauf, 16 years ago

removed WorldEntity, SpaceShip and several other objects
removed SpaceShip-related hacks in network and other places

  • Property svn:eol-style set to native
File size: 4.4 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 _NetworkExport 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  }
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  bool GamestateClient::processGamestates(){
73    if(tempGamestate_==NULL)
74      return false;
75    int id = GAMESTATEID_INITIAL;
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_=NULL;
80    gamestateMap_[processed->getID()]=processed;
81    last_diff_ = processed->getID();
82    id = processed->getID();
83    sendAck(id);
84    return true;
85  }
86
87
88  /**
89  * This function removes a Synchronisable out of the universe
90  * @param it iterator of the list pointing to the object
91  * @return iterator pointing to the next object in the list
92  */
93  void GamestateClient::removeObject(orxonox::ObjectList<Synchronisable>::iterator &it) {
94    orxonox::ObjectList<Synchronisable>::iterator temp=it;
95    ++it;
96    delete  *temp;
97  }
98
99  packet::Gamestate *GamestateClient::getGamestate(){
100    packet::Gamestate *gs = new packet::Gamestate();
101    gs->collectData(0);
102    return gs;
103  }
104
105  void GamestateClient::cleanup(){
106    std::map<int, packet::Gamestate*>::iterator temp, it = gamestateMap_.begin();
107    while(it!=gamestateMap_.end()){
108      if(it->first>=last_diff_)
109        break;
110      // otherwise delete that stuff
111      delete (*it).second;
112      temp=it++;
113      gamestateMap_.erase(temp);
114    }
115    tempGamestate_=NULL;
116  }
117
118  void GamestateClient::printGamestateMap(){
119    std::map<int, packet::Gamestate*>::iterator it;
120    COUT(4) << "gamestates: ";
121    for(it=gamestateMap_.begin(); it!=gamestateMap_.end(); it++){
122      COUT(4) << it->first << ":" << it->second << "|";
123    }
124    COUT(4) << std::endl;
125
126  }
127
128  bool GamestateClient::sendAck(unsigned int gamestateID){
129    packet::Acknowledgement *ack = new packet::Acknowledgement(gamestateID, 0);
130    if(!ack->send()){
131      COUT(3) << "could not ack gamestate: " << gamestateID << std::endl;
132      return false;
133    }
134    else{
135      COUT(3) << "acked a gamestate: " << gamestateID << std::endl;
136      return true;
137    }
138  }
139
140  packet::Gamestate *GamestateClient::processGamestate(packet::Gamestate *gs){
141    if(gs->isCompressed())
142    {
143      bool b = gs->decompressData();
144      assert(b);
145    }
146    if(gs->isDiffed()){
147      packet::Gamestate *base = gamestateMap_[gs->getBaseID()];
148      if(!base){
149        delete gs;
150        return 0;
151      }
152//       assert(base); //TODO: fix this
153      packet::Gamestate *undiffed = gs->undiff(base);
154      delete gs;
155      gs=undiffed;
156      COUT(3) << "successfully undiffed gamestate id: " << undiffed->getID() << std::endl;
157    }
158    if(gs->spreadData())
159      return gs;
160    else
161      return NULL;
162  }
163
164}
165
Note: See TracBrowser for help on using the repository browser.