mobs move slower in liquids

This commit is contained in:
TenPlus1 2020-06-28 14:35:51 +01:00
parent dfdd55848d
commit b17a5bfe77
1 changed files with 22 additions and 7 deletions

29
api.lua
View File

@ -6,7 +6,7 @@ local use_cmi = minetest.global_exists("cmi")
mobs = { mobs = {
mod = "redo", mod = "redo",
version = "20200625", version = "20200628",
intllib = S, intllib = S,
invis = minetest.global_exists("invisibility") and invisibility or {} invis = minetest.global_exists("invisibility") and invisibility or {}
} }
@ -206,7 +206,6 @@ end
function mob_class:collision() function mob_class:collision()
local pos = self.object:get_pos() local pos = self.object:get_pos()
local vel = self.object:get_velocity()
local x, z = 0, 0 local x, z = 0, 0
local width = -self.collisionbox[1] + self.collisionbox[4] + 0.5 local width = -self.collisionbox[1] + self.collisionbox[4] + 0.5
@ -234,7 +233,9 @@ function mob_class:set_velocity(v)
-- halt mob if it has been ordered to stay -- halt mob if it has been ordered to stay
if self.order == "stand" then if self.order == "stand" then
self.object:set_velocity({x = 0, y = 0, z = 0}) self.object:set_velocity({x = 0, y = 0, z = 0})
return return
end end
@ -253,11 +254,25 @@ function mob_class:set_velocity(v)
-- set velocity with hard limit of 10 -- set velocity with hard limit of 10
local vel = self.object:get_velocity() local vel = self.object:get_velocity()
self.object:set_velocity({ local new_vel = {
x = max(-10, min((sin(yaw) * -v) + c_x, 10)), x = (sin(yaw) * -v) + c_x,
y = max(-10, min((vel and vel.y or 0), 10)), y = vel.y,
z = max(-10, min((cos(yaw) * v) + c_y, 10)) z = (cos(yaw) * v) + c_y
}) }
-- check if standing in water and use liquid viscocity to slow mob
local visc = minetest.registered_nodes[self.standing_in].liquid_viscosity
-- assume any mob not flying will slow down in water as mobs flying in air
-- wont be near water, and mobs flying in water will swim at full speed
if not self.flying_in and (visc and visc > 0) then
new_vel.x = new_vel.x ~= 0 and new_vel.x / visc or 0
new_vel.y = new_vel.y ~= 0 and new_vel.y / visc or 0
new_vel.z = new_vel.z ~= 0 and new_vel.z / visc or 0
end
self.object:set_velocity(new_vel)
end end
-- global version of above function -- global version of above function