From fba0b7dade5047830ecbf00a5460361fec864fd9 Mon Sep 17 00:00:00 2001
From: Hourier <66951241+Hourier@users.noreply.github.com>
Date: Sun, 17 Oct 2021 14:39:04 +0900
Subject: [PATCH] =?utf8?q?[Refactor]=20#1746=20monster-loader-savefile10.c?=
=?utf8?q?pp/h=20=E3=82=92=E3=82=AF=E3=83=A9=E3=82=B9=E5=8C=96=E3=81=97?=
=?utf8?q?=E3=80=81=E3=83=95=E3=82=A1=E3=82=AF=E3=83=88=E3=83=AA=E3=82=AF?=
=?utf8?q?=E3=83=A9=E3=82=B9=E3=81=8B=E3=82=89=E5=8F=96=E3=81=A3=E3=81=A6?=
=?utf8?q?=E3=81=93=E3=82=8C=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E3=81=97?=
=?utf8?q?=E3=81=9F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit
---
Hengband/Hengband/Hengband.vcxproj | 4 +
Hengband/Hengband/Hengband.vcxproj.filters | 15 ++++
src/Makefile.am | 4 +
src/load/dummy-loader.cpp | 6 +-
src/load/floor-loader.cpp | 18 ++--
src/load/monster/monster-loader-base.h | 12 +++
src/load/monster/monster-loader-factory.cpp | 52 +++++++++++
src/load/monster/monster-loader-factory.h | 16 ++++
src/load/monster/monster-loader-version-types.h | 6 ++
src/load/old/load-v1-5-0.cpp | 13 ++-
src/load/old/monster-loader-savefile10.cpp | 112 ++++++++++++------------
src/load/old/monster-loader-savefile10.h | 12 ++-
12 files changed, 197 insertions(+), 73 deletions(-)
create mode 100644 src/load/monster/monster-loader-base.h
create mode 100644 src/load/monster/monster-loader-factory.cpp
create mode 100644 src/load/monster/monster-loader-factory.h
create mode 100644 src/load/monster/monster-loader-version-types.h
diff --git a/Hengband/Hengband/Hengband.vcxproj b/Hengband/Hengband/Hengband.vcxproj
index b80ae7482..b3a3d6ae9 100644
--- a/Hengband/Hengband/Hengband.vcxproj
+++ b/Hengband/Hengband/Hengband.vcxproj
@@ -271,6 +271,7 @@
+
@@ -927,6 +928,9 @@
+
+
+
diff --git a/Hengband/Hengband/Hengband.vcxproj.filters b/Hengband/Hengband/Hengband.vcxproj.filters
index 6502d87a2..630f84924 100644
--- a/Hengband/Hengband/Hengband.vcxproj.filters
+++ b/Hengband/Hengband/Hengband.vcxproj.filters
@@ -2370,6 +2370,9 @@
load\item
+
+ load\monster
+
@@ -5139,6 +5142,15 @@
load\item
+
+ load\monster
+
+
+ load\monster
+
+
+ load\monster
+
@@ -5367,6 +5379,9 @@
{346bc9ba-a477-404f-bc72-2ad9c1ec6daf}
+
+ {ad11aaea-5034-4df2-9a9b-d63457fdad55}
+
diff --git a/src/Makefile.am b/src/Makefile.am
index be396ae9f..4507dae8f 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -361,6 +361,10 @@ hengband_SOURCES = \
load/item/item-loader-factory.cpp load/item/item-loader-factory.h \
load/item/item-loader-version-types.h \
\
+ load/monster/monster-loader-base.h \
+ load/monster/monster-loader-factory.cpp load/monster/monster-loader-factory.h \
+ load/monster/monster-loader-version-types.h \
+ \
load/old/item-flag-types-savefile10.h \
load/old/item-loader-savefile10.cpp load/old/item-loader-savefile10.h \
load/old/load-v1-5-0.cpp load/old/load-v1-5-0.h \
diff --git a/src/load/dummy-loader.cpp b/src/load/dummy-loader.cpp
index 8ca51f9ad..2be275ea4 100644
--- a/src/load/dummy-loader.cpp
+++ b/src/load/dummy-loader.cpp
@@ -1,6 +1,7 @@
#include "load/dummy-loader.h"
#include "load/angband-version-comparer.h"
#include "load/load-util.h"
+#include "load/monster/monster-loader-factory.h"
#include "load/old/monster-loader-savefile10.h"
#include "system/floor-type-definition.h"
#include "system/monster-type-definition.h"
@@ -39,9 +40,10 @@ void rd_dummy_monsters(player_type *player_ptr)
return;
auto tmp16s = rd_s16b();
+ monster_type dummy_mon;
+ auto monster_loader = MonsterLoaderFactory::create_loader(player_ptr);
for (int i = 0; i < tmp16s; i++) {
- monster_type dummy_mon;
- rd_monster(player_ptr, &dummy_mon);
+ monster_loader->rd_monster(&dummy_mon);
}
}
diff --git a/src/load/floor-loader.cpp b/src/load/floor-loader.cpp
index 0c938990b..01535d5bc 100644
--- a/src/load/floor-loader.cpp
+++ b/src/load/floor-loader.cpp
@@ -11,6 +11,7 @@
#include "load/item/item-loader-factory.h"
#include "load/load-util.h"
#include "load/old-feature-types.h"
+#include "load/monster/monster-loader-factory.h"
#include "load/old/item-loader-savefile10.h"
#include "load/old/load-v1-5-0.h"
#include "load/old/monster-loader-savefile10.h"
@@ -173,17 +174,16 @@ errr rd_saved_floor(player_type *player_ptr, saved_floor_type *sf_ptr)
if (limit > w_ptr->max_m_idx)
return 161;
- for (int i = 1; i < limit; i++) {
- grid_type *g_ptr;
- MONSTER_IDX m_idx;
- monster_type *m_ptr;
- m_idx = m_pop(floor_ptr);
- if (i != m_idx)
+ auto monster_loader = MonsterLoaderFactory::create_loader(player_ptr);
+ for (auto i = 1; i < limit; i++) {
+ auto m_idx = m_pop(floor_ptr);
+ if (i != m_idx) {
return 162;
+ }
- m_ptr = &floor_ptr->m_list[m_idx];
- rd_monster(player_ptr, m_ptr);
- g_ptr = &floor_ptr->grid_array[m_ptr->fy][m_ptr->fx];
+ auto *m_ptr = &floor_ptr->m_list[m_idx];
+ monster_loader->rd_monster(m_ptr);
+ auto *g_ptr = &floor_ptr->grid_array[m_ptr->fy][m_ptr->fx];
g_ptr->m_idx = m_idx;
real_r_ptr(m_ptr)->cur_num++;
}
diff --git a/src/load/monster/monster-loader-base.h b/src/load/monster/monster-loader-base.h
new file mode 100644
index 000000000..bfb113385
--- /dev/null
+++ b/src/load/monster/monster-loader-base.h
@@ -0,0 +1,12 @@
+#pragma once
+
+struct monster_type;
+struct player_type;
+class MonsterLoaderBase {
+public:
+ virtual ~MonsterLoaderBase() = default;
+ virtual void rd_monster(monster_type *m_ptr) = 0;
+
+protected:
+ MonsterLoaderBase() = default;
+};
diff --git a/src/load/monster/monster-loader-factory.cpp b/src/load/monster/monster-loader-factory.cpp
new file mode 100644
index 000000000..956871594
--- /dev/null
+++ b/src/load/monster/monster-loader-factory.cpp
@@ -0,0 +1,52 @@
+/*!
+ * @brief ã¢ã³ã¹ã¿ã¼æ
å ±ãã»ã¼ããã¼ã¿ããèªã¿è¾¼ãã¯ã©ã¹ãé¸æãããã¡ã¯ããªã¯ã©ã¹
+ * @date 2021/10/16
+ * @author Hourier
+ */
+
+#include "load/monster/monster-loader-base.h"
+#include "load/monster/monster-loader-factory.h"
+#include "load/monster/monster-loader-version-types.h"
+#include "load/load-util.h"
+#include "load/old/monster-loader-savefile10.h"
+
+/*!
+ * @brief ã¢ã¤ãã èªã¿è¾¼ã¿ã¯ã©ã¹ãè¿å´ãã.
+ * @return ã¢ã¤ãã èªã¿è¾¼ã¿ã¯ã©ã¹ã¸ã®åç
§ãã¤ã³ã¿.
+ * @details MonsterLoaderBaseã¯ç´ç²ä»®æ³é¢æ°ãå«ãã®ã§åç
§ãè¿ãå¿
è¦ããã.
+ * (å¤ãè¿ãè¨è¨ã¯ã³ã³ãã¤ã«ã¨ã©ã¼)
+ */
+std::shared_ptr MonsterLoaderFactory::create_loader(player_type *player_ptr)
+{
+ auto version = get_version();
+ switch (version) {
+ case MonsterLoaderVersionType::LOAD10:
+ return std::make_shared(player_ptr);
+ case MonsterLoaderVersionType::LOAD11:
+ // dummy yet.
+ default:
+ throw("Invalid loader version was specified!");
+ }
+}
+
+/*!
+ * @brief MonsterLoaderã®ãã¼ã¸ã§ã³åãæ¿ã.
+ * @return ã»ã¼ããã¡ã¤ã«ãã¼ã¸ã§ã³ç¾¤ã®ä¸ã§äºææ§ã®ããæå¤ã®ãã¼ã¸ã§ã³.
+ * @details (åå¿é²)ä¾ãã°ãã¼ã¸ã§ã³15ã§æ´ã«å¤æ´ãããå ´åã以ä¸ã®ããã«æ¸ãæãããã¨.
+ *
+ * if (loading_savefile_version_is_older_than(15)) {
+ * return MonsterLoaderVersionType::LOAD11;
+ * } else if (loading_savefile_version_is_older_than(11)) {
+ * return MonsterLoaderVersionType::LOAD10;
+ * } else {
+ * return MonsterLoaderVersionType::LOAD15;
+ * }
+ */
+MonsterLoaderVersionType MonsterLoaderFactory::get_version()
+{
+ if (loading_savefile_version_is_older_than(11)) {
+ return MonsterLoaderVersionType::LOAD10;
+ } else {
+ return MonsterLoaderVersionType::LOAD11;
+ }
+}
diff --git a/src/load/monster/monster-loader-factory.h b/src/load/monster/monster-loader-factory.h
new file mode 100644
index 000000000..3f93365da
--- /dev/null
+++ b/src/load/monster/monster-loader-factory.h
@@ -0,0 +1,16 @@
+#pragma once
+
+#include
+
+enum class MonsterLoaderVersionType;
+struct player_type;
+struct monster_type;
+class MonsterLoaderBase;
+class MonsterLoaderFactory {
+public:
+ static std::shared_ptr create_loader(player_type *player_ptr);
+
+private:
+ MonsterLoaderFactory() = delete;
+ static MonsterLoaderVersionType get_version();
+};
diff --git a/src/load/monster/monster-loader-version-types.h b/src/load/monster/monster-loader-version-types.h
new file mode 100644
index 000000000..c5097ce35
--- /dev/null
+++ b/src/load/monster/monster-loader-version-types.h
@@ -0,0 +1,6 @@
+#pragma once
+
+enum class MonsterLoaderVersionType {
+ LOAD10,
+ LOAD11,
+};
diff --git a/src/load/old/load-v1-5-0.cpp b/src/load/old/load-v1-5-0.cpp
index 9fe15520b..86fdeb3c3 100644
--- a/src/load/old/load-v1-5-0.cpp
+++ b/src/load/old/load-v1-5-0.cpp
@@ -16,6 +16,7 @@
#include "load/item/item-loader-factory.h"
#include "load/item/item-loader-version-types.h"
#include "load/load-util.h"
+#include "load/monster/monster-loader-factory.h"
#include "load/old-feature-types.h"
#include "load/old/item-loader-savefile10.h"
#include "load/old/monster-loader-savefile10.h"
@@ -701,19 +702,17 @@ errr rd_dungeon_old(player_type *player_ptr)
return (161);
}
+ auto monster_loader = MonsterLoaderFactory::create_loader(player_ptr);
for (int i = 1; i < limit; i++) {
- MONSTER_IDX m_idx;
- monster_type *m_ptr;
- m_idx = m_pop(floor_ptr);
+ auto m_idx = m_pop(floor_ptr);
if (i != m_idx) {
load_note(format(_("ã¢ã³ã¹ã¿ã¼é
ç½®ã¨ã©ã¼ (%d <> %d)", "Monster allocation error (%d <> %d)"), i, m_idx));
return (162);
}
- m_ptr = &floor_ptr->m_list[m_idx];
- rd_monster(player_ptr, m_ptr);
- grid_type *g_ptr;
- g_ptr = &floor_ptr->grid_array[m_ptr->fy][m_ptr->fx];
+ auto m_ptr = &floor_ptr->m_list[m_idx];
+ monster_loader->rd_monster(m_ptr);
+ auto *g_ptr = &floor_ptr->grid_array[m_ptr->fy][m_ptr->fx];
g_ptr->m_idx = m_idx;
real_r_ptr(m_ptr)->cur_num++;
}
diff --git a/src/load/old/monster-loader-savefile10.cpp b/src/load/old/monster-loader-savefile10.cpp
index 0578b16c4..6bc1216b9 100644
--- a/src/load/old/monster-loader-savefile10.cpp
+++ b/src/load/old/monster-loader-savefile10.cpp
@@ -9,138 +9,142 @@
#include "util/enum-converter.h"
#include "util/quarks.h"
+MonsterLoader10::MonsterLoader10(player_type *player_ptr)
+ : player_ptr(player_ptr)
+{
+}
+
/*!
- * @brief ã¢ã³ã¹ã¿ã¼ãèªã¿è¾¼ã(ç¾ç) / Read a monster (New method)
- * @param player_ptr ãã¬ã¤ã¤ã¼ã¸ã®åç
§ãã¤ã³ã¿
- * @param m_ptr ã¢ã³ã¹ã¿ã¼ä¿åå
ãã¤ã³ã¿
+ * @brief ã¢ã³ã¹ã¿ã¼ãèªã¿è¾¼ã(v3.0.0 Savefile ver10ã¾ã§)
*/
-void rd_monster(player_type *player_ptr, monster_type *m_ptr)
+void MonsterLoader10::rd_monster(monster_type *m_ptr_)
{
+ this->m_ptr = m_ptr_;
if (h_older_than(1, 5, 0, 0)) {
- rd_monster_old(player_ptr, m_ptr);
+ rd_monster_old(this->player_ptr, this->m_ptr);
return;
}
auto flags = rd_u32b();
- m_ptr->r_idx = rd_s16b();
- m_ptr->fy = rd_byte();
- m_ptr->fx = rd_byte();
+ this->m_ptr->r_idx = rd_s16b();
+ this->m_ptr->fy = rd_byte();
+ this->m_ptr->fx = rd_byte();
- m_ptr->hp = rd_s16b();
- m_ptr->maxhp = rd_s16b();
- m_ptr->max_maxhp = rd_s16b();
+ this->m_ptr->hp = rd_s16b();
+ this->m_ptr->maxhp = rd_s16b();
+ this->m_ptr->max_maxhp = rd_s16b();
if (h_older_than(2, 1, 2, 1)) {
- m_ptr->dealt_damage = 0;
+ this->m_ptr->dealt_damage = 0;
} else {
- m_ptr->dealt_damage = rd_s32b();
+ this->m_ptr->dealt_damage = rd_s32b();
}
if (any_bits(flags, SaveDataMonsterFlagType::AP_R_IDX))
- m_ptr->ap_r_idx = rd_s16b();
+ this->m_ptr->ap_r_idx = rd_s16b();
else
- m_ptr->ap_r_idx = m_ptr->r_idx;
+ this->m_ptr->ap_r_idx = this->m_ptr->r_idx;
if (any_bits(flags, SaveDataMonsterFlagType::SUB_ALIGN))
- m_ptr->sub_align = rd_byte();
+ this->m_ptr->sub_align = rd_byte();
else
- m_ptr->sub_align = 0;
+ this->m_ptr->sub_align = 0;
if (any_bits(flags, SaveDataMonsterFlagType::CSLEEP))
- m_ptr->mtimed[MTIMED_CSLEEP] = rd_s16b();
+ this->m_ptr->mtimed[MTIMED_CSLEEP] = rd_s16b();
else
- m_ptr->mtimed[MTIMED_CSLEEP] = 0;
+ this->m_ptr->mtimed[MTIMED_CSLEEP] = 0;
- m_ptr->mspeed = rd_byte();
+ this->m_ptr->mspeed = rd_byte();
- m_ptr->energy_need = rd_s16b();
+ this->m_ptr->energy_need = rd_s16b();
if (any_bits(flags, SaveDataMonsterFlagType::FAST)) {
- m_ptr->mtimed[MTIMED_FAST] = rd_byte();
+ this->m_ptr->mtimed[MTIMED_FAST] = rd_byte();
} else
- m_ptr->mtimed[MTIMED_FAST] = 0;
+ this->m_ptr->mtimed[MTIMED_FAST] = 0;
if (any_bits(flags, SaveDataMonsterFlagType::SLOW)) {
- m_ptr->mtimed[MTIMED_SLOW] = rd_byte();
+ this->m_ptr->mtimed[MTIMED_SLOW] = rd_byte();
} else
- m_ptr->mtimed[MTIMED_SLOW] = 0;
+ this->m_ptr->mtimed[MTIMED_SLOW] = 0;
if (any_bits(flags, SaveDataMonsterFlagType::STUNNED)) {
- m_ptr->mtimed[MTIMED_STUNNED] = rd_byte();
+ this->m_ptr->mtimed[MTIMED_STUNNED] = rd_byte();
} else
- m_ptr->mtimed[MTIMED_STUNNED] = 0;
+ this->m_ptr->mtimed[MTIMED_STUNNED] = 0;
if (any_bits(flags, SaveDataMonsterFlagType::CONFUSED)) {
- m_ptr->mtimed[MTIMED_CONFUSED] = rd_byte();
+ this->m_ptr->mtimed[MTIMED_CONFUSED] = rd_byte();
} else
- m_ptr->mtimed[MTIMED_CONFUSED] = 0;
+ this->m_ptr->mtimed[MTIMED_CONFUSED] = 0;
if (any_bits(flags, SaveDataMonsterFlagType::MONFEAR)) {
- m_ptr->mtimed[MTIMED_MONFEAR] = rd_byte();
+ this->m_ptr->mtimed[MTIMED_MONFEAR] = rd_byte();
} else
- m_ptr->mtimed[MTIMED_MONFEAR] = 0;
+ this->m_ptr->mtimed[MTIMED_MONFEAR] = 0;
if (any_bits(flags, SaveDataMonsterFlagType::TARGET_Y)) {
- m_ptr->target_y = rd_s16b();
+ this->m_ptr->target_y = rd_s16b();
} else
- m_ptr->target_y = 0;
+ this->m_ptr->target_y = 0;
if (any_bits(flags, SaveDataMonsterFlagType::TARGET_X)) {
- m_ptr->target_x = rd_s16b();
+ this->m_ptr->target_x = rd_s16b();
} else
- m_ptr->target_x = 0;
+ this->m_ptr->target_x = 0;
if (any_bits(flags, SaveDataMonsterFlagType::INVULNER)) {
- m_ptr->mtimed[MTIMED_INVULNER] = rd_byte();
+ this->m_ptr->mtimed[MTIMED_INVULNER] = rd_byte();
} else
- m_ptr->mtimed[MTIMED_INVULNER] = 0;
+ this->m_ptr->mtimed[MTIMED_INVULNER] = 0;
- m_ptr->mflag.clear();
- m_ptr->mflag2.clear();
+ this->m_ptr->mflag.clear();
+ this->m_ptr->mflag2.clear();
if (any_bits(flags, SaveDataMonsterFlagType::SMART)) {
if (loading_savefile_version_is_older_than(2)) {
auto tmp32u = rd_u32b();
- migrate_bitflag_to_flaggroup(m_ptr->smart, tmp32u);
+ migrate_bitflag_to_flaggroup(this->m_ptr->smart, tmp32u);
// 3.0.0Alpha10以åã®SM_CLONED(ãããä½ç½®22)ãSM_PET(23)ãSM_FRIEDLY(28)ãMFLAG2ã«ç§»è¡ãã
// ãããä½ç½®ã®å®ç¾©ã¯ãªããªãã®ã§ããããä½ç½®ã®å¤ããã¼ãã³ã¼ãããã
std::bitset<32> rd_bits(tmp32u);
- m_ptr->mflag2[MFLAG2::CLONED] = rd_bits[22];
- m_ptr->mflag2[MFLAG2::PET] = rd_bits[23];
- m_ptr->mflag2[MFLAG2::FRIENDLY] = rd_bits[28];
- m_ptr->smart.reset(i2enum(22)).reset(i2enum(23)).reset(i2enum(28));
+ this->m_ptr->mflag2[MFLAG2::CLONED] = rd_bits[22];
+ this->m_ptr->mflag2[MFLAG2::PET] = rd_bits[23];
+ this->m_ptr->mflag2[MFLAG2::FRIENDLY] = rd_bits[28];
+ this->m_ptr->smart.reset(i2enum(22)).reset(i2enum(23)).reset(i2enum(28));
} else {
- rd_FlagGroup(m_ptr->smart, rd_byte);
+ rd_FlagGroup(this->m_ptr->smart, rd_byte);
}
} else {
- m_ptr->smart.clear();
+ this->m_ptr->smart.clear();
}
if (any_bits(flags, SaveDataMonsterFlagType::EXP)) {
- m_ptr->exp = rd_u32b();
+ this->m_ptr->exp = rd_u32b();
} else
- m_ptr->exp = 0;
+ this->m_ptr->exp = 0;
if (any_bits(flags, SaveDataMonsterFlagType::MFLAG2)) {
if (loading_savefile_version_is_older_than(2)) {
auto tmp8u = rd_byte();
constexpr auto base = enum2i(MFLAG2::KAGE);
- migrate_bitflag_to_flaggroup(m_ptr->mflag2, tmp8u, base, 7);
+ migrate_bitflag_to_flaggroup(this->m_ptr->mflag2, tmp8u, base, 7);
} else {
- rd_FlagGroup(m_ptr->mflag2, rd_byte);
+ rd_FlagGroup(this->m_ptr->mflag2, rd_byte);
}
}
if (any_bits(flags, SaveDataMonsterFlagType::NICKNAME)) {
char buf[128];
rd_string(buf, sizeof(buf));
- m_ptr->nickname = quark_add(buf);
+ this->m_ptr->nickname = quark_add(buf);
} else
- m_ptr->nickname = 0;
+ this->m_ptr->nickname = 0;
if (any_bits(flags, SaveDataMonsterFlagType::PARENT))
- m_ptr->parent_m_idx = rd_s16b();
+ this->m_ptr->parent_m_idx = rd_s16b();
else
- m_ptr->parent_m_idx = 0;
+ this->m_ptr->parent_m_idx = 0;
}
diff --git a/src/load/old/monster-loader-savefile10.h b/src/load/old/monster-loader-savefile10.h
index e8bbfa383..1fae762e3 100644
--- a/src/load/old/monster-loader-savefile10.h
+++ b/src/load/old/monster-loader-savefile10.h
@@ -1,5 +1,15 @@
#pragma once
+#include "load/monster/monster-loader-base.h"
+
struct monster_type;
struct player_type;
-void rd_monster(player_type *player_ptr, monster_type *m_ptr);
+class MonsterLoader10 : public MonsterLoaderBase {
+public:
+ MonsterLoader10(player_type *player_ptr);
+ void rd_monster(monster_type *m_ptr) override;
+
+private:
+ player_type *player_ptr;
+ monster_type *m_ptr = nullptr;
+};
--
2.11.0