From d25e638cfb463a2472f99173ab35ac9fe2d5c71a Mon Sep 17 00:00:00 2001 From: Methacrylon Date: Thu, 14 Jun 2018 07:54:44 +0200 Subject: [PATCH] Add kill_mob and punch_entity triggers; it's now possible to create awards for killing mobs and punching entities --- awards.lua | 32 ++++++++++++++++++++++++++++ depends.txt | 1 + triggers.lua | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) diff --git a/awards.lua b/awards.lua index 4dc6681..a8828f5 100644 --- a/awards.lua +++ b/awards.lua @@ -958,3 +958,35 @@ if minetest.get_modpath("nyancat") then } }) end + +if minetest.get_modpath("mobs_animal") then + awards.register_award("mobs:kill_chicken", { + title = S("Chicken"), + description = S("A test award when mobs mod on"), + difficulty = 0.001, + trigger = { + type = "kill_mob", + mob = "mobs_animal:chicken", + target = 5 + } + }) + awards.register_award("mobs:kill_cow", { + title = S("Cows"), + description = S("A test award when mobs mod on"), + difficulty = 0.001, + trigger = { + type = "kill_mob", + mob = "mobs_animal:cow", + target = 5 + } + }) + awards.register_award("mobs:kill_all", { + title = S("All"), + description = S("A test award when mobs mod on"), + difficulty = 0.001, + trigger = { + type = "kill_mob", + target = 5 + } + }) +end diff --git a/depends.txt b/depends.txt index c8f3f2d..2728d45 100644 --- a/depends.txt +++ b/depends.txt @@ -12,3 +12,4 @@ moreblocks? fire? flowers? nyancat? +mobs_animal? diff --git a/triggers.lua b/triggers.lua index 1a7b311..b71814e 100644 --- a/triggers.lua +++ b/triggers.lua @@ -135,3 +135,63 @@ minetest.register_on_item_eat(function(_, _, itemstack, player, _) itemname = minetest.registered_aliases[itemname] or itemname awards.notify_craft(player, itemname, itemstack:get_count()) end) + +-- trigger for killing entities with more than 1 point of health +awards.register_trigger("kill_mob", { + type = "counted_key", + progress = "@1/@2 killed", + auto_description = { "Kill @2", "Kill @1×@2" }, + auto_description_total = { "Kill @1 mob", "Kill @1 mobs." }, + get_key = function(self, def) + return minetest.registered_aliases[def.trigger.mob] or def.trigger.mob + end +}) + +-- wait for all mobs to be registered +minetest.after(0, function() + local ents = minetest.registered_entities + local mobs = {} + for _, ent in pairs(ents) do + if ent.hp_max ~= nil and ent.hp_max > 0 then + table.insert(mobs,ent) + end + end + for _, mob in pairs(mobs) do + local old_func = mob.on_punch + mob.on_punch = function(self, player, a, b, c, d) + old_func(self, player, a, b, c, d) + if player and player:is_player() then + if self.health <= 0 then + awards.notify_kill_mob(player, self.name) + end + end + end + end +end) + +-- trigger for killing an entity +awards.register_trigger("punch_entity", { + type = "counted_key", + progress = "@1/@2 killed", + auto_description = { "Punch @2", "Punch @1×@2" }, + auto_description_total = { "Punch @1 entity", "Punch @1 entities." }, + get_key = function(self, def) + return minetest.registered_aliases[def.trigger.mob] or def.trigger.mob + end +}) + +-- wait for all entities to be registered +minetest.after(0, function() + local ents = minetest.registered_entities + for _, ent in pairs(ents) do + if ent.hp_max == nil then + local old_func = ent.on_punch + ent.on_punch = function(self, player, a, b, c, d) + old_func(self, player, a, b, c, d) + if player and player:is_player() then + awards.notify_punch_entity(player, self.name) + end + end + end + end +end)