From b987bfccb4adba232f52c0d8f94ccf126b0bd452 Mon Sep 17 00:00:00 2001 From: Isse Date: Fri, 14 Apr 2023 14:02:35 +0200 Subject: [PATCH] lazily calculate magnitude in goto and add comments --- rtsim/src/rule/npc_ai.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/rtsim/src/rule/npc_ai.rs b/rtsim/src/rule/npc_ai.rs index b5ff2ee199..cf24019378 100644 --- a/rtsim/src/rule/npc_ai.rs +++ b/rtsim/src/rule/npc_ai.rs @@ -283,9 +283,6 @@ fn goto(wpos: Vec3, speed_factor: f32, goal_dist: f32) -> impl Action { let mut waypoint = None; just(move |ctx| { - let rpos = wpos - ctx.npc.wpos; - let len = rpos.magnitude(); - // If we're close to the next waypoint, complete it if waypoint.map_or(false, |waypoint: Vec3| { ctx.npc.wpos.xy().distance_squared(waypoint.xy()) < WAYPOINT_DIST.powi(2) @@ -295,6 +292,8 @@ fn goto(wpos: Vec3, speed_factor: f32, goal_dist: f32) -> impl Action { // Get the next waypoint on the route toward the goal let waypoint = waypoint.get_or_insert_with(|| { + let rpos = wpos - ctx.npc.wpos; + let len = rpos.magnitude(); let wpos = ctx.npc.wpos + (rpos / len) * len.min(STEP_DIST); wpos.with_z( @@ -313,6 +312,8 @@ fn goto(wpos: Vec3, speed_factor: f32, goal_dist: f32) -> impl Action { .map(|_| {}) } +/// Try to walk fly a 3D position following the terrain altitude at an offset +/// without caring for obstacles. fn goto_flying( wpos: Vec3, speed_factor: f32, @@ -324,9 +325,6 @@ fn goto_flying( let mut waypoint = None; just(move |ctx| { - let rpos = wpos - ctx.npc.wpos; - let len = rpos.magnitude(); - // If we're close to the next waypoint, complete it if waypoint.map_or(false, |waypoint: Vec3| { ctx.npc.wpos.distance_squared(waypoint) < waypoint_dist.powi(2) @@ -336,6 +334,8 @@ fn goto_flying( // Get the next waypoint on the route toward the goal let waypoint = waypoint.get_or_insert_with(|| { + let rpos = wpos - ctx.npc.wpos; + let len = rpos.magnitude(); let wpos = ctx.npc.wpos + (rpos / len) * len.min(step_dist); wpos.with_z( @@ -364,6 +364,8 @@ fn goto_2d(wpos2d: Vec2, speed_factor: f32, goal_dist: f32) -> impl Action }) } +/// Try to fly toward a 2D position following the terrain altitude at an offset +/// without caring for obstacles. fn goto_2d_flying( wpos2d: Vec2, speed_factor: f32,