1+ local refresh_rate = 60
2+
3+ local look_offset = 0.5
4+ local look_distance = 1
5+ local tank_surroundings_check_distance = 2
6+ local function get_tank (entity )
7+ local position = entity .position
8+ local direction = entity .direction
9+
10+ local area
11+
12+ if direction == defines .direction .north then
13+ area = {{position .x - look_offset , position .y - look_distance }, {position .x + look_offset , position .y - look_distance }}
14+ elseif direction == defines .direction .west then
15+ area = {{position .x - look_distance - 0.1 , position .y - look_offset }, {position .x - look_distance + 0.1 , position .y + look_offset }}
16+ elseif direction == defines .direction .south then
17+ area = {{position .x - look_offset , position .y + look_distance }, {position .x + look_offset , position .y + look_distance }}
18+ elseif direction == defines .direction .east then
19+ area = {{position .x + look_distance - 0.1 , position .y - look_offset }, {position .x + look_distance + 0.1 , position .y + look_offset }}
20+ end
21+
22+ return entity .surface .find_entities_filtered {area = area , type = " storage-tank" }[1 ]
23+ end
24+
25+ local function find_in_global (combinator )
26+ for i = 0 , refresh_rate - 1 do
27+ for ei , c in pairs (global .combinators [i ]) do
28+ if c .entity == combinator then return c , i , ei end
29+ end
30+ end
31+ end
32+
33+ local function on_built (event )
34+ local entity = event .created_entity
35+
36+ if entity .name == " fluid-temperature-combinator" then
37+ local ei
38+ local n
39+ for i = 0 , refresh_rate - 1 do
40+ if not n or n >= # global .combinators [i ] then
41+ ei = i
42+ n = # global .combinators [i ]
43+ end
44+ end
45+ entity .rotatable = true
46+ table.insert (global .combinators [ei ], {entity = entity , tank = get_tank (entity )})
47+ end
48+ if entity .type == " storage-tank" then
49+ local area = {
50+ {entity .position .x - tank_surroundings_check_distance , entity .position .y - tank_surroundings_check_distance },
51+ {entity .position .x + tank_surroundings_check_distance , entity .position .y + tank_surroundings_check_distance }
52+ }
53+ local combinators = entity .surface .find_entities_filtered {area = area , name = " fluid-temperature-combinator" }
54+ for _ , combinator in pairs (combinators ) do
55+ find_in_global (combinator ).tank = get_tank (combinator )
56+ end
57+ end
58+ end
59+
60+ local function on_tick (event )
61+ for _ , combinator in pairs (global .combinators [event .tick % refresh_rate ]) do
62+ local count = 0
63+ if combinator .tank and combinator .tank .valid and combinator .tank .fluidbox [1 ] then
64+ count = combinator .tank .fluidbox [1 ].temperature
65+ end
66+ combinator .entity .get_or_create_control_behavior ().parameters = {
67+ enabled = true ,
68+ parameters = {
69+ {
70+ signal = {type = " virtual" , name = " fluid-temperature" },
71+ count = math.floor (count ),
72+ index = 1
73+ }
74+ }
75+ }
76+ end
77+ end
78+
79+ local function on_rotated (event )
80+ local entity = event .entity
81+ if entity .name == " fluid-temperature-combinator" then
82+ find_in_global (entity ).tank = get_tank (entity )
83+ end
84+ end
85+
86+ local function on_destroyed (event )
87+ local entity = event .entity
88+
89+ if entity .name == " fluid-temperature-combinator" then
90+ for i = 0 , refresh_rate - 1 do
91+ for ei , combinator in pairs (global .combinators [i ]) do
92+ if combinator .entity == entity then
93+ table.remove (global .combinators [i ], ei )
94+ return
95+ end
96+ end
97+ end
98+ end
99+ if entity .type == " storage-tank" then
100+ local area = {
101+ {entity .position .x - tank_surroundings_check_distance , entity .position .y - tank_surroundings_check_distance },
102+ {entity .position .x + tank_surroundings_check_distance , entity .position .y + tank_surroundings_check_distance }
103+ }
104+ local combinators = entity .surface .find_entities_filtered {area = area , name = " fluid-temperature-combinator" }
105+ for _ , combinator in pairs (combinators ) do
106+ find_in_global (combinator ).tank = get_tank (combinator )
107+ end
108+ end
109+ end
110+
111+ script .on_init (function ()
112+ global .combinators = global .combinators or {}
113+ for i = 0 , refresh_rate - 1 do
114+ global .combinators [i ] = global .combinators [i ] or {}
115+ end
116+ end )
117+
118+ script .on_configuration_changed (function (data )
119+ global .combinators = global .combinators or {}
120+ for i = 0 , refresh_rate - 1 do
121+ global .combinators [i ] = global .combinators [i ] or {}
122+ end
123+
124+ if data .mod_changes [" crafting_combinator" ] then
125+ for _ , force in pairs (game .forces ) do
126+ if force .technologies [" circuit-network" ].researched then
127+ force .recipes [" crafting-combinator" ].enabled = true
128+ end
129+ end
130+ end
131+ end )
132+
133+ script .on_event (defines .events .on_built_entity , on_built )
134+ script .on_event (defines .events .on_robot_built_entity , on_built )
135+
136+ script .on_event (defines .events .on_preplayer_mined_item , on_destroyed )
137+ script .on_event (defines .events .on_robot_pre_mined , on_destroyed )
138+ script .on_event (defines .events .on_entity_died , on_destroyed )
139+
140+ script .on_event (defines .events .on_tick , on_tick )
141+
142+ script .on_event (defines .events .on_player_rotated_entity , on_rotated )
0 commit comments