yl_speak_up/fs_initial_config.lua
2021-06-02 22:43:28 +02:00

183 lines
6.9 KiB
Lua

-- set name, description and owner of the NPC
-- (owner can only be set if the player has the npc_talk_master
-- priv - not with npc_talk_owner priv alone)
yl_speak_up.input_fs_initial_config = function(player, formname, fields)
local pname = player:get_player_name()
local n_id = yl_speak_up.speak_to[pname].n_id
if(fields.back_from_error_msg) then
-- no point in showing the formspec or error message again if we did so already
if(not(yl_speak_up.may_edit_npc(player, n_id))) then
return
end
-- show this formspec again
yl_speak_up.show_fs(player, "initial_config",
{n_id = n_id, d_id = yl_speak_up.speak_to[pname].d_id, false})
return
end
if(not(fields.save_initial_config)) then
-- is the player editing the npc? then leaving this config
-- dialog has to lead back to the talk dialog
if(yl_speak_up.edit_mode[pname] == n_id and n_id) then
yl_speak_up.show_fs(player, "talk",
{n_id = n_id, d_id = yl_speak_up.speak_to[pname].d_id})
end
-- else we can quit here
return
end
local error_msg = nil
-- the player is trying to save the initial configuration
-- is the player allowed to initialize this npc?
if(not(yl_speak_up.may_edit_npc(player, n_id))) then
error_msg = "You are not allowed to edit this NPC."
elseif(not(fields.n_npc) or string.len(fields.n_npc) < 2) then
error_msg = "The name of your NPC needs to be\nat least two characters long."
elseif(not(fields.n_description) or string.len(fields.n_description) < 2) then
error_msg = "Please provide a description of your NPC!"
-- sensible length limit
elseif(string.len(fields.n_npc)>40 or string.len(fields.n_description)>40) then
error_msg = "The name and description of your NPC\ncannot be longer than 40 characters."
-- want to change the owner?
elseif(fields.n_owner and fields.n_owner ~= yl_speak_up.npc_owner[ n_id ]) then
if( not(minetest.check_player_privs(player, {npc_talk_master=true}))) then
error_msg = "You need the \"npc_talk_master\" priv\nin order to change the owner."
elseif(not(minetest.check_player_privs(fields.n_owner, {npc_talk_owner=true}))) then
error_msg = "The NPC can only be owned by players that\n"..
"have the \"npc_talk_owner\" priv. Else the\n"..
"new owner could not edit his own NPC."
end
end
if(error_msg) then
yl_speak_up.show_fs(player, "msg", { input_to = "yl_speak_up:initial_config",
formspec = "size[6,2]"..
"label[0.2,0.0;"..tostring(error_msg).."]"..
"button[2,1.5;1,0.9;back_from_error_msg;Back]"})
return
end
local dialog = yl_speak_up.speak_to[pname].dialog
local d_id = yl_speak_up.speak_to[pname].d_id
local count = 0
if(dialog and dialog.n_dialogs) then
for k,v in pairs(dialog.n_dialogs) do
count = count + 1
end
end
-- we checked earlier if the player doing this change and the
-- player getting the NPC have appropriate privs
if(fields.n_owner ~= yl_speak_up.npc_owner[ n_id ]) then
yl_speak_up.log_change(pname, n_id,
"Owner changed from "..tostring(yl_speak_up.npc_owner[ n_id ])..
" to "..tostring(fields.n_owner).." for "..
"NPC name: \""..tostring(fields.n_npc))
yl_speak_up.npc_owner[ n_id ] = fields.n_owner
end
-- give the NPC its first dialog
if(not(dialog) or count==0) then
local f = {}
-- create a new dialog
f.d_id = yl_speak_up.text_new_dialog_id
-- ...with this text
f.d_text = "$GOOD_DAY$ $PLAYER_NAME$,\nI am $NPC_NAME$. I don't know much yet.\n"..
"Hopefully $OWNER_NAME$ will teach me to talk soon."
-- it is the first, initial dialog
f.d_sort = "0"
f.n_npc = fields.n_npc
f.n_description = fields.n_description
f.npc_owner = yl_speak_up.npc_owner[ n_id ]
-- create and save the first dialog for this npc
local dialog = yl_speak_up.fields_to_dialog(pname, f)
yl_speak_up.save_dialog(n_id, dialog)
yl_speak_up.speak_to[pname].dialog = dialog
yl_speak_up.log_change(pname, n_id,
"Initial config saved. "..
"NPC name: \""..tostring(fields.n_npc)..
"\" Description: \""..tostring(fields.n_description).."\"")
-- just change name and description
else
dialog = yl_speak_up.speak_to[pname].dialog
dialog.n_npc = fields.n_npc
dialog.n_description = fields.n_description
yl_speak_up.save_dialog(n_id, dialog)
yl_speak_up.log_change(pname, n_id,
"Name and/or description changed. "..
"NPC name: \""..tostring(fields.n_npc)..
"\" Description: \""..tostring(fields.n_description).."\"")
end
dialog = yl_speak_up.speak_to[pname].dialog
-- show nametag etc.
if yl_speak_up.speak_to[pname].obj then
local obj = yl_speak_up.speak_to[pname].obj
local ent = obj:get_luaentity()
if ent ~= nil then
ent.yl_speak_up.npc_name = dialog.n_npc
ent.yl_speak_up.npc_description = dialog.n_description
ent.owner = dialog.npc_owner
local i_text = dialog.n_npc .. "\n" ..
dialog.n_description .. "\n" ..
yl_speak_up.infotext
obj:set_properties({infotext = i_text})
yl_speak_up.update_nametag(ent)
end
end
-- actually start a chat with our new npc
yl_speak_up.show_fs(player, "talk", {n_id = n_id, d_id = d_id})
end
-- initialize the npc without having to use a staff;
-- returns true when initialization possible
yl_speak_up.get_fs_initial_config = function(player, n_id, d_id, is_initial_config)
local pname = player:get_player_name()
-- is the player allowed to edit this npc?
if(not(yl_speak_up.may_edit_npc(player, n_id))) then
return "size[6,2]"..
"label[0.2,0.0;Sorry. You are not authorized\nto edit this NPC.]"..
"button_exit[2,1.5;1,0.9;back_from_error_msg;Exit]"
end
local tmp_name = n_id
local tmp_descr = "A new NPC without description"
local tmp_text = "Please provide your new NPC with a name and description!"
local tmp_owner = (yl_speak_up.npc_owner[ n_id ] or "- none -")
-- use existing name and description as presets when just editing
if(not(is_initial_config)) then
local dialog = yl_speak_up.speak_to[pname].dialog
tmp_name = (dialog.n_npc or tmp_name)
tmp_descr = (dialog.n_description or tmp_descr)
tmp_text = "You can change the name and description of your NPC."
end
local formspec = "size[10,5]"..
"label[0.2,0.2;"..tmp_text.."]"..
-- name of the npc
"label[0.2,1.05;Name:]"..
"field[2.0,1.2;4,0.9;n_npc;;"..minetest.formspec_escape(tmp_name).."]"..
"tooltip[n_npc;n_npc: The name of the NPC;#FFFFFF;#000000]"..
-- description of the npc
"label[0.2,2.05;Description:]"..
"field[2.0,2.2;8,0.9;n_description;;"..minetest.formspec_escape(tmp_descr).."]"..
"tooltip[n_description;n_description: A description for the NPC;#FFFFFF;#000000]"..
-- the owner of the NPC
"label[0.2,3.05;Owner:]"..
"field[2.0,3.2;8,0.9;n_owner;;"..minetest.formspec_escape(tmp_owner).."]"..
"tooltip[n_owner;The owner of the NPC. This can only be changed\n"..
"if you have the npc_talk_master priv.;#FFFFFF;#000000]"..
-- save and exit buttons
"button_exit[3.2,4.2;2,0.9;save_initial_config;Save]"..
"button_exit[5.4,4.2;2,0.9;exit;Exit]"
-- show the formspec to the player
return formspec
end