AI companions that are real heroes in the sim

Recruitable AI party members that are ordinary SimPlayers driven by a 5 Hz brain sending client commands, yielding to real players, at 0.3 ms a tick for 120 of them.

A small party of four heroes fights in the golden sand of the Scorched Dunes, with poison clouds and a burst of light around them
Contents
  1. Real heroes, not a new entity
  2. A brain that presses the same buttons you do
  3. Class kits
  4. Rules that keep it honest
  5. Stepping aside for real players
  6. Private worlds
  7. What it costs
  8. Where the brain stops
  9. Related posts

When nobody else is online, a solo hero still gets the party game: a Warden holding the pack, a Stalker raining arrows, an Emberwright dropping Starfall on the elite, four names in the party frame. Diablo II's answer was one hireling; Duskbound's is three real heroes in the sim.

Real heroes, not a new entity

Option Problem
Bot clients (account, row, socket each) 120 extra sockets in one Bun process to simulate what's already in memory
New entity type a second branch in every combat path, skill, aura, buff, nameplate, animation and transfer, forever
A SimPlayer with one extra field none
TypeScript
bot: { ownerId: string }
  • Lives in world.players and state.players as cmp_<entryId>; combat, skills, animations, auras, transfers and nameplates just work
  • No session, so per-player events, saves and the private self sheet skip it naturally
  • PlayerState.owner lets clients draw ally plates and party-frame rows
The Party panel open on the left, listing three companions (Brannoc the Warden, Wren the Stalker and Tamsin the Warden) with Dismiss buttons, and the three heroes standing around their owner on a rainy coast
The Party panel (O) with a full crew at the Storm Coast. "Party 4 / 4 heroes · 3 / 3 companions". Each companion has a name, a class and a Dismiss button.

Recruit in the Party panel: class, name, Recruit. Three per hero, saved on the character.

  • Always at the owner's level; rebuilt on level-up; no inventory
  • Fixed skill build and attribute split per class
  • Gear rolled deterministically from id, level and difficulty: white under 5, magic from 5, rare weapon and chest from 15, all rares in Nightmare and Hell

A brain that presses the same buttons you do

The AI never touches state. It calls runCommand with move, attackTarget or cast, the same commands a client sends, so mana, cooldowns, weapon requirements and line of sight apply unchanged. It thinks every 200 ms, staggered across ticks:

  1. Stay close. More than 25 tiles from the owner, or stuck for 2.5 s: blink to a formation spot behind them (doors, cliffs, waypoints)
  2. Pick a target within 12 tiles of the owner; drop the fight beyond 10 tiles from the owner
  3. Choose an action from the class kit
  4. Position for range and line of sight, then send
Four heroes clustered in the rain on a dark coast road near a glowing exit marker, with a party frame listing four names in the top-left corner
Idle companions hold formation slots behind their owner. The party frame in the corner lists all four heroes with life bars, the same frame a real party gets.
server/game/companions/brain.tsTypeScript
let score = Math.hypot(m.x - bot.x, m.y - bot.y)
if (m.id === ownerTarget) score -= 100
else if (m.targetId === owner.id) score -= 50
else if (m.targetId === bot.id) score -= 30
if (m.id === brain.targetId) score -= 20

Owner's target, then whatever hits the owner, then whatever hits the companion, then nearest, with stickiness against flip-flopping. Sleeping monsters only within 7 tiles of the owner, so companions never wake the next room. Melee companions check for a path first; unreachable targets (across lava, water) are blacklisted for 5 s.

Class kits

Plain if priority lists. The whole Warden:

server/game/companions/brain.tsTypeScript
case 'warden': {
  if (lifeFrac < 0.45 && !hasBuff(bot, 'w_aegis') && canUse(w, bot, 'w_aegis')) return cast('w_aegis', true)
  if (!hasBuff(bot, 'w_rallying_cry') && canUse(w, bot, 'w_rallying_cry')) return cast('w_rallying_cry', true)
  if (!bot.aura && canUse(w, bot, 'w_hallowed_ground')) return cast('w_hallowed_ground', true)
  if (near >= 3 && canUse(w, bot, 'w_warcry')) return cast('w_warcry', true)
  if (pack >= 2 && canUse(w, bot, 'w_cleave')) return cast('w_cleave')
  if (elite(target) && canUse(w, bot, 'w_verdict')) return cast('w_verdict')
  if (elite(target) && canUse(w, bot, 'w_shieldbreaker')) return cast('w_shieldbreaker')
  if (canUse(w, bot, 'w_twin_cut')) return cast('w_twin_cut')
  if (canUse(w, bot, 'w_sunder')) return cast('w_sunder')
  return { kind: 'attack' }
}

canUse checks learned, cooldown, cost and weapon, so the list works at every level; at level 3 most lines fall through.

  • Warden: companion War Cry also taunts everything within 6 tiles. Without it, the tank was melee DPS in plate.
  • Stalker: Rain of Barbs on 3+, Volley and Quickdraw on pairs, then Seeker and Barbed Arrow.
  • Emberwright: keeps Rime Ward up, saves Starfall for elites and big packs.
  • Kiting: ranged classes step back from anything within 2.5 tiles, weighted 2:1 away-from-monster to toward-owner, so they back into the party.
  • No potions: mana regenerates 10%/s, life 5%/s out of combat. A companion out of mana mid-fight looks broken, not balanced.
A level-60 Emberwright and three companions clearing a pack in the Scorched Dunes, recorded in-engine.

Rules that keep it honest

Companions are a party for a solo hero, not three free farmers. The rules live in one hooks file:

  • No loot, no XP: skipped in lootEligible and chests
  • Kills credit the owner, undiluted: splitting XP with companions would punish using them
  • Half a player for scaling: monster life is +35% per extra player; a companion counts 0.5, so a full crew adds 52.5% instead of 105%, and bigger monsters pay proportionally more XP
  • Cheap death: nothing lost, never hardcore, back up beside the owner after 10 s
  • Not on the ladder: rift clears and boss kills record real players only

Stepping aside for real players

Companions fill a party to four heroes; real players can go to eight. When a real player would push it past four, a companion steps aside, and returns when there's room:

server/game/companions/registry.tsTypeScript
while (total > cap) {
  const pick = state
    .filter((s) => active(s).length > 0)
    .sort((a, b) => active(b).length - active(a).length || newest(active(b)) - newest(active(a)) || a.ownerId.localeCompare(b.ownerId))[0]
  const c = pick ? active(pick).sort((a, b) => b.since - a.since)[0] : undefined
  if (!pick || !c) break
  c.active = false
  out.push({ kind: 'yield', ownerId: pick.ownerId, id: c.id })
  total--
}
  • The owner with the most active companions yields first, newest companion first: two solo players with three each end up with one each, not three and zero
  • Returns a list of changes rather than applying them, so tests are trivial: full crew solo, newest steps aside, biggest crew yields first, four real players leave no room, companions return
  • Re-planned about once a second per room; a toast ("Wren steps aside to make room") and a Rejoin button

Private worlds

An Open / Private toggle on the character screen. Private joins carry privateKey = <characterId>, and rooms match on zoneId, difficulty and privateKey, so the hero gets their own instance of every zone, towns included.

onAuth enforces privateWorldAllows: the owner, their party, and anyone partied with someone inside. Invited players still arrive through the normal party transfer. server/game/privacy.test.ts pins it.

What it costs

scripts/companion-perf.ts: 40 owners fighting in the Scorched Dunes for 30 s, then again with three companions each (160 heroes):

A bar chart: 40 heroes with no companions at 0.17 ms average and 0.51 ms p95 per tick; 40 heroes plus 120 companions at 0.32 ms and 0.80 ms
One zone room, everyone fighting. The tick budget at 20 Hz is 50 ms. The companion AI itself averages 0.09 ms per tick.
Average tick p95
40 heroes 0.17 ms 0.51 ms
40 heroes + 120 companions 0.32 ms 0.80 ms
of which companion AI 0.09 ms

Companions don't count toward the 40-client cap (Colyseus counts sockets). The real cost of a crowded room is event fan-out (forty players); there, your own companions' effects are treated like your own casts and always reach you.

Where the brain stops

No model of boss telegraphs: companions don't leave the Architect's warning tiles or switch to the Choir's chiming voice. The data already exists (clients draw every telegraph from server events), so the next rule is a first line in think: if standing in a marked area, move out.