yl_speak_up/fs_edit_actions.lua
2021-06-15 19:40:56 +02:00

158 lines
5.9 KiB
Lua

-- Which diffrent types of actions are available?
-- -> The following fields are part of an action:
-- a_id the ID/key of the action
-- a_type selected from values_what
-- a_value used to store the subtype of a_type
--
-- 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>"
--
-- some helper lists for creating the formspecs and evaulating
-- the player's answers:
-- general direction of what could make up an action
local check_what = {
"- please select -",
"No action (default).", -- 2
"Normal trade - one item(stack) for another item(stack).", -- 3
"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
"The player has to manually enter a password or passphrase or some other text.", -- 6
"The player has to move virtual items in a virtual inventory to the right position.", -- 7
"Call custom functions that are supposed to be overridden by the server.", -- 8
}
-- how to store these as a_type in the action:
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_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 -"
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()
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 -"
data.item_node_name = ""
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)
if(not(a.a_type) or a.a_type == "" or a.a_type == "none") then
return "(nothing): Nothing to do. No action."
elseif(a.a_type == "trade") then
return "trade:" -- TODO show ation text
elseif(a.a_type == "npc_gives") then
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 "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 "Q: \""..tostring(a.a_question).."\" A:\""..tostring(a.a_value).."\"."
elseif(a.a_type == "puzzle") then
return "puzzle:" -- TODO show ation text
elseif(a.a_type == "custom") then
return "custom:" -- TODO show ation text
end
-- fallback
return tostring(a.a_value)
end
yl_speak_up.execute_all_actions = function(player, actions, o_id)
-- TODO: implement
end
yl_speak_up.execute_action = function(player, n_id, o_id, r)
-- TODO: implement. boils down to showing formspecs
-- fallback: unkown type
return false
end
-- these are only wrapper functions for those in fs_edit_general.lua
yl_speak_up.input_fs_edit_actions = function(player, formname, fields)
return yl_speak_up.input_fs_edit_option_related(player, formname, fields,
"a_", "actions", yl_speak_up.max_actions,
"(A)ctions", "tmp_action",
nil, -- unused - no block operations
values_what, {}, {}, {}, {},
check_what, {}, {}, {}, {},
nil, -- no variables
"edit_actions"
)
end
yl_speak_up.get_fs_edit_actions = function(player, table_click_result)
return yl_speak_up.get_fs_edit_option_related(player, table_click_result,
"a_", "actions", yl_speak_up.max_actions,
"(A)ctions", "tmp_action",
"What do you want to happen in this (A)ction?",
check_what, {}, {}, {}, {},
nil, -- no variables
yl_speak_up.show_action,
"table_of_elements",
nil, nil, nil, -- no variable handling here
nil -- nothing block-related to do here
)
end