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