From af4c42fea7112ada76fd9b273f771611532bdcf9 Mon Sep 17 00:00:00 2001 From: jordan4ibanez Date: Fri, 23 Apr 2021 17:10:44 -0400 Subject: [PATCH] Add skittish behavior (runaway from punch) and fix ocelot --- mods/ENTITIES/mcl_mobs/api/api.lua | 1 + .../mcl_mobs/api/mob_functions/ai.lua | 51 +++++++++++++++++-- .../api/mob_functions/interaction.lua | 28 +++++----- mods/ENTITIES/mobs_mc/iron_golem.lua | 3 +- mods/ENTITIES/mobs_mc/ocelot.lua | 2 + 5 files changed, 67 insertions(+), 18 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/api/api.lua b/mods/ENTITIES/mcl_mobs/api/api.lua index 373ac18b7..852bfd8af 100644 --- a/mods/ENTITIES/mcl_mobs/api/api.lua +++ b/mods/ENTITIES/mcl_mobs/api/api.lua @@ -337,6 +337,7 @@ function mobs:register_mob(name, def) fall_slow = def.fall_slow, projectile_cooldown_min = def.projectile_cooldown_min or 2, projectile_cooldown_max = def.projectile_cooldown_max or 6, + skittish = def.skittish, --end j4i stuff -- MCL2 extensions diff --git a/mods/ENTITIES/mcl_mobs/api/mob_functions/ai.lua b/mods/ENTITIES/mcl_mobs/api/mob_functions/ai.lua index f914ab9d5..09877ac2a 100644 --- a/mods/ENTITIES/mcl_mobs/api/mob_functions/ai.lua +++ b/mods/ENTITIES/mcl_mobs/api/mob_functions/ai.lua @@ -67,13 +67,26 @@ local land_state_list_wandering = {"stand", "walk"} local land_state_switch = function(self, dtime) + --do math after sure not attacking or running away + self.state_timer = self.state_timer - dtime + + --only run away + if self.skittish and self.state == "run" then + self.run_timer = self.run_timer - dtime + if self.run_timer > 0 then + return + end + + --continue + end + + --only attack if self.hostile and self.attacking then self.state = "attack" return end - --do math after sure not attacking - self.state_timer = self.state_timer - dtime + if self.state_timer <= 0 then self.state_timer = math.random(4,10) + math.random() @@ -185,7 +198,37 @@ local land_state_execution = function(self,dtime) elseif self.state == "run" then - print("run") + --do animation + mobs.set_mob_animation(self, "run") + + --enable rotation locking + mobs.movement_rotation_lock(self) + + --check for nodes to jump over + local node_in_front_of = mobs.jump_check(self) + + if node_in_front_of == 1 then + + mobs.jump(self) + + --turn if on the edge of cliff + --(this is written like this because unlike + --jump_check which simply tells the mob to jump + --this requires a mob to turn, removing the + --ease of a full implementation for it in a single + --function) + elseif node_in_front_of == 2 or (self.fear_height ~= 0 and cliff_check(self,dtime)) then + --turn 45 degrees if so + quick_rotate(self,dtime) + --stop the mob so it doesn't fall off + mobs.set_velocity(self,0) + end + + --only move forward if path is clear + if node_in_front_of == 0 or node_in_front_of == 1 then + --set the velocity of the mob + mobs.set_velocity(self,self.run_velocity) + end elseif self.state == "attack" then @@ -652,7 +695,7 @@ mobs.mob_step = function(self, dtime) self.object:remove() return false end - + --do death logic (animation, poof, explosion, etc) if self.health <= 0 then diff --git a/mods/ENTITIES/mcl_mobs/api/mob_functions/interaction.lua b/mods/ENTITIES/mcl_mobs/api/mob_functions/interaction.lua index 264eff9de..6cbc48b0b 100644 --- a/mods/ENTITIES/mcl_mobs/api/mob_functions/interaction.lua +++ b/mods/ENTITIES/mcl_mobs/api/mob_functions/interaction.lua @@ -1,5 +1,6 @@ local minetest_after = minetest.after local minetest_sound_play = minetest.sound_play +local minetest_dir_to_yaw = minetest.dir_to_yaw local math_floor = math.floor local math_min = math.min @@ -63,21 +64,24 @@ mobs.mob_punch = function(self, hitter, tflp, tool_capabilities, dir) return end - --neutral passive mobs switch to neutral hostile - if self.neutral then + --turn skittish mobs away and RUN + if self.skittish then - --drop in variables for attacking (stops crash) - self.attacking = hitter - self.punch_timer = 0 + self.state = "run" - self.hostile = true - --hostile_cooldown timer is initialized here - self.hostile_cooldown_timer = self.hostile_cooldown + self.run_timer = 5 --arbitrary 5 seconds - --initialize the group attack (check for other mobs in area, make them neutral hostile) - if self.group_attack then - mobs.group_attack_initialization(self) - end + local pos1 = self.object:get_pos() + pos1.y = 0 + local pos2 = hitter:get_pos() + pos2.y = 0 + + + local dir = vector_direction(pos2,pos1) + + local yaw = minetest_dir_to_yaw(dir) + + self.yaw = yaw end diff --git a/mods/ENTITIES/mobs_mc/iron_golem.lua b/mods/ENTITIES/mobs_mc/iron_golem.lua index 08f7cfe58..d4cd311a8 100644 --- a/mods/ENTITIES/mobs_mc/iron_golem.lua +++ b/mods/ENTITIES/mobs_mc/iron_golem.lua @@ -17,8 +17,7 @@ mobs:register_mob("mobs_mc:iron_golem", { passive = true, rotate = 270, hp_min = 100, - hp_max = 100, - rotate = 270, + hp_max = 100, protect = true, neutral = true, breath_max = -1, diff --git a/mods/ENTITIES/mobs_mc/ocelot.lua b/mods/ENTITIES/mobs_mc/ocelot.lua index bb62a54fa..8442d0614 100644 --- a/mods/ENTITIES/mobs_mc/ocelot.lua +++ b/mods/ENTITIES/mobs_mc/ocelot.lua @@ -30,6 +30,8 @@ local ocelot = { type = "animal", spawn_class = "passive", can_despawn = true, + rotate = 270, + skittish = true, hp_min = 10, hp_max = 10, xp_min = 1,