Skip to content

Commit 20b63ba

Browse files
AndrettinAJenbo
authored andcommitted
Simplified ParseAiId(), ParseMonsterClass() and ParseMonsterResistance(), ParseUniqueMonsterPack() with magic_enum
1 parent 1ca8fcf commit 20b63ba

File tree

1 file changed

+22
-53
lines changed

1 file changed

+22
-53
lines changed

Source/monstdat.cpp

Lines changed: 22 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ struct magic_enum::customize::enum_range<devilution::_monster_id> {
2727
static constexpr int max = devilution::NUM_DEFAULT_MTYPES;
2828
};
2929

30+
template <>
31+
struct magic_enum::customize::enum_range<devilution::monster_resistance> {
32+
static constexpr int min = 0;
33+
static constexpr int max = 128;
34+
};
35+
3036
namespace devilution {
3137

3238
namespace {
@@ -243,46 +249,10 @@ tl::expected<MonsterAvailability, std::string> ParseMonsterAvailability(std::str
243249

244250
tl::expected<MonsterAIID, std::string> ParseAiId(std::string_view value)
245251
{
246-
if (value == "Zombie") return MonsterAIID::Zombie;
247-
if (value == "Fat") return MonsterAIID::Fat;
248-
if (value == "SkeletonMelee") return MonsterAIID::SkeletonMelee;
249-
if (value == "SkeletonRanged") return MonsterAIID::SkeletonRanged;
250-
if (value == "Scavenger") return MonsterAIID::Scavenger;
251-
if (value == "Rhino") return MonsterAIID::Rhino;
252-
if (value == "GoatMelee") return MonsterAIID::GoatMelee;
253-
if (value == "GoatRanged") return MonsterAIID::GoatRanged;
254-
if (value == "Fallen") return MonsterAIID::Fallen;
255-
if (value == "Magma") return MonsterAIID::Magma;
256-
if (value == "SkeletonKing") return MonsterAIID::SkeletonKing;
257-
if (value == "Bat") return MonsterAIID::Bat;
258-
if (value == "Gargoyle") return MonsterAIID::Gargoyle;
259-
if (value == "Butcher") return MonsterAIID::Butcher;
260-
if (value == "Succubus") return MonsterAIID::Succubus;
261-
if (value == "Sneak") return MonsterAIID::Sneak;
262-
if (value == "Storm") return MonsterAIID::Storm;
263-
if (value == "FireMan") return MonsterAIID::FireMan;
264-
if (value == "Gharbad") return MonsterAIID::Gharbad;
265-
if (value == "Acid") return MonsterAIID::Acid;
266-
if (value == "AcidUnique") return MonsterAIID::AcidUnique;
267-
if (value == "Golem") return MonsterAIID::Golem;
268-
if (value == "Zhar") return MonsterAIID::Zhar;
269-
if (value == "Snotspill") return MonsterAIID::Snotspill;
270-
if (value == "Snake") return MonsterAIID::Snake;
271-
if (value == "Counselor") return MonsterAIID::Counselor;
272-
if (value == "Mega") return MonsterAIID::Mega;
273-
if (value == "Diablo") return MonsterAIID::Diablo;
274-
if (value == "Lazarus") return MonsterAIID::Lazarus;
275-
if (value == "LazarusSuccubus") return MonsterAIID::LazarusSuccubus;
276-
if (value == "Lachdanan") return MonsterAIID::Lachdanan;
277-
if (value == "Warlord") return MonsterAIID::Warlord;
278-
if (value == "FireBat") return MonsterAIID::FireBat;
279-
if (value == "Torchant") return MonsterAIID::Torchant;
280-
if (value == "HorkDemon") return MonsterAIID::HorkDemon;
281-
if (value == "Lich") return MonsterAIID::Lich;
282-
if (value == "ArchLich") return MonsterAIID::ArchLich;
283-
if (value == "Psychorb") return MonsterAIID::Psychorb;
284-
if (value == "Necromorb") return MonsterAIID::Necromorb;
285-
if (value == "BoneDemon") return MonsterAIID::BoneDemon;
252+
const std::optional<MonsterAIID> enumValueOpt = magic_enum::enum_cast<MonsterAIID>(value);
253+
if (enumValueOpt.has_value()) {
254+
return enumValueOpt.value();
255+
}
286256
return tl::make_unexpected("Unknown enum value");
287257
}
288258

@@ -305,21 +275,19 @@ tl::expected<monster_flag, std::string> ParseMonsterFlag(std::string_view value)
305275

306276
tl::expected<MonsterClass, std::string> ParseMonsterClass(std::string_view value)
307277
{
308-
if (value == "Undead") return MonsterClass::Undead;
309-
if (value == "Demon") return MonsterClass::Demon;
310-
if (value == "Animal") return MonsterClass::Animal;
278+
const std::optional<MonsterClass> enumValueOpt = magic_enum::enum_cast<MonsterClass>(value);
279+
if (enumValueOpt.has_value()) {
280+
return enumValueOpt.value();
281+
}
311282
return tl::make_unexpected("Unknown enum value");
312283
}
313284

314285
tl::expected<monster_resistance, std::string> ParseMonsterResistance(std::string_view value)
315286
{
316-
if (value == "RESIST_MAGIC") return RESIST_MAGIC;
317-
if (value == "RESIST_FIRE") return RESIST_FIRE;
318-
if (value == "RESIST_LIGHTNING") return RESIST_LIGHTNING;
319-
if (value == "IMMUNE_MAGIC") return IMMUNE_MAGIC;
320-
if (value == "IMMUNE_FIRE") return IMMUNE_FIRE;
321-
if (value == "IMMUNE_LIGHTNING") return IMMUNE_LIGHTNING;
322-
if (value == "IMMUNE_ACID") return IMMUNE_ACID;
287+
const std::optional<monster_resistance> enumValueOpt = magic_enum::enum_cast<monster_resistance>(value);
288+
if (enumValueOpt.has_value()) {
289+
return enumValueOpt.value();
290+
}
323291
return tl::make_unexpected("Unknown enum value");
324292
}
325293

@@ -345,9 +313,10 @@ tl::expected<uint16_t, std::string> ParseMonsterTreasure(std::string_view value)
345313

346314
tl::expected<UniqueMonsterPack, std::string> ParseUniqueMonsterPack(std::string_view value)
347315
{
348-
if (value == "None") return UniqueMonsterPack::None;
349-
if (value == "Independent") return UniqueMonsterPack::Independent;
350-
if (value == "Leashed") return UniqueMonsterPack::Leashed;
316+
const std::optional<UniqueMonsterPack> enumValueOpt = magic_enum::enum_cast<UniqueMonsterPack>(value);
317+
if (enumValueOpt.has_value()) {
318+
return enumValueOpt.value();
319+
}
351320
return tl::make_unexpected("Unknown enum value");
352321
}
353322

0 commit comments

Comments
 (0)