Showing posts with label protection. Show all posts
Showing posts with label protection. Show all posts

Sunday, October 10, 2021

Do Gloves Protect You From Broken Glass?

Yes, gloves protect you from handling broken glass - any pair of gloves.  But gloves are not needed when removing broken glass from a smashed window, just when picking up broken glass off the ground.

You deal with broken glass whenever you smash a window.  There's a bit of broken glass remaining in the window frame, and glass on the ground by the smashed window.  You might want to get rid of that broken glass, and I used to think that required leather gloves to prevent injuries.  I was wrong.

Picking Up Broken Glass - Wear Gloves (Any)

You might want to pick up the glass off the ground.  When you or a zombie walks over it, it makes a sound.  Some people strategically place the glass as a sort of alarm for when zombies are near.  Some people just don't like the mess.

If you're not wearing gloves when you pick up the glass, you may injure yourself.

It starts in ISWorldObjectContextMenu.  If you are interacting with brokenGlass, you start executing some code:

    -- broken glass interaction
--    if brokenGlass and playerObj:getClothingItem_Hands() then
	if brokenGlass then
--        local itemName = playerObj:getClothingItem_Hands():getName()
--        if itemName ~= "Fingerless Gloves" then
            context:addOption(getText("ContextMenu_PickupBrokenGlass"), worldObjects, ISWorldObjectContextMenu.onPickupBrokenGlass, brokenGlass, player)
--        end
    end

You notice that there's some commented out code where Fingerless Gloves don't protect you when you pick up broken glass.  I don't know if this is old code or just code that hasn't been implemented yet.  If they put this functionality in (not here, though), I hope they include surgical gloves and long gloves.

The code calls onPickupBrokenGlass

ISWorldObjectContextMenu.onPickupBrokenGlass = function(worldobjects, brokenGlass, player)
    local playerObj = getSpecificPlayer(player)
    if luautils.walkAdj(playerObj, brokenGlass:getSquare()) then
        ISTimedActionQueue.add(ISPickupBrokenGlass:new(playerObj, brokenGlass, 100));
    end
end

That calls ISPickupBrokenGlass, and in the perform() method we see what happens:

function ISPickupBrokenGlass:perform()
	-- add random damage to hands if no gloves (done in pickUpMoveable)
	if ISMoveableTools.isObjectMoveable(self.glass) then
        local moveable = ISMoveableTools.isObjectMoveable(self.glass)
        moveable:pickUpMoveable( self.character, self.square, self.glass, true )
    end

	-- needed to remove from queue / start next.
	ISBaseTimedAction.perform(self)
end

That calls ISMoveableTools.isObjectMoveable to retrieve the glass (a ISMoveableSpriteProps object) (code not included here), and then actually picks it up in ISMoveableSpriteProps:

        elseif self.isoType == "IsoBrokenGlass" then
            -- add random damage to hands if no gloves
            if not _character:getClothingItem_Hands() and ZombRand(3) == 0 then
                local handPart = _character:getBodyDamage():getBodyPart(BodyPartType.FromIndex(ZombRand(BodyPartType.ToIndex(BodyPartType.Hand_L),BodyPartType.ToIndex(BodyPartType.Hand_R) + 1)))
                handPart:setScratched(true, true);
                -- possible glass in hands
                if ZombRand(5) == 0 then
                    handPart:setHaveGlass(true);
                end
            end

This means that ANY clothing on the hands protects you when picking up broken glass.  So you don't need leather gloves - fingerless gloves or surgical gloves or whatever will protect your hands completely from the broken glass.

Removing Broken Glass From a Window - Don't Need Gloves

You should all already know that if you climb through a broken window, there's a good chance you're going to get injured from the broken glass.  So you want to remove the glass from the window before you climb through, especially if it's a window you're going to be climbing through regularly (like at your base).

I haven't found any code that might injure a character removing glass from a broken window.

function ISRemoveBrokenGlass:perform()
	self.window:removeBrokenGlass()

	-- needed to remove from queue / start next.
	ISBaseTimedAction.perform(self)
end

Unlike what we found in the ISPickupBrokenGlass:perform() method, nothing here has any opportunity to injure the character.  If the removeBrokenGlass() method could injure the character, it would pass in the character object.

Afterword

So wearing any gloves is better than wearing no gloves.  Sure, long gloves don't offer any scratch or bite resistance, but they'll protect your hands when you pick up glass.


Thanks for reading!  If you notice a mistake or have any questions I might answer by looking through the code, let me know!

Saturday, October 2, 2021

How Indie Stone Fixed the Bandana 90% Protection Bug... Poorly

Edit 2021-10-27: Indie Stone released Project Zomboid build 41.56, and at first look I think they've fixed the bug correctly.  I'll write a post that goes into detail soon.

Two weeks ago, I posted about how, because of a bug, bandanas effectively gave you 90% protection from zombie attacks (from standing zombies attacking you from the front).

About a week after I posted about bandanas, Indie Stone posted a changelist for their upcoming (at the time) 41.55 Build, and they mentioned "Fixed bandanas providing far too much head protection"

Two days ago, they released 41.55. 

This bug was not fixed correctly - it was a band-aid.  If I'd seen this bug fix as a code review, I would have sent it back so they could get it right before release.

What Was the Bug?

I had actually filed a bug on the Indie Stone forums in August, but at the time I didn't realize the ramifications.  When I realized what was going on, I posted my sensational example, that bandanas were effectively giving 90% protection.

You can look at my posts about the bug, but here's the summary:

Sometimes, when a zombie successfully attacks you, a piece of clothing will fall off of you and nullify the attack.  These items of clothing are exclusively worn on the head, and are basically hats/helmets, glasses, and some masks.

The chance that a piece of clothing falls off during an attack is one of the attributes (ChanceToFall) of the clothing defined in the scripts.  Football helmets had a ChanceToFall of 3.  Bandanas had a ChanceToFall of 5.  Baseball caps had a ChanceToFall of 80.  Glasses had a ChanceToFall of 50.  The adjusted value was modified to be larger if the attack was directed at the head or neck.

This line from the code was the problem:

if (((Clothing)inventoryItem3).getChanceToFall() <= 0 || Rand.Next(100) < n) continue;

Where n is the adjusted chance to fall value, and the code that came after was the code that made the clothing fall.

It basically says if the random 1-100 number is less than the adjusted chance to fall, DON'T FALL.  It should  have been > n, not < n.  Easy mistake to make.

Note that it also says that if the clothing's defined ChanceToFall is 0, it will never fall, despite the flipped <.

So the result of this bug was that a bunch of pieces of clothing that should almost never fall off were falling off regularly (bandanas, motorcycle helmets, football helmets).  Also, a bunch of hats that should fall off most of the time weren't (tin foil hats, baseball caps, beanies).

How Did They Fix It?

1. They removed the feature of falling clothing protecting you from attacks

They went from 41.54

        if (Rand.Next(100) > n4) {
            n = 1;
            boolean bl4 = false;
            if (this.getParentChar().helmetFall(n3 == BodyPartType.ToIndex(BodyPartType.Neck) || n3 == BodyPartType.ToIndex(BodyPartType.Head))) {
                return false;

}

To 41.55

        if (Rand.Next(100) > n4) {
            n = 1;
            boolean bl4 = false;
            this.getParentChar().helmetFall(n3 == BodyPartType.ToIndex(BodyPartType.Neck) || n3 == BodyPartType.ToIndex(BodyPartType.Head)); 

They basically removed that helmets falling would return false, meaning that "helmets" falling (a poorly named method, because it covered glasses and masks as well) no longer nullified successful attacks.

2. They adjusted the ChanceToFall values to 0 for SOME of the items, allowing those items to catch the other condition that I mentioned in the code snippet earlier (and so, never fall).

Changed items (plus a few more) with 41.54 values:

Military Helmet                       10
Bandana (Head)                         5
Kentucky Baseball Helmet              10
Riverside Rangers Baseball Helmet     10
Z Hurricanes Baseball Helmet          10
Crash Helmet                          10
Motorcycle Helmet                      3
Police Motorcycle Helmet              10
USA Crash Helmet                      10
Firefighter Helmet                    20
Football Helmet                        3
Hard Hat                              20
Mining Helmet                         20
Hockey Helmet                         10
Jockey Helmet - 1                     10
Riding Helmet                         10
Riot Helmet                            1
Airforce Helmet                       10
Spiffo Suit Head                      10
Bandana (Tied)                         5
Bandana (Face)                         5

Changed items (plus a few more) with updated 41.55 values:

Military Helmet                       10
Bandana (Head)                         5
Kentucky Baseball Helmet               0
Riverside Rangers Baseball Helmet      0
Z Hurricanes Baseball Helmet           0
Crash Helmet                          10
Motorcycle Helmet                      0
Police Motorcycle Helmet               0
USA Crash Helmet                       0
Firefighter Helmet                    20
Football Helmet                        0
Hard Hat                              20
Mining Helmet                         20
Hockey Helmet                         10
Jockey Helmet - 1                     10
Riding Helmet                         10
Riot Helmet                            0
Airforce Helmet                        0
Spiffo Suit Head                      10
Bandana (Tied)                         5
Bandana (Face)                         5

(I'm glad I wrote a little python script to extract these values while researching my other post!)

So, in 41.55, falling clothes no longer protect you, and certain (inconsistently determined) helmets aren't falling anymore.

But they didn't fix the actual bug I had mentioned.  Certain helmets weren't falling anymore, but others were - the code that was causing helmets to fall but tin foil hats to not usually fall was untouched.

My Reaction?

This is a half-assed solution.

I thought it was a cute feature that sometimes an attack could be nullified by a piece of clothing falling off.  Someone at Indie Stone did too, otherwise they never would have implemented it.  If they wanted to remove it that is certainly their decision.  But based on the other part of the fix I don't think they thought it through.

Changing the ChanceToFall values for SOME of the items shows that they didn't actually understand or investigate the bug they were fixing.  Motorcycle helmets were falling off too easily - so the solution is to change the ChanceToFall from 3 to 0?  They changed the ChanceToFall vales to 0 for a handful of items.  What about all of the other items?  Army helmets were also falling off too easily, but they were left at 10.  Firefighter helmets and Hard hats, which I think are the most common of these very protective helmets, are still falling off way more than they should.  They should actually fix the bug.

Edit: What Would I Do?

This section was written half a day after I first published this post, based on a comment cool_fox made on Reddit.

What would have been the result if they'd done my suggested fix, which was to swap the < for a > or >= in the snip of code I referenced earlier.


                int n = ((Clothing)inventoryItem3).getChanceToFall();
                if (bl) {
                    n += 40;
                }
                if (inventoryItem3.getType().equals(string)) {
                    n = 100;
                }
                if (((Clothing)inventoryItem3).getChanceToFall() <= 0 || Rand.Next(100) < n) continue;

This is something I think they should still do.  It will fix the problem where secure hats/helmets are falling more often than hats that are not well secured on the head.

But some hats and helmets would still be falling off more than they should.  Baseball caps would be falling off 100% of the time when the neck or head was hit, and 80% of the time otherwise.

To maintain the spirit of the preexisting code, I'd let clothes continue to fall, but ONLY when the head or neck was targeted.  I'd also let them continue protecting the wearer when clothes were knocked off (because I thought it was a cute detail).  I'd remove this code, because the purpose of it is to make items fall off more easily when the head or neck is hit.

And in the BodyDamage class I'd basically change this 41.54 code:

        if (Rand.Next(100) > n4) {
            n = 1;
            if (this.getParentChar().helmetFall(n3 == BodyPartType.ToIndex(BodyPartType.Neck) || n3 == BodyPartType.ToIndex(BodyPartType.Head))) {
                return false;
            }

To something like this:

        if (Rand.Next(100) > n4) {
            n = 1;
            if (n3 == BodyPartType.ToIndex(BodyPartType.Neck) || n3 == BodyPartType.ToIndex(BodyPartType.Head)) {
                if (this.getParentChar().helmetFall(true)) {
                    return false;
                }
            }

The intent would be to ONLY allow for helmets (and glasses and masks) to fall if the attack targeted the head or neck.  At that point, baseball caps would fall off (and protect) you at 80%, and bandanas would fall off at 5%.  And I'd restore the old values of ChanceToFall that were changed from 41.54 to 41.55.  

I'd then re-evaluate the ChanceToFall values for items that should fall easily to make the fall a little less easily.  Should glasses fall off at 50%?  Should tinfoil hats fall off at 80%.  I'd first look at cutting all of the values (including the small ChanceToFall ones) in half.  I wouldn't want any item falling off and protecting you 50% of the time or more - that way, it would be a happy little accident rather than protection you could count on having.  Glasses would fall off and protect on 25% of head/neck hits, and baseball caps would fall off during only 40% of head/neck hits.  Reasonable enough.

And I'd look to see where else helmetFall was being called, because I wouldn't want to break the code elsewhere.

And then, since I'm not that familiar with Project Zomboid code (I'm not someone who looks at PZ's code professionally), and I don't own this piece of code, I'd get someone to take a look to confirm I'm seeing everything I think I should be seeing.  I'm sure if I owned the code, I'd be much more familiar with it and would have an easier time finding the consequences of the changes I'm proposing here.

I'd also look into refactoring helmetFall.  There's some duplicate code there, with half the code pertaining to zombies and the other half to players, but many of the lines are identical.  I suspect the reason this bug originated because a change was made to the zombie half of the code but wasn't copied to the human half of the code.  I can accept that at some companies, under some deadlines, you might accept a little sloppy duplication in your code, but if that code causes a bug because the code wasn't adjusted in all places, that is a strong argument to fix that duplication problem.

I'd also want to change the name of helmetFall, since the name doesn't quite match what it does.  helmetFall checks to see if certain headgear (hats/helmets, glasses, masks) falls, and then makes the headgear fall, and then returns true if it did fall.  If I wanted to continue keeping that as one method (for simplicity), I might change the name to "doesHeadgearFall".  I have a little grudge against the name "helmetFall", because the first time I examined this part of the code I assumed it only dealt with helmets/hats, and I initially thought it made the helmets fall instead of randomly determining if the helmets fell.  Now I have a distrust of method names in the rest of the Project Zomboid code.

Now, back to the rest of the original post.

Should They Fix Their Processes?

I don't know.  It depends.  How many bugs are they creating?  What is the cost of their bugs?  How much time are they spending fixing bugs rather than doing new development?

A Code Review probably would have helped.  A second set of eyes catches a lot of the things the first set misses.  But do they need code reviews for bugs like this?  Do they have the time?  It is a small team working on a game and they are almost desperately trying to get multiplayer out.  This is the kind of bug that hardly anybody notices, so the consequences of just giving it a quick shake and putting a band-aid on the most sensational part are small.  

Unit testing?  I like unit tests, but it probably wouldn't have helped here.  If you think the bug is that bandanas are giving too much protection and a handful of helmets are falling too easily, you are only going to write unit tests for bandana protection and checking if your motorcycle helmet stops falling.

But, if they missing things like this bug fix, what else are they missing?  

Epilogue

The Indie Stone isn't writing rocket guidance software or medical device software or even financial software, so the consequences of 99%+ of their bugs is very small.  And nobody expects their software to be 100% bug free.  But when they're fixing something they know they got wrong on their first pass (a bug), they should probably spend a little more time trying to do things right.  Because the people who discovered the bugs are watching closely.


Thanks for reading!  If you think I made a mistake or have any questions you'd like me to answer by looking through the code, let me know!

Wednesday, August 25, 2021

Why 100% Protection Doesn't Protect 100% of the Time

Update 2021-09-16: In Build 41.54 this has been fixed.  Holes are put in clothing AFTER protection is determined.

Every once in a while I'll hear (read) about someone complaining that they had 100% protection on a body part and still got hit by a zombie.

Should 100% protection protect you 100% of the time?  Maybe.  Maybe not.  But it doesn't.  Why?

Sometimes you get a hole in your clothing from an attack shortly before Project Zomboid calculates whether your clothing protects you from that attack.

The Code

The BodyDamage class is where damage (to your body) is determined.

Before we get to this part of the code, Project Zomboid has already randomly determined which body part is being attacked.

Right before the roll to see if you are hit, it checks to see if you will get a hole because of the attack

        bl = false;
        bl = this.getParentChar().addHoleFromZombieAttacks(BloodBodyPartType.FromIndex(n3));
        if (Rand.Next(100) > n4) {

addHoleFromZombieAttacks is in the IsoGameCharacter class and randomly determines if this zombie attack put a hole in a piece of clothing on the body part being attacked.  This is really important, because if there is a new hole, the protection on that part of your body goes down.

After it checks to see if you got a hole, it checks to see if you've been hit.  So it is possible to get a hole in your clothes and not get hit.

After Project Zomboid determines you've been hit, it figures out if it is a scratch or laceration or bite.  Here is code from what happens if you are scratched.  The code for lacerations and bites is almost identical to this part:

                Float f2 = Float.valueOf(this.getParentChar().getBodyPartClothingDefense(n3, false, false));
                if (this.getHealth() > 0.0f) {
                    this.getParentChar().getEmitter().playSound("ZombieScratch");
                }
                if ((float)Rand.Next(100) < f2.floatValue()) {
                    return false;
                }

getBodyPartClothingDefense adds up the clothing protection from the body part being attacked, with a maximum of 100.  Remember, though, that if you acquired a hole right before this, your protection is lower than it was right before the attack.

If a random 0-99 roll is less than defense value of your clothes, the protection holds and nothing else happens.

If the protection doesn't hold, then the code continues and you take the damage (and possibly get the zombie infection).

Some Thoughts

If you are wearing magical longjohns with 100% protection to your upper torso, a single attack can put a hole in it and reduce that protection to 0%.  

If you are wearing layers, a single hole will not reduce your protection by as much, and if your layers gives you more than 100% protection, you may still have 100% protection!

Some items, like the Hard Hat, can't have holes.  Unless a Hard Hat gets knocked off, you will never take damage to the head while wearing it.


Thanks for reading!  If you see a mistake or want me to answer a question about the code, please let me know!


Update:  Project Zomboid developer lemmy101 posted that this is unintentional and will be fixed.  I'll be sure to follow up on this topic when that happens!

Monday, August 23, 2021

How Head Protection Works

Edit 2021-09-18: I misinterpreted this code a little.  Here I say that the code checks to see if a hat/helmet falls off if the attack is on the head or neck.  The correct interpretation is that the code checks to see if any clothing with the ChanceToFall attribute falls whenever there is an attack (with the chance to fall being modified if the attack is on the head or neck).  For more about this, take a look at this newer blog entry.

Head and neck protection is just like protection for other body parts, with one little addition - your hat might fall off.

So how does it work?

When a zombie attacks you, here are the steps:

  • Find hit location (the body part being attacked)
  • Calculate (potential) damage
  • Check if attack puts a hole in the clothing for that body part
  • Check to see if the attack hits
  • Check if a hat falls off
  • Determine severity of the attack (scratch, laceration, bite)
  • Check if the clothing protection repels the attack
  • Actually damage the character (change the health stat, etc)

Every time you see your hat fall off your head, it is because you were HIT on the head or neck, but instead of you taking damage your hat just fell off.  If your hat stays on, it can still protect you from the attack.

Let's look at the code!

Hats Sometimes Fall

Once the code has determined your survivor is hit

        if (Rand.Next(100) > n4) {
            n = 1;
            boolean bl4 = false;
            if (this.getParentChar().helmetFall(n3 == BodyPartType.ToIndex(BodyPartType.Neck) || n3 == BodyPartType.ToIndex(BodyPartType.Head))) {
                return false;
            }

Before anything else actually happens, it checks to see if your "helmet" falls!  I say "helmets" but this applies to all hats!  The baseball cap has a much greater chance of falling off here.  If it does it returns and skips all the code that calculates the damage!  No damage!

Your hat just took the hit for you.

helmetFall actually leads to a bunch of code in IsoGameCharacter, where it determines whether or not your hat fell based on the ChanceToFall attribute in the scripts.  In vanilla PZ the hats are in clothing_hats.txt, and here's an example of the hardhat:

	item Hat_HardHat
	{
		Type = Clothing,
		DisplayName = Hard Hat,
		ClothingItem = Hat_HardHat,
		BodyLocation = Hat,
		IconsForTexture = HardHatYellow;HardHatBlue;HardHatRed;HardHatWhite,
		CanHaveHoles = false,
		BloodLocation = Head,
		BiteDefense = 100,
		ScratchDefense = 100,
		ChanceToFall = 20,
		Insulation = 0.15,
		WindResistance = 0.25,
		WaterResistance = 0.5,
	}

Hard Hats have a base 20% chance to fall.  If you look at the other hats, you can see Baseball Caps have an 80% chance to fall and Football Helmets have a 3% chance to fall.

One thing I find interesting is that even if the hat doesn't normally protect your neck, a neck attack can still trigger your hat to fall and avoid causing damage to you.

Hats Also Protect Like Other Clothing

Once you are hit, the severity of the hit is calculated and then it checks to see if your clothing protected you, also in BodyDamage:

                Float f2 = Float.valueOf(this.getParentChar().getBodyPartClothingDefense(n3, false, false));
                if (this.getHealth() > 0.0f) {
                    this.getParentChar().getEmitter().playSound("ZombieScratch");
                }
                if ((float)Rand.Next(100) < f2.floatValue()) {
                    return false;
                }

getBodyPartClothingDefense goes through your clothes and determines which clothing covers the body part that was hit and then adds up the defense values (ScratchDefense for scratches and lacerations, BiteDefense for bites).

Once the defense number (up to 100) is calculated, it plays the awful zombie scratch sound.  

Then, it randomly rolls to check if your defense protected you from the attack.  If it did protect you, you skip all the damage!

So this is why sometimes you heard the zombie crunch sound but when you check your health status you have taken no damage.  Your armor protected you.

There Might Be a Bug

On 2021-08-13 I filed a bug stating that the ChanceToFall was being used incorrectly, opposite to how it should be used.  This would mean that a Hard Hat with a 20% chance to fall is actually falling 80% of the time.

Whether or not I am right about the bug or if the bug has been fixed by the time you read this, falling hats protect you from attacks.

Conclusion

Always wear a hat!  Even if it doesn't protect you much, a fallen hat means you were completely protected from an attack that otherwise would have caused head or neck damage.  And when the hat doesn't fall off, it still protects you like other pieces of clothing protect other parts of your body.


Thanks for reading!  If you see a mistake here or if you have any questions you'd like me to answer, let me know!


Do Gloves Protect You From Broken Glass?

Yes, gloves protect you from handling broken glass - any pair of gloves.  But gloves are not needed when removing broken glass from a smashe...