;
* THIS IS D:TANKVBI.SRC
VBI
	CLD			;PRECAUTION
* SET UP DLI VECTOR & ENABLE
	LDA	#DLI
	STA	VDSLST
	LDA	#<DLI
	STA	VDSLST+1
	LDA	#$C0
	STA	NMIEN
	LDA	ENABLE		;GAME/PAUSE FLAG
	BNE	PLAY		;IF ON THEN OK
	JMP	XVBI		;ELSE GO TO EXIT
PLAY
	LDA	#$70
	STA	ATRACT		;DEFEAT ATTRACT MODE
	LDX	#$01
AE1
	LDA	IMMUNE,X
	BEQ	AE2
	DEC	IMMUNE,X	;COUNTDOWN IMMUNITY TIMER IF ON
AE2
	DEX
	BPL	AE1
HITCK
* COLLISION CHECK
	LDX	#$01
AF1
	STX	SAVEX
	LDA	MOPF,X		;MISSILE HIT WALL?
	BEQ	AF3		;SKIP IF NO COLLISION
* SET POSITION
	LDY	SHOTV,X		;GET SHOT VERTICAL POS.
	LDA	MISSILES,Y	;GET MISSILE DATA FROM SCREEN
	AND	SHTMSK,X	;MASK MISSILE OFF
	STA	MISSILES,Y	;ERASE SHOT
	LDA	#$00
	STA	SHOTF,X
	SEC
	LDA	SHOTV,X
	SBC	#$20		;-TOP
	LSR			;/2
	LSR	A
	LSR			;/8
	LDX	#$14		;20 CHRS PER ROW
	JSR	MULTIPLY	;GET OFSET FROM TOP OF SCREEN
	CLC			;ADD TO SCREEN TO GET ADDRESS OF SCREEN ROW
	LDA	RESULT
	ADC	#SCREEN
	STA	POTMP1
	LDA	RESULT+1
	ADC	#<SCREEN
	STA	POTMP2
	LDX	SAVEX		;GET BACK LOOP COUNTER
	SEC
	LDA	SHOTH,X
	SBC	#$30		;- LEFT EDGE OFFSET
	LSR			;/2
	LSR	A
	LSR			;/8 COLOR COLOR CLOCKS FOR CHR POS.
	TAY
	LDA	(POTMP1),Y
	AND	#$3F		;MASK OFF COLOR
	CMP	#$07		;BORDER?
	BCC	AF3		;NO EFFECT
	TAX
	DEX			;ASSUME A FRACTURE
	LSR			;EVEN OR ODD?
	BCS	AF2		;OK
	LDX	#$00		;IF ALREADY FRACTURED THEN ERASE
AF2
	TXA
	STA	(POTMP1),Y
AF3
	LDX	SAVEX
	DEX
	BPL	AF1
; CHECK IF TANK SHOT
	LDX	#$01		;TANK
	LDY	#$02		;TANK # TIMES 2 TO INDEX A TABLE OF 2
	STY	SAVEY		;BYTE ADDRESSES
AF4
	LDA	CYCLES,X	;TANK ALREADY HIT?
	ORA	IMMUNE,X	;OR HAVE TEMPORARY IMMUNITY?
	BEQ	AF42
	JMP	AF7		;DON'T BOTHER
AF42
	LDY	WHO,X		;GET OPPONENTS TANK
	LDA	MOPL,Y		;SHOT?
	AND	HITMASK,X	;MASK OFF SHOOTING SELF
	BNE	AF41
	LDA	CYCLES,Y	;OTHER PLAYER BLOWING UP
	BEQ	AF40		;NO.
	CMP	#$FF		;FIRST CYCLE?
	BNE	AF7		;NOT FIRST CYCLE, IGNORE DEBRIS COLLISION
AF40
	LDA	P2PL,X		;CRASHED?
	AND	HITMASK,Y	;MASK OFF SHOOTING SELF
	BEQ	AF7		;NO HIT
* FIND UP ZAP ARRAY AND DOWN ZAP ARRAY
* AND FILL WITH CURRENT VERTICAL POSITION
AF41
	SED
	CLC
	LDA	SCORE,Y		;UPDATE SCORE IN DECIMAL
	ADC	#$01
	STA	SCORE,Y
	CLD
* THIS SECTION INTIALIZES THE TABLES FOR DZAP AND UZAP
* TO THE TANK'S CURRENT VERTICAL POSITION
	LDY	SAVEY
	LDA	UZTAB,Y
	STA	POTMP1
	LDA	UZTAB+1,Y
	STA	POTMP2
	LDA	DZTAB,Y
	STA	POTMP3
	LDA	DZTAB+1,Y
	STA	POTMP4
	LDY	#$08
	LDA	PLAYRV,X	;TANK VERTICAL POS.
AF5
	STA	(POTMP1),Y	;UZAP TABLE
	STA	(POTMP3),Y	;DZAP TABLE
	DEY
	BNE	AF5
* IF 1ST PLAYER HIT ERASE 2ND PLAYERS SHOT & VICE VERSA
	LDY	WHO,X
	LDA	#$00
	STA	SHOTF,Y
	STA	SHOTH,Y
	STA	HPOSM0,Y
	LDA	SHOTV,Y
	TAY
	LDA	#$00
	STA	MISSILES,Y
* ERASE HIT TANK
	LDA	PLAYRV,X
	STA	POTMP1
	STA	POTMP3
	LDA	PLAYT,X
	STA	POTMP2
	LDA	PLAYT2,X
	STA	POTMP4
	LDA	#$00
	LDY	#$0F
AF6
	STA	(POTMP1),Y
	STA	(POTMP3),Y
	DEY
	BPL	AF6
	LDY	SAVEY
	LDA	#$FF
	STA	CYCLES,X
AF7
	DEC	SAVEY
	DEC	SAVEY
	DEX
	BMI	XHITCK
	JMP	AF4		;BRANCH OUT OF RANGE, JUMP USED
XHITCK
	STA	HITCLR		;CLEAR COLLISIONS
* UPDATE ANY TANK EXPLOSION
EXPLODE
	LDY	#$02
	STY	SAVEY
	LDX	#$01
	STX	SAVEX
AG1
	LDA	CYCLES,X
	BNE	AG10
	JMP	AG5		;BRANCH OUT OF RANGE, JUMP USED
AG10
	DEC	CYCLES,X
	BNE	AG11		;IF NOT LAST TIME THROUGH EXPLOSION CYCLE
	LDA	STARTH,X	;RESET TANK POS.
	STA	PLAYRH,X
	LDA	STARTV,X
	STA	PLAYRV,X
	LDA	#$FF		;SET IMMUNITY TIMER TO APROX. 4 SEC.
	STA	IMMUNE,X
	BNE	AG5		;ALWAYS.
AG11
	LDA	RANDOM		;PICK A RANDOM COLOR
	STA	COLPM2,X	;USE TO FLASH TANK COLOR (EXPLOSION COLOR)
* GET ADDRESSES OF UZAP AND DZAP TABLES AND PLACE ON PAGE
	LDA	UZTAB,Y		;0 FOR ACCESS
	STA	POTMP1
	LDA	UZTAB+1,Y
	STA	POTMP2
	LDA	DZTAB,Y
	STA	POTMP3
	LDA	DZTAB+1,Y
	STA	POTMP4
* POTMP1&2 HAVE ADDRESS OF UZAP ARRAY,POTMP3&4 HAVE ADDRESS
* SET POTMP5&6 TO ADDRESS OF PLAYER OF DZAP ARRAY.
	LDA	#$00
	STA	POTMP5
	LDA	PLAYT,X
	STA	POTMP6
	LDA	#$08
	STA	INDEX
AG2
	LDY	INDEX
	LDA	(POTMP1),Y	;GET VERTICAL POS. OF OLD LINE IN UZAP
	TAY			;USE TO INDEX INTO PLAYER RAM
	LDA	#$00
	STA	(POTMP5),Y	;ERASE OLD LINE
	LDY	INDEX
	LDA	(POTMP3),Y	;GET VERTICAL POS. OF OLD LINE IN DZAP
	TAY			;FIND OLD BYTE IN PLAYER RAM
	LDA	#$00
	STA	(POTMP5),Y	;ERASE OLD LINE
	LDY	INDEX
	SEC
	LDA	(POTMP1),Y	;GET V.POS. OF ELEMENT IN UZAP
	SBC	INDEX		;CALCULATE NEW POS.
	CMP	#$20		;TOO HIGH?
	BCC	AG3
	STA	(POTMP1),Y	;SAVE BACK IN UZAP ARRAY
	TAY			;USE TO INDEX INTO PLAYER RAM
	LDA	#$FF		;VALUE OF A LINE
	STA	(POTMP5),Y	;PUT NEW LINE ON
AG3
* DOWN WORKS THE SAME AS UP EXCEPT THAT THE INDEX IS ADDED TO THE
* ARRAY INSTEAD OF SUBTRACTED
	LDY	INDEX
	CLC
	LDA	(POTMP3),Y
	ADC	INDEX
	CMP	#$C0		;TOO LOW?
	BCS	AG4
	STA	(POTMP3),Y
	TAY
	LDA	#$FF
	STA	(POTMP5),Y	;DRAW NEW LINE
AG4
	DEC	INDEX
	BNE	AG2
AG5
	LDY	SAVEY
	DEY
	DEY
	DEX
	BMI	MOVSHELL
	JMP	AG1		;BRANCH OUT OF RANGE, JMP USED
* MOVE MISSILES
MOVSHELL
	LDX	#$01
AH1
	LDA	SHOTF,X		;SHOT IN PROGRESS?
	BEQ	AH3		;NO UPDATE
	LDY	SHOTV,X
	LDA	MISSILES,Y	;GET OLD MISSILE BYTE
	AND	SHTMSK,X	;MASK OFF SHOT KEEP A14Y OTHER SHOT ON
	STA	MISSILES,Y	;SAVE IT BACK
	LDY	SHOTD,X		;GET SHOT DIRECTION
	CLC
	LDA	SHOTH,X		;GET SHOT HORIZ. POS.
	ADC	HOFFS,Y		;CALCULATE NEW HORZ. POS.
* THE LINES HERE THAT ARE REMed OUT ARE NOT NEEDED AS LONG
* AS THERE IS A SCREEN BORDER FOR THE SHOT TO HIT
*	CMP	#$30		;LEFT EDGE
*	BCC	AH2
*	CMP	#$C8		;RIGHT EDGE
*	BCS	AH2
	STA	SHOTH,X		;SAVE NEW SHOT HORIZ. POS.
	STA	HPOSM0,X	;TELL ANTIC NEW H. POS.
	CLC
	LDA	SHOTV,X		;CALCULATE NEW VERT. POS.
	ADC	VOFFS,Y
*	CMP	#$20
*	BCC	AH2
*	CMP	#$B0
*	BCS	AH2
	STA	SHOTV,X		;SAVE IT BACK
	TAY			;USE TO INDEX INTO MISSILE RAM
	LDA	SHELLS,X	;DATA FOR SHOT I14AGE
	ORA	MISSILES,Y	;MERGE WITH ANY OTHER MISSILE DATA
	STA	MISSILES,Y	;PUT ON SCREEN
*	BNE	AH3		;ALWAYS
*AH2
*	LDA	#$00
*	STA	HPOSM0,X
*	STA	SHOTF,X
AH3
	DEX
	BPL	AH1
*
*
*
UPDATE
	LDA	RTCLOC
	AND	#$02
	BEQ	AJ1
	JMP	XTANK
AJ1
	LDA	RTCLOC
	AND	#$01
	STA	TANK		;EACH TANK IS HANDLED EVERY OTHER VBI
	TAX
	LDA	CYCLES,X	;DEAD??
	BEQ	AJ2
	JMP	XTANK
AJ2
	LDA	TURETF,X	;CHECK TURRET FLAG
	BNE	DOTURRET	;IF FLAG ON CONTINUE IN TURRET ROUTINE
	LDA	STRIG0,X	;CHECK IF BUTTON DOWN
	BEQ	DOTURRET	;BUTTON DOWN
	JMP	DOTANK
DOTURRET
	LDA	STRIG0,X	;IS BUTTON STILL HELD DOWN
	BEQ	ROTATE		;THEN KEEP ROTATING TURRET
* OTHERWISE BUTTON RELEASED-SHOOT
	LDA	#$00
	STA	TURETF,X	;RETURN TO NORMAL JOYSTICK MOVEMENT
	LDA	SHOTF,X		;SHOT IN PROGRESS?
	BNE	DOTANK		;NO SHOT GO TO TANK MODE
	LDA	TURETD,X
	STA	SHOTD,X		;SHOT DIRECTION=TURRET DIRECTION
	CMP	#$05		;SHOT COMING OUT RIGHT SIDE OF TANK?
* SEP SHOT POS. TO CENTER OF TANK
	LDA	PLAYRH,X
	BCS	AK1		;IF SHOT OUT OF LEFT SIDE OF TANK
	SEC
	SBC	#$02		;START SHOT OVER TO THE LEFT
AK1
	CLC
	ADC	#$04
	STA	SHOTH,X
	STA	HPOSM0,X
	CLC
	LDA	PLAYRV,X
	ADC	#$08
	STA	SHOTV,X
	TAY
	LDA	SHELLS,X
	ORA	MISSILES,Y
	STA	MISSILES,Y
	STA	SHOTF,X		;SET SHOT FLAG TO NON-ZERO
* SET FLAG FOR SHOT SOUND
	LDA	#$10
	STA	SHOTS,X
	JMP	XTANK		;LEAVE
* DRAW TURRET POSITION THIS FRAME
ROTATE
	LDA	#$01
	STA	TURETF,X	;SET TURRET MODE ON
	LDA	RTCLOC		;TURN TURRET EVERY 8TH JIFFY
	AND	#$04
	BNE	BK1
	CLC
	LDY	STICK0,X
	LDA	DOFFS,Y
	ADC	TURETD,X
	AND	#$07		;KEEP DIR 0-7
	STA	TURETD,X
BK1	LDA	PLAYRH,X
	STA	NEWH		;KEEP TEMP VALUES BECAUSE WE CAN'T CHANGE REAL
	LDA	PLAYRV,X	;-VALUES BACK IF NEW POS. INVALID
	STA	NEWV
	LDA	TURETD,X
	STA	NEWID
	LDA	PLAYRF,X	;GET CURRENT DIRECTION
	STA	NEWF		;TEMP VALUE
	STA	NEWD
	ASL			;INDEX INTO TANK SHAPES TABLE
	TAY
	LDA	TANKTAB,Y
	STA	POTMP1
	LDA	TANKTAB+1,Y
	STA	POTMP1+1	;WE NOW HAVE A PAGE 0 POINTER TO NEW
	JMP	MID		;ITANK IMAGE
DOTANK
	LDX	TANK		;INDEX TO WHICH TANK
	LDY	STICK0,X	;READ STICK
	LDA	PLAYRH,X
	STA	NEWH		;KEEP TENP VALUES BECAUSE WE CAN'T CHANCE-
	LDA	PLAYRV,X	;-REALVALUES BACK IF NEW POS. INVALID
	STA	NEWV
	LDA	TURETD,X
	STA	NEWID
	LDA	RTCLOC
	AND	#$1C		;SLOW DOWN ROTATION OF TANK
	PHP			;SAVE PROCESSOR STATUS
	LDA	PLAYRF,X	;GET CURRENT TANK FACING
	PLP			;GET BACK DELAY TIMER STATUS
	BNE	AK0		;LEAVE IF TO SOON TO ROTATE WITH OLD FACING
	CLC			;VALUE IN ACCUM.
	LDA	TURETD,X	;TURRET ROTATES WITH BODY OF TANK
	ADC	DOFFS,Y		;+1,0, OR -1
	AND	#$07		;KEEP TURRET DIRECTION 0-7
	STA	NEWID
	CLC
	LDA	PLAYRF,X	;GET CURRENT DIRECTION
	ADC	DOFFS,Y		;ROTATE LEFT OR RIGHT DEPENDING ON STICK
	AND	#$07		;KEEP DIR 0-7
AK0
	STA	NEWF		;TEMP VALUE
	STA	NEWD
	ASL			;INDEX INTO TANK SHAPES TABLE
	TAY
	LDA	TANKTAB,Y
	STA	POTMP1
	LDA	TANKTAB+1,Y
	STA	POTMP1+1	;WE NOW HAVE A PAGE 0 POINTER TO NEW TANK IMAGE
MID	LDA	STICK0,X
	AND	#$03		;KEEP UID BITS
	CMP	#$03		;NEUTRAL
	BEQ	DRTANK		;DRAW TANK THEN
	CMP	#$01		;DOWN BIT OFF?
	BNE	AL1		;NO.
	CLC
	LDA	NEWF
	ADC	#$04		;DIRECTION IS 180 DEC. OPPOSITE FACING
	AND	#$07		;KEEP DIR 0-7
	STA	NEWD
AL1
	LDY	NEWD
	CLC
	LDA	PLAYRH,X
	ADC	HOFFS,Y
	CMP	#$C8		;RIGHT EDGE
	BCS	XTANK
	CMP	#$30		;LEFT EDGE
	BCC	XTANK
	STA	NEWH
	CLC
	LDA	PLAYRV,X
	ADC	VOFFS,Y
	CMP	#$B0		;BOTTOM
	BCS	XTANK
	CMP	#$20		;TOP
	BCC	XTANK
	STA	NEWV
DRTANK
	JSR	LOOKAHEAD	;CHECK FOR LEGAL MOVE
	BCS	DRTURRET	;POSITION NO GOOD
	LDX	TANK
	LDA	NEWID
	STA	TURETD,X
	LDA	NEWF
	STA	PLAYRF,X
	LDA	NEWH
	STA	PLAYRH,X
	STA	HPOSP0,X	;TELL ANTIC NEW POS.
	LDA	NEWV
	STA	PLAYRV,X
	STA	POTMP3		;BUILD PLAYER ADDRESS ON PAGEO
	LDA	PLAYT,X
	STA	POTMP4
	LDY	#$0F		;16 ELEMENTS
AM2
	LDA	(POTMP1),Y	;GET A BYTE OF TANK DATA
	STA	(POTMP3),Y	;PUT IN PLAYER RAM
	DEY
	BPL	AM2		;TILL ALL BYTES ARE MOVED
;TANK DRAWN, NOW DO TURRET
DRTURRET
	LDX	TANK
	LDA	TURETD,X
	ASL
	TAY
	LDA	TURTAB,Y
	STA	POTMP1
	LDA	TURTAB+1,Y
	STA	POTMP2
	LDA	PLAYRV,X
	STA	POTMP3
	LDA	PLAYT2,X
	STA	POTMP4
	LDY	#$0F
AN1
	LDA	(POTMP1),Y	;GET TURRET DATA
	STA	(POTMP3),Y	;STORE IN PLAYER RAM
	DEY
	BPL	AN1
	LDA	PLAYRH,X	;GET HORIZ. POS.
	STA	HPOSP2,X	;TELL ANTIC
XTANK
;
SOUND
	LDY	#$02
	LDX	#$01
AP1
	LDA	SHOTS,X
	BEQ	AP2
	DEC	SHOTS,X
	LDA	SHOTS,X
	STA	AUDC3,Y
	LDA	#$20
	STA	AUDF3,Y
AP2
	DEY
	DEY
	DEX
	BPL	AP1
SOUND2
	LDX	#$01
	LDY	#$02
AR1
	LDA	CYCLES,X	;TANK DYING
	BEQ	AR2		;DO MOTOR SOUND IF NOT
	EOR	#$FF
	STA	AUDF1,Y
	EOR	#$FF
	LSR
	LSR
	LSR
	LSR
	LSR
	AND	#$0F		;KEEP VOLUME IN RANGE
	ORA	#$20		;SET DISTORTION
	STA	AUDC1,Y
	BNE	AR3		;ALWAYS. SKIP MOTOR SOUND
AR2
	LDA	$21		;SET DISTORTION, LOW VOLUME
	STA	AUDC1,Y
	LDA	IDLE,X		;ASSUME IDLE TONE
	STA	AUDF1,Y
	LDA	STICK0,X
	AND	#$03		;KEEP UP/DOWN BITS
	CMP	#$03		;MOVING?
	BEQ	AR3		;NO
	LDA	VROOM,X		;ENCREASE PITCH
	STA	AUDF1,Y
AR3
	DEY
	DEY
	DEX
	BPL	AR1
COUNTDOWN
	SED
	LDA	MINUTES
	ORA	SECONDS
	BEQ	XCOUNT		;LEAVE IF 0
	DEC	TIMER
	BNE	XCOUNT
	LDA	#60
	STA	TIMER		;60 VBLANKS PER SEC.
	SEC
	LDA	SECONDS
	SBC	#$01
	STA	SECONDS
	BNE	AS2
	LDA	#$59
	STA	SECONDS		;RESET # OF SECONDS TO 59 DEC.
	LDA	MINUTES		;CHECK IF MINUTES ALREADY AT 0
	BNE	AS1		;OK
	LDA	#$00
	STA	GAME		;FLAG GAME NOT IN PROGRESS
	STA	ENABLE		;SET VBI FLAG TO SKIP GAME ROUTINES
	BEQ	AS2		;ALWAYS
AS1
	SEC
	LDA	MINUTES
	SBC	#$01
	STA	MINUTES
AS2
	LDA	MINUTES
	ORA	#$10		;TURN DECIMAL INTO A CHARACTER
	STA	LINE3+8
	LDA	SECONDS
	LSR			;GET lo's
	LSR
	LSR
	LSR
	ORA	#$10		;MAKE A CHR
	STA	LINE3+10
	LDA	SECONDS
	AND	#$0F		;GET i's
	ORA	#$10
	STA	LINE3+11
XCOUNT
	CLD
	LDX	#$01
AT1
	LDY	SCPOS,X
	LDA	SCORE,X		;PRINT SCORE ON SCREEN
	LSR
	LSR
	LSR
	LSR
	ORA	CSHIFT,X	;MAKE CHR+COLOR#
	STA	LINE2,Y
	LDA	SCORE,X
	AND	#$0F
	ORA	CSHIFT,X
	STA	LINE2+1,Y
	DEX
	BPL	AT1
XVBI
	JMP	XITVBV
;
