2015-07-22 00:04:54 +02:00
|
|
|
-- reading previous quests
|
|
|
|
local file = io.open(minetest.get_worldpath().."/quests", "r")
|
|
|
|
if file then
|
|
|
|
minetest.log("action", "Reading quests...")
|
|
|
|
quests = minetest.deserialize(file:read("*all"))
|
|
|
|
file:close()
|
|
|
|
end
|
|
|
|
quests = quests or {}
|
|
|
|
quests.registered_quests = {}
|
|
|
|
quests.active_quests = quests.active_quests or {}
|
|
|
|
quests.successfull_quests = quests.successfull_quests or {}
|
|
|
|
quests.failed_quests = quests.failed_quests or {}
|
2015-07-27 01:49:32 +02:00
|
|
|
quests.info_quests = quests.info_quests or {}
|
2015-07-22 00:04:54 +02:00
|
|
|
quests.hud = quests.hud or {}
|
|
|
|
for idx,_ in pairs(quests.hud) do
|
|
|
|
quests.hud[idx].first = true
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
quests.formspec_lists = {}
|
2015-07-27 21:45:41 +02:00
|
|
|
function quests.round(num, n)
|
2015-07-22 00:04:54 +02:00
|
|
|
local mult = 10^(n or 0)
|
|
|
|
return math.floor(num * mult + .5) / mult
|
|
|
|
end
|
|
|
|
|
|
|
|
quests.colors = {
|
|
|
|
new = "0xAAAA00",
|
|
|
|
success = "0x00AD00",
|
2015-07-27 01:49:32 +02:00
|
|
|
failed = "0xAD0000",
|
2015-07-22 00:04:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
local MP = minetest.get_modpath("quests")
|
|
|
|
|
2015-07-27 01:49:32 +02:00
|
|
|
function quests.sorted_pairs(t)
|
|
|
|
local a = {}
|
|
|
|
for n in pairs(t) do table.insert(a, n) end
|
|
|
|
table.sort(a)
|
|
|
|
local i = 0 -- iterator variable
|
|
|
|
local iter = function () -- iterator function
|
|
|
|
i = i + 1
|
|
|
|
if a[i] == nil then return nil
|
|
|
|
else return a[i], t[a[i]]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return iter
|
|
|
|
end
|
|
|
|
|
2015-07-22 00:04:54 +02:00
|
|
|
dofile(MP .. "/central_message.lua")
|
|
|
|
dofile(MP .. "/core.lua")
|
|
|
|
dofile(MP .. "/hud.lua")
|
|
|
|
dofile(MP .. "/formspecs.lua")
|
|
|
|
|
|
|
|
-- support for unified_inventory
|
|
|
|
if (minetest.get_modpath("unified_inventory") ~= nil) then
|
|
|
|
dofile(minetest.get_modpath("quests") .. "/unified_inventory.lua")
|
|
|
|
elseif (minetest.get_modpath("inventory_plus") ~= nil) then
|
|
|
|
dofile(minetest.get_modpath("quests") .. "/inventory_plus.lua")
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- write the quests to file
|
2015-07-23 20:40:36 +02:00
|
|
|
minetest.register_on_shutdown(function()
|
2015-07-22 00:04:54 +02:00
|
|
|
for playername, quest in pairs(quests.active_quests) do
|
|
|
|
for questname, questspecs in pairs(quest) do
|
2015-07-27 01:49:32 +02:00
|
|
|
if questspecs.finished then
|
2015-07-22 00:04:54 +02:00
|
|
|
quests.active_quests[playername][questname] = nil -- make sure no finished quests are saved as unfinished
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-07-27 01:49:32 +02:00
|
|
|
local file, err = io.open(minetest.get_worldpath().."/quests", "w")
|
|
|
|
if file then
|
|
|
|
file:write(minetest.serialize({
|
2015-07-22 00:04:54 +02:00
|
|
|
active_quests = quests.active_quests,
|
|
|
|
successfull_quests = quests.successfull_quests,
|
2015-07-27 01:49:32 +02:00
|
|
|
failed_quests = quests.failed_quests,
|
|
|
|
info_quests = quests.info_quests,
|
|
|
|
hud = quests.hud}))
|
2015-07-22 00:04:54 +02:00
|
|
|
file:close()
|
2015-07-27 01:49:32 +02:00
|
|
|
minetest.log("action", "Wrote quests to file")
|
|
|
|
else
|
|
|
|
minetest.log("action", "Failed writing quests to file: open failed: " .. err)
|
2015-07-22 00:04:54 +02:00
|
|
|
end
|
|
|
|
end)
|