From deff5ac464483409db3b6a6e55a91d07419fa5e6 Mon Sep 17 00:00:00 2001 From: Lars Corbijn Date: Wed, 10 Jan 2024 10:40:41 +0100 Subject: [PATCH 1/2] Harden belt detection code Loading with Bobs & Angles caused a crash on a nil at this point. --- prototypes/technology/belts.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prototypes/technology/belts.lua b/prototypes/technology/belts.lua index 887790d..b3b199c 100644 --- a/prototypes/technology/belts.lua +++ b/prototypes/technology/belts.lua @@ -9,7 +9,7 @@ print(serpent.line(belt_names)) for _, tech in pairs(data.raw["technology"]) do if tech.effects then for _, effect in pairs(tech.effects) do - if effect.type == "unlock-recipe" then + if effect.type == "unlock-recipe" and data.raw.recipe[effect.recipe] then local has_recipe = "" -- the single recipe result is a string and it is the name of the belt local result_name = data.raw.recipe[effect.recipe].result From f69f4eb46ee54876b4b10b7b0794d1438fc9f7d2 Mon Sep 17 00:00:00 2001 From: Lars Corbijn Date: Wed, 10 Jan 2024 10:48:33 +0100 Subject: [PATCH 2/2] Rework backup external belt tier tech generation The previous implementation had several issues: * The slowest belt detection was broken * It always created a prerequisite on the upgrade of the external connections to the previous tier of belt, even if this was the slowest belt tier. This change reworks the creation by creating an ordering of the belts by speed. The first entry is the slowest and thus does not need an upgrade, the sencond one does not need a prerequisite. --- prototypes/technology/belts.lua | 47 ++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/prototypes/technology/belts.lua b/prototypes/technology/belts.lua index b3b199c..f741914 100644 --- a/prototypes/technology/belts.lua +++ b/prototypes/technology/belts.lua @@ -4,7 +4,17 @@ for _, belt in pairs(data.raw["transport-belt"]) do belt_names[belt.name] = true end -print(serpent.line(belt_names)) +-- Create an ordering of the belts by speed +-- to be used in the backup code +local belts_by_speed = {} +for belt_name, _ in pairs(belt_names) do + belts_by_speed[#belts_by_speed+1] = belt_name +end +local ordering = function (b1, b2) + return data.raw["transport-belt"][b1].speed < data.raw["transport-belt"][b2].speed +end +table.sort(belts_by_speed, ordering) + for _, tech in pairs(data.raw["technology"]) do if tech.effects then @@ -65,21 +75,28 @@ for _, tech in pairs(data.raw["technology"]) do end --- for the remaining techs, we need to add the belts as level 0 techs, EXCEPT for the slowest one -local slowest_belt = nil +-- For belt types where we do not have a natural parent technology we +-- create a separate level 0 technology. Two special cases: +-- 1. We start with external connections using the slowest belt tech, +-- so no technology upgrading to this level is added. +-- 2. For the upgrade to the first tier there is no prerequisite, as we +-- do not know about a technology unlocking the belt itself, nor is +-- there a technology that upgrades the external connection to the +-- previous belt tier (as that is the starting tier) for belt_name, _ in pairs(belt_names) do - if slowest_belt == nil then - slowest_belt = belt_name - else - if data.raw["transport-belt"][belt_name].speed < data.raw["transport-belt"][belt_name].speed then - slowest_belt = belt_name - end + -- Find speed index + local index = nil + for i, v in ipairs(belts_by_speed) do + if v == belt_name then index = i end end -end - --- now for the remaining belts, add them as level 0 techs -for belt_name in pairs(belt_names) do - if belt_name ~= slowest_belt then + if index > 1 then -- Skip slowest belt + log("Adding tech for " .. belt_name) + prerequisites = {} + if index > 2 then + -- There is a tech upgrading to 1 tier lower belts + local prev_belt_name = belts_by_speed[index-1] + prerequisites = {"f2pl-belt-" .. prev_belt_name} + end local item = data.raw.item[belt_name] data:extend { { type = "technology", @@ -87,7 +104,7 @@ for belt_name in pairs(belt_names) do icon = item.icon, icon_size = item.icon_size, icon_mipmaps = item.icon_mipmaps, - prerequisites = { "f2pl-belt-" .. slowest_belt }, + prerequisites = prerequisites, unit = { count = 100, ingredients = { { "automation-science-pack", 1 } },