Debian Codegraph @

code/game/g_playerstore.c

raw
1 /*
2 ===========================================================================
3 Copyright (C) 2008-2009 Poul Sander
4
5 This file is part of Open Arena source code.
6
7 Open Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Open Arena source code is distributed in the hope that it will be
13 useful, 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 Open Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22
23 #include "g_local.h"
24
25 #define MAX_PLAYERS_STORED 32
26
27 #define GUID_SIZE 32
28
29 typedef struct {
30 char guid[GUID_SIZE+1]; //The guid is 32 chars long
31 int age; //int that grows each time a new player is stored. The lowest number is always replaced. Reset to 0 then retrieved.
32 int persistant[MAX_PERSISTANT]; //This is the only information we need to save
33 int timePlayed;
34 int accuracy[WP_NUM_WEAPONS][2];
35 } playerstore_t;
36
37 static playerstore_t playerstore[MAX_PLAYERS_STORED];
38
39 static int nextAge;
40
41 /*
42 *Resets the player store. Should be called everytime game.qvm is loaded.
43 */
44 void PlayerStoreInit( void ) {
45 memset(playerstore,0,sizeof(playerstore));
46 nextAge = 1;
47 }
48
49 void PlayerStore_store(char* guid, playerState_t ps) {
50 int place2store = -1;
51 int lowestAge = 32000;
52 int i;
53 if(strlen(guid)<32)
54 {
55 G_LogPrintf("Playerstore: Failed to store player. Invalid guid: %s\n",guid);
56 return;
57 }
58 for(i=0;i<MAX_PLAYERS_STORED;i++) {
59 if(!Q_stricmp(guid,playerstore[i].guid)) {
60 place2store=i;
61 }
62 }
63
64 if(place2store<0)
65 for(i=0;i<MAX_PLAYERS_STORED;i++) {
66 if(playerstore[i].age < lowestAge) {
67 place2store = i;
68 lowestAge = playerstore[i].age;
69 }
70 }
71
72 //Make sure we don't store in -1
73 if(place2store<0)
74 place2store = 0;
75 //place2store is now the place to store to.
76 playerstore[place2store].age = nextAge++;
77 Q_strncpyz(playerstore[place2store].guid,guid,GUID_SIZE+1);
78 memcpy(playerstore[place2store].persistant,ps.persistant,sizeof(int[MAX_PERSISTANT]));
79 memcpy(playerstore[place2store].accuracy,level.clients[ps.clientNum].accuracy, sizeof(playerstore[0].accuracy) );
80 playerstore[place2store].timePlayed = level.time - level.clients[ps.clientNum].pers.enterTime;
81 G_LogPrintf("Playerstore: Stored player with guid: %s in %u\n", playerstore[place2store].guid,place2store);
82 }
83
84 void PlayerStore_restore(char* guid, playerState_t *ps) {
85 int i;
86 if(strlen(guid)<32)
87 {
88 G_LogPrintf("Playerstore: Failed to restore player. Invalid guid: %s\n",guid);
89 return;
90 }
91 for(i=0;i<MAX_PLAYERS_STORED;i++) {
92 if(!Q_stricmpn(guid,playerstore[i].guid,GUID_SIZE) && playerstore[i].age != -1) {
93 memcpy(ps->persistant,playerstore[i].persistant,sizeof(int[MAX_PERSISTANT]));
94 memcpy(level.clients[ps->clientNum].accuracy, playerstore[i].accuracy,sizeof(playerstore[0].accuracy) );
95 level.clients[ps->clientNum].pers.enterTime = level.time - playerstore[i].timePlayed;
96 //Never ever restore a player with negative score
97 if(ps->persistant[PERS_SCORE]<0)
98 ps->persistant[PERS_SCORE]=0;
99 playerstore[i].age = -1;
100 G_LogPrintf("Restored player with guid: %s\n",guid);
101 return;
102 }
103 }
104 G_LogPrintf("Playerstore: Nothing to restore. Guid: %s\n",guid);
105 }