implemented storing action npc_gives

This commit is contained in:
Sokomine 2021-06-14 23:44:28 +02:00
parent 2ace5da2c9
commit ed333192cb
3 changed files with 147 additions and 5 deletions

View File

@ -51,12 +51,26 @@ yl_speak_up.action_inv_changed = function(inv, listname, index, stack, player, h
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()
data.item_node_name = stack:get_name().." "..stack:get_count()
local meta = stack:get_meta()
if(meta and meta:get_string("description")) then
data.item_desc = meta:get_string("description")
end
if(meta and meta:get_string("yl_speak_up:quest_id")) then
data.item_quest_id = meta:get_string("yl_speak_up:quest_id")
end
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()
data.item_node_name = stack:get_name().." "..stack:get_count()
local meta = stack:get_meta()
if(meta and meta:get_string("description")) then
data.item_desc = meta:get_string("description")
end
if(meta and meta:get_string("yl_speak_up:quest_id")) then
data.item_quest_id = meta:get_string("yl_speak_up:quest_id")
end
elseif(how == "take" and data.what == 5) then
data.item_desc = "- no item set -"
end
@ -74,7 +88,9 @@ yl_speak_up.show_action = function(a)
elseif(a.a_type == "trade") then
return "trade:" -- TODO show ation text
elseif(a.a_type == "npc_gives") then
return "npc_gives:" -- TODO show ation text
return "The NPC gives \""..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 == "npc_wants") then
return "npc_wants:" -- TODO show ation text
elseif(a.a_type == "text_input") then

View File

@ -250,6 +250,20 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields,
elseif(fields.chat_msg_text
and data and data.what and data.what == 6 and id_prefix == "r_") then
data.chat_msg_text = fields.chat_msg_text
elseif(fields.action_item_quest_id
and fields.action_item_quest_id ~= ""
and fields.action_item_quest_id ~= "- none set -"
and data and data.what and data.what == 4 and id_prefix == "a_") then
data.item_quest_id = fields.action_item_quest_id
end
-- action_item_quest_id and action_item_desc can be set at the same time
if(fields.action_item_desc
and fields.action_item_desc ~= ""
and fields.action_item_desc ~= "- no item set -"
and data and data.what and data.what == 4 and id_prefix == "a_") then
-- TODO: check if it diffrent from the default one of the stack
data.item_desc = fields.action_item_desc
end
-- the save button was pressed
@ -281,6 +295,9 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields,
end
v[ id_prefix.."id" ] = x_id
-- if needed: show a message after successful save so that the player can take
-- his items back from the trade_inv slots
local show_save_msg = nil
local sorted_key_list = yl_speak_up.sort_keys(elements)
if( x_id == "new" and #sorted_key_list >= max_entries_allowed) then
-- this really should not happen during the normal course of operation
@ -406,6 +423,90 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields,
return
end
v[ "r_value" ] = data.chat_msg_text
-- "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 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
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:"..formspec_input_to,
formspec = "size[9,2.5]"..
"label[0.2,0.5;Please insert an item first! Your NPC"..
"needs\nto know what it shall give to the player.]"..
"button[1.5,2.0;2,0.9;back_from_error_msg;Back]"})
return
end
v[ "a_on_failure" ] = data.action_failure_dialog
-- change the node in the slot
local stack = trade_inv:get_stack("npc_gives", 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,
formspec = "size[9,2.5]"..
"label[0.2,0.5;This item is unkown. Please use only known"..
"items.]"..
"button[1.5,2.0;2,0.9;back_from_error_msg;Back]"})
return
end
local meta = stack:get_meta()
-- what does the NPC want to give?
v[ "a_value" ] = stack:get_name().." "..stack:get_count()
-- set new description if there is one set (optional)
if(data.item_desc
and data.item_desc ~= ""
and data.item_desc ~= "- none set -") then
meta:set_string("description", data.item_desc)
v[ "a_item_desc" ] = data.item_desc
end
-- set special ID (optional)
if(data.item_quest_id
and data.item_quest_id ~= ""
and data.item_quest_id ~= "- no item set -") 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
meta:set_string("yl_speak_up:quest_item_from", tostring(n_id))
-- 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))
v[ "a_item_quest_id" ] = data.item_quest_id
end
if( v["a_item_quest_id"] and not(v[ "a_item_desc"])) then
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:"..formspec_input_to,
formspec = "size[9,2.5]"..
"label[0.2,0.5;You can't set a special quest ID without "..
"also changing\nthe description. The player would be "..
"unable to tell\nthe quest item and normal items "..
"apartapart.]"..
"button[1.5,2.0;2,0.9;back_from_error_msg;Back]"})
return
end
local player_inv = player:get_inventory()
if(not(player_inv:room_for_item("main", stack))) then
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:"..formspec_input_to,
formspec = "size[9,2.5]"..
"label[0.2,0.5;You have no room in your inventory for "..
"the example\nitem. Please make room so that it can be"..
"given back to you!]"..
"button[1.5,2.0;2,0.9;back_from_error_msg;Back]"})
return
end
player_inv:add_item("main", stack)
trade_inv:remove_item("npc_gives", 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
@ -422,6 +523,12 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields,
table.insert(yl_speak_up.npc_was_changed[ n_id ],
"Dialog "..tostring(d_id)..": "..element_desc.." "..tostring(x_id)..
" added/changed for option "..tostring(o_id)..".")
if(show_save_msg) then
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:"..formspec_input_to,
formspec = show_save_msg})
return
end
-- TODO: when trying to save: save to disk as well?
-- show the new/changed precondition
yl_speak_up.show_fs(player, formspec_input_to, x_id)

View File

@ -174,8 +174,23 @@ yl_speak_up.get_fs_edit_option_dialog = function(player, n_id, d_id, o_id)
"Maximum amount of pre(C)onditions per option reached!"
end
-- build action list the same way as list of preconditions and effects
local list_of_actions = ""
-- TODO: build the same way as list of preconditions and effects
local actions = d_option.actions
local count_actions = 0
if(actions) then
local sorted_key_list = yl_speak_up.sort_keys(actions)
for i, k in ipairs(sorted_key_list) do
local v = actions[ k ]
list_of_actions = list_of_actions..
minetest.formspec_escape(v.a_id)..",#FFFF00,"..
minetest.formspec_escape(v.a_type)..","..
minetest.formspec_escape(
yl_speak_up.show_action(v))..","
count_actions = count_actions + 1
end
end
--[[ TODO handle trade actions
if(dialog.trades and dialog.trades[ tostring(d_id).." "..tostring(o_id) ]) then
local tr = dialog.trades[ tostring(d_id).." "..tostring(o_id)]
-- show the trade in the result/effects list
@ -184,8 +199,12 @@ yl_speak_up.get_fs_edit_option_dialog = function(player, n_id, d_id, o_id)
minetest.formspec_escape("NPC sells "..
table.concat(tr.buy, ";").." for "..
table.concat(tr.pay, ";"))
--]]
if(count_actions < yl_speak_up.max_actions) then
list_of_actions = list_of_actions..",#00FF00,add,Add a new (A)ction"
else
list_of_actions = ",#00FF00,add,Add a new (A)ction"
list_of_actions = list_of_actions..",#AAAAAA,-,"..
"Maximum amount of (A)ctions per option reached!"
end
-- find the right target dialog for this option (if it exists)