;spiker include

;the code and data needed to drive the deadly 'spikers'



Move_spikers
	cmp.w #4,level
	beq spike_0
	cmp.w #6,level
	beq spike_0
	rts
spike_0
	move.w #0,shit
	move.w #0,XL6
	move.l #spiker_variables,a3
	move.l #5,d4	;six spikers (36 bytes)
spike_3
	bsr run_spiker	;move and erase
	addq.l #6,a3
	dbf d4,spike_3
	rts

Print_spikers
	cmp.w #4,level
	beq spike_11
	cmp.w #6,level
	beq spike_11
	rts
spike_11
	move.l #spiker_variables,a3
	move.l #3,d4
spike_9
	tst.w (a3)
	beq spike_10		;not running - don't waste time
	bsr check_spiker_part1
	bsr unblank_spiker
	bsr check_spiker_part2
spike_10
	addq.l #6,a3
	dbf d4,spike_9
	rts


run_spiker
	tst.w (a3)	;is this one on?
	bne spike_4		;yes - skip to movement

	cmp.w #1,XL6
	beq spike_1		;already one started - skip out
	
	move.w DISTANCE,d0
	cmp.w shut_off,d0
	bgt spike_1	

	move.w XL7,d2
	and.w d2,d0
	tst.w d0
	bne spike_1		;not ready to appear

	move.w XL5,d0	;random number-ish for y-cord
	move.w d0,d1
	and.w #127,d0
	add.w #44,d0	;d0 = 44 to 171 (y -cord)
	move.w d0,2(a3)	;poke into y-cord
	move.w #352,(a3) ;the x-cord

	add.w #53173,d1	
	ror.w #2,d1
	move.w d1,XL5	;muddle random number

	and.w #1,d1
	move.w d1,4(a3)	;start up/down
	move.w #1,XL6	;flag set - spiker on this call
	bsr Sound6
	;spiker now successfully set up. next round it will start to appear.
	rts

spike_4
	move.w (a3),d1
	move.w 2(a3),d0
	bsr erase_spiker

	cmp.w #1,4(a3)	;check up/down (1=up)
	beq spiker_up
	bra spiker_down
sp_3
	subq.w #2,(a3)
	move.w (a3),d0
	cmp.w #10,d0
	bgt spike_1		;not at far left
	move.w #0,(a3)	;switch off
spike_1
	rts

spiker_up
	move.w 2(a3),d0
	cmp.w #68,d0
	blt spiker_slow_up
	sub.w #3,2(a3)
	bra sp_3

spiker_down
	move.w 2(a3),d0
	cmp.w #150,d0
	bgt spiker_slow_down
	add.w #3,2(a3)
	bra sp_3


spiker_slow_up
	addq.w #2,(a3)
	subq.w #2,2(a3)
	move.w 2(a3),d0
	cmp.w #48,d0
	bgt sp_3
	move.w #0,4(a3)
	bra sp_3

spiker_slow_down
	addq.w #2,(a3)
	addq.w #2,2(a3)
	move.w 2(a3),d0
	cmp.w #170,d0
	blt sp_3
	move.w #1,4(a3)
	bra sp_3



unblank_spiker
	tst.w (a3)		;is this one activated?
	beq spike_1
	move.w (a3),d1
	move.w 2(a3),d0
	moveq #0,d2
	mulu.w #48,d0
	move.w d1,d2
	lsr.w #3,d2
	move.l #SCREEN6,a2
	add.l d0,a2
	add.l d2,a2
	move.w 4(a3),d0
	bsr which_spiker
	and.w #$000F,d1
	ror.w #4,d1
	or.w #$9f0,d1					;d1 is now BLTCON0
	jsr waitblit
	move.w #0,BLTCON1(a6)			; Special modes: none
	move.w d1,BLTCON0(a6)			; Copy A -> D
	move.l a0,BLTAPTH(a6)			; Area to copy FROM
	move.w #0,BLTAMOD(a6)			; Data is consectutive!
	move.l a2,BLTDPTH(a6)			; Area to copy TO
	move.w #44,BLTDMOD(a6)			; Screen width - ship width (.b)
	move.w #$2c2,BLTSIZE(a6)		; Size and DO IT!
	rts

erase_spiker
	mulu.w #48,d0
	move.w d1,d2
	lsr.w #3,d2
	move.l #SCREEN6,a2
	add.l d0,a2
	add.l d2,a2
	and.w #$000F,d1
	ror.w #4,d1
	or.w #DEST,d1					;d1 is now BLTCON0
	jsr waitblit
	move.w #0,BLTCON1(a6)			; Special modes: none
	move.l a2,BLTDPTH(a6)			; Area to copy TO
	move.w #44,BLTDMOD(a6)			; skip a word each line here too
	move.w d1,BLTCON0(a6)			; Copy A -> D (without A)
	move.w #$2c2,BLTSIZE(a6)		; Size and DO IT!
	rts

check_spiker_part1
	move.w (a3),d1
	move.w 2(a3),d0
	moveq #0,d2
	mulu.w #48,d0
	move.w d1,d2
	lsr.w #3,d2
	move.l #SCREEN4,a2
	add.l d0,a2
	add.l d2,a2
	and.w #$000F,d1
	ror.w #4,d1
	or.w #$cc0,d1	
	move.w 4(a3),d0		;can change direction depending on going up or down
	bsr which_spiker
	jsr waitblit
	move.w d1,BLTCON0(a6)			; Copy A.B -> D (without D)
	move.w #0,BLTCON1(a6)			; Special modes: none
	move.l a0,BLTAPTH(a6)			; Graphic
	move.w #0,BLTAMOD(a6)
	move.l a2,BLTBPTH(a6)			; Area to check
	move.w #44,BLTBMOD(a6)			; skip a word each line here too
	move.w #$2c2,BLTSIZE(a6)			; Size and DO IT!
	jsr waitblit

	move.w DMACONR(a6),d0
	lsr.w #6,d0
	btst #7,d0
	bne cm_4
	move.w #1,shit
cm_4
	rts

check_spiker_part2
	cmp.w #1,shit
	beq collision_3
	rts
collision_3
	;here we must check for missile1,2 and bomb 1,2 in turn
	move.l #miss_variables,a2
	bsr check_it_missile3
	move.l #miss_variables+6,a2
	bsr check_it_missile3
	move.l #bomb_variables,a2
	bsr check_it_bomb3
	move.l #bomb_variables+6,a2
	bsr check_it_bomb3
	rts

check_it_missile3
	tst.w (a2)
	beq cm_3		;no weapon operating
	move.w 2(a2),d1	;miss.x
	move.w 4(a2),d0 ;miss.y

	and.l #$0000ffff,d0
	and.l #$000000ff,d1
	move.l d1,d2
	bclr #0,d2
	mulu.w #48,d0
	move.l #SCREEN6+1920,a1
	add.l d0,a1
	add.l d2,a1
	move.w (a1),d0
	btst #0,d1
	bne cm_1
	move.w #$ff00,d2
	bra cm_2
cm_1
	move.w #$00ff,d2
cm_2
	and.w d2,d0
	tst.w d0
	bne destroyed_spiker
cm_3
	rts ;no hit

check_it_bomb3
	tst.w (a2)
	beq cm_3		;no weapon operating
	move.w 2(a2),d1	;bomb.x
	move.w 4(a2),d0 ;bomb.y
	and.l #$0000ffff,d0
	and.l #$000000ff,d1
	move.l d1,d2
	bclr #0,d2
	mulu.w #48,d0
	move.l #SCREEN6+1920,a1
	add.l d0,a1
	add.l d2,a1

	move.w (a1),d0
	and.w #$ff00,d0
	tst.w d0
	bne destroyed_spiker
	rts


destroyed_spiker
	jsr Sound2
	add.w #10,score

	move.w #2,(a2)		;special case to dispose of old missile

	move.w (a3),d1
	move.w 2(a3),d0
	bsr erase_spiker
	move.w (a3),d0
	move.w 2(a3),d1
	bsr Bang
	move.w #0,(a3)	;switch off spiker
	rts


which_spiker
	move.l #spiker_gfx1,a0
	move.w DISTANCE,d0
	btst #2,d0
	beq.s ws1
	move.l #spiker_gfx2,a0
ws1
	rts

Reset_spikers
	move.l #spiker_variables,a3
	move.l #0,(a3)
	move.l #0,4(a3)
	move.l #0,8(a3)
	move.l #0,12(a3)
	move.l #0,16(a3)
	move.l #0,20(a3)
	move.l #0,24(a3)
	move.l #0,28(a3)
	move.l #0,32(a3)
	move.l #0,36(a3)
	rts

shit
	dc.w 0

spiker_variables
	dcb.w 40
	;+0 (x/status)
	;+2 y
	;+4 dy (1,0)

