        @program        prog_code,prog_name

;************** Start of Fargo program ***************

prog_code:
        move.w  #0,-(a7)
        jsr romlib[set_font]
        lea  2(a7),a7

        move.b #0,score1
        move.b #0,score2
reset_loop:
        jsr flib[clr_scr]

        move.w #4,-(a7) ;print out program info
        pea text(PC)
        move.w #123,-(a7)
        move.w #73,-(a7)
        jsr romlib[puttext]
        lea 10(a7),a7

        clr.l d0

        move.b score1,d0
        move.w #20,d3
        bsr shownum

        move.b score2,d0
        move.w #201,d3
        bsr shownum

        move.w #2,d1 ;2 direction
        move.w #0,d2  ;1 direction
        move.w #61,d3 ;x1 position
        move.w #55,d4 ;x1 position
        move.w #179,d5 ;x2 position
        move.w #55,d6 ;y2 position
        move.b #0,d7
        clr.w d0


draw_lin:
        move.w  #0,-(a7)
        move.w  d0,-(a7)
        jsr flib[pixel_on]
        lea     4(a7),a7
        add #1,d0
        cmp.w #241,d0
        bne draw_lin
        clr.w d0
draw_lins:
        move.w  d0,-(a7)
        move.w  #0,-(a7)
        jsr flib[pixel_on]
        lea     4(a7),a7
        move.w  d0,-(a7)
        move.w  #239,-(a7)
        jsr flib[pixel_on]
        lea     4(a7),a7
        add #1,d0
        cmp.w #122,d0
        bne draw_lins



main_loop:
        clr.w d0
slow_loop:

        add.w #1,d0
        cmp.w #2500,d0 ;decrease this value to speed up game
        bne slow_loop

        move.w  #$FEFF,($600018) ;if esc key pressed
	nop
	nop
	nop
	nop
	move.b	($60001B),d0
        cmp.b #$BF,d0
        beq exit

;all of the code until dir2 is for the keyboard sensing which is quite
;inefficient and should really be put in the slow_loop to make the game more
;responsive


        add #1,d2

        move.b d7,d0
        lsr.b #4,d0
        and.b #1,d0
        cmp.b #1,d0
        beq skip_1

        move.w #$FFDF,($600018) ;checks for F6
        nop
        nop
        nop
        nop
        move.b ($60001B),d0
        not.b d0
        and.b #$10,d0

        or.b d0,d7 ;sets bit 5 of d7 to the key pressed
             
        lsr.b #4,d0
        add.b d0,d2
        bra n_skip_1
skip_1:
        and.b #$EF,d7

        move.w #$FFDF,($600018) ;checks the row containing F6
        nop
        nop
        nop
        nop
        move.b ($60001B),d0
        not.b d0
        and.b #$10,d0

        or.b d0,d7 ;sets bit 5 of d7 to the key pressed

n_skip_1:


        move.b d7,d0
        lsr.b #3,d0
        and.b #1,d0
        cmp.b #1,d0
        beq skip_2

        move.w #$FFFE,($600018) ;checks the row containing hand
        nop
        nop
        nop
        nop
        move.b ($60001B),d0
        not.b d0
        and.b #$08,d0

        or.b d0,d7 ;sets bit 4 of d7 to 1 if F3 pressed

        lsr.b #3,d0
        sub.b d0,d2
        bra n_skip_2
skip_2:
        and.b #$F7,d7

        move.w #$FFFE,($600018) ;checks the row containing hand
        nop
        nop
        nop
        nop
        move.b ($60001B),d0
        not.b d0
        and.b #$08,d0

        or.b d0,d7 ;sets bit 4 of d7 to 1 if F3 pressed




n_skip_2:
        cmp.b #0,d2 ;direction from -1 to 3
        beq letd2_eq3 ;go to dir2

        sub.b #1,d2

        cmp.b #04,d2 ;direction for 4 to 0
        beq letd2_eq0 ;then return to dir2

dir2:

        move.w	#$FFFE,($600018)
	nop
	nop
	nop
	nop
	move.b	($60001B),d0

        add.b #1,d1

        not.b d0
        lsr.b #5,d0  ;d0=$04 for down or d0=$01 for up
        and.b #05,d0

        move.b d4,-(a7) ;push d4

        move.b d7,d4 ;use d4 for temp, take out relevent information
        and.b #5,d4
        cmp.b d0,d4
        beq move2 ;stores the current key-this line restores d1,goes to move
        move.b d7,d4 ;puts the new values for up and down into d7
        and.b #$18,d4
        or.b d0,d4
        move.b d4,d7

        move.b d0,d4 ;if going up d4=1
        and.b #01,d4
        add.b d4,d1

        lsr.b #2,d0 ;if going down d0=1
        and.b #01,d0
        sub.b d0,d1

        cmp.b #0,d1 ;direction from -1 to 3
        beq letd1_eq3
        sub.b #1,d1

        cmp.b #04,d1 ;direction for 4 to 0
        beq letd1_eq0 ;then goes to move then draw

        bra move

;if players at same pixel it is turned on before sensing (added since release)
check_x:
        cmp.w d3,d5
        bne draw2

        move.w  d6,-(a7) ;draws the pixel for player 2
        move.w  d5,-(a7)
        jsr flib[pixel_on]
        lea     4(a7),a7

        move.w  d4,-(a7) ;draws the pixel for player 1
        move.w  d3,-(a7)
        jsr flib[pixel_on]
        lea     4(a7),a7

        bra draw2
draw:
        cmp.w d4,d6
        beq check_x

draw2:
        move.w d4,-(a7) ;tests the pixel
        move.w d3,-(a7)
        jsr flib[find_pixel]
        lea 4(a7),a7               ;reset stack ptr
        btst d0,(a0)
        bne crash_1
detect_1:
        move.w d6,-(a7) ;tests the pixel
        move.w d5,-(a7)
        jsr flib[find_pixel]
        lea 4(a7),a7               ;reset stack ptr
        btst d0,(a0)
        bne crash_2 

        cmp #0,d3
        beq restart

        move.w  d6,-(a7) ;draws the pixel for player 2
        move.w  d5,-(a7)
        jsr flib[pixel_on]
        lea     4(a7),a7

        move.w  d4,-(a7) ;draws the pixel for player 1
        move.w  d3,-(a7)
        jsr flib[pixel_on]
        lea     4(a7),a7
	bra	main_loop


move:
        move.b (a7)+,d4 ;pop d4

        cmp.b #0,d1
        beq inc_x
        cmp.b #1,d1
        beq inc_y
        cmp.b #2,d1
        beq dec_x
        cmp.b #3,d1
        beq dec_y

move3:
        cmp.b #0,d2
        beq inc2_x
        cmp.b #1,d2
        beq inc2_y
        cmp.b #2,d2
        beq dec2_x
        cmp.b #3,d2
        beq dec2_y

move2:  sub.b #1,d1
        bra move

inc_x:
        add.w #1,d5
        bra move3
inc_y:
        add.w #1,d6
        bra move3
dec_x:
        sub.w #1,d5
        bra move3
dec_y:
        sub.w #1,d6
        bra move3

inc2_x:
        add.w #1,d3
        bra draw
inc2_y:
        add.w #1,d4
        bra draw
dec2_x:
        sub.w #1,d3
        bra draw
dec2_y:
        sub.w #1,d4
        bra draw

letd2_eq3:
        move.b #3,d2
        bra dir2

letd2_eq0:
        move.b #0,d2
        bra dir2

letd1_eq3:
        move.b #3,d1
        bra move

letd1_eq0:
        move.b #0,d1
        bra move

crash_1:
        move.w #4,-(a7)
        pea crash(PC)
        move.w #123,-(a7)
        move.w #20,-(a7)
        jsr romlib[puttext]
        lea 10(a7),a7
        move.b #0,d3
        add.b #1,score2
        bra detect_1

crash_2:
        move.w #4,-(a7)
        pea crash(PC)
        move.w #123,-(a7)
        move.w #201,-(a7)
        jsr romlib[puttext]
        lea  10(a7),a7
        add.b #1,score1
        bra restart

shownum:
        moveq #2,d1
        lea strbuf(PC),a0

        adda.l  d1,a0
        clr.b  (a0)
        subq.b  #1,d1
num_loop:
        divu #10,d0
        move.l d0,d2
        lsr.l #8,d2
        lsr.l #8,d2
        add.b #48,d2
        move.b d2,-(a0)
        and.l #$FFFF,d0
        dbra d1,num_loop

        move.w #4,-(a7)
        pea strbuf(PC)
        move.w #123,-(a7)
        move.w d3,-(a7)
        jsr romlib[puttext]
        lea 10(a7),a7

        rts

restart:
        jsr flib[idle_loop]
        jsr flib[idle_loop]
        bra reset_loop
exit:
        jsr flib[idle_loop]
        rts
;*****************************************************
; miscellaneous program data
;*****************************************************

prog_name       dc.b    "FTron 12",0
crash           dc.b    "Crash",0
text            dc.b    "FTron 12 by Gareth James",0
score1          dc.b    0
score2          dc.b    0
strbuf          dc.l    0

;*************** End of Fargo program ****************

	reloc_open
        add_library     flib
        add_library     romlib
	reloc_close
	end
