Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/network/GamestateClient.cc @ 7904

Last change on this file since 7904 was 7801, checked in by dafrick, 13 years ago

Merging presentation2 branch back to trunk.

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