Source:NetHack 5.0.0/src/sfstruct.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to sfstruct.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/sfstruct.c#line12]], for example.

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

1. /* NetHack 5.0	sfstruct.c	$NHDT-Date: 1606765215 2020/11/30 19:40:15 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.4 $ */
2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3. /*-Copyright (c) Michael Allison, 2025. */
4. /* NetHack may be freely redistributed.  See license for details. */
5. 
6. #include "hack.h"
7. #include "sfprocs.h"
8. 
9. /* #define SFLOGGING */          /* debugging */
10. 
11. staticfn void sfstruct_read_error(void);
12. 
13. /* historical full struct savings */
14. 
15. #ifdef SAVEFILE_DEBUGGING
16. #if defined(__GNUC__)
17. #define DEBUGFORMATSTR64 "%s %s %ld %ld %d\n"
18. #elif defined(_MSC_VER)
19. #define DEBUGFORMATSTR64 "%s %s %lld %ld %d\n"
20. #endif
21. #endif
22. 
23. #ifdef SFLOGGING
24. staticfn void logging_finish(void);
25. #endif
26. 
27. #define SFO_BODY(dt) \
28. {                                                                                   \
29.     bwrite(nhfp->fd, (genericptr_t) d_##dt, sizeof *d_##dt);                        \
30. }
31. 
32. #define SFI_BODY(dt) \
33. {                                                                                   \
34.     if (nhfp->eof) {                                                                \
35.        sfstruct_read_error();                                                       \
36.     }                                                                               \
37.     mread(nhfp->fd, (genericptr_t) d_##dt, sizeof *d_##dt);                         \
38.     if (restoreinfo.mread_flags == -1)                                              \
39.         nhfp->eof = TRUE;                                                           \
40. }
41. 
42. #define SF_A(dtyp) \
43. void historical_sfo_##dtyp(NHFILE *, dtyp *d_##dtyp, const char *);                 \
44. void historical_sfi_##dtyp(NHFILE *, dtyp *d_##dtyp, const char *);                 \
45.                                                                                     \
46. void historical_sfo_##dtyp(NHFILE *nhfp, dtyp *d_##dtyp,                            \
47.                            const char *myname UNUSED)                               \
48.     SFO_BODY(dtyp)                                                                  \
49.                                                                                     \
50. void historical_sfi_##dtyp(NHFILE *nhfp, dtyp *d_##dtyp,                            \
51.                   const char *myname UNUSED)                                        \
52.     SFI_BODY(dtyp)
53. 
54. #define SFO_CBODY(dt)                                                               \
55.     {                                                                               \
56.         norm_ptrs_##dt(d_##dt);                                            \
57.         bwrite(nhfp->fd, (genericptr_t) d_##dt, sizeof *d_##dt);                    \
58.     }
59. 
60. #define SFI_CBODY(dt)                                                               \
61.     {                                                                               \
62.         if (nhfp->eof) {                                                            \
63.             sfstruct_read_error();                                                  \
64.         }                                                                           \
65.         mread(nhfp->fd, (genericptr_t) d_##dt, sizeof *d_##dt);                     \
66.         norm_ptrs_##dt(d_##dt);                                            \
67.         if (restoreinfo.mread_flags == -1)                                          \
68.             nhfp->eof = TRUE;                                                       \
69.     }
70. 
71. #define SF_C(keyw, dtyp) \
72. void historical_sfo_##dtyp(NHFILE *, keyw dtyp *d_##dtyp,                           \
73.                            const char *);                                           \
74. void historical_sfi_##dtyp(NHFILE *, keyw dtyp *d_##dtyp,                           \
75.                            const char *);                                           \
76. extern void norm_ptrs_##dtyp(keyw dtyp *d_##dtyp);                         \
77.                                                                                     \
78. void historical_sfo_##dtyp(NHFILE *nhfp, keyw dtyp *d_##dtyp,                       \
79.                            const char *myname UNUSED)                               \
80.     SFO_CBODY(dtyp)                                                                 \
81.                                                                                     \
82. void historical_sfi_##dtyp(NHFILE *nhfp, keyw dtyp *d_##dtyp,                       \
83.                            const char *myname UNUSED)                               \
84.     SFI_CBODY(dtyp)
85. 
86. #define SF_X(xxx, dtyp) \
87. void historical_sfo_##dtyp(NHFILE *, xxx *d_##dtyp, const char *, int);             \
88. void historical_sfi_##dtyp(NHFILE *, xxx *d_##dtyp, const char *, int);             \
89.                                                                                     \
90. void historical_sfo_##dtyp(NHFILE *nhfp, xxx *d_##dtyp,                             \
91.                            const char *myname UNUSED, int bflen UNUSED)             \
92.     SFO_BODY(dtyp)                                                                  \
93.                                                                                     \
94. void historical_sfi_##dtyp(NHFILE *nhfp, xxx *d_##dtyp,                             \
95.                            const char *myname UNUSED, int bflen UNUSED)             \
96.     SFI_BODY(dtyp)
97. 
98. 
99. #include "sfmacros.h"
100. SF_C(struct, version_info)
101. 
102. void historical_sfo_char(NHFILE *, char *d_char, const char *, int);
103. void historical_sfi_char(NHFILE *, char *d_char, const char *, int);
104. 
105. void
106. historical_sfo_char(NHFILE *nhfp, char *d_char,
107.                     const char *myname UNUSED, int cnt)
108. {
109.     bwrite(nhfp->fd, (genericptr_t) d_char, cnt * sizeof (char));
110. }
111. 
112. void
113. historical_sfi_char(NHFILE *nhfp, char *d_char,
114.                     const char *myname UNUSED, int cnt)
115. {
116.     mread(nhfp->fd, (genericptr_t) d_char, cnt * sizeof (char));
117.     if (restoreinfo.mread_flags == -1)
118.         nhfp->eof = TRUE;
119. }
120. //extern void sfo_genericptr(NHFILE *, void **, const char *);
121. //extern void sfi_genericptr(NHFILE *, void **, const char *);
122. //extern void sfo_x_genericptr(NHFILE *, void **, const char *);
123. //extern void sfi_x_genericptr(NHFILE *, void **, const char *);
124. 
125. void historical_sfo_genericptr_t(NHFILE *, genericptr_t *d_genericptr_t,
126.                                  const char *);
127. void historical_sfi_genericptr_t(NHFILE *, genericptr_t *d_genericptr_t,
128.                                  const char *);
129. void
130. historical_sfo_genericptr_t(NHFILE *nhfp, genericptr_t *d_genericptr_t,
131.                             const char *myname UNUSED)
132. {
133.     bwrite(nhfp->fd, (genericptr_t) d_genericptr_t, sizeof *d_genericptr_t);
134. }
135. void
136. historical_sfi_genericptr_t(NHFILE *nhfp, genericptr_t *d_genericptr_t,
137.                             const char *myname UNUSED)
138. {
139.     if (nhfp->eof) {
140.         sfstruct_read_error();
141.     }
142.     mread(nhfp->fd, (genericptr_t) d_genericptr_t, sizeof *d_genericptr_t);
143.     if (restoreinfo.mread_flags == -1)
144.         nhfp->eof = TRUE;
145. }
146. 
147. SF_X(uint8_t, bitfield)
148. 
149. struct sf_structlevel_procs historical_sfo_procs = {
150.     "",
151.     /* sf */
152.     {
153.         historical_sfo_arti_info,
154.         historical_sfo_nhrect,
155.         historical_sfo_branch,
156.         historical_sfo_bubble,
157.         historical_sfo_cemetery,
158.         historical_sfo_context_info,
159.         historical_sfo_nhcoord,
160.         historical_sfo_damage,
161.         historical_sfo_dest_area,
162.         historical_sfo_dgn_topology,
163.         historical_sfo_dungeon,
164.         historical_sfo_d_level,
165.         historical_sfo_ebones,
166.         historical_sfo_edog,
167.         historical_sfo_egd,
168.         historical_sfo_emin,
169.         historical_sfo_engr,
170.         historical_sfo_epri,
171.         historical_sfo_eshk,
172.         historical_sfo_fe,
173.         historical_sfo_flag,
174.         historical_sfo_fruit,
175.         historical_sfo_gamelog_line,
176.         historical_sfo_kinfo,
177.         historical_sfo_levelflags,
178.         historical_sfo_ls_t,
179.         historical_sfo_linfo,
180.         historical_sfo_mapseen_feat,
181.         historical_sfo_mapseen_flags,
182.         historical_sfo_mapseen_rooms,
183.         historical_sfo_mkroom,
184.         historical_sfo_monst,
185.         historical_sfo_mvitals,
186.         historical_sfo_obj,
187.         historical_sfo_objclass,
188.         historical_sfo_q_score,
189.         historical_sfo_rm,
190.         historical_sfo_spell,
191.         historical_sfo_stairway,
192.         historical_sfo_s_level,
193.         historical_sfo_trap,
194.         historical_sfo_version_info,
195.         historical_sfo_you,
196.         historical_sfo_any,
197. 
198.         historical_sfo_aligntyp,
199.         historical_sfo_boolean,
200.         historical_sfo_coordxy,
201.         historical_sfo_genericptr_t,
202.         historical_sfo_int,
203.         historical_sfo_int16,
204.         historical_sfo_int32,
205.         historical_sfo_int64,
206.         historical_sfo_long,
207.         historical_sfo_schar,
208.         historical_sfo_short,
209.         historical_sfo_size_t,
210.         historical_sfo_time_t,
211.         historical_sfo_uchar,
212.         historical_sfo_uint16,
213.         historical_sfo_uint32,
214.         historical_sfo_uint64,
215.         historical_sfo_ulong,
216.         historical_sfo_unsigned,
217.         historical_sfo_ushort,
218.         historical_sfo_xint16,
219.         historical_sfo_xint8,
220.         historical_sfo_char,
221.         historical_sfo_bitfield,
222.     }
223. };
224. 
225. struct sf_structlevel_procs historical_sfi_procs = {
226.     "",
227.     /* sf */
228.     {
229.         historical_sfi_arti_info,
230.         historical_sfi_nhrect,
231.         historical_sfi_branch,
232.         historical_sfi_bubble,
233.         historical_sfi_cemetery,
234.         historical_sfi_context_info,
235.         historical_sfi_nhcoord,
236.         historical_sfi_damage,
237.         historical_sfi_dest_area,
238.         historical_sfi_dgn_topology,
239.         historical_sfi_dungeon,
240.         historical_sfi_d_level,
241.         historical_sfi_ebones,
242.         historical_sfi_edog,
243.         historical_sfi_egd,
244.         historical_sfi_emin,
245.         historical_sfi_engr,
246.         historical_sfi_epri,
247.         historical_sfi_eshk,
248.         historical_sfi_fe,
249.         historical_sfi_flag,
250.         historical_sfi_fruit,
251.         historical_sfi_gamelog_line,
252.         historical_sfi_kinfo,
253.         historical_sfi_levelflags,
254.         historical_sfi_ls_t,
255.         historical_sfi_linfo,
256.         historical_sfi_mapseen_feat,
257.         historical_sfi_mapseen_flags,
258.         historical_sfi_mapseen_rooms,
259.         historical_sfi_mkroom,
260.         historical_sfi_monst,
261.         historical_sfi_mvitals,
262.         historical_sfi_obj,
263.         historical_sfi_objclass,
264.         historical_sfi_q_score,
265.         historical_sfi_rm,
266.         historical_sfi_spell,
267.         historical_sfi_stairway,
268.         historical_sfi_s_level,
269.         historical_sfi_trap,
270.         historical_sfi_version_info,
271.         historical_sfi_you,
272.         historical_sfi_any,
273. 
274.         historical_sfi_aligntyp,
275.         historical_sfi_boolean,
276.         historical_sfi_coordxy,
277.         historical_sfi_genericptr_t,
278.         historical_sfi_int,
279.         historical_sfi_int16,
280.         historical_sfi_int32,
281.         historical_sfi_int64,
282.         historical_sfi_long,
283.         historical_sfi_schar,
284.         historical_sfi_short,
285.         historical_sfi_size_t,
286.         historical_sfi_time_t,
287.         historical_sfi_uchar,
288.         historical_sfi_uint16,
289.         historical_sfi_uint32,
290.         historical_sfi_uint64,
291.         historical_sfi_ulong,
292.         historical_sfi_unsigned,
293.         historical_sfi_ushort,
294.         historical_sfi_xint16,
295.         historical_sfi_xint8,
296.         historical_sfi_char,
297.         historical_sfi_bitfield,
298.     }
299. };
300. 
301. /*
302.  * The historical bwrite() and mread() functions follow
303.  */
304. 
305. #ifdef SAVEFILE_DEBUGGING
306. static long floc = 0L;
307. #endif
308. 
309. /*
310.  * historical structlevel savefile writing and reading routines follow.
311.  * These were moved here from save.c and restore.c between 3.6.3 and 5.0.0,.
312.  */
313. 
314. staticfn int getidx(int, int);
315. 
316. #if defined(UNIX) || defined(WIN32)
317. #define USE_BUFFERING
318. #endif
319. 
320. struct restore_info restoreinfo = {
321.     "externalcomp", 0,
322. };
323. 
324. #define MAXFD 5
325. enum {NOFLG = 0, NOSLOT = 1};
326. static int bw_sticky[MAXFD] = {-1,-1,-1,-1,-1};
327. static int bw_buffered[MAXFD] = {0,0,0,0,0};
328. #ifdef USE_BUFFERING
329. static FILE *bw_FILE[MAXFD] = {0,0,0,0,0};
330. #endif
331. 
332. #ifdef SFLOGGING
333. static FILE *ofp[20] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
334.                          0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
335.     char ofnamebuf[80];
336.     static long ocnt = 0L;
337. 
338.     static FILE *ifp[20] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
339.                              0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
340.     char ifnamebuf[80];
341.     static long icnt = 0L;
342. #endif
343. 
344. 
345. /*
346.  * Presumably, the fdopen() to allow use of stdio fwrite()
347.  * over write() was done for performance or functionality
348.  * reasons to help some particular platform long ago.
349.  *
350.  * There have been some issues being encountered with the
351.  * implementation due to having an individual set of
352.  * tracking variables, even though there were nested
353.  * sets of open fd (like INSURANCE).
354.  *
355.  * This uses an individual tracking entry for each fd
356.  * being used.
357.  *
358.  * Some notes:
359.  *
360.  * Once buffered IO (stdio) has been enabled on the file
361.  * associated with a descriptor via fdopen():
362.  *
363.  *    1. If you use bufoff and bufon to try to toggle the
364.  *       use of write vs fwrite; the code just tracks which
365.  *       routine is to be called through the tracking
366.  *       variables and acts accordingly.
367.  *             bw_sticky[]    -  used to find the index number for
368.  *                               the fd that is stored in it, or -1
369.  *                               if it is a free slot.
370.  *             bw_buffered[]  -  indicator that buffered IO routines
371.  *                               are available for use.
372.  *             bw_FILE[]      -  the non-zero FILE * for use in calling
373.  *                               fwrite() when bw_buffered[] is also
374.  *                               non-zero.
375.  *
376.  *    2. It is illegal to call close(fd) after fdopen(), you
377.  *       must always use fclose() on the FILE * from
378.  *       that point on, so care must be taken to never call
379.  *       close(fd) on the underlying fd or bad things will
380.  *       happen.
381.  */
382. 
383. staticfn int
384. getidx(int fd, int flg)
385. {
386.     int i, retval = -1;
387. 
388.     for (i = 0; i < MAXFD; ++i)
389.         if (bw_sticky[i] == fd)
390.             return i;
391.     if (flg == NOSLOT)
392.         return retval;
393.     for (i = 0; i < MAXFD; ++i)
394.         if (bw_sticky[i] < 0) {
395.             bw_sticky[i] = fd;
396.             retval = i;
397.             break;
398.         }
399.     return retval;
400. }
401. 
402. /* Let caller know that bclose() should handle it (TRUE) */
403. boolean
404. close_check(int fd)
405. {
406.     int idx = getidx(fd, NOSLOT);
407.     boolean retval = FALSE;
408. 
409.     if (idx >= 0)
410.         retval = TRUE;
411.     return retval;
412. }
413. 
414. void
415. bufon(int fd)
416. {
417.     int idx = getidx(fd, NOFLG);
418. 
419.     if (idx >= 0) {
420.         bw_sticky[idx] = fd;
421. #ifdef USE_BUFFERING
422.         if (bw_buffered[idx])
423.             panic("buffering already enabled");
424.         if (!bw_FILE[idx]) {
425.             if ((bw_FILE[idx] = fdopen(fd, "w")) == 0)
426.                 panic("buffering of file %d failed", fd);
427.         }
428.         bw_buffered[idx] = (bw_FILE[idx] != 0);
429. #else
430.         bw_buffered[idx] = 1;
431. #endif
432.     }
433. }
434. 
435. void
436. bufoff(int fd)
437. {
438.     int idx = getidx(fd, NOFLG);
439. 
440.     if (idx >= 0) {
441.         bflush(fd);
442.         bw_buffered[idx] = 0;     /* just a flag that says "use write(fd)" */
443.     }
444. }
445. 
446. void
447. bclose(int fd)
448. {
449.     int idx = getidx(fd, NOSLOT);
450. 
451.     bufoff(fd);     /* sets bw_buffered[idx] = 0 */
452.     if (idx >= 0) {
453. #ifdef USE_BUFFERING
454.         if (bw_FILE[idx]) {
455.             (void) fclose(bw_FILE[idx]);
456.             bw_FILE[idx] = 0;
457.         } else
458. #endif
459.             close(fd);
460.         /* return the idx to the pool */
461.         bw_sticky[idx] = -1;
462.     }
463. #ifdef SFLOGGING
464.     if (fd >= 0 && fd <= SIZE(ofp)) {
465.         if (ofp[fd]) {
466.             fclose(ofp[fd]);
467.             ofp[fd] = 0;
468.         } else if (ifp[fd]) {
469.             fclose(ifp[fd]);
470.             ifp[fd] = 0;
471.         }
472.     }
473. #endif
474.     return;
475. }
476. 
477. void
478. bflush(int fd)
479. {
480.     int idx = getidx(fd, NOFLG);
481. 
482.     if (idx >= 0) {
483. #ifdef USE_BUFFERING
484.         if (bw_FILE[idx]) {
485.            if (fflush(bw_FILE[idx]) == EOF)
486.                panic("flush of savefile failed!");
487.         }
488. #endif
489.     }
490.     return;
491. }
492. 
493. void
494. bwrite(int fd, const genericptr_t loc, unsigned num)
495. {
496.     boolean failed;
497.     int idx = getidx(fd, NOFLG);
498. 
499. #ifdef SFLOGGING
500.     if (fd >= 0 && fd < SIZE(ofp)) {
501.         if (!ofp[fd]) {
502.             Snprintf(ofnamebuf, sizeof ofnamebuf, "bwrite_%02d.log", fd);
503.             ofp[fd] = fopen(ofnamebuf, "w");
504.         }
505.         if (ofp[fd]) {
506.             fprintf(ofp[fd], "%08ld, %08ld, %d\n", ocnt,
507.                     ftell(ofp[fd]), num);
508.             ocnt++;
509.         }
510.     }
511. #endif
512. 
513.     if (idx >= 0) {
514.         if (num == 0) {
515.             /* nothing to do; we need a special case to exit early
516.                because glibc fwrite doesn't give reliable
517.                success/failure indication when writing 0 bytes */
518.             return;
519.         }
520. 
521. #ifdef USE_BUFFERING
522.         if (bw_buffered[idx] && bw_FILE[idx]) {
523.             failed = (fwrite(loc, (int) num, 1, bw_FILE[idx]) != 1);
524.         } else
525. #endif /* UNIX */
526.         {
527.             /* lint wants 3rd arg of write to be an int; lint -p an unsigned */
528. #if defined(BSD) || defined(ULTRIX) || defined(WIN32) || defined(_MSC_VER)
529.             failed = ((long) write(fd, loc, (int) num) != (long) num);
530. #else /* e.g. SYSV, __TURBOC__ */
531.             failed = ((long) write(fd, loc, num) != (long) num);
532. #endif
533.         }
534.         if (failed) {
535. #if defined(HANGUPHANDLING)
536.             if (program_state.done_hup)
537.                 nh_terminate(EXIT_FAILURE);
538.             else
539. #endif
540.                 panic("cannot write %u bytes to file #%d", num, fd);
541.         }
542.     } else
543.         impossible("fd not in list (%d)?", fd);
544. }
545. 
546. /*  ===================================================== */
547. 
548. void
549. mread(int fd, genericptr_t buf, unsigned len)
550. {
551. 
552. #ifdef SFLOGGING
553.     if (fd >= 0 && fd < 9) {
554.         if (!ifp[fd]) {
555.             Snprintf(ifnamebuf, sizeof ifnamebuf, "mread_%02d.log", fd);
556.             ifp[fd] = fopen(ifnamebuf, "w");
557.         }
558.         if (ifp[fd]) {
559.             fprintf(ifp[fd], "%08ld, %08ld, %d\n", icnt,
560.                     ftell(ifp[fd]), (int) len);
561.             icnt++;
562.         }
563.     }
564. #endif
565. 
566. #if defined(BSD) || defined(ULTRIX) || defined(WIN32)
567. #define readLenType int
568. #else /* e.g. SYSV, __TURBOC__ */
569. #define readLenType unsigned
570. #endif
571.     readLenType rlen;
572.         /* Not perfect, but we don't have ssize_t available. */
573.     rlen = (readLenType) read(fd, buf, (readLenType) len);
574.     if ((readLenType) rlen != (readLenType) len) {
575.         if ((restoreinfo.mread_flags == 1) /* means "return anyway" */
576.             || (program_state.reading_bonesfile == 1)) {
577.             restoreinfo.mread_flags = -1;
578.             return;
579.         } else {
580. #ifndef SFCTOOL
581.             pline("Read %d instead of %u bytes.", (int) rlen, len);
582.             display_nhwindow(WIN_MESSAGE, TRUE); /* flush before error() */
583.             if (program_state.restoring) {
584.                 (void) nhclose(fd);
585.                 (void) delete_savefile();
586.                 error("Error restoring old game.");
587.             }
588.             panic("Error reading level file.");
589. #else
590.             printf("Read %d instead of %u bytes.\n", (int) rlen, len);
591. #endif
592.         }
593.     }
594. }
595. 
596. staticfn void
597. sfstruct_read_error(void)
598. {
599.     /* problem */;
600. }
601. 
602. #ifdef SFLOGGING
603. staticfn void
604. logging_finish(void)
605. {
606.     int i;
607. 
608.     for (i = 0; i < SIZE(ofp); ++i) {
609.         if (ofp[i]) {
610.             fclose(ofp[i]);
611.             ofp[i] = 0;
612.         }
613.     }
614.     ocnt = 0L;
615. 
616.     for (i = 0; i < SIZE(ifp); ++i) {
617.         if (ifp[i]) {
618.             fclose(ifp[i]);
619.             ifp[i] = 0;
620.         }
621.     }
622.     icnt = 0L;
623. }
624. #endif  /* SFLOGGING */
625. 
626. #undef SF_X
627. #undef SF_C
628. #undef SF_A
629. 
630. /* end of sfstruct.c */
631.