use std::{collections::HashMap, ops::Range, time::Duration}; use bevy::prelude::*; use crate::{ colonist::{Colonist, Path, repath}, items::{Item, ItemAssets, ItemKind, drop_items, stock}, pathfinding::{find_path, find_path_adjacent, is_adjacent, nearest_tile}, rng::SimRng, sim::SimSystems, world::{Plant, TileChanged, TilePos, WorldMap}, worldgen::WorldGenerated, }; pub struct JobsPlugin; impl Plugin for JobsPlugin { fn build(&self, app: &mut App) { app.insert_resource(SimRng::new(0x5EED)) .init_resource::() .init_resource::() .add_message::() .add_message::() .add_systems( Update, ( clear_reservations.run_if(on_message::), handle_orders, ), ) .add_systems(FixedUpdate, choose_jobs.in_set(SimSystems::Decide)) .add_systems(FixedUpdate, run_jobs.in_set(SimSystems::Act)); } } pub const CHOP_TICKS: u32 = 240; pub const WOOD_PER_TREE: u32 = 10; const TREE_SEARCH_TILES: usize = 6000; const WANDER_RADIUS: i32 = 8; const IDLE_SECS: Range = 1.0..4.0; const IDLE_AFTER_ORDER_SECS: f32 = 10.0; #[derive(Component, Default, Debug, Clone, PartialEq)] pub struct CurrentJob(pub Option); #[derive(Debug, Clone, Copy, PartialEq)] pub struct Job { pub task: Task, pub ordered: bool, } #[derive(Debug, Clone, Copy, PartialEq)] pub enum Task { MoveTo(UVec2), Chop { tree: UVec2, progress: u32 }, } #[derive(Component, Debug)] pub struct IdleTimer(Timer); impl Default for IdleTimer { fn default() -> Self { Self(Timer::from_seconds(IDLE_SECS.start, TimerMode::Once)) } } impl IdleTimer { fn restart(&mut self, secs: f32) { self.0.set_duration(Duration::from_secs_f32(secs)); self.0.reset(); } } #[derive(Resource, Default, Debug)] pub struct Reservations(HashMap); impl Reservations { pub fn reserve(&mut self, tile: UVec2, by: Entity) { self.0.insert(tile, by); } pub fn holder(&self, tile: UVec2) -> Option { self.0.get(&tile).copied() } pub fn release_all(&mut self, by: Entity) { self.0.retain(|_, holder| *holder != by); } } #[derive(Resource, Debug)] pub struct ColonyGoals { pub wood: u32, } impl Default for ColonyGoals { fn default() -> Self { Self { wood: 50 } } } #[derive(Message, Debug, Clone, Copy)] pub struct Order { pub colonist: Entity, pub tile: UVec2, } fn clear_reservations(mut reservations: ResMut) { reservations.0.clear(); } fn handle_orders( mut orders: MessageReader, map: Res, mut reservations: ResMut, mut colonists: Query<(&TilePos, &mut Path, &mut CurrentJob)>, ) { for order in orders.read() { let Ok((tile, mut path, mut job)) = colonists.get_mut(order.colonist) else { continue; }; if map.plant(order.tile.as_ivec2()) == Some(Plant::Tree) { reservations.release_all(order.colonist); reservations.reserve(order.tile, order.colonist); path.0.truncate(1); job.0 = Some(Job { task: Task::Chop { tree: order.tile, progress: 0, }, ordered: true, }); } else if let Some(new_path) = repath(&map, tile.0, &path.0, order.tile) { reservations.release_all(order.colonist); path.0 = new_path; job.0 = Some(Job { task: Task::MoveTo(order.tile), ordered: true, }); } } } pub fn wood_wanted(goals: &ColonyGoals, in_stock: u32, trees_being_chopped: u32) -> bool { in_stock + trees_being_chopped * WOOD_PER_TREE < goals.wood } pub fn find_tree( map: &WorldMap, reservations: &Reservations, from: UVec2, ) -> Option<(UVec2, Vec)> { let tree = nearest_tile(map, from, TREE_SEARCH_TILES, |p| { map.plant(p.as_ivec2()) == Some(Plant::Tree) && reservations.holder(p).is_none() })?; let path = find_path_adjacent(map, from, tree)?; Some((tree, path)) } #[allow(clippy::too_many_arguments)] fn choose_jobs( time: Res