From 9feac43e64bbeb1df45d97928a0b5a49fce2be58 Mon Sep 17 00:00:00 2001 From: TeTpaAka Date: Fri, 27 Mar 2015 12:56:43 +0100 Subject: [PATCH] autohide the hud when there are no active quests --- core.lua | 12 ++++++-- formspecs.lua | 29 ++++++++++++++++-- hud.lua | 83 ++++++++++++++++++++++++++++++++++++--------------- init.lua | 3 ++ 4 files changed, 98 insertions(+), 29 deletions(-) diff --git a/core.lua b/core.lua index 51d3461..9ac3589 100644 --- a/core.lua +++ b/core.lua @@ -110,9 +110,15 @@ function quests.accept_quest(playername, questname) quests.successfull_quests[playername][questname] = {count = 1} end quests.active_quests[playername][questname].finished = true + for _,quest in ipairs(quests.hud[playername].list) do + if (quest.name == questname) then + local player = minetest.get_player_by_name(playername) + player:hud_change(quest.id, "number", 0x00AD00) + end + end minetest.after(3, function(playername, questname) quests.active_quests[playername][questname] = nil - minetest.after(1,quests.update_hud,playername) + quests.update_hud(playername) end, playername, questname) return true -- the quest is finished, the mod can give a reward end @@ -141,7 +147,7 @@ function quests.abort_quest(playername, questname) end quests.active_quests[playername][questname].finished = true - for _,quest in ipairs(quests.hud[playername]) do + for _,quest in ipairs(quests.hud[playername].list) do if (quest.name == questname) then local player = minetest.get_player_by_name(playername) player:hud_change(quest.id, "number", 0xAD0000) @@ -149,7 +155,7 @@ function quests.abort_quest(playername, questname) end minetest.after(3, function(playername, questname) quests.active_quests[playername][questname] = nil - minetest.after(1,quests.update_hud,playername) + quests.update_hud(playername) end, playername, questname) end diff --git a/formspecs.lua b/formspecs.lua index f21757b..b88bef8 100644 --- a/formspecs.lua +++ b/formspecs.lua @@ -65,13 +65,19 @@ function quests.create_config(playername, integrated) formspec = formspec .. "size[7,3]" end formspec = formspec .. "checkbox[.25,.25;quests_config_enable;" .. S("Enable HUD") .. ";" - if(quests.hud[playername] ~= nil) then + if(quests.hud[playername] ~= nil and quests.hud[playername].list ~= nil) then + formspec = formspec .. "true" + else + formspec = formspec .. "false" + end + formspec = formspec .. "]checkbox[.25,.75;quests_config_autohide;" .. S("Autohide HUD") .. ";" + if(quests.hud[playername] ~= nil and quests.hud[playername].autohide) then formspec = formspec .. "true" else formspec = formspec .. "false" end formspec = formspec .. "]".. - "button[.25,1.25;3,.7;quests_config_return;" .. S("Return") .. "]" + "button[.25,1.75;3,.7;quests_config_return;" .. S("Return") .. "]" return formspec end @@ -173,11 +179,30 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) -- config if (fields["quests_config_enable"]) then + quests.hud[playername].autohide = false if (fields["quests_config_enable"] == "true") then quests.show_hud(playername) else quests.hide_hud(playername) end + if (formname == "quests:config") then + minetest.show_formspec(playername, "quests:config", quests.create_config(playername)) + else + unified_inventory.set_inventory_formspec(player, "quests_config") + end + end + if (fields["quests_config_autohide"]) then + if (fields["quests_config_autohide"] == "true") then + quests.hud[playername].autohide = true + quests.update_hud(playername) + else + quests.hud[playername].autohide = false + end + if (formname == "quests:config") then + minetest.show_formspec(playername, "quests:config", quests.create_config(playername)) + else + unified_inventory.set_inventory_formspec(player, "quests_config") + end end if (fields["quests_config_return"]) then if (formname == "quests:config") then diff --git a/hud.lua b/hud.lua index 509d1e6..7cb2697 100644 --- a/hud.lua +++ b/hud.lua @@ -15,8 +15,11 @@ local hud_config = { position = {x = 1, y = 0.2}, -- call this function to enable the HUD for the player that shows his quests -- the HUD can only show up to show_max quests -function quests.show_hud(playername) - if (quests.hud[playername] ~= nil) then +function quests.show_hud(playername, autohide) + if (quests.hud[playername] == nil) then + quests.hud[playername] = { autohide = autohide} + end + if (quests.hud[playername].list ~= nil) then return end local hud = { @@ -31,20 +34,20 @@ function quests.show_hud(playername) local player = minetest.get_player_by_name(playername) if (player == nil) then - return + return false end - quests.hud[playername] = {} - table.insert(quests.hud[playername], { value=0, id=player:hud_add(hud) }) + quests.hud[playername].list = {} + table.insert(quests.hud[playername].list, { value=0, id=player:hud_add(hud) }) minetest.after(0, quests.update_hud, playername) end -- call this method to hide the hud function quests.hide_hud(playername) local player = minetest.get_player_by_name(playername) - if (player == nil) then + if (player == nil or quests.hud[playername] == nil or quests.hud[playername].list == nil) then return end - for _,quest in pairs(quests.hud[playername]) do + for _,quest in pairs(quests.hud[playername].list) do player:hud_remove(quest.id) if (quest.id_background ~= nil) then player:hud_remove(quest.id_background) @@ -53,7 +56,7 @@ function quests.hide_hud(playername) player:hud_remove(quest.id_bar) end end - quests.hud[playername] = nil + quests.hud[playername].list = nil end @@ -71,6 +74,12 @@ function quests.update_hud(playername) if (quests.hud[playername] == nil or quests.active_quests[playername] == nil) then return end + if (quests.hud[playername].list == nil) then + if (quests.hud[playername].autohide and next(quests.active_quests[playername]) ~= nil) then + quests.show_hud(playername) + end + return + end local player = minetest.get_player_by_name(playername) if (player == nil) then return @@ -81,14 +90,11 @@ function quests.update_hud(playername) local change = false local visible = {} local remove = {} - for j,hud_element in ipairs(quests.hud[playername]) do + for j,hud_element in ipairs(quests.hud[playername].list) do if (hud_element.name ~= nil) then if (quests.active_quests[playername][hud_element.name] ~= nil) then if (hud_element.value ~= quests.active_quests[playername][hud_element.name].value) then hud_element.value = quests.active_quests[playername][hud_element.name].value - if (hud_element.value == quests.registered_quests[hud_element.name].max) then - player:hud_change(hud_element.id, "number", 0x00BB00) - end player:hud_change(hud_element.id, "text", get_quest_hud_string(hud_element.name, quests.active_quests[playername][hud_element.name])) if (hud_element.id_bar ~= nil) then player:hud_change(hud_element.id_bar, "number", math.floor(40 * hud_element.value / quests.registered_quests[hud_element.name].max)) @@ -121,7 +127,7 @@ function quests.update_hud(playername) --remove ended quests if (remove[1] ~= nil) then for _,j in ipairs(remove) do - table.remove(quests.hud[playername], j) + table.remove(quests.hud[playername].list, j) i = i - 1 end end @@ -157,7 +163,7 @@ function quests.update_hud(playername) text = "quests_questbar.png" }) end - table.insert(quests.hud[playername], { name = questname, + table.insert(quests.hud[playername].list, { name = questname, id = id, id_background = id_background, id_bar = id_bar, @@ -168,19 +174,48 @@ function quests.update_hud(playername) end end end + + if (quests.hud[playername].autohide) then + if (next(quests.active_quests[playername]) == nil) then + player:hud_change(quests.hud[playername].list[1].id, "text", S("No more Quests")) + minetest.after(3, function(playername) + if (next(quests.active_quests[playername]) ~= nil) then + player:hud_change(quests.hud[playername].list[1].id, "text", S("Quests:")) + else + quests.hide_hud(playername) + end + end, playername) + end + end end -- show the HUDs -for playername,id in pairs(quests.hud) do - if (id ~= nil) then - quests.hud[playername] = nil - minetest.after(10, function(playername) - quests.show_hud(playername) - quests.update_hud(playername) - end, playername) +--for playername,id in pairs(quests.hud) do +-- if (id ~= nil) then +-- quests.hud[playername] = nil +-- minetest.after(10, function(playername) +-- quests.show_hud(playername) +-- quests.update_hud(playername) +-- end, playername) +-- end +--end + +minetest.register_on_joinplayer(function(player) + local playername = player:get_player_name() + if (quests.hud[playername] ~= nil) then + if (not(quests.hud[playername].first)) then + return + end + local list = quests.hud[playername].list + print(dump(list)) + local autohide = quests.hud[playername].autohide + quests.hud[playername] = { autohide = autohide } + if (list ~= nil) then + minetest.after(1, function(playername) + quests.show_hud(playername) + end, playername) + end end -end - - +end) diff --git a/init.lua b/init.lua index c3ec828..11b75b9 100644 --- a/init.lua +++ b/init.lua @@ -11,6 +11,9 @@ quests.active_quests = quests.active_quests or {} quests.successfull_quests = quests.successfull_quests or {} quests.failed_quests = quests.failed_quests or {} quests.hud = quests.hud or {} +for idx,_ in pairs(quests.hud) do + quests.hud[idx].first = true +end quests.formspec_lists = {}