implementing storing action npc_wants

This commit is contained in:
Sokomine 2021-06-15 00:14:21 +02:00
parent ed333192cb
commit 4d81955421
2 changed files with 52 additions and 21 deletions

View File

@ -5,8 +5,15 @@
-- a_type selected from values_what
-- a_value used to store the subtype of a_type
--
-- on_failure: TODO - does this make sense here?
-- r_value alternate target dialog if the previous *effect* failed
-- npc_gives and npc_wants:
-- a_on_failure if the action fails, go to this dialog
-- a_value itemstack of the given/wanted item in string form
-- a_item_desc the description the NPC shall set for that itemstack
-- (so that the player can distinguish it from other
-- itemstacks with the same items)
-- a_item_quest_id Special ID to make sure that it is really the *right*
-- item and not just something the player faked with an
-- engraving table or something similar
--
-- Note: Trades are not stored as actions - they are stored in
-- dialog.trades[ trade_id ] with <trade_id> == "<d_id> <o_id>"
@ -61,6 +68,7 @@ yl_speak_up.action_inv_changed = function(inv, listname, index, stack, player, h
end
elseif(how == "take" and data.what == 4) then
data.item_desc = "- no item set -"
data.item_node_name = ""
-- "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_node_name = stack:get_name().." "..stack:get_count()
@ -73,6 +81,7 @@ yl_speak_up.action_inv_changed = function(inv, listname, index, stack, player, h
end
elseif(how == "take" and data.what == 5) then
data.item_desc = "- no item set -"
data.item_node_name = ""
end
-- show the updated formspec to the player
yl_speak_up.show_fs(player, "edit_actions", nil)
@ -92,7 +101,9 @@ yl_speak_up.show_action = function(a)
"\" (\""..tostring(a.a_value or "- ? -").."\") "..
"with ID \""..tostring(a.a_item_quest_id or "- no special ID -").."\"."
elseif(a.a_type == "npc_wants") then
return "npc_wants:" -- TODO show ation text
return "The NPC wants \""..tostring(a.a_item_desc or "- default description -")..
"\" (\""..tostring(a.a_value or "- ? -").."\") "..
"with ID \""..tostring(a.a_item_quest_id or "- no special ID -").."\"."
elseif(a.a_type == "text_input") then
return "text_input:" -- TODO show ation text
elseif(a.a_type == "puzzle") then

View File

@ -425,10 +425,15 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields,
v[ "r_value" ] = data.chat_msg_text
-- "The NPC gives something to the player (i.e. a quest item).", -- 4
-- "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 == 4) then
elseif(data.what and id_prefix == "a_" and (data.what == 4 or data.what == 5)) then
local trade_inv_list = "npc_gives"
if(data.what == 5) then
trade_inv_list = "npc_wants"
end
local trade_inv = minetest.get_inventory({type="detached", name="yl_speak_up_player_"..pname})
if(not(trade_inv) or trade_inv:is_empty("npc_gives")) then
if(not(trade_inv) or trade_inv:is_empty( trade_inv_list )) then
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:"..formspec_input_to,
formspec = "size[9,2.5]"..
@ -439,7 +444,7 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields,
end
v[ "a_on_failure" ] = data.action_failure_dialog
-- change the node in the slot
local stack = trade_inv:get_stack("npc_gives", 1)
local stack = trade_inv:get_stack( trade_inv_list, 1)
if(not(stack) or not(minetest.registered_items[ stack:get_name() ])) then
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:"..formspec_input_to,
@ -452,17 +457,26 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields,
local meta = stack:get_meta()
-- what does the NPC want to give?
v[ "a_value" ] = stack:get_name().." "..stack:get_count()
if(data.what == 5) then
data.item_desc = meta:get_string("description")
end
-- set new description if there is one set (optional)
if(data.item_desc
and data.item_desc ~= ""
and data.item_desc ~= "- none set -") then
if(data.what == 4) then
meta:set_string("description", data.item_desc)
end
v[ "a_item_desc" ] = data.item_desc
end
if(data.what == 5) then
data.item_quest_id = meta:get_string("yl_speak_up:quest_id")
end
-- set special ID (optional)
if(data.item_quest_id
and data.item_quest_id ~= ""
and data.item_quest_id ~= "- no item set -") then
if(data.what == 4) then
-- which player got this quest item?
meta:set_string("yl_speak_up:quest_item_for", pname)
-- include the NPC id so that we know which NPC gave it
@ -470,9 +484,10 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields,
-- extend quest_id by NPC id so that it becomes more uniq
meta:set_string("yl_speak_up:quest_id",
tostring(n_id).." "..tostring(data.item_quest_id))
end
v[ "a_item_quest_id" ] = data.item_quest_id
end
if( v["a_item_quest_id"] and not(v[ "a_item_desc"])) then
if( v["a_item_quest_id"] and not(v[ "a_item_desc"]) and data.what == 4) then
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:"..formspec_input_to,
formspec = "size[9,2.5]"..
@ -495,18 +510,13 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields,
return
end
player_inv:add_item("main", stack)
trade_inv:remove_item("npc_gives", stack)
trade_inv:remove_item(trade_inv_list, stack)
-- just send a message that the save was successful and give the player time to
-- take his items back
show_save_msg = "size[9,2.5]"..
"label[0.2,0.5;The information was saved successfully.\n"..
"The item has been returned to your inventory.]"..
"button[1.5,2.0;2,0.9;back_from_error_msg;Back]"
-- "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
-- TODO: implement
end
-- only save if something was actually selected
@ -938,12 +948,17 @@ yl_speak_up.get_fs_edit_option_related = function(player, table_click_result,
if(data and data.action_failure_dialog) then
nr = data.action_failure_dialog + 1
end
local stack_name = ""
if(data and data.item_node_name) then
stack_name = data.item_node_name
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[3.2,4.0;"..minetest.formspec_escape(stack_name).."]"..
"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;"..
@ -989,12 +1004,17 @@ yl_speak_up.get_fs_edit_option_related = function(player, table_click_result,
if(data and data.action_failure_dialog) then
nr = data.action_failure_dialog + 1
end
local stack_name = ""
if(data and data.item_node_name) then
stack_name = data.item_node_name
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[3.2,4.0;"..minetest.formspec_escape(stack_name).."]"..
"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;"..