Merge pull request 'mob_attack_better_strat' (#2774) from mob_attack_better_strat into master

Reviewed-on: https://git.minetest.land/MineClone2/MineClone2/pulls/2774
Reviewed-by: cora <cora@noreply.git.minetest.land>
This commit is contained in:
cora 2022-10-14 00:30:42 +00:00
commit 102469f7ab

View File

@ -2039,8 +2039,24 @@ local monster_attack = function(self)
local type, name = "", "" local type, name = "", ""
local min_dist = self.view_range + 1 local min_dist = self.view_range + 1
local objs = minetest.get_objects_inside_radius(s, self.view_range) local objs = minetest.get_objects_inside_radius(s, self.view_range)
local blacklist_attack = {}
for n = 1, #objs do for n = 1, #objs do
if not objs[n]:is_player() then
obj = objs[n]:get_luaentity()
if obj then
player = obj.object
name = obj.name or ""
end
if obj.type == self.type and obj.passive == false and obj.state == "attack" and obj.attack then
table.insert(blacklist_attack, obj.attack)
end
end
end
for n = 1, #objs do
if objs[n]:is_player() then if objs[n]:is_player() then
if mcl_mobs.invis[ objs[n]:get_player_name() ] or (not object_in_range(self, objs[n])) then if mcl_mobs.invis[ objs[n]:get_player_name() ] or (not object_in_range(self, objs[n])) then
@ -2058,6 +2074,7 @@ local monster_attack = function(self)
type = obj.type type = obj.type
name = obj.name or "" name = obj.name or ""
end end
end end
-- find specific mob to attack, failing that attack player/npc/animal -- find specific mob to attack, failing that attack player/npc/animal
@ -2074,16 +2091,24 @@ local monster_attack = function(self)
p.y = p.y + 1 p.y = p.y + 1
sp.y = sp.y + 1 sp.y = sp.y + 1
local attacked_p = false
for c=1, #blacklist_attack do
if blacklist_attack[c] == player then
attacked_p = true
end
end
-- choose closest player to attack -- choose closest player to attack
if dist < min_dist if dist < min_dist
and not attacked_p
and line_of_sight(self, sp, p, 2) == true then and line_of_sight(self, sp, p, 2) == true then
min_dist = dist min_dist = dist
min_player = player min_player = player
end end
end end
end end
if not min_player then
min_player=blacklist_attack[math.random(#blacklist_attack)]
end
-- attack player -- attack player
if min_player then if min_player then
do_attack(self, min_player) do_attack(self, min_player)