1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * |
---|
4 | * |
---|
5 | * License notice: |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
20 | * |
---|
21 | * Author: |
---|
22 | * Oliver Scheuss, (C) 2007 |
---|
23 | * Co-authors: |
---|
24 | * ... |
---|
25 | * |
---|
26 | */ |
---|
27 | |
---|
28 | // |
---|
29 | // C++ Implementation: GameStateManager |
---|
30 | // |
---|
31 | // Description: |
---|
32 | // |
---|
33 | // |
---|
34 | // Author: Oliver Scheuss, (C) 2007 |
---|
35 | // |
---|
36 | // Copyright: See COPYING file that comes with this distribution |
---|
37 | // |
---|
38 | // |
---|
39 | #include "GameStateManager.h" |
---|
40 | |
---|
41 | namespace network { |
---|
42 | |
---|
43 | GameStateManager::GameStateManager(ClientInformation *head) |
---|
44 | { |
---|
45 | id=0; |
---|
46 | head_=head; |
---|
47 | } |
---|
48 | |
---|
49 | GameStateManager::~GameStateManager() |
---|
50 | { |
---|
51 | } |
---|
52 | |
---|
53 | void GameStateManager::update(){ |
---|
54 | reference = getSnapshot(id); |
---|
55 | gameStateMap.insert(std::pair<int, GameState*>(id, reference)); |
---|
56 | gameStateUsed[id]=0; |
---|
57 | ++id; |
---|
58 | return; |
---|
59 | } |
---|
60 | |
---|
61 | GameStateCompressed GameStateManager::popGameState(int clientID){ |
---|
62 | int gID = head_->findClient(clientID)->getGamestateID(); |
---|
63 | if(gID!=GAMESTATEID_INITIAL){ |
---|
64 | GameState *client = gameStateMap[gID]; |
---|
65 | GameState *server = reference; |
---|
66 | return encode(client, server); |
---|
67 | } |
---|
68 | GameState *server = reference; |
---|
69 | return encode(server); |
---|
70 | // return an undiffed gamestate and set appropriate flags |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | |
---|
75 | /** |
---|
76 | * This function goes through the whole list of synchronisables and |
---|
77 | * saves all the synchronisables to a flat "list". |
---|
78 | * @return struct of type gamestate containing the size of the whole gamestate and a pointer linking to the flat list |
---|
79 | */ |
---|
80 | GameState *GameStateManager::getSnapshot(int id) |
---|
81 | { |
---|
82 | //the size of the gamestate |
---|
83 | int totalsize=0; |
---|
84 | int memsize=1000; |
---|
85 | //the size of one specific synchronisable |
---|
86 | int tempsize=0; |
---|
87 | // get the start of the Synchronisable list |
---|
88 | orxonox::Iterator<Synchronisable> it; |
---|
89 | // struct for return value of Synchronisable::getData() |
---|
90 | syncData sync; |
---|
91 | |
---|
92 | GameState *retval=new GameState; //return value |
---|
93 | retval->id=id++; |
---|
94 | // reserve a little memory and increase it later on |
---|
95 | COUT(2) << "mallocing" << std::endl; |
---|
96 | retval->data = (unsigned char*)malloc(memsize); |
---|
97 | COUT(2) << "malloced" << std::endl; |
---|
98 | |
---|
99 | // offset of memory functions |
---|
100 | int offset=0; |
---|
101 | // go through all Synchronisables |
---|
102 | for(it = orxonox::ObjectList<Synchronisable>::start(); it != 0; ++it){ |
---|
103 | //get size of the synchronisable |
---|
104 | tempsize=it->getSize(); |
---|
105 | //COUT(2) << "size of synchronisable: " << tempsize << std::endl; |
---|
106 | // add place for data and 3 ints (length,classid,objectid) |
---|
107 | totalsize+=tempsize+3*sizeof(int); |
---|
108 | // allocate additional space |
---|
109 | if(totalsize+tempsize>memsize){ |
---|
110 | retval->data = (unsigned char *)realloc((void *)retval->data, totalsize+1000); |
---|
111 | memsize+=1000; |
---|
112 | } |
---|
113 | |
---|
114 | // run Synchronisable::getData with offset and additional place for 3 ints in between (for ids and length) |
---|
115 | sync=it->getData(retval->data+offset+3*sizeof(int)); |
---|
116 | *(retval->data+offset)=sync.length; |
---|
117 | *(retval->data+offset+sizeof(int))=sync.objectID; |
---|
118 | *(retval->data+offset+2*sizeof(int))=sync.classID; |
---|
119 | // increase data pointer |
---|
120 | offset+=tempsize+3*sizeof(int); |
---|
121 | } |
---|
122 | retval->size=totalsize; |
---|
123 | return retval; |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | |
---|
128 | GameStateCompressed GameStateManager::encode(GameState *a, GameState *b){ |
---|
129 | GameState r = diff(a,b); |
---|
130 | r.diffed = true; |
---|
131 | return compress_(&r); |
---|
132 | } |
---|
133 | |
---|
134 | GameStateCompressed GameStateManager::encode(GameState *a){ |
---|
135 | a->diffed=false; |
---|
136 | return compress_(a); |
---|
137 | } |
---|
138 | |
---|
139 | GameState GameStateManager::diff(GameState *a, GameState *b){ |
---|
140 | unsigned char *ap = a->data, *bp = b->data; |
---|
141 | int of=0; // pointers offset |
---|
142 | int dest_length=0; |
---|
143 | if(a->size>=b->size) |
---|
144 | dest_length=a->size; |
---|
145 | else |
---|
146 | dest_length=b->size; |
---|
147 | unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char)); |
---|
148 | while(of<a->size && of<b->size){ |
---|
149 | *(dp+of)=*(ap+of)^*(bp+of); // do the xor |
---|
150 | ++of; |
---|
151 | } |
---|
152 | if(a->size!=b->size){ // do we have to fill up ? |
---|
153 | unsigned char n=0; |
---|
154 | if(a->size<b->size){ |
---|
155 | while(of<dest_length){ |
---|
156 | *(dp+of)=n^*(bp+of); |
---|
157 | of++; |
---|
158 | } |
---|
159 | } else{ |
---|
160 | while(of<dest_length){ |
---|
161 | *(dp+of)=*(ap+of)^n; |
---|
162 | of++; |
---|
163 | } |
---|
164 | } |
---|
165 | } |
---|
166 | // should be finished now |
---|
167 | GameState r = {b->id, dest_length, dp}; |
---|
168 | return r; |
---|
169 | } |
---|
170 | |
---|
171 | GameStateCompressed GameStateManager::compress_(GameState *a) { |
---|
172 | COUT(2) << "compressing gamestate" << std::endl; |
---|
173 | int size = a->size; |
---|
174 | uLongf buffer = (uLongf)((a->size + 12)*1.01)+1; |
---|
175 | unsigned char* dest = (unsigned char*)malloc( buffer ); |
---|
176 | int retval; |
---|
177 | retval = compress( dest, &buffer, a->data, (uLong)size ); |
---|
178 | |
---|
179 | switch ( retval ) { |
---|
180 | case Z_OK: std::cout << "successfully compressed" << std::endl; break; |
---|
181 | case Z_MEM_ERROR: std::cout << "not enough memory available" << std::endl; break; |
---|
182 | case Z_BUF_ERROR: std::cout << "not enough memory available in the buffer" << std::endl; break; |
---|
183 | case Z_DATA_ERROR: std::cout << "data corrupted" << std::endl; break; |
---|
184 | } |
---|
185 | |
---|
186 | GameStateCompressed compressedGamestate; |
---|
187 | compressedGamestate.compsize = buffer; |
---|
188 | compressedGamestate.normsize = size; |
---|
189 | compressedGamestate.id = GAMESTATE; |
---|
190 | compressedGamestate.data = dest; |
---|
191 | compressedGamestate.diffed = a->diffed; |
---|
192 | |
---|
193 | return compressedGamestate; |
---|
194 | } |
---|
195 | |
---|
196 | void GameStateManager::ackGameState(int clientID, int gamestateID){ |
---|
197 | ClientInformation *temp = head_->findClient(clientID); |
---|
198 | int curid = temp->getID(); |
---|
199 | // decrease usage of gamestate and save it |
---|
200 | deleteUnusedGameState(curid); |
---|
201 | //increase gamestateused |
---|
202 | ++gameStateUsed.find(gamestateID)->second; |
---|
203 | temp->setGamestateID(gamestateID); |
---|
204 | /* |
---|
205 | GameState *old = clientGameState[clientID]; |
---|
206 | deleteUnusedGameState(old); |
---|
207 | clientGameState[clientID]=idGameState[gamestateID];*/ |
---|
208 | } |
---|
209 | |
---|
210 | bool GameStateManager::deleteUnusedGameState(int gamestateID){ |
---|
211 | int used = --(gameStateUsed.find(gamestateID)->second); |
---|
212 | if(id-gamestateID>KEEP_GAMESTATES && used==0){ |
---|
213 | // delete gamestate |
---|
214 | delete gameStateMap.find(gamestateID)->second; |
---|
215 | gameStateMap.erase(gamestateID); |
---|
216 | return true; |
---|
217 | } |
---|
218 | return false; |
---|
219 | } |
---|
220 | |
---|
221 | } |
---|
222 | |
---|
223 | |
---|