*
* T-90   V1.1   by ROGER FISCHLIN
*                  STEIGERWALDWEG 6
*                  D-6450 HANAU 7
*                  WEST GERMANY
*
*          This program is public domain.
*
*
*
*


	incdir   	"fh1:include/"

	include	intuition/intuition.i
	include	intuition/intuition_lib.i
        	include 	exec/memory.i
	include	exec/exec_lib.i
	include	exec/execbase.i
	include	graphics/graphics_lib.i
	include	graphics/text.i
	include	libraries/dos_lib.i
	include	libraries/dos.i
	include	libraries/dosextens.i
	include	workbench/startup.i



	include	insert/Macros




	clr.l	WB_Message	; startup code
	movem.l	d0/a0,-(sp)		

	jsr	OpenThem	; get libraries 

	sub.l	a1,a1	
	CALLEXEC 	FindTask
	move.l	d0,a4
	tst.l	pr_CLI(a4)
	beq.s	VonWB	
				
	movem.l	(sp)+,d0/a0
	bra	Ende_Startup

VonWB	lea.l	8(sp),sp	
	lea	pr_MsgPort(a4),a0
	CALLEXEC 	WaitPort		
	lea	pr_MsgPort(a4),a0
	CALLEXEC 	GetMsg		
	move.l	d0,WB_Message
	
	move.l	d0,a0
	move.l	sm_ArgList(a0),a0
	cmp.l	#0,a0
	beq	Ende_Startup
	move.l	(a0),d1		
	CALLDOS	CurrentDir
Ende_Startup	bsr	Start
	tst.l	WB_Message		
	beq.s	SU_Label0		
	CALLEXEC 	Forbid		
	move.l	WB_Message,a1
	CALLEXEC 	ReplyMsg
SU_Label0	moveq	#0,d0
	rts

WB_Message	dc.l	0

OpenThem	lea	dosname(pc),a1		; open libs
	moveq.l	#0,d0
	CALLEXEC 	OpenLibrary
	move.l	d0,_DOSBase	
	lea	intname(pc),a1                 
	moveq.l	#0,d0		
	CALLEXEC 	OpenLibrary
	move.l	d0,_IntuitionBase  
	lea	grafname(pc),a1
	moveq.l	#0,d0
	CALLEXEC 	OpenLibrary
	move.l	d0,_GfxBase
	rts

_DOSBase	dc.l	0
dosname	DOSNAME
_GfxBase	dc.l	0
grafname	GRAFNAME
_IntuitionBase	dc.l	0
intname	INTNAME
Window1	dc.l	0
TOPAZ_80	dc.l	T_80name
	dc.w	TOPAZ_EIGHTY
	dc.b	FS_NORMAL,FPF_ROMFONT
	even
T_80name	dc.b	"topaz.font",0
	even
FONT80	dc.l	0


Start	lea.l	TOPAZ_80,a0		; opnen TOPAZ 80
	CALLGRAF	OpenFont
	move.l	d0,FONT80

	lea.l	Screendef,a0		; open screen
	CALLINT	OpenScreen
	tst.l	d0
	beq	QUIT1
	move.l	d0,ScreenPtr1


	lea.l	Windowdef1,a0		; open window	
	CALLINT	OpenWindow
	tst.l	d0
	beq	QUIT2
	move.l	d0,Window1

	move.l	d0,a0		; set colours
	CALLINT	ViewPortAddress
	move.l	d0,a0
	move.l	#16,d0
	lea.l	Palette,a1
	CALLGRAF	LoadRGB4
	CALLINT	RemakeDisplay


	sub.l	a1,a1		; DOS requester should appear in FileMaster's window
	CALLEXEC	FindTask
	move.l	d0,a0
	move.l	pr_WindowPtr(a0),OldWindow
	move.l	Window1,pr_WindowPtr(a0)



	move.l	ScreenPtr1,a0
	CALLINT	ScreenToFront

	
WindowPointer set Window1		

	SETAPEN	#2			; bachground
	RECTFILL	#0,#0,#640,#200
	SETAPEN	#1			; fields
	RECTFILL	#55-2,#20,#160+55+2,#160+30+1
	SETAPEN	#1
	RECTFILL	#370+55-2,#20,#370+160+55+2,#160+30+1
	SETAPEN	#0
	RECTFILL	#55,#30,#160+55,#160+30
	SETAPEN	#0
	RECTFILL	#370+55,#30,#370+160+55,#160+30
	SETDRMD	#RP_JAM1
	WRITE	#55+80-(9*4),#20+7,#0,<"Player 1:">
	WRITE	#370+55+80-(9*4),#20+7,#0,<"Player 2:">
	jsr	MakeRects

	lea.l	W1_G1(pc),a0
	move.l	WindowPointer,a1
	sub.l	a2,a2
	CALLINT	RefreshGadgets	
	jsr	Info

	move.l	WindowPointer,a1
	move.l	wd_RPort(a1),a1	
	move.l	FONT80,a0
	CALLGRAF	SetFont
	SETDRMD	#RP_JAM2
	SETBPEN	#2
	jsr	LoadHiScores
	jsr	MakeScores
	jsr	LoadKeys

Inwait	jsr	DisplayHiScores

wait	move.l	Window1,a0		; wait .....
	move.l	wd_UserPort(a0),a0
	move.l	a0,a5
	CALLEXEC	WaitPort		
	move.l	a5,a0
	CALLEXEC	GetMsg		; get message
	move.l	d0,a1	
	move.l	im_Class(a1),d4	; get data
	move.w	im_Code(a1),d5
	move.w	im_Qualifier(a1),d3
	move.l	im_IAddress(a1),a4
	CALLEXEC 	ReplyMsg		; reply message
	
waitGadget	moveq.l	#0,d0		
	move.b	gg_GadgetID+1(a4),d0	; get gadget ID

	cmp.l	#GADGETUP,d4
	bne	wait
GameEXIT	cmp.b	#3,d0
	beq	QUIT
	tst.b	d0
	beq	ABOUT
	cmp.b	#1,d0
	beq	GAME_ONE
	cmp.b	#2,d0
	beq	GAME_TWO
	cmp.b	#4,d0
	beq	DEFINE_KEYS
	
	bra	QUIT
	



Error	move.l	ScreenPtr1,a0
	CALLINT	DisplayBeep
	bra	wait


QUIT	sub.l	a1,a1
	CALLEXEC	FindTask
	move.l	d0,a0
	move.l	OldWindow,pr_WindowPtr(a0)
	move.l	Window1,a0		; bye, bye !
	CALLINT	CloseWindow
	move.l	ScreenPtr1,a0
	CALLINT	CloseScreen
	moveq.l	#0,d0
	rts

OldWindow	dc.l	0

Screendef	dc.w	0,0,640,200,3
	dc.b	0,1
	dc.w	V_HIRES	
	dc.w	CUSTOMSCREEN!SCREENBEHIND
	dc.l	TOPAZ_80
	dc.l	.S_Title,0,0
	
	even
.S_Title	dc.b	" T90  V1.1  by Roger Fischlin 3/1990            ",0
	even


Windowdef1	dc.w	0,0,640,200
	dc.b	7,1
	dc.l	GADGETUP
	dc.l	ACTIVATE!SMART_REFRESH!BACKDROP!SMART_REFRESH!BORDERLESS
	dc.l	W1_G1
	dc.l	0
	dc.l	0
ScreenPtr1	dc.l	0,0
	dc.w	0,0,0,0,CUSTOMSCREEN

W1_G1	GADGET	W1_G2,270,75,100,0,<"About">,1,4
W1_G2	GADGET	W1_G3,270,95,100,1,<"One Player">,1,4
W1_G3	GADGET	W1_G4,270,115,100,2,<"Two Players">,1,4
W1_G4	GADGET	W1_G5,270,135,100,3,<"Quit">,1,4
W1_G5	GADGET	0,270,55,100,4,<"Keys">,1,4


QUIT3	move.l	Window1,a0		
	CALLINT	CloseWindow

QUIT2	move.l	ScreenPtr1,a0
	CALLINT	CloseScreen

QUIT1	move.l	#RECOVERY_ALERT,d0	; error message
	move.l	#30,d1
	lea.l	ErrorText,a0
	CALLINT	DisplayAlert

	moveq.l	#0,d0
	rts


ErrorText	dc.w	60
	dc.b	17
	dc.b	"T 90 : ERROR !?! I cannot open the screen / window !"
	dc.b 	0,0
	even



MakeRects
WindowPointer set Window1		

	SETAPEN	#1			; score
	RECTFILL	#270,#20,#370,#40+2
	SETAPEN	#0
	RECTFILL	#270+2,#21,#370-2,#41
	SETAPEN	#1			; next piece
	RECTFILL	#270,#160,#370,#191
	SETAPEN	#0
	RECTFILL	#270+2,#161,#370-2,#190
	SETAPEN	#0
	RECTFILL	#55,#30,#160+55,#160+30
	SETAPEN	#0
	RECTFILL	#370+55,#30,#370+160+55,#160+30
	jmp	PrepareFields


ShowNextPiece	SETAPEN	#0
	RECTFILL	#270+2,#161,#370-2,#190
	lea.l	Pieces(pc),a0		; show next piece
	move.l	NextPiece,d0
	lsl.l	#4,d0
	add.l	d0,a0
	move.l	(a0),a0
	move.l	8(a0),d0
	lea.l	20(a0),a1
	lea.l	BlockImages(pc),a2	; put up image structures
	moveq.l	#0,d1
.L0	clr.l	ig_NextImage(a2)
	move.b	d0,ig_PlaneOnOff(a2)
	move.l	d1,d2
	and.l	#3,d2
	lsl.l	#4,d2
	addq.l	#2,d2
	move.w	d2,ig_LeftEdge(a2)
	move.l	d1,d2
	lsr.w	#2,d2
	lsl.w	#3,d2
	addq.l	#2,d2
	move.w	d2,ig_TopEdge(a2)
	move.w	#16-4,ig_Width(a2)
	move.w	#8-2,ig_Height(a2)
	move.w	#3,ig_Depth(a2)
	tst.b	(a1)+
	beq.s	.L1
	lea.l	ig_SIZEOF(a2),a2
	move.l	a2,-4(a2)		; pointer to next image structure
.L1	addq.l	#1,d1
	cmp.b	#12,d1
	bne	.L0
	clr.l	-4(a2)		; clear last pointer 
	move.l	(a0),d2
	move.l	4(a0),d3
	lsl.l	#3,d2
	lsl.l	#2,d3
	move.l	#270+50,d0
	move.l	#161+14,d1
	sub.l	d2,d0
	sub.l	d3,d1
	move.l	Window1,a0
	move.l	wd_RPort(a0),a0
	lea.l	BlockImages,a1
	CALLINT	DrawImage
	rts


Palette	dc.w	$000,$eee,$876,$839,$04d,$e20,$3c0,$fd0
	
	ds.b	100

FieldA	ds.b	12*21
FieldB	ds.b	12*21

ScoreA	dc.l	0
ScoreB	dc.l	0
Time	dc.l	0
NextPiece	dc.l	0
PieceA	dc.l	2
PieceB	dc.l	0
DegreeA	dc.l	0
DegreeB	dc.l	0
PositionAX	dc.l	0
PositionAY	dc.l	0
PositionBX	dc.l	0
PositionBY	dc.l	0
Timer	dc.l	0
Clock	dc.l	0
GameOver	dc.w	0
Player	dc.b	0
	even



	dc.b	"KEYS:"

LeftA	dc.w	$20
RightA	dc.w	$22
RotateA	dc.w	$21
DropA	dc.w	$40

LeftB	dc.w	$2d
RightB	dc.w	$2f
RotateB	dc.w	$2e
DropB	dc.w	$43


PrepareFields	lea.l	FieldA(pc),a0	; clear it
	lea.l	FieldB(pc),a1
	move.l	#19,d0
.Q1	move.l	#9,d1
	move.b	#$ff,(a0)+
	move.b	#$ff,(a1)+
.Q2	move.b	#0,(a0)+
	move.b	#0,(a1)+
	dbra	d1,.Q2
	move.b	#$ff,(a0)+
	move.b	#$ff,(a1)+
	dbra	d0,.Q1
	
	move.l	#11,d0
.Q3	move.b	#$ff,(a0)+
	move.b	#$ff,(a1)+
	dbra	d0,.Q3
	rts


Pieces	dc.l	Piece1_0,Piece1_1,Piece1_2,Piece1_3
	dc.l	Piece2_0,Piece2_1,Piece2_2,Piece2_3
	dc.l	Piece3_0,Piece3_1,Piece3_2,Piece3_3
	dc.l	Piece4_0,Piece4_1,Piece4_2,Piece4_3
	dc.l	Piece5_0,Piece5_1,Piece5_2,Piece5_3
	dc.l	Piece6_0,Piece6_1,Piece6_2,Piece6_3
	dc.l	Piece7_0,Piece7_1,Piece7_2,Piece7_3



Piece1_0	dc.l	3,2	; size
	dc.l	1	; colour
	dc.l	0,0	; offset 
	dc.l	$00FF0000	; data
	dc.l	$FFFFFF00
	dc.l	0,0
Piece1_1	dc.l	2,3	; size
	dc.l	1	; colour
	dc.l	1,0	; offset 
	dc.l	$FF000000	; data
	dc.l	$FFFF0000	
	dc.l	$FF000000
	dc.l	0
Piece1_2	dc.l	3,2	; size
	dc.l	1	; colour
	dc.l	-1,1	; offset 
	dc.l	$FFFFFF00	; data
	dc.l	$00FF0000
	dc.l	0,0
Piece1_3	dc.l	2,3	; size
	dc.l	1	; colour
	dc.l	0,-1	; offset 
	dc.l	$00FF0000	; data
	dc.l	$FFFF0000
	dc.l	$00FF0000
	dc.l	0

Piece2_0	dc.l	3,2	; size
	dc.l	2	; colour
	dc.l	-1,0	; offset 
	dc.l	$00FFFF00	; data
	dc.l	$FFFF0000
	dc.l	0
	dc.l	0
Piece2_1	dc.l	2,3	; size
	dc.l	2	; colour
	dc.l	1,0	; offset 
	dc.l	$FF000000	; data
	dc.l	$FFFF0000
	dc.l	$00FF0000
	dc.l	0
Piece2_2	dc.l	3,2	; size
	dc.l	2	; colour
	dc.l	-1,0	; offset 
	dc.l	$00FFFF00	; data
	dc.l	$FFFF0000
	dc.l	$0
	dc.l	0
Piece2_3	dc.l	2,3	; size
	dc.l	2	; colour
	dc.l	1,0	; offset 
	dc.l	$FF000000	; data
	dc.l	$FFFF0000
	dc.l	$00FF0000
	dc.l	0

Piece3_0	dc.l	3,2	; size
	dc.l	3	; colour
	dc.l	-1,0	; offset 
	dc.l	$FFFF0000	; data
	dc.l	$00FFFF00
	dc.l	0
	dc.l	0
Piece3_1	dc.l	2,3	; size
	dc.l	3	; colour
	dc.l	1,0	; offset 
	dc.l	$00FF0000	; data
	dc.l	$FFFF0000
	dc.l	$FF000000
	dc.l	0
Piece3_2	dc.l	3,2	; size
	dc.l	3	; colour
	dc.l	-1,0	; offset 
	dc.l	$FFFF0000	; data
	dc.l	$00FFFF00
	dc.l	$0
	dc.l	0
Piece3_3	dc.l	2,3	; size
	dc.l	3	; colour
	dc.l	1,0	; offset 
	dc.l	$00FF0000	; data
	dc.l	$FFFF0000
	dc.l	$FF000000
	dc.l	0

Piece4_0	dc.l	4,1	; size
	dc.l	4	; colour
	dc.l	-1,0	; offset 
	dc.l	$FFFFFFFF	; data
	dc.l	0
	dc.l	0
	dc.l	0
Piece4_1	dc.l	1,4	; size
	dc.l	4	; colour
	dc.l	1,0	; offset 
	dc.l	$FF000000	; data
	dc.l	$FF000000
	dc.l	$FF000000
	dc.l	$FF000000
Piece4_2	dc.l	4,1	; size
	dc.l	4	; colour
	dc.l	-1,0	; offset 
	dc.l	$FFFFFFFF	; data
	dc.l	$0
	dc.l	$0
	dc.l	0
Piece4_3	dc.l	1,4	; size
	dc.l	4	; colour
	dc.l	1,0	; offset 
	dc.l	$FF000000	; data
	dc.l	$FF000000
	dc.l	$FF000000
	dc.l	$FF000000

Piece5_0
Piece5_1
Piece5_2
Piece5_3	dc.l	2,2	; size
	dc.l	5	; colour
	dc.l	1,0	; offset 
	dc.l	$FFFF0000	; data
	dc.l	$FFFF0000
	dc.l	0
	dc.l	0

Piece6_0	dc.l	3,2	; size
	dc.l	6	; colour
	dc.l	-1,0	; offset 
	dc.l	$0000FF00	; data
	dc.l	$FFFFFF00
	dc.l	0
	dc.l	0
Piece6_1	dc.l	2,3	; size
	dc.l	6	; colour
	dc.l	1,0	; offset 
	dc.l	$FF000000	; data
	dc.l	$FF000000
	dc.l	$FFFF0000
	dc.l	0
Piece6_2	dc.l	3,2	; size
	dc.l	6	; colour
	dc.l	-1,0	; offset 
	dc.l	$FFFFFF00	; data
	dc.l	$FF000000
	dc.l	$0
	dc.l	0
Piece6_3	dc.l	2,3	; size
	dc.l	6	; colour
	dc.l	1,0	; offset 
	dc.l	$FFFF0000	; data
	dc.l	$00FF0000
	dc.l	$00FF0000
	dc.l	0


Piece7_0	dc.l	3,2	; size
	dc.l	7	; colour
	dc.l	-1,0	; offset 
	dc.l	$FF000000	; data
	dc.l	$FFFFFF00
	dc.l	0
	dc.l	0
Piece7_1	dc.l	2,3	; size
	dc.l	7	; colour
	dc.l	1,0	; offset 
	dc.l	$FFFF0000	; data
	dc.l	$FF000000
	dc.l	$FF000000
	dc.l	0
Piece7_2	dc.l	3,2	; size
	dc.l	7	; colour
	dc.l	-1,0	; offset 
	dc.l	$FFFFFF00	; data
	dc.l	$0000FF00
	dc.l	$0
	dc.l	0
Piece7_3	dc.l	2,3	; size
	dc.l	7	; colour
	dc.l	1,0	; offset 
	dc.l	$00FF0000	; data
	dc.l	$00FF0000
	dc.l	$FFFF0000
	dc.l	0

Windowdef2	dc.w	200,25,260,140
	dc.b	1,1
	dc.l	GADGETUP
	dc.l	ACTIVATE!SMART_REFRESH!SMART_REFRESH
	dc.l	W2_G1
	dc.l	0
	dc.l	0
ScreenPtr2	dc.l	0,0
	dc.w	0,0,0,0,CUSTOMSCREEN

W2_G1	GADGET	0,80,120,100,0,<"OK">,1,4

Window2	dc.l	0

WindowPointer set Window2

ABOUT	jsr	Info
	jmp	wait


Info	move.l	ScreenPtr1,ScreenPtr2
	lea.l	Windowdef2(pc),a0
	CALLINT	OpenWindow
	move.l	d0,Window2
	tst.l	d0
	beq	.X
	
	SETAPEN	#2
	RECTFILL	#2,#1,#257,#138

	lea.l	W2_G1(pc),a0
	move.l	WindowPointer,a1
	sub.l	a2,a2
	CALLINT	RefreshGadgets	
	SETDRMD	#RP_JAM1
	
	WRITE	#((260-(4*8))/2)+1,#16,#0,<"T-90">
	WRITE	#(260-(4*8))/2,#15,#1,<"T-90">
	WRITE	#((260-(10*8))/2)+1,#26,#0,<"ญญญญญญญญญญ">
	WRITE	#(260-(10*8))/2,#25,#1,<"ญญญญญญญญญญ">
	
	WRITE	#((260-(22*8))/2)+1,#36,#0,<"Release 1.1  /  4.3.90">
	WRITE	#(260-(22*8))/2,#35,#1,<"Release 1.1  /  4.3.90">
	WRITE	#((260-(17*8))/2)+1,#56,#0,<"by Roger Fischlin">
	WRITE	#(260-(17*8))/2,#55,#7,<"by Roger Fischlin">
	WRITE	#((260-(17*8))/2)+1,#66,#0,<"   Steigerwaldweg 6">
	WRITE	#(260-(17*8))/2,#65,#7,<"   Steigerwaldweg 6">
	WRITE	#((260-(17*8))/2)+1,#76,#0,<"   D-6450 Hanau 7">
	WRITE	#(260-(17*8))/2,#75,#7,<"   D-6450 Hanau 7">
	WRITE	#((260-(17*8))/2)+1,#86,#0,<"   West Germany">
	WRITE	#(260-(17*8))/2,#85,#7,<"   West Germany">
	WRITE	#((260-(25*8))/2)+1,#106,#0,<"T-90 is public domain !!!">
	WRITE	#(260-(25*8))/2,#105,#1,<"T-90 is public domain !!!">

	
	move.l	Window2,a0		; wait .....
	move.l	wd_UserPort(a0),a0
	CALLEXEC	WaitPort		
	move.l	Window2,a0
	CALLINT	CloseWindow
.X	rts

WindowPointer set Window1


BlockImages	ds.b	ig_SIZEOF*16

********************************************************
*
*  Player 1:
*
********************************************************

DisplayA	lea.l	Pieces(pc),a0
	move.l	PieceA,d0
	lsl.l	#4,d0
	add.l	d0,a0
	move.l	(a0),a0
	move.l	8(a0),d0

MakeBIA	lea.l	20(a0),a1
	lea.l	BlockImages(pc),a2	; put up image structures
	moveq.l	#0,d1
.L0	clr.l	ig_NextImage(a2)
	move.b	d0,ig_PlaneOnOff(a2)
	move.l	d1,d2
	and.l	#3,d2
	lsl.l	#4,d2
	addq.l	#2,d2
	move.w	d2,ig_LeftEdge(a2)
	move.l	d1,d2
	lsr.w	#2,d2
	lsl.w	#3,d2
	addq.l	#2,d2
	move.w	d2,ig_TopEdge(a2)
	move.w	#16-4,ig_Width(a2)
	move.w	#8-2,ig_Height(a2)
	move.w	#3,ig_Depth(a2)
	tst.b	(a1)+
	beq	.L1
	lea.l	ig_SIZEOF(a2),a2
	move.l	a2,-4(a2)		; pointer to next image structure
.L1	addq.l	#1,d1
	cmp.b	#16,d1
	bne	.L0
	clr.l	-4(a2)		; clear last pointer 
	
	move.l	PieceA,d0		; display image
	lsl.l	#4,d0
	move.l	DegreeA,d1
	lsl.l	#3,d1
	add.l	d0,d1
	lea.l	Pieces(pc),a1
	add.l	d1,a1
	move.l	(a1),a1
	move.l	PositionAX,d0
	lsl.w	#4,d0
	add.w	#55,d0
	move.l	PositionAY,d1
	lsl.w	#3,d1
	add.w	#30,d1
	move.l	Window1,a0
	move.l	wd_RPort(a0),a0
	lea.l	BlockImages,a1
	CALLINT	DrawImage
	rts

RotatePieceA	move.l	PieceA,d0	; check if you can rotate it !
	lsl.l	#4,d0
	move.l	DegreeA,d1
	addq.l	#1,d1
	and.l	#3,d1
	lsl.l	#2,d1
	add.l	d0,d1
	lea.l	Pieces,a0
	add.l	d1,a0
	move.l	(a0),a0	
	move.l	12(a0),d0
	move.l	16(a0),d1
	add.l	PositionAX,d0
	add.l	PositionAY,d1
	jsr	CheckA
	tst.l	d0
	bne	.wait

	move.l	PieceA,d0		; rotate piece of player A
	lsl.l	#4,d0
	move.l	DegreeA,d1
	lsl.l	#2,d1
	add.l	d0,d1
	lea.l	Pieces,a0
	
	add.l	d1,a0
	move.l	(a0),a0
	moveq.l	#0,d0
	jsr	MakeBIA		; clear old piece
	
	add.l	#1,DegreeA
	and.l	#3,DegreeA

	move.l	PieceA,d0
	lsl.l	#4,d0
	move.l	DegreeA,d1
	lsl.l	#2,d1
	add.l	d0,d1
	lea.l	Pieces,a0
	add.l	d1,a0
	move.l	(a0),a0
	move.l	12(a0),d0
	move.l	16(a0),d1
	add.l	d0,PositionAX
	add.l	d1,PositionAY
	move.l	8(a0),d0
	jsr	MakeBIA
.wait	jmp	GameWait

Key	and.l	#$7f,d5
	cmp.w	LeftA,d5		; any useful key pressed ???
	beq	MoveLeftA
	cmp.w	RightA,d5
	beq	MoveRightA
	cmp.w	RotateA,d5
	beq	RotatePieceA
	cmp.w	DropA,d5
	beq	DropPieceA
	jmp	GameWait

MoveLeftA	move.l	PieceA,d0		
	lsl.l	#4,d0
	move.l	DegreeA,d1
	lsl.l	#2,d1
	add.l	d0,d1
	lea.l	Pieces,a0
	add.l	d1,a0
	move.l	(a0),a0
	move.l	PositionAX,d0
	subq.l	#1,d0
	move.l	PositionAY,d1
	jsr	CheckA		; check if there's any room
	tst.l	d0
	bne	.wait
	moveq.l	#0,d0
	jsr	MakeBIA		; clear old piece
	move.l	PieceA,d0
	lsl.l	#4,d0
	move.l	DegreeA,d1
	lsl.l	#2,d1
	add.l	d0,d1
	lea.l	Pieces,a0
	add.l	d1,a0
	move.l	(a0),a0
	sub.l	#1,PositionAX
	move.l	8(a0),d0
	jsr	MakeBIA
.wait	jmp	GameWait

MoveRightA	move.l	PieceA,d0		
	lsl.l	#4,d0
	move.l	DegreeA,d1
	lsl.l	#2,d1
	add.l	d0,d1
	lea.l	Pieces,a0
	add.l	d1,a0
	move.l	(a0),a0
	move.l	PositionAX,d0
	addq.l	#1,d0
	move.l	PositionAY,d1
	jsr	CheckA
	tst.l	d0
	bne	.wait
	moveq.l	#0,d0
	jsr	MakeBIA		; clear old piece
	move.l	PieceA,d0
	lsl.l	#4,d0
	move.l	DegreeA,d1
	lsl.l	#2,d1
	add.l	d0,d1
	lea.l	Pieces,a0
	add.l	d1,a0
	move.l	(a0),a0
	add.l	#1,PositionAX
	move.l	8(a0),d0
	jsr	MakeBIA
.wait	jmp	GameWait

FallA	move.l	PieceA,d0		
	lsl.l	#4,d0
	move.l	DegreeA,d1
	lsl.l	#2,d1
	add.l	d0,d1
	lea.l	Pieces,a0
	add.l	d1,a0
	move.l	(a0),a0
	move.l	PositionAX,d0
	move.l	PositionAY,d1
	addq.l	#1,d1
	jsr	CheckA
	tst.l	d0
	bne	Drop_waitA
	moveq.l	#0,d0
	jsr	MakeBIA		; clear old piece
	move.l	PieceA,d0
	lsl.l	#4,d0
	move.l	DegreeA,d1
	lsl.l	#2,d1
	add.l	d0,d1
	lea.l	Pieces,a0
	add.l	d1,a0
	move.l	(a0),a0
	add.l	#1,PositionAY
	move.l	8(a0),d0
	jsr	MakeBIA
	jmp	GameWait


DropPieceA	move.l	PieceA,d0		
	lsl.l	#4,d0
	move.l	DegreeA,d1
	lsl.l	#2,d1
	add.l	d0,d1
	lea.l	Pieces,a0
	add.l	d1,a0
	move.l	(a0),a0
	move.l	PositionAX,d0
	move.l	PositionAY,d1
	addq.l	#1,d1
	jsr	CheckA
	tst.l	d0
	bne	Drop_waitA
	add.l	#1,ScoreA
	moveq.l	#0,d0
	jsr	MakeBIA		; clear old piece
	move.l	PieceA,d0
	lsl.l	#4,d0
	move.l	DegreeA,d1
	lsl.l	#2,d1
	add.l	d0,d1
	lea.l	Pieces,a0
	add.l	d1,a0
	move.l	(a0),a0
	add.l	#1,PositionAY
	move.l	8(a0),d0
	jsr	MakeBIA
	jmp	DropPieceA

Drop_waitA	move.l	PieceA,d0		; copy piece into field
	lsl.l	#4,d0
	move.l	DegreeA,d1
	lsl.l	#2,d1
	add.l	d0,d1
	lea.l	Pieces,a0
	add.l	d1,a0
	move.l	(a0),a0
	lea.l	FieldA(pc),a1
	move.l	PositionAX,d0
	addq.l	#1,d0
	add.l	d0,a1
	move.l	PositionAY,d0
	mulu	#12,d0
	add.l	d0,a1
	lea.l	20(a0),a0
	moveq.l	#3,d2
	moveq.l	#3,d3
.E1	moveq.l	#3,d4
.E2	move.b	(a0)+,d0
	or.b	d0,(a1)+
	dbra	d4,.E2
	add.l	#12-4,a1
	dbra	d3,.E1

	jsr	ShowScores
	jsr	DeleteRowA
	jsr	NewPieceA
	tst.l	d0
	bne	GameQUITA
	jmp	GameWait
	
	


CheckA	movem.l	d1-d7/a0-a5,-(sp)	; check if there are no blocks
	lea.l	FieldA(pc),a1
	addq.l	#1,d0
	add.l	d0,a1
	mulu	#12,d1
	add.l	d1,a1
	moveq.l	#3,d2
	lea.l	.L1(pc),a2
	moveq.l	#3,d3
.E1	moveq.l	#3,d4
.E2	move.b	(a1)+,(a2)+
	dbra	d4,.E2
	add.l	#12-4,a1
	dbra	d3,.E1

	move.l	20(a0),d0
	move.l	.L1,d1
	and.l	d0,d1
	move.l	d1,d0
	tst.l	d0
	bne	.E3
	move.l	24(a0),d0
	move.l	.L2,d1
	and.l	d0,d1
	move.l	d1,d0
	tst.l	d0
	bne	.E3

	move.l	28(a0),d0
	move.l	.L3,d1
	and.l	d0,d1
	move.l	d1,d0
	tst.l	d0
	bne	.E3
	move.l	32(a0),d0
	move.l	.L4,d1
	and.l	d0,d1
	move.l	d1,d0
.E3	movem.l	(sp)+,d1-d7/a0-a5
	rts
.L1	dc.l	0
.L2	dc.l	0
.L3	dc.l	0
.L4	dc.l	0


NewPieceA	jsr	GetPiece
	move.l	d0,PieceA
	lsl.l	#4,d0
	add.l	#Pieces,d0
	move.l	d0,a0
	move.l	(a0),a0
	moveq.l	#4,d0
	move.l	d0,PositionAX
	moveq.l	#0,d1
	move.l	d1,PositionAY
	clr.l	DegreeA
	jsr	CheckA
	tst.l	d0
	bne.s	.L1
	jsr	DisplayA
	moveq.l	#0,d0
.L1	rts


DeleteRowA	moveq.l	#1,d0		; delete all posible rows / player A
.W1	move.l	d0,-(sp)
	bsr.s	.KillRowA
	move.l	(sp)+,d0
	addq.l	#1,d0
	cmp.w	#20,d0		; until row 20
	bne.s	.W1
	jmp	ShowScores		; update score

.KillRowA	lea.l	FieldA+1(pc),a0	; delete row if possible
	move.l	d0,d1
	mulu	#12,d1
	add.l	d1,a0

	move.l	#9,d1		; check if there are spaces
.T1	tst.b	(a0)+
	beq	.NoMove
	dbra	d1,.T1

.T5	add.l	#10,ScoreA
	move.l	d0,d1
	mulu	#12,d1
	lea.l	FieldA+12(pc),a1
	lea.l	FieldA(pc),a0
	add.l	d1,a0
	add.l	d1,a1
.T2	move.b	-(a0),-(a1)
	cmp.l	#FieldA,a0
	bne.s	.T2
	lea.l	FieldA+1(pc),a0
	move.l	#9,d0
.T3	move.b	#0,(a0)+
	dbra	d0,.T3
		
	move.l	Window1,a1		; scoll it
	move.l	wd_RPort(a1),a1
	moveq.l	#0,d0	
	moveq.l	#-8,d1
	move.l	#55,d2
	move.l	#30,d3
	move.l	#160+55,d4
	move.l	4(sp),d5
	addq.l	#1,d5
	lsl.l	#3,d5
	add.l	#30,d5
	CALLGRAF	ScrollRaster

	SETAPEN	#0			
	RECTFILL	#55,#30,#160+55,#30+8
.NoMove	rts


GameOverA	SETAPEN	#1
	RECTFILL	#55+16-2,#30+65-1,#55+160-16+2,#30+65+30+1
	SETAPEN	#2
	RECTFILL	#55+16,#30+65,#55+160-16,#30+65+30
	SETDRMD	#RP_JAM1
	WRITE	#56+80-(11*4),#95+11+7,#0,<"GAME OVER !">
	WRITE	#55+80-(11*4),#95+11+6,#7,<"GAME OVER !">
	rts

********************************************************
*
*  Player 2:
*
********************************************************

DisplayB	lea.l	Pieces(pc),a0
	move.l	PieceB,d0
	lsl.l	#4,d0
	add.l	d0,a0
	move.l	(a0),a0
	move.l	8(a0),d0

MakeBIB	lea.l	20(a0),a1
	lea.l	BlockImages(pc),a2	; put up image structures
	moveq.l	#0,d1
.L0	clr.l	ig_NextImage(a2)
	move.b	d0,ig_PlaneOnOff(a2)
	move.l	d1,d2
	and.l	#3,d2
	lsl.l	#4,d2
	addq.l	#2,d2
	move.w	d2,ig_LeftEdge(a2)
	move.l	d1,d2
	lsr.w	#2,d2
	lsl.w	#3,d2
	addq.l	#2,d2
	move.w	d2,ig_TopEdge(a2)
	move.w	#16-4,ig_Width(a2)
	move.w	#8-2,ig_Height(a2)
	move.w	#3,ig_Depth(a2)
	tst.b	(a1)+
	beq	.L1
	lea.l	ig_SIZEOF(a2),a2
	move.l	a2,-4(a2)		; pointer to next image structure
.L1	addq.l	#1,d1
	cmp.b	#16,d1
	bne	.L0
	clr.l	-4(a2)		; clear last pointer 
	
	move.l	PieceB,d0		; display image
	lsl.l	#4,d0
	move.l	DegreeB,d1
	lsl.l	#3,d1
	add.l	d0,d1
	lea.l	Pieces(pc),a1
	add.l	d1,a1
	move.l	(a1),a1
	move.l	PositionBX,d0
	lsl.w	#4,d0
	add.w	#55+370,d0
	move.l	PositionBY,d1
	lsl.w	#3,d1
	add.w	#30,d1
	move.l	Window1,a0
	move.l	wd_RPort(a0),a0
	lea.l	BlockImages,a1
	CALLINT	DrawImage
	rts

RotatePieceB	move.l	PieceB,d0	; check if you can rotate it !
	lsl.l	#4,d0
	move.l	DegreeB,d1
	addq.l	#1,d1
	and.l	#3,d1
	lsl.l	#2,d1
	add.l	d0,d1
	lea.l	Pieces,a0
	add.l	d1,a0
	move.l	(a0),a0	
	move.l	12(a0),d0
	move.l	16(a0),d1
	add.l	PositionBX,d0
	add.l	PositionBY,d1
	jsr	CheckB
	tst.l	d0
	bne	.wait

	move.l	PieceB,d0		; rotate piece of player B
	lsl.l	#4,d0
	move.l	DegreeB,d1
	lsl.l	#2,d1
	add.l	d0,d1
	lea.l	Pieces,a0
	
	add.l	d1,a0
	move.l	(a0),a0
	moveq.l	#0,d0
	jsr	MakeBIB		; clear old piece
	
	add.l	#1,DegreeB
	and.l	#3,DegreeB

	move.l	PieceB,d0
	lsl.l	#4,d0
	move.l	DegreeB,d1
	lsl.l	#2,d1
	add.l	d0,d1
	lea.l	Pieces,a0
	add.l	d1,a0
	move.l	(a0),a0
	move.l	12(a0),d0
	move.l	16(a0),d1
	add.l	d0,PositionBX
	add.l	d1,PositionBY
	move.l	8(a0),d0
	jsr	MakeBIB
.wait	jmp	GameWait


MoveLeftB	move.l	PieceB,d0		
	lsl.l	#4,d0
	move.l	DegreeB,d1
	lsl.l	#2,d1
	add.l	d0,d1
	lea.l	Pieces,a0
	add.l	d1,a0
	move.l	(a0),a0
	move.l	PositionBX,d0
	subq.l	#1,d0
	move.l	PositionBY,d1
	jsr	CheckB		; check if there's any room
	tst.l	d0
	bne	.wait
	moveq.l	#0,d0
	jsr	MakeBIB		; clear old piece
	move.l	PieceB,d0
	lsl.l	#4,d0
	move.l	DegreeB,d1
	lsl.l	#2,d1
	add.l	d0,d1
	lea.l	Pieces,a0
	add.l	d1,a0
	move.l	(a0),a0
	sub.l	#1,PositionBX
	move.l	8(a0),d0
	jsr	MakeBIB
.wait	jmp	GameWait

MoveRightB	move.l	PieceB,d0		
	lsl.l	#4,d0
	move.l	DegreeB,d1
	lsl.l	#2,d1
	add.l	d0,d1
	lea.l	Pieces,a0
	add.l	d1,a0
	move.l	(a0),a0
	move.l	PositionBX,d0
	addq.l	#1,d0
	move.l	PositionBY,d1
	jsr	CheckB
	tst.l	d0
	bne	.wait
	moveq.l	#0,d0
	jsr	MakeBIB		; clear old piece
	move.l	PieceB,d0
	lsl.l	#4,d0
	move.l	DegreeB,d1
	lsl.l	#2,d1
	add.l	d0,d1
	lea.l	Pieces,a0
	add.l	d1,a0
	move.l	(a0),a0
	add.l	#1,PositionBX
	move.l	8(a0),d0
	jsr	MakeBIB
.wait	jmp	GameWait

FallB	move.l	PieceB,d0		
	lsl.l	#4,d0
	move.l	DegreeB,d1
	lsl.l	#2,d1
	add.l	d0,d1
	lea.l	Pieces,a0
	add.l	d1,a0
	move.l	(a0),a0
	move.l	PositionBX,d0
	move.l	PositionBY,d1
	addq.l	#1,d1
	jsr	CheckB
	tst.l	d0
	bne	Drop_waitB
	moveq.l	#0,d0
	jsr	MakeBIB		; clear old piece
	move.l	PieceB,d0
	lsl.l	#4,d0
	move.l	DegreeB,d1
	lsl.l	#2,d1
	add.l	d0,d1
	lea.l	Pieces,a0
	add.l	d1,a0
	move.l	(a0),a0
	add.l	#1,PositionBY
	move.l	8(a0),d0
	jsr	MakeBIB
	jmp	GameWait


DropPieceB	move.l	PieceB,d0		
	lsl.l	#4,d0
	move.l	DegreeB,d1
	lsl.l	#2,d1
	add.l	d0,d1
	lea.l	Pieces,a0
	add.l	d1,a0
	move.l	(a0),a0
	move.l	PositionBX,d0
	move.l	PositionBY,d1
	addq.l	#1,d1
	jsr	CheckB
	tst.l	d0
	bne	Drop_waitB
	add.l	#1,ScoreB
	moveq.l	#0,d0
	jsr	MakeBIB		; clear old piece
	move.l	PieceB,d0
	lsl.l	#4,d0
	move.l	DegreeB,d1
	lsl.l	#2,d1
	add.l	d0,d1
	lea.l	Pieces,a0
	add.l	d1,a0
	move.l	(a0),a0
	add.l	#1,PositionBY
	move.l	8(a0),d0
	jsr	MakeBIB
	jmp	DropPieceB

Drop_waitB	move.l	PieceB,d0		; copy piece into field
	lsl.l	#4,d0
	move.l	DegreeB,d1
	lsl.l	#2,d1
	add.l	d0,d1
	lea.l	Pieces,a0
	add.l	d1,a0
	move.l	(a0),a0
	lea.l	FieldB(pc),a1
	move.l	PositionBX,d0
	addq.l	#1,d0
	add.l	d0,a1
	move.l	PositionBY,d0
	mulu	#12,d0
	add.l	d0,a1
	lea.l	20(a0),a0
	moveq.l	#3,d2
	moveq.l	#3,d3
.E1	moveq.l	#3,d4
.E2	move.b	(a0)+,d0
	or.b	d0,(a1)+
	dbra	d4,.E2
	add.l	#12-4,a1
	dbra	d3,.E1

	jsr	ShowScores
	jsr	DeleteRowB
	jsr	NewPieceB
	tst.l	d0
	bne	GameQUITB
	jmp	GameWait
	
	


CheckB	movem.l	d1-d7/a0-a5,-(sp)	; check if there are no blocks
	lea.l	FieldB(pc),a1
	addq.l	#1,d0
	add.l	d0,a1
	mulu	#12,d1
	add.l	d1,a1
	moveq.l	#3,d2
	lea.l	.L1(pc),a2
	moveq.l	#3,d3
.E1	moveq.l	#3,d4
.E2	move.b	(a1)+,(a2)+
	dbra	d4,.E2
	add.l	#12-4,a1
	dbra	d3,.E1

	move.l	20(a0),d0
	move.l	.L1,d1
	and.l	d0,d1
	move.l	d1,d0
	tst.l	d0
	bne	.E3
	move.l	24(a0),d0
	move.l	.L2,d1
	and.l	d0,d1
	move.l	d1,d0
	tst.l	d0
	bne	.E3

	move.l	28(a0),d0
	move.l	.L3,d1
	and.l	d0,d1
	move.l	d1,d0
	tst.l	d0
	bne	.E3
	move.l	32(a0),d0
	move.l	.L4,d1
	and.l	d0,d1
	move.l	d1,d0
.E3	movem.l	(sp)+,d1-d7/a0-a5
	rts
.L1	dc.l	0
.L2	dc.l	0
.L3	dc.l	0
.L4	dc.l	0


NewPieceB	jsr	GetPiece
	move.l	d0,PieceB
	lsl.l	#4,d0
	add.l	#Pieces,d0
	move.l	d0,a0
	move.l	(a0),a0
	moveq.l	#4,d0
	move.l	d0,PositionBX
	moveq.l	#0,d1
	move.l	d1,PositionBY
	clr.l	DegreeB
	jsr	CheckB
	tst.l	d0
	bne.s	.L1
	jsr	DisplayB
	moveq.l	#0,d0
.L1	rts


DeleteRowB	moveq.l	#1,d0		; delete all posible rows / player B
.W1	move.l	d0,-(sp)
	bsr.s	.KillRowB
	move.l	(sp)+,d0
	addq.l	#1,d0
	cmp.w	#20,d0		; until row 20
	bne.s	.W1
	jmp	ShowScores		; update score

.KillRowB	lea.l	FieldB+1(pc),a0	; delete row if possible
	move.l	d0,d1
	mulu	#12,d1
	add.l	d1,a0

	move.l	#9,d1		; check if there are spaces
.T1	tst.b	(a0)+
	beq	.NoMove
	dbra	d1,.T1

.T5	add.l	#10,ScoreB
	move.l	d0,d1
	mulu	#12,d1
	lea.l	FieldB+12(pc),a1
	lea.l	FieldB(pc),a0
	add.l	d1,a0
	add.l	d1,a1
.T2	move.b	-(a0),-(a1)
	cmp.l	#FieldB,a0
	bne.s	.T2
	lea.l	FieldB+1(pc),a0
	move.l	#9,d0
.T3	move.b	#0,(a0)+
	dbra	d0,.T3
		
	move.l	Window1,a1		; scoll it
	move.l	wd_RPort(a1),a1
	moveq.l	#0,d0	
	moveq.l	#-8,d1
	move.l	#370+55,d2
	move.l	#30,d3
	move.l	#370+160+55,d4
	move.l	4(sp),d5
	addq.l	#1,d5
	lsl.l	#3,d5
	add.l	#30,d5
	CALLGRAF	ScrollRaster

	SETAPEN	#0			
	RECTFILL	#55,#30,#160+55,#30+8
.NoMove	rts


GameOverB	SETAPEN	#1
	RECTFILL	#370+55+16-2,#30+65-1,#370+55+160-16+2,#30+65+30+1
	SETAPEN	#2
	RECTFILL	#370+55+16,#30+65,#370+55+160-16,#30+65+30
	SETDRMD	#RP_JAM1
	WRITE	#370+56+80-(11*4),#95+11+7,#0,<"GAME OVER !">
	WRITE	#370+55+80-(11*4),#95+11+6,#7,<"GAME OVER !">
	rts

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





GetPiece	move.l	NextPiece,d0
	move.l	d0,d1
.L1	add.b	$bfe801,d1
	lsr.b	#1,d1
	and.l	#7,d1
	cmp.b	#7,d1
	bne.s	.L2
	add.b	$dff007,d1
	bra.s	.L1
.L2	move.l	d1,NextPiece
	move.l	d0,-(sp)
	jsr	ShowNextPiece
	move.l	(sp)+,d0
	rts

GAME_ONE	move.w	#$00ff,GameOver
	move.b	#1,Player
	bra.s	WER1
GAME_TWO	clr.w	GameOver
	move.b	#2,Player
WER1	jsr	MakeRects
	move.l	#GADGETUP!RAWKEY!INTUITICKS,d0	; modify IDCMP flags (less multitasking friendly) 
	move.l	Window1,a0
	CALLINT	ModifyIDCMP
	clr.l	DegreeA
	clr.l	DegreeB
	clr.l	ScoreA
	clr.l	ScoreB
	jsr	ShowScores		; shoe score
	jsr	NewPieceA
	tst.l	d0
	bne	GameQUITA
	
		
GameWait	move.l	Window1,a0		; wait .....
	move.l	wd_UserPort(a0),a0
	move.l	a0,a5
	CALLEXEC	WaitPort		
	move.l	a5,a0
	CALLEXEC	GetMsg		; get message
	move.l	d0,a1	
	move.l	im_Class(a1),d4	; get data
	move.w	im_Code(a1),d5
	move.w	im_Qualifier(a1),d3
	move.l	im_IAddress(a1),a4
	CALLEXEC 	ReplyMsg		; reply message
	
	moveq.l	#0,d0		
	move.b	gg_GadgetID+1(a4),d0	; get gadget ID

	cmp.l	#GADGETUP,d4
	bne.s	.J1
	tst.b	d0
	beq	GameAbout
	cmp.b	#4,d0
	beq	Game_Keys

	bra	GameQUIT2
.J1	cmp.l	#INTUITICKS,d4
	beq	GameTIMER
	
	btst	#7,d5
	beq	GameWait 
	
	tst.b	GameOver
	bne.s	.Z1

	and.l	#$7f,d5
	cmp.w	LeftA,d5		; Player A : any useful key pressed ???
	beq	MoveLeftA
	cmp.w	RightA,d5
	beq	MoveRightA
	cmp.w	RotateA,d5
	beq	RotatePieceA
	cmp.w	DropA,d5
	beq	DropPieceA

.Z1	tst.b	GameOver+1
	bne	GameWait
	and.l	#$7f,d5
	cmp.w	LeftB,d5		; Player B : any useful key pressed ???
	beq	MoveLeftB
	cmp.w	RightB,d5
	beq	MoveRightB
	cmp.w	RotateB,d5
	beq	RotatePieceB
	cmp.w	DropB,d5
	beq	DropPieceB
	jmp	GameWait


GameTIMER	add.l	#1,Timer		; check time
	and.l	#7,Timer	
	cmp.l	#3,Timer
	beq.s	.B
	tst.l	Timer
	bne	GameWait
	tst.b	GameOver
	bne	GameWait
	jmp	FallA		; drop piece A

.B	tst.b	GameOver+1
	bne	GameWait
	jmp	FallB		; drop piece B

GameQUIT2	move.l	d0,-(sp)		; user quits
	move.l	#GADGETUP,d0
	move.l	Window1,a0
	CALLINT	ModifyIDCMP
	move.l	(sp)+,d0
	jmp	GameEXIT	

GameAbout	jsr	Info
	jmp	GameWait
	
GameQUITA	jsr	GameOverA		; player one : game over
	move.b	#$ff,GameOver
	tst.b	GameOver+1		; player two also game over ?
	beq	GameWait
	cmp.b	#1,Player
	bne.s	.W1
	jsr	NewScore
.W1	move.l	#GADGETUP,d0
	move.l	Window1,a0
	CALLINT	ModifyIDCMP
	bra	GQUIT3

GameQUITB	jsr	GameOverB
	move.b	#$ff,GameOver+1
	tst.b	GameOver		; player two : game over
	beq	GameWait		; player one : also game over ?
	move.l	#GADGETUP,d0
	move.l	Window1,a0
	CALLINT	ModifyIDCMP
GQUIT3	move.l	#50,d1
	CALLDOS	Delay
	SETAPEN	#0
	RECTFILL	#55,#30,#160+55,#160+30
	SETAPEN	#0
	RECTFILL	#370+55,#30,#370+160+55,#160+30
	jmp	Inwait

ScoreText1	dc.b	"1: xxxxxxxx",0
	even
ScoreText2	dc.b	"2: xxxxxxxx",0
	even

ShowScores	lea.l	ScoreText1+3(pc),a0	; update scores
	move.l	ScoreA,d0
	jsr	MakeZahl
	lea.l	ScoreText2+3(pc),a0
	move.l	ScoreB,d0
	jsr	MakeZahl
	SETDRMD	#RP_JAM2
	SETBPEN	#0
	WRITEMEM	#320-(11*4),#20+9,#6,ScoreText1,#11
	WRITEMEM	#320-(11*4),#30+9,#7,ScoreText2,#11
	rts


MakeZahl	moveq.l	#7,d2
	moveq.l	#0,d3
	lea.l	Potenzen,a1
MZ1	move.b	#"0"-1,d1
MZ2	addq	#1,d1
	sub.l	(a1),d0
	bcc	MZ2
	add.l	(a1)+,d0
	tst.b	d2
	beq	MZ3
	cmp.b	#"0",d1
	beq	MZ4
	moveq.l	#1,d3
	bra	MZ3
MZ4	tst.b	d3
	bne	MZ3
	move.b	#" ",d1
MZ3	move.b	d1,(a0)+
	dbra	d2,MZ1
	rts
Potenzen	dc.l	10000000
	dc.l	1000000
	dc.l	100000
	dc.l	10000
	dc.l	1000
	dc.l	100
	dc.l	10
	dc.l	1

	
HiScores	dc.b	" 1.         xxxxxxx"
	dc.b	" 2.         xxxxxxx"
	dc.b	" 3.         xxxxxxx"
	dc.b	" 4.         xxxxxxx"
	dc.b	" 5.         xxxxxxx"
	dc.b	" 6.         xxxxxxx"
	dc.b	" 7.         xxxxxxx"
	dc.b	" 8.         xxxxxxx"
	dc.b	" 9.         xxxxxxx"
	dc.b	"10.         xxxxxxx"
	dc.b	"11.         xxxxxxx"
	dc.b	"12.         xxxxxxx"
	dc.b	"13.         xxxxxxx"
	dc.b	"14.         xxxxxxx"
	dc.b	"15.         xxxxxxx"
	dc.b	"16.         xxxxxxx"
	dc.b	"17.         xxxxxxx"
	dc.b	"18.         xxxxxxx"
	dc.b	"19.         xxxxxxx"
	dc.b	"20.         xxxxxxx"
	dc.b	"21.         xxxxxxx"
	dc.b	"22.         xxxxxxx"
	dc.b	"23.         xxxxxxx"
	dc.b	"24.         xxxxxxx"
	dc.b	"25.         xxxxxxx"
	dc.b	"26.         xxxxxxx"
	dc.b	"27.         xxxxxxx"
	dc.b	"28.         xxxxxxx"
	dc.b	"29.         xxxxxxx"
	dc.b	"30.         xxxxxxx"
	dc.b	"31.         xxxxxxx"
	dc.b	"32.         xxxxxxx"
	
DisplayHiScores
	SETDRMD	#RP_JAM2
	SETBPEN	#0	
	WRITEMEM	#55+4,#38+(0*10),#7,HiScores+(0*19),#19
	WRITEMEM	#55+4,#38+(1*10),#6,HiScores+(1*19),#19
	WRITEMEM	#55+4,#38+(2*10),#5,HiScores+(2*19),#19
	WRITEMEM	#55+4,#38+(3*10),#4,HiScores+(3*19),#19
	WRITEMEM	#55+4,#38+(4*10),#3,HiScores+(4*19),#19
	WRITEMEM	#55+4,#38+(5*10),#2,HiScores+(5*19),#19
	WRITEMEM	#55+4,#38+(6*10),#1,HiScores+(6*19),#19
	WRITEMEM	#55+4,#38+(7*10),#2,HiScores+(7*19),#19
	WRITEMEM	#55+4,#38+(8*10),#3,HiScores+(8*19),#19
	WRITEMEM	#55+4,#38+(9*10),#4,HiScores+(9*19),#19
	WRITEMEM	#55+4,#38+(10*10),#5,HiScores+(10*19),#19
	WRITEMEM	#55+4,#38+(11*10),#6,HiScores+(11*19),#19
	WRITEMEM	#55+4,#38+(12*10),#7,HiScores+(12*19),#19
	WRITEMEM	#55+4,#38+(13*10),#6,HiScores+(13*19),#19
	WRITEMEM	#55+4,#38+(14*10),#5,HiScores+(14*19),#19
	WRITEMEM	#55+4,#38+(15*10),#4,HiScores+(15*19),#19
	WRITEMEM	#55+4+370,#38+(0*10),#7,HiScores+(16*19),#19
	WRITEMEM	#55+4+370,#38+(1*10),#6,HiScores+(17*19),#19
	WRITEMEM	#55+4+370,#38+(2*10),#5,HiScores+(18*19),#19
	WRITEMEM	#55+4+370,#38+(3*10),#4,HiScores+(19*19),#19
	WRITEMEM	#55+4+370,#38+(4*10),#3,HiScores+(20*19),#19
	WRITEMEM	#55+4+370,#38+(5*10),#2,HiScores+(21*19),#19
	WRITEMEM	#55+4+370,#38+(6*10),#1,HiScores+(22*19),#19
	WRITEMEM	#55+4+370,#38+(7*10),#2,HiScores+(23*19),#19
	WRITEMEM	#55+4+370,#38+(8*10),#3,HiScores+(24*19),#19
	WRITEMEM	#55+4+370,#38+(9*10),#4,HiScores+(25*19),#19
	WRITEMEM	#55+4+370,#38+(10*10),#5,HiScores+(26*19),#19
	WRITEMEM	#55+4+370,#38+(11*10),#6,HiScores+(27*19),#19
	WRITEMEM	#55+4+370,#38+(12*10),#7,HiScores+(28*19),#19
	WRITEMEM	#55+4+370,#38+(13*10),#6,HiScores+(29*19),#19
	WRITEMEM	#55+4+370,#38+(14*10),#5,HiScores+(30*19),#19
	WRITEMEM	#55+4+370,#38+(15*10),#4,HiScores+(31*19),#19
	rts

	even
ScoreData	dc.b	"Roger",0,0,0
	dc.l	32
	dc.b	"Roger",0,0,0
	dc.l	31
	dc.b	"Roger",0,0,0
	dc.l	30
	dc.b	"Roger",0,0,0
	dc.l	29
	dc.b	"Roger",0,0,0
	dc.l	28
	dc.b	"Roger",0,0,0
	dc.l	27
	dc.b	"Roger",0,0,0
	dc.l	26
	dc.b	"Roger",0,0,0
	dc.l	25
	dc.b	"Roger",0,0,0
	dc.l	24
	dc.b	"Roger",0,0,0
	dc.l	23
	dc.b	"Roger",0,0,0
	dc.l	22
	dc.b	"Roger",0,0,0
	dc.l	21
	dc.b	"Roger",0,0,0
	dc.l	20
	dc.b	"Roger",0,0,0
	dc.l	19
	dc.b	"Roger",0,0,0
	dc.l	18
	dc.b	"Roger",0,0,0
	dc.l	17
	dc.b	"Roger",0,0,0
	dc.l	16
	dc.b	"Roger",0,0,0
	dc.l	15
	dc.b	"Roger",0,0,0
	dc.l	14
	dc.b	"Roger",0,0,0
	dc.l	13
	dc.b	"Roger",0,0,0
	dc.l	12
	dc.b	"Roger",0,0,0
	dc.l	11
	dc.b	"Roger",0,0,0
	dc.l	10
	dc.b	"Roger",0,0,0
	dc.l	9
	dc.b	"Roger",0,0,0
	dc.l	8
	dc.b	"Roger",0,0,0
	dc.l	7
	dc.b	"Roger",0,0,0
	dc.l	6
	dc.b	"Roger",0,0,0
	dc.l	5
	dc.b	"Roger",0,0,0
	dc.l	4
	dc.b	"Roger",0,0,0
	dc.l	3
	dc.b	"Roger",0,0,0
	dc.l	2
	dc.b	"Roger",0,0,0
	dc.l	1
	dc.b	"Roger",0,0,0
	dc.l	0


MakeScores	lea.l	HiScores+3(pc),a3
	lea.l	ScoreData(pc),a4
	move.l	#31,d5
.F1	move.l	a3,a0
	moveq.l	#7,d0
.F2	move.b	#" ",(a0)+
	dbra	d0,.F2
	moveq.l	#7,d0
	move.l	a3,a5
	move.l	a4,a6
.F3	move.b	(a4)+,d1
	tst.b	d1
	beq.s	.F4
	move.b	d1,(a3)+
	dbra	d0,.F3
.F4	lea.l	8(a5),a3
	lea.l	8(a6),a4
	move.l	(a4)+,d0
	move.l	a3,a0
	jsr	MakeZahl
	lea.l	19(a5),a3
	dbra	d5,.F1
	rts


Windowdef3	dc.w	180,55,300,90
	dc.b	1,1
	dc.l	GADGETUP
	dc.l	ACTIVATE!SMART_REFRESH!SMART_REFRESH
	dc.l	W3_G1
	dc.l	0
	dc.l	0
ScreenPtr3	dc.l	0,0
	dc.w	0,0,0,0,CUSTOMSCREEN

W3_G1	GADGET	W3_G2,100,70,100,0,<"OK">,1,4
W3_G2	dc.l	0
	dc.w	(300-72)/2,40
	dc.w	72,10
	dc.w	GADGHCOMP,RELVERIFY!STRINGCENTER,STRGADGET
	dc.l	0,0,0,0,W3_G2_Info
	dc.l	0,0
W3_G2_Info	dc.l	Name
	dc.l	UndoName
	dc.w	0,9
	ds.b	si_SIZEOF
Name	ds.b	12
UndoName	ds.b	12


WindowPointer set Window2

NewScore	move.l	ScoreData+(31*12)+8,d0	; is 32nd score lower than your score ?
	cmp.l	ScoreA,d0
	bhi	.Y
	
	

	move.l	ScreenPtr1,ScreenPtr3
	lea.l	Windowdef3(pc),a0
	CALLINT	OpenWindow
	move.l	d0,Window2
	tst.l	d0
.Y	bne	.X
	rts
	
.X	SETAPEN	#2
	RECTFILL	#2,#1,#297,#88

	lea.l	W3_G1(pc),a0
	move.l	WindowPointer,a1
	sub.l	a2,a2
	CALLINT	RefreshGadgets	
	lea.l	W3_G2(pc),a0
	move.l	WindowPointer,a1
	sub.l	a2,a2
	CALLINT	ActivateGadget
	
	SETDRMD	#RP_JAM1
	
	WRITE	#((300-(11*8))/2)+1,#11,#0,<"Well done !">
	WRITE	#(300-(11*8))/2,#10,#1,<"Well done !">
	WRITE	#((300-(16*8))/2)+1,#26,#0,<"Enter your name :">
	WRITE	#(300-(16*8))/2,#25,#7,<"Enter your name :">
	
	move.l	Window2,a0		; wait .....
	move.l	wd_UserPort(a0),a0
	CALLEXEC	WaitPort		
	move.l	Window2,a0
	CALLINT	CloseWindow

	lea.l	ScoreData+(31*12)(pc),a0
	lea.l	Name(pc),a1
	moveq.l	#7,d0
.W1	move.b	(a1)+,(a0)+
	dbra	d0,.W1
	move.l	ScoreA,(a0)


	lea.l	.Struktur(pc),a0
	movem.l	d1-d7/a0-a2,-(sp)
	move.l	a0,a2
	move.l	6(a2),d5	
	subq.l	#1,d5
.label1	moveq.l	#0,d7	
	moveq.l	#0,d6	
.label2	move.l	d6,d0
	move.l	d0,d1
	addq.l	#1,d1
	bsr	.Vergleiche
	bls	.label3
	move.l	d6,d0
	move.l	d0,d1
	addq.l	#1,d1
	moveq.l	#1,d7
	bsr	.Vertausche
	move.l	d6,d4	
.label3	addq	#1,d6
	cmp.l	d6,d5
	bhi	.label2
	move.l	d4,d5
	tst.b	d7
	bne	.label1	
	move.l	(a2),d0
	movem.l	(sp)+,d1-d7/a0-a2
	jsr	SaveHiScores
	jmp	MakeScores



.Vergleiche	mulu	4(a2),d0
	mulu	4(a2),d1
	add.l	(a2),d0
	add.l	(a2),d1
	move.l	d0,a0
	move.l	d1,a1
	lea.l	8(a0),a0
	lea.l	8(a1),a1
	move.w	4(a2),d2
.label4	cmpm.l	(a0)+,(a1)+
	rts


.Vertausche	mulu	4(a2),d0
	mulu	4(a2),d1
	add.l	(a2),d0
	add.l	(a2),d1
	move.l	d0,a0
	move.l	d1,a1
	move.w	4(a2),d0
	subq.l	#1,d0
	
.label9	move.b	(a0),d1
	move.b	(a1),(a0)+
	move.b	d1,(a1)+
	dbra	d0,.label9
	rts
		
		

.Struktur	dc.l	ScoreData
	dc.w	8+4
	dc.l	32

	
WindowPointer set Window1
	even
ScoreName	dc.b	"T90.HiScores",0


LoadHiScores	move.l	#ScoreName,d1
	move.l	#MODE_OLDFILE,d2
	CALLDOS	Open
	tst.l	d0
	beq	.E
	move.l	d0,d6
	move.l	d0,d1
	move.l	#ScoreData,d2
	move.l	#32*12,d3
	CALLDOS	Read
	move.l	d6,d1
	CALLDOS	Close
	lea.l	ScoreData(pc),a0
	move.l	#(32*12)-1,d0
.Q	eor.b	#$fa,(a0)+
	dbra	d0,.Q
.E	rts
			
	
SaveHiScores	lea.l	ScoreData(pc),a0
	move.l	#(32*12)-1,d0
.Q1	eor.b	#$fa,(a0)+
	dbra	d0,.Q1
	move.l	#ScoreName,d1
	move.l	#MODE_NEWFILE,d2
	CALLDOS	Open
	tst.l	d0
	beq	.E
	move.l	d0,d6
	move.l	d0,d1
	move.l	#ScoreData,d2
	move.l	#32*12,d3
	CALLDOS	Write
	move.l	d6,d1
	CALLDOS	Close
.E	lea.l	ScoreData(pc),a0
	move.l	#(32*12)-1,d0
.Q2	eor.b	#$fa,(a0)+
	dbra	d0,.Q2
	rts

KeyName	dc.b	"T90.Keys",0
	even
SaveKeys	move.l	#KeyName,d1		; save key map
	move.l	#MODE_NEWFILE,d2
	CALLDOS	Open
	tst.l	d0
	beq	.E
	move.l	d0,d6
	move.l	d0,d1
	move.l	#LeftA,d2
	move.l	#4*2*2,d3
	CALLDOS	Write
	move.l	d6,d1
	CALLDOS	Close
.E	rts

LoadKeys	move.l	#KeyName,d1		; load key map
	move.l	#MODE_OLDFILE,d2
	CALLDOS	Open
	tst.l	d0
	beq	.E
	move.l	d0,d6
	move.l	d0,d1
	move.l	#LeftA,d2
	move.l	#4*2*2,d3
	CALLDOS	Read
	move.l	d6,d1
	CALLDOS	Close
.E	rts

Windowdef4	dc.w	180,55-20,300,90+40
	dc.b	1,1
	dc.l	RAWKEY
	dc.l	ACTIVATE!SMART_REFRESH!SMART_REFRESH
	dc.l	0
	dc.l	0
	dc.l	0
ScreenPtr4	dc.l	0,0
	dc.w	0,0,0,0,CUSTOMSCREEN

WindowPointer set Window2



DEFINE_KEYS	bsr.s	SetKeys
	jmp	wait
Game_Keys	bsr.s	SetKeys
	jmp	GameWait


SetKeys	lea.l	LeftA(pc),a0
	moveq.l	#7,d0
.P2	clr.w	(a0)+
	dbra	d0,.P2
	move.l	ScreenPtr1,ScreenPtr4
	lea.l	Windowdef4(pc),a0
	CALLINT	OpenWindow
	move.l	d0,Window2
	tst.l	d0
	bne	.X
	rts
.X	SETAPEN	#2
	RECTFILL	#2,#1,#297,#88+40

	SETDRMD	#RP_JAM1
	WRITE	#(300-(6*8))/2,#12+1,#0,<"Keys :">	
	WRITE	#(300-(6*8))/2,#12,#6,<"Keys :">	

	WRITE	#(300-(24*8))/2,#10+20,#1,<"Player One : Left   :">	
	jsr	.WAIT
	move.w	d5,LeftA
	WRITEMEM	#(300-(24*8))/2+25*8,#10+20,#0,Puffer,#2
	WRITE	#(300-(24*8))/2,#20+20,#1,<"             Right  :">	
	jsr	.WAIT
	move.w	d5,RightA
	WRITEMEM	#(300-(24*8))/2+25*8,#20+20,#0,Puffer,#2
	WRITE	#(300-(24*8))/2,#30+20,#1,<"             Rotate :">	
	jsr	.WAIT
	move.w	d5,RotateA
	WRITEMEM	#(300-(24*8))/2+25*8,#30+20,#0,Puffer,#2
	WRITE	#(300-(24*8))/2,#40+20,#1,<"             Drop   :">	
	jsr	.WAIT
	move.w	d5,DropA
	WRITEMEM	#(300-(24*8))/2+25*8,#40+20,#0,Puffer,#2
	WRITE	#(300-(24*8))/2,#50+20,#7,<"Player Two : Left   :">	
	jsr	.WAIT
	move.w	d5,LeftB
	WRITEMEM	#(300-(24*8))/2+25*8,#50+20,#0,Puffer,#2
	WRITE	#(300-(24*8))/2,#60+20,#7,<"             Right  :">	
	jsr	.WAIT
	move.w	d5,RightB
	WRITEMEM	#(300-(24*8))/2+25*8,#60+20,#0,Puffer,#2
	WRITE	#(300-(24*8))/2,#70+20,#7,<"             Rotate :">	
	jsr	.WAIT
	move.w	d5,RotateB
	WRITEMEM	#(300-(24*8))/2+25*8,#70+20,#0,Puffer,#2
	WRITE	#(300-(24*8))/2,#80+20,#7,<"             Drop   :">	
	jsr	.WAIT
	move.w	d5,DropB
	WRITEMEM	#(300-(24*8))/2+25*8,#80+20,#0,Puffer,#2

;
; save key map
;

	WRITE	#(300-(12*8))/2,#100+20,#1,<"Save (y/n) :">	
	
	move.l	Window2,a0		; no more RAWKEY ...
	move.l	#VANILLAKEY,d0
	CALLINT	ModifyIDCMP
	move.l	Window2,a0		; wait 
	move.l	wd_UserPort(a0),a0
	move.l	a0,a5
	CALLEXEC	WaitPort		
	move.l	a5,a0
	CALLEXEC	GetMsg		
	move.l	d0,a1	
	move.w	im_Code(a1),d5
	CALLEXEC 	ReplyMsg
	cmp.b	#"y",d5		; yes ?
	bne.s	.Close
		
	jsr	SaveKeys

.Close	move.l	Window2,a0
	CALLINT	CloseWindow
	rts
.Error	move.l	ScreenPtr1,a0
	CALLINT	DisplayBeep
.WAIT	move.l	Window2,a0		; wait .....
	move.l	wd_UserPort(a0),a0
	move.l	a0,a5
	CALLEXEC	WaitPort		
	move.l	a5,a0
	CALLEXEC	GetMsg		; get message
	move.l	d0,a1	
	CALLEXEC 	ReplyMsg	
	move.l	Window2,a0		; wait until you release key !
	move.l	wd_UserPort(a0),a0
	move.l	a0,a5
	CALLEXEC	WaitPort		
	move.l	a5,a0
	CALLEXEC	GetMsg		
	move.l	d0,a1	
	move.w	im_Code(a1),d5
	CALLEXEC 	ReplyMsg
	and.l	#$7f,d5		; key already defined ?
	lea.l	LeftA(pc),a0
	moveq.l	#7,d0
.P1	cmp.w	(a0)+,d5
	beq	.Error	
	dbra	d0,.P1
	lea.l	.Hex(pc),a0
	move.l	d5,d0
	lsr.w	#4,d0
	and.l	#$f,d0
	move.b	(a0,d0),Puffer
	move.l	d5,d0
	and.l	#$f,d0
	move.b	(a0,d0),Puffer+1
	rts
.Hex	dc.b	"0123456789ABCDEF"
Puffer	ds.b	2
	


