z_mirror/Krastorio2

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

Caleb HeuerClean up legacy event parameter041f29c4

santichu95/patch-1
4.5 KiB165 linesraw
1local util = require("scripts.util")
2
3--- @class ShelterData
4--- @field container LuaEntity
5--- @field electric LuaEntity
6--- @field light LuaEntity
7--- @field former_spawn_point MapPosition
8
9--- @param force LuaForce
10local function init_force(force)
11  if not storage.shelter then
12    return
13  end
14  storage.shelter.forces[force.index] = {}
15end
16
17local function spawn_flying_texts()
18  for unit_number, entity in pairs(storage.shelter.inactive) do
19    if entity.valid then
20      util.entity_flying_text(entity, { "message.kr-shelter-is-inactive" }, { r = 1 })
21    else
22      storage.shelter.inactive[unit_number] = nil
23    end
24  end
25end
26
27--- @param e EntityBuiltEvent
28local function on_entity_built(e)
29  local entity = e.entity or e.destination
30  if not entity.valid then
31    return
32  end
33  if entity.name ~= "kr-shelter-container" and entity.name ~= "kr-shelter-plus-container" then
34    return
35  end
36
37  local force = entity.force
38  local surface = entity.surface
39  local force_shelters = storage.shelter.forces[force.index]
40
41  if force_shelters[surface.index] then
42    local name = entity.name:gsub("kr%-", "kr-inactive-"):gsub("%-container", "")
43    local position = entity.position
44    local force = entity.force
45    local player = entity.last_user
46    local surface = entity.surface
47    entity.destroy()
48
49    local new_entity = surface.create_entity({
50      name = name,
51      position = position,
52      force = force,
53      player = player,
54      create_build_effect_smoke = false,
55      raise_built = true,
56    })
57    if new_entity and new_entity.valid then
58      storage.shelter.inactive[new_entity.unit_number] = new_entity
59    end
60    return
61  end
62
63  -- Build entities
64  local _, _, base_name = string.find(entity.name, "^(.*)%-container")
65  --- @type ShelterData
66  local shelter_data = { container = entity }
67
68  for key, suffix in pairs({ electric = "", light = "-light" }) do
69    local name = base_name .. suffix
70    shelter_data[key] = surface.create_entity({
71      name = name,
72      position = entity.position,
73      force = force,
74      player = entity.last_user,
75      create_build_effect_smoke = false,
76      raise_built = true,
77    })
78  end
79
80  -- Set spawn point
81  shelter_data.former_spawn_point = force.get_spawn_position(surface)
82  force.set_spawn_position({ x = entity.position.x, y = entity.position.y + 3.5 }, surface)
83
84  force_shelters[surface.index] = shelter_data
85end
86
87--- @param e EntityDestroyedEvent
88local function on_entity_destroyed(e)
89  local entity = e.entity
90  if not entity or not entity.valid then
91    return
92  end
93  if entity.name ~= "kr-shelter-container" and entity.name ~= "kr-shelter-plus-container" then
94    return
95  end
96
97  local inactive = storage.shelter.inactive[entity.unit_number]
98  if inactive then
99    storage.shelter.inactive[entity.unit_number] = nil
100  else
101    local force = entity.force
102
103    local force_shelters = storage.shelter.forces[force.index]
104    if not force_shelters then
105      return
106    end
107
108    local surface_index = entity.surface.index
109    local shelter_data = force_shelters[surface_index]
110    if not shelter_data then
111      return
112    end
113
114    -- Destroy entities
115    shelter_data.electric.destroy()
116    shelter_data.light.destroy()
117    force_shelters[surface_index] = nil
118
119    -- Restore former spawn point
120    force.set_spawn_position(shelter_data.former_spawn_point, surface_index)
121  end
122end
123
124--- @param e EventData.on_force_created
125local function on_force_created(e)
126  if e.force.valid then
127    init_force(e.force)
128  end
129end
130
131local shelter = {}
132
133function shelter.on_init()
134  storage.shelter = {
135    --- @type table<uint, table<uint, ShelterData>>
136    forces = {},
137    --- @type table<uint, LuaEntity>
138    inactive = {},
139  }
140
141  for _, force in pairs(game.forces) do
142    init_force(force)
143  end
144end
145
146shelter.events = {
147  [defines.events.on_built_entity] = on_entity_built,
148  [defines.events.on_entity_cloned] = on_entity_built,
149  [defines.events.on_entity_died] = on_entity_destroyed,
150  [defines.events.on_force_created] = on_force_created,
151  [defines.events.on_player_mined_entity] = on_entity_destroyed,
152  [defines.events.on_robot_built_entity] = on_entity_built,
153  [defines.events.on_robot_mined_entity] = on_entity_destroyed,
154  [defines.events.script_raised_built] = on_entity_built,
155  [defines.events.script_raised_destroy] = on_entity_destroyed,
156  [defines.events.script_raised_revive] = on_entity_built,
157  [defines.events.on_space_platform_built_entity] = on_entity_built,
158  [defines.events.on_space_platform_mined_entity] = on_entity_destroyed,
159}
160
161shelter.on_nth_tick = {
162  [180] = spawn_flying_texts,
163}
164
165return shelter