#include "GameStateClient.h" namespace network { GameStateClient::GameStateClient() { } GameStateClient::~GameStateClient() { } bool GameStateClient::pushGameState(GameStateCompressed compstate){ if(compstate.diffed) return loadSnapshot(decode(reference, compstate)); else return loadSnapshot(decode(compstate)); } /** * This function removes a Synchronisable out of the universe * @param it iterator of the list pointing to the object * @return iterator pointing to the next object in the list */ void GameStateClient::removeObject(orxonox::Iterator &it){ orxonox::Iterator temp=it; ++it; delete *temp; } /** * This function loads a Snapshort of the gamestate into the universe * @param state a GameState struct containing the size of the gamestate and a pointer linking to a flat list (returned by getSnapshot) */ bool GameStateClient::loadSnapshot(GameState state) { unsigned char *data=state.data; // get the start of the Synchronisable list orxonox::Iterator it=orxonox::ObjectList::start(); syncData sync; // loop as long as we have some data ;) while(data < state.data+state.size){ // prepare the syncData struct sync.length = *(int *)data; data+=sizeof(int); sync.objectID = *(int *)data; data+=sizeof(int); sync.classID = *(int *)data; data+=sizeof(int); sync.data = data; data+=sync.length; if(it->objectID!=sync.objectID){ // bad luck ;) // delete the synchronisable (obviously seems to be deleted on the server) while(it != 0 && it->objectID!=sync.objectID){ removeObject(it); } if(it==0){ // add the new object // =================== factory command to add object // can we be sure the object really was added? it=orxonox::ObjectList::end(); it->objectID=sync.objectID; it->classID=sync.classID; } } else { // we have our object if(! it->updateData(sync)) std::cout << "We couldn't update objectID: " \ << sync.objectID << "; classID: " << sync.classID << std::endl; } } return true; } GameState GameStateClient::diff(GameState a, GameState b){ unsigned char *ap = a.data, *bp = b.data; int of=0; // pointers offset int dest_length=0; if(a.size>=b.size) dest_length=a.size; else dest_length=b.size; unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char)); while(of