From 312b9a49dfd2701c4b71c8f0a00515d4e9e649c4 Mon Sep 17 00:00:00 2001 From: Sokomine Date: Thu, 10 Jun 2021 18:06:21 +0200 Subject: [PATCH] renamed eval_precondition_function to eval_and_execute_function, moved to fs_edit_general.lua and used for results as well --- fs_edit_general.lua | 70 +++++++++++++++++++++++++ fs_edit_preconditions.lua | 2 +- functions.lua | 106 +------------------------------------- 3 files changed, 72 insertions(+), 106 deletions(-) diff --git a/fs_edit_general.lua b/fs_edit_general.lua index 6299149..9b6b75d 100644 --- a/fs_edit_general.lua +++ b/fs_edit_general.lua @@ -1,4 +1,74 @@ +-- evaluate those preconditions of type "function" (set by the staff) +-- and execute those effects/results of type "function" (set by the staff) +-- WARNING: This is extremly powerful! +-- The code is taken out of the function +-- calculate_displayable_options(..) +-- (written by AliasAlreadyTaken). +-- It requires the npc_master priv to add or edit this prereq. +-- The function is called by +-- * yl_speak_up.eval_precondition and +-- * yl_speak_up.eval_effect. +yl_speak_up.eval_and_execute_function = function(player, x_v, id_prefix) + local pname = player:get_player_name() + + --minetest.chat_send_all("this is in a single prereq or effect: "..dump(x_v)) + local x_id = x_v[ id_prefix.. "id" ] + if x_v[ id_prefix.."type" ] ~= "function" then + return true + end + + local code = x_v[ id_prefix.."value" ] + if code:byte(1) == 27 then + local obj = yl_speak_up.speak_to[pname].obj + local n_id = yl_speak_up.speak_to[pname].n_id + local npc = get_number_from_id(n_id) + if obj:get_luaentity() and tonumber(npc) then + minetest.log("error","[MOD] yl_speak_up: NPC with ID n_"..npc.." at position "..minetest.pos_to_string(obj:get_pos(),0).." could not compile the content of "..x_id.." :"..dump(code) .. " because of illegal bytecode for player "..pname) + else + minetest.log("error","[MOD] yl_speak_up: NPC with ID unknown could not compile the content of "..x_id.." :"..dump(code) .. " for player unknown because of illegal bytecode") + end + end + + local param = "playername" + if( id_prefix == "r_") then + param = "player" + end + local f, msg = loadstring("return function("..param..") " .. code .. " end") + + if not f then + local obj = yl_speak_up.speak_to[pname].obj + local n_id = yl_speak_up.speak_to[pname].n_id + local npc = get_number_from_id(n_id) + if obj:get_luaentity() and tonumber(npc) then + minetest.log("error","[MOD] yl_speak_up: NPC with ID n_"..npc.." at position "..minetest.pos_to_string(obj:get_pos(),0).." could not compile the content of "..x_id.." :"..dump(code) .. " for player "..pname) + else + minetest.log("error","[MOD] yl_speak_up: NPC with ID unknown could not compile the content of "..x_id.." :"..dump(code) .. " for player unknown") + end + else + local func = f() + + local ok, ret = pcall(func,pname) + + if not ok then + local obj = yl_speak_up.speak_to[pname].obj + local n_id = yl_speak_up.speak_to[pname].n_id + local npc = get_number_from_id(n_id) + if obj:get_luaentity() and tonumber(npc) then + minetest.log("error","[MOD] yl_speak_up: NPC with ID n_"..npc.." at position "..minetest.pos_to_string(obj:get_pos(),0).." could not execute the content of "..x_id.." :"..dump(code) .. " for player "..pname) + else + minetest.log("error","[MOD] yl_speak_up: NPC with ID unknown could not execute the content of "..x_id.." :"..dump(code) .. " for player unknown") + end + end + + if type(ret) == "boolean" then + return ret + end + end + -- fallback + return false +end + -- These two functions -- * yl_speak_up.input_fs_edit_option_related and diff --git a/fs_edit_preconditions.lua b/fs_edit_preconditions.lua index 860fa9c..d2004b4 100644 --- a/fs_edit_preconditions.lua +++ b/fs_edit_preconditions.lua @@ -237,7 +237,7 @@ yl_speak_up.eval_precondition = function(player, n_id, p) elseif(p.p_type == "function") then -- a precondition set by using the staff; -- extremly powerful (executes any lua code) - return yl_speak_up.eval_precondition_function(player, p) + return yl_speak_up.eval_and_execute_function(player, p, "p_") elseif(p.p_type == "state") then local var_val = false if(not(p.p_variable) or p.p_variable == "") then diff --git a/functions.lua b/functions.lua index 90ef360..5d4f7f8 100644 --- a/functions.lua +++ b/functions.lua @@ -349,70 +349,6 @@ local function delete_option(n_id, d_id, o_id) end --- evaluate those preconditions of type "function" (set by the staff) --- WARNING: This is extremly powerful! --- The code is taken out of the function --- calculate_displayable_options(..) --- (written by AliasAlreadyTaken). --- It requires the npc_master priv to add or edit this prereq. --- The function is called by yl_speak_up.eval_precondition -yl_speak_up.eval_precondition_function = function(player, p_v) - local pname = player:get_player_name() - - --minetest.chat_send_all("this is in a single prereq: "..dump(p_v)) - local p_id = p_v.p_id - if p_v.p_type ~= "function" then - return true - end - - local code = p_v.p_value - if code:byte(1) == 27 then - local obj = yl_speak_up.speak_to[pname].obj - local n_id = yl_speak_up.speak_to[pname].n_id - local npc = get_number_from_id(n_id) - if obj:get_luaentity() and tonumber(npc) then - minetest.log("error","[MOD] yl_speak_up: NPC with ID n_"..npc.." at position "..minetest.pos_to_string(obj:get_pos(),0).." could not compile the content of "..p_id.." :"..dump(code) .. " because of illegal bytecode for player "..pname) - else - minetest.log("error","[MOD] yl_speak_up: NPC with ID unknown could not compile the content of "..p_id.." :"..dump(code) .. " for player unknown because of illegal bytecode") - end - end - - local f, msg = loadstring("return function(playername) " .. code .. " end") - - if not f then - local obj = yl_speak_up.speak_to[pname].obj - local n_id = yl_speak_up.speak_to[pname].n_id - local npc = get_number_from_id(n_id) - if obj:get_luaentity() and tonumber(npc) then - minetest.log("error","[MOD] yl_speak_up: NPC with ID n_"..npc.." at position "..minetest.pos_to_string(obj:get_pos(),0).." could not compile the content of "..p_id.." :"..dump(code) .. " for player "..pname) - else - minetest.log("error","[MOD] yl_speak_up: NPC with ID unknown could not compile the content of "..p_id.." :"..dump(code) .. " for player unknown") - end - else - local func = f() - - local ok, ret = pcall(func,pname) - - if not ok then - local obj = yl_speak_up.speak_to[pname].obj - local n_id = yl_speak_up.speak_to[pname].n_id - local npc = get_number_from_id(n_id) - if obj:get_luaentity() and tonumber(npc) then - minetest.log("error","[MOD] yl_speak_up: NPC with ID n_"..npc.." at position "..minetest.pos_to_string(obj:get_pos(),0).." could not execute the content of "..p_id.." :"..dump(code) .. " for player "..pname) - else - minetest.log("error","[MOD] yl_speak_up: NPC with ID unknown could not execute the content of "..p_id.." :"..dump(code) .. " for player unknown") - end - end - - if type(ret) == "boolean" then - return ret - end - end - -- fallback - return false -end - - local function calculate_displayable_options(pname, d_options) -- Let's go through all the options and see if we need to display them to the user @@ -2653,47 +2589,7 @@ yl_speak_up.input_talk = function(player, formname, fields) end if not(edit_mode) and v.r_type == "function" then - local code = v.r_value - if code:byte(1) == 27 then - local obj = yl_speak_up.speak_to[pname].obj - local n_id = yl_speak_up.speak_to[pname].n_id - local npc = get_number_from_id(n_id) - if obj:get_luaentity() and tonumber(npc) then - minetest.log("error","[MOD] yl_speak_up: NPC with ID n_"..npc.." at position "..minetest.pos_to_string(obj:get_pos(),0).." could not compile the content of "..v.r_id.." :"..dump(v.r_value) .. " because of illegal bytecode for player "..pname) - else - minetest.log("error","[MOD] yl_speak_up: NPC with ID unknown could not compile the content of "..v.r_id.." :"..dump(v.r_value) .. " for player unknown because of illegal bytecode") - end - end - - local f, msg = loadstring("return function(player) " .. code .. " end") - - if not f then - local obj = yl_speak_up.speak_to[pname].obj - local n_id = yl_speak_up.speak_to[pname].n_id - local npc = get_number_from_id(n_id) - if obj:get_luaentity() and tonumber(npc) then - minetest.log("error","[MOD] yl_speak_up: NPC with ID n_"..npc.." at position "..minetest.pos_to_string(obj:get_pos(),0).." could not compile the content of "..v.r_id.." :"..dump(v.r_value) .. " for player "..pname) - else - minetest.log("error","[MOD] yl_speak_up: NPC with ID unknown could not compile the content of "..v.r_id.." :"..dump(v.r_value) .. " for player unknown") - end - else - - - local func = f() - - local ok, ret = pcall(func,pname) - - if not ok then - local obj = yl_speak_up.speak_to[pname].obj - local n_id = yl_speak_up.speak_to[pname].n_id - local npc = get_number_from_id(n_id) - if obj:get_luaentity() and tonumber(npc) then - minetest.log("error","[MOD] yl_speak_up: NPC with ID n_"..npc.." at position "..minetest.pos_to_string(obj:get_pos(),0).." could not execute the content of "..v.r_id.." :"..dump(v.r_value) .. " for player "..pname) - else - minetest.log("error","[MOD] yl_speak_up: NPC with ID unknown could not execute the content of "..v.r_id.." :"..dump(v.r_value) .. " for player unknown") - end - end - end + yl_speak_up.eval_and_execute_function(player, v, "r_") end if v.r_type == "dialog" then