allow to combine values of variables via operator

This commit is contained in:
Sokomine 2025-06-09 02:52:39 +02:00
parent 7896d53dd6
commit 94906c221b

View File

@ -568,6 +568,76 @@ yl_speak_up.custom_functions_r_[ "set_variable_to_random_value_from_list" ] = {
}
yl_speak_up.custom_functions_r_[ "var0_eq_var1_op_var2" ] = {
description = "Set a <variable0> to the value of <variable1> <operator> <variable2>.",
param1_text = "Name of the variable0 to set:",
param1_desc = "Which variable do you want to set to the new value?",
param2_text = "Name of variable1:",
param2_desc = "Which variable holds the value of the first operand?",
param3_text = "Operand (+ - * / % ^ and or):",
param3_desc = "Choose operand: Add, subtract, multiply, divide, modulo, exponent, and, or.",
param4_text = "Name of variable2:",
param4_desc = "Which variable holds the value of the second operand?",
code = function(player, n_id, r)
if(not(r) or not(r.r_param1) or r.r_param1 == ""
or not(r.r_param2) or r.r_param2 == ""
or not(r.r_param3) or r.r_param3 == ""
or not(r.r_param4) or r.r_param4 == "") then
return false
end
-- the owner is already encoded in the variable name
local pname = player:get_player_name()
local owner = yl_speak_up.npc_owner[ n_id ]
local prefix = "$ "..tostring(owner).." "
local v_1 = yl_speak_up.get_quest_variable_value(pname, prefix..r.r_param2)
local v_2 = yl_speak_up.get_quest_variable_value(pname, prefix..r.r_param3)
local new_value = nil
local op = r.r_param3
if(op == 'and') then
new_value = v_1 and v_2
elseif(op == 'or') then
new_value = v_1 or v_2
else
v_1 = tonumber(v_1)
v_2 = tonumber(v_2)
if(v_1 == nil or v_2 == nil) then
new_value = nil
elseif(op == '+') then
new_value = v_1 + v_2
elseif(op == '-') then
new_value = v_1 - v_2
elseif(op == '*') then
new_value = v_1 * v_2
elseif(op == '/') then
if(v_2 ~= 0) then
new_value = v_1 / v_2
else
new_value = nil
end
elseif(op == '%') then
if(v_2 ~= 0) then
new_value = v_1 % v_2
else
new_value = nil
end
elseif(op == '^') then
new_value = v_1 ^ v_2
end
end
if(new_value == nil) then
return false
end
local ret = yl_speak_up.set_quest_variable_value(pname, prefix..r.r_param1, new_value)
local o_id = yl_speak_up.speak_to[pname].o_id or "?"
yl_speak_up.debug_msg(player, n_id, o_id, tostring(r.r_id).." "..
"state: Success: "..tostring(ret).." for setting "..tostring(r.r_param1).." to "..
tostring(new_value)..".")
return ret
end
}
yl_speak_up.custom_functions_r_[ "play_sound" ] = {
description = "Plays a sound.",
param1_text = "Name of the sound(file):",