Skip to content

Commit 7dd8791

Browse files
committed
Temp commit of the current changes to vision map. The vision map calculation should be correct here,
1 parent e057e1e commit 7dd8791

File tree

10 files changed

+254
-71
lines changed

10 files changed

+254
-71
lines changed

src/mapping/chokes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::mapping::map_point;
2-
use crate::path_find::pos::{Pos, PositionAPI, NormalPosAPI};
2+
use crate::path_find::pos::{NormalPosAPI, Pos, PositionAPI};
33
use crate::path_find::pos::{DIAGONAL_MINUS_CARDINAL, MULT, MULTF32, SQRT2};
44
use crate::path_find::PathFind;
55
use pathfinding::prelude::absdiff;

src/mapping/map.rs

+41-6
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,19 @@ impl Map {
209209
-> (Vec<(usize, usize)>, f32) {
210210
let start_int = (start.0.round() as usize, start.1.round() as usize);
211211
let end_int = (end.0.round() as usize, end.1.round() as usize);
212-
let window_int = possible_window.map(|((x0, y0), (x1, y1))|
213-
((x0.round() as usize, y0.round() as usize),
214-
(x1.round() as usize, y1.round() as usize)));
212+
let window_int = possible_window.map(|((x0, y0), (x1, y1))| {
213+
((x0.round() as usize, y0.round() as usize),
214+
(x1.round() as usize, y1.round() as usize))
215+
});
215216

216217
let map = self.get_map(map_type);
217-
map.find_path(start_int, end_int, large, influence, possible_heuristic, window_int, possible_distance_from_target)
218+
map.find_path(start_int,
219+
end_int,
220+
large,
221+
influence,
222+
possible_heuristic,
223+
window_int,
224+
possible_distance_from_target)
218225
}
219226

220227
/// Basic version of find_path with all parameters except heuristic set to false or None.
@@ -224,7 +231,6 @@ impl Map {
224231
end: (f32, f32),
225232
possible_heuristic: Option<u8>)
226233
-> (Vec<(usize, usize)>, f32) {
227-
228234
let start_int = (start.0.round() as usize, start.1.round() as usize);
229235
let end_int = (end.0.round() as usize, end.1.round() as usize);
230236

@@ -247,7 +253,32 @@ impl Map {
247253
pub fn clear_vision(&mut self) { self.vision_map.clear(); }
248254
pub fn add_vision_unit(&mut self, unit: VisionUnit) { self.vision_map.add_unit(unit); }
249255
pub fn calculate_vision_map(&mut self) { self.vision_map.calculate_vision_map(&self.points); }
250-
pub fn vision_status(& self, point: (f32, f32)) -> usize { self.vision_map.vision_status(point) }
256+
pub fn vision_status(&self, point: (f32, f32)) -> usize { self.vision_map.vision_status(point) }
257+
258+
pub fn add_influence_to_vision(&mut self, map_type: u8, seen_value: usize, detection_value: usize) {
259+
let map = self.get_map_mut(map_type);
260+
let vision_map = &(self.vision_map); // self.get_vision();
261+
map.add_influence_to_map_by_vision(vision_map, seen_value, detection_value);
262+
263+
// map.add_influence_to_map_by_vision(self.get_vision(), seen_value, detection_value)
264+
// self.add_influence_to_map_by_vision(map, seen_value, detection_value);
265+
}
266+
267+
fn add_influence_to_map_by_vision(&mut self, map: &mut PathFind, seen_value: usize, detection_value: usize) {
268+
let vision_map = self.get_vision();
269+
270+
for x in 0..map.width {
271+
for y in 0..map.height {
272+
let status = vision_map.vision_status((x as f32, y as f32));
273+
if status == 1 {
274+
map.add_influence_spot((x, y), seen_value);
275+
}
276+
if status == 2 {
277+
map.add_influence_spot((x, y), detection_value);
278+
}
279+
}
280+
}
281+
}
251282
}
252283

253284
impl Map {
@@ -424,6 +455,10 @@ impl Map {
424455
panic!("Map type {} does not exist", map_type.to_string());
425456
}
426457

458+
fn get_vision(&mut self) -> &mut VisionMap{
459+
return &mut self.vision_map;
460+
}
461+
427462
pub fn get_map_mut(&mut self, map_type: u8) -> &mut PathFind {
428463
if map_type == 0 {
429464
return &mut self.ground_pathing;

src/mapping/vision.rs

+42-8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use super::map_point;
99
#[derive(Copy, Clone)]
1010
pub enum VisionStatus {
1111
NotSeen,
12+
NotSeenButDetected,
1213
Seen,
1314
Detected,
1415
}
@@ -55,10 +56,11 @@ impl VisionMap {
5556

5657
pub fn add_unit(&mut self, unit: VisionUnit) { self.units.push(unit); }
5758

58-
pub fn vision_status(& self, position: (f32, f32)) -> usize {
59+
pub fn vision_status(&self, position: (f32, f32)) -> usize {
5960
let int_point = round_point2(position);
6061
match self.points[int_point.0][int_point.1] {
6162
VisionStatus::NotSeen => 0,
63+
VisionStatus::NotSeenButDetected => 0,
6264
VisionStatus::Seen => 1,
6365
VisionStatus::Detected => 2,
6466
}
@@ -103,6 +105,7 @@ impl VisionMap {
103105
for y in 0..self.height {
104106
match self.points[x][y] {
105107
VisionStatus::NotSeen => vision_map[x][y] = 0,
108+
VisionStatus::NotSeenButDetected => vision_map[x][y] = 0,
106109
VisionStatus::Seen => vision_map[x][y] = 1,
107110
VisionStatus::Detected => vision_map[x][y] = 2,
108111
}
@@ -144,8 +147,12 @@ fn set_vision(points: &mut Vec<Vec<VisionStatus>>, position: &(f32, f32), sight_
144147
for y in rect.y..rect.y_end {
145148
let d = octile_distance_f32(u_position, (x, y));
146149

147-
if d <= sight_range && matches!(points[x][y], VisionStatus::NotSeen) {
148-
points[x][y] = VisionStatus::Seen;
150+
if d <= sight_range {
151+
if matches!(points[x][y], VisionStatus::NotSeen) {
152+
points[x][y] = VisionStatus::Seen;
153+
} else if matches!(points[x][y], VisionStatus::NotSeenButDetected) {
154+
points[x][y] = VisionStatus::Detected;
155+
}
149156
}
150157
}
151158
}
@@ -155,6 +162,27 @@ fn calc_ground_detection(points: &mut Vec<Vec<VisionStatus>>,
155162
map_points: &Vec<Vec<map_point::MapPoint>>,
156163
position: &(f32, f32),
157164
sight_range: f32) {
165+
let u_position = round_point2(*position);
166+
let size = ((sight_range * 2f32) as usize, (sight_range * 2f32) as usize);
167+
let width = points.len();
168+
let height = points[0].len();
169+
170+
let rect = rectangle::Rectangle::init_from_center2(u_position, size, width, height);
171+
172+
for x in rect.x..rect.x_end {
173+
for y in rect.y..rect.y_end {
174+
let d = octile_distance_f32(u_position, (x, y));
175+
176+
if d <= sight_range {
177+
if matches!(points[x][y], VisionStatus::NotSeen) || matches!(points[x][y], VisionStatus::NotSeenButDetected) {
178+
points[x][y] = VisionStatus::NotSeenButDetected;
179+
} else {
180+
points[x][y] = VisionStatus::Detected;
181+
}
182+
}
183+
}
184+
}
185+
158186
let circumference = 2f32 * sight_range * std::f32::consts::PI;
159187
let rays = circumference as usize;
160188
let step_mult = 1.3f32;
@@ -217,22 +245,28 @@ fn calc_ground_vision(points: &mut Vec<Vec<VisionStatus>>,
217245
for step in 0..steps {
218246
// Rays are only drawn until non-walkable is found and thus the cannot go out of bounds
219247
let step_f32 = step as f32 / step_mult;
220-
let new_pos =
221-
((position.0 + v_x * step_f32) as usize, (position.1 + v_y * step_f32) as usize);
248+
let new_pos = ((position.0 + v_x * step_f32) as usize, (position.1 + v_y * step_f32) as usize);
222249

223-
224250
// TODO: Same for height difference
225251
if map_points[new_pos.0][new_pos.1].height > max_height_seen {
226252
// Ray can't reach further
227-
println!("Ray {} stopped at ({}, {}), angle was {} and vector was ({}, {}) with step {}", index, new_pos.0, new_pos.1, angle, v_x, v_y, step_f32);
253+
println!("Ray {} stopped at ({}, {}), angle was {} and vector was ({}, {}) with step {}",
254+
index, new_pos.0, new_pos.1, angle, v_x, v_y, step_f32);
228255
break;
229256
}
230257

231258
if matches!(points[new_pos.0][new_pos.1], VisionStatus::NotSeen) {
232259
// if new_pos.0 == 25 && new_pos.1 == 8 {
233-
println!("Ray {} set vision to ({}, {}), angle was {} and vector was ({}, {}) with step {}", index, new_pos.0, new_pos.1, angle, v_x, v_y, step_f32);
260+
println!("Ray {} set vision to ({}, {}), angle was {} and vector was ({}, {}) with step {}",
261+
index, new_pos.0, new_pos.1, angle, v_x, v_y, step_f32);
234262
// }
235263
points[new_pos.0][new_pos.1] = VisionStatus::Seen;
264+
} else if matches!(points[new_pos.0][new_pos.1], VisionStatus::NotSeenButDetected) {
265+
// if new_pos.0 == 25 && new_pos.1 == 8 {
266+
println!("Ray {} set vision to ({}, {}), angle was {} and vector was ({}, {}) with step {}",
267+
index, new_pos.0, new_pos.1, angle, v_x, v_y, step_f32);
268+
// }
269+
points[new_pos.0][new_pos.1] = VisionStatus::Detected;
236270
}
237271
}
238272
}

0 commit comments

Comments
 (0)