Narrow positions and bounding boxes at the boundary

Values read back from the game always use the named members, so they
are narrowed once where they enter a function rather than at each use.

`LuaControl.force` is a ForceID union, so reading a force only method
off it needs the cast the rest of the repo already uses.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
bbassie
2026-08-07 15:31:43 +00:00
co-authored by Claude Opus 5
parent 3f46055b70
commit a4213dadf5
3 changed files with 27 additions and 18 deletions
+6 -5
View File
@@ -15,11 +15,11 @@ local abs = math.abs
local commands = {}
--- @param player LuaPlayer
--- @param area BoundingBox
--- @param area ExpUtil_AABB.Box
--- @return boolean
local function location_break(player, area)
local surface = player.surface -- Allow remote view
local is_charted = player.force.is_chunk_charted
local is_charted = (player.force --[[@as LuaForce]]).is_chunk_charted
if is_charted(surface, { x = floor(area.left_top.x / 32), y = floor(area.left_top.y / 32) }) then
return true
elseif is_charted(surface, { x = floor(area.left_top.x / 32), y = floor(area.right_bottom.y / 32) }) then
@@ -63,13 +63,14 @@ SelectArea:on_selection(function(event)
}
local count = 0
local hits = {} --- @type MapPosition[]
local hits = {} --- @type MapPosition.struct[]
for _, entity in ipairs(entities) do
local skip = false
local entity_position = entity.position --[[@as MapPosition.struct]]
for _, pos in ipairs(hits) do
local x = abs(entity.position.x - pos.x)
local y = abs(entity.position.y - pos.y)
local x = abs(entity_position.x - pos.x)
local y = abs(entity_position.y - pos.y)
if x * x + y * y < 36 then
skip = true
break
@@ -57,7 +57,7 @@ local function rename_station(event)
-- Find the closest resource
local icon = ""
local item_name = ""
local bounding_box = entity.bounding_box
local bounding_box = entity.bounding_box --[[@as ExpUtil_AABB.Box]]
local resources = entity.surface.find_entities_filtered{ position = entity.position, radius = 250, type = "resource" }
if #resources > 0 then
local closest_recourse --- @type LuaEntity?
@@ -66,8 +66,9 @@ local function rename_station(event)
-- Check which recourse is closest
for _, resource in ipairs(resources) do
local dx = px - resource.bounding_box.left_top.x
local dy = py - resource.bounding_box.left_top.y
local resource_box = resource.bounding_box --[[@as ExpUtil_AABB.Box]]
local dx = px - resource_box.left_top.x
local dy = py - resource_box.left_top.y
local distance = (dx * dx) + (dy * dy)
if distance < closest_distance then
closest_distance = distance
@@ -78,7 +79,8 @@ local function rename_station(event)
-- Set the item name and icon
if closest_recourse then
item_name = closest_recourse.name:gsub("^%l", string.upper):gsub("-", " ") -- remove dashes and making first letter capital
local product = closest_recourse.prototype.mineable_properties.products[1]
local products = assert(closest_recourse.prototype.mineable_properties.products)
local product = assert(products[1])
icon = string.format("[img=%s.%s]", product.type, product.name)
end
end
+15 -9
View File
@@ -42,12 +42,14 @@ end
--- @param options FlyingText.create_above_entity_param
function FlyingText.create_above_entity(options)
local entity = assert(options.target_entity, "A target entity is required")
local size_y = entity.bounding_box.left_top.y - entity.bounding_box.right_bottom.y
local bounding_box = entity.bounding_box --[[@as ExpUtil_AABB.Box]]
local size_y = bounding_box.left_top.y - bounding_box.right_bottom.y
local position = entity.position --[[@as MapPosition.struct]]
local offset = options.offset or { x = 0, y = 0 }
options.position = {
x = offset.x + entity.position.x,
y = offset.y + entity.position.y + size_y * 0.25,
x = offset.x + position.x,
y = offset.y + position.y + size_y * 0.25,
}
FlyingText.create(options)
@@ -62,12 +64,14 @@ end
function FlyingText.create_above_player(options)
local player = assert(options.target_player, "A target player is required")
local entity = player.character; if not entity then return end
local size_y = entity.bounding_box.left_top.y - entity.bounding_box.right_bottom.y
local bounding_box = entity.bounding_box --[[@as ExpUtil_AABB.Box]]
local size_y = bounding_box.left_top.y - bounding_box.right_bottom.y
local position = entity.position --[[@as MapPosition.struct]]
local offset = options.offset or { x = 0, y = 0 }
options.position = {
x = offset.x + entity.position.x,
y = offset.y + entity.position.y + size_y * 0.25,
x = offset.x + position.x,
y = offset.y + position.y + size_y * 0.25,
}
FlyingText.create(options)
@@ -82,13 +86,15 @@ end
function FlyingText.create_as_player(options)
local player = assert(options.target_player, "A target player is required")
local entity = player.character; if not entity then return end
local size_y = entity.bounding_box.left_top.y - entity.bounding_box.right_bottom.y
local bounding_box = entity.bounding_box --[[@as ExpUtil_AABB.Box]]
local size_y = bounding_box.left_top.y - bounding_box.right_bottom.y
local position = entity.position --[[@as MapPosition.struct]]
local offset = options.offset or { x = 0, y = 0 }
options.color = player.chat_color
options.position = {
x = offset.x + entity.position.x,
y = offset.y + entity.position.y + size_y * 0.25,
x = offset.x + position.x,
y = offset.y + position.y + size_y * 0.25,
}
FlyingText.create(options)