| 1 |
use std::{ |
| 2 |
collections::{HashMap, HashSet}, |
| 3 |
ops::Range, |
| 4 |
time::Duration, |
| 5 |
}; |
| 6 |
|
| 7 |
use bevy::prelude::*; |
| 8 |
|
| 9 |
use crate::{ |
| 10 |
colonist::{Colonist, Path, repath}, |
| 11 |
items::{ |
| 12 |
CARRY_CAPACITY, Carrying, Item, ItemAssets, ItemKind, ItemQuery, drop_items, room_for, |
| 13 |
stock, take_items, |
| 14 |
}, |
| 15 |
light::{ |
| 16 |
LIT_BRIGHTNESS, LightMap, Structure, StructureAssets, StructureKind, brightness, dark_soon, |
| 17 |
light_from, spawn_structure, |
| 18 |
}, |
| 19 |
needs::{EXHAUSTED_REST, Rest, SleepContext, done_sleeping, wants_sleep}, |
| 20 |
pathfinding::{find_path, find_path_adjacent, is_adjacent, nearest_tile}, |
| 21 |
rng::SimRng, |
| 22 |
sim::{SimSystems, SimTick, hour_of_day}, |
| 23 |
world::{Plant, Terrain, TileChanged, TilePos, WorldMap}, |
| 24 |
worldgen::WorldGenerated, |
| 25 |
zones::{Zones, can_zone, find_stockpile_site, new_stockpile_tiles, plan_expansion}, |
| 26 |
}; |
| 27 |
|
| 28 |
pub struct JobsPlugin; |
| 29 |
|
| 30 |
impl Plugin for JobsPlugin { |
| 31 |
fn build(&self, app: &mut App) { |
| 32 |
app.insert_resource(SimRng::new(0x5EED)) |
| 33 |
.init_resource::<Reservations>() |
| 34 |
.init_resource::<ColonyGoals>() |
| 35 |
.add_message::<Order>() |
| 36 |
.add_message::<TileChanged>() |
| 37 |
.add_systems( |
| 38 |
Update, |
| 39 |
( |
| 40 |
clear_reservations.run_if(on_message::<WorldGenerated>), |
| 41 |
handle_orders, |
| 42 |
), |
| 43 |
) |
| 44 |
.add_systems(FixedUpdate, choose_jobs.in_set(SimSystems::Decide)) |
| 45 |
.add_systems(FixedUpdate, run_jobs.in_set(SimSystems::Act)); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
pub const EXPAND_WORK: u32 = 240; |
| 50 |
pub const CAMPFIRE_MIN_FUEL: u32 = 3 * crate::sim::TICKS_PER_HOUR as u32; |
| 51 |
pub const HOME_RADIUS: f32 = 10.0; |
| 52 |
const LIT_WORK_RATE: u32 = 2; |
| 53 |
const DARK_WORK_RATE: u32 = 1; |
| 54 |
pub const EXPANSION_TILES: usize = 8; |
| 55 |
pub const MIN_EMPTY_STOCKPILE_TILES: usize = 3; |
| 56 |
const SEARCH_TILES: usize = 15000; |
| 57 |
const WANDER_RADIUS: i32 = 8; |
| 58 |
const IDLE_SECS: Range<f32> = 1.0..4.0; |
| 59 |
const IDLE_AFTER_ORDER_SECS: f32 = 10.0; |
| 60 |
const WORK_PAUSE_SECS: Range<f32> = 0.2..0.8; |
| 61 |
const NEIGHBORS: [IVec2; 8] = [ |
| 62 |
IVec2::new(1, 0), |
| 63 |
IVec2::new(-1, 0), |
| 64 |
IVec2::new(0, 1), |
| 65 |
IVec2::new(0, -1), |
| 66 |
IVec2::new(1, 1), |
| 67 |
IVec2::new(1, -1), |
| 68 |
IVec2::new(-1, 1), |
| 69 |
IVec2::new(-1, -1), |
| 70 |
]; |
| 71 |
|
| 72 |
#[derive(Component, Default, Debug, Clone, PartialEq)] |
| 73 |
pub struct CurrentJob(pub Option<Job>); |
| 74 |
|
| 75 |
#[derive(Debug, Clone, Copy, PartialEq)] |
| 76 |
pub struct Job { |
| 77 |
pub task: Task, |
| 78 |
pub ordered: bool, |
| 79 |
} |
| 80 |
|
| 81 |
#[derive(Debug, Clone, Copy, PartialEq)] |
| 82 |
pub enum Task { |
| 83 |
MoveTo(UVec2), |
| 84 |
Work { |
| 85 |
site: UVec2, |
| 86 |
kind: WorkKind, |
| 87 |
progress: u32, |
| 88 |
}, |
| 89 |
Haul { |
| 90 |
pickup: Option<UVec2>, |
| 91 |
to: UVec2, |
| 92 |
}, |
| 93 |
ExpandStockpile { |
| 94 |
site: UVec2, |
| 95 |
progress: u32, |
| 96 |
}, |
| 97 |
Build { |
| 98 |
site: UVec2, |
| 99 |
kind: StructureKind, |
| 100 |
pickup: Option<UVec2>, |
| 101 |
progress: u32, |
| 102 |
}, |
| 103 |
Sleep { |
| 104 |
spot: UVec2, |
| 105 |
}, |
| 106 |
} |
| 107 |
|
| 108 |
pub fn build_work(kind: StructureKind) -> u32 { |
| 109 |
match kind { |
| 110 |
StructureKind::Campfire => 360, |
| 111 |
StructureKind::Torch => 120, |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
pub fn work_rate(light: &LightMap, tile: UVec2) -> u32 { |
| 116 |
if light.is_lit(tile) { |
| 117 |
LIT_WORK_RATE |
| 118 |
} else { |
| 119 |
DARK_WORK_RATE |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 124 |
pub enum WorkKind { |
| 125 |
Chop, |
| 126 |
Mine, |
| 127 |
} |
| 128 |
|
| 129 |
impl WorkKind { |
| 130 |
pub fn work(self) -> u32 { |
| 131 |
match self { |
| 132 |
WorkKind::Chop => 480, |
| 133 |
WorkKind::Mine => 720, |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
fn complete(self, map: &mut WorldMap, site: UVec2) { |
| 138 |
match self { |
| 139 |
WorkKind::Chop => map.set_plant(site, None), |
| 140 |
WorkKind::Mine => map.set(site, Terrain::Gravel), |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
pub struct SiteWork { |
| 146 |
pub kind: WorkKind, |
| 147 |
pub item: ItemKind, |
| 148 |
pub count: u32, |
| 149 |
} |
| 150 |
|
| 151 |
pub fn site_work(map: &WorldMap, site: UVec2) -> Option<SiteWork> { |
| 152 |
let p = site.as_ivec2(); |
| 153 |
if map.plant(p) == Some(Plant::Tree) { |
| 154 |
return Some(SiteWork { |
| 155 |
kind: WorkKind::Chop, |
| 156 |
item: ItemKind::Wood, |
| 157 |
count: 10, |
| 158 |
}); |
| 159 |
} |
| 160 |
let (item, count) = match map.get(p)? { |
| 161 |
Terrain::Stone => (ItemKind::Stone, 10), |
| 162 |
Terrain::IronOre => (ItemKind::Iron, 10), |
| 163 |
Terrain::GoldOre => (ItemKind::Gold, 5), |
| 164 |
_ => return None, |
| 165 |
}; |
| 166 |
Some(SiteWork { |
| 167 |
kind: WorkKind::Mine, |
| 168 |
item, |
| 169 |
count, |
| 170 |
}) |
| 171 |
} |
| 172 |
|
| 173 |
#[derive(Component, Debug)] |
| 174 |
pub struct IdleTimer(Timer); |
| 175 |
|
| 176 |
impl Default for IdleTimer { |
| 177 |
fn default() -> Self { |
| 178 |
Self(Timer::from_seconds(IDLE_SECS.start, TimerMode::Once)) |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
impl IdleTimer { |
| 183 |
fn restart(&mut self, secs: f32) { |
| 184 |
self.0.set_duration(Duration::from_secs_f32(secs)); |
| 185 |
self.0.reset(); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 190 |
pub enum Target { |
| 191 |
Site(UVec2), |
| 192 |
Pickup(UVec2), |
| 193 |
Storage(UVec2), |
| 194 |
StockpileExpansion, |
| 195 |
SleepSpot(UVec2), |
| 196 |
} |
| 197 |
|
| 198 |
#[derive(Resource, Default, Debug)] |
| 199 |
pub struct Reservations(HashMap<Target, Entity>); |
| 200 |
|
| 201 |
impl Reservations { |
| 202 |
pub fn reserve(&mut self, target: Target, by: Entity) { |
| 203 |
self.0.insert(target, by); |
| 204 |
} |
| 205 |
|
| 206 |
pub fn holder(&self, target: Target) -> Option<Entity> { |
| 207 |
self.0.get(&target).copied() |
| 208 |
} |
| 209 |
|
| 210 |
pub fn is_free(&self, target: Target) -> bool { |
| 211 |
self.holder(target).is_none() |
| 212 |
} |
| 213 |
|
| 214 |
pub fn release(&mut self, target: Target) { |
| 215 |
self.0.remove(&target); |
| 216 |
} |
| 217 |
|
| 218 |
pub fn release_all(&mut self, by: Entity) { |
| 219 |
self.0.retain(|_, holder| *holder != by); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
#[derive(Resource, Debug, Clone)] |
| 224 |
pub struct ColonyGoals(pub HashMap<ItemKind, u32>); |
| 225 |
|
| 226 |
impl Default for ColonyGoals { |
| 227 |
fn default() -> Self { |
| 228 |
Self(HashMap::from([ |
| 229 |
(ItemKind::Wood, 50), |
| 230 |
(ItemKind::Stone, 40), |
| 231 |
(ItemKind::Iron, 20), |
| 232 |
(ItemKind::Gold, 10), |
| 233 |
])) |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
impl ColonyGoals { |
| 238 |
pub fn goal(&self, kind: ItemKind) -> u32 { |
| 239 |
self.0.get(&kind).copied().unwrap_or(0) |
| 240 |
} |
| 241 |
|
| 242 |
pub fn wanted(&self, kind: ItemKind, in_stock: u32, on_the_way: u32) -> bool { |
| 243 |
in_stock + on_the_way < self.goal(kind) |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
#[derive(Message, Debug, Clone, Copy)] |
| 248 |
pub struct Order { |
| 249 |
pub colonist: Entity, |
| 250 |
pub tile: UVec2, |
| 251 |
} |
| 252 |
|
| 253 |
fn clear_reservations(mut reservations: ResMut<Reservations>) { |
| 254 |
reservations.0.clear(); |
| 255 |
} |
| 256 |
|
| 257 |
fn handle_orders( |
| 258 |
mut orders: MessageReader<Order>, |
| 259 |
map: Res<WorldMap>, |
| 260 |
zones: Res<Zones>, |
| 261 |
items: Query<(&Item, &TilePos)>, |
| 262 |
mut reservations: ResMut<Reservations>, |
| 263 |
mut colonists: Query<(&TilePos, &mut Path, &mut CurrentJob)>, |
| 264 |
) { |
| 265 |
for order in orders.read() { |
| 266 |
let Ok((tile, mut path, mut job)) = colonists.get_mut(order.colonist) else { |
| 267 |
continue; |
| 268 |
}; |
| 269 |
let loose_item = items |
| 270 |
.iter() |
| 271 |
.find(|(_, pos)| pos.0 == order.tile && !zones.is_stockpile(pos.0)); |
| 272 |
let task = if let Some(work) = site_work(&map, order.tile) { |
| 273 |
Some(Task::Work { |
| 274 |
site: order.tile, |
| 275 |
kind: work.kind, |
| 276 |
progress: 0, |
| 277 |
}) |
| 278 |
} else if let Some((item, _)) = loose_item { |
| 279 |
find_storage(&map, &zones, &reservations, &items, order.tile, item.kind).map(|to| { |
| 280 |
Task::Haul { |
| 281 |
pickup: Some(order.tile), |
| 282 |
to, |
| 283 |
} |
| 284 |
}) |
| 285 |
} else { |
| 286 |
None |
| 287 |
}; |
| 288 |
if let Some(task) = task { |
| 289 |
reservations.release_all(order.colonist); |
| 290 |
reserve_task(&mut reservations, task, order.colonist); |
| 291 |
path.0.truncate(1); |
| 292 |
job.0 = Some(Job { |
| 293 |
task, |
| 294 |
ordered: true, |
| 295 |
}); |
| 296 |
} else if let Some(new_path) = repath(&map, tile.0, &path.0, order.tile) { |
| 297 |
reservations.release_all(order.colonist); |
| 298 |
path.0 = new_path; |
| 299 |
job.0 = Some(Job { |
| 300 |
task: Task::MoveTo(order.tile), |
| 301 |
ordered: true, |
| 302 |
}); |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
fn reserve_task(reservations: &mut Reservations, task: Task, by: Entity) { |
| 308 |
match task { |
| 309 |
Task::MoveTo(_) => {} |
| 310 |
Task::Work { site, .. } => reservations.reserve(Target::Site(site), by), |
| 311 |
Task::Haul { pickup, to } => { |
| 312 |
if let Some(pickup) = pickup { |
| 313 |
reservations.reserve(Target::Pickup(pickup), by); |
| 314 |
} |
| 315 |
reservations.reserve(Target::Storage(to), by); |
| 316 |
} |
| 317 |
Task::ExpandStockpile { .. } => reservations.reserve(Target::StockpileExpansion, by), |
| 318 |
Task::Sleep { spot } => reservations.reserve(Target::SleepSpot(spot), by), |
| 319 |
Task::Build { site, pickup, .. } => { |
| 320 |
reservations.reserve(Target::Site(site), by); |
| 321 |
if let Some(pickup) = pickup { |
| 322 |
reservations.reserve(Target::Pickup(pickup), by); |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
pub fn find_work( |
| 329 |
map: &WorldMap, |
| 330 |
reservations: &Reservations, |
| 331 |
from: UVec2, |
| 332 |
wanted: impl Fn(ItemKind) -> bool, |
| 333 |
) -> Option<Task> { |
| 334 |
let mut found = None; |
| 335 |
nearest_tile(map, from, SEARCH_TILES, |stand| { |
| 336 |
found = NEIGHBORS.iter().find_map(|&d| { |
| 337 |
let site = stand.as_ivec2() + d; |
| 338 |
if site.x < 0 || site.y < 0 { |
| 339 |
return None; |
| 340 |
} |
| 341 |
let site = site.as_uvec2(); |
| 342 |
let work = site_work(map, site)?; |
| 343 |
(wanted(work.item) && reservations.is_free(Target::Site(site))).then_some(Task::Work { |
| 344 |
site, |
| 345 |
kind: work.kind, |
| 346 |
progress: 0, |
| 347 |
}) |
| 348 |
}); |
| 349 |
found.is_some() |
| 350 |
})?; |
| 351 |
found |
| 352 |
} |
| 353 |
|
| 354 |
pub fn find_storage<'a>( |
| 355 |
map: &WorldMap, |
| 356 |
zones: &Zones, |
| 357 |
reservations: &Reservations, |
| 358 |
items: impl IntoIterator<Item = (&'a Item, &'a TilePos)> + Clone, |
| 359 |
from: UVec2, |
| 360 |
kind: ItemKind, |
| 361 |
) -> Option<UVec2> { |
| 362 |
nearest_tile(map, from, SEARCH_TILES, |p| { |
| 363 |
zones.is_stockpile(p) |
| 364 |
&& reservations.is_free(Target::Storage(p)) |
| 365 |
&& room_for(items.clone().into_iter(), p, kind) > 0 |
| 366 |
}) |
| 367 |
} |
| 368 |
|
| 369 |
pub fn find_haul( |
| 370 |
map: &WorldMap, |
| 371 |
zones: &Zones, |
| 372 |
reservations: &Reservations, |
| 373 |
items: &[(Item, TilePos)], |
| 374 |
from: UVec2, |
| 375 |
) -> Option<Task> { |
| 376 |
let loose: HashSet<UVec2> = items |
| 377 |
.iter() |
| 378 |
.map(|(_, pos)| pos.0) |
| 379 |
.filter(|&p| !zones.is_stockpile(p) && reservations.is_free(Target::Pickup(p))) |
| 380 |
.collect(); |
| 381 |
if loose.is_empty() { |
| 382 |
return None; |
| 383 |
} |
| 384 |
let pickup = nearest_tile(map, from, SEARCH_TILES, |p| loose.contains(&p))?; |
| 385 |
let (item, _) = items.iter().find(|(_, pos)| pos.0 == pickup)?; |
| 386 |
let refs = items.iter().map(|(i, p)| (i, p)); |
| 387 |
let to = find_storage(map, zones, reservations, refs, pickup, item.kind)?; |
| 388 |
Some(Task::Haul { |
| 389 |
pickup: Some(pickup), |
| 390 |
to, |
| 391 |
}) |
| 392 |
} |
| 393 |
|
| 394 |
pub fn stockpile_too_full(zones: &Zones, items: &[(Item, TilePos)]) -> bool { |
| 395 |
let occupied: HashSet<UVec2> = items.iter().map(|(_, pos)| pos.0).collect(); |
| 396 |
zones.has_stockpile() && zones.empty_stockpile_tiles(&occupied) < MIN_EMPTY_STOCKPILE_TILES |
| 397 |
} |
| 398 |
|
| 399 |
pub fn find_expansion( |
| 400 |
map: &WorldMap, |
| 401 |
zones: &Zones, |
| 402 |
reservations: &Reservations, |
| 403 |
items: &[(Item, TilePos)], |
| 404 |
) -> Option<Task> { |
| 405 |
if !reservations.is_free(Target::StockpileExpansion) { |
| 406 |
return None; |
| 407 |
} |
| 408 |
let site = if zones.has_stockpile() { |
| 409 |
if !stockpile_too_full(zones, items) { |
| 410 |
return None; |
| 411 |
} |
| 412 |
*plan_expansion(map, zones, EXPANSION_TILES).first()? |
| 413 |
} else { |
| 414 |
find_stockpile_site(map, central_item_tile(items)?)? |
| 415 |
}; |
| 416 |
Some(Task::ExpandStockpile { site, progress: 0 }) |
| 417 |
} |
| 418 |
|
| 419 |
fn central_item_tile(items: &[(Item, TilePos)]) -> Option<UVec2> { |
| 420 |
let tiles: Vec<Vec2> = items.iter().map(|(_, pos)| pos.0.as_vec2()).collect(); |
| 421 |
let centroid = tiles.iter().sum::<Vec2>() / tiles.len().max(1) as f32; |
| 422 |
items.iter().map(|(_, pos)| pos.0).min_by(|a, b| { |
| 423 |
a.as_vec2() |
| 424 |
.distance_squared(centroid) |
| 425 |
.total_cmp(&b.as_vec2().distance_squared(centroid)) |
| 426 |
}) |
| 427 |
} |
| 428 |
|
| 429 |
pub struct LightingContext<'a> { |
| 430 |
pub map: &'a WorldMap, |
| 431 |
pub zones: &'a Zones, |
| 432 |
pub reservations: &'a Reservations, |
| 433 |
pub items: &'a [(Item, TilePos)], |
| 434 |
pub fires: &'a [(UVec2, StructureKind, u32)], |
| 435 |
pub planned: &'a [(UVec2, StructureKind)], |
| 436 |
} |
| 437 |
|
| 438 |
impl LightingContext<'_> { |
| 439 |
fn buildable(&self, tile: UVec2) -> bool { |
| 440 |
can_zone(self.map, tile.as_ivec2()) |
| 441 |
&& !self.zones.is_stockpile(tile) |
| 442 |
&& self.map.plant(tile.as_ivec2()).is_none() |
| 443 |
&& !self.fires.iter().any(|(pos, ..)| *pos == tile) |
| 444 |
&& !self.items.iter().any(|(_, pos)| pos.0 == tile) |
| 445 |
&& self.reservations.is_free(Target::Site(tile)) |
| 446 |
} |
| 447 |
|
| 448 |
fn lights(&self) -> impl Iterator<Item = (UVec2, StructureKind)> + Clone + '_ { |
| 449 |
self.fires |
| 450 |
.iter() |
| 451 |
.map(|&(pos, kind, _)| (pos, kind)) |
| 452 |
.chain(self.planned.iter().copied()) |
| 453 |
} |
| 454 |
|
| 455 |
fn campfire_needed(&self, home: UVec2) -> bool { |
| 456 |
let burning = self.fires.iter().any(|&(pos, kind, fuel)| { |
| 457 |
kind == StructureKind::Campfire |
| 458 |
&& fuel >= CAMPFIRE_MIN_FUEL |
| 459 |
&& pos.as_vec2().distance(home.as_vec2()) <= HOME_RADIUS |
| 460 |
}); |
| 461 |
let planned = self |
| 462 |
.planned |
| 463 |
.iter() |
| 464 |
.any(|(_, kind)| *kind == StructureKind::Campfire); |
| 465 |
!burning && !planned |
| 466 |
} |
| 467 |
|
| 468 |
fn darkest_stockpile_tile(&self, home: UVec2) -> Option<UVec2> { |
| 469 |
self.zones |
| 470 |
.stockpile_tiles() |
| 471 |
.filter(|&t| brightness(light_from(t, self.lights())) < LIT_BRIGHTNESS) |
| 472 |
.min_by(|a, b| { |
| 473 |
a.as_vec2() |
| 474 |
.distance_squared(home.as_vec2()) |
| 475 |
.total_cmp(&b.as_vec2().distance_squared(home.as_vec2())) |
| 476 |
.then((a.y, a.x).cmp(&(b.y, b.x))) |
| 477 |
}) |
| 478 |
} |
| 479 |
|
| 480 |
fn site_near(&self, target: UVec2, max_distance: f32) -> Option<UVec2> { |
| 481 |
nearest_tile(self.map, target, SEARCH_TILES, |p| self.buildable(p)) |
| 482 |
.filter(|site| site.as_vec2().distance(target.as_vec2()) <= max_distance) |
| 483 |
} |
| 484 |
|
| 485 |
fn wood_source(&self, from: UVec2, amount: u32) -> Option<UVec2> { |
| 486 |
nearest_tile(self.map, from, SEARCH_TILES, |p| { |
| 487 |
self.reservations.is_free(Target::Pickup(p)) |
| 488 |
&& self.items.iter().any(|(item, pos)| { |
| 489 |
pos.0 == p && item.kind == ItemKind::Wood && item.count >= amount |
| 490 |
}) |
| 491 |
}) |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
pub fn find_lighting(ctx: &LightingContext, from: UVec2) -> Option<Task> { |
| 496 |
let home = ctx.zones.home(from); |
| 497 |
let (kind, site) = if ctx.campfire_needed(home) { |
| 498 |
(StructureKind::Campfire, ctx.site_near(home, HOME_RADIUS)?) |
| 499 |
} else { |
| 500 |
let dark = ctx.darkest_stockpile_tile(home)?; |
| 501 |
let reach = StructureKind::Torch.radius() * 0.6; |
| 502 |
(StructureKind::Torch, ctx.site_near(dark, reach)?) |
| 503 |
}; |
| 504 |
let pickup = ctx.wood_source(from, kind.wood_cost())?; |
| 505 |
Some(Task::Build { |
| 506 |
site, |
| 507 |
kind, |
| 508 |
pickup: Some(pickup), |
| 509 |
progress: 0, |
| 510 |
}) |
| 511 |
} |
| 512 |
|
| 513 |
fn on_the_way(map: &WorldMap, jobs: impl Iterator<Item = Option<Job>>) -> HashMap<ItemKind, u32> { |
| 514 |
let mut totals = HashMap::new(); |
| 515 |
for job in jobs.flatten() { |
| 516 |
if let Task::Work { site, .. } = job.task |
| 517 |
&& let Some(work) = site_work(map, site) |
| 518 |
{ |
| 519 |
*totals.entry(work.item).or_insert(0) += work.count; |
| 520 |
} |
| 521 |
} |
| 522 |
totals |
| 523 |
} |
| 524 |
|
| 525 |
#[allow(clippy::too_many_arguments)] |
| 526 |
fn choose_jobs( |
| 527 |
mut commands: Commands, |
| 528 |
time: Res<Time>, |
| 529 |
map: Res<WorldMap>, |
| 530 |
zones: Res<Zones>, |
| 531 |
goals: Res<ColonyGoals>, |
| 532 |
item_assets: Res<ItemAssets>, |
| 533 |
tick: Res<SimTick>, |
| 534 |
mut rng: ResMut<SimRng>, |
| 535 |
mut reservations: ResMut<Reservations>, |
| 536 |
mut items: ItemQuery, |
| 537 |
structures: Query<(&TilePos, &Structure), Without<Colonist>>, |
| 538 |
mut colonists: Query< |
| 539 |
( |
| 540 |
Entity, |
| 541 |
&TilePos, |
| 542 |
&mut Path, |
| 543 |
&mut CurrentJob, |
| 544 |
&mut IdleTimer, |
| 545 |
&mut Carrying, |
| 546 |
&Rest, |
| 547 |
), |
| 548 |
With<Colonist>, |
| 549 |
>, |
| 550 |
) { |
| 551 |
let fires: Vec<(UVec2, StructureKind, u32)> = structures |
| 552 |
.iter() |
| 553 |
.map(|(pos, s)| (pos.0, s.kind, s.fuel)) |
| 554 |
.collect(); |
| 555 |
let mut planned: Vec<(UVec2, StructureKind)> = colonists |
| 556 |
.iter() |
| 557 |
.filter_map(|c| match c.3.0?.task { |
| 558 |
Task::Build { site, kind, .. } => Some((site, kind)), |
| 559 |
_ => None, |
| 560 |
}) |
| 561 |
.collect(); |
| 562 |
let dark = dark_soon(*tick); |
| 563 |
let carried: Vec<Item> = colonists.iter().filter_map(|c| c.5.0).collect(); |
| 564 |
let stocks: HashMap<ItemKind, u32> = ItemKind::ALL |
| 565 |
.iter() |
| 566 |
.map(|&kind| { |
| 567 |
let all = items.iter().map(|(_, item, _)| item).chain(carried.iter()); |
| 568 |
(kind, stock(all, kind)) |
| 569 |
}) |
| 570 |
.collect(); |
| 571 |
let mut pending = on_the_way(&map, colonists.iter().map(|c| c.3.0)); |
| 572 |
let hour = hour_of_day(*tick); |
| 573 |
for (entity, tile, mut path, mut job, mut idle, mut carrying, rest) in &mut colonists { |
| 574 |
let exhausted = rest.0 < EXHAUSTED_REST; |
| 575 |
if exhausted |
| 576 |
&& job |
| 577 |
.0 |
| 578 |
.is_some_and(|j| !j.ordered && !matches!(j.task, Task::Sleep { .. })) |
| 579 |
{ |
| 580 |
reservations.release_all(entity); |
| 581 |
job.0 = None; |
| 582 |
path.0.truncate(1); |
| 583 |
} |
| 584 |
let rested = idle.0.tick(time.delta()).is_finished(); |
| 585 |
if job.0.is_some() || !path.0.is_empty() || !(rested || exhausted) { |
| 586 |
continue; |
| 587 |
} |
| 588 |
let snapshot: Vec<(Item, TilePos)> = |
| 589 |
items.iter().map(|(_, item, pos)| (*item, *pos)).collect(); |
| 590 |
let refs = snapshot.iter().map(|(i, p)| (i, p)); |
| 591 |
let wanted = |kind: ItemKind| { |
| 592 |
goals.wanted( |
| 593 |
kind, |
| 594 |
stocks[&kind], |
| 595 |
pending.get(&kind).copied().unwrap_or(0), |
| 596 |
) |
| 597 |
}; |
| 598 |
let sleep = SleepContext { |
| 599 |
map: &map, |
| 600 |
zones: &zones, |
| 601 |
reservations: &reservations, |
| 602 |
items: &snapshot, |
| 603 |
fires: &fires, |
| 604 |
}; |
| 605 |
let task = if wants_sleep(rest.0, hour) { |
| 606 |
Some(Task::Sleep { |
| 607 |
spot: sleep.spot(tile.0), |
| 608 |
}) |
| 609 |
} else if let Some(load) = carrying.0 { |
| 610 |
let storage = find_storage(&map, &zones, &reservations, refs, tile.0, load.kind); |
| 611 |
if storage.is_none() { |
| 612 |
drop_items( |
| 613 |
&mut commands, |
| 614 |
&mut items, |
| 615 |
&item_assets, |
| 616 |
tile.0, |
| 617 |
load.kind, |
| 618 |
load.count, |
| 619 |
); |
| 620 |
carrying.0 = None; |
| 621 |
} |
| 622 |
storage.map(|to| Task::Haul { pickup: None, to }) |
| 623 |
} else { |
| 624 |
let lighting = LightingContext { |
| 625 |
map: &map, |
| 626 |
zones: &zones, |
| 627 |
reservations: &reservations, |
| 628 |
items: &snapshot, |
| 629 |
fires: &fires, |
| 630 |
planned: &planned, |
| 631 |
}; |
| 632 |
find_expansion(&map, &zones, &reservations, &snapshot) |
| 633 |
.or_else(|| dark.then(|| find_lighting(&lighting, tile.0)).flatten()) |
| 634 |
.or_else(|| find_work(&map, &reservations, tile.0, wanted)) |
| 635 |
.or_else(|| find_haul(&map, &zones, &reservations, &snapshot, tile.0)) |
| 636 |
}; |
| 637 |
if let Some(task) = task { |
| 638 |
if let Task::Work { site, .. } = task |
| 639 |
&& let Some(work) = site_work(&map, site) |
| 640 |
{ |
| 641 |
*pending.entry(work.item).or_insert(0) += work.count; |
| 642 |
} |
| 643 |
if let Task::Build { site, kind, .. } = task { |
| 644 |
planned.push((site, kind)); |
| 645 |
} |
| 646 |
reserve_task(&mut reservations, task, entity); |
| 647 |
job.0 = Some(Job { |
| 648 |
task, |
| 649 |
ordered: false, |
| 650 |
}); |
| 651 |
continue; |
| 652 |
} |
| 653 |
let goal = tile.0.as_ivec2() |
| 654 |
+ IVec2::new( |
| 655 |
rng.range_i32(-WANDER_RADIUS..WANDER_RADIUS + 1), |
| 656 |
rng.range_i32(-WANDER_RADIUS..WANDER_RADIUS + 1), |
| 657 |
); |
| 658 |
if map.in_bounds(goal) |
| 659 |
&& let Some(wander_path) = find_path(&map, tile.0, goal.as_uvec2()) |
| 660 |
{ |
| 661 |
path.0 = wander_path.into(); |
| 662 |
idle.restart(rng.range_f32(IDLE_SECS)); |
| 663 |
} |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
enum Step { |
| 668 |
Continue, |
| 669 |
Done, |
| 670 |
} |
| 671 |
|
| 672 |
fn walk_to(map: &WorldMap, from: UVec2, to: UVec2, path: &mut Path) -> Step { |
| 673 |
match find_path(map, from, to) { |
| 674 |
Some(p) if !p.is_empty() => { |
| 675 |
path.0 = p.into(); |
| 676 |
Step::Continue |
| 677 |
} |
| 678 |
_ => Step::Done, |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
#[allow(clippy::too_many_arguments)] |
| 683 |
fn run_jobs( |
| 684 |
mut commands: Commands, |
| 685 |
mut map: ResMut<WorldMap>, |
| 686 |
mut zones: ResMut<Zones>, |
| 687 |
mut reservations: ResMut<Reservations>, |
| 688 |
mut rng: ResMut<SimRng>, |
| 689 |
item_assets: Res<ItemAssets>, |
| 690 |
structure_assets: Res<StructureAssets>, |
| 691 |
light: Res<LightMap>, |
| 692 |
tick: Res<SimTick>, |
| 693 |
mut items: ItemQuery, |
| 694 |
structures: Query<&TilePos, (With<Structure>, Without<Colonist>)>, |
| 695 |
mut changed: MessageWriter<TileChanged>, |
| 696 |
mut colonists: Query< |
| 697 |
( |
| 698 |
Entity, |
| 699 |
&TilePos, |
| 700 |
&mut Path, |
| 701 |
&mut CurrentJob, |
| 702 |
&mut IdleTimer, |
| 703 |
&mut Carrying, |
| 704 |
&Rest, |
| 705 |
), |
| 706 |
With<Colonist>, |
| 707 |
>, |
| 708 |
) { |
| 709 |
let hour = hour_of_day(*tick); |
| 710 |
for (entity, tile, mut path, mut current, mut idle, mut carrying, rest) in &mut colonists { |
| 711 |
let Some(mut job) = current.0 else { |
| 712 |
continue; |
| 713 |
}; |
| 714 |
if !path.0.is_empty() { |
| 715 |
continue; |
| 716 |
} |
| 717 |
let step = match &mut job.task { |
| 718 |
Task::MoveTo(_) => Step::Done, |
| 719 |
Task::Sleep { spot } => { |
| 720 |
if reservations.holder(Target::SleepSpot(*spot)) != Some(entity) { |
| 721 |
Step::Done |
| 722 |
} else if tile.0 != *spot { |
| 723 |
let step = walk_to(&map, tile.0, *spot, &mut path); |
| 724 |
if matches!(step, Step::Done) { |
| 725 |
reservations.release(Target::SleepSpot(*spot)); |
| 726 |
*spot = tile.0; |
| 727 |
reservations.reserve(Target::SleepSpot(*spot), entity); |
| 728 |
} |
| 729 |
Step::Continue |
| 730 |
} else if done_sleeping(rest.0, hour) { |
| 731 |
Step::Done |
| 732 |
} else { |
| 733 |
Step::Continue |
| 734 |
} |
| 735 |
} |
| 736 |
Task::Work { |
| 737 |
site, |
| 738 |
kind, |
| 739 |
progress, |
| 740 |
} => { |
| 741 |
let (site, kind) = (*site, *kind); |
| 742 |
let work = site_work(&map, site).filter(|w| w.kind == kind); |
| 743 |
let ours = reservations.holder(Target::Site(site)) == Some(entity); |
| 744 |
match work { |
| 745 |
Some(work) if ours => { |
| 746 |
if !is_adjacent(tile.0.as_ivec2(), site.as_ivec2()) { |
| 747 |
match find_path_adjacent(&map, tile.0, site) { |
| 748 |
Some(p) if !p.is_empty() => { |
| 749 |
path.0 = p.into(); |
| 750 |
Step::Continue |
| 751 |
} |
| 752 |
_ => Step::Done, |
| 753 |
} |
| 754 |
} else { |
| 755 |
*progress += work_rate(&light, tile.0); |
| 756 |
if *progress < kind.work() { |
| 757 |
Step::Continue |
| 758 |
} else { |
| 759 |
kind.complete(&mut map, site); |
| 760 |
changed.write(TileChanged(site)); |
| 761 |
drop_items( |
| 762 |
&mut commands, |
| 763 |
&mut items, |
| 764 |
&item_assets, |
| 765 |
site, |
| 766 |
work.item, |
| 767 |
work.count, |
| 768 |
); |
| 769 |
Step::Done |
| 770 |
} |
| 771 |
} |
| 772 |
} |
| 773 |
_ => Step::Done, |
| 774 |
} |
| 775 |
} |
| 776 |
Task::ExpandStockpile { site, progress } => { |
| 777 |
if tile.0 != *site { |
| 778 |
walk_to(&map, tile.0, *site, &mut path) |
| 779 |
} else { |
| 780 |
*progress += work_rate(&light, tile.0); |
| 781 |
if *progress >= EXPAND_WORK { |
| 782 |
if zones.has_stockpile() { |
| 783 |
let tiles = plan_expansion(&map, &zones, EXPANSION_TILES); |
| 784 |
zones.add_tiles(&map, tiles); |
| 785 |
} else { |
| 786 |
zones.add_tiles(&map, new_stockpile_tiles(*site)); |
| 787 |
} |
| 788 |
Step::Done |
| 789 |
} else { |
| 790 |
Step::Continue |
| 791 |
} |
| 792 |
} |
| 793 |
} |
| 794 |
Task::Build { |
| 795 |
site, |
| 796 |
kind, |
| 797 |
pickup, |
| 798 |
progress, |
| 799 |
} => { |
| 800 |
let (site, kind) = (*site, *kind); |
| 801 |
let ours = reservations.holder(Target::Site(site)) == Some(entity); |
| 802 |
let blocked = map.plant(site.as_ivec2()).is_some() |
| 803 |
|| structures.iter().any(|pos| pos.0 == site); |
| 804 |
let cost = kind.wood_cost(); |
| 805 |
if !ours || blocked { |
| 806 |
Step::Done |
| 807 |
} else if let Some(from) = *pickup { |
| 808 |
if reservations.holder(Target::Pickup(from)) != Some(entity) { |
| 809 |
Step::Done |
| 810 |
} else if tile.0 != from { |
| 811 |
walk_to(&map, tile.0, from, &mut path) |
| 812 |
} else { |
| 813 |
let load = take_items(&mut commands, &mut items, from, cost) |
| 814 |
.filter(|load| load.kind == ItemKind::Wood && load.count > 0); |
| 815 |
carrying.0 = load; |
| 816 |
match load { |
| 817 |
Some(load) if load.count >= cost => { |
| 818 |
reservations.release(Target::Pickup(from)); |
| 819 |
*pickup = None; |
| 820 |
Step::Continue |
| 821 |
} |
| 822 |
_ => Step::Done, |
| 823 |
} |
| 824 |
} |
| 825 |
} else if !carrying |
| 826 |
.0 |
| 827 |
.is_some_and(|load| load.kind == ItemKind::Wood && load.count >= cost) |
| 828 |
{ |
| 829 |
Step::Done |
| 830 |
} else if !is_adjacent(tile.0.as_ivec2(), site.as_ivec2()) { |
| 831 |
match find_path_adjacent(&map, tile.0, site) { |
| 832 |
Some(p) if !p.is_empty() => { |
| 833 |
path.0 = p.into(); |
| 834 |
Step::Continue |
| 835 |
} |
| 836 |
_ => Step::Done, |
| 837 |
} |
| 838 |
} else { |
| 839 |
*progress += work_rate(&light, tile.0); |
| 840 |
if *progress < build_work(kind) { |
| 841 |
Step::Continue |
| 842 |
} else { |
| 843 |
carrying.0 = carrying.0.and_then(|load| { |
| 844 |
(load.count > cost).then_some(Item { |
| 845 |
count: load.count - cost, |
| 846 |
..load |
| 847 |
}) |
| 848 |
}); |
| 849 |
spawn_structure(&mut commands, &structure_assets, site, kind); |
| 850 |
Step::Done |
| 851 |
} |
| 852 |
} |
| 853 |
} |
| 854 |
Task::Haul { pickup, to } => { |
| 855 |
let to = *to; |
| 856 |
let destination_valid = zones.is_stockpile(to) |
| 857 |
&& reservations.holder(Target::Storage(to)) == Some(entity); |
| 858 |
if !destination_valid { |
| 859 |
Step::Done |
| 860 |
} else if let Some(from) = *pickup { |
| 861 |
if reservations.holder(Target::Pickup(from)) != Some(entity) { |
| 862 |
Step::Done |
| 863 |
} else if tile.0 != from { |
| 864 |
walk_to(&map, tile.0, from, &mut path) |
| 865 |
} else { |
| 866 |
let kind = items |
| 867 |
.iter() |
| 868 |
.find(|(_, _, pos)| pos.0 == from) |
| 869 |
.map(|(_, item, _)| item.kind); |
| 870 |
let room = kind.map_or(0, |kind| { |
| 871 |
room_for(items.iter().map(|(_, i, p)| (i, p)), to, kind) |
| 872 |
}); |
| 873 |
match take_items(&mut commands, &mut items, from, CARRY_CAPACITY.min(room)) |
| 874 |
.filter(|load| load.count > 0) |
| 875 |
{ |
| 876 |
Some(load) => { |
| 877 |
carrying.0 = Some(load); |
| 878 |
reservations.release(Target::Pickup(from)); |
| 879 |
*pickup = None; |
| 880 |
Step::Continue |
| 881 |
} |
| 882 |
None => Step::Done, |
| 883 |
} |
| 884 |
} |
| 885 |
} else if let Some(load) = carrying.0 { |
| 886 |
if tile.0 != to { |
| 887 |
walk_to(&map, tile.0, to, &mut path) |
| 888 |
} else { |
| 889 |
let room = room_for(items.iter().map(|(_, i, p)| (i, p)), to, load.kind); |
| 890 |
let dropped = load.count.min(room); |
| 891 |
if dropped > 0 { |
| 892 |
drop_items( |
| 893 |
&mut commands, |
| 894 |
&mut items, |
| 895 |
&item_assets, |
| 896 |
to, |
| 897 |
load.kind, |
| 898 |
dropped, |
| 899 |
); |
| 900 |
} |
| 901 |
carrying.0 = (load.count > dropped).then_some(Item { |
| 902 |
count: load.count - dropped, |
| 903 |
..load |
| 904 |
}); |
| 905 |
Step::Done |
| 906 |
} |
| 907 |
} else { |
| 908 |
Step::Done |
| 909 |
} |
| 910 |
} |
| 911 |
}; |
| 912 |
match step { |
| 913 |
Step::Continue => current.0 = Some(job), |
| 914 |
Step::Done => { |
| 915 |
reservations.release_all(entity); |
| 916 |
current.0 = None; |
| 917 |
let ordered_move = matches!( |
| 918 |
job, |
| 919 |
Job { |
| 920 |
task: Task::MoveTo(_), |
| 921 |
ordered: true |
| 922 |
} |
| 923 |
); |
| 924 |
idle.restart(if ordered_move { |
| 925 |
IDLE_AFTER_ORDER_SECS |
| 926 |
} else { |
| 927 |
rng.range_f32(WORK_PAUSE_SECS) |
| 928 |
}); |
| 929 |
} |
| 930 |
} |
| 931 |
} |
| 932 |
} |
| 933 |
|
| 934 |
#[cfg(test)] |
| 935 |
mod tests { |
| 936 |
use super::*; |
| 937 |
use crate::{ |
| 938 |
colonist::follow_path, |
| 939 |
items::STACK_LIMIT, |
| 940 |
sim::{SimPos, sim_pos}, |
| 941 |
world::Terrain, |
| 942 |
zones::NEW_STOCKPILE_SIZE, |
| 943 |
}; |
| 944 |
|
| 945 |
const TICK: Duration = Duration::from_nanos(1_000_000_000 / 60); |
| 946 |
|
| 947 |
fn test_app(map: WorldMap) -> App { |
| 948 |
let mut app = App::new(); |
| 949 |
app.insert_resource(map) |
| 950 |
.insert_resource(SimRng::new(1)) |
| 951 |
.init_resource::<Time>() |
| 952 |
.init_resource::<Reservations>() |
| 953 |
.init_resource::<ColonyGoals>() |
| 954 |
.init_resource::<ItemAssets>() |
| 955 |
.init_resource::<StructureAssets>() |
| 956 |
.init_resource::<LightMap>() |
| 957 |
.init_resource::<SimTick>() |
| 958 |
.init_resource::<Zones>() |
| 959 |
.add_message::<Order>() |
| 960 |
.add_message::<TileChanged>() |
| 961 |
.add_systems( |
| 962 |
Update, |
| 963 |
( |
| 964 |
handle_orders, |
| 965 |
choose_jobs, |
| 966 |
run_jobs, |
| 967 |
follow_path, |
| 968 |
crate::needs::update_rest, |
| 969 |
) |
| 970 |
.chain(), |
| 971 |
); |
| 972 |
app |
| 973 |
} |
| 974 |
|
| 975 |
fn spawn_colonist(app: &mut App, tile: UVec2) -> Entity { |
| 976 |
app.world_mut() |
| 977 |
.spawn(( |
| 978 |
Colonist { |
| 979 |
name: "Test".into(), |
| 980 |
}, |
| 981 |
TilePos(tile), |
| 982 |
sim_pos(WorldMap::tile_center(tile)), |
| 983 |
)) |
| 984 |
.id() |
| 985 |
} |
| 986 |
|
| 987 |
fn run_ticks(app: &mut App, ticks: u32) { |
| 988 |
for _ in 0..ticks { |
| 989 |
app.world_mut().resource_mut::<Time>().advance_by(TICK); |
| 990 |
app.update(); |
| 991 |
} |
| 992 |
} |
| 993 |
|
| 994 |
fn map_with_trees(trees: &[UVec2]) -> WorldMap { |
| 995 |
let mut map = WorldMap::filled(UVec2::new(20, 20), Terrain::Grass); |
| 996 |
for &t in trees { |
| 997 |
map.set_plant(t, Some(Plant::Tree)); |
| 998 |
} |
| 999 |
map |
| 1000 |
} |
| 1001 |
|
| 1002 |
fn wood_on_map(app: &mut App) -> u32 { |
| 1003 |
let world = app.world_mut(); |
| 1004 |
let items: Vec<Item> = world.query::<&Item>().iter(world).copied().collect(); |
| 1005 |
stock(items.iter(), ItemKind::Wood) |
| 1006 |
} |
| 1007 |
|
| 1008 |
fn items_at(app: &mut App) -> Vec<(UVec2, u32)> { |
| 1009 |
let world = app.world_mut(); |
| 1010 |
let mut items: Vec<(UVec2, u32)> = world |
| 1011 |
.query::<(&Item, &TilePos)>() |
| 1012 |
.iter(world) |
| 1013 |
.map(|(item, pos)| (pos.0, item.count)) |
| 1014 |
.collect(); |
| 1015 |
items.sort_by_key(|(p, _)| (p.y, p.x)); |
| 1016 |
items |
| 1017 |
} |
| 1018 |
|
| 1019 |
fn put_wood(app: &mut App, tile: UVec2, count: u32) { |
| 1020 |
app.world_mut().spawn(( |
| 1021 |
Item { |
| 1022 |
kind: ItemKind::Wood, |
| 1023 |
count, |
| 1024 |
}, |
| 1025 |
TilePos(tile), |
| 1026 |
)); |
| 1027 |
} |
| 1028 |
|
| 1029 |
fn set_goals(app: &mut App, goals: &[(ItemKind, u32)]) { |
| 1030 |
*app.world_mut().resource_mut::<ColonyGoals>() = |
| 1031 |
ColonyGoals(goals.iter().copied().collect()); |
| 1032 |
} |
| 1033 |
|
| 1034 |
fn stockpile_size(app: &App) -> usize { |
| 1035 |
let map = app.world().resource::<WorldMap>(); |
| 1036 |
let zones = app.world().resource::<Zones>(); |
| 1037 |
map.positions().filter(|&p| zones.is_stockpile(p)).count() |
| 1038 |
} |
| 1039 |
|
| 1040 |
fn stored(app: &mut App) -> u32 { |
| 1041 |
let zones = app.world().resource::<Zones>().clone(); |
| 1042 |
items_at(app) |
| 1043 |
.iter() |
| 1044 |
.filter(|(p, _)| zones.is_stockpile(*p)) |
| 1045 |
.map(|(_, c)| c) |
| 1046 |
.sum() |
| 1047 |
} |
| 1048 |
|
| 1049 |
fn hauling_app(stockpile: (IVec2, IVec2)) -> App { |
| 1050 |
let mut app = test_app(map_with_trees(&[])); |
| 1051 |
set_goals(&mut app, &[]); |
| 1052 |
let map = app.world().resource::<WorldMap>().clone(); |
| 1053 |
app.world_mut() |
| 1054 |
.resource_mut::<Zones>() |
| 1055 |
.add_stockpile(&map, stockpile.0, stockpile.1); |
| 1056 |
app |
| 1057 |
} |
| 1058 |
|
| 1059 |
fn carrying(app: &App, colonist: Entity) -> Option<Item> { |
| 1060 |
app.world().get::<Carrying>(colonist).unwrap().0 |
| 1061 |
} |
| 1062 |
|
| 1063 |
fn job(app: &App, colonist: Entity) -> Option<Job> { |
| 1064 |
app.world().get::<CurrentJob>(colonist).unwrap().0 |
| 1065 |
} |
| 1066 |
|
| 1067 |
#[test] |
| 1068 |
fn idle_colonists_cut_trees_when_the_colony_needs_wood() { |
| 1069 |
let tree = UVec2::new(8, 5); |
| 1070 |
let mut app = test_app(map_with_trees(&[tree])); |
| 1071 |
spawn_colonist(&mut app, UVec2::new(2, 5)); |
| 1072 |
run_ticks(&mut app, 60 * 15); |
| 1073 |
assert_eq!( |
| 1074 |
app.world().resource::<WorldMap>().plant(tree.as_ivec2()), |
| 1075 |
None |
| 1076 |
); |
| 1077 |
assert_eq!(wood_on_map(&mut app), 10); |
| 1078 |
assert!(app.world().resource::<Reservations>().0.is_empty()); |
| 1079 |
} |
| 1080 |
|
| 1081 |
#[test] |
| 1082 |
fn colonists_leave_trees_alone_when_there_is_enough_wood() { |
| 1083 |
let tree = UVec2::new(8, 5); |
| 1084 |
let mut app = test_app(map_with_trees(&[tree])); |
| 1085 |
set_goals(&mut app, &[]); |
| 1086 |
spawn_colonist(&mut app, UVec2::new(2, 5)); |
| 1087 |
run_ticks(&mut app, 60 * 15); |
| 1088 |
assert_eq!( |
| 1089 |
app.world().resource::<WorldMap>().plant(tree.as_ivec2()), |
| 1090 |
Some(Plant::Tree) |
| 1091 |
); |
| 1092 |
} |
| 1093 |
|
| 1094 |
#[test] |
| 1095 |
fn colonists_never_pick_the_same_tree() { |
| 1096 |
let trees = [UVec2::new(10, 10), UVec2::new(10, 11)]; |
| 1097 |
let mut app = test_app(map_with_trees(&trees)); |
| 1098 |
let a = spawn_colonist(&mut app, UVec2::new(5, 10)); |
| 1099 |
let b = spawn_colonist(&mut app, UVec2::new(5, 11)); |
| 1100 |
run_ticks(&mut app, 61); |
| 1101 |
let target = |c| match job(&app, c) { |
| 1102 |
Some(Job { |
| 1103 |
task: Task::Work { site, .. }, |
| 1104 |
.. |
| 1105 |
}) => site, |
| 1106 |
other => panic!("expected chop job, got {other:?}"), |
| 1107 |
}; |
| 1108 |
assert_ne!(target(a), target(b)); |
| 1109 |
} |
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
#[test] |
| 1114 |
fn work_in_progress_counts_towards_goals() { |
| 1115 |
let goals = ColonyGoals(HashMap::from([(ItemKind::Wood, 25)])); |
| 1116 |
assert!(goals.wanted(ItemKind::Wood, 10, 10)); |
| 1117 |
assert!(!goals.wanted(ItemKind::Wood, 10, 20)); |
| 1118 |
assert!(!goals.wanted(ItemKind::Wood, 30, 0)); |
| 1119 |
assert!(!goals.wanted(ItemKind::Gold, 0, 0)); |
| 1120 |
} |
| 1121 |
|
| 1122 |
#[test] |
| 1123 |
fn ordered_chop_ignores_the_wood_goal() { |
| 1124 |
let tree = UVec2::new(8, 5); |
| 1125 |
let mut app = test_app(map_with_trees(&[tree])); |
| 1126 |
set_goals(&mut app, &[]); |
| 1127 |
let colonist = spawn_colonist(&mut app, UVec2::new(2, 5)); |
| 1128 |
app.world_mut().write_message(Order { |
| 1129 |
colonist, |
| 1130 |
tile: tree, |
| 1131 |
}); |
| 1132 |
run_ticks(&mut app, 60 * 15); |
| 1133 |
assert_eq!( |
| 1134 |
app.world().resource::<WorldMap>().plant(tree.as_ivec2()), |
| 1135 |
None |
| 1136 |
); |
| 1137 |
} |
| 1138 |
|
| 1139 |
#[test] |
| 1140 |
fn ordering_a_move_cancels_the_current_job_and_frees_its_tree() { |
| 1141 |
let tree = UVec2::new(15, 5); |
| 1142 |
let mut app = test_app(map_with_trees(&[tree])); |
| 1143 |
let colonist = spawn_colonist(&mut app, UVec2::new(2, 5)); |
| 1144 |
run_ticks(&mut app, 61); |
| 1145 |
assert!(matches!( |
| 1146 |
job(&app, colonist), |
| 1147 |
Some(Job { |
| 1148 |
task: Task::Work { .. }, |
| 1149 |
.. |
| 1150 |
}) |
| 1151 |
)); |
| 1152 |
app.world_mut().write_message(Order { |
| 1153 |
colonist, |
| 1154 |
tile: UVec2::new(2, 15), |
| 1155 |
}); |
| 1156 |
run_ticks(&mut app, 1); |
| 1157 |
assert_eq!( |
| 1158 |
job(&app, colonist).map(|j| j.task), |
| 1159 |
Some(Task::MoveTo(UVec2::new(2, 15))) |
| 1160 |
); |
| 1161 |
assert!( |
| 1162 |
app.world() |
| 1163 |
.resource::<Reservations>() |
| 1164 |
.is_free(Target::Site(tree)) |
| 1165 |
); |
| 1166 |
} |
| 1167 |
|
| 1168 |
#[test] |
| 1169 |
fn idle_colonists_haul_loose_items_into_the_stockpile() { |
| 1170 |
let mut app = hauling_app((IVec2::new(15, 15), IVec2::new(17, 15))); |
| 1171 |
put_wood(&mut app, UVec2::new(5, 5), 20); |
| 1172 |
spawn_colonist(&mut app, UVec2::new(2, 5)); |
| 1173 |
run_ticks(&mut app, 60 * 15); |
| 1174 |
assert_eq!(items_at(&mut app), vec![(UVec2::new(15, 15), 20)]); |
| 1175 |
} |
| 1176 |
|
| 1177 |
#[test] |
| 1178 |
fn large_stacks_take_several_trips_and_spill_into_more_stockpile_tiles() { |
| 1179 |
let mut app = hauling_app((IVec2::new(15, 15), IVec2::new(17, 15))); |
| 1180 |
put_wood(&mut app, UVec2::new(5, 5), STACK_LIMIT + 10); |
| 1181 |
spawn_colonist(&mut app, UVec2::new(2, 5)); |
| 1182 |
run_ticks(&mut app, 60 * 40); |
| 1183 |
let stacks = items_at(&mut app); |
| 1184 |
assert_eq!(stored(&mut app), STACK_LIMIT + 10); |
| 1185 |
assert!(stacks.len() >= 2); |
| 1186 |
assert!(stacks.iter().all(|(_, count)| *count <= STACK_LIMIT)); |
| 1187 |
} |
| 1188 |
|
| 1189 |
#[test] |
| 1190 |
fn items_already_in_a_stockpile_stay_put() { |
| 1191 |
let mut app = hauling_app((IVec2::new(5, 5), IVec2::new(16, 16))); |
| 1192 |
put_wood(&mut app, UVec2::new(5, 5), 20); |
| 1193 |
let colonist = spawn_colonist(&mut app, UVec2::new(2, 5)); |
| 1194 |
run_ticks(&mut app, 60 * 5); |
| 1195 |
assert_eq!(items_at(&mut app), vec![(UVec2::new(5, 5), 20)]); |
| 1196 |
assert_eq!(carrying(&app, colonist), None); |
| 1197 |
} |
| 1198 |
|
| 1199 |
#[test] |
| 1200 |
fn colonists_found_a_stockpile_when_items_need_storing() { |
| 1201 |
let mut app = test_app(map_with_trees(&[])); |
| 1202 |
set_goals(&mut app, &[]); |
| 1203 |
put_wood(&mut app, UVec2::new(5, 5), 20); |
| 1204 |
spawn_colonist(&mut app, UVec2::new(2, 5)); |
| 1205 |
run_ticks(&mut app, 60 * 15); |
| 1206 |
assert_eq!( |
| 1207 |
stockpile_size(&app), |
| 1208 |
(NEW_STOCKPILE_SIZE * NEW_STOCKPILE_SIZE) as usize |
| 1209 |
); |
| 1210 |
assert_eq!(stored(&mut app), 20); |
| 1211 |
} |
| 1212 |
|
| 1213 |
#[test] |
| 1214 |
fn no_stockpile_is_founded_while_there_is_nothing_to_store() { |
| 1215 |
let mut app = test_app(map_with_trees(&[])); |
| 1216 |
set_goals(&mut app, &[]); |
| 1217 |
spawn_colonist(&mut app, UVec2::new(2, 5)); |
| 1218 |
run_ticks(&mut app, 60 * 5); |
| 1219 |
assert_eq!(stockpile_size(&app), 0); |
| 1220 |
} |
| 1221 |
|
| 1222 |
#[test] |
| 1223 |
fn two_colonists_never_haul_the_same_stack() { |
| 1224 |
let mut app = hauling_app((IVec2::new(15, 15), IVec2::new(16, 16))); |
| 1225 |
put_wood(&mut app, UVec2::new(5, 5), 10); |
| 1226 |
put_wood(&mut app, UVec2::new(5, 6), 10); |
| 1227 |
let a = spawn_colonist(&mut app, UVec2::new(2, 5)); |
| 1228 |
let b = spawn_colonist(&mut app, UVec2::new(2, 6)); |
| 1229 |
run_ticks(&mut app, 61); |
| 1230 |
let pickup = |c| match job(&app, c).map(|j| j.task) { |
| 1231 |
Some(Task::Haul { pickup, .. }) => pickup, |
| 1232 |
other => panic!("expected haul, got {other:?}"), |
| 1233 |
}; |
| 1234 |
assert_ne!(pickup(a), pickup(b)); |
| 1235 |
} |
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
#[test] |
| 1240 |
fn interrupted_haulers_deliver_their_load_later() { |
| 1241 |
let mut app = hauling_app((IVec2::new(15, 15), IVec2::new(17, 15))); |
| 1242 |
put_wood(&mut app, UVec2::new(4, 5), 20); |
| 1243 |
let colonist = spawn_colonist(&mut app, UVec2::new(2, 5)); |
| 1244 |
let mut ticks = 0; |
| 1245 |
while carrying(&app, colonist).is_none() && ticks < 60 * 10 { |
| 1246 |
run_ticks(&mut app, 1); |
| 1247 |
ticks += 1; |
| 1248 |
} |
| 1249 |
app.world_mut().write_message(Order { |
| 1250 |
colonist, |
| 1251 |
tile: UVec2::new(2, 2), |
| 1252 |
}); |
| 1253 |
run_ticks(&mut app, 60 * 3); |
| 1254 |
assert!(carrying(&app, colonist).is_some()); |
| 1255 |
run_ticks(&mut app, 60 * 20); |
| 1256 |
assert_eq!(carrying(&app, colonist), None); |
| 1257 |
assert_eq!(stored(&mut app), 20); |
| 1258 |
} |
| 1259 |
|
| 1260 |
#[test] |
| 1261 |
fn right_clicking_loose_items_orders_a_haul() { |
| 1262 |
let mut app = hauling_app((IVec2::new(15, 15), IVec2::new(15, 15))); |
| 1263 |
set_goals(&mut app, &[(ItemKind::Wood, 1000)]); |
| 1264 |
put_wood(&mut app, UVec2::new(5, 5), 20); |
| 1265 |
let colonist = spawn_colonist(&mut app, UVec2::new(2, 5)); |
| 1266 |
app.world_mut().write_message(Order { |
| 1267 |
colonist, |
| 1268 |
tile: UVec2::new(5, 5), |
| 1269 |
}); |
| 1270 |
run_ticks(&mut app, 1); |
| 1271 |
assert!(matches!( |
| 1272 |
job(&app, colonist), |
| 1273 |
Some(Job { |
| 1274 |
task: Task::Haul { .. }, |
| 1275 |
ordered: true |
| 1276 |
}) |
| 1277 |
)); |
| 1278 |
} |
| 1279 |
|
| 1280 |
fn mining_map() -> WorldMap { |
| 1281 |
let mut map = WorldMap::filled(UVec2::new(20, 20), Terrain::Grass); |
| 1282 |
for y in 0..20 { |
| 1283 |
map.set(UVec2::new(10, y), Terrain::Stone); |
| 1284 |
} |
| 1285 |
map.set(UVec2::new(10, 5), Terrain::IronOre); |
| 1286 |
map |
| 1287 |
} |
| 1288 |
|
| 1289 |
fn items_of(app: &mut App, kind: ItemKind) -> u32 { |
| 1290 |
let world = app.world_mut(); |
| 1291 |
let items: Vec<Item> = world.query::<&Item>().iter(world).copied().collect(); |
| 1292 |
stock(items.iter(), kind) |
| 1293 |
} |
| 1294 |
|
| 1295 |
#[test] |
| 1296 |
fn colonists_mine_rock_the_colony_needs_and_leave_gravel() { |
| 1297 |
let mut app = test_app(mining_map()); |
| 1298 |
set_goals(&mut app, &[(ItemKind::Stone, 10)]); |
| 1299 |
spawn_colonist(&mut app, UVec2::new(8, 12)); |
| 1300 |
run_ticks(&mut app, 60 * 12); |
| 1301 |
assert_eq!(items_of(&mut app, ItemKind::Stone), 10); |
| 1302 |
assert_eq!(items_of(&mut app, ItemKind::Iron), 0); |
| 1303 |
let map = app.world().resource::<WorldMap>(); |
| 1304 |
let gravel = map |
| 1305 |
.positions() |
| 1306 |
.filter(|p| map.get(p.as_ivec2()) == Some(Terrain::Gravel)) |
| 1307 |
.count(); |
| 1308 |
assert_eq!(gravel, 1); |
| 1309 |
} |
| 1310 |
|
| 1311 |
#[test] |
| 1312 |
fn colonists_walk_past_rock_they_do_not_need() { |
| 1313 |
let mut app = test_app(mining_map()); |
| 1314 |
set_goals(&mut app, &[(ItemKind::Iron, 10)]); |
| 1315 |
spawn_colonist(&mut app, UVec2::new(8, 15)); |
| 1316 |
run_ticks(&mut app, 60 * 15); |
| 1317 |
assert_eq!(items_of(&mut app, ItemKind::Iron), 10); |
| 1318 |
assert_eq!(items_of(&mut app, ItemKind::Stone), 0); |
| 1319 |
assert_eq!( |
| 1320 |
app.world().resource::<WorldMap>().get(IVec2::new(10, 5)), |
| 1321 |
Some(Terrain::Gravel) |
| 1322 |
); |
| 1323 |
} |
| 1324 |
|
| 1325 |
#[test] |
| 1326 |
fn right_clicking_rock_orders_mining_regardless_of_goals() { |
| 1327 |
let mut app = test_app(mining_map()); |
| 1328 |
set_goals(&mut app, &[]); |
| 1329 |
let colonist = spawn_colonist(&mut app, UVec2::new(8, 12)); |
| 1330 |
app.world_mut().write_message(Order { |
| 1331 |
colonist, |
| 1332 |
tile: UVec2::new(10, 12), |
| 1333 |
}); |
| 1334 |
run_ticks(&mut app, 60 * 10); |
| 1335 |
assert_eq!( |
| 1336 |
app.world().resource::<WorldMap>().get(IVec2::new(10, 12)), |
| 1337 |
Some(Terrain::Gravel) |
| 1338 |
); |
| 1339 |
} |
| 1340 |
|
| 1341 |
#[test] |
| 1342 |
fn colonists_expand_a_stockpile_that_is_nearly_full() { |
| 1343 |
let mut app = hauling_app((IVec2::new(5, 5), IVec2::new(6, 5))); |
| 1344 |
let colonist = spawn_colonist(&mut app, UVec2::new(2, 5)); |
| 1345 |
run_ticks(&mut app, 61); |
| 1346 |
assert!(matches!( |
| 1347 |
job(&app, colonist).map(|j| j.task), |
| 1348 |
Some(Task::ExpandStockpile { .. }) |
| 1349 |
)); |
| 1350 |
run_ticks(&mut app, 60 * 5); |
| 1351 |
assert_eq!(stockpile_size(&app), 2 + EXPANSION_TILES); |
| 1352 |
} |
| 1353 |
|
| 1354 |
#[test] |
| 1355 |
fn roomy_stockpiles_are_left_alone() { |
| 1356 |
let mut app = hauling_app((IVec2::new(5, 5), IVec2::new(8, 5))); |
| 1357 |
spawn_colonist(&mut app, UVec2::new(2, 5)); |
| 1358 |
run_ticks(&mut app, 60 * 5); |
| 1359 |
assert_eq!(stockpile_size(&app), 4); |
| 1360 |
} |
| 1361 |
|
| 1362 |
fn set_hour(app: &mut App, hour: u64) { |
| 1363 |
let ticks = (hour + 24 - 6) % 24 * crate::sim::TICKS_PER_HOUR; |
| 1364 |
*app.world_mut().resource_mut::<SimTick>() = SimTick(ticks); |
| 1365 |
} |
| 1366 |
|
| 1367 |
fn structures(app: &mut App) -> Vec<(UVec2, StructureKind)> { |
| 1368 |
let world = app.world_mut(); |
| 1369 |
world |
| 1370 |
.query::<(&TilePos, &Structure)>() |
| 1371 |
.iter(world) |
| 1372 |
.map(|(pos, s)| (pos.0, s.kind)) |
| 1373 |
.collect() |
| 1374 |
} |
| 1375 |
|
| 1376 |
fn put_campfire(app: &mut App, tile: UVec2) { |
| 1377 |
app.world_mut().spawn(( |
| 1378 |
Structure { |
| 1379 |
kind: StructureKind::Campfire, |
| 1380 |
fuel: StructureKind::Campfire.fuel_ticks(), |
| 1381 |
}, |
| 1382 |
TilePos(tile), |
| 1383 |
)); |
| 1384 |
} |
| 1385 |
|
| 1386 |
#[test] |
| 1387 |
fn colonists_light_a_campfire_near_the_stockpile_when_night_falls() { |
| 1388 |
let mut app = hauling_app((IVec2::new(10, 10), IVec2::new(12, 12))); |
| 1389 |
put_wood(&mut app, UVec2::new(10, 10), 20); |
| 1390 |
set_hour(&mut app, 18); |
| 1391 |
spawn_colonist(&mut app, UVec2::new(2, 5)); |
| 1392 |
run_ticks(&mut app, 60 * 20); |
| 1393 |
let built = structures(&mut app); |
| 1394 |
let campfire = built |
| 1395 |
.iter() |
| 1396 |
.find(|(_, kind)| *kind == StructureKind::Campfire) |
| 1397 |
.expect("a campfire"); |
| 1398 |
assert!(campfire.0.as_vec2().distance(Vec2::new(11.0, 11.0)) <= HOME_RADIUS); |
| 1399 |
assert_eq!(wood_on_map(&mut app), 10); |
| 1400 |
} |
| 1401 |
|
| 1402 |
#[test] |
| 1403 |
fn no_fires_are_built_during_the_day() { |
| 1404 |
let mut app = hauling_app((IVec2::new(10, 10), IVec2::new(12, 12))); |
| 1405 |
put_wood(&mut app, UVec2::new(10, 10), 20); |
| 1406 |
set_hour(&mut app, 10); |
| 1407 |
spawn_colonist(&mut app, UVec2::new(2, 5)); |
| 1408 |
run_ticks(&mut app, 60 * 10); |
| 1409 |
assert!(structures(&mut app).is_empty()); |
| 1410 |
} |
| 1411 |
|
| 1412 |
#[test] |
| 1413 |
fn without_wood_nothing_can_be_built() { |
| 1414 |
let mut app = hauling_app((IVec2::new(10, 10), IVec2::new(12, 12))); |
| 1415 |
set_hour(&mut app, 22); |
| 1416 |
spawn_colonist(&mut app, UVec2::new(2, 5)); |
| 1417 |
run_ticks(&mut app, 60 * 10); |
| 1418 |
assert!(structures(&mut app).is_empty()); |
| 1419 |
} |
| 1420 |
|
| 1421 |
#[test] |
| 1422 |
fn torches_light_stockpile_tiles_beyond_the_campfire() { |
| 1423 |
let mut app = hauling_app((IVec2::new(2, 10), IVec2::new(17, 10))); |
| 1424 |
put_wood(&mut app, UVec2::new(9, 10), 20); |
| 1425 |
put_campfire(&mut app, UVec2::new(9, 12)); |
| 1426 |
set_hour(&mut app, 22); |
| 1427 |
spawn_colonist(&mut app, UVec2::new(9, 8)); |
| 1428 |
run_ticks(&mut app, 60 * 15); |
| 1429 |
assert!( |
| 1430 |
structures(&mut app) |
| 1431 |
.iter() |
| 1432 |
.any(|(_, kind)| *kind == StructureKind::Torch) |
| 1433 |
); |
| 1434 |
} |
| 1435 |
|
| 1436 |
|
| 1437 |
#[test] |
| 1438 |
fn work_is_slower_in_the_dark() { |
| 1439 |
let tree = UVec2::new(8, 5); |
| 1440 |
let finish_ticks = |daylight: f32| { |
| 1441 |
let mut app = test_app(map_with_trees(&[tree])); |
| 1442 |
let size = app.world().resource::<WorldMap>().size; |
| 1443 |
app.insert_resource(LightMap::compute(size, daylight, [])); |
| 1444 |
spawn_colonist(&mut app, UVec2::new(7, 5)); |
| 1445 |
let mut ticks = 0; |
| 1446 |
while app |
| 1447 |
.world() |
| 1448 |
.resource::<WorldMap>() |
| 1449 |
.plant(tree.as_ivec2()) |
| 1450 |
.is_some() |
| 1451 |
{ |
| 1452 |
run_ticks(&mut app, 1); |
| 1453 |
ticks += 1; |
| 1454 |
} |
| 1455 |
ticks |
| 1456 |
}; |
| 1457 |
let (lit, dark) = (finish_ticks(1.0), finish_ticks(0.0)); |
| 1458 |
assert!( |
| 1459 |
dark > lit + WorkKind::Chop.work() / 2 - 10, |
| 1460 |
"lit {lit}, dark {dark}" |
| 1461 |
); |
| 1462 |
} |
| 1463 |
|
| 1464 |
const NAP_TEST_REST: f32 = 0.25; |
| 1465 |
|
| 1466 |
fn set_rest(app: &mut App, colonist: Entity, rest: f32) { |
| 1467 |
app.world_mut().get_mut::<Rest>(colonist).unwrap().0 = rest; |
| 1468 |
} |
| 1469 |
|
| 1470 |
fn rest(app: &App, colonist: Entity) -> f32 { |
| 1471 |
app.world().get::<Rest>(colonist).unwrap().0 |
| 1472 |
} |
| 1473 |
|
| 1474 |
fn task(app: &App, colonist: Entity) -> Option<Task> { |
| 1475 |
job(app, colonist).map(|j| j.task) |
| 1476 |
} |
| 1477 |
|
| 1478 |
#[test] |
| 1479 |
fn tired_colonists_nap_and_get_up_when_rested() { |
| 1480 |
let mut app = test_app(map_with_trees(&[])); |
| 1481 |
set_goals(&mut app, &[]); |
| 1482 |
set_hour(&mut app, 10); |
| 1483 |
let colonist = spawn_colonist(&mut app, UVec2::new(5, 5)); |
| 1484 |
set_rest(&mut app, colonist, NAP_TEST_REST); |
| 1485 |
run_ticks(&mut app, 61); |
| 1486 |
assert!(matches!(task(&app, colonist), Some(Task::Sleep { .. }))); |
| 1487 |
let mut ticks = 0; |
| 1488 |
while matches!(task(&app, colonist), Some(Task::Sleep { .. })) && ticks < 60 * 60 * 10 { |
| 1489 |
run_ticks(&mut app, 60); |
| 1490 |
ticks += 60; |
| 1491 |
} |
| 1492 |
assert!(!matches!(task(&app, colonist), Some(Task::Sleep { .. }))); |
| 1493 |
assert!(rest(&app, colonist) > 0.95); |
| 1494 |
} |
| 1495 |
|
| 1496 |
#[test] |
| 1497 |
fn colonists_go_to_bed_at_night_and_sleep_until_morning() { |
| 1498 |
let mut app = test_app(map_with_trees(&[])); |
| 1499 |
set_goals(&mut app, &[]); |
| 1500 |
set_hour(&mut app, 22); |
| 1501 |
let colonist = spawn_colonist(&mut app, UVec2::new(5, 5)); |
| 1502 |
set_rest(&mut app, colonist, 0.8); |
| 1503 |
run_ticks(&mut app, 61); |
| 1504 |
assert!(matches!(task(&app, colonist), Some(Task::Sleep { .. }))); |
| 1505 |
run_ticks(&mut app, 3 * crate::sim::TICKS_PER_HOUR as u32); |
| 1506 |
assert_eq!(rest(&app, colonist), 1.0); |
| 1507 |
assert!(matches!(task(&app, colonist), Some(Task::Sleep { .. }))); |
| 1508 |
} |
| 1509 |
|
| 1510 |
#[test] |
| 1511 |
fn rested_colonists_stay_up_during_the_day() { |
| 1512 |
let mut app = test_app(map_with_trees(&[])); |
| 1513 |
set_goals(&mut app, &[]); |
| 1514 |
set_hour(&mut app, 12); |
| 1515 |
let colonist = spawn_colonist(&mut app, UVec2::new(5, 5)); |
| 1516 |
set_rest(&mut app, colonist, 0.8); |
| 1517 |
run_ticks(&mut app, 60 * 5); |
| 1518 |
assert!(!matches!(task(&app, colonist), Some(Task::Sleep { .. }))); |
| 1519 |
} |
| 1520 |
|
| 1521 |
#[test] |
| 1522 |
fn exhausted_colonists_drop_their_work_to_sleep() { |
| 1523 |
let tree = UVec2::new(15, 5); |
| 1524 |
let mut app = test_app(map_with_trees(&[tree])); |
| 1525 |
let colonist = spawn_colonist(&mut app, UVec2::new(2, 5)); |
| 1526 |
run_ticks(&mut app, 61); |
| 1527 |
assert!(matches!(task(&app, colonist), Some(Task::Work { .. }))); |
| 1528 |
set_rest(&mut app, colonist, EXHAUSTED_REST / 2.0); |
| 1529 |
run_ticks(&mut app, 30); |
| 1530 |
assert!(matches!(task(&app, colonist), Some(Task::Sleep { .. }))); |
| 1531 |
assert!( |
| 1532 |
app.world() |
| 1533 |
.resource::<Reservations>() |
| 1534 |
.is_free(Target::Site(tree)) |
| 1535 |
); |
| 1536 |
} |
| 1537 |
|
| 1538 |
#[test] |
| 1539 |
fn exhaustion_does_not_override_orders() { |
| 1540 |
let tree = UVec2::new(15, 5); |
| 1541 |
let mut app = test_app(map_with_trees(&[tree])); |
| 1542 |
let colonist = spawn_colonist(&mut app, UVec2::new(2, 5)); |
| 1543 |
app.world_mut().write_message(Order { |
| 1544 |
colonist, |
| 1545 |
tile: tree, |
| 1546 |
}); |
| 1547 |
run_ticks(&mut app, 2); |
| 1548 |
set_rest(&mut app, colonist, EXHAUSTED_REST / 2.0); |
| 1549 |
run_ticks(&mut app, 30); |
| 1550 |
assert!(matches!( |
| 1551 |
job(&app, colonist), |
| 1552 |
Some(Job { |
| 1553 |
task: Task::Work { .. }, |
| 1554 |
ordered: true |
| 1555 |
}) |
| 1556 |
)); |
| 1557 |
} |
| 1558 |
|
| 1559 |
#[test] |
| 1560 |
fn colonists_chop_from_a_neighbouring_tile() { |
| 1561 |
let tree = UVec2::new(8, 5); |
| 1562 |
let mut app = test_app(map_with_trees(&[tree])); |
| 1563 |
let colonist = spawn_colonist(&mut app, UVec2::new(2, 5)); |
| 1564 |
run_ticks(&mut app, 60 * 5); |
| 1565 |
let tile = app.world().get::<TilePos>(colonist).unwrap().0; |
| 1566 |
assert!(is_adjacent(tile.as_ivec2(), tree.as_ivec2())); |
| 1567 |
assert_eq!( |
| 1568 |
app.world().get::<SimPos>(colonist).unwrap().0, |
| 1569 |
WorldMap::tile_center(tile) |
| 1570 |
); |
| 1571 |
} |
| 1572 |
} |