From 96f692215eb4f937587a0721d8580820cbbef4ce Mon Sep 17 00:00:00 2001 From: Rubenwardy Date: Sat, 23 Feb 2013 12:02:02 +0000 Subject: [PATCH] table based triggers --- api.lua | 20 ++++++++++++++++++- init.lua | 31 ++++++++++++------------------ readme.md | 28 +++++++++++++-------------- triggers.lua | 54 +++++++++++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 96 insertions(+), 37 deletions(-) diff --git a/api.lua b/api.lua index 05b6306..6b51195 100644 --- a/api.lua +++ b/api.lua @@ -31,6 +31,24 @@ dofile(minetest.get_modpath("awards").."/triggers.lua") -- API Functions 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 end @@ -84,7 +102,7 @@ minetest.register_chatcommand("list_awards", { params = "", description = "list_awards: list your awards", 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 minetest.chat_send_player(name, str); diff --git a/init.lua b/init.lua index 6292254..e9214df 100644 --- a/init.lua +++ b/init.lua @@ -10,31 +10,24 @@ dofile(minetest.get_modpath("awards").."/api.lua") awards.register_achievement("award_mesefind",{ title = "First Mese Find", description = "Found some Mese!", + trigger={ + type="dig", + node="default:mese", + target=1, + }, }) awards.register_onDig(function(player,data) - if not data['count']['default'] or not data['count']['default']['mese'] then - return - end - - if data['count']['default']['mese'] > 0 then - return "award_mesefind" - end + return nil end) - -- First Brick Placed! awards.register_achievement("award_foundations",{ title = "Foundations", description = "Every house starts from its foundations!", -}) - -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) \ No newline at end of file + trigger={ + type="place", + node="default:brick", + target=1, + }, +}) \ No newline at end of file diff --git a/readme.md b/readme.md index 917f2aa..26b2fff 100644 --- a/readme.md +++ b/readme.md @@ -16,27 +16,27 @@ The API * desciption * sound [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) * -- gives an award to a player -* awards.register_onDig(func) +* awards.register_onDig(func(player,data)) * -- return award name or null - * -- there will be built in versions of this function -* awards.register_onPlace(func) +* awards.register_onPlace(func(player,data)) * -- return award name or null - * -- there will be built in versions of this function Player Data =========== A list of data referenced/hashed by the player's name. - -* name [string] -* getNodeCount('node_name') [function] -* count [table] - dig counter - * modname [table] - * itemname [int] -* place [table] - place counter - * modname [table] - * itemname [int] \ No newline at end of file +* player name + * name [string] + * count [table] - dig counter + * modname [table] + * itemname [int] + * place [table] - place counter + * modname [table] + * itemname [int] \ No newline at end of file diff --git a/triggers.lua b/triggers.lua index 2ce81de..03d7703 100644 --- a/triggers.lua +++ b/triggers.lua @@ -47,7 +47,31 @@ minetest.register_on_dignode(function(pos, oldnode, digger) local data=player_data[playern] 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 awards.give_achievement(playern,res) @@ -91,7 +115,31 @@ minetest.register_on_placenode(function(pos, newnode, placer) local data=player_data[playern] 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 awards.give_achievement(playern,res) @@ -102,7 +150,7 @@ end) minetest.register_on_newplayer(function(player) minetest.chat_send_player(player:get_player_name(),"[Awards] Registering you now...") - + --Player data root player_data[player:get_player_name()]={} player_data[player:get_player_name()]['name']=player:get_player_name()