From f571f9d6eb7b11dc003aaf6c0a19b9db40ad356e Mon Sep 17 00:00:00 2001
From: rubenwardy <rw@rubenwardy.com>
Date: Wed, 4 Apr 2018 20:55:33 +0100
Subject: Add support for death reasons

---
 README.md    |  2 ++
 api.lua      |  5 +++++
 triggers.lua | 26 +++++++++++++++++++-------
 3 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/README.md b/README.md
index 938a4ef..c780aea 100644
--- a/README.md
+++ b/README.md
@@ -97,6 +97,8 @@ Callbacks (register a function to be run)
 * craft type: Craft something.
 	* item: the crafted item type. If nil, all crafted items are counted
 * death type: Die.
+	* reason: the death reason, one of the types in PlayerHPChangeReason (see lua_api.txt)
+				or nil for total deaths.
 * chat type: Write a chat message.
 * join type: Join the server.
 * (for all types) target - how many times to dig/place/craft/etc.
diff --git a/api.lua b/api.lua
index 21f196b..b19daf9 100644
--- a/api.lua
+++ b/api.lua
@@ -48,6 +48,11 @@ local function convert_data()
 			data[from] = nil
 		end
 
+		data.death = {
+			unknown = data.death,
+			__total = data.death,
+		}
+
 		-- Convert item db to new format
 		local counted_items = {
 			count = "dig",
diff --git a/triggers.lua b/triggers.lua
index 9721b38..6f39cf3 100644
--- a/triggers.lua
+++ b/triggers.lua
@@ -14,13 +14,6 @@
 -- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 --
 
-awards.register_trigger("death", {
-	type = "counted",
-	progress = "@1/@2 deaths",
-	auto_description = { "Die once", "Die @1 times" },
-})
-minetest.register_on_dieplayer(awards.notify_death)
-
 
 awards.register_trigger("chat", {
 	type = "counted",
@@ -45,6 +38,25 @@ awards.register_trigger("join", {
 minetest.register_on_joinplayer(awards.notify_join)
 
 
+awards.register_trigger("death", {
+	type = "counted_key",
+	progress = "@1/@2 deaths",
+	auto_description = { "Die once of @2", "Die @1 times of @2" },
+	auto_description_total = { "Die @1 times.", "Mine @1 times" },
+	get_key = function(self, def)
+		return def.trigger.reason
+	end,
+})
+minetest.register_on_dieplayer(function(player, reason)
+	if reason then
+		reason = reason.type
+	else
+		reason = "unknown"
+	end
+	awards.notify_death(player, reason)
+end)
+
+
 awards.register_trigger("dig", {
 	type = "counted_key",
 	progress = "@1/@2 dug",
-- 
cgit v1.2.3