Skip to content

Commit e200794

Browse files
committed
Initial commit
1 parent 859c1c0 commit e200794

File tree

6 files changed

+204
-0
lines changed

6 files changed

+204
-0
lines changed

changelog.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
##0.1.0##
2+
+ Initial release

control.lua

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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)

data-updates.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
local name = "fluid-temperature-combinator"
2+
3+
local entity = util.table.deepcopy(data.raw["constant-combinator"]["constant-combinator"])
4+
entity.name = name
5+
entity.minable.result = name
6+
entity.item_slot_count = 1
7+
8+
local item = util.table.deepcopy(data.raw["item"]["constant-combinator"])
9+
item.name = name
10+
item.order = "b[combinators]-cb[fluid-temperature-combinator]"
11+
item.place_result = name
12+
13+
local recipe = util.table.deepcopy(data.raw["recipe"]["constant-combinator"])
14+
recipe.name = name
15+
recipe.result = name
16+
17+
table.insert(data.raw.technology["circuit-network"].effects,
18+
{
19+
type = "unlock-recipe",
20+
recipe = "fluid-temperature-combinator"
21+
}
22+
)
23+
24+
data:extend({entity, item, recipe,
25+
{
26+
type = "virtual-signal",
27+
name = "fluid-temperature",
28+
icon = "__fluid-temperature-combinator__/graphics/temp.png",
29+
subgroup = "virtual-signal",
30+
order = "yyy"
31+
},
32+
})

graphics/temp.png

678 Bytes
Loading

info.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "fluid-temperature-combinator",
3+
"version": "0.1.0",
4+
"factorio_version": "0.14",
5+
"title": "Fluid Temperature Combinator",
6+
"author": "theRustyKnife",
7+
"description": "A combinator that can read the temperature of a fluid in adjacent tank.",
8+
"homepage": "https://mods.factorio.com/mods/theRustyKnife/fluid-temperature-combinator",
9+
"date": "25.10.2016",
10+
"dependencies": []
11+
}

locale/en/en.cfg

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[entity-description]
2+
fluid-temperature-combinator=Reads the temperature of fluid in an adjacent tank
3+
4+
[entity-name]
5+
fluid-temperature-combinator=Fluid temperature combinator
6+
7+
[item-description]
8+
fluid-temperature-combinator=Reads the temperature of fluid in an adjacent tank
9+
10+
[item-name]
11+
fluid-temperature-combinator=Fluid temperature combinator
12+
13+
[recipe-name]
14+
fluid-temperature-combinator=Fluid temperature combinator
15+
16+
[virtual-signal-name]
17+
fluid-temperature=Fluid temperature

0 commit comments

Comments
 (0)