]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/loadSave.cpp
Make functions const where possible.
[quix0rs-blobwars.git] / src / loadSave.cpp
1 /*
2 Copyright (C) 2004 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 "loadSave.h"
22
23 void initSaveSlots()
24 {
25         char filename[PATH_MAX];
26         char string[100];
27         struct stat fileInfo;
28         int modTime = 0;
29
30         Game tempGame;
31
32         engine.continueSaveSlot = 0;
33
34         FILE *fp;
35
36         //READ SAVE GAME DATA
37         for (int i = 0 ; i < 5 ; i++)
38         {
39                 snprintf(filename, sizeof filename, "%ssave%d.dat", engine.userHomeDirectory, i);
40
41                 fp = fopen(filename, "rb");
42
43                 if (!fp)
44                 {
45                         strlcpy(string, "%.2d - %s", sizeof string);
46                         snprintf(engine.saveSlot[i], sizeof engine.saveSlot[i], string, (i + 1), _("Empty"));
47                 }
48                 else
49                 {
50                         if (fread(&tempGame, sizeof(Game), 1, fp) != 1)
51                         {
52                                 strlcpy(string, "%.2d - %s", sizeof string);
53                                 snprintf(engine.saveSlot[i], sizeof engine.saveSlot[i], string, (i + 1), _("Corrupt Save Data"));
54                         }
55                         else
56                         {
57                                 snprintf(engine.saveSlot[i], sizeof engine.saveSlot[i], "%.2d - %s (%.2d:%.2d:%.2d)", (i + 1), _(tempGame.stageName), tempGame.totalHours, tempGame.totalMinutes, tempGame.totalSeconds);
58                         }
59
60                         if (stat(filename, &fileInfo) != -1)
61                         {
62                                 if (fileInfo.st_mtime > modTime)
63                                 {
64                                         modTime = fileInfo.st_mtime;
65                                         engine.continueSaveSlot = (i + 1);
66                                 }
67                         }
68
69                         fclose(fp);
70                 }
71         }
72         
73         debug(("Continue Save Slot = %d\n", engine.continueSaveSlot));
74 }
75
76 /*
77 Fill in later...
78 */
79 bool loadGame(int slot)
80 {
81         debug(("Loading Game #%d...\n", slot));
82         game.clear();
83
84         SDL_Delay(500);
85         char filename[PATH_MAX];
86         
87         char line[1024];
88         char string[2][1024];
89         int param[2];
90         
91         Data *data;
92
93         FILE *fp;
94         
95         int sanity = 0;
96
97         snprintf(filename, sizeof filename, "%ssave%d.dat", engine.userHomeDirectory, slot);
98
99         fp = fopen(filename, "rb");
100         
101         if (!fp)
102         {
103                 return false;
104         }
105
106         if (fread(&game, sizeof(Game), 1, fp) != 1)
107         {
108                 fclose(fp);
109                 graphics.showErrorAndExit("The save data loaded was not in the format expected", "");
110         }
111         
112         fclose(fp);
113         
114         snprintf(filename, sizeof filename, "%spersistant%d.dat", engine.userHomeDirectory, slot);
115
116         fp = fopen(filename, "rb");
117         
118         if (!fp)
119         {
120                 return false;
121         }
122         
123         while (true)
124         {
125                 fgets(line, 1024, fp);
126                 
127                 sscanf(line, "%*c %[^\"] %*c %*c %[^\"] %*c %d %d", string[0], string[1], &param[0], &param[1]);
128                 
129                 data = new Data();
130                 
131                 data->set(string[0], string[1], param[0], param[1]);
132                 
133                 debug(("Read %s %s %d %d\n", data->key, data->value, data->current, data->target));
134                 
135                 if ((data->current == -1) && (data->target == -1))
136                 {
137                         delete data;
138                         break;
139                 }
140
141                 gameData.addCompletedObjective(data);
142                 
143                 sanity++;
144                 
145                 if (sanity == 10000)
146                 {
147                         debug(("Sanity Check #1 > 10000!\n"));
148                         fclose(fp);
149                         exit(1);
150                 }
151         }
152         
153         sanity = 0;
154         
155         char stageName[50];
156         int numberOfLines = 0;
157         
158         Persistant *persistant;
159         PersistData *persistData;
160         
161         while (true)
162         {
163                 fgets(line, 1024, fp);
164                 
165                 sscanf(line, "%[^\n\r]", string[0]);
166                 strlcpy(stageName, string[0], sizeof stageName);
167                 
168                 if (strcmp(stageName, "@EOF@") == 0)
169                 {
170                         break;
171                 }
172                 
173                 fgets(line, 1024, fp);
174                 sscanf(line, "%d", &numberOfLines);
175                 
176                 debug(("Read %s with %d lines.\n", stageName, numberOfLines));
177                 
178                 persistant = map.createPersistant(stageName);
179                 
180                 for (int i = 0 ; i < numberOfLines ; i++)
181                 {
182                         persistData = new PersistData();
183                         
184                         fgets(line, 1024, fp);
185                         
186                         strlcpy(persistData->data, line, sizeof persistData->data);
187                         
188                         //debug(("Read %d: %s", i, persistData->data));
189                         
190                         persistant->addLine(persistData->data);
191                         
192                         sanity++;
193                 
194                         if (sanity == 100000)
195                         {
196                                 debug(("Sanity Check #2 > 100000!\n"));
197                                 fclose(fp);
198                                 exit(1);
199                         }
200                 }
201         }
202
203         fclose(fp);
204         
205         debug(("Loaded Successfully\n"));
206
207         return true;
208 }
209
210 int confirmSave()
211 {
212         if (game.autoSave)
213         {
214                 return game.autoSaveSlot;
215         }
216         
217         if (!engine.loadWidgets("data/saveWidgets"))
218                 graphics.showErrorAndExit(ERR_FILE, "data/saveWidgets");
219         
220         int slot[6], quitYes, quitNo;
221         slot[0] = slot[1] = slot[2] = slot[3] = slot[4] = slot[5] = 0;
222         quitYes = quitNo = 0;
223         
224         engine.setWidgetVariable("slot1", &slot[0]);
225         engine.setWidgetVariable("slot2", &slot[1]);
226         engine.setWidgetVariable("slot3", &slot[2]);
227         engine.setWidgetVariable("slot4", &slot[3]);
228         engine.setWidgetVariable("slot5", &slot[4]);
229         engine.setWidgetVariable("slot6", &slot[5]);
230         
231         engine.setWidgetVariable("contyes", &quitYes);
232         engine.setWidgetVariable("contno", &quitNo);
233         
234         char widgetName[10];
235         widgetName[0] = 0;
236         
237         for (int i = 0 ; i < 5 ; i++)
238         {
239                 snprintf(widgetName, sizeof widgetName, "slot%d", i + 1);
240                 strlcpy(engine.getWidgetByName(widgetName)->label, engine.saveSlot[i], sizeof engine.getWidgetByName(widgetName)->label);
241         }
242         
243         engine.highlightWidget("slot1");
244         
245         int menuSound = 0;
246         
247         int rtn = -1;
248         
249         engine.showWidgetGroup("gameSlots", true);
250         engine.showWidgetGroup("continueconf", false);
251         
252         graphics.setFontSize(4);
253         SDL_Surface *title = graphics.quickSprite("savetitle", graphics.getString("Save Game", true));
254         
255         while (true)
256         {
257                 graphics.updateScreen();
258                 SDL_FillRect(graphics.screen, NULL, graphics.black);
259                 engine.getInput();
260                 config.populate();
261
262                 menuSound = engine.processWidgets();
263
264                 if (menuSound)
265                         audio.playMenuSound(menuSound);
266                 
267                 graphics.blit(title, 320, 100, graphics.screen, true);
268                 
269                 drawWidgets();
270                 
271                 if (slot[5])
272                 {
273                         engine.showWidgetGroup("gameSlots", false);
274                         engine.showWidgetGroup("continueconf", true);
275                         engine.highlightWidget("contno");
276                         drawWidgets();
277                         slot[5] = 0;
278                 }
279                 
280                 if (quitYes)
281                 {
282                         break;
283                 }
284                 
285                 if (quitNo)
286                 {
287                         engine.showWidgetGroup("gameSlots", true);
288                         engine.showWidgetGroup("continueconf", false);
289                         engine.highlightWidget("slot1");
290                         drawWidgets();
291                         quitNo = 0;
292                 }
293                 
294                 for (int i = 0 ; i < 5 ; i++)
295                 {
296                         if (slot[i])
297                         {
298                                 rtn = i;
299                         }
300                 }
301                 
302                 if ((slot[0]) || (slot[1]) || (slot[2]) || (slot[3]) || (slot[4]) || (slot[5]))
303                 {
304                         break;
305                 }
306
307                 SDL_Delay(16);
308         }
309         
310         SDL_FillRect(graphics.screen, NULL, graphics.black);
311         graphics.updateScreen();
312         SDL_Delay(250);
313         
314         return rtn;
315 }
316
317 void saveGame()
318 {
319         char message[256];
320
321         SDL_FillRect(graphics.screen, NULL, graphics.black);
322         graphics.updateScreen();
323         SDL_Delay(500);
324         
325         int slot = confirmSave();
326         
327         if (slot == -1)
328                 return;
329
330         graphics.setFontSize(1);
331         graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
332         snprintf(message, sizeof message, _("Saving Game to Save Slot #%d. Please Wait..."), slot + 1);
333         graphics.drawString(message, 320, 220, true, graphics.screen);
334         graphics.updateScreen();
335
336         char filename[PATH_MAX];
337
338         FILE *fp;
339
340         snprintf(filename, sizeof filename, "%ssave%d.dat", engine.userHomeDirectory, slot);
341
342         fp = fopen(filename, "wb");
343         
344         if (!fp)
345         {
346                 graphics.showErrorAndExit("File write error whilst saving game", "");
347         }
348
349         fwrite(&game, sizeof(Game), 1, fp);
350         
351         fclose(fp);
352         
353         snprintf(filename, sizeof filename, "%spersistant%d.dat", engine.userHomeDirectory, slot);
354
355         fp = fopen(filename, "wt");
356         
357         if (!fp)
358         {
359                 graphics.showErrorAndExit("File write error whilst saving game", "");
360         }
361         
362         createPersistantMapData();
363         
364         Data *data = (Data*)gameData.dataList.getHead();
365
366         while (data->next != NULL)
367         {
368                 data = (Data*)data->next;
369                 
370                 fprintf(fp, "\"%s\" \"%s\" %d %d\n", data->key, data->value, data->current, data->target);
371         }
372         
373         fprintf(fp, "\"@EOF@\" \"@EOF@\" -1 -1\n");
374         
375         Persistant *persistant = (Persistant*)map.persistantList.getHead();
376         PersistData *persistData;
377         
378         while (persistant->next != NULL)
379         {
380                 persistant = (Persistant*)persistant->next;
381                 
382                 if (strcmp(persistant->stageName, "@none@") == 0)
383                 {
384                         continue;
385                 }
386                 
387                 fprintf(fp, "%s\n", persistant->stageName);
388                 fprintf(fp, "%d\n", persistant->numberOfLines);
389         
390                 persistData = (PersistData*)persistant->dataList.getHead();
391                 
392                 while (persistData->next != NULL)
393                 {
394                         persistData = (PersistData*)persistData->next;
395                         
396                         fprintf(fp, "%s", persistData->data);
397                 }
398         }
399         
400         fprintf(fp, "@EOF@\n");
401         
402         fclose(fp);
403         
404         map.clear();
405         
406         SDL_Delay(500);
407
408         graphics.drawString(_("Save Complete"), 320, 260, true, graphics.screen);
409         graphics.updateScreen();
410
411         SDL_Delay(500);
412 }