aboutsummaryrefslogtreecommitdiff
path: root/server/src/data/demands.rs
blob: 3f4b839d870aa863e4195ca9e458a6d234c53bc4 (plain)
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
/*
    Hurry Curry! - a game about cooking
    Copyright 2024 metamuffin

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, version 3 of the License only.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.

*/
use hurrycurry_protocol::{Demand, ItemIndex, Recipe, TileIndex};
use std::collections::{HashMap, HashSet};

pub fn generate_demands(
    tiles: &HashSet<TileIndex>,
    items: &HashSet<ItemIndex>,
    raw_demands: &[(ItemIndex, Option<ItemIndex>, f32)],
    recipes: &[Recipe],
) -> Vec<Demand> {
    let recipes = recipes
        .iter()
        .filter(|r| r.tile().map(|t| tiles.contains(&t)).unwrap_or(true))
        .collect::<Vec<_>>();

    let mut producable = HashMap::new();

    for i in items {
        producable.insert(*i, 0.0);
    }

    loop {
        let prod_count = producable.len();

        for r in &recipes {
            let output_count = r.outputs().iter().filter(|o| !items.contains(o)).count();
            let Some(ingred_cost) = r
                .inputs()
                .iter()
                .map(|i| producable.get(i).copied())
                .reduce(|a, b| {
                    if let (Some(a), Some(b)) = (a, b) {
                        Some(a + b)
                    } else {
                        None
                    }
                })
                .unwrap_or(Some(0.))
            else {
                continue;
            };

            let base_cost = match r {
                Recipe::Passive { duration, .. } => 2. + duration * 0.1,
                Recipe::Active { duration, .. } => 2. + duration,
                Recipe::Instant { .. } => 1.,
            };

            let output_cost = (ingred_cost + base_cost) / output_count as f32;
            for o in r.outputs() {
                let cost = producable.entry(o).or_insert(f32::INFINITY);
                *cost = cost.min(output_cost);
            }
        }

        if prod_count == producable.len() {
            break;
        }
    }

    raw_demands
        .iter()
        .filter_map(|(i, o, d)| {
            producable.get(i).map(|cost| Demand {
                input: *i,
                output: *o,
                duration: *d,
                points: *cost as i64,
            })
        })
        .collect()
}