yl_speak_up/custom_functions_you_can_override.lua

290 lines
12 KiB
Lua

-----------------------------------------------------------------------------
-- This can be customized for each server.
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
-- Placeholders (i.e. $NPC_NAME$) in texts
-----------------------------------------------------------------------------
-- 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 or "", "%$([%a_]+)%$", subs)
return text
end
-----------------------------------------------------------------------------
-- Custom preconditions
-----------------------------------------------------------------------------
-- When you change an existing texts NPC that make use of that precondition will no
-- longer be able to recognize it. You have to reconfigure all those NPC!
-- Adding new texts is no problem.
yl_speak_up.custom_server_functions.precondition_descriptions = {
"(internal) hour of ingame day",
"(internal) player's health points",
"(internal) player has priv:",
}
-- please return a useful value depending on the function;
-- parameter:
-- player the player object
-- desc entry from yl_speak_up.custom_server_functions.precondition_descriptions above
-- precondition contains the data of the precondition for additional information;
-- precondition.p_var_cmp_value contains a user-specified value against which
-- the return value of this function is checked. This depends on
-- precondition.p_operator. Depending on the operator, precondition.p_var_cmp_value
-- may also be used as a parameter to this function.
-- Just make sure to tell your users what return values they shall expect and which
-- operators make sense!
yl_speak_up.custom_server_functions.precondition_eval = function(player, descr, precondition)
if(descr == "(internal) hour of ingame day") then
-- timeofday is between 0..1; translate to 24 hours
return math.floor((minetest.get_timeofday() * 24)+0.5)
elseif(descr == "(internal) player's health points") then
return player:get_hp()
elseif(descr == "(internal) player has priv:") then
-- the name of the priv is derived from the parameter
local ret = minetest.check_player_privs(player,
minetest.string_to_privs(tostring(precondition.p_var_cmp_value)))
-- evaluate the parameter directly
if(not(ret) or precondition.p_operator == "true_for_param"
or precondition.p_operator == "false_for_param") then
return ret
end
-- return the parameter for == / != comparison
return tostring(precondition.p_var_cmp_value)
-- if this custom server function does not exist: return false
else
return false
end
end
-- a custom precondition; still works, but less useful than the above method.
-- Please use
-- yl_speak_up.custom_server_functions.precondition_descriptions = {..}
-- and
-- 1yl_speak_up.custom_server_functions.precondition_eval(player, descr, precondition)
-- instead!
-- 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
-----------------------------------------------------------------------------
-- Custom effects
-----------------------------------------------------------------------------
-- 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
-----------------------------------------------------------------------------
-- Custom actions
-----------------------------------------------------------------------------
-- 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]"..
"button[4.75,0.8;3.0,0.9;failed_action;Fail to stare back]"..
"tooltip[back_to_talk;Click here if you want to abort/cancel.]"..
"tooltip[failed_action;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 was aborted
yl_speak_up.execute_next_action(player, a_id, nil)
return
end
if(fields.failed_action) 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
-----------------------------------------------------------------------------
-- Custom preconditions and effects (functions; they have the type "evaluate")
-----------------------------------------------------------------------------
-- each entry in the table has the following format:
-- key: for display in edit options dialog and dropdown menu,
-- function_name: name of the function that shall be called
-- description: long description of what the function does,
-- param1_text: label for the input field for param1 (if empty, no input field is offered)
-- param1_desc: mouseover text for the input field for param1
-- ...
-- param9_text: label for the input field for param1
-- param9_desc: mouseover text for the input field for param1
--
-- preconditions:
yl_speak_up.custom_functions_p_ = {}
-- actions: (not yet used)
yl_speak_up.custom_functions_a_ = {}
-- results:
yl_speak_up.custom_functions_r_ = {}
-- example function for preconditions:
yl_speak_up.custom_functions_p_[ "example function" ] = {
function_call = "yl_speak_up.custom_example_function",
description = "This function is just an example. It does nothing.",
param1_text = "1. Parameter:",
param1_desc = "This is the value passed to the function as first parameter.",
param2_text = "2. Parameter:",
param2_desc = "This is the value passed to the function as second parameter.",
param3_text = "3. Parameter:",
param3_desc = "This is the value passed to the function as 3. parameter.",
param4_text = "4. Parameter:",
param4_desc = "This is the value passed to the function as 4. parameter.",
param5_text = "5. Parameter:",
param5_desc = "This is the value passed to the function as 5. parameter.",
param6_text = "6. Parameter:",
param6_desc = "This is the value passed to the function as 6. parameter.",
param7_text = "7. Parameter:",
param7_desc = "This is the value passed to the function as 7. parameter.",
param8_text = "8. Parameter:",
param8_desc = "This is the value passed to the function as 8. parameter.",
param9_text = "9. Parameter:",
param9_desc = "This is the value passed to the function as 9. parameter.",
}
-- TODO: actually implement these as examples
yl_speak_up.custom_functions_p_[ "set_variable_to_random_number" ] = {
function_call = "yl_speak_up.set_variable_to_random_number",
description = "Set a variable to a random number.",
param1_text = "Name of the variable:",
param1_desc = "Which variable do you want to set to a random number?",
param2_text = "Minimum value:",
param2_desc = "Which is the MINIMUM value the varible might be set to?",
param3_text = "Maximum value:",
param3_desc = "Which is the MAXIMUM value the varible might be set to?",
}
yl_speak_up.custom_functions_p_[ "set_variable_to_random_value_from_list" ] = {
function_call = "yl_speak_up.select_random_value_from_list",
description = "Set a variable to a random value from a list.",
param1_text = "Name of the variable:",
param1_desc = "Which variable do you want to set to a random value from your list?",
param2_text = "Possible values:",
param2_desc = "Enter all the possible values/texts for your variable here.\n"..
"Seperate the entires by \"|\", i.e.: \"entry1|entry2|entry3\".",
}
yl_speak_up.custom_functions_p_[ "get_level_of_player" ] = {
function_call = "yl_speak_up.get_player_level",
description = "Get the level the player has achieved.",
}
-- example function for results/effects:
yl_speak_up.custom_functions_r_[ "example function" ] = {
function_call = "yl_speak_up.custom_example_function",
description = "Describe here in short form what your function does.",
description = "This function is just an example. It does nothing.",
param1_text = "1. Parameter:",
param1_desc = "This is the value passed to the function as first parameter.",
param2_text = "2. Parameter:",
param2_desc = "This is the value passed to the function as second parameter.",
param3_text = "3. Parameter:",
param3_desc = "This is the value passed to the function as 3. parameter.",
param4_text = "4. Parameter:",
param4_desc = "This is the value passed to the function as 4. parameter.",
param5_text = "5. Parameter:",
param5_desc = "This is the value passed to the function as 5. parameter.",
param6_text = "6. Parameter:",
param6_desc = "This is the value passed to the function as 6. parameter.",
param7_text = "7. Parameter:",
param7_desc = "This is the value passed to the function as 7. parameter.",
param8_text = "8. Parameter:",
param8_desc = "This is the value passed to the function as 8. parameter.",
param9_text = "9. Parameter:",
param9_desc = "This is the value passed to the function as 9. parameter.",
}