Prev: C6FD Up: Map Next: C973
C71C: Character behaviour programs and reaction tables
The programs that make the characters act on their own, run by 976C. Every character has a pointer (in its entry in C9BA) to where it has got to in its program, and a reaction table. Each comment below decodes one instruction.
HOW THE PROGRAMS RUN. On each of its turns a character works through its program until one instruction succeeds; that ends its turn, and next turn it carries on from the following instruction. An instruction that fails either falls through to the next one, or, if it has an 'if it fails go to' address, jumps there, in the same turn. A character gives up after six failures in one turn. 'Go to' instructions do not use up a turn. Actions without a target or instrument let the game choose suitable objects, exactly as it does for the player's commands, so 'attack' means 'attack whoever is here' and 'take' means 'pick up something'.
Instruction formats (the low nibble of the first byte is the type; bit 4 means a failure address follows, bit 5 'once only', bit 6 'no orders from the player here'):
Bytes Instruction
t, action, target, instrument do an action with those objects ($FF = let the game choose)
t+1, address, 0 call a special routine (the routines in A406-A91B)
4, action do an action and let the game choose any objects; action $FF means 'do nothing this turn'
$0E, address go to
$0C, action switch to the reaction for that action
$0F, n switch to a randomly chosen entry of the reaction table
other switch to the default entry of the reaction table and end the turn
REACTION TABLES are lists of 3-byte entries ending with $FF: an action and a program address. When something is done to a character, the entry for that action (if any) becomes its program (99FB); entries with action 0 are the default behaviours, used by 'switch to a random behaviour'. In memory each character's reaction table comes straight before its program (the goblins' three tables come first, together); the headings below mark where each table and each program begins. Common reactions are shared: C94D is the general 'fight back' program (attack; attack again; run; repeat, or follow the attacker if it runs), C96E the response to being given something ('thank you' or 'what do you expect me to do with this?'), and C962 the response to being captured (go through an exit or door if possible, otherwise run).
WHAT THE CHARACTERS DO:
  • Gandalf (C7A9) begins by giving Bilbo the curious map and opening the round green door. After that he is a random wanderer: five little routines (C7B1, C7BF, C7C3, C7C9, C7CD) chosen at random make him run about, pick things up and ask 'what's this?', drop or give things away, open and close doors, and make small talk.
  • Thorin (C7DB) follows Bilbo whenever he can. When he cannot, he picks up the small curious key if he finds it (saying once 'this was thrains key'), asks 'where's the thief?' if Bilbo is invisible, or runs through his idle routine (A550): wait, 'hurry up', or sit down and sing about gold.
  • The wood elf (C80C) wanders at random capturing anyone it meets, who ends up in the Elvenking's dungeon (A316).
  • The warg (C817) attacks anyone it can; otherwise it follows its prey, or runs around Bilbo howling (A4CA), or runs off.
  • The butler (C83F) acts out the story from the book: he unlocks the red door with the red key, opens it, shuts it and locks it again; he opens a barrel and drinks the wine, closes the barrel, opens the trap door, throws the barrel through it into the river, and closes the trap door - capturing any intruder at every opportunity - and then starts again.
  • Elrond (C889) greets Bilbo when he arrives and gives him lunch (A8D9), and otherwise waits.
  • The dragon (C8A7) sleeps on the treasure until it is taken (A5D5); then, as long as the dragon itself is somewhere lit, each turn there is about a 1 in 6 chance that it descends and burns Bilbo to a crisp, otherwise 'in the distance you see the shape of a monstrous dragon flying after you.' Before that, if Bilbo comes into one of its three rooms it flies to him (A591), speaks (A5BB) and burns him.
  • Bard (C8CD) does nothing on his own: he waits for an order from the player (A79F), and then repeats that order every turn. His reaction to being attacked is to SHOOT (C8D8).
  • Gollum (C8E6) asks his riddle when he meets Bilbo (A7C6), and kills him if the answer is wrong (A7EA); he mutters about his precious (A81A), paces north and south-west, drops and picks up the golden ring, and if attacked puts the ring on, becomes invisible and runs.
  • The trolls (C92A) wait until Bilbo comes into the clearing and then say their lines (A8B1). After that their program is: try to eat Bilbo, wait, eat, wait, eat, wait, eat, and then dawn (A865), which turns them to stone. So after the trolls have spoken, Bilbo must be out of the clearing on the turns they try to eat him; waiting outside for the new day dawning (as the HELP message says) is the way.
  • The goblins patrol set routes (for example C731: open the small insignificant crack, go up, down, close the crack, south, north), capturing anyone they meet on the way, who ends up in the goblins' dungeon; the last three goblins (C784) just run about attacking and capturing.
One instruction in this area is changed during play: A79F rewrites the instruction at C8D1 to hold Bard's latest order, which is why SAVE and LOAD take care to save three of its bytes (8284).
The goblins: three reaction tables (one for each kind of goblin), followed by their three behaviour programs.
C71C DEFB $00,$31,$C7 the nasty and hideous goblins: default behaviour (also one of the choices for a random one): program at C731
C71F DEFB $0F,$8D,$C7 the nasty and hideous goblins: when it is attacked: switch to the program at C78D
C722 DEFB $FF End of the table
C723 DEFB $00,$60,$C7 the vicious goblin: default behaviour (also one of the choices for a random one): program at C760
C726 DEFB $0F,$8D,$C7 the vicious goblin: when it is attacked: switch to the program at C78D
C729 DEFB $FF End of the table
C72A DEFB $00,$84,$C7 the horrible, mean and disgusting goblins: default behaviour (also one of the choices for a random one): program at C784
C72D DEFB $0F,$8D,$C7 the horrible, mean and disgusting goblins: when it is attacked: switch to the program at C78D
C730 DEFB $FF End of the table
Behaviour program for the nasty and hideous goblins.
C731 DEFB $02,$10,$06,$FF action 16 [open] (A298) target small insignificant crack, with -
C735 DEFB $14,$30,$3C,$C7 action 48 [capture] (A316); if it fails go to C73C
C739 DEFB $0E,$35,$C7 go to C735
C73C DEFB $04,$09 action 9 [up go] (8D19)
C73E DEFB $14,$30,$45,$C7 action 48 [capture] (A316); if it fails go to C745
C742 DEFB $0E,$3E,$C7 go to C73E
C745 DEFB $04,$0A action 10 [down go] (8D19)
C747 DEFB $14,$30,$4E,$C7 action 48 [capture] (A316); if it fails go to C74E
C74B DEFB $0E,$47,$C7 go to C747
C74E DEFB $02,$0C,$06,$FF action 12 [close] (90A2) target small insignificant crack, with -
C752 DEFB $04,$02 action 2 [south go] (8D19)
C754 DEFB $14,$30,$5B,$C7 action 48 [capture] (A316); if it fails go to C75B
C758 DEFB $0E,$54,$C7 go to C754
C75B DEFB $04,$01 action 1 [north go] (8D19)
C75D DEFB $0E,$31,$C7 go to C731
Behaviour program for the vicious goblin.
C760 DEFB $04,$06 action 6 [northwest go] (8D19)
C762 DEFB $14,$30,$69,$C7 action 48 [capture] (A316); if it fails go to C769
C766 DEFB $0E,$62,$C7 go to C762
C769 DEFB $04,$01 action 1 [north go] (8D19)
C76B DEFB $14,$30,$72,$C7 action 48 [capture] (A316); if it fails go to C772
C76F DEFB $0E,$6B,$C7 go to C76B
C772 DEFB $04,$08 action 8 [southwest go] (8D19)
C774 DEFB $14,$30,$7B,$C7 action 48 [capture] (A316); if it fails go to C77B
C778 DEFB $0E,$74,$C7 go to C774
C77B DEFB $04,$09 action 9 [up go] (8D19)
C77D DEFB $14,$30,$60,$C7 action 48 [capture] (A316); if it fails go to C760
C781 DEFB $0E,$7D,$C7 go to C77D
Behaviour program for the horrible, mean and disgusting goblins.
C784 DEFB $04,$24 action 36 [run] (8F17)
C786 DEFB $04,$0F action 15 [attack with] (90DB)
C788 DEFB $04,$30 action 48 [capture] (A316)
C78A DEFB $0E,$84,$C7 go to C784
The goblins' own reaction to being attacked: capture the attacker, then go back to the normal program.
C78D DEFB $04,$30 action 48 [capture] (A316)
C78F DEFB $07 switch to default behaviour and end turn
Gandalf: reaction table (how the character reacts when something is done to them), then behaviour program (what they do on their own turns).
C790 DEFB $00,$B1,$C7 default behaviour (also one of the choices for a random one): program at C7B1
C793 DEFB $00,$BF,$C7 default behaviour (also one of the choices for a random one): program at C7BF
C796 DEFB $00,$C3,$C7 default behaviour (also one of the choices for a random one): program at C7C3
C799 DEFB $00,$C9,$C7 default behaviour (also one of the choices for a random one): program at C7C9
C79C DEFB $00,$CD,$C7 default behaviour (also one of the choices for a random one): program at C7CD
C79F DEFB $1D,$6E,$C9 when it is given something: switch to the program at C96E
C7A2 DEFB $30,$62,$C9 when it is captured: switch to the program at C962
C7A5 DEFB $0F,$4D,$C9 when it is attacked: switch to the program at C94D
C7A8 DEFB $FF End of the table
Behaviour program for Gandalf.
C7A9 DEFB $02,$1D,$03,$00 action 29 [give to] (9308) target curious map, with bilbo
C7AD DEFB $02,$10,$05,$FF action 16 [open] (9078) target round green door, with -
C7B1 DEFB $04,$24 action 36 [run] (8F17)
C7B3 DEFB $14,$13,$BF,$C7 action 19 [take] (8CC8); if it fails go to C7BF
C7B7 DEFB $03,$1C,$A4,$00 call A41C (Character routine: "what's this?")
C7BB DEFB $04,$0C action 12 [close]
C7BD DEFB $04,$1D action 29 [give to] (9308)
C7BF DEFB $04,$24 action 36 [run] (8F17)
C7C1 DEFB $04,$0D action 13 [drop] (8C45)
C7C3 DEFB $04,$24 action 36 [run] (8F17)
C7C5 DEFB $03,$25,$A4,$00 call A425 (Character routine: small talk)
C7C9 DEFB $04,$24 action 36 [run] (8F17)
C7CB DEFB $04,$1D action 29 [give to] (9308)
C7CD DEFB $04,$10 action 16 [open]
C7CF DEFB $0F,$04 switch to a random behaviour (1 of up to 4)
Thorin: reaction table (how the character reacts when something is done to them), then behaviour program (what they do on their own turns).
C7D1 DEFB $00,$DB,$C7 default behaviour (also one of the choices for a random one): program at C7DB
C7D4 DEFB $1D,$6E,$C9 when it is given something: switch to the program at C96E
C7D7 DEFB $0F,$4D,$C9 when it is attacked: switch to the program at C94D
C7DA DEFB $FF End of the table
Behaviour program for Thorin.
C7DB DEFB $12,$27,$00,$FF,$E4,$C7 action 39 [follow] (8F40) target bilbo, with -; if it fails go to C7E4
C7E1 DEFB $0E,$DB,$C7 go to C7DB
C7E4 DEFB $12,$13,$02,$FF,$EE,$C7 action 19 [take] (8CC8) target small curious key, with -; if it fails go to C7EE
C7EA DEFB $23,$43,$A4,$00 call A443 (Character routine: "this was thrains key") (once)
C7EE DEFB $13,$39,$A5,$00,$FB,$C7 call A539 (Character routine: "where's the thief?"); if it fails go to C7FB
C7F4 DEFB $04,$FF do nothing this turn
C7F6 DEFB $04,$24 action 36 [run] (8F17)
C7F8 DEFB $0E,$DB,$C7 go to C7DB
C7FB DEFB $03,$50,$A5,$00 call A550 (Character routine: Thorin)
C7FF DEFB $0E,$DB,$C7 go to C7DB
The wood elf: reaction table (how the character reacts when something is done to them), then behaviour program (what they do on their own turns).
C802 DEFB $00,$0C,$C8 default behaviour (also one of the choices for a random one): program at C80C
C805 DEFB $0F,$0C,$C8 when it is attacked: switch to the program at C80C
C808 DEFB $1D,$6E,$C9 when it is given something: switch to the program at C96E
C80B DEFB $FF End of the table
Behaviour program for the wood elf.
C80C DEFB $04,$30 action 48 [capture] (A316)
C80E DEFB $04,$24 action 36 [run] (8F17)
C810 DEFB $0E,$0C,$C8 go to C80C
The warg: reaction table (how the character reacts when something is done to them), then behaviour program (what they do on their own turns).
C813 DEFB $00,$17,$C8 default behaviour (also one of the choices for a random one): program at C817
C816 DEFB $FF End of the table
Behaviour program for the warg.
C817 DEFB $14,$0F,$1E,$C8 action 15 [attack with] (90DB); if it fails go to C81E
C81B DEFB $0E,$17,$C8 go to C817
C81E DEFB $14,$27,$29,$C8 action 39 [follow] (8F40); if it fails go to C829
C822 DEFB $03,$CA,$A4,$00 call A4CA (Character routine: the warg)
C826 DEFB $0E,$17,$C8 go to C817
C829 DEFB $14,$24,$30,$C8 action 36 [run] (8F17); if it fails go to C830
C82D DEFB $0E,$17,$C8 go to C817
C830 DEFB $04,$FF do nothing this turn
C832 DEFB $0E,$17,$C8 go to C817
The butler: reaction table (how the character reacts when something is done to them), then behaviour program (what they do on their own turns).
C835 DEFB $00,$3F,$C8 default behaviour (also one of the choices for a random one): program at C83F
C838 DEFB $0F,$4F,$C8 when it is attacked: switch to the program at C84F
C83B DEFB $1D,$6E,$C9 when it is given something: switch to the program at C96E
C83E DEFB $FF End of the table
Behaviour program for the butler.
C83F DEFB $02,$26,$08,$0F action 38 [unlock with] (A264) target red door, with red key
C843 DEFB $02,$10,$08,$FF action 16 [open] (9078) target red door, with -
C847 DEFB $02,$0C,$08,$FF action 12 [close] (90A2) target red door, with -
C84B DEFB $02,$25,$08,$0F action 37 [lock with] (A264) target red door, with red key
C84F DEFB $14,$30,$56,$C8 action 48 [capture] (A316); if it fails go to C856
C853 DEFB $0E,$4F,$C8 go to C84F
C856 DEFB $02,$10,$13,$FF action 16 [open] (9078) target barrel, with -
C85A DEFB $02,$21,$14,$FF action 33 [drink] (9206) target wine, with -
C85E DEFB $04,$30 action 48 [capture] (A316)
C860 DEFB $02,$0C,$13,$FF action 12 [close] (90A2) target barrel, with -
C864 DEFB $02,$10,$0C,$FF action 16 [open] (A577) target large trap door, with -
C868 DEFB $04,$30 action 48 [capture] (A316)
C86A DEFB $02,$13,$13,$FF action 19 [take] (8CC8) target barrel, with -
C86E DEFB $02,$2C,$13,$0C action 44 [throw through] (9404) target barrel, with large trap door
C872 DEFB $04,$30 action 48 [capture] (A316)
C874 DEFB $02,$0C,$0C,$FF action 12 [close] (A577) target large trap door, with -
C878 DEFB $14,$30,$3F,$C8 action 48 [capture] (A316); if it fails go to C83F
C87C DEFB $0E,$3F,$C8 go to C83F
Elrond: reaction table (how the character reacts when something is done to them), then behaviour program (what they do on their own turns).
C87F DEFB $00,$8F,$C8 default behaviour (also one of the choices for a random one): program at C88F
C882 DEFB $0F,$99,$C8 when it is attacked: switch to the program at C899
C885 DEFB $1D,$6E,$C9 when it is given something: switch to the program at C96E
C888 DEFB $FF End of the table
Behaviour program for Elrond.
C889 DEFB $13,$4C,$A4,$00,$93,$C8 call A44C (Character routine: greet Bilbo); if it fails go to C893
C88F DEFB $03,$D9,$A8,$00 call A8D9 (Character routine: Elrond gives Bilbo lunch)
C893 DEFB $04,$FF do nothing this turn
C895 DEFB $14,$FF,$89,$C8 end turn and go to C889
C899 DEFB $14,$0F,$89,$C8 action 15 [attack with] (90DB); if it fails go to C889
C89D DEFB $0E,$99,$C8 go to C899
The dragon: reaction table (how the character reacts when something is done to them), then behaviour program (what they do on their own turns).
C8A0 DEFB $00,$A7,$C8 default behaviour (also one of the choices for a random one): program at C8A7
C8A3 DEFB $0F,$BC,$C8 when it is attacked: switch to the program at C8BC
C8A6 DEFB $FF End of the table
Behaviour program for the dragon.
C8A7 DEFB $13,$D5,$A5,$00,$B0,$C8 call A5D5 (Character routine: the dragon wakes); if it fails go to C8B0
C8AD DEFB $0E,$A7,$C8 go to C8A7
C8B0 DEFB $13,$91,$A5,$00,$C1,$C8 call A591 (Character routine: the dragon comes for Bilbo); if it fails go to C8C1
C8B6 DEFB $13,$BB,$A5,$00,$C1,$C8 call A5BB (Character routine: the dragon speaks); if it fails go to C8C1
C8BC DEFB $04,$2D action 45 [burn] (A232)
C8BE DEFB $0E,$A7,$C8 go to C8A7
C8C1 DEFB $04,$24 action 36 [run] (8F17)
C8C3 DEFB $0E,$A7,$C8 go to C8A7
Bard: reaction table (how the character reacts when something is done to them), then behaviour program (what they do on their own turns).
C8C6 DEFB $00,$CD,$C8 default behaviour (also one of the choices for a random one): program at C8CD
C8C9 DEFB $0F,$D8,$C8 when it is attacked: switch to the program at C8D8
C8CC DEFB $FF End of the table
Behaviour program for Bard.
C8CD DEFB $43,$9F,$A7,$00 call A79F (Character routine: Bard remembers his orders) (no orders)
C8D1 DEFB $42,$13,$25,$FF action 19 [take] (8CC8) target wooden chest, with - (no orders)
C8D5 DEFB $0E,$CD,$C8 go to C8CD
C8D8 DEFB $54,$3A,$4D,$C9 action 58 [shoot] (8FE0); if it fails go to C94D (no orders)
C8DC DEFB $0E,$D8,$C8 go to C8D8
Gollum: reaction table (how the character reacts when something is done to them), then behaviour program (what they do on their own turns).
C8DF DEFB $00,$E6,$C8 default behaviour (also one of the choices for a random one): program at C8E6
C8E2 DEFB $0F,$1A,$C9 when it is attacked: switch to the program at C91A
C8E5 DEFB $FF End of the table
Behaviour program for Gollum.
C8E6 DEFB $13,$C6,$A7,$00,$FF,$C8 call A7C6 (Character routine: Gollum asks his riddle); if it fails go to C8FF
C8EC DEFB $43,$EA,$A7,$00 call A7EA (Character routine: Gollum waits for the answer) (no orders)
C8F0 DEFB $02,$0D,$10,$FF action 13 [drop] (8C45) target valuable golden ring, with -
C8F4 DEFB $14,$01,$10,$C9 action 1 [north go] (8D19); if it fails go to C910
C8F8 DEFB $02,$13,$10,$FF action 19 [take] (8CC8) target valuable golden ring, with -
C8FC DEFB $0E,$E6,$C8 go to C8E6
C8FF DEFB $14,$01,$15,$C9 action 1 [north go] (8D19); if it fails go to C915
C903 DEFB $13,$1A,$A8,$00,$E6,$C8 call A81A (Character routine: Gollum mutters); if it fails go to C8E6
C909 DEFB $02,$13,$10,$FF action 19 [take] (8CC8) target valuable golden ring, with -
C90D DEFB $0E,$E6,$C8 go to C8E6
C910 DEFB $04,$08 action 8 [southwest go] (8D19)
C912 DEFB $0E,$F8,$C8 go to C8F8
C915 DEFB $04,$08 action 8 [southwest go] (8D19)
C917 DEFB $0E,$03,$C9 go to C903
C91A DEFB $02,$28,$10,$FF action 40 [wear] (A2C0) target valuable golden ring, with -
C91E DEFB $04,$24 action 36 [run] (8F17)
C920 DEFB $0E,$E6,$C8 go to C8E6
The trolls: reaction table (how the character reacts when something is done to them), then behaviour program (what they do on their own turns).
C923 DEFB $00,$30,$C9 default behaviour (also one of the choices for a random one): program at C930
C926 DEFB $0F,$4D,$C9 when it is attacked: switch to the program at C94D
C929 DEFB $FF End of the table
Behaviour program for the trolls.
C92A DEFB $13,$B1,$A8,$00,$2A,$C9 call A8B1 (Character routine: the trolls talk); if it fails go to C92A
C930 DEFB $03,$42,$A8,$00 call A842 (Character routine: a character eats Bilbo)
C934 DEFB $04,$FF do nothing this turn
C936 DEFB $03,$42,$A8,$00 call A842 (Character routine: a character eats Bilbo)
C93A DEFB $04,$FF do nothing this turn
C93C DEFB $03,$42,$A8,$00 call A842 (Character routine: a character eats Bilbo)
C940 DEFB $04,$FF do nothing this turn
C942 DEFB $03,$42,$A8,$00 call A842 (Character routine: a character eats Bilbo)
C946 DEFB $03,$65,$A8,$00 call A865 (Character routine: dawn in the trolls' clearing)
C94A DEFB $0E,$30,$C9 go to C930
Shared reaction program: fight back. Used by Gandalf, Thorin, the trolls, and by Bard when he cannot shoot.
C94D DEFB $54,$0F,$5A,$C9 action 15 [attack with] (90DB); if it fails go to C95A (no orders)
C951 DEFB $54,$0F,$61,$C9 action 15 [attack with] (90DB); if it fails go to C961 (no orders)
C955 DEFB $44,$24 action 36 [run] (8F17) (no orders)
C957 DEFB $0E,$4D,$C9 go to C94D
C95A DEFB $54,$27,$61,$C9 action 39 [follow] (8F40); if it fails go to C961 (no orders)
C95E DEFB $0E,$4D,$C9 go to C94D
C961 DEFB $07 switch to default behaviour and end turn
Shared reaction program: what to do when captured. Used by Gandalf.
C962 DEFB $14,$1E,$69,$C9 action 30 [go through]; if it fails go to C969
C966 DEFB $04,$24 action 36 [run] (8F17)
C968 DEFB $07 switch to default behaviour and end turn
C969 DEFB $04,$FF do nothing this turn
C96B DEFB $0E,$62,$C9 go to C962
Shared reaction program: being given something. Used by Elrond, Gandalf, Thorin, the butler, the wood elf.
C96E DEFB $03,$06,$A4,$00 call A406 (Character routine: react to being given something)
C972 DEFB $07 switch to default behaviour and end turn
Prev: C6FD Up: Map Next: C973