quests/formspecs.lua

341 lines
12 KiB
Lua
Raw Normal View History

2015-03-03 22:04:01 +01:00
-- Boilerplate to support localized strings if intllib mod is installed.
local S
if minetest.get_modpath("intllib") then
S = intllib.Getter()
else
-- If you don't use insertions (@1, @2, etc) you can use this:
S = function(s) return s end
end
2015-03-02 20:32:42 +01:00
-- construct the questlog
2015-03-04 20:56:00 +01:00
function quests.create_formspec(playername, tab, integrated)
2015-03-02 20:32:42 +01:00
local queststringlist = {}
local questlist = {}
quests.formspec_lists[playername] = quests.formspec_lists[playername] or {}
quests.formspec_lists[playername].id = 1
quests.formspec_lists[playername].list = {}
tab = tab or quests.formspec_lists[playername].tab or "1"
2015-07-24 22:29:05 +02:00
if tab == "1" then
2015-03-02 20:32:42 +01:00
questlist = quests.active_quests[playername] or {}
2015-07-24 22:29:05 +02:00
elseif tab == "2" then
2015-03-02 20:32:42 +01:00
questlist = quests.successfull_quests[playername] or {}
2015-07-24 22:29:05 +02:00
elseif tab == "3" then
2015-03-02 20:32:42 +01:00
questlist = quests.failed_quests[playername] or {}
end
quests.formspec_lists[playername].tab = tab
2015-07-24 22:29:05 +02:00
2015-03-02 20:32:42 +01:00
local no_quests = true
for questname,questspecs in pairs(questlist) do
2015-07-24 22:29:05 +02:00
if not questspecs.finished then
local quest = quests.registered_quests[questname]
local queststring = quest.title
if quest.simple then
if questspecs.count and questspecs.count > 1 then
queststring = queststring .. " - " .. questspecs.count
elseif not questspecs.count and quest.max ~= 1 then
queststring = queststring .. " - (" .. quests.round(questspecs.value, 2) .. "/" .. quest.max .. ")"
end
else
if questspecs.count and questspecs.count > 1 then
queststring = queststring .. " - " .. questspecs.count
elseif not questspecs.count and quest.max ~= 1 then
queststring = queststring .. " - (...)"
end
2015-03-02 20:32:42 +01:00
end
table.insert(queststringlist, queststring)
table.insert(quests.formspec_lists[playername].list, questname)
no_quests = false
end
end
2015-03-04 20:56:00 +01:00
local formspec = ""
2015-07-24 22:29:05 +02:00
if not integrated then
2015-03-04 20:56:00 +01:00
formspec = formspec .. "size[7,9]"
end
formspec = formspec .. "tabheader[0,0;quests_header;" .. S("Open quests") .. "," .. S("Finished quests") .. "," .. S("Failed quests") .. ";" .. tab .. "]"
2015-07-24 22:29:05 +02:00
if no_quests then
2015-03-03 22:04:01 +01:00
formspec = formspec .. "label[0.25,0.25;" .. S("There are no quests in this category.") .. "]"
2015-03-02 20:32:42 +01:00
else
2015-03-04 20:56:00 +01:00
formspec = formspec .. "textlist[0.25,0.25;6.5,6.5;quests_questlist;"..table.concat(queststringlist, ",") .. ";1;false]"
2015-03-02 20:32:42 +01:00
end
2015-07-24 22:29:05 +02:00
if quests.formspec_lists[playername].tab == "1" then
2015-03-04 20:56:00 +01:00
formspec = formspec .."button[0.25,7;3,.7;quests_abort;" .. S("Abort quest") .. "]"
2015-03-02 20:32:42 +01:00
end
2015-03-04 20:56:00 +01:00
formspec = formspec .. "button[3.75,7;3,.7;quests_config;" .. S("Configure") .. "]"..
"button[.25,8;3,.7;quests_info;" .. S("Info") .. "]"..
"button_exit[3.75,8;3,.7;quests_exit;" .. S("Exit") .. "]"
2015-03-02 20:32:42 +01:00
return formspec
end
-- construct the configuration
2015-03-04 20:56:00 +01:00
function quests.create_config(playername, integrated)
local formspec = ""
if (not integrated) then
formspec = formspec .. "size[7,3]"
end
formspec = formspec .. "checkbox[.25,.25;quests_config_enable;" .. S("Enable HUD") .. ";"
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
2015-03-02 20:32:42 +01:00
formspec = formspec .. "true"
else
formspec = formspec .. "false"
end
formspec = formspec .. "]checkbox[.25,1.25;quests_config_central_message;" .. S("Central messages") .. ";"
if(quests.hud[playername] ~= nil and quests.hud[playername].central_message_enabled) then
formspec = formspec .. "true"
else
formspec = formspec .. "false"
end
formspec = formspec .. "]" ..
"button[.25,2.25;3,.7;quests_config_return;" .. S("Return") .. "]"
2015-03-02 20:32:42 +01:00
return formspec
end
local function wordwrap(text, linelength)
local lines = text:split("\n")
local ret = ""
for i = 1,#lines do
local line = lines[i]
while (#line > linelength) do
local split = false
local j = linelength
while (not split) do
if (string.sub(line, j, j) == " ") then
split = true
ret = ret .. string.sub(line, 1, j) .. "\n"
line = string.sub(line, j + 1)
end
if (j <= 1) then
break
end
j = j - 1
end
if (not split) then
ret = ret .. string.sub(line, 1, linelength) .. "\n"
line = string.sub(line, linelength);
end
end
ret = ret .. line .. "\n"
end
return ret
end
2015-03-02 20:32:42 +01:00
-- construct the info formspec
2015-07-24 22:29:05 +02:00
function quests.create_info(playername, questname, taskid, integrated)
2015-03-04 20:56:00 +01:00
local formspec = ""
2015-07-24 22:29:05 +02:00
if not integrated then
formspec = formspec .. "size[7.5,9]"
2015-03-04 20:56:00 +01:00
end
2015-07-24 22:29:05 +02:00
formspec = formspec .. "label[0.8,0.1;"
2015-03-02 20:32:42 +01:00
2015-07-24 22:29:05 +02:00
if questname then
local quest = quests.registered_quests[questname]
formspec = formspec .. quest.title .. "]" ..
"image[0,0;0.8,0.8;" .. quest.icon .. "]"
2015-03-02 20:32:42 +01:00
2015-07-24 22:29:05 +02:00
if quest.simple then
formspec = formspec .. "textarea[.4,1;7.2,7;_;;" .. minetest.formspec_escape(quest.description) .. "]"
else
quests.formspec_lists[playername].taskid = nil
local taskidlist = {}
local taskstringlist = {}
for taskname, task in pairs(quest.tasks) do
local plr_task = quests.active_quests[playername][questname][taskname]
if not plr_task or (plr_task and plr_task.visible) then
-- not plr_task => quest is finished, display all tasks
table.insert(taskidlist, taskname)
local color = ""
if plr_task then
if plr_task.finished then
color = "#00BB00"
end
if plr_task.disabled then
color = "#AAAAAA"
end
end
table.insert(taskstringlist, color .. task.title .. " - " .. quests.round(plr_task.value, 2) .. "/" .. task.max)
end
end
local task = false
if taskid ~= nil then
task = quest.tasks[taskidlist[taskid]]
end
task = task or {title=S("No task selected"), description=""}
formspec = formspec .. "textarea[.4,1;7.2,2;_;;" .. minetest.formspec_escape(quest.description) .. "]" ..
"textlist[0.1,2.9;7,2;quest_info_tasklist;" .. table.concat(taskstringlist, ",") .. "]" ..
"label[0.8,5.2;" .. task.title .. "]" ..
"textarea[.4,6;7.2,2;__;;" .. minetest.formspec_escape(task.description) .. "]"
if task.icon then
formspec = formspec .. "image[0,5.1;0.8,0.8;" .. task.icon .. "]"
end
end
if quests.formspec_lists[playername].tab == "1" then
formspec = formspec .. "button[3.6,8;3,.7;quests_info_abort;" .. S("Abort quest") .. "]"
2015-03-02 20:32:42 +01:00
end
else
2015-03-03 22:04:01 +01:00
formspec = formspec .. S("No quest specified.") .. "]"
2015-03-02 20:32:42 +01:00
end
2015-07-24 22:29:05 +02:00
formspec = formspec .. "button[.4,8;3,.7;quests_info_return;" .. S("Return") .. "]"
2015-03-02 20:32:42 +01:00
return formspec
end
-- show the player playername his/her questlog
function quests.show_formspec(playername)
minetest.show_formspec(playername, "quests:questlog", quests.create_formspec(playername))
end
-- chatcommand to see a full list of quests:
minetest.register_chatcommand("quests", {
params = "",
2015-03-03 22:04:01 +01:00
description = S("Show all open quests"),
2015-03-02 20:32:42 +01:00
func = function(name, param)
minetest.show_formspec(name, "quests:questlog", quests.create_formspec(name))
return true
end
})
-- Handle the return fields of the questlog
minetest.register_on_player_receive_fields(function(player, formname, fields)
2015-07-24 22:29:05 +02:00
if player == nil then
2015-03-02 20:32:42 +01:00
return
end
2015-07-24 22:29:05 +02:00
local playername = player:get_player_name()
if playername == "" then
2015-03-02 20:32:42 +01:00
return
end
2015-03-04 20:56:00 +01:00
-- questlog
2015-07-24 22:29:05 +02:00
if fields.quests_header then
if formname == "quests:questlog" then
minetest.show_formspec(playername, "quests:questlog", quests.create_formspec(playername, fields.quests_header))
2015-03-04 20:56:00 +01:00
else
2015-07-24 22:29:05 +02:00
if fields.quests_header == "1" then
2015-03-04 20:56:00 +01:00
unified_inventory.set_inventory_formspec(player, "quests")
2015-07-24 22:29:05 +02:00
elseif fields.quests_header == "2" then
2015-03-04 20:56:00 +01:00
unified_inventory.set_inventory_formspec(player, "quests_successfull")
return
else
unified_inventory.set_inventory_formspec(player, "quests_failed")
2015-03-02 20:32:42 +01:00
return
end
2015-03-04 20:56:00 +01:00
end
return
end
if (fields["quests_questlist"]) then
local event = minetest.explode_textlist_event(fields["quests_questlist"])
if (event.type == "CHG") then
quests.formspec_lists[playername].id = event.index
end
end
if (fields["quests_abort"]) then
if (quests.formspec_lists[playername].id == nil) then
return
end
quests.abort_quest(playername, quests.formspec_lists[playername]["list"][quests.formspec_lists[playername].id])
if (formname == "quests:questlog") then
2015-03-02 20:32:42 +01:00
minetest.show_formspec(playername, "quests:questlog", quests.create_formspec(playername))
2015-03-04 20:56:00 +01:00
else
unified_inventory.set_inventory_formspec(player, "quests")
2015-03-02 20:32:42 +01:00
end
2015-03-04 20:56:00 +01:00
end
if (fields["quests_config"]) then
if (formname == "quests:questlog") then
2015-03-02 20:32:42 +01:00
minetest.show_formspec(playername, "quests:config", quests.create_config(playername))
2015-03-04 20:56:00 +01:00
else
unified_inventory.set_inventory_formspec(player, "quests_config")
2015-03-02 20:32:42 +01:00
end
2015-03-04 20:56:00 +01:00
end
if (fields["quests_info"]) then
if (formname == "quests:questlog") then
2015-07-24 22:29:05 +02:00
minetest.show_formspec(playername, "quests:info", quests.create_info(playername, quests.formspec_lists[playername].list[quests.formspec_lists[playername].id], nil, false))
2015-03-04 20:56:00 +01:00
else
unified_inventory.set_inventory_formspec(player, "quests_info")
2015-03-02 20:32:42 +01:00
end
end
2015-03-04 20:56:00 +01:00
-- config
if (fields["quests_config_enable"]) then
quests.hud[playername].autohide = false
2015-03-04 20:56:00 +01:00
if (fields["quests_config_enable"] == "true") then
quests.show_hud(playername)
else
quests.hide_hud(playername)
2015-03-02 20:32:42 +01:00
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
2015-03-04 20:56:00 +01:00
end
if (fields["quests_config_central_message"]) then
if (fields["quests_config_central_message"] == "true") then
quests.hud[playername].central_message_enabled = true
else
quests.hud[playername].central_message_enabled = 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
2015-03-04 20:56:00 +01:00
if (fields["quests_config_return"]) then
if (formname == "quests:config") then
2015-03-02 20:32:42 +01:00
minetest.show_formspec(playername, "quests:questlog", quests.create_formspec(playername))
2015-03-04 20:56:00 +01:00
else
unified_inventory.set_inventory_formspec(player, "quests")
2015-03-02 20:32:42 +01:00
end
end
2015-03-04 20:56:00 +01:00
-- info
2015-07-24 22:29:05 +02:00
if fields.quest_info_tasklist then
local event = minetest.explode_textlist_event(fields.quest_info_tasklist)
if event.type == "CHG" then
if formname == "quests:questlog" then
minetest.show_formspec(playername, "quests:info", quests.create_info(playername, quests.formspec_lists[playername].list[quests.formspec_lists[playername].id]), event.index, false)
else
quests.formspec_lists[playername].taskid = event.index
unified_inventory.set_inventory_formspec(player, "quests_info")
end
end
end
if fields.quests_info_abort then
2015-03-04 20:56:00 +01:00
if (quests.formspec_lists[playername].id == nil) then
return
end
2015-07-24 22:29:05 +02:00
quests.abort_quest(playername, quests.formspec_lists[playername].list[quests.formspec_lists[playername].id])
if formname == "quests:info" then
2015-03-02 20:32:42 +01:00
minetest.show_formspec(playername, "quests:questlog", quests.create_formspec(playername))
2015-03-04 20:56:00 +01:00
else
unified_inventory.set_inventory_formspec(player, "quests")
2015-03-02 20:32:42 +01:00
end
2015-03-04 20:56:00 +01:00
end
2015-07-24 22:29:05 +02:00
if fields.quests_info_return then
if formname == "quests:info" then
2015-03-02 20:32:42 +01:00
minetest.show_formspec(playername, "quests:questlog", quests.create_formspec(playername))
2015-03-04 20:56:00 +01:00
else
unified_inventory.set_inventory_formspec(player, "quests")
2015-03-02 20:32:42 +01:00
end
end
end)