
maxscrdiv EQU 8
max3ddiv EQU 5
playerheight EQU 12*1024
playercrouched EQU 6*1024
scrheight EQU 80

; k/j/m
; 4/8
; s/x
; b/n

xpos EQU 0	;l
zpos EQU 4	;l
zsinval EQU 8	;w
zcosval EQU 10	;w
ztox EQU 12	;l
xsinval EQU 16	;w
xcosval EQU 18	;w
xspd EQU 20	;w
zspd EQU 22	;w
mapx EQU 24	;b
mapz EQU 25	;b
whichtile EQU 26 ;b
xofflight EQU 28	;w
zofflight EQU 30	;w
offlight EQU 32		;w
zlinestore EQU 34
zlinedir EQU 38
zposdir EQU 40
zposstore EQU 42
xdiststore EQU 44
xdistdir EQU 46
zwallfound EQU 48

xlinestore EQU 50
xlinedir EQU 54
xposdir EQU 56
xposstore EQU 58
zdiststore EQU 60
zdistdir EQU 62
xwallfound EQU 64


midoffset EQU 104*4*40

 SECTION Scrn,CODE
OpenLib         equ -552
CloseLib        equ -414

vhposr		equ $006	
vhposrl		equ $007 
bltcon0		equ $40 
bltcon1		equ $42
bltcpt		equ $48
bltbpt		equ $4c
bltapt		equ $50
spr0ctl		equ $142
spr1ctl		equ $14a
spr2ctl		equ $152
spr3ctl		equ $15a
spr4ctl		equ $162
spr5ctl		equ $16a
spr6ctl		equ $172
spr7ctl		equ $17a
spr0pos		equ $140
spr1pos		equ $148
spr2pos		equ $150
spr3pos		equ $158
spr4pos		equ $160
spr5pos		equ $168
spr6pos		equ $170
spr7pos		equ $178
bltdpt     	equ $54
bltafwm		equ $44
bltalwm		equ $46
bltsize     	equ $58
bltcmod     	equ $60
bltbmod     	equ $62
bltamod     	equ $64
bltdmod     	equ $66
diwstart        equ $8e         ; Screen hardware registers.
diwstop         equ $90
ddfstart        equ $92
ddfstop         equ $94
bplcon0         equ $100
bplcon1         equ $102
col0            equ $180
col1            equ $182
col2		equ $184
col3		equ $186
col4		equ $188
col5		equ $18a
col6		equ $18c
col7		equ $18e
col8            equ $190
col9            equ $192
col10           equ $194
dmacon		equ $96
dmaconr		equ $002
intenar		equ $01c
intena		equ $09a
intreq		equ $09c
intreqr		equ $01e
intreqrl	equ $01f
bpl1pth         equ $e0
bpl1ptl         equ $e2
bpl2pth		equ $e4
bpl2ptl		equ $e6
bpl3pth		equ $e8
bpl3ptl		equ $ea
bpl4pth		equ $ec
bpl4ptl		equ $ee
bpl5pth		equ $f0
bpl5ptl		equ $f2
bpl6pth		equ $f4
bpl6ptl		equ $f6
bpl7pth		equ $f8
bpl7ptl		equ $fa
bpl8pth		equ $fc
bpl8ptl		equ $fe
spr0pth		equ $120
spr0ptl		equ $122
spr1pth		equ $124
spr1ptl		equ $126
spr2pth		equ $128
spr2ptl		equ $12a
spr3pth		equ $12c
spr3ptl		equ $12e
spr4pth		equ $130
spr4ptl		equ $132
spr5pth		equ $134
spr5ptl		equ $136
spr6pth		equ $138
spr6ptl		equ $13a
spr7pth		equ $13c
spr7ptl		equ $13e

; move.l #length,d0
; moveq.l #2,d1	; chipmem
; move.l 4.w,a6
; jsr allocmem(a6) = -198

; tst.l d0
; beq.s ohbugger
; move.l d0,memaddr


; move.l 4.w,a6
; move.l memaddr,a1
; move.l #size,d0
; jsr freemem(a6) =-210


** This waits for the blitter to finish before allowing program
** execution to continue.

WB MACRO
\@bf:
 btst #6,dmaconr(a6)
 bne.s \@bf
 ENDM

*Another version for when d6 <> dff000

WBSLOW MACRO
\@bf:
 btst #6,$dff000+dmaconr
 bne.s \@bf
 ENDM

WT MACRO
\@bf:
 btst #6,(a3)
 bne.s \@bd
 rts
\@bd: 
 btst #4,(a0)
 beq.s \@bf
 ENDM

WTNOT MACRO
\@bf:
 btst #6,(a3)
 bne.s \@bd
 rts
\@bd: 
 btst #4,(a0)
 bne.s \@bf
 ENDM
 
**

 include "macros.i"
 include "ab3:source/defs.i"

* Load level into buffers.
 clr.b doanything

 move.l 4.w,a6
 move.l #doslibname,a1
 moveq #0,d0
 jsr -552(a6)
 move.l d0,doslib

; bra noload

 move.l d0,a6
 move.l #LDname,d1
 move.l #1005,d2
 jsr -30(a6)
 move.l d0,LDhandle

 move.l doslib,a6
 move.l d0,d1
 move.l #LEVELDATA,d2
 move.l #70000,d3
 jsr -42(a6)

 move.l doslib,a6
 move.l LDhandle,d1
 jsr -36(a6)

********

 move.l doslib,a6
 move.l #LGname,d1
 move.l #1005,d2
 jsr -30(a6)
 move.l d0,LGhandle

 move.l doslib,a6
 move.l d0,d1
 move.l #LEVELGRAPHICS,d2
 move.l #30000,d3
 jsr -42(a6)

 move.l doslib,a6
 move.l LGhandle,d1
 jsr -36(a6)

********

 move.l doslib,a6
 move.l #LCname,d1
 move.l #1005,d2
 jsr -30(a6)
 move.l d0,LChandle

 move.l doslib,a6
 move.l d0,d1
 move.l #LEVELCLIPS,d2
 move.l #50000,d3
 jsr -42(a6)

 move.l doslib,a6
 move.l LChandle,d1
 jsr -36(a6)

*******

noload:

********

; move.l doslib,a6
; move.l #Prefsname,d1
; move.l #1005,d2
; jsr -30(a6)
; move.l d0,Prefshandle

; move.l doslib,a6
; move.l d0,d1
; move.l #Prefsfile,d2
; move.l #50,d3
; jsr -42(a6)

; move.l doslib,a6
; move.l Prefshandle,d1
; jsr -36(a6)

*******

 cmp.b #'s',Prefsfile+2
 seq STEREO

 move.l doslib,d0
 move.l d0,a1
 move.l 4.w,a6
 jsr CloseLib(a6)

 jmp stuff
endstuff:

 move.l #$dff000,a6    ; NB V. IMPORTANT: A6=CUSTOM BASE
 move.w #$87c0,dmacon(a6)
 move.w #$8020,dmacon(a6)
 move.w intenar(a6),saveinters
 move.w #$7fff,intena(a6)
 move.w #$00ff,$dff09e

*** Put myself in supervisor mode

 move.l #blag,$80
 trap #0
; move.l $6c,d0
; move.l #blag,$6c
; move.w #$8010,intreq(a6)

 rts
 
saveit: ds.l 10
doslibname: dc.b 'dos.library',0
 even
doslib: dc.l 0

LDname: dc.b 'ab3:includes/tstlev.bin',0
 even
LDhandle: dc.l 0
LGname: dc.b 'ab3:includes/tstlev.graph.bin',0
 even
LGhandle: dc.l 0
LCname: dc.b 'ab3:includes/tstlev.clips',0
 even
LChandle: dc.l 0

Prefsname: dc.b 'ram:prefs',0
 even
Prefshandle: dc.l 0

Prefsfile:
 dc.b 'k8nx'
 ds.b 50
 
 even

blag:
; move.w #$10,intreq(a6)
; move.l d0,$6c
; move.w #$7fff,intena(a6)

 move.w #$20,$dff1dc

 move.l $6c,saveit
 move.l #Chan0inter,$6c
 jsr KInt_Init
 

 
****************************
* Initialize level
****************************
* Poke all clip offsets into
* correct bit of level data.
****************************
 lea.l LEVELGRAPHICS,a0
 move.l 12(a0),a1
 add.l a0,a1
 move.l a1,ZoneGraphAdds
 move.l (a0),a1
 add.l a0,a1
 move.l a1,DoorData
 move.l 4(a0),a1
 add.l a0,a1
 move.l a1,LiftData
 move.l 8(a0),a1
 add.l a0,a1
 move.l a1,SwitchData
 adda.w #16,a0
 move.l a0,ZoneAdds

 lea.l LEVELDATA,a1
 move.l 16(a1),a2
 add.l a1,a2
 move.l a2,Points
 move.l 20(a1),a2
 add.l a1,a2
 move.l a2,FloorLines
 move.l 24(a1),a2
 add.l a1,a2
 move.l a2,ObjectData
 move.l 28(a1),a2
 add.l a1,a2
 move.l a2,PlayerShotData
 move.l 32(a1),a2
 add.l a1,a2
 move.l a2,NastyShotData
 move.l 36(a1),a2
 add.l a1,a2
 move.l a2,ObjectPoints  
 move.l 40(a1),a2
 add.l a1,a2
 move.l a2,PLR1_Obj
 move.l 44(a1),a2
 add.l a1,a2
 move.l a2,PLR2_Obj
 move.w 14(a1),NumObjectPoints

; bra noclips
  
 lea.l LEVELCLIPS,a2
 moveq #0,d0
 move.w 10(a1),d7	;numzones
assignclips:
 move.l (a0)+,a3
 add.l a1,a3	; pointer to a zone
 adda.w #ToListOfGraph,a3 ; pointer to zonelist
dowholezone:
 tst.w (a3)
 blt.s nomorethiszone
 tst.w 2(a3)
 blt.s thisonenull

 move.l d0,d1
 asr.l #1,d1
 move.w d1,2(a3)

findnextclip:
 cmp.w #-2,(a2,d0.l)
 beq.s foundnextclip
 addq.l #2,d0
 bra.s findnextclip
foundnextclip
 addq.l #2,d0

thisonenull:
 addq #8,a3 
 bra.s dowholezone
nomorethiszone:
 dbra d7,assignclips
 
 lea (a2,d0.l),a2
 move.l a2,CONNECT_TABLE
 
noclips:

* Put in addresses of glowything

 move.l red_des,a0
 move.w (a0),d0
 addq #1,d0
 muls #6,d0
 lea 2(a0,d0.w),a1
 move.l red_des+4,a0
 move.l a1,(a0)
 move.w #0,(a1)

 move.l yellow_des,a0
 move.w (a0),d0
 addq #1,d0
 muls #6,d0
 lea 2(a0,d0.w),a1
 move.l yellow_des+4,a0
 move.l a1,(a0)
 move.w #0,(a1)
 
 move.l green_des,a0
 move.w (a0),d0
 addq #1,d0
 muls #6,d0
 lea 2(a0,d0.w),a1
 move.l green_des+4,a0
 move.l a1,(a0)
 move.w #0,(a1)
 
 move.l blue_des,a0
 move.w (a0),d0
 addq #1,d0
 muls #6,d0
 lea 2(a0,d0.w),a1
; move.l #protinit-750000,a6
 move.l blue_des+4,a0
 move.l a1,(a0)
 move.w #0,(a1)


************************************
 
 cmp.b #'k',Prefsfile
 bne.s nkb
 st PLR1KEYS
 clr.b PLR1PATH
 clr.b PLR1MOUSE
 clr.b PLR1JOY
nkb:
 cmp.b #'m',Prefsfile
 bne.s nmc
 clr.b PLR1KEYS
 clr.b PLR1PATH
 st PLR1MOUSE
 clr.b PLR1JOY
nmc:
 cmp.b #'j',Prefsfile
 bne.s njc
 clr.b PLR1KEYS
 clr.b PLR1PATH
 clr.b PLR1MOUSE
 st PLR1JOY
njc:
 
 clr.b PLR1_StoodInTop
 move.l #playerheight,PLR1s_height
 
 move.l #empty,pos1LEFT
 move.l #empty,pos2LEFT
 move.l #empty,pos1RIGHT
 move.l #empty,pos2RIGHT
 move.l #emptyend,Samp0endLEFT
 move.l #emptyend,Samp1endLEFT
 move.l #emptyend,Samp0endRIGHT
 move.l #emptyend,Samp1endRIGHT
 
 move.l #nullspr,d0
 move.w d0,s4l
 move.w d0,s5l
 move.w d0,s6l
 move.w d0,s7l
 swap d0
 move.w d0,s4h
 move.w d0,s5h
 move.w d0,s6h
 move.w d0,s7h 
 
 move.l #nullline,d0
 move.w d0,n1l
 swap d0
 move.w d0,n1h
 
 move.l #Panel,d0
 move.w d0,p1l
 swap d0
 move.w d0,p1h
 move.l #Panel+40,d0
 move.w d0,p2l
 swap d0
 move.w d0,p2h
 move.l #Panel+40*2,d0
 move.w d0,p3l
 swap d0
 move.w d0,p3h
 move.l #Panel+40*3,d0
 move.w d0,p4l
 swap d0
 move.w d0,p4h
 move.l #Panel+40*4,d0
 move.w d0,p5l
 swap d0
 move.w d0,p5h
 move.l #Panel+40*5,d0
 move.w d0,p6l
 swap d0
 move.w d0,p6h
 move.l #Panel+40*6,d0
 move.w d0,p7l
 swap d0
 move.w d0,p7h
 move.l #Panel+40*7,d0
 move.w d0,p8l
 swap d0
 move.w d0,p8h
 
*******************************
* TIMER SCREEN SETUP
; move.l #TimerScr,d0
; move.w d0,p1l
; swap d0
; move.w d0,p1h
; move.w #$1201,Panelcon

 move.l #borders,d0
 move.w d0,s0l
 swap d0
 move.w d0,s0h
 move.l #borders+2592,d0
 move.w d0,s1l
 swap d0
 move.w d0,s1h
 move.l #borders+2592*2,d0
 move.w d0,s2l
 swap d0
 move.w d0,s2h
 move.l #borders+2592*3,d0
 move.w d0,s3l
 swap d0
 move.w d0,s3h
 
 move.w #52*256+64,borders
 move.w #212*256+0,borders+8
 move.w #52*256+64,borders+2592
 move.w #212*256+128,borders+8+2592
 move.w #52*256+192,borders+2592*2
 move.w #212*256+0,borders+8+2592*2
 move.w #52*256+192,borders+2592*3
 move.w #212*256+128,borders+8+2592*3
 
 move.l #FacePlace,d0
 move.w d0,f1l
 swap d0
 move.w d0,f1h
 move.l #FacePlace+32*24,d0
 move.w d0,f2l
 swap d0
 move.w d0,f2h
 move.l #FacePlace+32*24*2,d0
 move.w d0,f3l
 swap d0
 move.w d0,f3h
 move.l #FacePlace+32*24*3,d0
 move.w d0,f4l
 swap d0
 move.w d0,f4h
 move.l #FacePlace+32*24*4,d0
 move.w d0,f5l
 swap d0
 move.w d0,f5h
 
 move.l #PanelCop,d0
 move.w d0,pcl1
 move.w d0,pcl2
 swap d0
 move.w d0,pch1
 move.w d0,pch2
 
 move.l #bigfield,d0
 move.w d0,ocl
 swap d0
 move.w d0,och

 bset.b #1,$bfe001

 move.l #bigfield,$dff080    ; Point the copper at our copperlist.
 move.w #$00ff,$dff09e

; move.l #Blurbfield,$dff080

 move.w #0,d0

 move.l #scrn,d0
 move.w d0,pl1l
 swap d0
 move.w d0,pl1h

 move.l #scrn+40,d0
 move.w d0,pl2l
 swap d0
 move.w d0,pl2h

 move.l #scrn+80,d0
 move.w d0,pl3l
 swap d0
 move.w d0,pl3h

 move.l #scrn+120,d0
 move.w d0,pl4l
 swap d0
 move.w d0,pl4h

 move.l #scrn+160,d0
 move.w d0,pl5l
 swap d0
 move.w d0,pl5h

 move.l #scrn+200,d0
 move.w d0,pl6l
 swap d0
 move.w d0,pl6h

 move.l #scrn+240,d0
 move.w d0,pl7l
 swap d0
 move.w d0,pl7h

 move.l #healthpal,a5
 move.l #colbars,a0
 move.l #colbars2,a2
 move.w #scrheight-1,d0
 move.l #0,d6
 move.w #0,d3
 move.w #$2bdf,startwait
 move.w #$2d01,endwait
fillcop
 move.w #$180,d1

 move.l a0,a1
 move.l a2,a3
 move.w #$10c,(a1)+
 move.w #$10c,(a3)+
 move.w d3,(a1)+
 move.w d3,(a3)+
 eor.w #$8000,d3

 move.w #$106,(a1)+
 move.w #$106,(a3)+
 move.w #$2c42,d5
 or.w d3,d5
 and.w #$fffe,d5
 move.w d5,(a1)+
 move.w d5,(a3)+
 bsr do32

 move.w #$106,(a1)+
 move.w #$106,(a3)+
 move.w #$4c42,d5
 or.w d3,d5
 and.w #$fffe,d5
 move.w d5,(a1)+
 move.w d5,(a3)+
 bsr do32

 move.w #$106,(a1)+
 move.w #$106,(a3)+
 move.w #$6c42,d5
 or.w d3,d5
 and.w #$fffe,d5
 move.w d5,(a1)+
 move.w d5,(a3)+
 bsr do32
 
**********************************

 cmp.b #'s',option
 bra.s smallscrn

 move.w startwait,(a1)+
 move.w #$fffe,(a1)+
 move.w endwait,(a1)+
 move.w #$ff00,(a1)+
 move.w startwait,(a3)+
 move.w #$fffe,(a3)+
 move.w endwait,(a3)+
 move.w #$ff00,(a3)+
 
 
 add.w #$300,startwait
 add.w #$300,endwait
 
smallscrn: 

 move.l #$1060c42,(a1)+
 move.l #$1060c42,(a3)+
 move.w #$19e,(a1)+
 move.w (a5),(a1)+
 move.w #$19e,(a3)+
 move.w (a5)+,(a3)+

**********************************

 adda.w #104*4,a0
 adda.w #104*4,a2

 dbra d0,fillcop

**********************************
 cmp.b #'s',option
 bra smallnotlarge
 move.w #$38,fetchstart
 move.w #$b8,fetchstop
 move.w #$2c81,winstart
 move.w #$2cc1,winstop
 move.w #-40,modulo
 move.w #-40,modulo+4
 
 move.l #scrn+40,a0
 move.l #scrn+160,a1
 move.l #scrn+280,a2
 move.l #scrntab,a3
 move.w #319,d7	; counter
 move.w #0,d1	; xpos
plotscrnloop:
 move.b (a3)+,d0
 move.w d1,d2
 asr.w #3,d2
 move.b d1,d3
 not.b d3
 bclr.b d3,-40(a0,d2.w)
 bclr.b d3,(a0,d2.w)
 bclr.b d3,40(a0,d2.w)
 bclr.b d3,-40(a1,d2.w)
 bclr.b d3,(a1,d2.w)
 bclr.b d3,40(a1,d2.w)
 bclr.b d3,-40(a2,d2.w)
 btst #0,d0
 beq.s nobp1
 bset.b d3,-40(a0,d2.w)
nobp1:
 btst #1,d0
 beq.s nobp2
 bset.b d3,(a0,d2.w)
nobp2:
 btst #2,d0
 beq.s nobp3
 bset.b d3,40(a0,d2.w)
nobp3:
 btst #3,d0
 beq.s nobp4
 bset.b d3,-40(a1,d2.w)
nobp4:
 btst #4,d0
 beq.s nobp5
 bset.b d3,(a1,d2.w)
nobp5:
 btst #5,d0
 beq.s nobp6
 bset.b d3,40(a1,d2.w)
nobp6:
 btst #6,d0
 beq.s nobp7
 bset.b d3,-40(a2,d2.w)
nobp7:

 addq #1,d1

 dbra d7,plotscrnloop

smallnotlarge:

**********************************

****************************
 jsr INITPLAYER
; bsr initobjpos
****************************

 
 move.l #$dff000,a6
 
 move.l #null,$dff0a0
 move.w #100,$dff0a4
 move.w #443,$dff0a6
 move.w #63,$dff0a8

 move.l #null2,$dff0b0
 move.w #100,$dff0b4
 move.w #443,$dff0b6
 move.w #63,$dff0b8

 move.l #null4,$dff0c0
 move.w #100,$dff0c4
 move.w #443,$dff0c6
 move.w #63,$dff0c8

 move.l #null3,$dff0d0
 move.w #100,$dff0d4
 move.w #443,$dff0d6
 move.w #63,$dff0d8

 move.l #tab,a1
 move.w #64,d7
 move.w #0,d6
outerlop
 move.l #pretab,a0
 move.w #255,d5
scaledownlop:
 move.b (a0)+,d0
 ext.w d0
 ext.l d0
 muls d6,d0
 asr.l #6,d0
 move.b d0,(a1)+
 dbra d5,scaledownlop
 addq #1,d6
 dbra d7,outerlop
 
 move.l #$dff000,a6

 move.w #$c018,intena(a6)
 
 move.w #$f,dmacon(a6)
 move.w #$820f,dmacon(a6)
 
; bsr protinit
 
 bsr FullEnergy
 move.w #63,OldAmmo
 move.w #0,Ammo
 bsr AmmoBar
 move.w #0,OldAmmo
 
 move.b #1,PLR1_GunDamage
 move.b #1,PLR2_GunDamage
 move.w #3,PLR1_GunNoise
 move.w #3,PLR2_GunNoise
 
 move.w #10,GunData	; 10 shots pistol
 st GunData+7
 clr.b GunData+16+7
 clr.w GunData+16
 move.b #0,GunSelected
 
; move.w #$20,$1dc(a6)
 
 move.w #$0,$dff034
 move.w #0,Conditions
 move.l #KeyMap,a5
 clr.b $45(a5)
 
 move.l #ingame,mt_data
 clr.b UseAllChannels

; cmp.b #'b',Prefsfile+3
; bne.s .noback
; jsr mt_init
;.noback:
;
; clr.b CHANNELDATA
; clr.b CHANNELDATA+8
; clr.b CHANNELDATA+16
; clr.b CHANNELDATA+24
;
; cmp.b #'b',Prefsfile+3
; bne.s noreserve
;
; st CHANNELDATA
; st CHANNELDATA+8
; st CHANNELDATA+16
; st CHANNELDATA+24
;noreserve: 
 
; st CHANNELDATA
; st CHANNELDATA+8
 
 move.l #bass,pos0LEFT
 move.l #bassend,Samp0endLEFT
 move.l #playerheight,PLR1s_targheight
 move.l #playerheight,PLR1s_height

 move.b #%11011000,$bfd200
 move.b #%00010000,$bfd000
waitloop:
 btst.b #4,$bfd000
 bne.s waitloop
 move.b #%11000000,$bfd200
 st doanything
 
lop: 

 move.l #$dff000,a6

 move.w hitcol,d0
 beq.s nofadedownhc
 sub.w #$100,d0
 move.w d0,hitcol
 move.w d0,hitcol2
nofadedownhc:

 move.l drawpt,d0
 move.l olddrawpt,drawpt
 move.l d0,olddrawpt
 move.l d0,$dff084
 move.l drawpt,a3
 adda.w #10,a3
 move.l a3,frompt
 add.l #104*4*40,a3
 move.l a3,midpt

waitfortop:
 btst.b #0,intreqrl(a6)
 beq waitfortop
 move.w #$1,intreq(a6)
 
; tst.b GOTTOSEND
; beq.s sendnothing
; clr.b GOTTOSEND
; move.l #Chan0inter,$6c

sendnothing:
  
 move.b SpaceTapped,SPCTAP
 clr.b SpaceTapped

 move.l waterpt,a0
 move.l (a0)+,watertouse
 cmp.l #endwaterlist,a0
 blt.s okwat
 move.l #waterlist,a0
okwat:
 move.l a0,waterpt

 add.w #640,wtan
 and.w #8191,wtan
 add.w #1,wateroff
 and.w #63,wateroff

 move.w FramesToDraw,TempFrames
 move.w #0,FramesToDraw

 lea GunData,a6
 moveq #0,d0
 move.b GunSelected,d0
 add.w d0,d0
 lea (a6,d0.w*8),a6
 move.w (a6),Ammo

 JSR INITTIMER

 bsr PLR1_Control

 move.l #ZoneBrightTable,a1
 move.l ZoneAdds,a2
 move.l PLR1_ListOfGraphRooms,a0
doallz
 move.w (a0),d0
 blt.s doneallz
 add.w #8,a0
 
 move.l (a2,d0.w*4),a3
 add.l #LEVELDATA,a3
 move.w ToZoneBrightness(a3),d2

 blt.s justbright
 move.w d2,d3
 lsr.w #8,d3
 tst.b d3
 beq.s justbright

 move.l #BrightAnimTable,a4
 move.w -2(a4,d3.w*2),d2
 
justbright:
 move.w d2,(a1,d0.w*4)

 move.w ToUpperBrightness(a3),d2

 blt.s justbright2
 move.w d2,d3
 lsr.w #8,d3
 tst.b d3
 beq.s justbright2

 move.l #BrightAnimTable,a4
 move.w -2(a4,d3.w*2),d2
 
justbright2:
 move.w d2,2(a1,d0.w*4)

 bra doallz

doneallz:


; bsr PLR2_Control

******************************************
 jsr objmoveanim
 bsr EnergyBar
 bsr AmmoBar
 clr.b PLR1_clicked
 clr.b PLR2_clicked
******************************************


 
 move.l ObjectPoints,a1
 move.l PLR1_Obj,a0
 move.b damagetaken(a0),d2
 beq notbeenshot
 move.w #$f00,hitcol
 move.w #$f00,hitcol2
 ext.w d2
 sub.w d2,Energy
 move.b #0,damagetaken(a0)
 move.l #Cheese,FacesPtr
 move.w #3,Cheese
 move.w #-1,FacesCounter
 
 movem.l d0-d7/a0-a6,-(a7)
 move.w #19,Samplenum
 clr.b notifplaying
 move.w #0,Noisex
 move.w #0,Noisez
 move.w #100,Noisevol
 jsr MakeSomeNoise
 
 movem.l (a7)+,d0-d7/a0-a6
 
notbeenshot:
 move.b Energy+1,numlives(a0)
 
 move.w (a0),d0
 move.l PLR1_xoff,(a1,d0.w*8)
 move.l PLR1_zoff,4(a1,d0.w*8)
 move.l PLR1_Roompt,a1
 
 move.w (a1),12(a0)
 move.w (a1),d2
 move.l #ZoneBrightTable,a1
 move.l (a1,d2.w*4),d2
 tst.b PLR1_StoodInTop
 bne.s .okinbott
 swap d2
.okinbott:

 move.w d2,2+128(a0)
 
 move.l PLR1_yoff,d0
 asr.l #7,d0
 move.w d0,4(a0)
 
 move.b PLR1_StoodInTop,ObjInTop+128(a0)
 
 move.w PLR1_sinval,d4
 muls #100,d4
 move.w PLR1_cosval,d5
 muls #100,d5
 
 move.b GunSelected,d0
 asl.b #2,d0
 add.b GunFrame,d0
 move.b d0,11+128(a0)
 
 tst.b GunFrame
 beq.s .noless
 subq.b #1,GunFrame
.noless: 
 
 move.l ObjectPoints,a1
 move.w 128(a0),d0
 move.w 12(a0),12+128(a0)
 move.l PLR1_xoff,d1
 add.l d4,d1
 move.l d1,(a1,d0.w*8)
 move.l PLR1_zoff,d1
 add.l d5,d1
 move.l d1,4(a1,d0.w*8)
 move.w 4(a0),d0
 add.w #16,d0
 move.w d0,4+128(a0)
 move.w #$2010,6+128(a0)

 move.l ObjectPoints,a1
 move.l PLR2_Obj,a0
 move.w (a0),d0
 move.l PLR2_xoff,(a1,d0.w*8)
 move.l PLR2_zoff,4(a1,d0.w*8)
 move.l PLR2_Roompt,a1
************************
 move.w #-1,12(a0)
 move.w 10(a1),2(a0)
************************
 move.l PLR2_yoff,d0
 add.l #playerheight+128*24,d0
 asr.l #7,d0
 move.w d0,4(a0)


 jsr SendToSlave
 

 move.w #0,scaleval

 move.l PLR1_xoff,xoff
 move.l PLR1_yoff,yoff
 move.l PLR1_zoff,zoff
 move.w PLR1_angpos,angpos
 move.l PLR1_ListOfGraphRooms,ListOfGraphRooms
 move.l PLR1_PointsToRotatePtr,PointsToRotatePtr
 move.l PLR1_Roompt,Roompt

 move.w #0,leftclip
 move.w #96,rightclip
 move.w #0,deftopclip
 move.w #79,defbotclip
 move.w #0,topclip
 move.w #79,botclip
; sub.l #10*104*4,frompt
; sub.l #10*104*4,midpt

* Subroom loop

 bsr DrawDisplay
 
 bra noglass
 
************************************
* Test glass routine:
************************************

 move.l #WorkSpace,a0
 move.l frompt,a2
 move.w #104*4,d3
 move.w #1,d6
ribl
 move.w #31,d0
readinto
 move.w #15,d1
 move.l a2,a1
readintodown
 move.w (a1),(a0)+
 adda.w d3,a1
 move.w (a1),(a0)+
 adda.w d3,a1
 move.w (a1),(a0)+
 adda.w d3,a1
 move.w (a1),(a0)+
 adda.w d3,a1
 dbra d1,readintodown
; add.w #256-128,a0
 addq #4,a2
 dbra d0,readinto
 addq #4,a2
 dbra d6,ribl
 
* We now have the screen in a buffer
* for squidging.

 move.l frompt,a2
 move.l #WorkSpace,a0
 move.l glassballpt,a3
 move.w #$fff,d7
 move.w #1,d6 
rfbl:
 move.w #31,d0
readoutfrom:
 move.w #15,d1
 move.l a2,a1
 moveq.w #0,d5
readoutfromdown:
 move.w (a3)+,d2
 beq.s nono1
; add.w d5,d2
 move.w (a0,d2.w*2),d2
 and.w d7,d2
 move.w d2,(a1)
nono1:
 addq #1,d5
 add.w d3,a1
 move.w (a3)+,d2
 beq.s nono2
; add.w d5,d2
 move.w (a0,d2.w*2),d2
 and.w d7,d2
 move.w d2,(a1)
nono2:
 addq #1,d5
 add.w d3,a1
 move.w (a3)+,d2
 beq.s nono3
; add.w d5,d2
 move.w (a0,d2.w*2),d2
 and.w d7,d2
 move.w d2,(a1)
nono3:
 addq #1,d5
 add.w d3,a1
 move.w (a3)+,d2
 beq.s nono4
; add.w d5,d2
 move.w (a0,d2.w*2),d2
 and.w d7,d2
 move.w d2,(a1)
nono4:
 addq #1,d5
 add.w d3,a1
 dbra d1,readoutfromdown
 addq #4,a2
; adda.w #128,a0
 dbra d0,readoutfrom
 addq #4,a2
 dbra d6,rfbl
 
 move.l glassballpt,d0
 add.l #64*64*2,d0
 cmp.l #endglass,d0
 blt notoffglass
 move.l #glassball,d0
notoffglass
 move.l d0,glassballpt
 
noglass:
 
 tst.b PLR2
 bra.s nodrawp2
 
 
 move.l PLR2_xoff,xoff
 move.l PLR2_yoff,yoff
 move.l PLR2_zoff,zoff
 move.w PLR2_angpos,angpos
 move.l PLR2_ListOfGraphRooms,ListOfGraphRooms
 move.l PLR2_PointsToRotatePtr,PointsToRotatePtr

 move.w #0,leftclip
 move.w #96,rightclip
 move.w #10,deftopclip
 move.w #69,defbotclip
 add.l #68*104*4,frompt
 add.l #68*104*4,midpt

 bsr DrawDisplay


nodrawp2:
 
; move.l #brightentab,a0
; move.l frompt,a3
; adda.w #(4*33)+(104*4*20),a3
; move.w #20,d7
; move.w #20,d6
;horl:
; move.w d6,d5
; move.l a3,a1
;vertl
; move.w (a1),d0
; move.w (a0,d0.w*2),(a1)
; addq #4,a1
; dbra d5,vertl
; adda.w #104*4,a3
; dbra d7,horl

 move.l #$dff000,a6

; move.w #$300,col0(a6)

 move.l #KeyMap,a5
 tst.b $45(a5)
 beq.s noend
 
 bra endnomusic
noend:
 
 tst.w Energy
 bgt .noend
 cmp.b #'i',option+4
 bne end
 move.w #10,Energy
.noend:

 move.l SwitchData,a0
 tst.b 24+8(a0)
 bne end
  
 JSR STOPTIMER
 
 bra lop

***************************************************************************
***************************************************************************
****************** End of Main Loop here ********************************** 
***************************************************************************
***************************************************************************

 include "SERIAL_NIGHTMARE"

GunSelected: dc.b 0
 even

GunData:
; 0=Pistol 1=Big Gun
; ammoleft,ammopershot(b),gunnoise(b),ammoinclip(b)
; VISIBLE/INSTANT (0/FF)
; damage,gotgun(b)
; Delay (w)

 dc.w 0
 dc.b 1,3
 dc.b 20
 dc.b -1
 dc.b 1,$ff
 dc.w 5,0,0,0
 
 dc.w 0
 dc.b 1,1
 dc.b 40
 dc.b 0
 dc.b 4,0
 dc.w 10,0,0,0

 dc.w 0
 dc.b 1,9
 dc.b 5
 dc.b 0
 dc.b 10,0
 dc.w 50,0,0,0

protA: dc.w 0

Path:
; incbin "testpath"
endpath:
pathpt: dc.l Path

PLR1KEYS: dc.b 0
PLR1PATH: dc.b 0
PLR1MOUSE: dc.b -1
PLR1JOY: dc.b 0
 
 even

Bobble: dc.w 0
xwobble: dc.l 0
xwobxoff: dc.w 0
xwobzoff: dc.w 0

PLR1_Control:

; Take a snapshot of everything.

 move.l PLR1_xoff,d2
 move.l d2,PLR1_oldxoff
 move.l d2,oldx
 move.l PLR1_zoff,d3
 move.l d3,PLR1_oldzoff
 move.l d3,oldz
 move.l PLR1s_xoff,d0
 move.l d0,PLR1_xoff
 move.l d0,newx
 move.l PLR1s_zoff,d1
 move.l d1,newz
 move.l d1,PLR1_zoff

 move.l PLR1s_height,PLR1_height
 
 sub.l d2,d0
 sub.l d3,d1
 move.l d0,xdiff
 move.l d1,zdiff
 move.w PLR1s_sinval,PLR1_sinval
 move.w PLR1s_cosval,PLR1_cosval
 move.w PLR1s_angpos,PLR1_angpos
 move.l PLR1s_yoff,d0
 move.l #SineTable,a1
 move.w Bobble,d1
 move.w (a1,d1.w),d1
 move.w d1,d3
 ble.s notnegative
 neg.w d1
notnegative:
 add.w #16384,d1
 asr.w #5,d1
 move.w d1,d2
 add.w d1,d1
 add.w d2,d1
 ext.l d1
 add.l d1,d0
 
 asr.w #6,d3
 ext.l d3
 move.l d3,xwobble
 move.w PLR1_sinval,d1
 muls d3,d1
 move.w PLR1_cosval,d2
 muls d3,d2
 swap d1
 swap d2
 asr.w #7,d1
 move.w d1,xwobxoff
 asr.w #7,d2
 neg.w d2
 move.w d2,xwobzoff
 
 
 move.l d0,PLR1_yoff
 move.l d0,newy
 move.l d0,oldy
 move.l PLR1_height,thingheight
 move.l #40*256,StepUpVal
 move.l #$1000000,StepDownVal
 
 move.l PLR1_Roompt,objroom
 move.w #%100000000,wallflags
 move.b PLR1_StoodInTop,StoodInTop

 bsr MoveObject
 move.b StoodInTop,PLR1_StoodInTop
 move.w #0,wallflags
 move.l objroom,PLR1_Roompt
 move.w newx,PLR1_xoff
 move.w newz,PLR1_zoff
 move.l PLR1_xoff,PLR1s_xoff
 move.l PLR1_zoff,PLR1s_zoff
 
 move.l PLR1_Roompt,a0
 
 move.l ToZoneFloor(a0),d0
 tst.b PLR1_StoodInTop
 beq.s notintop
 move.l ToUpperFloor(a0),d0
notintop:

 move.l ToZoneRoof(a0),SplitHeight

 adda.w #ToZonePts,a0
 sub.l PLR1_height,d0
 move.l d0,PLR1s_tyoff

; move.l (a0),a0		; jump to viewpoint list
 * A0 is pointing at a pointer to list of points to rotate
 move.w (a0)+,d1
 ext.l d1
 add.l PLR1_Roompt,d1
 move.l d1,PLR1_PointsToRotatePtr
 tst.w (a0)+
 beq.s nobackgraphics
 move.l a0,-(a7)
 jsr putinbackdrop 
 move.l (a7)+,a0
nobackgraphics:
 move.l a0,PLR1_ListOfGraphRooms

*****************************************************

 rts

KeyMap: ds.b 256

PLR2_Control:
 move.l #SineTable,a0

 bsr turnleftright

 move.w PLR2_angspd,d1
 move.w PLR2_angpos,d0
 move.w (a0,d0.w),PLR2_sinval
 adda.w #2048,a0
 move.w (a0,d0.w),PLR2_cosval

 move.l PLR2_xspdval,d6
 move.l PLR2_zspdval,d7

 move.w PLR2_xoff,oldxoff
 move.w PLR2_zoff,oldzoff

 neg.l d6
 ble.s .nobug1
 asr.l #1,d6
 add.l #1,d6
 bra.s .bug1
.nobug1
 asr.l #1,d6
.bug1:

; beq.s goinnowhere
; blt.s goinfor
; cmp.l #4*65536,d6
; ble.s goinnowhere
; move.l #4*65536,d6
;goinfor:
; cmp.l #-4*65536,d6
; bge.s goinnowhere
; move.l #-4*65536,d6
;goinnowhere:

 neg.l d7
 ble.s .nobug2
 asr.l #1,d7
 add.l #1,d7
 bra.s .bug2
.nobug2
 asr.l #1,d7
.bug2: 
 
; beq.s goinnowhere2
; blt.s goinfor2
; cmp.l #4*65536,d7
; ble.s goinnowhere2
; move.l #4*65536,d7
;goinfor2:
; cmp.l #-4*65536,d7
; bge.s goinnowhere2
; move.l #-4*65536,d7
;goinnowhere2:

 move.w PLR2_sinval,d1
 move.w PLR2_cosval,d2
 move.w PLR2_ForwardSpd,d3
 
 muls d3,d2
 muls d3,d1

 sub.l d1,d6
 sub.l d2,d7
 add.l PLR2_pushx,d6
 add.l PLR2_pushz,d7 
 add.l d6,PLR2_xspdval
 add.l d7,PLR2_zspdval
 move.l PLR2_xspdval,d6
 move.l PLR2_zspdval,d7
 add.l d6,PLR2_xoff
 add.l d7,PLR2_zoff

 move.w PLR2_xoff,newx
 move.w PLR2_zoff,newz
 move.w oldxoff,oldx
 move.w oldzoff,oldz
 move.l PLR2_xspdval,xdiff
 move.l PLR2_zspdval,zdiff
 move.l PLR2_Roompt,objroom
 move.w #%100000000,wallflags
 bsr MoveObject
 move.w #0,wallflags
 move.l objroom,PLR2_Roompt
 move.w newx,PLR2_xoff
 move.w newz,PLR2_zoff
 
 move.l PLR2_xoff,d0
 move.l PLR2_zoff,d1
 sub.l oldxoff,d0
 sub.l oldzoff,d1
 move.l #0,PLR2_pushx
 move.l #0,PLR2_pushz
 move.l d0,PLR2_opushx
 move.l d1,PLR2_opushz

 move.l PLR2_Roompt,a0
 move.l 2(a0),d0
 sub.l #playerheight,d0
 move.l d0,PLR2_tyoff
 adda.w #22,a0

; move.l (a0),a0		; jump to viewpoint list
 * A0 is pointing at a pointer to list of points to rotate
 move.l (a0)+,PLR2_PointsToRotatePtr
 move.l a0,PLR2_ListOfGraphRooms

*****************************************************

 move.l PLR2_tyoff,d0
 move.l PLR2_yoff,d1
 move.l PLR2_yvel,d2
 add.l d2,d1
 add.l #1024,d2
 sub.l d1,d0
 bgt.s .shouldfall
 move.l #0,d2
 add.l d0,d1
.shouldfall:
 move.l d2,PLR2_yvel
 move.l d1,PLR2_yoff
 rts


DrawDisplay:

 move.l #SineTable,a0
 move.w angpos,d0
 move.w (a0,d0.w),d6
 adda.w #2048,a0
 move.w (a0,d0.w),d7
 move.w d6,sinval
 move.w d7,cosval

 move.l yoff,d0
 asr.l #8,d0
 move.w d0,d1
 and.w #63,d1
 move.w d1,wallyoff
 asl.w #2,d0
 move.w d0,flooryoff
 
 move.w xoff,d6
 move.w d6,d3
 asr.w #1,d3
 add.w d3,d6
 asr.w #1,d6
 move.w d6,xoff34
 
 move.w zoff,d6
 move.w d6,d3
 asr.w #1,d3
 add.w d3,d6
 asr.w #1,d6
 move.w d6,zoff34

 bsr RotateLevelPts
 bsr RotateObjectPts
 bsr OrderZones

 move.l ListOfGraphRooms,a0

 move.l endoflist,a0
subroomloop:
 move.w -(a0),d7
 blt jumpoutofrooms
 
; bsr setlrclip
; move.w leftclip,d0
; cmp.w rightclip,d0
; bge subroomloop
 move.l a0,-(a7)
 
 move.l ZoneAdds,a0
 move.l (a0,d7.w*4),a0
 add.l #LEVELDATA,a0
 move.l ToZoneRoof(a0),SplitHeight
 
 move.l ZoneGraphAdds,a0
 move.l 4(a0,d7.w*8),a2
 move.l (a0,d7.w*8),a0
 
 add.l #LEVELGRAPHICS,a0
 add.l #LEVELGRAPHICS,a2
 move.l a2,ThisRoomToDraw+4
 move.l a0,ThisRoomToDraw

 move.l ListOfGraphRooms,a1
 
finditit:
 tst.w (a1)
 blt nomoretodoatall
 cmp.w (a1),d7
 beq outoffind
 adda.w #8,a1
 bra finditit
 
outoffind:

 move.l a1,-(a7)

 move.w #0,leftclip
 move.w #96,rightclip
 moveq #0,d7
 move.w 2(a1),d7
 blt.s outofrcliplop
 move.l #LEVELCLIPS,a0
 lea (a0,d7.l*2),a0

 tst.w (a0)
 blt outoflcliplop
 
 bsr NEWsetlclip
 
intolcliplop:		; clips
 tst.w (a0)
 blt outoflcliplop
 
 bsr NEWsetlclip 
 bra intolcliplop
 
outoflcliplop:

 addq #2,a0

 tst.w (a0)
 blt outofrcliplop
 
 bsr NEWsetrclip
 
intorcliplop:		; clips
 tst.w (a0)
 blt outofrcliplop
 
 bsr NEWsetrclip 
 bra intorcliplop
 
outofrcliplop:
 
 move.w leftclip,d0
 cmp.w #95,d0
 bge dontbothercantseeit
 move.w rightclip,d1
 blt dontbothercantseeit
 cmp.w d1,d0
 bge dontbothercantseeit
 
 move.l PLR1_yoff,d0
 cmp.l SplitHeight,d0
 blt.s botfirst
 
 move.l ThisRoomToDraw+4,a0
 cmp.l #LEVELGRAPHICS,a0
 beq.s noupperroom
 st DOUPPER
 bsr dothisroom
noupperroom:
 move.l ThisRoomToDraw,a0
 clr.b DOUPPER
 bsr dothisroom
 
 bra dontbothercantseeit
botfirst:

 move.l ThisRoomToDraw,a0
 clr.b DOUPPER
 bsr dothisroom
 move.l ThisRoomToDraw+4,a0
 cmp.l #LEVELGRAPHICS,a0
 beq.s noupperroom2
 st DOUPPER
 bsr dothisroom
noupperroom2:
 
dontbothercantseeit:
pastemp:

 move.l (a7)+,a1
 move.l ThisRoomToDraw,a0
 move.w (a0),d7
 adda.w #8,a1
 bra finditit
 
nomoretodoatall:
 
 move.l (a7)+,a0
 
 bra subroomloop

jumpoutofrooms:

 rts
 
prot9: dc.w 0
 
TempBuffer: ds.l 100 

ClipTable: ds.l 30
EndOfClipPt: dc.l 0
DOUPPER: dc.w 0

dothisroom

 move.w (a0)+,d0
 move.w d0,currzone
 move.l #ZoneBrightTable,a1
 move.l (a1,d0.w*4),d1
 tst.b DOUPPER
 bne.s .okbot
 swap d1
.okbot:
 move.w d1,ZoneBright

polyloop:
 move.w (a0)+,d0
 blt jumpoutofloop
 beq itsawall
 cmp.w #3,d0
 beq itsasetclip
 blt itsafloor
 cmp.w #4,d0
 beq itsanobject
 cmp.w #5,d0
 beq.s itsanarc
 cmp.w #6,d0
 beq itsalightbeam
 cmp.w #7,d0
 beq.s itswater
 cmp.w #9,d0
 ble itsachunkyfloor
 cmp.w #11,d0
 ble.s itsabumpyfloor
 cmp.w #12,d0
 beq.s itsbackdrop
 cmp.w #13,d0
 beq.s itsaseewall
 
 bra polyloop
 
itsaseewall:
 st seethru
 jsr itsawalldraw
 bra polyloop
 
itsbackdrop:
 jsr putinbackdrop
 bra polyloop
 
itswater:
 move.w #1,d0
 move.l #FloorLine,LineToUse
 st usewater
 clr.b usebumps
 jsr itsafloordraw
 bra polyloop
 
itsanarc:
 jsr CurveDraw
 bra polyloop
itsanobject:
 jsr ObjDraw
 bra polyloop
itsalightbeam:
 jsr LightDraw
 bra polyloop
 
itsabumpyfloor:
 sub.w #9,d0
 st usebumps
 st smoothbumps
 clr.b usewater
 move.l #BumpLine,LineToUse
 jsr itsafloordraw
 bra polyloop
 
itsachunkyfloor:
 subq.w #7,d0
 st usebumps
 sub.w #12,topclip
; add.w #10,botclip
 clr.b smoothbumps
 clr.b usewater
 move.l #BumpLine,LineToUse
 jsr itsafloordraw
 add.w #12,topclip
; sub.w #10,botclip
 bra polyloop 
 
itsafloor:

 move.l #FloorLine,LineToUse
* 1,2 = floor/roof
 clr.b usewater
 clr.b usebumps
 jsr itsafloordraw
 bra polyloop
itsasetclip:
 bra polyloop
itsawall:
 clr.b seethru
; move.l #stripbuffer,a1
 jsr itsawalldraw
 bra polyloop

jumpoutofloop:
 rts

ThisRoomToDraw: dc.l 0,0
SplitHeight: dc.l 0

 include "ab3:source/OrderZones"

ReadMouse:
 clr.l d0
 clr.l d1
 move.w $a(a6),d0
 lsr.w #8,d0
 ext.l d0
 move.w d0,d3
 move.w oldmy,d2
 sub.w d2,d0

 cmp.w #127,d0
 blt nonegy
 move.w #255,d1
 sub.w d0,d1
 move.w d1,d0
 neg.w d0
nonegy:

 cmp.w #-127,d0
 bge nonegy2
 move.w #255,d1
 add.w d0,d1
 move.w d1,d0
nonegy2:

 add.b d0,d2
 add.w d0,oldy2
 move.w d2,oldmy
 move.w d2,d0

 move.w oldy2,d0
 move.w d0,ymouse

 clr.l d0
 clr.l d1
 move.w $a(a6),d0
 ext.w d0
 ext.l d0
 move.w d0,d3
 move.w oldmx,d2
 sub.w d2,d0

 cmp.w #127,d0
 blt nonegx
 move.w #255,d1
 sub.w d0,d1
 move.w d1,d0
 neg.w d0
nonegx:

 cmp.w #-127,d0
 bge nonegx2
 move.w #255,d1
 add.w d0,d1
 move.w d1,d0
nonegx2:

 add.b d0,d2
 move.w d0,d1
 move.w d2,oldmx

 move.w #$0,$dff034
 btst #2,$dff016
 beq.s noturn

 add.w d0,oldx2
 move.w oldx2,d0
 and.w #2047,d0
 move.w d0,oldx2
 
 asl.w #2,d0
 sub.w prevx,d0
 add.w d0,prevx
 add.w d0,PLR1s_angpos
 move.w #0,lrs
 rts

noturn:

; got to move lr instead. 

; d1 = speed moved l/r

 move.w d1,lrs

 rts
 
lrs: dc.w 0
prevx: dc.w 0
 
mang: dc.w 0
oldymouse: dc.w 0
xmouse: dc.w 0
ymouse: dc.w 0
oldx2: dc.w 0
oldmx: dc.w 0
oldmy: dc.w 0
oldy2: dc.w 0

RotateLevelPts:

 move.w sinval,d6
 swap d6
 move.w cosval,d6

 move.l PointsToRotatePtr,a0
 move.l Points,a3
 move.l #Rotated,a1
 move.l #OnScreen,a2
 move.w xoff,d4
 move.w zoff,d5
 
; move.w #$c40,$dff106
; move.w #$f00,$dff180
 
pointrotlop:
 move.w (a0)+,d7
 blt.s outofpointrot
 
 move.w (a3,d7*4),d0
 sub.w d4,d0
 move.w d0,d2
 move.w 2(a3,d7*4),d1
 sub.w d5,d1
 muls d6,d2
 swap d6
 move.w d1,d3
 muls d6,d3
 sub.l d3,d2
 add.l d2,d2
 swap d2
 ext.l d2
 asl.l #7,d2
 add.l xwobble,d2
 move.l d2,(a1,d7*8)

 muls d6,d0
 swap d6
 muls d6,d1
 add.l d0,d1
 asl.l #2,d1
 swap d1
 move.l d1,4(a1,d7*8)

 tst.w d1
 bgt.s ptnotbehind
 tst.w d2
 bgt.s onrightsomewhere
 move.w #0,d2
 bra putin
onrightsomewhere:
 move.w #96,d2
 bra putin
ptnotbehind:

 divs d1,d2
 add.w #47,d2
putin:
 move.w d2,(a2,d7*2)
 
 bra pointrotlop
outofpointrot:

; move.w #$c40,$dff106
; move.w #$ff0,$dff180

 rts

RotateObjectPts:

 move.w sinval,d5
 move.w cosval,d6

 move.l ObjectData,a4
 move.l ObjectPoints,a0
 move.w NumObjectPoints,d7
 move.l #ObjRotated,a1
 move.l #ObsInLine,a2
objpointrotlop:
 
 move.w (a0),d0
 sub.w xoff,d0
 move.w 4(a0),d1
 addq #8,a0
 
 tst.w 12(a4)
 blt noworkout
 
 sub.w zoff,d1
 move.w d0,d2
 muls d6,d2
 move.w d1,d3
 muls d5,d3
 sub.l d3,d2
 add.l d2,d2
 swap d2
 move.w d2,(a1)+
 
 muls d5,d0
 muls d6,d1
 add.l d0,d1
 asl.l #2,d1
 swap d1
 moveq #0,d3
 
 move.w d1,(a1)+
 ext.l d2
 asl.l #7,d2
 add.l xwobble,d2
 move.l d2,(a1)+
 sub.l xwobble,d2
 tst.w d1
 ble.s notinline


 move.l d2,d3
 divs d1,d2
 cmp.w #-20,d2
 blt.s notinline
 cmp.w #20,d2
 sle d3
notinline
 move.b d3,(a2)+

 add.w #64,a4
 dbra d7,objpointrotlop

 rts
 
noworkout:
  clr.b (a2)+
  move.l #0,(a1)+
  move.l #0,(a1)+
  add.w #64,a4
  dbra d7,objpointrotlop
  rts

LightDraw:

 move.w (a0)+,d0
 move.w (a0)+,d1
 move.l #Rotated,a1
 move.w 6(a1,d0.w*8),d2
 ble.s oneendbehind
 move.w 6(a1,d1.w*8),d3
 bgt.s bothendsinfront

oneendbehind:
 rts
bothendsinfront:
 
 move.l #OnScreen,a2
 move.w (a2,d0.w*2),d0
 bge.s okleftend
 moveq #0,d0
okleftend:
 move.w (a2,d1.w*2),d1
 bgt.s somevis
 rts
somevis:
 cmp.w #95,d0
 ble.s somevis2
 rts
somevis2:
 cmp.w #95,d1
 ble.s okrightend
 move.w #95,d1
okrightend:

 sub.w d0,d1
 blt.s wrongbloodywayround
 move.l #brightentab,a4
 move.l #objintocop,a1
 lea (a1,d0.w*2),a1
 
 move.l frompt,a3
 move.w #104*4,d6
 move.w #79,d2
lacross:
 move.w d2,d3
 move.l a3,a2
 adda.w (a1)+,a2
ldown:
 add.w d6,a2
 move.w (a2),d7
 move.w (a4,d7.w*2),(a2)
 dbra d3,ldown
 dbra d1,lacross
 
wrongbloodywayround:
 
 rts

FaceToPlace: dc.w 0

Cheese:
 dc.w 4,15

FacesList:
 dc.w 0,4*4
 dc.w 1,2*4
 dc.w 0,2*4
 dc.w 2,2*4
 dc.w 0,2*4
 dc.w 1,3*4
 dc.w 0,2*4
 dc.w 2,3*4
 dc.w 0,5*4
 dc.w 1,2*4
 dc.w 0,2*4
 dc.w 2,2*4
 dc.w 0,2*4
 dc.w 1,2*4
 dc.w 0,2*4
 dc.w 2,3*4
 dc.w 0,1*4
 dc.w 1,3*4
 dc.w 0,1*4
 dc.w 2,3*4
 dc.w 0,1*4

EndOfFacesList:

FacesPtr:
 dc.l FacesList
FacesCounter:
 dc.w 0
Expression:
 dc.w 0

PlaceFace:

 move.w FacesCounter,d0
 subq #1,d0
 bgt.s NoNewFace

 move.l FacesPtr,a0
 
 move.w 2(a0),d0
 move.w (a0),Expression
 addq #4,a0
 cmp.l #EndOfFacesList,a0
 blt.s NotFirstFace

 move.l #FacesList,a0

NotFirstFace
 move.l a0,FacesPtr

NoNewFace:

 move.w d0,FacesCounter

 Move.w FaceToPlace,d0
 muls #5,d0
 add.w Expression,d0
 move.l #FacePlace+10,a0
 move.l #Faces,a1
 muls #(4*32*5),d0
 adda.w d0,a1
 move.w #4,d0
 move.w #24,d1
 
 move.w #4,d3
bitplaneloop:
 move.w #31,d2
PlaceFaceToPlaceInFacePlaceLoop:
 move.l (a1),(a0)
 adda.w d0,a1
 adda.w d1,a0
 dbra d2,PlaceFaceToPlaceInFacePlaceLoop
 dbra d3,bitplaneloop
 
 rts
 
Energy:
 dc.w 191
OldEnergy:
 dc.w 191
Ammo: dc.w 63
OldAmmo: dc.w 63

FullEnergy:
 move.w #127,Energy
 move.w #127,OldEnergy
 move.l #health,a0
 move.l #borders,a1
 add.l #25*8*2+6,a1
 lea 2592(a1),a2
 move.w #127,d0
PutInFull:
 move.b (a0)+,(a1)
 move.b (a0)+,8(a1)
 add.w #16,a1
 move.b (a0)+,(a2)
 move.b (a0)+,8(a2)
 add.w #16,a2
 dbra d0,PutInFull
 
 rts
 
EnergyBar:

 move.w Energy,d0
 cmp.w OldEnergy,d0
 bne.s gottochange
 
NoChange
 rts
 
gottochange:
  
 blt LessEnergy
 cmp.w #127,Energy
 blt.s NotMax
 move.w #127,Energy
NotMax:

 move.w Energy,d0
 move.w OldEnergy,d2
 sub.w d0,d2
 beq.s NoChange	
 neg.w d2
 
 move.w #127,d3
 sub.w d0,d3
 
 move.l #health,a0
 lea (a0,d3.w*4),a0
 move.l #borders+25*16+6,a1
 lsl.w #4,d3
 add.w d3,a1
 lea 2592(a1),a2
 
EnergyRise:
 move.b (a0)+,(a1)
 move.b (a0)+,8(a1)
 add.w #16,a1
 move.b (a0)+,(a2)
 move.b (a0)+,8(a2)
 add.w #16,a2
 subq #1,d2
 bgt.s EnergyRise

 move.w Energy,OldEnergy

 rts 

LessEnergy: 
 move.w OldEnergy,d2
 sub.w d0,d2
 
 move.w #127,d3
 sub.w OldEnergy,d3
 
 move.l #borders+25*16+6,a1
 asl.w #4,d3
 add.w d3,a1
 lea 2592(a1),a2

EnergyDrain:
 move.b #0,(a1)
 move.b #0,8(a1)
 move.b #0,(a2)
 move.b #0,8(a2)
 add.w #16,a1
 add.w #16,a2
 subq #1,d2
 bgt.s EnergyDrain

 move.w Energy,OldEnergy

 rts 

AmmoBar:

 move.w Ammo,d0
 cmp.w OldAmmo,d0
 bne.s .gottochange
 
.NoChange
 rts
 
.gottochange:
  
 blt LessAmmo
 cmp.w #63,Ammo
 blt.s .NotMax
 move.w #63,Ammo
.NotMax:

 move.w Ammo,d0
 move.w OldAmmo,d2
 sub.w d0,d2
 beq.s .NoChange
 neg.w d2
 
 move.w #63,d3
 sub.w d0,d3
 
 move.l #Ammunition,a0
 lea (a0,d3.w*8),a0
 move.l #borders+5184+25*16+1,a1
 lsl.w #5,d3
 add.w d3,a1
 lea 2592(a1),a2
 
AmmoRise:
 move.b (a0)+,(a1)
 move.b (a0)+,8(a1)
 add.w #16,a1
 move.b (a0)+,(a2)
 move.b (a0)+,8(a2)
 add.w #16,a2
 move.b (a0)+,(a1)
 move.b (a0)+,8(a1)
 add.w #16,a1
 move.b (a0)+,(a2)
 move.b (a0)+,8(a2)
 add.w #16,a2
 subq #1,d2
 bgt.s AmmoRise

 move.w Ammo,OldAmmo

 rts 


LessAmmo: 
 move.w OldAmmo,d2
 sub.w d0,d2
 
 move.w #63,d3
 sub.w OldAmmo,d3
 
 move.l #borders++5184+25*16+1,a1
 asl.w #5,d3
 add.w d3,a1
 lea 2592(a1),a2

AmmoDrain:
 move.b #0,(a1)
 move.b #0,8(a1)
 move.b #0,(a2)
 move.b #0,8(a2)
 add.w #16,a1
 add.w #16,a2
 move.b #0,(a1)
 move.b #0,8(a1)
 move.b #0,(a2)
 move.b #0,8(a2)
 add.w #16,a1
 add.w #16,a2
 subq #1,d2
 bgt.s AmmoDrain

 move.w Ammo,OldAmmo

 rts 

nulop:
 move.w #$0010,$dff000+intreq
 rte
 
doanything: dc.w 0
 
end:

 move.l drawpt,d0
 move.l olddrawpt,drawpt
 move.l d0,olddrawpt
 move.l d0,$dff084

 clr.b doanything
 cmp.b #'b',Prefsfile+3
 bne.s .noback
 jsr mt_end
.noback
 tst.w Energy
 bgt.s wevewon

 move.l #gameover,mt_data
 st UseAllChannels
 clr.b reachedend
 jsr mt_init
playgameover:
 move.l #$dff000,a6
waitfortop2:
 btst.b #0,intreqrl(a6)
 beq waitfortop2
 move.w #$1,intreq(a6)

 jsr mt_music

 tst.b reachedend
 beq.s playgameover
 
 bra wevelost
wevewon:
 move.l #welldone,mt_data
 st UseAllChannels
 clr.b reachedend
 jsr mt_init
playwelldone:
 move.l #$dff000,a6
waitfortop3:
 btst.b #0,intreqrl(a6)
 beq waitfortop3
 move.w #$1,intreq(a6)

 jsr mt_music

 tst.b reachedend
 beq.s playwelldone


wevelost:
 jmp closeeverything 

endnomusic
 clr.b doanything
 cmp.b #'b',Prefsfile+3
 bne.s .noback
 jsr mt_end
.noback
 jmp closeeverything

do32:
 move.w #31,d7
 move.w #$180,d1
across:
 move.w d1,(a1)+
 move.w d1,(a3)+
 move.w #0,(a1)+
 move.w #0,(a3)+
 add.w #2,d1
 dbra d7,across
 rts


 
*************************************
* Set left and right clip values
*************************************
 
 

NEWsetlclip:
 move.l #OnScreen,a1
 move.l #Rotated,a2
 move.l CONNECT_TABLE,a3
 
 move.w (a0),d0
 bge.s .notignoreleft
 
; move.l #0,(a6)
 
 bra .leftnotoktoclip
.notignoreleft:

 move.w 6(a2,d0*8),d3	; left z val
 bgt.s .leftclipinfront
 addq #2,a0
 rts

 tst.w 6(a2,d0*8)
 bgt.s .leftnotoktoclip
.ignoreboth:
; move.l #0,(a6)
; move.l #96*65536,4(a6)
 move.w #0,leftclip
 move.w #96,rightclip
 addq #8,a6
 addq #2,a0
 rts

.leftclipinfront:
 move.w (a1,d0*2),d1	; left x on screen
 move.w (a0),d2
 move.w 2(a3,d2.w*4),d2
 move.w (a1,d2.w*2),d2
 cmp.w d1,d2
 bgt.s .leftnotoktoclip

; move.w d1,(a6)
; move.w d3,2(a6)
 cmp.w leftclip(pc),d1
 ble.s .leftnotoktoclip
 move.w d1,leftclip
.leftnotoktoclip:

 addq #2,a0

 rts

NEWsetrclip
 move.l #OnScreen,a1
 move.l #Rotated,a2
 move.l CONNECT_TABLE,a3
 move.w (a0),d0
 bge.s .notignoreright
; move.w #96,4(a6)
; move.w #0,6(a6)
 move.w #0,d4
 bra .rightnotoktoclip
.notignoreright:
 move.w 6(a2,d0*8),d4	; right z val
 bgt.s .rightclipinfront
; move.w #96,4(a6)
; move.w #0,6(a6)
 bra.s .rightnotoktoclip

.rightclipinfront:
 move.w (a1,d0*2),d1	; right x on screen
 move.w (a0),d2
 move.w (a3,d2.w*4),d2
 move.w (a1,d2.w*2),d2
 cmp.w d1,d2
 blt.s .rightnotoktoclip
; move.w d1,4(a6)
; move.w d4,6(a6)

 cmp.w rightclip(pc),d1
 bge.s .rightnotoktoclip
 addq #1,d1
 move.w d1,rightclip
.rightnotoktoclip:
 addq #8,a6
 addq #2,a0
 rts

FIRSTsetlrclip:
 move.l #OnScreen,a1
 move.l #Rotated,a2
 
 move.w (a0)+,d0
 bge.s .notignoreleft
 bra .leftnotoktoclip
.notignoreleft:

 move.w 6(a2,d0*8),d3	; left z val
 bgt.s .leftclipinfront

 move.w (a0),d0
 blt.s .ignoreboth
 tst.w 6(a2,d0*8)
 bgt.s .leftnotoktoclip
.ignoreboth
 move.w #96,rightclip
 move.w #0,leftclip
 addq #2,a0
 rts

.leftclipinfront:
 move.w (a1,d0*2),d1	; left x on screen
 cmp.w leftclip(pc),d1
 ble.s .leftnotoktoclip
 move.w d1,leftclip
.leftnotoktoclip:

 move.w (a0)+,d0
 bge.s .notignoreright
 move.w #0,d4
 bra .rightnotoktoclip
.notignoreright:
 move.w 6(a2,d0*8),d4	; right z val
 ble.s .rightnotoktoclip

.rightclipinfront:
 move.w (a1,d0*2),d1	; right x on screen
 cmp.w rightclip(pc),d1
 bge.s .rightnotoktoclip
 addq #1,d1
 move.w d1,rightclip
.rightnotoktoclip:

; move.w leftclip,d0
; move.w rightclip,d1
; cmp.w d0,d1
; bge.s .noswap
; move.w #96,rightclip
; move.w #0,leftclip
;.noswap:

 rts


leftclip2: dc.w 0
rightclip2: dc.w 0
ZoneBright: dc.w 0
 
npolys: dc.w 0

PLR1_fire: dc.b 0
PLR2_fire: dc.b 0

turnleftright:

 move.w PLR2_angspd,d1
 move.w PLR2_angpos,d0

 move.w #120,d7
 muls TempFrames,d7

 move.w d1,d2
 neg.w d2
 cmp.w d7,d2
 ble.s okslow
 move.w d7,d2
okslow
 neg.w d7
 cmp.w d7,d2
 bge.s okslo
 move.w d7,d2
okslo:

 asr.w #1,d7

 btst #1,$d(a6)
 sne d3
 beq.s notleft
 move.w d7,d2
 neg.w d2
; tst.w d1
; bge.s notleft
; moveq #0,d1
notleft

 btst #1,$c(a6)
 sne d4
 beq.s notright
 move.w d7,d2
; tst.w d1
; ble.s notright
; moveq #0,d1
notright

 move.w #0,PLR2_ForwardSpd

 btst #0,$dff00c
 sne d5
 eor.b d4,d5
 beq.s notup
 move.w TempFrames,d7
 neg.w d7
 asl.w #2,d7
 move.w d7,PLR2_ForwardSpd
notup:

 btst #0,$dff00d
 sne d5
 eor.b d3,d5
 beq.s notdown
 move.w TempFrames,d7
 asl.w #2,d7
 move.w d7,PLR2_ForwardSpd
notdown:

 add.w d2,d1
 cmp.w #-360,d1
 bge.s okspdlft
 move.w #-360,d1
okspdlft:
 cmp.w #360,d1
 ble.s okspdrgt
 move.w #360,d1
okspdrgt:

 move.w d1,PLR2_angspd
 add.w d1,d0
 and.w #8191,d0
 move.w d0,PLR2_angpos
 rts

*****************************************************

 include "ab3:source/ObjectMove"

pastdata:
***********************************
* This routine animates brightnesses.

 
liftpt: dc.l liftanimtab

brightpt:
 dc.l brightanimtab


liftanim:
 rts

******************************
 include "ab3:source/Anims"
******************************
startpass:
; include "ab3:source/password_reloc.s"
endpass:

rotanimpt: dc.w 0
xradd: dc.w 5
yradd: dc.w 8
xrpos: dc.w 320
yrpos: dc.w 320

rotanim:
 rts
 
option:
 dc.l 0,0

********** WALL STUFF *******************************

 include "AB3:source/wallroutine3.chipmem"

*****************************************************

******************************************
* floor polygon

numsidestd: dc.w 0
bottomline: dc.w 0

NewCornerBuff:
 ds.l 100

itsafloordraw:

* If D0 =1 then its a floor otherwise (=2) it's
* a roof.

 move.w #0,above
 move.w (a0)+,d6	; ypos of poly
 
 move.w leftclip(pc),d7
 cmp.w rightclip(pc),d7
 bge.s dontdrawreturn
 
 move.w botclip,d7
 sub.w #40,d7
 ble.s dontdrawreturn
 sub.w flooryoff,d6
 bgt.s below
 blt.s aboveplayer
dontdrawreturn:
 move.w (a0)+,d6	; sides-1
 add.w d6,d6
 add.w d6,a0
 add.w #4+6,a0
 rts
aboveplayer:
 cmp.w #2,d0
 bne.s dontdrawreturn
 move.w #40,d7
 sub.w topclip,d7 
 blt.s dontdrawreturn
 move.w #1,d0
 move.w d0,above
 neg.w d6
below:
 cmp.w #1,d0
 bne.s dontdrawreturn
 move.w d6,distaddr
 muls #64,d6
 move.l d6,ypos
 divs d7,d6		; zpos of bottom
			; visible line
 move.w d6,minz
 move.w d7,bottomline

; Go round each point finding out
; if it should be visible or not.

 move.l a0,-(a7)

 move.w (a0)+,d7	; number of sides
 move.l #Rotated,a1
 move.l #OnScreen,a2
 move.l #NewCornerBuff,a3
 moveq #0,d4
 moveq #0,d5
 moveq #0,d6
 clr.b anyclipping
 
cornerprocessloop:

 move.w (a0)+,d0
 move.w 6(a1,d0.w*8),d1
 ble  .canttell
 
 move.w (a2,d0.w*2),d3
 cmp.w leftclip,d3
 bgt.s .nol
 st d4
 st anyclipping
 bra.s .nos
.nol:
 cmp.w rightclip,d3
 blt.s .nor
 st d6
 st anyclipping
 bra.s .nos
.nor:
 st d5
.nos:
 bra .cantell

.canttell:
 st d5
 st anyclipping

.cantell:


 dbra d7,cornerprocessloop
 
 move.l (a7)+,a0
 tst.b d5
 bne.s somefloortodraw
 eor.b d4,d6
 bne dontdrawreturn

somefloortodraw:

 move.w #80,top
 move.w #-1,bottom
 move.w #0,drawit
 move.l #Rotated,a1
 move.l #OnScreen,a2
 move.w (a0)+,d7	; no of sides
sideloop:
 move.w minz,d6
 move.w (a0)+,d1
 move.w (a0),d3
 move.w 6(a1,d1*8),d4	;first z
 cmp.w d6,d4
 bgt firstinfront
 move.w 6(a1,d3*8),d5	; sec z
 cmp.w d6,d5
 ble bothbehind
** line must be on left and partially behind.
 sub.w d5,d4
 move.l (a1,d1*8),d0
 sub.l (a1,d3*8),d0
 asr.l #7,d0
 sub.w d5,d6
 muls d6,d0	; new x coord
 divs d4,d0
 ext.l d0
 asl.l #7,d0

 add.l (a1,d3*8),d0
 move.w minz,d4
 move.w (a2,d3*2),d2
 divs d4,d0
 add.w #47,d0
 move.l ypos,d3
 divs d5,d3
 move.w bottomline,d1 
 bra lineclipped

firstinfront:
 move.w 6(a1,d3*8),d5	; sec z
 cmp.w d6,d5
 bgt bothinfront
** line must be on right and partially behind.
 sub.w d4,d5	; dz
 move.l (a1,d3*8),d2
 sub.l (a1,d1*8),d2	; dx
 sub.w d4,d6
 asr.l #7,d2
 muls d6,d2	; new x coord
 divs d5,d2
 ext.l d2
 asl.l #7,d2
 add.l (a1,d1*8),d2
 move.w minz,d5
 move.w (a2,d1*2),d0
 divs d5,d2
 add.w #47,d2
 move.l ypos,d1
 divs d4,d1
 move.w bottomline,d3 
 bra lineclipped

bothinfront:

* Also, usefully enough, both are on-screen
* so no bottom clipping is needed.

 move.w (a2,d1*2),d0	; first x
 move.w (a2,d3*2),d2	; second x
 move.l ypos,d1
 move.l d1,d3
 divs d4,d1		; first y
 divs d5,d3		; second y
lineclipped:
 move.l #rightsidetab,a3
 cmp.w d1,d3
 beq lineflat
 st drawit
 bgt lineonright
 move.l #leftsidetab,a3
 exg d1,d3
 exg d0,d2
 
 lea (a3,d1*2),a3
 
 cmp.w top(pc),d1
 bge.s .nonewtop
 move.w d1,top
.nonewtop:
 cmp.w bottom(pc),d3
 ble.s .nonewbot
 move.w d3,bottom
.nonewbot:

 sub.w d1,d3	; dy
 sub.w d0,d2	; dx
 
 blt .linegoingleft
 sub.w #1,d0

 ext.l d2
 divs d3,d2
 move.w d2,d6
 swap d2

; moveq #0,d6
; sub.w d3,d2
; blt.s .noco
;.makeco
; addq #1,d6
; sub.w d3,d2
; bge.s .makeco
;.noco
; add.w d3,d2
 
 move.w d3,d4
 move.w d3,d5
 subq #1,d5
 move.w d6,d1
 addq #1,d1

.pixlopright:
 move.w d0,(a3)+
 sub.w d2,d4
 bge.s .nobigstep
 add.w d1,d0
 add.w d3,d4
 dbra d5,.pixlopright
 bra lineflat
.nobigstep
 add.w d6,d0
 dbra d5,.pixlopright
 bra lineflat

.linegoingleft:

 sub.w #1,d0
 
 neg.w d2

 ext.l d2
 divs d3,d2
 move.w d2,d6
 swap d2


; moveq #0,d6
; sub.w d3,d2
; blt.s .nocol
;.makecol
; addq #1,d6
; sub.w d3,d2
; bge.s .makecol
;.nocol
; add.w d3,d2


 
 move.w d3,d4
 move.w d3,d5
 subq #1,d5

 move.w d6,d1
 addq #1,d1

.pixlopleft:
 sub.w d2,d4
 bge.s .nobigstepl
 sub.w d1,d0
 add.w d3,d4
 move.w d0,(a3)+
 dbra d5,.pixlopleft
 bra lineflat
 
.nobigstepl
 sub.w d6,d0
 move.w d0,(a3)+
 dbra d5,.pixlopleft
 bra lineflat
 
lineonright:

 lea (a3,d1*2),a3
 
 cmp.w top(pc),d1
 bge.s .nonewtop
 move.w d1,top
.nonewtop:
 cmp.w bottom(pc),d3
 ble.s .nonewbot
 move.w d3,bottom
.nonewbot:

 sub.w d1,d3	; dy
 sub.w d0,d2	; dx
 blt .linegoingleft
; addq #1,d0
 ext.l d2
 divs d3,d2
 move.w d2,d6
 swap d2

; moveq #0,d6
; sub.w d3,d2
; blt.s .noco
;.makeco
; addq #1,d6
; sub.w d3,d2
; bge.s .makeco
;.noco
; add.w d3,d2
 
 move.w d3,d4
 move.w d3,d5
 subq #1,d5
 move.w d6,d1
 addq #1,d1

.pixlopright:
 sub.w d2,d4
 bge.s .nobigstep
 add.w d1,d0
 add.w d3,d4
 move.w d0,(a3)+
 dbra d5,.pixlopright
 bra lineflat
 
.nobigstep
 add.w d6,d0
 move.w d0,(a3)+
 dbra d5,.pixlopright
 bra lineflat

.linegoingleft:
; addq #1,d0
 neg.w d2

 ext.l d2
 divs d3,d2
 move.w d2,d6
 swap d2


; moveq #0,d6
; sub.w d3,d2
; blt.s .nocol
;.makecol
; addq #1,d6
; sub.w d3,d2
; bge.s .makecol
;.nocol
; add.w d3,d2

 move.w d3,d4
 move.w d3,d5
 subq #1,d5
 move.w d6,d1
 addq #1,d1

.pixlopleft:
 move.w d0,(a3)+
 sub.w d2,d4
 bge.s .nobigstepl
 sub.w d1,d0
 add.w d3,d4
 dbra d5,.pixlopleft
 bra lineflat
 
.nobigstepl
 sub.w d6,d0
 dbra d5,.pixlopleft

lineflat:
 
bothbehind:
 dbra d7,sideloop

pastsides:


 addq #2,a0
 
 move.w #104*4,linedir
 move.l frompt,a6
 add.l #104*4*41,a6
 move.w (a0)+,scaleval
 move.w (a0)+,whichtile
 move.w (a0)+,d6
 add.w ZoneBright,d6
 move.w d6,lighttype
 move.w above(pc),d6
 beq groundfloor
* on ceiling:
 move.w #-104*4,linedir
 suba.w #104*4,a6
groundfloor:

 move.w xoff,d6
 move.w zoff,d7
 add.w xwobxoff,d7
 add.w xwobzoff,d6
 move.w scaleval(pc),d3
 beq.s .samescale
 bgt.s .scaledown
 neg.w d3
 asr.l d3,d7
 asr.l d3,d6
 bra.s .samescale
.scaledown:
 asl.l d3,d6
 asl.l d3,d7
.samescale
 asl.w #8,d7
 move.w d6,sxoff
 move.w d7,szoff
 bra pastscale 

 asr.l #3,d1
 asr.l #3,d2
 asr.l #2,d1
 asr.l #2,d2
 asr.l #1,d1
 asr.l #1,d2
scaleprogfrom
 nop
 nop
 asl.l #1,d1
 asl.l #1,d2
 asl.l #2,d1
 asl.l #2,d2
 asl.l #3,d1
 asl.l #3,d2

top: dc.w 0
bottom: dc.w 0
ypos: dc.l 0
nfloors: dc.w 0
lighttype: dc.w 0
above: dc.w 0 
linedir: dc.w 0
distaddr: dc.w 0
 
minz: dc.w 0
leftsidetab:
 ds.w 80
rightsidetab:
 ds.w 80
leftsideclip:
 ds.w 80
rightsideclip:
 ds.w 80

movespd: dc.w 0
largespd: dc.l 0

pastscale:

 tst.b drawit(pc)
 beq dontdrawfloor

 move.l a0,-(a7)

 move.l #leftsidetab,a4
 move.w top(pc),d1
 move.w bottom(pc),d7
 tst.w above
 beq.s clipfloor
 
 move.w #40,d3
 move.w d3,d4
 sub.w topclip,d3
 sub.w botclip,d4
 cmp.w d3,d1
 bge predontdrawfloor
 cmp.w d4,d7
 blt predontdrawfloor
 cmp.w d4,d1
 bge.s .nocliptoproof
 move.w d4,d1
.nocliptoproof
 cmp.w d3,d7
 blt doneclip
 move.w d3,d7
 bra doneclip
 
clipfloor:
 move.w botclip,d4
 sub.w #40,d4
 cmp.w d4,d1
 bge predontdrawfloor
 move.w topclip,d3
 sub.w #40,d3
 cmp.w d3,d1
 bge.s .nocliptopfloor
 move.w d3,d1
.nocliptopfloor 
 cmp.w d3,d7
 ble predontdrawfloor
 cmp.w d4,d7
 blt.s .noclipbotfloor
 move.w d4,d7
.noclipbotfloor:

doneclip:

 lea (a4,d1*2),a4
; move.l #dists,a2
 move.w distaddr,d0
 muls #64,d0
 move.l d0,a2
; muls #25,d0
; adda.w d0,a2
; lea (a2,d1*2),a2
 sub.w d1,d7
 ble predontdrawfloor 
 move.w d1,d0
 bne.s .notzero
 moveq #1,d0
.notzero
 muls linedir,d1
 add.l d1,a6
 move.l #floorscalecols,a1
 move.l LineToUse,a5
 
 tst.b anyclipping
 beq dofloornoclip
 
dofloor:
; move.w (a2)+,d0
 move.w leftclip(pc),d3
 move.w rightclip(pc),d4
 move.w rightsidetab-leftsidetab(a4),d2
 addq #1,d2
 cmp.w d3,d2
 ble.s nodrawline
 cmp.w d4,d2
 ble.s noclipright
 move.w d4,d2
noclipright:
 move.w (a4),d1
 cmp.w d4,d1
 bge.s nodrawline
 cmp.w d3,d1
 bge.s noclipleft
 move.w d3,d1
noclipleft:
 cmp.w d1,d2
 ble.s nodrawline

 move.w d1,leftedge
 move.w d2,rightedge
 
 move.l a6,a3
 movem.l d0/d7/a2/a4/a5/a6,-(a7)
 move.l a2,d7
 divs d0,d7
 move.w d7,d0
 jsr (a5)
 movem.l (a7)+,d0/d7/a2/a4/a5/a6
nodrawline
 adda.w linedir(pc),a6
 addq #2,a4
 addq #1,d0
 subq #1,d7
 bgt dofloor

predontdrawfloor
 move.l (a7)+,a0

dontdrawfloor:

 CACHE_FREEZE_OFF d2
 rts

anyclipping: dc.w 0

dofloornoclip:
; move.w (a2)+,d0
 move.w rightsidetab-leftsidetab(a4),d2
 addq #1,d2
 move.w (a4)+,leftedge
 move.w d2,rightedge

 move.l a6,a3
 movem.l d0/d7/a2/a4/a5/a6,-(a7)
 move.l a2,d7
 divs d0,d7
 move.w d7,d0
 jsr (a5)
 movem.l (a7)+,d0/d7/a2/a4/a5/a6
 adda.w linedir(pc),a6
 addq #1,d0
 subq #1,d7
 bgt dofloornoclip

 bra predontdrawfloor


dists:
; incbin "floordists"
drawit: dc.w 0

LineToUse: dc.l 0

***************************
* Right then, time for the floor
* routine...
* For test purposes, give it
* a3 = point to screen
* d0= z distance away
* and sinval+cosval must be set up.
***************************

leftedge: dc.w 0
rightedge: dc.w 0

rndpt: dc.l rndtab

WaterFloorLine:

 CACHE_OFF d2

 move.l rndpt,a2
 move.w (a2)+,d1
 move.w (a2)+,d2
 move.w (a2)+,d3
 cmp.l #endrnd-4,a2
 blt.s okrnd
 suba.w #98,a2
okrnd: 
 move.l a2,rndpt

 asr.w #6,d0
 move.w d0,d1
 move.w d0,d2
 move.w d0,d3
 move.l clipd(pc,d1.w*4),d1
 move.l clipd(pc,d2.w*4),d2
 move.l clipd(pc,d3.w*4),d3
 bra pcli

 dc.l 0
clipd:
 dc.l 0
 dc.l 512
 dc.l 512*2
 dc.l 512*3
 dc.l 512*4
 dc.l 512*5
 dc.l 512*6
 dc.l 512*7
 dc.l 512*8
 dc.l 512*9
 dc.l 512*10
 dc.l 512*11
 dc.l 512*12
 dc.l 512*13
 dc.l 512*14
 dc.l 512*15
 dc.l 512*15
 dc.l 512*15
 dc.l 512*15
 dc.l 512*15
 dc.l 512*15
 dc.l 512*15
 dc.l 512*15
 dc.l 512*15
 dc.l 512*15
 dc.l 512*15
 dc.l 512*15

pcli:

 move.l #brightentab,a2
 move.l a2,a4
 move.l a4,a5
 add.l d1,a2
 add.l d2,a4
 add.l d3,a5

 move.l #doacrossline,a1
 move.w leftedge(pc),d1
 move.w rightedge(pc),d2
 sub.w d1,d2
 move.w time(pc,d1.w*2),d1
 move.w time(pc,d2.w*2),d2
 lea (a1,d1.w),a1
 move.w (a1,d2.w),d4
 move.w #$4e75,(a1,d2.w)

 moveq #0,d0
 jsr (a1)
 move.w d4,(a1,d2.w)

 CACHE_ON d2

 rts
 
time:
val SET 0
 REPT 100
 dc.w val
val SET val+10
 ENDR
 
storeit: dc.l 0

doacrossline:
 incbin "Doacrossline"
 rts

dst: dc.w 0

FloorLine:

 move.l #floortile,a0
 adda.w whichtile,a0
 move.w lighttype,d1
 
 move.w d0,dst
 
 move.w d0,d2
*********************
* Old version
 asr.w #6,d2
 add.w #5,d1
*********************
; asr.w #3,d2
; sub.w #4,d2
; cmp.w #6,d2
; blt.s flbrbr
; move.w #6,d2
;flbrbr:
*********************
 add.w d2,d1
 bge.s .fixedbright
 moveq #0,d1
.fixedbright:
 cmp.w #28,d1
 ble.s .smallbright
 move.w #28,d1
.smallbright:
 lea floorscalecols,a1
 add.l floorbright(pc,d1.w*4),a1
 bra pastfloorbright
 
ConstCol: dc.w 0
 
BumpLine:

 tst.b smoothbumps
 beq.s Chunky
 
 move.l #SmoothTile,a0
 lea Smoothscalecols,a1
 bra pastast
 
Chunky:

 moveq #0,d2
 move.l #Bumptile,a0
 move.w whichtile,d2
 adda.w d2,a0
 ror.l #2,d2
 lsr.w #6,d2
 rol.l #2,d2
 and.w #15,d2
 move.l #ConstCols,a1
 move.w (a1,d2.w*2),ConstCol
 lea Bumpscalecols,a1
 
pastast:
 move.w lighttype,d1
 
 move.w d0,dst
 
 move.w d0,d2
*********************
* Old version
 asr.w #6,d2
 add.w #5,d1
*********************
; asr.w #3,d2
; sub.w #4,d2
; cmp.w #6,d2
; blt.s flbrbr
; move.w #6,d2
;flbrbr:
*********************
 add.w d2,d1
 bge.s .fixedbright
 moveq #0,d1
.fixedbright:
 cmp.w #28,d1
 ble.s .smallbright
 move.w #28,d1
.smallbright:
 add.l floorbright(pc,d1.w*4),a1
 bra pastfloorbright
 

floorbright:
 dc.l 512*0
 dc.l 512*1
 dc.l 512*1
 dc.l 512*2
 dc.l 512*2
 
 dc.l 512*3
 dc.l 512*3
 dc.l 512*4
 dc.l 512*4
 dc.l 512*5
 
 dc.l 512*5
 dc.l 512*6
 dc.l 512*6
 dc.l 512*7
 dc.l 512*7
 
 dc.l 512*8
 dc.l 512*8
 dc.l 512*9
 dc.l 512*9
 dc.l 512*10
 
 dc.l 512*10
 dc.l 512*11
 dc.l 512*11
 dc.l 512*12
 dc.l 512*12
 
 dc.l 512*13
 dc.l 512*13
 dc.l 512*14
 dc.l 512*14

widthleft: dc.w 0
scaleval: dc.w 0
sxoff: dc.w 0
szoff: dc.w 0
xoff34: dc.w 0
zoff34: dc.w 0
scosval: dc.w 0
ssinval: dc.w 0


floorsetbright:
 move.l #walltiles,a0

pastfloorbright


 move.w d0,d1
 muls cosval,d1	; change in x across whole width
 move.w d0,d2
 muls sinval,d2	; change in z across whole width
 neg.l d2
scaleprog:
 move.w scaleval(pc),d3
 beq.s .samescale
 bgt.s .scaledown
 neg.w d3
 asr.l d3,d1
 asr.l d3,d2
 bra.s .samescale
.scaledown:
 asl.l d3,d1
 asl.l d3,d2
.samescale

 move.l d1,d3 ;	z cos
 move.l d3,d6
 move.l d3,d5
 asr.l #1,d6
 add.l d6,d3
 asr.l #1,d3

 move.l d2,d4	; z sin
 move.l d4,d6
 asr.l #1,d6
 add.l d4,d6
 add.l d3,d4
 neg.l d4	; start x
 
 asr.l #1,d6	; zsin/2
 sub.l d6,d5	; start z
 
 move.w d4,startsmoothx
 move.w d5,startsmoothz
 
 swap d4
 asr.l #8,d5
 add.w szoff,d5
 add.w sxoff,d4
 and.w #63,d4
 and.w #63*256,d5
 move.b d4,d5

 asr.l #6,d1
 asr.l #6,d2
 move.w leftedge(pc),d6
 beq.s nomultleft
 
 move.l d1,d3
 asr.l #6,d3
 muls d6,d3
 asl.l #6,d3
 swap d3
 add.b d3,d5

 move.l d2,d3
 asr.l #6,d3
 muls d6,d3
 asl.l #6,d3
 swap d3
 lsl.w #8,d3
 add.w d3,d5
 
nomultleft:

 move.w d1,a4
 move.w d2,a5
 asr.l #8,d2
 and.w #%0011111100000000,d2
 swap d1
 add.w d1,d2
 move.w #%11111100111111,d1
 and.w d1,d5
 swap d5
 move.w startsmoothz,d5
 swap d5
 swap d2
 move.w a5,d2
 swap d2
 
***********************************
 
 move.w d6,a2
 move.l d2,d6
 add.w #256,d6
 
 moveq #0,d0

 tst.w a2
 beq startatleftedge
 
 move.w widthleft(pc),d4
 
 move.w rightedge(pc),d3
 
 cmp.w #31,a2
 bgt.s notinfirststrip
 lea (a3,a2.w*4),a3
 cmp.w #32,d3
 ble.s allinfirststrip
 move.w #32,d7
 sub.w d7,d3
 sub.w a2,d7
 bra intofirststrip

allinfirststrip
 sub.w a2,d3
 move.w d3,d7
 move.w #0,d4
 bra allintofirst

notinfirststrip:
 sub.w #32,a2
 sub.w #32,d3
 adda.w #33*4,a3
 cmp.w #31,a2
 bgt.s notstartinsec
 lea (a3,a2.w*4),a3
 cmp.w #32,d3
 ble.s allinsecstrip
 move.w #32,d7
 sub.w d7,d3
 sub.w a2,d7
 move.w d3,d4
 bra tstwat

allinsecstrip
 sub.w a2,d3
 move.w d3,d7
 move.w #0,d4
 bra tstwat
 rts
 
prot8: dc.w 0
 
notstartinsec:
 sub.w #32,a2
 sub.w #32,d3
 adda.w #33*4,a3
 lea (a3,a2.w*4),a3
 cmp.w #32,d3
 ble.s allinthirdstrip
 move.w #32,d7
 sub.w d7,d3
 sub.w a2,d7
 move.w d3,d4
 bra tstwat
 rts

allinthirdstrip
 sub.w a2,d3
 move.w d3,d7
 move.w #0,d4
 bra tstwat
 rts

startatleftedge:

 move.w rightedge(pc),d3
 sub.w a2,d3
 
 move.w d3,d7
 cmp.w #32,d7
 ble.s .notoowide
 move.w #32,d7
.notoowide:
 sub.w d7,d3
intofirststrip:

 move.w d3,d4
allintofirst:

 move.w startsmoothx,d3

tstwat:

 tst.b usewater
 bne texturedwater
 
 
******************************
* BumpMap the floor/ceiling! *
 tst.b usebumps
 bne.s BumpMap
******************************
 
ordinary:
 moveq #0,d0

 dbra d7,acrossscrn
 rts
 
usebumps: dc.w $0
smoothbumps: dc.w $0
 
 include "ab3:source/bumpmap.s"

 CNOP 0,4
backbefore:
 and.w d1,d5
 move.b (a0,d5.w*4),d0
 move.w (a1,d0.w*2),(a3)
 addq #4,a3
 add.w a4,d3
 addx.l d6,d5
 dbcs d7,acrossscrn
 dbcc d7,backbefore
 bcc.s past1
 add.w #256,d5 
 bra.s past1
 
acrossscrn:
 and.w d1,d5
 move.b (a0,d5.w*4),d0
 move.w (a1,d0.w*2),(a3)
 addq #4,a3
 add.w a4,d3
 addx.l d2,d5
 dbcs d7,acrossscrn
 dbcc d7,backbefore
 bcc.s past1
 add.w #256,d5 
past1:

 move.w d4,d7
 bne.s .notdoneyet
 CACHE_FREEZE_ON d2
 rts
.notdoneyet:

 cmp.w #32,d7
 ble.s .notoowide
 move.w #32,d7
.notoowide
 sub.w d7,d4  
 addq #4,a3
 
 dbra d7,acrossscrn
 CACHE_FREEZE_ON d2
 rts
 
waterpt: dc.l waterlist

waterlist:
 dc.l waterfile
 dc.l waterfile+2
 dc.l waterfile+256
 dc.l waterfile+256+2
 dc.l waterfile+512
 dc.l waterfile+512+2
 dc.l waterfile+768
 dc.l waterfile+768+2
; dc.l waterfile+768
; dc.l waterfile+512+2
; dc.l waterfile+512
; dc.l waterfile+256+2
; dc.l waterfile+256
; dc.l waterfile+2
endwaterlist:
 
watertouse: dc.l waterfile
 
wtan: dc.w 0
wateroff: dc.w 0
 
texturedwater:

 add.w wateroff,d5

 move.l #brightentab,a1
 move.w dst,d0
 clr.b d0
 add.w d0,d0
 adda.w d0,a1

 move.w dst,d0
 asl.w #7,d0
 add.w wtan,d0
 and.w #8191,d0
 move.l #SineTable,a0
 move.w (a0,d0.w),d0
 ext.l d0
 move.w dst,d3
 add.w #300,d3
 divs d3,d0
 asr.w #6,d0
 add.w #2,d0
 
 move.w dst,d3
 asr.w #7,d3
 add.w d3,d0
 
 muls #104*4,d0
 move.w d0,a6

 move.l watertouse,a0

 move.w startsmoothx,d3
 dbra d7,acrossscrnw
 rts

backbeforew:
 and.w d1,d5
 move.w (a0,d5.w*4),d0
 move.b 1(a3,a6.w),d0
 move.w (a1,d0.w*2),(a3)
 addq #4,a3
 add.w a4,d3
 addx.l d6,d5
 dbcs d7,acrossscrnw
 dbcc d7,backbeforew
 bcc.s past1w
 add.w #256,d5 
 bra.s past1w
 
acrossscrnw:
 and.w d1,d5
 move.w (a0,d5.w*4),d0
 move.b 1(a3,a6.w),d0
 move.w (a1,d0.w*2),(a3)
 addq #4,a3
 add.w a4,d3
 addx.l d2,d5
 dbcs d7,acrossscrnw
 dbcc d7,backbeforew
 bcc.s past1w
 add.w #256,d5 
past1w:

 move.w d4,d7
 bne.s .notdoneyet
 rts
.notdoneyet:

 cmp.w #32,d7
 ble.s .notoowide
 move.w #32,d7
.notoowide
 sub.w d7,d4  
 addq #4,a3
 
 dbra d7,acrossscrnw
 CACHE_FREEZE_ON d2
 rts

usewater: dc.w 0
startsmoothx: dc.w 0
startsmoothz: dc.w 0

********************************
*
 include "AB3:source/ObjDraw3.chipram"
*
********************************

numframes:
 dc.w 0

alframe: dc.l Objects+4096
 
alan:
 dcb.l 4,0
 dcb.l 4,1
 dcb.l 4,2
 dcb.l 4,3
endalan:

alanptr: dc.l alan

Time2: dc.l 0
dispco:
 dc.w 0

KInt_Init	;VBR Assumed $0
		move.l $68.w,OLDKINT
		Move.l	#KInt_Main,$68.w	Install Interrupt 
		And.b	#$3f,$bfe201		Set Timers
		Move.b	#$7f,$bfed01
		Move.b	$bfed01,d0
		Move.b	#$88,$bfed01
		St.b	KInt_CCode		
		Move.b	#$a0,$bfee01		Start Timey Thing
		Rts				And return

OLDKINT: dc.l 0

KInt_Main	
		Movem.l	d0/d1/a0/a1/a6,-(a7)	Stack everything
		Move.w	#8,$dff09a		Temp Disable Int.
		Move.w	$dff01e,d0		Intreqr
		And.w	#8,d0			Mask Out All X^ K_Int
	Beq	KInt_End			Not Keyboard Interrupt
		Lea	$bfed01,a6
		Move.w	#$8,$dff09c		Clear Int.Request
		Move.b	-$100(a6),d0		Move Raw Keyboard value
		Ror.b	#1,d0			Roll to correct
		Not.b	d0			
		Move.b	d0,KInt_CCode		Save Corrected Keycode
.HandShake	Move.b	#8,(a6)
		Move.b	#7,-$900(a6)
		Move.b	#0,-$800(a6)
		Move.b	#0,-$100(a6)
		Move.b	#$d1,$100(a6)		
		Tst.b	(a6)	
.wait		Btst	#0,(a6)
	Beq.s	.wait
		Move.b	#$a0,$100(a6)		
		Move.b	(a6),d0		
		Move.b	#$88,(a6)
		Lea	KeyMap,a1
		Moveq.w	#0,d0
		Move.b	KInt_CCode(pc),d0
	Bmi.s	KInt_KeyUp			neg if up 

KInt_KeyDown
		st (a1,d0.w)
	Bra	KInt_End

KInt_KeyUp
		And.w	#$7f,d0			Make code Positive
		clr.b (a1,d0.w)
KInt_End	Movem.l	(a7)+,d0/d1/a0/a1/a6	Unstack Everything
	
		Move.w	#$8008,$dff09a		Re-enable Int.
		Rte

KInt_CCode	Ds.b	1
KInt_Askey	Ds.b	1
KInt_OCode	Ds.w	1

 
PLR1_mouse_control
 jsr ReadMouse
 jsr PLR1_alwayskeys
 move.l #SineTable,a0
 move.w PLR1s_angspd,d1
 move.w PLR1s_angpos,d0
 and.w #8190,d0
 move.w (a0,d0.w),PLR1s_sinval
 adda.w #2048,a0
 move.w (a0,d0.w),PLR1s_cosval

 move.l PLR1s_xspdval,d6
 move.l PLR1s_zspdval,d7

 neg.l d6
 ble.s .nobug1
 asr.l #1,d6
 add.l #1,d6
 bra.s .bug1
.nobug1
 asr.l #1,d6
.bug1:

 neg.l d7
 ble.s .nobug2
 asr.l #1,d7
 add.l #1,d7
 bra.s .bug2
.nobug2
 asr.l #1,d7
.bug2: 

 
 move.w ymouse,d3
 sub.w oldymouse,d3
 add.w d3,oldymouse
 asr.w #1,d3
 cmp.w #50,d3
 ble.s nofastfor
 move.w #50,d3
nofastfor:
 cmp.w #-50,d3
 bge.s nofastback
 move.w #-50,d3
nofastback:

 tst.b Ducked
 beq.s .nohalve
 asr.w #1,d3
.nohalve

 move.w d3,d2
 asl.w #3,d2
 move.w d2,d1
 add.w d2,d1
 add.w d2,d1
 add.w Bobble,d1
 and.w #8190,d1
 move.w d1,Bobble

 move.w PLR1s_sinval,d1
 move.w PLR1s_cosval,d2
 
 move.w d2,d4
 move.w d1,d5
 muls lrs,d4
 muls lrs,d5
 

 muls d3,d2
 muls d3,d1
 sub.l d4,d1
 add.l d5,d2

 sub.l d1,d6
 sub.l d2,d7
 add.l d6,PLR1s_xspdval
 add.l d7,PLR1s_zspdval
 move.l PLR1s_xspdval,d6
 move.l PLR1s_zspdval,d7
 add.l d6,PLR1s_xoff
 add.l d7,PLR1s_zoff

 tst.b PLR1_fire
 beq.s .firenotpressed
; fire was pressed last time.
 btst #6,$bfe001
 bne.s .firenownotpressed
; fire is still pressed this time.
 st PLR1_fire
 bra .doneplr1
 
.firenownotpressed:
; fire has been released.
 clr.b PLR1_fire
 bra .doneplr1
 
.firenotpressed

; fire was not pressed last frame...

 btst #6,$bfe001
; if it has still not been pressed, go back above
 bne.s .firenownotpressed
; fire was not pressed last time, and was this time, so has
; been clicked.
 st PLR1_clicked
 st PLR1_fire

.doneplr1:
 move.l PLR1s_tyoff,d0
 move.l PLR1s_yoff,d1
 move.l PLR1s_yvel,d2
 sub.l d1,d0
 bgt.s .aboveground
 sub.l #1024,d2
 blt.s .notfast
 sub.l #2048,d2
.notfast:
 add.l d2,d1
 sub.l d2,d0
 blt.s .pastitall
 move.l #0,d2
 add.l d0,d1
 bra.s .pastitall

.aboveground:
 add.l d2,d1
 add.l #1024,d2
.pastitall:

 move.l d2,PLR1s_yvel
 move.l d1,PLR1s_yoff
 
 rts

PLR1_follow_path:

 move.l pathpt,a0
 move.w (a0),d1
 move.w d1,PLR1s_xoff
 move.w 2(a0),d1
 move.w d1,PLR1s_zoff
 move.w 4(a0),d0
 add.w d0,d0
 and.w #8190,d0
 move.w d0,PLR1_angpos

 move.w TempFrames,d0
 asl.w #3,d0
 adda.w d0,a0
 
 cmp.l #endpath,a0
 blt notrestartpath
 move.l #Path,a0
notrestartpath:
 move.l a0,pathpt

 rts
 
OldSpace: dc.b 0
SpaceTapped: dc.b 0
SPCTAP: dc.b 0
Ducked: dc.b 0
 even

PLR1_alwayskeys
 move.l #KeyMap,a5
 move.b $40(a5),d1
 beq.s nottapped
 tst.b OldSpace
 bne.s nottapped
 st SpaceTapped
nottapped:
 move.b d1,OldSpace

 tst.b $22(a5)
 beq.s notduck
 clr.b $22(a5)
 move.l #playerheight,PLR1s_targheight
 not.b Ducked
 beq.s notduck
 move.l #playercrouched,PLR1s_targheight
notduck:

 move.l PLR1_Roompt,a4
 move.l ToZoneFloor(a4),d0
 sub.l ToZoneRoof(a4),d0
 tst.b PLR1_StoodInTop
 beq.s usebottom
 move.l ToUpperFloor(a4),d0
 sub.l ToUpperRoof(a4),d0
usebottom:

 cmp.l #playerheight+3*1024,d0
 bgt.s oktostand
 st Ducked
 move.l #playercrouched,PLR1s_targheight
oktostand:

 move.l PLR1s_height,d0
 move.l PLR1s_targheight,d1
 cmp.l d1,d0
 beq.s noupordown
 bgt.s crouch
 add.l #1024,d0
 bra noupordown
crouch:
 sub.l #1024,d0
noupordown:
 move.l d0,PLR1s_height

 tst.b $27(a5)
 beq.s notselkey
 st PLR1KEYS
 clr.b PLR1PATH
 clr.b PLR1MOUSE
 clr.b PLR1JOY
notselkey:

 tst.b $26(a5)
 beq.s notseljoy
 clr.b PLR1KEYS
 clr.b PLR1PATH
 clr.b PLR1MOUSE
 st PLR1JOY
notseljoy:

 tst.b $37(a5)
 beq.s notselmouse
 clr.b PLR1KEYS
 clr.b PLR1PATH
 st PLR1MOUSE
 clr.b PLR1JOY
notselmouse:

 lea 1(a5),a4
 lea GunData,a3
 moveq #0,d0
 move.w #4,d1
pickweap
 tst.b (a4)+
 beq.s notgotweap
 tst.b 7(a3)
 beq.s notgotweap
 move.b d0,GunSelected
notgotweap
 addq #1,d0
 adda.w #16,a3
 dbra d1,pickweap

 rts

PLR1_keyboard_control:

 move.l #SineTable,a0
 
 jsr PLR1_alwayskeys
 move.l #KeyMap,a5

 move.w PLR1s_angpos,d0
 move.w #70,d1
 move.w #7,d2
 tst.b $61(a5)
 beq.s nofaster
 move.w #120,d1
 move.w #10,d2
nofaster:
 tst.b Ducked
 beq.s .nohalve
 asr.w #1,d2
.nohalve

 moveq #0,d4 
; tst.b $67(a5)
; bne.s slidelr
 
 tst.b $4f(a5)
 beq.s noleftturn
 sub.w d1,d0
noleftturn
 move.l #KeyMap,a5
 tst.b $4e(a5)
 beq.s norightturn
 add.w d1,d0
norightturn
; bra.s noslide

slidelr:
 tst.b $39(a5)
 beq.s noleftslide
 move.w d2,d4
 asr.w #1,d4
noleftslide
 move.l #KeyMap,a5
 tst.b $3a(a5)
 beq.s norightslide
 sub.w d2,d4
 asr.w #1,d4
norightslide
  
noslide:
  
 and.w #8191,d0
 move.w d0,PLR1s_angpos
 
 move.w (a0,d0.w),PLR1s_sinval
 adda.w #2048,a0
 move.w (a0,d0.w),PLR1s_cosval

 move.l PLR1s_xspdval,d6
 move.l PLR1s_zspdval,d7

 neg.l d6
 ble.s .nobug1
 asr.l #1,d6
 add.l #1,d6
 bra.s .bug1
.nobug1
 asr.l #1,d6
.bug1:

 neg.l d7
 ble.s .nobug2
 asr.l #1,d7
 add.l #1,d7
 bra.s .bug2
.nobug2
 asr.l #1,d7
.bug2: 

 moveq #0,d3
 
 tst.b $4c(a5)
 beq.s noforward
 neg.w d2
 move.w d2,d3
 
noforward:
 tst.b $4d(a5)
 beq.s nobackward
 move.w d2,d3
nobackward:
 
 move.w d3,d2
 asl.w #3,d2
 move.w d2,d1
 add.w d2,d1
 add.w d2,d1
 add.w Bobble,d1
 and.w #8190,d1
 move.w d1,Bobble
 
 move.w PLR1s_sinval,d1
 muls d3,d1
 move.w PLR1s_cosval,d2
 muls d3,d2

 sub.l d1,d6
 sub.l d2,d7
 move.w PLR1s_sinval,d1
 muls d4,d1
 move.w PLR1s_cosval,d2
 muls d4,d2
 sub.l d2,d6
 add.l d1,d7
 
 add.l d6,PLR1s_xspdval
 add.l d7,PLR1s_zspdval
 move.l PLR1s_xspdval,d6
 move.l PLR1s_zspdval,d7
 add.l d6,PLR1s_xoff
 add.l d7,PLR1s_zoff
 
 tst.b PLR1_fire
 beq.s .firenotpressed
; fire was pressed last time.
 tst.b $65(a5)
 beq.s .firenownotpressed
; fire is still pressed this time.
 st PLR1_fire
 bra .doneplr1
 
.firenownotpressed:
; fire has been released.
 clr.b PLR1_fire
 bra .doneplr1
 
.firenotpressed

; fire was not pressed last frame...

 tst.b $65(a5)
; if it has still not been pressed, go back above
 beq.s .firenownotpressed
; fire was not pressed last time, and was this time, so has
; been clicked.
 st PLR1_clicked
 st PLR1_fire

.doneplr1:

 move.l PLR1s_tyoff,d0
 move.l PLR1s_yoff,d1
 move.l PLR1s_yvel,d2
 sub.l d1,d0
 bgt.s .aboveground
 sub.l #512,d2
 blt.s .notfast
 move.l #0,d2
.notfast:
 add.l d2,d1
 sub.l d2,d0
 blt.s .pastitall
 move.l #0,d2
 add.l d0,d1
 bra.s .pastitall

.aboveground:
 add.l d2,d1
 add.l #1024,d2
.pastitall:

 move.l d2,PLR1s_yvel
 move.l d1,PLR1s_yoff

 rts
passspace:
 ds.l 400 

PLR1_JoyStick_control:

 move.l #KeyMap,a5
 move.l #SineTable,a0

 btst #1,$dff00c
 sne d0
 btst #1,$dff00d
 sne d1
 btst #0,$dff00c
 sne d2
 btst #0,$dff00d
 sne d3
 btst #7,$bfe001
 seq $65(a5)

 move.b d0,$4f(a5)
 move.b d1,$4e(a5)
 eor.b d0,d2
 move.b d2,$4c(a5)
 eor.b d1,d3
 move.b d3,$4d(a5)
 
 jsr PLR1_alwayskeys

 move.w PLR1s_angpos,d0
 move.w #70,d1
 move.w #7,d2
 tst.b $61(a5)
 beq.s .nofaster
 move.w #120,d1
 move.w #10,d2
.nofaster:

 tst.b Ducked
 beq.s .nohalve
 asr.w #1,d2
.nohalve:

 moveq #0,d4 
; tst.b $67(a5)
; bne.s slidelr
 
 tst.b $4f(a5)
 beq.s .noleftturn
 sub.w d1,d0
.noleftturn
 move.l #KeyMap,a5
 tst.b $4e(a5)
 beq.s .norightturn
 add.w d1,d0
.norightturn
; bra.s noslide

.slidelr:
 tst.b $39(a5)
 beq.s .noleftslide
 move.w d2,d4
 asr.w #1,d4
.noleftslide
 move.l #KeyMap,a5
 tst.b $3a(a5)
 beq.s .norightslide
 sub.w d2,d4
 asr.w #1,d4
.norightslide
  
.noslide:
  
 and.w #8191,d0
 move.w d0,PLR1s_angpos
 
 move.w (a0,d0.w),PLR1s_sinval
 adda.w #2048,a0
 move.w (a0,d0.w),PLR1s_cosval

 move.l PLR1s_xspdval,d6
 move.l PLR1s_zspdval,d7

 neg.l d6
 ble.s .nobug1
 asr.l #1,d6
 add.l #1,d6
 bra.s .bug1
.nobug1
 asr.l #1,d6
.bug1:

 neg.l d7
 ble.s .nobug2
 asr.l #1,d7
 add.l #1,d7
 bra.s .bug2
.nobug2
 asr.l #1,d7
.bug2: 

 moveq #0,d3
 
 tst.b $4c(a5)
 beq.s .noforward
 neg.w d2
 move.w d2,d3
.noforward:
 tst.b $4d(a5)
 beq.s .nobackward
 move.w d2,d3
.nobackward:
 
 move.w d3,d2
 asl.w #3,d2
 move.w d2,d1
 add.w d2,d1
 add.w d2,d1
 add.w Bobble,d1
 and.w #8190,d1
 move.w d1,Bobble
 
 move.w PLR1s_sinval,d1
 muls d3,d1
 move.w PLR1s_cosval,d2
 muls d3,d2

 sub.l d1,d6
 sub.l d2,d7
 move.w PLR1s_sinval,d1
 muls d4,d1
 move.w PLR1s_cosval,d2
 muls d4,d2
 sub.l d2,d6
 add.l d1,d7
 
 add.l d6,PLR1s_xspdval
 add.l d7,PLR1s_zspdval
 move.l PLR1s_xspdval,d6
 move.l PLR1s_zspdval,d7
 add.l d6,PLR1s_xoff
 add.l d7,PLR1s_zoff
 
 tst.b PLR1_fire
 beq.s .firenotpressed
; fire was pressed last time.
 tst.b $65(a5)
 beq.s .firenownotpressed
; fire is still pressed this time.
 st PLR1_fire
 bra .doneplr1
 
.firenownotpressed:
; fire has been released.
 clr.b PLR1_fire
 bra .doneplr1
 
.firenotpressed

; fire was not pressed last frame...

 tst.b $65(a5)
; if it has still not been pressed, go back above
 beq.s .firenownotpressed
; fire was not pressed last time, and was this time, so has
; been clicked.
 st PLR1_clicked
 st PLR1_fire

.doneplr1:

 move.l PLR1s_tyoff,d0
 move.l PLR1s_yoff,d1
 move.l PLR1s_yvel,d2
 sub.l d1,d0
 bgt.s .aboveground
 sub.l #512,d2
 blt.s .notfast
 move.l #0,d2
.notfast:
 add.l d2,d1
 sub.l d2,d0
 blt.s .pastitall
 move.l #0,d2
 add.l d0,d1
 bra.s .pastitall

.aboveground:
 add.l d2,d1
 add.l #1024,d2
.pastitall:

 move.l d2,PLR1s_yvel
 move.l d1,PLR1s_yoff

 rts

prot7: dc.w 0
SendToSlave:

; First of all, position of player

 jsr INITSEND

 move.w PLR1_xoff,d0
 swap d0
 move.w PLR1_zoff,d0
 jsr SENDLONG

 move.l PLR1_yoff,d0
 jsr SENDLONG

 move.b PLR1_StoodInTop,d0
 lsl.w #8,d0
 move.l PLR1_Roompt,a2
 move.b 1(a2),d0
 swap d0
 move.w PLR1_angpos,d0
 jsr SENDLONG
 
 move.l ObjectData,a5
 move.l ObjectPoints,a6
SendObjLoop
 move.w (a5),d1
 blt.s lastonedone
 move.w 12(a5),d2
 blt.s notthisonedear
 tst.b worry(a5)
 beq.s notthisonedear
 move.w (a6,d1.w*8),d0
 swap d0
 move.w 4(a6,d1.w*8),d0
 jsr SENDLONG
 move.w 4(a5),d0
 swap d0
 clr.w d0
 move.b d2,d0
 lsl.w #8,d0
 move.b ObjInTop(a5),d0
 jsr SENDLONG
 move.w 6(a5),d0
 swap d0
 move.w 14(a5),d0
 jsr SENDLONG
 move.l 8(a5),d0
 jsr SENDLONG

notthisonedear:
 add.w #64,a5
 bra SendObjLoop

lastonedone:

 move.l #$80000000,d0
 jsr SENDLAST

 rts
 
GOTTOSEND: dc.w 0

OtherInter:
 move.w #$0010,$dff000+intreq
 movem.l d0-d7/a0-a6,-(a7)
 bra.s justshake

Chan0inter:

 move.w #$0010,$dff000+intreq

 tst.b doanything
 bne.s dosomething
 rte
 
dosomething:


 addq.w #1,FramesToDraw
 tst.b counting
 beq nostopcounter
 JSR STOPCOUNTNOADD
nostopcounter:
 movem.l d0-d7/a0-a6,-(a7)
 
; jsr INITREC
; jsr RECEIVE
 
; tst.l BUFFER
; beq.s justshake
; st GOTTOSEND
; move.l #OtherInter,$6c

justshake:
 
 cmp.b #'b',Prefsfile+3
 bne.s .noback
 jsr mt_music
.noback:
 
 bra dontshowtime
 
 tst.b oktodisplay
 beq dontshowtime
 clr.b oktodisplay
 subq.w #1,dispco
 bgt dontshowtime
 move.w #10,dispco
 
 move.l #TimerScr+10,a0
 move.l TimeCount,d0
 bge.s timenotneg
 move.l #1111*256,d0
timenotneg:
 asr.l #8,d0
 move.l #digits,a1
 move.w #7,d2
digitlop
 divs #10,d0
 swap d0
 lea (a1,d0.w*8),a2
 move.b (a2)+,(a0)
 move.b (a2)+,24(a0)
 move.b (a2)+,24*2(a0)
 move.b (a2)+,24*3(a0)
 move.b (a2)+,24*4(a0)
 move.b (a2)+,24*5(a0)
 move.b (a2)+,24*6(a0)
 move.b (a2)+,24*7(a0)
 subq #1,a0
 swap d0
 ext.l d0
 dbra d2,digitlop

 move.l #TimerScr+10+24*10,a0
 move.l NumTimes,d0
 move.l #digits,a1
 move.w #3,d2
digitlop2
 divs #10,d0
 swap d0
 lea (a1,d0.w*8),a2
 move.b (a2)+,(a0)
 move.b (a2)+,24(a0)
 move.b (a2)+,24*2(a0)
 move.b (a2)+,24*3(a0)
 move.b (a2)+,24*4(a0)
 move.b (a2)+,24*5(a0)
 move.b (a2)+,24*6(a0)
 move.b (a2)+,24*7(a0)
 subq #1,a0
 swap d0
 ext.l d0
 dbra d2,digitlop2

 move.l #TimerScr+10+24*20,a0
 moveq #0,d0
 move.w FramesToDraw,d0
 move.l #digits,a1
 move.w #2,d2
digitlop3
 divs #10,d0
 swap d0
 lea (a1,d0.w*8),a2
 move.b (a2)+,(a0)
 move.b (a2)+,24(a0)
 move.b (a2)+,24*2(a0)
 move.b (a2)+,24*3(a0)
 move.b (a2)+,24*4(a0)
 move.b (a2)+,24*5(a0)
 move.b (a2)+,24*6(a0)
 move.b (a2)+,24*7(a0)
 subq #1,a0
 swap d0
 ext.l d0
 dbra d2,digitlop3

dontshowtime:

 move.w Robotanimpos,d0
 add.w #6*38,d0
 cmp.w #6*38*64,d0
 blt.s norebot
 move.w #0,d0
norebot:
 move.w d0,Robotanimpos
 
 tst.w d0
 seq d1
 cmp.w #6*32*38,d0
 seq d2
 or.b d2,d1
 or.b d1,clump
 
 move.w Robotarmpos,d0
 add.w #6*14,d0
 cmp.w #6*14*64,d0
 blt.s norebot2
 move.w #0,d0
norebot2:
 move.w d0,Robotarmpos
 
 move.l alanptr,a0
 move.l (a0)+,alframe
 cmp.l #endalan,a0
 blt.s nostartalan
 move.l #alan,a0
nostartalan:
 move.l a0,alanptr
 
 move.l #$dff000,a6

 cmp.b #'4',Prefsfile+1
 bne.s nomuckabout
 
 move.w #$0,d0 
 tst.b NoiseMade0LEFT
 beq.s noturnoff0
 move.w #1,d0
noturnoff0:
 tst.b NoiseMade0RIGHT
 beq.s noturnoff1
 or.w #2,d0
noturnoff1:
 tst.b NoiseMade1RIGHT
 beq.s noturnoff2
 or.w #4,d0
noturnoff2:
 tst.b NoiseMade1LEFT
 beq.s noturnoff3
 or.w #8,d0
noturnoff3:
 move.w d0,dmacon(a6)
 
nomuckabout:

 tst.b PLR1MOUSE
 beq.s PLR1_nomouse
 bsr PLR1_mouse_control
PLR1_nomouse:
 tst.b PLR1KEYS
 beq.s PLR1_nokeys
 bsr PLR1_keyboard_control
PLR1_nokeys:
; tst.b PLR1PATH
; beq.s PLR1_nopath
; bsr PLR1_follow_path
;PLR1_nopath:
 tst.b PLR1JOY
 beq.s PLR1_nojoy
 bsr PLR1_JoyStick_control
PLR1_nojoy: 
 
; tst.b PLR2_fire
; beq.s firenotpressed2
; fire was pressed last time.
; btst #7,$bfe001
; bne.s firenownotpressed2
; fire is still pressed this time.
; st PLR2_fire
; bra dointer
 
firenownotpressed2:
; fire has been released.
; clr.b PLR2_fire
; bra dointer
 
firenotpressed2

; fire was not pressed last frame...

; btst #7,$bfe001
; if it has still not been pressed, go back above
; bne.s firenownotpressed2
; fire was not pressed last time, and was this time, so has
; been clicked.
; st PLR2_clicked
; st PLR2_fire
 
dointer
 
 cmp.b #'4',Prefsfile+1
 beq fourchannel
 
 btst #1,$dff000+intreqr
 bne.s newsampbitl

 movem.l (a7)+,d0-d7/a0-a6
 tst.b counting
 beq .nostartcounter
 JSR STARTCOUNT
.nostartcounter:
noneed:
 
 rte
 
 
swappedem: dc.w 0
 
newsampbitl:

 move.w #$820f,$dff000+dmacon

 move.w #$200,$dff000+intreq
 
; tst.b CHANNELDATA
; bne nochannel0
 
 move.l pos0LEFT,a0
 move.l pos2LEFT,a1

 move.l #tab,a2
 
 moveq #0,d0
 moveq #0,d1
 move.b vol0left,d0
 move.b vol2left,d1
 cmp.b d1,d0
 slt swappedem
 bge.s fbig0

; d1 is bigger so scale d0 and use d1
; as audiochannel volume.

 exg a0,a1
 asl.w #6,d0
 divs d1,d0
 lsl.w #8,d0
 adda.w d0,a2
 move.w d1,$dff0a8
 bra.s donechan0

fbig0:
 tst.w d0
 beq.s donechan0
 asl.w #6,d1
 divs d0,d1
 lsl.w #8,d1
 adda.w d1,a2
 move.w d0,$dff0a8

donechan0:
 
 move.l Aupt0,a3
 move.l a3,$dff0a0
 move.l Auback0,Aupt0
 move.l a3,Auback0
 
 move.l Auback0,a3
 
 moveq #0,d0
 moveq #0,d1
 moveq #0,d2
 moveq #0,d3
 moveq #0,d4
 moveq #0,d5
 move.w #49,d7
loop:
 move.l (a0)+,d0
 move.b (a1)+,d1
 move.b (a1)+,d2
 move.b (a1)+,d3
 move.b (a1)+,d4
 move.b (a2,d3.w),d5
 swap d5
 move.b (a2,d1.w),d5
 asl.l #8,d5
 move.b (a2,d2.w),d5
 swap d5
 move.b (a2,d4.w),d5
 add.l d5,d0
 move.l d0,(a3)+
 dbra d7,loop

 tst.b swappedem
 beq.s .ok23
 exg a0,a1
.ok23:
 
 cmp.l Samp0endLEFT,a0
 blt.s .notoffendsamp1
 move.l #bass,a0
 move.l #bassend,Samp0endLEFT
 move.b #63,vol0left
 st LEFTCHANDATA+1
 move.w #0,LEFTCHANDATA+2
.notoffendsamp1:

 cmp.l Samp2endLEFT,a1
 blt.s .notoffendsamp2
 move.l #empty,a1
 move.l #emptyend,Samp2endLEFT
 move.b #0,vol2left
 st LEFTCHANDATA+1+8
 move.w #0,LEFTCHANDATA+2+8
.notoffendsamp2:
 
 move.l a0,pos0LEFT
 move.l a1,pos2LEFT

nochannel0:

 tst.b CHANNELDATA+16
 bne nochannel1

 
 move.l pos0RIGHT,a0
 move.l pos2RIGHT,a1

 move.l Aupt1,a3
 move.l a3,$dff0b0
 move.l Auback1,Aupt1
 move.l a3,Auback1

 move.l #tab,a2
 
 moveq #0,d0
 moveq #0,d1
 move.b vol0right,d0
 move.b vol2right,d1
 cmp.b d1,d0
 slt swappedem
 bge.s fbig1

; d1 is bigger so scale d0 and use d1
; as audiochannel volume.

 exg a0,a1
 asl.w #6,d0
 divs d1,d0
 lsl.w #8,d0
 adda.w d0,a2
 move.w d1,$dff0b8
 bra.s donechan1

fbig1:
 tst.w d0
 beq.s donechan1
 asl.w #6,d1
 divs d0,d1
 lsl.w #8,d1
 adda.w d1,a2
 move.w d0,$dff0b8

donechan1:
 moveq #0,d0
 moveq #0,d1
 moveq #0,d2
 moveq #0,d3
 moveq #0,d4
 moveq #0,d5
 move.w #49,d7
loop2:
 move.l (a0)+,d0
 move.b (a1)+,d1
 move.b (a1)+,d2
 move.b (a1)+,d3
 move.b (a1)+,d4
 move.b (a2,d3.w),d5
 swap d5
 move.b (a2,d1.w),d5
 asl.l #8,d5
 move.b (a2,d2.w),d5
 swap d5
 move.b (a2,d4.w),d5
 add.l d5,d0
 move.l d0,(a3)+
 dbra d7,loop2
 
 tst.b swappedem
 beq.s ok01
 exg a0,a1
ok01:
 
 cmp.l Samp0endRIGHT,a0
 blt.s .notoffendsamp1
 move.l #empty,a0
 move.l #emptyend,Samp0endRIGHT
 move.b #0,vol0right
 st RIGHTCHANDATA+1
 move.w #0,RIGHTCHANDATA+2
.notoffendsamp1:

 cmp.l Samp2endRIGHT,a1
 blt.s .notoffendsamp2
 move.l #empty,a1
 move.l #emptyend,Samp2endRIGHT
 move.b #0,vol2right
 st RIGHTCHANDATA+1+8
 move.w #0,RIGHTCHANDATA+2+8
.notoffendsamp2:

 move.l a0,pos0RIGHT
 move.l a1,pos2RIGHT

nochannel1:

******************* Other two channels

 move.l pos1LEFT,a0
 move.l pos3LEFT,a1

 move.l #tab,a2
 
 moveq #0,d0
 moveq #0,d1
 move.b vol1left,d0
 move.b vol3left,d1
 cmp.b d1,d0
 slt swappedem
 bge.s fbig2

; d1 is bigger so scale d0 and use d1
; as audiochannel volume.

 exg a0,a1
 asl.w #6,d0
 divs d1,d0
 lsl.w #8,d0
 adda.w d0,a2
 move.w d1,$dff0d8
 bra.s donechan2

fbig2:
 tst.w d0
 beq.s donechan2
 asl.w #6,d1
 divs d0,d1
 lsl.w #8,d1
 adda.w d1,a2
 move.w d0,$dff0d8

donechan2:

 move.l Aupt2,a3
 move.l a3,$dff0d0
 move.l Auback2,Aupt2
 move.l a3,Auback2
 
 moveq #0,d0
 moveq #0,d1
 moveq #0,d2
 moveq #0,d3
 moveq #0,d4
 moveq #0,d5
 move.w #49,d7
loop3:
 move.l (a0)+,d0
 move.b (a1)+,d1
 move.b (a1)+,d2
 move.b (a1)+,d3
 move.b (a1)+,d4
 move.b (a2,d3.w),d5
 swap d5
 move.b (a2,d1.w),d5
 asl.l #8,d5
 move.b (a2,d2.w),d5
 swap d5
 move.b (a2,d4.w),d5
 add.l d5,d0
 move.l d0,(a3)+
 dbra d7,loop3

 tst.b swappedem
 beq.s .ok23
 exg a0,a1
.ok23:

 cmp.l Samp1endLEFT,a0
 blt.s .notoffendsamp3
 move.l #empty,a0
 move.l #emptyend,Samp1endLEFT
 move.b #0,vol1left
 st LEFTCHANDATA+1+4
 move.w #0,LEFTCHANDATA+2+4
.notoffendsamp3:

 cmp.l Samp3endLEFT,a1
 blt.s .notoffendsamp4
 move.l #empty,a1
 move.l #emptyend,Samp3endLEFT
 move.b #0,vol3left
 st LEFTCHANDATA+1+12
 move.w #0,LEFTCHANDATA+2+12
.notoffendsamp4:

 move.l a0,pos1LEFT
 move.l a1,pos3LEFT
 
 move.l pos1RIGHT,a0
 move.l pos3RIGHT,a1

 move.l Aupt3,a3
 move.l a3,$dff0c0
 move.l Auback3,Aupt3
 move.l a3,Auback3

 move.l #tab,a2
 
 moveq #0,d0
 moveq #0,d1
 move.b vol1right,d0
 move.b vol3right,d1
 cmp.b d1,d0
 slt.s swappedem
 bge.s fbig3

 exg a0,a1
 asl.w #6,d0
 divs d1,d0
 lsl.w #8,d0
 adda.w d0,a2
 move.w d1,$dff0c8
 bra.s donechan3

fbig3:
 tst.w d0
 beq.s donechan3
 asl.w #6,d1
 divs d0,d1
 lsl.w #8,d1
 adda.w d1,a2
 move.w d0,$dff0c8
donechan3:

 moveq #0,d0
 moveq #0,d1
 moveq #0,d2
 moveq #0,d3
 moveq #0,d4
 moveq #0,d5
 move.w #49,d7
loop4:
 move.l (a0)+,d0
 move.b (a1)+,d1
 move.b (a1)+,d2
 move.b (a1)+,d3
 move.b (a1)+,d4
 move.b (a2,d3.w),d5
 swap d5
 move.b (a2,d1.w),d5
 asl.l #8,d5
 move.b (a2,d2.w),d5
 swap d5
 move.b (a2,d4.w),d5
 add.l d5,d0
 move.l d0,(a3)+
 dbra d7,loop4
 
 tst.b swappedem
 beq.s .ok23
 exg a0,a1
.ok23:
 
 cmp.l Samp1endRIGHT,a0
 blt.s notoffendsamp3
 move.l #empty,a0
 move.l #emptyend,Samp1endRIGHT
 move.b #0,vol1right
 st RIGHTCHANDATA+1+4
 move.w #0,RIGHTCHANDATA+2+4
notoffendsamp3:

 cmp.l Samp3endRIGHT,a1
 blt.s notoffendsamp4
 move.l #empty,a1
 move.l #emptyend,Samp3endRIGHT
 move.b #0,vol3right
 st RIGHTCHANDATA+1+12
 move.w #0,RIGHTCHANDATA+2+12
notoffendsamp4:

 move.l a0,pos1RIGHT
 move.l a1,pos3RIGHT

 movem.l (a7)+,d0-d7/a0-a6
 tst.b counting
 beq .nostartcounter
 JSR STARTCOUNT
.nostartcounter:

 rte
 
***********************************
* 4 channel sound routine
***********************************

fourchannel:

 move.l #$dff000,a6

 btst #7,intreqrl(a6)
 beq.s nofinish0
 move.w #0,LEFTCHANDATA+2
 st LEFTCHANDATA+1
 move.l #bass,$a0(a6)
 move.w #18370/2,$a4(a6) 
 move.w #$0080,intreq(a6)
nofinish0:
 
 tst.b NoiseMade0pLEFT
 beq.s NoChan0sound

 move.l Samp0endLEFT,d0
 move.l pos0LEFT,d1
 sub.l d1,d0
 asr.w #1,d0
 move.w d0,$a4(a6)
 move.l d1,$a0(a6)
 move.w #$8201,dmacon(a6)
 moveq #0,d0
 move.b vol0left,d0
 move.w d0,$a8(a6)

NoChan0sound:

 btst #0,intreqr(a6)
 beq.s nofinish1
 move.w #0,RIGHTCHANDATA+2
 st RIGHTCHANDATA+1
 move.l #null,$b0(a6)
 move.w #100,$b4(a6)
 move.w #$0100,intreq(a6)
nofinish1:

 tst.b NoiseMade0pRIGHT
 beq.s NoChan1sound

 move.l Samp0endRIGHT,d0
 move.l pos0RIGHT,d1
 sub.l d1,d0
 asr.w #1,d0
 move.w d0,$b4(a6)
 move.l d1,$b0(a6)
 move.w #$8202,dmacon(a6)
 moveq #0,d0
 move.b vol0right,d0
 move.w d0,$b8(a6)

NoChan1sound:


 btst #1,intreqr(a6)
 beq.s nofinish2
 move.w #0,RIGHTCHANDATA+2+4
 st RIGHTCHANDATA+1+4
 move.l #null,$c0(a6)
 move.w #100,$c4(a6)
 move.w #$0200,intreq(a6)
nofinish2:

 tst.b NoiseMade1pRIGHT
 beq.s NoChan2sound

 move.l Samp1endRIGHT,d0
 move.l pos1RIGHT,d1
 sub.l d1,d0
 asr.w #1,d0
 move.w d0,$c4(a6)
 move.l d1,$c0(a6)
 move.w #$8204,dmacon(a6)
 moveq #0,d0
 move.b vol1right,d0
 move.w d0,$c8(a6)

NoChan2sound:

 btst #2,intreqr(a6)
 beq.s nofinish3
 move.w #0,LEFTCHANDATA+2+4
 st LEFTCHANDATA+1+4
 move.l #null,$d0(a6)
 move.w #100,$d4(a6)
 move.w #$0400,intreq(a6)
nofinish3:

 tst.b NoiseMade1pLEFT
 beq.s NoChan3sound

 move.l Samp1endLEFT,d0
 move.l pos1LEFT,d1
 sub.l d1,d0
 asr.w #1,d0
 move.w d0,$d4(a6)
 move.l d1,$d0(a6)
 move.w #$8208,dmacon(a6)
 moveq #0,d0
 move.b vol1left,d0
 move.w d0,$d8(a6)
 
NoChan3sound:
 
nomorechannels:

 move.l NoiseMade0LEFT,NoiseMade0pLEFT
 move.l #0,NoiseMade0LEFT
 move.l NoiseMade0RIGHT,NoiseMade0pRIGHT
 move.l #0,NoiseMade0RIGHT

 movem.l (a7)+,d0-d7/a0-a6
 tst.b counting
 beq .nostartcounter
 JSR STARTCOUNT
.nostartcounter:

 rte
 
backbeat: dc.w 0

Samp0endRIGHT: dc.l emptyend
Samp1endRIGHT: dc.l emptyend
Samp2endRIGHT: dc.l emptyend
Samp3endRIGHT: dc.l emptyend
Samp0endLEFT: dc.l emptyend
Samp1endLEFT: dc.l emptyend
Samp2endLEFT: dc.l emptyend
Samp3endLEFT: dc.l emptyend

Aupt0: dc.l null
Auback0: dc.l null+500
Aupt2: dc.l null3
Auback2: dc.l null3+500
Aupt3: dc.l null4
Auback3: dc.l null4+500
Aupt1: dc.l null2
Auback1: dc.l null2+500

NoiseMade0LEFT: dc.b 0
NoiseMade1LEFT: dc.b 0
NoiseMade2LEFT: dc.b 0
NoiseMade3LEFT: dc.b 0
NoiseMade0pLEFT: dc.b 0
NoiseMade1pLEFT: dc.b 0
NoiseMade2pLEFT: dc.b 0
NoiseMade3pLEFT: dc.b 0
NoiseMade0RIGHT: dc.b 0
NoiseMade1RIGHT: dc.b 0
NoiseMade2RIGHT: dc.b 0
NoiseMade3RIGHT: dc.b 0
NoiseMade0pRIGHT: dc.b 0
NoiseMade1pRIGHT: dc.b 0
NoiseMade2pRIGHT: dc.b 0
NoiseMade3pRIGHT: dc.b 0

empty: ds.l 100
emptyend:
 
**************************************
* I want a routine to calculate all the
* info needed for the sound player to
* work, given say position of noise, volume
* and sample number.

Samplenum: dc.w 0
Noisex: dc.w 0
Noisez: dc.w 0
Noisevol: dc.w 0
chanpick: dc.w 0
IDNUM: dc.w 0
needleft: dc.b 0
needright: dc.b 0
STEREO: dc.b $0
even
prot6: dc.w 0

 even
 
CHANNELDATA:
LEFTCHANDATA:
 dc.l $FF000000
 dc.l $00000000
 dc.l $00000000
 dc.l $00000000
RIGHTCHANDATA:
 dc.l $00000000
 dc.l $00000000
 dc.l $00000000
 dc.l $00000000
 
RIGHTPLAYEDTAB: ds.l 20
LEFTPLAYEDTAB: ds.l 20
 
MakeSomeNoise:

; Plan for new sound handler:
; It is sent a sample number,
; a position relative to the
; player, an id number and a volume.
; Also notifplaying.

; indirect inputs are the available
; channel flags and whether or not
; stereo sound is selected.

; the algorithm must decide
; whether the new sound is more
; important than the ones already
; playing. Thus an 'importance'
; must be calculated, probably
; using volume.

; The output needs to be:

; Write the pointers and volumes of
; the sound channels


 tst.b notifplaying
 beq.s dontworry

; find if we are already playing

 move.b IDNUM,d0
 move.w #7,d1
 lea CHANNELDATA,a3
findsameasme
 tst.b (a3)
 bne.s notavail
 cmp.b 1(a3),d0
 beq SameAsMe
notavail:
 add.w #4,a3
 dbra d1,findsameasme
 bra dontworry
SameAsMe
 rts

dontworry:

; Ok its fine for us to play a sound.
; So calculate left/right volume.

 move.w Noisex,d1
 muls d1,d1
 move.w Noisez,d2
 muls d2,d2
 move.w #64,d3
 moveq #1,d0
 add.l d1,d2
 beq pastcalc

 move.w #31,d0
.findhigh
 btst d0,d2
 bne .foundhigh
 dbra d0,.findhigh
.foundhigh
 asr.w #1,d0
 clr.l d3
 bset d0,d3
 move.l d3,d0

 move.w d0,d3
 muls d3,d3	; x*x
 sub.l d2,d3	; x*x-a
 asr.l #1,d3	; (x*x-a)/2
 divs d0,d3	; (x*x-a)/2x
 sub.w d3,d0	; second approx
 bgt .stillnot0
 move.w #1,d0
.stillnot0

 move.w d0,d3
 muls d3,d3
 sub.l d2,d3
 asr.l #1,d3
 divs d0,d3
 sub.w d3,d0	; second approx
 bgt .stillnot02
 move.w #1,d0
.stillnot02
 
 move.w #64,d3
 muls Noisevol,d3
 asr.w #1,d0
 addq #1,d0
 divs d0,d3

 cmp.w #64,d3
 ble.s notooloud
 move.w #64,d3
notooloud:

pastcalc:

	; d3 contains volume of noise.
	
 move.w d3,d4
 
 move.w d3,d2
 muls Noisex,d2
 add.w d0,d0
 divs d0,d2
 
 bgt.s quietleft
 add.w d2,d4
 bge.s donequiet
 move.w #0,d4
 bra.s donequiet
quietleft:
 sub.w d2,d3
 bge.s donequiet
 move.w #0,d3
donequiet:

; d3=leftvol?
; d4=rightvol?

 clr.w needleft

 cmp.b d3,d4
 bgt.s RightLouder
 
; Left is louder; is it MUCH louder?

 st needleft
 move.w d3,d2
 sub.w d4,d2
 cmp.w #32,d2
 slt needright
 bra aboutsame
 
RightLouder:
 st needright
 move.w d4,d2
 sub.w d3,d2
 cmp.w #32,d2
 slt needleft
 
aboutsame:

 tst.b STEREO
 beq NOSTEREO

; Find least important sound on left

 move.l #0,a2
 move.l #0,d5
 move.w #10000,d2
 move.b IDNUM,d0
 lea LEFTCHANDATA,a3
 move.w #3,d1
FindLeftChannel
 tst.b (a3)
 bne.s .notactive
 cmp.b 1(a3),d0
 beq.s FOUNDLEFT
 cmp.w 2(a3),d2
 blt.s .notactive
 move.w 2(a3),d2
 move.l a3,a2
 move.w d5,d6

.notactive:
 add.w #4,a3
 add.w #1,d5
 dbra d1,FindLeftChannel
 move.l a2,a3
 bra.s gopastleft
FOUNDLEFT:
 move.w d5,d6
gopastleft:
 tst.l a3
 bne.s FOUNDALEFT
 rts
FOUNDALEFT:

; d6 = channel number
 move.b d0,1(a3)
 move.w d3,2(a3)

 move.w Samplenum,d5
 move.l #SampleList,a3
 move.l (a3,d5.w*8),a1
 move.l 4(a3,d5.w*8),a2

 tst.b d6
 seq NoiseMade0LEFT
 beq.s .chan0
 cmp.b #2,d6
 slt NoiseMade1LEFT
 blt .chan1
 seq NoiseMade2LEFT
 beq .chan2
 st NoiseMade3LEFT

 move.b d5,LEFTPLAYEDTAB+9
 move.b d3,LEFTPLAYEDTAB+1+9
 move.b d4,LEFTPLAYEDTAB+2+9
 move.b d3,vol3left
 move.l a1,pos3LEFT
 move.l a2,Samp3endLEFT
 bra dorightchan
 
.chan0: 
 move.b d5,LEFTPLAYEDTAB
 move.b d3,LEFTPLAYEDTAB+1
 move.b d4,LEFTPLAYEDTAB+2
 move.l a1,pos0LEFT
 move.l a2,Samp0endLEFT
 move.b d3,vol0left
 bra dorightchan
 
.chan1:
 move.b d5,LEFTPLAYEDTAB+3
 move.b d3,LEFTPLAYEDTAB+1+3
 move.b d4,LEFTPLAYEDTAB+2+3
 move.b d3,vol1left
 move.l a1,pos1LEFT
 move.l a2,Samp1endLEFT
 bra dorightchan

.chan2: 
 move.b d5,LEFTPLAYEDTAB+6
 move.b d3,LEFTPLAYEDTAB+1+6
 move.b d4,LEFTPLAYEDTAB+2+6
 move.l a1,pos2LEFT
 move.l a2,Samp2endLEFT
 move.b d3,vol2left
 
dorightchan:

; Find least important sound on right

 move.l #0,a2
 move.l #0,d5
 move.w #10000,d2
 move.b IDNUM,d0
 lea RIGHTCHANDATA,a3
 move.w #3,d1
FindRightChannel
 tst.b (a3)
 bne.s .notactive
 cmp.b 1(a3),d0
 beq.s FOUNDRIGHT
 cmp.w 2(a3),d2
 blt.s .notactive
 move.w 2(a3),d2
 move.l a3,a2
 move.w d5,d6

.notactive:
 add.w #4,a3
 add.w #1,d5
 dbra d1,FindRightChannel
 move.l a2,a3
 bra.s gopastright
FOUNDRIGHT:
 move.w d5,d6
gopastright:
 tst.l a3
 bne.s FOUNDARIGHT
 rts
FOUNDARIGHT:

; d6 = channel number
 move.b d0,1(a3)
 move.w d3,2(a3)

 move.w Samplenum,d5
 move.l #SampleList,a3
 move.l (a3,d5.w*8),a1
 move.l 4(a3,d5.w*8),a2

 tst.b d6
 seq NoiseMade0RIGHT
 beq.s .chan0
 cmp.b #2,d6
 slt NoiseMade1RIGHT
 blt .chan1
 seq NoiseMade2RIGHT
 beq .chan2
 st NoiseMade3RIGHT

 move.b d5,RIGHTPLAYEDTAB+9
 move.b d3,RIGHTPLAYEDTAB+1+9
 move.b d4,RIGHTPLAYEDTAB+2+9
 move.b d4,vol3right
 move.l a1,pos3RIGHT
 move.l a2,Samp3endRIGHT
 rts
 
.chan0: 
 move.b d5,RIGHTPLAYEDTAB
 move.b d3,RIGHTPLAYEDTAB+1
 move.b d4,RIGHTPLAYEDTAB+2
 move.l a1,pos0RIGHT
 move.l a2,Samp0endRIGHT
 move.b d4,vol0right
 rts
 
.chan1:
 move.b d5,RIGHTPLAYEDTAB+3
 move.b d3,RIGHTPLAYEDTAB+1+3
 move.b d4,RIGHTPLAYEDTAB+2+3
 move.b d3,vol1right
 move.l a1,pos1RIGHT
 move.l a2,Samp1endRIGHT
 rts

.chan2: 
 move.b d5,RIGHTPLAYEDTAB+6
 move.b d3,RIGHTPLAYEDTAB+1+6
 move.b d4,RIGHTPLAYEDTAB+2+6
 move.l a1,pos2RIGHT
 move.l a2,Samp2endRIGHT
 move.b d3,vol2right
 rts

NOSTEREO:
 move.l #0,a2
 move.l #0,d5
 move.w #10000,d2
 move.b IDNUM,d0
 lea CHANNELDATA,a3
 move.w #7,d1
FindChannel
 tst.b (a3)
 bne.s .notactive
 cmp.b 1(a3),d0
 beq.s FOUNDCHAN
 cmp.w 2(a3),d2
 blt.s .notactive
 move.w 2(a3),d2
 move.l a3,a2
 move.w d5,d6

.notactive:
 add.w #4,a3
 add.w #1,d5
 dbra d1,FindChannel
 move.l a2,a3
 bra.s gopastchan
FOUNDCHAN:
 move.w d5,d6
gopastchan:
 tst.l a3
 bne.s FOUNDACHAN
tooquiet:
 rts
FOUNDACHAN:

; d6 = channel number

 cmp.w d2,d3
 blt.s tooquiet

 move.b d0,1(a3)
 move.w d3,2(a3)

 move.w Samplenum,d5
 move.l #SampleList,a3
 move.l (a3,d5.w*8),a1
 move.l 4(a3,d5.w*8),a2

 tst.b d6
 seq NoiseMade0LEFT
 beq .chan0
 cmp.b #2,d6
 slt NoiseMade1LEFT
 blt .chan1
 seq NoiseMade2LEFT
 beq .chan2
 cmp.b #4,d6
 slt NoiseMade3LEFT
 blt .chan3
 seq NoiseMade0RIGHT
 beq .chan4
 cmp.b #6,d6
 slt NoiseMade1RIGHT
 blt .chan5
 seq NoiseMade2RIGHT
 beq .chan6
 st NoiseMade3RIGHT

 move.b d5,RIGHTPLAYEDTAB+9
 move.b d3,RIGHTPLAYEDTAB+1+9
 move.b d4,RIGHTPLAYEDTAB+2+9
 move.b d4,vol3right
 move.l a1,pos3RIGHT
 move.l a2,Samp3endRIGHT
 rts

.chan3:
 move.b d5,LEFTPLAYEDTAB+9
 move.b d3,LEFTPLAYEDTAB+1+9
 move.b d4,LEFTPLAYEDTAB+2+9
 move.b d3,vol3left
 move.l a1,pos3LEFT
 move.l a2,Samp3endLEFT
 bra dorightchan
 
.chan0: 
 move.b d5,LEFTPLAYEDTAB
 move.b d3,LEFTPLAYEDTAB+1
 move.b d4,LEFTPLAYEDTAB+2
 move.l a1,pos0LEFT
 move.l a2,Samp0endLEFT
 move.b d3,vol0left
 rts
 
.chan1:
 move.b d5,LEFTPLAYEDTAB+3
 move.b d3,LEFTPLAYEDTAB+1+3
 move.b d4,LEFTPLAYEDTAB+2+3
 move.b d3,vol1left
 move.l a1,pos1LEFT
 move.l a2,Samp1endLEFT
 rts

.chan2: 
 move.b d5,LEFTPLAYEDTAB+6
 move.b d3,LEFTPLAYEDTAB+1+6
 move.b d4,LEFTPLAYEDTAB+2+6
 move.l a1,pos2LEFT
 move.l a2,Samp2endLEFT
 move.b d3,vol2left
 rts
 
.chan4: 
 move.b d5,RIGHTPLAYEDTAB
 move.b d3,RIGHTPLAYEDTAB+1
 move.b d4,RIGHTPLAYEDTAB+2
 move.l a1,pos0RIGHT
 move.l a2,Samp0endRIGHT
 move.b d4,vol0right
 rts
 
.chan5:
 move.b d5,RIGHTPLAYEDTAB+3
 move.b d3,RIGHTPLAYEDTAB+1+3
 move.b d4,RIGHTPLAYEDTAB+2+3
 move.b d3,vol1right
 move.l a1,pos1RIGHT
 move.l a2,Samp1endRIGHT
 rts

.chan6: 
 move.b d5,RIGHTPLAYEDTAB+6
 move.b d3,RIGHTPLAYEDTAB+1+6
 move.b d4,RIGHTPLAYEDTAB+2+6
 move.l a1,pos2RIGHT
 move.l a2,Samp2endRIGHT
 move.b d3,vol2right
 rts

SampleList:
 dc.l Scream,EndScream
 dc.l Shoot,EndShoot
 dc.l Munch,EndMunch
 dc.l PooGun,EndPooGun
 dc.l Collect,EndCollect
;5
 dc.l DoorNoise,EndDoorNoise
 dc.l 0,0
 dc.l Stomp,EndStomp
 dc.l LowScream,EndLowScream
 dc.l BaddieGun,EndBaddieGun
;10
 dc.l SwitchNoise,EndSwitch
 dc.l Reload,EndReload
 dc.l NoAmmo,EndNoAmmo
 dc.l Splotch,EndSplotch
 dc.l SplatPop,EndSplatPop
;15
 dc.l Boom,EndBoom
 dc.l Hiss,EndHiss
 dc.l Howl1,EndHowl1
 dc.l Howl2,EndHowl2
 dc.l Pant,EndPant
;20
 dc.l Whoosh,EndWhoosh

saveinters:  
 dc.w 0

z: dc.w 10

notifplaying:
 dc.w 0

audpos1: dc.w 0
audpos1b: dc.w 0
audpos2: dc.w 0
audpos2b: dc.w 0
audpos3: dc.w 0
audpos3b: dc.w 0
audpos4: dc.w 0
audpos4b: dc.w 0

vol0left: dc.w 0
vol0right: dc.w 0
vol1left: dc.w 0
vol1right: dc.w 0
vol2left: dc.w 0
vol2right: dc.w 0
vol3left: dc.w 0
vol3right: dc.w 0

pos: dc.l 0

pos0LEFT: dc.l empty
pos1LEFT: dc.l empty
pos2LEFT: dc.l empty
pos3LEFT: dc.l empty
pos0RIGHT: dc.l empty
pos1RIGHT: dc.l empty
pos2RIGHT: dc.l empty
pos3RIGHT: dc.l empty

numtodo dc.w 0

npt: dc.w 0

pretab:
val SET 0
 REPT 128
 dc.b val
val SET val+1
 ENDR
val SET -128
 REPT 128
 dc.b val
val SET val+1
 ENDR 

tab:
 ds.b 256*65


test: dc.l 0
 ds.l 30

 even
ConstCols:
 incbin "ConstCols"
 even
Smoothscalecols:
; incbin "smoothbumppalscaled"
 even
SmoothTile:
; incbin "smoothbumptile"
 even
Bumpscalecols:
; incbin "Bumppalscaled"
 even
Bumptile:
; incbin "bumptile"
 even
scalecols: incbin "bytepixpalscaled"
 even
floorscalecols: incbin "floorpalscaled"
 even
PaletteAddr: dc.l 0
ChunkAddr: dc.l 0
walltiles:
 dc.l GreenMechanicWALL
 dc.l BlueGreyMetalWALL
 dc.l TechnoDetailWALL
 dc.l BlueStoneWALL
 dc.l RedAlertWALL
 dc.l RockWALL

GreenMechanicWALL: incbin "ab3:includes/walls/greenmechanic.wad"
BlueGreyMetalWALL: incbin "ab3:includes/walls/BlueGreyMetal.wad"
TechnoDetailWALL: incbin "ab3:includes/walls/TechnoDetail.wad"
BlueStoneWALL: incbin "ab3:includes/walls/bluestone.wad"
RedAlertWALL: incbin "ab3:includes/walls/redalert.wad"
RockWALL: incbin "ab3:includes/walls/rock.wad"
 
floortile:
 incbin "floortile" 
 even
wallrouts:
; incbin "2x2WallDraw" 
 CNOP 0,64
BackPicture:
 incbin "backfile"
EndBackPicture:

drawpt: dc.l colbars2
olddrawpt: dc.l colbars
frompt: dc.l 0 
 
SineTable:
 incbin "bigsine"

angpos: dc.w 0
angspd: dc.w 0
flooryoff: dc.w 0
xoff: dc.l 0
yoff: dc.l 0
yvel: dc.l 0
zoff: dc.l 0
tyoff: dc.l 0
xspdval: dc.l 0
zspdval: dc.l 0
Zone: dc.w 0

PLR1: dc.b $ff
 even
PLR1_cosval: dc.w 0
PLR1_sinval: dc.w 0
PLR1_angpos: dc.w 0
PLR1_angspd: dc.w 0
PLR1_xoff: dc.l 0
PLR1_yoff: dc.l 0
PLR1_yvel: dc.l 0
PLR1_zoff: dc.l 0
PLR1_tyoff: dc.l 0
PLR1_xspdval: dc.l 0
PLR1_zspdval: dc.l 0
PLR1_Zone: dc.w 0
PLR1_Roompt: dc.l 0
PLR1_OldRoompt: dc.l 0
PLR1_PointsToRotatePtr: dc.l 0
PLR1_ListOfGraphRooms: dc.l 0
PLR1_oldxoff: dc.l 0
PLR1_oldzoff: dc.l 0
PLR1_StoodInTop: dc.b 0
 even
PLR1_height: dc.l 0

 ds.w 4

PLR1s_cosval: dc.w 0
PLR1s_sinval: dc.w 0
PLR1s_angpos: dc.w 0
PLR1s_angspd: dc.w 0
PLR1s_xoff: dc.l 0
PLR1s_yoff: dc.l 0
PLR1s_yvel: dc.l 0
PLR1s_zoff: dc.l 0
PLR1s_tyoff: dc.l 0
PLR1s_xspdval: dc.l 0
PLR1s_zspdval: dc.l 0
PLR1s_Zone: dc.w 0
PLR1s_Roompt: dc.l 0
PLR1s_OldRoompt: dc.l 0
PLR1s_PointsToRotatePtr: dc.l 0
PLR1s_ListOfGraphRooms: dc.l 0
PLR1s_oldxoff: dc.l 0
PLR1s_oldzoff: dc.l 0
PLR1s_height: dc.l 0
PLR1s_targheight: dc.l 0

 ds.w 4

PLR2: dc.b $0
 even
PLR2_cosval: dc.w 0
PLR2_sinval: dc.w 0
PLR2_angpos: dc.w 0
PLR2_angspd: dc.w 0
PLR2_xoff: dc.l 0
PLR2_yoff: dc.l 0
PLR2_yvel: dc.l 0
PLR2_zoff: dc.l 0
PLR2_tyoff: dc.l 0
PLR2_xspdval: dc.l 0
PLR2_zspdval: dc.l 0
PLR2_Zone: dc.w 0
PLR2_Roompt: dc.l 0
PLR2_OldRoompt: dc.l 0
PLR2_PointsToRotatePtr: dc.l 0
PLR2_ListOfGraphRooms: dc.l 0
PLR2_ForwardSpd: dc.w 0

liftanimtab:

endliftanimtab:
 
glassball:
 incbin "glassball.inc"
endglass
glassballpt: dc.l glassball
 
rndtab: ; incbin "randfile"
endrnd: 
 
brightanimtab:
 dcb.w 200,20
 dc.w 5
 dc.w 10,20
 dc.w 5
 dcb.w 30,20
 dc.w 7,10,10,5,10,0,5,6,5,6,5,6,5,6,0
 dcb.w 40,0
 dc.w 1,2,3,2,3,2,3,2,3,2,3,2,3,0
 dcb.w 300,0
 dc.w 1,0,1,0,2,2,2,5,5,5,5,5,5,5,5,5,6,10
 dc.w -1

Roompt: dc.l 0
OldRoompt: dc.l 0

*****************************************************************
 *
 include "AB3:source/LevelData2"
 *
*****************************************************************


wallpt: dc.l 0
floorpt: dc.l 0

Rotated:
 ds.l 800 
ObjRotated:
 ds.l 800

OnScreen:
 ds.l 800 
 
startwait: dc.w 0
endwait: dc.w 0

Faces: incbin "faces2raw"

*******************************************************************

consttab:
 incbin "constantfile"

*******************************************************************
 
darkentab: incbin "darkenedcols"
brightentab: incbin "brightenfile"
WorkSpace:
 ds.l 8192 
waterfile: incbin "waterfile"

 SECTION ffff,CODE_C

nullspr: dc.l 0
 
 cnop 0,8
borders:
 incbin "leftbord"
 incbin "rightbord"

health: incbin "healthstrip"
Ammunition: incbin "ammostrip"
healthpal: incbin "healthpal"
PanelKeys: incbin "greenkey"
 incbin "redkey"
 incbin "yellowkey"
 incbin "bluekey"

null: ds.w 500
null2: ds.w 500
null3: ds.w 500
null4: ds.w 500


Blurbfield:

 dc.w bpl1ptl
bl1l: dc.w 0
 dc.w bpl1pth
bl1h: dc.w 0

 dc.w diwstart,$2c81
 dc.w diwstop,$1cc1
 dc.w ddfstart,$38
 dc.w ddfstop,$b8
 dc.w bplcon0,$9201
 dc.w bplcon1,0
 dc.w $106,$c40
blcols:
 dc.w col0,0
 dc.w col1,$fff

 dc.w $108,0
 dc.w $10a,0

 dc.w $ffff,$fffe
 dc.w $ffff,$fffe

nullline:
 ds.b 80

bigfield:    
                ; Start of our copper list.

 dc.w dmacon,$8020
 dc.w intreq,$8011
 dc.w $1fc,$f
 dc.w diwstart
winstart: dc.w $2cb1
 dc.w diwstop
winstop: dc.w $2c91
 dc.w ddfstart
fetchstart: dc.w $48
 dc.w ddfstop
fetchstop: dc.w $88

bordercols:
 incbin "borderpal"

 dc.w spr0ptl
s0l:
 dc.w 0
 dc.w spr0pth
s0h:
 dc.w 0
 dc.w spr1ptl
s1l:
 dc.w 0
 dc.w spr1pth
s1h:
 dc.w 0
 dc.w spr2ptl
s2l:
 dc.w 0
 dc.w spr2pth
s2h:
 dc.w 0
 dc.w spr3ptl
s3l:
 dc.w 0
 dc.w spr3pth
s3h:
 dc.w 0
 dc.w spr4ptl
s4l:
 dc.w 0
 dc.w spr4pth
s4h:
 dc.w 0
 dc.w spr5ptl
s5l:
 dc.w 0
 dc.w spr5pth
s5h:
 dc.w 0
 dc.w spr6ptl
s6l:
 dc.w 0
 dc.w spr6pth
s6h:
 dc.w 0
 dc.w spr7ptl
s7l:
 dc.w 0
 dc.w spr7pth
s7h:
 dc.w 0


 dc.w $106,$c42
 incbin "borderpal"

 dc.w $106,$8c42
 dc.w col0
hitcol: dc.w $0
 dc.w $106,$c42
 dc.w col0
hitcol2: dc.w 0

 dc.w bplcon0,$7201
 dc.w bplcon1
smoff:
 dc.w $0

 dc.w $108
modulo: dc.w -24
 dc.w $10a,-24

 dc.w bpl1pth
pl1h
 dc.w 0

 dc.w bpl1ptl
pl1l
 dc.w 0

 dc.w bpl2pth
pl2h
 dc.w 0

 dc.w bpl2ptl
pl2l
 dc.w 0

 dc.w bpl3pth
pl3h
 dc.w 0

 dc.w bpl3ptl
pl3l
 dc.w 0

 dc.w bpl4pth
pl4h
 dc.w 0

 dc.w bpl4ptl
pl4l
 dc.w 0

 dc.w bpl5pth
pl5h
 dc.w 0

 dc.w bpl5ptl
pl5l
 dc.w 0

 dc.w bpl6pth
pl6h
 dc.w 0

 dc.w bpl6ptl
pl6l
 dc.w 0

 dc.w bpl7pth
pl7h
 dc.w 0

 dc.w bpl7ptl
pl7l
 dc.w 0


 dc.w $1001,$ff00
 dc.w intreq,$11
yposcop:
 dc.w $2a11,$fffe
 dc.w $8a,0
 
 ds.l 104*12
 
colbars:
val SET $2a
 dcb.l 104*80,$1fe0000
 dc.w $106,$c42
 
 dc.w $80
pch1:
 dc.w 0
 dc.w $82
pcl1:
 dc.w 0 
 dc.w $88,0
 
 dc.w $ffff,$fffe       ; End copper list.

 ds.l 104*12

colbars2:
val SET $2a
 dcb.l 104*80,$1fe0000
 
 dc.w $106,$c42
 
 dc.w $80
pch2:
 dc.w 0
 dc.w $82
pcl2:
 dc.w 0
 
 dc.w $88,0
 
 dc.w $ffff,$fffe       ; End copper list.

 ds.l 104*10

old: dc.l 0

prot5: dc.w 0
PanelCop:

 dc.w $10c,0
 dc.w bplcon0,$1201
 dc.w bpl1ptl
n1l:
 dc.w 0
 dc.w bpl1pth
n1h:
 dc.w 0
 dc.w $108,-24
 incbin "Panelpal"

 dc.w bpl2pth
p2h
 dc.w 0

 dc.w bpl2ptl
p2l
 dc.w 0

 dc.w bpl3pth
p3h
 dc.w 0

 dc.w bpl3ptl
p3l
 dc.w 0

 dc.w bpl4pth
p4h
 dc.w 0
 dc.w bpl4ptl
p4l
 dc.w 0
 dc.w bpl5pth
p5h
 dc.w 0
 dc.w bpl5ptl
p5l
 dc.w 0
 dc.w bpl6pth
p6h
 dc.w 0
 dc.w bpl6ptl
p6l
 dc.w 0
 dc.w bpl7pth
p7h
 dc.w 0
 dc.w bpl7ptl
p7l
 dc.w 0
 dc.w bpl8pth
p8h
 dc.w 0
 dc.w bpl8ptl
p8l
 dc.w 0
 
 dc.w $80
och:
 dc.w 0
 dc.w $82
ocl:
 dc.w 0

 dc.w ddfstart,$38
 dc.w ddfstop,$b8
 dc.w diwstart,$2c81
 dc.w diwstop,$2cc1
 
 dc.w bplcon0
Panelcon: dc.w $0211
 dc.w bpl1pth
p1h
 dc.w 0

 dc.w bpl1ptl
p1l
 dc.w 0


 dc.w $108,40*7
 dc.w $10a,40*7

 dc.w $ffff,$fffe

 dc.w $180,$fff


 dc.w $f801,$ff00
 dc.w col1,$50
 dc.w $f901,$ff00
 dc.w col1,$90
 dc.w $fa01,$ff00
 dc.w col1,$f0
 dc.w $fb01,$ff00
 dc.w col1,$f0
 dc.w $fc01,$ff00
 dc.w col1,$90
 dc.w $fd01,$ff00
 dc.w col1,$50

 dc.w $fe01,$ff00
 dc.w col1,$fff
 
 dc.w $ffdf,$fffe
 dc.w $a01,$ff00
 dc.w bplcon0,$201
 
 incbin "faces2cols"
 dc.w bpl1pth
f1h
 dc.w 0

 dc.w bpl1ptl
f1l
 dc.w 0

 dc.w bpl2pth
f2h
 dc.w 0

 dc.w bpl2ptl
f2l
 dc.w 0

 dc.w bpl3pth
f3h
 dc.w 0

 dc.w bpl3ptl
f3l
 dc.w 0

 dc.w bpl4pth
f4h
 dc.w 0
 dc.w bpl4ptl
f4l
 dc.w 0

 dc.w bpl5pth
f5h
 dc.w 0
 dc.w bpl5ptl
f5l
 dc.w 0
 
 dc.w $0c01,$ff00
 dc.w bplcon0,$5201
  
 dc.w $ffff,$fffe

 cnop 0,64
FacePlace:
 ds.l 6*32*5



********************************************
* Stuff you don't have to worry about yet. *
********************************************

closeeverything:

 jsr mt_end

 move.l #$dff000,a6
 move.l old,$dff080     ; Restore old copper list.
 move.l old,d0
 move.w d0,ocl
 swap d0
 move.w d0,och
 move.w #$8020,dmacon(a6)
 move.w #$f,dmacon(a6)
 move.l saveit,$6c.w
 move.l OLDKINT,$68.w
 move.w saveinters,d0
 or.w #$c000,d0
 move.w d0,intena(a6)
 clr.w $dff0a8
 clr.w $dff0b8
 clr.w $dff0c8
 clr.w $dff0d8

; move.w #3,d0
;nonewvbl
; btst #5,intreqrl(a6)
; beq.s nonewvbl
; move.w #$20,intreq(a6)
; dbra d0,nonewvbl

; move.l oldview,a1
; move.l a1,d0
; move.l gfxbase,a6
; jsr -$de(a6)
 
 
; cmp.b #'t',option+1
; bra.s leaveold
 move.w #$f8e,$dff1dc
;leaveold:
 
 rte

intbase: dc.l 0
gfxbase: dc.l 0
oldview: dc.l 0

stuff:

	Lea	gfxname(pc),a1	
	Moveq.l	#0,d0
	Move.l	$4.w,a6	
	Jsr	-$228(a6)
	Move.l 	d0,gfxbase
	Move.l	d0,a6				Use As Base Reg
	Move.l	34(a6),oldview
	move.l 38(a6),old

 jmp endstuff

gfxname dc.b "graphics.library",0
 even
INTUNAME	dc.b	"intuition.library",0

 even


 cnop 0,64

Panel:
 incbin "PanelRaw"

TimerScr: ds.b 40*64

scrntab:
 ds.b 16
val SET 32
 REPT 96
 dc.b val,val,val
val SET val+1
 ENDR
 ds.b 16

 cnop 0,64
scrn:

 dcb.l 8,$33333333
 dc.l 0
 dc.l 0
 
 dcb.l 8,$0f0f0f0f
 dc.l 0
 dc.l 0

 dcb.l 8,$00ff00ff
 dc.l 0
 dc.l 0
 
 dcb.l 8,$0000ffff
 dc.l 0
 dc.l 0
 
 dc.l 0,-1,0,-1,0,-1,0,-1
 dc.l 0
 dc.l 0
 
 dc.l -1,-1,0,0,-1,-1,0,0
 dc.l 0
 dc.l 0
 
 dc.l 0,0,-1,-1,-1,-1,-1,-1
 dc.l 0
 dc.l 0
 
NumTimes: dc.l 0
TimeCount: dc.l 0
oldtime: dc.l 0
counting: dc.b 0
oktodisplay: dc.b 0

INITTIMER:
 move.l #0,TimeCount
 move.l #0,NumTimes
 rts
 
STARTCOUNT:
 move.l d0,-(a7)
 move.l $dff004,d0
 and.l #$1ffff,d0
 move.l d0,oldtime
 st counting
 move.l (a7)+,d0
 rts

STOPCOUNT:
 move.l d0,-(a7)
 move.l $dff004,d0
 and.l #$1ffff,d0
 
 sub.l oldtime,d0
 cmp.l #-256,d0
 bge.s okcount
 add.l #313*256,d0
okcount:
 add.l d0,TimeCount
 addq.l #1,NumTimes
 clr.b counting
 move.l (a7)+,d0
 rts

STOPCOUNTNOADD:
 move.l d0,-(a7)
 move.l $dff004,d0
 and.l #$1ffff,d0
 
 sub.l oldtime,d0
 cmp.l #-256,d0
 bge.s okcount2
 add.l #313*256,d0
okcount2:
 add.l d0,TimeCount
 clr.b counting
 move.l (a7)+,d0
 rts

maxbot: dc.w 0
tstneg: dc.l 0

STOPTIMER:
 st oktodisplay
 rts
 
digits: incbin "numbers.inc"

 
 Section Sounds,CODE

Scream: incbin "ab3:sounds/Scream"
 ds.w 100
EndScream:
LowScream: incbin "ab3:sounds/LowScream"
 ds.w 100
EndLowScream:
BaddieGun: incbin "ab3:sounds/BaddieGun"
EndBaddieGun:
bass: incbin "ab3:sounds/backbass+drum"
bassend:
Shoot: incbin "ab3:sounds/fire!"
EndShoot:
Munch: incbin "ab3:sounds/munch"
EndMunch:
PooGun: incbin "ab3:sounds/shoot.dm"
EndPooGun:
Collect: incbin "ab3:sounds/collect"
EndCollect:
DoorNoise: incbin "ab3:sounds/newdoor"
EndDoorNoise:
Stomp: incbin "ab3:sounds/footstep3"
EndStomp:
SwitchNoise: incbin "ab3:sounds/switch"
EndSwitch:
Reload: incbin "ab3:sounds/switch1.SFX"
EndReload:
NoAmmo: incbin "ab3:sounds/noammo"
EndNoAmmo:
Splotch: incbin "ab3:sounds/splotch"
EndSplotch:
SplatPop: incbin "ab3:sounds/splatpop"
EndSplatPop:
Boom: incbin "ab3:sounds/boom"
EndBoom:
Hiss: incbin "ab3:sounds/newhiss"
EndHiss:
Howl1: incbin "ab3:sounds/howl1"
EndHowl1:
Howl2: incbin "ab3:sounds/howl2"
EndHowl2:
Pant: incbin "ab3:sounds/pant"
EndPant:
Whoosh: incbin "ab3:sounds/whoosh"
EndWhoosh:
 SECTION music,code_c

UseAllChannels: dc.w 0

mt_init:move.l	mt_data,a0
	move.l	a0,a1
	add.l	#$3b8,a1
	moveq	#$7f,d0
	moveq	#0,d1
mt_loop:move.l	d1,d2
	subq.w	#1,d0
mt_lop2:move.b	(a1)+,d1
	cmp.b	d2,d1
	bgt.s	mt_loop
	dbf	d0,mt_lop2
	addq.b	#1,d2

	lea	mt_samplestarts(pc),a1
	asl.l	#8,d2
	asl.l	#2,d2
	add.l	#$43c,d2
	add.l	a0,d2
	move.l	d2,a2
	moveq	#$1e,d0
mt_lop3:clr.l	(a2)
	move.l	a2,(a1)+
	moveq	#0,d1
	move.w	42(a0),d1
	asl.l	#1,d1
	add.l	d1,a2
	add.l	#$1e,a0
	dbf	d0,mt_lop3

	or.b	#$2,$bfe001
	move.b	#$6,mt_speed
	clr.w	$dff0a8
	clr.w	$dff0b8
	clr.w	$dff0c8
	clr.w	$dff0d8
	clr.b	mt_songpos
	clr.b	mt_counter
	clr.w	mt_pattpos
	rts

mt_end:	clr.w	$dff0a8
	clr.w	$dff0b8
	clr.w	$dff0c8
	clr.w	$dff0d8
	move.w	#$f,$dff096
	rts

mt_music:
	movem.l	d0-d4/a0-a3/a5-a6,-(a7)
	move.l	mt_data,a0
	addq.b	#$1,mt_counter
	move.b	mt_counter,D0
	cmp.b	mt_speed,D0
	blt.s	mt_nonew
	clr.b	mt_counter
	bra	mt_getnew

mt_nonew:
	lea	mt_voice1(pc),a6
	lea	$dff0a0,a5
	bsr	mt_checkcom
	lea	mt_voice2(pc),a6
	lea	$dff0b0,a5
	bsr	mt_checkcom
	tst.b UseAllChannels
	beq mt_endr
 	lea	mt_voice3(pc),a6
	lea	$dff0c0,a5
	bsr	mt_checkcom
	lea	mt_voice4(pc),a6
	lea	$dff0d0,a5
	bsr	mt_checkcom
	bra	mt_endr

mt_arpeggio:
	moveq	#0,d0
	move.b	mt_counter,d0
	divs	#$3,d0
	swap	d0
	cmp.w	#$0,d0
	beq.s	mt_arp2
	cmp.w	#$2,d0
	beq.s	mt_arp1

	moveq	#0,d0
	move.b	$3(a6),d0
	lsr.b	#4,d0
	bra.s	mt_arp3
mt_arp1:moveq	#0,d0
	move.b	$3(a6),d0
	and.b	#$f,d0
	bra.s	mt_arp3
mt_arp2:move.w	$10(a6),d2
	bra.s	mt_arp4
mt_arp3:asl.w	#1,d0
	moveq	#0,d1
	move.w	$10(a6),d1
	lea	mt_periods(pc),a0
	moveq	#$24,d7
mt_arploop:
	move.w	(a0,d0.w),d2
	cmp.w	(a0),d1
	bge.s	mt_arp4
	addq.l	#2,a0
	dbf	d7,mt_arploop
	rts
mt_arp4:move.w	d2,$6(a5)
	rts

mt_getnew:
	move.l	mt_data,a0
	move.l	a0,a3
	move.l	a0,a2
	add.l	#$c,a3
	add.l	#$3b8,a2
	add.l	#$43c,a0

	moveq	#0,d0
	move.l	d0,d1
	move.b	mt_songpos,d0
	move.b	(a2,d0.w),d1
	asl.l	#8,d1
	asl.l	#2,d1
	add.w	mt_pattpos,d1
	clr.w	mt_dmacon

	lea	$dff0a0,a5
	lea	mt_voice1(pc),a6
	bsr.s	mt_playvoice
	lea	$dff0b0,a5
	lea	mt_voice2(pc),a6
	bsr.s	mt_playvoice
	tst.b UseAllChannels
	beq mt_setdma
	lea	$dff0c0,a5
	lea	mt_voice3(pc),a6
	bsr.s	mt_playvoice
	lea	$dff0d0,a5
	lea	mt_voice4(pc),a6
	bsr.s	mt_playvoice
	bra	mt_setdma

mt_playvoice:
	move.l	(a0,d1.l),(a6)
	addq.l	#4,d1
	moveq	#0,d2
	move.b	$2(a6),d2
	and.b	#$f0,d2
	lsr.b	#4,d2
	move.b	(a6),d0
	and.b	#$f0,d0
	or.b	d0,d2
	tst.b	d2
	beq.s	mt_setregs
	moveq	#0,d3
	lea	mt_samplestarts(pc),a1
	move.l	d2,d4
	subq.l	#$1,d2
	asl.l	#2,d2
	mulu	#$1e,d4
	move.l	(a1,d2.l),$4(a6)
	move.w	(a3,d4.l),$8(a6)
	move.w	$2(a3,d4.l),$12(a6)
	move.w	$4(a3,d4.l),d3
	tst.w	d3
	beq.s	mt_noloop
	move.l	$4(a6),d2
	asl.w	#1,d3
	add.l	d3,d2
	move.l	d2,$a(a6)
	move.w	$4(a3,d4.l),d0
	add.w	$6(a3,d4.l),d0
	move.w	d0,8(a6)
	move.w	$6(a3,d4.l),$e(a6)
	move.w	$12(a6),d0
	asr.w #2,d0
	move.w d0,$8(a5)
	bra.s	mt_setregs
mt_noloop:
	move.l	$4(a6),d2
	add.l	d3,d2
	move.l	d2,$a(a6)
	move.w	$6(a3,d4.l),$e(a6)
	move.w	$12(a6),d0
	asr.w #2,d0
	move.w d0,$8(a5)
mt_setregs:
	move.w	(a6),d0
	and.w	#$fff,d0
	beq	mt_checkcom2
	move.b	$2(a6),d0
	and.b	#$F,d0
	cmp.b	#$3,d0
	bne.s	mt_setperiod
	bsr	mt_setmyport
	bra	mt_checkcom2
mt_setperiod:
	move.w	(a6),$10(a6)
	and.w	#$fff,$10(a6)
	move.w	$14(a6),d0
	move.w	d0,$dff096
	clr.b	$1b(a6)

	move.l	$4(a6),(a5)
	move.w	$8(a6),$4(a5)
	move.w	$10(a6),d0
	and.w	#$fff,d0
	move.w	d0,$6(a5)
	move.w	$14(a6),d0
	or.w	d0,mt_dmacon
	bra	mt_checkcom2

mt_setdma:
 	move.w #250,d0
mt_wait:
 	add.w #1,testchip
 	dbra d0,mt_wait
	move.w	mt_dmacon,d0
	or.w	#$8000,d0
	and.w #%1111111111110011,d0
	move.w	d0,$dff096
	move.w #250,d0
mt_wait2:
	add.w #1,testchip
	dbra	d0,mt_wait2
	lea	$dff000,a5
	tst.b UseAllChannels
	beq.s noall
	lea	mt_voice4(pc),a6
	move.l	$a(a6),$d0(a5)
	move.w	$e(a6),$d4(a5)
	lea	mt_voice3(pc),a6
	move.l	$a(a6),$c0(a5)
	move.w	$e(a6),$c4(a5)
noall:
	lea	mt_voice2(pc),a6
	move.l	$a(a6),$b0(a5)
	move.w	$e(a6),$b4(a5)
	lea	mt_voice1(pc),a6
	move.l	$a(a6),$a0(a5)
	move.w	$e(a6),$a4(a5)

	add.w	#$10,mt_pattpos
	cmp.w	#$400,mt_pattpos
	bne.s	mt_endr
mt_nex:	clr.w	mt_pattpos
	clr.b	mt_break
	addq.b	#1,mt_songpos
	and.b	#$7f,mt_songpos
	move.b	mt_songpos,d1
;	cmp.b	mt_data+$3b6,d1
;	bne.s	mt_endr
;	move.b	mt_data+$3b7,mt_songpos
mt_endr:tst.b	mt_break
	bne.s	mt_nex
	movem.l	(a7)+,d0-d4/a0-a3/a5-a6
	rts

mt_setmyport:
	move.w	(a6),d2
	and.w	#$fff,d2
	move.w	d2,$18(a6)
	move.w	$10(a6),d0
	clr.b	$16(a6)
	cmp.w	d0,d2
	beq.s	mt_clrport
	bge.s	mt_rt
	move.b	#$1,$16(a6)
	rts
mt_clrport:
	clr.w	$18(a6)
mt_rt:	rts

CODESTORE: dc.l 0

mt_myport:
	move.b	$3(a6),d0
	beq.s	mt_myslide
	move.b	d0,$17(a6)
	clr.b	$3(a6)
mt_myslide:
	tst.w	$18(a6)
	beq.s	mt_rt
	moveq	#0,d0
	move.b	$17(a6),d0
	tst.b	$16(a6)
	bne.s	mt_mysub
	add.w	d0,$10(a6)
	move.w	$18(a6),d0
	cmp.w	$10(a6),d0
	bgt.s	mt_myok
	move.w	$18(a6),$10(a6)
	clr.w	$18(a6)
mt_myok:move.w	$10(a6),$6(a5)
	rts
mt_mysub:
	sub.w	d0,$10(a6)
	move.w	$18(a6),d0
	cmp.w	$10(a6),d0
	blt.s	mt_myok
	move.w	$18(a6),$10(a6)
	clr.w	$18(a6)
	move.w	$10(a6),$6(a5)
	rts

mt_vib:	move.b	$3(a6),d0
	beq.s	mt_vi
	move.b	d0,$1a(a6)

mt_vi:	move.b	$1b(a6),d0
	lea	mt_sin(pc),a4
	lsr.w	#$2,d0
	and.w	#$1f,d0
	moveq	#0,d2
	move.b	(a4,d0.w),d2
	move.b	$1a(a6),d0
	and.w	#$f,d0
	mulu	d0,d2
	lsr.w	#$6,d2
	move.w	$10(a6),d0
	tst.b	$1b(a6)
	bmi.s	mt_vibmin
	add.w	d2,d0
	bra.s	mt_vib2
mt_vibmin:
	sub.w	d2,d0
mt_vib2:move.w	d0,$6(a5)
	move.b	$1a(a6),d0
	lsr.w	#$2,d0
	and.w	#$3c,d0
	add.b	d0,$1b(a6)
	rts

mt_nop:	move.w	$10(a6),$6(a5)
	rts


mt_checkcom:
	move.w	$2(a6),d0
	and.w	#$fff,d0
	beq.s	mt_nop
	move.b	$2(a6),d0
	and.b	#$f,d0
	tst.b	d0
	beq	mt_arpeggio
	cmp.b	#$1,d0
	beq.s	mt_portup
	cmp.b	#$2,d0
	beq	mt_portdown
	cmp.b	#$3,d0
	beq	mt_myport
	cmp.b	#$4,d0
	beq	mt_vib
	move.w	$10(a6),$6(a5)
	cmp.b	#$a,d0
	beq.s	mt_volslide
	rts

mt_volslide:
	moveq	#0,d0
	move.b	$3(a6),d0
	lsr.b	#4,d0
	tst.b	d0
	beq.s	mt_voldown
	add.w	d0,$12(a6)
	cmp.w	#$40,$12(a6)
	bmi.s	mt_vol2
	move.w	#$40,$12(a6)
mt_vol2:move.w	$12(a6),d0
	asr.w #2,d0
	move.w d0,$8(a5)
	rts

mt_voldown:
	moveq	#0,d0
	move.b	$3(a6),d0
	and.b	#$f,d0
	sub.w	d0,$12(a6)
	bpl.s	mt_vol3
	clr.w	$12(a6)
mt_vol3:move.w	$12(a6),d0
	asr.w #2,d0
	move.w d0,$8(a5)
	rts

mt_portup:
	moveq	#0,d0
	move.b	$3(a6),d0
	sub.w	d0,$10(a6)
	move.w	$10(a6),d0
	and.w	#$fff,d0
	cmp.w	#$71,d0
	bpl.s	mt_por2
	and.w	#$f000,$10(a6)
	or.w	#$71,$10(a6)
mt_por2:move.w	$10(a6),d0
	and.w	#$fff,d0
	move.w	d0,$6(a5)
	rts

mt_portdown:
	clr.w	d0
	move.b	$3(a6),d0
	add.w	d0,$10(a6)
	move.w	$10(a6),d0
	and.w	#$fff,d0
	cmp.w	#$358,d0
	bmi.s	mt_por3
	and.w	#$f000,$10(a6)
	or.w	#$358,$10(a6)
mt_por3:move.w	$10(a6),d0
	and.w	#$fff,d0
	move.w	d0,$6(a5)
	rts

mt_checkcom2:
	move.b	$2(a6),d0
	and.b	#$f,d0
	cmp.b	#$e,d0
	beq.s	mt_setfilt
	cmp.b	#$d,d0
	beq.s	mt_pattbreak
	cmp.b	#$b,d0
	beq.s	mt_posjmp
	cmp.b	#$c,d0
	beq.s	mt_setvol
	cmp.b	#$f,d0
	beq.s	mt_setspeed
	rts

mt_setfilt:
	move.b	$3(a6),d0
	and.b	#$1,d0
	asl.b	#$1,d0
	and.b	#$fd,$bfe001
	or.b	d0,$bfe001
	rts
mt_pattbreak:
	not.b	mt_break
	rts
mt_posjmp:
	st reachedend
	move.b	$3(a6),d0
	subq.b	#$1,d0
	move.b	d0,mt_songpos
	not.b	mt_break
	rts
mt_setvol:
	cmp.b	#$40,$3(a6)
	ble.s	mt_vol4
	move.b	#$40,$3(a6)
mt_vol4:move.b	$3(a6),d0
	asr.w #2,d0
	move.w d0,$8(a5)
	rts
mt_setspeed:
	cmp.b	#$1f,$3(a6)
	ble.s	mt_sets
	move.b	#$1f,$3(a6)
mt_sets:move.b	$3(a6),d0
	beq.s	mt_rts2
	move.b	d0,mt_speed
	clr.b	mt_counter
mt_rts2:rts

mt_sin:
 DC.b $00,$18,$31,$4a,$61,$78,$8d,$a1,$b4,$c5,$d4,$e0,$eb,$f4,$fa,$fd
 DC.b $ff,$fd,$fa,$f4,$eb,$e0,$d4,$c5,$b4,$a1,$8d,$78,$61,$4a,$31,$18

mt_periods:
 DC.w $0358,$0328,$02fa,$02d0,$02a6,$0280,$025c,$023a,$021a,$01fc,$01e0
 DC.w $01c5,$01ac,$0194,$017d,$0168,$0153,$0140,$012e,$011d,$010d,$00fe
 DC.w $00f0,$00e2,$00d6,$00ca,$00be,$00b4,$00aa,$00a0,$0097,$008f,$0087
 DC.w $007f,$0078,$0071,$0000,$0000

reachedend: dc.b 0
mt_speed:	DC.b	6
mt_songpos:	DC.b	0
mt_pattpos:	DC.w	0
mt_counter:	DC.b	0

mt_break:	DC.b	0
mt_dmacon:	DC.w	0
mt_samplestarts:DS.L	$1f
mt_voice1:	DS.w	10
		DC.w	1
		DS.w	3
mt_voice2:	DS.w	10
		DC.w	2
		DS.w	3
mt_voice3:	DS.w	10
		DC.w	4
		DS.w	3
mt_voice4:	DS.w	10
		DC.w	8
		DS.w	3

testchip: dc.w 0

;/* End of File */
mt_data: dc.l 0
tstchip: dc.l 0

ingame:
; incbin "ab3:includes/ingame"
gameover: incbin "ab3:includes/gameover"
welldone: incbin "ab3:includes/welldone"
