From d5f95d3be907d3aeb9fc44761ca8198c9e598b56 Mon Sep 17 00:00:00 2001 From: FaceDeer Date: Sat, 11 Feb 2023 17:04:25 -0700 Subject: [PATCH] quick and dirty locking and unlocking via cairns --- collectible_lore/init.lua | 23 ++++++++++++++++- collectible_lore/items.lua | 53 +++++++++++++++++++++++++++++++++++--- 2 files changed, 71 insertions(+), 5 deletions(-) diff --git a/collectible_lore/init.lua b/collectible_lore/init.lua index d80f505..6aa7730 100644 --- a/collectible_lore/init.lua +++ b/collectible_lore/init.lua @@ -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) @@ -32,4 +52,5 @@ collectible_lore.register_lorebook = function(def) ids[def.id] = def table.insert(collectible_lore.lorebooks, def) table.sort(collectible_lore.lorebooks, collectible_lore_sort) -end \ No newline at end of file +end + diff --git a/collectible_lore/items.lua b/collectible_lore/items.lua index 24a3eb4..0edda2a 100644 --- a/collectible_lore/items.lua +++ b/collectible_lore/items.lua @@ -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("")) + 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)