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