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