flatland_pathfinding/
grid.rs1use flatland_protocol::{
4 BuildingView, DoorView, LifeState, ResourceNodeState, TerrainKindView, TerrainZoneView,
5 ZPlatformView, ZTransitionView,
6};
7
8pub const PLAYER_RADIUS_M: f32 = 0.45;
9pub const PATH_CLEARANCE_M: f32 = 0.35;
11pub const SEGMENT_SAMPLE_M: f32 = 0.3;
13
14#[derive(Debug, Clone, Copy)]
15pub struct NavBlockingCircle {
16 pub x: f32,
17 pub y: f32,
18 pub radius_m: f32,
19}
20
21#[derive(Debug, Clone)]
23pub struct NavWorld {
24 pub world_width_m: f32,
25 pub world_height_m: f32,
26 pub terrain_zones: Vec<TerrainZoneView>,
27 pub z_platforms: Vec<ZPlatformView>,
28 pub z_transitions: Vec<ZTransitionView>,
29 pub buildings: Vec<BuildingView>,
30 pub doors: Vec<DoorView>,
31 pub circles: Vec<NavBlockingCircle>,
32}
33
34impl NavWorld {
35 pub fn elevation_at(&self, x: f32, y: f32) -> f32 {
36 terrain_at(&self.terrain_zones, x, y)
37 .map(|z| z.elevation)
38 .unwrap_or(0.0)
39 }
40
41 pub fn terrain_kind_at(&self, x: f32, y: f32) -> TerrainKindView {
42 terrain_at(&self.terrain_zones, x, y)
43 .map(|z| z.kind)
44 .unwrap_or(TerrainKindView::Grass)
45 }
46}
47
48fn terrain_at(zones: &[TerrainZoneView], x: f32, y: f32) -> Option<&TerrainZoneView> {
49 zones
50 .iter()
51 .find(|zone| x >= zone.x0 && x < zone.x1 && y >= zone.y0 && y < zone.y1)
52}
53
54pub(crate) fn terrain_cost(kind: TerrainKindView) -> u16 {
55 match kind {
56 TerrainKindView::Grass => 10,
57 TerrainKindView::Hill => 14,
58 TerrainKindView::Bog => 25,
59 TerrainKindView::ShallowWater => 40,
60 TerrainKindView::DeepWater => u16::MAX,
61 TerrainKindView::Trail => 8,
62 TerrainKindView::Rock => u16::MAX,
63 }
64}
65
66pub fn collides_player_at(x: f32, y: f32, world: &NavWorld) -> bool {
67 if world.circles.iter().any(|o| {
68 circle_overlap(x, y, PLAYER_RADIUS_M, o.x, o.y, o.radius_m)
69 }) {
70 return true;
71 }
72 let pad = PLAYER_RADIUS_M;
73 world.buildings.iter().any(|b| {
74 let hw = b.width_m / 2.0 + pad;
75 let hd = b.depth_m / 2.0 + pad;
76 x >= b.x - hw && x <= b.x + hw && y >= b.y - hd && y <= b.y + hd
77 })
78}
79
80fn circle_overlap(ax: f32, ay: f32, ar: f32, bx: f32, by: f32, br: f32) -> bool {
81 let dx = ax - bx;
82 let dy = ay - by;
83 let min_dist = ar + br;
84 dx * dx + dy * dy < min_dist * min_dist
85}
86
87pub(crate) fn block_circle(blocked: &mut [bool], width: i16, height: i16, cx: f32, cy: f32, radius_m: f32) {
88 let block_r = radius_m + PLAYER_RADIUS_M + PATH_CLEARANCE_M;
89 let r = block_r.ceil() as i16;
90 let ix = cx.floor() as i16;
91 let iy = cy.floor() as i16;
92 for dy in -r..=r {
93 for dx in -r..=r {
94 let cell_x = ix + dx;
95 let cell_y = iy + dy;
96 if cell_x < 0 || cell_y < 0 || cell_x >= width || cell_y >= height {
97 continue;
98 }
99 let cell_cx = cell_x as f32 + 0.5;
100 let cell_cy = cell_y as f32 + 0.5;
101 if (cell_cx - cx).hypot(cell_cy - cy) <= block_r {
102 let idx = (cell_y as usize) * (width as usize) + (cell_x as usize);
103 blocked[idx] = true;
104 }
105 }
106 }
107}
108
109pub(crate) fn mark_building_footprint(blocked: &mut [bool], width: i16, height: i16, building: &BuildingView) {
110 let pad = PLAYER_RADIUS_M + PATH_CLEARANCE_M;
111 let hw = building.width_m / 2.0;
112 let hd = building.depth_m / 2.0;
113 let x0 = (building.x - hw - pad).floor() as i16;
114 let y0 = (building.y - hd - pad).floor() as i16;
115 let x1 = (building.x + hw + pad).ceil() as i16 - 1;
116 let y1 = (building.y + hd + pad).ceil() as i16 - 1;
117 if x1 < x0 || y1 < y0 {
118 return;
119 }
120 for x in x0..=x1 {
121 for y in y0..=y1 {
122 if x >= 0 && y >= 0 && x < width && y < height {
123 let idx = (y as usize) * (width as usize) + (x as usize);
124 blocked[idx] = true;
125 }
126 }
127 }
128}
129
130pub(crate) fn clear_door_cells(blocked: &mut [bool], width: i16, height: i16, doors: &[DoorView]) {
131 for door in doors {
132 if door.open {
133 let (x, y) = world_to_cell(door.x, door.y);
134 for dx in -1i16..=1 {
135 for dy in -1i16..=1 {
136 if dx.abs() + dy.abs() <= 1 {
137 let cx = x + dx;
138 let cy = y + dy;
139 if cx >= 0 && cy >= 0 && cx < width && cy < height {
140 let idx = (cy as usize) * (width as usize) + (cx as usize);
141 blocked[idx] = false;
142 }
143 }
144 }
145 }
146 }
147 }
148}
149
150pub(crate) fn world_to_cell(x: f32, y: f32) -> (i16, i16) {
151 (x.floor() as i16, y.floor() as i16)
152}
153
154pub(crate) fn cell_center(x: i16, y: i16) -> (f32, f32) {
155 (x as f32 + 0.5, y as f32 + 0.5)
156}
157
158pub fn circles_from_views(
160 resource_nodes: &[flatland_protocol::ResourceNodeView],
161 npcs: &[flatland_protocol::NpcView],
162) -> Vec<NavBlockingCircle> {
163 let mut circles = Vec::new();
164 for node in resource_nodes {
165 if node.blocking
166 && matches!(
167 node.state,
168 ResourceNodeState::Available | ResourceNodeState::Harvesting
169 )
170 {
171 let radius = if node.blocking_radius_m > 0.0 {
172 node.blocking_radius_m
173 } else {
174 0.8
175 };
176 circles.push(NavBlockingCircle {
177 x: node.x,
178 y: node.y,
179 radius_m: radius,
180 });
181 }
182 }
183 for npc in npcs {
184 let alive =
185 npc.life_state != Some(LifeState::Dead) && npc.hp_pct.is_none_or(|h| h > 0.0);
186 if alive {
187 circles.push(NavBlockingCircle {
188 x: npc.x,
189 y: npc.y,
190 radius_m: 0.55,
191 });
192 }
193 }
194 circles
195}