;##########################################3
;#########Timer Interrupt for Zcasino
; ver 1.0
#ifdef WithTimer
Timer_Interrupt:
	push	hl		; save hl and af
	push	af
	ld	hl,timer	; increase timer
	inc	(hl)
	pop	af
	pop	hl
	ret	
	
syncronize:			; Wait x interrupt
	ld	a,6		; Wait 6 interrupt
	ld	hl,timer	; Use my timer routine
	cp	(hl)		; 
        jr      c,too_slow	; Timer >= 6

	
synloop:
	cp	(hl)
	jr	nc,synloop	; Timer <=6
	ld	(hl),0
	ret
too_slow
	ld	(hl),0
	ret
#endif	
;0000000000000000
;Some usefullroutine
GrBufBackup:					;main identifier of grbufbak
	push	hl
        ld hl,plotsscreen                       ;hl->screen
        ld de,saferam1                          ;de->safe bak memory
        ld bc,768                               ;bc=786 bytes to copy
        ldir                                    ;LoaD Increment Repeat (bc=0)
	pop	hl
        ret                                     ;return

GrBufResBackup:                                 ;main identifier of grresbak
        push	hl
        ld hl,saferam1                          ;hl->safe bak memory
        ld de,plotsscreen                       ;de->screen
        ld bc,768                               ;bc=786 bytes to copy
        ldir                                    ;copy backup to screen
        pop	hl
        ret                                     ;return
clrgrbuf:
	ld	hl,plotsscreen
	ld	(hl),0
	ld	de,plotsscreen+1
	ld	bc,767
	ldir
	ret

;##########################
;#Vputs hl(Number)
vputshl:
        push hl
        bcall(_setxxxxop2)
        bcall(_op2toop1)
        ld a,5
        bcall(_dispop1a)
        pop hl
        ret
;Fast Tilemap Routine - By Dan Englender And Mod By paxl

Drawmap:
   ld hl, plotsscreen
   ld bc, 8*256+0
DrawmapLoop:
   ld a, (de)
   call sub_for_mapping		;soustraire l'offset qui est pour les anims
   push de
   push hl
   ld l, a
   ld h, 0
   add hl, hl
   add hl, hl
   add hl, hl
   ld de, Worldtile
   add hl, de
   ex de, hl
   pop hl
   push bc
   ld b, 8
   push hl
DrawmapLoop2:
   ld a, (de)
   ld (hl),a
   inc de
   push bc
   ld bc, 12
   add hl, bc
   pop bc
   djnz DrawmapLoop2
   pop hl
   pop bc
   pop de
   inc de
   inc hl
   inc c
   ld a, c
   cp 12
   jr nz, Drawmaploop
   ld c,0
   push de
   ld de, 7*12
   add hl, de
   pop de
   push hl
   ld	h,0
   ld	l,24d
   add hl,de
   ex de,hl
   pop hl
   djnz Drawmaploop
   ret
;###########################33
;#In = A=tilenumber(With offset)
;#out = A=tilenumber(W/o offset)
sub_for_mapping:
	cp	60h			; Personage tile (2animation)
	jr	nc,suboffA0andanim
	ret
suboffA0andanim:
	push	bc			;Save bc register
	sub	3Fh
	ld	b,a
	ld	a,(animadd_2anim)
	add	a,b
	pop	bc			;Restore him from stack
	ret
;##############################
;#In = A=Dice-1
;	  De=Position
;#out= The dice is print to the graph buffer	        
DrawDice:
	push	hl
	push	de
	ld	l,a
	ld	h,$00
	add	hl,hl
	add	hl,hl
	add	hl,hl
	add	hl,hl
	add	hl,hl
	ex	de,hl		; Skip all sprite
	ld	ix,AllDice
	add	ix,de
	pop	de
	ld	bc,$1002
	ld	a,d
	ld	l,e
	call	ilsprite
	pop	hl
	ret
		
;####################################
;######In : Nothing
;######Out: Dice interface is printed
;#####WARNING AFTER THIS ROUTINE TEXTWRITE IS ON
DiceInterface:
;	ld	hl,mise
;	ld	(hl),1
		
	ld	hl,plotsscreen
	ld	(hl),0
	ld	de,plotsscreen+1
	ld	bc,767
	ldir
	
	ld	hl,plotsscreen		;1ere ligne
	ld	(hl),$FF
	ld	de,plotsscreen+1
	ld	bc,11
	ldir
	
	ld	hl,plotsscreen+384	; 32 eme ligne
	ld	(hl),$FF
	ld	de,plotsscreen+385
	ld	bc,11
	ldir


	ld	hl,plotsscreen+756
	ld	(hl),$FF
	ld	de,plotsscreen+757
	ld	bc,11
	ldir
	
	set	7,(iy+$14)	; Set textwrite
	
		
	ld	de,$2101
	ld	hl,YourCashStr
	call	setvputs
	
	ld	de,$2A01
	ld	hl,YourBetStr
	call	setvputs

	ret
	
;########################
;#####In : Nothing
;####### Out: Mise is the mise of the player or it quit if the player fait bnkeroute
InputMise:
	ld	a,$FE					; Enter the keyloop: Probe $FE groop	
	call	directin				; MirageOs direct input routine
	cp	$F7		\	jp	z,Add1ToMise	; Add 1 to mise if up
	cp	$FE		\	jp	z,down1ToMise	; Down 1 to mise if down
	cp	$FB		\	jp	z,add100tomise
	cp	$FD		\	jp	z,down100tomise
	
	ld	a,$BF									;Probe for [MODE] or [2nd]
	call	directin								;; MirageOs direct input routine		
	cp	$DF		\	jp	z,InputMise2ndPress
	cp	$BF		\	jp	z,Gamereturn	
	jr	InputMise
add100tomise:
	ld	hl,(mise)
	ld	de,0064h
	add	hl,de
	ld	bc,(cash)
	
	call	cphlbc
	
	jr	nc,inputmise
	
	ld	(mise),hl
	jr	InputMiseReturn

down100tomise:
	ld	hl,(mise)
	ld	de,$0064
	or	a
	sbc hl,de
	ld	bc,(cash)
	
	call	cphlbc
	
	jr	z,inputmise	\	jr	nc,inputmise
	
	ld	(mise),hl
	jr	InputMiseReturn
		
Add1ToMise:
	ld	hl,(Mise)
	inc	hl
	ld	bc,(cash)	
	
	call	cphlbc
	
	jr	nc,InputMise
	
	ld	(mise),hl
	jr	InputMiseReturn
	
Down1ToMise:
	ld	hl,(Mise)
	dec	hl
	ld	bc,$0000
	
	
	call	cphlbc
	jr	z,InputMise
	

	ld	(mise),hl
	jr	InputMiseReturn

InputMiseReturn:
	ld	a,0
	ret
	
InputMise2ndPress:
	halt
	ld	a,$BF
	call	directin
	cp	$DF		\	jr	z,InputMise2ndPress
	ld	a,1
	ret	
	
;##############33
;PutMise & Cash on screen
PutMiseCashonScreen:
	ld	de,$2130
	ld	(pencol),de
	ld	hl,(cash)
	call	vputshl
	ld	bc,$0000
	call	cphlbc	\	jp	z,quittoshell		; Your a looser
	
	ld	de,$2A30
	ld	(pencol),de
	ld	hl,(mise)
	call	vputshl
	ret
;################################################
;#########Input Dice routine for chance dice
inputdice:
	call	GrBufBackup
;	xor	a
;	ld	(Dee),a
InputDiceInnerLp:
	call	GrBufResBackup
	ld	a,(Dee)
	ld	de,$320D
	call	DrawDice
	call	FastCopy
InputDiceKeyLoop:
	ld	a,$FE
	call	directin
	cp	$F7	\	jp	z,InputDiceUp1	;up key
	cp	$FE	\	jp	z,InputDiceDown1	;Down Key
	
	ld	a,$BF
	call	directin
	cp	$DF	\	jp	z,InputDiceExit
	jr	InputDiceKeyLoop
	
InputDiceUp1:
	ld	a,$FE
	ei
	halt
	call	directin
	cp	$F7
	jr	z,InputDiceUp1
	
	ld	a,(Dee)
	cp	5	\	jr	z,InputDiceKeyLoop
	
	inc	a
	ld	(Dee),a
	jr	InputDiceInnerLp

InputDiceDown1:
	ld	a,$FE
	ei
	halt
	call	directin
	cp	$FE
	jr	z,InputDiceDown1
	
	ld	a,(Dee)
	cp	0	\	jr	z,InputDiceKeyLoop
	
	dec	a
	ld	(Dee),a
	jr	InputDiceInnerLp

InputDiceExit:
	ld	a,$BF
	call	directin
	ei 
	halt
	cp	$DF
	jr	z,InputDiceExit
	ret			
;####################
;######Make a ret but -2 on stack with some delay	
Gamereturn:
	ld	b,50
	call	delayb
	pop	hl
	ret	

;###############################
; Draw a card routine
DrawcardinA:
	push	hl		; save position
	push	af		; and number
	
	ld	a,h
	ld	bc,$1302
	ld	ix,BlankcardSpr
	call	ilsprite
	
	pop	af			; Calculate Number card and sign
	ld	l,a
	ld	h,$00
	ld	a,13
	bcall(_divhlbya)		; divise
	push	af		; card number
	
	
	add	hl,hl
	add	hl,hl
	add	hl,hl
	ex	de,hl
	ld	ix,CardSingsprite
	add	ix,de
	ld	hl,2*1		; Ne pas poper
	add	hl,sp
	bcall(_ldhlind)	; vas cherche la position
	ld	a,h
	inc	l
	inc	a
	call	putsprite8
	
	pop	af
	ld	l,a
	ld	h,0
	add	hl,hl		; *2
	add	hl,hl		; *4
	add	hl,hl		; *8
	ld	ix,CardNumberSprite
	ex	de,hl
	add	ix,de
	pop	hl
	ld	a,l
	add	a,9
	ld	l,a
	ld	a,h
	inc	a
	call	putsprite8
;	call	fastcopy
	ret
	
;__________________________________________________
; Draw a card
DrawACard:
	push	bc
	push	hl
DrawAcardInternal:
	ld	b,52
	call	irandom
	push	af
	ld	a,(NbrOfCard)
	cp	40	\	call	z,ShuffleDeck
	ld	b,a
	ld	hl,DeckCard-1
	pop	af
DrawACardLoops:
	inc	hl
	cp	(hl)	\	jr	z,DrawAcardInternal
	djnz	DrawACardloops		
	ld	(hl),a
	push	af
;	ld	h,0
;	ld	l,a
;	ld	a,13
;	bcall(_divhlbya)
;	or	a		\	call	z,setaceflag
;	
	ld	hl,NbrOfCard
	inc	(hl)
;	bcall(_getkey)
	pop	af
	pop	hl
	pop	bc
	ret
	
;setaceflag:
;	ld	a,(aceflag)
;	set	0,a
;	ld	(aceflag),a
;	bcall(_getkey)
;	ret
;+______________________________________
;_________Routine pour brasser le deck

ShuffleDeck:
	ld	hl,DeckCard
	ld	(hl),$FF
	ld	de,DeckCard+1
	ld	bc,53
	ldir
	
	ld	hl,NbrOfCard
	ld	(hl),$01
	ld	a,1
	ret		





;0000000000000000000 Mirage routine ported to ion

setvputs:
	ld	(pencol),de
	bcall(_vputs)
	ret	

QuittoShell:
	ld	bc,$FFFF
	RTextWrite
	RTextInverse
QuittoshellLoop:
	pop	hl
	call	cphlbc
	ret	z
	jr	QuittoShell
	
delayb:
	ei
delaybl:
	halt
	djnz	delaybl
	ret		
	
centertext:
	ld	d,a
	xor	a
	ld	e,a
	jp	setvputs	
	
fastcopys:
	push	af
	push	bc
	push	hl
	push	de
	call	ionfastcopy
	pop	de
	pop	hl
	pop	bc
	pop	af			
	
	
directin:
	ld	b,a
	jp    Direct_Input	
	
	
	
cphlbc:
	push	hl
	or	a
	sbc	hl,bc
	pop	hl
	ret

putsprite8:
	ld	b,8
	jp	ionPutSprite	
	
	
vnewline:
	ld	de,(pencol)
	ld	e,0
	ld	a,d
	add	a,7
	ld	d,a
	ld	(pencol),de
	ret	
	


;Input:
;HL = Compress From
;DE = Compress To
;BC = Bytes to Decompress

DecompressRLE:
          ld a, (hl)
          cp $91
          jr z, DispRLERun
          ldi
DispRLEC:
          ret po
          jr DecompressRLE
DispRLERun:
          inc hl
          inc hl
          ld a, (hl)
DispRLERunL:
          dec hl
          dec a
          ldi
          jr nz, DispRLERunL
          inc hl
          jr DispRLEC	
.end	
	
	
	
	
	