aboutsummaryrefslogtreecommitdiff
path: root/server/data/src/registry.rs
blob: 7d56567dc8757bee79e0623a036d8746a7ad1f2c (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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
    Hurry Curry! - a game about cooking
    Copyright (C) 2025 Hurry Curry! Contributors

    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::{Gamedata, ItemIndex, Recipe, TileIndex};
use log::debug;
use std::{
    collections::{BTreeSet, HashMap},
    sync::RwLock,
};

use crate::Serverdata;

#[derive(Default)]
pub(crate) struct ItemTileRegistry {
    tiles: RwLock<Vec<String>>,
    items: RwLock<Vec<String>>,
}

impl ItemTileRegistry {
    pub fn register_tile(&self, name: String) -> TileIndex {
        TileIndex(Self::register(&self.tiles, name))
    }
    pub fn register_item(&self, name: String) -> ItemIndex {
        ItemIndex(Self::register(&self.items, name))
    }
    fn register(db: &RwLock<Vec<String>>, name: String) -> usize {
        let mut db = db.write().unwrap();
        // TODO maybe btreemap for better time complexity
        if let Some(index) = db.iter().position(|e| e == &name) {
            index
        } else {
            let index = db.len();
            db.push(name);
            index
        }
    }
    pub fn finish(self) -> (Vec<String>, Vec<String>) {
        (
            self.items.into_inner().unwrap(),
            self.tiles.into_inner().unwrap(),
        )
    }
}

pub(crate) fn filter_unused_tiles_and_items(data: &mut Gamedata, serverdata: &mut Serverdata) {
    debug!("running unused item/tile filter");
    let mut used_items = BTreeSet::new();
    let mut used_tiles = BTreeSet::new();

    for recipe in &data.recipes {
        used_items.extend(recipe.inputs());
        used_items.extend(recipe.outputs());
        used_tiles.extend(recipe.tile());
    }
    for demand in &data.demands {
        used_items.insert(demand.input);
        used_items.extend(demand.output);
    }
    for rg in serverdata.recipe_groups.values() {
        used_items.extend(rg);
    }
    for &(tile, item) in serverdata.initial_map.values() {
        used_tiles.insert(tile);
        used_items.extend(item);
    }

    let mut item_names = Vec::new();
    let mut item_map = HashMap::new();
    for item in used_items {
        item_map.insert(item, ItemIndex(item_names.len()));
        item_names.push(data.item_name(item).to_string());
    }

    let mut tile_names = Vec::new();
    let mut tile_map = HashMap::new();
    for tile in used_tiles {
        tile_map.insert(tile, TileIndex(tile_names.len()));
        tile_names.push(data.tile_name(tile).to_string());
    }

    debug!(
        "removing {} items and {} tiles from registry",
        data.item_names.len() - item_names.len(),
        data.tile_names.len() - tile_names.len()
    );

    data.item_names = item_names;
    data.tile_names = tile_names;

    for recipe in &mut data.recipes {
        match recipe {
            Recipe::Passive {
                tile,
                input,
                output,
                ..
            } => {
                if let Some(tile) = tile {
                    *tile = tile_map[tile]
                }
                *input = item_map[input];
                if let Some(output) = output {
                    *output = item_map[output];
                }
            }
            Recipe::Active {
                tile,
                input,
                outputs,
                ..
            } => {
                if let Some(tile) = tile {
                    *tile = tile_map[tile]
                }
                *input = item_map[input];
                for output in outputs {
                    if let Some(output) = output {
                        *output = item_map[output];
                    }
                }
            }
            Recipe::Instant {
                tile,
                inputs,
                outputs,
                ..
            } => {
                if let Some(tile) = tile {
                    *tile = tile_map[tile]
                }
                for input in inputs {
                    if let Some(input) = input {
                        *input = item_map[input];
                    }
                }
                for output in outputs {
                    if let Some(output) = output {
                        *output = item_map[output];
                    }
                }
            }
        }
    }
    for demand in &mut data.demands {
        demand.input = item_map[&demand.input];
        if let Some(output) = &mut demand.output {
            *output = item_map[output];
        }
    }
    for rg in serverdata.recipe_groups.values_mut() {
        *rg = rg.clone().into_iter().map(|e| item_map[&e]).collect();
    }
    for (tile, item) in serverdata.initial_map.values_mut() {
        *tile = tile_map[tile];
        if let Some(item) = item {
            *item = item_map[item]
        }
    }
    data.tile_walkable = data
        .tile_walkable
        .clone()
        .into_iter()
        .map(|e| tile_map[&e])
        .collect();
    data.tile_interactable_empty = data
        .tile_interactable_empty
        .clone()
        .into_iter()
        .map(|e| tile_map[&e])
        .collect();
    data.tile_placeable_items = data
        .tile_placeable_items
        .clone()
        .into_iter()
        .map(|(tile, items)| {
            (
                tile_map[&tile],
                items.into_iter().map(|i| item_map[&i]).collect(),
            )
        })
        .collect();

    debug!("done")
}