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, (C) 2007 |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | // |
---|
30 | // C++ Implementation: GameStateManager |
---|
31 | // |
---|
32 | // Description: |
---|
33 | // |
---|
34 | // |
---|
35 | // Author: Oliver Scheuss, (C) 2007 |
---|
36 | // |
---|
37 | // Copyright: See COPYING file that comes with this distribution |
---|
38 | // |
---|
39 | // |
---|
40 | |
---|
41 | #include "GamestateManager.h" |
---|
42 | |
---|
43 | #include <utility> |
---|
44 | #include <iostream> |
---|
45 | #include <zlib.h> |
---|
46 | #include <cassert> |
---|
47 | |
---|
48 | #include "core/CoreIncludes.h" |
---|
49 | #include "core/BaseObject.h" |
---|
50 | #include "ClientInformation.h" |
---|
51 | #include "synchronisable/Synchronisable.h" |
---|
52 | #include "synchronisable/NetworkCallbackManager.h" |
---|
53 | |
---|
54 | namespace orxonox |
---|
55 | { |
---|
56 | GamestateManager::GamestateManager() { |
---|
57 | id_=0; |
---|
58 | } |
---|
59 | |
---|
60 | GamestateManager::~GamestateManager() { |
---|
61 | } |
---|
62 | |
---|
63 | bool GamestateManager::update(){ |
---|
64 | // cleanup(); |
---|
65 | return getSnapshot(); |
---|
66 | } |
---|
67 | |
---|
68 | bool GamestateManager::add(packet::Gamestate *gs, unsigned int clientID){ |
---|
69 | assert(gs); |
---|
70 | std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateQueue.find(clientID); |
---|
71 | if(it!=gamestateQueue.end()){ |
---|
72 | // delete obsolete gamestate |
---|
73 | delete it->second; |
---|
74 | } |
---|
75 | gamestateQueue[clientID] = gs; |
---|
76 | return true; |
---|
77 | } |
---|
78 | |
---|
79 | bool GamestateManager::processGamestates(){ |
---|
80 | std::map<unsigned int, packet::Gamestate*>::iterator it; |
---|
81 | // now push only the most recent gamestates we received (ignore obsolete ones) |
---|
82 | for(it = gamestateQueue.begin(); it!=gamestateQueue.end(); it++){ |
---|
83 | bool b = processGamestate(it->second); |
---|
84 | assert(b); |
---|
85 | delete it->second; |
---|
86 | } |
---|
87 | // now clear the queue |
---|
88 | gamestateQueue.clear(); |
---|
89 | //and call all queued callbacks |
---|
90 | NetworkCallbackManager::callCallbacks(); |
---|
91 | return true; |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | bool GamestateManager::getSnapshot(){ |
---|
96 | reference = new packet::Gamestate(); |
---|
97 | if(!reference->collectData(++id_)){ //we have no data to send |
---|
98 | delete reference; |
---|
99 | reference=0; |
---|
100 | } |
---|
101 | return true; |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | * this function is used to keep the memory usage low |
---|
106 | * it tries to delete all the unused gamestates |
---|
107 | * |
---|
108 | * |
---|
109 | */ |
---|
110 | /* void GamestateManager::cleanup(){ |
---|
111 | std::map<int,int>::iterator it = gamestateUsed.begin(); |
---|
112 | while(it!=gamestateUsed.end()){ |
---|
113 | if((id_-(*it).first)<KEEP_GAMESTATES) |
---|
114 | break; |
---|
115 | if( (*it).second <= 0 ){ |
---|
116 | COUT(5) << "GameStateManager: deleting gamestate with id: " << (*it).first << ", uses: " << (*it).second << std::endl; |
---|
117 | std::map<int, packet::Gamestate *>::iterator tempit = gamestateMap.find((*it).first); |
---|
118 | if( tempit != gamestateMap.end() ){ |
---|
119 | packet::Gamestate *temp = tempit->second; |
---|
120 | if(temp){ |
---|
121 | delete gamestateMap[(*it).first]; |
---|
122 | gamestateMap.erase((*it).first); |
---|
123 | } |
---|
124 | } |
---|
125 | gamestateUsed.erase(it++); |
---|
126 | continue; |
---|
127 | } |
---|
128 | it++; |
---|
129 | } |
---|
130 | }*/ |
---|
131 | |
---|
132 | packet::Gamestate *GamestateManager::popGameState(unsigned int clientID) { |
---|
133 | //why are we searching the same client's gamestate id as we searched in |
---|
134 | //Server::sendGameState? |
---|
135 | packet::Gamestate *gs; |
---|
136 | unsigned int gID = ClientInformation::findClient(clientID)->getGamestateID(); |
---|
137 | if(!reference) |
---|
138 | return 0; |
---|
139 | gs = reference->doSelection(clientID, 10000); |
---|
140 | // gs = new packet::Gamestate(*reference); |
---|
141 | // save the (undiffed) gamestate in the clients gamestate map |
---|
142 | gamestateMap_[clientID].insert(std::pair<int, packet::Gamestate*>(gs->getID(), gs)); |
---|
143 | //chose wheather the next gamestate is the first or not |
---|
144 | packet::Gamestate *client=NULL; |
---|
145 | if(gID != GAMESTATEID_INITIAL){ |
---|
146 | std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(clientID); |
---|
147 | if(clientMap!=gamestateMap_.end()){ |
---|
148 | std::map<unsigned int, packet::Gamestate*>::iterator it = clientMap->second.find(gID); |
---|
149 | if(it!=clientMap->second.end()) |
---|
150 | client = it->second; |
---|
151 | } |
---|
152 | } |
---|
153 | if(client){ |
---|
154 | // COUT(3) << "diffing" << std::endl; |
---|
155 | gs = gs->diff(client); |
---|
156 | } |
---|
157 | else{ |
---|
158 | // COUT(3) << "not diffing" << std::endl; |
---|
159 | gs = new packet::Gamestate(*gs); |
---|
160 | } |
---|
161 | bool b = gs->compressData(); |
---|
162 | assert(b); |
---|
163 | return gs; |
---|
164 | } |
---|
165 | |
---|
166 | |
---|
167 | bool GamestateManager::ack(unsigned int gamestateID, unsigned int clientID) { |
---|
168 | ClientInformation *temp = ClientInformation::findClient(clientID); |
---|
169 | assert(temp); |
---|
170 | unsigned int curid = temp->getGamestateID(); |
---|
171 | |
---|
172 | if(gamestateID == 0){ |
---|
173 | temp->setGamestateID(GAMESTATEID_INITIAL); |
---|
174 | return true; |
---|
175 | } |
---|
176 | |
---|
177 | assert(curid==(unsigned int)GAMESTATEID_INITIAL || curid<gamestateID); |
---|
178 | COUT(4) << "acking gamestate " << gamestateID << " for clientid: " << clientID << " curid: " << curid << std::endl; |
---|
179 | std::map<unsigned int, packet::Gamestate*>::iterator it, tempit; |
---|
180 | for(it = gamestateMap_[clientID].begin(); it!=gamestateMap_[clientID].end() && it->first<gamestateID; it++){ |
---|
181 | delete it->second; |
---|
182 | tempit=it++; |
---|
183 | gamestateMap_[clientID].erase(tempit); |
---|
184 | } |
---|
185 | temp->setGamestateID(gamestateID); |
---|
186 | TrafficControl::processAck(clientID, gamestateID); |
---|
187 | return true; |
---|
188 | } |
---|
189 | |
---|
190 | void GamestateManager::removeClient(ClientInformation* client){ |
---|
191 | assert(client); |
---|
192 | std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(client->getID()); |
---|
193 | // first delete all remained gamestates |
---|
194 | std::map<unsigned int, packet::Gamestate*>::iterator it; |
---|
195 | for(it=clientMap->second.begin(); it!=clientMap->second.end(); it++) |
---|
196 | delete it->second; |
---|
197 | // now delete the clients gamestatemap |
---|
198 | gamestateMap_.erase(clientMap); |
---|
199 | } |
---|
200 | |
---|
201 | bool GamestateManager::processGamestate(packet::Gamestate *gs){ |
---|
202 | if(gs->isCompressed()) |
---|
203 | { |
---|
204 | bool b = gs->decompressData(); |
---|
205 | assert(b); |
---|
206 | } |
---|
207 | assert(!gs->isDiffed()); |
---|
208 | return gs->spreadData(); |
---|
209 | } |
---|
210 | |
---|
211 | } |
---|