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 | * ... |
---|
23 | * Co-authors: |
---|
24 | * ... |
---|
25 | * |
---|
26 | */ |
---|
27 | |
---|
28 | #include "GameStateClient.h" |
---|
29 | |
---|
30 | namespace network { |
---|
31 | |
---|
32 | GameStateClient::GameStateClient() |
---|
33 | { |
---|
34 | } |
---|
35 | |
---|
36 | |
---|
37 | GameStateClient::~GameStateClient() |
---|
38 | { |
---|
39 | } |
---|
40 | |
---|
41 | bool GameStateClient::pushGameState(GameStateCompressed *compstate){ |
---|
42 | if(compstate->diffed) |
---|
43 | return loadSnapshot(decode(reference, *compstate)); |
---|
44 | else |
---|
45 | return loadSnapshot(decode(*compstate)); |
---|
46 | } |
---|
47 | |
---|
48 | |
---|
49 | /** |
---|
50 | * This function removes a Synchronisable out of the universe |
---|
51 | * @param it iterator of the list pointing to the object |
---|
52 | * @return iterator pointing to the next object in the list |
---|
53 | */ |
---|
54 | void GameStateClient::removeObject(orxonox::Iterator<Synchronisable> &it){ |
---|
55 | orxonox::Iterator<Synchronisable> temp=it; |
---|
56 | ++it; |
---|
57 | delete *temp; |
---|
58 | } |
---|
59 | |
---|
60 | /** |
---|
61 | * This function loads a Snapshort of the gamestate into the universe |
---|
62 | * @param state a GameState struct containing the size of the gamestate and a pointer linking to a flat list (returned by getSnapshot) |
---|
63 | */ |
---|
64 | bool GameStateClient::loadSnapshot(GameState state) |
---|
65 | { |
---|
66 | unsigned char *data=state.data; |
---|
67 | std::cout << "loadSnapshot: loading gs: " << state.id << std::endl; |
---|
68 | // get the start of the Synchronisable list |
---|
69 | orxonox::Iterator<Synchronisable> it=orxonox::ObjectList<Synchronisable>::start(); |
---|
70 | syncData sync; |
---|
71 | // loop as long as we have some data ;) |
---|
72 | while(data < state.data+state.size){ |
---|
73 | // prepare the syncData struct |
---|
74 | sync.length = (int)*data; |
---|
75 | data+=sizeof(int); |
---|
76 | sync.objectID = (int)*data; |
---|
77 | data+=sizeof(int); |
---|
78 | sync.classID = (int)*data; |
---|
79 | data+=sizeof(int); |
---|
80 | sync.data = data; |
---|
81 | data+=sync.length; |
---|
82 | |
---|
83 | if(it->objectID!=sync.objectID){ |
---|
84 | // bad luck ;) |
---|
85 | // delete the synchronisable (obviously seems to be deleted on the server) |
---|
86 | while(it != 0 && it->objectID!=sync.objectID){ |
---|
87 | removeObject(it); |
---|
88 | } |
---|
89 | if(it==0){ |
---|
90 | orxonox::BaseObject *no = ID(sync.classID)->fabricate(); |
---|
91 | ((Synchronisable *)no)->objectID=sync.objectID; |
---|
92 | ((Synchronisable *)no)->classID=sync.classID; |
---|
93 | it=orxonox::ObjectList<Synchronisable>::end(); |
---|
94 | // update data and create object/entity... |
---|
95 | if( !(((Synchronisable *)no)->updateData(sync)) && !(((Synchronisable *)no)->create()) ) |
---|
96 | COUT(0) << "We couldn't create/update the object: " << sync.objectID << std::endl; |
---|
97 | ++it; |
---|
98 | } |
---|
99 | } else { |
---|
100 | // we have our object |
---|
101 | if(! it->updateData(sync)) |
---|
102 | std::cout << "We couldn't update objectID: " \ |
---|
103 | << sync.objectID << "; classID: " << sync.classID << std::endl; |
---|
104 | } |
---|
105 | ++it; |
---|
106 | } |
---|
107 | |
---|
108 | return true; |
---|
109 | } |
---|
110 | |
---|
111 | GameState GameStateClient::diff(GameState a, GameState b){ |
---|
112 | unsigned char *ap = a.data, *bp = b.data; |
---|
113 | int of=0; // pointers offset |
---|
114 | int dest_length=0; |
---|
115 | if(a.size>=b.size) |
---|
116 | dest_length=a.size; |
---|
117 | else |
---|
118 | dest_length=b.size; |
---|
119 | unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char)); |
---|
120 | while(of<a.size && of<b.size){ |
---|
121 | *(dp+of)=*(ap+of)^*(bp+of); // do the xor |
---|
122 | ++of; |
---|
123 | } |
---|
124 | if(a.size!=b.size){ // do we have to fill up ? |
---|
125 | unsigned char n=0; |
---|
126 | if(a.size<b.size){ |
---|
127 | while(of<dest_length){ |
---|
128 | *(dp+of)=n^*(bp+of); |
---|
129 | of++; |
---|
130 | } |
---|
131 | } else{ |
---|
132 | while(of<dest_length){ |
---|
133 | *(dp+of)=*(ap+of)^n; |
---|
134 | of++; |
---|
135 | } |
---|
136 | } |
---|
137 | } |
---|
138 | // should be finished now |
---|
139 | GameState r = {b.id, dest_length, dp}; |
---|
140 | return r; |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | GameState GameStateClient::decompress(GameStateCompressed a){ |
---|
145 | int normsize = a.normsize; |
---|
146 | int compsize = a.compsize; |
---|
147 | unsigned char* dest = (unsigned char*)malloc( normsize ); |
---|
148 | int retval; |
---|
149 | uLongf length=normsize; |
---|
150 | retval = uncompress( dest, &length, a.data, (uLong)compsize ); |
---|
151 | |
---|
152 | switch ( retval ) { |
---|
153 | case Z_OK: std::cout << "successfully decompressed" << std::endl; break; |
---|
154 | case Z_MEM_ERROR: std::cout << "not enough memory available" << std::endl; break; |
---|
155 | case Z_BUF_ERROR: std::cout << "not enough memory available in the buffer" << std::endl; break; |
---|
156 | case Z_DATA_ERROR: std::cout << "data corrupted" << std::endl; break; |
---|
157 | } |
---|
158 | |
---|
159 | GameState gamestate; |
---|
160 | gamestate.id = a.id; |
---|
161 | gamestate.size = normsize; |
---|
162 | gamestate.data = dest; |
---|
163 | gamestate.diffed = a.diffed; |
---|
164 | |
---|
165 | return gamestate; |
---|
166 | } |
---|
167 | |
---|
168 | |
---|
169 | GameState GameStateClient::decode(GameState a, GameStateCompressed x){ |
---|
170 | GameState t = decompress(x); |
---|
171 | return diff(a, t); |
---|
172 | } |
---|
173 | |
---|
174 | GameState GameStateClient::decode(GameStateCompressed x){ |
---|
175 | GameState t = decompress(x); |
---|
176 | return t; |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | |
---|
181 | } |
---|