This commit is contained in:
2026-03-22 12:56:52 +00:00
parent 360ffc7526
commit 2eaf0196a5
7 changed files with 420 additions and 23 deletions
+244 -12
View File
@@ -9,23 +9,31 @@
//! Uses Changed<TaskQueue> + Changed<TaskState> to minimise queries.
use crate::entities::behaviour::{EntityBehaviourRegistry, EntityType};
use crate::entities::cargo::HaulSlot;
use crate::entities::shared_components::Ambulatory;
use crate::entities::tasks::components::{IdleState, Task, TaskQueue, TaskState};
use crate::entities::tasks::components::{
ChopStep, DropStep, HaulStep, IdleState, Task, TaskQueue, TaskState,
};
use crate::entities::tasks::events::{TaskClaimed, TaskCompleted, TaskFailed};
use crate::entities::tasks::idle::execute_idle;
use crate::world::chunks::ChunkMap;
use crate::world::generation::forestry::{fell_tree, TreePart};
use crate::world::tiles::tile_changed::TileChangedEvent;
use crate::world::tiles::visibility::TileOcclusionEvent;
use crate::world::tiles::TileMap;
use bevy::prelude::*;
use bevy_rand::prelude::*;
use rand::{RngExt, SeedableRng};
/// Main task executor. Runs in FixedUpdate.
pub fn task_executor_system(
mut commands: Commands,
tilemap: Res<TileMap>,
mut tilemap_mut: ResMut<TileMap>,
chunk_map: Res<ChunkMap>,
asset_server: Res<AssetServer>,
mut rng_q: Query<&mut WyRand, With<GlobalRng>>,
mut tick: Local<u32>,
tree_parts: Query<(Entity, &TreePart)>,
mut query: Query<(
Entity,
&mut TaskQueue,
@@ -34,10 +42,13 @@ pub fn task_executor_system(
&Transform,
&mut Sprite,
&EntityType,
Option<&mut HaulSlot>,
)>,
mut claimed_writer: MessageWriter<TaskClaimed>,
mut completed_writer: MessageWriter<TaskCompleted>,
mut failed_writer: MessageWriter<TaskFailed>,
mut tile_changed: MessageWriter<TileChangedEvent>,
mut occlusion: MessageWriter<TileOcclusionEvent>,
) {
let Ok(mut rng) = rng_q.single_mut() else {
return;
@@ -46,8 +57,16 @@ pub fn task_executor_system(
*tick = tick.wrapping_add(1);
let current_tick = *tick;
for (entity, mut queue, mut state, mut ambulatory, transform, mut sprite, entity_type) in
query.iter_mut()
for (
entity,
mut queue,
mut state,
mut ambulatory,
transform,
mut sprite,
entity_type,
mut haul_slot,
) in query.iter_mut()
{
let origin = transform.translation.as_ivec3();
let behaviour = EntityBehaviourRegistry::global_get(entity_type.0);
@@ -58,6 +77,7 @@ pub fn task_executor_system(
sigma_world: behaviour.idle.sigma_world,
state: IdleState::Picking {
retry_after_tick: 0,
retry_count: 0,
},
});
}
@@ -110,13 +130,224 @@ pub fn task_executor_system(
ambulatory.current_path = None;
}
}
Task::ChopTree { .. } | Task::HaulObject { .. } | Task::DropHauled { .. } => {
debug!(
"Task {} unimplemented for entity {:?}",
current_task.name(),
entity
);
*state = TaskState::Failed;
Task::ChopTree {
trunk_pos,
chop_ticks,
step,
} => match step {
ChopStep::MovingToTree => {
if !tilemap_mut.fixture_tiles.contains_key(trunk_pos) {
failed_writer.write(TaskFailed {
entity,
task: current_task.clone(),
reason: "tree already gone",
});
*state = TaskState::Failed;
continue;
}
if ambulatory.target.is_none() {
ambulatory.target = Some(Vec3::new(
trunk_pos.x as f32,
trunk_pos.y as f32,
trunk_pos.z as f32 + 1.0,
));
ambulatory.current_path = None;
}
let dx = transform.translation.x - trunk_pos.x as f32;
let dy = transform.translation.y - trunk_pos.y as f32;
let dist_sq = dx * dx + dy * dy;
let chop_range_sq = (crate::constants::TILE_SIZE * 2.5)
* (crate::constants::TILE_SIZE * 2.5);
if dist_sq <= chop_range_sq {
ambulatory.target = None;
ambulatory.current_path = None;
*step = ChopStep::Chopping {
ticks_remaining: *chop_ticks,
};
}
}
ChopStep::Chopping { ticks_remaining } => {
if *ticks_remaining == 0 {
let trunk_positions = fell_tree(
*trunk_pos,
&tree_parts,
&mut commands,
&mut tilemap_mut,
&mut tile_changed,
&mut occlusion,
);
let log_sprite = asset_server.load("tree_trunk.png");
for &pos in trunk_positions.iter() {
crate::entities::cargo::spawn_log_cargo(
&mut commands,
&mut tilemap_mut,
pos,
log_sprite.clone(),
);
}
*step = ChopStep::Done;
} else {
*ticks_remaining -= 1;
}
}
ChopStep::Done => {
*state = TaskState::Completed;
}
},
Task::HaulCargo {
cargo_entity,
cargo_pos,
dest,
step,
} => {
let Some(ref mut haul) = haul_slot else {
failed_writer.write(TaskFailed {
entity,
task: current_task.clone(),
reason: "entity has no HaulSlot",
});
*state = TaskState::Failed;
continue;
};
match step {
HaulStep::MovingToCargo => {
if !tilemap_mut.cargo_tiles.contains_key(cargo_pos) {
failed_writer.write(TaskFailed {
entity,
task: current_task.clone(),
reason: "cargo no longer exists",
});
*state = TaskState::Failed;
continue;
}
if ambulatory.target.is_none() {
ambulatory.target = Some(Vec3::new(
cargo_pos.x as f32,
cargo_pos.y as f32,
cargo_pos.z as f32 + 1.0,
));
ambulatory.current_path = None;
}
let dx = transform.translation.x - cargo_pos.x as f32;
let dy = transform.translation.y - cargo_pos.y as f32;
let dist_sq = dx * dx + dy * dy;
let pickup_range_sq = (crate::constants::TILE_SIZE * 1.5)
* (crate::constants::TILE_SIZE * 1.5);
if dist_sq <= pickup_range_sq {
ambulatory.target = None;
*step = HaulStep::PickingUp;
}
}
HaulStep::PickingUp => {
if haul.is_occupied() {
failed_writer.write(TaskFailed {
entity,
task: current_task.clone(),
reason: "HaulSlot already occupied",
});
*state = TaskState::Failed;
continue;
}
if let Some(_) = tilemap_mut.remove_cargo(cargo_pos) {
haul.pick_up(*cargo_entity);
*step = HaulStep::MovingToDest;
} else {
failed_writer.write(TaskFailed {
entity,
task: current_task.clone(),
reason: "cargo vanished before pickup",
});
*state = TaskState::Failed;
}
}
HaulStep::MovingToDest => {
if ambulatory.target.is_none() {
let drop_pos = tilemap_mut
.find_nearest_free_cargo_tile(*dest, 8)
.unwrap_or(*dest);
ambulatory.target = Some(Vec3::new(
drop_pos.x as f32,
drop_pos.y as f32,
drop_pos.z as f32 + 1.0,
));
ambulatory.current_path = None;
}
let target_pos = ambulatory.target.unwrap_or(Vec3::ZERO);
let dx = transform.translation.x - target_pos.x;
let dy = transform.translation.y - target_pos.y;
let dist_sq = dx * dx + dy * dy;
let arrive_sq = (crate::constants::TILE_SIZE * 1.5)
* (crate::constants::TILE_SIZE * 1.5);
if dist_sq <= arrive_sq {
ambulatory.target = None;
*step = HaulStep::Dropping;
}
}
HaulStep::Dropping => {
if let Some(cargo) = haul.release() {
let drop_pos = tilemap_mut
.find_nearest_free_cargo_tile(
transform.translation.as_ivec3(),
8,
)
.unwrap_or(transform.translation.as_ivec3());
let _ = tilemap_mut.place_cargo(drop_pos, cargo);
// TODO: update Cargo.tile_pos via CargoDroppedEvent
}
*step = HaulStep::Done;
}
HaulStep::Done => {
*state = TaskState::Completed;
}
}
}
Task::DropHauled { pos, step } => {
let Some(ref mut haul) = haul_slot else {
*state = TaskState::Completed;
continue;
};
match step {
DropStep::Moving => {
if ambulatory.target.is_none() {
let drop_pos = tilemap_mut
.find_nearest_free_cargo_tile(*pos, 8)
.unwrap_or(*pos);
ambulatory.target = Some(Vec3::new(
drop_pos.x as f32,
drop_pos.y as f32,
drop_pos.z as f32 + 1.0,
));
ambulatory.current_path = None;
}
let target_pos = ambulatory.target.unwrap_or(Vec3::ZERO);
let dx = transform.translation.x - target_pos.x;
let dy = transform.translation.y - target_pos.y;
let dist_sq = dx * dx + dy * dy;
let arrive_sq = (crate::constants::TILE_SIZE * 1.5)
* (crate::constants::TILE_SIZE * 1.5);
if dist_sq <= arrive_sq {
ambulatory.target = None;
*step = DropStep::Dropping;
}
}
DropStep::Dropping => {
if let Some(cargo) = haul.release() {
let drop_pos = tilemap_mut
.find_nearest_free_cargo_tile(
transform.translation.as_ivec3(),
8,
)
.unwrap_or(transform.translation.as_ivec3());
let _ = tilemap_mut.place_cargo(drop_pos, cargo);
}
*step = DropStep::Done;
}
DropStep::Done => {
*state = TaskState::Completed;
}
}
}
}
}
@@ -134,7 +365,7 @@ pub fn task_executor_system(
failed_writer.write(TaskFailed {
entity,
task: completed_task.clone(),
reason: "unimplemented",
reason: completed_task.name(),
});
}
@@ -144,6 +375,7 @@ pub fn task_executor_system(
sigma_world: behaviour.idle.sigma_world,
state: IdleState::Picking {
retry_after_tick: 0,
retry_count: 0,
},
});
}