[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 | |
---|
| 31 | #include <zlib.h> |
---|
| 32 | |
---|
| 33 | #include "core/CoreIncludes.h" |
---|
| 34 | #include "core/BaseObject.h" |
---|
[1758] | 35 | #include "core/Iterator.h" |
---|
[1707] | 36 | #include "Synchronisable.h" |
---|
[1769] | 37 | #include "packet/Acknowledgement.h" |
---|
[1707] | 38 | |
---|
| 39 | |
---|
| 40 | namespace network |
---|
| 41 | { |
---|
| 42 | struct GameStateItem{ |
---|
| 43 | packet::Gamestate *state; |
---|
| 44 | int id; |
---|
| 45 | }; |
---|
| 46 | |
---|
| 47 | GamestateClient::GamestateClient() { |
---|
| 48 | COUT(5) << "this: " << this << std::endl; |
---|
| 49 | last_diff_=0; |
---|
| 50 | last_gamestate_=GAMESTATEID_INITIAL-1; |
---|
| 51 | tempGamestate_=NULL; |
---|
| 52 | myShip_=NULL; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | GamestateClient::~GamestateClient() { |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | bool GamestateClient::ack(int gamestateID, int clientID){ |
---|
| 59 | return true; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | bool GamestateClient::add(packet::Gamestate *gs, int clientID){ |
---|
| 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; |
---|
[1707] | 76 | int id = GAMESTATEID_INITIAL; |
---|
| 77 | bool b = saveShipCache(); |
---|
[1751] | 78 | packet::Gamestate *processed = processGamestate(tempGamestate_); |
---|
[1769] | 79 | if(!processed){ |
---|
| 80 | if(b) |
---|
| 81 | loadShipCache(); |
---|
| 82 | return false; |
---|
| 83 | } |
---|
[1767] | 84 | // assert(processed); |
---|
[1751] | 85 | //successfully loaded data from gamestate. now save gamestate for diff and delete the old gs |
---|
[1769] | 86 | tempGamestate_=NULL; |
---|
[1751] | 87 | gamestateMap_[processed->getID()]=processed; |
---|
[1767] | 88 | last_diff_ = processed->getID(); |
---|
[1751] | 89 | if(b) |
---|
| 90 | loadShipCache(); |
---|
| 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 | */ |
---|
[1747] | 102 | void GamestateClient::removeObject(orxonox::ObjectList<Synchronisable>::iterator &it) { |
---|
| 103 | orxonox::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(); |
---|
[1767] | 110 | gs->collectData(0); |
---|
[1707] | 111 | return gs; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | void GamestateClient::cleanup(){ |
---|
| 115 | std::map<int, packet::Gamestate*>::iterator temp, it = gamestateMap_.begin(); |
---|
| 116 | while(it!=gamestateMap_.end()){ |
---|
| 117 | if(it->first>=last_diff_) |
---|
| 118 | break; |
---|
| 119 | // otherwise delete that stuff |
---|
| 120 | delete (*it).second; |
---|
| 121 | temp=it++; |
---|
| 122 | gamestateMap_.erase(temp); |
---|
| 123 | } |
---|
| 124 | tempGamestate_=NULL; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | void GamestateClient::printGamestateMap(){ |
---|
| 128 | std::map<int, packet::Gamestate*>::iterator it; |
---|
| 129 | COUT(4) << "gamestates: "; |
---|
| 130 | for(it=gamestateMap_.begin(); it!=gamestateMap_.end(); it++){ |
---|
| 131 | COUT(4) << it->first << ":" << it->second << "|"; |
---|
| 132 | } |
---|
| 133 | COUT(4) << std::endl; |
---|
| 134 | |
---|
| 135 | } |
---|
[1769] | 136 | |
---|
| 137 | bool GamestateClient::sendAck(unsigned int gamestateID){ |
---|
| 138 | packet::Acknowledgement *ack = new packet::Acknowledgement(gamestateID, 0); |
---|
| 139 | if(!ack->send()){ |
---|
| 140 | COUT(3) << "could not ack gamestate: " << gamestateID << std::endl; |
---|
| 141 | return false; |
---|
| 142 | } |
---|
| 143 | else{ |
---|
| 144 | COUT(3) << "acked a gamestate: " << gamestateID << std::endl; |
---|
| 145 | return true; |
---|
| 146 | } |
---|
| 147 | } |
---|
[1707] | 148 | |
---|
| 149 | bool GamestateClient::saveShipCache(){ |
---|
[1758] | 150 | if(myShip_==NULL){ |
---|
[1707] | 151 | myShip_ = orxonox::SpaceShip::getLocalShip(); |
---|
[1767] | 152 | // COUT(2) << "myShip_: " << myShip_ << " getLocalShip(): " << orxonox::SpaceShip::getLocalShip() << std::endl; |
---|
[1758] | 153 | if(!myShip_) |
---|
| 154 | return false; |
---|
| 155 | } |
---|
[1707] | 156 | if(myShip_){ |
---|
| 157 | // unsigned char *data = new unsigned char[myShip_->getSize()]; |
---|
[1751] | 158 | int size=myShip_->getSize2(0, 0x1); |
---|
[1707] | 159 | if(size==0) |
---|
| 160 | return false; |
---|
| 161 | shipCache_ = new unsigned char [size]; |
---|
| 162 | unsigned char *temp = shipCache_; |
---|
[1751] | 163 | if(!myShip_->getData(temp, 0, 0x1)) |
---|
[1707] | 164 | COUT(3) << "could not save shipCache" << std::endl; |
---|
| 165 | return true; |
---|
| 166 | }else |
---|
| 167 | return false; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | bool GamestateClient::loadShipCache(){ |
---|
[1767] | 171 | myShip_=orxonox::SpaceShip::getLocalShip(); //TODO: remove this (only a hack) |
---|
[1707] | 172 | if(myShip_ && shipCache_){ |
---|
[1767] | 173 | assert(myShip_->getIdentifier()); |
---|
[1707] | 174 | unsigned char *temp = shipCache_; |
---|
| 175 | myShip_->updateData(temp, 0x2); |
---|
| 176 | delete shipCache_; |
---|
| 177 | return true; |
---|
| 178 | }else |
---|
| 179 | return false; |
---|
| 180 | } |
---|
| 181 | |
---|
[1751] | 182 | packet::Gamestate *GamestateClient::processGamestate(packet::Gamestate *gs){ |
---|
| 183 | if(gs->isCompressed()) |
---|
| 184 | assert(gs->decompressData()); |
---|
| 185 | if(gs->isDiffed()){ |
---|
| 186 | packet::Gamestate *base = gamestateMap_[gs->getBaseID()]; |
---|
[1769] | 187 | if(!base){ |
---|
| 188 | delete gs; |
---|
[1767] | 189 | return 0; |
---|
[1769] | 190 | } |
---|
[1767] | 191 | // assert(base); //TODO: fix this |
---|
[1751] | 192 | packet::Gamestate *undiffed = gs->undiff(base); |
---|
| 193 | delete gs; |
---|
| 194 | gs=undiffed; |
---|
| 195 | COUT(3) << "successfully undiffed gamestate id: " << undiffed->getID() << std::endl; |
---|
| 196 | } |
---|
| 197 | if(gs->spreadData()) |
---|
| 198 | return gs; |
---|
| 199 | else |
---|
| 200 | return NULL; |
---|
[1712] | 201 | } |
---|
[1707] | 202 | |
---|
| 203 | } |
---|
| 204 | |
---|