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