Health and Damage
Health is a number that hits take away and healing gives back, between zero and a top. Armor trims each hit but never to nothing, and a short protection time after a hit stops one blow from landing many times. At zero the character is down until it is revived.

Paste this to your AI
Copy it into Claude, Cursor or ChatGPT. It already has good values and the usual mistakes in it.
Add "Health and Damage" to my game.
What it should do: Health tells the player how much more they can take. It turns every enemy and hazard into a price and gives healing a value.
Build it exactly as this state machine. First check that it fits my game, then build it like this:
```mermaid
stateDiagram-v2
[*] --> healthy
healthy: Open to hits
protected: Protected
down: Down
healthy --> healthy: nothing lands
healthy --> protected: a hit lands
healthy --> down: the hit is deadly
protected --> protected: protection goes on
protected --> healthy: protection ends
down --> down: stays down
down --> protected: revived, protected
down --> healthy: revived, no protection
```
Use these starting values:
- maxHealth: 100 units
- protectionTime: 0.8 s
- armor: 2 units
- minDamage: 1 units
- reviveHealth: 50 units
Watch out for these common mistakes:
- Overkill counts as damage dealt: Tell your AI: report what was actually taken off, never more than the health that was left.
- Armor flips a small hit into healing: Tell your AI: after armor, a hit does at least the minimum, and a hit of zero or less does nothing.
- A hit and a potion in the same moment: Tell your AI: the hit counts first, then the healing, and a character that is down cannot be healed. Pick one order and keep it.
- A sliver of health reads as zero: Tell your AI: show health rounded up, so anyone still alive shows at least 1, and decide being down by the real number.
- Hits right after getting back up: Tell your AI: start the protection time on a revive, just like after a hit.
- Strange numbers in a hit: Tell your AI: count a hit or a healing only when it is a real number above zero. Everything else does nothing.
Do not change anything else in the game. Do not claim anything without proof: model what it takes, and show me what your solution rests on and how I can see that it works. If you cannot show something, say you do not know. When you are done, tell me in plain words how to try it.Or skip this and connect the MCP: your AI then finds all of this by itself.Connect the MCP
What it does
Health tells the player how much more they can take. It turns every enemy and hazard into a price and gives healing a value.
How it runs, as a diagram
Everything it can be in, and what moves it on. Your AI builds from exactly this.
How it runs, as a diagram
- Open to hits → Open to hits: nothing lands
- Open to hits → Protected: a hit lands
- Open to hits → Down: the hit is deadly
- Protected → Protected: protection goes on
- Protected → Open to hits: protection ends
- Down → Down: stays down
- Down → Protected: revived, protected
- Down → Open to hits: revived, no protection
Good starting values
| Value | Start with | What it changes |
|---|---|---|
| maxHealth | 100 units | How big the full bar is. 100 leaves room to make hits 1 percent or 10 percent of it without fractions. |
| protectionTime | 0.8 s | How long after a landed hit more hits are ignored. Long enough to see what happened and step away, short enough that the player still feels danger. Zero turns it off. |
| armor | 2 units | What is taken off every hit, flat. Small armor matters against small hits and fades against big ones. |
| minDamage | 1 units | The least a real hit does after armor. Above zero, armor can never make a character untouchable. Zero lets strong armor soak up a hit completely. |
| reviveHealth | 50 units | How much health a revive gives back. Half a bar gives a second chance without wiping out the cost of going down. It is cut to the full bar if that is smaller. |
What usually goes wrong
Overkill counts as damage dealt
A hit of 900 on a character with 3 health left is reported as 900 damage. Score, life steal or a damage number on screen then shows far more than the hit really did.
Fix: Tell your AI: report what was actually taken off, never more than the health that was left.
Armor flips a small hit into healing
A hit of 1 against armor 2 comes out as minus 1, and the character gains health from being hit.
Fix: Tell your AI: after armor, a hit does at least the minimum, and a hit of zero or less does nothing.
A hit and a potion in the same moment
At 20 health, a hit of 30 and a potion of 30 arrive together. If the potion counts first, the character lives. If the hit counts first, it dies. Without a rule, the result changes between runs.
Fix: Tell your AI: the hit counts first, then the healing, and a character that is down cannot be healed. Pick one order and keep it.
A sliver of health reads as zero
After many small hits, 0.0004 health is left. The bar rounds down to 0, yet the character is alive and the player is confused.
Fix: Tell your AI: show health rounded up, so anyone still alive shows at least 1, and decide being down by the real number.
Hits right after getting back up
A character revived next to the enemy that dropped it is hit again in the same breath, and goes down in a loop.
Fix: Tell your AI: start the protection time on a revive, just like after a hit.
Strange numbers in a hit
A broken formula hands over not-a-number, or an endless value. One such number poisons the health for the rest of the game, and the bar shows nothing.
Fix: Tell your AI: count a hit or a healing only when it is a real number above zero. Everything else does nothing.
How to check it works
- Your character has full health of 100 and armor of 2. a hit of 30 lands: health drops to 72 and the game counts 28 as taken.
- Armor is larger than the hit. a hit of 1 lands against armor 2: health drops by the minimum of 1, and with the minimum set to zero, it does not change at all.
- Your character has 10 health. a hit of 500 lands: health is exactly zero, the character is down, and only 10 is counted as taken.
- A hit has just landed. another hit lands right after: the second hit takes nothing off.
- A hit has just landed and hits keep arriving every moment. you wait out the protection time: the next hit lands after the protection time, and not sooner or much later.
- Your character has 95 of 100 health. healing of 40 arrives: health stops at exactly 100, and only 5 is counted as healed.
- Your character is down at zero health. healing arrives: health stays zero and nothing is healed.
- Your character is down. you revive it and a hit arrives right after: it stands up with the revive amount of health and the hit takes nothing off.
- Your character has 10 health. a hit of 50 and healing of 100 arrive at the same moment: the hit counts first, the character goes down, and the healing is lost.
- Your character has full health. a hit of zero arrives, or a hit that armor soaks up completely: no protection starts and the next real hit lands.
Works well with
- Stamina System: Players read two bars at once. Give them different colors and different speeds, so health feels precious and stamina feels temporary. Run both side by side. Health ends the run, stamina only limits the moves.
- Hunger and Thirst: The damage then passes through armor and protection time, unless you send it past them on purpose. Let hunger and thirst report damage over time, and let health apply it.
Common questions
how do health points work in games
A number between zero and a top. Hits take it down, healing brings it back up, and at zero the character is down. Good versions add armor, a short protection time after a hit and a rule for who counts first.
how long should invincibility after a hit last
Start with about 0.8 seconds. Shorter and a single touch can hit many times in a row, longer and the player stops feeling danger.
how does armor reduce damage
A flat amount comes off each hit, with a minimum so that small hits still do something. Flat armor is strong against many small hits and weak against one big hit.
what do i tell my ai to add health
Paste this: Add health from 0 to 100. A hit takes off its damage minus armor 2, but at least 1. After a hit, ignore hits for 0.8 seconds. Healing stops at full. At zero the character is down until revived, and a hit counts before healing.
Where it comes from
We could not find a reliable origin for this one yet.
What was checked
- 20 tests pass on the reference code.
- The state diagram was checked against the code, at the default values and at every listed low and high value.
Your AI gets the code
Tested example code comes through the Spielerando connection, so your AI can use it right away. How to connect it