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

71
api.lua
View File

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

View File

@ -23,27 +23,86 @@ old fork in Carbone, under same license.
* (for all types) target - how many to dig / place * (for all types) target - how many to dig / place
* See Triggers * See Triggers
* secret [optional] - if true, then player needs to unlock to find out what it is. * 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)) * awards.register_trigger(name, func(awardname, def))
* Note: awards.on[name] is automatically created for triggers * Note: awards.on[name] is automatically created for triggers
* 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) * awards.give_achievement(name, award)
* -- gives an award to a player * gives an award to a player
* name is the player name
# Included in the Mod # 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 ## 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)) * 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)) * 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)) * 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)) * awards.register_on_chat(func(player, data))
* -- return award name or null * data is player data (see below)
* awards.register_on_join(func(player, data)) * return award name or null
* -- return award name or null * awards.register_on_join(func(player, data)
* awards.register_onCraft(func(player,data)) * data is player data (see below)
* -- return award name or null * return award name or null
# Player Data # Player Data

View File

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