diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/bot/src/algos/customer.rs | 104 | ||||
-rw-r--r-- | server/bot/src/main.rs | 2 | ||||
-rw-r--r-- | server/src/data/demands.rs | 7 | ||||
-rw-r--r-- | server/src/entity/bot.rs | 1 |
4 files changed, 64 insertions, 50 deletions
diff --git a/server/bot/src/algos/customer.rs b/server/bot/src/algos/customer.rs index 69712ba8..c1a13d31 100644 --- a/server/bot/src/algos/customer.rs +++ b/server/bot/src/algos/customer.rs @@ -22,7 +22,7 @@ use crate::{ use hurrycurry_client_lib::Game; use hurrycurry_protocol::{glam::IVec2, DemandIndex, Message, PacketS, PlayerID}; use log::info; -use rand::random; +use rand::{random, seq::IndexedRandom, thread_rng}; #[derive(Debug, Clone)] pub enum Customer { @@ -56,29 +56,28 @@ impl Default for Customer { impl BotAlgo for Customer { fn tick(&mut self, me: PlayerID, game: &Game, dt: f32) -> BotInput { let _ = (me, game, dt); - let Some(playerdata) = game.players.get_mut(&me) else { + let Some(playerdata) = game.players.get(&me) else { return BotInput::default(); }; let pos = playerdata.movement.position; match self { Customer::New => { - if let Some((chair, _)) = game + if let Some(&chair) = game .tiles .iter() - .find(|(_, t)| game.data.tile_name(t.kind) == "chair") + .filter(|(_, t)| game.data.tile_name(t.kind) == "chair") + .map(|(p, _)| *p) + .collect::<Vec<_>>() + .choose(&mut thread_rng()) { - if let Some(path) = find_path(&game.walkable, pos.as_ivec2(), *chair) { + if let Some(path) = find_path(&game.walkable, pos.as_ivec2(), chair) { info!("{me:?} -> entering"); - *self = Customer::Entering { - path, - chair: *chair, - }; + *self = Customer::Entering { path, chair }; } } BotInput::default() } Customer::Entering { path, chair } => { - let direction = path.next_direction(pos); if path.is_done() { let demand = DemandIndex(random::<u32>() as usize % game.data.demands.len()); info!("{me:?} -> waiting"); @@ -97,7 +96,7 @@ impl BotAlgo for Customer { } } else { BotInput { - direction, + direction: path.next_direction(pos), ..Default::default() } } @@ -107,9 +106,6 @@ impl BotAlgo for Customer { demand, timeout, } => { - playerdata - .movement - .input((chair.as_vec2() + 0.5) - pos, false); *timeout -= dt; if *timeout <= 0. { let path = find_path( @@ -194,7 +190,10 @@ impl BotAlgo for Customer { ..Default::default() } } else { - BotInput::default() + BotInput { + direction: (chair.as_vec2() + 0.5) - pos, + ..Default::default() + } } } } @@ -204,46 +203,59 @@ impl BotAlgo for Customer { progress, chair, } => { - playerdata - .movement - .input((chair.as_vec2() + 0.5) - playerdata.position(), false); let demand = &game.data.demands[demand.0]; *progress += dt / demand.duration; if *progress >= 1. { - self.cpackets.push_back(PacketS::ReplaceHand { - player, - item: demand.to, - }); - if demand.to.is_some() { - self.cpackets.push_back(PacketS::Interact { - player, - pos: Some(*target), + if let Some(path) = find_path( + &game.walkable, + pos.as_ivec2(), + pos.as_ivec2(), + // game.data.customer_spawn.as_ivec2(), + ) { + let mut packets = Vec::new(); + packets.push(PacketS::ReplaceHand { + player: me, + item: demand.output, }); - self.cpackets - .push_back(PacketS::Interact { player, pos: None }); + if demand.output.is_some() { + packets.push(PacketS::Interact { + player: me, + pos: Some(*target), + }); + packets.push(PacketS::Interact { + player: me, + pos: None, + }); + // *self.chairs.get_mut(chair).unwrap() = true; + // game.score.demands_completed += 1; + // game.score.points += demand.points; + // game.score_changed = true; + info!("{me:?} -> exiting"); + *self = Customer::Exiting { path }; + return BotInput { + extra: packets, + ..Default::default() + }; + } } - let path = find_path( - &game.walkable, - playerdata.position().as_ivec2(), - game.data.customer_spawn.as_ivec2(), - ) - .ok_or(anyhow!("no path to exit"))?; - *self.chairs.get_mut(chair).unwrap() = true; - game.score.demands_completed += 1; - game.score.points += demand.points; - game.score_changed = true; - info!("{player:?} -> exiting"); - *state = Customer::Exiting { path } + } + BotInput { + direction: (chair.as_vec2() + 0.5) - pos, + ..Default::default() } } Customer::Exiting { path } => { - playerdata - .movement - .input(path.next_direction(playerdata.position()), false); if path.is_done() { - info!("{player:?} -> leave"); - self.cpackets.push_back(PacketS::Leave { player }); - customers_to_remove.push(player); + info!("{me:?} -> leave"); + BotInput { + leave: true, + ..Default::default() + } + } else { + BotInput { + direction: path.next_direction(pos), + ..Default::default() + } } } } diff --git a/server/bot/src/main.rs b/server/bot/src/main.rs index cf115358..bb30cb68 100644 --- a/server/bot/src/main.rs +++ b/server/bot/src/main.rs @@ -92,6 +92,7 @@ fn main() -> Result<()> { boost, interact, leave, + extra, } = b.state.tick(b.id, &game, dt); if leave { @@ -112,6 +113,7 @@ fn main() -> Result<()> { boost, pos: None, }); + network.queue_out.extend(extra); true }); diff --git a/server/src/data/demands.rs b/server/src/data/demands.rs index 176ca232..3f4b839d 100644 --- a/server/src/data/demands.rs +++ b/server/src/data/demands.rs @@ -15,8 +15,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ -use super::Demand; -use hurrycurry_protocol::{ItemIndex, Recipe, TileIndex}; +use hurrycurry_protocol::{Demand, ItemIndex, Recipe, TileIndex}; use std::collections::{HashMap, HashSet}; pub fn generate_demands( @@ -79,8 +78,8 @@ pub fn generate_demands( .iter() .filter_map(|(i, o, d)| { producable.get(i).map(|cost| Demand { - from: *i, - to: *o, + input: *i, + output: *o, duration: *d, points: *cost as i64, }) diff --git a/server/src/entity/bot.rs b/server/src/entity/bot.rs index 301dbca3..cc67f640 100644 --- a/server/src/entity/bot.rs +++ b/server/src/entity/bot.rs @@ -75,6 +75,7 @@ impl<T: BotAlgo> Entity for BotDriver<T> { boost: input.boost, pos: None, }); + c.packet_in.extend(input.extra); Ok(()) } fn destructor(&mut self, c: EntityContext<'_>) { |