| 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 | //the size of one specific synchronisable |
|---|
| 85 | int tempsize=0; |
|---|
| 86 | // get the start of the Synchronisable list |
|---|
| 87 | orxonox::Iterator<Synchronisable> it; |
|---|
| 88 | // struct for return value of Synchronisable::getData() |
|---|
| 89 | syncData sync; |
|---|
| 90 | |
|---|
| 91 | GameState *retval=new GameState; //return value |
|---|
| 92 | retval->id=id++; |
|---|
| 93 | // reserve a little memory and increase it later on |
|---|
| 94 | retval->data = (unsigned char*)malloc(1); |
|---|
| 95 | |
|---|
| 96 | // offset of memory functions |
|---|
| 97 | int offset=0; |
|---|
| 98 | // go through all Synchronisables |
|---|
| 99 | for(it = orxonox::ObjectList<Synchronisable>::start(); it != 0; ++it){ |
|---|
| 100 | //get size of the synchronisable |
|---|
| 101 | tempsize=it->getSize(); |
|---|
| 102 | //COUT(2) << "size of synchronisable: " << tempsize << std::endl; |
|---|
| 103 | // add place for data and 3 ints (length,classid,objectid) |
|---|
| 104 | totalsize+=tempsize+3*sizeof(int); |
|---|
| 105 | // allocate additional space |
|---|
| 106 | retval->data = (unsigned char *)realloc((void *)retval->data, totalsize); |
|---|
| 107 | |
|---|
| 108 | // run Synchronisable::getData with offset and additional place for 3 ints in between (for ids and length) |
|---|
| 109 | sync=it->getData(retval->data+offset+3*sizeof(int)); |
|---|
| 110 | *(retval->data+offset)=sync.length; |
|---|
| 111 | *(retval->data+offset+sizeof(int))=sync.objectID; |
|---|
| 112 | *(retval->data+offset+2*sizeof(int))=sync.classID; |
|---|
| 113 | // increase data pointer |
|---|
| 114 | offset+=tempsize+3*sizeof(int); |
|---|
| 115 | } |
|---|
| 116 | retval->size=totalsize; |
|---|
| 117 | return retval; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | GameStateCompressed GameStateManager::encode(GameState *a, GameState *b){ |
|---|
| 123 | GameState r = diff(a,b); |
|---|
| 124 | r.diffed = true; |
|---|
| 125 | return compress_(&r); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | GameStateCompressed GameStateManager::encode(GameState *a){ |
|---|
| 129 | a->diffed=false; |
|---|
| 130 | return compress_(a); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | GameState GameStateManager::diff(GameState *a, GameState *b){ |
|---|
| 134 | unsigned char *ap = a->data, *bp = b->data; |
|---|
| 135 | int of=0; // pointers offset |
|---|
| 136 | int dest_length=0; |
|---|
| 137 | if(a->size>=b->size) |
|---|
| 138 | dest_length=a->size; |
|---|
| 139 | else |
|---|
| 140 | dest_length=b->size; |
|---|
| 141 | unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char)); |
|---|
| 142 | while(of<a->size && of<b->size){ |
|---|
| 143 | *(dp+of)=*(ap+of)^*(bp+of); // do the xor |
|---|
| 144 | ++of; |
|---|
| 145 | } |
|---|
| 146 | if(a->size!=b->size){ // do we have to fill up ? |
|---|
| 147 | unsigned char n=0; |
|---|
| 148 | if(a->size<b->size){ |
|---|
| 149 | while(of<dest_length){ |
|---|
| 150 | *(dp+of)=n^*(bp+of); |
|---|
| 151 | of++; |
|---|
| 152 | } |
|---|
| 153 | } else{ |
|---|
| 154 | while(of<dest_length){ |
|---|
| 155 | *(dp+of)=*(ap+of)^n; |
|---|
| 156 | of++; |
|---|
| 157 | } |
|---|
| 158 | } |
|---|
| 159 | } |
|---|
| 160 | // should be finished now |
|---|
| 161 | GameState r = {b->id, dest_length, dp}; |
|---|
| 162 | return r; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | GameStateCompressed GameStateManager::compress_(GameState *a) { |
|---|
| 166 | int size = a->size; |
|---|
| 167 | uLongf buffer = (uLongf)((a->size + 12)*1.01)+1; |
|---|
| 168 | unsigned char* dest = (unsigned char*)malloc( buffer ); |
|---|
| 169 | int retval; |
|---|
| 170 | retval = compress( dest, &buffer, a->data, (uLong)size ); |
|---|
| 171 | |
|---|
| 172 | switch ( retval ) { |
|---|
| 173 | case Z_OK: std::cout << "successfully compressed" << std::endl; break; |
|---|
| 174 | case Z_MEM_ERROR: std::cout << "not enough memory available" << std::endl; break; |
|---|
| 175 | case Z_BUF_ERROR: std::cout << "not enough memory available in the buffer" << std::endl; break; |
|---|
| 176 | case Z_DATA_ERROR: std::cout << "data corrupted" << std::endl; break; |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | GameStateCompressed compressedGamestate; |
|---|
| 180 | compressedGamestate.compsize = buffer; |
|---|
| 181 | compressedGamestate.normsize = size; |
|---|
| 182 | compressedGamestate.id = GAMESTATE; |
|---|
| 183 | compressedGamestate.data = dest; |
|---|
| 184 | compressedGamestate.diffed = a->diffed; |
|---|
| 185 | |
|---|
| 186 | return compressedGamestate; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | void GameStateManager::ackGameState(int clientID, int gamestateID){ |
|---|
| 190 | ClientInformation *temp = head_->findClient(clientID); |
|---|
| 191 | int curid = temp->getID(); |
|---|
| 192 | // decrease usage of gamestate and save it |
|---|
| 193 | deleteUnusedGameState(curid); |
|---|
| 194 | //increase gamestateused |
|---|
| 195 | ++gameStateUsed.find(gamestateID)->second; |
|---|
| 196 | temp->setGamestateID(gamestateID); |
|---|
| 197 | /* |
|---|
| 198 | GameState *old = clientGameState[clientID]; |
|---|
| 199 | deleteUnusedGameState(old); |
|---|
| 200 | clientGameState[clientID]=idGameState[gamestateID];*/ |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | bool GameStateManager::deleteUnusedGameState(int gamestateID){ |
|---|
| 204 | int used = --(gameStateUsed.find(gamestateID)->second); |
|---|
| 205 | if(id-gamestateID>KEEP_GAMESTATES && used==0){ |
|---|
| 206 | // delete gamestate |
|---|
| 207 | delete gameStateMap.find(gamestateID)->second; |
|---|
| 208 | gameStateMap.erase(gamestateID); |
|---|
| 209 | return true; |
|---|
| 210 | } |
|---|
| 211 | return false; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | |
|---|