generated from your-land/yl_template
Cleans api.lua
This commit is contained in:
parent
93b177d18c
commit
df0be1e07d
89
api.lua
89
api.lua
@ -287,92 +287,3 @@ end
|
||||
|
||||
function yl_api_nodestages.register_stages(stages) return
|
||||
register_stages(stages) end
|
||||
|
||||
--[[ meh
|
||||
|
||||
function yl_api_nodestages.a(stages, overwrite)
|
||||
|
||||
-- defense
|
||||
|
||||
if ((type(overwrite) ~= "boolean") and (overwrite ~= nil)) then
|
||||
return false, yl_api_nodestages.t("error_not_a_boolean_or_nil",
|
||||
dump(overwrite))
|
||||
end
|
||||
|
||||
-- modname is now part of the stage and needs to be validated there
|
||||
--if (minetest.get_modpath(modname) ~= "string") then
|
||||
-- return false, yl_api_nodestages.t("error_not_a_mod", dump(modname))
|
||||
--end
|
||||
|
||||
if (type(stages) ~= "table") then
|
||||
return false, yl_api_nodestages.t("error_not_a_table", dump(stages))
|
||||
end
|
||||
|
||||
if (yl_api_nodestages.is_valid(stages) == false) then
|
||||
return false,
|
||||
yl_api_nodestages.t("error_not_a_valid_table", dump(stages))
|
||||
end
|
||||
|
||||
-- payload
|
||||
|
||||
local num_stages = 0
|
||||
|
||||
-- TODO: Specialcase empty table deletes all stages
|
||||
|
||||
for i = #stages, 1, -1 do
|
||||
local stage = table.copy(stages[i])
|
||||
|
||||
assert((stage ~= nil), "TODO: ERROR")
|
||||
|
||||
if type(stage.next_stages) == "nil" then
|
||||
stage.duration = 0
|
||||
else
|
||||
-- if next_stages exists and duration is not set then exit with error during server load
|
||||
assert((stage.duration ~= nil), "TODO: ERROR")
|
||||
|
||||
if type(stage.next_stages) == 'string' then
|
||||
stage.next_stages = {{stage.next_stages, 1}}
|
||||
end
|
||||
|
||||
-- Unless we require the content level mod to depend on others, we can't validate this here
|
||||
--for _, next_stage in ipairs(stage.next_stages) do
|
||||
-- -- if node is not registered then crash server! :D
|
||||
-- assert(minetest.registered_nodes[next_stage[1] ], "TODO: ERROR")
|
||||
--end
|
||||
end
|
||||
|
||||
local node_definition = table.copy(stage.node_definition)
|
||||
local nodename = stage.stage_name
|
||||
|
||||
if (((overwrite == false) or (overwrite == nil)) and
|
||||
(minetest.registered_nodes[nodename] ~= nil)) then
|
||||
-- No overwrite, but item exists
|
||||
return false,
|
||||
yl_api_nodestages.t("error_item_exists", dump(nodename))
|
||||
end
|
||||
|
||||
-- MT properties
|
||||
node_definition.description = stage.description
|
||||
node_definition.tiles = stage.tiles
|
||||
|
||||
-- custom properties
|
||||
node_definition._stage = stage
|
||||
|
||||
-- node functions
|
||||
if ((minetest.registered_nodes[nodename] == nil) or
|
||||
(minetest.registered_nodes[nodename].on_timer)) then
|
||||
node_definition.on_timer = yl_api_nodestages.on_timer
|
||||
end
|
||||
|
||||
node_definition.on_construct = yl_api_nodestages.on_construct
|
||||
node_definition.on_destruct = yl_api_nodestages.on_destruct
|
||||
|
||||
minetest.register_node(":" .. nodename, node_definition)
|
||||
|
||||
num_stages = num_stages + 1
|
||||
end
|
||||
|
||||
return true,
|
||||
yl_api_nodestages.t("success_changed_num_stages", dump(num_stages))
|
||||
end
|
||||
]]--
|
191
internal.lua
191
internal.lua
@ -11,192 +11,5 @@ end
|
||||
|
||||
function yl_api_nodestages.log(text) return log(text) end
|
||||
|
||||
local function remove_timer(pos)
|
||||
local t = minetest.get_node_timer(pos)
|
||||
t:stop()
|
||||
end
|
||||
|
||||
function yl_api_nodestages.remove_timer(pos) return remove_timer(pos) end
|
||||
|
||||
function yl_api_nodestages.on_destruct(pos) return remove_timer(pos) end
|
||||
|
||||
local function on_construct(pos)
|
||||
local node = minetest.get_node(pos)
|
||||
local nodename = node.name
|
||||
local stage = minetest.registered_nodes[nodename]._stage
|
||||
local duration = stage.duration or 0
|
||||
local node_definition = minetest.registered_nodes[nodename]
|
||||
|
||||
-- node functions
|
||||
if ((minetest.registered_nodes[nodename] == nil) or
|
||||
-- Only add a nodetimer if the node does not yet have one
|
||||
(minetest.registered_nodes[nodename].on_timer)) then
|
||||
node_definition.on_timer = yl_api_nodestages.on_timer
|
||||
end
|
||||
|
||||
if (duration > 0) then
|
||||
local current_duration = duration
|
||||
local t = minetest.get_node_timer(pos)
|
||||
t:start(current_duration)
|
||||
end
|
||||
end
|
||||
|
||||
function yl_api_nodestages.on_construct(pos) return on_construct(pos) end
|
||||
|
||||
local function get_target_nodename(node, pos, next_stages)
|
||||
local sum = 0
|
||||
for _, stage_chance in pairs(next_stages) do sum = sum + stage_chance[2] end
|
||||
local dice = math.random(sum)
|
||||
for _, stage_chance in pairs(next_stages) do
|
||||
dice = dice - stage_chance[2]
|
||||
if dice <= 0 then
|
||||
if (minetest.registered_nodes[stage_chance[1]] == nil) then
|
||||
minetest.log("warning",
|
||||
yl_api_nodestages.t(
|
||||
"target_nodestage_does_not_exist",
|
||||
dump(stage_chance[1]), dump(node.name),
|
||||
dump(pos)))
|
||||
return nil
|
||||
elseif ((stage_chance[3] ~= nil) and
|
||||
(type(stage_chance[3].can_set) == "function") and
|
||||
(stage_chance[3].can_set(pos) == false)) then
|
||||
return node.name
|
||||
else
|
||||
return stage_chance[1]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- should never return this
|
||||
return yl_api_nodestages.error
|
||||
end
|
||||
|
||||
local function on_timer(pos, elapsed)
|
||||
|
||||
local node = minetest.get_node(pos)
|
||||
|
||||
local stage = minetest.registered_nodes[node.name]._stage
|
||||
local target_nodename
|
||||
local timer = 0
|
||||
local duration
|
||||
|
||||
if ((stage == nil) or (stage.next_stages == nil) or (stage.duration == nil)) then
|
||||
yl_api_nodestages.remove_timer(pos)
|
||||
return
|
||||
end
|
||||
|
||||
repeat
|
||||
duration = stage.duration
|
||||
timer = timer + duration
|
||||
target_nodename = get_target_nodename(node, pos, stage.next_stages)
|
||||
|
||||
if (target_nodename == yl_api_nodestages.error) then
|
||||
-- Thing kaputt, stop it.
|
||||
minetest.log("error", yl_api_nodestages.t("undefined_state_abort"))
|
||||
yl_api_nodestages.remove_timer(pos)
|
||||
return
|
||||
end
|
||||
|
||||
if (target_nodename == nil) then
|
||||
-- Fallback to last known target_nodename
|
||||
target_nodename = stage.stage_name
|
||||
break
|
||||
else
|
||||
-- Last stage most often does not have a _stage
|
||||
stage = minetest.registered_nodes[target_nodename] and
|
||||
minetest.registered_nodes[target_nodename]._stage or nil
|
||||
end
|
||||
until ((timer >= elapsed) or (stage == nil) or (stage.duration == 0) or (stage.duration == nil))
|
||||
|
||||
if (node.name == target_nodename) then
|
||||
return true end
|
||||
|
||||
minetest.set_node(pos, {name = target_nodename})
|
||||
|
||||
local remaining = timer - elapsed
|
||||
if (stage and stage.duration and (remaining > 0)) then
|
||||
local nodetimer = minetest.get_node_timer(pos)
|
||||
nodetimer:set(stage.duration, remaining)
|
||||
end
|
||||
end
|
||||
|
||||
function yl_api_nodestages.on_timer(pos, elapsed) return on_timer(pos, elapsed) end
|
||||
|
||||
-- Validation
|
||||
--
|
||||
|
||||
function get_valid_copy_of_stage(stage)
|
||||
if (type(stage) ~= "table") then return false, t("stage_not_table") end
|
||||
|
||||
local stage = table.copy(stage)
|
||||
|
||||
if (yl_api_nodestages.is_valid_stage(stage) == false) then
|
||||
return false, t("stage_not_valid")
|
||||
end
|
||||
|
||||
--[[
|
||||
local nodename = stage.stage_name
|
||||
local overwrite = stage.overwrite
|
||||
|
||||
if (((overwrite == false) or (overwrite == nil)) and
|
||||
(minetest.registered_nodes[nodename] ~= nil)) then
|
||||
-- No overwrite, but item exists
|
||||
return false, yl_api_nodestages.t("error_item_exists", dump(nodename))
|
||||
end
|
||||
]]--
|
||||
|
||||
return true, stage
|
||||
|
||||
end
|
||||
|
||||
function yl_api_nodestages.get_valid_copy_of_stage(stage)
|
||||
return get_valid_copy_of_stage(stage)
|
||||
end
|
||||
|
||||
local function is_valid_stage(stage)
|
||||
return true -- TODO: Implement validation
|
||||
end
|
||||
|
||||
function yl_api_nodestages.is_valid_stage(stage)
|
||||
return is_valid_stage(stage)
|
||||
end
|
||||
|
||||
function yl_api_nodestages.is_valid(stages)
|
||||
|
||||
local good, bad, total = 0, 0, 0
|
||||
local reasons = {}
|
||||
|
||||
for _, stage in ipairs(stages) do
|
||||
total = total + 1
|
||||
local success, message = is_valid_stage(stage)
|
||||
if (success == true) then
|
||||
good = good + 1
|
||||
else
|
||||
bad = bad + 1
|
||||
table.insert(reasons, message)
|
||||
end
|
||||
end
|
||||
|
||||
if (bad == 0) then
|
||||
minetest.log("action",
|
||||
"[MOD] yl_api_nodestages : bad = " .. tostring(bad) ..
|
||||
", good = " .. tostring(good) .. ", total = " ..
|
||||
tostring(total))
|
||||
return true, good, bad, total, reasons
|
||||
else
|
||||
minetest.log("warning",
|
||||
"[MOD] yl_api_nodestages : bad = " .. tostring(bad) ..
|
||||
", good = " .. tostring(good) .. ", total = " ..
|
||||
tostring(total))
|
||||
return false, good, bad, total, reasons
|
||||
end
|
||||
end
|
||||
|
||||
function yl_api_nodestages.validate_all_stages()
|
||||
local stages = {}
|
||||
for _, def in pairs(minetest.registered_nodes) do
|
||||
local stage = def._stage
|
||||
table.insert(stages, stage)
|
||||
end
|
||||
return yl_api_nodestages.is_valid(stages)
|
||||
end
|
||||
dofile(yl_api_nodestages.modpath .. "internal_nodefunctions.lua")
|
||||
dofile(yl_api_nodestages.modpath .. "internal_validation.lua")
|
||||
|
Loading…
Reference in New Issue
Block a user