Debian Codegraph @

code/cgame/cg_challenges.c

raw
1 /*
2 ===========================================================================
3 Copyright (C) 2008 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 "cg_local.h"
24 #include "../qcommon/q_shared.h"
25 #include "../renderer/tr_types.h"
26 #include "../game/bg_public.h"
27 #include "../game/challenges.h"
28 #ifdef PARTofUI
29 #include "../q3_ui/ui_local.h"
30 #else
31 #include "../cgame/cg_local.h"
32 #endif
33
34 #define FILENAME "challenges.dat"
35
36 //First two static variables that may only be accessed throgh functions in this file
37 //The challengeTable is of constant size so there is room for more challenges being added in the furture
38 static unsigned int challengeTable[CHALLENGES_MAX];
39 //We have a variable to see if the system is initialized
40 static qboolean challengesInitialized = qfalse;
41
42 //This function initializes the challenge system. It has no effect if the system is already intialized.
43 void challenges_init(void)
44 {
45 fileHandle_t file;
46 int fileLen;
47 int i;
48
49 //If already initialized then do nothing
50 if(challengesInitialized)
51 return;
52
53 //Else open file
54 fileLen = trap_FS_FOpenFile(FILENAME, &file, FS_READ);
55
56 //If the file not is big enough to contain all challenges then initialize from zero
57 if(fileLen<sizeof(challengeTable))
58 {
59 trap_FS_FCloseFile(file); //Lets remeber to close the file before all returns
60 for(i=0;i<CHALLENGES_MAX;i++)
61 challengeTable[i]=0; //Init all challenges to zero
62 challengesInitialized = qtrue; //Still consider the system loaded!
63 return; //Not enough data to actually read
64 }
65
66 //If the file is big enough:
67 trap_FS_Read(challengeTable,sizeof(challengeTable),file);
68
69 trap_FS_FCloseFile(file); //Lets remember to close the file
70 challengesInitialized = qtrue; //We are initialized!
71 return;
72 }
73
74 //This functions saves the challenges. It only needs to be called once for each game. It should be called from the shutdown function
75 void challenges_save(void)
76 {
77 fileHandle_t file;
78 int fileStatus;
79 int i;
80 if(!challengesInitialized)
81 {
82 //The challenge system has not been initialized and therefore cannot be saved
83 //CG_Printf("Challenge system not initialized.\n");
84 return;
85 }
86 //Open the challenges file for writing:
87 fileStatus = trap_FS_FOpenFile(FILENAME, &file, FS_WRITE);
88 if(fileStatus<0)
89 {
90 //For some reason file opening failed
91 #ifndef PARTofUI
92 CG_Printf("Failed to open challenges.dat for writing\n");
93 #endif
94 return;
95 }
96 for(i=0;i<CHALLENGES_MAX;i++)
97 {
98 //Write the table to disk
99 trap_FS_Write((const void*)&challengeTable[i],sizeof(unsigned int),file);
100 }
101
102 //Always close the file in id tech 3
103 trap_FS_FCloseFile(file);
104 //Lets make the challenges system uninitialized since changes after this will most likely not be saved anyway.
105 challengesInitialized = qfalse;
106 #ifndef PARTofUI
107 CG_Printf("Wrote challenges.cfg\n");
108 #endif
109 }
110
111 //Get the number of times a given challenge has been completed
112 unsigned int getChallenge(int challenge)
113 {
114 challenges_init();
115 if(challenge>=CHALLENGES_MAX)
116 return 0;
117 return challengeTable[challenge];
118 }
119
120 void addChallenge(int challenge)
121 {
122 if(challenge>=CHALLENGES_MAX)
123 {
124 #ifndef PARTofUI
125 CG_Printf("Challenge #%u is >=CHALLENGES_MAX\n",challenge);
126 #endif
127 return; //Maybe also print an error?
128 }
129 challenges_init();
130 challengeTable[challenge]++;
131 //CG_Printf("Increased challenge #%u by one. Is now %u\n",challenge,challengeTable[challenge]);
132 }