Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy2/src/network/GamestateClient.cc @ 2256

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

(re)implemented NACKing for gamestates

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