fusion-lab/control.lua

changeset 19
adbc5c74f279
parent 10
101603241531
--- a/fusion-lab/control.lua	Wed Jul 23 16:08:55 2025 +0300
+++ b/fusion-lab/control.lua	Wed Aug 27 10:26:12 2025 +0300
@@ -1,3 +1,15 @@
+---@class FusionLabStorageData
+---@field heat_interface LuaEntity heat buffer shadowing this entity
+
+---@class FusionLabStorage
+---@field fusion_labs table<LuaEntity, FusionLabStorageData>?
+storage = {}
+
+---@param heat_interface LuaEntity
+local function init_heat_interface(heat_interface)
+	heat_interface.custom_status = {diode=defines.entity_status_diode.green, label={"entity-status.normal"}}
+end
+
 local function init_storage()
 	if storage.fusion_labs == nil
 	then
@@ -25,6 +37,8 @@
 						}}
 						heat_interfaces[1].temperature = 15
 						heat_interfaces[1].destructible = false
+						heat_interfaces[1].active = false
+						init_heat_interface(heat_interfaces[1])
 					end
 					storage.fusion_labs[lab] = {
 						heat_interface = heat_interfaces[1],
@@ -37,7 +51,7 @@
 
 local function cleanup_labs()
 	local new_storage_fusion_labs = {}
-	for lab, lab_info in pairs(storage.fusion_labs)
+	for lab, lab_info in pairs(storage.fusion_labs or {})
 	do
 		if lab.valid
 		then
@@ -50,7 +64,7 @@
 script.on_nth_tick(31, function(event)
 	init_storage()
 	local need_cleanup = false
-	for lab, lab_info in pairs(storage.fusion_labs)
+	for lab, lab_info in pairs(storage.fusion_labs or {})
 	do
 		if not lab.valid
 		then
@@ -58,6 +72,16 @@
 		elseif lab.status == defines.entity_status.working
 		then
 			local heat_interface = lab_info.heat_interface
+
+			if not heat_interface.valid
+			then
+				-- (╯°□°)╯︵ ┻━┻
+				game.print("[Fusion lab] Heat interface data corrupt, rebuilding")
+				storage.fusion_labs = nil
+				init_storage()
+				return
+			end
+
 			lab.minable = (heat_interface.temperature <= 900)
 			-- A cryogenic plant cooling hot fluoroketone voids 1320kW
 			-- of heat energy, so the fusion lab with no modules just happens
@@ -80,30 +104,40 @@
 				lab.surface.create_entity{
 					name = 'crash-site-fire-flame',
 					position={lab.position.x + 3.5 * (math.random() - 0.5), lab.position.y + 3.5 * (math.random() - 0.5)},
-					force=game.forces.enemy,
+					force=game.forces.neutral,
 				}
 			end
-			--[[
-			if heat_interface.temperature > 750.0
-			then
-				lab.die()
-			end
-			]]--
 		end
 	end
 	if need_cleanup
 	then
 		cleanup_labs()
 	end
+	for _, player in pairs(game.players)
+	do
+		if player.gui.relative.fusion_lab_gui and player.opened and player.opened.name == "fusion-lab"
+		then
+			for lab, lab_info in pairs(storage.fusion_labs)
+			do
+				if lab == player.opened
+				then
+					local heat_interface = lab_info.heat_interface
+					local gui = player.gui.relative.fusion_lab_gui
+					gui.heat.value = heat_interface.temperature / 700
+					gui.heat.caption = {"format-degrees", string.format("%.2f", heat_interface.temperature)}
+				end
+			end
+		end
+	end
 end)
 
-
 script.on_event(defines.events.on_entity_died, function(event)
 	local lab = event.entity
-	-- we can't rely on storage here
+	event.entity.force = game.forces.neutral
+	-- we can't rely on storage here; search for the heat interface on the map
 	local heat_interfaces = lab.surface.find_entities_filtered{
-		name='fusion-lab-heat-interface',
-		position=lab.position,
+		name = 'fusion-lab-heat-interface',
+		position = lab.position,
 		limit = 1,
 	}
 	if #heat_interfaces > 0
@@ -111,14 +145,26 @@
 		local heat_interface = heat_interfaces[1]
 		if heat_interface.temperature > 700
 		then
-			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}}
+			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}}
 			do
-				local pos = {x = lab.position.x + ofs[1], y = lab.position.y + ofs[2]}
+				local pos = {
+					x = lab.position.x + ofs[1],
+					y = lab.position.y + ofs[2]}
 				lab.surface.create_entity{
 					name = 'artillery-projectile',
 					position = pos,
 					target = pos,
-					force = game.forces.enemy,
+					force = game.forces.neutral,
+					cause = event.entity,
 				}
 			end
 		end
@@ -126,9 +172,10 @@
 	end
 end, {{filter="name", name="fusion-lab"}})
 
+---@param event EventData
 local function on_fusion_lab_mined(event)
 	init_storage()
-	local entity = event.entity
+	local entity = event.entity ---@type LuaEntity
 	for _, heat_interface in pairs(entity.surface.find_entities_filtered{
 			name='fusion-lab-heat-interface',
 			position = entity.position
@@ -148,6 +195,7 @@
 	}
 	heat_interface.temperature = 15
 	heat_interface.destructible = false
+	heat_interface.active = false
 	storage.fusion_labs[lab] = {heat_interface = heat_interface}
 end
 
@@ -156,4 +204,4 @@
 script.on_event(defines.events.on_space_platform_mined_entity, on_fusion_lab_mined, {{filter="name", name="fusion-lab"}})
 script.on_event(defines.events.on_built_entity, on_fusion_lab_built, {{filter="name", name="fusion-lab"}})
 script.on_event(defines.events.on_robot_built_entity, on_fusion_lab_built, {{filter="name", name="fusion-lab"}})
-script.on_event(defines.events.on_space_platform_built_entity, on_fusion_lab_built, {{filter="name", name="fusion-lab"}})
+script.on_event(defines.events.on_space_platform_built_entity, on_fusion_lab_built, {{filter="name", name="fusion-lab"}})
\ No newline at end of file

mercurial