Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Date: ???
- Added an alert to beacon interference. It doesn't last forever, though. Existing interference will be pinned. Resolves https://github.com/pyanodon/pybugreports/issues/1413
- Added an alert sound to beacon interference
- Added a remote API to add or remove buildings from the beacon overloading blacklist
- Fixed that adjusting a beacon's AM or FM value would close the UI
- Fixed that adjusting a beacon's AM or FM value would break the undo queue
- Fixed beacon items being duplicated when manually constructed. Resolves https://github.com/pyanodon/pybugreports/issues/1273
- Fixed beacons not being useable with YAFC and other planner mods. Resolves https://github.com/pyanodon/pybugreports/issues/1449
- Fixed codex search bar saving the search term when switching pages and closing the UI. Resolves https://github.com/pyanodon/pybugreports/issues/1409
Expand Down
2 changes: 1 addition & 1 deletion info.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"dependencies": [
"base >= 2.0.58",
"~ pycoalprocessinggraphics >= 3.0.6",
"~ pypostprocessing >= 3.0.37",
"~ pypostprocessing >= 3.0.42",
"(?) DiscoScience",
"(?) better-victory-screen",
"(?) elevated-rails",
Expand Down
2 changes: 1 addition & 1 deletion migrations/beacon-interference.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
for _, surface in pairs(game.surfaces) do
for _, entity in pairs(surface.find_entities_filtered{typpe = {"assembling-machine", "furnace", "rocket-silo", "mining-drill"}}) do
for _, entity in pairs(surface.find_entities_filtered{type = {"assembling-machine", "furnace", "rocket-silo", "mining-drill"}}) do
local id = storage.beacon_interference_icons[entity.unit_number]
if id then
local rendering_object = rendering.get_object_by_id(id)
Expand Down
107 changes: 42 additions & 65 deletions scripts/beacons.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ end)

local function enable_entity(entity)
local name = entity.name:gsub("%-mk..+", "")
if storage.farms[name] ~= nil then
return
end
if storage.farms[name] ~= nil then return end
entity.active = true
local unit_number = entity.unit_number
local rendering_id = storage.beacon_interference_icons[unit_number]
Expand All @@ -93,9 +91,7 @@ end

local function disable_entity(entity)
local name = entity.name:gsub("%-mk..+", "")
if storage.farms[name] ~= nil then
return
end
if storage.farms[name] ~= nil then return end
entity.active = false
entity.custom_status = {
diode = defines.entity_status_diode.red,
Expand Down Expand Up @@ -147,56 +143,47 @@ end
---Replaces a beacon entity with a different frequency entity
---@param entity LuaEntity
---@param new_beacon_name string
---@param player integer? player whose undo queue this action is added to
---@return LuaEntity?
local function change_frequency(entity, new_beacon_name, player)
if not entity or not entity.valid then
return
end
---@param player_index integer? player whose undo queue this action is added to
---@return LuaEntity? entity the current beacon or whatever it was replaced with
local function change_frequency(entity, new_beacon_name, player_index)
if not entity or not entity.valid then return end
-- Ghost only?
if entity.type == "entity-ghost" then
if entity.ghost_name == new_beacon_name then
return
end
return entity.surface.create_entity {
if entity.ghost_name == new_beacon_name then return entity end
-- Replace entity
local new_entity = entity.surface.create_entity {
name = entity.type,
inner_name = new_beacon_name,
position = entity.position,
force = entity.force_index,
player = player,
quality = entity.quality,
force = entity.force_index,
create_build_effect_smoke = false,
fast_replace = true,
spill = false,
inner_name = new_beacon_name,
}
entity.destroy()
return new_entity
else -- No ghost
if entity.name == new_beacon_name then
return
end
-- Not ghost
if entity.name == new_beacon_name then return entity end
-- Get current effect receivers
local receivers = {}
for _, receiver in pairs(entity.get_beacon_effect_receivers()) do
receivers[receiver.unit_number] = receiver
end
local player = player_index and game.get_player(player_index)
local index, action = py.find_latest_undo_action(player_index, entity, "built-entity")
-- Replace entity
local mineable_result = entity.prototype.mineable_properties.products[1].name
local new_entity = entity.surface.create_entity {
name = new_beacon_name,
position = entity.position,
quality = entity.quality,
force = entity.force_index,
player = player,
fast_replace = true,
spill = false,
player = index and player,
undo_index = index,
create_build_effect_smoke = false
}
if player then
--if there is no inventory attatched to the player theres no need to remove the item
local inventory = game.players[player].get_main_inventory()
if inventory then
inventory.remove {name = mineable_result, amount = 1}
end
entity.destroy()
if action then
-- needs to be done after the construction, ref https://forums.factorio.com/viewtopic.php?t=132714
player.undo_redo_stack.remove_undo_action(index, action) -- remove old action from queue
end
-- Get new effect receivers
for _, receiver in pairs(new_entity.get_beacon_effect_receivers()) do
Expand All @@ -208,9 +195,7 @@ local function change_frequency(entity, new_beacon_name, player)
alert = alert or beacon_check(receiver)
end
if player and alert then
-- player.play_sound{

-- }
player.play_sound{path="utility/alert_destroyed"}
end
if remote.interfaces["cryogenic-distillation"] then
remote.call("cryogenic-distillation", "am_fm_beacon_settings_changed", new_entity)
Expand All @@ -219,31 +204,29 @@ local function change_frequency(entity, new_beacon_name, player)
end
end

-- storing in a local table is fine because it only exists for that tick
local beacon_ghosts = {}

-- If a pipette is placed on top of a ghost, the AM/FM setting is lost without script intervention
Beacons.events.on_pre_build = function(event)
if event.build_mode ~= defines.build_mode.normal then
return
end
if event.build_mode ~= defines.build_mode.normal then return end
local surface = game.get_player(event.player_index).surface
local colliding_ghost = surface.find_entity("entity-ghost", event.position)
if colliding_ghost and our_beacons[colliding_ghost.ghost_name] then
storage.last_beacon_ghost = colliding_ghost.ghost_name
end
beacon_ghosts[event.player_index] = colliding_ghost and our_beacons[colliding_ghost.ghost_name] and colliding_ghost.ghost_name or nil
end

Beacons.events.on_built = function(event)
local entity = event.entity
local ghost = storage.last_beacon_ghost
storage.last_beacon_ghost = nil
local ghost_name = beacon_ghosts[event.player_index]
beacon_ghosts[event.player_index] = nil
if not entity.valid then return end
local alert
if entity.type == "beacon" then
if not our_beacons[entity.name] then return end
-- If the ghost doesn't match the placed entity, then fix it
-- TODO: find a way to have this action properly work with the undo stack
if ghost and entity.name ~= ghost then
change_frequency(entity, ghost)
return
if ghost_name and entity.name ~= ghost_name then
change_frequency(entity, ghost_name, event.player_index)
return -- recievers already updated and player notified of issues
end
for _, reciver in pairs(entity.get_beacon_effect_receivers()) do
alert = alert or beacon_check(reciver)
Expand All @@ -252,9 +235,7 @@ Beacons.events.on_built = function(event)
alert = beacon_check(entity)
end
if event.player_index and alert then
-- game.get_player(event.player_index).play_sound{

-- }
game.get_player(event.player_index).play_sound{path="utility/alert_destroyed"}
end
end

Expand All @@ -265,29 +246,25 @@ Beacons.events.on_destroyed = function(event)

if entity.type == "beacon" then
if not our_beacons[entity.name] then return end
local recivers = entity.get_beacon_effect_receivers()
local recievers = entity.get_beacon_effect_receivers()
entity.destroy() -- is needed for beacon check to remove interference if there was any
for _, reciver in pairs(recivers) do
beacon_check(reciver)
for _, reciever in pairs(recievers) do
beacon_check(reciever)
end
if table_size(recivers) ~= 0 and remote.interfaces["cryogenic-distillation"] then
remote.call("cryogenic-distillation", "am_fm_beacon_destroyed", recivers, recivers[1].surface)
if table_size(recievers) ~= 0 and remote.interfaces["cryogenic-distillation"] then
remote.call("cryogenic-distillation", "am_fm_beacon_destroyed", recievers, recievers[1].surface)
end
end
end

Beacons.events.on_entity_settings_pasted = function(event)
local source, destination = event.source, event.destination
if not source.valid or not destination.valid then
return
end
if not source.valid or not destination.valid then return end

local source_name = (source.type == "entity-ghost" and source.ghost_name) or source.name
local destination_name = (destination.type == "entity-ghost" and destination.ghost_name) or destination.name
-- Not a beacon or not changing
if not (our_beacons[source_name] and our_beacons[destination_name]) or source_name == destination_name then
return
end
if not our_beacons[source_name] or not our_beacons[destination_name] or source_name == destination_name then return end

change_frequency(destination, source_name, event.player_index)
end
Expand Down Expand Up @@ -384,5 +361,5 @@ gui_events[defines.events.on_gui_click]["py_beacon_confirm"] = function(event)
local beacon_name_prefix = our_beacons[init_name] .. "-AM"
local beacon_name = beacon_name_prefix .. gui.AM_flow.AM.slider_value .. "-FM" .. gui.FM_flow.FM.slider_value

change_frequency(beacon, beacon_name, event.player_index)
player.opened = change_frequency(beacon, beacon_name, event.player_index)
end
Loading