From 4efec1ef58ba4afe4692a22a361079b5026a7de3 Mon Sep 17 00:00:00 2001 From: jordan4ibanez Date: Fri, 23 Apr 2021 12:55:11 -0400 Subject: [PATCH] Add in chicken slow falling --- mods/ENTITIES/mcl_mobs/api/api.lua | 1 + .../mcl_mobs/api/mob_functions/ai.lua | 23 +++++++++++++++ .../mcl_mobs/api/mob_functions/movement.lua | 28 +++++++++++++++++++ mods/ENTITIES/mobs_mc/chicken.lua | 1 + 4 files changed, 53 insertions(+) diff --git a/mods/ENTITIES/mcl_mobs/api/api.lua b/mods/ENTITIES/mcl_mobs/api/api.lua index 307a180903..fc153a5fe1 100644 --- a/mods/ENTITIES/mcl_mobs/api/api.lua +++ b/mods/ENTITIES/mcl_mobs/api/api.lua @@ -355,6 +355,7 @@ function mobs:register_mob(name, def) hostile_cooldown = def.hostile_cooldown or 15, tilt_fly = def.tilt_fly, tilt_swim = def.tilt_swim, + fall_slow = def.fall_slow, -- End of MCL2 extensions on_spawn = def.on_spawn, diff --git a/mods/ENTITIES/mcl_mobs/api/mob_functions/ai.lua b/mods/ENTITIES/mcl_mobs/api/mob_functions/ai.lua index d6d51b8deb..e883a36702 100644 --- a/mods/ENTITIES/mcl_mobs/api/mob_functions/ai.lua +++ b/mods/ENTITIES/mcl_mobs/api/mob_functions/ai.lua @@ -1,5 +1,7 @@ local math_random = math.random local math_pi = math.pi +local math_floor = math.floor +local math_round = math.round local vector_multiply = vector.multiply local vector_add = vector.add @@ -22,6 +24,12 @@ local quick_rotate = function(self,dtime) end end +--a simple helper function for rounding +--http://lua-users.org/wiki/SimpleRound +function round2(num, numDecimalPlaces) + return tonumber(string.format("%." .. (numDecimalPlaces or 0) .. "f", num)) +end + --[[ _ _ @@ -89,6 +97,21 @@ local land_state_execution = function(self,dtime) float_now = true end + --make slow falling mobs fall slow + if self.fall_slow then + if self.object:get_velocity().y < 0 then + --lua is acting really weird so we have to help it + if round2(self.object:get_acceleration().y, 1) == -self.gravity then + self.object:set_acceleration(vector_new(0,0,0)) + mobs.mob_fall_slow(self) + end + else + if round2(self.object:get_acceleration().y, 1) == 0 then + self.object:set_acceleration(vector_new(0,-self.gravity,0)) + end + end + end + if self.state == "stand" then --do animation diff --git a/mods/ENTITIES/mcl_mobs/api/mob_functions/movement.lua b/mods/ENTITIES/mcl_mobs/api/mob_functions/movement.lua index 811488396f..819df63533 100644 --- a/mods/ENTITIES/mcl_mobs/api/mob_functions/movement.lua +++ b/mods/ENTITIES/mcl_mobs/api/mob_functions/movement.lua @@ -113,8 +113,36 @@ mobs.jump = function(self, velocity) self.object:add_velocity(vector_new(0,velocity,0)) end +--make mobs fall slowly +mobs.mob_fall_slow = function(self) + + local current_velocity = self.object:get_velocity() + + local goal_velocity = { + x = 0, + y = -2, + z = 0, + } + local new_velocity_addition = vector.subtract(goal_velocity,current_velocity) + + new_velocity_addition.x = 0 + new_velocity_addition.z = 0 + + if vector_length(new_velocity_addition) > vector_length(goal_velocity) then + vector.multiply(new_velocity_addition, (vector_length(goal_velocity) / vector_length(new_velocity_addition))) + end + + new_velocity_addition.x = 0 + new_velocity_addition.z = 0 + + --smooths out mobs a bit + if vector_length(new_velocity_addition) >= 0.0001 then + self.object:add_velocity(new_velocity_addition) + end + +end --[[ diff --git a/mods/ENTITIES/mobs_mc/chicken.lua b/mods/ENTITIES/mobs_mc/chicken.lua index 2fe82293e3..2e1a2e5245 100644 --- a/mods/ENTITIES/mobs_mc/chicken.lua +++ b/mods/ENTITIES/mobs_mc/chicken.lua @@ -18,6 +18,7 @@ mobs:register_mob("mobs_mc:chicken", { xp_max = 3, collisionbox = {-0.2, -0.01, -0.2, 0.2, 0.69, 0.2}, runaway = true, + fall_slow = true, floats = 1, visual = "mesh", mesh = "mobs_mc_chicken.b3d",