;************************************************************************
;*            Falcon 030 True Color 2D/3D graphic engine
;*
;*               Written by: Vince Valenti
;*              (c)1993-1996 by JV Enterprises
;*
;*  This product is released as Freeware, and can be used free of
;*  charge in any product for the Falcon, as long as you include a credits
;*  to Vince and JV Enterprises in the program and Manual.
;*
;*  This program was written in GFA Assembler.
;*
;*  Note:  Any intruction that look like so:   move.w brite,d4
;*         is optimized by the GFA compiler
;*         to read like this:                  move.w brite(pc),d4
;*         If you plan to convert this
;*         source to another assembler
;*         make sure it can add the (pc) or
;*         include it yourself.
;************************************************************************


;  --- General purpose routines    ---
             jmp       aseven               ;+28 Jump to find out stack value
             jmp       mouse_hand           ;+32 Jump for mouse handler

;  --- 2D TC block routines        ---
             jmp       r_put                ;+36 jump to replace put
             jmp       r_putp               ;+40 Jump to replace put part
             jmp       t_put                ;+44 jump to trans put
             jmp       ty_put               ;+48 jump to trans put w/ y-clip
             jmp       blk_get              ;+52 jump to block get
             jmp       stars                ;+56 Jump for stars

;  --- TC full screen routines     ---
             jmp       put_full             ;+60 jump to clear screen
             jmp       fade                 ;+64 jump to fade routine
             jmp       fade2                ;+68 Jump for fade-in-game routine


;  --- 3D scale and T-map routines ---
             jmp       t_scale_put          ;+72 jump to trans scale put
             jmp       y_map_put            ;+76 jump to texture map put
             jmp       floor_map            ;+80 jump to floor texture map


;  --- 3D rotate and sort routines ---
             jmp       sound_trans          ;+84 Jump to sound translate
             jmp       y_rot_trans          ;+88 jump to rotate and translate
             jmp       z_sort_rout          ;+92 jump to z-sort w/offset calc


;  --- Misc config variables       ---
brite:       .DC.l 0   ;                    ;+96 FADE variable 0=off
y_value:     .DC.w 200 ;                    ;100 max y
y_value2:    .DC.w 199 ;                    ;102 max y-1
trans:       .DC.w 640,0                    ;104 transparent
NONE:        

;*********************
;******** A-Seven ************         locate A7

aseven:      move.l    sp,d0
             rts       

;*************** S-T-A-R-S ********  ; w:deg,w:var_flip,blk_addr,scn_addr

stars:       movem.l   d0-d7/a0-a3,-(sp)

             move.w    4+48(sp),d0          ;degrees
             move.w    6+48(sp),d1          ;y position
             movea.l   8+48(sp),a0          ;block addr
             movea.l   12+48(sp),a1         ;screen addr

             muls.w    #640,d1              ; offset y for start
             adda.l    d1,a1

             mulu.w    #58254,d0            ;((320/360)*&HFFFF) * D0
             swap.w    d0                   ;calc offset

             addq.l    #2,a0                ; bypass x len in block addr

             move.w    (a0)+,d3             ; get the y len in block addr
             subq.w    #1,d3

star_out:    movea.l   a1,a3                ;
             move.w    #319,d2              ;
             sub.w     d0,d2                ;
             blt.s     star_cont            ;
             movea.l   a0,a2                ;
             adda.w    d0,a2                ;
             adda.w    d0,a2                ;
;
star_in:     move.w    (a2)+,(a3)+          ; *
             dbf       d2,star_in           ; *

star_cont:   move.w    d0,d2                ; *
             subq.w    #1,d2                ; *
             blt.s     star_end             ; *
             movea.l   a0,a2                ; *

star_in2:    move.w    (a2)+,(a3)+          ; *
             dbf       d2,star_in2          ; *

star_end:    adda.w    #640,a1              ; *
             adda.w    #640,a0              ; *
             dbf       d3,star_out          ; *

             movem.l   (sp)+,d0-d7/a0-a3
             rts       



; ****** REPLACE PUT - PART ***********    ;replace put

r_putp:      movem.l   d0-d3/a0-a2,-(sp)
             movea.l   16+28(sp),a1         ;x,y,blk_addr,scn_addr,part
             movea.l   12+28(sp),a0         ; good for hit points & stuff
             move.l    8+28(sp),d1
             move.l    4+28(sp),d0
             move.w    20+28(sp),d3
             beq.s     r_endp

             add.l     d0,d0
             adda.l    d0,a1                ;offset x on dest

             mulu.w    #640,d1              ;offset y on dest
             adda.l    d1,a1

             move.w    (a0)+,d0             ;xl and yl
             sub.w     d3,d0                ; sup part from all xl

             bpl.s     cont_part            ; force a max of xl if part is >than
             move.w    -2(a0),d3
             clr.w     d0

cont_part:   add.w     d0,d0                ; add for 16bit TC

             move.w    (a0)+,d1
             subq.w    #1,d1
             subq.w    #1,d3                ;offset

r_loop1p:    move.l    d3,d2                ;setup loop
             movea.l   a1,a2                ;

r_loop2p:    move.w    (a0)+,(a2)+          ;
;                                           ;
             dbf       d2,r_loop2p          ;
;
             adda.w    d0,a0                ;add offset
             lea       640(a1),a1           ;
             dbf       d1,r_loop1p          ;

r_endp:      movem.l   (sp)+,d0-d3/a0-a2
             rts       


; ****** REPLACE PUT ***********

r_put:       movem.l   d0-d2/a0-a2,-(sp)
             movea.l   16+24(sp),a1         ;x,y,blk_addr,scn_addr
             movea.l   12+24(sp),a0
             move.l    8+24(sp),d1
             move.l    4+24(sp),d0
             add.l     d0,d0
             adda.l    d0,a1                ;offset x on dest

             mulu.w    #640,d1              ;offset y on dest
             adda.l    d1,a1

             moveq     #7,d2
             move.w    (a0)+,d0             ;xl and yl
             move.w    (a0)+,d1
             subq.w    #1,d1
             subq.w    #1,d0                ;offset and divide by 2
             lsr.w     #1,d0

r_loop1:     move.l    d0,d2                ;
             movea.l   a1,a2                ;

r_loop2:     move.l    (a0)+,(a2)+          ;
;
             dbf       d2,r_loop2           ;
;
             lea       640(a1),a1           ;
             dbf       d1,r_loop1           ;

             movem.l   (sp)+,d0-d2/a0-a2
             rts       



; ***** TRANS PUT **********

t_put:       movem.l   d0-d4/a0-a2,-(sp)
             lea       trans,a0
             move.w    (a0),d4
             movea.l   16+32(sp),a1         ;x,y,blk_addr,scn_addr
             movea.l   12+32(sp),a0
             move.l    8+32(sp),d1
             move.l    4+32(sp),d0
             add.l     d0,d0
             adda.l    d0,a1                ;offset x on dest

             mulu.w    d4,d1                ;offset y on dest
             adda.l    d1,a1

             moveq     #3,d2
             move.w    (a0)+,d0             ;xl and yl
             move.w    (a0)+,d1

t_lop_sl:    subq.w    #1,d0                ;offset and divide by 2
             subq.w    #1,d1
             lsr.w     #1,d0

t_loop1:     move.l    d0,d2                ; *
             movea.l   a1,a2                ; *
;                                             *
t_loop2:     move.w    (a0)+,d3             ; *
             beq.s     ttest1               ; *
             move.w    d3,(a2)              ; *
ttest1:      move.w    (a0)+,d3             ; *
             beq.s     ttest4               ; *
             move.w    d3,2(a2)             ; *

ttest4:      addq.w    #4,a2                ; *
             dbf       d2,t_loop2           ; *

             adda.w    d4,a1                ; *
             dbf       d1,t_loop1           ; *

             movem.l   (sp)+,d0-d4/a0-a2
             rts       



; ***** TRANS PUT W/ Y CLIP**********

ty_put:      movem.l   d0-d4/a0-a2,-(sp)
             movea.l   16+32(sp),a1         ;x,y,blk_addr,scn_addr
             movea.l   12+32(sp),a0
             move.l    8+32(sp),d1

             move.w    y_value2,d0          ; compare high y value
             cmp.w     d0,d1
             bgt       tyc_end

             move.l    4+32(sp),d0
             add.w     d0,d0
             adda.w    d0,a1                ;offset x on dest
             move.w    (a0)+,d0

             clr.l     d4
             move.w    d0,d3                ;x-clip
             add.w     d3,d3
             add.w     6+32(sp),d3
             subi.w    #319,d3
             ble.s     tcy_xcont
             asr.w     #1,d3
             sub.w     d3,d0
             asl.w     #1,d3
             move.w    d3,d4

tcy_xcont:   move.w    (a0)+,d2
             move.w    d2,d3

             add.w     d1,d2

             mulu.w    #640,d1              ;offset y on dest
             adda.l    d1,a1

             sub.w     y_value,d2
             ble.s     ty_cont
             sub.w     d2,d3
             ble.s     tyc_end

;xl and yl
ty_cont:     move.w    d3,d1
             subq.w    #1,d1
             subq.w    #1,d0

tyc_loop1:   move.l    d0,d2                ; *
             movea.l   a1,a2                ; *
;                                             *
tyc_loop4:   move.w    (a0)+,d3             ; *
             beq.s     tyctest2             ; *
             move.w    d3,(a2)              ; *
             move.w    d3,2(a2)             ; *
;                                             *
tyctest2:    addq.w    #4,a2                ; *
             dbf       d2,tyc_loop4         ; *

             adda.w    d4,a0                ; *
             lea       640(a1),a1           ; *
             dbf       d1,tyc_loop1         ; *

tyc_end:     movem.l   (sp)+,d0-d4/a0-a2
             rts       



; ******* FULL SCREEN CLEAR * NON BLiTTER ***********  ;clear screen

put_full:    movem.l   d0-d7/a0-a6,-(sp)    ;     l:scn  l:numberofbytes
             movea.l   4+60(sp),a0          ; scn
             move.l    8+60(sp),d0          ; number of bytes
             adda.l    d0,a0
             lsr.l     #6,d0
             subq.l    #1,d0

             clr.l     d1
             clr.l     d2
             clr.l     d3
             clr.l     d4

             clr.l     d5
             clr.l     d6
             clr.l     d7
             clr.l     a1

put_fl:      movem.l   d1-d7/a1,-(a0)
             movem.l   d1-d7/a1,-(a0)
             dbf       d0,put_fl

             movem.l   (sp)+,d0-d7/a0-a6
             rts       


;********* BLOCK GET ************

blk_get:     movem.l   d0-d3/a0-a2,-(sp)
             lea       trans,a0
             move.w    (a0),d3
             movea.l   20+28(sp),a1         ;x,y,w:xl,w:yl,blk_addr,scn_addr
             movea.l   16+28(sp),a0
             move.l    8+28(sp),d1
             move.l    4+28(sp),d0
             add.l     d0,d0
             adda.l    d0,a1                ;offset x on dest
             mulu.w    d3,d1                ;offset y on dest
             adda.l    d1,a1

             move.w    12+28(sp),d0         ;xl and yl
             move.w    14+28(sp),d1
             andi.w    #%1111111111111110,d0
             move.w    d0,(a0)+
             move.w    d1,(a0)+
             subq.w    #1,d0                ;offset and divide by 2
             subq.w    #1,d1
             lsr.w     #1,d0

g_loop1:     move.l    d0,d2                ;*
             movea.l   a1,a2                ;*
g_loop2:     move.l    (a2)+,(a0)+          ;*
             dbf       d2,g_loop2           ;*
             adda.w    d3,a1
             dbf       d1,g_loop1           ;*

             movem.l   (sp)+,d0-d3/a0-a2
             rts       



; ********** Fade routine ******* ad_buf,ad_scn,w:fade (4-0)fadein (-)fadeout

fade:        movem.l   d0-d7/a0-a6,-(sp)
             movea.l   4+60(sp),a0
             movea.l   8+60(sp),a1
             lea       look_up,a2
             move.w    12+60(sp),d0
             blt.s     fade_out
             move.w    d0,d2
             add.w     d2,d2
             move.w    0(a2,d2.w),d1
             swap.w    d1
             move.w    0(a2,d2.w),d1

             move.w    y_value,d2
             mulu.w    #40,d2
             subq.w    #1,d2

fin_loop:    move.l    (a0)+,d3             ;*
             and.l     d1,d3                ;*
             move.l    d3,(a1)+             ;*
             move.l    (a0)+,d3             ;*
             and.l     d1,d3                ;*
             move.l    d3,(a1)+             ;*
             move.l    (a0)+,d3             ;*
             and.l     d1,d3                ;*
             move.l    d3,(a1)+             ;*
             move.l    (a0)+,d3             ;*
             and.l     d1,d3                ;*
             move.l    d3,(a1)+             ;*
             dbf       d2,fin_loop          ;*

             bra.s     fade_end

fade_out:    neg.w     d0
             move.w    d0,d2
             add.w     d2,d2
             move.w    0(a2,d2.w),d1
             swap.w    d1
             move.w    0(a2,d2.w),d1
             move.w    #7999,d2

fout_loop:   and.l     d1,(a1)+             ;*
             and.l     d1,(a1)+             ;*
             and.l     d1,(a1)+             ;*
             and.l     d1,(a1)+             ;*
             dbf       d2,fout_loop         ;*

fade_end:    movem.l   (sp)+,d0-d7/a0-a6
             rts       

; ********** Fade routine2 ******* ad_buf,w:fade(4-0),w:0=and,1=or,2R 3G 4B

fade2:       movem.l   d0-d7/a0-a6,-(sp)
             movea.l   4+60(sp),a0
             lea       look_up,a2
             move.w    8+60(sp),d0
             add.w     d0,d0
             move.w    0(a2,d0.w),d1        ; get fade value from lookup
             swap.w    d1
             move.w    0(a2,d0.w),d1

             move.w    y_value,d3
             mulu.w    #40,d3
             subq.w    #1,d3

             move.w    10+60(sp),d0
             bne.s     fnand2

fand2_in:    and.l     d1,(a0)+             ;*
             and.l     d1,(a0)+             ;*
             and.l     d1,(a0)+             ;*
             and.l     d1,(a0)+             ;*
             dbf       d3,fand2_in          ;*

             bra.s     fade2_end

fnand2:      move.l    #%11111000000000001111100000000000,d2
             cmpi.w    #2,d0
             beq.s     for2_yes             ; mask only red, blue or green
             cmpi.w    #3,d0
             beq.s     for2_yes2
             cmpi.w    #4,d0
             bne.s     for2_in
             ror.l     #6,d2
for2_yes2:   ror.l     #5,d2
for2_yes:    and.l     d2,d1

for2_in:     or.l      d1,(a0)+             ;*
             or.l      d1,(a0)+             ;*
             or.l      d1,(a0)+             ;*
             or.l      d1,(a0)+             ;*
             dbf       d3,for2_in           ;*

fade2_end:   movem.l   (sp)+,d0-d7/a0-a6
             rts       

;******* TRANS SCALE PUT *******

t_scale_put: movem.l   d0-d7/a0-a6,-(sp)
             movea.l   20+60(sp),a1         ; cx,by,ty,block,screen
             movea.l   a1,a4
             movea.l   a4,a5

             move.w    y_value,d3           ; get y value....
             mulu.w    #640,d3              ; caluate end of y in bytes
             subq.w    #1,d3
             adda.l    d3,a5

             movea.l   16+60(sp),a0
             move.w    10+60(sp),d3
             move.w    #640,d0
             muls.w    d3,d0                ; y offset
             adda.l    d0,a1

             sub.w     14+60(sp),d3
             clr.l     d4
             move.w    2(a0),d4             ; y-len

             lsl.l     #8,d4
             divu.w    d3,d4
             andi.l    #$ffff,d4
             lsl.l     #8,d4                ;create scale value (a6)
             movea.l   d4,a6

             lea       tcolor,a2            ; set up color fading D5
             lsl.l     #4,d4
             swap.w    d4

             move.w    brite,d0
             cmpi.w    #0,d0                ; turn off fade if zero
             bne.s     not_equ_z
             clr.l     d4

not_equ_z:   sub.w     d0,d4
             add.w     d4,d4
             move.w    0(a2,d4.w),d5
             move.w    92(a2,d4.w),d7
             beq       end

color:       clr.l     d0                   ; clear x and y value
             move.l    d0,d1
             move.w    (a0)+,d0             ; x-len
             move.w    (a0)+,d1             ; y-len

             move.l    a6,d3                ; move from center to lower right x
             lsr.l     #8,d3
             move.l    d0,d2
             lsl.l     #7,d2
             divu.w    d3,d2
             move.w    d2,d3
             add.w     6+60(sp),d2
             ext.l     d2
             add.l     d2,d2                ; x offset
             adda.l    d2,a1

             moveq     #5,d6
             cmpi.w    #17,d0
             blt.s     offset
             addq.w    #1,d6
             cmpi.w    #33,d0
             blt.s     offset
             addq.w    #1,d6                ; D6 is source y-offset

offset:      swap.w    d0
             move.l    4+60(sp),d2          ; check for x-clip at 80 and 248
             sub.w     d3,d2
             add.w     6+60(sp),d3

             cmpi.w    #0,d2
             bge.s     move_on
             subi.w    #-1,d2               ; check if less than 0 and clip
             neg.w     d2

             clr.l     d3
lft_clp:     sub.l     a6,d0
             ble       end
             add.l     a6,d3
             dbf       d2,lft_clp

             swap.w    d3
             add.w     d3,d3
             adda.w    d3,a0
             bra.s     move_on2

move_on:     cmpi.w    #319,d3              ; check if greater than 319 and clip
             ble.s     move_on2
             subi.w    #320,d3

rgt_clp:     sub.l     a6,d0
             ble       end
             subq.l    #2,a1
             dbf       d3,rgt_clp

move_on2:    swap.w    d1                   ; and create fixed point loop
             subq.l    #8,d0                ; offset by eight
             subq.l    #8,d1
             addq.w    #2,a1
             cmpa.l    #$e000,a6
             blt       large

             cmpi.w    #0,d5
             beq       ts_loopbnf
             tst.w     d7
             bpl       ts_loopb22

;-----------------------------------------------

ts_loop:     cmpa.l    a4,a1
             blt       end
             cmpa.l    a5,a1
             bgt.s     next
             movea.l   a1,a2                ; optimized for Fade shrinking
             move.l    d0,d2

             move.l    d1,d3
             swap.w    d3
             lsl.w     d6,d3
             lea       0(a0,d3.w),a3

ts_loop2:    move.l    d2,d3
             swap.w    d3
             add.w     d3,d3
             move.w    0(a3,d3.w),d4
             beq.s     ts_trans
             move.w    d4,d3
             lsr.w     d7,d3                ; hell section **********
             and.w     d5,d3
             sub.w     d3,d4
             sub.w     d3,d4
             sub.w     d3,d4

             move.w    d4,-(a2)
             sub.l     a6,d2
             bgt.s     ts_loop2

             lea       -640(a1),a1
             sub.l     a6,d1
             bgt.s     ts_loop
             bra       end

ts_trans:    subq.w    #2,a2
             sub.l     a6,d2
             bgt.s     ts_loop2

next:        lea       -640(a1),a1
             subq.l    #1,a5
             sub.l     a6,d1
             bgt.s     ts_loop
             bra       end


ts_loopb22:  cmpa.l    a4,a1                ; optimized for Quick fade
             blt       end
             cmpa.l    a5,a1
             bgt.s     nextb22
             movea.l   a1,a2
             move.l    d0,d2

             move.l    d1,d3
             swap.w    d3
             lsl.w     d6,d3
             lea       0(a0,d3.w),a3

ts_loop22:   move.l    d2,d3
             swap.w    d3
             add.w     d3,d3
             move.w    0(a3,d3.w),d4
             beq.s     ts_trans2
             move.w    d4,d3
             lsr.w     d7,d3                ; hell section **********
             and.w     d5,d3
             sub.w     d3,d4

             move.w    d4,-(a2)
             sub.l     a6,d2
             bgt.s     ts_loop22

             lea       -640(a1),a1
             sub.l     a6,d1
             bgt.s     ts_loopb22
             bra       end

ts_trans2:   subq.w    #2,a2
             sub.l     a6,d2
             bgt.s     ts_loop22

nextb22:     lea       -640(a1),a1
             sub.l     a6,d1
             bgt.s     ts_loopb22
             bra       end


ts_loopbnf:  cmpa.l    a4,a1                ;Optimized for shrinking
             blt       end
             cmpa.l    a5,a1
             bgt.s     nextbnf
             movea.l   a1,a2
             move.l    d0,d2

             move.l    d1,d3
             swap.w    d3
             lsl.w     d6,d3
             lea       0(a0,d3.w),a3

ts_loopnf:   move.l    d2,d3                ;no Fade shrink
             swap.w    d3
             add.w     d3,d3
             move.w    0(a3,d3.w),d4
             beq.s     ts_transnf

             move.w    d4,-(a2)
             sub.l     a6,d2
             bgt.s     ts_loopnf

             lea       -640(a1),a1
             sub.l     a6,d1
             bgt.s     ts_loopbnf
             bra       end

ts_transnf:  subq.w    #2,a2
             sub.l     a6,d2
             bgt.s     ts_loopnf

nextbnf:     lea       -640(a1),a1
             sub.l     a6,d1
             bgt.s     ts_loopbnf
             bra       end


large:       move.l    d0,d3                ; optimized for enlarging ******
             clr.l     d2
             swap.w    d3
             add.w     d3,d3
             adda.w    d3,a0
             move.l    #$10000,d5

ts_loopl:    cmpa.l    a4,a1
             blt       end
             cmpa.l    a5,a1
             bgt.s     next2
             movea.l   a1,a2
             move.l    d0,d2

             move.l    d1,d3
             swap.w    d3
             lsl.w     d6,d3
             addq.w    #2,d3
             lea       0(a0,d3.w),a3

ts_loop2l:   move.w    -(a3),d4
             beq.s     ts_transl
ts_nontran:  move.w    d4,-(a2)
             sub.w     a6,d2
             bcc.s     ts_nontran
             sub.l     d5,d2
             bpl.s     ts_loop2l

             lea       -640(a1),a1
             sub.l     a6,d1
             bgt.s     ts_loopl
             bra       end

ts_transl:   subq.w    #2,a2
             sub.w     a6,d2
             bcc.s     ts_transl
             sub.l     d5,d2
             bpl.s     ts_loop2l

next2:       lea       -640(a1),a1
             sub.l     a6,d1
             bgt.s     ts_loopl


end:         movem.l   (sp)+,d0-d7/a0-a6
             rts       



; ********* Y-TEXTURE MAP ROUTINE *******  ;x1,x3,y1,y3,y2,y4,blk,scn,w:dr

y_map_put:   movem.l   d0-d7/a0-a6,-(sp)
             clr.l     d0
             move.w    6+60(sp),d0
             cmpi.w    #319,d0
             bgt       end_map
             move.w    10+60(sp),d1         ;x3 value
             ext.l     d1
             move.l    d1,8+60(sp)

             cmpi.w    #0,d1
             blt       end_map
             sub.w     d0,d1                ; create dx in (D1)
             ble       end_map

             movea.l   28+60(sp),a0         ;get blk addr (A0)
             move.w    (a0)+,d7             ;setup for transparent (D7)

             moveq     #64,d2               ;only 64 pixles wide allowed
             swap.w    d2                   ;store bx value (A6)
             movea.l   d2,a6
             subq.l    #8,a6

             cmpi.w    #64,d1
             ble.s     y_map_sclp

             divu.w    d1,d2
             andi.l    #$ffff,d2
             bra.s     y_map_bclp


y_map_sclp:  lsr.l     #8,d2
             divu.w    d1,d2
             andi.l    #$ffff,d2
             lsl.l     #8,d2                ;create xlen value (D2)

y_map_bclp:  clr.l     d6
             move.w    (a0)+,d6             ;temp store of Y value
             swap.w    d6
             movea.l   d6,a5                ;store BY at (A5)
             subq.l    #8,a5

setcolor:    move.w    18+60(sp),d4
             sub.w     26+60(sp),d4         ;setup fade control
             move.w    14+60(sp),d5
             sub.w     22+60(sp),d5
             add.w     d4,d5
             lsr.w     #1,d5

colorn:      lsr.l     #8,d6
             divu.w    d5,d6
             andi.l    #$ffff,d6
             lsl.l     #8,d6                ; create fraction for color
             lsl.l     #4,d6

             lea       tcolor,a1            ; set up color fading (D6)
             swap.w    d6

             move.w    brite,d5
             cmpi.w    #0,d5                ; turn off fade if zero
             bne.s     not_equ_z1
             clr.l     d6

not_equ_z1:  sub.w     d5,d6
             add.w     d6,d6
             movea.w   92(a1,d6.w),a2
             move.w    0(a1,d6.w),d6

             moveq     #1,d3

             clr.l     d5                   ; set y length for door openings
             move.w    36+60(sp),d5
             beq.s     move_map2
             swap.w    d5
             suba.l    d5,a6
             cmpa.l    #0,a6                ; offset loop
             ble       end_map


move_map2:   move.w    18+60(sp),d0
             sub.w     14+60(sp),d0
             ext.l     d0
             asl.l     #8,d0
             divs.w    d1,d0
             ext.l     d0
             asl.l     #8,d0
             move.l    d0,12+60(sp)         ;create DY for y4 and store in y2

             move.w    26+60(sp),d0
             sub.w     22+60(sp),d0
             ext.l     d0
             asl.l     #8,d0
             divs.w    d1,d0
             ext.l     d0
             asl.l     #8,d0
             move.l    d0,20+60(sp)         ;create Dy2 and store at y

             move.l    a0,28+60(sp)

             move.l    8+60(sp),d0
             add.l     d0,d0
             add.l     d0,32+60(sp)         ;offset ADDR
             movea.l   #640,a4

             move.l    24+60(sp),d0         ; flip len and y
             swap.w    d0
             move.l    d0,24+60(sp)
             move.l    16+60(sp),d0
             swap.w    d0
             move.l    d0,16+60(sp)
             movea.l   #$ffff,a3

             exg.l     a6,d2
             tst.w     d7
             beq       x_loop_t
             cmpa.w    a3,a2
             bne       x_loop_f
             move.l    #$10000,d6
             movea.l   #128,a2


x_loop_r:    move.l    a5,d5                ; ******** NO FADE MODE *********
             move.l    8+60(sp),d7
             blt       end_map
             cmpi.w    #319,d7
             bgt       y_cont_r

             move.l    a5,d7
             move.w    16+60(sp),d0
             move.w    d0,d4
             sub.w     24+60(sp),d0

bigger:      lsr.l     #3,d7                ;shift if value is greater than one
             divu.w    d0,d7
             andi.l    #$ffff,d7
;             lsl.l     #3,d7                ;create ylen value (D7)

             cmp.w     y_value2,d4          ;compare bottom clip
             ble.s     yclip
             move.w    d4,d0
             sub.w     y_value2,d0          ;sub ypos
             sub.w     d0,d4                ;sub difference to y
             mulu.w    d7,d0                ;mul remainder and ylen
             lsl.l     #3,d0
             sub.l     d0,d5                ;sub ylen from by
             ble.s     y_cont_r

yclip:       mulu.w    #640,d4              ;mult to screen
             movea.l   28+60(sp),a0         ;reset addrs
             movea.l   32+60(sp),a1
             adda.l    d4,a1

             move.w    24+60(sp),d4
             cmpi.w    #0,d4                ;compare top clip
             bge.s     yclipt
             subi.w    #0,d4                ;sub ypos
             neg.w     d4                   ;negate
             mulu.w    d7,d4                ;mul remainder and ylen
             lsl.l     #3,d4
             clr.w     d4
             sub.l     d4,d5                ;sub ylen from by
             ble.s     y_cont_r
             swap.w    d4
             lsl.w     #7,d4
             adda.w    d4,a0                ;add offset to source

yclipt:      lsl.l     #3,d7
             move.l    d2,d0                ; set offset for block x
             swap.w    d0
             add.w     d0,d0
             lea       0(a0,d0.w),a0
             cmp.l     a3,d7
             bgt.s     y_loop_r

             move.l    d5,d0
             swap.w    d0
             lsl.w     #7,d0
             adda.w    d0,a0

y_loop_r2:   move.w    (a0),d4
             suba.w    a2,a0
y_loop2_r2:  move.w    d4,(a1)
             suba.l    a4,a1                ; dec y
             sub.w     d7,d5
             bcc.s     y_loop2_r2
             sub.l     d6,d5
             bpl.s     y_loop_r2
             bra.s     y_cont_r

y_loop_r:    move.l    d5,d0                ;set offset for block y
             swap.w    d0
             lsl.w     #7,d0
             move.w    0(a0,d0.w),(a1)
             suba.l    a4,a1                ; dec y
             sub.l     d7,d5
             bgt.s     y_loop_r

y_cont_r:    subq.l    #2,32+60(sp)

             move.l    20+60(sp),d0         ;add to len
             sub.l     d0,24+60(sp)

             move.l    12+60(sp),d0         ; add to y
             sub.l     d0,16+60(sp)

             subq.l    #1,8+60(sp)
             sub.l     a6,d2
             bgt       x_loop_r
             bra       end_map


x_loop_f:    move.l    a5,d7                ; ******** FADE MODE *********

             move.l    8+60(sp),d5
             blt       end_map
             cmpi.w    #319,d5
             bgt       y_cont_ff

             move.w    16+60(sp),d0
             sub.w     24+60(sp),d0

biggerf:     lsr.l     #4,d7                ;shift if value is greater than one
             divu.w    d0,d7
             andi.l    #$ffff,d7
;             lsl.l     #4,d7                ;create ylen value (D7)

cont_smalf:  lea       tcolor,a1            ; set up color fading (D6)
             move.l    d7,d4
             lsl.l     #8,d4                ; double for not lsl.ing d7
             swap.w    d4
             sub.w     brite,d4
             add.w     d4,d4
             movea.w   92(a1,d4.w),a2
             move.w    0(a1,d4.w),d6
             cmpa.w    #0,a2
             beq       y_cont_ff
             move.l    a5,d5

             move.w    16+60(sp),d4         ;create y% offset
             cmp.w     y_value2,d4          ;compare bottom clip
             blt.s     yclipf
             move.w    d4,d0
             sub.w     y_value2,d0          ;sub ypos
             move.l    d7,d3                ;move ylen
             mulu.w    d0,d3                ;mul remainder and ylen
             lsl.l     #4,d3
             sub.l     d3,d5                ;sub ylen from by
             ble       y_cont_ff
             sub.w     d0,d4                ;sub difference to y

yclipf:      mulu.w    #640,d4              ;mult to screen
             movea.l   28+60(sp),a0         ;reset addrs
             movea.l   32+60(sp),a1
             adda.l    d4,a1

             move.w    24+60(sp),d4
             cmpi.w    #0,d4                ;compare top clip
             bge.s     ycliptf
             subi.w    #0,d4                ;sub ypos
             neg.w     d4                   ;negate
             mulu.w    d7,d4                ;mul remainder and ylen
             lsl.l     #4,d4
             clr.w     d4
             sub.l     d4,d5                ;sub ylen from by
             ble.s     y_cont_ff
             swap.w    d4
             lsl.w     #7,d4
             adda.w    d4,a0                ;add offset to source

ycliptf:     lsl.l     #4,d7
             move.l    d2,d0                ; set offset for block x
             swap.w    d0
             add.w     d0,d0
             lea       0(a0,d0.w),a0
             exg.l     d7,a2
             cmp.w     a3,d7
             bne.s     y_loop_pre

y_loop_f2:   move.l    d5,d0                ;set offset for block y
             swap.w    d0
             lsl.w     #7,d0
             move.w    0(a0,d0.w),(a1)
             suba.l    a4,a1                ; dec y
             sub.l     a2,d5
             bgt.s     y_loop_f2
             bra.s     y_cont_f

y_loop_pre:  tst.w     d7
             bpl.s     y_loop_f4

y_loop_f:    move.l    d5,d0                ;set offset for block y
             swap.w    d0
             lsl.w     #7,d0
             move.w    0(a0,d0.w),d4
             move.w    d4,d0
             lsr.w     d7,d0
             and.w     d6,d0
             sub.w     d0,d4
             sub.w     d0,d4
             sub.w     d0,d4
             move.w    d4,(a1)
             suba.l    a4,a1                ; dec y
             sub.l     a2,d5
             bgt.s     y_loop_f
             bra.s     y_cont_f

y_loop_f4:   move.l    d5,d0                ;set offset for block y
             swap.w    d0
             lsl.w     #7,d0
             move.w    0(a0,d0.w),d4
             move.w    d4,d0
             lsr.w     d7,d0
             and.w     d6,d0
             sub.w     d0,d4
             move.w    d4,(a1)
             suba.l    a4,a1                ; dec y
             sub.l     a2,d5
             bgt.s     y_loop_f4

y_cont_f:    exg.l     d7,a2
y_cont_ff:   subq.l    #2,32+60(sp)

             move.l    20+60(sp),d0         ;add to len
             sub.l     d0,24+60(sp)

             move.l    12+60(sp),d0         ; add to y
             sub.l     d0,16+60(sp)

             subq.l    #1,8+60(sp)
             sub.l     a6,d2
             bgt       x_loop_f
             bra       end_map


;x_loop_tpr:  cmpa.l    #$8000,a6
;             blt       x_lop_ttpp

x_loop_t:    move.l    a5,d5                ; ******** TRANS MODE *********

             move.l    8+60(sp),d7
             blt       end_map
             cmpi.w    #319,d7
             bgt       y_cont_tt

             move.l    a5,d7
             move.w    16+60(sp),d0
             sub.w     24+60(sp),d0

biggert:     lsr.l     #4,d7                ;shift if value is greater than one
             divu.w    d0,d7
             andi.l    #$ffff,d7
;             lsl.l     #4,d7                ;create ylen value (D7)

cont_smalt:  lea       tcolor,a1            ; set up color fading (D6)
             move.l    d7,d4
             lsl.l     #8,d4
             swap.w    d4

             move.w    brite,d6
             cmpi.w    #0,d6                ; turn off fade if zero
             bne.s     not_equ_z3
             clr.l     d4

not_equ_z3:  sub.w     d6,d4
             add.w     d4,d4
             movea.w   92(a1,d4.w),a2
             move.w    0(a1,d4.w),d6
             cmpa.w    #0,a2
             beq       y_cont_tt

             move.w    16+60(sp),d4         ;create y% offset
             cmp.w     y_value2,d4          ;compare bottom clip
             blt.s     yclipte
             move.w    d4,d0
             sub.w     y_value2,d0          ;sub ypos
             move.l    d7,d3                ;move ylen
             mulu.w    d0,d3                ;mul remainder and ylen
             lsl.l     #4,d3
             sub.l     d3,d5                ;sub ylen from by
             sub.w     d0,d4                ;sub difference to y

yclipte:     mulu.w    #640,d4              ;mult to screen
             movea.l   28+60(sp),a0         ;reset addrs
             movea.l   32+60(sp),a1
             adda.l    d4,a1

             move.w    24+60(sp),d4
             cmpi.w    #0,d4                ;compare top clip
             bgt.s     ycliptt
             subi.w    #0,d4                ;sub ypos
             neg.w     d4                   ;negate
             mulu.w    d7,d4                ;mul remainder and ylen
             lsl.l     #4,d4
             clr.w     d4
             sub.l     d4,d5                ;sub ylen from by
             swap.w    d4
             lsl.w     #7,d4
             adda.w    d4,a0                ;add offset to source

ycliptt:     lsl.l     #4,d7
             move.l    d2,d0                ; set offset for block x
             swap.w    d0
             add.w     d0,d0
             lea       0(a0,d0.w),a0
             exg.l     d7,a2
             cmp.w     a3,d7
             bne.s     y_loop_tp
             cmpa.l    a3,a2
             bge.s     y_loop_tp

             move.l    d5,d0
             swap.w    d0
             lsl.w     #7,d0
             adda.w    d0,a0
             move.l    #$10000,d6
             move.w    #128,d7

y_loop_tl:   move.w    (a0),d4
             beq.s     y_trans_tl
             suba.w    d7,a0

y_lop2_tl:   move.w    d4,(a1)
             suba.l    a4,a1                ; dec y
             sub.w     a2,d5
             bcc.s     y_lop2_tl
             sub.l     d6,d5
             bpl.s     y_loop_tl
             bra.s     y_cont_t

y_trans_tl:  suba.w    d7,a0
y_transtl:   suba.l    a4,a1                ; dec y
             sub.w     a2,d5
             bcc.s     y_transtl
             sub.l     d6,d5
             bpl.s     y_loop_tl
             bra.s     y_cont_t


y_loop_tp:   tst.w     d7
             bpl.s     y_loop_ta

y_loop_t:    move.l    d5,d0                ;set offset for block y
             swap.w    d0
             lsl.w     #7,d0
             move.w    0(a0,d0.w),d4
             beq.s     y_trans
             move.w    d4,d0
             lsr.w     d7,d0
             and.w     d6,d0
             sub.w     d0,d4
             sub.w     d0,d4
y_fade_t:    sub.w     d0,d4
             move.w    d4,(a1)
y_trans:     suba.l    a4,a1                ; dec y
             sub.l     a2,d5
             bgt.s     y_loop_t
             bra.s     y_cont_t

y_loop_ta:   move.l    d5,d0                ;set offset for block y
             swap.w    d0
             lsl.w     #7,d0
             move.w    0(a0,d0.w),d4
             beq.s     y_transa
             move.w    d4,d0
             lsr.w     d7,d0
             and.w     d6,d0
y_fade_ta:   sub.w     d0,d4
             move.w    d4,(a1)
y_transa:    suba.l    a4,a1                ; dec y
             sub.l     a2,d5
             bgt.s     y_loop_ta

y_cont_t:    exg.l     d7,a2
y_cont_tt:   subq.l    #2,32+60(sp)

             move.l    20+60(sp),d0         ;add to len
             sub.l     d0,24+60(sp)

             move.l    12+60(sp),d0         ; add to y
             sub.l     d0,16+60(sp)

             subq.l    #1,8+60(sp)
             sub.l     a6,d2
             bgt       x_loop_t

end_map:     movem.l   (sp)+,d0-d7/a0-a6
             rts       



;******** Floor Mapping  ******  w:(x1,y1,x2,y2,x3,y3,x4,y4),block,screen
loc:         .DC.w 0


floor_map:   movem.l   d0-d7/a0-a6,-(sp)

             clr.l     d3
             move.w    6+60(sp),d3          ; calculate top Y
             cmp.w     10+60(sp),d3
             ble.s     floor_sm1
             move.w    10+60(sp),d3
floor_sm1:   cmp.w     14+60(sp),d3
             ble.s     floor_sm2
             move.w    14+60(sp),d3
floor_sm2:   cmp.w     18+60(sp),d3
             ble.s     floor_sm3
             move.w    18+60(sp),d3
floor_sm3:   cmp.w     y_value2,d3
             bgt       fmap_end
             tst.w     d3
             bpl.s     floor_lg
             clr.w     d3

floor_lg:    move.w    6+60(sp),d6          ; calculate bottom Y
             cmp.w     10+60(sp),d6
             bge.s     floor_lg1
             move.w    10+60(sp),d6
floor_lg1:   cmp.w     14+60(sp),d6
             bge.s     floor_lg2
             move.w    14+60(sp),d6
floor_lg2:   cmp.w     18+60(sp),d6
             bge.s     floor_lg3
             move.w    18+60(sp),d6
floor_lg3:   tst.w     d6
             blt       fmap_end
             cmp.w     y_value2,d6
             ble.s     floor_lg4
             move.w    y_value2,d6
floor_lg4:   sub.w     d3,d6
             bmi       fmap_end

             lea       loc,a1
             move.w    d3,(a1)              ;store top for later
             mulu.w    #640,d3              ;calculate screen address
             add.l     d3,24+60(sp)
             addi.l    #4,20+60(sp)         ;offset block address


             move.w    4+60(sp),d2          ;x1
             move.w    12+60(sp),d3         ;x3
             move.w    8+60(sp),d4          ;x2
             move.w    16+60(sp),d5         ;x4

fmap_clip:   clr.l     d7                   ;calculate x-clip
             tst.w     d2
             bpl.s     fmap_xplus
             tst.w     d4
             bpl.s     fmap_xplus
             ori.w     #%1,d7
fmap_xplus:  cmpi.w    #319,d2
             ble.s     fmap_ymin
             cmpi.w    #319,d4
             ble.s     fmap_ymin
             ori.w     #%10,d7
fmap_ymin:   tst.w     d3
             bpl.s     fmap_yplus
             tst.w     d5
             bpl.s     fmap_yplus
             btst      #0,d7
             bne       fmap_end
fmap_yplus:  cmpi.w    #319,d3
             bls.s     f_clipend
             cmpi.w    #319,d5
             bls.s     f_clipend
             btst      #1,d7
             bne       fmap_end

f_clipend:   move.w    d6,d4
             moveq.l   #-1,d5
             lea       lines,a0

flr_clrl:    move.w    d5,(a0)              ; clear x's with -1's
             move.w    d5,6(a0)
             move.w    d5,12(a0)
             move.w    d5,18(a0)
             lea       24(a0),a0
             dbf       d4,flr_clrl

             lea       lines,a0
;             move.w    4+60(sp),d2          ;x1
;             move.w    12+60(sp),d3         ;x3
             move.w    6+60(sp),d4          ;y1
             move.w    14+60(sp),d5         ;y3
             clr.l     a1
             bsr       fmap_set

             lea       lines,a0
             adda.w    #6,a0
             move.w    4+60(sp),d2          ;x1
             move.w    8+60(sp),d3          ;x2
             move.w    6+60(sp),d4          ;y1
             move.w    10+60(sp),d5         ;y2
             moveq     #0,d7
             bset      #31,d7
             movea.l   d7,a1

;             bsr       fmap_clip            ; check 1&2 if out of screen
;             move.w    d7,4+60(sp)

             bsr       fmap_set

             lea       lines,a0
             adda.w    #12,a0
             move.w    12+60(sp),d2         ;x3
             move.w    16+60(sp),d3         ;x4
             move.w    14+60(sp),d4         ;y3
             move.w    18+60(sp),d5         ;y4
             moveq     #63,d7
             bset      #31,d7
             movea.l   d7,a1

;             bsr       fmap_clip            ; check 3&4 if out of screen
;             and.w     4+60(sp),d7          ; if both out dont continue
;             bne       fmap_end

             bsr       fmap_set

             lea       lines,a0
             adda.w    #18,a0
             move.w    8+60(sp),d2          ;x2
             move.w    16+60(sp),d3         ;x4
             move.w    10+60(sp),d4         ;y2
             move.w    18+60(sp),d5         ;y4
             movea.l   #63,a1
             bsr       fmap_set

             lea       lines,a2

fmap_loop:   lea       lines2,a0            ;move buffer to A0

             movea.l   a2,a6
             move.w    (a2),d0              ; calculate high x
             cmp.w     6(a2),d0
             bgt.s     floor_hi1
             lea       6(a2),a6
             move.w    6(a2),d0
floor_hi1:   cmp.w     12(a2),d0
             bgt.s     floor_hi2
             lea       12(a2),a6
             move.w    12(a2),d0
floor_hi2:   cmp.w     18(a2),d0
             bgt.s     floor_hi3
             lea       18(a2),a6
             move.w    18(a2),d0
floor_hi3:   subi.w    #320,d0
             tst.w     d0
             bmi       fmap_dcont

             movea.l   a2,a5
             move.l    #640,d1
             clr.l     d3

             move.w    (a2),d3
             cmp.l     d3,d1                ; calculate lo x
             ble.s     floor_lo0
             move.l    d3,d1
floor_lo0:   move.w    6(a2),d3
             cmp.l     d3,d1
             ble.s     floor_lo1
             lea       6(a2),a5
             move.l    d3,d1
floor_lo1:   move.w    12(a2),d3
             cmp.l     d3,d1
             ble.s     floor_lo2
             lea       12(a2),a5
             move.w    d3,d1
floor_lo2:   move.w    18(a2),d3
             cmp.l     d3,d1
             ble.s     floor_lo3
             lea       18(a2),a5
             move.l    d3,d1
floor_lo3:   cmpi.w    #640,d1
             bge       fmap_dcont

             subi.w    #320,d1
             move.w    d1,(a2)
             move.w    d0,6(a2)

             move.w    2(a5),d2             ;(d2)-x1
             move.w    4(a5),d4             ;(d4)-y1   ; get value from lines
             move.w    2(a6),d3             ;(d3)-x2
             move.w    4(a6),d5             ;(d5)-y2   ; get value from line2

fmap_nexg:   move.w    d3,d0                ;--------------
             sub.w     d2,d0                ; (D0)=DX
             bge.s     fmap_abs2
             neg.w     d0
fmap_abs2:   move.w    d5,d1
             sub.w     d4,d1                ; (D1)=dy
             bge.s     fmap_xdraw
             neg.w     d1

fmap_xdraw:  cmp.w     d1,d0                ; if dx>=dy  -- xindependant
             blt       fmap_ydraw

; ------------------ x indpendant routines

             cmp.w     d3,d2                ; if x1>x2
             ble.s     fmap_x_1

             move.l    #128,d7
             cmp.w     d4,d5                ; if y2<=y1
             bgt.s     fmap_x_c
             neg.l     d7
fmap_x_c:    movea.l   d7,a6                ; (a6)=offset
             movea.l   #-2,a5
             move.w    d4,d3                ; (D3)=y
             bra.s     fmap_xc

fmap_x_1:    move.l    #128,d7
             cmp.w     d4,d5                ; if y2<=y1
             bgt.s     fmap_x_1c
             neg.l     d7
fmap_x_1c:   movea.l   d7,a6
             movea.l   #2,a5
             move.w    d4,d3                ;(d2)already x (d3)=y

fmap_xc:     move.w    d1,d4                ; (D4)d2=dy*2 (inc1)
             add.w     d4,d4
             move.w    d4,d5                ; (d5)d=d2-dx
             sub.w     d0,d5
             sub.w     d0,d1
             add.w     d1,d1                ; (D1)yind inc2

             movea.l   20+60(sp),a1         ; calculate a1(block address)
             asl.w     #7,d3
             adda.w    d3,a1
             add.w     d2,d2
             adda.w    d2,a1
             adda.l    a5,a6

fmap_xloop:  move.w    (a1),(a0)+           ; X-LOOP----------------------

             tst.w     d5
             bpl.s     fmap_xlp_d
             add.w     d4,d5                ; dec false
             adda.l    a5,a1
             dbf       d0,fmap_xloop
             bra       fmap_dcont2

fmap_xlp_d:  add.w     d1,d5                ; dec true
             adda.l    a6,a1
             dbf       d0,fmap_xloop
             bra       fmap_dcont2

;------------------ y independant routines

fmap_ydraw:  cmp.w     d5,d4                ; if y1>y2
             ble.s     fmap_y_1
             movea.l   #-128,a5

             moveq.l   #2,d7
             cmp.w     d2,d3                ; if x2<=x1
             bgt.s     fmap_y_c
             neg.l     d7
fmap_y_c:    movea.l   d7,a6                ; (a6)=offset
             move.w    d4,d3                ; (D3)=y
             bra.s     fmap_yc

fmap_y_1:    movea.l   #128,a5
             moveq.l   #2,d7
             cmp.w     d2,d3                ; if x2<=x1
             bgt.s     fmap_y_1c
             neg.l     d7
fmap_y_1c:   movea.l   d7,a6
             move.w    d4,d3                ;(d2)already x (d3)=y

fmap_yc:     move.w    d0,d4                ; (D4)d2=dx*2 (inc1)
             add.w     d4,d4
             move.w    d4,d5                ; (d5)d=d2-dy
             sub.w     d1,d5
             sub.w     d1,d0
             add.w     d0,d0                ; (D0)yind inc2
;                                         ; (d1)=dy / inc2(d0)

             movea.l   20+60(sp),a1         ; calculate a1(block address)
             asl.w     #7,d3
             adda.w    d3,a1
             add.w     d2,d2
             adda.w    d2,a1
             adda.l    a5,a6


fmap_yloop:  move.w    (a1),(a0)+           ; Y-LOOP---------------------

             tst.w     d5
             bpl.s     fmap_ylp_d
             add.w     d4,d5                ; dec false
             adda.l    a5,a1
             dbf       d1,fmap_yloop
             bra.s     fmap_dcont2

fmap_ylp_d:  add.w     d0,d5                ; dec true
             adda.l    a6,a1
             dbf       d1,fmap_yloop

fmap_dcont2: lea       lines2,a3            ; ----DRAW from buffer to screen

             cmpa.l    a3,a0
             ble       fmap_dcont

             move.w    (a2),d0              ; get x's
             move.w    6(a2),d1

             movea.l   24+60(sp),a1

fmap_xlrg:   sub.w     d0,d1

             add.w     d0,d0                ;calc offset to screen
             adda.w    d0,a1

             move.l    a0,d0
             sub.l     a3,d0
             bclr      #0,d0
             lsl.l     #7,d0
             addq.w    #1,d1
             divu.w    d1,d0
             subq.w    #1,d1
             andi.l    #$ffff,d0
             lsl.l     #8,d0                ;(d0)=inc%

             clr.l     d2                   ;sx%=(d2)

             move.w    (a2),d7
             bge.s     fmap_nlclp
             neg.w     d7
             sub.w     d7,d1                ; reset dx
             blt.s     fmap_dcont

             move.l    d0,d2
             asr.l     #8,d2
             mulu.w    d7,d2
             asl.l     #8,d2                ;redew sx
             add.w     d7,d7
             adda.w    d7,a1                ;modify screen

fmap_nlclp:  move.w    6(a2),d7
             cmpi.w    #319,d7
             ble.s     fmap_large
             subi.w    #319,d7
             sub.w     d7,d1                ;modify dx
             blt.s     fmap_dcont

fmap_large:  cmpi.l    #$e000,d0
             bgt.s     fmap_nldlp

             move.l    #$10000,d5           ;enlarge

             move.l    d2,d7
             swap.w    d7
             add.w     d7,d7
             lea       0(a3,d7.w),a3

fmap_loop2l: move.w    (a3)+,d7             ;-Draw enlarged screen loop
fmp_nontran: move.w    d7,(a1)+
             subq.w    #1,d1
             add.w     d0,d2
             bcc.s     fmp_nontran
             add.l     d5,d2
             tst.w     d1
             bpl.s     fmap_loop2l
             bra.s     fmap_dcont

fmap_nldlp:  move.l    d2,d7                ;-Draw to screen loop
             swap.w    d7
             add.w     d7,d7
             move.w    0(a3,d7.w),(a1)+
             add.l     d0,d2
             dbf       d1,fmap_nldlp

fmap_dcont:  adda.w    #24,a2
             addi.l    #640,24+60(sp)
             dbf       d6,fmap_loop

fmap_end:    movem.l   (sp)+,d0-d7/a0-a6
             rts       

;-----------floor/ceiling map settings

fmap_set:    addi.w    #320,d3              ; keep x from going neg.
             addi.w    #320,d2
             move.w    d3,d0
             sub.w     d2,d0                ; (D0)=DX
             bge.s     fmap_abs
             neg.w     d0
fmap_abs:    move.w    d5,d1
             sub.w     d4,d1                ; (D1)=dy
             bge.s     fmap_xind
             neg.w     d1

fmap_xind:   cmp.w     d1,d0                ; if dx>dy  -- xindependant
             ble       fmap_yind

             move.l    #65535,d7
             tst.w     d0
             bls.s     fmp_xlopc

             divu.w    d0,d7
             andi.l    #$ffff,d7

fmp_xlopc:   lsl.l     #6,d7
             addi.l    #63,d7

             cmp.w     d5,d4                ; if y1>y2
             ble.s     fmp_x_1

             movea.l   #4194300,a3          ;sx%(a3)=64
             neg.l     d7
             movea.l   d7,a2                ;(a2)=-inc%

             moveq     #1,d7
             cmp.w     d2,d3                ; if x2>x1
             ble.s     fmp_x_c
             neg.l     d7
fmp_x_c:     movea.l   d7,a6                ; (a6)=offset
             move.w    d3,d2                ; (d2)=x
             move.w    d5,d3                ; (D3)=y
             bra.s     fmp_xc

fmp_x_1:     movea.l   d7,a2                ;(a2)=inc%
             clr.l     a3                   ;(a3)sx%=0

             moveq     #1,d7
             cmp.w     d2,d3                ; if x2<=x1
             bgt.s     fmp_x_1c
             neg.l     d7
fmp_x_1c:    movea.l   d7,a6
             move.w    d4,d3                ;(d2)already x (d3)=y

fmp_xc:      move.w    d3,d7
             lea       loc,a4
             sub.w     (a4),d7
             blt.s     fmp_xmul
             mulu.w    #24,d7
             adda.l    d7,a0

fmp_xmul:    move.w    d1,d7                ; (D7)temp d2=dy*2
             add.w     d7,d7
             move.w    d7,d5                ; (d5)d=d2-dx
             sub.w     d0,d5
             movea.w   d7,a5                ; (A5)=inc1
             move.w    d1,d7
             sub.w     d0,d7
             add.w     d7,d7
             movea.w   d7,a4                ; (a4)=inc2
             addq.w    #1,d0

             move.l    a1,d4                ; move position to d4

fmp_xloop:   tst.w     d5                   ;if d<0
             bpl.s     fmp_xdec
             add.w     a5,d5                ;d=d+inc
             add.w     a6,d2
             adda.l    a2,a3
             dbf       d0,fmp_xloop
             rts       

fmp_xdec:    add.w     a4,d5                ;d=d+inc2
             tst.w     d3
             bmi.s     fmp_xndb
             cmp.w     y_value,d3
             bge       fmp_end
             move.w    d2,(a0)+
             tst.l     d4
             bpl.s     fmp_xnda

             move.w    d4,(a0)+             ;x=static y=variable
             move.l    a3,d7
             swap.w    d7
             move.w    d7,(a0)+
             adda.w    #18,a0
             bra.s     fmp_xndb

fmp_xnda:    move.l    a3,d7                ;x=variable y=static
             swap.w    d7
             move.w    d7,(a0)+
             move.w    d4,(a0)+
             adda.w    #18,a0

fmp_xndb:    addq.w    #1,d3
             add.w     a6,d2
             adda.l    a2,a3
             dbf       d0,fmp_xloop
             rts       



fmap_yind:   move.l    #65535,d7
;             tst.w     d1
;             bls.s     fmp_ylopc
             addq.w    #1,d1
             divu.w    d1,d7
             andi.l    #$ffff,d7

fmp_ylopc:   lsl.l     #6,d7
             cmp.w     d5,d4                ; if y1>y2   ---- y independant
             ble.s     fmp_y_1

             movea.l   #4194300,a3          ;sx%(a3)=64
             neg.l     d7
             movea.l   d7,a2                ;(a2)=-inc%

             moveq     #1,d7
             cmp.w     d2,d3                ; if x2>x1
             ble.s     fmp_y_c
             neg.l     d7
fmp_y_c:     movea.l   d7,a6                ; (a6)=offset
             move.w    d3,d2                ; (d2)=x
             move.w    d5,d3                ; (D3)=y
             bra.s     fmp_yc

fmp_y_1:     clr.l     a3
             movea.l   d7,a2

             moveq     #1,d7
             cmp.w     d2,d3                ; if x2<=x1
             bgt.s     fmp_y_1c
             neg.l     d7
fmp_y_1c:    movea.l   d7,a6
             move.w    d4,d3                ;(d2)already x (d3)=y

fmp_yc:      move.w    d3,d7
             lea       loc,a4
             sub.w     (a4),d7
             blt.s     fmp_ymul
             mulu.w    #24,d7
             adda.l    d7,a0

fmp_ymul:    subq.w    #1,d1
             move.w    d0,d7                ; (D7)temp d2=dx*2
             add.w     d7,d7
             move.w    d7,d5                ; (d5)d=d2-dy
             sub.w     d1,d5
             movea.w   d7,a5                ; (A5)=inc1
             move.w    d0,d7
             sub.w     d1,d7
             add.w     d7,d7
             movea.w   d7,a4                ; (a4)=inc2
             addq.w    #1,d1

             move.l    a1,d4                ; move position to d4

fmp_yloop:   tst.w     d3
             bmi.s     fmp_yndb
             cmp.w     y_value,d3
             bge       fmp_end

             move.w    d2,(a0)+
             tst.l     d4
             bpl.s     fmp_ynda

             move.w    d4,(a0)+             ;x=static y=variable
             move.l    a3,d7
             swap.w    d7
             move.w    d7,(a0)+
             adda.w    #18,a0
             bra.s     fmp_yndb

fmp_ynda:    move.l    a3,d7                ;x=variable y=static
             swap.w    d7
             move.w    d7,(a0)+
             move.w    d4,(a0)+
             adda.w    #18,a0

fmp_yndb:    adda.l    a2,a3
             addq.w    #1,d3

             tst.w     d5                   ;if d<0
             bpl.s     fmp_ydec
             add.w     a5,d5                ;d=d+inc
             dbf       d1,fmp_yloop
             rts       

fmp_ydec:    add.w     a4,d5                ;d=d+inc2
             add.w     a6,d2
             dbf       d1,fmp_yloop

fmp_end:     rts       


; ********  Z-sort routine ******** lk_addr%,vt_addr%,gr_offst%,w:objects&

z_sort_rout: movem.l   d0-d7/a0-a6,-(sp)
             movea.l   4+60(sp),a0          ; get lookup addr
             movea.l   8+60(sp),a1          ; get vector addr
             addq.l    #4,a1
             movea.l   12+60(sp),a2         ; get graphic offset
             move.w    16+60(sp),d0         ;get number of objects
             beq       z_end
             subq.w    #1,d0                ;subtract for DBF
             clr.l     d4

ave_z:       move.w    (a0)+,d1             ;get graphic
             move.w    d4,(a0)+             ;set offset
             move.w    (a1),d3              ;get first z value

             cmpi.w    #256,d1              ;cmp 4 vector
             blt.s     lamp_4v
;             cmpi.w    #2048,d1
;             bgt.s     lamp_4v
             moveq     #12,d2               ; set two vector object
             subi.w    #32,d3
;             cmpi.w    #256,d1
;             blt.s     cont_z
;             subi.w    #36,d3
             bra       cont_z

lamp_4v:     add.w     18(a1),d3            ;get second z value and ave
             asr.w     #1,d3
;             cmpi.w    #22,d1
;             blt.s     lamp_4cn
;             subi.w    #36,d3

lamp_4cn:    moveq     #24,d2

cont_z:      move.w    d3,2(a0)             ; move ave z to lookup
             addq.l    #4,a0
             adda.w    d2,a1
             add.w     d2,d4
             dbf       d0,ave_z

             movea.l   4+60(sp),a0
             move.w    16+60(sp),d0
             subq.w    #1,d0

sort_out:    movea.l   a0,a1
             adda.w    #14,a1               ;setup quick inner loop (no +6)

             clr.l     a4

             move.w    d0,d1
             subq.w    #1,d1
             blt.s     cont_sort2
             move.w    6(a0),d3

sort_in:     move.w    (a1),d4
             cmp.w     d4,d3
             bge.s     cont_sort
             move.w    d4,d3
             movea.l   a1,a4

cont_sort:   addq.l    #8,a1
             dbf       d1,sort_in

             cmpi.w    #-56,d3              ;-22 -48
             blt.s     z_end

             cmpa.l    #0,a4
             beq.s     cont_sort2

             subq.l    #6,a4                ; correct for quick inner loop +6
             move.l    a4,d2
             sub.l     a0,d2
             lsr.l     #1,d2
             movea.l   a2,a5
             adda.l    d2,a5

             move.l    (a2),d2              ; sort graph_offest
             move.l    (a5),(a2)
             move.l    d2,(a5)

             move.l    (a0),d2              ; sort lk_table
             move.l    4(a0),d3
             move.l    (a4),(a0)
             move.l    4(a4),4(a0)
             move.l    d2,(a4)+
             move.l    d3,(a4)

cont_sort2:  addq.l    #8,a0
             addq.l    #4,a2
             dbf       d0,sort_out
z_end:       movem.l   (sp)+,d0-d7/a0-a6
             rts       

;***************** Sound Rotate and Translate ** w:x&,w:z&,w:rot&,w:xs&,w:zs&

sound_trans: movem.l   d1-d7/a0-a6,-(sp)

             movea.w   56+4(sp),a5          ;(a5) =player
             movea.w   56+6(sp),a6          ;(a6) =player

             move.w    56+8(sp),d1          ; rot
             andi.w    #$fffc,d1            ; AND to 4deg.
             lea       cos_sin,a3
             adda.w    d1,a3
             move.w    (a3)+,d1
             move.w    (a3),d2

             move.w    56+10(sp),d5         ; calc cos_sin on x
             sub.w     a5,d5                ; translate player x pos
             move.w    d5,d7                ; copy x to d7 for use on Z

             muls.w    d2,d5                ;x*cos
             asr.l     #8,d5
             move.w    56+12(sp),d6
             sub.w     a6,d6                ; translate player z pos
             move.w    d6,d3                ; copy z for use on Z
             muls.w    d1,d3                ;z*sin
             asr.l     #8,d3
             add.w     d5,d3                ;x*cos+z*sin

             muls.w    d1,d7                ;calculate sin/cos on z
             neg.l     d7                   ;x*-sin
             asr.l     #8,d7
             muls.w    d2,d6                ;z*cos
             asr.l     #8,d6
             add.w     d7,d6

             move.w    d3,d0
             swap.w    d0
             move.w    d6,d0

             movem.l   (sp)+,d1-d7/a0-a6
             rts       

;***************** Rotate and Translate Vectors ** x&,z&,w:vectr&,w:degree,addr%
; w:+20=yslide,y:ymove
y_rot_trans: movem.l   d0-d7/a0-a6,-(sp)
             movea.l   60+4(sp),a5          ;(A5) = player x value
             movea.l   60+8(sp),a6          ;(A6) = player z value

             movea.w   60+22(sp),a1         ;(a1/a2)move/slide
             movea.w   60+20(sp),a2         ;80 - 39

             move.w    60+12(sp),d0
             beq       rot_end
             subi.w    #1,d0                ;(D0) = count of vectors
             movea.l   60+16(sp),a0         ;(A0) = start of array

             move.w    60+14(sp),d1
             lea       cos_sin,a3
             move.w    d1,d2
             andi.w    #3,d2
             beq.s     no_cos_int

             andi.w    #$fffc,d1            ; interpolated sin and cos
             adda.w    d1,a3
             move.w    (a3)+,d1
             move.w    (a3)+,d2
             add.w     (a3)+,d1
             add.w     (a3),d2
             asr.w     #1,d1
             asr.w     #1,d2
             bra.s     rot_loop

no_cos_int:  adda.w    d1,a3
             move.w    (a3)+,d1             ;(D1) sin value
             move.w    (a3),d2              ;(D2) cos value

rot_loop:    move.w    (a0),d5              ; calc cos_sin on x
             sub.w     a5,d5                ; translate player x pos
             move.w    d5,d7                ; copy x to d7 for use on Z

             muls.w    d2,d5                ;x*cos
             asr.l     #8,d5
             move.w    4(a0),d6
             sub.w     a6,d6                ; translate player z pos
             move.w    d6,d3                ; copy z for use on Z
             muls.w    d1,d3                ;z*sin
             asr.l     #8,d3
             add.w     d5,d3
;                                            x*cos+z*sin
             muls.w    d1,d7                ;calculate sin/cos on z
             neg.l     d7                   ;x*-sin
             asr.l     #8,d7
             muls.w    d2,d6                ;z*cos
             asr.l     #8,d6
             add.w     d7,d6                ;x*-sin+z*cos

             bgt.s     rot_xcont3           ;setup -z stuff

             asr.w     #2,d6
             cmpi.w    #-80,d6
             bgt.s     rot_xcont3
             clr.l     (a0)+
             move.w    #-100,(a0)+
             bra       rot_cnt

rot_xcont3:  move.w    d6,4(a0)
             asr.w     #1,d6
rot_xex:     moveq     #-54,d7              ;calculate z value for perspective
             sub.w     d6,d7                ;z/(-54-z)
             ext.l     d6
             asl.l     #8,d6
             divs.w    d7,d6
;                                           calculate x screen coord.
xcont2:      move.w    d3,d7                ;(x-(-x)*z)+164
             neg.w     d7
             muls.w    d6,d7
             asr.l     #7,d7
             add.w     d3,d3                ; mult by 2 for jag
             sub.w     d7,d3
             addi.w    #160,d3
             move.w    d3,(a0)+

             move.w    (a0),d5

             move.w    a1,d7                ;(y-(80-y)*z)-24

             sub.w     d5,d7
             muls.w    d6,d7
             ext.l     d5
             asl.l     #8,d5
             sub.l     d7,d5
             move.l    d5,d6
             asr.l     #1,d6
             add.l     d6,d5
             asr.l     #8,d5
             sub.w     a2,d5                ;(39
             move.w    d5,(a0)

             addq.l    #4,a0

rot_cnt:     dbf       d0,rot_loop

rot_end:     movem.l   (sp)+,d0-d7/a0-a6
             rts       


;***************  Mouse Handler *************

mouse_hand:  move.l    a0,-(sp)
             lea       mouse_var(pc),a0
             move.l    a0,d0
             movea.l   (sp)+,a0
             rts       

mouse_var:   .DC.w 0,0,0,0

mouse_int:   movem.l   d1/a1,-(sp)
             lea       mouse_var(pc),a1
             move.w    6(a1),d1
             move.b    (a0)+,d0
             andi.w    #7,d0
             move.w    d0,(a1)+
             move.b    (a0)+,d0
             ext.w     d0
             cmpi.w    #1,d0
             beq.s     ms_no1
             asr.w     d1,d0
ms_no1:      add.w     d0,(a1)+

             move.b    (a0)+,d0
             ext.w     d0
             cmpi.w    #1,d0
             beq.s     ms_no2
             asr.w     d1,d0
ms_no2:      add.w     d0,(a1)+
             movem.l   (sp)+,d1/a1
             rts       


;******* Temp Variables
             .EVEN 

dunv_data:   .DC.l 0,0,0,0,0,0

;******* TRUE COLOR FADER *******
;13+(st0)21
             .DC.w 0,0,0,0

tcolor:      .DC.w 0,0,0,0,0,0,0,0
             .DC.w 0,0,0,0,0,0,0,0
             .DC.w %0000000000000000,%0000000000000000
clr_color:   .DC.w %0000100001100001
             .DC.w %0001100011100011,%0000100001100001,%0011100111100111
             .DC.w %0001100011100011,%0111101111101111
;                             ^                 ^                 ^
             .DC.w %0011100111100111,0,0,0,0,0,0,0
             .DC.w 0,0,0,0,0,0,0,0,0,0,$ffff,$ffff,$ffff,$ffff

             .DC.w $ffff,$ffff,$ffff
tcolorlsr:   .DC.w $ffff,$ffff,$ffff
             .DC.w $ffff,$ffff,$ffff,$ffff,$ffff,$ffff
             .DC.w $ffff,$ffff,$ffff,$ffff,$ffff,$ffff,4,3,$8004,2
             .DC.w $8003,1
             .DC.w $8002,0,0,0,0,0,0,0,0,0,0,0,0,0,0
             .DC.w 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
;********************************************


;********** COS - SIN  Table **********

cos_sin:     .DC.w 0,256,18,255,36,254,53,250,71,246,88,241,104,234
             .DC.w 120,226,136,217,150,207,165,196,178,184,190,171
             .DC.w 202,158,212,143,222,128,230,112,237,96,243,79,248,62
             .DC.w 252,44,255,27,256,9,256,-9,255,-27,252,-44,248,-62
             .DC.w 243,-79,237,-96,230,-112,222,-128,212,-143,202,-158
             .DC.w 190,-171,178,-184,165,-196,150,-207,136,-217,120,-226
             .DC.w 104,-234,88,-241,71,-246,53,-250,36,-254,18,-255,0,-256
             .DC.w -18,-255,-36,-254,-53,-250,-71,-246,-88,-241,-104,-234
             .DC.w -120,-226,-136,-217,-150,-207,-165,-196,-178,-184
             .DC.w -190,-171,-202,-158,-212,-143,-222,-128,-230,-112
             .DC.w -237,-96,-243,-79,-248,-62,-252,-44,-255,-27,-256,-9
             .DC.w -256,9,-255,27,-252,44,-248,62,-243,79,-237,96,-230,112
             .DC.w -222,128,-212,143,-202,158,-190,171,-178,184,-165,196
             .DC.w -150,207,-136,217,-120,226,-104,234,-88,241,-71,246
             .DC.w -53,250,-36,254,-18,255,0,256,18,255
;********************************************

clr_fade1:   .DC.b 2,4,6,8,10,12
clr_fade2:   .DC.b 12,10,8,6,4,4,2,2,0,0,0

             .EVEN 
;********************************************
;********** Fade Table ***********

look_up:     .DC.w %1111111111111111,%1111011110011110,%1101011010011010
             .DC.w %0101001010001010,%0001000010000010,%0000000000000000

;*******************************
;******************************
             .DC.l 1,2,3,4

lines:       .DS.b 4800
lines2:      .DS.b 256


