From b2d9c119acfe531865e10b5e3e1457e43904c7f5 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Wed, 30 May 2018 12:56:39 +0200 Subject: [PATCH] Add mobs:spawn_child --- mods/ENTITIES/mobs/api.lua | 93 ++++++++++++++++++++------------- mods/ENTITIES/mobs/api.txt | 10 ++++ mods/ENTITIES/mobs_mc/horse.lua | 34 ++---------- mods/ENTITIES/mobs_mc/pig.lua | 35 ++----------- 4 files changed, 72 insertions(+), 100 deletions(-) diff --git a/mods/ENTITIES/mobs/api.lua b/mods/ENTITIES/mobs/api.lua index b284216f8..209ad79a6 100644 --- a/mods/ENTITIES/mobs/api.lua +++ b/mods/ENTITIES/mobs/api.lua @@ -969,52 +969,25 @@ local breed = function(self) -- custom breed function if self.on_breed then - -- when false skip going any further if self.on_breed(self, ent) == false then - return + return end - else - effect(pos, 15, "tnt_smoke.png", 1, 2, 2, 15, 5) end - local mob = minetest.add_entity(pos, self.name) - local ent2 = mob:get_luaentity() + local child = mobs:spawn_child(pos, self.name) + + local ent_c = child:get_luaentity() + + -- Use parent's texture local textures = self.base_texture - - -- using specific child texture (if found) - if self.child_texture then - textures = self.child_texture[1] - end - - -- and resize to half height - mob:set_properties({ + child:set_properties({ textures = textures, - visual_size = { - x = self.base_size.x * .5, - y = self.base_size.y * .5, - }, - collisionbox = { - self.base_colbox[1] * .5, - self.base_colbox[2] * .5, - self.base_colbox[3] * .5, - self.base_colbox[4] * .5, - self.base_colbox[5] * .5, - self.base_colbox[6] * .5, - }, - selectionbox = { - self.base_selbox[1] * .5, - self.base_selbox[2] * .5, - self.base_selbox[3] * .5, - self.base_selbox[4] * .5, - self.base_selbox[5] * .5, - self.base_selbox[6] * .5, - }, }) + -- tamed and owned by parents' owner - ent2.child = true - ent2.tamed = true - ent2.owner = self.owner + ent_c.tamed = true + ent_c.owner = self.owner end) num = 0 @@ -3907,6 +3880,52 @@ function mobs:feed_tame(self, clicker, feed_count, breed, tame) return false end +-- Spawn a child +function mobs:spawn_child(pos, mob_type) + local child = minetest.add_entity(pos, mob_type) + if not child then + return + end + + local ent = child:get_luaentity() + effect(pos, 15, "tnt_smoke.png", 1, 2, 2, 15, 5) + + ent.child = true + + local textures + -- using specific child texture (if found) + if ent.child_texture then + textures = ent.child_texture[1] + end + + -- and resize to half height + child:set_properties({ + textures = textures, + visual_size = { + x = ent.base_size.x * .5, + y = ent.base_size.y * .5, + }, + collisionbox = { + ent.base_colbox[1] * .5, + ent.base_colbox[2] * .5, + ent.base_colbox[3] * .5, + ent.base_colbox[4] * .5, + ent.base_colbox[5] * .5, + ent.base_colbox[6] * .5, + }, + selectionbox = { + ent.base_selbox[1] * .5, + ent.base_selbox[2] * .5, + ent.base_selbox[3] * .5, + ent.base_selbox[4] * .5, + ent.base_selbox[5] * .5, + ent.base_selbox[6] * .5, + }, + }) + + return child +end + -- DISABLED IN MCL2 --[=[ diff --git a/mods/ENTITIES/mobs/api.txt b/mods/ENTITIES/mobs/api.txt index 6d9ecc7c0..75b2b98a8 100644 --- a/mods/ENTITIES/mobs/api.txt +++ b/mods/ENTITIES/mobs/api.txt @@ -361,6 +361,16 @@ true the mob will not spawn. 'name' is the name of the animal/monster +MineClone 2 extensions +---------------------- + +mobs:spawn_child(pos, mob_type) + +This function spawns a mob as a child. The parameter mob_type is the +entitystring of the new mob. +This function returns the mob on success and nil otherwise. + + Making Arrows ------------- diff --git a/mods/ENTITIES/mobs_mc/horse.lua b/mods/ENTITIES/mobs_mc/horse.lua index 60f8738d4..4d27299f9 100644 --- a/mods/ENTITIES/mobs_mc/horse.lua +++ b/mods/ENTITIES/mobs_mc/horse.lua @@ -253,37 +253,9 @@ local horse = { on_breed = function(parent1, parent2) local pos = parent1.object:get_pos() - local mob = minetest.add_entity(pos, parent1.name) - - -- resize to half height - mob:set_properties({ - visual_size = { - x = parent1.base_size.x * .5, - y = parent1.base_size.y * .5, - }, - collisionbox = { - parent1.base_colbox[1] * .5, - parent1.base_colbox[2] * .5, - parent1.base_colbox[3] * .5, - parent1.base_colbox[4] * .5, - parent1.base_colbox[5] * .5, - parent1.base_colbox[6] * .5, - }, - selectionbox = { - parent1.base_selbox[1] * .5, - parent1.base_selbox[2] * .5, - parent1.base_selbox[3] * .5, - parent1.base_selbox[4] * .5, - parent1.base_selbox[5] * .5, - parent1.base_selbox[6] * .5, - }, - }) - local ent = mob:get_luaentity() - -- tamed and owned by parents' owner - ent.child = true - ent.tamed = true - ent.owner = parent1.owner - return false + if mobs:spawn_child(pos, parent1.name) then + return false + end end, } diff --git a/mods/ENTITIES/mobs_mc/pig.lua b/mods/ENTITIES/mobs_mc/pig.lua index d8b80ed0c..fede3f111 100644 --- a/mods/ENTITIES/mobs_mc/pig.lua +++ b/mods/ENTITIES/mobs_mc/pig.lua @@ -171,38 +171,9 @@ mobs:register_mob("mobs_mc:pig", { on_breed = function(parent1, parent2) local pos = parent1.object:get_pos() - local mob = minetest.add_entity(pos, parent1.name) - - -- resize to half height - mob:set_properties({ - visual_size = { - x = parent1.base_size.x * .5, - y = parent1.base_size.y * .5, - }, - collisionbox = { - parent1.base_colbox[1] * .5, - parent1.base_colbox[2] * .5, - parent1.base_colbox[3] * .5, - parent1.base_colbox[4] * .5, - parent1.base_colbox[5] * .5, - parent1.base_colbox[6] * .5, - }, - selectionbox = { - parent1.base_selbox[1] * .5, - parent1.base_selbox[2] * .5, - parent1.base_selbox[3] * .5, - parent1.base_selbox[4] * .5, - parent1.base_selbox[5] * .5, - parent1.base_selbox[6] * .5, - }, - }) - local ent = mob:get_luaentity() - - -- tamed and owned by parents' owner - ent.child = true - ent.tamed = true - ent.owner = parent1.owner - return false + if mobs:spawn_child(pos, parent1.name) then + return false + end end, })