From 4cf05cbc47b5ddab616170097673947d69e93a29 Mon Sep 17 00:00:00 2001 From: Rubenwardy Date: Sat, 23 Feb 2013 15:13:21 +0000 Subject: [PATCH] add onDeath --- api.lua | 10 ++++++++++ init.lua | 10 ++++++++++ readme.md | 4 +++- triggers.lua | 39 +++++++++++++++++++++++++++++++++++++-- 4 files changed, 60 insertions(+), 3 deletions(-) diff --git a/api.lua b/api.lua index 6b51195..b80b797 100644 --- a/api.lua +++ b/api.lua @@ -46,6 +46,12 @@ function awards.register_achievement(name,data_table) target=data_table['trigger']['target'], } table.insert(awards.onPlace,tmp) + elseif data_table['trigger']['type']=="death" then + local tmp={ + award=name, + target=data_table['trigger']['target'], + } + table.insert(awards.onDeath,tmp) end end @@ -60,6 +66,10 @@ function awards.register_onPlace(func) table.insert(awards.onPlace,func); end +function awards.register_onDeath(func) + table.insert(awards.onDeath,func); +end + function awards.give_achievement(name,award) local data=player_data[name] diff --git a/init.lua b/init.lua index 1573063..f538d87 100644 --- a/init.lua +++ b/init.lua @@ -81,4 +81,14 @@ awards.register_achievement("award_mine4",{ node="default:stone", target=10000, }, +}) + +-- First Death +awards.register_achievement("award_death1",{ + title = "First Death", + description = "Oh well, it does not matter you have more lives than a cat", + trigger={ + type="death", + target=1, + }, }) \ No newline at end of file diff --git a/readme.md b/readme.md index 26b2fff..8b011ed 100644 --- a/readme.md +++ b/readme.md @@ -19,13 +19,15 @@ The API * 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 + * (for all types) target - how many to dig / place * awards.give_achievement(name,award) * -- gives an award to a player * awards.register_onDig(func(player,data)) * -- return award name or null * awards.register_onPlace(func(player,data)) * -- return award name or null +* awards.register_onDeath(func(player,data)) + * -- return award name or null Player Data diff --git a/triggers.lua b/triggers.lua index 8b51b01..0f985fe 100644 --- a/triggers.lua +++ b/triggers.lua @@ -149,12 +149,47 @@ minetest.register_on_placenode(function(pos, newnode, placer) end end) -minetest.register_on_newplayer(function(player) - minetest.chat_send_player(player:get_player_name(),"[Awards] Registering you now...") +minetest.register_on_dieplayer(function(player) + player_data[player:get_player_name()]['deaths']=player_data[player:get_player_name()]['deaths']+1 + + -- Set up the variables + local playern=player:get_player_name() + local data=player_data[playern] + + -- Roll through the onDeath functions + for i=1,# awards.onDeath do + local res=nil + if type(awards.onDeath[i]) == "function" then + -- run the function + print(i.." is a function") + res=awards.onDeath[i](player,data) + elseif type(awards.onDeath[i]) == "table" then + -- handle table here + print(i.." is a table") + if not awards.onDeath[i]['target'] or not awards.onDeath[i]['award'] then + -- table running failed! + else + -- run the table + if not data['deaths'] then + -- table running failed! + elseif data['deaths'] > awards.onDeath[i]['target']-1 then + res=awards.onDeath[i]['award'] + end + end + end + + if res~=nil then + awards.give_achievement(playern,res) + end + end +end) + +minetest.register_on_newplayer(function(player) --Player data root player_data[player:get_player_name()]={} player_data[player:get_player_name()]['name']=player:get_player_name() + player_data[player:get_player_name()]['deaths']=0 --The player counter player_data[player:get_player_name()]['count']={}