OSDN Git Service

セーブ時に地上では忘れた明かりの再計算をしないようになってたのを修正。
[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         /* Update lite/view */
1059         p_ptr->update |= (PU_VIEW | PU_LITE | PU_MON_LITE);
1060
1061         /* Update monsters */
1062         p_ptr->update |= (PU_MONSTERS | PU_DISTANCE | PU_FLOW);
1063
1064
1065         /*** Meta info ***/
1066
1067         /* Number of floor_id used from birth */
1068         wr_s16b(max_floor_id);
1069
1070         /* Current dungeon type */
1071         wr_byte(dungeon_type);
1072
1073
1074         /*** On the surface  ***/
1075         if (!p_ptr->floor_id)
1076         {
1077                 /* No array elements */
1078                 wr_byte(0);
1079
1080                 /* Write the current floor data */
1081                 wr_saved_floor(NULL);
1082
1083                 /* Success */
1084                 return TRUE;
1085         }
1086
1087
1088         /*** In the dungeon ***/
1089
1090         /* Number of array elements */
1091         wr_byte(MAX_SAVED_FLOORS);
1092
1093         /* Write the saved_floors array */
1094         for (i = 0; i < MAX_SAVED_FLOORS; i++)
1095         {
1096                 saved_floor_type *sf_ptr = &saved_floors[i];
1097
1098                 wr_s16b(sf_ptr->floor_id);
1099                 wr_byte(sf_ptr->savefile_id);
1100                 wr_s16b(sf_ptr->dun_level);
1101                 wr_s32b(sf_ptr->last_visit);
1102                 wr_u32b(sf_ptr->visit_mark);
1103                 wr_s16b(sf_ptr->upper_floor_id);
1104                 wr_s16b(sf_ptr->lower_floor_id);
1105         }
1106
1107         /* Extract pointer to current floor */
1108         cur_sf_ptr = get_sf_ptr(p_ptr->floor_id);
1109
1110         /* Save current floor to temporal file */
1111         if (!save_floor(cur_sf_ptr, (SLF_SECOND))) return FALSE;
1112
1113         /* Move data in temporal files to the savefile */
1114         for (i = 0; i < MAX_SAVED_FLOORS; i++)
1115         {
1116                 saved_floor_type *sf_ptr = &saved_floors[i];
1117
1118                 /* Unused element */
1119                 if (!sf_ptr->floor_id) continue;
1120
1121                 /* Load temporal saved floor file */
1122                 if (load_floor(sf_ptr, (SLF_SECOND | SLF_NO_KILL)))
1123                 {
1124                         /* Mark success */
1125                         wr_byte(0);
1126
1127                         /* Write saved floor data to the save file */
1128                         wr_saved_floor(sf_ptr);
1129                 }
1130                 else
1131                 {
1132                         /* Mark failure */
1133                         wr_byte(1);
1134                 }
1135         }
1136
1137         /* Restore current floor */
1138         if (!load_floor(cur_sf_ptr, (SLF_SECOND))) return FALSE;
1139
1140         /* Success */
1141         return TRUE;
1142 }
1143
1144
1145
1146 /*
1147  * Actually write a save-file
1148  */
1149 static bool wr_savefile_new(void)
1150 {
1151         int        i, j;
1152
1153         u32b              now;
1154
1155         byte            tmp8u;
1156         u16b            tmp16u;
1157
1158
1159         /* Compact the objects */
1160         compact_objects(0);
1161         /* Compact the monsters */
1162         compact_monsters(0);
1163
1164         /* Guess at the current time */
1165         now = time((time_t *)0);
1166
1167
1168         /* Note the operating system */
1169         sf_system = 0L;
1170
1171         /* Note when the file was saved */
1172         sf_when = now;
1173
1174         /* Note the number of saves */
1175         sf_saves++;
1176
1177
1178         /*** Actually write the file ***/
1179
1180         /* Dump the file header */
1181         xor_byte = 0;
1182         wr_byte(FAKE_VER_MAJOR);
1183         xor_byte = 0;
1184         wr_byte(FAKE_VER_MINOR);
1185         xor_byte = 0;
1186         wr_byte(FAKE_VER_PATCH);
1187         xor_byte = 0;
1188
1189         /* Initial value of xor_byte */
1190         tmp8u = (byte)randint0(256);
1191         wr_byte(tmp8u);
1192
1193
1194         /* Reset the checksum */
1195         v_stamp = 0L;
1196         x_stamp = 0L;
1197
1198         /* Write the savefile version for Hengband 1.1.1 and later */
1199         wr_byte(H_VER_EXTRA);
1200         wr_byte(H_VER_PATCH);
1201         wr_byte(H_VER_MINOR);
1202         wr_byte(H_VER_MAJOR);
1203
1204         /* Operating system */
1205         wr_u32b(sf_system);
1206
1207
1208         /* Time file last saved */
1209         wr_u32b(sf_when);
1210
1211         /* Number of past lives */
1212         wr_u16b(sf_lives);
1213
1214         /* Number of times saved */
1215         wr_u16b(sf_saves);
1216
1217
1218         /* Space */
1219         wr_u32b(0L);
1220         wr_u32b(0L);
1221
1222
1223         /* Write the RNG state */
1224         wr_randomizer();
1225
1226
1227         /* Write the boolean "options" */
1228         wr_options();
1229
1230
1231         /* Dump the number of "messages" */
1232         tmp16u = message_num();
1233         if (compress_savefile && (tmp16u > 40)) tmp16u = 40;
1234         wr_u16b(tmp16u);
1235
1236         /* Dump the messages (oldest first!) */
1237         for (i = tmp16u - 1; i >= 0; i--)
1238         {
1239                 wr_string(message_str((s16b)i));
1240         }
1241
1242
1243         /* Dump the monster lore */
1244         tmp16u = max_r_idx;
1245         wr_u16b(tmp16u);
1246         for (i = 0; i < tmp16u; i++) wr_lore(i);
1247
1248
1249         /* Dump the object memory */
1250         tmp16u = max_k_idx;
1251         wr_u16b(tmp16u);
1252         for (i = 0; i < tmp16u; i++) wr_xtra(i);
1253
1254         /* Dump the towns */
1255         tmp16u = max_towns;
1256         wr_u16b(tmp16u);
1257
1258         /* Dump the quests */
1259         tmp16u = max_quests;
1260         wr_u16b(tmp16u);
1261
1262         /* Dump the quests */
1263         tmp8u = MAX_RANDOM_QUEST-MIN_RANDOM_QUEST;
1264         wr_byte(tmp8u);
1265
1266         for (i = 0; i < max_quests; i++)
1267         {
1268                 /* Save status for every quest */
1269                 wr_s16b(quest[i].status);
1270
1271                 /* And the dungeon level too */
1272                 /* (prevents problems with multi-level quests) */
1273                 wr_s16b(quest[i].level);
1274
1275                 wr_byte(quest[i].complev);
1276
1277                 /* Save quest status if quest is running */
1278                 if (quest[i].status == QUEST_STATUS_TAKEN || quest[i].status == QUEST_STATUS_COMPLETED || ((i >= MIN_RANDOM_QUEST) && (i <= MAX_RANDOM_QUEST)))
1279                 {
1280                         wr_s16b(quest[i].cur_num);
1281                         wr_s16b(quest[i].max_num);
1282                         wr_s16b(quest[i].type);
1283                         wr_s16b(quest[i].r_idx);
1284                         wr_s16b(quest[i].k_idx);
1285                         wr_byte(quest[i].flags);
1286                         wr_byte(quest[i].dungeon);
1287                 }
1288         }
1289
1290         /* Dump the position in the wilderness */
1291         wr_s32b(p_ptr->wilderness_x);
1292         wr_s32b(p_ptr->wilderness_y);
1293
1294         wr_byte(p_ptr->wild_mode);
1295         wr_byte(ambush_flag);
1296
1297         wr_s32b(max_wild_x);
1298         wr_s32b(max_wild_y);
1299
1300         /* Dump the wilderness seeds */
1301         for (i = 0; i < max_wild_x; i++)
1302         {
1303                 for (j = 0; j < max_wild_y; j++)
1304                 {
1305                         wr_u32b(wilderness[j][i].seed);
1306                 }
1307         }
1308
1309         /* Hack -- Dump the artifacts */
1310         tmp16u = max_a_idx;
1311         wr_u16b(tmp16u);
1312         for (i = 0; i < tmp16u; i++)
1313         {
1314                 artifact_type *a_ptr = &a_info[i];
1315                 wr_byte(a_ptr->cur_num);
1316                 wr_s16b(a_ptr->floor_id);
1317         }
1318
1319
1320
1321         /* Write the "extra" information */
1322         wr_extra();
1323
1324         /* Dump the "player hp" entries */
1325         tmp16u = PY_MAX_LEVEL;
1326         wr_u16b(tmp16u);
1327         for (i = 0; i < tmp16u; i++)
1328         {
1329                 wr_s16b(p_ptr->player_hp[i]);
1330         }
1331
1332
1333         /* Write spell data */
1334         wr_u32b(p_ptr->spell_learned1);
1335         wr_u32b(p_ptr->spell_learned2);
1336         wr_u32b(p_ptr->spell_worked1);
1337         wr_u32b(p_ptr->spell_worked2);
1338         wr_u32b(p_ptr->spell_forgotten1);
1339         wr_u32b(p_ptr->spell_forgotten2);
1340
1341         wr_s16b(p_ptr->learned_spells);
1342         wr_s16b(p_ptr->add_spells);
1343
1344         /* Dump the ordered spells */
1345         for (i = 0; i < 64; i++)
1346         {
1347                 wr_byte(p_ptr->spell_order[i]);
1348         }
1349
1350
1351         /* Write the inventory */
1352         for (i = 0; i < INVEN_TOTAL; i++)
1353         {
1354                 object_type *o_ptr = &inventory[i];
1355
1356                 /* Skip non-objects */
1357                 if (!o_ptr->k_idx) continue;
1358
1359                 /* Dump index */
1360                 wr_u16b((u16b)i);
1361
1362                 /* Dump object */
1363                 wr_item(o_ptr);
1364         }
1365
1366         /* Add a sentinel */
1367         wr_u16b(0xFFFF);
1368
1369         /* Note the towns */
1370         tmp16u = max_towns;
1371         wr_u16b(tmp16u);
1372
1373         /* Note the stores */
1374         tmp16u = MAX_STORES;
1375         wr_u16b(tmp16u);
1376
1377         /* Dump the stores of all towns */
1378         for (i = 1; i < max_towns; i++)
1379         {
1380                 for (j = 0; j < MAX_STORES; j++)
1381                 {
1382                         wr_store(&town[i].store[j]);
1383                 }
1384         }
1385
1386         /* Write the pet command settings */
1387         wr_s16b(p_ptr->pet_follow_distance);
1388         wr_s16b(p_ptr->pet_extra_flags);
1389
1390         /* Write screen dump for sending score */
1391         if (screen_dump && (p_ptr->wait_report_score || !p_ptr->is_dead))
1392         {
1393                 wr_string(screen_dump);
1394         }
1395         else
1396         {
1397                 wr_string("");
1398         }
1399
1400         /* Player is not dead, write the dungeon */
1401         if (!p_ptr->is_dead)
1402         {
1403                 /* Dump the dungeon */
1404                 if (!wr_dungeon()) return FALSE;
1405
1406                 /* Dump the ghost */
1407                 wr_ghost();
1408
1409                 /* No scripts */
1410                 wr_s32b(0);
1411         }
1412
1413
1414         /* Write the "value check-sum" */
1415         wr_u32b(v_stamp);
1416
1417         /* Write the "encoded checksum" */
1418         wr_u32b(x_stamp);
1419
1420
1421         /* Error in save */
1422         if (ferror(fff) || (fflush(fff) == EOF)) return FALSE;
1423
1424         /* Successful save */
1425         return TRUE;
1426 }
1427
1428
1429 /*
1430  * Medium level player saver
1431  *
1432  * XXX XXX XXX Angband 2.8.0 will use "fd" instead of "fff" if possible
1433  */
1434 static bool save_player_aux(char *name)
1435 {
1436         bool    ok = FALSE;
1437
1438         int             fd = -1;
1439
1440         int             mode = 0644;
1441
1442
1443         /* No file yet */
1444         fff = NULL;
1445
1446
1447         /* File type is "SAVE" */
1448         FILE_TYPE(FILE_TYPE_SAVE);
1449
1450
1451         /* Create the savefile */
1452         fd = fd_make(name, mode);
1453
1454         /* File is okay */
1455         if (fd >= 0)
1456         {
1457                 /* Close the "fd" */
1458                 (void)fd_close(fd);
1459
1460                 /* Open the savefile */
1461                 fff = my_fopen(name, "wb");
1462
1463                 /* Successful open */
1464                 if (fff)
1465                 {
1466                         /* Write the savefile */
1467                         if (wr_savefile_new()) ok = TRUE;
1468
1469                         /* Attempt to close it */
1470                         if (my_fclose(fff)) ok = FALSE;
1471                 }
1472
1473                 /* Remove "broken" files */
1474                 if (!ok) (void)fd_kill(name);
1475         }
1476
1477
1478         /* Failure */
1479         if (!ok) return (FALSE);
1480
1481         counts_write(0, playtime);
1482
1483         /* Successful save */
1484         character_saved = TRUE;
1485
1486         /* Success */
1487         return (TRUE);
1488 }
1489
1490
1491
1492 /*
1493  * Attempt to save the player in a savefile
1494  */
1495 bool save_player(void)
1496 {
1497         int             result = FALSE;
1498
1499         char    safe[1024];
1500
1501
1502 #ifdef SET_UID
1503
1504 # ifdef SECURE
1505
1506         /* Get "games" permissions */
1507         beGames();
1508
1509 # endif
1510
1511 #endif
1512
1513
1514         /* New savefile */
1515         strcpy(safe, savefile);
1516         strcat(safe, ".new");
1517
1518 #ifdef VM
1519         /* Hack -- support "flat directory" usage on VM/ESA */
1520         strcpy(safe, savefile);
1521         strcat(safe, "n");
1522 #endif /* VM */
1523
1524         /* Remove it */
1525         fd_kill(safe);
1526
1527         update_playtime();
1528
1529         /* Attempt to save the player */
1530         if (save_player_aux(safe))
1531         {
1532                 char temp[1024];
1533
1534                 /* Old savefile */
1535                 strcpy(temp, savefile);
1536                 strcat(temp, ".old");
1537
1538 #ifdef VM
1539                 /* Hack -- support "flat directory" usage on VM/ESA */
1540                 strcpy(temp, savefile);
1541                 strcat(temp, "o");
1542 #endif /* VM */
1543
1544                 /* Remove it */
1545                 fd_kill(temp);
1546
1547                 /* Preserve old savefile */
1548                 fd_move(savefile, temp);
1549
1550                 /* Activate new savefile */
1551                 fd_move(safe, savefile);
1552
1553                 /* Remove preserved savefile */
1554                 fd_kill(temp);
1555
1556                 /* Hack -- Pretend the character was loaded */
1557                 character_loaded = TRUE;
1558
1559 #ifdef VERIFY_SAVEFILE
1560
1561                 /* Lock on savefile */
1562                 strcpy(temp, savefile);
1563                 strcat(temp, ".lok");
1564
1565                 /* Remove lock file */
1566                 fd_kill(temp);
1567
1568 #endif
1569
1570                 /* Success */
1571                 result = TRUE;
1572         }
1573
1574
1575 #ifdef SET_UID
1576
1577 # ifdef SECURE
1578
1579         /* Drop "games" permissions */
1580         bePlayer();
1581
1582 # endif
1583
1584 #endif
1585
1586         /* Return the result */
1587         return (result);
1588 }
1589
1590
1591
1592 /*
1593  * Attempt to Load a "savefile"
1594  *
1595  * Version 2.7.0 introduced a slightly different "savefile" format from
1596  * older versions, requiring a completely different parsing method.
1597  *
1598  * Note that savefiles from 2.7.0 - 2.7.2 are completely obsolete.
1599  *
1600  * Pre-2.8.0 savefiles lose some data, see "load2.c" for info.
1601  *
1602  * Pre-2.7.0 savefiles lose a lot of things, see "load1.c" for info.
1603  *
1604  * On multi-user systems, you may only "read" a savefile if you will be
1605  * allowed to "write" it later, this prevents painful situations in which
1606  * the player loads a savefile belonging to someone else, and then is not
1607  * allowed to save his game when he quits.
1608  *
1609  * We return "TRUE" if the savefile was usable, and we set the global
1610  * flag "character_loaded" if a real, living, character was loaded.
1611  *
1612  * Note that we always try to load the "current" savefile, even if
1613  * there is no such file, so we must check for "empty" savefile names.
1614  */
1615 bool load_player(void)
1616 {
1617         int             fd = -1;
1618
1619         errr    err = 0;
1620
1621         byte    vvv[4];
1622
1623 #ifdef VERIFY_TIMESTAMP
1624         struct stat     statbuf;
1625 #endif
1626
1627         cptr    what = "generic";
1628
1629
1630         /* Paranoia */
1631         turn = 0;
1632
1633         /* Paranoia */
1634         p_ptr->is_dead = FALSE;
1635
1636
1637         /* Allow empty savefile name */
1638         if (!savefile[0]) return (TRUE);
1639
1640
1641 #if !defined(MACINTOSH) && !defined(WINDOWS) && !defined(VM)
1642
1643         /* XXX XXX XXX Fix this */
1644
1645         /* Verify the existance of the savefile */
1646         if (access(savefile, 0) < 0)
1647         {
1648                 /* Give a message */
1649 #ifdef JP
1650                 msg_print("¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤¬¤¢¤ê¤Þ¤»¤ó¡£");
1651 #else
1652                 msg_print("Savefile does not exist.");
1653 #endif
1654
1655                 msg_print(NULL);
1656
1657                 /* Allow this */
1658                 return (TRUE);
1659         }
1660
1661 #endif
1662
1663
1664 #ifdef VERIFY_SAVEFILE
1665
1666         /* Verify savefile usage */
1667         if (!err)
1668         {
1669                 FILE *fkk;
1670
1671                 char temp[1024];
1672
1673                 /* Extract name of lock file */
1674                 strcpy(temp, savefile);
1675                 strcat(temp, ".lok");
1676
1677                 /* Check for lock */
1678                 fkk = my_fopen(temp, "r");
1679
1680                 /* Oops, lock exists */
1681                 if (fkk)
1682                 {
1683                         /* Close the file */
1684                         my_fclose(fkk);
1685
1686                         /* Message */
1687 #ifdef JP
1688                         msg_print("¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤Ï¸½ºß»ÈÍÑÃæ¤Ç¤¹¡£");
1689 #else
1690                         msg_print("Savefile is currently in use.");
1691 #endif
1692
1693                         msg_print(NULL);
1694
1695                         /* Oops */
1696                         return (FALSE);
1697                 }
1698
1699                 /* Create a lock file */
1700                 fkk = my_fopen(temp, "w");
1701
1702                 /* Dump a line of info */
1703                 fprintf(fkk, "Lock file for savefile '%s'\n", savefile);
1704
1705                 /* Close the lock file */
1706                 my_fclose(fkk);
1707         }
1708
1709 #endif
1710
1711
1712         /* Okay */
1713         if (!err)
1714         {
1715                 /* Open the savefile */
1716                 fd = fd_open(savefile, O_RDONLY);
1717
1718                 /* No file */
1719                 if (fd < 0) err = -1;
1720
1721                 /* Message (below) */
1722 #ifdef JP
1723                 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤ò³«¤±¤Þ¤»¤ó¡£";
1724 #else
1725                 if (err) what = "Cannot open savefile";
1726 #endif
1727
1728         }
1729
1730         /* Process file */
1731         if (!err)
1732         {
1733
1734 #ifdef VERIFY_TIMESTAMP
1735                 /* Get the timestamp */
1736                 (void)fstat(fd, &statbuf);
1737 #endif
1738
1739                 /* Read the first four bytes */
1740                 if (fd_read(fd, (char*)(vvv), 4)) err = -1;
1741
1742                 /* What */
1743 #ifdef JP
1744                 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤òÆɤá¤Þ¤»¤ó¡£";
1745 #else
1746                 if (err) what = "Cannot read savefile";
1747 #endif
1748
1749
1750                 /* Close the file */
1751                 (void)fd_close(fd);
1752         }
1753
1754         /* Process file */
1755         if (!err)
1756         {
1757
1758                 /* Extract version */
1759                 z_major = vvv[0];
1760                 z_minor = vvv[1];
1761                 z_patch = vvv[2];
1762                 sf_extra = vvv[3];
1763
1764
1765                 /* Clear screen */
1766                 Term_clear();
1767
1768                 /* Attempt to load */
1769                 err = rd_savefile_new();
1770
1771                 /* Message (below) */
1772 #ifdef JP
1773                 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤ò²òÀϽÐÍè¤Þ¤»¤ó¡£";
1774 #else
1775                 if (err) what = "Cannot parse savefile";
1776 #endif
1777
1778         }
1779
1780         /* Paranoia */
1781         if (!err)
1782         {
1783                 /* Invalid turn */
1784                 if (!turn) err = -1;
1785
1786                 /* Message (below) */
1787 #ifdef JP
1788                 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤¬²õ¤ì¤Æ¤¤¤Þ¤¹";
1789 #else
1790                 if (err) what = "Broken savefile";
1791 #endif
1792
1793         }
1794
1795 #ifdef VERIFY_TIMESTAMP
1796         /* Verify timestamp */
1797         if (!err && !arg_wizard)
1798         {
1799                 /* Hack -- Verify the timestamp */
1800                 if (sf_when > (statbuf.st_ctime + 100) ||
1801                     sf_when < (statbuf.st_ctime - 100))
1802                 {
1803                         /* Message */
1804 #ifdef JP
1805                         what = "̵¸ú¤Ê¥¿¥¤¥à¡¦¥¹¥¿¥ó¥×¤Ç¤¹";
1806 #else
1807                         what = "Invalid timestamp";
1808 #endif
1809
1810
1811                         /* Oops */
1812                         err = -1;
1813                 }
1814         }
1815 #endif
1816
1817
1818         /* Okay */
1819         if (!err)
1820         {
1821                 /* Give a conversion warning */
1822                 if ((FAKE_VER_MAJOR != z_major) ||
1823                     (FAKE_VER_MINOR != z_minor) ||
1824                     (FAKE_VER_PATCH != z_patch))
1825                 {
1826                         if (z_major == 2 && z_minor == 0 && z_patch == 6)
1827                         {
1828 #ifdef JP
1829                                 msg_print("¥Ð¡¼¥¸¥ç¥ó 2.0.* ÍѤΥ»¡¼¥Ö¥Õ¥¡¥¤¥ë¤òÊÑ´¹¤·¤Þ¤·¤¿¡£");
1830 #else
1831                                 msg_print("Converted a 2.0.* savefile.");
1832 #endif
1833
1834                         }
1835                         else
1836                         {
1837                                 /* Message */
1838 #ifdef JP
1839                                 msg_format("¥Ð¡¼¥¸¥ç¥ó %d.%d.%d ÍѤΥ»¡¼¥Ö¡¦¥Õ¥¡¥¤¥ë¤òÊÑ´¹¤·¤Þ¤·¤¿¡£",
1840                                     (z_major > 9) ? z_major-10 : z_major , z_minor, z_patch);
1841 #else
1842                                 msg_format("Converted a %d.%d.%d savefile.",
1843                                     (z_major > 9) ? z_major-10 : z_major , z_minor, z_patch);
1844 #endif
1845                         }
1846                         msg_print(NULL);
1847                 }
1848
1849                 /* Player is dead */
1850                 if (p_ptr->is_dead)
1851                 {
1852                         /* Cheat death */
1853                         if (arg_wizard)
1854                         {
1855                                 /* A character was loaded */
1856                                 character_loaded = TRUE;
1857
1858                                 /* Done */
1859                                 return (TRUE);
1860                         }
1861
1862                         /* Player is no longer "dead" */
1863                         p_ptr->is_dead = FALSE;
1864
1865                         /* Count lives */
1866                         sf_lives++;
1867
1868                         /* Forget turns */
1869                         turn = old_turn = 0;
1870
1871                         /* Done */
1872                         return (TRUE);
1873                 }
1874
1875                 /* A character was loaded */
1876                 character_loaded = TRUE;
1877
1878                 {
1879                         u32b tmp = counts_read(2);
1880                         if (tmp > p_ptr->count)
1881                                 p_ptr->count = tmp;
1882                         if (counts_read(0) > playtime || counts_read(1) == playtime)
1883                                 counts_write(2, ++p_ptr->count);
1884                         counts_write(1, playtime);
1885                 }
1886
1887                 /* Success */
1888                 return (TRUE);
1889         }
1890
1891
1892 #ifdef VERIFY_SAVEFILE
1893
1894         /* Verify savefile usage */
1895         if (TRUE)
1896         {
1897                 char temp[1024];
1898
1899                 /* Extract name of lock file */
1900                 strcpy(temp, savefile);
1901                 strcat(temp, ".lok");
1902
1903                 /* Remove lock */
1904                 fd_kill(temp);
1905         }
1906
1907 #endif
1908
1909
1910         /* Message */
1911 #ifdef JP
1912         msg_format("¥¨¥é¡¼(%s)¤¬¥Ð¡¼¥¸¥ç¥ó%d.%d.%d ÍÑ¥»¡¼¥Ö¥Õ¥¡¥¤¥ëÆɤ߹þÃæ¤ËȯÀ¸¡£",
1913                    what, (z_major>9) ? z_major - 10 : z_major, z_minor, z_patch);
1914 #else
1915         msg_format("Error (%s) reading %d.%d.%d savefile.",
1916                    what, (z_major>9) ? z_major - 10 : z_major, z_minor, z_patch);
1917 #endif
1918         msg_print(NULL);
1919
1920         /* Oops */
1921         return (FALSE);
1922 }
1923
1924
1925 void remove_loc(void)
1926 {
1927 #ifdef VERIFY_SAVEFILE
1928         char temp[1024];
1929 #endif /* VERIFY_SAVEFILE */
1930
1931 #ifdef SET_UID
1932 # ifdef SECURE
1933
1934         /* Get "games" permissions */
1935         beGames();
1936
1937 # endif /* SECURE */
1938 #endif /* SET_UID */
1939
1940 #ifdef VERIFY_SAVEFILE
1941
1942         /* Lock on savefile */
1943         strcpy(temp, savefile);
1944         strcat(temp, ".lok");
1945
1946         /* Remove lock file */
1947         fd_kill(temp);
1948
1949 #endif /* VERIFY_SAVEFILE */
1950
1951 #ifdef SET_UID
1952 # ifdef SECURE
1953
1954         /* Drop "games" permissions */
1955         bePlayer();
1956
1957 # endif /* SECURE */
1958 #endif /* SET_UID */
1959
1960 }
1961
1962
1963 /*
1964  * Actually write a temporal saved floor file
1965  */
1966 static bool save_floor_aux(saved_floor_type *sf_ptr)
1967 {
1968         byte tmp8u;
1969
1970         /* Compact the objects */
1971         compact_objects(0);
1972         /* Compact the monsters */
1973         compact_monsters(0);
1974
1975
1976         /*** Actually write the file ***/
1977
1978         /* Initial value of xor_byte */
1979         tmp8u = (byte)randint0(256);
1980         xor_byte = 0;
1981         wr_byte(tmp8u);
1982
1983
1984         /* Reset the checksum */
1985         v_stamp = 0L;
1986         x_stamp = 0L;
1987
1988         /* Write the sign of this process */
1989         wr_u32b(saved_floor_file_sign);
1990
1991         /* Dump the dungeon floor */
1992         wr_saved_floor(sf_ptr);
1993
1994
1995         /* Write the "value check-sum" */
1996         wr_u32b(v_stamp);
1997
1998         /* Write the "encoded checksum" */
1999         wr_u32b(x_stamp);
2000
2001
2002         /* Error in save */
2003         if (ferror(fff) || (fflush(fff) == EOF)) return FALSE;
2004
2005         /* Successful save */
2006         return TRUE;
2007 }
2008
2009
2010 /*
2011  * Attempt to save the temporally saved-floor data
2012  */
2013 bool save_floor(saved_floor_type *sf_ptr, u32b mode)
2014 {
2015         FILE *old_fff = NULL;
2016         byte old_xor_byte = 0;
2017         u32b old_v_stamp = 0;
2018         u32b old_x_stamp = 0;
2019
2020         char floor_savefile[1024];
2021         int fd = -1;
2022         bool ok = FALSE;
2023
2024         if (!(mode & SLF_SECOND))
2025         {
2026 #ifdef SET_UID
2027 # ifdef SECURE
2028                 /* Get "games" permissions */
2029                 beGames();
2030 # endif
2031 #endif
2032         }
2033
2034         /* We have one file already opened */
2035         else
2036         {
2037                 /* Backup original values */
2038                 old_fff = fff;
2039                 old_xor_byte = xor_byte;
2040                 old_v_stamp = v_stamp;
2041                 old_x_stamp = x_stamp;
2042         }
2043
2044         /* New savefile */
2045         sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id);
2046
2047         /* Remove it */
2048         fd_kill(floor_savefile);
2049
2050         /* Attempt to save the player */
2051
2052         /* No file yet */
2053         fff = NULL;
2054
2055         /* File type is "SAVE" */
2056         FILE_TYPE(FILE_TYPE_SAVE);
2057
2058         /* Create the savefile */
2059         fd = fd_make(floor_savefile, 0644);
2060
2061         /* File is okay */
2062         if (fd >= 0)
2063         {
2064                 /* Close the "fd" */
2065                 (void)fd_close(fd);
2066
2067                 /* Open the savefile */
2068                 fff = my_fopen(floor_savefile, "wb");
2069
2070                 /* Successful open */
2071                 if (fff)
2072                 {
2073                         /* Write the savefile */
2074                         if (save_floor_aux(sf_ptr)) ok = TRUE;
2075
2076                         /* Attempt to close it */
2077                         if (my_fclose(fff)) ok = FALSE;
2078                 }
2079
2080                 /* Remove "broken" files */
2081                 if (!ok) (void)fd_kill(floor_savefile);
2082         }
2083
2084         if (!(mode & SLF_SECOND))
2085         {
2086 #ifdef SET_UID
2087 # ifdef SECURE
2088                 /* Drop "games" permissions */
2089                 bePlayer();
2090 # endif
2091 #endif
2092         }
2093
2094         /* We have one file already opened */
2095         else
2096         {
2097                 /* Restore original values */
2098                 fff = old_fff;
2099                 xor_byte = old_xor_byte;
2100                 v_stamp = old_v_stamp;
2101                 x_stamp = old_x_stamp;
2102         }
2103
2104         /* Return the result */
2105         return ok;
2106 }