Skip to content

Commit

Permalink
improve direction checks
Browse files Browse the repository at this point in the history
* more descriptive variable names
* decreased tolerance for what counts as aligned.
* distance vector values are now rounded instead of being floored, which improves smooth walking results.
  • Loading branch information
LevFendi committed May 14, 2024
1 parent a533447 commit 03870ef
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
5 changes: 3 additions & 2 deletions control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3758,8 +3758,8 @@ script.on_event("read-cursor-distance-vector", function(event)
if players[pindex].menu ~= "crafting" then
local c_pos = players[pindex].cursor_pos
local p_pos = players[pindex].position
local diff_x = math.floor(c_pos.x - p_pos.x)
local diff_y = math.floor(c_pos.y - p_pos.y)
local diff_x = math.floor(c_pos.x - p_pos.x + 0.5)
local diff_y = math.floor(c_pos.y - p_pos.y + 0.5)
local dir_x = dirs.east
if diff_x < 0 then dir_x = dirs.west end
local dir_y = dirs.south
Expand All @@ -3774,6 +3774,7 @@ script.on_event("read-cursor-distance-vector", function(event)
.. fa_utils.direction_lookup(dir_y)
printout(result, pindex)
game.get_player(pindex).print(result, { volume_modifier = 0 })
--Show cursor position
rendering.draw_circle({
color = { 1, 0.2, 0 },
radius = 0.1,
Expand Down
22 changes: 11 additions & 11 deletions scripts/fa-utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ end
* Returns 1 of 8 main directions, based on the ratios of the x and y distances.
* The deciding ratio is 1 to 4, meaning that for an object that is 100 tiles north, it can be offset by up to 25 tiles east or west before it stops being counted as "directly" in the north.
* The arctangent of 1/4 is about 14 degrees, meaning that the field of view that directly counts as a cardinal direction is about 30 degrees, while for a diagonal direction it is about 60 degrees.]]
function mod.get_direction_biased(pos_that, pos_this)
local diff_x = pos_that.x - pos_this.x
local diff_y = pos_that.y - pos_this.y
function mod.get_direction_biased(pos_target, pos_origin)
local diff_x = pos_target.x - pos_origin.x
local diff_y = pos_target.y - pos_origin.y
local dir = -1

if math.abs(diff_x) > 4 * math.abs(diff_y) then --along east-west
Expand Down Expand Up @@ -126,9 +126,9 @@ end
* Returns 1 of 8 main directions, based on the ratios of the x and y distances.
* The deciding ratio is 1 to 2.5, meaning that for an object that is 25 tiles north, it can be offset by up to 10 tiles east or west before it stops being counted as "directly" in the north.
* The arctangent of 1/2.5 is about 22 degrees, meaning that the field of view that directly counts as a cardinal direction is about 44 degrees, while for a diagonal direction it is about 46 degrees.]]
function mod.get_direction_precise(pos_that, pos_this)
local diff_x = pos_that.x - pos_this.x
local diff_y = pos_that.y - pos_this.y
function mod.get_direction_precise(pos_target, pos_origin)
local diff_x = pos_target.x - pos_origin.x
local diff_y = pos_target.y - pos_origin.y
local dir = -1

if math.abs(diff_x) > 2.5 * math.abs(diff_y) then --along east-west
Expand Down Expand Up @@ -164,10 +164,10 @@ function mod.get_direction_precise(pos_that, pos_this)
end

--Checks whether a cardinal or diagonal direction is precisely aligned.
function mod.is_direction_aligned(pos_that, pos_this)
local diff_x = math.abs(pos_this.x - pos_that.x)
local diff_y = math.abs(pos_this.y - pos_that.y)
return (diff_x < 1 or diff_y < 1 or (diff_x - diff_y) < 1)
function mod.is_direction_aligned(pos_target, pos_origin)
local diff_x = math.abs(pos_origin.x - pos_target.x)
local diff_y = math.abs(pos_origin.y - pos_target.y)
return (diff_x < 0.5 or diff_y < 0.5 or math.abs(diff_x - diff_y) < 0.5)
end

--Converts an input direction into a localised string.
Expand Down Expand Up @@ -657,7 +657,7 @@ function mod.get_substring_before_dash(str)
end
end

--Reads the localised result for the distance and direction from one point to the other. Also mentions if they are precisely aligned.
--Reads the localised result for the distance and direction from one point to the other. Also mentions if they are precisely aligned. Distances are rounded.
function mod.dir_dist_locale(pos1, pos2)
local dir_dist = mod.dir_dist(pos1, pos2)
local aligned_note = ""
Expand Down

0 comments on commit 03870ef

Please sign in to comment.