Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutorial/src/libraries/network/GamestateClient.cc @ 9215

Last change on this file since 9215 was 7801, checked in by dafrick, 14 years ago

Merging presentation2 branch back to trunk.

  • Property svn:eol-style set to native
File size: 5.5 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 "util/Debug.h"
32#include "core/ObjectList.h"
33#include "synchronisable/Synchronisable.h"
34#include "synchronisable/NetworkCallbackManager.h"
35#include "packet/Acknowledgement.h"
36#include "packet/Gamestate.h"
37
38
39namespace orxonox
40{
41  struct _NetworkExport GameStateItem
42  {
43    packet::Gamestate *state;
44    unsigned int id;
45  };
46
47  GamestateClient::GamestateClient()
48  {
49    COUT(5) << "this: " << this << std::endl;
50    lastAckedGamestateID_=GAMESTATEID_INITIAL-1;
51    lastProcessedGamestateID_=GAMESTATEID_INITIAL-1;
52    tempGamestate_=NULL;
53  }
54
55  GamestateClient::~GamestateClient()
56  {
57      std::map<unsigned int, packet::Gamestate *>::iterator it;
58      for ( it = this->gamestateMap_.begin(); it != this->gamestateMap_.end(); ++it )
59          delete it->second;
60      if( this->tempGamestate_ )
61          delete this->tempGamestate_;
62  }
63
64  bool GamestateClient::ackGamestate(unsigned int gamestateID, unsigned int clientID)
65  {
66    return true;
67  }
68
69  bool GamestateClient::addGamestate(packet::Gamestate *gs, unsigned int clientID)
70  {
71    if(tempGamestate_!=NULL)
72    {
73      //delete the obsolete gamestate
74      if(tempGamestate_->getID()>gs->getID())
75        return false;
76      delete tempGamestate_;
77    }
78    tempGamestate_=gs;
79    return true;
80  }
81
82  bool GamestateClient::processGamestates()
83  {
84    if(tempGamestate_==NULL)
85      return false;
86    bool isDiffed = tempGamestate_->isDiffed();
87    int id = GAMESTATEID_INITIAL;
88    packet::Gamestate *processed = processGamestate(tempGamestate_);
89    assert(processed);
90
91    //now call the queued callbacks
92    NetworkCallbackManager::callCallbacks();
93
94    if (!processed)
95    {
96      assert(0);
97      sendAck(0);
98      return false;
99    }
100    //successfully loaded data from gamestate. now save gamestate for diff and delete the old gs
101    tempGamestate_=NULL;
102    gamestateMap_[processed->getID()]=processed;
103    lastProcessedGamestateID_ = processed->getID();
104    if(isDiffed)
105      lastAckedGamestateID_ = processed->getBaseID();
106    id = processed->getID();
107    sendAck(id);
108    return true;
109  }
110
111
112  /**
113  * This function removes a Synchronisable out of the universe
114  * @param it iterator of the list pointing to the object
115  * @return iterator pointing to the next object in the list
116  */
117  void GamestateClient::removeObject(ObjectListIterator<Synchronisable> &it)
118  {
119    ObjectListIterator<Synchronisable> temp=it;
120    ++it;
121    temp->destroy(); // or delete?
122  }
123
124  packet::Gamestate *GamestateClient::getGamestate()
125  {
126    packet::Gamestate *gs = new packet::Gamestate();
127    if(!gs->collectData(this->getLastProcessedGamestateID(NETWORK_PEER_ID_SERVER), 0x2))
128    {
129      delete gs;
130      return 0;
131    }
132    return gs;
133  }
134
135  void GamestateClient::cleanup()
136  {
137    std::map<unsigned int, packet::Gamestate*>::iterator temp, it = gamestateMap_.begin();
138    while(it!=gamestateMap_.end())
139    {
140      if(it->first>=lastAckedGamestateID_)
141        break;
142      // otherwise delete that stuff
143      delete it->second;
144      temp=it++;
145      gamestateMap_.erase(temp);
146    }
147    tempGamestate_=NULL;
148  }
149
150  void GamestateClient::printGamestateMap()
151  {
152    std::map<unsigned int, packet::Gamestate*>::iterator it;
153    COUT(4) << "gamestates: ";
154    for(it=gamestateMap_.begin(); it!=gamestateMap_.end(); it++)
155    {
156      COUT(4) << it->first << ':' << it->second << '|';
157    }
158    COUT(4) << std::endl;
159
160  }
161
162  bool GamestateClient::sendAck(unsigned int gamestateID)
163  {
164    packet::Acknowledgement *ack = new packet::Acknowledgement(gamestateID, 0);
165    if(!ack->send())
166    {
167      COUT(3) << "could not ack gamestate: " << gamestateID << std::endl;
168      return false;
169    }
170    else
171    {
172      COUT(5) << "acked a gamestate: " << gamestateID << std::endl;
173      return true;
174    }
175  }
176
177  packet::Gamestate *GamestateClient::processGamestate(packet::Gamestate *gs)
178  {
179    if(gs->isCompressed())
180    {
181      bool b = gs->decompressData();
182      assert(b);
183    }
184    if(gs->isDiffed())
185    {
186      assert(0);
187//       packet::Gamestate *base = gamestateMap_[gs->getBaseID()];
188//       if(!base)
189//       {
190//         COUT(0) << "could not find base gamestate id: " << gs->getBaseID() << endl;
191//         assert(0);
192//         delete gs;
193//         return 0;
194//       }
195//       packet::Gamestate *undiffed = gs->undiff(base);
196//       delete gs;
197//       gs=undiffed;
198//       COUT(5) << "successfully undiffed gamestate id: " << undiffed->getID() << std::endl;
199    }
200    if(gs->spreadData(0x2))
201      return gs;
202    else
203    {
204      COUT(0) << "could not spread gamestate" << endl;
205      assert(0);
206      return NULL;
207    }
208  }
209
210}
211
Note: See TracBrowser for help on using the repository browser.