The following are the algorithms used by xu4. Most of them are exact matches to the original Ultima IV, while others are simply guessed. Algorithms have a certain level of accuracy to the original Ultima IV, which is indicated by the "Accuracy" column.

Values are: exact - Exact emulation of the original Ultima IV behavior near - Near-exact emulation unknown - Level of accuracy unknown

# Camping

random(8) == 0
(((moves/100) >= 0x10000) or (((moves/100) & 0xffff) != lastcamp))
lastcamp = (moves/100) & 0xffff
random(8) == 0
random(4) == 0
(map == Skara Brae) and (random(4) == 0)

# Combat

random(32) == 0
!placedLeadersLeader and (random(8) == 0)
if (guard)ncreatures = numPartyMembers * 2
elsencreatures = 1
ncreatures = random(8) + 1
if (ncreatures == 1) { if (creature.encountersize == 0) ncreatures = random(creature.encountersize) + creature.encountersize + 1 else ncreatures = 8
}
while (ncreatures > (2 * numPartyMembers)) ncreatures = random(16) + 1
(aura == quick) and playerIsNotDisabled and (random(2) == 0)
random(8) == 0
Add monster to random empty location in combat monsters location array
random(2) == 0 and !alreadyPoisoned
random(2) == 0
maxDamage = weapon.damage
maxDamage += player.strength
if(maxDamage > 255)maxDamage=255
realDamage = random(maxDamage)
x = random(creature.basehp >> 2)
damage = (x >> 4) + ((x >> 2) & 0xfc)
damage += x % 10
xu4_random(0x100) > armor.defense
(player.dex + 0x80) >= xu4_random(0x100)
(player.status == 'G') or (player.status == 'P')

# Party

if (!adjustment.isTimeLimited or (((moves / 16) >= 0x10000) or (((moves / 16) & 0xFFFF) != lastvirtue))) { lastvirtue = (moves / 16) & 0xFFFF; return true;
}
random(5) == 0
random(2) == 0
(hull < 50) and (random(4) == 0)
random(50) + random(8) + 10
yes if all party members are disabled
!(person of that class already in party) and
(numPartyMembers + 1 < (avatar.hpMax / 100)) and
((party.karma[class] == 0) or (party.karma[class] >= 40))

# Game

for (i = 0; i < 0x20; i++) { test_x = random(map.width) test_y = random(map.height) tile = map.tileAt(test_x, test_y) if (tile.isCreatureWalkable()) { x = test_x y = test_y }
}
bool ok = false
int tries = 0
static const int MAX_TRIES = 10
while (!ok and (tries < MAX_TRIES)) { dx = 7 dy = random(7) if (random(2))dx = -dx if (random(2))dy = -dy if (random(2)) { temp = dx dx = dy dy = temp } if (creature.canMoveOn(map.tileAt(avatar.x + dx, avatar.y + dy))) { spawn_x = avatar.x + dx spawn_y = avatar.y + dy ok = true } else tries++