[514] | 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 | |
---|
[416] | 28 | #include "GameStateClient.h" |
---|
| 29 | |
---|
| 30 | namespace network { |
---|
| 31 | |
---|
| 32 | GameStateClient::GameStateClient() |
---|
| 33 | { |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | GameStateClient::~GameStateClient() |
---|
| 38 | { |
---|
| 39 | } |
---|
| 40 | |
---|
[620] | 41 | bool GameStateClient::pushGameState(GameStateCompressed *compstate){ |
---|
| 42 | if(compstate->diffed) |
---|
| 43 | return loadSnapshot(decode(reference, *compstate)); |
---|
[436] | 44 | else |
---|
[620] | 45 | return loadSnapshot(decode(*compstate)); |
---|
[416] | 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; |
---|
[620] | 67 | std::cout << "loadSnapshot: loading gs: " << state.id << std::endl; |
---|
[416] | 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 |
---|
[620] | 74 | sync.length = (int)*data; |
---|
[416] | 75 | data+=sizeof(int); |
---|
[620] | 76 | sync.objectID = (int)*data; |
---|
[416] | 77 | data+=sizeof(int); |
---|
[620] | 78 | sync.classID = (int)*data; |
---|
[416] | 79 | data+=sizeof(int); |
---|
| 80 | sync.data = data; |
---|
| 81 | data+=sync.length; |
---|
[514] | 82 | |
---|
[416] | 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 | } |
---|
[505] | 89 | if(it==0){ |
---|
[636] | 90 | std::cout << "classid: " << sync.classID << ", name: " << ID(sync.classID)->getName() << std::endl; |
---|
[505] | 91 | orxonox::BaseObject *no = ID(sync.classID)->fabricate(); |
---|
| 92 | ((Synchronisable *)no)->objectID=sync.objectID; |
---|
| 93 | ((Synchronisable *)no)->classID=sync.classID; |
---|
[416] | 94 | it=orxonox::ObjectList<Synchronisable>::end(); |
---|
[567] | 95 | // update data and create object/entity... |
---|
| 96 | if( !(((Synchronisable *)no)->updateData(sync)) && !(((Synchronisable *)no)->create()) ) |
---|
| 97 | COUT(0) << "We couldn't create/update the object: " << sync.objectID << std::endl; |
---|
| 98 | ++it; |
---|
[416] | 99 | } |
---|
| 100 | } else { |
---|
| 101 | // we have our object |
---|
| 102 | if(! it->updateData(sync)) |
---|
| 103 | std::cout << "We couldn't update objectID: " \ |
---|
| 104 | << sync.objectID << "; classID: " << sync.classID << std::endl; |
---|
| 105 | } |
---|
[505] | 106 | ++it; |
---|
[416] | 107 | } |
---|
[514] | 108 | |
---|
[416] | 109 | return true; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | GameState GameStateClient::diff(GameState a, GameState b){ |
---|
| 113 | unsigned char *ap = a.data, *bp = b.data; |
---|
| 114 | int of=0; // pointers offset |
---|
| 115 | int dest_length=0; |
---|
| 116 | if(a.size>=b.size) |
---|
| 117 | dest_length=a.size; |
---|
| 118 | else |
---|
| 119 | dest_length=b.size; |
---|
| 120 | unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char)); |
---|
| 121 | while(of<a.size && of<b.size){ |
---|
| 122 | *(dp+of)=*(ap+of)^*(bp+of); // do the xor |
---|
| 123 | ++of; |
---|
| 124 | } |
---|
| 125 | if(a.size!=b.size){ // do we have to fill up ? |
---|
| 126 | unsigned char n=0; |
---|
| 127 | if(a.size<b.size){ |
---|
| 128 | while(of<dest_length){ |
---|
| 129 | *(dp+of)=n^*(bp+of); |
---|
| 130 | of++; |
---|
| 131 | } |
---|
| 132 | } else{ |
---|
| 133 | while(of<dest_length){ |
---|
| 134 | *(dp+of)=*(ap+of)^n; |
---|
| 135 | of++; |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | // should be finished now |
---|
| 140 | GameState r = {b.id, dest_length, dp}; |
---|
| 141 | return r; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | GameState GameStateClient::decompress(GameStateCompressed a){ |
---|
| 146 | int normsize = a.normsize; |
---|
| 147 | int compsize = a.compsize; |
---|
[624] | 148 | int bufsize; |
---|
| 149 | if(normsize < compsize) |
---|
| 150 | bufsize = compsize; |
---|
| 151 | else |
---|
| 152 | bufsize = normsize; |
---|
| 153 | unsigned char* dest = (unsigned char*)malloc( bufsize ); |
---|
[416] | 154 | int retval; |
---|
| 155 | uLongf length=normsize; |
---|
[632] | 156 | //std::cout << "gamestateclient" << std::endl; |
---|
| 157 | //std::cout << "normsize " << a.normsize << " compsize " << a.compsize << " " << bufsize << std::endl; |
---|
[416] | 158 | retval = uncompress( dest, &length, a.data, (uLong)compsize ); |
---|
[632] | 159 | //std::cout << "length " << length << std::endl; |
---|
[416] | 160 | switch ( retval ) { |
---|
[620] | 161 | case Z_OK: std::cout << "successfully decompressed" << std::endl; break; |
---|
[416] | 162 | case Z_MEM_ERROR: std::cout << "not enough memory available" << std::endl; break; |
---|
| 163 | case Z_BUF_ERROR: std::cout << "not enough memory available in the buffer" << std::endl; break; |
---|
| 164 | case Z_DATA_ERROR: std::cout << "data corrupted" << std::endl; break; |
---|
| 165 | } |
---|
[514] | 166 | |
---|
[416] | 167 | GameState gamestate; |
---|
| 168 | gamestate.id = a.id; |
---|
| 169 | gamestate.size = normsize; |
---|
| 170 | gamestate.data = dest; |
---|
[436] | 171 | gamestate.diffed = a.diffed; |
---|
[632] | 172 | |
---|
[416] | 173 | return gamestate; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | |
---|
| 177 | GameState GameStateClient::decode(GameState a, GameStateCompressed x){ |
---|
| 178 | GameState t = decompress(x); |
---|
| 179 | return diff(a, t); |
---|
| 180 | } |
---|
| 181 | |
---|
[436] | 182 | GameState GameStateClient::decode(GameStateCompressed x){ |
---|
| 183 | GameState t = decompress(x); |
---|
| 184 | return t; |
---|
| 185 | } |
---|
[416] | 186 | |
---|
| 187 | |
---|
[436] | 188 | |
---|
[416] | 189 | } |
---|