Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network64/src/network/GamestateClient.cc @ 2355

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

made some adjustments mostly to the networkid (classid) in order to have it platform independent

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