OSDN Git Service

clear_cave()でm_ptr->cdisやflow情報を消しているので、セーブ実行後に
[hengband/hengband.git] / src / save.c
1 /* File: save.c */
2
3 /* Purpose: interact with savefiles */
4
5 #include "angband.h"
6
7
8
9 /*
10  * Some "local" parameters, used to help write savefiles
11  */
12
13 static FILE     *fff;           /* Current save "file" */
14
15 static byte     xor_byte;       /* Simple encryption */
16
17 static u32b     v_stamp = 0L;   /* A simple "checksum" on the actual values */
18 static u32b     x_stamp = 0L;   /* A simple "checksum" on the encoded bytes */
19
20
21
22 /*
23  * These functions place information into a savefile a byte at a time
24  */
25
26 static void sf_put(byte v)
27 {
28         /* Encode the value, write a character */
29         xor_byte ^= v;
30         (void)putc((int)xor_byte, fff);
31
32         /* Maintain the checksum info */
33         v_stamp += v;
34         x_stamp += xor_byte;
35 }
36
37 static void wr_byte(byte v)
38 {
39         sf_put(v);
40 }
41
42 static void wr_u16b(u16b v)
43 {
44         sf_put((byte)(v & 0xFF));
45         sf_put((byte)((v >> 8) & 0xFF));
46 }
47
48 static void wr_s16b(s16b v)
49 {
50         wr_u16b((u16b)v);
51 }
52
53 static void wr_u32b(u32b v)
54 {
55         sf_put((byte)(v & 0xFF));
56         sf_put((byte)((v >> 8) & 0xFF));
57         sf_put((byte)((v >> 16) & 0xFF));
58         sf_put((byte)((v >> 24) & 0xFF));
59 }
60
61 static void wr_s32b(s32b v)
62 {
63         wr_u32b((u32b)v);
64 }
65
66 static void wr_string(cptr str)
67 {
68         while (*str)
69         {
70                 wr_byte(*str);
71                 str++;
72         }
73         wr_byte(*str);
74 }
75
76
77 /*
78  * These functions write info in larger logical records
79  */
80
81
82 /*
83  * Write an "item" record
84  */
85 static void wr_item(object_type *o_ptr)
86 {
87         u32b flags = 0x00000000;
88
89         if (o_ptr->pval) flags |= SAVE_ITEM_PVAL;
90         if (o_ptr->discount) flags |= SAVE_ITEM_DISCOUNT;
91         if (o_ptr->number != 1) flags |= SAVE_ITEM_NUMBER;
92         if (o_ptr->name1) flags |= SAVE_ITEM_NAME1;
93         if (o_ptr->name2) flags |= SAVE_ITEM_NAME2;
94         if (o_ptr->timeout) flags |= SAVE_ITEM_TIMEOUT;
95         if (o_ptr->to_h) flags |= SAVE_ITEM_TO_H;
96         if (o_ptr->to_d) flags |= SAVE_ITEM_TO_D;
97         if (o_ptr->to_a) flags |= SAVE_ITEM_TO_A;
98         if (o_ptr->ac) flags |= SAVE_ITEM_AC;
99         if (o_ptr->dd) flags |= SAVE_ITEM_DD;
100         if (o_ptr->ds) flags |= SAVE_ITEM_DS;
101         if (o_ptr->ident) flags |= SAVE_ITEM_IDENT;
102         if (o_ptr->marked) flags |= SAVE_ITEM_MARKED;
103         if (o_ptr->art_flags[0]) flags |= SAVE_ITEM_ART_FLAGS0;
104         if (o_ptr->art_flags[1]) flags |= SAVE_ITEM_ART_FLAGS1;
105         if (o_ptr->art_flags[2]) flags |= SAVE_ITEM_ART_FLAGS2;
106         if (o_ptr->art_flags[3]) flags |= SAVE_ITEM_ART_FLAGS3;
107         if (o_ptr->curse_flags) flags |= SAVE_ITEM_CURSE_FLAGS;
108         if (o_ptr->held_m_idx) flags |= SAVE_ITEM_HELD_M_IDX;
109         if (o_ptr->xtra1) flags |= SAVE_ITEM_XTRA1;
110         if (o_ptr->xtra2) flags |= SAVE_ITEM_XTRA2;
111         if (o_ptr->xtra3) flags |= SAVE_ITEM_XTRA3;
112         if (o_ptr->xtra4) flags |= SAVE_ITEM_XTRA4;
113         if (o_ptr->xtra5) flags |= SAVE_ITEM_XTRA5;
114         if (o_ptr->feeling) flags |= SAVE_ITEM_FEELING;
115         if (o_ptr->inscription) flags |= SAVE_ITEM_INSCRIPTION;
116         if (o_ptr->art_name) flags |= SAVE_ITEM_ART_NAME;
117
118         /*** Item save flags ***/
119         wr_u32b(flags);
120
121         /*** Write only un-obvious elements ***/
122         wr_s16b(o_ptr->k_idx);
123
124         /* Location */
125         wr_byte(o_ptr->iy);
126         wr_byte(o_ptr->ix);
127
128         if (flags & SAVE_ITEM_PVAL) wr_s16b(o_ptr->pval);
129
130         if (flags & SAVE_ITEM_DISCOUNT) wr_byte(o_ptr->discount);
131         if (flags & SAVE_ITEM_NUMBER) wr_byte(o_ptr->number);
132
133         wr_s16b(o_ptr->weight);
134
135         if (flags & SAVE_ITEM_NAME1) wr_byte(o_ptr->name1);
136         if (flags & SAVE_ITEM_NAME2) wr_byte(o_ptr->name2);
137         if (flags & SAVE_ITEM_TIMEOUT) wr_s16b(o_ptr->timeout);
138
139         if (flags & SAVE_ITEM_TO_H) wr_s16b(o_ptr->to_h);
140         if (flags & SAVE_ITEM_TO_D) wr_s16b(o_ptr->to_d);
141         if (flags & SAVE_ITEM_TO_A) wr_s16b(o_ptr->to_a);
142         if (flags & SAVE_ITEM_AC) wr_s16b(o_ptr->ac);
143         if (flags & SAVE_ITEM_DD) wr_byte(o_ptr->dd);
144         if (flags & SAVE_ITEM_DS) wr_byte(o_ptr->ds);
145
146         if (flags & SAVE_ITEM_IDENT) wr_byte(o_ptr->ident);
147
148         if (flags & SAVE_ITEM_MARKED) wr_byte(o_ptr->marked);
149
150         if (flags & SAVE_ITEM_ART_FLAGS0) wr_u32b(o_ptr->art_flags[0]);
151         if (flags & SAVE_ITEM_ART_FLAGS1) wr_u32b(o_ptr->art_flags[1]);
152         if (flags & SAVE_ITEM_ART_FLAGS2) wr_u32b(o_ptr->art_flags[2]);
153         if (flags & SAVE_ITEM_ART_FLAGS3) wr_u32b(o_ptr->art_flags[3]);
154
155         if (flags & SAVE_ITEM_CURSE_FLAGS) wr_u32b(o_ptr->curse_flags);
156
157         /* Held by monster index */
158         if (flags & SAVE_ITEM_HELD_M_IDX) wr_s16b(o_ptr->held_m_idx);
159
160         /* Extra information */
161         if (flags & SAVE_ITEM_XTRA1) wr_byte(o_ptr->xtra1);
162         if (flags & SAVE_ITEM_XTRA2) wr_byte(o_ptr->xtra2);
163         if (flags & SAVE_ITEM_XTRA3) wr_byte(o_ptr->xtra3);
164         if (flags & SAVE_ITEM_XTRA4) wr_s16b(o_ptr->xtra4);
165         if (flags & SAVE_ITEM_XTRA5) wr_s16b(o_ptr->xtra5);
166
167         /* Feelings */
168         if (flags & SAVE_ITEM_FEELING) wr_byte(o_ptr->feeling);
169
170         if (flags & SAVE_ITEM_INSCRIPTION) wr_string(quark_str(o_ptr->inscription));
171         if (flags & SAVE_ITEM_ART_NAME) wr_string(quark_str(o_ptr->art_name));
172 }
173
174
175 /*
176  * Write a "monster" record
177  */
178 static void wr_monster(monster_type *m_ptr)
179 {
180         u32b flags = 0x00000000;
181
182         if (m_ptr->ap_r_idx != m_ptr->r_idx) flags |= SAVE_MON_AP_R_IDX;
183         if (m_ptr->sub_align) flags |= SAVE_MON_SUB_ALIGN;
184         if (m_ptr->csleep) flags |= SAVE_MON_CSLEEP;
185         if (m_ptr->fast) flags |= SAVE_MON_FAST;
186         if (m_ptr->slow) flags |= SAVE_MON_SLOW;
187         if (m_ptr->stunned) flags |= SAVE_MON_STUNNED;
188         if (m_ptr->confused) flags |= SAVE_MON_CONFUSED;
189         if (m_ptr->monfear) flags |= SAVE_MON_MONFEAR;
190         if (m_ptr->target_y) flags |= SAVE_MON_TARGET_Y;
191         if (m_ptr->target_x) flags |= SAVE_MON_TARGET_X;
192         if (m_ptr->invulner) flags |= SAVE_MON_INVULNER;
193         if (m_ptr->smart) flags |= SAVE_MON_SMART;
194         if (m_ptr->exp) flags |= SAVE_MON_EXP;
195         if (m_ptr->mflag2) flags |= SAVE_MON_MFLAG2;
196         if (m_ptr->nickname) flags |= SAVE_MON_NICKNAME;
197
198         /*** Monster save flags ***/
199         wr_u32b(flags);
200
201         /*** Write only un-obvious elements ***/
202         wr_s16b(m_ptr->r_idx);
203         wr_byte(m_ptr->fy);
204         wr_byte(m_ptr->fx);
205         wr_s16b(m_ptr->hp);
206         wr_s16b(m_ptr->maxhp);
207         wr_s16b(m_ptr->max_maxhp);
208
209         /* Monster race index of its appearance */
210         if (flags & SAVE_MON_AP_R_IDX) wr_s16b(m_ptr->ap_r_idx);
211
212         if (flags & SAVE_MON_SUB_ALIGN) wr_byte(m_ptr->sub_align);
213         if (flags & SAVE_MON_CSLEEP) wr_s16b(m_ptr->csleep);
214
215         wr_byte(m_ptr->mspeed);
216         wr_s16b(m_ptr->energy_need);
217
218         if (flags & SAVE_MON_FAST) wr_byte(m_ptr->fast);
219         if (flags & SAVE_MON_SLOW) wr_byte(m_ptr->slow);
220         if (flags & SAVE_MON_STUNNED) wr_byte(m_ptr->stunned);
221         if (flags & SAVE_MON_CONFUSED) wr_byte(m_ptr->confused);
222         if (flags & SAVE_MON_MONFEAR) wr_byte(m_ptr->monfear);
223         if (flags & SAVE_MON_TARGET_Y) wr_s16b(m_ptr->target_y);
224         if (flags & SAVE_MON_TARGET_X) wr_s16b(m_ptr->target_x);
225         if (flags & SAVE_MON_INVULNER) wr_byte(m_ptr->invulner);
226         if (flags & SAVE_MON_SMART) wr_u32b(m_ptr->smart);
227         if (flags & SAVE_MON_EXP) wr_u32b(m_ptr->exp);
228         if (flags & SAVE_MON_MFLAG2) wr_byte(m_ptr->mflag2);
229         if (flags & SAVE_MON_NICKNAME) wr_string(quark_str(m_ptr->nickname));
230 }
231
232
233 /*
234  * Write a "lore" record
235  */
236 static void wr_lore(int r_idx)
237 {
238         monster_race *r_ptr = &r_info[r_idx];
239
240         /* Count sights/deaths/kills */
241         wr_s16b(r_ptr->r_sights);
242         wr_s16b(r_ptr->r_deaths);
243         wr_s16b(r_ptr->r_pkills);
244         wr_s16b(r_ptr->r_tkills);
245
246         /* Count wakes and ignores */
247         wr_byte(r_ptr->r_wake);
248         wr_byte(r_ptr->r_ignore);
249
250         /* Extra stuff */
251         wr_byte(r_ptr->r_xtra1);
252         wr_byte(r_ptr->r_xtra2);
253
254         /* Count drops */
255         wr_byte(r_ptr->r_drop_gold);
256         wr_byte(r_ptr->r_drop_item);
257
258         /* Count spells */
259         wr_byte(r_ptr->r_cast_inate);
260         wr_byte(r_ptr->r_cast_spell);
261
262         /* Count blows of each type */
263         wr_byte(r_ptr->r_blows[0]);
264         wr_byte(r_ptr->r_blows[1]);
265         wr_byte(r_ptr->r_blows[2]);
266         wr_byte(r_ptr->r_blows[3]);
267
268         /* Memorize flags */
269         wr_u32b(r_ptr->r_flags1);
270         wr_u32b(r_ptr->r_flags2);
271         wr_u32b(r_ptr->r_flags3);
272         wr_u32b(r_ptr->r_flags4);
273         wr_u32b(r_ptr->r_flags5);
274         wr_u32b(r_ptr->r_flags6);
275
276
277         /* Monster limit per level */
278         wr_byte(r_ptr->max_num);
279
280         /* Location in saved floor */
281         wr_s16b(r_ptr->floor_id);
282
283         /* Later (?) */
284         wr_byte(0);
285 }
286
287
288 /*
289  * Write an "xtra" record
290  */
291 static void wr_xtra(int k_idx)
292 {
293         byte tmp8u = 0;
294
295         object_kind *k_ptr = &k_info[k_idx];
296
297         if (k_ptr->aware) tmp8u |= 0x01;
298         if (k_ptr->tried) tmp8u |= 0x02;
299
300         wr_byte(tmp8u);
301 }
302
303
304 /*
305  * Write a "store" record
306  */
307 static void wr_store(store_type *st_ptr)
308 {
309         int j;
310
311         /* Save the "open" counter */
312         wr_u32b(st_ptr->store_open);
313
314         /* Save the "insults" */
315         wr_s16b(st_ptr->insult_cur);
316
317         /* Save the current owner */
318         wr_byte(st_ptr->owner);
319
320         /* Save the stock size */
321         wr_s16b(st_ptr->stock_num);
322
323         /* Save the "haggle" info */
324         wr_s16b(st_ptr->good_buy);
325         wr_s16b(st_ptr->bad_buy);
326
327         wr_s32b(st_ptr->last_visit);
328
329         /* Save the stock */
330         for (j = 0; j < st_ptr->stock_num; j++)
331         {
332                 /* Save each item in stock */
333                 wr_item(&st_ptr->stock[j]);
334         }
335 }
336
337
338 /*
339  * Write RNG state
340  */
341 static errr wr_randomizer(void)
342 {
343         int i;
344
345         /* Zero */
346         wr_u16b(0);
347
348         /* Place */
349         wr_u16b(Rand_place);
350
351         /* State */
352         for (i = 0; i < RAND_DEG; i++)
353         {
354                 wr_u32b(Rand_state[i]);
355         }
356
357         /* Success */
358         return (0);
359 }
360
361
362 /*
363  * Write the "options"
364  */
365 static void wr_options(void)
366 {
367         int i;
368
369         u16b c;
370
371
372         /*** Oops ***/
373
374         /* Oops */
375         for (i = 0; i < 4; i++) wr_u32b(0L);
376
377
378         /*** Special Options ***/
379
380         /* Write "delay_factor" */
381         wr_byte(delay_factor);
382
383         /* Write "hitpoint_warn" */
384         wr_byte(hitpoint_warn);
385
386
387         /*** Cheating options ***/
388
389         c = 0;
390
391         if (p_ptr->wizard) c |= 0x0002;
392
393         if (cheat_peek) c |= 0x0100;
394         if (cheat_hear) c |= 0x0200;
395         if (cheat_room) c |= 0x0400;
396         if (cheat_xtra) c |= 0x0800;
397         if (cheat_know) c |= 0x1000;
398         if (cheat_live) c |= 0x2000;
399         if (cheat_save) c |= 0x4000;
400
401         wr_u16b(c);
402
403         /* Autosave info */
404         wr_byte(autosave_l);
405         wr_byte(autosave_t);
406         wr_s16b(autosave_freq);
407
408         /*** Extract options ***/
409
410         /* Analyze the options */
411         for (i = 0; option_info[i].o_desc; i++)
412         {
413                 int os = option_info[i].o_set;
414                 int ob = option_info[i].o_bit;
415
416                 /* Process real entries */
417                 if (option_info[i].o_var)
418                 {
419                         /* Set */
420                         if (*option_info[i].o_var)
421                         {
422                                 /* Set */
423                                 option_flag[os] |= (1L << ob);
424                         }
425
426                         /* Clear */
427                         else
428                         {
429                                 /* Clear */
430                                 option_flag[os] &= ~(1L << ob);
431                         }
432                 }
433         }
434
435
436         /*** Normal options ***/
437
438         /* Dump the flags */
439         for (i = 0; i < 8; i++) wr_u32b(option_flag[i]);
440
441         /* Dump the masks */
442         for (i = 0; i < 8; i++) wr_u32b(option_mask[i]);
443
444
445         /*** Window options ***/
446
447         /* Dump the flags */
448         for (i = 0; i < 8; i++) wr_u32b(window_flag[i]);
449
450         /* Dump the masks */
451         for (i = 0; i < 8; i++) wr_u32b(window_mask[i]);
452 }
453
454
455 /*
456  * Hack -- Write the "ghost" info
457  */
458 static void wr_ghost(void)
459 {
460         int i;
461
462         /* Name */
463 #ifdef JP
464         wr_string("ÉÔÀµ¤Ê¥´¡¼¥¹¥È");
465 #else
466         wr_string("Broken Ghost");
467 #endif
468
469
470         /* Hack -- stupid data */
471         for (i = 0; i < 60; i++) wr_byte(0);
472 }
473
474
475 /*
476  * Save quick start data
477  */
478 static void save_quick_start(void)
479 {
480         int i;
481
482         wr_byte(previous_char.psex);
483         wr_byte(previous_char.prace);
484         wr_byte(previous_char.pclass);
485         wr_byte(previous_char.pseikaku);
486         wr_byte(previous_char.realm1);
487         wr_byte(previous_char.realm2);
488
489         wr_s16b(previous_char.age);
490         wr_s16b(previous_char.ht);
491         wr_s16b(previous_char.wt);
492         wr_s16b(previous_char.sc);
493         wr_s32b(previous_char.au);
494
495         for (i = 0; i < 6; i++) wr_s16b(previous_char.stat_max[i]);
496         for (i = 0; i < 6; i++) wr_s16b(previous_char.stat_max_max[i]);
497
498         for (i = 0; i < PY_MAX_LEVEL; i++) wr_s16b(previous_char.player_hp[i]);
499
500         wr_s16b(previous_char.chaos_patron);
501
502         for (i = 0; i < 8; i++) wr_s16b(previous_char.vir_types[i]);
503
504         for (i = 0; i < 4; i++) wr_string(previous_char.history[i]);
505
506         wr_byte(previous_char.quests);
507
508         /* No quick start after using debug mode or cheat options */
509         if (p_ptr->noscore) previous_char.quick_ok = FALSE;
510
511         wr_byte((byte)previous_char.quick_ok);
512 }
513
514 /*
515  * Write some "extra" info
516  */
517 static void wr_extra(void)
518 {
519         int i,j;
520         byte tmp8u;
521
522         wr_string(player_name);
523
524         wr_string(p_ptr->died_from);
525
526         save_quick_start();
527
528         for (i = 0; i < 4; i++)
529         {
530                 wr_string(p_ptr->history[i]);
531         }
532
533         /* Race/Class/Gender/Spells */
534         wr_byte(p_ptr->prace);
535         wr_byte(p_ptr->pclass);
536         wr_byte(p_ptr->pseikaku);
537         wr_byte(p_ptr->psex);
538         wr_byte(p_ptr->realm1);
539         wr_byte(p_ptr->realm2);
540         wr_byte(0);     /* oops */
541
542         wr_byte(p_ptr->hitdie);
543         wr_u16b(p_ptr->expfact);
544
545         wr_s16b(p_ptr->age);
546         wr_s16b(p_ptr->ht);
547         wr_s16b(p_ptr->wt);
548
549         /* Dump the stats (maximum and current) */
550         for (i = 0; i < 6; ++i) wr_s16b(p_ptr->stat_max[i]);
551         for (i = 0; i < 6; ++i) wr_s16b(p_ptr->stat_max_max[i]);
552         for (i = 0; i < 6; ++i) wr_s16b(p_ptr->stat_cur[i]);
553
554         /* Ignore the transient stats */
555         for (i = 0; i < 12; ++i) wr_s16b(0);
556
557         wr_u32b(p_ptr->au);
558
559         wr_u32b(p_ptr->max_exp);
560         wr_u32b(p_ptr->exp);
561         wr_u16b(p_ptr->exp_frac);
562         wr_s16b(p_ptr->lev);
563
564         for (i = 0; i < 64; i++) wr_s16b(p_ptr->spell_exp[i]);
565         for (i = 0; i < 5; i++) for (j = 0; j < 64; j++) wr_s16b(p_ptr->weapon_exp[i][j]);
566         for (i = 0; i < 10; i++) wr_s16b(p_ptr->skill_exp[i]);
567         for (i = 0; i < 108; i++) wr_s32b(p_ptr->magic_num1[i]);
568         for (i = 0; i < 108; i++) wr_byte(p_ptr->magic_num2[i]);
569
570         wr_byte(p_ptr->start_race);
571         wr_s32b(p_ptr->old_race1);
572         wr_s32b(p_ptr->old_race2);
573         wr_s16b(p_ptr->old_realm);
574
575         for (i = 0; i < MAX_MANE; i++)
576         {
577                 wr_s16b(p_ptr->mane_spell[i]);
578                 wr_s16b(p_ptr->mane_dam[i]);
579         }
580         wr_s16b(p_ptr->mane_num);
581
582         for (i = 0; i < MAX_KUBI; i++)
583         {
584                 wr_s16b(kubi_r_idx[i]);
585         }
586
587         for (i = 0; i < 4; i++)
588         {
589                 wr_s16b(battle_mon[i]);
590                 wr_u32b(mon_odds[i]);
591         }
592
593         wr_s16b(p_ptr->town_num); /* -KMW- */
594
595         /* Write arena and rewards information -KMW- */
596         wr_s16b(p_ptr->arena_number);
597         wr_s16b(p_ptr->inside_arena);
598         wr_s16b(p_ptr->inside_quest);
599         wr_s16b(p_ptr->inside_battle);
600         wr_byte(p_ptr->exit_bldg);
601         wr_byte(p_ptr->leftbldg); /* save building leave status -KMW- */
602
603         wr_s16b(p_ptr->oldpx);
604         wr_s16b(p_ptr->oldpy);
605
606         /* Save builing rewards */
607         wr_s16b(MAX_BACT);
608
609         for (i = 0; i < MAX_BACT; i++) wr_s16b(p_ptr->rewards[i]);
610
611         wr_s16b(p_ptr->mhp);
612         wr_s16b(p_ptr->chp);
613         wr_u16b(p_ptr->chp_frac);
614
615         wr_s16b(p_ptr->msp);
616         wr_s16b(p_ptr->csp);
617         wr_u16b(p_ptr->csp_frac);
618
619         /* Max Player and Dungeon Levels */
620         wr_s16b(p_ptr->max_plv);
621         tmp8u = (byte)max_d_idx;
622         wr_byte(tmp8u);
623         for (i = 0; i < tmp8u; i++)
624                 wr_s16b(max_dlv[i]);
625
626         /* More info */
627         wr_s16b(0);     /* oops */
628         wr_s16b(0);     /* oops */
629         wr_s16b(0);     /* oops */
630         wr_s16b(0);     /* oops */
631         wr_s16b(p_ptr->sc);
632         wr_s16b(0);     /* oops */
633
634         wr_s16b(0);             /* old "rest" */
635         wr_s16b(p_ptr->blind);
636         wr_s16b(p_ptr->paralyzed);
637         wr_s16b(p_ptr->confused);
638         wr_s16b(p_ptr->food);
639         wr_s16b(0);     /* old "food_digested" */
640         wr_s16b(0);     /* old "protection" */
641         wr_s16b(p_ptr->energy_need);
642         wr_s16b(p_ptr->fast);
643         wr_s16b(p_ptr->slow);
644         wr_s16b(p_ptr->afraid);
645         wr_s16b(p_ptr->cut);
646         wr_s16b(p_ptr->stun);
647         wr_s16b(p_ptr->poisoned);
648         wr_s16b(p_ptr->image);
649         wr_s16b(p_ptr->protevil);
650         wr_s16b(p_ptr->invuln);
651         wr_s16b(p_ptr->ult_res);
652         wr_s16b(p_ptr->hero);
653         wr_s16b(p_ptr->shero);
654         wr_s16b(p_ptr->shield);
655         wr_s16b(p_ptr->blessed);
656         wr_s16b(p_ptr->tim_invis);
657         wr_s16b(p_ptr->word_recall);
658         wr_s16b(p_ptr->recall_dungeon);
659         wr_s16b(p_ptr->alter_reality);
660         wr_s16b(p_ptr->see_infra);
661         wr_s16b(p_ptr->tim_infra);
662         wr_s16b(p_ptr->oppose_fire);
663         wr_s16b(p_ptr->oppose_cold);
664         wr_s16b(p_ptr->oppose_acid);
665         wr_s16b(p_ptr->oppose_elec);
666         wr_s16b(p_ptr->oppose_pois);
667         wr_s16b(p_ptr->tsuyoshi);
668         wr_s16b(p_ptr->tim_esp);
669         wr_s16b(p_ptr->wraith_form);
670         wr_s16b(p_ptr->resist_magic);
671         wr_s16b(p_ptr->tim_regen);
672         wr_s16b(p_ptr->kabenuke);
673         wr_s16b(p_ptr->tim_stealth);
674         wr_s16b(p_ptr->tim_ffall);
675         wr_s16b(p_ptr->tim_sh_touki);
676         wr_s16b(p_ptr->lightspeed);
677         wr_s16b(p_ptr->tsubureru);
678         wr_s16b(p_ptr->magicdef);
679         wr_s16b(p_ptr->tim_res_nether);
680         wr_s16b(p_ptr->tim_res_time);
681         wr_byte(p_ptr->mimic_form);
682         wr_s16b(p_ptr->tim_mimic);
683         wr_s16b(p_ptr->tim_sh_fire);
684         wr_s16b(p_ptr->tim_sh_holy);
685         wr_s16b(p_ptr->tim_eyeeye);
686
687         /* by henkma */
688         wr_s16b(p_ptr->tim_reflect);
689         wr_s16b(p_ptr->multishadow);
690         wr_s16b(p_ptr->dustrobe);
691
692         wr_s16b(p_ptr->chaos_patron);
693         wr_u32b(p_ptr->muta1);
694         wr_u32b(p_ptr->muta2);
695         wr_u32b(p_ptr->muta3);
696
697         for (i = 0; i<8; i++)
698                 wr_s16b(p_ptr->virtues[i]);
699         for (i = 0; i<8; i++)
700                 wr_s16b(p_ptr->vir_types[i]);
701
702         wr_s16b(p_ptr->ele_attack);
703         wr_u32b(p_ptr->special_attack);
704         wr_s16b(p_ptr->ele_immune);
705         wr_u32b(p_ptr->special_defense);
706         wr_byte(p_ptr->knowledge);
707         wr_byte(0);     /* oops */
708         wr_byte(0);     /* oops */
709         wr_byte(p_ptr->action);
710         wr_byte(0);
711         wr_byte(preserve_mode);
712         wr_byte(p_ptr->wait_report_score);
713
714         /* Future use */
715         for (i = 0; i < 12; i++) wr_u32b(0L);
716
717         /* Ignore some flags */
718         wr_u32b(0L);    /* oops */
719         wr_u32b(0L);    /* oops */
720         wr_u32b(0L);    /* oops */
721
722
723         /* Write the "object seeds" */
724         wr_u32b(seed_flavor);
725         wr_u32b(seed_town);
726
727
728         /* Special stuff */
729         wr_u16b(p_ptr->panic_save);
730         wr_u16b(p_ptr->total_winner);
731         wr_u16b(p_ptr->noscore);
732
733
734         /* Write death */
735         wr_byte(p_ptr->is_dead);
736
737         /* Write feeling */
738         wr_byte(feeling);
739
740         /* Turn of last "feeling" */
741         wr_s32b(old_turn);
742
743         /* Current turn */
744         wr_s32b(turn);
745
746         wr_s32b(dungeon_turn);
747
748         wr_s32b(old_battle);
749
750         wr_s16b(today_mon);
751         wr_s16b(p_ptr->today_mon);
752         wr_s16b(p_ptr->riding);
753
754         /* Current floor_id */
755         wr_s16b(p_ptr->floor_id);
756
757         wr_u32b(playtime);
758
759         wr_s32b(p_ptr->visit);
760
761         wr_u32b(p_ptr->count);
762 }
763
764
765
766 /*
767  * hook function to sort monsters by level
768  */
769 static bool ang_sort_comp_cave_temp(vptr u, vptr v, int a, int b)
770 {
771         cave_template_type *who = (cave_template_type *)(u);
772
773         u16b o1 = who[a].occurrence;
774         u16b o2 = who[b].occurrence;
775
776         return o2 <= o1;
777 }
778
779
780 /*
781  * Sorting hook -- Swap function
782  */
783 static void ang_sort_swap_cave_temp(vptr u, vptr v, int a, int b)
784 {
785         cave_template_type *who = (cave_template_type *)(u);
786
787         cave_template_type holder;
788
789         /* Swap */
790         holder = who[a];
791         who[a] = who[b];
792         who[b] = holder;
793 }
794
795
796 /*
797  * Actually write a saved floor data
798  * using effectively compressed format.
799  */
800 static void wr_saved_floor(saved_floor_type *sf_ptr)
801 {
802         cave_template_type *template;
803         u16b max_num_temp;
804         u16b num_temp = 0;
805         int dummy_why;
806
807         int i, y, x;
808
809         u16b tmp16u;
810
811         byte count;
812         u16b prev_u16b;
813
814
815         /*** Basic info ***/
816
817         /* Dungeon floor specific info follows */
818
819         if (!sf_ptr)
820         {
821                 /*** Not a saved floor ***/
822
823                 wr_s16b(dun_level);
824         }
825         else
826         {
827                 /*** The saved floor ***/
828
829                 wr_s16b(sf_ptr->floor_id);
830                 wr_byte(sf_ptr->savefile_id);
831                 wr_s16b(sf_ptr->dun_level);
832                 wr_s32b(sf_ptr->last_visit);
833                 wr_u32b(sf_ptr->visit_mark);
834                 wr_s16b(sf_ptr->upper_floor_id);
835                 wr_s16b(sf_ptr->lower_floor_id);
836         }
837
838         wr_u16b(base_level);
839         wr_u16b(num_repro);
840         wr_u16b((u16b)py);
841         wr_u16b((u16b)px);
842         wr_u16b(cur_hgt);
843         wr_u16b(cur_wid);
844         wr_byte(feeling);
845
846
847
848         /*********** Make template for cave_type **********/
849
850         /*
851          * Usually number of templates are fewer than 255.  Even if
852          * more than 254 are exist, the occurrence of each template
853          * with larger ID is very small when we sort templates by
854          * occurrence.  So we will use two (or more) bytes for
855          * templete ID larger than 254.
856          *
857          * Ex: 256 will be "0xff" "0x01".
858          *     515 will be "0xff" "0xff" "0x03"
859          */
860
861         /* Fake max number */
862         max_num_temp = 255;
863
864         /* Allocate the "template" array */
865         C_MAKE(template, max_num_temp, cave_template_type);
866
867         /* Extract template array */
868         for (y = 0; y < cur_hgt; y++)
869         {
870                 for (x = 0; x < cur_wid; x++)
871                 {
872                         cave_type *c_ptr = &cave[y][x];
873
874                         for (i = 0; i < num_temp; i++)
875                         {
876                                 if (template[i].info == c_ptr->info &&
877                                     template[i].feat == c_ptr->feat &&
878                                     template[i].mimic == c_ptr->mimic &&
879                                     template[i].special == c_ptr->special)
880                                 {
881                                         /* Same terrain is exist */
882                                         template[i].occurrence++;
883                                         break;
884                                 }
885                         }
886
887                         /* Are there same one? */
888                         if (i < num_temp) continue;
889
890                         /* If the max_num_temp is too small, increase it. */
891                         if (num_temp >= max_num_temp)
892                         {
893                                 cave_template_type *old_template = template;
894
895                                 /* Re-allocate the "template" array */
896                                 C_MAKE(template, max_num_temp + 255, cave_template_type);
897                                 C_COPY(template, old_template, max_num_temp, cave_template_type);
898                                 C_FREE(old_template, max_num_temp, cave_template_type);
899                                 max_num_temp += 255;
900                         }
901
902                         /* Add new template */
903                         template[num_temp].info = c_ptr->info;
904                         template[num_temp].feat = c_ptr->feat;
905                         template[num_temp].mimic = c_ptr->mimic;
906                         template[num_temp].special = c_ptr->special;
907                         template[num_temp].occurrence = 1;
908
909                         /* Increase number of template */
910                         num_temp++;
911                 }
912         }
913
914         /* Select the sort method */
915         ang_sort_comp = ang_sort_comp_cave_temp;
916         ang_sort_swap = ang_sort_swap_cave_temp;
917
918         /* Sort by occurrence */
919         ang_sort(template, &dummy_why, num_temp);
920
921
922         /*** Dump templates ***/
923
924         /* Total templates */
925         wr_u16b(num_temp);
926
927         /* Dump the templates */
928         for (i = 0; i < num_temp; i++)
929         {
930                 cave_template_type *ct_ptr = &template[i];
931
932                 /* Dump it */
933                 wr_u16b(ct_ptr->info);
934                 wr_byte(ct_ptr->feat);
935                 wr_byte(ct_ptr->mimic);
936                 wr_s16b(ct_ptr->special);
937         }
938
939
940
941         /*** "Run-Length-Encoding" of cave ***/
942
943         /* Note that this will induce two wasted bytes */
944         count = 0;
945         prev_u16b = 0;
946
947         /* Dump the cave */
948         for (y = 0; y < cur_hgt; y++)
949         {
950                 for (x = 0; x < cur_wid; x++)
951                 {
952                         cave_type *c_ptr = &cave[y][x];
953
954                         for (i = 0; i < num_temp; i++)
955                         {
956                                 if (template[i].info == c_ptr->info &&
957                                     template[i].feat == c_ptr->feat &&
958                                     template[i].mimic == c_ptr->mimic &&
959                                     template[i].special == c_ptr->special)
960                                         break;
961                         }
962
963                         /* Extract an ID */
964                         tmp16u = i;
965
966                         /* If the run is broken, or too full, flush it */
967                         if ((tmp16u != prev_u16b) || (count == MAX_UCHAR))
968                         {
969                                 wr_byte((byte)count);
970
971                                 while (prev_u16b >= MAX_UCHAR)
972                                 {
973                                         /* Mark as actual data is larger than 254 */
974                                         wr_byte(MAX_UCHAR);
975                                         prev_u16b -= MAX_UCHAR;
976                                 }
977
978                                 wr_byte((byte)prev_u16b);
979                                 prev_u16b = tmp16u;
980                                 count = 1;
981                         }
982
983                         /* Continue the run */
984                         else
985                         {
986                                 count++;
987                         }
988                 }
989         }
990
991         /* Flush the data (if any) */
992         if (count)
993         {
994                 wr_byte((byte)count);
995
996                 while (prev_u16b >= MAX_UCHAR)
997                 {
998                         /* Mark as actual data is larger than 254 */
999                         wr_byte(MAX_UCHAR);
1000                         prev_u16b -= MAX_UCHAR;
1001                 }
1002                 wr_byte((byte)prev_u16b);
1003         }
1004
1005
1006         /* Free the "template" array */
1007         C_FREE(template, max_num_temp, cave_template_type);
1008
1009
1010         /*** Dump objects ***/
1011
1012         /* Total objects */
1013         wr_u16b(o_max);
1014
1015         /* Dump the objects */
1016         for (i = 1; i < o_max; i++)
1017         {
1018                 object_type *o_ptr = &o_list[i];
1019
1020                 /* Dump it */
1021                 wr_item(o_ptr);
1022         }
1023
1024
1025         /*** Dump the monsters ***/
1026
1027         /* Total monsters */
1028         wr_u16b(m_max);
1029
1030         /* Dump the monsters */
1031         for (i = 1; i < m_max; i++)
1032         {
1033                 monster_type *m_ptr = &m_list[i];
1034
1035                 /* Dump it */
1036                 wr_monster(m_ptr);
1037         }
1038 }
1039
1040
1041 /*
1042  * Write the current dungeon (new method)
1043  */
1044 static bool wr_dungeon(void)
1045 {
1046         saved_floor_type *cur_sf_ptr;
1047         int i;
1048
1049         /* Forget the lite */
1050         forget_lite();
1051
1052         /* Forget the view */
1053         forget_view();
1054
1055         /* Forget the view */
1056         clear_mon_lite();
1057
1058
1059         /*** Meta info ***/
1060
1061         /* Number of floor_id used from birth */
1062         wr_s16b(max_floor_id);
1063
1064         /* Current dungeon type */
1065         wr_byte(dungeon_type);
1066
1067
1068         /*** On the surface  ***/
1069         if (!p_ptr->floor_id)
1070         {
1071                 /* No array elements */
1072                 wr_byte(0);
1073
1074                 /* Write the current floor data */
1075                 wr_saved_floor(NULL);
1076
1077                 /* Success */
1078                 return TRUE;
1079         }
1080
1081
1082         /*** In the dungeon ***/
1083
1084         /* Number of array elements */
1085         wr_byte(MAX_SAVED_FLOORS);
1086
1087         /* Write the saved_floors array */
1088         for (i = 0; i < MAX_SAVED_FLOORS; i++)
1089         {
1090                 saved_floor_type *sf_ptr = &saved_floors[i];
1091
1092                 wr_s16b(sf_ptr->floor_id);
1093                 wr_byte(sf_ptr->savefile_id);
1094                 wr_s16b(sf_ptr->dun_level);
1095                 wr_s32b(sf_ptr->last_visit);
1096                 wr_u32b(sf_ptr->visit_mark);
1097                 wr_s16b(sf_ptr->upper_floor_id);
1098                 wr_s16b(sf_ptr->lower_floor_id);
1099         }
1100
1101         /* Extract pointer to current floor */
1102         cur_sf_ptr = get_sf_ptr(p_ptr->floor_id);
1103
1104         /* Save current floor to temporal file */
1105         if (!save_floor(cur_sf_ptr, (SLF_SECOND))) return FALSE;
1106
1107         /* Move data in temporal files to the savefile */
1108         for (i = 0; i < MAX_SAVED_FLOORS; i++)
1109         {
1110                 saved_floor_type *sf_ptr = &saved_floors[i];
1111
1112                 /* Unused element */
1113                 if (!sf_ptr->floor_id) continue;
1114
1115                 /* Load temporal saved floor file */
1116                 if (load_floor(sf_ptr, (SLF_SECOND | SLF_NO_KILL)))
1117                 {
1118                         /* Mark success */
1119                         wr_byte(0);
1120
1121                         /* Write saved floor data to the save file */
1122                         wr_saved_floor(sf_ptr);
1123                 }
1124                 else
1125                 {
1126                         /* Mark failure */
1127                         wr_byte(1);
1128                 }
1129         }
1130
1131         /* Restore current floor */
1132         if (!load_floor(cur_sf_ptr, (SLF_SECOND))) return FALSE;
1133
1134
1135         /* Update lite/view */
1136         p_ptr->update |= (PU_VIEW | PU_LITE | PU_MON_LITE);
1137
1138         /* Update monsters */
1139         p_ptr->update |= (PU_MONSTERS | PU_DISTANCE | PU_FLOW);
1140
1141         /* Success */
1142         return TRUE;
1143 }
1144
1145
1146
1147 /*
1148  * Actually write a save-file
1149  */
1150 static bool wr_savefile_new(void)
1151 {
1152         int        i, j;
1153
1154         u32b              now;
1155
1156         byte            tmp8u;
1157         u16b            tmp16u;
1158
1159
1160         /* Compact the objects */
1161         compact_objects(0);
1162         /* Compact the monsters */
1163         compact_monsters(0);
1164
1165         /* Guess at the current time */
1166         now = time((time_t *)0);
1167
1168
1169         /* Note the operating system */
1170         sf_system = 0L;
1171
1172         /* Note when the file was saved */
1173         sf_when = now;
1174
1175         /* Note the number of saves */
1176         sf_saves++;
1177
1178
1179         /*** Actually write the file ***/
1180
1181         /* Dump the file header */
1182         xor_byte = 0;
1183         wr_byte(FAKE_VER_MAJOR);
1184         xor_byte = 0;
1185         wr_byte(FAKE_VER_MINOR);
1186         xor_byte = 0;
1187         wr_byte(FAKE_VER_PATCH);
1188         xor_byte = 0;
1189
1190         /* Initial value of xor_byte */
1191         tmp8u = (byte)randint0(256);
1192         wr_byte(tmp8u);
1193
1194
1195         /* Reset the checksum */
1196         v_stamp = 0L;
1197         x_stamp = 0L;
1198
1199         /* Write the savefile version for Hengband 1.1.1 and later */
1200         wr_byte(H_VER_EXTRA);
1201         wr_byte(H_VER_PATCH);
1202         wr_byte(H_VER_MINOR);
1203         wr_byte(H_VER_MAJOR);
1204
1205         /* Operating system */
1206         wr_u32b(sf_system);
1207
1208
1209         /* Time file last saved */
1210         wr_u32b(sf_when);
1211
1212         /* Number of past lives */
1213         wr_u16b(sf_lives);
1214
1215         /* Number of times saved */
1216         wr_u16b(sf_saves);
1217
1218
1219         /* Space */
1220         wr_u32b(0L);
1221         wr_u32b(0L);
1222
1223
1224         /* Write the RNG state */
1225         wr_randomizer();
1226
1227
1228         /* Write the boolean "options" */
1229         wr_options();
1230
1231
1232         /* Dump the number of "messages" */
1233         tmp16u = message_num();
1234         if (compress_savefile && (tmp16u > 40)) tmp16u = 40;
1235         wr_u16b(tmp16u);
1236
1237         /* Dump the messages (oldest first!) */
1238         for (i = tmp16u - 1; i >= 0; i--)
1239         {
1240                 wr_string(message_str((s16b)i));
1241         }
1242
1243
1244         /* Dump the monster lore */
1245         tmp16u = max_r_idx;
1246         wr_u16b(tmp16u);
1247         for (i = 0; i < tmp16u; i++) wr_lore(i);
1248
1249
1250         /* Dump the object memory */
1251         tmp16u = max_k_idx;
1252         wr_u16b(tmp16u);
1253         for (i = 0; i < tmp16u; i++) wr_xtra(i);
1254
1255         /* Dump the towns */
1256         tmp16u = max_towns;
1257         wr_u16b(tmp16u);
1258
1259         /* Dump the quests */
1260         tmp16u = max_quests;
1261         wr_u16b(tmp16u);
1262
1263         /* Dump the quests */
1264         tmp8u = MAX_RANDOM_QUEST-MIN_RANDOM_QUEST;
1265         wr_byte(tmp8u);
1266
1267         for (i = 0; i < max_quests; i++)
1268         {
1269                 /* Save status for every quest */
1270                 wr_s16b(quest[i].status);
1271
1272                 /* And the dungeon level too */
1273                 /* (prevents problems with multi-level quests) */
1274                 wr_s16b(quest[i].level);
1275
1276                 wr_byte(quest[i].complev);
1277
1278                 /* Save quest status if quest is running */
1279                 if (quest[i].status == QUEST_STATUS_TAKEN || quest[i].status == QUEST_STATUS_COMPLETED || ((i >= MIN_RANDOM_QUEST) && (i <= MAX_RANDOM_QUEST)))
1280                 {
1281                         wr_s16b(quest[i].cur_num);
1282                         wr_s16b(quest[i].max_num);
1283                         wr_s16b(quest[i].type);
1284                         wr_s16b(quest[i].r_idx);
1285                         wr_s16b(quest[i].k_idx);
1286                         wr_byte(quest[i].flags);
1287                         wr_byte(quest[i].dungeon);
1288                 }
1289         }
1290
1291         /* Dump the position in the wilderness */
1292         wr_s32b(p_ptr->wilderness_x);
1293         wr_s32b(p_ptr->wilderness_y);
1294
1295         wr_byte(p_ptr->wild_mode);
1296         wr_byte(ambush_flag);
1297
1298         wr_s32b(max_wild_x);
1299         wr_s32b(max_wild_y);
1300
1301         /* Dump the wilderness seeds */
1302         for (i = 0; i < max_wild_x; i++)
1303         {
1304                 for (j = 0; j < max_wild_y; j++)
1305                 {
1306                         wr_u32b(wilderness[j][i].seed);
1307                 }
1308         }
1309
1310         /* Hack -- Dump the artifacts */
1311         tmp16u = max_a_idx;
1312         wr_u16b(tmp16u);
1313         for (i = 0; i < tmp16u; i++)
1314         {
1315                 artifact_type *a_ptr = &a_info[i];
1316                 wr_byte(a_ptr->cur_num);
1317                 wr_s16b(a_ptr->floor_id);
1318         }
1319
1320
1321
1322         /* Write the "extra" information */
1323         wr_extra();
1324
1325         /* Dump the "player hp" entries */
1326         tmp16u = PY_MAX_LEVEL;
1327         wr_u16b(tmp16u);
1328         for (i = 0; i < tmp16u; i++)
1329         {
1330                 wr_s16b(p_ptr->player_hp[i]);
1331         }
1332
1333
1334         /* Write spell data */
1335         wr_u32b(p_ptr->spell_learned1);
1336         wr_u32b(p_ptr->spell_learned2);
1337         wr_u32b(p_ptr->spell_worked1);
1338         wr_u32b(p_ptr->spell_worked2);
1339         wr_u32b(p_ptr->spell_forgotten1);
1340         wr_u32b(p_ptr->spell_forgotten2);
1341
1342         wr_s16b(p_ptr->learned_spells);
1343         wr_s16b(p_ptr->add_spells);
1344
1345         /* Dump the ordered spells */
1346         for (i = 0; i < 64; i++)
1347         {
1348                 wr_byte(p_ptr->spell_order[i]);
1349         }
1350
1351
1352         /* Write the inventory */
1353         for (i = 0; i < INVEN_TOTAL; i++)
1354         {
1355                 object_type *o_ptr = &inventory[i];
1356
1357                 /* Skip non-objects */
1358                 if (!o_ptr->k_idx) continue;
1359
1360                 /* Dump index */
1361                 wr_u16b((u16b)i);
1362
1363                 /* Dump object */
1364                 wr_item(o_ptr);
1365         }
1366
1367         /* Add a sentinel */
1368         wr_u16b(0xFFFF);
1369
1370         /* Note the towns */
1371         tmp16u = max_towns;
1372         wr_u16b(tmp16u);
1373
1374         /* Note the stores */
1375         tmp16u = MAX_STORES;
1376         wr_u16b(tmp16u);
1377
1378         /* Dump the stores of all towns */
1379         for (i = 1; i < max_towns; i++)
1380         {
1381                 for (j = 0; j < MAX_STORES; j++)
1382                 {
1383                         wr_store(&town[i].store[j]);
1384                 }
1385         }
1386
1387         /* Write the pet command settings */
1388         wr_s16b(p_ptr->pet_follow_distance);
1389         wr_s16b(p_ptr->pet_extra_flags);
1390
1391         /* Write screen dump for sending score */
1392         if (screen_dump && (p_ptr->wait_report_score || !p_ptr->is_dead))
1393         {
1394                 wr_string(screen_dump);
1395         }
1396         else
1397         {
1398                 wr_string("");
1399         }
1400
1401         /* Player is not dead, write the dungeon */
1402         if (!p_ptr->is_dead)
1403         {
1404                 /* Dump the dungeon */
1405                 if (!wr_dungeon()) return FALSE;
1406
1407                 /* Dump the ghost */
1408                 wr_ghost();
1409
1410                 /* No scripts */
1411                 wr_s32b(0);
1412         }
1413
1414
1415         /* Write the "value check-sum" */
1416         wr_u32b(v_stamp);
1417
1418         /* Write the "encoded checksum" */
1419         wr_u32b(x_stamp);
1420
1421
1422         /* Error in save */
1423         if (ferror(fff) || (fflush(fff) == EOF)) return FALSE;
1424
1425         /* Successful save */
1426         return TRUE;
1427 }
1428
1429
1430 /*
1431  * Medium level player saver
1432  *
1433  * XXX XXX XXX Angband 2.8.0 will use "fd" instead of "fff" if possible
1434  */
1435 static bool save_player_aux(char *name)
1436 {
1437         bool    ok = FALSE;
1438
1439         int             fd = -1;
1440
1441         int             mode = 0644;
1442
1443
1444         /* No file yet */
1445         fff = NULL;
1446
1447
1448         /* File type is "SAVE" */
1449         FILE_TYPE(FILE_TYPE_SAVE);
1450
1451
1452         /* Create the savefile */
1453         fd = fd_make(name, mode);
1454
1455         /* File is okay */
1456         if (fd >= 0)
1457         {
1458                 /* Close the "fd" */
1459                 (void)fd_close(fd);
1460
1461                 /* Open the savefile */
1462                 fff = my_fopen(name, "wb");
1463
1464                 /* Successful open */
1465                 if (fff)
1466                 {
1467                         /* Write the savefile */
1468                         if (wr_savefile_new()) ok = TRUE;
1469
1470                         /* Attempt to close it */
1471                         if (my_fclose(fff)) ok = FALSE;
1472                 }
1473
1474                 /* Remove "broken" files */
1475                 if (!ok) (void)fd_kill(name);
1476         }
1477
1478
1479         /* Failure */
1480         if (!ok) return (FALSE);
1481
1482         counts_write(0, playtime);
1483
1484         /* Successful save */
1485         character_saved = TRUE;
1486
1487         /* Success */
1488         return (TRUE);
1489 }
1490
1491
1492
1493 /*
1494  * Attempt to save the player in a savefile
1495  */
1496 bool save_player(void)
1497 {
1498         int             result = FALSE;
1499
1500         char    safe[1024];
1501
1502
1503 #ifdef SET_UID
1504
1505 # ifdef SECURE
1506
1507         /* Get "games" permissions */
1508         beGames();
1509
1510 # endif
1511
1512 #endif
1513
1514
1515         /* New savefile */
1516         strcpy(safe, savefile);
1517         strcat(safe, ".new");
1518
1519 #ifdef VM
1520         /* Hack -- support "flat directory" usage on VM/ESA */
1521         strcpy(safe, savefile);
1522         strcat(safe, "n");
1523 #endif /* VM */
1524
1525         /* Remove it */
1526         fd_kill(safe);
1527
1528         update_playtime();
1529
1530         /* Attempt to save the player */
1531         if (save_player_aux(safe))
1532         {
1533                 char temp[1024];
1534
1535                 /* Old savefile */
1536                 strcpy(temp, savefile);
1537                 strcat(temp, ".old");
1538
1539 #ifdef VM
1540                 /* Hack -- support "flat directory" usage on VM/ESA */
1541                 strcpy(temp, savefile);
1542                 strcat(temp, "o");
1543 #endif /* VM */
1544
1545                 /* Remove it */
1546                 fd_kill(temp);
1547
1548                 /* Preserve old savefile */
1549                 fd_move(savefile, temp);
1550
1551                 /* Activate new savefile */
1552                 fd_move(safe, savefile);
1553
1554                 /* Remove preserved savefile */
1555                 fd_kill(temp);
1556
1557                 /* Hack -- Pretend the character was loaded */
1558                 character_loaded = TRUE;
1559
1560 #ifdef VERIFY_SAVEFILE
1561
1562                 /* Lock on savefile */
1563                 strcpy(temp, savefile);
1564                 strcat(temp, ".lok");
1565
1566                 /* Remove lock file */
1567                 fd_kill(temp);
1568
1569 #endif
1570
1571                 /* Success */
1572                 result = TRUE;
1573         }
1574
1575
1576 #ifdef SET_UID
1577
1578 # ifdef SECURE
1579
1580         /* Drop "games" permissions */
1581         bePlayer();
1582
1583 # endif
1584
1585 #endif
1586
1587         /* Return the result */
1588         return (result);
1589 }
1590
1591
1592
1593 /*
1594  * Attempt to Load a "savefile"
1595  *
1596  * Version 2.7.0 introduced a slightly different "savefile" format from
1597  * older versions, requiring a completely different parsing method.
1598  *
1599  * Note that savefiles from 2.7.0 - 2.7.2 are completely obsolete.
1600  *
1601  * Pre-2.8.0 savefiles lose some data, see "load2.c" for info.
1602  *
1603  * Pre-2.7.0 savefiles lose a lot of things, see "load1.c" for info.
1604  *
1605  * On multi-user systems, you may only "read" a savefile if you will be
1606  * allowed to "write" it later, this prevents painful situations in which
1607  * the player loads a savefile belonging to someone else, and then is not
1608  * allowed to save his game when he quits.
1609  *
1610  * We return "TRUE" if the savefile was usable, and we set the global
1611  * flag "character_loaded" if a real, living, character was loaded.
1612  *
1613  * Note that we always try to load the "current" savefile, even if
1614  * there is no such file, so we must check for "empty" savefile names.
1615  */
1616 bool load_player(void)
1617 {
1618         int             fd = -1;
1619
1620         errr    err = 0;
1621
1622         byte    vvv[4];
1623
1624 #ifdef VERIFY_TIMESTAMP
1625         struct stat     statbuf;
1626 #endif
1627
1628         cptr    what = "generic";
1629
1630
1631         /* Paranoia */
1632         turn = 0;
1633
1634         /* Paranoia */
1635         p_ptr->is_dead = FALSE;
1636
1637
1638         /* Allow empty savefile name */
1639         if (!savefile[0]) return (TRUE);
1640
1641
1642 #if !defined(MACINTOSH) && !defined(WINDOWS) && !defined(VM)
1643
1644         /* XXX XXX XXX Fix this */
1645
1646         /* Verify the existance of the savefile */
1647         if (access(savefile, 0) < 0)
1648         {
1649                 /* Give a message */
1650 #ifdef JP
1651                 msg_print("¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤¬¤¢¤ê¤Þ¤»¤ó¡£");
1652 #else
1653                 msg_print("Savefile does not exist.");
1654 #endif
1655
1656                 msg_print(NULL);
1657
1658                 /* Allow this */
1659                 return (TRUE);
1660         }
1661
1662 #endif
1663
1664
1665 #ifdef VERIFY_SAVEFILE
1666
1667         /* Verify savefile usage */
1668         if (!err)
1669         {
1670                 FILE *fkk;
1671
1672                 char temp[1024];
1673
1674                 /* Extract name of lock file */
1675                 strcpy(temp, savefile);
1676                 strcat(temp, ".lok");
1677
1678                 /* Check for lock */
1679                 fkk = my_fopen(temp, "r");
1680
1681                 /* Oops, lock exists */
1682                 if (fkk)
1683                 {
1684                         /* Close the file */
1685                         my_fclose(fkk);
1686
1687                         /* Message */
1688 #ifdef JP
1689                         msg_print("¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤Ï¸½ºß»ÈÍÑÃæ¤Ç¤¹¡£");
1690 #else
1691                         msg_print("Savefile is currently in use.");
1692 #endif
1693
1694                         msg_print(NULL);
1695
1696                         /* Oops */
1697                         return (FALSE);
1698                 }
1699
1700                 /* Create a lock file */
1701                 fkk = my_fopen(temp, "w");
1702
1703                 /* Dump a line of info */
1704                 fprintf(fkk, "Lock file for savefile '%s'\n", savefile);
1705
1706                 /* Close the lock file */
1707                 my_fclose(fkk);
1708         }
1709
1710 #endif
1711
1712
1713         /* Okay */
1714         if (!err)
1715         {
1716                 /* Open the savefile */
1717                 fd = fd_open(savefile, O_RDONLY);
1718
1719                 /* No file */
1720                 if (fd < 0) err = -1;
1721
1722                 /* Message (below) */
1723 #ifdef JP
1724                 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤ò³«¤±¤Þ¤»¤ó¡£";
1725 #else
1726                 if (err) what = "Cannot open savefile";
1727 #endif
1728
1729         }
1730
1731         /* Process file */
1732         if (!err)
1733         {
1734
1735 #ifdef VERIFY_TIMESTAMP
1736                 /* Get the timestamp */
1737                 (void)fstat(fd, &statbuf);
1738 #endif
1739
1740                 /* Read the first four bytes */
1741                 if (fd_read(fd, (char*)(vvv), 4)) err = -1;
1742
1743                 /* What */
1744 #ifdef JP
1745                 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤òÆɤá¤Þ¤»¤ó¡£";
1746 #else
1747                 if (err) what = "Cannot read savefile";
1748 #endif
1749
1750
1751                 /* Close the file */
1752                 (void)fd_close(fd);
1753         }
1754
1755         /* Process file */
1756         if (!err)
1757         {
1758
1759                 /* Extract version */
1760                 z_major = vvv[0];
1761                 z_minor = vvv[1];
1762                 z_patch = vvv[2];
1763                 sf_extra = vvv[3];
1764
1765
1766                 /* Clear screen */
1767                 Term_clear();
1768
1769                 /* Attempt to load */
1770                 err = rd_savefile_new();
1771
1772                 /* Message (below) */
1773 #ifdef JP
1774                 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤ò²òÀϽÐÍè¤Þ¤»¤ó¡£";
1775 #else
1776                 if (err) what = "Cannot parse savefile";
1777 #endif
1778
1779         }
1780
1781         /* Paranoia */
1782         if (!err)
1783         {
1784                 /* Invalid turn */
1785                 if (!turn) err = -1;
1786
1787                 /* Message (below) */
1788 #ifdef JP
1789                 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤¬²õ¤ì¤Æ¤¤¤Þ¤¹";
1790 #else
1791                 if (err) what = "Broken savefile";
1792 #endif
1793
1794         }
1795
1796 #ifdef VERIFY_TIMESTAMP
1797         /* Verify timestamp */
1798         if (!err && !arg_wizard)
1799         {
1800                 /* Hack -- Verify the timestamp */
1801                 if (sf_when > (statbuf.st_ctime + 100) ||
1802                     sf_when < (statbuf.st_ctime - 100))
1803                 {
1804                         /* Message */
1805 #ifdef JP
1806                         what = "̵¸ú¤Ê¥¿¥¤¥à¡¦¥¹¥¿¥ó¥×¤Ç¤¹";
1807 #else
1808                         what = "Invalid timestamp";
1809 #endif
1810
1811
1812                         /* Oops */
1813                         err = -1;
1814                 }
1815         }
1816 #endif
1817
1818
1819         /* Okay */
1820         if (!err)
1821         {
1822                 /* Give a conversion warning */
1823                 if ((FAKE_VER_MAJOR != z_major) ||
1824                     (FAKE_VER_MINOR != z_minor) ||
1825                     (FAKE_VER_PATCH != z_patch))
1826                 {
1827                         if (z_major == 2 && z_minor == 0 && z_patch == 6)
1828                         {
1829 #ifdef JP
1830                                 msg_print("¥Ð¡¼¥¸¥ç¥ó 2.0.* ÍѤΥ»¡¼¥Ö¥Õ¥¡¥¤¥ë¤òÊÑ´¹¤·¤Þ¤·¤¿¡£");
1831 #else
1832                                 msg_print("Converted a 2.0.* savefile.");
1833 #endif
1834
1835                         }
1836                         else
1837                         {
1838                                 /* Message */
1839 #ifdef JP
1840                                 msg_format("¥Ð¡¼¥¸¥ç¥ó %d.%d.%d ÍѤΥ»¡¼¥Ö¡¦¥Õ¥¡¥¤¥ë¤òÊÑ´¹¤·¤Þ¤·¤¿¡£",
1841                                     (z_major > 9) ? z_major-10 : z_major , z_minor, z_patch);
1842 #else
1843                                 msg_format("Converted a %d.%d.%d savefile.",
1844                                     (z_major > 9) ? z_major-10 : z_major , z_minor, z_patch);
1845 #endif
1846                         }
1847                         msg_print(NULL);
1848                 }
1849
1850                 /* Player is dead */
1851                 if (p_ptr->is_dead)
1852                 {
1853                         /* Cheat death */
1854                         if (arg_wizard)
1855                         {
1856                                 /* A character was loaded */
1857                                 character_loaded = TRUE;
1858
1859                                 /* Done */
1860                                 return (TRUE);
1861                         }
1862
1863                         /* Player is no longer "dead" */
1864                         p_ptr->is_dead = FALSE;
1865
1866                         /* Count lives */
1867                         sf_lives++;
1868
1869                         /* Forget turns */
1870                         turn = old_turn = 0;
1871
1872                         /* Done */
1873                         return (TRUE);
1874                 }
1875
1876                 /* A character was loaded */
1877                 character_loaded = TRUE;
1878
1879                 {
1880                         u32b tmp = counts_read(2);
1881                         if (tmp > p_ptr->count)
1882                                 p_ptr->count = tmp;
1883                         if (counts_read(0) > playtime || counts_read(1) == playtime)
1884                                 counts_write(2, ++p_ptr->count);
1885                         counts_write(1, playtime);
1886                 }
1887
1888                 /* Success */
1889                 return (TRUE);
1890         }
1891
1892
1893 #ifdef VERIFY_SAVEFILE
1894
1895         /* Verify savefile usage */
1896         if (TRUE)
1897         {
1898                 char temp[1024];
1899
1900                 /* Extract name of lock file */
1901                 strcpy(temp, savefile);
1902                 strcat(temp, ".lok");
1903
1904                 /* Remove lock */
1905                 fd_kill(temp);
1906         }
1907
1908 #endif
1909
1910
1911         /* Message */
1912 #ifdef JP
1913         msg_format("¥¨¥é¡¼(%s)¤¬¥Ð¡¼¥¸¥ç¥ó%d.%d.%d ÍÑ¥»¡¼¥Ö¥Õ¥¡¥¤¥ëÆɤ߹þÃæ¤ËȯÀ¸¡£",
1914                    what, (z_major>9) ? z_major - 10 : z_major, z_minor, z_patch);
1915 #else
1916         msg_format("Error (%s) reading %d.%d.%d savefile.",
1917                    what, (z_major>9) ? z_major - 10 : z_major, z_minor, z_patch);
1918 #endif
1919         msg_print(NULL);
1920
1921         /* Oops */
1922         return (FALSE);
1923 }
1924
1925
1926 void remove_loc(void)
1927 {
1928 #ifdef VERIFY_SAVEFILE
1929         char temp[1024];
1930 #endif /* VERIFY_SAVEFILE */
1931
1932 #ifdef SET_UID
1933 # ifdef SECURE
1934
1935         /* Get "games" permissions */
1936         beGames();
1937
1938 # endif /* SECURE */
1939 #endif /* SET_UID */
1940
1941 #ifdef VERIFY_SAVEFILE
1942
1943         /* Lock on savefile */
1944         strcpy(temp, savefile);
1945         strcat(temp, ".lok");
1946
1947         /* Remove lock file */
1948         fd_kill(temp);
1949
1950 #endif /* VERIFY_SAVEFILE */
1951
1952 #ifdef SET_UID
1953 # ifdef SECURE
1954
1955         /* Drop "games" permissions */
1956         bePlayer();
1957
1958 # endif /* SECURE */
1959 #endif /* SET_UID */
1960
1961 }
1962
1963
1964 /*
1965  * Actually write a temporal saved floor file
1966  */
1967 static bool save_floor_aux(saved_floor_type *sf_ptr)
1968 {
1969         byte tmp8u;
1970
1971         /* Compact the objects */
1972         compact_objects(0);
1973         /* Compact the monsters */
1974         compact_monsters(0);
1975
1976
1977         /*** Actually write the file ***/
1978
1979         /* Initial value of xor_byte */
1980         tmp8u = (byte)randint0(256);
1981         xor_byte = 0;
1982         wr_byte(tmp8u);
1983
1984
1985         /* Reset the checksum */
1986         v_stamp = 0L;
1987         x_stamp = 0L;
1988
1989         /* Write the sign of this process */
1990         wr_u32b(saved_floor_file_sign);
1991
1992         /* Dump the dungeon floor */
1993         wr_saved_floor(sf_ptr);
1994
1995
1996         /* Write the "value check-sum" */
1997         wr_u32b(v_stamp);
1998
1999         /* Write the "encoded checksum" */
2000         wr_u32b(x_stamp);
2001
2002
2003         /* Error in save */
2004         if (ferror(fff) || (fflush(fff) == EOF)) return FALSE;
2005
2006         /* Successful save */
2007         return TRUE;
2008 }
2009
2010
2011 /*
2012  * Attempt to save the temporally saved-floor data
2013  */
2014 bool save_floor(saved_floor_type *sf_ptr, u32b mode)
2015 {
2016         FILE *old_fff = NULL;
2017         byte old_xor_byte = 0;
2018         u32b old_v_stamp = 0;
2019         u32b old_x_stamp = 0;
2020
2021         char floor_savefile[1024];
2022         int fd = -1;
2023         bool ok = FALSE;
2024
2025         if (!(mode & SLF_SECOND))
2026         {
2027 #ifdef SET_UID
2028 # ifdef SECURE
2029                 /* Get "games" permissions */
2030                 beGames();
2031 # endif
2032 #endif
2033         }
2034
2035         /* We have one file already opened */
2036         else
2037         {
2038                 /* Backup original values */
2039                 old_fff = fff;
2040                 old_xor_byte = xor_byte;
2041                 old_v_stamp = v_stamp;
2042                 old_x_stamp = x_stamp;
2043         }
2044
2045         /* New savefile */
2046         sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id);
2047
2048         /* Remove it */
2049         fd_kill(floor_savefile);
2050
2051         /* Attempt to save the player */
2052
2053         /* No file yet */
2054         fff = NULL;
2055
2056         /* File type is "SAVE" */
2057         FILE_TYPE(FILE_TYPE_SAVE);
2058
2059         /* Create the savefile */
2060         fd = fd_make(floor_savefile, 0644);
2061
2062         /* File is okay */
2063         if (fd >= 0)
2064         {
2065                 /* Close the "fd" */
2066                 (void)fd_close(fd);
2067
2068                 /* Open the savefile */
2069                 fff = my_fopen(floor_savefile, "wb");
2070
2071                 /* Successful open */
2072                 if (fff)
2073                 {
2074                         /* Write the savefile */
2075                         if (save_floor_aux(sf_ptr)) ok = TRUE;
2076
2077                         /* Attempt to close it */
2078                         if (my_fclose(fff)) ok = FALSE;
2079                 }
2080
2081                 /* Remove "broken" files */
2082                 if (!ok) (void)fd_kill(floor_savefile);
2083         }
2084
2085         if (!(mode & SLF_SECOND))
2086         {
2087 #ifdef SET_UID
2088 # ifdef SECURE
2089                 /* Drop "games" permissions */
2090                 bePlayer();
2091 # endif
2092 #endif
2093         }
2094
2095         /* We have one file already opened */
2096         else
2097         {
2098                 /* Restore original values */
2099                 fff = old_fff;
2100                 xor_byte = old_xor_byte;
2101                 v_stamp = old_v_stamp;
2102                 x_stamp = old_x_stamp;
2103         }
2104
2105         /* Return the result */
2106         return ok;
2107 }