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