From e82c318f0c517bfcd48efc22cf5b6436ff0985ff Mon Sep 17 00:00:00 2001 From: cora Date: Wed, 9 Nov 2022 04:57:48 +0100 Subject: [PATCH] Split off breeding --- mods/ENTITIES/mcl_mobs/api.lua | 138 +---------------------- mods/ENTITIES/mcl_mobs/breeding.lua | 142 ++++++++++++++++++++++++ mods/ENTITIES/mcl_mobs/init.lua | 2 + mods/ENTITIES/mobs_mc/chicken.lua | 2 +- mods/ENTITIES/mobs_mc/cow+mooshroom.lua | 4 +- mods/ENTITIES/mobs_mc/horse.lua | 4 +- mods/ENTITIES/mobs_mc/llama.lua | 4 +- mods/ENTITIES/mobs_mc/ocelot.lua | 2 +- mods/ENTITIES/mobs_mc/parrot.lua | 2 +- mods/ENTITIES/mobs_mc/pig.lua | 2 +- mods/ENTITIES/mobs_mc/rabbit.lua | 2 +- mods/ENTITIES/mobs_mc/sheep.lua | 2 +- mods/ENTITIES/mobs_mc/strider.lua | 2 +- mods/ENTITIES/mobs_mc/villager.lua | 2 +- mods/ENTITIES/mobs_mc/wolf.lua | 2 +- 15 files changed, 161 insertions(+), 151 deletions(-) create mode 100644 mods/ENTITIES/mcl_mobs/breeding.lua diff --git a/mods/ENTITIES/mcl_mobs/api.lua b/mods/ENTITIES/mcl_mobs/api.lua index ed780fee2..e0206b722 100644 --- a/mods/ENTITIES/mcl_mobs/api.lua +++ b/mods/ENTITIES/mcl_mobs/api.lua @@ -1,5 +1,6 @@ local mob_class = mcl_mobs.mob_class local mob_class_meta = {__index = mcl_mobs.mob_class} +local math, vector, minetest, mcl_mobs = math, vector, minetest, mcl_mobs -- API for Mobs Redo: MineClone 2 Edition (MRM) local MAX_MOB_NAME_LENGTH = 30 local HORNY_TIME = 30 @@ -753,7 +754,7 @@ local breed = function(self) end end - local child = mcl_mobs:spawn_child(pos, parent1.name) + local child = mcl_mobs.spawn_child(pos, parent1.name) local ent_c = child:get_luaentity() @@ -4062,141 +4063,6 @@ function mcl_mobs.register_egg(mob, desc, background_color, overlay_color, addeg end - --- No-op in MCL2 (capturing mobs is not possible). --- Provided for compability with Mobs Redo -function mcl_mobs:capture_mob(self, clicker, chance_hand, chance_net, chance_lasso, force_take, replacewith) - return false -end - - --- No-op in MCL2 (protecting mobs is not possible). -function mcl_mobs:protect(self, clicker) - return false -end - - --- feeding, taming and breeding (thanks blert2112) -function mcl_mobs:feed_tame(self, clicker, feed_count, breed, tame, notake) - if not self.follow then - return false - end - -- can eat/tame with item in hand - if self.nofollow or follow_holding(self, clicker) then - local consume_food = false - - -- tame if not still a baby - - if tame and not self.child then - if not self.owner or self.owner == "" then - self.tamed = true - self.owner = clicker:get_player_name() - consume_food = true - end - end - - -- increase health - - if self.health < self.hp_max and not consume_food then - consume_food = true - self.health = math.min(self.health + 4, self.hp_max) - - if self.htimer < 1 then - self.htimer = 5 - end - self.object:set_hp(self.health) - end - - -- make children grow quicker - - if not consume_food and self.child == true then - consume_food = true - -- deduct 10% of the time to adulthood - self.hornytimer = self.hornytimer + ((CHILD_GROW_TIME - self.hornytimer) * 0.1) - end - - -- breed animals - - if breed and not consume_food and self.hornytimer == 0 and not self.horny then - self.food = (self.food or 0) + 1 - consume_food = true - if self.food >= feed_count then - self.food = 0 - self.horny = true - end - end - - self:update_tag() - -- play a sound if the animal used the item and take the item if not in creative - if consume_food then - -- don't consume food if clicker is in creative - if not minetest.is_creative_enabled(clicker:get_player_name()) and not notake then - local item = clicker:get_wielded_item() - item:take_item() - clicker:set_wielded_item(item) - end - -- always play the eat sound if food is used, even in creative - self:mob_sound("eat", nil, true) - - else - -- make sound when the mob doesn't want food - self:mob_sound("random", true) - end - return true - end - return false -end - --- Spawn a child -function mcl_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() - mcl_mobs.effect(pos, 15, "mcl_particles_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, - }, - }) - - ent.animation = ent._child_animations - ent._current_animation = nil - set_animation(ent, "stand") - - return child -end - local timer = 0 minetest.register_globalstep(function(dtime) timer = timer + dtime diff --git a/mods/ENTITIES/mcl_mobs/breeding.lua b/mods/ENTITIES/mcl_mobs/breeding.lua new file mode 100644 index 000000000..65a774e1f --- /dev/null +++ b/mods/ENTITIES/mcl_mobs/breeding.lua @@ -0,0 +1,142 @@ +local math, vector, minetest, mcl_mobs = math, vector, minetest, mcl_mobs +local mob_class = mcl_mobs.mob_class + +local MAX_MOB_NAME_LENGTH = 30 +local HORNY_TIME = 30 +local HORNY_AGAIN_TIME = 300 +local CHILD_GROW_TIME = 60*20 + + +-- No-op in MCL2 (capturing mobs is not possible). +-- Provided for compability with Mobs Redo +function mcl_mobs:capture_mob(self, clicker, chance_hand, chance_net, chance_lasso, force_take, replacewith) + return false +end + + +-- No-op in MCL2 (protecting mobs is not possible). +function mcl_mobs:protect(self, clicker) + return false +end + + +-- feeding, taming and breeding (thanks blert2112) +function mob_class:feed_tame(clicker, feed_count, breed, tame, notake) + if not self.follow then + return false + end + -- can eat/tame with item in hand + if self.nofollow or follow_holding(self, clicker) then + local consume_food = false + + -- tame if not still a baby + + if tame and not self.child then + if not self.owner or self.owner == "" then + self.tamed = true + self.owner = clicker:get_player_name() + consume_food = true + end + end + + -- increase health + + if self.health < self.hp_max and not consume_food then + consume_food = true + self.health = math.min(self.health + 4, self.hp_max) + + if self.htimer < 1 then + self.htimer = 5 + end + self.object:set_hp(self.health) + end + + -- make children grow quicker + + if not consume_food and self.child == true then + consume_food = true + -- deduct 10% of the time to adulthood + self.hornytimer = self.hornytimer + ((CHILD_GROW_TIME - self.hornytimer) * 0.1) + end + + -- breed animals + + if breed and not consume_food and self.hornytimer == 0 and not self.horny then + self.food = (self.food or 0) + 1 + consume_food = true + if self.food >= feed_count then + self.food = 0 + self.horny = true + end + end + + self:update_tag() + -- play a sound if the animal used the item and take the item if not in creative + if consume_food then + -- don't consume food if clicker is in creative + if not minetest.is_creative_enabled(clicker:get_player_name()) and not notake then + local item = clicker:get_wielded_item() + item:take_item() + clicker:set_wielded_item(item) + end + -- always play the eat sound if food is used, even in creative + self:mob_sound("eat", nil, true) + + else + -- make sound when the mob doesn't want food + self:mob_sound("random", true) + end + return true + end + return false +end + +-- Spawn a child +function mcl_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() + mcl_mobs.effect(pos, 15, "mcl_particles_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, + }, + }) + + ent.animation = ent._child_animations + ent._current_animation = nil + ent:set_animation("stand") + + return child +end diff --git a/mods/ENTITIES/mcl_mobs/init.lua b/mods/ENTITIES/mcl_mobs/init.lua index 74b07efd9..16961d66b 100644 --- a/mods/ENTITIES/mcl_mobs/init.lua +++ b/mods/ENTITIES/mcl_mobs/init.lua @@ -9,6 +9,8 @@ dofile(path .. "/physics.lua") -- Mob API dofile(path .. "/api.lua") +dofile(path .. "/breeding.lua") + -- Spawning Algorithm dofile(path .. "/spawning.lua") diff --git a/mods/ENTITIES/mobs_mc/chicken.lua b/mods/ENTITIES/mobs_mc/chicken.lua index b8ee1a877..399294390 100644 --- a/mods/ENTITIES/mobs_mc/chicken.lua +++ b/mods/ENTITIES/mobs_mc/chicken.lua @@ -83,7 +83,7 @@ mcl_mobs.register_mob("mobs_mc:chicken", { fear_height = 4, on_rightclick = function(self, clicker) - if mcl_mobs:feed_tame(self, clicker, 1, true, false) then return end + if self:feed_tame(clicker, 1, true, false) then return end if mcl_mobs:protect(self, clicker) then return end if mcl_mobs:capture_mob(self, clicker, 0, 60, 5, false, nil) then return end end, diff --git a/mods/ENTITIES/mobs_mc/cow+mooshroom.lua b/mods/ENTITIES/mobs_mc/cow+mooshroom.lua index 5096d43d3..5ac5912a4 100644 --- a/mods/ENTITIES/mobs_mc/cow+mooshroom.lua +++ b/mods/ENTITIES/mobs_mc/cow+mooshroom.lua @@ -59,7 +59,7 @@ local cow_def = { run_start = 41, run_end = 81, run_speed = 60, }, on_rightclick = function(self, clicker) - if mcl_mobs:feed_tame(self, clicker, 1, true, false) then return end + if self:feed_tame(clicker, 1, true, false) then return end if mcl_mobs:protect(self, clicker) then return end if self.child then @@ -97,7 +97,7 @@ mooshroom_def.spawn_in_group_min = 4 mooshroom_def.spawn_in_group = 8 mooshroom_def.textures = { {"mobs_mc_mooshroom.png", "mobs_mc_mushroom_red.png"}, {"mobs_mc_mooshroom_brown.png", "mobs_mc_mushroom_brown.png" } } mooshroom_def.on_rightclick = function(self, clicker) - if mcl_mobs:feed_tame(self, clicker, 1, true, false) then return end + if self:feed_tame(clicker, 1, true, false) then return end if mcl_mobs:protect(self, clicker) then return end if self.child then diff --git a/mods/ENTITIES/mobs_mc/horse.lua b/mods/ENTITIES/mobs_mc/horse.lua index a91b8acab..a69234b44 100644 --- a/mods/ENTITIES/mobs_mc/horse.lua +++ b/mods/ENTITIES/mobs_mc/horse.lua @@ -337,7 +337,7 @@ local horse = { elseif (iname == "mcl_farming:carrot_item_gold") then heal = 4 end - if heal > 0 and mcl_mobs:feed_tame(self, clicker, heal, true, false) then + if heal > 0 and self:feed_tame(clicker, heal, true, false) then return end end @@ -352,7 +352,7 @@ local horse = { elseif (iname == "mcl_farming:hay_block") then heal = 20 end - if heal > 0 and mcl_mobs:feed_tame(self, clicker, heal, false, false) then + if heal > 0 and self:feed_tame(clicker, heal, false, false) then return end diff --git a/mods/ENTITIES/mobs_mc/llama.lua b/mods/ENTITIES/mobs_mc/llama.lua index 4f4e101a6..1f458ec89 100644 --- a/mods/ENTITIES/mobs_mc/llama.lua +++ b/mods/ENTITIES/mobs_mc/llama.lua @@ -156,7 +156,7 @@ mcl_mobs.register_mob("mobs_mc:llama", { local item = clicker:get_wielded_item() if item:get_name() == "mcl_farming:hay_block" then -- Breed with hay bale - if mcl_mobs:feed_tame(self, clicker, 1, true, false) then return end + if self:feed_tame(clicker, 1, true, false) then return end elseif not self._has_chest and item:get_name() == "mcl_chests:chest" then item:take_item() clicker:set_wielded_item(item) @@ -173,7 +173,7 @@ mcl_mobs.register_mob("mobs_mc:llama", { return else -- Feed with anything else - if mcl_mobs:feed_tame(self, clicker, 1, false, true) then return end + if self:feed_tame(clicker, 1, false, true) then return end end if mcl_mobs:protect(self, clicker) then return end diff --git a/mods/ENTITIES/mobs_mc/ocelot.lua b/mods/ENTITIES/mobs_mc/ocelot.lua index cfb8d83f0..8a7ea7545 100644 --- a/mods/ENTITIES/mobs_mc/ocelot.lua +++ b/mods/ENTITIES/mobs_mc/ocelot.lua @@ -130,7 +130,7 @@ cat.sounds = { distance = 16, } cat.on_rightclick = function(self, clicker) - if mcl_mobs:feed_tame(self, clicker, 1, true, false) then return end + if self:feed_tame(clicker, 1, true, false) then return end if mcl_mobs:capture_mob(self, clicker, 0, 60, 5, false, nil) then return end if mcl_mobs:protect(self, clicker) then return end diff --git a/mods/ENTITIES/mobs_mc/parrot.lua b/mods/ENTITIES/mobs_mc/parrot.lua index 54a76c976..0aeebde8d 100644 --- a/mods/ENTITIES/mobs_mc/parrot.lua +++ b/mods/ENTITIES/mobs_mc/parrot.lua @@ -207,7 +207,7 @@ mcl_mobs.register_mob("mobs_mc:parrot", { return end -- Feed to tame, but not breed - if mcl_mobs:feed_tame(self, clicker, 1, false, true) then return end + if self:feed_tame(clicker, 1, false, true) then return end perch(self,clicker) end, do_custom = function(self,dtime) diff --git a/mods/ENTITIES/mobs_mc/pig.lua b/mods/ENTITIES/mobs_mc/pig.lua index c790de996..3fb2d2edb 100644 --- a/mods/ENTITIES/mobs_mc/pig.lua +++ b/mods/ENTITIES/mobs_mc/pig.lua @@ -108,7 +108,7 @@ mcl_mobs.register_mob("mobs_mc:pig", { local wielditem = clicker:get_wielded_item() -- Feed pig if wielditem:get_name() ~= "mcl_mobitems:carrot_on_a_stick" then - if mcl_mobs:feed_tame(self, clicker, 1, true, false) then return end + if self:feed_tame(clicker, 1, true, false) then return end end if mcl_mobs:protect(self, clicker) then return end diff --git a/mods/ENTITIES/mobs_mc/rabbit.lua b/mods/ENTITIES/mobs_mc/rabbit.lua index cb595fd2a..6d13fbe22 100644 --- a/mods/ENTITIES/mobs_mc/rabbit.lua +++ b/mods/ENTITIES/mobs_mc/rabbit.lua @@ -83,7 +83,7 @@ local rabbit = { }, on_rightclick = function(self, clicker) -- Feed, tame protect or capture - if mcl_mobs:feed_tame(self, clicker, 1, true, false) then return end + if self:feed_tame(clicker, 1, true, false) then return end if mcl_mobs:protect(self, clicker) then return end if mcl_mobs:capture_mob(self, clicker, 0, 50, 80, false, nil) then return end end, diff --git a/mods/ENTITIES/mobs_mc/sheep.lua b/mods/ENTITIES/mobs_mc/sheep.lua index f970c6e6e..4f3d3a048 100644 --- a/mods/ENTITIES/mobs_mc/sheep.lua +++ b/mods/ENTITIES/mobs_mc/sheep.lua @@ -205,7 +205,7 @@ mcl_mobs.register_mob("mobs_mc:sheep", { on_rightclick = function(self, clicker) local item = clicker:get_wielded_item() - if mcl_mobs:feed_tame(self, clicker, 1, true, false) then return end + if self:feed_tame(clicker, 1, true, false) then return end if mcl_mobs:protect(self, clicker) then return end if item:get_name() == "mcl_tools:shears" and not self.gotten and not self.child then diff --git a/mods/ENTITIES/mobs_mc/strider.lua b/mods/ENTITIES/mobs_mc/strider.lua index 9b7f44ab9..a49845f7e 100644 --- a/mods/ENTITIES/mobs_mc/strider.lua +++ b/mods/ENTITIES/mobs_mc/strider.lua @@ -123,7 +123,7 @@ local strider = { local wielditem = clicker:get_wielded_item() if wielditem:get_name() ~= "mcl_crimson:warped_fungus" then - if mcl_mobs:feed_tame(self, clicker, 1, true, true) then return end + if self:feed_tame(clicker, 1, true, true) then return end end if self.child then diff --git a/mods/ENTITIES/mobs_mc/villager.lua b/mods/ENTITIES/mobs_mc/villager.lua index 6aa913a98..7bdba3336 100644 --- a/mods/ENTITIES/mobs_mc/villager.lua +++ b/mods/ENTITIES/mobs_mc/villager.lua @@ -1963,7 +1963,7 @@ mcl_mobs.register_mob("mobs_mc:villager", { end end if clicker and not self.horny then - mcl_mobs:feed_tame(self, clicker, 1, true, false, true) + self:feed_tame(clicker, 1, true, false, true) it:take_item(1) end return it diff --git a/mods/ENTITIES/mobs_mc/wolf.lua b/mods/ENTITIES/mobs_mc/wolf.lua index d3a34f306..09377cc26 100644 --- a/mods/ENTITIES/mobs_mc/wolf.lua +++ b/mods/ENTITIES/mobs_mc/wolf.lua @@ -167,7 +167,7 @@ end dog.on_rightclick = function(self, clicker) local item = clicker:get_wielded_item() - if mcl_mobs:feed_tame(self, clicker, 1, true, false) then + if self:feed_tame(clicker, 1, true, false) then return elseif mcl_mobs:protect(self, clicker) then return