OSDN Git Service

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