split action_inv_changed function into editor/ and interpreter functionality

This commit is contained in:
Sokomine 2024-02-07 21:44:39 +01:00
parent e33e1326fd
commit c2728f853b
3 changed files with 66 additions and 48 deletions

View File

@ -79,6 +79,8 @@ yl_speak_up.add_to_command_help_text = yl_speak_up.add_to_command_help_text..
-- dofile(modpath .. "exec_eval_preconditions.lua")
-- dofile(modpath .. "exec_actions.lua")
-- dofile(modpath .. "exec_apply_effects.lua")
-- in edit mode we need a more complex reaction to inventory changes
dofile(modpath .. "exec_actions_action_inv_changed.lua")
-- some helper functions for formatting text for a formspec talbe
dofile(modpath .. "print_as_table.lua")
-- -- create i.e. a dropdown list of player names

View File

@ -0,0 +1,62 @@
local old_action_inv_changed = yl_speak_up.action_inv_changed
-- 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()
if(not(pname) or not(yl_speak_up.speak_to[pname])) then
return
end
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 old_action_inv_changed(inv, listname, index, stack, player, how)
end
-- is the player in the process of editing an action of the npc_gives/npc_wants type?
local target_fs = "edit_actions"
local data = yl_speak_up.speak_to[pname][ "tmp_action" ]
if(not(data) or (data.what ~= 4 and data.what ~= 5)) then
-- we are editing an action
if(data) then
return
end
-- it might be a precondition
data = yl_speak_up.speak_to[pname][ "tmp_prereq" ]
if(not(data) or (data.what ~= 8)) then
return
end
target_fs = "edit_preconditions"
end
-- "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
if(how == "put") then
data.item_node_name = stack:get_name().." "..stack:get_count()
local meta = stack:get_meta()
if(meta and meta:get_string("description")) 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")
if(item_was_for and item_was_for ~= "") then
new_desc = string.gsub(new_desc, item_was_for, "$PLAYER_NAME$")
end
data.item_desc = new_desc
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 = ""
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, target_fs, nil)
-- no need to check anything more here; the real checks need to be done
-- when the player presses the save/store/execute button
end

View File

@ -13,56 +13,10 @@ yl_speak_up.action_inv_changed = function(inv, listname, index, stack, player, h
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
if(listname and listname == "npc_gives") then
yl_speak_up.input_fs_action_npc_gives(player, "action_npc_gives", {})
return
end
if(listname and listname == "npc_gives") then
yl_speak_up.input_fs_action_npc_gives(player, "action_npc_gives", {})
return
end
-- is the player in the process of editing an action of the npc_gives/npc_wants type?
local target_fs = "edit_actions"
local data = yl_speak_up.speak_to[pname][ "tmp_action" ]
if(not(data) or (data.what ~= 4 and data.what ~= 5)) then
-- we are editing an action
if(data) then
return
end
-- it might be a precondition
data = yl_speak_up.speak_to[pname][ "tmp_prereq" ]
if(not(data) or (data.what ~= 8)) then
return
end
target_fs = "edit_preconditions"
end
-- "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
if(how == "put") then
data.item_node_name = stack:get_name().." "..stack:get_count()
local meta = stack:get_meta()
if(meta and meta:get_string("description")) 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")
if(item_was_for and item_was_for ~= "") then
new_desc = string.gsub(new_desc, item_was_for, "$PLAYER_NAME$")
end
data.item_desc = new_desc
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 = ""
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, target_fs, nil)
-- no need to check anything more here; the real checks need to be done
-- when the player presses the save/store/execute button
end