diff --git a/fs_edit_general.lua b/fs_edit_general.lua index 1c78c6d..f29be2e 100644 --- a/fs_edit_general.lua +++ b/fs_edit_general.lua @@ -204,6 +204,8 @@ yl_speak_up.save_element_p_or_a_or_e = function( local v = {} -- determine p_type v[ id_prefix.."type" ] = values_what[ data.what ] + -- so that we don't have to compare number values of data.what + local what_type = values_what[ data.what ] local dialog = yl_speak_up.speak_to[pname].dialog if(not(dialog) or not(dialog.n_dialogs) @@ -238,14 +240,14 @@ yl_speak_up.save_element_p_or_a_or_e = function( return end -- "an internal state (i.e. of a quest)", -- 2 - if(data.what == 2 and id_prefix ~= "a_") then + if(what_type == "state" and id_prefix ~= "a_") then v[ id_prefix.."value" ] = "expression" v[ id_prefix.."operator" ] = values_operator[ data.operator ] v[ id_prefix.."var_cmp_value" ] = (data.var_cmp_value or "") v[ id_prefix.."variable" ] = yl_speak_up.add_pname_to_var(data.variable_name, pname) -- "a block somewhere", -- 3 - elseif(data.what == 3 and id_prefix ~= "a_") then + elseif(what_type == "block" and id_prefix ~= "a_") then v[ id_prefix.."value" ] = values_block[ data.block ] if(not(data.block_pos) or not(data.node_data) or not(data.node_data.name)) then yl_speak_up.show_fs(player, "msg", { @@ -290,7 +292,7 @@ yl_speak_up.save_element_p_or_a_or_e = function( -- "a trade", -- 4 -- (only for preconditions; not for effects) - elseif(data.what == 4 and id_prefix == "p_") then + elseif(what_type == "trade" and id_prefix == "p_") then -- this depends on the trade associated with that option; therefore, -- it does not need any more parameters (they come dynamicly from the -- trade) @@ -299,7 +301,7 @@ yl_speak_up.save_element_p_or_a_or_e = function( -- "the inventory of the player", -- 5 -- "the inventory of the NPC", -- 6 -- (only for preconditions; not for effects) - elseif((data.what == 5 or data.what == 6) and id_prefix == "p_") then + elseif((what_type == "player_inv" or what_type == "npc_inv") and id_prefix == "p_") then v.p_value = values_inv[ data.inv ] if(v.p_value and v.p_value ~= "inv_is_empty") then if(not(data.inv_stack_name) or data.inv_stack_name == "") then @@ -317,7 +319,7 @@ yl_speak_up.save_element_p_or_a_or_e = function( -- "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 + elseif(id_prefix == "r_" and (what_type == "give_item" or what_type == "take_item")) then if(not(data.inv_stack_name) or data.inv_stack_name == "") then yl_speak_up.show_fs(player, "msg", { input_to = "yl_speak_up:"..formspec_input_to, @@ -340,7 +342,7 @@ yl_speak_up.save_element_p_or_a_or_e = function( -- "move the player to a given position (requires npc_master priv)", -- 9 - elseif(data.what and id_prefix == "r_" and data.what == 9) then + elseif(what_type == "move" and id_prefix == "r_") then if(not(data.move_to_x) or not(data.move_to_y) or not(data.move_to_z)) then yl_speak_up.show_fs(player, "msg", { input_to = "yl_speak_up:"..formspec_input_to, @@ -362,9 +364,9 @@ yl_speak_up.save_element_p_or_a_or_e = function( v[ "r_value" ] = minetest.pos_to_string( {x = data.move_to_x, y = data.move_to_y, z = data.move_to_z}) - -- effect "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 + -- effect "execute Lua code (requires npc_master priv)", -- precondition: 8; effect: 10 + elseif((what_type == "function" and id_prefix == "p_") + or (what_type == "function" and id_prefix == "r_")) then if(not(data.lua_code) or data.lua_code == "") then yl_speak_up.show_fs(player, "msg", { input_to = "yl_speak_up:"..formspec_input_to, @@ -387,7 +389,7 @@ yl_speak_up.save_element_p_or_a_or_e = function( -- "NPC crafts something", -- 4 -- (only for effects; not for preconditions) - elseif(data.what and id_prefix == "r_" and data.what == 4) then + elseif(what_type == "craft" and id_prefix == "r_") then local player_inv = player:get_inventory() if(player_inv:get_stack("craftpreview", 1):is_empty()) then yl_speak_up.show_fs(player, "msg", { @@ -410,12 +412,12 @@ yl_speak_up.save_element_p_or_a_or_e = function( -- "go to other dialog if the *previous* effect failed", -- 5 -- (only for effects; not for preconditions) - elseif(data.what and id_prefix == "r_" and data.what == 5) then + elseif(what_type == "on_failure" and id_prefix == "r_") then v[ "r_value" ] = data.on_failure -- "send a chat message to all players", -- 6 -- (only for effects; not for preconditions) - elseif(data.what and id_prefix == "r_" and data.what == 6) then + elseif(what_type == "chat_all" and id_prefix == "r_") then data.chat_msg_text = fields.chat_msg_text -- allow saving only if the placeholders are all present -- (reason for requiring them: players and server owners ought to @@ -437,7 +439,7 @@ yl_speak_up.save_element_p_or_a_or_e = function( -- "Normal trade - one item(stack) for another item(stack).", -- 3 -- (only for actions) - elseif(data.what and id_prefix == "a_" and data.what == 3) then + elseif(what_type == "trade" and id_prefix == "a_") then -- remember which option was selected yl_speak_up.speak_to[pname].o_id = o_id -- do not switch target dialog (we are in edit mode) @@ -474,11 +476,8 @@ yl_speak_up.save_element_p_or_a_or_e = function( -- "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 or data.what == 5)) then - local trade_inv_list = "npc_gives" - if(data.what == 5) then - trade_inv_list = "npc_wants" - end + elseif((what_type == "npc_gives" or what_type == "npc_wants") and id_prefix == "a_") then + local trade_inv_list = what_type local trade_inv = minetest.get_inventory({type="detached", name="yl_speak_up_player_"..pname}) if(not(trade_inv) or trade_inv:is_empty( trade_inv_list )) then yl_speak_up.show_fs(player, "msg", { @@ -521,7 +520,7 @@ yl_speak_up.save_element_p_or_a_or_e = function( v[ "a_value" ] = stack:get_name().." "..stack:get_count() -- for displaying as a background image data.item_string = v[ "a_value" ] - if(data.what == 5) then + if(what_type == "npc_wants") then -- try to reconstruct $PLAYER_NAME$ (may not always work) local item_was_for = meta:get_string("yl_speak_up:quest_item_for") local new_desc = meta:get_string("description") @@ -534,19 +533,19 @@ yl_speak_up.save_element_p_or_a_or_e = function( if(data.item_desc and data.item_desc ~= "" and data.item_desc ~= "- none set -") then - if(data.what == 4) then + if(what_type == "npc_gives") then meta:set_string("description", data.item_desc) end v[ "a_item_desc" ] = data.item_desc end - if(data.what == 5) then + if(what_type == "npc_wants") 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 + if(what_type == "npc_gives") 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 @@ -557,7 +556,7 @@ yl_speak_up.save_element_p_or_a_or_e = function( end v[ "a_item_quest_id" ] = data.item_quest_id end - if( v["a_item_quest_id"] and not(v[ "a_item_desc"]) and data.what == 4) then + if( v["a_item_quest_id"] and not(v[ "a_item_desc"]) and what_type == "npc_gives") then yl_speak_up.show_fs(player, "msg", { input_to = "yl_speak_up:"..formspec_input_to, formspec = "size[9,2.5]".. @@ -589,7 +588,7 @@ yl_speak_up.save_element_p_or_a_or_e = function( "button[1.5,2.0;2,0.9;back_from_saving;Back]" -- "The player has to manually enter a password or passphrase or some other text.", -- 6 - elseif(data.what and id_prefix == "a_" and data.what == 6) then + elseif(what_type == "text_input" and id_prefix == "a_") then if(not(data.quest_question)) then data.quest_question = "Your answer:" end @@ -619,20 +618,19 @@ yl_speak_up.save_element_p_or_a_or_e = function( v[ "a_on_failure" ] = sorted_dialog_list[ data.action_failure_dialog ] -- "Call custom functions that are supposed to be overridden by the server.", -- - -- precondition: 8; action: 7; effect: 11 - 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 + -- precondition: 9; action: 7; effect: 11 + elseif((id_prefix == "a_" and what_type == "custom") + or (id_prefix == "p_" and what_type == "custom") + or (id_prefix == "r_" and what_type == "custom")) then v[ id_prefix.."value" ] = data.custom_param if(id_prefix == "a_") then local sorted_dialog_list = yl_speak_up.sort_keys(dialog.n_dialogs) v[ "a_on_failure" ] = sorted_dialog_list[ data.action_failure_dialog ] end - -- "The preconditions of another dialog option are fulfilled/not fulfilled.", -- 9 - -- precondition: 9 - elseif(data.what and id_prefix == "p_" and data.what == 9) then + -- "The preconditions of another dialog option are fulfilled/not fulfilled.", -- 10 + -- precondition: 10 + elseif(what_type == "other" and id_prefix == "p_") then if(data.other_o_id and data.other_o_id ~= "-select-") then v[ "p_value" ] = data.other_o_id end @@ -734,6 +732,11 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields, -- field inputs: those do not trigger a sending of the formspec on their own + local what_type = "" + if(data and data.what and values_what[ data.what ]) then + what_type = values_what[ data.what ] + end + local was_changed = false -- are we talking about an inventory? -- (inventory only applies to preconditions; not effects) @@ -741,10 +744,10 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields, if(((fields.inv_stack_name and fields.inv_stack_name ~= "") or (fields.store_item_name and fields.store_item_name ~= "")) and data and data.what - and ((id_prefix == "p_" and data.what >= 5 and data.what <= 6) + and ((id_prefix == "p_" and (what_type == "player_inv" or what_type == "npc_inv")) -- "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 - or (id_prefix == "r_" and data.what >= 7 and data.what <= 8))) then + or (id_prefix == "r_" and (what_type == "give_item" or what_type == "take_item")))) then local wanted = "" local wanted_name = "" if(not(fields.store_item_name)) then @@ -793,13 +796,13 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields, -- comparison value for a variable (same for both preconditions and effects) elseif(fields.var_cmp_value - and data and data.what and data.what == 2 and id_prefix ~= "a_") then + and data and data.what and what_type == "state" and id_prefix ~= "a_") then data.var_cmp_value = fields.var_cmp_value was_changed = true -- text for a chat message elseif(fields.chat_msg_text - and data and data.what and data.what == 6 and id_prefix == "r_") then + and data and data.what and what_type == "chat_all" and id_prefix == "r_") then data.chat_msg_text = fields.chat_msg_text was_changed = true @@ -807,16 +810,16 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields, and fields.custom_param ~= "- Insert a text that is passed on to your function here -" and fields.custom_param ~= "" and data and 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 + and ((id_prefix == "a_" and what_type == "custom") + or (id_prefix == "p_" and what_type == "custom") + or (id_prefix == "r_" and what_type == "custom"))) then data.custom_param = fields.custom_param was_changed = true 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 + and data and data.what and what_type == "npc_gives" and id_prefix == "a_") then data.item_quest_id = fields.action_item_quest_id was_changed = true end @@ -824,14 +827,14 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields, 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 + and data and data.what and what_type == "npc_gives" and id_prefix == "a_") then -- TODO: check if it diffrent from the default one of the stack data.item_desc = fields.action_item_desc was_changed = true end if(fields.quest_question and fields.quest_question ~= "" - and data and data.what and data.what == 6 and id_prefix == "a_") then + and data and data.what and what_type == "text_input" and id_prefix == "a_") then data.quest_question = fields.quest_question was_changed = true end @@ -839,7 +842,7 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields, if(fields.quest_answer and fields.quest_answer ~= "- Insert the correct answer here -" and fields.quest_answer ~= "" - and data and data.what and data.what == 6 and id_prefix == "a_") then + and data and data.what and what_type == "text_input" and id_prefix == "a_") then data.quest_answer = fields.quest_answer was_changed = true end @@ -1162,6 +1165,7 @@ yl_speak_up.get_fs_edit_option_related = function(player, table_click_result, end local data = yl_speak_up.speak_to[pname][ tmp_data_cache ] + if(not(data) or not(data.what)) then data = { what = 1} end @@ -1240,9 +1244,13 @@ yl_speak_up.get_fs_edit_option_related = function(player, table_click_result, if(data.what) then yl_speak_up.speak_to[pname][ tmp_data_cache ] = data end + local what_type = "" + if(data and data.what and values_what[ data.what ]) then + what_type = values_what[ data.what ] + end -- "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 + if(data.what and what_type == "state" and id_prefix ~= "a_") then return yl_speak_up.get_fs_edit_option_p_and_e_state( pname, dialog, formspec, data, id_prefix, save_button, e, text_variable, text_select_value, text_select_operator, @@ -1250,14 +1258,14 @@ yl_speak_up.get_fs_edit_option_related = function(player, table_click_result, -- "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 + elseif(data.what and what_type == "block" and id_prefix ~= "a_") then return yl_speak_up.get_fs_edit_option_p_and_e_block( pname, dialog, formspec, data, id_prefix, save_button, e, text_block_position, values_block, check_block) -- "a trade", -- 4 -- (trade - only for preconditions; effects have something else here) - elseif(data.what and id_prefix == "p_" and data.what == 4) then + elseif(data.what and id_prefix == "p_" and what_type == "trade") then return yl_speak_up.get_fs_edit_option_precondition_trade( pname, dialog, formspec, data, id_prefix, save_button, e, values_trade, check_trade) @@ -1265,80 +1273,80 @@ yl_speak_up.get_fs_edit_option_related = function(player, table_click_result, -- "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 + elseif(data.what and id_prefix == "p_" and (what_type == "player_inv" or what_type == "npc_inv")) then return yl_speak_up.get_fs_edit_option_precondition_inv( pname, dialog, formspec, data, id_prefix, save_button, e, values_inv, check_inv) -- "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 + elseif(data.what and id_prefix == "r_" and (what_type == "give_item" or what_type=="take_item")) then return yl_speak_up.get_fs_edit_option_effect_give_item_or_take_item( 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 + elseif(data.what and id_prefix == "r_" and what_type == "move") then return yl_speak_up.get_fs_edit_option_effect_move( 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 + -- "execute Lua code (requires npc_master priv)", -- precondition: 8; effect: 10 + elseif((data.what and id_prefix == "p_" and what_type == "function") + or (data.what and id_prefix == "r_" and what_type == "function")) then return yl_speak_up.get_fs_edit_option_p_and_e_function( 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 + elseif(data.what and id_prefix == "r_" and what_type == "craft") then return yl_speak_up.get_fs_edit_option_effect_craft( 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 + elseif(data.what and id_prefix == "r_" and what_type == "on_failure") then return yl_speak_up.get_fs_edit_option_effect_on_failure( 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 + elseif(data.what and id_prefix == "r_" and what_type == "chat_all") then return yl_speak_up.get_fs_edit_option_effect_chat_all( 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 + elseif(data.what and id_prefix == "a_" and what_type == "trade") then return yl_speak_up.get_fs_edit_option_action_trade( 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 + elseif(data.what and id_prefix == "a_" and what_type == "npc_gives") then return yl_speak_up.get_fs_edit_option_action_npc_gives( 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 + elseif(data.what and id_prefix == "a_" and what_type == "npc_wants") then return yl_speak_up.get_fs_edit_option_action_npc_wants( 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 + elseif(data.what and id_prefix == "a_" and what_type == "text_input") then return yl_speak_up.get_fs_edit_option_action_text_input( 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 + -- precondition: 9; action: 7; effect: 11 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 + and ((id_prefix == "a_" and what_type == "custom") + or (id_prefix == "p_" and what_type == "custom") + or (id_prefix == "r_" and what_type == "custom"))) then return yl_speak_up.get_fs_edit_option_all_custom( pname, dialog, formspec, data, id_prefix, save_button, e) - -- "The preconditions of another dialog option are fulfilled/not fulfilled.", -- 9 + -- "The preconditions of another dialog option are fulfilled/not fulfilled.", -- 10 -- precondition: 9 - elseif(data.what and id_prefix == "p_" and data.what == 9) then + elseif(data.what and id_prefix == "p_" and what_type == "other") then return yl_speak_up.get_fs_other_option_preconditions( pname, dialog, formspec, data, id_prefix, save_button, e) end @@ -1617,7 +1625,7 @@ yl_speak_up.get_fs_edit_option_effect_move = function( end --- "execute Lua code (requires npc_master priv)", -- precondition: 7; effect: 10 +-- "execute Lua code (requires npc_master priv)", -- precondition: 8; effect: 10 yl_speak_up.get_fs_edit_option_p_and_e_function = function( pname, dialog, formspec, data, id_prefix, save_button, e) if(e) then @@ -1910,7 +1918,7 @@ end -- "Call custom functions that are supposed to be overridden by the server.", -- 7 --- precondition: 8; action: 7; effect: 11 +-- precondition: 9; action: 7; effect: 11 yl_speak_up.get_fs_edit_option_all_custom = function( pname, dialog, formspec, data, id_prefix, save_button, e) if(e) then @@ -1952,8 +1960,8 @@ yl_speak_up.get_fs_edit_option_all_custom = function( end --- "The preconditions of another dialog option are fulfilled/not fulfilled.", -- 9 --- precondition: 9 +-- "The preconditions of another dialog option are fulfilled/not fulfilled.", -- 10 +-- precondition: 10 yl_speak_up.get_fs_other_option_preconditions = function( pname, dialog, formspec, data, id_prefix, save_button, e) local dialog = yl_speak_up.speak_to[pname].dialog