aboutsummaryrefslogtreecommitdiff
path: root/server/src/entity/campaign.rs
blob: 5838d3ce40abe51de3effc3e5d70c9680264275d (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
/*
    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 super::{Entity, EntityContext};
use crate::{message::TrError, scoreboard::ScoreboardStore, server::GameServerExt, trm};
use anyhow::Result;
use hurrycurry_protocol::{
    glam::{IVec2, Vec2},
    Message, PacketC, PlayerID, TileIndex,
};
use serde::{Deserialize, Serialize};

#[derive(Debug, Default, Clone)]
pub struct Map {
    pub location: Vec2,
    pub name: String,
}

impl Entity for Map {
    fn tick(&mut self, c: EntityContext) -> Result<()> {
        let mut activate = false;
        c.game
            .players_spatial_index
            .query(self.location, 0.5, |_, _| activate = true);

        if activate {
            *c.load_map = Some(self.name.clone());
        }

        Ok(())
    }
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum GateCondition {
    All(Vec<GateCondition>),
    Any(Vec<GateCondition>),
    Stars(String, u8),
}

#[derive(Debug, Clone)]
pub struct Gate {
    pub active: bool,
    pub unlocked: bool,
    pub location: IVec2,
    pub blocker_tile: TileIndex,
    pub condition: GateCondition,
}
impl Entity for Gate {
    fn tick(&mut self, c: EntityContext<'_>) -> Result<()> {
        if self.active {
            self.active = false;
            self.unlocked = self.condition.check(c.scoreboard);
            if !self.unlocked {
                c.game
                    .set_tile(self.location, Some(self.blocker_tile), c.packet_out)
            }
        }
        Ok(())
    }
    fn interact(
        &mut self,
        c: EntityContext<'_>,
        pos: Option<IVec2>,
        _player: PlayerID,
    ) -> Result<bool, TrError> {
        if !self.unlocked && pos == Some(self.location) {
            c.packet_out.push_back(PacketC::ServerMessage {
                message: trm!(
                    "s.campaign.unlock_condition",
                    m = self.condition.show(c.scoreboard) // TODO localize
                ),
                error: false,
            });
            return Ok(true);
        }
        Ok(false)
    }
}

impl GateCondition {
    fn check(&self, scoreboard: &ScoreboardStore) -> bool {
        match self {
            GateCondition::All(cs) => cs.iter().all(|c| c.check(scoreboard)),
            GateCondition::Any(cs) => cs.iter().any(|c| c.check(scoreboard)),
            GateCondition::Stars(map, thres) => scoreboard.get(map).map_or(false, |s| {
                s.best.first().map_or(false, |b| b.score.stars >= *thres)
            }),
        }
    }
    pub fn show(&self, scoreboard: &ScoreboardStore) -> Message {
        match self {
            GateCondition::All(cs) => cs
                .iter()
                .map(|c| c.show(scoreboard))
                .reduce(|a, b| trm!("s.campaign.list_helper", m = a, m = b))
                .unwrap_or(Message::Text(String::new())),
            GateCondition::Any(cs) => cs
                .iter()
                .map(|c| c.show(scoreboard))
                .reduce(|a, b| trm!("s.campaign.list_helper", m = a, m = b))
                .unwrap_or(Message::Text(String::new())),
            GateCondition::Stars(map, thres) => {
                trm!(
                    "s.campaign.condition.stars",
                    s = thres.to_string(),
                    s = map.to_string()
                )
            }
        }
    }
}