From 061602d9d46d4e4607e407c064070709ef99f9b7 Mon Sep 17 00:00:00 2001 From: jordan4ibanez Date: Sat, 17 Apr 2021 12:28:07 -0400 Subject: [PATCH] Overhaul separation of swimming and flying for ease of use with writing mobs api --- mods/ENTITIES/mcl_mobs/api/api.lua | 2 + .../mcl_mobs/api/mob_functions/ai.lua | 62 +++++++++---------- .../mcl_mobs/api/mob_functions/movement.lua | 29 ++++++++- mods/ENTITIES/mobs_mc/squid.lua | 3 +- 4 files changed, 61 insertions(+), 35 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/api/api.lua b/mods/ENTITIES/mcl_mobs/api/api.lua index 330fdcd84..47804b0d0 100644 --- a/mods/ENTITIES/mcl_mobs/api/api.lua +++ b/mods/ENTITIES/mcl_mobs/api/api.lua @@ -311,6 +311,8 @@ function mobs:register_mob(name, def) wandering = true, current_animation = "", gravity = GRAVITY, + swim = def.swim, + swim_in = def.swim_in or {mobs_mc.items.water_source, "mcl_core:water_flowing", mobs_mc.items.river_water_source}, --set_animation = mobs.set_animation, --end j4i stuff diff --git a/mods/ENTITIES/mcl_mobs/api/mob_functions/ai.lua b/mods/ENTITIES/mcl_mobs/api/mob_functions/ai.lua index fe091b5d2..794f8d2f1 100644 --- a/mods/ENTITIES/mcl_mobs/api/mob_functions/ai.lua +++ b/mods/ENTITIES/mcl_mobs/api/mob_functions/ai.lua @@ -173,26 +173,24 @@ end --[[ -______ _ _ _______ _ _ -| ___| | (_) / / ___| (_) (_) -| |_ | |_ _ _ _ __ __ _ / /\ `--.__ ___ _ __ ___ _ __ ___ _ _ __ __ _ -| _| | | | | | | '_ \ / _` | / / `--. \ \ /\ / / | '_ ` _ \| '_ ` _ \| | '_ \ / _` | -| | | | |_| | | | | | (_| |/ / /\__/ /\ V V /| | | | | | | | | | | | | | | | (_| | -\_| |_|\__, |_|_| |_|\__, /_/ \____/ \_/\_/ |_|_| |_| |_|_| |_| |_|_|_| |_|\__, | - __/ | __/ | __/ | - |___/ |___/ |___/ + _____ _ +/ ___| (_) +\ `--.__ ___ _ __ ___ + `--. \ \ /\ / / | '_ ` _ \ +/\__/ /\ V V /| | | | | | | +\____/ \_/\_/ |_|_| |_| |_| ]]-- -- state switching logic (stand, walk, run, attacks) -local fly_state_list_wandering = {"stand", "fly"} +local swim_state_list_wandering = {"stand", "swim"} -local fly_state_switch = function(self, dtime) +local swim_state_switch = function(self, dtime) self.state_timer = self.state_timer - dtime if self.wandering and self.state_timer <= 0 then self.state_timer = math.random(4,10) + math.random() - self.state = fly_state_list_wandering[math.random(1,#fly_state_list_wandering)] + self.state = swim_state_list_wandering[math.random(1,#swim_state_list_wandering)] end end @@ -203,13 +201,13 @@ local flop = function(self,dtime) end --this is to swap the built in engine acceleration modifier -local fly_physics_swapper = function(self,inside_fly_node) +local swim_physics_swapper = function(self,inside_swim_node) - --should be flying, gravity is applied, switch to floating - if inside_fly_node and self.object:get_acceleration().y ~= 0 then + --should be swimming, gravity is applied, switch to floating + if inside_swim_node and self.object:get_acceleration().y ~= 0 then self.object:set_acceleration(vector_new(0,0,0)) - --not be flying, gravity isn't applied, switch to falling - elseif not inside_fly_node and self.object:get_acceleration().y == 0 then + --not be swim, gravity isn't applied, switch to falling + elseif not inside_swim_node and self.object:get_acceleration().y == 0 then self.pitch = 0 self.object:set_acceleration(vector_new(0,-self.gravity,0)) end @@ -218,27 +216,27 @@ end local random_pitch_multiplier = {-1,1} -- states are executed here -local fly_state_execution = function(self,dtime) +local swim_state_execution = function(self,dtime) local pos = self.object:get_pos() pos.y = pos.y + self.object:get_properties().collisionbox[5] local current_node = minetest_get_node(pos).name - local inside_fly_node = false + local inside_swim_node = false - --quick scan everything to see if inside fly node - for _,id in pairs(self.fly_in) do + --quick scan everything to see if inside swim node + for _,id in pairs(self.swim_in) do if id == current_node then - inside_fly_node = true + inside_swim_node = true break end end --turn gravity on or off - fly_physics_swapper(self,inside_fly_node) + swim_physics_swapper(self,inside_swim_node) - --fly properly if inside fly node - if inside_fly_node then + --swim properly if inside swim node + if inside_swim_node then if self.state == "stand" then @@ -250,9 +248,9 @@ local fly_state_execution = function(self,dtime) --print("standing") - mobs.set_fly_velocity(self,0) + mobs.set_swim_velocity(self,0) - elseif self.state == "fly" then + elseif self.state == "swim" then self.walk_timer = self.walk_timer - dtime @@ -270,9 +268,9 @@ local fly_state_execution = function(self,dtime) end - mobs.set_fly_velocity(self,self.walk_velocity) + mobs.set_swim_velocity(self,self.walk_velocity) end - --flop around if not inside fly node + --flop around if not inside swim node else --print("flopping") flop(self,dtime) @@ -304,10 +302,10 @@ mobs.mob_step = function(self, dtime) - --swimming/flying - if self.fly then - fly_state_switch(self, dtime) - fly_state_execution(self, dtime) + --swimming + if self.swim then + swim_state_switch(self, dtime) + swim_state_execution(self, dtime) --regular mobs that walk around else land_state_switch(self, dtime) diff --git a/mods/ENTITIES/mcl_mobs/api/mob_functions/movement.lua b/mods/ENTITIES/mcl_mobs/api/mob_functions/movement.lua index 72f93ea24..0cf6a6ab2 100644 --- a/mods/ENTITIES/mcl_mobs/api/mob_functions/movement.lua +++ b/mods/ENTITIES/mcl_mobs/api/mob_functions/movement.lua @@ -11,6 +11,17 @@ local vector_multiply = vector.multiply local minetest_yaw_to_dir = minetest.yaw_to_dir + +--[[ + _ _ +| | | | +| | __ _ _ __ __| | +| | / _` | '_ \ / _` | +| |___| (_| | | | | (_| | +\_____/\__,_|_| |_|\__,_| +]] + + -- move mob in facing direction --this has been modified to be internal --internal = lua (self.yaw) @@ -71,6 +82,22 @@ mobs.jump = function(self, velocity) self.object:add_velocity(vector_new(0,velocity,0)) end + + + + +--[[ + _____ _ +/ ___| (_) +\ `--.__ ___ _ __ ___ + `--. \ \ /\ / / | '_ ` _ \ +/\__/ /\ V V /| | | | | | | +\____/ \_/\_/ |_|_| |_| |_| +]]-- + + + + --make mobs flop mobs.flop = function(self, velocity) @@ -106,7 +133,7 @@ end --this has been modified to be internal --internal = lua (self.yaw) --engine = c++ (self.object:get_yaw()) -mobs.set_fly_velocity = function(self, v) +mobs.set_swim_velocity = function(self, v) local yaw = (self.yaw or 0) local pitch = (self.pitch or 0) diff --git a/mods/ENTITIES/mobs_mc/squid.lua b/mods/ENTITIES/mobs_mc/squid.lua index f650472fc..09fa8b128 100644 --- a/mods/ENTITIES/mobs_mc/squid.lua +++ b/mods/ENTITIES/mobs_mc/squid.lua @@ -48,8 +48,7 @@ mobs:register_mob("mobs_mc:squid", { }, visual_size = {x=3, y=3}, makes_footstep_sound = false, - fly = true, - fly_in = { mobs_mc.items.water_source, "mcl_core:water_flowing", mobs_mc.items.river_water_source }, + swim = true, breathes_in_water = true, jump = false, view_range = 16,