Exile (Standard version) disassembly
====================================
Exile was written by Peter Irvin and Jeremy Smith, and was published by
Superior Software for the BBC Micro in 1988. It is a graphical adventure game
in which the player must retrieve a spaceship's destinator, which has been
stolen by a deranged scientist and hidden deep in a maze of tunnels and
caverns. The game features a procedurally generated landscape and a realistic
physics engine. It is notable for its technical complexity.

The following disassembly was created by reverse engineering binary images,
without access to any source code. It is nevertheless reasonably complete,
allowing the technical approaches used to be understood.

The author of this disassembly imposes no additional copyright restrictions
beyond those already present on the game itself. It is provided for educational
purposes only, and it is hoped that the original authors will accept it in the
good faith it was intended - as a tribute to their skills.

Technical notes
===============
This disassembly analyses the standard version of the game.

A disassembly of the game's disk protection and supervisor may be found at:
http://www.level7.org.uk/miscellany/exile-disk-protection-and-supervisor-disassembly.txt

A disassembly of the enhanced version of the game may be found at:
http://www.level7.org.uk/miscellany/exile-enhanced-disassembly.txt

The game is decrypted before use. There is extensive use of self-modifying code
throughout. The carry flag is often used in combination with shift and BIT
operations to process variables. BIT and CMP are also used as null operations
for optimised branching, which is noted in the disassembly by "(nop)".

Areas of particular interest:

&0d4d - &10cf : sprite plotting
All the sprites used in the game are generated from a 128x81 four colour bitmap
(stored at &53ec). The sprite plotting routines allow an arbitrary portion of
this bitmap to be translated into a choice of palettes and flipped horizontally
and vertically. The sixteen colours provided by the system are split into two
groups of eight - one for objects (background), and one for tiles (foreground).
These will be referred to here by upper and lower case letters respectively.

&1715 - &19a6 : landscape generation
A procedural generation algorithm is used to create a 256x256 tile landscape,
overlaid with a small amount of mapped data (stored at &4fec) for the start and
end game and areas associated with set-piece puzzles. Various positions are
indicated by the algorithm as potential places for tertiary objects.

&1a0b - &1e18 : physics engine
Objects are stored in one of three lists. The primary list has sixteen slots
for objects that are onscreen or nearly onscreen, which are processed by the
physics engine each update. The secondary list provides a further thirty-two
slots for objects whose position is remembered when offscreen. The tertiary
list stores objects that are fixed to, or emerge from, a particular location.

For example, a nest of birds is stored in the tertiary list. When the player
moves near to it, a bird may emerge, creating an object on the primary list,
and the nest is marked as having one fewer bird. Should the player teleport
away, the bird becomes offscreen. It is then removed from the primary list, and
the nest is marked a having one more bird. Similarly, a turret is promoted to
the primary list when onscreen, and unless destroyed, returned to the tertiary
list when it becomes offscreen. If the player were to drop a remote control
device or disturb a robot, these would be moved to the secondary list once they
became offscreen and remain there until they became primary again.

&1320 - &149c : sound generation
Sounds are generated using two envelopes, one for volume, one for frequency.

&27c9 - &2866 : NPC stimuli
&316b - &31ab
Many NPCs have a mood which affects their behaviour, and is in turn affected
by various stimuli. 

&6235 - &67ec : copy protection
Before starting, the game prompts for a word from the novella that accompanied
a purchased copy. If the correct word is not entered, the game starts in a demo
mode that hangs after a random period of time.

Game-play notes
===============
Invisible objects become visible under the influence of red mushrooms.

Windy caverns cease to be so once underwater.

Imps can be fed according to their type (see &317f) to yield power pods
capsules, a weapon that fires plasma balls, or active grenades.

Many creatures have a minimum energy which must be overcome in order to destroy
them. A clawed robot therefore cannot be destroyed simply by firing at it
repeatedly with an icer - a more explosive means of destruction must be found.

Interesting Pokes
=================
&24aa = &60, &1b70 = &a9
invulnerability

&2d79 = &60
infinite energy

&2cf3 = &00, &2d0d = &00
use all weapons, even if not collected

&31bf = &00
use all keys, even if not collected

&2cdc = &55
fire coronium boulders when jetpack is selected

&34c7 = &00
pocket any object, regardless of size

&2c2c = &00
scroll around landscape without moving player

&0324 = &3c
see invisible debris in windy tiles

Game disassembly
================
; ExileB
; 001200 0079AD 67BA

# &1200 - &798e is decrypted at &798e

# &1200 - &78ec is moved to &0100 - &67ec at &793e

; obstruction_patterns                                              # &00 is entirely empty, &ff is entirely obstructed
&0100 00 00 00 00 00 00 00 00 ; &00
&0108 00 08 10 18 20 28 30 38 ; &08
&0110 00 10 20 30 40 50 60 70 ; &10
&0118 08 28 48 68 88 a8 c8 e8 ; &18
&0120 ff ff 00 00 00 00 00 00 ; &20
&0128 60 98 b8 d0 e0 f0 ff ff ; &28
&0130 00 00 08 18 28 40 60 98 ; &30
&0138 00 00 ff ff ff ff ff ff ; &38
&0140 38 30 28 20 18 10 08 00 ; &40
&0148 70 60 50 40 30 20 10 00 ; &48
&0150 e8 c8 a8 88 68 48 28 08 ; &50
&0158 00 00 00 00 00 00 ff ff ; &58
&0160 ff ff f0 e0 d0 b8 98 60 ; &60
&0168 98 60 40 28 18 08 00 00 ; &68
&0170 ff ff ff ff ff ff 00 00 ; &70
&0178 40 40 00 00 00 00 00 00 ; &78
&0180 00 00 00 00 00 00 40 40 ; &80
&0188 00 00 ff ff ff ff 00 00 ; &88
&0190 40 40 00 00 00 00 40 40 ; &90
&0198 80 68 48 10 00 00 00 00 ; &98
&01a0 00 00 00 00 10 48 68 80 ; &a0

; process_actions
&01a8 a2 26    LDX #&26
&01aa 86 ac    STX &ac ; key
; process_actions_loop
&01ac a6 ac    LDX &ac ; key
&01ae bd 6b 12 LDA &126b,X ; action_keys_pressed                    # Negative if key pressed
&01b1 10 0f    BPL &01c2 ; consider_next_action
&01b3 c9 c0    CMP #&c0                                             # Set negative if key pressed now and previous time
&01b5 bd 1d 12 LDA &121d,X ; action_routine_addresses_high_table    # &01 set to suppress auto-repeat
&01b8 6a       ROR A                                                # 84218421 -> .8421842 1, i.e. set carry if so
&01b9 10 04    BPL &01bf ; not_recently_pressed
&01bb b0 05    BCS &01c2 ; consider_next_action
&01bd 29 7f    AND #&7f                                             # Remaining bits specify high byte of routine address
; not_recently_pressed
&01bf 20 ca 01 JSR &01ca ; call_action_routine
; consider_next_action
&01c2 c6 ac    DEC &ac ; key
&01c4 10 e6    BPL &01ac ; process_actions_loop
&01c6 ee f5 11 INC &11f5 ; suppress_checking_for_keypresses         # Set to non-zero to check all keys
&01c9 60       RTS

; call_action_routine
&01ca 48       PHA ; action_routine_address_high
&01cb bd f6 11 LDA &11f6,X ; action_routine_addresses_low_table
&01ce 48       PHA ; action_routine_address_low
&01cf 60       RTS                                                  # Leave via action routine

; wipe_screen_and_start_game
&01d0 58       CLI
&01d1 a9 60    LDA #&60 ; &6000 = screen_memory
&01d3 85 90    STA &90 ; wipe_address_high
&01d5 a9 00    LDA #&00
&01d7 85 8f    STA &8f ; wipe_address_low
&01d9 a8       TAY
; wipe_screen_loop                                                  # Wipe &6000 - &7fff
&01da 91 8f    STA (&8f),Y ; wipe_address
&01dc c8       INY
&01dd d0 fb    BNE &01da ; wipe_screen_loop
&01df e6 90    INC &90 ; wipe_address_high
&01e1 10 f7    BPL &01da ; wipe_screen_loop
&01e3 a9 01    LDA #&01 ; R1: Number of characters per line
&01e5 a2 40    LDX #&40                                             # Display screen
&01e7 8d 00 fe STA &fe00 ; video register number
&01ea 8e 01 fe STX &fe01 ; video register value
&01ed 4c b6 19 JMP &19b6 ; main_game_loop

; unused
&01f0 aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa               # &01ff upwards is used as stack
&0200 aa aa aa aa

; irq1_vector
&0204 a6 12 ; &12a6 = irq1_handler

# Particle types
# ==============
# &0206 = particle_types_ttl_randomness_table
# &0207 = particle_types_ttl_table
# &0208 = particle_types_speed_randomness_table
# &0209 = particle_types_speed_table
# &020a = particle_types_colour_and_flags_table
#         Flags given to new particles, stored in particles_colour_and_flags (see &28dd)
#             8....... always set; temporary unset in particle_colour_and_flags to suppress half double-height particles
#             .4...... set if particle is double height
#             ..2..... set if particle is plotted on foreground
#             ...1.... set if particle accelerates
#             ....8... set to cycle particle colour
#             .....421 colour
# &020b = particle_types_colour_and_flags_randomness_table
# &020c = particle_types_flags_table
#         Flags used in creation of new particles:
#             84...... if &80, set new particle base velocities from object velocities
#                      if &c0, set new particle base velocities from object acceleration
#                      otherwise, use previously specified velocities
#             ..2..... if set, set new particle base position from object position
#             ...1.... if set, consider vertical flipping when setting new particle base position
#             ....8... if set, use vertical centre of object for new particle base position
#             .....4.. if set, consider horizontal flipping when setting new particle base position
#             ......2. if set, use horizontal centre of object for new particle base position
#             .......1 if set, add object velocities to new particle velocities
# &020d = particle_types_x_randomness_table
# &020e = particle_types_y_randomness_table
# &020f = particle_types_velocity_x_randomness_table
# &0210 = particle_types_velocity_y_randomness_table

; particle_types_data
;      6  7  8  9  a  b  c  d  e  f  0
;     ttl   mag   cf    fl  x  y xv yv
;         r     r     r     r  r  r  r
&0206 0f 1e 0f 0a 91 02 a0 1f 1f 03 03 ; &00 (PARTICLE_PLASMA)
&0211 0f 03 0f 18 86 01 ed 00 00 03 03 ; &0b (PARTICLE_JETPACK)
&021c ff 00 00 00 91 46 2a 00 00 2f 2f ; &16 (PARTICLE_EXPLOSION)
&0227 07 05 07 0a 81 02 20 7f 3f 00 00 ; &21 (PARTICLE_FIREBALL)
&0232 07 02 0f 03 82 01 2a 00 00 03 03 ; &2c (PARTICLE_PROJECTILE_TRAIL)
&023d 0f 14 0f 1e 81 42 00 00 00 0f 03 ; &37 (PARTICLE_ENGINE)
&0248 03 10 01 3f a8 07 2d 00 00 00 01 ; &42 (PARTICLE_AIM)
&0253 1c 08 00 00 88 47 00 ff ff 00 00 ; &4d (PARTICLE_STAR_OR_MUSHROOM)
&025e 0f 14 07 0a 97 41 22 00 00 03 03 ; &58 (PARTICLE_FLASK)
&0269 07 0a 03 06 97 41 01 00 00 0f 03 ; &63 (PARTICLE_WATER)
&0274 07 0a 0f 28 97 41 00 ff ff 03 03 ; &6e (PARTICLE_WIND)

; unused
&027f 00 00 00 00 00 00 00 00

; unused
&0287 4c df 14 JMP &14df ; prepare_game_for_save_without_copy_protection_check

# Object types
# ============
# object type                          sprite                                         palette and pickup
# &00 OBJECT_PLAYER                  : &028a &04 ; SPRITE_SPACESUIT_VERTICAL          &02ef &3e ; mwY, no
# &01 OBJECT_ACTIVE_CHATTER          : &028b &14 ; SPRITE_CHATTER                     &02f0 &1b ; cyR, no
# &02 OBJECT_CREW_MEMBER             : &028c &04 ; SPRITE_SPACESUIT_VERTICAL          &02f1 &2e ; mwG, no
# &03 OBJECT_FLUFFY                  : &028d &75 ; SPRITE_FLUFFY                      &02f2 &f2 ; rmW, yes
# &04 OBJECT_SMALL_HIVE              : &028e &1e ; SPRITE_SLIME_THREE                 &02f3 &32 ; rmY, no
# &05 OBJECT_LARGE_HIVE              : &028f &1b ; SPRITE_LARGE_HIVE                  &02f4 &32 ; rmY, no
# &06 OBJECT_RED_FROGMAN             : &0290 &10 ; SPRITE_FROGMAN_ONE                 &02f5 &53 ; rcM, no
# &07 OBJECT_GREEN_FROGMAN           : &0291 &10 ; SPRITE_FROGMAN_ONE                 &02f6 &05 ; gyK, no
# &08 OBJECT_INVISIBLE_FROGMAN       : &0292 &10 ; SPRITE_FROGMAN_ONE                 &02f7 &0f ; cwK, no
# &09 OBJECT_RED_SLIME               : &0293 &1c ; SPRITE_SLIME_ONE                   &02f8 &14 ; rwR, no
# &0a OBJECT_GREEN_SLIME             : &0294 &1c ; SPRITE_SLIME_ONE                   &02f9 &29 ; gwG, no
# &0b OBJECT_YELLOW_SLIME            : &0295 &20 ; SPRITE_BOULDER                     &02fa &bc ; ywY, yes
# &0c OBJECT_DENSE_NEST              : &0296 &70 ; SPRITE_NEST                        &02fb &65 ; gyC, no
# &0d OBJECT_SUCKING_NEST            : &0297 &70 ; SPRITE_NEST                        &02fc &65 ; gyC, no
# &0e OBJECT_BIG_FISH                : &0298 &61 ; SPRITE_BIG_FISH                    &02fd &f7 ; gmW, yes
# &0f OBJECT_WORM                    : &0299 &52 ; SPRITE_WORM_ONE                    &02fe &97 ; gmR, yes
# &10 OBJECT_PIRANHA                 : &029a &72 ; SPRITE_PIRANHA_ONE                 &02ff &d3 ; rcM, yes
# &11 OBJECT_WASP                    : &029b &4f ; SPRITE_WASP_ONE                    &0300 &c7 ; gmB, yes
# &12 OBJECT_ACTIVE_GRENADE          : &029c &21 ; SPRITE_BALL                        &0301 &ef ; cwC, yes
# &13 OBJECT_ICER_BULLET             : &029d &08 ; SPRITE_BULLET_HORIZONTAL           &0302 &7e ; mwW, no
# &14 OBJECT_TRACER_BULLET           : &029e &08 ; SPRITE_BULLET_HORIZONTAL           &0303 &5f ; cwM, no
# &15 OBJECT_CANNONBALL              : &029f &21 ; SPRITE_BALL                        &0304 &3c ; ywY, no
# &16 OBJECT_BLUE_DEATH_BALL         : &02a0 &21 ; SPRITE_BALL                        &0305 &5a ; BBM, no
# &17 OBJECT_RED_BULLET              : &02a1 &08 ; SPRITE_BULLET_HORIZONTAL           &0306 &11 ; rgR, no
# &18 OBJECT_PISTOL_BULLET           : &02a2 &08 ; SPRITE_BULLET_HORIZONTAL           &0307 &2d ; ryG, no
# &19 OBJECT_PLASMA_BALL             : &02a3 &21 ; SPRITE_BALL                        &0308 &34 ; rwY, no
# &1a OBJECT_HOVERING_BALL           : &02a4 &78 ; SPRITE_HOVERING_BALL               &0309 &e1 ; rgC, yes
# &1b OBJECT_INVISIBLE_HOVERING_BALL : &02a5 &78 ; SPRITE_HOVERING_BALL               &030a &80 ; kyK, yes
# &1c OBJECT_MAGENTA_ROLLING_ROBOT   : &02a6 &13 ; SPRITE_ROLLING_ROBOT               &030b &55 ; gyM, no
# &1d OBJECT_RED_ROLLING_ROBOT       : &02a7 &13 ; SPRITE_ROLLING_ROBOT               &030c &1b ; cyR, no
# &1e OBJECT_BLUE_ROLLING_ROBOT      : &02a8 &13 ; SPRITE_ROLLING_ROBOT               &030d &4c ; ywB, no
# &1f OBJECT_GREEN_WHITE_TURRET      : &02a9 &5e ; SPRITE_TURRET                      &030e &59 ; gwM, no
# &20 OBJECT_CYAN_RED_TURRET         : &02aa &5e ; SPRITE_TURRET                      &030f &23 ; rcG, no
# &21 OBJECT_HOVERING_ROBOT          : &02ab &15 ; SPRITE_HOVERING_ROBOT              &0310 &72 ; rmW, no
# &22 OBJECT_MAGENTA_CLAWED_ROBOT    : &02ac &16 ; SPRITE_CLAWED_ROBOT                &0311 &2e ; mwG, no
# &23 OBJECT_CYAN_CLAWED_ROBOT       : &02ad &16 ; SPRITE_CLAWED_ROBOT                &0312 &7b ; cyW, no
# &24 OBJECT_GREEN_CLAWED_ROBOT      : &02ae &16 ; SPRITE_CLAWED_ROBOT                &0313 &77 ; gmW, no
# &25 OBJECT_RED_CLAWED_ROBOT        : &02af &16 ; SPRITE_CLAWED_ROBOT                &0314 &33 ; rcY, no
# &26 OBJECT_TRIAX                   : &02b0 &04 ; SPRITE_SPACESUIT_VERTICAL          &0315 &39 ; gwY, no
# &27 OBJECT_MAGGOT                  : &02b1 &52 ; SPRITE_WORM_ONE                    &0316 &8b ; cyK, yes
# &28 OBJECT_GARGOYLE                : &02b2 &45 ; SPRITE_GARGOYLE                    &0317 &44 ; rwB, no
# &29 OBJECT_RED_MAGENTA_IMP         : &02b3 &64 ; SPRITE_IMP_WALKING_ONE             &0318 &51 ; rgM, no
# &2a OBJECT_RED_YELLOW_IMP          : &02b4 &64 ; SPRITE_IMP_WALKING_ONE             &0319 &0d ; ryK, no
# &2b OBJECT_BLUE_CYAN_IMP           : &02b5 &64 ; SPRITE_IMP_WALKING_ONE             &031a &46 ; bcB, no
# &2c OBJECT_CYAN_YELLOW_IMP         : &02b6 &64 ; SPRITE_IMP_WALKING_ONE             &031b &2b ; cyG, no
# &2d OBJECT_RED_CYAN_IMP            : &02b7 &64 ; SPRITE_IMP_WALKING_ONE             &031c &53 ; rcM, no
# &2e OBJECT_GREEN_YELLOW_BIRD       : &02b8 &59 ; SPRITE_BIRD_ONE                    &031d &35 ; gyY, no
# &2f OBJECT_WHITE_YELLOW_BIRD       : &02b9 &59 ; SPRITE_BIRD_ONE                    &031e &3c ; ywY, no
# &30 OBJECT_RED_MAGENTA_BIRD        : &02ba &59 ; SPRITE_BIRD_ONE                    &031f &02 ; rmK, no
# &31 OBJECT_INVISIBLE_BIRD          : &02bb &59 ; SPRITE_BIRD_ONE                    &0320 &01 ; rgK, no
# &32 OBJECT_LIGHTNING               : &02bc &6d ; SPRITE_LIGHTNING_QUARTER           &0321 &70 ; kyW, no
# &33 OBJECT_RED_MUSHROOM_BALL       : &02bd &63 ; SPRITE_MUSHROOM_BALL               &0322 &9c ; ywR, yes
# &34 OBJECT_BLUE_MUSHROOM_BALL      : &02be &63 ; SPRITE_MUSHROOM_BALL               &0323 &cf ; cwB, yes
# &35 OBJECT_INVISIBLE_DEBRIS        : &02bf &0b ; SPRITE_BULLET_SIXTY                &0324 &00 ; kyK, no
# &36 OBJECT_RED_DROP                : &02c0 &0f ; SPRITE_DROP                        &0325 &14 ; rwR, no
# &37 OBJECT_FIREBALL                : &02c1 &17 ; SPRITE_FIREBALL                    &0326 &10 ; kyR, no
# &38 OBJECT_INACTIVE_CHATTER        : &02c2 &14 ; SPRITE_CHATTER                     &0327 &4b ; cyB, no
# &39 OBJECT_MOVING_FIREBALL         : &02c3 &17 ; SPRITE_FIREBALL                    &0328 &10 ; kyR, no
# &3a OBJECT_GIANT_BLOCK             : &02c4 &39 ; SPRITE_STONE                       &0329 &0c ; ywK, no
# &3b OBJECT_ENGINE_FIRE             : &02c5 &17 ; SPRITE_FIREBALL                    &032a &34 ; rwY, no
# &3c OBJECT_HORIZONTAL_METAL_DOOR   : &02c6 &4a ; SPRITE_METAL_DOOR_HORIZONTAL       &032b &6b ; cyC, no
# &3d OBJECT_VERTICAL_METAL_DOOR     : &02c7 &4b ; SPRITE_METAL_DOOR_VERTICAL         &032c &6b ; cyC, no
# &3e OBJECT_HORIZONTAL_STONE_DOOR   : &02c8 &3c ; SPRITE_STONE_HORIZONTAL_QUARTER    &032d &42 ; rmB, no
# &3f OBJECT_VERTICAL_STONE_DOOR     : &02c9 &41 ; SPRITE_STONE_VERTICAL_QUARTER      &032e &42 ; rmB, no
# &40 OBJECT_BUSH                    : &02ca &1a ; SPRITE_TALL_BUSH                   &032f &31 ; rgY, no
# &41 OBJECT_TRANSPORTER_BEAM        : &02cb &71 ; SPRITE_TRANSPORTER_BEAM            &0330 &6f ; cwC, no
# &42 OBJECT_SWITCH                  : &02cc &2e ; SPRITE_SWITCH                      &0331 &15 ; gyR, no
# &43 OBJECT_PIANO                   : &02cd &5d ; SPRITE_PIANO                       &0332 &2e ; mwG, no
# &44 OBJECT_EXPLOSION               : &02ce &17 ; SPRITE_FIREBALL                    &0333 &12 ; rmR, no
# &45 OBJECT_BOULDER                 : &02cf &20 ; SPRITE_BOULDER                     &0334 &cb ; cyB, yes
# &46 OBJECT_CANNON                  : &02d0 &56 ; SPRITE_CANNON                      &0335 &33 ; rcY, no
# &47 OBJECT_ALIEN_WEAPON            : &02d1 &57 ; SPRITE_ALIEN_WEAPON                &0336 &b1 ; rgY, yes
# &48 OBJECT_MAGGOT_MACHINE          : &02d2 &47 ; SPRITE_SPACESHIP_WALL_PIPES        &0337 &62 ; rmC, no
# &49 OBJECT_PLACEHOLDER             : &02d3 &22 ; SPRITE_CRYSTAL                     &0338 &00 ; kyK, no
# &4a OBJECT_DESTINATOR              : &02d4 &60 ; SPRITE_CONSOLE                     &0339 &db ; cyM, yes
# &4b OBJECT_POWER_POD               : &02d5 &7b ; SPRITE_POWER_POD                   &033a &9f ; cwR, yes
# &4c OBJECT_EMPTY_FLASK             : &02d6 &76 ; SPRITE_FLASK                       &033b &8f ; cwK, yes
# &4d OBJECT_FULL_FLASK              : &02d7 &76 ; SPRITE_FLASK                       &033c &cf ; cwB, yes
# &4e OBJECT_REMOTE_CONTROL_DEVICE   : &02d8 &58 ; SPRITE_REMOTE_CONTROL_DEVICE       &033d &e5 ; gyC, yes
# &4f OBJECT_CANNON_CONTROL_DEVICE   : &02d9 &58 ; SPRITE_REMOTE_CONTROL_DEVICE       &033e &8e ; mwK, yes
# &50 OBJECT_INACTIVE_GRENADE        : &02da &21 ; SPRITE_BALL                        &033f &ef ; cwC, yes
# &51 OBJECT_CYAN_YELLOW_GREEN_KEY   : &02db &4d ; SPRITE_KEY                         &0340 &ab ; cyG, yes
# &52 OBJECT_RED_YELLOW_GREEN_KEY    : &02dc &4d ; SPRITE_KEY                         &0341 &ad ; ryG, yes
# &53 OBJECT_GREEN_YELLOW_RED_KEY    : &02dd &4d ; SPRITE_KEY                         &0342 &95 ; gyR, yes
# &54 OBJECT_YELLOW_WHITE_RED_KEY    : &02de &4d ; SPRITE_KEY                         &0343 &9c ; ywR, yes
# &55 OBJECT_CORONIUM_BOULDER        : &02df &20 ; SPRITE_BOULDER                     &0344 &91 ; rgR, yes
# &56 OBJECT_RED_MAGENTA_RED_KEY     : &02e0 &4d ; SPRITE_KEY                         &0345 &92 ; rmR, yes
# &57 OBJECT_BLUE_CYAN_GREEN_KEY     : &02e1 &4d ; SPRITE_KEY                         &0346 &a6 ; bcG, yes
# &58 OBJECT_CORONIUM_CRYSTAL        : &02e2 &22 ; SPRITE_CRYSTAL                     &0347 &91 ; rgR, yes
# &59 OBJECT_JETPACK_BOOSTER         : &02e3 &6b ; SPRITE_JETPACK_BOOSTER             &0348 &b1 ; rgY, yes
# &5a OBJECT_PISTOL                  : &02e4 &6c ; SPRITE_WEAPON                      &0349 &8e ; mwK, yes
# &5b OBJECT_ICER                    : &02e5 &6c ; SPRITE_WEAPON                      &034a &e0 ; kyC, yes
# &5c OBJECT_BLASTER                 : &02e6 &79 ; SPRITE_PILL                        &034b &a2 ; rmG, yes
# &5d OBJECT_PLASMA_GUN              : &02e7 &6c ; SPRITE_WEAPON                      &034c &b5 ; gyY, yes
# &5e OBJECT_PROTECTION_SUIT         : &02e8 &04 ; SPRITE_SPACESUIT_VERTICAL          &034d &b3 ; rcY, yes
# &5f OBJECT_FIRE_IMMUNITY_DEVICE    : &02e9 &7a ; SPRITE_DEVICE                      &034e &e3 ; rcC, yes
# &60 OBJECT_MUSHROOM_IMMUNITY_PILL  : &02ea &63 ; SPRITE_MUSHROOM_BALL               &034f &d5 ; gyM, yes
# &61 OBJECT_WHISTLE_ONE             : &02eb &7c ; SPRITE_WHISTLE                     &0350 &e3 ; rcC, yes
# &62 OBJECT_WHISTLE_TWO             : &02ec &7c ; SPRITE_WHISTLE                     &0351 &d7 ; gmM, yes
# &63 OBJECT_RADIATION_IMMUNITY_PILL : &02ed &79 ; SPRITE_PILL                        &0352 &f0 ; kyW, yes
# &64 OBJECT_INVISIBLE_INERT         : &02ee &77 ; SPRITE_NONE_TWO                    &0353 &f1 ; rgW, yes

; object_types_sprite_table                                         # Sprite used for object by default
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
&028a 04 14 04 75 1e 1b 10 10 10 1c 1c 20 70 70 61 52 ; &00
&029a 72 4f 21 08 08 21 21 08 08 21 78 78 13 13 13 5e ; &10
&02aa 5e 15 16 16 16 16 04 52 45 64 64 64 64 64 59 59 ; &20
&02ba 59 59 6d 63 63 0b 0f 17 14 17 39 17 4a 4b 3c 41 ; &30
&02ca 1a 71 2e 5d 17 20 56 57 47 22 60 7b 76 76 58 58 ; &40
&02da 21 4d 4d 4d 4d 20 4d 4d 22 6b 6c 6c 79 6c 04 7a ; &50
&02ea 63 7c 7c 79 77                                  ; &60

; object_types_palette_and_pickup_table                             # 8....... set if object can be picked up by player
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f               # .4218421 palette
&02ef 3e 1b 2e f2 32 32 53 05 0f 14 29 bc 65 65 f7 97 ; &00
&02ff d3 c7 ef 7e 5f 3c 5a 11 2d 34 e1 80 55 1b 4c 59 ; &10
&030f 23 72 2e 7b 77 33 39 8b 44 51 0d 46 2b 53 35 3c ; &20
&031f 02 01 70 9c cf 00 14 10 4b 10 0c 34 6b 6b 42 42 ; &30
&032f 31 6f 15 2e 12 cb 33 b1 62 00 db 9f 8f cf e5 8e ; &40
&033f ef ab ad 95 9c 91 92 a6 91 b1 8e e0 a2 b5 b3 e3 ; &50
&034f d5 e3 d7 f0 f1                                  ; &60

# Object type flags
# =================
# 8....... if set, object doesn't collide with other objects
# .4...... if set, do not demote object to secondary, but remove when far away (OBJECT_TYPE_FLAG_DO_NOT_KEEP_AS_SECONDARY)
# ..2..... if set, keep object as primary until further away than if unset (OBJECT_TYPE_FLAG_KEEP_AS_PRIMARY_FOR_LONGER) 
# ...1.... if set, return object to tertiary list when far away (OBJECT_TYPE_FLAG_KEEP_AS_TERTIARY)
# .421....     &00 : object remains primary regardless of distance
#              &20 : object is demoted to secondary when more than 4 or 12 tiles offscreen, depending on speed and support
#              &50 : object returns to tertiary list when more than 1 tile offscreen
#              &60 : object is removed when more than 4 or 12 tiles offscreen, depending on speed and support
#              &70 : object returns to tertiary list when more than 4 or 12 tiles offscreen, depending on speed and support
# ....8... if set, return object to nest or pipe when removing it from primary list (OBJECT_TYPE_FLAG_SPAWNED_FROM_NEST)
# .....421 object weight (adding one doubles weight, 7 is static) (OBJECT_TYPE_FLAG_WEIGHT_MASK)
#
# object type                          flags 
# &00 OBJECT_PLAYER                  : &0354 &03 ; weight 3, keep as primary
# &01 OBJECT_ACTIVE_CHATTER          : &0355 &23 ; weight 3, demote to secondary when far offscreen
# &02 OBJECT_CREW_MEMBER             : &0356 &23 ; weight 3, demote to secondary when far offscreen
# &03 OBJECT_FLUFFY                  : &0357 &22 ; weight 2, demote to secondary when far offscreen
# &04 OBJECT_SMALL_HIVE              : &0358 &77 ; static, return to tertiary when far offscreen
# &05 OBJECT_LARGE_HIVE              : &0359 &77 ; static, return to tertiary when far offscreen
# &06 OBJECT_RED_FROGMAN             : &035a &26 ; weight 6, demote to secondary when far offscreen
# &07 OBJECT_GREEN_FROGMAN           : &035b &6d ; weight 5, spawned from nest, remove when far offscreen
# &08 OBJECT_INVISIBLE_FROGMAN       : &035c &6e ; weight 6, spawned from nest, remove when far offscreen
# &09 OBJECT_RED_SLIME               : &035d &f7 ; static, intangible, return to tertiary when far offscreen
# &0a OBJECT_GREEN_SLIME             : &035e &6e ; weight 6, spawned from nest, remove when far offscreen
# &0b OBJECT_YELLOW_SLIME            : &035f &25 ; weight 5, demote to secondary when far offscreen
# &0c OBJECT_DENSE_NEST              : &0360 &f7 ; static, intangible, return to tertiary when far offscreen
# &0d OBJECT_SUCKING_NEST            : &0361 &f7 ; static, intangible, return to tertiary when far offscreen
# &0e OBJECT_BIG_FISH                : &0362 &25 ; weight 5, demote to secondary when far offscreen
# &0f OBJECT_WORM                    : &0363 &69 ; weight 1, spawned from nest, remove when far offscreen
# &10 OBJECT_PIRANHA                 : &0364 &6b ; weight 3, spawned from nest, remove when far offscreen
# &11 OBJECT_WASP                    : &0365 &68 ; weight 0, spawned from nest, remove when far offscreen
# &12 OBJECT_ACTIVE_GRENADE          : &0366 &04 ; weight 4, keep as primary
# &13 OBJECT_ICER_BULLET             : &0367 &63 ; weight 3, remove when far offscreen
# &14 OBJECT_TRACER_BULLET           : &0368 &66 ; weight 6, remove when far offscreen
# &15 OBJECT_CANNONBALL              : &0369 &66 ; weight 6, remove when far offscreen
# &16 OBJECT_BLUE_DEATH_BALL         : &036a &65 ; weight 5, remove when far offscreen
# &17 OBJECT_RED_BULLET              : &036b &66 ; weight 6, remove when far offscreen
# &18 OBJECT_PISTOL_BULLET           : &036c &62 ; weight 2, remove when far offscreen
# &19 OBJECT_PLASMA_BALL             : &036d &64 ; weight 4, remove when far offscreen
# &1a OBJECT_HOVERING_BALL           : &036e &69 ; weight 1, spawned from nest, remove when far offscreen
# &1b OBJECT_INVISIBLE_HOVERING_BALL : &036f &69 ; weight 1, spawned from nest, remove when far offscreen
# &1c OBJECT_MAGENTA_ROLLING_ROBOT   : &0370 &24 ; weight 4, demote to secondary when far offscreen
# &1d OBJECT_RED_ROLLING_ROBOT       : &0371 &25 ; weight 5, demote to secondary when far offscreen
# &1e OBJECT_BLUE_ROLLING_ROBOT      : &0372 &26 ; weight 6, demote to secondary when far offscreen
# &1f OBJECT_GREEN_WHITE_TURRET      : &0373 &77 ; static, return to tertiary when far offscreen
# &20 OBJECT_CYAN_RED_TURRET         : &0374 &77 ; static, return to tertiary when far offscreen
# &21 OBJECT_HOVERING_ROBOT          : &0375 &23 ; weight 3, demote to secondary when far offscreen
# &22 OBJECT_MAGENTA_CLAWED_ROBOT    : &0376 &05 ; weight 5, keep as primary
# &23 OBJECT_CYAN_CLAWED_ROBOT       : &0377 &05 ; weight 5, keep as primary
# &24 OBJECT_GREEN_CLAWED_ROBOT      : &0378 &05 ; weight 5, keep as primary
# &25 OBJECT_RED_CLAWED_ROBOT        : &0379 &05 ; weight 5, keep as primary
# &26 OBJECT_TRIAX                   : &037a &04 ; weight 4, keep as primary
# &27 OBJECT_MAGGOT                  : &037b &68 ; weight 0, spawned from nest, remove when far offscreen
# &28 OBJECT_GARGOYLE                : &037c &77 ; static, return to tertiary when far offscreen
# &29 OBJECT_RED_MAGENTA_IMP         : &037d &6a ; weight 2, spawned from nest, remove when far offscreen
# &2a OBJECT_RED_YELLOW_IMP          : &037e &6c ; weight 4, spawned from nest, remove when far offscreen
# &2b OBJECT_BLUE_CYAN_IMP           : &037f &6b ; weight 3, spawned from nest, remove when far offscreen
# &2c OBJECT_CYAN_YELLOW_IMP         : &0380 &6b ; weight 3, spawned from nest, remove when far offscreen
# &2d OBJECT_RED_CYAN_IMP            : &0381 &6c ; weight 4, spawned from nest, remove when far offscreen
# &2e OBJECT_GREEN_YELLOW_BIRD       : &0382 &6c ; weight 4, spawned from nest, remove when far offscreen
# &2f OBJECT_WHITE_YELLOW_BIRD       : &0383 &6c ; weight 4, spawned from nest, remove when far offscreen
# &30 OBJECT_RED_MAGENTA_BIRD        : &0384 &6d ; weight 5, spawned from nest, remove when far offscreen
# &31 OBJECT_INVISIBLE_BIRD          : &0385 &6d ; weight 5, spawned from nest, remove when far offscreen
# &32 OBJECT_LIGHTNING               : &0386 &e5 ; weight 5, intangible, remove when far offscreen
# &33 OBJECT_RED_MUSHROOM_BALL       : &0387 &61 ; weight 1, remove when far offscreen
# &34 OBJECT_BLUE_MUSHROOM_BALL      : &0388 &61 ; weight 1, remove when far offscreen
# &35 OBJECT_INVISIBLE_DEBRIS        : &0389 &e4 ; weight 4, intangible, remove when far offscreen
# &36 OBJECT_RED_DROP                : &038a &e5 ; weight 5, intangible, remove when far offscreen
# &37 OBJECT_FIREBALL                : &038b &e8 ; weight 0, intangible, spawned from nest, remove when far offscreen
# &38 OBJECT_INACTIVE_CHATTER        : &038c &24 ; weight 4, demote to secondary when far offscreen
# &39 OBJECT_MOVING_FIREBALL         : &038d &ec ; weight 4, intangible, spawned from nest, remove when far offscreen
# &3a OBJECT_GIANT_BLOCK             : &038e &26 ; weight 6, demote to secondary when far offscreen
# &3b OBJECT_ENGINE_FIRE             : &038f &d7 ; static, intangible, return to tertiary when offscreen
# &3c OBJECT_HORIZONTAL_METAL_DOOR   : &0390 &57 ; static, return to tertiary when offscreen
# &3d OBJECT_VERTICAL_METAL_DOOR     : &0391 &57 ; static, return to tertiary when offscreen
# &3e OBJECT_HORIZONTAL_STONE_DOOR   : &0392 &57 ; static, return to tertiary when offscreen
# &3f OBJECT_VERTICAL_STONE_DOOR     : &0393 &57 ; static, return to tertiary when offscreen
# &40 OBJECT_BUSH                    : &0394 &d6 ; weight 6, intangible, return to tertiary when offscreen
# &41 OBJECT_TRANSPORTER_BEAM        : &0395 &d7 ; static, intangible, return to tertiary when offscreen
# &42 OBJECT_SWITCH                  : &0396 &57 ; static, return to tertiary when offscreen
# &43 OBJECT_PIANO                   : &0397 &25 ; weight 5, demote to secondary when far offscreen
# &44 OBJECT_EXPLOSION               : &0398 &82 ; weight 2, intangible, keep as primary
# &45 OBJECT_BOULDER                 : &0399 &26 ; weight 6, demote to secondary when far offscreen
# &46 OBJECT_CANNON                  : &039a &25 ; weight 5, demote to secondary when far offscreen
# &47 OBJECT_ALIEN_WEAPON            : &039b &24 ; weight 4, demote to secondary when far offscreen
# &48 OBJECT_MAGGOT_MACHINE          : &039c &77 ; static, return to tertiary when far offscreen
# &49 OBJECT_PLACEHOLDER             : &039d &74 ; weight 4, return to tertiary when far offscreen
# &4a OBJECT_DESTINATOR              : &039e &24 ; weight 4, demote to secondary when far offscreen
# &4b OBJECT_POWER_POD               : &039f &02 ; weight 2, keep as primary
# &4c OBJECT_EMPTY_FLASK             : &03a0 &22 ; weight 2, demote to secondary when far offscreen
# &4d OBJECT_FULL_FLASK              : &03a1 &24 ; weight 4, demote to secondary when far offscreen
# &4e OBJECT_REMOTE_CONTROL_DEVICE   : &03a2 &22 ; weight 2, demote to secondary when far offscreen
# &4f OBJECT_CANNON_CONTROL_DEVICE   : &03a3 &22 ; weight 2, demote to secondary when far offscreen
# &50 OBJECT_INACTIVE_GRENADE        : &03a4 &24 ; weight 4, demote to secondary when far offscreen
# &51 OBJECT_CYAN_YELLOW_GREEN_KEY   : &03a5 &23 ; weight 3, demote to secondary when far offscreen
# &52 OBJECT_RED_YELLOW_GREEN_KEY    : &03a6 &23 ; weight 3, demote to secondary when far offscreen
# &53 OBJECT_GREEN_YELLOW_RED_KEY    : &03a7 &23 ; weight 3, demote to secondary when far offscreen
# &54 OBJECT_YELLOW_WHITE_RED_KEY    : &03a8 &23 ; weight 3, demote to secondary when far offscreen
# &55 OBJECT_CORONIUM_BOULDER        : &03a9 &25 ; weight 5, demote to secondary when far offscreen
# &56 OBJECT_RED_MAGENTA_RED_KEY     : &03aa &23 ; weight 3, demote to secondary when far offscreen
# &57 OBJECT_BLUE_CYAN_GREEN_KEY     : &03ab &23 ; weight 3, demote to secondary when far offscreen
# &58 OBJECT_CORONIUM_CRYSTAL        : &03ac &02 ; weight 2, keep as primary
# &59 OBJECT_JETPACK_BOOSTER         : &03ad &26 ; weight 6, demote to secondary when far offscreen
# &5a OBJECT_PISTOL                  : &03ae &23 ; weight 3, demote to secondary when far offscreen
# &5b OBJECT_ICER                    : &03af &23 ; weight 3, demote to secondary when far offscreen
# &5c OBJECT_BLASTER                 : &03b0 &23 ; weight 3, demote to secondary when far offscreen
# &5d OBJECT_PLASMA_GUN              : &03b1 &23 ; weight 3, demote to secondary when far offscreen
# &5e OBJECT_PROTECTION_SUIT         : &03b2 &26 ; weight 6, demote to secondary when far offscreen
# &5f OBJECT_FIRE_IMMUNITY_DEVICE    : &03b3 &25 ; weight 5, demote to secondary when far offscreen
# &60 OBJECT_MUSHROOM_IMMUNITY_PILL  : &03b4 &22 ; weight 2, demote to secondary when far offscreen
# &61 OBJECT_WHISTLE_ONE             : &03b5 &22 ; weight 2, demote to secondary when far offscreen
# &62 OBJECT_WHISTLE_TWO             : &03b6 &22 ; weight 2, demote to secondary when far offscreen
# &63 OBJECT_RADIATION_IMMUNITY_PILL : &03b7 &25 ; weight 5, demote to secondary when far offscreen
# &64 OBJECT_INVISIBLE_INERT         : &03b8 &e7 ; static, intangible, remove when far offscreen

; object_types_flags_table
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
&0354 03 23 23 22 77 77 26 6d 6e f7 6e 25 f7 f7 25 69 ; &00
&0364 6b 68 04 63 66 66 65 66 62 64 69 69 24 25 26 77 ; &10
&0374 77 23 05 05 05 05 04 68 77 6a 6c 6b 6b 6c 6c 6c ; &20
&0384 6d 6d e5 61 61 e4 e5 e8 24 ec 26 d7 57 57 57 57 ; &30
&0394 d6 d7 57 25 82 26 25 24 77 74 24 02 22 24 22 22 ; &40
&03a4 24 23 23 23 23 25 23 23 02 26 23 23 23 23 26 25 ; &50
&03b4 22 22 22 25 e7                                  ; &60

# Update routines
# ===============
# Tile update routines are called during the following, if the corresponding bit is set:
#     8... TILE_PROCESSING_FLAG_PLOTTING    : when tile is being plotted
#     .4.. TILE_PROCESSING_FLAG_OBSTRUCTION : when tile is being checked for obstruction
#     ..2. TILE_PROCESSING_FLAG_COLLISION   : when tile is being checked for collision
#     ...1 TILE_PROCESSING_FLAG_EVENTS      : when tile is picked at random during update_events
#
# tile type                                   low       high        update routine                          called
# &00 TILE_INVISIBLE_SWITCH                 : &03b9 &d7 &0432 &20 ; &3ef2 update_invisible_switch_tile      &20 . . 2 .
# &01 TILE_TRANSPORTER                      : &03ba &c8 &0433 &b0 ; &3ee3 update_transporter_tile           &b0 8 . 2 1
# &02 TILE_SPACE_WITH_OBJECT_FROM_DATA      : &03bb &a4 &0434 &91 ; &3fbf update_tile_with_object_from_data &90 8 . . 1
# &03 TILE_METAL_DOOR                       : &03bc &7d &0435 &f0 ; &3e98 update_metal_door_tile            &f0 8 4 2 1
# &04 TILE_STONE_DOOR                       : &03bd &7a &0436 &f0 ; &3e95 update_stone_door_tile            &f0 8 4 2 1
# &05 TILE_STONE_HALF_WITH_OBJECT_FROM_TYPE : &03be &9c &0437 &b1 ; &3fb7 update_tile_with_object_from_type &b0 8 . 2 1
# &06 TILE_SPACE_WITH_OBJECT_FROM_TYPE      : &03bf &9c &0438 &b1 ; &3fb7 update_tile_with_object_from_type &b0 8 . 2 1
# &07 TILE_GREENERY_WITH_OBJECT_FROM_TYPE   : &03c0 &9c &0439 &b1 ; &3fb7 update_tile_with_object_from_type &b0 8 . 2 1
# &08 TILE_SWITCH                           : &03c1 &b2 &043a &b1 ; &3fcd update_switch_tile                &b0 8 . 2 1
# &09 TILE_NEST                             : &03c2 &00 &043b &b0 ; &3e1b update_nest_or_pipe_tile          &b0 8 . 2 1
# &0a TILE_PIPE                             : &03c3 &00 &043c &b0 ; &3e1b update_nest_or_pipe_tile          &b0 8 . 2 1
# &0b TILE_CONSTANT_WIND                    : &03c4 &26 &043d &31 ; &3f41 update_constant_wind_tile         &30 . . 2 1
# &0c TILE_ENGINE                           : &03c5 &6f &043e &90 ; &3e8a update_engine_tile                &90 8 . . 1
# &0d TILE_WATER                            : &03c6 &88 &043f &31 ; &3fa3 update_water_tile                 &30 . . 2 1
# &0e TILE_VARIABLE_WIND                    : &03c7 &fd &0440 &30 ; &3f18 update_variable_wind_tile         &30 . . 2 1
# &0f TILE_MUSHROOMS                        : &03c8 &b7 &0441 &31 ; &3fd2 update_mushroom_tile              &30 . . 2 1
#
# Explosion update routines are called when an object's energy reaches zero, determined by object type.
#
# explosion type
# &10 &00 indestructible                    : &03c9 &7b &0442 &02 ; &4096 consider_teleporting_damaged_player
# &11 &40 explode with loud squeal          : &03ca &a3 &0443 &02 ; &40be explode_object_with_loud_squeal
# &12 &80 explode with no squeal            : &03cb &a0 &0444 &02 ; &40bb explode_object_by_turning_into_fireball
# &13 &c0 explode with squeal               : &03cc &aa &0445 &02 ; &40c5 explode_object_with_squeal
#
# object type                          low       high        update routine                       explosion type
# &00 OBJECT_PLAYER                  : &03cd &f6 &0446 &0b ; &4a11 update_player                  indestructible
# &01 OBJECT_ACTIVE_CHATTER          : &03ce &bc &0447 &0a ; &48d7 update_active_chatter          indestructible
# &02 OBJECT_CREW_MEMBER             : &03cf &d5 &0448 &88 ; &46f0 update_crew_member             turn into fireball
# &03 OBJECT_FLUFFY                  : &03d0 &6d &0449 &84 ; &4288 update_fluffy                  turn into fireball
# &04 OBJECT_SMALL_HIVE              : &03d1 &94 &044a &cd ; &4baf update_hive                    explode with squeal
# &05 OBJECT_LARGE_HIVE              : &03d2 &94 &044b &cd ; &4baf update_hive                    explode with squeal
# &06 OBJECT_RED_FROGMAN             : &03d3 &48 &044c &86 ; &4463 update_red_frogman             turn into fireball
# &07 OBJECT_GREEN_FROGMAN           : &03d4 &5c &044d &86 ; &4477 update_green_frogman           turn into fireball
# &08 OBJECT_INVISIBLE_FROGMAN       : &03d5 &5a &044e &86 ; &4475 update_invisible_frogman       turn into fireball
# &09 OBJECT_RED_SLIME               : &03d6 &ae &044f &09 ; &47c9 update_red_slime               indestructible
# &0a OBJECT_GREEN_SLIME             : &03d7 &0f &0450 &04 ; &422a update_green_slime             indestructible
# &0b OBJECT_YELLOW_SLIME            : &03d8 &4b &0451 &04 ; &4266 update_yellow_slime            indestructible
# &0c OBJECT_DENSE_NEST              : &03d9 &6e &0452 &09 ; &4789 update_dense_nest              indestructible
# &0d OBJECT_SUCKING_NEST            : &03da &d2 &0453 &0f ; &4ded update_sucking_nest            indestructible
# &0e OBJECT_BIG_FISH                : &03db &46 &0454 &89 ; &4761 update_big_fish                turn into fireball
# &0f OBJECT_WORM                    : &03dc &ef &0455 &83 ; &420a update_worm                    turn into fireball
# &10 OBJECT_PIRANHA                 : &03dd &06 &0456 &d1 ; &4f21 update_piranha_or_wasp         explode with squeal
# &11 OBJECT_WASP                    : &03de &06 &0457 &91 ; &4f21 update_piranha_or_wasp         turn into fireball
# &12 OBJECT_ACTIVE_GRENADE          : &03df &dc &0458 &c4 ; &42f7 update_active_grenade          explode with squeal
# &13 OBJECT_ICER_BULLET             : &03e0 &a4 &0459 &c8 ; &46bf update_icer_bullet             explode with squeal
# &14 OBJECT_TRACER_BULLET           : &03e1 &f9 &045a &c7 ; &4614 update_tracer_bullet           explode with squeal
# &15 OBJECT_CANNONBALL              : &03e2 &0b &045b &c5 ; &4326 update_cannonball              explode with squeal
# &16 OBJECT_BLUE_DEATH_BALL         : &03e3 &17 &045c &c5 ; &4332 update_blue_death_ball         explode with squeal
# &17 OBJECT_RED_BULLET              : &03e4 &2f &045d &c5 ; &434a update_red_bullet              explode with squeal
# &18 OBJECT_PISTOL_BULLET           : &03e5 &00 &045e &c6 ; &441b update_pistol_bullet           explode with squeal
# &19 OBJECT_PLASMA_BALL             : &03e6 &6d &045f &8c ; &4a88 update_plasma_ball             turn into fireball
# &1a OBJECT_HOVERING_BALL           : &03e7 &cc &0460 &45 ; &43e7 update_hovering_ball           explode with loud squeal
# &1b OBJECT_INVISIBLE_HOVERING_BALL : &03e8 &d0 &0461 &45 ; &43eb update_invisible_hovering_ball explode with loud squeal
# &1c OBJECT_MAGENTA_ROLLING_ROBOT   : &03e9 &c3 &0462 &50 ; &4ede update_rolling_robot           explode with loud squeal
# &1d OBJECT_RED_ROLLING_ROBOT       : &03ea &c3 &0463 &50 ; &4ede update_rolling_robot           explode with loud squeal
# &1e OBJECT_BLUE_ROLLING_ROBOT      : &03eb &c7 &0464 &50 ; &4ee2 update_blue_rolling_robot      explode with loud squeal
# &1f OBJECT_GREEN_WHITE_TURRET      : &03ec &bd &0465 &50 ; &4ed8 update_turret                  explode with loud squeal
# &20 OBJECT_CYAN_RED_TURRET         : &03ed &bd &0466 &50 ; &4ed8 update_turret                  explode with loud squeal
# &21 OBJECT_HOVERING_ROBOT          : &03ee &e9 &0467 &49 ; &4804 update_hovering_robot          explode with loud squeal
# &22 OBJECT_MAGENTA_CLAWED_ROBOT    : &03ef &04 &0468 &4a ; &481f update_clawed_robot            explode with loud squeal
# &23 OBJECT_CYAN_CLAWED_ROBOT       : &03f0 &04 &0469 &4a ; &481f update_clawed_robot            explode with loud squeal
# &24 OBJECT_GREEN_CLAWED_ROBOT      : &03f1 &04 &046a &4a ; &481f update_clawed_robot            explode with loud squeal
# &25 OBJECT_RED_CLAWED_ROBOT        : &03f2 &04 &046b &4a ; &481f update_clawed_robot            explode with loud squeal
# &26 OBJECT_TRIAX                   : &03f3 &e9 &046c &08 ; &4704 update_triax                   indestructible
# &27 OBJECT_MAGGOT                  : &03f4 &37 &046d &90 ; &4e52 update_maggot                  turn into fireball
# &28 OBJECT_GARGOYLE                : &03f5 &55 &046e &c3 ; &4170 update_gargoyle                explode with squeal
# &29 OBJECT_RED_MAGENTA_IMP         : &03f6 &d4 &046f &86 ; &44ef update_imp                     turn into fireball
# &2a OBJECT_RED_YELLOW_IMP          : &03f7 &d4 &0470 &86 ; &44ef update_imp                     turn into fireball
# &2b OBJECT_BLUE_CYAN_IMP           : &03f8 &d4 &0471 &86 ; &44ef update_imp                     turn into fireball
# &2c OBJECT_CYAN_YELLOW_IMP         : &03f9 &d4 &0472 &86 ; &44ef update_imp                     turn into fireball
# &2d OBJECT_RED_CYAN_IMP            : &03fa &d4 &0473 &86 ; &44ef update_imp                     turn into fireball
# &2e OBJECT_GREEN_YELLOW_BIRD       : &03fb &16 &0474 &88 ; &4631 update_bird                    turn into fireball
# &2f OBJECT_WHITE_YELLOW_BIRD       : &03fc &16 &0475 &88 ; &4631 update_bird                    turn into fireball
# &30 OBJECT_RED_MAGENTA_BIRD        : &03fd &06 &0476 &88 ; &4621 update_whistling_bird          turn into fireball
# &31 OBJECT_INVISIBLE_BIRD          : &03fe &10 &0477 &88 ; &462b update_invisible_bird          turn into fireball
# &32 OBJECT_LIGHTNING               : &03ff &e6 &0478 &02 ; &4101 update_lightning               indestructible
# &33 OBJECT_RED_MUSHROOM_BALL       : &0400 &7d &0479 &08 ; &4698 update_mushroom_ball           indestructible
# &34 OBJECT_BLUE_MUSHROOM_BALL      : &0401 &7d &047a &08 ; &4698 update_mushroom_ball           indestructible
# &35 OBJECT_INVISIBLE_DEBRIS        : &0402 &76 &047b &09 ; &4791 update_invisible_debris        indestructible
# &36 OBJECT_RED_DROP                : &0403 &7e &047c &c9 ; &4799 update_red_drop                explode with squeal
# &37 OBJECT_FIREBALL                : &0404 &bb &047d &0c ; &4ad6 update_fireball                indestructible
# &38 OBJECT_INACTIVE_CHATTER        : &0405 &a6 &047e &0a ; &48c1 update_inactive_chatter        indestructible
# &39 OBJECT_MOVING_FIREBALL         : &0406 &0b &047f &0d ; &4b26 update_moving_fireball         indestructible
# &3a OBJECT_GIANT_BLOCK             : &0407 &81 &0480 &05 ; &439c update_giant_block             indestructible
# &3b OBJECT_ENGINE_FIRE             : &0408 &fa &0481 &0d ; &4c15 update_engine_fire             indestructible
# &3c OBJECT_HORIZONTAL_METAL_DOOR   : &0409 &68 &0482 &ce ; &4c83 update_door                    explode with squeal
# &3d OBJECT_VERTICAL_METAL_DOOR     : &040a &68 &0483 &ce ; &4c83 update_door                    explode with squeal
# &3e OBJECT_HORIZONTAL_STONE_DOOR   : &040b &68 &0484 &ce ; &4c83 update_door                    explode with squeal
# &3f OBJECT_VERTICAL_STONE_DOOR     : &040c &68 &0485 &ce ; &4c83 update_door                    explode with squeal
# &40 OBJECT_BUSH                    : &040d &8e &0486 &0d ; &4ba9 update_bush                    indestructible
# &41 OBJECT_TRANSPORTER_BEAM        : &040e &6b &0487 &0f ; &4d86 update_transporter_beam        indestructible
# &42 OBJECT_SWITCH                  : &040f &82 &0488 &cb ; &499d update_switch                  explode with squeal
# &43 OBJECT_PIANO                   : &0410 &92 &0489 &05 ; &43ad update_inert_object            indestructible
# &44 OBJECT_EXPLOSION               : &0411 &81 &048a &11 ; &4f9c update_explosion               indestructible
# &45 OBJECT_BOULDER                 : &0412 &92 &048b &05 ; &43ad update_inert_object            indestructible
# &46 OBJECT_CANNON                  : &0413 &d3 &048c &02 ; &40ee update_cannon                  indestructible
# &47 OBJECT_ALIEN_WEAPON            : &0414 &fb &048d &43 ; &4216 update_alien_weapon            explode with loud squeal
# &48 OBJECT_MAGGOT_MACHINE          : &0415 &84 &048e &03 ; &419f update_maggot_machine          indestructible
# &49 OBJECT_PLACEHOLDER             : &0416 &49 &048f &0d ; &4b64 update_placeholder_object      indestructible
# &4a OBJECT_DESTINATOR              : &0417 &59 &0490 &05 ; &4374 update_destinator              indestructible
# &4b OBJECT_POWER_POD               : &0418 &45 &0491 &85 ; &4360 update_power_pod               turn into fireball
# &4c OBJECT_EMPTY_FLASK             : &0419 &8c &0492 &05 ; &43a7 update_empty_flask             indestructible
# &4d OBJECT_FULL_FLASK              : &041a &93 &0493 &05 ; &43ae update_full_flask              indestructible
# &4e OBJECT_REMOTE_CONTROL_DEVICE   : &041b &36 &0494 &05 ; &4351 update_remote_control_device   indestructible
# &4f OBJECT_CANNON_CONTROL_DEVICE   : &041c &36 &0495 &05 ; &4351 update_remote_control_device   indestructible
# &50 OBJECT_INACTIVE_GRENADE        : &041d &3d &0496 &c3 ; &4158 update_inactive_grenade        explode with squeal
# &51 OBJECT_CYAN_YELLOW_GREEN_KEY   : &041e &6d &0497 &0d ; &4b88 update_collectable_object      indestructible
# &52 OBJECT_RED_YELLOW_GREEN_KEY    : &041f &6d &0498 &0d ; &4b88 update_collectable_object      indestructible
# &53 OBJECT_GREEN_YELLOW_RED_KEY    : &0420 &6d &0499 &0d ; &4b88 update_collectable_object      indestructible
# &54 OBJECT_YELLOW_WHITE_RED_KEY    : &0421 &6d &049a &0d ; &4b88 update_collectable_object      indestructible
# &55 OBJECT_CORONIUM_BOULDER        : &0422 &af &049b &03 ; &41ca update_coronium_boulder        indestructible
# &56 OBJECT_RED_MAGENTA_RED_KEY     : &0423 &6d &049c &0d ; &4b88 update_collectable_object      indestructible
# &57 OBJECT_BLUE_CYAN_GREEN_KEY     : &0424 &6d &049d &0d ; &4b88 update_collectable_object      indestructible
# &58 OBJECT_CORONIUM_CRYSTAL        : &0425 &a7 &049e &03 ; &41c2 update_coronium_crystal        indestructible
# &59 OBJECT_JETPACK_BOOSTER         : &0426 &6d &049f &0d ; &4b88 update_collectable_object      indestructible
# &5a OBJECT_PISTOL                  : &0427 &6d &04a0 &0d ; &4b88 update_collectable_object      indestructible
# &5b OBJECT_ICER                    : &0428 &6d &04a1 &0d ; &4b88 update_collectable_object      indestructible
# &5c OBJECT_BLASTER                 : &0429 &6d &04a2 &0d ; &4b88 update_collectable_object      indestructible
# &5d OBJECT_PLASMA_GUN              : &042a &6d &04a3 &0d ; &4b88 update_collectable_object      indestructible
# &5e OBJECT_PROTECTION_SUIT         : &042b &6d &04a4 &0d ; &4b88 update_collectable_object      indestructible
# &5f OBJECT_FIRE_IMMUNITY_DEVICE    : &042c &6d &04a5 &0d ; &4b88 update_collectable_object      indestructible
# &60 OBJECT_MUSHROOM_IMMUNITY_PILL  : &042d &6d &04a6 &0d ; &4b88 update_collectable_object      indestructible
# &61 OBJECT_WHISTLE_ONE             : &042e &6d &04a7 &0d ; &4b88 update_collectable_object      indestructible
# &62 OBJECT_WHISTLE_TWO             : &042f &6d &04a8 &0d ; &4b88 update_collectable_object      indestructible
# &63 OBJECT_RADIATION_IMMUNITY_PILL : &0430 &6d &04a9 &0d ; &4b88 update_collectable_object      indestructible
# &64 OBJECT_INVISIBLE_INERT         : &0431 &92 &04aa &05 ; &43ad update_inert_object            indestructible

; update_routine_addresses_low_table
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f               # tiles and explosions
&03b9 d7 c8 a4 7d 7a 9c 9c 9c b2 00 00 26 6f 88 fd b7 ; &00
&03c9 7b a3 a0 aa                                     ; &10
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f               # objects
&03cd f6 bc d5 6d 94 94 48 5c 5a ae 0f 4b 6e d2 46 ef ; &00
&03dd 06 06 dc a4 f9 0b 17 2f 00 6d cc d0 c3 c3 c7 bd ; &10
&03ed bd e9 04 04 04 04 e9 37 55 d4 d4 d4 d4 d4 16 16 ; &20
&03fd 06 10 e6 7d 7d 76 7e bb a6 0b 81 fa 68 68 68 68 ; &30
&040d 8e 6b 82 92 81 92 d3 fb 84 49 59 45 8c 93 36 36 ; &40
&041d 3d 6d 6d 6d 6d af 6d 6d a7 6d 6d 6d 6d 6d 6d 6d ; &50
&042d 6d 6d 6d 6d 92                                  ; &60

; update_routine_addresses_high_table
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f               # tiles and explosions
&0432 20 b0 91 f0 f0 b1 b1 b1 b1 b0 b0 31 90 31 30 31 ; &00         #     8421.... when to call tile update routine
&0442 02 02 02 02                                     ; &10         #     ....8421 routine offset high
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f               # objects
&0446 0b 0a 88 84 cd cd 86 86 86 09 04 04 09 0f 89 83 ; &00         #     84...... explosion type
&0456 d1 91 c4 c8 c7 c5 c5 c5 c6 8c 45 45 50 50 50 50 ; &10         #     ..218421 routine offset high
&0466 50 49 4a 4a 4a 4a 08 90 c3 86 86 86 86 86 88 88 ; &20
&0476 88 88 02 08 08 09 c9 0c 0a 0d 05 0d ce ce ce ce ; &30
&0486 0d 0f cb 05 11 05 02 43 03 0d 05 85 05 05 05 05 ; &40
&0496 c3 0d 0d 0d 0d 03 0d 0d 03 0d 0d 0d 0d 0d 0d 0d ; &50
&04a6 0d 0d 0d 0d 05                                  ; &60

# Tile types
# ==========
# tile type                                       sprite                                                palette
# &00 TILE_INVISIBLE_SWITCH                     : &04ab &c6 SPRITE_NONE                                 &052b &80 kyk
# &01 TILE_TRANSPORTER                          : &04ac &ce SPRITE_TRANSPORTER                          &052c &02 spaceship
# &02 TILE_SPACE_WITH_OBJECT_FROM_DATA          : &04ad &c6 SPRITE_NONE                                 &052d &91 rgr
# &03 TILE_METAL_DOOR                           : &04ae &c6 SPRITE_NONE                                 &052e &91 rgr
# &04 TILE_STONE_DOOR                           : &04af &c6 SPRITE_NONE                                 &052f &91 rgr
# &05 TILE_STONE_HALF_WITH_OBJECT_FROM_TYPE     : &04b0 &bb SPRITE_STONE_HORIZONTAL_HALF                &0530 &00 stone
# &06 TILE_SPACE_WITH_OBJECT_FROM_TYPE          : &04b1 &c6 SPRITE_NONE                                 &0531 &91 rgr
# &07 TILE_GREENERY_WITH_OBJECT_FROM_TYPE       : &04b2 &18 SPRITE_GREENERY                             &0532 &a8 ggg
# &08 TILE_SWITCH                               : &04b3 &2d SPRITE_SWITCH_BOX                           &0533 &dc ywm
# &09 TILE_NEST                                 : &04b4 &70 SPRITE_NEST                                 &0534 &b8 ggy
# &0a TILE_PIPE                                 : &04b5 &6a SPRITE_PIPE                                 &0535 &8c ywk
# &0b TILE_CONSTANT_WIND                        : &04b6 &c6 SPRITE_NONE                                 &0536 &80 kyk
# &0c TILE_ENGINE                               : &04b7 &23 SPRITE_SPACESHIP_SUPPORT                    &0537 &c9 gwb
# &0d TILE_WATER                                : &04b8 &39 SPRITE_STONE                                &0538 &4a BBB
# &0e TILE_VARIABLE_WIND                        : &04b9 &c6 SPRITE_NONE                                 &0539 &80 kyk
# &0f TILE_MUSHROOMS                            : &04ba &62 SPRITE_MUSHROOMS                            &053a &06 mushroom
# &10 TILE_GREEN_HORIZONTAL_QUARTER             : &04bb &c0 SPRITE_EARTH_HORIZONTAL_QUARTER_WITH_EDGE   &053b &88 ggk
# &11 TILE_POSSIBLE_LEAF                        : &04bc &8e SPRITE_LEAF                                 &053c &05 leaf
# &12 TILE_STONE_ONE                            : &04bd &39 SPRITE_STONE                                &053d &04 earth
# &13 TILE_STONE_SLOPE_FORTY_FIVE_FULL          : &04be &44 SPRITE_STONE_SLOPE_FORTY_FIVE_FULL          &053e &00 stone
# &14 TILE_SPACESHIP_WALL_PIPES                 : &04bf &47 SPRITE_SPACESHIP_WALL_PIPES                 &053f &02 spaceship
# &15 TILE_SPACESHIP_WALL_VERTICAL_QUARTER      : &04c0 &26 SPRITE_SPACESHIP_WALL_VERTICAL_QUARTER      &0540 &02 spaceship
# &16 TILE_SPACESHIP_WALL_HORIZONTAL_HALF_TWO   : &04c1 &48 SPRITE_SPACESHIP_WALL_HORIZONTAL_HALF_TWO   &0541 &02 spaceship
# &17 TILE_SPACESHIP_WALL_HORIZONTAL_QUARTER    : &04c2 &49 SPRITE_SPACESHIP_WALL_HORIZONTAL_QUARTER    &0542 &02 spaceship
# &18 TILE_WALL_MOUNTED_EQUIPMENT               : &04c3 &df SPRITE_WALL_MOUNTED_EQUIPMENT               &0543 &02 spaceship
# &19 TILE_SPACE                                : &04c4 &c6 SPRITE_NONE                                 &0544 &91 rgr
# &1a TILE_SHORT_BUSH                           : &04c5 &99 SPRITE_SHORT_BUSH                           &0545 &03 bush
# &1b TILE_TALL_BUSH                            : &04c6 &9a SPRITE_TALL_BUSH                            &0546 &03 bush
# &1c TILE_SPACESHIP_WALL_SMALL_CORNER          : &04c7 &25 SPRITE_SPACESHIP_WALL_SMALL_CORNER          &0547 &02 spaceship
# &1d TILE_SPACESHIP_WALL_CORNER                : &04c8 &2b SPRITE_SPACESHIP_WALL_CORNER                &0548 &02 spaceship
# &1e TILE_STONE_TWO                            : &04c9 &39 SPRITE_STONE                                &0549 &00 stone
# &1f TILE_STONE_HORIZONTAL_HALF                : &04ca &3b SPRITE_STONE_HORIZONTAL_HALF                &054a &00 stone
# &20 TILE_STONE_HORIZONTAL_QUARTER             : &04cb &3c SPRITE_STONE_HORIZONTAL_QUARTER             &054b &00 stone
# &21 TILE_COLUMN                               : &04cc &55 SPRITE_COLUMN                               &054c &bc ywy
# &22 TILE_LEAF                                 : &04cd &8e SPRITE_LEAF                                 &054d &b1 rgy
# &23 TILE_STONE_SLOPE_FORTY_FIVE               : &04ce &43 SPRITE_STONE_SLOPE_FORTY_FIVE               &054e &00 stone
# &24 TILE_STONE_SLOPE_TWENTY_TWO_ONE           : &04cf &34 SPRITE_STONE_SLOPE_TWENTY_TWO_ONE           &054f &00 stone
# &25 TILE_STONE_SLOPE_TWENTY_TWO_TWO           : &04d0 &35 SPRITE_STONE_SLOPE_TWENTY_TWO_TWO           &0550 &00 stone
# &26 TILE_SPACESHIP_WALL_SLOPE_TWELVE_ONE      : &04d1 &27 SPRITE_SPACESHIP_WALL_SLOPE_TWELVE_ONE      &0551 &01 spaceship
# &27 TILE_SPACESHIP_WALL_SLOPE_TWELVE_TWO      : &04d2 &28 SPRITE_SPACESHIP_WALL_SLOPE_TWELVE_TWO      &0552 &01 spaceship
# &28 TILE_SPACESHIP_WALL_SLOPE_TWELVE_THREE    : &04d3 &29 SPRITE_SPACESHIP_WALL_SLOPE_TWELVE_THREE    &0553 &01 spaceship
# &29 TILE_SPACESHIP_WALL_SLOPE_TWELVE_FOUR     : &04d4 &2a SPRITE_SPACESHIP_WALL_SLOPE_TWELVE_FOUR     &0554 &01 spaceship
# &2a TILE_STONE_SLOPE_SEVENTY_EIGHT            : &04d5 &42 SPRITE_STONE_SLOPE_SEVENTY_EIGHT            &0555 &00 stone
# &2b TILE_EARTH_EDGE                           : &04d6 &bf SPRITE_EARTH_EDGE                           &0556 &04 earth
# &2c TILE_EARTH_HORIZONTAL_QUARTER_WITH_EDGE   : &04d7 &40 SPRITE_EARTH_HORIZONTAL_QUARTER_WITH_EDGE   &0557 &04 earth
# &2d TILE_EARTH                                : &04d8 &3d SPRITE_EARTH                                &0558 &04 earth
# &2e TILE_EARTH_SLOPE_FORTY_FIVE               : &04d9 &38 SPRITE_EARTH_SLOPE_FORTY_FIVE               &0559 &04 earth
# &2f TILE_EARTH_SLOPE_TWENTY_TWO_ONE           : &04da &36 SPRITE_EARTH_SLOPE_TWENTY_TWO_ONE           &055a &04 earth
# &30 TILE_EARTH_SLOPE_TWENTY_TWO_TWO           : &04db &37 SPRITE_EARTH_SLOPE_TWENTY_TWO_TWO           &055b &04 earth
# &31 TILE_EARTH_HORIZONTAL_HALF_WITH_EDGE      : &04dc &3e SPRITE_EARTH_HORIZONTAL_HALF_WITH_EDGE      &055c &04 earth
# &32 TILE_SPACESHIP_WALL_CORNER_PIPES_TWO      : &04dd &33 SPRITE_SPACESHIP_WALL_CORNER_PIPES_TWO      &055d &02 spaceship
# &33 TILE_RAIL_HORIZONTAL                      : &04de &31 SPRITE_RAIL_HORIZONTAL                      &055e &01 spaceship
# &34 TILE_RAIL_CORNER                          : &04df &2f SPRITE_RAIL_CORNER                          &055f &01 spaceship
# &35 TILE_RAIL_VERTICAL                        : &04e0 &30 SPRITE_RAIL_VERTICAL                        &0560 &01 spaceship
# &36 TILE_SPACESHIP_WALL_HORIZONTAL_HALF       : &04e1 &2c SPRITE_SPACESHIP_WALL_HORIZONTAL_HALF       &0561 &02 spaceship
# &37 TILE_SPACESHIP_WALL_CORNER_PIPES          : &04e2 &24 SPRITE_SPACESHIP_WALL_CORNER_PIPES_ONE      &0562 &02 spaceship
# &38 TILE_SPACESHIP_WALL_HORIZONTAL_HALF_PIPES : &04e3 &32 SPRITE_SPACESHIP_WALL_HORIZONTAL_HALF_PIPES &0563 &02 spaceship
# &39 TILE_STONE_VERTICAL_QUARTER               : &04e4 &41 SPRITE_STONE_VERTICAL_QUARTER               &0564 &00 stone
# &3a TILE_GARGOYLE                             : &04e5 &45 SPRITE_GARGOYLE                             &0565 &00 stone
# &3b TILE_STONE_HORIZONTAL_THREE_QUARTERS      : &04e6 &3a SPRITE_STONE_HORIZONTAL_THREE_QUARTERS      &0566 &00 stone
# &3c TILE_RED_PIPE                             : &04e7 &6a SPRITE_PIPE                                 &0567 &82 rmk
# &3d TILE_SPACESHIP_SUPPORT                    : &04e8 &23 SPRITE_SPACESHIP_SUPPORT                    &0568 &02 spaceship
# &3e TILE_CONSOLE                              : &04e9 &60 SPRITE_CONSOLE                              &0569 &64 rwC
# &3f TILE_SPACESHIP_LEG                        : &04ea &cc SPRITE_SPACESHIP_LEG                        &056a &ee mwc

; tiles_sprite_and_y_flip_table                                     # 8....... if set, flip vertically for collision checking
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f               #          i.e. for unflipped tile, obstruction is at top
;                                                                   #          otherwise, for unflipped tile, obstruction is at bottom
&04ab c6 ce c6 c6 c6 bb c6 18 2d 70 6a c6 23 39 c6 62 ; &00         # .4218421 sprite
&04bb c0 8e 39 44 47 26 48 49 df c6 99 9a 25 2b 39 3b ; &10
&04cb 3c 55 8e 43 34 35 27 28 29 2a 42 bf 40 3d 38 36 ; &20
&04db 37 3e 33 31 2f 30 2c 24 32 41 45 3a 6a 23 60 cc ; &30

; tiles_y_offset_and_pattern_table                                  # 8421.... start of tile from top of tile, in &10 fractions
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f               # ....8421 set of four patterns to use from &05ab
&04eb 00 00 00 00 00 00 00 d0 c5 b0 c7 00 06 00 00 c0 ; &00
&04fb b0 a0 07 08 00 04 80 c0 70 00 b0 80 99 08 00 80 ; &10
&050b c0 00 a0 03 02 82 01 41 81 c1 04 f0 b0 00 03 02 ; &20
&051b 82 70 06 c0 c5 04 80 06 80 04 99 30 c7 06 a9 00 ; &30

; tiles_palette_table                                               # &00 - &06 to set palette from function, otherwise value
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
&052b 80 02 91 91 91 00 91 a8 dc b8 8c 80 c9 4a 80 06 ; &00
&053b 88 05 04 00 02 02 02 02 02 91 03 03 02 02 00 00 ; &10
&054b 00 bc b1 00 00 00 01 01 01 01 00 04 04 04 04 04 ; &20
&055b 04 04 02 01 01 01 02 02 02 00 00 00 82 02 64 ee ; &30

; tiles_obstruction_y_offsets_table                                 # Added to obstruction pattern to offset tile vertically
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f               # 8421.... &10 fractions to add if not flipped vertically
&056b 0f 3a 0f 0f 0f 77 0f f0 b0 f0 c0 0f 00 f0 0f f0 ; &00         # ....8421 &10 fractions to add if flipped vertical
&057b 0f 0f 00 06 0f 00 77 b3 0f 0f 0f 0f 90 06 0f 77 ; &10
&058b b3 f0 0f 10 07 70 0b 37 73 b0 00 0f b3 0f 10 07 ; &20
&059b 70 77 00 b3 b0 00 77 00 77 00 90 2c c0 00 90 0f ; &30

; obstruction_pattern_low_addresses_table                           # Using the low nibble of tiles_y_fraction_table * 4
;      -  v  h vh                                                   # and the tile orientation, offset into obstruction_patterns
&05ab 00 00 00 00 ; &0
&05af 08 40 40 08 ; &1
&05b3 10 48 48 10 ; &2
&05b7 18 50 50 18 ; &3
&05bb 38 20 70 58 ; &4
&05bf 38 78 70 80 ; &5
&05c3 30 60 68 28 ; &6
&05c7 88 90 88 90 ; &7
&05cb a0 50 98 18 ; &8
&05cf 18 98 50 a0 ; &9

; tile_tertiary_object_ranges_table
;      0  1  2  3  4  5  6  7  8  9
&05d3 00 1d 39 57 7a 9e bc d8 f6 fe

; tertiary_objects_data_offset
;      0  1  2  3  4  5  6  7  8
&05dd 01 fe fb fa f8 f5 f5 f3 f3

; tertiary_objects_type_offset
;      0  1  2  3  4  5  6  7  8
&05e6 00 f5 e9 de ce be b1 a1 98

# Tertiary objects
# ================
# TILE_CHECK_TERTIARY_OBJECT_RANGE_ZERO
# 
# &00 : &05ef &ff ; x    : doesn't match any tile, used when worms emerge anywhere in world
#       &06ee &89 ; tile : TILE_NEST | TILE_FLIP_HORIZONTAL 
#       &0987 &7c ; data : 31 creatures
#       &0a72 &0f ; type : OBJECT_WORM
# 
# &01 : &05f0 &ff ; x    : doesn't match any tile, used when maggots emerge anywhere in world
#       &06ef &89 ; tile : TILE_NEST | TILE_FLIP_HORIZONTAL 
#       &0988 &60 ; data : 24 creatures
#       &0a73 &27 ; type : OBJECT_MAGGOT
# 
# &02 : &05f1 &b0 ; x    : (&b0, &4e) 
#       &06f0 &89 ; tile : TILE_NEST | TILE_FLIP_HORIZONTAL (spawns immediately)
#       &0989 &04 ; data : 1 creature
#       &0a74 &2e ; type : OBJECT_GREEN_YELLOW_BIRD
# 
# &03 : &05f2 &ec ; x    : (&ec, &c0) 
#       &06f1 &89 ; tile : TILE_NEST | TILE_FLIP_HORIZONTAL (spawns immediately)
#       &098a &88 ; data : 2 creatures, with bush
#       &0a75 &07 ; type : OBJECT_GREEN_FROGMAN
# 
# &04 : &05f3 &77 ; x    : (&77, &54) 
#       &06f2 &89 ; tile : TILE_NEST | TILE_FLIP_HORIZONTAL (spawns immediately)
#       &098b &88 ; data : 2 creatures, with bush
#       &0a76 &2f ; type : OBJECT_WHITE_YELLOW_BIRD
# 
# &05 : &05f4 &64 ; x    : (&64, &94) 
#       &06f3 &8a ; tile : TILE_PIPE | TILE_FLIP_HORIZONTAL (spawns immediately)
#       &098c &a0 ; data : 8 creatures, with bush
#       &0a77 &2d ; type : OBJECT_RED_CYAN_IMP
# 
# &06 : &05f5 &9a ; x    : (&9a, &80) 
#       &06f4 &46 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE | TILE_FLIP_VERTICAL 
#       &098d &a6 ; data : fires OBJECT_ICER_BULLET
#       &0a78 &1f ; type : OBJECT_GREEN_WHITE_TURRET
# 
# &07 : &05f6 &af ; x    : (&af, &61) 
#       &06f5 &c6 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL 
#       &098e &ae ; data : fires OBJECT_RED_BULLET
#       &0a79 &1f ; type : OBJECT_GREEN_WHITE_TURRET
# 
# &08 : &05f7 &da ; x    : (&da, &80) 
#       &06f6 &06 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE 
#       &098f &83 ; data : type 3 (magenta / red, sucks OBJECT_CORONIUM_BOULDER with power &40)
#       &0a7a &0d ; type : OBJECT_SUCKING_NEST
# 
# &09 : &05f8 &c6 ; x    : (&c6, &c0) 
#       &06f7 &06 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE 
#       &0990 &86 ; data : type 6 (red / magenta, blows OBJECT_CORONIUM_BOULDER with power &7f)
#       &0a7b &0d ; type : OBJECT_SUCKING_NEST
# 
# &0a : &05f9 &36 ; x    : (&36, &8c) 
#       &06f8 &46 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE | TILE_FLIP_VERTICAL 
#       &0991 &82 ; data : type 2 (cyan / magenta, sucks OBJECT_WASP with power &7f)
#       &0a7c &0d ; type : OBJECT_SUCKING_NEST
# 
# &0b : &05fa &9f ; x    : (&9f, &c0) 
#       &06f9 &05 ; tile : TILE_STONE_HALF_WITH_OBJECT_FROM_TYPE 
#       &0992 &80 ; data
#       &0a7d &0c ; type : OBJECT_DENSE_NEST
# 
# &0c : &05fb &2e ; x    : (&2e, &94) 
#       &06fa &05 ; tile : TILE_STONE_HALF_WITH_OBJECT_FROM_TYPE 
#       &0993 &80 ; data
#       &0a7e &60 ; type : OBJECT_MUSHROOM_IMMUNITY_PILL
# 
# &0d : &05fc &a9 ; x    : (&a9, &9c) 
#       &06fb &00 ; tile : TILE_INVISIBLE_SWITCH 
#       &0994 &ad ; data : switch effects &15, set &02
#       &0a7f &2c ; type : triggered by OBJECT_CYAN_YELLOW_IMP
# 
# &0e : &05fd &9c ; x    : (&9c, &3c) 
#       &06fc &c3 ; tile : TILE_METAL_DOOR | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL (horizontal door)
#       &0995 &01 ; data : door colour 0 (cyg), locked
#                        ; &01 toggled (locked or unlocked) by switch at (&9d, &3b)
#       &0a80 &00 ; type : (unused)
# 
# &0f : &05fe &83 ; x    : (&83, &77) 
#       &06fd &04 ; tile : TILE_STONE_DOOR (horizontal door)
#       &0996 &f7 ; data ; door colour 7 (mwb), locked, open
#                        ; &02 set (opened) by invisible switch at (&87, &77)
#                        ; &02 cleared (closed) by invisible switch at (&7f, &77)
#                        ; &02 cleared (closed) by invisible switch at (&83, &76)
# 
# &10 : &05ff &88 ; x    : (&88, &72) 
#       &06fe &83 ; tile : TILE_METAL_DOOR | TILE_FLIP_HORIZONTAL (vertical door)
#       &0997 &a1 ; data : door colour 2 (gyr), locked
#                        ; &01 toggled (locked or unlocked) by switch at (&46, &56) or switch at (&8b, &71)
# 
# &11 : &0600 &5f ; x    : (&5f, &c0) 
#       &06ff &84 ; tile : TILE_STONE_DOOR | TILE_FLIP_HORIZONTAL (vertical door)
#       &0998 &f1 ; data : door colour 7 (mwb), locked
# 
# &12 : &0601 &57 ; x    : (&57, &94) 
#       &0700 &84 ; tile : TILE_STONE_DOOR | TILE_FLIP_HORIZONTAL (vertical door)
#       &0999 &f7 ; data : door colour 7 (mwb), locked
#                        ; &02 toggled (opened or closed) by switch at (&4d, &80)
# 
# &13 : &0602 &bf ; x    : (&bf, &80) 
#       &0701 &83 ; tile : TILE_METAL_DOOR | TILE_FLIP_HORIZONTAL (vertical door)
#       &099a &81 ; data : door colour 0 (cyg), locked
#                        ; &02 set (opened) by invisible switch at (&c1, &7c)
# 
# &14 : &0603 &9d ; x    : (&9d, &3b) 
#       &0702 &88 ; tile : TILE_SWITCH | TILE_FLIP_HORIZONTAL 
#       &099b &0a ; data : switch effects &01, toggle &01
# 
# &15 : &0604 &4d ; x    : (&4d, &80) 
#       &0703 &48 ; tile : TILE_SWITCH | TILE_FLIP_VERTICAL 
#       &099c &ac ; data ; switch effects &05, toggle &02
# 
# &16 : &0605 &45 ; x    : (&45, &4e) 
#       &0704 &02 ; tile : TILE_SPACE_WITH_OBJECT_FROM_DATA 
#       &099d &d2 ; data : OBJECT_RED_YELLOW_GREEN_KEY
# 
# &17 : &0606 &81 ; x    : (&81, &75) 
#       &0705 &02 ; tile : TILE_SPACE_WITH_OBJECT_FROM_DATA 
#       &099e &df ; data : OBJECT_FIRE_IMMUNITY_DEVICE
# 
# &18 : &0607 &b3 ; x    : (&b3, &80) 
#       &0706 &42 ; tile : TILE_SPACE_WITH_OBJECT_FROM_DATA | TILE_FLIP_VERTICAL 
#       &099f &d4 ; data : OBJECT_YELLOW_WHITE_RED_KEY
# 
# &19 : &0608 &3f ; x    : (&3f, &80) 
#       &0707 &02 ; tile : TILE_SPACE_WITH_OBJECT_FROM_DATA 
#       &09a0 &a3 ; data : OBJECT_CYAN_CLAWED_ROBOT
# 
# &1a : &0609 &cb ; x    : (&cb, &d8) 
#       &0708 &7b ; tile : TILE_STONE_HORIZONTAL_THREE_QUARTERS | TILE_FLIP_VERTICAL 
# 
# &1b : &060a &40 ; x    : (&40, &4e) 
#       &0709 &22 ; tile : TILE_LEAF
# 
# &1c : &060b &4c ; x    : (&4c, &80) 
#       &070a &1e ; tile : TILE_STONE_TWO 
# 
# TILE_CHECK_TERTIARY_OBJECT_RANGE_ONE
# 
# &1d : &060c &ca ; x    : (&ca, &58), (&ca, &d8) 
#       &070b &06 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE 
#       &09a1 &84 ; data : type 4 (yellow / white, sucks OBJECT_PIRANHA with power &50)
#       &0a81 &0d ; type : OBJECT_SUCKING_NEST
# 
# &1e : &060d &2f ; x    : (&2f, &94) 
#       &070c &06 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE 
#       &09a2 &85 ; data : type 5 (yellow / green, blows all objects with power &7f)
#       &0a82 &0d ; type : OBJECT_SUCKING_NEST
# 
# &1f : &060e &a7 ; x    : (&a7, &80) 
#       &070d &c6 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL 
#       &09a3 &ae ; data : fires OBJECT_RED_BULLET
#       &0a83 &1f ; type : OBJECT_GREEN_WHITE_TURRET
# 
# &20 : &060f &56 ; x    : (&56, &94) 
#       &070e &06 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE 
#       &09a4 &80 ; data : type 0 (cyan / green, sucks all objects with power &50)
#       &0a84 &0d ; type : OBJECT_SUCKING_NEST
# 
# &21 : &0610 &34 ; x    : (&34, &8c) 
#       &070f &46 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE | TILE_FLIP_VERTICAL 
#       &09a5 &80 ; data
#       &0a85 &5c ; type : OBJECT_BLASTER
# 
# &22 : &0611 &e3 ; x    : (&e3, &98) 
#       &0710 &46 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE | TILE_FLIP_VERTICAL 
#       &09a6 &88 ; data : type 8 (green / green, sucks OBJECT_WORM with power &40)
#       &0a86 &0d ; type : OBJECT_SUCKING_NEST
# 
# &23 : &0612 &3b ; x    : (&3b, &c0) 
#       &0711 &85 ; tile : TILE_STONE_HALF_WITH_OBJECT_FROM_TYPE | TILE_FLIP_HORIZONTAL 
#       &09a7 &ac ; data : fires OBJECT_BLUE_DEATH_BALL
#       &0a87 &20 ; type : OBJECT_CYAN_RED_TURRET
# 
# &24 : &0613 &e4 ; x    : (&e4, &80) 
#       &0712 &85 ; tile : TILE_STONE_HALF_WITH_OBJECT_FROM_TYPE | TILE_FLIP_HORIZONTAL (less aggressive)
#       &09a8 &c4 ; data : spawns OBJECT_WASP
#       &0a88 &05 ; type : OBJECT_LARGE_HIVE
# 
# &25 : &0614 &80 ; x    : (&80, &c5) 
#       &0713 &87 ; tile : TILE_GREENERY_WITH_OBJECT_FROM_TYPE | TILE_FLIP_HORIZONTAL (less aggressive)
#       &09a9 &c0 ; data : spawns OBJECT_PIRANHA
#       &0a89 &04 ; type : OBJECT_SMALL_HIVE
# 
# &26 : &0615 &e0 ; x    : (&e0, &98) 
#       &0714 &89 ; tile : TILE_NEST | TILE_FLIP_HORIZONTAL (spawns immediately)
#       &09aa &04 ; data : 1 creature
#       &0a8a &06 ; type : OBJECT_RED_FROGMAN
# 
# &27 : &0616 &64 ; x    : (&64, &80) 
#       &0715 &89 ; tile : TILE_NEST | TILE_FLIP_HORIZONTAL (spawns immediately)
#       &09ab &a8 ; data : 10 creatures, with bush
#       &0a8b &31 ; type : OBJECT_INVISIBLE_BIRD
# 
# &28 : &0617 &37 ; x    : (&37, &8c) 
#       &0716 &c7 ; tile : TILE_GREENERY_WITH_OBJECT_FROM_TYPE | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL (less aggressive)
#       &09ac &c4 ; data : spawns OBJECT_WASP
#       &0a8c &05 ; type : OBJECT_LARGE_HIVE
# 
# &29 : &0618 &47 ; x    : (&47, &c0) 
#       &0717 &0a ; tile : TILE_PIPE 
#       &09ad &bc ; data : 15 creatures, with bush
#       &0a8d &2a ; type : OBJECT_RED_YELLOW_IMP
# 
# &2a : &0619 &9f ; x    : (&9f, &3a) 
#       &0718 &8c ; tile : TILE_ENGINE | TILE_FLIP_HORIZONTAL 
#       &09ae &7d ; data ; inactive
#                        ; &01 cleared (activated) by invisible switch at (&9b, &3b)
# 
# &2b : &061a &9c ; x    : (&9c, &3d) 
#       &0719 &03 ; tile : TILE_METAL_DOOR (horizontal door)
#       &09af &01 ; data : door colour 0 (cyg), locked
#                        ; &01 toggled (locked or unlocked) by switch at (&9d, &3b)
# 
# &2c : &061b &aa ; x    : (&aa, &98) 
#       &071a &84 ; tile : TILE_STONE_DOOR | TILE_FLIP_HORIZONTAL (vertical door)
#       &09b0 &c1 ; data : door colour 4 (rmb), locked
#                        ; &02 set (opened) by invisible switch at (&a9, &9c)
# 
# &2d : &061c &9b ; x    : (&9b, &80) 
#       &071b &83 ; tile : TILE_METAL_DOOR | TILE_FLIP_HORIZONTAL (vertical door)
#       &09b1 &d1 ; data : door colour 5 (rmr), locked
# 
# &2e : &061d &9a ; x    : (&9a, &5c) 
#       &071c &43 ; tile : TILE_METAL_DOOR | TILE_FLIP_VERTICAL (vertical door)
#       &09b2 &91 ; data : door colour 1 (ryg), locked
# 
# &2f : &061e &5e ; x    : (&5e, &c0) 
#       &071d &84 ; tile : TILE_STONE_DOOR | TILE_FLIP_HORIZONTAL (vertical door)
#       &09b3 &f1 ; data : door colour 7 (mwb), locked
# 
# &30 : &061f &c7 ; x    : (&c7, &c0) 
#       &071e &84 ; tile : TILE_STONE_DOOR | TILE_FLIP_HORIZONTAL (vertical door)
#       &09b4 &f1 ; data : door colour 7 (mwb), locked
# 
# &31 : &0620 &8a ; x    : (&8a, &71) 
#       &071f &02 ; tile : TILE_SPACE_WITH_OBJECT_FROM_DATA 
#       &09b5 &da ; data : OBJECT_PISTOL
# 
# &32 : &0621 &60 ; x    : (&60, &98) 
#       &0720 &01 ; tile : TILE_TRANSPORTER 
#       &09b6 &f7 ; data : destination &b, inactive, uses key 6 (OBJECT_BLUE_CYAN_GREEN_KEY)
# 
# &33 : &0622 &9d ; x    : (&9d, &49) 
#       &0721 &41 ; tile : TILE_TRANSPORTER | TILE_FLIP_VERTICAL 
#       &09b7 &f3 ; data : destination &9, inactive, uses key 6 (OBJECT_BLUE_CYAN_GREEN_KEY)
# 
# &34 : &0623 &a2 ; x    : (&a2, &58) 
#       &0722 &01 ; tile : TILE_TRANSPORTER 
#       &09b8 &d8 ; data : destination &c, uses key 5 (OBJECT_RED_MAGENTA_RED_KEY)
#                        ; &02 toggled (destination toggled between &c and &d) by switch at (&a1, &58)
# 
# &35 : &0624 &b2 ; x    : (&b2, &80) 
#       &0723 &01 ; tile : TILE_TRANSPORTER 
#       &09b9 &88 ; data : destination &4, uses key 3 (OBJECT_YELLOW_WHITE_RED_KEY)
# 
# &36 : &0625 &98 ; x    : (&98, &80) 
#       &0724 &2e ; tile : TILE_EARTH_SLOPE_FORTY_FIVE
# 
# &37 : &0626 &a9 ; x    : (&a9, &80) 
#       &0725 &1e ; tile : TILE_STONE_TWO 
# 
# &38 : &0627 &db ; x    : (&db, &80) 
#       &0726 &3b ; tile : TILE_STONE_HORIZONTAL_THREE_QUARTERS 
# 
# TILE_CHECK_TERTIARY_OBJECT_RANGE_TWO
# 
# &39 : &0628 &28 ; x    : (&28, &98) 
#       &0727 &46 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE | TILE_FLIP_VERTICAL 
#       &09ba &80 ; data
#       &0a8e &09 ; type : OBJECT_RED_SLIME
# 
# &3a : &0629 &29 ; x    : (&29, &98) 
#       &0728 &46 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE | TILE_FLIP_VERTICAL 
#       &09bb &83 ; data : type 3 (magenta / red, sucks OBJECT_CORONIUM_BOULDER with power &40)
#       &0a8f &0d ; type : OBJECT_SUCKING_NEST
# 
# &3b : &062a &3c ; x    : (&3c, &80) 
#       &0729 &06 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE 
#       &09bc &83 ; data : type 3 (magenta / red, sucks OBJECT_CORONIUM_BOULDER with power &40)
#       &0a90 &0d ; type : OBJECT_SUCKING_NEST
# 
# &3c : &062b &98 ; x    : (&98, &4e) 
#       &072a &c6 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL 
#       &09bd &b0 ; data : fires OBJECT_PISTOL_BULLET
#       &0a91 &1f ; type : OBJECT_GREEN_WHITE_TURRET
# 
# &3d : &062c &63 ; x    : (&63, &c0) 
#       &072b &06 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE 
#       &09be &aa ; data : fires OBJECT_CANNONBALL
#       &0a92 &20 ; type : OBJECT_CYAN_RED_TURRET
# 
# &3e : &062d &cb ; x    : (&cb, &dc) 
#       &072c &06 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE 
#       &09bf &80 ; data
#       &0a93 &55 ; type : OBJECT_CORONIUM_BOULDER
# 
# &3f : &062e &61 ; x    : (&61, &c6) 
#       &072d &06 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE 
#       &09c0 &80 ; data
#       &0a94 &55 ; type : OBJECT_CORONIUM_BOULDER
# 
# &40 : &062f &a3 ; x    : (&a3, &c0) 
#       &072e &46 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE | TILE_FLIP_VERTICAL 
#       &09c1 &87 ; data : type 7 (yellow / cyan, blows OBJECT_PIRANHA with power &50)
#       &0a95 &0d ; type : OBJECT_SUCKING_NEST
# 
# &41 : &0630 &ce ; x    : (&ce, &d8) 
#       &072f &06 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE 
#       &09c2 &80 ; data
#       &0a96 &63 ; type : OBJECT_RADIATION_IMMUNITY_PILL
# 
# &42 : &0631 &e9 ; x    : (&e9, &98) 
#       &0730 &09 ; tile : TILE_NEST 
#       &09c3 &30 ; data : 12 creatures
#       &0a97 &0f ; type : OBJECT_WORM
# 
# &43 : &0632 &80 ; x    : (&80, &88) 
#       &0731 &c9 ; tile : TILE_NEST | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL (spawns immediately)
#       &09c4 &08 ; data : 2 creatures
#       &0a98 &2e ; type : OBJECT_GREEN_YELLOW_BIRD
# 
# &44 : &0633 &2e ; x    : (&2e, &98) 
#       &0732 &89 ; tile : TILE_NEST | TILE_FLIP_HORIZONTAL (spawns immediately)
#       &09c5 &10 ; data : 4 creatures
#       &0a99 &0a ; type : OBJECT_GREEN_SLIME
# 
# &45 : &0634 &4f ; x    : (&4f, &80) 
#       &0733 &ca ; tile : TILE_PIPE | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL (spawns immediately)
#       &09c6 &7c ; data : 31 creatures
#       &0a9a &1b ; type : OBJECT_INVISIBLE_HOVERING_BALL  
# 
# &46 : &0635 &79 ; x    : (&79, &76) 
#       &0734 &8a ; tile : TILE_PIPE | TILE_FLIP_HORIZONTAL (spawns immediately)
#       &09c7 &04 ; data : 1 creature
#       &0a9b &37 ; type : OBJECT_FIREBALL
# 
# &47 : &0636 &87 ; x    : (&87, &bf) 
#       &0735 &8a ; tile : TILE_PIPE | TILE_FLIP_HORIZONTAL (spawns immediately)
#       &09c8 &10 ; data : 4 creatures
#       &0a9c &29 ; type : OBJECT_RED_MAGENTA_IMP
# 
# &48 : &0637 &b6 ; x    : (&b6, &80) 
#       &0736 &0a ; tile : TILE_PIPE 
#       &09c9 &a8 ; data : 10 creatures, with bush
#       &0a9d &1a ; type : OBJECT_HOVERING_BALL
# 
# &49 : &0638 &97 ; x    : (&97, &5c) 
#       &0737 &4a ; tile : TILE_PIPE | TILE_FLIP_VERTICAL 
#       &09ca &90 ; data : 4 creatures, with bush
#       &0a9e &1a ; type : OBJECT_HOVERING_BALL
# 
# &4a : &0639 &2d ; x    : (&2d, &c7) 
#       &0738 &8a ; tile : TILE_PIPE | TILE_FLIP_HORIZONTAL (spawns immediately)
#       &09cb &04 ; data : 1 creature
#       &0a9f &37 ; type : OBJECT_FIREBALL
# 
# &4b : &063a &d6 ; x    : (&d6, &72) 
#       &0739 &04 ; tile : TILE_STONE_DOOR (horizontal door)
#       &09cc &c1 ; data : door colour 4 (rmb), locked
#                        ; &01 toggled (locked or unlocked) by switch at (&d5, &73)
#                        ; &02 toggled (opened or closed) by switch at (&d4, &6f)
# 
# &4c : &063b &5c ; x    : (&5c, &b8), (&5c, &c0)
#       &073a &44 ; tile : TILE_STONE_DOOR | TILE_FLIP_VERTICAL (vertical door)
#       &09cd &f1 ; data : door colour 7 (mwb), locked
# 
# &4d : &063c &a0 ; x    : (&a0, &63) 
#       &073b &41 ; tile : TILE_TRANSPORTER | TILE_FLIP_VERTICAL 
#       &09ce &e1 ; data : destination &0, inactive, uses key 6 (OBJECT_BLUE_CYAN_GREEN_KEY)
# 
# &4e : &063d &74 ; x    : (&74, &54) 
#       &073c &01 ; tile : TILE_TRANSPORTER 
#       &09cf &95 ; data : destination &a, inactive, uses key 3 (OBJECT_YELLOW_WHITE_RED_KEY)
# 
# &4f : &063e &6a ; x    : (&6a, &de) 
#       &073d &c8 ; tile : TILE_SWITCH | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL 
#       &09d0 &bc ; data : switch effects &07, toggle &02
# 
# &50 : &063f &a1 ; x    : (&a1, &58) 
#       &073e &48 ; tile : TILE_SWITCH | TILE_FLIP_VERTICAL 
#       &09d1 &b4 ; data : switch effects &06, toggle &02
# 
# &51 : &0640 &9f ; x    : (&9f, &3b) 
#       &073f &cc ; tile : TILE_ENGINE | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL 
#       &09d2 &7d ; data ; inactive
#                        ; &01 cleared (activated) by invisible switch at (&9b, &3b)
# 
# &52 : &0641 &89 ; x    : (&89, &72) 
#       &0740 &82 ; tile : TILE_SPACE_WITH_OBJECT_FROM_DATA | TILE_FLIP_HORIZONTAL 
#       &09d3 &a1 ; data : OBJECT_HOVERING_ROBOT
# 
# &53 : &0642 &85 ; x    : (&85, &bf) 
#       &0741 &02 ; tile : TILE_SPACE_WITH_OBJECT_FROM_DATA 
#       &09d4 &d6 ; data : OBJECT_RED_MAGENTA_RED_KEY
# 
# &54 : &0643 &6b ; x    : (&6b, &88) 
#       &0742 &02 ; tile : TILE_SPACE_WITH_OBJECT_FROM_DATA 
#       &09d5 &dd ; data : OBJECT_PLASMA_GUN
# 
# &55 : &0644 &ae ; x    : (&ae, &98) 
#       &0743 &02 ; tile : TILE_SPACE_WITH_OBJECT_FROM_DATA 
#       &09d6 &e2 ; data : OBJECT_WHISTLE_TWO
# 
# &56 : &0645 &65 ; x    : (&65, &b4) 
#       &0744 &1e ; tile : TILE_STONE_TWO 
# 
# TILE_CHECK_TERTIARY_OBJECT_RANGE_THREE
# 
# &57 : &0646 &e2 ; x    : (&e2, &c0) 
#       &0745 &89 ; tile : TILE_NEST | TILE_FLIP_HORIZONTAL (spawns immediately)
#       &09d7 &04 ; data : 1 creature
#       &0aa0 &37 ; type : OBJECT_FIREBALL
# 
# &58 : &0647 &ed ; x    : (&ed, &bc) 
#       &0746 &09 ; tile : TILE_NEST 
#       &09d8 &0c ; data : 3 creatures
#       &0aa1 &0a ; type : OBJECT_GREEN_SLIME
# 
# &59 : &0648 &80 ; x    : (&80, &54) 
#       &0747 &0a ; tile : TILE_PIPE 
#       &09d9 &04 ; data : 1 creature
#       &0aa2 &37 ; type : OBJECT_FIREBALL
# 
# &5a : &0649 &cd ; x    : (&cd, &7c) 
#       &0748 &4a ; tile : TILE_PIPE | TILE_FLIP_VERTICAL 
#       &09da &20 ; data : 8 creatures
#       &0aa3 &4b ; type : OBJECT_POWER_POD
# 
# &5b : &064a &a8 ; x    : (&a8, &68) 
#       &0749 &ca ; tile : TILE_PIPE | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL (spawns immediately)
#       &09db &21 ; data : 8 creatures, inactive
#                        ; &01 toggled (activated or deactivated) by switch at (&ab, &6b)
#                        ; &01 set (deactivated) by invisible switch at (&a8, &69)
#       &0aa4 &4b ; type : OBJECT_POWER_POD
# 
# &5c : &064b &2b ; x    : (&2b, &80) 
#       &074a &4a ; tile : TILE_PIPE | TILE_FLIP_VERTICAL 
#       &09dc &a0 ; data : 8 creatures, with bush
#       &0aa5 &2d ; type : OBJECT_RED_CYAN_IMP
# 
# &5d : &064c &ab ; x    : (&ab, &80) 
#       &074b &86 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE | TILE_FLIP_HORIZONTAL 
#       &09dd &b0 ; data : fires OBJECT_PISTOL_BULLET
#       &0aa6 &1f ; type : OBJECT_GREEN_WHITE_TURRET
# 
# &5e : &064d &9d ; x    : (&9d, &6f) 
#       &074c &46 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE | TILE_FLIP_VERTICAL 
#       &09de &ac ; data : fires OBJECT_BLUE_DEATH_BALL
#                        ; &01 toggled (activated or deactivated) by switch at (&ab, &6b)
#       &0aa7 &20 ; type : OBJECT_CYAN_RED_TURRET
# 
# &5f : &064e &62 ; x    : (&62, &c0) 
#       &074d &46 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE | TILE_FLIP_VERTICAL 
#       &09df &83 ; data : type 3 (magenta / red, sucks OBJECT_CORONIUM_BOULDER with power &40)
#       &0aa8 &0d ; type : OBJECT_SUCKING_NEST
# 
# &60 : &064f &e5 ; x    : (&e5, &bc) 
#       &074e &46 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE | TILE_FLIP_VERTICAL 
#       &09e0 &81 ; data : type 1 (blue / magenta, blows OBJECT_HORIZONTAL_STONE_DOOR with power &30)
#       &0aa9 &0d ; type : OBJECT_SUCKING_NEST
# 
# &61 : &0650 &70 ; x    : (&70, &88) 
#       &074f &86 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE | TILE_FLIP_HORIZONTAL 
#       &09e1 &84 ; data ; type 4 (fires every 4 frames, OBJECT_LIGHTNING)
#       &0aaa &28 ; type : OBJECT_GARGOYLE
# 
# &62 : &0651 &ec ; x    : (&ec, &bc) 
#       &0750 &45 ; tile : TILE_STONE_HALF_WITH_OBJECT_FROM_TYPE | TILE_FLIP_VERTICAL 
#       &09e2 &80 ; data
#       &0aab &55 ; type : OBJECT_CORONIUM_BOULDER
# 
# &63 : &0652 &83 ; x    : (&83, &5c) 
#       &0751 &47 ; tile : TILE_GREENERY_WITH_OBJECT_FROM_TYPE | TILE_FLIP_VERTICAL (more aggressive)
#       &09e3 &c4 ; data : spawns OBJECT_WASP
#       &0aac &05 ; type : OBJECT_LARGE_HIVE
# 
# &64 : &0653 &c1 ; x    : (&c1, &7c) 
#       &0752 &00 ; tile : TILE_INVISIBLE_SWITCH 
#       &09e4 &85 ; data : switch effects &10, set &02
#       &0aad &80 ; type : triggered by any object
# 
# &65 : &0654 &c6 ; x    : (&c6, &7c) 
#       &0753 &00 ; tile : TILE_INVISIBLE_SWITCH 
#       &09e5 &95 ; data : switch effects &12, set &02
#       &0aae &00 ; type : triggered by OBJECT_PLAYER
# 
# &66 : &0655 &67 ; x    : (&67, &da) 
#       &0754 &00 ; tile : TILE_INVISIBLE_SWITCH 
#       &09e6 &a3 ; data : switch effects &14, set &01
#       &0aaf &80 ; type : triggered by any object
# 
# &67 : &0656 &eb ; x    : (&eb, &bc) 
#       &0755 &00 ; tile : TILE_INVISIBLE_SWITCH 
#       &09e7 &b5 ; data : switch effects &16, set &02
#       &0ab0 &80 ; type : triggered by any object
# 
# &68 : &0657 &2d ; x    : (&2d, &94) 
#       &0756 &84 ; tile : TILE_STONE_DOOR | TILE_FLIP_HORIZONTAL (vertical door)
#       &09e8 &f1 ; data : door colour 7 (mwb), locked
# 
# &69 : &0658 &98 ; x    : (&98, &54) 
#       &0757 &c3 ; tile : TILE_METAL_DOOR | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL (horizontal door)
#       &09e9 &ad ; data : door colour 2 (gyr), locked, slow
# 
# &6a : &0659 &aa ; x    : (&aa, &9c) 
#       &0758 &44 ; tile : TILE_STONE_DOOR | TILE_FLIP_VERTICAL (vertical door)
#       &09ea &c1 ; data : door colour 4 (rmb), locked
#                        ; &02 set (opened) by invisible switch at (&a9, &9c)
# 
# &6b : &065a &cc ; x    : (&cc, &7c) 
#       &0759 &43 ; tile : TILE_METAL_DOOR | TILE_FLIP_VERTICAL (vertical door)
#       &09eb &81 ; data : door colour 0 (cyg), locked
#                        ; &02 set (opened) by invisible switch at (&c6, &7c)
# 
# &6c : &065b &a5 ; x    : (&a5, &80) 
#       &075a &43 ; tile : TILE_METAL_DOOR | TILE_FLIP_VERTICAL (vertical door)
#       &09ec &89 ; data : door colour 0 (cyg), locked, slow
# 
# &6d : &065c &9e ; x    : (&9e, &6b) 
#       &075b &43 ; tile : TILE_METAL_DOOR | TILE_FLIP_VERTICAL (vertical door)
#       &09ed &a0 ; data : door colour 2 (gyr), unlocked
# 
# &6e : &065d &a2 ; x    : (&a2, &c0) 
#       &075c &44 ; tile : TILE_STONE_DOOR | TILE_FLIP_VERTICAL (vertical door)
#       &09ee &c1 ; data : door colour 4 (rmb), locked
#                        ; &01 toggled (locked or unlocked) by switch at (&c4, &c4)
# 
# &6f : &065e &d7 ; x    : (&d7, &c0) 
#       &075d &84 ; tile : TILE_STONE_DOOR | TILE_FLIP_HORIZONTAL (vertical door)
#       &09ef &f1 ; data : door colour 7 (mwb), locked
# 
# &70 : &065f &e6 ; x    : (&e6, &bc) 
#       &075e &44 ; tile : TILE_STONE_DOOR | TILE_FLIP_VERTICAL (vertical door)
#       &09f0 &f1 ; data : door colour 7 (mwb), locked
#                        ; &02 toggled (opened or closed) by switch at (&e3, &9c) or (&e3, &bc)
# 
# &71 : &0660 &e7 ; x    : (&e7, &bc) 
#       &075f &04 ; tile : TILE_STONE_DOOR (horizontal door)
#       &09f1 &c1 ; data : door colour 4 (rmb), locked
#                        ; &02 set (opened) by invisible switch at (&eb, &bc)
# 
# &72 : &0661 &94 ; x    : (&94, &5c) 
#       &0760 &01 ; tile : TILE_TRANSPORTER 
#       &09f2 &8c ; data : destination &6, uses key 3 (OBJECT_YELLOW_WHITE_RED_KEY)
# 
# &73 : &0662 &7c ; x    : (&7c, &c0) 
#       &0761 &08 ; tile : TILE_SWITCH 
#       &09f3 &a4 ; data : switch effects &04, toggle &02
# 
# &74 : &0663 &e3 ; x    : (&e3, &9c), (&e3, &bc) 
#       &0762 &08 ; tile : TILE_SWITCH 
#       &09f4 &e4 ; data : switch effects &0c, toggle &02
# 
# &75 : &0664 &45 ; x    : (&45, &c0) 
#       &0763 &02 ; tile : TILE_SPACE_WITH_OBJECT_FROM_DATA 
#       &09f5 &d7 ; data : OBJECT_BLUE_CYAN_GREEN_KEY
# 
# &76 : &0665 &9b ; x    : (&9b, &66) 
#       &0764 &02 ; tile : TILE_SPACE_WITH_OBJECT_FROM_DATA 
#       &09f6 &9d ; data : OBJECT_RED_ROLLING_ROBOT
# 
# &77 : &0666 &9f ; x    : (&9f, &73) 
#       &0765 &82 ; tile : TILE_SPACE_WITH_OBJECT_FROM_DATA | TILE_FLIP_HORIZONTAL 
#       &09f7 &e1 ; data : OBJECT_WHISTLE_ONE
# 
# &78 : &0667 &c2 ; x    : (&c2, &7c), (&c2, &80) 
#       &0766 &1e ; tile : TILE_STONE_TWO 
# 
# &79 : &0668 &71 ; x    : (&71, &88) 
#       &0767 &2d ; tile : TILE_EARTH 
# 
# TILE_CHECK_TERTIARY_OBJECT_RANGE_FOUR
# 
# &7a : &0669 &67 ; x    : (&67, &c8) 
#       &0768 &46 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE | TILE_FLIP_VERTICAL 
#       &09f8 &a6 ; data : fires OBJECT_ICER_BULLET
#       &0ab1 &20 ; type : OBJECT_CYAN_RED_TURRET
# 
# &7b : &066a &4f ; x    : (&4f, &68), (&4f, &b8)
#       &0769 &46 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE | TILE_FLIP_VERTICAL 
#       &09f9 &81 ; data : type 1 (fires every 8 frames, OBJECT_PLASMA_BALL)
#       &0ab2 &28 ; type : OBJECT_GARGOYLE
# 
# &7c : &066b &cf ; x    : (&cf, &b8) 
#       &076a &45 ; tile : TILE_STONE_HALF_WITH_OBJECT_FROM_TYPE | TILE_FLIP_VERTICAL 
#       &09fa &85 ; data : type 5 (yellow / green, blows all objects with power &7f)
#       &0ab3 &0d ; type : OBJECT_SUCKING_NEST
# 
# &7d : &066c &d2 ; x    : (&d2, &9d) 
#       &076b &46 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE | TILE_FLIP_VERTICAL 
#       &09fb &83 ; data : type 3 (magenta / red, sucks OBJECT_CORONIUM_BOULDER with power &40)
#       &0ab4 &0d ; type : OBJECT_SUCKING_NEST
# 
# &7e : &066d &e2 ; x    : (&e2, &a2), (&e2, &b8) 
#       &076c &c6 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL 
#       &09fc &83 ; data : type 3 (fires every 8 frames, OBJECT_PLASMA_BALL)
#       &0ab5 &28 ; type : OBJECT_GARGOYLE
# 
# &7f : &066e &7a ; x    : (&7a, &94), (&7a, &a8) 
#       &076d &89 ; tile : TILE_NEST | TILE_FLIP_HORIZONTAL (spawns immediately)
#       &09fd &d0 ; data ; 20 creatures, with bush
#       &0ab6 &27 ; type : OBJECT_MAGGOT
# 
# &80 : &066f &62 ; x    : (&62, &72) 
#       &076e &c9 ; tile : TILE_NEST | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL (spawns immediately)
#       &09fe &a8 ; data : 10 creatures, with bush
#       &0ab7 &31 ; type : OBJECT_INVISIBLE_BIRD
# 
# &81 : &0670 &da ; x    : (&da, &d8) 
#       &076f &49 ; tile : TILE_NEST | TILE_FLIP_VERTICAL 
#       &09ff &04 ; data : 1 creature
#       &0ab8 &0e ; type : OBJECT_BIG_FISH
# 
# &82 : &0671 &76 ; x    : (&76, &94), (&76, &a8) 
#       &0770 &89 ; tile : TILE_NEST | TILE_FLIP_HORIZONTAL (spawns immediately)
#       &0a00 &04 ; data ; 1 creature
#       &0ab9 &08 ; type : OBJECT_INVISIBLE_FROGMAN
# 
# &83 : &0672 &b2 ; x    : (&b2, &8d) 
#       &0771 &ca ; tile : TILE_PIPE | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL (spawns immediately)
#       &0a01 &d0 ; data : 20 creatures, with bush
#       &0aba &11 ; type : OBJECT_WASP
# 
# &84 : &0673 &66 ; x    : (&66, &66) 
#       &0772 &ca ; tile : TILE_PIPE | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL (spawns immediately)
#       &0a02 &88 ; data : 2 creatures, with bush
#       &0abb &39 ; type : OBJECT_MOVING_FIREBALL
# 
# &85 : &0674 &d7 ; x    : (&d7, &6e) 
#       &0773 &8a ; tile : TILE_PIPE | TILE_FLIP_HORIZONTAL (spawns immediately)
#       &0a03 &04 ; data : 1 creature
#       &0abc &37 ; type : OBJECT_FIREBALL
# 
# &86 : &0675 &83 ; x    : (&83, &78) 
#       &0774 &8a ; tile : TILE_PIPE | TILE_FLIP_HORIZONTAL (spawns immediately)
#       &0a04 &04 ; data : 1 creature
#       &0abd &37 ; type : OBJECT_FIREBALL
# 
# &87 : &0676 &84 ; x    : (&84, &6c) 
#       &0775 &8a ; tile : TILE_PIPE | TILE_FLIP_HORIZONTAL (spawns immediately)
#       &0a05 &04 ; data : 1 creature
#       &0abe &37 ; type : OBJECT_FIREBALL
# 
# &88 : &0677 &80 ; x    : (&80, &75) 
#       &0776 &0a ; tile : TILE_PIPE 
#       &0a06 &08 ; data : 2 creatures
#       &0abf &2a ; type : OBJECT_RED_YELLOW_IMP
# 
# &89 : &0678 &87 ; x    : (&87, &77) 
#       &0777 &00 ; tile : TILE_INVISIBLE_SWITCH 
#       &0a07 &bd ; data : switch effects &17, set &02
#       &0ac0 &80 ; type : triggered by any object
# 
# &8a : &0679 &9b ; x    : (&9b, &3b) 
#       &0778 &00 ; tile : TILE_INVISIBLE_SWITCH 
#       &0a08 &8a ; data : switch effects &11, clear &01
#       &0ac1 &4a ; type : triggered by OBJECT_DESTINATOR
# 
# &8b : &067a &50 ; x    : (&50, &60) 
#       &0779 &c4 ; tile : TILE_STONE_DOOR | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL (horizontal door)
#       &0a09 &f1 ; data : door colour 7 (mwb), locked
# 
# &8c : &067b &ae ; x    : (&ae, &62) 
#       &077a &43 ; tile : TILE_METAL_DOOR | TILE_FLIP_VERTICAL (vertical door)
#       &0a0a &d1 ; data : door colour 5 (rmr), locked
#                        ; &02 toggled (opened or closed) by switch at (&d5, &73)
# 
# &8d : &067c &64 ; x    : (&64, &c8) 
#       &077b &04 ; tile : TILE_STONE_DOOR (horizontal door)
#       &0a0b &f1 ; data : door colour 7 (mwb), locked
#                        ; &02 toggled (opened or closed) by switch at (&67, &cb)
# 
# &8e : &067d &a3 ; x    : (&a3, &69) 
#       &077c &43 ; tile : TILE_METAL_DOOR | TILE_FLIP_VERTICAL (vertical door)
#       &0a0c &b1 ; data : door colour 3 (ywr), locked
# 
# &8f : &067e &63 ; x    : (&63, &cc) 
#       &077d &84 ; tile : TILE_STONE_DOOR | TILE_FLIP_HORIZONTAL (vertical door)
#       &0a0d &f1 ; data : door colour 7 (mwb), locked
# 
# &90 : &067f &b8 ; x    : (&b8, &c5) 
#       &077e &44 ; tile : TILE_STONE_DOOR | TILE_FLIP_VERTICAL (vertical door)
#       &0a0e &c1 ; data : door colour 4 (rmb), locked
#                        ; &02 toggled (opened or closed) by switch at (&b8, &c3)
#                        ; &02 set (opened) by invisible switch at (&b4, &c2)
# 
# &91 : &0680 &7f ; x    : (&7f, &94), (&7f, &c3) 
#       &077f &04 ; tile : TILE_STONE_DOOR (horizontal door)
#       &0a0f &c1 ; data : door colour 4 (rmb), locked
#                        ; &02 set (opened) by invisible switch at (&80, &c2)
# 
# &92 : &0681 &82 ; x    : (&82, &c3) 
#       &0780 &44 ; tile : TILE_STONE_DOOR | TILE_FLIP_VERTICAL (vertical door)
#       &0a10 &c1 ; data : door colour 4 (rmb), locked
#                        ; &02 toggled (opened or closed) by switch at (&7c, &c0)
# 
# &93 : &0682 &e0 ; x    : (&e0, &b8) 
#       &0781 &84 ; tile : TILE_STONE_DOOR | TILE_FLIP_HORIZONTAL (vertical door)
#       &0a11 &c1 ; data : door colour 4 (rmb), locked
#                        ; &02 toggled (opened or closed) by switch at (&e3, &9c) or (&e3, &bc)
# 
# &94 : &0683 &9c ; x    : (&9c, &66) 
#       &0782 &41 ; tile : TILE_TRANSPORTER | TILE_FLIP_VERTICAL 
#       &0a12 &e2 ; data : destination &1, uses key 6 (OBJECT_BLUE_CYAN_GREEN_KEY)
# 
# &95 : &0684 &61 ; x    : (&61, &d9) 
#       &0783 &01 ; tile : TILE_TRANSPORTER 
#       &0a13 &e4 ; data : destination &2, uses key 6 (OBJECT_BLUE_CYAN_GREEN_KEY)
#                        ; &01 set (deactivated) by invisible switch at (&67, &da)
# 
# &96 : &0685 &9d ; x    : (&9d, &58) 
#       &0784 &01 ; tile : TILE_TRANSPORTER 
#       &0a14 &dc ; data : destination &e, uses key 5 (OBJECT_RED_MAGENTA_RED_KEY)
#                        ; &02 toggled (destination toggled between &e and &f) by switch at (&a1, &58)
# 
# &97 : &0686 &29 ; x    : (&29, &c6) 
#       &0785 &01 ; tile : TILE_TRANSPORTER 
#       &0a15 &a0 ; data : destination &0, uses key 4 (not collectable)
#                        ; &03 toggled (destination toggled between &0 and &1, activated or deactivated) by switch at (&29, &c8)
# 
# &98 : &0687 &46 ; x    : (&46, &56) 
#       &0786 &c8 ; tile : TILE_SWITCH | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL 
#       &0a16 &c2 ; data : switch effects &08, toggle &01
# 
# &99 : &0688 &9f ; x    : (&9f, &6b) 
#       &0787 &42 ; tile : TILE_SPACE_WITH_OBJECT_FROM_DATA | TILE_FLIP_VERTICAL 
#       &0a17 &cb ; data : OBJECT_POWER_POD
# 
# &9a : &0689 &9a ; x    : (&9a, &66) 
#       &0788 &82 ; tile : TILE_SPACE_WITH_OBJECT_FROM_DATA | TILE_FLIP_HORIZONTAL 
#       &0a18 &b8 ; data : OBJECT_INACTIVE_CHATTER
# 
# &9b : &068a &74 ; x    : (&74, &94) 
#       &0789 &3b ; tile : TILE_STONE_HORIZONTAL_THREE_QUARTERS 
# 
# &9c : &068b &75 ; x    : (&75, &94), (&75, &a8)
#       &078a &11 ; tile : TILE_POSSIBLE_LEAF 
# 
# &9d : &068c &77 ; x    : (&77, &94) 
#       &078b &3b ; tile : TILE_STONE_HORIZONTAL_THREE_QUARTERS 
# 
# TILE_CHECK_TERTIARY_OBJECT_RANGE_FIVE
# 
# &9e : &068d &b2 ; x    : (&b2, &c2) 
#       &078c &89 ; tile : TILE_NEST | TILE_FLIP_HORIZONTAL (spawns immediately)
#       &0a19 &a8 ; data : 10 creatures, with bush
#       &0ac2 &10 ; type : OBJECT_PIRANHA
# 
# &9f : &068e &e4 ; x    : (&e4, &b4) 
#       &078d &c9 ; tile : TILE_NEST | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL (spawns immediately)
#       &0a1a &10 ; data : 4 creatures
#       &0ac3 &2f ; type : OBJECT_WHITE_YELLOW_BIRD
# 
# &a0 : &068f &62 ; x    : (&62, &a2) 
#       &078e &ca ; tile : TILE_PIPE | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL (spawns immediately)
#       &0a1b &98 ; data : 6 creatures, with bush
#       &0ac4 &30 ; type : OBJECT_RED_MAGENTA_BIRD
# 
# &a1 : &0690 &63 ; x    : (&63, &b5) 
#       &078f &8a ; tile : TILE_PIPE | TILE_FLIP_HORIZONTAL (spawns immediately)
#       &0a1c &a0 ; data : 8 creatures, with bush
#       &0ac5 &30 ; type : OBJECT_RED_MAGENTA_BIRD
# 
# &a2 : &0691 &82 ; x    : (&82, &bf) 
#       &0790 &c6 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL 
#       &0a1d &80 ; data
#       &0ac6 &09 ; type : OBJECT_RED_SLIME
# 
# &a3 : &0692 &61 ; x    : (&61, &c7) 
#       &0791 &06 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE 
#       &0a1e &83 ; data : type 3 (magenta / red, sucks OBJECT_CORONIUM_BOULDER with power &40)
#       &0ac7 &0d ; type : OBJECT_SUCKING_NEST
# 
# &a4 : &0693 &d4 ; x    : (&d4, &bf) 
#       &0792 &c6 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL 
#       &0a1f &80 ; data
#       &0ac8 &09 ; type : OBJECT_RED_SLIME
# 
# &a5 : &0694 &d3 ; x    : (&d3, &be) 
#       &0793 &c6 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL 
#       &0a20 &80 ; data
#       &0ac9 &09 ; type : OBJECT_RED_SLIME
# 
# &a6 : &0695 &77 ; x    : (&77, &aa) 
#       &0794 &06 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE 
#       &0a21 &80 ; data
#       &0aca &4f ; type : OBJECT_CANNON_CONTROL_DEVICE
# 
# &a7 : &0696 &2e ; x    : (&2e, &d6) 
#       &0795 &06 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE 
#       &0a22 &80 ; data
#       &0acb &24 ; type : OBJECT_GREEN_CLAWED_ROBOT
# 
# &a8 : &0697 &64 ; x    : (&64, &d6) 
#       &0796 &85 ; tile : TILE_STONE_HALF_WITH_OBJECT_FROM_TYPE | TILE_FLIP_HORIZONTAL 
#       &0a23 &00 ; data
#       &0acc &4a ; type : OBJECT_DESTINATOR
# 
# &a9 : &0698 &86 ; x    : (&86, &56) 
#       &0797 &47 ; tile : TILE_GREENERY_WITH_OBJECT_FROM_TYPE | TILE_FLIP_VERTICAL (more aggressive)
#       &0a24 &c4 ; data : spawns OBJECT_WASP
#       &0acd &04 ; type : OBJECT_SMALL_HIVE (not used as target)
# 
# &aa : &0699 &a5 ; x    : (&a5, &6b), (&a5, &e7) 
#       &0798 &4a ; tile : TILE_PIPE | TILE_FLIP_VERTICAL 
#       &0a25 &40 ; data : 16 creatures
#       &0ace &1a ; type : OBJECT_HOVERING_BALL
# 
# &ab : &069a &a0 ; x    : (&a0, &bf) 
#       &0799 &ca ; tile : TILE_PIPE | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL (spawns immediately)
#       &0a26 &84 ; data : 1 creature, with bush
#       &0acf &39 ; type : OBJECT_MOVING_FIREBALL
# 
# &ac : &069b &d1 ; x    : (&d1, &d3) 
#       &079a &8a ; tile : TILE_PIPE | TILE_FLIP_HORIZONTAL (spawns immediately)
#       &0a27 &28 ; data : 10 creatures
#       &0ad0 &10 ; type : OBJECT_PIRANHA
# 
# &ad : &069c &b4 ; x    : (&b4, &c2) 
#       &079b &00 ; tile : TILE_INVISIBLE_SWITCH 
#       &0a28 &75 ; data : switch effects &0e, set &02
#       &0ad1 &00 ; type : triggered by OBJECT_PLAYER
# 
# &ae : &069d &7f ; x    : (&7f, &77) 
#       &079c &00 ; tile : TILE_INVISIBLE_SWITCH 
#       &0a29 &bc ; data : switch effects &17, clear &02
#       &0ad2 &4c ; type : triggered by OBJECT_EMPTY_FLASK
# 
# &af : &069e &a3 ; x    : (&a3, &63) 
#       &079d &44 ; tile : TILE_STONE_DOOR | TILE_FLIP_VERTICAL (vertical door)
#       &0a2a &f1 ; data : door colour 7 (mwb), locked
# 
# &b0 : &069f &9f ; x    : (&9f, &71) 
#       &079e &43 ; tile : TILE_METAL_DOOR | TILE_FLIP_VERTICAL (vertical door)
#       &0a2b &d1 ; data : door colour 5 (rmr), locked
# 
# &b1 : &06a0 &99 ; x    : (&99, &4c) 
#       &079f &c3 ; tile : TILE_METAL_DOOR | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL (horizontal door)
#       &0a2c &a9 ; data : door colour 2 (gyr), locked, slow
# 
# &b2 : &06a1 &80 ; x    : (&80, &77) 
#       &07a0 &44 ; tile : TILE_STONE_DOOR | TILE_FLIP_VERTICAL (vertical door)
#       &0a2d &f1 ; data : door colour 7 (mwb), locked
#                        ; &02 set (opened) by invisible switch at (&87, &77)
#                        ; &02 cleared (closed) by invisible switch at (&7f, &77)
#                        ; &02 cleared (closed) by invisible switch at (&83, &76)
# 
# &b3 : &06a2 &67 ; x    : (&67, &ce) 
#       &07a1 &44 ; tile : TILE_STONE_DOOR | TILE_FLIP_VERTICAL (vertical door)
#       &0a2e &c0 ; data : door colour 4 (rmb), unlocked
# 
# &b4 : &06a3 &da ; x    : (&da, &6d) 
#       &07a2 &04 ; tile : TILE_STONE_DOOR (horizontal door)
#       &0a2f &c1 ; data : door colour 4 (rmb), locked
#                        ; &01 toggled (locked or unlocked) by switch at (&d5, &73)
#                        ; &02 toggled (opened or closed) by switch at (&d4, &6f)
# 
# &b5 : &06a4 &89 ; x    : (&89, &71) 
#       &07a3 &41 ; tile : TILE_TRANSPORTER | TILE_FLIP_VERTICAL 
#       &0a30 &8f ; data : destination &7, inactive, uses key 3 (OBJECT_YELLOW_WHITE_RED_KEY)
#                        ; &01 toggled (locked or unlocked) by switch at (&46, &56) or switch at (&8b, &71)
# 
# &b6 : &06a5 &95 ; x    : (&95, &5d) 
#       &07a4 &c8 ; tile : TILE_SWITCH | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL 
#       &0a31 &94 ; data : switch effects &02, toggle &02
# 
# &b7 : &06a6 &8b ; x    : (&8b, &71) 
#       &07a5 &88 ; tile : TILE_SWITCH | TILE_FLIP_HORIZONTAL 
#       &0a32 &c2 ; data : switch effects &08, toggle &01
# 
# &b8 : &06a7 &ab ; x    : (&ab, &6b) 
#       &07a6 &c8 ; tile : TILE_SWITCH | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL 
#       &0a33 &ca ; data : switch effects &09, toggle &01
# 
# &b9 : &06a8 &c4 ; x    : (&c4, &c4) 
#       &07a7 &c8 ; tile : TILE_SWITCH | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL 
#       &0a34 &fa ; data : switch effects &0f, toggle &01
# 
# &ba : &06a9 &9d ; x    : (&9d, &5d) 
#       &07a8 &02 ; tile : TILE_SPACE_WITH_OBJECT_FROM_DATA 
#       &0a35 &9c ; data : OBJECT_MAGENTA_ROLLING_ROBOT
# 
# &bb : &06aa &aa ; x    : (&aa, &61) 
#       &07a9 &8c ; tile : TILE_ENGINE | TILE_FLIP_HORIZONTAL 
#       &0a36 &fe ; data ; inactive
#                        ; &02 toggled (activated or deactivated) by switch at (&d5, &73)
# 
# TILE_CHECK_TERTIARY_OBJECT_RANGE_SIX
# 
# &bc : &06ab &bb ; x    : (&bb, &c3) 
#       &07aa &49 ; tile : TILE_NEST | TILE_FLIP_VERTICAL 
#       &0a37 &10 ; data : 4 creatures
#       &0ad3 &0a ; type : OBJECT_GREEN_SLIME
# 
# &bd : &06ac &47 ; x    : (&47, &59) 
#       &07ab &89 ; tile : TILE_NEST | TILE_FLIP_HORIZONTAL (spawns immediately)
#       &0a38 &14 ; data : 5 creatures
#       &0ad4 &2f ; type : OBJECT_WHITE_YELLOW_BIRD
# 
# &be : &06ad &8a ; x    : (&8a, &78) 
#       &07ac &0a ; tile : TILE_PIPE 
#       &0a39 &90 ; data : 4 creatures, with bush
#       &0ad5 &29 ; type : OBJECT_RED_MAGENTA_IMP
# 
# &bf : &06ae &a7 ; x    : (&a7, &9a) 
#       &07ad &0a ; tile : TILE_PIPE 
#       &0a3a &98 ; data : 6 creatures, with bush
#       &0ad6 &2c ; type : OBJECT_CYAN_YELLOW_IMP
# 
# &c0 : &06af &61 ; x    : (&61, &d7) 
#       &07ae &8a ; tile : TILE_PIPE | TILE_FLIP_HORIZONTAL (spawns immediately)
#       &0a3b &04 ; data : 1 creature
#       &0ad7 &37 ; type : OBJECT_FIREBALL
# 
# &c1 : &06b0 &9e ; x    : (&9e, &51) 
#       &07af &c6 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL 
#       &0a3c &a4 ; data : fires OBJECT_ACTIVE_GRENADE
#       &0ad8 &20 ; type : OBJECT_CYAN_RED_TURRET
# 
# &c2 : &06b1 &2e ; x    : (&2e, &c8) 
#       &07b0 &06 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE 
#       &0a3d &80 ; data
#       &0ad9 &3a ; type : OBJECT_GIANT_BLOCK
# 
# &c3 : &06b2 &d6 ; x    : (&d6, &a1) 
#       &07b1 &06 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE 
#       &0a3e &83 ; data : type 3 (magenta / red, sucks OBJECT_CORONIUM_BOULDER with power &40)
#       &0ada &0d ; type : OBJECT_SUCKING_NEST
# 
# &c4 : &06b3 &7e ; x    : (&7e, &76) 
#       &07b2 &47 ; tile : TILE_GREENERY_WITH_OBJECT_FROM_TYPE | TILE_FLIP_VERTICAL (more aggressive)
#       &0a3f &c6 ; data : spawns OBJECT_WASP, inactive
#                        ; &02 set (deactivated) by invisible switch at (&87, &77)
#                        ; &02 cleared (activated) by invisible switch at (&7f, &77)
#                        ; &02 cleared (activated) by invisible switch at (&83, &76)
#       &0adb &05 ; type : OBJECT_LARGE_HIVE
# 
# &c5 : &06b4 &da ; x    : (&da, &6e) 
#       &07b3 &87 ; tile : TILE_GREENERY_WITH_OBJECT_FROM_TYPE | TILE_FLIP_HORIZONTAL (less aggressive)
#       &0a40 &c4 ; data : spawns OBJECT_WASP
#       &0adc &05 ; type : OBJECT_LARGE_HIVE
# 
# &c6 : &06b5 &aa ; x    : (&aa, &62) 
#       &07b4 &cc ; tile : TILE_ENGINE | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL 
#       &0a41 &fe ; data ; inactive
#                        ; &02 toggled (activated or deactivated) by switch at (&d5, &73)
# 
# &c7 : &06b6 &ab ; x    : (&ab, &69) 
#       &07b5 &41 ; tile : TILE_TRANSPORTER | TILE_FLIP_VERTICAL 
#       &0a42 &aa ; data : destination &5, uses key 4 (not collectable)
#                        ; &01 toggled (destination toggled between &5 and &4) by switch at (&ab, &6b)
# 
# &c8 : &06b7 &45 ; x    : (&45, &57) 
#       &07b6 &01 ; tile : TILE_TRANSPORTER 
#       &0a43 &90 ; data : destination &8, uses key 3 (OBJECT_YELLOW_WHITE_RED_KEY)
#                        ; &01 toggled (destination toggled between &8 and &9) by switch at (&46, &56) or switch at (&8b, &71)
# 
# &c9 : &06b8 &67 ; x    : (&67, &cb) 
#       &07b7 &08 ; tile : TILE_SWITCH 
#       &0a44 &ec ; data : switch effects &0d, toggle &02
# 
# &ca : &06b9 &d4 ; x    : (&d4, &6f) 
#       &07b8 &08 ; tile : TILE_SWITCH 
#       &0a45 &dc ; data : switch effects &0b, toggle &02
# 
# &cb : &06ba &29 ; x    : (&29, &c8) 
#       &07b9 &08 ; tile : TILE_SWITCH
#       &0a46 &9e ; data : switch effects &03, toggle &03
# 
# &cc : &06bb &b8 ; x    : (&b8, &c3) 
#       &07ba &08 ; tile : TILE_SWITCH 
#       &0a47 &f4 ; data : switch effects &0e, toggle &02
# 
# &cd : &06bc &6b ; x    : (&6b, &e1) 
#       &07bb &c4 ; tile : TILE_STONE_DOOR | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL (horizontal door)
#       &0a48 &f7 ; data : door colour 7 (mwb), locked, open
#                        ; &02 toggled (opened or closed) by switch at (&6a, &de)
# 
# &ce : &06bd &69 ; x    : (&69, &de) 
#       &07bc &84 ; tile : TILE_STONE_DOOR | TILE_FLIP_HORIZONTAL (vertical door)
#       &0a49 &f1 ; data : door colour 7 (mwb), locked
# 
# &cf : &06be &9d ; x    : (&9d, &56) 
#       &07bd &43 ; tile : TILE_METAL_DOOR | TILE_FLIP_VERTICAL (vertical door)
#       &0a4a &f1 ; data : door colour 7 (mwb), locked
# 
# &d0 : &06bf &94 ; x    : (&94, &5f) 
#       &07be &43 ; tile : TILE_METAL_DOOR | TILE_FLIP_VERTICAL (vertical door)
#       &0a4b &81 ; data : door colour 0 (cyg), locked
#                        ; &02 toggled (opened or closed) by switch at (&95, &5d)
# 
# &d1 : &06c0 &63 ; x    : (&63, &ca) 
#       &07bf &84 ; tile : TILE_STONE_DOOR | TILE_FLIP_HORIZONTAL (vertical door)
#       &0a4c &f1 ; data : door colour 7 (mwb), locked
# 
# &d2 : &06c1 &b4 ; x    : (&b4, &c3) 
#       &07c0 &04 ; tile : TILE_STONE_DOOR (horizontal door)
#       &0a4d &f1 ; data : door colour 7 (mwb), locked
#                        ; &02 toggled (opened or closed) by switch at (&b8, &c3)
#                        ; &02 set (opened) by invisible switch at (&b4, &c2)
# 
# &d3 : &06c2 &a1 ; x    : (&a1, &6b) 
#       &07c1 &83 ; tile : TILE_METAL_DOOR | TILE_FLIP_HORIZONTAL (vertical door)
#       &0a4e &b1 ; data : door colour 3 (ywr), locked
# 
# &d4 : &06c3 &9f ; x    : (&9f, &57) 
#       &07c2 &82 ; tile : TILE_SPACE_WITH_OBJECT_FROM_DATA | TILE_FLIP_HORIZONTAL 
#       &0a4f &db ; data : OBJECT_ICER
# 
# &d5 : &06c4 &a0 ; x    : (&a0, &6b) 
#       &07c3 &82 ; tile : TILE_SPACE_WITH_OBJECT_FROM_DATA | TILE_FLIP_HORIZONTAL 
#       &0a50 &9e ; data : OBJECT_BLUE_ROLLING_ROBOT
# 
# &d6 : &06c5 &57 ; x    : (&57, &69) 
#       &07c4 &0d ; tile : TILE_WATER 
# 
# &d7 : &06c6 &e1 ; x    : (&e1, &73) 
#       &07c5 &0d ; tile : TILE_WATER 
# 
# TILE_CHECK_TERTIARY_OBJECT_RANGE_SEVEN
# 
# &d8 : &06c7 &7f ; x    : (&7f, &c1) 
#       &07c6 &46 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE | TILE_FLIP_VERTICAL 
#       &0a51 &84 ; data : type 4 (yellow / white, sucks OBJECT_PIRANHA with power &50)
#       &0add &0d ; type : OBJECT_SUCKING_NEST
# 
# &d9 : &06c8 &a6 ; x    : (&a6, &69) 
#       &07c7 &06 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE 
#       &0a52 &ac ; data ; fires OBJECT_BLUE_DEATH_BALL
#                        ; &01 toggled (activated or deactivated) by switch at (&ab, &6b)
#       &0ade &20 ; type : OBJECT_CYAN_RED_TURRET
# 
# &da : &06c9 &b4 ; x    : (&b4, &c5) 
#       &07c8 &06 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE 
#       &0a53 &80 ; data : type 0 (cyan / green, sucks all objects with power &50)
#       &0adf &0d ; type : OBJECT_SUCKING_NEST
# 
# &db : &06ca &53 ; x    : (&53, &95), (&53, &a5) 
#       &07c9 &06 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE 
#       &0a54 &80 ; data : type 0 (cyan / green, sucks all objects with power &50)
#       &0ae0 &0d ; type : OBJECT_SUCKING_NEST
# 
# &dc : &06cb &61 ; x    : (&61, &d8) 
#       &07ca &06 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE 
#       &0a55 &80 ; data
#       &0ae1 &48 ; type : OBJECT_MAGGOT_MACHINE
# 
# &dd : &06cc &d4 ; x    : (&d4, &73) 
#       &07cb &45 ; tile : TILE_STONE_HALF_WITH_OBJECT_FROM_TYPE | TILE_FLIP_VERTICAL 
#       &0a56 &80 ; data
#       &0ae2 &51 ; type : OBJECT_CYAN_YELLOW_GREEN_KEY
# 
# &de : &06cd &82 ; x    : (&82, &c5) 
#       &07cc &45 ; tile : TILE_STONE_HALF_WITH_OBJECT_FROM_TYPE | TILE_FLIP_VERTICAL 
#       &0a57 &80 ; data
#       &0ae3 &0c ; type : OBJECT_DENSE_NEST
# 
# &df : &06ce &e3 ; x    : (&e3, &b5) 
#       &07cd &45 ; tile : TILE_STONE_HALF_WITH_OBJECT_FROM_TYPE | TILE_FLIP_VERTICAL 
#       &0a58 &80 ; data
#       &0ae4 &55 ; type : OBJECT_CORONIUM_BOULDER
# 
# &e0 : &06cf &75 ; x    : (&75, &87) 
#       &07ce &06 ; tile : TILE_SPACE_WITH_OBJECT_FROM_TYPE 
#       &0a59 &80 ; data
#       &0ae5 &22 ; type : OBJECT_MAGENTA_CLAWED_ROBOT
# 
# &e1 : &06d0 &c3 ; x    : (&c3, &c5) 
#       &07cf &07 ; tile : TILE_GREENERY_WITH_OBJECT_FROM_TYPE (more aggressive)
#       &0a5a &c0 ; data : spawns OBJECT_PIRANHA
#       &0ae6 &04 ; type : OBJECT_SMALL_HIVE
# 
# &e2 : &06d1 &84 ; x    : (&84, &70) 
#       &07d0 &89 ; tile : TILE_NEST | TILE_FLIP_HORIZONTAL (spawns immediately)
#       &0a5b &04 ; data : 1 creature
#       &0ae7 &2e ; type : OBJECT_GREEN_YELLOW_BIRD
# 
# &e3 : &06d2 &9e ; x    : (&9e, &69) 
#       &07d1 &09 ; tile : TILE_NEST 
#       &0a5c &08 ; data : 2 creatures
#       &0ae8 &2f ; type : OBJECT_WHITE_YELLOW_BIRD
# 
# &e4 : &06d3 &c6 ; x    : (&c6, &be) 
#       &07d2 &8a ; tile : TILE_PIPE | TILE_FLIP_HORIZONTAL (spawns immediately)
#       &0a5d &90 ; data : 4 creatures, with bush
#       &0ae9 &2b ; type : OBJECT_BLUE_CYAN_IMP
# 
# &e5 : &06d4 &64 ; x    : (&64, &c6) 
#       &07d3 &4a ; tile : TILE_PIPE | TILE_FLIP_VERTICAL 
#       &0a5e &a2 ; data : 8 creatures, with bush, inactive
#                        ; &02 toggled (activated or deactivated) by switch at (&67, &cb)
#       &0aea &2a ; type : OBJECT_RED_YELLOW_IMP
# 
# &e6 : &06d5 &a2 ; x    : (&a2, &5b) 
#       &07d4 &4a ; tile : TILE_PIPE | TILE_FLIP_VERTICAL 
#       &0a5f &04 ; data : 1 creature
#       &0aeb &21 ; type : OBJECT_HOVERING_ROBOT
# 
# &e7 : &06d6 &28 ; x    : (&28, &d8) 
#       &07d5 &ca ; tile : TILE_PIPE | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL (spawns immediately)
#       &0a60 &04 ; data : 1 creature
#       &0aec &02 ; type : OBJECT_CREW_MEMBER
# 
# &e8 : &06d7 &29 ; x    : (&29, &d8) 
#       &07d6 &ca ; tile : TILE_PIPE | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL (spawns immediately)
#       &0a61 &04 ; data : 1 creature
#       &0aed &02 ; type : OBJECT_CREW_MEMBER
# 
# &e9 : &06d8 &9d ; x    : (&9d, &5b) 
#       &07d7 &4a ; tile : TILE_PIPE | TILE_FLIP_VERTICAL 
#       &0a62 &20 ; data : 8 creatures
#       &0aee &1a ; type : OBJECT_HOVERING_BALL
# 
# &ea : &06d9 &83 ; x    : (&83, &76) 
#       &07d8 &00 ; tile : TILE_INVISIBLE_SWITCH 
#       &0a63 &bc ; data : switch effects &17, clear &02
#       &0aef &80 ; type : triggered by any object
# 
# &eb : &06da &a8 ; x    : (&a8, &69) 
#       &07d9 &00 ; tile : TILE_INVISIBLE_SWITCH 
#       &0a64 &53 ; data : switch effects &0a, set &01
#       &0af0 &4b ; type : triggered by OBJECT_POWER_POD
# 
# &ec : &06db &80 ; x    : (&80, &c2) 
#       &07da &00 ; tile : TILE_INVISIBLE_SWITCH 
#       &0a65 &9d ; data : switch effects &13, set &02
#       &0af1 &80 ; type : triggered by any object
# 
# &ed : &06dc &aa ; x    : (&aa, &63) 
#       &07db &48 ; tile : TILE_SWITCH | TILE_FLIP_VERTICAL 
#       &0a66 &84 ; data : switch effects &00, toggle &02
# 
# &ee : &06dd &d5 ; x    : (&d5, &73) 
#       &07dc &08 ; tile : TILE_SWITCH 
#       &0a67 &da ; data : switch effects &0b, toggle &01
# 
# &ef : &06de &a0 ; x    : (&a0, &67) 
#       &07dd &82 ; tile : TILE_SPACE_WITH_OBJECT_FROM_DATA | TILE_FLIP_HORIZONTAL 
#       &0a68 &cb ; data : OBJECT_POWER_POD
# 
# &f0 : &06df &9f ; x    : (&9f, &51) 
#       &07de &82 ; tile : TILE_SPACE_WITH_OBJECT_FROM_DATA | TILE_FLIP_HORIZONTAL 
#       &0a69 &de ; data : OBJECT_PROTECTION_SUIT
# 
# &f1 : &06e0 &d6 ; x    : (&d6, &73) 
#       &07df &82 ; tile : TILE_SPACE_WITH_OBJECT_FROM_DATA | TILE_FLIP_HORIZONTAL 
#       &0a6a &c5 ; data : OBJECT_BOULDER
# 
# &f2 : &06e1 &62 ; x    : (&62, &cc) 
#       &07e0 &02 ; tile : TILE_SPACE_WITH_OBJECT_FROM_DATA 
#       &0a6b &a5 ; data : OBJECT_RED_CLAWED_ROBOT
# 
# &f3 : &06e2 &69 ; x    : (&69, &d1) 
#       &07e1 &c4 ; tile : TILE_STONE_DOOR | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL (horizontal door)
#       &0a6c &c1 ; data : door colour 4 (rmb), locked
#                        ; &02 toggled (opened or closed) by switch at (&67, &cb)
# 
# &f4 : &06e3 &2c ; x    : (&2c, &d6) 
#       &07e2 &c4 ; tile : TILE_STONE_DOOR | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL (horizontal door)
#       &0a6d &f1 ; data : door colour 7 (mwb), locked
#                        ; &03 toggled (locked or unlocked and open or closed) by switch at (&29, &c8)
# 
# &f5 : &06e4 &a5 ; x    : (&a5, &64), (&a5, &65) 
#       &07e3 &0b ; tile : TILE_CONSTANT_WIND 
#       &0a6e &70 ; data ; blow downwards with power &70
# 
# TILE_CHECK_TERTIARY_OBJECT_RANGE_EIGHT
# 
# &f6 : &06e5 &b8 ; x    : (&b8, &60), (&b8, &61), (&b8, &62), (&b8, &63), (&b8, &64), (&b8, &65), (&b8, &66), (&b8, &67),
#                          (&b8, &68), (&b8, &69), (&b8, &6a), (&b8, &6b), (&b8, &6c), (&b8, &6d), (&b8, &6e), (&b8, &6f),
#                          (&b8, &70), (&b8, &71), (&b8, &72), (&b8, &73), (&b8, &74), (&b8, &75), (&b8, &76), (&b8, &77),
#                          (&b8, &78), (&b8, &79), (&b8, &7a), (&b8, &7b), (&b8, &7c), (&b8, &7d), (&b8, &7e), (&b8, &7f),
#                          (&b8, &aa), (&b8, &ab), (&b8, &ac), (&b8, &ad), (&b8, &ae), (&b8, &af), (&b8, &b0), (&b8, &b1),
#                          (&b8, &b2), (&b8, &b3), (&b8, &b4), (&b8, &b5), (&b8, &b6), (&b8, &b7), (&b8, &b8), (&b8, &b9),
#                          (&b8, &ba), (&b8, &bb), (&b8, &bc), (&b8, &bd), (&b8, &be), (&b8, &bf), (&b8, &c0), (&b8, &c6),
#                          (&b8, &c7), (&b8, &c8), (&b8, &ce), (&b8, &cf), (&b8, &d0), (&b8, &d1), (&b8, &d2), (&b8, &d3),
#                          (&b8, &d4), (&b8, &d5), (&b8, &d6), (&b8, &d7), (&b8, &d8), (&b8, &d9), (&b8, &da), (&b8, &db),
#                          (&b8, &dc), (&b8, &dd), (&b8, &de), (&b8, &df), (&b8, &e0), (&b8, &e1) 
#       &07e4 &0b ; tile : TILE_CONSTANT_WIND 
#       &0a6f &d0 ; data : blow upwards with power &20
# 
# &f7 : &06e6 &b9 ; x    : (&b9, &60), (&b9, &61), (&b9, &62), (&b9, &63), (&b9, &64), (&b9, &65), (&b9, &66), (&b9, &67),
#                          (&b9, &68), (&b9, &69), (&b9, &6a), (&b9, &6b), (&b9, &6c), (&b9, &6d), (&b9, &6e), (&b9, &6f),
#                          (&b9, &70), (&b9, &71), (&b9, &72), (&b9, &73), (&b9, &74), (&b9, &75), (&b9, &76), (&b9, &77),
#                          (&b9, &78), (&b9, &79), (&b9, &7a), (&b9, &7b), (&b9, &7c), (&b9, &7d), (&b9, &7e), (&b9, &7f),
#                          (&b9, &80), (&b9, &81), (&b9, &82), (&b9, &83), (&b9, &84), (&b9, &85), (&b9, &86), (&b9, &87),
#                          (&b9, &88), (&b9, &89), (&b9, &8a), (&b9, &8b), (&b9, &8c), (&b9, &8d), (&b9, &8e), (&b9, &8f),
#                          (&b9, &90), (&b9, &91), (&b9, &92), (&b9, &93), (&b9, &94), (&b9, &95), (&b9, &96), (&b9, &97),
#                          (&b9, &98), (&b9, &99), (&b9, &9a), (&b9, &9b), (&b9, &9c), (&b9, &9d), (&b9, &9e), (&b9, &9f),
#                          (&b9, &a0), (&b9, &a1), (&b9, &a2), (&b9, &a3), (&b9, &a4), (&b9, &a5), (&b9, &a6), (&b9, &a7),
#                          (&b9, &a8), (&b9, &a9), (&b9, &aa), (&b9, &ab), (&b9, &ac), (&b9, &ad), (&b9, &ae), (&b9, &af),
#                          (&b9, &b0), (&b9, &b1), (&b9, &b2), (&b9, &b3), (&b9, &b4), (&b9, &b5), (&b9, &b6), (&b9, &b7),
#                          (&b9, &b8), (&b9, &b9), (&b9, &ba), (&b9, &bb), (&b9, &bc), (&b9, &bd), (&b9, &be), (&b9, &bf),
#                          (&b9, &c0), (&b9, &c6), (&b9, &c7), (&b9, &c8), (&b9, &cd), (&b9, &ce), (&b9, &cf), (&b9, &d0),
#                          (&b9, &d1), (&b9, &d2), (&b9, &d3), (&b9, &d4), (&b9, &d5), (&b9, &d6), (&b9, &d7), (&b9, &d8),
#                          (&b9, &d9), (&b9, &da), (&b9, &db), (&b9, &dc), (&b9, &dd), (&b9, &de), (&b9, &df), (&b9, &e0) 
#       &07e5 &0b ; tile : TILE_CONSTANT_WIND 
#       &0a70 &80 ; data : blow upwards with power &80
# 
# &f8 : &06e7 &d9 ; x    : (&d9, &57), (&d9, &58), (&d9, &59), (&d9, &5a), (&d9, &5b), (&d9, &5c), (&d9, &5d), (&d9, &5e),
#                          (&d9, &5f), (&d9, &60), (&d9, &61), (&d9, &62), (&d9, &63), (&d9, &64), (&d9, &65), (&d9, &66),
#                          (&d9, &67), (&d9, &68), (&d9, &69), (&d9, &6a), (&d9, &6b), (&d9, &70), (&d9, &71), (&d9, &72),
#                          (&d9, &73), (&d9, &74), (&d9, &75), (&d9, &76), (&d9, &77), (&d9, &78), (&d9, &79), (&d9, &7a),
#                          (&d9, &7b), (&d9, &7c), (&d9, &7d), (&d9, &7e), (&d9, &7f), (&d9, &80), (&d9, &85), (&d9, &86),
#                          (&d9, &87), (&d9, &88), (&d9, &8d), (&d9, &8e), (&d9, &8f), (&d9, &90), (&d9, &91), (&d9, &92),
#                          (&d9, &93), (&d9, &94), (&d9, &95), (&d9, &96), (&d9, &97), (&d9, &98), (&d9, &99), (&d9, &9a),
#                          (&d9, &9b), (&d9, &9c), (&d9, &9d), (&d9, &9e), (&d9, &9f), (&d9, &a0), (&d9, &b3), (&d9, &b4),
#                          (&d9, &b5), (&d9, &b6), (&d9, &b7), (&d9, &b8), (&d9, &b9), (&d9, &ba), (&d9, &bb), (&d9, &bc),
#                          (&d9, &bd), (&d9, &be), (&d9, &bf), (&d9, &c0), (&d9, &c1), (&d9, &c2), (&d9, &c3), (&d9, &c4),
#                          (&d9, &c5), (&d9, &c6), (&d9, &c7), (&d9, &c8), (&d9, &c9), (&d9, &ca), (&d9, &cb), (&d9, &cc),
#                          (&d9, &cd), (&d9, &ce), (&d9, &cf), (&d9, &d0), (&d9, &d1), (&d9, &d2), (&d9, &d3), (&d9, &d4),
#                          (&d9, &d5), (&d9, &d6), (&d9, &d7), (&d9, &d8), (&d9, &d9), (&d9, &da), (&d9, &db), (&d9, &dc),
#                          (&d9, &dd), (&d9, &de), (&d9, &df), (&d9, &e0), (&d9, &e1), (&d9, &e2), (&d9, &e3), (&d9, &e4),
#                          (&d9, &e5), (&d9, &e6), (&d9, &e7), (&d9, &e8) 
#       &07e6 &d1 ; tile : TILE_POSSIBLE_LEAF | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL (yellow or white leaves)
# 
# &f9 : &06e8 &59 ; x    : (&59, &52), (&59, &63), (&59, &64), (&59, &65), (&59, &66), (&59, &67), (&59, &68), (&59, &69),
#                          (&59, &6a), (&59, &6b), (&59, &6c), (&59, &6d), (&59, &6e), (&59, &6f), (&59, &72), (&59, &73),
#                          (&59, &74), (&59, &75), (&59, &76), (&59, &77), (&59, &78), (&59, &79), (&59, &7a), (&59, &7b),
#                          (&59, &7c), (&59, &7d), (&59, &7e), (&59, &7f), (&59, &80), (&59, &81), (&59, &82), (&59, &83),
#                          (&59, &84), (&59, &85), (&59, &86), (&59, &87), (&59, &88), (&59, &89), (&59, &8a), (&59, &8b),
#                          (&59, &8c), (&59, &8d), (&59, &8e), (&59, &8f), (&59, &90), (&59, &91), (&59, &92), (&59, &a3),
#                          (&59, &a4), (&59, &a5), (&59, &a6), (&59, &a7), (&59, &a8), (&59, &a9), (&59, &aa), (&59, &ab),
#                          (&59, &ac), (&59, &ad), (&59, &ae), (&59, &af), (&59, &b0), (&59, &b1), (&59, &b2), (&59, &b3),
#                          (&59, &b4), (&59, &b5), (&59, &b6), (&59, &b7), (&59, &b8), (&59, &b9), (&59, &ba), (&59, &bb),
#                          (&59, &bc), (&59, &bd), (&59, &be), (&59, &bf), (&59, &c0), (&59, &c1), (&59, &c2), (&59, &c3),
#                          (&59, &c4) 
#       &07e7 &91 ; tile : TILE_POSSIBLE_LEAF | TILE_FLIP_HORIZONTAL (green leaves)
# 
# &fa : &06e9 &79 ; x    : (&79, &53), (&79, &54), (&79, &55), (&79, &56), (&79, &57), (&79, &58), (&79, &59), (&79, &5a),
#                          (&79, &5b), (&79, &5c), (&79, &5d), (&79, &5e), (&79, &5f), (&79, &60), (&79, &61), (&79, &62),
#                          (&79, &63), (&79, &64), (&79, &65), (&79, &66), (&79, &67), (&79, &68), (&79, &69), (&79, &6a),
#                          (&79, &6b), (&79, &6c), (&79, &6d), (&79, &6e), (&79, &6f), (&79, &70), (&79, &71), (&79, &72),
#                          (&79, &73), (&79, &78), (&79, &79), (&79, &7a), (&79, &7b), (&79, &7c), (&79, &7d), (&79, &7e),
#                          (&79, &7f), (&79, &80), (&79, &81), (&79, &82), (&79, &93), (&79, &94), (&79, &95), (&79, &96),
#                          (&79, &97), (&79, &98), (&79, &99), (&79, &9a), (&79, &9b), (&79, &9c), (&79, &9d), (&79, &9e),
#                          (&79, &9f), (&79, &a0), (&79, &a1), (&79, &a2), (&79, &a3), (&79, &a4), (&79, &a5), (&79, &a6),
#                          (&79, &a7), (&79, &a8), (&79, &a9), (&79, &aa), (&79, &ab), (&79, &ac), (&79, &ad), (&79, &ae),
#                          (&79, &af), (&79, &b0), (&79, &b1), (&79, &b2), (&79, &b3), (&79, &b4), (&79, &b5), (&79, &b6),
#                          (&79, &b7), (&79, &b8), (&79, &b9), (&79, &ba) 
#       &07e8 &d1 ; tile : TILE_POSSIBLE_LEAF | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL (yellow or white leaves)
# 
# &fb : &06ea &39 ; x    : (&39, &80), (&39, &81), (&39, &82), (&39, &83), (&39, &84), (&39, &85), (&39, &86), (&39, &87),
#                          (&39, &88), (&39, &89), (&39, &8a), (&39, &8b), (&39, &8c), (&39, &8d), (&39, &8e), (&39, &8f),
#                          (&39, &90), (&39, &91), (&39, &92), (&39, &93), (&39, &94), (&39, &95), (&39, &96), (&39, &97),
#                          (&39, &98), (&39, &99), (&39, &9a), (&39, &9b), (&39, &9c), (&39, &9d), (&39, &9e), (&39, &9f),
#                          (&39, &a0), (&39, &a1), (&39, &a2), (&39, &b3), (&39, &b4), (&39, &b5), (&39, &b6), (&39, &b7),
#                          (&39, &b8), (&39, &b9), (&39, &ba), (&39, &bb), (&39, &bc), (&39, &bd), (&39, &be), (&39, &bf),
#                          (&39, &c0), (&39, &c1), (&39, &c2), (&39, &c3), (&39, &c4), (&39, &c5), (&39, &ca), (&39, &cb),
#                          (&39, &cc) 
#       &07e9 &d1 ; tile : TILE_POSSIBLE_LEAF | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL (yellow or white leaves)
# 
# &fc : &06eb &48 ; x    : (&48, &50), (&48, &51), (&48, &52), (&48, &58), (&48, &59), (&48, &5a), (&48, &5b), (&48, &6c),
#                          (&48, &6d), (&48, &6e), (&48, &6f), (&48, &70), (&48, &71), (&48, &72), (&48, &73), (&48, &74),
#                          (&48, &75), (&48, &76), (&48, &77), (&48, &78), (&48, &79), (&48, &7a), (&48, &7b), (&48, &7c),
#                          (&48, &7d), (&48, &7e), (&48, &7f), (&48, &80), (&48, &81), (&48, &82), (&48, &83), (&48, &84),
#                          (&48, &85), (&48, &86), (&48, &87), (&48, &88), (&48, &89), (&48, &8a), (&48, &8b), (&48, &8c),
#                          (&48, &8d), (&48, &8e), (&48, &8f), (&48, &90), (&48, &91), (&48, &92), (&48, &93), (&48, &94),
#                          (&48, &95), (&48, &96), (&48, &97), (&48, &98), (&48, &99), (&48, &9a), (&48, &9b) 
#       &07ea &91 ; tile : TILE_POSSIBLE_LEAF | TILE_FLIP_HORIZONTAL (green leaves)
# 
# &fd : &06ec &e8 ; x    : (&e8, &80), (&e8, &81), (&e8, &82), (&e8, &83), (&e8, &84), (&e8, &85), (&e8, &86), (&e8, &87),
#                          (&e8, &88), (&e8, &89), (&e8, &8a), (&e8, &8b), (&e8, &8c), (&e8, &8d), (&e8, &8e), (&e8, &8f),
#                          (&e8, &90), (&e8, &91), (&e8, &92), (&e8, &93), (&e8, &94), (&e8, &95), (&e8, &96), (&e8, &97),
#                          (&e8, &98), (&e8, &99), (&e8, &9a), (&e8, &9b), (&e8, &9c), (&e8, &9d), (&e8, &9e), (&e8, &9f)
#       &07eb &91 ; tile : TILE_POSSIBLE_LEAF | TILE_FLIP_HORIZONTAL (green leaves)
# 

; tertiary_objects_x
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
&05ef ff ff b0 ec 77 64 9a af da c6 36 9f 2e a9 9c 83 ; &00
&05ff 88 5f 57 bf 9d 4d 45 81 b3 3f cb 40 4c ca 2f a7 ; &10
&060f 56 34 e3 3b e4 80 e0 64 37 47 9f 9c aa 9b 9a 5e ; &20
&061f c7 8a 60 9d a2 b2 98 a9 db 28 29 3c 98 63 cb 61 ; &30
&062f a3 ce e9 80 2e 4f 79 87 b6 97 2d d6 5c a0 74 6a ; &40
&063f a1 9f 89 85 6b ae 65 e2 ed 80 cd a8 2b ab 9d 62 ; &50
&064f e5 70 ec 83 c1 c6 67 eb 2d 98 aa cc a5 9e a2 d7 ; &60
&065f e6 e7 94 7c e3 45 9b 9f c2 71 67 4f cf d2 e2 7a ; &70
&066f 62 da 76 b2 66 d7 83 84 80 87 9b 50 ae 64 a3 63 ; &80
&067f b8 7f 82 e0 9c 61 9d 29 46 9f 9a 74 75 77 b2 e4 ; &90
&068f 62 63 82 61 d4 d3 77 2e 64 86 a5 a0 d1 b4 7f a3 ; &a0
&069f 9f 99 80 67 da 89 95 8b ab c4 9d aa bb 47 8a a7 ; &b0
&06af 61 9e 2e d6 7e da aa ab 45 67 d4 29 b8 6b 69 9d ; &c0
&06bf 94 63 b4 a1 9f a0 57 e1 7f a6 b4 53 61 d4 82 e3 ; &d0
&06cf 75 c3 84 9e c6 64 a2 28 29 9d 83 a8 80 aa d5 a0 ; &e0
&06df 9f d6 62 69 2c a5 b8 b9 d9 59 79 39 48 e8 03    ; &f0

; tertiary_objects_tile_and_flip
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
&06ee 89 89 89 89 89 8a 46 c6 06 06 46 05 05 00 c3 04 ; &00
&06fe 83 84 84 83 88 48 02 02 42 02 7b 22 1e 06 06 c6 ; &10
&070e 06 46 46 85 85 87 89 89 c7 0a 8c 03 84 83 43 84 ; &20
&071e 84 02 01 41 01 01 2e 1e 3b 46 46 06 c6 06 06 06 ; &30
&072e 46 06 09 c9 89 ca 8a 8a 0a 4a 8a 04 44 41 01 c8 ; &40
&073e 48 cc 82 02 02 02 1e 89 09 0a 4a ca 4a 86 46 46 ; &50
&074e 46 86 45 47 00 00 00 00 84 c3 44 43 43 43 44 84 ; &60
&075e 44 04 01 08 08 02 02 82 1e 2d 46 46 45 46 c6 89 ; &70
&076e c9 49 89 ca ca 8a 8a 8a 0a 00 00 c4 43 04 43 84 ; &80
&077e 44 04 44 84 41 01 01 01 c8 42 82 3b 11 3b 89 c9 ; &90
&078e ca 8a c6 06 c6 c6 06 06 85 47 4a ca 8a 00 00 44 ; &a0
&079e 43 c3 44 44 04 41 c8 88 c8 c8 02 8c 49 89 0a 0a ; &b0
&07ae 8a c6 06 06 47 87 cc 41 01 08 08 08 08 c4 84 43 ; &c0
&07be 43 84 04 83 82 82 0d 0d 46 06 06 06 06 45 45 45 ; &d0
&07ce 06 07 89 09 8a 4a 4a ca ca 4a 00 00 00 48 08 82 ; &e0
&07de 82 82 02 c4 c4 0b 0b 0b d1 91 d1 d1 91 91 0d    ; &f0

; tile_strip_flips
&07ed 2e 2e 2e 2e 2e 2e 2e 2e 2e                                    # One byte per tile in horizontal or vertical strip

; tile_strip_tiles
&07f6 2e 2e 2e 2e 2e 2e 2e 2e 2e                                    # One byte per tile in horizontal or vertical strip

# game_state starts at &07f8
#
# When saving.
#     &00d9 - &00dc is copied to &07f8 - &07fb (rnd_state)
#             &00dd is copied to &07fc         (player_object_held)
#             &00de is copied to &07fd         (player_angle)
#             &00df is copied to &07fe         (player_facing)

; game_time
&07ff 00 00 00 00

; player_deaths
&0803 00 00 00

; player_collected
; player_keys_collected                                             # If negative, open doors with:
&0806 00 ; &00 : OBJECT_CYAN_YELLOW_GREEN_KEY                       #     cyan frame, yellow highlights, green lock
&0807 00 ; &01 : OBJECT_RED_YELLOW_GREEN_KEY                        #     red frame, yellow highlights, green lock
&0808 00 ; &02 : OBJECT_GREEN_YELLOW_RED_KEY                        #     green frame, yellow highlights, red lock
&0809 00 ; &03 : OBJECT_YELLOW_WHITE_RED_KEY                        #     yellow frame, white highlights, red lock
&080a 00 ; &04 : (not collectable)                                  #     red frame, magenta highlights, blue lock
&080b 00 ; &05 : OBJECT_RED_MAGENTA_RED_KEY                         #     red frame, magenta highlights, red lock
&080c 00 ; &06 : OBJECT_BLUE_CYAN_GREEN_KEY                         #     blue frame, cyan highlights, green lock
&080d 00 ; &07 : (not collectable)                                  #     magenta frame, white highlights, blue lock
; player_weapons_collected
; player_jetpack_booster_collected
&080e 00 ; &00 : OBJECT_JETPACK_BOOSTER
&080f 00 ; &01 : OBJECT_PISTOL
&0810 00 ; &02 : OBJECT_ICER
&0811 00 ; &03 : OBJECT_BLASTER
&0812 00 ; &04 : OBJECT_PLASMA_GUN
; player_protection_suit_collected
&0813 00 ; &05 : OBJECT_PROTECTION_SUIT
; player_fire_immunity_device_collected
&0814 00 ; OBJECT_FIRE_IMMUNITY_DEVICE
; player_mushroom_immunity_pill_collected
&0815 00 ; OBJECT_MUSHROOM_IMMUNITY_PILL
; player_whistle_one_collected
&0816 00 ; OBJECT_WHISTLE_ONE
; player_whistle_two_collected
&0817 00 ; OBJECT_WHISTLE_TWO
; player_radiation_immunity_pill_collected
&0818 00 ; OBJECT_RADIATION_IMMUNITY_PILL

; door_timer
&0819 00

; player_mushroom_timers
;     r  b
&081a 00 00                                                         # Added to when player touches mushrooms

; chatter_energy_reserve
&081c 00

; explosion_timer
&081d 00

; flooding_state
&081e 00                                                            # Negative if world is being flooded

; earthquake_state
&081f 00                                                            # Negative if earthquake has started

; unused
&0820 ff

; player_next_teleport
&0821 00

; player_teleports_remembered
&0822 00

; player_teleports_x
&0823 32 8e d2 63 99

; player_teleports_y
&0828 98 c0 c0 c7 3c

; copy_protection_third_byte                                        # permuted_rnd_pair
&082d 00                                                            # (?(rnd_state + 1) ^ ?(rnd_state + 3) & &7f)) ^ &65

; waterline_x_ranges_y_fraction
&082e 00 00 00 00

; waterline_x_ranges_y
&0832 ce df c1 c1

; waterline_x_ranges_desired_y
&0836 ce df c1 c1

; imp_types_gifts_remaining
;      0  1  2  3  4
&083a 04 0a 01 01 0a

; clawed_robots_availability                                        # Negative if clawed robot hasn't been disturbed
&083f 80 80 80 80                                                   # Zero if clawed robot has teleported away

; clawed_robots_teleporting_energy                                  # Negative to allow clawed robot to teleport back
&0843 00 00 00 00

; player_pockets_used
&0847 00

; player_pockets
&0848 50 ; OBJECT_INACTIVE_GRENADE
&0849 50 ; OBJECT_INACTIVE_GRENADE
&084a 50 ; OBJECT_INACTIVE_GRENADE
&084b 50 ; OBJECT_INACTIVE_GRENADE
&084c 50 ; OBJECT_INACTIVE_GRENADE

; player_weapon
&084d 00 ; jetpack

; player_weapons_energy_low
&084e 00 ; &00 (jetpack)
&084f 00 ; &01 (pistol)
&0850 00 ; &02 (icer)
&0851 00 ; &03 (blaster)
&0852 00 ; &04 (plasma gun)
&0853 00 ; &05 (protection suit)

; player_weapons_energy_high
&0854 30 ; &00 (jetpack)
&0855 10 ; &01 (pistol)
&0856 10 ; &02 (icer)
&0857 01 ; &03 (blaster)
&0858 08 ; &04 (plasma gun)
&0859 10 ; &05 (protection suit)

; weapons_energy_cost
&085a 01 ; &00 (jetpack)                                            # Jetpack uses 1 energy per two or eight frames
&085b 06 ; &01 (pistol)                                             # Pistol uses 6 energy per shot
&085c 10 ; &02 (icer)                                               # Icer uses 16 energy per shot
&085d ff ; &03 (blaster)                                            # Blaster uses 255 energy per discharge
&085e 32 ; &04 (plasma gun)                                         # Plasma gun uses 50 energy per shot
&085f 00 ; &05 (protection suit)                                    # Set when damage incurred

# Primary objects
# ===============
#       &00 : (&9b, &3b) &00 (OBJECT_PLAYER)
#       &01 : (&99, &3b) &26 (OBJECT_TRIAX)
# &02 - &0f : empty

; objects_type
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
&0860 00 26 d7 57 57 57 57 d6 d7 57 25 82 26 25 24 77

; objects_sprite
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
&0870 04 04 02 22 24 22 22 24 23 23 23 23 25 23 23 02

; objects_x_fraction                                                # Slot &10 is used for targeting (OBJECT_SLOT_TARGET)
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f 10
&0880 c0 64 23 23 23 26 25 22 22 22 25 e7 d7 c8 a4 7d 7a

; objects_x                                                         # Slot &11 is used for waterfall (OBJECT_SLOT_WATERFALL)
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f 10 11
&0891 9b 99 00 00 00 00 00 00 00 00 00 00 00 00 00 00 bc 65

; objects_y_fraction
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f 10
&08a3 80 20 94 48 5c 5a ae 0f 4b 6e d2 46 ef 06 06 dc a4

; objects_y
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f 10 11
&08b4 3b 3b 00 00 00 00 00 00 00 00 00 00 00 00 00 00 04 dc

# Object flags
# ============
# 8....... OBJECT_FLAG_FLIP_HORIZONTAL : set if object is flipped horizontally
# .4...... OBJECT_FLAG_FLIP_VERTICAL   : set if object is flipped vertically
# ..2..... OBJECT_FLAG_PENDING_REMOVAL : set if object has been marked for removal
# ...1.... OBJECT_FLAG_TELEPORTING     : set if object is teleporting
# ....8... OBJECT_FLAG_WAS_DAMAGED     : set if object has just taken damage of 8 or more
# .....4.. OBJECT_FLAG_NEWLY_CREATED   : set if object has just been created, and not yet updated
# ......2. OBJECT_FLAG_SUPPORTED       : set if object is supported beneath by tile or another object
# .......1 OBJECT_FLAG_NOT_PLOTTED     : set if object isn't currently plotted

; objects_flags
&08c6 81 11 01 01 01 01 01 01 01 01 01 01 01 01 01 01

; objects_palette
&08d6 7e 39 a6 0b 81 fa 68 68 68 68 8e 6b 82 92 81 92

; objects_velocity_x
&08e6 00 00 84 49 59 45 8c 93 36 36 3d 6d 6d 6d 6d af 

; objects_velocity_y
&08f6 00 10 a7 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d 92 20

# Object target and flags
# =======================
# 84...... directness of targeting for NPCs that have targets
#              &c0 TARGET_FLAG_DIRECTNESS_THREE : always take direct path to target
#              &80 TARGET_FLAG_DIRECTNESS_TWO   : always take direct path to target
#              &40 TARGET_FLAG_DIRECTNESS_ONE   : 1 in 4 chance of taking relaxed path to target
#              &00 TARGET_FLAG_DIRECTNESS_ZERO  : always take relaxed path to target
# ..2..... if set, avoid target
# ...18421 object to target (set to the same object if no target)

; objects_target_object_and_flags
&0906 b0 00 f0 f0 b1 b1 b1 b1 b0 b0 31 90 31 30 31 02

; objects_tx                                                        # tile x of target or teleport
&0916 02 99 02 0b 0a 88 84 cd cd 86 86 86 09 04 04 09

; objects_energy                                                    # Zero to explode
&0926 ff c8 83 d1 91 c4 c8 c7 c5 c5 c5 c6 8c 45 45 50

; objects_ty                                                        # tile y of target or teleport
&0936 50 3b 50 50 49 4a 4a 4a 4a 08 90 c3 86 86 86 86

; objects_touching                                                  # Negative if not touching another object
&0946 86 88 88 88 88 02 08 08 09 c9 0c 0a 0d 05 0d ce

; objects_timer                                                     # Use depends on object type
&0956 ce 0e ce 0d 0f cb 05 11 05 02 43 03 0d 05 85 05

; objects_tertiary_data_offset                                      # Pointer into tertiary_objects_data, or zero
&0966 00 05 05 c3 0d 0d 0d 0d 03 0d 0d 03 0d 0d 0d 0d

# Object state
# ============
# Use depends on object type. For NPCs that walk or respond to stimuli:
#
# 84...... NPC_MOOD_MASK    : NPC mood, affected by stimuli
#              &80 NPC_MOOD_MINUS_TWO
#              &c0 NPC_MOOD_MINUS_ONE
#              &00 NPC_MOOD_ZERO
#              &40 NPC_MOOD_PLUS_ONE
# ..2..... NPC_CLIMBING     : set for imps climbing steep slopes
# ...1.... NPC_WAS_FED      : set for imps that have been fed and are returning home
# ....8421 NPC_WALKING_MASK : number of frames since NPC was standing on a surface shallow enough to walk

; objects_state                                                     # Use depends on object type
&0976 0d 0d 0d 0d 0d 0d 0d 05 c6 ce c6 c6 c6 bb c6 18

; tertiary_objects_data
&0986 00 7c 60 04 88 88 a0 a6 ae 83 86 82 80 80 ad 01
&0996 f7 a1 f1 f7 81 0a ac d2 df d4 a3 84 85 ae 80 80
&09a6 88 ac c4 c0 04 a8 c4 bc 7d 01 c1 d1 91 f1 f1 da
&09b6 f7 f3 d8 88 80 83 83 b0 aa 80 80 87 80 30 08 10
&09c6 7c 04 10 a8 90 04 c1 f1 e1 95 bc b4 7d a1 d6 dd
&09d6 e2 04 0c 04 20 21 a0 b0 ac 83 81 84 80 c4 85 95
&09e6 a3 b5 f1 ad c1 81 89 a0 c1 f1 f1 c1 8c a4 e4 d7
&09f6 9d e1 a6 81 85 83 83 d0 a8 04 04 d0 88 04 04 04
&0a06 08 bd 8a f1 d1 f1 b1 f1 c1 c1 c1 c1 e2 e4 dc a0
&0a16 c2 cb b8 a8 10 98 a0 80 83 80 80 80 80 00 c4 40
&0a26 84 28 75 bc f1 d1 a9 f1 c0 c1 8f 94 c2 ca fa 9c
&0a36 fe 10 14 90 98 04 a4 80 83 c6 c4 fe aa 90 ec dc
&0a46 9e f4 f7 f1 f1 81 f1 f1 b1 db 9e 84 ac 80 80 80
&0a56 80 80 80 80 c0 04 08 90 a2 04 04 04 20 bc 53 9d
&0a66 84 da cb de c5 a5 c1 f1 70 d0 80

; tertiary_objects_type
&0a71 00 0f 27 2e 07 2f 2d 1f 1f 0d 0d 0d 0c 60 2c 00
&0a81 0d 0d 1f 0d 5c 0d 20 05 04 06 31 05 2a 09 0d 0d
&0a91 1f 20 55 55 0d 63 0f 2e 0a 1b 37 29 1a 1a 37 37
&0aa1 0a 37 4b 4b 2d 1f 20 0d 0d 28 55 05 80 00 80 80
&0ab1 20 28 0d 0d 28 27 31 0e 08 11 39 37 37 37 2a 80
&0ac1 4a 10 2f 30 30 09 0d 09 09 4f 24 4a 04 1a 39 10
&0ad1 00 4c 0a 2f 29 2c 37 20 3a 0d 05 05 0d 20 0d 0d
&0ae1 48 51 0c 55 22 04 2e 2f 2b 2a 21 02 02 1a 80 4b
&0af1 80

# Secondary objects
# =================
#       &00 : (&9b, &39) &64 (OBJECT_INVISIBLE_INERT)
#       &01 : (&a3, &5d) &43 (OBJECT_PIANO)
#       &02 : (&98, &4d) &50 (OBJECT_INACTIVE_GRENADE)
#       &03 : (&98, &4d) &50 (OBJECT_INACTIVE_GRENADE)
#       &04 : (&a4, &67) &59 (OBJECT_JETPACK_BOOSTER)
#       &05 : (&9f, &49) &50 (OBJECT_INACTIVE_GRENADE)
#       &06 : (&a0, &49) &46 (OBJECT_CANNON)
#       &07 : (&c0, &4e) &50 (OBJECT_INACTIVE_GRENADE)
#       &08 : (&48, &56) &50 (OBJECT_INACTIVE_GRENADE)
#       &09 : (&83, &78) &4e (OBJECT_REMOTE_CONTROL_DEVICE)
#       &0a : (&c5, &60) &45 (OBJECT_BOULDER)
#       &0b : (&87, &59) &53 (OBJECT_GREEN_YELLOW_RED_KEY)
#       &0c : (&97, &5e) &1d (OBJECT_RED_ROLLING_ROBOT)
#       &0d : (&e1, &61) &03 (OBJECT_FLUFFY)
#       &0e : (&84, &5b) &50 (OBJECT_INACTIVE_GRENADE)
#       &0f : (&98, &80) &50 (OBJECT_INACTIVE_GRENADE)
#       &10 : (&99, &3c) &4a (OBJECT_DESTINATOR)
#       &11 : (&e7, &80) &3a (OBJECT_GIANT_BLOCK)
#       &12 : (&7c, &77) &4c (OBJECT_EMPTY_FLASK)
# &13 - &1f : empty

; secondary_objects_x
&0af2 9b a3 98 98 a4 9f a0 c0 48 83 c5 87 97 e1 84 98
&0b02 99 e7 7c 00 00 00 00 00 00 00 00 00 00 00 00 00

; secondary_objects_y
&0b12 39 5d 4d 4d 67 49 49 4e 56 78 60 59 5e 61 5b 80
&0b22 3c 80 77 00 00 00 00 00 00 00 00 00 00 00 00 00

; secondary_objects_type
&0b32 64 43 50 50 59 50 46 50 50 4e 45 53 1d 03 50 50
&0b42 4a 3a 4c 00 00 00 00 00 00 00 00 00 00 00 00 00

; game_state_checksum_one                                           # Checksum of unencrypted &07f8 - &0b51, ^ &dc
&0b52 dc

; secondary_objects_energy_and_x_y_fractions                        # 8421.... energy
&0b53 f0 f3 71 79 fb 42 f5 47 f3 f2 f3 f2 f0 fb f2 40               # ....84.. x fraction
&0b63 fb f0 f1 00 00 00 00 00 00 00 00 00 00 00 00 00               # ......21 y fraction

; secondary_object_update_next_object
&0b73 00

; secondary_object_update_random_shuffle
&0b74 00

; game_state_checksum_two                                           # Used to encrypt or decrypt game_state_checksum_one
&0b75 43

; end of game state

; secondary_object_update_mode
&0b76 00                                                            # Negative to consider all, positive to consider only next

; secondary_object_update_distance
&0b77 00

; sprite_foreground_or_background_mask
&0b78 ff

; colours_1_and_2_pixel_values_table
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
;     YB GR MR CR WR YG CB MG GG WG bb YC WY YR WM WC               # Right pixel sets colour 1, left pixel sets colour 2
&0b79 ca c9 e3 e9 eb ce f8 e6 cc ee 30 de ef cb fb fe

; screen_scrolling_offset_x_fraction
; screen_scrolling_offset_y_fraction - 2
;     cx px cy py
&0b89 00 ea 00 ea

; screen_scrolling_offset_x
; screen_scrolling_offset_x - 2
;     cx px cy py
&0b8d 00 ea 00 ea

; screen_start_x_fraction
; screen_start_y_fraction - 2
;     cx px cy py
&0b91 ea ea ea ea

; screen_start_x
; screen_start_y - 2
;     cx px cy py
&0b95 ea ea ea ea

; screen_size_x_fraction
; screen_size_y_fraction - 2
;     cx px cy py
&0b99 00 ea 00 ea

; screen_size_x
; screen_size_y - 2
;     cx px cy py
&0b9d 08 ea 04 ea

; fraction_to_pixel_rounding_table
;     cx px cy py
&0ba1 f0 f0 f8 f8

; offscreen_flags_to_check_table
;      c  p                                                         # &05 = 0101 : current sprite is offscreen in x or y
&0ba5 05 0a                                                         # &0a = 1010 : previous sprite is offscreen in x or y

; copy_protection_first_byte                                        # expected_word_obfuscated_second_checksum
&0ba7 00                                                            # expected_word_second_checksum ^ (word_number & &7f) ^ &65

; copy_protection_second_byte                                       # entered_word_second_checksum
&0ba8 00

; set_object_Y_velocities_from_this_object
&0ba9 a5 43    LDA &43 ; this_object_velocity_x
&0bab 99 e6 08 STA &08e6,Y ; objects_velocity_x
&0bae a5 45    LDA &45 ; this_object_velocity_y
&0bb0 99 f6 08 STA &08f6,Y ; objects_velocity_y
&0bb3 60       RTS

; set_this_object_velocities_from_object_Y
&0bb4 b9 e6 08 LDA &08e6,Y ; objects_velocity_x
&0bb7 85 43    STA &43 ; this_object_velocity_x
&0bb9 b9 f6 08 LDA &08f6,Y ; objects_velocity_y
&0bbc 85 45    STA &45 ; this_object_velocity_y
&0bbe 60       RTS

; check_if_object_fired
&0bbf a5 aa    LDA &aa ; this_object
&0bc1 cd d7 29 CMP &29d7 ; player_object_fired
&0bc4 60       RTS

; check_if_object_hit_by_remote_control
&0bc5 a9 4e    LDA #&4e ; OBJECT_REMOTE_CONTROL_DEVICE
; check_if_object_hit_by_other_control
&0bc7 38       SEC
&0bc8 ae d7 29 LDX &29d7 ; player_object_fired                      # Negative if no object fired
&0bcb 30 1a    BMI &0be7 ; leave
&0bcd 5d 60 08 EOR &0860,X ; objects_type
&0bd0 d0 15    BNE &0be7 ; leave                                    # Leave with carry set if control device not fired
&0bd2 a9 18    LDA #&18 ; 24 * &20 fraction = 3 tiles
&0bd4 20 9c 35 JSR &359c ; check_for_obstruction_between_objects    # Returns carry clear if no obstruction
&0bd7 b0 0e    BCS &0be7 ; leave                                    # Leave with carry set if obstructed or too far
&0bd9 20 a0 22 JSR &22a0 ; calculate_angle_of_object_X_to_this_object
&0bdc e5 34    SBC &34 ; player_aiming_angle_with_flip
&0bde e9 80    SBC #&80 ; 180 degrees
&0be0 20 56 32 JSR &3256 ; invert_if_negative
&0be3 65 83    ADC &83 ; distance                                   # More precise angle needed if further away
&0be5 c9 18    CMP #&18                                             # Leave with carry set if too wide an angle
; leave
&0be7 60       RTS

; consider_promoting_secondary_objects
&0be8 2c 76 0b BIT &0b76 ; secondary_object_update_mode             # Negative to consider all secondary objects
&0beb 30 61    BMI &0c4e ; consider_promoting_secondary_objects_to_primary_objects
; consider_promoting_next_secondary_object_to_primary               # X = 0 here, so this_object = player
&0bed 20 b6 3b JSR &3bb6 ; get_maximum_of_this_object_velocities    # Returns A = larger of x and y velocities
&0bf0 4a       LSR A
&0bf1 4a       LSR A
&0bf2 6d 77 0b ADC &0b77 ; secondary_object_update_distance         # Keep track of how far the player has moved
&0bf5 8d 77 0b STA &0b77 ; secondary_object_update_distance         # If too far, consider promoting all secondary objects
&0bf8 b0 54    BCS &0c4e ; consider_promoting_secondary_objects_to_primary_objects
&0bfa ce 73 0b DEC &0b73 ; secondary_object_update_next_object      # Check secondary objects in random order
&0bfd 10 0d    BPL &0c0c ; skip_wraparound
&0bff 20 87 25 JSR &2587 ; rnd                                      # When all secondary objects have been checked,
&0c02 29 1f    AND #&1f
&0c04 8d 74 0b STA &0b74 ; secondary_object_update_random_shuffle   # start again, but in a different order
&0c07 a9 1f    LDA #&1f
&0c09 8d 73 0b STA &0b73 ; secondary_object_update_next_object
; skip_wraparound
&0c0c ad 73 0b LDA &0b73 ; secondary_object_update_next_object
&0c0f 4d 74 0b EOR &0b74 ; secondary_object_update_random_shuffle
&0c12 aa       TAX
&0c13 a0 04    LDY #&04                                             # Treat as less of a priority
; promote_secondary_object_to_primary_if_Y_slots_free
&0c15 bd 12 0b LDA &0b12,X ; secondary_objects_y                    # Leave if no secondary object in slot
&0c18 f0 33    BEQ &0c4d ; leave
&0c1a bd 32 0b LDA &0b32,X ; secondary_objects_type
&0c1d 20 62 1e JSR &1e62 ; create_new_object_if_Y_slots_free        # Returns carry clear if object created, X = slot
&0c20 b0 2b    BCS &0c4d ; leave
&0c22 bd f2 0a LDA &0af2,X ; secondary_objects_x
&0c25 99 91 08 STA &0891,Y ; objects_x
&0c28 bd 12 0b LDA &0b12,X ; secondary_objects_y
&0c2b 99 b4 08 STA &08b4,Y ; objects_y
&0c2e bd 53 0b LDA &0b53,X ; secondary_objects_energy_and_x_y_fractions
&0c31 48       PHA ; energy_and_x_y_fractions
&0c32 09 0f    ORA #&0f                                             # 8421.... energy
&0c34 99 26 09 STA &0926,Y ; objects_energy
&0c37 68       PLA ; energy_and_x_y_fractions
&0c38 0a       ASL A                                                # ....8421 -> 8421....
&0c39 0a       ASL A
&0c3a 0a       ASL A
&0c3b 0a       ASL A
&0c3c 48       PHA ; x_and_y_fractions
&0c3d 29 c0    AND #&c0                                             # ....84.. x fraction, accurate to four pixels
&0c3f 99 80 08 STA &0880,Y ; objects_x_fraction
&0c42 68       PLA ; x_and_y_fractions
&0c43 0a       ASL A                                                # ..21.... -> 21......
&0c44 0a       ASL A
&0c45 99 a3 08 STA &08a3,Y ; objects_y_fraction                     # ......12 y fraction, accurate to eight pixels
&0c48 a9 00    LDA #&00
&0c4a 9d 12 0b STA &0b12,X ; secondary_objects_y                    # Set to zero to indicate no secondary object in slot
; leave
&0c4d 60       RTS
; consider_promoting_secondary_objects_to_primary_objects
&0c4e a2 1f    LDX #&1f                                             # For each secondary object,
; consider_promoting_secondary_objects_to_primary_loop
&0c50 bd 12 0b LDA &0b12,X ; secondary_objects_y
&0c53 85 55    STA &55 ; this_object_y
&0c55 bd f2 0a LDA &0af2,X ; secondary_objects_x
&0c58 85 53    STA &53 ; this_object_x
&0c5a a0 04    LDY #&04                                             # Is the object more than four tiles offscreen?
&0c5c 86 9e    STX &9e ; tmp_x
&0c5e 20 1d 11 JSR &111d ; check_if_this_object_is_far_away         # Returns carry set if far away
&0c61 a6 9e    LDX &9e ; tmp_x
&0c63 b0 05    BCS &0c6a ; consider_next_secondary_object           # If closer, promote secondary object to primary
&0c65 a0 01    LDY #&01                                             # Treat as more of a priority
&0c67 20 15 0c JSR &0c15 ; promote_secondary_object_to_primary_if_Y_slots_free
; consider_next_secondary_object
&0c6a ca       DEX
&0c6b 10 e3    BPL &0c50 ; consider_promoting_secondary_objects_to_primary_loop
&0c6d 60       RTS

; demote_primary_object_to_secondary
&0c6e a2 1f    LDX #&1f
; find_free_slot_loop
&0c70 bd 12 0b LDA &0b12,X ; secondary_objects_y                    # Zero if no secondary object in slot
&0c73 f0 06    BEQ &0c7b ; found_free_slot
&0c75 ca       DEX
&0c76 10 f8    BPL &0c70 ; find_free_slot_loop
&0c78 4c 92 1f JMP &1f92 ; flash_background                         # Flash background if no free slot; object lost
; found_free_slot
&0c7b a5 41    LDA &41 ; this_object_type
&0c7d 9d 32 0b STA &0b32,X ; secondary_objects_type
&0c80 a5 53    LDA &53 ; this_object_x
&0c82 9d f2 0a STA &0af2,X ; secondary_objects_x
&0c85 a5 55    LDA &55 ; this_object_y
&0c87 9d 12 0b STA &0b12,X ; secondary_objects_y
&0c8a a5 4f    LDA &4f ; this_object_x_fraction                     # Store top two bits of x fraction in ....84..
&0c8c 0a       ASL A
&0c8d 2a       ROL A
&0c8e 2a       ROL A
&0c8f 29 03    AND #&03
&0c91 85 9c    STA &9c ; x_and_y_fractions
&0c93 a5 51    LDA &51 ; this_object_y_fraction                     # Store top two bits of y fraction in ......12
&0c95 0a       ASL A
&0c96 26 9c    ROL &9c ; x_and_y_fractions
&0c98 0a       ASL A
&0c99 26 9c    ROL &9c ; x_and_y_fractions
&0c9b a5 15    LDA &15 ; this_object_energy                         # Store top four bits of energy in 8421....
&0c9d 29 f0    AND #&f0
&0c9f 05 9c    ORA &9c ; x_and_y_fractions
&0ca1 9d 53 0b STA &0b53,X ; secondary_objects_energy_and_x_y_fractions
&0ca4 60       RTS

; update_object_sprite
&0ca5 a9 3f    LDA #&3f                                             # Use background colours, masked by foreground
&0ca7 8d 78 0b STA &0b78 ; sprite_foreground_or_background_mask
&0caa 20 4d 0d JSR &0d4d ; consider_replotting_sprite
&0cad a6 aa    LDX &aa ; this_object
&0caf 5e c6 08 LSR &08c6,X ; objects_flags                          # Remove previous OBJECT_FLAG_NOT_PLOTTED
&0cb2 a5 6e    LDA &6e ; offscreen_flags
&0cb4 29 05    AND #&05 ; 0101                                      # Non-zero if current sprite is offscreen in x or y
&0cb6 c9 01    CMP #&01
&0cb8 3e c6 08 ROL &08c6,X ; objects_flags                          # Set OBJECT_FLAG_NOT_PLOTTED if object is offscreen
&0cbb a9 ff    LDA #&ff                                             # Use foreground colours
&0cbd 8d 78 0b STA &0b78 ; sprite_foreground_or_background_mask
&0cc0 60       RTS

; handle_teleporting
&0cc1 a4 dd    LDY &dd ; player_object_held                         # Positive if player is holding an object
&0cc3 10 2a    BPL &0cef ; leave                                    # Player can't voluntarily teleport holding an object
&0cc5 ce 22 08 DEC &0822 ; player_teleports_remembered
&0cc8 10 07    BPL &0cd1 ; skip_using_fallback                      # Does the player have remembered positions remaining?
&0cca ee 22 08 INC &0822 ; player_teleports_remembered
&0ccd a0 04    LDY #&04                                             # If not, use the fallback position
&0ccf d0 07    BNE &0cd8 ; set_player_teleporting                   # Always branches
; skip_using_fallback
&0cd1 ce 21 08 DEC &0821 ; player_next_teleport
&0cd4 20 61 2c JSR &2c61 ; fix_player_next_teleport                 # Keep between 0 and 3
&0cd7 a8       TAY
; set_player_teleporting
&0cd8 b9 23 08 LDA &0823,Y ; player_teleports_x
&0cdb 85 14    STA &14 ; this_object_tx
&0cdd b9 28 08 LDA &0828,Y ; player_teleports_y
&0ce0 85 16    STA &16 ; this_object_ty
&0ce2 20 0d 44 JSR &440d ; play_sound_for_teleporting
; set_this_object_teleporting
&0ce5 a5 6f    LDA &6f ; this_object_flags
&0ce7 09 10    ORA #&10 ; OBJECT_FLAG_TELEPORTING
&0ce9 85 6f    STA &6f ; this_object_flags
&0ceb a9 20    LDA #&20 ; 32                                        # Update at old and new position for 16 frames each
&0ced 85 12    STA &12 ; this_object_timer
; leave
&0cef 60       RTS

; set_object_teleporting
&0cf0 b9 c6 08 LDA &08c6,Y ; objects_flags
&0cf3 09 10    ORA #&10 ; OBJECT_FLAG_TELEPORTING
&0cf5 99 c6 08 STA &08c6,Y ; objects_flags
&0cf8 a9 20    LDA #&20 ; 32                                        # Update at old and new position for 16 frames each
&0cfa 99 56 09 STA &0956,Y ; objects_timer
&0cfd 60       RTS

; reduce_sprite_if_teleporting                                      # Called with X = 3 for previous sprite
;                                                                   #             X = 2 for current sprite
&0cfe b5 6d    LDA &6d,X ; this_object_flags - 2
&0d00 29 10    AND #&10 ; OBJECT_FLAG_TELEPORTING
&0d02 f0 46    BEQ &0d4a ; to_skip_calculating_sizes_and_flips      # Is the object teleporting?
; reduce_sprite_if_teleporting_loop                                 # Loop through X = 3 for y, X = 1 for x if previous sprite
&0d04 8a       TXA                                                  #           or X = 2 for y, X = 0 for x if current sprite
&0d05 29 01    AND #&01
&0d07 a8       TAY
&0d08 b9 12 00 LDA &0012,Y ; this_object_timer (teleport timer)     # Use timer to determine amount of reduction
&0d0b e0 02    CPX #&02
&0d0d b0 06    BCS &0d15 ; is_y
; is_x                                                              # Offset timer if reducing in x
&0d0f 85 9c    STA &9c ; timer
&0d11 4a       LSR A
&0d12 4a       LSR A
&0d13 65 9c    ADC &9c ; timer
; is_y
&0d15 29 07    AND #&07
&0d17 a8       TAY
&0d18 b5 4b    LDA &4b,X ; this_object_sprite_width
; calculate_size_reduction_loop
&0d1a 4a       LSR A
&0d1b 88       DEY
&0d1c 10 fc    BPL &0d1a ; calculate_size_reduction_loop
&0d1e 2a       ROL A
&0d1f 85 9c    STA &9c ; size_reduction
&0d21 b5 4b    LDA &4b,X ; this_object_sprite_width
&0d23 38       SEC
&0d24 e5 9c    SBC &9c ; size_reduction
&0d26 4a       LSR A                                                # (size - reduction) / 2
&0d27 3d a1 0b AND &0ba1,X ; fraction_to_pixel_rounding_table       # Reduce to a whole number of pixels
&0d2a 48       PHA ; offset          
&0d2b 18       CLC
&0d2c 75 5f    ADC &5f,X ; this_object_sprite_spritesheet_x         # Centre reduced sprite on spritesheet
&0d2e 69 00    ADC #&00
&0d30 95 5f    STA &5f,X ; this_object_sprite_spritesheet_x
&0d32 68       PLA ; offset
&0d33 75 4f    ADC &4f,X ; this_object_x_fraction                   # Centre reduced sprite on screen
&0d35 95 4f    STA &4f,X ; this_object_x_fraction
&0d37 90 02    BCC &0d3b ; skip_overflow
&0d39 f6 53    INC &53,X ; this_object_x
; skip_overflow
&0d3b a5 9c    LDA &9c ; size_reduction
&0d3d 3d a1 0b AND &0ba1,X ; fraction_to_pixel_rounding_table
&0d40 95 4b    STA &4b,X ; this_object_sprite_width
&0d42 ca       DEX                                                  # Repeat for x
&0d43 ca       DEX
&0d44 10 be    BPL &0d04 ; reduce_sprite_if_teleporting_loop
&0d46 e8       INX                                                  # Restore X to original value
&0d47 e8       INX
&0d48 e8       INX
&0d49 e8       INX
; to_skip_calculating_sizes_and_flips
&0d4a 4c 86 0d JMP &0d86 ; skip_calculating_sizes_and_flips

; consider_replotting_sprite
&0d4d a2 03    LDX #&03
&0d4f 06 6e    ASL &6e ; offscreen_flags                            # If top nibble set, suppress calculating variables if scrolling screen
; calculate_sprite_variables_loop                                   # This code is run four times:
;                                                                   #     X = 3 : previous sprite, y, height, vertical
;                                                                   #         2 : current sprite, y, height, vertical
;                                                                   #         1 : previous sprite, x, width, horizontal
;                                                                   #         0 : current sprite, x, width, horizontal
&0d51 b0 78    BCS &0dcb ; set_bit_in_offscreen_flags
&0d53 e0 02    CPX #&02
&0d55 90 2f    BCC &0d86 ; skip_calculating_sizes_and_flips         # For X = 3 and 2, calculate sizes and flips
; calculate_sprite_sizes_and_flips
&0d57 b4 73    LDY &73,X ; this_object_sprite - 2
&0d59 b9 89 5e LDA &5e89,Y ; sprites_height_and_vertical_flip_table
&0d5c 4a       LSR A                                                # Set carry if sprite is vertically flipped
&0d5d b9 0c 5e LDA &5e0c,Y ; sprites_width_and_horizontal_flip_table
&0d60 6a       ROR A                                                # Set carry if sprite if horizontally flipped
&0d61 29 80    AND #&80
&0d63 6a       ROR A                                                # Set &80 if horizontally flipped, &40 if vertically flipped
&0d64 55 6f    EOR &6f,X ; this_object_flip - 2
&0d66 95 61    STA &61,X ; this_object_sprite_x_flip - 2            # &80 set if horizontally flipped
&0d68 0a       ASL A
&0d69 95 63    STA &63,X ; this_object_sprite_y_flip - 2            # &80 set if vertically flipped
&0d6b b9 0c 5e LDA &5e0c,Y ; sprites_width_and_horizontal_flip_table
&0d6e 29 f0    AND #&f0                                             # 8421.... width of sprite in pixels, minus one
&0d70 95 49    STA &49,X ; this_object_sprite_width - 2
&0d72 b9 89 5e LDA &5e89,Y ; sprites_height_and_vertical_flip_table
&0d75 29 f8    AND #&f8                                             # 84218... height of sprite in rows, minus one
&0d77 95 4b    STA &4b,X ; this_object_sprite_height - 2
&0d79 b9 06 5f LDA &5f06,Y ; sprites_spritesheet_x_table
&0d7c 95 5d    STA &5d,X ; this_object_sprite_spritesheet_x - 2
&0d7e b9 83 5f LDA &5f83,Y ; sprites_spritesheet_y_table
&0d81 95 5f    STA &5f,X ; this_object_sprite_spritesheet_y - 2
&0d83 4c fe 0c JMP &0cfe ; reduce_sprite_if_teleporting             # Returns to &0d86
; skip_calculating_sizes_and_flips
&0d86 b5 4f    LDA &4f,X ; this_object_x_fraction
&0d88 3d a1 0b AND &0ba1,X ; fraction_to_pixel_rounding_table       # Round down to pixel
&0d8b 38       SEC
&0d8c fd 91 0b SBC &0b91,X ; screen_start_x_fraction
&0d8f 95 57    STA &57,X ; this_object_screen_x_fraction            # Calculate object position relative to screen top left
&0d91 b5 53    LDA &53,X ; this_object_x
&0d93 fd 95 0b SBC &0b95,X ; screen_start_x
&0d96 95 5b    STA &5b,X ; this_object_screen_x
&0d98 b5 57    LDA &57,X ; this_object_screen_x_fraction
&0d9a 18       CLC
&0d9b 75 4b    ADC &4b,X ; this_object_sprite_width
&0d9d 85 8f    STA &8f ; right_or_bottom_fraction                   # then relative position of right or bottom of sprite
&0d9f b5 5b    LDA &5b,X ; this_object_screen_x
&0da1 69 00    ADC #&00
&0da3 85 90    STA &90 ; right_or_bottom
&0da5 30 5f    BMI &0e06 ; is_offscreen                             # Branch if sprite is beyond left or top of screen
&0da7 b0 47    BCS &0df0 ; crop_sprite_to_left_or_top               # Branch if sprite partly beyond left or top of screen
&0da9 a5 8f    LDA &8f ; right_or_bottom_fraction
&0dab 38       SEC
&0dac fd 99 0b SBC &0b99,X ; screen_size_x_fraction
&0daf 85 8f    STA &8f ; right_or_bottom_fraction
&0db1 a5 90    LDA &90 ; right_or_bottom
&0db3 fd 9d 0b SBC &0b9d,X ; screen_size_x
&0db6 85 90    STA &90 ; right_or_bottom
&0db8 f0 19    BEQ &0dd3 ; crop_sprite_to_right_or_bottom
&0dba 10 0f    BPL &0dcb ; beyond_right_or_bottom                   # Branch if sprite is beyond right or bottom of screen
; is_onscreen
&0dbc b5 57    LDA &57,X ; this_object_screen_x_fraction
; is_onscreen_using_A_as_x_fraction
&0dbe 18       CLC
&0dbf 7d 89 0b ADC &0b89,X ; screen_scrolling_offset_x_fraction     # Apply offset to previous sprites to account for
&0dc2 95 57    STA &57,X ; this_object_screen_x_fraction            # scrolling if scrolling left or up (is always zero
&0dc4 b5 5b    LDA &5b,X ; this_object_screen_x                     # for current sprites or if scrolling right or down)
&0dc6 7d 8d 0b ADC &0b8d,X ; screen_scrolling_offset_x
&0dc9 95 5b    STA &5b,X ; this_object_screen_x
; beyond_right_or_bottom
; set_bit_in_offscreen_flags
&0dcb 26 6e    ROL &6e ; offscreen_flags                            # Set a bit if sprite entirely beyond edge of screen:
;                                                                   #     8... set if previous sprite offscreen vertically
;                                                                   #     .4.. set if current sprite offscreen vertically
;                                                                   #     ..2. set if previous sprite offscreen horizontally
;                                                                   #     ...1 set if current sprite offscreen horizontally
&0dcd ca       DEX
&0dce 30 39    BMI &0e09 ; check_if_object_needs_replotting
&0dd0 4c 51 0d JMP &0d51 ; calculate_sprite_variables_loop

; crop_sprite_to_right_or_bottom
&0dd3 a5 8f    LDA &8f ; right_or_bottom_fraction
&0dd5 f5 4b    SBC &4b,X ; this_object_sprite_width
&0dd7 b0 f2    BCS &0dcb ; set_bit_in_offscreen_flags               # Branch if sprite is beyond right or bottom of screen
&0dd9 5d a1 0b EOR &0ba1,X ; fraction_to_pixel_rounding_table
&0ddc 95 4b    STA &4b,X ; this_object_sprite_width                 # Limit sprite size to accommodate clipping
&0dde b5 63    LDA &63,X ; this_object_sprite_x_flip                # &80 set if flipped
&0de0 10 da    BPL &0dbc ; is_onscreen
; is_flipped
&0de2 a5 8f    LDA &8f ; right_or_bottom_fraction
&0de4 38       SEC
&0de5 fd a1 0b SBC &0ba1,X ; fraction_to_pixel_rounding_table
&0de8 75 5f    ADC &5f,X ; this_object_sprite_spritesheet_x
&0dea 69 00    ADC #&00
&0dec 95 5f    STA &5f,X ; this_object_sprite_spritesheet_x         # Fix start on spritesheet to accommodate clipping
&0dee 90 cc    BCC &0dbc ; is_onscreen                              # Always branches

; crop_sprite_to_left_or_top
&0df0 a5 8f    LDA &8f ; right_or_bottom_fraction
&0df2 95 4b    STA &4b,X ; this_object_sprite_width                 # Limit sprite size to accommodate clipping
&0df4 b5 63    LDA &63,X ; this_object_sprite_x_flip                # &80 set if flipped
&0df6 30 08    BMI &0e00 ; skip_fixing_spritesheet
; is_not_flipped
&0df8 b5 5f    LDA &5f,X ; this_object_sprite_spritesheet_x
&0dfa f5 57    SBC &57,X ; this_object_screen_x_fraction
&0dfc 69 00    ADC #&00
&0dfe 95 5f    STA &5f,X ; this_object_sprite_spritesheet_x         # Fix start on spritesheet to accommodate clipping
; skip_fixing_spritesheet
&0e00 a9 00    LDA #&00
&0e02 95 5b    STA &5b,X ; this_object_screen_x                     # Set sprite to start at edge of screen
&0e04 f0 b8    BEQ &0dbe ; is_onscreen_using_A_as_x_fraction        # Always branches
; is_offscreen
&0e06 38       SEC                                                  # Set carry to indicate sprite is offscreen
&0e07 b0 c2    BCS &0dcb ; set_bit_in_offscreen_flags               # Always branches

; check_if_object_needs_replotting
&0e09 a5 6e    LDA &6e ; offscreen_flags
&0e0b 29 0f    AND #&0f ; 1111
&0e0d f0 0b    BEQ &0e1a ; check_if_object_has_changed              # Branch if object is and was onscreen
&0e0f 29 05    AND #&05 ; 0101
&0e11 f0 70    BEQ &0e83 ; to_replot_sprite                         # Replot object if previously offscreen, now onscreen
&0e13 a5 6e    LDA &6e ; offscreen_flags
&0e15 29 0a    AND #&0a ; 1010
&0e17 f0 6a    BEQ &0e83 ; to_replot_sprite                         # Replot object if previously onscreen, now offscreen
&0e19 60       RTS                                                  # Nothing to do if object is and was offscreen
; check_if_object_has_changed                                       # Check if the object has changed
&0e1a a5 51    LDA &51 ; this_object_y_fraction
&0e1c 45 52    EOR &52 ; this_object_previous_y_fraction
&0e1e 29 f8    AND #&f8                                             # Rounding to pixels vertically
&0e20 d0 61    BNE &0e83 ; to_replot_sprite
&0e22 a5 4f    LDA &4f ; this_object_x_fraction
&0e24 45 50    EOR &50 ; this_object_previous_x_fraction
&0e26 29 f0    AND #&f0                                             # and horizontally
&0e28 d0 59    BNE &0e83 ; to_replot_sprite
&0e2a a5 55    LDA &55 ; this_object_y
&0e2c 45 56    EOR &56 ; this_object_previous_y
&0e2e d0 53    BNE &0e83 ; to_replot_sprite
&0e30 a5 53    LDA &53 ; this_object_x
&0e32 45 54    EOR &54 ; this_object_previous_x
&0e34 d0 4d    BNE &0e83 ; to_replot_sprite                         # Replot if the position has changed
&0e36 a5 75    LDA &75 ; this_object_sprite
&0e38 45 76    EOR &76 ; this_object_previous_sprite
&0e3a d0 47    BNE &0e83 ; to_replot_sprite                         # Replot if the sprite has changed
&0e3c a5 73    LDA &73 ; this_object_palette
&0e3e 45 74    EOR &74 ; this_object_previous_palette
&0e40 d0 41    BNE &0e83 ; to_replot_sprite                         # Replot if the palette has changed
&0e42 a5 71    LDA &71 ; this_object_flip
&0e44 45 72    EOR &72 ; this_object_previous_flip
&0e46 d0 3b    BNE &0e83 ; to_replot_sprite                         # Replot if the flip has changed
&0e48 a5 6f    LDA &6f ; this_object_flags
&0e4a 05 70    ORA &70 ; this_object_previous_flags
&0e4c 29 10    AND #&10 ; OBJECT_FLAG_TELEPORTING
&0e4e d0 33    BNE &0e83 ; to_replot_sprite                         # Replot if the object is or was teleporting
; check_for_scrolling_onto_in_x
&0e50 a5 4c    LDA &4c ; this_object_previous_sprite_width
&0e52 c5 4b    CMP &4b ; this_object_sprite_width
&0e54 b0 30    BCS &0e86 ; check_for_scrolling_onto_in_y            # If the previous sprite was smaller in x,
&0e56 a5 63    LDA &63 ; this_object_sprite_x_flip                  # i.e. the object is scrolling onto the screen,
&0e58 45 cc    EOR &cc ; screen_scrolling_sign_x                    # consider plotting only the newly displayed part
&0e5a 30 0a    BMI &0e66 ; not_right
&0e5c a5 5f    LDA &5f ; this_object_sprite_spritesheet_x
&0e5e 65 4c    ADC &4c ; this_object_previous_sprite_width
&0e60 69 10    ADC #&10
&0e62 69 00    ADC #&00
&0e64 85 5f    STA &5f ; this_object_sprite_spritesheet_x
; not_right
&0e66 a5 4b    LDA &4b ; this_object_sprite_width
&0e68 38       SEC
&0e69 e5 4c    SBC &4c ; this_object_previous_sprite_width
&0e6b e9 10    SBC #&10
&0e6d 85 4b    STA &4b ; this_object_sprite_width
&0e6f a5 cc    LDA &cc ; screen_scrolling_sign_x
&0e71 30 43    BMI &0eb6 ; plot_sprite_without_unplotting
&0e73 a5 4c    LDA &4c ; this_object_previous_sprite_width
&0e75 18       CLC
&0e76 69 10    ADC #&10
&0e78 65 57    ADC &57 ; this_object_screen_x_fraction
&0e7a 85 57    STA &57 ; this_object_screen_x_fraction
&0e7c 90 38    BCC &0eb6 ; plot_sprite_without_unplotting
&0e7e e6 5b    INC &5b ; this_object_screen_x
&0e80 4c b6 0e JMP &0eb6 ; plot_sprite_without_unplotting
; to_replot_sprite
&0e83 4c ba 0e JMP &0eba ; replot_sprite
; check_for_scrolling_onto_in_y
&0e86 a5 4e    LDA &4e ; this_object_previous_sprite_height
&0e88 c5 4d    CMP &4d ; this_object_sprite_height                  # If the previous sprite is smaller in y,
&0e8a b0 3f    BCS &0ecb ; leave                                    # i.e. the object is scrolling onto the screen,
&0e8c a5 65    LDA &65 ; this_object_sprite_y_flip                  # consider plotting only the newly displayed part
&0e8e 45 ce    EOR &ce ; screen_scrolling_sign_y
&0e90 30 0a    BMI &0e9c ; not_bottom
&0e92 a5 61    LDA &61 ; this_object_sprite_spritesheet_y
&0e94 65 4e    ADC &4e ; this_object_previous_sprite_height
&0e96 69 08    ADC #&08
&0e98 69 00    ADC #&00
&0e9a 85 61    STA &61 ; this_object_sprite_spritesheet_y
; not_bottom
&0e9c a5 4d    LDA &4d ; this_object_sprite_height
&0e9e 38       SEC
&0e9f e5 4e    SBC &4e ; this_object_previous_sprite_height
&0ea1 e9 08    SBC #&08
&0ea3 85 4d    STA &4d ; this_object_sprite_height
&0ea5 a5 ce    LDA &ce ; screen_scrolling_sign_y
&0ea7 30 0d    BMI &0eb6 ; plot_sprite_without_unplotting
&0ea9 a5 4e    LDA &4e ; this_object_previous_sprite_height
&0eab 18       CLC
&0eac 69 08    ADC #&08
&0eae 65 59    ADC &59 ; this_object_screen_y_fraction
&0eb0 85 59    STA &59 ; this_object_screen_y_fraction
&0eb2 90 02    BCC &0eb6 ; plot_sprite_without_unplotting
&0eb4 e6 5d    INC &5d ; this_object_screen_y
; plot_sprite_without_unplotting
&0eb6 a9 0a    LDA #&0a ; 1010
&0eb8 85 6e    STA &6e ; offscreen_flags                            # Set bits to indicate sprite was previously offscreen
; replot_sprite
&0eba a9 02    LDA #&02
&0ebc 85 ae    STA &ae ; use_previous_variables                     # Will become 1 to use previous variables at &0ec6
&0ebe a9 00    LDA #&00 ; KK
&0ec0 85 00    STA &00 ; colour_0_pixel_value                       # Colour 0 is always black
&0ec2 48       PHA ; value not used                                 # Reserve a byte on stack for near padding
&0ec3 ba       TSX
&0ec4 86 6a    STX &6a ; stack_pointer_prior_to_pushing_sprite_data
; replot_sprite_loop
&0ec6 c6 ae    DEC &ae ; use_previous_variables
&0ec8 10 02    BPL &0ecc ; plot_or_unplot_sprite                    # Unplot previous sprite, then plot current sprite
&0eca 68       PLA ; value not used
; leave
&0ecb 60       RTS

; plot_or_unplot_sprite
&0ecc a6 ae    LDX &ae ; use_previous_variables                     # 0 if using current, 1 if using previous variables
&0ece a5 6e    LDA &6e ; offscreen_flags
&0ed0 3d a5 0b AND &0ba5,X ; offscreen_flags_to_check_table         # Check if the sprite is offscreen in either x or y
&0ed3 d0 f1    BNE &0ec6 ; replot_sprite_loop                       # If so, skip unplotting or plotting
&0ed5 b5 5f    LDA &5f,X ; this_object_sprite_spritesheet_x
&0ed7 0a       ASL A
&0ed8 69 00    ADC #&00                                             # 8421.421 -> 421.4218
&0eda 0a       ASL A
&0edb 69 00    ADC #&00                                             #          -> 21.42184
&0edd 29 1f    AND #&1f                                             #          -> ...42184, i.e. column in sheet DIV 4
&0edf 85 9c    STA &9c ; sprite_data_offset_low                     # i.e. x byte offset of sprite in four colour data
; convert_palette_to_sixteen_colour_pixel_values                    # Convert palette to sixteen colour pixel values
&0ee1 b5 73    LDA &73,X ; this_object_palette
&0ee3 4a       LSR A                                                # Top nibble of palette sets colour 3
&0ee4 4a       LSR A
&0ee5 4a       LSR A
&0ee6 4a       LSR A
&0ee7 a8       TAY
&0ee8 b9 48 1e LDA &1e48,Y ; colour_3_pixel_values_table
&0eeb 29 55    AND #&55 ; right pixel
&0eed 85 11    STA &11 ; colour_3_right_pixel_value
&0eef 0a       ASL A                                                # Convert to left pixel
&0ef0 85 22    STA &22 ; colour_3_left_pixel_value
&0ef2 b5 73    LDA &73,X ; this_object_palette
&0ef4 29 0f    AND #&0f                                             # Bottom nibble of palette sets colours 1 and 2
&0ef6 a8       TAY
&0ef7 b9 79 0b LDA &0b79,Y ; colours_1_and_2_pixel_values_table
&0efa 2d 78 0b AND &0b78 ; sprite_foreground_or_background_mask
&0efd a8       TAY ; colours_1_and_2_pixel_value
&0efe 29 55    AND #&55 ; right pixel                               # Right pixel of table entry sets colour 1
&0f00 85 01    STA &01 ; colour_1_right_pixel_value
&0f02 0a       ASL A                                                # Convert to left pixel
&0f03 85 02    STA &02 ; colour_1_left_pixel_value
&0f05 98       TYA ; colours_1_and_2_pixel_value
&0f06 29 aa    AND #&aa ; left pixel                                # Left pixel of table entry sets colour 2
&0f08 85 20    STA &20 ; colour_2_left_pixel_value
&0f0a 4a       LSR A                                                # Convert to right pixel
&0f0b 85 10    STA &10 ; colour_2_right_pixel_value
; determine_pixel_order                                             # Determine whether pixel order needs to be swapped
&0f0d b5 63    LDA &63,X ; this_object_sprite_x_flip                # &80 set if flipping horizontally
&0f0f 85 65    STA &65 ; flip
&0f11 4a       LSR A                                                # 84...... -> ..84...
&0f12 4a       LSR A                                                # i.e. &10 set if flipping horizontally
&0f13 4a       LSR A
&0f14 35 4b    AND &4b,X ; this_object_sprite_width                 # &10 set if sprite is an odd number of pixels wide
&0f16 55 57    EOR &57,X ; this_object_screen_x_fraction            # &10 set if sprite starts on an odd pixel on screen
&0f18 55 5f    EOR &5f,X ; this_object_sprite_spritesheet_x         # &10 set if sprite starts on an odd pixel in spritesheet
&0f1a 29 10    AND #&10
&0f1c f0 18    BEQ &0f36 ; skip_swapping_pixel_values
; swap_pixel_values                                                 # Swap left and right pixel values
&0f1e a5 02    LDA &02 ; colour_1_left_pixel_value
&0f20 a4 01    LDY &01 ; colour_1_right_pixel_value
&0f22 85 01    STA &01 ; colour_1_right_pixel_value
&0f24 84 02    STY &02 ; colour_1_left_pixel_value
&0f26 a5 20    LDA &20 ; colour_2_left_pixel_value
&0f28 a4 10    LDY &10 ; colour_2_right_pixel_value
&0f2a 85 10    STA &10 ; colour_2_right_pixel_value
&0f2c 84 20    STY &20 ; colour_2_left_pixel_value
&0f2e a5 22    LDA &22 ; colour_3_left_pixel_value
&0f30 a4 11    LDY &11 ; colour_3_right_pixel_value
&0f32 85 11    STA &11 ; colour_3_right_pixel_value
&0f34 84 22    STY &22 ; colour_3_left_pixel_value
; skip_swapping_pixel_values
&0f36 a9 20    LDA #&20 ; &0020                                     # Move one row down sprite sheet
&0f38 a0 00    LDY #&00
&0f3a 24 65    BIT &65 ; flip                                       # &40 set if flipping vertically
&0f3c 70 03    BVS &0f41 ; set_sprite_data_spritesheet_between_rows
; not_flipping_vertically
&0f3e a9 e0    LDA #&e0 ; &ffe0                                     # Move one row up sprite sheet
&0f40 88       DEY ; &ff
; set_sprite_data_spritesheet_between_rows
&0f41 8d 7d 10 STA &107d ; sprite_data_spritesheet_between_rows_low
&0f44 8c 85 10 STY &1085 ; sprite_data_spritesheet_between_rows_high
; calculate_sprite_data_address                                     # Calculate the address for the bottom of the sprite
&0f47 b5 4d    LDA &4d,X ; this_object_sprite_height                # 84218... height of sprite in rows, minus one
&0f49 50 02    BVC &0f4d ; not_vertically_flipped                   # &40 set if flipping vertically (at &0f3a)
&0f4b a9 00    LDA #&00                                             # Use top of sprite if vertically flipped
; not_vertically_flipped
&0f4d 18       CLC
&0f4e 75 61    ADC &61,X ; this_object_sprite_spritesheet_y         # 84218.21
&0f50 69 00    ADC #&00
&0f52 0a       ASL A
&0f53 69 00    ADC #&00                                             # 84218.21 -> 4218.218
&0f55 0a       ASL A
&0f56 69 00    ADC #&00                                             #          -> 218.2184
&0f58 85 9d    STA &9d ; sprite_data_offset_high
&0f5a 29 e0    AND #&e0                                             #          -> 218..... i.e. row in sheet MOD 8 * &20
&0f5c 05 9c    ORA &9c ; sprite_data_offset_low                     # Add column component to row component
&0f5e 69 ec    ADC #&ec ; &53ec = sprite_data
&0f60 8d 23 10 STA &1023 ; sprite_data_address_low
&0f63 a5 9d    LDA &9d ; sprite_data_offset_high
&0f65 29 0f    AND #&0f                                             # 218.2184 -> ....2184 i.e. row in sheet DIV 8
&0f67 69 53    ADC #&53
&0f69 8d 24 10 STA &1024 ; sprite_data_address_high
; calculate_widths_for_row_plotting
&0f6c b5 57    LDA &57,X ; this_object_screen_x_fraction
&0f6e 29 10    AND #&10 ; 1 pixel
&0f70 75 4b    ADC &4b,X ; this_object_sprite_width
&0f72 85 99    STA &99 ; last_pixel_oddness
&0f74 6a       ROR A
&0f75 29 f0    AND #&f0                                             # Round down to number of pixels
&0f77 4a       LSR A                                                # Convert to number of sixteen colour bytes * 8
&0f78 85 a0    STA &a0 ; column_offset_for_initial_row_offset
&0f7a b5 5f    LDA &5f,X ; this_object_sprite_spritesheet_x
&0f7c 29 30    AND #&30                                             # Offset in pixels in four colour byte
&0f7e 75 4b    ADC &4b,X ; this_object_sprite_width
&0f80 6a       ROR A                                                # 8421.... -> C8421...
&0f81 4a       LSR A                                                #          -> .C8421..
&0f82 4a       LSR A                                                #          -> ..C8421.
&0f83 4a       LSR A                                                #          -> ...C8421
&0f84 a8       TAY                                                  # Y = number of pixels needed
&0f85 4a       LSR A                                                #          -> ....C842
&0f86 4a       LSR A                                                #          -> .....C84
&0f87 85 69    STA &69 ; sprite_bytes_per_row                       # i.e. number of four colour bytes needed
&0f89 b5 4b    LDA &4b,X ; this_object_sprite_width
&0f8b 4a       LSR A                                                # 8421.... -> .8421...
&0f8c 4a       LSR A                                                #          -> ..8421..
&0f8d 4a       LSR A                                                #          -> ...8421.
&0f8e 4a       LSR A                                                #          -> ....8421
&0f8f 85 9d    STA &9d ; width_in_four_colour_bytes
&0f91 98       TYA                                                  # Y = number of pixels needed
&0f92 29 03    AND #&03
&0f94 18       CLC
&0f95 65 6a    ADC &6a ; stack_pointer_prior_to_pushing_sprite_data
&0f97 e9 01    SBC #&01
&0f99 8d 52 10 STA &1052 ; stack_near_padding_address_low           # Set near end of stack pixel data
&0f9c aa       TAX
&0f9d e8       INX
&0f9e e5 9d    SBC &9d ; width_in_four_colour_bytes
&0fa0 e9 02    SBC #&02
&0fa2 8d 55 10 STA &1055 ; stack_far_padding_address_low            # Set far end of stack pixel data
&0fa5 a0 ca    LDY #&ca ; DEX
&0fa7 24 65    BIT &65 ; flip                                       # &80 set if flipping horizontally
&0fa9 10 04    BPL &0faf ; not_flipped_horizontally
&0fab aa       TAX
&0fac ca       DEX
&0fad a0 e8    LDY #&e8 ; INX
; not_flipped_horizontally
&0faf a5 99    LDA &99 ; last_pixel_oddness
&0fb1 29 10    AND #&10 ; 1 pixel                                   # Is the last pixel of the sprite byte aligned?
&0fb3 f0 04    BEQ &0fb9 ; set_initial_stack_offset_and_stack_direction
&0fb5 8c b8 0f STY &0fb8 ; tweak_initial_stack_offset_for_row_opcode
; tweak_initial_stack_offset_for_row_opcode
&0fb8 e8       INX                                                  # If not, add another byte to accommodate it
#     or       DEX if not flipping horizontally
&0fb9 86 6b    STX &6b ; initial_stack_offset_for_row
&0fbb 8c 5c 10 STY &105c ; plot_row_of_sprite_from_stack_direction_opcode_one
&0fbe 8c 60 10 STY &1060 ; plot_row_of_sprite_from_stack_direction_opcode_two
&0fc1 a6 ae    LDX &ae ; use_previous_variables
&0fc3 b5 4d    LDA &4d,X ; this_object_sprite_height
&0fc5 4a       LSR A                                                # 84218... -> .84218..
&0fc6 4a       LSR A                                                #          -> ..84218.
&0fc7 4a       LSR A                                                #          -> ...84218
&0fc8 85 6c    STA &6c ; rows_remaining
; calculate_screen_address
&0fca b5 59    LDA &59,X ; this_object_screen_y_fraction            # 84218...
&0fcc 18       CLC
&0fcd 75 4d    ADC &4d,X ; this_object_sprite_height                # 84218...
&0fcf 85 8f    STA &8f ; screen_address_low
&0fd1 b5 5d    LDA &5d,X ; this_object_screen_y
&0fd3 69 00    ADC #&00
&0fd5 85 90    STA &90 ; screen_address_high
&0fd7 20 58 1f JSR &1f58 ; consider_setting_crtc_start_address
&0fda a5 8f    LDA &8f ; screen_address_low
&0fdc 46 90    LSR &90 ; screen_address_high                        # 84218... -> h84218..
&0fde 6a       ROR A
&0fdf 46 90    LSR &90 ; screen_address_high                        #          -> hh84218.
&0fe1 6a       ROR A
&0fe2 46 90    LSR &90 ; screen_address_high                        #          -> hhh84218
&0fe4 6a       ROR A
&0fe5 a8       TAY
&0fe6 29 07    AND #&07                                             #          -> .....218 i.e. row in group
&0fe8 05 a0    ORA &a0 ; column_offset_for_initial_row_offset       # Add column component
&0fea 85 a1    STA &a1 ; initial_screen_offset_for_row              # Set offset for initial row in group
&0fec 09 07    ORA #&07
&0fee 85 a0    STA &a0 ; second_screen_offset_for_row               # Set offset for first row in next group
&0ff0 b5 57    LDA &57,X ; this_object_screen_x_fraction
&0ff2 29 e0    AND #&e0                                             # Round x to &20 fraction (one byte)
&0ff4 65 b2    ADC &b2 ; scaled_screen_start_offset_low             # &20 fraction = 8 byte column of two pixels
&0ff6 85 8f    STA &8f ; screen_address_low                         # so divide by 4 (&20 / 8) to give screen address
&0ff8 98       TYA
&0ff9 29 f8    AND #&f8                                             # hhh84218 -> hhh84... i.e. group
&0ffb 55 5b    EOR &5b,X ; this_object_screen_x                     # Add column component to group component
&0ffd 65 b3    ADC &b3 ; scaled_screen_start_offset_high
&0fff 6a       ROR A                                                # hhh84... -> Chhh84..
&1000 66 8f    ROR &8f ; screen_address_low
&1002 4a       LSR A                                                #          -> .Chhh84.
&1003 66 8f    ROR &8f ; screen_address_low
&1005 09 60    ORA #&60 ; &6000 = screen_memory                     # Wrap around end of screen memory
&1007 85 90    STA &90 ; screen_address_high
; plot_sprite_row_loop_after_wraparound_check
&1009 c9 7f    CMP #&7f
&100b d0 13    BNE &1020 ; plot_sprite_row_loop                     # Will the plotting cross the end of screen memory?
&100d a5 8f    LDA &8f ; screen_address_low
&100f 18       CLC
&1010 65 a1    ADC &a1 ; initial_screen_offset_for_row
&1012 90 0c    BCC &1020 ; plot_sprite_row_loop
; prepare_to_cross_end_of_screen_memory                             # If the row will cross the end of screen memory,
&1014 85 67    STA &67 ; initial_stack_offset_for_row_after_wraparound
&1016 a9 50    LDA #&50 ; BVC &109c ; handle_wraparound_row         # alter the flow of the code to handle wraparound
&1018 8d 70 10 STA &1070 ; to_handle_wraparound_row_opcode          # by splitting the row into two sections; first, from
&101b a9 2a    LDA #&2a                                             # &7f.. to &7fff, then a second from &6000 - &60..
&101d 8d 71 10 STA &1071 ; to_handle_wraparound_row_opcode + 1
; plot_sprite_row_loop                                              # Convert sprite data to sixteen colours and push to stack
&1020 a4 69    LDY &69 ; sprite_bytes_per_row
; plot_row_of_sprite_to_stack_loop                                  # For each byte of four colour sprite data,
&1022 b9 ff ff LDA &ffff,Y
#     actually LDA sprite_data_address,Y                            # Get byte of four colour sprite data
&1025 aa       TAX
&1026 29 11    AND #&11 ; 0003                                      # Get the first pixel from the right
&1028 8d 2c 10 STA &102c ; colour_translation_one
&102b a5 ff    LDA &ff
#     actually LDA colour_translation_one                           # Convert to a sixteen colour pixel
#     i.e.     LDA &00 ; colour_0_pixel_value       if colour 0
#              LDA &01 ; colour_1_right_pixel_value if colour 1
#              LDA &10 ; colour_2_right_pixel_value if colour 2
#              LDA &11 ; colour_3_right_pixel_value if colour 3
&102d 48       PHA ; sixteen colour right pixel                     # Push to stack
&102e 8a       TXA
&102f 29 22    AND #&22 ; 0030                                      # Get the first pixel from the right
&1031 8d 35 10 STA &1035 ; colour_translation_two
&1034 a5 ff    LDA &ff
#     actually LDA colour_translation_two                           # Convert to a sixteen colour pixel
#     i.e.     LDA &00 ; colour_0_pixel_value      if colour 0
#              LDA &02 ; colour_1_left_pixel_value if colour 1
#              LDA &20 ; colour_2_left_pixel_value if colour 2
#              LDA &22 ; colour_3_left_pixel_value if colour 3
&1036 48       PHA ; sixteen colour left pixel                      # Push to stack
&1037 8a       TXA
&1038 4a       LSR A                                                # 84218421 -> ..842184
&1039 4a       LSR A                                                # i.e. move pixels three and four to one and two
&103a aa       TAX
&103b 29 11    AND #&11 ; 0003                                      # Get the third (now first) pixel from the right
&103d 8d 41 10 STA &1041 ; colour_translation_three
&1040 a5 ff    LDA &ff
#     actually LDA colour_translation_three                         # Convert to a sixteen colour pixel
#     i.e.     LDA &00 ; colour_0_pixel_value       if colour 0
#              LDA &01 ; colour_1_right_pixel_value if colour 1
#              LDA &10 ; colour_2_right_pixel_value if colour 2
#              LDA &11 ; colour_3_right_pixel_value if colour 3
&1042 48       PHA ; sixteen colour right pixel                     # Push to stack
&1043 8a       TXA
&1044 29 22    AND #&22 ; 0030                                      # Get the fourth (now second) pixel from the right
&1046 8d 4a 10 STA &104a ; colour_translation_four
&1049 a5 ff    LDA &ff
#     actually LDA colour_translation_four                          # Convert to a sixteen colour pixel
#     i.e.     LDA &00 ; colour_0_pixel_value      if colour 0
#              LDA &02 ; colour_1_left_pixel_value if colour 1
#              LDA &20 ; colour_2_left_pixel_value if colour 2
#              LDA &22 ; colour_3_left_pixel_value if colour 3
&104b 48       PHA ; sixteen colour left pixel                      # Push to stack
&104c 88       DEY
&104d 10 d3    BPL &1022 ; plot_row_of_sprite_to_stack_loop         # i.e. push four bytes of sixteen colour data
&104f 48       PHA                                                  # Reserve a byte on stack for far padding; this isn't
;                                                                   # strictly needed, as stack pointer will be reset
&1050 c8       INY ; 0
&1051 8c ff 01 STY &01ff
#     actually STY stack_near_padding_address                       # Push black pixel at near end of stack
&1054 8c ff 01 STY &01ff
#     actually STY stack_far_padding_address                        # Push black pixel at far end of stack
; plot_row_of_sprite_from_stack                                     # Plot sprite data from stack to screen
&1057 38       SEC
&1058 a4 a1    LDY &a1 ; initial_screen_offset_for_row              # Starting from the right of the screen,
; plot_row_of_sprite_from_stack_from_Y
&105a a6 6b    LDX &6b ; initial_stack_offset_for_row
; plot_row_of_sprite_from_stack_loop                                # For each byte of the row,
; plot_row_of_sprite_from_stack_direction_opcode_one
&105c ca       DEX
#     or       INX if horizontally flipped
&105d bd 00 01 LDA &0100,X                                          # Get byte from stack for right pixel
; plot_row_of_sprite_from_stack_direction_opcode_two
&1060 ca       DEX
#     or       INX if horizontally flipped
&1061 1d 00 01 ORA &0100,X                                          # Merge with byte from stack for left pixel
; plot_sprite_plotting_opcodes
&1064 51 8f    EOR (&8f),Y ; screen_address                         # If plotting background,
&1066 30 02    BMI &106a ; skip_byte                                # don't plot background if foreground present
;                                                                   # If plotting foreground, &1064 - &1067 becomes:
;                                                                   #     &1064 91 1b BMI &1068 ; plot_byte
;                                                                   #     &1066 30 02 EOR (&1b),Y ; screen_address 
;                                                                   # The branch is always taken, i.e. overwrite screen
;                                                                   # See toggle_sprite_plotting_mode (&10f0)
; plot_byte
&1068 91 8f    STA (&8f),Y ; screen_address                         # Plot combined left and right pixels to screen
; skip_byte
&106a 98       TYA
&106b e9 08    SBC #&08                                             # Move left two pixels
&106d a8       TAY
&106e b0 ec    BCS &105c ; plot_row_of_sprite_from_stack_loop
; to_handle_wraparound_row_opcode
&1070 a6 6a    LDX &6a ; stack_pointer_prior_to_pushing_sprite_data
#     or       BVC &109c ; handle_wraparound_row if plotting will cross end of screen memory
; consider_next_row
&1072 9a       TXS                                                  # Restore the stack position
&1073 c6 6c    DEC &6c ; rows_remaining
&1075 30 58    BMI &10cf ; finished_plotting                        # If not finished,
; move_to_next_row
&1077 c6 a1    DEC &a1 ; screen_offset
&1079 ad 23 10 LDA &1023 ; sprite_data_address_low
&107c 69 20    ADC #&20                                             # Move to next row of sprite sheet
#     actually ADC sprite_data_spritesheet_between_rows_low
&107e 8d 23 10 STA &1023 ; sprite_data_address_low
&1081 ad 24 10 LDA &1024 ; sprite_data_address_high
&1084 69 00    ADC #&00
#     actually ADC sprite_data_spritesheet_between_rows_high
&1086 8d 24 10 STA &1024 ; sprite_data_address_high
&1089 c0 f9    CPY #&f9
&108b b0 93    BCS &1020 ; plot_sprite_row_loop                     # Branch if not topmost row in group
; next_group
&108d a5 a0    LDA &a0 ; second_screen_offset_for_row
&108f 85 a1    STA &a1 ; initial_screen_offset_for_row
&1091 a5 90    LDA &90 ; screen_address_high
&1093 e9 01    SBC #&01                                             # Move up a group
&1095 09 60    ORA #&60 ; &6000 = screen_memory                     # Wrap around end of screen memory
&1097 85 90    STA &90 ; screen_address_high
&1099 4c 09 10 JMP &1009 ; plot_sprite_row_loop_after_wraparound_check

; handle_wraparound_row
&109c a5 90    LDA &90 ; screen_address_high
&109e c9 7f    CMP #&7f                                             # Set carry if in last page of screen memory
;                                                                   # i.e. first half of wraparound row has been plotted
&10a0 49 1f    EOR #&1f                                             # &7f00 -> &6000, i.e. move to start of screen memory
&10a2 85 90    STA &90 ; screen_address_high
&10a4 90 0c    BCC &10b2 ; after_second_half_of_row
; after_first_half_of_row
&10a6 a5 8f    LDA &8f ; screen_address_low
&10a8 85 68    STA &68 ; screen_address_low_before_wraparound
&10aa a9 00    LDA #&00                                             # Plot second half of wraparound row from &6000
&10ac 85 8f    STA &8f ; screen_address_low
&10ae a4 67    LDY &67 ; initial_stack_offset_for_row_after_wraparound
&10b0 b0 a8    BCS &105a ; plot_row_of_sprite_from_stack_from_Y     # Always branches
; after_second_half_of_row
&10b2 a5 68    LDA &68 ; screen_address_low_before_wraparound
&10b4 85 8f    STA &8f ; screen_address_low                         # Pretend the row was plotted as one part
&10b6 a6 6a    LDX &6a ; stack_pointer_prior_to_pushing_sprite_data
&10b8 c0 f9    CPY #&f9
&10ba 90 07    BCC &10c3 ; restore_original_flow                    # Branch if topmost row in group; if so, no more needed
&10bc c6 67    DEC &67 ; initial_stack_offset_for_row_after_wraparound
&10be 18       CLC
&10bf a5 6c    LDA &6c ; rows_remaining                             # Otherwise, remaining rows in this group need to be split
&10c1 d0 af    BNE &1072 ; consider_next_row                        # Always branches
; restore_original_flow
&10c3 a9 a6    LDA #&a6 ; LDX &6a ; stack_pointer_prior_to_pushing_sprite_data
&10c5 8d 70 10 STA &1070 ; to_handle_wraparound_row_opcode          # Restore code to original flow
&10c8 a9 6a    LDA #&6a
&10ca 8d 71 10 STA &1071 ; to_handle_wraparound_row_opcode + 1
&10cd 90 a3    BCC &1072 ; consider_next_row                        # Always branches

; finished_plotting
&10cf 4c c6 0e JMP &0ec6 ; replot_sprite_loop                       # On first pass, X = 1, so previous sprite unplotted
                                                                    # Repeat with X = 0 to plot with current variables

; plot_tile_strip
&10d2 20 f0 10 JSR &10f0 ; toggle_sprite_plotting_mode              # Plot tiles as foreground
; plot_tile_strip_loop
&10d5 c6 af    DEC &af ; tiles_remaining_to_plot
&10d7 30 17    BMI &10f0 ; toggle_sprite_plotting_mode              # Set to background plotting when leaving
&10d9 a6 af    LDX &af ; tiles_remaining_to_plot
&10db bd ed 07 LDA &07ed,X ; tile_strip_flips
&10de 85 09    STA &09 ; tile_flip
&10e0 bd f6 07 LDA &07f6,X ; tile_strip_tiles
&10e3 85 08    STA &08 ; tile_type                                  # Unnecessary code
&10e5 20 9b 23 JSR &239b ; set_sprite_variables_for_tile
&10e8 20 06 11 JSR &1106 ; plot_tile
&10eb e6 95    INC &95 ; tile_x if strip is horizontal
#     or       INC &97 ; tile_y if strip is vertical
#     actually INC (tile_variable_for_plotting)
&10ed 4c d5 10 JMP &10d5 ; plot_tile_strip_loop

; toggle_sprite_plotting_mode
&10f0 a2 01    LDX #&01
; toggle_sprite_plotting_mode_loop
&10f2 bd 64 10 LDA &1064,X ; plot_sprite_plotting_opcodes           # Swap order of opcodes to toggle between plotting
&10f5 bc 66 10 LDY &1066,X ; plot_sprite_plotting_opcodes + 2       # foreground and background
&10f8 9d 66 10 STA &1066,X ; plot_sprite_plotting_opcodes + 2
&10fb 98       TYA
&10fc 9d 64 10 STA &1064,X ; plot_sprite_plotting_opcodes
&10ff ca       DEX
&1100 10 f0    BPL &10f2 ; toggle_sprite_plotting_mode_loop
; leave
&1102 60       RTS

; get_and_plot_tile                                                 # Unused entry point
&1103 20 98 23 JSR &2398 ; get_tile_and_set_sprite_variables
; plot_tile
&1106 c0 19    CPY #&19 ; TILE_SPACE
&1108 f0 f8    BEQ &1102 ; leave                                    # Leave if tile is empty
&110a a9 00    LDA #&00
&110c 85 6e    STA &6e ; offscreen_flags                            # Clear top four bits to calculate sprite variables
&110e a9 00    LDA #&00
&1110 85 6f    STA &6f ; this_object_flags                          # Set to zero to indicate tile isn't teleporting!
&1112 85 70    STA &70 ; this_object_previous_flags
&1114 4c 4d 0d JMP &0d4d ; consider_replotting_sprite

; screen_size_for_distance_check_fraction_table
;      x     y
&1117 e0 ea c0                                                      # Second byte is unused

; screen_size_for_distance_check_table
;      x     y
&111a 07 ea 03                                                      # Second byte is unused

; check_if_this_object_is_far_away                                  # Called with Y = distance to consider far away
&111d 84 9b    STY &9b ; distance
&111f e6 9b    INC &9b ; distance                                   # Increase distance by two when checking y
&1121 e6 9b    INC &9b ; distance                                   # (not present in extended version)
&1123 a2 02    LDX #&02
; check_if_this_object_is_far_away_loop                             # Loop through X = 2 for y, X = 0 for x
&1125 bd 95 0b LDA &0b95,X ; screen_start_x
&1128 38       SEC
&1129 f5 53    SBC &53,X ; this_object_x
&112b 85 9d    STA &9d ; relative_x
&112d 18       CLC
&112e e5 9b    SBC &9b ; distance
&1130 10 1b    BPL &114d ; leave_with_carry_set                     # Leave with carry set if too far left or up
&1132 bd 17 11 LDA &1117,X ; screen_size_for_distance_check_fraction_table
&1135 18       CLC
&1136 7d 91 0b ADC &0b91,X ; screen_start_x_fraction
&1139 bd 1a 11 LDA &111a,X ; screen_size_for_distance_check_table
&113c 65 9d    ADC &9d ; relative_x
&113e 18       CLC
&113f 65 9b    ADC &9b ; distance
&1141 30 0a    BMI &114d ; leave_with_carry_set                     # Leave with carry set if too far right or down
&1143 c6 9b    DEC &9b ; distance
&1145 c6 9b    DEC &9b ; distance
&1147 ca       DEX                                                  # Restore distance to initial value when checking x
&1148 ca       DEX                                                  # (not present in extended version)
&1149 f0 da    BEQ &1125 ; check_if_this_object_is_far_away_loop
&114b 18       CLC                                                  # Leave with carry clear to indicate object not far away
&114c 60       RTS
; leave_with_carry_set
&114d 38       SEC                                                  # Leave with carry set to indicate object is far away
&114e 60       RTS

; earth_tiles_rotation_table
&114f 19 ; &00 : TILE_SPACE
&1150 2d ; &01 : TILE_EARTH
&1151 ed ; &02 : TILE_EARTH | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL
&1152 6d ; &03 : TILE_EARTH | TILE_FLIP_VERTICAL
&1153 ad ; &04 : TILE_EARTH | TILE_FLIP_HORIZONTAL
&1154 2d ; &05 : TILE_EARTH
&1155 ed ; &06 : TILE_EARTH | TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL
&1156 5e ; &07 : TILE_STONE_TWO | TILE_FLIP_VERTICAL
&1157 9e ; &08 : TILE_STONE_TWO | TILE_FLIP_HORIZONTAL

; tile_rotations_table
&1158 00 ; &02 : TILE_FLIP_NONE
&1159 c0 ; &03 : TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL
&115a 80 ; &04 : TILE_FLIP_HORIZONTAL
&115b 40 ; &05 : TILE_FLIP_VERTICAL

; slope_tiles_table
&115c 2e ; &00 : TILE_EARTH_SLOPE_FORTY_FIVE
&115d 2f ; &01 : TILE_EARTH_SLOPE_TWENTY_TWO_ONE
&115e 2e ; &02 : TILE_EARTH_SLOPE_FORTY_FIVE
&115f 23 ; &03 : TILE_STONE_SLOPE_FORTY_FIVE

; sloping_passage_feature_tiles_table
&1160 06 ; &11 : TILE_CHECK_TERTIARY_OBJECT_RANGE_SIX
&1161 04 ; &12 : TILE_CHECK_TERTIARY_OBJECT_RANGE_FOUR
&1162 06 ; &13 : TILE_CHECK_TERTIARY_OBJECT_RANGE_SIX
&1163 04 ; &14 : TILE_CHECK_TERTIARY_OBJECT_RANGE_FOUR
&1164 07 ; &15 : TILE_CHECK_TERTIARY_OBJECT_RANGE_SEVEN
&1165 05 ; &16 : TILE_CHECK_TERTIARY_OBJECT_RANGE_FIVE
&1166 05 ; &17 : TILE_CHECK_TERTIARY_OBJECT_RANGE_FIVE
&1167 06 ; &18 : TILE_CHECK_TERTIARY_OBJECT_RANGE_SIX

; surface_feature_tiles_table
&1168 19 : &19 : TILE_SPACE
&1169 2c ; &1a : TILE_EARTH_HORIZONTAL_QUARTER_WITH_EDGE
&116a 19 ; &1b : TILE_SPACE
&116b 2b ; &1c : TILE_EARTH_EDGE

; horizontal_passage_feature_tiles_table
&116c 00 ; &1d : TILE_CHECK_TERTIARY_OBJECT_RANGE_ZERO
&116d 01 ; &1e : TILE_CHECK_TERTIARY_OBJECT_RANGE_ONE
&116e 02 ; &1f : TILE_CHECK_TERTIARY_OBJECT_RANGE_TWO
&116f 03 ; &20 : TILE_CHECK_TERTIARY_OBJECT_RANGE_THREE
&1170 1a ; &21 : TILE_SHORT_BUSH
&1171 21 ; &22 : TILE_COLUMN
&1172 09 ; &23 : TILE_NEST
&1173 9b ; &24 : TILE_TALL_BUSH | TILE_FLIP_HORIZONTAL
&1174 12 ; &25 : TILE_STONE_ONE
&1175 10 ; &26 : TILE_GREEN_HORIZONTAL_QUARTER
&1176 60 ; &27 : TILE_STONE_HORIZONTAL_QUARTER | TILE_FLIP_VERTICAL
&1177 2b ; &28 : TILE_EARTH_EDGE
&1178 0f ; &29 : TILE_MUSHROOMS                                     # Red mushrooms on floor
&1179 4f ; &2a : TILE_MUSHROOMS | TILE_FLIP_VERTICAL                # Blue mushroom on ceiling
&117a 04 ; &2b : TILE_CHECK_TERTIARY_OBJECT_RANGE_FOUR
&117b 0a ; &2c : TILE_PIPE

; feature_tiles_table
&117c 1b ; &00 : TILE_TALL_BUSH
&117d 5a ; &01 : TILE_SHORT_BUSH | TILE_FLIP_VERTICAL
&117e 19 ; &02 : TILE_SPACE
&117f 19 ; &03 : TILE_SPACE
&1180 1e ; &04 : TILE_STONE_TWO
&1181 13 ; &05 : TILE_STONE_SLOPE_FORTY_FIVE_FULL
&1182 24 ; &06 : TILE_STONE_SLOPE_TWENTY_TWO_ONE
&1183 2c ; &07 : TILE_EARTH_HORIZONTAL_QUARTER_WITH_EDGE
&1184 19 ; &08 : TILE_SPACE

; strata_palette_table                                              # Stone uses &1194 and &1185 - &118d
&1185 8d ; ryk
&1186 82 ; rmk
&1187 8b ; cyk
&1188 8f ; cwk
&1189 84 ; rwk
&118a 89 ; gwk
&118b 8d ; ryk
&118c 81 ; rgk
&118d 82 ; rmk
&118e 81 ; rgk                                                      # Earth uses &118e - &1193
&118f 85 ; gyk
&1190 b2 ; rmy
&1191 cd ; ryb
&1192 90 ; kyr
&1193 95 ; gyr
&1194 81 ; rgk

; bushes_palette_table
&1195 b1 ; rgy
&1196 97 ; gmr
&1197 fd ; ryw
&1198 f3 ; rcw

; suppress_tile_scrolling
&1199 00                                                            # Unused in standard version

; number_of_sound_channels
&119a 03

; unused
&119b 01

; frequency_ranges_limit_table
&119c 00 40 84 b6

; frequency_ranges_base_table
&11a0 e0 10 4a 80

; sound_channels_set_frequency_byte_table
&11a4 e0 c0 a0 80

; sound_channels_set_volume_byte_table
&11a8 f0 d0 b0 90

; sound_channels_volume                                             # Current values
&11ac 00 00 00 00
; sound_channels_frequency
&11b0 00 00 00 00

; sound_channels_volume_stage_offset                                # Pointer into envelopes_table for current stage
&11b4 33 22 11 00 
; sound_channels_frequency_stage_offset
&11b8 33 22 11 00 

; sound_channels_volume_stage_duration                              # Duration of current stage remaining
&11bc 33 22 11 00 
; sound_channels_frequency_stage_duration
&11c0 33 22 11 00

; sound_channels_volume_loops_remaining                             # Number of times left to loop
&11c4 33 22 11 00
; sound_channels_frequency_loops_remaining
&11c8 33 22 11 00

; sound_channels_volume_duration                                    # Number of stages or loops remaining in envelope
&11cc 00 00 00 00
; sound_channels_frequency_duration
&11d0 00 00 00 00

; sound_channels_volume_loop_offset                                 # Pointer into envelopes_table for first stage in loop
&11d4 33 22 11 00
; sound_channels_frequency_loop_offset
&11d8 33 22 11 00

; sound_channels_volume_reduction                                   # Used to make distant sounds quieter
&11dc 00 00 00 00

; sound_channels_parameters_address_low                             # Used to determine which sounds are similar
&11e0 00 00 00 00

; vsync_state
&11e4 00

; palette_registers_table
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
;      K  R  G  Y  B  M  C  W  k  r  g  y  b  m  c  w
&11e5 07 16 25 34 43 52 61 70 87 96 a5 b4 c3 d2 e1 f0

; suppress_checking_for_keypresses
&11f5 00                                                            # Zero to suppress checking for all but pause

# Actions
# =======
#           key     repeat  routine
#     &00 : COPY    yes     &3576 ; handle_pause
#     &01 : f0      no      &2ce2 ; handle_changing_weapon_or_transferring_energy
#     &02 : f1      no      &2ce2 ; handle_changing_weapon_or_transferring_energy
#     &03 : f2      no      &2ce2 ; handle_changing_weapon_or_transferring_energy
#     &04 : f3      no      &2ce2 ; handle_changing_weapon_or_transferring_energy
#     &05 : f4      no      &2ce2 ; handle_changing_weapon_or_transferring_energy
#     &06 : f5      no      &2ce2 ; handle_changing_weapon_or_transferring_energy
#     &07 : f6      no      &2ce2 ; handle_changing_weapon_or_transferring_energy
#     &08 : f7      no      &2ce2 ; handle_changing_weapon_or_transferring_energy
#     &09 : f8      no      &2ce2 ; handle_changing_weapon_or_transferring_energy
#     &0a : f9      no      &2ce2 ; handle_changing_weapon_or_transferring_energy
#     &0b : f9      yes     &14d6 ; handle_preparing_game_for_save
#     &0c : G       no      &34f8 ; handle_retrieving_object
#     &0d : SPACE   no      &2d33 ; handle_firing
#     &0e : I       yes     &3120 ; handle_centring_aim
#     &0f : LEFT    no      &2c1d ; handle_scrolling_viewpoint
#     &10 : RIGHT   no      &2c1d ; handle_scrolling_viewpoint
#     &11 : UP      no      &2c1d ; handle_scrolling_viewpoint
#     &12 : DOWN    no      &2c1d ; handle_scrolling_viewpoint
#     &13 : K       yes     &3129 ; handle_lowering_aim
#     &14 : O       yes     &3126 ; handle_raising_aim
#     &15 : @       yes     &2c81 ; handle_using_booster
#     &16 : CTRL    yes     &2c7a ; handle_lying_down
#     &17 : TAB     no      &1e19 ; handle_swapping_direction
#     &18 : Y       no      &2c99 ; handle_playing_whistle_one
#     &19 : U       no      &2cac ; handle_playing_whistle_two
#     &1a : T       no      &0cc1 ; handle_teleporting
#     &1b : R       no      &2c3c ; handle_remembering_position
#     &1c : >       yes     &32d9 ; handle_throwing_object
#     &1d : M       yes     &32c8 ; handle_dropping_object
#     &1e : <       yes     &32b6 ; handle_picking_up_object
#     &1f : S       no      &34b1 ; handle_storing_object
#     &20 : V       no      &129a ; handle_toggling_sound
#     &21 : Q       yes     &2c6d ; handle_thrusting_left
#     &22 : W       yes     &2c6a ; handle_thrusting_right
#     &23 : P       yes     &2c73 ; handle_thrusting_up
#     &24 : P       no      &3b93 ; handle_jumping
#     &25 : L       yes     &2c70 ; handle_thrusting_down
#     &26 : SHIFT   yes     &149c ; handle_pressing_shift (null function)

; action_routine_addresses_low_table                                # Low byte of routine address
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
&11f6 75 e1 e1 e1 e1 e1 e1 e1 e1 e1 e1 d5 f7 32 1f 1c ; &00
&1206 1c 1c 1c 28 25 80 79 18 98 ab c0 3b d8 c7 b5 b0 ; &10
&1216 99 6c 69 72 92 6f 9b                            ; &20

; action_routine_addresses_high_table                               # .......1 suppress autorepeat if set
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f               # 8421842. -> .8421842 high byte of routine address
&121d 6a 59 59 59 59 59 59 59 59 59 59 28 69 5b 62 59 ; &00
&122d 59 59 59 62 62 58 58 3d 59 59 19 59 64 64 64 69 ; &10
&123d 25 58 58 58 77 58 28                            ; &20

; action_keycodes_table
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
&1244 69 20 71 72 73 14 74 75 16 76 77 77 53 62 25 19 ; &00
&1254 79 39 29 46 36 47 01 60 44 35 23 33 67 65 66 51 ; &10
&1264 63 10 21 37 37 56 00                            ; &20

; action_keys_pressed
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
&126b 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; &00
&127b 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; &10
&128b 00 00 00 00 00 00 00                            ; &20

; sound_enabled
&1292 ff                                                            # &ff if sound enabled, &00 if sound disabled

; update_game_time_or_player_deaths_loop
&1293 e8       INX
; update_game_time_or_player_deaths                                 # Called with X = 0 for game time, X = 4 for player deaths
&1294 fe ff 07 INC &07ff,X ; game_time
&1297 f0 fa    BEQ &1293 ; update_game_time_or_player_deaths_loop
&1299 60       RTS

; handle_toggling_sound
&129a ad 92 12 LDA &1292 ; sound_enabled
&129d 49 ff    EOR #&ff
&129f 8d 92 12 STA &1292 ; sound_enabled
&12a2 60       RTS

; to_leave_after_restoring_registers
&12a3 4c 92 13 JMP &1392 ; leave_after_restoring_registers

; irq1_handler
&12a6 98       TYA
&12a7 48       PHA ; tmp_y
&12a8 8a       TXA
&12a9 48       PHA ; tmp_x
&12aa 2c 4d fe BIT &fe4d ; System VIA interrupt flag register       # &80 set if interrupt occurred
&12ad 10 f4    BPL &12a3 ; to_leave_after_restoring_registers
&12af a9 7f    LDA #&7f
&12b1 8d 4d fe STA &fe4d ; System VIA interrupt flag register       # Clear interrupt
&12b4 50 12    BVC &12c8 ; is_vsync_interrupt                       # &40 set if System VIA timer 1 interrupt occurred
; is_timer_interrupt
&12b6 a9 01    LDA #&01
&12b8 8d 21 fe STA &fe21 ; video ULA palette register               # Set colour 0 to cyan for surface of water
&12bb a0 18    LDY #&18
; delay_loop                                                        # Delay for one line of raster
&12bd 88       DEY                                                  # 2 cycles
&12be d0 fd    BNE &12bd ; delay_loop                               # 3 cycles if branch taken, 2 if not; 119 cycle delay
&12c0 a9 03    LDA #&03
&12c2 8d 21 fe STA &fe21 ; video ULA palette register               # Set colour 0 to blue for water
&12c5 4c 92 13 JMP &1392 ; leave_after_restoring_registers
; is_vsync_interrupt                                                # Otherwise, the interrupt was caused by a v-sync
&12c8 ad cf 14 LDA &14cf ; waterline_timer_high                     # Zero if the waterline is not on screen
&12cb f0 09    BEQ &12d6 ; skip_setting_timer
&12cd ac ce 14 LDY &14ce ; waterline_timer_low
&12d0 8c 44 fe STY &fe44 ; System VIA timer 1 counter LSB
&12d3 8d 45 fe STA &fe45 ; System VIA timer 1 counter MSB
; skip_setting_timer
&12d6 a9 07    LDA #&07
&12d8 8d 21 fe STA &fe21 ; video ULA palette register               # Set colour 0 to black for absence of water
&12db ee e4 11 INC &11e4 ; vsync_state
&12de ad be 14 LDA &14be ; palette_registers_need_updating          # Non-zero if palette registers need updating
&12e1 f0 06    BEQ &12e9 ; skip_updating_palette_registers
&12e3 20 0f 3e JSR &3e0f ; update_palette_registers                 # Returns Y = 0
&12e6 8c be 14 STY &14be ; palette_registers_need_updating          # Set to zero to indicate palette doesn't need updating
; skip_updating_palette_registers
&12e9 a9 7f    LDA #&7f                                             # Set top bit as input, low seven bits as output
&12eb 8d 43 fe STA &fe43 ; System VIA data direction register A
&12ee a9 03    LDA #&03                                             # Disable keyboard auto-scan
&12f0 8d 40 fe STA &fe40 ; System VIA port B input/output register
&12f3 ae f5 11 LDX &11f5 ; suppress_checking_for_keypresses         # Zero to suppress checking for all but pause
&12f6 f0 02    BEQ &12fa ; check_for_keypresses_loop
&12f8 a2 26    LDX #&26
; check_for_keypresses_loop                                         # For each key,
&12fa bd 44 12 LDA &1244,X ; action_keycodes_table
&12fd 8d 4f fe STA &fe4f ; System VIA input/output register A
&1300 ad 4f fe LDA &fe4f ; System VIA input/output register A
&1303 2a       ROL A                                                # Set carry if key pressed
&1304 7e 6b 12 ROR &126b,X ; action_keys_pressed                    # Set &80 if key pressed
&1307 ca       DEX
&1308 10 f0    BPL &12fa ; check_for_keypresses_loop
&130a a9 0b    LDA #&0b                                             # Enable keyboard auto-scan
&130c 8d 40 fe STA &fe40 ; System VIA port B input/output register
&130f e8       INX ; 0
&1310 8e f5 11 STX &11f5 ; suppress_checking_for_keypresses         # Set to zero to suppress checking until actions processed
&1313 2c bd 14 BIT &14bd ; suppress_updating_sound                  # Positive if game paused
&1316 10 7a    BPL &1392 ; leave_after_restoring_registers
&1318 20 94 12 JSR &1294 ; update_game_time_or_player_deaths        # X = 0 here, so update game time
&131b a9 ff    LDA #&ff                                             # Set all bits as output
&131d 8d 43 fe STA &fe43 ; System VIA data direction register A
&1320 ae 9a 11 LDX &119a ; number_of_sound_channels
; update_sound_channel_loop                                         # Loop through X = 3 to 0 for sound channels
&1323 20 99 13 JSR &1399 ; update_sound_envelope                    # Update volume envelope
&1326 b0 0a    BCS &1332 ; skip_fading                              # Returns carry set if envelope hasn't ended
&1328 bd ac 11 LDA &11ac,X ; sound_channels_volume
&132b e9 01    SBC #&01                                             # Fade sound at end of duration
&132d 90 47    BCC &1376 ; skip_setting_frequency
&132f 9d ac 11 STA &11ac,X ; sound_channels_volume
; skip_fading
&1332 e8       INX                                                  # X += 4, i.e. use frequency envelope
&1333 e8       INX
&1334 e8       INX
&1335 e8       INX
&1336 20 99 13 JSR &1399 ; update_sound_envelope                    # Update frequency envelope
&1339 ca       DEX                                                  # Returns carry set if sound hasn't ended
&133a ca       DEX                                                  #         A = channel frequency
&133b ca       DEX                                                  #         zero set at end of envelope stage
&133c ca       DEX                                                  # Restore X to original value
&133d 90 37    BCC &1376 ; skip_setting_frequency
&133f d0 04    BNE &1345 ; consider_setting_frequency
&1341 c5 23    CMP &23 ; previous_frequency
&1343 f0 31    BEQ &1376 ; skip_setting_frequency               
; consider_setting_frequency
&1345 49 ff    EOR #&ff                                             # Frequency played = 4000000 / (32 * value written)
;                                                                   # so invert to keep lower numbers meaning lower pitches
&1347 a0 04    LDY #&04
&1349 84 23    STY &23 ; frequency_high                             # Only bottom two bits will be used, so effectively zero
; find_frequency_range_loop                                         # Convert A into a value, depending on which range it is in:
&134b 88       DEY                                                  #     &00 - &3f -> &20 - &5f, Y = 0 -> &020 - &05f
&134c d9 9c 11 CMP &119c,Y ; frequency_ranges_limit_table           #     &40 - &83 -> &30 - &73, Y = 1 -> &060 - &0e6
&134f 90 fa    BCC &134b ; find_frequency_range_loop                #     &84 - &b5 -> &3a - &6b, Y = 2 -> &0e8 - &1ac
&1351 f9 a0 11 SBC &11a0,Y ; frequency_ranges_base_table            #     &b6 - &ff -> &36 - &7f, Y = 3 -> &1b0 - &1fc
; shift_frequency_loop
&1354 88       DEY
&1355 30 06    BMI &135d ; set_frequency
&1357 0a       ASL A
&1358 26 23    ROL &23 ; frequency_high
&135a 4c 54 13 JMP &1354 ; shift_frequency_loop
; set_frequency
&135d 48       PHA ; frequency_low
&135e 29 0f    AND #&0f                                             # 84218421 -> ....8421
&1360 1d a4 11 ORA &11a4,X ; sound_channels_set_frequency_byte_table
&1363 20 e4 13 JSR &13e4 ; write_byte_to_sound_chip                 # Set four least significant bits of frequency for channel
&1366 68       PLA ; frequency_low
&1367 e0 00    CPX #&00
&1369 f0 0b    BEQ &1376 ; skip_setting_frequency
&136b 46 23    LSR &23 ; frequency_high
&136d 6a       ROR A                                                # 84218421 -> t8421842
&136e 46 23    LSR &23 ; frequency_high
&1370 6a       ROR A                                                #          -> tt842184
&1371 4a       LSR A                                                #          -> 0tt84218
&1372 4a       LSR A                                                #          -> 00tt8421
&1373 20 e4 13 JSR &13e4 ; write_byte_to_sound_chip                 # Set six most significant bits of frequency for channel
; skip_setting_frequency
&1376 bd ac 11 LDA &11ac,X ; sound_channels_volume
&1379 fd dc 11 SBC &11dc,X ; sound_channels_volume_reduction
&137c b0 02    BCS &1380 ; skip_floor
&137e a9 00    LDA #&00
; skip_floor
&1380 2d 92 12 AND &1292 ; sound_enabled                            # &ff if sound enabled, &00 if sound disabled
&1383 49 ff    EOR #&ff
&1385 4a       LSR A
&1386 4a       LSR A
&1387 4a       LSR A
&1388 4a       LSR A
&1389 1d a8 11 ORA &11a8,X ; sound_channels_set_volume_byte_table
&138c 20 e4 13 JSR &13e4 ; write_byte_to_sound_chip                 # Set volume for channel
&138f ca       DEX
&1390 10 91    BPL &1323 ; update_sound_channel_loop
; leave_after_restoring_registers
&1392 68       PLA ; tmp_x
&1393 aa       TAX
&1394 68       PLA ; tmp_y
&1395 a8       TAY
&1396 a5 fc    LDA &fc ; irq_accumulator
&1398 40       RTI

; update_sound_envelope                                             # Called with X = channel + 0 for volume
&1399 18       CLC                                                  #             X = channel + 4 for frequency
&139a bd cc 11 LDA &11cc,X ; sound_channels_volume_duration         # Are there any stages of the envelope remaining?
&139d f0 44    BEQ &13e3 ; leave                                    # Leave with carry clear to indicate envelope has ended
&139f bc b4 11 LDY &11b4,X ; sound_channels_volume_stage_offset
&13a2 bd bc 11 LDA &11bc,X ; sound_channels_volume_stage_duration   # Non-zero if this stage hasn't ended
&13a5 d0 2d    BNE &13d4 ; skip_starting_stage
; start_stage
&13a7 bd c4 11 LDA &11c4,X ; sound_channels_volume_loops_remaining  # Non-zero if this loop hasn't ended
&13aa d0 05    BNE &13b1 ; not_end_of_loop
&13ac de cc 11 DEC &11cc,X ; sound_channels_volume_duration
&13af f0 32    BEQ &13e3 ; leave                                    # Leave at end of envelope
; not_end_of_loop
&13b1 c8       INY
&13b2 b9 b9 2d LDA &2db9,Y ; envelopes_table                        # Get first byte of stage data
&13b5 10 15    BPL &13cc ; not_loop                                 # If positive, use as duration of stage
; is_loop
&13b7 de c4 11 DEC &11c4,X ; sound_channels_volume_loops_remaining  # Negative indicates start or end of a loop
&13ba 10 0a    BPL &13c6 ; skip_starting_loop                       # Has the current loop ended?
; start_loop
&13bc 29 7f    AND #&7f                                             # Low seven bits indicate number of loops
&13be 9d c4 11 STA &11c4,X ; sound_channels_volume_loops_remaining
&13c1 c8       INY                                                  # Next byte is start of loop
&13c2 98       TYA
&13c3 9d d4 11 STA &11d4,X ; sound_channels_volume_loop_offset
; skip_starting_loop
&13c6 bc d4 11 LDY &11d4,X ; sound_channels_volume_loop_offset      # Return to start of loop
&13c9 b9 b9 2d LDA &2db9,Y ; envelopes_table
; not_loop
&13cc 9d bc 11 STA &11bc,X ; sound_channels_volume_stage_duration
&13cf c8       INY                                                  # Move to second byte of stage data
&13d0 98       TYA
&13d1 9d b4 11 STA &11b4,X ; sound_channels_volume_stage_offset
; skip_starting_stage
&13d4 bd ac 11 LDA &11ac,X ; sound_channels_volume
&13d7 85 23    STA &23 ; previous_volume
&13d9 79 b9 2d ADC &2db9,Y ; envelopes_table                        # Second byte of stage data is added to value
&13dc 9d ac 11 STA &11ac,X ; sound_channels_volume                  # Leave with A = volume or frequency value
&13df de bc 11 DEC &11bc,X ; sound_channels_volume_stage_duration   # Leave with zero set at end of an envelope stage
&13e2 38       SEC                                                  # Leave with carry set to indicate envelope hasn't ended
; leave
&13e3 60       RTS

; write_byte_to_sound_chip 
&13e4 8d 4f fe STA &fe4f ; System VIA input/output register A
&13e7 a9 00    LDA #&00                                             # Set sound chip write pin low
&13e9 8d 40 fe STA &fe40 ; System VIA port B input/output register
&13ec a9 22    LDA #&22 ; 00100010
; delay_loop_one
&13ee 4a       LSR A                                                # 2 cycles
&13ef 90 fd    BCC &13ee ; delay_loop_one                           # 3 cycles if branch taken, 2 if not
;                                                                   # Don't branch on second pass; 9 cycle delay
&13f1 8d 40 fe STA &fe40 ; System VIA port B input/output register  # Set sound chip write pin high (A = 8 here)
; delay_loop_two
&13f4 4a       LSR A
&13f5 90 fd    BCC &13f4 ; delay_loop_two                           # Don't branch on fourth pass; 19 cycle delay
&13f7 60       RTS

# Sounds
# ======
# Calls to play_sound and play_sound_on_channel_zero are followed by a four
# byte parameter block. These specify envelopes and starting values for volume
# (first two bytes) and frequency (second two bytes). These are as follows:
#
# 84218421 ........ envelope to use (offset into envelopes_table - 1)
# ........ 8421.... initial value of volume or frequency
# ........ ....8421 duration of envelope in stages or loops

; play_sound_on_channel_zero
&13f8 38       SEC                                                  # Set carry to use channel zero
&13f9 24 18    BIT &18 ; (nop)
; play_sound
#13fa          CLC                                                  # Clear carry to use channels one, two or three
&13fb 85 98    STA &98 ; tmp_a
&13fd 86 99    STX &99 ; tmp_x
&13ff 84 9a    STY &9a ; tmp_y
&1401 08       PHP ; carry set if channel zero
&1402 68       PLA ; carry set if channel zero                      # Parameters follow function call
&1403 aa       TAX
&1404 68       PLA ; return_address_low
&1405 85 9b    STA &9b ; parameters_address_low
&1407 18       CLC
&1408 69 04    ADC #&04                                             # Four bytes of parameters
&140a a8       TAY
&140b 68       PLA ; return_address_high
&140c 85 9c    STA &9c ; parameters_address_high
&140e 69 00    ADC #&00
&1410 48       PHA ; return_address_high                            # Return to code after end of parameters
&1411 98       TYA
&1412 48       PHA ; return_address_low
&1413 8a       TXA
&1414 48       PHA ; carry set
&1415 20 59 35 JSR &3559 ; get_object_distance_from_screen_centre
&1418 c9 10    CMP #&10                                             # Don't play a sound if too distant
&141a b0 78    BCS &1494 ; leave_after_restoring_registers_and_flags
&141c 0a       ASL A
&141d 0a       ASL A
&141e 0a       ASL A
&141f 0a       ASL A
&1420 a8       TAY ; volume reduction                               # More distant objects are quieter
&1421 28       PLP ; carry set if channel zero
&1422 90 09    BCC &142d ; not_channel_zero
; is_channel_zero
&1424 a2 00    LDX #&00
&1426 ad cc 11 LDA &11cc ; sound_channels_volume_duration + 0       # Zero if no sound is being played on channel
&1429 f0 39    BEQ &1464 ; play_sound_on_channel
&142b b0 22    BCS &144f ; find_quieter_channel_loop                # Always branches
; not_channel_zero
&142d ae 9a 11 LDX &119a ; number_of_sound_channels
; find_free_channel_loop
&1430 bd cc 11 LDA &11cc,X ; sound_channels_volume_duration         # Zero if no sound being played on channel
&1433 f0 2f    BEQ &1464 ; play_sound_on_channel
&1435 ca       DEX
&1436 d0 f8    BNE &1430 ; find_free_channel_loop                   # Check channels three, two and one
&1438 ad e1 11 LDA &11e1 ; sound_channels_parameters_address_low + 1
&143b 4d e2 11 EOR &11e2 ; sound_channels_parameters_address_low + 2
&143e 4d e3 11 EOR &11e3 ; sound_channels_parameters_address_low + 3
&1441 ae 9a 11 LDX &119a ; number_of_sound_channels
; find_similar_channel_loop
&1444 dd e0 11 CMP &11e0,X ; sound_channels_parameters_address_low  # Are two channels playing the same sound?
;                                                                   # If so, X is the remaining channel
&1447 f0 15    BEQ &145e ; is_similar
&1449 ca       DEX
&144a d0 f8    BNE &1444 ; find_similar_channel_loop
&144c ae 9a 11 LDX &119a ; number_of_sound_channels
; find_quieter_channel_loop
&144f 98       TYA ; volume reduction
&1450 dd dd 11 CMP &11dd,X ; sound_channels_volume_reduction + 1
&1453 f0 0f    BEQ &1464 ; play_sound_on_channel                    # Branch if same
&1455 90 0d    BCC &1464 ; play_sound_on_channel                    # or nearer distance
&1457 ca       DEX
&1458 f0 3b    BEQ &1495 ; leave_after_restoring_registers          # Don't play sound if further away
&145a 10 f3    BPL &144f ; find_quieter_channel_loop
&145c 30 37    BMI &1495 ; leave_after_restoring_registers          # Always branches (for channel 0)
; is_similar
&145e ca       DEX                                                  # Use one of the two channels
&145f d0 03    BNE &1464 ; play_sound_on_channel
&1461 ae 9a 11 LDX &119a ; number_of_sound_channels                 # If X was 1, channel 3 is one of them
; play_sound_on_channel
&1464 98       TYA ; volume reduction
&1465 9d dc 11 STA &11dc,X ; sound_channels_volume_reduction
&1468 a5 9b    LDA &9b ; parameters_address_low
&146a 9d e0 11 STA &11e0,X ; sound_channels_parameters_address_low
&146d a0 01    LDY #&01                                             # parameters_address is address of parameters - 1
&146f 08       PHP ; flags
&1470 78       SEI
; set_envelopes_from_parameters_loop                                # Start with X = 0, Y = 1 for volume envelope
&1471 b1 9b    LDA (&9b),Y ; parameters_address                     # First (or third) byte of parameters sets offset into
&1473 9d b4 11 STA &11b4,X ; sound_channels_volume_stage_offset     # envelope table, i.e. which envelope to use
&1476 c8       INY
&1477 b1 9b    LDA (&9b),Y ; parameters_address                     # Second (or fourth) byte of parameters
&1479 29 f0    AND #&f0
&147b 9d ac 11 STA &11ac,X ; sound_channels_volume                  # Top nibble sets initial value of envelope
&147e 51 9b    EOR (&9b),Y ; parameters_address
&1480 9d cc 11 STA &11cc,X ; sound_channels_volume_duration         # Bottom nibble sets duration of envelope
&1483 c8       INY
&1484 a9 00    LDA #&00
&1486 9d bc 11 STA &11bc,X ; sound_channels_volume_stage_duration   # Set to zero to update envelope on next update
&1489 9d c4 11 STA &11c4,X ; sound_channels_volume_loops_remaining
&148c e8       INX                                                  # Repeat with X = 4, Y = 3 for frequency envelope
&148d e8       INX
&148e e8       INX
&148f e8       INX
&1490 e0 08    CPX #&08
&1492 90 dd    BCC &1471 ; set_envelopes_from_parameters_loop
; leave_after_restoring_registers_and_flags
&1494 28       PLP ; flags
; leave_after_restoring_registers
&1495 a5 98    LDA &98 ; tmp_a
&1497 a6 99    LDX &99 ; tmp_x
&1499 a4 9a    LDY &9a ; tmp_y
&149b 38       SEC                                                  # This is used at &2d6d, &42c5, &4386 and &4d31
; handle_pressing_shift                                             # SHIFT by itself does nothing
&149c 60       RTS

; play_middle_beep
&149d 20 fa 13 JSR &13fa ; play_sound
&14a0 17 e3 2f 72                                                   # Play sound for middle beep
&14a4 60       RTS

; play_high_beep
&14a5 20 fa 13 JSR &13fa ; play_sound
&14a8 17 82 13 f2                                                   # Play sound for high beep
&14ac 60       RTS

; play_low_beep
&14ad 20 fa 13 JSR &13fa ; play_sound
&14b0 5d 04 ff 05                                                   # Play sound for low beep
&14b4 60       RTS

; play_squeal
&14b5 20 fa 13 JSR &13fa ; play_sound
&14b8 33 03 2d 84                                                   # Play sound for squeal
&14bc 60       RTS

; suppress_updating_sound
&14bd 92                                                            # Positive if game paused

; palette_registers_need_updating
&14be 01                                                            # Non-zero if palette registers need updating

; angle_calculation_half_quadrants_table                            # x- = left, x+ = right, y- = up, y+ = down
;     xY Xy xY Xy xY Xy xY Xy                                       # &00 = right, &40 = down, &80 = left, &c0 = up
;     -- -- +- +- -+ -+ ++ ++
&14bf bf 80 c0 ff 40 7f 3f 00

; screen_centre_offset_x
&14c7 04                                                            # Number of tiles from left to centre of screen

; screen_scroll_offset_x
&14c8 00                                                            # Number of tiles player has scrolled the viewport

; screen_centre_offset_y
&14c9 02                                                            # Number of tiles from top to centre of screen

; screen_scroll_offset_y
&14ca 00                                                            # Number of tiles player has scrolled the viewport

; viewpoint_object
&14cb 00                                                            # Always zero (player) in standard version

; new_viewpoint_object           
&14cc 00                                                            # Unused in standard version

; new_tiles_exposed_during_scrolling
&14cd 00                                                            # &80 if new tiles have been exposed during scrolling

; waterline_timer_low
&14ce 00
; waterline_timer_high
&14cf 00 

; waterline_y_fraction
&14d0 00
; waterline_y
&14d1 00

; waterline_x_ranges_x_table                                        # 0 : West caves with crew members
;      0  1  2  3                                                   # 1 : Triax's lab
&14d2 00 54 74 a0                                                   # 2 : central caves 
                                                                    # 3 : East caves (same level as 2)

; handle_preparing_game_for_save
&14d6 2c 91 12 BIT &1291 ; action_keys_pressed + &26 (SHIFT)        # Negative if SHIFT pressed
&14d9 10 4e    BPL &1529 ; leave                                    # SHIFT-f9 to prepare game for save
; prepare_game_for_save
&14db 20 4e 39 JSR &394e ; check_copy_protection                    # Hangs if demo mode
&14de f8       SED
; prepare_game_for_save_without_copy_protection_check
&14df 78       SEI
&14e0 a5 da    LDA &da ; rnd_state + 1
&14e2 45 dc    EOR &dc ; rnd_state + 3
&14e4 29 7f    AND #&7f
&14e6 49 65    EOR #&65
&14e8 8d 2d 08 STA &082d ; copy_protection_third_byte               # permuted_rnd_pair
&14eb 38       SEC
&14ec a0 07    LDY #&07
; copy_variables_loop                                               # Copy &00d9 - &00df to &07f8 - &07fe
&14ee b9 d8 00 LDA &00d8,Y ; rnd_state - 1                          #      &00d9 - &00dc to &07f8 - &07fb rnd_state
&14f1 99 f7 07 STA &07f7,Y ; game_state - 1                         #              &00dd to &07fc         player_object_held
&14f4 88       DEY                                                  #              &00de to &07fd         player_angle
&14f5 d0 f7    BNE &14ee ; copy_variables_loop                      #              &00df to &07fe         player_facing
; encrypt_game_state
&14f7 a9 6e    LDA #&6e
&14f9 85 9d    STA &9d ; key
&14fb a9 92    LDA #&92
; encrypt_game_state_loop                                           # Encrypt &07f8 - &5bf7 to &2c00 - &7fff
&14fd 65 9d    ADC &9d ; key
&14ff 69 15    ADC #&15
&1501 85 9d    STA &9d ; key
&1503 b9 f8 07 LDA &07f8,Y ; game_state
#     actually LDA source_address,Y
&1506 48       PHA ; byte of game state
&1507 4d 52 0b EOR &0b52 ; game_state_checksum_one
&150a 8d 52 0b STA &0b52 ; game_state_checksum_one
&150d 68       PLA ; byte of game state
&150e 45 9d    EOR &9d ; key
&1510 48       PHA ; encrypted byte of game state
&1511 4d 75 0b EOR &0b75 ; game_state_checksum_two
&1514 8d 75 0b STA &0b75 ; game_state_checksum_two
&1517 68       PLA ; encrypted byte of game state
&1518 99 00 2c STA &2c00,Y ; saved_game_state
#     actually STA target_address,Y
&151b c8       INY
&151c d0 df    BNE &14fd ; encrypt_game_state_loop
&151e ee 05 15 INC &1505 ; source_address_high
&1521 ee 1a 15 INC &151a ; target_address_high
&1524 10 d7    BPL &14fd ; encrypt_game_state_loop
&1526 4c bb 28 JMP &28bb ; wipe_memory_then_infinite_loop
; leave
&1529 60       RTS

; consider_how_to_scroll_screen
&152a 4e 76 0b LSR &0b76 ; secondary_object_update_mode             # Clear top bit to consider only next secondary object
&152d 20 88 22 JSR &2288 ; get_this_object_centre                   # Get the player's centre
; consider_how_to_scroll_screen_in_x
&1530 a2 00    LDX #&00                                             # Calculate for x
&1532 20 d2 15 JSR &15d2 ; calculate_amount_of_scrolling_needed_in_direction
&1535 85 a2    STA &a2 ; amount_of_scrolling_needed_in_x            # In &20 fraction sections, eight per tile
&1537 20 54 32 JSR &3254 ; make_positive
&153a 85 9d    STA &9d ; absolute_amount_of_scrolling_needed_in_x
&153c c9 0c    CMP #&0c                                             # If the screen needs to scroll more than one and a
&153e b0 4e    BCS &158e ; redraw_screen                            # half tiles horizontally, redraw it
&1540 c9 02    CMP #&02
&1542 90 14    BCC &1558 ; set_tile_sections_to_scroll_x
&1544 f0 08    BEQ &154e ; skip_checking_half_tile_alignment
&1546 a0 04    LDY #&04
&1548 a5 c7    LDA &c7 ; screen_origin_x_fraction
&154a 29 7f    AND #&7f
&154c f0 09    BEQ &1557 ; set_tile_sections_to_scroll_x_from_Y     # If aligned with a full or half tile, scroll half a tile
; skip_checking_half_tile_alignment
&154e a0 02    LDY #&02
&1550 a5 c7    LDA &c7 ; screen_origin_x_fraction
&1552 29 20    AND #&20
&1554 f0 01    BEQ &1557 ; set_tile_sections_to_scroll_x_from_Y     # If aligned with a quarter tile, scroll quarter of a tile
&1556 88       DEY ; 1                                              # Otherwise, scroll an eighth of a tile
; set_tile_sections_to_scroll_x_from_Y
&1557 98       TYA
; set_tile_sections_to_scroll_x
&1558 24 a2    BIT &a2 ; amount_of_scrolling_needed_in_x            # Use sign of amount of scrolling required
&155a 20 56 32 JSR &3256 ; invert_if_negative
&155d 85 cf    STA &cf ; screen_tile_sections_to_scroll_x           # This may not be used if y scrolling needed is greater
; consider_how_to_scroll_screen_in_y
&155f a2 02    LDX #&02                                             # Calculate for y
&1561 20 d2 15 JSR &15d2 ; calculate_amount_of_scrolling_needed_in_direction
&1564 a8       TAY ; y scrolling needed
&1565 20 56 32 JSR &3256 ; invert_if_negative
&1568 c5 9d    CMP &9d ; absolute_amount_of_scrolling_needed_in_x   # If scrolling needed is greater in x than y,
&156a 90 1d    BCC &1589 ; zero_screen_tile_sections_to_scroll_y    # scroll horizontally, not vertically
&156c c9 0c    CMP #&0c                                             # If the screen needs to scroll more than one and a
&156e b0 1e    BCS &158e ; redraw_screen                            # half tiles vertically, redraw it
&1570 c9 02    CMP #&02
&1572 98       TYA ; y scrolling needed
&1573 90 0d    BCC &1582 ; set_screen_tile_sections_to_scroll_y     # Scroll a single section if needed vertically
&1575 a9 01    LDA #&01                                             # If more sections needed, only scroll one at a time
&1577 24 c9    BIT &c9 ; screen_origin_y_fraction
&1579 70 02    BVS &157d ; not_aligned                              # unless aligned to a full or half tile,
&157b a9 02    LDA #&02                                             # in which case, scroll two sections
; not_aligned
&157d c0 00    CPY #&00                                             # Use sign of amount of scrolling required
&157f 20 56 32 JSR &3256 ; invert_if_negative
; set_screen_tile_sections_to_scroll_y
&1582 85 d1    STA &d1 ; screen_tile_sections_to_scroll_y           # Scroll vertically, not horizontally
&1584 a9 00    LDA #&00
&1586 85 cf    STA &cf ; screen_tile_sections_to_scroll_x
&1588 60       RTS
; zero_screen_tile_sections_to_scroll_y
&1589 a9 00    LDA #&00                                             # Scroll horizontally, not vertically
&158b 85 d1    STA &d1 ; screen_tile_sections_to_scroll_y
&158d 60       RTS
; redraw_screen
&158e a9 80    LDA #&80
&1590 85 c7    STA &c7 ; screen_origin_x_fraction                   # These are single values, not pairs for current and
&1592 85 c9    STA &c9 ; screen_origin_y_fraction                   # previous sprite plotting, like screen_start_*
&1594 a5 8d    LDA &8d ; this_object_centre_y
&1596 38       SEC
&1597 e9 01    SBC #&01                                             # Set top of screen to player y - 0.5 tiles
&1599 85 ca    STA &ca ; screen_origin_y
&159b a5 8b    LDA &8b ; this_object_centre_x
&159d e9 04    SBC #&04                                             # Set left of screen to player x - 3.5 tiles 
&159f 85 c8    STA &c8 ; screen_origin_x
&15a1 a9 fe    LDA #&fe
&15a3 85 d1    STA &d1 ; screen_tile_sections_to_scroll_y           # Redraw screen by scrolling upwards two sections a time
&15a5 a9 00    LDA #&00
&15a7 85 cf    STA &cf ; screen_tile_sections_to_scroll_x
&15a9 20 1f 16 JSR &161f ; update_screen_variables
&15ac 20 6c 1f JSR &1f6c ; set_crtc_start_address
&15af a5 ca    LDA &ca ; screen_origin_y
&15b1 18       CLC
&15b2 69 04    ADC #&04                                             # Starting at the bottom of the screen, four tiles down
&15b4 85 ca    STA &ca ; screen_origin_y
&15b6 a2 08    LDX #&08                                             # 8 * 2 = 16 sections = 4 tiles
; redraw_screen_loop
&15b8 86 ab    STX &ab ; rows_remaining
&15ba 20 1f 16 JSR &161f ; update_screen_variables
&15bd 20 84 36 JSR &3684 ; prepare_screen_for_scrolling             # Wipe strip of screen and cache tiles
&15c0 20 d2 10 JSR &10d2 ; plot_tile_strip                          # Plot strip of tiles
&15c3 a6 ab    LDX &ab ; rows_remaining
&15c5 ca       DEX
&15c6 d0 f0    BNE &15b8 ; redraw_screen_loop
&15c8 86 d1    STX &d1 ; screen_tile_sections_to_scroll_y           # Set to zero to indicate no scrolling needed
&15ca a9 f0    LDA #&f0
&15cc 85 6e    STA &6e ; offscreen_flags                            # Set top four bits to suppress calculating sprite variables for player
&15ce 8d 76 0b STA &0b76 ; secondary_object_update_mode             # Set top bit to consider all secondary objects on redraw
&15d1 60       RTS

; calculate_amount_of_scrolling_needed_in_direction                 # Called with X = 0 for x, X = 2 for y
&15d2 2c ab 19 BIT &19ab ; ship_moving                              # Negative if ship is moving at end of game
&15d5 10 09    BPL &15e0 ; not_moving_ship
&15d7 8a       TXA
&15d8 49 02    EOR #&02
&15da d0 3f    BNE &161b ; leave                                    # Leave if calculating x, with A = 2 to scroll right
&15dc a9 3b    LDA #&3b
&15de 85 8d    STA &8d ; this_object_centre_y                       # Fix player y position at end of game
; not_moving_ship
&15e0 b5 87    LDA &87,X ; this_object_centre_x_fraction
&15e2 d5 c7    CMP &c7,X ; screen_origin_x_fraction
&15e4 08       PHP ; relative x fraction carry
&15e5 b5 43    LDA &43,X ; this_object_velocity_x                   # Consider the player's velocity
&15e7 e0 00    CPX #&00
&15e9 f0 03    BEQ &15ee ; not_y
&15eb c9 80    CMP #&80                                             # If y, divide by &10; &40 (0.25 tile/update) -> 4
&15ed 6a       ROR A
; not_y
&15ee 20 75 32 JSR &3275 ; divide_by_eight                          # If x, divide by &08; &40 (0.25 tile/update) -> 8
&15f1 dd 1c 16 CMP &161c,X ; screen_scrolling_velocity_x
&15f4 f0 0b    BEQ &1601 ; skip_changing_amount_owing               # If this isn't the same as the scrolling velocity,
&15f6 10 06    BPL &15fe ; increase_amount_owing                    # adjust scrolling velocity towards it; this keeps the
;                                                                   # scrolling from starting or stopping abruptly
; decrease_amount_owing
&15f8 de 1c 16 DEC &161c,X ; screen_scrolling_velocity_x
&15fb de 1c 16 DEC &161c,X ; screen_scrolling_velocity_x
; increase_amount_owing
&15fe fe 1c 16 INC &161c,X ; screen_scrolling_velocity_x
; skip_changing_amount_owing
&1601 bd 1c 16 LDA &161c,X ; screen_scrolling_velocity_x            # Scrolling velocity is in &20 fraction sections for x
;                                                                   #                          &40 fraction sections for y
&1604 20 75 32 JSR &3275 ; divide_by_eight                          # Calculate corresponding number of tiles for x
;                                                                   #                     but number of half tiles for y
&1607 7d c8 14 ADC &14c8,X ; screen_scroll_offset_x                 # Add any viewport scroll
&160a 18       CLC
&160b 75 8b    ADC &8b,X ; this_object_centre_x
&160d 28       PLP ; relative x fraction carry                      # Account for difference in fractions
&160e f5 c8    SBC &c8,X ; screen_origin_x
&1610 38       SEC
&1611 fd c7 14 SBC &14c7,X ; screen_centre_offset_x                 # player_x - centre_x + scrolling_velocity_x
;                                                                   # Will be used as screen_tile_sections_to_scroll_x
;                                                                   # if this is the component needing most scrolling
&1614 d5 cf    CMP &cf,X ; screen_tile_sections_to_scroll_x
&1616 10 03    BPL &161b ; leave                                    # Leave if scrolling more to right/down than previously
&1618 18       CLC
&1619 69 01    ADC #&01
; leave
&161b 60       RTS

; screen_scrolling_velocity_x
; screen_scrolling_velocity_y - 2
;      x     y
&161c 00 00 00                                                      # Second byte is unused

; update_screen_variables
&161f a0 00    LDY #&00
&1621 a2 02    LDX #&02
; calculate_tile_processing_flag_for_caching_loop                   # Loop through X = 2 for y, X = 0 for x
&1623 b5 cf    LDA &cf,X ; screen_tile_sections_to_scroll_x
&1625 f0 05    BEQ &162c ; not_scrolling
&1627 b5 c7    LDA &c7,X ; screen_origin_x_fraction                 # Is the edge of the screen aligned with a tile?
&1629 d0 01    BNE &162c ; not_aligned
&162b 88       DEY                                                  # If so, set top bit to indicate new tiles exposed
; not_aligned
; not_scrolling
&162c ca       DEX
&162d ca       DEX
&162e f0 f3    BEQ &1623 ; calculate_tile_processing_flag_for_caching_loop
&1630 8c cd 14 STY &14cd ; new_tiles_exposed_during_scrolling       # Set &80 to indicate new tiles exposed during scrolling
;                                                                   # and so call tile update routines when plotting strip
&1633 a2 02    LDX #&02
&1635 b5 cf    LDA &cf,X ; screen_tile_sections_to_scroll_x
&1637 0a       ASL A
&1638 a0 00    LDY #&00                                             # &00 to indicate scrolling right/down
&163a 90 01    BCC &163d ; not_negative
&163c 88       DEY                                                  # or &ff to indicate scrolling left/up
; not_negative
&163d e0 02    CPX #&02
&163f 90 01    BCC &1642 ; not_y
&1641 0a       ASL A                                                # One tile scroll section = &40 fraction for y
; not_y
&1642 0a       ASL A                                                #                         = &20 fraction for x
&1643 0a       ASL A                                                # (note doubling at &1637)
&1644 0a       ASL A
&1645 0a       ASL A
&1646 95 cb    STA &cb,X ; screen_tile_fractions_to_scroll_x
&1648 18       CLC
&1649 75 c7    ADC &c7,X ; screen_origin_x_fraction
&164b 95 c7    STA &c7,X ; screen_origin_x_fraction
&164d 98       TYA
&164e 95 cc    STA &cc,X ; screen_scrolling_sign_x                  # Set to &ff if scrolling left/up, &00 if right/down
&1650 75 c8    ADC &c8,X ; screen_origin_x
&1652 95 c8    STA &c8,X ; screen_origin_x
&1654 ca       DEX
&1655 ca       DEX
&1656 10 dd    BPL &1635 ; calculate_new_screen_origin_loop
; calculate_screen_start_offset_and_scaled_screen_offset            # screen_start_offset is added to screen addresses
&1658 a5 c7    LDA &c7 ; screen_origin_x_fraction                   # &6000 + screen_start_offset = top left of screen
&165a 85 b2    STA &b2 ; scaled_screen_start_offset_low             # scaled_screen_start_offset = screen_start_offset * 4
&165c 85 b0    STA &b0 ; screen_start_offset_low                    # Will be divided by four at &1686
&165e a5 ca    LDA &ca ; screen_origin_y
&1660 4a       LSR A
&1661 85 9b    STA &9b ; offset_low
&1663 a5 c9    LDA &c9 ; screen_origin_y_fraction
&1665 6a       ROR A
&1666 46 9b    LSR &9b ; offset_low
&1668 6a       ROR A
&1669 46 9b    LSR &9b ; offset_low
&166b 6a       ROR A
&166c 65 c8    ADC &c8 ; screen_origin_x
&166e 85 9c    STA &9c ; offset_high
&1670 a5 9b    LDA &9b ; offset_low
&1672 69 00    ADC #&00
&1674 a0 08    LDY #&08
; shift_loop
&1676 06 9c    ASL &9c ; offset_high
&1678 2a       ROL A
&1679 b0 04    BCS &167f ; wraparound
&167b c9 80    CMP #&80
&167d 90 02    BCC &1681 ; skip_wraparound
; wraparound
&167f e9 80    SBC #&80
; skip_wraparound
&1681 88       DEY
&1682 d0 f2    BNE &1676 ; shift_loop
&1684 85 b3    STA &b3 ; scaled_screen_start_offset_high            # Used for plotting sprites and pixels
&1686 4a       LSR A
&1687 66 b0    ROR &b0 ; screen_start_offset_low
&1689 4a       LSR A
&168a 66 b0    ROR &b0 ; screen_start_offset_low
&168c 85 b1    STA &b1 ; screen_start_offset_high                   # Used for wiping strips, divided by eight to set crtc
; calculate_screen_size_and_scrolling_variables
&168e a2 02    LDX #&02
; calculate_screen_size_and_scrolling_variables_loop                # Loop through X = 2 for y, X = 0 for x
&1690 a9 00    LDA #&00
&1692 38       SEC
&1693 f5 cb    SBC &cb,X ; screen_tile_fractions_to_scroll_x
&1695 a8       TAY                                                  # Y = - screen_tile_fractions_to_scroll_x
&1696 a9 00    LDA #&00
&1698 f5 cc    SBC &cc,X ; screen_scrolling_sign_x                  # &ff -> &00 if scrolling left/up, &00 -> &ff if right/down
&169a 10 03    BPL &169f ; set_offset_for_previous_sprites
&169c a9 00    LDA #&00                                             # Don't offset previous sprites when scrolling right or down
&169e a8       TAY
; set_offset_for_previous_sprites
&169f 9d 8e 0b STA &0b8e,X ; screen_scrolling_offset_x + 1 (previous sprites)
&16a2 98       TYA
&16a3 9d 8a 0b STA &0b8a,X ; screen_scrolling_offset_x_fraction + 1 (previous sprites)
&16a6 b5 cb    LDA &cb,X ; screen_tile_fractions_to_scroll_x
&16a8 b4 cc    LDY &cc,X ; screen_scrolling_sign_x                  # &ff if scrolling left/up, &00 if right/down
&16aa 18       CLC
&16ab 30 04    BMI &16b1 ; set_screen_size_for_previous_sprites
&16ad 49 ff    EOR #&ff                                             # Invert if scrolling right or down, to keep negative
&16af 38       SEC
&16b0 88       DEY
; set_screen_size_for_previous_sprites                              # Screen is smaller for unplotting sprites when scrolling
&16b1 7d 99 0b ADC &0b99,X ; screen_size_x_fraction (current sprites)
&16b4 9d 9a 0b STA &0b9a,X ; screen_size_x_fraction + 1 (previous sprites)
&16b7 98       TYA
&16b8 7d 9d 0b ADC &0b9d,X ; screen_size_x (current sprites)
&16bb 9d 9e 0b STA &0b9e,X ; screen_size_x + 1 (previous sprites)
&16be b5 c7    LDA &c7,X ; screen_origin_x_fraction
&16c0 9d 91 0b STA &0b91,X ; screen_start_x_fraction (current sprites)
&16c3 18       CLC
&16c4 7d 8a 0b ADC &0b8a,X ; screen_scrolling_offset_x_fraction + 1 (previous sprites)
&16c7 9d 92 0b STA &0b92,X ; screen_start_x_fraction + 1 (previous sprites)
&16ca b5 c8    LDA &c8,X ; screen_origin_x
&16cc 9d 95 0b STA &0b95,X ; screen_start_x (current sprites)
&16cf 7d 8e 0b ADC &0b8e,X ; screen_scrolling_offset_x + 1 (previous sprites)
&16d2 9d 96 0b STA &0b96,X ; screen_start_x + 1 (current sprites)
&16d5 ca       DEX
&16d6 ca       DEX
&16d7 10 b7    BPL &1690 ; calculate_screen_size_and_scrolling_variables_loop
; calculate_waterline_timer
&16d9 a5 c8    LDA &c8 ; screen_origin_x
&16db 20 bc 2c JSR &2cbc ; get_waterline_for_x
&16de a0 01    LDY #&01
&16e0 ad d0 14 LDA &14d0 ; waterline_y_fraction
&16e3 e5 c9    SBC &c9 ; screen_origin_y_fraction
&16e5 85 9c    STA &9c ; relative_y_fraction
&16e7 ad d1 14 LDA &14d1 ; waterline_y
&16ea e5 ca    SBC &ca ; screen_origin_y
&16ec 90 23    BCC &1711 ; set_waterline_timer_high                 # Set timer_high to 1 if entire screen below waterline
&16ee c9 04    CMP #&04
&16f0 b0 1e    BCS &1710 ; set_waterline_timer_high_to_zero         # Set timer_high to 0 if entire screen above waterline
&16f2 85 9d    STA &9d ; relative_y
&16f4 a5 9c    LDA &9c ; relative_y_fraction                        # &08 fraction = 1 pixel, so add &40 for each row
&16f6 0a       ASL A
&16f7 26 9d    ROL &9d ; relative_y
&16f9 0a       ASL A
&16fa 26 9d    ROL &9d ; relative_y
&16fc 0a       ASL A
&16fd 26 9d    ROL &9d ; relative_y
&16ff 29 c0    AND #&c0                                             # Round down to start of row
&1701 69 70    ADC #&70 ; &1770
&1703 78       SEI
&1704 8d ce 14 STA &14ce ; waterline_timer_low
&1707 a5 9d    LDA &9d ; relative_y
&1709 69 17    ADC #&17
&170b 8d cf 14 STA &14cf ; waterline_timer_high
&170e 58       CLI
&170f 60       RTS
; set_waterline_timer_high_to_zero
&1710 88       DEY
; set_waterline_timer_high
&1711 8c cf 14 STY &14cf ; waterline_timer_high
&1714 60       RTS

; get_tile_and_check_for_tertiary_objects
&1715 a2 00    LDX #&00
&1717 86 bd    STX &bd ; this_object_tertiary_data_offset
&1719 86 00    STX &00 ; tile_was_from_map_data                     # Clear top bit to indicate tile not from map data
&171b 20 8d 17 JSR &178d ; get_tile                                 # Returns A = tile_type_and_flip
&171e 85 08    STA &08 ; tile_type_and_flip
&1720 29 c0    AND #&c0 ; TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL
&1722 85 09    STA &09 ; tile_flip
&1724 45 08    EOR &08 ; tile_type_and_flip
&1726 c9 09    CMP #&09 ; TILE_CHECK_TERTIARY_OBJECT_RANGE_EIGHT + 1
&1728 b0 48    BCS &1772 ; skip_checking_tertiary_objects           # Tile type will be replaced at &1761 if <= &09
; check_for_tertiary_objects
&172a 85 9d    STA &9d ; tile_type
&172c a8       TAY
&172d be d4 05 LDX &05d4,Y ; tile_tertiary_object_ranges_table + 1  # Get offset of last tertiary object of next type, + 1
&1730 bd ef 05 LDA &05ef,X ; tertiary_objects_x
&1733 48       PHA ; previous_tertiary_object_x
&1734 a5 95    LDA &95 ; tile_x
&1736 9d ef 05 STA &05ef,X ; tertiary_objects_x                     # Temporarily use its value as a final one to check
&1739 be d3 05 LDX &05d3,Y ; tile_tertiary_object_ranges_table      # Get offset of first tertiary object of this type
&173c ca       DEX
; check_for_tertiary_objects_loop                                   # For each tertiary object of this type,
&173d e8       INX
&173e dd ef 05 CMP &05ef,X ; tertiary_objects_x                     # Is its x coordinate the same as the tile?
&1741 d0 fa    BNE &173d ; check_for_tertiary_objects_loop
&1743 8a       TXA ; tertiary_object_offset_in_range
&1744 d9 d4 05 CMP &05d4,Y ; tile_tertiary_object_ranges_table + 1  # Was a tertiary object found?
&1747 b0 11    BCS &175a ; add_feature_tile                         # If not, add a landscape feature
&1749 79 dd 05 ADC &05dd,Y ; tertiary_objects_data_offset
&174c 85 bd    STA &bd ; this_object_tertiary_data_offset           # Not all tertiary objects have a data byte
&174e 18       CLC
&174f 79 e6 05 ADC &05e6,Y ; tertiary_objects_type_offset
&1752 85 be    STA &be ; this_object_tertiary_type_offset           # Not all tertiary objects have a type byte
&1754 bd ee 06 LDA &06ee,X ; tertiary_objects_tile_and_flip
&1757 4c 61 17 JMP &1761 ; set_tile_type_and_flip                   # Use tile for tertiary object
; add_feature_tile
&175a a6 9d    LDX &9d ; tile_type                                  # Use the tile type to determine a feature to add
&175c bd 7c 11 LDA &117c,X ; feature_tiles_table
&175f 45 09    EOR &09 ; tile_flip                                  # keeping the flip given by the algorithm or map data
; set_tile_type_and_flip
&1761 85 08    STA &08 ; tile_type_and_flip
&1763 be d4 05 LDX &05d4,Y ; tile_tertiary_object_ranges_table + 1
&1766 68       PLA ; previous_tertiary_object_x
&1767 9d ef 05 STA &05ef,X ; tertiary_objects_x                     # Restore previous value of first object of next type
&176a a5 08    LDA &08 ; tile_type_and_flip
&176c 29 c0    AND #&c0 ; TILE_FLIP_HORIZONTAL | TILE_FLIP_VERTICAL
&176e 85 09    STA &09 ; tile_flip
&1770 45 08    EOR &08 ; tile_type_and_flip                         # Remove flip flags
; skip_checking_tertiary_objects
&1772 85 08    STA &08 ; tile_type                                  # Tile types are now those that will be used in the game
&1774 c9 10    CMP #&10 ; TILE_MUSHROOMS + 1
&1776 b0 14    BCS &178c ; leave                                    # Does this tile have an update routine?
&1778 ba       TSX
&1779 86 26    STX &26 ; stack_pointer_to_leave_to_if_unable_to_create_primary_object # Unnecessary code; overwritten at &19eb
&177b aa       TAX ; tile type
&177c bd 32 04 LDA &0432,X ; object_types_update_routine_addresses_high_table
&177f 24 2d    BIT &2d ; tile_processing_mode                       # Should the tile update routine be called for this function?
&1781 f0 07    BEQ &178a ; skip_calling_tile_update_routine         # If so, corresponding bit in high nibble will be set
&1783 29 0f    AND #&0f                                             # Low nibble is high byte of update routine address offset
&1785 a4 bd    LDY &bd ; this_object_tertiary_data_offset
&1787 20 ea 19 JSR &19ea ; call_tile_update_routine                 # This may convert a tertiary object into a primary one
; skip_calling_tile_update_routine
&178a a5 08    LDA &08 ; tile_type                                  # Leave with A = tile type
&178c 60       RTS

; get_tile
&178d a5 97    LDA &97 ; tile_y                                     # Calculate a triangular function of x and y, f1
&178f aa       TAX
&1790 4a       LSR A
&1791 45 95    EOR &95 ; tile_x
&1793 29 f8    AND #&f8
&1795 4a       LSR A
&1796 65 95    ADC &95 ; tile_x
&1798 4a       LSR A
&1799 65 97    ADC &97 ; tile_y
&179b 85 9d    STA &9d ; f1_tile_xy
&179d 8a       TXA ; tile_y                                         # Use f1 and y to determine whether to use mapped data
&179e c9 79    CMP #&79
&17a0 90 06    BCC &17a8 ; skip_subtraction
&17a2 c9 bf    CMP #&bf
&17a4 90 50    BCC &17f6 ; get_tile_from_algorithm_skipping_check
&17a6 e9 46    SBC #&46
; skip_subtraction
&17a8 c9 48    CMP #&48
&17aa b0 06    BCS &17b2 ; skip_addition
&17ac c9 3e    CMP #&3e
&17ae b0 46    BCS &17f6 ; get_tile_from_algorithm_skipping_check
&17b0 69 0a    ADC #&0a
; skip_addition
&17b2 85 10    STA &10 ; f2_tile_xy                                 # Calculate three permuted versions of f1
&17b4 29 a8    AND #&a8
&17b6 49 6f    EOR #&6f
&17b8 4a       LSR A
&17b9 65 95    ADC &95 ; tile_x
&17bb 49 60    EOR #&60
&17bd 69 28    ADC #&28
&17bf 85 0f    STA &0f ; f3_tile_xy
&17c1 29 38    AND #&38
&17c3 49 a4    EOR #&a4
&17c5 65 10    ADC &10 ; f2_tile_xy
&17c7 85 10    STA &10 ; f2_tile_xy
&17c9 a8       TAY
&17ca 49 2c    EOR #&2c
&17cc 65 0f    ADC &0f ; f3_tile_xy                                 # A = f4_tile_xy
&17ce c0 20    CPY #&20
&17d0 b0 24    BCS &17f6 ; get_tile_from_algorithm_skipping_check   # Use f3 to determine whether to use mapped data
&17d2 c9 20    CMP #&20
&17d4 b0 1c    BCS &17f2 ; get_tile_from_algorithm
; get_tile_from_map_data                                            # Use f2 and f4 to determine offset into map data
&17d6 c6 00    DEC &00 ; tile_was_from_map_data                     # Set top bit to indicate tile was from map data
&17d8 a8       TAY ; f4_tile_xy
&17d9 0a       ASL A
&17da 0a       ASL A
&17db 0a       ASL A
&17dc 45 10    EOR &10 ; f2_tile_xy
&17de 85 a4    STA &a4 ; map_data_address_low
&17e0 98       TYA ; f4_tile_xy
&17e1 29 03    AND #&03
&17e3 69 4f    ADC #&4f ; &4fec = map_data
&17e5 85 a5    STA &a5 ; map_data_address_high
&17e7 a0 ec    LDY #&ec
&17e9 b1 a4    LDA (&a4),Y ; map_data_address                       # Get tile type and flip from map data
&17eb 60       RTS

; to_get_tile_for_surface
&17ec 4c 37 19 JMP &1937 ; get_tile_for_surface

; to_leave_with_empty_tile
&17ef 4c d8 18 JMP &18d8 ; leave_with_empty_tile

; get_tile_from_algorithm                                           # A = f4_tile_xy, X = tile_y
&17f2 c9 3d    CMP #&3d
&17f4 90 f9    BCC &17ef ; to_leave_with_empty_tile                 # Add irregular caverns near mapped data
; get_tile_from_algorithm_skipping_check
&17f6 e0 4e    CPX #&4e                                             # If y < &4e, i.e. above surface, use TILE_SPACE
&17f8 90 f5    BCC &17ef ; to_leave_with_empty_tile
&17fa f0 f0    BEQ &17ec ; to_get_tile_for_surface
&17fc e0 4f    CPX #&4f
&17fe d0 0e    BNE &180e ; is_below_surface
; is_one_below_surface                                              # If y = &4f, i.e. one below surface, use solid layer
&1800 a5 95    LDA &95 ; tile_x                                     # except where removed by mapped data or irregular cavern
&1802 c9 40    CMP #&40
&1804 f0 05    BEQ &180b ; leave_with_leaf
&1806 a0 01    LDY #&01                                             # Use TILE_EARTH (earth_tiles_rotation_table + 1)
&1808 4c 27 19 JMP &1927 ; to_leave_with_tile_from_table
; leave_with_leaf
&180b a9 62    LDA #&62 ; TILE_LEAF | TILE_FLIP_VERTICAL            # Set (&40, &4f) to be a vertically flipped leaf
&180d 60       RTS                                                  # ((&40, &4e) is set to TILE_LEAF by tertiary object &1b)
; is_below_surface
&180e a4 9d    LDY &9d ; f1_tile_xy
&1810 a9 00    LDA #&00                                             # Use earth to fill in the sides and bottom of world
&1812 85 9d    STA &9d ; f1_tile_xy
&1814 a5 95    LDA &95 ; tile_x
&1816 24 97    BIT &97 ; tile_y
&1818 30 07    BMI &1821 ; is_bottom_half_of_world
; is_top_half_of_world                                              # If y < &80 (top half of world, top third of ground)
&181a 69 1d    ADC #&1d
&181c c9 5e    CMP #&5e                                             # Set x = &00 - &40 and &e3 - &ff to be solid
&181e 4c 25 18 JMP &1825 ; check_if_side_of_world
; is_bottom_half_of_world                                           # If y >= &80,
&1821 69 07    ADC #&07
&1823 c9 2b    CMP #&2b                                             # Set x = &00 - &23 and &f9 - &ff to be solid
; check_if_side_of_world
&1825 90 55    BCC &187c ; to_leave_with_earth_or_stone             # i.e. fill in different sized sides of the world
&1827 98       TYA ; f1_tile_xy
&1828 29 e8    AND #&e8                                             # Using f1 to generate a complicated pattern,
&182a c5 97    CMP &97 ; tile_y
&182c 90 4e    BCC &187c ; to_leave_with_earth_or_stone             # set the bottom and other parts of the world to be earth
&182e 84 9d    STY &9d ; f1_tile_xy                                 # Otherwise, use f1 to determine whether to use earth or stone
&1830 8a       TXA ; tile_y                                         # Calculate another function of x and y, f5
&1831 0a       ASL A
&1832 65 97    ADC &97 ; tile_y
&1834 4a       LSR A
&1835 65 97    ADC &97 ; tile_y
&1837 29 e0    AND #&e0
&1839 65 95    ADC &95 ; tile_x
&183b 29 e8    AND #&e8
&183d d0 13    BNE &1852 ; consider_passages_and_caverns            # Use f5 to determine where to add square caverns
; is_square_cavern
&183f a5 97    LDA &97 ; tile_y
&1841 10 ac    BPL &17ef ; to_leave_with_empty_tile
; is_windy_square_cavern                                            # which are windy in the bottom half of the world
&1843 a5 95    LDA &95 ; tile_x
&1845 4a       LSR A
&1846 4a       LSR A
&1847 4a       LSR A
&1848 aa       TAX
&1849 a9 0e    LDA #&0e ; TILE_VARIABLE_WIND
&184b e0 0a    CPX #&0a                                             # Is this one of the two square caverns below the
&184d d0 02    BNE &1851 ; skip_flip                                # west stone door? If so, use a constant downdraft
&184f a9 8e    LDA #&8e ; TILE_VARIABLE_WIND | TILE_FLIP_HORIZONTAL
; skip_flip
&1851 60       RTS

; consider_passages_and_caverns                                     # Consider horizontal, vertical and sloping passages
&1852 98       TYA ; f1_tile_xy                                     # Calculate another permuted version of f1, f6
&1853 4a       LSR A
&1854 4a       LSR A
&1855 29 30    AND #&30
&1857 4a       LSR A
&1858 65 95    ADC &95 ; tile_x
&185a 4a       LSR A
&185b 45 95    EOR &95 ; tile_x
&185d 4a       LSR A
&185e 45 95    EOR &95 ; tile_x
&1860 65 95    ADC &95 ; tile_x
&1862 29 fd    AND #&fd
&1864 45 95    EOR &95 ; tile_x
&1866 29 07    AND #&07
&1868 d0 0e    BNE &1878 ; not_vertical_shaft                       # Use f6 to determine where to add vertical shafts
&186a a5 95    LDA &95 ; tile_x
&186c 30 07    BMI &1875 ; leave_with_vertical_shaft
&186e 4a       LSR A
&186f 65 97    ADC &97 ; tile_y
&1871 29 30    AND #&30
&1873 f0 03    BEQ &1878 ; not_vertical_shaft
; leave_with_vertical_shaft
&1875 a9 08    LDA #&08 ; TILE_CHECK_TERTIARY_OBJECT_RANGE_EIGHT
&1877 60       RTS
; not_vertical_shaft
&1878 e0 52    CPX #&52                                             # Fill in a horizontal passage that would otherwise
&187a b0 03    BCS &187f ; not_close_to_surface                     # appear close to the surface
; to_leave_with_earth_or_stone
&187c 4c 1c 19 JMP &191c ; leave_with_earth_or_stone
; not_close_to_surface
&187f 98       TYA ; f1_tile_xy                                     # Calculate another permuted version of f1, f7
&1880 29 68    AND #&68
&1882 65 97    ADC &97 ; tile_y
&1884 4a       LSR A
&1885 65 97    ADC &97 ; tile_y
&1887 4a       LSR A
&1888 65 97    ADC &97 ; tile_y
&188a 29 fc    AND #&fc
&188c 45 97    EOR &97 ; tile_y
&188e 29 17    AND #&17
&1890 d0 4d    BNE &18df ; consider_sloping_passage                 # Use f7 to determine where to add sloped passages
; consider_horizontal_passage
&1892 98       TYA ; f1_tile_xy                                     # Calculate two permuted version of f1, f8 and f9
&1893 65 95    ADC &95 ; tile_x
&1895 29 50    AND #&50
&1897 f0 3f    BEQ &18d8 ; leave_with_empty_tile                    # Use f8 to determine where to add empty horizontal passages
&1899 25 95    AND &95 ; tile_x
&189b 4a       LSR A
&189c 4a       LSR A
&189d 65 97    ADC &97 ; tile_y
&189f 4a       LSR A
&18a0 4a       LSR A
&18a1 29 0f    AND #&0f
&18a3 c9 08    CMP #&08
&18a5 90 08    BCC &18af ; is_horizontal_passage_type_one           # Use f9 to determine where to add horizontal passages with features
; is_horizontal_passage_type_two
&18a7 24 9d    BIT &9d ; f1_tile_xy
&18a9 50 16    BVC &18c1 ; use_passage_feature
&18ab 09 04    ORA #&04
&18ad d0 12    BNE &18c1 ; use_passage_feature                      # Always branches
; is_horizontal_passage_type_one
&18af 85 9c    STA &9c ; special_type
&18b1 49 05    EOR #&05                                             # Use f9 to determine type of some special features
&18b3 c9 06    CMP #&06
&18b5 a5 9c    LDA &9c ; special_type
&18b7 b0 08    BCS &18c1 ; use_passage_feature
&18b9 98       TYA ; f1_tile_xy                                     # Calculate another permuted version of f1, f10
&18ba 4a       LSR A
&18bb 65 97    ADC &97 ; tile_y
&18bd 45 95    EOR &95 ; tile_x
&18bf 29 07    AND #&07                                             # Use f10 to determine type of some special features
; use_passage_feature
&18c1 18       CLC
&18c2 69 1d    ADC #&1d ; horizontal_passage_feature_tiles_table - earth_tiles_rotation_table
&18c4 48       PHA ; tile offset
&18c5 20 46 19 JSR &1946 ; calculate_slope_function_for_tile_x_y    # Returns carry clear if passage middle or edge
&18c8 68       PLA ; tile offset
&18c9 90 0d    BCC &18d8 ; leave_with_empty_tile                    # Don't add features where crossed by sloping passage
&18cb a8       TAY
&18cc b9 4f 11 LDA &114f,Y ; horizontal_passage_feature_tiles_table - &1d
&18cf a4 97    LDY &97 ; tile_y
&18d1 c0 e0    CPY #&e0
&18d3 d0 02    BNE &18d7 ; skip_flip
&18d5 49 40    EOR #&40 ; TILE_FLIP_VERTICAL                        # Flip mushrooms in flooded passage at bottom right of world
; skip_flip
&18d7 60       RTS
; leave_with_empty_tile
&18d8 a0 00    LDY #&00                                             # Use TILE_SPACE (earth_tiles_rotation_table + 0)
; leave_with_tile_from_table
&18da 38       SEC
&18db b9 4f 11 LDA &114f,Y ; earth_tiles_rotation_table
&18de 60       RTS

; consider_sloping_passage
&18df 20 46 19 JSR &1946 ; calculate_slope_function_for_tile_x_y    # Returns carry clear if passage, Y = slope type
&18e2 b0 38    BCS &191c ; leave_with_earth_or_stone
&18e4 c0 00    CPY #&00                                             # Y = 0 if empty centre of sloping passage
&18e6 f0 f2    BEQ &18da ; leave_with_tile_from_table               # Leave with TILE_SPACE
; is_sloping_passage_edge
&18e8 a5 9d    LDA &9d ; f1_tile_xy                                 # Calculate another permuted version of f1, f11
&18ea 48       PHA ; f1_tile_xy
&18eb 84 9c    STY &9c ; slope_type
&18ed 2a       ROL A                                                # 84218421 -> 8 4218421.
&18ee 2a       ROL A                                                #          -> 4 218421.8
&18ef 2a       ROL A                                                #          -> 2 18421.84
&18f0 29 01    AND #&01                                             #          ->   .......4
&18f2 2a       ROL A                                                #          ->   ......4.
&18f3 a8       TAY                                                  # Y = 0 or 2, depending on &40 of f1
&18f4 68       PLA ; f1_tile_xy
&18f5 65 95    ADC &95 ; tile_x
&18f7 2a       ROL A
&18f8 45 97    EOR &97 ; tile_y
&18fa 29 1a    AND #&1a
&18fc d0 15    BNE &1913 ; use_slope                                # Use f11 to determine where to add sloping passage features
; use_sloping_passage_feature
&18fe 98       TYA
&18ff a4 9c    LDY &9c ; slope_type
&1901 59 56 11 EOR &1156,Y ; tile_rotations_table - 2
&1904 29 7f    AND #&7f
&1906 c9 40    CMP #&40                                             # Set carry if TILE_FLIP_VERTICAL
&1908 2a       ROL A                                                # Set &01 if TILE_FLIP_VERTICAL
&1909 29 07    AND #&07
&190b aa       TAX                                                  # X = (slope_type * 2) + (1 if TILE_FLIP_VERTICAL)
&190c bd 60 11 LDA &1160,X ; sloping_passage_feature_tiles_table
&190f 59 56 11 EOR &1156,Y ; tile_rotations_table - 2               # Apply rotation to slope
&1912 60       RTS
; use_slope
&1913 b9 5c 11 LDA &115c,Y ; slope_tiles_table
&1916 a4 9c    LDY &9c ; slope_type
&1918 59 56 11 EOR &1156,Y ; tile_rotations_table - 2               # Apply rotation to slope
&191b 60       RTS

; leave_with_earth_or_stone
&191c a5 9d    LDA &9d ; f1_tile_xy                                 # Zero if filling sides or bottom or world
&191e 4a       LSR A                                                # 84218421 -> .8421842
&191f 4a       LSR A                                                #          -> ..842184
&1920 4a       LSR A                                                #          -> ...84218
&1921 29 0e    AND #&0e                                             #          -> ....421.
&1923 4a       LSR A                                                #          -> .....421
&1924 69 01    ADC #&01                                             # i.e. convert f1 into an offset between &01 and &08
&1926 a8       TAY
; to_leave_with_tile_from_table
&1927 4c da 18 JMP &18da ; leave_with_tile_from_table

; consider_adding_surface_feature
&192a 65 95    ADC &95 ; tile_x                                     # Use a function of x to determine which surface feature to add
&192c 2a       ROL A                                                # 84218421 -> 8 4218421.
&192d 2a       ROL A                                                #          -> 4 218421.8
&192e 2a       ROL A                                                #          -> 2 18421.84
&192f 29 02    AND #&02                                             #          -> 2 ......8.
&1931 69 19    ADC #&19 ; surface_feature_tiles_table - earth_tiles_rotation_table
&1933 a8       TAY
&1934 4c da 18 JMP &18da ; leave_with_tile_from_table

; get_tile_for_surface
&1937 a0 19    LDY #&19                                             # Use TILE_SPACE (earth_tiles_rotation_table + &19)
&1939 a5 95    LDA &95 ; tile_x
&193b 4a       LSR A
&193c 65 95    ADC &95 ; tile_x
&193e 29 17    AND #&17
&1940 d0 e8    BNE &192a ; consider_adding_surface_feature          # Use a function of x to determine where to add surface features
&1942 66 9d    ROR &9d ; f1_tile_xy
&1944 6a       ROR A                                                # Set &80 (TILE_HORIZONTAL_FLIP) depending on f1
&1945 60       RTS

; calculate_slope_function_for_tile_x_y
&1946 8a       TXA ; tile_y
&1947 4a       LSR A
&1948 45 97    EOR &97 ; tile_y
&194a 29 06    AND #&06
&194c d0 23    BNE &1971 ; skip_sloping_cavern
; consider_adding_sloping_cavern
&194e 98       TYA ; f1_tile_xy                                     # Use f1 to determine function
&194f a0 02    LDY #&02
&1951 29 20    AND #&20                                             # 84218421 -> ..2....
&1953 0a       ASL A                                                #          -> .4.....
&1954 0a       ASL A                                                #          -> 8......
&1955 49 e5    EOR #&e5 ; SBC                                       # i.e. either &65 (ADC) or &e5 (SBC)
&1957 8d 61 19 STA &1961 ; sloping_cavern_function_opcode
&195a 30 02    BMI &195e ; skip_flip
&195c a0 04    LDY #&04
; skip_flip
&195e 8a       TXA ; tile_y                                         # Use a function of X and Y to determine where to add sloping caverns
&195f 69 16    ADC #&16
; sloping_cavern_function_opcode
&1961 65 95    ADC &95 ; tile_x
#     or       SBC &23 ; tile_x
&1963 29 5f    AND #&5f
&1965 aa       TAX
&1966 ca       DEX
&1967 e0 0c    CPX #&0c
&1969 90 38    BCC &19a3 ; leave_with_zero_Y                        # Leave with zero to indicate middle of sloping cavern
&196b f0 38    BEQ &19a5 ; leave_with_carry_clear                   # Leave with Y non-zero to indicate edge of sloping cavern
&196d c8       INY
&196e e8       INX
&196f f0 34    BEQ &19a5 ; leave_with_carry_clear                   # Leave with Y non-zero to indicate edge of sloping cavern
; skip_sloping_cavern
&1971 a5 95    LDA &95 ; tile_x
&1973 4a       LSR A                                                # 84218421 -> .8421842 1
&1974 4a       LSR A                                                #          -> ..842184 2
&1975 4a       LSR A                                                #          -> ...84218 4
&1976 4a       LSR A                                                #          -> ....8421 8
&1977 b0 29    BCS &19a2 ; leave                                    # Break sloping passages every eight tiles
&1979 a9 01    LDA #&01                                             # Use a function of x and y to determine where to add / sloping passages
&197b 65 95    ADC &95 ; tile_x
&197d 65 97    ADC &97 ; tile_y
&197f 29 8f    AND #&8f
&1981 c9 01    CMP #&01
&1983 f0 1e    BEQ &19a3 ; leave_with_zero_Y                        # Leave with Y zero to indicate middle of passage
&1985 aa       TAX                                                  # Use a function of x and y to determine where to add \ sloping passages
&1986 38       SEC
&1987 a5 97    LDA &97 ; tile_y
&1989 e5 95    SBC &95 ; tile_x
&198b 29 2f    AND #&2f
&198d c9 01    CMP #&01
&198f f0 12    BEQ &19a3 ; leave_with_zero_Y                        # Leave with Y zero to indicate middle of passage
&1991 a0 02    LDY #&02
&1993 c9 02    CMP #&02
&1995 f0 0e    BEQ &19a5 ; leave_with_carry_clear                   # Leave with Y non-zero to indicate edge of passage
&1997 c8       INY ; 3
&1998 90 0b    BCC &19a5 ; leave_with_carry_clear                   # Leave with Y non-zero to indicate edge of passage
&199a c8       INY ; 4
&199b e0 02    CPX #&02
&199d f0 06    BEQ &19a5 ; leave_with_carry_clear                   # Leave with Y non-zero to indicate edge of passage
&199f c8       INY ; 5
&19a0 90 03    BCC &19a5 ; leave_with_carry_clear                   # Leave with Y non-zero to indicate edge of passage
; leave                                                             # Leave with carry set to indicate solid tile
&19a2 60       RTS
; leave_with_zero_Y
&19a3 a0 00    LDY #&00                                             # Leave with zero to indicate empty tile
; leave_with_carry_clear
&19a5 18       CLC                                                  # Leave with carry clear to indicate empty or sloping tile
&19a6 60       RTS

; distances_to_remove_objects_table                                 # &01 : used for objects with type flags &50
;      1  2  3                                                      # &02 : used for objects with type flags &20 and &60
&19a7 01 0c 04                                                      # &03 : used for objects with type flags &70

; player_is_east_of_76
&19aa 00                                                            # Negative if player x >= &76

; ship_moving
&19ab 00                                                            # Negative if player's ship is moving at end of game

; player_weights_when_holding_objects_table                         # Player is weight 3 when not holding anything
;      0  1  2  3  4  5  6
&19ac 02 02 03 04 04 05 06

; held_object_any_top_or_bottom_collision
&19b3 00                                                            # &80 set if held object collided with tiles or objects (not player)

; this_object_wedged
&19b4 00                                                            # &80 set if object wedged between obstacles to top and bottom

; player_is_completely_dematerialised
&19b5 00                                                            # Negative at midpoint of player teleport

; main_game_loop
&19b6 46 27    LSR &27 ; whistle_one_active                         # Clear top bit to indicate whistle one not being played
&19b8 e6 c0    INC &c0 ; frame_counter
&19ba a5 c0    LDA &c0 ; frame_counter
&19bc a0 ff    LDY #&ff
&19be a2 05    LDX #&05
; update_timers_loop                                                # Set every_sixty_four_frames (&c1) negative every 64 frames
&19c0 4a       LSR A                                                # Set every_thirty_two_frames (&c2) negative every 32 frames
&19c1 90 01    BCC &19c4 ; skip_updating_timer                      # Set every_sixteen_frames (&c3) negative every 16 frames
&19c3 c8       INY                                                  # Set every_eight_frames (&c4) negative every 8 frames
; skip_updating_timer                                               # Set every_four_frames (&c5) negative every 4 frames
&19c4 94 c1    STY &c1,X ; every_sixty_four_frames                  # Set every_two_frames (&c6) negative every 2 frames
&19c6 ca       DEX
&19c7 10 f7    BPL &19c0 ; update_timers_loop
&19c9 20 97 1f JSR &1f97 ; update_background_flash
&19cc 20 0b 1a JSR &1a0b ; update_objects
&19cf 20 9a 25 JSR &259a ; update_events
&19d2 a2 02    LDX #&02
; update_player_mushroom_timers_loop                                # Loop through X = 2 for blue, X = 1 for red
&19d4 bd 19 08 LDA &0819,X ; player_mushroom_timers - 1             # Non-zero if player is under influence of mushrooms
&19d7 f0 03    BEQ &19dc ; skip_updating_mushroom_timer
&19d9 de 19 08 DEC &0819,X ; player_mushroom_timers - 1
; skip_updating_mushroom_timer
&19dc ca       DEX
&19dd 10 f5    BPL &19d4 ; update_player_mushroom_timers_loop
&19df ad 1d 08 LDA &081d ; explosion_timer                          # Negative if explosion in progress
&19e2 f0 03    BEQ &19e7 ; skip_updating_explosion_timer
&19e4 ee 1d 08 INC &081d ; explosion_timer
; skip_updating_explosion_timer
&19e7 4c b6 19 JMP &19b6 ; main_game_loop

; call_tile_update_routine                                          # Called with A = high byte of update routine address offset
&19ea ba       TSX
&19eb 86 26    STX &26 ; stack_pointer_to_leave_to_if_unable_to_create_primary_object # Leave call_tile_update_routine if so
&19ed a6 08    LDX &08 ; tile_type
&19ef 10 04    BPL &19f5 ; call_update_routine                      # Always branches

; call_object_update_routine                                        # Called with A = object type + &14
&19f1 aa       TAX                                                  #             Y = this_object_touching
&19f2 bd 32 04 LDA &0432,X ; update_routine_addresses_high_table
; call_update_routine                                               # Called with X = routine number
&19f5 48       PHA ; update_routine_address_high
&19f6 bd b9 03 LDA &03b9,X ; update_routine_addresses_low_table
&19f9 18       CLC
&19fa 69 1a    ADC #&1a ; &3e1a = update_routines_base - 1
&19fc aa       TAX
&19fd 68       PLA ; update_routine_address_high
&19fe 29 3f    AND #&3f                                             # Lowest six bits is high byte of update routine address offset
&1a00 69 3e    ADC #&3e
&1a02 48       PHA ; update_routine_address_high                    # Push address of update routine to stack
&1a03 8a       TXA
&1a04 48       PHA ; update_routine_address_low
&1a05 a6 bc    LDX &bc ; this_object_data
&1a07 8a       TXA
&1a08 c0 00    CPY #&00
&1a0a 60       RTS                                                  # Leave via update routine
                                                                    # Call with A = X = this_object_data
                                                                    #      Y = this_object_touching
                                                                    #      negative set if not touching other object

; update_objects
&1a0b a2 00    LDX #&00
; update_objects_loop                                               # For each primary object,
&1a0d 86 aa    STX &aa ; this_object
&1a0f bd b4 08 LDA &08b4,X ; objects_y                              # Non-zero if object in slot
&1a12 d0 03    BNE &1a17 ; update_object
&1a14 4c 10 1e JMP &1e10 ; consider_next_object
; update_object
&1a17 85 55    STA &55 ; this_object_y
&1a19 85 56    STA &56 ; this_object_previous_y
&1a1b bd c6 08 LDA &08c6,X ; objects_flags
&1a1e 85 6f    STA &6f ; this_object_flags
&1a20 85 70    STA &70 ; this_object_previous_flags
&1a22 85 37    STA &37 ; this_object_x_flip                         # Set &80 if object is horizontally flipped
&1a24 0a       ASL A
&1a25 85 39    STA &39 ; this_object_y_flip                         # Set &80 if object is vertically flipped
&1a27 bd 91 08 LDA &0891,X ; objects_x
&1a2a 85 53    STA &53 ; this_object_x
&1a2c 85 54    STA &54 ; this_object_previous_x
&1a2e bd 80 08 LDA &0880,X ; objects_x_fraction
&1a31 85 4f    STA &4f ; this_object_x_fraction
&1a33 85 50    STA &50 ; this_object_previous_x_fraction
&1a35 bd f6 08 LDA &08f6,X ; objects_velocity_y
&1a38 85 45    STA &45 ; this_object_velocity_y
&1a3a 85 46    STA &46 ; this_object_previous_velocity_y
&1a3c bd e6 08 LDA &08e6,X ; objects_velocity_x
&1a3f 85 43    STA &43 ; this_object_velocity_x
&1a41 85 44    STA &44 ; this_object_previous_velocity_x
&1a43 bd a3 08 LDA &08a3,X ; objects_y_fraction
&1a46 85 51    STA &51 ; this_object_y_fraction
&1a48 85 52    STA &52 ; this_object_previous_y_fraction
&1a4a bd 70 08 LDA &0870,X ; objects_sprite
&1a4d 85 75    STA &75 ; this_object_sprite
&1a4f 85 76    STA &76 ; this_object_previous_sprite
&1a51 bd d6 08 LDA &08d6,X ; objects_palette
&1a54 85 73    STA &73 ; this_object_palette
&1a56 85 74    STA &74 ; this_object_previous_palette
&1a58 bd 60 08 LDA &0860,X ; objects_type
&1a5b 85 41    STA &41 ; this_object_type
&1a5d bd 66 09 LDA &0966,X ; objects_tertiary_data_offset
&1a60 85 3d    STA &3d ; this_object_tertiary_data_offset
&1a62 bd 06 09 LDA &0906,X ; objects_target_object_and_flags
&1a65 85 3e    STA &3e ; this_object_target_object_and_flags
&1a67 85 3f    STA &3f ; this_object_previous_target_object_and_flags
&1a69 29 1f    AND #&1f ; OBJECT_TARGET_OBJECT_MASK
&1a6b 85 0e    STA &0e ; this_object_target_object
&1a6d bd 16 09 LDA &0916,X ; objects_tx
&1a70 85 14    STA &14 ; this_object_tx
&1a72 bd 26 09 LDA &0926,X ; objects_energy
&1a75 85 15    STA &15 ; this_object_energy
&1a77 bd 36 09 LDA &0936,X ; objects_ty
&1a7a 85 16    STA &16 ; this_object_ty
&1a7c bd 46 09 LDA &0946,X ; objects_touching
&1a7f 85 3b    STA &3b ; this_object_touching
&1a81 bd 76 09 LDA &0976,X ; objects_state
&1a84 85 11    STA &11 ; this_object_state
&1a86 bd 56 09 LDA &0956,X ; objects_timer
&1a89 85 12    STA &12 ; this_object_timer
&1a8b 85 13    STA &13 ; this_object_previous_timer
&1a8d a5 aa    LDA &aa ; this_object
&1a8f 0a       ASL A
&1a90 0a       ASL A
&1a91 0a       ASL A
&1a92 0a       ASL A
&1a93 05 aa    ORA &aa ; this_object
&1a95 65 c0    ADC &c0 ; frame_counter
&1a97 85 06    STA &06 ; this_object_frame_counter                  # Each object has a pair of frame counters,
&1a99 29 0f    AND #&0f
&1a9b 85 07    STA &07 ; this_object_frame_counter_sixteen          # which are never the same as for any other object
&1a9d a5 53    LDA &53 ; this_object_x
&1a9f 20 bc 2c JSR &2cbc ; get_waterline_for_x                      # Sets waterline_y
&1aa2 a4 aa    LDY &aa ; this_object
&1aa4 cc d8 29 CPY &29d8 ; whistle_two_activating_object            # Did this object play whistle two? (player or red bird)
&1aa7 d0 03    BNE &1aac ; skip_clearing_whistle_two
&1aa9 6e d8 29 ROR &29d8 ; whistle_two_activating_object            # If so, set top bit to forget that
; skip_clearing_whistle_two
&1aac 20 20 1e JSR &1e20 ; get_object_weight
&1aaf 85 38    STA &38 ; this_object_weight
&1ab1 c9 07    CMP #&07
&1ab3 66 2c    ROR &2c ; this_object_static                         # Set top bit if object is static
&1ab5 10 03    BPL &1aba ; skip_zeroing_velocities
&1ab7 20 a3 28 JSR &28a3 ; set_this_object_velocities_to_zero       # Static objects don't move
; skip_zeroing_velocities
&1aba a6 75    LDX &75 ; this_object_sprite
&1abc bd 0c 5e LDA &5e0c,X ; sprites_width_and_horizontal_flip_table
&1abf 29 f0    AND #&f0                                             # 8421.... width in pixels, minus one
&1ac1 85 3a    STA &3a ; this_object_width
&1ac3 bd 89 5e LDA &5e89,X ; sprites_height_and_vertical_flip_table
&1ac6 29 f8    AND #&f8                                             # 84218... height in rows, minus one
&1ac8 85 3c    STA &3c ; this_object_height
&1aca a2 00    LDX #&00
&1acc 86 40    STX &40 ; this_object_acceleration_x
&1ace 86 42    STX &42 ; this_object_acceleration_y
&1ad0 86 d3    STX &d3 ; player_aiming_angle_acceleration
&1ad2 8e e5 29 STX &29e5 ; this_object_object_collision_y_flags     # Set to zero to indicate no collision by default
&1ad5 8e e6 29 STX &29e6 ; this_object_object_collision_x_flags     # Set to zero to indicate no collision by default
&1ad8 86 1d    STX &1d ; this_object_pre_collision_velocity_magnitude
&1ada 86 30    STX &30 ; child_object_created                       # Set to positive to indicate no child object created
&1adc 86 1c    STX &1c ; tile_collision_angle
&1ade ca       DEX ; &ff
&1adf 86 05    STX &05 ; player_is_upright                          # Set &80 to indicate player is upright by default
&1ae1 86 2b    STX &2b ; this_object_visibility                     # Set &80 to indicate object is not invisible
&1ae3 20 31 2a JSR &2a31 ; add_velocities_to_position
&1ae6 a6 aa    LDX &aa ; this_object
&1ae8 d0 0f    BNE &1af9 ; not_player
; is_player
&1aea a4 dd    LDY &dd ; player_object_held                         # Negative if no object held
&1aec 30 0b    BMI &1af9 ; not_holding_object
&1aee 20 20 1e JSR &1e20 ; get_object_weight
&1af1 a8       TAY
&1af2 b9 ac 19 LDA &19ac,Y ; player_weights_when_holding_objects_table
&1af5 85 38    STA &38 ; this_object_weight                         # Set player weight depending on what is held
&1af7 a6 aa    LDX &aa ; this_object
; not_holding_object
; not_player
&1af9 e4 dd    CPX &dd ; player_object_held
&1afb d0 50    BNE &1b4d ; not_held_object                          # Is this object being held by the player?
; is_held_object
&1afd ae 70 08 LDX &0870 ; objects_sprite + 0 (player)
&1b00 bd 89 5e LDA &5e89,X ; sprites_height_and_vertical_flip_table # Player height minus one pixel
&1b03 38       SEC
&1b04 e5 3c    SBC &3c  this_object_height                          # Align centre of held object and centre of player
&1b06 08       PHP ; object height minus player height sign
&1b07 6a       ROR A
&1b08 49 80    EOR #&80
&1b0a 29 f8    AND #&f8                                             # Round down to pixel
&1b0c 6d a3 08 ADC &08a3 ; objects_y_fraction
&1b0f 85 51    STA &51 ; this_object_y_fraction
&1b11 85 0c    STA &0c ; held_object_y_fraction
&1b13 ad b4 08 LDA &08b4 ; objects_y + 0 (player)
&1b16 69 00    ADC #&00
&1b18 28       PLP ; object height minus player height sign
&1b19 e9 00    SBC #&00
&1b1b 85 55    STA &55 ; this_object_y
&1b1d 85 0d    STA &0d ; held_object_y
&1b1f bd 0c 5e LDA &5e0c,X ; sprites_width_and_horizontal_flip_table # Player width minus one pixel
&1b22 69 0f    ADC #&0f ; 1 pixel - 1 + carry                       # Carry is set from &1b19, so add &10
&1b24 a2 00    LDX #&00 ; overflow or underflow
&1b26 2c c6 08 BIT &08c6 ; objects_flags + 0 (player)               # &80 set if flipped horizontally, i.e. facing left
&1b29 10 08    BPL &1b33 ; not_facing_left
; is_facing_left
&1b2b a5 3a    LDA &3a ; this_object_width
&1b2d 69 10    ADC #&10 ; 1 pixel
&1b2f ca       DEX ; &ff ; overflow or underflow
&1b30 20 56 32 JSR &3256 ; invert_if_negative                       # Subtract (width - 1) + 1 pixel if held object to left
; not_facing_left
&1b33 18       CLC
&1b34 6d 80 08 ADC &0880 ; objects_x_fraction + 0 (player)          # Position held object to side of player
&1b37 85 4f    STA &4f ; this_object_x_fraction
&1b39 85 0a    STA &0a ; held_object_x_fraction
&1b3b 8a       TXA ; overflow or underflow
&1b3c 6d 91 08 ADC &0891 ; objects_x + 0 (player)
&1b3f 85 53    STA &53 ; this_object_x
&1b41 85 0b    STA &0b ; held_object_x
&1b43 a0 00    LDY #&00 ; player
&1b45 20 b4 0b JSR &0bb4 ; set_this_object_velocities_from_object_Y # Held object follows player's velocity
&1b48 ad c6 08 LDA &08c6 ; objects_flags + 0 (player)
&1b4b 85 37    STA &37 ; this_object_x_flip                         # Held object follows player's horizontal flip
; not_held_object
&1b4d 20 48 2a JSR &2a48 ; calculate_this_object_maximum_x_y
&1b50 24 2c    BIT &2c ; this_object_static                         # Top bit set if object is static
&1b52 30 63    BMI &1bb7 ; skip_checking_for_collisions
&1b54 20 64 2a JSR &2a64 ; check_for_collisions
&1b57 a4 aa    LDY &aa ; this_object
&1b59 d0 17    BNE &1b72 ; not_player
; is_player
&1b5b a5 1c    LDA &1c ; tile_collision_angle                       # Calculate angle of player relative to collision
&1b5d e5 de    SBC &de ; player_angle
&1b5f e9 40    SBC #&40 ; 90 degrees                                # Zero if player's head-feet axis perpendicular to
&1b61 20 56 32 JSR &3256 ; invert_if_negative                       # surface, &40 (90 degrees) if parallel to surface
&1b64 4a       LSR A
&1b65 4a       LSR A                                                # Divide by four
&1b66 69 c0    ADC #&c0 ; -&40
&1b68 65 1d    ADC &1d ; this_object_pre_collision_velocity_magnitude
&1b6a 90 06    BCC &1b72 ; skip_body_collision_damage               # If (angle / 4) + speed - &40 >= 0,
&1b6c 4a       LSR A                                                # use half that value
&1b6d 20 0c 25 JSR &250c ; damage_object_without_destroying         # Damage player from body colliding with tiles
&1b70 85 15    STA &15 ; this_object_energy
; skip_body_collision_damage
; not_player
&1b72 a5 18    LDA &18 ; this_object_tile_collision_y_flags         # &80 set if collision to bottom from tiles
&1b74 0d e5 29 ORA &29e5 ; this_object_object_collision_y_flags     # &80 set if collision to bottom from other objects
&1b77 85 19    STA &19 ; this_object_any_bottom_collision           # Set &80 if either to indicate collision to bottom
&1b79 ad e5 29 LDA &29e5 ; this_object_object_collision_y_flags     # &40 set if collision to top from other objects
&1b7c 0a       ASL A                                                # &80 set if collision to top from other objects
&1b7d 05 1a    ORA &1a ; this_object_tile_collision_y_sign          # &80 set if collision to top from tiles
&1b7f 85 1a    STA &1a ; this_object_any_top_collision              # Set &80 if either to indicate collision to top
&1b81 4e b4 19 LSR &19b4 ; this_object_wedged                       # Clear &80 to indicate not wedged by default
&1b84 a5 6f    LDA &6f ; this_object_flags
&1b86 29 fd    AND #&fd ; !OBJECT_FLAG_NEWLY_CREATED
&1b88 85 6f    STA &6f ; this_object_flags
&1b8a 24 1a    BIT &1a ; this_object_any_collision
&1b8c 30 0a    BMI &1b98 ; not_supported                            # If there was no collision to this object's top,
&1b8e 24 19    BIT &19 ; this_object_any_bottom_collision
&1b90 10 25    BPL &1bb7 ; skip_moving_away_from_ground             # but there was a collision to this object's bottom,
; is_supported
&1b92 09 02    ORA #&02 ; OBJECT_FLAG_SUPPORTED                     # this object is supported by tiles or other objects
&1b94 85 6f    STA &6f ; this_object_flags
&1b96 d0 1f    BNE &1bb7 ; skip_moving_away_from_ground             # Always branches
; not_supported
&1b98 a5 70    LDA &70 ; this_object_previous_flags
&1b9a 29 02    AND #&02 ; OBJECT_FLAG_SUPPORTED
&1b9c f0 19    BEQ &1bb7 ; skip_moving_away_from_ground
; was_supported
&1b9e 38       SEC
&1b9f 6e b4 19 ROR &19b4 ; this_object_wedged                       # Set &80 to indicate object wedged between obstacles to top and bottom
&1ba2 a2 00    LDX #&00
&1ba4 a5 79    LDA &79 ; this_object_right_obstruction
&1ba6 c5 77    CMP &77 ; this_object_left_obstruction
&1ba8 f0 0d    BEQ &1bb7 ; skip_moving_object_out_of_wedge          # If the wedge slopes,
&1baa 08       PHP ; obstruction direction sign
&1bab a9 10    LDA #&10                                             # Push away from wedge, e.g. out of corner of slope
&1bad 28       PLP ; obstruction direction sign
&1bae 20 56 32 JSR &3256 ; invert_if_negative
&1bb1 20 38 2a JSR &2a38 ; add_A_to_position
&1bb4 20 48 2a JSR &2a48 ; calculate_this_object_maximum_x_y
; skip_moving_object_out_of_wedge
; skip_checking_for_collisions
&1bb7 a4 41    LDY &41 ; this_object_type
&1bb9 b9 54 03 LDA &0354,Y ; object_types_flags_table
&1bbc 85 9f    STA &9f ; this_object_type_flags
&1bbe 0a       ASL A                                                # 84218421 -> 4218421.
&1bbf 85 bf    STA &bf ; this_object_removal_flags                  # Set &80 (becomes &40) if OBJECT_TYPE_FLAG_DO_NOT_KEEP_AS_SECONDARY (&40) was set
&1bc1 0a       ASL A                                                #          -> 218421..
&1bc2 29 c0    AND #&c0                                             #          -> 21......
&1bc4 0a       ASL A                                                #          -> 1....... 2
&1bc5 2a       ROL A                                                #          -> .......2 1
&1bc6 2a       ROL A                                                #          -. ......21
;                                                                   # i.e. &01 set if OBJECT_TYPE_FLAG_KEEP_AS_TERTIARY set
;                                                                   #      &02 set if OBJECT_TYPE_FLAG_KEEP_AS_PRIMARY_FOR_LONGER set
&1bc7 f0 30    BEQ &1bf9 ; skip_distance_check                      # If neither, branch with carry clear, to clear &80 of
;                                                                   # this_object_removal_flags, i.e. not remove object
; consider_if_object_has_become_far_away
&1bc9 aa       TAX
&1bca e0 02    CPX #&02                                             # If OBJECT_TYPE_FLAG_KEEP_AS_PRIMARY_FOR_LONGER set,
&1bcc d0 0d    BNE &1bdb ; skip_considering_speed_and_support       # but not OBJECT_TYPE_FLAG_KEEP_AS_TERTIARY, 
&1bce 20 b6 3b JSR &3bb6 ; get_maximum_of_this_object_velocities    # Returns A = larger of x and y velocities
&1bd1 c9 05    CMP #&05
&1bd3 6a       ROR A
&1bd4 49 80    EOR #&80                                             # Set &80 if not moving quickly
&1bd6 25 19    AND &19 ; this_object_any_bottom_collision           # &80 set if collision to bottom, i.e. supported
&1bd8 10 01    BPL &1bdb ; skip_using_shorter_distance
&1bda e8       INX                                                  # If both, use 4 tiles as limit, not 12
; skip_using_shorter_distance
; skip_considering_speed_and_support
&1bdb bc a6 19 LDY &19a6,X ; distances_to_remove_objects_table - 1  # Y = distance to consider far away
&1bde a5 07    LDA &07 ; this_object_frame_counter_sixteen
&1be0 29 03    AND #&03
&1be2 c9 03    CMP #&03
&1be4 90 13    BCC &1bf9 ; skip_distance_check                      # Every four frames,
&1be6 a5 6f    LDA &6f ; this_object_flags
&1be8 29 14    AND #&14 ; OBJECT_FLAG_TELEPORTING | OBJECT_FLAG_NEWLY_CREATED
&1bea 18       CLC
&1beb d0 0c    BNE &1bf9 ; skip_distance_check                      # If the object isn't teleported or newly created,
&1bed a5 9f    LDA &9f ; this_object_type_flags
&1bef 29 08    AND #&08  ; OBJECT_TYPE_FLAG_SPAWNED_FROM_NEST       # If the object was spawned from a nest
&1bf1 2d b5 19 AND &19b5 ; player_is_completely_dematerialised      # and the player is briefly removed in teleportation,
&1bf4 d0 03    BNE &1bf9 ; skip_distance_check                      # branch with carry clear to suppress object removal
&1bf6 20 1d 11 JSR &111d ; check_if_this_object_is_far_away         # Returns carry set if far away
; skip_distance_check
&1bf9 66 bf    ROR &bf ; this_object_removal_flags                  # Set &80 if object is determined to be far away
&1bfb a5 6f    LDA &6f ; this_object_flags
&1bfd 29 10    AND #&10 ; OBJECT_FLAG_TELEPORTING
&1bff f0 46    BEQ &1c47 ; not_teleporting
; is_teleporting
&1c01 a5 12    LDA &12 ; this_object_timer (teleport timer)
&1c03 f0 39    BEQ &1c3e ; finish_teleporting
&1c05 c9 11    CMP #&11
&1c07 d0 0a    BNE &1c13 ; skip_temporary_removal
&1c09 85 55    STA &55 ; this_object_y                              # Set to &11 to briefly remove object during teleportation
&1c0b a6 aa    LDX &aa ; this_object                                # Zero if player
&1c0d d0 04    BNE &1c13 ; not_player
&1c0f ca       DEX ; &ff
&1c10 8e b5 19 STX &19b5 ; player_is_completely_dematerialised      # Set to negative to indicate player is briefly removed
; not_player
; skip_temporary_removal
&1c13 c9 10    CMP #&10
&1c15 d0 22    BNE &1c39 ; skip_changing_position
; change_position
&1c17 a6 aa    LDX &aa ; this_object                                # Zero if player
&1c19 d0 03    BNE &1c1e ; set_new_position_after_teleport
&1c1b 8e b5 19 STX &19b5 ; player_is_completely_dematerialised      # Set to positive to indicate player no longer removed
; set_new_position_after_teleport
&1c1e a2 02    LDX #&02
; set_new_position_after_teleport_loop                              # Loop through X = 2 for y, X = 0 for x
&1c20 b5 3a    LDA &3a,X ; this_object_width
&1c22 49 ff    EOR #&ff
&1c24 4a       LSR A
&1c25 95 4f    STA &4f,X ; this_object_x_fraction                   # Put teleported object in centre of tile
&1c27 b5 14    LDA &14,X ; this_object_tx
&1c29 95 53    STA &53,X ; this_object_x
&1c2b ca       DEX
&1c2c ca       DEX
&1c2d 10 f1    BPL &1c20 ; set_new_position_after_teleport_loop
&1c2f 20 a3 28 JSR &28a3 ; set_this_object_velocities_to_zero
&1c32 20 fa 13 JSR &13fa ; play_sound
&1c35 33 f3 63 f3                                                   # Play sound for object changing position in teleport
; skip_changing_position
&1c39 c6 12    DEC &12 ; this_object_timer (teleport timer)
&1c3b 4c 2e 1d JMP &1d2e ; skip_accelerating_object
; finish_teleporting
&1c3e a5 6f    LDA &6f ; this_object_flags
&1c40 29 ef    AND #&ef ; !OBJECT_FLAG_TELEPORTING
&1c42 85 6f    STA &6f ; this_object_flags
&1c44 20 49 25 JSR &2549 ; increase_energy_by_one
; not_teleporting
&1c47 a5 55    LDA &55 ; this_object_y
&1c49 c9 4f    CMP #&4f
&1c4b b0 45    BCS &1c92 ; skip_applying_surface_wind               # No wind below surface
; apply_surface_wind
&1c4d a9 00    LDA #&00
&1c4f 85 b4    STA &b4 ; vector_x
&1c51 85 b6    STA &b6 ; vector_y
&1c53 a5 55    LDA &55 ; this_object_y
&1c55 e9 4e    SBC #&4e                                             # Wind is centred around x = &4e (surface level)
&1c57 a2 02    LDX #&02
; apply_surface_wind_loop                                           # Loop through X = 2 for y, X = 0 for x
&1c59 a4 38    LDY &38 ; this_object_weight
&1c5b c8       INY
&1c5c 66 9d    ROR &9d ; wind_velocity_sign
&1c5e 20 54 32 JSR &3254 ; make_positive
&1c61 c9 1e    CMP #&1e
&1c63 90 22    BCC &1c87 ; consider_next_component                  # No wind less than &1e from centre
&1c65 c9 32    CMP #&32
&1c67 90 06    BCC &1c6f ; skip_increasing_wind
&1c69 88       DEY                                                  # Double wind at &32 from centre
&1c6a c9 3c    CMP #&3c
&1c6c 90 01    BCC &1c6f ; skip_increasing_wind
&1c6e 88       DEY                                                  # Double wind again at &3c from centre
; skip_increasing_wind
&1c6f e9 08    SBC #&08                                             # Use distance to set wind strength
&1c71 0a       ASL A
&1c72 10 04    BPL &1c78 ; skip_ceiling
&1c74 88       DEY                                                  # Use maximum wind in case of overflow
&1c75 88       DEY
&1c76 a9 7f    LDA #&7f
; skip_ceiling
&1c78 c8       INY
&1c79 10 02    BPL &1c7d ; skip_floor
&1c7b a0 00    LDY #&00
; skip_floor
&1c7d 24 9d    BIT &9d ; wind_velocity_sign
&1c7f 20 56 32 JSR &3256 ; invert_if_negative                       # Apply sign to velocity
&1c82 95 b4    STA &b4,X ; vector_x
&1c84 20 94 3f JSR &3f94 ; add_weighted_vector_component_to_this_object_velocity
; consider_next_component
&1c87 a5 53    LDA &53 ; this_object_x
&1c89 e9 9b    SBC #&9b                                             # Wind is centred around x = &9b
&1c8b ca       DEX
&1c8c ca       DEX
&1c8d f0 ca    BEQ &1c59 ; apply_surface_wind_loop
&1c8f 20 73 3f JSR &3f73 ; add_wind_particle_using_velocities
; skip_applying_surface_wind
&1c92 a6 3d    LDX &3d ; this_object_tertiary_data_offset
&1c94 bd 86 09 LDA &0986,X ; tertiary_objects_data
&1c97 85 bc    STA &bc ; this_object_data
&1c99 a5 41    LDA &41 ; this_object_type
&1c9b 18       CLC
&1c9c 69 14    ADC #&14                                             # Object routines follow tile and explosion routines
&1c9e a4 3b    LDY &3b ; this_object_touching
&1ca0 20 f1 19 JSR &19f1 ; call_object_update_routine               # Update object depending on type
&1ca3 a6 aa    LDX &aa ; this_object
&1ca5 e4 dd    CPX &dd ; player_object_held
&1ca7 d0 3a    BNE &1ce3 ; not_held_object
; consider_dropping_held_object
&1ca9 a2 02    LDX #&02
; consider_dropping_held_object_check_loop                          # Loop through X = 2 for y, X = 0 for x
&1cab b5 0a    LDA &0a,X ; held_object_x_fraction
&1cad f5 4f    SBC &4f,X ; this_object_x_fraction
&1caf 08       PHP ; sign of relative object position
&1cb0 69 30    ADC #&30                                             # &30 = 3 pixels horizontally, 6 pixels vertically
&1cb2 a8       TAY
&1cb3 b5 0b    LDA &0b,X ; held_object_x
&1cb5 69 00    ADC #&00
&1cb7 28       PLP ; sign of relative object position
&1cb8 f5 53    SBC &53,X ; this_object_x
&1cba d0 04    BNE &1cc0 ; drop_held_object
&1cbc c0 60    CPY #&60 ; &30 * 2
&1cbe 90 06    BCC &1cc6 ; skip_dropping_held_object                # Has the object moved too far from the player?
; drop_held_object
&1cc0 20 c8 32 JSR &32c8 ; handle_dropping_object                   # If so, drop it
&1cc3 20 aa 28 JSR &28aa ; set_this_object_position_from_previous_position
; skip_dropping_held_object
&1cc6 ca       DEX
&1cc7 ca       DEX
&1cc8 f0 e1    BEQ &1cab ; consider_dropping_held_object_check_loop
&1cca a5 3b    LDA &3b ; this_object_touching                       # &80 set if not touching another object
&1ccc f0 09    BEQ &1cd7 ; is_touching_player                       # If the held object isn't touching the player,
&1cce aa       TAX
&1ccf bc 60 08 LDY &0860,X ; objects_type
&1cd2 19 54 03 ORA &0354,Y ; object_types_flags_table               # &80 set if other object is intangible
&1cd5 49 80    EOR #&80                                             # Set &80 if touching a tangible object, not the player
; is_touching_player
&1cd7 05 1b    ORA &1b ; this_object_tile_top_or_bottom_collision   # &80 set if held object collided with tiles above or below
&1cd9 8d b3 19 STA &19b3 ; held_object_any_top_or_bottom_collision  # Set &80 if held object collided with anything above or below
&1cdc 10 05    BPL &1ce3 ; skip_setting_held_object_velocities_from_player
; set_held_object_velocities_from_player
&1cde a0 00    LDY #&00 ; OBJECT_SLOT_PLAYER                        # Keep held object moving with player even if it collided
&1ce0 20 a9 0b JSR &0ba9 ; set_object_Y_velocities_from_this_object
; skip_setting_held_object_velocities_from_player
; not_held_object
&1ce3 a5 15    LDA &15 ; this_object_energy                         # If zero, object is exploding
&1ce5 d0 0f    BNE &1cf6 ; not_exploding
; is_exploding
&1ce7 a4 41    LDY &41 ; this_object_type
&1ce9 b9 46 04 LDA &0446,Y ; object_types_update_routine_addresses_high_table + &16
&1cec 29 c0    AND #&c0                                             # 84...... explosion type
&1cee 0a       ASL A                                                # 84...... -> 8 4.......
&1cef 2a       ROL A                                                #          -> 4 .......8
&1cf0 2a       ROL A                                                #          ->   ......84
&1cf1 69 10    ADC #&10                                             # Explosion routines follow tile routines
&1cf3 20 f1 19 JSR &19f1 ; call_object_update_routine               # Update exploding object
; not_exploding
&1cf6 20 87 25 JSR &2587 ; rnd
&1cf9 4a       LSR A                                                # 84218421 -> .8421842
&1cfa 4a       LSR A                                                #          -> ..842184
&1cfb 09 01    ORA #&01
&1cfd cd 1a 08 CMP &081a ; player_mushroom_timers + 0 (red)         # If player is under the influence of red mushrooms,
&1d00 6a       ROR A                                                # randomly and increasingly make invisible objects visible
&1d01 49 ff    EOR #&ff
&1d03 05 2b    ORA &2b ; this_object_visibility                     # &80 set if not invisible
&1d05 85 2b    STA &2b ; this_object_visibility                     # Otherwise, set &80 to make visible
; consider_demoting_or_returning_object
&1d07 a4 41    LDY &41 ; this_object_type
&1d09 b9 54 03 LDA &0354,Y ; object_types_flags_table
&1d0c 29 18    AND #&18 ; OBJECT_TYPE_FLAG_KEEP_AS_TERTIARY | OBJECT_TYPE_FLAG_SPAWNED_FROM_NEST
&1d0e f0 1b    BEQ &1d2b ; not_tertiary_object_or_tertiary_object_spawn
&1d10 aa       TAX ; type flags
&1d11 a5 bc    LDA &bc ; this_object_data
&1d13 20 33 25 JSR &2533 ; check_if_object_is_pending_removal       # Returns carry set if object is pending removal
&1d16 b0 0e    BCS &1d26 ; skip_demoting_or_returning_object
&1d18 24 bf    BIT &bf ; this_object_removal_flags                  # &80 set if object was deemed too far away
&1d1a 10 0a    BPL &1d26 ; skip_demoting_or_returning_object
&1d1c e0 10    CPX #&10 ; OBJECT_TYPE_FLAG_KEEP_AS_TERTIARY
&1d1e f0 04    BEQ &1d24 ; demote_to_tertiary_object
; return_spawn_to_tertiary_object
&1d20 18       CLC
&1d21 69 04    ADC #&04                                             # Add 1 to number of creatures in nest or pipe
&1d23 2c 09 80 BIT &8009 ; (nop)
; demote_to_tertiary_object
#1d24          ORA #&80                                             # Set &80 to indicate now present as tertiary object,
#                                                                   # so can be recreated as a primary object if needed
; skip_demoting_or_returning_object
&1d26 a6 3d    LDX &3d ; this_object_tertiary_data_offset
&1d28 9d 86 09 STA &0986,X ; tertiary_objects_data
; not_tertiary_object_or_tertiary_object_spawn
&1d2b 20 01 1f JSR &1f01 ; apply_acceleration_to_velocities
; skip_accelerating_object
&1d2e a6 aa    LDX &aa ; this_object
&1d30 18       CLC                                                  # Clear carry to indicate object should be plotted
&1d31 f0 29    BEQ &1d5c ; skip_removing_primary_object
; not_player
&1d33 20 33 25 JSR &2533 ; check_if_object_is_pending_removal       # Returns carry set if object is pending removal
&1d36 b0 09    BCS &1d41 ; skip_converting_to_secondary
&1d38 24 bf    BIT &bf ; this_object_removal_flags
&1d3a 10 20    BPL &1d5c ; skip_removing_primary_object             # &80 set if object was deemed too far away
&1d3c 70 03    BVS &1d41 ; skip_demoting_to_secondary               # &40 set if object shouldn't become secondary
&1d3e 20 6e 0c JSR &0c6e ; demote_primary_object_to_secondary
; skip_demoting_to_secondary
&1d41 a5 aa    LDA &aa ; this_object
&1d43 20 29 1e JSR &1e29 ; remove_object_for_touching_and_targeting
&1d46 aa       TAX
&1d47 a9 00    LDA #&00 ; OBJECT_SLOT_PLAYER
&1d49 ec cb 14 CPX &14cb ; viewpoint_object                         # Is the viewpoint object being demoted?
&1d4c d0 03    BNE &1d51 ; not_player                               # Always branches in standard version
&1d4e 8d cc 14 STA &14cc ; new_viewpoint_object                     # If so, use the player as the viewpoint object
; not_player
&1d51 85 53    STA &53 ; this_object_x
&1d53 85 55    STA &55 ; this_object_y                              # Set to zero to remove primary object
&1d55 e4 dd    CPX &dd ; player_object_held
&1d57 d0 02    BNE &1d5b ; not_holding
&1d59 66 dd    ROR &dd ; player_object_held                         # Set top bit to stop holding object
; not_holding
&1d5b 38       SEC                                                  # Set carry to indicate object shouldn't be plotted
; skip_removing_primary_object
&1d5c 24 2b    BIT &2b ; this_object_visibility                     # Positive if invisible
&1d5e 30 01    BMI &1d61 ; is_visible                               # If the object is invisible,
&1d60 38       SEC                                                  # Set carry to indicate object shouldn't be plotted
; is_visible
&1d61 a5 70    LDA &70 ; this_object_previous_flags
&1d63 6a       ROR A                                                # 84218421 -> C8421842 1
&1d64 6a       ROR A                                                #          -> 1C842184 2
&1d65 29 c0    AND #&c0                                             #          -> 1C...... ; &01 = OBJECT_FLAG_NOT_PLOTTED
&1d67 85 9c    STA &9c ; invisibility_and_not_plotted
&1d69 4a       LSR A                                                #          -> .1C.....
&1d6a 4a       LSR A                                                #          -> ..1C....
&1d6b 05 9c    ORA &9c ; invisibility_and_not_plotted               #          -> 1C1C....
&1d6d 85 6e    STA &6e ; offscreen_flags                            # Flags will be rotated left when considering replotting
;                                                                   # If OBJECT_FLAG_NOT_PLOTTED was set, skip unplotting
;                                                                   # If object was invisible or removed, skip plotting
&1d6f a5 37    LDA &37 ; this_object_x_flip
&1d71 0a       ASL A                                                # Set what will become &80 if horizontally flipped
&1d72 a5 39    LDA &39 ; this_object_y_flip
&1d74 6a       ROR A                                                # Set what will become &40 if vertically flipped
&1d75 29 c0    AND #&c0
&1d77 85 71    STA &71 ; this_object_flip
&1d79 a5 6f    LDA &6f ; this_object_flags
&1d7b 29 c0    AND #&c0 ; OBJECT_FLAG_FLIP_HORIZONTAL | OBJECT_FLAG_FLIP_VERTICAL
&1d7d 85 72    STA &72 ; this_object_previous_flip
&1d7f 45 6f    EOR &6f ; this_object_flags
&1d81 45 71    EOR &71 ; this_object_flip
&1d83 29 f3    AND #&f3 ; !(OBJECT_FLAG_NEWLY_CREATED | OBJECT_FLAG_WAS_DAMAGED)
&1d85 85 6f    STA &6f ; this_object_flags
&1d87 a5 55    LDA &55 ; this_object_y
&1d89 9d b4 08 STA &08b4,X ; objects_y
&1d8c a5 53    LDA &53 ; this_object_x
&1d8e 9d 91 08 STA &0891,X ; objects_x
&1d91 a5 4f    LDA &4f ; this_object_x_fraction
&1d93 9d 80 08 STA &0880,X ; objects_x_fraction
&1d96 a5 45    LDA &45 ; this_object_velocity_y
&1d98 9d f6 08 STA &08f6,X ; objects_velocity_y
&1d9b a5 43    LDA &43 ; this_object_velocity_x
&1d9d 9d e6 08 STA &08e6,X ; objects_velocity_x
&1da0 a5 6f    LDA &6f ; this_object_flags
&1da2 9d c6 08 STA &08c6,X ; objects_flags
&1da5 a5 51    LDA &51 ; this_object_y_fraction
&1da7 9d a3 08 STA &08a3,X ; objects_y_fraction
&1daa a5 75    LDA &75 ; this_object_sprite
&1dac 9d 70 08 STA &0870,X ; objects_sprite
&1daf a5 73    LDA &73 ; this_object_palette
&1db1 9d d6 08 STA &08d6,X ; objects_palette
&1db4 a5 41    LDA &41 ; this_object_type
&1db6 9d 60 08 STA &0860,X ; objects_type
&1db9 a5 3d    LDA &3d ; this_object_tertiary_data_offset
&1dbb 9d 66 09 STA &0966,X ; objects_tertiary_data_offset
&1dbe a5 3e    LDA &3e ; this_object_target_object_and_flags
&1dc0 29 e0    AND #&e0 ; !(OBJECT_TARGET_OBJECT_MASK)
&1dc2 05 0e    ORA &0e ; this_object_target_object
&1dc4 9d 06 09 STA &0906,X ; objects_target_object_and_flags
&1dc7 a5 14    LDA &14 ; this_object_tx
&1dc9 9d 16 09 STA &0916,X ; objects_tx
&1dcc a5 15    LDA &15 ; this_object_energy
&1dce 9d 26 09 STA &0926,X ; objects_energy
&1dd1 a5 16    LDA &16 ; this_object_ty
&1dd3 9d 36 09 STA &0936,X ; objects_ty
&1dd6 a5 3b    LDA &3b ; this_object_touching
&1dd8 09 80    ORA #&80                                             # Set top bit to indicate not touching other object
&1dda 9d 46 09 STA &0946,X ; objects_touching
&1ddd a5 11    LDA &11 ; this_object_state
&1ddf 9d 76 09 STA &0976,X ; objects_state
&1de2 a5 12    LDA &12 ; this_object_timer
&1de4 9d 56 09 STA &0956,X ; objects_timer
&1de7 ec cb 14 CPX &14cb ; viewpoint_object                         # Always zero (player) in standard version
&1dea d0 0e    BNE &1dfa ; not_viewpoint_object
; is_viewpoint_object
&1dec 20 2a 15 JSR &152a ; consider_how_to_scroll_screen
&1def 20 1f 16 JSR &161f ; update_screen_variables
&1df2 20 84 36 JSR &3684 ; prepare_screen_for_scrolling             # Wipe strip of screen and cache tiles
&1df5 a2 00    LDX #&00
&1df7 8e 89 2e STX &2e89 ; suppress_physical_screen_scrolling       # Set to zero to allow physical screen scrolling
; not_viewpoint_object
&1dfa 20 a5 0c JSR &0ca5 ; update_object_sprite                     # Replot object
&1dfd ec cb 14 CPX &14cb ; viewpoint_object
&1e00 d0 0e    BNE &1e10 ; consider_next_object
; is_viewpoint_object
&1e02 20 d2 10 JSR &10d2 ; plot_tile_strip
&1e05 20 58 1f JSR &1f58 ; consider_setting_crtc_start_address
&1e08 20 7e 20 JSR &207e ; update_particles
&1e0b 20 e8 0b JSR &0be8 ; consider_promoting_secondary_objects
&1e0e a6 aa    LDX &aa ; this_object
; consider_next_object
&1e10 e8       INX
&1e11 e0 10    CPX #&10
&1e13 b0 03    BCS &1e18 ; leave
&1e15 4c 0d 1a JMP &1a0d ; update_objects_loop
; leave
&1e18 60       RTS

; handle_swapping_direction
&1e19 a9 80    LDA #&80
&1e1b 45 df    EOR &df ; player_facing
&1e1d 85 df    STA &df ; player_facing
&1e1f 60       RTS

; get_object_weight
&1e20 a9 07    LDA #&07 ; OBJECT_TYPE_FLAG_WEIGHT_MASK
; get_object_type_flags
&1e22 be 60 08 LDX &0860,Y ; objects_type
&1e25 3d 54 03 AND &0354,X ; object_types_flags_table
&1e28 60       RTS

; remove_object_for_touching_and_targeting                          # Called with A = object to remove
&1e29 a2 0f    LDX #&0f
; remove_object_for_touching_and_targeting_loop                     # For each of the other objects,
&1e2b dd 46 09 CMP &0946,X ; objects_touching
&1e2e d0 03    BNE &1e33 ; not_touching                             # Was the other object touching this one?
&1e30 7e 46 09 ROR &0946,X ; objects_touching                       # Set top bit to stop other object touching this one
; not_touching
&1e33 48       PHA ; object to remove
&1e34 5d 06 09 EOR &0906,X ; objects_target_object_and_flags
&1e37 29 1f    AND #&1f ; OBJECT_TARGET_OBJECT_MASK
&1e39 d0 04    BNE &1e3f ; not_targeting                            # Was the other object targeting this one?
&1e3b 8a       TXA
&1e3c 9d 06 09 STA &0906,X ; objects_target_object_and_flags        # Set other object targeting itself, i.e. not this one
; not_targeting
&1e3f 68       PLA ; object to remove
&1e40 ca       DEX
&1e41 10 e8    BPL &1e2b ; remove_object_for_touching_and_targeting_loop
&1e43 60       RTS

; water_velocities_table
;      -  v  h vh                                                   # Top nibble is y velocity, bottom nibble is x velocity
&1e44 00 80 07 70                                                   # Second byte is unused

; colour_3_pixel_values_table
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f               #  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
&1e48 00 03 0c 0f 30 33 3c 3f c0 c3 cc cf f0 f3 fc ff               # KK RR GG YY BB MM CC WW kk rr gg yy bb mm cc ww

; number_of_particles
&1e58 ff

; number_of_particles_times_eight
&1e59 f8

; create_new_object_even_if_no_slots_free
&1e5a a0 00    LDY #&00
&1e5c 2c a0 01 BIT &01a0 ; (nop)
; create_new_object_if_one_slot_free
#1e5d          LDY #&01
&1e5f 2c a0 04 BIT &04a0 ; (nop)
; create_new_object_if_four_slots_free
#1e60          LDY #&04
; create_new_object_if_Y_slots_free                                 # Called with Y = number of slots that must be free
&1e62 8d c5 1e STA &1ec5 ; new_object_type                          #             A = new object type
&1e65 8e ff 1e STX &1eff ; tmp_x
&1e68 98       TYA
&1e69 aa       TAX
&1e6a d0 49    BNE &1eb5 ; find_free_object_slot                    # If this object is high priority,
; find_most_distant_object                                          # Find a free slot or an object that can be replaced
&1e6c 84 2e    STY &2e ; furthest_distance                          # Set to zero
&1e6e 84 2f    STY &2f ; most_distance_object                       # Set to zero
&1e70 a0 0f    LDY #&0f
; find_most_distant_object_loop
&1e72 c4 aa    CPY &aa ; this_object
&1e74 f0 1f    BEQ &1e95 ; consider_next_object                     # Don't replace the object doing the creating
&1e76 b9 b4 08 LDA &08b4,Y ; objects_y
&1e79 f0 49    BEQ &1ec4 ; create_new_object_in_slot_Y              # Zero if no object in slot; if so, use this slot
&1e7b a9 50    LDA #&50 ; OBJECT_TYPE_FLAG_DO_NOT_KEEP_AS_SECONDARY | OBJECT_TYPE_FLAG_KEEP_AS_TERTIARY
&1e7d 20 22 1e JSR &1e22 ; get_object_type_flags
&1e80 c9 40    CMP #&40 ; OBJECT_TYPE_FLAG_DO_NOT_KEEP_AS_SECONDARY
&1e82 d0 11    BNE &1e95 ; consider_next_object
&1e84 b9 c6 08 LDA &08c6,Y ; objects_flags                          # &01 set if OBJECT_FLAG_NOT_PLOTTED set
&1e87 6a       ROR A
&1e88 90 0b    BCC &1e95 ; consider_next_object                     # Don't replace an object that is currently plotted
&1e8a 20 5b 35 JSR &355b ; get_object_Y_distance_from_screen_centre
&1e8d c5 2e    CMP &2e ; furthest_distance
&1e8f 90 04    BCC &1e95 ; consider_next_object                     # Is this the most distant object found so far?
&1e91 85 2e    STA &2e ; furthest_distance
&1e93 84 2f    STY &2f ; most_distance_object                       # If so, note it as a possibility for replacement
; consider_next_object
&1e95 88       DEY
&1e96 d0 da    BNE &1e72 ; find_most_distant_object_loop
&1e98 38       SEC
&1e99 a4 2f    LDY &2f ; most_distance_object                       # Zero if no object was suitable for replacing
&1e9b f0 61    BEQ &1efe ; leave_after_restoring_X                  # Leave with carry set to indicate no object created
&1e9d a9 08    LDA #&08 ; OBJECT_TYPE_FLAG_SPAWNED_FROM_NEST
&1e9f 20 22 1e JSR &1e22 ; get_object_type_flags
&1ea2 f0 0b    BEQ &1eaf ; skip_returning_spawn_to_nest             # Did the object come from a nest or a pipe?
&1ea4 be 66 09 LDX &0966,Y ; objects_tertiary_data_offset
&1ea7 bd 86 09 LDA &0986,X ; tertiary_objects_data
&1eaa 69 03    ADC #&03                                             # Carry is set, so add 4; increase nest contents by 1
&1eac 9d 86 09 STA &0986,X ; tertiary_objects_data
; skip_returning_spawn_to_nest
&1eaf 98       TYA
&1eb0 20 29 1e JSR &1e29 ; remove_object_for_touching_and_targeting # Returns negative
&1eb3 30 0f    BMI &1ec4 ; create_new_object_in_slot_Y              # Always branches
; find_free_object_slot
&1eb5 a0 00    LDY #&00
; find_free_object_slot_loop
&1eb7 c8       INY
&1eb8 c0 10    CPY #&10
&1eba b0 42    BCS &1efe ; leave_after_restoring_X                  # Leave with carry set to indicate no object created
&1ebc b9 b4 08 LDA &08b4,Y ; objects_y                              # Zero if no object in slot
&1ebf d0 f6    BNE &1eb7 ; find_free_object_slot_loop
&1ec1 ca       DEX
&1ec2 d0 f3    BNE &1eb7 ; find_free_object_slot_loop
; create_new_object_in_slot_Y
&1ec4 a2 00    LDX #&00
#     actually LDX new_object_type
&1ec6 bd ef 02 LDA &02ef,X ; object_types_palette_and_pickup_table
&1ec9 29 7f    AND #&7f                                             # .4218421 palette
&1ecb 99 d6 08 STA &08d6,Y ; objects_palette
&1ece bd 8a 02 LDA &028a,X ; object_types_sprite_table
&1ed1 99 70 08 STA &0870,Y ; objects_sprite                         # Use sprite for type by default
&1ed4 a9 05    LDA #&05 ; OBJECT_FLAG_NEWLY_CREATED | OBJECT_FLAG_NOT_PLOTTED
&1ed6 99 c6 08 STA &08c6,Y ; objects_flags
&1ed9 a9 ff    LDA #&ff
&1edb 99 46 09 STA &0946,Y ; objects_touching                       # Set to negative to indicate not touching other object
&1ede 98       TYA
&1edf 99 06 09 STA &0906,Y ; objects_target_object_and_flags        # Set to new object to indicate not targeting other object
&1ee2 a9 00    LDA #&00
&1ee4 99 66 09 STA &0966,Y ; objects_tertiary_data_offset           # Set to zero to indicate no tertiary data
&1ee7 99 76 09 STA &0976,Y ; objects_state
&1eea 99 56 09 STA &0956,Y ; objects_timer
&1eed 99 e6 08 STA &08e6,Y ; objects_velocity_x
&1ef0 99 f6 08 STA &08f6,Y ; objects_velocity_y
&1ef3 8a       TXA
&1ef4 99 60 08 STA &0860,Y ; objects_type
&1ef7 20 a3 2d JSR &2da3 ; set_object_energy_from_type
&1efa 20 78 28 JSR &2878 ; set_object_x_y_from_this_object_x_y
&1efd 18       CLC                                                  # Leave with carry clear to indicate object created
;                                                                   #            Y = slot
; leave_after_restoring_X
&1efe a2 00    LDX #&00
#     actually LDX tmp_x
&1f00 60       RTS

; apply_acceleration_to_velocities
&1f01 a2 02    LDX #&02
; apply_acceleration_to_velocities_loop                             # Loop through X = 2 for y, X = 0 for x
&1f03 e0 02    CPX #&02                                             # Set carry if y, clear carry if x
;                                                                   # i.e. add one to y acceleration for gravity
&1f05 b5 40    LDA &40,X ; this_object_acceleration_x
&1f07 08       PHP ; acceleration sign
&1f08 75 43    ADC &43,X ; this_object_velocity_x
&1f0a 20 7f 32 JSR &327f ; prevent_overflow
&1f0d a8       TAY ; new velocity
&1f0e 28       PLP ; acceleration sign
&1f0f 20 56 32 JSR &3256 ; invert_if_negative
&1f12 e9 3f    SBC #&3f
&1f14 c9 40    CMP #&40
&1f16 b0 12    BCS &1f2a ; skip_limiting_velocity
&1f18 b4 43    LDY &43,X ; this_object_velocity_x
&1f1a 98       TYA ; old velocity
&1f1b 20 56 32 JSR &3256 ; invert_if_negative
&1f1e c9 40    CMP #&40
&1f20 b0 08    BCS &1f2a ; skip_limiting_velocity
&1f22 a9 40    LDA #&40                                             # Limit velocity to +/- &40
&1f24 c0 00    CPY #&00                                             # Keep sign of velocity
&1f26 20 56 32 JSR &3256 ; invert_if_negative
&1f29 a8       TAY ; new velocity
; skip_limiting_velocity
&1f2a 24 c3    BIT &c3 ; every_sixteen_frames
&1f2c 10 08    BPL &1f36 ; skip_applying_inertia
&1f2e 98       TYA ; new velocity
&1f2f f0 05    BEQ &1f36 ; skip_applying_inertia                    # If the velocity is not zero,
&1f31 10 02    BPL &1f35 ; apply_inertia_to_positive_velocities     # reduce its absolute value by one to apply inertia
; apply_inertia_to_negative_velocities
&1f33 c8       INY
&1f34 c9 88    CMP #&88 ; (nop)
; apply_intertia_to_positive_velocities
#1f35          DEY
; skip_applying_inertia
&1f36 94 43    STY &43,X ; this_object_velocity_x
&1f38 ca       DEX
&1f39 ca       DEX
&1f3a 10 c7    BPL &1f03 ; apply_acceleration_to_velocities_loop
&1f3c 60       RTS

; add_jetpack_thrust_particles
&1f3d a5 40    LDA &40 ; this_object_acceleration_x
&1f3f 05 42    ORA &42 ; this_object_acceleration_y                 # If not accelerating,
&1f41 f0 4e    BEQ &1f91 ; leave                                    # don't add particles, but leave with zero
;                                                                   # If SPRITE_SPACESUIT_FORTY_FIVE_UP or SPRITE_SPACESUIT_VERTICAL,
&1f43 a9 ed    LDA #&ed ; 1110 1101                                 # Use horizontal edge, consider flipping
&1f45 a4 75    LDY &75 ; this_object_sprite
&1f47 c0 02    CPY #&02 ; SPRITE_SPACESUIT_JUMPING
&1f49 b0 02    BCS &1f4d ; set_jetpack_particles_flags
; is_horizontal_or_forty_five_down                                  # If SPRITE_SPACESUIT_HORIZONTAL or SPRITE_SPACESUIT_FORTY_FIVE_DOWN.
&1f4b a9 eb    LDA #&eb ; 1110 1011                                 # Use horizontal centre, don't consider flipping
; set_jetpack_particles_flags
&1f4d 8d 17 02 STA &0217 ; particle_types_flags_table + &0b (PARTICLE_JETPACK)
&1f50 a0 0b    LDY #&0b ; PARTICLE_JETPACK
&1f52 20 8c 21 JSR &218c ; add_particle
&1f55 a9 ff    LDA #&ff                                             # Leave with non-zero to indicate accelerating
&1f57 60       RTS

; consider_setting_crtc_start_address
&1f58 ad 89 2e LDA &2e89 ; suppress_physical_screen_scrolling       # Zero when updating player object
&1f5b d0 34    BNE &1f91 ; leave
&1f5d ce 89 2e DEC &2e89 ; suppress_physical_screen_scrolling       # Set to non-zero to suppress scrolling again
; wait_for_vsync
&1f60 ad e4 11 LDA &11e4 ; vsync_state                              # Incremented every v-sync
&1f63 c9 02    CMP #&02
&1f65 90 f9    BCC &1f60 ; wait_for_vsync
&1f67 a9 00    LDA #&00
&1f69 8d e4 11 STA &11e4 ; vsync_state
; set_crtc_start_address
&1f6c 08       PHP ; flags
&1f6d 78       SEI
&1f6e a5 b0    LDA &b0 ; screen_start_offset_low
&1f70 85 9c    STA &9c ; crtc_address_low
&1f72 a5 b1    LDA &b1 ; screen_start_offset_high
&1f74 4a       LSR A
&1f75 66 9c    ROR &9c ; crtc_address_low
&1f77 4a       LSR A
&1f78 66 9c    ROR &9c ; crtc_address_low
&1f7a 4a       LSR A
&1f7b 66 9c    ROR &9c ; crtc_address_low
&1f7d 69 0c    ADC #&0c ; &6000 (screen_memory) / &800
&1f7f a0 0c    LDY #&0c ; R12 Displayed screen start address register (high)
&1f81 8c 00 fe STY &fe00 ; video register number
&1f84 8d 01 fe STA &fe01 ; video register value
&1f87 a5 9c    LDA &9c
&1f89 c8       INY ; &0d ; R13: Displayed screen start address register (low)
&1f8a 8c 00 fe STY &fe00 ; video register number
&1f8d 8d 01 fe STA &fe01 ; video register value
&1f90 28       PLP ; flags
; leave
&1f91 60       RTS

; flash_background
&1f92 a0 0b    LDY #&0b                                             # Flash background for 10 frames
&1f94 84 2a    STY &2a ; background_flash_cooldown
&1f96 60       RTS

; update_background_flash
&1f97 a4 2a    LDY &2a ; background_flash_cooldown
&1f99 f0 f6    BEQ &1f91 ; leave
&1f9b c6 2a    DEC &2a ; background_flash_cooldown
&1f9d f0 07    BEQ &1fa6 ; set_colour_zero_to_black
&1f9f 20 87 25 JSR &2587 ; rnd
&1fa2 29 20    AND #&20
&1fa4 f0 02    BEQ &1fa8 ; set_colour_zero                          # 1 in 8 chance of setting colour 0 to white
; set_colour_zero_to_black
&1fa6 a9 07    LDA #&07
; set_colour_zero
&1fa8 8d e5 11 STA &11e5 ; palette_registers_table + 0 (K)
&1fab ee be 14 INC &14be ; palette_registers_need_updating          # Set to non-zero to indicate registers need updating
&1fae 60       RTS

; check_if_object_Y_damaged_by_projectiles
&1faf a4 3b    LDY &3b ; this_object_touching
&1fb1 30 14    BMI &1fc7 ; leave_with_negative
&1fb3 b9 60 08 LDA &0860,Y ; objects_type
&1fb6 c9 44    CMP #&44 ; OBJECT_EXPLOSION                          # Explosions can't be damaged
&1fb8 f0 0b    BEQ &1fc5 ; leave_with_negative
&1fba c9 40    CMP #&40 ; OBJECT_BUSH                               # Bushes can't be damaged
&1fbc f0 07    BEQ &1fc5 ; leave_with_negative
&1fbe 38       SEC
&1fbf e9 25    SBC #&25 ; OBJECT_RED_CLAWED_ROBOT                   # Red clawed robot and Triax can't be damaged
&1fc1 c9 02    CMP #&02 ; OBJECT_TRIAX - OBJECT_RED_CLAWED_ROBOT + 1
&1fc3 b0 02    BCS &1fc7 ; leave_with_Y
; leave_with_negative
&1fc5 a0 ff    LDY #&ff                                             # Leave with negative if object can't be damaged
; leave_with_Y
&1fc7 98       TYA                                                  # Leave with positive if object can be damaged
&1fc8 60       RTS

; check_if_object_Y_collides_with_lightning_or_fireball
&1fc9 20 ad 2d JSR &2dad ; get_range_for_object_Y
&1fcc e0 04    CPX #&04 ; OBJECT_RANGE_PROJECTILES
&1fce f0 0a    BEQ &1fda ; leave                                    # Leave with zero to indicate object can't collide
; check_if_object_collides_with_plasma_ball
&1fd0 c9 40    CMP #&40 ; OBJECT_EXPLOSION
&1fd2 f0 06    BEQ &1fda ; leave                                    # Leave with zero to indicate object can't collide
&1fd4 c9 44    CMP #&44 ; OBJECT_BUSH
&1fd6 f0 02    BEQ &1fda ; leave                                    # Leave with zero to indicate object can't collide
&1fd8 c9 37    CMP #&37 ; OBJECT_FIREBALL
; leave
&1fda 60       RTS                                                  # Leave with non-zero to indicate object can collide

; plot_pixel
&1fdb a5 93    LDA &93 ; pixel_y_fraction                           # Convert pixel_y into row on screen
&1fdd 46 94    LSR &94 ; pixel_y
&1fdf 6a       ROR A
&1fe0 46 94    LSR &94 ; pixel_y
&1fe2 6a       ROR A
&1fe3 46 94    LSR &94 ; pixel_y
&1fe5 6a       ROR A
&1fe6 aa       TAX                                                  # X = screen row
&1fe7 29 07    AND #&07
&1fe9 a8       TAY                                                  # Y = row in group
&1fea a5 91    LDA &91 ; pixel_x_fraction
&1fec 29 e0    AND #&e0                                             # Round pixel_x to &20 fraction (one byte)
&1fee 65 b2    ADC &b2 ; scaled_screen_start_offset_low             # &20 fraction = 8 byte column of two pixels
&1ff0 85 8f    STA &8f ; screen_address_low                         # so divide by 4 (&20 / 8) to give screen address
&1ff2 8a       TXA
&1ff3 29 f8    AND #&f8                                             # hhh84218 -> hhh84... i.e. group
&1ff5 45 92    EOR &92 ; pixel_x
&1ff7 65 b3    ADC &b3 ; scaled_screen_start_offset_high
&1ff9 6a       ROR A                                                # hhh84... -> Chhh84..
&1ffa 66 8f    ROR &8f ; screen_address_low
&1ffc 4a       LSR A                                                #          -> .Chhh84.
&1ffd 66 8f    ROR &8f ; screen_address_low
&1fff a6 99    LDX &99 ; pixel_colour
; plot_pixel_without_fully_calculating_screen_address
&2001 09 60    ORA #&60 ; &6000 = screen_memory                     # Wrap around end of screen memory
&2003 85 90    STA &90 ; screen_address_high
; plot_pixel_without_calculating_screen_address
&2005 a5 91    LDA &91 ; pixel_x_fraction
&2007 29 10    AND #&10
&2009 c9 10    CMP #&10
&200b a9 aa    LDA #&aa ; left pixel
&200d 90 01    BCC &2010 ; not_right_pixel
&200f 4a       LSR A                                                # Convert to right pixel
; not_right_pixel
&2010 3d 48 1e AND &1e48,X ; colour_3_pixel_values_table
&2013 51 8f    EOR (&8f),Y ; screen_address
&2015 91 8f    STA (&8f),Y ; screen_address
&2017 60       RTS

; plot_second_pixel
&2018 88       DEY                                                  # Move up a row
&2019 10 ea    BPL &2005 ; plot_pixel_without_calculating_screen_address
&201b c8       INY                                                  # Move down a row
&201c a5 8f    LDA &8f ; screen_address_low
&201e e9 f8    SBC #&f8                                             # Move up a group
&2020 85 8f    STA &8f ; screen_address_low
&2022 a5 90    LDA &90 ; screen_address_high
&2024 e9 01    SBC #&01
&2026 10 d9    BPL &2001 ; plot_pixel_without_fully_calculating_screen_address # Always branches

; plot_or_unplot_particle_using_colour_A
&2028 85 99    STA &99 ; pixel_colour
; plot_or_unplot_particle
&202a 38       SEC
&202b bd d8 28 LDA &28d8,X ; particles_x_fraction
&202e e5 c7    SBC &c7 ; screen_origin_x_fraction
&2030 85 91    STA &91 ; pixel_x_fraction
&2032 bd da 28 LDA &28da,X ; particles_x
&2035 e5 c8    SBC &c8 ; screen_origin_x
&2037 c9 08    CMP #&08                                             # Screen is eight tiles wide
&2039 b0 42    BCS &207d ; leave                                    # Leave with carry set if off left or right of screen
&203b 85 92    STA &92 ; pixel_x
&203d bd d9 28 LDA &28d9,X ; particles_y_fraction
&2040 e5 c9    SBC &c9 ; screen_origin_y_fraction
&2042 29 f8    AND #&f8
&2044 a8       TAY ; pixel_y_fraction
&2045 bd db 28 LDA &28db,X ; particles_y
&2048 e5 ca    SBC &ca ; screen_origin_y
&204a 85 94    STA &94 ; pixel_y
&204c d0 09    BNE &2057 ; is_below_top_of_screen
&204e c0 00    CPY #&00 ; pixel_y_fraction
&2050 d0 05    BNE &2057 ; is_below_top_of_screen
; is_on_top_edge_of_screen
&2052 24 a3    BIT &a3 ; particle_colour_and_flags                  # &80 set if unplotting halves of double-heigh particles
&2054 30 21    BMI &2077 ; crosses_top_or_bottom_edge_of_screen
&2056 60       RTS                                                  # Leave with carry set if offscreen
; is_below_top_of_screen
&2057 c9 04    CMP #&04                                             # Screen is four tiles high
&2059 90 08    BCC &2063 ; is_above_bottom_of_screen
&205b d0 20    BNE &207d ; leave
&205d c0 00    CPY #&00 ; pixel_y_fraction
&205f f0 0c    BEQ &206d ; is_just_below_bottom_edge_of_screen
&2061 b0 1a    BCS &207d ; leave
; is_above_bottom_of_screen
&2063 84 93    STY &93 ; pixel_y_fraction
&2065 20 db 1f JSR &1fdb ; plot_pixel
&2068 24 a3    BIT &a3 ; particle_colour_and_flags                  # &40 set if particle is double height
&206a 70 ac    BVS &2018 ; plot_second_pixel
&206c 60       RTS
; is_just_below_bottom_edge_of_screen
&206d 24 a3    BIT &a3 ; particle_colour_and_flags
&206f 50 0c    BVC &207d ; leave                                    # &40 set if particle is double height
&2071 10 0a    BPL &207d ; leave                                    # &80 set if unplotting halves of double-heigh particles
&2073 c6 94    DEC &94 ; pixel_y
&2075 a0 f8    LDY #&f8
; crosses_top_or_bottom_edge_of_screen
&2077 84 93    STY &93 ; pixel_y_fraction
&2079 20 db 1f JSR &1fdb ; plot_pixel
&207c 38       SEC                                                  # Leave with carry set to indicate not onscreen
; leave
&207d 60       RTS

; update_particles
&207e ae 58 1e LDX &1e58 ; number_of_particles
&2081 30 fa    BMI &207d ; leave
&2083 a9 38    LDA #&38 ; SEC                                       # Set loop opcodes to update all particles
&2085 8d 22 21 STA &2122 ; update_particles_consider_next_particle_opcode
&2088 a9 4c    LDA #&4c ; JMP
&208a 8d 5a 21 STA &215a ; remove_particle_consider_next_particle_opcode
&208d a5 c8    LDA &c8 ; screen_origin_x
&208f 20 bc 2c JSR &2cbc ; get_waterline_for_x
&2092 ae 59 1e LDX &1e59 ; number_of_particles_times_eight
; update_particles_loop
&2095 86 9e    STX &9e ; particle_offset
&2097 bd dd 28 LDA &28dd,X ; particles_colour_and_flags
&209a 85 a3    STA &a3 ; particle_colour_and_flags
&209c 29 07    AND #&07
&209e 20 28 20 JSR &2028 ; plot_or_unplot_particle_using_colour_A   # Unplot particle; returns carry set if offscreen
&20a1 a6 9e    LDX &9e ; particle_offset
&20a3 b0 71    BCS &2116 ; remove_particle_if_carry_set
&20a5 a5 a3    LDA &a3 ; particle_colour_and_flags
&20a7 29 10    AND #&10 ; PARTICLE_FLAG_ACCELERATE                  # If &10 set, accelerate particle
&20a9 f0 28    BEQ &20d3 ; skip_accelerating_particle
; accelerate_particle
&20ab a0 01    LDY #&01                                             # Downward acceleration by default
&20ad bd db 28 LDA &28db,X ; particles_y
&20b0 cd d1 14 CMP &14d1 ; waterline_y
&20b3 90 15    BCC &20ca ; not_in_water
&20b5 d0 08    BNE &20bf ; is_in_water
&20b7 bd d9 28 LDA &28d9,X ; particles_y_fraction
&20ba cd d0 14 CMP &14d0 ; waterline_y_fraction
&20bd 90 0b    BCC &20ca ; not_in_water
; is_in_water                                                       # If the particle is below the waterline,
&20bf a0 fd    LDY #&fd ; -3                                        # add upwards acceleration
&20c1 20 87 25 JSR &2587 ; rnd
&20c4 29 07    AND #&07
&20c6 09 06    ORA #&06                                             # and set it to either cyan or white
&20c8 85 99    STA &99 ; pixel_colour
; not_in_water
&20ca 98       TYA
&20cb 7d d7 28 ADC &28d7,X ; particles_velocity_y
&20ce 70 03    BVS &20d3 ; skip_accelerating_particle
&20d0 9d d7 28 STA &28d7,X ; particles_velocity_y
; skip_accelerating_particle
&20d3 a5 a3    LDA &a3 ; particle_colour_and_flags
&20d5 29 f7    AND #&f7 ; !PARTICLE_FLAG_CYCLE_COLOURS
&20d7 c5 a3    CMP &a3 ; particle_colour_and_flags
&20d9 b0 06    BCS &20e1 ; skip_cycling_colour                      # If &08 set, cycle colour of particle
; cycle_colour_of_particle
&20db 69 01    ADC #&01
&20dd 29 07    AND #&07
&20df 85 99    STA &99 ; pixel_colour
; skip_cycling_colour
&20e1 de dc 28 DEC &28dc,X ; particles_ttl
&20e4 f0 54    BEQ &213a ; remove_particle
; update_particle
&20e6 a0 01    LDY #&01                                             # Loop through Y = 1 to apply y velocity
; apply_particle_velocities_loop                                    #              Y = 0 to apply x velocity
&20e8 18       CLC
&20e9 bd d6 28 LDA &28d6,X ; particles_velocity_x
&20ec 48       PHA ; velocity
&20ed 7d d8 28 ADC &28d8,X ; particles_x_fraction
&20f0 9d d8 28 STA &28d8,X ; particles_x_fraction
&20f3 90 03    BCC &20f8 ; skip_overflow
&20f5 fe da 28 INC &28da,X ; particles_x
; skip_overflow
&20f8 68       PLA ; velocity
&20f9 10 03    BPL &20fe ; skip_underflow
&20fb de da 28 DEC &28da,X ; particles_x
; skip_underflow
&20fe e8       INX
&20ff 88       DEY
&2100 f0 e6    BEQ &20e8 ; apply_particle_velocities_loop
&2102 a6 9e    LDX &9e ; particle_offset
&2104 a5 a3    LDA &a3 ; particle_colour_and_flags
&2106 29 f8    AND #&f8
&2108 05 99    ORA &99 ; pixel_colour
&210a 9d dd 28 STA &28dd,X ; particles_colour_and_flags
&210d 29 7f    AND #&7f ; !PARTICLE_FLAG_PLOT_HALVES                # Clear &80 to suppress plotting halves of double-height
;                                                                   # particles that cross top or bottom edge of screen
&210f 85 a3    STA &a3 ; particle_colour_and_flags
&2111 20 2a 20 JSR &202a ; plot_or_unplot_particle                  # Plot particle; returns carry set if offscreen
&2114 a6 9e    LDX &9e ; particle_offset                            #                        A = pixel value after plotting
; remove_particle_if_carry_set
&2116 b0 22    BCS &213a ; remove_particle
&2118 29 c0    AND #&c0                                             # Was the particle plotted on foreground?
&211a f0 06    BEQ &2122 ; consider_next_particle
&211c a5 a3    LDA &a3 ; particle_colour_and_flags
&211e 29 20    AND #&20 ; PARTICLE_FLAG_FOREGROUND                  # If &20 not set, remove on hitting foreground
&2120 f0 13    BEQ &2135 ; remove_particle_after_unplotting
; consider_next_particle
; update_particles_consider_next_particle_opcode
&2122 38       SEC if called from update_particles (see &2085)      # Loop over all particles if updating all particles
#     or       RTS if called from add_particles (see &212e)         # Leave if updating a single particle
&2123 8a       TXA
&2124 e9 08    SBC #&08                                             # Move to previous particle
&2126 aa       TAX
&2127 90 03    BCC &212c ; leave_after_restoring_opcodes
&2129 4c 95 20 JMP &2095 ; update_particles_loop
; leave_after_restoring_opcodes
&212c a9 60    LDA #&60 ; RTS                                       # Set loop opcodes to leave after updating one particle
&212e 8d 22 21 STA &2122 ; update_particles_consider_next_particle_opcode
&2131 8d 5a 21 STA &215a ; remove_particle_consider_next_particle_opcode
&2134 60       RTS

; remove_particle_after_unplotting
&2135 20 2a 20 JSR &202a ; plot_or_unplot_particle
&2138 a6 9e    LDX &9e ; particle_offset
; remove_particle
&213a ac 59 1e LDY &1e59 ; number_of_particles_times_eight
; compact_particle_list_loop
&213d b9 d6 28 LDA &28d6,Y ; particles_velocity_x                   # Shuffle all particles up a slot
&2140 9d d6 28 STA &28d6,X ; particles_velocity_x                   # i.e. remove gap for removed particle
&2143 c8       INY
&2144 e8       INX
&2145 8a       TXA
&2146 29 07    AND #&07
&2148 d0 f3    BNE &213d ; compact_particle_list_loop
&214a a6 9e    LDX &9e ; particle_offset
&214c 38       SEC
&214d ad 59 1e LDA &1e59 ; number_of_particles_times_eight
&2150 e9 08    SBC #&08                                             # Move to previous particle
&2152 8d 59 1e STA &1e59 ; number_of_particles_times_eight
&2155 ce 58 1e DEC &1e58 ; number_of_particles
&2158 30 d2    BMI &212c ; leave_after_restoring_opcodes            # Leave if no particles remaining
; remove_particle_consider_next_particle_opcode
&215a 60       RTS if called from add_particles (see &212e)
#     or       JMP &2122 ; consider_next_particle if called from update_particles (see &208a)
&215b 22 21

; find_free_particle_slot
&215d ae 58 1e LDX &1e58 ; number_of_particles
&2160 e0 1f    CPX #&1f
&2162 f0 10    BEQ &2174 ; replace_random_particle                  # Are all the particle slots used?
&2164 e8       INX                                                  # If not, use the next one
&2165 8e 58 1e STX &1e58 ; number_of_particles
&2168 8a       TXA
&2169 0a       ASL A
&216a 0a       ASL A
&216b 0a       ASL A
&216c 8d 59 1e STA &1e59 ; number_of_particles_times_eight
&216f 85 9e    STA &9e ; new_particle_offset
&2171 aa       TAX
&2172 90 17    BCC &218b ; leave                                    # Always branches
; replace_random_particle
&2174 a5 da    LDA &da ; rnd_state + 1
&2176 29 f8    AND #&f8                                             # Pick a random particle to replace
&2178 aa       TAX
&2179 86 9e    STX &9e ; new_particle_offset
&217b bd dd 28 LDA &28dd,X ; particles_colour_and_flags
&217e 09 80    ORA #&80 ; PARTICLE_FLAG_PLOT_HALVES                 # Set &80 to unplot halves of double-height particles
;                                                                   # particles that cross top or bottom edge of screen
&2180 85 a3    STA &a3 ; particle_colour_and_flags
&2182 29 07    AND #&07
&2184 20 28 20 JSR &2028 ; plot_particle_colour_A                   # Unplot particle being replaced
&2187 a6 9e    LDX &9e ; new_particle_offset
&2189 a4 a1    LDY &a1 ; new_particles_type
; leave
&218b 60       RTS

; add_particle                                                      # Called with Y = particle type
&218c a9 01    LDA #&01
; add_particles                                                     # Called with Y = particle type, A = number of particles
&218e 84 a1    STY &a1 ; new_particles_type
&2190 85 9f    STA &9f ; count
&2192 b9 0c 02 LDA &020c,Y ; particle_types_flags_table             # For all the new particles,
&2195 85 a0    STA &a0 ; new_particles_flags
&2197 29 20    AND #&20                                             # If &20 set, set position from object position
&2199 f0 0a    BEQ &21a5 ; skip_using_object_position
; copy_object_position
&219b a2 06    LDX #&06
; copy_object_position_loop                                         # Loop through X = 6 for &8d, new_particles_y
&219d b5 4f    LDA &4f,X ; this_object_x_fraction                   #              X = 4 for &8b, new_particles_x
&219f 95 87    STA &87,X ; new_particles_x_fraction                 #              X = 2 for &89, new_particles_y_fraction
&21a1 ca       DEX                                                  #              X = 0 for &87, new_particles_x_fraction
&21a2 ca       DEX
&21a3 10 f8    BPL &219d ; copy_object_position_loop
; skip_using_object_position
&21a5 a5 c8    LDA &c8 ; screen_origin_x
&21a7 18       CLC
&21a8 e5 8b    SBC &8b ; new_particles_x
&21aa 38       SEC
&21ab e9 01    SBC #&01
&21ad c9 f6    CMP #&f6 ; -10                                       # Is -10 <= -(relative particle x + 1) < 0
;                                                                   #    i.e. -1 <= relative particle x < (8 + 1) ?
&21af 90 da    BCC &218b ; leave                                    # Leave if off left or right of screen
;                                                                   # Otherwise carry is set for next subtraction
&21b1 a5 8d    LDA &8d ; new_particles_y
&21b3 e5 ca    SBC &ca ; screen_origin_y
&21b5 18       CLC
&21b6 69 01    ADC #&01
&21b8 c9 06    CMP #&06                                             # Is 0 <= (relative particle y + 1) < 6
#                                                                   #    i.e. -1 <= relative particle y < (4 + 1) ?
&21ba b0 cf    BCS &218b ; leave                                    # Leave if off top or bottom of screen
&21bc 24 a0    BIT &a0 ; new_particles_flags
&21be 10 17    BPL &21d7 ; skip_setting_base_velocities             # If &80 set, set base velocities from object velocities
&21c0 a2 40    LDX #&40 ; this_object_acceleration_x
&21c2 70 02    BVS &21c6 ; set_new_particles_velocities             # If &c0 set, set base velocities from object acceleration
&21c4 a2 43    LDX #&43 ; this_object_velocity_x
; set_new_particles_base_velocities
&21c6 b5 00    LDA &00,X ; this_object_acceleration_x - &40 or this_object_velocity_x - &43
&21c8 85 b4    STA &b4 ; vector_x
&21ca b5 02    LDA &02,X ; this_object_acceleration_y - &40 or this_object_velocity_y - &43
&21cc 85 b6    STA &b6 ; vector_y
&21ce 20 d4 22 JSR &22d4 ; calculate_angle_from_vector
&21d1 a4 a1    LDY &a1 ; new_particles_type
&21d3 49 80    EOR #&80                                             # Particles move in opposite direction
&21d5 85 b5    STA &b5 ; angle
; skip_setting_base_velocities
&21d7 20 87 25 JSR &2587 ; rnd                                      # Determine magnitude of velocity
&21da 39 08 02 AND &0208,Y ; particle_types_speed_randomness_table
&21dd 18       CLC
&21de 79 09 02 ADC &0209,Y ; particle_types_speed_table
&21e1 20 57 23 JSR &2357 ; calculate_vector_from_magnitude_and_angle
&21e4 06 a0    ASL &a0 ; new_particles_flags                        # 84218421 -> 4218421.
&21e6 06 a0    ASL &a0 ; new_particles_flags                        #          -> 218421..
&21e8 06 a0    ASL &a0 ; new_particles_flags                        #          -> 18421...
&21ea a2 02    LDX #&02
; set_new_particles_base_position_loop                              # Loop through X = 2 for y, X = 0 for x
&21ec a9 00    LDA #&00                                             # Align particles with left or top of object by default
&21ee 06 a0    ASL &a0 ; new_particles_flags                        # If &10 or &04 set, follow object flip for position
&21f0 90 06    BCC &21f8 ; not_flipped
&21f2 b4 37    LDY &37,X ; this_object_x_flip                       # &80 set if flipped
&21f4 10 02    BPL &21f8 ; not_flipped
&21f6 b5 3a    LDA &3a,X ; this_object_width
; skip_following_object_flip
&21f8 06 a0    ASL &a0 ; new_particles_flags                        # If &08 or &02 set, use centre, not edge of object
&21fa 90 03    BCC &21ff ; skip_using_centre
&21fc b5 3a    LDA &3a,X ; this_object_width
&21fe 4a       LSR A
; skip_using_centre
&21ff 75 87    ADC &87,X ; new_particles_x_fraction
&2201 95 87    STA &87,X ; new_particles_x_fraction
&2203 90 02    BCC &2207 ; skip_overflow
&2205 f6 8b    INC &8b,X ; new_particles_x
; skip_overflow
&2207 ca       DEX
&2208 ca       DEX
&2209 f0 e1    BEQ &21ec ; set_new_particles_base_position_loop
; add_particles_loop                                                # For each new particle,
&220b a4 a1    LDY &a1 ; new_particles_type
&220d a5 da    LDA &da ; rnd_state + 1                              # Determine colour and properties
&220f 39 0b 02 AND &020b,Y ; particle_types_colour_and_flags_randomness_table
&2212 59 0a 02 EOR &020a,Y ; particle_types_colour_and_flags_table
&2215 48       PHA ; particle_colour_and_flags
&2216 20 5d 21 JSR &215d ; find_free_particle_slot                  # Returns X = offset into particles_data
&2219 68       PLA ; particle_colour_and_flags
&221a 85 a3    STA &a3 ; particle_colour_and_flags
&221c 29 07    AND #&07
&221e 85 99    STA &99 ; pixel_colour
&2220 20 87 25 JSR &2587 ; rnd                                      # Determine time to live
&2223 39 06 02 AND &0206,Y ; particle_types_ttl_randomness_table
&2226 79 07 02 ADC &0207,Y ; particle_types_ttl_table
&2229 9d dc 28 STA &28dc,X ; particles_ttl
&222c 86 9c    STX &9c ; particle_offset
&222e a2 fe    LDX #&fe ; -2
; set_new_particle_position_and_velocity_loop                       # Loop through X = -2 for x
&2230 86 a2    STX &a2 ; loop_offset                                #              X =  0 for y
&2232 20 87 25 JSR &2587 ; rnd
&2235 4a       LSR A                                                # Set sign of velocity at random
&2236 39 0f 02 AND &020f,Y ; particle_types_velocity_x_randomness_table
&2239 90 02    BCC &223d ; skip_inversion
&223b 49 ff    EOR #&ff
; skip_inversion
&223d 75 b6    ADC &b6,X ; vector_x + 2
&223f 20 7f 32 JSR &327f ; prevent_overflow
&2242 48       PHA ; velocity
&2243 a5 da    LDA &da ; rnd_state + 1                              # Add randomness to position
&2245 39 0d 02 AND &020d,Y ; particle_types_x_randomness_table
&2248 75 89    ADC &89,X ; new_particle_x_fraction + 2
&224a 48       PHA ; fraction 
&224b b5 8d    LDA &8d,X ; new_particles_x + 2
&224d 69 00    ADC #&00
&224f a6 9c    LDX &9c ; particle_offset
&2251 9d da 28 STA &28da,X ; particles_x
&2254 68       PLA ; fraction
&2255 9d d8 28 STA &28d8,X ; particles_x_fraction
&2258 68       PLA ; velocity
&2259 9d d6 28 STA &28d6,X ; particles_velocity_x
&225c e6 9c    INC &9c ; particle_offset                            # Use particles_y_* on second pass
&225e a6 a2    LDX &a2 ; loop_offset
&2260 c8       INY                                                  # Use particle_types_y_*_table on second pass
&2261 e8       INX                                                  # Use vector_y and new_particle_y_* on second pass
&2262 e8       INX
&2263 f0 cb    BEQ &2230 ; set_new_particle_position_and_velocity_loop
&2265 a6 9e    LDX &9e ; new_particles_offset
&2267 20 e6 20 JSR &20e6 ; update_particle                          # Plot new particle
&226a 24 a0    BIT &a0 ; new_particles_flags                        # If &01 set, add object velocities to particle velocities
&226c 10 15    BPL &2283 ; skip_adding_object_velocities
&226e a0 02    LDY #&02
&2270 e8       INX
&2271 c9 ca    CMP #&ca ; (nop)
; add_object_velocities_loop                                        # Loop through Y = 2 for y velocities
#2272          DEX                                                  #              Y = 0 for x velocities
&2273 bd d6 28 LDA &28d6,X ; particles_velocity_x
&2276 79 43 00 ADC &0043,Y ; this_object_velocity_x
&2279 20 7f 32 JSR &327f ; prevent_overflow
&227c 9d d6 28 STA &28d6,X ; particles_velocity_x
&227f 88       DEY
&2280 88       DEY
&2281 f0 ef    BEQ &2272 ; add_object_velocities_loop
; skip_setting_object_velocities
&2283 c6 9f    DEC &9f ; count
&2285 d0 84    BNE &220b ; add_particles_loop
&2287 60       RTS

; get_this_object_centre
&2288 86 9d    STX &9d ; tmp_x
&228a a2 02    LDX #&02
; get_this_object_centre_loop                                       # Loop through X = 2 for y, X = 0 for x
&228c b5 3a    LDA &3a,X ; this_object_width
&228e 4a       LSR A
&228f 75 4f    ADC &4f,X ; this_object_x_fraction
&2291 95 87    STA &87,X ; this_object_centre_x_fraction
&2293 b5 53    LDA &53,X ; this_object_x
&2295 69 00    ADC #&00
&2297 95 8b    STA &8b,X ; this_object_centre_x
&2299 ca       DEX
&229a ca       DEX
&229b f0 ef    BEQ &228c ; get_this_object_centre_loop
&229d a6 9d    LDX &9d ; tmp_x
&229f 60       RTS

# Angles
# ======
# Angles are stored such that &100 = 360 degrees, starting from straight right
# at &00 and increasing clockwise. &40 = down, &80 = left, &c0 = up.
#
# calculate_vector_from_magnitude_and_angle takes an angle and a magnitude
# and converts to the x and y components of a vector.
#
# calculate_angle_from_vector takes a vector and converts to an angle and
# magnitude.
#
# Both are very approximate, but they are almost the inverse of each other:
#
#     calculate_vector_from_magnitude_and_angle     calculate_angle_from_vector
#     a:&00, m:&20 -> x:&20, y:&00                  x:&20, y:&00 -> a:&00, m:&20     0    degrees - accurate
#     a:&08, m:&20 -> x:&20, y:&08                  x:&20, y:&08 -> a:&08, m:&20    11.25 degrees - should be x:&1e, y: &06
#     a:&10, m:&20 -> x:&20, y:&10                  x:&20, y:&10 -> a:&10, m:&20    22.50 degrees - should be x:&1d, y: &0c
#     a:&18, m:&20 -> x:&20, y:&18                  x:&20, y:&18 -> a:&18, m:&20    33.75 degrees - should be x:&1a, y: &11
#     a:&20, m:&20 -> x:&20, y:&20                  x:&20, y:&20 -> a:&1f, m:&20    45    degrees - should be x:&17, y: &17
#     a:&28, m:&20 -> x:&18, y:&20                  x:&18, y:&20 -> a:&27, m:&20    56.25 degrees - should be x:&11, y: &1a
#     a:&30, m:&20 -> x:&10, y:&20                  x:&10, y:&20 -> a:&2f, m:&20    67.50 degrees - should be x:&0c, y: &1d
#     a:&38, m:&20 -> x:&08, y:&20                  x:&08, y:&20 -> a:&37, m:&20    78.75 degrees - should be x:&0c, y: &1e
#     a:&40, m:&20 -> x:&00, y:&20                  x:&00, y:&20 -> a:&3f, m:&20    90    degrees - accurate
#     a:&48, m:&20 -> x:&f8, y:&20                  x:&f8, y:&20 -> a:&48, m:&20
#     a:&50, m:&20 -> x:&f0, y:&20                  x:&f0, y:&20 -> a:&50, m:&20
#     a:&58, m:&20 -> x:&e8, y:&20                  x:&e8, y:&20 -> a:&58, m:&20
#     a:&60, m:&20 -> x:&e0, y:&20                  x:&e0, y:&20 -> a:&66, m:&20
#     a:&68, m:&20 -> x:&e0, y:&18                  x:&e0, y:&18 -> a:&67, m:&20
#     a:&70, m:&20 -> x:&e0, y:&10                  x:&e0, y:&10 -> a:&6f, m:&20
#     a:&78, m:&20 -> x:&e0, y:&08                  x:&e0, y:&08 -> a:&77, m:&20
#     a:&80, m:&20 -> x:&e0, y:&00                  x:&e0, y:&00 -> a:&7f, m:&20
#     a:&88, m:&20 -> x:&e0, y:&f8                  x:&e0, y:&f8 -> a:&88, m:&20
#     a:&90, m:&20 -> x:&e0, y:&f0                  x:&e0, y:&f0 -> a:&90, m:&20
#     a:&98, m:&20 -> x:&e0, y:&e8                  x:&e0, y:&e8 -> a:&98, m:&20
#     a:&a0, m:&20 -> x:&e0, y:&e0                  x:&e0, y:&e0 -> a:&9f, m:&20
#     a:&a8, m:&20 -> x:&e8, y:&e0                  x:&e8, y:&e0 -> a:&a7, m:&20
#     a:&b0, m:&20 -> x:&f0, y:&e0                  x:&f0, y:&e0 -> a:&af, m:&20
#     a:&b8, m:&20 -> x:&f8, y:&e0                  x:&f8, y:&e0 -> a:&b7, m:&20
#     a:&c0, m:&20 -> x:&00, y:&e0                  x:&00, y:&e0 -> a:&c0, m:&20
#     a:&c8, m:&20 -> x:&08, y:&e0                  x:&08, y:&e0 -> a:&c8, m:&20
#     a:&d0, m:&20 -> x:&10, y:&e0                  x:&10, y:&e0 -> a:&d0, m:&20
#     a:&d8, m:&20 -> x:&18, y:&e0                  x:&18, y:&e0 -> a:&d8, m:&20
#     a:&e0, m:&20 -> x:&20, y:&e0                  x:&20, y:&e0 -> a:&e0, m:&20
#     a:&e8, m:&20 -> x:&20, y:&e8                  x:&20, y:&e8 -> a:&e7, m:&20
#     a:&f0, m:&20 -> x:&20, y:&f0                  x:&20, y:&f0 -> a:&ef, m:&20
#     a:&f8, m:&20 -> x:&20, y:&f8                  x:&20, y:&f8 -> a:&f7, m:&20

; calculate_angle_of_object_X_to_this_object
&22a0 20 88 22 JSR &2288 ; get_this_object_centre
&22a3 bc 70 08 LDY &0870,X ; objects_sprite
&22a6 b9 0c 5e LDA &5e0c,Y ; sprites_width_and_horizontal_flip_table
&22a9 4a       LSR A                                                # Width in fractions, divided by two
&22aa 7d 80 08 ADC &0880,X ; objects_x_fraction
&22ad 85 88    STA &88 ; other_object_centre_x_fraction
&22af bd 91 08 LDA &0891,X ; objects_x
&22b2 69 00    ADC #&00
&22b4 85 8c    STA &8c ; other_object_centre_x
&22b6 b9 89 5e LDA &5e89,Y ; sprites_height_and_vertical_flip_table
&22b9 4a       LSR A                                                # Height in fractions, divided by two
&22ba 7d a3 08 ADC &08a3,X ; objects_y_fraction
&22bd 85 8a    STA &8a ; other_object_centre_y_fraction
&22bf bd b4 08 LDA &08b4,X ; objects_y
&22c2 69 00    ADC #&00
&22c4 85 8e    STA &8e ; other_object_centre_y
&22c6 20 fe 22 JSR &22fe ; calculate_normalised_relative_position_of_centres
&22c9 4c d7 22 JMP &22d7 ; calculate_angle_from_A_and_absolute_vector_y

; calculate_angle_from_this_object_velocities
&22cc a5 43    LDA &43 ; this_object_velocity_x
&22ce 85 b4    STA &b4 ; vector_x
&22d0 a5 45    LDA &45 ; this_object_velocity_y
; calculate_angle_from_vector_x_and_A
&22d2 85 b6    STA &b6 ; vector_y
; calculate_angle_from_vector
&22d4 20 3d 23 JSR &233d ; get_absolute_vector_components           # Returns A = absolute_vector_x
; calculate_angle_from_A_and_absolute_vector_y
&22d7 c5 b7    CMP &b7 ; absolute_vector_y
&22d9 90 05    BCC &22e0 ; skip_swap                                # Branch if absolute_vector_x < absolute_vector_y
&22db a8       TAY
&22dc a5 b7    LDA &b7 ; absolute_vector_y
&22de 84 b7    STY &b7 ; magnitude                                  # Actually the largest component, but used as an
;                                                                   # approximation to the magnitude of the vector elsewhere
; skip_swap                                                         # A = smallest component, Y or &b7 = largest component
&22e0 26 99    ROL &99 ; vector_signs                               # &01 set if abs x >= abs y, clear if abs x < abs y
&22e2 a0 08    LDY #&08
&22e4 84 b5    STY &b5 ; angle                                      # Considering the angle in a 45 degree half quadrant,
; division_loop                                                     # Divide smallest component by largest component
&22e6 0a       ASL A
&22e7 c5 b7    CMP &b7 ; magnitude
&22e9 90 02    BCC &22ed ; skip_subtraction
&22eb e5 b7    SBC &b7 ; magnitude
; skip_subtraction
&22ed 26 b5    ROL &b5 ; angle
&22ef 90 f5    BCC &22e6 ; division_loop
&22f1 a5 99    LDA &99 ; vector_signs                               # &02 set if x positive, &04 set if y positive (&2354)
&22f3 29 07    AND #&07
&22f5 a8       TAY
&22f6 a5 b5    LDA &b5 ; angle
&22f8 59 bf 14 EOR &14bf,Y ; angle_calculation_half_quadrants_table # Apply the half quadrant to the angle
&22fb 85 b5    STA &b5 ; angle
&22fd 60       RTS

; calculate_normalised_relative_position_of_centres
&22fe a0 04    LDY #&04
; calculate_relative_position_of_centres_loop                       # Loop through Y = 4 for y, Y = 2 for x
&2300 85 b7    STA &b7 ; absolute_relative_x                        # Only meaningful on second pass; is number of tiles
&2302 b9 86 00 LDA &0086,Y ; other_object_centre_x_fraction - 2
&2305 f9 85 00 SBC &0085,Y ; this_object_centre_x_fraction - 2      # Calculate relative position of other object to this
&2308 99 79 00 STA &0079,Y ; relative_x_fraction - 2
&230b b9 8a 00 LDA &008a,Y ; other_object_centre_x_fraction - 2
&230e f9 89 00 SBC &0089,Y ; this_object_centre_x - 2
&2311 99 7a 00 STA &007a,Y ; relative_x - 2
&2314 38       SEC
&2315 10 03    BPL &231a ; skip_inversion
&2317 20 56 32 JSR &3256 ; invert_if_negative
; skip_inversion
&231a 26 99    ROL &99 ; vector_signs                               # Set bit if relative position is negative
&231c 88       DEY
&231d 88       DEY                                                  # Finish loop with Y = 0
&231e d0 e0    BNE &2300 ; calculate_relative_position_of_centres_loop
&2320 05 b7    ORA &b7 ; absolute_relative_x                        # Approximately the largest of x and y distances, in tiles
&2322 0a       ASL A
; division_loop                                                     # Divide relative x and y by 2 * largest distance in tiles
&2323 46 7c    LSR &7c ; relative_x
&2325 66 7b    ROR &7b ; relative_x_fraction
&2327 46 7e    LSR &7e ; relative_y
&2329 66 7d    ROR &7d ; relative_y_fraction
&232b c8       INY
&232c 4a       LSR A
&232d d0 f4    BNE &2323 ; division_loop
&232f 84 b8    STY &b8 ; relative_tiles_log                         # Y is the smallest value of n for which d < 2 ** (n - 1)
;                                                                   # where d is the distance between the objects in tiles
;                                                                   # Y = 1 for d = 0, 2 for d = 1, 3 for d = 2 or 3, etc
&2331 a5 7d    LDA &7d ; relative_y_fraction
&2333 20 56 32 JSR &3256 ; invert_if_negative
&2336 85 b7    STA &b7 ; absolute_vector_y
&2338 a5 7b    LDA &7b ; relative_x_fraction
&233a 4c 56 32 JMP &3256 ; invert_if_negative                       # Leave with A = absolute_vector_x

; get_absolute_vector_components
&233d a0 02    LDY #&02                                             # Y = 2 for y
&233f 20 46 23 JSR &2346 ; get_absolute_vector_component
&2342 85 b7    STA &b7 ; absolute_vector_y
&2344 88       DEY                                                  # Y = 0 for x
&2345 88       DEY
; get_absolute_vector_component
&2346 a9 7f    LDA #&7f
&2348 d9 b4 00 CMP &00b4,Y ; vector_x                               # Set carry if vector < &7f
&234b b9 b4 00 LDA &00b4,Y ; vector_x
&234e b0 04    BCS &2354 ; skip_inversion
&2350 49 ff    EOR #&ff
&2352 69 01    ADC #&01                                             # Carry is clear
;                                                                   # Inversion turns x -> -x for &01 <= x <= &7f
;                                                                   #                         and &81 <= x <= &ff
;                                                                   #           and keeps &00 and &80 unchanged
; skip_inversion
&2354 26 99    ROL &99 ; vector_signs                               # Set bit if velocity was positive
&2356 60       RTS

; calculate_vector_from_magnitude_and_angle                         # Called with A = magnitude, angle in &b5 ; angle
&2357 85 b4    STA &b4 ; vector_x
&2359 a5 b5    LDA &b5 ; angle
&235b 85 9d    STA &9d ; angle_quadrant
&235d a0 05    LDY #&05                                             # Multiply magnitude by lowest five bits (&1f) of angle
&235f a9 00    LDA #&00                                             # i.e. angle in half quadrant (0, 45, 90 ... degrees)
; multiply_magnitude_loop
&2361 46 9d    LSR &9d ; angle_quadrant
&2363 90 03    BCC &2368 ; skip_addition
&2365 18       CLC
&2366 65 b4    ADC &b4 ; vector_x
; skip_addition
&2368 6a       ROR A
&2369 88       DEY
&236a d0 f5    BNE &2361 ; multiply_magnitude_loop                  # Y = 0 at end of loop
&236c 46 9d    LSR &9d ; angle_quadrant                             # &20 of angle set if second half of 90 degree quadrant
&236e 90 0a    BCC &237a ; skip_second_half_of_quadrant_swap
&2370 a4 b4    LDY &b4 ; vector_x                                   # If so, use x = m, y = m - x
&2372 85 b4    STA &b4 ; vector_x
&2374 98       TYA
&2375 e5 b4    SBC &b4; vector_x
&2377 85 b4    STA &b4; vector_x
&2379 98       TYA
; skip_second_half_of_quadrant_swap
&237a 46 9d    LSR &9d ; angle_quadrant                             # &40 of angle set if x and y are opposite signs
&237c 90 08    BCC &2386 ; skip_same_sign_swap
&237e 49 ff    EOR #&ff                                             # If so, swap x and y, then invert x; x = -y, y = x
&2380 a8       TAY
&2381 c8       INY
&2382 a5 b4    LDA &b4 ; vector_x
&2384 84 b4    STY &b4 ; vector_x
; skip_same_sign_swap
&2386 46 9d    LSR &9d ; angle_quadrant                             # &80 of angle set if y is negative
&2388 90 0b    BCC &2395 ; skip_y_negative_inversion
&238a 49 ff    EOR #&ff                                             # If so, invert x and y; x = -x, y = -y
&238c a8       TAY
&238d c8       INY
&238e a9 00    LDA #&00
&2390 e5 b4    SBC &b4 ; vector_x
&2392 85 b4    STA &b4 ; vector_x
&2394 98       TYA
; skip_y_negative_inversion
&2395 85 b6    STA &b6 ; vector_y
&2397 60       RTS

; get_tile_and_set_sprite_variables
&2398 20 15 17 JSR &1715 ; get_tile_and_check_for_tertiary_objects  # Returns A = tile type
; set_sprite_variables_for_tile
&239b a8       TAY
; calculate_palette_for_tile
&239c b9 2b 05 LDA &052b,Y ; tiles_palette_table                    # Get entry in tile palette table for tile type
&239f d0 0d    BNE &23ae ; not_stone
; is_stone                                                          # &00 to use stone colour scheme
&23a1 a5 97    LDA &97 ; tile_y
&23a3 38       SEC
&23a4 e9 54    SBC #&54                                             # Stone doesn't appear above y = &50
&23a6 4a       LSR A                                                # 8421.... -> ....8421
&23a7 4a       LSR A
&23a8 4a       LSR A
&23a9 4a       LSR A
&23aa aa       TAX
&23ab bd 85 11 LDA &1185,X ; strata_palette_table                   # Colour of stones changes every 16 tile rows
; not_stone
&23ae c9 03    CMP #&03                                             # &01 or &02 to use spaceship colour schemes
&23b0 b0 09    BCS &23bb ; not_spacecraft_part
; is_spacecraft_part
&23b2 69 b1    ADC #&b1 ; &01 -> &b2 (rmy), &02 -> &b3 (rcy)        # Colour of spaceships and base in top half of world
&23b4 24 97    BIT &97 ; tile_y
&23b6 10 54    BPL &240c ; set_tile_palette                         # is different from
&23b8 0a       ASL A
&23b9 69 90    ADC #&90 ; &01 -> &f5 (gyw), &02 -> &f7 (gmw)        # colour of Triax's machinery in bottom half of world
; not_spacecraft_part
&23bb c9 03    CMP #&03                                             # &03 to use bush colour scheme
&23bd d0 11    BNE &23d0 ; not_bush
; is_bush
&23bf a5 09    LDA &09 ; tile_flip
&23c1 2a       ROL A
&23c2 2a       ROL A
&23c3 2a       ROL A
&23c4 e5 97    SBC &97 ; tile_y
&23c6 6a       ROR A
&23c7 18       CLC
&23c8 65 95    ADC &95 ; tile_x
&23ca 29 03    AND #&03
&23cc aa       TAX
&23cd bd 95 11 LDA &1195,X ; bushes_palette_table                   # Colour of bushes depends on position and flip
; not_bush
&23d0 c9 04    CMP #&04                                             # &04 to use earth colour scheme
&23d2 d0 0c    BNE &23e0 ; not_earth
; is_earth
&23d4 a5 97    LDA &97 ; tile_y                                     # Earth doesn't appear above Y = &4e
&23d6 2a       ROL A                                                # 842..... -> .....842
&23d7 2a       ROL A
&23d8 2a       ROL A
&23d9 2a       ROL A
&23da 29 07    AND #&07
&23dc aa       TAX
&23dd bd 8c 11 LDA &118c,X ; strata_palette_table + 7               # Colour of earths changes every 32 tile rows
; not_four
&23e0 c9 05    CMP #&05                                             # &05 to use leaf colour scheme for TILE_POSSIBLE_LEAF
&23e2 d0 1c    BNE &2400 ; not_leaf
; is_leaf
&23e4 a5 97    LDA &97 ; tile_y
&23e6 6a       ROR A                                                # 84218421 -> 1 .8421842
&23e7 6a       ROR A                                                #          -> 2 1.842184
&23e8 45 97    EOR &97 ; tile_y                                     #                        &04 gets EORed with &01
&23ea 6a       ROR A                                                #          -> 4 21.84218
&23eb 90 02    BCC &23ef ; skip_removing_leaf
&23ed a0 19    LDY #&19 ; TILE_SPACE                                # Remove leaf if (y & 4) and (y & 1) not same
; skip_removing_leaf
&23ef 6a       ROR A                                                #          -> 8 421.8421
&23f0 e5 97    SBC &97 ; tile_y
&23f2 29 40    AND #&40 ; TILE_FLIP_VERTICAL                        # Flip some leaves vertically
&23f4 45 09    EOR &09 ; tile_flip
&23f6 24 09    BIT &09 ; tile_flip                                  # &40 set if tile was previously flipped vertically,
&23f8 85 09    STA &09 ; tile_flip                                  # which depends on the tertiary object for the shaft
&23fa a9 b1    LDA #&b1 ; rgy                                       # If not, leaves are green
&23fc 50 0e    BVC &240c ; set_tile_palette
&23fe 69 0a    ADC #&0a ; &bb (cyy) or &bc (ywy)                    # Otherwise, leaves are yellow or white
; not_leaf
&2400 c9 06    CMP #&06                                             # &06 to use mushroom colour scheme
&2402 d0 08    BNE &240c ; set_tile_palette                         # Otherwise use table entry as palette
; is_mushroom
&2404 a9 9c    LDA #&9c ; ywr                                       # Unflipped mushrooms (on floor) are red
&2406 24 09    BIT &09 ; tile_flip                                  # &40 set if flipped vertically
&2408 50 02    BVC &240c ; set_tile_palette                         # Colour of mushrooms depends on vertical flip
&240a a9 cf    LDA #&cf ; cwb                                       # Flipped mushrooms (on ceiling) are blue
; set_tile_palette
&240c 85 73    STA &73 ; this_object_palette
&240e 85 74    STA &74 ; this_object_previous_palette
&2410 a5 09    LDA &09 ; tile_flip
&2412 85 71    STA &71 ; this_object_flip
&2414 85 72    STA &72 ; this_object_previous_flip
&2416 b9 ab 04 LDA &04ab,Y ; tiles_sprite_and_y_flip_table          # .4218421 sprite
&2419 29 7f    AND #&7f
&241b aa       TAX
&241c 85 75    STA &75 ; this_object_sprite
&241e 85 76    STA &76 ; this_object_previous_sprite
&2420 b9 eb 04 LDA &04eb,Y ; tiles_y_offset_and_pattern_table       # 8421.... start of tile from top of tile, in &10 fractions
&2423 29 f0    AND #&f0
&2425 24 09    BIT &09 ; tile_flip                                  # &40 set if flipped vertically
&2427 50 07    BVC &2430 ; not_flipped_vertically
&2429 7d 89 5e ADC &5e89,X ; sprites_height_and_vertical_flip_table
&242c 09 07    ORA #&07                                             # Round down to pixel vertically
&242e 49 ff    EOR #&ff
; not_flipped_vertically
&2430 85 51    STA &51 ; this_object_y_fraction
&2432 85 52    STA &52 ; this_object_previous_y_fraction
&2434 a9 00    LDA #&00
&2436 24 09    BIT &09 ; tile_flip                                  # &80 set if flipped horizontally
&2438 10 05    BPL &243f ; not_flipped_horizontally
&243a a9 f2    LDA #&f2                                             # &f0 + 1 for carry + 1 for flip flag
&243c fd 0c 5e SBC &5e0c,X ; sprites_width_and_horizontal_flip_table
; not_flipped_horizontally
&243f 85 4f    STA &4f ; this_object_x_fraction
&2441 85 50    STA &50 ; this_object_previous_x_fraction
&2443 a5 95    LDA &95 ; tile_x
&2445 85 53    STA &53 ; this_object_x
&2447 85 54    STA &54 ; this_object_previous_x
&2449 a5 97    LDA &97 ; tile_y
&244b 85 55    STA &55 ; this_object_y
&244d 85 56    STA &56 ; this_object_previous_y
&244f 60       RTS

; set_obstruction_data_variables_for_bottom_tile
&2450 a9 04    LDA #&04 ; bottom_tile_obstruction_* - top_tile_obstruction_*
&2452 2c a9 00 BIT &00a9 ; (nop)
; set_obstruction_data_variables_for_top_tile
#2453          LDA #&00
&2455 48       PHA ; variable offset
&2456 8e 90 24 STX &2490 ; tmp_x
&2459 20 15 17 JSR &1715 ; get_tile_and_check_for_tertiary_objects  # Returns A = tile type
&245c a8       TAY
&245d 68       PLA ; variable offset
&245e aa       TAX
&245f b9 6b 05 LDA &056b,Y ; tiles_obstruction_y_offsets_table
&2462 24 09    BIT &09 ; tile_flip                                  # &40 set if flipped vertically
&2464 50 04    BVC &246a ; not_flipped
&2466 0a       ASL A                                                # Use bottom nibble is tile is flipped vertically
&2467 0a       ASL A
&2468 0a       ASL A
&2469 0a       ASL A                                                # ....8421 -> 8421....
; not_flipped
&246a 29 f0    AND #&f0                                             # Use top nibble if tile is not flipped vertically
&246c f0 02    BEQ &2470 ; skip_rounding                            # If zero, apply no offset to the obstruction pattern
&246e 09 0f    ORA #&0f                                             # Otherwise, round to end of pixel
; skip_rounding
&2470 95 7e    STA &7e,X ; top_tile_obstruction_y_offset
&2472 a5 09    LDA &09 ; tile_flip
&2474 0a       ASL A                                                # Set carry if flipped horizontally
&2475 85 9c    STA &9c ; tile_flip                                  # &80 set if flipped vertically
&2477 59 ab 04 EOR &04ab,Y ; tiles_sprite_and_y_flip_table          # &80 set if flipped vertically
&247a 95 7f    STA &7f,X ; top_tile_sprite_and_y_flip
&247c b9 eb 04 LDA &04eb,Y ; tiles_y_offset_and_pattern_table       # ....8421 set of four patterns to use from &05ab
&247f 2a       ROL A                                                # Set &01 (becomes &02) if flipped horizontally
;                                                                   # 84218421 -> 4218421h
&2480 06 9c    ASL &9c ; tile_flip                                  
&2482 2a       ROL A                                                # Set &01 if flipped vertically
;                                                                   #          -> 218421hv
&2483 29 3f    AND #&3f                                             #          -> ..8421hv
&2485 a8       TAY
&2486 b9 ab 05 LDA &05ab,Y ; obstruction_pattern_low_addresses_table
&2489 95 7c    STA &7c,X ; top_tile_obstruction_data_address_low    # Use obstruction pattern for flip
&248b a9 01    LDA #&01 ; &0100 = obstruction_patterns
&248d 95 7d    STA &7d,X ; top_tile_obstruction_data_address_high
&248f a2 00    LDX #&00
#     actually LDX tmp_x
&2491 60       RTS                                                  # Leave with A = 1

; play_scream_if_damaged
&2492 20 3c 25 JSR &253c ; check_if_object_was_damaged              # Returns carry set if object has just taken >= 8 damage
&2495 90 0e    BCC &24a5 ; leave
; play_scream
&2497 20 fa 13 JSR &13fa ; play_sound
&249a 33 03 2d 24                                                   # Play sound for scream
&249e 20 fa 14 JSR &13fa ; play_sound
&24a1 33 03 2b 25                                                   # Play sound for scream
; leave
&24a5 60       RTS

; damage_object                                                     # Called with A = damage, Y = object
&24a6 c0 00    CPY #&00 
&24a8 d0 41    BNE &24eb ; not_player
; is_player
&24aa 8d 5f 08 STA &085f ; player_damage                            # Also weapons_energy_cost + 5 (protection suit)
&24ad 24 da    BIT &da ; rnd_state + 1                              # 1 in 2 chance of skipping damage immobility
&24af 30 08    BMI &24b9 ; skip_immobility_because_of_damage
&24b1 46 31    LSR &31 ; player_is_lying_down                       # Clear top bit to stop lying down
&24b3 c5 ba    CMP &ba ; player_immobility_timers + 0 (movement)
&24b5 90 02    BCC &24b9 ; skip_immobility_because_of_damage        # Don't add extra immobility for lesser damage
&24b7 85 ba    STA &ba ; player_immobility_timers + 0 (movement)
; skip_immobility_because_of_damage
&24b9 8a       TXA
&24ba 48       PHA ; tmp_x
&24bb 2c 13 08 BIT &0813 ; player_protection_suit_collected         # Negative if protection suit collected
&24be 10 0d    BPL &24cd ; no_protection
&24c0 a2 05    LDX #&05 ; protection suit
&24c2 20 79 2d JSR &2d79 ; reduce_energy_of_weapon_X                # Reduce energy of protection suit by 2 * damage
&24c5 20 79 2d JSR &2d79 ; reduce_energy_of_weapon_X
&24c8 20 92 2d JSR &2d92 ; check_reliability                        # Returns carry clear if protection suit unreliable
&24cb b0 0d    BCS &24da ; skip_multiplying_damage
; no_protection
&24cd a0 03    LDY #&03
; multiply_damage_loop                                              # If not protected, multiply damage by eight
&24cf 0e 5f 08 ASL &085f ; player_damage                            # 84218421 -> 8 421842.
&24d2 90 03    BCC &24d7 ; skip_overflow                            # or highest power of two that doesn't cause overflow
&24d4 6e 5f 08 ROR &085f ; player_damage                            # 8 4218421 -> 84218421
; skip_overflow
&24d7 88       DEY
&24d8 d0 f5    BNE &24cf ; multiply_damage_loop
; skip_multiplying_damage
&24da a5 da    LDA &da ; rnd_state + 1
&24dc 29 07    AND #&07
&24de cd 5f 08 CMP &085f ; player_damage                            # Likelihood of scream depends on amount of damage
&24e1 b0 03    BCS &24e6 ; skip_scream                              # Always scream for damage >= 7
&24e3 20 97 24 JSR &2497 ; play_scream
; skip_scream
&24e6 68       PLA ; tmp_x
&24e7 aa       TAX ; tmp_x
&24e8 ad 5f 08 LDA &085f ; player_damage
; not_player
&24eb 85 9d    STA &9d ; damage
&24ed c9 08    CMP #&08
&24ef 90 08    BCC &24f9 ; skip_setting_flag
&24f1 b9 c6 08 LDA &08c6,Y ; objects_flags
&24f4 09 08    ORA #&08 ; OBJECT_FLAG_WAS_DAMAGED                   # Set &08 to indicate object has just taken 8 or more damage
&24f6 99 c6 08 STA &08c6,Y ; objects_flags
; skip_setting_flag
&24f9 b9 26 09 LDA &0926,Y ; objects_energy
&24fc 85 9c    STA &9c ; energy
&24fe 38       SEC
&24ff e5 9d    SBC &9d ; damage
&2501 b0 05    BCS &2508 ; set_energy
; set_energy_to_zero
&2503 a9 00    LDA #&00                                             # Set energy to zero to make object explode
&2505 2c a9 01 BIT &01a9 ; (nop)
; set_energy_to_one
#2506          LDA #&01
; set_energy
&2508 99 26 09 STA &0926,Y ; objects_energy
&250b 60       RTS                                                  # Leave with A = energy

; damage_object_without_destroying
&250c 20 a6 24 JSR &24a6 ; damage_object                            # Returns A = energy
&250f d0 04    BNE &2515 ; leave
&2511 a5 9c    LDA &9c ; energy
&2513 d0 f1    BNE &2506 ; set_energy_to_one
&2515 60       RTS

; set_object_for_removal
&2516 b9 c6 08 LDA &08c6,Y ; objects_flags
&2519 09 20    ORA #&20 ; OBJECT_FLAG_PENDING_REMOVAL
&251b 99 c6 08 STA &08c6,Y ; objects_flags
&251e 60       RTS

; reduce_energy_by_one
&251f 18       CLC
&2520 a5 15    LDA &15 ; this_object_energy
&2522 f0 04    BEQ &2528 ; leave
&2524 e9 00    SBC #&00                                             # Carry is clear, so subtract one
&2526 85 15    STA &15 ; this_object_energy
; leave
&2528 60       RTS

; set_object_for_removal
&2529 48       PHA ; tmp_a
&252a a5 6f    LDA &6f ; this_object_flags
&252c 09 20    ORA #&20 ; OBJECT_FLAG_PENDING_REMOVAL
&252e 85 6f    STA &6f ; this_object_flags
&2530 68       PLA ; tmp_a
&2531 38       SEC                                                  # Leave with carry set to indicate pending renewal (not used)
&2532 60       RTS

; check_if_object_is_pending_removal
&2533 48       PHA ; tmp_a
&2534 a5 6f    LDA &6f ; this_object_flags
&2536 29 20    AND #&20 ; OBJECT_FLAG_PENDING_REMOVAL
&2538 c9 20    CMP #&20 ; OBJECT_FLAG_PENDING_REMOVAL
&253a 68       PLA ; tmp_a
&253b 60       RTS                                                  # Leave with carry set if object is pending removal

; check_if_object_was_damaged
&253c 48       PHA ; tmp_a
&253d a5 6f    LDA &6f ; this_object_flags
&253f 29 08    AND #&08 ; OBJECT_FLAG_WAS_DAMAGED                   # Set if object has just taken 8 or more damage
&2541 c9 08    CMP #&08 ; OBJECT_FLAG_WAS_DAMAGED
&2543 68       PLA ; tmp_a
&2544 60       RTS                                                  # Leave with carry set if object has just taken >= 8 damage

; decrease_energy_by_one
&2545 c6 15    DEC &15 ; this_object_energy
&2547 d0 04    BNE &254d ; leave                                    # Leave unless zero, i.e. ensure at least one energy
; increase_energy_by_one
&2549 e6 15    INC &15 ; this_object_energy
&254b f0 f8    BEQ &2545 ; decrease_energy_by_one                   # Branch if overflow
; leave
&254d 60       RTS

; increase_energy_by_one_if_not_zero
&254e e6 15    INC &15 ; this_object_energy
&2550 c6 15    DEC &15 ; this_object_energy
&2552 d0 f5    BNE &2549 ; increase_energy_by_one
&2554 60       RTS

; update_sprite_offset_using_velocities                             # Called with A = modulus
&2555 a2 03    LDX #&03 ; divide velocities by 16
; update_sprite_offset_using_scaled_velocities
&2557 85 9c    STA &9c ; modulus
&2559 20 b6 3b JSR &3bb6 ; get_maximum_of_this_object_velocities    # Returns A = larger of x and y velocities
; divide_by_sixteen_loop
&255c 4a       LSR A
&255d ca       DEX
&255e 10 fc    BPL &255c ; divide_by_sixteen_loop
&2560 38       SEC                                                  # Add 1 + (largest velocity / (2 ** (X + 1)) to offset
&2561 65 12    ADC &12 ; this_object_timer (sprite offset)          # i.e. objects animate faster when moving faster
&2563 38       SEC
; calculate_remainder_loop
&2564 e5 9c    SBC &9c ; modulus                                    # Keep between 0 and modulus - 1
&2566 b0 fc    BCS &2564 ; calculate_remainder_loop
&2568 65 9c    ADC &9c ; modulus
&256a 85 12    STA &12 ; this_object_timer (sprite offset)
&256c 60       RTS                                                  # Leave with A = sprite offset

; set_npc_facing_tile_collision                                     # Used for frogmen and imps
&256d 24 1b    BIT &1b ; this_object_tile_top_or_bottom_collision   # &80 set if object hit tiles above or below
&256f 10 06    BPL &2577 ; leave
&2571 a5 1c    LDA &1c ; tile_collision_angle                       # &40 if collision was to left  -> &c0, flipped
;                                                                   # &c0 if collision was to right -> &40, unflipped
&2573 49 ff    EOR #&ff                                             # i.e. make NPC face the surface
&2575 85 37    STA &37 ; this_object_x_flip
; leave
&2577 60       RTS

; consider_flipping_object_to_match_velocity_x
&2578 a9 03    LDA #&03                                             # 1 in 64 chance of flipping object
; consider_flipping_object_to_match_velocity_x_A                    # Called with A = probability of flipping object
&257a 25 d9    AND &d9 ; rnd_state
&257c d0 06    BNE &2584 ; leave_with_this_object_x_flip
; flip_object_to_match_velocity_x
&257e a5 43    LDA &43 ; this_object_velocity_x
&2580 f0 02    BEQ &2584 ; leave_with_this_object_x_flip
&2582 85 37    STA &37 ; this_object_x_flip
; leave_with_this_object_x_flip
&2584 a5 37    LDA &37 ; this_object_x_flip
&2586 60       RTS                                                  # Leave with A = this_object_x_flip

; rnd
&2587 65 dc    ADC &dc ; rnd_state + 3
&2589 65 d9    ADC &d9 ; rnd_state
&258b 85 d9    STA &d9 ; rnd_state
&258d 65 db    ADC &db ; rnd_state + 2
&258f 85 db    STA &db ; rnd_state + 2
&2591 65 da    ADC &da ; rnd_state + 1
&2593 85 da    STA &da ; rnd_state + 1
&2595 65 dc    ADC &dc ; rnd_state + 3
&2597 85 dc    STA &dc ; rnd_state + 3
&2599 60       RTS

; update_events
&259a a9 67    LDA #&67                                             # Flood up to y = &67 if world is flooding
&259c 2c 1e 08 BIT &081e ; flooding_state                           # Negative if world is flooding
&259f 30 3e    BMI &25df ; skip_updating_triax_lab
; update_triax_lab                                                  # Otherwise, update Triax's lab
&25a1 a9 60    LDA #&60 ; 24 * 4                                    # Maggots never run out while machine is operating
&25a3 8d 88 09 STA &0988 ; tertiary_objects_data + &02              # Tertiary object &01, world maggots
&25a6 24 c1    BIT &c1 ; every_sixty_four_frames
&25a8 10 19    BPL &25c3 ; skip_creating_maggot                     # Every sixty four frames, create a maggot
&25aa a9 27    LDA #&27 ; OBJECT_MAGGOT
&25ac 20 60 1e JSR &1e60 ; create_new_object_if_four_slots_free     # Returns carry clear if object created, Y = slot
&25af b0 12    BCS &25c3 ; skip_creating_maggot
&25b1 a9 d9    LDA #&d9                                             # Create maggot in transporter tile of maggot machine
&25b3 99 b4 08 STA &08b4,Y ; objects_y
&25b6 a9 61    LDA #&61
&25b8 99 91 08 STA &0891,Y ; objects_x
&25bb 99 80 08 STA &0880,Y ; objects_x_fraction
&25be a9 70    LDA #&70
&25c0 99 a3 08 STA &08a3,Y ; objects_y_fraction
; skip_creating_maggot
&25c3 ad 48 0a LDA &0a48 ; tertiary_objects_data + &c2              # Tertiary object &cd, TILE_STONE_DOOR at (&6b, &e1) (bottom of Triax's lab)
&25c6 24 c2    BIT &c2 ; every_thirty_two_frames                    # Every thirty two frames,
&25c8 10 0b    BPL &25d5 ; set_door_data
&25ca 29 fd    AND #&fd ; !DOOR_FLAG_OPENING                        # Close the bottom door in Triax's lab
&25cc ac 33 08 LDY &0833 ;  waterline_x_ranges_y + 1 (Triax's lab)
&25cf c0 e0    CPY #&e0                                             # unless the waterline is more than a tile above it
&25d1 b0 02    BCS &25d5 ; set_door_data
&25d3 09 02    ORA #&02 ; DOOR_FLAG_OPENING                         # in which case, open the door
; set_door_data
&25d5 8d 48 0a STA &0a48 ; tertiary_objects_data + &c2              # Tertiary object &cd, TILE_STONE_DOOR at (&6b, &e1) (bottom of Triax's lab)
&25d8 29 02    AND #&02 ; DOOR_FLAG_OPENING
&25da 0a       ASL A                                                # &02 -> &10
&25db 0a       ASL A
&25dc 0a       ASL A                                            
&25dd 69 d2    ADC #&d2                                             # &02 -> &e2, i.e. make water drop if door is open
;                                                                   # &00 -> &d2, i.e. make water rise if door is closed
; skip_updating_triax_lab
&25df 8d 37 08 STA &0837 ; waterline_x_ranges_desired_y + 1 (Triax's lab)
&25e2 ad 1f 08 LDA &081f ; earthquake_state                         # Negative if earthquake has started
&25e5 10 2b    BPL &2612 ; skip_earthquake
; is_earthquake
&25e7 0a       ASL A                                                # 84218421 -> 4218421. i.e. earth quake timer
&25e8 c5 db    CMP &db ; rnd_state + 2                              # Set carry more as frequently as earth quake progresses
&25ea 29 10    AND #&10
&25ec 2a       ROL A                                                # ...8.... -> ..8....C
&25ed f0 0b    BEQ &25fa ; skip_worsening_earthquake                # Worsen earthquake increasingly frequently at start
&25ef 24 c4    BIT &c4 ; every_eight_frames
&25f1 10 07    BPL &25fa ; skip_worsening_earthquake                # Only worsen every eight frames,
&25f3 c9 21    CMP #&21
&25f5 f0 03    BEQ &25fa ; skip_worsening_earthquake                # Then decreasingly frequently
&25f7 ee 1f 08 INC &081f ; earthquake_state                         # Earthquake gradually worsens
; skip_worsening_earthquake
&25fa 4a       LSR A
&25fb d0 15    BNE &2612 ; skip_earthquake                          # If carry was set, i.e. with varying frequency,
&25fd 20 87 25 JSR &2587 ; rnd
&2600 29 01    AND #&01                                             # Randomly shudder screen
&2602 09 5a    ORA #&5a
&2604 a0 02    LDY #&02 ; R2: Horizontal sync position register
&2606 78       SEI
&2607 8c 00 fe STY &fe00 ; video register number
&260a 8d 01 fe STA &fe01 ; video register value
&260d 58       CLI
&260e a0 00    LDY #&00 ; OBJECT_SLOT_PLAYER                        # Use player position as source of sound
&2610 f0 07    BEQ &2619 ; play_earthquake_sound                    # Always branches
; skip_earthquake
&2612 2c 1e 08 BIT &081e ; flooding_state
&2615 30 0f    BMI &2626 ; skip_waterfall_sound                     # If not in endgame,
&2617 a0 11    LDY #&11 ; OBJECT_SLOT_WATERFALL                     # Use waterfall position as source of sound
; play_waterfall_or_earthquake_sound
&2619 84 aa    STY &aa ; this_object
&261b 24 c4    BIT &c4 ; every_eight_frames                         # Every eight frames,
&261d 10 07    BPL &2626 ; skip_waterfall_sound
&261f 20 f8 13 JSR &13f8 ; play_sound_on_channel_zero
&2622 70 c2 6e a3                                                   # Play sound for waterfall or earthquake
; skip_waterfall_sound
&2626 a2 fe    LDX #&fe                                             # Raise waterline for 32 out of 64 frames
&2628 a5 c0    LDA &c0 ; frame_counter
&262a 29 20    AND #&20
&262c d0 02    BNE &2630 ; not_lowering
&262e a2 02    LDX #&02                                             # Lower waterline for other 32 out of 64 frames
; not_lowering
&2630 86 a2    STX &a2 ; waterline_delta
&2632 a2 03    LDX #&03
; update_waterlines_loop                                            # For each of the world's x ranges,
&2634 a9 18    LDA #&18
&2636 38       SEC
&2637 fd 2e 08 SBC &082e,X ; waterline_x_ranges_y_fraction
&263a bd 36 08 LDA &0836,X ; waterline_x_ranges_desired_y           # Move waterline towards desired value
&263d fd 32 08 SBC &0832,X ; waterline_x_ranges_y
&2640 65 a2    ADC &a2 ; waterline_delta                            # allowing periodic rising and falling
&2642 08       PHP ; underflow
&2643 a0 02    LDY #&02 ; range
&2645 20 5e 32 JSR &325e ; keep_within_range                        # Don't move more than +/- &02 fraction per update
&2648 7d 2e 08 ADC &082e,X ; waterline_x_ranges_y_fraction
&264b 9d 2e 08 STA &082e,X ; waterline_x_ranges_y_fraction
&264e 90 03    BCC &2653 ; skip_overflow
&2650 fe 32 08 INC &0832,X ; waterline_x_ranges_y
; skip_overflow
&2653 28       PLP ; underflow
&2654 10 03    BPL &2659 ; skip_underflow
&2656 de 32 08 DEC &0832,X ; waterline_x_ranges_y
; skip_underflow
&2659 ca       DEX
&265a 10 d8    BPL &2634 ; update_waterlines_loop
&265c a9 10    LDA #&10 ; TILE_PROCESSING_FLAG_EVENTS               # Only call tile update routines that want to know about
&265e 85 2d    STA &2d ; tile_processing_mode                       # random events (everything other than invisible switches)
; consider_emerging_worm_or_maggot
&2660 a9 07    LDA #&07                                             # Consider a random tile +/- 4 tiles in x and y
&2662 20 43 27 JSR &2743 ; get_random_tile_near_player              # Also calls tile update routines for events
&2665 20 15 17 JSR &1715 ; get_tile_and_check_for_tertiary_objects  # Returns A = tile type
&2668 c9 2d    CMP #&2d ; TILE_EARTH
&266a d0 5c    BNE &26c8 ; skip_emerging_worm_or_maggot             # Worms and maggots only emerge from solid earth
&266c a5 c3    LDA &c3 ; every_sixteen_frames
&266e 10 58    BPL &26c8 ; skip_emerging_worm_or_maggot             # every sixteen frames
&2670 ad 1e 08 LDA &081e ; flooding_state                           # &80 set if world is flooding
&2673 29 80    AND #&80
&2675 49 80    EOR #&80                                             # &80 clear if world is flooding; otherwise, only
&2677 05 da    ORA &da ; rnd_state + 1                              # emerge worms or maggots in bottom half of world
&2679 c5 97    CMP &97 ; tile_y                                     # Worms or maggots more likely further down
&267b b0 4b    BCS &26c8 ; skip_emerging_worm_or_maggot
&267d 2c 1d 08 BIT &081d ; explosion_timer                          # Negative if explosion in progress
&2680 30 06    BMI &2688 ; emerge_worm_or_maggot
&2682 a5 db    LDA &db ; rnd_state + 2
&2684 29 70    AND #&70                                             # If not, 1 in 8 chance of emerging worm or maggot
&2686 d0 40    BNE &26c8 ; skip_emerging_worm_or_maggot
; emerge_worm_or_maggot
&2688 a0 01    LDY #&01                                             # Use tertiary_objects_data + &01 (&0987, world worms)
&268a 20 87 25 JSR &2587 ; rnd
&268d c9 08    CMP #&08                                             # 31 in 32 chance of using preferred type
&268f 6a       ROR A                                                # Set &80 if using preferred type
&2690 2c 1e 08 BIT &081e ; flooding_state                           # Negative if world is flooding
&2693 30 06    BMI &269b ; is_flooding                              # Set &80 if world is flooding to prefer maggots
&2695 2c aa 19 BIT &19aa ; player_is_east_of_76                     # Negative if player is east of &76
&2698 20 56 32 JSR &3256 ; invert_if_negative                       # Clear &80 if player is east of &76 to prefer worms
; is_flooding
&269b 0a       ASL A                                                # If &80 set, use maggot as preferred type
&269c 90 01    BCC &269f ; is_worm                                  # If &80 clear, use worm as preferred type
; is_maggot
&269e c8       INY                                                  # Use tertiary_objects_data + &02 (&0988, world maggots)
; is_worm
&269f 84 bd    STY &bd ; this_object_tertiary_data_offset
&26a1 84 be    STY &be ; this_object_tertiary_type_offset
&26a3 b9 86 09 LDA &0986,Y ; tertiary_objects_data
&26a6 0a       ASL A                                                # Consider number of creatures remaining
&26a7 c5 db    CMP &db ; rnd_state + 2                              # Worms and maggots less likely to emerge when fewer left
&26a9 90 1d    BCC &26c8 ; skip_emerging_worm_or_maggot
&26ab 20 60 27 JSR &2760 ; spawn_object_in_event                    # Returns carry set if object couldn't be created
&26ae b0 18    BCS &26c8 ; skip_emerging_worm_or_maggot
&26b0 99 a3 08 STA &08a3,Y ; objects_y_fraction                     # A = objects_x_fraction here, so roughly centre in y
&26b3 ad 91 08 LDA &0891 ; objects_x + 0 (player)
&26b6 e5 95    SBC &95 ; tile_x
&26b8 99 e6 08 STA &08e6,Y ; objects_velocity_x                     # Set worm or maggot moving towards player
&26bb ad b4 08 LDA &08b4 ; objects_y + 0 (player)
&26be e5 97    SBC &97 ; tile_y
&26c0 99 f6 08 STA &08f6,Y ; objects_velocity_y
&26c3 a9 80    LDA #&80
&26c5 99 76 09 STA &0976,Y ; objects_state (behaviour and walking)  # Set &80 to make worm or maggot want to burrow (out of earth)
; skip_emerging_worm_or_maggot
&26c8 a5 97    LDA &97 ; tile_y
&26ca c9 4e    CMP #&4e
&26cc b0 18    BCS &26e6 ; skip_adding_star                         # Is the random tile above surface?
&26ce 85 8d    STA &8d ; new_particles_y
&26d0 a5 95    LDA &95 ; tile_x
&26d2 85 8b    STA &8b ; new_particles_x
&26d4 a9 00    LDA #&00
&26d6 85 89    STA &89 ; new_particles_y_fraction
&26d8 85 87    STA &87 ; new_particles_x_fraction
&26da ad b5 19 LDA &19b5 ; player_is_completely_dematerialised      # &80 set if player is briefly removed
&26dd 05 00    ORA &00 ; tile_was_from_map_data                     # &80 set if tile was from map data
&26df 30 05    BMI &26e6 ; skip_adding_star                         # i.e. don't add stars inside the spaceships
&26e1 a0 4d    LDY #&4d ; PARTICLE_STAR_OR_MUSHROOM
&26e3 20 8c 21 JSR &218c ; add_particle                             # Add a star
; skip_adding_star
&26e6 ac 1f 08 LDY &081f ; earthquake_state
&26e9 88       DEY
&26ea c0 c8    CPY #&c8
&26ec 6a       ROR A                                                # &80 set if late earthquake
&26ed 2d 1e 08 AND &081e ; flooding_state                           # &80 set if world is being flooded
&26f0 05 c2    ORA &c2 ; every_thirty_two_frames                    # &80 set every thirty two frames
&26f2 10 20    BPL &2714 ; skip_summoning_triax_and_checking_copy_protection
&26f4 20 87 25 JSR &2587 ; rnd                                      # 1 in 256 chance to consider summoning Triax
&26f7 d0 1b    BNE &2714 ; skip_summoning_triax_and_checking_copy_protection
&26f9 ad b4 08 LDA &08b4 ; objects_y + 0 (player)
&26fc e9 14    SBC #&14                                             # &80 set if player y >= &94
&26fe 0d 1e 08 ORA &081e ; flooding_state                           # &80 set if world is being flooded
&2701 10 0e    BPL &2711 ; skip_summoning_triax                     # i.e. don't summon Triax in upper world until endgame
&2703 a9 26    LDA #&26 ; OBJECT_TRIAX
&2705 20 18 3c JSR &3c18 ; count_objects_of_type_A                  # Returns Y = count
&2708 d0 07    BNE &2711 ; skip_summoning_triax                     # If Triax isn't already present,
&270a a9 26    LDA #&26 ; OBJECT_TRIAX
&270c 20 60 1e JSR &1e60 ; create_new_object_if_four_slots_free     # Returns carry clear if object created, Y = slot
&270f 90 27    BCC &2738 ; set_object_y
; skip_summoning_triax
&2711 20 4e 39 JSR &394e ; check_copy_protection                    # Hangs if demo mode
;                                                                   # 1 in 256 chance of hanging every 32 frames
; skip_summoning_triax_and_checking_copy_protection
&2714 24 c4    BIT &c4 ; every_eight_frames
&2716 10 2a    BPL &2742 ; leave                                    # Every eight frames,
; consider_summoning_clawed_robot
&2718 20 87 25 JSR &2587 ; rnd
&271b 29 03    AND #&03                                             # Pick a random clawed robot
&271d aa       TAX
&271e bd 3f 08 LDA &083f,X ; clawed_robots_availability
&2721 30 1f    BMI &2742 ; leave                                    # Negative if clawed robot is dormant
&2723 d0 1d    BNE &2742 ; leave                                    # Positive non-zero if already promoted to primary
&2725 fe 43 08 INC &0843,X ; clawed_robots_teleporting_energy       # Weakened robots take longer to return to game,
&2728 10 18    BPL &2742 ; leave                                    # If the robot has the energy to return to game
&272a 8a       TXA
&272b 18       CLC
&272c 69 22    ADC #&22 ; OBJECT_MAGENTA_CLAWED_ROBOT
&272e 20 60 1e JSR &1e60 ; create_new_object_if_four_slots_free     # Returns carry clear if object created, Y = slot
&2731 b0 0f    BCS &2742 ; leave
&2733 a9 01    LDA #&01
&2735 9d 3f 08 STA &083f,X ; clawed_robots_availability             # Set to positive non-zero to indicate promoted to primary
; set_object_y
&2738 a9 fe    LDA #&fe                                             # Put Triax or clawed robot at bottom of world
&273a 99 b4 08 STA &08b4,Y ; objects_y                              # Will teleport nearer to player randomly in update
&273d a9 c0    LDA #&c0 ; TARGET_FLAG_DIRECTNESS_THREE | OBJECT_SLOT_PLAYER
&273f 99 06 09 STA &0906,Y ; objects_target_object_and_flags        # More directly towards the player
; leave
&2742 60       RTS

; get_random_tile_near_player                                       # Called with A = diameter around player
&2743 85 9d    STA &9d ; diameter
&2745 4a       LSR A
&2746 85 9c    STA &9c ; half_diameter
&2748 20 87 25 JSR &2587 ; rnd
&274b 25 9d    AND &9d ; diameter
&274d 6d 91 08 ADC &0891 ; objects_x + 0 (player)
&2750 e5 9c    SBC &9c ; half_diameter
&2752 85 95    STA &95 ; tile_x
&2754 a5 d9    LDA &d9 ; rnd_state
&2756 25 9d    AND &9d ; diameter
&2758 6d b4 08 ADC &08b4 ; objects_y + 0 (player)
&275b e5 9c    SBC &9c ; half_diameter
&275d 85 97    STA &97 ; tile_y
&275f 60       RTS

; spawn_object_in_event                                             # Called with Y = tertiary_data_offset
&2760 ba       TSX
&2761 86 26    STX &26 ; stack_pointer_to_leave_to_if_unable_to_create_primary_object # Leave spawn_object_in_event if so
&2763 a2 06    LDX #&06                                             # Six free slots needed to spawn object
&2765 4c 4f 3e JMP &3e4f ; spawn_object

; find_a_target_and_fire_at_it                                      # Called with A = target type, &80 set to target player too
&2768 85 9d    STA &9d ; target_type                                #             X = projectile type
&276a a5 15    LDA &15 ; this_object_energy
&276c 4a       LSR A
; find_a_target_and_fire_at_it_with_likelihood_A_divided_by_four
&276d 4a       LSR A
&276e 4a       LSR A
&276f 69 02    ADC #&02
&2771 c5 da    CMP &da ; rnd_state + 1                              # Enemies more likely to fire when they have more energy
&2773 90 14    BCC &2789 ; leave
&2775 8e 80 27 STX &2780 ; projectile_type
&2778 a5 9d    LDA &9d ; target_type
&277a 20 2a 3c JSR &3c2a ; find_object                              # Returns positive if object found, X = object
&277d 30 0a    BMI &2789 ; leave
&277f a0 18    LDY #&18 ; OBJECT_PISTOL_BULLET
#     actually LDY projectile_type
&2781 20 8a 27 JSR &278a ; fire_at_target                           # Returns positive if trying to fire backwards
&2784 30 03    BMI &2789 ; leave
&2786 20 36 31 JSR &3136 ; flip_this_object_horizontally            # Flip object to face target
; leave
&2789 60       RTS

; fire_at_target                                                    # Called with X = target
&278a 20 87 25 JSR &2587 ; rnd
&278d 29 3f    AND #&3f ; (&10 * 4) - 1
&278f 69 b4    ADC #&b4 ; &2d * 4                                   # Use a random firing velocity between &2d and &3c
; fire_at_target_with_velocity                                      # Called with X = target
&2791 84 a0    STY &a0 ; projectile_type
&2793 20 55 33 JSR &3355 ; calculate_firing_vector_from_distance    # Returns carry set if unable to calculate velocities
&2796 6a       ROR A                                                # Set &80 if unable to calculate velocities
&2797 38       SEC                                                  # Leave with carry set to indicate unable to calculate velocities
&2798 30 2e    BMI &27c8 ; leave                                    # Leave with negative to indicate not trying to fire backwards
&279a a5 43    LDA &43 ; this_object_velocity_x
&279c e5 b4    SBC &b4 ; vector_x
&279e 20 7f 32 JSR &327f ; prevent_overflow
&27a1 45 37    EOR &37 ; this_object_x_flip
&27a3 10 22    BPL &27c7 ; leave_with_carry_clear                   # Leave with positive to indicate trying to fire backwards
&27a5 8a       TXA
&27a6 48       PHA ; target
&27a7 a5 dc    LDA &dc ; rnd_state + 3
&27a9 29 03    AND #&03
&27ab 45 b6    EOR &b6 ; vector_y                                   # Make projectile y velocity slightly random
&27ad 85 b6    STA &b6 ; vector_y                                   # Child object velocities set from vector at &33cc
&27af a5 a0    LDA &a0 ; projectile_type
&27b1 20 b8 33 JSR &33b8 ; create_child_object                      # Returns carry clear if object created, X = slot
&27b4 68       PLA ; target
&27b5 b0 0e    BCS &27c5 ; not_created                              # Leave (with carry clear) if projectile couldn't be created
&27b7 9d 06 09 STA &0906,X ; objects_target_object_and_flags        # Set target for projectile
&27ba 20 87 25 JSR &2587 ; rnd
&27bd 29 07    AND #&07
&27bf 5d e6 08 EOR &08e6,X ; objects_velocity_x                     # Make projectile x velocity slightly random
&27c2 9d e6 08 STA &08e6,X ; objects_velocity_x
; not_created
&27c5 a0 ff    LDY #&ff                                             # Leave with negative to indicate not trying to fire backwards
; leave_with_carry_clear
&27c7 18       CLC                                                  # Leave with carry clear to indicate able to calculate velocities
; leave
&27c8 60       RTS

; check_for_npc_stimuli                                             # Called with X = npc stimuli type
&27c9 a9 00    LDA #&00
&27cb 85 21    STA &21 ; stimuli
&27cd 86 22    STX &22 ; npc_type
&27cf a5 06    LDA &06 ; this_object_frame_counter
&27d1 29 3f    AND #&3f
&27d3 d0 3b    BNE &2810 ; skip_finding_target                      # Every sixty four frames, 
&27d5 bd 6b 31 LDA &316b,X ; npc_stimuli_types_phobia_table         # Look for objects that the NPC will move away from
&27d8 bc 75 31 LDY &3175,X ; npc_stimuli_types_target_table         # and objects that the NPC will move towards
&27db 20 fe 3b JSR &3bfe ; find_a_target                            # Returns positive if target found, X = target
;                                                                   #         carry set if target is primary type or player
&27de 30 07    BMI &27e7 ; no_target
&27e0 26 21    ROL &21 ; stimuli                                    # Set what will become &40 of A if phobia found
&27e2 d0 01    BNE &27e5 ; not_player
&27e4 38       SEC
; not_player
&27e5 26 21    ROL &21 ; stimuli                                    # Set what will become &20 of A if player found
; no_target
&27e7 a6 22    LDX &22 ; npc_type
&27e9 24 11    BIT &11 ; this_object_state (behaviour and walking)
&27eb 10 17    BPL &2804 ; skip_targeting_home                      # Positive if NPC_MOOD_ZERO or NPC_MOOD_PLUS_ONE
&27ed 70 15    BVS &2804 ; skip_targeting_home                      # &40 set if NPC_MOOD_MINUS_ONE
; is_mood_minus_two
&27ef a5 21    LDA &21 ; stimuli                                    # Non-zero if phobia or player found
&27f1 f0 08    BEQ &27fb ; skip_avoiding_target
&27f3 20 11 3c JSR &3c11 ; avoid_target
&27f6 20 87 25 JSR &2587 ; rnd
&27f9 30 09    BMI &2804 ; skip_targeting_home                      # 1 in 2 chance of not looking for home
; skip_avoiding_target
&27fb bd 89 31 LDA &3189,X ; npc_stimuli_types_home_table
&27fe a8       TAY
&27ff 20 fe 3b JSR &3bfe ; find_a_target                            # Returns positive if target found, X = target
;                                                                   # If so, NPC will head towards home
&2802 a6 22    LDX &22 ; npc_type
; skip_targeting_home
&2804 20 87 25 JSR &2587 ; rnd
&2807 30 07    BMI &2810 ; skip_targeting_food
&2809 bd 7f 31 LDA &317f,X ; npc_stimuli_types_absorb_table
&280c a8       TAY
&280d 20 fe 3b JSR &3bfe ; find_a_target                            # Returns positive if target found, X = target
;                                                                   #         carry set if target is primary type
;                                                                   # If so, NPC will head towards food
; skip_targeting_food
; skip_finding_target
&2810 06 21    ASL &21 ; stimuli                                    # Clear what will become &10 of A unless food absorbed
&2812 a6 22    LDX &22 ; npc_type
&2814 bd 7f 31 LDA &317f,X ; npc_stimuli_types_food_table
&2817 20 e1 3b JSR &3be1 ; consider_absorbing_object_touched        # Returns zero if object absorbed
&281a d0 02    BNE &281e ; not_absorbed
&281c e6 21    INC &21 ; stimuli                                    # Set what will become &10 of A if food absorbed
;                                                                   # Set &01 of stimuli if food absorbed
; not_absorbed
&281e a6 22    LDX &22 ; npc_type
&2820 bc 93 31 LDY &3193,X ; npc_stimuli_types_responses_table
&2823 a5 21    LDA &21 ; stimuli
&2825 84 9c    STY &9c ; responses
&2827 20 3c 25 JSR &253c ; check_if_object_was_damaged              # Returns carry set if object has just taken >= 8 damage
&282a 2a       ROL A                                                # Set what will become &08 if NPC has just taken damage
&282b ac 1d 08 LDY &081d ; explosion_timer                          # Negative if explosion in progress
&282e c0 cf    CPY #&cf
&2830 f0 01    BEQ &2833 ; is_start_of_explosion
&2832 18       CLC
; is_start_of_explosion
&2833 2a       ROL A                                                # Set what will become &04 if start of explosion
&2834 ac 1e 08 LDY &081e ; flooding_state                           # Negative if world is flooding
&2837 c0 80    CPY #&80
&2839 2a       ROL A                                                # Set what will become &02 if world is flooding
&283a a4 06    LDY &06 ; this_object_frame_counter
&283c c0 ff    CPY #&ff
&283e 2a       ROL A                                                # Set &01 every 256 frames
&283f 25 da    AND &da ; rnd_state + 1
&2841 f0 23    BEQ &2866 ; leave                                    # 1 in 2 chance of considering any particular stimulus
&2843 a2 00    LDX #&00
&2845 a0 07    LDY #&07
; check_responses_loop
&2847 4a       LSR A                                                # Get lowest bit of stimuli; if set, it was experienced
&2848 90 07    BCC &2851 ; consider_next_bit
&284a ca       DEX
&284b 24 9c    BIT &9c ; flags_to_respond_to                        # Get highest bit of responses
&284d 10 02    BPL &2851 ; consider_next_bit
&284f e8       INX                                                  # Increase X if response bit is set, decrease if unset
&2850 e8       INX
; consider_next_bit
&2851 06 9c    ASL &9c ; responses                                  # Consider next bit of responses on next pass
&2853 88       DEY
&2854 d0 f1    BNE &2847 ; check_responses_loop
&2856 8a       TXA
&2857 f0 0d    BEQ &2866 ; leave
&2859 29 c0    AND #&c0 ; NPC_MOOD_MINUS_ONE                        # If X negative, i.e. more response bits for experienced
;                                                                   # stimuli were unset, make NPC mood more negative
&285b 30 02    BMI &285f ; alter_npc_behaviour
&285d a9 40    LDA #&40 ; NPC_MOOD_PLUS_ONE                         # If X positive, i.e. same or more response bits for
;                                                                   # experienced stimuli were set, make NPC mood more positive
; alter_npc_behaviour
&285f 18       CLC
&2860 65 11    ADC &11 ; this_object_state (behaviour and walking)  # Apply chance of mood to NPC
&2862 70 02    BVS &2866 ; leave
&2864 85 11    STA &11 ; this_object_state (behaviour and walking)
; leave
&2866 60       RTS                                                  # Leave with &01 of stimuli set if food absorbed

; set_object_x_y_and_tx_ty_from_tile_x_y
&2867 a5 95    LDA &95 ; tile_x
&2869 99 91 08 STA &0891,Y ; objects_x
&286c 99 16 09 STA &0916,Y ; objects_tx
&286f a5 97    LDA &97 ; tile_y
&2871 99 b4 08 STA &08b4,Y ; objects_y
&2874 99 36 09 STA &0936,Y ; objects_ty
&2877 60       RTS

; set_object_x_y_from_this_object_x_y
&2878 a5 53    LDA &53 ; this_object_x
&287a 99 91 08 STA &0891,Y ; objects_x
&287d a5 55    LDA &55 ; this_object_y
&287f 99 b4 08 STA &08b4,Y ; objects_y
&2882 60       RTS

; set_this_object_tx_ty_from_object_X_x_y
&2883 bd 91 08 LDA &0891,X ; objects_x
&2886 85 14    STA &14 ; this_object_tx
&2888 bd b4 08 LDA &08b4,X ; objects_y
&288b 85 16    STA &16 ; this_object_ty
&288d 60       RTS

; set_target_object_x_y_from_this_object_tx_ty
&288e a2 10    LDX #&10 ; OBJECT_SLOT_TARGET
&2890 a5 14    LDA &14 ; this_object_tx
&2892 9d 91 08 STA &0891,X ; objects_x
&2895 a5 16    LDA &16 ; this_object_ty
&2897 9d b4 08 STA &08b4,X ; objects_y
&289a a9 80    LDA #&80
&289c 9d 80 08 STA &0880,X ; objects_x_fraction
&289f 9d a3 08 STA &08a3,X ; objects_y_fraction
&28a2 60       RTS

; set_this_object_velocities_to_zero
&28a3 a9 00    LDA #&00
&28a5 85 43    STA &43 ; this_object_velocity_x
&28a7 85 45    STA &45 ; this_object_velocity_y
&28a9 60       RTS

; set_this_object_position_from_previous_position
&28aa a5 52    LDA &52 ; this_object_previous_y_fraction
&28ac 85 51    STA &51 ; this_object_y_fraction
; set_this_object_position_from_previous_position_except_y_fraction
&28ae a5 56    LDA &56 ; this_object_previous_y
&28b0 85 55    STA &55 ; this_object_y
&28b2 a5 50    LDA &50 ; this_object_previous_x_fraction
&28b4 85 4f    STA &4f ; this_object_x_fraction
&28b6 a5 54    LDA &54 ; this_object_previous_x
&28b8 85 53    STA &53 ; this_object_x
&28ba 60       RTS

; wipe_memory_then_infinite_loop
&28bb a9 00    LDA #&00
&28bd a8       TAY
&28be 88       DEY ; &ff
&28bf 99 08 7f STA &7f08,Y ; rom_copyright_offset - &ff             # Set &8007 to zero to ignore as ROM
&28c2 c8       INY ; 0
; wipe_memory_loop                                                  # Wipe &01c4 - &28c3
&28c3 99 c4 01 STA &01c4,Y
#     actually STA wipe_address,Y
&28c6 c8       INY
&28c7 d0 fa    BNE &28c3 ; wipe_memory_loop
&28c9 ee c5 28 INC &28c5 ; wipe_address_high
&28cc cd c3 28 CMP &28c3 ; wipe_memory_loop                         # Becomes zero once wiping reaches &28c3
&28cf d0 f2    BNE &28c3 ; wipe_memory_loop
&28d1 6e 8e 02 ROR &028e ; os_available_ram                         # Set top bit to indicate system has 32k memory
; infinite_loop
&28d4 30 fe    BMI &28d4 ; infinite_loop                            # Always branches

; particles_data                                                    # &28d6 = particles_velocity_x
;     xv yv xf yf  x  y  l cf                                       # &28d7 = particles_velocity_y
&28d6 00 00 00 00 00 00 00 00                                       # &28d8 = particles_x_fraction
&28de 00 00 00 00 00 00 00 00                                       # &28d9 = particles_y_fraction
&28e6 00 00 00 00 00 00 00 00                                       # &28da = particles_x
&28ee 00 00 00 00 00 00 00 00                                       # &28db = particles_y
&28f6 00 00 00 00 00 00 00 00                                       # &28dc = particles_ttl
&28fe 00 00 00 00 00 00 00 00                                       # &28dd = particles_colour_and_flags
&2906 00 00 00 00 00 00 00 00                                       #     8....... always set
&290e 00 00 00 00 00 00 00 00                                       #     .4...... set if particle is double height
&2916 00 00 00 00 00 00 00 00                                       #     ..2..... set if particle is plotted on foreground
&291e 00 00 00 00 00 00 00 00                                       #     ...1.... set if particle accelerates
&2926 00 00 00 00 00 00 00 00                                       #     ....8... set to cycle particle colour
&292e 00 00 00 00 00 00 00 00                                       #     .....421 colour
&2936 00 00 00 00 00 00 00 00
&293e 00 00 00 00 00 00 00 00
&2946 00 00 00 00 00 00 00 00
&294e 00 00 00 00 00 00 00 00
&2956 00 00 00 00 00 00 00 00
&295e 00 00 00 00 00 00 00 00
&2966 00 00 00 00 00 00 00 00
&296e 00 00 00 00 00 00 00 00
&2976 00 00 00 00 00 00 00 00
&297e 00 00 00 00 00 00 00 00
&2986 00 00 00 00 00 00 00 00
&298e 00 00 00 00 00 00 00 00
&2996 00 00 00 00 00 00 00 00
&299e 00 00 00 00 00 00 00 00
&29a6 00 00 00 00 00 00 00 00
&29ae 00 00 00 00 00 00 00 00
&29b6 00 00 00 00 00 00 00 00
&29be 00 00 00 00 00 00 00 00
&29c6 00 00 00 00 00 00 00 00
&29ce 00 00 00 00 00 00 00 00

; player_firing_cooldown
&29d6 00

; player_object_fired
&29d7 ff

; whistle_two_activating_object
&29d8 ff                                                            # Positive if whistle two played

; this_object_crosses_tile_in_x
; this_object_crosses_tile_in_y - 2
;      x     y
&29d9 00 ea 00                                                      # Second byte is unused

; collision_velocity_direction_flags_table
;      x     y
&29dc 0c ea 03                                                      # Second byte is unused

; collision_pixel_rounding_table
;      x     y
&29df 0f ea 07                                                      # Second byte is unused

; collision_pixel_rounding_mask_table
;      x     y
&29e2 f0 ea f8                                                      # Second byte is unused

; this_object_object_collision_y_flags
&29e5 00

; this_object_object_collision_x_flags
&29e6 00

; smallest_overlap_collision_flags_table
;      0     2     4     6
;     x+    y+    x-    y-
&29e7 20 ea 80 ea 10 ea 40                                          # Second, fourth and sixth bytes are unused

# Object type ranges
# ==================
# range                                                                                                     energy
# &00                               : player and allies                                                     &7f
# &01                               : hives                                                                 &3f
# &02                               : frogmen, slimes, nests and big fish                                   &7f
# &03                               : worm, piranha, wasp                                                   &07
# &04 (OBJECT_RANGE_PROJECTILES)    : active grenade, projectiles and hovering balls                        &3f
# &05                               : rolling robots, turrets and hovering robots                           &81
# &06 (OBJECT_RANGE_FLYING_ENEMIES) : clawed robots, Triax, maggot, gargoyle, imps and birds                &fd
# &07                               : lightning, mushroom balls, invisible debris, red drop and fireball    &3f
# &08 (OBJECT_RANGE_SCENERY)        : moving fireball, giant block, doors, bush, machinery                  &ff
# &09 (OBJECT_RANGE_EQUIPMENT)      : equipment and collectables                                            &7d

; object_type_ranges_table
;      0  1  2  3  4  5  6  7  8  9
&29ee 00 04 06 0f 12 1c 22 32 38 4a

; object_type_ranges_energy_table                                   # Used for initial energy
;      0  1  2  3  4  5  6  7  8  9                                 #      and to determine duration of explosions
&29f8 7f 3f 7f 07 3f 81 fd 3f ff 7d

; consider_npc_burrowing
&2a02 a5 17    LDA &17 ; this_object_surrounded_by_tiles            # Negative if NPC is surrounded by tiles
&2a04 10 0b    BPL &2a11 ; consider_starting_to_burrow
; is_burrowing                                                      # If the NPC is surrounded by tiles, i.e. burrowing,
&2a06 20 87 25 JSR &2587 ; rnd
&2a09 d0 13    BNE &2a1e ; reduce_velocities                        # 1 in 256 chance of removing NPC
; set_object_as_far_away
&2a0b 06 bf    ASL &bf ; this_object_removal_flags
&2a0d 38       SEC                                                  # Set &80 to regard object as far away, and thus remove
&2a0e 66 bf    ROR &bf ; this_object_removal_flags
; leave                                                             # Leave with negative if NPC removed
&2a10 60       RTS
; consider_starting_to_burrow
&2a11 a5 1b    LDA &1b ; this_object_tile_top_or_bottom_collision   # &80 set if object hit tiles above or below
&2a13 25 11    AND &11 ; this_object_state (behaviour and walking)  # &80 set if NPC wants to burrow
&2a15 10 f9    BPL &2a10 ; leave
&2a17 20 fa 13 JSR &13fa ; play_sound
&2a1a 23 e3 82 12                                                   # Play sound for NPC starting to burrow
; reduce_velocities_for_burrowing
&2a1e c6 42    DEC &42 ; this_object_acceleration_y                 # No gravity for NPCs starting to borrow
&2a20 a2 02    LDX #&02
; reduce_velocities_for_burrowing_loop                              # Loop through X = 2 for y, X = 0 for x
&2a22 b5 44    LDA &44,X ; this_object_previous_velocity_x
&2a24 20 cd 3b JSR &3bcd ; get_sign                                 # Returns &01 if positive, &ff if negative
&2a27 0a       ASL A                                                #         &02 if positive, &fe if negative
&2a28 95 43    STA &43,X ; this_object_velocity_x
&2a2a ca       DEX
&2a2b ca       DEX
&2a2c 10 f4    BPL &2a22 ; reduce_velocities_for_burrowing
&2a2e 20 aa 28 JSR &28aa ; set_this_object_position_from_previous_position
; add_velocities_to_position
&2a31 a2 02    LDX #&02                                             # X = 2 for y
&2a33 20 36 2a JSR &2a36 ; add_velocity_y_to_position               # X = 0 for x
; add_velocity_y_to_position
&2a36 b5 43    LDA &43,X ; this_object_velocity_x
; add_A_to_position                                                 # Called with X = 0 for x, X = 2 for y
&2a38 10 02    BPL &2a3c ; skip_underflow
&2a3a d6 53    DEC &53,X ; this_object_x                            # Will be corrected at &2a43 if no underflow
; skip_underflow
&2a3c 18       CLC
&2a3d 75 4f    ADC &4f,X ; this_object_x_fraction
&2a3f 95 4f    STA &4f,X ; this_object_x_fraction
&2a41 90 02    BCC &2a45 ; skip_overflow
&2a43 f6 53    INC &53,X ; this_object_x
; skip_overflow
&2a45 ca       DEX                                                  # X becomes zero for second pass
&2a46 ca       DEX
&2a47 60       RTS                                                  # Leave with negative if NPC burrowing

; calculate_this_object_maximum_x_y
&2a48 a2 02    LDX #&02
; calculate_this_object_maximum_x_y_loop                            # Loop through X = 2 for y, X = for x
&2a4a b5 3a    LDA &3a,X ; this_object_width
&2a4c 18       CLC
&2a4d 75 4f    ADC &4f,X ; this_object_x_fraction
&2a4f 95 47    STA &47,X ; this_object_maximum_x_fraction
&2a51 08       PHP ; overflow
&2a52 b5 53    LDA &53,X ; this_object_x
&2a54 69 00    ADC #&00
&2a56 95 48    STA &48,X ; this_object_maximum_x
&2a58 28       PLP ; overflow
&2a59 7e d9 29 ROR &29d9,X ; this_object_crosses_tile_in_x          # Set top bit if object crosses edge of tile
&2a5c ca       DEX
&2a5d ca       DEX
&2a5e f0 ea    BEQ &2a4a ; calculate_this_object_maximum_x_y_loop
&2a60 60       RTS

; to_check_for_collision_with_water_and_tiles
&2a61 4c e8 2e JMP &2ee8 ; check_for_collision_with_water_and_tiles

; check_for_collisions
; check_for_collision_with_other_objects
&2a64 a5 53    LDA &53 ; this_object_x
&2a66 18       CLC
&2a67 69 02    ADC #&02
&2a69 8d 86 2a STA &2a86 ; this_object_x_plus_two
&2a6c e9 02    SBC #&02
&2a6e 8d 82 2a STA &2a82 ; this_object_x_minus_two
&2a71 a4 55    LDY &55 ; this_object_y
&2a73 88       DEY
&2a74 88       DEY
&2a75 8c 8d 2a STY &2a8d ; this_object_y_minus_two
&2a78 a0 ef    LDY #&ef ; &f0 - 1                                   # Will become &f0 on first pass
&2a7a 38       SEC
; consider_next_other_object                                        # For each other object,
&2a7b c8       INY
&2a7c f0 e3    BEQ &2a61 ; to_check_for_collision_with_water_and_tiles
&2a7e b9 a1 07 LDA &07a1,Y ; objects_x - &f0                        # Quickly check if it is near this object
&2a81 c9 52    CMP #&52
#     actually CMP this_object_x_minus_two
&2a83 90 f6    BCC &2a7b ; consider_next_other_object               # Don't consider if other object is too far left
&2a85 c9 54    CMP #&54
#     actually CMP this_object_x_plus_two
&2a87 b0 f2    BCS &2a7b ; consider_next_other_object               # Don't consider if other object is too far right
&2a89 b9 c4 07 LDA &07c4,Y ; objects_y - &f0
&2a8c e9 53    SBC #&53
#     actually SBC this_object_y_minus_two
&2a8e c9 03    CMP #&03
&2a90 b0 e9    BCS &2a7b ; consider_next_other_object               # Don't consider if other object is too far above or below
&2a92 98       TYA
&2a93 69 10    ADC #&10
&2a95 e5 aa    SBC &aa ; this_object
&2a97 f0 e2    BEQ &2a7b ; consider_next_other_object               # Don't consider if the same object
&2a99 be 80 07 LDX &0780,Y ; objects_sprite - &f0
&2a9c bd 0c 5e LDA &5e0c,X ; sprites_width_and_horizontal_flip_table
&2a9f 85 9b    STA &9b ; other_object_width_and_flip
&2aa1 bd 89 5e LDA &5e89,X ; sprites_height_and_vertical_flip_table
&2aa4 85 9d    STA &9d ; other_object_height_and_flip
&2aa6 b9 c4 07 LDA &07c4,Y ; objects_y - &f0
&2aa9 85 98    STA &98 ; other_object_x_or_y
&2aab b9 b3 07 LDA &07b3,Y ; objects_y_fraction - &f0
&2aae a2 02    LDX #&02
; check_for_overlap_loop                                            # Loop through X = 2 for y, X = 0 for x
&2ab0 1d df 29 ORA &29df,X ; collision_pixel_rounding_table
&2ab3 38       SEC
&2ab4 f5 47    SBC &47,X ; this_object_maximum_x_fraction           # Calculate other_object_x - this_object_maximum_x
;                                                                   # i.e other_object_left - this_object_right, which is
;                                                                   # how much this object is overlapped on the right (plus)
&2ab6 08       PHP ; underflow
&2ab7 3d e2 29 AND &29e2,X ; collision_pixel_rounding_mask_table
&2aba 18       CLC
&2abb fd df 29 SBC &29df,X ; collision_pixel_rounding_table
&2abe 95 7b    STA &7b,X ; collision_plus_x_fraction
&2ac0 a5 98    LDA &98 ; other_object_x_or_y                        # y on first pass, x on second
&2ac2 e9 00    SBC #&00
&2ac4 28       PLP ; underflow
&2ac5 f5 48    SBC &48,X ; this_object_maximum_x
&2ac7 10 b2    BPL &2a7b ; consider_next_other_object               # If positive, no overlap
&2ac9 95 7c    STA &7c,X ; collision_plus_x
&2acb b5 9b    LDA &9b,X ; other_object_width_and_flip              # Only width is used; flip is in lowest bit
&2acd 1d df 29 ORA &29df,X ; collision_pixel_rounding_or_table
&2ad0 38       SEC
&2ad1 75 3a    ADC &3a,X ; this_object_width                        # Calculate combined widths of both objects
&2ad3 08       PHP ; overflow
&2ad4 1d df 29 ORA &29df,X ; collision_pixel_rounding_table
&2ad7 38       SEC
&2ad8 75 7b    ADC &7b,X ; collision_plus_x_fraction                # plus = other_left - this_right, i.e. calculate
;                                                                   # other_left - this_right + other_width + this_width
;                                                                   # = (other_left + other_width) - (this_right - this_width) 
&2ada 95 7f    STA &7f,X ; collision_minus_x_fraction               # i.e. other_object_right - this_object_left, which is
;                                                                   # how much this object is overlapped on the left (minus)
&2adc b5 7c    LDA &7c,X ; collision_plus_x
&2ade 69 00    ADC #&00
&2ae0 28       PLP ; overflow
&2ae1 69 00    ADC #&00
&2ae3 30 96    BMI &2a7b ; consider_next_other_object               # If negative, no overlap
&2ae5 95 80    STA &80,X ; collision_minus_x
&2ae7 15 7f    ORA &7f,X ; collision_minus_x_fraction
&2ae9 f0 90    BEQ &2a7b ; consider_next_other_object               # If objects are exactly touching, no overlap
&2aeb b9 a1 07 LDA &07a1,Y ; objects_x - &f0                        # Set values necessary for x calculation
&2aee 85 98    STA &98 ; other_object_x_or_y
&2af0 b9 90 07 LDA &0790,Y ; objects_x_fraction - &f0
&2af3 ca       DEX
&2af4 ca       DEX
&2af5 f0 b9    BEQ &2ab0 ; check_for_overlap_loop                   # then repeat for x
&2af7 a6 aa    LDX &aa ; this_object
&2af9 98       TYA ; other object
&2afa 18       CLC
&2afb 69 10    ADC #&10
&2afd d0 04    BNE &2b03 ; not_held_object_and_player               # Non-zero if other object isn't player
&2aff e4 dd    CPX &dd ; player_object_held
&2b01 f0 2c    BEQ &2b2f ; to_consider_next_other_object            # Don't consider collision between held object and player
; not_held_object_and_player
&2b03 2c b3 19 BIT &19b3 ; held_object_any_top_or_bottom_collision  # Negative if held object collided in previous update
&2b06 30 08    BMI &2b10 ; not_player_and_held_object               # If so, consider its collisions this time
&2b08 e0 00    CPX #&00
&2b0a d0 04    BNE &2b10 ; not_player_and_held_object
&2b0c c5 dd    CMP &dd ; player_object_held
&2b0e f0 1f    BEQ &2b2f ; to_consider_next_other_object            # Don't consider collision between player and held object
; not_player_and_held_object
&2b10 aa       TAX
&2b11 20 87 25 JSR &2587 ; rnd                                      # 1 in 2 chance of noting this object is touching other
&2b14 05 3b    ORA &3b ; this_object_touching                       # Negative if not touching a previously checked object
&2b16 10 02    BPL &2b1a ; skip_setting_this_object_touching
&2b18 86 3b    STX &3b ; this_object_touching                       # Set to positive to note this object touching other
; skip_setting_this_object_touching
&2b1a 20 87 25 JSR &2587 ; rnd                                      # 1 in 2 chance of noting other object is touching this
&2b1d 19 56 08 ORA &0856,Y ; objects_touching - &f0                 # Negative if not touching a previously checked object
&2b20 10 05    BPL &2b27 ; skip_setting_other_object_touching
&2b22 a5 aa    LDA &aa ; this_object
&2b24 99 56 08 STA &0856,Y ; objects_touching - &f0                 # Set to positive to note other object touching this
; skip_setting_other_object_touching
&2b27 be 70 07 LDX &0770,Y ; objects_type - &f0
&2b2a bd 54 03 LDA &0354,X ; object_types_flags_table               # &80 set if intangible
&2b2d 10 03    BPL &2b32 ; is_affected_by_collision                 # Don't consider collision if other object intangible
; to_consider_next_other_object
&2b2f 4c 7b 2a JMP &2a7b ; consider_next_other_object
; is_affected_by_collision
&2b32 29 07    AND #&07 ; OBJECT_TYPE_FLAG_WEIGHT_MASK
&2b34 85 90    STA &90 ; other_object_weight
&2b36 a6 41    LDX &41 ; this_object_type
&2b38 bd 54 03 LDA &0354,X ; object_types_flags_table               # &80 set if intangible
&2b3b 30 f2    BMI &2b2f ; to_consider_next_other_object            # Don't consider collision if this object intangible
&2b3d 29 07    AND #&07 ; OBJECT_TYPE_FLAG_WEIGHT_MASK
&2b3f 38       SEC
&2b40 e5 90    SBC &90 ; other_object_weight                        # Carry is left set by subtraction is this object heavier
&2b42 66 9e    ROR &9e ; weight_difference_sign                     # Set negative if this object is heavier than other object
&2b44 20 4c 32 JSR &324c ; invert_if_positive                       # Actually makes positive
&2b47 85 90    STA &90 ; weight_difference
&2b49 84 ab    STY &ab ; other_object_offset
&2b4b a2 06    LDX #&06
&2b4d a9 ff    LDA #&ff
&2b4f 85 9c    STA &9c ; largest_overlap
; find_smallest_overlap_loop                                        # Loop through X = 6, collision_minus_y (top)
&2b51 b5 7c    LDA &7c,X ; collision_plus_x                         #              X = 4, collision_minus_x (left)
&2b53 08       PHP ; sign                                           #              X = 2, collision_plus_y (bottom)
&2b54 4a       LSR A                                                #              X = 0, collision_plus_x (right)
&2b55 b5 7b    LDA &7b,X ; collision_plus_x_fraction
&2b57 6a       ROR A
&2b58 28       PLP ; sign
&2b59 20 56 32 JSR &3256 ; invert_if_negative
&2b5c c5 9c    CMP &9c ; smallest_overlap
&2b5e b0 04    BCS &2b64 ; not_smallest_overlap
&2b60 85 9c    STA &9c ; smallest_overlap
&2b62 8a       TXA
&2b63 a8       TAY                                                  # Y = X value for smallest overlap
; not_smallest_overlap                                              # which is the direction this object has been hit from
&2b64 ca       DEX
&2b65 ca       DEX
&2b66 10 e9    BPL &2b51 ; find_smallest_overlap_loop
&2b68 98       TYA
&2b69 29 02    AND #&02                                             # &00 if smallest overlap was in x, &02 if in y
&2b6b aa       TAX
&2b6c bd dc 29 LDA &29dc,X ; collision_velocity_direction_flags_table
&2b6f 85 9f    STA &9f ; collision_velocity_direction_flags
&2b71 b9 e7 29 LDA &29e7,Y ; smallest_overlap_collision_flags_table
&2b74 48       PHA ; collision flag for smallest overlap
&2b75 29 c0    AND #&c0                                             # &80 set if smallest overlap was collision_plus_y
;                                                                   #         i.e. collision to bottom
&2b77 8d e5 29 STA &29e5 ; this_object_object_collision_y_flags     # &40 set if smallest overlap was collision_minus_y
;                                                                   #         i.e. collision to top
&2b7a 68       PLA ; collision flag for smallest overlap
&2b7b 0a       ASL A
&2b7c 0a       ASL A                                                # &80 set if smallest overlap was collision_plus_x
;                                                                   #         i.e. collision to right
&2b7d 8d e6 29 STA &29e6 ; this_object_object_collision_x_flags     # &40 set if smallest overlap was collision_minus_x
;                                                                   #         i.e. collision to left
&2b80 b9 7c 00 LDA &007c,Y ; collision_plus_x
&2b83 08       PHP ; sign
&2b84 20 cd 3b JSR &3bcd ; get_sign                                 # Returns &01 if positive, &ff if negative
&2b87 0a       ASL A
&2b88 18       CLC
&2b89 75 43    ADC &43,X ; this_object_velocity_x                   # Set this object moving away from other object
&2b8b 95 43    STA &43,X ; this_object_velocity_x                   # by +2 or -2 units in largest overlap of x and y
&2b8d b9 7b 00 LDA &007b,Y ; collision_plus_x_fraction
&2b90 28       PLP ; sign
&2b91 20 38 2a JSR &2a38 ; add_A_to_position                        # Move this object away from other object so not overlapping
&2b94 20 48 2a JSR &2a48 ; calculate_this_object_maximum_x_y
&2b97 a4 ab    LDY &ab ; other_object_offset
&2b99 a6 43    LDX &43 ; this_object_velocity_x
&2b9b b9 f6 07 LDA &07f6,Y ; objects_velocity_x - &f0
&2b9e 20 b6 2b JSR &2bb6 ; apply_collision_to_objects_velocities
&2ba1 86 43    STX &43 ; this_object_velocity_x                     # Set new x velocity for this object
&2ba3 99 f6 07 STA &07f6,Y ; objects_velocity_x - &f0               # Set new x velocity for other object
&2ba6 a6 45    LDX &45 ; this_object_velocity_y
&2ba8 b9 06 08 LDA &0806,Y ; objects_velocity_y - &f0
&2bab 20 b6 2b JSR &2bb6 ; apply_collision_to_objects_velocities
&2bae 86 45    STX &45 ; this_object_velocity_y                     # Set new y velocity for this object
&2bb0 99 06 08 STA &0806,Y ; objects_velocity_y - &f0               # Set new y velocity for other object
&2bb3 4c 7b 2a JMP &2a7b ; consider_next_other_object

; apply_collision_to_objects_velocities                             # Called with A = other object x or y velocity,
;                                                                   #             X = this object x or y velocity
&2bb6 20 ee 2b JSR &2bee ; calculate_transfer_velocities            # Returns X = 0
&2bb9 20 c6 2b JSR &2bc6 ; apply_collision_to_object_velocity       # Apply lesser transfer velocity
&2bbc a2 01    LDX #&01
&2bbe 20 c6 2b JSR &2bc6 ; apply_collision_to_object_velocity       # Apply greater transfer velocity
&2bc1 a5 a1    LDA &a1 ; other_object_final_velocity
&2bc3 a6 a0    LDX &a0 ; this_object_final_velocity
&2bc5 60       RTS

; apply_collision_to_object_velocity                                # Called with X = 0 for other object
;                                                                   #             X = 1 for this object
&2bc6 b5 a2    LDA &a2,X ; transfer_velocities
&2bc8 c9 80    CMP #&80
&2bca 6a       ROR A                                                # Divide by two, keeping sign
&2bcb 46 9f    LSR &9f ; collision_velocity_direction_flags         # Carry set if this direction had smallest overlap
;                                                                   # i.e. is the direction this object has been hit from
&2bcd b0 05    BCS &2bd4 ; skip_doubling
&2bcf 75 a2    ADC &a2,X ; transfer_velocities                      # If so, double to move this object away from collision
&2bd1 20 7f 32 JSR &327f ; prevent_overflow
; skip_doubling
&2bd4 24 9e    BIT &9e ; weight_difference_sign                     # Negative if this object is heavier than other object
&2bd6 30 08    BMI &2be0 ; not_this_object
&2bd8 20 4c 32 JSR &324c ; invert_if_positive                       # If so, subtract rather than add transfer velocity
&2bdb ca       DEX                                                  # from other object if X was 1 (becomes 0)
&2bdc f0 02    BEQ &2be0 ; not_this_object
&2bde a2 01    LDX #&01                                             # or this object if X was 0 (becomes &ff, then 1)
; not_this_object
&2be0 48       PHA ; addition
&2be1 20 e5 2b JSR &2be5 ; add_to_final_velocity
&2be4 68       PLA ; addition
; add_to_final_velocity
&2be5 18       CLC
&2be6 75 a0    ADC &a0,X ; other_object_final_velocity
&2be8 20 7f 32 JSR &327f ; prevent_overflow
&2beb 95 a0    STA &a0,X ; other_object_final_velocity
&2bed 60       RTS

; calculate_transfer_velocities
&2bee 85 a1    STA &a1 ; this_object_initial_velocity
&2bf0 86 a0    STX &a0 ; other_object_initial_velocity
&2bf2 38       SEC
&2bf3 e5 a0    SBC &a0 ; other_object_initial_velocity              # Calculate difference between two input velocities
&2bf5 c9 80    CMP #&80
&2bf7 6a       ROR A                                                # Divide by two, keeping sign
&2bf8 50 02    BVC &2bfc ; skip_overflow
&2bfa 49 80    EOR #&80                                             # Keep sign if overflow
; skip_overflow
&2bfc 85 a3    STA &a3 ; half_velocity_difference                   # (this_velocity - other_velocity) / 2
&2bfe a6 90    LDX &90 ; weight_difference
&2c00 d0 01    BNE &2c03 ; skip_floor
&2c02 e8       INX                                                  # Halve if weights are the same
; skip_floor
; division_loop                                                     # Otherwise halve for each unit of difference in weight,
&2c03 c9 80    CMP #&80                                             # keeping the sign
&2c05 6a       ROR A
&2c06 ca       DEX
&2c07 d0 fa    BNE &2c03 ; division_loop
&2c09 c9 80    CMP #&80
&2c0b 69 00    ADC #&00                                             # Round up if negative
&2c0d 85 a2    STA &a2 ; transfer_velocities + 0 (lesser)           # Lesser velocity; applied to heavier object
&2c0f 38       SEC
&2c10 e5 a3    SBC &a3 ; half_velocity_difference
&2c12 85 a3    STA &a3 ; transfer_velocities + 1 (greater)          # Greater velocity; applied to lighter object
&2c14 60       RTS

; screen_scroll_deltas_table
;     x- x+ y- y+
&2c15 ff 01 ff 01

; screen_scroll_limits_table
;     x- x+ y- y+
&2c19 fe 02 fc 04                                                   # Can scroll +/- 2 tiles in x, +/- 4 tiles in y

; handle_scrolling_viewpoint                                        # Called with X = &0f for left, &10 for right,
&2c1d 8a       TXA                                                  #                 &11 for up,   &12 for down
&2c1e 38       SEC
&2c1f e9 0f    SBC #&0f                                             # left: &00, right: &01, up: &02, down: &03
&2c21 aa       TAX
&2c22 29 02    AND #&02
&2c24 a8       TAY                                                  # Y = &00 for x, &02 for y
&2c25 b9 c8 14 LDA &14c8,Y ; screen_scroll_offset_x                 # Get the current scroll offset for this direction
&2c28 dd 19 2c CMP &2c19,X ; screen_scroll_limits_table             # Can it be scrolled any more?
&2c2b f0 3c    BEQ &2c69 ; leave
&2c2d 18       CLC
&2c2e 7d 15 2c ADC &2c15,X ; screen_scroll_deltas_table             # Is so, apply delta
&2c31 99 c8 14 STA &14c8,Y ; screen_scroll_offset_x
&2c34 20 fa 13 JSR &13fa ; play_sound
&2c37 3d 04 11 d4                                                   # Play sound for scrolling viewpoint
&2c3b 60       RTS

; handle_remembering_position
&2c3c a5 15    LDA &15 ; this_object_energy
&2c3e c9 08    CMP #&08
&2c40 90 27    BCC &2c69 ; leave                                    # Can't remember position if player is too damaged
&2c42 ac 22 08 LDY &0822 ; player_teleports_remembered
&2c45 c0 04    CPY #&04
&2c47 b0 01    BCS &2c4a ; skip_ceiling
&2c49 c8       INY
; skip_ceiling
&2c4a 8c 22 08 STY &0822 ; player_teleports_remembered
&2c4d 20 88 22 JSR &2288 ; get_this_object_centre                   # Returns A = this_object_centre_x
&2c50 ac 21 08 LDY &0821 ; player_next_teleport
&2c53 99 23 08 STA &0823,Y ; player_teleports_x
&2c56 a5 8d    LDA &8d ; this_object_centre_y
&2c58 99 28 08 STA &0828,Y ; player_teleports_y
&2c5b 20 9d 14 JSR &149d ; play_middle_beep                         # Play middle beep for remembering position
&2c5e ee 21 08 INC &0821 ; player_next_teleport
; fix_player_next_teleport
&2c61 ad 21 08 LDA &0821 ; player_next_teleport
&2c64 29 03    AND #&03
&2c66 8d 21 08 STA &0821 ; player_next_teleport
; leave
&2c69 60       RTS

; handle_thrusting_right
&2c6a e6 40    INC &40 ; this_object_acceleration_x
&2c6c 60       RTS

; handle_thrusting_left
&2c6d c6 40    DEC &40 ; this_object_acceleration_x
&2c6f 60       RTS

; handle_thrusting_down
&2c70 e6 42    INC &42 ; this_object_acceleration_y
&2c72 60       RTS

; handle_thrusting_up
&2c73 c6 42    DEC &42 ; this_object_acceleration_y
&2c75 2c 8a 35 BIT &358a ; player_has_functioning_jetpack           # Negative if jetpack is functional
&2c78 10 1e    BPL &2c98 ; leave
; handle_lying_down
; set_object_jumping_or_flying
&2c7a a5 11    LDA &11 ; this_object_state (behaviour and walking)
&2c7c 09 0f    ORA #&0f                                             # Set low nibble to &0f to pretend the object hasn't
&2c7e 85 11    STA &11 ; this_object_state (behaviour and walking)  # been on a surface shallow enough to walk on, and so
&2c80 60       RTS                                                  # make the object jump or fly. This doesn't set the
;                                                                   # player's angle if lying down, which is set at &3876.

; handle_using_booster
&2c81 ad 8a 35 LDA &358a ; player_has_functioning_jetpack           # Negative if jetpack is functional
&2c84 2d 0e 08 AND &080e ; player_jetpack_booster_collected         # Negative if jetpack booster has been collected
&2c87 10 0f    BPL &2c98 ; leave
&2c89 a5 42    LDA &42 ; this_object_acceleration_y
&2c8b 30 07    BMI &2c94 ; skip_setting_flying                      # If not boosting upwards,
&2c8d a5 40    LDA &40 ; this_object_acceleration_x
&2c8f f0 03    BEQ &2c94 ; skip_setting_flying
&2c91 20 7a 2c JSR &2c7a ; set_object_jumping_or_flying             # Set playing flying, not walking
; skip_setting_flying
; double_accelerations
&2c94 06 40    ASL &40 ; this_object_acceleration_x                 # Double acceleration when using jetpack booster
&2c96 06 42    ASL &42 ; this_object_acceleration_y
; leave
&2c98 60       RTS

; handle_playing_whistle_two
&2c99 2c 17 08 BIT &0817 ; player_whistle_two_collected             # Negative if whistle two has been collected
&2c9c 10 fa    BPL &2c98 ; leave
; play_whistle_two_sound
&2c9e 20 fa 13 JSR &13fa ; play_sound
&2ca1 b0 24 b6 e2                                                   # Play sound for playing whistle two
&2ca5 a5 aa    LDA &aa ; this_object
&2ca7 8d d8 29 STA &29d8 ; whistle_two_activating_object            # Set to positive to indicate whistle two played
&2caa 10 08    BPL &2cb4 ; play_sound_for_whistle                   # Always branches

; handle_playing_whistle_one
&2cac 2c 16 08 BIT &0816 ; player_whistle_one_collected             # Negative if whistle one has been collected
&2caf 10 e7    BPL &2c98 ; leave
&2cb1 38       SEC
&2cb2 66 27    ROR &27 ; whistle_one_active                         # Set top bit to indicate whistle one being played
; play_sound_for_whistle
&2cb4 20 fa 13 JSR &13fa ; play_sound
&2cb7 b0 24 b6 b3                                                   # Play sound for playing whistle one or two
&2cbb 60       RTS

; get_waterline_for_x                                               # Called with A = x to find water level for
&2cbc a2 04    LDX #&04
; find_x_range_loop
&2cbe ca       DEX
&2cbf dd d2 14 CMP &14d2,X ; waterline_x_ranges_x_table
&2cc2 90 fa    BCC &2cbe ; find_x_range_loop
; get_waterline_for_range_x
&2cc4 bd 2e 08 LDA &082e,X ; waterline_x_ranges_y_fraction
&2cc7 8d d0 14 STA &14d0 ; waterline_y_fraction
&2cca 18       CLC
&2ccb ed 2f 08 SBC &082f ; waterline_x_ranges_y_fraction + 1 (Triax's lab)
&2cce bd 32 08 LDA &0832,X ; waterline_x_ranges_y
&2cd1 8d d1 14 STA &14d1 ; waterline_y
&2cd4 ed 33 08 SBC &0833 ; waterline_x_ranges_y + 1 (Triax's lab)   # Is the water level lower (i.e. greater y) than
&2cd7 a2 01    LDX #&01                                             # the water level for range 1 (Triax's lab)?
&2cd9 b0 e9    BCS &2cc4 ; get_waterline_for_range_x                # If so, return the water level for Triax's lab instead
&2cdb 60       RTS

; weapons_bullet_type_table
&2cdc 00 ; &00 (jetpack)         : (none)
&2cdd 18 ; &01 (pistol)          : OBJECT_PISTOL_BULLET
&2cde 13 ; &02 (icer)            : OBJECT_ICER_BULLET
&2cdf fb ; &03 (blaster)         : negative to discharge blaster; &fb = -5, so discharge for five frames
&2ce0 19 ; &04 (plasma gun)      : OBJECT_PLASMA_BALL
&2ce1 00 ; &05 (protection suit) : (none)

; handle_changing_weapon_or_transferring_energy
&2ce2 ca       DEX ; weapon number
&2ce3 2c 91 12 BIT &1291 ; action_keys_pressed + &26 (SHIFT)        # Negative if SHIFT pressed
&2ce6 30 1a    BMI &2d02 ; transfer_energy
; change_weapon
&2ce8 8a       TXA ; weapon number
&2ce9 f0 09    BEQ &2cf4 ; skip_checking_for_weapon_to_change       # Jetpack is always present
&2ceb c9 06    CMP #&06
&2ced b0 36    BCS &2d25 ; leave                                    # Don't change to bad weapon
&2cef bd 0e 08 LDA &080e,X ; player_weapons_collected               # Negative if weapon has been collected
&2cf2 10 32    BPL &2d26 ; leave                                    # Don't change to weapon that hasn't been collected
; skip_checking_for_weapon_to_change
&2cf4 8e 4d 08 STX &084d ; player_weapon
&2cf7 bd 54 08 LDA &0854,X ; player_weapons_energy_high
&2cfa 4a       LSR A
&2cfb 4a       LSR A
&2cfc 4a       LSR A
&2cfd 85 25    STA &25 ; energy_level_bells_remaining               # Cue one bell for each &800 of energy remaining
&2cff 4c a5 14 JMP &14a5 ; play_high_beep                           # Play high beep at start even if no energy
; transfer_energy
&2d02 8a       TXA
&2d03 f0 09    BEQ &2d0e ; skip_checking_for_weapon_to_transfer     # Jetpack is always present
&2d05 e0 06    CPX #&06
&2d07 b0 1d    BCS &2d26 ; leave                                    # Don't transfer from bad weapon
&2d09 bd 0e 08 LDA &080e,X ; player_weapons_collected               # Negative if weapon has been collected
&2d0c 10 18    BPL &2d26 ; leave                                    # Don't transfer from weapon that hasn't been collected
; skip_checking_for_weapon_to_transfer
&2d0e 20 27 2d JSR &2d27 ; consider_reducing_weapon_energy          # Returns carry clear if insufficient energy
&2d11 90 13    BCC &2d26 ; leave
&2d13 ae 4d 08 LDX &084d ; player_weapon
; increase_weapon_X_energy
&2d16 bd 54 08 LDA &0854,X ; player_weapons_energy_high
&2d19 18       CLC
&2d1a 69 08    ADC #&08                                             # Add &800 of energy to current weapon
&2d1c b0 03    BCS &2d21 ; skip_ceiling
&2d1e 9d 54 08 STA &0854,X ; player_weapons_energy_high
; skip_ceiling
&2d21 a9 01    LDA #&01                                             # Cue one bell to indicate transfer of energy
&2d23 85 25    STA &25 ; energy_level_bells_remaining
; leave
&2d25 60       RTS
; leave
&2d26 60       RTS

; consider_reducing_weapon_energy
&2d27 bd 54 08 LDA &0854,X ; player_weapons_energy_high
&2d2a 38       SEC
&2d2b e9 08    SBC #&08                                             # Remove &800 of energy from selected weapon
&2d2d 90 03    BCC &2d32 ; leave                                    # Leave with carry clear to indicate insufficient energy
&2d2f 9d 54 08 STA &0854,X ; player_weapons_energy_high
; leave
&2d32 60       RTS                                                  # Leave with carry set to indicate weapon energy reduced

; handle_firing
&2d33 20 0f 33 JSR &330f ; calculate_firing_vector_from_aiming_angle
&2d36 a5 dd    LDA &dd ; player_object_held                         # Positive if player is holding object
&2d38 8d d7 29 STA &29d7 ; player_object_fired                      # Set to positive to indicate object fired
&2d3b 10 54    BPL &2d91 ; leave                                    # Can't fire when holding object
&2d3d a9 05    LDA #&05
&2d3f 8d d6 29 STA &29d6 ; player_firing_cooldown
&2d42 ae 4d 08 LDX &084d ; player_weapon
&2d45 20 92 2d JSR &2d92 ; check_reliability                        # Returns carry clear if weapon is unreliable
&2d48 90 47    BCC &2d91 ; leave
&2d4a bd dc 2c LDA &2cdc,X ; weapons_bullet_type_table              # Zero for jetpack and protection suit
&2d4d f0 42    BEQ &2d91 ; leave
&2d4f 85 36    STA &36 ; player_blaster_timer
&2d51 30 23    BMI &2d76 ; skip_creating_bullet                     # Negative if blaster
&2d53 20 b8 33 JSR &33b8 ; create_child_object                      # Returns carry clear if object created, X = slot
&2d56 b0 39    BCS &2d91 ; leave
&2d58 ae 4d 08 LDX &084d ; player_weapon
&2d5b ca       DEX
&2d5c f0 11    BEQ &2d6f ; is_pistol
&2d5e ca       DEX
&2d5f f0 05    BEQ &2d66 ; is_icer
&2d61 20 ad 14 JSR &14ad ; play_low_beep                            # Play low beep for firing plasma gun; returns carry set
&2d64 b0 10    BCS &2d76 ; finished_playing_sound                   # Always branches
; is_icer
&2d66 20 fa 13 JSR &13fa ; play_sound                               # Returns carry set
&2d69 3d 04 3d d3                                                   # Play sound for firing icer
&2d6d b0 07    BCS &2d76 ; finished_playing_sound                   # Always branches
; is_pistrol
&2d6f 20 fa 13 JSR &13fa ; play_sound
&2d72 3d 04 3d 04                                                   # Play sound for firing pistol
; finished_playing_sound
; skip_firing_bullet
&2d76 ae 4d 08 LDX &084d ; player_weapon
; reduce_energy_of_weapon_X
&2d79 bd 4e 08 LDA &084e,X ; player_weapons_energy_low
&2d7c fd 5a 08 SBC &085a,X ; weapons_energy_cost
&2d7f 9d 4e 08 STA &084e,X ; player_weapons_energy_low
&2d82 bd 54 08 LDA &0854,X ; player_weapons_energy_high
&2d85 e9 00    SBC #&00
&2d87 b0 05    BCS &2d8e ; skip_underflow
&2d89 a9 00    LDA #&00
&2d8b 9d 4e 08 STA &084e,X ; player_weapons_energy_low
; skip_underflow
&2d8e 9d 54 08 STA &0854,X ; player_weapons_energy_high
; leave
&2d91 60       RTS

; check_reliability                                                 # Called with X = weapon slot
&2d92 bd 54 08 LDA &0854,X ; player_weapons_energy_high
&2d95 c9 04    CMP #&04                                             # If the weapon has more than &400 energy,
&2d97 b0 09    BCS &2da2 ; leave                                    # Leave with carry set to allow firing
&2d99 ca       DEX
&2d9a e0 ff    CPX #&ff                                             # Set carry if jetpack
&2d9c e8       INX                                                  # i.e. jetpack sometimes works even with no energy
&2d9d 6a       ROR A                                                # C ......21 -> 1 C......2
&2d9e 6a       ROR A                                                #            -> 2 1C......
&2d9f 6a       ROR A                                                #            -> . 21C.....
&2da0 c5 da    CMP &da ; rnd_state + 1                              # Energy      chance of working for weapon  for jetpack
;                                                                   # &300 - &3ff                       3 in 4       7 in 8
;                                                                   # &200 - &2ff                       1 in 2       5 in 8
;                                                                   # &100 - &1ff                       1 in 4       3 in 8
; leave                                                             # &000 - &0ff                            0       1 in 8
&2da2 60       RTS                                                  # Leave with carry clear if weapon was unreliable

; set_object_energy_from_type
&2da3 20 b0 2d JSR &2db0 ; get_range_for_object_type_A
&2da6 bd f8 29 LDA &29f8,X ; object_type_ranges_energy_table
&2da9 99 26 09 STA &0926,Y ; objects_energy
&2dac 60       RTS

; get_range_for_object_Y
&2dad b9 60 08 LDA &0860,Y ; objects_type
; get_range_for_object_type_A
&2db0 a2 0a    LDX #&0a
; get_range_for_object_type_A_loop
&2db2 ca       DEX
&2db3 dd ee 29 CMP &29ee,X ; object_type_ranges_table
&2db6 90 fa    BCC &2db2 ; get_range_for_object_type_A_loop
&2db8 60       RTS

# Envelopes
# =========
# Sound envelopes have stages, which apply the same delta to a volume or
# frequency a number of times, and loops, which repeat a set of stages.
#
# A byte with the top bit set indicates the start or end of a loop. The
# remaining bits specify the duration of the loop. A byte can end one loop
# and start another.
#
# A byte with the top bit clear indicates a stage. The remaining bits specify
# the duration of the stage. A second byte is the delta to apply at each step.

# &05 2 (v): -64x3, +4x1
# &09 4 (p): +6x5, (-2x12, +3x3)x2, -7x1, +1x2
# &11 4 (p): -7x1, +1x2, -1x2, -16x8
# &13 2 (p): +1x2, -1x2
# &17 2 (v): -16x8, -8x8
# &17 3 (v): -16x8, -8x8, -5x1
# &1b 2 (p): -5x1, (-95x3, -127x3)x7
# &1d 3 (p): (-95x3, -127x3)x7, (-93x2, -127x2)x3, +0x62
# &23 3 (v): (-93x2, -127x2)x3, +0x62, +6x1
# &29 2 (v): +0x62, +6x1
# &2d 4 (p): +12x10, +0x10, -2x120, +16x15
# &2f 2 (p): +0x10, -2x120
# &33 3 (v): +16x15, -12x15, (+2x4, -2x5)x120
# &37 3 (p): (+2x4, -2x5)x120, -16x8, -8x10
# &3d 3 (p): -16x8, -8x10, -4x12
# &3d 4 (b): -16x8, -8x10, -4x12, (+2x3, +1x3, +0x3, -1x3, -2x3)x18
# &43 6 (p): (+2x3, +1x3, +0x3, -1x3, -2x3)x18, +3x3, +1x3, +0x3, -1x12, +32x4
# &4f 5 (p): +3x3, +1x3, +0x3, -1x12, +32x4
# &57 7 (b): +32x4, +16x5, +8x5, -32x4, -16x5, -8x5, (-8x1, +1x8)x97
# &5d 4 (v): -32x4, -16x5, -8x5, (-8x1, +1x8)x97
# &63 3 (p): (-8x1, +1x8)x97, (+26x1, -2x13)x97, +24x1
# &68 3 (p): (+26x1, -2x13)x97, +24x1, +0x100
# &6e 3 (p): +24x1, +0x100, (+0x2, +64x1, +0x2, -68x1)x8
# &70 2 (v): +0x100, (+0x2, +64x1, +0x2, -68x1)x8
# &72 5 (v): (+0x2, +64x1, +0x2, -68x1)x8, (+0x3, +12x1, +0x3, -12x1)x16, +0x16, +47x1, +0x16
# &7b 5 (p): (+0x3, +12x1, +0x3, -12x1)x16, +0x16, +47x1, +0x16, -7x1
# &82 2 (p): -12x1, +0x16
# &85 2 (p): +0x16, +47x1
# &85 7 (p): +0x16, +47x1, +0x16, -7x1, +0x16, -15x1, (-16x16)x3
# &91 2 (v): (-16x16)x3, (+32x4, -3x2, -64x2)x7
# &94 4 (v): (+32x4, -3x2, -64x2)x7, +20x11, (-16x3, +16x3)x3, -68x3
# &9c 5 (v): +20x11, (-16x3, +16x3)x3, -68x3, +6x7, (-2x2, +2x4)x2
# &a6 5 (p): +6x7, (-2x2, +2x4)x2, -1x17, +20x11, +2x1
# &b0 4 (v): +20x11, +2x1, -125x2, +3x10
# &b6 2 (p): +3x10, +9x4
# &b6 3 (p): +3x10, +9x4, (+11x1, -32x1, +21x1)x8
# &ba 4 (p): (+11x1, -32x1, +21x1)x8, (+20x1, -20x1)x44, -1x16, -8x20
# &c1 3 (p): (+20x1, -20x1)x44, -1x16, -8x20
# &c7 1 (v): -1x16
# &c7 3 (v): -1x16, -8x20, +2x40
# &cb 2 (p): +2x40, +0x1
# &cd 2 (p): +0x1, ()x127                                            # Actually chatter_pitch x1, ()x127  (see &4925)
# &ff 5 (p): (+16x3, -16x3)x8, -64x3, +4x1, +6x5, (-2x12, +3x3)x2

; envelopes_table
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
&2db9 88 03 10 03 f0 80 03 c0 01 04 05 06 82 0c fe 03
&2dc9 03 80 01 f9 02 01 02 ff 08 f0 08 f8 01 fb 87 03
&2dd9 a1 03 81 80 83 02 a3 02 81 80 3e 00 01 06 0a 0c
&2de9 0a 00 78 fe 0f 10 0f f4 f8 04 02 05 fe 80 08 f0
&2df9 0a f8 0c fc 92 03 02 03 01 03 00 03 ff 03 fe 80
&2e09 03 03 03 01 03 00 0c ff 04 20 05 10 05 08 04 e0
&2e19 05 f0 05 f8 e1 01 f8 08 01 e1 01 1a 0d fe 80 01
&2e29 18 64 00 88 02 00 01 40 02 00 01 bc 90 03 00 01
&2e39 0c 03 00 01 f4 80 10 00 01 2f 10 00 01 f9 10 00
&2e49 01 f1 83 10 f0 87 04 20 02 fd 02 c0 80 0b 14 83
&2e59 03 f0 03 10 80 03 bc 07 06 82 02 fe 04 02 80 11
&2e69 ff 0b 14 01 02 02 83 0a 03 04 09 88 01 0b 01 e0
&2e79 01 15 ac 01 14 01 ec 80 10 ff 14 f8 28 02 01 00               # Falls through to use &ff and &a9 for envelope &cd

; suppress_physical_screen_scrolling
&2e89 ff

; check_for_top_and_bottom_tile_collisions                          # Called with X = tile x fraction DIV 32
&2e8a a9 00    LDA #&00                                             #             i.e. section of tile(s) to check
&2e8c 85 a2    STA &a2 ; top_tile_obstruction                       # Set to zero to indicate no obstruction by default
&2e8e 85 a3    STA &a3 ; bottom_tile_obstruction                    # Set to zero to indicate no obstruction by default
&2e90 b1 7c    LDA (&7c),Y ; top_tile_obstruction_data_address      # Determine where the top tile obstruction begins
&2e92 18       CLC
&2e93 65 7e    ADC &7e ; top_tile_obstruction_y_offset
&2e95 90 02    BCC &2e99 ; skip_floor
&2e97 a9 ff    LDA #&ff                                             # &ff indicates whole tile is clear or obstructed
; skip_floor_top
&2e99 38       SEC
&2e9a e5 84    SBC &84 ; this_object_top_y_fraction_rounded         # Calculate obstruction boundary - object_top
&2e9c 90 08    BCC &2ea6 ; no_or_full_obstruction_from_top_tile
&2e9e c5 3c    CMP &3c  this_object_height
&2ea0 90 02    BCC &2ea4 ; skip_ceiling_top                         # Use the bottom of the obstruction
&2ea2 a5 3c    LDA &3c  this_object_height                          # or the bottom of the object
; skip_ceiling_top
&2ea4 85 a2    STA &a2 ; top_tile_obstruction                       # This is the amount the tile obstructs the object if
;                                                                   # tile is flipped; will be inverted at &2ed2 if unflipped
; no_or_full_obstruction_from_top_tile
&2ea6 a5 3c    LDA &3c  this_object_height
&2ea8 2c db 29 BIT &29db ; this_object_crosses_tile_in_y            # Top bit set if object crosses tile edge
&2eab 10 21    BPL &2ece ; skip_checking_bottom_tile
; check_bottom_tile
&2ead b1 80    LDA (&80),Y ; bottom_tile_obstruction_data_address   # Determine where the bottom tile obstruction begins
&2eaf 18       CLC
&2eb0 65 82    ADC &82 ; bottom_tile_obstruction_y_offset
&2eb2 90 02    BCC &2eb6 ; skip_floor_bottom
&2eb4 a9 ff    LDA #&ff                                             # &ff indicates whole tile is clear or obstructed
; skip_floor_bottom
&2eb6 c5 85    CMP &85 ; this_object_bottom_y_fraction_rounded      # Consider obstruction boundary - object_bottom
&2eb8 90 02    BCC &2ebc ; skip_ceiling_bottom                      # Use the bottom of the obstruction
&2eba a5 85    LDA &85 ; this_object_bottom_y_fraction_rounded      # or the bottom of the object
; skip_ceiling_bottom
&2ebc 85 a3    STA &a3 ; bottom_tile_obstruction                    # This is the amount the tile obstructs the object if
;                                                                   # tile is flipped; will be inverted at &2ec4 if unflipped
&2ebe 24 83    BIT &83 ; bottom_tile_sprite_and_y_flip              # &80 set if flipped vertically
&2ec0 30 07    BMI &2ec9 ; skip_adjusting_bottom_tile               # Flipped tiles have the obstruction at the top
&2ec2 a5 85    LDA &85 ; this_object_bottom_y_fraction_rounded      # Unflipped tiles have the obstruction at the bottom
&2ec4 38       SEC
&2ec5 e5 a3    SBC &a3 ; bottom_tile_obstruction                    # Calculate object_bottom - obstruction_bottom
&2ec7 85 a3    STA &a3 ; bottom_tile_obstruction                    # i.e. the amount bottom tile obstructs the object
; skip_adjusting_bottom_tile
&2ec9 a9 00    LDA #&00
&2ecb 38       SEC
&2ecc e5 84    SBC &84 ; this_object_top_y_fraction_rounded
; skip_checking_bottom_tile
&2ece 24 7f    BIT &7f ; top_tile_sprite_and_y_flip                 # &80 set if flipped vertically
&2ed0 30 05    BMI &2ed7 ; skip_adjusting_top_tile                  # Flipped tiles have the obstruction at the top
&2ed2 38       SEC                                                  # Unflipped tiles have the obstruction at the bottom
&2ed3 e5 a2    SBC &a2 ; top_tile_obstruction                       # Calculate object_top - obstruction_top
&2ed5 85 a2    STA &a2 ; top_tile_obstruction                       # i.e. the amount top tile obstructs the object
; skip_adjusting_top_tile
&2ed7 a5 a2    LDA &a2 ; top_tile_obstruction
&2ed9 18       CLC
&2eda 65 a3    ADC &a3 ; bottom_tile_obstruction                    # Calculate total amount object is obstructed by tiles
&2edc 69 06    ADC #&06                                             # Add three quarters of a pixel, i.e. round up
&2ede 6a       ROR A                                                # Divide by minus four to match proportions at &3011
&2edf 4a       LSR A                                                # i.e. -1 for &04 fraction, -64 for a tile
&2ee0 29 fe    AND #&fe
&2ee2 49 ff    EOR #&ff
&2ee4 18       CLC
&2ee5 69 01    ADC #&01
&2ee7 60       RTS                                                  # Leaves with zero if no collision

; check_for_collision_with_water_and_tiles
&2ee8 a9 20    LDA #&20 ; TILE_PROCESSING_FLAG_COLLISION            # Only call tile update routines that want to know
&2eea 85 2d    STA &2d ; tile_processing_mode                       # about object collisions with tiles (everything other
;                                                                   # than engines and spaces with objects)
&2eec 46 17    LSR &17 ; this_object_surrounded_by_tiles            # Clear top bit to indicate not surrounded by default
; calculate_this_object_waterline                                   # Calculate the waterline relative to bottom of object
&2eee a5 49    LDA &49 ; this_object_maximum_y_fraction
&2ef0 38       SEC
&2ef1 ed d0 14 SBC &14d0 ; waterline_y_fraction                     # Bottom of object - waterline
&2ef4 aa       TAX
&2ef5 a5 4a    LDA &4a ; this_object_maximum_y
&2ef7 ed d1 14 SBC &14d1 ; waterline_y
&2efa f0 05    BEQ &2f01 ; set_this_object_waterline                # If there is more than a tile of difference, either
&2efc a2 00    LDX #&00                                             # Set to &00 to indicate object completely above water
&2efe 90 01    BCC &2f01 ; set_this_object_waterline
&2f00 ca       DEX ; &ff                                            # Set to &ff to indicate object is completely underwater
; set_this_object_waterline
&2f01 86 20    STX &20 ; this_object_waterline                      # Amount of object, in fractions, below world waterline
; consider_water_tiles
&2f03 a5 53    LDA &53 ; this_object_x
&2f05 85 95    STA &95 ; tile_x
&2f07 a5 55    LDA &55 ; this_object_y
&2f09 85 97    STA &97 ; tile_y
&2f0b a2 00    LDX #&00
&2f0d 86 01    STX &01 ; water_tile                                 # Set to zero to indicate not in water by default
&2f0f 20 53 24 JSR &2453 ; set_obstruction_data_variables_for_top_tile
&2f12 85 81    STA &81 ; bottom_tile_obstruction_data_address_high  # A = 1 from set_obstruction_data_variables_for_top_tile
&2f14 06 01    ASL &01 ; water_tile                                 # Top bit set by tile update routine if water tile
&2f16 90 01    BCC &2f19 ; not_top_water                            # If the top tile the object is in is a water tile,
&2f18 ca       DEX ; &ff                                            # Set to &ff to indicate object is completely underwater
; not_top_water
&2f19 2c db 29 BIT &29db ; this_object_crosses_tile_in_y            # Top bit set if object crosses tile edge
&2f1c 10 0f    BPL &2f2d ; skip_checking_bottom_tile
&2f1e e6 97    INC &97 ; tile_y
&2f20 20 50 24 JSR &2450 ; set_obstruction_data_variables_for_bottom_tile
&2f23 06 01    ASL &01 ; water_tile                                 # Top bit set by tile update routine if water tile
&2f25 90 12    BCC &2f39 ; not_bottom_water                         # If the bottom tile the object is in is a water tile,
&2f27 8a       TXA
&2f28 05 49    ORA &49 ; this_object_maximum_y_fraction             # use amount of object in bottom tile, or &ff from X
&2f2a aa       TAX                                                  # if top tile was a water tile too
&2f2b b0 0c    BCS &2f39 ; skip_using_top_for_bottom                # Always branches
; skip_checking_bottom_tile
&2f2d a5 7e    LDA &7e ; top_tile_obstruction_y_offset
&2f2f 85 82    STA &82 ; bottom_tile_obstruction_y_offset
&2f31 a5 7c    LDA &7c ; top_tile_obstruction_data_address_low
&2f33 85 80    STA &80 ; bottom_tile_obstruction_data_address_low
&2f35 a5 7f    LDA &7f ; top_tile_sprite_and_y_flip
&2f37 85 83    STA &83 ; bottom_tile_sprite_and_y_flip
; skip_using_top_for_bottom
&2f39 e4 20    CPX &20 ; this_object_waterline                      # Is the amount of object covered by water tiles more
&2f3b b0 02    BCS &2f3f ; skip_floor_waterline                     # than the amount covered by the world water level?
&2f3d a6 20    LDX &20 ; this_object_waterline                      # If not, use world water level
; skip_floor_waterline
&2f3f 86 8f    STX &8f ; this_object_tiles_or_world_waterline
&2f41 a4 38    LDY &38 ; this_object_weight
&2f43 d0 01    BNE &2f46 ; skip_floor_weight
&2f45 c8       INY
; skip_floor_weight
&2f46 a5 3c    LDA &3c  this_object_height
&2f48 4a       LSR A
&2f49 4a       LSR A
&2f4a 85 9a    STA &9a ; this_object_height_divided_by_four
&2f4c a2 04    LDX #&04
&2f4e a5 8f    LDA &8f ; this_object_tiles_or_world_waterline       # Non-zero if object is partially or wholly in water
&2f50 d0 01    BNE &2f53 ; set_this_object_in_water
&2f52 38       SEC                                                  # Carry set to indicate object is not in any water
; set_this_object_in_water
&2f53 66 1f    ROR &1f ; this_object_in_water                       # Clear top bit if object is in any water, set if not
&2f55 30 35    BMI &2f8c ; no_collision_with_water
; apply_buoyancy_loop                                               # A = amount object is under water, or &ff
&2f57 e5 9a    SBC &9a ; this_object_height_divided_by_four
&2f59 90 0e    BCC &2f69 ; finished_applying_buoyancy               # Shorter objects are more buoyant
&2f5b 88       DEY ; object weight
&2f5c 30 04    BMI &2f62 ; is_light                                 # Lighter objects are more buoyant
&2f5e d0 04    BNE &2f64 ; is_heavy
&2f60 c6 45    DEC &45 ; this_object_velocity_y
; is_light
&2f62 c6 45    DEC &45 ; this_object_velocity_y
; is_heavy
&2f64 ca       DEX
&2f65 d0 f0    BNE &2f57 ; apply_buoyancy_loop
&2f67 f0 1c    BEQ &2f85 ; skip_adding_water_particles              # Always branches
; finished_applying_buoyancy
&2f69 a5 45    LDA &45 ; this_object_velocity_y                     # If the object crosses the waterline
&2f6b 30 18    BMI &2f85 ; skip_adding_water_particles              # and is moving downwards, splash water particles
; add_water_particles_for_splash
&2f6d a9 c0    LDA #&c0                                             # Set water particles moving upwards
&2f6f 85 b5    STA &b5 ; angle
&2f71 20 88 22 JSR &2288 ; get_this_object_centre
&2f74 a5 49    LDA &49 ; this_object_maximum_y_fraction
&2f76 e5 8f    SBC &8f ; this_object_tiles_or_world_waterline       # Calculate absolute waterline
&2f78 85 89    STA &89 ; new_particles_y_fraction
&2f7a a5 4a    LDA &4a ; this_object_maximum_y
&2f7c e9 00    SBC #&00
&2f7e 85 8d    STA &8d ; new_particles_y
&2f80 a0 63    LDY #&63 ; PARTICLE_WATER
&2f82 20 8c 21 JSR &218c ; add_particle
; skip_adding_water_particles
&2f85 24 c5    BIT &c5 ; every_four_frames
&2f87 10 03    BPL &2f8c ; skip_dampening_velocities                # Every four frames,
&2f89 20 22 32 JSR &3222 ; dampen_this_object_velocities            # slow objects that are in water
; skip_dampening_velocities
; check_for_collision_with_tiles
&2f8c a5 51    LDA &51 ; this_object_y_fraction
&2f8e 29 f8    AND #&f8                                             # Round top of object to middle of pixel
&2f90 09 04    ORA #&04
&2f92 85 84    STA &84 ; this_object_top_y_fraction_rounded
&2f94 a5 49    LDA &49 ; this_object_maximum_y_fraction
&2f96 29 f8    AND #&f8                                             # Round bottom of object to middle of pixel
&2f98 09 04    ORA #&04
&2f9a 85 85    STA &85 ; this_object_bottom_y_fraction_rounded
&2f9c a5 4f    LDA &4f ; this_object_x_fraction                     # Consider the left edge of the object
&2f9e 4a       LSR A                                                # Convert to two pixel wide tile sections
&2f9f 4a       LSR A
&2fa0 4a       LSR A
&2fa1 4a       LSR A
&2fa2 4a       LSR A
&2fa3 a8       TAY
&2fa4 20 8a 2e JSR &2e8a ; check_for_top_and_bottom_tile_collisions # Check left edge of object against top and bottom tiles
&2fa7 85 77    STA &77 ; this_object_left_obstruction               # Negative if left edge of object overlaps obstruction 
&2fa9 a9 00    LDA #&00
&2fab 85 78    STA &78 ; this_object_top_obstruction                # Set to zero to indication no obstruction by default
&2fad 85 7a    STA &7a ; this_object_bottom_obstruction             # Set to zero to indication no obstruction by default
&2faf a5 3a    LDA &3a ; this_object_width
&2fb1 4a       LSR A
&2fb2 4a       LSR A
&2fb3 4a       LSR A
&2fb4 4a       LSR A
&2fb5 4a       LSR A
&2fb6 85 ab    STA &ab ; sections_to_check                          # For each two pixel wide section of the object,
; check_for_tile_collisions_on_top_and_bottom_edges_tile_loop
&2fb8 a5 84    LDA &84 ; this_object_top_y_fraction_rounded
&2fba 38       SEC
&2fbb e5 7e    SBC &7e ; top_tile_obstruction_y_offset
&2fbd b0 02    BCS &2fc1 ; skip_floor_top
&2fbf a9 00    LDA #&00
; skip_floor_top
&2fc1 85 a0    STA &a0 ; top_relative_y
&2fc3 a5 85    LDA &85 ; this_object_bottom_y_fraction_rounded
&2fc5 38       SEC
&2fc6 e5 82    SBC &82 ; bottom_tile_obstruction_y_offset
&2fc8 b0 02    BCS &2fcc ; skip_floor_bottom
&2fca a9 00    LDA #&00
; skip_floor_bottom
&2fcc 85 a1    STA &a1 ; bottom_relative_y
; check_for_tile_collisions_above_and_below_section_loop
&2fce b1 7c    LDA (&7c),Y ; top_tile_obstruction_data_address      # Check top edge of object against top tile
&2fd0 c5 a0    CMP &a0 ; top_relative_y
&2fd2 6a       ROR A
&2fd3 45 7f    EOR &7f ; top_tile_sprite_and_y_flip                 # &80 set if flipped vertically
&2fd5 30 02    BMI &2fd9 ; no_obstruction_from_top_tile
&2fd7 c6 78    DEC &78 ; this_object_top_obstruction                # Set negative to indicate top edge of object overlaps obstruction
; no_obstruction_from_top_tile
&2fd9 b1 80    LDA (&80),Y ; bottom_tile_obstruction_data_address   # Check bottom edge of object against bottom tile
&2fdb c5 a1    CMP &a1 ; bottom_relative_y
&2fdd 6a       ROR A
&2fde 45 83    EOR &83 ; bottom_tile_sprite_and_y_flip              # &80 set if flipped vertically
&2fe0 30 02    BMI &2fe4 ; no_obstruction_from_bottom_tile
&2fe2 c6 7a    DEC &7a ; this_object_bottom_obstruction             # Set negative to indicate bottom edge of object overlaps obstruction
; no_obstruction_from_bottom_tile
&2fe4 c6 ab    DEC &ab ; sections_to_check
&2fe6 30 27    BMI &300f ; finished_checking_for_tile_collisions_above_and_below
&2fe8 c8       INY                                                  # Move to next section?
&2fe9 c0 08    CPY #&08                                             # Does that cross a tile edge?
&2feb 90 e1    BCC &2fce ; check_for_tile_collisions_above_and_below_section_loop
&2fed e6 95    INC &95 ; tile_x                                     # If so, move to next tile horizontally
&2fef 20 50 24 JSR &2450 ; set_obstruction_data_variables_for_bottom_tile
&2ff2 2c db 29 BIT &29db ; this_object_crosses_tile_in_y            # Top bit set if object crosses tile edge
&2ff5 10 08    BPL &2fff ; use_bottom_tile_as_top_tile
&2ff7 c6 97    DEC &97 ; tile_y
&2ff9 20 53 24 JSR &2453 ; set_obstruction_data_variables_for_top_tile
&2ffc 4c 0b 30 JMP &300b ; skip_using_bottom_tile_as_top_tile
; use_bottom_tile_as_top_tile
&2fff a5 82    LDA &82 ; bottom_tile_obstruction_y_offset
&3001 85 7e    STA &7e ; top_tile_obstruction_y_offset
&3003 a5 80    LDA &80 ; bottom_tile_obstruction_data_address_low
&3005 85 7c    STA &7c ; top_tile_obstruction_data_address_low
&3007 a5 83    LDA &83 ; bottom_tile_sprite_and_y_flip
&3009 85 7f    STA &7f ; top_tile_sprite_and_y_flip
; skip_using_bottom_tile_as_top_tile
&300b a0 00    LDY #&00
&300d f0 a9    BEQ &2fb8 ; check_for_tile_collisions_on_top_and_bottom_edges_tile_loop # Always branches
; finished_checking_for_tile_collisions_above_and_below
&300f a5 7a    LDA &7a ; this_object_bottom_obstruction             # Multiply by eight to match proportions at &2ede
&3011 0a       ASL A                                                # Obstruction counts are already negative
&3012 0a       ASL A                                                # -8 for &20 fraction  section =  -1 for &04 fraction
&3013 0a       ASL A                                                #                              = -64 for a tile
&3014 85 7a    STA &7a ; this_object_bottom_obstruction
&3016 a5 78    LDA &78 ; this_object_top_obstruction
&3018 0a       ASL A
&3019 0a       ASL A
&301a 0a       ASL A
&301b 85 78    STA &78 ; this_object_top_obstruction
&301d 38       SEC
&301e e5 7a    SBC &7a ; this_object_bottom_obstruction             # Calculate top obstructions - bottom obstructions
&3020 85 b4    STA &b4 ; vector_x (sic)                             # Will be rotated at &3072
&3022 85 1a    STA &1a ; this_object_tile_collision_y_sign          # Positive if collision more to bottom, negative if top
&3024 49 ff    EOR #&ff
&3026 18       CLC
&3027 69 01    ADC #&01
&3029 85 18    STA &18 ; this_object_tile_collision_y_flags         # Set &80 if collision more to bottom
&302b a5 7a    LDA &7a ; this_object_bottom_obstruction
&302d 05 78    ORA &78 ; this_object_bottom_obstruction
&302f c9 01    CMP #&01
&3031 66 1b    ROR &1b ; this_object_tile_top_or_bottom_collision   # Set &80 if any collision along top or bottom edges
&3033 20 8a 2e JSR &2e8a ; check_for_top_and_bottom_tile_collisions # Check right edge of object against top and bottom tiles
&3036 85 79    STA &79 ; this_object_right_obstruction              # Negative if right edge of object overlaps obstruction
&3038 38       SEC
&3039 e5 77    SBC &77 ; this_object_left_obstruction               # Calculate right obstructions - left obstructions
&303b 85 b6    STA &b6 ; vector_y (sic)                             # Will be rotated at &3072
&303d 05 b4    ORA &b4 ; vector_x
&303f d0 2b    BNE &306c ; apply_tile_collision_to_position_and_velocity
; no_overall_obstruction                                            # Either entirely in space, or entirely in tiles
&3041 a5 77    LDA &77 ; this_object_left_obstruction
&3043 05 78    ORA &78 ; this_object_top_obstruction
&3045 f0 24    BEQ &306b ; leave
; halve_object_velocities_and_clear_obstructions                    # If entirely in tiles,
&3047 20 aa 28 JSR &28aa ; set_this_object_position_from_previous_position # Don't move object, but slow it down
&304a a5 43    LDA &43 ; this_object_velocity_x
&304c c9 80    CMP #&80
&304e 6a       ROR A                                                # Halve x velocity, keeping sign
&304f 85 43    STA &43 ; this_object_velocity_x
&3051 a5 45    LDA &45 ; this_object_velocity_y
&3053 c9 80    CMP #&80
&3055 6a       ROR A                                                # Halve y velocity, keeping sign
&3056 85 45    STA &45 ; this_object_velocity_y
&3058 20 48 2a JSR &2a48 ; calculate_this_object_maximum_x_y        # Returns X = &fe
&305b e8       INX ; &ff
&305c 86 18    STX &18 ; this_object_tile_collision_y_flags         # Set &80 to indicate collision to bottom
&305e 86 1a    STX &1a ; this_object_tile_collision_y_sign          # Set &80 to indicate collision to top
&3060 86 17    STX &17 ; this_object_surrounded_by_tiles            # Set &80 to indicate object surrounded by tiles
&3062 e8       INX ; &0
&3063 86 77    STX &77 ; this_object_left_obstruction               # Set all obstruction values to zero
&3065 86 79    STX &79 ; this_object_right_obstruction              # to suppress any change to velocities from collision
&3067 86 78    STX &78 ; this_object_top_obstruction
&3069 86 7a    STX &7a ; this_object_bottom_obstruction
; leave
&306b 60       RTS

# apply_tile_collision_to_position_and_velocity is called with:
#
# &77 ; this_object_left_obstruction negative if object hit an obstruction to left, zero if not
# &78 ; this_object_top_obstruction negative if object hit an obstruction to top, zero if not
# &79 ; this_object_right_obstruction negative if object hit an obstruction to right, zero if not
# &7a ; this_object_bottom_obstruction negative if object hit an obstruction to bottom, zero if not
#
# vector_y = this_object_right_obstruction - this_object_left_obstruction (sic)
# vector_x = this_object_top_obstruction - this_object_bottom_obstruction (sic)
#
# Obstruction values are negative if a collision occurred in that direction.
#
# If collision to bottom, x = +, y = 0 -> tile_collision_angle = &00
# If collision to left,   x = 0, y = + -> tile_collision_angle = &40
# If collision to top,    x = -, y = 0 -> tile_collision_angle = &80
# If collision to right,  x = 0, y = - -> tile_collision_angle = &c0

; apply_tile_collision_to_position_and_velocity
&306c 20 d4 22 JSR &22d4 ; calculate_angle_from_vector              # vector and thus tile_collision_angle is rotated by
&306f 85 1c    STA &1c ; tile_collision_angle                       # 90 degrees clockwise from where collision occurred
&3071 38       SEC
&3072 e9 60    SBC #&60 ; -135 degrees (i.e. anticlockwise)         # Rotate another 45 degrees to consider which of left,
;                                                                   # right, top or bottom object is most obstructed in
;                                                                   # top -> &20, right -> &60, bottom -> &a0, left -> &e0
&3074 29 c0    AND #&c0                                             # top -> &00, right -> &40, bottom -> &80, left -> &c0
&3076 0a       ASL A                                                # 84...... -> 8 4.......
&3077 2a       ROL A                                                #          -> 4 .......8
&3078 2a       ROL A                                                #          -> . ......84, i.e. shift &c0 into &03
;                                                                   # &00 -> Y = &00 if object most obstructed to top
;                                                                   # &40 -> Y = &01 if object most obstructed to right
;                                                                   # &80 -> Y = &02 if object most obstructed to bottom
;                                                                   # &c0 -> Y = &03 if object most obstructed to left
&3079 a8       TAY
&307a 49 02    EOR #&02                                             # Set X to the opposite direction
&307c aa       TAX                                                  # i.e. direction object needs to move away in
&307d b9 77 00 LDA &0077,Y ; this_object_left_obstruction           # Compare the obstruction counts for Y and X
#     actually LDA this_object_considered_obstruction
&3080 d5 77    CMP &77,X ; this_object_left_obstruction             # Find which of the pair has the obstruction
#     actually CMP this_object_opposite_obstruction
&3082 b0 02    BCS &3086 ; is_obstruction
&3084 b5 77    LDA &77,X ; this_object_left_obstruction             # If both are obstructed, use the least obstructed
#     actually LDA this_object_opposite_obstruction
; is_obstruction
&3086 c9 00    CMP #&00
&3088 d0 02    BNE &308c ; skip_floor
&308a a9 fe    LDA #&fe ; -2                                        # Always try to move out of obstruction
; skip_floor
&308c 0a       ASL A
&308d 0a       ASL A
&308e 48       PHA ; amount to move
; determine_which_vector_component_to_modify                        # Determine whether most obstructed component is in x or y
&308f 88       DEY                                                  # top -> &ff, right -> &00, bottom -> &01, left -> &02
&3090 98       TYA
&3091 29 01    AND #&01                                             # top -> &01, right -> &00, bottom -> &01, left -> &00
&3093 0a       ASL A                                                # top -> &02, right -> &00, bottom -> &02, left -> &00
&3094 aa       TAX                                                  # X = 0 if most obstructed component is in x, 2 if in y
&3095 68       PLA ; amount to move
&3096 e0 00    CPX #&00
&3098 d0 06    BNE &30a0 ; not_x
&309a 69 0f    ADC #&0f                                             # Round up to next pixel if x
&309c 90 02    BCC &30a0 ; skip_ceiling
&309e a9 fe    LDA #&fe ; -2
; skip_ceiling
; not_x
&30a0 c0 02    CPY #&02                                             # top -> &ff, right -> &00, bottom -> &01, left -> &02
&30a2 90 01    BCC &30a5 ; not_top_or_left
&30a4 c8       INY                                                  # top -> &00                               left -> &03
; not_top_or_left
&30a5 08       PHP ; amount to move sign                            # Negative set from CPY at &30a0 if right or down
&30a6 20 4c 32 JSR &324c ; invert_if_positive                       # Otherwise, make negative obstruction positive
&30a9 28       PLP ; amount to move sign
&30aa 20 38 2a JSR &2a38 ; add_A_to_position                        # Move object out of obstruction
&30ad 20 48 2a JSR &2a48 ; calculate_this_object_maximum_x_y
&30b0 20 cc 22 JSR &22cc ; calculate_angle_from_this_object_velocities
&30b3 85 1e    STA &1e ; this_object_pre_collision_velocity_angle   # Angle that the object was moving in
&30b5 a4 b7    LDY &b7 ; magnitude
&30b7 84 1d    STY &1d ; this_object_pre_collision_velocity_magnitude
&30b9 38       SEC
&30ba e5 1c    SBC &1c ; tile_collision_angle                       # e.g. if bottom collision, tile_collision_angle = &00,
;                                                                   #      &00 <= angle < &80 branches
;                                                                   #      i.e. any angle in bottom half of circle
&30bc 85 b5    STA &b5 ; angle
&30be 10 12    BPL &30d2 ; was_moving_towards_obstruction           # Branch if object was moving towards obstruction
; was_moving_away_from_obstruction
&30c0 e9 c0    SBC #&c0 ; 270 degrees
&30c2 20 56 32 JSR &3256 ; invert_if_negative
&30c5 c9 2a    CMP #&2a ; ~59 degrees
&30c7 b0 32    BCS &30fb ; leave                                    # Leave if not grazing obstruction at angle ~< 60 degrees
&30c9 a5 b7    LDA &b7 ; magnitude
&30cb c9 40    CMP #&40
&30cd 90 2c    BCC &30fb ; leave                                    # If object is moving fast, don't move it but slow it down
&30cf 4c 47 30 JMP &3047 ; halve_object_velocities_and_clear_obstructions
; was_moving_towards_obstruction                                    # If moving towards obstruction, bounce off it
&30d2 38       SEC                                                  # A = velocity_angle - tile_collision_angle
;                                                                   #   = &40 if hitting obstruction head on
&30d3 e9 3f    SBC #&3f ; ~90 degrees                               # Get angle relative to head on collision
&30d5 20 75 32 JSR &3275 ; divide_by_eight
&30d8 65 b5    ADC &b5 ; angle
&30da 49 ff    EOR #&ff
&30dc 38       SEC
&30dd 65 1c    ADC &1c ; tile_collision_angle                       # Bounce off obstruction at reduced angle
&30df 85 b5    STA &b5 ; angle
&30e1 a5 b7    LDA &b7 ; magnitude
&30e3 c9 20    CMP #&20
&30e5 90 02    BCC &30e9 ; skip_ceiling
&30e7 a9 20    LDA #&20                                             # Limit bounce velocity
; skip_ceiling
&30e9 e9 02    SBC #&02                                             # Lose some velocity when bouncing off obstruction
&30eb b0 02    BCS &30ef ; skip_floor
&30ed a9 00    LDA #&00
; skip_floor
&30ef 20 35 32 JSR &3235 ; calculate_seven_eighths                  # Lose more velocity
&30f2 20 57 23 JSR &2357 ; calculate_vector_from_magnitude_and_angle
&30f5 85 45    STA &45 ; this_object_velocity_y                     # Use bounce velocity as object's new velocity
&30f7 a5 b4    LDA &b4 ; vector_x
&30f9 85 43    STA &43 ; this_object_velocity_x
; leave
&30fb 60       RTS

; update_player_aiming_angle
&30fc a5 d3    LDA &d3 ; player_aiming_angle_acceleration
&30fe f0 08    BEQ &3108 ; skip_acceleration                        # Set angle velocity to zero if not accelerating
&3100 18       CLC
&3101 65 33    ADC &33 ; player_aiming_angle_velocity
&3103 a0 10    LDY #&10 ; range
&3105 20 5e 32 JSR &325e ; keep_within_range                        # Keep velocity within -&10 <= angle <= &10
; skip_acceleration
&3108 85 33    STA &33 ; player_aiming_angle_velocity
&310a 18       CLC
&310b 65 32    ADC &32 ; player_aiming_angle_without_flip
&310d a0 3f    LDY #&3f ; range
&310f 20 5e 32 JSR &325e ; keep_within_range                        # Keep angle within -&3f <= angle <= &3f
&3112 85 32    STA &32 ; player_aiming_angle_without_flip           #                   -90 degrees < angle < 90 degrees
&3114 24 37    BIT &37 ; this_object_x_flip
&3116 10 05    BPL &311d ; not_flipped
&3118 49 7f    EOR #&7f
&311a 18       CLC
&311b 69 01    ADC #&01
; not_flipped
&311d 85 34    STA &34 ; player_aiming_angle_with_flip
&311f 60       RTS

; handle_centring_aim
&3120 a9 00    LDA #&00
&3122 85 32    STA &32 ; player_aiming_angle_without_flip
&3124 85 33    STA &33 ; player_aiming_angle_velocity
; handle_raising_aim
&3126 c6 d3    DEC &d3 ; player_aiming_angle_acceleration
&3128 2c e6 d3 BIT &d3e6 ; (nop)
; handle_lowering_aim
#3129          INC &d3 ; player_aiming_angle_acceleration
; create_aim_particle
&312b 20 0f 33 JSR &330f ; calculate_firing_vector_from_aiming_angle
&312e 20 36 31 JSR &3136 ; flip_this_object_horizontally            # Put aiming particles on same side as player's face
&3131 a0 42    LDY #&42 ; PARTICLE_AIM
&3133 20 8c 21 JSR &218c ; add_particle
; flip_this_object_horizontally
&3136 a5 37    LDA &37 ; this_object_x_flip
&3138 49 80    EOR #&80
&313a 85 37    STA &37 ; this_object_x_flip
&313c 60       RTS

; previous_viewpoint_object
&313d 00                                                            # Unused in standard version

; permute_copy_protection_seed                                      # Called with Y = &44
&313e b9 58 49 LDA &4958,Y ; copy_protection_seed - &44
&3141 20 78 32 JSR &3278 ; divide_by_four                           # .4.1.421
&3144 29 15    AND #&15                                             #          -> ...4.1.4
&3146 79 58 49 ADC &4958,Y ; copy_protection_seed - &44             #          -> .4xxxxxx
&3149 60       RTS                                                  # Leave with permuted_seed

# Transporter destinations
# ========================
# &00 : (&62, &c7) used by transporter at (&a0, &63), and transporter at (&29, &c6) depending on switch
# &01 : (&ad, &62) used by transporter at (&9c, &66), and transporter at (&29, &c6) depending on switch
# &02 : (&2a, &cd) used by transporter at (&61, &d9)
# &03 : (&0b, &0b) (unused)
# &04 : (&9d, &58) used by transporter at (&b2, &80)
# &05 : (&af, &62) used by transporter at (&ab, &69)
# &06 : (&9e, &69) used by transporter at (&94, &5c)
# &07 : (&45, &57) used by transporter at (&89, &71)
# &08 : (&89, &71) used by transporter at (&45, &57)
# &09 : (&9d, &3c) used by transporter at (&9d, &49)
# &0a : (&b5, &66) used by transporter at (&74, &54)
# &0b : (&a2, &63) used by transporter at (&60, &98)
# &0c : (&72, &54) used by transporter at (&a2, &58) depending on switch
# &0d : (&a7, &80) used by transporter at (&a2, &58) depending on switch
# &0e : (&9f, &49) used by transporter at (&9d, &58) depending on switch
# &0f : (&b0, &80) used by transporter at (&9d, &58) depending on switch

; transporter_destinations_x_table
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
&314a 62 ad 2a 0b 9d af 9e 45 89 9d b5 a2 72 a7 9f b0

; transporter_destinations_y_table
&315a c7 62 cd 0b 58 62 69 57 71 3c 66 63 54 80 49 80

; player_needs_to_retrieve_object
&316a ff                                                            # Positive if player needs to retrieve object

# NPC stimuli types
# =================
# &00 : OBJECT_RED_MAGENTA_IMP
# &01 : OBJECT_RED_YELLOW_IMP
# &02 : OBJECT_BLUE_CYAN_IMP
# &03 : OBJECT_CYAN_YELLOW_IMP
# &04 : OBJECT_RED_CYAN_IMP
# &05 : OBJECT_MAGENTA_ROLLING_ROBOT, OBJECT_RED_ROLLING_ROBOT and OBJECT_BLUE_ROLLING_ROBOT
# &06 : OBJECT_FLUFFY
# &07 : OBJECT_ACTIVE_CHATTER and OBJECT_INACTIVE_CHATTER
# &08 : OBJECT_GREEN_SLIME
# &09 : OBJECT_RED_FROGMAN

; npc_stimuli_types_phobia_table                                    # Objects that the NPCs avoid
&316b 81 ; &00 (red/magenta imps) : OBJECT_ACTIVE_CHATTER | &80 (player)
&316c 81 ; &01 (red/yellow imps)  : OBJECT_ACTIVE_CHATTER | &80 (player)
&316d ba ; &02 (blue/cyan imps)   : OBJECT_GIANT_BLOCK | &80 (player)
&316e cd ; &03 (cyan/yellow imps) : OBJECT_FULL_FLASK | &80 (player)
&316f 81 ; &04 (red/cyan imps)    : OBJECT_ACTIVE_CHATTER | &80 (player)
&3170 a9 ; &05 (rolling robots)   : OBJECT_RED_MAGENTA_IMP | &80 (player)
&3171 37 ; &06 (fluffy)           : OBJECT_FIREBALL
&3172 37 ; &07 (chatter)          : OBJECT_FIREBALL
&3173 8a ; &08 (green slime)      : OBJECT_GREEN_SLIME | &80 (player)
&3174 0f ; &09 (red frogman)      : OBJECT_WORM

; npc_stimuli_types_target_table                                    # Objects that the NPCs target
&3175 29 ; &00 (red/magenta imps) : OBJECT_RED_MAGENTA_IMP
&3176 29 ; &01 (red/yellow imps)  : OBJECT_RED_MAGENTA_IMP
&3177 55 ; &02 (blue/cyan imps)   : OBJECT_CORONIUM_BOULDER
&3178 37 ; &03 (cyan/yellow imps) : OBJECT_FIREBALL
&3179 86 ; &04 (red/cyan imps)    : OBJECT_RANGE_FLYING_ENEMIES
&317a 86 ; &05 (rolling robots)   : OBJECT_RANGE_FLYING_ENEMIES
&317b 86 ; &06 (fluffy)           : OBJECT_RANGE_FLYING_ENEMIES
&317c 86 ; &07 (chatter)          : OBJECT_RANGE_FLYING_ENEMIES
&317d 86 ; &08 (green slime)      : OBJECT_RANGE_FLYING_ENEMIES
&317e 0f ; &09 (red frogman)      : OBJECT_WORM

; npc_stimuli_types_food_table                                      # Objects that the NPCs absorb
&317f 11 ; &00 (red/magenta imps) : OBJECT_WASP
&3180 2f ; &01 (red/yellow imps)  : OBJECT_WHITE_YELLOW_BIRD
&3181 10 ; &02 (blue/cyan imps)   : OBJECT_PIRANHA
&3182 34 ; &03 (cyan/yellow imps) : OBJECT_BLUE_MUSHROOM_BALL
&3183 30 ; &04 (red/cyan imps)    : OBJECT_RED_MAGENTA_BIRD
&3184 35 ; &05 (rolling robots)   : OBJECT_INVISIBLE_DEBRIS
&3185 34 ; &06 (fluffy)           : OBJECT_BLUE_MUSHROOM_BALL
&3186 58 ; &07 (chatter)          : OBJECT_CORONIUM_CRYSTAL
&3187 58 ; &08 (green slime)      : OBJECT_CORONIUM_CRYSTAL
&3188 0f ; &09 (red frogman)      : OBJECT_WORM

; npc_stimuli_types_home_table                                      # Objects that the NPCs seek as home
&3189 40 ; &00 (red/magenta imps) : OBJECT_BUSH
&318a 40 ; &01 (red/yellow imps)  : OBJECT_BUSH
&318b 40 ; &02 (blue/cyan imps)   : OBJECT_BUSH
&318c 40 ; &03 (cyan/yellow imps) : OBJECT_BUSH
&318d 40 ; &04 (red/cyan imps)    : OBJECT_BUSH
&318e 88 ; &05 (rolling robots)   : OBJECT_RANGE_SCENERY, also OBJECT_INVISIBLE_FROGMAN | &80 (player)
&318f 00 ; &06 (fluffy)           : OBJECT_PLAYER
&3190 00 ; &07 (chatter)          : OBJECT_PLAYER
&3191 37 ; &08 (green slime)      : OBJECT_FIREBALL
&3192 3a ; &09 (red frogman)      : OBJECT_GIANT_BLOCK

; npc_stimuli_types_responses_table                                 # If a bit is set, stimulus makes NPC mood more positive
;                                                                   # Otherwise, stimulus makes NPC mood more negative
;                                   80 40 20 10 08 04 02    time    flood   damage  explode eating  player  phobia
&3193 cc ; &00 (red/magenta imps) : 01 02 .. .. 10 20 ..    +       +       -       -       +       +       -
&3194 f6 ; &01 (red/yellow imps)  : 01 02 04 08 .. 20 40    +       +       +       +       -       +       +
&3195 8c ; &02 (blue/cyan imps)   : 01 .. .. .. 10 20 ..    +       -       -       -       +       +       -
&3196 72 ; &03 (cyan/yellow imps) : .. 02 04 08 .. .. 40    -       +       +       +       -       -       +
&3197 f2 ; &04 (red/cyan imps)    : 01 02 04 08 .. .. 40    +       +       +       +       -       -       +
&3198 76 ; &05 (robots)           : .. 02 04 08 .. 20 40    -       +       +       +       -       +       +
&3199 88 ; &06 (fluffy)           : 01 .. .. .. 10 .. ..    +       -       -       -       +       -       -
&319a a4 ; &07 (chatter)          : 01 .. 04 .. .. 20 ..    +       -       +       -       -       +       -
&319b 1a ; &08 (green slime)      : .. .. .. 08 10 .. 40    -       -       -       +       +       -       +
&319c 0e ; &09 (red frogman)      : .. .. .. .. 10 20 40    -       -       -       -       +       +       +

; imp_types_projectile_table
&319d 34 ; &00 (red/magenta imps) : OBJECT_BLUE_MUSHROOM_BALL
&319e 17 ; &01 (red/yellow imps)  : OBJECT_RED_BULLET
&319f 58 ; &02 (blue/cyan imps)   : OBJECT_CORONIUM_CRYSTAL
&31a0 33 ; &03 (cyan/yellow imps) : OBJECT_RED_MUSHROOM_BALL
&31a1 33 ; &04 (red/cyan imps)    : OBJECT_RED_MUSHROOM_BALL

; imp_types_minimum_energy_table
&31a2 0a ; &00 (red/magenta imps)                                   # Red/magenta imps have a minimum energy of 10
&31a3 50 ; &01 (red/yellow imps)                                    # Red/yellow imps have a minimum energy of 80
&31a4 46 ; &02 (blue/cyan imps)                                     # Blue/cyan imps have a minimum energy of 70
&31a5 14 ; &03 (cyan/yellow imps)                                   # Cyan/yellow imps have a minimum energy of 20
&31a6 13 ; &04 (red/cyan imps)                                      # Red/cyan imps have a minimum energy of 19

; imp_types_gift_table
&31a7 4b ; &00 (red/magenta imps) : OBJECT_POWER_POD                # x  4 (&083a)
&31a8 12 ; &01 (red/yellow imps)  : OBJECT_ACTIVE_GRENADE           # x 10 (&083b)
&31a9 47 ; &02 (blue/cyan imps)   : OBJECT_ALIEN_WEAPON             # x  1 (&083c)
&31aa 47 ; &03 (cyan/yellow imps) : OBJECT_ALIEN_WEAPON             # x  1 (&083d)
&31ab 12 ; &04 (red/cyan imps)    : OBJECT_ACTIVE_GRENADE           # x 10 (&083e)

; consider_toggling_lock                                            # Called with A = &00 for transporter beam, &40 for door
&31ac 69 40    ADC #&40                                             # Set overflow if transporter beam
&31ae a5 bc    LDA &bc ; this_object_data
&31b0 70 04    BVS &31b6 ; not_transporter_beam
; is_transporter_beam
&31b2 69 60    ADC #&60                                             # 0 1 2 3 4 5 6 7 -> 6 7 8 9 a b c d
&31b4 4a       LSR A                                                #                 -> 3 3 4 4 5 5 6 6
&31b5 b8       CLV                                                  # i.e. use keys 3 - 6 for transporter beams
; not_transporter_beam
&31b6 4a       LSR A                                                # If door, .4218421 -> ......421
&31b7 4a       LSR A                                                # i.e. door colour sets key needed
&31b8 4a       LSR A
&31b9 4a       LSR A
&31ba aa       TAX
&31bb bd 06 08 LDA &0806,X ; player_keys_collected                  # Negative if key has been collected
&31be 10 17    BPL &31d7 ; leave
&31c0 a5 bc    LDA &bc ; this_object_data
&31c2 49 01    EOR #&01 ; DOOR_FLAG_LOCKED                          # Toggle &01 to lock or unlock door or transporter beam
&31c4 50 08    BVC &31ce ; not_door
; is_door
&31c6 4a       LSR A                                                # Set carry if door is now locked
&31c7 29 fe    AND #&fe ; DOOR_FLAG_MOVING >> 1                     # Clear what will become &04 to stop it moving if locked
&31c9 b0 02    BCS &31cd ; not_unlocked
&31cb 09 01    ORA #&01 ; DOOR_FLAG_OPENING >> 1                    # Set what will become &02 to open unlocked door
; not_unlocked
&31cd 2a       ROL A
; not_door
&31ce 85 bc    STA &bc ; this_object_data
&31d0 20 fa 13 JSR &13fa ; play_sound
&31d3 94 64 ba c4                                                   # Play sound for locking or unlocking door or beam
; leave
&31d7 60       RTS

; move_towards_target                                               # Called with A = magnitude, Y = maximum acceleration
&31d8 a2 ff    LDX #&ff
; move_towards_target_with_probability_X                            # Called with A = magnitude, X = probability, Y = maximum acceleration
&31da e4 da    CPX &da ; rnd_state + 1
&31dc 90 17    BCC &31f5 ; leave
&31de 84 9c    STY &9c ; maximum_acceleration
&31e0 48       PHA ; magnitude
&31e1 20 8e 28 JSR &288e ; set_target_object_x_y_from_this_object_tx_ty # Returns X = &10 (OBJECT_SLOT_TARGET)
&31e4 68       PLA ; magnitude
&31e5 20 47 33 JSR &3347 ; use_vector_between_object_centres        # Use vector from this object to tx, ty as desired velocity
&31e8 a2 02    LDX #&02
; add_velocities_loop                                               # Loop through X = 2 for y, X = 0 for x
&31ea a0 00    LDY #&00 ; no weight
&31ec b5 b4    LDA &b4,X ; vector_x
&31ee 20 f6 31 JSR &31f6 ; apply_weighted_acceleration_to_this_object_velocity
&31f1 ca       DEX
&31f2 ca       DEX
&31f3 f0 f5    BEQ &31ea ; add_velocities_loop
; leave
&31f5 60       RTS

; apply_weighted_acceleration_to_this_object_velocity               # Called with A = desired velocity, Y = weight factor
&31f6 38       SEC                                                  #             &9c = maximum acceleration
&31f7 f5 43    SBC &43,X ; this_object_velocity_x
&31f9 20 01 32 JSR &3201 ; apply_weight_and_limit_to_acceleration   # Returns carry clear
&31fc 75 43    ADC &43,X ; this_object_velocity_x                   # Move velocity towards desired velocity
&31fe 95 43    STA &43,X ; this_object_velocity_x                   # but not by more than +/- maximum_acceleration
&3200 60       RTS

; apply_weight_and_limit_to_acceleration                            # Called with A = acceleration, Y = weight factor
&3201 20 7f 32 JSR &327f ; prevent_overflow                         #             &9c = maximum acceleration
&3204 08       PHP ; sign
&3205 20 56 32 JSR &3256 ; invert_if_negative
; apply_weight_loop                                                 # Divide by 2 ** weight factor
&3208 4a       LSR A
&3209 88       DEY
&320a 10 fc    BPL &3208 ; apply_weight_loop
&320c 2a       ROL A                                                # Undo last division
&320d c5 9c    CMP &9c ; maximum_acceleration
&320f 90 02    BCC &3213 ; skip_limit
&3211 a5 9c    LDA &9c ; maximum_acceleration                       # Limit acceleration to +/- maximum_acceleration
; skip_limit
&3213 28       PLP ; sign
&3214 20 56 32 JSR &3256 ; invert_if_negative
&3217 18       CLC
&3218 60       RTS

; dampen_this_object_velocities_four_times                          # Unused entry point
&3219 20 22 32 JSR &3222 ; dampen_this_object_velocities
&321c 20 22 32 JSR &3222 ; dampen_this_object_velocities
; dampen_this_object_velocities_twice
&321f 20 22 32 JSR &3222 ; dampen_this_object_velocities
; dampen_this_object_velocities
&3222 20 2d 32 JSR &322d ; dampen_this_object_velocity_x
; dampen_this_object_velocity_y
&3225 a5 45    LDA &45 ; this_object_velocity_y
&3227 20 35 32 JSR &3235 ; calculate_seven_eighths
&322a 85 45    STA &45 ; this_object_velocity_y
&322c 60       RTS
; dampen_this_object_velocity_x
&322d a5 43    LDA &43 ; this_object_velocity_x
&322f 20 35 32 JSR &3235 ; calculate_seven_eighths
&3232 85 43    STA &43 ; this_object_velocity_x
&3234 60       RTS

; calculate_seven_eighths
&3235 85 9c    STA &9c ; value
&3237 20 54 32 JSR &3254 ; make_positive
&323a 69 07    ADC #&07
&323c 4a       LSR A
&323d 4a       LSR A
&323e 4a       LSR A
&323f 24 9c    BIT &9c ; value
&3241 20 56 32 JSR &3256 ; invert_if_negative
&3244 85 9b    STA &9b ; one_eighth
&3246 a5 9c    LDA &9c ; value
&3248 38       SEC
&3249 e5 9b    SBC &9b ; one_eighth
&324b 60       RTS                                                  # Leave with seven eighths of initial value

; invert_if_positive
&324c 18       CLC
&324d 30 04    BMI &3253 ; leave                                    # Not necessarily the sign of A
&324f 49 ff    EOR #&ff
&3251 69 01    ADC #&01
; leave
&3253 60       RTS

; make_positive
&3254 c9 00    CMP #&00
; invert_if_negative
&3256 18       CLC
&3257 10 04    BPL &325d ; leave                                    # Not necessarily the sign of A
&3259 49 ff    EOR #&ff
&325b 69 01    ADC #&01
; leave
&325d 60       RTS

; keep_with_range                                                   # Called with A = centre value, Y = range
&325e 84 9c    STY &9c ; range
&3260 85 9d    STA &9d ; value
&3262 20 54 32 JSR &3254 ; make_positive
&3265 c5 9c    CMP &9c ; range
&3267 90 06    BCC &326f ; leave_with_value                         # Leave with value if -range < value < range
&3269 98       TYA ; range
&326a 24 9d    BIT &9d ; value
&326c 4c 56 32 JMP &3256 ; invert_if_negative                       # Leave with range if value >= 0, -range if value < 0
; leave_with_value
&326f a5 9d    LDA &9d ; value
&3271 60       RTS

; divide_by_sixteen                                                 # Unused entry point
&3272 c9 80    CMP #&80                                             # Keep sign and divide by two
&3274 6a       ROR A
; divide_by_eight
&3275 c9 80    CMP #&80                                             # Keep sign and divide by two
&3277 6a       ROR A
; divide_by_four
&3278 c9 80    CMP #&80                                             # Keep sign and divide by two
&327a 6a       ROR A
&327b c9 80    CMP #&80                                             # Keep sign and divide by two
&327d 6a       ROR A
&327e 60       RTS

; prevent_overflow
&327f 50 04    BVC &3285 ; leave                                    # V set if arithmetic overflow
&3281 a9 7f    LDA #&7f
&3283 69 00    ADC #&00                                             # Keep sign
; leave
&3285 60       RTS

; change_object_type                                                # Called with A = new type
&3286 85 41    STA &41 ; this_object_type
&3288 a8       TAY
&3289 b9 ef 02 LDA &02ef,Y ; object_types_palette_and_pickup_table
&328c 29 7f    AND #&7f                                             # .4218421 palette
&328e 85 73    STA &73 ; this_object_palette                        # Use default palette for type
&3290 a9 00    LDA #&00                                             # Use default sprite for type
; change_object_sprite_to_base_plus_A
&3292 18       CLC
; change_object_sprite_to_base_plus_A_plus_carry
&3293 a4 41    LDY &41 ; this_object_type
&3295 79 8a 02 ADC &028a,Y ; object_types_sprite_table
; change_object_sprite_to_A
&3298 c5 75    CMP &75 ; this_object_sprite
&329a f0 e9    BEQ &3285 ; leave
&329c 85 75    STA &75 ; this_object_sprite
&329e a8       TAY
&329f a2 02    LDX #&02 ; y
&32a1 a5 3c    LDA &3c  this_object_height
&32a3 38       SEC
&32a4 f9 89 5e SBC &5e89,Y ; sprites_height_and_vertical_flip_table
&32a7 20 b0 32 JSR &32b0 ; subtract_A_from_position                 # Returns X = 0, x
; subtract_width_from_position
&32aa a5 3a    LDA &3a ; this_object_width
&32ac 38       SEC
&32ad f9 0c 5e SBC &5e0c,Y ; sprites_width_and_horizontal_flip_table
; subtract_A_from_position
&32b0 6a       ROR A
&32b1 49 80    EOR #&80
&32b3 4c 38 2a JMP &2a38 ; add_A_to_position                        # Keep centre of object in same position

; handle_picking_up_object
&32b6 20 d5 3b JSR &3bd5 ; check_object_touching_angle              # Returns positive if object in a suitable position
&32b9 30 0c    BMI &32c7 ; leave
&32bb bc 60 08 LDY &0860,X ; objects_type
&32be b9 ef 02 LDA &02ef,Y ; object_types_palette_and_pickup_table  # &80 set if object can be picked up
&32c1 25 dd    AND &dd ; player_object_held                         # &80 clear if player is already holding an object
&32c3 10 02    BPL &32c7 ; leave
&32c5 86 dd    STX &dd ; player_object_held                         # Set to positive to indicate player holding an object
; leave
&32c7 60       RTS

; handle_dropping_object
&32c8 24 dd    BIT &dd ; player_object_held                         # Negative if player not holding an object
&32ca 30 fb    BMI &32c7 ; leave
&32cc 38       SEC
&32cd 66 dd    ROR &dd ; player_object_held                         # Set to negative to indicate not holding an object
&32cf 4c a5 14 JMP &14a5 ; play_high_beep                           # Play high beep for dropping object

; throwing_velocities_by_weight_table
;      0  1  2  3  4  5  6
&32d2 20 20 20 20 20 10 08                                          # Very heavy objects can't be thrown as far

; handle_throwing_object
&32d9 20 0f 33 JSR &330f ; calculate_firing_vector_from_aiming_angle
&32dc a4 dd    LDY &dd ; player_object_held                         # Negative if player not holding an object
&32de 30 e7    BMI &32c7 ; leave
&32e0 20 20 1e JSR &1e20 ; get_object_weight
&32e3 a8       TAY
&32e4 a6 dd    LDX &dd ; player_object_held
&32e6 20 c8 32 JSR &32c8 ; handle_dropping_object                   # Stop holding the object
&32e9 20 87 25 JSR &2587 ; rnd
&32ec 29 07    AND #&07                                             # Add randomness to throw
&32ee 79 d2 32 ADC &32d2,Y ; throwing_velocities_by_weight_table    # Throw depends on weight of object
&32f1 20 57 23 JSR &2357 ; calculate_vector_from_magnitude_and_angle
&32f4 24 19    BIT &19 ; this_object_any_bottom_collision           # &80 set if collision to bottom, i.e. supported
&32f6 30 08    BMI &3300 ; skip_adding_y_velocity
&32f8 a5 b6    LDA &b6 ; vector_y
&32fa 18       CLC
&32fb 65 45    ADC &45 ; this_object_velocity_y
&32fd 20 7f 32 JSR &327f ; prevent_overflow
; skip_adding_y_velocity
&3300 9d f6 08 STA &08f6,X ; objects_velocity_y
&3303 a5 b4    LDA &b4 ; vector_x
&3305 18       CLC
&3306 65 43    ADC &43 ; this_object_velocity_x
&3308 20 7f 32 JSR &327f ; prevent_overflow
&330b 9d e6 08 STA &08e6,X ; objects_velocity_x
&330e 60       RTS

; calculate_firing_vector_from_aiming_angle
&330f a5 34    LDA &34 ; player_aiming_angle_with_flip
; calculate_firing_vector_from_angle_A
&3311 85 b5    STA &b5 ; angle
&3313 20 87 25 JSR &2587 ; rnd
&3316 29 03    AND #&03                                             # Add randomness to firing velocity
&3318 69 40    ADC #&40 ; magnitude
&331a 20 57 23 JSR &2357 ; calculate_vector_from_magnitude_and_angle
; calculate_firing_vector_from_this_object_velocity
&331d a5 43    LDA &43 ; this_object_velocity_x
&331f 65 b4    ADC &b4 ; vector_x
&3321 20 7f 32 JSR &327f ; prevent_overflow
&3324 08       PHP ; x sign
&3325 20 56 32 JSR &3256 ; invert_if_negative
&3328 c9 50    CMP #&50                                             # If x speed >= &50
&332a 90 10    BCC &333c ; skip_ceiling
&332c a5 43    LDA &43 ; this_object_velocity_x
&332e 20 56 32 JSR &3256 ; invert_if_negative
&3331 69 20    ADC #&20                                             # use the largest of (object x speed + &20) and &50
&3333 20 7f 32 JSR &327f ; prevent_overflow
&3336 c9 50    CMP #&50
&3338 b0 02    BCS &333c ; skip_ceiling
&333a a9 50    LDA #&50
; skip_ceiling
&333c 28       PLP ; x sign
&333d 20 56 32 JSR &3256 ; invert_if_negative
&3340 85 b4    STA &b4 ; vector_x
&3342 60       RTS

; use_vector_between_object_centres_scaled_by_firing_velocity
&3343 85 a3    STA &a3 ; firing_velocity_times_four
&3345 4a       LSR A
&3346 4a       LSR A
; use_vector_between_object_centres                                 # Called with A = magnitude
&3347 85 a2    STA &a2 ; firing_velocity
&3349 20 a0 22 JSR &22a0 ; calculate_angle_of_object_X_to_this_object
&334c a5 b7    LDA &b7 ; magnitude
&334e 85 84    STA &84 ; distance_fraction
&3350 a5 a2    LDA &a2 ; firing_velocity
&3352 4c 57 23 JMP &2357 ; calculate_vector_from_magnitude_and_angle

; calculate_firing_vector_from_distance                             # Called with A = firing_velocity_times_four
;                                                                   #             X = target object
;                                                                   # Start by calculating vector to target
&3355 20 43 33 JSR &3343 ; use_vector_between_object_centres_scaled_by_firing_velocity
&3358 a5 b8    LDA &b8 ; relative_tiles_log
&335a c9 06    CMP #&06                                             # If objects are 16 or more tiles apart in either x or y,
&335c b0 46    BCS &33a4 ; leave                                    # Leave with carry set to indicate couldn't calculate
&335e 46 a2    LSR &a2 ; firing_velocity
&3360 46 a2    LSR &a2 ; firing_velocity
&3362 06 84    ASL &84 ; distance_fraction                          # This is the (approximate) magnitude of the vector
&3364 a9 00    LDA #&00
&3366 a0 08    LDY #&08
&3368 06 84    ASL &84 ; distance_fraction
; division_loop                                                     # Divide (4 * magnitude) / (firing_velocity / 4)
&336a 2a       ROL A ; distance                                     # i.e. ~16 * magnitude / firing_velocity
&336b c5 a2    CMP &a2 ; firing_velocity
&336d 90 02    BCC &3371 ; skip_subtraction
&336f e5 a2    SBC &a2 ; firing_velocity
; skip_subtraction
&3371 26 84    ROL &84 ; distance_fraction                          # Store result progressively in low bits of distance_fraction
&3373 88       DEY
&3374 d0 f4    BNE &336a ; division_loop
; scale_result
&3376 a5 b8    LDA &b8 ; relative_tiles_log
&3378 18       CLC
&3379 69 04    ADC #&04 ; * 16                                      # Multiply by another 16
&337b a8       TAY
&337c a9 00    LDA #&00
; scaling_loop                                                      # then scale by the approximate distance in tiles
&337e 06 84    ASL &84 ; distance_fraction
&3380 2a       ROL A
&3381 88       DEY
&3382 d0 fa    BNE &337e ; scaling_loop
; apply_upwards_velocity_depending_on_distance
&3384 49 ff    EOR #&ff                                             # Invert high byte of result to add upwards velocity
&3386 38       SEC                                                  # to account for the projectile falling over distance
&3387 65 b6    ADC &b6 ; vector_y                                   # Add more upwards velocity the further away target is
&3389 38       SEC
&338a 70 18    BVS &33a4 ; leave                                    # Leave with carry set to indicate couldn't calculate
&338c 85 b6    STA &b6 ; vector_y                                   # Set firing vector y from angle and distance
&338e 20 56 32 JSR &3256 ; invert_if_negative
&3391 a8       TAY ; absolute vector_y
&3392 bd e6 08 LDA &08e6,X ; objects_velocity_x
&3395 65 b4    ADC &b4 ; vector_x
&3397 20 7f 32 JSR &327f ; prevent_overflow
&339a 85 b4    STA &b4 ; vector_x                                   # Set firing vector x from angle and target x velocity
&339c 20 56 32 JSR &3256 ; invert_if_negative
&339f 20 c1 3b JSR &3bc1 ; get_maximum_of_A_and_Y
&33a2 c5 a3    CMP &a3 ; firing_velocity_times_four                 # Leave with carry set if that would mean firing too fast
; leave
&33a4 60       RTS                                                  # Leave with carry clear if vector calculated

; create_lightning
&33a5 a2 32    LDX #&32 ; OBJECT_LIGHTNING
&33a7 a9 28    LDA #&28 ; x velocity
; create_projectile_with_zero_velocity_y
&33a9 a0 00    LDY #&00 ; y velocity
; create_projectile                                                 # Called with A = x velocity, Y = y velocity, X = type
&33ab 24 37    BIT &37 ; this_object_x_flip
&33ad 20 56 32 JSR &3256 ; invert_if_negative                       # Fire left if facing left
&33b0 85 b4    STA &b4 ; vector_x
&33b2 84 b6    STY &b6 ; vector_y
&33b4 20 1d 33 JSR &331d ; calculate_firing_vector_from_this_object_velocity
&33b7 8a       TXA
; create_child_object
&33b8 20 5d 1e JSR &1e5d ; create_new_object_if_one_slot_free       # Returns carry clear if object created, Y = slot
&33bb b0 e7    BCS &33a4 ; leave                                    # Leave with carry set if object couldn't be created
&33bd 38       SEC
&33be 66 30    ROR &30 ; child_object_created                       # Set &80 to suppress changing player sprite when firing
&33c0 98       TYA
&33c1 aa       TAX                                                  # X = child object slot
&33c2 a5 39    LDA &39 ; this_object_y_flip                         # &80 set if vertically flipped
&33c4 29 80    AND #&80
&33c6 4a       LSR A                                                # Set &40 if vertically flipped
;                                                                   # i.e. child inherits vertical flip of parent
&33c7 09 05    ORA #&05 ; OBJECT_FLAG_NOT_PLOTTED | OBJECT_FLAG_NEWLY_CREATED
&33c9 99 c6 08 STA &08c6,Y ; objects_flags
&33cc 20 2f 34 JSR &342f ; set_object_X_velocities_from_vector
&33cf bc 70 08 LDY &0870,X ; objects_sprite                         # Y = sprite
&33d2 38       SEC
&33d3 a5 3c    LDA &3c  this_object_height
&33d5 f9 89 5e SBC &5e89,Y ; sprites_height_and_vertical_flip_table
&33d8 4a       LSR A
&33d9 65 51    ADC &51 ; this_object_y_fraction                     # Align child object with vertical centre of parent
&33db 9d a3 08 STA &08a3,X ; objects_y_fraction
&33de a5 55    LDA &55 ; this_object_y
&33e0 69 00    ADC #&00
&33e2 9d b4 08 STA &08b4,X ; objects_y
&33e5 a5 43    LDA &43 ; this_object_velocity_x                     # parent x velocity - child x velocity
&33e7 38       SEC
&33e8 e5 b4    SBC &b4 ; vector_x
&33ea 20 7f 32 JSR &327f ; prevent_overflow
&33ed 85 9d    STA &9d ; relative_velocity_x                        # Negative if child moving right relative to parent
&33ef 45 43    EOR &43 ; this_object_velocity_x
&33f1 18       CLC
&33f2 08       PHP ; negative if parent and child moving in same direction in x
&33f3 a5 9d    LDA &9d ; relative_velocity_x
&33f5 10 08    BPL &33ff ; is_moving_left_relative_to_parent
; is_moving_right_relative_to_parent
&33f7 a5 3a    LDA &3a ; this_object_width                          # Object width minus one pixel
&33f9 69 18    ADC #&18                                             # Add half pixel gap
&33fb a0 01    LDY #&01                                             # Overflow to next tile
&33fd d0 07    BNE &3406 ; consider_overflow_or_underflow           # Always branches
; is_moving_left_relative_to_parent
&33ff a9 e9    LDA #&e9 ; - &17 (carry is clear, so actually - &18) # Subtract half pixel gap, and width of child
&3401 f9 0c 5e SBC &5e0c,Y ; sprites_width_and_horizontal_flip_table
&3404 a0 ff    LDY #&ff                                             # Underflow to previous tile
; consider_overflow_or_underflow
&3406 b0 01    BCS &3409 ; skip_overflow_or_underflow
&3408 88       DEY
; skip_overflow_or_underflow
&3409 18       CLC
&340a 65 4f    ADC &4f ; this_object_x_fraction
&340c 85 9c    STA &9c ; child_x_fraction
&340e 98       TYA
&340f 65 53    ADC &53 ; this_object_x
&3411 a8       TAY
&3412 a5 9d    LDA &9d ; relative_velocity_x
&3414 28       PLP ; negative if parent and child moving in same direction in x
&3415 30 04    BMI &341b ; is_moving_in_same_direction
; is_moving_in_opposite_direction
&3417 a9 01    LDA #&01
&3419 e5 b4    SBC &b4 ; vector_x
; is_moving_in_same_direction
&341b c9 00    CMP #&00
&341d 10 01    BPL &3420 ; skip_overflow
&341f 88       DEY
; skip_underflow
&3420 18       CLC
&3421 65 9c    ADC &9c ; child_x_fraction                           # Move child away from parent
&3423 9d 80 08 STA &0880,X ; objects_x_fraction
&3426 90 01    BCC &3429 ; skip_overflow
&3428 c8       INY
; skip_overflow
&3429 98       TYA
&342a 9d 91 08 STA &0891,X ; objects_x
&342d 18       CLC                                                  # Leave with carry clear to indicate child object created
&342e 60       RTS                                                  #            X = child object slot

; set_object_X_velocities_from_vector
&342f a5 b4    LDA &b4 ; vector_x
&3431 9d e6 08 STA &08e6,X ; objects_velocity_x
&3434 a5 b6    LDA &b6 ; vector_y
&3436 9d f6 08 STA &08f6,X ; objects_velocity_y
&3439 60       RTS

; accelerate_all_objects                                            # Accelerate all object to / away from this one
&343a a9 ff    LDA #&ff
; accelerate_all_objects_within_angle                               # Called with A = angle range, &b5 = centre angle
&343c 85 a1    STA &a1 ; angle_range
&343e a5 b5    LDA &b5 ; angle
&3440 85 a0    STA &a0 ; centre_angle
&3442 a2 0f    LDX #&0f
; accelerate_all_objects_loop                                       # For each object,
&3444 e4 aa    CPX &aa ; this_object
&3446 f0 5c    BEQ &34a4 ; consider_next_object                     # Don't affect the source
&3448 a5 35    LDA &35 ; acceleration_power                         # Used as distance, in &20 fractions
&344a 20 9c 35 JSR &359c ; check_for_obstruction_between_objects_A  # Returns carry set if no line of sight
;                                                                   # also calculates angle and distance
&344d b0 55    BCS &34a4 ; consider_next_object
&344f a5 b5    LDA &b5 ; angle
&3451 45 29    EOR &29 ; acceleration_sign                          # &80 set if accelerating towards source
&3453 85 b5    STA &b5 ; angle
&3455 e5 a0    SBC &a0 ; centre_angle
&3457 c5 a1    CMP &a1 ; angle_range
&3459 90 06    BCC &3461 ; is_within_angle_range
&345b 49 ff    EOR #&ff
&345d c5 a1    CMP &a1 ; angle_range
&345f b0 43    BCS &34a4 ; consider_next_object                     # Is the target within the angle range?
; is_within_angle_range
&3461 bc 60 08 LDY &0860,X ; objects_type
&3464 b9 54 03 LDA &0354,Y ; object_types_flags_table
&3467 29 07    AND #&07 ; OBJECT_TYPE_FLAG_WEIGHT_MASK
&3469 c9 07    CMP #&07
&346b 66 24    ROR &24 ; static                                     # Set &80 if target is static
&346d 0a       ASL A                                                # Heavier objects are accelerated less
&346e 69 08    ADC #&08 ; 1 tile
&3470 65 83    ADC &83 ; distance
&3472 e5 35    SBC &35 ; acceleration_power
&3474 49 ff    EOR #&ff                                             # power - (target weight * 2 + 8 + distance)
&3476 b0 2c    BCS &34a4 ; consider_next_object                     # Branch if target is too distant for its weight, or static
&3478 24 28    BIT &28 ; acceleration_damages_targets               # &80 set to damage targets
&347a 10 0e    BPL &348a ; skip_damaging_target
&347c c9 04    CMP #&04
&347e 90 0a    BCC &348a ; skip_damaging_target                     # Light acceleration doesn't damage target
&3480 86 9d    STX &9d ; target
&3482 a4 9d    LDY &9d ; target
&3484 48       PHA ; acceleration * 2
&3485 0a       ASL A
&3486 20 a6 24 JSR &24a6 ; damage_object                            # Damage target by acceleration
&3489 68       PLA ; acceleration * 2
; skip_damaging_target
&348a 24 24    BIT &24  static                                      # &80 if target is static
&348c 30 16    BMI &34a4 ; consider_next_object                     # Static targets aren't accelerated
&348e 4a       LSR A                                                # acceleration = power - (weight_distance) / 2
&348f 20 57 23 JSR &2357 ; calculate_vector_from_magnitude_and_angle # Returns A = vector_y
&3492 7d f6 08 ADC &08f6,X ; objects_velocity_y
&3495 70 03    BVS &349a ; skip_accelerating_y                      # Apply to target velocity unless that overflows
&3497 9d f6 08 STA &08f6,X ; objects_velocity_y
; skip_accelerating_y
&349a a5 b4    LDA &b4 ; vector_x
&349c 7d e6 08 ADC &08e6,X ; objects_velocity_x
&349f 70 03    BVS &34a4 ; skip_accelerating_x
&34a1 9d e6 08 STA &08e6,X ; objects_velocity_x
; skip_accelerating_x
; consider_next_object
&34a4 ca       DEX
&34a5 10 9d    BPL &3444 ; accelerate_all_objects_loop
&34a7 46 28    LSR &28 ; acceleration_damages_targets               # Clear &80 to not damage targets by default
&34a9 a9 28    LDA #&28 ; 5 tiles
&34ab 85 35    STA &35 ; acceleration_power                         # Set default acceleration power
&34ad e8       INX ; 0
&34ae 86 29    STX &29 ; acceleration_sign                          # Set to positive to accelerate away from source
&34b0 60       RTS

; handle_storing_object
&34b1 a9 04    LDA #&04                                             # Don't use fifth pocket when storing permanently
&34b3 2c a9 05 BIT &05a9 ; (nop)
; store_object
#34b4          LDA #&05                                             # Use fifth pocket temporarily; will be retrieved
&34b6 8d db 34 STA &34db ; player_pockets_used
&34b9 a4 dd    LDY &dd ; player_object_held                         # Negative if player not holding object
&34bb 18       CLC                                                  # Leave with carry clear to indicate no object held
&34bc 30 6f    BMI &352d ; leave
&34be be 70 08 LDX &0870,Y ; objects_sprite
&34c1 bd 89 5e LDA &5e89,X ; sprites_height_and_vertical_flip_table
&34c4 c9 38    CMP #&38 ; 7 pixels                                  # Can't store objects that are too tall
&34c6 b0 65    BCS &352d ; leave                                    # Leave with carry set to indicate object still held
&34c8 b9 60 08 LDA &0860,Y ; objects_type
&34cb c9 4b    CMP #&4b ; OBJECT_POWER_POD
&34cd d0 08    BNE &34d7 ; not_power_pod
&34cf a2 00    LDX #&00 ; jetpack
&34d1 20 16 2d JSR &2d16 ; increase_weapon_X_energy                 # Add &800 of energy for each power pod 
&34d4 4c f4 34 JMP &34f4 ; leave_with_carry_clear                   # Leave with carry clear to indicate no object held
; not_power_pod
&34d7 ae 47 08 LDX &0847 ; player_pockets_used
&34da e0 05    CPX #&05
#     actually CPX number_of_pockets_to_use
&34dc b0 4f    BCS &352d ; leave                                    # Leave with carry set to indicate object still held
&34de a2 05    LDX #&05
&34e0 48       PHA
; shuffle_pockets_loop                                              # Make space in first pocket for stored object
&34e1 bd 46 08 LDA &0846,X ; player_pockets - 2
&34e4 9d 47 08 STA &0847,X ; player_pockets - 1
&34e7 ca       DEX
&34e8 d0 f7    BNE &34e1 ; shuffle_pockets_loop
&34ea 68       PLA
&34eb 8d 48 08 STA &0848 ; player_pockets                           # Store object in first pocket
&34ee ee 47 08 INC &0847 ; player_pockets_used
&34f1 20 c8 32 JSR &32c8 ; handle_dropping_object                   # Set player_object_held to negative
; leave_with_carry_clear
&34f4 18       CLC                                                  # Leave with carry clear to indicate no object held
&34f5 4c 16 25 JMP &2516 ; set_object_for_removal

; handle_retrieving_object
&34f8 20 b4 34 JSR &34b4 ; store_object                             # Returns carry clear if object not held
&34fb 6e 6a 31 ROR &316a ; player_needs_to_retrieve_object          # If so, make positive to indicate object needs retrieving
&34fe 60       RTS

; consider_retrieving_object
&34ff 2c 6a 31 BIT &316a ; player_needs_to_retrieve_object          # Positive if player needs to retrieve object
&3502 30 29    BMI &352d ; leave
; retrieve_object
&3504 a5 37    LDA &37 ; this_object_x_flip
&3506 29 80    AND #&80                                             # Use either right (&00) or left (&80) of player
&3508 20 11 33 JSR &3311 ; calculate_firing_vector_from_angle_A
&350b ae 47 08 LDX &0847 ; player_pockets_used
&350e f0 19    BEQ &3529 ; leave_after_resetting_player_needs_to_retrieve_object
&3510 bd 47 08 LDA &0847,X ; player_pockets - 1
&3513 20 b8 33 JSR &33b8 ; create_child_object                      # Returns carry clear if object created, X = slot
&3516 b0 15    BCS &352d ; leave
&3518 86 dd    STX &dd ; player_object_held                         # Set to positive to indicate player holding object
&351a 20 fa 13 JSR &13fa ; play_sound
&351d 17 82 13 c2                                                   # Play sound for retrieving object
&3521 a4 dd    LDY &dd ; player_object_held
&3523 20 a9 0b JSR &0ba9 ; set_object_Y_velocities_from_this_object
&3526 ce 47 08 DEC &0847 ; player_pockets_used
; leave_after_resetting_player_needs_to_retrieve_object
&3529 38       SEC
&352a 6e 6a 31 ROR &316a ; player_needs_to_retrieve_object          # Set to negative to indicate object retrieved
; leave
&352d 60       RTS

; give_object_minimum_energy                                        # Called with Y = minimum energy for object
&352e a5 15    LDA &15 ; this_object_energy
&3530 f0 05    BEQ &3537 ; leave                                    # If zero, object is exploding; leave
&3532 20 c1 3b JSR &3bc1 ; get_maximum_of_A_and_Y
&3535 85 15    STA &15 ; this_object_energy
; leave
&3537 60       RTS

; gain_energy_and_flash_if_damaged
&3538 a0 1e    LDY #&1e                                             # Give object a minimum energy of 30
; gain_energy_Y_and_flash_if_damaged
&353a 24 c5    BIT &c5 ; every_four_frames
&353c 10 09    BPL &3547 ; skip_gaining_energy                      # Every four frames,
&353e a5 15    LDA &15 ; this_object_energy
&3540 c9 c0    CMP #&c0
&3542 b0 03    BCS &3547 ; skip_gaining_energy                      # if energy < 192,
&3544 20 4e 25 JSR &254e ; increase_energy_by_one_if_not_zero       # gain one energy
; skip_gaining_energy
; flash_if_damaged
&3547 20 2e 35 JSR &352e ; give_object_minimum_energy
&354a 0a       ASL A                                                # Clear carry if energy < 128
&354b 08       PHP ; carry clear if energy < &80
&354c b0 06    BCS &3554 ; not_low
&354e a5 06    LDA &06 ; this_object_frame_counter
&3550 29 07    AND #&07
&3552 c9 02    CMP #&02                                             # If so, use damaged palette two in eight frames
; not_low
&3554 20 df 4d JSR &4ddf ; use_damaged_palette_if_carry_clear
&3557 28       PLP ; carry clear if energy < &80                    # Leave with carry clear if energy < 128 (not used)
&3558 60       RTS

; get_object_distance_from_screen_centre
&3559 a4 aa    LDY &aa ; this_object
; get_object_Y_distance_from_screen_centre
&355b 38       SEC
&355c b9 91 08 LDA &0891,Y ; objects_x
&355f e9 04    SBC #&04                                             # Screen is 4 * 2 tiles wide
&3561 e5 c8    SBC &c8 ; screen_origin_x
&3563 20 56 32 JSR &3256 ; invert_if_negative                       # Returns carry clear
&3566 85 9d    STA &9d ; delta_x
&3568 b9 b4 08 LDA &08b4,Y ; objects_y
&356b e9 01    SBC #&01 ; 2 - carry                                 # Screen is 2 * 2 tiles high
&356d e5 ca    SBC &ca ; screen_origin_y
&356f 20 56 32 JSR &3256 ; invert_if_negative
&3572 65 9d    ADC &9d ; delta_x
&3574 6a       ROR A                                                # Leave with (delta_x + delta_y) / 2, keeping sign
&3575 60       RTS

; handle_pause
&3576 4e bd 14 LSR &14bd ; suppress_updating_sound                  # Clear top bit to suppress updating sounds
&3579 20 81 35 JSR &3581 ; wait_for_copy_to_be_released
; wait_for_copy_to_be_pressed
&357c 2c 6b 12 BIT &126b ; action_keys_pressed + 0 (COPY)
&357f 10 fb    BPL &357c ; wait_for_copy_to_be_pressed
; wait_for_copy_to_be_released
&3581 2c 6b 12 BIT &126b ; action_keys_pressed + 0 (COPY)
&3584 30 fb    BMI &3581 ; wait_for_copy_to_be_released
&3586 6e bd 14 ROR &14bd ; suppress_updating_sound                  # Set top bit to update sounds
&3589 60       RTS

; player_has_functioning_jetpack
&358a ff

; suppress_updating_screen
&358b ff                                                            # If positive, suppress updating screen; always negative

; scrolling_offsets_x_fraction_table                                # Eight tiles minus one &20 fraction horizontal section
;      -  +
&358c 00 e0

; scrolling_offsets_x_table
;      -  +
&358e 00 07

; scrolling_offsets_y_fraction_table                                # Four tiles minus one &40 fraction vertical section
;      -  +
&3590 00 c0

; scrolling_offsets_y_table
;      -  +
&3591 00 03

; screen_wipe_offsets_high_table                                    # Offset in pages from top to bottom of screen
;      0 -2 -4 -6
&3594 00 1e 1c 1a

; waterline_blocks_line_of_sight
&3598 00

; offset_of_last_door_considered
&3599 00

; check_for_obstruction_between_objects_80                          # Called with X = target object
&359a a9 80    LDA #&80 ; 128 &20 fractions = 16 tiles
; check_for_obstruction_between_objects                             # Called with A = distance in &20 fractions
&359c 8d cd 35 STA &35cd ; maximum_distance
&359f bd b4 08 LDA &08b4,X ; objects_y                              # Zero if no object in slot
&35a2 38       SEC                                                  # Set carry to leave with carry set via &34d2
&35a3 f0 29    BEQ &35ce ; skip_distance_check                      # Leave with distance = maximum_distance
&35a5 bd 60 08 LDA &0860,X ; objects_type
&35a8 e9 3c    SBC #&3c ; OBJECT_HORIZONTAL_METAL_DOOR              # If the target is a door,
&35aa c9 04    CMP #&04 ; OBJECT_VERTICAL_STONE_DOOR - OBJECT_HORIZONTAL_METAL_DOOR + 1
&35ac b0 06    BCS &35b4 ; not_door
&35ae bd 66 09 LDA &0966,X ; objects_tertiary_data_offset           # Stop door being created as tertiary object
&35b1 8d 99 35 STA &3599 ; door_to_suppress                         # when the tile it contains is considered
; not_door
&35b4 a9 20    LDA #&20 ; magnitude                                 # Get a vector that's one-eighth of a tile long
&35b6 20 47 33 JSR &3347 ; use_vector_between_object_centres        # Sets distance_fraction to magnitude of vector
&35b9 a4 b8    LDY &b8 ; relative_tiles_log                         # Power of two needed to contain distance in tiles
&35bb c8       INY                                                  # Multiply by eight to give distance in &20 fractions
&35bc c8       INY
&35bd c8       INY
&35be a9 00    LDA #&00
; scaling_loop                                                      # Convert distance to number of &20 fractions
&35c0 06 84    ASL &84 ; distance_fraction
&35c2 2a       ROL A ; distance
&35c3 90 02    BCC &35c7 ; skip_ceiling
&35c5 a9 fd    LDA #&fd
; skip_ceiling
&35c7 88       DEY
&35c8 d0 f6    BNE &35c0 ; scaling_loop
&35ca 69 01    ADC #&01
&35cc c9 40    CMP #&40
#     actually CMP maximum_distance
; skip_distance_check
&35ce 90 05    BCC &35d5 ; within_range                             # If distance is beyond the maximum to check,
&35d0 85 83    STA &83 ; distance
&35d2 4c 7a 36 JMP &367a ; leave_with_carry_set                     # Leave with carry set to indicate no line of sight
; within_range
; check_for_obstruction_along_vector
&35d5 85 84    STA &84 ; distance_to_check                          # In &20 fraction sections
&35d7 86 9d    STX &9d ; tmp_x
&35d9 a2 02    LDX #&02
; prepare_variables_and_opcodes_loop                                # Loop through X = 2 for y, X = 0 for x
&35db 8d 34 36 STA &3634 ; next_tile_y_branch_opcode                # Only meaningful on second pass
&35de 49 76    EOR #&76 ; &e6 (INC) ^ &90 (BCC) or &c6 (DEC) ^ &b0 (BCS)
&35e0 8d 36 36 STA &3636 ; next_tile_y_opcode                       # &c6 = DEC &.. or &e6 = INC &..
&35e3 86 83    STX &83 ; distance
&35e5 b5 3a    LDA &3a,X ; this_object_width
&35e7 4a       LSR A
&35e8 75 4f    ADC &4f,X ; this_object_x_fraction
&35ea 95 80    STA &80,X ; tile_x_fraction                          # Set initial position to be this object's centre
&35ec b5 53    LDA &53,X ; this_object_x
&35ee 69 00    ADC #&00
&35f0 95 95    STA &95,X ; tile_x
&35f2 b5 b4    LDA &b4,X ; vector_x
&35f4 4a       LSR A                                                # 84218421 -> .8421842 1
&35f5 29 20    AND #&20                                             #          -> ..4.....
&35f7 49 90    EOR #&90                                             # &90 BCC if clear or &b0 BCS if set
&35f9 ca       DEX
&35fa ca       DEX
&35fb f0 de    BEQ &35db ; prepare_variables_and_opcodes_loop
&35fd 8d 42 36 STA &3642 ; next_tile_x_branch_opcode
&3600 49 76    EOR #&76 ; &e6 (INC) ^ &90 (BCC) or &c6 (DEC) ^ &b0 (BCS)
&3602 8d 44 36 STA &3644 ; next_tile_x_opcode                       # &c6 = DEC &.. or &e6 = INC &..
&3605 a9 40    LDA #&40 ; TILE_PROCESSING_FLAG_OBSTRUCTION          # Only call tile update routines that want to know
&3607 85 2d    STA &2d ; tile_processing_mode                       # about tiles being checked for obstruction (doors)
&3609 a5 95    LDA &95 ; tile_x
&360b 20 bc 2c JSR &2cbc ; get_waterline_for_x
&360e a5 82    LDA &82 ; tile_y_fraction
&3610 cd d0 14 CMP &14d0 ; waterline_y_fraction
&3613 a5 97    LDA &97 ; tile_y
&3615 ed d1 14 SBC &14d1 ; waterline_y
&3618 6a       ROR A                                                # C ........ -> . C.......
&3619 6a       ROR A                                                #            -> . .C......
&361a 6a       ROR A                                                #            -> . ..C.....
&361b 29 20    AND #&20
&361d 49 b0    EOR #&b0                                             # &b0 BCC if carry clear, i.e. starting below water
&361f 8d 70 36 STA &3670 ; waterline_check_branch_opcode            # &90 BCS if carry set, i.e. starting above water
&3622 a6 9d    LDX &9d ; tmp_x
&3624 20 53 24 JSR &2453 ; set_obstruction_data_variables_for_top_tile
; check_for_obstruction_loop
&3627 a5 82    LDA &82 ; tile_y_fraction                            # Move along the line in y,
&3629 18       CLC
&362a 65 b6    ADC &b6 ; vector_y
&362c 85 82    STA &82 ; tile_y_fraction
&362e 29 f8    AND #&f8
&3630 09 04    ORA #&04                                             # Round to middle of pixel
&3632 85 81    STA &81 ; tile_y_fraction_rounded
; next_tile_y_branch_opcode                                         # moving to next tile if necessary
&3634 b0 05    BCS &363b ; skip_tile_y if y velocity is negative
#     or       BCC &363b ; skip_tile_y if y velocity is positive
; next_tile_y_opcode
&3636 e6 97    INC &97 ; tile_y if y velocity is positive
#     or       DEC &97 ; tile_y if y velocity is negative
&3638 20 53 24 JSR &2453 ; set_obstruction_data_variables_for_top_tile
; skip_tile_y
&363b a5 80    LDA &80 ; tile_x_fraction                            # Move along the line in x,
&363d 18       CLC
&363e 65 b4    ADC &b4 ; vector_x
&3640 85 80    STA &80 ; tile_x_fraction
; next_tile_x_branch_opcode                                         # moving to next tile if necessary
&3642 b0 05    BCS &3649 ; skip_tile_x if x velocity is negative
#     or       BCC &3649 ; skip_tile_x if x velocity is positive
; next_tile_x_opcode
&3644 e6 95    INC &95 ; tile_x if x velocity is positive
#     or       DEC &95 ; tile_x if x velocity is negative
&3646 20 53 24 JSR &2453 ; set_obstruction_data_variables_for_top_tile
; skip_tile_x
&3649 a5 80    LDA &80 ; tile_x_fraction
&364b 29 e0    AND #&e0                                             # Convert x fraction to section of tile
&364d 0a       ASL A
&364e 2a       ROL A
&364f 2a       ROL A
&3650 2a       ROL A
&3651 a8       TAY
&3652 b1 7c    LDA (&7c),Y ; top_tile_obstruction_data_address      # Determine where the tile obstruction begins
&3654 65 7e    ADC &7e ; top_tile_obstruction_y_offset
&3656 90 02    BCC &365a ; skip_ceiling
&3658 a9 ff    LDA #&ff                                             # &ff indicates whole tile is clear or obstructed
; skip_ceiling
&365a c5 81    CMP &81 ; tile_y_fraction_rounded
&365c 6a       ROR A                                                # Set &80 if point to check if below that
&365d 45 7f    EOR &7f ; top_tile_sprite_and_y_flip                 # &80 set if flipped vertically, i.e. obstruction at top
&365f 10 19    BPL &367a ; leave_with_carry_set                     # Leave with carry set if obstruction blocks line of sight
&3661 2c 98 35 BIT &3598 ; waterline_blocks_line_of_sight           # Negative to consider waterline as obstruction
&3664 10 0c    BPL &3672 ; skip_waterline_check
&3666 a5 82    LDA &82 ; tile_y_fraction
&3668 cd d0 14 CMP &14d0 ; waterline_y_fraction
&366b a5 97    LDA &97 ; tile_y
&366d ed d1 14 SBC &14d1 ; waterline_y
; waterline_check_branch_opcode                                     # Leave with carry set if waterline crossed
&3670 90 08    BCC &367a ; leave_with_carry_set if check started below the waterline
#     or       BCS &367a ; leave_with_carry_set if check started above the waterline
; skip_waterline_check
&3672 e6 83    INC &83 ; distance
&3674 c6 84    DEC &84 ; distance_to_check
&3676 d0 af    BNE &3627 ; check_for_obstruction_loop
&3678 18       CLC                                                  # Leave with carry clear to indicate no obstruction
&3679 24 38    BIT &38 ; (nop)
; leave_with_carry_set
#367a          SEC                                                  # Leave with carry set to indicate obstruction
&367b a9 ff    LDA #&ff
&367d 8d 99 35 STA &3599 ; door_to_suppress                         # Allow door to be created when tile next plotted
&3680 8d 98 35 STA &3598 ; waterline_blocks_line_of_sight           # Set top bit to consider waterline next time
; leave
&3683 60       RTS

; prepare_screen_for_scrolling
&3684 ad 8b 35 LDA &358b ; suppress_updating_screen                 # Positive to suppress updating screen
&3687 10 fa    BPL &3683 ; leave                                    # Always negative; never branches
&3689 a5 cf    LDA &cf ; screen_tile_sections_to_scroll_x
&368b f0 58    BEQ &36e5 ; skip_preparing_screen_in_x
; prepare_screen_for_scrolling_in_x                                 # Calculate first tile on vertical edge of screen
&368d a4 cc    LDY &cc ; screen_scrolling_sign_x                    # &ff if scrolling left, &00 if scrolling right
&368f c8       INY
&3690 b9 8c 35 LDA &358c,Y ; scrolling_offsets_x_fraction_table
&3693 18       CLC
&3694 65 c7    ADC &c7 ; screen_origin_x_fraction
&3696 b9 8e 35 LDA &358e,Y ; scrolling_offsets_x_table
&3699 65 c8    ADC &c8 ; screen_origin_x
&369b 85 95    STA &95 ; tile_x
&369d a5 ca    LDA &ca ; screen_origin_y
&369f 85 97    STA &97 ; tile_y
&36a1 a5 cb    LDA &cb ; screen_tile_fractions_to_scroll_x          # Calculate screen address and size of strip to wipe
&36a3 24 cc    BIT &cc ; screen_scrolling_sign_x
&36a5 20 56 32 JSR &3256 ; invert_if_negative
&36a8 4a       LSR A                                                # One &20 fraction = 8 byte column, so divide by four
&36a9 4a       LSR A
&36aa 85 9c    STA &9c ; strip_width_in_bytes
&36ac a2 11    LDX #&11 ; 16 + 1                                    # Wipe four tiles of four groups = 16 groups
&36ae 38       SEC
&36af a5 b0    LDA &b0 ; screen_start_offset_low
&36b1 24 cc    BIT &cc ; screen_scrolling_sign_x
&36b3 30 02    BMI &36b7 ; is_scrolling_right
; is_scrolling_left
&36b5 e5 9c    SBC &9c ; strip_width_in_bytes                       # Wipe left of screen if scrolling left, right if right
; is_scrolling_right
&36b7 85 8f    STA &8f ; screen_address_low
&36b9 a5 b1    LDA &b1 ; screen_start_offset_high
&36bb e9 00    SBC #&00
&36bd 18       CLC
&36be 69 60    ADC #&60 ; &6000 = screen_memory
&36c0 38       SEC
&36c1 c6 9c    DEC &9c ; strip_width_in_bytes
&36c3 24 cc    BIT &cc ; screen_scrolling_sign_x
&36c5 30 0d    BMI &36d4 ; start_wiping_vertical_strip_after_moving_up_a_group
&36c7 10 0d    BPL &36d6 ; start_wiping_vertical_strip              # Always branches
; wipe_vertical_strip_group_loop
&36c9 a9 00    LDA #&00
&36cb a4 9c    LDY &9c ; strip_width
; wipe_vertical_strip_byte_loop
&36cd 91 8f    STA (&8f),Y ; screen_address
&36cf 88       DEY
&36d0 10 fb    BPL &36cd ; wipe_vertical_strip_byte_loop
&36d2 a5 90    LDA &90 ; screen_address_high
; start_wiping_vertical_strip_after_moving_up_a_group
&36d4 e9 02    SBC #&02                                             # Move up a group
; start_wiping_vertical_strip
&36d6 09 60    ORA #&60 ; &6000 = screen_memory                     # Wrap around end of screen memory
&36d8 85 90    STA &90 ; screen_address_high
&36da ca       DEX
&36db d0 ec    BNE &36c9 ; wipe_vertical_strip_group_loop
&36dd a0 04    LDY #&04                                             # Update four tiles along vertical edge of screen
&36df a9 97    LDA #&97 ; tile_y
&36e1 a2 02    LDX #&02 ; screen_origin_y_fraction - &c7
&36e3 d0 76    BNE &375b ; skip_preparing_screen_in_y               # Always branches
; skip_preparing_screen_in_x
&36e5 a6 d1    LDX &d1 ; screen_tile_sections_to_scroll_y
&36e7 f0 72    BEQ &375b ; skip_updating_screen_in_y
; prepare_screen_for_scrolling_in_x                                 # Calculate first tile on horizontal edge of screen
&36e9 a4 ce    LDY &ce ; screen_scrolling_sign_y                    # &ff if scrolling up, &00 if scrolling down
&36eb c8       INY
&36ec b9 90 35 LDA &3590,Y ; scrolling_offsets_y_fraction_table
&36ef 18       CLC
&36f0 65 c9    ADC &c9 ; screen_origin_y_fraction
&36f2 b9 92 35 LDA &3592,Y ; scrolling_offsets_y_table
&36f5 65 ca    ADC &ca ; screen_origin_y
&36f7 85 97    STA &97 ; tile_y
&36f9 a5 c8    LDA &c8 ; screen_origin_x
&36fb 85 95    STA &95 ; tile_x
&36fd 98       TYA                                                  # Calculate screen address and size of strip to wipe
&36fe f0 02    BEQ &3702 ; is_scrolling_up  
; is_scrolling_down
&3700 a4 d1    LDY &d1 ; screen_tile_sections_to_scroll_y           # Wipe top of screen if scrolling up, bottom if down
; is_scrolling_up
&3702 a5 b0    LDA &b0 ; screen_start_offset_low
&3704 18       CLC
&3705 85 93    STA &93 ; strip_offset
&3707 a5 b1    LDA &b1 ; screen_start_offset_high
&3709 79 94 35 ADC &3594,Y ; screen_wipe_offsets_high_table
&370c 29 1f    AND #&1f
&370e 09 60    ORA #&60 ; &6000 = screen_memory
&3710 85 94    STA &94 ; wipe_address_high
&3712 a5 d1    LDA &d1 ; screen_tile_sections_to_scroll_y           # One section corresponds to one group
&3714 20 56 32 JSR &3256 ; invert_if_negative
&3717 85 a0    STA &a0 ; groups_to_wipe
; wipe_horizontal_strip_group_loop
&3719 a5 94    LDA &94 ; wipe_address_high
&371b 85 90    STA &90 ; screen_address_high
&371d a2 02    LDX #&02                                             # Wipe fraction of first page, then whole of second page
&371f a4 93    LDY &93 ; strip_offset
&3721 a9 00    LDA #&00
&3723 85 8f    STA &8f ; screen_address_low
; wipe_left_of_horizontal_strip_loop
&3725 91 8f    STA (&8f),Y ; screen_address
&3727 c8       INY
&3728 d0 fb    BNE &3725 ; wipe_left_of_horizontal_strip_loop
&372a e6 90    INC &90 ; screen_address_high
&372c 10 06    BPL &3734 ; skip_wraparound
&372e a9 60    LDA #&60 ; &6000 = screen_memory
&3730 85 90    STA &90 ; screen_address_high
&3732 a9 00    LDA #&00
; skip_wraparound
&3734 ca       DEX
&3735 d0 ee    BNE &3725 ; wipe_left_of_horizontal_strip_loop
&3737 c6 90    DEC &90 ; screen_address_high
&3739 c6 8f    DEC &8f ; screen_address_low
&373b a4 93    LDY &93 ; strip_offset
&373d f0 07    BEQ &3746 ; skip_right_of_strip                      # If screen isn't page aligned,
&373f a9 00    LDA #&00                                             # Wipe fraction of third page
; wipe_right_of_horizontal_strip_loop
&3741 91 8f    STA (&8f),Y ; screen_address
&3743 88       DEY
&3744 d0 fb    BNE &3741 ; wipe_right_of_horizontal_strip_loop
; skip_right_of_strip
&3746 a5 94    LDA &94 ; wipe_address_high
&3748 18       CLC
&3749 69 02    ADC #&02                                             # Move down a group
&374b 29 1f    AND #&1f
&374d 09 60    ORA #&60 ; &6000 = screen_memory                     # Wrap around end of screen memory
&374f 85 94    STA &94 ; wipe_address_high
&3751 c6 a0    DEC &a0 ; groups_to_wipe
&3753 d0 c4    BNE &3719 ; wipe_horizontal_strip_loop
&3755 a9 95    LDA #&95 ; tile_x
&3757 a0 08    LDY #&08                                             # Update eight tiles alone horizontal edge of screen
&3759 a2 00    LDX #&00 ; screen_origin_x_fraction - &c7
; skip_preparing_screen_in_y
&375b 8d ec 10 STA &10ec ; tile_variable_for_plotting
&375e 8d 8b 37 STA &378b ; tile_variable_for_caching
&3761 48       PHA ; tile strip variable
&3762 b5 c7    LDA &c7,X ; screen_origin_x_fraction
&3764 f0 01    BEQ &3767 ; skip_extra_tile
&3766 c8       INY                                                  # Cache an extra tile if screen origin not tile aligned
; skip_extra_tile
&3767 84 af    STY &af ; tiles_remaining_to_plot
&3769 84 ae    STY &ae ; tiles_remaining_to_cache
&376b 68       PLA ; tile_strip_variable
&376c aa       TAX
&376d b5 00    LDA &00,X ; tile_x - &95
&376f 48       PHA ; tile coordinate
&3770 8a       TXA
&3771 48       PHA ; tile_strip_variable
&3772 ad cd 14 LDA &14cd ; new_tiles_exposed_during_scrolling       # &80 set if any new tiles are being considered
&3775 29 80    AND #&80 ; TILE_PROCESSING_FLAG_PLOTTING             # If so, only call tile update routines that want to
&3777 85 2d    STA &2d ; tile_processing_mode                       # know about new tiles being plotted (everything but
;                                                                   # invisible switches, wind, water or mushrooms)
; cache_tile_strip_tiles_loop                                       # For each tile in the strip,
&3779 c6 ae    DEC &ae ; tiles_remaining
&377b 30 12    BMI &378f ; finished_caching_tile_strip_tiles
&377d 20 15 17 JSR &1715 ; get_tile_and_check_for_tertiary_objects  # Returns A = tile type
&3780 a6 ae    LDX &ae ; tiles_remaining_to_cache
&3782 9d f6 07 STA &07f6,X ; tile_strip_tiles                       # store its type and flip for subsequent plotting
&3785 a5 09    LDA &09 ; tile_flip
&3787 9d ed 07 STA &07ed,X ; tile_strip_flips
&378a e6 95    INC &95 ; tile_x if strip is horizontal
#     or       INC &97 ; tile_y if strip is vertical
#     actually INC (tile_variable_for_caching)
&378c 4c 79 37 JMP &3779 ; cache_tile_strip_tiles_loop
; finished_caching_tile_strip_tiles
&378f 68       PLA ; tile coordinate
&3790 aa       TAX
&3791 68       PLA ; tile_strip_variable
&3792 95 00    STA &00,X ; tile_x - &95
&3794 60       RTS

; update_player_angle_facing_and_sprite
&3795 20 94 2c JSR &2c94 ; double_accelerations
&3798 24 c3    BIT &c3 ; every_sixteen_frames
&379a 10 10    BPL &37ac ; skip_recovering_energy                   # Every sixteen frames,
&379c a5 15    LDA &15 ; this_object_energy
&379e 69 04    ADC #&04                                             # Player gains 4 energy
&37a0 b0 02    BCS &37a4 ; is_overflow
&37a2 85 15    STA &15 ; this_object_energy
; is_overflow
&37a4 a2 00    LDX #&00 ; jetpack
&37a6 20 92 2d JSR &2d92 ; check_reliability                        # Reliability of jetpack is tested
&37a9 6e 8a 35 ROR &358a ; player_has_functioning_jetpack           # Set top bit if jetpack is functional, clear if not
; skip_recovering_energy
&37ac a9 10    LDA #&10                                             # Carry is indeterminate here, but it doesn't matter much
&37ae e5 15    SBC &15 ; this_object_energy
&37b0 90 02    BCC &37b4 ; skip_immobilising_player_because_of_damage
&37b2 85 ba    STA &ba ; player_immobility_timers + 0 (movement)    # Make player immobile because of damage
; skip_immobilising_player_because_of_damage
&37b4 a5 ba    LDA &ba ; player_immobility_timers + 0 (movement)
&37b6 c9 06    CMP #&06
&37b8 90 03    BCC &37bd ; skip_disabling_jetpack_because_of_immobility
&37ba 4e 8a 35 LSR &358a ; player_has_functioning_jetpack           # Clear top bit to disable jetpack
; skip_disabling_jetpack_because_of_immobility
&37bd a5 bb    LDA &bb ; player_immobility_timers + 1 (thrusting)
&37bf f0 05    BEQ &37c6 ;not_prevented_from_thrusting
&37c1 c6 bb    DEC &bb ; player_immobility_timers + 1 (thrusting)
&37c3 4e 8a 35 LSR &358a ; player_has_functioning_jetpack           # Clear top bit to disable jetpack
; not_prevented_from_thrusting
&37c6 46 31    LSR &31 ; player_is_lying_down                       # Clear top bit to stop lying down by default
; consider_if_player_is_upright
&37c8 a5 de    LDA &de ; player_angle                               # &c0 if upright
&37ca 38       SEC
&37cb e9 cf    SBC #&cf
&37cd c9 e1    CMP #&e1                                             # Set carry if &b0 <= player_angle < &cf
&37cf 6a       ROR A                                                # Keep &80 set is player is roughly upright
&37d0 25 05    AND &05 ; player_is_upright                          # &80 set at &1adf, cleared if jumping at &3baa
&37d2 85 05    STA &05 ; player_is_upright                          # &80 set if player is upright and hasn't just jumped
&37d4 a5 42    LDA &42 ; this_object_acceleration_y
&37d6 d0 20    BNE &37f8 ; is_accelerating_in_y
&37d8 a5 40    LDA &40 ; this_object_acceleration_x
&37da d0 0a    BNE &37e6 ; is_accelerating_in_x
; consider_if_pinned
&37dc a5 17    LDA &17 ; this_object_surrounded_by_tiles            # &80 set if object surrounded by tiles
&37de 0d b4 19 ORA &19b4 ; this_object_wedged                       # &80 set if object is wedged between obstacles to top and bottom
&37e1 0d 81 12 ORA &1281 ; actions_keys_pressed + &16 (CTRL)        # &80 set if player wants to lie down
&37e4 85 31    STA &31 ; player_is_lying_down                       # If so, set negative to make player to lie down
; is_acelerating_in_x
&37e6 20 8c 3b JSR &3b8c ; check_if_player_or_npc_jumping_or_flying # Returns carry set if jumping or flying
&37e9 90 25    BCC &3810 ; skip_draining_jetpack
&37eb 24 19    BIT &19 ; this_object_any_bottom_collision           # &80 set if collision to bottom, i.e. supported
&37ed 10 09    BPL &37f8 ; no_bottom_collision
&37ef 20 25 32 JSR &3225 ; dampen_this_object_velocity_y
&37f2 20 25 32 JSR &3225 ; dampen_this_object_velocity_y
&37f5 20 25 32 JSR &3225 ; dampen_this_object_velocity_y
; no_bottom_collision
; is_accelerating_in_y
&37f8 2c 8a 35 BIT &358a ; player_has_functioning_jetpack           # Negative if jetpack is functional
&37fb 10 13    BPL &3810 ; skip_draining_jetpack
&37fd 20 3d 1f JSR &1f3d ; add_jetpack_thrust_particles             # Returns zero if not accelerating
&3800 f0 0e    BEQ &3810 ; skip_draining_jetpack
&3802 a5 c4    LDA &c4 ; every_eight_frames
&3804 0d 80 12 ORA &1280 ; action_keys_pressed + &15 (@)            # Negative if jetpack booster being used
&3807 25 c6    AND &c6 ; every_two_frames                           # Drain jetpack four times as quickly if booster used
&3809 10 05    BPL &3810 ; skip_draining_jetpack
&380b a2 00    LDX #&00 ; jetpack
&380d 20 79 2d JSR &2d79 ; reduce_energy_of_weapon_X                # Jetpack uses 1 energy every eight frames
;                                                                   # Boosted jetpack uses 1 energy every two frames
; skip_draining_jetpack
&3810 a5 ba    LDA &ba ; player_immobility_timers + 0 (movement)
&3812 f0 45    BEQ &3859 ; skip_immobility_rotating                 # Is the player immobilised because of damage or collision?
; update_rotating_player                                            # Player rotates when immobile
&3814 c6 ba    DEC &ba ; player_immobility_timers + 0 (movement)
&3816 a5 de    LDA &de ; player_angle
&3818 0a       ASL A
&3819 85 9c    STA &9c ; player_angle_times_two
&381b a5 1e    LDA &1e ; this_object_pre_collision_velocity_angle
&381d 24 1b    BIT &1b ; this_object_tile_top_or_bottom_collision   # &80 set if object hit tiles above or below
&381f 30 08    BMI &3829 ; hit_tiles                                # If so, set rotating off surface
&3821 a5 b9    LDA &b9 ; player_immobility_rotation_velocity
&3823 24 3b    BIT &3b ; this_object_touching                       # Negative if not touching another object
&3825 30 19    BMI &3840 ; not_touching_other_object                # If no, don't add to rotation
; hit_object
&3827 a9 40    LDA #&40 ; &80 (180 degrees) / 2
; hit_tiles                                                         # Determine direction of rotation
&3829 0a       ASL A
&382a 38       SEC
&382b e5 9c    SBC &9c ; player_angle_times_two                     # from difference in angles
&382d 6a       ROR A                                                # Negative if collision to top right or bottom left
;                                                                   # Positive if collision to top left or bottom right
&382e 08       PHP ; sign
&382f a5 1d    LDA &1d ; this_object_pre_collision_velocity_magnitude
&3831 4a       LSR A                                                # Set speed of rotation from player's previous speed
&3832 4a       LSR A
&3833 09 01    ORA #&01
&3835 28       PLP ; sign
&3836 20 56 32 JSR &3256 ; invert_if_negative
&3839 65 b9    ADC &b9 ; player_immobility_rotation_velocity
&383b a0 20    LDY #&20 ; range
&383d 20 5e 32 JSR &325e ; keep_within_range
; not_touching_other_object
&3840 24 c5    BIT &c5 ; every_four_frames                          # Every four frames,
&3842 10 0b    BPL &384f ; set_player_immobility_rotation_velocity
&3844 c9 04    CMP #&04                                             # if absolute player_immobility_rotation_velocity >= 4,
&3846 90 07    BCC &384f ; set_player_immobility_rotation_velocity
&3848 c9 fd    CMP #&fd
&384a b0 03    BCS &384f ; set_player_immobility_rotation_velocity
&384c 20 35 32 JSR &3235 ; calculate_seven_eighths                  # reduce by seven eighths
; set_player_immobility_rotation_velocity
&384f 85 b9    STA &b9 ; player_immobility_rotation_velocity
&3851 18       CLC
&3852 65 de    ADC &de ; player_angle
&3854 85 de    STA &de ; player_angle
&3856 4c b9 38 JMP &38b9 ; skip_setting_facing
; skip_immobility_rotating
; update_player_angle_and_player_facing
&3859 a9 00    LDA #&00
&385b a2 02    LDX #&02
; use_acceleration_as_vector_loop                                   # Loop through X = 2 for y, X = 0 for x
&385d b4 40    LDY &40,X ; this_object_acceleration_x
&385f 94 b4    STY &b4,X ; vector_x
&3861 c0 01    CPY #&01                                             # Set carry if acceleration is non-zero
&3863 2a       ROL A                                                # Set A to non-zero if acceleration is non-zero
&3864 ca       DEX
&3865 ca       DEX
&3866 f0 f5    BEQ &385d ; use_acceleration_as_vector_loop
&3868 aa       TAX                                                  # X = zero if not accelerating
&3869 f0 09    BEQ &3874 ; no_acceleration
&386b 2c 8a 35 BIT &358a ; player_has_functioning_jetpack           # Negative if jetpack is functional
&386e 10 04    BPL &3874 ; no_acceleration
&3870 20 d4 22 JSR &22d4 ; calculate_angle_from_vector
&3873 2c a9 c0 BIT &c0a9 ; (nop)
; no_acceleration                                                   # If the player isn't accelerating,
#3874          LDA #&c0 ; upright                                   # Aim for upright if player isn't lying down
&3876 24 31    BIT &31 ; player_is_lying_down                       # Negative if player is lying down
&3878 10 08    BPL &3882 ; not_lying_down
&387a a9 fd    LDA #&fd ; 0 - ~4 degrees                            # Aim for nearly horizontal if player is lying down
;                                                                   # Face is slightly above feet
&387c 24 df    BIT &df ; player_facing                              # Positive if facing right when upright
&387e 10 02    BPL &3882 ; is_facing_right
; is_facing_left
&3880 a9 83    LDA #&83 ; 180 + ~4 degrees
; is_facing_right
; not_lying_down
&3882 e5 de    SBC &de ; player_angle
&3884 a8       TAY                                                  # Y = deviation of actual angle from default angle
&3885 e0 02    CPX #&02
&3887 d0 0a    BNE &3893 ; not_accelerating_only_vertically
; is_accelerating_only_vertically
&3889 e9 74    SBC #&74
&388b c9 18    CMP #&18
&388d b0 04    BCS &3893 ; not_accelerating_only_vertically         # Branch if &74 <= deviation < &8c, i.e.
;                                                                   #     180 - ~17 degrees <= deviation < 180 + ~17 degrees
&388f a0 00    LDY #&00                                             # Otherwise, set to zero to keep actual angle
&3891 f0 08    BEQ &389b ; set_player_facing_as_needing_update      # Always branches
; not_accelerating_only_vertically
&3893 a5 40    LDA &40 ; this_object_acceleration_x
&3895 f0 04    BEQ &389b ; not_accelerating_horizontally
&3897 24 05    BIT &05 ; player_is_upright                          # Negative if player is upright and hasn't just jumped
&3899 30 02    BMI &389d ; is_upright
; not_accelerating_horizontally
; set_player_facing_as_needing_update
&389b a2 00    LDX #&00                                             # Set to zero to set player_facing at &38b7
; is_upright
&389d 20 8c 3b JSR &3b8c ; check_if_player_or_npc_jumping_or_flying # Returns carry set if not standing
&38a0 98       TYA
&38a1 b0 06    BCS &38a9 ; not_standing
&38a3 24 31    BIT &31 ; player_is_lying_down                       # Negative if player is lying down
&38a5 30 02    BMI &38a9 ; not_standing
&38a7 a9 00    LDA #&00                                             # Don't affect player_angle if player is standing
; not_standing
&38a9 20 78 32 JSR &3278 ; divide_by_four
&38ac 65 de    ADC &de ; player_angle
&38ae 85 de    STA &de ; player_angle                               # &80 set if head above feet
&38b0 45 40    EOR &40 ; this_object_acceleration_x                 # &80 set if accelerating left
&38b2 49 80    EOR #&80
&38b4 ca       DEX                                                  # If not accelerating,
&38b5 30 02    BMI &38b9 ; skip_setting_facing
&38b7 85 df    STA &df ; player_facing                              # Set player facing angle
; skip_setting_facing
&38b9 20 8f 3a JSR &3a8f ; consider_updating_walking_player
&38bc ad 8a 35 LDA &358a ; player_has_functioning_jetpack           # Negative if jetpack is functional
&38bf 30 0b    BMI &38cc ; has_functional_jetpack
&38c1 20 8c 3b JSR &3b8c ; check_if_player_or_npc_jumping_or_flying # Returns carry set if jumping
&38c4 a9 00    LDA #&00
&38c6 85 42    STA &42 ; this_object_acceleration_y                 # No y acceleration from jetpack if not functional
&38c8 90 02    BCC &38cc ; not_jumping
&38ca 85 40    STA &40 ; this_object_acceleration_x                 # No x acceleration if jumping without functional jetpack
; not_jumping
; has_functional_jetpack
&38cc a4 df    LDY &df ; player_facing                              # Positive if facing right when upright
&38ce a5 de    LDA &de ; player_angle
; set_spacesuit_sprite_and_palette                                  # Called with A = angle, Y = x_flip
&38d0 24 30    BIT &30 ; child_object_created                       # Negative if player has just fired a projectile
&38d2 30 05    BMI &38d9 ; skip_setting_sprite
&38d4 84 9e    STY &9e ; x_flip
&38d6 20 06 39 JSR &3906 ; set_spacesuit_sprite_from_angle
; skip_setting_sprite
&38d9 a5 06    LDA &06 ; this_object_frame_counter
&38db 29 1f    AND #&1f
&38dd 0a       ASL A
&38de c5 15    CMP &15 ; this_object_energy                         # Flash for longer when lower on energy
&38e0 08       PHP ; carry set if flashing because of low energy
&38e1 a4 41    LDY &41 ; this_object_type
&38e3 b9 ef 02 LDA &02ef,Y ; object_types_palette_and_pickup_table
&38e6 29 7f    AND #&7f                                             # .4218421 palette
&38e8 a4 41    LDY &41 ; this_object_type
&38ea d0 10    BNE &38fc ; not_player
; is_player
&38ec a2 05    LDX #&05 ; protection suit
&38ee 20 92 2d JSR &2d92 ; check_reliability                        # Returns carry clear if protection suit unreliable
&38f1 6a       ROR A                                                # Set &80 if protection suit reliable
&38f2 2d 13 08 AND &0813 ; player_protection_suit_collected         # &80 set if protection suit collected
&38f5 2a       ROL A                                                # Set carry if protection suit collected and reliable
&38f6 a9 33    LDA #&33 ; rcY                                       # Protected player is red, cyan and yellow
&38f8 b0 02    BCS &38fc ; is_protected
; not_protected
&38fa a9 3e    LDA #&3e ; mwY                                       # Unprotected player is magenta, white and yellow
; is_protected
; not_player
&38fc 28       PLP ; carry set if flashing because of low energy
&38fd 90 04    BCC &3903 ; not_flashing
&38ff a5 73    LDA &73 ; this_object_palette
&3901 49 0b    EOR #&0b ; &3e -> &35 (gyY)                          # Damaged player is green, yellow and yellow regardless
; not_flashing
&3903 85 73    STA &73 ; this_object_palette
&3905 60       RTS

; set_spacesuit_sprite_from_angle                                   # Called with A = angle
&3906 4a       LSR A                                                # 84218421 -> .8421842 1
&3907 4a       LSR A                                                #          -> ..842184 2
&3908 4a       LSR A                                                #          -> ...84218 4
&3909 4a       LSR A                                                #          -> ....8421 8
&390a 4a       LSR A                                                #          -> .....842 1 i.e divide by &20
;                                                                   #                        to give half quadrants
&390b 69 00    ADC #&00                                             # Add carry to rotate boundaries by a quarter of a quadrant
&390d 24 9e    BIT &9e ; x_flip
&390f 30 04    BMI &3915 ; is_flipped
; not_flipped
&3911 49 07    EOR #&07
&3913 69 01    ADC #&01                                             # Carry may or not be set, depending on quarter quadrant
; is_flipped
&3915 48       PHA ; half quadrant
&3916 29 04    AND #&04
&3918 c9 04    CMP #&04                                             # Carry set if head is above feet
&391a 6a       ROR A
&391b 85 37    STA &37 ; this_object_x_flip
&391d 45 9e    EOR &9e ; x_flip
&391f 85 39    STA &39 ; this_object_y_flip
&3921 68       PLA ; half quadrant                          
&3922 29 03    AND #&03                                             # 0 if horizontal            : SPRITE_SPACESUIT_HORIZONTAL
;                                                                   # 1 if 45 degrees, head up   : SPRITE_SPACESUIT_FORTY_FIVE_HEAD_UP
;                                                                   # 2 if vertical
;                                                                   # 3 if 45 degrees, head down : SPRITE_SPACESUIT_FORTY_FIVE_HEAD_DOWN
&3924 c9 02    CMP #&02 ; SPRITE_SPACESUIT_JUMPING
&3926 d0 23    BNE &394b ; to_change_object_sprite_to_A             # If not upright, use angled sprite
; is_upright
&3928 a5 43    LDA &43 ; this_object_velocity_x
&392a 20 56 32 JSR &3256 ; invert_if_negative
&392d 4a       LSR A
&392e f0 18    BEQ &3948 ; use_standing_or_walking_sprite           # Branch if standing
; is_jumping_or_walking
&3930 20 8c 3b JSR &3b8c ; check_if_player_or_npc_jumping_or_flying # Returns carry set if jumping
&3933 a9 02    LDA #&02 ; SPRITE_SPACESUIT_JUMPING
&3935 b0 14    BCS &394b ; to_change_object_sprite_to_A
; is_walking
&3937 a9 08    LDA #&08 ; modulus
&3939 20 55 25 JSR &2555 ; update_sprite_offset_using_velocities    # Returns A = sprite offset
&393c 4a       LSR A
&393d 48       PHA ; walking stage
&393e a5 43    LDA &43 ; this_object_velocity_x                     # &80 set if moving left
&3940 45 37    EOR &37 ; this_object_x_flip                         # &80 set if horizontally flipped, i.e. facing left
&3942 2a       ROL A                                                # Set carry if walking in opposite direction to movement
&3943 68       PLA ; walking stage
&3944 90 02    BCC &3948 ; not_walking_backwards
&3946 49 03    EOR #&03                                             # If so, reverse animation
; not_walking_backwards
; use_standing_or_walking_sprite
&3948 18       CLC
&3949 69 04    ADC #&04 ; SPRITE_SPACESUIT_VERTICAL                 # Use SPRITE_SPACESUIT_STANDING to SPRITE_SPACESUIT_WALKING_THREE
; to_change_object_sprite_to_A
&394b 4c 98 32 JMP &3298 ; change_object_sprite_to_A

# Copy protection check
# =====================
# Pass if:
# permuted_seed ^ expected_word_obfuscated_second_checksum ^ entered_word_second_checksum ^ permuted_rnd_pair = 0
#
# Given:
#     word_number = permuted_seed ^ rnd_pair, so permuted_seed = word_number ^ rnd_pair
#     permuted_seed < &80, so top bit of word_number = top bit of rnd_pair
#     expected_word_obfuscated_second_checksum = expected_word_second_checksum ^ (word_number & &7f) ^ &65
#     permuted_rnd_pair = (rnd_pair & &7f) ^ &65
#
# Pass if:
# word_number ^ rnd_pair ^ expected_word_second_checksum ^ (word_number & &7f) ^ &65 ^ entered_word_second_checksum ^ (rnd_pair & &7f) ^ &65 = 0
# (word_number ^ (word_number & &7f)) ^ (rnd_pair ^ (rnd_pair & &7f)) ^ (&65 ^ &65) ^ expected_word_second_checksum ^ entered_word_second_checksum = 0
# (top bit of word_number) ^ (top bit of rnd_pair) ^ expected_word_second_checksum ^ entered_word_second_checksum = 0
# expected_word_second_checksum ^ entered_word_second_checksum = 0
#
# i.e. if entered word is expected word

; check_copy_protection
&394e a0 44    LDY #&44
&3950 a2 02    LDX #&02
&3952 20 3e 31 JSR &313e ; permute_copy_protection_seed             # Returns A = permuted_seed
; check_copy_protection_loop                                        # Loop through X = &02, Y = &44, &0ba7 ; copy_protection_first_byte
;                                                                   #                       expected_word_obfuscated_second_checksum
&3955 59 63 0b EOR &0b63,Y ; copy_protection_first_byte - &44       #              X = &01, Y = &45, &0ba8 ; copy_protection_second_byte
;                                                                   #                       entered_word_second_checksum
&3958 c8       INY
&3959 ca       DEX
&395a d0 f9    BNE &3955 ; check_copy_protection_loop               
&395c 59 e7 07 EOR &07e7,Y ; copy_protection_third_byte - &46       # permuted_rnd_pair
&395f d0 ed    BNE &394e ; check_copy_protection                    # Infinite loop if values not as expected
&3961 60       RTS

# NPC walking types
# =================
# &00 : player
# &01 : frogmen
# &02 : imps, fluffy
# &03 : green slime
# &04 : robots
# &05 : (unused)
# &06 : worms and maggots

;      0  1  2  3  4  5  6
; npc_walking_types_maximum_angle_table                             # Maximum angle of slope that NPC can walk up
;      0  1  2  3  4  5  6
&3962 32 80 80 20 20 20 80                                          # &20 = 45 degrees, &32 = 70 degrees, &80 = 180 degrees

; npc_walking_types_maximum_acceleration_table
;      0  1  2  3  4  5  6
&3969 06 08 10 03 04 05 08                                          # Value for player is modified

; npc_walking_types_weight_table                                    # Value to reduce speed by two to the power of
;      0  1  2  3  4  5  6
&3970 00 01 01 01 01 01 01

; npc_walking_types_turn_probability_table                          # Probability of NPC turning away from wall or drop
;      0  1  2  3  4  5  6
&3977 ea c8 c8 10 00 00 c8                                          # Not used for player

; npc_walking_types_jump_probability_table                          # Probability of NPC jumping
;      0  1  2  3  4  5  6
&397e ea 40 08 08 00 00 10                                          # Not used for player

; check_for_space_below_object
&3985 a5 3a    LDA &3a ; this_object_width
&3987 4a       LSR A
&3988 65 4f    ADC &4f ; this_object_x_fraction                     # Use horizontal centre of object
&398a 85 87    STA &87 ; test_x_fraction
&398c a5 53    LDA &53 ; this_object_x
&398e 69 00    ADC #&00
&3990 85 95    STA &95 ; tile_x
&3992 a5 49    LDA &49 ; this_object_maximum_y_fraction
&3994 85 89    STA &89 ; test_y_fraction                            # Use bottom of object
&3996 a5 4a    LDA &4a ; this_object_maximum_y
&3998 85 97    STA &97 ; tile_y
&399a d0 29    BNE &39c5 ; check_for_space_at_position              # Always branches; leave with A = amount of space

; check_for_space_to_side_of_object
&399c 20 88 22 JSR &2288 ; get_this_object_centre
&399f a5 87    LDA &87 ; this_object_centre_x_fraction
&39a1 e9 7f    SBC #&7f                                             # Half a tile to left of object's centre
&39a3 85 87    STA &87 ; test_x_fraction
&39a5 a5 8b    LDA &8b ; this_object_centre_x
&39a7 e9 00    SBC #&00
&39a9 85 95    STA &95 ; tile_x
&39ab 20 77 3b JSR &3b77 ; set_this_object_relative_tx_ty           # Returns negative if target is left of object
&39ae 30 02    BMI &39b2 ; is_left_of_target
; is_right_of_target
&39b0 e6 95    INC &95 ; tile_x                                     # If not, use half a tile to right of object's centre
; is_left_of_target
&39b2 a5 4b    LDA &4b ; this_object_sprite_width
&39b4 4a       LSR A                                                # Offset test point downwards by half object width (sic)
&39b5 65 49    ADC &49 ; this_object_maximum_y_fraction             # i.e. wider objects need shallower slopes
&39b7 08       PHP ; tile overflow
&39b8 e9 7f    SBC #&7f                                             # Half a tile above object's bottom
&39ba 85 89    STA &89 ; test_y_fraction
&39bc a5 4a    LDA &4a ; this_object_maximum_y
&39be e9 00    SBC #&00
&39c0 28       PLP ; tile overflow
&39c1 69 00    ADC #&00
&39c3 85 97    STA &97 ; tile_y
; check_for_space_at_position
&39c5 86 ae    STX &ae ; tmp_x
&39c7 a5 87    LDA &87 ; test_x_fraction                            # Convert x fraction to section of tile
&39c9 4a       LSR A
&39ca 4a       LSR A
&39cb 4a       LSR A
&39cc 4a       LSR A
&39cd 4a       LSR A
&39ce a8       TAY
&39cf a9 00    LDA #&00
&39d1 85 83    STA &83 ; unobstructed_space_below                   # Set to zero to indicate no space by default
&39d3 a9 40    LDA #&40 ; TILE_PROCESSING_FLAG_OBSTRUCTION          # Only call tile update routines that want to know
&39d5 85 2d    STA &2d ; tile_processing_mode                       # about tiles being checked for obstruction (doors)
&39d7 a5 89    LDA &89 ; test_y_fraction
&39d9 29 f8    AND #&f8
&39db 09 04    ORA #&04                                             # Round test position to middle of pixel vertically
; check_point_for_obstruction
&39dd 85 81    STA &81 ; y_fraction
&39df 84 9e    STY &9e ; test_x_section
&39e1 20 53 24 JSR &2453 ; set_obstruction_data_variables_for_top_tile
&39e4 a4 9e    LDY &9e ; test_x_section
&39e6 b1 7c    LDA (&7c),Y ; top_tile_obstruction_data_address      # Determine where the tile obstruction begins
&39e8 18       CLC
&39e9 65 7e    ADC &7e ; top_tile_obstruction_y_offset
&39eb 90 02    BCC &39ef ; skip_ceiling
&39ed a9 ff    LDA #&ff                                             # &ff indicates whole tile is clear or obstructed
; skip_ceiling
&39ef aa       TAX
&39f0 c5 81    CMP &81 ; y_fraction                                 # Set carry if test point is above that boundary
&39f2 6a       ROR A                                                # Set &80 if test point is above boundary
&39f3 45 7f    EOR &7f ; top_tile_sprite_and_y_flip                 # &80 set if flipped vertically, i.e. obstruction at top
&39f5 10 22    BPL &3a19 ; is_obstructed                            # Leave if test point is obstructed
; not_obstructed
&39f7 24 7f    BIT &7f ; top_tile_sprite_and_y_flip                 # &80 set if flipped vertically, i.e. obstruction at top
&39f9 10 10    BPL &3a0b ; not_flipped_vertically
; is_flipped_vertically
; consider_checking_bottom_tile
&39fb a5 81    LDA &81 ; y_fraction
&39fd 49 ff    EOR #&ff                                             # Interval between test point and bottom of tile is
&39ff 65 83    ADC &83 ; unobstructed_space_below                   # space; add it the total
&3a01 b0 12    BCS &3a15 ; at_least_a_tile_unobstructed_below       # Leave if that makes the total a tile or more of space
&3a03 85 83    STA &83 ; unobstructed_space_below
&3a05 e6 97    INC &97 ; tile_y                                     # Consider top of bottom tile
&3a07 a9 04    LDA #&04
&3a09 d0 d2    BNE &39dd ; check_point_for_obstruction              # Always branches
; not_flipped_vertically
&3a0b 8a       TXA ; top tile obstruction boundary
&3a0c e8       INX
&3a0d f0 ec    BEQ &39fb ; consider_checking_bottom_tile            # Check bottom tile if top tile was wholly clear
&3a0f e5 81    SBC &81 ; y_fraction                                 # Otherwise, interval between test point and obstruction
&3a11 65 83    ADC &83 ; unobstructed_space_below                   # boundary is space; add it to the total
&3a13 90 02    BCC &3a17 ; skip_ceiling
; at_least_a_tile_unobstructed_below
&3a15 a9 ff    LDA #&ff                                             # Leave with &ff to indicate at least a tile of
;                                                                   # unobstructed space below test point
; skip_ceiling
&3a17 85 83    STA &83 ; unobstructed_space_below
; is_obstructed
&3a19 a6 ae    LDX &ae ; tmp_x
&3a1b a5 83    LDA &83 ; unobstructed_space_below
&3a1d 60       RTS                                                  # Leave with A = amount of unobstructed space below

; consider_hovering_over_ground                                     # Called for flying NPCs
&3a1e 24 c5    BIT &c5 ; every_four_frames
&3a20 10 31    BPL &3a53 ; leave                                    # Every four frames,
&3a22 a5 3c    LDA &3c ; this_object_height
&3a24 49 ff    EOR #&ff
&3a26 4a       LSR A
&3a27 85 8a    STA &8a ; minus_half_height
&3a29 20 85 39 JSR &3985 ; check_for_space_below_object             # Returns A = level amount of unobstructed space below
&3a2c c9 ff    CMP #&ff                                             # &ff if at least a tile of unobstructed space below
&3a2e f0 11    BEQ &3a41 ; accelerate_upwards_once                  # Accelerate upwards if in free space
&3a30 c5 8a    CMP &8a ; minus_half_height
&3a32 b0 1f    BCS &3a53 ; leave                                    # Do nothing if high above surface
&3a34 20 87 25 JSR &2587 ; rnd
&3a37 09 c0    ORA #&c0
&3a39 65 83    ADC &83 ; unobstructed_space_below
&3a3b b0 02    BCS &3a3f ; accelerate_upwards_twice
&3a3d c6 42    DEC &42 ; this_object_acceleration_y                 # Accelerate upwards more nearer to surface
; accelerate_upwards_twice
&3a3f c6 42    DEC &42 ; this_object_acceleration_y
; accelerate_upwards_once
&3a41 c6 42    DEC &42 ; this_object_acceleration_y
&3a43 4c 25 32 JMP &3225 ; dampen_this_object_velocity_y

; check_if_npc_can_continue_walking
&3a46 24 c5    BIT &c5 ; every_four_frames
&3a48 18       CLC                                                  # Leave with carry clear 3 in 4 frames
&3a49 10 08    BPL &3a53 ; leave
&3a4b 20 9c 39 JSR &399c ; check_for_space_to_side_of_object        # Returns amount of unobstructed space
&3a4e 38       SEC
&3a4f f0 02    BEQ &3a53 ; leave                                    # Leave with carry set if no space
;                                                                   #       i.e. NPC is approaching a wall
&3a51 c9 ff    CMP #&ff                                             # Leave with carry set if a tile or more of space
;                                                                   #       i.e. NPC is approaching a drop
; leave
&3a53 60       RTS                                                  # Otherwise, leave with carry clear
;                                                                   #       to indicate NPC has walkable surface ahead of it

; consider_setting_npc_jumping
&3a54 20 86 3b JSR &3b86 ; check_if_npc_can_walk                    # Returns carry set if not on walkable surface
&3a57 b0 fa    BCS &3a53 ; leave
; set_npc_jumping
&3a59 a5 04    LDA &04 ; walking_speed
; set_npc_jumping_with_speed_A
&3a5b a8       TAY ; maximum acceleration                           # A = magnitude, Y = maximum acceleration
&3a5c 86 ae    STX &ae ; tmp_x
&3a5e 20 d8 31 JSR &31d8 ; move_towards_target
&3a61 a6 ae    LDX &ae ; tmp_x
&3a63 a5 42    LDA &42 ; this_object_acceleration_y
&3a65 e9 0a    SBC #&0a
&3a67 85 42    STA &42 ; this_object_acceleration_y
&3a69 18       CLC                                                  # Leave with carry clear to indicate jumping
; to_set_object_jumping_or_flying
&3a6a 4c 7a 2c JMP &2c7a ; set_object_jumping_or_flying

; update_walking_state
&3a6d 24 05    BIT &05 ; player_is_upright                          # Negative if object is not player
;                                                                   # or if player is upright and hasn't just jumped
&3a6f 10 f9    BPL &3a6a ; to_set_object_jumping_or_flying          
&3a71 a5 1b    LDA &1b ; this_object_tile_top_or_bottom_collision   # &80 set if object hit tiles above or below
&3a73 0d e5 29 ORA &29e5 ; this_object_object_collision_y_flags     # &80 set if collision to bottom from other objects
&3a76 38       SEC                                                  # If neither, no surface below object; set carry
&3a77 10 03    BPL &3a7c ; no_collision
&3a79 20 ad 3b JSR &3bad ; check_if_slope_is_too_sleep_for_npc      # Returns carry set if slope is too steep for walking
; no_collision
&3a7c a5 11    LDA &11 ; this_object_state (behaviour and walking)  # Low nibble contains number of frames since player or
&3a7e 29 f0    AND #&f0 ; !NPC_WALKING_MASK                         # NPC was standing on a surface shallow enough to walk
&3a80 90 0a    BCC &3a8c ; set_this_object_state                    # Reset that count if surface is shallow enough to walk
&3a82 45 11    EOR &11 ; this_object_state (behaviour and walking)
&3a84 c9 0f    CMP #&0f
&3a86 b0 e2    BCS &3a6a ; skip_increase                            # If surface is too steep, or there is no surface,
&3a88 e6 11    INC &11 ; this_object_state (behaviour and walking)  # Increase number of frames since viable surface
; skip_increase
&3a8a a5 11    LDA &11 ; this_object_state (behaviour and walking)
; set_this_object_state
&3a8c 85 11    STA &11 ; this_object_state (behaviour and walking)
&3a8e 60       RTS

; consider_updating_walking_player
&3a8f a9 1f    LDA #&1f                                             # Player walking speed
&3a91 85 04    STA &04 ; walking_speed
&3a93 a5 07    LDA &07 ; this_object_frame_counter_sixteen
&3a95 c9 02    CMP #&02                                             # Set carry 14 frames every 16
&3a97 a5 38    LDA &38 ; this_object_weight                         # i.e. allow occasional unimpeded acceleration
&3a99 e9 05    SBC #&05
&3a9b a8       TAY
&3a9c 90 1f    BCC &3abd ; skip_reducing_acceleration
; consider_reducing_walking_acceleration_because_of_weight
&3a9e 20 8c 3b JSR &3b8c ; check_if_player_or_npc_jumping_or_flying # Returns carry set if jumping or flying
&3aa1 90 1a    BCC &3abd ; skip_reducing_acceleration
&3aa3 24 19    BIT &19 ; this_object_any_bottom_collision           # &80 set if collision to bottom, i.e. supported
&3aa5 30 16    BMI &3abd ; skip_reducing_acceleration
; reduce_walking_acceleration_because_of_weight_loop                # Holding a heavy object makes it harder to jump or fly
&3aa7 a2 02    LDX #&02
; reduce_walking_acceleration_because_of_weight_component_loop      # Loop through X = 2 for y, X = 0 for x
&3aa9 b5 40    LDA &40,X ; this_object_acceleration_x
&3aab c9 80    CMP #&80                                             # Halve acceleration, keeping sign
&3aad 6a       ROR A
&3aae 10 02    BPL &3ab2 ; not_negative
&3ab0 69 00    ADC #&00
; not_negative
&3ab2 95 40    STA &40,X ; this_object_acceleration_x
&3ab4 ca       DEX
&3ab5 ca       DEX
&3ab6 f0 f1    BEQ &3aa9 ; reduce_walking_acceleration_because_of_weight_component_loop
&3ab8 88       DEY
&3ab9 10 ec    BPL &3aa7 ; reduce_walking_acceleration_because_of_weight_loop
&3abb 30 4e    BMI &3b0b ; update_walking_npc_or_player             # Always branches; called X = 0
; skip_reducing_acceleration
&3abd a9 0f    LDA #&0f                                             # Player maximum acceleration when walking
&3abf c8       INY ; weight
; reduce_maximum_acceleration_because_of_weight_loop
&3ac0 4a       LSR A                                                # Halve maximum acceleration for every unit of weight
&3ac1 88       DEY
&3ac2 10 fc    BPL &3ac0 ; reduce_maximum_acceleration_because_of_weight_loop
&3ac4 69 01    ADC #&01
&3ac6 a4 40    LDY &40 ; this_object_acceleration_x
&3ac8 84 d2    STY &d2 ; this_object_relative_tx                    # Set player's target to match walking direction
&3aca d0 04    BNE &3ad0 ; skip_floor
&3acc 84 04    STY &04 ; walking_speed                              # Set to zero if player isn't walking left or right
&3ace a9 01    LDA #&01
; skip_floor
&3ad0 8d 69 39 STA &3969 ; npc_walking_types_maximum_acceleration_table + 0 (player)
&3ad3 a2 00    LDX #&00 ; npc walking type
&3ad5 20 8c 3b JSR &3b8c ; check_if_player_or_npc_jumping_or_flying # Returns carry set if jumping or flying
&3ad8 b0 02    BCS &3adc ; to_update_walking_npc_or_player
&3ada 86 40    STX &40 ; this_object_acceleration_x                 # Set to zero to stop thrusting when walking
; to_update_walking_npc_or_player
&3adc 4c 0b 3b JMP &3b0b ; update_walking_npc_or_player             # Called with X = 0, npc walking type for player

; update_walking_npc_and_check_for_obstacles_with_speed_A           # Called with X = npc walking type
&3adf 85 04    STA &04 ; walking_speed
; update_walking_npc_and_check_for_obstacles                        # Called with A = speed, X = npc walking type
&3ae1 20 08 3b JSR &3b08 ; update_walking_npc
&3ae4 20 46 3a JSR &3a46 ; check_if_npc_can_continue_walking        # Returns carry clear if NPC can continue walking
&3ae7 90 13    BCC &3afc ; skip_moving_away_from_target
; consider_moving_away_from_target                                  # Otherwise, the NPC is facing a wall or a drop
&3ae9 20 87 25 JSR &2587 ; rnd
&3aec dd 77 39 CMP &3977,X ; npc_walking_types_turn_probability_table
&3aef 90 13    BCC &3b04 ; to_consider_setting_npc_jumping
&3af1 a9 01    LDA #&01
&3af3 24 d2    BIT &d2 ; this_object_relative_tx
&3af5 20 4c 32 JSR &324c ; invert_if_positive                       # Use +1 if tx < x, -1 if tx >= x
&3af8 65 53    ADC &53 ; this_object_x                              # i.e. move away from target in x
&3afa 85 14    STA &14 ; this_object_tx
; skip_moving_away_from_target
&3afc 20 87 25 JSR &2587 ; rnd
&3aff dd 7e 39 CMP &397e,X ; npc_walking_types_jump_probability_table
&3b02 b0 03    BCS &3b07 ; leave
; to_consider_setting_npc_jumping
&3b04 4c 54 3a JMP &3a54 ; consider_setting_npc_jumping             # Leaves with carry clear if jumping, set if not jumping
; leave
&3b07 60       RTS                                                  # Leave with carry set to indicate not jumping

; update_walking_npc                                                # Called with X = npc walking type
&3b08 20 77 3b JSR &3b77 ; set_this_object_relative_tx_ty
; update_walking_npc_or_player                                      # Called with X = 0 for player
&3b0b 20 6d 3a JSR &3a6d ; update_walking_state                     # Returns this_object_state
&3b0e 29 0f    AND #&0f ; NPC_WALKING_MASK
&3b10 d0 73    BNE &3b85 ; leave                                    # Zero if NPC on surface shallow enough to walk on
&3b12 bc 69 39 LDY &3969,X ; npc_walking_types_maximum_acceleration_table
&3b15 84 9c    STY &9c ; maximum_acceleration
&3b17 bc 70 39 LDY &3970,X ; npc_walking_types_weight_table         # Used as weight for apply_weight_and_limit_to_velocity
&3b1a 20 ad 3b JSR &3bad ; check_if_slope_is_too_sleep_for_npc      # Returns A = absolute tile_collision_angle
&3b1d e9 2c    SBC #&2c
&3b1f c9 28    CMP #&28                                             # Clear carry if &2c <= angle < &54
&3b21 a5 04    LDA &04 ; walking_speed                              #                ~61 degrees <= angle < ~118 degrees
&3b23 90 36    BCC &3b5b ; climb_steep_slope                        # i.e. if slope is greater than 60 degrees to horizontal
; walk_along_flat_or_shallow_slope
&3b25 24 d2    BIT &d2 ; this_object_relative_tx
&3b27 20 56 32 JSR &3256 ; invert_if_negative                       # Turn into velocity based on x direction to target
&3b2a 38       SEC
&3b2b e5 43    SBC &43 ; this_object_velocity_x
&3b2d 20 01 32 JSR &3201 ; apply_weight_and_limit_to_acceleration   # Returns A = acceleration
&3b30 a8       TAY ; acceleration
&3b31 29 80    AND #&80                                             # Set &80 if moving left
&3b33 45 1c    EOR &1c ; tile_collision_angle                       # &00 if collision directly beneath, &40 to left, &c0 to right
&3b35 69 40    ADC #&40
&3b37 0a       ASL A                                                # Set carry if moving left relative to surface
&3b38 a5 d2    LDA &d2 ; this_object_relative_tx
&3b3a d0 02    BNE &3b3e ; not_at_target_horizontally
&3b3c a0 00    LDY #&00                                             # Set to zero to stop accelerating when at target
; not_at_target_horizontally
&3b3e a9 10    LDA #&10 ; 22.5 degrees                              # To right, slightly into surface to keep contact
&3b40 90 02    BCC &3b44 ; is_moving_right
; is_moving_left
&3b42 a9 6f    LDA #&6f ; ~180 - 22.5 degrees                       # To left, slightly into surface to keep contact
; is_moving_right
&3b44 65 1c    ADC &1c ; tile_collision_angle
&3b46 85 b5    STA &b5 ; angle
&3b48 98       TYA ; acceleration
&3b49 20 56 32 JSR &3256 ; invert_if_negative
&3b4c 20 57 23 JSR &2357 ; calculate_vector_from_magnitude_and_angle # Returns A = vector_y
&3b4f 85 42    STA &42 ; this_object_acceleration_y                 # Accelerate NPC along flat or shallow slope
&3b51 a5 b4    LDA &b4 ; vector_x
&3b53 85 40    STA &40 ; this_object_acceleration_x
&3b55 20 25 32 JSR &3225 ; dampen_this_object_velocity_y
&3b58 4c 25 32 JMP &3225 ; dampen_this_object_velocity_y
; climb_steep_slope                                                 # A = walking_speed
&3b5b 24 d4    BIT &d4 ; this_object_relative_ty
&3b5d 20 56 32 JSR &3256 ; invert_if_negative                       # Turn into velocity based on y direction to target
&3b60 a2 02    LDX #&02 ; y
&3b62 20 f6 31 JSR &31f6 ; apply_weighted_acceleration_to_this_object_velocity
&3b65 a9 08    LDA #&08
&3b67 24 1c    BIT &1c ; tile_collision_angle                       # Positive if surface is to left, negative if to right
&3b69 20 4c 32 JSR &324c ; invert_if_positive                       # -8 if to left, 8 to right
&3b6c 85 40    STA &40 ; this_object_acceleration_x                 # i.e. accelerate slightly into surface to keep contact
&3b6e 20 2d 32 JSR &322d ; dampen_this_object_velocity_x
&3b71 20 2d 32 JSR &322d ; dampen_this_object_velocity_x
&3b74 4c 2d 32 JMP &322d ; dampen_this_object_velocity_x

; set_this_object_relative_tx_ty
&3b77 a5 16    LDA &16 ; this_object_ty
&3b79 18       CLC
&3b7a e5 55    SBC &55 ; this_object_y
&3b7c 85 d4    STA &d4 ; this_object_relative_ty
&3b7e a5 14    LDA &14 ; this_object_tx
&3b80 38       SEC
&3b81 e5 53    SBC &53 ; this_object_x
&3b83 85 d2    STA &d2 ; this_object_relative_tx
&3b85 60       RTS

; check_if_npc_can_walk
&3b86 20 8c 3b JSR &3b8c ; check_if_player_or_npc_jumping_or_flying # Returns frames since standing on walkable surface
&3b89 c9 01    CMP #&01                                             # Leave with carry clear if NPC can walk
&3b8b 60       RTS                                                  #            A = this_object_state & NPC_WALKING_MASK

; check_if_player_or_npc_jumping
&3b8c a5 11    LDA &11 ; this_object_state (behaviour and walking)  # Low nibble contains number of frames since player or
&3b8e 29 0f    AND #&0f ; NPC_WALKING_MASK                          # NPC was standing on a surface shallow enough to walk
&3b90 c9 0a    CMP #&0a                                             # Leave with carry set if jumping or flying
&3b92 60       RTS                                                  #            A = this_object_state & NPC_WALKING_MASK

; handle_jumping
&3b93 20 8c 3b JSR &3b8c ; check_if_player_or_npc_jumping
&3b96 c9 05    CMP #&05
&3b98 b0 32    BCS &3bcc ; leave                                    # Can't jump if not recently on walkable surface
&3b9a a9 f6    LDA #&f6                                             # Use upward velocity of (&0a - weight) *2
&3b9c 2c 80 12 BIT &1280 ; action_keys_pressed + &15 (@)            # Negative if jetpack booster being used
&3b9f 10 02    BPL &3ba3 ; not_boosting
&3ba1 a9 f0    LDA #&f0                                             # or (&10 - weight) * 2, if booster is used
; not_boosting
&3ba3 65 38    ADC &38 ; this_object_weight
&3ba5 0a       ASL A
&3ba6 65 45    ADC &45 ; this_object_velocity_y
&3ba8 85 45    STA &45 ; this_object_velocity_y
&3baa 46 05    LSR &05 ; player_is_upright                          # Clear &80 to indicate jumping
&3bac 60       RTS

; check_if_slope_is_too_sleep_for_npc
&3bad a5 1c    LDA &1c ; tile_collision_angle
&3baf 20 56 32 JSR &3256 ; invert_if_negative
&3bb2 dd 62 39 CMP &3962,X ; npc_walking_types_maximum_angle_table  # Leave with carry set if slope is too steep
&3bb5 60       RTS                                                  # Leave with A = absolute tile_collision_angle

; get_maximum_of_this_object_velocities
&3bb6 a5 45    LDA &45 ; this_object_velocity_y
&3bb8 20 56 32 JSR &3256 ; invert_if_negative
&3bbb a8       TAY
&3bbc a5 43    LDA &43 ; this_object_velocity_x
&3bbe 20 56 32 JSR &3256 ; invert_if_negative
; get_maximum_of_A_and_Y
&3bc1 84 9d    STY &9d ; tmp
&3bc3 c5 9d    CMP &9d ; tmp
&3bc5 b0 05    BCS &3bcc ; leave
&3bc7 a8       TAY
&3bc8 a5 9d    LDA &9d ; tmp
&3bca 84 9d    STY &9d ; tmp
; leave
&3bcc 60       RTS                                                  # Leave with A = larger velocity

; get_sign
&3bcd 0a       ASL A
&3bce a9 ff    LDA #&ff                                             # Leave with &ff if A was negative
&3bd0 b0 fa    BCS &3bcc ; leave
&3bd2 a9 01    LDA #&01                                             # Leave with &01 if A was positive
&3bd4 60       RTS

; check_object_touching_angle
&3bd5 a6 3b    LDX &3b ; this_object_touching                       # Negative if not touching another object
&3bd7 30 07    BMI &3be0 ; leave
&3bd9 20 a0 22 JSR &22a0 ; calculate_angle_of_object_X_to_this_object
&3bdc 69 40    ADC #&40 ; 90 degrees                                # Allow 90 degrees to either side of direct line
&3bde 45 37    EOR &37 ; this_object_x_flip                         # &80 if this object is flipped horizontally
; leave
&3be0 60       RTS                                                  # Leave with positive if at angle that can be picked up

; consider_absorbing_object_touched                                 # Called with A = object type to absorb
&3be1 a4 3b    LDY &3b ; this_object_touching
&3be3 d9 60 08 CMP &0860,Y ; objects_type                           # Is the touched object the right type?
&3be6 d0 0f    BNE &3bf7 ; leave                                    # Leave with non-zero to indicate object not absorbed
&3be8 20 d5 3b JSR &3bd5 ; check_object_touching_angle              # Returns positive if object at a viable angle
&3beb 30 0a    BMI &3bf7 ; leave
&3bed a4 3b    LDY &3b ; this_object_touching
&3bef 20 16 25 JSR &2516 ; set_object_for_removal
&3bf2 20 ad 14 JSR &14ad ; play_low_beep                            # Play low beep to indicate object absorbed
&3bf5 a9 00    LDA #&00                                             # Leave with zero to indicate object absorbed
; leave
&3bf7 60       RTS

; consider_finding_target                                           # Called with A = object type, Y = object range
&3bf8 a6 07    LDX &07 ; this_object_frame_counter_sixteen
&3bfa e0 0f    CPX #&0f                                             # Every sixteen frames,
&3bfc 30 19    BMI &3c17 ; leave
; find_a_target
&3bfe 20 2a 3c JSR &3c2a ; find_object                              # Returns positive if object found
&3c01 30 14    BMI &3c17 ; leave
&3c03 86 0e    STX &0e ; this_object_target_object
&3c05 a9 40    LDA #&40 ; TARGET_FLAG_DIRECTNESS_ONE                # Will be upgraded when NPC next updates path
&3c07 d0 0c    BNE &3c15 ; set_this_object_target_object_and_flags  # Always branches

; avoid_fireballs
&3c09 a9 37    LDA #&37 ; OBJECT_FIREBALL
&3c0b a8       TAY
; avoid_object_type_Y
&3c0c 20 f8 3b JSR &3bf8 ; consider_finding_target                  # Returns positive if object found
&3c0f 30 06    BMI &3c17 ; leave
; avoid_target
&3c11 a5 3e    LDA &3e ; this_object_target_object_and_flags
&3c13 09 20    ORA #&20 ; TARGET_FLAG_AVOIDING
; set_this_object_target_object_and_flags
&3c15 85 3e    STA &3e ; this_object_target_object_and_flags
; leave
&3c17 60       RTS

; count_objects_of_type_A                                           # Called with A = object type
&3c18 a0 7f    LDY #&7f                                             # Positive to suppress checking object ranges
&3c1a 38       SEC                                                  # Set carry to count
&3c1b 20 30 3c JSR &3c30 ; find_or_count_objects
&3c1e a4 9f    LDY &9f ; count                                      # Leave with Y = count
&3c20 60       RTS

; find_nearest_object_probabilities_table                           # Probabilities of considering object as find
;      0  1  2  3                                                   # &00 : 1 in 2 chance if two secondary types found
&3c21 80 ff 20 80                                                   # &01 : 1 in 1 chance if one primary type found
                                                                    # &02 : 1 in 8 chance if one primary, one secondary
                                                                    # &03 : 1 in 2 chance if two primary types found

; nearest_object
&3c25 00

; primary_type
&3c26 00

; secondary_type_or_range
&3c27 00

; nearest_object_distance
&3c28 00

; primary_type_includes_player
&3c29 00

; find_object                                                       # Called with A = primary object type to find
;                                                                   #                 &80 set to include player
;                                                                   #             Y = secondary object type to find
;                                                                   #                 &80 set to find object range
&3c2a 18       CLC                                                  # Clear carry to consider obstructions
&3c2b 24 38    BIT &38 ; (nop)
; find_object_ignoring_obstructions
#3c2c          SEC                                                  # Set carry to consider only distances, not obstruction
&3c2d 66 9b    ROR &9b ; finding_mode              
&3c2f 18       CLC                                                  # Clear carry to consider obstruction
; find_or_count_objects
&3c30 66 a1    ROR &a1 ; finding_or_counting
&3c32 8d 29 3c STA &3c29 ; primary_type_includes_player
&3c35 29 7f    AND #&7f
&3c37 8d 26 3c STA &3c26 ; primary_type
&3c3a 8c 27 3c STY &3c27 ; secondary_type_or_range
&3c3d a2 ff    LDX #&ff
&3c3f 8e 25 3c STX &3c25 ; nearest_object                           # Set to negative to indicate no object by default
&3c42 8e 28 3c STX &3c28 ; nearest_object_distance
&3c45 e8       INX ; 0
&3c46 86 a0    STX &a0 ; finds
&3c48 86 9f    STX &9f ; count
&3c4a 20 87 25 JSR &2587 ; rnd
&3c4d 29 0f    AND #&0f
&3c4f 85 a3    STA &a3 ; random_shuffle
&3c51 a0 0f    LDY #&0f
; find_or_count_objects_loop                                        # For each object, shuffled randomly,
&3c53 84 9e    STY &9e ; object_to_check
&3c55 98       TYA
&3c56 45 a3    EOR &a3 ; random_shuffle
&3c58 a8       TAY
&3c59 b9 b4 08 LDA &08b4,Y ; objects_y                              # Zero if no object in slot
&3c5c f0 6b    BEQ &3cc9 ; consider_next_object
&3c5e c4 aa    CPY &aa ; this_object
&3c60 f0 67    BEQ &3cc9 ; consider_next_object                     # Don't consider this object
&3c62 b9 60 08 LDA &0860,Y ; objects_type
&3c65 d0 05    BNE &3c6c ; not_player
; is_player
&3c67 2c 29 3c BIT &3c29 ; primary_type_includes_player             # Negative to include player
&3c6a 30 05    BMI &3c71 ; is_suitable_type
; not_player
&3c6c cd 26 3c CMP &3c26 ; primary_type
&3c6f d0 06    BNE &3c77 ; not_suitable_type
; is_suitable_type
&3c71 a5 a0    LDA &a0 ; finds
&3c73 09 01    ORA #&01                                             # Set &01 to indicate primary type found
&3c75 d0 14    BNE &3c8b ; set_finds                                # Always branches
; not_suitable_type
&3c77 2c 27 3c BIT &3c27 ; secondary_type_or_range                  # Negative to check object ranges
&3c7a 10 06    BPL &3c82 ; skip_checking_range
&3c7c 20 b0 2d JSR &2db0 ; get_range_for_object_type_A              # Returns X = range
&3c7f 8a       TXA
&3c80 09 80    ORA #&80                                             # Set &80 to consider range
; skip_checking_range
&3c82 cd 27 3c CMP &3c27 ; secondary_type_or_range
&3c85 d0 42    BNE &3cc9 ; consider_next_object
&3c87 a5 a0    LDA &a0 ; finds
&3c89 29 02    AND #&02                                             # Clear &01 to indicate secondary type found
; set_finds
&3c8b 29 03    AND #&03
&3c8d 85 a0    STA &a0 ; finds
&3c8f aa       TAX
&3c90 e6 9f    INC &9f ; count
&3c92 24 a1    BIT &a1 ; finding_or_counting                        # Top bit set if counting objects
&3c94 30 33    BMI &3cc9 ; consider_next_object                     # If so, consider all objects
; is_finding                                                        # A matching object has been found
&3c96 20 87 25 JSR &2587 ; rnd
&3c99 dd 21 3c CMP &3c21,X ; find_nearest_object_probabilities_table
&3c9c b0 2b    BCS &3cc9 ; consider_next_object
&3c9e 98       TYA
&3c9f aa       TAX
&3ca0 24 9b    BIT &9b ; finding_mode                               # Negative if ignoring obstructions
&3ca2 10 0e    BPL &3cb2 ; is_considering_obstructions
; is_ignoring_obstructions
&3ca4 a9 00    LDA #&00                                             # Zero to not check for obstructions
&3ca6 20 9c 35 JSR &359c ; check_for_obstruction_between_objects    # Determine distance to object
&3ca9 a5 83    LDA &83 ; distance
&3cab cd 28 3c CMP &3c28 ; nearest_object_distance                  # Is this object nearer than the previous find?
&3cae 90 11    BCC &3cc1 ; use_as_nearest_object
&3cb0 b0 17    BCS &3cc9 ; consider_next_object                     # Always branches
; is_considering_obstructions
&3cb2 20 87 25 JSR &2587 ; rnd
&3cb5 29 4f    AND #&4f                                             # Use random distance up to 10 tiles
&3cb7 4d 28 3c EOR &3c28 ; nearest_object_distance
&3cba 20 9c 35 JSR &359c ; check_for_obstruction_between_objects    # Returns carry clear if no obstructions within distance
&3cbd b0 0a    BCS &3cc9 ; consider_next_object
&3cbf a5 83    LDA &83 ; distance
; use_as_nearest_object
&3cc1 8d 28 3c STA &3c28 ; nearest_object_distance
&3cc4 06 a0    ASL &a0 ; finds                                      # Carry is clear here; ......21 -> .....210
&3cc6 8e 25 3c STX &3c25 ; nearest_object
; consider_next_object
&3cc9 a4 9e    LDY &9e ; object_to_check
&3ccb 88       DEY
&3ccc 10 85    BPL &3c53 ; find_or_count_objects_loop
&3cce 46 a0    LSR &a0 ; finds                                      # ......21 -> .......2 1
&3cd0 46 a0    LSR &a0 ; finds                                      #          -> ........ 2
&3cd2 ae 25 3c LDX &3c25 ; nearest_object                           # Leave with carry set if last find was primary type 
&3cd5 60       RTS                                                  # Leave with X = nearest object

; check_if_object_is_at_target
&3cd6 a5 16    LDA &16 ; this_object_ty
&3cd8 c5 55    CMP &55 ; this_object_y
&3cda d0 04    BNE &3ce0 ; leave                                    # Leave with not equal if object not at target
&3cdc a5 14    LDA &14 ; this_object_tx
&3cde c5 53    CMP &53 ; this_object_x
; leave
&3ce0 60       RTS                                                  # Leave with equal if object at target

; route_angle_randomness
&3ce1 00

; route_angle_base
&3ce2 00

; route_best_distance
&3ce3 00

; route_best_angle
&3ce4 00

; route_attempts_remaining
&3ce5 00

; check_if_object_has_target
&3ce6 a6 0e    LDX &0e ; this_object_target_object
&3ce8 bd b4 08 LDA &08b4,X ; objects_y                              # Zero if no object in slot
&3ceb d0 06    BNE &3cf3 ; is_present
; not_present                                                       # If the target is no longer present,
&3ced a6 aa    LDX &aa ; this_object
&3cef 86 0e    STX &0e ; this_object_target_object                  # set this object's target to be itself, i.e. no target
&3cf1 86 3e    STX &3e ; this_object_target_object_and_flags
; is_present
&3cf3 e4 aa    CPX &aa ; this_object                                # Leave with not equal if object has target
&3cf5 60       RTS

; consider_if_npc_can_see_target
&3cf6 a5 07    LDA &07 ; this_object_frame_counter_sixteen          # Every sixteen frames,
&3cf8 d0 2b    BNE &3d25 ; leave
&3cfa 20 e6 3c JSR &3ce6 ; check_if_object_has_target               # Returns not equal if object has target
&3cfd f0 37    BEQ &3d36 ; reduce_targeting_directness
&3cff 20 9a 35 JSR &359a ; check_for_obstruction_between_objects_80 # Returns carry set if no line of sight
&3d02 b0 19    BCS &3d1d ; is_unable_to_see_target
; is_able_to_see_target                                             # If NPC has target and it can see it,
&3d04 a5 3e    LDA &3e ; this_object_target_object_and_flags        # set maximum directness to head there directly
&3d06 09 c0    ORA #&c0 ; TARGET_FLAG_DIRECTNESS_TWO | TARGET_FLAG_DIRECTNESS_ONE
&3d08 85 3e    STA &3e ; this_object_target_object_and_flags
&3d0a 29 20    AND #&20 ; TARGET_FLAG_AVOIDING
&3d0c d0 03    BNE &3d11 ; is_avoiding
&3d0e 4c 83 28 JMP &2883 ; set_this_object_tx_ty_from_object_X_x_y  # Use target object to set target position
; is_avoiding                                                       # unless avoiding,
&3d11 20 a0 22 JSR &22a0 ; calculate_angle_of_object_X_to_this_object
&3d14 49 80    EOR #&80 ; 180 degrees                               # in which case, head in opposite direction
&3d16 85 b5    STA &b5 ; angle
&3d18 a9 7f    LDA #&7f ; ~180 degrees
&3d1a 4c a7 3d JMP &3da7 ; find_route_to_target_with_angle_range_A
; is_unable_to_see_target
&3d1d a5 3e    LDA &3e ; this_object_target_object_and_flags        # If TARGET_FLAG_DIRECTNESS_TWO set,
&3d1f 10 04    BPL &3d25 ; leave
&3d21 29 bf    AND #&bf ; !TARGET_FLAG_DIRECTNESS_ONE               # clear TARGET_FLAG_DIRECTNESS_ONE
&3d23 85 3e    STA &3e ; this_object_target_object_and_flags        # i.e. drop from directness level three to two
; leave
&3d25 60       RTS

; consider_updating_npc_path
&3d26 20 f6 3c JSR &3cf6 ; consider_if_npc_can_see_target           # Set directness flags depending on obstruction
&3d29 24 3e    BIT &3e ; this_object_target_object_and_flags
&3d2b 30 04    BMI &3d31 ; use_direct_path                          # Use direct path if directness levels two or three
&3d2d 50 39    BVC &3d68 ; use_relaxed_path                         # Use relaxed path if level zero
&3d2f 70 0f    BVS &3d40 ; use_slightly_relaxed_path                # Use slightly relaxed path if level one; always branches

; use_direct_path                                                   # If directness levels two or three,
&3d31 20 98 3d JSR &3d98 ; check_if_npc_path_update_needed_ignoring_collisions # Returns zero every 8 or 64 frames
&3d34 d0 09    BNE &3d3f ; leave                                    # Otherwise, continue along path towards target
; reduce_targeting_directness
&3d36 38       SEC
&3d37 a5 3e    LDA &3e ; this_object_target_object_and_flags
&3d39 e9 40    SBC #&40 ; TARGET_FLAG_DIRECTNESS_ONE                # Reduce directness by a level
&3d3b 90 02    BCC &3d3f ; leave
&3d3d 85 3e    STA &3e ; this_object_target_object_and_flags
; leave
&3d3f 60       RTS

; use_slightly_relaxed_path                                         # If directness level one,
&3d40 20 87 25 JSR &2587 ; rnd
&3d43 29 03    AND #&03
&3d45 f0 21    BEQ &3d68 ; use_relaxed_path                         # 1 in 4 chance of using relaxed path
&3d47 4a       LSR A
&3d48 05 da    ORA &da ; rnd_state + 1
&3d4a f0 ea    BEQ &3d36 ; lessen_targeting_enthusiasm              # 1 in 512 chance of dropping to level zero
&3d4c 20 94 3d JSR &3d94 ; check_if_npc_path_update_needed          # Returns zero every 8 or 64 frames
&3d4f d0 53    BNE &3da4 ; leave
&3d51 a6 0e    LDX &0e ; this_object_target_object
&3d53 a9 20    LDA #&20 ; magnitude
&3d55 20 47 33 JSR &3347 ; use_vector_between_object_centres
&3d58 a5 3e    LDA &3e ; this_object_target_object_and_flags
&3d5a 29 20    AND #&20 ; TARGET_FLAG_AVOIDING
&3d5c f0 06    BEQ &3d64 ; not_avoiding                             # Otherwise use a direct path towards target
; is_avoiding
&3d5e a5 b5    LDA &b5 ; angle
&3d60 49 80    EOR #&80 ; 180 degrees                               # or away from target, if avoiding
&3d62 85 b5    STA &b5 ; angle
; not_avoiding
&3d64 a9 3f    LDA #&3f ; 90 degrees
&3d66 d0 3f    BNE &3da7 ; find_route_to_target_with_angle_range_A  # Always branches

; use_relaxed_path                                                  # If directness level zero,
&3d68 20 94 3d JSR &3d94 ; check_if_npc_path_update_needed          # Returns zero every 8 or 64 frames
&3d6b d0 37    BNE &3da4 ; leave
&3d6d 20 87 25 JSR &2587 ; rnd
&3d70 29 07    AND #&07
&3d72 e9 03    SBC #&03
&3d74 65 43    ADC &43 ; this_object_velocity_x                     # Perturb x velocity slightly
&3d76 85 b4    STA &b4 ; vector_x
&3d78 a5 d9    LDA &d9 ; rnd_state
&3d7a 29 07    AND #&07
&3d7c e9 03    SBC #&03
&3d7e 65 45    ADC &45 ; this_object_velocity_y                     # Perturb y velocity slightly
&3d80 20 d2 22 JSR &22d2 ; calculate_angle_from_vector_x_and_A      # Use perturbed velocities to generate an angle
&3d83 a9 ff    LDA #&ff ; ~360 degrees
&3d85 a6 da    LDX &da ; rnd_state + 1
&3d87 e0 08    CPX #&08                                             # 1 in 32 chance of using 360 degree range
&3d89 90 06    BCC &3d91 ; to_find_route_to_target_with_angle_range_A
&3d8b 4a       LSR A ; &7f ; ~180 degrees                           # 7 in 32 chance of using 180 degree range
&3d8c e0 40    CPX #&40
&3d8e 90 01    BCC &3d91 ; to_find_route_to_target_with_angle_range_A
&3d90 4a       LSR A ; &3f ; ~90 degrees                            # 3 in 4 chance of using 90 degree range
; to_find_route_to_target_with_angle_range_A
&3d91 4c a7 3d JMP &3da7 ; find_route_to_target_with_angle_range_A

; check_if_npc_path_update_needed
&3d94 24 1b    BIT &1b ; this_object_tile_top_or_bottom_collision   # &80 set if object hit tiles above or below
&3d96 30 07    BMI &3d9f ; leave_with_zero_every_eight_frames       # Every 8 frames, if hit tiles above or below
; check_if_npc_path_update_needed_ignoring_collisions
&3d98 a0 3f    LDY #&3f                                             # Every 64 frames, if not at target
&3d9a 20 d6 3c JSR &3cd6 ; check_if_object_is_at_target             # Returns equal if object is at target
&3d9d d0 02    BNE &3da1 ; not_at_target
; leave_with_zero_every_eight_frames
&3d9f a0 07    LDY #&07                                             # Every 8 frames, if at target
; not_at_target
&3da1 98       TYA
&3da2 24 06    BIT &06 ; this_object_frame_counter
; leave
&3da4 60       RTS                                                  # Leave with zero if update needed

; find_route_to_target                                              # Unused entry point
&3da5 a9 ff    LDA #&ff
; find_route_to_target_with_angle_range_A                           # Called with A = range of angles to consider
&3da7 8d e1 3c STA &3ce1 ; route_angle_randomness
&3daa 4a       LSR A
&3dab 85 9d    STA &9d ; half_randomness
&3dad a5 b5    LDA &b5 ; angle
&3daf e5 9d    SBC &9d ; half_randomness
&3db1 8d e2 3c STA &3ce2 ; route_angle_base
&3db4 a9 04    LDA #&04
&3db6 8d e5 3c STA &3ce5 ; route_attempts_remaining
&3db9 8d e3 3c STA &3ce3 ; route_best_distance
; find_route_to_target_loop                                         # For each of four attempts,
&3dbc 20 87 25 JSR &2587 ; rnd
&3dbf 2d e1 3c AND &3ce1 ; route_angle_randomness                   # Pick a random angle with the range
&3dc2 6d e2 3c ADC &3ce2 ; route_angle_base
&3dc5 85 b5    STA &b5 ; angle
&3dc7 48       PHA ; angle_to_check
&3dc8 a9 20    LDA #&20 ; magnitude
&3dca 20 57 23 JSR &2357 ; calculate_vector_from_magnitude_and_angle
&3dcd 20 87 25 JSR &2587 ; rnd
&3dd0 29 1f    AND #&1f                                             # Use random distance for line of sight check
&3dd2 69 10    ADC #&10 ; 16 * &20 fraction = 2 tiles               # between 2 and 6 tiles
&3dd4 4e 98 35 LSR &3598 ; waterline_blocks_line_of_sight           # Clear top bit to ignore waterline
&3dd7 20 d5 35 JSR &35d5 ; check_for_obstruction_along_vector       # Returns carry clear if line of sight
&3dda 68       PLA ; angle_to_check
&3ddb 90 28    BCC &3e05 ; set_target                               # If no obstructions, use this angle
&3ddd a6 83    LDX &83 ; distance
&3ddf ec e3 3c CPX &3ce3 ; route_best_distance                      # Otherwise, is it unobstructed for longer?
&3de2 90 06    BCC &3dea ; not_longer
&3de4 8e e3 3c STX &3ce3 ; route_best_distance                      # If so, note as best so far
&3de7 8d e4 3c STA &3ce4 ; route_best_angle
; not_longer
&3dea ce e5 3c DEC &3ce5 ; route_attempts_remaining
&3ded d0 cd    BNE &3dbc ; find_route_to_target_loop
&3def ad e4 3c LDA &3ce4 ; route_best_angle                         # Use the best angle found
&3df2 85 b5    STA &b5 ; angle
&3df4 a9 20    LDA #&20 ; magnitude
&3df6 20 57 23 JSR &2357 ; calculate_vector_from_magnitude_and_angle
&3df9 ad e3 3c LDA &3ce3 ; route_best_distance
&3dfc c9 0a    CMP #&0a ; 10 * &20 fraction = 1.25 tiles            # provided it is long enough
&3dfe 90 0d    BCC &3e0d ; leave
&3e00 e9 08    SBC #&08 ; 8 * &20 fraction = 1 tile                 # Stop one tile short of obstruction
&3e02 20 d5 35 JSR &35d5 ; check_for_obstruction_along_vector       # Calculate tile just short of obstruction
; set_target
; set_this_object_tx_ty_from_tile_x_y
&3e05 a5 95    LDA &95 ; tile_x
&3e07 85 14    STA &14 ; this_object_tx
&3e09 a5 97    LDA &97 ; tile_y
&3e0b 85 16    STA &16 ; this_object_ty
; leave
&3e0d 60       RTS

; unused
&3e0e 60       RTS

; update_palette_registers
&3e0f a0 10    LDY #&10
; update_palette_registers_loop
&3e11 b9 e4 11 LDA &11e4,Y ; palette_registers_table - 1
&3e14 8d 21 fe STA &fe21 ; video ULA palette register
&3e17 88       DEY
&3e18 d0 f7    BNE &3e11 ; update_palette_registers_loop
&3e1a 60       RTS

; update_routines_base

# Tile update routines
# ====================

# Nest and pipe tiles (TILE_NEST and TILE_PIPE)
# =============================================
# Tertiary data byte is as follows:
#
# 8....... if set, nest or pipe has bush (used as home target for imps)
# .42184.. number of creatures to spawn
# ......21 if non-zero, nest or pipe is inactive; suppress spawning
#
# Tertiary type byte sets type of spawn
#
# Tertiary tile byte sets immediacy of spawning:
#
# 8....... if set, consider spawning when nest or pipe is plotted
#
# update_nest_or_pipe_tile is called when plotting tiles, processing collisions and during events

; update_nest_or_pipe_tile                                          # Called with Y = this_object_tertiary_data_offset
&3e1b f0 6b    BEQ &3e88 ; leave_with_carry_set                     # Leave if Y = 0, i.e. no tertiary object for tile
&3e1d a2 05    LDX #&05                                             # Five slots needed when spawning
&3e1f a5 2d    LDA &2d ; tile_processing_mode
&3e21 29 90    AND #&90 ; TILE_PROCESSING_FLAG_PLOTTING | TILE_PROCESSING_FLAG_EVENTS
&3e23 f0 23    BEQ &3e48 ; consider_spawning
&3e25 10 02    BPL &3e29 ; is_event
; is_plotting_tile
&3e27 a2 00    LDX #&00                                             # Zero slots needs when first plotting tiles
; is_event
&3e29 a5 09    LDA &09 ; tile_flip
&3e2b 0a       ASL A                                                # Set carry if flipped horizontally
;                                                                   # If so, consider spawning creature immediately
&3e2c b9 86 09 LDA &0986,Y ; tertiary_objects_data                  # &80 clear if object has already become primary
&3e2f 10 1c    BPL &3e4d ; spawn_object_if_carry_set
&3e31 08       PHP ; negative, carry set if tile flipped horizontally
&3e32 8a       TXA
&3e33 48       PHA ; tmp_x
&3e34 a9 40    LDA #&40 ; OBJECT_BUSH
&3e36 20 42 40 JSR &4042 ; create_primary_object_from_tertiary      # Create bush object if it doesn't already exist
&3e39 a9 40    LDA #&40
&3e3b 99 a3 08 STA &08a3,Y ; objects_y_fraction
&3e3e 99 80 08 STA &0880,Y ; objects_x_fraction
&3e41 a4 bd    LDY &bd ; this_object_tertiary_data_offset
&3e43 68       PLA ; tmp_x
&3e44 aa       TAX
&3e45 28       PLP ; negative, carry set if tile flipped horizontally
&3e46 30 05    BMI &3e4d ; spawn_object_if_carry_set                # Always branches
; consider_spawning
&3e48 20 87 25 JSR &2587 ; rnd
&3e4b c9 f7    CMP #&f7                                             # 1 in 32 chance of nest or pipe spawning
; spawn_object_if_carry_set
&3e4d 90 39    BCC &3e88 ; leave_with_carry_set
; spawn_object
&3e4f 86 a2    STX &a2 ; slots needed
&3e51 b9 86 09 LDA &0986,Y ; tertiary_objects_data
&3e54 85 a3    STA &a3 ; data
&3e56 0a       ASL A                                                # 84218421 -> 8 4218421.
&3e57 c9 08    CMP #&08
&3e59 90 2d    BCC &3e88 ; leave_with_carry_set                     # Does the nest contain at least one creature?
&3e5b 29 06    AND #&06                                             #               .....21.
&3e5d d0 29    BNE &3e88 ; leave_with_carry_set                     # Is the nest active?
&3e5f a6 be    LDX &be ; this_object_tertiary_type_offset
&3e61 bd 71 0a LDA &0a71,X ; tertiary_objects_type
&3e64 a4 a2    LDY &a2 ; slots_needed
&3e66 85 a2    STA &a2 ; new_object_type
&3e68 20 54 40 JSR &4054 ; create_primary_object_from_tertiary_if_Y_slots_free # Leaves update routine if can't create object
;                                                                   # Otherwise, returns carry clear
&3e6b a5 a3    LDA &a3 ; data
&3e6d e9 03    SBC #&03                                             # Carry is clear here, so subtract four
&3e6f 9d 86 09 STA &0986,X ; tertiary_objects_data                  # to reduce number of creatures it contains
&3e72 a9 05    LDA #&05 ; OBJECT_FLAG_NEWLY_CREATED | OBJECT_FLAG_NOT_PLOTTED
&3e74 99 c6 08 STA &08c6,Y ; objects_flags
&3e77 a6 a2    LDX &a2 ; new_object_type
&3e79 bd 8a 02 LDA &028a,X ; object_types_sprite_table
&3e7c aa       TAX
&3e7d bd 0c 5e LDA &5e0c,X ; sprites_width_and_horizontal_flip_table
&3e80 49 ff    EOR #&ff
&3e82 4a       LSR A                                                # Centre spawn with nest or pipe
&3e83 99 80 08 STA &0880,Y ; objects_x_fraction
&3e86 18       CLC                                                  # Not used
; leave
&3e87 60       RTS
; leave_with_carry_set
&3e88 38       SEC                                                  # Not used
&3e89 60       RTS

# Engine tiles (TILE_ENGINE)
# ==========================
# Tertiary data byte is as follows:
#
# 8....... if set, engine fire needs primary object creating
# ......21 if non-zero, engine is inactive
#
# update_engine_tile is called when plotting tiles, processing collisions and during events

; update_engine_tile                                                # Called with Y = this_object_tertiary_data_offset
&3e8a f0 fb    BEQ &3e87 ; leave                                    # Leave if Y = 0, i.e. no tertiary object for tile
&3e8c a9 3b    LDA #&3b ; OBJECT_ENGINE_FIRE
&3e8e 4c 42 40 JMP &4042 ; create_primary_object_from_tertiary      # Create engine fire object if it doesn't already exist

# Door tiles (TILE_METAL_DOOR and TILE_STONE_DOOR)
# ================================================
# Tertiary data byte is as follows:
#
# 8....... if set, door needs primary object creating
# .421.... door colour
# ....8... if set, door is slow, is being or has been destroyed (DOOR_FLAG_SLOW_OR_DESTROYED) 
# .....4.. if set, door is moving (DOOR_FLAG_MOVING)
# ......2. if set, door is opening; if clear, door is closing (DOOR_FLAG_OPENING)
# .......1 if set, door is locked (DOOR_FLAG_LOCKED)
#
# Tertiary tile byte sets door orientation:
#
# 84...... &00 or &c0 horizontal door
#          &40 or &80 vertical door
#
# objects_state is used for tile x or tile y
# objects_tx is used for open fraction
# objects_ty is used for orientation
#
# update_stone_door_tile is called when plotting tiles, checking for obstruction, processing collisions and during events
# update_metal_door_tile is called when plotting tiles, checking for obstruction, processing collisions and during events

; door_tiles_table                                                  # Tiles that are equivalent in obstruction
&3e91 17 ; &00 (closed horizontal door) : TILE_SPACESHIP_WALL_HORIZONTAL_QUARTER
&3e92 19 ; &01 (open horizontal door)   : TILE_SPACE
&3e93 2a ; &02 (closed vertical door)   : TILE_STONE_SLOPE_SEVENTY_EIGHT
&3e94 19 ; &03 (open vertical door)     : TILE_SPACE

; update_stone_door_tile                                            # Called with Y = this_object_tertiary_data_offset
&3e95 a9 3e    LDA #&3e ; OBJECT_HORIZONTAL_STONE_DOOR
&3e97 2c a9 3c BIT &3ca9 ; (nop)
; update_metal_door_tile                                            # Called with Y = this_object_tertiary_data_offset
#3e98          LDA #&3c ; OBJECT_HORIZONTAL_METAL_DOOR
&3e9a 85 9c    STA &9c ; door_type
&3e9c cc 99 35 CPY &3599 ; door_to_suppress                         # Don't create door during obstruction check
&3e9f f0 41    BEQ &3ee2 ; leave
&3ea1 a5 09    LDA &09 ; tile_flip                                  # &80 set if flipped horizontally, &40 if vertically
&3ea3 0a       ASL A                                                # 84...... -> 8 4.......
&3ea4 2a       ROL A                                                #          -> 4 .......8
&3ea5 69 00    ADC #&00
&3ea7 29 01    AND #&01                                             # 0 if neither or both set, horizontal door
&3ea9 48       PHA ; door orientation                               # 1 if one set, vertical door
&3eaa b9 86 09 LDA &0986,Y ; tertiary_objects_data
&3ead 30 01    BMI &3eb0 ; is_tertiary
&3eaf 4a       LSR A                                                # If not tertiary, use &04 DOOR_FLAG_MOVING instead
; is_tertiary
&3eb0 29 02    AND #&02 ; DOOR_FLAG_OPENING                         # &02 set if door is open (&04 set if door is moving)
&3eb2 4a       LSR A                                                # ......2. -> ......2
&3eb3 e9 00    SBC #&00                                             #          -> &ff if unset, &00 if set
&3eb5 85 a3    STA &a3 ; door_open_fraction
&3eb7 a5 2d    LDA &2d ; tile_processing_mode                       # Negative if TILE_PROCESSING_FLAG_PLOTTING set
&3eb9 30 09    BMI &3ec4 ; skip_setting_tile                        # If not plotting tiles,
&3ebb 68       PLA ; door orientation
&3ebc 48       PHA ; door orientation
&3ebd 2a       ROL A                                                # Carry set if door is open
&3ebe aa       TAX                                                  # 0: h closed, 1: h open, 2: v closed, 3: v open
&3ebf bd 91 3e LDA &3e91,X ; door_tiles_table                       # Use tile that is equivalent in obstruction to door
&3ec2 85 08    STA &08 ; tile_type_and_flip
; skip_setting_tile
&3ec4 68       PLA ; door orientation
&3ec5 24 2d    BIT &2d ; tile_processing_mode
&3ec7 70 19    BVS &3ee2 ; leave                                    # &40 set if TILE_PROCESSING_FLAG_OBSTRUCTION
; create_door                                                       # If not considering obstructions, create door object
&3ec9 48       PHA ; door orientation                               # 0 if horizontal door, 1 if vertical door
&3eca 18       CLC
&3ecb 65 9c    ADC &9c ; door_type
&3ecd 20 42 40 JSR &4042 ; create_primary_object_from_tertiary      # Create door object if it doesn't already exist
&3ed0 68       PLA ; door orientation
&3ed1 0a       ASL A
&3ed2 99 36 09 STA &0936,Y ; objects_ty (door orientation)          # 0 for horizontal door, 2 for vertical door
&3ed5 aa       TAX
&3ed6 b5 95    LDA &95,X ; tile_x
&3ed8 e9 00    SBC #&00
&3eda 99 76 09 STA &0976,Y ; objects_state (door tile x or y)
&3edd a5 a3    LDA &a3 ; door_open_fraction
&3edf 99 16 09 STA &0916,Y ; objects_tx (door open fraction)
; leave
&3ee2 60       RTS

# Transporter tiles (TILE_TRANSPORTER)
# ====================================
# Tertiary data byte is as follows:
#
# 8....... if set, transporter beam needs primary object creating
# .42..... key to unlock
#              &00 : OBJECT_YELLOW_WHITE_RED_KEY
#              &20 : (key 4, not collectable)
#              &40 : OBJECT_RED_MAGENTA_RED_KEY
#              &60 : OBJECT_BLUE_CYAN_GREEN_KEY
# ...1842. transporter destination
# .......1 if set, transporter is inactive
#
# update_transporter_tile is called when plotting tiles, processing collisions and during events

; update_transporter_tile
&3ee3 a9 41    LDA #&41 ; OBJECT_TRANSPORTER_BEAM
&3ee5 20 42 40 JSR &4042 ; create_primary_object_from_tertiary      # Create beam object if it doesn't already exist
&3ee8 a9 40    LDA #&40
&3eea 99 80 08 STA &0880,Y ; objects_x_fraction
&3eed 0a       ASL A
&3eee 99 a3 08 STA &08a3,Y ; objects_y_fraction
; leave
&3ef1 60       RTS

# Invisible switch tiles (TILE_INVISIBLE_SWITCH)
# ==============================================
# Tertiary data byte is as follows:
#
# 84218... switch effects number
# .....42. bits to clear or set in other objects
# .......1 set bits if set, clear bits if clear
#
# Tertiary type byte sets object type than can trigger switch, or negative for any object
#
# update_invisible_switch_tile is called when processing collisions

; update_invisible_switch_tile
&3ef2 a6 be    LDX &be ; this_object_tertiary_type_offset
&3ef4 bd 71 0a LDA &0a71,X ; tertiary_objects_type                  # Type is negative if any object can trigger switch
&3ef7 30 04    BMI &3efd ; consider_triggering_invisible_switch
&3ef9 c5 41    CMP &41 ; this_object_type                           # Otherwise, type is object type required to trigger
&3efb d0 f4    BNE &3ef1 ; leave
; consider_triggering_invisible_switch
&3efd a4 aa    LDY &aa ; this_object
&3eff 20 c5 49 JSR &49c5 ; check_if_object_can_trigger_switches     # Returns carry set if object can trigger
&3f02 90 ed    BCC &3ef1 ; leave
&3f04 a4 bd    LDY &bd ; this_object_tertiary_data_offset
&3f06 b9 86 09 LDA &0986,Y ; tertiary_objects_data
&3f09 48       PHA ; data
&3f0a 4a       LSR A                                                # 84218421 -> .8421842
&3f0b 09 fc    ORA #&fc                                             #          -> ******42, i.e. bits to set or clear
&3f0d 49 03    EOR #&03
&3f0f aa       TAX ; data mask
&3f10 68       PLA ; data
&3f11 b0 02    BCS &3f15 ; to_process_switch_effects
&3f13 29 f8    AND #&f8 ; switch effect number and data toggle      # 84218... switch effects number; clear bits to toggle
; to_process_switch_effects
&3f15 4c db 49 JMP &49db ; process_switch_effects

# Variable wind tiles (TILE_VARIABLE_WIND)
# ========================================
# update_variable_wind_tile is called when processing collisions and during events

; update_variable_wind_tile
&3f18 24 20    BIT &20 ; this_object_waterline
&3f1a 30 d5    BMI &3ef1 ; leave                                    # Random winds don't occur when underwater
&3f1c 24 09    BIT &09 ; tile_flip                                  # &80 set if flipped horizontally, i.e. one of the two
&3f1e 10 04    BPL &3f24 ; not_flipped                              # square caverns below the west stone door (&184f)
&3f20 a9 70    LDA #&70                                             # If so, use a constant downdraft
&3f22 10 23    BPL &3f47 ; apply_wind_velocities_from_A             # Always branches
; not_flipped
&3f24 a5 c0    LDA &c0 ; frame_counter                              # In the other windy square caverns,
&3f26 2a       ROL A
&3f27 2a       ROL A
&3f28 85 b5    STA &b5 ; angle                                      # Angle of wind cycles 360 degrees every 64 frames
&3f2a a5 da    LDA &da ; rnd_state + 1
&3f2c 29 1f    AND #&1f                                             # Magnitude of wind has a random component
&3f2e 45 97    EOR &97 ; tile_y                                     # but also depends on tile position
&3f30 0a       ASL A
&3f31 29 7f    AND #&7f
&3f33 24 95    BIT &95 ; tile_x
&3f35 10 04    BPL &3f3b ; is_left_half_of_world
; is_right_half_of_world
&3f37 29 3f    AND #&3f
&3f39 69 28    ADC #&28
; is_left_half_of_world
&3f3b 20 57 23 JSR &2357 ; calculate_vector_from_magnitude_and_angle
&3f3e 4c 4f 3f JMP &3f4f ; apply_wind_velocities_from_vector

# Constant wind tiles (TILE_CONSTANT_WIND)
# ========================================
# Tertiary data byte is as follows:
# 
# 8421.... y velocity
# ....8421 x velocity
#
# update_constant_wind_tile is called when processing collisions and during events

; update_constant_wind_tile                                         # Called with Y = this_object_tertiary_data_offset
&3f41 98       TYA
&3f42 f0 59    BEQ &3f9d ; update_variable_water_tile               # Used to create the bottom river in Triax's lab
&3f44 b9 86 09 LDA &0986,Y ; tertiary_objects_data
; apply_wind_velocities_from_A
&3f47 85 b6    STA &b6 ; vector_y                                   # Top nibble of data sets y velocity
&3f49 0a       ASL A
&3f4a 0a       ASL A
&3f4b 0a       ASL A
&3f4c 0a       ASL A
&3f4d 85 b4    STA &b4 ; vector_x                                   # Bottom nibble of data sets x velocity
; apply_wind_velocities_from_vector
&3f4f 20 1b 40 JSR &401b ; create_invisible_debris_if_event         # Returns non-zero if event
&3f52 d0 62    BNE &3fb6 ; leave                                    # Otherwise, an object has collided with this tile
&3f54 a2 02    LDX #&02
; apply_wind_velocity_loop                                          # Loop through X = 2 for y, X = 0 for x
&3f56 a4 38    LDY &38 ; this_object_weight
&3f58 c0 04    CPY #&04
&3f5a b0 01    BCS &3f5d ; not_light
&3f5c c8       INY                                                  # Halve wind effects if object is heavy
; not_light
&3f5d 24 20    BIT &20 ; this_object_waterline                      # Negative if completely under water
&3f5f 10 01    BPL &3f62 ; not_underwater
&3f61 c8       INY                                                  # Halve wind effects if object is under water
; not_underwater
&3f62 24 1f    BIT &1f ; this_object_in_water                       # Positive if object is in any water
&3f64 10 06    BPL &3f6c ; skip_frame_check                         # Always add water velocity
&3f66 a5 c0    LDA &c0 ; frame_counter
&3f68 29 10    AND #&10
&3f6a f0 4a    BEQ &3fb6 ; leave                                    # Add wind velocity 16 out of 32 frames
; skip_frame_check
&3f6c 20 94 3f JSR &3f94 ; add_weighted_vector_component_to_this_object_velocity
&3f6f ca       DEX
&3f70 ca       DEX
&3f71 f0 e3    BEQ &3f56 ; apply_wind_velocity_loop
; add_wind_particle_using_velocities
&3f73 20 d4 22 JSR &22d4 ; calculate_angle_from_vector
&3f76 a5 da    LDA &da ; rnd_state + 1
&3f78 4a       LSR A
&3f79 c5 b7    CMP &b7 ; magnitude
&3f7b b0 39    BCS &3fb6 ; leave                                    # More wind particles the stronger the wind is
&3f7d a0 6e    LDY #&6e ; PARTICLE_WIND
; set_new_particles_position_from_this_object
&3f7f a2 02    LDX #&02
; set_new_particles_position_loop                                   # Loop through X = 2 for y, X = 0 for x
&3f81 b5 4f    LDA &4f,X ; this_object_x_fraction
&3f83 e9 40    SBC #&40                                             # Offset by quarter of a tile to left and up
&3f85 95 87    STA &87,X ; new_particles_x_fraction                 # This is a base to which randomness is added
&3f87 b5 53    LDA &53,X ; this_object_x
&3f89 e9 00    SBC #&00
&3f8b 95 8b    STA &8b,X ; new_particles_x
&3f8d ca       DEX
&3f8e ca       DEX
&3f8f f0 f0    BEQ &3f81 ; set_new_particles_position_loop
&3f91 4c 8c 21 JMP &218c ; add_particle

; add_weighted_vector_component_to_this_object_velocity             # Called with Y = weight factor, X = 0 for x, 2 for y
&3f94 a9 0c    LDA #&0c ; wind acceleration
&3f96 85 9c    STA &9c ; maximum_acceleration
&3f98 b5 b4    LDA &b4,X ; vector_x
&3f9a 4c f6 31 JMP &31f6 ; apply_weighted_acceleration_to_this_object_velocity

# Water tiles (TILE_WATER)
# ========================
# Tile &0d is still water
# Tile &4d is unused
# Tile &8d is river in Triax's lab
# Tile &cd is waterfall in Triax's lab
#
# update_water_tile is called when processing collisions and during events

; update_variable_water_tile
&3f9d a5 c0    LDA &c0 ; frame_counter
&3f9f 29 10    AND #&10                                             # 16 out of 32 frames
&3fa1 d0 13    BNE &3fb6 ; leave
; update_water_tile
&3fa3 20 1b 40 JSR &401b ; create_invisible_debris_if_event         # Returns non-zero if event
&3fa6 d0 0e    BNE &3fb6 ; leave
&3fa8 a5 09    LDA &09 ; tile_flip
&3faa 0a       ASL A                                                # 84...... -> 4....... 8
&3fab 2a       ROL A                                                #          -> .......8 4
&3fac 2a       ROL A                                                #          -> ......84
&3fad aa       TAX
&3fae bd 44 1e LDA &1e44,X ; water_velocities_table                 # Use tile flip to determine velocity of water
&3fb1 d0 94    BNE &3f47 ; apply_wind_velocities_from_A
; is_still_water
&3fb3 38       SEC
&3fb4 66 01    ROR &01 ; water_tile                                 # Set top bit to indicate this is a water tile
; leave
&3fb6 60       RTS

# Tiles with objects from type (TILE_*_WITH_OBJECT_FROM_TYPE)
# ===========================================================
# Objects are created as soon as tile is considered, although may be returned
# to tertiary when they go offscreen.
#
# Tertiary type byte sets type of object.
#
# Tertiary data byte depends on type of object. The top bit is always as follows:
#
# 8....... if set, object needs primary object creating
#          if unset, object is either primary or has been collected or destroyed
#
# update_tile_with_object_from_type is called when plotting tiles, processing collisions and during events

; update_tile_with_object_from_type
&3fb7 a6 be    LDX &be ; this_object_tertiary_type_offset
&3fb9 bd 71 0a LDA &0a71,X ; tertiary_objects_type
&3fbc 4c 42 40 JMP &4042 ; create_primary_object_from_tertiary

# Tiles with objects from data (TILE_SPACE_WITH_OBJECT_FROM_DATA)
# ===============================================================
# Objects are created as placeholders as soon as tile is considered, which are
# returned to tertiary when they go offscreen. When disturbed, these
# placeholder objects are converted to proper object types, which may be
# demoted to secondary when they go offscreen, but do not return to tertiary.
#
# Tertiary data byte is as follows:
#
# 8....... if set, object is tertiary object, not placeholder or primary / secondary
# .4218421 type of object
#
# update_tile_with_object_from_data is called when plotting tiles and during events

; update_tile_with_object_from_data
&3fbf b9 86 09 LDA &0986,Y ; tertiary_objects_data
&3fc2 29 7f    AND #&7f
&3fc4 20 42 40 JSR &4042 ; create_primary_object_from_tertiary
&3fc7 a9 49    LDA #&49 ; OBJECT_PLACEHOLDER
&3fc9 99 60 08 STA &0860,Y ; objects_type
&3fcc 60       RTS

# Switch tiles (TILE_SWITCH)
# ==========================
# Tertiary data byte is as follows:
#
# 8....... if set, switch needs primary object creating
# .4218... switch effects number
# .....42. bits to toggle in other objects
# .......1 switch state
#
# update_switch_tile is called when plotting tiles, processing collisions and during events

; update_switch_tile
&3fcd a9 42    LDA #&42 ; OBJECT_SWITCH
&3fcf 4c 42 40 JMP &4042 ; create_primary_object_from_tertiary      # Create switch object if it doesn't already exist

# Mushroom tiles (TILE_MUSHROOMS)
# ===============================
# Tile &0d is red mushrooms
# Tile &4d is blue mushrooms
#
# update_mushroom_tile is called when processing collisions and during events

; update_mushroom_tile
&3fd2 a2 33    LDX #&33 ; OBJECT_RED_MUSHROOM_BALL
&3fd4 24 09    BIT &09 ; tile_flip                                  # &40 set if vertically flipped, i.e. blue mushrooms
&3fd6 50 01    BVC &3fd9 ; not_blue
&3fd8 e8       INX ; &34 ; OBJECT_BLUE_MUSHROOM_BALL
; not_blue                                                          # If tile picked by random event, create a mushroom ball
&3fd9 20 1d 40 JSR &401d ; create_object_if_event                   # Returns zero if not event, carry set if couldn't create
;                                                                   # Returns A = &ff if event
&3fdc f0 0c    BEQ &3fea ; is_collision
&3fde b0 09    BCS &3fe9 ; leave
; is_event
&3fe0 24 09    BIT &09 ; tile_flip                                  # &40 set if vertically flipped, i.e. blue mushrooms
&3fe2 50 02    BVC &3fe6 ; set_new_mushroom_ball_y_fraction
&3fe4 a9 00    LDA #&00                                             # Put blue mushroom balls on top of tile (&00)
; set_new_mushroom_ball_y_fraction
&3fe6 99 a3 08 STA &08a3,Y ; objects_y_fraction                     # Put red mushroom balls on bottom of tile (&ff)
; leave
&3fe9 60       RTS
; is_collision                                                      # If processing collision with tile,
&3fea a5 09    LDA &09 ; tile_flip                                  # &40 set if vertically flipped, i.e. blue mushrooms
&3fec 0a       ASL A                                                # 84218421 -> 8 4218421.
&3fed 0a       ASL A                                                #          -> 4 218421.., i.e. carry set if blue mushrooms
&3fee a4 aa    LDY &aa ; this_object
; play_sound_for_mushrooms
&3ff0 98       TYA
&3ff1 d0 06    BNE &3ff9 ; not_player
; is_player
&3ff3 69 00    ADC #&00                                             # Carry set if blue mushrooms
&3ff5 aa       TAX                                                  # X = 0 for red mushrooms, 1 for blue mushrooms
&3ff6 20 05 40 JSR &4005 ; add_to_player_mushroom_timer
; not_player
&3ff9 20 fa 13 JSR &13fa ; play_sound
&3ffc 33 f3 1d 03                                                   # Play sound for collision with mushroom tile
&4000 a0 4d    LDY #&4d ; PARTICLE_STAR_OR_MUSHROOM
&4002 4c 7f 3f JMP &3f7f ; set_new_particles_position_from_this_object

; add_to_player_mushroom_timer
&4005 a9 3f    LDA #&3f
&4007 7d 1a 08 ADC &081a,X ; player_mushroom_timers
&400a b0 03    BCS &400f ; skip_ceiling
&400c 9d 1a 08 STA &081a,X ; player_mushroom_timers
; skip_ceiling
&400f 2c 15 08 BIT &0815 ; player_mushroom_immunity_pill_collected  # Negative if mushroom immunity pill collected
&4012 30 2d    BMI &4041 ; leave
&4014 d5 ba    CMP &ba,X ; player_immobility_timers
&4016 90 29    BCC &4041 ; leave
&4018 95 ba    STA &ba,X ; player_immobility_timers
&401a 60       RTS

; create_invisible_debris_if_event
&401b a2 35    LDX #&35 ; OBJECT_INVISIBLE_DEBRIS
; create_object_if_event                                            # Called with X = object type
&401d a9 10    LDA #&10 ; TILE_PROCESSING_FLAG_EVENTS
&401f 24 2d    BIT &2d ; tile_processing_mode
&4021 f0 1e    BEQ &4041 ; leave                                    # Leave with zero if this isn't because of an event
&4023 8a       TXA ; object type
&4024 48       PHA ; object type
&4025 20 18 3c JSR &3c18 ; count_objects_of_type_A                  # Returns Y = count
&4028 c0 04    CPY #&04                                             # Don't create if four or more such objects already exist
&402a 68       PLA ; object type
&402b b0 12    BCS &403f ; leave_with_non_zero                      # Leave with carry set if object couldn't be created
&402d 20 5a 1e JSR &1e5a ; create_new_object_even_if_no_slots_free  # Returns carry clear if object created, Y = slot
&4030 b0 0d    BCS &403f ; leave_with_non_zero                      # Leave with carry set if object couldn't be created
&4032 20 67 28 JSR &2867 ; set_object_x_y_and_tx_ty_from_tile_x_y
&4035 a5 d9    LDA &d9 ; rnd_state
&4037 99 a3 08 STA &08a3,Y ; objects_y_fraction                     # Set random position within tile
&403a a5 da    LDA &da ; rnd_state + 1
&403c 99 80 08 STA &0880,Y ; objects_x_fraction
; leave_with_non_zero
&403f a9 ff    LDA #&ff                                             # Leave with non-zero if this was because of an event
; leave
&4041 60       RTS

; create_primary_object_from_tertiary                               # Called with A = object type
&4042 a0 00    LDY #&00 ; free slots needed
&4044 2c a0 08 BIT &08a0 ; (nop)
; create_primary_object_from_tertiary_if_eight_slots_free           # Unused entry point
#4045          LDY #&08 ; free slots needed
&4047 85 a2    STA &a2 ; new_object_type
&4049 a6 bd    LDX &bd ; this_object_tertiary_data_offset           # Zero if not a tertiary object that has a data byte
&404b f0 07    BEQ &4054 ; create_primary_object_from_tertiary_if_Y_slots_free
&404d bd 86 09 LDA &0986,X ; tertiary_objects_data                  # &80 clear if object has already become primary
&4050 10 3f    BPL &4091 ; leave_calling_routine
&4052 a5 a2    LDA &a2 ; new_object_type
; create_primary_object_from_tertiary_if_Y_slots_free
&4054 20 62 1e JSR &1e62 ; create_new_object_if_Y_slots_free        # Returns carry clear if object created, X = slot
&4057 b0 38    BCS &4091 ; leave_tile_update_routine
&4059 20 67 28 JSR &2867 ; set_object_x_y_and_tx_ty_from_tile_x_y
&405c a6 a2    LDX &a2 ; new_object_type
&405e bd 8a 02 LDA &028a,X ; object_types_sprite_table
&4061 aa       TAX
&4062 a5 09    LDA &09 ; tile_flip
&4064 09 05    ORA #&05 ; OBJECT_FLAG_NEWLY_CREATED | OBJECT_FLAG_NOT_PLOTTED
&4066 99 c6 08 STA &08c6,Y ; objects_flags                          # Give object same flip as tile
;                                                                   # This sets other properties for some types of object
&4069 a9 00    LDA #&00
&406b 24 09    BIT &09 ; tile_flip                                  # &80 set if flipped horizontally
&406d 10 03    BPL &4072 ; set_new_object_x_fraction
&406f fd 0c 5e SBC &5e0c,X ; sprites_width_and_horizontal_flip_table
; set_new_object_x_fraction
&4072 99 80 08 STA &0880,Y ; objects_x_fraction
&4075 a9 00    LDA #&00
&4077 24 09    BIT &09 ; tile_flip                                  # &40 set if flipped vertically
&4079 70 03    BVS &407e ; set_new_object_y_fraction
&407b fd 89 5e SBC &5e89,X ; sprites_height_and_vertical_flip_table
; set_new_object_y_fraction
&407e 99 a3 08 STA &08a3,Y ; objects_y_fraction
&4081 a5 bd    LDA &bd ; this_object_tertiary_data_offset
&4083 99 66 09 STA &0966,Y ; objects_tertiary_data_offset
&4086 aa       TAX
&4087 bd 86 09 LDA &0986,X ; tertiary_objects_data
&408a 29 7f    AND #&7f                                             # Clear &80 to indicate object is now primary
&408c 9d 86 09 STA &0986,X ; tertiary_objects_data
&408f 18       CLC                                                  # Clear carry to indicate object created
&4090 60       RTS
; leave_calling_routine
&4091 a6 26    LDX &26 ; stack_pointer_to_leave_to_if_unable_to_create_primary_object
&4093 9a       TXS
&4094 38       SEC                                                  # Set carry to indicate object couldn't be created
; leave
&4095 60       RTS

# Explosion update routines
# =========================

; consider_teleporting_damaged_player
&4096 a6 aa    LDX &aa ; this_object
&4098 d0 fb    BNE &4095 ; leave                                    # Leave if not player
&409a e6 15    INC &15 ; this_object_energy                         # Give player non-zero energy to prevent exploding
&409c 20 87 25 JSR &2587 ; rnd
&409f 10 0b    BPL &40ac ; skip_automatically_teleporting_player    # 1 in 2 chance of automatically teleporting player
; automatically_teleport_player
&40a1 20 c8 32 JSR &32c8 ; handle_dropping_object
&40a4 a2 04    LDX #&04 ; player_deaths - game_time                 # Update player deaths
&40a6 20 94 12 JSR &1294 ; update_game_time_or_player_deaths
&40a9 4c c1 0c JMP &0cc1 ; handle_teleporting                       # Automatically teleport damaged player
; skip_automatically_teleporting_player
&40ac a5 da    LDA &da ; rnd_state + 1
&40ae c9 c0    CMP #&c0                                             # 1 in 4 chance of retrieving object
&40b0 6a       ROR A
&40b1 25 dd    AND &dd ; player_object_held                         # if one isn't already being held
&40b3 10 03    BPL &40b8 ; skip_retrieving_object
&40b5 20 04 35 JSR &3504 ; retrieve_object
; skip_retrieving_object
&40b8 4c c8 32 JMP &32c8 ; handle_dropping_object

; explode_object_by_turning_into_fireball
&40bb 4c b6 4a JMP &4ab6 ; turn_object_into_fireball_of_duration_seven

; explode_object_with_loud_squeal
&40be 20 fa 13 JSR &13fa ; play_sound
&40c1 57 07 43 f6                                                   # Play sound for loud squeal
; explode_object_with_squeal
&40c5 20 b5 14 JSR &14b5 ; play_squeal
&40c8 a5 41    LDA &41 ; this_object_type
&40ca 20 b0 2d JSR &2db0 ; get_range_for_object_type_A
&40cd bd f8 29 LDA &29f8,X ; object_type_ranges_energy_table
&40d0 4a       LSR A
&40d1 4a       LSR A
&40d2 4a       LSR A
&40d3 4a       LSR A
&40d4 4a       LSR A
&40d5 69 03    ADC #&03                                             # explosion duration = (initial energy / 32) + 3
&40d7 10 02    BPL &40db ; explode_object_with_duration_A           # Always branches
; explode_object_with_duration_from_energy                          # Unused entry point
&40d9 a5 15    LDA &15 ; this_object_energy
; explode_object_with_duration_A
&40db 20 f8 13 JSR &13f8 ; play_sound_on_channel_zero
&40de 17 03 11 04                                                   # Play sound for exploding object
; explode_object_with_duration_A_but_no_sound
&40e2 85 3d    STA &3d ; this_object_tertiary_data_offset (explosion duration)
&40e4 a9 44    LDA #&44 ; OBJECT_EXPLOSION
&40e6 85 41    STA &41 ; this_object_type
; start_explosion_timer
&40e8 a9 ce    LDA #&ce ; -50
&40ea 8d 1d 08 STA &081d ; explosion_timer                          # Negative if explosion in progress
&40ed 60       RTS

# Object update routines
# ======================

# Cannon (OBJECT_CANNON)
# ======================

; update_cannon
&40ee a9 4f    LDA #&4f ; OBJECT_CANNON_CONTROL_DEVICE
&40f0 20 c7 0b JSR &0bc7 ; check_if_object_hit_by_other_control     # Returns carry clear if object hit by control
&40f3 b0 07    BCS &40fc ; skip_firing_cannon
&40f5 a2 15    LDX #&15 ; OBJECT_CANNONBALL
&40f7 a9 40    LDA #&40 ; x velocity
&40f9 20 a9 33 JSR &33a9 ; create_projectile_with_zero_velocity_y
; skip_firing_cannon
&40fc a9 0f    LDA #&0f                                             # 1 in 16 chance of flipping cannon when moving
&40fe 4c 7a 25 JMP &257a ; consider_flipping_object_to_match_velocity_x_A

# Lightning (OBJECT_LIGHTNING)
# ============================
# objects_state is used for size
# objects_timer is used for lifespan

; update_lightning                                                  # Called with Y = object touching, or negative if none
&4101 30 0e    BMI &4111 ; not_touching_other_object
&4103 20 c9 1f JSR &1fc9 ; check_if_object_Y_collides_with_lightning_or_fireball # Returns zero if object doesn't collide
&4106 f0 0c    BEQ &4114 ; not_touching_anything_collidable
&4108 c9 01    CMP #&01 ; OBJECT_ACTIVE_CHATTER
&410a f0 05    BEQ &4111 ; skip_damaging_object
&410c a9 50    LDA #&50
&410e 20 a6 24 JSR &24a6 ; damage_object                            # Lightning causes 80 damage
; skip_damaging_object
; not_touching_other_object
&4111 98       TYA
&4112 49 ff    EOR #&ff                                             # &80 set if touching something
; not_touching_anything_collidable
&4114 a6 11    LDX &11 ; this_object_state (lightning size)         # Absolute value is lighting size; &fc <= size <= &04
;                                                                   # Positive if growing or full length, negative if shrinking
&4116 05 1b    ORA &1b ; this_object_tile_top_or_bottom_collision   # &80 set if object hit tiles above or below
&4118 49 ff    EOR #&ff                                             # &80 set if no collision with objects or tiles
&411a 05 11    ORA &11 ; this_object_state (lightning size)         # &80 set if lightning already growing smaller
&411c 30 09    BMI &4127 ; no_collision                             # If the lightning collided with something,
&411e 8a       TXA ; lightning size
&411f 20 4c 32 JSR &324c ; invert_if_positive                       # set it growing smaller from its current size
&4122 aa       TAX ; lightning size
&4123 d0 02    BNE &4127 ; not_zero
&4125 a2 fe    LDX #&fe ; -2
; not_zero
; no_collision
&4127 c6 12    DEC &12 ; this_object_timer (lightning timer)        # Starts at zero, becomes negative
&4129 a5 12    LDA &12 ; this_object_timer (lightning timer)
&412b c9 e7    CMP #&e7 ; -25                                       # Keep lightning growing or full length for 25 frames
&412d f0 04    BEQ &4133 ; is_at_turning_point                      # By branching, set A negative, i.e. start shrinking
&412f e8       INX                                                  # Lengthen or shrink the lightning
&4130 f0 23    BEQ &4155 ; to_set_object_for_removal                # Remove it at zero size
&4132 8a       TXA
; is_at_turning_point
&4133 a0 04    LDY #&04 ; range
&4135 20 5e 32 JSR &325e ; keep_within_range
&4138 85 11    STA &11 ; this_object_state (lightning size)
&413a 20 54 32 JSR &3254 ; make_positive
&413d 69 6c    ADC #&6c ; SPRITE_LIGHTNING_QUARTER - 1              # Use SPRITE_LIGHTNING_QUARTER to SPRITE_NEST
&413f 20 98 32 JSR &3298 ; change_object_sprite_to_A
&4142 a5 06    LDA &06 ; this_object_frame_counter
&4144 4a       LSR A
&4145 66 39    ROR &39 ; this_object_y_flip                         # Flip lightning vertically every frame
&4147 4a       LSR A
&4148 66 37    ROR &37 ; this_object_x_flip                         # Flip lighting horizontally every two frames
&414a c6 42    DEC &42 ; this_object_acceleration_y                 # No gravity for lightning
; use_previous_velocities
&414c a5 44    LDA &44 ; this_object_previous_velocity_x
&414e 85 43    STA &43 ; this_object_velocity_x
&4150 a5 46    LDA &46 ; this_object_previous_velocity_y
&4152 85 45    STA &45 ; this_object_velocity_y
&4154 60       RTS
; to_set_object_for_removal
&4155 4c 29 25 JMP &2529 ; set_object_for_removal

# Inactive grenades (OBJECT_INACTIVE_GRENADE)
# ===========================================
# objects_energy is used for disturbance
# objects_state is used to note if the grenade has ever been held by the player

; update_inactive_grenade
&4158 20 9d 4b JSR &4b9d ; consider_disturbing_object               # Grenades are static until touched by another object
&415b 20 bf 0b JSR &0bbf ; check_if_object_fired
&415e f0 0b    BEQ &416b ; was_fired
&4160 c5 dd    CMP &dd ; player_object_held
&4162 d0 03    BNE &4167 ; not_held                                 # Note if the player has held the inactivate grenade
&4164 85 11    STA &11 ; this_object_state (inactive grenade held)  # Set to non-zero to indicate player has held grenade
; leave
&4166 60       RTS
; not_held
&4167 a5 11    LDA &11 ; this_object_state (inactive grenade held)  # Non-zero if player has ever held grenade
&4169 f0 fb    BEQ &4166 ; leave                                    # If the grenade was held, activate it when dropped
; was_fired                                                         
&416b a9 12    LDA #&12 ; OBJECT_ACTIVE_GRENADE
&416d 4c 86 32 JMP &3286 ; change_object_type

# Gargoyles (OBJECT_GARGOYLE)
# ===========================
# Tertiary data byte is as follows:
#
# 8....... if set, gargoyle needs primary object creating
# .4218421 gargoyle type (sets projectile and firing frequency)

; update_gargoyle                                                   # Called with X = this_object_data
&4170 bd 8b 41 LDA &418b,X ; gargoyles_projectile_frequency_table
&4173 25 06    AND &06 ; this_object_frame_counter
&4175 d0 0f    BNE &4186 ; skip_firing
&4177 bc 95 41 LDY &4195,X ; gargoyles_projectile_velocity_y_table
&417a bd 90 41 LDA &4190,X ; gargoyles_projectile_velocity_x_table
&417d 48       PHA ; x velocity
&417e bd 9a 41 LDA &419a,X ; gargoyles_projectile_type_table
&4181 aa       TAX ; type
&4182 68       PLA ; x velocity
&4183 20 ab 33 JSR &33ab ; create_projectile
; skip_firing
&4186 a0 5a    LDY #&5a                                             # Gargoyles have a minimum energy of 90
&4188 4c 3a 35 JMP &353a ; gain_energy_Y_and_flash_if_damaged

; gargoyles_projectile_frequency_table                              # Types 0 and 2 are unused
;      0  1  2  3  4
&418b 0f 07 07 07 03

; gargoyles_projectile_velocity_x_table
;      0  1  2  3  4
&4190 11 7f 7f 7f 01

; gargoyles_projectile_velocity_y_table
;      0  1  2  3  4
&4195 c0 0c 04 f9 9a

; gargoyles_projectile_type_table
;      0  1  2  3  4
&419a 32 19 19 19 32                                                # &19 = OBJECT_PLASMA_BALL, &32 = OBJECT_LIGHTNING

# Maggot machine (OBJECT_MAGGOT_MACHINE)
# ======================================

; update_maggot_machine
&419f a5 c0    LDA &c0 ; frame_counter
&41a1 29 3f    AND #&3f
&41a3 aa       TAX
&41a4 d0 17    BNE &41bd ; consider_flashing_maggot_machine         # Every sixty four frames,
&41a6 a5 55    LDA &55 ; this_object_y
&41a8 cd d1 14 CMP &14d1 ; waterline_y
&41ab 90 0a    BCC &41b7 ; not_underwater                           # Is the maggot machine underwater?
&41ad a9 80    LDA #&80                                             # Maggot machine explodes with 128 energy
&41af 8d 1f 08 STA &081f ; earthquake_state                         # Set to negative to start earthquake
&41b2 8d 1e 08 STA &081e ; flooding_state                           # Set to negative to start flooding
&41b5 30 2e    BMI &41e5 ; flash_background_and_explode             # Always branches
; not_underwater
&41b7 20 b5 14 JSR &14b5 ; play_squeal                              # Play squeal for maggot machine
&41ba 20 36 31 JSR &3136 ; flip_this_object_horizontally            # Flip maggot machine
;                                                                   # Maggots are created at &25aa
; consider_flashing_maggot_machine
&41bd e0 08    CPX #&08                                             # Flash for 8 out of 64 frames
&41bf 4c df 4d JMP &4ddf ; use_damaged_palette_if_carry_clear

# Coronium crystal OBJECT_CORONIUM_CRYSTAL)
# =========================================
# objects_timer is used for lifespan
#
# Coronium boulder (OBJECT_CORONIUM_BOULDER)
# ==========================================

; update_coronium_crystal                                           # Called with Y = object touching, or negative if none
&41c2 a9 0a    LDA #&0a
&41c4 e6 12    INC &12 ; this_object_timer (coronium timer)         # Coronium crystals explode with duration 10 after 64 frames
&41c6 e6 12    INC &12 ; this_object_timer (coronium timer)
&41c8 30 1e    BMI &41e8 ; to_explode_object_with_duration_A
; update_coronium_boulder                                           # Called with Y = object touching, or negative if none
&41ca 98       TYA
&41cb 30 1e    BMI &41eb ; not_touching_other_object
&41cd f0 26    BEQ &41f5 ; is_touching_player
&41cf b9 60 08 LDA &0860,Y ; objects_type
&41d2 c9 55    CMP #&55 ; OBJECT_CORONIUM_BOULDER
&41d4 f0 04    BEQ &41da ; coronium_explosion
&41d6 c9 58    CMP #&58 ; OBJECT_CORONIUM_CRYSTAL
&41d8 d0 11    BNE &41eb ; not_touching_other_object
; coronium_explosion
&41da 20 16 25 JSR &2516 ; set_object_for_removal                   # Remove object touching
&41dd 20 20 1e JSR &1e20 ; get_object_weight                        # Get weight of object touching
&41e0 65 38    ADC &38 ; this_object_weight                         # Add to this object's weight
&41e2 0a       ASL A
&41e3 69 03    ADC #&03                                             # Coronium explodes with duration (weight * 2) + 3
;                                                                   # OBJECT_CORONIUM_BOULDER has weight 5
;                                                                   # OBJECT_CORONIUM_CRYSTAL has weight 2
;                                                                   # Two boulders              : explosion duration 23
;                                                                   # One boulder + one crystal : explosion duration 17
;                                                                   # Two crystals              : explosion duration 11
; flash_background_and_explode
&41e5 20 92 1f JSR &1f92 ; flash_background
; to_explode_object_with_duration_A
&41e8 4c db 40 JMP &40db ; explode_object_with_duration_A
; not_touching_other_object
&41eb a5 da    LDA &da ; rnd_state + 1
&41ed 29 c0    AND #&c0                                             # 1 in 4 chance of radiation damage if holding
&41ef 05 dd    ORA &dd ; player_object_held
&41f1 c5 aa    CMP &aa ; this_object
&41f3 d0 0e    BNE &4203 ; skip_radiation_damage
; is_touching_player
&41f5 ad 18 08 LDA &0818 ; player_radiation_immunity_pill_collected # Negative if radiation immunity pill collected
&41f8 05 20    ORA &20 ; this_object_waterline                      # Negative if the coronium is completely underwater
&41fa 30 07    BMI &4203 ; skip_radiation_damage                    # If the player doesn't have immunity,
&41fc a9 08    LDA #&08
&41fe a0 00    LDY #&00 ; OBJECT_SLOT_PLAYER
&4200 20 a6 24 JSR &24a6 ; damage_object                            # Coronium causes 8 damage to player
; skip_radiation_damage
&4203 20 87 25 JSR &2587 ; rnd                                      # Use random palette for coronium
&4206 4a       LSR A                                                # Clear top bit to use background plotting
&4207 85 73    STA &73 ; this_object_palette
&4209 60       RTS

# Worms (OBJECT_WORM)
# ===================
# Spawned from earth tiles by random events.
#
# objects_state is used for NPC behaviour and walking
# objects_timer is used for jump cooldown

; update_worm
&420a a9 86    LDA #&86 ; OBJECT_RED_FROGMAN | &80                  # Set &80 to target player too
&420c a0 07    LDY #&07 ; OBJECT_GREEN_FROGMAN                      # Worms avoid green frogmen
&420e a2 00    LDX #&00                                             # Worm causes no damage
&4210 20 5e 4e JSR &4e5e ; update_worm_or_maggot
&4213 4c 11 3c JMP &3c11 ; avoid_target                             # Worms avoid green frogmen and player

# Alien weapon (OBJECT_ALIEN_WEAPON)
# ==================================
# Given by blue/cyan and cyan/yellow imps in exchange for food.

; update_alien_weapon
&4216 20 4e 25 JSR &254e ; increase_energy_by_one_if_not_zero
&4219 20 bf 0b JSR &0bbf ; check_if_object_fired
&421c d0 69    BNE &4287 ; leave
&421e a2 19    LDX #&19 ; OBJECT_PLASMA_BALL
&4220 a9 40    LDA #&40 ; x velocity
&4222 20 a9 33 JSR &33a9 ; create_projectile_with_zero_velocity_y
&4225 b0 60    BCS &4287 ; leave
&4227 4c ad 14 JMP &14ad ; play_low_beep                            # Play low beep for firing alien weapon

# Green slimes (OBJECT_GREEN_SLIME)
# =================================
# Spawned from nests.
#
# objects_state is used for NPC behaviour and walking
# objects_timer is used for sprite offset

; update_green_slime
&422a 20 78 25 JSR &2578 ; consider_flipping_object_to_match_velocity_x
&422d a2 08    LDX #&08 ; npc stimuli type
&422f 20 c9 27 JSR &27c9 ; check_for_npc_stimuli
&4232 20 26 3d JSR &3d26 ; consider_updating_npc_path
&4235 20 02 2a JSR &2a02 ; consider_npc_burrowing
&4238 46 21    LSR &21 ; stimuli                                    # &01 set if green slime absorbed coronium crystal
&423a 90 0c    BCC &4248 ; skip_converting_to_yellow_slime
&423c a9 0b    LDA #&0b ; OBJECT_YELLOW_SLIME
; change_slime_type
&423e 20 fa 13 JSR &13fa ; play_sound
&4241 b0 24 b6 e2                                                   # Play sound for slime changing colour
&4245 4c 86 32 JMP &3286 ; change_object_type
; skip_converting_to_yellow_slime
&4248 a2 03    LDX #&03 ; npc walking type
&424a a9 0c    LDA #&0c ; speed
&424c 20 df 3a JSR &3adf ; update_walking_npc_and_check_for_obstacles_with_speed_A
&424f 20 8c 3b JSR &3b8c ; check_if_player_or_npc_jumping           # Returns carry set if jumping
&4252 90 04    BCC &4258 ; not_jumping
&4254 a9 0f    LDA #&0f                                             # Use SPRITE_BOULDER if green slime is jumping
&4256 85 12    STA &12 ; this_object_timer (sprite offset)
; not_jumping
&4258 a9 11    LDA #&11 ; modulus
&425a 20 55 25 JSR &2555 ; update_sprite_offset_using_velocities    # Returns A = sprite offset, carry clear
&425d e9 08    SBC #&08
&425f 20 56 32 JSR &3256 ; invert_if_negative
&4262 4a       LSR A
&4263 4c 92 32 JMP &3292 ; change_object_sprite_to_base_plus_A      # Use SPRITE_SLIME_ONE to SPRITE_SLIME_FOUR otherwise

# Yellow slimes (OBJECT_YELLOW_SLIME)
# ===================================
# Created by feeding green slimes coronium crystals.
#
# objects_timer is used as timer for conversion back to green slime

; update_yellow_slime                                               # Called with Y = object touching, or negative if none
&4266 d0 02    BNE &426a ; skip_resetting_timer
&4268 84 12    STY &12 ; this_object_timer (yellow slime waking)    # If held by player, set to zero to reset timer
; skip_resetting_timer
&426a a5 da    LDA &da ; rnd_state + 1
&426c 25 1b    AND &1b ; this_object_tile_top_or_bottom_collision   # &80 set if object hit tiles above or below
&426e 10 08    BPL &4278 ; skip_converting_to_green_slime           # If the yellow slime is resting on a surface,
&4270 e6 12    INC &12 ; this_object_timer (yellow slime waking)    # increase the timer
&4272 d0 04    BNE &4278 ; skip_converting_to_green_slime           # When timer overflows,
&4274 a9 0a    LDA #&0a ; OBJECT_GREEN_SLIME                        # convert back to green slime
&4276 d0 c6    BNE &423e ; change_slime_type                        # Always branches
; skip_converting_to_green_slime
&4278 a2 3c    LDX #&3c ; ywY
&427a a5 dc    LDA &dc ; rnd_state + 3
&427c 4a       LSR A
&427d 09 80    ORA #&80
&427f c5 12    CMP &12 ; this_object_timer (yellow slime waking)
&4281 b0 02    BCS &4285 ; skip_flashing_green
&4283 a2 39    LDX #&39 ; gwY                                       # Flash green increasingly as timer increases
; skip_flashing_green
&4285 86 73    STX &73 ; this_object_palette
; leave
&4287 60       RTS

# Fluffy (OBJECT_FLUFFY)
# ======================
# objects_state is used for NPC behaviour and walking
# objects_timer is used for animation

; update_fluffy
&4288 a0 29    LDY #&29                                             # Fluffy has a minimum energy of 41
&428a 20 2e 35 JSR &352e ; give_object_minimum_energy
&428d a2 06    LDX #&06 ; npc stimuli type
&428f 20 c9 27 JSR &27c9 ; check_for_npc_stimuli
&4292 20 26 3d JSR &3d26 ; consider_updating_npc_path
&4295 20 3c 25 JSR &253c ; check_if_object_was_damaged              # Returns carry set if object has just taken >= 8 damage
&4298 b0 24    BCS &42be ; is_squealing                             # Squeal if damaged
&429a a5 11    LDA &11 ; this_object_state (behaviour and walking)
&429c 29 c0    AND #&c0 ; NPC_MOOD_MASK
&429e c9 80    CMP #&80
&42a0 f0 1c    BEQ &42be ; is_squealing                             # Squeal if &80 (NPC_MOOD_MINUS_TWO)
&42a2 a5 aa    LDA &aa ; this_object
&42a4 b0 02    BCS &42a8 ; skip_clearing_target                     # Branch if &c0 (NPC_MOOD_MINUS_ONE)
; is_not_negative_mood                                              # If &00 (NPC_MOOD_ZERO) or &40 (NPC_MOOD_PLUS_ONE),
&42a6 85 0e    STA &0e ; this_object_target_object                  # Set to same object to not seek target
; skip_clearing_target
&42a8 a5 06    LDA &06 ; this_object_frame_counter
&42aa 29 0b    AND #&0b ; 1011
&42ac d0 2d    BNE &42db ; consider_animating_fluffy                # Every eight frames, check for enemies
&42ae a9 2a    LDA #&2a ; OBJECT_RED_YELLOW_IMP
&42b0 a0 86    LDY #&86 ; OBJECT_RANGE_FLYING_ENEMIES
&42b2 20 2c 3c JSR &3c2c ; find_object_ignoring_obstructions        # Returns positive if object found
&42b5 30 12    BMI &42c9 ; skip_squealing                           # If an enemy is found,
&42b7 ad 28 3c LDA &3c28 ; nearest_object_distance
&42ba c5 da    CMP &da ; rnd_state + 1                              # squeal with greater likelihood if nearer
&42bc b0 0b    BCS &42c9 ; skip_squealing
; is_squealing
&42be 20 fa 13 JSR &13fa ; play_sound
&42c1 b0 24 b6 e2                                                   # Play sound for Fluffy squealing; returns carry set
&42c5 66 12    ROR &12 ; this_object_timer (fluffy active)          # Set &80 to make Fluffy active
&42c7 30 12    BMI &42db ; consider_animating_fluffy
; skip_squealing
&42c9 a5 11    LDA &11 ; this_object_state (behaviour and walking)  #    &80 &c0 &00 &40
&42cb 20 4c 32 JSR &324c ; invert_if_positive                       # -> &80 &c0 &00 &c0
&42ce c5 da    CMP &da ; rnd_state + 1
&42d0 66 12    ROR &12 ; this_object_timer (fluffy active)          # i.e. animate more when happy or unhappy
&42d2 10 07    BPL &42db ; consider_animating_fluffy                # Don't purr if not active
&42d4 20 fa 13 JSR &13fa ; play_sound
&42d7 c7 81 c1 f3                                                   # Play sound for Fluffy purring
; consider_animating_fluffy
&42db a5 da    LDA &da ; rnd_state + 1
&42dd 29 02    AND #&02                                             # 0 for x, 2 for y
&42df aa       TAX
&42e0 a5 12    LDA &12 ; this_object_timer (fluffy active)          # If Fluffy is active,
&42e2 55 37    EOR &37,X ; this_object_x_flip                       # Flip horizontally or vertically at random
&42e4 95 37    STA &37,X ; this_object_x_flip
&42e6 25 12    AND &12 ; this_object_timer (fluffy active)
&42e8 10 3b    BPL &4325 ; leave
&42ea a5 dd    LDA &dd ; player_object_held
&42ec 45 aa    EOR &aa ; this_object
&42ee f0 35    BEQ &4325 ; leave                                    # Don't move Fluffy if held by player
; not_held
&42f0 a2 02    LDX #&02 ; npc walking type
&42f2 a9 28    LDA #&28 ; speed
&42f4 4c df 3a JMP &3adf ; update_walking_npc_and_check_for_obstacles_with_speed_A

# Active grenades (OBJECT_ACTIVE_GRENADE)
# =======================================
# objects_timer is used as timer for detonation

; update_active_grenade
&42f7 20 bf 0b JSR &0bbf ; check_if_object_fired
&42fa d0 09    BNE &4305 ; not_fired
; was_fired
&42fc a9 00    LDA #&00
&42fe 85 12    STA &12 ; this_object_timer (active grenade timer)
&4300 a9 50    LDA #&50 ; OBJECT_INACTIVE_GRENADE
&4302 4c 86 32 JMP &3286 ; change_object_type
; not_fired
&4305 a9 0a    LDA #&0a                                             # Grenades explode with duration 10 if destroyed
&4307 a6 15    LDX &15 ; this_object_energy
&4309 f0 08    BEQ &4313 ; to_explode_object_with_duration_A
&430b a5 12    LDA &12 ; this_object_timer (active grenade timer)
&430d c9 60    CMP #&60                                             # Grenades explode after 96 frames
&430f 90 05    BCC &4316 ; skip_exploding_grenade
&4311 a9 10    LDA #&10 ; explosion duration                        # Grenades explode with duration 16 on detonation
; to_explode_object_with_duration_A
&4313 4c db 40 JMP &40db ; explode_object_with_duration_A
; skip_exploding_grenade
&4316 e6 12    INC &12 ; this_object_timer (active grenade timer)
&4318 20 d4 4d JSR &4dd4 ; rotate_colour_from_A                     # Animate colour of active grenade
&431b 8a       TXA                                                  # Returns X = 0 once every sixteen frames
&431c d0 07    BNE &4325 ; skip_sound
&431e 20 fa 13 JSR &13fa ; play_sound
&4321 57 07 cb 82                                                   # Play sound for active grenade
; skip_sound
&4325 60       RTS

# Cannonballs (OBJECT_CANNONBALL)
# ===============================
#
# Blue death balls (OBJECT_BLUE_DEATH_BALL)
# =========================================

; update_cannonball                                                 # Called with Y = object touching, or negative if none
&4326 c6 42    DEC &42 ; this_object_acceleration_y                 # No gravity for cannonball
&4328 20 af 1f JSR &1faf ; check_if_object_Y_damaged_by_projectiles # Returns negative if not, or Y = object
&432b 30 05    BMI &4332 ; skip_damaging_object
&432d a9 aa    LDA #&aa
&432f 20 a6 24 JSR &24a6 ; damage_object                            # Cannonballs cause 110 damage, separate to explosion
; skip_damaging_object
; update_blue_death_ball                                            # Called with Y = object touching, or negative if none
&4332 20 af 1f JSR &1faf ; check_if_object_Y_damaged_by_projectiles # Returns negative if not, or Y = object
&4335 a9 10    LDA #&10                                             # Explode with duration 16
&4337 c0 00    CPY #&00
&4339 10 d8    BPL &4313 ; to_explode_object_with_duration_A        # Explode if ball hit object
&433b 24 1b    BIT &1b ; this_object_tile_top_or_bottom_collision   # &80 set if object hit tiles above or below
&433d 30 d4    BMI &4313 ; to_explode_object_with_duration_A        # Explode if ball hit ceiling or floor
&433f 20 1f 25 JSR &251f ; reduce_energy_by_one                     # Returns A = energy
;                                                                   # Cannonballs and blue death balls have limited lifespan
&4342 f0 cf    BEQ &4313 ; to_explode_object_with_duration_A        # Explode when out of energy with duration 0
&4344 20 cc 22 JSR &22cc ; calculate_angle_from_this_object_velocities
&4347 4c d9 46 JMP &46d9 ; create_projectile_particle_trail

# Red bullets (OBJECT_RED_BULLET)
# ===============================

; update_red_bullet
&434a a9 06    LDA #&06 ; explosion duration
&434c a2 1e    LDX #&1e ; damage                                    # Red bullets cause 30 damage, separate to explosion
&434e 4c 1b 46 JMP &461b ; update_bullet_with_particle_trail_and_consider_moving_towards_player

# Remote control device (OBJECT_REMOTE_CONTROL_DEVICE)
# ====================================================

; update_remote_control_device
&4351 20 bf 0b JSR &0bbf ; check_if_object_fired
&4354 d0 50    BNE &43a6 ; leave
&4356 20 fa 13 JSR &13fa ; play_sound
&4359 57 07 c1 d3                                                   # Play sound for firing remote control device
&435d 4c 2b 31 JMP &312b ; create_aim_particle

# Power pods (OBJECT_POWER_POD)
# =============================

; update_power_pod
&4360 20 1f 25 JSR &251f ; reduce_energy_by_one                     # Power pods have a limited lifespan
&4363 a5 07    LDA &07 ; this_object_frame_counter_sixteen
&4365 c9 02    CMP #&02
&4367 20 df 4d JSR &4ddf ; use_damaged_palette_if_carry_clear       # Flash power pod 2 frames in 16
&436a b0 3a    BCS &43a6 ; leave
&436c 20 fa 13 JSR &13fa ; play_sound
&436f 05 f2 ff c5                                                   # Play sound for power pod pulsing
&4373 60       RTS

# Destinator (OBJECT_DESTINATOR)
# ==============================

; update_destinator
&4374 2c ab 19 BIT &19ab ; ship_moving                              # Negative if player's ship is moving at end of game
&4377 30 2d    BMI &43a6 ; leave
&4379 ad ae 09 LDA &09ae ; tertiary_objects_data + &28              # Tertiary object &2a, TILE_ENGINE at (&9f, &3a)
&437c 4a       LSR A                                                # &01 cleared if invisible switch at (&9b, &3b) triggered
&437d b0 0a    BCS &4389 ; not_in_ship                              # i.e. if destinator has been returned to player's ship
&437f 20 fa 13 JSR &13fa ; play_sound
&4382 91 02 85 47                                                   # Play sound for destinator activation; returns carry set
&4386 6e ab 19 ROR &19ab ; ship_moving                              # Set to negative to indicate ship is moving
; not_in_ship
&4389 a5 06    LDA &06 ; this_object_frame_counter
&438b 29 1f    AND #&1f
&438d c9 01    CMP #&01
&438f 20 df 4d JSR &4ddf ; use_damaged_palette_if_carry_clear       # Flash destinator 1 frame in 16
&4392 b0 12    BCS &43a6 ; leave
&4394 20 fa 13 JSR &13fa ; play_sound
&4397 33 03 85 12                                                   # Play sound for destinator pulsing
&439b 60       RTS

# Giant blocks (OBJECT_GIANT_BLOCK)
# =================================

; update_giant_block
&439c a5 20    LDA &20 ; this_object_waterline
&439e c9 c0    CMP #&c0
&43a0 90 04    BCC &43a6 ; leave                                    # If the giant block is at least three quarters underwater,
&43a2 c6 42    DEC &42 ; this_object_acceleration_y                 # make it float upwards
&43a4 c6 42    DEC &42 ; this_object_acceleration_y
; leave
&43a6 60       RTS

# Empty flask (OBJECT_EMPTY_FLASK)
# ================================

; update_empty_flask
&43a7 a9 4d    LDA #&4d ; OBJECT_FULL_FLASK
&43a9 24 1f    BIT &1f ; this_object_in_water                       # Positive if object is in any water
&43ab 10 37    BPL &43e4 ; to_change_object_type                    # If so, fill the flask
; update_inert_object
; leave
&43ad 60       RTS

# Full flask (OBJECT_FULL_FLASK)
# ==============================
# objects_timer is used for emptying

; update_full_flask                                                 # Called with Y = object touching, or negative if none
&43ae 30 07    BMI &43b7 ; not_touching_other_object
&43b0 20 b6 3b JSR &3bb6 ; get_maximum_of_this_object_velocities    # Returns A = larger of x and y velocities
&43b3 c9 0a    CMP #&0a
&43b5 b0 06    BCS &43bd ; start_emptying_flask                     # If the flask hit another object too quickly,
; not_touching_other_object
&43b7 a5 1d    LDA &1d ; this_object_pre_collision_velocity_magnitude
&43b9 c9 14    CMP #&14
&43bb 90 04    BCC &43c1 ; skip_starting_timer                      # or hit a tile too quickly,
; start_emptying_flask
&43bd a9 10    LDA #&10
&43bf 85 12    STA &12 ; this_object_timer (full flask emptying)    # Set to non-zero to start emptying it
; not_disturbed
&43c1 a5 12    LDA &12 ; this_object_timer (full flask emptying)    # Non-zero if emptying
&43c3 f0 e8    BEQ &43ad ; leave
&43c5 a4 3b    LDY &3b ; this_object_touching
&43c7 30 0a    BMI &43d3 ; not_extinguishing_fireball
&43c9 b9 60 08 LDA &0860,Y ; objects_type
&43cc c9 37    CMP #&37 ; OBJECT_FIREBALL                           # If the flask touched a fireball,
&43ce d0 03    BNE &43d3 ; not_extinguishing_fireball
&43d0 20 16 25 JSR &2516 ; set_object_for_removal                   # Remove the fireball
; not_extinguishing_fireball
&43d3 a9 c0    LDA #&c0                                             # Particles move upwards
&43d5 85 b5    STA &b5 ; angle
&43d7 a0 58    LDY #&58 ; PARTICLE_FLASK
&43d9 a9 08    LDA #&08
&43db 20 8e 21 JSR &218e ; add_particles                            # Add 8 water particles
&43de c6 12    DEC &12 ; this_object_timer (full flask emptying)
&43e0 d0 cb    BNE &43ad ; leave
&43e2 a9 4c    LDA #&4c ; OBJECT_EMPTY_FLASK
; to_change_object_type
&43e4 4c 86 32 JMP &3286 ; change_object_type

# Hovering balls (OBJECT_HOVERING_BALL)
# Invisible hovering balls (OBJECT_INVISIBLE_HOVERING_BALL)
# ========================================================= 
# Spawned from pipes.
#
# objects_timer is used for lifespan

; update_hovering_ball                                              # Called with Y = object touching, or negative if none
&43e7 20 d2 4d JSR &4dd2 ; rotate_colour_from_frame_counter         # Animate colour of hovering ball
&43ea 98       TYA
; update_invisible_hovering_ball                                    # Called with Y = object touching, or negative if none
&43eb 30 13    BMI &4400 ; not_touching_other_object                # If the hovering ball is touching something
&43ed b9 60 08 LDA &0860,Y ; objects_type
&43f0 45 41    EOR &41 ; this_object_type
&43f2 f0 0c    BEQ &4400 ; not_touching_other_object                # that isn't another hovering ball
&43f4 a9 03    LDA #&03
&43f6 20 a6 24 JSR &24a6 ; damage_object                            # Hovering balls cause 3 damage
&43f9 20 fa 13 JSR &13fa ; play_sound
&43fc 33 03 85 02                                                   # Play sound for hovering ball damaging object
; not_touching_other_object
&4400 a5 15    LDA &15 ; this_object_energy
&4402 29 04    AND #&04
&4404 85 15    STA &15 ; this_object_energy
&4406 c6 12    DEC &12 ; this_object_timer (hovering ball ttl)
&4408 d0 0b    BNE &4415 ; move_hovering_ball                       # Hovering balls disappear after 256 frames
&440a 20 0b 2a JSR &2a0b ; set_object_as_far_away                   # Will return hovering ball to nest
; play_sound_for_teleporting
&440d 20 fa 13 JSR &13fa ; play_sound
&4410 29 c2 37 f3                                                   # Play sound for object teleporting
&4414 60       RTS
; move_hovering_ball
&4415 20 6e 48 JSR &486e ; move_hovering_npc
&4418 4c 7a 48 JMP &487a ; thrust_towards_target                    # Hovering balls move twice as quickly as other flying NPCs

# Pistol bullets (OBJECT_PISTOL_BULLET)
# =====================================

; update_pistol_bullet                                              # Called with Y = object touching, or negative if none
&441b 20 af 1f JSR &1faf ; check_if_object_Y_damaged_by_projectiles # Returns negative if not, or Y = object
&441e 30 14    BMI &4434 ; move_bullet
&4420 a9 0a    LDA #&0a
&4422 20 a6 24 JSR &24a6 ; damage_object                            # Pistol bullets cause 10 damage
; explode_bullet
&4425 20 f8 13 JSR &13f8 ; play_sound_on_channel_zero
&4428 17 03 1b 02                                                   # Play sound for exploding bullet
&442c 20 b9 4a JSR &4ab9 ; turn_object_into_fireball_of_duration_two
&442f a9 02    LDA #&02 ; explosion duration
&4431 4c e2 40 JMP &40e2 ; explode_object_with_duration_A_but_no_sound

; move_bullet
&4434 20 1f 25 JSR &251f ; reduce_energy_by_one                     # Bullets have a limited lifespan
&4437 f0 ec    BEQ &4425 ; explode_bullet
&4439 24 1b    BIT &1b ; this_object_tile_top_or_bottom_collision   # &80 set if object hit tiles above or below
&443b 10 0a    BPL &4447 ; no_collision
&443d c9 3e    CMP #&3e
&443f b0 e4    BCS &4425 ; explode_bullet                           # Bullets explode when hitting tiles that are close
&4441 e9 14    SBC #&14
&4443 90 e0    BCC &4425 ; explode_bullet                           # and when hitting tiles that are far away
&4445 85 15    STA &15 ; this_object_energy                         # Otherwise, collision reduces bullet's lifespan
; no_collision
&4447 20 cc 22 JSR &22cc ; calculate_angle_from_this_object_velocities
&444a 85 39    STA &39 ; this_object_y_flip (bullet angle)          # &80 set if bullet moving up
;                                                                   # Flip vertically if moving up
&444c 24 39    BIT &39 ; this_object_y_flip (bullet angle)          # &40 set if bullet moving left
&444e 50 02    BVC &4452 ; is_moving_left
; is_moving_left
&4450 49 ff    EOR #&ff                                             # Flip horizontally if moving left
; is_moving_right
&4452 85 37    STA &37 ; this_object_x_flip
&4454 29 7f    AND #&7f ; ~180 degrees
&4456 4a       LSR A                                                # Divide angle by 8 (11.25 degrees) to give &00 - &0f
&4457 4a       LSR A
&4458 4a       LSR A
&4459 c9 04    CMP #&04                                             # If &00,        use &00 : SPRITE_BULLET_HORIZONTAL
;                                                                   #    &01,        use &01 : SPRITE_BULLET_TWENTY_TWO
;                                                                   #    &02,        use &02 : SPRITE_BULLET_FORTY_FIVE
;                                                                   #    &03,        use &03 : SPRITE_BULLET_SIXTY
&445b 90 03    BCC &4460 ; to_change_object_sprite_to_base_plus_A
&445d 4a       LSR A
&445e 49 06    EOR #&06                                             # If &04 or &05, use &04 : SPRITE_BULLET_SEVENTY_FIVE
;                                                                   # If &06 or &07, use &05 : SPRITE_BULLET_VERTICAL
;                                                                   # If &08 or &09, use &02 : SPRITE_BULLET_FORTY_FIVE
;                                                                   # If &0a or &0b, use &03 : SPRITE_BULLET_SIXTY
;                                                                   # If &0c or &0d, use &00 : SPRITE_BULLET_HORIZONTAL
;                                                                   # If &0e or &0f, use &01 : SPRITE_BULLET_TWENTY_TWO
; to_change_object_sprite_to_base_plus_A
&4460 4c 92 32 JMP &3292 ; change_object_sprite_to_base_plus_A      # Use SPRITE_BULLET_HORIZONTAL to SPRITE_BULLET_VERTICAL

# Red frogman (OBJECT_RED_FROGMAN)
# Green frogmen (OBJECT_GREEN_FROGMAN)
# Invisible frogman (OBJECT_INVISIBLE_FROGMAN)
# ============================================
# Spawned from nests. Red frogman doesn't return.
#
# objects_state is used for NPC behaviour and walking
# objects_timer is used for jump cooldown

; update_red_frogman
&4463 a2 09    LDX #&09 ; npc stimuli type
&4465 20 c9 27 JSR &27c9 ; check_for_npc_stimuli
&4468 a9 33    LDA #&33 ; OBJECT_RED_MUSHROOM_BALL                  # Red frogman avoids red mushroom balls
&446a a8       TAY
&446b 20 0c 3c JSR &3c0c ; avoid_object_type_Y
&446e 20 26 3d JSR &3d26 ; consider_updating_npc_path
&4471 a0 64    LDY #&64                                             # Red frogman has a minimum energy of 100
&4473 d0 15    BNE &448a ; set_frogman_minimum_energy               # Always branches

; update_invisible_frogman
&4475 46 2b    LSR &2b ; this_object_visibility                     # Clear top bit to make invisible frogman invisible
; update_green_frogman
&4477 a6 3b    LDX &3b ; this_object_touching
&4479 d0 0d    BNE &4488 ; not_touching_player
; is_touching_player
&447b 20 05 40 JSR &4005 ; add_to_player_mushroom_timer             # X = 0, so treat cyan and green frogmen as red mushrooms
&447e a9 07    LDA #&07
&4480 85 12    STA &12 ; this_object_timer (npc jump cooldown)      # Suppress jumping and animate kicking legs for 7 frames
&4482 0a       ASL A
&4483 a0 00    LDY #&00 ; OBJECT_SLOT_PLAYER
&4485 20 a6 24 JSR &24a6 ; damage_object                            # Cyan and green frogmen cause 14 damage to player
; not_touching_player
&4488 a0 5a    LDY #&5a                                             # Cyan and green frogmen have a minimum energy of 90
; set_frogman_minimum_energy
&448a 20 2e 35 JSR &352e ; give_object_minimum_energy
&448d a0 14    LDY #&14 ; 1010
&448f 84 04    STY &04 ; walking_speed
&4491 a2 01    LDX #&01 ; npc walking type
&4493 20 08 3b JSR &3b08 ; update_walking_npc
&4496 20 78 25 JSR &2578 ; consider_flipping_object_to_match_velocity_x
&4499 20 86 3b JSR &3b86 ; check_if_npc_can_walk                    # Returns carry set if not on walkable surface
&449c b0 0e    BCS &44ac ; consider_starting_jump_if_not_underwater
&449e 20 ad 3b JSR &3bad ; check_if_slope_is_too_sleep_for_npc      # Returns A = absolute tile_collision_angle
&44a1 c9 28    CMP #&28 ; 56.25 degrees
&44a3 90 0f    BCC &44b4 ; consider_starting_jump
&44a5 20 6d 25 JSR &256d ; set_npc_facing_tile_collision
&44a8 a2 ff    LDX #&ff                                             # Negative to suppress jumping if slope too steep
&44aa d0 0a    BNE &44b6 ; consider_starting_jump_with_X            # Always branches
; consider_starting_jump_if_not_underwater
&44ac 24 1f    BIT &1f ; this_object_in_water                       # Positive if object is in any water
&44ae 30 2c    BMI &44dc ; skip_starting_jump                       # Don't jump if not in water
&44b0 a5 07    LDA &07 ; this_object_frame_counter_sixteen
&44b2 d0 28    BNE &44dc ; skip_starting_jump                       # Every sixteen frames,
; consider_starting_jump
&44b4 a2 04    LDX #&04
; consider_starting_jump_with_X
&44b6 a5 12    LDA &12 ; this_object_timer (npc jump cooldown)
&44b8 d0 22    BNE &44dc ; skip_starting_jump                       # Don't jump if jumped or touched player recently
&44ba a9 09    LDA #&09
&44bc 2c e5 29 BIT &29e5 ; this_object_object_collision_y_flags     # &80 set if collision to bottom from other objects
&44bf 30 10    BMI &44d1 ; set_frogman_jumping_with_speed_A         # If so, jump with speed 9 * 4
&44c1 a5 04    LDA &04 ; walking_speed
&44c3 4a       LSR A
&44c4 4a       LSR A
&44c5 a4 db    LDY &db ; rnd_state + 2
&44c7 c0 20    CPY #&20                                             # 7 in 8 chance of jumping with speed 4 * 4
&44c9 b0 07    BCS &44d2 ; set_frogman_jumping_with_speed_X
&44cb c0 0a    CPY #&0a                                             # 22 in 256 chance of jumping with speed 20 * 4
&44cd b0 02    BCS &44d1 ; set_frogman_jumping_with_speed_A
&44cf 69 05    ADC #&05                                             # 10 in 256 chance of jumping with speed 25 * 4
; set_frogman_jumping_with_speed_A
&44d1 aa       TAX
; set_frogman_jumping_with_speed_X
&44d2 85 12    STA &12 ; this_object_timer (npc jump cooldown)
&44d4 8a       TXA
&44d5 30 05    BMI &44dc ; skip_starting_jump
&44d7 0a       ASL A
&44d8 0a       ASL A
&44d9 20 5b 3a JSR &3a5b ; set_npc_jumping_with_speed_A
; skip_starting_jump
; animate_sprite_from_timer                                         # Also used for worms and maggots
&44dc a5 12    LDA &12 ; this_object_timer (npc jump cooldown)
&44de 18       CLC                                                  # Use SPRITE_FROGMAN_ONE
&44df f0 0b    BEQ &44ec ; to_change_object_sprite_to_base_plus_A_plus_carry
&44e1 c6 12    DEC &12 ; this_object_timer (npc jump cooldown)
&44e3 a5 12    LDA &12 ; this_object_timer (npc jump cooldown)      # unless kicking legs immediately after jump
&44e5 f0 05    BEQ &44ec ; to_change_object_sprite_to_base_plus_A_plus_carry
&44e7 4a       LSR A                                                # 84218421 -> .8421842
&44e8 4a       LSR A                                                #          -> ..842184
&44e9 29 01    AND #&01                                             #          -> .......4
;                                                                   # Use SPRITE_FROGMAN_TWO or SPRITE_FROGMAN_THREE
&44eb 38       SEC                                                  # Set carry to add one to sprite
; to_change_object_sprite_to_base_plus_A_plus_carry
&44ec 4c 93 32 JMP &3293 ; change_object_sprite_to_base_plus_A_plus_carry

Red/magenta imps (OBJECT_RED_MAGENTA_IMP)
Red/yellow imps (OBJECT_RED_YELLOW_IMP)
Blue/cyan imps (OBJECT_BLUE_CYAN_IMP)
Cyan/yellow imps (OBJECT_CYAN_YELLOW_IMP)
Red/Cyan imps (OBJECT_RED_CYAN_IMP)
=========================================
# Spawned from pipes.
#
# objects_state is used for NPC behaviour and walking
# objects_timer is used for sprite offset

; update_imp
&44ef a5 6f    LDA &6f ; this_object_flags
&44f1 29 04    AND #&04 ; OBJECT_FLAG_NEWLY_CREATED
&44f3 f0 04    BEQ &44f9 ; not_newly_created
&44f5 a9 80    LDA #&80 ; NPC_MOOD_MINUS_TWO                        # Set mood of newly spawned imps
&44f7 85 11    STA &11 ; this_object_state (behaviour and walking)
; not_newly_created
&44f9 a0 28    LDY #&28                                             # Imps move with speed &28 if in non-zero mood
&44fb a5 11    LDA &11 ; this_object_state (behaviour and walking)
&44fd 0a       ASL A                                                # Set &80 if NPC_MOOD_MINUS_ONE or NPC_MOOD_PLUS_ONE
&44fe 45 11    EOR &11 ; this_object_state (behaviour and walking)  # Set &80 if NPC_MOOD_MINUS_TWO
&4500 30 02    BMI &4504 ; not_zero_mood
&4502 a0 10    LDY #&10                                             # Otherwise, imps move with speed &10
; not_zero_mood
&4504 84 04    STY &04 ; walking_speed
&4506 a5 41    LDA &41 ; this_object_type
&4508 38       SEC
&4509 e9 29    SBC #&29 ; OBJECT_RED_MAGENTA_IMP                    # Convert object type into NPC stimuli type
&450b aa       TAX ; npc stimuli type
&450c a5 11    LDA &11 ; this_object_state (behaviour and walking)
&450e 29 10    AND #&10 ; NPC_WAS_FED
&4510 08       PHP ; not equal if was fed
&4511 a5 08    LDA &08 ; tile_type
&4513 c9 0a    CMP #&0a ; TILE_PIPE
&4515 d0 2b    BNE &4542 ; not_home
&4517 46 04    LSR &04 ; walking_speed                              # Slow imps down near pipe
&4519 46 04    LSR &04 ; walking_speed
&451b 20 88 22 JSR &2288 ; get_this_object_centre
&451e a5 87    LDA &87 ; this_object_centre_x_fraction
&4520 20 56 32 JSR &3256 ; invert_if_negative
&4523 c9 68    CMP #&68
&4525 90 1b    BCC &4542 ; not_home
&4527 24 18    BIT &18 ; this_object_tile_collision_y_flags         # &80 set if collision to bottom, i.e. imp touching pipe
&4529 10 17    BPL &4542 ; not_home
&452b 28       PLP ; not equal if was fed
&452c f0 11    BEQ &453f ; skip_spawning_gift
&452e de 3a 08 DEC &083a,X ; imp_types_gifts_remaining
&4531 30 0c    BMI &453f ; skip_spawning_gift
&4533 bd a7 31 LDA &31a7,X ; imp_types_gift_table
&4536 aa       TAX                                                  # A = x velocity, X =type
&4537 a0 c8    LDY #&c8 ; -&38                                      # Y = y velocity
&4539 20 ab 33 JSR &33ab ; create_projectile
&453c 20 b5 14 JSR &14b5 ; play_squeal                              # Play squeal for imp spawning gift
; skip_spawning_gift
&453f 4c 0b 2a JMP &2a0b ; set_object_as_far_away
; not_home
&4542 bc a2 31 LDY &31a2,X ; imp_types_minimum_energy_table
&4545 20 2e 35 JSR &352e ; give_object_minimum_energy               # Imps have a minimum energy depending on type
&4548 20 c9 27 JSR &27c9 ; check_for_npc_stimuli                    # Called with X = npc stimuli type
&454b 28       PLP ; not equal if was fed
&454c d0 04    BNE &4552 ; was_fed
&454e 46 21    LSR &21 ; stimuli                                    # &01 set if imp fed
&4550 90 08    BCC &455a ; not_fed
; was_fed
&4552 a5 11    LDA &11 ; this_object_state (behaviour and walking)
&4554 29 3f    AND #&3f                                             # Clear existing mood
&4556 09 90    ORA #&90 ; NPC_MOOD_MINUS_TWO | NPC_WAS_FED
&4558 85 11    STA &11 ; this_object_state (behaviour and walking)
; not_fed
&455a 20 26 3d JSR &3d26 ; consider_updating_npc_path
&455d a2 02    LDX #&02 ; npc walking type
&455f 20 e1 3a JSR &3ae1 ; update_walking_npc_and_check_for_obstacles
&4562 a4 3b    LDY &3b ; this_object_touching
&4564 c4 0e    CPY &0e ; this_object_target_object
&4566 d0 16    BNE &457e ; skip_attacking_target
&4568 24 11    BIT &11 ; this_object_state (behaviour and walking)
&456a 30 12    BMI &457e ; skip_attacking_target
&456c 20 d5 3b JSR &3bd5 ; check_object_touching_angle              # Returns positive if object can be picked up
&456f 30 0d    BMI &457e ; skip_attacking_target
&4571 a4 3b    LDY &3b ; this_object_touching
&4573 a9 05    LDA #&05
&4575 20 a6 24 JSR &24a6 ; damage_object                            # Imps cause 5 damage
&4578 20 b4 0b JSR &0bb4 ; set_this_object_velocities_from_object_Y # Imp follows touching object when damaging it
&457b 4c 9f 45 JMP &459f ; is_at_target
; skip_attacking_target
&457e 20 8c 3b JSR &3b8c ; check_if_player_or_npc_jumping           # Returns carry set if jumping
&4581 b0 30    BCS &45b3 ; is_jumping
&4583 29 0f    AND #&0f                                             # A = frames since standing on walkable surface
&4585 d0 0f    BNE &4596 ; is_walking
&4587 20 ad 3b JSR &3bad ; check_if_slope_is_too_sleep_for_npc      # Returns A = absolute tile_collision_angle
&458a c9 28    CMP #&28 ; 56.25 degrees
&458c a5 11    LDA &11 ; this_object_state (behaviour and walking)
&458e 29 df    AND #&df ; !NPC_CLIMBING
&4590 90 02    BCC &4594 ; skip_climbing                            # Branch if slope is shallower than &28 (56.25 degrees)
&4592 09 20    ORA #&20 ; NPC_CLIMBING
; skip_climbing
&4594 85 11    STA &11 ; this_object_state (behaviour and walking)
; is_walking
&4596 a5 11    LDA &11 ; this_object_state (behaviour and walking)
&4598 29 20    AND #&20 ; NPC_CLIMBING
&459a f0 25    BEQ &45c1 ; skip_starting_jump
; is_climbing
&459c 20 6d 25 JSR &256d ; set_npc_facing_tile_collision
; is_at_target
&459f a9 0c    LDA #&0c ; modulus
&45a1 a2 02    LDX #&02 ; divide velocities by 8
&45a3 20 57 25 JSR &2557 ; update_sprite_offset_using_scaled_velocities # Returns A = sprite offset
&45a6 4a       LSR A                                                # ....8421 -> ....842 1
&45a7 4a       LSR A                                                #          -> .....84 2
&45a8 4a       LSR A                                                #          -> ......8 4, i.e. set carry from &04 of sprite offset
&45a9 a9 67    LDA #&67 ; SPRITE_IMP_CLIMBING_ONE                   # Use SPRITE_CLIMBING_ONE or SPRITE_IMP_CLIMBING_TWO
&45ab 69 00    ADC #&00
&45ad d0 3e    BNE &45ed ; set_imp_sprite                           # Always branches
; not_in_water
&45af a9 69    LDA #&69 ; SPRITE_IMP_JUMPING                        # Use SPRITE_IMP_JUMPING if jumping, but not in water
&45b1 d0 35    BNE &45e8 ; consider_flipping_imp_and_set_sprite     # Always branches
; is_jumping
&45b3 24 1f    BIT &1f ; this_object_in_water                       # Positive if object is in any water
&45b5 30 f8    BMI &45af ; not_in_water
; is_in_water
&45b7 20 87 25 JSR &2587 ; rnd
&45ba 29 1f    AND #&1f                                             # 1 in 32 chance of imp starting jump
&45bc d0 03    BNE &45c1 ; skip_starting_jump
&45be 20 59 3a JSR &3a59 ; set_npc_jumping
; skip_starting_jump
&45c1 a4 3b    LDY &3b ; this_object_touching
&45c3 c4 0e    CPY &0e ; this_object_target_object
&45c5 f0 d8    BEQ &459f ; is_at_target
; consider_firing_at_player
&45c7 a0 00    LDY #&00 ; OBJECT_TYPE_PLAYER
&45c9 84 9d    STY &9d ; target_type
&45cb a6 22    LDX &22 ; npc_type
&45cd bd 9d 31 LDA &319d,X ; imp_types_projectile_table
&45d0 aa       TAX
&45d1 a9 08    LDA #&08                                             # 3 in 128 chance of firing at player (see &276f)
&45d3 20 6d 27 JSR &276d ; find_a_target_and_fire_at_it_with_likelihood_A_divided_by_four
&45d6 a9 64    LDA #&64 ; SPRITE_IMP_WALKING_ONE
&45d8 a6 43    LDX &43 ; this_object_velocity_x
&45da f0 11    BEQ &45ed ; set_imp_sprite
&45dc a9 0c    LDA #&0c ; modulus
&45de a2 02    LDX #&02 ; divide velocities by 8
&45e0 20 57 25 JSR &2557 ; update_sprite_offset_using_scaled_velocities # Returns A = sprite offset
&45e3 4a       LSR A                                                # ....8421 -> ....842
&45e4 4a       LSR A                                                #          -> .....84
&45e5 18       CLC
&45e6 69 64    ADC #&64 ; SPRITE_IMP_WALKING_ONE                    # Use SPRITE_IMP_ONE to SPRITE_IMP_WALKING_THREE
; consider_flipping_imp_and_set_sprite
&45e8 48       PHA ; sprite
&45e9 20 78 25 JSR &2578 ; consider_flipping_object_to_match_velocity_x
&45ec 68       PLA ; sprite
; set_imp_sprite
&45ed 20 98 32 JSR &3298 ; change_object_sprite_to_A
&45f0 20 3c 25 JSR &253c ; check_if_object_was_damaged              # Returns carry set if object has just taken >= 8 damage
&45f3 a9 a5    LDA #&a5 ; pitch
&45f5 b0 12    BCS &4609 ; play_imp_sound                           # Branch to scream if imp is damaged
&45f7 a5 07    LDA &07 ; this_object_frame_counter_sixteen
&45f9 d0 18    BNE &4613 ; skip_sound
&45fb 20 87 25 JSR &2587 ; rnd                                      # Random pitch for imp
&45fe 4a       LSR A                                                # 84218421 -> .8421842 1
&45ff 6a       ROR A                                                #          -> 1.842184
&4600 10 11    BPL &4613 ; skip_sound                               # 1 in 2 chance of playing sound otherwise
&4602 45 11    EOR &11 ; this_object_state (behaviour and walking)
&4604 29 e0    AND #&e0 ; NPC_MOOD_MINUS_TWO | NPC_CLIMBING         # Pitch depends on mood and if the imp is climbing
&4606 4a       LSR A
&4607 09 05    ORA #&05
; play_imp_sound
&4609 8d 12 46 STA &4612 ; imp_sound_parameters + 3 (pitch)
&460c 20 fa 13 JSR &13fa ; play_sound
; imp_sound_parameters
&460f 9c 05 a6 a5                                                   # Play sound for imp (pitch is modified)
; skip_sound
&4613 60       RTS

# Tracer bullets (OBJECT_TRACER_BULLET)
# =====================================

; update_tracer_bullet
&4614 20 49 25 JSR &2549 ; increase_energy_by_one                   # Undo loss at &4434; tracer bullets don't time out
&4617 a9 08    LDA #&08 ; explosion duration
&4619 a2 0f    LDX #&0f ; damage                                    # Tracer bullets cause 15 damage, separate to explosion
; update_bullet_with_particle_trail_and_consider_moving_towards_player
&461b 20 c3 46 JSR &46c3 ; update_bullet_with_particle_trail
&461e 4c 7a 46 JMP &467a ; consider_moving_towards_player

# Green/yellow birds (OBJECT_GREEN_YELLOW_BIRD)
# White/yellow birds (OBJECT_WHITE_YELLOW_BIRD)
# Red/magenta birds (OBJECT_RED_MAGENTA_BIRD)
# Invisible birds (OBJECT_INVISIBLE_BIRD)
# =======================================
# Spawned from nests or pipes.
#
# objects_state is used for visibility
# objects_timer is used for sprite offset

; update_whistling_bird                                             # Called with Y = object touching, or negative if none
&4621 a5 da    LDA &da ; rnd_state + 1
&4623 d0 1a    BNE &463f ; skip_sound                               # 1 in 256 chance of red/magenta bird whistling
&4625 20 9e 2c JSR &2c9e ; play_whistle_two_sound                   # This will deactivate Chatter
&4628 4c 3f 46 JMP &463f ; skip_sound
; update_invisible_bird                                             # Called with Y = object touching, or negative if none
&462b a5 11    LDA &11 ; this_object_state (bird visibility)        # Non-zero if bird has recently taken damage
&462d d0 02    BNE &4631 ; update_bird
&462f 46 2b    LSR &2b ; this_object_visibility                     # Clear top bit to make invisible
; not_invisible
; update_bird                                                       # Called with Y = object touching, or negative if none
&4631 20 87 25 JSR &2587 ; rnd
&4634 29 3f    AND #&3f                                             # 1 in 64 chance of bird calling
&4636 d0 07    BNE &463f ; skip_sound
&4638 20 fa 13 JSR &13fa ; play_sound
&463b 57 07 43 f6                                                   # Play sound for bird calling
; skip_sound
&463f a6 41    LDX &41 ; this_object_type
&4641 98       TYA ; object touching
&4642 d0 06    BNE &464a ; not_touching_player
&4644 bd 62 46 LDA &4662,X ; birds_damage_table - &2e (OBJECT_GREEN_YELLOW_BIRD)
&4647 20 a6 24 JSR &24a6 ; damage_object                            # Birds cause damage to player depending on type
; not_touching_player
&464a bc 66 46 LDY &4666,X ; birds_energy_table - &2e (OBJECT_GREEN_YELLOW_BIRD)
&464d 20 2e 35 JSR &352e ; give_object_minimum_energy               # Birds have minimum energy depending on type
&4650 29 7f    AND #&7f
&4652 85 15    STA &15 ; this_object_energy
&4654 20 3c 25 JSR &253c ; check_if_object_was_damaged              # Returns carry set if object has just taken >= 8 damage
&4657 66 11    ROR &11 ; this_object_state (bird visibility)        # If so, set to non-zero to make invisible bird visible
&4659 d0 2d    BNE &4688 ; dampen_velocity_if_underwater
&465b a9 14    LDA #&14 ; modulus
&465d 20 55 25 JSR &2555 ; update_sprite_offset_using_velocities    # Returns A = sprite offset
&4660 4a       LSR A                                                # ...184.. -> ....184.
&4661 4a       LSR A                                                #          -> .....184, i.e. &00 - &13 -> &00 -> &04
&4662 c9 04    CMP #&04
&4664 d0 02    BNE &4668 ; set_bird_sprite                          # Use SPRITE_BIRD_ONE to SPRITE_BIRD_FOUR for &00 - &03
&4666 a9 02    LDA #&02                                             # Use SPRITE_BIRD_THREE for &04
; set_bird_sprite
&4668 20 92 32 JSR &3292 ; change_object_sprite_to_base_plus_A
&466b a9 11    LDA #&11 ; OBJECT_WASP                               # Birds eat wasps
&466d 20 e1 3b JSR &3be1 ; consider_absorbing_object_touched
&4670 a9 11    LDA #&11 ; OBJECT_WASP
; move_fireball
&4672 a0 00    LDY #&00 ; no range
&4674 20 f8 3b JSR &3bf8 ; consider_finding_target                  # Birds target wasps
&4677 20 09 3c JSR &3c09 ; avoid_fireballs                          # Birds avoid fireballs
; consider_moving_towards_player
&467a 20 26 3d JSR &3d26 ; consider_updating_npc_path
&467d a0 08    LDY #&08 ; maximum acceleration
&467f a9 40    LDA #&40 ; magnitude
&4681 a2 40    LDX #&40 ; 1 in 4 chance
&4683 20 da 31 JSR &31da ; move_towards_target_with_probability_X
&4686 c6 42    DEC &42 ; this_object_acceleration_y                 # No gravity for birds
; dampen_velocity_if_underwater
&4688 24 1f    BIT &1f ; this_object_in_water                       # Positive if object is in any water
&468a 30 03    BMI &468f ; leave
&468c 20 1f 32 JSR &321f ; dampen_this_object_velocities_twice
; leave
&468f 60       RTS

; birds_damage_table
&4690 00 ; &2e (OBJECT_GREEN_YELLOW_BIRD)                           # Green/yellow birds cause no damage to player
&4691 03 ; &2f (OBJECT_WHITE_YELLOW_BIRD)                           # White/yellow birds cause 3 damage to player
&4692 40 ; &30 (OBJECT_RED_MAGENTA_BIRD)                            # Red/magenta birds cause 64 damage to player
&4693 14 ; &31 (OBJECT_INVISIBLE_BIRD)                              # Invisible birds cause 20 damage to player

; birds_energy_table
&4694 00 ; &2e (OBJECT_GREEN_YELLOW_BIRD)
&4695 00 ; &2f (OBJECT_WHITE_YELLOW_BIRD)
&4696 1e ; &30 (OBJECT_RED_MAGENTA_BIRD)                            # Red/magenta birds have a minimum energy of 30
&4697 00 ; &31 (OBJECT_INVISIBLE_BIRD)

# Red mushroom balls (OBJECT_RED_MUSHROOM_BALL)
# Blue mushroom balls (OBJECT_BLUE_MUSHROOM_BALL)
# ===============================================

; update_mushroom_ball                                              # Called with Y = object touching, or negative if none
&4698 30 0c    BMI &46a6 ; not_touching_other_object
&469a b9 60 08 LDA &0860,Y ; objects_type
&469d c9 37    CMP #&37 ; OBJECT_FIREBALL
&469f d0 0a    BNE &46ab ; consider_exploding_mushroom_ball
; convert_mushroom_ball_to_coronium_crystal                         # Fireballs convert mushroom balls to coronium crystals
&46a1 a9 58    LDA #&58 ; OBJECT_CORONIUM_CRYSTAL
&46a3 4c 86 32 JMP &3286 ; change_object_type
; not_touching_other_object
&46a6 20 1f 25 JSR &251f ; reduce_energy_by_one                     # Mushroom balls have a limited lifespan
&46a9 d0 e4    BNE &468f ; leave
; consider_exploding_mushroom_ball                                  # If touched, or when out of energy,
&46ab a5 db    LDA &db ; rnd_state + 2
&46ad 30 e0    BMI &468f ; leave                                    # 1 in 2 chance of exploding into particles
&46af a5 73    LDA &73 ; this_object_palette
&46b1 4a       LSR A                                                # Palette sets amount to add to player_mushroom_timer
&46b2 20 f0 3f JSR &3ff0 ; play_sound_for_mushrooms                 # Adds to player_mushroom_timer if player was touching
&46b5 a9 20    LDA #&20                                             # Create 32 mushroom particles
&46b7 a0 4d    LDY #&4d ; PARTICLE_STAR_OR_MUSHROOM
&46b9 20 8e 21 JSR &218e ; add_particles
&46bc 4c 29 25 JMP &2529 ; set_object_for_removal

# Icer bullets (OBJECT_ICER_BULLET)
# =================================

; update_icer_bullet
&46bf a9 02    LDA #&02 ; explosion duration
&46c1 a2 14    LDX #&14 ; damage
; update_bullet_with_particle_trail                                 # Called with A = explosion duration, X = damage
&46c3 48       PHA ; explosion duration
&46c4 20 af 1f JSR &1faf ; check_if_object_Y_damaged_by_projectiles # Returns negative if not, or Y = object
&46c7 68       PLA ; explosion duration
&46c8 c0 00    CPY #&00
&46ca 30 0a    BMI &46d6 ; no_collision_with_object
&46cc 20 db 40 JSR &40db ; explode_object_with_duration_A           # Explode bullet
&46cf 8a       TXA ; damage
&46d0 20 a6 24 JSR &24a6 ; damage_object                            # Bullets cause damage depending on type
&46d3 4c a3 28 JMP &28a3 ; set_this_object_velocities_to_zero       # Bullet explosions on collision with objects don't move
; no_collision_with_object
&46d6 20 34 44 JSR &4434 ; move_bullet                              # May explode bullet
; create_projectile_particle_trail
&46d9 a5 b5    LDA &b5 ; angle
&46db 49 80    EOR #&80                                             # Particles leave rear of bullet
&46dd 85 b5    STA &b5 ; angle
&46df a6 41    LDX &41 ; this_object_type
&46e1 bd d9 46 LDA &46d9,X ; bullet_particles_colour_and_flags_table - &13 (OBJECT_ICER_BULLET)
&46e4 8d 36 02 STA &0236 ; particle_types_colour_and_flags_table + &2c (PARTICLE_PROJECTILE_TRAIL)
&46e7 a0 2c    LDY #&2c ; PARTICLE_PROJECTILE_TRAIL
&46e9 4c 8c 21 JMP &218c ; add_particle                             # Add particle trail of colour depending on type of bullet

; bullet_particles_colour_and_flags_table                           # &0237 = &01, so randomly choose one of two colours
&46ec 02 ; &13 (OBJECT_ICER_BULLET)     : green or yellow
&46ed 04 ; &14 (OBJECT_TRACER_BULLET)   : blue or magenta
&46ee 08 ; &15 (OBJECT_CANNONBALL)      : cycle colours
&46ef 08 ; &16 (OBJECT_BLUE_DEATH_BALL) : cycle colours

# Crew members (OBJECT_CREW_MEMBER)
# =================================
# Spawned from pipes, but do not return.

; update_crew_member
&46f0 20 92 24 JSR &2492 ; play_scream_if_damaged
&46f3 20 6d 3a JSR &3a6d ; update_walking_state
&46f6 20 4e 25 JSR &254e ; increase_energy_by_one_if_not_zero
&46f9 a9 07    LDA #&07                                             # 1 in 32 chance of flipping crew member
&46fb 20 7a 25 JSR &257a ; consider_flipping_object_to_match_velocity_x_A # Returns A = x flip
&46fe a8       TAY ; x flip
&46ff a9 c0    LDA #&c0 ; upright
&4701 4c d0 38 JMP &38d0 ; set_spacesuit_sprite_and_palette

# Triax (OBJECT_TRIAX)
# ====================

; update_triax
&4704 a9 4a    LDA #&4a ; OBJECT_OBJECT_DESTINATOR
&4706 20 e1 3b JSR &3be1 ; consider_absorbing_object_touched        # Returns zero if object absorbed
&4709 d0 07    BNE &4712 ; not_absorbed
; triax_absorbed_destinator
&470b a9 80    LDA #&80
&470d 8d 23 0a STA &0a23 ; tertiary_objects_data + &9d              # Tertiary object &a8, OBJECT_DESTINATOR at (&64, &d6)
;                                                                   # Return destinator to default location in Triax's lab
&4710 d0 4a    BNE &475c ; make_triax_teleport_away                 # Always branches
; not_absorbed
&4712 a5 3e    LDA &3e ; this_object_target_object_and_flags        # &80 set if TARGET_FLAG_DIRECTNESS_TWO or TARGET_FLAG_DIRECTNESS_THREE
&4714 30 04    BMI &471a ; can_see_or_has_seen_player               # i.e. if Triax can see or has recently seen the player
&4716 a5 06    LDA &06 ; this_object_frame_counter
&4718 f0 42    BEQ &475c ; make_triax_teleport_away                 # Triax teleports away 1 in 256 frames otherwise
; can_see_or_has_seen_player
&471a a5 15    LDA &15 ; this_object_energy
&471c c9 40    CMP #&40
&471e b0 06    BCS &4726 ; not_weak
&4720 a5 d9    LDA &d9 ; rnd_state
&4722 c9 04    CMP #&04
&4724 90 36    BCC &475c ; make_triax_teleport_away                 # 1 in 64 chance of Triax teleporting away if energy < &40
; not_weak
&4726 a5 db    LDA &db ; rnd_state + 2
&4728 c9 08    CMP #&08                                             # 1 in 32 chance of Triax firing a grenade
&472a a9 13    LDA #&13 ; OBJECT_ICER_BULLET
&472c b0 02    BCS &4730 ; not_grenade
&472e a9 12    LDA #&12 ; OBJECT_ACTIVE_GRENADE
; not_grenade
&4730 20 61 48 JSR &4861 ; consider_firing_at_player_and_move_triax # Triax fires and moves like a clawed robot
&4733 a5 15    LDA &15 ; this_object_energy
&4735 c9 05    CMP #&05
&4737 2a       ROL A                                                # Set &01 if energy >= &05
&4738 05 dc    ORA &dc ; rnd_state + 3
&473a 05 da    ORA &da ; rnd_state + 1
&473c 4a       LSR A                                                # Set carry if &01 was set
&473d 90 1d    BCC &475c ; make_triax_teleport_away                 # 1 in 4 chance of Triax teleporting away if energy < &05
&473f ad aa 19 LDA &19aa ; player_is_east_of_76
&4742 49 80    EOR #&80                                             # &80 set if player is west of &80
&4744 0d 1e 08 ORA &081e ; flooding_state                           # &80 set if world is being flooded
&4747 30 06    BMI &474f ; skip_random_teleport
&4749 a5 dc    LDA &dc ; rnd_state + 3
&474b 29 03    AND #&03
&474d f0 0d    BEQ &475c ; make_triax_teleport_away                 # If neither, 1 in 4 chance of Triax teleporting away
; skip_random_teleport
&474f 20 87 25 JSR &2587 ; rnd
&4752 f0 08    BEQ &475c ; make_triax_teleport_away                 # 1 in 256 chance of Triax teleporting away regardless
&4754 20 f0 46 JSR &46f0 ; update_crew_member                       # Triax screams, walks, and is always upright
&4757 a0 00    LDY #&00                                             # Allow Triax to teleport into player's spaceship
&4759 4c 8b 48 JMP &488b ; consider_teleporting_to_random_tile_near_player
; make_triax_teleport_away
&475c a9 00    LDA #&00                                             # Set target y to zero to remove Triax
&475e 4c 9e 48 JMP &489e ; set_this_object_ty_and_set_teleporting

# Big fish (OBJECT_BIG_FISH)
# ==========================
# Spawned from nest.

; update_big_fish
&4761 a9 10    LDA #&10 ; OBJECT_PIRANHA                            # Big fish eats piranhas
&4763 20 e1 3b JSR &3be1 ; consider_absorbing_object_touched
&4766 a0 19    LDY #&19                                             # Big fish has a minimum energy of 25
&4768 20 2e 35 JSR &352e ; give_object_minimum_energy
&476b a5 20    LDA &20 ; this_object_waterline
&476d c9 32    CMP #&32                                             # Is any part of big fish out of water?
&476f 90 51    BCC &47c2 ; leave
; is_underwater
&4771 a9 10    LDA #&10 ; OBJECT_PIRANHA
&4773 a8       TAY ; range                                          # Set to positive to suppress checking for range/
&4774 20 f8 3b JSR &3bf8 ; consider_finding_target
&4777 20 26 3d JSR &3d26 ; consider_updating_npc_path
&477a a9 10    LDA #&10 ; magnitude
&477c 24 3e    BIT &3e ; this_object_target_object_and_flags        # &80 set if TARGET_FLAG_DIRECTNESS_TWO or TARGET_FLAG_DIRECTNESS_THREE
&477e 10 01    BPL &4781 ; not_seen_piranha
&4780 0a       ASL A                                                # Move twice as fast if can see or has seen pirahna
; not_seen_piranha
&4781 a0 02    LDY #&02 ; maximum acceleration
&4783 20 d8 31 JSR &31d8 ; move_towards_target
&4786 4c 78 25 JMP &2578 ; consider_flipping_object_to_match_velocity_x

# Dense nests (OBJECT_DENSE_NEST)
# ===============================

; update_dense_nest                                                 # Called with Y = object touching, or negative if none
&4789 98       TYA
&478a 05 dc    ORA &dc ; rnd_state + 3                              # 1 in 2 chance of dense nest zeroing touched object velocities
&478c 30 34    BMI &47c2 ; leave
&478e 4c a9 0b JMP &0ba9 ; set_object_Y_velocities_from_this_object

# Invisible debris (OBJECT_INVISIBLE_DEBRIS)
# ==========================================
# Created in windy tiles to buffet other objects.

; update_invisible_debris
&4791 20 1f 25 JSR &251f ; reduce_energy_by_one                     # Invisible debris has a limited lifespan
&4794 d0 2c    BNE &47c2 ; leave
&4796 4c 29 25 JMP &2529 ; set_object_for_removal

# Red drops (OBJECT_RED_DROP)
# ===========================

; update_red_drop                                                   # Called with Y = object touching, or negative if none
&4799 30 23    BMI &47be ; not_touching_other_object
&479b b9 60 08 LDA &0860,Y ; objects_type
&479e c9 09    CMP #&09 ; OBJECT_RED_SLIME
&47a0 f0 20    BEQ &47c2 ; leave
&47a2 c9 0b    CMP #&0b ; OBJECT_YELLOW_SLIME
&47a4 f0 1d    BEQ &47c3 ; convert_yellow_to_slime_coronium_boulder # Yellow slimes are converted to coronium boulders
&47a6 c9 10    CMP #&10 ; OBJECT_PIRANHA
&47a8 f0 0c    BEQ &47b6 ; explode_red_drop                         # Piranhas aren't damaged by red drop
&47aa 20 f8 13 JSR &13f8 ; play_sound_on_channel_zero
&47ad 17 03 1b 02                                                   # Play sound for object being damaged by red drop
&47b1 a9 64    LDA #&64
&47b3 20 a6 24 JSR &24a6 ; damage_object                            # Red drop causes 100 damage
; explode_red_drop
&47b6 20 a5 14 JSR &14a5 ; play_high_beep                           # Play high beep for exploding red drop
&47b9 a9 00    LDA #&00 ; explosion duration
&47bb 4c e2 40 JMP &40e2 ; explode_object_with_duration_A_but_no_sound
; not_touching_other_object
&47be 24 1b    BIT &1b ; this_object_tile_top_or_bottom_collision   # &80 set if object hit tiles above or below
&47c0 30 f4    BMI &47b6 ; explode_red_drop                         # Explode red drop when it hits tiles
; leave
&47c2 60       RTS
; convert_yellow_slime_to_coronium_boulder
&47c3 a9 55    LDA #&55 ; OBJECT_CORONIUM_BOULDER
&47c5 99 60 08 STA &0860,Y ; objects_type
&47c8 60       RTS

# Red slimes (OBJECT_RED_SLIME)
# =============================

; update_red_slime
&47c9 a5 07    LDA &07 ; this_object_frame_counter_sixteen
&47cb f0 0a    BEQ &47d7 ; consider_spawning_red_drop
&47cd 4a       LSR A                                                # Animate red slime from frame counter / 2
&47ce 38       SEC
&47cf e9 04    SBC #&04                                             # 0 to 7 -> -4 to 3
&47d1 10 26    BPL &47f9 ; set_red_slime_sprite
&47d3 49 ff    EOR #&ff                                             # -4 to -1 -> 0 to 3
&47d5 10 22    BPL &47f9 ; set_red_slime_sprite                     # i.e. use SPRITE_SLIME_ONE to SPRITE_SLIME_FOUR
;                                                                   # Always branches
; consider_spawning_red_drop
&47d7 24 da    BIT &da ; rnd_state + 1
&47d9 10 1c    BPL &47f7 ; skip_creating_red_drop                   # 1 in chance of creating red drop every sixteen frames
&47db a9 36    LDA #&36 ; OBJECT_RED_DROP
&47dd 20 60 1e JSR &1e60 ; create_new_object_if_four_slots_free     # Returns carry clear if object created, Y = slot
&47e0 b0 15    BCS &47f7 ; skip_creating_red_drop
&47e2 a9 90    LDA #&90
&47e4 24 37    BIT &37 ; this_object_x_flip                         # Negative if flipped horizontally
&47e6 30 02    BMI &47ea ; is_flipped
&47e8 a9 30    LDA #&30
; is_flipped
&47ea 99 80 08 STA &0880,Y ; objects_x_fraction
&47ed a9 40    LDA #&40
&47ef 99 a3 08 STA &08a3,Y ; objects_y_fraction
&47f2 a9 04    LDA #&04
&47f4 99 f6 08 STA &08f6,Y ; objects_velocity_y
; skip_creating_red_drop
&47f7 a9 03    LDA #&03 ; SPRITE_SLIME_FOUR - SPRITE_SLIME_ONE
; set_red_slime_sprite
&47f9 18       CLC
&47fa 69 1c    ADC #&1c ; SPRITE_SLIME_ONE
&47fc a8       TAY
&47fd 85 75    STA &75 ; this_object_sprite
&47ff a2 00    LDX #&00 ; x
&4801 4c aa 32 JMP &32aa ; subtract_width_from_position

# Hovering robot (OBJECT_HOVERING_ROBOT)
# ======================================

; update_hovering_robot
&4804 20 10 4f JSR &4f10 ; set_turret_or_robot_energy               # Returns carry clear if energy < &80
&4807 90 b9    BCC &47c2 ; leave
&4809 a5 dc    LDA &dc ; rnd_state + 3
&480b 4a       LSR A
&480c d0 07    BNE &4815 ; skip_sound                               # 1 in 128 chance of playing sound
&480e 20 fa 13 JSR &13fa ; play_sound
&4811 33 f3 63 e3                                                   # Play sound for hovering robot
; skip_sound
&4815 a5 d9    LDA &d9 ; rnd_state
&4817 c9 40    CMP #&40
&4819 b0 53    BCS &486e ; move_hovering_npc                        # 1 in 4 chance of considering firing at player
&481b a9 18    LDA #&18 ; OBJECT_PISTOL_BULLET
&481d d0 45    BNE &4864 ; consider_firing_at_player_and_move_robot # Always branches

# Green clawed robot (OBJECT_GREEN_CLAWED_ROBOT)
# Magenta clawed robot (OBJECT_MAGENTA_CLAWED_ROBOT)
# Cyan clawed robot (OBJECT_CYAN_CLAWED_ROBOT)
# Red clawed robot (OBJECT_RED_CLAWED_ROBOT)
# ==================================================

; update_clawed_robot
&481f 20 3c 25 JSR &253c ; check_if_object_was_damaged              # Returns carry set if object has just taken >= 8 damage
&4822 66 11    ROR &11 ; this_object_state (clawed robot damaged)   # Set &80 if clawed robot it damaged
&4824 46 11    LSR &11 ; this_object_state (clawed robot damaged)
&4826 a6 41    LDX &41 ; this_object_type
&4828 bc 81 48 LDY &4881,X ; clawed_robots_energy_table - &22 (OBJECT_MAGENTA_CLAWED_ROBOT)
&482b 20 2e 35 JSR &352e ; give_object_minimum_energy               # Clawed robots have minimum energy depending on type
&482e f0 92    BEQ &47c2 ; leave                                    # Leave if clawed robot is exploding
&4830 29 f8    AND #&f8
&4832 4a       LSR A                                                # Energy level will be considered when teleporting back
&4833 9d 21 08 STA &0821,X ; clawed_robots_energy - &22 (OBJECT_MAGENTA_CLAWED_ROBOT)
&4836 0a       ASL A
&4837 c9 8c    CMP #&8c
&4839 90 08    BCC &4843 ; set_clawed_robot_teleporting_away        # Teleport away if energy < 140
; not_low_on_energy
&483b a5 3e    LDA &3e ; this_object_target_object_and_flags
&483d 29 c0    AND #&c0 ; TARGET_FLAG_DIRECTNESS_TWO | TARGET_FLAG_DIRECTNESS_ONE
&483f 05 06    ORA &06 ; this_object_frame_counter                  # 1 in 256 frames, if the clawed robot can't see or
&4841 d0 0a    BNE &484d ; skip_teleporting_away                    # hasn't recently seen the plan, teleport it away
; set_clawed_robot_teleporting_away 
&4843 a5 11    LDA &11 ; this_object_state (clawed robot damaged)
&4845 d0 06    BNE &484d ; skip_teleporting_away                    # Don't teleport if recently damaged
&4847 9d 1d 08 STA &081d,X ; clawed_robots_availability - &22 (OBJECT_MAGENTA_CLAWED_ROBOT) # Set to zero to indicate clawed
&484a 4c 9e 48 JMP &489e ; set_this_object_ty_and_set_teleporting   # robot has teleported away
; skip_teleporting_away
&484d a0 46    LDY #&46                                             # Limit clawed robots to below top of Pericles spaceship
&484f 20 8b 48 JSR &488b ; consider_teleporting_to_random_tile_near_player
&4852 20 87 25 JSR &2587 ; rnd
&4855 4a       LSR A                                                # 1 in 128 chance of playing sound
&4856 d0 07    BNE &485f ; skip_sound
&4858 20 fa 13 JSR &13fa ; play_sound
&485b 17 03 68 a3                                                   # Play sound for clawed robot
; skip_sound
&485f a9 13    LDA #&13 ; OBJECT_ICER_BULLET
; consider_firing_at_player_and_move_triax
&4861 20 49 25 JSR &2549 ; increase_energy_by_one                   # Triax and clawed robots gain two energy per update
; consider_firing_at_player_and_move_robot
&4864 20 49 25 JSR &2549 ; increase_energy_by_one                   # Hovering robot gains one energy per update
&4867 aa       TAX ; projectile type
&4868 a8       TAY ; range                                          # Set to positive to suppress checking for range/
&4869 a9 81    LDA #&81 ; OBJECT_ACTIVE_CHATTER | &80               # &80 set to target player too
&486b 20 68 27 JSR &2768 ; find_a_target_and_fire_at_it
; move_hovering_npc                                                 # Also hovering balls and hovering robots
&486e a9 00    LDA #&00 ; OBJECT_SLOT_PLAYER
&4870 85 0e    STA &0e ; this_object_target_object
&4872 20 26 3d JSR &3d26 ; consider_updating_npc_path
&4875 a9 07    LDA #&07                                             # 1 in 32 chance of flipping hovering NPC
&4877 20 7a 25 JSR &257a ; consider_flipping_object_to_match_velocity_x_A
; thrust_towards_target                                             # Also Chatter
&487a a9 1c    LDA #&1c ; magnitude
&487c a0 04    LDY #&04 ; maximum acceleration
&487e a2 80    LDX #&80 ; 1 in 2 chance
&4880 20 da 31 JSR &31da ; move_towards_target_with_probability_X
&4883 c6 42    DEC &42 ; this_object_acceleration_y                 # No gravity for hovering NPCs
&4885 20 1e 3a JSR &3a1e ; consider_hovering_over_ground
&4888 4c 3d 1f JMP &1f3d ; add_jetpack_thrust_particles

; consider_teleporting_to_random_tile_near_player
&488b 24 17    BIT &17 ; this_object_surrounded_by_tiles            # &80 set if object surrounded by tiles
&488d 10 31    BPL &48c0 ; leave                                    # This is the case if Triax or robot's y = &fe
&488f a9 40    LDA #&40 ; TARGET_FLAG_DIRECTNESS_ONE
&4891 85 3e    STA &3e ; this_object_target_object_and_flags
&4893 a9 03    LDA #&03                                             # Consider a random tile +/- 2 tiles in x and y
&4895 20 43 27 JSR &2743 ; get_random_tile_near_player
&4898 20 05 3e JSR &3e05 ; set_this_object_tx_ty_from_tile_x_y
&489b 20 c1 3b JSR &3bc1 ; get_maximum_of_A_and_Y
; set_this_object_ty_and_set_teleporting
&489e 85 16    STA &16 ; this_object_ty
&48a0 4c e5 0c JMP &0ce5 ; set_this_object_teleporting

; clawed_robots_energy_table
&48a3 46 ; &22 (OBJECT_MAGENTA_CLAWED_ROBOT)                        # Magenta clawed robot has minimum energy of 70
&48a4 5a ; &23 (OBJECT_CYAN_CLAWED_ROBOT)                           # Cyan clawed robot has minimum energy of 90
&48a5 80 ; &24 (OBJECT_GREEN_CLAWED_ROBOT)                          # Green clawed robot has minimum energy of 128
&48a6 82 ; &25 (OBJECT_RED_CLAWED_ROBOT)                            # Red clawed robot has minimum energy of 130

# Active chatter (OBJECT_ACTIVE_CHATTER)
# Inactive chatter (OBJECT_INACTIVE_CHATTER)
# ==========================================
# objects_state is used for NPC behaviour
# objects_timer is used chattering timer and activation flag

; update_inactive_or_active_chatter
&48a7 a5 27    LDA &27 ; whistle_one_active                         # &80 set if whistle one played
&48a9 29 80    AND #&80
&48ab 10 04    BPL &48b1 ; not_whistled
&48ad 85 12    STA &12 ; this_object_timer (chatter chattering timer and activation flag) # Set to negative to activate Chatter
&48af 85 11    STA &11 ; this_object_state (behaviour and walking) # Set to NPC_MOOD_MINUS_TWO
; not_whistled
&48b1 a2 07    LDX #&07 ; npc stimuli type
&48b3 20 c9 27 JSR &27c9 ; check_for_npc_stimuli
&48b6 20 26 3d JSR &3d26 ; consider_updating_npc_path
&48b9 46 21    LSR &21 ; stimuli                                    # &01 set if Chatter fed coronium crystal
&48bb 90 03    BCC &48c0 ; leave                                    # If so, increase Chatter's energy reserve
; increase_chatter_energy_reserve
&48bd ee 1c 08 INC &081c ; chatter_energy_reserve
; leave
&48c0 60       RTS

; update_inactive_chatter
&48c1 20 a7 48 JSR &48a7 ; update_inactive_or_active_chatter
&48c4 a5 12    LDA &12 ; this_object_timer (chatter chattering timer and activation flag) # Negative if whistle one played
&48c6 10 f8    BPL &48c0 ; leave
&48c8 85 15    STA &15 ; this_object_energy
&48ca ce 1c 08 DEC &081c ; chatter_energy_reserve                   # Reduce energy reserve by one each activation
&48cd 30 ee    BMI &48bd ; increase_chatter_energy                  # Keep Chatter's energy from falling below zero
;                                                                   # but don't activate Chatter if energy reserve is zero
; activate_chatter
&48cf a9 01    LDA #&01 ; OBJECT_ACTIVE_CHATTER
&48d1 2c a9 38 BIT &38a9 ; (nop)
; deactivate_chatter
#48d2          LDA #&38 ; OBJECT_INACTIVE_CHATTER
&48d4 4c 86 32 JMP &3286 ; change_object_type

; update_active_chatter
&48d7 20 a7 48 JSR &48a7 ; update_inactive_or_active_chatter
&48da a0 00    LDY #&00 ; minimum energy                            # Chatter has a minimum energy of 0, but cannot be destroyed
&48dc 20 47 35 JSR &3547 ; flash_if_damaged
&48df a5 15    LDA &15 ; this_object_energy
&48e1 f0 ef    BEQ &48d2 ; deactivate_chatter                       # Deactivate Chatter if damaged too much
&48e3 a9 1f    LDA #&1f                                             # 1 in 8 chance of flipping active Chatter
&48e5 20 7a 25 JSR &257a ; consider_flipping_object_to_match_velocity_x_A
&48e8 24 c4    BIT &c4 ; every_eight_frames
&48ea 10 22    BPL &490e ; skip_firing_lightning                    # Every eight frames, consider firing lightning
&48ec a9 20    LDA #&20 ; OBJECT_CYAN_RED_TURRET
&48ee a0 86    LDY #&86 ; OBJECT_RANGE_FLYING_ENEMIES
&48f0 20 2a 3c JSR &3c2a ; find_object
&48f3 30 19    BMI &490e ; skip_firing_lightning
&48f5 a5 b5    LDA &b5 ; angle                                      # Angle of target, set by find_object
&48f7 69 40    ADC #&40 ; 90 degrees
&48f9 85 37    STA &37 ; this_object_x_flip
&48fb 45 6f    EOR &6f ; this_object_flags
&48fd 30 0f    BMI &490e ; skip_firing_lightning                    # Don't fire backwards
&48ff a5 b5    LDA &b5 ; angle
&4901 29 7f    AND #&7f ; ~180 degrees
&4903 e9 0a    SBC #&0a
&4905 c9 6c    CMP #&6c                                             # If &0a <= angle < &76
&4907 90 05    BCC &490e ; skip_firing_lightning                    #    ~14 degrees <= angle < ~165 degrees, don't fire
;                                                                   # i.e. only fire within ~14 degrees of horizontal
&4909 85 12    STA &12 ; this_object_timer (chatter chattering timer and activation flag) # Chatter chatters when firing
&490b 20 a5 33 JSR &33a5 ; create_lightning
; skip_firing_lightning
&490e a5 12    LDA &12 ; this_object_timer (chatter chattering timer and activation flag)
&4910 f0 21    BEQ &4933 ; skip_chattering
&4912 c6 12    DEC &12 ; this_object_timer (chatter chattering timer and activation flag)
&4914 a5 d9    LDA &d9 ; rnd_state
&4916 c9 c0    CMP #&c0                                             # 1 in 4 chance of playing a sound if chattering
&4918 90 19    BCC &4933 ; skip_chattering
&491a a5 dc    LDA &dc ; rnd_state + 3
&491c 4a       LSR A                                                # 84218421 -> .8421842 1
&491d 4a       LSR A                                                #          -> ..842184 2
&491e 45 11    EOR &11 ; this_object_state (behaviour and walking)  # Set two top bits from Chatter's mood, rest as random
&4920 69 40    ADC #&40                                             # &80 &c0 &00 &40 -> &c0 &00 &40 &80
&4922 49 c0    EOR #&c0                                             #                 -> &00 &c0 &80 &40
&4924 4a       LSR A                                                #                 -> &00 &60 &40 &20
&4925 8d 88 2e STA &2e88 ; envelopes_table + &cf (chatter_pitch)    # Set delta for first stage of pitch envelope. This
;                                                                   # stage is duration one (&2e87), so this sets the pitch
&4928 20 fa 13 JSR &13fa ; play_sound
&492b 33 f3 cd 82                                                   # Play sound for Chatter chattering
&492e a9 4b    LDA #&4b ; cyB
&4931 85 73    STA &73 ; this_object_palette
; skip_chattering
&4933 ae d8 29 LDX &29d8 ; whistle_two_activating_object            # Positive if whistle two played
&4936 30 14    BMI &494c ; skip_producing_power_pod
&4938 20 9a 35 JSR &359a ; check_for_obstruction_between_objects_80
&493b b0 0f    BCS &494c ; skip_producing_power_pod                 # If Chatter can see the source of the whistle,
&493d a0 4b    LDY #&4b ; OBJECT_POWER_POD
&493f a9 40    LDA #&40 ; &10 (firing velocity) * 4
&4941 20 91 27 JSR &2791 ; fire_at_target_with_velocity             # Returns carry set if unable to calculate velocities
&4944 10 06    BPL &494c ; skip_deactivating                        #         positive if firing would have been backwards
&4946 b0 04    BCS &494c ; skip_deactivating                        # Bug: carry clear doesn't mean power pod was created
&4948 a9 00    LDA #&00
&494a 85 15    STA &15 ; this_object_energy                         # Set to zero to deactivate Chatter
; skip_deactivating
; skip_producing_power_pod
&494c a5 3b    LDA &3b ; this_object_touching                       # If Chatter isn't touching anything
&494e 05 0e    ORA &0e ; this_object_target_object                  # or targeting any other object
&4950 d0 02    BNE &4954 ; to_thrust_towards_target
&4952 85 3e    STA &3e ; this_object_target_object_and_flags        # then set player as target
; to_thrust_towards_target
&4954 4c 7a 48 JMP &487a ; thrust_towards_target

; unused
&4957 60       RTS

# Switches (OBJECT_SWITCH)
# ========================
# Tertiary data byte is as follows:
#
# 8....... if set, switch needs primary object creating
# .4218... switch effects number
# .....42. bits to toggle in other objects
# .......1 switch state
#
# objects_tx is used to suppress auto-repeat

; switch_effects_table
&4958 00 b0 bb 84    ; &00 : &0a36, &0a41, &0a0a        # switch at (&d5, &73)
&495c 00 0f 29       ; &01 : &0995, &09af               # switch at (&9d, &3b)
&495f 00 c5          ; &02 : &0a4b                      # switch at (&95, &5d)
&4961 00 e7 8f       ; &03 : &0a6d, &0a15               # switch at (&29, &c8)
&4964 00 8a          ; &04 : &0a10                      # switch at (&7c, &c0)
&4966 00 13          ; &05 : &0999                      # switch at (&4d, &80)
&4968 00 8e 32       ; &06 : &0a14, &09b8               # switch at (&a1, &58)
&496b 00 c2          ; &07 : &0a48                      # switch at (&6a, &de)
&496d 00 11 aa bd    ; &08 : &0997, &0a30, &0a43        # switches at (&46, &56), (&8b, &71)
&4971 00 58 cc 55 bc : &09 : &09de, &0a52, &09db, &0a42 # switch at (&ab, &6b)
&4976 00 55          ; &0a : &09db                      # invisible switch at (&a8, &69)
&4978 00 46 a9       ; &0b : &09cc, &0a2f               # switches at (&d4, &6f), (&d5, &73)1
&497b 00 6a 8b       ; &0c : &09f0, &0a11               # switch at (&e3, &9c) or (&e3, &bc)
&497e 00 e6 85 d8    ; &0d : &0a6c, &0a0b, &0a5e        # switch at (&67, &cb)
&4982 00 c7 88       ; &0e : &0a4d, &0a0e               # invisible switch at (&b4, &c2), switch at (&b8, &c3)
&4985 00 68          ; &0f : &09ee                      # switch at (&c4, &c4)
&4987 00 14          ; &10 : &099a                      # invisible switch at (&c1, &7c) 
&4989 00 28 4c       ; &11 : &09ae, &09d2               # invisible switch at (&9b, &3b)
&498c 00 65          ; &12 : &09eb                      # invisible switch at (&c6, &7c)
&498e 00 89          ; &13 : &0a0f                      # invisible switch at (&80, &c2)
&4990 00 8d          ; &14 : &0a13                      # invisible switch at (&67, &da)
&4992 00 64 2a       ; &15 : &09ea, &09b0               # invisible switch at (&a9, &9c)
&4995 00 6b          ; &16 : &09f1                      # invisible switch at (&eb, &bc)
&4997 00 a7 b9 10    ; &17 : &0a2d, &0a3f, &0996        # invisible switches at (&87, &77), (&7f, &77), (&83, &76)
&499b 00             ; (end of list)

; copy_protection_seed
&499c ea

; update_switch                                                     # Called with Y = object touching, or negative if none
&499d 18       CLC                                                  # Clear carry to indicate not triggered if not touched
&499e 30 03    BMI &49a3 ; not_touched
&49a0 20 c5 49 JSR &49c5 ; check_if_object_can_trigger_switches     # Returns carry set if object can trigger
; not_touched
&49a3 66 14    ROR &14 ; this_object_tx (switch repeat suppressor)  # Set &80 if switch is pressed, clear if not
&49a5 10 16    BPL &49bd ; skip_triggering_switch
&49a7 a5 14    LDA &14 ; this_object_tx (switch repeat suppressor)  # &80 set if switch has just been pressed
&49a9 0a       ASL A
&49aa d0 11    BNE &49bd ; skip_triggering_switch                   # Branch if switch is pressed a second time
&49ac 2a       ROL A
&49ad 45 bc    EOR &bc ; this_object_data                           # Toggle &01 to indicate whether switch is on
&49af 85 bc    STA &bc ; this_object_data                           # Remaining bits of data set switch effect
&49b1 a2 ff    LDX #&ff ; data mask
&49b3 20 db 49 JSR &49db ; process_switch_effects
&49b6 20 fa 13 JSR &13fa ; play_sound
&49b9 3d 04 11 d4                                                   # Play sound for switch being pressed
; skip_triggering_switch
; set_this_object_x_flip_from_data
&49bd a5 bc    LDA &bc ; this_object_data                           # If &01 set, switch is on
&49bf 4a       LSR A
&49c0 66 37    ROR &37 ; this_object_x_flip                         # If so, set top bit to flip switch horizontally
&49c2 4c 38 35 JMP &3538 ; gain_energy_and_flash_if_damaged         # Switches have a minimum energy of 30

; check_if_object_can_trigger_switches                              # Called with X = object type
&49c5 20 20 1e JSR &1e20 ; get_object_weight
&49c8 c9 02    CMP #&02                                             # If object is very light,
&49ca 90 0e    BCC &49da ; leave                                    # Leave with carry clear to indicate not triggering
&49cc e0 35    CPX #&35 ; OBJECT_INVISIBLE_DEBRIS
&49ce f0 06    BEQ &49d6 ; is_invisible_debris                      # Set carry if invisible debris
&49d0 e0 27    CPX #&27 ; OBJECT_MAGGOT
&49d2 b0 06    BCS &49da ; leave                                    # Leave with carry set to indicate triggering
&49d4 e0 22    CPX #&22 ; OBJECT_MAGENTA_CLAWED_ROBOT               # Set carry if clawed robots or Triax, clear otherwise
; is_invisible_debris
&49d6 2a       ROL A
&49d7 49 01    EOR #&01                                             # Invert carry,
&49d9 6a       ROR A                                                # i.e. invisible debris, clawed robots and Triax don't trigger switches
; leave
&49da 60       RTS

; process_switch_effects                                            # Called with A 84218... switch effects number
;                                                                   #               .....42. bits to toggle in other objects
&49db 86 9c    STX &9c ; mask                                       #             X = data mask
&49dd 4a       LSR A                                                # 84218421 -> .8421842 1
&49de 48       PHA ; switch effect byte
&49df 29 03    AND #&03                                             #             ......42
&49e1 85 9d    STA &9d ; toggle
&49e3 68       PLA ; switch effect byte
&49e4 4a       LSR A                                                # .8421842 -> ..842184
&49e5 4a       LSR A                                                #          -> ...84218
&49e6 aa       TAX ; switch effects number
&49e7 a0 ff    LDY #&ff
; find_switch_effect_loop
&49e9 c8       INY
&49ea b9 58 49 LDA &4958,Y ; switch_effects_table                   # &00 indicates start of set of switch effects
&49ed d0 fa    BNE &49e9 ; find_switch_effect_loop
&49ef ca       DEX
&49f0 10 f7    BPL &49e9 ; find_switch_effect_loop
&49f2 aa       TAX
; process_switch_effects_loop                                       # Otherwise, byte is offset into tertiary_objects_data
&49f3 bd 86 09 LDA &0986,X ; tertiary_objects_data
&49f6 85 9a    STA &9a ; previous_data
&49f8 25 9c    AND &9c ; mask
&49fa 45 9d    EOR &9d ; toggle
&49fc 9d 86 09 STA &0986,X ; tertiary_objects_data
&49ff c8       INY
&4a00 be 58 49 LDX &4958,Y ; switch_effects_table                   # Consider next byte of set of switch effects
&4a03 d0 ee    BNE &49f3 ; process_switch_effects_loop              # &00 indicates end of set of switch effects
&4a05 c5 9a    CMP &9a ; previous_data
&4a07 f0 07    BEQ &4a10 ; leave                                    # No sound if switch had no effect on last object
&4a09 20 fa 13 JSR &13fa ; play_sound
&4a0c c7 c3 c1 03                                                   # Play sound for switch having an effect
; leave
&4a10 60       RTS

# Player (OBJECT_PLAYER)
# ======================

; update_player                                                     # Called with Y = object touching, or negative if none
&4a11 30 09    BMI &4a1c ; skip_picking_up_touched_object           #        carry set
&4a13 b9 60 08 LDA &0860,Y ; objects_type
&4a16 49 03    EOR #&03 ; OBJECT_FLUFFY 
&4a18 d0 02    BNE &4a1c ; skip_picking_up_touched_object
&4a1a 84 dd    STY &dd ; player_object_held                         # Pick up Fluffy if touched
; skip_picking_up_touched_object
&4a1c 6e d7 29 ROR &29d7 ; player_object_fired                      # Set to negative to indicate no object fired by default
&4a1f a5 53    LDA &53 ; this_object_x
&4a21 c9 76    CMP #&76
&4a23 6e aa 19 ROR &19aa ; player_is_east_of_76                     # Set &80 if player x >= &76, clear if not
&4a26 20 ff 34 JSR &34ff ; consider_retrieving_object
&4a29 a5 dd    LDA &dd ; player_object_held
&4a2b 48       PHA ; player_object_held
&4a2c 20 a8 01 JSR &01a8 ; process_actions
&4a2f a9 10    LDA #&10 ; OBJECT_FLAG_TELEPORTING
&4a31 24 6f    BIT &6f ; this_object_flags
&4a33 d0 03    BNE &4a38 ; skip_updating_player_angle_facing_and_sprite
; not_teleporting
&4a35 20 95 37 JSR &3795 ; update_player_angle_facing_and_sprite
; skip_updating_player_angle_facing_and_sprite
&4a38 20 fc 30 JSR &30fc ; update_player_aiming_angle
&4a3b 68       PLA ; player_object_held
&4a3c a8       TAY
&4a3d 30 18    BMI &4a57 ; not_holding_object
; is_holding_object
&4a3f a5 37    LDA &37 ; this_object_x_flip                         # &80 set if flipped horizontally
&4a41 45 6f    EOR &6f ; this_object_flags                          # &80 set if flipped horizontally
&4a43 10 12    BPL &4a57 ; skip_setting_held_object_position        # If either, position held object on left of player
&4a45 be 70 08 LDX &0870,Y ; objects_sprite
&4a48 bd 0c 5e LDA &5e0c,X ; sprites_width_and_horizontal_flip_table
&4a4b 24 37    BIT &37 ; this_object_x_flip
&4a4d 20 4c 32 JSR &324c ; invert_if_positive
&4a50 a2 00    LDX #&00 ; OBJECT_SLOT_PLAYER
&4a52 24 6f    BIT &6f ; this_object_flags
&4a54 20 38 2a JSR &2a38 ; add_A_to_position
; skip_setting_held_object_position
; not_holding_object
&4a57 24 c5    BIT &c5 ; every_four_frames
&4a59 10 0d    BPL &4a68 ; skip_playing_energy_level_bells          # Once every four frames,
&4a5b a5 25    LDA &25 ; energy_level_bells_remaining
&4a5d f0 09    BEQ &4a68 ; skip_playing_energy_level_bells          # if bells for energy level are pending,
&4a5f c6 25    DEC &25 ; energy_level_bells_remaining
&4a61 20 fa 13 JSR &13fa ; play_sound
&4a64 17 e3 2f 82                                                   # Play sound for energy level bell
; skip_playing_energy_level_bells
&4a68 ce d6 29 DEC &29d6 ; player_firing_cooldown                   # Non-zero if player has fired recently
&4a6b d0 03    BNE &4a70 ; skip_suppressing_fire
&4a6d 4e 78 12 LSR &1278 ; action_keys_pressed + &0d (SPACE)        # Clear top bit to prevent firing again
; skip_suppressing_fire
&4a70 a5 36    LDA &36 ; player_blaster_timer                       # Negative if player has fired blaster recently
&4a72 10 13    BPL &4a87 ; skip_discharging_blaster
&4a74 e6 36    INC &36 ; player_blaster_timer
&4a76 20 e8 40 JSR &40e8 ; start_explosion_timer
&4a79 a9 0a    LDA #&0a                                             # Blaster behaves as explosion of duration 10
;                                                                   # i.e. causes damage to other objects (but not player,
;                                                                   # because the player is the exploding object)
&4a7b 85 3d    STA &3d ; this_object_tertiary_data_offset (explosion duration)
&4a7d 20 f8 13 JSR &13f8 ; play_sound_on_channel_zero
&4a80 17 03 11 04                                                   # Play sound for discharging blaster
&4a84 20 9c 4f JSR &4f9c ; update_explosion
; skip_discharging_blaster
&4a87 60       RTS

# Plasma balls (OBJECT_PLASMA_BALL)
# =================================

; update_plasma_ball                                                # Called with Y = object touching, or negative if none
&4a88 30 08    BMI &4a92 ; not_touching_other_object
&4a8a b9 60 08 LDA &0860,Y ; objects_type
&4a8d 20 d0 1f JSR &1fd0 ; check_if_object_collides_with_plasma_ball
&4a90 d0 21    BNE &4ab3 ; turn_object_into_fireball_of_duration_thirteen # Plasma balls turn into fireballs on contact
; not_touching_other_object
&4a92 a5 1f    LDA &1f ; this_object_in_water                       # Positive if object is in any water
&4a94 05 d9    ORA &d9 ; rnd_state
&4a96 05 dc    ORA &dc ; rnd_state + 3
&4a98 10 2e    BPL &4ac8 ; remove_plasma_ball_or_fireball           # If underwater, 1 in 4 chance of removing plasma ball
&4a9a 20 1f 25 JSR &251f ; reduce_energy_by_one                     # Plasma balls have a limited lifespan; returns A = energy
&4a9d f0 2c    BEQ &4acb ; to_set_object_for_removal
&4a9f c9 03    CMP #&03
&4aa1 a0 a0    LDY #&a0                                             # Clear &01 to not add object velocity to particles
&4aa3 a9 03    LDA #&03
&4aa5 b0 04    BCS &4aab ; set_plasma_particles_flag                # Branch if plasma ball has energy >= &03
; add_plasma_particles
&4aa7 a0 a1    LDY #&a1                                             # Set &01 to add object velocity to particles
&4aa9 a9 1e    LDA #&1e
; set_plasma_particles_flags
&4aab 8c 0c 02 STY &020c ; particle_types_flags_table + &00 (PARTICLE_PLASMA)
&4aae a0 00    LDY #&00 ; PARTICLE_PLASMA
&4ab0 4c 8e 21 JMP &218e ; add_particles

; turn_object_into_fireball_of_duration_thirteen
&4ab3 a9 0d    LDA #&0d                                             # &0d > &08, so will cause big damage at start (&4aee)
&4ab5 2c a9 07 BIT &07a9 ; (nop)
; turn_object_into_fireball_of_duration_seven
#4ab6          LDA #&07                                             # &07 < &08, so will not cause big damage
&4ab8 2c a9 02 BIT &02a9 ; (nop)
; turn_object_into_fireball_of_duration_two
#4ab9          LDA #&02                                             # &02 < &08, so will not cause big damage
&4abb 85 12    STA &12 ; this_object_timer
&4abd 85 15    STA &15 ; this_object_energy
&4abf a9 00    LDA #&00                                             # Zero to indicate fireball is from exploding object
&4ac1 85 0e    STA &0e ; this_object_target_object (fireball type)
&4ac3 a9 37    LDA #&37 ; OBJECT_FIREBALL
&4ac5 4c 86 32 JMP &3286 ; change_object_type

; remove_plasma_ball_or_fireball
&4ac8 20 a7 4a JSR &4aa7 ; add_plasma_particles
; to_set_object_for_removal
&4acb 4c 29 25 JMP &2529 ; set_object_for_removal

# Fireballs (OBJECT_FIREBALL)
# Moving fireballs (OBJECT_MOVING_FIREBALL)
# =========================================
# Spawned from pipes or nest, also created by explosions.
#
# objects_target_object_and_flags is used for fireball type; non-zero if permanent fireball, zero if temporary
# objects_timer is used for lifespan for temporary fireballs

; fireball_palettes_table
;      0  1  2  3  4  5  6  7
&4ace 10 34 34 34 10 34 10 34                                       # &10 = kyR, &34 = rwY

; update_fireball
&4ad6 a5 20    LDA &20 ; this_object_waterline                      # Negative if completely under water
&4ad8 25 db    AND &db ; rnd_state + 2
&4ada 25 da    AND &da ; rnd_state + 1                              # If completely underwater, 1 in chance of removing fireball
&4adc 30 ea    BMI &4ac8 ; remove_plasma_ball_or_fireball
&4ade a5 0e    LDA &0e ; this_object_target_object (fireball type)  # Non-zero if spawned from tertiary object,
&4ae0 d0 68    BNE &4b4a ; update_permanent_fireball                # zero if from exploding object
; update_temporary_fireball
&4ae2 c6 12    DEC &12 ; this_object_timer (fireball timer)         # Temporary fireballs have a limited lifespan
&4ae4 30 e5    BMI &4acb ; to_set_object_for_removal
&4ae6 a2 0a    LDX #&0a                                             # Temporary fireballs causes 10 damage
; consider_fireball_damage_and_animate
&4ae8 a5 07    LDA &07 ; this_object_frame_counter_sixteen
&4aea d0 08    BNE &4af4 ; not_big_damage                           # Every sixteen frames,
&4aec a5 12    LDA &12 ; this_object_timer (fireball timer)
&4aee c9 08    CMP #&08
&4af0 90 02    BCC &4af4 ; not_big_damage
&4af2 a2 5a    LDX #&5a                                             # Fireball causes 90 damage at start of long explosions
;                                                                   # Permanent fireballs cause 90 damage 248 frames in 256
; not_big_damage
&4af4 98       TYA ; object touching
&4af5 30 14    BMI &4b0b ; not_touching_other_object
&4af7 d0 06    BNE &4aff ; not_touching_player
&4af9 2c 14 08 BIT &0814 ; player_fire_immunity_device_collected    # Negative if fire immunity device collected
&4afc 10 01    BPL &4aff ; not_immune_to_fire
&4afe aa       TAX                                                  # Set to zero to cause no damage if so
; not_immune_to_fire
; not_touching_player
&4aff 8a       TXA ; damage
&4b00 20 a6 24 JSR &24a6 ; damage_object                            # Fireball causes damage depending on type and frame
&4b03 20 c9 1f JSR &1fc9 ; check_if_object_Y_collides_with_lightning_or_fireball
&4b06 f0 03    BEQ &4b0b ; not_touching_other_object
&4b08 20 b4 0b JSR &0bb4 ; set_this_object_velocities_from_object_Y # Fireball follows touching object when damaging it
; not_touching_other_object
&4b0b 20 87 25 JSR &2587 ; rnd
&4b0e 85 37    STA &37 ; this_object_x_flip                         # Flip horizontally at random
&4b10 0a       ASL A
&4b11 85 39    STA &39 ; this_object_y_flip                         # Flip vertically at random
&4b13 a5 12    LDA &12 ; this_object_timer (fireball timer)
&4b15 29 07    AND #&07
&4b17 aa       TAX
&4b18 bd ce 4a LDA &4ace,X ; fireball_palettes_table
&4b1b 85 73    STA &73 ; this_object_palette
&4b1d a9 c0    LDA #&c0                                             # Set fireball particles moving upwards
&4b1f 85 b5    STA &b5 ; angle
&4b21 a0 21    LDY #&21 ; PARTICLE_FIREBALL
&4b23 4c 8c 21 JMP &218c ; add_particle

; update_moving_fireball
&4b26 a2 04    LDX #&04                                             # Moving fireball causes 4 damage
&4b28 20 e8 4a JSR &4ae8 ; consider_fireball_damage_and_animate
&4b2b 20 aa 28 JSR &28aa ; set_this_object_position_from_previous_position
&4b2e a5 3b    LDA &3b ; this_object_touching
&4b30 f0 03    BEQ &4b35 ; is_touching_player                       # Moving fireball follows player if touching
&4b32 20 4c 41 JSR &414c ; use_previous_velocities
; is_touching_player
&4b35 20 72 46 JSR &4672 ; move_fireball
&4b38 24 1f    BIT &1f ; this_object_in_water                       # Positive if object is in any water
&4b3a 30 08    BMI &4b44 ; not_underwater
&4b3c a9 fc    LDA #&fc                                             # Accelerate upwards, out of water
&4b3e 85 42    STA &42 ; this_object_acceleration_y
&4b40 24 20    BIT &20 ; this_object_waterline                      # Negative if completely under water
&4b42 30 84    BMI &4ac8 ; remove_plasma_ball_or_fireball
; not_underwater
&4b44 20 01 1f JSR &1f01 ; apply_acceleration_to_velocities
&4b47 4c 31 2a JMP &2a31 ; add_velocities_to_position

; update_permanent_fireball
&4b4a 20 ae 28 JSR &28ae ; set_this_object_position_from_previous_position_except_y_fraction
&4b4d 24 da    BIT &da ; rnd_state + 1                              # 1 in 2 chance of moving permanent fireball
&4b4f 10 58    BPL &4ba9 ; set_this_object_velocities_to_zero_and_position_from_previous_position
&4b51 a5 da    LDA &da ; rnd_state + 1
&4b53 29 0f    AND #&0f                                             # Random component
&4b55 65 07    ADC &07 ; this_object_frame_counter_sixteen          # plus rhythmic component
&4b57 0a       ASL A
&4b58 65 51    ADC &51 ; this_object_y_fraction
&4b5a 69 18    ADC #&18
&4b5c 85 51    STA &51 ; this_object_y_fraction
&4b5e c6 12    DEC &12 ; this_object_timer (fireball timer)         # Only used for animation, not lifespan
&4b60 a2 14    LDX #&14                                             # Permanent fireballs causes 20 damage
&4b62 d0 84    BNE &4ae8 ; consider_fireball_damage_and_animate     # Always branches

# Placeholders (OBJECT_PLACEHOLDER)
# =================================
# Tertiary data byte is as follows:
#
# 8....... if set, placeholder needs primary object creating
# .4218421 type of object

; update_placeholder_object                                         # Called with Y = object touching, or negative if none
&4b64 30 05    BMI &4b6b ; not_touching_other_object
&4b66 20 c5 49 JSR &49c5 ; check_if_object_can_trigger_switches     # Returns carry set if object can trigger
&4b69 b0 14    BCS &4b7f ; convert_placeholder_object
; not_touching_other_object
&4b6b a4 07    LDY &07 ; this_object_frame_counter_sixteen          # Every sixteen frames,
&4b6d d0 3a    BNE &4ba9 ; set_this_object_velocities_to_zero_and_position_from_previous_position
&4b6f a5 bc    LDA &bc ; this_object_data
&4b71 20 b0 2d JSR &2db0 ; get_range_for_object_type_A
&4b74 e0 09    CPX #&09 ; OBJECT_RANGE_EQUIPMENT                    # If it isn't a collectable,
&4b76 f0 31    BEQ &4ba9 ; set_this_object_velocities_to_zero_and_position_from_previous_position
;                                                                   # i.e. it is a robot, Chatter or the boulder,
&4b78 a2 00    LDX #&00 ; OBJECT_SLOT_PLAYER                        # Can the placeholder object see the player?
&4b7a 20 9a 35 JSR &359a ; check_for_obstruction_between_objects_80 # Returns carry clear if no obstruction to player
&4b7d b0 2a    BCS &4ba9 ; set_this_object_velocities_to_zero_and_position_from_previous_position
; convert_placeholder_object                                        # If so, convert into a proper object
&4b7f a5 bc    LDA &bc ; this_object_data                           # Top bit is clear, as placeholder is already primary
&4b81 85 41    STA &41 ; this_object_type
&4b83 a9 ff    LDA #&ff
&4b85 85 15    STA &15 ; this_object_energy
&4b87 60       RTS

# Cyan/yellow/green key (OBJECT_CYAN_YELLOW_GREEN_KEY)
# Red/yellow/green key (OBJECT_RED_YELLOW_GREEN_KEY)
# Green/yellow/red key (OBJECT_GREEN_YELLOW_RED_KEY)
# Yellow/white/red key (OBJECT_YELLOW_WHITE_RED_KEY)
# Red/magenta/red key (OBJECT_RED_MAGENTA_RED_KEY)
# Blue/cyan/green key (OBJECT_BLUE_CYAN_GREEN_KEY)
# Jetpack booster (OBJECT_JETPACK_BOOSTER)
# Pistol (OBJECT_PISTOL)
# Icer (OBJECT_ICER)
# Blaster (OBJECT_BLASTER)
# Plasma gun (OBJECT_PLASMA_GUN)
# Protection suit (OBJECT_PROTECTION_SUIT)
# Fire immunity device (OBJECT_FIRE_IMMUNITY_DEVICE)
# Mushroom immunity pill (OBJECT_MUSHROOM_IMMUNITY_PILL)
# Whistle one (OBJECT_WHISTLE_ONE)
# Whistle two (OBJECT_WHISTLE_TWO)
# Radiation immunity pill (OBJECT_RADIATION_IMMUNITY_PILL)
# ========================================================
# objects_energy is used for disturbance

; update_collectable_object
&4b88 a5 dd    LDA &dd ; player_object_held
&4b8a c5 aa    CMP &aa ; this_object
&4b8c d0 0f    BNE &4b9d ; not_holding
; is_holding
&4b8e a6 41    LDX &41 ; this_object_type
&4b90 de b5 07 DEC &07b5,X ; player_collected - &51 (OBJECT_CYAN_YELLOW_GREEN_KEY) # Set to negative to indicate object collected
&4b93 20 fa 13 JSR &13fa ; play_sound
&4b96 72 a5 7b 85                                                   # Play sound for collecting object
&4b91 4c 29 25 JMP &2529 ; set_object_for_removal
; not_holding
; consider_disturbing_object                                        # Collectables are static until touched by another object
&4b9d a4 3b    LDY &3b ; this_object_touching                       # Negative if not touching another object
&4b9f 30 04    BMI &4ba5 ; not_touching_other_object
; is_touching
&4ba1 06 15    ASL &15 ; this_object_energy (collectable disturbed)
&4ba3 46 15    LSR &15 ; this_object_energy (collectable disturbed) # Clear top bit to indicate object disturbed
; not_touching_other_object
&4ba5 24 15    BIT &15 ; this_object_energy (collectable disturbed) # Top bit clear if object has been disturbed
&4ba7 10 6b    BPL &4c14 ; leave                                    # Leave if object touching another object
; update_bush
; set_this_object_velocities_to_zero_and_position_from_previous_position
&4ba9 20 a3 28 JSR &28a3 ; set_this_object_velocities_to_zero
&4bac 4c aa 28 JMP &28aa ; set_this_object_position_from_previous_position

# Small hive (OBJECT_SMALL_HIVE)
# Large hive (OBJECT_LARGE_HIVE)
# ==============================
# Tertiary data byte is as follows:
#
# 8....... if set, hive needs primary object creating
# .42184.. object type of spawn
# ......21 if non-zero, hive is inactive
#
# Tertiary tile byte sets aggressiveness of spawn
#
# 8....... if set, consider spawning when nest or pipe is plotted
#
# objects_state is used for spawn type

; update_hive                                                       # Called with A = this_object_data
&4baf 4a       LSR A                                                # .42184.. -> ..42184.
&4bb0 4a       LSR A                                                #          -> ...42184
&4bb1 85 11    STA &11 ; this_object_state (hive spawn type)
&4bb3 20 e1 3b JSR &3be1 ; consider_absorbing_object_touched        # Hives absorb their spawn
&4bb6 a9 46    LDA #&46
&4bb8 20 2e 35 JSR &352e ; give_object_minimum_energy               # Hives have a minimum energy of 70
&4bbb 24 c5    BIT &c5 ; every_four_frames
&4bbd 10 55    BPL &4c14 ; leave                                    # Once every four frames,
&4bbf a5 bc    LDA &bc ; this_object_data
&4bc1 29 03    AND #&03
&4bc3 d0 4f    BNE &4c14 ; leave                                    # If the hive is active,
&4bc5 a5 11    LDA &11 ; this_object_state (hive spawn type)
&4bc7 20 18 3c JSR &3c18 ; count_objects_of_type_A                  # Count the number of spawned objects currently present
&4bca 20 87 25 JSR &2587 ; rnd
&4bcd 25 d9    AND &d9 ; rnd_state
&4bcf 25 db    AND &db ; rnd_state + 2
&4bd1 29 07    AND #&07
&4bd3 c5 9f    CMP &9f ; count
&4bd5 90 3d    BCC &4c14 ; leave                                    # Less likely to spawn when more spawn already present
&4bd7 a9 0e    LDA #&0e ; OBJECT_BIG_FISH
&4bd9 a0 86    LDY #&86 ; OBJECT_RANGE_FLYING_ENEMIES
&4bdb 20 2a 3c JSR &3c2a ; find_object                              # Returns positive if object found
&4bde 10 34    BPL &4c14 ; leave                                    # Don't spawn if big fish or flying enemies are present
&4be0 20 fa 13 JSR &13fa ; play_sound
&4be3 33 f3 4f 35                                                   # Play sound for hive spawning
&4be7 a5 37    LDA &37 ; this_object_x_flip
&4be9 29 80    AND #&80
&4beb 85 b5    STA &b5 ; angle                                      # Spawn comes out either left or right, depending on flip
&4bed a9 20    LDA #&20 ; magnitude
&4bef 20 57 23 JSR &2357 ; calculate_vector_from_magnitude_and_angle
&4bf2 a5 11    LDA &11 ; this_object_state (hive spawn type)
&4bf4 20 b8 33 JSR &33b8 ; create_child_object                      # Returns carry clear if object created, X = slot
&4bf7 b0 1b    BCS &4c14 ; leave
&4bf9 a5 aa    LDA &aa ; this_object
&4bfb 9d 06 09 STA &0906,X ; objects_target_object_and_flags        # Set hive as target for spawn
&4bfe a9 20    LDA #&20 ; 1 in 8 chance of targeting player
&4c00 24 37    BIT &37 ; this_object_x_flip
&4c02 30 02    BMI &4c06 ; set_aggressiveness                       # If the hive isn't horizontally flipped,
&4c04 a9 a0    LDA #&a0 ; 5 in 8 chance of targeting player         # its spawn are more aggressive
; set_aggressiveness
&4c06 9d 76 09 STA &0976,X ; (wasp or piranha aggressiveness)
&4c09 0a       ASL A                                                # Set carry if more aggressive
&4c0a 90 08    BCC &4c14 ; leave
&4c0c bd d6 08 LDA &08d6,X ; objects_palette
&4c0f 49 3b    EOR #&3b                                             # More aggressive wasps use palette &7c (ywW)
;                                                                   # More aggressive piranha use palette &68 (ggC)
&4c11 9d d6 08 STA &08d6,X ; objects_palette
; leave
&4c14 60       RTS

# Engine fire (OBJECT_ENGINE_FIRE)
# ================================
# Tertiary data byte is as follows:
#
# 8....... if set, engine fire needs primary object creating
# ......21 if non-zero, engine is inactive
#
# objects_timer is used for timer

; update_engine_fire                                                # Called with A = X = this_object_data, Y = object touching 
&4c15 29 03    AND #&03                                             # Non-zero if engine switched off
&4c17 d0 5f    BNE &4c78 ; reset_and_hide_fire
&4c19 e6 11    INC &11 ; this_object_state (engine fire timer)      # Switch off engine fire after 256 frames
&4c1b 10 04    BPL &4c21 ; skip_switching_off
&4c1d e8       INX                                                  # Set lowest two bits to &02 to switch off fire
&4c1e e8       INX
&4c1f 86 bc    STX &bc ; this_object_data
; skip_switching_off
&4c21 a5 dc    LDA &dc ; rnd_state + 3
&4c23 c5 11    CMP &11 ; this_object_state (engine fire timer)
&4c25 90 53    BCC &4c7a ; hide_fire                                # Fire more likely to be hidden later in burn
&4c27 0a       ASL A
&4c28 85 37    STA &37 ; this_object_x_flip                         # Set horizontal flip at random
&4c2a 0a       ASL A
&4c2b 85 39    STA &39 ; this_object_y_flip                         # Set vertical flip at random
&4c2d 98       TYA
&4c2e 30 04    BMI &4c34 ; not_touching_other_object
&4c30 aa       TAX
&4c31 fe e6 08 INC &08e6,X ; objects_velocity_x                     # Accelerate any object touching the fire
; not_touching_other_object
&4c34 a0 ff    LDY #&ff
&4c36 84 87    STY &87 ; new_particles_x_fraction
&4c38 a5 db    LDA &db ; rnd_state + 2
&4c3a 85 89    STA &89 ; new_particles_y_fraction
&4c3c a5 53    LDA &53 ; this_object_x
&4c3e 85 8b    STA &8b ; new_particles_x
&4c40 a5 55    LDA &55 ; this_object_y
&4c42 85 8d    STA &8d ; new_particles_y
&4c44 c8       INY ; 0
&4c45 84 b5    STY &b5 ; angle                                      # Engine fire particles move right
&4c47 a0 37    LDY #&37 ; PARTICLE_ENGINE
&4c49 20 8c 21 JSR &218c ; add_particle                             # Add particle for engine fire
&4c4c a5 c0    LDA &c0 ; frame_counter
&4c4e 18       CLC
&4c4f 65 55    ADC &55 ; this_object_y
&4c51 29 03    AND #&03
&4c53 d0 13    BNE &4c68 ; skip_blowing_other_objects_away          # 1 in 4 chance of fire blowing other objects away
&4c55 38       SEC
&4c56 66 28    ROR &28 ; acceleration_damages_targets               # Set &80 to damage targets accelerated too much
&4c58 a9 50    LDA #&50 ; 10 tiles
&4c5a 85 35    STA &35 ; acceleration_power
&4c5c a9 14    LDA #&14 ; angle range                               # +/- 28.125 degrees
&4c5e 20 3c 34 JSR &343c ; accelerate_all_objects_within_angle
&4c61 20 f8 13 JSR &13f8 ; play_sound_on_channel_zero
&4c64 70 c2 6e a3                                                   # Play sound for engine fire
; skip_blowing_other_objects_away
&4c68 a0 34    LDY #&34 ; rwY                                       # Make fire visible
&4c6a a5 c0    LDA &c0 ; frame_counter
&4c6c 2a       ROL A
&4c6d 2a       ROL A
&4c6e 2a       ROL A
&4c6f 2a       ROL A
&4c70 65 c0    ADC &c0 ; frame_counter
&4c72 29 3f    AND #&3f                                             # Set random x fraction between &90 and &cf
&4c74 69 90    ADC #&90
&4c76 d0 06    BNE &4c7e ; set_palette_and_x_fraction               # Always branches
; reset_and_hide_fire
&4c78 85 11    STA &11 ; this_object_state (engine fire timer)      # Set to zero to reset fire
; hide_fire
&4c7a a0 00    LDY #&00 ; kyK
&4c7c a9 40    LDA #&40                                             # Hide fire behind foreground part of tile
; set_palette_and_x_fraction
&4c7e 84 73    STY &73 ; this_object_palette
&4c80 85 4f    STA &4f ; this_object_x_fraction
&4c82 60       RTS

# Horizontal metal door (OBJECT_HORIZONTAL_METAL_DOOR)
# Vertical metal door (OBJECT_VERTICAL_METAL_DOOR)
# Horizontal stone door (OBJECT_HORIZONTAL_STONE_DOOR)
# Vertical stone door (OBJECT_VERTICAL_STONE_DOOR)
# ====================================================
# Tertiary data byte is as follows:
#
# 8....... if set, door needs primary object creating
# .421.... door colour
# ....8... if set, door is slow, is being or has been destroyed (DOOR_FLAG_SLOW_OR_DESTROYED)
# .....4.. if set, door is moving (DOOR_FLAG_MOVING)
# ......2. if set, door is opening; if clear, door is closing (DOOR_FLAG_OPENING)
# .......1 if set, door is locked (DOOR_FLAG_LOCKED)
#
# objects_state is used for tile x or tile y
# objects_tx is used for open fraction
# objects_ty is used for orientation

; update_door                                                       # Called with Y = object touching, or negative if none
&4c83 30 08    BMI &4c8d ; not_touched
&4c85 20 c5 49 JSR &49c5 ; check_if_object_can_trigger_switches     # Returns carry set if object can trigger door
&4c88 b0 03    BCS &4c8d ; was_touched
&4c8a 38       SEC
&4c8b 66 3b    ROR &3b ; this_object_touching                       # Otherwise, set top bit to indicate door wasn't touched
; was_touched
; not_touched
&4c8d 46 39    LSR &39 ; this_object_y_flip                         # Clear top bit to prevent door being flipped vertically
&4c8f a6 16    LDX &16 ; this_object_ty (door orientation)          # 0 for x, 2 for y
&4c91 a4 11    LDY &11 ; this_object_state (door tile x or y)
&4c93 94 53    STY &53,X ; this_object_x                            # Fix door to tile
&4c95 a9 ff    LDA #&ff
&4c97 95 4f    STA &4f,X ; this_object_x_fraction                   # Fraction will be updated later
&4c99 a5 3d    LDA &3d ; this_object_tertiary_data_offset           # Stop door being created as tertiary object when tile considered
&4c9b 8d 99 35 STA &3599 ; door_to_suppress                         # Unnecessary code; performed in check_for_obstruction_between_objects
&4c9e 20 c5 0b JSR &0bc5 ; check_if_object_hit_by_remote_control    # Returns carry clear if object hit by remote control
&4ca1 b0 05    BCS &4ca8 ; skip_toggling_door_lock
&4ca3 a9 40    LDA #&40                                             # &40 set to use data bits for door
&4ca5 20 ac 31 JSR &31ac ; consider_toggling_lock                   # If so, consider unlocking door
; skip_toggling_door_lock
&4ca8 6e 99 35 ROR &3599 ; door_to_suppress                         # Allow door to be created when tile next considered
&4cab a5 bc    LDA &bc ; this_object_data
&4cad 09 04    ORA #&04 ; DOOR_FLAG_MOVING                          # Set door moving by default
&4caf 48       PHA ; data
&4cb0 4a       LSR A                                                # 84218421 -> .8421842 1
&4cb1 6a       ROR A                                                #          -> 1.842184 2
&4cb2 6a       ROR A                                                #          -> 21.84218 4 ; &80 set if &02 was set
&4cb3 85 9f    STA &9f ; door_opening_and_locked_flags              #                        ; &40 set if &01 was set
&4cb5 4a       LSR A                                                # 21.84218 -> 421.8421 8 ; carry set if &08 was set
&4cb6 29 07    AND #&07                                             #          -> .....421 door colour
&4cb8 85 9e    STA &9e ; door_colour
&4cba 29 03    AND #&03                                             #          -> ......21 door colour pair
&4cbc aa       TAX ; door colour pair
&4cbd 08       PHP ; carry set if slow or being destroyed
&4cbe a5 15    LDA &15 ; this_object_energy
&4cc0 dd 76 4d CMP &4d76,X ; doors_energy_table                     # If the door has more than the energy needed to
&4cc3 a9 ff    LDA #&ff                                             # destroy its type, give it full energy
&4cc5 b0 0e    BCS &4cd5 ; set_door_energy
&4cc7 28       PLP ; carry set if slow or being destroyed
&4cc8 08       PHP ; carry set if slow or being destroyed
&4cc9 a9 00    LDA #&00                                             # If DOOR_FLAG_SLOW_OR_DESTROYED (&08) was
&4ccb b0 08    BCS &4cd5 ; set_door_energy                          # set, give the door zero energy to set it exploding
&4ccd 28       PLP ; carry set if slow or being destroyed
&4cce 68       PLA ; data
&4ccf 09 08    ORA #&08 ; DOOR_FLAG_SLOW_OR_DESTROYED               # Otherwise, mark it as being destroyed
&4cd1 48       PHA ; data
&4cd2 08       PHP ; carry set if slow or being destroyed
&4cd3 b0 02    BCS &4cd7 ; skip_setting_door_energy                 # Bug: never branches
; set_door_energy
&4cd5 85 15    STA &15 ; this_object_energy
; skip_setting_door_energy
&4cd7 bd 72 4d LDA &4d72,X ; doors_speed_table                      # Doors move at a speed depending on their colour
&4cda 28       PLP ; carry set if slow or being destroyed
&4cdb 90 02    BCC &4cdf ; not_slow_or_being_destroyed
&4cdd a9 01    LDA #&01                                             # unless slow or being destroyed
; not_slow_or_being_destroyed
&4cdf 24 9f    BIT &9f ; door_opening_and_locked_flags              # &80 set if DOOR_FLAG_OPENING (&02) was set
&4ce1 30 09    BMI &4cec ; set_door_speed
; is_closing
&4ce3 4a       LSR A                                                # Doors close at half the speed they open
&4ce4 49 ff    EOR #&ff
&4ce6 24 3b    BIT &3b ; this_object_touching                       # Positive if door is touching something
&4ce8 30 02    BMI &4cec ; set_door_speed
&4cea a9 ff    LDA #&ff                                             # unless touching something, when they close very slowly
; set_door_speed
&4cec 85 9c    STA &9c ; door_speed
&4cee a5 14    LDA &14 ; this_object_tx (door open fraction)
&4cf0 49 80    EOR #&80
&4cf2 38       SEC
&4cf3 e5 9c    SBC &9c ; door_speed                                 # Set V if (fraction EOR &80) crosses &80, i.e. if
&4cf5 50 44    BVC &4d3b ; not_at_end_of_track                      # fraction crosses &00, i.e. door is at end of track
; at_end_of_track
&4cf7 20 7f 32 JSR &327f ; prevent_overflow
&4cfa a8       TAY ; door open fraction EOR &80
&4cfb 10 0c    BPL &4d09 ; stop_door                                # Positive if door has closed; otherwise, door is open
&4cfd 8a       TXA ; door colour pair                               # Door types 0 (cyG, metal) and 4 (rmb, stone) open, then close
&4cfe d0 0d    BNE &4d0d ; skip_stopping_door                       # Other door types stay open or closed
&4d00 ad 19 08 LDA &0819 ; door_timer
&4d03 c9 14    CMP #&14 ; 20                                        # After forty frames,
&4d05 b0 06    BCS &4d0d ; skip_stopping_door
&4d07 90 19    BCC &4d22 ; toggle_door_opening                      # Always branches
; stop_door
&4d09 68       PLA ; data
&4d0a 29 fb    AND #&fb ; !DOOR_FLAG_MOVING                         # Clear DOOR_FLAG_MOVING (&04) to stop door moving
&4d0c 48       PHA ; data
; skip_stopping_door
&4d0d 24 3b    BIT &3b ; this_object_touching
&4d0f 30 29    BMI &4d3a ; set_door_open_fraction_from_Y
&4d11 24 9f    BIT &9f ; door_opening_and_locked_flags              # &40 set if DOOR_FLAG_LOCKED (&01) was set
&4d13 70 25    BVS &4d3a ; set_door_open_fraction_from_Y
; not_locked
&4d15 8a       TXA ; door colour pair
&4d16 d0 0a    BNE &4d22 ; toggle_door_opening                      # If door type 0 (cyG, metal) or 4 (rmb, stone),
&4d18 ad 19 08 LDA &0819 ; door_timer
&4d1b d0 1d    BNE &4d3a ; set_door_open_fraction_from_Y
&4d1d a9 3c    LDA #&3c ; 60                                        # prepare timer for automatic closing
&4d1f 8d 19 08 STA &0819 ; door_timer
; toggle_door_opening
&4d22 68       PLA ; data
&4d23 49 02    EOR #&02 ; DOOR_FLAG_OPENING                         # Swap between opening and closing
&4d25 48       PHA ; data
&4d26 29 02    AND #&02 ; DOOR_FLAG_OPENING
&4d28 f0 09    BEQ &4d33 ; is_closing
; is_opening
&4d2a 20 fa 13 JSR &13fa ; play_sound
&4d2d c7 c3 c1 13                                                   # Play sound for door opening; returns carry set
&4d31 b0 07    BCS &4d3a ; set_door_open_fraction_from_Y            # Always branches
; is_closing
&4d33 20 fa 13 JSR &13fa ; play_sound
&4d36 c7 c3 c1 03                                                   # Play sound for door closing
; set_door_open_fraction_from_Y
&4d3a 98       TYA ; door open fraction EOR &80
; not_at_end_of_track
&4d3b 49 80    EOR #&80                                             # Undo EOR at &4cf0
&4d3d a6 16    LDX &16 ; this_object_ty (door orientation)
&4d3f a8       TAY ; door open fraction
&4d40 38       SEC
&4d41 e5 14    SBC &14 ; this_object_tx (door open fraction)
&4d43 c9 80    CMP #&80
&4d45 6a       ROR A                                                # Set sign of velocity from door open fraction
&4d46 95 43    STA &43,X ; this_object_velocity_x
&4d48 98       TYA ; door open fraction
&4d49 85 14    STA &14 ; this_object_tx (door open fraction)
&4d4b 18       CLC
&4d4c 69 10    ADC #&10 ; 1 pixel                                   # Offset door by one pixel horizontally
&4d4e 95 4f    STA &4f,X ; this_object_x_fraction
&4d50 a5 11    LDA &11 ; this_object_state (door tile x or y)
&4d52 69 00    ADC #&00
&4d54 95 53    STA &53,X ; this_object_x
&4d56 68       PLA ; data
&4d57 a4 15    LDY &15 ; this_object_energy
&4d59 d0 02    BNE &4d5d ; not_being_destroyed
&4d5b 09 04    ORA #&04 ; DOOR_FLAG_MOVING                          # Set door moving when being destroyed
; not_being_destroyed
&4d5d 85 bc    STA &bc ; this_object_data
&4d5f a6 3d    LDX &3d ; this_object_tertiary_data_offset
&4d61 9d 86 09 STA &0986,X ; tertiary_objects_data
&4d64 a6 9e    LDX &9e ; door_colour
&4d66 bd 7a 4d LDA &4d7a,X ; doors_palette_table
&4d69 24 9f    BIT &9f ; door_opening_and_locked_flags              # &40 set if DOOR_FLAG_LOCKED (&01) was set
&4d6b 70 02    BVS &4d6f ; set_door_palette
; is_unlocked
&4d6d 29 0f    AND #&0f                                             # Set colour three of door to black if unlocked
; set_door_palette
&4d6f 85 73    STA &73 ; this_object_palette
&4d71 60       RTS

; doors_speed_table                                                 # 0 = type &00 (cyG) and &04 (rmB)
;      0  1  2  3                                                   # 1 = type &01 (ryG) and &05 (rmR)
&4d72 20 10 08 20                                                   # 2 = type &02 (gyR) and &06 (bcG)
                                                                    # 3 = type &03 (ywR) and &07 (mwB)

; doors_energy_table                                                # Minimum amount of energy door can have before exploding
;      0  1  2  3                                                   # This gives the following effective minimum energies:
&4d76 80 74 c0 80                                                   # cyG metal doors have a minimum energy of 128
                                                                    # ryG metal doors have a minimum energy of 140
                                                                    # gyR metal doors have a minimum energy of 64
                                                                    # ywR metal doors have a minimum energy of 128
                                                                    # rmB stone doors have a minimum energy of 128
                                                                    # rmR metal doors have a minimum energy of 140
                                                                    # mwB metal and stone doors have a minimum energy of 128

; doors_palette_table
&4d79 2b ; &00 : cyG (becomes &0b, cyK)                             # Cyan frame, yellow highlights, green lock (metal)
&4d7a 2d ; &01 : ryG (becomes &0d, ryK)                             # Red frame, yellow highlights, green lock (metal)
&4d7b 15 ; &02 : gyR (becomes &05, gyK)                             # Green frame, yellow highlights, red lock (metal)
&4d7c 1c ; &03 : ywR (becomes &0c, ywK)                             # Yellow frame, white highlights, red lock (metal)
&4d7d 42 ; &04 : rmB (becomes &02, rmK)                             # Red frame, magenta highlights, blue lock (stone)
&4d7e 12 ; &05 : rmR (becomes &02, rmK)                             # Red frame, magenta highlights, red lock (metal)
&4d7f 26 ; &06 : bcG (becomes &06, bcK)                             # Blue frame, cyan highlights, green lock (unused)
&4d80 4e ; &07 : mwB (becomes &0e, mwK)                             # Magenta frame, white highlights, blue lock (metal or stone)

# Transporter beam (OBJECT_TRANSPORTER_BEAM)
# ==========================================
# Tertiary data byte is as follows:
#
# 8....... if set, transporter beam needs primary object creating
# .42..... key to unlock
#              &00 : OBJECT_YELLOW_WHITE_RED_KEY
#              &20 : (key 4, not collectable)
#              &40 : OBJECT_RED_MAGENTA_RED_KEY
#              &60 : OBJECT_BLUE_CYAN_GREEN_KEY
# ...1842. transporter destination
# .......1 if set, transporter is inactive
#
# objects_state is used for y fraction

; transporter_beams_palette_table
&4d82 52 ; rmM
&4d83 63 ; rcC
&4d84 35 ; gyB
&4d85 21 ; rgG

; update_transporter_beam                                           # Called with A = X = this_object_data, Y = object touching
&4d86 4a       LSR A                                                # 84218421 -> .8421842 1
&4d87 29 0f    AND #&0f                                             #          -> ....1842 1
&4d89 aa       TAX ; destination
&4d8a a9 b0    LDA #&b0 ; y fraction
&4d8c b0 2f    BCS &4dbd ; is_stationary                            # Lowest bit of data set if transporter is stationary
; is_moving
&4d8e 98       TYA
&4d8f 30 1c    BMI &4dad ; not_touching_other_object
; is_touching_other_object
&4d91 b9 c6 08 LDA &08c6,Y ; objects_flags
&4d94 29 10    AND #&10 ; OBJECT_FLAG_TELEPORTING
&4d96 d0 15    BNE &4dad ; not_touching_object                      # If the touching object isn't already teleporting,
&4d98 bd 4a 31 LDA &314a,X ; transporter_destinations_x_table
&4d9b 99 16 09 STA &0916,Y ; objects_tx                             # Used as destination for teleporting objects
&4d9e bd 5a 31 LDA &315a,X ; transporter_destinations_y_table
&4da1 99 36 09 STA &0936,Y ; objects_ty                             # Used as destination for teleporting objects
&4da4 20 f0 0c JSR &0cf0 ; set_object_teleporting
&4da7 20 a9 0b JSR &0ba9 ; set_object_Y_velocities_from_this_object
&4daa 20 0d 44 JSR &440d ; play_sound_for_teleporting
; not_touching_other_object
&4dad a5 6f    LDA &6f ; this_object_flags
&4daf 29 04    AND #&04 ; OBJECT_FLAG_NEWLY_CREATED
&4db1 d0 15    BNE &4dc8 ; skip_updating_beam_position
&4db3 a5 11    LDA &11 ; this_object_state (beam y fraction)
&4db5 69 20    ADC #&20
&4db7 c9 b1    CMP #&b1
&4db9 90 02    BCC &4dbd ; skip_wraparound
&4dbb e9 b0    SBC #&b0
; skip_wraparound
; is_stationary
&4dbd 85 11    STA &11 ; this_object_state (beam y fraction)
&4dbf 24 39    BIT &39 ; this_object_y_flip                         # If transporter base in ceiling, beam moves up
&4dc1 20 4c 32 JSR &324c ; invert_if_positive                       # If transporter base in floor, beam moves down
&4dc4 85 51    STA &51 ; this_object_y_fraction
&4dc6 c6 51    DEC &51 ; this_object_y_fraction
; skip_updating_beam_position
&4dc8 20 c5 0b JSR &0bc5 ; check_if_object_hit_by_remote_control    # Returns carry clear if object hit by remote control
&4dcb b0 05    BCS &4dd2 ; not_hit_by_remote_control
&4dcd a9 00    LDA #&00                                             # &00 clear to use data bits for transporter beam
&4dcf 20 ac 31 JSR &31ac ; consider_toggling_lock
; not_hit_by_remote_control
; rotate_colour_from_frame_counter
&4dd2 a5 06    LDA &06 ; this_object_frame_counter
; rotate_colour_from_A
&4dd4 4a       LSR A
&4dd5 4a       LSR A
&4dd6 29 03    AND #&03
&4dd8 aa       TAX
&4dd9 bd 82 4d LDA &4d82,X ; transporter_beams_palette_table
&4ddc 85 73    STA &73 ; this_object_palette
; leave
&4dde 60       RTS

; use_damaged_palette_if_carry_clear
&4ddf a4 41    LDY &41 ; this_object_type
&4de1 b9 ef 02 LDA &02ef,Y ; object_types_palette_and_pickup_table
&4de4 29 7f    AND #&7f                                             # .4218421 palette
&4de6 b0 02    BCS &4dea ; set_palette
&4de8 49 30    EOR #&30                                             # Toggle colour 3 : KRGYBMCW -> YGRKWCMB
; set_palette
&4dea 85 73    STA &73 ; this_object_palette
&4dec 60       RTS

# Sucking nests (OBJECT_SUCKING_NEST)
# ===================================
# Tertiary data byte is as follows:
#
# 8....... if set, sucking nest needs primary object creating
# .4218421 sucking nest type
#              &00 : cyan/green,   triggered by all objects, attracts with power &50
#              &01 : blue/magenta, triggered by OBJECT_HORIZONTAL_STONE_DOOR, repels with power &30
#              &02 : cyan/magenta, triggered by OBJECT_WASP, attracts with power &7f
#              &03 : magenta/red,  triggered by triggered by OBJECT_CORONIUM_BOULDER, attracts with power &40
#              &04 : yellow/white, triggered by OBJECT_PIRANHA, attracts with power &50
#              &05 : yellow/green, triggered by all objects, repels with power &7f
#              &06 : red/magenta,  triggered by OBJECT_CORONIUM_BOULDER, repels with power &7f
#              &07 : yellow/cyan,  triggered by OBJECT_PIRANHA, repels with power &50
#              &08 : green/green,  triggered by OBJECT_WORM, attracts with power &40
#
# objects_state is used for activeness

; update_sucking_nest                                               # Called with X = this_object_data, Y = object touching
&4ded bd 49 4e LDA &4e49,X ; sucking_nests_palette_direction_table  # Top seven bits set palette
&4df0 4a       LSR A                                                # Clear top bit to use background plotting
&4df1 85 73    STA &73 ; this_object_palette
&4df3 a5 07    LDA &07 ; this_object_frame_counter_sixteen
&4df5 f0 14    BEQ &4e0b ; consider_sucking
&4df7 bd 37 4e LDA &4e37,X ; sucking_nests_trigger_table            # Negative if sucking nest is triggered by all objects
&4dfa 30 0d    BMI &4e09 ; set_state                                # If so, &80 is set, so sucking nest is always active
&4dfc a8       TAY
&4dfd c9 55    CMP #&55 ; OBJECT_CORONIUM_BOULDER
&4dff d0 02    BNE &4e03 ; not_coronium_boulder
&4e01 a0 0b    LDY #&0b ; OBJECT_YELLOW_SLIME
; not_coronium_boulder
&4e03 20 2a 3c JSR &3c2a ; find_object                              # Returns X = object, or negative if none
&4e06 8a       TXA
&4e07 49 ff    EOR #&ff                                             # Set &80 if object found, to make sucking nest active
; set_state
&4e09 85 11    STA &11 ; this_object_state (sucking nest active)
; consider_sucking
&4e0b a5 11    LDA &11 ; this_object_state (sucking nest active)    # &80 set if sucking nest is active
&4e0d 10 18    BPL &4e27 ; skip_accelerating_other_objects
&4e0f a6 bc    LDX &bc ; this_object_data
&4e11 bd 49 4e LDA &4e49,X ; sucking_nests_palette_direction_table  # Lowest bit set if attracting, clear if repelling
&4e14 4a       LSR A
&4e15 66 29    ROR &29 ; acceleration_sign                          # &80 set if accelerating towards nest
&4e17 bd 40 4e LDA &4e40,X ; sucking_nests_power_table
&4e1a 85 35    STA &35 ; acceleration_power
&4e1c 20 3a 34 JSR &343a ; accelerate_all_objects                   # Accelerate all objects within line of sight,
;                                                                   # depending on weight and distance from sucking nest
&4e1f a5 db    LDA &db ; rnd_state + 2
&4e21 85 37    STA &37 ; this_object_x_flip                         # Flip sucking nest horizontally at random
&4e23 c9 50    CMP #&50                                             # 1 in 256 chance of sucking nest causing 80 damage
&4e25 f0 02    BEQ &4e29 ; consider_damaging_target
; skip_accelerating_other_objects
&4e27 a9 02    LDA #&02                                             # Otherwise, sucking nest causes 2 damage
; consider_damaging_target
&4e29 a4 3b    LDY &3b ; this_object_touching                       # Negative if not touching any object
&4e2b 30 b1    BMI &4dde ; leave
&4e2d 20 fa 13 JSR &13fa ; play_sound
&4e30 57 07 57 97                                                   # Play sound for sucking nest touching object
&4e34 4c a6 24 JMP &24a6 ; damage_object                            # Sucking nest causes damage depending on chance

; sucking_nests_trigger_table
&4e37 ff ; type &00 : all objects
&4e38 3e ; type &01 : OBJECT_HORIZONTAL_STONE_DOOR
&4e39 11 ; type &02 : OBJECT_WASP
&4e3a 55 ; type &03 : OBJECT_CORONIUM_BOULDER (and OBJECT_YELLOW_SLIME)
&4e3b 10 ; type &04 : OBJECT_PIRANHA
&4e3c ff ; type &05 : all objects
&4e3d 55 ; type &06 : OBJECT_CORONIUM_BOULDER (and OBJECT_YELLOW_SLIME)
&4e3e 10 ; type &07 : OBJECT_PIRANHA
&4e3f 0f ; type &08 : OBJECT_WORM

; sucking_nests_power_table
&4e40 50 ; type &00 :  10 tiles
&4e41 30 ; type &01 :   6 tiles
&4e42 7f ; type &02 : ~16 tiles
&4e43 40 ; type &03 :   8 tiles
&4e44 50 ; type &04 :  10 tiles
&4e45 7f ; type &05 : ~16 tiles
&4e46 7f ; type &06 : ~16 tiles
&4e47 50 ; type &07 :  10 tiles
&4e48 40 ; type &08 :   8 tiles

; sucking_nests_palette_direction_table                             # 8421842. palette
&4e49 5f ; type &00 : &2f (cwG), attracts                           # .......1 if set attract, if clear repel
&4e4a ac ; type &01 : &56 (bcM). repels
&4e4b bf ; type &02 : &5f (cwM). attracts
&4e4c 3d ; type &03 : &1e (mwR). attracts
&4e4d f9 ; type &04 : &7c (ywW). attracts
&4e4e 58 ; type &05 : &2c (ywG). repels
&4e4f a2 ; type &06 : &51 (rgM). repels
&4e50 d8 ; type &07 : &6c (ywC). repels
&4e51 4b ; type &08 : &25 (gyG). attracts

# Maggots (OBJECT_MAGGOT)
# =======================
# Spawned from earth tiles by random events, and created by maggot machine.
#
# objects_state is used for NPC behaviour and walking
# objects_timer is used for jump cooldown

; update_maggot
&4e52 a5 15    LDA &15 ; this_object_energy
&4e54 29 7f    AND #&7f
&4e56 85 15    STA &15 ; this_object_energy
&4e58 a9 82    LDA #&82 ; OBJECT_CREW_MEMBER | &80                  # Set &80 to target player too
&4e5a a0 2f    LDY #&2f ; OBJECT_WHITE_YELLOW_BIRD
&4e5c a2 14    LDX #&14                                             # Maggots cause 20 damage
; update_worm_or_maggot
&4e5e 8e 86 4e STX &4e86 ; worm_or_maggot_damage
&4e61 20 f8 3b JSR &3bf8 ; consider_finding_target
&4e64 20 26 3d JSR &3d26 ; consider_updating_npc_path
&4e67 20 02 2a JSR &2a02 ; consider_npc_burrowing                   # Returns negative if NPC removed or burrowing
&4e6a 30 57    BMI &4ec3 ; animate_worm_or_maggot
&4e6c 20 87 25 JSR &2587 ; rnd
&4e6f 24 1f    BIT &1f ; this_object_in_water                       # Positive if object is in any water
&4e71 30 02    BMI &4e75 ; not_underwater
; is_underwater
&4e73 a9 ff    LDA #&ff                                             # Worms and maggots always want to borrow underwater
; not_underwater
&4e75 06 11    ASL &11 ; this_object_state (behaviour and walking)
&4e77 c9 f6    CMP #&f6                                             # Otherwise, 10 in 256 chance of wanting to burrow
&4e79 66 11    ROR &11 ; this_object_state (behaviour and walking)  # Set top bit if worm or maggot wants to burrow
&4e7b a4 3b    LDY &3b ; this_object_touching
&4e7d c4 0e    CPY &0e ; this_object_target_object
&4e7f d0 09    BNE &4e8a ; not_touching_target
&4e81 a9 0a    LDA #&0a
&4e83 85 12    STA &12 ; this_object_timer (npc jump cooldown)
&4e85 a9 14    LDA #&14
#     actually LDA worm_or_maggot_damage
&4e87 20 a6 24 JSR &24a6 ; damage_object                            # Maggot or worm damage depends on object type
; not_touching_target
&4e8a a6 20    LDX &20 ; this_object_waterline
&4e8c ca       DEX                                                  # &00 becomes &ff if entirely above water
;                                                                   # &ff becomes &fe is entirely under water
&4e8d 30 05    BMI &4e94 ; not_at_waterline                         # Positive if maggot is close to waterline
&4e8f a6 c3    LDX &c3 ; every_sixteen_frames                       # Negative every sixteen frames
&4e91 8e e5 29 STX &29e5 ; this_object_object_collision_y_flags     # Set &80 to simulate collision to bottom
;                                                                   # This makes the maggot or worm jump off the waterline
; not_at_waterline
&4e94 a9 10    LDA #&10 ; speed
&4e96 24 3e    BIT &3e ; this_object_target_object_and_flags        # &80 set if TARGET_FLAG_DIRECTNESS_TWO or TARGET_FLAG_DIRECTNESS_THREE
&4e98 10 1e    BPL &4eb8 ; not_seen_target
; has_seen_target
&4e9a 0a       ASL A                                                # Move twice as fast if can see or has seen target
&4e9b 48       PHA ; speed
&4e9c 20 59 35 JSR &3559 ; get_object_distance_from_screen_centre
&4e9f c9 0f    CMP #&0f
&4ea1 b0 14    BCS &4eb7 ; skip_sound                               # Don't play sound if far away
&4ea3 49 0f    EOR #&0f
&4ea5 c5 db    CMP &db ; rnd_state + 2                              # More likely to play sound if nearer
&4ea7 90 0e    BCC &4eb7 ; skip_sound                               # (1 in 16 chance at middle of screen)
&4ea9 20 fa 13 JSR &13fa ; play_sound
&4eac 33 f3 09 b4                                                   # Play sound for worm or maggot squealing
&4eb0 20 fa 13 JSR &13fa ; play_sound
&4eb3 33 f3 07 b5                                                   # Play sound for worm or maggot squealing
; skip_sound_and_pull
&4eb7 68       PLA ; speed
; not_seen_target
&4eb8 a2 06    LDX #&06 ; npc walking type
&4eba 20 df 3a JSR &3adf ; update_walking_npc_and_check_for_obstacles_with_speed_A # Returns carry clear if jumping
&4ebd b0 04    BCS &4ec3 ; animate_worm_or_maggot
&4ebf a9 06    LDA #&06
&4ec1 85 12    STA &12 ; this_object_timer (npc jump cooldown)
; animate_worm_or_maggot
&4ec3 20 78 25 JSR &2578 ; consider_flipping_object_to_match_velocity_x
&4ec6 a5 45    LDA &45 ; this_object_velocity_y
&4ec8 e9 04    SBC #&04
&4eca 85 39    STA &39 ; this_object_y_flip                         # Flip vertically if y velocity < 4
&4ecc a5 06    LDA &06 ; this_object_frame_counter
&4ece 29 04    AND #&04
&4ed0 4a       LSR A                                                # Set &02 4 frames in 8 to make worm or maggot wriggle
&4ed1 05 12    ORA &12 ; this_object_timer (npc jump cooldown)
&4ed3 85 12    STA &12 ; this_object_timer (npc jump cooldown)
&4ed5 4c dc 44 JMP &44dc ; animate_sprite_from_timer                # Use SPRITE_WORM_ONE if not jumping or wriggling,
;                                                                   # SPRITE_WORM_TWO or SPRITE_WORM_THREE otherwise

# Green/white turrets (OBJECT_GREEN_WHITE_TURRET)
# Cyan/red turrets (OBJECT_CYAN_RED_TURRET)
# ===============================================
# Tertiary data byte is as follows:
#
# 8....... if set, turret needs primary object creating
# .421842. projectile type
# .......1 if set, turret is inactive
#
# Magenta rolling robot (OBJECT_MAGENTA_ROLLING_ROBOT)
# Red rolling robot (OBJECT_RED_ROLLING_ROBOT)
# Blue rolling robot (OBJECT_BLUE_ROLLING_ROBOT)
# ====================================================

; update_turret                                                     # Called with A = this_object_data
&4ed8 4a       LSR A                                                # .......1 set if turret is inactive
&4ed9 b0 35    BCS &4f10 ; set_turret_or_robot_energy
; is_active
&4edb aa       TAX                                                  # .421842. projectile type
&4edc d0 1b    BNE &4ef9 ; consider_firing                          # Always branches
; update_rolling_robot
&4ede 24 15    BIT &15 ; this_object_energy
&4ee0 10 2e    BPL &4f10 ; set_turret_or_robot_energy               # Magenta and red rolling robots don't move if energy < 128
; update_blue_rolling_robot
&4ee2 a2 05    LDX #&05 ; npc stimuli type
&4ee4 20 c9 27 JSR &27c9 ; check_for_npc_stimuli
&4ee7 20 26 3d JSR &3d26 ; consider_updating_npc_path
&4eea a2 04    LDX #&04 ; npc walking type
&4eec a9 18    LDA #&18 ; speed
&4eee 20 df 3a JSR &3adf ; update_walking_npc_and_check_for_obstacles_with_speed_A
&4ef1 20 78 25 JSR &2578 ; consider_flipping_object_to_match_velocity_x
&4ef4 a4 41    LDY &41 ; this_object_type
&4ef6 be 02 4f LDX &4f02,Y ; rolling_robots_bullet_table - &1c (OBJECT_MAGENTA_ROLLING_ROBOT)
; consider_firing
&4ef9 24 15    BIT &15 ; this_object_energy
&4efb 10 13    BPL &4f10 ; set_turret_or_robot_energy               # Rolling robots and turrets don't fire if energy < 128
&4efd a0 84    LDY #&84 ; OBJECT_RANGE_PROJECTILES
&4eff a5 55    LDA &55 ; this_object_y
&4f01 c9 b4    CMP #&b4                                             # Is this the turret at the top of Triax's lab?
&4f03 b0 06    BCS &4f0b ; not_triax_lab_turret
&4f05 24 db    BIT &db ; rnd_state + 2
&4f07 70 02    BVS &4f0b ; not_triax_lab_turret
&4f09 a0 86    LDY #&86 ; OBJECT_RANGE_FLYING_ENEMIES
; not_triax_lab_turret
&4f0b a9 81    LDA #&81 ; OBJECT_ACTIVE_CHATTER | &80               # &80 set to target player too
&4f0d 20 68 27 JSR &2768 ; find_a_target_and_fire_at_it
; set_turret_or_robot_energy
&4f10 a6 41    LDX &41 ; this_object_type
&4f12 bc fc 4e LDY &4efc,X ; rolling_robots_minimum_energy_table
&4f15 4c 3a 35 JMP &353a ; gain_energy_Y_and_flash_if_damaged       # Returns carry clear if energy < &80

; rolling_robots_minimum_energy_table
&4f18 14 ; &1c (OBJECT_MAGENTA_ROLLING_ROBOT)                       # Magenta rolling robots have a minimum energy of 20
&4f19 46 ; &1d (OBJECT_RED_ROLLING_ROBOT)                           # Red rolling robots have a minimum energy of 70
&4f1a 46 ; &1e (OBJECT_BLUE_ROLLING_ROBOT)                          # Blue rolling robots have a minimum energy of 70
&4f1b 14 : &1f (OBJECT_GREEN_WHITE_TURRET)                          # Green/white turrets have a minimum energy of 20
&4f1c 7f : &1f (OBJECT_CYAN_RED_TURRET)                             # Cyan/red turrets have a minimum energy of 127
&4f1d 14 : &1f (OBJECT_HOVERING_ROBOT)                              # Hovering robots have a minimum energy of 20

; rolling_robots_bullet_table
&4f1e 18 ; &1c (OBJECT_MAGENTA_ROLLING_ROBOT) : OBJECT_PISTOL_BULLET
&4f1f 13 ; &1d (OBJECT_RED_ROLLING_ROBOT)     : OBJECT_ICER_BULLET
&4f20 14 ; &1e (OBJECT_BLUE_ROLLING_ROBOT)    : OBJECT_TRACER_BULLET

# Piranhas (OBJECT_PIRANHA)
# Wasps (OBJECT_WASP)
# =========================
# objects_state is used for aggressiveness
# objects_timer is used for sprite offset

; update_piranha_or_wasp
&4f21 a9 05    LDA #&05 ; OBJECT_LARGE_HIVE                         # Wasps use large hives as default target
&4f23 a4 41    LDY &41 ; this_object_type
&4f25 c0 11    CPY #&11 ; OBJECT_WASP                               # Set carry if wasp, clear carry if piranha
&4f27 66 39    ROR &39 ; this_object_y_flip                         # Negative if wasp, positive if piranha
&4f29 30 06    BMI &4f31 ; is_wasp
; is_piranha
&4f2b a9 04    LDA #&04                                             # Piranhas sink
&4f2d 85 42    STA &42 ; this_object_acceleration_y
&4f2f a9 04    LDA #&04 ; OBJECT_SMALL_HIVE                         # Piranhas target small hives as home
; is_wasp
&4f31 c6 42    DEC &42 ; this_object_acceleration_y                 # No gravity for wasps
&4f33 24 db    BIT &db ; rnd_state + 2
&4f35 70 0b    BVS &4f42 ; skip_finding_target                      # 1 in 2 chance of considering finding target every sixteen frames
&4f37 a6 11    LDX &11 ; this_object_state (wasp or piranha aggressiveness)
&4f39 e4 da    CPX &da ; rnd_state + 1                              # More aggressive spawn more likely to target player than hive
&4f3b b0 02    BCS &4f3f ; find_target_for_piranha_or_wasp
; target_player
&4f3d a9 00    LDA #&00 ; OBJECT_PLAYER
; find_target_for_piranha_or_wasp
&4f3f 20 f8 3b JSR &3bf8 ; consider_finding_target
; skip_finding_target
&4f42 20 26 3d JSR &3d26 ; consider_updating_npc_path
&4f45 20 87 25 JSR &2587 ; rnd
&4f48 f0 0d    BEQ &4f57 ; play_sound_for_piranha_or_wasp           # 1 in 256 chance of playing sound when not damaging
&4f4a c5 11    CMP &11 ; this_object_state (wasp or piranha aggressiveness)
&4f4c 90 10    BCC &4f5e ; skip_damaging_player                     # More aggressive spawn more likely to damage player
&4f4e a4 3b    LDY &3b ; this_object_touching
&4f50 d0 0c    BNE &4f5e ; skip_damaging_player
&4f52 a9 18    LDA #&18
&4f54 20 a6 24 JSR &24a6 ; damage_object                            # Piranhas and wasps cause 24 damage
; play_sound_for_piranha_or_wasp                                    # Always play sound if player was damaged
&4f57 20 fa 13 JSR &13fa ; play_sound
&4f5a 33 f3 4f 35                                                   # Play sound for piranha or wasp
; skip_damaging_player
&4f5e a9 0c    LDA #&0c ; modulus
&4f60 20 55 25 JSR &2555 ; update_sprite_offset_using_velocities    # Returns A = sprite offset
&4f63 4a       LSR A                                                # ....8421 -> .....842
&4f64 4a       LSR A                                                #          -> ......84, i.e. &00 - &0b -> &00 - &02
&4f65 20 92 32 JSR &3292 ; change_object_sprite_to_base_plus_A      # Use SPRITE_PIRANHA_ONE to SPRITE_PIRANHA_THREE
;                                                                   # or SPRITE_WASP_ONE to SPRITE_WASP_THREE
&4f68 20 7e 25 JSR &257e ; flip_object_to_match_velocity_x
&4f6b 24 1b    BIT &1b ; this_object_tile_top_or_bottom_collision   # &80 set if object hit tiles above or below
&4f6d 30 06    BMI &4f75 ; move_wasp_or_piranha
&4f6f a5 39    LDA &39 ; this_object_y_flip                         # &80 is set if wasp, clear if piranha
&4f71 45 1f    EOR &1f ; this_object_in_water                       # &80 set if not in any water
&4f73 30 26    BMI &4f9b ; leave                                    # Piranhas and wasps don't move if out of element
; move_wasp_or_piranha
&4f75 a9 30    LDA #&30 ; magnitude
&4f77 a0 18    LDY #&18 ; maximum acceleration
&4f79 a2 28    LDX #&28 ; 5 in 32 chance
&4f7b 20 da 31 JSR &31da ; move_towards_target_with_probability_X
&4f7e 24 c4    BIT &c4 ; every_eight_frames
&4f80 10 19    BPL &4f9b ; leave                                    # Every eight frames,
&4f82 20 87 25 JSR &2587 ; rnd
&4f85 29 02    AND #&02                                             # 0 for x, 2 for y
&4f87 aa       TAX
&4f88 a5 db    LDA &db ; rnd_state + 2
&4f8a 29 1f    AND #&1f                                             # add between -&10 to &0f to either x or y acceleration
&4f8c e9 10    SBC #&10
&4f8e 75 40    ADC &40,X ; this_object_acceleration_x
&4f90 95 40    STA &40,X ; this_object_acceleration_x
&4f92 a5 15    LDA &15 ; this_object_energy
&4f94 c9 0a    CMP #&0a
&4f96 b0 03    BCS &4f9b ; leave
&4f98 4c 4e 25 JMP &254e ; increase_energy_by_one_if_not_zero
; leave
&4f9b 60       RTS

# Explosions (OBJECT_EXPLOSION)
# =============================
# objects_tertiary_data_offset is used for explosion duration

; update_explosion
&4f9c a9 80    LDA #&80 ; TILE_PROCESSING_FLAG_PLOTTING             # Create primary objects for any tertiary objects
&4f9e 85 2d    STA &2d ; tile_processing_mode                       # that are present in tiles surrounding explosion
&4fa0 a6 53    LDX &53 ; this_object_x
&4fa2 ca       DEX
&4fa3 86 95    STX &95 ; tile_x
; check_for_tertiary_objects_around_explosion_y_loop                # Loop through x - 1, x and x + 1
&4fa5 a6 55    LDX &55 ; this_object_y
&4fa7 ca       DEX
&4fa8 86 97    STX &97 ; tile_y
; check_for_tertiary_objects_around_explosion_x_loop                # Loop through y - 1, y and y + 1
&4faa 20 15 17 JSR &1715 ; get_tile_and_check_for_tertiary_objects
&4fad a6 55    LDX &55 ; this_object_y
&4faf e4 97    CPX &97 ; tile_y
&4fb1 e6 97    INC &97 ; tile_y
&4fb3 b0 f5    BCS &4faa ; check_for_tertiary_objects_around_explosion_y_loop
&4fb5 a6 53    LDX &53 ; this_object_x
&4fb7 e4 95    CPX &95 ; tile_x
&4fb9 e6 95    INC &95 ; tile_x
&4fbb b0 e8    BCS &4fa5 ; check_for_tertiary_objects_around_explosion_x_loop
&4fbd a5 da    LDA &da ; rnd_state + 1                              # Use random palette for explosion
&4fbf 29 13    AND #&13 ; &00 &01 &02 &03 &10 &11 &12 &13           # &00 &01 &02 &03 &10 &11 &12 &13
&4fc1 85 73    STA &73 ; this_object_palette                        # kyK rgK rmK rcK kyR rgR rmR rcR
&4fc3 a9 0a    LDA #&0a ; 10
&4fc5 a0 16    LDY #&16 ; PARTICLE_EXPLOSION
&4fc7 20 8e 21 JSR &218e ; add_particles                            # Add ten explosion particles
&4fca a5 3d    LDA &3d ; this_object_tertiary_data_offset (explosion duration)
&4fcc f0 17    BEQ &4fe5 ; to_set_object_for_removal
&4fce c6 3d    DEC &3d ; this_object_tertiary_data_offset (explosion duration)
&4fd0 a5 dc    LDA &dc ; rnd_state + 3
&4fd2 29 07    AND #&07
&4fd4 c5 3d    CMP &3d ; this_object_tertiary_data_offset (explosion duration)
&4fd6 b0 c3    BCS &4f9b ; leave
&4fd8 a5 3d    LDA &3d ; this_object_tertiary_data_offset (explosion duration)
&4fda c9 08    CMP #&08                                             # At the start of a long explosion,
&4fdc 66 28    ROR &28 ; acceleration_damages_targets               # Set &80 to damage targets accelerated too much
&4fde 0a       ASL A                                                # Explosion duration sets distance
&4fdf 0a       ASL A                                                # 1 duration = 0.25 tiles
&4fe0 85 35    STA &35 ; acceleration_power                         # Explosion acceleration reduces with time
&4fe2 4c 3a 34 JMP &343a ; accelerate_all_objects                   # Accelerate all other objects away from explosion
; to_set_object_for_removal
&4fe5 4c 29 25 JMP &2529 ; set_object_for_removal

; unused
&4fe8 f3 97 52 4a

; map_data
&4fec 95 b6 19 ef 6f 6e 70 5e d4 a9 a9 57 6d 06 6e ed
&4ffc 2d 6e 6e 06 ca 70 ad 07 5e 5e 53 62 53 9b 35 9e
&500c 15 16 e9 22 57 97 0c cc 8c 78 3f bd 05 ed e2 0a
&501c f0 05 2d 6e d3 07 e4 24 63 a1 a5 64 53 07 a4 63
&502c 66 7e 3e dc 8c 72 e8 bc 06 19 22 6d de d3 19 71
&503c f1 7e 29 f4 39 a9 d3 06 53 a1 e4 07 d4 a9 d3 1a
&504c c1 77 d7 41 6f a1 6d 53 f5 d3 21 19 a1 53 06 e5
&505c ee 19 97 d3 13 ea 75 02 d3 9b 53 ea 5f 85 72 21
&506c 6e 2c 2d 07 ad ed b1 25 19 2f 53 3b 9e e2 d3 62
&507c 02 f0 2d 06 a4 d3 19 21 53 21 ed 30 d3 6a 59 a4
&508c 6d 70 6f 04 a4 64 a2 a2 1e 04 d3 01 4a 3b 64 63
&509c f0 2d 17 ed f4 2f 12 30 d3 21 fa a2 a1 e2 8d 2e
&50ac 64 6e 02 ee 04 05 13 ee 4a 6a 2d 05 9b 2d 25 65
&50bc ed fe 31 6f f0 14 ee bf df 8d d3 6a de 53 8b 1e
&50cc ee ad 70 7a 24 a1 22 6d 22 d3 21 93 df 01 02 dc
&50dc ae 7c 06 af df b2 07 29 03 5e cd ea 53 cd 07 8f
&50ec fc 94 66 69 30 07 62 35 d6 9d bf 2f 9d 62 62 1f
&50fc 53 21 d3 43 fe 45 93 74 9e f0 91 ae a1 62 02 07
&510c 6a cc d9 3d e2 ed ed b0 b4 15 e6 19 57 17 9d 4c
&511c ed a2 93 65 03 21 9e 05 b4 b0 06 ee 5e a1 5e 25
&512c 49 f9 07 7c de de ea 07 67 04 bd 68 53 cc 26 a8
&513c 7a 21 de e2 9e 06 53 a1 1e e2 04 e8 9e 04 64 06
&514c b9 06 da 13 e3 4a 21 f8 05 c2 32 97 07 62 ed 70
&515c ef ea d3 6a e4 19 c6 f3 03 19 a8 1e 28 9e f5 29
&516c 07 04 70 21 1e 1e 06 fa ee 2c 2d f0 13 53 bb f0
&517c 56 21 ed a1 aa c0 c4 53 62 ef 2f f0 70 5e a1 19
&518c 6f de 1e a1 24 02 5f 62 6d 06 71 8d 13 71 b0 af
&519c 56 ea de a5 21 e5 4b 8d 03 2f 29 2d 57 38 6e 07
&51ac d3 19 2a e3 b5 6e 49 e5 70 62 b0 12 53 d3 22 6d
&51bc df 8d 53 a1 d4 df 21 1e 2d f0 22 70 6e 35 12 e2
&51cc 9a 23 a1 61 68 05 a5 d3 04 2e 06 19 07 d3 e1 2e
&51dc 24 9b 53 cd 07 cd ca 0f 52 ed e2 2e 05 34 78 04
&51ec 3a 7b 04 ad 53 e1 b1 07 df 21 13 fa 7e 19 5e 7b
&51fc 05 96 3f bd 54 dd 19 b1 32 bc 69 2b 21 6f ee 19
&520c b8 b2 2d 2d 64 20 53 03 53 a1 3e fe d3 07 53 fb
&521c a8 b7 29 2b e8 bc 68 dd 19 39 a2 b1 f0 53 1e ad
&522c 70 3b 03 d6 53 1e 7a a5 07 1b 53 de 1e 9e d3 21
&523c d6 19 68 fd 02 6a 34 66 b0 9e 04 ef 04 de ed f1
&524c ed 18 a4 69 17 53 53 e2 ed 30 de ea 9e 19 19 47
&525c ef 06 8c 72 ef 19 2f f0 ed b9 99 b1 de 23 a3 78
&526c a2 2f 30 ef 04 b5 e4 a1 d3 19 7a a1 cd da 1b d3
&527c 6d 06 ed 71 b1 6e 04 6d ee af 4a d1 5e 1e 53 7c
&528c ef 18 f0 2d 02 b8 7f 62 7c 8d 12 de c6 e4 8b ae
&529c 0d ed ad 8d e2 df b1 6e e4 04 64 07 25 f0 e4 19
&52ac 2e ef 19 b0 4f 32 75 07 e4 8d 5f 21 d4 cd cb 53
&52bc 8f ae af ed 4a 21 a1 79 23 ea 07 13 54 f5 62 24
&52cc 6f 4a ee 11 13 93 1e a9 25 5f a1 7a 24 a5 5e 0f
&52dc a4 1e ee 19 a0 53 e1 a1 93 93 d3 e1 32 97 93 53
&52ec d3 19 21 1e f9 19 a5 03 6b 21 ae 12 7c 6a fa 2d
&52fc 38 72 bf b0 21 ef 11 b5 56 36 02 3d 68 01 8c 30
&530c d3 21 64 7e 64 a1 7c 21 54 fe f2 6e e4 29 5f 04
&531c 19 19 2a af 2d e2 6a 6f a7 69 f7 e9 32 a8 fc 28
&532c fa 07 d3 6a 64 32 53 05 4a 62 04 56 d3 6a 54 a4
&533c 6d 53 e2 ed 69 14 1e ef 37 00 40 28 d3 05 2d 74
&534c ed 05 ef 5e 53 e3 19 a5 30 05 17 a1 a8 5f 21 05
&535c 22 ed e2 b1 62 02 64 65 6d 2c 12 cc 6d e2 04 d3
&536c 53 a1 19 ab a2 cd 8b 13 01 af 21 ed 51 94 f5 29
&537c 39 2e f0 1a 5e 02 1e 7a ad ed 39 70 b1 ee 03 b4
&538c d6 8d 53 21 c2 de 8b 2d ed 19 2f 2f 01 af ff 3f
&539c 53 00 ed 25 06 ef 24 e2 2d ed ed de 5e 7b 31 07
&53ac 13 cd d3 1b d4 cd 07 06 a2 6f a2 31 f0 06 f8 62
&53bc a1 53 aa 00 64 05 00 25 0f 6d 53 ed d3 19 13 93
&53cc 22 d3 22 e1 05 64 65 2d 70 19 62 06 22 63 63 bb
&53dc 64 63 53 04 22 72 63 7e 64 63 64 05 22 65 5b a1

; sprite_data                                                       # 128 x 81 pixel sprite sheet, 32 bytes per row
&53ec c0 00 00 00 32 11 80 10 00 00 00 20 06 08 00 00
&53fc 01 8c 00 66 80 00 00 00 01 02 08 64 90 80 00 66
&540c 8c 00 00 00 56 a3 c0 ca 00 00 00 07 2d 66 00 00
&541c 00 00 00 60 c0 00 00 00 01 0b 88 42 f0 3e 64 4c
&542c 3c 00 00 00 03 01 19 68 00 00 13 21 2d 00 00 00
&543c 0b 8d 00 40 68 00 00 00 02 ab 00 cb f8 18 90 4c
&544c df 00 00 00 46 23 00 1c 00 00 37 07 06 00 00 00
&545c ca 35 04 42 fc 80 00 00 13 19 0a e8 74 99 83 1e
&546c 3c c0 00 00 46 23 00 0a 00 00 1f 21 00 00 00 00
&547c cb bd c5 63 4f c0 00 00 15 9f 0b e2 33 00 87 78
&548c ff 0c 00 00 07 83 59 0e 00 01 3f 07 00 00 03 0f
&549c 4b ad f5 7b 6f e0 00 00 47 ff 26 c0 65 01 96 c0
&54ac c7 f4 00 00 04 02 11 08 00 03 1f 07 00 00 0f 0f
&54bc 4a 25 b5 db ff ac 00 00 4f ff 4e 80 61 0b 1e 00
&54cc ff ef 00 00 06 13 11 5c 00 21 3f 21 00 01 0f 0f
&54dc 4b ad a5 4b 7a 9e 80 00 37 fb ce 80 f8 01 96 e0
&54ec 79 79 c0 00 06 13 11 1e 00 07 1f 21 00 03 1f 2f
&54fc 4b ad 85 43 6f 7c c0 00 13 f9 8c 40 e0 00 87 3c
&550c 2f 3d cc 00 26 03 00 8c 00 21 3f 07 00 03 0e 0d
&551c 4a 25 04 42 df 4f ca 00 13 f0 8c 10 80 11 87 1e
&552c ff ff 3c 00 04 03 28 24 00 07 3f 07 00 07 0f 0f
&553c 0b 8d 00 40 bd ef 9e 00 0d f0 8f 00 0c 10 80 84
&554c 96 c7 f7 00 04 06 68 30 00 21 1f 21 00 06 0b 0d
&555c 00 00 00 40 9f 3e 5e 80 15 f1 ae 00 cc 32 32 00
&556c ff ff de 80 40 40 40 10 00 07 3f 21 00 11 44 22
&557c 03 0e 00 42 ff a7 fc 84 37 eb cc 01 0e 00 12 08
&558c b6 1e 8f 68 60 60 00 03 00 21 3f 07 01 0f 0f 0f
&559c 0e 0b 08 41 a7 bf 9f 68 6f ef 0f 11 ca 00 25 0c
&55ac ff ff ff 8e 00 00 00 ca 00 07 1f 07 01 0f 0f 0f
&55bc 88 88 88 62 6f 9f 3f f8 07 bf 0d 03 2d 00 48 44
&55cc 69 c7 78 be 00 11 91 06 00 21 3f 07 00 80 00 90
&55dc 00 12 c0 71 cf ff ef fe 11 9b 88 33 ed 00 4b 0e
&55ec 6d 6f 3d 8f 00 23 c0 0c 00 21 3f 21 30 e8 30 b9
&55fc 0c 25 e0 50 6f 8f d6 af 01 8a 08 07 0f 08 00 00
&560c ff ff ff ff 00 01 19 1a 11 07 1f 21 73 fc 73 bb
&561c ce 25 c0 66 ed af cf cb 00 0b 8c ff 8f 1d ff ff
&562c 3c 9e c7 3c 00 23 11 2a 13 07 3f 07 74 f2 74 b0
&563c ce 00 31 06 cf ff 9f df 01 09 00 07 0f 08 dc e0
&564c ff ff ff ff 00 03 59 08 04 21 3f 07 64 b2 64 90
&565c ed ff e2 ff 7f bf df 7f 22 00 88 33 6f 11 dd ff
&566c e3 3d 0f e3 00 01 11 4c 17 21 1f 07 64 32 32 07
&567c ed ff 80 06 7b 3e 7f 1f 74 11 c0 03 07 00 dc e0
&568c ff ff ff ff 00 02 11 7e 0c 07 3f 07 64 77 b0 27
&569c eb ff ee ff 2f e7 3f bf 40 10 00 11 46 11 dd ff
&56ac 3c 8f 79 2d 00 07 00 3c 97 07 3f 21 64 72 80 05
&56bc eb 00 33 06 7f 3f 7e ef 60 10 80 01 0e 00 cc 00
&56cc ff ff ff ff 00 27 00 0c 0c 21 1f 21 fe 00 20 07
&56dc e7 25 c0 66 df ff cf 4f 70 10 c0 00 cc 00 11 cc
&56ec cb 6b d6 c7 08 0d 00 40 97 21 1f 07 f4 20 20 27
&56fc 6f 25 e0 05 9e cf c7 6d dc 33 40 11 00 00 d1 88
&570c 87 e7 1e de 18 81 00 60 0c 07 3f 07 00 32 64 05
&571c 69 00 00 05 8f 4f 5f ef 8e 23 08 c1 60 00 00 00
&572c ff ff ff ff 98 10 00 00 0c 07 3f 07 64 32 32 06
&573c 6f 88 40 20 df fe ff b7 0d 03 04 61 c0 8e 30 80
&574c a7 1e c7 4b 90 10 80 00 97 07 1f 07 64 77 b0 00
&575c 6f 08 90 22 f7 3f ef 3f 8c 23 04 01 11 4a 07 0c
&576c ff ff ff ff 88 22 00 00 1f 21 1f 21 64 72 80 20
&577c 6f 98 b0 64 5f b7 2f ef 8f ab 08 11 11 ed f0 f7
&578c 9e c7 79 1e 08 74 00 88 0c 21 3f 21 e0 00 30 e2
&579c 6f 78 70 bc 5f af 7b 8f 8b ab 2e 9f 01 bd 00 00
&57ac ff ff ff ff 88 40 11 c0 84 07 3f 07 e8 20 73 ee
&57bc 6f 7c a0 06 cf ff 6f ed cc 22 3f 99 99 2f 73 ee
&57cc 7b 5a 8f c7 80 60 10 00 1f 07 1f 07 c0 3a 30 e0
&57dc 6f 3c 04 9c 8f 7d cf f7 37 11 11 ff 99 0f 30 c4
&57ec e3 0f 1e e7 08 70 10 80 00 11 ee 0f 01 3a 04 20
&57fc 6f d8 a0 0e ff c7 ef 5f 07 09 dd 99 88 9f 00 00
&580c ff ff ff ff 88 dc 10 c0 00 11 ee 0f 1b 3a 06 0e
&581c 6f 1c c0 04 d7 6f 7f 4f 07 0d 0c 9f 00 ee 73 ee
&582c 3c 9e e3 1e 0c 8e 33 40 00 01 0e 44 0a 3a 04 0e
&583c 6f bc 80 2a 1f ff fd cf 07 0d 08 00 00 5c 30 cc
&584c ff ff ff ff cc 0d 23 08 00 11 ee 0b 0b 3a 02 4e
&585c 6f e8 80 9f bf bd 9f ff 0e 81 08 00 00 2c 30 c4
&586c 4f 79 0f e3 c0 8d 03 04 00 05 0e 0f 1b 3a 06 0a
&587c 6f 5f 48 03 ef 0f 9f 7b 0c c5 08 00 00 6e 30 cc
&588c ff ff ff ff 8c 8e 23 00 00 04 00 0f 0a 2a 0e 0e
&589c 6f 1f 0c 06 7b 2f ff 2f 80 c1 00 00 00 2c 00 00
&58ac 3d bc 79 0f 88 8f ab 2e 00 1d ee 78 09 09 4e 4e
&58bc 6f ef 4e 40 1f ff eb 2f c4 10 00 00 00 5c 33 cc
&58cc 3c 8f 7d 3c c8 8b aa 2e 00 0d 0e 08 0c 03 0a 0a
&58dc 6f 8f df 60 ff bd 3f 6f c0 10 88 00 00 00 30 cc
&58ec ff ff ff ff cc 44 33 00 01 08 00 0f 0e 07 0e 0e
&58fc f0 f0 f0 c0 bd 1f 1f ff 00 10 80 00 00 00 30 cc
&590c e3 1e 8f e3 48 37 01 cc 01 5d ee 78 2f 27 4e 4e
&591c 6f ff ef ec 9f 7f df 3d 00 88 00 00 00 00 30 cc
&592c ff ff ff ff 88 07 01 0e 03 01 0e 08 0d 05 0a 0a
&593c 6f ff ef ec df de ff 8f 22 91 40 40 00 00 30 c4
&594c 0f c7 79 2d c4 03 01 0e 03 c4 01 0f 0f 07 0e 0e
&595c 6f ff ef de 7f 4f cb ef 20 54 40 41 00 00 30 cc
&596c ff ff ff ff ce cb 63 0e 06 00 03 f0 0a 02 0a 0a
&597c 6f ff ef de 6f 6f 8f bf 45 42 61 c3 ac 85 30 cc
&598c 6b 79 0f c7 4a c3 61 20 16 d5 8f 00 55 55 55 55
&599c 6f ff ef be ff a7 ff bd 61 00 a9 81 7f ce 30 cc
&59ac 7f ff ff ff cc 90 40 31 0e 01 0f 0f 0f 0f 0f 0f
&59bc 6f ff ef be 3d bf df 9f 20 b8 01 03 88 07 30 cc
&59cc cf c7 b5 3c c6 10 88 30 0f 0f 0f 0f 0f 0f 0f 0f
&59dc f0 f0 f0 7e 8f ef 4f ff 89 a4 99 00 cc ce 30 c4
&59ec f7 ff ff ce 00 10 80 00 0f 0f 0f 0f 0f 07 0e 0f
&59fc 00 30 00 02 f0 f2 f3 f4 a0 90 80 00 77 cd 30 cc
&5a0c b5 0f e3 68 00 00 00 10 0f 0f 0f 0f 0f 07 19 1e
&5a1c 00 31 00 55 60 f0 70 e0 51 40 11 22 0f 0f 30 cc
&5a2c 79 ad 7b 0c 00 00 00 10 0c 03 00 00 0e 02 47 69
&5a3c 00 20 11 20 40 a0 20 40 12 39 32 31 f0 6f 30 c4
&5a4c ff ff 8f c0 00 00 00 30 3c c3 f0 f0 19 55 1e 83
&5a5c 00 02 00 e0 00 2b 04 00 22 c6 31 3e 00 f6 30 cc
&5a6c 2f 6b df 88 00 00 00 21 0f 0f 0f 0f 47 0f 69 4f
&5a7c 00 06 01 51 00 22 09 44 10 50 33 3f 06 6f 30 cc
&5a8c ef 6b 5a 80 00 00 00 61 00 06 00 00 0f 1e 87 0f
&5a9c 00 0e c0 c0 11 7f 26 44 00 28 33 33 60 0f 30 cc
&5aac 7f ff cf 00 00 00 00 43 f0 96 f0 f0 0f 69 0f 0e
&5abc 01 0d ea 82 15 55 67 4c 10 0c 11 22 00 44 30 c4
&5acc 96 c7 78 00 00 00 00 43 0f 0f 0f 0f 1e 83 0f 1b
&5adc 67 0b 50 c0 33 df ef ee 00 08 00 00 0f cd 30 cc
&5aec ff ff ca 00 00 00 00 a5 00 0c 00 03 69 4f 0e 4d
&5afc 46 00 10 22 aa 9d aa aa 00 08 55 00 09 f0 30 cc
&5b0c 2d 3c e8 00 00 00 10 87 f0 3c f0 c3 87 0f 1b 07
&5b1c 4d 2e 10 80 ef 17 23 bf 65 11 60 00 69 49 30 cc
&5b2c ef 1e 0c 00 00 00 10 d7 0f 0f 0f 0f 0f 0e 4d 0c
&5b3c 8f 2e 30 44 46 0e 37 99 33 8a b0 88 69 6e 00 00
&5b4c 7b cf c0 00 00 00 30 7f 0f 0f 0f 0f 0f 1b 07 00
&5b5c 8f 00 20 00 0f 07 1b 06 55 9d 55 02 0f 08 70 ee
&5b6c ef 6f 88 00 00 00 30 7b 0a 0d 0a 0d 0e 4d 0c 00
&5b7c ce 00 00 44 0b 06 1f 0b 66 0a fa 27 09 01 00 ac
&5b8c 2d 5e 80 00 00 00 21 2f 55 22 55 22 1b 07 00 00
&5b9c 20 00 00 22 30 01 08 11 33 8c a0 fd 09 21 91 3d
&5bac f3 ef 00 00 00 00 53 a7 0f 0f 0f 0f 4d 0c 00 00
&5bbc e0 00 00 22 43 0b 0c 32 80 19 50 27 69 25 91 0f
&5bcc ef 3c 00 00 00 00 53 af 0f 0f 0f 0f 07 00 00 00
&5bdc c0 00 00 01 04 82 44 20 00 04 55 02 6f 3d 80 0e
&5bec b4 2e 00 00 00 00 a7 ef 05 0f 0f 0a 0c 00 00 00
&5bfc 80 00 00 88 06 03 00 30 00 24 24 24 6f 2c 91 8c
&5c0c ff 68 00 00 00 10 a5 7f 84 aa 55 12 08 10 4c 00
&5c1c e8 00 11 c0 07 0b 0c 30 80 24 ff 24 69 37 3a 0c
&5c2c 6b 4c 00 00 00 01 0f ff 84 00 00 12 08 32 2e 00
&5c3c 44 00 10 00 00 00 00 66 80 00 f6 24 09 33 47 07
&5c4c ff c0 00 00 00 10 6f 8f 85 0f 0f 1a 2e 03 2e 00
&5c5c 00 00 10 80 71 9a 0c 47 00 fb 64 99 09 22 47 09
&5c6c a5 08 00 00 00 30 7f df 87 0b 0d 1e 17 01 0c 00
&5c7c 00 00 10 c0 a7 12 28 06 19 fd 80 f6 0f 02 47 2e
&5c8c 5e 80 00 00 00 30 7b f5 83 49 29 1c 21 88 6e c1
&5c9c 00 00 33 40 aa 52 70 46 18 ff 88 64 0f 22 23 a6
&5cac 9f 00 00 00 00 53 3f 7f c9 6c 63 39 47 88 6a ac
&5cbc 08 00 27 00 ff 12 c0 47 00 fa 00 00 09 22 11 0c
&5ccc 78 00 00 00 00 c3 2f 4f 64 3f cf 62 df 03 1f ba
&5cdc 04 00 cf 09 af 9a d0 47 4c 66 00 00 69 22 00 0a
&5cec ce 00 00 00 00 d3 6f e7 33 04 02 cc 8e 02 1f 9f
&5cfc 82 00 8f 09 aa ce 60 45 4c 00 00 44 69 33 00 0e
&5d0c 68 00 00 00 00 97 7f 3f f0 0f 0f 0f 8f 04 17 99
&5d1c c8 00 4f 19 ff cf 08 22 00 00 00 e8 0f 08 01 0f
&5d2c 8c 00 00 00 10 0f ff ff c3 87 0f 0f 8f 1d 2e 8f
&5d3c ec 01 4e 1d af 8d 0c 13 88 00 df 80 f0 0f 0f f0
&5d4c 48 00 00 00 10 7a 6d bf f0 0f 0f 0f 65 23 e6 88
&5d5c ee cb 2e 2e aa ee 00 03 08 02 ff c0 00 1c 83 00
&5d6c 88 00 00 00 30 3f 4f af 77 ff ff ee 23 23 08 f0
&5d7c f0 c3 0c 26 77 bb cc c5 1d 85 4f e0 06 1d 8b 06
&5d8c 80 00 00 00 61 ff df ef 70 a5 0f 0e 03 19 0c ff
&5d9c ff 83 00 01 00 00 00 80 0c 03 4f 28 60 1d 8b 60
&5dac ff ff 00 04 52 cf ff 3f 70 c3 0f 0e 47 0c 04 00
&5dbc 00 44 08 01 70 91 08 e3 1d 87 4f 0c 00 1c 83 00
&5dcc ff ff 2d 2a d3 ef cf f7 70 2d 0f 0e 47 c4 0c 31
&5ddc 00 61 08 00 87 59 0c 61 18 87 46 1f 0f 0f 0f 0f
&5dec 00 00 22 11 97 2f 6f cf 70 87 0f 0e 23 80 0c 31
&5dfc 00 41 00 00 0f 1d cc 40 10 06 00 17 f0 80 10 f0 

# Sprites
# =======
# sprite                                            width     height    size    sheet x   sheet y   position  flip
# &00 SPRITE_SPACESUIT_HORIZONTAL                 : &5e0c &c0 &5e89 &40 13 x  9 &5f06 &36 &5f83 &42 ( 99, 72)     
# &01 SPRITE_SPACESUIT_FORTY_FIVE_HEAD_UP         : &5e0d &a0 &5e8a &80 11 x 17 &5f07 &44 &5f84 &02 ( 68, 64)     
# &02 SPRITE_SPACESUIT_JUMPING                    : &5e0e &50 &5e8b &98  6 x 20 &5f08 &c5 &5f85 &e9 ( 92, 61)     
# &03 SPRITE_SPACESUIT_FORTY_FIVE_HEAD_DOWN       : &5e0f &90 &5e8c &91 10 x 19 &5f09 &04 &5f86 &81 ( 64, 48)   v 
# &04 SPRITE_SPACESUIT_VERTICAL                   : &5e10 &40 &5e8d &a8  5 x 22 &5f0a &66 &5f87 &98 (102, 19)     
# &05 SPRITE_SPACESUIT_WALKING_ONE                : &5e11 &50 &5e8e &a0  6 x 21 &5f0b &06 &5f88 &98 ( 96, 19)     
# &06 SPRITE_SPACESUIT_WALKING_TWO                : &5e12 &60 &5e8f &a0  7 x 21 &5f0c &91 &5f89 &e0 ( 25, 28)     
# &07 SPRITE_SPACESUIT_WALKING_THREE              : &5e13 &40 &5e90 &a0  5 x 21 &5f0d &41 &5f8a &e0 ( 20, 28)     
# &08 SPRITE_BULLET_HORIZONTAL                    : &5e14 &21 &5e91 &09  3 x  2 &5f0e &60 &5f8b &7a (  6, 79) h v 
# &09 SPRITE_BULLET_TWENTY_TWO                    : &5e15 &20 &5e92 &08  3 x  2 &5f0f &43 &5f8c &00 ( 52,  0)     
# &0a SPRITE_BULLET_FORTY_FIVE                    : &5e16 &20 &5e93 &10  3 x  3 &5f10 &d0 &5f8d &72 ( 13, 78)     
# &0b SPRITE_BULLET_SIXTY                         : &5e17 &21 &5e94 &19  3 x  4 &5f11 &d4 &5f8e &e1 ( 77, 60) h v 
# &0c SPRITE_BULLET_SEVENTY_FIVE                  : &5e18 &11 &5e95 &19  2 x  4 &5f12 &e4 &5f8f &5a ( 78, 75) h v 
# &0d SPRITE_BULLET_VERTICAL                      : &5e19 &11 &5e96 &18  2 x  4 &5f13 &e4 &5f90 &3a ( 78, 71) h   
# &0e SPRITE_LEAF                                 : &5e1a &50 &5e97 &58  6 x 12 &5f14 &a4 &5f91 &81 ( 74, 48)     
# &0f SPRITE_DROP                                 : &5e1b &30 &5e98 &18  4 x  4 &5f15 &03 &5f92 &00 ( 48,  0)     
# &10 SPRITE_FROGMAN_ONE                          : &5e1c &60 &5e99 &60  7 x 13 &5f16 &97 &5f93 &e1 (121, 60)     
# &11 SPRITE_FROGMAN_TWO                          : &5e1d &51 &5e9a &78  6 x 16 &5f17 &63 &5f94 &0a ( 54, 65) h   
# &12 SPRITE_FROGMAN_THREE                        : &5e1e &50 &5e9b &81  6 x 17 &5f18 &03 &5f95 &02 ( 48, 64)   v 
# &13 SPRITE_ROLLING_ROBOT                        : &5e1f &50 &5e9c &98  6 x 20 &5f19 &05 &5f96 &e9 ( 80, 61)     
# &14 SPRITE_CHATTER                              : &5e20 &80 &5e9d &78  9 x 16 &5f1a &77 &5f97 &60 (119, 12)     
# &15 SPRITE_HOVERING_ROBOT                       : &5e21 &50 &5e9e &70  6 x 15 &5f1b &97 &5f98 &68 (121, 13)     
# &16 SPRITE_CLAWED_ROBOT                         : &5e22 &50 &5e9f &98  6 x 20 &5f1c &65 &5f99 &e9 ( 86, 61)     
# &17 SPRITE_FIREBALL                             : &5e23 &b0 &5ea0 &90 12 x 19 &5f1d &06 &5f9a &00 ( 96,  0)     
# &18 SPRITE_GREENERY                             : &5e24 &b0 &5ea1 &28 12 x  6 &5f1e &06 &5f9b &00 ( 96,  0)     
# &19 SPRITE_SHORT_BUSH                           : &5e25 &80 &5ea2 &48  9 x 10 &5f1f &06 &5f9c &49 ( 96, 41)     
# &1a SPRITE_TALL_BUSH                            : &5e26 &80 &5ea3 &78  9 x 16 &5f20 &06 &5f9d &49 ( 96, 41)     
# &1b SPRITE_LARGE_HIVE                           : &5e27 &50 &5ea4 &69  6 x 14 &5f21 &e6 &5f9e &50 (110, 10)   v 
# &1c SPRITE_SLIME_ONE                            : &5e28 &90 &5ea5 &20 10 x  5 &5f22 &c6 &5f9f &68 (108, 13)     
# &1d SPRITE_SLIME_TWO                            : &5e29 &70 &5ea6 &28  8 x  6 &5f23 &d6 &5fa0 &68 (109, 13)     
# &1e SPRITE_SLIME_THREE                          : &5e2a &50 &5ea7 &38  6 x  8 &5f24 &e6 &5fa1 &58 (110, 11)     
# &1f SPRITE_SLIME_FOUR                           : &5e2b &30 &5ea8 &48  4 x 10 &5f25 &f6 &5fa2 &50 (111, 10)     
# &20 SPRITE_BOULDER                              : &5e2c &40 &5ea9 &38  5 x  8 &5f26 &37 &5fa3 &d0 (115, 26)     
# &21 SPRITE_BALL                                 : &5e2d &20 &5eaa &20  3 x  5 &5f27 &d6 &5fa4 &d9 (109, 59)     
# &22 SPRITE_CRYSTAL                              : &5e2e &10 &5eab &08  2 x  2 &5f28 &65 &5fa5 &a0 ( 86, 20)     
# &23 SPRITE_SPACESHIP_SUPPORT                    : &5e2f &f1 &5eac &f8 16 x 32 &5f29 &02 &5fa6 &00 ( 32,  0) h   
# &24 SPRITE_SPACESHIP_WALL_CORNER_PIPES_ONE      : &5e30 &f1 &5ead &f8 16 x 32 &5f2a &03 &5fa7 &28 ( 48,  5) h   
# &25 SPRITE_SPACESHIP_WALL_SMALL_CORNER          : &5e31 &71 &5eae &68  8 x 14 &5f2b &12 &5fa8 &01 ( 33, 32) h   
# &26 SPRITE_SPACESHIP_WALL_VERTICAL_QUARTER      : &5e32 &30 &5eaf &f9  4 x 32 &5f2c &c2 &5fa9 &01 ( 44, 32)   v 
# &27 SPRITE_SPACESHIP_WALL_SLOPE_TWELVE_ONE      : &5e33 &f0 &5eb0 &f9 16 x 32 &5f2d &03 &5faa &09 ( 48, 33)   v 
# &28 SPRITE_SPACESHIP_WALL_SLOPE_TWELVE_TWO      : &5e34 &f0 &5eb1 &b9 16 x 24 &5f2e &03 &5fab &49 ( 48, 41)   v 
# &29 SPRITE_SPACESHIP_WALL_SLOPE_TWELVE_THREE    : &5e35 &f0 &5eb2 &79 16 x 16 &5f2f &03 &5fac &89 ( 48, 49)   v 
# &2a SPRITE_SPACESHIP_WALL_SLOPE_TWELVE_FOUR     : &5e36 &f0 &5eb3 &39 16 x  8 &5f30 &03 &5fad &c9 ( 48, 57)   v 
# &2b SPRITE_SPACESHIP_WALL_CORNER                : &5e37 &f1 &5eb4 &f8 16 x 32 &5f31 &02 &5fae &01 ( 32, 32) h   
# &2c SPRITE_SPACESHIP_WALL_HORIZONTAL_HALF       : &5e38 &f0 &5eb5 &78 16 x 16 &5f32 &02 &5faf &81 ( 32, 48)     
# &2d SPRITE_SWITCH_BOX                           : &5e39 &30 &5eb6 &38  4 x  8 &5f33 &04 &5fb0 &41 ( 64, 40)     
# &2e SPRITE_SWITCH                               : &5e3a &60 &5eb7 &38  7 x  8 &5f34 &96 &5fb1 &81 (105, 48)     
# &2f SPRITE_RAIL_CORNER                          : &5e3b &30 &5eb8 &38  4 x  8 &5f35 &c4 &5fb2 &41 ( 76, 40)     
# &30 SPRITE_RAIL_VERTICAL                        : &5e3c &30 &5eb9 &f8  4 x 32 &5f36 &04 &5fb3 &80 ( 64, 16)     
# &31 SPRITE_RAIL_HORIZONTAL                      : &5e3d &f0 &5eba &38 16 x  8 &5f37 &04 &5fb4 &41 ( 64, 40)     
# &32 SPRITE_SPACESHIP_WALL_HORIZONTAL_HALF_PIPES : &5e3e &f0 &5ebb &78 16 x 16 &5f38 &43 &5fb5 &80 ( 52, 16)     
# &33 SPRITE_SPACESHIP_WALL_CORNER_PIPES_TWO      : &5e3f &f1 &5ebc &f8 16 x 32 &5f39 &43 &5fb6 &20 ( 52,  4) h   
# &34 SPRITE_STONE_SLOPE_TWENTY_TWO_ONE           : &5e40 &f0 &5ebd &f8 16 x 32 &5f3a &00 &5fb7 &00 (  0,  0)     
# &35 SPRITE_STONE_SLOPE_TWENTY_TWO_TWO           : &5e41 &f0 &5ebe &78 16 x 16 &5f3b &00 &5fb8 &00 (  0,  0)     
# &36 SPRITE_EARTH_SLOPE_TWENTY_TWO_ONE           : &5e42 &f0 &5ebf &f8 16 x 32 &5f3c &05 &5fb9 &00 ( 80,  0)     
# &37 SPRITE_EARTH_SLOPE_TWENTY_TWO_TWO           : &5e43 &f0 &5ec0 &78 16 x 16 &5f3d &05 &5fba &00 ( 80,  0)     
# &38 SPRITE_EARTH_SLOPE_FORTY_FIVE               : &5e44 &f1 &5ec1 &f8 16 x 32 &5f3e &01 &5fbb &89 ( 16, 49) h   
# &39 SPRITE_STONE                                : &5e45 &f0 &5ec2 &f8 16 x 32 &5f3f &00 &5fbc &80 (  0, 16)     
# &3a SPRITE_STONE_HORIZONTAL_THREE_QUARTERS      : &5e46 &f0 &5ec3 &c9 16 x 26 &5f40 &00 &5fbd &80 (  0, 16)   v 
# &3b SPRITE_STONE_HORIZONTAL_HALF                : &5e47 &f0 &5ec4 &78 16 x 16 &5f41 &00 &5fbe &80 (  0, 16)     
# &3c SPRITE_STONE_HORIZONTAL_QUARTER             : &5e48 &f0 &5ec5 &38 16 x  8 &5f42 &00 &5fbf &31 (  0, 38)     
# &3d SPRITE_EARTH                                : &5e49 &f0 &5ec6 &f8 16 x 32 &5f43 &05 &5fc0 &80 ( 80, 16)     
# &3e SPRITE_EARTH_HORIZONTAL_HALF_WITH_EDGE      : &5e4a &f0 &5ec7 &89 16 x 18 &5f44 &05 &5fc1 &09 ( 80, 33)   v 
# &3f SPRITE_EARTH_EDGE                           : &5e4b &f0 &5ec8 &09 16 x  2 &5f45 &05 &5fc2 &89 ( 80, 49)   v 
# &40 SPRITE_EARTH_HORIZONTAL_QUARTER_WITH_EDGE   : &5e4c &f0 &5ec9 &49 16 x 10 &5f46 &05 &5fc3 &49 ( 80, 41)   v 
# &41 SPRITE_STONE_VERTICAL_QUARTER               : &5e4d &30 &5eca &f8  4 x 32 &5f47 &00 &5fc4 &80 (  0, 16)     
# &42 SPRITE_STONE_SLOPE_SEVENTY_EIGHT            : &5e4e &60 &5ecb &f8  7 x 32 &5f48 &c0 &5fc5 &80 ( 12, 16)     
# &43 SPRITE_STONE_SLOPE_FORTY_FIVE               : &5e4f &f0 &5ecc &f9 16 x 32 &5f49 &00 &5fc6 &71 (  0, 46)   v 
# &44 SPRITE_STONE_SLOPE_FORTY_FIVE_FULL          : &5e50 &f0 &5ecd &f9 16 x 32 &5f4a &00 &5fc7 &01 (  0, 32)   v 
# &45 SPRITE_GARGOYLE                             : &5e51 &70 &5ece &68  8 x 14 &5f4b &44 &5fc8 &d0 ( 68, 26)     
# &46 SPRITE_NONE                                 : &5e52 &00 &5ecf &00  1 x  1 &5f4c &c0 &5fc9 &00 ( 12,  0)     
# &47 SPRITE_SPACESHIP_WALL_PIPES                 : &5e53 &f0 &5ed0 &f8 16 x 32 &5f4d &03 &5fca &80 ( 48, 16)     
# &48 SPRITE_SPACESHIP_WALL_HORIZONTAL_HALF_TWO   : &5e54 &f0 &5ed1 &78 16 x 16 &5f4e &03 &5fcb &01 ( 48, 32)     
# &49 SPRITE_SPACESHIP_WALL_HORIZONTAL_QUARTER    : &5e55 &f0 &5ed2 &38 16 x  8 &5f4f &03 &5fcc &41 ( 48, 40)     
# &4a SPRITE_METAL_DOOR_HORIZONTAL                : &5e56 &f0 &5ed3 &39 16 x  8 &5f50 &07 &5fcd &4a (112, 73)   v 
# &4b SPRITE_METAL_DOOR_VERTICAL                  : &5e57 &30 &5ed4 &f9  4 x 32 &5f51 &07 &5fce &89 (112, 49)   v 
# &4c SPRITE_SPACESHIP_LEG                        : &5e58 &31 &5ed5 &f9  4 x 32 &5f52 &53 &5fcf &a8 ( 53, 21) h v 
# &4d SPRITE_KEY                                  : &5e59 &41 &5ed6 &28  5 x  6 &5f53 &17 &5fd0 &e9 (113, 61) h   
# &4e SPRITE_TRANSPORTER                          : &5e5a &f0 &5ed7 &48 16 x 10 &5f54 &02 &5fd1 &f9 ( 32, 63)     
# &4f SPRITE_WASP_ONE                             : &5e5b &20 &5ed8 &18  3 x  4 &5f55 &d4 &5fd2 &c0 ( 77, 24)     
# &50 SPRITE_WASP_TWO                             : &5e5c &40 &5ed9 &11  5 x  3 &5f56 &80 &5fd3 &72 (  8, 78)   v 
# &51 SPRITE_WASP_THREE                           : &5e5d &20 &5eda &19  3 x  4 &5f57 &d4 &5fd4 &c0 ( 77, 24)   v 
# &52 SPRITE_WORM_ONE                             : &5e5e &50 &5edb &20  6 x  5 &5f58 &07 &5fd5 &00 (112,  0)     
# &53 SPRITE_WORM_TWO                             : &5e5f &50 &5edc &29  6 x  6 &5f59 &f6 &5fd6 &20 (111,  4)   v 
# &54 SPRITE_WORM_THREE                           : &5e60 &30 &5edd &41  4 x  9 &5f5a &c6 &5fd7 &00 (108,  0)   v 
# &55 SPRITE_COLUMN                               : &5e61 &70 &5ede &f8  8 x 32 &5f5b &87 &5fd8 &e0 (120, 28)     
# &56 SPRITE_CANNON                               : &5e62 &c0 &5edf &70 13 x 15 &5f5c &04 &5fd9 &00 ( 64,  0)     
# &57 SPRITE_ALIEN_WEAPON                         : &5e63 &40 &5ee0 &30  5 x  7 &5f5d &84 &5fda &18 ( 72,  3)     
# &58 SPRITE_REMOTE_CONTROL_DEVICE                : &5e64 &40 &5ee1 &20  5 x  5 &5f5e &47 &5fdb &11 (116, 34)     
# &59 SPRITE_BIRD_ONE                             : &5e65 &20 &5ee2 &20  3 x  5 &5f5f &67 &5fdc &e1 (118, 60)     
# &5a SPRITE_BIRD_TWO                             : &5e66 &60 &5ee3 &19  7 x  4 &5f60 &c6 &5fdd &c0 (108, 24)   v 
# &5b SPRITE_BIRD_THREE                           : &5e67 &60 &5ee4 &18  7 x  4 &5f61 &c6 &5fde &c8 (108, 25)     
# &5c SPRITE_BIRD_FOUR                            : &5e68 &40 &5ee5 &28  5 x  6 &5f62 &96 &5fdf &51 (105, 42)     
# &5d SPRITE_PIANO                                : &5e69 &f0 &5ee6 &60 16 x 13 &5f63 &02 &5fe0 &b1 ( 32, 54)     
# &5e SPRITE_TURRET                               : &5e6a &70 &5ee7 &48  8 x 10 &5f64 &44 &5fe1 &78 ( 68, 15)     
# &5f SPRITE_WALL_MOUNTED_EQUIPMENT               : &5e6b &20 &5ee8 &80  3 x 17 &5f65 &d4 &5fe2 &00 ( 77,  0)     
# &60 SPRITE_CONSOLE                              : &5e6c &70 &5ee9 &58  8 x 12 &5f66 &c3 &5fe3 &2a ( 60, 69)     
# &61 SPRITE_BIG_FISH                             : &5e6d &90 &5eea &58 10 x 12 &5f67 &67 &5fe4 &00 (118,  0)     
# &62 SPRITE_MUSHROOMS                            : &5e6e &c0 &5eeb &39 13 x  8 &5f68 &36 &5fe5 &02 ( 99, 64)   v 
# &63 SPRITE_MUSHROOM_BALL                        : &5e6f &30 &5eec &19  4 x  4 &5f69 &86 &5fe6 &02 (104, 64)   v 
# &64 SPRITE_IMP_WALKING_ONE                      : &5e70 &40 &5eed &68  5 x 14 &5f6a &01 &5fe7 &00 ( 16,  0)     
# &65 SPRITE_IMP_WALKING_TWO                      : &5e71 &50 &5eee &68  6 x 14 &5f6b &51 &5fe8 &00 ( 21,  0)     
# &66 SPRITE_IMP_WALKING_THREE                    : &5e72 &60 &5eef &68  7 x 14 &5f6c &31 &5fe9 &70 ( 19, 14)     
# &67 SPRITE_IMP_CLIMBING_ONE                     : &5e73 &40 &5ef0 &58  5 x 12 &5f6d &b1 &5fea &00 ( 27,  0)     
# &68 SPRITE_IMP_CLIMBING_TWO                     : &5e74 &40 &5ef1 &68  5 x 14 &5f6e &b1 &5feb &60 ( 27, 12)     
# &69 SPRITE_IMP_JUMPING                          : &5e75 &30 &5ef2 &58  4 x 12 &5f6f &c4 &5fec &e0 ( 76, 28)     
# &6a SPRITE_PIPE                                 : &5e76 &f0 &5ef3 &38 16 x  8 &5f70 &02 &5fed &4a ( 32, 73)     
# &6b SPRITE_JETPACK_BOOSTER                      : &5e77 &20 &5ef4 &38  3 x  8 &5f71 &67 &5fee &0a (118, 65)     
# &6c SPRITE_WEAPON                               : &5e78 &30 &5ef5 &28  4 x  6 &5f72 &47 &5fef &b1 (116, 54)     
# &6d SPRITE_LIGHTNING_QUARTER                    : &5e79 &31 &5ef6 &48  4 x 10 &5f73 &15 &5ff0 &99 ( 81, 51) h   
# &6e SPRITE_LIGHTNING_HALF                       : &5e7a &70 &5ef7 &48  8 x 10 &5f74 &55 &5ff1 &99 ( 85, 51)     
# &6f SPRITE_LIGHTNING_THREE_QUARTERS             : &5e7b &b1 &5ef8 &48 12 x 10 &5f75 &35 &5ff2 &99 ( 83, 51) h   
# &70 SPRITE_NEST                                 : &5e7c &f0 &5ef9 &48 16 x 10 &5f76 &05 &5ff3 &99 ( 80, 51)     
# &71 SPRITE_TRANSPORTER_BEAM                     : &5e7d &70 &5efa &08  8 x  2 &5f77 &00 &5ff4 &72 (  0, 78)     
# &72 SPRITE_PIRANHA_ONE                          : &5e7e &51 &5efb &30  6 x  7 &5f78 &16 &5ff5 &c9 ( 97, 57) h   
# &73 SPRITE_PIRANHA_TWO                          : &5e7f &41 &5efc &20  5 x  5 &5f79 &37 &5ff6 &61 (115, 44) h   
# &74 SPRITE_PIRANHA_THREE                        : &5e80 &50 &5efd &28  6 x  6 &5f7a &e6 &5ff7 &59 (110, 43)     
# &75 SPRITE_FLUFFY                               : &5e81 &51 &5efe &39  6 x  8 &5f7b &76 &5ff8 &c1 (103, 56) h v 
# &76 SPRITE_FLASK                                : &5e82 &30 &5eff &70  4 x 15 &5f7c &04 &5ff9 &d0 ( 64, 26)     
# &77 SPRITE_NONE_TWO                             : &5e83 &80 &5f00 &38  9 x  8 &5f7d &b6 &5ffa &11 (107, 34)     
# &78 SPRITE_HOVERING_BALL                        : &5e84 &30 &5f01 &30  4 x  7 &5f7e &c4 &5ffb &88 ( 76, 17)     
# &79 SPRITE_PILL                                 : &5e85 &30 &5f02 &20  4 x  5 &5f7f &47 &5ffc &89 (116, 49)     
# &7a SPRITE_DEVICE                               : &5e86 &50 &5f03 &20  6 x  5 &5f80 &84 &5ffd &90 ( 72, 18)     
# &7b SPRITE_POWER_POD                            : &5e87 &50 &5f04 &20  6 x  5 &5f81 &b6 &5ffe &e8 (107, 29)     
# &7c SPRITE_WHISTLE                              : &5e88 &40 &5f05 &20  5 x  5 &5f82 &a4 &5fff &90 ( 74, 18)     

; sprites_width_and_horizontal_flip_table                           # 8421.... width of sprite in pixels, minus one
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f               # .......1 flip sprite horizontally if set
&5e0c c0 a0 50 90 40 50 60 40 21 20 20 21 11 11 50 30 ; &00         #
&5e1c 60 51 50 50 80 50 50 b0 b0 80 80 50 90 70 50 30 ; &10         # Also width of sprite in fractions
&5e2c 40 20 10 f1 f1 71 30 f0 f0 f0 f0 f1 f0 30 60 30 ; &20
&5e3c 30 f0 f0 f1 f0 f0 f0 f0 f1 f0 f0 f0 f0 f0 f0 f0 ; &30
&5e4c f0 30 60 f0 f0 70 00 f0 f0 f0 f0 30 31 41 f0 20 ; &40
&5e5c 40 20 50 50 30 70 c0 40 40 20 60 60 40 f0 70 20 ; &50
&5e6c 70 90 c0 30 40 50 60 40 40 30 f0 20 30 31 70 b1 ; &60
&5e7c f0 70 51 41 50 51 30 80 30 30 50 50 40          ; &70

; sprites_height_and_vertical_flip_table                            # 84218... height of sprite in rows, minus one
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f               # .......1 flip sprite vertically if set
&5e89 40 80 98 91 a8 a0 a0 a0 09 08 10 19 19 18 58 18 ; &00         #
&5e99 60 78 81 98 78 70 98 90 28 48 78 69 20 28 38 48 ; &10         # Also height of sprite in fractions
&5ea9 38 20 08 f8 f8 68 f9 f9 b9 79 39 f8 78 38 38 38 ; &20
&5eb9 f8 38 78 f8 f8 78 f8 78 f8 f8 c9 78 38 f8 89 09 ; &30
&5ec9 49 f8 f8 f9 f9 68 00 f8 78 38 39 f9 f9 28 48 18 ; &40
&5ed9 11 19 20 29 41 f8 70 30 20 20 19 18 28 60 48 80 ; &50
&5ee9 58 58 39 19 68 68 68 58 68 58 38 38 28 48 48 48 ; &60
&5ef9 48 08 30 20 28 39 70 38 30 20 20 20 20          ; &70

; sprites_spritesheet_x_table                                       # Left of sprite in spritesheet, in pixel columns
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f               #
&5f06 36 44 c5 04 66 06 91 41 60 43 d0 d4 e4 e4 a4 03 ; &00         # 8421.... -> ....8421 +1x
&5f16 97 63 03 05 77 97 65 06 06 06 06 e6 c6 d6 e6 f6 ; &10         # .....421 -> .421.... +16x
&5f26 37 d6 65 02 03 12 c2 03 03 03 03 02 02 04 96 c4 ; &20
&5f36 04 04 43 43 00 00 05 05 01 00 00 00 00 05 05 05 ; &30
&5f46 05 00 c0 00 00 44 c0 03 03 03 07 07 53 17 02 d4 ; &40
&5f56 80 d4 07 f6 c6 87 04 84 47 67 c6 c6 96 02 44 d4 ; &50
&5f66 c3 67 36 86 01 51 31 b1 b1 c4 02 67 47 15 55 35 ; &60
&5f76 05 00 16 37 e6 76 04 b6 c4 47 84 b6 a4          ; &70

; sprites_spritesheet_y_table                                       # Top of sprite in spritesheet, in rows
;      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f               #
&5f83 42 02 e9 81 98 98 e0 e0 7a 00 72 e1 5a 3a 81 00 ; &00         # 84218... -> ...84218 +1y
&5f93 e1 0a 02 e9 60 68 e9 00 00 49 49 50 68 68 58 50 ; &10         # ......21 -> .21..... +32y
&5fa3 d0 d9 a0 00 28 01 01 09 49 89 c9 01 81 41 81 41 ; &20
&5fb3 80 41 80 20 00 00 00 00 89 80 80 80 31 80 09 89 ; &30
&5fc3 49 80 80 71 01 d0 00 80 01 41 4a 89 a8 e9 f9 c0 ; &40
&5fd3 72 c0 00 20 00 e0 00 18 11 e1 c0 c8 51 b1 78 00 ; &50
&5fe3 2a 00 02 02 00 00 70 00 60 e0 4a 0a b1 99 99 99 ; &60
&5ff3 99 72 c9 61 59 c1 d0 11 88 89 90 e8 90          ; &70

; unused
&6000 16 16 16 16 16 16 16 16 06 06 06 06

; entry_point_after_relocation
&600c a9 01    LDA #&01 ; R1: Number of characters per line
&600e a2 28    LDX #&28 ; 40
&6010 8d 00 fe STA &fe00 ; video register number
&6013 8e 01 fe STX &fe01 ; video register value
&6016 a9 0c    LDA #&0c ; R12: Displayed screen start address register (high)
&6018 a2 28    LDX #&28                                             # Set MODE 7 screen start address to &7c00
&601a 8d 00 fe STA &fe00 ; video register number
&601d 8e 01 fe STX &fe01 ; video register value
&6020 a9 0d    LDA #&0d ; R13: Displayed screen start address register (low)
&6022 a2 00    LDX #&00
&6024 8d 00 fe STA &fe00 ; video register number
&6027 8e 01 fe STX &fe01 ; video register value
&602a a2 83    LDX #&83 ; &fc83 = &10000 - &37d
&602c a0 fc    LDY #&fc
&602e a9 00    LDA #&00
; calculate_encrypted_game_state_checksum_loop
&6030 4d f8 07 EOR &07f8 ; game_state                               # Checksum &07f8 - &0b74 (game_state_checksum_two - 1)
#     actually EOR checksum_address
&6033 ee 31 60 INC &6031 ; checksum_address_low
&6036 d0 03    BNE &603b ; skip_page
&6038 ee 32 60 INC &6032 ; checksum_address_high
; skip_page
&603b e8       INX
&603c d0 f2    BNE &6030 ; calculate_encrypted_game_state_checksum_loop
&603e c8       INY
&603f d0 ef    BNE &6030 ; calculate_encrypted_game_state_checksum_loop
&6041 8d 85 60 STA &6085 ; encrypted_game_state_checksum
; decrypt_game_state
&6044 38       SEC
&6045 f8       SED
&6046 a9 82    LDA #&82 ; &fc82 = &10000 - &37e
&6048 85 02    STA &02 ; size_low
&604a a9 fc    LDA #&fc
&604c 85 03    STA &03 ; size_high
&604e a0 00    LDY #&00
&6050 a9 6e    LDA #&6e
&6052 85 01    STA &01 ; key
&6054 a9 92    LDA #&92
; decrypt_game_state_loop                                           # Decrypt &07f8 - &0b75 (game_state_checksum_two)
&6056 65 01    ADC &01 ; key
&6058 69 15    ADC #&15
&605a 85 01    STA &01 ; key
&605c 59 f8 07 EOR &07f8,Y ; game_state
#     actually EOR source_address,Y
&605f 99 f8 07 STA &07f8,Y ; game_state
#     actually EOR target_address,Y
&6062 45 01    EOR &01 ; key
&6064 c8       INY
&6065 d0 06    BNE &606d ; skip_page
&6067 ee 5e 60 INC &605e ; source_address_high
&606a ee 61 60 INC &6061 ; target_address_high
; skip_page
&606d e6 02    INC &02 ; size_low
&606f d0 e5    BNE &6056 ; decrypt_game_state_loop
&6071 e6 03    INC &03 ; size_high
&6073 d0 e1    BNE &6056 ; decrypt_game_state_loop
&6075 d8       CLD
; infinite_loop_if_fallback_teleport_not_as_expected
&6076 ad 27 08 LDA &0827 ; player_teleports_x + 4 (fallback)
&6079 c9 99    CMP #&99                                             # (&99, &3c)
&607b d0 f9    BNE &6076 ; ; infinite_loop_if_fallback_teleport_not_as_expected
&607d ad 2c 08 LDA &082c ; player_teleports_y + 4 (fallback)
&6080 c9 3c    CMP #&3c
&6082 d0 f2    BNE &6076 ; ; infinite_loop_if_fallback_teleport_not_as_expected
&6084 a9 64    LDA #&64
#     actually LDA encrypted_game_state_checksum
&6086 4d 75 0b EOR &0b75 ; game_state_checksum_two
&6089 49 9f    EOR #&9f
&608b 8d 52 0b STA &0b52 ; game_state_checksum_one
&608e a9 43    LDA #&43
&6090 8d 75 0b STA &0b75 ; game_state_checksum_two
&6093 a0 07    LDY #&07
; move_game_state_variables_loop                                    # Copy &07f8 - &07fb to &00d9 - &00dc (rnd_state)
&6095 b9 f7 07 LDA &07f7,Y ; game_state - 1                         #              &07fc to &07dd         (player_object_held)
&6098 99 d8 00 STA &00d8,Y ; rnd_state - 1                          #              &07fd to &00de         (player_angle)
&609b 88       DEY                                                  #              &07fe to &00df         (player_facing)

&609c d0 f7    BNE &6095 ; move_game_state_variables_loop
; mark_objects_as_not_plotted
&609e a0 0f    LDY #&0f
; mark_objects_as_not_plotted_loop
&60a0 b9 c6 08 LDA &08c6,Y ; objects_flags
&60a3 09 01    ORA #&01 ; OBJECT_FLAG_NOT_PLOTTED
&60a5 99 c6 08 STA &08c6,Y ; objects_flags
&60a8 88       DEY
&60a9 d0 f5    BNE &60a0 ; mark_objects_as_not_plotted_loop
&60ab ad 9c 49 LDA &499c ; copy_protection_seed                     # Determine word to use using permuted seed and rnd state
&60ae 4a       LSR A                                                # .4.1.421 -> ..4.1.42
&60af 4a       LSR A                                                #          -> ...4.1.4
&60b0 6d 9c 49 ADC &499c ; copy_protection_seed                     #          -> .4xxxxxx (top bit clear)
&60b3 45 da    EOR &da ; rnd_state + 1                              # rnd_pair = ?(rnd_state + 1) ^ ?(rnd_state + 3)
&60b5 45 dc    EOR &dc ; rnd_state + 3                              # word_number = permuted_seed ^ rnd_pair
;                                                                   # Top bit of word_number = top bit of rnd_pair
&60b7 20 35 62 JSR &6235 ; copy_protection_screen
&60ba a9 0f    LDA #&0f                                             # Use OS defaults
&60bc 8d 42 fe STA &fe42 ; System VIA data direction register B
&60bf a9 0c    LDA #&0c                                             # Set addressable latch B4 = 1
&60c1 8d 40 fe STA &fe40 ; System VIA port B input/output register
&60c4 a9 05    LDA #&05                                             # Set addressable latch B5 = 0, i.e. scroll from &6000
&60c6 8d 40 fe STA &fe40 ; System VIA port B input/output register
&60c9 a9 00    LDA #&00                                             # Set System VIA timers 1 and 2 to one-shot mode
&60cb 8d 4b fe STA &fe4b ; System VIA auxiliary control register
&60ce a9 00    LDA #&00                                             # Set User VIA timers 1 and 2 to one-shot mode
&60d0 8d 6b fe STA &fe6b ; User VIA auxiliary control register
&60d3 a9 04    LDA #&04                                             # Use OS defaults
&60d5 8d 4c fe STA &fe4c ; System VIA peripheral control register
&60d8 a9 0e    LDA #&0e                                             # Use OS defaults
&60da 8d 6c fe STA &fe6c ; User VIA peripheral control register
&60dd a9 7f    LDA #&7f
&60df 8d 4e fe STA &fe4e ; System VIA interrupt enable register     # Disable all System VIA interrupts
&60e2 8d 6e fe STA &fe6e ; User VIA interrupt enable register       # Disable all User VIA interrupts
&60e5 a9 c2    LDA #&c2
&60e7 8d 4e fe STA &fe4e ; System VIA interrupt enable register     # Enable System VIA timer 1 and CA1 (v-sync) interrupts
&60ea a2 0a    LDX #&0a
&60ec a0 09    LDY #&09
; set_video_registers_loop
&60ee bd 5e 65 LDA &655e,X ; video_ula_and_register_values
&60f1 8c 00 fe STY &fe00 ; video register number
&60f4 8d 01 fe STA &fe01 ; video register value
&60f7 ca       DEX
&60f8 88       DEY
&60f9 10 f3    BPL &60ee ; set_video_registers_loop
&60fb bd 5e 65 LDA &655e,X ; video_ula_and_register_values
&60fe 8d 20 fe STA &fe20 ; video ULA control register
&6101 4c d0 01 JMP &01d0 ; wipe_screen_and_start_game

; write_character
&6104 8d ff ff STA &ffff
#     actually STA text_screen_address
&6107 ee 05 61 INC &6105 ; text_screen_address_low
&610a d0 03    BNE &610f ; skip_page
&610c ee 06 61 INC &6106 ; text_screen_address_high
; skip_page
&610f 60       RTS

; write_string
&6110 86 0a    STX &0a ; string_address_low
&6112 85 0b    STA &0b ; string_address_high
&6114 a0 00    LDY #&00 
; write_string_loop 
&6116 b1 0a    LDA (&0a),Y ; string_address
&6118 f0 06    BEQ &6120 ; leave
&611a 20 04 61 JSR &6104 ; write_character
&611d c8       INY
&611e d0 f6    BNE &6116 ; write_string_loop                        # Always branches
; leave
&6120 60       RTS

; write_number
&6121 a2 00    LDX #&00
&6123 86 01    STX &01 ; tens
&6125 38       SEC
; calculate_tens_and_ones_loop
&6126 e9 0a    SBC #&0a
&6128 e6 01    INC &01 ; tens
&612a b0 fa    BCS &6126 ; calculate_tens_and_ones_loop
&612c 48       PHA ; ones
&612d a5 01    LDA &01 ; tens
&612f 69 2f    ADC #&2f ; "0" - 1
&6131 c9 30    CMP #&30 ; "0"
&6133 f0 03    BEQ &6138 ; skip_leading_zero
&6135 20 04 61 JSR &6104 ; write_character
; skip_leading_zero
&6138 68       PLA ; ones
&6139 18       CLC
&613a 69 3a    ADC #&3a ; "0" + &0a
&613c 4c 04 61 JMP &6104 ; write_character

; write_number_and_ordinal_suffix
&613f 48       PHA ; number
&6140 20 21 61 JSR &6121 ; write_number
&6143 68       PLA ; number
&6144 c9 04    CMP #&04
&6146 90 02    BCC &614a ; skip_ceiling
&6148 a9 04    LDA #&04
; skip_ceiling
&614a aa       TAX
&614b ca       DEX
&614c bd 58 61 LDA &6158,X ; ordinal_suffixes
&614f 20 04 61 JSR &6104 ; write_character
&6152 bd 5c 61 LDA &615c,X ; ordinal_suffixes + 4
&6155 4c 04 61 JMP &6104 ; write_character

; ordinal_suffixes
&6158 73 6e 72 74                                     ; "snrt"
&615c 74 64 64 68                                     ; "tddh"

; please_type_this_word_string
&6160 83 50 6c 65 61 73 65 20 74 79 70 65 20 74 68 69 ; "Please type this word from the novella:"
&6170 73 20 77 6f 72 64 20 66 72 6f 6d 20 74 68 65 20
&6180 6e 6f 76 65 6c 6c 61 3a 00

; page_string
&6189 86 50 61 67 65 20 00                            ; TELETEXT_CYAN, "Page "

; line_from_string
&6190 82 6c 69 6e 65 20 66 72 6f 6d 20 00             ; TELETEXT_GREEN, "line from "

; word_from_string
&619c 81 77 6f 72 64 20 66 72 6f 6d 20 00             ; TELETEXT_RED, "word from "

; top_string
&61a8 74 6f 70 00                                     ; "top"

; bottom_string
&61ac 62 6f 74 74 6f 6d 00                            ; "bottom"

; left_string
&61b3 6c 65 66 74 00                                  ; "left"

; right_string
&61b8 72 69 67 68 74 00                               ; "right"

; wrong_try_again_string
&61be 81 57 72 6f 6e 67 2e 20 54 72 79 20 61 67 61 69 ; TELETEXT_RED, "Wrong. Try again"
&61ce 6e 00

; wrong_again_running_demo_version_string
&61d0 82 57 72 6f 6e 67 20 61 67 61 69 6e 2e 20 52 75 ; TELETEXT_GREEN, "Wrong again. Running demo version."
&61e0 6e 6e 69 6e 67 20 64 65 6d 6f 20 76 65 72 73 69
&61f0 6f 6e 2e 00

; this_will_hang_after_a_few_minutes_string
&61f4 81 54 68 69 73 20 77 69 6c 6c 20 68 61 6e 67 20 ; TELETEXT_RED, "This will hang up after a few minutes"
&6204 75 70 20 61 66 74 65 72 20 61 20 66 65 77 20 6d
&6214 69 6e 75 74 65 73 00

; correct_running_game_string
&621b 86 43 6f 72 72 65 63 74 2e 20 52 75 6e 6e 69 6e ; TELETEXT_CYAN, "Correct. Running game..."
&622b 67 20 67 61 6d 65 2e 2e 2e 00 

; copy_protection_screen                                            # Called with A = word to use
&6235 20 97 63 JSR &6397 ; find_word_data
&6238 20 42 64 JSR &6442 ; initialise_copy_protection_screen
&623b a9 a0    LDA #&a0 ; &7ca0 = screen_memory + &28 *4
&623d 8d 05 61 STA &6105 ; text_screen_address_low
&6240 a9 7c    LDA #&7c
&6242 8d 06 61 STA &6106 ; text_screen_address_high
&6245 a2 60    LDX #&60 ; &6160 = please_type_this_word_string
&6247 a9 61    LDA #&61
&6249 20 10 61 JSR &6110 ; write_string                             # Write "Please type this word from the novella:"
; write_page_for_word
&624c a9 25    LDA #&25 ; &7d25 = screen_memory + &28 * 7 + 13
&624e 8d 05 61 STA &6105 ; text_screen_address_low
&6251 a9 7d    LDA #&7d
&6253 8d 06 61 STA &6106 ; text_screen_address_high
&6256 a2 89    LDX #&89 ; &6189 = page_string
&6258 a9 61    LDA #&61
&625a 20 10 61 JSR &6110 ; write_string                             # Write "Page "
&625d a5 04    LDA &04 ; page_for_word
&625f 20 21 61 JSR &6121 ; write_number                             # Write page number for requested word
; write_line_for_word
&6262 a9 70    LDA #&70 ; &7d70 = screen_memory + &28 * 9 + 8
&6264 8d 05 61 STA &6105 ; text_screen_address_low
&6267 a9 7d    LDA #&7d
&6269 8d 06 61 STA &6106 ; text_screen_address_high
&626c a5 05    LDA &05 ; line_for_word                              # Negative if line from bottom of page
&626e 10 02    BPL &6272 ; skip_inverting_line
&6270 49 ff    EOR #&ff
&6272 18       CLC
; skip_inverting_line
&6273 69 01    ADC #&01
&6275 20 3f 61 JSR &613f ; write_number_and_ordinal_suffix          # Write line number for requested word
&6278 a2 90    LDX #&90 ; &6190 = line_from_string
&627a a9 61    LDA #&61
&627c 20 10 61 JSR &6110 ; write_string                             # Write "line from "
&627f 24 05    BIT &05 ; line_for_word                              # Negative if line from bottom of page
&6281 30 0a    BMI &628d ; is_line_from_bottom
; is_line_from_top
&6283 a2 a8    LDX #&a8 ; &61a8 = top_string
&6285 a9 61    LDA #&61
&6287 20 10 61 JSR &6110 ; write_string                             # Write "top"
&628a 4c 94 62 JMP &6294 ; write_word_for_word
; is_line_from_bottom
&628d a2 ac    LDX #&ac ; &61ac = bottom_string
&628f a9 61    LDA #&61
&6291 20 10 61 JSR &6110 ; write_string                             # Write "bottom"
; write_word_for_word
&6294 a9 c0    LDA #&c0 ; &7dc0 = screen_memory + &28 * 11 + 8
&6296 8d 05 61 STA &6105 ; text_screen_address_low
&6299 a9 7d    LDA #&7d
&629b 8d 06 61 STA &6106 ; text_screen_address_high
&629e a5 07    LDA &07 ; word_for_word
&62a0 2c 9c 49 BIT &499c ; copy_protection_seed                     # &40 set if word from right of line
&62a3 50 05    BVC &62aa ; not_from_right
; is_from_right
&62a5 18       CLC
&62a6 a5 06    LDA &06 ; words_on_line
&62a8 e5 07    SBC &07 ; word_for_word
; not_from_right
&62aa 18       CLC
&62ab 69 01    ADC #&01
&62ad 20 3f 61 JSR &613f ; write_number_and_ordinal_suffix          # Write word number for requested word
&62b0 a2 9c    LDX #&9c ; &619c = word_from_string
&62b2 a9 61    LDA #&61
&62b4 20 10 61 JSR &6110 ; write_string                             # Write "word from "
&62b7 2c 9c 49 BIT &499c ; copy_protection_seed                     # &40 set if word from right of line
&62ba 70 0a    BVS &62c6 ; is_word_from_right
; is_word_from_left
&62bc a2 b3    LDX #&b3 ; &61b3 = left_string
&62be a9 61    LDA #&61
&62c0 20 10 61 JSR &6110 ; write_string                             # Write "left"
&62c3 4c cd 62 JMP &62cd ; copy_protection_screen_loop
; is_word_from_right
&62c6 a2 b8    LDX #&b8 ; &61b8 = right_string
&62c8 a9 61    LDA #&61
&62ca 20 10 61 JSR &6110 ; write_string                             # Write "right"
; copy_protection_screen_loop
&62cd 20 5b 64 JSR &645b ; get_input_word
&62d0 a2 00    LDX #&00
&62d2 86 01    STX &01 ; entered_word_first_checksum
; calculate_entered_word_first_checksum_loop
&62d4 bd 8a 7e LDA &7e8a,X ; entered_word
&62d7 c9 0d    CMP #&0d ; CR
&62d9 f0 09    BEQ &62e4 ; finished_calculating_checksum
&62db 06 01    ASL &01 ; entered_word_first_checksum
&62dd 65 01    ADC &01 ; entered_word_first_checksum
&62df 85 01    STA &01 ; entered_word_first_checksum
&62e1 e8       INX
&62e2 d0 f0    BNE &62d4 ; calculate_entered_word_first_checksum_loop # Always branches
; finished_calculating_checksum
&62e4 a5 01    LDA &01 ; entered_word_first_checksum
&62e6 c5 08    CMP &08 ; expected_word_first_checksum
&62e8 f0 44    BEQ &632e ; is_correct
&62ea ce 93 63 DEC &6393 ; attempts_remaining
&62ed f0 14    BEQ &6303 ; too_many_attempts
; is_wrong
&62ef a9 29    LDA #&29 ; &7f29 = screen_memory + &28 * 20 + 9
&62f1 8d 05 61 STA &6105 ; text_screen_address_low
&62f4 a9 7f    LDA #&7f
&62f6 8d 06 61 STA &6106 ; text_screen_address_high
&62f9 a2 be    LDX #&be ; &61be = wrong_try_again_string
&62fb a9 61    LDA #&61
&62fd 20 10 61 JSR &6110 ; write_string                             # Write "Wrong. Try again"
&6300 4c cd 62 JMP &62cd ; copy_protection_screen_loop
; too_many_attempts
&6303 20 57 63 JSR &6357 ; set_copy_protection_bytes
&6306 a9 93    LDA #&93 ; &7d93 = screen_memory + &28 * 10 + 3
&6308 8d 05 61 STA &6105 ; text_screen_address_low
&630b a9 7d    LDA #&7d
&630d 8d 06 61 STA &6106 ; text_screen_address_high
&6310 a2 d0    LDX #&d0 ; &61d0 = wrong_again_running_demo_version_string
&6312 a9 61    LDA #&61
&6314 20 10 61 JSR &6110 ; write_string                             # Write "Wrong again. Running demo version."
&6317 a9 e1    LDA #&e1 ; &7de1 = screen_memory + &28 * 12 + 1
&6319 8d 05 61 STA &6105 ; text_screen_address_low
&631c a9 7d    LDA #&7d
&631e 8d 06 61 STA &6106 ; text_screen_address_high
&6321 a2 f4    LDX #&f4 ; &61f4 = this_will_hang_after_a_few_minutes_string
&6323 a9 61    LDA #&61
&6325 20 10 61 JSR &6110 ; write_string                             # Write "This will hang up after a few minutes"
&6328 20 42 63 JSR &6342 ; delay
&632b 4c 42 63 JMP &6342 ; delay
; is_correct
&632e 20 57 63 JSR &6357 ; set_copy_protection_bytes
&6331 a9 95    LDA #&95 ; &7d95 = screen_memory + &28 * 10 + 5
&6333 8d 05 61 STA &6105 ; text_screen_address_low
&6336 a9 7d    LDA #&7d
&6338 8d 06 61 STA &6106 ; text_screen_address_high
&633b a2 1b    LDX #&1b ; &621b = correct_running_game_string
&633d a9 62    LDA #&62
&633f 20 10 61 JSR &6110 ; write_string                             # Write "Correct. Running game..."
; delay
&6342 a9 0a    LDA #&0a
&6344 8d 94 63 STA &6394 ; delay_count
&6347 a2 00    LDX #&00
&6349 a0 00    LDY #&00
; delay_loop
&634b ca       DEX
&634c d0 fd    BNE &634b ; delay_loop
&634e 88       DEY
&634f d0 fa    BNE &634b ; delay_loop
&6351 ce 94 63 DEC &6394 ; delay_count
&6354 d0 f5    BNE &634b ; delay_loop
&6356 60       RTS

; set_copy_protection_bytes
&6357 a5 09    LDA &09 ; expected_word_obfuscated_second_checksum   # expected_word_second_checksum ^ (word_number & &7f) ^ &65
&6359 8d a7 0b STA &0ba7 ; copy_protection_first_byte
&635c a2 00    LDX #&00
&635e 86 01    STX &01 ; entered_word_second_checksum
&6360 8a       TXA
&6361 18       CLC
; calculate_entered_word_second_checksum_loop
&6362 e5 01    SBC &01 ; entered_word_second_checksum
&6364 85 01    STA &01 ; entered_word_second_checksum
&6366 bd 8a 7e LDA &7e8a,X ; entered_word
&6369 e8       INX
&636a 49 0d    EOR #&0d
&636c d0 f4    BNE &6362 ; calculate_entered_word_second_checksum_loop
&636e a5 01    LDA &01 ; entered_word_second_checksum
&6370 8d a8 0b STA &0ba8 ; copy_protection_second_byte
&6373 4c 13 64 JMP &6413 ; wipe_mode_seven_screen

; unused                                                            # Bytes other than spaces are shifted by &01
&6376 29 44 2a 20 51 4b 4e 20 4a 73 77 6a 6f 20 27 20 ; "(C) PJM Irvin & JC Smith 1988"
&6386 4b 44 20 54 6e 6a 75 69 20 32 3a 39 39

; attempts_remaining
&6393 08

; delay_count
&6394 0a

; requested_word
&6395 00

; word
&6396 00

; find_word_data                                                    # Called with A = word to find
&6397 8d 95 63 STA &6395 ; requested_word
&639a a9 00    LDA #&00
&639c 8d 96 63 STA &6396 ; word
; find_word_data_from_start
&639f ad 96 63 LDA &6396 ; word
&63a2 29 80    AND #&80
&63a4 8d 96 63 STA &6396 ; word
&63a7 a9 00    LDA #&00
&63a9 85 04    STA &04 ; page_for_word
&63ab a9 69    LDA #&69 ; &6569 = word_data
&63ad 85 02    STA &02 ; data_address_low
&63af a9 65    LDA #&65
&63b1 85 03    STA &03 ; data_address_high
&63b3 a0 00    LDY #&00
; find_word_data_loop
&63b5 b1 02    LDA (&02),Y ; data_address                           # Get a byte of word data
&63b7 f0 e6    BEQ &639f ; find_word_data_from_start                # &00 indicates end of word data; wrap word &c8 to &48
&63b9 29 f0    AND #&f0
&63bb d0 0f    BNE &63cc ; not_page_change
; is_page_change                                                    # If low nibble is zero, change of page number
&63bd b1 02    LDA (&02),Y ; data_address                           # Top nibble is change to page number
&63bf 18       CLC
&63c0 65 04    ADC &04 ; page_for_word
&63c2 85 04    STA &04 ; page_for_word
&63c4 e6 02    INC &02 ; data_address_low
&63c6 d0 ed    BNE &63b5 ; find_word_data_loop
&63c8 e6 03    INC &03 ; data_address_high
&63ca d0 e9    BNE &63b5 ; find_word_data_loop                      # Always branches
; not_page_change                                                   # If low nibble not zero, word location and checksums
&63cc ad 96 63 LDA &6396 ; word
&63cf cd 95 63 CMP &6395 ; requested_word
&63d2 f0 10    BEQ &63e4 ; found_requested_word
&63d4 ee 96 63 INC &6396 ; word
&63d7 18       CLC
&63d8 a5 02    LDA &02 ; data_address_low
&63da 69 03    ADC #&03                                             # Move to next word or page change
&63dc 85 02    STA &02 ; data_address_low
&63de 90 d5    BCC &63b5 ; find_word_data_loop
&63e0 e6 03    INC &03 ; data_address_low
&63e2 d0 d1    BNE &63b5 ; find_word_data_loop                      # Always branches
; found_requested_word
&63e4 b1 02    LDA (&02),Y ; data_address                           # First byte, low nibble, sets line
&63e6 29 0f    AND #&0f
&63e8 38       SEC
&63e9 e9 08    SBC #&08
&63eb 85 05    STA &05 ; line_for_word
&63ed b1 02    LDA (&02),Y ; data_address
&63ef 4a       LSR A
&63f0 4a       LSR A
&63f1 4a       LSR A
&63f2 4a       LSR A
&63f3 85 06    STA &06 ; words_on_line                              # First byte, high nibble is total number of words on line
&63f5 c8       INY
&63f6 b1 02    LDA (&02),Y ; data_address
&63f8 85 08    STA &08 ; expected_word_first_checksum               # Second byte is first checksum of expected word
&63fa c8       INY
&63fb b1 02    LDA (&02),Y ; data_address
&63fd 85 09    STA &09 ; expected_word_obfuscated_second_checksum   # Third byte is obfuscated second checksum of expected word
&63ff a5 04    LDA &04 ; page_for_word                              # Word = (page * 8) + line) % words_on_line
&6401 0a       ASL A
&6402 0a       ASL A
&6403 0a       ASL A
&6404 18       CLC
&6405 65 05    ADC &05 ; line_for_word
; calculate_modulus_loop
&6407 c5 06    CMP &06 ; words_on_line
&6409 90 05    BCC &6410 ; word_for_word
&640b e5 06    SBC &06 ; words_on_line
&640d 4c 07 64 JMP &6407 ; calculate_modulus_loop
; set_word_for_word
&6410 85 07    STA &07 ; word_for_word
&6412 60       RTS

; wipe_mode_seven_screen
&6413 a9 00    LDA #&00
&6415 a8       TAY
&6416 a2 04    LDX #&04
; wipe_mode_seven_screen_loop                                       # Wipe &7c00 - &7fff
&6418 99 00 7c STA &7c00,Y ; screen_memory
#     actually STA screen_address,Y
&641b 88       DEY
&641c d0 fa    BNE &6418 ; wipe_mode_seven_screen_loop
&641e ee 1a 64 INC &641a ; screen_address_high
&6421 ca       DEX
&6422 d0 f4    BNE &6418 ; wipe_mode_seven_screen_loop
&6424 ce 1a 64 DEC &641a ; screen_address_high
&6427 ce 1a 64 DEC &641a ; screen_address_high
&642a ce 1a 64 DEC &641a ; screen_address_high
&642d ce 1a 64 DEC &641a ; screen_address_high
&6430 a9 00    LDA #&00
&6432 a2 0e    LDX #&0e ; R14: Memory-address of text-cursor (high)
&6434 8e 00 fe STX &fe00 ; video register number
&6437 8d 01 fe STA &fe01 ; video register value
&643a e8       INX ; R15: Memory-address of text-cursor (low)
&643b 8e 00 fe STX &fe00 ; video register number
&643e 8d 01 fe STA &fe01 ; video register value
&6441 60       RTS

; initialise_copy_protection_screen
&6442 20 13 64 JSR &6413 ; wipe_mode_seven_screen
&6445 a9 84    LDA #&84 ; TELETEXT_BLUE
&6447 8d 87 7e STA &7e87 ; &7e87 = screen_memory + &28 * 16 + 7
&644a a9 9d    LDA #&9d ; TELETEXT_NEW_BACKGROUND
&644c 8d 88 7e STA &7e88 ; &7e88 = screen_memory + &28 * 16 + 8
&644f a9 83    LDA #&83 ; TELETEXT_YELLOW
&6451 8d 89 7e STA &7e89 ; &7e89 = screen_memory + &28 * 16 + 9
&6454 a9 9c    LDA #&9c ; TELETEXT_BLACK_BACKGROUND
&6456 8d 9c 7e STA &7e9c ; &7e9c = screen_memory + &28 * 16 + 28
&6459 60       RTS

; input_position
&645a 00

; get_input_word
&645b 20 f5 64 JSR &64f5 ; initialise_keys
&645e a2 10    LDX #&10
&6460 a9 20    LDA #&20 ; " "
; wipe_entered_word_loop
&6462 9d 8a 7e STA &7e8a,X ; entered_word
&6465 ca       DEX
&6466 10 fa    BPL &6462 ; wipe_entered_word_loop
&6468 a2 00    LDX #&00
&646a 8e 5a 64 STX &645a ; input_position
; get_input_word_loop
&646d a9 8a    LDA #&8a ; &7e8a = entered_word, also screen_memory + &28 * 16 + 10
&646f 18       CLC
&6470 6d 5a 64 ADC &645a ; input_position
&6473 a8       TAY
&6474 a9 7e    LDA #&7e
&6476 69 00    ADC #&00
&6478 38       SEC
&6479 e9 54    SBC #&54
&647b a2 0f    LDX #&0f ; R15: Memory-address of text-cursor (low)
&647d 8e 00 fe STX &fe00 ; video register number
&6480 8c 01 fe STY &fe01 ; video register value
&6483 ca       DEX ; R14: Memory-address of text-cursor (high)
&6484 8e 00 fe STX &fe00 ; video register number
&6487 8d 01 fe STA &fe01 ; video register value
&648a 20 29 65 JSR &6529 ; get_input_character
&648d c9 0d    CMP #&0d ; CR
&648f f0 24    BEQ &64b5 ; is_cr
&6491 c9 7f    CMP #&7f ; DELETE
&6493 d0 10    BNE &64a5 ; not_delete
; is_delete
&6495 ae 5a 64 LDX &645a ; input_position
&6498 f0 d3    BEQ &646d ; get_input_word_loop
&649a a9 20    LDA #&20 ; " "
&649c 9d 89 7e STA &7e89,X ; entered_word + 1
&649f ce 5a 64 DEC &645a ; input_position
&64a2 4c 6d 64 JMP &646d ; get_input_word_loop
; not_delete
&64a5 ae 5a 64 LDX &645a ; input_position
&64a8 e0 10    CPX #&10
&64aa f0 c1    BEQ &646d ; get_input_word_loop
&64ac 9d 8a 7e STA &7e8a,X ; entered_word
&64af ee 5a 64 INC &645a ; input_position
&64b2 4c 6d 64 JMP &646d ; get_input_word_loop
; is_cr
&64b5 ae 5a 64 LDX &645a ; input_position
&64b8 9d 8a 7e STA &7e8a,X ; entered_word
&64bb 60       RTS

; keycodes
&64bc 49 ; ENTER
&64bd 59 : DELETE
&64be 41 ; A
&64bf 64 ; B
&64c0 52 ; C
&64c1 32 ; D
&64c2 22 ; E
&64c3 43 ; F
&64c4 53 ; G
&64c5 54 ; H
&64c6 25 ; I
&64c7 45 ; J
&64c8 46 ; K
&64c9 56 ; L
&64ca 65 ; M
&64cb 55 ; N
&64cc 36 ; O
&64cd 37 ; P
&64ce 10 ; Q
&64cf 33 ; R
&64d0 51 ; S
&64d1 23 ; T
&64d2 35 ; U
&64d3 63 ; V
&64d4 21 ; W
&64d5 42 ; X
&64d6 44 ; Y
&64d7 61 ; Z

; keys_pressed
&64d8 00 ; ENTER
&64d9 00 ; DELETE
&64da 00 ; A
&64db 00 ; B
&64dc 00 ; C
&64dd 00 ; D
&64de 00 ; E
&64df 00 ; F
&64e0 00 ; G
&64e1 00 ; H
&64e2 00 ; I
&64e3 00 ; J
&64e4 00 ; K
&64e5 00 ; L
&64e6 00 ; M
&64e7 00 ; N
&64e8 00 ; O
&64e9 00 ; P
&64ea 00 ; Q
&64eb 00 ; R
&64ec 00 ; S
&64ed 00 ; T
&64ee 00 ; U
&64ef 00 ; V
&64f0 00 ; W
&64f1 00 ; X
&64f2 00 ; Y
&64f3 00 ; Z

; suppress_input
&64f4 00

; initialise_keys
&64f5 38       SEC
&64f6 b0 01    BCS &64f9 ; check_for_keys                           # Always branches
; get_keypress  
&64f8 18       CLC
; check_for_keys
&64f9 6e f4 64 ROR &64f4 ; suppress_input                           # Set &80 if initialising keys
&64fc a9 7f    LDA #&7f                                             # Set top bit as input, low seven bits as output
&64fe 8d 43 fe STA &fe43 ; System VIA data direction register A
&6501 a9 03    LDA #&03                                             # Disable keyboard auto scan
&6503 8d 40 fe STA &fe40 ; System VIA port B input/output register
&6506 a2 1b    LDX #&1b
; check_for_keypresses_loop
&6508 bd bc 64 LDA &64bc,X ; keycodes
&650b 8d 4f fe STA &fe4f ; System VIA input/output register A
&650e ad 4f fe LDA &fe4f ; System VIA input/output register A
&6511 2a       ROL A
&6512 7e d8 64 ROR &64d8,X ; keys_pressed
&6515 2c f4 64 BIT &64f4 ; suppress_input                           # &80 set if initialising keys
&6518 30 08    BMI &6522 ; consider_next_key
&651a bd d8 64 LDA &64d8,X ; consider_next_key
&651d 2a       ROL A
&651e 90 02    BCC &6522 ; consider_next_key
&6520 10 03    BPL &6525 ; leave_with_key
; consider_next_key
&6522 ca       DEX
&6523 10 e3    BPL &6508 ; check_for_keypresses_loop
; leave_with_key
&6525 a9 0b    LDA #&0b                                             # Unnecessary code
;                                                                   # Probably missing STA &fe40 to enable keyboard auto scan
&6527 8a       TXA
&6528 60       RTS

; get_input_character
&6529 20 f8 64 JSR &64f8 ; get_keypress                             # Returns A = offset of key pressed in keycodes
&652c 30 fb    BMI &6529 ; get_input_character
&652e d0 03    BNE &6533 ; not_cr
&6530 a9 0d    LDA #&0d ; CR
&6532 60       RTS
; not_cr
&6533 ca       DEX
&6534 d0 03    BNE &6539 ; not_delete
&6536 a9 7f    LDA #&7f ; DELETE
&6538 60       RTS
; not_delete
&6539 18       CLC
&653a 69 3f    ADC #&3f ; "A" - 2
&653c 60       RTS

; unused
&653d 32 34 2f 31 30 2f 38 38 20 44 69 73 63 20 28 63 ; "24/10/88 Disc (compact) RELEASE 2"
&654d 6f 6d 70 61 63 74 29 20 52 45 4c 45 41 53 45 20
&655d 32 

; video_ula_and_register_values
&655e 14 ; video ULA control register                               # Use MODE 2, disable cursor
&655f 7f ; R0: Horizontal total register
&6560 00 ; R1: Number of characters per line
&6561 5b ; R2: Horizontal sync position register
&6562 28 ; R3: Sync width register
&6563 26 ; R4: Vertical total register
&6564 00 ; R5: Vertical total adjust register
&6565 10 ; R6: Vertical displayed register
&6566 1b ; R7: Vertical sync position
&6567 00 ; R8: Interlace and delay register
&6568 07 ; R9: Scan lines per character register

# Word data
# =========
# First byte:
#    8421.... total number of words on line, or zero to skip pages
#    ....8421 line, or number of pages to skip
#
# Second byte is first checksum of expected word, checked against entered word to give "correct" or "wrong" message
# Third byte is obfuscated second checksum of expected word, checked in check_copy_protection to hang game if incorrect
#
# expected_word_obfuscated_second_checksum = expected_word_second_checksum ^ (word number & &7f) ^ &65

;     wl c1 c2
&6569 06       ; move 6 pages to page 6
&656a f8 6b 62 ; &00 : page  6, 1st line from top,     4th word from left, 12th word from right : infected
&656d 29 5e 4b ; &01 : page  6, 2nd line from top,     2nd word from left,  1st word from right : promotion
&6570 ca ec 67 ; &02 : page  6, 3rd line from top,     3rd word from left, 10th word from right : on
&6573 cb d1 b3 ; &03 : page  6, 4th line from top,     4th word from left,  9th word from right : German
&6576 5f 56 3f ; &04 : page  6, 8th line from top,     1st word from left,  5th word from right : eager
&6579 f6 67 6b ; &05 : page  6, 2nd line from bottom,  2nd word from left, 14th word from right : mass
&657c 87 a0 8f ; &06 : page  6, 1st line from bottom,  8th word from left,  1st word from right : planetfall
&657f 01       ; move 1 page to page 7
&6580 47 9c 9d ; &07 : page  7, 1st line from bottom,  4th word from left,  1st word from right : ship
&6583 01       ; move 1 page to page 8
&6584 58 e8 45 ; &08 : page  8, 1st line from top,     5th word from left,  1st word from right : computer
&6587 dd e5 3e ; &09 : page  8, 6th line from top,     5th word from left,  9th word from right : and
&658a c2 e4 8d ; &0a : page  8, 6th line from bottom, 11th word from left,  2nd word from right : seemed
&658d a3 85 9e ; &0b : page  8, 5th line from bottom, 10th word from left,  1st word from right : sank
&6590 c5 02 32 ; &0c : page  8, 3rd line from bottom,  2nd word from left, 11th word from right : Professor
&6593 c7 24 5b ; &0d : page  8, 1st line from bottom,  4th word from left,  9th word from right : intuition 
&6596 01       ; move 1 page to page 9
&6597 c8 57 01 ; &0e : page  9, 1st line from top,     1st word from left, 12th word from right : You
&659a d9 47 96 ; &0f : page  9, 2nd line from top,     9th word from left,  5th word from right : keen
&659d fa 58 07 ; &10 : page  9, 3rd line from top,    15th word from left,  1st word from right : various
&65a0 c3 e1 73 ; &11 : page  9, 5th line from bottom,  8th word from left,  5th word from right : arranged
&65a3 d6 32 1b ; &12 : page  9, 2nd line from bottom,  6th word from left,  8th word from right : was
&65a6 d7 a5 81 ; &13 : page  9, 1st line from bottom,  7th word from left,  7th word from right : they
&65a9 01       ; move 1 page to page 10
&65aa e9 20 3b ; &14 : page 10, 2nd line from top,    12th word from left,  3rd word from right : its
&65ad 3e 6c 40 ; &15 : page 10, 7th line from top,     3rd word from left,  1st word from right : noise
&65b0 e2 e5 21 ; &16 : page 10, 6th line from bottom,  5th word from left, 10th word from right : and
&65b3 e3 e7 31 ; &17 : page 10, 5th line from bottom,  6th word from left,  9th word from right : had
&65b6 95 b0 8a ; &18 : page 10, 3rd line from bottom,  6th word from left,  4th word from right : what
&65b9 01       ; move 1 page to page 11
&65ba e8 10 1b ; &19 : page 11, 1st line from top,     5th word from left, 10th word from right : found
&65bd b9 64 93 ; &1a : page 11, 2nd line from top,     2nd word from left, 10th word from right : Pahn
&65c0 5a a7 7b ; &1b : page 11, 3rd line from top,     1st word from left,  5th word from right : This
&65c3 eb 26 25 ; &1c : page 11, 4th line from top,     8th word from left,  7th word from right : the
&65c6 6d d9 86 ; &1d : page 11, 6th line from top,     4th word from left,  3rd word from right : either
&65c9 f1 32 17 ; &1e : page 11, 7th line from bottom,  7th word from left,  9th word from right : was
&65cc f5 9b 4c ; &1f : page 11, 3rd line from bottom, 11th word from left,  5th word from right : opening
&65cf 01       ; move 1 page to page 12
&65d0 dd 1c 1f ; &20 : page 12, 6th line from top,    11th word from left,  3rd word from right : might
&65d3 b2 c4 35 ; &21 : page 12, 6th line from bottom,  3rd word from left,  9th word from right : Somehow
&65d6 34 81 06 ; &22 : page 12, 4th line from bottom,  3rd word from left,  1st word from right : Spiegel
&65d9 47 c4 62 ; &23 : page 12, 1st line from bottom,  4th word from left,  1st word from right : minimum
&65dc 01       ; move 1 page to page 13
&65dd 89 b4 71 ; &24 : page 13, 2nd line from top,     2nd word from left,  7th word from right : ground
&65e0 01       ; move 1 page to page 14
&65e1 64 2b 2f ; &25 : page 14, 4th line from bottom,  1st word from left,  6th word from right : set
&65e4 a7 7e 20 ; &26 : page 14, 1st line from bottom,  2nd word from left,  9th word from right : sleek
&65e7 01       ; move 1 page to page 15
&65e8 e9 b7 94 ; &27 : page 15, 2nd line from top,    10th word from left,  5th word from right : were
&65eb dc 0f 2c ; &28 : page 15, 5th line from top,     8th word from left,  6th word from right : got
&65ee 8e 3f 2e ; &29 : page 15, 7th line from top,     7th word from left,  2nd word from right : benefit
&65f1 33 ce 0b ; &2a : page 15, 5th line from bottom,  2nd word from left,  2nd word from right : build
&65f4 37 bd 7c ; &2b : page 15, 1st line from bottom,  3rd word from left,  1st word from right : madam
&65f7 01       ; move 1 page to page 16
&65f8 7a 43 2c ; &2c : page 16, 3rd line from top,     5th word from left,  3rd word from right : Yet
&65fb 7c df 99 ; &2d : page 16, 5th line from top,     7th word from left,  1st word from right : Please
&65fe 6e 30 6e ; &2e : page 16, 7th line from top,     3rd word from left,  4th word from right : human
&6601 b5 5d 75 ; &2f : page 16, 3rd line from bottom,  5th word from left,  7th word from right : customary
&6604 97 cb 15 ; &30 : page 16, 1st line from bottom,  2nd word from left,  8th word from right : nicknamed
&6607 01       ; move 1 page to page 17
&6608 c0 07 05 ; &31 : page 17, 8th line from bottom,  9th word from left,  4th word from right : but
&660b e3 37 44 ; &32 : page 17, 5th line from bottom,  6th word from left,  9th word from right : aliens
&660e e5 10 bc ; &33 : page 17, 3rd line from bottom,  8th word from left,  7th word from right : cave
&6611 37 f1 89 ; &34 : page 17, 1st line from bottom,  1st word from left,  3rd word from right : before
&6614 01       ; move 1 page to page 18
&6615 aa e8 a4 ; &35 : page 18, 3rd line from top,     7th word from left,  4th word from right : back
&6618 dc 11 3d ; &36 : page 18, 5th line from top,     6th word from left,  8th word from right : discussed
&661b c2 a4 5f ; &37 : page 18, 6th line from bottom,  7th word from left,  6th word from right : pretty
&661e 37 4d 12 ; &38 : page 18, 1st line from bottom,  3rd word from left,  1st word from right : beast
&6621 01       ; move 1 page to page 19
&6622 79 8d 01 ; &39 : page 19, 2nd line from top,     7th word from left,  1st word from right : shouted
&6625 eb c0 82 ; &3a : page 19, 4th line from top,     2nd word from left, 13th word from right : scooping
&6628 3e 58 52 ; &3b : page 19, 7th line from top,     3rd word from left,  1st word from right : diseases
&662b b5 5d a5 ; &3c : page 19, 3rd line from bottom,  7th word from left,  5th word from right : worthwhile
&662e 01       ; move 1 page to page 20
&662f c9 b5 b3 ; &3d : page 20, 2nd line from top,     6th word from left,  7th word from right : some
&6632 dd 4e 14 ; &3e : page 20, 6th line from top,    10th word from left,  4th word from right : terrified
&6635 de 78 19 ; &3f : page 20, 7th line from top,    11th word from left,  3rd word from right : photo
&6638 57 46 56 ; &40 : page 20, 1st line from bottom,  5th word from left,  1st word from right : something
&663b 01       ; move 1 page to page 21
&663c c9 be cb ; &41 : page 21, 2nd line from top,     2nd word from left, 11th word from right : wind
&663f cc 79 c0 ; &42 : page 21, 5th line from top,     5th word from left,  8th word from right : into
&6642 74 3f 52 ; &43 : page 21, 4th line from bottom,  4th word from left,  4th word from right : top
&6645 d5 f7 c9 ; &44 : page 21, 3rd line from bottom, 10th word from left,  4th word from right : to
&6648 67 e8 cb ; &45 : page 21, 1st line from bottom,  6th word from left,  1st word from right : planet
&664b 01       ; move 1 page to page 22
&664c e9 b5 c8 ; &46 : page 22, 2nd line from top,    10th word from left,  5th word from right : some
&664f 3c 86 fd ; &47 : page 22, 5th line from top,     1st word from left,  3rd word from right : mushroom
&6652 54 64 6e ; &48 : page 22, 4th line from bottom,  3rd word from left,  3rd word from right : revulsion
&6655 c5 c4 4b ; &49 : page 22, 3rd line from bottom,  6th word from left,  7th word from right : fired
&6658 b7 73 75 ; &4a : page 22, 1st line from bottom, 11th word from left,  1st word from right : moved
&665b 01       ; move 1 page to page 23
&665c b8 92 6b ; &4b : page 23, 1st line from top,     9th word from left,  3rd word from right : ordered
&665f a9 56 3d ; &4c : page 23, 2nd line from top,     6th word from left,  5th word from right : left
&6662 ea d2 61 ; &4d : page 23, 3rd line from top,     5th word from left, 10th word from right : stopped
&6665 2d d3 24 ; &4e : page 23, 6th line from top,     2nd word from left,  1st word from right : microscope
&6668 bf 7c 7b ; &4f : page 23, 8th line from top,     5th word from left,  7th word from right : Captain
&666b e0 f1 ed ; &50 : page 23, 8th line from bottom,  9th word from left,  6th word from right : before
&666e e3 b2 d1 ; &51 : page 23, 5th line from bottom, 12th word from left,  3rd word from right : when
&6671 e5 41 7b ; &52 : page 23, 3rd line from bottom, 14th word from left,  1st word from right : a
&6674 a6 ec 36 ; &53 : page 23, 2nd line from bottom,  3rd word from left,  8th word from right : on
&6677 01       ; move 1 page to page 24
&6678 a8 26 6d ; &54 : page 24, 1st line from top,     3rd word from left,  8th word from right : the
&667b 99 ff c3 ; &55 : page 24, 2nd line from top,     5th word from left,  5th word from right : been
&667e 40 a3 4c ; &56 : page 24, 8th line from bottom,  1st word from left,  4th word from right : outnumbered
&6681 d4 bc 66 ; &57 : page 24, 4th line from bottom,  7th word from left,  7th word from right : quickly
&6684 d6 60 2f ; &58 : page 24, 2nd line from bottom,  9th word from left,  5th word from right : temple
&6687 27 33 1c ; &59 : page 24, 1st line from bottom,  2nd word from left,  1st word from right : icer
&668a 01       ; move 1 page to page 25
&668b b8 5e 2b ; &5a : page 25, 1st line from top,     3rd word from left,  9th word from right : hollered
&668e d9 81 08 ; &5b : page 25, 2nd line from top,     7th word from left,  7th word from right : leaving
&6691 3a ce ec ; &5c : page 25, 3rd line from top,     2nd word from left,  2nd word from right : with
&6694 bb 26 64 ; &5d : page 25, 4th line from top,     6th word from left,  6th word from right : the
&6697 53 76 7b ; &5e : page 25, 5th line from bottom,  1st word from left,  5th word from right : imagination
&669a 01       ; move 1 page to page 26
&669b 01       ; move 1 page to page 27
&669c b8 d0 cc ; &5f : page 27, 1st line from top,     8th word from left,  4th word from right : an
&669f dc 93 01 ; &60 : page 27, 5th line from top,    13th word from left,  1st word from right : should
&66a2 6d e8 49 ; &61 : page 27, 6th line from top,     6th word from left,  1st word from right : die
&66a5 7f ef 91 ; &62 : page 27, 8th line from top,     7th word from left,  1st word from right : reports
&66a8 f4 09 6e ; &63 : page 27, 4th line from bottom,  3rd word from left, 13th word from right : for
&66ab c5 ed 58 ; &64 : page 27, 3rd line from bottom, 10th word from left,  3rd word from right : volumes
&66ae 27 3b 75 ; &65 : page 27, 1st line from bottom,  2nd word from left,  1st word from right : Commander
&66b1 01       ; move 1 page to page 28
&66b2 59 4b 45 ; &66 : page 28, 2nd line from top,     1st word from left,  5th word from right : baffled
&66b5 ba 2e 40 ; &67 : page 28, 3rd line from top,     7th word from left,  5th word from right : seismological
&66b8 ec 18 fd ; &68 : page 28, 5th line from top,     5th word from left, 10th word from right : bottom
&66bb 9d e2 48 ; &69 : page 28, 6th line from top,     5th word from left,  5th word from right : ago
&66be 01       ; move 1 page to page 29
&66bf e6 5a 4a ; &6a : page 29, 2nd line from bottom,  7th word from left,  8th word from right : molecules
&66c2 97 30 74 ; &6b : page 29, 1st line from bottom,  7th word from left,  3rd word from right : seconds
&66c5 02       ; move 2 pages to page 31
&66c6 32 6b 4d ; &6c : page 31, 6th line from bottom,  3rd word from left,  1st word from right : beautiful
&66c9 25 10 e2 ; &6d : page 31, 3rd line from bottom,  2nd word from left,  1st word from right : cave
&66cc 66 06 54 ; &6e : page 31, 2nd line from bottom,  1st word from left,  6th word from right : His
&66cf 57 97 0a ; &6f : page 31, 1st line from bottom,  3rd word from left,  3rd word from right : creation
&66d2 01       ; move 1 page to page 32
&66d3 48 57 7f ; &70 : page 32, 1st line from top,     1st word from left,  4th word from right : You
&66d6 ca 49 50 ; &71 : page 32, 3rd line from top,     7th word from left,  6th word from right : I
&66d9 5b a7 12 ; &72 : page 32, 4th line from top,     4th word from left,  2nd word from right : this
&66dc 6e 86 f8 ; &73 : page 32, 7th line from top,     1st word from left,  6th word from right : shouting
&66df 01       ; move 1 page to page 33
&66e0 b9 d8 e2 ; &74 : page 33, 2nd line from top,    10th word from left,  2nd word from right : search
&66e3 ac d5 12 ; &75 : page 33, 5th line from top,     3rd word from left,  8th word from right : he
&66e6 dd da fe ; &76 : page 33, 6th line from top,     1st word from left, 13th word from right : Sprake
&66e9 25 83 68 ; &77 : page 33, 3rd line from bottom,  2nd word from left,  1st word from right : sleep
&66ec b7 10 7a ; &78 : page 33, 1st line from bottom,  8th word from left,  4th word from right : found
&66ef 01       ; move 1 page to page 34
&66f0 62 68 57 ; &79 : page 34, 6th line from bottom,  5th word from left,  2nd word from right : Tessara
&66f3 a3 da f2 ; &7a : page 34, 5th line from bottom,  2nd word from left,  9th word from right : Sprake
&66f6 d6 6e 17 ; &7b : page 34, 2nd line from bottom,  2nd word from left, 12th word from right : mind
&66f9 01       ; move 1 page to page 35
&66fa d8 61 1d ; &7c : page 35, 1st line from top,    12th word from left,  2nd word from right : like
&66fd ba 64 f4 ; &7d : page 35, 3rd line from top,     5th word from left,  7th word from right : Pahn
&6700 7b b5 7d ; &7e : page 35, 4th line from top,     7th word from left,  1st word from right : spreading
&6703 c0 80 00 ; &7f : page 35, 8th line from bottom,  5th word from left,  8th word from right : speaks
&6706 51 e6 71 ; &80 : page 35, 7th line from bottom,  3rd word from left,  3rd word from right : it
&6709 e2 dd 93 ; &81 : page 35, 6th line from bottom,  5th word from left, 10th word from right : go
&670c 43 99 84 ; &82 : page 35, 5th line from bottom,  4th word from left,  1st word from right : them
&670f d6 73 66 ; &83 : page 35, 2nd line from bottom, 10th word from left,  4th word from right : mode
&6712 47 4e 2c ; &84 : page 35, 1st line from bottom,  4th word from left,  1st word from right : careful
&6715 01       ; move 1 page to page 36
&6716 39 5c 7c ; &85 : page 36, 2nd line from top,     1st word from left,  3rd word from right : dripping
&6719 cb 6b 0a ; &86 : page 36, 4th line from top,    12th word from left,  1st word from right : pitch
&671c 5d 2f 92 ; &87 : page 36, 6th line from top,     3rd word from left,  3rd word from right : hard
&671f 50 cc 3a ; &88 : page 36, 8th line from bottom,  5th word from left,  1st word from right : voice
&6722 33 3c 3b ; &89 : page 36, 5th line from bottom,  1st word from left,  3rd word from right : who
&6725 37 85 9e ; &8a : page 36, 1st line from bottom,  2nd word from left,  2nd word from right : insane
&6728 01       ; move 1 page to page 37
&6729 e8 6b 4f ; &8b : page 37, 1st line from top,    13th word from left,  2nd word from right : eyes
&672c 29 02 a4 ; &8c : page 37, 2nd line from top,     2nd word from left,  1st word from right : screamed
&672f 4f cc a4 ; &8d : page 37, 8th line from top,     4th word from left,  1st word from right : soul
&6732 c2 42 7e ; &8e : page 37, 6th line from bottom, 11th word from left,  2nd word from right : away
&6735 75 1d 12 ; &8f : page 37, 3rd line from bottom,  3rd word from left,  5th word from right : would
&6738 67 61 88 ; &90 : page 37, 1st line from bottom,  4th word from left,  3rd word from right : airlocks
&673b 01       ; move 1 page to page 38
&673c 5c 0f 15 ; &91 : page 38, 5th line from top,     3rd word from left,  3rd word from right : imp
&673f 30 eb 89 ; &92 : page 38, 8th line from bottom,  2nd word from left,  2nd word from right : no
&6742 22 45 3f ; &93 : page 38, 6th line from bottom,  1st word from left,  2nd word from right : Sun
&6745 c3 20 3b ; &94 : page 38, 5th line from bottom,  8th word from left,  5th word from right : its
&6748 65 dd 71 ; &95 : page 38, 3rd line from bottom,  4th word from left,  3rd word from right : anything
&674b 01       ; move 1 page to page 39
&674c d8 65 62 ; &96 : page 39, 1st line from top,     5th word from left,  9th word from right : from
&674f 69 42 2d ; &97 : page 39, 2nd line from top,     4th word from left,  3rd word from right : input
&6752 ea fa 20 ; &98 : page 39, 3rd line from top,     3rd word from left, 12th word from right : few
&6755 cd 34 79 ; &99 : page 39, 6th line from top,     2nd word from left, 11th word from right : gathered
&6758 a5 e7 3c ; &9a : page 39, 3rd line from bottom,  4th word from left,  7th word from right : had
&675b 96 d5 7c ; &9b : page 39, 2nd line from bottom,  1st word from left,  9th word from right : He
&675e 01       ; move 1 page to page 40
&675f 29 81 38 ; &9c : page 40, 2nd line from top,     2nd word from left,  1st word from right : Spiegel
&6762 5a d7 80 ; &9d : page 40, 3rd line from top,     2nd word from left,  4th word from right : do
&6765 8b 86 03 ; &9e : page 40, 4th line from top,     4th word from left,  5th word from right : replied
&6768 c2 62 87 ; &9f : page 40, 6th line from bottom, 11th word from left,  2nd word from right : kill
&676b 77 1f 10 ; &a0 : page 40, 1st line from bottom,  1st word from left,  7th word from right : eyelids
&676e 02       ; move 2 pages to page 42
&676f 8b 6b 65 ; &a1 : page 42, 4th line from top,     4th word from left,  5th word from right : eyes
&6772 ef de 18 ; &a2 : page 42, 8th line from top,     4th word from left, 11th word from right : pouring
&6775 b1 57 a1 ; &a3 : page 42, 7th line from bottom,  8th word from left,  4th word from right : animal
&6778 c2 d3 51 ; &a4 : page 42, 6th line from bottom,  3rd word from left, 10th word from right : chillingly
&677b 24 10 aa ; &a5 : page 42, 4th line from bottom,  1st word from left,  2nd word from right : cave
&677e 47 7a f7 ; &a6 : page 42, 1st line from bottom,  4th word from left,  1st word from right : separate
&6781 01       ; move 1 page to page 43
&6782 4b 33 62 ; &a7 : page 43, 4th line from top,     4th word from left,  1st word from right : icer
&6785 dc 35 03 ; &a8 : page 43, 5th line from top,     2nd word from left, 12th word from right : stepped
&6788 83 49 08 ; &a9 : page 43, 5th line from bottom,  4th word from left,  5th word from right : I
&678b 94 b0 b8 ; &aa : page 43, 4th line from bottom,  4th word from left,  6th word from right : What
&678e 57 b7 98 ; &ab : page 43, 1st line from bottom,  3rd word from left,  3rd word from right : were
&6791 01       ; move 1 page to page 44
&6792 9a 99 aa ; &ac : page 44, 3rd line from top,     9th word from left,  1st word from right : them
&6795 7b 26 14 ; &ad : page 44, 4th line from top,     2nd word from left,  6th word from right : the
&6798 60 b8 13 ; &ae : page 44, 8th line from bottom,  5th word from left,  2nd word from right : years
&679b 03       ; move 3 pages to page 47
&679c ec f7 a2 ; &af : page 47, 5th line from top,    13th word from left,  2nd word from right : to
&679f ce 8d 08 ; &b0 : page 47, 7th line from top,     7th word from left,  6th word from right : shouted
&67a2 71 e4 5c ; &b1 : page 47, 7th line from bottom,  2nd word from left,  6th word from right : of
&67a5 72 79 b0 ; &b2 : page 47, 6th line from bottom,  3rd word from left,  5th word from right : into
&67a8 73 da bb ; &b3 : page 47, 5th line from bottom,  4th word from left,  4th word from right : Sprake
&67ab 97 17 4f ; &b4 : page 47, 1st line from bottom,  3rd word from left,  7th word from right : leered
&67ae 01       ; move 1 page to page 48
&67af 88 21 9c ; &b5 : page 48, 1st line from top,     1st word from left,  8th word from right : Activate
&67b2 2b 55 01 ; &b6 : page 48, 4th line from top,     2nd word from left,  1st word from right : being
&67b5 43 0c 56 ; &b7 : page 48, 5th line from bottom,  4th word from left,  1st word from right : anyway
&67b8 84 06 02 ; &b8 : page 48, 4th line from bottom,  5th word from left,  4th word from right : his
&67bb 02       ; move 2 pages to page 50
&67bc b9 79 bb ; &b9 : page 50, 2nd line from top,     3rd word from left,  9th word from right : into
&67bf 2a 86 b5 ; &ba : page 50, 3rd line from top,     1st word from left,  2nd word from right : Lord
&67c2 a2 df 59 ; &bb : page 50, 6th line from bottom,  9th word from left,  2nd word from right : me
&67c5 a5 e5 0b ; &bc : page 50, 3rd line from bottom,  2nd word from left,  9th word from right : and
&67c8 01       ; move 1 page to page 51
&67c9 48 f0 1f ; &bd : page 51, 1st line from top,     1st word from left,  4th word from right : machine
&67cc ad f5 1e ; &be : page 51, 6th line from top,     8th word from left,  3rd word from right : lab
&67cf e2 d0 ac ; &bf : page 51, 6th line from bottom,  7th word from left,  8th word from right : an
&67d2 c4 4f 30 ; &c0 : page 51, 4th line from bottom,  5th word from left,  8th word from right : door
&67d5 02       ; move 2 pages to page 53
&67d6 c8 81 12 ; &c1 : page 53, 1st line from top,     1st word from left, 12th word from right : Leaving
&67d9 6b f7 cf ; &c2 : page 53, 4th line from top,     4th word from left,  3rd word from right : to
&67dc 67 40 c7 ; &c3 : page 53, 1st line from bottom,  6th word from left,  1st word from right : Fire
&67df 01       ; move 1 page to page 54
&67e0 69 f0 2c ; &c4 : page 54, 2nd line from top,     4th word from left,  3rd word from right : slowly
&67e3 eb 68 66 ; &c5 : page 54, 4th line from top,    12th word from left,  3rd word from right : lucky
&67e6 26 39 0f ; &c6 : page 54, 2nd line from bottom,  1st word from left,  2nd word from right : own
&67e9 a7 81 25 ; &c7 : page 54, 1st line from bottom,  6th word from left,  5th word from right : long
&67ec 00       ; end of list

; relocate_binary_and_saved_position
&78ed 78       SEI
&78ee d8       CLD
&78ef a9 7f    LDA #&7f
&78f1 8d 4e fe STA &fe4e ; System VIA interrupt enable register     # Disable all System VIA interrupts
&78f4 8d 6e fe STA &fe6e ; User VIA interrupt enable register       # Disable all User VIA interrupts
&78f7 8d 4d fe STA &fe4d ; System VIA interrupt flag register       # Clear all System VIA interrupts
&78fa 8d 6d fe STA &fe6d ; User VIA interrupt flag register         # Clear all User VIA interrupts
&78fd a9 00    LDA #&00
&78ff a2 df    LDX #&df
; wipe_0001_to_00df_loop
&7901 95 00    STA &00,X                                            # Wipe &0001 - &00df loop
&7903 ca       DEX
&7904 d0 fb    BNE &7901 ; wipe_0001_to_00df_loop
&7906 a9 28    LDA #&28 ; 5 tiles
&7908 85 35    STA &35 ; acceleration_power
&790a a9 c0    LDA #&c0 ; upright
&790c 85 de    STA &de ; player_angle
&790e c6 dd    DEC &dd ; player_object_held
&7910 a2 ff    LDX #&ff
&7912 9a       TXS
&7913 ad 96 02 LDA &0296 ; os_system_clock + 4
&7916 29 57    AND #&57 ; .4.1.421
&7918 85 01    STA &01 ; rnd
; move_game_state
&791a a9 82    LDA #&82 ; &fc82 = &10000 - &37e
&791c 85 02    STA &02 ; size_low
&791e a9 fc    LDA #&fc
&7920 85 03    STA &03 ; size_high
&7922 a0 ff    LDY #&ff
; move_game_state_loop                                              # Move &0400 - &077d to &18f8 - &1c75
&7924 b9 7e 06 LDA &067e,Y                                          # Will be moved again to &07f8 - &0b75 at &793e
#     actually LDA game_state_source_address,Y
&7927 99 76 1b STA &1b76,Y
#     actually STA game_state_target_address,Y
&792a 88       DEY
&792b c0 ff    CPY #&ff
&792d d0 07    BNE &7936 ; skip_page
&792f 18       CLC
&7930 ce 26 79 DEC &7926 ; game_state_source_address_high
&7933 ce 29 79 DEC &7929 ; game_state_target_address_high
; skip_page
&7936 e6 02    INC &02 ; size_low
&7938 d0 ea    BNE &7924 ; move_game_state
&793a e6 03    INC &03 ; size_high
&793c d0 e6    BNE &7924 ; move_game_state
; move_1200_to_78ec_loop                                            # Move &1200 - &78ec to &0100 - &67ec
&793e ad 00 12 LDA &1200
#     actually LDA source_address
&7941 8d 00 01 STA &0100
#     actually STA target_address
&7944 ee 3f 79 INC &793f ; source_address_low
&7947 d0 03    BNE &794c ; skip_page_source
&7949 ee 40 79 INC &7940 ; source_address_high
; skip_page_source
&794c ee 42 79 INC &7942 ; target_address_low
&794f d0 03    BNE &7954 ; skip_page_target
&7951 ee 43 79 INC &7943 ; target_address_high
; skip_page_target
&7954 ad 3f 79 LDA &793f ; source_address_low
&7957 c9 ed    CMP #&ed ; &78ed
&7959 d0 e3    BNE &793e ; move_1200_to_78ec_loop
&795b ad 40 79 LDA &7940 ; source_address_high
&795e c9 78    CMP #&78
&7960 d0 dc    BNE &793e ; move_1200_to_78ec_loop
&7962 a9 ff    LDA #&ff                                             # Set all bits as output
&7964 8d 43 fe STA &fe43 ; System VIA data direction register A
&7967 a2 03    LDX #&03
; silence_sound_channels_loop
&7969 bd 7a 79 LDA &797a,X ; sound_channels_set_volume_to_zero_byte_table
&796c 20 e4 13 JSR &13e4 ; write_byte_to_sound_chip
&796f ca       DEX
&7970 10 f7    BPL &7969 ; silence_sound_channels_loop
&7972 a5 01    LDA &01 ; rnd
&7974 8d 9c 49 STA &499c ; copy_protection_seed
&7977 4c 0c 60 JMP &600c ; entry_point_after_relocation

; sound_channels_set_volume_to_zero_byte_table
;      0  1  2  3
&797a ff df bf 9f

; unused
&797e 4c 00 30 JMP &3000

; unused
&7981 2c

; leave_to_relocate_binary_and_saved_position
&7982 68       PLA
&7982 aa       TAX
&7984 68       PLA
&7985 49 01    EOR #&01 ; &0146 = &79aa ^ &78ec
&7987 48       PHA
&7988 8a       TXA
&7989 49 46    EOR #&46
&798b 48       PHA
&798c 60       RTS                                                  # Leaves to &78ed ; relocate_binary_and_saved_position

; unused
&798d 0f

; decrypt_byte
&798e 49 f2    EOR #&f2
#     becomes  BNE &7982 ; leave_to_relocate_binary_and_saved_position when &798e is decrypted
&7990 65 70    ADC &70 ; eor
&7992 69 97    ADC #&97
&7994 85 70    STA &70 ; eor
&7996 59 00 12 EOR &1200,Y
#     actually EOR source_address,Y
&7999 99 00 12 STA &1200,Y
#     actually STA target_address,Y
&799c 45 70    EOR &70 ; eor
&799e c8       INY
&799f d0 07    BNE &79a8 ; next_byte
&79a1 ee 98 79 INC &7998 ; source_address_high
&79a4 ee 9b 79 INC &799b ; target_address_high
&79a7 60       RTS
; next_byte
&79a8 20 8e 79 JSR &798e ; decrypt_byte
&79ab d0 e1    BNE &798e ; decrypt_byte

; entry_point
&79ad a9 23    LDA #&23
&79af 85 70    STA &70 ; eor
&79b1 a9 78    LDA #&78
&79b3 a0 00    LDY #&00
&79b5 38       SEC
&79b6 78       SEI
&79b7 4c 8e 79 JMP &798e ; decrypt_byte

