summaryrefslogtreecommitdiff
path: root/nixie_tubes/init.lua
blob: 7f10e5bd96c706c8fcdc819f95c327230b164f60 (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
-- simple nixie tubes mod
-- by Vanessa Ezekowitz

nixie_tubes = {}

local S
if minetest.get_modpath("intllib") then
	S = intllib.Getter()
else
	S = function(s) return s end
end

local nixie_types = {
	"1",
	"2",
	"3",
	"4",
	"5",
	"6",
	"7",
	"8",
	"9",
	"0",
	"colon",
	"period",
	"off"
}

local tube_cbox = {
	type = "fixed",
	fixed = { -11/32, -8/16, -11/32, 11/32, 8/16, 11/32 }
}

-- the following functions based on the so-named ones in Jeija's digilines mod

local reset_meta = function(pos)
	minetest.get_meta(pos):set_string("formspec", "field[channel;Channel;${channel}]")
end

local on_digiline_receive_std = function(pos, node, channel, msg)
	local meta = minetest.get_meta(pos)
	local setchan = meta:get_string("channel")
	if setchan ~= channel then return end
	local num = tonumber(msg)
	if msg == "colon" or msg == "period" or msg == "off" or (num and (num >= 0 and num <= 9)) then
		if string.sub(node.name,1,21) == "nixie_tubes:numitron_" then
			minetest.swap_node(pos, { name = "nixie_tubes:numitron_"..msg, param2 = node.param2})
		else
			minetest.swap_node(pos, { name = "nixie_tubes:tube_"..msg, param2 = node.param2})
		end
	end
end

local on_digiline_receive_deca = function(pos, node, channel, msg)

	local meta = minetest.get_meta(pos)
	local setchan = meta:get_string("channel")
	if setchan ~= channel then return end
	local tubenum = string.gsub(node.name, "nixie_tubes:decatron_", "")
	local num = tonumber(msg)

	if msg == "off" or (num and (num >= 0 and num <= 9)) then
		minetest.swap_node(pos, { name = "nixie_tubes:decatron_"..msg, param2 = node.param2})

	elseif msg == "inc" then
		num = (tonumber(tubenum) or 0) + 1
		if num > 9 then
			num = 0
			digiline:receptor_send(pos, digiline.rules.default, channel, "carry")
		end
		minetest.swap_node(pos, { name = "nixie_tubes:decatron_"..num, param2 = node.param2})

	elseif msg == "dec" then
		num = (tonumber(tubenum) or 0) - 1
		if num < 0 then
			num = 9
			digiline:receptor_send(pos, digiline.rules.default, channel, "borrow")
		end
		minetest.swap_node(pos, { name = "nixie_tubes:decatron_"..num, param2 = node.param2})

	elseif msg == "get" then
		digiline:receptor_send(pos, digiline.rules.default, channel, tubenum)

	end
end

-- the nodes:

for _,tube in ipairs(nixie_types) do
	local groups = { cracky = 2, not_in_creative_inventory = 1}
	local light = LIGHT_MAX-4
	local light2 = LIGHT_MAX-5
	local description = S("Nixie Tube ("..tube..")")
	local description2 = S("Decatron ("..tube..")")
	local cathode = "nixie_tube_cathode_off.png^nixie_tube_cathode_"..tube..".png"
	local cathode2 = "decatron_cathode_"..tube..".png"
	local cathode3 = "numitron_filaments.png^numitron_"..tube..".png"

	if tube == "off" then
		groups = {cracky = 2}
		light = nil
		light2 = nil
		description = S("Nixie Tube")
		description2 = S("Decatron")
		description3 = S("Numitron Tube")
		cathode = "nixie_tube_cathode_off.png"
		cathode2 = "nixie_tube_blank.png"
		cathode3 = "numitron_filaments.png"
	end

	minetest.register_node("nixie_tubes:tube_"..tube, {
		description = description,
		drawtype = "mesh",
		mesh = "nixie_tube.obj",
		tiles = {
			"nixie_tube_base.png",
			"nixie_tube_backing.png",
			cathode,
			"nixie_tube_anode.png",
			"nixie_tube_glass.png",
		},
		use_texture_alpha = true,
		groups = groups,
		paramtype = "light",
		paramtype2 = "facedir",
		light_source = light,
		selection_box = tube_cbox,
		collision_box = tube_cbox,
		on_construct = function(pos)
			reset_meta(pos)
		end,
		on_receive_fields = function(pos, formname, fields, sender)
			if (fields.channel) then
				minetest.get_meta(pos):set_string("channel", fields.channel)
			end
		end,
		digiline = {
			receptor = {},
			effector = {
				action = on_digiline_receive_std
			},
		},
		drop = "nixie_tubes:tube_off"
	})

	minetest.register_node("nixie_tubes:numitron_"..tube, {
		description = description3,
		drawtype = "mesh",
		mesh = "nixie_tube.obj",
		tiles = {
			"nixie_tube_base.png",
			"nixie_tube_backing.png",
			cathode3,
			"nixie_tube_anode.png",
			"nixie_tube_glass.png",
		},
		use_texture_alpha = true,
		groups = groups,
		paramtype = "light",
		paramtype2 = "facedir",
		light_source = light,
		selection_box = tube_cbox,
		collision_box = tube_cbox,
		on_construct = function(pos)
			reset_meta(pos)
		end,
		on_receive_fields = function(pos, formname, fields, sender)
			if (fields.channel) then
				minetest.get_meta(pos):set_string("channel", fields.channel)
			end
		end,
		digiline = {
			receptor = {},
			effector = {
				action = on_digiline_receive_std
			},
		},
		drop = "nixie_tubes:numitron_off"
	})

	if tube ~= "colon" and tube ~= "period" then
		minetest.register_node("nixie_tubes:decatron_"..tube, {
			description = description2,
			drawtype = "mesh",
			mesh = "decatron.obj",
			tiles = {
				"nixie_tube_base.png",
				"decatron_internals.png",
				"decatron_anode.png",
				"decatron_cathode_pins.png",
				cathode2,
				"nixie_tube_glass.png",
			},
			use_texture_alpha = true,
			groups = groups,
			paramtype = "light",
			paramtype2 = "facedir",
			light_source = light2,
			selection_box = tube_cbox,
			collision_box = tube_cbox,
			after_place_node = function(pos, placer, itemstack, pointed_thing)
				minetest.set_node(pos, { name = "air"})
				minetest.rotate_node(itemstack, placer, pointed_thing)
				if minetest.get_node(pos).param2 == 12 then
					minetest.set_node(pos, { name = "nixie_tubes:decatron_off", param2 = 15 })
				end
			end,
			on_construct = function(pos)
				reset_meta(pos)
			end,
			on_receive_fields = function(pos, formname, fields, sender)
				if (fields.channel) then
					minetest.get_meta(pos):set_string("channel", fields.channel)
				end
			end,
			digiline = {
				receptor = {},
				effector = {
					action = on_digiline_receive_deca
				},
			},
			drop = "nixie_tubes:decatron_off"
		})
	end
end

-- Alpha-numeric tubes (Burroughs B-7971 or similar)

--[[

Map of display wires:

      --1------
     |\   |8  /|
    6| \  |  / |2
     | 7\ | /9 |
     |   \|/   |
14--> ---- ---- <--10
     |   /|\   |
     |13/ | \11|
    5| /  |  \ |3
     |/ 12|   \|
      ------4--
          _
      --¯¯ ¯¯-- <--15

-- Wire positions in table:
-- char = { 1, 2, 3, 4, .... , 13, 14, 15 }

]]--

local alnum_chars = {
	{ " ",	{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } },	-- 32
	{ "!",	{ 0,0,0,0,1,1,0,0,0,0,0,0,0,0,0 } },
	{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1,1,1,1,1,1,1,1,1,1,1,1,1 } },	-- all-on
	{ string.char(129),	{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } },	-- "cursor" segment
}

local on_digiline_receive_alnum = function(pos, node, channel, msg)
	local meta = minetest.get_meta(pos)
	local setchan = meta:get_string("channel")
	if setchan ~= channel then return end
	if msg and msg ~= "" and type(msg) == "string" then
		local asc = string.byte(msg)
		if msg == "off" then
			minetest.swap_node(pos, { name = "nixie_tubes:alnum_32", param2 = node.param2})
		elseif msg == "colon" then
			minetest.swap_node(pos, { name = "nixie_tubes:alnum_58", param2 = node.param2})
		elseif msg == "period" then
			minetest.swap_node(pos, { name = "nixie_tubes:alnum_46", param2 = node.param2})
		elseif msg == "del" then
			minetest.swap_node(pos, { name = "nixie_tubes:alnum_127", param2 = node.param2})
		elseif msg == "allon" then
			minetest.swap_node(pos, { name = "nixie_tubes:alnum_128", param2 = node.param2})
		elseif msg == "cursor" then
			minetest.swap_node(pos, { name = "nixie_tubes:alnum_129", param2 = node.param2})
		elseif asc > 31 and alnum_chars[asc - 31] then
			minetest.swap_node(pos, { name = "nixie_tubes:alnum_"..asc, param2 = node.param2})
		elseif msg == "get" then -- get value as ASCII numerical value
			digiline:receptor_send(pos, digiline.rules.default, channel, tonumber(string.match(minetest.get_node(pos).name,"nixie_tubes:alnum_(.+)"))) -- wonderfully horrible string manipulaiton
		elseif msg == "getstr" then -- get actual char
			digiline:receptor_send(pos, digiline.rules.default, channel, string.char(tonumber(string.match(minetest.get_node(pos).name,"nixie_tubes:alnum_(.+)"))))
		end
	elseif msg and type(msg) == "number" then
		if msg == 0 then
			minetest.swap_node(pos, { name = "nixie_tubes:alnum_32", param2 = node.param2})
		elseif msg > 31 and alnum_chars[msg - 31] ~= nil then
			minetest.swap_node(pos, { name = "nixie_tubes:alnum_"..tostring(msg), param2 = node.param2})
		end
	end
end

for i in ipairs(alnum_chars) do
	local char = alnum_chars[i][1]
	local bits = alnum_chars[i][2]

	local groups = { cracky = 2, not_in_creative_inventory = 1}
	local light = LIGHT_MAX-4
	local description = S("Alphanumeric Nixie Tube ("..char..")")

	local wires = "nixie_tube_alnum_wires.png"
	for j = 1, 15 do
		if bits[j] == 1 then
			wires = wires.."^nixie_tube_alnum_seg_"..j..".png"
		end
	end

	if char == " " then
		groups = {cracky = 2}
		light = nil
		description = S("Alphanumeric Nixie Tube")
		wires = "nixie_tube_alnum_wires.png"
	end

	minetest.register_node("nixie_tubes:alnum_"..string.byte(char), {
		description = description,
		drawtype = "mesh",
		mesh = "nixie_tube.obj",
		tiles = {
			"nixie_tube_base.png",
			"nixie_tube_backing.png",
			wires,
			"nixie_tube_anode.png",
			"nixie_tube_glass.png",
		},
		use_texture_alpha = true,
		groups = groups,
		paramtype = "light",
		paramtype2 = "facedir",
		light_source = light,
		selection_box = tube_cbox,
		collision_box = tube_cbox,
		on_construct = function(pos)
			reset_meta(pos)
		end,
		on_receive_fields = function(pos, formname, fields, sender)
			if (fields.channel) then
				minetest.get_meta(pos):set_string("channel", fields.channel)
			end
		end,
		digiline = {
			receptor = {},
			effector = {
				action = on_digiline_receive_alnum
			},
		},
		drop = "nixie_tubes:alnum_32"
	})
end

-- crafts

minetest.register_craft({
	output = "nixie_tubes:tube_off 4",
	recipe = {
		{ "", "default:glass", "" },
		{ "default:glass", "default:sign_wall", "default:glass" },
		{ "default:glass", "default:mese_crystal_fragment", "default:glass" }
	},
})

minetest.register_craft({
	output = "nixie_tubes:numitron_off 4",
	recipe = {
		{ "", "default:glass", "" },
		{ "default:glass", "default:copper_ingot", "default:glass" },
		{ "default:glass", "default:mese_crystal_fragment", "default:glass" }
	},
})


minetest.register_craft({
	output = "nixie_tubes:alnum_32 4",
	recipe = {
		{ "", "default:glass", "" },
		{ "default:glass", "default:sign_wall", "default:glass" },
		{ "default:glass", "default:mese_crystal", "default:glass" }
	},
})