@@ -27,6 +27,12 @@ struct magic_enum::customize::enum_range<devilution::_monster_id> {
27
27
static constexpr int max = devilution::NUM_DEFAULT_MTYPES;
28
28
};
29
29
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
+
30
36
namespace devilution {
31
37
32
38
namespace {
@@ -243,46 +249,10 @@ tl::expected<MonsterAvailability, std::string> ParseMonsterAvailability(std::str
243
249
244
250
tl::expected<MonsterAIID, std::string> ParseAiId (std::string_view value)
245
251
{
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
+ }
286
256
return tl::make_unexpected (" Unknown enum value" );
287
257
}
288
258
@@ -305,21 +275,19 @@ tl::expected<monster_flag, std::string> ParseMonsterFlag(std::string_view value)
305
275
306
276
tl::expected<MonsterClass, std::string> ParseMonsterClass (std::string_view value)
307
277
{
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
+ }
311
282
return tl::make_unexpected (" Unknown enum value" );
312
283
}
313
284
314
285
tl::expected<monster_resistance, std::string> ParseMonsterResistance (std::string_view value)
315
286
{
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
+ }
323
291
return tl::make_unexpected (" Unknown enum value" );
324
292
}
325
293
@@ -345,9 +313,10 @@ tl::expected<uint16_t, std::string> ParseMonsterTreasure(std::string_view value)
345
313
346
314
tl::expected<UniqueMonsterPack, std::string> ParseUniqueMonsterPack (std::string_view value)
347
315
{
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
+ }
351
320
return tl::make_unexpected (" Unknown enum value" );
352
321
}
353
322
0 commit comments