]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/CGameData.cpp
Don't link pak tool with SDL.
[quix0rs-blobwars.git] / src / CGameData.cpp
1 /*
2 Copyright (C) 2004-2011 Parallel Realities
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #include "headers.h"
22
23 GameData::GameData()
24 {
25         completedWorld = false;
26 }
27
28 void GameData::clear()
29 {
30         dataList.clear();
31 }
32
33 void GameData::destroy()
34 {
35         clear();
36 }
37
38 void GameData::addCompletedObjective(const char *key, const char *value, int current, int target)
39 {
40         Data *data = (Data*)dataList.getHead();
41
42         while (data->next != NULL)
43         {
44                 data = (Data*)data->next;
45                 if (strcmp(key, data->key) == 0)
46                 {
47                         if (strcmp(value, data->value) == 0)
48                         {
49                                 data->set(key, value, current, target);
50                                 return;
51                         }
52                 }
53         }
54
55         data = new Data();
56         data->set(key, value, current, target);
57
58         dataList.add(data);
59 }
60
61 void GameData::addCompletedObjective(Data *newData)
62 {
63         Data *data = (Data*)dataList.getHead();
64
65         while (data->next != NULL)
66         {
67                 data = (Data*)data->next;
68                 if (strcmp(data->key, newData->key) == 0)
69                 {
70                         if (strcmp(data->value, newData->value) == 0)
71                         {
72                                 data->set(newData->key, newData->value, newData->current, newData->target);
73                                 return;
74                         }
75                 }
76         }
77
78         dataList.add(newData);
79 }
80
81 void GameData::setMIARescueCount(const char *key, int rescues, int total)
82 {
83         Data *data = (Data*)dataList.getHead();
84
85         char newKey[100];
86         snprintf(newKey, sizeof newKey, "%s MIAs", key);
87
88         while (data->next != NULL)
89         {
90                 data = (Data*)data->next;
91                 if (strcmp(newKey, data->key) == 0)
92                 {
93                         strlcpy(data->value, "MIAs", sizeof data->value);
94                         data->current = rescues;
95                         data->target = total;
96                         return;
97                 }
98         }
99
100         data = new Data();
101
102         data->set(newKey, "MIAs", rescues, total);
103
104         dataList.add(data);
105 }
106
107 bool GameData::MIARescued(const char *stageName, char *name)
108 {
109         Data *data = (Data*)dataList.getHead();
110
111         char newName[100];
112         snprintf(newName, sizeof newName, "MIA_%s", name);
113
114         while (data->next != NULL)
115         {
116                 data = (Data*)data->next;
117                 if (strcmp(data->key, stageName) == 0)
118                 {
119                         if (strcmp(data->value, newName) == 0)
120                         {
121                                 return data->isComplete();
122                         }
123                 }
124         }
125
126         return false;
127 }
128
129 bool GameData::objectiveCompleted(const char *stageName, const char *name)
130 {
131         Data *data = (Data*)dataList.getHead();
132
133         while (data->next != NULL)
134         {
135                 data = (Data*)data->next;
136                 if (strcmp(data->key, stageName) == 0)
137                 {
138                         if (strcmp(data->value, name) == 0)
139                         {
140                                 return (data->current == data->target);
141                         }
142                 }
143         }
144
145         return false;
146 }
147
148 void GameData::getObjectiveValues(const char *stageName, const char *name, int *current, int *target)
149 {
150         *current = -1;
151         *target = -1;
152         
153         Data *data = (Data*)dataList.getHead();
154
155         while (data->next != NULL)
156         {
157                 data = (Data*)data->next;
158                 if (strcmp(data->key, stageName) == 0)
159                 {
160                         if (strcmp(data->value, name) == 0)
161                         {
162                                 data->getCurrentTarget(current, target);
163                                 return;
164                         }
165                 }
166         }
167 }
168
169 bool GameData::stagePreviouslyCleared(const char *stageName)
170 {
171         Data *data = (Data*)dataList.getHead();
172
173         while (data->next != NULL)
174         {
175                 data = (Data*)data->next;
176                 if (strcmp(data->key, stageName) == 0)
177                 {
178                         return true;
179                 }
180         }
181
182         return false;
183 }
184
185 bool GameData::isCompleted(const char *key, const char *value)
186 {
187         Data *data = (Data*)dataList.getHead();
188
189         while (data->next != NULL)
190         {
191                 data = (Data*)data->next;
192                 if (strcmp(key, data->key) == 0)
193                 {
194                         if (strcmp(value, data->value) == 0)
195                                 return true;
196                 }
197         }
198
199         return false;
200 }
201
202 bool GameData::levelPrefectlyCleared(const char *level)
203 {
204         Data *data = (Data*)dataList.getHead();
205         
206         bool found = false;
207
208         while (data->next != NULL)
209         {
210                 data = (Data*)data->next;
211
212                 if (strcmp(data->key, level) == 0)
213                 {
214                         found = true;
215
216                         if (!data->isComplete())
217                                 return false;
218                 }
219         }
220         
221         if (!found)
222                 return false;
223
224         return true;
225 }
226
227 bool GameData::requiredLevelCleared(const char *requiredLevel)
228 {
229         Data *data = (Data*)dataList.getHead();
230
231         while (data->next != NULL)
232         {
233                 data = (Data*)data->next;
234
235                 if (strcmp(data->key, requiredLevel) == 0)
236                 {
237                         return true;
238                 }
239         }
240
241         return false;
242 }
243
244 /*
245 Whether or not all the levels in the game have been unlocked
246 */
247 void GameData::calculateWorldCompleted()
248 {
249         completedWorld = false;
250         
251         Data *data = (Data*)dataList.getHead();
252
253         while (data->next != NULL)
254         {
255                 data = (Data*)data->next;
256                 
257                 if (strcmp(data->key, "BioMech HQ") == 0)
258                 {
259                         completedWorld = true;
260                 }
261         }
262 }
263
264 int GameData::getPercentageComplete()
265 {
266         float percentage, total, completed;
267
268         total = completed = percentage = 0;
269
270         Data *data = (Data*)dataList.getHead();
271
272         while (data->next != NULL)
273         {
274                 data = (Data*)data->next;
275
276                 total++;
277
278                 if (data->isComplete())
279                         completed++;
280         }
281
282         if ((total == 0) || (completed == 0))
283                 return 0;
284
285         percentage = (completed / total);
286         percentage *= 100;
287
288         return (int)percentage;
289 }