Skip to content

Monster AI: target search, the command, the path request, following, the tier AIs

What an AI event does once the tick reaches it: how the target is found, how a command becomes a mode, a skill and a path request, how a minion keeps to its leader, and what each of the 18 tier AIs decides, with its draws in order. How far each statement is evidenced is README § G and § H; the sentence past the first boundary is H1 there, quoted once and referred to below.

When Tag
Row kinds 1, 2, 4, 5 of an event [C] the kind switch: kind 2 calls it, 1 through MonAiTargetOrIdle, 4 and 5 through MonAiTargetOrWait; the kind column over the 148 rows
A think asks for its own hostile [C] 4 197 calls from the think code of 11 rows, with the decision’s game and unit
A think commands [C] SetModeAtUnit (999), UseSkill (295) and UseSeqSkill (61) each hold one MSM0 with their mode, unit and target or point, and MonAiMove’s MonSetMode commands 5 482 times under the walkers; [I] MonAiSetMode and a direct MonAiMove; nine more helpers command by their own MonSetMode call (B7a)
A think overrides the next plan [C] MonAiPathOverride then the next command’s first plan: type 1 → 7 (39), 2 (205), 7 (1 646), 13 (456), 0 → the command’s own; a non-zero byte is path+0x90 in 127 of 127; [I] AiPathOverrideSet called directly, the fatal range
Anchor Used for
MonStats.txt:aidist the search range
MonStats.txt:aip1 aip1..8, per difficulty
MonStats.txt:Skill1, MonStats.txt:Sk1mode the skills and modes the thinks use
MonStats.txt:Velocity, MonStats.txt:Run row 28’s and row 67’s speed terms
MonStats2.txt:MeleeRng the hireling’s melee approach
Hireling.txt:Skill1, Hireling.txt:Mode1, Hireling.txt:Chance1, Hireling.txt:ChancePerLvl1, Hireling.txt:DefaultChance MERCAI_PickSkill
Skills.txt:aitype, Skills.txt:aurastate, Skills.txt:aura MERCAI_PickSkill’s skips
State Type Source
the target lists ten heads at Game+0x10F8, 16-byte nodes {unit, flags, next, prev} § A
the unit’s list Unit+0xD0, 11 none § A
the parameter list AiCtl+0x20/+0x24 § E
the command MonCmd, 0x20 bytes § B
the one-shot override MonPathCtl+0x18/+0x1C/+0x20 § D
Name Value Tag
default range 35 when aidist is 0 [I]
player list skip 55 or more [I]
no-plan path byte 100; 101 → type 13 [C] commands with no override before them: 100 → no plan (805), 101 → first plan type 13 (2 214), 0 other
override parameter range −126..126, fatal outside; the byte capped at 77 [C] emulated, 240 cases each for the two functions

Every entry below uses these. [C] Range/RangeB give 0 with no step for signed n < 1, else low & (n − 1) or unsigned low mod n, Pow2 has no test, and D100() and chance(K) take the signed remainder and compare (120 cases each, the kept cat-emu-9 outputs)

Write Means
U, G, T, d the unit, the game, G+0xA8, G+0x6D
t, dist, melee ctx+0x08, ctx+0x14, ctx+0x18: the event’s target, distance and melee test
v14, v18, v1C AiCtl+0x14, +0x18, +0x1C (i32)
aip(K) the i16 at MonStats + 0x56 + 6(K−1) + 2d
D100() one step of U’s seed, giving the new low word mod 100, unsigned
chance(K) D100() < aip(K), a signed compare (jge past the taken branch)
Range(n) RNG_Range or RNG_RangeB on U’s seed: low mod n, and 0 with no step for n < 1
Pow2(n) RNG_RangePow2 on U’s seed
draw#k the k-th step of U’s seed in this decision, on the path shown; a helper’s own steps are in § F
Dist(a, b), InTown(u), Region(u) AiDistUnits; RoomIsTown(UnitGetRoom(u)); LogicRegionIdAt of the unit
FindTarget, FindHostile, Ovr(type, param, byte) MonAiFindTarget, MonAiFindHostile, MonAiPathOverride(U, type, param, byte)
SetModeAtUnit, UseSkill, Idle, Delay, Walk, Walk13, WalkZ, WalkStop, RunStop, WalkToPoint, Ring, T56, Toward, StepAway, StepAwayXY, WanderNear, WanderRingDirect § B and § C
PetMove, Follow, HireFight AiPetMove (T67b), AiHireFollow (T61a), AiHireFight (T61)

Modes are MonMode rows: 0 DT, 1 NU, 2 WL, 4 A1, 5 A2, 8 S1, 9 S2, 10 S3, 11 S4, 12 DD, 14 SQ, 15 RN.

  • A1. [C] The lists. Lists 0–7 are one per player slot: TargetListAddPlayer, from SUnitCreate for a player when fewer than 9 clients, takes the first free slot, and TargetListAttach(unit, i) links a unit after player i’s node (MERC_Init, MercRevive, SkillReviveDo, the summon do functions; 15 calls). So the player comes first, then its hirelings and summons. Lists 8 and 9: TargetListAdd(unit, 8 or 9) pushes at the head: 8 from WandererSpawn and two AI functions, 9 (decoys) from the inactive restore, MissileSpawnOwnedMonster, Attract, the confuse callback, Bone Wall and Bone Prison.
A2. MonAiFindTarget(game, unit, ctl, out dist, out melee): [I] (all of a–f)
a. if the unit has no room: return none
rec ← none; read ← 0
if AiCtl flag 0x40 is set: clear it; lineTest ← true ; one shot: it forces the test on
else if the room is outdoors: lineTest ← false
else:
lineTest ← AiCtl flag 0x08 is clear
a monster: rec ← its MonsterData+0x50 record
if lineTest and rec: read ← rec+0x24; lineTest ← read = 0
b. r ← MonAiForcedTarget; if r: go to f
c. if UnitAlignGet(unit) = 0:
best ← MonStats+0x52 + d, u8; 35 when 0 ; the range is the start of best
nearestSeen ← 0x7FFFFFFF
for each list 0..7:
head counts only when in the searcher's act (Unit+0x18 byte), in a room, and that room not a town
a counted head's AI distance updates nearestSeen
if that distance ≥ 55: skip the whole list
a dead head's distance reads as 0x7FFFFFFF
the head and the list's later nodes (no act, town or death test on these) are taken
when distance < best (unsigned) and (not lineTest or UnitLineBlocked(unit, cand, 4) = 0): best ← distance
list 8: act test, then the same rule against the same best
d. else: search kind 5 from the unit's logic region (LogicRegionIdAt), with best from 35
e. the decoys of list 9 (act test, the line test, their own best from 0x7FFFFFFF, no range; in d, the search's own).
MonAiPreferDecoy chooses the nearest over the found target
when there is none, or
when the decoy is nearer than 6 and a type-2 test plan to the found target has no point
(and search kind 7 finds nothing within 19)
f. with a target:
unless alignment 2: set flag 0x08; if rec: rec+0x24 ← (read = 0 ? 1 : 0)
melee ← InMeleeRange(unit, target, 0); dist ← its distance
without: melee ← 0; dist ← nearestSeen
  • A2a. [I] Distances here are AiDistToPoint from the unit’s own point (A7). So, forced targets aside, a target is never taken at or beyond aidist (35 when the cell is 0), a decoy has no such bound, and once a monster has had a target flag 0x08 turns the line test off until something clears it. MonAiMove’s flag 1, on a refused move, sets 0x40 (B6), so the next search tests the line again.
  • A3. [C] The forced target (MonsterData+0x38 kind, +0x34 guid; taunt, attract, confuse), emulated to its boundaries (200 cases): kind 0 returns 0 with no clear and a kind past 4 (unsigned) clears; kinds 1, 2 and 4 look up the guid as a player, a monster or a type-3 unit, none found clearing. With the function’s flag argument non-zero the found unit goes to UnitLineBlocked(found, unit, 4); with flag 0 it must be a living player or monster or a type-3 unit, or the kind and guid are cleared. Kind 3 draws RNG_RangePow2(unit seed, 2), calls MonAlignSet with the flips below, then the kind-6 search (flag non-zero) or the kind-5 search (flag 0). The out distance is AiDistUnits(found, unit), sized by the found unit. [I] On a blocked line the kind and guid are cleared; which caller passes which flag (the reading: MonAiFindHostile non-zero, MonAiFindTarget 0); what the searches return and the alignment restore. Kind 3 (confusion) draws RNG_RangePow2(unit seed, 2) and flips the alignment for the search: alignment 1 becomes 0 on a draw of 0 and 2 otherwise; on a non-zero draw alignment 0 becomes 2 and 2 becomes 0. It then searches kind 5 (the caller’s line test, from 35) or kind 6, and restores the first alignment.
  • A4. [I] MonAiFindHostile is the forced target, else search kind 6 and MonAiPreferDecoy between its nearest and the search’s decoy (the same rule as A2 e). After it, a think’s distance tests read what the entry says: some read ctx’s, some the hostile’s (0x7FFFFFFF without one).
  • A5. [C] The unit search. AiUnitSearch(game, unit, out, param, kind) takes g_AiSearchKinds[kind] = {walker, callback} (the callback from param when the table’s is 0) and dispatches walker 1, 2, 3, else 0; the table is 13 pairs, walkers 0,0,1,0,0,2,0,0,0,3,3,2,0, kinds 0–2 and 10 with no callback; its 29 calls pass kind 1 19 times, 6 twice, and 2, 3, 5, 7, 8, 9, 11, 12 once each (two tools). [I] What each walker walks and the kind roles. Walkers: 0 the near rooms’ unit lists (Room+0x74, next Unit+0xE8); 1 the near rooms’ client players (Room+0x48, count +0x78); 2 the near rooms that are not towns and hold a client; 3 the owner’s minion group. Kind roles, read from their callbacks: 3 nearest living non-hostile monster, 4 a monster in mode 0 near, 5 the alignment-2 search, 6 and 7 the nearest hostile within 49 with a line test, 8 a closed door, 9 the dead minions of the owner, 11 a hostile with a teleport point, 12 a count of living monsters of one class.
  • A6. [C] The hostility test with a draw. AiHostileCandidate (kinds 6, 7, 11) takes a living player or monster out of town with Unit+0xC4 bit 0x04 set, for a living searcher of type 0 or 1; the candidate’s death is tested before the searcher’s, and a candidate with no room counts as not in town. When the candidate has state 146 and is a player, it draws RNG_Range(candidate seed, 100) and, below 80, calls InMeleeRange(searcher, cand, 0); a monster with state 146 reaches that call with no draw. The rest return UnitsHostile. (400 cases) [I] The refusals after InMeleeRange: the player outside melee range, the monster inside it.
  • A7. [C] The AI distance. AiDistToPoint(unit, x, y): dx ← |ux − x|, dy ← |uy − y|, the result (2·max(dx, dy) + min(dx, dy)) / 2, a signed division (lea; cdq; sub; sar 1) on non-negative values, so a floor; the position from path +0x02/+0x06 (u16) for unit types 0, 1, 3 and from path +0x0C/+0x10 for 2, 4, 5, and 0 without a path. AiDistUnits(a, b) first subtracts the signed UNIT_GetSizeX(a) (for a monster the MonStats2 +0x08 signed byte) from both components, floor 0; AiDistToPointSized subtracts (size >> 1) + 1 with an unsigned shift, so a negative size is not halved as a signed value. Emulated for a of types 0, 1, 2, 4 and 5 (240 cases each). [I] An a of type 3, whose size is an item’s, was not run.
  • A7a. [C] AiDistToPoint’s map matches the emulation in 240 cases over the six unit types; the control (dx + dy) / 2 fails all 240. (dist)
  • A8. [C] UnitDistSq(a, b) is dx² + dy² mod 2³² (imul, add; 240 cases with 84 sums past 2³¹ − 1), compared signed by AiFallenCorpseCb and AiGreaterMummyCb. [I] The other callers’ compares; one square fits for |dx| ≤ 46 340, and no stock path reaches a wrap.
  • A9. [C] No light radius term, at the scan’s scope. The five search functions’ direct callees include no stat getter, and the ten getters’ 1 418 calls take no immediate stat id 89 (1 213 with an immediate id); no push 0x59 or mov r32, 0x59 comes within ten instructions before a getter call. [I] That no code in the image reads stat 89 by an immediate id is the wider reading; the scan finds 205 getter calls with no immediate id, where counted 215 with another getter list, and those calls are unresolved, so the walkers’ and callbacks’ code is not placed in or out. The server’s one visibility term found is state 146 in A6.
  • A10. [C] MonSkillTargetOk, which 13 calls in 10 AI functions use, reads TgtPlaceCheck, Skills +0x008 bit 0; see row 13 and row 22 below for the tier’s.
B1. MonCmdInit(unit, mode, cmd): [C] `+0x15` is 101 exactly where `MonModeEveryFrame` and the class's MonStats2 `*mv` bit say so, 19 431 `MSM0` in 69 logs
clear the used skill and the command; set mode and unit
cmd+0x15 ← 101 when the mode is every-frame (MonModeEveryFrame), else 100
B2. MonSetMode(game, cmd, flag): [I]
set the path target: a unit at cmd+0x08, else the point cmd+0x0C/+0x10
path+0x94 ← 20
MonMovePlan(cmd+0x15, cmd+0x18, AL = cmd+0x1C) ; § D
start the mode
on refusal: MonModeStartNeutral
B3. MonAiUseSkill(game, unit, mode, skill, target, x, y): [C] 295 calls: one `MSM0` with the call's mode, target and point, one skill start of the same skill inside, result 1 when the mode started; `Unit+0xC4` bit 0x40 set after the call in modes 4, 5, 7, 8, 9 (151 of 151); [I] mode 14, modes ≥ 16, the refusal's `Idle(10)`, `PathSetStopDist`, `SkillFindByOwner`
if mode ≥ 16: stop here
MonCmdInit(mode)
UNIT_SetUsedSkill(SkillFindByOwner(unit, skill, −1))
Unit+0xC4 |= 0x40
PathSetStopDist(path, 1) ; path+0x93 ← 0
target, x, y into the command
if MonSetMode(game, cmd, 0) starts: return 1
MonAiIdle(10); return 0
B4. MonAiUseSeqSkill(skill, target, x, y): [C] (all of B4) code's map to `MonSetMode`, 120 cases
0 when skill < 0 or skill ≥ the Skills count (signed)
the same as B3 in mode 14, without the 0x40 bit; the used skill ← SkillFindByOwner(U, skill, −1), 0 when none
PathSetStopDist(1); MonSetMode(G, cmd, 0) ; flag 0
B5. MonAiSetMode(mode, x, y), MonAiSetModeAtUnit(mode, target): [C] `SetModeAtUnit`'s 999 commands with its mode, unit and target; [I] `MonAiSetMode` (no call logged), the flag 1, the self-removal
build a command at a point or a unit; MonSetMode(…, 1)
MonAiSetMode(0, 0, 0) is the AI's self-removal
B6. MonAiMove(game, unit, target, mode, x, y, stop, flags) → 0 or 1: [C] the refusals: `Walk(t, 7)` takes 1 step with no command and returns 1 at `low mod 100 ≥ 70` (47), else 5 steps with the ring's command (96); `Walk(t, 0)` 0 steps, no command, result 0 (44); [I] mode 15 under state 60, `path+0x93`, flag 1's `0x40`, flag 4 alone
if mode = 15 and state 60: clear the one-shot override; mode ← 2
path+0x93 ← stop − 1 ; 0 outside 1..19
the command at the unit or point; r ← MonSetMode(…, 1)
if r ≠ 0: return 1
flags & 4: cancel the AI event
flags & 1: set AiCtl flag 0x40 ; for a monster with a control block
if flags & 2 is clear: return 0 ; no idle, no draw: nothing is queued here
one step of U's seed ; 1 step
if low mod 100 < 70: MonAiWanderRing(4) else MonAiIdle(10)
return 1
  • B7. [C] The walkers around MonAiMove. MonAiWalkToUnit(t, flags) (Walk), MonAiWalkToUnitStop(t, n) (WalkStop), MonAiWalkToUnitType13 (Walk13: override type 13), MonAiWalkToUnitOrZero (WalkZ), MonAiRunToUnit, MonAiRunToUnitFlags, and the point forms MonAiWalkToPoint, MonAiRunToPoint (WL under state 60), MonAiWalkToPointOrStop (cancels the AI event on refusal). MonAiRunToUnitStop(t, stop) (RunStop) is MonAiMove(t, 15, 0, 0, stop, 0); it and WalkStop replace only the stop word’s low byte by 1 when that byte is 0, the high bytes passing unchanged. The ten walkers’ maps to their MonAiMove or MonSetMode call are emulated (arguments, command block, seed, stop byte; 120 cases each). [I] What happens on a refusal lies past that call: Walk(t, 7) cancels the AI event, sets flag 0x40 and takes B6’s draw and fallback, Walk(t, 0) returns 0 with nothing queued, and WalkToPointOrStop cancels.
  • B7a. [C] Helpers that command themselves. Besides the five commands of § B, nine AI helpers call MonSetMode directly in the logs: MonAiWanderNearUnit (1 561 calls), MonAiMoveToUnitDist (453), MonAiMoveToPoint (210), MonAiWalkToPoint (198), MonAiWalkToPointCmd (134), MonAiWalkToPointOrStop (42), MonAiWalkToUnitOrZero (34), MonAiIdle (4), MonAiWanderRingDirect (4).
B8. Ring(r) = MonAiWanderRing(r): [C] (all of B8) code's map to its `MonAiMove` call, B8a
step ; draw 1
even low word: dy ← r & 0xFF; dx ← RangeB(seed, r & 0xFF) ; draw 2, none when r = 0
odd low word: dx ← r & 0xFF; dy ← RangeB(seed, r) ; draw 2, none when r = 0
step; negate dx on an odd low ; next draw
step; negate dy on an odd low ; next draw
MonAiMove(0, 2, x + dx, y + dy, 1, 0)
  • B8a. [C] This map up to its call of MonAiMove (arguments and the unit seed at the boundary, site) matches the emulation in 200 cases; the control with the parity reversed fails the 177 that separate it. MonAiMove and after are not covered. (ring)
  • B8b. [C] MonAiWanderRingDirect, MonAiWanderNearUnit and MonAiRunNearUnit do alike, to their MonSetMode call (120 cases each): WanderRingDirect centres on the unit itself and calls MonSetMode(G, cmd, 1) in WL, not MonAiMove; WanderNearUnit with a null other is Ring(2); RunNearUnit takes RN, or WL with the override words cleared under state 60, tested after the draws. All three take the radius as r & 0xFF.
B9. StepAway(other, d, flag) = MonAiStepAway: [C] (all of B9) code's map to its `MonAiMove` call, 120 cases
the point d & 0xFF subtiles from the unit on each axis, away from other, 0 on an equal axis
if d & 0xFF > 5: MonAiPathOverride(0, d)
MonAiMove(0, 2, x', y', 1, flag ? 4 : 0) ; no draw
B10. StepAwayXY(o, n, f) = MonAiStepAwayXY: [C] (all of B10) code's map to its `MonAiMove` call, 120 cases
sx, sy: sign of (ux − ox), (uy − oy), unsigned compares, 0 on an equal axis
if (u8) n > 5: AiPathOverrideSet(0; 0, n)
return MonAiMove(0, 15, ux + (u8) n · sx, uy + (u8) n · sy, 1, f ? 4 : 0) ; no draw in its body
B11. T56(target, byte, flag) = MonAiWalkToUnitType56: [C] (all of B11) code's map past the override to its `MonAiMove` call, 120 cases
one step of U's seed ; 1 step
MonAiPathOverride(type 5 when the new low byte < 0x80, else 6; the byte)
MonAiMove(target, 2, 0, 0, 1, flag ? 4 : 0)
B12. Toward(t, max, ring) = MonAiWalkTowardUnit → MonAiMoveToUnitDist(t, 2, max, ring): [C] (all of B12) code's map to `MonSetMode`, 120 cases each; the strictness of `D < ring`, `n > max`, `s < n` gives the same point either way
D ← AiDistUnits(U, t); dir ← −1 when D < ring, else +1
n ← min(|D − ring|, max)
dx ← |tx − ux|; dy ← |ty − uy|; s ← max(dx + dy, n)
if s > 0:
ix ← dx·n ÷ s; iy ← dy·n ÷ s ; signed division
while ix + iy < n: ix ← ix + 1; iy ← iy + 1
point ← (ux + sgn(tx − ux)·ix·dir, uy + sgn(ty − uy)·iy·dir)
MonCmdInit(U, mode); PathSetStopDist(path, 1); the point into the command; MonSetMode(…, 1) ; no draw
  • B9a. [C] StepAway’s and T56’s maps up to their first boundary (MonAiPathOverride at sites, or MonAiMove) match the emulation: 200 flee cases (8 with a null other return 0, 100 reach the override, 92 the move) and 120 typedraw cases; the controls fail all separating cases (69 and 120).

Idle(n), Delay(n) are README § C. [C] The next AI event at T + n (README C2, C3). [I] Their form at n ≤ 0, none logged

The planners behind the request are path/search’s and read the live grid. The type table, its flags and its draws are path/overview § D; this section is what the AI asks for.

  • D1. [I] Its parts. The command’s target unit or point; the path type MonCmd+0x15; the one-shot override in MonPathCtl (+0x18 type, +0x1C step parameter, +0x20 byte); path +0x93 (stop distance) and +0x94 = 20.
D2. MonAiPathOverride(unit, type, param, byte): [C] (all of D2) code's map, 240 cases each for the two functions
param outside −126..126 is fatal
if type = 1: type ← 7
AiPathOverrideSet(type, param, byte) ; ECX the unit's MonsterData path control, 0 with no MonsterData:
; the non-zero words are then written through a null pointer
AiPathOverrideSet(type, param, byte): [C]
each word is stored only when non-zero, so a 0 keeps what an earlier call set this event
byte: u8, capped at 77
D3. MonMovePlan(type, param, byte): [I]
take each non-zero override word; clear all three
if type = 100: no plan
if type = 101: type ← 13
byte defaults to 5
MonPathPlanType(type)
MonPathCtl+0x10 ← the step parameter
MonPathCtl+0x14 ← 10 when the plan was not type 1 and the path has a target unit, else −1
D4. MonPathPlanType(type): [I]
path+0x90 ← byte; the type; PathPlan(path, unit, 0)
with no point after types 2, 7, 9 or 13: plan type 15
  • D6. [C] Inside decisions the log holds 9 934 plan entries: types 2 (148), 5 (217), 6 (203), 7 (1 961), 13 (5 825) and 15 (1 580). Of them 9 618 have an exit line, and none of those steps the unit seed. The other 316 have no exit line, why is not examined, and the seed statement does not cover them. Type 1 was not requested in that run.
  • D8. [I] The town. For a hostile searcher a player head in a town room is never a target and its list’s pets are skipped with it, and every AI path type ends in a planner that PathPlan runs only after PathTargetNotTown.
  • E1. [C] MonAiKeepNear, emulated to its boundaries inside MonAiPreChecks (200 cases). With AiCtl+0x0C = −1 it returns 0. It clears AiCtl+0x0C to −1 when the class may not take mode 2, when the unit (AiCtl+0x10, AiCtl+0x0C) is not found, or when it is dead. The distance compares are signed: at AI distance d ≤ 1 from a player (type 0) it calls MonAiStepAway(unit, 19, 1), while a monster at d ≤ 1 returns 0; past 20 it sets MonAiPathOverride(U, 7, 0, 40) and calls MonAiWanderNearUnit(unit, 19); between, it returns 0. [I] After each call it stops the event; on a refused step it goes straight to MonAiWanderNearUnit(unit, 19) and stops. At 21 or more it sets MonAiPathOverride(U, 7, 0, 40) and wanders to a ring point of radius 19 around that unit (MonAiWanderNearUnit(unit, 19), 4 unit-seed steps), and stops. Between, the AI goes on.
  • E2. [C] Who sets the pair. AiCtlFollowSet writes it from UniqueMinions (minion → its boss), SkillReviveDo, SummonFinish and SkillDo71Taunt, its 4 calls (three tools): +0x0C the other’s +0x0C, +0x10 its type (or −1, 1); in UniqueMinions the unit just made gets its input unit.
  • E3. [C] The owner and the group. MonSetOwner writes AiCtl+0x28, +0x2C, +0x30, MonsterOwner(U) reads the three into GameFindUnit, and MonGroupForEach reads them, then the owner’s +0x34. [I] A leader is a unit whose owner is itself.
  • E4. [C] The parameter list at AiCtl+0x20/+0x24: records of 0x1C bytes {next, prev, kind, +0x0C, +0x10, +0x14, +0x18} in a circular list, +0x20 the head. AiParamsAlloc links a zeroed record before the head (the first record becomes the head); AiParamsPush(unit, blk) copies blk+0x08..+0x18 into a new record and makes it the head; AiParamsHead returns the head; AiParamsPop unlinks and frees it; AiParamsFind(unit, kind, makeHead) scans from the head’s next record round to the head, so the head is examined last, returns the first record of that kind met and with makeHead makes it the head. AiParamsHead and AiParamsFind emulated, 120 cases each. [I] AiParamsAlloc, Push and Pop past their pool calls.
  • E5. [C] A leader’s call. MonHelpBlock has 146 logged calls (row 13 126, row 6 11, row 20 2, row 65 7); rows 6, 13 and 20 pass a block of kind 1 whose other four words are non-zero, and 81 blocks reappear word for word as a later AID head record of another unit. Row 65 passes a kind-14 block (T30a). [I] MonHelpBlock(unit, blk) pushes a copy of blk to each minion of the unit’s owner, the AiCtl+0x34 list. Rows 6, 13 and 20 write only the kind word (+0x08) of their stack block; the other four words the copies take (+0x0C..+0x18) are whatever the stack held.
Function Seed Draws Tag
Ring, WanderRingDirect, WanderNearUnit, RunNearUnit acting unit 4 steps, 3 when r & 0xFF = 0 [C] B8a for Ring; the other three, 46 cases at 0
MonAiMove, refusal with flag 2 acting unit 1 step, then 4 more on low mod 100 < 70 [C] 143 of 143 refused Walk(t, 7)
T56 acting unit 1 step [C] the seed at MonAiMove, 120 cases
MonAiForcedTarget, confusion acting unit 1 (RangePow2) [C] 48 cases
AiHostileCandidate, a player with state 146 the candidate player 1 (Range 100); a monster with state 146 and the other paths none [C] 7 cases, the seed compared
state row 2’s acting unit 1 or 2 steps, then maybe a ring walk [I]
UModTeleportTry acting unit; SpawnPosPick as drlg/lifecycle Range(100) < 15 after the life-and-word gate, before the pick; Range(100) < 25 only with the life percentage below 30, after it [I]
MonSetMode an object’s Sync start, the skill start functions anim/timing § N, skills/dispatch § P [C] of 9 416 calls with an exit, 9 166 leave the unit seed unchanged and 175 move it inside one skill start; [I] 75 A1/A2 commands of SetModeAtUnit move it 1 or 2 steps with no skill start logged, source unread (Open questions)
PathPlan, the AI’s types none [C] (D6)

Each entry is the think function as pseudocode, with its draw order and its evidence. Every entry’s block is a code reading, [I], and its first boundary is [C] per README § G. “The map” in a [C] line below is the map from the think’s inputs to its first callee or return, each emulated case’s boundary, and nothing after it. Where a row is under sentence H1, “H1 holds” means the sentence for the decisions and logs in that table, and no more. All 18 rows have no init; all have row kind 1 except 61 and 67 (kind 0), so the think runs only with a target but for those two. Only rows 29 and 61 have an alt. [C]

if not melee: [C] the block's one call and no other in 260 decisions, the exit seed its `AIR` seed; [I] `<` against `≤`
if chance(1): ; draw#1
Walk(t, 7); return
else if chance(3): ; draw#1
SetModeAtUnit(chance(4) ? 4 : 5, t); return ; draw#2: A1 or A2
Idle(aip(2))
  • T2a. [C] One draw out of melee range (226 decisions), one (8) or two (26) in it.
  • T2b. [C] The map to its first callee, four outcomes in the first run; no call past the first in any decision resumed.
1. if U is null, or U.mode ∈ {0, 12}: return [I] (all of 1–5)
2. UNIT_CancelEvents(G, U, 2, 0) ; drop the pending AI event
room ← UnitGetRoom(U); if none: return
3. the flight scan:
for each room r of DUNGEON_GetNearRooms(room):
for i in 0 ..< 4: ; r+0x38, the ring of the last four dead monsters
m ← GameFindUnit(G, 1, r.deadGuid[i])
if m and m.mode = 0 and AiDistToPoint(m, U.x, U.y) < 15:
v14 ← 1; AiParamsPop(U); Ovr(0, 50, 0)
if StepAway(t, 12, 1):
if low(step) mod 20 < 1: UnitSetEvent(U, 17, 0) ; draw#1, only after a successful step
return
leave the scan ; the step was refused
4. if the mode read at step 1 ≠ 1: Idle(10); return ; only from NU
5. rec ← AiParamsHead(U)
if no rec:
if not melee and AiMonState3or19(U): Walk(t, 0); return
if dist < 15 and MonsterOwner(U) = U and Range(100) < aip(1): ; draw#1: a leader
blk.kind ← 1; MonHelpBlock(U, &blk); AiParamsPush(U, &blk)
SetModeAtUnit(9, t); return ; S2
if not melee:
if dist ≤ aip(2): Walk(t, 7); return
if Range(100) < 30: Ring(3); return ; next draw
Idle(10); return
if v14 ≠ 0 or Range(100) < aip(3): ; drawn only when v14 = 0
v14 ← 0
SetModeAtUnit(Range(100) < aip(4) ? 4 : 5, t); return
if Range(100) < 30: SetModeAtUnit(9, t); return
Idle(10); return
if rec.kind = 1:
if not melee:
if not Walk(t, 0): AiParamsPop(U)
return
if Range(100) ≥ aip(3): Idle(5); return
SetModeAtUnit(Range(100) < aip(4) ? 4 : 5, t); return
AiParamsPop(U); Idle(10)
  • T6a. [I] Draws. The scan’s mod-20 draw only after a successful step; then at most three Range(100) (aip1 or 30, aip3, aip4). UNIT_CancelEvents runs on every event before the scan, so a path that only returns queues nothing itself, and the next AI event comes from the mode’s end.
  • T6b. [C] The logged first call in every compared decision of the first run (1 393) is UNIT_CancelEvents at site with arguments 2 and 0. One outcome: the map’s mode 0 and 12 return was never taken.
  • T6c. [I] Past the first boundary this row is a consistency check only.
if not melee: [C] as row 2, 92 decisions; [I] the melee `Idle` (untaken), `<` against `≤`
if chance(1): Toward(t, 12, 0); return ; draw#1
else if chance(3): SetModeAtUnit(4, t); return ; draw#1
Idle(aip(2))
  • T9a. [C] The map; three outcomes in the first run; no call past the first in any decision resumed.
1. if melee and chance(3): SetModeAtUnit(4, t); return ; draw#1 [I] (all of 1–7)
2. srch ← {unit 0, count 0, dist 0, bound aip(4)·aip(4)}
3. if not MonTypeFlag(U, 8) or MonTypeFlag(U, 4): ; not a unique, or a champion
AiUnitSearch(G, U, &srch, 0, 9) ; own group's dead, AiDeadMinionCb
else:
AiUnitSearch(G, U, &srch, AiFallenCorpseCb, 1) ; a unique: near rooms
4. if chance(1): blk.kind ← 1; MonHelpBlock(U, &blk) ; post to the group, whether or not a corpse was found
5. if srch.unit and srch.count and chance(1) and MonSkillTargetOk(Skill1, srch.unit, 0, 0):
MonAiUseSeqSkill(Skill1, srch.unit, 0, 0); return ; Resurrect, mode 14
6. if t and dist < aip(5) and chance(2): UseSkill(Sk2mode, Skill2, t, 0, 0); return
h ← FindHostile(&dist, 0)
if h and dist < aip(5) and chance(2): UseSkill(Sk2mode, Skill2, h, 0, 0); return
7. if chance(3): T56(t, 3, 0) else Idle(10)
  • T13a. [C] Which dead, the tests, counts and last-taken stores emulated for both callbacks (240 cases each); AiDeadMinionCb computes AiDistUnits(searcher, candidate) before its null and type tests. [I] The bound word aip4² is the think’s. AiDeadMinionCb takes a monster in mode 12 with Unit+0xC4 bit 0x02, no state of the DataTables +0x150 set, owned by the searcher, at AI distance (AiDistUnits) ≤ aip4²: an AI distance compared with a square. AiFallenCorpseCb takes a monster whose MonBaseId is 19 or 58, alignment 0, Unit+0xC4 bit 0x02, no state of +0x150, in mode 12, no state of +0xD4, UnitDistSq ≤ aip4², and neither champion nor unique (MonTypeFlag 0x0C clear). Both count the candidates and keep the last one taken.
  • T13b. [I] Draw order: melee aip3; the aip1 call to the group; the aip1 resurrect draw (only with a corpse); up to two aip2 draws for the missile; one aip3 for the approach. FindHostile adds its callees’.
  • T13c. [C] The logged first call in all 363 compared decisions of the first run is the hand map’s: two outcomes (358 and 5): the attack when in melee and the draw passes, else MonTypeFlag(U, 8) at site. H1 holds for 103 decisions.
1. rec ← AiParamsHead(U) [C] H1's rule on the logged paths, 205 decisions, with told apart (T20c); [I] (all of 1–5) the untaken arms
2. if no rec and dist < 20 and MonsterOwner(U) = U and chance(5): ; draw#1: a leader
blk.kind ← 1; MonHelpBlock(U, &blk); AiParamsPush(U, &blk); rec ← &blk
3. if rec and rec.kind = 1:
if melee and Skill1 ≥ 0: AiParamsPop(U); UseSkill(Sk1mode, Skill1, t, 0, 0); return ; Jab
Ovr(2, 100, 0)
if not Walk(t, 0): AiParamsPop(U)
return
4. if not melee:
if v14 ≠ 0:
Ovr(2, 0, 4); Walk(t, 7)
if D100() > 10: v14 ← 0 ; a draw after the walk
return
T56(t, 0, 0); v14 ← 1; return
5. if not chance(1): Idle(aip(3)); return ; draw#1
if Skill1 ≥ 0 and chance(4): UseSkill(Sk1mode, Skill1, t, 0, 0); return ; draw#2
SetModeAtUnit(chance(2) ? 4 : 5, t) ; draw#2 or #3
  • T20a. [I] A head record of another kind is left in place and the plain decision runs.
  • T20b. [C] The map, MonsterOwner’s own GameFindUnit call included as a boundary; five outcomes in the first run.
1. if AiMonState3or19(U) and not melee: Walk(t, 7); return [I] (all of 1–3)
2. if dist ≤ aip(1):
if melee:
if chance(3): SetModeAtUnit(chance(4) ? 4 : 5, t); return ; draw#1, #2
Idle(aip(5)) ; no return: the walk follows in the same event
Walk(t, 7); return
3. if chance(2): Ring(3); return
Idle(aip(5))
  • T21a. [C] aip1 is a range, not a chance: 15 decisions at dist = aip1 take the in-range walk and 13 at aip1 + 1 the other arm; aip3 is 100 in the 18 cells of the six Mummy records and all 29 melee draws pass. [I] A failed aip3 draw runs Idle(aip5) and then Walk(t, 7) in the same event.
  • T21b. [C] The map up to the first callee (the Idle; the walk after it is a reading); five outcomes in the first run. H1 holds for 205 decisions, one call past the first in each.
1. if melee and chance(1): SetModeAtUnit(4, t); return ; draw#1 [I] (all of 1–8)
2. if dist < 5 and chance(1): SetModeAtUnit(5, t); return ; draw#1 or #2
3. s ← {corpse 0, wounded 0, taken 0, inRange 0, bound aip(5)², undead 0, uniquesToo 0}
4. if U.class = (MonStatsCount > 229 ? 229 : −1): ; Radament
A2Q1_BossSeen(G, U); s.bound ← (aip(5) + 10)²; s.undead ← 1
if d = 0: s.uniquesToo ← 1
5. AiUnitSearch(G, U, &s, AiGreaterMummyCb, 1)
6. if Skill2 ≥ 0 and s.wounded and chance(3):
UnitSetPathTargetUnit(U, s.wounded); UseSkill(Sk2mode, Skill2, s.wounded, 0, 0); return ; Bestow
if s.corpse and chance(2) and MonSkillTargetOk(Skill1, s.corpse, 0, 0):
UnitSetPathTargetUnit(U, s.corpse); MonAiUseSeqSkill(Skill1, s.corpse, 0, 0); return ; Resurrect2, mode 14
7. if Skill3 ≥ 0 and chance(4) and (h ← FindHostile(&dist, 0)):
UseSkill(Sk3mode, Skill3, h, 0, 0); return ; UnHolyBolt
8. if s.inRange < 1: Ovr(0, 50, 0); WalkStop(t, 3); return
if D100() > 49: Idle(6) else T56(t, 3, 0)
  • T22a. [C] AiGreaterMummyCb, emulated, its tests, counts and stores (240 cases). A monster other than the searcher, on the same side (AlignSame02), alignment not 2, Unit+0xC4 bit 0x02, not in state set 33; then with undead 0 its MonStats lUndead (+0x0D bit 3), with undead 1 MONSTATS_IsUndead (+0x0D bit 3 or bit 4, lUndead or hUndead); then unless uniquesToo, not a unique (MonTypeFlag(unit, 8)); then UnitDistSq ≤ bound. Each such unit counts in inRange; one in mode 12 with no state of the +0xD4 mask becomes corpse, one in another mode than 0 whose stat 6 differs from UnitGetMaxHp becomes wounded; each taken one counts in taken. The last one met of each kind is kept.

  • T22b. [I] Draw order: the two melee/near draws on aip1; then aip3 only with Skill2 ≥ 0 and a wounded unit, aip2 only with a corpse, aip4 only with Skill3 ≥ 0, and the 50/50 only with a unit in range.

  • T22c. [C] The map up to A2Q1_BossSeen or the search, the block’s seven words included in the non-Radament case; three outcomes in the first run. H1 holds for 62 decisions.

  • T22d. [I] Whether a champion corpse passes the skill’s own test is unlogged: see Open questions.

bits ← aip(5); b0, b1, b2 ← its bits 0, 1, 2 ; Pair, Skill2 (Firewall), Skill3 (Meteor) [I] (all of this block)
v14: 1 engaged, 2 fleeing; v18: the largest distance below 30 seen under s; v1C: the skill cooldown
Pair(x): if Range(100) < 50: UseSkill(Sk1mode, Skill1, x, 0, 0) else UseSkill(Sk4mode, Skill4, x, 0, 0)
Skills(x): if b1 and v1C ≤ 0 and Range(100) < aip(4): UseSkill(Sk2mode, Skill2, x, 0, 0); v1C ← 11; true
if b2 and v1C ≤ 0 and Range(100) < aip(4): UseSkill(Sk3mode, Skill3, x, 0, 0); v1C ← 11; true
false
1. if v1C > 0: v1C ← v1C − 1
h ← FindHostile(&hd, 0)
s ← AiMonState3or19(U)
2. if s:
if v14 = 0: v14 ← 1
if dist < 30 and dist > v18: v18 ← dist
if melee:
x ← D100() ; always drawn, before the bit test
if x ≤ 30 and b0: Pair(t) else SetModeAtUnit(4, t)
return
3. life ← UnitLifePct(U)
4. if v14 = 2: ; fleeing
if life ≥ 75: v14 ← 1; Walk(t, 7); return
if dist < 14 or dist ≤ v18:
Ovr(0, p, 0) ; T28a
if StepAway(t, 8, 1): return
if dist ≥ aip(3) or Range(100) ≥ aip(2): Idle(15); return
if Skills(t): return
if b0 and h and hd ≤ 20: Pair(h); return
T56(t, 4, 0); return
5. if life < 33:
v14 ← 2
if StepAway(t, 8, 0): return ; refused: on to step 6 or 7
6. if melee:
v14 ← 1
if D100() < aip(1):
if not (b0 and h): SetModeAtUnit(4, t); return
if Range(100) > 30: SetModeAtUnit(4, t); return
if hd ≤ 20: Pair(h); return
if Range(100) < 33: T56(t, 4, 0) else Idle(10)
return
7. if dist ≥ aip(3):
if v14 = 1: Walk(t, 7) else Idle(15)
return
v14 ← 1
if Range(100) ≥ aip(2): ; keep a distance
if dist > 20: Walk(t, 7); return
if dist < 9 and Range(100) < 50: StepAway(t, 8, 0); return
if Range(100) < 50: T56(t, 4, 0) else Idle(10) ; inside 9 this is the second 50
return
if Skills(t): return
if not (b0 and h and hd ≤ 20): Walk(t, 7); return
if Range(100) < 75: Pair(h) else T56(t, 4, 0)
  • T28a. [I] The flee step’s override. p is computed only when Velocity > 0, a signed 16-bit test, and is 0 otherwise. With the guard met: q: i32 ← (i16 Run · 100) ÷ (i16 Velocity), truncating, then q − 100, a result ≤ 0 becoming 0 and one ≥ 120 becoming 120, passed to MonAiPathOverride(unit, 0, p, 0). This is the one such computation in the think; the engaged flee’s step takes no override, and Run · 100 cannot wrap.

  • T28b. [C] The logged first call in all 279 compared decisions of the first run is the hand map’s: MonAiFindHostile at site, one outcome. H1 holds for 56 decisions, 168 calls past the first.

  • T28c. [C] The block run as written against every row 28 decision of the rev 4 and rev 6 logs: 100 decisions, 99 compared, 0 mismatches, 1 stopped at a return site not armed. Each boundary call’s function and return address, the unit seed at it, v14, v18, v1C, T56’s three arguments and the exit seed agree, with the logged inputs and results as oracles. Logged paths only: steps 1, 3, 5 with its step accepted, 6 and 7, Pair both ways, and T56 at two of its three sites. Reached by no log, so [I] only: step 2’s melee arm, all of step 4, Skills’ two uses. Controls: T56’s byte as 3 fails 23, the draw of step 6’s Range(100) > 30 made before its two tests fails 1, the second 50 inside 9 dropped fails 2; step 2’s draw made only with b0 fails none, as that arm is not reached.

T29. Row 29 BatDemon (AiBatDemon, alt AiBatDemonAlt)

Section titled “T29. Row 29 BatDemon (AiBatDemon, alt AiBatDemonAlt)”
life ← UnitLifePct(U) [I] (all of this block)
switch v14:
0, 4: SetModeAtUnit(10, 0); Delay(8); v14 ← 1; v18 ← 0
r ← stat 74 of U
if r ≠ 0: v1C ← aip(5)·r ÷ 8; UnitSetStat(U, 74, r + v1C, 0) else v1C ← 0
return
1: if v18 > 1 and not (not melee and dist > 6 and not AiMonState3or19(U) and (dist > 13 or life < 51)):
if v1C ≠ 0: UnitSetStat(U, 74, stat 74 of U − v1C, 0)
SetModeAtUnit(9, 0); Delay(6); v14 ← 3; return ; leave the perch
SetModeAtUnit(11, 0); Delay(v18 = 0 ? 10 : 15); v18 ← v18 + 1; return
2: if life < aip(1) and StepAway(t, 15, 0): v14 ← 4; return
if Range(100) < 33: Walk(t, 0); v14 ← 3; return
if Range(100) < 15: Ring(6); return
Idle(10); return
3: if life < aip(1) and StepAway(t, 15, 0): v14 ← 4; return
if not melee: Walk(t, 0); return
if v18 > 0: SetModeAtUnit(5, t); v18 ← 0; return
if AiMonState3or19(U) and Range(100) < aip(2) and StepAway(t, 12, 0): v14 ← 2; return
if Range(100) < aip(3): SetModeAtUnit(Range(100) < aip(4) ? 5 : 4, t); return
Idle(10); return
other: if dist < 15: Walk(t, 0); v14 ← 3; return
v14 ← 4; Idle(15)
alt:
a unit in mode 11: v14 ← 1; Delay(1)
else v14 = 4: v14 ← 1; SetModeAtUnit(10, 0)
else: MonAiInit(ctl, ctl.state); Idle(1)
  • T29a. [I] The aip5 · r ÷ 8 is signed and truncating, (x + (x >> 31 & 7)) >> 3. The first attack after a perch is A2 (v18 > 0). The alt’s second arm tests and writes v14 (AiCtl+0x14).
  • T29b. [C] The alt’s map (no draw) by emulation; the think’s logged first call, UnitLifePct at site, in all 110 compared think decisions of the first run, by hand map, one outcome. H1 holds for 215 decisions, 526 calls past the first.
1. rec ← AiParamsHead(U) [I] (all of 1–4)
if rec:
if rec.kind ∈ {1, 14} and (u ← GameFindUnit(G, rec+0x10, rec+0x0C)):
v14 ← 0; v18 ← 0; Ovr(13, 50, 0); Walk(u, 0); AiParamsPop(U); return
AiParamsPop(U)
2. tl ← UnitLifePct(t) ; the target's life
3. switch v14:
0: if melee:
v18 ← 0; v14 ← 1
if chance(1): SetModeAtUnit(4, t); return ; draw#1
Idle(aip(2)); return
1: v18 ← v18 + 1
if v18 > aip(3) and tl > aip(4): ; hit and run
v14 ← 2; v18 ← 0; Ovr(2, 50, 0); StepAway(t, 14, 1); return
if melee:
if Range(100) < aip(1): SetModeAtUnit(4, t); return ; draw#1
Idle(aip(2)); return
2: if dist > 12:
v18 ← v18 + 1
if v18 > 1: v14 ← 0; v18 ← 0
if D100() < 20: T56(t, 4, 0); return ; draw#1
Idle(10); return
Ovr(2, 50, 0)
if StepAway(t, 14, 1): return
v14 ← 0; v18 ← 0; Idle(10); return
other: Idle(10); return
4. Ovr(13, 50, 0); Walk(t, 7)
  • T30a. [I] The kind-14 record row 30 consumes is written by row 65’s think: it builds a block with +0x08 14, +0x0C the unit’s [+0xC] and +0x10 1 and passes it to MonHelpBlock.
  • T30b. [C] The record map up to GameFindUnit, AiParamsPop or UnitLifePct, with no draw before them; one outcome in the first run. H1 holds for 316 decisions, 243 on one path.
if melee: [I] (all of this block)
if Skill1 ≥ 0 and D100() < aip(1): ; draw#1
UseSkill(Sk1mode, Skill1, t, 0, 0); return
SetModeAtUnit(4, t); return
if D100() < aip(2): Idle(5); return ; draw#1
if D100() < aip(3): ; draw#2
if Skill1 ≥ 0 and Range(100) < aip(4): ; draw#3
UseSkill(Sk1mode, Skill1, t, 0, 0); return
if Skill2 ≥ 0: UseSkill(Sk2mode, Skill2, t, 0, 0); return
Ovr(1, 0, 0)
Walk(t, 7)
  • T34a. [C] Ovr(1, …) plans type 7 (2 calls; D2), and UseSkill comes after three draws on the unit’s seed, so RNG_Range takes the seed U + 0x20 set at entry. [I] The draws compare signed (jge); the skill tests are sign tests of the i16.
  • T34b. [C] The map by emulation; five outcomes in the first run. H1 holds for 51 decisions, two calls past the first in all.
1. r ← dist; h ← FindHostile(&r, 0) [I] (all of 1–7)
2. if no h:
if D100() > 49: Idle(aip(3)); return ; draw#1, unsigned
T56(t, 3, 0); return
3. if not melee and AiMonState3or19(U): SetModeAtUnit(4, h); return
4. if r < 6 and chance(4): ; draw#1
Ovr(0, 100, 0)
if StepAwayXY(h, 12, 1): return
5. if 0 < aip(8) and aip(8) < r and Range(100) < aip(1):
Ovr(0, 10, 0); WalkStop(h, (u8) aip(8)); return
if aip(5) < r:
Ovr(0, 100, 0); RunStop(h, (u8) aip(5)); return
6. if not chance(2): Idle(aip(3)); return
7. if Skill2 ≥ 0 and Range(100) < aip(6): UseSkill(Sk2mode, Skill2, h, 0, 0); return
if Skill3 ≥ 0 and Range(100) < aip(7): UseSkill(Sk3mode, Skill3, h, 0, 0); return
if Skill1 < 0: SetModeAtUnit(4, h); return
UseSkill(Sk1mode, Skill1, h, 0, 0)
  • T35a. [I] After FindHostile every distance test reads r, the hostile’s distance (0x7FFFFFFF without one), not the event’s, and every command after it is aimed at h; the no-hostile wander is at t. Up to five draws.

  • T35b. [C] The logged first call in all 95 compared decisions of the first run is the hand map’s: MonAiFindHostile at site, one outcome. H1 holds for 111 decisions, 226 calls past the first.

r ← dist [I] (all of this block)
if AiMonState3or19(U) and (h ← FindHostile(&r, 0)): SetModeAtUnit(4, h); return
h ← FindHostile(&r, 0)
if no h or r > 19:
if chance(3): Toward(t, aip(4), aip(5)); return ; draw#1
Idle(20); return
if chance(1): SetModeAtUnit(4, h); return ; draw#1
if D100() < 20: T56(t, 3, 0); return ; draw#2
Idle(aip(2))
  • T37a. [C] No skill column is read by the think or its callees to depth 2: none of their 1 447 instructions has a displacement 0x170–0x19F. [I] Deeper callees and MonSetMode’s one indirect call are not scanned.
  • T37b. [C] The state test up to the first or second FindHostile, the pointer argument included, no draw before it; one outcome in the first run. H1 holds for 81 of the 86 resumed decisions; the other 5 stay a consistency check.
1. cls ← U ? U.class : −1; O ← MonsterOwner(U) [I] (all of 1–8)
2. if no O or O.type ≠ 0: ; only a player owner keeps row 61
MonAiInit(ctl, cls = 271 ? 5 : 6); Idle(10); return
3. if UNIT_HasState(U, 12): STATE_Set(U, 12, 0) ; inferno
if U.mode ∈ {2, 15}: return ; no command
AiParamsHead(U) ; result not read
4. v ← v14
if 16 < v < 20 (signed): far ← 2v; near ← v; n ← v >> 1
else: far ← 24; near ← 16; n ← 5
e: u32 ← AiDistUnits(U, O)
5. if e > 100: Follow(O, U, 3, 0, 0, 0); return ; unsigned compares
if e > far: Follow(O, U, 1, 1, 60, 0); return
if e > near:
if O.mode ∈ {2, 6}: Follow(O, U, 0, 0, 0, 0); return
if O.mode = 3: Follow(O, U, 0, 1, 60, 0); return
6. if U.mode ≠ 1: UNIT_CancelEvents(G, U, 2, 0); Idle(5); return
7. ranged ← not (cls = 338 or 559 < cls ≤ 561)
blocked ← COLL_PatternTestBool(room, subX, subY, UnitPathGet48(U), 0x40)
if blocked and Pow2(128) < (ranged ? 12 : 6): Ring(5); return ; draw#1, signed
if not InTown(U) and (h ← FindHostile(&e2, &m)) and e2 < 25: ; unsigned
HireFight(cls, O, h, &U.seed, ctx); return
if blocked: Ring(5); return
8. if Region(O) ≠ Region(U): Follow(O, U, 0, 1, 60, 0); return
if AiDistUnits(U, O) ≤ 1:
n ← n − 1 ; byte
x, y ← the owner's path point
Ovr(7, 0, 0); if WanderNear(O, n): return
UNIT_CancelEvents(G, U, 2, 0); if StepAway(O, n, 1): return
Ovr(0, 0, 40); WalkToPoint(x, y); return
if Range(100) < 5: WanderNear(O, near); return ; draw#2
Idle(5)
HireFight(cls, O, t, seed, ctx): [I] (all of this block)
c ← 98 for cls 338, 560, 561 (the stat is not read)
else min(95, v14 + 40 + 2·stat 12 of U)
e ← AiDistUnits(U, t)
info ← PETS_GetInfo(O, U ? U.guid : −1); if no info: Idle(10); return
hit ← (low(step(seed)) mod 100) < c ; draw#1, signed
v14 ← hit ? 0 : v14 + 10
if aip(1) = 0: ; ranged
if e < 4 and (low(step(seed)) mod 100) < 50: ; draw#2
if WanderNear(O, 4): return
UNIT_CancelEvents(G, U, 2, 0); if StepAway(t, 4, 1): return
MERCAI_PickSkill(cls, info.w08, t, seed); return ; even after a miss
if hit: MERCAI_PickSkill(cls, info.w08, t, seed) else Idle(10)
return
if e < 3 and InMeleeRange(U, t, 0):
if hit: MERCAI_PickSkill(cls, info.w08, t, seed) else Idle(10)
return
PathSetStopDist(path, MonStats2.MeleeRng); MonAiRunToUnitFlags(t, 1)
MERCAI_PickSkill(cls, id, t, seed): [I] (all of this block)
if cls < 0 or cls ≥ MonStats count, or no record: return
L ← stat 12 of U
h ← HIRELING_GetRecord(G+0x70, id, L); if no h: return
dl ← max(0, L − h.Level); total ← h.DefaultChance
for k in 0 ..< 6:
s ← h.Skill[k]; if s < 1 or s ≥ Skills count: leave the loop
cum[k] ← 0
e ← SkillFindByOwner(U, s, −1)
if no e, or SKILLS_GetSkillLevel(U, e, 1) ≤ 0: continue
if Skills[s].aitype = 1 and aurastate in [0, States count) and UNIT_HasState(U, aurastate): continue
if s = 41 and t and UnitEdgeDistance(U, t) > SKILLS_GetSkillLevel(U, e, 1) ÷ 2 + 4: continue
total ← total + h.Chance[k] + (h.ChancePerLvl[k]·dl) ÷ 4 ; signed
cum[k] ← total
r ← 0 when total + 1 < 1
else step(seed) & total when (total + 1) & total = 0
else step(seed) mod (total + 1) ; draw#1, unsigned div
if r ≥ h.DefaultChance:
k ← the first in 0 ..< 6 with cum[k] ≥ r ; signed
if k < 6 and (s ← h.Skill[k]) > 0:
if Skills[s] has aura bit: PlayerSetSkill(U, 0, s, −1); Idle(10); return
UseSkill(h.Mode[k], s, t, 0, 0); return
if cls = 271: UseSkill(Sk1mode, Skill1, t, 0, 0); return
if cls ∈ {338, 359, 560, 561} and InMeleeRange(U, t, 0): SetModeAtUnit(4, t); return
Idle(10)
  • T61a. [I] Follow(O, U, kind, run, speed, n) = AiHireFollow. Kind 0: from a start direction (UnitDirToPoint on the owner, reduced by Dir64To8) it tries up to eight directions in turn, each a point 8 subtiles from the owner’s point in the owner’s logic region, with Ovr(0, speed, 40) and MonAiWalkToPointOrStop; none works: UNIT_CancelEvents(2, 0) and MonAiWanderRingDirect(owner, 4). Kind 1 with the owner in mode 2: Ovr(0, 0, 100) and a walk to the owner’s point; in any other mode it walks back along the owner’s 20-entry trail (player data +0xA0 the index, +0xA8 + 8k the points, newest first), skipping points within 5, with Ovr(0, speed, 100) and a walk, then Ovr(15, speed, 100) and a walk (a run with the run flag), then once per call AiPathOverrideSet(speed, 100) and the same move, and after 20 entries MonAiWanderRingDirect(owner, max(4, Dist/4)); a speed of 0 is replaced by 50 + Range(15). Kind 3: a spawn position near the owner (SpawnPosPick with class 363 when the MonStats count exceeds 363, else −1), UnitTeleport there, Unit+0xC8 |= 0x10000, Idle(5); it returns 0 on a failed pick, room or teleport. Row 61 reaches only kinds 3, 1, 0, 0, 0.

  • T61b. [I] The alt and states 5, 6. alt: if UNIT_HasState(U, 12): STATE_Set(U, 12, 0), then MonAiInit(ctl, ctl.state), Idle(1). State 5, also row 60’s think: not in mode 1 Idle(5); out of town with a hostile within 20 (signed): on Range(100) < 30 UseSkill(Sk1mode, Skill1, h, 0, 0) for class 271, else SetModeAtUnit(4, h); then on a second Range(100) < 30 T56(h, 4, 0); else Idle(10). Otherwise D100() < 20 Ring(5), else Idle(10). State 6: the same guard; out of town with FindTarget’s h, one r ← Range(100): out of melee r < 30 Walk(h, 7) else Idle(10), in melee r < 80 SetModeAtUnit(4, h) else Idle(10). Otherwise as state 5.

  • T61c. [I] From the code’s own writes v14 is a multiple of 10, so step 4’s 17–19 arm needs another writer. The record is looked up with G+0x70 and id the pet entry’s +8. When no weight was added and r = DefaultChance, the search reads stack slots past the break, which hold earlier contents, or reaches slot 6 and falls back; which skill a stale slot selects is not read.

  • T61d. [C] The first call MonsterOwner at site, in 240 emulated cases and in all 5 697 decisions of the first run, one outcome; the seven emulated families of the row (hire, hirealt, st5, st6, pick, fight, follow) each to their first callee or return. Everything past it is T61e.

  • T61e. [I] Past the first boundary a consistency check.

e ← dist; h ← FindHostile(&e, 0) ; e is the frame word it may rewrite [I] (all of this block)
if h:
if e > aip(2) and chance(3): ; draw#1
Ovr(0, 10, 0); WalkStop(h, (u8) aip(2)); return
if e ≤ aip(4) and chance(5): ; draw#1 or #2
Ovr(0, 25, 0)
if StepAway(h, 5, 1): return
SetModeAtUnit(4, t); return ; at t, not h
if e < aip(6) and chance(1): SetModeAtUnit(4, h); return ; up to #3
if e > aip(2) and chance(3):
Ovr(0, 10, 0); WalkStop(t, (u8) aip(2)); return
if chance(7): T56(t, 4, 0); return
Idle(aip(8))
  • T64a. [I] The walk-stop distance is the low byte of aip2; the compares use the full i16. At most five draws: three on the hostile branch, two after it.
UNIT_CancelEvents(G, U, 2, 0) [I]
if v14 ≠ 0: Ranged(ctx) else Melee(ctx)
  • T67b. [I] AiPetMove, returning 1 when it commanded: kind 0 walks to a point eight subtiles out in the owner’s direction class (an eight-direction search of free points in the owner’s region); kind 1 runs toward the owner’s path target with speed, or 40 + Range(40) when speed is 0; kind 2 wanders: Range(100) < 10 gives WanderRingDirect(ref, 3 + Range(3)); kind 3 teleports the pet next to the owner (SpawnPosPick for class 363 or 0, UnitTeleport, +0xC8 |= 0x10000, Idle(5)); kind 4 steps away when another pet of the owner stands within 2 (AiPetCrowdCb over the owner’s pet list); kind 5 Ovr(7, 0, 0) and WanderNearUnit(ref, dist). Its three Range calls are on the pet’s seed.

  • T67c. [I] Which code sets v14 for a necromancer’s pet is not read.

  • T67e. [I] Past the first boundary a consistency check.

n ← 1 [I] (all of this block)
if Skill1 ≥ 0 and (s ← SkillFindByOwner(U, Skill1, −1)) and (lv ← SKILLS_GetSkillLevel(U, s, 1)) > 0: n ← lv
h ← UNIT_HasState(U, 12) ; inferno
if Skill1 ≥ 0 and not melee and dist < n and not h:
if T > v14 and Range(100) < aip(1): ; draw#1
v14 ← T + aip(6); UseSkill(Sk1mode, Skill1, t, 0, 0); return
go to far
if h: STATE_Set(U, 12, 0)
if melee:
if Skill1 ≥ 0 and T > v14 and chance(2):
v14 ← T + aip(6); UseSkill(Sk1mode, Skill1, t, 0, 0); return
if chance(3): SetModeAtUnit(4, t); return
if chance(5): T56(t, 3, 0) else Idle(5)
return
far:
if chance(4): Walk(t, 7) else Idle(10)
  • T89a. [I] The skill’s level is used as a distance, and aip6 is a cooldown in frames. State 12 is cleared whenever the breath is not chosen at range. At most three draws.
  • T89b. [C] The skill-sign test up to SkillFindByOwner or, for Skill1 < 0, UNIT_HasState(U, 12), nothing after it; one outcome in the first run. H1 holds for 77 decisions.
if melee: [I]
if chance(1): ; draw#1
if chance(3): UseSkill(5, Skill1, t, 0, 0) ; draw#2: the constant mode 5, no sign test on Skill1
else SetModeAtUnit(4, t)
return
Idle(aip(4)); return
if chance(2): WalkZ(t) else Idle(aip(4)) ; draw#1
  • T125a. [C] The full map by emulation: UseSkill(5, Skill1, t, 0, 0), SetModeAtUnit(4, t), Idle(aip4) or WalkZ(t). Live: five outcomes in the first run.

An entry ends in one of § B’s commands, a helper of § C, or a return with nothing queued, as its block shows. [C] Of 6 518 rev 6 decisions the last call is a § B command or move helper in 2 957, a § C helper in 2 891 and another call in 670. [I] That nothing is queued after those 670

chance(1) with aip1 = 50 on a step whose new low word is 1 234 567: 1 234 567 mod 100 = 67, and 67 < 50 is false, so the branch is not taken and the seed has stepped once. [C] (22 emulated cases on that low word and 4 row-125 cases going to Idle(aip4) one step on; RNG_Step)

  • [I] A draw in a failed and. chance(K) short-circuits as written: row 13’s second chance(1) is drawn only with a corpse, row 22’s aip3 only with Skill2 ≥ 0 and a wounded unit. Keep the order of the tests.
  • [I] Row 21’s Idle does not return: the walk runs in the same event.
  • [C] The callbacks compare signed: AiDeadMinionCb an AI distance, AiFallenCorpseCb and AiGreaterMummyCb UnitDistSq, against the block bound. [I] Row 13’s kind-9 bound is a square, compared with an AI distance; row 22’s bounds are 32-bit signed imul squares of an i16.
  • [C] Row 125 uses the constant mode 5 and no sign test on Skill1: Skill1 is sign-extended (movsx) into MonAiUseSkill’s skill argument, and 46 cases with a negative i16 reach MonAiUseSkill(5, Skill1, t, 0, 0).
  • [C] A leader’s stack block reaches the minions word for word (E5). [I] It carries four words the code does not write, and row 30 reads +0x0C/+0x10 of a kind-1 head record for its GameFindUnit; these inputs do not give those words.
  • [C] Row 34’s override type 1 plans type 7 (D2), so Ovr(1, …) is no type-1 request: 39 of 39 logged type-1 overrides plan type 7.
  • The tier’s entries: §§ 2.1, 2.5, 2.8, 2.11, 4; §§ 2.4–2.6, 2.12–2.14; §§ 2.0, 2.2, 2.4; § 2.1; §§ 2.2, 2.3; § 2; § 2.1; § 2.5; § 2.13.
  • MonStats, Hireling, MonAI.
  • path/overview, path/search, skills/dispatch § M.