fusion-lab/control.lua

changeset 19
adbc5c74f279
parent 10
101603241531
equal deleted inserted replaced
18:6088a99984dc 19:adbc5c74f279
1 ---@class FusionLabStorageData
2 ---@field heat_interface LuaEntity heat buffer shadowing this entity
3
4 ---@class FusionLabStorage
5 ---@field fusion_labs table<LuaEntity, FusionLabStorageData>?
6 storage = {}
7
8 ---@param heat_interface LuaEntity
9 local function init_heat_interface(heat_interface)
10 heat_interface.custom_status = {diode=defines.entity_status_diode.green, label={"entity-status.normal"}}
11 end
12
1 local function init_storage() 13 local function init_storage()
2 if storage.fusion_labs == nil 14 if storage.fusion_labs == nil
3 then 15 then
4 storage.fusion_labs = {} 16 storage.fusion_labs = {}
5 local num_labs = 0 17 local num_labs = 0
23 position=lab.position, 35 position=lab.position,
24 force=lab.force, 36 force=lab.force,
25 }} 37 }}
26 heat_interfaces[1].temperature = 15 38 heat_interfaces[1].temperature = 15
27 heat_interfaces[1].destructible = false 39 heat_interfaces[1].destructible = false
40 heat_interfaces[1].active = false
41 init_heat_interface(heat_interfaces[1])
28 end 42 end
29 storage.fusion_labs[lab] = { 43 storage.fusion_labs[lab] = {
30 heat_interface = heat_interfaces[1], 44 heat_interface = heat_interfaces[1],
31 } 45 }
32 end 46 end
35 end 49 end
36 end 50 end
37 51
38 local function cleanup_labs() 52 local function cleanup_labs()
39 local new_storage_fusion_labs = {} 53 local new_storage_fusion_labs = {}
40 for lab, lab_info in pairs(storage.fusion_labs) 54 for lab, lab_info in pairs(storage.fusion_labs or {})
41 do 55 do
42 if lab.valid 56 if lab.valid
43 then 57 then
44 new_storage_fusion_labs[lab] = lab_info 58 new_storage_fusion_labs[lab] = lab_info
45 end 59 end
48 end 62 end
49 63
50 script.on_nth_tick(31, function(event) 64 script.on_nth_tick(31, function(event)
51 init_storage() 65 init_storage()
52 local need_cleanup = false 66 local need_cleanup = false
53 for lab, lab_info in pairs(storage.fusion_labs) 67 for lab, lab_info in pairs(storage.fusion_labs or {})
54 do 68 do
55 if not lab.valid 69 if not lab.valid
56 then 70 then
57 need_cleanup = true 71 need_cleanup = true
58 elseif lab.status == defines.entity_status.working 72 elseif lab.status == defines.entity_status.working
59 then 73 then
60 local heat_interface = lab_info.heat_interface 74 local heat_interface = lab_info.heat_interface
75
76 if not heat_interface.valid
77 then
78 -- (╯°□°)╯︵ ┻━┻
79 game.print("[Fusion lab] Heat interface data corrupt, rebuilding")
80 storage.fusion_labs = nil
81 init_storage()
82 return
83 end
84
61 lab.minable = (heat_interface.temperature <= 900) 85 lab.minable = (heat_interface.temperature <= 900)
62 -- A cryogenic plant cooling hot fluoroketone voids 1320kW 86 -- A cryogenic plant cooling hot fluoroketone voids 1320kW
63 -- of heat energy, so the fusion lab with no modules just happens 87 -- of heat energy, so the fusion lab with no modules just happens
64 -- to conveniently radiate exactly half of that energy rate. 88 -- to conveniently radiate exactly half of that energy rate.
65 -- 89 --
78 if heat_interface.temperature > 710.0 102 if heat_interface.temperature > 710.0
79 then 103 then
80 lab.surface.create_entity{ 104 lab.surface.create_entity{
81 name = 'crash-site-fire-flame', 105 name = 'crash-site-fire-flame',
82 position={lab.position.x + 3.5 * (math.random() - 0.5), lab.position.y + 3.5 * (math.random() - 0.5)}, 106 position={lab.position.x + 3.5 * (math.random() - 0.5), lab.position.y + 3.5 * (math.random() - 0.5)},
83 force=game.forces.enemy, 107 force=game.forces.neutral,
84 } 108 }
85 end 109 end
86 --[[
87 if heat_interface.temperature > 750.0
88 then
89 lab.die()
90 end
91 ]]--
92 end 110 end
93 end 111 end
94 if need_cleanup 112 if need_cleanup
95 then 113 then
96 cleanup_labs() 114 cleanup_labs()
97 end 115 end
116 for _, player in pairs(game.players)
117 do
118 if player.gui.relative.fusion_lab_gui and player.opened and player.opened.name == "fusion-lab"
119 then
120 for lab, lab_info in pairs(storage.fusion_labs)
121 do
122 if lab == player.opened
123 then
124 local heat_interface = lab_info.heat_interface
125 local gui = player.gui.relative.fusion_lab_gui
126 gui.heat.value = heat_interface.temperature / 700
127 gui.heat.caption = {"format-degrees", string.format("%.2f", heat_interface.temperature)}
128 end
129 end
130 end
131 end
98 end) 132 end)
99
100 133
101 script.on_event(defines.events.on_entity_died, function(event) 134 script.on_event(defines.events.on_entity_died, function(event)
102 local lab = event.entity 135 local lab = event.entity
103 -- we can't rely on storage here 136 event.entity.force = game.forces.neutral
137 -- we can't rely on storage here; search for the heat interface on the map
104 local heat_interfaces = lab.surface.find_entities_filtered{ 138 local heat_interfaces = lab.surface.find_entities_filtered{
105 name='fusion-lab-heat-interface', 139 name = 'fusion-lab-heat-interface',
106 position=lab.position, 140 position = lab.position,
107 limit = 1, 141 limit = 1,
108 } 142 }
109 if #heat_interfaces > 0 143 if #heat_interfaces > 0
110 then 144 then
111 local heat_interface = heat_interfaces[1] 145 local heat_interface = heat_interfaces[1]
112 if heat_interface.temperature > 700 146 if heat_interface.temperature > 700
113 then 147 then
114 for _, ofs in pairs{{0, 0}, {5, 0}, {-5, 0}, {0, 5}, {0, -5}, {1.5, 1.5}, {-1.5, 1.5}, {-1.5, -1.5}, {1.5, -1.5}} 148 for _, ofs in pairs{
149 {0, 0},
150 {5, 0},
151 {-5, 0},
152 {0, 5},
153 {0, -5},
154 {1.5, 1.5},
155 {-1.5, 1.5},
156 {-1.5, -1.5},
157 {1.5, -1.5}}
115 do 158 do
116 local pos = {x = lab.position.x + ofs[1], y = lab.position.y + ofs[2]} 159 local pos = {
160 x = lab.position.x + ofs[1],
161 y = lab.position.y + ofs[2]}
117 lab.surface.create_entity{ 162 lab.surface.create_entity{
118 name = 'artillery-projectile', 163 name = 'artillery-projectile',
119 position = pos, 164 position = pos,
120 target = pos, 165 target = pos,
121 force = game.forces.enemy, 166 force = game.forces.neutral,
167 cause = event.entity,
122 } 168 }
123 end 169 end
124 end 170 end
125 heat_interface.destroy() 171 heat_interface.destroy()
126 end 172 end
127 end, {{filter="name", name="fusion-lab"}}) 173 end, {{filter="name", name="fusion-lab"}})
128 174
175 ---@param event EventData
129 local function on_fusion_lab_mined(event) 176 local function on_fusion_lab_mined(event)
130 init_storage() 177 init_storage()
131 local entity = event.entity 178 local entity = event.entity ---@type LuaEntity
132 for _, heat_interface in pairs(entity.surface.find_entities_filtered{ 179 for _, heat_interface in pairs(entity.surface.find_entities_filtered{
133 name='fusion-lab-heat-interface', 180 name='fusion-lab-heat-interface',
134 position = entity.position 181 position = entity.position
135 }) 182 })
136 do 183 do
146 position=lab.position, 193 position=lab.position,
147 force=lab.force, 194 force=lab.force,
148 } 195 }
149 heat_interface.temperature = 15 196 heat_interface.temperature = 15
150 heat_interface.destructible = false 197 heat_interface.destructible = false
198 heat_interface.active = false
151 storage.fusion_labs[lab] = {heat_interface = heat_interface} 199 storage.fusion_labs[lab] = {heat_interface = heat_interface}
152 end 200 end
153 201
154 script.on_event(defines.events.on_player_mined_entity, on_fusion_lab_mined, {{filter="name", name="fusion-lab"}}) 202 script.on_event(defines.events.on_player_mined_entity, on_fusion_lab_mined, {{filter="name", name="fusion-lab"}})
155 script.on_event(defines.events.on_robot_mined_entity, on_fusion_lab_mined, {{filter="name", name="fusion-lab"}}) 203 script.on_event(defines.events.on_robot_mined_entity, on_fusion_lab_mined, {{filter="name", name="fusion-lab"}})

mercurial