[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] | 39 | namespace orxonox |
---|
[1707] | 40 | { |
---|
[2087] | 41 | struct _NetworkExport GameStateItem{ |
---|
[1707] | 42 | packet::Gamestate *state; |
---|
[2087] | 43 | unsigned int id; |
---|
[1707] | 44 | }; |
---|
| 45 | |
---|
| 46 | GamestateClient::GamestateClient() { |
---|
| 47 | COUT(5) << "this: " << this << std::endl; |
---|
| 48 | last_diff_=0; |
---|
| 49 | last_gamestate_=GAMESTATEID_INITIAL-1; |
---|
| 50 | tempGamestate_=NULL; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | GamestateClient::~GamestateClient() { |
---|
| 54 | } |
---|
| 55 | |
---|
[2087] | 56 | bool GamestateClient::ack(unsigned int gamestateID, unsigned int clientID){ |
---|
[1707] | 57 | return true; |
---|
| 58 | } |
---|
| 59 | |
---|
[2087] | 60 | bool GamestateClient::add(packet::Gamestate *gs, unsigned int clientID){ |
---|
[1707] | 61 | if(tempGamestate_!=NULL){ |
---|
| 62 | //delete the obsolete gamestate |
---|
| 63 | if(tempGamestate_->getID()>gs->getID()) |
---|
| 64 | return false; |
---|
| 65 | delete tempGamestate_; |
---|
| 66 | } |
---|
| 67 | tempGamestate_=gs; |
---|
| 68 | return true; |
---|
| 69 | } |
---|
[1747] | 70 | |
---|
[1769] | 71 | bool GamestateClient::processGamestates(){ |
---|
[1707] | 72 | if(tempGamestate_==NULL) |
---|
[1769] | 73 | return false; |
---|
[2662] | 74 | bool isDiffed = tempGamestate_->isDiffed(); |
---|
[1707] | 75 | int id = GAMESTATEID_INITIAL; |
---|
[1751] | 76 | packet::Gamestate *processed = processGamestate(tempGamestate_); |
---|
[2662] | 77 | assert(processed); |
---|
| 78 | |
---|
| 79 | //now call the queued callbacks |
---|
| 80 | NetworkCallbackManager::callCallbacks(); |
---|
| 81 | |
---|
| 82 | if (!processed){ |
---|
| 83 | sendAck(0); |
---|
| 84 | return false; |
---|
| 85 | } |
---|
[1751] | 86 | //successfully loaded data from gamestate. now save gamestate for diff and delete the old gs |
---|
[1769] | 87 | tempGamestate_=NULL; |
---|
[1751] | 88 | gamestateMap_[processed->getID()]=processed; |
---|
[2662] | 89 | if(isDiffed) |
---|
| 90 | last_diff_ = processed->getBaseID(); |
---|
[1751] | 91 | id = processed->getID(); |
---|
[1769] | 92 | sendAck(id); |
---|
| 93 | return true; |
---|
[1707] | 94 | } |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | /** |
---|
| 98 | * This function removes a Synchronisable out of the universe |
---|
| 99 | * @param it iterator of the list pointing to the object |
---|
| 100 | * @return iterator pointing to the next object in the list |
---|
| 101 | */ |
---|
[2171] | 102 | void GamestateClient::removeObject(ObjectList<Synchronisable>::iterator &it) { |
---|
| 103 | ObjectList<Synchronisable>::iterator temp=it; |
---|
[1707] | 104 | ++it; |
---|
| 105 | delete *temp; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | packet::Gamestate *GamestateClient::getGamestate(){ |
---|
| 109 | packet::Gamestate *gs = new packet::Gamestate(); |
---|
[3102] | 110 | if(!gs->collectData(0,0x2)){ |
---|
[2087] | 111 | delete gs; |
---|
| 112 | return 0; |
---|
| 113 | } |
---|
[1707] | 114 | return gs; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | void GamestateClient::cleanup(){ |
---|
[2087] | 118 | std::map<unsigned int, packet::Gamestate*>::iterator temp, it = gamestateMap_.begin(); |
---|
[1707] | 119 | while(it!=gamestateMap_.end()){ |
---|
| 120 | if(it->first>=last_diff_) |
---|
| 121 | break; |
---|
| 122 | // otherwise delete that stuff |
---|
| 123 | delete (*it).second; |
---|
| 124 | temp=it++; |
---|
| 125 | gamestateMap_.erase(temp); |
---|
| 126 | } |
---|
| 127 | tempGamestate_=NULL; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | void GamestateClient::printGamestateMap(){ |
---|
[2087] | 131 | std::map<unsigned int, packet::Gamestate*>::iterator it; |
---|
[1707] | 132 | COUT(4) << "gamestates: "; |
---|
| 133 | for(it=gamestateMap_.begin(); it!=gamestateMap_.end(); it++){ |
---|
| 134 | COUT(4) << it->first << ":" << it->second << "|"; |
---|
| 135 | } |
---|
| 136 | COUT(4) << std::endl; |
---|
| 137 | |
---|
| 138 | } |
---|
[2087] | 139 | |
---|
[1769] | 140 | bool GamestateClient::sendAck(unsigned int gamestateID){ |
---|
| 141 | packet::Acknowledgement *ack = new packet::Acknowledgement(gamestateID, 0); |
---|
| 142 | if(!ack->send()){ |
---|
| 143 | COUT(3) << "could not ack gamestate: " << gamestateID << std::endl; |
---|
| 144 | return false; |
---|
| 145 | } |
---|
| 146 | else{ |
---|
[2087] | 147 | COUT(5) << "acked a gamestate: " << gamestateID << std::endl; |
---|
[1769] | 148 | return true; |
---|
| 149 | } |
---|
| 150 | } |
---|
[1707] | 151 | |
---|
[1751] | 152 | packet::Gamestate *GamestateClient::processGamestate(packet::Gamestate *gs){ |
---|
| 153 | if(gs->isCompressed()) |
---|
[1907] | 154 | { |
---|
| 155 | bool b = gs->decompressData(); |
---|
| 156 | assert(b); |
---|
| 157 | } |
---|
[1751] | 158 | if(gs->isDiffed()){ |
---|
| 159 | packet::Gamestate *base = gamestateMap_[gs->getBaseID()]; |
---|
[1769] | 160 | if(!base){ |
---|
[2662] | 161 | COUT(3) << "could not find base gamestate id: " << gs->getBaseID() << endl; |
---|
[1769] | 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 | } |
---|
[3102] | 171 | if(gs->spreadData(0x2)) |
---|
[1751] | 172 | return gs; |
---|
| 173 | else |
---|
[2662] | 174 | { |
---|
| 175 | COUT(3) << "could not spread gamestate" << endl; |
---|
[1751] | 176 | return NULL; |
---|
[2662] | 177 | } |
---|
[1712] | 178 | } |
---|
[1707] | 179 | |
---|
| 180 | } |
---|
| 181 | |
---|