quick and dirty locking and unlocking via cairns

This commit is contained in:
FaceDeer 2023-02-11 17:04:25 -07:00
parent c505f7f3ce
commit d5f95d3be9
2 changed files with 71 additions and 5 deletions

View File

@ -1,8 +1,28 @@
local modpath = minetest.get_modpath(minetest.get_current_modname())
local S = minetest.get_translator(minetest.get_current_modname())
local modmeta = minetest.get_mod_storage()
collectible_lore = {}
collectible_lore.get_player_unlocks = function(player_name)
local unlocks_string = modmeta:get("player_" .. player_name)
if unlocks_string == nil then
return {}
else
return minetest.deserialize(unlocks_string)
end
end
local set_player_unlocks = function(player_name, unlocks_table)
modmeta:set_string("player_" .. player_name, minetest.serialize(unlocks_table))
end
collectible_lore.unlock = function(player_name, id)
local unlocks = collectible_lore.get_player_unlocks(player_name)
unlocks[id] = true
set_player_unlocks(player_name, unlocks)
end
dofile(modpath.."/items.lua")
local collectible_lore_sort = function(first, second)
@ -33,3 +53,4 @@ collectible_lore.register_lorebook = function(def)
table.insert(collectible_lore.lorebooks, def)
table.sort(collectible_lore.lorebooks, collectible_lore_sort)
end

View File

@ -1,4 +1,32 @@
local S = minetest.get_translator(minetest.get_current_modname())
local modmeta = minetest.get_mod_storage()
local get_cairn_looted_by_list = function(pos)
local loot_list_string = modmeta:get("cairn_" .. minetest.pos_to_string(pos))
if not loot_list_string then
return {}
end
return minetest.deserialize(loot_list_string)
end
local set_cairn_looted_by_list = function(pos, list)
modmeta:set_string("cairn_" .. minetest.pos_to_string(pos), minetest.serialize(list))
end
local cairn_loot = function(pos, player_name)
local list = get_cairn_looted_by_list(pos)
if list[player_name] then
return false
end
list[player_name] = true
local lore_id = collectible_lore.lorebooks[math.random(#(collectible_lore.lorebooks))].id
collectible_lore.unlock(player_name, lore_id)
minetest.debug("unlocked " .. lore_id)
list[player_name] = true
set_cairn_looted_by_list(pos, list)
return true
end
minetest.register_node("collectible_lore:cairn", {
description = S("Cairn"),
@ -27,24 +55,37 @@ minetest.register_node("collectible_lore:cairn", {
{-0.5, -0.5, -0.5, 0.5, 1, 0.5}
}
},
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
local player_name = clicker:get_player_name()
if player_name then
cairn_loot(pos, player_name)
end
end,
})
local player_state = {}
local get_formspec_for_player = function(player_name)
local selected
local state = player_state[player_name] or 1
local unlocks = collectible_lore.get_player_unlocks(player_name)
local form = {}
table.insert(form, "formspec_version[6]size[10,8]")
table.insert(form, "textlist[0,0;4,7;list;")
local count = 1
for index, value in pairs(collectible_lore.lorebooks) do
if state == count then
local unlocked = unlocks[value.id]
if unlocked and state == count then
selected = value
end
count = count + 1
table.insert(form, minetest.formspec_escape(value.title))
if unlocked then
table.insert(form, minetest.formspec_escape(value.title))
else
table.insert(form, S("<Locked>"))
end
table.insert(form, ",")
end
table.remove(form) -- removes trailing comma
@ -52,8 +93,12 @@ local get_formspec_for_player = function(player_name)
table.insert(form, "textarea[4.5,0;5.5,7;;text;")
local str = selected.text
table.insert(form, minetest.formspec_escape(str))
if selected then
local str = selected.text
table.insert(form, minetest.formspec_escape(str))
else
table.insert(form, " ")
end
table.insert(form, "]")
return table.concat(form)