cleanup and commented documentation

master
Juraj Vajda 2018-01-22 02:35:05 -05:00
parent 43d60e2bcc
commit 36f9b3d784
2 changed files with 59 additions and 26 deletions

76
api.lua
View File

@ -1,14 +1,17 @@
local enable_particles = minetest.settings:get_bool("enable_particles")
-- @module telemosaic
telemosaic = {}
-- @field extender_ranges
telemosaic.extender_ranges = {
-- not adding beacons here, since they don"t extend
["telemosaic:extender_one"] = 5,
["telemosaic:extender_two"] = 20,
["telemosaic:extender_three"] = 80
}
-- @field strengths
telemosaic.strengths = { "one", "two", "three" }
--- Add particle effect on deprature
-- @param {table} pos - deprature position
function telemosaic.effect_departure(pos)
minetest.sound_play("telemosaic_set", {
pos = pos,
@ -36,6 +39,8 @@ function telemosaic.effect_departure(pos)
})
end
--- Add particle effect on arrival
-- @param {table} pos - arrival position
function telemosaic.effect_arrival(pos)
minetest.sound_play("telemosaic_teleport", {
pos = pos,
@ -63,6 +68,10 @@ function telemosaic.effect_arrival(pos)
})
end
--- Generate formspec dynamically
-- @param {table} pos - position of the node
-- @param {table} table - custom parameters passed in the table
-- @return {string} formspec - formspec string what can be set to metadata
function telemosaic.get_formspec(pos, table)
local meta = minetest.get_meta(pos)
local arrivals_tbl = meta:get_string("arrivals_tbl")
@ -78,11 +87,11 @@ function telemosaic.get_formspec(pos, table)
local textlist = ""
for ipos, ival in pairs(arrivals_tbl) do
print("ival: ", dump(ival))
textlist = textlist..minetest.formspec_escape(ipos)..": "..ival..","
end
local button_teleport = ""
local pos2_colorize = minetest.colorize("#ff0000", pos2_str)
if pos2 then
@ -109,6 +118,8 @@ function telemosaic.get_formspec(pos, table)
return formspec
end
--- Minetest "register_on_player_receive_fields" function
-- @see https://github.com/minetest/minetest/blob/master/doc/lua_api.txt
minetest.register_on_player_receive_fields(function(player, formname, fields)
local mod_pos_pos2 = formname:split(":")
@ -158,7 +169,12 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end
end)
--
-- extenders
--
--- Minetest "after_place_node" function
-- @see https://github.com/minetest/minetest/blob/master/doc/lua_api.txt
function telemosaic.extender_after_place(pos, placer, itemstack, pointed_thing)
local positions = minetest.find_nodes_in_area_under_air(
{x = pos.x - 3, y = pos.y, z = pos.z - 3},
@ -186,13 +202,15 @@ function telemosaic.extender_after_place(pos, placer, itemstack, pointed_thing)
bmeta:set_int("range", brange)
telemosaic.set_status(bpos, placer)
bmeta:set_string("infotext", bdescription.."\nowner: "..bowner.."\nrange: "..brange.." blocks\nright-click to update info or with mese crystal fragment setup departure position")
bmeta:set_string("infotext", bdescription.."\nowner: "..bowner.."\nrange: "..brange.." blocks\nright-click for more info")
end
meta:set_string("owner", playername)
meta:set_string("infotext", description.."\nowner: "..playername)
end
--- Minetest "on_destruct" function
-- @see https://github.com/minetest/minetest/blob/master/doc/lua_api.txt
function telemosaic.extender_on_destruct(pos)
local node = minetest.get_node(pos)
if not node then return end
@ -219,12 +237,19 @@ function telemosaic.extender_on_destruct(pos)
bmeta:set_int("range", brange)
telemosaic.set_status(bpos)
bmeta:set_string("infotext", bdescription.."\nowner: "..bowner.."\nrange: "..brange.." blocks\nright-click to update info or with mese crystal fragment setup departure position")
bmeta:set_string("infotext", bdescription.."\nowner: "..bowner.."\nrange: "..brange.." blocks\nright-click for more info")
end
end
--
-- beacons
--
--- Finds extenders in the area under air, counts the range based on the found extenders power/strength
-- @param {table} be_pos - beacon position as a center point from where extenders will be searched
-- @returns {number} range - total power from found extenders
function telemosaic.get_range_from_extenders(be_pos)
-- 7x1x7 area
local positions = minetest.find_nodes_in_area_under_air(
{x = be_pos.x - 3, y = be_pos.y, z = be_pos.z - 3},
{x = be_pos.x + 3, y = be_pos.y, z = be_pos.z + 3},
@ -242,6 +267,8 @@ function telemosaic.get_range_from_extenders(be_pos)
return range
end
--- Manage state of the beacons, i.e. swap nodes and update metadata
-- @param {table} pos - beacon position of which the state should be managed
function telemosaic.set_status(pos)
local meta = minetest.get_meta(pos)
if not meta then return end
@ -273,9 +300,10 @@ function telemosaic.set_status(pos)
minetest.swap_node(pos, { name = "telemosaic:beacon" })
meta:set_string("state", "on")
end
end
--- Minetest node "after_place_node" function
-- @see https://github.com/minetest/minetest/blob/master/doc/lua_api.txt
function telemosaic.beacon_after_place(pos, placer, itemstack, pointed_thing)
local meta = minetest.get_meta(pos)
local nodename = itemstack:get_name()
@ -286,7 +314,7 @@ function telemosaic.beacon_after_place(pos, placer, itemstack, pointed_thing)
local bname = "beacon at: "..minetest.formspec_escape(minetest.pos_to_string(pos))
local description = minetest.registered_nodes[nodename]["description"]
print("playername: ", playername)
meta:set_int("range", range)
meta:set_string("bname", bname)
meta:set_string("owner", playername)
@ -294,11 +322,13 @@ function telemosaic.beacon_after_place(pos, placer, itemstack, pointed_thing)
meta:set_string("dep_pos", "")
meta:set_string("dest_pos", "not configured")
meta:set_string("arrivals_tbl", "{}")
meta:set_string("infotext", description.."\nowner: "..playername.."\nrange: "..range.." blocks\nright-click to update info or with mese crystal fragment setup departure position")
meta:set_string("infotext", description.."\nowner: "..playername.."\nrange: "..range.." blocks\nright-click for more info")
telemosaic.set_status(pos)
end
--- Minetest node "on_rightclick" function
-- @see https://github.com/minetest/minetest/blob/master/doc/lua_api.txt
function telemosaic.beacon_rightclick(pos, node, clicker, itemstack, pointed_thing)
local meta = minetest.get_meta(pos)
local meta_stack = itemstack:get_meta()
@ -307,7 +337,6 @@ function telemosaic.beacon_rightclick(pos, node, clicker, itemstack, pointed_thi
local nodename = node.name
local stackname = itemstack:get_name()
local range = telemosaic.get_range_from_extenders(pos)
local state = meta:get_string("state")
if not minetest.registered_nodes[nodename] then return end
local description = minetest.registered_nodes[nodename]["description"]
@ -317,35 +346,36 @@ function telemosaic.beacon_rightclick(pos, node, clicker, itemstack, pointed_thi
if stackname == "default:mese_crystal_fragment" and itemstack:get_count() == 1 then
itemstack:replace("telemosaic:key")
-- remember departure position in the item meta
meta_stack:set_string("dep_pos", minetest.pos_to_string(pos))
meta:set_string("infotext", description.."\nowner: "..ownername.."\nrange: "..range.." blocks\nstart pos: "..minetest.pos_to_string(pos).."\nright-click to update info")
meta:set_string("infotext", description.."\nowner: "..ownername.."\nrange: "..range.." blocks\nright-click for more info")
-- destination
elseif stackname == "telemosaic:key" then
-- get deprature position from item
-- get deprature position from item meta
local pos_stack = meta_stack:get_string("dep_pos")
pos_stack = minetest.string_to_pos(pos_stack)
-- do nothing if departure position is marked as arrival position
if math.floor(vector.distance(pos_stack, pos)) == 0 then
minetest.chat_send_player(clickername, "This is marked as your departure position, you have to mark your destination position now!")
return
-- do nothing when arrival position is protected
elseif minetest.is_protected(pos, clickername) then
minetest.chat_send_player(clickername, "You cannot configure protected beacons!")
return
end
-- enough extenders / range
-- if math.floor(vector.distance(pos_stack, pos)) <= range then
minetest.swap_node(pos, { name = "telemosaic:beacon" })
meta:set_string("state", "on")
itemstack:replace("default:mese_crystal_fragment")
-- set arrival pos to departure pos node
-- remove this node position from arrival node "arrivals_tbl"
local meta2 = minetest.get_meta(pos_stack)
local bname2 = meta2:get_string("bname")
local pos3 = meta2:get_string("dest_pos")
pos3 = minetest.string_to_pos(pos3)
-- not for the node we just clicked though
if pos3 and math.floor(vector.distance(pos3, pos)) ~= 0 then
local meta3 = minetest.get_meta(pos3)
local arrivals_tbl3 = meta3:get_string("arrivals_tbl")
@ -354,7 +384,9 @@ function telemosaic.beacon_rightclick(pos, node, clicker, itemstack, pointed_thi
arrivals_tbl3[minetest.pos_to_string(pos_stack)] = nil
meta3:set_string('arrivals_tbl', arrivals_tbl3)
telemosaic.set_status(pos3)
end
end
-- add arrival position to the arrival table list
local arrivals_tbl = meta:get_string("arrivals_tbl")
arrivals_tbl = minetest.deserialize(arrivals_tbl)
if not arrivals_tbl then arrivals_tbl = {} end
@ -365,26 +397,29 @@ function telemosaic.beacon_rightclick(pos, node, clicker, itemstack, pointed_thi
meta2:set_string("dest_pos", minetest.pos_to_string(pos))
meta:set_string("arrivals_tbl", arrivals_tbl)
meta:set_string("infotext", description.."\nowner: "..ownername.."\nrange: "..range.." blocks\nright-click for more info")
telemosaic.set_status(pos_stack)
meta:set_string("infotext", description.."\nowner: "..ownername.."\nrange: "..range.." blocks\nend pos: "..minetest.pos_to_string(pos).."\nright-click to update info")
-- default place_node callback
elseif itemstack:get_definition().type == "node" then
itemstack = minetest.item_place_node(itemstack, clicker, pointed_thing)
else
-- mod : pos : pos2
-- - pos2 is from itemstack so not always set
-- - pos2 is from itemstack so not always set, else "not configured"
local formspec = telemosaic.get_formspec(pos, {
range = range
})
local pos2 = meta:get_string("dest_pos")
minetest.show_formspec(clickername, "telemosaic:"..minetest.pos_to_string(pos)..":"..pos2, formspec)
meta:set_string("infotext", description.."\nowner: "..ownername.."\nrange: "..range.." blocks\nright-click to update info")
meta:set_string("infotext", description.."\nowner: "..ownername.."\nrange: "..range.." blocks\nright-click for more info")
end
return itemstack
end
--- Minetest node "on_destruct" function
-- @see https://github.com/minetest/minetest/blob/master/doc/lua_api.txt
function telemosaic.beacon_on_destruct(pos)
local meta = minetest.get_meta(pos)
local pos2 = meta:get_string("dest_pos")
@ -410,6 +445,7 @@ function telemosaic.beacon_on_destruct(pos)
telemosaic.set_status(pos2)
end
-- update meta in all arrivals list from this node
for k, v in pairs(arrivals_tbl) do
local ipos = minetest.string_to_pos(k)
local imeta = minetest.get_meta(ipos)

View File

@ -42,14 +42,12 @@ minetest.register_node("telemosaic:beacon", {
"telemosaic_beacon_side.png",
},
is_ground_content = false,
groups = { cracky = 2 },
-- groups = { cracky = 2, not_in_creative_inventory = 1 },
groups = { cracky = 2, not_in_creative_inventory = 1 },
drop = "telemosaic:beacon_off",
sounds = default.node_sound_stone_defaults(),
on_rightclick = telemosaic.beacon_rightclick,
on_destruct = telemosaic.beacon_on_destruct,
on_receive_fields = telemosaic.on_receive_fields,
-- on_punch = telemosaic.beacon_on_punch,
})
minetest.register_node("telemosaic:beacon_err", {
@ -63,8 +61,7 @@ minetest.register_node("telemosaic:beacon_err", {
"telemosaic_beacon_side.png",
},
is_ground_content = false,
groups = { cracky = 2 },
-- groups = { cracky = 2, not_in_creative_inventory = 1 },
groups = { cracky = 2, not_in_creative_inventory = 1 },
drop = "telemosaic:beacon_off",
sounds = default.node_sound_stone_defaults(),
on_rightclick = telemosaic.beacon_rightclick,
@ -77,5 +74,5 @@ minetest.register_tool("telemosaic:key", {
description = "Telemosaic key",
inventory_image = "telemosaic_key.png",
stack_max = 1,
-- groups = { not_in_creative_inventory = 1 },
groups = { not_in_creative_inventory = 1 },
})