table based triggers

This commit is contained in:
Rubenwardy 2013-02-23 12:02:02 +00:00
parent 0b4ac32bfb
commit 96f692215e
4 changed files with 96 additions and 37 deletions

20
api.lua

@ -31,6 +31,24 @@ dofile(minetest.get_modpath("awards").."/triggers.lua")
-- API Functions -- API Functions
function awards.register_achievement(name,data_table) function awards.register_achievement(name,data_table)
if data_table['trigger'] and data_table['trigger']['type'] then
if data_table['trigger']['type']=="dig" then
local tmp={
award=name,
node=data_table['trigger']['node'],
target=data_table['trigger']['target'],
}
table.insert(awards.onDig,tmp)
elseif data_table['trigger']['type']=="place" then
local tmp={
award=name,
node=data_table['trigger']['node'],
target=data_table['trigger']['target'],
}
table.insert(awards.onPlace,tmp)
end
end
awards['def'][name] = data_table awards['def'][name] = data_table
end end
@ -84,7 +102,7 @@ minetest.register_chatcommand("list_awards", {
params = "", params = "",
description = "list_awards: list your awards", description = "list_awards: list your awards",
func = function(name, param) func = function(name, param)
minetest.chat_send_player(name, "Your awards:"); minetest.chat_send_player(name, name.."'s awards:");
for _, str in pairs(player_data[name].unlocked) do for _, str in pairs(player_data[name].unlocked) do
minetest.chat_send_player(name, str); minetest.chat_send_player(name, str);

@ -10,31 +10,24 @@ dofile(minetest.get_modpath("awards").."/api.lua")
awards.register_achievement("award_mesefind",{ awards.register_achievement("award_mesefind",{
title = "First Mese Find", title = "First Mese Find",
description = "Found some Mese!", description = "Found some Mese!",
trigger={
type="dig",
node="default:mese",
target=1,
},
}) })
awards.register_onDig(function(player,data) awards.register_onDig(function(player,data)
if not data['count']['default'] or not data['count']['default']['mese'] then return nil
return
end
if data['count']['default']['mese'] > 0 then
return "award_mesefind"
end
end) end)
-- First Brick Placed! -- First Brick Placed!
awards.register_achievement("award_foundations",{ awards.register_achievement("award_foundations",{
title = "Foundations", title = "Foundations",
description = "Every house starts from its foundations!", description = "Every house starts from its foundations!",
trigger={
type="place",
node="default:brick",
target=1,
},
}) })
awards.register_onPlace(function(player,data)
if not data['place']['default'] or not data['place']['default']['brick'] then
return
end
if data['place']['default']['brick'] > 0 then
return "award_foundations"
end
end)

@ -16,27 +16,27 @@ The API
* desciption * desciption
* sound [optional] * sound [optional]
* image [optional] * image [optional]
* func [optional] - see below * trigger [optional] [table]
* type - "dig" or "place"
* (for dig/place type) node - the nodes name
* (for dig/place type) target - how many to dig / place
* awards.give_achievement(name,award) * awards.give_achievement(name,award)
* -- gives an award to a player * -- gives an award to a player
* awards.register_onDig(func) * awards.register_onDig(func(player,data))
* -- return award name or null * -- return award name or null
* -- there will be built in versions of this function * awards.register_onPlace(func(player,data))
* awards.register_onPlace(func)
* -- return award name or null * -- return award name or null
* -- there will be built in versions of this function
Player Data Player Data
=========== ===========
A list of data referenced/hashed by the player's name. A list of data referenced/hashed by the player's name.
* player name
* name [string] * name [string]
* getNodeCount('node_name') [function] * count [table] - dig counter
* count [table] - dig counter * modname [table]
* modname [table] * itemname [int]
* itemname [int] * place [table] - place counter
* place [table] - place counter * modname [table]
* modname [table] * itemname [int]
* itemname [int]

@ -47,7 +47,31 @@ minetest.register_on_dignode(function(pos, oldnode, digger)
local data=player_data[playern] local data=player_data[playern]
for i=1,# awards.onDig do for i=1,# awards.onDig do
local res=awards.onDig[i](player,data) local res=nil
if type(awards.onDig[i]) == "function" then
-- run the function
print(i.." is a function")
res=awards.onDig[i](player,data)
elseif type(awards.onDig[i]) == "table" then
-- handle table here
print(i.." is a table")
if not awards.onDig[i]['node'] or not awards.onDig[i]['target'] or not awards.onDig[i]['award'] then
-- table running failed!
else
-- run the table
local tnodedug = string.split(awards.onDig[i]['node'], ":")
local tmod=tnodedug[1]
local titem=tnodedug[2]
if tmod==nil or titem==nil or not data['count'][tmod] or not data['count'][tmod][titem] then
-- table running failed!
elseif data['count'][tmod][titem] > awards.onDig[i]['target']-1 then
res=awards.onDig[i]['award']
end
end
end
if res~=nil then if res~=nil then
awards.give_achievement(playern,res) awards.give_achievement(playern,res)
@ -91,7 +115,31 @@ minetest.register_on_placenode(function(pos, newnode, placer)
local data=player_data[playern] local data=player_data[playern]
for i=1,# awards.onPlace do for i=1,# awards.onPlace do
local res=awards.onPlace[i](player,data) local res=nil
if type(awards.onPlace[i]) == "function" then
-- run the function
print(i.." is a function")
res=awards.onPlace[i](player,data)
elseif type(awards.onPlace[i]) == "table" then
-- handle table here
print(i.." is a table")
if not awards.onPlace[i]['node'] or not awards.onPlace[i]['target'] or not awards.onPlace[i]['award'] then
-- table running failed!
else
-- run the table
local tnodedug = string.split(awards.onPlace[i]['node'], ":")
local tmod=tnodedug[1]
local titem=tnodedug[2]
if tmod==nil or titem==nil or not data['place'][tmod] or not data['place'][tmod][titem] then
-- table running failed!
elseif data['place'][tmod][titem] > awards.onPlace[i]['target']-1 then
res=awards.onPlace[i]['award']
end
end
end
if res~=nil then if res~=nil then
awards.give_achievement(playern,res) awards.give_achievement(playern,res)