| Routines

6670: Update the sniper's sprite tile references

Used by the routine at 621E. Updates the sprite tile references for animatory states 0x36/0xB6. On entry, the carry flag is set if the sniper is firing, in which case a new bullet will be initialised.
Input
A | Sniper's animatory state (0x36/0xB6)
D | Sniper's next y-coordinate (34-37)
E | Sniper's x-coordinate
H | 0xE3 (sniper)
6670 | PUSH AF | Save the sniper's animatory state
6671 | LD A,D | A=sniper's next y-coordinate (34-37)
6672 | EXX |
6673 | PUSH AF | Save the sniper's next y-coordinate
6674 | LD C,$03 | There are three columns in the sniper's sprite
6676 | SUB $21 | E'=1 (sniper is at full height), 2 (sniper is at half-height), 3 (only the sniper's head is visible), or 4 (the sniper has ducked out of sight)
6678 | LD E,A
6679 | CPL | D'=5-E' (number of rows of the sniper's sprite that will be filled with non-blank tiles)
667A | ADD A,$06
667C | LD D,A
The next section of code modifies the sprite tile references for animatory states 0x36/0xB6 column by column.
667D | LD H,$D7 | 0xD7 is the base page for sprite tile references
667F | LD B,D | B=number of non-blank tiles to initialise in this column
6680 | LD L,$A2 | Animatory state 0xA2 (gangster) will be used as a template for the sniper's sprite
6682 | LD A,(HL) | Collect a sprite tile reference from the template
6683 | LD L,$B6 | Copy the sprite tile reference into place
6685 | LD (HL),A
6686 | INC H | Move down a row
6687 | DJNZ $6680 | Jump back until all non-blank rows in this column have been done
6689 | LD A,E | A=number of blank rows in the sniper's sprite (1-4)
668A | AND A | Are there any?
668B | JR Z,$6693 | Jump if not (this jump is never made)
668D | LD B,E | Fill the remaining slots in this column with blank tiles
668E | LD (HL),$00
6690 | INC H
6691 | DJNZ $668E
6693 | DEC C | Have we done all three columns yet?
6694 | JR NZ,$667F | Jump back if not
6696 | POP AF | Restore the sniper's next y-coordinate (34-37) to A
6697 | JR NC,$66D3 | Jump unless the sniper is firing
The sniper is firing, so some further modifications to the sniper's sprite are required.
6699 | LD H,$D9 | Set the reference for tile 2 to 0xEA
669B | LD (HL),$EA
669D | INC H | Set the reference for tile 3 to 0x00 (blank tile)
669E | LD (HL),$00
66A0 | LD H,$DE | Set the reference for tile 7 to 0xEB
66A2 | LD (HL),$EB
Now we initialise a bullet.
66A4 | LD HL,$7FC2 | Point HL' at the third byte of bullet buffer 1 (at 7FC0)
66A7 | SUB $12 | D=16 or 17 (bullet's initial screen y-coordinate)
66A9 | LD D,A
66AA | LD A,(HL) | Is bullet buffer 1 at 7FC0 already being used?
66AB | AND A
66AC | JR Z,$66B0 | Jump if not
66AE | LD L,$C6 | Point HL' at the third byte of bullet buffer 2
66B0 | LD (HL),D | Store the bullet's initial screen y-coordinate
66B1 | LD A,($7FFE) | A=x-coordinate of the leftmost column of the play area on screen
66B4 | LD B,A | Copy this to B'
66B5 | INC L | Point HL' at the fourth byte of the bullet buffer
66B6 | CALL $F17F | Get a random number in A
66B9 | AND $70 | Keep only bits 4-6
66BB | ADD A,$04 | The initial pixel row at which the bullet will be drawn inside a cell is 4
66BD | LD (HL),A | Initialise bits 0-6 of the fourth byte of the bullet buffer
66BE | POP AF | Restore the sniper's animatory state to A
66BF | PUSH AF | Save the animatory state again
66C0 | AND $80 | Keep only bit 7 (the direction bit)
66C2 | OR (HL) | Initialise bit 7 of the fourth byte of the bullet buffer (the bullet's direction bit)
66C3 | LD (HL),A
66C4 | DEC L | Store the x-coordinate of the leftmost column of the play area on screen in the first byte of the bullet buffer
66C5 | DEC L
66C6 | DEC L
66C7 | LD (HL),B
66C8 | INC L | Point HL' at the second byte of the bullet buffer
66C9 | EXX |
66CA | RLCA | Set the carry flag if the sniper is facing right
66CB | LD A,E | A=sniper's x-coordinate
66CC | EXX |
66CD | JR NC,$66D1 | Jump if the sniper is facing left
66CF | INC A | Add 2 to the sniper's x-coordinate
66D0 | INC A
66D1 | SUB B | Subtract the x-coordinate of the leftmost column of the play area on screen to obtain the bullet's initial screen x-coordinate
66D2 | LD (HL),A | Store this in the second byte of the bullet buffer
66D3 | EXX |
66D4 | POP AF | Restore the sniper's animatory state to A
66D5 | JP $E9D5 | Update the sniper's animatory state and location and update the SRB