/*
Undercooked - 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 .
*/
pub mod movement;
mod pathfinding;
use crate::{
data::Gamedata,
game::Tile,
protocol::{DemandIndex, Message, PacketS, PlayerID},
};
use anyhow::{anyhow, Result};
use fake::{faker, Fake};
use glam::{IVec2, Vec2};
use log::debug;
use movement::MovementBase;
use pathfinding::{find_path, Path};
use rand::{random, thread_rng};
use std::{
collections::{HashMap, HashSet},
sync::Arc,
};
pub struct DemandState {
data: Arc,
walkable: HashSet,
chairs: HashMap,
customer_id_counter: PlayerID,
customers: HashMap,
}
enum CustomerState {
Entering {
path: Path,
chair: IVec2,
},
Waiting {
demand: DemandIndex,
chair: IVec2,
timeout: f32,
},
Eating {
demand: DemandIndex,
target: IVec2,
progress: f32,
chair: IVec2,
},
Exiting {
path: Path,
},
}
pub struct Customer {
movement: MovementBase,
state: CustomerState,
}
impl DemandState {
pub fn new(data: Arc, map: &HashMap) -> Self {
let chair = data.get_tile_by_name("chair");
Self {
walkable: map
.iter()
.filter(|(_, v)| !data.is_tile_colliding(v.kind))
.map(|(e, _)| *e)
.collect(),
chairs: map
.iter()
.filter(|(_, v)| Some(v.kind) == chair)
.map(|(e, _)| (*e, true))
.collect(),
customer_id_counter: PlayerID(0),
customers: Default::default(),
data,
}
}
}
impl DemandState {
pub fn tick(
&mut self,
packets_out: &mut Vec<(PlayerID, PacketS)>,
tiles: &mut HashMap,
data: &Gamedata,
dt: f32,
) -> Result<()> {
if self.customers.len() < 5 {
self.customer_id_counter.0 -= 1;
let id = self.customer_id_counter;
packets_out.push((
id,
PacketS::Join {
name: faker::name::fr_fr::Name().fake(),
character: -2,
},
));
let chair = self.select_chair().ok_or(anyhow!("no free chair found"))?;
let from = data.customer_spawn.as_ivec2();
let path = find_path(&self.walkable, from, chair)
.ok_or(anyhow!("no path from {from} to {chair}"))?;
self.customers.insert(
id,
Customer {
movement: MovementBase {
position: data.customer_spawn,
facing: Vec2::X,
vel: Vec2::ZERO,
},
state: CustomerState::Entering { path, chair },
},
);
}
let mut customers_to_remove = Vec::new();
for (&id, p) in &mut self.customers {
match &mut p.state {
CustomerState::Entering { path, chair } => {
debug!("{id:?} entering");
packets_out.push((id, path.execute_tick(&mut p.movement, &self.walkable, dt)));
if path.is_done() {
let demand = DemandIndex(random::() % self.data.demands.len());
packets_out.push((
id,
PacketS::Communicate {
message: Some(Message::Item(data.demand(demand).from)),
},
));
p.state = CustomerState::Waiting {
chair: *chair,
timeout: 60.,
demand,
};
}
}
CustomerState::Waiting {
chair,
demand,
timeout,
} => {
debug!("{id:?} waiting");
*timeout -= dt;
if *timeout <= 0. {
packets_out.push((id, PacketS::Communicate { message: None }));
let path = find_path(
&self.walkable,
p.movement.position.as_ivec2(),
data.customer_spawn.as_ivec2(),
)
.expect("no path to exit");
*self.chairs.get_mut(&chair).unwrap() = true;
p.state = CustomerState::Exiting { path }
} else {
let demand_data = &data.demand(*demand);
let demand_pos = [IVec2::NEG_X, IVec2::NEG_Y, IVec2::X, IVec2::Y]
.into_iter()
.find_map(|off| {
let pos = *chair + off;
if tiles
.get(&pos)
.map(|t| {
t.item
.as_ref()
.map(|i| i.kind == demand_data.from)
.unwrap_or_default()
})
.unwrap_or_default()
{
Some(pos)
} else {
None
}
});
if let Some(pos) = demand_pos {
packets_out.push((id, PacketS::Communicate { message: None }));
for edge in [true, false] {
packets_out.push((id, PacketS::Interact { pos, edge }))
}
p.state = CustomerState::Eating {
demand: *demand,
target: pos,
progress: 0.,
chair: *chair,
}
}
}
}
CustomerState::Eating {
demand,
target,
progress,
chair,
} => {
debug!("{id:?} eating");
let demand = data.demand(*demand);
*progress += dt / demand.duration;
if *progress >= 1. {
packets_out.push((
id,
PacketS::ReplaceHand {
item: Some(demand.to),
},
));
for edge in [true, false] {
packets_out.push((id, PacketS::Interact { pos: *target, edge }))
}
let path = find_path(
&self.walkable,
p.movement.position.as_ivec2(),
data.customer_spawn.as_ivec2(),
)
.ok_or(anyhow!("no path to exit"))?;
*self.chairs.get_mut(&chair).unwrap() = true;
p.state = CustomerState::Exiting { path }
}
}
CustomerState::Exiting { path } => {
debug!("{id:?} exiting");
packets_out.push((id, path.execute_tick(&mut p.movement, &self.walkable, dt)));
if path.is_done() {
packets_out.push((id, PacketS::Leave));
customers_to_remove.push(id);
}
}
}
}
for c in customers_to_remove {
self.customers.remove(&c).unwrap();
}
Ok(())
}
pub fn select_chair(&mut self) -> Option {
use rand::seq::IteratorRandom;
let (chosen, free) = self
.chairs
.iter_mut()
.filter(|(_p, free)| **free)
.choose(&mut thread_rng())?;
*free = false;
Some(*chosen)
}
}