OSDN Git Service

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