z_mirror/Krastorio2

git clone https://git.raiguard.me/z_mirror/Krastorio2

Caleb HeuerFix crash when activating transceiver on old savesec30390a

real-fusion-reactor
5.0 KiB173 linesraw
1local flib_migration = require("__flib__.migration")
2local biter_virus = require("scripts.biter-virus")
3local planetary_teleporter = require("scripts.planetary-teleporter")
4
5local function ensure_turret_force()
6  if not game.forces["kr-internal-turrets"] then
7    game.create_force("kr-internal-turrets")
8  end
9end
10
11local migrations = {}
12
13migrations.on_init = ensure_turret_force
14
15function migrations.on_configuration_changed(e)
16  if flib_migration.on_config_changed(e, migrations.versions) then
17    ensure_turret_force()
18  end
19end
20
21migrations.events = {
22  [defines.events.on_technology_effects_reset] = function(e)
23    if game.finished or game.finished_but_continuing then
24      e.force.technologies["kr-logo"].enabled = true
25    end
26  end,
27}
28
29migrations.versions = {
30  ["1.2.25"] = function()
31    error("Loading Krastorio 2 saves from versions prior to 1.2.25 is no longer supported.")
32  end,
33  ["1.3.0"] = function()
34    if game.finished or game.finished_but_continuing then
35      for _, force in pairs(game.forces) do
36        force.technologies["kr-logo"].enabled = true
37      end
38    end
39  end,
40  ["1.3.8"] = function()
41    -- Clean up any invalid roboport GUIs
42    local new = {}
43    for player_index, player_gui in pairs(storage.roboport_guis) do
44      if player_gui.valid then
45        new[player_index] = player_gui
46      end
47    end
48    storage.roboport_guis = new
49  end,
50  ["1.3.10"] = function()
51    -- Clean up any orphaned internal entities
52    local valid_unit_numbers = {}
53    for _, data in pairs(storage.tesla_coil.towers) do
54      for _, entity in pairs(data.entities) do
55        if entity.valid then
56          valid_unit_numbers[entity.unit_number or 0] = true
57        end
58      end
59    end
60    --- @diagnostic disable-next-line
61    for _, data in pairs(storage.planetary_teleporter.data) do
62      for _, entity in pairs(data.entities) do
63        if entity.valid then
64          valid_unit_numbers[entity.unit_number or 0] = true
65        end
66      end
67    end
68    for _, surface in pairs(game.surfaces) do
69      for _, entity in
70        pairs(surface.find_entities_filtered({
71          name = {
72            "kr-planetary-teleporter-turret",
73            "kr-planetary-teleporter-front-layer",
74            "kr-planetary-teleporter-collision-1",
75            "kr-planetary-teleporter-collision-2",
76            "kr-planetary-teleporter-collision-3",
77            "kr-tesla-coil-turret",
78            "kr-tesla-coil-collision",
79          },
80        }))
81      do
82        if entity.valid and not valid_unit_numbers[entity.unit_number] then
83          entity.destroy()
84        end
85      end
86    end
87  end,
88  ["2.0.0"] = function()
89    storage.creep = nil
90    storage.inserter_has_droplane_gui = nil
91    storage.temp_inserter_settings = nil
92    storage.virus = nil
93    biter_virus.on_init()
94    storage.intergalactic_transceiver.in_cutscene = {}
95
96    --- @diagnostic disable: undefined-field
97
98    if storage.inserter_guis then
99      for _, gui in pairs(storage.inserter_guis) do
100        if gui.valid then
101          gui.destroy()
102        end
103      end
104      storage.inserter_guis = nil
105    end
106
107    if storage.planetary_teleporter and storage.planetary_teleporter.data then
108      --- @type {entity: LuaEntity, name: string?}[]
109      local teleporters = {}
110      for _, data in pairs(storage.planetary_teleporter.data) do
111        local entity = data.entities.base
112        if not entity or not entity.valid then
113          goto continue
114        end
115
116        for key, stored_entity in pairs(data.entities) do
117          if key ~= "base" and stored_entity.valid then
118            stored_entity.destroy()
119          end
120        end
121
122        teleporters[#teleporters + 1] = { entity = entity, name = data.name }
123
124        ::continue::
125      end
126
127      storage.planetary_teleporter = nil
128
129      planetary_teleporter.on_init()
130      for _, data in pairs(teleporters) do
131        planetary_teleporter.migrate_teleporter(data.entity, data.name)
132      end
133    end
134
135    --- @param force LuaForce
136    --- @param name string
137    local function research(force, tech_name)
138      local tech = force.technologies[tech_name]
139      if tech then
140        tech.researched = true
141      end
142    end
143
144    for _, force in pairs(game.forces) do
145      if
146        force.technologies["production-science-pack"].researched
147        or force.technologies["utility-science-pack"].researched
148      then
149        local found_basic = false
150        local found_automation = false
151        for _, surface in pairs(game.surfaces) do
152          if surface.count_entities_filtered({ name = "lab" }) > 0 then
153            found_basic = true
154            found_automation = true
155          elseif surface.count_entities_filtered({ name = { "kr-advanced-lab" } }) > 0 then
156            found_automation = true
157          end
158        end
159        if not found_basic then
160          research(force, "kr-laboratory")
161          research(force, "radar")
162          research(force, "repair-pack")
163        end
164        if not found_automation then
165          research(force, "advanced-combinators")
166          research(force, "kr-fusion-reactor-equipment")
167        end
168      end
169    end
170  end,
171}
172
173return migrations