OSDN Git Service

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