z_mirror/Krastorio2

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

Caleb HeuerRemove references to inserter hotkey, which is now a standalone mod21723abc

discrete-steel-pipes
4.5 KiB166 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  --- @cast entity LuaEntity
37
38  local force = entity.force
39  local surface = entity.surface
40  local force_shelters = storage.shelter.forces[force.index]
41
42  if force_shelters[surface.index] then
43    local name = entity.name:gsub("kr%-", "kr-inactive-"):gsub("%-container", "")
44    local position = entity.position
45    local force = entity.force
46    local player = entity.last_user
47    local surface = entity.surface
48    entity.destroy()
49
50    local new_entity = surface.create_entity({
51      name = name,
52      position = position,
53      force = force,
54      player = player,
55      create_build_effect_smoke = false,
56      raise_built = true,
57    })
58    if new_entity and new_entity.valid then
59      storage.shelter.inactive[new_entity.unit_number] = new_entity
60    end
61    return
62  end
63
64  -- Build entities
65  local _, _, base_name = string.find(entity.name, "^(.*)%-container")
66  --- @type ShelterData
67  local shelter_data = { container = entity }
68
69  for key, suffix in pairs({ electric = "", light = "-light" }) do
70    local name = base_name .. suffix
71    shelter_data[key] = surface.create_entity({
72      name = name,
73      position = entity.position,
74      force = force,
75      player = entity.last_user,
76      create_build_effect_smoke = false,
77      raise_built = true,
78    })
79  end
80
81  -- Set spawn point
82  shelter_data.former_spawn_point = force.get_spawn_position(surface)
83  force.set_spawn_position({ x = entity.position.x, y = entity.position.y + 3.5 }, surface)
84
85  force_shelters[surface.index] = shelter_data
86end
87
88--- @param e EntityDestroyedEvent
89local function on_entity_destroyed(e)
90  local entity = e.entity
91  if not entity or not entity.valid then
92    return
93  end
94  if entity.name ~= "kr-shelter-container" and entity.name ~= "kr-shelter-plus-container" then
95    return
96  end
97
98  local inactive = storage.shelter.inactive[entity.unit_number]
99  if inactive then
100    storage.shelter.inactive[entity.unit_number] = nil
101  else
102    local force = entity.force
103
104    local force_shelters = storage.shelter.forces[force.index]
105    if not force_shelters then
106      return
107    end
108
109    local surface_index = entity.surface.index
110    local shelter_data = force_shelters[surface_index]
111    if not shelter_data then
112      return
113    end
114
115    -- Destroy entities
116    shelter_data.electric.destroy()
117    shelter_data.light.destroy()
118    force_shelters[surface_index] = nil
119
120    -- Restore former spawn point
121    force.set_spawn_position(shelter_data.former_spawn_point, surface_index)
122  end
123end
124
125--- @param e EventData.on_force_created
126local function on_force_created(e)
127  if e.force.valid then
128    init_force(e.force)
129  end
130end
131
132local shelter = {}
133
134function shelter.on_init()
135  storage.shelter = {
136    --- @type table<uint, table<uint, ShelterData>>
137    forces = {},
138    --- @type table<uint, LuaEntity>
139    inactive = {},
140  }
141
142  for _, force in pairs(game.forces) do
143    init_force(force)
144  end
145end
146
147shelter.events = {
148  [defines.events.on_built_entity] = on_entity_built,
149  [defines.events.on_entity_cloned] = on_entity_built,
150  [defines.events.on_entity_died] = on_entity_destroyed,
151  [defines.events.on_force_created] = on_force_created,
152  [defines.events.on_player_mined_entity] = on_entity_destroyed,
153  [defines.events.on_robot_built_entity] = on_entity_built,
154  [defines.events.on_robot_mined_entity] = on_entity_destroyed,
155  [defines.events.script_raised_built] = on_entity_built,
156  [defines.events.script_raised_destroy] = on_entity_destroyed,
157  [defines.events.script_raised_revive] = on_entity_built,
158  [defines.events.on_space_platform_built_entity] = on_entity_built,
159  [defines.events.on_space_platform_mined_entity] = on_entity_destroyed,
160}
161
162shelter.on_nth_tick = {
163  [180] = spawn_flying_texts,
164}
165
166return shelter