summaryrefslogtreecommitdiff
path: root/init.lua
blob: 3f477f9229a25bd3fe8eed629880ed2d2ec7b3c9 (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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
-- Pipeworks mod by Vanessa Ezekowitz - 2012-08-05
--
-- Entirely my own code.  This mod merely supplies enough nodes to build 
-- a bunch of pipes in all directions and with all types of junctions
--
-- License: WTFPL
--

-- comment-out the following dofile line to disnable the old pipe nodes.
dofile(minetest.get_modpath("pipeworks").."/oldpipes.lua")

-- tables

minetest.register_alias("pipeworks:pipe", "pipeworks:pipe_000000_empty")

local leftstub = {
	{ -32/64, -2/64, -6/64,   1/64, 2/64, 6/64 },	-- pipe segment against -X face
	{ -32/64, -4/64, -5/64,   1/64, 4/64, 5/64 },
	{ -32/64, -5/64, -4/64,   1/64, 5/64, 4/64 },
	{ -32/64, -6/64, -2/64,   1/64, 6/64, 2/64 },

	{ -32/64, -3/64, -8/64, -30/64, 3/64, 8/64 },	-- (the flange for it)
	{ -32/64, -5/64, -7/64, -30/64, 5/64, 7/64 },
	{ -32/64, -6/64, -6/64, -30/64, 6/64, 6/64 },
	{ -32/64, -7/64, -5/64, -30/64, 7/64, 5/64 },
	{ -32/64, -8/64, -3/64, -30/64, 8/64, 3/64 }
}

local rightstub = {
	{ -1/64, -2/64, -6/64,  32/64, 2/64, 6/64 },	-- pipe segment against +X face
	{ -1/64, -4/64, -5/64,  32/64, 4/64, 5/64 },
	{ -1/64, -5/64, -4/64,  32/64, 5/64, 4/64 },
	{ -1/64, -6/64, -2/64,  32/64, 6/64, 2/64 },

	{ 30/64, -3/64, -8/64, 32/64, 3/64, 8/64 },	-- (the flange for it)
	{ 30/64, -5/64, -7/64, 32/64, 5/64, 7/64 },
	{ 30/64, -6/64, -6/64, 32/64, 6/64, 6/64 },
	{ 30/64, -7/64, -5/64, 32/64, 7/64, 5/64 },
	{ 30/64, -8/64, -3/64, 32/64, 8/64, 3/64 }
}

local bottomstub = {
	{ -2/64, -32/64, -6/64,   2/64, 1/64, 6/64 },	-- pipe segment against -Y face
	{ -4/64, -32/64, -5/64,   4/64, 1/64, 5/64 },
	{ -5/64, -32/64, -4/64,   5/64, 1/64, 4/64 },
	{ -6/64, -32/64, -2/64,   6/64, 1/64, 2/64 },

	{ -3/64, -32/64, -8/64, 3/64, -30/64, 8/64 },	-- (the flange for it)
	{ -5/64, -32/64, -7/64, 5/64, -30/64, 7/64 },
	{ -6/64, -32/64, -6/64, 6/64, -30/64, 6/64 },
	{ -7/64, -32/64, -5/64, 7/64, -30/64, 5/64 },
	{ -8/64, -32/64, -3/64, 8/64, -30/64, 3/64 }
}


local topstub = {
	{ -2/64, -1/64, -6/64,   2/64, 32/64, 6/64 },	-- pipe segment against +Y face
	{ -4/64, -1/64, -5/64,   4/64, 32/64, 5/64 },
	{ -5/64, -1/64, -4/64,   5/64, 32/64, 4/64 },
	{ -6/64, -1/64, -2/64,   6/64, 32/64, 2/64 },

	{ -3/64, 30/64, -8/64, 3/64, 32/64, 8/64 },	-- (the flange for it)
	{ -5/64, 30/64, -7/64, 5/64, 32/64, 7/64 },
	{ -6/64, 30/64, -6/64, 6/64, 32/64, 6/64 },
	{ -7/64, 30/64, -5/64, 7/64, 32/64, 5/64 },
	{ -8/64, 30/64, -3/64, 8/64, 32/64, 3/64 }
}

local frontstub = {
	{ -6/64, -2/64, -32/64,   6/64, 2/64, 1/64 },	-- pipe segment against -Z face
	{ -5/64, -4/64, -32/64,   5/64, 4/64, 1/64 },
	{ -4/64, -5/64, -32/64,   4/64, 5/64, 1/64 },
	{ -2/64, -6/64, -32/64,   2/64, 6/64, 1/64 },

	{ -8/64, -3/64, -32/64, 8/64, 3/64, -30/64 },	-- (the flange for it)
	{ -7/64, -5/64, -32/64, 7/64, 5/64, -30/64 },
	{ -6/64, -6/64, -32/64, 6/64, 6/64, -30/64 },
	{ -5/64, -7/64, -32/64, 5/64, 7/64, -30/64 },
	{ -3/64, -8/64, -32/64, 3/64, 8/64, -30/64 }
}

local backstub = {
	{ -6/64, -2/64, -1/64,   6/64, 2/64, 32/64 },	-- pipe segment against -Z face
	{ -5/64, -4/64, -1/64,   5/64, 4/64, 32/64 },
	{ -4/64, -5/64, -1/64,   4/64, 5/64, 32/64 },
	{ -2/64, -6/64, -1/64,   2/64, 6/64, 32/64 },

	{ -8/64, -3/64, 30/64, 8/64, 3/64, 32/64 },	-- (the flange for it)
	{ -7/64, -5/64, 30/64, 7/64, 5/64, 32/64 },
	{ -6/64, -6/64, 30/64, 6/64, 6/64, 32/64 },
	{ -5/64, -7/64, 30/64, 5/64, 7/64, 32/64 },
	{ -3/64, -8/64, 30/64, 3/64, 8/64, 32/64 }
} 

local selectboxes = {
	{ -32/64,  -8/64,  -8/64,  8/64,  8/64,  8/64 },
	{ -8/64 ,  -8/64,  -8/64, 32/64,  8/64,  8/64 },
	{ -8/64 , -32/64,  -8/64,  8/64,  8/64,  8/64 },
	{ -8/64 ,  -8/64,  -8/64,  8/64, 32/64,  8/64 },
	{ -8/64 ,  -8/64, -32/64,  8/64,  8/64,  8/64 },
	{ -8/64 ,  -8/64,  -8/64,  8/64,  8/64, 32/64 }
}

bendsphere = {	
	{ -4/64, -4/64, -4/64, 4/64, 4/64, 4/64 },
	{ -5/64, -3/64, -3/64, 5/64, 3/64, 3/64 },
	{ -3/64, -5/64, -3/64, 3/64, 5/64, 3/64 },
	{ -3/64, -3/64, -5/64, 3/64, 3/64, 5/64 }
}

pumpbody = {
	{ -6/16, -8/16, -6/16, 6/16, 8/16, 6/16 }
}

valvebody = {
	{ -4/16, -4/16, -4/16, 4/16, 4/16, 4/16 }
}

valvehandle_on = {
	{ -5/16, 4/16, -1/16, 0, 5/16, 1/16 }
}

valvehandle_off = {
	{ -1/16, 4/16, -5/16, 1/16, 5/16, 0 }
}


-- Local Functions

local dbg = function(s)
	if DEBUG == 1 then
		print('[PIPEWORKS] ' .. s)
	end
end

function fix_newpipe_names(table, replacement)
	outtable={}
	for i in ipairs(table) do
		outtable[i]=string.gsub(table[i], "_XXXXX", replacement)
	end

	return outtable
end

local function addbox(t, b)
	for i in ipairs(b)
		do table.insert(t, b[i])
	end
end

local function autoroute(pos, state)

	local nctr = minetest.env:get_node(pos)
	if (string.find(nctr.name, "pipeworks:pipe_") == nil) then return end

	local pxm=0
	local pxp=0
	local pym=0
	local pyp=0
	local pzm=0
	local pzp=0

	local nxm = minetest.env:get_node({ x=pos.x-1, y=pos.y  , z=pos.z   })
	local nxp = minetest.env:get_node({ x=pos.x+1, y=pos.y  , z=pos.z   })
	local nym = minetest.env:get_node({ x=pos.x  , y=pos.y-1, z=pos.z   })
	local nyp = minetest.env:get_node({ x=pos.x  , y=pos.y+1, z=pos.z   })
	local nzm = minetest.env:get_node({ x=pos.x  , y=pos.y  , z=pos.z-1 })
	local nzp = minetest.env:get_node({ x=pos.x  , y=pos.y  , z=pos.z+1 })

	if (string.find(nxm.name, "pipeworks:pipe_") ~= nil) then pxm=1 end
	if (string.find(nxp.name, "pipeworks:pipe_") ~= nil) then pxp=1 end
	if (string.find(nym.name, "pipeworks:pipe_") ~= nil) then pym=1 end
	if (string.find(nyp.name, "pipeworks:pipe_") ~= nil) then pyp=1 end
	if (string.find(nzm.name, "pipeworks:pipe_") ~= nil) then pzm=1 end
	if (string.find(nzp.name, "pipeworks:pipe_") ~= nil) then pzp=1 end

	local nsurround = pxm..pxp..pym..pyp..pzm..pzp
	
	if nsurround == "000000" then nsurround = "110000" end

	minetest.env:add_node(pos, { name = "pipeworks:pipe_"..nsurround..state })
end

-- now define the nodes!

for xm = 0, 1 do
for xp = 0, 1 do
for ym = 0, 1 do
for yp = 0, 1 do
for zm = 0, 1 do
for zp = 0, 1 do
	outboxes = {}
	outsel = {}
	outimgs = {}
	if yp==1 then
		addbox(outboxes, topstub)
		table.insert(outsel, selectboxes[4])
		table.insert(outimgs, "pipeworks_pipe_end.png")
	else
		table.insert(outimgs, "pipeworks_plain.png")
	end
	if ym==1 then
		addbox(outboxes, bottomstub)
		table.insert(outsel, selectboxes[3])
		table.insert(outimgs, "pipeworks_pipe_end.png")
	else
		table.insert(outimgs, "pipeworks_plain.png")
	end
	if xp==1 then
		addbox(outboxes, rightstub)
		table.insert(outsel, selectboxes[2])
		table.insert(outimgs, "pipeworks_pipe_end.png")
	else
		table.insert(outimgs, "pipeworks_plain.png")
	end
	if xm==1 then
		addbox(outboxes, leftstub)
		table.insert(outsel, selectboxes[1])
		table.insert(outimgs, "pipeworks_pipe_end.png")
	else
		table.insert(outimgs, "pipeworks_plain.png")
	end
	if zp==1 then
		addbox(outboxes, backstub)
		table.insert(outsel, selectboxes[6])
		table.insert(outimgs, "pipeworks_pipe_end.png")
	else
		table.insert(outimgs, "pipeworks_plain.png")
	end
	if zm==1 then
		addbox(outboxes, frontstub)
		table.insert(outsel, selectboxes[5])
		table.insert(outimgs, "pipeworks_pipe_end.png")
	else
		table.insert(outimgs, "pipeworks_plain.png")
	end

	jx = xp+xm
	jy = yp+ym
	jz = zp+zm

	if (jx==1 and jy==1 and jz~=1) or (jx==1 and jy~=1 and jz==1) or (jx~= 1 and jy==1 and jz==1) then
		addbox(outboxes, bendsphere)
	end

	if (jx==2 and jy~=2 and jz~=2) then
		table.remove(outimgs, 5)
		table.remove(outimgs, 5)
		table.insert(outimgs, 5, "pipeworks_windowed_XXXXX.png")
		table.insert(outimgs, 5, "pipeworks_windowed_XXXXX.png")
	end

	if (jx~=2 and jy~=2 and jz==2) or (jx~=2 and jy==2 and jz~=2) then
		table.remove(outimgs, 3)
		table.remove(outimgs, 3)
		table.insert(outimgs, 3, "pipeworks_windowed_XXXXX.png")
		table.insert(outimgs, 3, "pipeworks_windowed_XXXXX.png")
	end

	pname = xm..xp..ym..yp..zm..zp

	if pname ~= "110000" then
		pgroups = {snappy=3, pipe=1, not_in_creative_inventory=1}
		pipedesc = "Pipe segment (empty, "..pname..")... You hacker, you."
	else
		pgroups = {snappy=3, pipe=1}
		pipedesc = "Pipe segment"
	end

	minetest.register_node("pipeworks:pipe_"..pname.."_empty", {
		description = pipedesc,
		drawtype = "nodebox",
		tiles = fix_newpipe_names(outimgs, "_empty"),
		paramtype = "light",
		selection_box = {
	             	type = "fixed",
			fixed = outsel
		},
		node_box = {
			type = "fixed",
			fixed = outboxes
		},
		groups = pgroups,
		sounds = default.node_sound_wood_defaults(),
		walkable = true,
		stack_max = 99,
		drop = "pipeworks:pipe_110000_empty",
		after_place_node = function(pos)
			autoroute({ x=pos.x-1, y=pos.y  , z=pos.z   }, "_empty")
			autoroute({ x=pos.x+1, y=pos.y  , z=pos.z   }, "_empty")
			autoroute({ x=pos.x  , y=pos.y-1, z=pos.z   }, "_empty")
			autoroute({ x=pos.x  , y=pos.y+1, z=pos.z   }, "_empty")
			autoroute({ x=pos.x  , y=pos.y  , z=pos.z-1 }, "_empty")
			autoroute({ x=pos.x  , y=pos.y  , z=pos.z+1 }, "_empty")
			autoroute(pos, "_empty")
		end,
		after_dig_node = function(pos)
			autoroute({ x=pos.x-1, y=pos.y  , z=pos.z   }, "_empty")
			autoroute({ x=pos.x+1, y=pos.y  , z=pos.z   }, "_empty")
			autoroute({ x=pos.x  , y=pos.y-1, z=pos.z   }, "_empty")
			autoroute({ x=pos.x  , y=pos.y+1, z=pos.z   }, "_empty")
			autoroute({ x=pos.x  , y=pos.y  , z=pos.z-1 }, "_empty")
			autoroute({ x=pos.x  , y=pos.y  , z=pos.z+1 }, "_empty")
		end
	})

	minetest.register_node("pipeworks:pipe_"..pname.."_loaded", {
		description = "Pipe segment (loaded, "..pname..")... You hacker, you.",
		drawtype = "nodebox",
		tiles = fix_newpipe_names(outimgs, "_loaded"),
		paramtype = "light",
		selection_box = {
	             	type = "fixed",
			fixed = outsel
		},
		node_box = {
			type = "fixed",
			fixed = outboxes
		},
		groups = {snappy=3, pipe=1, not_in_creative_inventory=1},
		sounds = default.node_sound_wood_defaults(),
		walkable = true,
		stack_max = 99,
		drop = "pipeworks:pipe_110000_loaded",
		after_place_node = function(pos)
			autoroute({ x=pos.x-1, y=pos.y  , z=pos.z   }, "_loaded")
			autoroute({ x=pos.x+1, y=pos.y  , z=pos.z   }, "_loaded")
			autoroute({ x=pos.x  , y=pos.y-1, z=pos.z   }, "_loaded")
			autoroute({ x=pos.x  , y=pos.y+1, z=pos.z   }, "_loaded")
			autoroute({ x=pos.x  , y=pos.y  , z=pos.z-1 }, "_loaded")
			autoroute({ x=pos.x  , y=pos.y  , z=pos.z+1 }, "_loaded")
			autoroute(pos, "_loaded")
		end,
		after_dig_node = function(pos)
			autoroute({ x=pos.x-1, y=pos.y  , z=pos.z   }, "_loaded")
			autoroute({ x=pos.x+1, y=pos.y  , z=pos.z   }, "_loaded")
			autoroute({ x=pos.x  , y=pos.y-1, z=pos.z   }, "_loaded")
			autoroute({ x=pos.x  , y=pos.y+1, z=pos.z   }, "_loaded")
			autoroute({ x=pos.x  , y=pos.y  , z=pos.z-1 }, "_loaded")
			autoroute({ x=pos.x  , y=pos.y  , z=pos.z+1 }, "_loaded")
		end
	})
end
end
end
end
end
end

-- the pump module

pumpboxes = {}
addbox(pumpboxes, leftstub)
addbox(pumpboxes, pumpbody)
addbox(pumpboxes, rightstub)

minetest.register_node("pipeworks:pump_on", {
	description = "Pump Module (on)",
	drawtype = "nodebox",
	tiles = {
		"pipeworks_pump_sides.png",
		"pipeworks_pump_sides.png",
		"pipeworks_pump_ends.png",
		"pipeworks_pump_ends.png",
		"pipeworks_pump_on.png",
		"pipeworks_pump_on.png"
	},
	paramtype = "light",
	selection_box = {
             	type = "fixed",
		fixed = { -0.5, -0.5, -0.5, 0.5, 0.5, 0.5 }
	},
	node_box = {
		type = "fixed",
		fixed = pumpboxes
	},
	groups = {snappy=3, pipe=1, not_in_creative_inventory=1},
	sounds = default.node_sound_wood_defaults(),
	walkable = true,
	stack_max = 99,
})

minetest.register_node("pipeworks:pump_off", {
	description = "Pump Module (off)",
	drawtype = "nodebox",
	tiles = {
		"pipeworks_pump_sides.png",
		"pipeworks_pump_sides.png",
		"pipeworks_pump_ends.png",
		"pipeworks_pump_ends.png",
		"pipeworks_pump_off.png",
		"pipeworks_pump_off.png"
	},
	paramtype = "light",
	selection_box = {
             	type = "fixed",
		fixed = { -0.5, -0.5, -0.5, 0.5, 0.5, 0.5 }
	},
	node_box = {
		type = "fixed",
		fixed = pumpboxes
	},
	groups = {snappy=3, pipe=1},
	sounds = default.node_sound_wood_defaults(),
	walkable = true,
	stack_max = 99,
})

-- valve module

valveboxes = {}
addbox(valveboxes, leftstub)
addbox(valveboxes, valvebody)
addbox(valveboxes, valvehandle_off)
addbox(valveboxes, rightstub)

minetest.register_node("pipeworks:valve_off", {
	description = "Valve (off)",
	drawtype = "nodebox",
	tiles = {
		"pipeworks_valvebody_top_off.png",
		"pipeworks_valvebody_bottom.png",
		"pipeworks_valvebody_ends.png",
		"pipeworks_valvebody_ends.png",
		"pipeworks_valvebody_sides.png",
		"pipeworks_valvebody_sides.png",
	},
	paramtype = "light",
	selection_box = {
             	type = "fixed",
		fixed = { -5/16, -4/16, -5/16, 6/16, 8/16, 6/16 }
	},
	node_box = {
		type = "fixed",
		fixed = valveboxes
	},
	groups = {snappy=3, pipe=1},
	sounds = default.node_sound_wood_defaults(),
	walkable = true,
	stack_max = 99,
})

valveboxes = {}
addbox(valveboxes, leftstub)
addbox(valveboxes, valvebody)
addbox(valveboxes, valvehandle_on)
addbox(valveboxes, rightstub)

minetest.register_node("pipeworks:valve_on", {
	description = "Valve (on)",
	drawtype = "nodebox",
	tiles = {
		"pipeworks_valvebody_top_on.png",
		"pipeworks_valvebody_bottom.png",
		"pipeworks_valvebody_ends.png",
		"pipeworks_valvebody_ends.png",
		"pipeworks_valvebody_sides.png",
		"pipeworks_valvebody_sides.png",
	},
	paramtype = "light",
	selection_box = {
             	type = "fixed",
		fixed = { -5/16, -4/16, -5/16, 6/16, 8/16, 6/16 }
	},
	node_box = {
		type = "fixed",
		fixed = valveboxes
	},
	groups = {snappy=3, pipe=1, not_in_creative_inventory=1},
	sounds = default.node_sound_wood_defaults(),
	walkable = true,
	stack_max = 99,
})

minetest.register_on_punchnode(function (pos, node)
	if node.name=="pipeworks:valve_on" then 
		minetest.env:add_node(pos, { name = "pipeworks:valve_off" })
	end
end)

minetest.register_on_punchnode(function (pos, node)
	if node.name=="pipeworks:valve_off" then 
		minetest.env:add_node(pos, { name = "pipeworks:valve_on" })
	end
end)


minetest.register_on_punchnode(function (pos, node)
	if node.name=="pipeworks:pump_on" then 
		minetest.env:add_node(pos, { name = "pipeworks:pump_off" })
	end
end)

minetest.register_on_punchnode(function (pos, node)
	if node.name=="pipeworks:pump_off" then 
		minetest.env:add_node(pos, { name = "pipeworks:pump_on" })
	end
end)

print("Pipeworks loaded!")