moved variable presets into individual functions

This commit is contained in:
Sokomine 2021-07-02 03:24:45 +02:00
parent 73ac297b98
commit 506db0ea67

View File

@ -1076,11 +1076,12 @@ yl_speak_up.get_fs_edit_option_related = function(player, table_click_result,
x_id = "new"
end
local e = nil
-- does the element exist already? if so: use the existing values as presets for data
-- (so that the element can be edited)
-- does kind of the opposite than the saving of values starting in line 323 of this file
if(x_id ~= "new" and data.what == 1 and elements[ x_id ]) then
local e = elements[ x_id ]
e = elements[ x_id ]
if( id_prefix == "r_" and e[ "r_type" ] == "dialog") then
-- dialog effects cannot be edited this way
return "size[9,2]"..
@ -1099,56 +1100,9 @@ yl_speak_up.get_fs_edit_option_related = function(player, table_click_result,
data.what = table.indexof(values_what, e[ id_prefix.."type" ])
if(data.what == -1) then
data.what = 1
-- a state/variable (precondition + effect)
elseif(data.what == 2 and id_prefix ~= "a_") then
data.operator = math.max(1,table.indexof(values_operator, e[ id_prefix.."operator" ]))
data.var_cmp_value = e[ id_prefix.."var_cmp_value" ]
data.variable_name = e[ id_prefix.."variable" ]
local var_list = get_sorted_player_var_list_function(pname)
data.variable = math.max(1, table.indexof(var_list, e[ id_prefix.."variable"])+1)
-- a block (precondition + effect)
elseif(data.what == 3 and id_prefix ~= "a_") then
data.block = math.max(1,table.indexof(values_block, e[ id_prefix.."value" ]))
data.node_data = {}
data.node_data.data = e[ id_prefix.."node" ]
data.node_data.param2 = e[ id_prefix.."param2" ]
data.block_pos = {x=e[ id_prefix.."pos" ].x,
y=e[ id_prefix.."pos" ].y,
z=e[ id_prefix.."pos" ].z}
-- the block below was punched
if(id_prefix == "p_" and data.block == 5) then
data.block_pos.y = data.block_pos.y - 1
end
-- a trade (precondition)
elseif(data.what == 4 and id_prefix == "p_") then
data.trade = math.max(1,table.indexof(values_trade, e[ "p_value" ]))
-- an invenetory (precondition)
elseif((data.what == 5 or data.what == 6) and id_prefix == "p_") then
data.inv = math.max(1,table.indexof(values_inv, e["p_value"]))
data.inv_stack_name = e[ "p_itemstack" ]
-- crafting (effect)
elseif(data.what and id_prefix == "r_" and data.what == 4) then
-- those items can at least be shown as background images
data.craftresult = e[ "r_value" ]
data.craft_grid = e[ "r_craft_grid"]
-- on failure (effect)
elseif(data.what and id_prefix == "r_" and data.what == 5) then
data.on_failure = e[ "r_value" ]
-- send a chat message (effect)
elseif(data.what and id_prefix == "r_" and data.what == 6) then
data.chat_msg_text = e[ "r_value" ]
-- normal trade (action)
elseif(data.what and id_prefix == "a_" and data.what == 3) then
data.trade_id = e[ "a_value" ]
-- use as background images
if(dialog and dialog.trades and dialog.trades[ data.trade_id ]) then
data.pay = dialog.trades[ data.trade_id ].pay[1]
data.buy = dialog.trades[ data.trade_id ].buy[1]
end
local sorted_dialog_list = yl_speak_up.sort_keys(dialog.n_dialogs)
data.action_failure_dialog = math.max(1,
table.indexof(sorted_dialog_list, e[ "a_on_failure" ]))
-- npc_gives/npc_wants (action)
-- (two seperate functions, but can be handled here together)
elseif(data.what and id_prefix == "a_" and (data.what == 4 or data.what == 5)) then
local sorted_dialog_list = yl_speak_up.sort_keys(dialog.n_dialogs)
data.action_failure_dialog = math.max(1,
@ -1160,47 +1114,7 @@ yl_speak_up.get_fs_edit_option_related = function(player, table_click_result,
local sorted_dialog_list = yl_speak_up.sort_keys(dialog.n_dialogs)
data.action_failure_dialog = math.max(1,
table.indexof(sorted_dialog_list, e[ "a_on_failure" ]))
-- entering password (action)
elseif(data.what and id_prefix == "a_" and data.what == 6) then
data.quest_question = e[ "a_question" ]
data.quest_answer = e[ "a_value" ]
local sorted_dialog_list = yl_speak_up.sort_keys(dialog.n_dialogs)
data.action_failure_dialog = math.max(1,
table.indexof(sorted_dialog_list, e[ "a_on_failure" ]))
-- custom function (action)
elseif(data.what
and ((id_prefix == "a_" and data.what == 7)
or (id_prefix == "p_" and data.what == 8)
or (id_prefix == "r_" and data.what == 11))) then
data.custom_param = e[ id_prefix.."value" ]
if(id_prefix == "a_") then
local sorted_dialog_list = yl_speak_up.sort_keys(dialog.n_dialogs)
data.action_failure_dialog = math.max(1,
table.indexof(sorted_dialog_list, e[ "a_on_failure" ]))
end
-- give_item/take_item
elseif(data.what and id_prefix == "r_" and data.what >= 7 and data.what <= 8) then
data.inv_stack_name = e[ "r_value" ] or ""
-- move
elseif(data.what and id_prefix == "r_" and data.what == 9) then
if(e[ "r_value"] and type(e[ "r_value" ]) == "string") then
local pos = minetest.string_to_pos(e[ "r_value" ])
if(pos) then
data.move_to_x = pos.x
data.move_to_y = pos.y
data.move_to_z = pos.z
end
end
-- lua code (precondition and effect)
elseif((data.what and id_prefix == "p_" and data.what == 7)
or (data.what and id_prefix == "r_" and data.what == 10)) then
if(e[ id_prefix.."value"] and e[ id_prefix.."value"] ~= "") then
data.lua_code = e[ id_prefix.."value" ]
end
end
end
if(e[ "alternate_text"]) then
data.alternate_text = e[ "alternate_text" ]
@ -1235,84 +1149,84 @@ yl_speak_up.get_fs_edit_option_related = function(player, table_click_result,
-- "an internal state (i.e. of a quest)", -- 2
-- (state is the second offered option in both preconditions and effects list)
if(data.what and data.what == 2 and id_prefix ~= "a_") then
return yl_speak_up.get_fs_edit_option_precondition_state(
pname, dialog, formspec, data, id_prefix, save_button)
return yl_speak_up.get_fs_edit_option_p_and_e_state(
pname, dialog, formspec, data, id_prefix, save_button, e)
-- "a block somewhere", -- 3
-- (block is the third offered option in both preconditions and effects list)
elseif(data.what and data.what == 3 and id_prefix ~= "a_") then
return yl_speak_up.get_fs_edit_option_precondition_block(
pname, dialog, formspec, data, id_prefix, save_button)
return yl_speak_up.get_fs_edit_option_p_and_e_block(
pname, dialog, formspec, data, id_prefix, save_button, e)
-- "a trade", -- 4
-- (trade - only for preconditions; effects have something else here)
elseif(data.what and id_prefix == "p_" and data.what == 4) then
return yl_speak_up.get_fs_edit_option_precondition_trade(
pname, dialog, formspec, data, id_prefix, save_button)
pname, dialog, formspec, data, id_prefix, save_button, e)
-- "the inventory of the player", -- 5
-- "the inventory of the NPC", -- 6
-- (inventory - only for preconditions; effects have something else here)
elseif(data.what and id_prefix == "p_" and data.what >= 5 and data.what <= 6) then
return yl_speak_up.get_fs_edit_option_precondition_inv(
pname, dialog, formspec, data, id_prefix, save_button)
pname, dialog, formspec, data, id_prefix, save_button, e)
-- "give item (created out of thin air) to player (requires npc_master priv)", -- 7
-- "take item from player and destroy it (requires npc_master priv)", -- 8
elseif(data.what and id_prefix == "r_" and (data.what == 7 or data.what == 8)) then
return yl_speak_up.get_fs_edit_option_effect_give_item_or_take_item(
pname, dialog, formspec, data, id_prefix, save_button)
pname, dialog, formspec, data, id_prefix, save_button, e)
-- "move the player to a given position (requires npc_master priv)", -- 9
elseif(data.what and id_prefix == "r_" and data.what == 9) then
return yl_speak_up.get_fs_edit_option_effect_move(
pname, dialog, formspec, data, id_prefix, save_button)
pname, dialog, formspec, data, id_prefix, save_button, e)
-- "execute Lua code (requires npc_master priv)", -- precondition: 7; effect: 10
elseif((data.what and id_prefix == "p_" and data.what == 7)
or (data.what and id_prefix == "r_" and data.what == 10)) then
return yl_speak_up.get_fs_edit_option_p_and_e_function(
pname, dialog, formspec, data, id_prefix, save_button)
pname, dialog, formspec, data, id_prefix, save_button, e)
-- "NPC crafts something", -- 4
-- (craft - only for effects - not for preconditions)
elseif(data.what and id_prefix == "r_" and data.what == 4) then
return yl_speak_up.get_fs_edit_option_effect_craft(
pname, dialog, formspec, data, id_prefix, save_button)
pname, dialog, formspec, data, id_prefix, save_button, e)
-- "go to other dialog if the *previous* effect failed", -- 5
-- (on_failure - only for effects - not for preconditions)
elseif(data.what and id_prefix == "r_" and data.what == 5) then
return yl_speak_up.get_fs_edit_option_effect_on_failure(
pname, dialog, formspec, data, id_prefix, save_button)
pname, dialog, formspec, data, id_prefix, save_button, e)
-- "send a chat message to all players" -- 6
elseif(data.what and id_prefix == "r_" and data.what == 6) then
return yl_speak_up.get_fs_edit_option_effect_chat_all(
pname, dialog, formspec, data, id_prefix, save_button)
pname, dialog, formspec, data, id_prefix, save_button, e)
-- "Normal trade - one item(stack) for another item(stack).", -- 3
elseif(data.what and id_prefix == "a_" and data.what == 3) then
return yl_speak_up.get_fs_edit_option_action_trade(
pname, dialog, formspec, data, id_prefix, save_button)
pname, dialog, formspec, data, id_prefix, save_button, e)
-- "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
return yl_speak_up.get_fs_edit_option_action_npc_gives(
pname, dialog, formspec, data, id_prefix, save_button)
pname, dialog, formspec, data, id_prefix, save_button, e)
-- "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
return yl_speak_up.get_fs_edit_option_action_npc_wants(
pname, dialog, formspec, data, id_prefix, save_button)
pname, dialog, formspec, data, id_prefix, save_button, e)
-- "The player has to manually enter a password or passphrase or some other text.", -- 6
-- (only for actions)
elseif(data.what and id_prefix == "a_" and data.what == 6) then
return yl_speak_up.get_fs_edit_option_action_text_input(
pname, dialog, formspec, data, id_prefix, save_button)
pname, dialog, formspec, data, id_prefix, save_button, e)
-- "Call custom functions that are supposed to be overridden by the server.", -- 7
-- precondition: 8; action: 7; effect: 11
@ -1321,7 +1235,7 @@ yl_speak_up.get_fs_edit_option_related = function(player, table_click_result,
or (id_prefix == "p_" and data.what == 8)
or (id_prefix == "r_" and data.what == 11))) then
return yl_speak_up.get_fs_edit_option_action_custom(
pname, dialog, formspec, data, id_prefix, save_button)
pname, dialog, formspec, data, id_prefix, save_button, e)
end
return "ERROR"
@ -1331,8 +1245,15 @@ end
----------------------------------------------------------------------------
-- begin of formspecs for types of preconditions, actions and effects
-- TODO: text_variable, text_select_operator
yl_speak_up.get_fs_edit_option_precondition_state = function(pname, dialog, formspec, data, id_prefix, save_button)
-- TODO: text_variable, text_select_operator, values_operator, get_sorted_player_var_list_function
yl_speak_up.get_fs_edit_option_p_and_e_state = function(pname, dialog, formspec, data, id_prefix, save_button, e)
if(e) then
data.operator = math.max(1,table.indexof(values_operator, e[ id_prefix.."operator" ]))
data.var_cmp_value = e[ id_prefix.."var_cmp_value" ]
data.variable_name = e[ id_prefix.."variable" ]
local var_list = get_sorted_player_var_list_function(pname)
data.variable = math.max(1, table.indexof(var_list, e[ id_prefix.."variable"])+1)
end
if(not(data.variable) or data.variable < 1) then
data.variable = 1
-- not enough selected yet for saving
@ -1391,8 +1312,21 @@ yl_speak_up.get_fs_edit_option_precondition_state = function(pname, dialog, form
end
-- TODO: block_pos as parameter?
yl_speak_up.get_fs_edit_option_precondition_block = function(pname, dialog, formspec, data, id_prefix, save_button)
-- TODO: block_pos as parameter?, values_block
yl_speak_up.get_fs_edit_option_p_and_e_block = function(pname, dialog, formspec, data, id_prefix, save_button, e)
if(e) then
data.block = math.max(1,table.indexof(values_block, e[ id_prefix.."value" ]))
data.node_data = {}
data.node_data.data = e[ id_prefix.."node" ]
data.node_data.param2 = e[ id_prefix.."param2" ]
data.block_pos = {x=e[ id_prefix.."pos" ].x,
y=e[ id_prefix.."pos" ].y,
z=e[ id_prefix.."pos" ].z}
-- the block below was punched
if(id_prefix == "p_" and data.block == 5) then
data.block_pos.y = data.block_pos.y - 1
end
end
local block_pos_str = "- none set -"
local node = {name = "- unknown -", param2 = "- unkown -"}
if(not(block_pos) and data and data.block_pos) then
@ -1461,7 +1395,10 @@ yl_speak_up.get_fs_edit_option_precondition_block = function(pname, dialog, form
end
yl_speak_up.get_fs_edit_option_precondition_trade = function(pname, dialog, formspec, data, id_prefix, save_button)
yl_speak_up.get_fs_edit_option_precondition_trade = function(pname, dialog, formspec, data, id_prefix, save_button, e)
if(e) then
data.trade = math.max(1,table.indexof(values_trade, e[ "p_value" ]))
end
if(not(data.trade) or data.trade == 1) then
data.trade = 1
-- not enough selected yet for saving
@ -1476,7 +1413,11 @@ yl_speak_up.get_fs_edit_option_precondition_trade = function(pname, dialog, form
end
yl_speak_up.get_fs_edit_option_precondition_inv = function(pname, dialog, formspec, data, id_prefix, save_button)
yl_speak_up.get_fs_edit_option_precondition_inv = function(pname, dialog, formspec, data, id_prefix, save_button, e)
if(e) then
data.inv = math.max(1,table.indexof(values_inv, e["p_value"]))
data.inv_stack_name = e[ "p_itemstack" ]
end
if(not(data.inv) or data.inv == 1) then
data.inv = 1
-- not enough selected yet for saving
@ -1501,7 +1442,10 @@ yl_speak_up.get_fs_edit_option_precondition_inv = function(pname, dialog, formsp
end
yl_speak_up.get_fs_edit_option_effect_give_item_or_take_item = function(pname, dialog, formspec, data, id_prefix, save_button)
yl_speak_up.get_fs_edit_option_effect_give_item_or_take_item = function(pname, dialog, formspec, data, id_prefix, save_button, e)
if(e) then
data.inv_stack_name = e[ "r_value" ] or ""
end
local text = "The following item shall be created out of thin air and added to the "..
"player's inventory:"
if(data.what == 8) then
@ -1526,7 +1470,17 @@ yl_speak_up.get_fs_edit_option_effect_give_item_or_take_item = function(pname, d
end
yl_speak_up.get_fs_edit_option_effect_move = function(pname, dialog, formspec, data, id_prefix, save_button)
yl_speak_up.get_fs_edit_option_effect_move = function(pname, dialog, formspec, data, id_prefix, save_button, e)
if(e) then
if(e[ "r_value"] and type(e[ "r_value" ]) == "string") then
local pos = minetest.string_to_pos(e[ "r_value" ])
if(pos) then
data.move_to_x = pos.x
data.move_to_y = pos.y
data.move_to_z = pos.z
end
end
end
return
"label[0.2,3.0;Move the player to this position:]"..
"label[0.2,3.5;Note: You can *save* this effect only if you have the "..
@ -1542,7 +1496,12 @@ end
-- for "precondition and effect"
yl_speak_up.get_fs_edit_option_p_and_e_function = function(pname, dialog, formspec, data, id_prefix, save_button)
yl_speak_up.get_fs_edit_option_p_and_e_function = function(pname, dialog, formspec, data, id_prefix, save_button, e)
if(e) then
if(e[ id_prefix.."value"] and e[ id_prefix.."value"] ~= "") then
data.lua_code = e[ id_prefix.."value" ]
end
end
return
"label[0.2,3.0;Execute the following Lua code (ought to return true or false):]"..
"label[0.2,3.5;Note: You can *save* this effect only if you have the "..
@ -1553,7 +1512,12 @@ yl_speak_up.get_fs_edit_option_p_and_e_function = function(pname, dialog, formsp
end
yl_speak_up.get_fs_edit_option_effect_craft = function(pname, dialog, formspec, data, id_prefix, save_button)
yl_speak_up.get_fs_edit_option_effect_craft = function(pname, dialog, formspec, data, id_prefix, save_button, e)
if(e) then
-- those items can at least be shown as background images
data.craftresult = e[ "r_value" ]
data.craft_grid = e[ "r_craft_grid"]
end
local bg_img = ""
if(data and data.craftresult and data.craft_grid) then
bg_img = "item_image[5.95,4.90;0.7,0.7;"..tostring(data.craftresult).."]"
@ -1580,7 +1544,10 @@ yl_speak_up.get_fs_edit_option_effect_craft = function(pname, dialog, formspec,
end
yl_speak_up.get_fs_edit_option_effect_on_failure = function(pname, dialog, formspec, data, id_prefix, save_button)
yl_speak_up.get_fs_edit_option_effect_on_failure = function(pname, dialog, formspec, data, id_prefix, save_button, e)
if(e) then
data.on_failure = e[ "r_value" ]
end
local dialog = yl_speak_up.speak_to[pname].dialog
local sorted_dialog_list = yl_speak_up.sort_keys(dialog.n_dialogs)
local nr = 1
@ -1614,7 +1581,10 @@ yl_speak_up.get_fs_edit_option_effect_on_failure = function(pname, dialog, forms
end
yl_speak_up.get_fs_edit_option_effect_chat_all = function(pname, dialog, formspec, data, id_prefix, save_button)
yl_speak_up.get_fs_edit_option_effect_chat_all = function(pname, dialog, formspec, data, id_prefix, save_button, e)
if(e) then
data.chat_msg_text = e[ "r_value" ]
end
local default_text = "$NPC_NAME$ (owned by $OWNER_NAME$) announces: $PLAYER_NAME$ "..
"- example; please enter the text -"
return
@ -1631,7 +1601,18 @@ yl_speak_up.get_fs_edit_option_effect_chat_all = function(pname, dialog, formspe
end
yl_speak_up.get_fs_edit_option_action_trade = function(pname, dialog, formspec, data, id_prefix, save_button)
yl_speak_up.get_fs_edit_option_action_trade = function(pname, dialog, formspec, data, id_prefix, save_button, e)
if(e) then
data.trade_id = e[ "a_value" ]
-- use as background images
if(dialog and dialog.trades and dialog.trades[ data.trade_id ]) then
data.pay = dialog.trades[ data.trade_id ].pay[1]
data.buy = dialog.trades[ data.trade_id ].buy[1]
end
local sorted_dialog_list = yl_speak_up.sort_keys(dialog.n_dialogs)
data.action_failure_dialog = math.max(1,
table.indexof(sorted_dialog_list, e[ "a_on_failure" ]))
end
local dialog = yl_speak_up.speak_to[pname].dialog
local d_id = yl_speak_up.speak_to[pname].d_id
local o_id = yl_speak_up.speak_to[pname].o_id
@ -1663,7 +1644,7 @@ yl_speak_up.get_fs_edit_option_action_trade = function(pname, dialog, formspec,
end
yl_speak_up.get_fs_edit_option_action_npc_gives = function(pname, dialog, formspec, data, id_prefix, save_button)
yl_speak_up.get_fs_edit_option_action_npc_gives = function(pname, dialog, formspec, data, id_prefix, save_button, e)
local bg_img = ""
if(data and data.item_string) then
bg_img = "item_image[2.15,3.65;0.7,0.7;"..tostring(data.item_string).."]"
@ -1710,7 +1691,7 @@ yl_speak_up.get_fs_edit_option_action_npc_gives = function(pname, dialog, formsp
end
yl_speak_up.get_fs_edit_option_action_npc_wants = function(pname, dialog, formspec, data, id_prefix, save_button)
yl_speak_up.get_fs_edit_option_action_npc_wants = function(pname, dialog, formspec, data, id_prefix, save_button, e)
local bg_img = ""
if(data and data.item_string) then
bg_img = "item_image[2.15,3.65;0.7,0.7;"..tostring(data.item_string).."]"
@ -1745,7 +1726,14 @@ yl_speak_up.get_fs_edit_option_action_npc_wants = function(pname, dialog, formsp
end
yl_speak_up.get_fs_edit_option_action_text_input = function(pname, dialog, formspec, data, id_prefix, save_button)
yl_speak_up.get_fs_edit_option_action_text_input = function(pname, dialog, formspec, data, id_prefix, save_button, e)
if(e) then
data.quest_question = e[ "a_question" ]
data.quest_answer = e[ "a_value" ]
local sorted_dialog_list = yl_speak_up.sort_keys(dialog.n_dialogs)
data.action_failure_dialog = math.max(1,
table.indexof(sorted_dialog_list, e[ "a_on_failure" ]))
end
return
"label[0.2,3.3;What to ask the player and which answer to expect:]"..
"label[0.2,4.0;Question to show:]"..
@ -1778,7 +1766,15 @@ yl_speak_up.get_fs_edit_option_action_text_input = function(pname, dialog, forms
end
yl_speak_up.get_fs_edit_option_action_custom = function(pname, dialog, formspec, data, id_prefix, save_button)
yl_speak_up.get_fs_edit_option_action_custom = function(pname, dialog, formspec, data, id_prefix, save_button, e)
if(e) then
data.custom_param = e[ id_prefix.."value" ]
if(id_prefix == "a_") then
local sorted_dialog_list = yl_speak_up.sort_keys(dialog.n_dialogs)
data.action_failure_dialog = math.max(1,
table.indexof(sorted_dialog_list, e[ "a_on_failure" ]))
end
end
formspec = formspec..
"label[0.2,3.3;Note: Calling a custom function will require direct support "..
"from the server.]"..