[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" |
---|
[1747] | 35 | #include "core/Iterator.h" |
---|
[1707] | 36 | #include "Synchronisable.h" |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | namespace network |
---|
| 40 | { |
---|
| 41 | struct GameStateItem{ |
---|
| 42 | packet::Gamestate *state; |
---|
| 43 | int id; |
---|
| 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 | myShip_=NULL; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | GamestateClient::~GamestateClient() { |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | bool GamestateClient::ack(int gamestateID, int clientID){ |
---|
| 58 | return true; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | bool GamestateClient::add(packet::Gamestate *gs, int clientID){ |
---|
| 62 | if(tempGamestate_!=NULL){ |
---|
| 63 | //delete the obsolete gamestate |
---|
| 64 | if(tempGamestate_->getID()>gs->getID()) |
---|
| 65 | return false; |
---|
| 66 | delete tempGamestate_; |
---|
| 67 | } |
---|
| 68 | tempGamestate_=gs; |
---|
| 69 | return true; |
---|
| 70 | } |
---|
[1747] | 71 | |
---|
[1707] | 72 | int GamestateClient::processGamestates(){ |
---|
| 73 | if(tempGamestate_==NULL) |
---|
| 74 | return 0; |
---|
| 75 | int id = GAMESTATEID_INITIAL; |
---|
| 76 | bool b = saveShipCache(); |
---|
[1712] | 77 | if(processGamestate(tempGamestate_)){ |
---|
[1707] | 78 | if(b) |
---|
| 79 | loadShipCache(); |
---|
| 80 | id = tempGamestate_->getID(); |
---|
| 81 | } |
---|
| 82 | cleanup(); |
---|
| 83 | return id; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | /** |
---|
| 88 | * This function removes a Synchronisable out of the universe |
---|
| 89 | * @param it iterator of the list pointing to the object |
---|
| 90 | * @return iterator pointing to the next object in the list |
---|
| 91 | */ |
---|
[1747] | 92 | void GamestateClient::removeObject(orxonox::ObjectList<Synchronisable>::iterator &it) { |
---|
| 93 | orxonox::ObjectList<Synchronisable>::iterator temp=it; |
---|
[1707] | 94 | ++it; |
---|
| 95 | delete *temp; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | packet::Gamestate *GamestateClient::getGamestate(){ |
---|
| 99 | packet::Gamestate *gs = new packet::Gamestate(); |
---|
| 100 | gs->collectData(GAMESTATEID_INITIAL); |
---|
| 101 | return gs; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | void GamestateClient::cleanup(){ |
---|
| 105 | std::map<int, packet::Gamestate*>::iterator temp, it = gamestateMap_.begin(); |
---|
| 106 | while(it!=gamestateMap_.end()){ |
---|
| 107 | if(it->first>=last_diff_) |
---|
| 108 | break; |
---|
| 109 | // otherwise delete that stuff |
---|
| 110 | delete (*it).second; |
---|
| 111 | temp=it++; |
---|
| 112 | gamestateMap_.erase(temp); |
---|
| 113 | } |
---|
| 114 | tempGamestate_=NULL; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | void GamestateClient::printGamestateMap(){ |
---|
| 118 | std::map<int, packet::Gamestate*>::iterator it; |
---|
| 119 | COUT(4) << "gamestates: "; |
---|
| 120 | for(it=gamestateMap_.begin(); it!=gamestateMap_.end(); it++){ |
---|
| 121 | COUT(4) << it->first << ":" << it->second << "|"; |
---|
| 122 | } |
---|
| 123 | COUT(4) << std::endl; |
---|
| 124 | |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | bool GamestateClient::saveShipCache(){ |
---|
| 128 | if(myShip_==NULL) |
---|
| 129 | myShip_ = orxonox::SpaceShip::getLocalShip(); |
---|
| 130 | if(myShip_){ |
---|
| 131 | // unsigned char *data = new unsigned char[myShip_->getSize()]; |
---|
| 132 | int size=myShip_->getSize(0x1); |
---|
| 133 | if(size==0) |
---|
| 134 | return false; |
---|
| 135 | shipCache_ = new unsigned char [size]; |
---|
| 136 | unsigned char *temp = shipCache_; |
---|
| 137 | if(!myShip_->getData2(temp, 0x1)) |
---|
| 138 | COUT(3) << "could not save shipCache" << std::endl; |
---|
| 139 | return true; |
---|
| 140 | }else |
---|
| 141 | return false; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | bool GamestateClient::loadShipCache(){ |
---|
| 145 | if(myShip_ && shipCache_){ |
---|
| 146 | unsigned char *temp = shipCache_; |
---|
| 147 | myShip_->updateData(temp, 0x2); |
---|
| 148 | delete shipCache_; |
---|
| 149 | return true; |
---|
| 150 | }else |
---|
| 151 | return false; |
---|
| 152 | } |
---|
| 153 | |
---|
[1712] | 154 | bool GamestateClient::processGamestate(packet::Gamestate *gs){ |
---|
| 155 | assert(gs->decompressData()); |
---|
| 156 | if(gs->isDiffed()) |
---|
| 157 | assert(gs->undiff(gamestateMap_[gs->getBaseID()])); |
---|
| 158 | return gs->spreadData(); |
---|
| 159 | } |
---|
[1707] | 160 | |
---|
| 161 | } |
---|
| 162 | |
---|