From 9f7899c01ab7cb1d1b9e5b026e82c3a77a5656ab Mon Sep 17 00:00:00 2001 From: jordan4ibanez Date: Tue, 6 Apr 2021 20:37:48 -0400 Subject: [PATCH] Enderman use vectors to check distance between player and self --- mods/ENTITIES/mobs_mc/enderman.lua | 41 ++++++++++++++++-------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/mods/ENTITIES/mobs_mc/enderman.lua b/mods/ENTITIES/mobs_mc/enderman.lua index 40d859f973..5b228a9afc 100644 --- a/mods/ENTITIES/mobs_mc/enderman.lua +++ b/mods/ENTITIES/mobs_mc/enderman.lua @@ -331,27 +331,30 @@ mobs:register_mob("mobs_mc:enderman", { for _,obj in pairs(minetest.get_connected_players()) do -- Check if they are looking at us. local player_pos = obj:get_pos() - local look_dir_not_normalized = obj:get_look_dir() - local look_dir = vector.normalize(look_dir_not_normalized) - local look_pos = vector.new({x = look_dir.x+player_pos.x, y = look_dir.y+player_pos.y + 1.5, z = look_dir.z+player_pos.z}) -- Arbitrary value (1.5) is head level according to player info mod. - -- Cast up to 64 to see if player is looking at enderman. - for n = 1,64,.25 do - local node = minetest.get_node(look_pos) - if node.name ~= "air" then - break - end - if look_pos.x-1enderpos.x and look_pos.y-2.89enderpos.y and look_pos.z-1enderpos.z then - self.provoked = "staring" - self.attack = minetest.get_player_by_name(obj:get_player_name()) - break - else - if self.provoked == "staring" then - self.provoked = "broke_contact" + if vector.distance(enderpos, player_pos) <= 64 then + + local look_dir_not_normalized = obj:get_look_dir() + local look_dir = vector.normalize(look_dir_not_normalized) + local look_pos = vector.new({x = look_dir.x+player_pos.x, y = look_dir.y+player_pos.y + 1.5, z = look_dir.z+player_pos.z}) -- Arbitrary value (1.5) is head level according to player info mod. + -- Cast up to 64 to see if player is looking at enderman. + for n = 1,64,.25 do + local node = minetest.get_node(look_pos) + if node.name ~= "air" then + break end + if look_pos.x-1enderpos.x and look_pos.y-2.89enderpos.y and look_pos.z-1enderpos.z then + self.provoked = "staring" + self.attack = minetest.get_player_by_name(obj:get_player_name()) + break + else + if self.provoked == "staring" then + self.provoked = "broke_contact" + end + end + look_pos.x = look_pos.x + (.25 * look_dir.x) + look_pos.y = look_pos.y + (.25 * look_dir.y) + look_pos.z = look_pos.z + (.25 * look_dir.z) end - look_pos.x = look_pos.x + (.25 * look_dir.x) - look_pos.y = look_pos.y + (.25 * look_dir.y) - look_pos.z = look_pos.z + (.25 * look_dir.z) end end -- TAKE AND PLACE STUFF BEHAVIOUR BELOW.