; Push - Ein Public-Domain-Programm von
; 	    Claus Brunzema
;         Gräfin-Theda-Str. 2
;            2970 Emden
;
execbase = 4
OpenLib = -408
CloseLib = -414
FindTask = -294
OpenWindow = -204
CloseWindow = -72
SetMenuStrip = -264
ClearMenuStrip = -54
SetRast = -234
DrawBorder = -108
PrintIText = -216
WaitPort = -384
GetMsg = -372
ReplyMsg = -378
ScrollRaster = -396
AutoRequest = -348
;
s:
	move.l	#0,a1
	move.l	execbase,a6
	jsr	FindTask(a6)
	move.l	d0,a0
	tst.l	$ac(a0)			;CLI ?
	bne.s	init			;ja!
	lea	$5c(a0),a0		;prgmsgport
	move.l	a0,-(sp)
	jsr	WaitPort(a6)
	move.l	(sp)+,a0
	jsr	GetMsg(a6)		;WBStartup-Msg abholen
init:
	lea	intname,a1
	jsr	OpenLib(a6)
	move.l	d0,intbase
	lea	gfxname,a1
	jsr	OpenLib(a6)
	move.l	d0,gfxbase
	lea	winstruct,a0
	move.l	intbase,a6
	jsr	OpenWindow(a6)
	move.l	d0,windowhd
	move.l	d0,a0
	move.l	50(a0),rastadr
	lea	menu,a1
	jsr	SetMenuStrip(a6)
initgame:
	move.l	rastadr,a1
	moveq	#0,d0
	move.l	gfxbase,a6
	jsr	SetRast(a6)		;Window löschen
	move.l	#$ffffffff,map		;Rand belegen
	move.w	#$ffff,map+4
	lea	map+6,a0
	moveq	#1,d3
	moveq	#4,d1
	moveq	#1,d0
xloop:
	sub.l	d7,d7			;x-Flag löschen
	move.b	d0,(a0)+
	abcd	d3,d0			;d0=d0+1  (BCD !!)
	dbra	d1,xloop
;
	move.b	#$ff,(a0)+
	moveq	#4,d1
	cmp.b	#$26,d0			;Ende ?  (BCD !!)
	blo.s	xloop
	move.b	#0,-2(a0)		;leeres Feld
	move.l	#$ffffffff,(a0)		;Rand
	move.w	#$ffff,4(a0)
;
	move.l	#10000,d1
	lea	map+34,a0		;Zeiger auf die 0
mixloop:
	move.b	$bfd800,d0		;Zufallszahl
	andi.b	#%00000011,d0		;nur von 0-3 als Richtung für
;					;das Mischen	
	tst.b	d0
	bne.s 	right
	cmpi.b	#$ff,-6(a0)
	beq.s	mixend
	move.b	-6(a0),(a0)
	suba	#6,a0
	move.b	#0,(a0)
	bra.s	mixend
right:
	cmpi.b	#1,d0
	bne.s 	down
	cmpi.b	#$ff,1(a0)
	beq.s	mixend
	move.b	1(a0),(a0)
	adda	#1,a0
	move.b	#0,(a0)
	bra.s	mixend
down:
	cmpi.b	#2,d0
	bne.s	left
	cmpi.b	#$ff,6(a0)
	beq.s	mixend
	move.b	6(a0),(a0)
	adda	#6,a0
	move.b	#0,(a0)
	bra.s	mixend
left:
	cmpi.b	#$ff,-1(a0)
	beq.s 	mixend
	move.b	-1(a0),(a0)
	suba	#1,a0
	move.b	#0,(a0)
	bra.s	mixend	
mixend:	dbra	d1,mixloop
;
	moveq	#0,d0			;xstart
	moveq	#0,d1			;ystart der Teile
	lea	map+6,a0
;
pieceloop:
	movem.l	d0-d1/a0,-(sp)
	move.b	(a0),d6
	beq.l	empty
	move.l	rastadr,a0
	lea	border,a1
	move.l	intbase,a6
	jsr	DrawBorder(a6)		;Teil zeichnen
	movem.l	(sp)+,d0-d1/a0
	moveq	#0,d3
	moveq	#0,d4
	move.b	(a0),d3
	move.b	d3,d4
	andi.b	#%11110000,d3
	beq.s	onechar
	asr	#4,d3
	addi.b	#48,d3
	move.b	d3,char
	andi.b	#%00001111,d4
	addi.b	#48,d4
	move.b	d4,char+1
	moveq	#8,d5			;x-offset
	bra.s	numbout
onechar:
	addi.b	#48,d4
	move.b	d4,char
	move.b	#0,char+1
	moveq	#4,d5			;x-offset
numbout:
	movem.l	d0-d1/a0,-(sp)
	addi.l	#6,d1
	addi.l	#20,d0
	sub.l	d5,d0
	move.l	rastadr,a0
	lea	text,a1
	move.l	intbase,a6
	jsr	PrintIText(a6)		;Nummer des Teils eindrucken
empty:
	movem.l	(sp)+,d0-d1/a0
	addi.l	#1,a0
	addi.l	#40,d0
	cmpi.l	#200,d0
	blo.l	pieceloop
	addi.l	#1,a0			;$ff (Randbegrenzung) überspr.
	moveq	#0,d0
	addi.l	#20,d1
	cmpi.l	#100,d1
	blo.l	pieceloop
;
gameloop:
	jsr	inputs
	cmpi.w	#$ffff,d1
	beq.s	gameloop		;nicht richtig geklickt
;
newgame:
	cmpi.w	#$f800,d1
	beq.l	initgame
;
abouttest:
	cmpi.w	#$f820,d1
	bne.s	exittest
	move.l	windowhd,a0
	lea	abouttxt,a1
	lea	knobtxt,a2
	lea	knobtxt,a3
	moveq	#0,d0
	moveq	#0,d1
	move.l	#300,d2
	moveq	#80,d3
	move.l	intbase,a6
	jsr	AutoRequest(a6)
	bra.s	gameloop
exittest:
	cmpi.w	#$f840,d1	
	beq.l	exit
;
makemove:
	move.l	windowhd,a0
	moveq	#0,d0
	move.w	108(a0),d0		;mausx
	bmi.s	gameloop
	cmpi.w	#200,d0
	bhi.s	gameloop
	divu	#40,d0
	andi.l	#$0000ffff,d0
	move.l	d0,mapptr		;x-Teil des Zeigers
	mulu	#40,d0
	move.w	d0,topedgex
	addi.w	#37,d0
	move.w	d0,botedgex
;
	moveq	#0,d0
	move.w	110(a0),d0		;mausy
	bmi.l	gameloop
	cmpi.w	#100,d0
	bhi.l	gameloop
	divu	#20,d0
	andi.l	#$0000ffff,d0
	move.l	d0,d1
	mulu	#6,d1
	add.l	d1,mapptr		;y-Teil d. Zeigers (*6)
	mulu	#20,d0
	move.w	d0,topedgey
	addi.w	#18,d0
	move.w	d0,botedgey
;
	add.l	#map+6,mapptr
;
	move.l	mapptr,a0
	move.b	(a0),d0
	beq.l	gameloop
	tst.b	-6(a0)			;testen, wo sich das leere
	beq.s	moveup			;Feld in Bezug auf das an-
	tst.b	1(a0)			;geklickte befindet
	beq.s	moveright
	tst.b	6(a0)
	beq.s	movedown
	tst.b	-1(a0)
	beq.l	moveleft
	bra.l	gameloop
;
moveup:
	move.b	(a0),-6(a0)
	move.b	#0,(a0)
	moveq	#0,d0
	moveq	#20,d1
	move.w	topedgex,d2
	move.w	topedgey,d3
	subi.w	#20,d3
	move.w	botedgex,d4
	move.w	botedgey,d5
	bra.l	doscroll
moveright:
	move.b	(a0),1(a0)
	move.b	#0,(a0)
	move.l	#-40,d0
	moveq	#0,d1
	move.w	topedgex,d2
	move.w	topedgey,d3
	move.w	botedgex,d4
	addi.w	#40,d4
	move.w	botedgey,d5
	bra.s	doscroll
movedown:
	move.b	(a0),6(a0)
	move.b	#0,(a0)
	moveq	#0,d0
	move.l	#-20,d1
	move.w	topedgex,d2
	move.w	topedgey,d3
	move.w	botedgex,d4
	move.w	botedgey,d5
	addi.w	#20,d5
	bra.s	doscroll
moveleft:
	move.b	(a0),-1(a0)
	move.b	#0,(a0)
	moveq	#40,d0
	moveq	#0,d1
	move.w	topedgex,d2
	subi.w	#40,d2
	move.w	topedgey,d3
	move.w	botedgex,d4
 	move.w	botedgey,d5
;
doscroll:
	move.l	rastadr,a1
	move.l	gfxbase,a6
	jsr	ScrollRaster(a6)	;Teil verschieben
	lea	map+6,a0
	moveq	#0,d0
	moveq	#1,d1
testloop:	;testen, ob schon fertig
	sub.l	d7,d7			;x-Flag clear
	abcd	d1,d0
	move.b	(a0)+,d4
	bpl.s	noborder		;$ff ?
	move.b	(a0)+,d4
noborder:
	cmp.b	d0,d4
	beq.s	testloop
	cmp.b	#$25,d0			;durchgekommen ?
	bne.l	gameloop
;
solved:
	move.l	rastadr,a1
	moveq	#0,d0
	move.l	gfxbase,a6
	jsr	SetRast(a6)
	move.l	rastadr,a0
	lea	solvtxt,a1
	moveq	#27,d0
	moveq	#27,d1
	move.l	intbase,a6
	jsr	PrintIText(a6)
	jsr	inputs			; 2x inputs, falls Taste 
	jsr	inputs			; festgehalten
	bra.l	initgame
;
;
inputs:
	move.l	windowhd,a0		;liefert IDCMP in d0 und
	move.l	86(a0),a0		;menu-Flags in d1
	move.l	execbase,a6
	jsr	WaitPort(a6)
	move.l	windowhd,a0
	move.l	86(a0),a0
	jsr	GetMsg(a6)
	move.l	d0,a1			;message-Adresse
	move.l	20(a1),d6		;IDCMP-Flags
	move.w	24(a1),d7		;menu
	jsr	ReplyMsg(a6)		;mess.adr. in a1
	move.l	d6,d0
	move.w	d7,d1
	btst	#9,d0			;closewindow angeklickt ?
	beq.s	noexit
	move.l	(sp)+,d0		;rts-Adresse des jsr inputs-
exit:					;Aufrufes vernichten
	move.l	windowhd,a0		;(schmutziger Trick, naja)
	move.l	intbase,a6
	jsr	ClearMenuStrip(a6)
	move.l	windowhd,a0
	jsr	CloseWindow(a6)
	move.l	gfxbase,a1
	move.l	execbase,a6
	jsr	CloseLib(a6)
	move.l	intbase,a1
	move.l	execbase,a6
	jsr	CloseLib(a6)
noexit:
	rts
;
;
;
intname:	dc.b	"intuition.library" ,0
gfxname:	dc.b	"graphics.library" ,0
even
gfxbase:	blk.l	1
intbase:	blk.l	1
windowhd:	blk.l	1
rastadr:	blk.l	1
map:		blk.b	42
mapptr:		blk.l	1
topedgex:	blk.w	1
topedgey:	blk.w	1
botedgex:	blk.w	1
botedgey:	blk.w	1
;
;
winstruct:
	dc.w	180				;xpos
	dc.w	10				;ypos
	dc.w	206				;Breite
	dc.w	112				;Höhe
	dc.b	-1				;detail_pen
	dc.b	-1				;block_pen
	dc.l	%00000000001100001000		;IDCMP
	dc.l	%0000000100001011000001110	;flags
	dc.l	0				;gadgets
	dc.l	0				;checkmark
	dc.l	wname
	dc.l	0				;screen
	dc.l	0				;bitmap
	dc.w	10				;minBreite
	dc.w	10				;minHöhe
	dc.w	600				;maxBreite
	dc.w	220				;maxHöhe
	dc.w	1				;screentype
;
wname:	dc.b 	"Push" ,0
;
;
even
menu:
	dc.l	0				;nextmenu
	dc.w	270				;xpos
	dc.w	0				;ypos
	dc.w	83				;Breite
	dc.w	10				;Höhe
	dc.w	1				;wählbar
	dc.l	menutxt
	dc.l	mitem1
	dc.w 	0,0,0,0
menutxt:
	dc.b	"Push" ,0
even
mitem1:
	dc.l	mitem2
	dc.w	0				;xpos
	dc.w	0				;ypos
	dc.w	90				;Breite
	dc.w	10				;Höhe
	dc.w	%001010010			;Flags
	dc.l	0				;Ausschluß
	dc.l	mitem1txt
	dc.l	0
	dc.b	0
	dc.b	0				;pad
	dc.l	0				;untermenue
	dc.l	0
mitem1txt:
	dc.b	0
	dc.b	1
	dc.b	0
	dc.b	0				;pad
	dc.w	0
	dc.w	1
	dc.l	0
	dc.l	m1txt
	dc.l	0
m1txt:
	dc.b	" New Game" ,0
even
mitem2:
	dc.l	mitem3
	dc.w	0				;xpos
	dc.w	10				;ypos
	dc.w	90				;Breite
	dc.w	10				;Höhe
	dc.w	%001010010			;Flags
	dc.l	0				;Ausschluß
	dc.l	mitem2txt
	dc.l	0
	dc.b	0
	dc.b	0				;pad
	dc.l	0				;untermenue
	dc.l	0
mitem2txt:
	dc.b	0
	dc.b	1
	dc.b	0
	dc.b	0				;pad
	dc.w	0
	dc.w	1
	dc.l	0
	dc.l	m2txt
	dc.l	0
m2txt:
	dc.b	" About" ,0
even
mitem3:
	dc.l	0
	dc.w	0				;xpos
	dc.w	20				;ypos
	dc.w	90				;Breite
	dc.w	10				;Höhe
	dc.w	%001010010			;Flags
	dc.l	0				;Ausschluß
	dc.l	mitem3txt
	dc.l	0
	dc.b	0
	dc.b	0				;pad
	dc.l	0				;untermenue
	dc.l	0
mitem3txt:
	dc.b	0
	dc.b	1
	dc.b	0
	dc.b	0				;pad
	dc.w	0
	dc.w	1
	dc.l	0
	dc.l	m3txt
	dc.l	0
m3txt:
	dc.b	" Quit" ,0
;
even
border:
	dc.w	0			;x-Abstand
	dc.w	0			;y-Abstand
	dc.b	1			;Zeichenfarbe
	dc.b	0			;Hintergrundfarbe
	dc.b	0			;JAM1-Modus
	dc.b	5			;5 Koordinatenpaare
	dc.l	krds			
	dc.l	border1			;kein weiterer Border
;
krds:
	dc.w	0,0
	dc.w	37,0
	dc.w	37,18
	dc.w	0,18
	dc.w	0,0
;
border1:
	dc.w	0			;x-Abstand
	dc.w	0			;y-Abstand
	dc.b	1			;Zeichenfarbe
	dc.b	0			;Hintergrundfarbe
	dc.b	0			;JAM1-Modus
	dc.b	5			;5 Koordinatenpaare
	dc.l	krds1			
	dc.l	0			;kein weiterer Border
;
krds1:
	dc.w	3,2
	dc.w	34,2
	dc.w	34,16
	dc.w	3,16
	dc.w	3,2
;
text:
	dc.b	1		;Textfarbe
	dc.b	0		;Hintergrundfarbe
	dc.b	0		;normale Darstellung
	dc.b	0		;pad
	dc.w	0		;x-Abstand
	dc.w	0		;y-Abstand
	dc.l	0		;Normfont
	dc.l	char
	dc.l	0		;kein weiterer Text
;
char:
	dc.b	"XX" ,0
;
;
;	Text für About-Requester
;
even
abouttxt:
	dc.b	2		;color
	dc.b	0		;color
	dc.b	0		;norm
	dc.b	0		;pad
	dc.w	13		;xpos
	dc.w	5		;ypos
	dc.l	0		;font
	dc.l	a1txt
	dc.l	about2txt
a1txt:
	dc.b	34,"Push",34,", a Public-Domain-Game by" ,0
even
about2txt:
	dc.b	2		;color
	dc.b	0		;color
	dc.b	0		;norm
	dc.b	0		;pad
	dc.w	80		;xpos
	dc.w	19		;ypos
	dc.l	0		;font
	dc.l	a2txt
	dc.l	about3txt
a2txt:
	dc.b	"Claus Brunzema" ,0
even
about3txt:
	dc.b	2		;color
	dc.b	0		;color
	dc.b	0		;norm
	dc.b	0		;pad
	dc.w	63		;xpos
	dc.w	32		;ypos
	dc.l	0		;font
	dc.l	a3txt
	dc.l	about4txt
a3txt:
	dc.b	"Gräfin-Theda-Str. 2" ,0
even
about4txt:
	dc.b	2		;color
	dc.b	0		;color
	dc.b	0		;norm
	dc.b	0		;pad
	dc.w	88		;xpos
	dc.w	42		;ypos
	dc.l	0		;font
	dc.l	a4txt
	dc.l	0
a4txt:
	dc.b	"D-2970 Emden" ,0
;
even
knobtxt:
	dc.b	2		;color
	dc.b	0		;color
	dc.b	0		;norm
	dc.b	0		;pad
	dc.w	5		;xpos
	dc.w	3		;ypos
	dc.l	0		;font
	dc.l	ktxt
	dc.l	0
ktxt:
	dc.b	"Ok" ,0
;
even
solvtxt:
	dc.b	1
	dc.b	0
	dc.b	0
	dc.b	0		;pad
	dc.w	10
	dc.w	10
	dc.l	0
	dc.l	solvbtxt
	dc.l	0
solvbtxt:
	dc.b	"Phantastisch !!!" ,0
