diff --git a/README.md b/README.md index deb03d1..d32ae4a 100644 --- a/README.md +++ b/README.md @@ -178,8 +178,8 @@ NPC to ask for a passwort or the answer to a question the NPC just asked. The player's answer is checked against the *expected answer* that you give when you set up this action. -Custom Actions -============== +Custom Preconditions, Actions and Effects +========================================= You can define custom actions and provide a parameter. The function yl_speak_up.get_fs_action_custom(player, param) gets called when such a function is executed and ought to return a formspec. @@ -187,3 +187,9 @@ Input ought to be sent to the function yl_speak_up.input_fs_action_custom(player, formname, fields) which acts as an example of what you need to take care of in your own implementation of this function. + +You can also define custom preconditions and effects. For more information, +take a look at + custrom_functions_you_can_override.lua +In order to add custom functions, you need to be able to edit that file or +execute Lua code on the server. diff --git a/custrom_functions_you_can_override.lua b/custrom_functions_you_can_override.lua new file mode 100644 index 0000000..67b21ee --- /dev/null +++ b/custrom_functions_you_can_override.lua @@ -0,0 +1,112 @@ + +-- this contains custom functions that you can override on your server +-- please take a look at the example code! + +-- replace some variables in the text the NPC speaks and which the player can use to reply +-- pname: the name of the player that is talking to the NPC; +-- Note: If you want to change this function, best call the original as well after +-- applying your custom changes. +yl_speak_up.replace_vars_in_text = function(text, dialog, pname) + local subs = { + MY_NAME = dialog.n_npc, + NPC_NAME = dialog.n_npc, + OWNER_NAME = dialog.npc_owner, + PLAYER_NAME = pname, + } + + local day_time_name = "day" + local day_time = minetest.get_timeofday() + if(day_time < 0.5) then + day_time_name = "morning" + elseif(day_time < 0.75) then + day_time_name = "afternoon" + else + day_time_name = "evening" + end + subs.GOOD_DAY = "Good "..day_time_name + subs.good_DAY = "good "..day_time_name + + -- Note: the $ char is a special one. It needs to be escaped with %$ in lua. + -- Note: when substitution argument is a table, we look up + -- substitutions in it using substring captured by "()" in + -- pattern. "[%a_]+" means one or more letter or underscore. + -- If lookup returns nil, then no substitution is made. + text = string.gsub(text, "%$([%a_]+)%$", subs) + + return text +end + + +-- a custom precondition; +-- has to return either true (=precondition is fulfilled) or false (=precondition is not fulfilled) +-- Note: This function will be called often. Make it efficient! +yl_speak_up.precondition_custom = function(player, param) + minetest.chat_send_player(player:get_player_name(), + "Checking custom precondition with parameter \""..tostring(param).."\"..") + -- return if your precondition is fulfilled (true) or not (false) + return true +end + + +-- a custom effect; +-- has to return true (=effect was executed successfully) or false (=effect failed for some reason) +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 + + +-- a custom action (the formspec shown to the player); +-- overwrite this function on your server with a custom one that shows the formspec +-- that you want! +yl_speak_up.get_fs_action_custom = function(player, param) + local pname = player:get_player_name() + local dialog = yl_speak_up.speak_to[pname].dialog + return "size[8.5,4.0]".. + "button[0.2,0.0;2.0,0.9;back_to_talk;Back to talk]".. + "button[4.75,0.0;3.0,0.9;finished_action;Stare back]".. + + "tooltip[back_to_talk;Click here if you want the action to FAIL.]".. + "tooltip[finished_action;Click here if you want the action to be a SUCCESS.]".. + "label[0.5,1.7;"..minetest.formspec_escape( + "["..(dialog.n_npc or "- ? -").." stares expectantly at you.]").."]".. + "label[0.5,2.5;Your custom parameter is:]".. + "label[1.5,3.0;"..minetest.formspec_escape(tostring(param)).."]".. + "label[0.5,3.5;Overwrite this function with a custom one!]" +end + + +-- a custom action (checking of the input); +-- overwrite this function on your server with a custom one that checks the +-- input to your formspec - but don't forget to call the necessary functions +-- as shown here +yl_speak_up.input_fs_action_custom = function(player, formname, fields) + -- back from error_msg? then show the formspec again + if(fields.back_from_error_msg) then + yl_speak_up.show_fs(player, "action_custom", nil) + return + end + local pname = player:get_player_name() + local a_id = yl_speak_up.speak_to[pname].a_id + if(fields.back_to_talk) then + -- the action failed + yl_speak_up.execute_next_action(player, a_id, false) + return + end + if(fields.finished_action) then + -- the action was a success + yl_speak_up.execute_next_action(player, a_id, true) + return + end + -- else show a message to the player that he ought to decide + yl_speak_up.show_fs(player, "msg", { + input_to = "yl_speak_up:action_custom", + formspec = "size[7,1.0]".. + "label[0.2,-0.2;".. + "Please click either on \"Stare back\" or \"Back to talk\"!]".. + "button[2,0.5;1.5,0.9;back_from_error_msg;Back]"}) +end + diff --git a/fs_edit_actions.lua b/fs_edit_actions.lua index b6f9b31..f348aa8 100644 --- a/fs_edit_actions.lua +++ b/fs_edit_actions.lua @@ -618,55 +618,6 @@ yl_speak_up.get_fs_action_text_input = function(player, param) end --- overwrite this function on your server with a custom one that checks the --- input to your formspec above -yl_speak_up.input_fs_action_custom = function(player, formname, fields) - -- back from error_msg? then show the formspec again - if(fields.back_from_error_msg) then - yl_speak_up.show_fs(player, "action_custom", nil) - return - end - local pname = player:get_player_name() - local a_id = yl_speak_up.speak_to[pname].a_id - if(fields.back_to_talk) then - -- the action failed - yl_speak_up.execute_next_action(player, a_id, false) - return - end - if(fields.finished_action) then - -- the action was a success - yl_speak_up.execute_next_action(player, a_id, true) - return - end - -- else show a message to the player that he ought to decide - yl_speak_up.show_fs(player, "msg", { - input_to = "yl_speak_up:action_custom", - formspec = "size[7,1.0]".. - "label[0.2,-0.2;".. - "Please click either on \"Stare back\" or \"Back to talk\"!]".. - "button[2,0.5;1.5,0.9;back_from_error_msg;Back]"}) -end - - --- overwrite this function on your server with a custom one that shows the formspec --- that you want! -yl_speak_up.get_fs_action_custom = function(player, param) - local pname = player:get_player_name() - local dialog = yl_speak_up.speak_to[pname].dialog - return "size[8.5,4.0]".. - "button[0.2,0.0;2.0,0.9;back_to_talk;Back to talk]".. - "button[4.75,0.0;3.0,0.9;finished_action;Stare back]".. - - "tooltip[back_to_talk;Click here if you want the action to FAIL.]".. - "tooltip[finished_action;Click here if you want the action to be a SUCCESS.]".. - "label[0.5,1.7;"..minetest.formspec_escape( - "["..(dialog.n_npc or "- ? -").." stares expectantly at you.]").."]".. - "label[0.5,2.5;Your custom parameter is:]".. - "label[1.5,3.0;"..minetest.formspec_escape(tostring(param)).."]".. - "label[0.5,3.5;Overwrite this function with a custom one!]" -end - - -- these are only wrapper functions for those in fs_edit_general.lua yl_speak_up.input_fs_edit_actions = function(player, formname, fields) diff --git a/fs_edit_effects.lua b/fs_edit_effects.lua index 6612ae5..1d0670b 100644 --- a/fs_edit_effects.lua +++ b/fs_edit_effects.lua @@ -694,16 +694,6 @@ yl_speak_up.execute_effect = function(player, n_id, o_id, r) 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) diff --git a/fs_edit_preconditions.lua b/fs_edit_preconditions.lua index ec2fbab..ac21c95 100644 --- a/fs_edit_preconditions.lua +++ b/fs_edit_preconditions.lua @@ -408,15 +408,6 @@ yl_speak_up.eval_precondition = function(player, n_id, p) 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) diff --git a/functions.lua b/functions.lua index 27a3694..7460997 100644 --- a/functions.lua +++ b/functions.lua @@ -2967,39 +2967,6 @@ yl_speak_up.may_edit_npc = function(player, n_id) end --- replace some variables in the text the NPC speaks and which the player can use to reply --- pname: the name of the player that is talking to the NPC -yl_speak_up.replace_vars_in_text = function(text, dialog, pname) - local subs = { - MY_NAME = dialog.n_npc, - NPC_NAME = dialog.n_npc, - OWNER_NAME = dialog.npc_owner, - PLAYER_NAME = pname, - } - - local day_time_name = "day" - local day_time = minetest.get_timeofday() - if(day_time < 0.5) then - day_time_name = "morning" - elseif(day_time < 0.75) then - day_time_name = "afternoon" - else - day_time_name = "evening" - end - subs.GOOD_DAY = "Good "..day_time_name - subs.good_DAY = "good "..day_time_name - - -- Note: the $ char is a special one. It needs to be escaped with %$ in lua. - -- Note: when substitution argument is a table, we look up - -- substitutions in it using substring captured by "()" in - -- pattern. "[%a_]+" means one or more letter or underscore. - -- If lookup returns nil, then no substitution is made. - text = string.gsub(text, "%$([%a_]+)%$", subs) - - return text -end - - -- log changes done by players or admins to NPCs yl_speak_up.log_change = function(pname, n_id, text) -- make sure all variables are defined diff --git a/init.lua b/init.lua index 96961c8..de49385 100644 --- a/init.lua +++ b/init.lua @@ -21,6 +21,12 @@ dofile(modpath .. "privs.lua") dofile(modpath .. "show_fs.lua") -- ask if the player wants to save, discard or go back in edit mode dofile(modpath .. "fs_save_or_discard_or_back.lua") +-- as the name says: a collection of custom functions that you can +-- override on your server or in your game to suit your needs; +-- Note: No special privs are needed to call custom functions. But... +-- of course you can change them only if you have access to +-- the server's file system or can execute lua code. +dofile(modpath .. "custrom_functions_you_can_override.lua") -- common functions for editing preconditions and effects dofile(modpath .. "fs_edit_general.lua") -- edit preconditions (can be reached through edit options dialog)