Expand hint system a bit

- Add reset command
- Reset alerts on resetting state
- Add option to hide hints
- Add option to add custom hint attrs
- Add option to pass in an already-fully-formed
  hint object and skip construction helpers
- Tidy up terrain hints a bit
This commit is contained in:
Aaron Suen 2020-09-05 16:08:17 -04:00
parent 065bef313c
commit 05d985c635
7 changed files with 62 additions and 15 deletions

View File

@ -20,8 +20,8 @@ nodecore.register_on_joinplayer("join hint setup", function(player)
msgcache[pname] = {}
end)
nodecore.register_on_discover(function(player)
local pname = player:get_player_name()
nodecore.register_on_discover(function(_, key, pname)
if not key then donecache[pname] = {} end
local dc = donecache[pname]
if not dc then return end
local mc = msgcache[pname]

View File

@ -32,22 +32,25 @@ local function loaddb(p)
cache[pname] = db
end
return db, player, pname
return db, player, pname, function()
player:get_meta():set_string(modname, minetest.serialize(db))
end
end
nodecore.get_player_discovered = loaddb
local function discover(p, k)
local db, player, pname = loaddb(p)
local db, player, pname, save = loaddb(p)
if not db then return end
if db[k] then return end
db[k] = true
player:get_meta():set_string(modname, minetest.serialize(db))
minetest.log("action", string_format("player %q discovered %q", pname, k))
for _, cb in pairs(nodecore.registered_on_discovers) do
cb(player, k, pname, db)
end
save()
end
nodecore.player_discover = discover

View File

@ -11,3 +11,4 @@ include("witness")
include("register")
include("state")
include("alerts")
include("reset")

View File

@ -1,6 +1,6 @@
-- LUALOCALS < ---------------------------------------------------------
local nodecore, type
= nodecore, type
local nodecore, pairs, type
= nodecore, pairs, type
-- LUALOCALS > ---------------------------------------------------------
nodecore.hints = {}
@ -29,14 +29,23 @@ local function conv(spec)
return function(db) return db[spec] end
end
function nodecore.register_hint(text, goal, reqs)
function nodecore.register_hint(text, goal, reqs, ext)
local hints = nodecore.hints
if type(text) == "table" then
hints[#hints + 1] = text
return text
end
local t = nodecore.translate(text)
local h = {
text = t,
goal = conv(goal),
reqs = conv(reqs)
}
if ext then
for k, v in pairs(ext) do
h[k] = v
end
end
hints[#hints + 1] = h
return h
end

View File

@ -0,0 +1,25 @@
-- LUALOCALS < ---------------------------------------------------------
local minetest, next, nodecore, pairs
= minetest, next, nodecore, pairs
-- LUALOCALS > ---------------------------------------------------------
minetest.register_chatcommand("tabula", {
desciption = "Reset NodeCore hints",
params = "rasa",
func = function(name, param)
if param ~= "rasa" then
return false, "If you're sure you want to erase all hint"
.. "progress, use the command \"/tabula rasa\""
end
local db, player, pname, save = nodecore.get_player_discovered(name)
if not db then
return false, "Must be online to use this command"
end
while next(db) do db[next(db)] = nil end
for _, cb in pairs(nodecore.registered_on_discovers) do
cb(player, nil, pname, db)
end
save()
return true, "All hints reset"
end
})

View File

@ -37,7 +37,7 @@ function nodecore.hint_state(pspec)
for _, hint in ipairs(nodecore.hints) do
if hint.goal(db, pname, player) then
done[#done + 1] = hint
elseif hint.reqs(db, pname, player) then
elseif (not hint.hide) and hint.reqs(db, pname, player) then
found[#found + 1] = hint
end
end

View File

@ -3,27 +3,36 @@ local nodecore
= nodecore
-- LUALOCALS > ---------------------------------------------------------
local function dug(n)
return {true,
"dig:nc_terrain:" .. n .. "_loose",
"dig:nc_terrain:" .. n,
"inv:nc_terrain:" .. n .. "_loose",
"inv:nc_terrain:" .. n,
}
end
nodecore.register_hint("dig up dirt",
"dig:nc_terrain:dirt_loose"
dug("dirt")
)
nodecore.register_hint("dig up gravel",
"dig:nc_terrain:gravel_loose",
dug("gravel"),
"toolcap:crumbly:2"
)
nodecore.register_hint("dig up sand",
"dig:nc_terrain:sand_loose"
dug("sand")
)
nodecore.register_hint("dig up stone",
"dig:nc_terrain:cobble_loose",
nodecore.register_hint("dig up cobble",
dug("cobble"),
"toolcap:cracky:2"
)
nodecore.register_hint("find deep stone strata",
"group:hard_stone",
"nc_terrain:cobble_loose"
dug("cobble")
)
nodecore.register_hint("find molten rock",