sf_api/gamestate/
unlockables.rs

1use std::num::NonZeroU8;
2
3use chrono::{DateTime, Local};
4use enum_map::Enum;
5use log::error;
6use num_derive::FromPrimitive;
7use strum::EnumIter;
8
9use super::*;
10use crate::{PlayerId, gamestate::items::*, misc::*};
11
12/// Information about the Hellevator event on the server. If it is active, you
13/// can get more detailed info via `active()`
14#[derive(Debug, Default, Clone)]
15#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
16pub struct HellevatorEvent {
17    /// The time the hellevator event was enabled at
18    pub start: Option<DateTime<Local>>,
19    /// The time the hellevator event will be disabled at
20    pub end: Option<DateTime<Local>>,
21    /// The time at which you will no longer be able to collect things for the
22    /// hellevator
23    pub collect_time_end: Option<DateTime<Local>>,
24    /// Contains the hellevator. This can be some(x), even if the event is not
25    /// going, so you should use the `active()` functions to get this
26    pub(crate) active: Option<Hellevator>,
27}
28
29#[derive(Debug)]
30pub enum HellevatorStatus<'a> {
31    /// The event is ongoing, but you have to send a `HellevatorEnter` command
32    /// to start using it
33    NotEntered,
34    /// The event is currently not available
35    NotAvailable,
36    /// The event has ended, but you can still claim the final reward
37    RewardClaimable,
38    /// A reference to the
39    Active(&'a Hellevator),
40}
41
42impl HellevatorEvent {
43    /// Checks if the event has started and not yet ended compared to the
44    /// current time
45    #[must_use]
46    pub fn is_event_ongoing(&self) -> bool {
47        let now = Local::now();
48        matches!((self.start, self.end), (Some(start), Some(end)) if end > now && start < now)
49    }
50
51    /// If the Hellevator event is active, this returns a reference to the
52    /// Information about it. Note that you still need to check the level >= 10
53    /// requirement yourself
54    #[must_use]
55    pub fn status(&self) -> HellevatorStatus<'_> {
56        match self.active.as_ref() {
57            None => HellevatorStatus::NotAvailable,
58            Some(h) if !self.is_event_ongoing() => {
59                if let Some(cend) = self.collect_time_end
60                    && !h.has_final_reward
61                    && Local::now() < cend
62                {
63                    return HellevatorStatus::RewardClaimable;
64                }
65                HellevatorStatus::NotAvailable
66            }
67            Some(h) if h.current_floor == 0 => HellevatorStatus::NotEntered,
68            Some(h) => HellevatorStatus::Active(h),
69        }
70    }
71
72    // /// If the Hellevator event is active, this returns a mutable reference
73    // to /// the Information about it
74    // #[must_use]
75    // pub fn active_mut(&mut self) -> Option<&mut Hellevator> {
76    //     let is_active = self.is_event_ongoing();
77    //     self.active.as_mut().filter(|_| is_active)
78    // }
79}
80
81#[derive(Debug, Default, Clone)]
82#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
83pub struct Hellevator {
84    pub key_cards: u32,
85    pub current_floor: u32,
86    pub points: u32,
87    pub has_final_reward: bool,
88
89    pub guild_points_today: u32,
90    pub guild_rank: u32,
91    pub guild_raid_floors: Vec<HellevatorRaidFloor>,
92
93    pub guild_raid_signup_start: DateTime<Local>,
94    pub guild_raid_start: DateTime<Local>,
95    pub monster_rewards: Vec<HellevatorMonsterReward>,
96
97    pub own_best_floor: u32,
98    pub shop_items: [HellevatorShopTreat; 3],
99
100    pub current_treat: Option<HellevatorShopTreat>,
101
102    pub next_card_generated: Option<DateTime<Local>>,
103    pub next_reset: Option<DateTime<Local>>,
104    pub start_contrib_date: Option<DateTime<Local>>,
105
106    pub rewards_yesterday: Option<HellevatorDailyReward>,
107    pub rewards_today: Option<HellevatorDailyReward>,
108    pub rewards_next: Option<HellevatorDailyReward>,
109
110    pub daily_treat_bonus: Option<HellevatorTreatBonus>,
111
112    pub current_monster: Option<HellevatorMonster>,
113
114    pub earned_today: u32,
115    pub earned_yesterday: u32,
116
117    pub(crate) brackets: Vec<u32>,
118}
119
120#[derive(Debug, Default, Clone)]
121#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
122pub struct HellevatorTreatBonus {
123    pub typ: HellevatorTreatBonusType,
124    pub amount: u32,
125}
126
127#[derive(Debug, Default, Clone)]
128#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
129pub struct HellevatorMonster {
130    pub id: i64,
131    pub level: u32,
132    pub typ: HellevatorMonsterElement,
133}
134
135#[derive(Debug, Clone, Default, Copy, PartialEq, Eq, Hash, FromPrimitive)]
136#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
137pub enum HellevatorMonsterElement {
138    Fire = 1,
139    Cold = 2,
140    Lightning = 3,
141    #[default]
142    Unknown = 240,
143}
144
145impl HellevatorMonster {
146    pub(crate) fn parse(data: &[i64]) -> Result<Self, SFError> {
147        Ok(HellevatorMonster {
148            id: data.cget(0, "h monster id")?,
149            level: data.csiget(1, "h monster level", 0)?,
150            typ: data.cfpget(2, "h monster typ", |a| a)?.unwrap_or_default(),
151        })
152    }
153}
154
155impl HellevatorTreatBonus {
156    pub(crate) fn parse(data: &[i64]) -> Result<Self, SFError> {
157        Ok(HellevatorTreatBonus {
158            typ: data
159                .cfpget(0, "hellevator treat bonus", |a| a)?
160                .unwrap_or_default(),
161            amount: data.csiget(1, "hellevator treat bonus a", 0)?,
162        })
163    }
164}
165
166#[derive(Debug, Clone, Default, Copy, PartialEq, Eq, Hash, FromPrimitive)]
167#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
168pub enum HellevatorTreatBonusType {
169    ExtraDamage = 14,
170    #[default]
171    Unknown = 240,
172}
173
174#[derive(Debug, Default, Clone)]
175#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
176pub struct HellevatorMonsterReward {
177    pub typ: HellevatorMonsterRewardTyp,
178    pub amount: u64,
179}
180
181#[derive(Debug, Clone, Default, Copy, PartialEq, Eq, Hash)]
182#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
183pub enum HellevatorMonsterRewardTyp {
184    Points,
185    Tickets,
186    Mushrooms,
187    Silver,
188    LuckyCoin,
189    Wood,
190    Stone,
191    Arcane,
192    Metal,
193    Souls,
194    Fruit(HabitatType),
195
196    #[default]
197    Unknown,
198}
199
200impl HellevatorMonsterRewardTyp {
201    pub(crate) fn parse(data: i64) -> HellevatorMonsterRewardTyp {
202        match data {
203            1 => HellevatorMonsterRewardTyp::Points,
204            2 => HellevatorMonsterRewardTyp::Tickets,
205            3 => HellevatorMonsterRewardTyp::Mushrooms,
206            4 => HellevatorMonsterRewardTyp::Silver,
207            5 => HellevatorMonsterRewardTyp::LuckyCoin,
208            6 => HellevatorMonsterRewardTyp::Wood,
209            7 => HellevatorMonsterRewardTyp::Stone,
210            8 => HellevatorMonsterRewardTyp::Arcane,
211            9 => HellevatorMonsterRewardTyp::Metal,
212            10 => HellevatorMonsterRewardTyp::Souls,
213            11 => HellevatorMonsterRewardTyp::Fruit(HabitatType::Shadow),
214            12 => HellevatorMonsterRewardTyp::Fruit(HabitatType::Light),
215            13 => HellevatorMonsterRewardTyp::Fruit(HabitatType::Earth),
216            14 => HellevatorMonsterRewardTyp::Fruit(HabitatType::Fire),
217            15 => HellevatorMonsterRewardTyp::Fruit(HabitatType::Water),
218
219            _ => HellevatorMonsterRewardTyp::Unknown,
220        }
221    }
222}
223
224#[derive(Debug, Default, Clone)]
225#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
226pub struct HellevatorRaidFloor {
227    pub(crate) today: i64,
228    pub(crate) yesterday: i64,
229
230    pub point_reward: u32,
231    pub silver_reward: u64,
232
233    pub today_assigned: Vec<String>,
234    pub yesterday_assigned: Vec<String>,
235}
236
237#[derive(Debug, Clone, Default, Copy, PartialEq, Eq, Hash, FromPrimitive)]
238#[non_exhaustive]
239#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
240pub enum HellevatorTreatType {
241    ChocolateChilliPepper = 1,
242    PeppermintChocolate = 2,
243    Electroshock = 3,
244    ChillIceCream = 4,
245    CracklingChewingGum = 5,
246    PeppermintChewingGum = 6,
247    BeerBiscuit = 7,
248    GingerBreadHeart = 8,
249    FortuneCookie = 9,
250    CannedSpinach = 10,
251    StoneBiscuit = 11,
252    OrganicGranolaBar = 12,
253    ChocolateGoldCoin = 13,
254    #[default]
255    Unknown = 230,
256}
257
258#[derive(Debug, Default, Clone)]
259#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
260pub struct HellevatorShopTreat {
261    pub is_special: bool,
262    pub typ: HellevatorTreatType,
263    pub price: u32,
264    pub duration: u32,
265    pub effect_strength: u32,
266}
267
268#[derive(Debug, Clone, Default, PartialEq, Eq)]
269#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
270pub struct HellevatorDailyReward {
271    // TODO: What is the purpose of these fields?
272    pub(crate) start_level: u16,
273    pub(crate) end_level: u16,
274
275    pub gold_chests: u16,
276    pub silver: u64,
277
278    pub fortress_chests: u16,
279    pub wood: u64,
280    pub stone: u64,
281
282    pub blacksmith_chests: u16,
283    pub arcane: u64,
284    pub metal: u64,
285}
286
287impl HellevatorDailyReward {
288    /// Returns `true` if the daily reward can be claimed
289    #[must_use]
290    pub fn claimable(&self) -> bool {
291        self.gold_chests > 0
292            || self.fortress_chests > 0
293            || self.blacksmith_chests > 0
294    }
295
296    pub(crate) fn parse(data: &[i64]) -> Option<HellevatorDailyReward> {
297        if data.len() < 10 {
298            return None;
299        }
300
301        Some(HellevatorDailyReward {
302            start_level: data.csiget(0, "start level", 0).unwrap_or(0),
303            end_level: data.csiget(1, "end level", 0).unwrap_or(0),
304            gold_chests: data.csiget(2, "gold chests", 0).unwrap_or(0),
305            silver: data.csiget(5, "silver reward", 0).unwrap_or(0),
306            fortress_chests: data.csiget(3, "ft chests", 0).unwrap_or(0),
307            wood: data.csiget(6, "wood reward", 0).unwrap_or(0),
308            stone: data.csiget(7, "stone reward", 0).unwrap_or(0),
309            blacksmith_chests: data.csiget(4, "bs chests", 0).unwrap_or(0),
310            arcane: data.csiget(8, "arcane reward", 0).unwrap_or(0),
311            metal: data.csiget(9, "metal reward", 0).unwrap_or(0),
312        })
313    }
314}
315
316impl Hellevator {
317    /// Converts the rank of a guild in the Hellevator into the reward bracket,
318    /// that they would be in (1 to 25). If the rank would gain no rewards, none
319    /// is returned here
320    #[must_use]
321    pub fn rank_to_rewards_rank(&self, rank: u32) -> Option<u32> {
322        let mut rank_limit = 0;
323        let mut bracket = 0;
324        for bracket_len in &self.brackets {
325            bracket += 1;
326            rank_limit += *bracket_len;
327            if rank <= rank_limit {
328                return Some(bracket);
329            }
330        }
331        None
332    }
333
334    pub(crate) fn update(
335        &mut self,
336        data: &[i64],
337        server_time: ServerTime,
338    ) -> Result<(), SFError> {
339        self.key_cards = data.csiget(0, "h key cards", 0)?;
340        self.next_card_generated = data.cstget(1, "next card", server_time)?;
341        self.next_reset = data.cstget(2, "h next reset", server_time)?;
342        self.current_floor = data.csiget(3, "h current floor", 0)?;
343        self.points = data.csiget(4, "h points", 0)?;
344        self.start_contrib_date =
345            data.cstget(5, "start contrib", server_time)?;
346        self.has_final_reward = data.cget(6, "hellevator final")? == 1;
347        self.own_best_floor = data.csiget(7, "hellevator best rank", 0)?;
348
349        for (pos, shop_item) in self.shop_items.iter_mut().enumerate() {
350            let start = data.skip(8 + pos, "shop item start")?;
351            shop_item.typ = start
352                .cfpget(0, "hellevator shop treat", |a| a)?
353                .unwrap_or_default();
354            // FIXME: This is wrong
355            shop_item.is_special =
356                start.cget(3, "hellevator shop special")? > 0;
357            shop_item.price =
358                start.csiget(6, "hellevator shop price", u32::MAX)?;
359            shop_item.duration =
360                start.csiget(9, "hellevator shop duration", 0)?;
361            shop_item.effect_strength =
362                start.csiget(12, "hellevator effect str", 0)?;
363        }
364
365        let c_typ = data.cget(23, "current ctyp")?;
366        self.current_treat = if c_typ > 0 {
367            Some(HellevatorShopTreat {
368                typ: FromPrimitive::from_i64(c_typ).unwrap_or_default(),
369                is_special: data.cget(24, "current item special")? > 0,
370                price: 0,
371                duration: data.csiget(25, "current item remaining", 0)?,
372                effect_strength: data.csiget(26, "current item effect", 0)?,
373            })
374        } else {
375            None
376        };
377
378        self.earned_today = data.csiget(27, "points earned today", 0)?;
379        // 28 => probably a "has acknowledged rank fall" msg
380        self.earned_yesterday = data.csiget(29, "points earned yd", 0)?;
381        // 30 => fallen to rank
382        // 31 => ???
383        Ok(())
384    }
385}
386
387#[derive(Debug, Default, Clone)]
388#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
389pub struct Witch {
390    /// The item type the witch wants today
391    pub required_item: Option<EquipmentSlot>,
392    /// Whether or not the cauldron is bubbling
393    pub cauldron_bubbling: bool,
394    /// The enchant role collection progress from 0-100
395    pub progress: u32,
396    /// The price in silver to enchant an item
397    pub enchantment_price: u64,
398    /// Contains the ident to use when you want to apply the enchantment. If
399    /// this is `None`, the enchantment has not been unlocked yet
400    pub enchantments: EnumMap<Enchantment, Option<EnchantmentIdent>>,
401}
402
403/// The S&F server needs a character specific value for enchanting items. This
404/// is that value
405#[derive(Debug, Clone, Copy, PartialEq, Eq)]
406#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
407pub struct EnchantmentIdent(pub(crate) NonZeroU8);
408
409impl Witch {
410    pub(crate) fn update(
411        &mut self,
412        data: &[i64],
413        server_time: ServerTime,
414    ) -> Result<(), SFError> {
415        self.required_item = None;
416        if data.cget(5, "w current item")? == 0 {
417            self.required_item =
418                ItemType::parse(data.skip(3, "witch item")?, server_time)?
419                    .and_then(|a| a.equipment_slot());
420        }
421        if self.required_item.is_none() {
422            self.cauldron_bubbling = true;
423        } else {
424            // I would like to offer the raw values here, but the -1 just
425            // makes this annoying. A Option<(u32, u32)> is also weird
426            let current: i32 = data.ciget(1, "witch current")?;
427            let target: i32 = data.ciget(2, "witch target")?;
428            #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
429            if current < 0 || target <= 0 {
430                self.progress = 100;
431            } else {
432                let current = f64::from(current);
433                let target = f64::from(target);
434                self.progress = ((current / target) * 100.0) as u32;
435            }
436        }
437
438        let e_count: u8 = data.ciget(7, "enchant count")?;
439        for i in 0..e_count {
440            let iid = data.cget(9 + 3 * i as usize, "iid")? - 1;
441            let key = match iid {
442                0 => continue,
443                10 => Enchantment::SwordOfVengeance,
444                30 => Enchantment::MariosBeard,
445                40 => Enchantment::ManyFeetBoots,
446                50 => Enchantment::ShadowOfTheCowboy,
447                60 => Enchantment::AdventurersArchaeologicalAura,
448                70 => Enchantment::ThirstyWanderer,
449                80 => Enchantment::UnholyAcquisitiveness,
450                90 => Enchantment::TheGraveRobbersPrayer,
451                100 => Enchantment::RobberBaronRitual,
452                x => {
453                    warn!("Unknown witch enchant itemtype: {x}");
454                    continue;
455                }
456            };
457            if let Some(val) = NonZeroU8::new(i + 1) {
458                *self.enchantments.get_mut(key) = Some(EnchantmentIdent(val));
459            }
460        }
461        Ok(())
462    }
463}
464
465#[derive(Debug, Clone, Default)]
466#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
467pub struct Blacksmith {
468    pub metal: u64,
469    pub arcane: u64,
470    pub dismantle_left: u8,
471    /// This seems to keep track of when you last dismantled. No idea why
472    pub last_dismantled: Option<DateTime<Local>>,
473}
474
475const PETS_PER_HABITAT: usize = 20;
476
477#[derive(Debug, Default, Clone)]
478#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
479pub struct Pets {
480    /// The total amount of pets collected in all habitats
481    pub total_collected: u16,
482    /// The rank this pet collection achieved in the hall of fame
483    pub rank: u32,
484    /// The honor this pet collection has gained
485    pub honor: u32,
486    pub max_pet_level: u16,
487    /// Information about the pvp opponent you can attack with your pets
488    pub opponent: PetOpponent,
489    /// Information about all the different habitats
490    pub habitats: EnumMap<HabitatType, Habitat>,
491    /// The next time the exploration will be possible without spending a
492    /// mushroom
493    pub next_free_exploration: Option<DateTime<Local>>,
494    /// The bonus the player receives from pets
495    pub atr_bonus: EnumMap<AttributeType, u32>,
496}
497
498#[derive(Debug, Default, Clone)]
499#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
500pub struct Habitat {
501    /// The state of the exploration of this habitat
502    pub exploration: HabitatExploration,
503    /// The amount of fruits you have for this class
504    pub fruits: u16,
505    /// Has this habitat already fought an opponent today. If so, they can not
506    /// do this until the next day
507    pub battled_opponent: bool,
508    /// All the different pets you can collect in this habitat
509    pub pets: [Pet; PETS_PER_HABITAT],
510}
511
512/// Represents the current state of the habitat exploration
513#[derive(Debug, Default, Clone)]
514#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
515pub enum HabitatExploration {
516    #[default]
517    /// Explored/won all 20 habitat battles. This means you can no longer fight
518    /// in the habitat
519    Finished,
520    /// The habitat has not yet been fully explored
521    Exploring {
522        /// The amount of pets you have already won fights against (explored)
523        /// 0..=19
524        fights_won: u32,
525        /// The level of the next habitat exploration fight
526        next_fight_lvl: u16,
527    },
528}
529
530#[derive(Debug, Default, Clone)]
531#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
532pub struct PetOpponent {
533    pub id: PlayerId,
534    pub pet_count: u32,
535    pub level_total: u32,
536    /// The next time a battle against this opponent will cost no mushroom
537    pub next_free_battle: Option<DateTime<Local>>,
538    /// The time the opponent was chosen
539    pub reroll_date: Option<DateTime<Local>>,
540    pub habitat: Option<HabitatType>,
541}
542
543impl Pets {
544    pub(crate) fn update(
545        &mut self,
546        data: &[i64],
547        server_time: ServerTime,
548    ) -> Result<(), SFError> {
549        let mut pet_id = 0;
550        for (element_idx, element) in [
551            HabitatType::Shadow,
552            HabitatType::Light,
553            HabitatType::Earth,
554            HabitatType::Fire,
555            HabitatType::Water,
556        ]
557        .into_iter()
558        .enumerate()
559        {
560            let info = self.habitats.get_mut(element);
561            let explored = data.csiget(210 + element_idx, "pet exp", 20)?;
562            info.exploration = if explored == 20 {
563                HabitatExploration::Finished
564            } else {
565                let next_lvl =
566                    data.csiget(238 + element_idx, "next exp pet lvl", 1_000)?;
567                HabitatExploration::Exploring {
568                    fights_won: explored,
569                    next_fight_lvl: next_lvl,
570                }
571            };
572            for (pet_pos, pet) in info.pets.iter_mut().enumerate() {
573                pet_id += 1;
574                pet.id = pet_id;
575                pet.level =
576                    data.csiget((pet_id + 1) as usize, "pet level", 0)?;
577                pet.fruits_today =
578                    data.csiget((pet_id + 109) as usize, "pet fruits td", 0)?;
579                pet.element = element;
580                pet.can_be_found =
581                    pet.level == 0 && explored as usize >= pet_pos;
582            }
583            info.battled_opponent =
584                1 == data.cget(223 + element_idx, "element ff")?;
585        }
586
587        self.total_collected = data.csiget(103, "total pets", 0)?;
588        self.opponent.id = data.csiget(231, "pet opponent id", 0)?;
589        self.opponent.next_free_battle =
590            data.cstget(232, "next free pet fight", server_time)?;
591        self.rank = data.csiget(233, "pet rank", 0)?;
592        self.honor = data.csiget(234, "pet honor", 0)?;
593
594        self.opponent.pet_count = data.csiget(235, "pet enemy count", 0)?;
595        self.opponent.level_total =
596            data.csiget(236, "pet enemy lvl total", 0)?;
597        self.opponent.reroll_date =
598            data.cstget(237, "pet enemy reroll date", server_time)?;
599
600        update_enum_map(&mut self.atr_bonus, data.skip(250, "pet atr boni")?);
601        Ok(())
602    }
603
604    pub(crate) fn update_pet_stat(&mut self, data: &[i64]) {
605        match PetStats::parse(data) {
606            Ok(ps) => {
607                let idx = ps.id;
608                if let Some(pet) =
609                    self.habitats.get_mut(ps.element).pets.get_mut(idx % 20)
610                {
611                    pet.stats = Some(ps);
612                }
613            }
614            Err(e) => {
615                error!("Could not parse pet stats: {e}");
616            }
617        }
618    }
619}
620
621#[derive(Debug, Default, Clone)]
622#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
623pub struct Pet {
624    pub id: u32,
625    pub level: u16,
626    /// The amount of fruits this pet got today
627    pub fruits_today: u16,
628    pub element: HabitatType,
629    /// This is None until you look at your pets again
630    pub stats: Option<PetStats>,
631    /// Check if this pet can be found already
632    pub can_be_found: bool,
633}
634
635#[derive(Debug, Default, Clone)]
636#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
637pub struct PetStats {
638    pub id: usize,
639    pub level: u16,
640    pub armor: u16,
641    pub class: Class,
642    pub attributes: EnumMap<AttributeType, u32>,
643    pub bonus_attributes: EnumMap<AttributeType, u32>,
644    pub min_damage: u16,
645    pub max_damage: u16,
646    pub element: HabitatType,
647}
648
649#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Enum, EnumIter, Hash)]
650#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
651pub enum HabitatType {
652    #[default]
653    Shadow = 0,
654    Light = 1,
655    Earth = 2,
656    Fire = 3,
657    Water = 4,
658}
659
660impl From<HabitatType> for AttributeType {
661    fn from(value: HabitatType) -> Self {
662        match value {
663            HabitatType::Water => AttributeType::Strength,
664            HabitatType::Light => AttributeType::Dexterity,
665            HabitatType::Earth => AttributeType::Intelligence,
666            HabitatType::Shadow => AttributeType::Constitution,
667            HabitatType::Fire => AttributeType::Luck,
668        }
669    }
670}
671
672impl HabitatType {
673    pub(crate) fn from_pet_id(id: i64) -> Option<Self> {
674        Some(match id {
675            1..=20 => HabitatType::Shadow,
676            21..=40 => HabitatType::Light,
677            41..=60 => HabitatType::Earth,
678            61..=80 => HabitatType::Fire,
679            81..=100 => HabitatType::Water,
680            _ => return None,
681        })
682    }
683
684    pub(crate) fn from_typ_id(id: i64) -> Option<Self> {
685        Some(match id {
686            1 => HabitatType::Shadow,
687            2 => HabitatType::Light,
688            3 => HabitatType::Earth,
689            4 => HabitatType::Fire,
690            5 => HabitatType::Water,
691            _ => return None,
692        })
693    }
694}
695
696impl PetStats {
697    pub(crate) fn parse(data: &[i64]) -> Result<Self, SFError> {
698        let pet_id: u32 = data.csiget(0, "pet index", 0)?;
699        let mut s = Self {
700            id: pet_id as usize,
701            level: data.csiget(1, "pet lvl", 0)?,
702            armor: data.csiget(2, "pet armor", 0)?,
703            class: data.cfpuget(3, "pet class", |a| a)?,
704            min_damage: data.csiget(14, "min damage", 0)?,
705            max_damage: data.csiget(15, "max damage", 0)?,
706
707            element: match data.cget(16, "pet element")? {
708                0 => HabitatType::from_pet_id(i64::from(pet_id)).ok_or_else(
709                    || SFError::ParsingError("det pet typ", pet_id.to_string()),
710                )?,
711                x => HabitatType::from_typ_id(x).ok_or_else(|| {
712                    SFError::ParsingError("det pet typ", x.to_string())
713                })?,
714            },
715            ..Default::default()
716        };
717        update_enum_map(&mut s.attributes, data.skip(4, "pet attrs")?);
718        update_enum_map(&mut s.bonus_attributes, data.skip(9, "pet bonus")?);
719        Ok(s)
720    }
721}
722
723/// The current state of the mirror
724#[derive(Debug, Clone, Copy, strum::EnumCount, Default)]
725#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
726pub enum Mirror {
727    /// The player is still collecting the mirror pieces
728    Pieces {
729        /// The amount of pieces the character has found
730        amount: u8,
731    },
732    /// The player has found all mirror pieces and thus has a working mirror
733    #[default]
734    Full,
735}
736
737impl Mirror {
738    pub(crate) fn parse(i: i64) -> Mirror {
739        /// Bitmask to cover bits 20 to 32, which is where each bit set is one
740        /// mirror piece found
741        const MIRROR_PIECES_MASK: i64 = 0xFFF8_0000;
742
743        if i & (1 << 8) != 0 {
744            return Mirror::Full;
745        }
746        Mirror::Pieces {
747            amount: (i & MIRROR_PIECES_MASK)
748                .count_ones()
749                .try_into()
750                .unwrap_or(0),
751        }
752    }
753}
754
755#[derive(Debug, Clone, Copy, PartialEq, Eq)]
756#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
757pub struct Unlockable {
758    /// Something like `Dungeon-key`
759    pub main_ident: i64,
760    /// Would be a specification of the main ident like for which dungeon
761    pub sub_ident: i64,
762}
763
764impl Unlockable {
765    pub(crate) fn parse(data: &[i64]) -> Result<Vec<Unlockable>, SFError> {
766        data.chunks_exact(2)
767            .filter(|chunk| chunk.first().copied().unwrap_or_default() != 0)
768            .map(|chunk| {
769                Ok(Unlockable {
770                    main_ident: chunk.cget(0, "unlockable ident")?,
771                    sub_ident: chunk.cget(1, "unlockable sub ident")?,
772                })
773            })
774            .collect()
775    }
776}
777
778/// The current progress towards all achievements
779#[derive(Debug, Default, Clone)]
780#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
781pub struct Achievements(pub Vec<Achievement>);
782
783impl Achievements {
784    pub(crate) fn update(&mut self, data: &[i64]) -> Result<(), SFError> {
785        self.0.clear();
786        let total_count = data.len() / 2;
787        if !data.len().is_multiple_of(2) {
788            warn!("achievement data has the wrong length: {}", data.len());
789            return Ok(());
790        }
791
792        for i in 0..total_count {
793            self.0.push(Achievement {
794                achieved: data.cget(i, "achievement achieved")? == 1,
795                progress: data.cget(i + total_count, "achievement achieved")?,
796            });
797        }
798        Ok(())
799    }
800
801    /// The amount of achievements, that have been earned
802    #[must_use]
803    pub fn owned(&self) -> u32 {
804        self.0.iter().map(|a| u32::from(a.achieved)).sum()
805    }
806}
807
808/// A small challenge you can complete in the game
809#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
810#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
811pub struct Achievement {
812    /// Whether or not this achievement has been completed
813    pub achieved: bool,
814    /// The progress of doing this achievement
815    pub progress: i64,
816}
817
818/// Contains all the items & monsters you have found in the scrapbook
819#[derive(Debug, Clone)]
820#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
821pub struct ScrapBook {
822    /// All the items, that this player has already collected. To check if an
823    /// item is in this, you should call `equipment_ident()` on an item and see
824    /// if this item contains that
825    pub items: HashSet<EquipmentIdent>,
826    /// All the monsters, that the player has seen already. I have only checked
827    /// this once, but this should match the tavern monster id.
828    // TODO: Dungeon monster ids?
829    pub monster: HashSet<u16>,
830}
831
832impl ScrapBook {
833    // 99% based on Hubert LipiƄskis Code
834    // https://github.com/HubertLipinski/sfgame-scrapbook-helper
835    pub(crate) fn parse(val: &str) -> Option<ScrapBook> {
836        let text = base64::Engine::decode(
837            &base64::engine::general_purpose::URL_SAFE,
838            val,
839        )
840        .ok()?;
841        if text.iter().all(|a| *a == 0) {
842            return None;
843        }
844
845        let mut index = 0;
846        let mut items = HashSet::new();
847        let mut monster = HashSet::new();
848
849        for byte in text {
850            for bit_pos in (0..=7).rev() {
851                index += 1;
852                let is_owned = ((byte >> bit_pos) & 1) == 1;
853                if !is_owned {
854                    continue;
855                }
856                if index < 801 {
857                    // Monster
858                    monster.insert(index.try_into().unwrap_or_default());
859                } else if let Some(ident) = parse_scrapbook_item(index) {
860                    // Items
861                    if !items.insert(ident) {
862                        error!(
863                            "Two scrapbook positions parsed to the same \
864                             ident: {index}"
865                        );
866                    }
867                } else {
868                    error!("Owned, but not parsed: {index}");
869                }
870            }
871        }
872        Some(ScrapBook { items, monster })
873    }
874}
875
876/// The identification of items in the scrapbook
877#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
878#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
879pub struct EquipmentIdent {
880    /// The class the item has and thus the wearer must have
881    pub class: Option<Class>,
882    /// The position at which the item is worn
883    pub typ: EquipmentSlot,
884    /// The model id, this is basically the "name"" of the item
885    pub model_id: u16,
886    /// The color variation of this item
887    pub color: u8,
888}
889
890#[allow(clippy::to_string_trait_impl)]
891impl ToString for EquipmentIdent {
892    fn to_string(&self) -> String {
893        let item_typ = self.typ.raw_id();
894        let model_id = self.model_id;
895        let color = self.color;
896
897        if let Some(class) = self.class {
898            let ci = class as u8 + 1;
899            format!("itm{item_typ}_{model_id}_{color}_{ci}")
900        } else {
901            format!("itm{item_typ}_{model_id}_{color}")
902        }
903    }
904}
905
906#[allow(clippy::enum_glob_use)]
907fn parse_scrapbook_item(item_idx: i64) -> Option<EquipmentIdent> {
908    use Class::*;
909    use EquipmentSlot::*;
910    let slots: [(_, _, _, &[_]); 44] = [
911        (801..1011, Amulet, None, &[]),
912        (1011..1051, Amulet, None, &[]),
913        (1051..1211, Ring, None, &[]),
914        (1211..1251, Ring, None, &[]),
915        (1251..1325, Talisman, None, &[]),
916        (1325..1365, Talisman, None, &[]),
917        (1365..1665, Weapon, Some(Warrior), &[]),
918        (1665..1705, Weapon, Some(Warrior), &[]),
919        (1705..1805, Shield, Some(Warrior), &[]),
920        (1805..1845, Shield, Some(Warrior), &[]),
921        (1845..1945, BreastPlate, Some(Warrior), &[]),
922        (1945..1985, BreastPlate, Some(Warrior), &[1954, 1955]),
923        (1985..2085, FootWear, Some(Warrior), &[]),
924        (2085..2125, FootWear, Some(Warrior), &[2094, 2095]),
925        (2125..2225, Gloves, Some(Warrior), &[]),
926        (2225..2265, Gloves, Some(Warrior), &[2234, 2235]),
927        (2265..2365, Hat, Some(Warrior), &[]),
928        (2365..2405, Hat, Some(Warrior), &[2374, 2375]),
929        (2405..2505, Belt, Some(Warrior), &[]),
930        (2505..2545, Belt, Some(Warrior), &[2514, 2515]),
931        (2545..2645, Weapon, Some(Mage), &[]),
932        (2645..2685, Weapon, Some(Mage), &[]),
933        (2685..2785, BreastPlate, Some(Mage), &[]),
934        (2785..2825, BreastPlate, Some(Mage), &[2794, 2795]),
935        (2825..2925, FootWear, Some(Mage), &[]),
936        (2925..2965, FootWear, Some(Mage), &[2934, 2935]),
937        (2965..3065, Gloves, Some(Mage), &[]),
938        (3065..3105, Gloves, Some(Mage), &[3074, 3075]),
939        (3105..3205, Hat, Some(Mage), &[]),
940        (3205..3245, Hat, Some(Mage), &[3214, 3215]),
941        (3245..3345, Belt, Some(Mage), &[]),
942        (3345..3385, Belt, Some(Mage), &[3354, 3355]),
943        (3385..3485, Weapon, Some(Scout), &[]),
944        (3485..3525, Weapon, Some(Scout), &[]),
945        (3525..3625, BreastPlate, Some(Scout), &[]),
946        (3625..3665, BreastPlate, Some(Scout), &[3634, 3635]),
947        (3665..3765, FootWear, Some(Scout), &[]),
948        (3765..3805, FootWear, Some(Scout), &[3774, 3775]),
949        (3805..3905, Gloves, Some(Scout), &[]),
950        (3905..3945, Gloves, Some(Scout), &[3914, 3915]),
951        (3945..4045, Hat, Some(Scout), &[]),
952        (4045..4085, Hat, Some(Scout), &[4054, 4055]),
953        (4085..4185, Belt, Some(Scout), &[]),
954        (4185..4225, Belt, Some(Scout), &[4194, 4195]),
955    ];
956
957    #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
958    for (pos, (range, typ, class, ignore)) in slots.into_iter().enumerate() {
959        if !range.contains(&item_idx) {
960            continue;
961        }
962        if ignore.contains(&item_idx) {
963            return None;
964        }
965
966        let is_epic = pos % 2 == 1;
967        let relative_pos = item_idx - range.start + 1;
968
969        let color = match relative_pos % 10 {
970            _ if typ == Talisman || is_epic => 1,
971            0 => 5,
972            1..=5 => relative_pos % 10,
973            _ => relative_pos % 10 - 5,
974        } as u8;
975
976        let model_id = match () {
977            () if is_epic => relative_pos + 49,
978            () if typ == Talisman => relative_pos,
979            () if relative_pos % 5 != 0 => relative_pos / 5 + 1,
980            () => relative_pos / 5,
981        } as u16;
982
983        return Some(EquipmentIdent {
984            class,
985            typ,
986            model_id,
987            color,
988        });
989    }
990    None
991}