actually save the preconditions

This commit is contained in:
Sokomine 2021-06-07 00:32:11 +02:00
parent a482f8664c
commit 2bd1863e79

View File

@ -12,15 +12,21 @@ local check_what = {
"the inventory of the NPC", -- 6
}
-- how to store these as p_type in the precondition:
local values_what = {nil, "state", "block", "trade", "player_inv", "npc_inv"}
-- options for "a trade"
local check_trade = {
"- please select -",
"The NPC has the item(s) he wants to sell in his inventory.",
"The player has the item(s) needed to pay the price.",
"The NPC ran out of stock.",
"The player cannot afford the price.",
"The NPC has the item(s) he wants to sell in his inventory.", -- 2
"The player has the item(s) needed to pay the price.", -- 3
"The NPC ran out of stock.", -- 4
"The player cannot afford the price.", -- 5
}
-- how to store these as p_value:
local values_trade = {nil, "npc_can_sell", "player_can_buy", "npc_is_out_of_stock", "player_has_not_enough"}
-- options for "the inventory of " (either player or NPC; perhaps blocks later on)
local check_inv = {
"- please select -",
@ -30,6 +36,9 @@ local check_inv = {
"The inventory is empty.",
}
-- how to store these as p_value (the actual itemstack gets stored as p_itemstack):
local values_inv = {nil, "inv_contains", "inv_does_not_contain", "has_room_for", "inv_is_empty"}
local check_block = {
"- please select -",
"The block is as it is now.",
@ -38,6 +47,11 @@ local check_block = {
"I can't punch it. The block is as the block *above* the one I punched.",
}
-- how to store these as p_value (the actual node data gets stored as p_node, p_param2 and p_pos):
-- Note: "node_is_like" occours twice because it is used to cover blocks that
-- cannot be punched as well as normal blocks.
local values_block = {nil, "node_is_like", "node_is_air", "node_is_diffrent_from", "node_is_like"}
-- comparison operators for variables
local check_operator = {
"- please select -", -- 1
@ -52,6 +66,9 @@ local check_operator = {
"is_unset (has no value)" -- 10
}
-- how to store these as p_value (the actual variable is stored in p_variable, and the value in p_cmp_value):
local values_operator = {nil, "==", "~=", ">=", ">", "<=", "<", "not", "is_set", "is_unset"}
-- some internal ones...
local check_variable = {
"- please select -",
@ -94,8 +111,6 @@ yl_speak_up.input_fs_edit_preconditions = function(player, formname, fields)
end
-- TODO: make delete work
-- TODO: make save work
-- TODO: check parameters
if(fields.select_block_pos) then
minetest.chat_send_player(pname,
@ -121,7 +136,7 @@ yl_speak_up.input_fs_edit_preconditions = function(player, formname, fields)
end
-- does the item exist?
if(minetest.registered_items[ parts[1] ]) then
data.inv_stack_name = parts[1].." "..parts[2]
data.inv_stack_name = parts[1].." "..tostring(size)
else
-- show error message
yl_speak_up.show_fs(player, "msg", {
@ -140,6 +155,101 @@ yl_speak_up.input_fs_edit_preconditions = function(player, formname, fields)
data.var_cmp_value = fields.var_cmp_value
end
-- the save button was pressed
if(fields.save_prereq and data and data.what and values_what[ data.what ]) then
-- for creating the new prerequirement; normal elements: p_type, p_value, p_id
local pq = {}
-- determine p_type
pq.p_type = values_what[ data.what ]
local dialog = yl_speak_up.speak_to[pname].dialog
if(not(dialog) or not(dialog.n_dialogs)
or not(dialog.n_dialogs[d_id])
or not(dialog.n_dialogs[d_id].d_options)
or not(dialog.n_dialogs[d_id].d_options[o_id])) then
-- this really should not happen during the normal course of operation
-- (only if the player sends forged formspec data or a bug occoured)
minetest.chat_send_player(pname, "Dialog or option does not exist.")
return
end
local prereq = dialog.n_dialogs[d_id].d_options[o_id].o_prerequisites
-- set p_id appropriately
if(not(p_id) or p_id == "new") then
p_id = "p_"..yl_speak_up.find_next_id(prereq)
if(not(prereq)) then
dialog.n_dialogs[d_id].d_options[o_id].o_prerequisites = {}
prereq = dialog.n_dialogs[d_id].d_options[o_id].o_prerequisites
end
end
pq.p_id = p_id
-- "an internal state (i.e. of a quest)", -- 2
if(data.what == 2) then
pq.p_value = values_operator[ data.operator ]
pq.p_var_cmp_value = (data.var_cmp_value or "")
-- TODO: p_variable
-- "a block somewhere", -- 3
elseif(data.what == 3) then
pq.p_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", {
input_to = "yl_speak_up:edit_preconditions",
formspec = "size[8,2]"..
"label[0.2,0.5;Error: Please select a block first!]"..
"button[1.5,1.5;2,0.9;back_from_error_msg;Back]"})
return
end
-- for "node_is_air", there is no need to store node name and parameter
if(pq.p_value
and (pq.p_value == "node_is_like"
or pq.p_value == "node_is_diffrent_from")) then
pq.p_node = data.node_data.name
pq.p_param2 = data.node_data.param2
end
-- we also need to store the position of the node
pq.p_pos = data.block_pos
-- "I can't punch it. The block is as the block *above* the one I punched.",
if(data.block == 5) then
pq.p_pos = pq.p_pos.y + 1
end
-- "a trade", -- 4
elseif(data.what == 4) then
-- this depends on the trade associated with that option; therefore,
-- it does not need any more parameters (they come dynamicly from the
-- trade)
pq.p_value = values_trade[ data.trade ]
-- "the inventory of the player", -- 5
-- "the inventory of the NPC", -- 6
elseif(data.what == 5 or data.what == 6) then
pq.p_value = values_inv[ data.inv ]
if(pq.p_value and pq.p_value ~= "inv_is_empty") then
-- we have checked this value earlier on
pq[ "p_itemstack" ] = data.inv_stack_name
end
end
-- only save if something was actually selected
if(pq.p_value) then
-- store the change in the dialog
dialog.n_dialogs[d_id].d_options[o_id].o_prerequisites[ p_id ] = pq
-- clear up data
yl_speak_up.speak_to[pname].p_id = nil
yl_speak_up.speak_to[pname].tmp_prereq = nil
-- record this as a change, but do not save do disk yet
table.insert(yl_speak_up.npc_was_changed[ n_id ],
"Dialog "..tostring(d_id)..": ".."Pre(C)ondition "..tostring(p_id)..
" added/changed for option "..tostring(o_id)..".")
-- TODO: when trying to save: save to disk as well?
-- show the new/changed precondition
yl_speak_up.show_fs(player, "edit_preconditions", p_id)
return
end
end
-- selections in a dropdown menu (they trigger sending the formspec)
-- select a general direction/type first
@ -220,15 +330,20 @@ yl_speak_up.get_fs_edit_preconditions = function(player, table_click_result)
end
-- did we arrive here through clicking on a prereq in the dialog edit options menu?
if(table_click_result) then
-- which precondition has the player selected?
local sorted_key_list = yl_speak_up.sort_keys(prereq)
local selected = minetest.explode_table_event(table_click_result)
-- use "new" if nothing fits
p_id = "new"
if((selected.type == "CHG" or selected.type == "DLC")
and selected.row <= #sorted_key_list) then
p_id = sorted_key_list[ selected.row ]
if(table_click_result or prereq[ table_click_result ]) then
if(not(prereq[ table_click_result ])) then
-- which precondition has the player selected?
local sorted_key_list = yl_speak_up.sort_keys(prereq)
local selected = minetest.explode_table_event(table_click_result)
-- use "new" if nothing fits
p_id = "new"
if((selected.type == "CHG" or selected.type == "DLC")
and selected.row <= #sorted_key_list) then
p_id = sorted_key_list[ selected.row ]
end
else
-- allow to directly specify a p_id to show
p_id = table_click_result
end
-- store which prereq we are talking about