Skip to content

Commit

Permalink
fix: use nearest waypoint as initial
Browse files Browse the repository at this point in the history
  • Loading branch information
pikdum committed Oct 19, 2024
1 parent 9b3f717 commit 0a51635
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
31 changes: 30 additions & 1 deletion lib/game/entities/mob.ex
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,29 @@ defmodule ThistleTea.Mob do
end
end

# creature.currentwaypoint doesn't seem reliable
# so instead, find the nearest to start with
def get_nearest_waypoint({x, y, z}, waypoints) do
case waypoints do
[] ->
0

_ ->
waypoints
|> Enum.min_by(fn cm ->
SpatialHash.distance(
{
cm.position_x,
cm.position_y,
cm.position_z
},
{x, y, z}
)
end)
|> Map.get(:point)
end
end

@impl GenServer
def init(creature) do
creature =
Expand All @@ -185,6 +208,12 @@ defmodule ThistleTea.Mob do
creature.position_z
)

nearest_waypoint =
get_nearest_waypoint(
{creature.position_x, creature.position_y, creature.position_z},
creature.creature_movement
)

state =
%{
creature: creature,
Expand All @@ -199,7 +228,7 @@ defmodule ThistleTea.Mob do
x0: creature.position_x,
y0: creature.position_y,
z0: creature.position_z,
initial_point: creature.currentwaypoint,
initial_point: nearest_waypoint,
running: false,
behavior: nil
}
Expand Down
2 changes: 1 addition & 1 deletion lib/game/utils/spatial_hash.ex
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ defmodule SpatialHash do
|> Enum.uniq()
end

defp distance({x1, y1, z1}, {x2, y2, z2}) do
def distance({x1, y1, z1}, {x2, y2, z2}) do
dx = x2 - x1
dy = y2 - y1
dz = z2 - z1
Expand Down

0 comments on commit 0a51635

Please sign in to comment.