enshwick 1.0.0-alpha.11

The Enshwick Loader's modding API for Enshrouded: the one crate mods are written against
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! The game's enums and masks that events carry, each value read by the
//! name of the engine's constant.

use crate::engine_ids as ids;

/// Declares a mask type over the bits of an engine mask, each bit a
/// constant named after the engine's constant for it.
macro_rules! mask {
    (
        $(#[$doc:meta])*
        $name:ident($bits:ty) {
            $($(#[$cdoc:meta])* $constant:ident = $bit:literal, $id:path;)*
        }
    ) => {
        $(#[$doc])*
        #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
        pub struct $name($bits);

        impl $name {
            $($(#[$cdoc])* pub const $constant: Self = Self(1 << $bit);)*

            /// Whether every bit of `other` is set.
            ///
            /// # Examples
            ///
            #[doc = concat!("See [`", stringify!($name), "`].")]
            #[must_use]
            pub const fn contains(self, other: Self) -> bool {
                self.0 & other.0 == other.0
            }

            /// Every bit of `self` and of `other`, as `|` gives, in a
            /// `const` too.
            ///
            /// # Examples
            ///
            #[doc = concat!("See [`", stringify!($name), "`].")]
            #[must_use]
            pub const fn union(self, other: Self) -> Self {
                Self(self.0 | other.0)
            }

            /// Each bit with the id of its engine constant.
            pub(super) const IDS: [(Self, &'static str); [$(stringify!($constant)),*].len()] =
                [$((Self::$constant, $id)),*];

            /// Each constant's name with the id of its engine constant.
            #[cfg(test)]
            const NAMES: [(&'static str, &'static str); [$(stringify!($constant)),*].len()] =
                [$((stringify!($constant), $id)),*];

            /// The set of the API's bits `bits`.
            pub(super) const fn from_bits(bits: $bits) -> Self {
                Self(bits)
            }

            /// The API's bits of the set.
            pub(super) const fn bits(self) -> $bits {
                self.0
            }
        }

        impl core::ops::BitOr for $name {
            type Output = Self;

            fn bitor(self, other: Self) -> Self {
                self.union(other)
            }
        }

        /// The names of the bits set, such as `{"MAGICAL_FIRE"}`.
        impl crate::script::Typed for $name {
            const TY: &'static crate::script::Ty = &crate::script::Ty::List(&crate::script::Ty::String);
        }

        impl crate::script::IntoScript for $name {
            fn into_value(self) -> crate::script::Value {
                let set = [$((stringify!($constant), Self::$constant)),*]
                    .into_iter()
                    .filter(|(_, bit)| self.contains(*bit))
                    .map(|(name, _)| crate::script::Value::String(name.to_owned()))
                    .collect();
                crate::script::Value::List(set)
            }
        }

        impl crate::script::FromScript for $name {
            fn from_value(value: crate::script::Value) -> Result<Self, crate::script::Error> {
                let names = <Vec<String> as crate::script::FromScript>::from_value(value)?;
                names.iter().try_fold(Self::default(), |set, name| {
                    [$((stringify!($constant), Self::$constant)),*]
                        .into_iter()
                        .find(|(known, _)| known == name)
                        .map(|(_, bit)| set.union(bit))
                        .ok_or_else(|| crate::script::Error::new(format!("{name:?} is no bit of {}", stringify!($name))))
                })
            }
        }
    };
}

/// Script conversions for an enum of the game's values with an `Other(u8)`
/// for values this crate does not name: each value is its variant's name,
/// and an unnamed one `Other(<number>)`.
macro_rules! named_values {
    ($($name:ident),*) => {$(
        impl crate::script::Typed for $name {
            const TY: &'static crate::script::Ty = &crate::script::Ty::String;
        }

        impl crate::script::IntoScript for $name {
            fn into_value(self) -> crate::script::Value {
                crate::script::Value::String(format!("{self:?}"))
            }
        }

        impl crate::script::FromScript for $name {
            fn from_value(value: crate::script::Value) -> Result<Self, crate::script::Error> {
                let text = <String as crate::script::FromScript>::from_value(value)?;
                if let Some(raw) = text.strip_prefix("Other(").and_then(|t| t.strip_suffix(')')) {
                    return raw.parse().map(Self::Other).map_err(|_| {
                        crate::script::Error::new(format!("{text:?} is no {}", stringify!($name)))
                    });
                }
                Self::IDS
                    .iter()
                    .map(|(value, _)| *value)
                    .find(|value| format!("{value:?}") == text)
                    .ok_or_else(|| crate::script::Error::new(format!("{text:?} is no {}", stringify!($name))))
            }
        }
    )*};
}

mask! {
    /// The damage types of a hit, a set the loader fills from the engine's
    /// damage type mask by the name of each bit.
    ///
    /// # Examples
    ///
    /// ```
    /// use enshwick::game_events::DamageTypeMask;
    ///
    /// let types = DamageTypeMask::MAGICAL_FIRE | DamageTypeMask::PHYSICAL_CUT;
    /// assert!(types.contains(DamageTypeMask::MAGICAL_FIRE));
    /// assert!(!types.contains(DamageTypeMask::MAGICAL_ICE));
    ///
    /// const COLD: DamageTypeMask = DamageTypeMask::MAGICAL_ICE.union(DamageTypeMask::PHYSICAL_CUT);
    /// assert!(COLD.contains(DamageTypeMask::MAGICAL_ICE));
    /// ```
    DamageTypeMask(u16) {
        /// Physical cut.
        PHYSICAL_CUT = 0, ids::DAMAGE_TYPE_PHYSICAL_CUT;
        /// Physical pierce.
        PHYSICAL_PIERCE = 1, ids::DAMAGE_TYPE_PHYSICAL_PIERCE;
        /// Physical blunt.
        PHYSICAL_BLUNT = 2, ids::DAMAGE_TYPE_PHYSICAL_BLUNT;
        /// Fire.
        MAGICAL_FIRE = 3, ids::DAMAGE_TYPE_MAGICAL_FIRE;
        /// Ice.
        MAGICAL_ICE = 4, ids::DAMAGE_TYPE_MAGICAL_ICE;
        /// The shroud's damage.
        MAGICAL_FOG = 5, ids::DAMAGE_TYPE_MAGICAL_FOG;
        /// Lightning.
        MAGICAL_LIGHTNING = 6, ids::DAMAGE_TYPE_MAGICAL_LIGHTNING;
        /// Healing.
        HEALING = 7, ids::DAMAGE_TYPE_HEALING;
        /// Poison.
        POISON = 8, ids::DAMAGE_TYPE_POISON;
        /// Wood, against trees and wooden things.
        WOOD = 9, ids::DAMAGE_TYPE_WOOD;
        /// Stone, against rock.
        STONE = 10, ids::DAMAGE_TYPE_STONE;
        /// Metal, against ore.
        METAL = 11, ids::DAMAGE_TYPE_METAL;
        /// Explosion.
        EXPLOSION = 12, ids::DAMAGE_TYPE_EXPLOSION;
    }
}

mask! {
    /// The attack categories of a hit, a set the loader fills from the
    /// engine's attack category mask by the name of each bit.
    ///
    /// # Examples
    ///
    /// ```
    /// use enshwick::game_events::AttackCategoryMask;
    ///
    /// let categories = AttackCategoryMask::MELEE | AttackCategoryMask::AOE;
    /// assert!(categories.contains(AttackCategoryMask::MELEE));
    /// assert!(!categories.contains(AttackCategoryMask::RANGED));
    /// ```
    AttackCategoryMask(u8) {
        /// Melee.
        MELEE = 0, ids::ATTACK_CATEGORY_MELEE;
        /// Ranged.
        RANGED = 1, ids::ATTACK_CATEGORY_RANGED;
        /// Magic.
        MAGIC = 2, ids::ATTACK_CATEGORY_MAGIC;
        /// A tool, such as a pickaxe.
        TOOL = 3, ids::ATTACK_CATEGORY_TOOL;
        /// Anything else.
        OTHER = 4, ids::ATTACK_CATEGORY_OTHER;
        /// Area of effect.
        AOE = 5, ids::ATTACK_CATEGORY_AO_E;
        /// A skill shot.
        SKILL_SHOT = 6, ids::ATTACK_CATEGORY_SKILL_SHOT;
    }
}

/// Where experience came from; the loader reads the engine's value of each
/// variant by its constant's name.
///
/// # Examples
///
/// See [`XpGainedEvent`](crate::game_events::XpGainedEvent).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ExperienceSourceType {
    /// No source given.
    None,
    /// Exploring.
    Exploration,
    /// Reading lore.
    Lore,
    /// A quest.
    Quest,
    /// A kill.
    Combat,
    /// Mining.
    Mining,
    /// A completed item set.
    ItemSetCompleted,
    /// A value this crate does not name, as the game gave it.
    Other(u8),
}

impl ExperienceSourceType {
    /// Each source with the id of its engine constant.
    pub(crate) const IDS: [(Self, &'static str); 7] = [
        (Self::None, ids::EXPERIENCE_SOURCE_NONE),
        (Self::Exploration, ids::EXPERIENCE_SOURCE_EXPLORATION),
        (Self::Lore, ids::EXPERIENCE_SOURCE_LORE),
        (Self::Quest, ids::EXPERIENCE_SOURCE_QUEST),
        (Self::Combat, ids::EXPERIENCE_SOURCE_COMBAT),
        (Self::Mining, ids::EXPERIENCE_SOURCE_MINING),
        (
            Self::ItemSetCompleted,
            ids::EXPERIENCE_SOURCE_ITEM_SET_COMPLETED,
        ),
    ];
}

/// What a player did at a Flame Altar; the loader reads the engine's value
/// of each variant by its constant's name.
///
/// # Examples
///
/// See [`FlameAltarEvent`](crate::game_events::FlameAltarEvent).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum FlameAltarActionType {
    /// Abandoned the base.
    Abandon,
    /// Reclaimed an abandoned base.
    Reclaim,
    /// Removed the water.
    RemoveWater,
    /// Made it the preferred respawn altar.
    SetAsPreferredSpawnAltar,
    /// A value this crate does not name, as the game gave it.
    Other(u8),
}

impl FlameAltarActionType {
    /// Each action with the id of its engine constant.
    pub(super) const IDS: [(Self, &'static str); 4] = [
        (Self::Abandon, ids::ALTAR_ACTION_ABANDON),
        (Self::Reclaim, ids::ALTAR_ACTION_RECLAIM),
        (Self::RemoveWater, ids::ALTAR_ACTION_REMOVE_WATER),
        (
            Self::SetAsPreferredSpawnAltar,
            ids::ALTAR_ACTION_SET_AS_PREFERRED_SPAWN_ALTAR,
        ),
    ];
}

/// A character's equipment slot; the loader reads the engine's value of
/// each variant by its constant's name.
///
/// # Examples
///
/// See [`HandleEquipmentSlotChangedEvent`](crate::game_events::HandleEquipmentSlotChangedEvent).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum EquipmentSlot {
    /// The vanity head slot.
    VanityHead,
    /// The vanity upper body slot.
    VanityUpperBody,
    /// The vanity arms slot.
    VanityArms,
    /// The vanity lower body slot.
    VanityLowerBody,
    /// The vanity feet slot.
    VanityFeet,
    /// The head armour.
    ArmourHead,
    /// The upper body armour.
    ArmourUpperBody,
    /// The arms armour.
    ArmourArms,
    /// The lower body armour.
    ArmourLowerBody,
    /// The feet armour.
    ArmourFeet,
    /// The first ring.
    Ring0,
    /// The second ring.
    Ring1,
    /// The ranged weapon.
    RangedWeapon,
    /// The shield.
    Shield,
    /// The arrows.
    Arrow,
    /// The spell.
    Spell,
    /// The fishing bait.
    FishingBait,
    /// The terrain material for building.
    TerrainMaterial,
    /// The building block material.
    BlueprintMaterialDefault,
    /// The roof material.
    BlueprintMaterialRoof,
    /// The overgrowth material.
    OvergrowthMaterial,
    /// The grappling hook.
    HookShot,
    /// The glider.
    Glider,
    /// The backpack.
    Backpack,
    /// The main hand.
    MainHand,
    /// A slot this crate does not name, as the game gave it.
    Other(u8),
}

impl EquipmentSlot {
    /// Each slot with the id of its engine constant.
    pub(super) const IDS: [(Self, &'static str); 25] = [
        (Self::VanityHead, ids::EQUIPMENT_SLOT_VANITY_HEAD),
        (Self::VanityUpperBody, ids::EQUIPMENT_SLOT_VANITY_UPPER_BODY),
        (Self::VanityArms, ids::EQUIPMENT_SLOT_VANITY_ARMS),
        (Self::VanityLowerBody, ids::EQUIPMENT_SLOT_VANITY_LOWER_BODY),
        (Self::VanityFeet, ids::EQUIPMENT_SLOT_VANITY_FEET),
        (Self::ArmourHead, ids::EQUIPMENT_SLOT_ARMOUR_HEAD),
        (Self::ArmourUpperBody, ids::EQUIPMENT_SLOT_ARMOUR_UPPER_BODY),
        (Self::ArmourArms, ids::EQUIPMENT_SLOT_ARMOUR_ARMS),
        (Self::ArmourLowerBody, ids::EQUIPMENT_SLOT_ARMOUR_LOWER_BODY),
        (Self::ArmourFeet, ids::EQUIPMENT_SLOT_ARMOUR_FEET),
        (Self::Ring0, ids::EQUIPMENT_SLOT_RING0),
        (Self::Ring1, ids::EQUIPMENT_SLOT_RING1),
        (Self::RangedWeapon, ids::EQUIPMENT_SLOT_RANGED_WEAPON),
        (Self::Shield, ids::EQUIPMENT_SLOT_SHIELD),
        (Self::Arrow, ids::EQUIPMENT_SLOT_ARROW),
        (Self::Spell, ids::EQUIPMENT_SLOT_SPELL),
        (Self::FishingBait, ids::EQUIPMENT_SLOT_FISHING_BAIT),
        (Self::TerrainMaterial, ids::EQUIPMENT_SLOT_TERRAIN_MATERIAL),
        (
            Self::BlueprintMaterialDefault,
            ids::EQUIPMENT_SLOT_BLUEPRINT_MATERIAL_DEFAULT,
        ),
        (
            Self::BlueprintMaterialRoof,
            ids::EQUIPMENT_SLOT_BLUEPRINT_MATERIAL_ROOF,
        ),
        (
            Self::OvergrowthMaterial,
            ids::EQUIPMENT_SLOT_OVERGROWTH_MATERIAL,
        ),
        (Self::HookShot, ids::EQUIPMENT_SLOT_HOOK_SHOT),
        (Self::Glider, ids::EQUIPMENT_SLOT_GLIDER),
        (Self::Backpack, ids::EQUIPMENT_SLOT_BACKPACK),
        (Self::MainHand, ids::EQUIPMENT_SLOT_MAIN_HAND),
    ];
}

named_values!(ExperienceSourceType, FlameAltarActionType, EquipmentSlot);

#[cfg(test)]
mod tests {
    use super::*;
    use crate::engine_enum::assert_named_after_engine;

    #[test]
    fn every_value_and_bit_takes_the_engine_s_name() {
        assert_named_after_engine(&EquipmentSlot::IDS);
        assert_named_after_engine(&ExperienceSourceType::IDS);
        assert_named_after_engine(&FlameAltarActionType::IDS);
        for names in [
            DamageTypeMask::NAMES.as_slice(),
            AttackCategoryMask::NAMES.as_slice(),
        ] {
            let named: Vec<_> = names
                .iter()
                .map(|&(name, id)| (Constant(name), id))
                .collect();
            assert_named_after_engine(&named);
        }
    }

    /// A constant's name, shown as it is.
    struct Constant(&'static str);

    impl core::fmt::Debug for Constant {
        fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
            f.write_str(self.0)
        }
    }
}