0
0
Fork 0

Search the area before giving up when no mobs in table left

otherwise it will advance stages prematurely
This commit is contained in:
cora 2022-11-25 00:26:18 +01:00
parent dabc09adb2
commit 624092ddd3
1 changed files with 37 additions and 25 deletions

View File

@ -116,7 +116,7 @@ function mcl_raids.promote_to_raidcaptain(c) -- object
end end
function mcl_raids.is_raidcaptain_near(pos) function mcl_raids.is_raidcaptain_near(pos)
for k,v in pairs(minetest.get_objects_inside_radius(pos,128)) do for k,v in pairs(minetest.get_objects_inside_radius(pos,32)) do
local l = v:get_luaentity() local l = v:get_luaentity()
if l and l._raidcaptain then return true end if l and l._raidcaptain then return true end
end end
@ -176,6 +176,7 @@ function mcl_raids.spawn_raid(event)
local mob = mcl_mobs.spawn(p,m) local mob = mcl_mobs.spawn(p,m)
local l = mob:get_luaentity() local l = mob:get_luaentity()
if l then if l then
l.raidmob = true
event.health_max = event.health_max + l.health event.health_max = event.health_max + l.health
table.insert(event.mobs,mob) table.insert(event.mobs,mob)
mcl_mobs:gopath(l,pos) mcl_mobs:gopath(l,pos)
@ -253,6 +254,36 @@ local function make_firework(pos,stime)
minetest.after(10,make_firework,pos,stime) minetest.after(10,make_firework,pos,stime)
end end
local function is_player_near(self)
for _,pl in pairs(minetest.get_connected_players()) do
if self.pos and vector.distance(pl:get_pos(),self.pos) < 64 then return true end
end
end
local function check_mobs(self)
local m = {}
local h = 0
for k,o in pairs(self.mobs) do
if o and o:get_pos() then
local l = o:get_luaentity()
h = h + l.health
table.insert(m,o)
end
end
if #m == 0 then --if no valid mobs in table search if there are any (reloaded ones) in the area
for k,o in pairs(minetest.get_objects_inside_radius(self.pos,64)) do
local l = o:get_luaentity()
if l and l.raidmob then
local l = o:get_luaentity()
h = h + l.health
table.insert(m,o)
end
end
end
self.mobs = m
return h
end
mcl_events.register_event("raid",{ mcl_events.register_event("raid",{
readable_name = "Raid", readable_name = "Raid",
max_stage = 5, max_stage = 5,
@ -280,38 +311,19 @@ mcl_events.register_event("raid",{
if lv and lv.factor and lv.factor > 1 then self.max_stage = 6 end if lv and lv.factor and lv.factor > 1 then self.max_stage = 6 end
end, end,
cond_progress = function(self) cond_progress = function(self)
local m = {} if not is_player_near(self) then return false end
local h = 0 self.health = check_mobs(self)
for k,o in pairs(self.mobs) do
if o and o:get_pos() then
local l = o:get_luaentity()
h = h + l.health
table.insert(m,o)
end
end
self.mobs = m
self.health = h
self.percent = math.max(0,(self.health / self.health_max ) * 100) self.percent = math.max(0,(self.health / self.health_max ) * 100)
if #m < 1 then if #self.mobs < 1 then
return true end return true end
end, end,
on_stage_begin = mcl_raids.spawn_raid, on_stage_begin = mcl_raids.spawn_raid,
cond_complete = function(self) cond_complete = function(self)
local player_near = false if not is_player_near(self) then return false end
for _,pl in pairs(minetest.get_connected_players()) do
if self.pos and vector.distance(pl:get_pos(),self.pos) < 72 then player_near = true end
end
if not player_near then return false end
--let the event api handle cancel the event when no players are near --let the event api handle cancel the event when no players are near
--without this check it would sort out the unloaded mob entities and --without this check it would sort out the unloaded mob entities and
--think the raid is defeated. --think the raid is defeated.
local m = {} check_mobs(self)
for k,o in pairs(self.mobs) do
if o and o:get_pos() then
local l = o:get_luaentity()
table.insert(m,o)
end
end
return self.stage >= self.max_stage and #m < 1 return self.stage >= self.max_stage and #m < 1
end, end,
on_complete = function(self) on_complete = function(self)