added custom precondition and effect

This commit is contained in:
Sokomine 2021-06-29 00:15:36 +02:00
parent 3a66d6b4ce
commit cef75205f5
3 changed files with 85 additions and 23 deletions

View File

@ -52,12 +52,15 @@ local check_what = {
"take item from player and destroy it (requires npc_master priv)", -- 8
"move the player to a given position (requires npc_master priv)", -- 9
"execute Lua code (requires npc_master priv)", -- 10
"Call custom functions that are supposed to be overridden by the server.", -- 11
}
-- how to store these as r_type in the precondition:
local values_what = {"", "state", "block", "craft", "on_failure", "chat_all",
-- the following require the npc_master priv:
"give_item", "take_item", "move", "function"}
"give_item", "take_item", "move", "function",
-- custom function (does not require npc_master priv)
"custom"}
-- unlike in the preconditions, the "I cannot punch it" option is
-- not offered here - because the player (and later the NPC) needs
@ -200,6 +203,8 @@ yl_speak_up.show_effect = function(r)
return "If the *previous* effect failed, go to dialog \""..tostring(r.r_value).. "\"."
elseif(r.r_type == "chat_all") then
return "Send chat message: \""..tostring(r.r_value).."\""
elseif(r.r_type == "custom") then
return "Call custom function with param: \""..tostring(r.r_value).."\"."
end
-- fallback
return tostring(r.r_value)
@ -679,12 +684,26 @@ yl_speak_up.execute_effect = function(player, n_id, o_id, r)
minetest.chat_send_all(text)
-- sending a chat message always counts as successful
return true
-- "Call custom functions that are supposed to be overridden by the server.", -- 11
elseif(r.r_type == "custom") then
-- execute the custom function
return yl_speak_up.effect_custom(player, r.r_value)
end
-- fallback: unkown type
return false
end
-- a custom function that the server can override
yl_speak_up.effect_custom = function(player, param)
minetest.chat_send_player(player:get_player_name(),
"Executing custom effect with parameter \""..tostring(param).."\"..")
-- return true if your effect executed successfuly, and false if not
-- (only relevant for following on_failure effects)
return true
end
-- these are only wrapper functions for those in fs_edit_general.lua
yl_speak_up.input_fs_edit_effects = function(player, formname, fields)

View File

@ -299,7 +299,10 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields,
elseif(fields.custom_param
and fields.custom_param ~= "- Insert a text that is passed on to your function here -"
and fields.custom_param ~= ""
and data and data.what and data.what == 7 and id_prefix == "a_") then
and data and data.what
and ((id_prefix == "a_" and data.what == 7)
or (id_prefix == "p_" and data.what == 8)
or (id_prefix == "r_" and data.what == 11))) then
data.custom_param = fields.custom_param
was_changed = true
@ -771,11 +774,17 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields,
local sorted_dialog_list = yl_speak_up.sort_keys(dialog.n_dialogs)
v[ "a_on_failure" ] = sorted_dialog_list[ data.action_failure_dialog ]
-- "Call custom functions that are supposed to be overridden by the server.", -- 7
elseif(data.what and id_prefix == "a_" and data.what == 7) then
v[ "a_value" ] = data.custom_param
local sorted_dialog_list = yl_speak_up.sort_keys(dialog.n_dialogs)
v[ "a_on_failure" ] = sorted_dialog_list[ data.action_failure_dialog ]
-- "Call custom functions that are supposed to be overridden by the server.", --
-- precondition: 8; action: 7; effect: 11
elseif(data.what
and ((id_prefix == "a_" and data.what == 7)
or (id_prefix == "p_" and data.what == 8)
or (id_prefix == "r_" and data.what == 11))) then
v[ id_prefix.."value" ] = data.custom_param
if(id_prefix == "a_") then
local sorted_dialog_list = yl_speak_up.sort_keys(dialog.n_dialogs)
v[ "a_on_failure" ] = sorted_dialog_list[ data.action_failure_dialog ]
end
end
-- only save if something was actually selected
@ -1082,11 +1091,16 @@ yl_speak_up.get_fs_edit_option_related = function(player, table_click_result,
data.action_failure_dialog = math.max(1,
table.indexof(sorted_dialog_list, e[ "a_on_failure" ]))
-- custom function (action)
elseif(data.what and id_prefix == "a_" and data.what == 7) then
data.custom_param = e[ "a_value" ]
local sorted_dialog_list = yl_speak_up.sort_keys(dialog.n_dialogs)
data.action_failure_dialog = math.max(1,
elseif(data.what
and ((id_prefix == "a_" and data.what == 7)
or (id_prefix == "p_" and data.what == 8)
or (id_prefix == "r_" and data.what == 11))) then
data.custom_param = e[ id_prefix.."value" ]
if(id_prefix == "a_") then
local sorted_dialog_list = yl_speak_up.sort_keys(dialog.n_dialogs)
data.action_failure_dialog = math.max(1,
table.indexof(sorted_dialog_list, e[ "a_on_failure" ]))
end
-- give_item/take_item
elseif(data.what and id_prefix == "r_" and data.what >= 7 and data.what <= 8) then
@ -1570,8 +1584,11 @@ yl_speak_up.get_fs_edit_option_related = function(player, table_click_result,
"The player shall enter the correct answer.")
-- "Call custom functions that are supposed to be overridden by the server.", -- 7
-- (only for actions)
elseif(data.what and id_prefix == "a_" and data.what == 7) then
-- precondition: 8; action: 7; effect: 11
elseif(data.what
and ((id_prefix == "a_" and data.what == 7)
or (id_prefix == "p_" and data.what == 8)
or (id_prefix == "r_" and data.what == 11))) then
if(not(data)) then
data = {}
end
@ -1586,15 +1603,22 @@ yl_speak_up.get_fs_edit_option_related = function(player, table_click_result,
"tooltip[custom_param;"..
"The custom parameter may help whoever implements the\n"..
"custom function to more easily see what it belongs to.\n"..
"Dialog and option ID are also passed as parameters.]"..
"tooltip[select_on_action_failure;"..
"If the player gives the wrong answer, you can show him\n"..
"a diffrent target dialog (i.e. with text \"No, that answer\n"..
"was wrong, but please try again!\"). In such a case the\n"..
"effects/results of the current dialog option are *not*\n"..
"executed.]"..
yl_speak_up.set_on_action_failure_dialog(pname, data,
"The player shall click on the right button.")
"Dialog and option ID are also passed as parameters.]"
if(id_prefix == "a_") then
formspec = formspec..
"tooltip[select_on_action_failure;"..
"If the player gives the wrong answer, you can show him\n"..
"a diffrent target dialog (i.e. with text \"No, that answer\n"..
"was wrong, but please try again!\"). In such a case the\n"..
"effects/results of the current dialog option are *not*\n"..
"executed.]"..
yl_speak_up.set_on_action_failure_dialog(pname, data,
"The player shall click on the right button.")
else
formspec = formspec..
"label[0.3,5.0;Note: Your custom function has to return either true "..
"or false.]"
end
end
return formspec..save_button
end

View File

@ -40,10 +40,15 @@ local check_what = {
"the inventory of the player", -- 5
"the inventory of the NPC", -- 6
"execute Lua code (requires npc_master priv)", -- 7
"Call custom functions that are supposed to be overridden by the server.", -- 8
}
-- how to store these as p_type in the precondition:
local values_what = {"", "state", "block", "trade", "player_inv", "npc_inv", "function"}
local values_what = {"", "state", "block", "trade", "player_inv", "npc_inv",
-- requires npc_master priv
"function",
-- custom function (does not require npc_master priv)
"custom"}
-- options for "a trade"
local check_trade = {
@ -195,6 +200,8 @@ yl_speak_up.show_precondition = function(p)
elseif(p.p_value == "inv_is_empty") then
return who.." has an empty inventory."
end
elseif(p.p_type == "custom") then
return "Call custom function with param: \""..tostring(p.p_value).."\"."
end
-- fallback
return tostring(p.p_value)
@ -392,12 +399,24 @@ yl_speak_up.eval_precondition = function(player, n_id, p)
return inv:is_empty(inv_name)
end
return false
elseif(p.p_type == "custom") then
-- execute the custom function
return yl_speak_up.precondition_custom(player, p.p_value)
end
-- fallback - unknown type
return false
end
-- a custom function that the server can override
yl_speak_up.precondition_custom = function(player, param)
minetest.chat_send_player(player:get_player_name(),
"Checking custom precondition with parameter \""..tostring(param).."\"..")
-- return weather your precondition is fulfilled (true) or not (false)
return true
end
-- these are only wrapper functions for those in fs_edit_general.lua
yl_speak_up.input_fs_edit_preconditions = function(player, formname, fields)