added yl_speak_up.custom_server_functions.precondition_descriptions and *.precondition_eval

This commit is contained in:
Sokomine 2022-01-17 03:07:45 +01:00
parent 7143f81995
commit 04b89c2e5e
3 changed files with 69 additions and 14 deletions

View File

@ -1,13 +1,57 @@
-- This file contains what is necessary to execute/evaluate a precondition.
-- some internal ones...
yl_speak_up.precondition_check_variable = {
-- "- please select -", -- this is automaticly added to the var list
"(internal) hour of ingame day", -- 2
"(internal) player's health points", -- 3
yl_speak_up.custom_server_functions = {}
-- When you change an existing texts NPC that make use of that precondition will no
-- longer be able to recognize it. You have to reconfigure all those NPC!
-- Adding new texts is no problem.
yl_speak_up.custom_server_functions.precondition_descriptions = {
"(internal) hour of ingame day",
"(internal) player's health points",
"(internal) player has priv:",
}
-- please return a useful value depending on the function;
-- parameter:
-- player the player object
-- desc entry from yl_speak_up.custom_server_functions.precondition_descriptions
-- precondition the data of the precondition for additional information;
-- precondition.p_var_cmp_value contains a user-specified value against which
-- the return value of this function is checked. This depends on
-- precondition.p_operator. Depending on the operator, precondition.p_var_cmp_value
-- may also be used as a parameter to this function.
-- Just make sure to tell your users what return values they shall expect and which
-- operators make sense!
yl_speak_up.custom_server_functions.precondition_eval = function(player, descr, precondition)
if(descr == "(internal) hour of ingame day") then
-- timeofday is between 0..1; translate to 24 hours
return math.floor((minetest.get_timeofday() * 24)+0.5)
elseif(descr == "(internal) player's health points") then
return player:get_hp()
elseif(descr == "(internal) player has priv:") then
-- the name of the priv is derived from the parameter
if(minetest.check_player_privs(player,
minetest.string_to_privs(tostring(precondition.p_var_cmp_value)))) then
-- TODO: change to true/false when new operator is available
return tostring(precondition.p_var_cmp_value)
else
return "false"
end
-- if this custom server function does not exist: return false
else
return false
end
end
-- this is called directly in yl_speak_up.get_fs_talkdialog
-- it returns a list of options whose preconditions are fulfilled
-- allow_recursion may be false - we need to avoid infinite loops
@ -100,13 +144,13 @@ yl_speak_up.eval_precondition = function(player, n_id, p, other_options_true_or_
if(not(p.p_variable) or p.p_variable == "") then
-- broken precondition
return false
-- "(internal) hour of ingame day", -- 2
elseif(p.p_variable == yl_speak_up.precondition_check_variable[2]) then
-- timeofday is between 0..1; translate to 24 hours
var_val = math.floor((minetest.get_timeofday() * 24)+0.5)
-- "(internal) player's health points", -- 3
elseif(p.p_variable == yl_speak_up.precondition_check_variable[3]) then
var_val = player:get_hp()
-- internal custom server functions for preconditions
elseif(table.indexof(yl_speak_up.custom_server_functions.precondition_descriptions,
p.p_variable) > -1) then
-- evaluate it
var_val = yl_speak_up.custom_server_functions.precondition_eval(
player, p.p_variable, p)
print("VAR_VAL is: "..tostring(var_val)) -- TODO
else
local pname = player:get_player_name()
local owner = yl_speak_up.npc_owner[ n_id ]

View File

@ -284,7 +284,18 @@ yl_speak_up.save_element_p_or_a_or_e = function(
v[ id_prefix.."value" ] = "expression"
v[ id_prefix.."operator" ] = values_operator[ data.operator ]
v[ id_prefix.."var_cmp_value" ] = (data.var_cmp_value or "")
v[ id_prefix.."variable" ] = yl_speak_up.add_pname_to_var(data.variable_name, pname)
-- if it is a custom server function,then do not preifx it with $ playername
if(id_prefix == "p_") then
local idx = table.indexof(yl_speak_up.custom_server_functions.precondition_descriptions,
data.variable_name)
if(idx > -1) then
v[ id_prefix.."variable" ] = data.variable_name
else
v[ id_prefix.."variable" ] = yl_speak_up.add_pname_to_var(data.variable_name, pname)
end
else
v[ id_prefix.."variable" ] = yl_speak_up.add_pname_to_var(data.variable_name, pname)
end
-- "a block somewhere", -- 3
elseif(what_type == "block" and id_prefix ~= "a_") then

View File

@ -152,8 +152,8 @@ local values_operator = {"", "==", "~=", ">=", ">", "<=", "<", "not", "is_set",
-- get the list of variables the player has read access to
yl_speak_up.get_sorted_player_var_list_read_access = function(pname)
local var_list = {}
-- copy the values from check_variable
for i, v in ipairs(yl_speak_up.precondition_check_variable) do
-- copy the values that are server-specific
for i, v in ipairs(yl_speak_up.custom_server_functions.precondition_descriptions) do
table.insert(var_list, v)
end
-- get the list of variables the player can read