fusion-lab/control.lua

changeset 10
101603241531
equal deleted inserted replaced
9:b0f85ff1a503 10:101603241531
1 local function init_storage()
2 if storage.fusion_labs == nil
3 then
4 storage.fusion_labs = {}
5 local num_labs = 0
6 for _, surface in pairs(game.surfaces)
7 do
8 for _, lab in pairs(surface.find_entities_filtered{name="fusion-lab"})
9 do
10 if lab.valid
11 then
12 num_labs = num_labs + 1
13 local heat_interfaces = surface.find_entities_filtered{
14 name='fusion-lab-heat-interface',
15 position=lab.position,
16 limit = 1,
17 }
18 local heat_interface
19 if #heat_interfaces == 0
20 then
21 heat_interfaces = {lab.surface.create_entity{
22 name='fusion-lab-heat-interface',
23 position=lab.position,
24 force=lab.force,
25 }}
26 heat_interfaces[1].temperature = 15
27 heat_interfaces[1].destructible = false
28 end
29 storage.fusion_labs[lab] = {
30 heat_interface = heat_interfaces[1],
31 }
32 end
33 end
34 end
35 end
36 end
37
38 local function cleanup_labs()
39 local new_storage_fusion_labs = {}
40 for lab, lab_info in pairs(storage.fusion_labs)
41 do
42 if lab.valid
43 then
44 new_storage_fusion_labs[lab] = lab_info
45 end
46 end
47 storage.fusion_labs = new_storage_fusion_labs
48 end
49
50 script.on_nth_tick(31, function(event)
51 init_storage()
52 local need_cleanup = false
53 for lab, lab_info in pairs(storage.fusion_labs)
54 do
55 if not lab.valid
56 then
57 need_cleanup = true
58 elseif lab.status == defines.entity_status.working
59 then
60 local heat_interface = lab_info.heat_interface
61 lab.minable = (heat_interface.temperature <= 900)
62 -- A cryogenic plant cooling hot fluoroketone voids 1320kW
63 -- of heat energy, so the fusion lab with no modules just happens
64 -- to conveniently radiate exactly half of that energy rate.
65 --
66 -- The 0.341 here is 31/60*0.66 = 0.341 degrees of heat increase
67 -- every 31 ticks, for 0.66 ℃/sec. it's written out as 0.341 to
68 -- avoid potential floating point rounding errors.
69 heat_interface.temperature = heat_interface.temperature + (1 + lab.consumption_bonus) * 0.341
70 if heat_interface.temperature > 600
71 then
72 lab.surface.create_entity{
73 name = 'fusion-lab-smoke-source',
74 position={lab.position.x + 3.5 * (math.random() - 0.5), lab.position.y + 3.5 * (math.random() - 0.5)},
75 force=lab.force,
76 }
77 end
78 if heat_interface.temperature > 710.0
79 then
80 lab.surface.create_entity{
81 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)},
83 force=game.forces.enemy,
84 }
85 end
86 --[[
87 if heat_interface.temperature > 750.0
88 then
89 lab.die()
90 end
91 ]]--
92 end
93 end
94 if need_cleanup
95 then
96 cleanup_labs()
97 end
98 end)
99
100
101 script.on_event(defines.events.on_entity_died, function(event)
102 local lab = event.entity
103 -- we can't rely on storage here
104 local heat_interfaces = lab.surface.find_entities_filtered{
105 name='fusion-lab-heat-interface',
106 position=lab.position,
107 limit = 1,
108 }
109 if #heat_interfaces > 0
110 then
111 local heat_interface = heat_interfaces[1]
112 if heat_interface.temperature > 700
113 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}}
115 do
116 local pos = {x = lab.position.x + ofs[1], y = lab.position.y + ofs[2]}
117 lab.surface.create_entity{
118 name = 'artillery-projectile',
119 position = pos,
120 target = pos,
121 force = game.forces.enemy,
122 }
123 end
124 end
125 heat_interface.destroy()
126 end
127 end, {{filter="name", name="fusion-lab"}})
128
129 local function on_fusion_lab_mined(event)
130 init_storage()
131 local entity = event.entity
132 for _, heat_interface in pairs(entity.surface.find_entities_filtered{
133 name='fusion-lab-heat-interface',
134 position = entity.position
135 })
136 do
137 heat_interface.destroy()
138 end
139 end
140
141 local function on_fusion_lab_built(event)
142 init_storage()
143 local lab = event.entity
144 heat_interface = lab.surface.create_entity{
145 name='fusion-lab-heat-interface',
146 position=lab.position,
147 force=lab.force,
148 }
149 heat_interface.temperature = 15
150 heat_interface.destructible = false
151 storage.fusion_labs[lab] = {heat_interface = heat_interface}
152 end
153
154 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"}})
156 script.on_event(defines.events.on_space_platform_mined_entity, on_fusion_lab_mined, {{filter="name", name="fusion-lab"}})
157 script.on_event(defines.events.on_built_entity, on_fusion_lab_built, {{filter="name", name="fusion-lab"}})
158 script.on_event(defines.events.on_robot_built_entity, on_fusion_lab_built, {{filter="name", name="fusion-lab"}})
159 script.on_event(defines.events.on_space_platform_built_entity, on_fusion_lab_built, {{filter="name", name="fusion-lab"}})

mercurial