Project Zomboid

Project Zomboid

[B42/B41] Bandits NPC (v2)
Workaround for Zombie Lunge Attack: Lua Code for BanditUpdate.lua
Hi everyone, I think (I hope) I’ve finally fixed the crash issue caused by regular zombies performing a lunge attack on bandits.

**Important note:** I haven’t fixed access to zombie “Moodles” (since zombies don’t have any), but I’ve worked around the problem selectively.

What I did: I re-enabled lunge attacks for zombies against the player, while preventing regular zombies from targeting bandits when attempting that type of attack.

---

### Solution

**File:** `42\media\lua\client\BanditUpdate.lua`

#### In function `Banditize`

Line 116

local function Banditize(zombie, brain)

At line 139, add:

-- Prevents the bandit from being the target of a lunge attack
zombie:setVariable("NoLungeTarget", true)

Updated code:

-- this ♥♥♥♥ here is important, removes black screen crashes
-- with this var set, game engine skips testDefense function that
-- wrongly refers to moodles, which zombie object does not have
zombie:setVariable("ZombieHitReaction", "Chainsaw")

-- Prevents the bandit from being the target of a lunge attack
zombie:setVariable("NoLungeTarget", true)

-- stfu
zombie:getEmitter():stopAll()

---

#### In function `UpdateZombies`

Original line 1410:

local function UpdateZombies(zombie)

(now line 1413 due to earlier additions).

Delete line 1412:

zombie:setVariable("NoLungeAttack", true)

Replace with:

-- Only disable lunge for zombies targeting bandits
local target = zombie:getTarget()
if target and target:getVariableBoolean("Bandit") then
zombie:setVariable("NoLungeAttack", true)
else
zombie:setVariable("NoLungeAttack", false) -- Re-enable lunge for zombies targeting players
end

So the code becomes:

line 1413 -> local function UpdateZombies(zombie)

-- Only disable lunge for zombies targeting bandits
local target = zombie:getTarget()
if target and target:getVariableBoolean("Bandit") then
zombie:setVariable("NoLungeAttack", true)
else
zombie:setVariable("NoLungeAttack", false) -- Re-enable lunge for zombies targeting players
end

line 1423 -> if zombie:getVariableBoolean("Bandit") then return end

---

#### In function `OnBanditUpdate`

Original line 1747:

local function OnBanditUpdate(zombie)

(now line 1756 due to earlier additions).

At line 1770, add:

-- Check if zombie targets a player and not a bandit
local target = zombie:getTarget()
if target and instanceof(target, "IsoPlayer") and not target:getVariableBoolean("Bandit") then
-- If zombie is on the ground (crawling) and close enough to the player
if zombie:isCrawling() then
local zx, zy, zz = zombie:getX(), zombie:getY(), zombie:getZ()
local px, py, pz = target:getX(), target:getY(), target:getZ()
local dist = math.sqrt(((zx - px) * (zx - px)) + ((zy - py) * (zy - py)))

if dist < 0.80 and math.abs(zz - pz) < 0.3 and zombie:CanSee(target) then
-- Check if there is no wall between zombie and player
local isWallTo = zombie:getSquare():isSomethingTo(target:getSquare())
if not isWallTo and zombie:isFacingObject(target, 0.3) then
-- Enable lunging for players
zombie:changeState(LungeState.instance())
zombie:getPathFindBehavior2():cancel()
zombie:setPath2(nil)
return -- Important: Exit the function to avoid further processing
end
end
end
end

So the code becomes:

line 1768 -> if BanditCompatibility.IsRagdoll(zombie) then return end

-- Check if zombie targets a player and not a bandit
local target = zombie:getTarget()
if target and instanceof(target, "IsoPlayer") and not target:getVariableBoolean("Bandit") then
-- If zombie is on the ground (crawling) and close enough to the player
if zombie:isCrawling() then
local zx, zy, zz = zombie:getX(), zombie:getY(), zombie:getZ()
local px, py, pz = target:getX(), target:getY(), target:getZ()
local dist = math.sqrt(((zx - px) * (zx - px)) + ((zy - py) * (zy - py)))

if dist < 0.80 and math.abs(zz - pz) < 0.3 and zombie:CanSee(target) then
-- Check if there is no wall between zombie and player
local isWallTo = zombie:getSquare():isSomethingTo(target:getSquare())
if not isWallTo and zombie:isFacingObject(target, 0.3) then
-- Enable lunging for players
zombie:changeState(LungeState.instance())
zombie:getPathFindBehavior2():cancel()
zombie:setPath2(nil)
return -- Important: Exit the function to avoid further processing
end
end
end
end

line 1793 -> local id = BanditUtils.GetZombieID(zombie)

---

You’re free to use this code provided by **BlaBla** (coder on LoversLab forums).

Hopefully this will help the devs of the amazing **Bandits mod** for Build 42.
Have a great day !
Naposledy upravil 🌴 Zunder 🌴; před 23 hodinami
< >
Zobrazuje se 11 z 1 komentářů
Thank you, I hope this works. Try to message the developer, maybe he's interested.
< >
Zobrazuje se 11 z 1 komentářů
Na stránku: 1530 50