Regeneration
In StarCraft, regeneration is the slow increase of a unit's health, shields, or energy.
How It Works
To understand regeneration, you have to understand that Starcraft hides data from you. Health and Shields each are stored in memory in 4 bytes, however Starcraft only tells you the last 3 bytes. The first byte is actually a hidden value which acts as a timer for regeneration. 1 byte = 8 bits, or 28 = 256.
Each frame, a certain regeneration amount is added (or subtracted) to the first byte, according to values in the mpqs:
- Protoss Shields: 7
- Zerg Health: 4
- Energy: 8
- Shield Battery: 1280
- Recharge Shields: -640 (-632 net)
- Burning Terran Building: -20
- Medic: 200
- Personnel Cloaking(Ghost): -18 (-10 net)
- Cloaking Field(Wraith): -21 (-13 net)
- Heal: -100 (-92 net)
For repair:
SCVs repair units and buildings in a similar way, based on build time and total HP. Multiple SCVs increase the repair rate by adding them together.
R = roundup(0.9 * H/B)
where R = repair rate in hp per frame (of animation), with:
H = health of the unit
B = build time of the unit in SCMDraft (which is counted as frames of animation)
Further, R2 = 256*R, and R2 is the value that gets added or subtracted each frame to the first byte of a unit's HP.
One important note is that R2 is a 2 byte value, which means that when R2 > 65535, it loops back over to 0. Here is an example:
A Terran Barracks with a build time of 1 frame, HP of 9999.
R2 = 256 * roundup(0.9 * 9999/1) = 2303770. R2 > 65535, so it rolls over in multiples of 65535.
R2 = 2303770 - k65535, R2 = 10010. Note that this is still fairly large, and means that each SCV will repair approximately 39 HP every frame.
A slightly more realistic value is that of the default battlecruiser with 500 HP, 2000 frames build time:
R2 = 256 * roundup(0.9 * 500/2000) = 58.
R = R2/256 = 0.2266 hp per frame of animation
0.2266 hp per frame * 24 frames of animation per real-time second = 5.44 hp per real-time second.
Building units, following from repair:
Building units follows the same formula, and this explains why some buildings/units come out injured, except they start out with 10% of the base HP, plus 1 frame of "repairing", plus the time it takes to build. In the case of the terran barracks, it will build in 1 frame and come out with .1 * 9999 + 10010/256 + 10010/256 = 1078.1 HP.
Calculation of healing rates
The time used here is realtime. Since Starcraft is normally played on fastest, or 42ms/frame, we can calculate the approximate rates of healing:
- Let sp = shield points, hp = hit points, f = frames, ms = milliseconds, s = seconds, p = fraction of points
- Protoss shields use 7, (256p/sp / 7p/f) / (1000ms/s / 42ms/f) = 1.536 seconds per shield point.
- Zerg Health use 4, so (256/4)/(1000/42) = 2.688 seconds per health point.
- Medics' Healing uses 200, so (256/200)/(1000/42) = 0.05376 seconds per health point or ~18.6 health per second.
- Shield batteries use 1280, so 256/1280/24 = 0.0084 seconds per shield point or ~119 shields per second.
Other rates can be similarly calculated.