
	bra	Init
	bra	Plot
	bra	Plot3D
	bra	Calc3D
	bra	Distance
	bra	PlotWay
	rts

; Init(PunktFeld)
;	a1

Init:
	move.l	a1,Points
	move.l	d0,Width
	lea	RotMat(pc),a0
	rts


; Plot3D(BitMap,X,Y,Z)
;	  a0  d0 d1 d2

Plot3D:
	movem.l	d0-d2,-(sp)
	bsr	Calc3D
	tst.l	d0
	bmi	.Exit
	bsr	Plot
.Exit	movem.l	(sp)+,d0-d2
	rts


; PlotWay(BitMap,Z1,Z2)
;	   a0	 d0 d1

PlotWay:
	movem.l	d0-d7/a1,-(sp)
	move.l	d0,d3
	move.l	d1,d7
	addq.l	#1,d1
	mulu	#6,d1
	mulu	Width+2(pc),d1
	move.l	Points(pc),a1
	add.l	d1,a1

.LoopZ	move.l	Width(pc),d6
	subq.l	#1,d6
.LoopX	move	-(a1),d2
	move	-(a1),d1
	move	-(a1),d0
	bsr	Plot3D

	dbf	d6,.LoopX
	subq.l	#1,d7
	cmp.l	d3,d7
	bge	.LoopZ

	movem.l	(sp)+,d0-d7/a1
.Exit	rts


; Plot(BitMap,X,Y,Color)
;	a0   d0 d1 d2

Plot:
	movem.l	d0-d7/a0-a6,-(sp)
	tst.l	d0
	bmi	.Exit
	tst.l	d1
	bmi	.Exit
	cmp.l	#320,d0
	bpl	.Exit
	cmp.l	#256,d1
	bpl	.Exit

	move	(a0),d3
	mulu	d1,d3
	move.l	d0,d4
	asr.l	#3,d4
	add.l	d4,d3	; Offset

	move	d0,d4
	and.l	#7,d4
	eor.l	#7,d4	; Bit

	move.b	5(a0),d5
	ext.w	d5
	asl.w	#2,d5
	lea	4(a0,d5.w),a1

	move.b	5(a0),d7
	ext.w	d7
	subq	#1,d7

.Loop	move.l	(a1),a2
	add.l	d3,a2

	btst	d7,d2
	bne	.Set

	bclr	d4,(a2)
	bra	.Cont

.Set	bset	d4,(a2)

.Cont	subq	#4,a1
	dbf	d7,.Loop

.Exit	movem.l	(sp)+,d0-d7/a0-a6
	rts




; Calc3D(X,Y,Z)
;	d0 d1 d2

Calc3D:
	movem.l	d3-d7/a0-a6,-(sp)

	lea	RotMat(pc),a1
	add	6(a1),d0
	add	14(a1),d1
	add	22(a1),d2

	clr.l	d3
	move	0(a1),d7
	muls	d0,d7
	add.l	d7,d3
	move	2(a1),d7
	muls	d1,d7
	add.l	d7,d3
	move	4(a1),d7
	muls	d2,d7
	add.l	d7,d3

	clr.l	d4
	move	8(a1),d7
	muls	d0,d7
	add.l	d7,d4
	move	10(a1),d7
	muls	d1,d7
	add.l	d7,d4
	move	12(a1),d7
	muls	d2,d7
	add.l	d7,d4

	clr.l	d5
	move	16(a1),d7
	muls	d0,d7
	add.l	d7,d5
	move	18(a1),d7
	muls	d1,d7
	add.l	d7,d5
	move	20(a1),d7
	muls	d2,d7
	add.l	d7,d5
	asl.l	#2,d5

	swap	d5
	ext.l	d5
	move.l	d5,d2
	asr.l	#3,d2
	sub.l	#31,d2
	neg.l	d2
	cmp.l	#32,d2
	blt	.NoCut
	moveq.l	#31,d2
.NoCut
	cmp.l	#8*16,d5	; Nach Verlassen der Farbpalette ist Schluß
	bgt	.Error
	add.l	#200,d5	; Koordinatenursprung nach hinten verschieben
	ble	.Error

	asr.l	#6,d3	; 30 Bit : 6 Bit = 24 Bit
	move.l	d3,d0
	divs	d5,d0	; 24 Bit : ?
	bvs	.Error
	asr.l	#6,d4
	move.l	d4,d1
	divs	d5,d1
	bvs	.Error

	cmp	#159,d0
	bgt	.Error
	cmp	#-160,d0
	blt	.Error
	cmp	#127,d1
	bgt	.Error
	cmp	#-128,d1
	blt	.Error

	add	#160,d0
	sub	#128,d1
	neg	d1

	ext.l	d0
	ext.l	d1

	movem.l	(sp)+,d3-d7/a0-a6
	rts

.Error	moveq.l	#-1,d0
	movem.l	(sp)+,d3-d7/a0-a6
	rts



; Distance(z1,z2,X,Y,Z)
;	   d0 d1 d2d3d4

Distance:
	movem.l	d3-d7/a0-a6,-(sp)

;	clr.l	MinX
;	clr.l	MinZ
	move.l	#$7fffffff,MinDist

	move.l	Points(pc),a1
	move.l	d1,d7
	addq.l	#1,d1
	mulu	#6,d1
	mulu	Width+2(pc),d1
	add.l	d1,a1
	move.l	d0,a2

.LoopZ	move.l	Width(pc),d6
	subq.l	#1,d6
.LoopX	clr.l	d0
	move	-(a1),d5
	sub	d4,d5
	muls	d5,d5
	add.l	d5,d0

	move	-(a1),d5
	sub	d3,d5
	muls	d5,d5
	add.l	d5,d0

	move	-(a1),d5
	sub	d2,d5
	muls	d5,d5
	add.l	d5,d0

	cmp.l	MinDist(pc),d0
	bge	.NoMin
	move.l	d0,MinDist
	move.l	d6,MinX
	move.l	d7,MinZ
.NoMin
	dbf	d6,.LoopX
	subq.l	#1,d7
	cmp.l	a2,d7
	bge	.LoopZ

	move.l	MinX(pc),d0
	move.l	MinZ(pc),d1
	move.l	MinDist(pc),d2
; d0 - x Koordinate im Punktefeld
; d1 - y Koordinate im Punktefeld
; d2 - Abstand zum Quadrat

.Exit	movem.l	(sp)+,d3-d7/a0-a6
	rts


RotMat	dc.w	$4000,0,0,0
	dc.w	0,$4000,0,0
	dc.w	0,0,$4000,0

Points	dc.l	0	; Zeiger
Width	dc.l	0	; Spielfeldbreite
MinX	dc.l	0
MinZ	dc.l	0
MinDist	dc.l	0

