OSDN Git Service

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