Skip to content

Commit

Permalink
v2.0.16 commit 1
Browse files Browse the repository at this point in the history
  • Loading branch information
ILLISIS committed Dec 16, 2024
1 parent 1a72990 commit 4fa8911
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 29 deletions.
2 changes: 1 addition & 1 deletion changelog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---------------------------------------------------------------------------------------------------
Version: 2.0.16
Date: 2024-12-??
Date: 2024-12-20
Changes:
- [mods] Added remote interface to register custom Constructrons and Service Stations.
- [Maraxis] Added compatibility for both land and underwater Constructron types.
Expand Down
25 changes: 24 additions & 1 deletion control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ end))
--===========================================================================--

local ensure_storages = function()
storage.constructron_names = storage.constructron_names or { ["constructron"] = true, ["constructron-rocket-powered"] = true}
storage.station_names = storage.station_names or { ["service_station"] = true }
--
storage.registered_entities = storage.registered_entities or {}
storage.constructron_statuses = storage.constructron_statuses or {}
--
Expand Down Expand Up @@ -418,24 +421,44 @@ local function remote_entities_built(entities)
end

-- register a new spider-vehicle as a constructron
-- note: ensure that all on_built events are handled.
---@param entity LuaEntity
---@param surface_index uint
local function remote_ctron_built(entity, surface_index)
if not entity.type == "spider-vehicle" then return end
if not storage.constructron_names[entity.name] then
storage.constructron_names[entity.name] = true
end
entity_proc.new_ctron_built(entity, surface_index)
end

-- register a new roboport as a constructron service station
-- note: ensure that all on_built events are handled.
---@param entity LuaEntity
---@param surface_index uint
local function remote_station_built(entity, surface_index)
if not entity.type == "roboport" then return end
if not storage.station_names[entity.name] then
storage.station_names[entity.name] = true
end
entity_proc.new_station_built(entity, surface_index)
end

-- remote interface to get constructron names
local function remote_get_ctron_names()
return storage.constructron_names
end

-- remote interface to get station names
local function remote_get_station_names()
return storage.station_names
end

remote.add_interface("ctron", {
["scan-entity"] = remote_entity_built,
["scan-entities"] = remote_entities_built,
["register-ctron"] = remote_ctron_built,
["register-station"] = remote_station_built
["get-ctron-names"] = remote_get_ctron_names,
["register-station"] = remote_station_built,
["get-station-names"] = remote_get_station_names,
})
2 changes: 1 addition & 1 deletion info.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Constructron-Continued",
"version": "2.0.15",
"version": "2.0.16",
"title": "Constructron-Continued",
"author": "ILLISIS",
"homepage": "https://github.com/ILLISIS/Constructron-continued/",
Expand Down
48 changes: 26 additions & 22 deletions script/entity_processor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ local ev = defines.events
entity_proc.new_ctron_built = function(entity, surface_index)
local registration_number = script.register_on_object_destroyed(entity)
storage.registered_entities[registration_number] = {
name = "constructron",
name = entity.name,
surface = surface_index
}
util_func.paint_constructron(entity, 'idle')
Expand All @@ -42,7 +42,7 @@ entity_proc.new_station_built = function(entity, surface_index)
local registration_number = script.register_on_object_destroyed(entity)
storage.service_stations[entity.unit_number] = entity
storage.registered_entities[registration_number] = {
name = "service_station",
name = entity.name,
surface = surface_index
}
storage.stations_count[surface_index] = storage.stations_count[surface_index] + 1
Expand All @@ -62,6 +62,12 @@ entity_proc.new_station_built = function(entity, surface_index)
storage.station_requests[entity.unit_number] = {}
end

local entity_types = {
["entity-ghost"] = true,
["tile-ghost"] = true,
["item-request-proxy"] = true
}

-- for entity creation
---@param event
---| EventData.on_built_entity
Expand All @@ -71,13 +77,13 @@ entity_proc.on_built_entity = function(event)
local entity = event.entity
local entity_type = entity.type
local surface_index = entity.surface.index
if storage.construction_job_toggle[surface_index] and (entity_type == 'entity-ghost' or entity_type == 'tile-ghost' or entity_type == 'item-request-proxy') then
if storage.construction_job_toggle[surface_index] and entity_types[entity_type] then
storage.construction_index = storage.construction_index + 1
storage.construction_entities[storage.construction_index] = entity
storage.entity_proc_trigger = true -- there is something to do start processing
elseif entity.name == 'constructron' or entity.name == "constructron-rocket-powered" then -- register constructron
elseif storage.constructron_names[entity.name] then -- register constructron
entity_proc.new_ctron_built(entity, surface_index)
elseif entity.name == "service_station" then -- register service station
elseif storage.station_names[entity.name] then -- register service station
entity_proc.new_station_built(entity, surface_index)
end
end
Expand Down Expand Up @@ -171,12 +177,12 @@ end,
script.on_event(ev.on_entity_cloned, function(event)
local entity = event.destination
local surface_index = entity.surface.index
if entity.name == 'constructron' or entity.name == "constructron-rocket-powered" then
if storage.constructron_names[entity.name] then
local registration_number = script.register_on_object_destroyed(entity)
util_func.paint_constructron(entity, 'idle')
storage.constructrons[entity.unit_number] = entity
storage.registered_entities[registration_number] = {
name = "constructron",
name = entity.name,
surface = surface_index
}
storage.constructron_statuses[entity.unit_number] = { busy = false }
Expand All @@ -191,11 +197,11 @@ script.on_event(ev.on_entity_cloned, function(event)
auto_target_without_gunner = true,
auto_target_with_gunner = true
}
elseif entity.name == "service_station" then
elseif storage.station_names[entity.name] then
local registration_number = script.register_on_object_destroyed(entity)
storage.service_stations[entity.unit_number] = entity
storage.registered_entities[registration_number] = {
name = "service_station",
name = entity.name,
surface = surface_index
}
storage.stations_count[surface_index] = storage.stations_count[surface_index] + 1
Expand All @@ -210,17 +216,16 @@ script.on_event(ev.on_entity_cloned, function(event)
end
end,
{
{filter = "name", name = "constructron", mode = "or"},
{filter = "name", name = "constructron-rocket-powered", mode = "or"},
{filter = "name", name = "service_station", mode = "or"},
{filter = "type", type = "spider-vehicle", mode = "or"},
{filter = "type", type = "roboport", mode = "or"},
{filter = "name", name = "ctron-combinator", mode = "or"}
})

script.on_event(ev.script_raised_teleported, function(event)
local entity = event.entity
local surface_index = entity.surface_index
if not (surface_index == event.old_surface_index) then return end
if entity.name == 'constructron' or entity.name == "constructron-rocket-powered" then
if storage.constructron_names[entity.name] then
util_func.paint_constructron(entity, 'idle')
storage.constructrons_count[event.old_surface_index] = math.max((storage.constructrons_count[event.old_surface_index] - 1), 0) -- update constructron count on old surface
storage.constructrons_count[entity.surface_index] = storage.constructrons_count[entity.surface_index] + 1 -- update constructron count on new surface
Expand All @@ -240,7 +245,7 @@ script.on_event(ev.script_raised_teleported, function(event)
if (storage.stations_count[entity.surface_index] > 0) then
storage.managed_surfaces[surface_index] = entity.surface.name
end
elseif entity.name == "service_station" then
elseif storage.station_names[entity.name] then
storage.stations_count[event.old_surface_index] = storage.stations_count[event.old_surface_index] - 1
storage.stations_count[entity.surface_index] = storage.stations_count[event.old_surface_index] + 1
-- configure surface management
Expand All @@ -252,9 +257,8 @@ script.on_event(ev.script_raised_teleported, function(event)
end
end,
{
{filter = "name", name = "constructron", mode = "or"},
{filter = "name", name = "constructron-rocket-powered", mode = "or"},
{filter = "name", name = "service_station", mode = "or"},
{filter = "type", type = "spider-vehicle", mode = "or"},
{filter = "type", type = "roboport", mode = "or"},
{filter = "name", name = "ctron-combinator", mode = "or"}
})

Expand All @@ -265,7 +269,7 @@ entity_proc.on_object_destroyed = function(event)
if not storage.registered_entities[event.registration_number] then return end
local removed_entity = storage.registered_entities[event.registration_number]
local surface_index = removed_entity.surface
if removed_entity.name == "constructron" or removed_entity.name == "constructron-rocket-powered" then
if storage.constructron_names[removed_entity.name] then
if game.surfaces[surface_index] then
storage.constructrons_count[surface_index] = math.max((storage.constructrons_count[surface_index] - 1), 0) -- update constructron count
if not storage.constructron_statuses[event.useful_id]["busy"] then -- if constructron is not busy
Expand All @@ -281,7 +285,7 @@ entity_proc.on_object_destroyed = function(event)
end
-- combinator management
util_func.update_ctron_combinator_signals(removed_entity.surface)
elseif removed_entity.name == "service_station" then
elseif storage.station_names[removed_entity.name] then
if game.surfaces[surface_index] then
storage.stations_count[surface_index] = storage.stations_count[surface_index] - 1
end
Expand All @@ -305,9 +309,9 @@ end
script.on_event(ev.on_object_destroyed, entity_proc.on_object_destroyed)

script.on_event(ev.script_raised_destroy, entity_proc.on_object_destroyed, {
{filter = "name", name = "constructron", mode = "or"},
{filter = "name", name = "constructron-rocket-powered", mode = "or"},
{filter = "name", name = "service_station", mode = "or"}
{filter = "type", type = "spider-vehicle", mode = "or"},
{filter = "type", type = "roboport", mode = "or"},
{filter = "name", name = "ctron-combinator", mode = "or"}
})

script.on_event(ev.on_sector_scanned, function(event)
Expand Down
8 changes: 4 additions & 4 deletions script/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ end)

script.on_event(defines.events.on_gui_opened, function(event)
local entity = event.entity
if entity and (entity.name == "constructron" or entity.name == "constructron-rocket-powered") then
if entity and storage.constructron_names[entity.name] then
local player = game.players[event.player_index]
if player.gui.relative.ctron_worker_frame then return end
local ctron_frame = player.gui.relative.add{
Expand Down Expand Up @@ -136,7 +136,7 @@ script.on_event(defines.events.on_gui_opened, function(event)
job_index = job.job_index
}
}
elseif entity and (entity.name == "service_station") then
elseif entity and storage.station_names[entity.name] then
local player = game.players[event.player_index]
if player.gui.relative.ctron_station_frame then return end
local ctron_station_frame = player.gui.relative.add{
Expand Down Expand Up @@ -188,7 +188,7 @@ script.on_event(defines.events.on_gui_closed, function(event)
local player = game.players[event.player_index]
if event.element and event.element.name == "ctron_main_window" then
gui_handlers.close_main_window(player)
elseif event.entity and (event.entity.name == "constructron" or event.entity.name == "constructron-rocket-powered") then
elseif event.entity and storage.constructron_names[event.entity.name] then
if player.gui.relative.ctron_worker_frame then
player.gui.relative.ctron_worker_frame.destroy()
if player.gui.screen.ctron_job_window then
Expand All @@ -198,7 +198,7 @@ script.on_event(defines.events.on_gui_closed, function(event)
if player.gui.relative.ctron_invis_flow then
player.gui.relative.ctron_invis_flow.destroy()
end
elseif event.entity and (event.entity.name == "service_station") then
elseif event.entity and storage.station_names[event.entity.name] then
if player.gui.relative.ctron_station_frame then
player.gui.relative.ctron_station_frame.destroy()
if player.gui.screen.ctron_cargo_window then
Expand Down

0 comments on commit 4fa8911

Please sign in to comment.