From c6b662ce7ae58a37dea89a88ea1edac6243b62d1 Mon Sep 17 00:00:00 2001 From: kay27 Date: Tue, 22 Jun 2021 05:10:45 +0400 Subject: [PATCH] [mcl_mobs/api] Add mob flow code from Crafter --- mods/ENTITIES/mcl_mobs/api/api.lua | 1 + .../mcl_mobs/api/mob_functions/ai.lua | 31 ++++++-- .../mcl_mobs/api/mob_functions/flow_lib.lua | 78 +++++++++++++++++++ 3 files changed, 102 insertions(+), 8 deletions(-) create mode 100644 mods/ENTITIES/mcl_mobs/api/mob_functions/flow_lib.lua diff --git a/mods/ENTITIES/mcl_mobs/api/api.lua b/mods/ENTITIES/mcl_mobs/api/api.lua index d1840f671..639eb517d 100644 --- a/mods/ENTITIES/mcl_mobs/api/api.lua +++ b/mods/ENTITIES/mcl_mobs/api/api.lua @@ -129,6 +129,7 @@ end local api_path = minetest.get_modpath(minetest.get_current_modname()).."/api/mob_functions/" --ignite all parts of the api +dofile(api_path .. "flow_lib.lua") dofile(api_path .. "ai.lua") dofile(api_path .. "animation.lua") dofile(api_path .. "collision.lua") diff --git a/mods/ENTITIES/mcl_mobs/api/mob_functions/ai.lua b/mods/ENTITIES/mcl_mobs/api/mob_functions/ai.lua index d16d24929..88ce3274b 100644 --- a/mods/ENTITIES/mcl_mobs/api/mob_functions/ai.lua +++ b/mods/ENTITIES/mcl_mobs/api/mob_functions/ai.lua @@ -9,6 +9,8 @@ local minetest_get_item_group = minetest.get_item_group local minetest_get_node = minetest.get_node local minetest_line_of_sight = minetest.line_of_sight local minetest_get_node_light = minetest.get_node_light +local minetest_registered_nodes = minetest.registered_nodes +local flow = mobs.get_flowing_dir local DOUBLE_PI = math.pi * 2 local THIRTY_SECONDTH_PI = DOUBLE_PI * 0.03125 @@ -521,8 +523,8 @@ ______ _ | _| | | | | | | | | | |_| | \_| |_|\__, | - __/ | - |___/ + __/ | + |___/ ]]-- -- state switching logic (stand, walk, run, attacks) @@ -675,12 +677,12 @@ end --[[ ___ |_ | - | |_ _ _ __ ___ _ __ - | | | | | '_ ` _ \| '_ \ + | |_ _ _ __ ___ _ __ + | | | | | '_ ` _ \| '_ \ /\__/ / |_| | | | | | | |_) | \____/ \__,_|_| |_| |_| .__/ - | | - |_| + | | + |_| ]]-- @@ -787,8 +789,8 @@ ___ ___ _ _ _ | |\/| |/ _` | | '_ \ | | / _ \ / _` | |/ __| | | | | (_| | | | | | | |___| (_) | (_| | | (__ \_| |_/\__,_|_|_| |_| \_____/\___/ \__, |_|\___| - __/ | - |___/ + __/ | + |___/ ]]-- --the main loop @@ -1011,6 +1013,19 @@ function mobs.mob_step(self, dtime) end end + --mobs flow from Crafter + local pos = self.object:get_pos() + if pos then + local flow_dir = flow(pos) + if flow_dir then + flow_dir = vector.multiply(flow_dir,10) + local vel = self.object:get_velocity() + local acceleration = vector.new(flow_dir.x-vel.x,flow_dir.y-vel.y,flow_dir.z-vel.z) + acceleration = vector.multiply(acceleration, 0.01) + self.object:add_velocity(acceleration) + end + end + --mob is stunned after being hit if self.pause_timer > 0 then self.pause_timer = self.pause_timer - dtime diff --git a/mods/ENTITIES/mcl_mobs/api/mob_functions/flow_lib.lua b/mods/ENTITIES/mcl_mobs/api/mob_functions/flow_lib.lua new file mode 100644 index 000000000..aa64bfb4e --- /dev/null +++ b/mods/ENTITIES/mcl_mobs/api/mob_functions/flow_lib.lua @@ -0,0 +1,78 @@ +--this is from https://github.com/HybridDog/builtin_item/blob/e6dfd9dce86503b3cbd1474257eca5f6f6ca71c2/init.lua#L50 +local +minetest,vector,math,pairs,minetest_get_node,vector_subtract,minetest_registered_nodes += +minetest,vector,math,pairs,minetest.get_node,vector.subtract,minetest.registered_nodes + +local tab +local n +local function get_nodes(pos) + tab,n = {},1 + for i = -1,1,2 do + for _,p in pairs({ + {x=pos.x+i, y=pos.y, z=pos.z}, + {x=pos.x, y=pos.y, z=pos.z+i} + }) do + tab[n] = {p, minetest_get_node(p)} + n = n+1 + end + end + return tab +end + + +local data +local param2 +local nd +local par2 +local name +local tmp +local c_node +function mobs.get_flowing_dir(pos) + c_node = minetest_get_node(pos).name + if c_node ~= "mcl_core:water_flowing" and c_node ~= "mcl_core:water" then + return nil + end + data = get_nodes(pos) + param2 = minetest_get_node(pos).param2 + if param2 > 7 then + return nil + end + if c_node == "mcl_core:water" then + for _,i in pairs(data) do + nd = i[2] + name = nd.name + par2 = nd.param2 + if name == "mcl_core:water_flowing" and par2 == 7 then + return(vector_subtract(i[1],pos)) + end + end + end + for _,i in pairs(data) do + nd = i[2] + name = nd.name + par2 = nd.param2 + if name == "mcl_core:water_flowing" and par2 < param2 then + return(vector_subtract(i[1],pos)) + end + end + for _,i in pairs(data) do + nd = i[2] + name = nd.name + par2 = nd.param2 + if name == "mcl_core:water_flowing" and par2 >= 11 then + return(vector_subtract(i[1],pos)) + end + end + for _,i in pairs(data) do + nd = i[2] + name = nd.name + par2 = nd.param2 + tmp = minetest_registered_nodes[name] + if tmp and not tmp.walkable and name ~= "mcl_core:water_flowing" and name ~= "mcl_core:water" then + return(vector_subtract(i[1],pos)) + end + end + + return nil +end