1
0
mirror of https://github.com/TeTpaAka/quests.git synced 2024-12-22 16:50:39 +01:00

Split quests.register_quest in two functions

Now register_quest only registers a quest and you have to start it with quests.start_quest()
This commit is contained in:
TeTpaAka 2015-02-28 11:48:27 +01:00
parent 403eb1aad8
commit 3e82fda965
2 changed files with 85 additions and 54 deletions

22
README
View File

@ -1,12 +1,18 @@
quests 0.1
quests is a simple quest framework for minetest that lets you define your own quests and handels the representation. quests is a simple quest framework for minetest that lets you define your own quests and handels the representation.
Dependencies: none
License: WTFPL
-------------------------------------------------------------------------------
You can see a full list of your active quests with the chatcommand /quests You can see a full list of your active quests with the chatcommand /quests
API: API:
quests.register_quest(playername,questname,quest) quests.register_quest(playername,questname,quest)
-- registers a quest for the specified player -- registers a quest for later use
-- --
-- playername is the name of the player, who gets the quest
-- questname is the name of the quest to identify it later -- questname is the name of the quest to identify it later
-- it should follow the naming conventions: "modname:questname" -- it should follow the naming conventions: "modname:questname"
-- quest is a table in the following format -- quest is a table in the following format
@ -21,6 +27,15 @@ quests.register_quest(playername,questname,quest)
-- returns true, when the quest was successfully registered -- returns true, when the quest was successfully registered
-- returns falls, when there was already such a quest -- returns falls, when there was already such a quest
quests.start_quest(playername, questname)
-- starts a quest for a specified player
--
-- playername - the name of the player
-- questname - the name of the quest, which was registered with quests.register_quest
--
-- returns false on failure
-- returns true if the quest was started
quests.update_quest(playername, questname, value) quests.update_quest(playername, questname, value)
-- when something happens that has effect on a quest, a mod should call this method -- when something happens that has effect on a quest, a mod should call this method
-- playername is the name of the player -- playername is the name of the player
@ -39,6 +54,9 @@ quests.accept_quest(playername, questname)
quests.abort_quest(playername, questname) quests.abort_quest(playername, questname)
-- call this method, when you want to end a quest even when it was not finished -- call this method, when you want to end a quest even when it was not finished
-- example: the player failed -- example: the player failed
--
-- returns false if the quest was not aborted
-- returns true when the quest was aborted
quests.show_hud(playername) quests.show_hud(playername)
-- shows the hud to player playername -- shows the hud to player playername

117
init.lua
View File

@ -8,7 +8,8 @@ if file then
file:close() file:close()
end end
quests = quests or {} quests = quests or {}
quests.registered_quests = quests.registered_quests or {} quests.registered_quests = {}
quests.active_quests = quests.active_quests or {}
quests.successfull_quests = quests.successfull_quests or {} quests.successfull_quests = quests.successfull_quests or {}
quests.failed_quests = quests.failed_quests or {} quests.failed_quests = quests.failed_quests or {}
quests.hud = quests.hud or {} quests.hud = quests.hud or {}
@ -67,11 +68,11 @@ function quests.update_hud(playername)
end end
local counter = 0 local counter = 0
local text = "Open Quests:\n\n" local text = "Open Quests:\n\n"
if (quests.registered_quests[playername] ~= nil) then if (quests.active_quests[playername] ~= nil) then
for questname,questspecs in pairs(quests.registered_quests[playername]) do for questname,questspecs in pairs(quests.active_quests[playername]) do
text = text .. questspecs["title"] .. "\n" text = text .. quests.registered_quests[questname]["title"] .. "\n"
if (questspecs["max"] ~= 1) then if (quests.registered_quests[questname]["max"] ~= 1) then
text = text .." (" .. round(questspecs["value"], 2) .. "/" .. questspecs["max"] .. ")\n" text = text .." (" .. round(questspecs["value"], 2) .. "/" .. quests.registered_quests[questname]["max"] .. ")\n"
end end
counter = counter + 1 counter = counter + 1
if (counter >= show_max) then if (counter >= show_max) then
@ -97,9 +98,8 @@ end
-- registers a quest for the specified player -- registers a quest for later use
-- --
-- playername is the name of the player, who gets the quest
-- questname is the name of the quest to identify it later -- questname is the name of the quest to identify it later
-- it should follow the naming conventions: "modname:questname" -- it should follow the naming conventions: "modname:questname"
-- quest is a table in the following format -- quest is a table in the following format
@ -113,20 +113,38 @@ end
-- --
-- returns true, when the quest was successfully registered -- returns true, when the quest was successfully registered
-- returns falls, when there was already such a quest -- returns falls, when there was already such a quest
function quests.register_quest(playername, questname, quest) function quests.register_quest(questname, quest)
if (quests.registered_quests[playername] == nil) then if (quests.registered_quests[questname] ~= nil) then
quests.registered_quests[playername] = {}
end
if (quests.registered_quests[playername][questname] ~=nil) then
return false -- The quest was not registered since there already a quest with that name return false -- The quest was not registered since there already a quest with that name
end end
quests.registered_quests[playername][questname] = quests.registered_quests[questname] =
{ value = 0, { title = quest.title or "missing title",
title = quest.title or "missing title",
description = quest.description or "missing description", description = quest.description or "missing description",
max = quest.max or 1, max = quest.max or 1,
autoaccept = quest.autoaccept or false, autoaccept = quest.autoaccept or false,
callback = quest.callback, } callback = quest.callback, }
return true
end
-- starts a quest for a specified player
--
-- playername - the name of the player
-- questname - the name of the quest, which was registered with quests.register_quest
--
-- returns false on failure
-- returns true if the quest was started
function quests.start_quest(playername, questname)
if (quests.registered_quests[questname] == nil) then
return false
end
if (quests.active_quests[playername] == nil) then
quests.active_quests[playername] = {}
end
if (quests.active_quests[playername][questname] ~= nil) then
return false -- the player has already this quest
end
quests.active_quests[playername][questname] = {value = 0}
quests.update_hud(playername) quests.update_hud(playername)
return true return true
end end
@ -139,21 +157,21 @@ end
-- returns true if the quest is finished -- returns true if the quest is finished
-- returns false if there is no such quest or the quest continues -- returns false if there is no such quest or the quest continues
function quests.update_quest(playername, questname, value) function quests.update_quest(playername, questname, value)
if (quests.registered_quests[playername] == nil) then if (quests.active_quests[playername] == nil) then
quests.registered_quests[playername] = {} quests.active_quests[playername] = {}
end end
if (quests.registered_quests[playername][questname] == nil) then if (quests.active_quests[playername][questname] == nil) then
return false -- there is no such quest return false -- there is no such quest
end end
if (value == nil) then if (value == nil) then
return false -- no value given return false -- no value given
end end
quests.registered_quests[playername][questname]["value"] = quests.registered_quests[playername][questname]["value"] + value quests.active_quests[playername][questname]["value"] = quests.active_quests[playername][questname]["value"] + value
if (quests.registered_quests[playername][questname]["value"] >= quests.registered_quests[playername][questname]["max"]) then if (quests.active_quests[playername][questname]["value"] >= quests.registered_quests[questname]["max"]) then
quests.registered_quests[playername][questname]["value"] = quests.registered_quests[playername][questname]["max"] quests.active_quests[playername][questname]["value"] = quests.registered_quests[questname]["max"]
if (quests.registered_quests[playername][questname]["autoaccept"]) then if (quests.registered_quests[questname]["autoaccept"]) then
if (quests.registered_quests[playername][questname]["callback"] ~= nil) then if (quests.registered_quests[questname]["callback"] ~= nil) then
quests.registered_quests[playername][questname]["callback"](playername, questname) quests.registered_quests[questname]["callback"](playername, questname)
end end
quests.accept_quest(playername,questname) quests.accept_quest(playername,questname)
quests.update_hud(playername) quests.update_hud(playername)
@ -169,17 +187,16 @@ end
-- returns true, when the quest is completed -- returns true, when the quest is completed
-- 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.active_quests[playername][questname]) then
if (quests.successfull_quests[playername] == nil) then if (quests.successfull_quests[playername] == nil) then
quests.successfull_quests[playername] = {} quests.successfull_quests[playername] = {}
end end
if (quests.successfull_quests[playername][questname] ~= nil) then if (quests.successfull_quests[playername][questname] ~= nil) then
quests.successfull_quests[playername][questname].count = quests.successfull_quests[playername][questname].count + 1 quests.successfull_quests[playername][questname].count = quests.successfull_quests[playername][questname].count + 1
else else
quests.successfull_quests[playername][questname] = quests.registered_quests[playername][questname] quests.successfull_quests[playername][questname] = {count = 1}
quests.successfull_quests[playername][questname].count = 1
end end
quests.registered_quests[playername][questname] = nil quests.active_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
end end
@ -188,24 +205,26 @@ end
-- call this method, when you want to end a quest even when it was not finished -- call this method, when you want to end a quest even when it was not finished
-- example: the player failed -- example: the player failed
--
-- returns false if the quest was not aborted
-- returns true when the quest was aborted
function quests.abort_quest(playername, questname) function quests.abort_quest(playername, questname)
if (questname == nil) then if (questname == nil) then
return return false
end end
if (quests.failed_quests[playername] == nil) then if (quests.failed_quests[playername] == nil) then
quests.failed_quests[playername] = {} quests.failed_quests[playername] = {}
end end
if (quests.registered_quests[playername][questname] == nil) then if (quests.active_quests[playername][questname] == nil) then
return return false
end end
if (quests.failed_quests[playername][questname] ~= nil) then if (quests.failed_quests[playername][questname] ~= nil) then
quests.failed_quests[playername][questname].count = quests.failed_quests[playername][questname].count + 1 quests.failed_quests[playername][questname].count = quests.failed_quests[playername][questname].count + 1
else else
quests.failed_quests[playername][questname] = quests.registered_quests[playername][questname] quests.failed_quests[playername][questname] = { count = 1 }
quests.failed_quests[playername][questname].count = 1
end end
quests.registered_quests[playername][questname] = nil quests.active_quests[playername][questname] = nil
quests.update_hud(playername) quests.update_hud(playername)
end end
@ -218,20 +237,20 @@ function quests.create_formspec(playername, tab)
quests.formspec_lists[playername].list = {} quests.formspec_lists[playername].list = {}
tab = tab or quests.formspec_lists[playername].tab or "1" tab = tab or quests.formspec_lists[playername].tab or "1"
if (tab == "1") then if (tab == "1") then
questlist = quests.registered_quests[playername] questlist = quests.active_quests[playername]
elseif (tab == "2") then elseif (tab == "2") then
questlist = quests.successfull_quests[playername] questlist = quests.successfull_quests[playername] or {}
elseif (tab == "3") then elseif (tab == "3") then
questlist = quests.failed_quests[playername] questlist = quests.failed_quests[playername] or {}
end end
quests.formspec_lists[playername].tab = tab quests.formspec_lists[playername].tab = tab
for questname,questspecs in pairs(questlist) do for questname,questspecs in pairs(questlist) do
local queststring = questspecs["title"] local queststring = quests.registered_quests[questname]["title"]
if (questspecs["count"] and questspecs["count"] > 1) then if (questspecs["count"] and questspecs["count"] > 1) then
queststring = queststring .. " - " .. questspecs["count"] queststring = queststring .. " - " .. questspecs["count"]
elseif(not questspecs["count"] and questspecs["max"] ~= 1) then elseif(not questspecs["count"] and quests.registered_quests[questname]["max"] ~= 1) then
queststring = queststring .. " - (" .. round(questspecs["value"], 2) .. "/" .. questspecs["max"] .. ")" queststring = queststring .. " - (" .. round(questspecs["value"], 2) .. "/" .. quests.registered_quests[questname]["max"] .. ")"
end end
table.insert(queststringlist, queststring) table.insert(queststringlist, queststring)
table.insert(quests.formspec_lists[playername].list, questname) table.insert(quests.formspec_lists[playername].list, questname)
@ -266,17 +285,10 @@ end
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;" "label[0.5,0.5;"
if (quests.formspec_lists[playername].tab == "1") then
formspec = formspec .. quests.registered_quests[playername][questname].title .. "]" ..
"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.registered_quests[questname].title .. "]" ..
formspec = formspec .. quests.successfull_quests[playername][questname].title .. "]".. "textarea[.5,1.5;6,4.5;description;;" .. quests.registered_quests[questname].description .. "]"
"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 if (quests.formspec_lists[playername].tab == "1") then
formspec = formspec .. "button[.5,6;3,.7;abort;Abort quest]" formspec = formspec .. "button[.5,6;3,.7;abort;Abort quest]"
end end
@ -359,7 +371,8 @@ 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,
active_quests = quests.active_quests,
successfull_quests = quests.successfull_quests, successfull_quests = quests.successfull_quests,
failed_quests = quests.failed_quests, failed_quests = quests.failed_quests,
hud = quests.hud})) hud = quests.hud}))