From 0184fe633741a9caef06eb5bc673da24aa372968 Mon Sep 17 00:00:00 2001 From: Sokomine Date: Wed, 12 May 2021 16:30:25 +0200 Subject: [PATCH] added simple replacements for player name, npc name, owner name, time of day --- README.md | 30 ++++++++++++++++++++++++++++++ functions.lua | 51 +++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 73 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index be432f2..2ebfc0e 100644 --- a/README.md +++ b/README.md @@ -57,3 +57,33 @@ mode by claiming to be the NPC's owner. Skin ==== 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. diff --git a/functions.lua b/functions.lua index 2486ec2..22a10d7 100644 --- a/functions.lua +++ b/functions.lua @@ -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, "]") 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;") - table.insert(formspec, minetest.formspec_escape(active_dialog.d_text) .. "\n") + table.insert(formspec, t .. "\n") table.insert(formspec, "]") 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]") 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 elseif allowed[sb_v.o_id] == true then 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, - "tooltip[button_" .. oid .. ";" .. minetest.formspec_escape(sb_v.o_text_when_prerequisites_met) .. "]" + "tooltip[button_" .. oid .. ";" .. t .. "]" ) 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 if sb_v.o_hide_when_prerequisites_not_met == "true" then 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 sb_v.o_grey_when_prerequisites_not_met == "true" and 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, - "tooltip[button_" .. oid .. ";" .. minetest.formspec_escape(sb_v.o_text_when_prerequisites_not_met) .. "]" + "tooltip[button_" .. oid .. ";" .. t .. "]" ) local l = h + 0.45 table.insert( 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]") 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, - "tooltip[button_" .. oid .. ";" .. minetest.formspec_escape(sb_v.o_text_when_prerequisites_not_met) .. "]" + "tooltip[button_" .. oid .. ";" .. t .. "]" ) local l = h + 0.45 table.insert( formspec, - "label[0.7," .. l .. ";" .. minetest.formspec_escape(sb_v.o_text_when_prerequisites_not_met) .. "]" + "label[0.7," .. l .. ";" .. t .. "]" ) 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_master=true})) 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