; ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ»
; Ί 3D Coordinate Routines.						   Ί
; ΜΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΉ
; Ί Contains everything to translate x,y,z coordinates and		   Ί
; Ί convert them to screen x,y coordinates.				   Ί
; ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ


; - Equates et al ----------------------------------------------------------

MaxZc			equ	1024
MinZc			equ	128
MaxAngle		equ	512

		
; - TABLES -----------------------------------------------------------------
			
Sin			incbin	sintab.dat
			even
Cos			incbin	costab.dat
			even


SinTable		HEX 0000FF9BFF36FED1FE6DFE09FDA5FD42
			HEX FCDFFC7CFC1AFBB9FB58FAF8FA99FA3A
			HEX F9DDF980F924F8CAF871F818F7C1F76C
			HEX F717F6C4F673F623F5D4F587F53CF4F2
			HEX F4AAF464F41FF3DDF39CF35DF320F2E5
			HEX F2ADF276F241F20FF1DEF1B0F184F15B
			HEX F133F10EF0EBF0CBF0ACF091F077F060
			HEX F04CF03AF02AF01DF012F00AF004F001
			HEX F001F001F005F00CF015F020F02EF03F
			HEX F051F067F07EF099F0B5F0D4F0F5F119
			HEX F13EF167F191F1BEF1ECF21DF250F286
			HEX F2BDF2F7F332F36FF3AFF3F0F433F478
			HEX F4BFF507F552F59DF5EBF63AF68AF6DC
			HEX F730F785F7DBF832F88BF8E4F93FF99B
			HEX F9F8FA56FAB4FB14FB74FBD5FC37FC99
			HEX FCFCFD5FFDC2FE26FE8BFEEFFF54FFB8
			HEX 001D008200E6014B01AF0213027702DA
			HEX 033D03A00402046304C30523058205E0
			HEX 063E069A06F5074F07A80800085708AC
			HEX 0900095309A409F40A420A8E0AD90B22
			HEX 0B6A0BB00BF30C350C760CB40CF00D2A
			HEX 0D630D990DCD0DFF0E2E0E5C0E870EB0
			HEX 0ED70EFC0F1E0F3E0F5B0F760F8F0FA5
			HEX 0FB90FCA0FD90FE60FF00FF70FFC0FFF
			HEX 0FFF0FFD0FF80FF10FE70FDB0FCC0FBB
			HEX 0FA80F920F790F5E0F410F220F000EDC
			HEX 0EB50E8C0E610E340E050DD30D9F0D69
			HEX 0D310CF70CBB0C7D0C3D0BFB0BB80B72
			HEX 0B2B0AE20A970A4B09FD09AE095D090A
			HEX 08B60861080B07B3075A070006A50649
			HEX 05EC058E052F04CF046F040D03AC0349
			HEX 02E60283021F01BB015700F3008E0029
			even


; - X,Y,Z Matrix data ------------------------------------------------------

MaxGrids		equ	5

Grid2			dc.w	-$20,-$20,0, -$10,-$20,0, 0,-$20,0, $10,-$20,0, $20,-$20,0
			dc.w	-$20,-$10,0, -$10,-$10,0, 0,-$10,0, $10,-$10,0, $20,-$10,0
			dc.w	-$20,0,0, -$10,0,0, 0,0,0, $10,0,0, $20,0,0
			dc.w	-$20,$10,0, -$10,$10,0, 0,$10,0, $10,$10,0, $20,$10,0
			dc.w	-$20,$20,0, -$10,$20,0, 0,$20,0, $10,$20,0, $20,$20,0

Grid			dc.w	-$20,-$20,0, -$10,-$20,0, 0,-$20,0, $10,-$20,0, $20,-$20,0
			dc.w	-$20,-$10,0, -$10,-$10,-$10, 0,-$10,-$10, $10,-$10,-$10, $20,-$10,0
			dc.w	-$20,0,0, -$10,0,-$10, 0,0,-$20, $10,0,-$10, $20,0,0
			dc.w	-$20,$10,0, -$10,$10,-$10, 0,$10,-$10, $10,$10,-$10, $20,$10,0
			dc.w	-$20,$20,0, -$10,$20,0, 0,$20,0, $10,$20,0, $20,$20,0

Grid3			dc.w	-$10,-$18,$10, 0,-$28,$10,  $10,-$18,$10
			dc.w	-$30,0,$10, $30,0,$10
			dc.w	-$10,$18,$10, 0,$28,$10,  $10,$18,$10
			dc.w	-$10,-$20,0, 0,-$30,0,  $10,-$20,0
			dc.w	-$30,0,0, 0,0,0, $30,0,0
			dc.w	-$10,$20,0, 0,$30,0,  $10,$20,0
			dc.w	-$10,-$18,-$10, 0,-$28,-$10,  $10,-$18,-$10
			dc.w	-$30,0,-$10, $30,0,-$10
			dc.w	-$10,$18,-$10, 0,$28,-$10,  $10,$18,-$10

Grid4			dc.w	-$28,$20,$18, -$30,$28,$20, 0,$20,-$30, $30,-$10,-$10, $40,-$20,$38
			dc.w	$10,-$18,-$20, -$40,-$28,0, $30,-$10,$28, $10,-$28,$18, $20,-$20,-$20
			dc.w	-$20,0,$10, -$10,$10,$10, 0,0,0, $10,0,$18, $20,0,$20
			dc.w	$10,$10,-$18, -$18,-$18,$38, $28,$10,$30, -$10,$48,-$18, $10,$40,-$40
			dc.w	-$40,$10,$40, -$10,$20,-$40, $10,$30,-$28, $10,$30,$28, -$30,$20,-$28	

Grid5			dc.w	-$10,-$20,-8, 0,-$28,-8, $10,-$20,-8, -$20,-$10,-8, $20,-$10,-8
			dc.w	-$28,0,-8, $28,0,-8, -$20,$10,-8, $20,$10,-8, -$10,$20,-8
			dc.w	0,$28,-8, $10,$20,-8, 0,0,0
			dc.w	-$10,-$20,8, 0,-$28,8, $10,-$20,8, -$20,-$10,8, $20,-$10,8
			dc.w	-$28,0,8, $28,0,8, -$20,$10,8, $20,$10,8, -$10,$20,8
			dc.w	0,$28,8, $10,$20,8


Grids			dc.l	Grid,Grid2,Grid3,Grid4,Grid5


; - Charset, anim and palette data -----------------------------------------

OrbChr			incbin	r&d\orb.chr
OrbInc			include	r&d\orb.inc
OrbANM			anmdef	ANM_Orb,OrbChr,0,0,0,0
ANM_Orb			;anm	ORB_0001,4,0
			anm1	ORB_0002,0,0
			anm	AEnd


; - Setup the Object matrix and gfx data etc. ------------------------------

hitxt1			dc.w	cp3
			dc.b	pfA,7,14,"CHRIS SHRIGLEY SAYS HI TO :",0
hitxt2			dc.w	cp3
			dc.b	pfA,2,16,"JAYNE, ZACH, JOEL, CARY, TOM, JOE,",0
hitxt3			dc.w	cp3
			dc.b	pfA,2,17,"ANDY GREEN, ROB TOONE, MAT SNEAP,",0
hitxt4			dc.w	cp3
			dc.b	pfA,2,18,"KATY, EUROCOM PEEPLE, NEVERSOFT,",0
hitxt5			dc.w	cp3
			dc.b	pfA,2,19,"CLOCKWORK TORTOISE PEEPLE, STU GREGG,",0
hitxt6			dc.w	cp3
			dc.b	pfA,2,20,"PARADOX PEEPLE, MA & PA, TARYN & STU,",0
hitxt7			dc.w	cp3
			dc.b	pfA,2,21,"MARK LORENZEN, PETE FOKOS, CHAZZ,",0
hitxt8			dc.w	cp3		       
			dc.b	pfA,2,22,"KEN LOBB, LEFT FIELD PEEPLE, RUSSEL &",0
hitxt9			dc.w	cp3
			dc.b	pfA,2,23,"DEBS, STEVE AND PHIL AND DINAH TOO,",0
hitxt10			dc.w	cp3
			dc.b	pfA,2,24,"GREAT BRITAIN AND THE COLONIES!",0

RotateSETUP		jsr	dark_fader
			jsr	wait_fade

			move.w	#$2700,sr
			jsr	SetupShell
			clr.w	map_x_p
			clr.w	map_y_p
			move.w	#$b42f,rndnum
			move.l	#"cHiG",rndseed
			
			lea	DisneyLogoCHR,a0	;compressed chr data
			jsr	BlitCharset
			lea	DisneyLogoMAP,a0	;compressed chr data
			lea	MapA_Buff,a1		;destination buffer
			jsr	DecompressSWD		;decompress data
			move.l	#ScrlB_Base,d6
			moveq	#0,d1			;print the logo to screen
			moveq	#0,d2
			jsr	GetScreenAddrs
			lea	MapA_Buff,a0
			moveq	#39,d0
			moveq	#25,d1
			moveq	#0,d4
			jsr	windowblock

			move.w	#(320)-$20,CenterX
			move.w	#(200/2)-$40,CenterY
			move.l	#MinZc+$14,Zc
			move.b	#1,Xc
			clr.w	Xangle
			clr.w	Yangle
			clr.w	Zangle
			jsr	Randy
			and.w	#7,d7
			move.w	d7,Xdelta
			jsr	Randy
			and.w	#7,d7
			move.w	d7,Ydelta
			jsr	Randy
			and.w	#7,d7
			move.w	d7,Zdelta
			bsr	CalcAngles

			moveq	#(5*5)-1,d6				;setup 25 objects
.looop			move.l	d6,-(sp)
			SPAWN_HI	16*32
			move.w	#Orb_OB,OB_Type(a0)
			move.b	#_ob_nooff+_ob_hiprior,OB_Flag2(a0)
			bset.b	#ob_noclip,OB_Flag1(a0)
			bset.b	#ob_newpal,OB_Flag1(a0)
			move.w	#cp4,OB_Pal(a0)
			lea	OrbANM,a1
			jsr	SetNewAnim
			jsr	ForceNewAnim
			move.l	(sp)+,d6
			dbra	d6,.looop

			move.w	#1,GridDex
			bsr	SetGrid
			bsr	SortSETUP
			bsr	SortObjectsZ
		
			move.w	#A3dVBLANK,VBLANKPhase	;vblank phase
			
			move.w	#4,PixX_Copy+2		;position it

			lea	hitxt1,a1
			jsr	PrintString
			lea	hitxt2,a1
			jsr	PrintString
			lea	hitxt3,a1
			jsr	PrintString
			lea	hitxt4,a1
			jsr	PrintString
			lea	hitxt5,a1
			jsr	PrintString
			lea	hitxt6,a1
			jsr	PrintString
			lea	hitxt7,a1
			jsr	PrintString
			lea	hitxt8,a1
			jsr	PrintString
			lea	hitxt9,a1
			jsr	PrintString
			lea	hitxt10,a1
			jsr	PrintString

			MUSIC	73
						
			clr.b	working
			st.b	service
			clr.b	joy1
			clr.b	d_joy1
			move	#$2000,sr		;enable irq
			
			lea	DisneyLogoPAL,a1		;fade
			jsr	lite_fader
			jsr	wait_fade

			move.w	#$eee,Pal3+28
			move.w	#$444,Pal3+30

	
; - Do some control stuff from the Joypad ----------------------------------

RotateTEST		jsr	WaitFrm

			cmp.b	#$f0,joy1
			bne.s	.1a
			jmp	ColdResetTheGame
			
.1a			btst.b	#start,d_joy1
			beq.s	.a1s
			eor.b	#1,Xc
			jsr	Randy
			and.w	#7,d7
			move.w	d7,Xdelta
			jsr	Randy
			and.w	#7,d7
			move.w	d7,Ydelta
			jsr	Randy
			and.w	#7,d7
			move.w	d7,Zdelta

.a1s			move.w	Xangle,d1
			move.w	Yangle,d2
			move.w	Zangle,d3

			tst.b	Xc
			bne	.bb

			btst.b	#up,joy1
			beq.s	.w1
			bsr	.up
			bra	.w2
.w1			btst.b	#down,joy1
			beq	.w2
			bsr	.down
.w2			btst.b	#left,joy1
			bne	.left
			btst.b	#right,joy1
			bne	.right
			
			btst.b	#but_a,d_joy1
			beq.s	.rrr
			bsr	SetGrid
.rrr			bra	RotateTEST

.up			cmp.l	#MaxZc,Zc
			bge.s	.u1
			add.l	#4,Zc
.u1			rts
.down			cmp.l	#MinZc,Zc
			ble.s	.d1
			sub.l	#4,Zc
.d1			rts

.left			btst.b	#but_a,joy1
			beq.s	.l1
			bsr	.leftX
.l1			btst.b	#but_b,joy1
			beq.s	.l2
			bsr	.leftY
.l2			btst.b	#but_c,joy1
			beq	.a
.leftZ			sub.w	Zdelta,d3
			bra	.a
.leftX			sub.w	Xdelta,d1
			rts
.leftY			sub.w	Ydelta,d2
			rts

.right			btst.b	#but_a,joy1
			beq.s	.r1
			bsr	.rightX
.r1			btst.b	#but_b,joy1
			beq.s	.r2
			bsr	.rightY
.r2			btst.b	#but_c,joy1
			beq	.a
.rightZ			add.w	Zdelta,d3
			bra	.a
.rightX			add.w	Xdelta,d1
			rts
.rightY			add.w	Ydelta,d2
			rts

.bb			btst.b	#but_a,d_joy1
			beq.s	.rr
			bsr	SetGrid
.rr			add.w	Xdelta,d1
			add.w	Ydelta,d2
			add.w	Zdelta,d3
.a			and.w	#$1ff,d1
			and.w	#$1ff,d2
			and.w	#$1ff,d3
			move.w	d1,Xangle
			move.w	d2,Yangle
			move.w	d3,Zangle

.aa			bsr	CalcAngles
			bra	RotateTEST


; - Read the data into the X,Y,Z of Objects --------------------------------

SetGrid			addq.w	#1,GridDex
			cmp.w	#MaxGrids,GridDex
			bne.s	.1
			clr.w	GridDex
.1			move.w	GridDex,d1
			asl.w	#2,d1
			lea	Grids,a1
			move.l	0(a1,d1.w),a1
			lea	OBJECT_RAM,a0
			moveq	#(5*5)-1,d6				;setup 49 objects
.looop			moveq	#0,d4
			move.w	d6,d4
			mulu	#6,d4
			move.w	0(a1,d4.w),OB_WXpos(a0)			;put data
			move.w	2(a1,d4.w),OB_WYpos(a0)
			move.w	4(a1,d4.w),OB_WZpos(a0)
			move.w	0(a1,d4.w),OB_Var1(a0)			;dup originals
			move.w	2(a1,d4.w),OB_Var2(a0)
			move.w	4(a1,d4.w),OB_Var3(a0)
			lea	128(a0),a0
			dbra	d6,.looop
			rts


; - Orb object control logic -----------------------------------------------
			
Orb_Logic		move.w	OB_Var1(a0),OB_WXpos(a0)	;restore original x,y,z
			move.w	OB_Var2(a0),OB_WYpos(a0)	;so that we can screw around with them!
			move.w	OB_Var3(a0),OB_WZpos(a0)
			move.w	CosZ,d1				;tie the Z into
			bpl.s	.1				;the cosine of Zangle
			neg.w	d1				;this gives a neat swooping effect
			lsr.w	#2,d1
			sub.w	d1,OB_WZpos(a0)
			bra	.2
.1			lsr.w	#2,d1
			add.w	d1,OB_WZpos(a0)
.2			move.w	OB_WXpos(a0),d1			;world x	
			move.w	OB_WYpos(a0),d2			;world y
			move.w	OB_WZpos(a0),d3			;world z
			bsr	RotateXYZ			;do da bizz
			move.w	d1,OB_Xpos(a0)			;screen x
			move.w	d2,OB_Ypos(a0)			;screen y
			move.w	d3,OB_Zpos(a0)			;screen z (used for sort etc.)
			rts
			cmp.w	#$60,d3				;make orbs go infront of logo
			ble.s	.a		       		;at certain z
			move.b	#_ob_nooff,OB_Flag2(a0)
			rts
.a			move.b	#_ob_hiprior+_ob_nooff,OB_Flag2(a0)
			rts



; - CODE -------------------------------------------------------------------

; ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ»
; Ί ROTATEXYZ - Rotate a point @ x,y,z around world origin 0,0,0	   Ί
; ΜΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΉ
; Ί Entry: d1=World Xpos : d2=World Ypos : d3=World Zpos		   Ί
; Ί  Exit: d1=Rotated Screen Xpos : d2=Rotated Screen Ypos		   Ί
; Ί	   d3=Rotated Screen Zpos		  			   Ί
; Ί  Used: d1-d7 (Not Saved)						   Ί
; ΜΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΉ
; Ί Rotates a point at x,y,z around point 0,0,0 by Xangle, Yangle, Zangle  Ί
; ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ

RotateXYZ		move.w	CosX,d4				;Cos(Xangle)
			move.w	SinX,d5				;Sin(Xangle)
			move.w	d4,d6
			move.w	d5,d7
			muls	d2,d4				;Ypos*Cos(Xangle)
			muls	d3,d5				;Zpos*Sin(Xangle)
			sub.l	d5,d4				;Ypos*Cos(Xangle)-Zpos*Sin(Xangle)
			muls	d2,d7				;Ypos*Sin(Xangle)
			muls	d3,d6				;Zpos*Cos(Xangle)
			add.l	d7,d6				;Ypos*Sin(Xangle)+Zpos*Cos(Xangle)
			lsr.l	#8,d4
			lsr.l 	#8,d6
			move.l	d4,d2				;y
			move.l	d6,d3				;z

			move.w	CosY,d4				;Cos(Yangle)
			move.w	SinY,d5				;Sin(Yangle)
			move.w	d4,d6				
			move.w	d5,d7
			muls	d1,d4				;Xpos*Cos(Yangle)
			muls	d3,d5				;Zpos*Sin(Yangle)
			sub.l	d5,d4				;Xpos*Cos(Yangle)-Zpos*Sin(Yangle)
			muls	d1,d7				;Xpos*Sin(Yangle)
			muls	d3,d6				;Zpos*Cos(Yangle)
			add.l	d7,d6				;Xpos*Sin(Yangle)+Zpos*Cos(Yangle)
			lsr.l	#8,d4
			lsr.l	#8,d6
			move.l	d4,d1				;x
			move.l	d6,d3				;z
			
			move.w	CosZ,d4				;Cos(Zangle)
			move.w	SinZ,d5				;Sin(Zangle)
			move.w	d4,d6				
			move.w	d5,d7
			muls	d1,d4				;Xpos*Cos(Zangle)
			muls	d2,d5				;Ypos*Sin(Zangle)
			sub.l	d5,d4				;Xpos*Cos(Zangle)-Ypos*Sin(Zangle)
			muls	d1,d7				;Xpos*Sin(Zangle)
			muls	d2,d6				;Ypos*Cos(Zangle)
			add.l	d7,d6				;Xpos*Sin(Zangle)+Ypos*Cos(Zangle)
			move.l	d4,d1				;x
			move.l	d6,d2				;y

			;drop thru to convert

; ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ»
; Ί CONVERTXY - Converts a rotated x,y,z to a Screen x,y		   Ί
; ΜΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΉ
; Ί Entry: d1=Xpos : d2=Ypos : d3=Zpos					   Ί
; Ί  Exit: d1=Screen X : d2=Screen Y					   Ί
; Ί  Used: d1-d5							   Ί
; ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ

ConvertXY		add.l	Zc,d3			;dz=dz+zc
			move.l	d3,d7			;gives slight fish eye lens effect
			divs	d3,d1			;dx=dx/dz
			add.w	CenterX,d1		;dx=dx+xc
			divs	d7,d2			;dy=dy/dz
			add.w	CenterY,d2		;dy=dy+yc
			rts

;			
; ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ»
; Ί CalcAngles								   Ί
; ΜΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΉ
; Ί Entry: Xangle, Yangle, Zangle					   Ί
; Ί  Exit: Cos/Sin of each angle. Perspective and pre adds.		   Ί
; Ί  Used: d1-d5/a0-a1							   Ί
; ΜΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΉ
; Ί Pre calculates and stores as much data as possible.			   Ί
; Ί This reduces the burden on the Rotation routine.			   Ί
; ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ

CalcAngles		lea	Cos,a0
			lea	Sin,a1
			move.w	Xangle,d3
			asl.w	#1,d3
			move.w	0(a0,d3.w),CosX			;d4=Cos(Xangle)
			move.w	0(a1,d3.w),SinX			;d5=Sin(Xangle)
			move.w	Yangle,d3
			asl.w	#1,d3
			move.w	0(a0,d3.w),CosY			;d4=Cos(Yangle)
			move.w	0(a1,d3.w),SinY			;d5=Sin(Yangle)
			move.w	Zangle,d3
			asl.w	#1,d3
			move.w	0(a0,d3.w),CosZ			;d4=Cos(Zangle)
			move.w	0(a1,d3.w),SinZ			;d5=Sin(Zangle)
			rts


; ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ»
; Ί SORT								   Ί
; ΜΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΉ
; Ί Entry: Sort_Z = The Zpos' of each active Object		  	   Ί
; Ί  	   Sort_D = The Object Index of each of the active Objects	   Ί
; Ί  Exit: Sorted Sort_D table (used in HandleObjectsSORTED)		   Ί
; Ί  Used: Lots of Registers						   Ί
; ΜΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΉ
; Ί This is a fast (ish) bubble sort. The sorted data is left intact	   Ί
; Ί and then the new Zpos' are re-read into the Sort_Z table. The up-	   Ί
; Ί dated table is then sorted again. And so it continues ...		   Ί
; Ί 									   Ί
; Ί Worst case for the sort would be if the whole matrix (Z) were flipped  Ί
; Ί in one frame (this would bring the sort to a grinding standstill for   Ί
; Ί a few seconds (ahem)).						   Ί
; ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ

SortSETUP		lea	Sort_Z,a0
			lea	Sort_D,a1
			moveq	#MaxObs-1,d0
.la1			move.w	#-1,(a1)+
			clr.w	(a0)+
			dbra	d0,.la1
			
			lea	OBJECT_RAM,a0
			lea	Sort_D,a1
			lea	Sort_Z,a2
			moveq	#0,d0			
			moveq	#0,d2
			moveq	#(MaxObs-1)*2,d1		
.1			btst.b	#ob_active,OB_Flag1(a0)	
			beq.s	.n
			move.w	d2,(a1)+
			move.w	OB_Zpos(a0),(a2)+
			addq.w	#2,d0			
.n			lea	128(a0),a0
			addq.w	#2,d2
			subq.w	#2,d1
			bpl.s	.1

			move.w	d0,SortElem1
			lsr.w	#1,d0
			subq.w	#1,d0
			move.w	d0,SortElem2
			rts


SortObjectsZ		lea	Sort_Z,a1		;update Zpos'
			lea	Sort_D,a2
			move.w	SortElem2,d1
.l3			lea	OBJECT_RAM,a0
			move.w	(a2)+,d2
			asl.w	#6,d2
			add.w	d2,a0
			move.w	OB_Zpos(a0),(a1)+
			dbra	d1,.l3
			
			move.w	SortElem1,d0
			move.w	#-2,d1
			
			subq.w	#2,d0			
			blt.s	.fin
			move.w	d0,s_tmp1		;no. of elements in list
.l1			addq.w	#2,d1			
 			cmp.w	s_tmp1,d1		;finished?
			blt.s	.xw
.fin			rts
 
.xw			lea	Sort_Z,a2
			add.w	d1,a2
			move.w	2(a2),d3		;need to swap?
			cmp.w	0(a2),d3		
			bge.s	.l1
			move.w	0(a2),2(a2)		;swap z
			move.w	d3,0(a2)
			
			lea	Sort_D,a1
			add.w	d1,a1
			move.w	0(a1),d2		;swap pointer
			move.w	2(a1),0(a1)
			move.w	d2,2(a1)
		
			move.w	d1,s_tmp2		;save index
	 
.l2			tst.w	d1			;test if not last entry
 			beq.s	.nxt	
	 
			lea	Sort_Z,a2
			add.w	d1,a2
			move.w	0(a2),d2		;need to swap?
 			cmp.w	-2(a2),d2		
			bge.s	.nxt
.b			move.w	-2(a2),0(a2)		;swap z
			move.w	d2,-2(a2)
		
 			lea	Sort_D,a1
			add.w	d1,a1
			move.w	0(a1),d2		;swap pointer
			move.w	-2(a1),0(a1)
			move.w	d2,-2(a1)
		
			subq.w	#2,d1
 			bra	.l2	

.nxt			move.w	s_tmp2,d1		;restore index 
 			bra	.l1			;loop
		 



BitmapSETUP		rts

BitmapTEST		rts

;; - TABLES -----------------------------------------------------------------
;
;SINTab1			incbin	sintab1.bin
;COSTab1			incbin	costab1.bin
;SINTab2			incbin	sintab2.bin
;COSTab2			incbin	costab2.bin
;			even
;
;sr_X			dc.w	0
;sr_Y			dc.w	0
;sr_SCALE		dc.w	0
;sr_ROT			dc.w	0
;			even
;
;			rsreset
;_sr_X			rs.w	1		
;_sr_Y			rs.w	1
;_sr_Z			rs.w	1
;_sr_R			rs.w	1
;
;testdata		dc.w	0,0,100,45
;
;SR_test			lea	testdata,a6
;
;ScaleRotate		lea	SINTab1,a0
;			lea	COSTab1,a1
;			lea	SINTab2,a2
;			lea	COSTab2,a3
;			
;			move.w	_sr_R(a6),d0			;rotation
;			move.w	_sr_Z(a6),d1			;scale factor
;
;			moveq	#0,d4				;calc scanning deltas
;			moveq	#0,d5
;			moveq	#0,d6
;			moveq	#0,d7
;			move.b	0(a1,d0.w),d4			;get costable1(rotation)
;			mulu	d1,d4				;*scale factor	
;			lsr.w	#8,d4				;/256
;			move.w	d4,DeltaX1			;delta x
;			move.b	0(a0,d0.w),d5			;get sintable1(rotation)
;			mulu	d1,d5				;*scale factor	
;			lsr.w	#8,d5				;/256
;			move.w	d5,DeltaY1			;delta y
;			move.b	0(a3,d0.w),d6			;get costable2(rotation)
;			mulu	d1,d6				;*scale factor	
;			lsr.w	#8,d6				;/256
;			move.w	d6,DeltaX2 			;delta x
;			move.b	0(a2,d0.w),d7			;get sintable2(rotation)
;			mulu	d1,d7				;*scale factor	
;			lsr.w	#8,d7				;/256
;			move.w	d7,DeltaY2			;delta y
;
;			;center on image to rotate
;			move.w	ImageWide,d0			;pre divided by 2
;			mulu	d4,d0				;*d1x
;			move.w	ImageDeep,d1			;pre divided by 2
;			mulu	d6,d1				;*d2x
;			sub.w	d0,d2				;x-
;			sub.w	d1,d2				;x-
;			move.w	d2,CenterX
;			move.w	ImageWide,d0			;pre divided by 2
;			mulu	d5,d0				;*d1y
;			move.w	ImageDeep,d1			;pre divided by 2
;			mulu	d7,d1				;*d2y
;			sub.w	d0,d3				;y-
;			sub.w	d1,d3				;y-
;			move.w	d3,CenterY
;
;			move.l	SourceImage,a0
;			move.l	DestImage,a1
;			clr.w	DestIndx
;
;			move.w	ImageDeep,d0			
;			
;			move.w	CenterX,d2			;get center as start point
;			move.w	CenterY,d3
;
;			move.w	ImageWide,d1
;			move.w	d1,d4
;
;.HLoop			add.w	DeltaX1,d2			;add deltas
;			add.w	DeltaY1,d3
;			mulu	d4,d3	  			
;			add.w	d2,d3				;d3=pixel pos in source data
;			bsr	GetPixel			;pull a pixel from source
;			bsr	PutPixel			;put the pixel into the destination
;			addq.w	#1,DestIndx			;next destination
;			dbra	d1,HLoop			;loop
		
;; ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ»
;; Ί BYTE per PIXEL							   Ί
;; ΜΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΉ
;; Ί Entry: a6 = data							   Ί
;; ΜΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΉ
;; Ί Converts SEGA %aaaabbbb formated graphic data to %aaaaaaaa bbbbbbbb	   Ί
;; Ί format data. Each nibble of graphic data becomes a full byte.	   Ί
;; Ί 									   Ί
;; Ί Data Structure (a6) ;						   Ί
;; Ί 	Source Data 		(.l)					   Ί
;; Ί 	Destination Data 	(.l)					   Ί
;; Ί 	Source Width (pixels)	(.w)					   Ί
;; Ί 	Source Height (pixels)	(.w)					   Ί
;; ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ
;
;			rsreset
;SourceDATA		rs.l	1
;DestinationDATA		rs.l	1
;SourceWIDE		rs.w	1
;SourceHIGH		rs.w	1
;			
;srctest			hex	00 11 22 00
;			hex	33 00 00 44
;			hex	55 66 77 88
;			hex	99 aa bb cc
;			hex 	dd 00 00 ee
;			hex	ff 00 00 01
;			hex	23 45 67 89 
;			hex	ab cd ef 00
;
;desttest		equ	SCRATCH_RAM+$100
;
;bpptesttab		dc.l	srctest,desttest
;			dc.w	8,8
;	
;bpptest			lea	bpptesttab,a6
;
;BYTEperPIXEL		lea	BPPTab,a3
;			move.l	SourceDATA(a6),a1
;			move.l	DestinationDATA(a6),a2
;			move.w	SourceWIDE(a6),d1
;			mulu	SourceHIGH(a6),d1
;			lsr.w	#1,d1
;			subq.w	#1,d1
;.loop			moveq	#0,d2
;			move.b	(a1)+,d2
;			add.w	d2,d2
;			move.w	0(a3,d2.w),(a2)+
;			dbra	d1,.loop
;.xit			rts
;
;
;; ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ»
;; Ί NIBBLE per PIXEL							   Ί
;; ΜΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΉ
;; Ί Entry: a6 = data							   Ί
;; ΜΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΉ
;; Ί Converts expanded %aaaaaaaa bbbbbbbb formated graphic to		   Ί
;; Ί SEGA %aaaabbbb graphic data						   Ί
;; Ί 									   Ί
;; Ί Data Structure (a6) ;						   Ί
;; Ί 	Source Data 		(.l)					   Ί
;; Ί 	Destination Data 	(.l)					   Ί
;; Ί 	Source Width (pixels)	(.w)					   Ί
;; Ί 	Source Height (pixels)	(.w)					   Ί
;; ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ
;
;npptesttab		dc.l	desttest,desttest	
;			dc.w	8,8
;
;npptest			lea	npptesttab,a6
;
;NIBBLEperPIXEL		move.l	SourceDATA(a6),a1
;			move.l	DestinationDATA(a6),a2
;			move.w	SourceWIDE(a6),d1
;			mulu	SourceHIGH(a6),d1
;			lsr.w	#1,d1
;			subq.w	#1,d1
;.loop			moveq	#0,d2
;			move.w	(a1)+,d2
;			and.w	#$0ff0,d2
;			lsr.w	#4,d2
;			move.b	d2,(a2)+
;			dbra	d1,.loop
;.xit			rts
;
;
;
;BPPcc			=	0
;BPPTab			rept	256
;			dc.b	(BPPcc&$F0)!((BPPcc&$F0)>>4)
;			dc.b	(BPPcc&$0F)!((BPPcc&$0F)<<4)
;BPPcc			=	BPPcc+1
;			endr

;RotateXYZ		lea	Cos,a0
;			lea	Sin,a1
;
;			move.w	Xangle,d3
;			asl.w	#1,d3
;			move.w	0(a0,d3.w),d4			;d4=Cos(Xangle)
;			move.w	0(a1,d3.w),d5			;d5=Sin(Xangle)
;			move.w	d4,d6
;			move.w	d5,d7
;			muls	d1,d4				;Ypos*Cos(Xangle)
;			muls	d2,d5				;Zpos*Sin(Xangle)
;			sub.l	d5,d4				;Ypos*Cos(Xangle)-Zpos*Sin(Xangle)
;			muls	d1,d7				;Ypos*Sin(Xangle)
;			muls	d2,d6				;Zpos*Cos(Xangle)
;			add.l	d7,d6				;Ypos*Sin(Xangle)+Zpos*Cos(Xangle)
;			lsr.l	#8,d4
;			lsr.l 	#8,d6
;			move.l	d4,d1				;y
;			move.l	d6,d2				;z
;
;			move.w	Yangle,d3
;			asl.w	#1,d3
;			move.w	0(a0,d3.w),d4			;d4=Cos(Yangle)
;			move.w	0(a1,d3.w),d5			;d5=Sin(Yangle)
;			move.w	d4,d6				;Xtemp=Xpos*Cos(Yangle)-Zpos*Sin(Yangle)
;			move.w	d5,d7
;			muls	d0,d4				;Xpos*Cos(Yangle)
;			muls	d2,d5				;Zpos*Sin(Yangle)
;			sub.l	d5,d4				;Xpos*Cos(Yangle)-Zpos*Sin(Yangle)
;			muls	d0,d7				;Xpos*Sin(Yangle)
;			muls	d2,d6				;Zpos*Cos(Yangle)
;			add.l	d7,d6				;Xpos*Sin(Yangle)+Zpos*Cos(Yangle)
;			lsr.l	#8,d4
;			lsr.l	#8,d6
;			move.l	d4,d0				;x
;			move.l	d6,d2				;z
;			
;			move.w	Zangle,d3
;			asl.w	#1,d3
;			move.w	0(a0,d3.w),d4			;d4=Cos(Zangle)
;			move.w	0(a1,d3.w),d5			;d5=Sin(Zangle)
;			move.w	d4,d6				
;			move.w	d5,d7
;			muls	d0,d4				;d4=Xpos*Cos(Zangle)
;			muls	d1,d5				;d5=Ypos*Sin(Zangle)
;			sub.l	d5,d4				;d4=Xpos*Cos(Zangle)-Ypos*Sin(Zangle)
;			muls	d0,d7				;d7=Xpos*Sin(Zangle)
;			muls	d1,d6				;d6=Ypos*Cos(Zangle)
;			add.l	d7,d6				;d6=Xpos*Sin(Zangle)+Ypos*Cos(Zangle)
;			move.l	d4,d0				;x
;			move.l	d6,d1				;y
;			rts
;
;
;; ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ»
;; Ί CONVERTXY - Converts a rotated x,y,z to a Screen x,y		   Ί
;; ΜΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΉ
;; Ί Entry: d0=Xpos : d1=Ypos : d2=Zpos					   Ί
;; Ί  Exit: d3=Screen X : d4=Screen Y					   Ί
;; Ί  Used: d0-d4/a0-a3							   Ί
;; ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ
;
;ConvertXY		move.w	Xangle,d3
;			asl.w	#1,d3
;			move.w	0(a1,d3.w),d4
;			muls	#50,d4
;			lsr.l	#8,d4	
;			add.w	CenterX,d4
;			move.w	Yangle,d3
;			asl.w	#1,d3
;			move.w	0(a0,d3.w),d5
;			muls	#30,d5
;			lsr.l	#8,d5	
;			add.w	CenterY,d5
;			    
;			add.l	Zc,d2			;dz=dz+zc
;			move.l	d2,d7
;			divs	d2,d0			;dx=dx/dz
;			add.l	d4,d0			;dx=dx+xc
;			move.l	d0,d3			;=dx
;			divs	d7,d1			;dy=dy/dz
;			add.l	d5,d1			;dy=dy+yc
;			move.l	d1,d4			;=dy
;			rts
;


