| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
 | use crate::{
    game::ActiveRecipe,
    protocol::{ItemIndex, TileIndex},
    recipes::{Action, Gamedata},
};
use log::{debug, info};
use std::collections::BTreeSet;
use Out::*;
pub enum Out {
    Take(usize),
    Put,
    Produce(ItemIndex),
    Consume(usize),
    SetActive(Option<f32>),
}
pub fn interact(
    data: &Gamedata,
    edge: bool,
    tile: TileIndex,
    active: &mut Option<ActiveRecipe>,
    mut items: Vec<ItemIndex>,
    mut hand: Option<ItemIndex>,
    mut out: impl FnMut(Out),
) {
    let mut allowed = BTreeSet::new();
    for r in &data.recipes {
        if r.tile == tile {
            allowed.extend(r.inputs.clone())
        }
    }
    if !edge {
        if let Some(ac) = active {
            if matches!(data.recipes[ac.recipe].action, Action::Active(_)) {
                ac.working -= 1;
            }
        }
        return;
    }
    if active.is_none() && !items.is_empty() && hand.is_none() {
        out(Take(items.len() - 1));
        return;
    }
    if active.is_none() {
        if let Some(hi) = hand {
            if allowed.contains(&hi) {
                out(Put);
                items.push(hi);
                hand = None;
            }
        }
    }
    if hand.is_none() {
        'rloop: for (ri, r) in data.recipes.iter().enumerate() {
            if tile != r.tile {
                continue;
            }
            let mut inputs = r.inputs.clone();
            for i in &items {
                debug!("take {i:?} {inputs:?}");
                let Some(pos) = inputs.iter().position(|e| e == i) else {
                    continue 'rloop;
                };
                inputs.remove(pos);
            }
            debug!("end {inputs:?}");
            if !inputs.is_empty() {
                continue;
            }
            match r.action {
                Action::Passive(_) => {
                    info!("use recipe {r:?}");
                    *active = Some(ActiveRecipe {
                        recipe: ri,
                        progress: 0.,
                        working: 0,
                    });
                    break 'rloop;
                }
                Action::Active(_) => {}
                Action::Instant => {
                    info!("use recipe {r:?}");
                    for i in 0..items.len() {
                        out(Consume(i))
                    }
                    for i in &r.outputs {
                        out(Produce(*i));
                    }
                    if !r.outputs.is_empty() {
                        out(Take(r.outputs.len() - 1));
                    }
                    items.clear();
                    break 'rloop;
                }
                Action::Never => (),
            }
        }
    }
}
pub fn tick_tile(
    dt: f32,
    data: &Gamedata,
    _tile: TileIndex,
    active: &mut Option<ActiveRecipe>,
    mut items: Vec<ItemIndex>,
    mut out: impl FnMut(Out),
) {
    if let Some(a) = active {
        let r = &data.recipes[a.recipe];
        a.progress += dt / r.action.duration();
        if a.progress >= 1. {
            for i in 0..items.len() {
                out(Consume(i))
            }
            for i in &r.outputs {
                out(Produce(*i));
            }
            out(SetActive(None));
            active.take();
        } else {
            out(SetActive(Some(a.progress)));
        }
    }
}
 |