2020-03-06 01:00:51 +03:00
|
|
|
--[[
|
|
|
|
|
2020-03-09 00:36:23 +03:00
|
|
|
Techage
|
|
|
|
=======
|
2020-03-06 01:00:51 +03:00
|
|
|
|
2020-03-09 00:36:23 +03:00
|
|
|
Copyright (C) 2020 Joachim Stolberg
|
2020-03-06 01:00:51 +03:00
|
|
|
|
2020-10-19 20:09:17 +03:00
|
|
|
AGPL v3
|
2020-03-06 01:00:51 +03:00
|
|
|
See LICENSE.txt for more information
|
|
|
|
|
|
|
|
server.lua:
|
|
|
|
|
|
|
|
]]--
|
|
|
|
|
2020-03-15 01:02:13 +03:00
|
|
|
-- for lazy programmers
|
|
|
|
local S = function(pos) if pos then return minetest.pos_to_string(pos) end end
|
|
|
|
local M = minetest.get_meta
|
|
|
|
|
2020-03-06 01:00:51 +03:00
|
|
|
local SERVER_CAPA = 5000
|
2020-03-15 01:02:13 +03:00
|
|
|
|
|
|
|
local function formspec(nvm)
|
|
|
|
local names = table.concat(nvm.names or {}, " ")
|
2020-03-06 01:00:51 +03:00
|
|
|
return "size[9,4]"..
|
|
|
|
default.gui_bg..
|
|
|
|
default.gui_bg_img..
|
|
|
|
default.gui_slots..
|
2020-03-15 01:02:13 +03:00
|
|
|
"field[0.2,1;9,1;names;Allowed user names (space separated):;"..names.."]" ..
|
2020-03-06 01:00:51 +03:00
|
|
|
"button_exit[3.5,2.5;2,1;exit;Save]"
|
|
|
|
end
|
|
|
|
|
|
|
|
minetest.register_node("techage:ta4_server", {
|
2020-03-15 01:02:13 +03:00
|
|
|
description = "TA4 Lua Server",
|
2020-03-06 01:00:51 +03:00
|
|
|
tiles = {
|
|
|
|
-- up, down, right, left, back, front
|
|
|
|
"techage_server_top.png",
|
|
|
|
"techage_server_top.png",
|
|
|
|
"techage_server_side.png",
|
|
|
|
"techage_server_side.png^[transformFX",
|
|
|
|
"techage_server_back.png",
|
|
|
|
{
|
2023-04-07 11:51:33 +03:00
|
|
|
name = "techage_server_front.png",
|
2020-03-06 01:00:51 +03:00
|
|
|
backface_culling = false,
|
|
|
|
animation = {
|
|
|
|
type = "vertical_frames",
|
|
|
|
aspect_w = 32,
|
|
|
|
aspect_h = 32,
|
|
|
|
length = 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
drawtype = "nodebox",
|
|
|
|
node_box = {
|
|
|
|
type = "fixed",
|
|
|
|
fixed = {
|
|
|
|
{ -3/16, -8/16, -7/16, 3/16, 6/16, 7/16},
|
|
|
|
},
|
|
|
|
},
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2020-03-06 01:00:51 +03:00
|
|
|
after_place_node = function(pos, placer)
|
2020-03-15 01:02:13 +03:00
|
|
|
local meta = M(pos)
|
|
|
|
local nvm = techage.get_nvm(pos)
|
2020-03-06 01:00:51 +03:00
|
|
|
local number = techage.add_node(pos, "techage:ta4_server")
|
|
|
|
meta:set_string("owner", placer:get_player_name())
|
|
|
|
meta:set_string("number", number)
|
2020-03-15 01:02:13 +03:00
|
|
|
meta:set_string("formspec", formspec(nvm))
|
|
|
|
nvm.size = 0
|
|
|
|
meta:set_string("infotext", "Server "..number..": ("..nvm.size.."/"..SERVER_CAPA..")")
|
2020-03-06 01:00:51 +03:00
|
|
|
minetest.get_node_timer(pos):start(20)
|
|
|
|
end,
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2020-03-06 01:00:51 +03:00
|
|
|
on_receive_fields = function(pos, formname, fields, player)
|
2020-03-15 01:02:13 +03:00
|
|
|
local meta = M(pos)
|
|
|
|
local nvm = techage.get_nvm(pos)
|
2020-03-06 01:00:51 +03:00
|
|
|
local owner = meta:get_string("owner")
|
|
|
|
if player:get_player_name() == owner then
|
|
|
|
if fields.names and fields.names ~= "" then
|
2020-03-15 01:02:13 +03:00
|
|
|
nvm.names = string.split(fields.names, " ")
|
|
|
|
meta:set_string("formspec", formspec(nvm))
|
2020-03-06 01:00:51 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2020-03-06 01:00:51 +03:00
|
|
|
on_dig = function(pos, node, puncher, pointed_thing)
|
|
|
|
if minetest.is_protected(pos, puncher:get_player_name()) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
techage.del_mem(pos)
|
2020-06-14 00:32:00 +03:00
|
|
|
minetest.node_dig(pos, node, puncher, pointed_thing)
|
2020-03-06 01:00:51 +03:00
|
|
|
end,
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2020-12-11 17:50:40 +03:00
|
|
|
after_dig_node = function(pos, oldnode, oldmetadata)
|
|
|
|
techage.remove_node(pos, oldnode, oldmetadata)
|
|
|
|
end,
|
|
|
|
|
2021-05-09 20:34:13 +03:00
|
|
|
on_timer = function(pos, elasped)
|
2020-03-15 01:02:13 +03:00
|
|
|
local meta = M(pos)
|
|
|
|
local nvm = techage.get_nvm(pos)
|
|
|
|
nvm.size = nvm.size or 0
|
|
|
|
local number = meta:get_string("number")
|
|
|
|
meta:set_string("infotext", "Server "..number..": ("..nvm.size.."/"..SERVER_CAPA..")")
|
|
|
|
return true
|
|
|
|
end,
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2020-03-06 01:00:51 +03:00
|
|
|
paramtype = "light",
|
|
|
|
sunlight_propagates = true,
|
2021-03-19 22:06:22 +03:00
|
|
|
use_texture_alpha = techage.CLIP,
|
2020-03-06 01:00:51 +03:00
|
|
|
paramtype2 = "facedir",
|
|
|
|
groups = {choppy=1, cracky=1, crumbly=1},
|
|
|
|
is_ground_content = false,
|
|
|
|
sounds = default.node_sound_stone_defaults(),
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_craft({
|
|
|
|
output = "techage:ta4_server",
|
|
|
|
recipe = {
|
|
|
|
{"default:steel_ingot", "dye:black", "default:steel_ingot"},
|
|
|
|
{"techage:ta4_ramchip", "default:copper_ingot", "techage:ta4_ramchip"},
|
|
|
|
{"techage:ta4_ramchip", "techage:ta4_wlanchip", "techage:ta4_ramchip"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2021-05-09 20:34:13 +03:00
|
|
|
minetest.register_node("techage:ta4_server2", {
|
|
|
|
description = "TA4 Lua Rack Server",
|
|
|
|
tiles = {
|
|
|
|
-- up, down, right, left, back, front
|
|
|
|
"techage_server2_top.png",
|
|
|
|
"techage_server2_top.png",
|
|
|
|
"techage_server2_side.png",
|
|
|
|
"techage_server2_side.png^[transformFX",
|
|
|
|
"techage_server2_back.png",
|
|
|
|
{
|
2023-04-07 11:51:33 +03:00
|
|
|
name = "techage_server2_front.png",
|
2021-05-09 20:34:13 +03:00
|
|
|
backface_culling = false,
|
|
|
|
animation = {
|
|
|
|
type = "vertical_frames",
|
|
|
|
aspect_w = 32,
|
|
|
|
aspect_h = 32,
|
|
|
|
length = 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
drawtype = "nodebox",
|
|
|
|
node_box = {
|
|
|
|
type = "fixed",
|
|
|
|
fixed = {
|
|
|
|
{-0.5, -0.5, -0.5, -0.4375, -0.4375, 0.5},
|
|
|
|
{-0.5, 0.4375, -0.5, -0.4375, 0.5, 0.5},
|
|
|
|
{0.4375, -0.5, -0.5, 0.5, -0.4375, 0.5},
|
|
|
|
{0.4375, 0.4375, -0.5, 0.5, 0.5, 0.5},
|
|
|
|
{-0.5, -0.5, -0.375, -0.4375, 0.5, -0.3125},
|
|
|
|
{-0.5, -0.5, 0.3125, -0.4375, 0.5, 0.375},
|
|
|
|
{0.4375, -0.5, 0.3125, 0.5, 0.5, 0.375},
|
|
|
|
{0.4375, -0.5, -0.375, 0.5, 0.5, -0.3125},
|
|
|
|
{-0.4375, -0.3125, -0.4375, 0.4375, 0.3125, 0.4375},
|
|
|
|
{0.4375, -0.0625, -0.4375, 0.5, 0, 0.4375},
|
|
|
|
{-0.5, -0.0625, -0.4375, -0.4375, 0, 0.4375},
|
|
|
|
}
|
|
|
|
},
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2021-05-09 20:34:13 +03:00
|
|
|
after_place_node = function(pos, placer)
|
|
|
|
local meta = M(pos)
|
|
|
|
local nvm = techage.get_nvm(pos)
|
|
|
|
local number = techage.add_node(pos, "techage:ta4_server2")
|
|
|
|
meta:set_string("owner", placer:get_player_name())
|
|
|
|
meta:set_string("number", number)
|
|
|
|
meta:set_string("formspec", formspec(nvm))
|
|
|
|
nvm.size = 0
|
|
|
|
meta:set_string("infotext", "Server "..number..": ("..nvm.size.."/"..SERVER_CAPA..")")
|
|
|
|
minetest.get_node_timer(pos):start(20)
|
|
|
|
end,
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2021-05-09 20:34:13 +03:00
|
|
|
on_receive_fields = function(pos, formname, fields, player)
|
|
|
|
local meta = M(pos)
|
|
|
|
local nvm = techage.get_nvm(pos)
|
|
|
|
local owner = meta:get_string("owner")
|
|
|
|
if player:get_player_name() == owner then
|
|
|
|
if fields.names and fields.names ~= "" then
|
|
|
|
nvm.names = string.split(fields.names, " ")
|
|
|
|
meta:set_string("formspec", formspec(nvm))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2021-05-09 20:34:13 +03:00
|
|
|
on_dig = function(pos, node, puncher, pointed_thing)
|
|
|
|
if minetest.is_protected(pos, puncher:get_player_name()) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
techage.del_mem(pos)
|
|
|
|
minetest.node_dig(pos, node, puncher, pointed_thing)
|
|
|
|
end,
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2021-05-09 20:34:13 +03:00
|
|
|
after_dig_node = function(pos, oldnode, oldmetadata)
|
|
|
|
techage.remove_node(pos, oldnode, oldmetadata)
|
|
|
|
end,
|
|
|
|
|
|
|
|
on_timer = function(pos, elasped)
|
|
|
|
local meta = M(pos)
|
|
|
|
local nvm = techage.get_nvm(pos)
|
|
|
|
nvm.size = nvm.size or 0
|
|
|
|
local number = meta:get_string("number")
|
|
|
|
meta:set_string("infotext", "Server "..number..": ("..nvm.size.."/"..SERVER_CAPA..")")
|
|
|
|
return true
|
|
|
|
end,
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2021-05-09 20:34:13 +03:00
|
|
|
paramtype = "light",
|
|
|
|
sunlight_propagates = true,
|
|
|
|
use_texture_alpha = techage.CLIP,
|
|
|
|
paramtype2 = "facedir",
|
|
|
|
groups = {choppy=1, cracky=1, crumbly=1},
|
|
|
|
is_ground_content = false,
|
|
|
|
sounds = default.node_sound_stone_defaults(),
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_craft({
|
|
|
|
type = "shapeless",
|
|
|
|
output = "techage:ta4_server2",
|
|
|
|
recipe = {"techage:ta4_server"},
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_craft({
|
|
|
|
type = "shapeless",
|
|
|
|
output = "techage:ta4_server",
|
|
|
|
recipe = {"techage:ta4_server2"},
|
|
|
|
})
|
|
|
|
|
2020-03-06 01:00:51 +03:00
|
|
|
local function calc_size(v)
|
|
|
|
if type(v) == "number" then
|
|
|
|
return 1
|
2021-02-07 16:33:20 +03:00
|
|
|
elseif type(v) == "boolean" then
|
|
|
|
return 1
|
2020-03-06 01:00:51 +03:00
|
|
|
elseif v == nil then
|
|
|
|
return 0
|
|
|
|
elseif type(v) == "string" then
|
|
|
|
return #v
|
|
|
|
elseif v.MemSize then
|
|
|
|
return v.MemSize
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-15 01:02:13 +03:00
|
|
|
local function get_memory(num, name)
|
2020-03-06 01:00:51 +03:00
|
|
|
local info = techage.get_node_info(num)
|
2020-03-15 01:02:13 +03:00
|
|
|
if info and info.pos then
|
|
|
|
local nvm = techage.get_nvm(info.pos)
|
|
|
|
nvm.names = nvm.names or {}
|
|
|
|
for _,n in ipairs(nvm.names) do
|
2020-03-06 01:00:51 +03:00
|
|
|
if name == n then
|
2020-03-15 01:02:13 +03:00
|
|
|
nvm.data = nvm.data or {}
|
2020-03-06 01:00:51 +03:00
|
|
|
return nvm
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function write_value(nvm, key, item)
|
|
|
|
if nvm and nvm.size < SERVER_CAPA then
|
|
|
|
if nvm.data[key] then
|
|
|
|
nvm.size = nvm.size - calc_size(nvm.data[key])
|
|
|
|
end
|
|
|
|
if type(item) == "table" then
|
|
|
|
item = safer_lua.datastruct_to_table(item)
|
|
|
|
end
|
|
|
|
nvm.size = nvm.size + calc_size(item)
|
|
|
|
nvm.data[key] = item
|
2020-03-15 01:02:13 +03:00
|
|
|
return true
|
2020-03-06 01:00:51 +03:00
|
|
|
end
|
2020-03-15 01:02:13 +03:00
|
|
|
return false
|
2022-01-03 23:40:31 +03:00
|
|
|
end
|
2020-03-06 01:00:51 +03:00
|
|
|
|
|
|
|
local function read_value(nvm, key)
|
|
|
|
local item = nvm.data[key]
|
|
|
|
if type(item) == "table" then
|
|
|
|
item = safer_lua.table_to_datastruct(item)
|
|
|
|
end
|
|
|
|
return item
|
2022-01-03 23:40:31 +03:00
|
|
|
end
|
2020-03-06 01:00:51 +03:00
|
|
|
|
2021-05-09 20:34:13 +03:00
|
|
|
techage.register_node({"techage:ta4_server", "techage:ta4_server2"}, {
|
2020-03-15 01:02:13 +03:00
|
|
|
on_recv_message = function(pos, src, topic, payload)
|
2020-03-06 01:00:51 +03:00
|
|
|
return "unsupported"
|
|
|
|
end,
|
|
|
|
on_node_load = function(pos)
|
|
|
|
minetest.get_node_timer(pos):start(20)
|
|
|
|
end,
|
2022-01-03 23:40:31 +03:00
|
|
|
})
|
2020-03-06 01:00:51 +03:00
|
|
|
|
|
|
|
|
|
|
|
techage.lua_ctlr.register_function("server_read", {
|
2022-01-03 23:40:31 +03:00
|
|
|
cmnd = function(self, num, key)
|
2020-03-06 01:00:51 +03:00
|
|
|
if type(key) == "string" then
|
2020-03-15 01:02:13 +03:00
|
|
|
local nvm = get_memory(num, self.meta.owner)
|
2020-03-06 01:00:51 +03:00
|
|
|
if nvm then
|
|
|
|
return read_value(nvm, key)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
self.error("Invalid server_read parameter")
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
help = " $server_read(num, key)\n"..
|
|
|
|
" Read a value from the server.\n"..
|
|
|
|
" 'key' must be a string.\n"..
|
2020-03-15 01:02:13 +03:00
|
|
|
' example: state = $server_read("123", "state")'
|
2020-03-06 01:00:51 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
techage.lua_ctlr.register_action("server_write", {
|
|
|
|
cmnd = function(self, num, key, value)
|
|
|
|
if type(key) == "string" then
|
2020-03-15 01:02:13 +03:00
|
|
|
local nvm = get_memory(num, self.meta.owner)
|
2020-03-06 01:00:51 +03:00
|
|
|
if nvm then
|
2020-03-15 01:02:13 +03:00
|
|
|
return write_value(nvm, key, value)
|
2020-03-06 01:00:51 +03:00
|
|
|
end
|
|
|
|
else
|
|
|
|
self.error("Invalid server_write parameter")
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
help = " $server_write(num, key, value)\n"..
|
|
|
|
" Store a value on the server under the key 'key'.\n"..
|
|
|
|
" 'key' must be a string. 'value' can be either a\n"..
|
|
|
|
" number, string, boolean, nil or data structure.\n"..
|
2020-03-15 01:02:13 +03:00
|
|
|
" return value: true if successful or false\n"..
|
|
|
|
' example: res = $server_write("123", "state", state)'
|
2020-03-06 01:00:51 +03:00
|
|
|
})
|