finished implementation of precondition "evaluate"

This commit is contained in:
Sokomine 2022-07-27 01:05:47 +02:00
parent 073b9d4284
commit dae9e308b4
3 changed files with 37 additions and 9 deletions

View File

@ -204,7 +204,6 @@ yl_speak_up.custom_functions_r_ = {}
-- example function for preconditions:
yl_speak_up.custom_functions_p_[ "example function" ] = {
function_call = "yl_speak_up.custom_example_function",
description = "This function is just an example. It tells the player its parameters.",
param1_text = "1. Parameter:",
param1_desc = "This is the value passed to the function as first parameter.",
@ -224,17 +223,36 @@ yl_speak_up.custom_functions_p_[ "example function" ] = {
param8_desc = "This is the value passed to the function as 8. parameter.",
param9_text = "9. Parameter:",
param9_desc = "This is the value passed to the function as 9. parameter.",
-- the actual implementation of the function
code = function(player, n_id, p)
local pname = player:get_player_name()
local str = ""
for i = 1,9 do
str = str.."\n\tParameter "..tostring(i)..": "..tostring(p["p_param"..tostring(i)])
end
minetest.chat_send_player(pname, "Checking precondition "..tostring(p.p_id)..
" for NPC with ID "..tostring(n_id)..": Executing custom function "..
tostring(p.p_value).." with the following parameters:"..
str.."\n(This function just tells you its parameters and returns true.)")
-- this function is just for demonstration; it always returns true
return true
end,
}
yl_speak_up.custom_functions_p_[ "get_level_of_player" ] = {
function_call = "yl_speak_up.get_player_level",
description = "Get the level the player has achieved.",
yl_speak_up.custom_functions_p_[ "get_xp_of_player" ] = {
description = "Get the xp the player has achieved (requires xp_redo).",
code = function(player, n_id, p)
local pname = player:get_player_name()
if(minetest.get_modpath("xp_redo")) then
return xp_redo.get_xp(pname)
end
return 0
end
}
-- TODO: actually implement these as examples
yl_speak_up.custom_functions_r_[ "set_variable_to_random_number" ] = {
function_call = "yl_speak_up.set_variable_to_random_number",
description = "Set a variable to a random number.",
param1_text = "Name of the variable:",
param1_desc = "Which variable do you want to set to a random number?",
@ -245,7 +263,6 @@ yl_speak_up.custom_functions_r_[ "set_variable_to_random_number" ] = {
}
yl_speak_up.custom_functions_r_[ "set_variable_to_random_value_from_list" ] = {
function_call = "yl_speak_up.select_random_value_from_list",
description = "Set a variable to a random value from a list.",
param1_text = "Name of the variable:",
param1_desc = "Which variable do you want to set to a random value from your list?",
@ -257,7 +274,6 @@ yl_speak_up.custom_functions_r_[ "set_variable_to_random_value_from_list" ] = {
-- example function for results/effects:
yl_speak_up.custom_functions_r_[ "example function" ] = {
function_call = "yl_speak_up.custom_example_function",
-- Describe here in short form what your function does:
description = "This function is just an example. It tells the player its parameters.",
param1_text = "1. Parameter:",

View File

@ -262,7 +262,18 @@ yl_speak_up.eval_precondition = function(player, n_id, p, other_options_true_or_
return yl_speak_up.eval_precondition_with_operator(p, properties[p.p_value])
elseif(p.p_type == "evaluate") then
return true -- TODO
if(not(player) or not(p.p_value)) then
return false
end
local custom_data = yl_speak_up.custom_functions_p_[p.p_value]
if(not(custom_data) or not(custom_data.code)) then
return false
end
local fun = custom_data.code
-- actually call the function
local ret = fun(player, n_id, p)
-- compare the result with wat is expected
return yl_speak_up.eval_precondition_with_operator(p, ret)
elseif(p.p_type == "block") then
if(not(p.p_pos) or type(p.p_pos) ~= "table"
or not(p.p_pos.x) or not(p.p_pos.y) or not(p.p_pos.z)) then

View File

@ -246,8 +246,9 @@ yl_speak_up.show_precondition = function(p, pname)
str = str..","
end
end
local i_op = math.max(1,table.indexof(values_operator, p.p_operator))
return "FUNCTION["..tostring(p.p_value).."]"..
"("..str..") "..tostring(check_operator[p.p_operator])..
"("..str..") "..tostring(check_operator[i_op])..
" "..tostring(p.p_var_cmp_value)
elseif(p.p_type == "block") then
if(not(p.p_pos) or type(p.p_pos) ~= "table"