]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/loadSave.cpp
Don't link pak tool with SDL.
[quix0rs-blobwars.git] / src / loadSave.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 "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                 if (!fgets(line, 1024, fp)) {
126                         fclose(fp);
127                         graphics.showErrorAndExit("Unexpected end of file reading save data", "");
128                 }
129
130                 sscanf(line, "%*c %[^\"] %*c %*c %[^\"] %*c %d %d", string[0], string[1], &param[0], &param[1]);
131                 
132                 data = new Data();
133                 
134                 data->set(string[0], string[1], param[0], param[1]);
135                 
136                 debug(("Read %s %s %d %d\n", data->key, data->value, data->current, data->target));
137                 
138                 if ((data->current == -1) && (data->target == -1))
139                 {
140                         delete data;
141                         break;
142                 }
143
144                 gameData.addCompletedObjective(data);
145                 
146                 sanity++;
147                 
148                 if (sanity == 10000)
149                 {
150                         debug(("Sanity Check #1 > 10000!\n"));
151                         fclose(fp);
152                         exit(1);
153                 }
154         }
155         
156         sanity = 0;
157         
158         char stageName[50];
159         int numberOfLines = 0;
160         
161         Persistant *persistant;
162         PersistData *persistData;
163         
164         while (true)
165         {
166                 if (!fgets(line, 1024, fp)) {
167                         fclose(fp);
168                         graphics.showErrorAndExit("Unexpected end of file reading save data", "");
169                 }
170
171                 sscanf(line, "%[^\n\r]", string[0]);
172                 strlcpy(stageName, string[0], sizeof stageName);
173                 
174                 if (strcmp(stageName, "@EOF@") == 0)
175                 {
176                         break;
177                 }
178
179                 if (!fgets(line, 1024, fp)) {
180                         fclose(fp);
181                         graphics.showErrorAndExit("Unexpected end of file reading save data", "");
182                 }
183                 sscanf(line, "%d", &numberOfLines);
184                 
185                 debug(("Read %s with %d lines.\n", stageName, numberOfLines));
186                 
187                 persistant = map.createPersistant(stageName);
188                 
189                 for (int i = 0 ; i < numberOfLines ; i++)
190                 {
191                         persistData = new PersistData();
192
193                         if (!fgets(line, 1024, fp)) {
194                                 fclose(fp);
195                                 graphics.showErrorAndExit("Unexpected end of file reading save data", "");
196                         }
197
198                         strlcpy(persistData->data, line, sizeof persistData->data);
199                         
200                         //debug(("Read %d: %s", i, persistData->data));
201                         
202                         persistant->addLine(persistData->data);
203                         
204                         sanity++;
205                 
206                         if (sanity == 100000)
207                         {
208                                 debug(("Sanity Check #2 > 100000!\n"));
209                                 fclose(fp);
210                                 exit(1);
211                         }
212                 }
213         }
214
215         fclose(fp);
216         
217         debug(("Loaded Successfully\n"));
218
219         return true;
220 }
221
222 int confirmSave()
223 {
224         if (game.autoSave)
225         {
226                 return game.autoSaveSlot;
227         }
228         
229         if (!engine.loadWidgets("data/saveWidgets"))
230                 graphics.showErrorAndExit(ERR_FILE, "data/saveWidgets");
231         
232         int slot[6], quitYes, quitNo;
233         slot[0] = slot[1] = slot[2] = slot[3] = slot[4] = slot[5] = 0;
234         quitYes = quitNo = 0;
235         
236         engine.setWidgetVariable("slot1", &slot[0]);
237         engine.setWidgetVariable("slot2", &slot[1]);
238         engine.setWidgetVariable("slot3", &slot[2]);
239         engine.setWidgetVariable("slot4", &slot[3]);
240         engine.setWidgetVariable("slot5", &slot[4]);
241         engine.setWidgetVariable("slot6", &slot[5]);
242         
243         engine.setWidgetVariable("contyes", &quitYes);
244         engine.setWidgetVariable("contno", &quitNo);
245         
246         char widgetName[10];
247         widgetName[0] = 0;
248         
249         for (int i = 0 ; i < 5 ; i++)
250         {
251                 snprintf(widgetName, sizeof widgetName, "slot%d", i + 1);
252                 strlcpy(engine.getWidgetByName(widgetName)->label, engine.saveSlot[i], sizeof engine.getWidgetByName(widgetName)->label);
253         }
254         
255         engine.highlightWidget("slot1");
256         
257         int menuSound = 0;
258         
259         int rtn = -1;
260         
261         engine.showWidgetGroup("gameSlots", true);
262         engine.showWidgetGroup("continueconf", false);
263         
264         graphics.setFontSize(4);
265         SDL_Surface *title = graphics.quickSprite("savetitle", graphics.getString("Save Game", true));
266         
267         while (true)
268         {
269                 graphics.updateScreen();
270                 SDL_FillRect(graphics.screen, NULL, graphics.black);
271                 engine.getInput();
272                 config.populate();
273
274                 menuSound = engine.processWidgets();
275
276                 if (menuSound)
277                         audio.playMenuSound(menuSound);
278                 
279                 graphics.blit(title, 320, 100, graphics.screen, true);
280                 
281                 drawWidgets();
282                 
283                 if (slot[5])
284                 {
285                         engine.showWidgetGroup("gameSlots", false);
286                         engine.showWidgetGroup("continueconf", true);
287                         engine.highlightWidget("contno");
288                         drawWidgets();
289                         slot[5] = 0;
290                 }
291                 
292                 if (quitYes)
293                 {
294                         break;
295                 }
296                 
297                 if (quitNo)
298                 {
299                         engine.showWidgetGroup("gameSlots", true);
300                         engine.showWidgetGroup("continueconf", false);
301                         engine.highlightWidget("slot1");
302                         drawWidgets();
303                         quitNo = 0;
304                 }
305                 
306                 for (int i = 0 ; i < 5 ; i++)
307                 {
308                         if (slot[i])
309                         {
310                                 rtn = i;
311                         }
312                 }
313                 
314                 if ((slot[0]) || (slot[1]) || (slot[2]) || (slot[3]) || (slot[4]) || (slot[5]))
315                 {
316                         break;
317                 }
318
319                 SDL_Delay(16);
320         }
321         
322         SDL_FillRect(graphics.screen, NULL, graphics.black);
323         graphics.updateScreen();
324         SDL_Delay(250);
325         
326         return rtn;
327 }
328
329 void saveGame()
330 {
331         char message[256];
332
333         SDL_FillRect(graphics.screen, NULL, graphics.black);
334         graphics.updateScreen();
335         SDL_Delay(500);
336         
337         int slot = confirmSave();
338         
339         if (slot == -1)
340                 return;
341
342         graphics.setFontSize(1);
343         graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
344         snprintf(message, sizeof message, _("Saving Game to Save Slot #%d. Please Wait..."), slot + 1);
345         graphics.drawString(message, 320, 220, true, graphics.screen);
346         graphics.updateScreen();
347
348         char filename[PATH_MAX];
349
350         FILE *fp;
351
352         snprintf(filename, sizeof filename, "%ssave%d.dat", engine.userHomeDirectory, slot);
353
354         fp = fopen(filename, "wb");
355         
356         if (!fp)
357         {
358                 graphics.showErrorAndExit("File write error whilst saving game", "");
359         }
360
361         if (fwrite(&game, sizeof(Game), 1, fp) != 1)
362         {
363                 fclose(fp);
364                 graphics.showErrorAndExit("File write error whilst saving game", strerror(errno));
365         }
366         
367         fclose(fp);
368         
369         snprintf(filename, sizeof filename, "%spersistant%d.dat", engine.userHomeDirectory, slot);
370
371         fp = fopen(filename, "wt");
372         
373         if (!fp)
374         {
375                 graphics.showErrorAndExit("File write error whilst saving game", "");
376         }
377         
378         createPersistantMapData();
379         
380         Data *data = (Data*)gameData.dataList.getHead();
381
382         while (data->next != NULL)
383         {
384                 data = (Data*)data->next;
385                 
386                 fprintf(fp, "\"%s\" \"%s\" %d %d\n", data->key, data->value, data->current, data->target);
387         }
388         
389         fprintf(fp, "\"@EOF@\" \"@EOF@\" -1 -1\n");
390         
391         Persistant *persistant = (Persistant*)map.persistantList.getHead();
392         PersistData *persistData;
393         
394         while (persistant->next != NULL)
395         {
396                 persistant = (Persistant*)persistant->next;
397                 
398                 if (strcmp(persistant->stageName, "@none@") == 0)
399                 {
400                         continue;
401                 }
402                 
403                 fprintf(fp, "%s\n", persistant->stageName);
404                 fprintf(fp, "%d\n", persistant->numberOfLines);
405         
406                 persistData = (PersistData*)persistant->dataList.getHead();
407                 
408                 while (persistData->next != NULL)
409                 {
410                         persistData = (PersistData*)persistData->next;
411                         
412                         fprintf(fp, "%s", persistData->data);
413                 }
414         }
415         
416         fprintf(fp, "@EOF@\n");
417         
418         fclose(fp);
419         
420         map.clear();
421         
422         SDL_Delay(500);
423
424         graphics.drawString(_("Save Complete"), 320, 260, true, graphics.screen);
425         graphics.updateScreen();
426
427         SDL_Delay(500);
428 }