register_on_unlock and on_unlock

This commit is contained in:
rubenwardy 2015-06-10 19:15:16 +01:00
parent cfe11a57c7
commit 30aee5090f
4 changed files with 205 additions and 143 deletions

73
api.lua
View File

@ -33,6 +33,7 @@ function awards.init()
awards.def = {}
awards.trigger_types = {}
awards.on = {}
awards.on_unlock = {}
end
function awards.load()
@ -56,18 +57,24 @@ function awards.register_trigger(name, func)
end
end
function awards.register_on_unlock(func)
table.insert(awards.on_unlock, func)
end
-- API Functions
function awards._additional_triggers(name, def)
-- Depreciated!
end
function awards.register_achievement(name, def)
def.name = name
-- Add Triggers
if def.trigger and def.trigger.type then
local func = awards.trigger_types[def.trigger.type]
if func then
func(name, def)
func(def)
else
awards._additional_triggers(name, def)
end
@ -85,7 +92,6 @@ function awards.register_achievement(name, def)
end
-- add the achievement to the definition table
def.name = name
awards.def[name] = def
end
@ -104,30 +110,34 @@ end
function awards.give_achievement(name, award)
-- Access Player Data
local data = awards.players[name]
local awdef = awards.def[award]
-- Perform checks
if not data then
return
end
if not awards.def[award] then
if not awdef then
return
end
awards.tbv(data,"unlocked")
-- check to see if the player does not already have that achievement
if not data.unlocked[award] or data.unlocked[award]~=award then
-- Don't give the achievement if it has already been given
if data.unlocked[award] and data.unlocked[award] == award then
return
end
-- Set award flag
data.unlocked[award]=award
data.unlocked[award] = award
-- Give Prizes
if awards.def[award] and awards.def[award].prizes then
for i = 1, #awards.def[award].prizes do
local itemstack = ItemStack(awards.def[award].prizes[i])
if awdef and awdef.prizes then
for i = 1, #awdef.prizes do
local itemstack = ItemStack(awdef.prizes[i])
if itemstack:is_empty() or not itemstack:is_known() then
return
end
local receiverref = core.get_player_by_name(name)
if receiverref == nil then
if not receiverref then
return
end
receiverref:get_inventory():add_item("main", itemstack)
@ -140,20 +150,36 @@ function awards.give_achievement(name, award)
local background = ""
local icon = ""
local custom_announce = ""
if awards.def[award].title then
title = awards.def[award].title
if awdef.title then
title = awdef.title
end
if awards.def[award].custom_announce then
custom_announce = awards.def[award].custom_announce
if awdef.custom_announce then
custom_announce = awdef.custom_announce
end
if awards.def[award].background then
background = awards.def[award].background
if awdef.background then
background = awdef.background
end
if awards.def[award].icon then
icon = awards.def[award].icon
if awdef.icon then
icon = awdef.icon
end
if awdef and awdef.description then
desc = awdef.description
end
-- Record this in the log
minetest.log("action", name.." has unlocked award "..title)
-- Save playertable
awards.save()
-- Run callbacks
if awdef.on_unlock and awdef.on_unlock(name, awdef) then
return
end
for _, callback in pairs(awards.on_unlock) do
if callback(name, awdef) then
return
end
if awards.def[award] and awards.def[award].description then
desc = awards.def[award].description
end
-- send the won award message to the player
@ -217,13 +243,6 @@ function awards.give_achievement(name, award)
player:hud_remove(four)
end)
end
-- record this in the log
minetest.log("action", name.." has unlocked award "..title)
-- save playertable
awards.save()
end
end
--[[minetest.register_chatcommand("gawd", {

View File

@ -1,19 +1,3 @@
-- AWARDS
--
-- Copyright (C) 2013-2015 rubenwardy
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU Lesser General Public License as published by
-- the Free Software Foundation; either version 2.1 of the License, or
-- (at your option) any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU Lesser General Public License for more details.
-- You should have received a copy of the GNU Lesser General Public License along
-- with this program; if not, write to the Free Software Foundation, Inc.,
-- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
--
function awards.tbv(tb,value,default)
if not default then
default = {}

View File

@ -23,27 +23,86 @@ old fork in Carbone, under same license.
* (for all types) target - how many to dig / place
* See Triggers
* secret [optional] - if true, then player needs to unlock to find out what it is.
* on_unlock [optional] - func(name, def)
* name is player name
* return true to cancel register_on_unlock callbacks and HUD
* awards.register_trigger(name, func(awardname, def))
* Note: awards.on[name] is automatically created for triggers
* awards.give_achievement(name,award)
* -- gives an award to a player
* awards.register_on_unlock(func(name, def))
* name is the player name
* def is the award def.
* return true to cancel HUD
* awards.give_achievement(name, award)
* gives an award to a player
* name is the player name
# Included in the Mod
The API, above, allows you to register awards
and triggers (things that look for events and unlock awards, they need
to be registered in order to get details from award_def.trigger).
However, all awards and triggers are separate from the API.
They can be found in init.lua and triggers.lua
## Triggers
Callbacks (register a function to be run)
### dig
trigger = {
type = "dig",
node = "default:dirt",
target = 50
}
### place
trigger = {
type = "place",
node = "default:dirt",
target = 50
}
### death
trigger = {
type = "death",
target = 5
}
### chat
trigger = {
type = "chat",
target = 100
}
### join
trigger = {
type = "join",
target = 100
}
## Callbacks relating to triggers
* awards.register_on_dig(func(player, data))
* -- return award name or null
* data is player data (see below)
* return award name or null
* awards.register_on_place(func(player, data))
* -- return award name or null
* data is player data (see below)
* return award name or null
* awards.register_on_death(func(player, data))
* -- return award name or null
* data is player data (see below)
* return award name or null
* awards.register_on_chat(func(player, data))
* -- return award name or null
* awards.register_on_join(func(player, data))
* -- return award name or null
* awards.register_onCraft(func(player,data))
* -- return award name or null
* data is player data (see below)
* return award name or null
* awards.register_on_join(func(player, data)
* data is player data (see below)
* return award name or null
# Player Data

View File

@ -14,43 +14,43 @@
-- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
--
awards.register_trigger("dig", function(name, def)
awards.register_trigger("dig", function(def)
local tmp = {
award = name,
award = def.name,
node = def.trigger.node,
target = def.trigger.target
}
table.insert(awards.on.dig, tmp)
end)
awards.register_trigger("place", function(name, def)
awards.register_trigger("place", function(def)
local tmp = {
award = name,
award = def.name,
node = def.trigger.node,
target = def.trigger.target
}
table.insert(awards.on.place, tmp)
end)
awards.register_trigger("death", function(name, def)
awards.register_trigger("death", function(def)
local tmp = {
award = name,
award = def.name,
target = def.trigger.target
}
table.insert(awards.on.death, tmp)
end)
awards.register_trigger("chat", function(name, def)
awards.register_trigger("chat", function(def)
local tmp = {
award = name,
award = def.name,
target = def.trigger.target
}
table.insert(awards.on.chat, tmp)
end)
awards.register_trigger("join", function(name, def)
awards.register_trigger("join", function(def)
local tmp = {
award = name,
award = def.name,
target = def.trigger.target
}
table.insert(awards.on.join, tmp)