aboutsummaryrefslogtreecommitdiff
path: root/server/src/entity/mod.rs
blob: 81061bb5816d0ca7478718ede3e912a0ae7ce644 (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
/*
    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/>.

*/
pub mod conveyor;
pub mod customers;
pub mod environment_effect;
pub mod item_portal;
pub mod player_portal;

use crate::{data::ItemTileRegistry, game::Game, interaction::Recipe};
use anyhow::{anyhow, Result};
use conveyor::Conveyor;
use customers::{demands::generate_demands, Customers};
use environment_effect::{EnvironmentController, EnvironmentEffect, EnvironmentEffectController};
use hurrycurry_protocol::{
    glam::{IVec2, Vec2},
    ItemIndex, PacketC, TileIndex,
};
use item_portal::ItemPortal;
use player_portal::PlayerPortal;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet, VecDeque};

pub trait EntityT: Clone {
    fn tick(&mut self, game: &mut Game, packet_out: &mut VecDeque<PacketC>, dt: f32) -> Result<()>;
}

macro_rules! entities {
    ($($e:ident),*) => {
        #[derive(Debug, Clone)]
        pub enum Entity { $($e($e)),* }
        impl EntityT for Entity {
            fn tick(&mut self, game: &mut Game, packet_out: &mut VecDeque<PacketC>, dt: f32) -> Result<()> {
                match self { $(Entity::$e(x) => x.tick(game, packet_out, dt)),*, }
            }
        }
    };
}

entities!(
    Conveyor,
    ItemPortal,
    PlayerPortal,
    Customers,
    EnvironmentEffectController,
    EnvironmentController
);

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum EntityDecl {
    Conveyor {
        from: Option<IVec2>,
        to: Option<IVec2>,
        filter_dir: Option<IVec2>,
        filter: Option<String>,
        dir: Option<IVec2>,
        speed: Option<f32>,
    },
    ItemPortal {
        from: Option<IVec2>,
        to: IVec2,
    },
    PlayerPortal {
        from: Option<Vec2>,
        to: Vec2,
    },
    Customers {},
    EnvironmentEffect(EnvironmentEffect),
    Environment(Vec<String>),
}

pub fn construct_entity(
    pos: Option<IVec2>,
    decl: &EntityDecl,
    reg: &ItemTileRegistry,
    tiles_used: &HashSet<TileIndex>,
    items_used: &HashSet<ItemIndex>,
    raw_demands: &[(ItemIndex, Option<ItemIndex>, f32)],
    recipes: &[Recipe],
    initial_map: &HashMap<IVec2, (TileIndex, Option<ItemIndex>)>,
) -> Result<Entity> {
    Ok(match decl.to_owned() {
        EntityDecl::ItemPortal { from, to } => Entity::ItemPortal(ItemPortal {
            from: from.or(pos).ok_or(anyhow!("no item portal start"))?,
            to,
        }),
        EntityDecl::PlayerPortal { from, to } => Entity::PlayerPortal(PlayerPortal {
            from: from
                .or(pos.map(|v| v.as_vec2()))
                .ok_or(anyhow!("no player portal start"))?,
            to,
        }),
        EntityDecl::Conveyor {
            from,
            to,
            speed,
            dir,
            filter,
            filter_dir,
        } => {
            let from = from.or(pos).ok_or(anyhow!("conveyor has no start"))?;
            let to = to
                .or(dir.map(|s| s + from))
                .ok_or(anyhow!("conveyor has no destination"))?;
            Entity::Conveyor(Conveyor {
                from,
                to,
                max_cooldown: 1. / speed.unwrap_or(2.),
                filter_tile: filter_dir.map(|o| to + o),
                filter_item: filter.map(|name| reg.register_item(name)),
                cooldown: 0.,
            })
        }
        EntityDecl::Customers {} => {
            let demands = generate_demands(tiles_used, items_used, raw_demands, recipes);
            let chair = reg.register_tile("chair".to_string());
            let chairs = initial_map
                .iter()
                .filter(|(_, (tile, _))| *tile == chair)
                .map(|(e, _)| (*e, true))
                .collect();
            Entity::Customers(Customers::new(chairs, demands)?)
        }
        EntityDecl::EnvironmentEffect(config) => {
            Entity::EnvironmentEffectController(EnvironmentEffectController::new(config))
        }
        EntityDecl::Environment(names) => {
            Entity::EnvironmentController(EnvironmentController(names))
        }
    })
}