fix?
This commit is contained in:
+22
-11
@@ -44,10 +44,11 @@ pub(super) fn execute_idle(
|
||||
|
||||
// Only reset path if target changed or no path exists
|
||||
if target_changed || ambulatory.current_path.is_none() {
|
||||
// +1.0 sub-tile offset matching original wandering system
|
||||
ambulatory.target = Some(Vec3::new(
|
||||
target.x as f32,
|
||||
target.y as f32,
|
||||
transform.translation.z,
|
||||
target.z as f32 + 1.0,
|
||||
));
|
||||
ambulatory.current_path = None;
|
||||
ambulatory.path_index = 0;
|
||||
@@ -56,6 +57,7 @@ pub(super) fn execute_idle(
|
||||
|
||||
/// Pick a random standable tile within wander radius of origin.
|
||||
/// Uses reservoir sampling to avoid heap allocation.
|
||||
/// Searches for actual surface z at each XY position (like original wandering system).
|
||||
fn pick_wander_target(
|
||||
origin: &IVec3,
|
||||
radius: i32,
|
||||
@@ -67,16 +69,25 @@ fn pick_wander_target(
|
||||
|
||||
for dx in -radius..=radius {
|
||||
for dy in -radius..=radius {
|
||||
let candidate = IVec3::new(
|
||||
origin.x + dx * ITILE_SIZE,
|
||||
origin.y + dy * ITILE_SIZE,
|
||||
origin.z,
|
||||
);
|
||||
if tilemap.is_standable(candidate) {
|
||||
count += 1;
|
||||
// Reservoir sampling: 1/count chance to replace chosen
|
||||
if rng.random_range(0..count) == 0 {
|
||||
chosen = Some(candidate);
|
||||
// Search for actual surface z at this XY - same logic as original wandering
|
||||
for z in -3i32..=4i32 {
|
||||
let floor_pos = IVec3::new(
|
||||
origin.x + dx * ITILE_SIZE,
|
||||
origin.y + dy * ITILE_SIZE,
|
||||
z * ITILE_SIZE,
|
||||
);
|
||||
// Check if there's a floor tile at this z
|
||||
if tilemap.floor_tiles.get(&floor_pos).is_some() {
|
||||
// Check if standable one tile above
|
||||
let above = IVec3::new(floor_pos.x, floor_pos.y, floor_pos.z + ITILE_SIZE);
|
||||
if tilemap.is_standable(above) {
|
||||
count += 1;
|
||||
// Reservoir sampling: 1/count chance to replace chosen
|
||||
if rng.random_range(0..count) == 0 {
|
||||
chosen = Some(above);
|
||||
}
|
||||
break; // found surface at this XY, stop z search
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user