Skip to content

Commit

Permalink
tweak slime ai
Browse files Browse the repository at this point in the history
  • Loading branch information
aratama committed Feb 11, 2025
1 parent c46833a commit c44661e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 29 deletions.
3 changes: 2 additions & 1 deletion assets/registry.actor.ron
Original file line number Diff line number Diff line change
Expand Up @@ -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)
]
)
Expand Down
31 changes: 15 additions & 16 deletions src/level/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&current);
Expand Down Expand Up @@ -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(&current) {
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(&current) {
path.push(prev);
current = prev;
}
path.reverse();
path
}

fn pop_lowest_node(
open_set: &mut HashSet<(i32, i32)>,
f_score: &HashMap<(i32, i32), i32>,
Expand Down
19 changes: 7 additions & 12 deletions src/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::<u32>() % random;
}

Expand Down Expand Up @@ -252,20 +253,14 @@ fn execute(
&registry,
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,
};

Expand Down

0 comments on commit c44661e

Please sign in to comment.