Author Topic: Exploding Buildings  (Read 1443 times)

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4955
Exploding Buildings
« on: May 26, 2007, 10:58:00 PM »
The follow details how buildings can spontaneously explode when damaged.


The function at offset 0xC in the Unit's virtual function table determines if a building should explode when damaged. The function takes no arguments, and returns 1 if the building exploded, and 0 otherwise. During unit processing, this function is called for each unit every 1179 game ticks (when tick MOD 1179 is 0). It will however only call this function for each successive unit if no building has exploded this way yet. (Once a building explodes, it sets the results of (tick MOD 1179) to 1, so it will prevent more than one building from exploding during the same round).

This function is typically a dummy stub that returns 0 for non buildings (although you could potentially override it if you wanted a vehicle that could explode when damaged). The default implementation for buildings works as follows (pseudocode):

Code: [Select]
If Unit.command = ctMoDevelop Then Return 0
If Unit.command = ctMoIdle Then Return 0
If Unit.command = ctMoUnDevelop And UnitType != Tokamak Then Return 0

If (Unit.flags And 0x180) = 0 Then Return 0

If Unit.damage < maxHitpoints / 10 Then Return 0

damageValue = ((Unit.damage * 10) / maxHitpoints) + 3
If UnitType = MagmaWell Then damageValue = (damageValue * 358) / 256  [About 140% greater]

If damageValue <= Rand(500) Then Return 0

Unit.DoDeath()

The maxHitpoint value is of course looked up for the creator of the unit, like always. (That is, UnitTypeInfo.playerInfo[Unit.creatorNum].maxHitpoints is used).