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