Monster AI: target search, the command, the path request, following, the tier AIs
Purpose
Section titled “Purpose”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.
Entry points
Section titled “Entry points”| 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 |
Inputs
Section titled “Inputs”Tables
Section titled “Tables”| 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 |
Runtime state
Section titled “Runtime state”| 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 |
Constants
Section titled “Constants”| 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 |
Notation
Section titled “Notation”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.
Algorithm
Section titled “Algorithm”A. Target search
Section titled “A. Target search”- A1. [C] The lists. Lists 0–7 are one per player slot:
TargetListAddPlayer, fromSUnitCreatefor a player when fewer than 9 clients, takes the first free slot, andTargetListAttach(unit, i)links a unit after playeri’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 fromWandererSpawnand 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
AiDistToPointfrom the unit’s own point (A7). So, forced targets aside, a target is never taken at or beyondaidist(35 when the cell is 0), a decoy has no such bound, and once a monster has had a target flag0x08turns the line test off until something clears it.MonAiMove’s flag 1, on a refused move, sets0x40(B6), so the next search tests the line again. - A3. [C] The forced target (
MonsterData+0x38kind,+0x34guid; 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 toUnitLineBlocked(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 drawsRNG_RangePow2(unit seed, 2), callsMonAlignSetwith the flips below, then the kind-6 search (flag non-zero) or the kind-5 search (flag 0). The out distance isAiDistUnits(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:MonAiFindHostilenon-zero,MonAiFindTarget0); what the searches return and the alignment restore. Kind 3 (confusion) drawsRNG_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]
MonAiFindHostileis the forced target, else search kind 6 andMonAiPreferDecoybetween 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 readctx’s, some the hostile’s (0x7FFFFFFFwithout one). - A5. [C] The unit search.
AiUnitSearch(game, unit, out, param, kind)takesg_AiSearchKinds[kind] = {walker, callback}(the callback fromparamwhen 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, nextUnit+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 withUnit+0xC4bit0x04set, 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 drawsRNG_Range(candidate seed, 100)and, below 80, callsInMeleeRange(searcher, cand, 0); a monster with state 146 reaches that call with no draw. The rest returnUnitsHostile. (400 cases) [I] The refusals afterInMeleeRange: 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/+0x10for 2, 4, 5, and 0 without a path.AiDistUnits(a, b)first subtracts the signedUNIT_GetSizeX(a)(for a monster the MonStats2+0x08signed byte) from both components, floor 0;AiDistToPointSizedsubtracts(size >> 1) + 1with an unsigned shift, so a negative size is not halved as a signed value. Emulated foraof types 0, 1, 2, 4 and 5 (240 cases each). [I] Anaof 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) / 2fails all 240. (dist) - A8. [C]
UnitDistSq(a, b)isdx² + dy²mod 2³² (imul,add; 240 cases with 84 sums past 2³¹ − 1), compared signed byAiFallenCorpseCbandAiGreaterMummyCb. [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 0x59ormov r32, 0x59comes 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, readsTgtPlaceCheck, Skills+0x008bit 0; see row 13 and row 22 below for the tier’s.
B. The command
Section titled “B. The command”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 100B2. 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: MonModeStartNeutralB3. 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 0B4. 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 0B5. 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-removalB6. 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 formsMonAiWalkToPoint,MonAiRunToPoint(WL under state 60),MonAiWalkToPointOrStop(cancels the AI event on refusal).MonAiRunToUnitStop(t, stop)(RunStop) isMonAiMove(t, 15, 0, 0, stop, 0); it andWalkStopreplace only the stop word’s low byte by 1 when that byte is 0, the high bytes passing unchanged. The ten walkers’ maps to theirMonAiMoveorMonSetModecall 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 flag0x40and takes B6’s draw and fallback,Walk(t, 0)returns 0 with nothing queued, andWalkToPointOrStopcancels. - B7a. [C] Helpers that command themselves. Besides the five commands of § B, nine AI helpers call
MonSetModedirectly 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.MonAiMoveand after are not covered. (ring) - B8b. [C]
MonAiWanderRingDirect,MonAiWanderNearUnitandMonAiRunNearUnitdo alike, to theirMonSetModecall (120 cases each):WanderRingDirectcentres on the unit itself and callsMonSetMode(G, cmd, 1)in WL, notMonAiMove;WanderNearUnitwith a null other isRing(2);RunNearUnittakes RN, or WL with the override words cleared under state 60, tested after the draws. All three take the radius asr & 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 drawB10. 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 bodyB11. 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 andT56’s maps up to their first boundary (MonAiPathOverrideat sites, orMonAiMove) match the emulation: 200fleecases (8 with a null other return 0, 100 reach the override, 92 the move) and 120typedrawcases; the controls fail all separating cases (69 and 120).
C. Idle and delay
Section titled “C. Idle and delay”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
D. The path request
Section titled “D. The path request”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 inMonPathCtl(+0x18type,+0x1Cstep parameter,+0x20byte); 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 77D3. 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 −1D4. 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
PathPlanruns only afterPathTargetNotTown.
E. Following
Section titled “E. Following”- E1. [C]
MonAiKeepNear, emulated to its boundaries insideMonAiPreChecks(200 cases). WithAiCtl+0x0C= −1 it returns 0. It clearsAiCtl+0x0Cto −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 distanced ≤ 1from a player (type 0) it callsMonAiStepAway(unit, 19, 1), while a monster atd ≤ 1returns 0; past 20 it setsMonAiPathOverride(U, 7, 0, 40)and callsMonAiWanderNearUnit(unit, 19); between, it returns 0. [I] After each call it stops the event; on a refused step it goes straight toMonAiWanderNearUnit(unit, 19)and stops. At 21 or more it setsMonAiPathOverride(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.
AiCtlFollowSetwrites it fromUniqueMinions(minion → its boss),SkillReviveDo,SummonFinishandSkillDo71Taunt, its 4 calls (three tools):+0x0Cthe other’s+0x0C,+0x10its type (or −1, 1); inUniqueMinionsthe unit just made gets its input unit. - E3. [C] The owner and the group.
MonSetOwnerwritesAiCtl+0x28,+0x2C,+0x30,MonsterOwner(U)reads the three intoGameFindUnit, andMonGroupForEachreads 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,+0x20the head.AiParamsAlloclinks a zeroed record before the head (the first record becomes the head);AiParamsPush(unit, blk)copiesblk+0x08..+0x18into a new record and makes it the head;AiParamsHeadreturns the head;AiParamsPopunlinks 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 withmakeHeadmakes it the head.AiParamsHeadandAiParamsFindemulated, 120 cases each. [I]AiParamsAlloc,PushandPoppast their pool calls. - E5. [C] A leader’s call.
MonHelpBlockhas 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 laterAIDhead record of another unit. Row 65 passes a kind-14 block (T30a). [I]MonHelpBlock(unit, blk)pushes a copy ofblkto each minion of the unit’s owner, theAiCtl+0x34list. 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.
F. Draws and seeds of the helpers
Section titled “F. Draws and seeds of the helpers”| 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) |
T. The tier AIs
Section titled “T. The tier AIs”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]
T2. Row 2 Skeleton
Section titled “T2. Row 2 Skeleton”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); returnelse if chance(3): ; draw#1 SetModeAtUnit(chance(4) ? 4 : 5, t); return ; draw#2: A1 or A2Idle(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.
T6. Row 6 Fallen
Section titled “T6. Row 6 Fallen”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: return3. 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 refused4. if the mode read at step 1 ≠ 1: Idle(10); return ; only from NU5. 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)(aip1or 30,aip3,aip4).UNIT_CancelEventsruns 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_CancelEventsat 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.
T9. Row 9 Wraith
Section titled “T9. Row 9 Wraith”if not melee: [C] as row 2, 92 decisions; [I] the melee `Idle` (untaken), `<` against `≤` if chance(1): Toward(t, 12, 0); return ; draw#1else if chance(3): SetModeAtUnit(4, t); return ; draw#1Idle(aip(2))- T9a. [C] The map; three outcomes in the first run; no call past the first in any decision resumed.
T13. Row 13 FallenShaman
Section titled “T13. Row 13 FallenShaman”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 rooms4. if chance(1): blk.kind ← 1; MonHelpBlock(U, &blk) ; post to the group, whether or not a corpse was found5. 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 146. 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); return7. 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);
AiDeadMinionCbcomputesAiDistUnits(searcher, candidate)before its null and type tests. [I] The bound wordaip4²is the think’s.AiDeadMinionCbtakes a monster in mode 12 withUnit+0xC4bit0x02, no state of the DataTables+0x150set, owned by the searcher, at AI distance (AiDistUnits) ≤aip4²: an AI distance compared with a square.AiFallenCorpseCbtakes a monster whoseMonBaseIdis 19 or 58, alignment 0,Unit+0xC4bit0x02, no state of+0x150, in mode 12, no state of+0xD4,UnitDistSq≤aip4², and neither champion nor unique (MonTypeFlag0x0Cclear). Both count the candidates and keep the last one taken. - T13b. [I] Draw order: melee
aip3; theaip1call to the group; theaip1resurrect draw (only with a corpse); up to twoaip2draws for the missile; oneaip3for the approach.FindHostileadds 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.
T20. Row 20 Scarab
Section titled “T20. Row 20 Scarab”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 arms2. 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 ← &blk3. 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) return4. 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; return5. 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 ownGameFindUnitcall included as a boundary; five outcomes in the first run.
T21. Row 21 Mummy
Section titled “T21. Row 21 Mummy”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); return3. if chance(2): Ring(3); return Idle(aip(5))- T21a. [C]
aip1is a range, not a chance: 15 decisions atdist = aip1take the in-range walk and 13 ataip1 + 1the other arm;aip3is 100 in the 18 cells of the six Mummy records and all 29 melee draws pass. [I] A failedaip3draw runsIdle(aip5)and thenWalk(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.
T22. Row 22 GreaterMummy
Section titled “T22. Row 22 GreaterMummy”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 #23. 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 ← 15. 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 147. if Skill3 ≥ 0 and chance(4) and (h ← FindHostile(&dist, 0)): UseSkill(Sk3mode, Skill3, h, 0, 0); return ; UnHolyBolt8. 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+0xC4bit0x02, not in state set 33; then withundead0 its MonStatslUndead(+0x0Dbit 3), withundead1MONSTATS_IsUndead(+0x0Dbit 3 or bit 4,lUndeadorhUndead); then unlessuniquesToo, not a unique (MonTypeFlag(unit, 8)); thenUnitDistSq≤bound. Each such unit counts ininRange; one in mode 12 with no state of the+0xD4mask becomescorpse, one in another mode than 0 whose stat 6 differs fromUnitGetMaxHpbecomeswounded; each taken one counts intaken. The last one met of each kind is kept. -
T22b. [I] Draw order: the two melee/near draws on
aip1; thenaip3only withSkill2 ≥ 0and a wounded unit,aip2only with a corpse,aip4only withSkill3 ≥ 0, and the 50/50 only with a unit in range. -
T22c. [C] The map up to
A2Q1_BossSeenor 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.
T28. Row 28 Vampire
Section titled “T28. Row 28 Vampire”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) return3. 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); return5. if life < 33: v14 ← 2 if StepAway(t, 8, 0): return ; refused: on to step 6 or 76. 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) return7. 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.
pis computed only whenVelocity > 0, a signed 16-bit test, and is 0 otherwise. With the guard met:q: i32 ← (i16 Run · 100) ÷ (i16 Velocity), truncating, thenq − 100, a result ≤ 0 becoming 0 and one ≥ 120 becoming 120, passed toMonAiPathOverride(unit, 0, p, 0). This is the one such computation in the think; the engaged flee’s step takes no override, andRun · 100cannot wrap. -
T28b. [C] The logged first call in all 279 compared decisions of the first run is the hand map’s:
MonAiFindHostileat 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,Pairboth ways, andT56at 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’sRange(100) > 30made before its two tests fails 1, the second 50 inside 9 dropped fails 2; step 2’s draw made only withb0fails 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 ÷ 8is 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 writesv14(AiCtl+0x14). - T29b. [C] The alt’s map (no draw) by emulation; the think’s logged first call,
UnitLifePctat 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.
T30. Row 30 Fetish
Section titled “T30. Row 30 Fetish”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 life3. 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); return4. 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
+0x0814,+0x0Cthe unit’s[+0xC]and+0x101 and passes it toMonHelpBlock. - T30b. [C] The record map up to
GameFindUnit,AiParamsPoporUnitLifePct, with no draw before them; one outcome in the first run. H1 holds for 316 decisions, 243 on one path.
T34. Row 34 Andariel
Section titled “T34. Row 34 Andariel”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); returnif D100() < aip(2): Idle(5); return ; draw#1if 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); returnOvr(1, 0, 0)Walk(t, 7)- T34a. [C]
Ovr(1, …)plans type 7 (2 calls; D2), andUseSkillcomes after three draws on the unit’s seed, soRNG_Rangetakes the seedU + 0x20set 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.
T35. Row 35 CorruptArcher
Section titled “T35. Row 35 CorruptArcher”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); return3. if not melee and AiMonState3or19(U): SetModeAtUnit(4, h); return4. if r < 6 and chance(4): ; draw#1 Ovr(0, 100, 0) if StepAwayXY(h, 12, 1): return5. 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)); return6. if not chance(2): Idle(aip(3)); return7. 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
FindHostileevery distance test readsr, the hostile’s distance (0x7FFFFFFFwithout one), not the event’s, and every command after it is aimed ath; the no-hostile wander is att. Up to five draws. -
T35b. [C] The logged first call in all 95 compared decisions of the first run is the hand map’s:
MonAiFindHostileat site, one outcome. H1 holds for 111 decisions, 226 calls past the first.
T37. Row 37 SkeletonBow
Section titled “T37. Row 37 SkeletonBow”r ← dist [I] (all of this block)if AiMonState3or19(U) and (h ← FindHostile(&r, 0)): SetModeAtUnit(4, h); returnh ← FindHostile(&r, 0)if no h or r > 19: if chance(3): Toward(t, aip(4), aip(5)); return ; draw#1 Idle(20); returnif chance(1): SetModeAtUnit(4, h); return ; draw#1if D100() < 20: T56(t, 3, 0); return ; draw#2Idle(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.
T61. Row 61 Hireable and state 4
Section titled “T61. Row 61 Hireable and state 4”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); return3. if UNIT_HasState(U, 12): STATE_Set(U, 12, 0) ; inferno if U.mode ∈ {2, 15}: return ; no command AiParamsHead(U) ; result not read4. 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); return6. if U.mode ≠ 1: UNIT_CancelEvents(G, U, 2, 0); Idle(5); return7. 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); return8. 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 (UnitDirToPointon the owner, reduced byDir64To8) it tries up to eight directions in turn, each a point 8 subtiles from the owner’s point in the owner’s logic region, withOvr(0, speed, 40)andMonAiWalkToPointOrStop; none works:UNIT_CancelEvents(2, 0)andMonAiWanderRingDirect(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+0xA0the index,+0xA8 + 8kthe points, newest first), skipping points within 5, withOvr(0, speed, 100)and a walk, thenOvr(15, speed, 100)and a walk (a run with the run flag), then once per callAiPathOverrideSet(speed, 100)and the same move, and after 20 entriesMonAiWanderRingDirect(owner, max(4, Dist/4)); a speed of 0 is replaced by50 + Range(15). Kind 3: a spawn position near the owner (SpawnPosPickwith class 363 when the MonStats count exceeds 363, else −1),UnitTeleportthere,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), thenMonAiInit(ctl, ctl.state),Idle(1). State 5, also row 60’s think: not in mode 1Idle(5); out of town with a hostile within 20 (signed): onRange(100) < 30UseSkill(Sk1mode, Skill1, h, 0, 0)for class 271, elseSetModeAtUnit(4, h); then on a secondRange(100) < 30T56(h, 4, 0); elseIdle(10). OtherwiseD100() < 20Ring(5), elseIdle(10). State 6: the same guard; out of town withFindTarget’sh, oner ← Range(100): out of meleer < 30Walk(h, 7)elseIdle(10), in meleer < 80SetModeAtUnit(4, h)elseIdle(10). Otherwise as state 5. -
T61c. [I] From the code’s own writes
v14is a multiple of 10, so step 4’s 17–19 arm needs another writer. The record is looked up withG+0x70andidthe pet entry’s+8. When no weight was added andr = 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
MonsterOwnerat 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.
T64. Row 64 SkeletonMage
Section titled “T64. Row 64 SkeletonMage”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 #3if e > aip(2) and chance(3): Ovr(0, 10, 0); WalkStop(t, (u8) aip(2)); returnif chance(7): T56(t, 4, 0); returnIdle(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.
T67. Row 67 NecroPet and state 7
Section titled “T67. Row 67 NecroPet and state 7”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 withspeed, or40 + Range(40)whenspeedis 0; kind 2 wanders:Range(100) < 10givesWanderRingDirect(ref, 3 + Range(3)); kind 3 teleports the pet next to the owner (SpawnPosPickfor class 363 or 0,UnitTeleport,+0xC8 |= 0x10000,Idle(5)); kind 4 steps away when another pet of the owner stands within 2 (AiPetCrowdCbover the owner’s pet list); kind 5Ovr(7, 0, 0)andWanderNearUnit(ref, dist). Its threeRangecalls are on the pet’s seed. -
T67c. [I] Which code sets
v14for a necromancer’s pet is not read. -
T67e. [I] Past the first boundary a consistency check.
T89. Row 89 Megademon
Section titled “T89. Row 89 Megademon”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 ← lvh ← UNIT_HasState(U, 12) ; infernoif 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 farif 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) returnfar:if chance(4): Walk(t, 7) else Idle(10)- T89a. [I] The skill’s level is used as a distance, and
aip6is 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
SkillFindByOwneror, forSkill1 < 0,UNIT_HasState(U, 12), nothing after it; one outcome in the first run. H1 holds for 77 decisions.
T125. Row 125 BloodLord
Section titled “T125. Row 125 BloodLord”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)); returnif 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)orWalkZ(t). Live: five outcomes in the first run.
Outputs
Section titled “Outputs”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
Worked example
Section titled “Worked example”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)
Reimplementation notes
Section titled “Reimplementation notes”- [I] A draw in a failed
and.chance(K)short-circuits as written: row 13’s secondchance(1)is drawn only with a corpse, row 22’saip3only withSkill2 ≥ 0and a wounded unit. Keep the order of the tests. - [I] Row 21’s
Idledoes not return: the walk runs in the same event. - [C] The callbacks compare signed:
AiDeadMinionCban AI distance,AiFallenCorpseCbandAiGreaterMummyCbUnitDistSq, 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 signedimulsquares of an i16. - [C] Row 125 uses the constant mode 5 and no sign test on
Skill1: Skill1 is sign-extended (movsx) intoMonAiUseSkill’s skill argument, and 46 cases with a negative i16 reachMonAiUseSkill(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/+0x10of a kind-1 head record for itsGameFindUnit; 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.
Evidence
Section titled “Evidence”- 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.