	title	Das Spiel Chase
	name	('CHASE')
	include	solvdm.lib

; *************************************
; ** THE GAME OF CHASE               **
; **     --FROM DR. DOBBS JOURN      **
; **            MAY 1977             **
; **     --MODIFIED FOR SOL          **
; ** +++++++++++++++++++++++++++++++ **
; ** Angepasst an den JOYCE          **
; ** Werner Cirsovius, Maerz 2002    **
; *************************************
;
	extrn	SOLon,CONin,VDMclr

;;CUP	equ	'Y'		; Original
CUP	equ	'Z'		; Deutsche Tastatur
;
; Spielsteine
;
_rob	equ	_bel		; Roboter
_me	equ	_cid		; Spieler
;
maxmov	equ	9		; Anzahl Zuege
;
dcorn	equ	0
ecorn	equ	2

height	equ	13
width	equ	25

lowd	equ	dcorn+1
lowe	equ	ecorn+1
hid	equ	dcorn+height-1
hie	equ	lowe+width+width-4

rcorn1	equ	SOLvid+ 3*maxcol+54
rcorn2	equ	SOLvid+ 4*maxcol+56
rcorn3	equ	SOLvid+ 5*maxcol+54
rcorn4	equ	SOLvid+ 6*maxcol+56
rcorn5	equ	SOLvid+ 7*maxcol+54
ms1	equ	SOLvid+15*maxcol+12
ms2	equ	SOLvid+ 4*maxcol+16
scr	equ	SOLvid+14*maxcol+61
sre	equ	SOLvid+13*maxcol+55
msy	equ	SOLvid+14*maxcol+57
mt1	equ	SOLvid+15*maxcol+52
endm1	equ	SOLvid+ 4*maxcol+16
endm3	equ	SOLvid+ 6*maxcol+16
yscr	equ	SOLvid+ 5*maxcol+39
;
; ##############
; # Spielstart #
; ##############
;
	ld	sp,stack	; Stack initialisieren
	call	SOLon		; SOL einschalten
	call	randomize	; Zufallszahl initiieren
begin:
	xor	a
	ld	(yscore),a	; Spielstand initialisieren
	ld	(rscore),a
enter:
	ld	sp,stack	; Stack initialisieren
	call	VDMclr		; .. und das Spiel
	call	score		; Spielstand ausgeben
	ld	hl,ms2
	ld	bc,mname
	call	msgo		; Spiel anzeigen
diff:
	ld	hl,ms1
	ld	bc,mdiff
	call	msgo		; Schwierigkeitsgrad abfragen
	call	inp
	and	LoMask
	ld	(dfst),a	; .. speichern
start:
	call	VDMclr		; Neues Spiel initialisiern
	call	score		; .. sowie Spielstand
	call	setboard	; .. und Spielfeld
	call	seed		; .. Sperren aufbauen
	call	rose		; Hilfe aufbauen
	call	robot		; Stellungen der Roboter bestimmen
	call	you		; Spielstein setzen
;
; #############
; # Hauptteil #
; #############
;
ymove:
	call	near		; Test Roboter in der Naehe
ymov1:
	call	inp		; Eingabe holen
	ld	hl,(youp)	; Spielerstellung holen
	ld_hl_8	' '		; Spielstein loeschen
	cp	'J'		; Eingabe testen
	call	z,rt
	cp	'U'
	call	z,ur
	cp	CUP
	call	z,up
	cp	'T'
	call	z,ul
	cp	'G'
	call	z,lt
	cp	'B'
	call	z,ll
	cp	'N'
	call	z,dn
	cp	'M'
	call	z,lr
	cp	'P'
	call	z,panic
	cp	'H'
	jp	z,y1
	ld_a_hl
	cp	' '
	jp	nz,death
y1:
	ld_hl_8	_me		; Spielstein setzen
	ld	(youp),hl	; .. Stellung speichern
	call	rmove		; Roboter bewegen
	call	end0		; Test Ende
	jr	ymove
;
; Zeichen von Tastatur lesen
;
inp:
	call	CONin		; Zeichen lesen
	cp	cr
	jp	z,enter
	cp	esc
	jp	z,begin
	ret
;
; Spielzuege der Roboter testen
;
rmove:
	ld	hl,(rob1)	; Spielfeld laden
	call	rdead		; Test ob belegt
	jr	c,rmov1		; .. ja
	call	seek		; Neues Feld holen
	ld	(rob1),hl
rmov1:
	ld	hl,(rob2)
	call	rdead
	jr	c,rmov2
	call	seek
	ld	(rob2),hl
rmov2:
	ld	hl,(rob3)
	call	rdead
	jr	c,rmov3
	call	seek
	ld	(rob3),hl
rmov3:
	ld	hl,(rob4)
	call	rdead
	jr	c,rmov4
	call	seek
	ld	(rob4),hl
rmov4:
	ld	hl,(rob5)
	call	rdead
	ret	c
	call	seek
	ld	(rob5),hl
	ret
;
; Test ob Spielfeld belegt
; EIN	Reg HL zeigt auf Spielfeld
;	
rdead:
	ld_a_hl			; Spielstein holen
	cp	'X'		; .. und testen
	scf
	ret	z
	ccf
	ret
;
; Roboterspielstein setzen
; EIN	Reg HL zeigt auf Spielfeld
;
seek:
	ld_hl_8	' '		; Spielfeld leeren
	push	hl
	call	subh		; Werte setzen
	pop	hl
	call	dech
	push	hl
	call	subv
	pop	hl
	call	decv
chek:
	ld_a_hl			; Spielstein holen
	cp	_me		; Test ob Spieler
	jp	z,death		; .. ja, das war's
	cp	'X'		; Test Gitter
	jr	z,flash
	ld_hl_8	_rob		; Roboter setzen
	ret
flash:
	ld	d,160		; Verzoegerung setzen
flas1:
	ld_hl_8	_fls		; Blitz anzeigen
	ld	e,255		; Innere Verzoegerung setzen
flas2:
	dec	e
	jr	nz,flas2
	ld	e,255
	ld_hl_8	_fls+MSB	; Blitz mit Attribut anzeigen
flas3:
	dec	e
	jr	nz,flas3
	dec	d
	jr	nz,flas1
	ld_hl_8	'X'
	xor	a
	ret
;
; Test, ob gleiche Reihe
;
subv:
	ld	a,l		; Position holen
	and	NOT maxcol-1	; .. maskieren
	ld	l,a
	ex	de,hl
	ld	hl,(youp)	; Und fuer Spieler
	ld	a,l
	and	NOT maxcol-1
	sub	e
	ld	c,a
	ld	a,h
	sbc	a,d
	ld	b,a
	ret
;
; Test, ob gleiche Spalte
;
subh:
	ld	a,l
	and	maxcol-1
	ld	l,a
	ex	de,hl
	ld	hl,(youp)
	ld	a,l
	and	maxcol-1
	sub	e
	ret
;
; Neue Spalte ermitteln
;
dech:
	ret	z
	jp	p,rt
	jr	lt
;
; Neue Reihe ermitteln
;
decv:
	rlca
	jr	c,up
	ld	a,c
	rrca
	jr	c,up
	xor	a
	add	a,b
	jr	nz,dn
	add	a,c
	ret	z	; .. runter falls ungleich
;
; Bewegungen ueber das Spielfeld
;
; N -> runter
;
dn:
	ld	de,maxcol
	add	hl,de
	ret
;
; Y -> hoch
;
up:
	ld	de,-maxcol
	add	hl,de
	ret
;
; B -> links runter
;
ll:
	call	dn
;
; G -> links
;
lt:
	dec	hl
	dec	hl
	ret
;
; M -> rechts runter
;
lr:
	call	dn
;
; J -> rechts
;
rt:
	inc	hl
	inc	hl
	ret
;
; U -> rechts hoch
;
ur:
	call	up
	jr	rt
;
; T -> links hoch
;
ul:
	call	up
	jr	lt
;
; Das war's - Spieler tot :-(
;
death:
	ld_hl_8	_dwn		; Den Tod anzeigen
	call	msg2		; .. und ausgeben
	ld	a,(rscore)	; Roboterstand erhoehen
	inc	a
	ld	(rscore),a
	call	score		; .. und anzeigen
	call	over
	call	CONin		; Zeichen lesen
	jp	start
;
; Paniktaste betaetigt
;
panic::
	push	af
	ld	a,(dfst)	; Schwierigkeitsgrad laden
	cp	9
	jr	nz,pan3
	ld	c,7
	call	rnd
	and	c
	cp	1
	jr	nz,pan4
pan5:
	push	hl
	ld	hl,(rob1)
	ld_hl_8	_rob		; Roboter setzen
	ld	hl,(rob2)
	ld_hl_8	_rob
	ld	hl,(rob3)
	ld_hl_8	_rob
	pop	hl
	jr	pann
pan3:
	ld	a,(dfst)	; Schwierigkeitsgrad laden
	sbc	a,3
	jp	p,pan1
	ld	c,1
	jr	panok
pan1:
	ld	a,(dfst)	; Schwierigkeitsgrad laden
	sbc	a,6
	ld	c,3
	jp	p,pan2
	jr	panok
pan2:
	ld	c,7
panok:
	call	rnd
	and	c
	cp	1
	jr	z,pann
pan4:
	pop	af
	ret
pann:
	call	you
	pop	af
	pop	af
	jp	ymove
;
; Test ob Spiel vorbei ist
;
over:
	ld	a,(rscore)	; Roboterzuege laden
	cp	maxmov
	jp	z,end1
over1:
	ld	a,(yscore)	; Spielerzuege laden
	cp	maxmov
	ret	nz
endgame:
	call	CONin		; Zeichen lesen
	call	VDMclr		; Spielfeld loeschen
	ld	hl,endm1
	ld	bc,endb2
	call	msgo
	ld	a,(yscore)
	add	a,'0'
	ld	hl,yscr
	ld_hl_a
	ld	a,(rscore)
	add	a,'0'
	inc	hl
	inc	hl
	ld_hl_a
	jr	end2
end1:
	call	CONin		; Zeichen lesen
	call	VDMclr		; Spielfeld loeschen
	ld	hl,endm1
	ld	bc,endb1
	call	msgo
	ld	a,(rscore)
	add	a,'0'
	ld	hl,yscr
	ld_hl_a
	ld	a,(yscore)
	add	a,'0'
	inc	hl
	inc	hl
	ld_hl_a
end2:
	ld	hl,endm3
	ld	bc,endb3
	call	msgo
end3:
	call	CONin		; Zeichen lesen
	jp	begin
;
; Koordinaten des Spielfeldes berechnen
;
plot:
	push	de
	xor	a
	ld	h,a
	ld	l,d		; Reihe
	add	hl,hl		; * 2
	add	hl,hl		; * 4
	add	hl,hl		; * 8
	add	hl,hl		; *16
	add	hl,hl		; *32
	add	hl,hl		; *64
	ld	d,a
	add	hl,de		; Spalte einfuegen
	ld	de,SOLvid
	add	hl,de		; .. Adresse berechnen
	pop	de
	ret
;
; Test ob Koordinaten im gueltigen Bereich liegen
;
limit:
	ld	a,d		; Reihe laden
	sbc	a,lowd		; .. testen
	scf
	ret	m
	ld	a,d
	sbc	a,hid
	scf
	ret	p
	ld	a,e		; Dann fuer Spalte
	sbc	a,lowe
	scf
	ret	m
	ld	a,e
	sbc	a,hie
	scf
	ret	p
	ccf
	ret
;
; Zufallszahl berechnen
; AUS	Akku haelt Zahl
;
rnd:
	push	bc
	push	hl
	ld	hl,sh+3
	ld	b,8
rtop:
	ld	a,(hl)
	rlca
	rlca
	rlca
	xor	(hl)
	rla
	rla
	dec	hl
	dec	hl
	dec	hl
	rl	(hl)
	inc	hl
	rl	(hl)
	inc	hl
	rl	(hl)
	inc	hl
	rl	(hl)
	djnz	rtop
	ld	a,(hl)		; Resultat laden
	pop	hl
	pop	bc
	ret
;
; Zufallszahl initiieren
;
randomize:
        ld      a,r             ; Refresh Counter
        ld      (sh+3),a 	; .. fuer Zahl
	nop
	nop
	nop
        ld      a,r
        ld      (sh+1),a
	ret
;
; Test ob noch Roboter existieren
;
end0:
	ld	hl,(rob1)
	ld_a_hl
	cp	_rob		; Roboter testen
	ret	z
	ld	hl,(rob2)
	ld_a_hl
	cp	_rob
	ret	z
	ld	hl,(rob3)
	ld_a_hl
	cp	_rob
	ret	z
	ld	hl,(rob4)
	ld_a_hl
	cp	_rob
	ret	z
	ld	hl,(rob5)
	ld_a_hl
	cp	_rob
	ret	z
	jr	win		; Kein Roboter mehr
;
; Test ob Roboter in der Naehe sind
;
near:	ld	hl,(youp)
	call	rt
	call	near2
	call	up
	call	near2
	call	lt
	call	near2
	call	lt
	call	near2
	call	lt
	call	near2
	call	dn
	call	near2
	call	rt
	call	near2
	call	rt
	call	near2
	call	mclear
	ret
;
; Test ob Roboter in der Naehe
;
near2:
	ld_a_hl
	cp	_rob
	ret	nz
	call	msg1		; Vorsicht - Roboter in der Naehe
	pop	hl
	ret
;
; Hinweis: Roboter in der Naehe
;
msg1:
	push	hl
	push	bc
	ld	hl,ms1
	ld	bc,m1
	call	msgq
	pop	bc
	pop	hl
	ret
;
; Hinweis: Zerstoert
;
msg2:
	ld	hl,ms1
	ld	bc,m2
	call	msgq
	ret
;
; Gewonnen
;
win:
	ld	hl,ms1
	ld	bc,mwin
	call	msgq
	ld	a,(yscore)	; Spielstand erhoehen
	inc	a
	ld	(yscore),a
	call	score
	call	over
	call	CONin		; Zeichen lesen
	jp	start
;
; Spielstand ausgeben
;
score:
	ld	hl,sre
	ld	bc,msr
	call	msgo
	ld	a,(yscore)
	add	a,'0'
	ld	hl,scr
	ld_hl_a
	ld	a,(rscore)
	add	a,'0'
	ld	de,maxcol
	add	hl,de
	ld_hl_a
	ld	hl,msy
	ld	bc,msr1
	call	msgo
	ld	hl,mt1
	ld	bc,msr2
	call	msgo
	ret
;
; Statistikzeile loeschen
;
mclear:
	push	hl
	push	bc
	ld	hl,ms1
	ld	b,maxcol/2
mcl1:
	ld_hl_8	' '
	inc	hl
	djnz	mcl1
	pop	bc
	pop	hl
	ret
;
; Meldung ausgeben
;
msgo:
	ld	a,(bc)
	cp	eot
	ret	z
	ld_hl_a
	inc	bc
	inc	hl
	jr	msgo
;
; Meldung mit Attribut ausgeben
;
msgq:
	ld	a,(bc)
	cp	eot
	ret	z
	or	MSB
	ld_hl_a
	inc	bc
	inc	hl
	jr	msgq
;
; Spielfeld aufbauen
;
setboard:
	ld	d,dcorn
	ld	e,ecorn
top:
	ld	b,width
t1:
	call	plot
	ld_hl_8	'X'
	inc	de
	call	plot
	ld_hl_8	' '
	inc	de
	djnz	t1
rside:
	ld	b,height
	dec	e
	dec	e
r1:
	inc	d
	call	plot
	ld_hl_8	'X'
	djnz	r1
both:
	ld	b,width-1
b1:
	dec	e
	call	plot
	ld_hl_8	' '
	dec	e
	call	plot
	ld_hl_8	'X'
	djnz	b1
lside:
	ld	b,height
l1:
	dec	d
	call	plot
	ld_hl_8	'X'
	djnz	l1
	ret
;
; Sperren aufbauen
;
seed:
	ld	a,(dfst)	; Schwierigkeitsgrad laden
	rlca
	ld	b,a
	ld	a,30
	sub	b		; Anzahl der Steine berechnen
	ld	b,a
loop1:
	call	rnd
	and	maxrow-1
	ld	d,a
	call	rnd
	and	NoMSB
	ld	e,a
	call	limit
	jr	c,loop1
	call	plot
	ld	a,l
	and	even
	ld	l,a
	ld_hl_8	'X'		; Spielstein setzen
	djnz	loop1
	ret
;
; Hilfe aufbauen
;
rose:
	ld	iy,$HLP		; Adresse laden
	ld	b,Hlplen/Hlpitm	; .. Laenge
rosloop:
	push	bc
	ld	l,(iy+0)	; Videoadresse holen
	ld	h,(iy+1)
	ld	c,(iy+2)	; .. und Meldung
	ld	b,(iy+3)
	call	msgo		; Meldung ausgeben
	ld	bc,Hlpitm
	add	iy,bc
	pop	bc
	djnz	rosloop
	ret
;
$HLP:
	dw	rcorn1,help1
Hlpitm	equ	$-$HLP
	dw	rcorn2,help2
	dw	rcorn3,help3
	dw	rcorn4,help4
	dw	rcorn5,help5
Hlplen	equ	$-$HLP
;
; Stellung der Roboter bestimmen
;
robot:
	call	sel
	ld	(rob1),hl
	call	sel
	ld	(rob2),hl
	call	sel
	ld	(rob3),hl
	call	sel
	ld	(rob4),hl
	call	sel
	ld	(rob5),hl
	ret
;
; Eigenen Spielstein setzen
;
you:
	call	selmove		; Freies Spielfeld suchen
	ld_hl_8	_me		; .. eigenen Stein setzen
	ld	(youp),hl
	ret
;
; Spielfeld selektieren
;
sel:
	call	selmove		; Freies Spielfeld suchen
	ld_hl_8	_rob		; .. Roboter setzen
	dec	b
	ret	nz
	jp	ymove
;
; Freies Spielfeld suchen
;
selmove:
	call	rnd
	and	MaxRow-1
	ld	d,a
	call	rnd
	and	even
	ld	e,a
	call	limit
	jr	c,selmove
	call	plot
	ld_a_hl
	cp	' '
	jr	nz,selmove
	ret
;
; Meldungen
;
mname:
	db	'* * *  Das Spiel CHASE  * * *'
	db	eot
m1:
	db	'  Vorsicht - Roboter!  '
	db	eot
m2:
	db	'    Du bist vernichtet!   '
	db	eot
mwin:
	db	' Gratulation - Du hast gewonnen! '
	db	eot
msr:
	db	'--Stand--'
	db	eot
msr1:
	db	'Du:'
	db	eot
msr2:
	db	'Roboter:'
	db	eot
endb1:
	db	'Die Roboter haben gewonnen - :'
	db	eot
endb2:
	db	'Du hast sie geschlagen -     :'
	db	eot
endb3:
	db	'Weiter mit beliebiger Taste...'
	db	eot
mdiff:
	db	' Schwierigkeitsgrad (0-9) ?'
	db	eot
help1:
	db	'T   ',CUP,'   U'
	db	eot
help2:
	db	'\ ',_aru,' /'
	db	eot
help3:
	db	'G ',_arl,' H ',_arr,' J'
	db	eot
help4:
	db	'/ ',_ard,' \'
	db	eot
help5:
	db	'B   N   M'
	db	eot
;
sh:
	dw	1234h
	dw	5678h
dfst:
	db	1
rscore:
	ds	1
yscore:
	ds	1
youp:
	ds	2
rob1:
	ds	2
rob2:
	ds	2
rob3:
	ds	2
rob4:
	ds	2
rob5:
	ds	2
;
	ds	2*32
stack	equ	$

	end	


