Skip to content

Commit 26cb9f7

Browse files
author
notnotmelon
committed
fix: Fixed that space factory buildings could get "stuck" in the space platform hub with no way to construct them. Resolves #259
1 parent fc8d450 commit 26cb9f7

3 files changed

Lines changed: 59 additions & 1 deletion

File tree

lib/string.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,11 @@ end
2626
string.starts_with = function(s, start)
2727
return s:sub(1, #start) == start
2828
end
29+
30+
---Returns a boolean indicating if the first string ends with the second string.
31+
---@param s string
32+
---@param ending string
33+
---@return boolean
34+
string.ends_with = function(s, ending)
35+
return ending == "" or s:sub(-#ending) == ending
36+
end

locale/en/locale.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,4 @@ ceiling=
180180
181181
[command-help-message]
182182
give-lost-factory-buildings=Gives you all picked-up factory buildings in item form, except those already in your player inventory. Using this command if the factory building items aren't actually gone may cause multiple items in your world to refer to the same factory building. These extra items will vanish with an "Invalid factory data!" message when placed.
183+
could-not-determine-with-certainty=[img=utility/warning_icon] [font=default-semibold][color=#23db30]Factorissimo:[/color][/font] Could not determine with 100% certainty which factory to unpack for ghost at __1__.\nRandomly choose factory at __2__. [font=default-semibold][color=#db23c3][Certainty was __3__%][/color][/font]

script/factory-buildings.lua

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,52 @@ local function handle_factory_placed(entity, tags)
794794
entity.destroy()
795795
end
796796

797+
-- https://github.com/notnotmelon/factorissimo-2-notnotmelon/issues/259
798+
local function try_randomly_tag_an_itemized_factory_from_space_platform_hub(entity_ghost, player_index)
799+
if not player_index then return end
800+
if entity_ghost.tags then return end
801+
local player = game.get_player(player_index)
802+
if not player then return end
803+
local space_platform = entity_ghost.surface.platform
804+
if not space_platform then return end
805+
local hub = space_platform.hub
806+
if not hub then return end
807+
local cursor_ghost = player.cursor_ghost
808+
if not cursor_ghost then return end
809+
local cursor_ghost_name = player.cursor_ghost.name.name
810+
if not has_layout(cursor_ghost_name) then return end
811+
if not cursor_ghost_name:ends_with("-instantiated") then return end
812+
local inventory = hub.get_inventory(defines.inventory.hub_main)
813+
if not inventory then return end
814+
if inventory.is_empty() or inventory.get_item_count(cursor_ghost_name) == 0 then return end
815+
816+
local random_indicies = {}
817+
for i = 1, #inventory do
818+
local stack = inventory[i]
819+
if stack and stack.valid_for_read and stack.name == cursor_ghost_name then
820+
if stack.type == "item-with-tags" and stack.tags and stack.tags.id then
821+
local factory = storage.saved_factories[stack.tags.id]
822+
if factory and factory.inside_surface.name == "space-factory-floor" then
823+
random_indicies[#random_indicies+1] = i
824+
end
825+
end
826+
end
827+
end
828+
829+
if #random_indicies == 0 then return end
830+
local index = random_indicies[math.random(1, #random_indicies)]
831+
local stack = inventory[index]
832+
entity_ghost.tags = stack.tags
833+
local factory = storage.saved_factories[entity_ghost.tags.id]
834+
835+
if #random_indicies > 1 then
836+
local certainty = string.format("%.2f", 100 / #random_indicies)
837+
local gps_1 = entity_ghost.gps_tag
838+
local gps_2 = string.format("[gps=%s,%s,%s]", factory.inside_x, factory.inside_y, factory.inside_surface.name)
839+
game.print{"command-help-message.could-not-determine-with-certainty", gps_1, gps_2, certainty}
840+
end
841+
end
842+
797843
factorissimo.on_event(factorissimo.events.on_built(), function(event)
798844
local entity = event.entity
799845
if not entity.valid then return end
@@ -808,8 +854,11 @@ factorissimo.on_event(factorissimo.events.on_built(), function(event)
808854

809855
if entity.type ~= "entity-ghost" then return end
810856
local ghost_name = entity.ghost_name
857+
if not has_layout(ghost_name) then return end
858+
859+
try_randomly_tag_an_itemized_factory_from_space_platform_hub(entity, event.player_index)
811860

812-
if has_layout(ghost_name) and entity.tags then
861+
if entity.tags then
813862
local copied_from_factory = storage.factories[entity.tags.id]
814863
if copied_from_factory then
815864
factorissimo.update_overlay(copied_from_factory, entity)

0 commit comments

Comments
 (0)