Skip to content

Sandtide RE: scripted damage (worm:damage)

Status: built for Melange 0.18.0 (worm:damage, Wormsign contributor melange.wormdamage). These are the research notes; the API as built is documented in developer-guide.md, and where the two differ the guide is right. Addresses are build #1077. [V] means read in the disassembly, [G] seen in game.

Sandtide 1.1’s break-out costs the freed worm half its health. The cut has to look like any other damage (the worm’s label, the team bar, a floating number), never kill the worm, and be identical on every machine. These notes follow the engine’s own non-explosion damage (poison at the turn start, fall damage) to find what can be called from a sim script and what has to be reproduced.

Damage is a two-step affair on the worm’s logic entity (WXWormLogicEntity, slot byte +0x30).

Step Function What it does
Accrue AddDamage 0x5ab7e0 (thiscall, amount, type) Sets data Worm.WeaponDisableMovement = 0. Unless container flag 0x800: entity +0xdc = 1, +0xdd = 0; for types other than 5 and 6 it resets Turn.Boring and adds to Turn.FriendlyDamage/Turn.EnemyDamage (posting Worm.Damaged.Current when the worm is the current one, which ends its turn). It always sets data DamagedWorm.Id and DamageTypeTaken and posts Worm.Damaged. Unless the worm is waiting to die (flag 4) it adds the amount to the pending slot of its type in the copy-on-write XOM array at container +0x118 (data at +0x20): types 2, 3 and 4 capped at 75 (150 for a type-4 Wormpot rule), all doubled by DoubleDamage, type 6 uncapped. Types other than 6 feed the stats (0x613230).
Apply ApplyDamage 0x5abc50 (thiscall, no arguments) Returns at once when health is 0. Sums pending types 0..6 plus the signed container int +0xcc. If the sum is not 0 and container +0xe8 != 4: sets data DamageGraphic.Amount (the sum), DamageGraphic.Offset (0 for the active worm, else 17.0f from [0x829550]) and DamageGraphic.Position (container +0x38), posts GSS.CreateDamageGraphic; for the first pending type other than 6 it delay-posts Worm.DamageComplete 2.5 s later and enters the state “Worm Displaying Damage Taken” (the hurt animation, camera 0x51cf20); then 0x5a9710 gives half the sum as health to the active worm if it carries flag 0x200. Then: if the sum is below the health, health -= sum (u16 at +0x11e); otherwise “waiting to die” (0x5a70e0: flag 4, health 0), WormManager.SurrenderTeamById for flag 0x100, and GameLogic.AddMeToDeathQueue. Finally the pending array and +0xcc are zeroed.

Callers:

  • AddDamage: explosions 0x5ae4f0, Damage.Impulse 0x5ae320 (melee), poison 0x5ac060 (type 6, amount container +0x10c; when the poison would kill, it is cut to health - 1: poison never kills), fall damage 0x5ac3e0 (type 1, Worm.FallDamageRatio), Vapourize 0x5ac160, mystery crates 0x5ca1f0.
  • ApplyDamage: the worm’s HandleMessage 0x5b07c0 on GameLogic.ApplyDamage (a broadcast every worm receives, at the turn end and after poison at the turn start), and Vapourize 0x5ac160, which calls it directly (apply, AddDamage(health, type), apply again).

So poison at the turn start is AddDamage(n, 6) per poisoned worm, then the GameLogic.ApplyDamage broadcast; type 6 gives a floating number but no hurt animation, no Worm.DamageComplete and no stats.

  • The worm’s label (WormHealthNameEntity, vtable 0x867bf4): its Update (slot 6, 0x5fdb40) calls 0x5fd830 every tick, which copies container +0x11e into its target value (+0x20); the shown value (+0x24) counts down to it. Any change of the u16 shows by itself.
  • The team bars (EnergyBarManagerEntity): HandleMessage 0x5ea830 recomputes every bar (0x5e97f0 re-sorts, 0x5e9a00 sets each team’s bar to the sum over its active worms of health - (+0xcc)) on exactly three messages: GameLogic.EndTurn (0x960cd0), GameLogic.ApplyDamage (0x960cd8) and Worm.Damaged (0x960ce0). It also rebuilds on WormManager.Reinitialise and, when needed, on GameLogic.Turn.Started. Nothing else subscribes to Worm.Damaged in the exe (the worm unit’s own object 0x95fc08 is only posted); client and sim mods may.
  • The floating number: GSS.CreateDamageGraphic is handled by 0x5009d0, which creates a DamageEntity and gives it the three DamageGraphic.* values it cached at 0x500210 (handles +0x50, +0x54, +0x58).

AddDamage(n, 6) followed by ApplyDamage on that one worm is the poison path and can be called from a sim tick (the engine itself does it from Vapourize). It was not used:

  1. ApplyDamage applies the worm’s whole pending damage, not only the scripted amount. Mid-turn that includes every explosion that hit the worm earlier in the turn: it would be shown and applied early, and if it is lethal the worm would enter the death queue mid-turn, outside the engine’s own turn-end order. Isolating the amount means saving, zeroing and restoring the copy-on-write array at +0x118 around the call (its refcount at +4, edit counter at +0x1c): a write into engine-internal storage that is riskier than the health write it would replace.
  2. It moves half the amount to a vampire worm (0x5a9710, active worm with flag 0x200).
  3. It always posts the floating number; show = false would need container +0xe8 forced to 4 around the call.
  4. It does not refresh the team bars: AddDamage posts Worm.Damaged before the subtraction, so a bar recomputed on it reads the old health. Between turns (inside a holdTurn after GameLogic.EndTurn) the bar would stay wrong until the next turn end.
  5. AddDamage also sets Worm.WeaponDisableMovement = 0 and the entity bytes +0xdc/+0xdd that the poison rule reads.

The route that landed: the container write plus the engine’s own messages

Section titled “The route that landed: the container write plus the engine’s own messages”

This is the fallback of the plan (a container health write and a team-health refresh), done with the engine’s own messages so the HUD behaves as for poison. worm:damage(points[, {show}]):

  1. Refuses inside a wum.sim.melee hit ("in hit"), for a drowning worm (state 8), for a dying worm (state 7, health 0, flag 4 “waiting to die”, or a scripted kill posted) and for an inactive one.
  2. Clamps points to health - 1: the worm keeps at least 1 health, so the death queue is never involved (the same rule poison follows).
  3. Writes health - amount to the u16 at +0x11e, exactly the store ApplyDamage makes for damage that does not kill. Pending damage (+0x118, +0xcc) is left alone, so the turn-end apply still applies the turn’s explosions.
  4. With show: sets DamageGraphic.Amount, .Offset and .Position as ApplyDamage does (by-name setters 0x50b940, 0x50bac0, 0x50bc40; ActiveWormIndex read with 0x50b790) and posts GSS.CreateDamageGraphic (a plain Message, vtable 0x81aa14).
  5. Sets DamagedWorm.Id (the slot) and DamageTypeTaken (6, poison’s type) as AddDamage does (0x50ba00), and posts Worm.Damaged: the bars recompute from the new health at once, and mods listening for it hear the damage. Worm.Damaged.Current is not posted, so the call never ends the worm’s turn.

Determinism: the call runs inside the sim tick on every machine; the health is part of the engine’s worm hash, and the messages are posted from the sim at that tick. The per-slot ledger (health taken, calls, tick and size of the last call) is the Wormsign contributor melange.wormdamage.

The checks at install (worm_anchor.cpp): ApplyDamage’s health compare at 0x5abf09 and its message build at 0x5abe1a, RTTI Message on 0x81aa14, message alloc and Post; for the number also the four setter prologues, the vector setter’s type check (0x50bcaa) and fld [0x829550] at 0x5abd99 with 17.0f there. Without the first set, worm:damage answers nil, "unavailable"; without the second it still damages but shows no number.

Seen in game [G] (2026-11-26, private 0.18.0 build of this branch)

Section titled “Seen in game [G] (2026-11-26, private 0.18.0 build of this branch)”
  • Offline Quick Game (testenv C): mid-turn, 3 s into the first turn’s play, damage(50, {show}) took a 100-health worm to 50 and damage(999, {show}) took another to 1, alive; damage(10) on the worm whose turn it was took it to 90 and the turn went on to its time-out. Inside a 400-tick holdTurn armed at GameLogic.Turn.Ended, damage(50, {show}) took a 100-health worm to 50. The floating number counted up to -50 at the worm, the labels read 1 and 50 at the next turn’s intro, and the turn-end team bar for that team went from 58 % to 45 % of its length in the 3 s after the hold’s call (241 and 184 of 400 health).
  • LocalNet P1 (A hosting, B joining): the same calls on both peers at ticks 651 (mid-turn) and 2972 (in the hold, this time on the other team, including damage(999) to 1). Every logged health line of both peers was identical (81 common ticks up to 3700), both answered wormsign.peers with diverged=0 (3537 and 3564 ticks compared), and neither logged an abort, InvalidValTimestamp or MissingTurnEndVal. The host showed the -99 number and the worm’s label at 1.