improved inserting quest steps before/after others

This commit is contained in:
Sokomine 2023-09-20 00:33:46 +02:00
parent 0204f38a46
commit 030f75be15

View File

@ -152,6 +152,7 @@ yl_speak_up.input_fs_add_quest_steps = function(player, formname, fields)
if(selected and selected.row and selected.row > 1 and selected.row <= #liste + 1) then
table.remove(yl_speak_up.quests[q_id].step_data[current_step].one_step_required, selected.row-1)
end
yl_speak_up.save_quest(q_id)
return yl_speak_up.show_fs(player, "add_quest_steps")
elseif(fields.delete_from_all_steps_required and current_step and step_data[current_step]) then
@ -161,6 +162,7 @@ yl_speak_up.input_fs_add_quest_steps = function(player, formname, fields)
if(selected and selected.row and selected.row > 1 and selected.row <= #liste + 1) then
table.remove(yl_speak_up.quests[q_id].step_data[current_step].all_steps_required, selected.row-1)
end
yl_speak_up.save_quest(q_id)
return yl_speak_up.show_fs(player, "add_quest_steps")
end
@ -172,33 +174,63 @@ yl_speak_up.input_fs_add_quest_steps = function(player, formname, fields)
end
local required_for_steps = yl_speak_up.quest_step_get_required_for_steps(step_data)
if(step_data[work_step]
and step_data[work_step].one_step_required
and #step_data[work_step].one_step_required < 1
and step_data[work_step].all_steps_required
and #step_data[work_step].all_steps_required < 1
and #required_for_steps[work_step] < 1) then
-- this quest step is definitely suited
-- minetest.chat_send_player("singleplayer","SELECTED ok: "..tostring(work_step)) -- TODO
if(mode == "add_to_one_needed") then
if(not(yl_speak_up.quests[q_id].step_data[current_step].one_step_required)) then
yl_speak_up.quests[q_id].step_data[current_step].one_step_required = {}
end
table.insert(yl_speak_up.quests[q_id].step_data[current_step].one_step_required, work_step)
elseif(mode == "add_to_all_needed") then
if(not(yl_speak_up.quests[q_id].step_data[current_step].all_steps_required)) then
yl_speak_up.quests[q_id].step_data[current_step].all_steps_required = {}
end
table.insert(yl_speak_up.quests[q_id].step_data[current_step].all_steps_required, work_step)
elseif(mode == "insert_after_prev_step") then
elseif(mode == "insert_before_next_step") then
return
end
return yl_speak_up.show_fs(player, "add_quest_steps")
end
-- if(step_data[work_step]
-- and step_data[work_step].one_step_required
-- and #step_data[work_step].one_step_required < 1
-- and step_data[work_step].all_steps_required
-- and #step_data[work_step].all_steps_required < 1
-- and #required_for_steps[work_step] < 1) then
-- this quest step is definitely suited
-- minetest.chat_send_player("singleplayer","SELECTED ok: "..tostring(work_step)) -- TODO
-- minetest.chat_send_player("singleplayer","FIELDS: "..minetest.serialize(fields))
-- TODO: implement
-- make sure we have a sane data structure
for i, s in ipairs({current_step, work_step}) do
if(s and yl_speak_up.quests[q_id].step_data[s]) then
if(not(yl_speak_up.quests[q_id].step_data[s].one_step_required)) then
yl_speak_up.quests[q_id].step_data[s].one_step_required = {}
end
if(not(yl_speak_up.quests[q_id].step_data[s].all_steps_required)) then
yl_speak_up.quests[q_id].step_data[s].all_steps_required = {}
end
end
end
-- actually do the work
if(mode == "add_to_one_needed") then
table.insert(yl_speak_up.quests[q_id].step_data[current_step].one_step_required, work_step)
elseif(mode == "add_to_all_needed") then
table.insert(yl_speak_up.quests[q_id].step_data[current_step].all_steps_required, work_step)
elseif(mode == "insert_after_prev_step") then
-- the work_step requires what the current step used to require
if(#step_data[current_step].one_step_required == 1) then
-- a clear insert is possible
yl_speak_up.quests[q_id].step_data[work_step].one_step_required = {
step_data[current_step].one_step_required[1]}
yl_speak_up.quests[q_id].step_data[current_step].one_step_required[1] = work_step
else
-- no useful information on what the new work_step ought to depend on;
-- we just insert the new step at the first place
table.insert(yl_speak_up.quests[q_id].step_data[current_step].one_step_required,
1, work_step)
end
elseif(mode == "insert_before_next_step") then
-- the work_step requires the current_step
table.insert(yl_speak_up.quests[q_id].step_data[work_step].one_step_required, 1, current_step)
-- the current step has exactly one successor? then we adjust that one
if(#required_for_steps[current_step] == 1) then
local next_step = required_for_steps[current_step][1]
local i = table.indexof(step_data[next_step].one_step_required, current_step)
local a = table.indexof(step_data[next_step].all_steps_required, current_step)
if(i > -1) then
-- is it in one_step_required? -> replace current_step with work_step
yl_speak_up.quests[q_id].step_data[next_step].one_step_required[i] = work_step
elseif(a > -1) then
-- or in all_steps_required? -> replace current_step with work_step
yl_speak_up.quests[q_id].step_data[next_step].all_steps_required[i] =work_step
end
end
end
yl_speak_up.save_quest(q_id)
return yl_speak_up.show_fs(player, "add_quest_steps")
end
@ -297,7 +329,7 @@ yl_speak_up.get_fs_add_quest_steps = function(player, param)
if(current_step and mode == "insert_after_prev_step") then
local prev_step = "-"
if(this_step_data and this_step_data.one_step_required and #this_step_data.one_step_required > 0) then
prev_step = #this_step_data.one_step_required[1]
prev_step = this_step_data.one_step_required[1]
end
table.insert(formspec, "label[0.2,3.3;between the previous step:]")
table.insert(formspec, "label[1.0,3.7;")
@ -311,7 +343,7 @@ yl_speak_up.get_fs_add_quest_steps = function(player, param)
elseif(current_step and mode == "insert_before_next_step") then
local next_step = "-"
if(current_step and required_for_steps[current_step] and #required_for_steps[current_step] > 0) then
next_step = #required_for_steps[current_step][1]
next_step = required_for_steps[current_step][1]
end
table.insert(formspec, "label[0.2,3.3;between the currently selected step:]")
table.insert(formspec, "label[1.0,3.7;")