Added successfull and failed quests

This commit is contained in:
TeTpaAka 2015-02-21 14:46:27 +01:00
parent 96a0e1a0e2
commit c3a7d986ae

View File

@ -1,7 +1,3 @@
quests = {}
quests.registered_quests = {}
quests.hud = {}
local show_max = 10 -- the maximum visible quests. local show_max = 10 -- the maximum visible quests.
-- reading previous quests -- reading previous quests
@ -11,6 +7,13 @@ if file then
quests = minetest.deserialize(file:read("*all")) quests = minetest.deserialize(file:read("*all"))
file:close() file:close()
end end
quests = quests or {}
quests.registered_quests = quests.registered_quests or {}
quests.successfull_quests = quests.successfull_quests or {}
quests.failed_quests = quests.failed_quests or {}
quests.hud = quests.hud or {}
quests.formspec_lists = {} quests.formspec_lists = {}
-- call this function to enable the HUD for the player that shows his quests -- call this function to enable the HUD for the player that shows his quests
@ -152,7 +155,7 @@ function quests.update_quest(playername, questname, value)
if (quests.registered_quests[playername][questname]["callback"] ~= nil) then if (quests.registered_quests[playername][questname]["callback"] ~= nil) then
quests.registered_quests[playername][questname]["callback"](playername, questname) quests.registered_quests[playername][questname]["callback"](playername, questname)
end end
quests.registered_quests[playername][questname] = nil quests.accept_quest(playername,questname)
quests.update_hud(playername) quests.update_hud(playername)
end end
return true -- the quest is finished return true -- the quest is finished
@ -167,6 +170,15 @@ end
-- returns false, when the quest is still ongoing -- returns false, when the quest is still ongoing
function quests.accept_quest(playername, questname) function quests.accept_quest(playername, questname)
if (quests.registered_quests[playername][questname]) then if (quests.registered_quests[playername][questname]) then
if (quests.successfull_quests[playername] == nil) then
quests.successfull_quests[playername] = {}
end
if (quests.successfull_quests[playername][questname] ~= nil) then
quests.successfull_quests[playername][questname].count = quests.successfull_quests[playername][questname].count + 1
else
quests.successfull_quests[playername][questname] = quests.registered_quests[playername][questname]
quests.successfull_quests[playername][questname].count = 1
end
quests.registered_quests[playername][questname] = nil quests.registered_quests[playername][questname] = nil
quests.update_hud(playername) quests.update_hud(playername)
return true -- the quest is finished, the mod can give a reward return true -- the quest is finished, the mod can give a reward
@ -179,29 +191,58 @@ end
function quests.abort_quest(playername, questname) function quests.abort_quest(playername, questname)
if (questname == nil) then if (questname == nil) then
return return
end
if (quests.failed_quests[playername] == nil) then
quests.failed_quests[playername] = {}
end end
if (quests.registered_quests[playername][questname] == nil) then
return
end
if (quests.failed_quests[playername][questname] ~= nil) then
quests.failed_quests[playername][questname].count = quests.failed_quests[playername][questname].count + 1
else
quests.failed_quests[playername][questname] = quests.registered_quests[playername][questname]
quests.failed_quests[playername][questname].count = 1
end
quests.registered_quests[playername][questname] = nil quests.registered_quests[playername][questname] = nil
quests.update_hud(playername) quests.update_hud(playername)
end end
-- construct the questlog -- construct the questlog
function quests.create_formspec(playername) function quests.create_formspec(playername, tab)
local queststringlist = {}
local questlist = {} local questlist = {}
quests.formspec_lists[playername] = {} quests.formspec_lists[playername] = quests.formspec_lists[playername] or {}
quests.formspec_lists[playername].id = 1 quests.formspec_lists[playername].id = 1
quests.formspec_lists[playername].list = {} quests.formspec_lists[playername].list = {}
for questname,questspecs in pairs(quests.registered_quests[playername]) do tab = tab or quests.formspec_lists[playername].tab or "1"
if (tab == "1") then
questlist = quests.registered_quests[playername]
elseif (tab == "2") then
questlist = quests.successfull_quests[playername]
elseif (tab == "3") then
questlist = quests.failed_quests[playername]
end
quests.formspec_lists[playername].tab = tab
for questname,questspecs in pairs(questlist) do
local queststring = questspecs["title"] local queststring = questspecs["title"]
if (questspecs["max"] ~= 1) then if (questspecs["count"] and questspecs["count"] > 1) then
local queststring = queststring .. " - (" .. round(questspecs["value"], 2) .. "/" .. questspecs["max"] .. ")" queststring = queststring .. " - " .. questspecs["count"]
elseif(not questspecs["count"] and questspecs["max"] ~= 1) then
queststring = queststring .. " - (" .. round(questspecs["value"], 2) .. "/" .. questspecs["max"] .. ")"
end end
table.insert(questlist, queststring) table.insert(queststringlist, queststring)
table.insert(quests.formspec_lists[playername].list, questname) table.insert(quests.formspec_lists[playername].list, questname)
end end
local formspec = "size[7,10]".. local formspec = "size[7,10]"..
"textlist[0.25,0.25;6.5,7.5;questlist;"..table.concat(questlist, ",") .. ";1;false]" .. "tabheader[0,0;header;Open quests,Finished quests,Failed quests;" .. tab .. "]"..
"button[0.25,8;3,.7;abort;Abort quest]" .. "textlist[0.25,0.25;6.5,7.5;questlist;"..table.concat(queststringlist, ",") .. ";1;false]"
"button[3.75,8;3,.7;config;Configure]".. if (quests.formspec_lists[playername].tab == "1") then
formspec = formspec .."button[0.25,8;3,.7;abort;Abort quest]"
end
formspec = formspec .. "button[3.75,8;3,.7;config;Configure]"..
"button[.25,9;3,.7;info;Info]".. "button[.25,9;3,.7;info;Info]"..
"button_exit[3.75,9;3,.7;exit;Exit]" "button_exit[3.75,9;3,.7;exit;Exit]"
return formspec return formspec
@ -224,10 +265,22 @@ end
-- construct the info formspec -- construct the info formspec
function quests.create_info(playername, questname) function quests.create_info(playername, questname)
local formspec = "size[7,6.5]" .. local formspec = "size[7,6.5]" ..
"label[0.5,0.5;" .. quests.registered_quests[playername][questname].title .. "]".. "label[0.5,0.5;"
"textarea[.5,1.5;6,4.5;description;;" .. quests.registered_quests[playername][questname].description .. "]" .. if (quests.formspec_lists[playername].tab == "1") then
"button[.5,6;3,.7;abort;Abort quest]".. formspec = formspec .. quests.registered_quests[playername][questname].title .. "]" ..
"button[3.25,6;3,.7;return;Return]" "textarea[.5,1.5;6,4.5;description;;" .. quests.registered_quests[playername][questname].description .. "]"
elseif (quests.formspec_lists[playername].tab == "2") then
formspec = formspec .. quests.successfull_quests[playername][questname].title .. "]"..
"textarea[.5,1.5;6,4.5;description;;" .. quests.successfull_quests[playername][questname].description .. "]"
elseif (quests.formspec_lists[playername].tab == "3") then
formspec = formspec .. quests.failed_quests[playername][questname].title .. "]"..
"textarea[.5,1.5;6,4.5;description;;" .. quests.failed_quests[playername][questname].description .. "]"
end
if (quests.formspec_lists[playername].tab == "1") then
formspec = formspec .. "button[.5,6;3,.7;abort;Abort quest]"
end
formspec = formspec .. "button[3.25,6;3,.7;return;Return]"
return formspec return formspec
end end
@ -251,6 +304,10 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
return return
end end
if (formname == "quests:questlog") then if (formname == "quests:questlog") then
if (fields["header"]) then
minetest.show_formspec(playername, "quests:questlog", quests.create_formspec(playername, fields["header"]))
return
end
if (fields["questlist"]) then if (fields["questlist"]) then
local event = minetest.explode_textlist_event(fields["questlist"]) local event = minetest.explode_textlist_event(fields["questlist"])
if (event.type == "CHG") then if (event.type == "CHG") then
@ -302,8 +359,10 @@ minetest.register_on_shutdown(function()
print "Writing quests to file" print "Writing quests to file"
local file = io.open(minetest.get_worldpath().."/quests", "w") local file = io.open(minetest.get_worldpath().."/quests", "w")
if (file) then if (file) then
file:write(minetest.serialize({registered_quests = quests.registered_quests, file:write(minetest.serialize({ registered_quests = quests.registered_quests,
hud = quests.hud})) successfull_quests = quests.successfull_quests,
failed_quests = quests.failed_quests,
hud = quests.hud}))
file:close() file:close()
end end
end) end)