Debian Codegraph @

prboom2/src/dsda/save.c

raw
1 //
2 // Copyright(C) 2020 by Ryan Krafnick
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. See the
12 // GNU General Public License for more details.
13 //
14 // DESCRIPTION:
15 // DSDA Save
16 //
17
18 #include <stdlib.h>
19
20 #include "doomstat.h"
21 #include "g_game.h"
22 #include "lprintf.h"
23 #include "z_zone.h"
24 #include "p_saveg.h"
25 #include "p_map.h"
26 #include "s_sound.h"
27
28 #include "dsda/args.h"
29 #include "dsda/configuration.h"
30 #include "dsda/data_organizer.h"
31 #include "dsda/excmd.h"
32 #include "dsda/features.h"
33 #include "dsda/mapinfo.h"
34 #include "dsda/music.h"
35 #include "dsda/options.h"
36 #include "dsda/settings.h"
37
38 #include "save.h"
39
40 static char* dsda_base_save_dir;
41 static char* dsda_wad_save_dir;
42
43 extern int dsda_max_kill_requirement;
44 extern int player_damage_last_tic;
45
46 static void dsda_ArchiveInternal(void) {
47 P_SAVE_X(dsda_max_kill_requirement);
48 P_SAVE_X(player_damage_last_tic);
49
50 for (int f = 0; f < FEATURE_SLOTS; f++) {
51 P_SAVE_X(dsda_UsedFeatures()[f]);
52 }
53 }
54
55 static void dsda_UnArchiveInternal(void) {
56 byte features[FEATURE_SLOTS];
57
58 P_LOAD_X(dsda_max_kill_requirement);
59 P_LOAD_X(player_damage_last_tic);
60
61 P_LOAD_X(features);
62 dsda_MergeFeatures(features);
63 }
64
65 static void dsda_ArchiveContext(void) {
66 int i;
67 int boom_logictic_value;
68 int true_logictic_value;
69
70 P_SAVE_BYTE(compatibility_level);
71 P_SAVE_BYTE(gameskill);
72 P_SAVE_BYTE(gameepisode);
73 P_SAVE_BYTE(gamemap);
74
75 for (i = 0; i < g_maxplayers; ++i)
76 P_SAVE_BYTE(playeringame[i]);
77
78 for (; i < FUTURE_MAXPLAYERS; ++i)
79 P_SAVE_BYTE(0);
80
81 P_SAVE_BYTE(idmusnum);
82
83 dsda_ArchiveMusic();
84
85 CheckSaveGame(dsda_GameOptionSize());
86 save_p = G_WriteOptions(save_p);
87
88 P_SAVE_X(leave_data);
89
90 P_SAVE_X(map_info.default_colormap);
91
92 P_SAVE_X(leveltime);
93 P_SAVE_X(totalleveltimes);
94 P_SAVE_X(levels_completed);
95
96 boom_logictic_value = boom_logictic;
97 P_SAVE_X(boom_logictic_value);
98
99 true_logictic_value = true_logictic;
100 P_SAVE_X(true_logictic_value);
101 }
102
103 static void dsda_UnArchiveContext(void) {
104 int i;
105 int epi, map;
106 int boom_logictic_value;
107 int true_logictic_value;
108
109 P_LOAD_BYTE(compatibility_level);
110 P_LOAD_BYTE(gameskill);
111
112 P_LOAD_BYTE(epi);
113 P_LOAD_BYTE(map);
114 dsda_UpdateGameMap(epi, map);
115
116 for (i = 0; i < g_maxplayers; ++i)
117 P_LOAD_BYTE(playeringame[i]);
118 save_p += FUTURE_MAXPLAYERS - g_maxplayers;
119
120 P_LOAD_BYTE(idmusnum);
121 if (idmusnum == 255)
122 idmusnum = -1;
123
124 dsda_UnArchiveMusic();
125
126 save_p += (G_ReadOptions(save_p) - save_p);
127
128 P_LOAD_X(leave_data);
129
130 G_InitNew(gameskill, gameepisode, gamemap, false);
131
132 P_LOAD_X(map_info.default_colormap);
133
134 P_LOAD_X(leveltime);
135 P_LOAD_X(totalleveltimes);
136 P_LOAD_X(levels_completed);
137
138 P_LOAD_X(boom_logictic_value);
139 boom_basetic = gametic - boom_logictic_value;
140
141 P_LOAD_X(true_logictic_value);
142 true_basetic = gametic - true_logictic_value;
143 }
144
145 int saved_pistolstart, saved_respawnparm, saved_fastparm, saved_nomonsters, saved_coop_spawns;
146
147 void dsda_ArchiveGameModifiers(void)
148 {
149 saved_pistolstart = pistolstart;
150 saved_respawnparm = respawnparm;
151 saved_fastparm = fastparm;
152 saved_nomonsters = nomonsters;
153 saved_coop_spawns = coop_spawns;
154
155 P_SAVE_X(saved_pistolstart);
156 P_SAVE_X(saved_respawnparm);
157 P_SAVE_X(saved_fastparm);
158 P_SAVE_X(saved_nomonsters);
159 P_SAVE_X(saved_coop_spawns);
160 }
161
162 void dsda_UnArchiveGameModifiers(void)
163 {
164 P_LOAD_X(saved_pistolstart);
165 P_LOAD_X(saved_respawnparm);
166 P_LOAD_X(saved_fastparm);
167 P_LOAD_X(saved_nomonsters);
168 P_LOAD_X(saved_coop_spawns);
169
170 dsda_UpdateIntConfig(dsda_config_pistol_start, saved_pistolstart, true);
171 dsda_UpdateIntConfig(dsda_config_respawn_monsters, saved_respawnparm, true);
172 dsda_UpdateIntConfig(dsda_config_fast_monsters, saved_fastparm, true);
173 dsda_UpdateIntConfig(dsda_config_no_monsters, saved_nomonsters, true);
174 dsda_UpdateIntConfig(dsda_config_coop_spawns, saved_coop_spawns, true);
175 }
176
177 void dsda_ArchiveAll(void) {
178 dsda_ArchiveContext();
179
180 P_ArchiveACS();
181 P_ArchivePlayers();
182 P_ThinkerToIndex();
183 P_ArchiveWorld();
184 P_ArchivePolyobjs();
185 P_ArchiveThinkers();
186 P_ArchiveScripts();
187 P_ArchiveSounds();
188 P_ArchiveAmbientSound();
189 P_ArchiveMisc();
190 P_IndexToThinker();
191 P_ArchiveRNG();
192 P_ArchiveMap();
193
194 dsda_ArchiveGameModifiers();
195 dsda_ArchiveInternal();
196 }
197
198 void dsda_UnArchiveAll(void) {
199 dsda_UnArchiveContext();
200
201 P_MapStart();
202 P_UnArchiveACS();
203 P_UnArchivePlayers();
204 P_UnArchiveWorld();
205 P_UnArchivePolyobjs();
206 P_UnArchiveThinkers();
207 P_UnArchiveScripts();
208 P_UnArchiveSounds();
209 P_UnArchiveAmbientSound();
210 P_UnArchiveMisc();
211 P_UnArchiveRNG();
212 P_UnArchiveMap();
213 P_MapEnd();
214
215 dsda_UnArchiveGameModifiers();
216 dsda_UnArchiveInternal();
217 }
218
219 void dsda_InitSaveDir(void) {
220 dsda_base_save_dir = dsda_DetectDirectory("DOOMSAVEDIR", dsda_arg_save);
221 }
222
223 char* dsda_SaveDir(void) {
224 dsda_arg_t* arg = dsda_Arg(dsda_arg_save);
225
226 if (!arg->found) {
227 if (dsda_IntConfig(dsda_config_organized_saves)) {
228 if (!dsda_wad_save_dir)
229 dsda_wad_save_dir = dsda_DataDir();
230
231 return dsda_wad_save_dir;
232 }
233 }
234
235 return dsda_base_save_dir;
236 }
237
238 extern const char* savegamename;
239
240 char* dsda_SaveGameName(int slot, dboolean via_cmd) {
241 int length;
242 char* name;
243 const char* save_dir;
244 const char* save_type;
245
246 if (slot > 9999 || slot < 0)
247 I_Error("dsda_SaveGameName: bad save slot %d", slot);
248
249 save_dir = dsda_SaveDir();
250
251 save_type = (via_cmd && demoplayback) ? "demosav" : savegamename;
252
253 length = strlen(save_type) + strlen(save_dir) + 10; // "/" + "9999.dsg\0"
254
255 name = Z_Malloc(length);
256
257 snprintf(name, length, "%s/%s%d.dsg", save_dir, save_type, slot);
258
259 return name;
260 }
261
262 static int* demo_save_slots;
263 static int allocated_save_slot_count;
264 static int demo_save_slot_count;
265
266 void dsda_ResetDemoSaveSlots(void) {
267 demo_save_slot_count = 0;
268 }
269
270 static void dsda_MarkSaveSlotUsed(int slot) {
271 int i;
272
273 if (!demorecording) return;
274
275 for (i = 0; i < demo_save_slot_count; ++i)
276 if (demo_save_slots[i] == slot)
277 return;
278
279 ++demo_save_slot_count;
280 if (demo_save_slot_count > allocated_save_slot_count) {
281 allocated_save_slot_count = allocated_save_slot_count ? allocated_save_slot_count * 2 : 8;
282 demo_save_slots =
283 Z_Realloc(demo_save_slots, allocated_save_slot_count * sizeof(*demo_save_slots));
284 }
285
286 demo_save_slots[demo_save_slot_count - 1] = slot;
287 }
288
289 int dsda_AllowAnyMenuSave(void) {
290 return !dsda_StrictMode() || dsda_AllowCasualExCmdFeatures();
291 }
292
293 int dsda_AllowMenuLoad(int slot) {
294 int i;
295
296 if (!demorecording) return true;
297 if (!dsda_AllowCasualExCmdFeatures()) return false;
298
299 for (i = 0; i < demo_save_slot_count; ++i)
300 if (demo_save_slots[i] == slot)
301 return true;
302
303 return false;
304 }
305
306 int dsda_AllowAnyMenuLoad(void) {
307 return !demorecording || dsda_AllowCasualExCmdFeatures();
308 }
309
310 static int last_save_file_slot = -1;
311
312 void dsda_SetLastLoadSlot(int slot) {
313 last_save_file_slot = slot;
314 }
315
316 void dsda_SetLastSaveSlot(int slot) {
317 dsda_MarkSaveSlotUsed(slot);
318 last_save_file_slot = slot;
319 }
320
321 int dsda_LastSaveSlot(void) {
322 return last_save_file_slot;
323 }
324
325 void dsda_ResetLastSaveSlot(void) {
326 last_save_file_slot = -1;
327 }
328
329 void dsda_UpdateAutoSaves(void) {
330 static int automap = -1;
331 static int autoepisode = -1;
332
333 void M_AutoSave(void);
334
335 if (!dsda_IntConfig(dsda_config_auto_save) ||
336 gamestate != GS_LEVEL ||
337 gameaction != ga_nothing ||
338 demoplayback ||
339 demorecording)
340 return;
341
342 if (automap != gamemap || autoepisode != gameepisode) {
343 automap = gamemap;
344 autoepisode = gameepisode;
345
346 if (!leveltime)
347 M_AutoSave();
348 }
349 }