1
0
mirror of https://gitlab.com/rubenwardy/awards.git synced 2024-12-28 03:30:40 +01:00

Add award unlock requirements

Fixes #52
This commit is contained in:
rubenwardy 2018-04-05 17:00:41 +01:00
parent 2f49d895ed
commit e110bbbc7a
3 changed files with 43 additions and 20 deletions

View File

@ -12,11 +12,14 @@ Majority of awards are back ported from Calinou's old fork in Carbone, under sam
## Registering Achievements ## Registering Achievements
```lua ```lua
awards.register_achievement(name, { awards.register_achievement("mymod:myaward", {
description = "The title of the award", description = "The title of the award",
-- Optional: -- Optional:
requires = { "amod:an_award" }, -- don't show this award or allow it to be unlocked
-- until required awards are unlocked
sound = {}, -- SimpleSoundSpec or false to play no sound sound = {}, -- SimpleSoundSpec or false to play no sound
-- if not provided, uses default sound -- if not provided, uses default sound
image = "icon_image.png", -- uses default icon otherwise image = "icon_image.png", -- uses default icon otherwise
@ -25,7 +28,7 @@ awards.register_achievement(name, {
trigger = { -- is only unlocked by direct calls to awards.unlock() otherwise trigger = { -- is only unlocked by direct calls to awards.unlock() otherwise
type = "trigger_type", type = "trigger_type",
-- see specific docs on the trigger to see what else goes here -- see specific docs on the trigger to see what else goes here
} },
}) })
``` ```
@ -38,7 +41,7 @@ local trigger = awards.register_trigger(name, {
progress = "%2/%2" progress = "%2/%2"
auto_description = { "Mine: @2", "Mine: @1×@2" }, auto_description = { "Mine: @2", "Mine: @1×@2" },
on_register = function(self, def) end on_register = function(self, def) end,
-- "counted_key" only, when no key is given (ie: a total) -- "counted_key" only, when no key is given (ie: a total)
auto_description_total = { "Mine @1 block.", "Mine @1 blocks." }, auto_description_total = { "Mine @1 block.", "Mine @1 blocks." },
@ -46,7 +49,7 @@ local trigger = awards.register_trigger(name, {
-- "counted_key" only, get key for particular award - return nil for a total -- "counted_key" only, get key for particular award - return nil for a total
get_key = function(self, def) get_key = function(self, def)
return minetest.registered_aliases[def.trigger.node] or def.trigger.node return minetest.registered_aliases[def.trigger.node] or def.trigger.node
end end,
}) })
``` ```
@ -107,46 +110,47 @@ Callbacks (register a function to be run)
### dig ### dig
trigger = { trigger = {
type = "dig", type = "dig",
node = "default:dirt", node = "default:dirt",
target = 50 target = 50,
} }
### place ### place
trigger = { trigger = {
type = "place", type = "place",
node = "default:dirt", node = "default:dirt",
target = 50 target = 50,
} }
### death ### death
trigger = { trigger = {
type = "death", type = "death",
target = 5 reason = "fall",
target = 5,
} }
### chat ### chat
trigger = { trigger = {
type = "chat", type = "chat",
target = 100 target = 100,
} }
### join ### join
trigger = { trigger = {
type = "join", type = "join",
target = 100 target = 100,
} }
### eat ### eat
trigger = { trigger = {
type = "eat", type = "eat",
item = "default:apple", item = "default:apple",
target = 100 target = 100,
} }
## Callbacks relating to triggers ## Callbacks relating to triggers

19
api.lua
View File

@ -296,6 +296,19 @@ function awards.register_achievement(name, def)
tdef:on_register(def) tdef:on_register(def)
end end
function def:can_unlock(data)
if not self.requires then
return true
end
for i=1, #self.requires do
if not data.unlocked[self.requires[i]] then
return false
end
end
return true
end
-- Add Award -- Add Award
awards.registered_awards[name] = def awards.registered_awards[name] = def
@ -341,6 +354,12 @@ function awards.unlock(name, award)
return return
end end
if not awdef:can_unlock(data) then
minetest.log("warning", "can_unlock returned false in unlock of " ..
award .. " for " .. name)
return
end
-- Unlock Award -- Unlock Award
minetest.log("action", name.." has unlocked award "..name) minetest.log("action", name.." has unlocked award "..name)
data.unlocked[award] = award data.unlocked[award] = award

View File

@ -28,7 +28,7 @@ local function order_awards(name)
end end
for _, def in pairs(awards.registered_awards) do for _, def in pairs(awards.registered_awards) do
if not hash_is_unlocked[def.name] then if not hash_is_unlocked[def.name] and def:can_unlock(data) then
local started = false local started = false
local score local score
if def.secret then if def.secret then