Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2835 was 2662, checked in by rgrieder, 16 years ago

Merged presentation branch back to trunk.

  • Property svn:eol-style set to native
File size: 4.9 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"
[2662]37#include "synchronisable/Synchronisable.h"
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;
[2662]77    bool isDiffed = tempGamestate_->isDiffed();
[1707]78    int id = GAMESTATEID_INITIAL;
[1751]79    packet::Gamestate *processed = processGamestate(tempGamestate_);
[2662]80    assert(processed);
81   
82    //now call the queued callbacks
83    NetworkCallbackManager::callCallbacks();
84   
85    if (!processed){
86      sendAck(0);
87      return false;
88    }
[1751]89    //successfully loaded data from gamestate. now save gamestate for diff and delete the old gs
[1769]90    tempGamestate_=NULL;
[1751]91    gamestateMap_[processed->getID()]=processed;
[2662]92    if(isDiffed)
93      last_diff_ = processed->getBaseID();
[1751]94    id = processed->getID();
[1769]95    sendAck(id);
96    return true;
[1707]97  }
98
99
100  /**
101  * This function removes a Synchronisable out of the universe
102  * @param it iterator of the list pointing to the object
103  * @return iterator pointing to the next object in the list
104  */
[2171]105  void GamestateClient::removeObject(ObjectList<Synchronisable>::iterator &it) {
106    ObjectList<Synchronisable>::iterator temp=it;
[1707]107    ++it;
108    delete  *temp;
109  }
110
111  packet::Gamestate *GamestateClient::getGamestate(){
112    packet::Gamestate *gs = new packet::Gamestate();
[2087]113    if(!gs->collectData(0)){
114      delete gs;
115      return 0;
116    }
[1707]117    return gs;
118  }
119
120  void GamestateClient::cleanup(){
[2087]121    std::map<unsigned int, packet::Gamestate*>::iterator temp, it = gamestateMap_.begin();
[1707]122    while(it!=gamestateMap_.end()){
123      if(it->first>=last_diff_)
124        break;
125      // otherwise delete that stuff
126      delete (*it).second;
127      temp=it++;
128      gamestateMap_.erase(temp);
129    }
130    tempGamestate_=NULL;
131  }
132
133  void GamestateClient::printGamestateMap(){
[2087]134    std::map<unsigned int, packet::Gamestate*>::iterator it;
[1707]135    COUT(4) << "gamestates: ";
136    for(it=gamestateMap_.begin(); it!=gamestateMap_.end(); it++){
137      COUT(4) << it->first << ":" << it->second << "|";
138    }
139    COUT(4) << std::endl;
140
141  }
[2087]142
[1769]143  bool GamestateClient::sendAck(unsigned int gamestateID){
144    packet::Acknowledgement *ack = new packet::Acknowledgement(gamestateID, 0);
145    if(!ack->send()){
146      COUT(3) << "could not ack gamestate: " << gamestateID << std::endl;
147      return false;
148    }
149    else{
[2087]150      COUT(5) << "acked a gamestate: " << gamestateID << std::endl;
[1769]151      return true;
152    }
153  }
[1707]154
[1751]155  packet::Gamestate *GamestateClient::processGamestate(packet::Gamestate *gs){
156    if(gs->isCompressed())
[1907]157    {
158      bool b = gs->decompressData();
159      assert(b);
160    }
[1751]161    if(gs->isDiffed()){
162      packet::Gamestate *base = gamestateMap_[gs->getBaseID()];
[1769]163      if(!base){
[2662]164        COUT(3) << "could not find base gamestate id: " << gs->getBaseID() << endl;
[1769]165        delete gs;
[1767]166        return 0;
[1769]167      }
[1907]168//       assert(base); //TODO: fix this
[1751]169      packet::Gamestate *undiffed = gs->undiff(base);
170      delete gs;
171      gs=undiffed;
[2087]172      COUT(5) << "successfully undiffed gamestate id: " << undiffed->getID() << std::endl;
[1751]173    }
174    if(gs->spreadData())
175      return gs;
176    else
[2662]177    {
178      COUT(3) << "could not spread gamestate" << endl;
[1751]179      return NULL;
[2662]180    }
[1712]181  }
[1707]182
183}
184
Note: See TracBrowser for help on using the repository browser.