variable handling related to current time

This commit is contained in:
Sokomine 2021-06-20 17:28:21 +02:00
parent a7288bb2b9
commit 827020d167
3 changed files with 45 additions and 7 deletions

View File

@ -69,10 +69,11 @@ local check_operator = {
"- please select -", -- 1
"set to value:", -- 2
"is no longer needed (unset)", -- 3
"set to current time", -- 4
}
-- how to store these as r_value (the actual variable is stored in r_variable, and the value in r_new_value):
local values_operator = {"", "set_to", "unset"}
local values_operator = {"", "set_to", "unset", "set_to_current_time"}
-- get the list of variables the player has *write* access to
@ -132,6 +133,8 @@ yl_speak_up.show_effect = function(r)
tostring(r.r_var_cmp_value).."\""
elseif(r.r_operator == "unset") then
return "discard VARIABLE[ "..tostring(r.r_variable).." ] (unset)"
elseif(r.r_operator == "set_to_current_time") then
return "set VARIABLE[ "..tostring(r.r_variable).." ] to the current time"
else
return "ERROR: Wrong operator \""..tostring(r.r_operator).."\" for "..
"VARIABLE[ "..tostring(r.r_variable).." ]"
@ -400,6 +403,10 @@ yl_speak_up.execute_effect = function(player, n_id, o_id, r)
new_value = r.r_var_cmp_value
elseif(r.r_operator and r.r_operator == "unset") then
new_value = nil
elseif(r.r_operator and r.r_operator == "set_to_current_time") then
-- we store the time in seconds - because microseconds would just
-- confuse the users and be too fine grained anyway
new_value = minetest.get_us_time()/1000000
else
yl_speak_up.debug_msg(player, n_id, o_id, tostring(r.r_id).." "..
"state: Unsupported type: "..tostring(r.r_value)..".")

View File

@ -945,8 +945,9 @@ yl_speak_up.get_fs_edit_option_related = function(player, table_click_result,
-- 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))
or (id_prefix == "r_" and (data.operator == 3))) then
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.2,5.1;- not used for this operator -]"
end
-- the list of available variables needs to be extended with the ones
@ -964,7 +965,16 @@ yl_speak_up.get_fs_edit_option_related = function(player, table_click_result,
table.concat(check_operator, ",")..";"..
tostring(data.operator)..";]"..
"label[11.2,4.3;"..text_select_value.."]"..
field_for_value
field_for_value..
"hypertext[1.2,6.0;16.0,2.5;some_text;<normal>"..
"<b>Note:</b> Each variable is player-specific and will be set and "..
"checked for the player that currently talks to your NPC.\n"..
"<b>Note:</b> You can set a variable to the current time in an effect. "..
"After that, use a precondition to check if that variable was set \"more "..
"than x seconds ago\" or \"less than x seconds ago\". This can be "..
"useful for prevending your NPC from handing out the same quest item again "..
"too quickly (players are inventive and may use your quest item for their "..
"own needs).\n</normal>]"
-- "a block somewhere", -- 3
-- (block is the third offered option in both preconditions and effects list)

View File

@ -92,11 +92,14 @@ local check_operator = {
"< (is smaller)", -- 7
"not (logically invert)", -- 8
"is_set (has a value)", -- 9
"is_unset (has no value)" -- 10
"is_unset (has no value)", -- 10
"more than x seconds ago", -- 11
"less than x seconds ago", -- 12
}
-- how to store these as p_value (the actual variable is stored in p_variable, and the value in p_cmp_value):
local values_operator = {"", "==", "~=", ">=", ">", "<=", "<", "not", "is_set", "is_unset"}
local values_operator = {"", "==", "~=", ">=", ">", "<=", "<", "not", "is_set", "is_unset",
"more_than_x_seconds_ago","less_than_x_seconds_ago"}
-- some internal ones...
local check_variable = {
@ -146,6 +149,12 @@ yl_speak_up.show_precondition = function(p)
return "VALUE_OF[ "..tostring(p.p_variable).." ] ~= nil (is_set)"
elseif(p.p_operator == "is_unset") then
return "VALUE_OF[ "..tostring(p.p_variable).." ] == nil (is_unset)"
elseif(p.p_operator == "more_than_x_seconds_ago") then
return "VALUE_OF[ "..tostring(p.p_variable).." ] was set to current time "..
"*more* than "..tostring(p.p_var_cmp_value).." seconds ago"
elseif(p.p_operator == "less_than_x_seconds_ago") then
return "VALUE_OF[ "..tostring(p.p_variable).." ] was set to current time "..
"*less* than "..tostring(p.p_var_cmp_value).." seconds ago"
end
if(p.p_var_cmp_value == "") then
return "VALUE_OF[ "..tostring(p.p_variable).." ] "..tostring(p.p_operator).." \"\""
@ -276,7 +285,7 @@ yl_speak_up.eval_precondition = function(player, n_id, p)
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
else
return tostring(var_val) >= tostring(p.p_var_cmp_value)
end
elseif(p.p_operator == ">") then
@ -297,6 +306,18 @@ yl_speak_up.eval_precondition = function(player, n_id, p)
else
return tostring(var_val) < tostring(p.p_var_cmp_value)
end
elseif(p.p_operator == "more_than_x_seconds_ago") then
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)) <
minetest.get_us_time()/1000000
elseif(p.p_operator == "less_than_x_seconds_ago") then
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
end
-- unsupported operator
return false