Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3x speedup of the distance transform. #28

Merged
merged 5 commits into from
Sep 10, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
226 changes: 169 additions & 57 deletions src/algorithms/distance_transform.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// Heavily based on https://github.com/Screeps-Tutorials/Screeps-Tutorials/blob/Master/basePlanningAlgorithms/distanceTransform.js

use screeps::{
self,
constants::{extra::ROOM_SIZE, Direction},
local::{LocalCostMatrix, LocalRoomTerrain, RoomXY},
constants::extra::ROOM_SIZE,
local::{LocalCostMatrix, LocalRoomTerrain, RoomCoordinate, RoomXY},
};

use crate::room_coordinate::{range_exclusive, range_inclusive};

/// Provides a Cost Matrix with values equal to the Chebyshev distance from any
/// wall terrain. This does *not* calculate based on constructed walls, only
/// terrain walls.
Expand All @@ -29,67 +30,178 @@ pub fn chebyshev_distance_transform_from_terrain(
/// This allows for calculating the distance transform from an arbitrary set of
/// positions. Other position values in the initial Cost Matrix should be
/// initialized to 255 (u8::MAX) to ensure the calculations work correctly.
pub fn chebyshev_distance_transform_from_cost_matrix(
initial_cm: LocalCostMatrix,
) -> LocalCostMatrix {
// Copy the initial cost matrix into the output cost matrix
let mut cm = initial_cm.clone();
Comment on lines -35 to -36
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We actually never needed this clone, since we get the cost-matrix by value; the old owner can't do anything with it.


pub fn chebyshev_distance_transform_from_cost_matrix(mut cm: LocalCostMatrix) -> LocalCostMatrix {
let zero = RoomCoordinate::new(0).unwrap();
let one = RoomCoordinate::new(1).unwrap();
let forty_eight = RoomCoordinate::new(ROOM_SIZE - 2).unwrap();
let forty_nine = RoomCoordinate::new(ROOM_SIZE - 1).unwrap();
// Pass 1: Top-to-Bottom, Left-to-Right

for x in 0..ROOM_SIZE {
for y in 0..ROOM_SIZE {
let current_position = unsafe { RoomXY::unchecked_new(x, y) };
// Phase A: first column
range_inclusive(one, forty_nine)
.map(|y| RoomXY { x: zero, y })
.fold(cm.get(RoomXY { x: zero, y: zero }), |top, xy| {
let val = cm.get(xy).min(top.saturating_add(1));
cm.set(xy, val);
val
});

// The distance to the closest wall is the minimum of the current position value
// and all of its neighbors. However, since we're going TTB:LTR, we
// can ignore tiles we know we haven't visited yet: TopRight, Right,
// BottomRight, and Bottom. We could include them and their default
// max values should get ignored, but why waste the processing cycles?
let min_value = [
Direction::Top,
Direction::TopLeft,
Direction::Left,
Direction::BottomLeft,
]
.into_iter()
.filter_map(|dir| current_position.checked_add_direction(dir))
.map(|position| cm.get(position))
.min()
.map(|x| x.saturating_add(1))
.map(|x| x.min(cm.get(current_position)))
.unwrap_or_else(|| cm.get(current_position));

cm.set(current_position, min_value);
}
}
// Phase B: the rest
range_inclusive(one, forty_nine)
.zip(range_inclusive(zero, forty_eight))
.for_each(|(current_x, left_x)| {
let initial_top = cm
.get(RoomXY {
x: current_x,
y: zero,
})
.min(
cm.get(RoomXY { x: left_x, y: zero })
.min(cm.get(RoomXY { x: left_x, y: one }))
.saturating_add(1),
);
cm.set(
RoomXY {
x: current_x,
y: zero,
},
initial_top,
);
let final_top = range_exclusive(zero, forty_nine)
.map(|y| {
(RoomXY { x: current_x, y }, unsafe {
[
RoomCoordinate::unchecked_new(y.u8() - 1),
y,
RoomCoordinate::unchecked_new(y.u8() + 1),
]
})
})
.fold(initial_top, |top, (current_xy, lefts)| {
let val = lefts
.into_iter()
.map(|y| RoomXY { x: left_x, y })
.map(|xy| cm.get(xy))
.min()
.unwrap()
.min(top)
.saturating_add(1)
.min(cm.get(current_xy));
cm.set(current_xy, val);
val
});
cm.set(
RoomXY {
x: current_x,
y: forty_nine,
},
cm.get(RoomXY {
x: current_x,
y: forty_nine,
})
.min(
final_top
.min(cm.get(RoomXY {
x: left_x,
y: forty_eight,
}))
.min(cm.get(RoomXY {
x: left_x,
y: forty_nine,
}))
.saturating_add(1),
),
);
});

// Pass 2: Bottom-to-Top, Right-to-Left

for x in (0..ROOM_SIZE).rev() {
for y in (0..ROOM_SIZE).rev() {
let current_position = unsafe { RoomXY::unchecked_new(x, y) };

// The same logic as with Pass 1 applies here, we're just going BTT:RTL instead,
// so the neighbors we ignore are: BottomLeft, Left, TopLeft, and
// Top.
let min_value = [
Direction::Bottom,
Direction::Right,
Direction::BottomRight,
Direction::TopRight,
]
.into_iter()
.filter_map(|dir| current_position.checked_add_direction(dir))
.map(|position| cm.get(position))
.min()
.map(|x| x.saturating_add(1))
.map(|x| x.min(cm.get(current_position)))
.unwrap_or_else(|| cm.get(current_position));
// Phase A: last column
range_inclusive(zero, forty_eight)
.map(|y| RoomXY { x: forty_nine, y })
.fold(
cm.get(RoomXY {
x: forty_nine,
y: forty_nine,
}),
|bottom, xy| {
let val = cm.get(xy).min(bottom.saturating_add(1));
cm.set(xy, val);
val
},
);

cm.set(current_position, min_value);
}
}
// Phase B: the rest
range_inclusive(zero, forty_eight)
.rev()
.zip(range_inclusive(one, forty_nine).rev())
.for_each(|(current_x, right_x)| {
let initial_bottom = cm
.get(RoomXY {
x: current_x,
y: forty_nine,
})
.min(
cm.get(RoomXY {
x: right_x,
y: forty_nine,
})
.min(cm.get(RoomXY {
x: right_x,
y: forty_eight,
}))
.saturating_add(1),
);
cm.set(
RoomXY {
x: current_x,
y: forty_nine,
},
initial_bottom,
);
let final_bottom = range_exclusive(zero, forty_nine)
.map(|y| {
(RoomXY { x: current_x, y }, unsafe {
[
RoomCoordinate::unchecked_new(y.u8() - 1),
y,
RoomCoordinate::unchecked_new(y.u8() + 1),
]
})
})
.rfold(initial_bottom, |bottom, (current_xy, rights)| {
let val = rights
.into_iter()
.map(|y| RoomXY { x: right_x, y })
.map(|xy| cm.get(xy))
.min()
.unwrap()
.min(bottom)
.saturating_add(1)
.min(cm.get(current_xy));
cm.set(current_xy, val);
val
});
cm.set(
RoomXY {
x: current_x,
y: zero,
},
cm.get(RoomXY {
x: current_x,
y: zero,
})
.min(
final_bottom
.min(cm.get(RoomXY {
x: right_x,
y: zero,
}))
.min(cm.get(RoomXY { x: right_x, y: one }))
.saturating_add(1),
),
);
});

cm
}