]> git.xonotic.org Git - xonotic/darkplaces.git/blob - sv_save.c
Rename qboolean to qbool
[xonotic/darkplaces.git] / sv_save.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
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 "quakedef.h"
22 #include "prvm_cmds.h"
23
24 /*
25 ===============================================================================
26
27 LOAD / SAVE GAME
28
29 ===============================================================================
30 */
31
32 #define SAVEGAME_VERSION        5
33
34 void SV_Savegame_to(prvm_prog_t *prog, const char *name)
35 {
36         qfile_t *f;
37         int             i, k, l, numbuffers, lightstyles = 64;
38         char    comment[SAVEGAME_COMMENT_LENGTH+1];
39         char    line[MAX_INPUTLINE];
40         qbool isserver;
41         char    *s;
42
43         // first we have to figure out if this can be saved in 64 lightstyles
44         // (for Quake compatibility)
45         for (i=64 ; i<MAX_LIGHTSTYLES ; i++)
46                 if (sv.lightstyles[i][0])
47                         lightstyles = i+1;
48
49         isserver = prog == SVVM_prog;
50
51         Con_Printf("Saving game to %s...\n", name);
52         f = FS_OpenRealFile(name, "wb", false);
53         if (!f)
54         {
55                 Con_Print("ERROR: couldn't open.\n");
56                 return;
57         }
58
59         FS_Printf(f, "%i\n", SAVEGAME_VERSION);
60
61         memset(comment, 0, sizeof(comment));
62         if(isserver)
63                 dpsnprintf(comment, sizeof(comment), "%-21.21s kills:%3i/%3i", PRVM_GetString(prog, PRVM_serveredictstring(prog->edicts, message)), (int)PRVM_serverglobalfloat(killed_monsters), (int)PRVM_serverglobalfloat(total_monsters));
64         else
65                 dpsnprintf(comment, sizeof(comment), "(crash dump of %s progs)", prog->name);
66         // convert space to _ to make stdio happy
67         // LadyHavoc: convert control characters to _ as well
68         for (i=0 ; i<SAVEGAME_COMMENT_LENGTH ; i++)
69                 if (ISWHITESPACEORCONTROL(comment[i]))
70                         comment[i] = '_';
71         comment[SAVEGAME_COMMENT_LENGTH] = '\0';
72
73         FS_Printf(f, "%s\n", comment);
74         if(isserver)
75         {
76                 for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
77                         FS_Printf(f, "%f\n", svs.clients[0].spawn_parms[i]);
78                 FS_Printf(f, "%d\n", current_skill);
79                 FS_Printf(f, "%s\n", sv.name);
80                 FS_Printf(f, "%f\n",sv.time);
81         }
82         else
83         {
84                 for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
85                         FS_Printf(f, "(dummy)\n");
86                 FS_Printf(f, "%d\n", 0);
87                 FS_Printf(f, "%s\n", "(dummy)");
88                 FS_Printf(f, "%f\n", host.realtime);
89         }
90
91         // write the light styles
92         for (i=0 ; i<lightstyles ; i++)
93         {
94                 if (isserver && sv.lightstyles[i][0])
95                         FS_Printf(f, "%s\n", sv.lightstyles[i]);
96                 else
97                         FS_Print(f,"m\n");
98         }
99
100         PRVM_ED_WriteGlobals (prog, f);
101         for (i=0 ; i<prog->num_edicts ; i++)
102         {
103                 FS_Printf(f,"// edict %d\n", i);
104                 //Con_Printf("edict %d...\n", i);
105                 PRVM_ED_Write (prog, f, PRVM_EDICT_NUM(i));
106         }
107
108 #if 1
109         FS_Printf(f,"/*\n");
110         FS_Printf(f,"// DarkPlaces extended savegame\n");
111         // darkplaces extension - extra lightstyles, support for color lightstyles
112         for (i=0 ; i<MAX_LIGHTSTYLES ; i++)
113                 if (isserver && sv.lightstyles[i][0])
114                         FS_Printf(f, "sv.lightstyles %i %s\n", i, sv.lightstyles[i]);
115
116         // darkplaces extension - model precaches
117         for (i=1 ; i<MAX_MODELS ; i++)
118                 if (sv.model_precache[i][0])
119                         FS_Printf(f,"sv.model_precache %i %s\n", i, sv.model_precache[i]);
120
121         // darkplaces extension - sound precaches
122         for (i=1 ; i<MAX_SOUNDS ; i++)
123                 if (sv.sound_precache[i][0])
124                         FS_Printf(f,"sv.sound_precache %i %s\n", i, sv.sound_precache[i]);
125
126         // darkplaces extension - save buffers
127         numbuffers = (int)Mem_ExpandableArray_IndexRange(&prog->stringbuffersarray);
128         for (i = 0; i < numbuffers; i++)
129         {
130                 prvm_stringbuffer_t *stringbuffer = (prvm_stringbuffer_t*) Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i);
131                 if(stringbuffer && (stringbuffer->flags & STRINGBUFFER_SAVED))
132                 {
133                         FS_Printf(f,"sv.buffer %i %i \"string\"\n", i, stringbuffer->flags & STRINGBUFFER_QCFLAGS);
134                         for(k = 0; k < stringbuffer->num_strings; k++)
135                         {
136                                 if (!stringbuffer->strings[k])
137                                         continue;
138                                 // Parse the string a bit to turn special characters
139                                 // (like newline, specifically) into escape codes
140                                 s = stringbuffer->strings[k];
141                                 for (l = 0;l < (int)sizeof(line) - 2 && *s;)
142                                 {       
143                                         if (*s == '\n')
144                                         {
145                                                 line[l++] = '\\';
146                                                 line[l++] = 'n';
147                                         }
148                                         else if (*s == '\r')
149                                         {
150                                                 line[l++] = '\\';
151                                                 line[l++] = 'r';
152                                         }
153                                         else if (*s == '\\')
154                                         {
155                                                 line[l++] = '\\';
156                                                 line[l++] = '\\';
157                                         }
158                                         else if (*s == '"')
159                                         {
160                                                 line[l++] = '\\';
161                                                 line[l++] = '"';
162                                         }
163                                         else
164                                                 line[l++] = *s;
165                                         s++;
166                                 }
167                                 line[l] = '\0';
168                                 FS_Printf(f,"sv.bufstr %i %i \"%s\"\n", i, k, line);
169                         }
170                 }
171         }
172         FS_Printf(f,"*/\n");
173 #endif
174
175         FS_Close (f);
176         Con_Print("done.\n");
177 }
178
179 /*
180 ===============
181 SV_Savegame_f
182 ===============
183 */
184 void SV_Savegame_f(cmd_state_t *cmd)
185 {
186         prvm_prog_t *prog = SVVM_prog;
187         char    name[MAX_QPATH];
188         qbool deadflag = false;
189
190         if (!sv.active)
191         {
192                 Con_Print("Can't save - no server running.\n");
193                 return;
194         }
195
196         deadflag = cl.islocalgame && svs.clients[0].active && PRVM_serveredictfloat(svs.clients[0].edict, deadflag);
197
198         if (cl.islocalgame)
199         {
200                 // singleplayer checks
201                 if (cl.intermission)
202                 {
203                         Con_Print("Can't save in intermission.\n");
204                         return;
205                 }
206
207                 if (deadflag)
208                 {
209                         Con_Print("Can't savegame with a dead player\n");
210                         return;
211                 }
212         }
213         else
214                 Con_Print(CON_WARN "Warning: saving a multiplayer game may have strange results when restored (to properly resume, all players must join in the same player slots and then the game can be reloaded).\n");
215
216         if (Cmd_Argc(cmd) != 2)
217         {
218                 Con_Print("save <savename> : save a game\n");
219                 return;
220         }
221
222         if (strstr(Cmd_Argv(cmd, 1), ".."))
223         {
224                 Con_Print("Relative pathnames are not allowed.\n");
225                 return;
226         }
227
228         strlcpy (name, Cmd_Argv(cmd, 1), sizeof (name));
229         FS_DefaultExtension (name, ".sav", sizeof (name));
230
231         SV_Savegame_to(prog, name);
232 }
233
234 /*
235 ===============
236 SV_Loadgame_f
237 ===============
238 */
239 void SV_Loadgame_f(cmd_state_t *cmd)
240 {
241         prvm_prog_t *prog = SVVM_prog;
242         char filename[MAX_QPATH];
243         char mapname[MAX_QPATH];
244         float time;
245         const char *start;
246         const char *end;
247         const char *t;
248         char *text;
249         prvm_edict_t *ent;
250         int i, k, numbuffers;
251         int entnum;
252         int version;
253         float spawn_parms[NUM_SPAWN_PARMS];
254         prvm_stringbuffer_t *stringbuffer;
255
256         if (Cmd_Argc(cmd) != 2)
257         {
258                 Con_Print("load <savename> : load a game\n");
259                 return;
260         }
261
262         strlcpy (filename, Cmd_Argv(cmd, 1), sizeof(filename));
263         FS_DefaultExtension (filename, ".sav", sizeof (filename));
264
265         Con_Printf("Loading game from %s...\n", filename);
266
267         // stop playing demos
268         if (cls.demoplayback)
269                 CL_Disconnect ();
270
271 #ifdef CONFIG_MENU
272         // remove menu
273         if (key_dest == key_menu || key_dest == key_menu_grabbed)
274                 MR_ToggleMenu(0);
275 #endif
276         key_dest = key_game;
277
278         cls.demonum = -1;               // stop demo loop in case this fails
279
280         t = text = (char *)FS_LoadFile (filename, tempmempool, false, NULL);
281         if (!text)
282         {
283                 Con_Print("ERROR: couldn't open.\n");
284                 return;
285         }
286
287         if(developer_entityparsing.integer)
288                 Con_Printf("SV_Loadgame_f: loading version\n");
289
290         // version
291         COM_ParseToken_Simple(&t, false, false, true);
292         version = atoi(com_token);
293         if (version != SAVEGAME_VERSION)
294         {
295                 Mem_Free(text);
296                 Con_Printf("Savegame is version %i, not %i\n", version, SAVEGAME_VERSION);
297                 return;
298         }
299
300         if(developer_entityparsing.integer)
301                 Con_Printf("SV_Loadgame_f: loading description\n");
302
303         // description
304         COM_ParseToken_Simple(&t, false, false, true);
305
306         for (i = 0;i < NUM_SPAWN_PARMS;i++)
307         {
308                 COM_ParseToken_Simple(&t, false, false, true);
309                 spawn_parms[i] = atof(com_token);
310         }
311         // skill
312         COM_ParseToken_Simple(&t, false, false, true);
313 // this silliness is so we can load 1.06 save files, which have float skill values
314         current_skill = (int)(atof(com_token) + 0.5);
315         Cvar_SetValue (&cvars_all, "skill", (float)current_skill);
316
317         if(developer_entityparsing.integer)
318                 Con_Printf("SV_Loadgame_f: loading mapname\n");
319
320         // mapname
321         COM_ParseToken_Simple(&t, false, false, true);
322         strlcpy (mapname, com_token, sizeof(mapname));
323
324         if(developer_entityparsing.integer)
325                 Con_Printf("SV_Loadgame_f: loading time\n");
326
327         // time
328         COM_ParseToken_Simple(&t, false, false, true);
329         time = atof(com_token);
330
331         if(developer_entityparsing.integer)
332                 Con_Printf("SV_Loadgame_f: spawning server\n");
333
334         SV_SpawnServer (mapname);
335         if (!sv.active)
336         {
337                 Mem_Free(text);
338                 Con_Print("Couldn't load map\n");
339                 return;
340         }
341         sv.paused = true;               // pause until all clients connect
342         sv.loadgame = true;
343
344         if(developer_entityparsing.integer)
345                 Con_Printf("SV_Loadgame_f: loading light styles\n");
346
347 // load the light styles
348
349         // -1 is the globals
350         entnum = -1;
351
352         for (i = 0;i < MAX_LIGHTSTYLES;i++)
353         {
354                 // light style
355                 start = t;
356                 COM_ParseToken_Simple(&t, false, false, true);
357                 // if this is a 64 lightstyle savegame produced by Quake, stop now
358                 // we have to check this because darkplaces may save more than 64
359                 if (com_token[0] == '{')
360                 {
361                         t = start;
362                         break;
363                 }
364                 strlcpy(sv.lightstyles[i], com_token, sizeof(sv.lightstyles[i]));
365         }
366
367         if(developer_entityparsing.integer)
368                 Con_Printf("SV_Loadgame_f: skipping until globals\n");
369
370         // now skip everything before the first opening brace
371         // (this is for forward compatibility, so that older versions (at
372         // least ones with this fix) can load savegames with extra data before the
373         // first brace, as might be produced by a later engine version)
374         for (;;)
375         {
376                 start = t;
377                 if (!COM_ParseToken_Simple(&t, false, false, true))
378                         break;
379                 if (com_token[0] == '{')
380                 {
381                         t = start;
382                         break;
383                 }
384         }
385
386         // unlink all entities
387         World_UnlinkAll(&sv.world);
388
389 // load the edicts out of the savegame file
390         end = t;
391         for (;;)
392         {
393                 start = t;
394                 while (COM_ParseToken_Simple(&t, false, false, true))
395                         if (!strcmp(com_token, "}"))
396                                 break;
397                 if (!COM_ParseToken_Simple(&start, false, false, true))
398                 {
399                         // end of file
400                         break;
401                 }
402                 if (strcmp(com_token,"{"))
403                 {
404                         Mem_Free(text);
405                         Host_Error ("First token isn't a brace");
406                 }
407
408                 if (entnum == -1)
409                 {
410                         if(developer_entityparsing.integer)
411                                 Con_Printf("SV_Loadgame_f: loading globals\n");
412
413                         // parse the global vars
414                         PRVM_ED_ParseGlobals (prog, start);
415
416                         // restore the autocvar globals
417                         Cvar_UpdateAllAutoCvars(prog->console_cmd->cvars);
418                 }
419                 else
420                 {
421                         // parse an edict
422                         if (entnum >= MAX_EDICTS)
423                         {
424                                 Mem_Free(text);
425                                 Host_Error("Host_PerformLoadGame: too many edicts in save file (reached MAX_EDICTS %i)", MAX_EDICTS);
426                         }
427                         while (entnum >= prog->max_edicts)
428                                 PRVM_MEM_IncreaseEdicts(prog);
429                         ent = PRVM_EDICT_NUM(entnum);
430                         memset(ent->fields.fp, 0, prog->entityfields * sizeof(prvm_vec_t));
431                         ent->priv.server->free = false;
432
433                         if(developer_entityparsing.integer)
434                                 Con_Printf("SV_Loadgame_f: loading edict %d\n", entnum);
435
436                         PRVM_ED_ParseEdict (prog, start, ent);
437
438                         // link it into the bsp tree
439                         if (!ent->priv.server->free && !VectorCompare(PRVM_serveredictvector(ent, absmin), PRVM_serveredictvector(ent, absmax)))
440                                 SV_LinkEdict(ent);
441                 }
442
443                 end = t;
444                 entnum++;
445         }
446
447         prog->num_edicts = entnum;
448         sv.time = time;
449
450         for (i = 0;i < NUM_SPAWN_PARMS;i++)
451                 svs.clients[0].spawn_parms[i] = spawn_parms[i];
452
453         if(developer_entityparsing.integer)
454                 Con_Printf("SV_Loadgame_f: skipping until extended data\n");
455
456         // read extended data if present
457         // the extended data is stored inside a /* */ comment block, which the
458         // parser intentionally skips, so we have to check for it manually here
459         if(end)
460         {
461                 while (*end == '\r' || *end == '\n')
462                         end++;
463                 if (end[0] == '/' && end[1] == '*' && (end[2] == '\r' || end[2] == '\n'))
464                 {
465                         if(developer_entityparsing.integer)
466                                 Con_Printf("SV_Loadgame_f: loading extended data\n");
467
468                         Con_Printf("Loading extended DarkPlaces savegame\n");
469                         t = end + 2;
470                         memset(sv.lightstyles[0], 0, sizeof(sv.lightstyles));
471                         memset(sv.model_precache[0], 0, sizeof(sv.model_precache));
472                         memset(sv.sound_precache[0], 0, sizeof(sv.sound_precache));
473                         BufStr_Flush(prog);
474
475                         while (COM_ParseToken_Simple(&t, false, false, true))
476                         {
477                                 if (!strcmp(com_token, "sv.lightstyles"))
478                                 {
479                                         COM_ParseToken_Simple(&t, false, false, true);
480                                         i = atoi(com_token);
481                                         COM_ParseToken_Simple(&t, false, false, true);
482                                         if (i >= 0 && i < MAX_LIGHTSTYLES)
483                                                 strlcpy(sv.lightstyles[i], com_token, sizeof(sv.lightstyles[i]));
484                                         else
485                                                 Con_Printf("unsupported lightstyle %i \"%s\"\n", i, com_token);
486                                 }
487                                 else if (!strcmp(com_token, "sv.model_precache"))
488                                 {
489                                         COM_ParseToken_Simple(&t, false, false, true);
490                                         i = atoi(com_token);
491                                         COM_ParseToken_Simple(&t, false, false, true);
492                                         if (i >= 0 && i < MAX_MODELS)
493                                         {
494                                                 strlcpy(sv.model_precache[i], com_token, sizeof(sv.model_precache[i]));
495                                                 sv.models[i] = Mod_ForName (sv.model_precache[i], true, false, sv.model_precache[i][0] == '*' ? sv.worldname : NULL);
496                                         }
497                                         else
498                                                 Con_Printf("unsupported model %i \"%s\"\n", i, com_token);
499                                 }
500                                 else if (!strcmp(com_token, "sv.sound_precache"))
501                                 {
502                                         COM_ParseToken_Simple(&t, false, false, true);
503                                         i = atoi(com_token);
504                                         COM_ParseToken_Simple(&t, false, false, true);
505                                         if (i >= 0 && i < MAX_SOUNDS)
506                                                 strlcpy(sv.sound_precache[i], com_token, sizeof(sv.sound_precache[i]));
507                                         else
508                                                 Con_Printf("unsupported sound %i \"%s\"\n", i, com_token);
509                                 }
510                                 else if (!strcmp(com_token, "sv.buffer"))
511                                 {
512                                         if (COM_ParseToken_Simple(&t, false, false, true))
513                                         {
514                                                 i = atoi(com_token);
515                                                 if (i >= 0)
516                                                 {
517                                                         k = STRINGBUFFER_SAVED;
518                                                         if (COM_ParseToken_Simple(&t, false, false, true))
519                                                                 k |= atoi(com_token);
520                                                         if (!BufStr_FindCreateReplace(prog, i, k, "string"))
521                                                                 Con_Printf(CON_ERROR "failed to create stringbuffer %i\n", i);
522                                                 }
523                                                 else
524                                                         Con_Printf("unsupported stringbuffer index %i \"%s\"\n", i, com_token);
525                                         }
526                                         else
527                                                 Con_Printf("unexpected end of line when parsing sv.buffer (expected buffer index)\n");
528                                 }
529                                 else if (!strcmp(com_token, "sv.bufstr"))
530                                 {
531                                         if (!COM_ParseToken_Simple(&t, false, false, true))
532                                                 Con_Printf("unexpected end of line when parsing sv.bufstr\n");
533                                         else
534                                         {
535                                                 i = atoi(com_token);
536                                                 stringbuffer = BufStr_FindCreateReplace(prog, i, STRINGBUFFER_SAVED, "string");
537                                                 if (stringbuffer)
538                                                 {
539                                                         if (COM_ParseToken_Simple(&t, false, false, true))
540                                                         {
541                                                                 k = atoi(com_token);
542                                                                 if (COM_ParseToken_Simple(&t, false, false, true))
543                                                                         BufStr_Set(prog, stringbuffer, k, com_token);
544                                                                 else
545                                                                         Con_Printf("unexpected end of line when parsing sv.bufstr (expected string)\n");
546                                                         }
547                                                         else
548                                                                 Con_Printf("unexpected end of line when parsing sv.bufstr (expected strindex)\n");
549                                                 }
550                                                 else
551                                                         Con_Printf(CON_ERROR "failed to create stringbuffer %i \"%s\"\n", i, com_token);
552                                         }
553                                 }       
554                                 // skip any trailing text or unrecognized commands
555                                 while (COM_ParseToken_Simple(&t, true, false, true) && strcmp(com_token, "\n"))
556                                         ;
557                         }
558                 }
559         }
560         Mem_Free(text);
561
562         // remove all temporary flagged string buffers (ones created with BufStr_FindCreateReplace)
563         numbuffers = (int)Mem_ExpandableArray_IndexRange(&prog->stringbuffersarray);
564         for (i = 0; i < numbuffers; i++)
565         {
566                 if ( (stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i)) )
567                         if (stringbuffer->flags & STRINGBUFFER_TEMP)
568                                 BufStr_Del(prog, stringbuffer);
569         }
570
571         if(developer_entityparsing.integer)
572                 Con_Printf("SV_Loadgame_f: finished\n");
573
574         // make sure we're connected to loopback
575         if (sv.active && cls.state == ca_disconnected)
576                 CL_EstablishConnection("local:1", -2);
577 }