MT-test3d/test3d_t416/init.lua
2017-02-12 01:57:00 +00:00

69 lines
2.5 KiB
Lua

local MEMSIZE=16
if not digiline then
print("Digilines not found.")
return
end
local oldprint=print
local function print(...)
for k,v in ipairs({...}) do
oldprint("[Test3D:T416] "..tostring(v))
end
end
print("Loading.")
local function t416_digiline_receive(pos, node, channel, msg)
--print("Digiline: "..tostring(pos.x)..","..tostring(pos.y)..","..tostring(pos.z).."; Channel: "..tostring(channel).."; Message: "..tostring(msg))
local meta = minetest.get_meta(pos)
local mem = string.split(meta:get_string("mem"),"\n")
local ms = ""
local chan = meta:get_string("channel")
local memoffset = meta:get_int("startaddr")
if channel:sub(1,#chan) == chan and tonumber(channel:sub(#chan+1),16)+1-memoffset <= MEMSIZE and tonumber(channel:sub(#chan+1),16)+1-memoffset > 0 then
local memaddr = tonumber(channel:sub(#chan+1),16)+1-memoffset
if tostring(msg):lower() == "get" then
digiline:receptor_send(pos, digiline.rules.default, channel, tonumber(mem[memaddr] or 0))
print("Reading address "..tostring(memaddr)..": "..tostring(mem[memaddr]))
else
mem[memaddr] = tonumber(msg) or 0
for k,v in ipairs(mem) do
ms = ms .. tostring(v) .. "\n"
end
meta:set_string("mem",ms)
print("Set address "..tostring(memaddr).." to "..tostring(tonumber(msg)))
end
end
end
print("Created T416 digiline function")
local function t416_set_meta(pos)
local meta = minetest.get_meta(pos)
local ms = ""
for i = 1, MEMSIZE do
ms = ms .. "0\n"
end
meta:set_string("mem",ms)
meta:set_string("channel","Default")
meta:set_int("startaddr",0)
meta:set_string("formspec","size[5,12]\nlabel[0.4,0.5;T416 Memory Node (16 words)]\nfield[0.5,2;4,1;channel;Channel prefix;${channel}]\nfield[0.5,3;4,1;startaddr;Starting address;${startaddr}]\ntextarea[0.5,4;4,8;mem;Memory contents;${mem}]")
end
minetest.register_node("test3d_t416:t416", {
description = "T416 Memory Node",
tiles = {"t416-top.png","t416-top.png","t416-side.png","t416-side.png","t416-side.png","t416-side.png"},
on_construct = t416_set_meta,
groups = {snappy=1,choppy=2,oddly_breakable_by_hand=2,flammable=3},
digiline = {
receptor = {},
effector = {
action = function (a,b,c,d) pcall(t416_digiline_receive,a,b,c,d) end
}
},
on_receive_fields = function(pos,_,fields,sender)
if fields.channel == nil then return end
local meta = minetest.get_meta(pos)
fields.channel = fields.channel or ""
meta:set_string("channel",fields.channel)
meta:set_int("startaddr",tonumber(fields.startaddr))
end
})
print("T416 node registered")