Sandtide RE: scripted damage (worm:damage)
Status: built for Melange 0.18.0 (
worm:damage, Wormsign contributormelange.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.
How the engine damages a worm [V]
Section titled “How the engine damages a worm [V]”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: explosions0x5ae4f0,Damage.Impulse0x5ae320(melee), poison0x5ac060(type 6, amount container+0x10c; when the poison would kill, it is cut tohealth - 1: poison never kills), fall damage0x5ac3e0(type 1,Worm.FallDamageRatio), Vapourize0x5ac160, mystery crates0x5ca1f0.ApplyDamage: the worm’sHandleMessage0x5b07c0onGameLogic.ApplyDamage(a broadcast every worm receives, at the turn end and after poison at the turn start), and Vapourize0x5ac160, 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.
What shows the damage [V]
Section titled “What shows the damage [V]”- The worm’s label (
WormHealthNameEntity, vtable0x867bf4): its Update (slot 6,0x5fdb40) calls0x5fd830every tick, which copies container+0x11einto its target value (+0x20); the shown value (+0x24) counts down to it. Any change of the u16 shows by itself. - The team bars (
EnergyBarManagerEntity):HandleMessage0x5ea830recomputes every bar (0x5e97f0re-sorts,0x5e9a00sets each team’s bar to the sum over its active worms ofhealth - (+0xcc)) on exactly three messages:GameLogic.EndTurn(0x960cd0),GameLogic.ApplyDamage(0x960cd8) andWorm.Damaged(0x960ce0). It also rebuilds onWormManager.Reinitialiseand, when needed, onGameLogic.Turn.Started. Nothing else subscribes toWorm.Damagedin the exe (the worm unit’s own object0x95fc08is only posted); client and sim mods may. - The floating number:
GSS.CreateDamageGraphicis handled by0x5009d0, which creates aDamageEntityand gives it the threeDamageGraphic.*values it cached at0x500210(handles+0x50,+0x54,+0x58).
Can a sim script call the engine path?
Section titled “Can a sim script call the engine path?”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:
ApplyDamageapplies 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+0x118around 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.- It moves half the amount to a vampire worm (
0x5a9710, active worm with flag0x200). - It always posts the floating number;
show = falsewould need container+0xe8forced to 4 around the call. - It does not refresh the team bars:
AddDamagepostsWorm.Damagedbefore the subtraction, so a bar recomputed on it reads the old health. Between turns (inside aholdTurnafterGameLogic.EndTurn) the bar would stay wrong until the next turn end. AddDamagealso setsWorm.WeaponDisableMovement = 0and the entity bytes+0xdc/+0xddthat 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}]):
- Refuses inside a
wum.sim.meleehit ("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. - Clamps
pointstohealth - 1: the worm keeps at least 1 health, so the death queue is never involved (the same rule poison follows). - Writes
health - amountto the u16 at+0x11e, exactly the storeApplyDamagemakes for damage that does not kill. Pending damage (+0x118,+0xcc) is left alone, so the turn-end apply still applies the turn’s explosions. - With
show: setsDamageGraphic.Amount,.Offsetand.PositionasApplyDamagedoes (by-name setters0x50b940,0x50bac0,0x50bc40;ActiveWormIndexread with0x50b790) and postsGSS.CreateDamageGraphic(a plainMessage, vtable0x81aa14). - Sets
DamagedWorm.Id(the slot) andDamageTypeTaken(6, poison’s type) asAddDamagedoes (0x50ba00), and postsWorm.Damaged: the bars recompute from the new health at once, and mods listening for it hear the damage.Worm.Damaged.Currentis 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 anddamage(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-tickholdTurnarmed atGameLogic.Turn.Ended,damage(50, {show})took a 100-health worm to 50. The floating number counted up to-50at 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 answeredwormsign.peerswithdiverged=0(3537 and 3564 ticks compared), and neither logged an abort,InvalidValTimestamporMissingTurnEndVal. The host showed the-99number and the worm’s label at 1.