summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrubenwardy <rubenwardy@gmail.com>2015-06-10 19:15:16 +0100
committerrubenwardy <rubenwardy@gmail.com>2016-07-29 16:02:14 +0100
commit30aee5090f65b58c70f888ea203ef2e73fbd4744 (patch)
tree56e32c2f2fa08e3f98ed5d3ec206275f3edfb85b
parentcfe11a57c71b1d84db35897b591fa66efac0c860 (diff)
register_on_unlock and on_unlock
-rw-r--r--api.lua233
-rw-r--r--helpers.lua16
-rw-r--r--readme.md79
-rw-r--r--triggers.lua20
4 files changed, 205 insertions, 143 deletions
diff --git a/api.lua b/api.lua
index 63119b3..a7c9506 100644
--- a/api.lua
+++ b/api.lua
@@ -33,6 +33,7 @@ function awards.init()
awards.def = {}
awards.trigger_types = {}
awards.on = {}
+ awards.on_unlock = {}
end
function awards.load()
@@ -56,18 +57,24 @@ function awards.register_trigger(name, func)
end
end
+function awards.register_on_unlock(func)
+ table.insert(awards.on_unlock, func)
+end
+
-- API Functions
function awards._additional_triggers(name, def)
-- Depreciated!
end
function awards.register_achievement(name, def)
+ def.name = name
+
-- Add Triggers
if def.trigger and def.trigger.type then
local func = awards.trigger_types[def.trigger.type]
if func then
- func(name, def)
+ func(def)
else
awards._additional_triggers(name, def)
end
@@ -85,7 +92,6 @@ function awards.register_achievement(name, def)
end
-- add the achievement to the definition table
- def.name = name
awards.def[name] = def
end
@@ -103,126 +109,139 @@ end
-- award - the name of the award to give
function awards.give_achievement(name, award)
-- Access Player Data
- local data = awards.players[name]
+ local data = awards.players[name]
+ local awdef = awards.def[award]
-- Perform checks
if not data then
return
end
- if not awards.def[award] then
+ if not awdef then
return
end
awards.tbv(data,"unlocked")
- -- check to see if the player does not already have that achievement
- if not data.unlocked[award] or data.unlocked[award]~=award then
- -- Set award flag
- data.unlocked[award]=award
-
- -- Give Prizes
- if awards.def[award] and awards.def[award].prizes then
- for i = 1, #awards.def[award].prizes do
- local itemstack = ItemStack(awards.def[award].prizes[i])
- if itemstack:is_empty() or not itemstack:is_known() then
- return
- end
- local receiverref = core.get_player_by_name(name)
- if receiverref == nil then
- return
- end
- receiverref:get_inventory():add_item("main", itemstack)
- end
- end
+ -- Don't give the achievement if it has already been given
+ if data.unlocked[award] and data.unlocked[award] == award then
+ return
+ end
- -- Get data from definition tables
- local title = award
- local desc = ""
- local background = ""
- local icon = ""
- local custom_announce = ""
- if awards.def[award].title then
- title = awards.def[award].title
- end
- if awards.def[award].custom_announce then
- custom_announce = awards.def[award].custom_announce
- end
- if awards.def[award].background then
- background = awards.def[award].background
- end
- if awards.def[award].icon then
- icon = awards.def[award].icon
- end
- if awards.def[award] and awards.def[award].description then
- desc = awards.def[award].description
- end
+ -- Set award flag
+ data.unlocked[award] = award
- -- send the won award message to the player
- if awards.show_mode == "formspec" then
- -- use a formspec to send it
- minetest.show_formspec(name, "achievements:unlocked", "size[4,2]"..
- "image_button_exit[0,0;4,2;"..background..";close1; ]"..
- "image_button_exit[0.2,0.8;1,1;"..icon..";close2; ]"..
- "label[1.1,1;"..title.."]"..
- "label[0.3,0.1;"..custom_announce.."]")
- elseif awards.show_mode == "chat" then
- -- use the chat console to send it
- minetest.chat_send_player(name, "Achievement Unlocked: "..title)
- if desc~="" then
- minetest.chat_send_player(name, desc)
+ -- Give Prizes
+ if awdef and awdef.prizes then
+ for i = 1, #awdef.prizes do
+ local itemstack = ItemStack(awdef.prizes[i])
+ if itemstack:is_empty() or not itemstack:is_known() then
+ return
end
- else
- local player = minetest.get_player_by_name(name)
- local one = player:hud_add({
- hud_elem_type = "image",
- name = "award_bg",
- scale = {x = 1, y = 1},
- text = background,
- position = {x = 0.5, y = 0},
- offset = {x = 0, y = 138},
- alignment = {x = 0, y = -1}
- })
- local two = player:hud_add({
- hud_elem_type = "text",
- name = "award_au",
- number = 0xFFFFFF,
- scale = {x = 100, y = 20},
- text = "Achievement Unlocked!",
- position = {x = 0.5, y = 0},
- offset = {x = 0, y = 40},
- alignment = {x = 0, y = -1}
- })
- local three = player:hud_add({
- hud_elem_type = "text",
- name = "award_title",
- number = 0xFFFFFF,
- scale = {x = 100, y = 20},
- text = title,
- position = {x = 0.5, y = 0},
- offset = {x = 30, y = 100},
- alignment = {x = 0, y = -1}
- })
- local four = player:hud_add({
- hud_elem_type = "image",
- name = "award_icon",
- scale = {x = 4, y = 4},
- text = icon,
- position = {x = 0.5, y = 0},
- offset = {x = -81.5, y = 126},
- alignment = {x = 0, y = -1}
- })
- minetest.after(3, function()
- player:hud_remove(one)
- player:hud_remove(two)
- player:hud_remove(three)
- player:hud_remove(four)
- end)
+ local receiverref = core.get_player_by_name(name)
+ if not receiverref then
+ return
+ end
+ receiverref:get_inventory():add_item("main", itemstack)
end
+ end
+
+ -- Get data from definition tables
+ local title = award
+ local desc = ""
+ local background = ""
+ local icon = ""
+ local custom_announce = ""
+ if awdef.title then
+ title = awdef.title
+ end
+ if awdef.custom_announce then
+ custom_announce = awdef.custom_announce
+ end
+ if awdef.background then
+ background = awdef.background
+ end
+ if awdef.icon then
+ 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)
- -- record this in the log
- minetest.log("action", name.." has unlocked award "..title)
+ -- Save playertable
+ awards.save()
- -- 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
+
+ -- send the won award message to the player
+ if awards.show_mode == "formspec" then
+ -- use a formspec to send it
+ minetest.show_formspec(name, "achievements:unlocked", "size[4,2]"..
+ "image_button_exit[0,0;4,2;"..background..";close1; ]"..
+ "image_button_exit[0.2,0.8;1,1;"..icon..";close2; ]"..
+ "label[1.1,1;"..title.."]"..
+ "label[0.3,0.1;"..custom_announce.."]")
+ elseif awards.show_mode == "chat" then
+ -- use the chat console to send it
+ minetest.chat_send_player(name, "Achievement Unlocked: "..title)
+ if desc~="" then
+ minetest.chat_send_player(name, desc)
+ end
+ else
+ local player = minetest.get_player_by_name(name)
+ local one = player:hud_add({
+ hud_elem_type = "image",
+ name = "award_bg",
+ scale = {x = 1, y = 1},
+ text = background,
+ position = {x = 0.5, y = 0},
+ offset = {x = 0, y = 138},
+ alignment = {x = 0, y = -1}
+ })
+ local two = player:hud_add({
+ hud_elem_type = "text",
+ name = "award_au",
+ number = 0xFFFFFF,
+ scale = {x = 100, y = 20},
+ text = "Achievement Unlocked!",
+ position = {x = 0.5, y = 0},
+ offset = {x = 0, y = 40},
+ alignment = {x = 0, y = -1}
+ })
+ local three = player:hud_add({
+ hud_elem_type = "text",
+ name = "award_title",
+ number = 0xFFFFFF,
+ scale = {x = 100, y = 20},
+ text = title,
+ position = {x = 0.5, y = 0},
+ offset = {x = 30, y = 100},
+ alignment = {x = 0, y = -1}
+ })
+ local four = player:hud_add({
+ hud_elem_type = "image",
+ name = "award_icon",
+ scale = {x = 4, y = 4},
+ text = icon,
+ position = {x = 0.5, y = 0},
+ offset = {x = -81.5, y = 126},
+ alignment = {x = 0, y = -1}
+ })
+ minetest.after(3, function()
+ player:hud_remove(one)
+ player:hud_remove(two)
+ player:hud_remove(three)
+ player:hud_remove(four)
+ end)
end
end
diff --git a/helpers.lua b/helpers.lua
index d25966b..084f563 100644
--- a/helpers.lua
+++ b/helpers.lua
@@ -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)
if not default then
default = {}
diff --git a/readme.md b/readme.md
index 4d78dde..f13a8ec 100644
--- a/readme.md
+++ b/readme.md
@@ -23,27 +23,86 @@ old fork in Carbone, under same license.
* (for all types) target - how many to dig / place
* See Triggers
* 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))
* Note: awards.on[name] is automatically created for triggers
-* awards.give_achievement(name,award)
- * -- gives an award to a player
+* 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)
+ * gives an award to a player
+ * name is the player name
# 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
+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))
- * -- return award name or null
+ * data is player data (see below)
+ * return award name or null
* 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))
- * -- return award name or null
+ * data is player data (see below)
+ * return award name or null
* awards.register_on_chat(func(player, data))
- * -- return award name or null
-* awards.register_on_join(func(player, data))
- * -- return award name or null
-* awards.register_onCraft(func(player,data))
- * -- return award name or null
+ * data is player data (see below)
+ * return award name or null
+* awards.register_on_join(func(player, data)
+ * data is player data (see below)
+ * return award name or null
# Player Data
diff --git a/triggers.lua b/triggers.lua
index ccb2c26..f1383e8 100644
--- a/triggers.lua
+++ b/triggers.lua
@@ -14,43 +14,43 @@
-- 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 = {
- award = name,
+ award = def.name,
node = def.trigger.node,
target = def.trigger.target
}
table.insert(awards.on.dig, tmp)
end)
-awards.register_trigger("place", function(name, def)
+awards.register_trigger("place", function(def)
local tmp = {
- award = name,
+ award = def.name,
node = def.trigger.node,
target = def.trigger.target
}
table.insert(awards.on.place, tmp)
end)
-awards.register_trigger("death", function(name, def)
+awards.register_trigger("death", function(def)
local tmp = {
- award = name,
+ award = def.name,
target = def.trigger.target
}
table.insert(awards.on.death, tmp)
end)
-awards.register_trigger("chat", function(name, def)
+awards.register_trigger("chat", function(def)
local tmp = {
- award = name,
+ award = def.name,
target = def.trigger.target
}
table.insert(awards.on.chat, tmp)
end)
-awards.register_trigger("join", function(name, def)
+awards.register_trigger("join", function(def)
local tmp = {
- award = name,
+ award = def.name,
target = def.trigger.target
}
table.insert(awards.on.join, tmp)