-
Notifications
You must be signed in to change notification settings - Fork 635
Expand file tree
/
Copy pathsave.c
More file actions
1227 lines (1122 loc) · 40.9 KB
/
Copy pathsave.c
File metadata and controls
1227 lines (1122 loc) · 40.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* NetHack 5.0 save.c $NHDT-Date: 1781973065 2026/06/20 16:31:05 $ $NHDT-Branch: NetHack-5.0 $:$NHDT-Revision: 1.263 $ */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/*-Copyright (c) Michael Allison, 2009. */
/* NetHack may be freely redistributed. See license for details. */
#include "hack.h"
#ifndef NO_SIGNAL
#include <signal.h>
#endif
#if !defined(LSC) && !defined(O_WRONLY) && !defined(AZTEC_C)
#include <fcntl.h>
#endif
#ifdef MICRO
int dotcnt, dotrow; /* also used in restore */
#endif
staticfn void savelevchn(NHFILE *);
staticfn void savelevl(NHFILE *);
staticfn void savedamage(NHFILE *);
staticfn void save_bubbles(NHFILE *, xint8);
staticfn void save_stairs(NHFILE *);
staticfn void save_bc(NHFILE *);
staticfn void saveobj(NHFILE *, struct obj *);
staticfn void saveobjchn(NHFILE *, struct obj **) NO_NNARGS;
staticfn void savemon(NHFILE *, struct monst *);
staticfn void savemonchn(NHFILE *, struct monst *) NO_NNARGS;
staticfn void savetrapchn(NHFILE *, struct trap *) NO_NNARGS;
staticfn void save_gamelog(NHFILE *);
staticfn void savegamestate(NHFILE *);
staticfn void savelev_core(NHFILE *, xint8);
staticfn void save_msghistory(NHFILE *);
staticfn void save_adjust_levelflags(long);
#if defined(HANGUPHANDLING)
#define HUP if (!program_state.done_hup)
#else
#define HUP
#endif
/* the #save command */
int
dosave(void)
{
clear_nhwindow(WIN_MESSAGE);
if (y_n("Really save?") == 'n') {
clear_nhwindow(WIN_MESSAGE);
if (gm.multi > 0)
nomul(0);
} else {
clear_nhwindow(WIN_MESSAGE);
pline("Saving...");
#if defined(HANGUPHANDLING)
program_state.done_hup = 0;
#endif
if (dosave0()) {
program_state.savefile_completed++;
u.uhp = -1; /* universal game's over indicator */
if (soundprocs.sound_exit_nhsound)
(*soundprocs.sound_exit_nhsound)("dosave");
/* make sure they see the Saving message */
display_nhwindow(WIN_MESSAGE, TRUE);
exit_nhwindows("Be seeing you...");
nh_terminate(EXIT_SUCCESS);
} else
docrt();
}
return ECMD_OK;
}
/* returns 1 if save successful */
int
dosave0(void)
{
const char *fq_save;
xint8 ltmp;
char whynot[BUFSZ];
NHFILE *nhfp, *onhfp;
int res = 0;
program_state.saving++; /* inhibit status and perm_invent updates */
notice_mon_off();
/* we may get here via hangup signal, in which case we want to fix up
a few of things before saving so that they won't be restored in
an improper state; these will be no-ops for normal save sequence */
u.uinvulnerable = 0;
if (iflags.save_uswallow)
u.uswallow = 1, iflags.save_uswallow = 0;
if (iflags.save_uinwater)
u.uinwater = 1, iflags.save_uinwater = 0; /* bypass set_uinwater() */
if (iflags.save_uburied)
u.uburied = 1, iflags.save_uburied = 0;
/* extra handling for hangup save or panic save; without this,
a thrown light source might trigger an "obj_is_local" panic;
if a thrown or kicked object is in transit, put it on the map;
when punished, make sure ball and chain are placed too */
done_object_cleanup(); /* maybe force some items onto map */
/* ensure SAVEF is set; pcmain sets it at startup but the player
name may have changed since then (strstrplay, strstrstrplay) */
set_savefile_name(TRUE);
if (!program_state.something_worth_saving || !gs.SAVEF[0])
goto done;
fq_save = fqname(gs.SAVEF, SAVEPREFIX, 1); /* level files take 0 */
#ifndef NO_SIGNAL
#if defined(UNIX) || defined(VMS)
sethanguphandler((void (*)(int) ) SIG_IGN);
#endif
(void) signal(SIGINT, SIG_IGN);
#endif
HUP if (iflags.window_inited) {
nh_uncompress(fq_save);
nhfp = open_savefile();
if (nhfp) {
close_nhfile(nhfp);
clear_nhwindow(WIN_MESSAGE);
There("seems to be an old save file.");
if (y_n("Overwrite the old file?") == 'n') {
nh_sfconvert(fq_save);
nh_compress(fq_save);
goto done;
}
}
}
HUP mark_synch(); /* flush any buffered screen output */
nhfp = create_savefile();
if (!nhfp) {
HUP pline("Cannot open save file.");
(void) delete_savefile(); /* ab@unido */
goto done;
}
if (nhfp && nhfp->fplog) {
nhfp->rcount = nhfp->wcount = 0L;
}
vision_recalc(2); /* shut down vision to prevent problems
in the event of an impossible() call */
/* undo date-dependent luck adjustments made at startup time */
if (flags.moonphase == FULL_MOON) /* ut-sally!fletcher */
change_luck(-1); /* and unido!ab */
if (flags.friday13)
change_luck(1);
if (iflags.window_inited)
HUP clear_nhwindow(WIN_MESSAGE);
#ifdef MICRO
dotcnt = 0;
dotrow = 2;
curs(WIN_MAP, 1, 1);
if (!WINDOWPORT(X11))
putstr(WIN_MAP, 0, "Saving:");
#endif
nhfp->mode = WRITING | FREEING;
store_version(nhfp);
store_plname_in_file(nhfp);
/* savelev() might save uball and uchain, releasing their memory if
FREEING, so we need to check their status now; if hero is swallowed,
uball and uchain will persist beyond saving map floor and inventory
so these copies of their pointers will be valid and savegamestate()
will know to save them separately (from floor and invent); when not
swallowed, uchain will be stale by then, and uball will be too if
ball is on the floor rather than carried */
gl.looseball = BALL_IN_MON ? uball : 0;
gl.loosechain = CHAIN_IN_MON ? uchain : 0;
savelev(nhfp, ledger_no(&u.uz));
savegamestate(nhfp);
/* While copying level files around, zero out u.uz to keep
* parts of the restore code from completely initializing all
* in-core data structures, since all we're doing is copying.
* This also avoids at least one nasty core dump.
* [gu.uz_save is used by save_bubbles() as well as to restore u.uz]
*/
gu.uz_save = u.uz;
u.uz.dnum = u.uz.dlevel = 0;
/* these pointers are no longer valid, and at least u.usteed
* may mislead place_monster() on other levels
*/
set_ustuck((struct monst *) 0); /* also clears u.uswallow */
u.usteed = (struct monst *) 0;
for (ltmp = (xint8) 1; ltmp <= maxledgerno(); ltmp++) {
if (ltmp == ledger_no(&gu.uz_save))
continue;
if (!(svl.level_info[ltmp].flags & LFILE_EXISTS))
continue;
#ifdef MICRO
curs(WIN_MAP, 1 + dotcnt++, dotrow);
if (dotcnt >= (COLNO - 1)) {
dotrow++;
dotcnt = 0;
}
if (!WINDOWPORT(X11)) {
putstr(WIN_MAP, 0, ".");
}
mark_synch();
#endif
onhfp = open_levelfile(ltmp, whynot);
if (!onhfp) {
HUP pline1(whynot);
close_nhfile(nhfp);
(void) delete_savefile();
HUP Strcpy(svk.killer.name, whynot);
HUP done(TRICKED);
goto done;
}
getlev(onhfp, svh.hackpid, ltmp);
close_nhfile(onhfp);
Sfo_xint8(nhfp, <mp, "gamestate-level_number");
savelev(nhfp, ltmp); /* actual level*/
delete_levelfile(ltmp);
}
close_nhfile(nhfp);
u.uz = gu.uz_save;
gu.uz_save.dnum = gu.uz_save.dlevel = 0;
/* get rid of current level --jgm */
delete_levelfile(ledger_no(&u.uz));
delete_levelfile(0);
nh_sfconvert(fq_save);
nh_compress(fq_save);
/* this should probably come sooner... */
program_state.something_worth_saving = 0;
res = 1;
done:
notice_mon_on();
program_state.saving--;
return res;
}
staticfn void
save_gamelog(NHFILE *nhfp)
{
struct gamelog_line *tmp = gg.gamelog, *tmp2;
int slen;
while (tmp) {
tmp2 = tmp->next;
if (nhfp->mode & (COUNTING | WRITING)) {
slen = Strlen(tmp->text);
Sfo_int(nhfp, &slen, "gamelog-length");
Sfo_char(nhfp, tmp->text, "gamelog-gamelog_text", slen);
Sfo_gamelog_line(nhfp, tmp, "gamelog-gamelog_line");
}
if (nhfp->mode & FREEING) {
free((genericptr_t) tmp->text);
free((genericptr_t) tmp);
}
tmp = tmp2;
}
if (nhfp->mode & (COUNTING | WRITING)) {
slen = -1;
Sfo_int(nhfp, &slen, "gamelog-length");
}
if (nhfp->mode & FREEING)
gg.gamelog = NULL;
}
staticfn void
savegamestate(NHFILE *nhfp)
{
int i;
unsigned long uid;
program_state.saving++; /* caller should/did already set this... */
uid = (unsigned long) getuid();
Sfo_ulong(nhfp, &uid, "gamestate-uid");
Sfo_char(nhfp, &svn.nhuuid[0], "nhuuid", sizeof svn.nhuuid);
Sfo_long(nhfp, &svm.moves, "gamestate-moves");
moves_to_relative_time(&svc.context.seer_turn);
moves_to_relative_time(&svc.context.digging.lastdigtime);
Sfo_context_info(nhfp, &svc.context, "gamestate-context");
relative_time_to_moves(&svc.context.seer_turn);
relative_time_to_moves(&svc.context.digging.lastdigtime);
Sfo_flag(nhfp, &flags, "gamestate-flags");
urealtime.finish_time = getnow();
urealtime.realtime += timet_delta(urealtime.finish_time,
urealtime.start_timing);
Sfo_long(nhfp, &svw.wreserve, "wreserve");
Sfo_int32(nhfp, &svw.wtreserved, "wtreserved");
/*
* In a normal save operation initiated by the player,
* the u.ustuck_mid and u.usteed_mid were already set
* by savemonchn() called from savelev_core(). Under
* that operation, u.ustuck and u.usteed pointers no longer
* point to valid locations here. Their non-zero values serve
* only to flag that they are were set, but the pointers
* must not be dereferenced.
*/
if (program_state.in_checkpoint) {
/*
* It is critical to ensure that u.ustuck_mid and u.usteed_mid
* hold current and correct data, in case this is needed by
* recover. The pointers, if set, are still pointing at
* valid data during a checkpoint operation, unlike during
* a synchronized save operation.
*/
u.ustuck_mid = (u.ustuck) ? u.ustuck->m_id : 0;
u.usteed_mid = (u.usteed) ? u.usteed->m_id : 0;
}
Sfo_you(nhfp, &u, "gamestate-you");
/* clear the in-memory value of these, now that they have been
* successfully written out.
*/
u.ustuck_mid = 0;
u.usteed_mid = 0;
Sfo_char(nhfp, yyyymmddhhmmss(ubirthday), "gamestate-ubirthday", 14);
Sfo_long(nhfp, &urealtime.realtime, "gamestate-realtime");
Sfo_char(nhfp, yyyymmddhhmmss(urealtime.start_timing), "gamestate-start_timing", 14);
/* this is the value to use for the next update of urealtime.realtime */
urealtime.start_timing = urealtime.finish_time;
save_killers(nhfp);
/* must come before gm.migrating_objs and gm.migrating_mons are freed */
save_timers(nhfp, RANGE_GLOBAL);
save_light_sources(nhfp, RANGE_GLOBAL);
/* when FREEING, deletes objects in invent and sets invent to Null;
pointers into invent (uwep, uarmg, uamul, &c) are set to Null too */
saveobjchn(nhfp, &gi.invent);
/* save ball and chain if they happen to be in an unusual state */
save_bc(nhfp);
saveobjchn(nhfp, &gm.migrating_objs); /* frees objs and sets to Null */
savemonchn(nhfp, gm.migrating_mons);
if (release_data(nhfp))
gm.migrating_mons = (struct monst *) 0;
for (i = 0; i < NUMMONS; ++i) {
Sfo_mvitals(nhfp, &svm.mvitals[i], "gamestate-mvitals");
}
save_dungeon(nhfp, (boolean) !!update_file(nhfp),
(boolean) !!release_data(nhfp));
savelevchn(nhfp);
Sfo_q_score(nhfp, &svq.quest_status, "gamestate-quest_status");
for (i = 0; i < (MAXSPELL + 1); ++i) {
Sfo_spell(nhfp, &svs.spl_book[i], "gamestate-spl_book");
}
save_artifacts(nhfp);
save_oracles(nhfp);
Sfo_char(nhfp, svp.pl_character, "gamestate-pl_character", sizeof svp.pl_character);
Sfo_char(nhfp, svp.pl_fruit, "gamestate-pl_fruit", sizeof svp.pl_fruit);
savefruitchn(nhfp);
savenames(nhfp);
save_msghistory(nhfp);
save_gamelog(nhfp);
save_luadata(nhfp);
if (nhfp->structlevel)
bflush(nhfp->fd);
program_state.saving--;
return;
}
/* potentially called from goto_level(do.c) as well as savestateinlock() */
boolean
tricked_fileremoved(NHFILE *nhfp, char *whynot)
{
if (!nhfp) {
pline1(whynot);
pline("Probably someone removed it.");
Strcpy(svk.killer.name, whynot);
done(TRICKED);
return TRUE;
}
return FALSE;
}
#ifdef INSURANCE
void
savestateinlock(void)
{
int hpid = 0;
char whynot[BUFSZ];
NHFILE *nhfp;
if (!program_state.something_worth_saving || program_state.in_self_recover)
return;
program_state.saving++; /* inhibit status and perm_invent updates */
/* When checkpointing is on, the full state needs to be written
* on each checkpoint. When checkpointing is off, only the pid
* needs to be in the level.0 file, so it does not need to be
* constantly rewritten. When checkpointing is turned off during
* a game, however, the file has to be rewritten once to truncate
* it and avoid restoring from outdated information.
*
* Restricting gh.havestate to this routine means that an additional
* noop pid rewriting will take place on the first "checkpoint" after
* the game is started or restored, if checkpointing is off.
*/
if (flags.ins_chkpt || gh.havestate) {
/* save the rest of the current game state in the lock file,
* following the original int pid, the current level number,
* and the current savefile name, which should not be subject
* to any internal compression schemes since they must be
* readable by an external utility
*/
nhfp = open_levelfile(0, whynot);
if (tricked_fileremoved(nhfp, whynot)) {
program_state.saving--;
return;
}
Sfi_int(nhfp, &hpid, "gamestate-hackpid");
if (svh.hackpid != hpid) {
Sprintf(whynot, "Level #0 pid (%d) doesn't match ours (%d)!",
hpid, svh.hackpid);
goto giveup;
}
close_nhfile(nhfp);
nhfp = create_levelfile(0, whynot);
if (!nhfp) {
pline1(whynot);
giveup:
Strcpy(svk.killer.name, whynot);
/* done(TRICKED) will return when running in wizard mode;
clear the display-update-suppression flag before rather
than after so that screen updating behaves normally;
game data shouldn't be inconsistent yet, unlike it would
become midway through saving */
program_state.saving--;
done(TRICKED);
return;
}
nhfp->mode = WRITING;
Sfo_int(nhfp, &svh.hackpid, "gamestate-hackpid");
if (flags.ins_chkpt) {
int currlev = ledger_no(&u.uz);
Sfo_int(nhfp, &currlev, "gamestate-savestateinlock");
save_savefile_name(nhfp);
store_version(nhfp);
store_plname_in_file(nhfp);
/* if ball and/or chain aren't on floor or in invent, keep a copy
of their pointers; not valid when on floor or in invent */
gl.looseball = BALL_IN_MON ? uball : 0;
gl.loosechain = CHAIN_IN_MON ? uchain : 0;
savegamestate(nhfp);
}
close_nhfile(nhfp);
}
program_state.saving--;
gh.havestate = flags.ins_chkpt;
return;
}
#endif
void
savelev(NHFILE *nhfp, xint8 lev)
{
boolean set_uz_save = (gu.uz_save.dnum == 0 && gu.uz_save.dlevel == 0);
/* caller might have already set up gu.uz_save and zeroed u.uz;
if not, we need to set it for save_bubbles(); caveat: if the
player quits during character selection, u.uz won't be set yet
but we'll be called during run-down */
if (set_uz_save && (nhfp->mode & (COUNTING | WRITING))) {
if (u.uz.dnum == 0 && u.uz.dlevel == 0) {
program_state.something_worth_saving = 0;
panic("savelev: where are we?");
}
gu.uz_save = u.uz;
}
savelev_core(nhfp, lev);
if (set_uz_save)
gu.uz_save.dnum = gu.uz_save.dlevel = 0; /* unset */
}
staticfn void
savelev_core(NHFILE *nhfp, xint8 lev)
{
#ifdef TOS
short tlev;
#endif
int i, c, r;
long timestamp;
coord *tmpc;
program_state.saving++; /* even if current mode is FREEING */
if (!nhfp)
panic("Save on bad file!"); /* impossible */
/*
* Level file contents:
* version info (handled by caller);
* save file info (compression type; also by caller);
* process ID;
* internal level number (ledger number);
* bones info;
* actual level data.
*
* If we're tearing down the current level without saving anything
* (which happens at end of game or upon entrance to endgame or
* after an aborted restore attempt) then we don't want to do any
* actual I/O. So when only freeing, we skip to the bones info
* portion (which has some freeing to do), then jump quite a bit
* further ahead to the middle of the 'actual level data' portion.
*/
if (nhfp->mode != FREEING) {
/* WRITING (probably ORed with FREEING), or COUNTING */
/* purge any dead monsters (necessary if we're starting
a panic save rather than a normal one, or sometimes
when changing levels without taking time -- e.g.
create statue trap then immediately level teleport) */
if (iflags.purge_monsters)
dmonsfree();
/* clear objs_deleted list too */
if (go.objs_deleted)
dobjsfree(); /* really free deleted objects */
if (lev >= 0 && lev <= maxledgerno())
svl.level_info[lev].flags |= VISITED;
Sfo_int(nhfp, &svh.hackpid, "gamestate-hackpid");
Sfo_xint8(nhfp, &lev, "gamestate-dlvl");
}
/* bones info comes before level data; the intent is for an external
program ('hearse') to be able to match a bones file with the
corresponding log file entry--or perhaps just skip that?--without
the guessing that was needed in 3.4.3 and without having to
interpret level data to find where to start; unfortunately it
still needs to handle all the data compression schemes */
savecemetery(nhfp, &svl.level.bonesinfo);
if (nhfp->mode == FREEING) /* see above */
goto skip_lots;
savelevl(nhfp);
for (c = 0; c < COLNO; ++c) {
for (r = 0; r < ROWNO; ++r) {
Sfo_schar(nhfp, &svl.lastseentyp[c][r], "lastseentyp");
}
}
/* preserve idle time when copying levels; match getlev()'s
monster catch-up guard */
timestamp = (!u.uz.dlevel || program_state.restoring == REST_LEVELS)
? svo.omoves : svm.moves;
Sfo_long(nhfp, ×tamp, "lev-timestmp");
save_stairs(nhfp);
Sfo_dest_area(nhfp, &svu.updest, "lev-updest");
Sfo_dest_area(nhfp, &svd.dndest, "lev-dndest");
save_adjust_levelflags(timestamp);
Sfo_levelflags(nhfp, &svl.level.flags, "lev-level_flags");
rest_adjust_levelflags(svm.moves - timestamp, FALSE);
Sfo_int(nhfp, &svd.doors_alloc, "lev-doors_alloc");
/* don't rely on underlying write() behavior to write
* nothing if count arg is 0, just skip it */
if (svd.doors_alloc) {
tmpc = svd.doors;
for (i = 0; i < svd.doors_alloc; ++i) {
Sfo_nhcoord(nhfp, tmpc, "lev-doors");
tmpc++;
}
}
save_rooms(nhfp); /* no dynamic memory to reclaim */
/* from here on out, saving also involves allocated memory cleanup */
skip_lots:
/* timers and lights must be saved before monsters and objects */
save_timers(nhfp, RANGE_LEVEL);
save_light_sources(nhfp, RANGE_LEVEL);
savemonchn(nhfp, fmon);
save_worm(nhfp); /* save worm information */
savetrapchn(nhfp, gf.ftrap);
saveobjchn(nhfp, &fobj);
saveobjchn(nhfp, &svl.level.buriedobjlist);
saveobjchn(nhfp, &gb.billobjs);
save_engravings(nhfp);
savedamage(nhfp); /* pending shop wall and/or floor repair */
save_regions(nhfp);
save_bubbles(nhfp, lev); /* for water and air */
save_exclusions(nhfp);
save_track(nhfp);
if (nhfp->mode != FREEING) {
if (nhfp->structlevel)
bflush(nhfp->fd);
}
program_state.saving--;
if (release_data(nhfp)) {
clear_level_structures();
gf.ftrap = 0;
gb.billobjs = 0;
(void) memset(svr.rooms, 0, sizeof(svr.rooms));
}
return;
}
void
save_adjust_levelflags(long timestamp)
{
/* use the same time base as lev-timestmp */
svl.level.flags.stasis_until -= timestamp;
}
staticfn void
savelevl(NHFILE *nhfp)
{
int x, y;
for (x = 0; x < COLNO; x++) {
for (y = 0; y < ROWNO; y++) {
Sfo_rm(nhfp, &levl[x][y], "location-rm");
}
}
return;
}
/* save Plane of Water's air bubbles and Plane of Air's clouds */
staticfn void
save_bubbles(NHFILE *nhfp, xint8 lev)
{
xint8 bbubbly;
/* air bubbles and clouds used to be saved as part of game state
because restoring them needs dungeon data that isn't available
during the first pass of their levels; now that they are part of
the current level instead, we write a zero or non-zero marker
so that restore can determine whether they are present even when
u.uz and ledger_no() aren't available to it yet */
bbubbly = 0;
if (lev == ledger_no(&water_level) || lev == ledger_no(&air_level))
bbubbly = lev; /* non-zero */
if (update_file(nhfp))
Sfo_xint8(nhfp, &bbubbly, "bubbles-bbubbly");
#if 0
if (nhfp->structlevel)
xxwrite(nhfp->fd, (genericptr_t) &bbubbly, sizeof bbubbly);
#endif
if (bbubbly)
save_waterlevel(nhfp); /* save air bubbles or clouds */
}
/* used when saving a level and also when saving dungeon overview data */
void
savecemetery(NHFILE *nhfp, struct cemetery **cemeteryaddr)
{
struct cemetery *thisbones, *nextbones;
int flag;
flag = *cemeteryaddr ? 0 : -1;
if (update_file(nhfp)) {
Sfo_int(nhfp, &flag, "cemetery-cemetery_flag");
}
nextbones = *cemeteryaddr;
while ((thisbones = nextbones) != 0) {
nextbones = thisbones->next;
if (update_file(nhfp)) {
Sfo_cemetery(nhfp, thisbones, "cemetery-bonesinfo");
}
if (release_data(nhfp))
free((genericptr_t) thisbones);
}
if (release_data(nhfp))
*cemeteryaddr = 0;
}
staticfn void
savedamage(NHFILE *nhfp)
{
struct damage *damageptr, *tmp_dam;
unsigned int xl = 0;
damageptr = svl.level.damagelist;
for (tmp_dam = damageptr; tmp_dam; tmp_dam = tmp_dam->next)
xl++;
if (update_file(nhfp)) {
Sfo_unsigned(nhfp, &xl, "damage-damage_count");
}
while (damageptr) {
if (update_file(nhfp)) {
Sfo_damage(nhfp, damageptr, "damage");
}
tmp_dam = damageptr;
damageptr = damageptr->next;
if (release_data(nhfp))
free((genericptr_t) tmp_dam);
}
if (release_data(nhfp))
svl.level.damagelist = 0;
}
staticfn void
save_stairs(NHFILE *nhfp)
{
stairway *stway = gs.stairs;
int buflen = (int) sizeof *stway;
while (stway) {
if (update_file(nhfp)) {
boolean use_relative = (program_state.restoring != REST_GSTATE
&& stway->tolev.dnum == u.uz.dnum);
if (use_relative) {
/* make dlevel relative to current level */
stway->tolev.dlevel -= u.uz.dlevel;
}
Sfo_int(nhfp, &buflen, "stairs-staircount");
Sfo_stairway(nhfp, stway, "stairs-stairway");
if (use_relative) {
/* reset stairway dlevel back to absolute */
stway->tolev.dlevel += u.uz.dlevel;
}
}
stway = stway->next;
}
if (update_file(nhfp)) {
buflen = -1;
Sfo_int(nhfp, &buflen, "stairs-staircount");
}
}
/* if ball and/or chain are loose, make an object chain for it/them and
save that separately from other objects */
staticfn void
save_bc(NHFILE *nhfp)
{
struct obj *bc_objs = 0;
/* save ball and chain if they are currently dangling free (i.e. not
on floor or in inventory); 'looseball' and 'loosechain' have been
set up in caller because ball and chain will be gone by now if on
floor, or ball gone if carried */
if (gl.loosechain) {
gl.loosechain->nobj = bc_objs; /* uchain */
bc_objs = gl.loosechain;
if (nhfp->mode & FREEING) {
setworn((struct obj *) 0, W_CHAIN); /* sets 'uchain' to Null */
gl.loosechain = (struct obj *) 0;
}
}
if (gl.looseball) {
gl.looseball->nobj = bc_objs;
bc_objs = gl.looseball;
if (nhfp->mode & FREEING) {
setworn((struct obj *) 0, W_BALL); /* sets 'uball' to Null */
gl.looseball = (struct obj *) 0;
}
}
saveobjchn(nhfp, &bc_objs); /* frees objs in list, sets bc_objs to Null */
}
/* save one object;
caveat: this is only for update_file(); caller handles release_data() */
staticfn void
saveobj(NHFILE *nhfp, struct obj *otmp)
{
int buflen, zerobuf = 0;
buflen = (int) sizeof (struct obj);
Sfo_int(nhfp, &buflen, "obj-obj_length");
Sfo_obj(nhfp, otmp, "obj");
if (otmp->oextra) {
buflen = ONAME(otmp) ? (int) strlen(ONAME(otmp)) + 1 : 0;
Sfo_int(nhfp, &buflen, "obj-oname_length");
if (buflen > 0) {
Sfo_char(nhfp, ONAME(otmp), "obj-oname", buflen);
}
/* defer to savemon() for this one */
if (OMONST(otmp)) {
savemon(nhfp, OMONST(otmp));
} else {
Sfo_int(nhfp, &zerobuf, "obj-omonst_length");
}
/* extra info about scroll of mail */
buflen = OMAILCMD(otmp) ? (int) strlen(OMAILCMD(otmp)) + 1 : 0;
Sfo_int(nhfp, &buflen, "obj-omailcmd_length");
if (buflen > 0) {
Sfo_char(nhfp, OMAILCMD(otmp), "obj-omailcmd", buflen);
}
/* omid used to be indirect via a pointer in oextra but has
become part of oextra itself; 0 means not applicable and
gets saved/restored whenever any other oextra components do */
Sfo_unsigned(nhfp, &OMID(otmp), "obj-omid");
}
}
/* save an object chain; sets head of list to Null when done;
handles release_data() for each object in the list */
staticfn void
saveobjchn(NHFILE *nhfp, struct obj **obj_p)
{
struct obj *otmp = *obj_p;
struct obj *otmp2;
boolean is_invent = (otmp && otmp == gi.invent);
int minusone = -1;
while (otmp) {
otmp2 = otmp->nobj;
if (update_file(nhfp)) {
saveobj(nhfp, otmp);
}
if (Has_contents(otmp))
saveobjchn(nhfp, &otmp->cobj);
if (release_data(nhfp)) {
/*
* If these are on the floor, the discarding could be
* due to game save, or we could just be changing levels.
* Always invalidate the pointer, but ensure that we have
* the o_id in order to restore the pointer on reload.
*/
if (otmp == svc.context.victual.piece) {
svc.context.victual.o_id = otmp->o_id;
svc.context.victual.piece = (struct obj *) 0;
}
if (otmp == svc.context.tin.tin) {
svc.context.tin.o_id = otmp->o_id;
svc.context.tin.tin = (struct obj *) 0;
}
if (otmp == svc.context.spbook.book) {
svc.context.spbook.o_id = otmp->o_id;
svc.context.spbook.book = (struct obj *) 0;
}
otmp->where = OBJ_FREE; /* set to free so dealloc will work */
otmp->nobj = NULL; /* nobj saved into otmp2 */
otmp->cobj = NULL; /* contents handled above */
otmp->timed = 0; /* not timed any more */
otmp->lamplit = 0; /* caller handled lights */
otmp->leashmon = 0; /* mon->mleashed could still be set;
* unfortunately, we can't clear that
* until after the monster is saved */
/* clear 'uball' and 'uchain' pointers if resetting their mask;
could also do same for other worn items but don't need to */
if ((otmp->owornmask & (W_BALL | W_CHAIN)) != 0)
setworn((struct obj *) 0,
otmp->owornmask & (W_BALL | W_CHAIN));
otmp->owornmask = 0L; /* no longer care */
program_state.freeingdata++;
dealloc_obj(otmp);
program_state.freeingdata--;
}
otmp = otmp2;
}
if (update_file(nhfp)) {
Sfo_int(nhfp, &minusone, "obj-obj_length");
}
if (release_data(nhfp)) {
if (is_invent)
allunworn(); /* clear uwep, uarm, uball, &c pointers */
*obj_p = (struct obj *) 0;
}
}
staticfn void
savemon(NHFILE *nhfp, struct monst *mtmp)
{
int buflen;
mtmp->mtemplit = 0; /* normally clear; if set here then a panic save
* is being written while bhit() was executing */
buflen = (int) sizeof (struct monst);
Sfo_int(nhfp, &buflen, "monst-monst_length");
Sfo_monst(nhfp, mtmp, "monst");
if (mtmp->mextra) {
buflen = MGIVENNAME(mtmp) ? (int) strlen(MGIVENNAME(mtmp)) + 1 : 0;
Sfo_int(nhfp, &buflen, "monst-mgivenname_length");
if (buflen > 0) {
Sfo_char(nhfp, MGIVENNAME(mtmp), "monst-mgivenname", buflen);
}
buflen = EGD(mtmp) ? (int) sizeof (struct egd) : 0;
Sfo_int(nhfp, &buflen, "monst-egd_length");
if (buflen > 0) {
Sfo_egd(nhfp, EGD(mtmp), "monst-egd");
}
buflen = EPRI(mtmp) ? (int) sizeof (struct epri) : 0;
Sfo_int(nhfp, &buflen, "monst-epri_length");
if (buflen > 0) {
Sfo_epri(nhfp, EPRI(mtmp), "monst-epri");
}
buflen = ESHK(mtmp) ? (int) sizeof (struct eshk) : 0;
Sfo_int(nhfp, &buflen, "monst-eshk_length");
if (buflen > 0) {
Sfo_eshk(nhfp, ESHK(mtmp), "monst-eshk");
}
buflen = EMIN(mtmp) ? (int) sizeof (struct emin) : 0;
Sfo_int(nhfp, &buflen, "monst-emin_length");
if (buflen > 0) {
Sfo_emin(nhfp, EMIN(mtmp), "monst-emin");
}
buflen = EDOG(mtmp) ? (int) sizeof (struct edog) : 0;
Sfo_int(nhfp, &buflen, "monst-edog_length");
if (buflen > 0) {
/* we only store relative times in save and bones */
moves_to_relative_time(&EDOG(mtmp)->droptime);
moves_to_relative_time(&EDOG(mtmp)->hungrytime);
Sfo_edog(nhfp, EDOG(mtmp), "monst-edog");
relative_time_to_moves(&EDOG(mtmp)->droptime);
relative_time_to_moves(&EDOG(mtmp)->hungrytime);
}
buflen = EBONES(mtmp) ? (int) sizeof (struct ebones) : 0;
Sfo_int(nhfp, &buflen, "monst-ebones_length");
if (buflen > 0) {
Sfo_ebones(nhfp, EBONES(mtmp), "monst-ebones");
}
/* mcorpsenm is inline int rather than pointer to something,
so doesn't need to be preceded by a length field */
buflen = (int) MCORPSENM(mtmp);
Sfo_int(nhfp, &buflen, "monst-mcorpsenm");
}
}
staticfn void
savemonchn(NHFILE *nhfp, struct monst *mtmp)
{
struct monst *mtmp2;
int minusone = -1;
while (mtmp) {
mtmp2 = mtmp->nmon;
if (update_file(nhfp)) {
mtmp->mnum = monsndx(mtmp->data);
if (mtmp->ispriest)
forget_temple_entry(mtmp); /* EPRI() */
savemon(nhfp, mtmp);
}
if (mtmp->minvent)
saveobjchn(nhfp, &mtmp->minvent);
if (release_data(nhfp)) {
if (mtmp == svc.context.polearm.hitmon) {
svc.context.polearm.m_id = mtmp->m_id;
svc.context.polearm.hitmon = NULL;
}
if (mtmp == u.ustuck)
u.ustuck_mid = u.ustuck->m_id;
if (mtmp == u.usteed)
u.usteed_mid = u.usteed->m_id;
mtmp->nmon = NULL; /* nmon saved into mtmp2 */
dealloc_monst(mtmp);
}
mtmp = mtmp2;
}
if (update_file(nhfp)) {
Sfo_int(nhfp, &minusone, "monst-monst_length");
}
}
/* save traps; gf.ftrap is the only trap chain so 2nd arg is superfluous */
staticfn void
savetrapchn(NHFILE *nhfp, struct trap *trap)
{
static struct trap zerotrap;
struct trap *trap2;
while (trap) {
boolean use_relative = (program_state.restoring != REST_GSTATE
&& trap->dst.dnum == u.uz.dnum);
trap2 = trap->ntrap;
if (use_relative)
trap->dst.dlevel -= u.uz.dlevel; /* make it relative */
if (update_file(nhfp)) {
Sfo_trap(nhfp, trap, "trap");
}
if (use_relative)
trap->dst.dlevel += u.uz.dlevel; /* reset back to absolute */
if (release_data(nhfp))
dealloc_trap(trap);
trap = trap2;
}
if (update_file(nhfp)) {
Sfo_trap(nhfp, &zerotrap, "trap");
}
}
/* save all the fruit names and ID's; this is used only in saving whole games
* (not levels) and in saving bones levels. When saving a bones level,
* we only want to save the fruits which exist on the bones level; the bones
* level routine marks nonexistent fruits by making the fid negative.
*/
void
savefruitchn(NHFILE *nhfp)
{
static struct fruit zerofruit;
struct fruit *f2, *f1;
f1 = gf.ffruit;
while (f1) {
f2 = f1->nextf;
if (f1->fid >= 0 && update_file(nhfp)) {
Sfo_fruit(nhfp, f1, "fruit");
}
if (release_data(nhfp))
dealloc_fruit(f1);
f1 = f2;