OSDN Git Service

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