preparations/formspecs for npc_wants/npc_gives

This commit is contained in:
Sokomine 2021-06-14 01:12:12 +02:00
parent 5d82cbc32d
commit a1ce1db375
4 changed files with 169 additions and 10 deletions

View File

@ -31,6 +31,41 @@ local check_what = {
local values_what = {"", "none", "trade", "npc_gives", "npc_wants", "text_input", "puzzle", "custom"}
-- monitor changes to the npc_gives and npc_wants slots (in particular when editing actions)
-- how: can be "put" or "take"
yl_speak_up.action_inv_changed = function(inv, listname, index, stack, player, how)
if(not(player)) then
return
end
local pname = player:get_player_name()
local n_id = yl_speak_up.speak_to[pname].n_id
-- if not in edit mode: the player may just be normally interacting with the NPC;
-- nothing to do for us here (wait for the player to click on "save")
if(not(n_id) or yl_speak_up.edit_mode[pname] ~= n_id) then
return
end
-- is the player in the process of editing an action of the npc_gives/npc_wants type?
local data = yl_speak_up.speak_to[pname][ "tmp_action" ]
if(not(data) or (data.what ~= 4 and data.what ~= 5)) then
return
end
-- "The NPC gives something to the player (i.e. a quest item).", -- 4
if( how == "put" and data.what == 4) then
data.item_desc = stack:get_description()
elseif(how == "take" and data.what == 4) then
data.item_desc = "- no item set -"
-- "The player is expected to give something to the NPC (i.e. a quest item).", -- 5
elseif(how == "put" and data.what == 5) then
data.item_desc = stack:get_description()
elseif(how == "take" and data.what == 5) then
data.item_desc = "- no item set -"
end
-- show the updated formspec to the player
yl_speak_up.show_fs(player, "edit_actions", nil)
-- TODO: implement
end
-- returns a human-readable text as description of the action
-- (as shown in the edit options dialog and in the edit effect formspec)
yl_speak_up.show_action = function(a)

View File

@ -43,7 +43,7 @@ local check_what = {
"an internal state (i.e. of a quest)", -- 2
"a block somewhere", -- 3
"NPC crafts soemthing", -- 4
"go to other dialog if the action (i.e. trade) failed", -- 5
"go to other dialog if the previous effect failed", -- 5
"send a chat message to all players", -- 6
}

View File

@ -471,14 +471,35 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields,
elseif(fields.select_on_failure) then
-- in this case we really want the name of the target dialog
yl_speak_up.speak_to[pname][ tmp_data_cache ].on_failure = fields.select_on_failure
elseif(fields.select_on_action_failure
and data and data.what and (data.what == 4 or data.what == 5) and id_prefix == "a_") then
local dialog = yl_speak_up.speak_to[pname].dialog
local sorted_dialog_list = yl_speak_up.sort_keys(dialog.n_dialogs)
local nr = table.indexof(sorted_dialog_list, fields.select_on_action_failure)
yl_speak_up.speak_to[pname][ tmp_data_cache ].action_failure_dialog = nr
end
-- the player wants to change/edit a precondition
-- "Normal trade - one item(stack) for another item(stack).", -- 3
-- (only for actions)
-- TODO: this needs to be adjusted
if(data and data.what == 3 and id_prefix == "a_") then
-- remember which option was selected
yl_speak_up.speak_to[pname].o_id = o_id
-- do not switch target dialog (we are in edit mode)
yl_speak_up.speak_to[pname].target_d_id = nil
-- create a new trade for this dialog and option - with ID "<d_id> <o_id>"
yl_speak_up.show_fs(player, "add_trade_simple", tostring(d_id).." "..tostring(o_id))
return
end
-- the player wants to change/edit a precondition or effect
if(not(fields.back)
and (fields.change_element or fields.select_what or fields.select_trade
or fields.select_inv or fields.select_block
or fields.select_variable or fields.select_operator
or fields.select_on_failure
or fields.select_on_action_failure
or fields.back_from_error_msg)) then
yl_speak_up.show_fs(player, formspec_input_to)
return
@ -587,21 +608,25 @@ yl_speak_up.get_fs_edit_option_related = function(player, table_click_result,
if(not(x_id)) then
x_id = "new"
end
local save_button = "button[5.0,9.3;1,0.7;save_element;Save]"
local save_button = "button[5.0,9.8;1,0.7;save_element;Save]"
local formspec =
"formspec_version[3]"..
"size[20,10]"..
"size[20,11]"..
"label[5,0.5;Edit "..element_desc.." \""..minetest.formspec_escape(x_id).."\"]"..
"label[0.2,1.5;"..what_do_you_want_txt.."]"..
"label[0.2,2.0;Something regarding...]"..
"dropdown[4.0,1.8;14.0,0.6;select_what;"..
table.concat(check_what, ",")..";"..
tostring(data.what)..";]"..
"label[1,8.5;If you are unsure if your setup of pre(C)onditions and (Ef)fects "..
"button[3.0,9.8;1,0.7;back;Abort]"
if(id_prefix ~= "a_") then
formspec = formspec..
"label[1,9.0;If you are unsure if your setup of pre(C)onditions and (Ef)fects "..
"works as intended,\ntype \"/npc_talk_debug "..tostring(n_id).."\" "..
"in chat in order to enter debug mode. You can leave it with "..
"\"/npc_talk_debug off\".]"..
"button[3.0,9.3;1,0.7;back;Abort]"
"\"/npc_talk_debug off\".]"
end
-- "an internal state (i.e. of a quest)", -- 2
-- (state is the second offered option in both preconditions and effects list)
@ -788,7 +813,95 @@ yl_speak_up.get_fs_edit_option_related = function(player, table_click_result,
"label[0.2,5.3;Note: Your chat message needs to contain the following placeholders,"..
" which will be replaced automaticly like in dialog texts:"..
"\n$NPC_NAME$, $PLAYER_NAME$ and $OWNER_NAME$.]"
end
-- "The NPC gives something to the player (i.e. a quest item).", -- 4
-- (only for actions)
elseif(data.what and id_prefix == "a_" and data.what == 4) then
local pname = player:get_player_name()
local dialog = yl_speak_up.speak_to[pname].dialog
local item_quest_id = "- none set -"
if(data and data.item_quest_id) then
item_quest_id = data.item_quest_id
end
local item_desc = "- no item set -"
if(data and data.item_desc) then
item_desc = data.item_desc
end
local nr = 1
if(data and data.action_failure_dialog) then
nr = data.action_failure_dialog + 1
end
local sorted_dialog_list = yl_speak_up.sort_keys(dialog.n_dialogs)
formspec = formspec..
"label[8,2.6;Your invnetory:]"..
"list[current_player;main;8,3;8,4;]"..
"label[1,3.1;"..minetest.formspec_escape(dialog.n_npc or "?").." gives:]"..
"list[detached:yl_speak_up_player_"..pname..";npc_gives;2,3.5;1,1;]"..
"label[1,4.9;The player shall take this offered item.\n"..
"If he doesn't, go to dialog:]"..
"dropdown[5.0,5.1;2.8,0.6;select_on_action_failure;"..
"- current one -,"..
table.concat(sorted_dialog_list, ",")..";"..tostring(nr)..";]"..
"label[0.2,6.1;Set a description to turn the item into a special\n"..
"quest item. Set a special ID (short text) so that\n"..
"the player cannot create a fake item. Click on \n"..
"\"Save\" to apply the changes.]"..
"label[0.2,8.3;Special ID to set:]"..
"field[3.2,8.0;14.5,0.6;action_item_quest_id;;"..
minetest.formspec_escape(tostring(item_quest_id)).."]"..
"tooltip[action_item_quest_id;"..
"Set this to a text that helps *you* to remember what this\n"..
"special quest item is for (i.e. \"quest_deliver_augusts_"..
"letter\").\n"..
"The ID will be extended with the ID of the NPC and the\n"..
"name of the player who got this item from the NPC.]"..
"label[0.2,9.0;Description to set:]"..
"field[3.2,8.7;14.5,0.6;action_item_desc;;"..
minetest.formspec_escape(tostring(item_desc)).."]"..
"tooltip[action_item_desc;"..
"Set this to a text that helps the *player* to remember what\n"..
"this special quest item is for (i.e. \"Letter from August to\n"..
"Frederike\" for a piece of paper).\n"..
"This description is shown in the inventory on mouseover.]"
-- "The player is expected to give something to the NPC (i.e. a quest item).", -- 5
-- (only for actions)
elseif(data.what and id_prefix == "a_" and data.what == 5) then
local pname = player:get_player_name()
local dialog = yl_speak_up.speak_to[pname].dialog
local item_quest_id = "- none set -"
if(data and data.item_quest_id) then
item_quest_id = data.item_quest_id
end
local item_desc = "- no item set -"
if(data and data.item_desc) then
item_desc = data.item_desc
end
local nr = 1
if(data and data.action_failure_dialog) then
nr = data.action_failure_dialog + 1
end
local sorted_dialog_list = yl_speak_up.sort_keys(dialog.n_dialogs)
formspec = formspec..
"label[8,2.6;Your invnetory:]"..
"list[current_player;main;8,3;8,4;]"..
"label[1,3.1;"..minetest.formspec_escape(dialog.n_npc or "?").." wants:]"..
"list[detached:yl_speak_up_player_"..pname..";npc_wants;2,3.5;1,1;]"..
"label[1,4.9;The player shall give the NPC this item.\n"..
"If he doesn't, go to dialog:]"..
"dropdown[5.0,5.1;2.8,0.6;select_on_action_failure;"..
"- current one -,"..
table.concat(sorted_dialog_list, ",")..";"..tostring(nr)..";]"..
"label[0.2,6.1;If you want a special ID and description, create\n"..
"those via the \"NPC gives something to the player\"\n"..
"menu option first and insert that item here.]"..
"label[0.2,8.3;Expected special ID:]"..
"label[4.0,8.3;"..
minetest.formspec_escape(tostring(item_quest_id)).."]"..
"label[0.2,9.0;Expected description:]"..
"label[4.0,9.0;"..
minetest.formspec_escape(tostring(item_desc)).."]"
end
return formspec..save_button
end

View File

@ -748,6 +748,10 @@ minetest.register_on_joinplayer(function(player, last_login)
local pname = player:get_player_name()
-- show formspec with updated information (perhaps sale is now possible)
yl_speak_up.show_fs(player, "trade_simple")
elseif(listname == "npc_gives"
or listname == "npc_wants") then
-- monitor changes in order to adjust the formspec
yl_speak_up.action_inv_changed(inv, listname, index, stack, player, "put")
end
end,
on_take = function(inv, listname, index, stack, player)
@ -762,6 +766,10 @@ minetest.register_on_joinplayer(function(player, last_login)
-- information may require an update (NPC might now be out of stock), or
-- the player can do the trade a second time
yl_speak_up.show_fs(player, "trade_simple")
elseif(listname == "npc_gives"
or listname == "npc_wants") then
-- monitor changes in order to adjust the formspec
yl_speak_up.action_inv_changed(inv, listname, index, stack, player, "take")
end
end,
})
@ -770,4 +778,7 @@ minetest.register_on_joinplayer(function(player, last_login)
trade_inv:set_size("buy", 1)
-- for setting up new simple trades
trade_inv:set_size("setup", 2*1)
-- for setting up actions
trade_inv:set_size("npc_gives", 1)
trade_inv:set_size("npc_wants", 1)
end)