OSDN Git Service

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