2016-09-24 20:44:30 +10:00
local statedir = minetest.get_worldpath ( ) .. ' / ' .. minetest.get_current_modname ( ) .. " / "
local oldprint = print
local function print ( ... )
for k , v in ipairs ( { ... } ) do
oldprint ( " [Test3D] " .. tostring ( v ) )
end
end
print ( " Test3D loading. " )
print ( " State directory: " .. statedir )
if not digiline then
print ( " Digilines not found. " )
return
end
2016-09-12 22:35:23 +10:00
local function t21_digiline_receive ( pos , node , channel , msg )
print ( pos , node , channel , msg )
end
2016-09-24 20:44:30 +10:00
print ( " Created T21 digiline function " )
2016-09-12 22:35:23 +10:00
2016-09-24 20:44:30 +10:00
print ( " Loading/creating states " )
_G.test3d = { }
_G.test3d . states = { }
-- states:
--[[
{
mem = { 4096 ints }
nodes = int
pc = int
}
] ] --
function _G . test3d . save ( )
local f = io.open ( statedir .. " /states.txt " , " wb " )
for k , v in pairs ( test3d.states ) do
f : write ( tostring ( k ) .. " \n " )
local g = io.open ( statedir .. " / " .. tostring ( k ) )
for l , m in ipairs ( test3d.states [ k ] ) do
g : write ( tostring ( m ) .. " \n " )
end
g : close ( )
end
f : close ( )
end
_G.test3d . laststate = 0
_G.test3d . genstate = { }
local statetab = { }
os.execute ( " mkdir " .. statedir ) -- just in case, 50/50 this kills minetest on some platforms
local f = io.open ( statedir .. " /states.txt " , " rb " )
local statetab = { }
if f ~= nil then
f : close ( )
for l in io.lines ( statedir .. " /states.txt " ) do
print ( " State: " .. l )
end
end
print ( " Creating T21 formspec " )
local function t21_set_meta ( pos )
minetest.get_meta ( pos ) : set_string ( " formspec " , " field[channel;Channel;${channel}] \n field[statename;State;${statename}] " )
end
minetest.register_on_shutdown ( test3d.save )
2016-09-12 22:35:23 +10:00
minetest.register_node ( " test3d:t21 " , {
description = " T21 Execution Node " ,
tiles = {
{
image = " t21.png " ,
animation = {
type = " vertical_frames " ,
aspect_w = 16 ,
aspect_h = 16 ,
length = 18 ,
length = 1.8 ,
} ,
} ,
} ,
2016-09-24 20:44:30 +10:00
on_construct = t21_set_meta ,
2016-09-12 22:35:23 +10:00
on_punch = function ( pos , _ , _ , _ )
local meta = minetest.get_meta ( pos )
local cbeep = meta : get_int ( " test3d_running " )
-- print("cbeep ",cbeep)
local nbeep = 1
if cbeep == 1 then
nbeep = 0
else
nbeep = 1
end
meta : set_int ( " test3d_running " , nbeep )
end ,
groups = { snappy = 1 , choppy = 2 , oddly_breakable_by_hand = 2 , flammable = 3 } ,
digiline = {
receptor = { } ,
effector = {
action = t21_digiline_receive
}
2016-09-24 20:44:30 +10:00
} ,
on_receive_fields = function ( pos , _ , fields , sender )
local meta = minetest.get_meta ( pos )
_G.test3d . states [ meta : get_string ( " statename " ) ] . nodes = _G.test3d . states [ meta : get_string ( " statename " ) ] . nodes - 1 -- decrement node counter
meta : set_string ( " statename " , fields.statename )
meta : set_string ( " channel " , fields.channel ) -- channel the thing listens for commands on
_G.test3d . states [ meta : get_string ( " statename " ) ] . nodes = _G.test3d . states [ meta : get_string ( " statename " ) ] . nodes + 1 -- increment new node counter
end
2016-09-12 22:35:23 +10:00
} )
2016-09-24 20:44:30 +10:00
print ( " T21 node registered " )
2016-09-12 22:35:23 +10:00
minetest.register_abm ( {
nodenames = { " test3d:t21 " } ,
interval = 1 ,
chance = 1 ,
action = function ( pos )
local node = minetest.get_node_or_nil ( pos )
local meta = minetest.get_meta ( pos )
local beepState = meta : get_int ( " test3d_running " )
if beepState == nil then beepState = 1 end
-- print(beepState)
2016-09-24 20:44:30 +10:00
if beepState == 1 and meta : get_string ( " statename " ) ~= nil and meta : get_string ( " statename " ) ~= " " then
--[[ minetest.sound_play("default_break_glass",{
2016-09-12 22:35:23 +10:00
pos = pos ,
max_hear_distance = 100
} )
2016-09-24 20:44:30 +10:00
] ] --
print ( " Running T21 node with instance " .. tostring ( meta : get_string ( " statename " ) ) .. " at position " .. tostring ( pos.x ) .. " , " .. tostring ( pos.y ) .. " , " .. tostring ( pos.z ) )
2016-09-12 22:35:23 +10:00
end
end ,
} )
2016-09-24 20:44:30 +10:00
print ( " T21 ABMs registered " )
minetest.register_chatcommand ( " lua " , { -- I'm a terrible person
params = " <text> " ,
description = " Execute some Lua code " ,
privs = { talk = true } ,
func = function ( _ , text )
e , lstr = pcall ( loadstring ( text ) )
minetest.chat_send_all ( tostring ( lstr ) )
end ,
} )