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
```lua
awards.register_achievement(name, {
awards.register_achievement("mymod:myaward", {
description = "The title of the award",
-- 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
-- if not provided, uses default sound
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
type = "trigger_type",
-- 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"
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)
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
get_key = function(self, def)
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
trigger = {
type = "dig",
node = "default:dirt",
target = 50
type = "dig",
node = "default:dirt",
target = 50,
}
### place
trigger = {
type = "place",
node = "default:dirt",
target = 50
type = "place",
node = "default:dirt",
target = 50,
}
### death
trigger = {
type = "death",
target = 5
type = "death",
reason = "fall",
target = 5,
}
### chat
trigger = {
type = "chat",
target = 100
type = "chat",
target = 100,
}
### join
trigger = {
type = "join",
target = 100
type = "join",
target = 100,
}
### eat
trigger = {
type = "eat",
item = "default:apple",
target = 100
type = "eat",
item = "default:apple",
target = 100,
}
## Callbacks relating to triggers

19
api.lua
View File

@ -296,6 +296,19 @@ function awards.register_achievement(name, def)
tdef:on_register(def)
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
awards.registered_awards[name] = def
@ -341,6 +354,12 @@ function awards.unlock(name, award)
return
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
minetest.log("action", name.." has unlocked award "..name)
data.unlocked[award] = award

View File

@ -28,7 +28,7 @@ local function order_awards(name)
end
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 score
if def.secret then