;нннннннннннннннннннннннннннннннннннннннннннннн 
;н Example for a list- or offset-sort routine н
;нннннннннннннннннннннннннннннннннннннннннннннн
;Note:
;This Example is NOT executable. It's just made to give you an example
;how to realize such a sort-routine. You should better read the 
;article about this algorithm before trying to understand this!!! 

;Made by Duke/Level Four for D.I.S.C issue #6

;ннннннннннннннн Statements ннннннннннннннн
	Anzbobs		=	50	;amount of bobs/points
	ZMax 		=	120	;max/min  permitted z-value of 
					;your points (+-)!

S:
Start:	
	;Example for usage:
	;------------------

	;At first you've to init all lists and the pointer-tables
;ннннннннннннннннннннннннннннннннннннннннннннннннннннннннннннннннн
init_sorttabs:
	lea	listsortpt_mi(pc),a0
	lea	listsorttab(pc),a1
	move.w	#Zmax*2,d0		;for neg. and pos. values
listsortpt_loop:
	move.l	a1,(a0)+		;store pointer to datalist
	move.l	a1,a2			;store pointer to...  
	move.l	a2,(a1)			;...itself at start of datalist
	addi.w	#[anzbobs+1]*4,a1	;a1=address of next list
	dbf	d0,listsortpt_loop
;ннннннннннннннннннннннннннннннннннннннннннннннннннннннннннннннннннн

loop:
	;Now rotate x,y and z-koord of 1 point of your object by 
	;using your standard vector-routine and then...

;нннннннннннннннннн Store x- and y-koord in list нннннннннннннннннннн
;d0=x-koord, d1=y-koord, d2=z-koord (after rotation,translation and 
;projection!)

	lea	listsortpt(pc),a1 ;a1=address of table of listpointers
	andi.w	#-2,d2		;force even z-value
	add.w	d2,d2		;d2=d2*2 (z-koord must be divisible by
				;4 without rest, because one pointer
				;is 4 bytes long (of course))
	add.w	d2,a1		;z-koord is now used as offset(addition
				;or subtraction depends on sign (+-)!)
	move.l	(a1),a1		;a1=address of correct list for koords
	addq.l	#4,(a1)		;correct pointer (old pointer +2 words
				;to skip last x- and y-koord!)
	move.l	(a1),a1		;a1=correct destination-address
	movem.w	d0/d1,(a1)	;store x and y-koord (2 words)
;нннннннннннннннннннннннннннннннннннннннннннннннннннннннннннннннннннннн

	;now rotate all remaining points and store them in this way!

	;and when you've done this you must...

;нннннннннннннннннн Fetch right koords (and blit bobs) нннннннннннннннн

	lea	listsortptend(pc),a0 ;a0=end of table of listpointers
	move.w	#Zmax*2,d3	;amount of possible z-values (+-)
make_list:
	move.l	(a0),a1		;a1=address of list
	subq.w	#4,a0		;a0=address of next listpointer
	move.l	(a1),a2		;a2 points on last stored koords in list
	cmp.l	a1,a2		;does a2 point on itself?
	bhi.s	fetch_koords	;if not (a2 > a1) go and fetch koords!
	subq.w	#1,d3		;otherwise go on searching koords
	bne.s	make_list	;if d3=0 then ....
	bra.s	exit_search	;....stop searching!
fetch_koords:
	movem.w	(a2),d0/d1	;d0=x-koord/d1=y-koord
	subq.w	#4,a2		;a2=address of next koords

blit_bob:
;нннннннннннннннннннннннннннннннннннннннннннннннннннннннннннннннннннннн
;		   Here/now you should blit the bob!
;нннннннннннннннннннннннннннннннннннннннннннннннннннннннннннннннннннннн
	cmp.l	a1,a2		;are there any koords left in list?
	bgt.s	fetch_koords	;yeah -> fetch next koords!
mb_nextz:
	move.l	a1,(a1)		;restore startpointer in list
	subq.w	#1,d3
	bne.s	make_list	;go on searching
exit_search:
	Rts
;нннннннннннннннннннннннннннннннннннннннннннннннннннннннннннннннннннннн


listsortpt_mi:	blk.l Zmax	;pointers to lists for points
				;with negative z-koords
listsortpt:	blk.l Zmax+1	;pointers to lists for points
				;with positive z-koords
listsortptend:
listsorttab:	blk.l [Zmax*2+1]*[anzbobs+1] ;table of lists
