From c44661eead3ce705889216eb2ad053f8e9762a50 Mon Sep 17 00:00:00 2001 From: aratama <16192627+aratama@users.noreply.github.com> Date: Tue, 11 Feb 2025 14:28:06 +0900 Subject: [PATCH] tweak slime ai --- assets/registry.actor.ron | 3 ++- src/level/world.rs | 31 +++++++++++++++---------------- src/strategy.rs | 19 +++++++------------ 3 files changed, 24 insertions(+), 29 deletions(-) diff --git a/assets/registry.actor.ron b/assets/registry.actor.ron index b3dbbda..92b58be 100644 --- a/assets/registry.actor.ron +++ b/assets/registry.actor.ron @@ -210,7 +210,8 @@ ActorRegistry( min_life: 1000, actions: [ Wait (count: 30, random: 30), - Approach (count: 120, random: 120, far: 200.0, near: 16.0), + // far:400だと画面外から発見されてしまいがち + Approach (count: 120, random: 120, far: 300.0, near: 16.0), Fire (count: 15, random: 15, range: 8.0) ] ) diff --git a/src/level/world.rs b/src/level/world.rs index e26c82c..3b48016 100644 --- a/src/level/world.rs +++ b/src/level/world.rs @@ -165,7 +165,7 @@ impl GameWorld { // 探索 while let Some(current) = pop_lowest_node(&mut open_set, &f_score) { if current == goal { - return Some(self.reconstruct_path(current, &came_from)); + return Some(reconstruct_path(current, &came_from)); } open_set.remove(¤t); @@ -207,25 +207,24 @@ impl GameWorld { TileType::Floor => 1, } } - - fn reconstruct_path( - &self, - current: (i32, i32), - came_from: &HashMap<(i32, i32), (i32, i32)>, - ) -> Vec<(i32, i32)> { - let mut path = vec![current]; - let mut current = current; - while let Some(&prev) = came_from.get(¤t) { - path.push(prev); - current = prev; - } - path.reverse(); - path - } } const BLOCKED_PATH_COST: i32 = std::i32::MAX; +fn reconstruct_path( + current: (i32, i32), + came_from: &HashMap<(i32, i32), (i32, i32)>, +) -> Vec<(i32, i32)> { + let mut path = vec![current]; + let mut current = current; + while let Some(&prev) = came_from.get(¤t) { + path.push(prev); + current = prev; + } + path.reverse(); + path +} + fn pop_lowest_node( open_set: &mut HashSet<(i32, i32)>, f_score: &HashMap<(i32, i32), i32>, diff --git a/src/strategy.rs b/src/strategy.rs index a696835..05dff17 100644 --- a/src/strategy.rs +++ b/src/strategy.rs @@ -2,6 +2,7 @@ use crate::actor::Actor; use crate::actor::ActorFireState; use crate::actor::ActorGroup; use crate::collision::SENSOR_GROUPS; +use crate::constant::TILE_SIZE; use crate::level::chunk::index_to_position; use crate::level::chunk::position_to_index; use crate::level::world::GameWorld; @@ -222,7 +223,7 @@ fn execute( count: _, random, } => { - if actor.commander.count == 0 { + if actor.commander.count == 0 && 0 < *random { actor.commander.count = rand::random::() % random; } @@ -252,20 +253,14 @@ fn execute( ®istry, position_to_index(origin), position_to_index(nearest.position), - 10, + (*far / TILE_SIZE) as i32, ); let destination = match route { - Some(route) => { - let router_fitered: Vec<_> = route - .iter() - .filter(|p| 8.0 < origin.distance(index_to_position(**p))) - .collect(); - match router_fitered.get(0) { - Some(index) => index_to_position(**index), - None => origin, - } - } + Some(route) => match route.get(1) { + Some(index) => index_to_position(*index), + None => origin, + }, None => origin, };