Fix use of created_entity

This commit is contained in:
Cooldude2606
2024-12-24 15:21:30 +00:00
parent 6e806b55de
commit 9a173edea7
3 changed files with 17 additions and 12 deletions

View File

@@ -15,6 +15,8 @@ local directions = {
["SW"] = 0.875,
}
--- @param entity LuaEntity
--- @return string
local function get_direction(entity)
local angle = math.atan2(entity.position.y, entity.position.x) / math.pi
for direction, required_angle in pairs(directions) do
@@ -29,19 +31,20 @@ end
local custom_string = " *"
local custom_string_len = #custom_string
--- @param event EventData.on_built_entity | EventData.on_robot_built_entity
local function station_name_changer(event)
local entity = event.created_entity
local entity = event.entity
local name = entity.name
if name == "entity-ghost" then
if entity.ghost_name ~= "train-stop" then return end
local backername = entity.backer_name
if backername ~= "" then
entity.backer_name = backername .. custom_string
local backer_name = entity.backer_name
if backer_name ~= "" then
entity.backer_name = backer_name .. custom_string
end
elseif name == "train-stop" then -- only do the event if its a train stop
local backername = entity.backer_name
if backername:sub(-custom_string_len) == custom_string then
entity.backer_name = backername:sub(1, -custom_string_len - 1)
local backer_name = entity.backer_name or ""
if backer_name:sub(-custom_string_len) == custom_string then
entity.backer_name = backer_name:sub(1, -custom_string_len - 1)
return
end
@@ -49,14 +52,14 @@ local function station_name_changer(event)
-- expanded box for recourse search:
local bounding2 = { { bounding_box.left_top.x - 100, bounding_box.left_top.y - 100 }, { bounding_box.right_bottom.x + 100, bounding_box.right_bottom.y + 100 } }
-- gets all resources in bounding_box2:
local recourses = game.surfaces[1].find_entities_filtered{ area = bounding2, type = "resource" }
if #recourses > 0 then -- save cpu time if their are no recourses in bounding_box2
local resources = game.surfaces[1].find_entities_filtered{ area = bounding2, type = "resource" }
if #resources > 0 then -- save cpu time if their are no resources in bounding_box2
local closest_distance
local px, py = bounding_box.left_top.x, bounding_box.left_top.y
local recourse_closed
-- Check which recourse is closest
for i, item in ipairs(recourses) do
for i, item in ipairs(resources) do
local dx, dy = px - item.bounding_box.left_top.x, py - item.bounding_box.left_top.y
local distance = (dx * dx) + (dy * dy)
if not closest_distance or distance < closest_distance then

View File

@@ -195,8 +195,9 @@ Event.add(defines.events.on_rocket_launch_ordered, function(event)
end)
--- Adds a silo to the list when it is built
--- @param event EventData.on_built_entity | EventData.on_robot_built_entity
local function on_built(event)
local entity = event.created_entity
local entity = event.entity
if entity.valid and entity.name == "rocket-silo" then
local force = entity.force
local force_name = force.name

View File

@@ -574,8 +574,9 @@ Event.on_nth_tick(150, function()
end)
--- Adds a silo to the list when it is built
--- @param event EventData.on_built_entity | EventData.on_robot_built_entity
local function on_built(event)
local entity = event.created_entity
local entity = event.entity
if entity.valid and entity.name == "rocket-silo" then
update_rocket_gui_progress(entity.force.name)
end