Merge pull request 'Give mobs 6 seconds of memory to prevent strange behavior when player hides behind something' (#1639) from jordan4ibanez/MineClone2:mineclone5 into mineclone5

Reviewed-on: MineClone2/MineClone2#1639
This commit is contained in:
jordan4ibanez 2021-04-25 02:14:53 +00:00
commit b937b20158
4 changed files with 69 additions and 17 deletions

View File

@ -328,6 +328,9 @@ function mobs:register_mob(name, def)
minimum_follow_distance = def.minimum_follow_distance or 0.5, --make mobs not freak out when underneath minimum_follow_distance = def.minimum_follow_distance or 0.5, --make mobs not freak out when underneath
memory = 0, -- memory timer if chasing/following
fly_random_while_attack = def.fly_random_while_attack,
--for spiders --for spiders
always_climb = def.always_climb, always_climb = def.always_climb,

View File

@ -481,7 +481,7 @@ local swim_state_execution = function(self,dtime)
self.yaw = (math_random() * (math.pi * 2)) self.yaw = (math_random() * (math.pi * 2))
--create a truly random pitch, since there is no easy access to pitch math that I can find --create a truly random pitch, since there is no easy access to pitch math that I can find
self.pitch = math_random() * random_pitch_multiplier[math_random(1,2)] self.pitch = math_random() * math.random(1,3) * random_pitch_multiplier[math_random(1,2)]
end end
--do animation --do animation
@ -626,7 +626,7 @@ local fly_state_execution = function(self,dtime)
self.yaw = (math_random() * (math.pi * 2)) self.yaw = (math_random() * (math.pi * 2))
--create a truly random pitch, since there is no easy access to pitch math that I can find --create a truly random pitch, since there is no easy access to pitch math that I can find
self.pitch = math_random() * random_pitch_multiplier[math_random(1,2)] self.pitch = math_random() * math.random(1,3) * random_pitch_multiplier[math_random(1,2)]
end end
--do animation --do animation
@ -899,23 +899,41 @@ mobs.mob_step = function(self, dtime)
--go get the closest player --go get the closest player
if attacking then if attacking then
self.memory = 6 --6 seconds of memory
--set initial punch timer --set initial punch timer
if self.attacking == nil then if self.attacking == nil then
if self.attack_type == "punch" then if self.attack_type == "punch" then
self.punch_timer = -1 self.punch_timer = -1
end end
end end
self.attacking = attacking self.attacking = attacking
--no player in area
else
--reset states when coming out of hostile state --no player in area
if self.attacking ~= nil then elseif self.memory > 0 then
self.state_timer = -1 --try to remember
self.memory = self.memory - dtime
--get if memory player is within viewing range
if self.attacking and self.attacking:is_player() then
local distance = vector_distance(self.object:get_pos(), self.attacking:get_pos())
if distance > self.view_range then
self.memory = 0
end
--out of viewing range, forget em
else
self.memory = 0
end end
self.attacking = nil if self.memory <= 0 then
--reset states when coming out of hostile state
if self.attacking ~= nil then
self.state_timer = -1
end
self.attacking = nil
self.memory = 0
end
end end
end end

View File

@ -279,6 +279,8 @@ ______ _ _ _ _
|__/ |__/
]]-- ]]--
local random_pitch_multiplier = {-1,1}
mobs.projectile_attack_fly = function(self, dtime) mobs.projectile_attack_fly = function(self, dtime)
--this needs an exception --this needs an exception
@ -287,18 +289,41 @@ mobs.projectile_attack_fly = function(self, dtime)
return return
end end
local distance_from_attacking = vector_distance(self.object:get_pos(), self.attacking:get_pos()) --this is specifically for random ghast movement
if self.fly_random_while_attack then
--enable rotation locking
mobs.movement_rotation_lock(self)
self.walk_timer = self.walk_timer - dtime
--reset the walk timer
if self.walk_timer <= 0 then
--re-randomize the walk timer
self.walk_timer = math.random(1,6) + math.random()
--set the mob into a random direction
self.yaw = (math_random() * (math.pi * 2))
--create a truly random pitch, since there is no easy access to pitch math that I can find
self.pitch = math_random() * math.random(1,3) * random_pitch_multiplier[math_random(1,2)]
end
if distance_from_attacking >= self.reach then
mobs.set_yaw_while_attacking(self)
mobs.set_pitch_while_attacking(self)
mobs.set_fly_velocity(self, self.run_velocity) mobs.set_fly_velocity(self, self.run_velocity)
mobs.set_mob_animation(self,"run")
else else
mobs.set_yaw_while_attacking(self) mobs.set_yaw_while_attacking(self)
mobs.set_pitch_while_attacking(self)
mobs.set_fly_velocity(self, 0) local distance_from_attacking = vector_distance(self.object:get_pos(), self.attacking:get_pos())
mobs.set_mob_animation(self,"stand")
if distance_from_attacking >= self.reach then
mobs.set_pitch_while_attacking(self)
mobs.set_fly_velocity(self, self.run_velocity)
mobs.set_mob_animation(self,"run")
else
mobs.set_pitch_while_attacking(self)
mobs.set_fly_velocity(self, 0)
mobs.set_mob_animation(self,"stand")
end
end end
@ -313,6 +338,11 @@ mobs.projectile_attack_fly = function(self, dtime)
--shoot --shoot
if self.projectile_timer <= 0 then if self.projectile_timer <= 0 then
if self.fly_random_while_attack then
mobs.set_yaw_while_attacking(self)
self.walk_timer = 0
end
--reset timer --reset timer
self.projectile_timer = math_random(self.projectile_cooldown_min, self.projectile_cooldown_max) self.projectile_timer = math_random(self.projectile_cooldown_min, self.projectile_cooldown_max)
mobs.shoot_projectile(self) mobs.shoot_projectile(self)

View File

@ -15,6 +15,7 @@ mobs:register_mob("mobs_mc:ghast", {
spawn_class = "hostile", spawn_class = "hostile",
group_attack = true, group_attack = true,
hostile = true, hostile = true,
fly_random_while_attack = true,
hp_min = 10, hp_min = 10,
hp_max = 10, hp_max = 10,
rotate = 270, rotate = 270,