After the random Faxanadu factoids of the last updates it's about time to make some more structured updates again: It's time to begin a new series of updates about a certain topic. And it's going to be quite a large series, so large in fact that I can't yet estimate how many parts this new series will contain. Anyway, the topic of this new series of updates is the collision detection code of Faxanadu. What exactly is going to be part of the next updates will become more clear after you've finished reading this update as the topic of this update is the main collision detection function from where all other functions dealing with collision detection are called.
Nearly all collision detection code is stored in ROM bank 14. The only exceptions are some calls to functions located in the Kernel bank and some meta-data and graphical data that must be loaded from other banks. The already mentioned main function of the collision detection code starts right at offset $8000, ends at offset $806D and is basically mainly a loop that calls the functions that perform the actual collision detection. For this reason it's a very good way to get an idea about what's going on under the hood. Let's get to the code.
CODE:
<br />$8000-> AD 26 4: LDA $426 ; Elixir flag
<br />$8003-> D0 A: BNE $800F
<br />$8005-> AD 2A 4: LDA $42A ; Hour glass flag
<br />$8008-> C9 FF: CMP #$FF
<br />$800A-> D0 3: BNE $800F
<br />$800C-> EE 83 3: INC $383 ; Update counter
<br />
At the very beginning of the main function two conditions for freezing all sprites on the screen are checked. Offset $426 contains a flag that indicates whether or not the user just used his elixir (used to restore the player's full health). People who know Faxanadu probably know that using the elixir freezes all sprites (including the player himself) until all health points are restored. The other check tests whether the hour glass (an item to freeze all enemies temporarily) is active. If it's not active then offset $42A will contain the value $FF, otherwise the remaining time of the hour glass is stored there.
If neither of these two items are active the value at offset $383 is increased. This value is used as some kind of timer when moving sprites.
Afterwards the main loop of the main function begins. All sprites on the screen are moved, updated and tested for all kinds of collisions with the player, magic, blocks and other things. There's a fixed maximum of 8 sprites per screen. This becomes obvious when looking at how information about active sprites is stored in memory. All data about the active sprites is stored in lots of arrays of size 8. The first of these arrays is located between offset $2CC and offset $2D3, it contains the sprite ID of each of the sprites on the screen. If less than 8 sprites are active the sprite ID of these inactive sprites is set to $FF and they are skipped in the main loop. The value at offset $378 is used to keep track on which sprite is in the process of being updated.
CODE:
<br />$800F-> A2 7: LDX #$7 ; Max sprite number
<br />$8011-> 8E 78 3: STX $378
<br />$8014-> BD CC 2: LDA $2CC,X ; Sprite IDs
<br />$8017-> 30 2A: BMI $8043
<br />
Afterwards it's once again checked if the elixir is active and if this is in fact the case nearly all of the loop is skipped over. Then the second of the sprite data arrays ($34C - $353) is used the first time. This array contains timers that are activated when the user hits an enemy. In that case normal sprite movement isn't used to update the sprite anymore, succesfully hitting an enemy is shown to the user by making the enemy sprite flicker on the screen for a very brief period of time. If an enemy is still supposed to flicker (e.g. his flicker timer is still not zero) the jump is taken.
CODE:
<br />$8019-> AD 26 4: LDA $426 ; Elixir flag
<br />$801C-> D0 22: BNE $8040
<br />$801E-> BD 4C 3: LDA $34C,X ; Flicker counter
<br />$8021-> D0 2B: BNE $804E
<br />
On with the next sprite data array ($334 - 34B). If the player hits an enemy with magic the ID of the magic used is stored in this array. This is necessary because certain kinds of magic force the enemy some pixels away from the player. This is also exactly what happens in the function at $8151 which is called afterwards.
CODE:
<br />$8023-> BD 34 3: LDA $334,X ; Magic flag
<br />$8026-> 30 6: BMI $802E
<br />$8028-> 20 51 81: JSR $8151 ; Handle hits with magic
<br />$802B-> 4C 40 80: JMP $8040
<br />
Now comes the main part of loop. In the following lines of code all the major functions responsible for sprite movement are called. In the first function the sprite behaves according to it's behaviour specification data. The second function determines whether the player hit an enemy with his weapon, the third loop checks if the player hit an enemy with magic. The fourth loop calculates some sprite size information necessary for the next two functions which determine whether the player was hit by enemy magic or if the player touched another sprite. Like in the first function the last function is also about making the sprite behave in a certain way. More on all of that will come in later updates.
CODE:
<br />$802E-> 20 6B A6: JSR $A66B ; Move sprite
<br />$8031-> 20 4 88: JSR $8804 ; Hit by weapon
<br />$8034-> 20 DC 8A: JSR $8ADC ; Hit by magic
<br />$8037-> 20 8 8A: JSR $8A08 ; Get sprite size
<br />$803A-> 20 7C 87: JSR $877C ; Player was hit by magic
<br />$803D-> 20 A 89: JSR $890A ; Player touched a sprite
<br />$8040-> 20 D2 8B: JSR $8BD2 ; Update sprites
<br />
The last part of the loop decreases the number of the sprite to update and restarts the loop if necessary.
CODE:
<br />$8043-> AE 78 3: LDX $378
<br />$8046-> CA: DEX ; Next sprite
<br />$8047-> 10 C8: BPL $8011
<br />$8049-> A9 0: LDA #$0
<br />$804B-> 85 33: STA $33
<br />$804D-> 60: RTS
<br />
After the main loop there's some code left. This code is responsible for updating a sprite after it was hit (the flickering effect). The flicker counter is decreased and the sprite is horizontally moved by two 2 pixels (that's what the function at the 2nd to last line does; it takes parameters in $374 and $375). Unless the sprite was one of three specifically specified sprites which never move anywhere because they're locked at the position where they are placed. If you wonder where exactly the flickering effect comes from, it's calculated from the value of the flicker counter when the sprite is redrawn.
CODE:
<br />$804E-> DE 4C 3: DEC $34C,X ; Flicker counter
<br />$8051-> BD CC 2: LDA $2CC,X ; Sprite ID
<br />$8054-> C9 2C: CMP #$2C
<br />$8056-> F0 E8: BEQ $8040
<br />$8058-> C9 30: CMP #$30
<br />$805A-> F0 E4: BEQ $8040
<br />$805C-> C9 31: CMP #$31
<br />$805E-> F0 E0: BEQ $8040
<br />$8060-> A9 2: LDA #$2
<br />$8062-> 8D 75 3: STA $375
<br />$8065-> A9 0: LDA #$0
<br />$8067-> 8D 74 3: STA $374
<br />$806A-> 20 94 84: JSR $8494 ; Move sprite
<br />$806D-> 4C 40 80: JMP $8040
<br />