-
Notifications
You must be signed in to change notification settings - Fork 635
Expand file tree
/
Copy pathrestore.c
More file actions
1688 lines (1511 loc) · 50.6 KB
/
Copy pathrestore.c
File metadata and controls
1688 lines (1511 loc) · 50.6 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 restore.c $NHDT-Date: 1781973064 2026/06/20 16:31:04 $ $NHDT-Branch: NetHack-5.0 $:$NHDT-Revision: 1.265 $ */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/*-Copyright (c) Michael Allison, 2009. */
/* NetHack may be freely redistributed. See license for details. */
#include "hack.h"
#include "tcap.h" /* for TERMLIB and ASCIIGRAPH */
#if defined(MICRO)
extern int dotcnt; /* shared with save */
extern int dotrow; /* shared with save */
#endif
staticfn void find_lev_obj(void);
staticfn void restlevchn(NHFILE *);
staticfn void restdamage(NHFILE *);
staticfn void restobj(NHFILE *, struct obj *);
staticfn struct obj *restobjchn(NHFILE *, boolean);
staticfn void restmon(NHFILE *, struct monst *);
staticfn struct monst *restmonchn(NHFILE *);
staticfn struct fruit *loadfruitchn(NHFILE *);
staticfn void freefruitchn(struct fruit *);
staticfn void rest_levl(NHFILE *);
staticfn void rest_stairs(NHFILE *);
#ifndef SFCTOOL
staticfn void ghostfruit(struct obj *);
staticfn boolean restgamestate(NHFILE *);
staticfn void restlevelstate(void);
staticfn int restlevelfile(xint8);
staticfn void rest_bubbles(NHFILE *);
staticfn void restore_gamelog(NHFILE *);
staticfn void reset_oattached_mids(boolean);
/* these ones are declared non-static in extern.h if SFCTOOL is defined */
staticfn boolean restgamestate(NHFILE *);
staticfn void rest_bubbles(NHFILE *);
staticfn void restore_gamelog(NHFILE *);
staticfn void restore_msghistory(NHFILE *);
#endif
/*
* Save a mapping of IDs from ghost levels to the current level. This
* map is used by the timer routines when restoring ghost levels.
*/
#define N_PER_BUCKET 64
struct bucket {
struct bucket *next;
struct {
unsigned gid; /* ghost ID */
unsigned nid; /* new ID */
} map[N_PER_BUCKET];
};
#ifndef SFCTOOL
staticfn void clear_id_mapping(void);
staticfn void add_id_mapping(unsigned, unsigned);
#endif /* SFCTOOL */
#ifdef AMII_GRAPHICS
void amii_setpens(int); /* use colors from save file */
extern int amii_numcolors;
#endif
#include "display.h"
#define Is_IceBox(o) ((o)->otyp == ICE_BOX ? TRUE : FALSE)
#ifndef SFCTOOL
/* Recalculate svl.level.objects[x][y], since this info was not saved. */
staticfn void
find_lev_obj(void)
{
struct obj *fobjtmp = (struct obj *) 0;
struct obj *otmp;
int x, y;
for (x = 0; x < COLNO; x++)
for (y = 0; y < ROWNO; y++)
svl.level.objects[x][y] = (struct obj *) 0;
/*
* Reverse the entire fobj chain, which is necessary so that we can
* place the objects in the proper order. Make all obj in chain
* OBJ_FREE so place_object will work correctly.
*/
while ((otmp = fobj) != 0) {
fobj = otmp->nobj;
otmp->nobj = fobjtmp;
otmp->where = OBJ_FREE;
fobjtmp = otmp;
}
/* fobj should now be empty */
/* Set svl.level.objects (as well as reversing the chain back again) */
while ((otmp = fobjtmp) != 0) {
fobjtmp = otmp->nobj;
place_object(otmp, otmp->ox, otmp->oy);
/* fixup(s) performed when restoring the level that the hero
is on, rather than just an arbitrary one */
if (u.uz.dlevel) { /* 0 during full restore until current level */
/* handle uchain and uball when they're on the floor */
if (otmp->owornmask & (W_BALL | W_CHAIN))
setworn(otmp, otmp->owornmask);
}
}
}
/* Things that were marked "in_use" when the game was saved (ex. via the
* infamous "HUP" cheat) get used up here.
*/
void
inven_inuse(boolean quietly)
{
struct obj *otmp, *otmp2;
for (otmp = gi.invent; otmp; otmp = otmp2) {
otmp2 = otmp->nobj;
if (otmp->in_use) {
if (!quietly)
pline("Finishing off %s...", xname(otmp));
useup(otmp);
}
}
}
#endif /* SFCTOOL */
staticfn void
restlevchn(NHFILE *nhfp)
{
int cnt = 0;
s_level *tmplev, *x;
svs.sp_levchn = (s_level *) 0;
Sfi_int(nhfp, &cnt, "levchn-lev_count");
for (; cnt > 0; cnt--) {
tmplev = (s_level *) alloc(sizeof(s_level));
Sfi_s_level(nhfp, tmplev, "levchn-s_level");
if (!svs.sp_levchn)
svs.sp_levchn = tmplev;
else {
for (x = svs.sp_levchn; x->next; x = x->next)
;
x->next = tmplev;
}
tmplev->next = (s_level *) 0;
}
}
staticfn void
restdamage(NHFILE *nhfp)
{
unsigned int dmgcount = 0;
int counter;
struct damage *tmp_dam;
#ifndef SFCTOOL
boolean ghostly = (nhfp->ftype == NHF_BONESFILE);
#endif
Sfi_unsigned(nhfp, &dmgcount, "damage-damage_count");
counter = (int) dmgcount;
if (!counter)
return;
do {
tmp_dam = (struct damage *) alloc(sizeof *tmp_dam);
Sfi_damage(nhfp, tmp_dam, "damage");
#ifndef SFCTOOL
if (ghostly)
tmp_dam->when += (svm.moves - svo.omoves);
tmp_dam->next = svl.level.damagelist;
svl.level.damagelist = tmp_dam;
#endif /* !SFCTOOL */
} while (--counter > 0);
}
/* restore one object */
staticfn void
restobj(NHFILE *nhfp, struct obj *otmp)
{
int buflen = 0;
unsigned omid = 0;
Sfi_obj(nhfp, otmp, "obj");
otmp->lua_ref_cnt = 0;
/* next object pointers are invalid; otmp->cobj needs to be left
as is--being non-null is key to restoring container contents */
otmp->nobj = otmp->nexthere = (struct obj *) 0;
/* non-null oextra needs to be reconstructed */
if (otmp->oextra) {
otmp->oextra = newoextra();
/* oname - object's name */
Sfi_int(nhfp, &buflen, "obj-oname_length");
if (buflen > 0) { /* includes terminating '\0' */
new_oname(otmp, buflen);
Sfi_char(nhfp, ONAME(otmp), "obj-oname", buflen);
}
/* omonst - corpse or statue might retain full monster details */
Sfi_int(nhfp, &buflen, "obj-omonst_length");
if (buflen > 0) {
newomonst(otmp);
/* this is actually a monst struct, so we
can just defer to restmon() here */
restmon(nhfp, OMONST(otmp));
}
/* omailcmd - feedback mechanism for scroll of mail */
Sfi_int(nhfp, &buflen, "obj-omailcmd_length");
if (buflen > 0) {
char *omailcmd = (char *) alloc(buflen);
Sfi_char(nhfp, omailcmd, "obj-omailcmd", buflen);
new_omailcmd(otmp, omailcmd);
free((genericptr_t) omailcmd);
}
/* omid - monster id number, connecting corpse to ghost */
newomid(otmp); /* superfluous; we're already allocated otmp->oextra */
Sfi_unsigned(nhfp, &omid, "obj-omid");
OMID(otmp) = omid;
}
}
staticfn struct obj *
restobjchn(NHFILE *nhfp, boolean frozen)
{
struct obj *otmp, *otmp2 = 0;
struct obj *first = (struct obj *) 0;
int buflen = 0;
#ifndef SFCTOOL
boolean ghostly = (nhfp->ftype == NHF_BONESFILE);
#endif
while (1) {
Sfi_int(nhfp, &buflen, "obj-obj_length");
if (buflen == -1)
break;
otmp = newobj();
assert(otmp != 0);
restobj(nhfp, otmp);
if (!first)
first = otmp;
else
otmp2->nobj = otmp;
#ifndef SFCTOOL
if (ghostly) {
unsigned nid = next_ident();
add_id_mapping(otmp->o_id, nid);
otmp->o_id = nid;
}
if (ghostly && otmp->otyp == SLIME_MOLD)
ghostfruit(otmp);
/* Ghost levels get object age shifted from old player's clock
* to new player's clock. Assumption: new player arrived
* immediately after old player died.
*/
if (ghostly && !frozen && !age_is_relative(otmp))
otmp->age = svm.moves - svo.omoves + otmp->age;
#endif /* !SFCTOOL */
/* get contents of a container or statue */
if (Has_contents(otmp)) {
struct obj *otmp3;
otmp->cobj = restobjchn(nhfp, Is_IceBox(otmp));
/* restore container back pointers */
for (otmp3 = otmp->cobj; otmp3; otmp3 = otmp3->nobj)
otmp3->ocontainer = otmp;
}
#ifndef SFCTOOL
if (otmp->bypass)
otmp->bypass = 0;
if (!ghostly) {
/* fix the pointers */
if (otmp->o_id == svc.context.victual.o_id)
svc.context.victual.piece = otmp;
if (otmp->o_id == svc.context.tin.o_id)
svc.context.tin.tin = otmp;
if (otmp->o_id == svc.context.spbook.o_id)
svc.context.spbook.book = otmp;
}
#endif /* !SFADUSTER */
otmp2 = otmp;
}
if (first && otmp2->nobj) {
impossible("Restobjchn: error reading objchn.");
otmp2->nobj = 0;
}
#ifdef SFCTOOL
nhUse(frozen);
#endif
return first;
}
/* restore one monster */
staticfn void
restmon(NHFILE *nhfp, struct monst *mtmp)
{
int buflen = 0, mc = 0;
Sfi_monst(nhfp, mtmp, "monst");
/* next monster pointer is invalid */
mtmp->nmon = (struct monst *) 0;
/* non-null mextra needs to be reconstructed */
if (mtmp->mextra) {
mtmp->mextra = newmextra();
/* mgivenname - monster's name */
Sfi_int(nhfp, &buflen, "monst-mgivenname_length");
if (buflen > 0) { /* includes terminating '\0' */
new_mgivenname(mtmp, buflen);
Sfi_char(nhfp, MGIVENNAME(mtmp), "monst-mgivenname", (int) buflen);
}
/* egd - vault guard */
Sfi_int(nhfp, &buflen, "monst-egd_length");
if (buflen > 0) {
newegd(mtmp);
Sfi_egd(nhfp, EGD(mtmp), "monst-egd");
}
/* epri - temple priest */
Sfi_int(nhfp, &buflen, "monst-epri_length");
if (buflen > 0) {
newepri(mtmp);
Sfi_epri(nhfp, EPRI(mtmp), "monst-epri");
}
/* eshk - shopkeeper */
Sfi_int(nhfp, &buflen, "monst-eshk_length");
if (buflen > 0) {
neweshk(mtmp);
Sfi_eshk(nhfp, ESHK(mtmp), "monst-eshk");
}
/* emin - minion */
Sfi_int(nhfp, &buflen, "monst-emin_length");
if (buflen > 0) {
newemin(mtmp);
Sfi_emin(nhfp, EMIN(mtmp), "monst-emin");
}
/* edog - pet */
Sfi_int(nhfp, &buflen, "monst-edog_length");
if (buflen > 0) {
newedog(mtmp);
Sfi_edog(nhfp, EDOG(mtmp), "monst-edog");
/* save or bones held a relative time */
relative_time_to_moves(&EDOG(mtmp)->droptime);
relative_time_to_moves(&EDOG(mtmp)->hungrytime);
/* sanity check to prevent rn2(0) */
if (EDOG(mtmp)->apport <= 0) {
EDOG(mtmp)->apport = 1;
}
}
/* ebones */
Sfi_int(nhfp, &buflen, "monst-ebones_length");
if (buflen > 0) {
newebones(mtmp);
Sfi_ebones(nhfp, EBONES(mtmp), "monst-ebones");
}
/* mcorpsenm - obj->corpsenm for mimic posing as corpse or
statue (inline int rather than pointer to something) */
Sfi_int(nhfp, &mc, "monst-mcorpsenm");
MCORPSENM(mtmp) = mc;
} /* mextra */
}
staticfn struct monst *
restmonchn(NHFILE *nhfp)
{
struct monst *mtmp, *mtmp2 = 0;
struct monst *first = (struct monst *) 0;
int buflen = 0;
#ifndef SFCTOOL
int offset;
boolean ghostly = (nhfp->ftype == NHF_BONESFILE);
#endif
while (1) {
Sfi_int(nhfp, &buflen, "monst-monst_length");
if (buflen == -1)
break;
mtmp = newmonst();
assert(mtmp != 0);
restmon(nhfp, mtmp);
if (!first)
first = mtmp;
else
mtmp2->nmon = mtmp;
#ifndef SFCTOOL
if (ghostly) {
unsigned nid = next_ident();
add_id_mapping(mtmp->m_id, nid);
mtmp->m_id = nid;
}
offset = mtmp->mnum;
mtmp->data = &mons[offset];
if (ghostly) {
int mndx = (mtmp->cham == NON_PM) ? monsndx(mtmp->data)
: mtmp->cham;
if (propagate(mndx, TRUE, ghostly) == 0) {
/* cookie to trigger purge in getbones() */
mtmp->mhpmax = DEFUNCT_MONSTER;
}
}
#endif /* !SFCTOOL */
if (mtmp->minvent) {
struct obj *obj;
mtmp->minvent = restobjchn(nhfp, FALSE);
#ifndef SFCTOOL
/* restore monster back pointer */
for (obj = mtmp->minvent; obj; obj = obj->nobj)
obj->ocarry = mtmp;
#else
nhUse(obj);
#endif
}
#ifndef SFCTOOL
if (mtmp->mw) {
struct obj *obj;
for (obj = mtmp->minvent; obj; obj = obj->nobj)
if (obj->owornmask & W_WEP)
break;
if (obj)
mtmp->mw = obj;
else {
MON_NOWEP(mtmp);
impossible("bad monster weapon restore");
}
}
if (mtmp->isshk)
restshk(mtmp, ghostly);
if (mtmp->ispriest)
restpriest(mtmp, ghostly);
if (mtmp->isgd) {
/* fixup for new bit MON_PARKED added post 5.0.0 */
if (!mtmp->mx && (mtmp->mstate & MON_PARKED) == 0L
&& (mtmp->mstate & MON_MIGRATING) == 0L) {
mtmp->mstate &= ~TERRAIN_FALLOUT_MASK;
mtmp->mstate |= MON_PARKED;
}
}
if (!ghostly) {
if (mtmp->m_id == svc.context.polearm.m_id)
svc.context.polearm.hitmon = mtmp;
}
#endif /* !SFCTOOL */
mtmp2 = mtmp;
}
#ifndef SFCTOOL
if (first && mtmp2->nmon) {
impossible("Restmonchn: error reading monchn.");
mtmp2->nmon = 0;
}
#endif
return first;
}
staticfn struct fruit *
loadfruitchn(NHFILE *nhfp)
{
struct fruit *flist, *fnext;
flist = 0;
for (;;) {
fnext = newfruit();
Sfi_fruit(nhfp, fnext, "fruit");
if (fnext->fid != 0) {
fnext->nextf = flist;
flist = fnext;
} else
break;
}
dealloc_fruit(fnext);
return flist;
}
staticfn void
freefruitchn(struct fruit *flist)
{
struct fruit *fnext;
while (flist) {
fnext = flist->nextf;
dealloc_fruit(flist);
flist = fnext;
}
}
#ifndef SFCTOOL
staticfn void
ghostfruit(struct obj *otmp)
{
struct fruit *oldf;
for (oldf = go.oldfruit; oldf; oldf = oldf->nextf)
if (oldf->fid == otmp->spe)
break;
if (!oldf)
impossible("no old fruit?");
else
otmp->spe = fruitadd(oldf->fname, (struct fruit *) 0);
}
#endif /* !SFCTOOL */
#ifdef SYSCF
#define SYSOPT_CHECK_SAVE_UID sysopt.check_save_uid
#else
#define SYSOPT_CHECK_SAVE_UID TRUE
#endif
#ifndef SFCTOOL
staticfn
#endif
boolean
restgamestate(NHFILE *nhfp)
{
int i;
struct flag newgameflags;
struct context_info newgamecontext; /* all 0, but has some pointers */
struct obj *bc_obj;
char timebuf[15];
unsigned long uid = 0;
#ifndef SFCTOOL
boolean defer_perm_invent, restoring_special;
struct obj *otmp;
#endif
Sfi_ulong(nhfp, &uid, "gamestate-uid");
Sfi_char(nhfp, &svn.nhuuid[0], "nhuuid", sizeof svn.nhuuid);
Sfi_long(nhfp, &svm.moves, "gamestate-moves");
#ifndef SFCTOOL
if (SYSOPT_CHECK_SAVE_UID
&& uid != (unsigned long) getuid()) { /* strange ... */
if (!gc.converted_savefile_loaded)
/* for wizard mode, issue a reminder; for others, treat it
* as an attempt to cheat and refuse to restore this file */
pline("Saved game was not yours.");
if (wizard || gc.converted_savefile_loaded) {
if (gc.converted_savefile_loaded)
gc.converted_savefile_loaded = FALSE;
} else {
return FALSE;
}
}
#endif /* SFCTOOL */
newgamecontext = svc.context; /* copy statically init'd context */
Sfi_context_info(nhfp, &svc.context, "gamestate-context");
relative_time_to_moves(&svc.context.seer_turn);
relative_time_to_moves(&svc.context.digging.lastdigtime);
svc.context.warntype.species = (ismnum(svc.context.warntype.speciesidx))
? &mons[svc.context.warntype.speciesidx]
: (struct permonst *) 0;
/* context.victual.piece, .tin.tin, .spellbook.book, and .polearm.hitmon
are pointers which get set to Null during save and will be recovered
via corresponding o_id or m_id while objs or mons are being restored */
/* we want to be able to revert to command line/environment/config
file option values instead of keeping old save file option values
if partial restore fails and we resort to starting a new game */
newgameflags = flags;
Sfi_flag(nhfp, &flags, "gamestate-flags");
#ifndef SFCTOOL
/* avoid keeping permanent inventory window up to date during restore
(setworn() calls update_inventory); attempting to include the cost
of unpaid items before shopkeeper's bill is available is a no-no;
named fruit names aren't accessible yet either
[3.6.2: moved perm_invent from flags to iflags to keep it out of
save files; retaining the override here is simpler than trying
to figure out where it really belongs now] */
defer_perm_invent = iflags.perm_invent;
iflags.perm_invent = FALSE;
/* wizard and discover are actually flags.debug and flags.explore;
player might be overriding the save file values for them;
in the discover case, we don't want to set that for a normal
game until after the save file has been removed */
iflags.deferred_X = (newgameflags.explore && !discover);
restoring_special = (wizard || discover);
if (newgameflags.debug) {
/* authorized by startup code; wizard mode exists and is allowed */
wizard = TRUE, discover = iflags.deferred_X = FALSE;
} else if (restoring_special) {
/* specified by save file; check authorization now. */
set_playmode();
}
role_init(); /* Reset the initial role, race, gender, and alignment */
#ifdef AMII_GRAPHICS
amii_setpens(amii_numcolors); /* use colors from save file */
#endif
#endif /* !SFCTOOL */
Sfi_long(nhfp, &svw.wreserve, "wreserve");
Sfi_int32(nhfp, &svw.wtreserved, "wtreserved");
Sfi_you(nhfp, &u, "gamestate-you");
gy.youmonst.cham = u.mcham;
#ifndef SFCTOOL
if (restoring_special && iflags.explore_error_flag) {
/* savefile has wizard or explore mode, but player is no longer
authorized to access either; can't downgrade mode any further, so
fail restoration. */
u.uhp = 0;
}
#endif
Sfi_char(nhfp, timebuf, "gamestate-ubirthday", 14);
timebuf[14] = '\0';
ubirthday = time_from_yyyymmddhhmmss(timebuf);
Sfi_long(nhfp, &urealtime.realtime, "gamestate-realtime");
Sfi_char(nhfp, timebuf, "gamestate-start_timing", 14);
timebuf[14] = '\0';
#ifndef SFCTOOL
urealtime.start_timing = time_from_yyyymmddhhmmss(timebuf);
/* current time is the time to use for next urealtime.realtime update */
urealtime.start_timing = getnow();
set_uasmon();
#ifdef CLIPPING
cliparound(u.ux, u.uy);
#endif
if (u.uhp <= 0 && (!Upolyd || u.mh <= 0)) {
u.ux = u.uy = 0; /* affects pline() [hence You()] */
You("were not healthy enough to survive restoration.");
/* wiz1_level.dlevel is used by mklev.c to see if lots of stuff is
* uninitialized, so we only have to set it and not the other stuff.
*/
wiz1_level.dlevel = 0;
u.uz.dnum = 0;
u.uz.dlevel = 1;
/* revert to pre-restore option settings */
iflags.deferred_X = FALSE;
iflags.perm_invent = defer_perm_invent;
flags = newgameflags;
svc.context = newgamecontext;
gy.youmonst = cg.zeromonst;
return FALSE;
}
/* in case hangup save occurred in midst of level change */
assign_level(&u.uz0, &u.uz);
#endif /* !SFCTOOL */
/* this stuff comes after potential aborted restore attempts */
restore_killers(nhfp);
restore_timers(nhfp, RANGE_GLOBAL, 0L);
restore_light_sources(nhfp);
gi.invent = restobjchn(nhfp, FALSE);
/* restore dangling (not on floor or in inventory) ball and/or chain */
bc_obj = restobjchn(nhfp, FALSE);
#ifndef SFCTOOL
while (bc_obj) {
struct obj *nobj = bc_obj->nobj;
bc_obj->nobj = (struct obj *) 0;
if (bc_obj->owornmask)
setworn(bc_obj, bc_obj->owornmask);
bc_obj = nobj;
}
#endif
gm.migrating_objs = restobjchn(nhfp, FALSE);
gm.migrating_mons = restmonchn(nhfp);
for (i = 0; i < NUMMONS; ++i) {
Sfi_mvitals(nhfp, &svm.mvitals[i], "gamestate-mvitals");
}
#ifndef SFCTOOL
/*
* There are some things after this that can have unintended display
* side-effects too early in the game.
* Disable see_monsters() here, re-enable it at the top of moveloop()
*/
gd.defer_see_monsters = TRUE;
/* this comes after inventory has been loaded */
for (otmp = gi.invent; otmp; otmp = otmp->nobj)
if (otmp->owornmask)
setworn(otmp, otmp->owornmask);
/* reset weapon so that player will get a reminder about "bashing"
during next fight when bare-handed or wielding an unconventional
item; for pick-axe, we aren't able to distinguish between having
applied or wielded it, so be conservative and assume the former */
otmp = uwep; /* `uwep' usually init'd by setworn() in loop above */
uwep = 0; /* clear it and have setuwep() reinit */
setuwep(otmp); /* (don't need any null check here) */
if (!uwep || uwep->otyp == PICK_AXE || uwep->otyp == GRAPPLING_HOOK)
gu.unweapon = TRUE;
#endif /* !SFCTOOL */
restore_dungeon(nhfp);
restlevchn(nhfp);
/* hero_seq isn't saved and restored because it can be recalculated */
gh.hero_seq = svm.moves << 3; /* normally handled in moveloop() */
Sfi_q_score(nhfp, &svq.quest_status, "gamestate-quest_status");
for (i = 0; i < (MAXSPELL + 1); ++i) {
Sfi_spell(nhfp, &svs.spl_book[i], "gamestate-spl_book");
}
restore_artifacts(nhfp);
restore_oracles(nhfp);
Sfi_char(nhfp, svp.pl_character,
"gamestate-pl_character", sizeof svp.pl_character);
/* Previous versions had a bug that clobbered pl_character on restore.
Fill it in if it was clobbered. */
if (svp.pl_character[0] == '\0') {
if ((Upolyd ? u.mfemale : flags.female) && gu.urole.name.f)
Strcpy(svp.pl_character, gu.urole.name.f);
else
Strcpy(svp.pl_character, gu.urole.name.m);
}
Sfi_char(nhfp, svp.pl_fruit, "gamestate-pl_fruit", sizeof svp.pl_fruit);
freefruitchn(gf.ffruit); /* clean up fruit(s) made by initoptions() */
gf.ffruit = loadfruitchn(nhfp);
restnames(nhfp);
restore_msghistory(nhfp);
restore_gamelog(nhfp);
restore_luadata(nhfp);
#ifndef SFCTOOL
/* must come after all mons & objs are restored */
relink_timers(FALSE);
relink_light_sources(FALSE);
adj_erinys(u.ualign.abuse);
/* inventory display is now viable */
iflags.perm_invent = defer_perm_invent;
#else
nhUse(bc_obj);
nhUse(newgamecontext);
nhUse(newgameflags);
#endif /* !SFCTOOL */
return TRUE;
}
#ifndef SFCTOOL
/* update game state pointers to those valid for the current level (so we
don't dereference a wild u.ustuck when saving game state, for instance) */
staticfn void
restlevelstate(void)
{
/*
* Note: restoring steed and engulfer/holder/holdee is now handled
* in getlev() and there's nothing left for restlevelstate() to do.
*/
return;
}
/* after getlev(), put current level into a level/lock file;
essential when splitting a save file into individual level files */
/*ARGSUSED*/
staticfn int
restlevelfile(xint8 ltmp)
{
char whynot[BUFSZ];
NHFILE *nhfp = (NHFILE *) 0;
nhfp = create_levelfile(ltmp, whynot);
if (!nhfp) {
/* failed to create a new file; don't attempt to make a panic save */
program_state.something_worth_saving = 0;
panic("restlevelfile: %s", whynot);
}
bufon(nhfp->fd);
nhfp->mode = WRITING | FREEING;
savelev(nhfp, ltmp);
close_nhfile(nhfp);
return 2;
}
/*
* restore_saved_game() prior to this left us at this position in
* the savefile for dorecover():
*
* format indicator (1 byte)
* n = count of critical size list (1 byte)
* n bytes of critical sizes (n bytes)
* version info
* --> plnametmp = player name size (int, 2 bytes)
* player name (PL_NSIZ_PLUS)
* current level (including pets)
* (non-level-based) game state
* other levels
*/
int
dorecover(NHFILE *nhfp)
{
xint8 ltmp = 0;
int rtmp;
char plname[PL_NSIZ_PLUS];
/* suppress map display if some part of the code tries to update that */
program_state.restoring = REST_GSTATE;
get_plname_from_file(nhfp, plname, TRUE);
Snprintf(svp.plname, sizeof(svp.plname), "%s", plname);
/*
* The position in the save file is now here:
*
* format indicator (1 byte)
* n = count of critical size list (1 byte)
* n bytes of critical sizes (n bytes)
* version info
* plnametmp = player name size (int, 2 bytes)
* player name (PL_NSIZ_PLUS)
* --> current level (including pets)
* (non-level-based) game state
* other levels
*/
getlev(nhfp, 0, (xint8) 0);
if (!restgamestate(nhfp)) {
NHFILE *tnhfp = get_freeing_nhfile();
display_nhwindow(WIN_MESSAGE, TRUE);
savelev(tnhfp, 0); /* discard current level */
close_nhfile(tnhfp);
close_nhfile(nhfp);
(void) delete_savefile();
u.usteed_mid = u.ustuck_mid = 0;
program_state.restoring = 0;
return 0;
}
/* after restgamestate() -> restnames() so that 'bases[]' is populated */
init_oclass_probs(); /* recompute go.oclass_prob_totals[] */
restlevelstate();
rtmp = restlevelfile(ledger_no(&u.uz));
if (rtmp < 2)
return rtmp; /* dorecover called recursively */
program_state.restoring = REST_LEVELS;
/* these pointers won't be valid while we're processing the
* other levels, but they'll be reset again by restlevelstate()
* afterwards, and in the meantime at least u.usteed may mislead
* place_monster() on other levels
*/
u.ustuck = (struct monst *) 0;
u.usteed = (struct monst *) 0;
#ifdef MICRO
#ifdef AMII_GRAPHICS
{
extern struct window_procs amii_procs;
if (WINDOWPORT(amii)) {
extern winid WIN_BASE;
clear_nhwindow(WIN_BASE); /* hack until there's a hook for this */
}
}
#else
clear_nhwindow(WIN_MAP);
#endif
clear_nhwindow(WIN_MESSAGE);
You("return to level %d in %s%s.", depth(&u.uz),
svd.dungeons[u.uz.dnum].dname,
flags.debug ? " while in debug mode"
: flags.explore ? " while in explore mode" : "");
curs(WIN_MAP, 1, 1);
dotcnt = 0;
dotrow = 2;
if (!WINDOWPORT(X11))
putstr(WIN_MAP, 0, "Restoring:");
#endif
restoreinfo.mread_flags = 1; /* return despite error */
while (1) {
Sfi_xint8(nhfp, <mp, "gamestate-level_number");
if (nhfp->eof)
break;
getlev(nhfp, 0, ltmp);
#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
rtmp = restlevelfile(ltmp);
if (rtmp < 2)
return rtmp; /* dorecover called recursively */
}
restoreinfo.mread_flags = 0;
rewind_nhfile(nhfp); /* return to beginning of file */
(void) validate(nhfp, (char *) 0, FALSE, 0);
get_plname_from_file(nhfp, plname, TRUE);
Snprintf(svp.plname, sizeof(svp.plname), "%s", plname);
/* not 0 nor REST_GSTATE nor REST_LEVELS */
program_state.restoring = REST_CURRENT_LEVEL;
getlev(nhfp, 0, (xint8) 0);
close_nhfile(nhfp);
restlevelstate();
program_state.something_worth_saving = 1; /* useful data now exists */
if (gu.uplift_needed_rev0_to_rev1 == 1) {
/* they've all been uplifted now */
gu.uplift_needed_rev0_to_rev1 = 0;
}
if (!wizard && !discover)
(void) delete_savefile();
if (Is_rogue_level(&u.uz))
assign_graphics(ROGUESET);
reset_glyphmap(gm_levelchange);
max_rank_sz(); /* to recompute gm.mrank_sz (botl.c) */
if ((uball && !uchain) || (uchain && !uball)) {
impossible("restgamestate: lost ball & chain");
/* poor man's unpunish() */
setworn((struct obj *) 0, W_CHAIN);
setworn((struct obj *) 0, W_BALL);
}
/* in_use processing must be after:
* + The inventory has been read so that freeinv() works.
* + The current level has been restored so billing information
* is available.
*/
inven_inuse(FALSE);
/* Set up the vision internals, after levl[] data is loaded
but before docrt(). */
reglyph_darkroom();
vision_reset();
gv.vision_full_recalc = 1; /* recompute vision (not saved) */
run_timers(); /* expire all timers that have gone off while away */
program_state.restoring = 0; /* affects bot() so clear before docrt() */
#ifdef INSURANCE
/* first checkpoint of the restored session; every level file has been
written and the current level has been read back in, so recover has
something to work with even if the player never changes level */
save_currentstate();
#endif
if (ge.early_raw_messages && !program_state.beyond_savefile_load) {
/*
* We're about to obliterate some potentially important
* startup messages, so give the player a chance to see them.
*/
ge.early_raw_messages = 0;
wait_synch();
}
u.usteed_mid = u.ustuck_mid = 0;
program_state.beyond_savefile_load = 1;
docrt();
clear_nhwindow(WIN_MESSAGE);
/* Success! */
welcome(FALSE);
check_special_room(FALSE);
return 1;
}
#endif /* !SFCTOOL */
staticfn void
rest_stairs(NHFILE *nhfp)
{
int buflen = 0;
stairway stway = UNDEFINED_VALUES;
#ifndef SFCTOOL
stairway *newst;
#endif
#ifndef SFCTOOL
stairway_free_all();
#endif
while (1) {
Sfi_int(nhfp, &buflen, "stairs-staircount");
if (buflen == -1)
break;
Sfi_stairway(nhfp, &stway, "stairs-stairway");
if (program_state.restoring != REST_GSTATE