Skip to content

Commit

Permalink
Implement custom path finding
Browse files Browse the repository at this point in the history
I found the path finding code highly complicated and also inefficient, since we allocated on the heap multiple times, without amortising the allocations.

This implementation provides a struct that implements the A* and also line of sight path finding in a way, that is compatible with the server implementations. We also re-use the heap allocations.

The A* algorithm is a basic implementation that uses a binary heap. Moved the code in korangar_utils, so that it can be properly optimized and also tested.
  • Loading branch information
hasenbanck committed Dec 21, 2024
1 parent dd253e5 commit 4a2b8c9
Show file tree
Hide file tree
Showing 8 changed files with 493 additions and 176 deletions.
43 changes: 1 addition & 42 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ resolver = "2"
members = ["korangar", "ragnarok_*", "korangar_*"]

[workspace.dependencies]
arrayvec = "0.7"
bitflags = "2.6"
bumpalo = "3.16"
bytemuck = "1.20"
Expand All @@ -27,7 +28,6 @@ lunify = "1.1"
mlua = "0.10"
num = "0.4"
option-ext = "0.2"
pathfinding = "4.11"
pcap = "2.2"
pollster = "0.4"
proc-macro2 = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion korangar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
arrayvec = { workspace = true }
bumpalo = { workspace = true, features = ["allocator_api"] }
bytemuck = { workspace = true, features = ["derive", "extern_crate_std", "min_const_generics"] }
cgmath = { workspace = true, features = ["mint", "serde"] }
Expand All @@ -23,7 +24,6 @@ lunify = { workspace = true }
mlua = { workspace = true, features = ["lua51", "vendored"] }
num = { workspace = true }
option-ext = { workspace = true }
pathfinding = { workspace = true }
pollster = { workspace = true }
ragnarok_bytes = { workspace = true, features = ["derive", "cgmath"] }
ragnarok_formats = { workspace = true, features = ["interface"] }
Expand Down
10 changes: 8 additions & 2 deletions korangar/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ use korangar_networking::{
DisconnectReason, HotkeyState, LoginServerLoginData, MessageColor, NetworkEvent, NetworkEventBuffer, NetworkingSystem, SellItem,
ShopItem,
};
use korangar_util::pathing::PathFinder;
#[cfg(feature = "debug")]
use korangar_util::texture_atlas::AtlasAllocation;
#[cfg(not(feature = "debug"))]
Expand Down Expand Up @@ -252,6 +253,7 @@ struct Client {
player_inventory: Inventory,
player_skill_tree: SkillTree,
hotbar: Hotbar,
path_finder: PathFinder,

point_light_set_buffer: ResourceSetBuffer<LightSourceKey>,
directional_shadow_object_set_buffer: ResourceSetBuffer<ObjectKey>,
Expand Down Expand Up @@ -510,6 +512,7 @@ impl Client {
let player_inventory = Inventory::default();
let player_skill_tree = SkillTree::default();
let hotbar = Hotbar::default();
let path_finder = PathFinder::default();

let point_light_set_buffer = ResourceSetBuffer::default();
let directional_shadow_object_set_buffer = ResourceSetBuffer::default();
Expand Down Expand Up @@ -658,6 +661,7 @@ impl Client {
player_inventory,
player_skill_tree,
hotbar,
path_finder,
point_light_set_buffer,
directional_shadow_object_set_buffer,
point_shadow_object_set_buffer,
Expand Down Expand Up @@ -956,6 +960,7 @@ impl Client {
&mut self.animation_loader,
&self.script_loader,
&self.map,
&mut self.path_finder,
saved_login_data.account_id,
character_information,
WorldPosition { x: 0, y: 0 },
Expand Down Expand Up @@ -1023,6 +1028,7 @@ impl Client {
&mut self.animation_loader,
&self.script_loader,
&self.map,
&mut self.path_finder,
entity_appeared_data,
client_tick,
);
Expand Down Expand Up @@ -1060,15 +1066,15 @@ impl Client {
let position_from = Vector2::new(position_from.x, position_from.y);
let position_to = Vector2::new(position_to.x, position_to.y);

entity.move_from_to(&self.map, position_from, position_to, starting_timestamp);
entity.move_from_to(&self.map, &mut self.path_finder, position_from, position_to, starting_timestamp);
#[cfg(feature = "debug")]
entity.generate_pathing_mesh(&self.device, &self.queue, &self.map, &self.pathing_texture_mapping);
}
}
NetworkEvent::PlayerMove(position_from, position_to, starting_timestamp) => {
let position_from = Vector2::new(position_from.x, position_from.y);
let position_to = Vector2::new(position_to.x, position_to.y);
self.entities[0].move_from_to(&self.map, position_from, position_to, starting_timestamp);
self.entities[0].move_from_to(&self.map, &mut self.path_finder, position_from, position_to, starting_timestamp);

#[cfg(feature = "debug")]
self.entities[0].generate_pathing_mesh(&self.device, &self.queue, &self.map, &self.pathing_texture_mapping);
Expand Down
Loading

0 comments on commit 4a2b8c9

Please sign in to comment.