summaryrefslogtreecommitdiff
path: root/flowing_logic.lua
blob: ac01a233e3440f237a1f184eaccde06d4193a430 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
-- This file provides the actual flow logic that makes liquids
-- move through the pipes.

local finite_liquids = minetest.setting_getbool("liquid_finite")
local pipe_liquid_shows_loaded = 1
local max_pressure = 4

if mesecon then
	pipereceptor_on = {
		receptor = {
			state = mesecon.state.on,
			rules = pipeworks.mesecons_rules
		}
	}

	pipereceptor_off = {
		receptor = {
			state = mesecon.state.off,
			rules = pipeworks.mesecons_rules
		}
	}
end

-- check if a valve, sensor, or other X-oriented device
-- has something connected at each end.

function pipeworks.is_device_connected(pos, node, axisdir, fdir_mod4, rotation)
	local fdir = node.param2
	local fdir_mod4_p2 = (fdir+2) % 4

	if rotation == "z" then
		fdir_mod4    = (fdir+1) % 4
		fdir_mod4_p2 = (fdir+3) % 4
	end

	local fdir_to_pos = {
		{x = pos.x+1, y = pos.y, z = pos.z  },
		{x = pos.x,   y = pos.y, z = pos.z-1},
		{x = pos.x-1, y = pos.y, z = pos.z  },
		{x = pos.x,   y = pos.y, z = pos.z+1},
	}

	local pos_adjacent1 = fdir_to_pos[fdir_mod4    + 1]
	local pos_adjacent2 = fdir_to_pos[fdir_mod4_p2 + 1]

	if rotation == "y" then
		pos_adjacent1 = { x=pos.x, y=pos.y+1, z=pos.z }
		pos_adjacent2 = { x=pos.x, y=pos.y-1, z=pos.z }
	end

	local adjacent_node1 = minetest.get_node(pos_adjacent1)
	local adjacent_node2 = minetest.get_node(pos_adjacent2)

	local set1
	local set2

	if string.find(dump(pipeworks.pipe_nodenames), adjacent_node1.name) or
	  (string.find(dump(pipeworks.device_nodenames), adjacent_node1.name) and
	  (adjacent_node1.param2 == fdir_mod4 or adjacent_node1.param2 == fdir_mod4_p2)) then
		set1 = true
	end

	if string.find(dump(pipeworks.pipe_nodenames), adjacent_node2.name) or
	  (string.find(dump(pipeworks.device_nodenames), adjacent_node2.name) and
	  (adjacent_node2.param2 == fdir_mod4 or adjacent_node2.param2 == fdir_mod4_p2)) then
		set2 = true
	end
	return {set1=set1, set2=set2, pos_adjacent1=pos_adjacent1, pos_adjacent2=pos_adjacent2}
end

-- Evaluate and balance liquid in all pipes

minetest.register_abm({
	nodenames = pipeworks.pipe_nodenames, 
	interval = 1,
	chance = 1,
	action = function(pos, node, active_object_count, active_object_count_wider)
		local coords = {
			{x = pos.x,   y = pos.y,   z = pos.z},
			{x = pos.x,   y = pos.y-1, z = pos.z},
			{x = pos.x,   y = pos.y+1, z = pos.z},
			{x = pos.x-1, y = pos.y,   z = pos.z},
			{x = pos.x+1, y = pos.y,   z = pos.z},
			{x = pos.x,   y = pos.y,   z = pos.z-1},
			{x = pos.x,   y = pos.y,   z = pos.z+1},
		}

		local num_connections = 0
		local connection_list = {}
		local total_level = 0
		
		for _,adjacentpos in ipairs(coords) do
			local adjacent_node = minetest.get_node(adjacentpos)
			if adjacent_node and string.find(dump(pipeworks.pipe_nodenames), adjacent_node.name) then

				local node_level = (minetest.get_meta(adjacentpos):get_float("liquid_level")) or 0
				if node_level < 0 then node_level = 0 end

				total_level = total_level + node_level
				num_connections = num_connections + 1
				table.insert(connection_list, adjacentpos)
			end
		end

		local average_level = total_level / num_connections

		for _, connected_pipe_pos in ipairs(connection_list) do

			local newnode
			local connected_pipe = minetest.get_node(connected_pipe_pos)
			local pipe_name = string.match(connected_pipe.name, "pipeworks:pipe_%d.*_")

			if connected_pipe and pipe_name then
				minetest.get_meta(connected_pipe_pos):set_float("liquid_level", average_level)

				if average_level > pipe_liquid_shows_loaded then
					newnode = pipe_name.."loaded"
				else
					newnode = pipe_name.."empty"
				end
			end

			if newnode and connected_pipe.name ~= newnode then 
				minetest.swap_node(connected_pipe_pos, {name = newnode, param2 = connected_pipe.param2})
			end
		end
	end
})

-- Process all pumps in the area

minetest.register_abm({
	nodenames = {"pipeworks:pump_on", "pipeworks:pump_off"},
	interval = 1,
	chance = 1,
	action = function(pos, node, active_object_count, active_object_count_wider)
		local minp =		{x = pos.x-1, y = pos.y-1, z = pos.z-1}
		local maxp =		{x = pos.x+1, y = pos.y, z = pos.z+1}
		local pos_above =	{x = pos.x, y = pos.y+1, z = pos.z}
		local node_above = minetest.get_node(pos_above)
		if not node_above then return end

		local meta = minetest.get_meta(pos_above)
		local node_level_above = meta:get_float("liquid_level")
		if node_level_above == nil then node_level_above = 0 end
		local pipe_name = string.match(node_above.name, "pipeworks:pipe_%d.*_")
						  or (node_above.name == "pipeworks:entry_panel" and node_above.param2 == 13)

		if pipe_name then
			if node.name == "pipeworks:pump_on" then
				local water_nodes = minetest.find_nodes_in_area(minp, maxp, 
									{"default:water_source", "default:water_flowing"})

				if (node_level_above < max_pressure ) and #water_nodes > 1 then
					meta:set_float("liquid_level", node_level_above + 4) -- add water to the pipe
				end
			else
				if node_level_above > 0 then
					meta:set_float("liquid_level", node_level_above - 0.5 ) -- leak the pipe down
				end
			end
		end
	end
})

-- Process all spigots and fountainheads in the area

minetest.register_abm({
	nodenames = {"pipeworks:spigot", "pipeworks:spigot_pouring", "pipeworks:fountainhead"},
	interval = 2,
	chance = 1,
	action = function(pos, node, active_object_count, active_object_count_wider)

		local fdir = node.param2 % 4
		if fdir ~= node.param2 then
			minetest.set_node(pos,{name = node.name, param2 = fdir})
		end

		local pos_below = {x = pos.x, y = pos.y-1, z = pos.z}
		local below_node = minetest.get_node(pos_below)
		if not below_node then return end

		if node.name == "pipeworks:fountainhead" then
			local pos_above = {x = pos.x, y = pos.y+1, z = pos.z}
			local node_above = minetest.get_node(pos_above) 
			if not node_above then return end

			local node_level_below = (minetest.get_meta(pos_below):get_float("liquid_level")) or 0

			if node_level_below > 1
			  and (node_above.name == "air" or node_above.name == "default:water_flowing") then
				minetest.set_node(pos_above, {name = "default:water_source"})
			elseif node_level_below < 0.95 and node_above.name == "default:water_source" then
				minetest.set_node(pos_above, {name = "air"})
			end

			if node_level_below >= 1
			  and (node_above.name == "air" or node_above.name == "default:water_source") then
				minetest.get_meta(pos_below):set_float("liquid_level", node_level_below - 1)
			end
		else
			if below_node.name == "air" or below_node.name == "default:water_flowing"
			  or below_node.name == "default:water_source" then 

				local fdir_to_pos = {
					{x = pos.x,   y = pos.y, z = pos.z+1},
					{x = pos.x+1, y = pos.y, z = pos.z  },
					{x = pos.x,   y = pos.y, z = pos.z-1},
					{x = pos.x-1, y = pos.y, z = pos.z  }
				}

				local pos_adjacent = fdir_to_pos[fdir+1]
				local adjacent_node = minetest.get_node(pos_adjacent)
				if not adjacent_node then return end

				local adjacent_node_level = (minetest.get_meta(pos_adjacent):get_float("liquid_level")) or 0
				local pipe_name = string.match(adjacent_node.name, "pipeworks:pipe_%d.*_")

				if pipe_name and adjacent_node_level > 1
				  and (below_node.name == "air" or below_node.name == "default:water_flowing") then
					minetest.set_node(pos, {name = "pipeworks:spigot_pouring", param2 = fdir})
					minetest.set_node(pos_below, {name = "default:water_source"})
				end

				if (pipe_name and adjacent_node_level < 0.95)
				  or (node.name ~= "pipeworks:spigot" and not pipe_name) then
					minetest.set_node(pos,{name = "pipeworks:spigot", param2 = fdir})
					if below_node.name == "default:water_source" then
						minetest.set_node(pos_below, {name = "air"})
					end
				end

				if adjacent_node_level >= 1
				  and (below_node.name == "air" or below_node.name == "default:water_source") then
					minetest.get_meta(pos_adjacent):set_float("liquid_level", adjacent_node_level - 1)
				end
			end
		end
	end
})

pipeworks.device_nodenames = {}

table.insert(pipeworks.device_nodenames,"pipeworks:valve_on_empty")
table.insert(pipeworks.device_nodenames,"pipeworks:valve_off_empty")
table.insert(pipeworks.device_nodenames,"pipeworks:valve_on_loaded")
table.insert(pipeworks.device_nodenames,"pipeworks:flow_sensor_empty")
table.insert(pipeworks.device_nodenames,"pipeworks:flow_sensor_loaded")
table.insert(pipeworks.device_nodenames,"pipeworks:entry_panel")

minetest.register_abm({
	nodenames = pipeworks.device_nodenames,
	interval = 2,
	chance = 1,
	action = function(pos, node, active_object_count, active_object_count_wider)

		local fdir			= node.param2
		local axisdir		= math.floor(fdir/4)
		local fdir_mod4		= fdir % 4
		local rotation

		if not string.match(node.name, "pipeworks:valve_off") then

			if node.name == "pipeworks:entry_panel" then
				rotation = "z"
				fdir_mod4 = (fdir+1) % 4

				-- reset the panel's facedir to predictable values, if needed

				if axisdir == 5 then
					minetest.swap_node(pos, {name = node.name, param2 = fdir_mod4 })
					return
				elseif axisdir ~= 0 and axisdir ~= 3 then 
					minetest.swap_node(pos, {name = node.name, param2 = 13 })
					return
				end

				if node.param2 == 13 then
					rotation = "y"
				end
			elseif axisdir ~= 0 and axisdir ~= 5 then -- if the device isn't horizontal, force it.
				minetest.swap_node(pos, {name = node.name, param2 = fdir_mod4})
				return
			end

			local connections = pipeworks.is_device_connected(pos, node, axisdir, fdir_mod4, rotation)

			local num_connections = 1
			local my_level = (minetest.get_meta(pos):get_float("liquid_level")) or 0
			local total_level = my_level

			if not connections.set1 and not connections.set2 then return end

			if connections.set1 then 
				num_connections = num_connections + 1
				total_level = total_level + (minetest.get_meta(connections.pos_adjacent1):get_float("liquid_level")) or 0
			end

			if connections.set2 then
				num_connections = num_connections + 1
				total_level = total_level + (minetest.get_meta(connections.pos_adjacent2):get_float("liquid_level")) or 0
			end

			local average_level = total_level / num_connections

			minetest.get_meta(pos):set_float("liquid_level", average_level)

			if connections.set1 then
				minetest.get_meta(connections.pos_adjacent1):set_float("liquid_level", average_level)
			end

			if connections.set2 then
				minetest.get_meta(connections.pos_adjacent2):set_float("liquid_level", average_level)
			end

			if node.name == "pipeworks:flow_sensor_empty" or
			   node.name == "pipeworks:flow_sensor_loaded" then
				local sensor = string.match(node.name, "pipeworks:flow_sensor_")
				local newnode = nil

				if my_level > 1 and (connections.set1 or connections.set2) then
					newnode = sensor.."loaded"
				else
					newnode = sensor.."empty"
				end

				if newnode ~= node.name then
					minetest.swap_node(pos, {name = newnode, param2 = node.param2})
					if mesecon then
						if newnode == "pipeworks:flow_sensor_empty" then
							mesecon.receptor_off(pos, rules) 
						else
							mesecon.receptor_on(pos, rules) 
						end
					end
				end
			end
		end
	end
})