awards/triggers.lua

204 lines
5.7 KiB
Lua
Raw Normal View History

2013-02-22 18:53:42 +01:00
-- AWARDS
-- by Rubenwardy, CC-BY-SA
-------------------------------------------------------
-- this is the trigger handler file for the awards mod
-------------------------------------------------------
2013-02-23 15:53:08 +01:00
-- Function and table holders for Triggers
2013-02-22 18:53:42 +01:00
awards.onDig={}
2013-02-22 21:38:01 +01:00
awards.onPlace={}
2013-02-22 18:53:42 +01:00
awards.onTick={}
2013-02-23 15:53:08 +01:00
awards.onDeath={}
2013-02-22 18:53:42 +01:00
-- Player functions
-- Trigger Handles
minetest.register_on_dignode(function(pos, oldnode, digger)
local nodedug = string.split(oldnode.name, ":")
2013-02-22 19:03:32 +01:00
local mod=nodedug[1]
local item=nodedug[2]
2013-02-22 19:38:09 +01:00
local playern = digger:get_player_name()
2013-02-22 19:03:32 +01:00
2013-02-22 19:38:09 +01:00
if (playern~=nil and nodedug~=nil and mod~=nil and item~=nil) then
--check the player's directory
if not player_data[playern] then
player_data[playern]={}
player_data[playern]['count']={}
player_data[playern]['name']=playern
2013-02-22 18:53:42 +01:00
end
2013-02-22 19:38:09 +01:00
--check player.count.mod
if not player_data[playern]['count'][mod] then
player_data[playern]['count'][mod]={}
2013-02-22 18:53:42 +01:00
end
2013-02-22 19:38:09 +01:00
--check player.count.mod.item
if not player_data[playern]['count'][mod][item] then
player_data[playern]['count'][mod][item]=0
2013-02-22 18:53:42 +01:00
end
2013-02-22 19:38:09 +01:00
player_data[playern]['count'][mod][item]=player_data[playern]['count'][mod][item]+1
print(" - "..mod..":"..item.." 's count is now "..(player_data[playern]['count'][mod][item]))
2013-02-22 19:03:32 +01:00
2013-02-22 19:38:09 +01:00
-- Roll through the onDig functions
local player=digger
local data=player_data[playern]
for i=1,# awards.onDig do
2013-02-23 13:02:02 +01:00
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
2013-02-22 19:38:09 +01:00
2013-02-22 20:35:43 +01:00
if res~=nil then
awards.give_achievement(playern,res)
2013-02-22 19:38:09 +01:00
end
end
2013-02-22 18:53:42 +01:00
end
end)
2013-02-22 21:38:01 +01:00
minetest.register_on_placenode(function(pos, newnode, placer)
local nodedug = string.split(newnode.name, ":")
local mod=nodedug[1]
local item=nodedug[2]
local playern = placer:get_player_name()
if (playern~=nil and nodedug~=nil and mod~=nil and item~=nil) then
--check the player's directory
if not player_data[playern] then
player_data[playern]={}
player_data[playern]['place']={}
player_data[playern]['name']=playern
end
--check player.count.mod
if not player_data[playern]['place'][mod] then
player_data[playern]['place'][mod]={}
end
--check player.count.mod.item
if not player_data[playern]['place'][mod][item] then
player_data[playern]['place'][mod][item]=0
end
player_data[playern]['place'][mod][item]=player_data[playern]['place'][mod][item]+1
print(" - "..mod..":"..item.." 's place is now "..(player_data[playern]['place'][mod][item]))
-- Roll through the onDig functions
local player=placer
local data=player_data[playern]
for i=1,# awards.onPlace do
2013-02-23 13:02:02 +01:00
local res=nil
2013-02-23 15:53:08 +01:00
2013-02-23 13:02:02 +01:00
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
2013-02-22 21:38:01 +01:00
if res~=nil then
awards.give_achievement(playern,res)
end
end
end
end)
2013-02-23 16:13:21 +01:00
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)
2013-02-23 13:02:02 +01:00
2013-02-23 16:13:21 +01:00
minetest.register_on_newplayer(function(player)
2013-02-22 19:38:09 +01:00
--Player data root
2013-02-22 18:53:42 +01:00
player_data[player:get_player_name()]={}
player_data[player:get_player_name()]['name']=player:get_player_name()
2013-02-23 16:13:21 +01:00
player_data[player:get_player_name()]['deaths']=0
2013-02-22 19:38:09 +01:00
--The player counter
player_data[player:get_player_name()]['count']={}
2013-02-22 21:38:01 +01:00
player_data[player:get_player_name()]['place']={}
2013-02-22 19:38:09 +01:00
--Table to contain achievement records
player_data[player:get_player_name()]['unlocked']={}
2013-02-22 20:57:24 +01:00
end)
minetest.register_on_shutdown(function()
save_playerD()
2013-02-22 18:53:42 +01:00
end)