diff --git a/eval_and_execute_function.lua b/eval_and_execute_function.lua new file mode 100644 index 0000000..47ac7d9 --- /dev/null +++ b/eval_and_execute_function.lua @@ -0,0 +1,58 @@ + +-- 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. +-- The npc also needs a priv to execute this. +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 + yl_speak_up.log_error_with_position(pname, n_id, + "could not compile the content of "..tostring(x_id).." :"..dump(code).. + " because of illegal bytecode for player "..tostring(pname)) + 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 + yl_speak_up.log_error_with_position(pname, n_id, + "could not compile the content of "..tostring(x_id).." :"..dump(code).. + " for player "..tostring(pname)) + else + local func = f() + + local ok, ret = pcall(func,pname) + + if not ok then + yl_speak_up.log_error_with_position(pname, n_id, + "could not execute the content of "..tostring(x_id).." :"..dump(code).. + " for player "..tostring(pname)) + end + + if type(ret) == "boolean" then + return ret + end + end + -- fallback + return false +end + + diff --git a/fs_edit_general.lua b/fs_edit_general.lua index 201ce37..60026c3 100644 --- a/fs_edit_general.lua +++ b/fs_edit_general.lua @@ -39,63 +39,6 @@ yl_speak_up.get_node_inv_lists = function(pos, search_for_list_name) end --- 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. --- The npc also needs a priv to execute this. -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 - yl_speak_up.log_error_with_position(pname, n_id, - "could not compile the content of "..tostring(x_id).." :"..dump(code).. - " because of illegal bytecode for player "..tostring(pname)) - 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 - yl_speak_up.log_error_with_position(pname, n_id, - "could not compile the content of "..tostring(x_id).." :"..dump(code).. - " for player "..tostring(pname)) - else - local func = f() - - local ok, ret = pcall(func,pname) - - if not ok then - yl_speak_up.log_error_with_position(pname, n_id, - "could not execute the content of "..tostring(x_id).." :"..dump(code).. - " for player "..tostring(pname)) - end - - if type(ret) == "boolean" then - return ret - end - end - -- fallback - return false -end - - -- helper function for yl_speak_up.input_fs_edit_option_related yl_speak_up.delete_element_p_or_a_or_e = function( player, pname, n_id, d_id, o_id, x_id, id_prefix, diff --git a/init.lua b/init.lua index 3d663f0..3ebe6fd 100644 --- a/init.lua +++ b/init.lua @@ -68,6 +68,8 @@ dofile(modpath .. "formspec_helpers.lua") dofile(modpath .. "fs_alternate_text.lua") -- implementation of chat command npc_talk_debug: dofile(modpath .. "command_npc_talk_debug.lua") +-- execute lua code directly (preconditions and effects) - requires priv +dofile(modpath .. "eval_and_execute_function.lua") -- common functions for editing preconditions and effects dofile(modpath .. "fs_edit_general.lua") -- edit preconditions (can be reached through edit options dialog)