moved formspecs out of exec_actions.lua and into fs/fs_action_*.lua

This commit is contained in:
Sokomine 2024-02-10 00:23:35 +01:00
parent a7d6d07785
commit cb0869f53c
5 changed files with 368 additions and 367 deletions

View File

@ -471,370 +471,4 @@ end
-- show the diffrent action-related formspecs and handle input to them
-- (Note: trade is handled in trade_simple.lua)
yl_speak_up.input_fs_action_npc_gives = function(player, formname, fields)
-- back from error_msg? then show the formspec again
if(fields.back_from_error_msg) then
-- do not create a new item
yl_speak_up.show_fs(player, "action_npc_gives", nil)
return
end
local pname = player:get_player_name()
local trade_inv = minetest.get_inventory({type="detached", name="yl_speak_up_player_"..pname})
if(not(yl_speak_up.speak_to[pname])) then
return
end
local a_id = yl_speak_up.speak_to[pname].a_id
if(fields.npc_does_not_have_item) then
-- the NPC can't supply the item - abort the action
yl_speak_up.execute_next_action(player, a_id, nil, formname)
return
end
-- is the npc_gives inv empty? then all went as expected.
-- (it does not really matter which button the player pressed in this case)
if(trade_inv:is_empty("npc_gives")) then
-- the NPC has given the item to the player; save the NPCs inventory
local n_id = yl_speak_up.speak_to[pname].n_id
yl_speak_up.save_npc_inventory(n_id)
-- the action was a success; the NPC managed to give the item to the player
yl_speak_up.execute_next_action(player, a_id, true, formname)
return
end
-- the npc_gives slot does not accept input - so we don't have to check for any misplaced items
-- but if the player aborts, give the item back to the NPC
if(fields.back_to_talk) then
-- strip the quest item info from the stack (so that it may stack again)
-- and give that (hopefully) stackable stack back to the NPC
yl_speak_up.action_quest_item_take_back(player)
-- the action failed
yl_speak_up.execute_next_action(player, a_id, nil, formname)
return
end
-- else show a message to the player that he ought to take the item
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:action_npc_gives",
formspec = "size[7,1.5]"..
"label[0.2,-0.2;"..
"Please take the offered item and click on \"Done\"!\n"..
"If you can't take it, click on \"Back to talk\".]"..
"button[2,1.0;1.5,0.9;back_from_error_msg;Back]"})
end
yl_speak_up.get_fs_action_npc_gives = function(player, param)
-- called for the first time; create the item the NPC wants to give
if(param) then
if(not(yl_speak_up.action_quest_item_prepare(player))) then
local pname = player:get_player_name()
local dialog = yl_speak_up.speak_to[pname].dialog
-- it's not the fault of the player that the NPC doesn't have the item;
-- so tell him that (the action will still fail)
return "size[7,2.0]"..
"label[0.2,-0.2;"..
minetest.formspec_escape(dialog.n_npc or "- ? -")..
" is very sorry:\n"..
"The item intended for you is currently unavailable.\n"..
"Please come back later!]"..
"button[2,1.5;1.5,0.9;npc_does_not_have_item;Back]"
end
end
local pname = player:get_player_name()
local dialog = yl_speak_up.speak_to[pname].dialog
return "size[8.5,8]"..
"list[current_player;main;0.2,3.85;8,1;]"..
"list[current_player;main;0.2,5.08;8,3;8]"..
"button[0.2,0.0;2.0,0.9;back_to_talk;Back to talk]"..
"button[4.75,1.6;1.5,0.9;finished_action;Done]"..
"tooltip[back_to_talk;Click here if you don't want to (or can't)\n"..
"take the offered item.]"..
"tooltip[finished_action;Click here once you have taken the item and\n"..
"stored it in your inventory.]"..
"label[1.5,0.7;"..minetest.formspec_escape(dialog.n_npc or "- ? -")..
" offers to you:]"..
-- unlike the npc_gives slot - which is used for setting up the NPC - the
-- npc_gives slot does not allow putting something in
"list[detached:yl_speak_up_player_"..pname..";npc_gives;3.25,1.5;1,1;]" ..
"label[1.5,2.7;Take the offered item and click on \"Done\" to proceed.]"
end
yl_speak_up.input_fs_action_npc_wants = function(player, formname, fields)
-- back from error_msg? then show the formspec again
if(fields.back_from_error_msg) then
yl_speak_up.show_fs(player, "action_npc_wants", nil)
return
end
local pname = player:get_player_name()
if(not(pname) or not(yl_speak_up.speak_to[pname])) then
return
end
local trade_inv = minetest.get_inventory({type="detached", name="yl_speak_up_player_"..pname})
local a_id = yl_speak_up.speak_to[pname].a_id
-- is the npc_wants inv empty and the player pressed the back to talk button? then the action failed.
if(trade_inv:is_empty("npc_wants") and fields.back_to_talk) then
-- the action was aborted
yl_speak_up.execute_next_action(player, a_id, nil, formname)
return
end
-- the player tried to give something; check if it is the right thing
if(not(trade_inv:is_empty("npc_wants"))) then
local stack = trade_inv:get_stack("npc_wants", 1)
-- check if it really is the item the NPC wanted; let the NPC take it
local is_correct_item = yl_speak_up.action_quest_item_take_back(player)
-- the action may have been a success or failure
yl_speak_up.execute_next_action(player, a_id, is_correct_item, formname)
return
end
-- else show a message to the player
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:action_npc_wants",
formspec = "size[7,1.5]"..
"label[0.2,-0.2;"..
"Please insert the item for the npc and click on \"Done\"!\n"..
"If you don't have what he wants, click on \"Back to talk\".]"..
"button[2,1.0;1.5,0.9;back_from_error_msg;Back]"})
end
yl_speak_up.get_fs_action_npc_wants = function(player, param)
local pname = player:get_player_name()
local dialog = yl_speak_up.speak_to[pname].dialog
return "size[8.5,8]"..
"list[current_player;main;0.2,3.85;8,1;]"..
"list[current_player;main;0.2,5.08;8,3;8]"..
"button[0.2,0.0;2.0,0.9;back_to_talk;Back to talk]"..
"button[4.75,1.6;1.5,0.9;finished_action;Done]"..
"tooltip[back_to_talk;Click here if you don't know what item the\n"..
"NPC wants or don't have the desired item.]"..
"tooltip[finished_action;Click here once you have placed the item in\n"..
"the waiting slot.]"..
"label[1.5,0.7;"..minetest.formspec_escape(dialog.n_npc or "- ? -")..
" expects something from you:]"..
"list[detached:yl_speak_up_player_"..pname..";npc_wants;3.25,1.5;1,1;]" ..
"label[1.5,2.7;Insert the right item and click on \"Done\" to proceed.]"
end
yl_speak_up.input_fs_action_text_input = function(player, formname, fields)
-- back from error_msg? then show the formspec again
if(fields.back_from_error_msg) then
-- the error message is only shown if the input was empty
yl_speak_up.show_fs(player, "action_text_input", "")
return
end
local pname = player:get_player_name()
-- the player is no longer talking to the NPC
if(not(pname) or not(yl_speak_up.speak_to[pname])) then
return
end
local a_id = yl_speak_up.speak_to[pname].a_id
local a = yl_speak_up.get_action_by_player(player)
if(fields.back_to_talk) then
-- the action was aborted
yl_speak_up.execute_next_action(player, a_id, nil, formname)
return
end
if(fields.finished_action and fields.quest_answer and fields.quest_answer ~= "") then
local n_id = yl_speak_up.speak_to[pname].n_id
-- is the answer correct?
-- strip leading and tailing blanks
local success = not(not(fields.quest_answer and a.a_value
and fields.quest_answer:trim() == a.a_value:trim()))
if(not(success)) then
yl_speak_up.log_change(pname, n_id,
"Action "..tostring(a_id)..
" "..tostring(yl_speak_up.speak_to[pname].o_id)..
" "..tostring(yl_speak_up.speak_to[pname].d_id)..
": Player answered with \""..tostring(fields.quest_answer:trim())..
"\", but we expected: \""..tostring(a.a_value:trim()).."\".")
else
yl_speak_up.log_change(pname, n_id,
"Action "..tostring(a_id)..
" "..tostring(yl_speak_up.speak_to[pname].o_id)..
" "..tostring(yl_speak_up.speak_to[pname].d_id)..
": Answer is correct.")
end
-- store what the player entered so that it can be examined by other functions
yl_speak_up.last_text_input[pname] = fields.quest_answer:trim()
-- the action was a either a success or failure
yl_speak_up.execute_next_action(player, a_id, success, formname)
return
end
-- no scrolling desired
fields.button_up = nil
fields.button_down = nil
--[[ this is too disruptive; it's better to just let the player select a button
-- else show a message to the player
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:action_text_input",
formspec = "size[7,1.5]"..
"label[0.2,-0.2;"..
"Please answer the question and click on \"Send answer\"!\n"..
"If you don't know the answer, click on \"Back to talk\".]"..
"button[2,1.0;1.5,0.9;back_from_error_msg;Back]"})
--]]
end
yl_speak_up.get_fs_action_text_input = function(player, param)
local pname = player:get_player_name()
local dialog = yl_speak_up.speak_to[pname].dialog
local a = yl_speak_up.get_action_by_player(player)
if(not(a)) then
return ""
end
local alternate_text =
(a.a_question or "Your answer:").."\n\n"..
(dialog.n_npc or "- ? -").." looks expectantly at you.]"
local formspec = {}
table.insert(formspec, "label[0.7,1.8;Answer:]")
table.insert(formspec, "button[45,1.0;9,1.8;finished_action;Send this answer]")
-- show the actual text for the option
yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
"field", "4.0,1.0;40,1.5",
"quest_answer",
";", --..minetest.formspec_escape("<your answer>"),
"Enter your answer here.",
true)
local h = 2.0
local pname_for_old_fs = nil
h = yl_speak_up.add_edit_button_fs_talkdialog(formspec, h,
"finished_action",
"Please enter your answer in the input field above and click here to "..
"send it.",
minetest.formspec_escape("[Send this answer]"),
true, nil, nil, pname_for_old_fs)
h = yl_speak_up.add_edit_button_fs_talkdialog(formspec, h,
"back_to_talk",
"If you don't know the answer or don't want to answer right now, "..
"choose this option to get back to the previous dialog.",
"I give up. Let's talk about something diffrent.",
true, nil, nil, pname_for_old_fs)
-- do not offer edit_mode in the trade formspec because it makes no sense there;
return yl_speak_up.show_fs_decorated(pname, nil, h, alternate_text, "",
table.concat(formspec, "\n"), nil, h)
--[[ old version with extra formspec
return --"size[12.0,4.5]"..
yl_speak_up.show_fs_simple_deco(12.0, 4.5)..
"button[0.2,0.0;2.0,0.9;back_to_talk;Back to talk]"..
"button[2.0,3.7;3.0,0.9;finished_action;Send answer]"..
"tooltip[back_to_talk;Click here if you don't know the answer.]"..
"tooltip[finished_action;Click here once you've entered the answer.]"..
"label[0.2,1.2;"..minetest.formspec_escape(a.a_question or "Your answer:").."]"..
"label[0.2,1.9;Answer:]"..
"field[1.6,2.2;10.0,0.6;quest_answer;;"..tostring(param or "").."]"..
"label[0.2,2.8;"..minetest.formspec_escape(
"["..(dialog.n_npc or "- ? -").." looks expectantly at you.]").."]"
--]]
end
-- action of the type "evaluate"
yl_speak_up.input_fs_action_evaluate = function(player, formname, fields)
local pname = player:get_player_name()
local a_id = yl_speak_up.speak_to[pname].a_id
-- the custom input_handler may have something to say here as well
local a = yl_speak_up.get_action_by_player(player)
if(player and a and a.a_value) then
local custom_data = yl_speak_up.custom_functions_a_[a.a_value]
if(custom_data and custom_data.code_input_handler) then
local n_id = yl_speak_up.speak_to[pname].n_id
local fun = custom_data.code_input_handler
-- actually call the function (which may change the value of fields)
fields = fun(player, n_id, a, formname, fields)
end
end
-- back from error_msg? then show the formspec again
if(fields.back_from_error_msg) then
yl_speak_up.show_fs(player, "action_evaluate", nil)
return
end
if(fields.back_to_talk) then
-- the action was aborted
yl_speak_up.execute_next_action(player, a_id, nil, formame)
return
end
if(fields.failed_action) then
-- the action failed
yl_speak_up.execute_next_action(player, a_id, false, formame)
return
end
if(fields.finished_action) then
-- the action was a success
yl_speak_up.execute_next_action(player, a_id, true, formame)
return
end
if(fields.quit) then
return
end
-- else show a message to the player that he ought to decide
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:action_evaluate",
formspec = "size[7,1.5]"..
"label[0.2,-0.2;"..
"Please click on one of the offered options\nor select \"Back to talk\"!]"..
"button[2,1.0;1.5,0.9;back_from_error_msg;Back]"})
end
yl_speak_up.get_fs_action_evaluate = function(player, param)
local pname = player:get_player_name()
local dialog = yl_speak_up.speak_to[pname].dialog
local n_id = yl_speak_up.speak_to[pname].n_id
local a = yl_speak_up.get_action_by_player(player)
if(not(a)) then
return ""
end
if(not(player) or not(a.a_value)) then
return "label[0.2,0.5;Ups! An internal error occoured. Please tell your "..
"local admin to check the brain of this lifeform here.]"..
"button[1.5,1.5;2,0.9;back_to_talk;Back]"
end
local custom_data = yl_speak_up.custom_functions_a_[a.a_value]
if(not(custom_data) or not(custom_data.code)) then
return "label[0.2,0.5;Ups! An internal error occoured. Please tell your "..
"local admin that the internal function "..
minetest.formspec_escape(tostring(a.a_value))..
"somehow got lost/broken.]"..
"button[1.5,1.5;2,0.9;back_to_talk;Back]"
end
local fun = custom_data.code
-- actually call the function
return fun(player, n_id, a)
end
yl_speak_up.register_fs("action_npc_gives",
yl_speak_up.input_action_npc_gives,
yl_speak_up.get_fs_action_npc_gives,
-- force formspec version 1 for this:
1
)
yl_speak_up.register_fs("action_npc_wants",
yl_speak_up.input_action_npc_wants,
yl_speak_up.get_fs_action_npc_wants,
-- force formspec version 1 for this:
1
)
yl_speak_up.register_fs("action_text_input",
yl_speak_up.input_action_text_input,
yl_speak_up.get_fs_action_text_input,
-- no special formspec version required:
nil
)
yl_speak_up.register_fs("action_evaluate",
yl_speak_up.input_action_evaluate,
yl_speak_up.get_fs_action_evaluate,
-- no special formspec version required:
nil
)
-- -> now moved to fs/fs_action_*.lua

82
fs/fs_action_evaluate.lua Normal file
View File

@ -0,0 +1,82 @@
-- action of the type "evaluate"
yl_speak_up.input_fs_action_evaluate = function(player, formname, fields)
local pname = player:get_player_name()
local a_id = yl_speak_up.speak_to[pname].a_id
-- the custom input_handler may have something to say here as well
local a = yl_speak_up.get_action_by_player(player)
if(player and a and a.a_value) then
local custom_data = yl_speak_up.custom_functions_a_[a.a_value]
if(custom_data and custom_data.code_input_handler) then
local n_id = yl_speak_up.speak_to[pname].n_id
local fun = custom_data.code_input_handler
-- actually call the function (which may change the value of fields)
fields = fun(player, n_id, a, formname, fields)
end
end
-- back from error_msg? then show the formspec again
if(fields.back_from_error_msg) then
yl_speak_up.show_fs(player, "action_evaluate", nil)
return
end
if(fields.back_to_talk) then
-- the action was aborted
yl_speak_up.execute_next_action(player, a_id, nil, formame)
return
end
if(fields.failed_action) then
-- the action failed
yl_speak_up.execute_next_action(player, a_id, false, formame)
return
end
if(fields.finished_action) then
-- the action was a success
yl_speak_up.execute_next_action(player, a_id, true, formame)
return
end
if(fields.quit) then
return
end
-- else show a message to the player that he ought to decide
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:action_evaluate",
formspec = "size[7,1.5]"..
"label[0.2,-0.2;"..
"Please click on one of the offered options\nor select \"Back to talk\"!]"..
"button[2,1.0;1.5,0.9;back_from_error_msg;Back]"})
end
yl_speak_up.get_fs_action_evaluate = function(player, param)
local pname = player:get_player_name()
local dialog = yl_speak_up.speak_to[pname].dialog
local n_id = yl_speak_up.speak_to[pname].n_id
local a = yl_speak_up.get_action_by_player(player)
if(not(a)) then
return ""
end
if(not(player) or not(a.a_value)) then
return "label[0.2,0.5;Ups! An internal error occoured. Please tell your "..
"local admin to check the brain of this lifeform here.]"..
"button[1.5,1.5;2,0.9;back_to_talk;Back]"
end
local custom_data = yl_speak_up.custom_functions_a_[a.a_value]
if(not(custom_data) or not(custom_data.code)) then
return "label[0.2,0.5;Ups! An internal error occoured. Please tell your "..
"local admin that the internal function "..
minetest.formspec_escape(tostring(a.a_value))..
"somehow got lost/broken.]"..
"button[1.5,1.5;2,0.9;back_to_talk;Back]"
end
local fun = custom_data.code
-- actually call the function
return fun(player, n_id, a)
end
yl_speak_up.register_fs("action_evaluate",
yl_speak_up.input_fs_action_evaluate,
yl_speak_up.get_fs_action_evaluate,
-- no special formspec version required:
nil
)

View File

@ -0,0 +1,97 @@
-- show the diffrent action-related formspecs and handle input to them
-- (Note: trade is handled in trade_simple.lua)
yl_speak_up.input_fs_action_npc_gives = function(player, formname, fields)
-- back from error_msg? then show the formspec again
if(fields.back_from_error_msg) then
-- do not create a new item
yl_speak_up.show_fs(player, "action_npc_gives", nil)
return
end
local pname = player:get_player_name()
local trade_inv = minetest.get_inventory({type="detached", name="yl_speak_up_player_"..pname})
if(not(yl_speak_up.speak_to[pname])) then
return
end
local a_id = yl_speak_up.speak_to[pname].a_id
if(fields.npc_does_not_have_item) then
-- the NPC can't supply the item - abort the action
yl_speak_up.execute_next_action(player, a_id, nil, formname)
return
end
-- is the npc_gives inv empty? then all went as expected.
-- (it does not really matter which button the player pressed in this case)
if(trade_inv:is_empty("npc_gives")) then
-- the NPC has given the item to the player; save the NPCs inventory
local n_id = yl_speak_up.speak_to[pname].n_id
yl_speak_up.save_npc_inventory(n_id)
-- the action was a success; the NPC managed to give the item to the player
yl_speak_up.execute_next_action(player, a_id, true, formname)
return
end
-- the npc_gives slot does not accept input - so we don't have to check for any misplaced items
-- but if the player aborts, give the item back to the NPC
if(fields.back_to_talk) then
-- strip the quest item info from the stack (so that it may stack again)
-- and give that (hopefully) stackable stack back to the NPC
yl_speak_up.action_quest_item_take_back(player)
-- the action failed
yl_speak_up.execute_next_action(player, a_id, nil, formname)
return
end
-- else show a message to the player that he ought to take the item
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:action_npc_gives",
formspec = "size[7,1.5]"..
"label[0.2,-0.2;"..
"Please take the offered item and click on \"Done\"!\n"..
"If you can't take it, click on \"Back to talk\".]"..
"button[2,1.0;1.5,0.9;back_from_error_msg;Back]"})
end
yl_speak_up.get_fs_action_npc_gives = function(player, param)
-- called for the first time; create the item the NPC wants to give
if(param) then
if(not(yl_speak_up.action_quest_item_prepare(player))) then
local pname = player:get_player_name()
local dialog = yl_speak_up.speak_to[pname].dialog
-- it's not the fault of the player that the NPC doesn't have the item;
-- so tell him that (the action will still fail)
return "size[7,2.0]"..
"label[0.2,-0.2;"..
minetest.formspec_escape(dialog.n_npc or "- ? -")..
" is very sorry:\n"..
"The item intended for you is currently unavailable.\n"..
"Please come back later!]"..
"button[2,1.5;1.5,0.9;npc_does_not_have_item;Back]"
end
end
local pname = player:get_player_name()
local dialog = yl_speak_up.speak_to[pname].dialog
return "size[8.5,8]"..
"list[current_player;main;0.2,3.85;8,1;]"..
"list[current_player;main;0.2,5.08;8,3;8]"..
"button[0.2,0.0;2.0,0.9;back_to_talk;Back to talk]"..
"button[4.75,1.6;1.5,0.9;finished_action;Done]"..
"tooltip[back_to_talk;Click here if you don't want to (or can't)\n"..
"take the offered item.]"..
"tooltip[finished_action;Click here once you have taken the item and\n"..
"stored it in your inventory.]"..
"label[1.5,0.7;"..minetest.formspec_escape(dialog.n_npc or "- ? -")..
" offers to you:]"..
-- unlike the npc_gives slot - which is used for setting up the NPC - the
-- npc_gives slot does not allow putting something in
"list[detached:yl_speak_up_player_"..pname..";npc_gives;3.25,1.5;1,1;]" ..
"label[1.5,2.7;Take the offered item and click on \"Done\" to proceed.]"
end
yl_speak_up.register_fs("action_npc_gives",
yl_speak_up.input_fs_action_npc_gives,
yl_speak_up.get_fs_action_npc_gives,
-- force formspec version 1 for this:
1
)

View File

@ -0,0 +1,63 @@
yl_speak_up.input_fs_action_npc_wants = function(player, formname, fields)
-- back from error_msg? then show the formspec again
if(fields.back_from_error_msg) then
yl_speak_up.show_fs(player, "action_npc_wants", nil)
return
end
local pname = player:get_player_name()
if(not(pname) or not(yl_speak_up.speak_to[pname])) then
return
end
local trade_inv = minetest.get_inventory({type="detached", name="yl_speak_up_player_"..pname})
local a_id = yl_speak_up.speak_to[pname].a_id
-- is the npc_wants inv empty and the player pressed the back to talk button? then the action failed.
if(trade_inv:is_empty("npc_wants") and fields.back_to_talk) then
-- the action was aborted
yl_speak_up.execute_next_action(player, a_id, nil, formname)
return
end
-- the player tried to give something; check if it is the right thing
if(not(trade_inv:is_empty("npc_wants"))) then
local stack = trade_inv:get_stack("npc_wants", 1)
-- check if it really is the item the NPC wanted; let the NPC take it
local is_correct_item = yl_speak_up.action_quest_item_take_back(player)
-- the action may have been a success or failure
yl_speak_up.execute_next_action(player, a_id, is_correct_item, formname)
return
end
-- else show a message to the player
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:action_npc_wants",
formspec = "size[7,1.5]"..
"label[0.2,-0.2;"..
"Please insert the item for the npc and click on \"Done\"!\n"..
"If you don't have what he wants, click on \"Back to talk\".]"..
"button[2,1.0;1.5,0.9;back_from_error_msg;Back]"})
end
yl_speak_up.get_fs_action_npc_wants = function(player, param)
local pname = player:get_player_name()
local dialog = yl_speak_up.speak_to[pname].dialog
return "size[8.5,8]"..
"list[current_player;main;0.2,3.85;8,1;]"..
"list[current_player;main;0.2,5.08;8,3;8]"..
"button[0.2,0.0;2.0,0.9;back_to_talk;Back to talk]"..
"button[4.75,1.6;1.5,0.9;finished_action;Done]"..
"tooltip[back_to_talk;Click here if you don't know what item the\n"..
"NPC wants or don't have the desired item.]"..
"tooltip[finished_action;Click here once you have placed the item in\n"..
"the waiting slot.]"..
"label[1.5,0.7;"..minetest.formspec_escape(dialog.n_npc or "- ? -")..
" expects something from you:]"..
"list[detached:yl_speak_up_player_"..pname..";npc_wants;3.25,1.5;1,1;]" ..
"label[1.5,2.7;Insert the right item and click on \"Done\" to proceed.]"
end
yl_speak_up.register_fs("action_npc_wants",
yl_speak_up.input_fs_action_npc_wants,
yl_speak_up.get_fs_action_npc_wants,
-- force formspec version 1 for this:
1
)

125
fs/fs_action_text_input.lua Normal file
View File

@ -0,0 +1,125 @@
yl_speak_up.input_fs_action_text_input = function(player, formname, fields)
-- back from error_msg? then show the formspec again
if(fields.back_from_error_msg) then
-- the error message is only shown if the input was empty
yl_speak_up.show_fs(player, "action_text_input", "")
return
end
local pname = player:get_player_name()
-- the player is no longer talking to the NPC
if(not(pname) or not(yl_speak_up.speak_to[pname])) then
return
end
local a_id = yl_speak_up.speak_to[pname].a_id
local a = yl_speak_up.get_action_by_player(player)
if(fields.back_to_talk) then
-- the action was aborted
yl_speak_up.execute_next_action(player, a_id, nil, formname)
return
end
if(fields.finished_action and fields.quest_answer and fields.quest_answer ~= "") then
local n_id = yl_speak_up.speak_to[pname].n_id
-- is the answer correct?
-- strip leading and tailing blanks
local success = not(not(fields.quest_answer and a.a_value
and fields.quest_answer:trim() == a.a_value:trim()))
if(not(success)) then
yl_speak_up.log_change(pname, n_id,
"Action "..tostring(a_id)..
" "..tostring(yl_speak_up.speak_to[pname].o_id)..
" "..tostring(yl_speak_up.speak_to[pname].d_id)..
": Player answered with \""..tostring(fields.quest_answer:trim())..
"\", but we expected: \""..tostring(a.a_value:trim()).."\".")
else
yl_speak_up.log_change(pname, n_id,
"Action "..tostring(a_id)..
" "..tostring(yl_speak_up.speak_to[pname].o_id)..
" "..tostring(yl_speak_up.speak_to[pname].d_id)..
": Answer is correct.")
end
-- store what the player entered so that it can be examined by other functions
yl_speak_up.last_text_input[pname] = fields.quest_answer:trim()
-- the action was a either a success or failure
yl_speak_up.execute_next_action(player, a_id, success, formname)
return
end
-- no scrolling desired
fields.button_up = nil
fields.button_down = nil
--[[ this is too disruptive; it's better to just let the player select a button
-- else show a message to the player
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:action_text_input",
formspec = "size[7,1.5]"..
"label[0.2,-0.2;"..
"Please answer the question and click on \"Send answer\"!\n"..
"If you don't know the answer, click on \"Back to talk\".]"..
"button[2,1.0;1.5,0.9;back_from_error_msg;Back]"})
--]]
end
yl_speak_up.get_fs_action_text_input = function(player, param)
local pname = player:get_player_name()
local dialog = yl_speak_up.speak_to[pname].dialog
local a = yl_speak_up.get_action_by_player(player)
if(not(a)) then
return ""
end
local alternate_text =
(a.a_question or "Your answer:").."\n\n"..
(dialog.n_npc or "- ? -").." looks expectantly at you.]"
local formspec = {}
table.insert(formspec, "label[0.7,1.8;Answer:]")
table.insert(formspec, "button[45,1.0;9,1.8;finished_action;Send this answer]")
-- show the actual text for the option
yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
"field", "4.0,1.0;40,1.5",
"quest_answer",
";", --..minetest.formspec_escape("<your answer>"),
"Enter your answer here.",
true)
local h = 2.0
local pname_for_old_fs = nil
h = yl_speak_up.add_edit_button_fs_talkdialog(formspec, h,
"finished_action",
"Please enter your answer in the input field above and click here to "..
"send it.",
minetest.formspec_escape("[Send this answer]"),
true, nil, nil, pname_for_old_fs)
h = yl_speak_up.add_edit_button_fs_talkdialog(formspec, h,
"back_to_talk",
"If you don't know the answer or don't want to answer right now, "..
"choose this option to get back to the previous dialog.",
"I give up. Let's talk about something diffrent.",
true, nil, nil, pname_for_old_fs)
-- do not offer edit_mode in the trade formspec because it makes no sense there;
return yl_speak_up.show_fs_decorated(pname, nil, h, alternate_text, "",
table.concat(formspec, "\n"), nil, h)
--[[ old version with extra formspec
return --"size[12.0,4.5]"..
yl_speak_up.show_fs_simple_deco(12.0, 4.5)..
"button[0.2,0.0;2.0,0.9;back_to_talk;Back to talk]"..
"button[2.0,3.7;3.0,0.9;finished_action;Send answer]"..
"tooltip[back_to_talk;Click here if you don't know the answer.]"..
"tooltip[finished_action;Click here once you've entered the answer.]"..
"label[0.2,1.2;"..minetest.formspec_escape(a.a_question or "Your answer:").."]"..
"label[0.2,1.9;Answer:]"..
"field[1.6,2.2;10.0,0.6;quest_answer;;"..tostring(param or "").."]"..
"label[0.2,2.8;"..minetest.formspec_escape(
"["..(dialog.n_npc or "- ? -").." looks expectantly at you.]").."]"
--]]
end
yl_speak_up.register_fs("action_text_input",
yl_speak_up.input_fs_action_text_input,
yl_speak_up.get_fs_action_text_input,
-- no special formspec version required:
nil
)