implemented precondition 'property' and prepared precondition 'evaluate'

This commit is contained in:
Sokomine 2022-06-19 02:49:25 +02:00
parent 8bfc59a9a2
commit 4612576cfc
4 changed files with 288 additions and 104 deletions

View File

@ -21,6 +21,15 @@ yl_speak_up.calculate_available_generic_dialogs = function(current_n_id, player)
local pname = player:get_player_name()
-- the IDs of all those NPCs whose dialogs can be added
local n_id_list = {}
-- cache the properties of the NPC
local properties = yl_speak_up.get_npc_properties(pname)
if(not(properties)) then
properties = {}
else
properties = properties.properties
end
-- Let's go through all the options and see if we need to display them to the user
-- check all options: option key (n_id), option value/data (list of preconditions)
for n_id, prereq in pairs(yl_speak_up.generic_dialog_conditions) do
@ -33,7 +42,7 @@ yl_speak_up.calculate_available_generic_dialogs = function(current_n_id, player)
if(not(include_this)
-- only certain types of preconditions are allowed because the other ones would
-- be too expensive or make no sense here
or not(yl_speak_up.eval_precondition(player, current_n_id, p, nil))) then
or not(yl_speak_up.eval_precondition(player, current_n_id, p, nil, properties))) then
include_this = false
break
end

View File

@ -55,10 +55,20 @@ yl_speak_up.eval_all_preconditions = function(player, prereq, o_id, other_option
return true
end
yl_speak_up.debug_msg(player, n_id, o_id, "Checking preconditions..")
-- we need to be fast and efficient here - and the properties stay fixed for the NPC
-- during this call, so we can cache them
local properties = yl_speak_up.get_npc_properties(pname)
if(not(properties)) then
properties = {}
else
properties = properties.properties
end
for k, p in pairs(prereq) do
yl_speak_up.debug_msg(player, n_id, o_id, "..checking "..
tostring(p.p_id)..": "..yl_speak_up.show_precondition(p, pname))
if(not(yl_speak_up.eval_precondition(player, n_id, p, other_options_true_or_false))) then
if(not(yl_speak_up.eval_precondition(player, n_id, p, other_options_true_or_false, properties))) then
yl_speak_up.debug_msg(player, n_id, o_id, tostring(p.p_id)..
" -> is false. Aborting.")
-- no need to look any further - once we hit a false, it'll stay false
@ -71,8 +81,120 @@ yl_speak_up.eval_all_preconditions = function(player, prereq, o_id, other_option
end
-- helper function for yl_speak_up.eval_precondition
-- (needed by "state", "property" and "evaluate")
-- Parameters:
-- p.p_operator the operator (>, <, ==, is_set, ...) from values_operator
-- p.p_var_cmp_value the value against which we compare
-- var_val the current value - that one which we want to check
yl_speak_up.eval_precondition_with_operator = function(p, var_val)
if(p.p_operator == "not") then
return not(var_val)
elseif(p.p_operator == "is_set") then
return var_val ~= nil
elseif(p.p_operator == "is_unset") then
return var_val == nil
-- for security reasons: do this manually instead of just evaluating a term
elseif(p.p_operator == "==") then
if(p.p_var_cmp_value == nil or var_val == nil) then
return false
end
-- best do these comparisons in string form to make sure both are of same type
return tostring(var_val) == tostring(p.p_var_cmp_value)
elseif(p.p_operator == "~=") then
return tostring(var_val) ~= tostring(p.p_var_cmp_value)
elseif(p.p_operator == ">=") then
if(p.p_var_cmp_value == nil or var_val == nil) then
return false
end
-- compare numeric if possible
if(tonumber(var_val) and tonumber(p.p_var_cmp_value)) then
return tonumber(var_val) >= tonumber(p.p_var_cmp_value)
-- fallback: compare as strings
else
return tostring(var_val) >= tostring(p.p_var_cmp_value)
end
elseif(p.p_operator == ">") then
if(p.p_var_cmp_value == nil or var_val == nil) then
return false
end
if(tonumber(var_val) and tonumber(p.p_var_cmp_value)) then
return tonumber(var_val) > tonumber(p.p_var_cmp_value)
else
return tostring(var_val) > tostring(p.p_var_cmp_value)
end
elseif(p.p_operator == "<=") then
if(p.p_var_cmp_value == nil or var_val == nil) then
return false
end
if(tonumber(var_val) and tonumber(p.p_var_cmp_value)) then
return tonumber(var_val) <= tonumber(p.p_var_cmp_value)
else
return tostring(var_val) <= tostring(p.p_var_cmp_value)
end
elseif(p.p_operator == "<") then
if(p.p_var_cmp_value == nil or var_val == nil) then
return false
end
if(tonumber(var_val) and tonumber(p.p_var_cmp_value)) then
return tonumber(var_val) < tonumber(p.p_var_cmp_value)
else
return tostring(var_val) < tostring(p.p_var_cmp_value)
end
elseif(p.p_operator == "more_than_x_seconds_ago") then
if(p.p_var_cmp_value == nil or var_val == nil) then
return false
end
if(not(tonumber(var_val)) or not(tonumber(p.p_var_cmp_value))) then
return true
end
return (tonumber(var_val) + tonumber(p.p_var_cmp_value)) <
math.floor(minetest.get_us_time()/1000000)
elseif(p.p_operator == "less_than_x_seconds_ago") then
if(p.p_var_cmp_value == nil or var_val == nil) then
return false
end
if(not(tonumber(var_val)) or not(tonumber(p.p_var_cmp_value))) then
return false
end
return (tonumber(var_val) + tonumber(p.p_var_cmp_value)) >
minetest.get_us_time()/1000000
-- this is currently equivalent to >= but may change in the future
-- TODO: quest steps may be strings in the future
elseif(p.p_operator == "quest_step_done") then
-- if the variable is not set at all, then the quest step definitely
-- has not been reached yet
if((p.p_var_cmp_value == nil) or (var_val == nil)) then
return false
end
-- compare numeric if possible
if(tonumber(var_val) and tonumber(p.p_var_cmp_value)) then
return tonumber(var_val) >= tonumber(p.p_var_cmp_value)
-- fallback: compare as strings
else
return tostring(var_val) >= tostring(p.p_var_cmp_value)
end
-- this is currently equivalent to < but may change in the future
-- TODO: quest steps may be strings in the future
elseif(p.p_operator == "quest_step_not_done") then
-- if the variable is not set at all, then the quest step definitely
-- has not been reached yet
if((p.p_var_cmp_value == nil) or (var_val == nil)) then
return true
end
if(tonumber(var_val) and tonumber(p.p_var_cmp_value)) then
return tonumber(var_val) < tonumber(p.p_var_cmp_value)
else
return tostring(var_val) < tostring(p.p_var_cmp_value)
end
end
-- unsupported operator
return false
end
-- checks if precondition p is true for the player and npc n_id
yl_speak_up.eval_precondition = function(player, n_id, p, other_options_true_or_false)
yl_speak_up.eval_precondition = function(player, n_id, p, other_options_true_or_false, properties)
if(not(p.p_type) or p.p_type == "") then
-- empty prerequirement: automaticly true (fallback)
return true
@ -122,109 +244,25 @@ yl_speak_up.eval_precondition = function(player, n_id, p, other_options_true_or_
-- the owner is alrady encoded in the variable name
var_val = yl_speak_up.get_quest_variable_value(pname, p.p_variable)
end
-- actually evaulate it
return yl_speak_up.eval_precondition_with_operator(p, var_val)
if(p.p_operator == "not") then
return not(var_val)
elseif(p.p_operator == "is_set") then
return var_val ~= nil
elseif(p.p_operator == "is_unset") then
return var_val == nil
-- for security reasons: do this manually instead of just evaluating a term
elseif(p.p_operator == "==") then
if(p.p_var_cmp_value == nil) then
return false
end
-- best do these comparisons in string form to make sure both are of same type
return tostring(var_val) == tostring(p.p_var_cmp_value)
elseif(p.p_operator == "~=") then
return tostring(var_val) ~= tostring(p.p_var_cmp_value)
elseif(p.p_operator == ">=") then
if(p.p_var_cmp_value == nil) then
return false
end
-- compare numeric if possible
if(tonumber(var_val) and tonumber(p.p_var_cmp_value)) then
return tonumber(var_val) >= tonumber(p.p_var_cmp_value)
-- fallback: compare as strings
elseif(p.p_type == "property") then
-- fallback in case this function is called alone, without properties
if(not(properties)) then
local pname = player:get_player_name()
properties = yl_speak_up.get_npc_properties(pname)
-- if there are no properties: return false
if(not(properties)) then
properties = {}
else
return tostring(var_val) >= tostring(p.p_var_cmp_value)
end
elseif(p.p_operator == ">") then
if(p.p_var_cmp_value == nil) then
return false
end
if(tonumber(var_val) and tonumber(p.p_var_cmp_value)) then
return tonumber(var_val) > tonumber(p.p_var_cmp_value)
else
return tostring(var_val) > tostring(p.p_var_cmp_value)
end
elseif(p.p_operator == "<=") then
if(p.p_var_cmp_value == nil) then
return false
end
if(tonumber(var_val) and tonumber(p.p_var_cmp_value)) then
return tonumber(var_val) <= tonumber(p.p_var_cmp_value)
else
return tostring(var_val) <= tostring(p.p_var_cmp_value)
end
elseif(p.p_operator == "<") then
if(p.p_var_cmp_value == nil) then
return false
end
if(tonumber(var_val) and tonumber(p.p_var_cmp_value)) then
return tonumber(var_val) < tonumber(p.p_var_cmp_value)
else
return tostring(var_val) < tostring(p.p_var_cmp_value)
end
elseif(p.p_operator == "more_than_x_seconds_ago") then
if(p.p_var_cmp_value == nil) then
return false
end
if(not(tonumber(var_val)) or not(tonumber(p.p_var_cmp_value))) then
return true
end
return (tonumber(var_val) + tonumber(p.p_var_cmp_value)) <
math.floor(minetest.get_us_time()/1000000)
elseif(p.p_operator == "less_than_x_seconds_ago") then
if(p.p_var_cmp_value == nil) then
return false
end
if(not(tonumber(var_val)) or not(tonumber(p.p_var_cmp_value))) then
return false
end
return (tonumber(var_val) + tonumber(p.p_var_cmp_value)) >
minetest.get_us_time()/1000000
-- this is currently equivalent to >= but may change in the future
-- TODO: quest steps may be strings in the future
elseif(p.p_operator == "quest_step_done") then
-- if the variable is not set at all, then the quest step definitely
-- has not been reached yet
if((p.p_var_cmp_value == nil) or (var_val == nil)) then
return false
end
-- compare numeric if possible
if(tonumber(var_val) and tonumber(p.p_var_cmp_value)) then
return tonumber(var_val) >= tonumber(p.p_var_cmp_value)
-- fallback: compare as strings
else
return tostring(var_val) >= tostring(p.p_var_cmp_value)
end
-- this is currently equivalent to < but may change in the future
-- TODO: quest steps may be strings in the future
elseif(p.p_operator == "quest_step_not_done") then
-- if the variable is not set at all, then the quest step definitely
-- has not been reached yet
if((p.p_var_cmp_value == nil) or (var_val == nil)) then
return true
end
if(tonumber(var_val) and tonumber(p.p_var_cmp_value)) then
return tonumber(var_val) < tonumber(p.p_var_cmp_value)
else
return tostring(var_val) < tostring(p.p_var_cmp_value)
properties = properties.properties
end
end
-- unsupported operator
return false
return yl_speak_up.eval_precondition_with_operator(p, properties[p.p_value])
elseif(p.p_type == "evaluate") then
return true -- TODO
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

@ -223,6 +223,22 @@ yl_speak_up.save_element_p_or_a_or_e = function(
v[ id_prefix.."variable" ] = yl_speak_up.add_pname_to_var(data.variable_name, pname)
end
-- "the value of a property of the NPC (for generic NPC)"
elseif(what_type == "property" and id_prefix ~= "a_") then
v[ id_prefix.."value" ] = (data.property or "")
v[ id_prefix.."operator" ] = values_operator[ data.operator ]
v[ id_prefix.."var_cmp_value" ] = (data.var_cmp_value or "")
-- "something that has to be calculated or evaluated (=call a function)"
elseif(what_type == "evaluate" and id_prefix ~= "a_") then
v[ id_prefix.."value" ] = (data.evaluate or "")
v[ id_prefix.."operator" ] = values_operator[ data.operator ]
v[ id_prefix.."var_cmp_value" ] = (data.var_cmp_value or "")
-- transfer the parameters
for i = 1, 9 do
v[ id_prefix.."param"..str(i) ] = (data["param"..str(i)] or "")
end
-- "a block somewhere", -- 3
elseif(what_type == "block" and id_prefix ~= "a_") then
v[ id_prefix.."value" ] = values_block[ data.block ]
@ -871,8 +887,10 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields,
data.match_stack_size = fields.select_match_stack_size:split(" ")[1]
-- comparison value for a variable (same for both preconditions and effects)
-- (also used for checking return values of functions and property values)
elseif(fields.var_cmp_value
and data and data.what and what_type == "state" and id_prefix ~= "a_") then
and data and data.what and id_prefix ~= "a_"
and (what_type == "state" or what_type == "property" or what_type == "evaluate")) then
data.var_cmp_value = fields.var_cmp_value
was_changed = true
@ -1010,6 +1028,20 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields,
local nr = table.indexof(check_operator, fields.select_operator)
yl_speak_up.speak_to[pname][ tmp_data_cache ].operator = nr
end
-- "the value of a property of the NPC (for generic NPC)"
if(fields.property and fields.property ~= "") then
yl_speak_up.speak_to[pname][ tmp_data_cache ].property = fields.property
end
-- "something that has to be calculated or evaluated (=call a function)"
if(fields.evaluate and fields.evaluate ~= "") then
yl_speak_up.speak_to[pname][ tmp_data_cache ].evaluate = fields.evaluate
end
for i = 1,9 do
local s = "param"..tostring(i)
if(fields[s] and fields[s] ~= "") then
yl_speak_up.speak_to[pname][ tmp_data_cache ][s] = fields[s]
end
end
-- another dialog option is true or false
-- Note: "-select-" can be choosen here as well
if(fields.select_other_o_id and fields.select_other_o_id ~= "") then
@ -1354,6 +1386,18 @@ yl_speak_up.get_fs_edit_option_related = function(player, table_click_result,
text_variable, text_select_value, text_select_operator,
values_operator, check_operator, get_sorted_player_var_list_function )
-- "the value of a property of the NPC (for generic NPC)"
elseif(data.what and what_type == "property" and id_prefix == "p_") then
return yl_speak_up.get_fs_edit_option_p_and_e_property(
pname, dialog, formspec, data, id_prefix, save_button, e,
text_select_operator, values_operator, check_operator)
-- "something that has to be calculated or evaluated (=call a function)"
elseif(data.what and what_type == "evaluate" and id_prefix == "p_") then
return yl_speak_up.get_fs_edit_option_p_and_e_evaluate(
pname, dialog, formspec, data, id_prefix, save_button, e,
text_select_operator, values_operator, check_operator)
-- "a block somewhere", -- 3
-- (block is the third offered option in both preconditions and effects list)
elseif(data.what and what_type == "block" and id_prefix ~= "a_") then
@ -1533,6 +1577,68 @@ yl_speak_up.get_fs_edit_option_p_and_e_state = function(
end
-- "the value of a property of the NPC (for generic NPC)"
yl_speak_up.get_fs_edit_option_p_and_e_property = function(
pname, dialog, formspec, data, id_prefix, save_button, e,
text_select_operator, values_operator, check_operator)
if(e) then
data.operator = math.max(1,table.indexof(values_operator, e[ id_prefix.."operator" ]))
data.var_cmp_value = e[ id_prefix.."var_cmp_value" ]
data.property = e[ id_prefix.."value"]
end
if(not(data.property) or data.property == "") then
-- not enough selected yet for saving
save_button = ""
elseif(not(data.operator) or data.operator == 1) then
data.operator = 1
save_button = ""
end
local field_for_value = "field[11.7,4.8;7.5,0.6;var_cmp_value;;"..
minetest.formspec_escape(data.var_cmp_value or "- enter value -").."]"
-- do not show value input field for unary operators
-- (unary operators are diffrent for prerequirements and effects)
if(not(data.operator)
or (id_prefix == "p_" and (data.operator == 1 or (data.operator>=8 and data.operator<11)))
-- "unset", "set_to_current_time"
or (id_prefix == "r_" and (data.operator == 3 or data.operator == 4))) then
field_for_value = "label[11.7,5.1;- not used for this operator -]"
end
local operator_list = {}
for i, v in ipairs(check_operator) do
v2 = values_operator[i]
if( v2 ~= "quest_step_done" and v2 ~= "quest_step_not_done"
and v2 ~= "true_for_param" and v2 ~= "false_for_param") then
table.insert(operator_list, v)
end
end
-- the list of available variables needs to be extended with the ones
return formspec..
"label[0.2,3.3;The NPC shall have the following property:]"..
"label[0.2,4.3;Name of property:]"..
"field[1.0,4.8;5.0,0.6;property;;"..
minetest.formspec_escape(data.property or "- enter name -").."]"..
"label[7.0,4.3;"..text_select_operator.."]"..
"dropdown[7.0,4.8;4.5,0.6;select_operator;"..
table.concat(operator_list, ",")..";"..
tostring(data.operator)..";]"..
"label[11.7,4.3;Compare property with this value:]"..
field_for_value..
"hypertext[1.2,7.0;16.0,2.5;some_text;<normal>"..
"<b>Note:</b> Properties are useful for NPC that have a generic "..
"behaviour and may vary their behaviour slightly.\n"..
"</normal>]"..
save_button
end
-- "something that has to be calculated or evaluated (=call a function)"
yl_speak_up.get_fs_edit_option_p_and_e_evaluate = function(
pname, dialog, formspec, data, id_prefix, save_button, e,
text_select_operator, values_operator, check_operator)
-- TODO: actually implement
return formspec
end
-- helper function for:
-- yl_speak_up.get_fs_edit_option_p_and_e_block

View File

@ -12,6 +12,19 @@
-- p_operator selected from values_operator
-- p_var_cmp_value can be set freely by the player
--
-- the value of a property of the NPC (for generic NPC) ("property"):
-- p_value name of the property that shall be checked
-- p_operator operator for cheking the property against p_expected_val
-- p_var_cmp_value the expected value of the property
--
-- something that has to be calculated or evaluated (=call a function) ("evaluate"):
-- p_value the name of the function that is to be called
-- p_param1 the first paramter (optional; depends on function)
-- ..
-- p_param9 the 9th parameter (optional; depends on function)
-- p_operator operator for checking the result
-- p_var_cmp_value compare the result of the function with this value
--
-- a block in the world ("block"):
-- p_pos a position in the world; determined by asking the player
-- to punch the block
@ -62,6 +75,8 @@
local check_what = {
"- please select -",
"an internal state (i.e. of a quest)", -- 2
"the value of a property of the NPC (for generic NPC)",
"something that has to be calculated or evaluated (=call a function)",
"a block somewhere", -- 3
"a trade", -- 4
"the inventory of the player", -- 5
@ -76,7 +91,7 @@ local check_what = {
}
-- how to store these as p_type in the precondition:
local values_what = {"", "state", "block", "trade",
local values_what = {"", "state", "property", "evaluate", "block", "trade",
"player_inv", "npc_inv", "block_inv",
"player_offered_item",
-- "function" requires npc_master priv:
@ -224,6 +239,22 @@ yl_speak_up.show_precondition = function(p, pname)
end
return var_name.." "..tostring(p.p_operator).." "..
tostring(p.p_var_cmp_value)
elseif(p.p_type == "property") then
local i = math.max(1,table.indexof(values_operator, p.p_operator))
return tostring(p.p_value)..
" "..tostring(check_operator[i])..
" "..tostring(p.p_var_cmp_value)
elseif(p.p_type == "evaluate") then
local str = ""
for i = 1, 9 do
str = str..tostring(p["p_param" + str(i)])
if(i < 9) then
str = str..","
end
end
return "FUNCTION["..tostring(p.p_value).."]"..
"("..str..") "..tostring(check_operator[p.p_operator])..
" "..tostring(p.p_var_cmp_value)
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