diff --git a/fs_add_quest_steps.lua b/fs_add_quest_steps.lua index 59c5c50..c5e267b 100644 --- a/fs_add_quest_steps.lua +++ b/fs_add_quest_steps.lua @@ -85,57 +85,74 @@ yl_speak_up.input_fs_add_quest_steps = function(player, formname, fields) if(not(fields) or not(player)) then return end - local res1 = yl_speak_up.player_is_working_on_quest(player) - if(res1.error_msg) then + local res = yl_speak_up.player_is_working_on_quest(player) + if(res.error_msg) then yl_speak_up.show_fs(player, "msg", { input_to = "yl_speak_up:add_quest_steps", - formspec = yl_speak_up.get_fs_quest_edit_error(res1.error_msg, "back")}) + formspec = yl_speak_up.get_fs_quest_edit_error(res.error_msg, "back")}) return end - local pname = res1.pname - local q_id = res1.q_id - local current_step = res1.current_step - local step_data = res1.step_data + local pname = res.pname + local q_id = res.q_id + local current_step = res.current_step + local step_data = res.step_data + local quest = res.quest if(fields.back_from_error_msg) then yl_speak_up.show_fs(player, "add_quest_steps") return end - if(fields.back) then - yl_speak_up.show_fs(player, "manage_quest_steps", current_step) - return - end - local mode = yl_speak_up.speak_to[pname].quest_step_mode + if(fields.back) then + -- go back to quest overview + if(mode and mode == "manage_quest_npcs") then + return yl_speak_up.show_fs(player, "manage_quests") + end + return yl_speak_up.show_fs(player, "manage_quest_steps", current_step) + end -- has a quest step be selected? local work_step = nil - if(fields.add_step and fields.add_quest_step) then + if(fields.add_element and fields.add_element_name) then -- create a new quest step - local new_step = fields.add_quest_step:trim() + local new_step = fields.add_element_name:trim() -- a new one shall be created - local res = yl_speak_up.quest_step_add_quest_step(pname, q_id, new_step) - if(res ~= "OK") then + local res2 = yl_speak_up.quest_step_add_quest_step(pname, q_id, new_step) + if(res2 ~= "OK") then yl_speak_up.show_fs(player, "msg", { input_to = "yl_speak_up:add_quest_steps", formspec = "size[9,2]".. "label[0.2,0.0;Error:\n".. - minetest.formspec_escape(minetest.wrap_text(res,80)).."]".. + minetest.formspec_escape(minetest.wrap_text(res2,80)).."]".. "button[1.5,1.5;2,0.9;back_from_error_msg;Back]"}) - return res + return res2 end -- this will also be set if the quest step exists already; this is fine so far work_step = new_step elseif(fields.add_from_available - and yl_speak_up.speak_to[pname].available_quest_steps) then + and yl_speak_up.speak_to[pname].list_available) then -- selected a quest step from the list of available steps offered - local liste = yl_speak_up.speak_to[pname].available_quest_steps + local liste = yl_speak_up.speak_to[pname].list_available local selected = minetest.explode_table_event(fields.add_from_available) if(selected and selected.row and selected.row > 1 and selected.row <= #liste + 1) then work_step = liste[selected.row - 1] end + elseif(fields.add_to_npc_list + and yl_speak_up.speak_to[pname].list_available) then + -- selected an NPC from the list of available NPC offered + local liste = yl_speak_up.speak_to[pname].list_available + local selected = minetest.explode_table_event(fields.add_to_npc_list) + if(selected and selected.row and selected.row > 1 and selected.row <= #liste + 1) then + local npc_id = liste[selected.row - 1] + if(table.indexof(res.quest.npcs or {}, npc_id) == -1) then + table.insert(yl_speak_up.quests[q_id].npcs, npc_id) + yl_speak_up.save_quest(q_id) + end + end + return yl_speak_up.show_fs(player, "add_quest_steps") + elseif(fields.delete_from_one_step_required and current_step and step_data[current_step]) then -- remove a quest step from the list (from one step required) local selected = minetest.explode_table_event(fields.delete_from_one_step_required) @@ -155,6 +172,17 @@ yl_speak_up.input_fs_add_quest_steps = function(player, formname, fields) end yl_speak_up.save_quest(q_id) return yl_speak_up.show_fs(player, "add_quest_steps") + + elseif(fields.delete_from_npc_list) then + -- remove an NPC from the list of contributors + -- TODO: check if it *can* be removed + local selected = minetest.explode_table_event(fields.delete_from_npc_list) + local liste = (res.quest.npcs or {}) + if(selected and selected.row and selected.row > 1 and selected.row <= #liste + 1) then + table.remove(yl_speak_up.quests[q_id].npcs, selected.row - 1) + end + yl_speak_up.save_quest(q_id) + return yl_speak_up.show_fs(player, "add_quest_steps") end if(mode == "embedded_select") then @@ -276,6 +304,82 @@ yl_speak_up.quest_step_list_show_table = function(formspec, table_specs, liste, end +-- returns list of NPCs that pname can edit and that are not yet part of quest_npc_list +yl_speak_up.quest_get_npc_candidate_list = function(pname, quest_npc_liste) + -- build a list of candidates + local npc_list = {} + for k, v in pairs(yl_speak_up.npc_list) do + -- only NPC that are not already added + if(table.indexof(quest_npc_liste or {}, k) == -1 + -- and only those that the player can edit + and (v.owner == pname or (v.may_edit and v.may_edit[pname]))) then + table.insert(npc_list, k) + end + end + table.sort(npc_list) + return npc_list +end + + +-- lists npc that are either already added or could be added +yl_speak_up.quest_npc_show_table = function(formspec, table_specs, liste, step_data) + local grey_if_zero = function(fs, n) + if(n and n == 0) then + table.insert(fs, "#444444") + else + table.insert(fs, "#FFFFFF") + end + table.insert(fs, minetest.formspec_escape(n)) + end + + table.insert(formspec, "tablecolumns[".. + "color;text,align=right;".. -- used in this many quest steps + "color;text,align=left;".. -- n_id + "color;text,align=left;".. -- owner + "color;text,align=left".. -- name of NPC + "]table[") + table.insert(formspec, table_specs) + table.insert(formspec,"#FFFFFF,Used:,#FFFFFF,n_id:,#FFFFFF,Name") + table.insert(formspec, minetest.formspec_escape(",")) + table.insert(formspec, " description:,#FFFFFF,Owner:,") + local tmp = {} + for i, n_id in ipairs(liste or {}) do + -- find out in how many quest steps this NPC is used + local used = 0 + for s, d in pairs(step_data) do + for loc_id, loc in pairs(d.where or {}) do + if(loc and loc.n_id and loc.n_id == "n_"..n_id) then + used = used + 1 + end + end + end + grey_if_zero(tmp, used) + -- the n_id of the NPC + table.insert(tmp, "#AAFFAA") + table.insert(tmp, "n_"..minetest.formspec_escape(n_id)) + -- get information from the NPC list (see fs_npc_list.lua) + local owner = "- ? -" + local name = "- ? -" + if(yl_speak_up.npc_list[n_id]) then + local npc = yl_speak_up.npc_list[n_id] + owner = npc.owner + name = (npc.name or name) + if(npc.desc and npc.desc ~= "") then + name = name..', '..(npc.desc or "") + end + end + -- name and description of the NPC + table.insert(tmp, "#AAFFAA") + table.insert(tmp, minetest.formspec_escape(name)) + -- owner of the NPC + table.insert(tmp, "#AAFFAA") + table.insert(tmp, minetest.formspec_escape(owner)) + end + table.insert(formspec, table.concat(tmp, ",")) + table.insert(formspec, ";]") +end + + -- param is unused yl_speak_up.get_fs_add_quest_steps = function(player, param) local res = yl_speak_up.player_is_working_on_quest(player) @@ -283,7 +387,8 @@ yl_speak_up.get_fs_add_quest_steps = function(player, param) return yl_speak_up.get_fs_quest_edit_error(res.error_msg, "back") end local pname = res.pname - local step_data = res.step_data + local step_data = res.step_data or {} + -- find out if a quest step is required by other quest steps local required_for_steps = yl_speak_up.quest_step_get_required_for_steps(step_data) @@ -295,10 +400,18 @@ yl_speak_up.get_fs_add_quest_steps = function(player, param) this_step_data = step_data[current_step] end local mode = "" - if(pname and yl_speak_up.speak_to[pname] and yl_speak_up.speak_to[pname].quest_step_mode) then + if(param) then + mode = param + yl_speak_up.speak_to[pname].quest_step_mode = param + elseif(pname and yl_speak_up.speak_to[pname] and yl_speak_up.speak_to[pname].quest_step_mode) then mode = yl_speak_up.speak_to[pname].quest_step_mode end + local add_what = "Add a new quest step named:" + if(mode == "manage_quest_npcs") then + add_what = "Add the NPC with n_:" + end + local formspec = {} if(mode and mode == "embedded_select") then table.insert(formspec, "size[30,12]container[6,0;18.5,12]") @@ -306,6 +419,7 @@ yl_speak_up.get_fs_add_quest_steps = function(player, param) else table.insert(formspec, "size[18.5,17.3]") end + -- add back button table.insert(formspec, "button[8,0;2,0.7;back;Back]") -- show which quest we're working at @@ -319,9 +433,11 @@ yl_speak_up.get_fs_add_quest_steps = function(player, param) table.insert(formspec, "]") -- add new quest step - table.insert(formspec, "label[0.2,2.2;Add a new quest step named:]") - table.insert(formspec, "button[16.1,2.4;1.2,0.7;add_step;Add]") - table.insert(formspec, "field[1.0,2.4;15,0.7;add_quest_step;;]") + table.insert(formspec, "label[0.2,2.2;") + table.insert(formspec, add_what) + table.insert(formspec, "]") + table.insert(formspec, "button[16.1,2.4;1.2,0.7;add_element;Add]") + table.insert(formspec, "field[1.0,2.4;15,0.7;add_element_name;;]") local y_pos = 3.3 if(current_step and mode == "insert_after_prev_step") then @@ -380,6 +496,27 @@ yl_speak_up.get_fs_add_quest_steps = function(player, param) step_data, required_for_steps) table.insert(formspec, "label[0.2,7.5;(Click on an entry to delete it from the list above.)]") y_pos = 8.3 + -- which NPC may contribute to the quest? + elseif(mode == "manage_quest_npcs") then + table.insert(formspec, "container[0,3.3;18,6]") + table.insert(formspec, "label[0.2,0;so that the NPC ".. + minetest.colorize("#9999FF", "may contribute").. + " to the quest like these NPC:]") + yl_speak_up.quest_npc_show_table(formspec, + "0.2,0.2;17.0,3.0;delete_from_npc_list;", + res.quest.npcs or {}, + step_data) + table.insert(formspec, "label[0.2,3.4;(Click on an entry to delete it from the list above.)]") + local available_npcs = yl_speak_up.quest_get_npc_candidate_list(pname, res.quest.npcs or {}) + yl_speak_up.speak_to[pname].list_available = available_npcs + table.insert(formspec, "label[0.2,4.4;or select an NPC from the list below:]") + yl_speak_up.quest_npc_show_table(formspec, + "0.2,4.6;17.0,6.0;add_to_npc_list;", + available_npcs or {}, step_data) + table.insert(formspec, "label[0.2,10.8;Used: Shows in how many quest steps this NPC is used.]") + table.insert(formspec, "container_end[]") + y_pos = 4.2 + return table.concat(formspec, "") end -- some quest steps may not be available/may not make sense @@ -408,7 +545,7 @@ yl_speak_up.get_fs_add_quest_steps = function(player, param) end end table.sort(available_steps) - yl_speak_up.speak_to[pname].available_quest_steps = available_steps + yl_speak_up.speak_to[pname].list_available = available_steps table.insert(formspec, "container[0,") table.insert(formspec, tostring(y_pos))