mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-08-13 00:45:11 +09:00
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:
@@ -15,11 +15,11 @@ local abs = math.abs
|
|||||||
local commands = {}
|
local commands = {}
|
||||||
|
|
||||||
--- @param player LuaPlayer
|
--- @param player LuaPlayer
|
||||||
--- @param area BoundingBox
|
--- @param area ExpUtil_AABB.Box
|
||||||
--- @return boolean
|
--- @return boolean
|
||||||
local function location_break(player, area)
|
local function location_break(player, area)
|
||||||
local surface = player.surface -- Allow remote view
|
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
|
if is_charted(surface, { x = floor(area.left_top.x / 32), y = floor(area.left_top.y / 32) }) then
|
||||||
return true
|
return true
|
||||||
elseif is_charted(surface, { x = floor(area.left_top.x / 32), y = floor(area.right_bottom.y / 32) }) then
|
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 count = 0
|
||||||
local hits = {} --- @type MapPosition[]
|
local hits = {} --- @type MapPosition.struct[]
|
||||||
for _, entity in ipairs(entities) do
|
for _, entity in ipairs(entities) do
|
||||||
local skip = false
|
local skip = false
|
||||||
|
|
||||||
|
local entity_position = entity.position --[[@as MapPosition.struct]]
|
||||||
for _, pos in ipairs(hits) do
|
for _, pos in ipairs(hits) do
|
||||||
local x = abs(entity.position.x - pos.x)
|
local x = abs(entity_position.x - pos.x)
|
||||||
local y = abs(entity.position.y - pos.y)
|
local y = abs(entity_position.y - pos.y)
|
||||||
if x * x + y * y < 36 then
|
if x * x + y * y < 36 then
|
||||||
skip = true
|
skip = true
|
||||||
break
|
break
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ local function rename_station(event)
|
|||||||
-- Find the closest resource
|
-- Find the closest resource
|
||||||
local icon = ""
|
local icon = ""
|
||||||
local item_name = ""
|
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" }
|
local resources = entity.surface.find_entities_filtered{ position = entity.position, radius = 250, type = "resource" }
|
||||||
if #resources > 0 then
|
if #resources > 0 then
|
||||||
local closest_recourse --- @type LuaEntity?
|
local closest_recourse --- @type LuaEntity?
|
||||||
@@ -66,8 +66,9 @@ local function rename_station(event)
|
|||||||
|
|
||||||
-- Check which recourse is closest
|
-- Check which recourse is closest
|
||||||
for _, resource in ipairs(resources) do
|
for _, resource in ipairs(resources) do
|
||||||
local dx = px - resource.bounding_box.left_top.x
|
local resource_box = resource.bounding_box --[[@as ExpUtil_AABB.Box]]
|
||||||
local dy = py - resource.bounding_box.left_top.y
|
local dx = px - resource_box.left_top.x
|
||||||
|
local dy = py - resource_box.left_top.y
|
||||||
local distance = (dx * dx) + (dy * dy)
|
local distance = (dx * dx) + (dy * dy)
|
||||||
if distance < closest_distance then
|
if distance < closest_distance then
|
||||||
closest_distance = distance
|
closest_distance = distance
|
||||||
@@ -78,7 +79,8 @@ local function rename_station(event)
|
|||||||
-- Set the item name and icon
|
-- Set the item name and icon
|
||||||
if closest_recourse then
|
if closest_recourse then
|
||||||
item_name = closest_recourse.name:gsub("^%l", string.upper):gsub("-", " ") -- remove dashes and making first letter capital
|
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)
|
icon = string.format("[img=%s.%s]", product.type, product.name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -42,12 +42,14 @@ end
|
|||||||
--- @param options FlyingText.create_above_entity_param
|
--- @param options FlyingText.create_above_entity_param
|
||||||
function FlyingText.create_above_entity(options)
|
function FlyingText.create_above_entity(options)
|
||||||
local entity = assert(options.target_entity, "A target entity is required")
|
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 }
|
local offset = options.offset or { x = 0, y = 0 }
|
||||||
|
|
||||||
options.position = {
|
options.position = {
|
||||||
x = offset.x + entity.position.x,
|
x = offset.x + position.x,
|
||||||
y = offset.y + entity.position.y + size_y * 0.25,
|
y = offset.y + position.y + size_y * 0.25,
|
||||||
}
|
}
|
||||||
|
|
||||||
FlyingText.create(options)
|
FlyingText.create(options)
|
||||||
@@ -62,12 +64,14 @@ end
|
|||||||
function FlyingText.create_above_player(options)
|
function FlyingText.create_above_player(options)
|
||||||
local player = assert(options.target_player, "A target player is required")
|
local player = assert(options.target_player, "A target player is required")
|
||||||
local entity = player.character; if not entity then return end
|
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 }
|
local offset = options.offset or { x = 0, y = 0 }
|
||||||
|
|
||||||
options.position = {
|
options.position = {
|
||||||
x = offset.x + entity.position.x,
|
x = offset.x + position.x,
|
||||||
y = offset.y + entity.position.y + size_y * 0.25,
|
y = offset.y + position.y + size_y * 0.25,
|
||||||
}
|
}
|
||||||
|
|
||||||
FlyingText.create(options)
|
FlyingText.create(options)
|
||||||
@@ -82,13 +86,15 @@ end
|
|||||||
function FlyingText.create_as_player(options)
|
function FlyingText.create_as_player(options)
|
||||||
local player = assert(options.target_player, "A target player is required")
|
local player = assert(options.target_player, "A target player is required")
|
||||||
local entity = player.character; if not entity then return end
|
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 }
|
local offset = options.offset or { x = 0, y = 0 }
|
||||||
|
|
||||||
options.color = player.chat_color
|
options.color = player.chat_color
|
||||||
options.position = {
|
options.position = {
|
||||||
x = offset.x + entity.position.x,
|
x = offset.x + position.x,
|
||||||
y = offset.y + entity.position.y + size_y * 0.25,
|
y = offset.y + position.y + size_y * 0.25,
|
||||||
}
|
}
|
||||||
|
|
||||||
FlyingText.create(options)
|
FlyingText.create(options)
|
||||||
|
|||||||
Reference in New Issue
Block a user