On this page
Damage and armor: from a hit to the hit points
Two things do damage in this engine: a projectile that hits, and an explosion. Both end in one function of the game logic, which finds the victim's armor, scales the number for the teams, books the kill and takes the hit points. Everything that can be hurt (a soldier, a tank, a fence that breaks) has the same armor component.
A projectile's hit#
At the impact (bf2_w32ded.exe!0x0056a530) the damage is three factors:
damage = damageMod(projectile's material, struck material) x angle factor x projectile.getDamage(hit point)- The material pair. Every surface has a material id, and so does every projectile. The material manager (
0x005ed190) keeps a cell for each pair with a damage modifier, an effect, a decal and a sound. This table, in the game's data, is the whole of "what hurts what": that a rifle does little to a tank is a number in it, not a rule in the code. A pair without a cell takes the manager's default, 1.0. - The angle factor scales a glancing hit down, not below the armor template's
angleMod. getDamageis the damage over the distance.
Nothing is sent when the result is at or below 0.01.
The one way down#
GameLogic::addDamage (0x004599f0):
- the victim's armor is the first object with an armor component going up from the struck part to the root: a part may have its own armor, or pass the hit up;
- damage below 0.1 is dropped;
- the attacker's player, object and team are resolved;
teamDamageScale(0x00586c80) applies the server's friendly fire settings;handleDamage(0x00586eb0) books the kill, the assists and the events (scoring);- the armor's
addDamage(0x006325d0) limits the overkill and sets the hit points; a change of state (wreck, destroyed) runs the destruction: effects, an explosion of its own, the removal.
Damage a thing does to itself (burning below its critical level, drowning, lying upside down, a wreck bleeding out, leaving the map) goes through the same function with the armor's own object as the victim.
The armor template#
Every ObjectTemplate.armor.* line sets a field of the armor template (0x0060db30). The ones that shape play, with the constructor's defaults:
| Property | Default | Meaning |
|---|---|---|
hitPoints, maxHitPoints | 10, 10 | where it starts, and the ceiling for repairs |
wreckHitPoints | 10 | the hit points go down to minus this: the wreck's own health |
criticalDamage, hpLostWhileCriticalDamage | 0, 1 | at or below the first it loses the second every second (a burning vehicle) |
hpLostWhileUpSideDown | 0 | per second on its roof |
hpLostWhileInWater, ...InDeepWater | 0, 0 | per second, after waterDamageDelay 3 s / deepWaterDamageDelay 1 s |
waterLevel, deepWaterLevel | -0.3, 1 | how deep counts |
timeToStayAsWreck | 20 | seconds as a wreck; -1 is forever |
explosionDamage, explosionRadius, explosionForce | 0 | the blast when it is destroyed |
wreckExplosion* | 0 | the blast when the wreck goes |
defaultMaterial | -1 | the material an explosion's modifier is looked up against |
canBeDestroyed | 1 | 0 for things that only take marks |
angleMod | 0 | the floor of the angle factor |
The port's reader was run over every armor line of the shipped server archives: 3,369 lines in 159 templates, every property known and every value parsed.
Over time#
The armor's update (0x00631510) runs on the server:
- Critical: once a second, at or below
criticalDamage, the armor damages itself byhpLostWhileCriticalDamagetimes the time passed. - Water (
0x0062f910): the first damage after the delay, then every second while it stays in. Soldiers do not use this: they drown through their own update. - Wreck (
0x0062fac0): a wreck bleedswreckHitPoints / (timeToStayAsWreck x 30)each tick until it is at minuswreckHitPoints, and is then removed. A soldier's body does not bleed; it waits and fades.
Explosions#
An explosion (0x0056ddb0) asks every object within its radius and computes for each (0x0056d620):
d = the smaller of the distances to the object's position and to its sight point
damage = falloff(radius, d) x scale x damageMod(explosion's material, armor's defaultMaterial) x explosionDamage
falloff = 1 inside radius x 0.2, then a straight line down to 0 at the radius (0x0056c5f0)The 0.2 is an engine variable, the hot spot. Damage at or below 0.1 is dropped; the rest is queued and applied on the next tick.
Soldiers get two more tests.
- Line of sight (
0x0056d020): rays go from the centre, and from four points a little beside it, to three points of the soldier's skeleton with the weights 0.5, 0.35 and 0.15. The sum of the weights of the points that can be reached is the soldier'sscale. Something in the way that this same explosion is about to destroy counts half. So a wall saves you, a crate that the blast breaks half saves you. - The cone (
0x0056c640): an explosion with a cone angle (a claymore) hurts only soldiers inside the cone in front of it. Other objects are hurt as in a sphere.
The push (0x0056c7d0) is the same falloff times the explosion's force, times a ratio from the object's armor (explosionForceMod, capped by explosionForceMax) or, without an armor, from its mass. A soldier gets an upward part added, a tenth of the push under water, and a factor between 0.8 and 1.295 from the C runtime's random number.
That last factor comes from the process-wide rand(), not from a generator the game seeds. Two runs of the original with the same input throw a soldier differently. The port's function takes the drawn value as an input, and its test supplies it.
Heroes changes the blast in several ways the sweeps read: its cover ray lets thrown things through, a stomp or gravity weapon takes no sight test at all, a ducked hero in a tank takes no share, and shields are the armor's own. They are on Heroes: damage and widgets, each with its state in the port.
How it is tested#
| Step | Check |
|---|---|
| the armor template | every armor line of the shipped data parses |
| the armor's update core (critical, attacker timer) | 20,000 generated cases against the original function, sub-calls stubbed |
| the water timers, the wreck countdown | verified against the original functions |
| the falloff, damage to one object, the push, the cone, the sight factor | verified against the original functions |
| team damage scale | verified |
| the whole chain | recorded runs of the original server beside the port's: who was hit, for how much, in which tick |
Read as drafts in the notes, not verified: the explosion's entry function as a whole, the upside-down check's axis, and the kill bookkeeping's order of events.
In the code#
| What | Where |
|---|---|
| armor template and component | crates/bf2-gameplay/src/health (ArmorTemplate, Armor::tick_core, Armor::wreck_tick) |
| explosions | crates/bf2-gameplay/src/kills/explosion.rs (explosion_impulse, soldier_in_cone, soldier_sight_factor) |
| materials | crates/bf2-physics/src/material.rs |
| the chain on the server | crates/bf2-server/src/damage.rs, damage_explosion.rs, vehicles_armor.rs |
| the data test | crates/bf2-gameplay/tests/it_bf2/retail_armor.rs |
Notes: notes/subsystems/gameplay-damage.md, notes/subsystems/physics-materials.md, notes/subsystems/gameplay-kills.md.
docs/dev/weapons/damage-and-armor.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.