summaryrefslogtreecommitdiff
path: root/tubes.lua
blob: 90e6ecf81ab2b40431f0590edebc7899bff70c4a (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
-- This file supplies pneumatic tubes and a 'test' device

minetest.register_node("pipeworks:testobject", {
	description = "Pneumatic tube test object",
	tiles = {
		"pipeworks_testobject.png",
	},
	paramtype = "light",
	groups = {snappy=3, tubedevice=1},
	sounds = default.node_sound_wood_defaults(),
	walkable = true,
	after_place_node = function(pos)
			tube_scanforobjects(pos)
	end,
	after_dig_node = function(pos)
			tube_scanforobjects(pos)
	end,
})

tubenodes={}

-- tables

minetest.register_alias("pipeworks:tube", "pipeworks:tube_000000")

tube_leftstub = {
	{ -32/64, -9/64, -9/64, 9/64, 9/64, 9/64 },	-- tube segment against -X face
}

tube_rightstub = {
	{ -9/64, -9/64, -9/64,  32/64, 9/64, 9/64 },	-- tube segment against +X face
}

tube_bottomstub = {
	{ -9/64, -32/64, -9/64,   9/64, 9/64, 9/64 },	-- tube segment against -Y face
}


tube_topstub = {
	{ -9/64, -9/64, -9/64,   9/64, 32/64, 9/64 },	-- tube segment against +Y face
}

tube_frontstub = {
	{ -9/64, -9/64, -32/64,   9/64, 9/64, 9/64 },	-- tube segment against -Z face
}

tube_backstub = {
	{ -9/64, -9/64, -9/64,   9/64, 9/64, 32/64 },	-- tube segment against -Z face
} 

tube_selectboxes = {
	{ -32/64,  -10/64,  -10/64,  10/64,  10/64,  10/64 },
	{ -10/64 ,  -10/64,  -10/64, 32/64,  10/64,  10/64 },
	{ -10/64 , -32/64,  -10/64,  10/64,  10/64,  10/64 },
	{ -10/64 ,  -10/64,  -10/64,  10/64, 32/64,  10/64 },
	{ -10/64 ,  -10/64, -32/64,  10/64,  10/64,  10/64 },
	{ -10/64 ,  -10/64,  -10/64,  10/64,  10/64, 32/64 }
}

--  Functions

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

-- now define the nodes!
function register_tube(name,desc,plain_textures,noctr_textures,end_textures,short_texture,inv_texture,special)
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
	local outboxes = {}
	local outsel = {}
	local outimgs = {}

	if yp==1 then
		tube_addbox(outboxes, tube_topstub)
		table.insert(outsel, tube_selectboxes[4])
		table.insert(outimgs, noctr_textures[4])
	else
		table.insert(outimgs, plain_textures[4])
	end
	if ym==1 then
		tube_addbox(outboxes, tube_bottomstub)
		table.insert(outsel, tube_selectboxes[3])
		table.insert(outimgs, noctr_textures[3])
	else
		table.insert(outimgs, plain_textures[3])
	end
	if xp==1 then
		tube_addbox(outboxes, tube_rightstub)
		table.insert(outsel, tube_selectboxes[2])
		table.insert(outimgs, noctr_textures[2])
	else
		table.insert(outimgs, plain_textures[2])
	end
	if xm==1 then
		tube_addbox(outboxes, tube_leftstub)
		table.insert(outsel, tube_selectboxes[1])
		table.insert(outimgs, noctr_textures[1])
	else
		table.insert(outimgs, plain_textures[1])
	end
	if zp==1 then
		tube_addbox(outboxes, tube_backstub)
		table.insert(outsel, tube_selectboxes[6])
		table.insert(outimgs, noctr_textures[6])
	else
		table.insert(outimgs, plain_textures[6])
	end
	if zm==1 then
		tube_addbox(outboxes, tube_frontstub)
		table.insert(outsel, tube_selectboxes[5])
		table.insert(outimgs, noctr_textures[5])
	else
		table.insert(outimgs, plain_textures[5])
	end

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

	if (jx+jy+jz) == 1 then
		if xm == 1 then 
			table.remove(outimgs, 3)
			table.insert(outimgs, 3, end_textures[3])
		end
		if xp == 1 then 
			table.remove(outimgs, 4)
			table.insert(outimgs, 4, end_textures[4])
		end
		if ym == 1 then 
			table.remove(outimgs, 1)
			table.insert(outimgs, 1, end_textures[1])
		end
		if xp == 1 then 
			table.remove(outimgs, 2)
			table.insert(outimgs, 2, end_textures[2])
		end
		if zm == 1 then 
			table.remove(outimgs, 5)
			table.insert(outimgs, 5, end_textures[5])
		end
		if zp == 1 then 
			table.remove(outimgs, 6)
			table.insert(outimgs, 6, end_textures[6])
		end
	end

	local tname = xm..xp..ym..yp..zm..zp
	local tgroups = ""

	if tname ~= "000000" then
		tgroups = {snappy=3, tube=1, not_in_creative_inventory=1}
		tubedesc = desc.." ("..tname..")... You hacker, you."
		iimg=nil
		wscale = {x=1,y=1,z=1}
	else
		tgroups = {snappy=3, tube=1}
		tubedesc = desc
		iimg=inv_texture
		outimgs = {
			short_texture,short_texture,
			end_textures[3],end_textures[4],
			short_texture,short_texture
		}
		outboxes = { -24/64, -9/64, -9/64, 24/64, 9/64, 9/64 }
		outsel = { -24/64, -10/64, -10/64, 24/64, 10/64, 10/64 }
		wscale = {x=1,y=1,z=0.01}
	end
	
	table.insert(tubenodes,name.."_"..tname)
	
	nodedef={
		description = tubedesc,
		drawtype = "nodebox",
		tiles = outimgs,
		inventory_image=iimg,
		wield_image=iimg,
		wield_scale=wscale,
		paramtype = "light",
		selection_box = {
	             	type = "fixed",
			fixed = outsel
		},
		node_box = {
			type = "fixed",
			fixed = outboxes
		},
		groups = tgroups,
		sounds = default.node_sound_wood_defaults(),
		walkable = true,
		stack_max = 99,
		drop = name.."_000000",
		tubelike=1,
		on_construct = function(pos)
			local meta = minetest.env:get_meta(pos)
			meta:set_int("tubelike",1)
			if minetest.registered_nodes[name.."_"..tname].on_construct_ then
				minetest.registered_nodes[name.."_"..tname].on_construct_(pos)
			end
		end,
		after_place_node = function(pos)
			tube_scanforobjects(pos)
			if minetest.registered_nodes[name.."_"..tname].after_place_node_ then
				minetest.registered_nodes[name.."_"..tname].after_place_node_(pos)
			end
		end,
		after_dig_node = function(pos)
			tube_scanforobjects(pos)
			if minetest.registered_nodes[name.."_"..tname].after_dig_node_ then
				minetest.registered_nodes[name.."_"..tname].after_dig_node_(pos)
			end
		end
	}
	
	if special==nil then special={} end

	for key,value in pairs(special) do
		if key=="on_construct" or key=="after_dig_node" or key=="after_place_node" then
			nodedef[key.."_"]=value
		elseif key=="groups" then
			for group,val in pairs(value) do
				nodedef.groups[group]=val
			end
		else
			nodedef[key]=value
		end
	end
	
	minetest.register_node(name.."_"..tname, nodedef)

end
end
end
end
end
end
end

noctr_textures={"pipeworks_tube_noctr.png","pipeworks_tube_noctr.png","pipeworks_tube_noctr.png",
		"pipeworks_tube_noctr.png","pipeworks_tube_noctr.png","pipeworks_tube_noctr.png"}
plain_textures={"pipeworks_tube_plain.png","pipeworks_tube_plain.png","pipeworks_tube_plain.png",
		"pipeworks_tube_plain.png","pipeworks_tube_plain.png","pipeworks_tube_plain.png"}
end_textures={"pipeworks_tube_end.png","pipeworks_tube_end.png","pipeworks_tube_end.png",
		"pipeworks_tube_end.png","pipeworks_tube_end.png","pipeworks_tube_end.png"}
short_texture="pipeworks_tube_short.png"
inv_texture="pipeworks_tube_inv.png"

register_tube("pipeworks:tube","Pneumatic tube segment",plain_textures,noctr_textures,end_textures,short_texture,inv_texture)

mese_noctr_textures={"pipeworks_mese_tube_noctr_1.png","pipeworks_mese_tube_noctr_2.png","pipeworks_mese_tube_noctr_3.png",
		"pipeworks_mese_tube_noctr_4.png","pipeworks_mese_tube_noctr_5.png","pipeworks_mese_tube_noctr_6.png"}

mese_plain_textures={"pipeworks_mese_tube_plain_1.png","pipeworks_mese_tube_plain_2.png","pipeworks_mese_tube_plain_3.png",
		"pipeworks_mese_tube_plain_4.png","pipeworks_mese_tube_plain_5.png","pipeworks_mese_tube_plain_6.png"}
mese_end_textures={"pipeworks_mese_tube_end.png","pipeworks_mese_tube_end.png","pipeworks_mese_tube_end.png",
		"pipeworks_mese_tube_end.png","pipeworks_mese_tube_end.png","pipeworks_mese_tube_end.png"}
mese_short_texture="pipeworks_mese_tube_short.png"
mese_inv_texture="pipeworks_mese_tube_inv.png"

detector_plain_textures={"pipeworks_detector_tube_plain.png","pipeworks_detector_tube_plain.png","pipeworks_detector_tube_plain.png",
		"pipeworks_detector_tube_plain.png","pipeworks_detector_tube_plain.png","pipeworks_detector_tube_plain.png"}
detector_inv_texture="pipeworks_detector_tube_inv.png"

meseadjlist={{x=0,y=0,z=1},{x=0,y=0,z=-1},{x=0,y=1,z=0},{x=0,y=-1,z=0},{x=1,y=0,z=0},{x=-1,y=0,z=0}}

register_tube("pipeworks:mese_tube","Mese pneumatic tube segment",mese_plain_textures,mese_noctr_textures,
	mese_end_textures,mese_short_texture,mese_inv_texture,
	{tube={can_go=function(pos,node,velocity,stack)
		tbl={}
		local meta=minetest.env:get_meta(pos)
		local inv=meta:get_inventory()
		local found=false
		local name=stack:get_name()
		for i,vect in ipairs(meseadjlist) do
			for _,st in ipairs(inv:get_list("line"..tostring(i))) do
				if st:get_name()==name then
					found=true
					table.insert(tbl,vect)
				end
			end
		end
		if found==false then
			for i,vect in ipairs(meseadjlist) do
				if inv:is_empty("line"..tostring(i)) then
					table.insert(tbl,vect)
				end
			end
		end
		return tbl
	end},
	on_construct = function(pos)
		local meta = minetest.env:get_meta(pos)
		meta:set_string("formspec",
				"size[8,11]"..
				"list[current_name;line1;1,0;7,1;]"..
				"list[current_name;line2;1,1;7,1;]"..
				"list[current_name;line3;1,2;7,1;]"..
				"list[current_name;line4;1,3;7,1;]"..
				"list[current_name;line5;1,4;7,1;]"..
				"list[current_name;line6;1,5;7,1;]"..
				"image[0,0;1,1;white.png]"..
				"image[0,1;1,1;black.png]"..
				"image[0,2;1,1;green.png]"..
				"image[0,3;1,1;yellow.png]"..
				"image[0,4;1,1;blue.png]"..
				"image[0,5;1,1;red.png]"..
				"list[current_player;main;0,7;8,4;]")
		meta:set_string("infotext", "Mese pneumatic tube")
		local inv = meta:get_inventory()
		for i=1,6 do
			inv:set_size("line"..tostring(i), 7*1)
		end
	end,
	can_dig = function(pos,player)
		local meta = minetest.env:get_meta(pos);
		local inv = meta:get_inventory()
		return (inv:is_empty("line1") and inv:is_empty("line2") and inv:is_empty("line3") and
			inv:is_empty("line4") and inv:is_empty("line5") and inv:is_empty("line6"))
	end})


mesecons_rules={{x=0,y=0,z=1},{x=0,y=0,z=-1},{x=1,y=0,z=0},{x=-1,y=0,z=0},{x=1,y=1,z=0},{x=1,y=-1,z=0},
		{x=-1,y=1,z=0},{x=-1,y=-1,z=0},{x=0,y=1,z=1},{x=0,y=-1,z=1},{x=0,y=1,z=-1},{x=0,y=-1,z=-1}}

register_tube("pipeworks:detector_tube_on","Detector tube segment on (you hacker you)",detector_plain_textures,noctr_textures,
	end_textures,short_texture,detector_inv_texture,
	{tube={can_go=function(pos,node,velocity,stack)
		local meta = minetest.env:get_meta(pos)
		local name = minetest.env:get_node(pos).name
		local nitems=meta:get_int("nitems")+1
		meta:set_int("nitems", nitems)
		minetest.after(0.1,minetest.registered_nodes[name].item_exit,pos)
		return meseadjlist
	end},
	groups={mesecon=2,not_in_creative_inventory=1},
	drop="pipeworks:detector_tube_off_000000",
	mesecons={receptor={state="on",
				rules=mesecons_rules}},
	item_exit = function(pos)
		local meta = minetest.env:get_meta(pos)
		local nitems=meta:get_int("nitems")-1
		local name = minetest.env:get_node(pos).name
		if nitems==0 then
			minetest.env:set_node(pos,{name=string.gsub(name,"on","off")})
			mesecon:receptor_off(pos,mesecons_rules)
		else
			meta:set_int("nitems", nitems)
		end
	end,
	on_construct = function(pos)
		local meta = minetest.env:get_meta(pos)
		meta:set_int("nitems", 1)
		local name = minetest.env:get_node(pos).name
		minetest.after(0.1,minetest.registered_nodes[name].item_exit,pos)
	end})

register_tube("pipeworks:detector_tube_off","Detector tube segment",detector_plain_textures,noctr_textures,
	end_textures,short_texture,detector_inv_texture,
	{tube={can_go=function(pos,node,velocity,stack)
		local name = minetest.env:get_node(pos).name
		minetest.env:set_node(pos,{name=string.gsub(name,"off","on")})
		mesecon:receptor_on(pos,mesecons_rules)
		return meseadjlist
	end},
	groups={mesecon=2},
	mesecons={receptor={state="off",
				rules=mesecons_rules}}})

register_tube("pipeworks:accelerator_tube","Accelerator pneumatic tube segment",plain_textures,noctr_textures,end_textures,
		short_texture,inv_texture,
		{tube={can_go=function(pos,node,velocity,stack)
			velocity.speed=velocity.speed+1
			return meseadjlist
		end}})

modpath=minetest.get_modpath("pipeworks")
dofile(modpath.."/teleport_tube.lua")