added simple replacements for player name, npc name, owner name, time of day

This commit is contained in:
Sokomine 2021-05-12 16:30:25 +02:00
parent 771b980356
commit 0184fe6337
2 changed files with 73 additions and 8 deletions

View File

@ -57,3 +57,33 @@ mode by claiming to be the NPC's owner.
Skin Skin
==== ====
The skin and what the NPC wields can be changed via the "Edit Skin" button. The skin and what the NPC wields can be changed via the "Edit Skin" button.
Simple variables
================
If you want to let your NPC greet the player by name, you can do so. Some
variables/texts are replaced appropriately in the text the NPC says and
the player can reply:
$MY_NAME$ will be replaced by the name of the NPC
$NPC_NAME$ same as above
$OWNER_NAME$ will be replaced by the name of the owner of the NPC
$PLAYER_NAME$ will be replaced by the name of the player talking to the NPC
$GOOD_DAY$ will be replaced by "Good morning", "Good afternoon" or
"Good evening" - depending on the ingame time of day
$good_DAY$ same as above, but starts with a lowercase letter (i.e.
"good morning")
Note: If you want to extend this, you can do the following in your own mod:
local old_function = yl_speak_up.replace_vars_in_text
yl_speak_up.replace_vars_in_text = function(text, dialog, pname)
-- do not forget to call the old function
text = old_function(text, dialog, pname)
-- do your own replacements
text = string.gsub(text, "$TEXT_TO_REPLACE$", "new text")
-- do not forget to return the new text
return text
end
The replacements will not be applied in edit mode.

View File

@ -1279,11 +1279,14 @@ local function get_fs_talkdialog(player, n_id, d_id)
table.insert(formspec, minetest.formspec_escape(active_dialog.d_text)) table.insert(formspec, minetest.formspec_escape(active_dialog.d_text))
table.insert(formspec, "]") table.insert(formspec, "]")
else else
-- replace $NPC_NAME$ etc.
local t = minetest.formspec_escape(yl_speak_up.replace_vars_in_text(
active_dialog.d_text, dialog, pname))
table.insert(formspec, "hypertext[0.2,5;19.6,17.8;d_text;<normal>") table.insert(formspec, "hypertext[0.2,5;19.6,17.8;d_text;<normal>")
table.insert(formspec, minetest.formspec_escape(active_dialog.d_text) .. "\n</normal>") table.insert(formspec, t .. "\n</normal>")
table.insert(formspec, "]") table.insert(formspec, "]")
table.insert(formspec, "tooltip[d_text;") table.insert(formspec, "tooltip[d_text;")
table.insert(formspec, minetest.formspec_escape(active_dialog.d_text):trim()) table.insert(formspec, t:trim())
table.insert(formspec, ";#000000;#FFFFFF]") table.insert(formspec, ";#000000;#FFFFFF]")
end end
@ -1362,16 +1365,22 @@ local function get_fs_talkdialog(player, n_id, d_id)
-- normal mode: show an option if the prerequirements (if any are defined) are met -- normal mode: show an option if the prerequirements (if any are defined) are met
elseif allowed[sb_v.o_id] == true then elseif allowed[sb_v.o_id] == true then
h = h + 1 h = h + 1
-- replace $NPC_NAME$ etc.
local t = minetest.formspec_escape(yl_speak_up.replace_vars_in_text(
sb_v.o_text_when_prerequisites_met, dialog, pname))
table.insert(formspec, "button[0.5," .. h .. ";53.8,0.9;button_" .. oid .. ";]") table.insert(formspec, "button[0.5," .. h .. ";53.8,0.9;button_" .. oid .. ";]")
table.insert( table.insert(
formspec, formspec,
"tooltip[button_" .. oid .. ";" .. minetest.formspec_escape(sb_v.o_text_when_prerequisites_met) .. "]" "tooltip[button_" .. oid .. ";" .. t .. "]"
) )
local l = h + 0.45 local l = h + 0.45
table.insert(formspec, "label[0.7," .. l .. ";" .. minetest.formspec_escape(sb_v.o_text_when_prerequisites_met) .. "]") table.insert(formspec, "label[0.7," .. l .. ";" .. t .. "]")
else else
if sb_v.o_hide_when_prerequisites_not_met == "true" then if sb_v.o_hide_when_prerequisites_not_met == "true" then
else else
-- replace $NPC_NAME$ etc.
local t = minetest.formspec_escape(yl_speak_up.replace_vars_in_text(
sb_v.o_text_when_prerequisites_not_met, dialog, pname))
if if
sb_v.o_grey_when_prerequisites_not_met == "true" and sb_v.o_grey_when_prerequisites_not_met == "true" and
sb_v.o_text_when_prerequisites_not_met == "" sb_v.o_text_when_prerequisites_not_met == ""
@ -1400,12 +1409,12 @@ local function get_fs_talkdialog(player, n_id, d_id)
table.insert(formspec, "button[0.5," .. h .. ";53.8,0.9;button_" .. oid .. ";]") table.insert(formspec, "button[0.5," .. h .. ";53.8,0.9;button_" .. oid .. ";]")
table.insert( table.insert(
formspec, formspec,
"tooltip[button_" .. oid .. ";" .. minetest.formspec_escape(sb_v.o_text_when_prerequisites_not_met) .. "]" "tooltip[button_" .. oid .. ";" .. t .. "]"
) )
local l = h + 0.45 local l = h + 0.45
table.insert( table.insert(
formspec, formspec,
"label[0.7," .. l .. ";" .. minetest.formspec_escape(sb_v.o_text_when_prerequisites_not_met) .. "]" "label[0.7," .. l .. ";" .. t .. "]"
) )
table.insert(formspec, "box[0.5," .. h .. ";53.8,0.9;#BBBBBB]") table.insert(formspec, "box[0.5," .. h .. ";53.8,0.9;#BBBBBB]")
end end
@ -1424,12 +1433,12 @@ local function get_fs_talkdialog(player, n_id, d_id)
table.insert(formspec, "button[0.5," .. h .. ";53.8,0.9;button_" .. oid .. ";]") table.insert(formspec, "button[0.5," .. h .. ";53.8,0.9;button_" .. oid .. ";]")
table.insert( table.insert(
formspec, formspec,
"tooltip[button_" .. oid .. ";" .. minetest.formspec_escape(sb_v.o_text_when_prerequisites_not_met) .. "]" "tooltip[button_" .. oid .. ";" .. t .. "]"
) )
local l = h + 0.45 local l = h + 0.45
table.insert( table.insert(
formspec, formspec,
"label[0.7," .. l .. ";" .. minetest.formspec_escape(sb_v.o_text_when_prerequisites_not_met) .. "]" "label[0.7," .. l .. ";" .. t .. "]"
) )
end end
end end
@ -3548,3 +3557,29 @@ yl_speak_up.may_edit_npc = function(player, n_id)
or minetest.check_player_privs(player, {npc_talk_master=true}) or minetest.check_player_privs(player, {npc_talk_master=true})
or minetest.check_player_privs(player, {npc_master=true})) or minetest.check_player_privs(player, {npc_master=true}))
end 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)
-- Note: the $ char is a special one. It needs to be escaped with %$ in lua.
text = string.gsub(text, "%$MY_NAME%$", dialog.n_npc)
text = string.gsub(text, "%$NPC_NAME%$", dialog.n_npc)
text = string.gsub(text, "%$OWNER_NAME%$", dialog.npc_owner)
text = string.gsub(text, "%$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
text = string.gsub(text, "%$GOOD_DAY%$", "Good "..day_time_name)
text = string.gsub(text, "%$good_DAY%$", "good "..day_time_name)
minetest.chat_send_player("singleplayer","replaced text: "..tostring(text))
return text
end