veloren_common/terrain/sprite/
mod.rs

1//! Here's the deal.
2//!
3//! Blocks are always 4 bytes. The first byte is the [`BlockKind`]. For filled
4//! blocks, the remaining 3 sprites are the block colour. For unfilled sprites
5//! (air, water, etc.) the remaining 3 bytes correspond to sprite data. That's
6//! not a lot to work with! As a result, we're pulling every rabbit out of the
7//! bit-twiddling hat to squash as much information as possible into those 3
8//! bytes.
9//!
10//! Fundamentally, sprites are composed of one or more elements: the
11//! [`SpriteKind`], which tells us what the sprite *is*, and a list of
12//! attributes that define extra properties that the sprite has. Some examples
13//! of attributes might include:
14//!
15//! - the orientation of the sprite (with respect to the volume it sits within)
16//! - whether the sprite has snow cover on it
17//! - a 'variation seed' that allows frontends to pseudorandomly customise the
18//!   appearance of the sprite in a manner that's consistent across clients
19//! - Whether doors are open, closed, or permanently locked
20//! - The stage of growth of a plant
21//! - The kind of plant that sits in pots/planters/vessels
22//! - The colour of the sprite
23//! - The material of the sprite
24//!
25//! # Category
26//!
27//! The first of the three bytes is the sprite 'category'. As much as possible,
28//! we should try to have the properties of each sprite within a category be
29//! consistent with others in the category, to improve performance.
30//!
31//! Since a single byte is not enough to disambiguate the [`SpriteKind`] (we
32//! have more than 256 kinds, so there's not enough space), the category also
33//! corresponds to a 'kind mask': a bitmask that, when applied to the first two
34//! of the three bytes gives us the [`SpriteKind`].
35//!
36//! [`BlockKind`]: crate::terrain::block::BlockKind
37//! [`SpriteKind`]: crate::terrain::sprite::SpriteKind
38
39mod magic;
40//use inline_tweak::tweak_fn;
41pub use self::magic::{Attribute, AttributeError};
42use crate::{
43    attributes,
44    comp::{BuffData, BuffKind, item::ItemDefinitionIdOwned, tool::ToolKind},
45    effect::BuffEffect,
46    lottery::LootSpec,
47    make_case_elim,
48    resources::Secs,
49    sprites,
50    terrain::Block,
51};
52use common_i18n::Content;
53use hashbrown::HashMap;
54use lazy_static::lazy_static;
55use num_derive::FromPrimitive;
56use serde::{Deserialize, Serialize};
57use std::{
58    borrow::Cow,
59    convert::{Infallible, TryFrom},
60    fmt,
61};
62use strum::EnumIter;
63use vek::*;
64
65/// A sprite that can be deserialized with all its attributes.
66///
67/// Say we have created the sprites:
68/// ```ignore
69/// sprites! {
70///    Furniture = 0 has Ori, MirrorX {
71///       Chair,
72///       Table,
73///    }
74/// }
75/// ```
76/// And given we're deserializing from ron we could deserialize an array
77/// of `StructureSprite` that look like this:
78/// ```ignore
79/// [
80///    // This will be a `SpriteKind::Chair` with default attributes
81///    Chair(),
82///    // This will be a `SpriteKind::Chair` with the given attributes `Ori(2)` and `MirrorX(true)`.
83///    Chair(Ori(2), MirrorX(true)),
84///    // This will be a `SpriteKind::Table` with the given attribute `Ori(2)` and the rest of its
85///    // attributes set to default.
86///    Table(Ori(4)),
87/// ]
88/// ```
89#[derive(Copy, Clone, Debug, Eq, PartialEq, Deserialize)]
90#[serde(transparent)]
91pub struct StructureSprite(StructureSpriteKind);
92
93impl StructureSprite {
94    /// Assigns this structure sprite to a block.
95    ///
96    /// Returns error if [`Block::try_with_sprite`] fails.
97    pub fn apply_to_block(self, block: Block) -> Result<Block, Block> {
98        self.0.apply_to_block(block)
99    }
100}
101
102sprites! {
103    Void = 0 {
104        Empty = 0,
105    },
106    // Generic collection of sprites, no attributes but anything goes
107    Misc = 1 {
108        Ember      = 0x00,
109        SmokeDummy = 0x01,
110        Bomb       = 0x02,
111        FireBlock  = 0x03, // FireBlock for Burning Buff
112        HotSurface = 0x04,
113        Stones2    = 0x05, // Same as `Stones` but not collectible
114        TrainSmoke = 0x06,
115    },
116    // Furniture. In the future, we might add an attribute to customise material
117    // TODO: Remove sizes and variants, represent with attributes
118    Furniture = 2 has Ori, MirrorX {
119        // Indoor
120        BookshelfArabic    = 0x0D,
121        WallTableArabic    = 0x0E,
122        TableArabicLarge   = 0x0F,
123        TableArabicSmall   = 0x10,
124        CupboardArabic     = 0x11,
125        OvenArabic         = 0x12,
126        CushionArabic      = 0x13,
127        CanapeArabic       = 0x14,
128        Shelf              = 0x15,
129        Planter            = 0x16,
130        BedMesa            = 0x18,
131        WallTableMesa      = 0x19,
132        MirrorMesa         = 0x1A,
133        WardrobeSingleMesa = 0x1B,
134        WardrobeDoubleMesa = 0x1C,
135        CupboardMesa       = 0x1D,
136        TableCoastalLarge  = 0x1E,
137        BenchCoastal       = 0x1F,
138        // Crafting
139        CraftingBench    = 0x20,
140        Forge            = 0x21,
141        Cauldron         = 0x22,
142        Anvil            = 0x23,
143        CookingPot       = 0x24,
144        SpinningWheel    = 0x25,
145        TanningRack      = 0x26,
146        Loom             = 0x27,
147        DismantlingBench = 0x28,
148        RepairBench      = 0x29,
149        // Uncollectable containers
150        Barrel            = 0x30,
151        CrateBlock        = 0x31,
152        // Wall
153        HangingBasket     = 0x50,
154        HangingSign       = 0x51,
155        ChristmasOrnament = 0x52,
156        ChristmasWreath   = 0x53,
157        WallLampWizard    = 0x54,
158        WallLamp          = 0x55,
159        WallLampSmall     = 0x56,
160        WallSconce        = 0x57,
161        DungeonWallDecor  = 0x58,
162        WallLampMesa      = 0x59,
163        // Outdoor
164        Tent          = 0x60,
165        Bedroll       = 0x61,
166        BedrollSnow   = 0x62,
167        BedrollPirate = 0x63,
168        Sign          = 0x64,
169        Helm          = 0x65,
170        // Misc
171        Scarecrow      = 0x70,
172        FountainArabic = 0x71,
173        Hearth         = 0x72,
174        ChestWoodDouble= 0x73,
175        LanternpostWoodUpper = 0x74,
176        LanternpostWoodBase = 0x75,
177        LampMetalBase = 0x76,
178        BlacksmithBellows = 0x77,
179        CarpenterTable = 0x78,
180        CarpenterCrateWoodS = 0x79,
181        CarpenterCrateWoodL = 0x7A,
182        CarpenterToolsWall = 0x7B,
183        CarpenterLogCutter = 0x7C,
184        BarrelWoodCoal = 0x7D,
185        BarrelWoodWater = 0x7E,
186        BasketWovenL = 0x7F,
187        BasketWovenM = 0x80,
188        BasketWovenS = 0x81,
189        BonfireMLit = 0x82,
190        BonfireMUnlit = 0x83,
191        BucketWoodM = 0x84,
192        MirrorWoodM = 0x85,
193        SackLeatherM = 0x86,
194        TrophyframeWoodBear = 0x87,
195        TrophyframeWoodDeer = 0x88,
196        JugClayM = 0x89,
197        LogsWoodBranchS = 0x8A,
198        DiningtableWoodCorner = 0x8B,
199        DiningtableWoodBody = 0x8C,
200        BenchWoodEnd = 0x8D,
201        BenchWoodMiddle = 0x8E,
202        LogsWoodCoreEnd = 0x8F,
203        LogsWoodCoreMiddle = 0x90,
204        LogsWoodBarkEnd = 0x91,
205        LogsWoodBarkMiddle = 0x92,
206        LogsWoodBranchEnd = 0x93,
207        LogsWoodBranchMiddle = 0x94,
208        SeatWoodBlueMiddle = 0x95,
209        SeatWoodBlueSide = 0x96,
210        RopeCoilM = 0x97,
211        BedWoodWoodlandHead = 0x99,
212        BedWoodWoodlandMiddle = 0x9A,
213        BedWoodWoodlandTail = 0x9B,
214        BenchWoodWoodlandGreen1 = 0x9C,
215        BenchWoodWoodlandGreen2 = 0x9D,
216        BenchWoodWoodlandGreen3 = 0x9E,
217        BenchWoodWoodland = 0xA0,
218        ChairWoodWoodland = 0xA1,
219        ChairWoodWoodland2 = 0xA2,
220        CoatrackMetalWoodland = 0xA3,
221        CoatrackWoodWoodland = 0xA4,
222        DrawerWoodWoodlandL1 = 0xA5,
223        DrawerWoodWoodlandL2 = 0xA6,
224        DrawerWoodWoodlandM1 = 0xA7,
225        DrawerWoodWoodlandM2 = 0xA8,
226        DrawerWoodWoodlandS = 0xA9,
227        HandCartWoodHead = 0xAA,
228        HandCartWoodMiddle = 0xAB,
229        HandCartWoodTail = 0xAC,
230        FlowerpotWoodWoodlandS = 0xAD,
231        DiningtableWoodWoodlandRound = 0xAE,
232        DiningtableWoodWoodlandSquare = 0xAF,
233        TableWoodFancyWoodlandCorner = 0xB0,
234        TableWoodFancyWoodlandBody = 0xB1,
235        WardrobedoubleWoodWoodland = 0xB2,
236        WardrobedoubleWoodWoodland2 = 0xB3,
237        WardrobesingleWoodWoodland = 0xB4,
238        WardrobesingleWoodWoodland2 = 0xB5,
239        BedCliffHead = 0xB6,
240        BedCliffMiddle = 0xB7,
241        BedCliffTail = 0xB8,
242        BedCoastalHead = 0xB9,
243        BedCoastalMiddle = 0xBA,
244        BedCoastalTail = 0xBB,
245        BedDesertHead = 0xBC,
246        BedDesertMiddle = 0xBD,
247        BedDesertTail = 0xBE,
248        BedSavannahHead = 0xBF,
249        BedSavannahMiddle = 0xC0,
250        BedSavannahTail = 0xC1,
251        Ladder = 0xC2,
252        BookshelfEnd = 0xC3,
253        BookshelfMiddle = 0xC4,
254        HandrailWoodWoodlandBase = 0xC5,
255        HandrailWoodWoodlandMiddle = 0xC6,
256        HandrailWoodWoodlandTop = 0xC7,
257        BroomWoodWoodlandBlue = 0xC8,
258        ShovelWoodWoodlandGreen = 0xC9,
259        PitchforkWoodWoodlandGreen = 0xCA,
260        RakeWoodWoodland = 0xCB,
261        FenceWoodGateWoodland = 0xCC,
262        Hay = 0xCD,
263        CrystalBall = 0xCE,
264    },
265    // Sprites representing plants that may grow over time (this does not include plant parts, like fruit).
266    Plant = 3 has Growth, Owned, SnowCovered, Collectable {
267        // Cacti
268        BarrelCactus    = 0x00,
269        RoundCactus     = 0x01,
270        ShortCactus     = 0x02,
271        MedFlatCactus   = 0x03,
272        ShortFlatCactus = 0x04,
273        LargeCactus     = 0x05,
274        TallCactus      = 0x06,
275        // Flowers
276        BlueFlower    = 0x10,
277        PinkFlower    = 0x11,
278        PurpleFlower  = 0x12,
279        RedFlower     = 0x13,
280        WhiteFlower   = 0x14,
281        YellowFlower  = 0x15,
282        Sunflower     = 0x16,
283        Moonbell      = 0x17,
284        Pyrebloom     = 0x18,
285        LushFlower    = 0x19,
286        LanternFlower = 0x1A,
287        // Grasses, ferns, and other 'wild' plants/fungi
288        // TODO: remove sizes, make part of the `Growth` attribute
289        LongGrass          = 0x20,
290        MediumGrass        = 0x21,
291        ShortGrass         = 0x22,
292        Fern               = 0x23,
293        LargeGrass         = 0x24,
294        Reed               = 0x25,
295        TaigaGrass         = 0x26,
296        GrassBlue          = 0x27,
297        SavannaGrass       = 0x28,
298        TallSavannaGrass   = 0x29,
299        RedSavannaGrass    = 0x2A,
300        SavannaBush        = 0x2B,
301        Welwitch           = 0x2C,
302        LeafyPlant         = 0x2D,
303        DeadBush           = 0x2E,
304        JungleFern         = 0x2F,
305        GrassBlueShort     = 0x30,
306        GrassBlueMedium    = 0x31,
307        GrassBlueLong      = 0x32,
308        CavernLillypadBlue = 0x33,
309        EnsnaringVines     = 0x34,
310        LillyPads          = 0x35,
311        JungleLeafyPlant   = 0x36,
312        JungleRedGrass     = 0x37,
313        LanternPlant       = 0x38,
314        SporeReed          = 0x39,
315        DeadPlant          = 0x3A,
316        // Crops, berries, and fungi
317        Corn          = 0x41,
318        WheatYellow   = 0x42,
319        WheatGreen    = 0x43, // TODO: Remove `WheatGreen`, make part of the `Growth` attribute
320        LingonBerry   = 0x44,
321        Blueberry     = 0x45,
322        Lettuce       = 0x46,
323        Pumpkin       = 0x47,
324        Carrot        = 0x48,
325        Tomato        = 0x49,
326        Radish        = 0x4A,
327        Turnip        = 0x4B,
328        Flax          = 0x4C,
329        Mushroom      = 0x4D,
330        CaveMushroom  = 0x4E,
331        Cotton        = 0x4F,
332        WildFlax      = 0x50,
333        SewerMushroom = 0x51,
334        LushMushroom  = 0x52,
335        RockyMushroom = 0x53,
336        GlowMushroom  = 0x54,
337        // Seaweeds, corals, and other underwater plants
338        StonyCoral       = 0x61,
339        SoftCoral        = 0x62,
340        SeaweedTemperate = 0x63,
341        SeaweedTropical  = 0x64,
342        GiantKelp        = 0x65,
343        BullKelp         = 0x66,
344        WavyAlgae        = 0x67,
345        SeaGrapes        = 0x68,
346        MermaidsFan      = 0x69,
347        SeaAnemone       = 0x6A,
348        Seagrass         = 0x6B,
349        RedAlgae         = 0x6C,
350        // Danglying ceiling plants/fungi
351        Liana                   = 0x71,
352        MycelBlue               = 0x72,
353        CeilingMushroom         = 0x73,
354        Mold                    = 0x74,
355        Root                    = 0x75,
356        CeilingLanternPlant     = 0x76,
357        CeilingLanternFlower    = 0x77,
358        CeilingJungleLeafyPlant = 0x78,
359    },
360    // Solid resources
361    // TODO: Remove small variants, make deposit size be an attribute
362    Resource = 4 has Owned, SnowCovered {
363        // Gems and ores
364        // Woods and twigs
365        Twigs     = 0x00,
366        Wood      = 0x01,
367        Bamboo    = 0x02,
368        Hardwood  = 0x03,
369        Ironwood  = 0x04,
370        Frostwood = 0x05,
371        Eldwood   = 0x06,
372        // Other
373        Apple       = 0x20,
374        Coconut     = 0x21,
375        Stones      = 0x22,
376        Seashells   = 0x23,
377        Beehive     = 0x24,
378        Bowl        = 0x25,
379        PotionMinor = 0x26,
380        //= 0x27,
381        VialEmpty   = 0x28,
382    },
383    MineableResource = 5 has Damage {
384        Amethyst      = 0x00,
385        Ruby          = 0x01,
386        Sapphire      = 0x02,
387        Emerald       = 0x03,
388        Topaz         = 0x04,
389        Diamond       = 0x05,
390        Bloodstone    = 0x06,
391        Coal          = 0x07,
392        Cobalt        = 0x08,
393        Copper        = 0x09,
394        Iron          = 0x0A,
395        Tin           = 0x0B,
396        Silver        = 0x0C,
397        Gold          = 0x0D,
398        Velorite      = 0x0E,
399        VeloriteFrag  = 0x0F,
400        Mud           = 0x10,
401        Grave         = 0x11,
402    },
403    // Structural elements including doors and building parts
404    Structural = 6 has Ori {
405        // Doors and keyholes
406        Door         = 0x00,
407        DoorDark     = 0x01,
408        DoorWide     = 0x02,
409        BoneKeyhole  = 0x03,
410        BoneKeyDoor  = 0x04,
411        Keyhole      = 0x05,
412        KeyDoor      = 0x06,
413        GlassKeyhole = 0x07,
414        KeyholeBars  = 0x08,
415        HaniwaKeyDoor = 0x09,
416        HaniwaKeyhole = 0x0A,
417        TerracottaKeyDoor = 0x0B,
418        TerracottaKeyhole = 0x0C,
419        SahaginKeyhole = 0x0D,
420        SahaginKeyDoor = 0x0E,
421        VampireKeyDoor = 0x0F,
422        VampireKeyhole = 0x10,
423        MyrmidonKeyDoor = 0x11,
424        MyrmidonKeyhole = 0x12,
425        MinotaurKeyhole = 0x13,
426
427        // Windows
428        Window1      = 0x14,
429        Window2      = 0x15,
430        Window3      = 0x16,
431        Window4      = 0x17,
432        WitchWindow  = 0x18,
433        WindowArabic = 0x19,
434        // Walls
435        GlassBarrier    = 0x20,
436        SeaDecorBlock   = 0x21,
437        CliffDecorBlock = 0x22,
438        MagicalBarrier  = 0x23,
439        OneWayWall      = 0x24,
440        // Gates and grates
441        SeaDecorWindowHor = 0x30,
442        SeaDecorWindowVer = 0x31,
443        DropGate          = 0x32,
444        DropGateBottom    = 0x33,
445        WoodBarricades    = 0x34,
446        // Misc
447        Rope          = 0x40,
448        SeaDecorChain = 0x41,
449        IronSpike     = 0x42,
450        DoorBars      = 0x43,
451        HaniwaTrap    = 0x44,
452        HaniwaTrapTriggered = 0x45,
453        TerracottaStatue = 0x46,
454        TerracottaBlock = 0x47,
455        MetalChain = 0x48,
456        Bell = 0x49,
457    },
458    // Decorative items, both natural and artificial
459    Decor = 7 has Ori {
460        // Natural
461        Bones          = 0x00,
462        IceCrystal     = 0x01,
463        GlowIceCrystal = 0x02,
464        CrystalHigh    = 0x03,
465        CrystalLow     = 0x04,
466        UnderwaterVent = 0x05,
467        SeaUrchin      = 0x06,
468        IceSpike       = 0x07,
469        Orb            = 0x08,
470        EnsnaringWeb   = 0x09,
471        DiamondLight   = 0x0A,
472
473        // Artificial
474        Gravestone        = 0x10,
475        Melon             = 0x11,
476        ForgeTools        = 0x12,
477        JugAndBowlArabic  = 0x13,
478        JugArabic         = 0x14,
479        DecorSetArabic    = 0x15,
480        SepareArabic      = 0x16,
481        Candle            = 0x17,
482        SmithingTable     = 0x18,
483        Forge0            = 0x19,
484        GearWheel0        = 0x1A,
485        Quench0           = 0x1B,
486        SeaDecorEmblem    = 0x1C,
487        SeaDecorPillar    = 0x1D,
488        MagicalSeal       = 0x1E,
489        JugAndCupsCoastal = 0x1F,
490    },
491    Lamp = 8 has Ori, LightEnabled {
492        // Standalone lights
493        Lantern         = 0x00,
494        StreetLamp      = 0x01,
495        StreetLampTall  = 0x02,
496        SeashellLantern = 0x03,
497        FireBowlGround  = 0x04,
498        MesaLantern     = 0x05,
499        LanternpostWoodLantern = 0x06,
500        LampMetalShinglesRed = 0x07,
501        LampTerracotta = 0x08,
502        LampMetalShinglesCyan = 0x09,
503        LanternAirshipWallBlackS = 0x0A,
504        LanternAirshipWallBrownS = 0x0B,
505        LanternAirshipWallChestnutS = 0x0C,
506        LanternAirshipWallRedS = 0x0D,
507        LanternAirshipGroundBlackS = 0x0E,
508        LanternAirshipGroundBrownS = 0x0F,
509        LanternAirshipGroundChestnutS = 0x10,
510        LanternAirshipGroundRedS = 0x11,
511    },
512    // These are all expected to return `Some` for `collectible_info`
513    //
514    // NOTE: Collectable attr currently unused, plan is to add collected models for at least some
515    // of these.
516    Container = 9 has Ori, Owned, Collectable {
517        Chest             = 0x00,
518        DungeonChest0     = 0x01,
519        DungeonChest1     = 0x02,
520        DungeonChest2     = 0x03,
521        DungeonChest3     = 0x04,
522        DungeonChest4     = 0x05,
523        DungeonChest5     = 0x06,
524        CoralChest        = 0x07,
525        HaniwaUrn         = 0x08,
526        TerracottaChest   = 0x09,
527        SahaginChest      = 0x0A,
528        CommonLockedChest = 0x0B,
529        ChestBuried       = 0x0C,
530        Crate             = 0x0D,
531        // SLOT           = 0x0E,
532        // SLOT           = 0x0F,
533        WitchChest        = 0x10,
534        PirateChest       = 0x11,
535    },
536    Modular = 10 has Ori, AdjacentType {
537        FenceWoodWoodland = 0x00,
538    }
539}
540
541attributes! {
542    Ori { bits: 3, err: Infallible, from: |bits| Ok(Self(bits as u8)), into: |Ori(x)| x as u16 },
543    MirrorX { bits: 1, err: Infallible, from: |bits| Ok(Self(bits == 1)), into: |MirrorX(x)| x as u16 },
544    MirrorY { bits: 1, err: Infallible, from: |bits| Ok(Self(bits == 1)), into: |MirrorY(x)| x as u16 },
545    MirrorZ { bits: 1, err: Infallible, from: |bits| Ok(Self(bits == 1)), into: |MirrorZ(x)| x as u16 },
546    Growth { bits: 4, err: Infallible, from: |bits| Ok(Self(bits as u8)), into: |Growth(x)| x as u16 },
547    LightEnabled { bits: 1, err: Infallible, from: |bits| Ok(Self(bits == 1)), into: |LightEnabled(x)| x as u16 },
548    Collectable { bits: 1, err: Infallible, from: |bits| Ok(Self(bits == 1)), into: |Collectable(x)| x as u16 },
549    Damage { bits: 3, err: Infallible, from: |bits| Ok(Self(bits as u8)), into: |Damage(x)| x as u16 },
550    Owned { bits: 1, err: Infallible, from: |bits| Ok(Self(bits == 1)), into: |Owned(x)| x as u16 },
551    AdjacentType { bits: 3, err: Infallible, from: |bits| Ok(Self(bits as u8)), into: |AdjacentType(x)| x as u16 },
552    SnowCovered { bits: 1, err: Infallible, from: |bits| Ok(Self(bits == 1)), into: |SnowCovered(x)| x as u16 },
553}
554
555// The orientation of the sprite, 0..16
556#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, Deserialize)]
557pub struct Ori(pub u8);
558
559#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, Deserialize)]
560pub struct MirrorX(pub bool);
561
562#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, Deserialize)]
563pub struct MirrorY(pub bool);
564
565#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, Deserialize)]
566pub struct MirrorZ(pub bool);
567
568// The growth of the plant, 0..16
569#[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize)]
570pub struct Growth(pub u8);
571
572impl Default for Growth {
573    fn default() -> Self { Self(15) }
574}
575
576// Whether a light has been toggled on or off.
577#[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize)]
578pub struct LightEnabled(pub bool);
579
580impl Default for LightEnabled {
581    fn default() -> Self { Self(true) }
582}
583
584#[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize)]
585pub struct Collectable(pub bool);
586
587impl Default for Collectable {
588    fn default() -> Self { Self(true) }
589}
590
591#[derive(Default, Copy, Clone, Debug, PartialEq, Eq, Deserialize)]
592pub struct Owned(pub bool);
593
594/** Relative Neighbor Position:
595    an enum to determine the exact sprite for AdjacentType sprites
596    I - Straight - 0
597    L - Corner - 1
598    T - Junction - 2
599    X - Intersection - 3
600    End - single connection - 4
601**/
602
603#[derive(Default, Copy, Clone, Debug, PartialEq, Eq, Deserialize, FromPrimitive, Hash)]
604#[repr(u8)]
605pub enum RelativeNeighborPosition {
606    #[default]
607    I,
608    L,
609    T,
610    X,
611    End,
612}
613
614#[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize)]
615#[serde(from = "RelativeNeighborPosition")]
616pub struct AdjacentType(pub u8);
617
618impl From<RelativeNeighborPosition> for AdjacentType {
619    fn from(value: RelativeNeighborPosition) -> Self { Self(value as u8) }
620}
621
622impl Default for AdjacentType {
623    fn default() -> Self { Self::from(RelativeNeighborPosition::I) }
624}
625
626// Damage of an ore
627#[derive(Copy, Clone, Debug, PartialEq, Eq, Default, Deserialize)]
628pub struct Damage(pub u8);
629
630// Whether a sprite has snow on it
631#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, Deserialize)]
632pub struct SnowCovered(pub bool);
633
634impl SpriteKind {
635    #[inline]
636    //#[tweak_fn]
637    pub fn solid_height(&self) -> Option<f32> {
638        // Beware: the height *must* be <= `MAX_HEIGHT` or the collision system will not
639        // properly detect it!
640        Some(match self {
641            SpriteKind::Bedroll => 0.3,
642            SpriteKind::BedrollSnow => 0.4,
643            SpriteKind::BedrollPirate => 0.3,
644            SpriteKind::Tomato => 1.65,
645            SpriteKind::BarrelCactus => 0.909,
646            SpriteKind::LargeCactus => 3.0,
647            SpriteKind::TallCactus => 2.63,
648            SpriteKind::Scarecrow => 3.0,
649            SpriteKind::Turnip => 0.36,
650            SpriteKind::Pumpkin => 0.81,
651            SpriteKind::Chest => 1.09,
652            SpriteKind::CommonLockedChest
653            | SpriteKind::DungeonChest0
654            | SpriteKind::DungeonChest1
655            | SpriteKind::DungeonChest2
656            | SpriteKind::DungeonChest3
657            | SpriteKind::DungeonChest4
658            | SpriteKind::DungeonChest5
659            | SpriteKind::CoralChest
660            | SpriteKind::HaniwaUrn
661            | SpriteKind::SahaginChest
662            | SpriteKind::TerracottaChest
663            | SpriteKind::WitchChest
664            | SpriteKind::PirateChest => 1.09,
665            SpriteKind::TerracottaStatue => 5.29,
666            SpriteKind::TerracottaBlock => 1.00,
667            // Fence is more than 1.0 to prevent auto block-hopping onto the fence.
668            SpriteKind::FenceWoodWoodland => 1.09,
669            SpriteKind::SeaDecorChain => 1.09,
670            SpriteKind::SeaDecorBlock => 1.00,
671            SpriteKind::SeaDecorWindowHor => 0.55,
672            SpriteKind::SeaDecorWindowVer => 1.09,
673            SpriteKind::SeaDecorPillar => 2.55,
674            SpriteKind::SeashellLantern => 2.09,
675            SpriteKind::MesaLantern => 1.3,
676            SpriteKind::Rope => 1.09,
677            SpriteKind::MetalChain => 1.09,
678            SpriteKind::StreetLamp => 2.65,
679            SpriteKind::Carrot => 0.18,
680            SpriteKind::Radish => 0.18,
681            SpriteKind::FireBowlGround => 0.55,
682            SpriteKind::BedMesa => 0.82,
683            SpriteKind::DungeonWallDecor => 1.0,
684            SpriteKind::Planter => 1.09,
685            SpriteKind::WardrobeSingleMesa => 2.0,
686            SpriteKind::WardrobeDoubleMesa => 2.0,
687            SpriteKind::MirrorMesa => 2.0,
688            SpriteKind::Mud => 0.36,
689            SpriteKind::ChestBuried => 0.91,
690            SpriteKind::StonyCoral => 1.4,
691            SpriteKind::CraftingBench => 1.18,
692            SpriteKind::Forge => 1.818,
693            SpriteKind::Cauldron => 1.27,
694            SpriteKind::SpinningWheel => 1.454,
695            SpriteKind::TanningRack => 1.363,
696            SpriteKind::Loom => 1.545,
697            SpriteKind::Anvil => 1.18,
698            SpriteKind::CookingPot => 1.091,
699            SpriteKind::DismantlingBench => 1.091,
700            SpriteKind::IceSpike => 1.0,
701            SpriteKind::RepairBench => 1.2,
702            SpriteKind::RoundCactus => 0.72,
703            SpriteKind::ShortCactus => 1.36,
704            SpriteKind::MedFlatCactus => 1.36,
705            SpriteKind::ShortFlatCactus => 0.91,
706            SpriteKind::Bell => 1.0,
707            // TODO: Find suitable heights.
708            SpriteKind::Apple
709            | SpriteKind::Beehive
710            | SpriteKind::Velorite
711            | SpriteKind::VeloriteFrag
712            | SpriteKind::Coconut
713            | SpriteKind::StreetLampTall
714            | SpriteKind::Window1
715            | SpriteKind::Window2
716            | SpriteKind::Window3
717            | SpriteKind::Window4
718            | SpriteKind::DropGate
719            | SpriteKind::WitchWindow
720            | SpriteKind::SeaUrchin
721            | SpriteKind::IronSpike
722            | SpriteKind::GlassBarrier
723            | SpriteKind::GlassKeyhole
724            | SpriteKind::Keyhole
725            | SpriteKind::KeyDoor
726            | SpriteKind::BoneKeyhole
727            | SpriteKind::BoneKeyDoor
728            | SpriteKind::HaniwaKeyhole
729            | SpriteKind::HaniwaKeyDoor
730            | SpriteKind::SahaginKeyhole
731            | SpriteKind::SahaginKeyDoor
732            | SpriteKind::VampireKeyhole
733            | SpriteKind::VampireKeyDoor
734            | SpriteKind::HaniwaTrap
735            | SpriteKind::HaniwaTrapTriggered
736            | SpriteKind::TerracottaKeyDoor
737            | SpriteKind::TerracottaKeyhole
738            | SpriteKind::MyrmidonKeyDoor
739            | SpriteKind::MyrmidonKeyhole
740            | SpriteKind::MinotaurKeyhole
741            | SpriteKind::Bomb
742            | SpriteKind::OneWayWall
743            | SpriteKind::DoorBars
744            | SpriteKind::KeyholeBars
745            | SpriteKind::WoodBarricades
746            | SpriteKind::DiamondLight
747            | SpriteKind::CrystalBall => 1.0,
748            // TODO: Figure out if this should be solid or not.
749            SpriteKind::Shelf => 1.0,
750            SpriteKind::Lantern => 0.9,
751            SpriteKind::CrystalHigh | SpriteKind::CrystalLow => 1.5,
752            SpriteKind::Bloodstone
753            | SpriteKind::Coal
754            | SpriteKind::Cobalt
755            | SpriteKind::Copper
756            | SpriteKind::Iron
757            | SpriteKind::Tin
758            | SpriteKind::Silver
759            | SpriteKind::Gold => 0.6,
760            SpriteKind::EnsnaringVines
761            | SpriteKind::CavernLillypadBlue
762            | SpriteKind::EnsnaringWeb => 0.15,
763            SpriteKind::LillyPads => 0.1,
764            SpriteKind::WindowArabic | SpriteKind::BookshelfArabic => 1.9,
765            SpriteKind::DecorSetArabic => 2.6,
766            SpriteKind::SepareArabic => 2.2,
767            SpriteKind::CushionArabic => 0.4,
768            SpriteKind::JugArabic => 1.4,
769            SpriteKind::TableArabicSmall => 0.9,
770            SpriteKind::TableArabicLarge => 1.0,
771            SpriteKind::TableCoastalLarge => 1.0,
772            SpriteKind::BenchCoastal => 1.0,
773            SpriteKind::CanapeArabic => 1.2,
774            SpriteKind::CupboardArabic => 4.5,
775            SpriteKind::WallTableArabic => 2.3,
776            SpriteKind::JugAndBowlArabic => 1.4,
777            SpriteKind::JugAndCupsCoastal => 1.4,
778            SpriteKind::Melon => 0.7,
779            SpriteKind::OvenArabic => 3.2,
780            SpriteKind::FountainArabic => 2.4,
781            SpriteKind::Hearth => 2.3,
782            SpriteKind::ForgeTools => 2.8,
783            SpriteKind::CliffDecorBlock | SpriteKind::FireBlock => 1.0,
784            SpriteKind::Wood
785            | SpriteKind::Hardwood
786            | SpriteKind::Ironwood
787            | SpriteKind::Frostwood
788            | SpriteKind::Eldwood => 7.0 / 11.0,
789            SpriteKind::Bamboo => 9.0 / 11.0,
790            SpriteKind::MagicalBarrier => 3.0,
791            SpriteKind::MagicalSeal => 1.0,
792            SpriteKind::Helm => 1.909,
793            SpriteKind::Sign => 16.0 / 11.0,
794            SpriteKind::SmithingTable => 13.0 / 11.0,
795            SpriteKind::Forge0 => 17.0 / 11.0,
796            SpriteKind::GearWheel0 => 3.0 / 11.0,
797            SpriteKind::Quench0 => 8.0 / 11.0,
798            SpriteKind::HotSurface => 0.01,
799            SpriteKind::Barrel => 1.0,
800            SpriteKind::CrateBlock => 1.0,
801            SpriteKind::BarrelWoodWater | SpriteKind::BarrelWoodCoal => 1.545,
802            SpriteKind::LanternpostWoodLantern | SpriteKind::LanternpostWoodUpper => 2.000,
803            SpriteKind::LanternpostWoodBase => 3.000,
804            SpriteKind::LampMetalShinglesRed => 1.000,
805            SpriteKind::LampMetalShinglesCyan => 1.000,
806            SpriteKind::LampMetalBase => 2.818,
807            SpriteKind::LampTerracotta => 1.727,
808            SpriteKind::BlacksmithBellows => 0.545,
809            SpriteKind::CarpenterTable => 2.000,
810            SpriteKind::CarpenterCrateWoodS => 0.727,
811            SpriteKind::CarpenterCrateWoodL => 1.273,
812            SpriteKind::CarpenterLogCutter => 1.545,
813            SpriteKind::BasketWovenL | SpriteKind::JugClayM => 1.000,
814            SpriteKind::BasketWovenM => 0.909,
815            SpriteKind::BasketWovenS => 0.818,
816            SpriteKind::BonfireMLit | SpriteKind::BonfireMUnlit => 2.273,
817            SpriteKind::BucketWoodM | SpriteKind::SackLeatherM => 1.091,
818            SpriteKind::MirrorWoodM => 1.364,
819            SpriteKind::TrophyframeWoodBear => 1.455,
820            SpriteKind::TrophyframeWoodDeer => 1.727,
821            SpriteKind::ChestWoodDouble => 1.182,
822            SpriteKind::DiningtableWoodCorner => 1.273,
823            SpriteKind::DiningtableWoodBody => 1.273,
824            SpriteKind::BenchWoodEnd => 0.636,
825            SpriteKind::BenchWoodMiddle => 0.636,
826            SpriteKind::LogsWoodCoreEnd => 0.818,
827            SpriteKind::LogsWoodCoreMiddle => 0.818,
828            SpriteKind::LogsWoodBarkEnd => 1.091,
829            SpriteKind::LogsWoodBarkMiddle => 1.091,
830            SpriteKind::LogsWoodBranchEnd => 1.091,
831            SpriteKind::LogsWoodBranchMiddle => 1.091,
832            SpriteKind::LogsWoodBranchS => 1.091,
833            SpriteKind::SeatWoodBlueMiddle => 1.818,
834            SpriteKind::SeatWoodBlueSide => 1.818,
835            SpriteKind::LanternAirshipWallBlackS
836            | SpriteKind::LanternAirshipWallBrownS
837            | SpriteKind::LanternAirshipWallChestnutS
838            | SpriteKind::LanternAirshipWallRedS => 1.182,
839            SpriteKind::LanternAirshipGroundBlackS
840            | SpriteKind::LanternAirshipGroundBrownS
841            | SpriteKind::LanternAirshipGroundChestnutS
842            | SpriteKind::LanternAirshipGroundRedS => 0.909,
843            SpriteKind::RopeCoilM => 0.363,
844            SpriteKind::BedCliffHead => 0.636,
845            SpriteKind::BedCliffMiddle => 0.636,
846            SpriteKind::BedCliffTail => 0.636,
847            SpriteKind::BedCoastalHead => 0.636,
848            SpriteKind::BedCoastalMiddle => 0.636,
849            SpriteKind::BedCoastalTail => 0.636,
850            SpriteKind::BedDesertHead => 0.545,
851            SpriteKind::BedDesertMiddle => 0.545,
852            SpriteKind::BedDesertTail => 0.545,
853            SpriteKind::BedSavannahHead => 0.545,
854            SpriteKind::BedSavannahMiddle => 0.545,
855            SpriteKind::BedSavannahTail => 0.545,
856            SpriteKind::BedWoodWoodlandHead => 0.727,
857            SpriteKind::BedWoodWoodlandMiddle => 0.727,
858            SpriteKind::BedWoodWoodlandTail => 0.727,
859            SpriteKind::BookshelfEnd => 3.0,
860            SpriteKind::BookshelfMiddle => 3.0,
861            SpriteKind::BenchWoodWoodlandGreen1 => 1.545,
862            SpriteKind::BenchWoodWoodlandGreen2 => 1.545,
863            SpriteKind::BenchWoodWoodlandGreen3 => 1.545,
864            SpriteKind::BenchWoodWoodland => 1.545,
865            SpriteKind::ChairWoodWoodland => 1.636,
866            SpriteKind::ChairWoodWoodland2 => 1.727,
867            SpriteKind::CoatrackMetalWoodland => 2.364,
868            SpriteKind::CoatrackWoodWoodland => 2.364,
869            SpriteKind::Crate => 0.909,
870            SpriteKind::DrawerWoodWoodlandS => 1.000,
871            SpriteKind::DrawerWoodWoodlandM1 => 0.909,
872            SpriteKind::DrawerWoodWoodlandM2 => 0.909,
873            SpriteKind::DrawerWoodWoodlandL1 => 1.273,
874            SpriteKind::DrawerWoodWoodlandL2 => 1.273,
875            SpriteKind::DiningtableWoodWoodlandRound => 1.273,
876            SpriteKind::DiningtableWoodWoodlandSquare => 1.273,
877            SpriteKind::TableWoodFancyWoodlandCorner => 1.273,
878            SpriteKind::TableWoodFancyWoodlandBody => 1.273,
879            SpriteKind::WardrobesingleWoodWoodland => 2.364,
880            SpriteKind::WardrobesingleWoodWoodland2 => 2.364,
881            SpriteKind::WardrobedoubleWoodWoodland => 2.364,
882            SpriteKind::WardrobedoubleWoodWoodland2 => 2.364,
883            SpriteKind::FlowerpotWoodWoodlandS => 0.455,
884            SpriteKind::HandCartWoodHead => 1.091,
885            SpriteKind::HandCartWoodMiddle => 1.091,
886            SpriteKind::HandCartWoodTail => 1.091,
887            SpriteKind::HandrailWoodWoodlandBase | SpriteKind::HandrailWoodWoodlandMiddle => 1.727,
888            SpriteKind::HandrailWoodWoodlandTop => 1.181,
889            SpriteKind::Hay => 1.09,
890            _ => return None,
891        })
892    }
893
894    pub fn valid_collision_dir(
895        &self,
896        entity_aabb: Aabb<f32>,
897        block_aabb: Aabb<f32>,
898        move_dir: Vec3<f32>,
899        parent: &Block,
900    ) -> bool {
901        match self {
902            SpriteKind::OneWayWall => {
903                // Find the intrusion vector of the collision
904                let dir = entity_aabb.collision_vector_with_aabb(block_aabb);
905
906                // Determine an appropriate resolution vector (i.e: the minimum distance
907                // needed to push out of the block)
908                let max_axis = dir.map(|e| e.abs()).reduce_partial_min();
909                let resolve_dir = -dir.map(|e| {
910                    if e.abs().to_bits() == max_axis.to_bits() {
911                        e.signum()
912                    } else {
913                        0.0
914                    }
915                });
916
917                let is_moving_into = move_dir.dot(resolve_dir) <= 0.0;
918
919                is_moving_into
920                    && parent.get_attr().is_ok_and(|Ori(ori)| {
921                        Vec2::new(
922                            0.0,
923                            parent.get_attr::<MirrorY>().map_or(1.0, |m| match m.0 {
924                                true => -1.0,
925                                false => 1.0,
926                            }),
927                        )
928                        .rotated_z(std::f32::consts::PI * 0.25 * ori as f32)
929                        .with_z(0.0)
930                        .map2(resolve_dir, |e, r| (e - r).abs() < 0.1)
931                        .reduce_and()
932                    })
933            },
934            _ => true,
935        }
936    }
937
938    /// What loot table would collecting this sprite draw from, by default?
939    ///
940    /// NOTE: `Item::try_reclaim_from_block` is what you probably looking for
941    /// instead.
942    ///
943    /// `None` = block cannot be collected.
944    /// `Some(None)` = block can be collected, but does not give back an item.
945    /// `Some(Some(_))` = block can be collected and gives back an item.
946    #[inline]
947    pub fn default_loot_spec(&self) -> Option<Option<LootSpec<&'static str>>> {
948        let item = LootSpec::Item;
949        let table = LootSpec::LootTable;
950        Some(Some(match self {
951            SpriteKind::Apple => item("common.items.food.apple"),
952            SpriteKind::Mushroom => item("common.items.food.mushroom"),
953            SpriteKind::Velorite => item("common.items.mineral.ore.velorite"),
954            SpriteKind::VeloriteFrag => item("common.items.mineral.ore.veloritefrag"),
955            //SpriteKind::BlueFlower => item("common.items.flowers.blue"),
956            //SpriteKind::PinkFlower => item("common.items.flowers.pink"),
957            //SpriteKind::PurpleFlower => item("common.items.flowers.purple"),
958            SpriteKind::RedFlower => item("common.items.flowers.red"),
959            //SpriteKind::WhiteFlower => item("common.items.flowers.white"),
960            //SpriteKind::YellowFlower => item("common.items.flowers.yellow"),
961            SpriteKind::Sunflower => item("common.items.flowers.sunflower"),
962            //SpriteKind::LongGrass => item("common.items.grasses.long"),
963            //SpriteKind::MediumGrass => item("common.items.grasses.medium"),
964            //SpriteKind::ShortGrass => item("common.items.grasses.short"),
965            SpriteKind::Lettuce => item("common.items.food.lettuce"),
966            SpriteKind::Coconut => item("common.items.food.coconut"),
967            SpriteKind::Beehive => item("common.items.crafting_ing.honey"),
968            SpriteKind::Stones => item("common.items.crafting_ing.stones"),
969            SpriteKind::Twigs => item("common.items.crafting_ing.twigs"),
970            SpriteKind::VialEmpty => item("common.items.crafting_ing.empty_vial"),
971            SpriteKind::Bowl => item("common.items.crafting_ing.bowl"),
972            SpriteKind::PotionMinor => item("common.items.consumable.potion_minor"),
973            SpriteKind::Amethyst => item("common.items.mineral.gem.amethyst"),
974            SpriteKind::Ruby => item("common.items.mineral.gem.ruby"),
975            SpriteKind::Diamond => item("common.items.mineral.gem.diamond"),
976            SpriteKind::Sapphire => item("common.items.mineral.gem.sapphire"),
977            SpriteKind::Topaz => item("common.items.mineral.gem.topaz"),
978            SpriteKind::Emerald => item("common.items.mineral.gem.emerald"),
979            SpriteKind::Bloodstone => item("common.items.mineral.ore.bloodstone"),
980            SpriteKind::Coal => item("common.items.mineral.ore.coal"),
981            SpriteKind::Cobalt => item("common.items.mineral.ore.cobalt"),
982            SpriteKind::Copper => item("common.items.mineral.ore.copper"),
983            SpriteKind::Iron => item("common.items.mineral.ore.iron"),
984            SpriteKind::Tin => item("common.items.mineral.ore.tin"),
985            SpriteKind::Silver => item("common.items.mineral.ore.silver"),
986            SpriteKind::Gold => item("common.items.mineral.ore.gold"),
987            SpriteKind::Cotton => item("common.items.crafting_ing.cotton_boll"),
988            SpriteKind::Moonbell => item("common.items.flowers.moonbell"),
989            SpriteKind::Pyrebloom => item("common.items.flowers.pyrebloom"),
990            SpriteKind::WildFlax => item("common.items.flowers.wild_flax"),
991            SpriteKind::Seashells => item("common.items.crafting_ing.seashells"),
992            SpriteKind::RoundCactus => item("common.items.crafting_ing.cactus"),
993            SpriteKind::ShortFlatCactus => item("common.items.crafting_ing.cactus"),
994            SpriteKind::MedFlatCactus => item("common.items.crafting_ing.cactus"),
995            SpriteKind::Bomb => item("common.items.utility.bomb"),
996            SpriteKind::Chest => table("common.loot_tables.sprite.chest"),
997            SpriteKind::DungeonChest0 => table("common.loot_tables.dungeon.gnarling.chest"),
998            SpriteKind::DungeonChest1 => table("common.loot_tables.dungeon.adlet.chest"),
999            SpriteKind::DungeonChest2 => table("common.loot_tables.dungeon.sahagin.chest"),
1000            SpriteKind::DungeonChest3 => table("common.loot_tables.dungeon.haniwa.chest"),
1001            SpriteKind::DungeonChest4 => table("common.loot_tables.dungeon.myrmidon.chest"),
1002            SpriteKind::DungeonChest5 => table("common.loot_tables.dungeon.cultist.chest"),
1003            SpriteKind::CoralChest => table("common.loot_tables.dungeon.sea_chapel.chest_coral"),
1004            SpriteKind::HaniwaUrn => table("common.loot_tables.dungeon.haniwa.key"),
1005            SpriteKind::TerracottaChest => {
1006                table("common.loot_tables.dungeon.terracotta.chest_terracotta")
1007            },
1008            SpriteKind::SahaginChest => table("common.loot_tables.dungeon.sahagin.key_chest"),
1009            SpriteKind::CommonLockedChest => table("common.loot_tables.dungeon.sahagin.chest"),
1010            SpriteKind::ChestBuried => table("common.loot_tables.sprite.chest-buried"),
1011            SpriteKind::Crate => table("common.loot_tables.sprite.crate"),
1012            SpriteKind::Mud => table("common.loot_tables.sprite.mud"),
1013            SpriteKind::Grave => table("common.loot_tables.sprite.mud"),
1014            SpriteKind::Wood => item("common.items.log.wood"),
1015            SpriteKind::Bamboo => item("common.items.log.bamboo"),
1016            SpriteKind::Hardwood => item("common.items.log.hardwood"),
1017            SpriteKind::Ironwood => item("common.items.log.ironwood"),
1018            SpriteKind::Frostwood => item("common.items.log.frostwood"),
1019            SpriteKind::Eldwood => item("common.items.log.eldwood"),
1020            // TODO: why does this have a loot table?
1021            SpriteKind::MagicalBarrier => table("common.loot_tables.sprite.chest"),
1022            SpriteKind::WitchChest => table("common.loot_tables.spot.witch"),
1023            SpriteKind::PirateChest => table("common.loot_tables.spot.buccaneer"),
1024            SpriteKind::Keyhole
1025            | SpriteKind::BoneKeyhole
1026            | SpriteKind::HaniwaKeyhole
1027            | SpriteKind::VampireKeyhole
1028            | SpriteKind::GlassKeyhole
1029            | SpriteKind::KeyholeBars
1030            | SpriteKind::SahaginKeyhole
1031            | SpriteKind::TerracottaKeyhole
1032            | SpriteKind::MyrmidonKeyhole
1033            | SpriteKind::MinotaurKeyhole => {
1034                return Some(None);
1035            },
1036            _ => return None,
1037        }))
1038    }
1039
1040    /// Is this sprite *expected* to be picked up?
1041    ///
1042    /// Note, this will `Some(_)` even when the `Collectable` attr is `false`.
1043    ///
1044    /// * `None` means sprite can't be collected.
1045    /// * `Some(None)` means sprite can be collected without any mine tool.
1046    /// * `Some(Some(_))` means sprite can be collected but requires a tool.
1047    #[inline]
1048    pub fn collectible_info(&self) -> Option<Option<ToolKind>> {
1049        self.default_loot_spec().map(|_| self.mine_tool())
1050    }
1051
1052    /// Should the sprite behave like a container?
1053    ///
1054    /// That means:
1055    /// * The sprite is collectible (checked by test).
1056    /// * The sprite is not explodable.
1057    /// * `SpriteInteractKind::Chest` is used in the interaction character state
1058    ///   when collecting.
1059    /// * `should_drop_mystery` returns `true`.
1060    /// * Structure tests allows SpriteCfg.loot_table to be set for this sprite.
1061    ///
1062    /// If you just asking where you can collect this sprite without any tool,
1063    /// use [`Block::is_directly_collectible`].
1064    ///
1065    /// Implicit invariant of this method is that only sprites listed here
1066    /// are expected to use SpriteCfg.loot_table because that needs
1067    /// [`SpriteKind::should_drop_mystery`] to be `true` to avoid displaying the
1068    /// `default_loot_spec` items.
1069    #[inline]
1070    pub fn is_defined_as_container(&self) -> bool { self.category() == Category::Container }
1071
1072    /// Does this drop random items or potentially have a a custom loot_table in
1073    /// the SpriteCfg.
1074    ///
1075    /// This acts as a hint to avoid displaying the items from
1076    /// `Item::try_reclaim_from_block`.
1077    ///
1078    /// Some items may drop random items, yet aren't containers. So
1079    /// [`SpriteKind::is_defined_as_container()`] alone is insufficient for
1080    /// this.
1081    #[inline]
1082    pub fn should_drop_mystery(&self) -> bool {
1083        self.is_defined_as_container()
1084            || matches!(
1085                self.default_loot_spec(),
1086                Some(Some(LootSpec::LootTable { .. } | LootSpec::Lottery { .. }))
1087            )
1088    }
1089
1090    /// Get the position and direction to mount this sprite if any.
1091    #[inline]
1092    //#[tweak_fn]
1093    pub fn mount_offset(&self) -> Option<(Vec3<f32>, Vec3<f32>)> {
1094        match self {
1095            SpriteKind::ChairWoodWoodland
1096            | SpriteKind::ChairWoodWoodland2
1097            | SpriteKind::BenchWoodWoodlandGreen1
1098            | SpriteKind::BenchWoodWoodlandGreen2
1099            | SpriteKind::BenchWoodWoodlandGreen3
1100            | SpriteKind::BenchWoodWoodland
1101            | SpriteKind::BenchWoodEnd
1102            | SpriteKind::BenchWoodMiddle
1103            | SpriteKind::BenchCoastal => Some((Vec3::new(0.0, 0.0, 0.5), Vec3::unit_x())),
1104            SpriteKind::SeatWoodBlueMiddle | SpriteKind::SeatWoodBlueSide => {
1105                Some((Vec3::new(0.4, 0.0, 0.5), Vec3::unit_x()))
1106            },
1107            SpriteKind::Helm => Some((Vec3::new(0.0, -1.1, 0.0), Vec3::unit_y())),
1108            SpriteKind::BedWoodWoodlandHead
1109            | SpriteKind::BedCliffHead
1110            | SpriteKind::BedDesertHead
1111            | SpriteKind::BedCoastalHead
1112            | SpriteKind::BedSavannahHead => Some((Vec3::new(1.4, 0.0, 0.5), Vec3::unit_x())),
1113            SpriteKind::BedMesa => Some((Vec3::new(0.0, 0.0, 0.6), -Vec3::unit_y())),
1114            SpriteKind::BedrollSnow | SpriteKind::BedrollPirate => {
1115                Some((Vec3::new(0.0, 0.0, 0.1), -Vec3::unit_x()))
1116            },
1117            SpriteKind::Bedroll => Some((Vec3::new(0.0, 0.0, 0.1), Vec3::unit_y())),
1118            _ => None,
1119        }
1120    }
1121
1122    pub fn is_bed(&self) -> bool {
1123        matches!(
1124            self,
1125            SpriteKind::BedWoodWoodlandHead
1126                | SpriteKind::BedMesa
1127                | SpriteKind::BedCliffHead
1128                | SpriteKind::BedCoastalHead
1129                | SpriteKind::BedDesertHead
1130                | SpriteKind::BedSavannahHead
1131                | SpriteKind::Bedroll
1132                | SpriteKind::BedrollSnow
1133                | SpriteKind::BedrollPirate
1134        )
1135    }
1136
1137    #[inline]
1138    pub fn is_mountable(&self) -> bool { self.mount_offset().is_some() }
1139
1140    /// Get the buff provided by the block (currently used for mounting)
1141    #[inline]
1142    pub fn mount_buffs(&self) -> Option<Vec<BuffEffect>> {
1143        match self {
1144            sprite if sprite.is_bed() => Some(vec![BuffEffect {
1145                kind: BuffKind::RestingHeal,
1146                data: BuffData::new(0.02, Some(Secs(1.0))),
1147                cat_ids: Vec::new(),
1148            }]),
1149            _ => None,
1150        }
1151    }
1152
1153    #[inline]
1154    pub fn is_controller(&self) -> bool { matches!(self, SpriteKind::Helm) }
1155
1156    #[inline]
1157    pub fn is_door(&self) -> bool {
1158        matches!(
1159            self,
1160            SpriteKind::Door | SpriteKind::DoorWide | SpriteKind::DoorDark
1161        )
1162    }
1163
1164    /// Which tool (if any) is needed to collect this sprite?
1165    #[inline]
1166    pub fn mine_tool(&self) -> Option<ToolKind> {
1167        match self {
1168            SpriteKind::Velorite
1169            | SpriteKind::VeloriteFrag
1170            // Gems
1171            | SpriteKind::Amethyst
1172            | SpriteKind::Ruby
1173            | SpriteKind::Diamond
1174            | SpriteKind::Sapphire
1175            | SpriteKind::Emerald
1176            | SpriteKind::Topaz
1177            | SpriteKind::Bloodstone
1178            | SpriteKind::Coal
1179            | SpriteKind::Cobalt
1180            | SpriteKind::Copper
1181            | SpriteKind::Iron
1182            | SpriteKind::Tin
1183            | SpriteKind::Silver
1184            | SpriteKind::Gold => Some(ToolKind::Pick),
1185            SpriteKind::Grave | SpriteKind::Mud => Some(ToolKind::Shovel),
1186            _ => None,
1187        }
1188    }
1189
1190    pub fn required_mine_damage(&self) -> Option<u8> {
1191        Some(match self {
1192            SpriteKind::Gold => 6,
1193            SpriteKind::Silver => 6,
1194            SpriteKind::Bloodstone => 6,
1195            SpriteKind::Cobalt => 6,
1196            SpriteKind::Coal => 4,
1197            SpriteKind::Iron => 4,
1198            SpriteKind::Copper => 3,
1199            SpriteKind::Tin => 3,
1200            SpriteKind::Amethyst => 3,
1201            SpriteKind::Ruby => 3,
1202            SpriteKind::Sapphire => 3,
1203            SpriteKind::Emerald => 3,
1204            SpriteKind::Topaz => 3,
1205            SpriteKind::Diamond => 3,
1206            SpriteKind::Velorite => 3,
1207            SpriteKind::VeloriteFrag => 2,
1208            _ => return None,
1209        })
1210    }
1211
1212    /// Defines how much damage it takes for a mined resource to possibly
1213    /// make an extra drop.
1214    pub fn mine_drop_interval(&self) -> u8 {
1215        match self {
1216            SpriteKind::Gold => 3,
1217            SpriteKind::Silver => 3,
1218            SpriteKind::Bloodstone => 3,
1219            SpriteKind::Cobalt => 3,
1220            SpriteKind::Coal => 2,
1221            SpriteKind::Iron => 2,
1222            SpriteKind::Copper => 1,
1223            SpriteKind::Tin => 1,
1224            SpriteKind::Emerald => 3,
1225            SpriteKind::Sapphire => 3,
1226            SpriteKind::Amethyst => 3,
1227            SpriteKind::Topaz => 3,
1228            SpriteKind::Diamond => 3,
1229            SpriteKind::Ruby => 3,
1230            SpriteKind::Velorite => 3,
1231            SpriteKind::VeloriteFrag => 2,
1232            _ => 1,
1233        }
1234    }
1235
1236    /// Requires this item in the inventory to harvest, uses item_definition_id
1237    // TODO: Do we want to consolidate this with mine_tool at all? Main differences
1238    // are that mine tool requires item to be an equippable tool, be equipped, and
1239    // does not consume item while required_item requires that the item be in the
1240    // inventory and will consume the item on collecting the sprite.
1241    pub fn unlock_condition(self, cfg: Option<&SpriteCfg>) -> Option<Cow<'_, UnlockKind>> {
1242        let kind = if let Some(unlock) = cfg.and_then(|cfg| cfg.unlock.as_ref()) {
1243            Cow::Borrowed(unlock)
1244        } else {
1245            Cow::Owned(match self {
1246                SpriteKind::CommonLockedChest => {
1247                    UnlockKind::Consumes(ItemDefinitionIdOwned::Simple(String::from(
1248                        "common.items.utility.lockpick.lockpick_copper",
1249                    )))
1250                },
1251                SpriteKind::SahaginKeyhole => UnlockKind::Consumes(ItemDefinitionIdOwned::Simple(
1252                    String::from("common.items.keys.sahagin_key"),
1253                )),
1254                SpriteKind::BoneKeyhole => UnlockKind::Consumes(ItemDefinitionIdOwned::Simple(
1255                    String::from("common.items.keys.bone_key"),
1256                )),
1257                SpriteKind::HaniwaKeyhole => UnlockKind::Consumes(ItemDefinitionIdOwned::Simple(
1258                    String::from("common.items.keys.haniwa_key"),
1259                )),
1260                SpriteKind::VampireKeyhole => UnlockKind::Consumes(ItemDefinitionIdOwned::Simple(
1261                    String::from("common.items.keys.vampire_key"),
1262                )),
1263                SpriteKind::GlassKeyhole => UnlockKind::Consumes(ItemDefinitionIdOwned::Simple(
1264                    String::from("common.items.keys.glass_key"),
1265                )),
1266                SpriteKind::TerracottaChest => UnlockKind::Consumes(
1267                    ItemDefinitionIdOwned::Simple(String::from(
1268                        "common.items.keys.terracotta_key_chest",
1269                    ))
1270                    .to_owned(),
1271                ),
1272                SpriteKind::TerracottaKeyhole => UnlockKind::Consumes(
1273                    ItemDefinitionIdOwned::Simple(String::from(
1274                        "common.items.keys.terracotta_key_door",
1275                    ))
1276                    .to_owned(),
1277                ),
1278                SpriteKind::MyrmidonKeyhole => UnlockKind::Consumes(
1279                    ItemDefinitionIdOwned::Simple(String::from("common.items.keys.myrmidon_key"))
1280                        .to_owned(),
1281                ),
1282                SpriteKind::MinotaurKeyhole => UnlockKind::Consumes(
1283                    ItemDefinitionIdOwned::Simple(String::from("common.items.keys.minotaur_key"))
1284                        .to_owned(),
1285                ),
1286                SpriteKind::WitchChest => UnlockKind::Consumes(ItemDefinitionIdOwned::Simple(
1287                    String::from("common.items.utility.lockpick.lockpick_cobalt"),
1288                )),
1289                SpriteKind::PirateChest => UnlockKind::Consumes(ItemDefinitionIdOwned::Simple(
1290                    String::from("common.items.utility.lockpick.lockpick_iron"),
1291                )),
1292                _ => return None,
1293            })
1294        };
1295        Some(kind)
1296    }
1297
1298    /// Get the [`Content`] that this sprite is labelled with.
1299    pub fn content(&self, cfg: Option<SpriteCfg>) -> Option<Content> {
1300        cfg.and_then(|cfg| cfg.content)
1301    }
1302
1303    // TODO: phase out use of this method in favour of `sprite.has_attr::<Ori>()`
1304    #[inline]
1305    pub fn has_ori(&self) -> bool { self.category().has_attr::<Ori>() }
1306}
1307
1308impl fmt::Display for SpriteKind {
1309    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{:?}", self) }
1310}
1311
1312use strum::IntoEnumIterator;
1313
1314lazy_static! {
1315    pub static ref SPRITE_KINDS: HashMap<String, SpriteKind> =
1316        SpriteKind::iter().map(|sk| (sk.to_string(), sk)).collect();
1317}
1318
1319impl<'a> TryFrom<&'a str> for SpriteKind {
1320    type Error = ();
1321
1322    #[inline]
1323    fn try_from(s: &'a str) -> Result<Self, Self::Error> { SPRITE_KINDS.get(s).copied().ok_or(()) }
1324}
1325
1326// TODO: Free and Requires are currently unused.
1327#[derive(Clone, Debug, Serialize, Deserialize)]
1328pub enum UnlockKind {
1329    /// The sprite can be freely unlocked without any conditions
1330    Free,
1331    /// The sprite requires that the opening character has a given item in their
1332    /// inventory
1333    Requires(ItemDefinitionIdOwned),
1334    /// The sprite will consume the given item from the opening character's
1335    /// inventory
1336    Consumes(ItemDefinitionIdOwned),
1337}
1338
1339#[derive(Default, Clone, Debug, Serialize, Deserialize)]
1340pub struct SpriteCfg {
1341    /// Signifies that this sprite needs an item to be unlocked.
1342    pub unlock: Option<UnlockKind>,
1343    /// Signifies a text associated with this sprite.
1344    /// This also allows (but not requires) internationalization.
1345    ///
1346    /// Notice boards are an example of sprite that uses this.
1347    pub content: Option<Content>,
1348    /// Signifies a loot table associated with this sprite. The string referes
1349    /// to the loot table asset identifier.
1350    ///
1351    /// Chests are an example of sprite that can use this. For simple sprites
1352    /// like flowers using [`SpriteKind::default_loot_spec`] method is
1353    /// recommended instead.
1354    ///
1355    /// If you place a custom loot table on a sprite, make sure it's listed in
1356    /// [`SpriteKind::is_defined_as_container`], which should be enforced in
1357    /// tests, if possible.
1358    ///
1359    ///`collectible_info` must be `Some` for a sprite to be collectible.
1360    /// Adding a loot table to other sprites will not enable collecting
1361    /// them. `is_defined_as_container` is necessary to avoid displaying
1362    /// items from the default loot table (and all containers are included in
1363    /// `collectible_info`).
1364    ///
1365    /// NOTE: this is sent to the clients, we may potentionally strip this info
1366    /// on sending.
1367    pub loot_table: Option<String>,
1368}
1369
1370#[cfg(test)]
1371mod tests {
1372    use super::*;
1373
1374    #[test]
1375    fn sprite_containers_are_collectible() {
1376        for sprite in SpriteKind::all() {
1377            if sprite.is_defined_as_container() {
1378                assert!(sprite.collectible_info().is_some());
1379            }
1380        }
1381    }
1382
1383    #[test]
1384    fn sprite_conv_kind() {
1385        for sprite in SpriteKind::all() {
1386            let block = Block::air(*sprite);
1387            assert_eq!(block.sprite_category(), Some(sprite.category()));
1388            assert_eq!(block.get_sprite(), Some(*sprite));
1389        }
1390    }
1391
1392    #[test]
1393    fn sprite_attr() {
1394        for category in Category::all() {
1395            if category.has_attr::<Ori>() {
1396                for sprite in category.all_sprites() {
1397                    for i in 0..4 {
1398                        let block = Block::air(*sprite).with_attr(Ori(i)).unwrap();
1399                        assert_eq!(block.get_attr::<Ori>().unwrap(), Ori(i));
1400                        assert_eq!(block.get_sprite(), Some(*sprite));
1401                    }
1402                }
1403            }
1404            if category.has_attr::<Growth>() {
1405                for sprite in category.all_sprites() {
1406                    for i in 0..16 {
1407                        let block = Block::air(*sprite).with_attr(Growth(i)).unwrap();
1408                        assert_eq!(block.get_attr::<Growth>().unwrap(), Growth(i));
1409                        assert_eq!(block.get_sprite(), Some(*sprite));
1410                    }
1411                }
1412            }
1413        }
1414    }
1415}