1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
use crate::*;

use std::collections::HashMap;
use std::hash::Hash;

// crafting
/// A transition from one or more items into one or more different items.
/// Can be used for all sorts of crafting.
#[derive(new, Clone, Serialize, Deserialize, Debug, Builder)]
pub struct ItemTransitionDefinition<K, I, E, S> {
    /// The id of this item transition.
    pub key: K,
    /// The name of the transition.
    pub name: String,
    /// The friendly name of the transition.
    pub friendly_name: String,
    /// The icon path.
    pub icon_path: Option<String>,
    /// The different input items, quantities and `UseMode` for each one.
    pub input_items: Vec<(I, usize, UseMode)>,
    /// The required stats conditions required to process the transition.
    pub stat_conditions: Vec<StatCondition<S>>,
    /// The effectors applied during crafting.
    pub stat_effectors: Vec<E>,
    /// The different output items.
    pub output_items: Vec<(I, usize)>,
    /// What happens when you lose the condition required to continue the transition.
    pub on_condition_lost: ConditionLostReaction,
    /// The time to complete the transition.
    pub time_to_complete: f64,
    /// Consume the input items at the start of the transition, regardless of the result.
    pub consume_input_immediate: bool,
    /// Automatically transition when all the required conditions are met.
    pub auto_trigger: bool,
}

/// The way items are used in a transition.
#[derive(Clone, Serialize, Deserialize, Debug)]
pub enum UseMode {
    /// Consumes the item. The item will be lost.
    Consume,
    /// Uses a set amount of durability from the item.
    UseOnce {
        /// The amount of durability used.
        durability: f64,
    },
    /// Uses a set amount of durability from the item each second.
    UsePerSecond {
        /// The durability usage per second.
        rate: f64,
    },
}

/// What happens when the transition is stopped or conditions aren't met anymore for it to
/// continue.
#[derive(Clone, Serialize, Deserialize, Debug)]
pub enum ConditionLostReaction {
    /// Nothing happens, the transition continues.
    None,
    /// The transition pauses and keeps its progress.
    Pause,
    /// The transition is cancelled and all progress is lost.
    /// If consume_input_immediate was true, the input items are not returned.
    Cancel,
}

/// A transition in progress.
#[derive(new, Clone, Serialize, Deserialize, Debug)]
pub struct ItemTransitionBatch<K> {
    /// The transition id.
    transition: K,
    /// The number of transitions that are queued.
    remaining: u32,
    /// The time until the current transition is completed.
    next_completion_remaining: f64,
}

/// The definitions of all known stats.
#[derive(Debug, Clone, Serialize, Deserialize, new)]
pub struct ItemTransitionDefinitions<K: Hash + Eq, I, E, S> {
    /// The definitions.
    pub defs: HashMap<K, ItemTransitionDefinition<K, I, E, S>>,
}

impl<K: Hash + Eq, I, E, S> Default for ItemTransitionDefinitions<K, I, E, S> {
    fn default() -> Self {
        Self {
            defs: HashMap::default(),
        }
    }
}

impl<K: Hash + Eq + Clone, I, E, S> From<Vec<ItemTransitionDefinition<K, I, E, S>>>
    for ItemTransitionDefinitions<K, I, E, S>
{
    fn from(t: Vec<ItemTransitionDefinition<K, I, E, S>>) -> Self {
        let defs = t
            .into_iter()
            .map(|s| (s.key.clone(), s))
            .collect::<HashMap<_, _>>();
        Self::new(defs)
    }
}