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!


No comments:

Post a Comment

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...