Login

Subversion Repositories NedoOS

Rev

Rev 1893 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

я╗┐        DEVICE ZXSPECTRUM128
        include "../../_sdk/sys_h.asm"

scrbase=0x4000
prarr_scrbase=0x8000
sprmaxwid=32
sprmaxhgt=32
scrwid=160 ;double pixels
scrwidpix=scrwid*2
scrhgt=200

STACK=0x3ff0
tempsp=0x3f06 ;6 bytes for prspr
INTSTACK=0x3f00

nofocuskey=0xff

NPIECES=20*12
PIECESZ=8

SAVING=1;0

        org PROGSTART
begin
        jp GO ;patched by prspr
GO
        ld sp,STACK
        OS_HIDEFROMPARENT
        ld e,0+0x80 ;EGA
        OS_SETGFX ;e=0:EGA, e=2:MC, e=3:6912, e=6:text ;+SET FOCUS ;e=-1: disable gfx (out: e=old gfxmode) +8=noturbo, +0x80=keep gfx pages
        ld de,emptypal
        OS_SETPAL ;╨▓╨║╨╗╤О╤З╨░╨╡╨╝ ╤З╤С╤А╨╜╤Г╤О ╨┐╨░╨╗╨╕╤В╤А╤Г, ╤З╤В╨╛╨▒╤Л ╨▒╤Л╨╗╨╛ ╨╜╨╡╨╖╨░╨╝╨╡╤В╨╜╨╛ ╨┐╨╡╤А╨╡╨▒╤А╨╛╤Б╨║╤Г ╤Н╨║╤А╨░╨╜╨░
        ld e,0
        OS_CLS ;╨╛╤З╨╕╤Б╤В╨╕╨╗╨╕ ╤В╨╡╨║╤Г╤Й╨╕╨╣ ╤Н╨║╤А╨░╨╜

        OS_GETMAINPAGES
;dehl=pages in 0000,4000,8000,c000
        ld a,e
        ld (pgmain4000),a
        ld a,h
        ld (pgmain8000),a
        ld a,l
        ld (pgmainc000),a

        ld a,r
        ld (rndseed1),a
        OS_GETTIMER ;dehl=timer
        ld (rndseed2),hl
         ld (oldupdtimer),hl

        ld de,path
        OS_CHDIR
       
         if SAVING
        ld de,filename
        OS_OPENHANDLE
        ;ld a,-1
        or a
        jr nz,noloadini
        push bc
        ld de,SAVEDATA
        ld hl,SAVEDATAsz
        OS_READHANDLE
        pop bc
        OS_CLOSEHANDLE
        jr loadiniq
noloadini
        xor a
        ld (level),a
        call genxy
loadiniq
         else
        call genxy
         endif

        OS_NEWPAGE ;╨╖╨░╨║╨░╨╖╨░╨╗╨╕ ╨╜╨╛╨▓╤Г╤О ╤Б╤В╤А╨░╨╜╨╕╤Ж╤Г, ╤З╤В╨╛╨▒╤Л ╨│╤А╤Г╨╖╨╕╤В╤М ╤В╤Г╨┤╨░ ╨┤╨░╨╜╨╜╤Л╨╡
        ld a,e
        ld (pggriddata0),a ;╨╖╨░╨┐╨╛╨╝╨╜╨╕╨╗╨╕ ╨╡╤С ╨╜╨╛╨╝╨╡╤А
        OS_NEWPAGE ;╨╖╨░╨║╨░╨╖╨░╨╗╨╕ ╨┤╤А╤Г╨│╤Г╤О ╨╜╨╛╨▓╤Г╤О ╤Б╤В╤А╨░╨╜╨╕╤Ж╤Г, ╤З╤В╨╛╨▒╤Л ╨│╤А╤Г╨╖╨╕╤В╤М ╤В╤Г╨┤╨░ ╨┤╨░╨╜╨╜╤Л╨╡
        ld a,e
        ld (pggriddata1),a ;╨╖╨░╨┐╨╛╨╝╨╜╨╕╨╗╨╕ ╨╡╤С ╨╜╨╛╨╝╨╡╤А

        OS_NEWPAGE ;╨╖╨░╨║╨░╨╖╨░╨╗╨╕ ╨╜╨╛╨▓╤Г╤О ╤Б╤В╤А╨░╨╜╨╕╤Ж╤Г, ╤З╤В╨╛╨▒╤Л ╨│╤А╤Г╨╖╨╕╤В╤М ╤В╤Г╨┤╨░ ╨┤╨░╨╜╨╜╤Л╨╡
        ld a,e
        ld (pgpicdata0),a ;╨╖╨░╨┐╨╛╨╝╨╜╨╕╨╗╨╕ ╨╡╤С ╨╜╨╛╨╝╨╡╤А
        OS_NEWPAGE ;╨╖╨░╨║╨░╨╖╨░╨╗╨╕ ╨┤╤А╤Г╨│╤Г╤О ╨╜╨╛╨▓╤Г╤О ╤Б╤В╤А╨░╨╜╨╕╤Ж╤Г, ╤З╤В╨╛╨▒╤Л ╨│╤А╤Г╨╖╨╕╤В╤М ╤В╤Г╨┤╨░ ╨┤╨░╨╜╨╜╤Л╨╡
        ld a,e
        ld (pgpicdata1),a ;╨╖╨░╨┐╨╛╨╝╨╜╨╕╨╗╨╕ ╨╡╤С ╨╜╨╛╨╝╨╡╤А

        OS_NEWPAGE ;╨╖╨░╨║╨░╨╖╨░╨╗╨╕ ╨╜╨╛╨▓╤Г╤О ╤Б╤В╤А╨░╨╜╨╕╤Ж╤Г, ╤З╤В╨╛╨▒╤Л ╨│╤А╤Г╨╖╨╕╤В╤М ╤В╤Г╨┤╨░ ╨┤╨░╨╜╨╜╤Л╨╡
        ld a,e
        ld (pgscrdata0),a ;╨╖╨░╨┐╨╛╨╝╨╜╨╕╨╗╨╕ ╨╡╤С ╨╜╨╛╨╝╨╡╤А
        OS_NEWPAGE ;╨╖╨░╨║╨░╨╖╨░╨╗╨╕ ╨┤╤А╤Г╨│╤Г╤О ╨╜╨╛╨▓╤Г╤О ╤Б╤В╤А╨░╨╜╨╕╤Ж╤Г, ╤З╤В╨╛╨▒╤Л ╨│╤А╤Г╨╖╨╕╤В╤М ╤В╤Г╨┤╨░ ╨┤╨░╨╜╨╜╤Л╨╡
        ld a,e
        ld (pgscrdata1),a ;╨╖╨░╨┐╨╛╨╝╨╜╨╕╨╗╨╕ ╨╡╤С ╨╜╨╛╨╝╨╡╤А

        ld a,(pggriddata0)
        ld de,filenamegrid0 ;╨╕╨╝╤П ╤Д╨░╨╣╨╗╨░
        ld hl,0xc000 ;╨║╤Г╨┤╨░ ╨│╤А╤Г╨╖╨╕╨╝
        call loadfile_in_ahl ;╨╖╨░╨│╤А╤Г╨╖╨╕╨╗╨╕ ╨╛╨┤╨╕╨╜ ╤Н╨║╤А╨░╨╜╨╜╤Л╨╣ ╤Д╨░╨╣╨╗ ╨▓ ╨╛╨┤╨╜╤Г ╤Б╤В╤А╨░╨╜╨╕╤Ж╤Г
        ld a,(pggriddata1)
        ld de,filenamegrid1 ;╨╕╨╝╤П ╤Д╨░╨╣╨╗╨░
        ld hl,0xc000 ;╨║╤Г╨┤╨░ ╨│╤А╤Г╨╖╨╕╨╝
        call loadfile_in_ahl ;╨╖╨░╨│╤А╤Г╨╖╨╕╨╗╨╕ ╨┤╤А╤Г╨│╨╛╨╣ ╤Н╨║╤А╨░╨╜╨╜╤Л╨╣ ╤Д╨░╨╣╨╗ ╨▓ ╨┤╤А╤Г╨│╤Г╤О ╤Б╤В╤А╨░╨╜╨╕╤Ж╤Г

        ld a,(pgpicdata0)
        ld de,filenamepic0 ;╨╕╨╝╤П ╤Д╨░╨╣╨╗╨░
        ld hl,0xc000 ;╨║╤Г╨┤╨░ ╨│╤А╤Г╨╖╨╕╨╝
        call loadfile_in_ahl ;╨╖╨░╨│╤А╤Г╨╖╨╕╨╗╨╕ ╨╛╨┤╨╕╨╜ ╤Н╨║╤А╨░╨╜╨╜╤Л╨╣ ╤Д╨░╨╣╨╗ ╨▓ ╨╛╨┤╨╜╤Г ╤Б╤В╤А╨░╨╜╨╕╤Ж╤Г
        ld a,(pgpicdata1)
        ld de,filenamepic1 ;╨╕╨╝╤П ╤Д╨░╨╣╨╗╨░
        ld hl,0xc000 ;╨║╤Г╨┤╨░ ╨│╤А╤Г╨╖╨╕╨╝
        call loadfile_in_ahl ;╨╖╨░╨│╤А╤Г╨╖╨╕╨╗╨╕ ╨┤╤А╤Г╨│╨╛╨╣ ╤Н╨║╤А╨░╨╜╨╜╤Л╨╣ ╤Д╨░╨╣╨╗ ╨▓ ╨┤╤А╤Г╨│╤Г╤О ╤Б╤В╤А╨░╨╜╨╕╤Ж╤Г

;╤Б╨╡╨╣╤З╨░╤Б ╤Г ╨╜╨░╤Б ╨▓╨║╨╗╤О╤З╨╡╨╜╤Л ╤Б╤В╤А╨░╨╜╨╕╤Ж╤Л ╨┐╤А╨╛╨│╤А╨░╨╝╨╝╤Л, ╨║╨░╨║ ╨▒╤Л╨╗╨╛

;pic (0x8000) & grid (0xc000) -> scr (0x4000)
        ld a,(pgpicdata0)
        SETPG8000
        ld a,(pggriddata0)
        SETPGC000
        ld a,(pgscrdata0)
        SETPG4000
        call addgridpg
        if 0
        ld hl,0x8000+8000 ;╤В╨░╨╝ ╨▓ ╨║╨░╤А╤В╨╕╨╜╨║╨╡ ╨┐╨░╨╗╨╕╤В╤А╨░ (╨┐╨╛ ╨▒╨░╨╣╤В╤Г ╨╜╨░ ╤Ж╨▓╨╡╤В)
        ld de,pal
        ld b,16
copypal0
        ld a,(hl)
        inc hl
        ld (de),a
        inc de
        ld (de),a
        inc de
        djnz copypal0 ;╤Б╨║╨╛╨┐╨╕╤А╨╛╨▓╨░╨╗╨╕ ╨┐╨░╨╗╨╕╤В╤А╤Г ╨▓ pal (╨┐╨╛ 2 ╨▒╨░╨╣╤В╨░ ╨╜╨░ ╤Ж╨▓╨╡╤В)
        endif
       
        ld a,(pgpicdata1)
        SETPG8000
        ld a,(pggriddata1)
        SETPGC000
        ld a,(pgscrdata1)
        SETPG4000
        call addgridpg

        ld ix,pieces ;x,y,pg,addr
       
        ld hl,0x4000-1-(8*40)
        push hl
        ld de,mask00-4
        call reservepiece ;hl=scr, de=mask-4, out: hl+=2 ;ix+=5
        ld b,+(20-2)/2
piecelinetop0
        push bc
        ld de,mask10-4
        call reservepiece ;hl=scr, de=mask-4, out: hl+=2 ;ix+=5
        ld de,mask20-4
        call reservepiece ;hl=scr, de=mask-4, out: hl+=2 ;ix+=5
        pop bc
        djnz piecelinetop0
        ld de,mask30-4
        call reservepiece ;hl=scr, de=mask-4, out: hl+=2 ;ix+=5
        pop hl
        ld bc,40*16
        add hl,bc

         ld b,+(12-2)/2
piecelines0
         push bc
       
        push hl
        ld de,mask01-4
        call reservepiece ;hl=scr, de=mask-4, out: hl+=2 ;ix+=5
        ld b,+(20-2)/2
piecelineodd0
        push bc
        ld de,mask11-4
        call reservepiece ;hl=scr, de=mask-4, out: hl+=2 ;ix+=5
        ld de,mask21-4
        call reservepiece ;hl=scr, de=mask-4, out: hl+=2 ;ix+=5
        pop bc
        djnz piecelineodd0
        ld de,mask31-4
        call reservepiece ;hl=scr, de=mask-4, out: hl+=2 ;ix+=5
        pop hl
        ld bc,40*16
        add hl,bc

        push hl
        ld de,mask02-4
        call reservepiece ;hl=scr, de=mask-4, out: hl+=2 ;ix+=5
        ld b,+(20-2)/2
piecelineeven0
        push bc
        ld de,mask21-4;mask12-4
        call reservepiece ;hl=scr, de=mask-4, out: hl+=2 ;ix+=5
        ld de,mask11-4;mask22-4
        call reservepiece ;hl=scr, de=mask-4, out: hl+=2 ;ix+=5
        pop bc
        djnz piecelineeven0
        ld de,mask32-4
        call reservepiece ;hl=scr, de=mask-4, out: hl+=2 ;ix+=5
        pop hl
        ld bc,40*16
        add hl,bc
       
         pop bc
         djnz piecelines0

        ld de,mask03-4
        call reservepiece ;hl=scr, de=mask-4, out: hl+=2 ;ix+=5
        ld b,+(20-2)/2
piecelinebottom0
        push bc
        ld de,mask13-4
        call reservepiece ;hl=scr, de=mask-4, out: hl+=2 ;ix+=5
        ld de,mask23-4
        call reservepiece ;hl=scr, de=mask-4, out: hl+=2 ;ix+=5
        pop bc
        djnz piecelinebottom0
        ld de,mask33-4
        call reservepiece ;hl=scr, de=mask-4, out: hl+=2 ;ix+=5

        call swapimer

;╤В╨╡╨┐╨╡╤А╤М ╨╜╨░╨┤╨╛ ╨▓╤Л╨▓╨╡╤Б╤В╨╕ ╨╖╨░╨│╤А╤Г╨╢╨╡╨╜╨╜╤Л╨╡ ╨┤╨░╨╜╨╜╤Л╨╡ ╨╜╨░ ╤Н╨║╤А╨░╨╜
        ld a,(pgscrdata0)
        SETPGC000 ;╨▓╨║╨╗╤О╤З╨╕╨╗╨╕ ╤Б╤В╤А╨░╨╜╨╕╤Ж╤Г ╤Б ╨┤╨░╨╜╨╜╤Л╨╝╨╕ ╨▓ c000
        ld a,(user_scr0_low) ;ok
        SETPG4000 ;╨▓╨║╨╗╤О╤З╨╕╨╗╨╕ ╨┐╨╛╨╗-╤Н╨║╤А╨░╨╜╨░ ╨▓ 4000
        ld hl,0xc000
        ld de,0x4000
        ld bc,0x4000
        ldir ;╨┐╨╡╤А╨╡╨▒╤А╨╛╤Б╨╕╨╗╨╕ ╨╜╨░ ╤Н╨║╤А╨░╨╜

        ld a,(pgscrdata1)
        SETPGC000 ;╨▓╨║╨╗╤О╤З╨╕╨╗╨╕ ╤Б╤В╤А╨░╨╜╨╕╤Ж╤Г ╤Б ╨┤╨░╨╜╨╜╤Л╨╝╨╕ ╨▓ c000
        ld a,(user_scr0_high) ;ok
        SETPG4000 ;╨▓╨║╨╗╤О╤З╨╕╨╗╨╕ ╨┤╤А╤Г╨│╨╕╨╡ ╨┐╨╛╨╗-╤Н╨║╤А╨░╨╜╨░ ╨▓ 4000
        ld hl,0xc000
        ld de,0x4000
        ld bc,0x4000
        ldir ;╨┐╨╡╤А╨╡╨▒╤А╨╛╤Б╨╕╨╗╨╕ ╨╜╨░ ╤Н╨║╤А╨░╨╜

        call setpgsscr40008000_current ;╨▓╨║╨╗╤О╤З╨╕╨╗╨╕ ╤Б╤В╤А╨░╨╜╨╕╤Ж╤Л ╤Н╨║╤А╨░╨╜╨░
       
        ld de,palsneg;pal;sprpal
        OS_SETPAL ;╨▓╨║╨╗╤О╤З╨╕╨╗╨╕ ╨┐╨░╨╗╨╕╤В╤А╤Г ╤Б╨┐╤А╨░╨╣╤В╨╛╨▓;╨║╨░╤А╤В╨╕╨╜╨║╨╕
       
        call setpgsmain40008000 ;╨▓╨║╨╗╤О╤З╨╕╨╗╨╕ ╤Б╤В╤А╨░╨╜╨╕╤Ж╤Г ╨┐╤А╨╛╨│╤А╨░╨╝╨╝╤Л ╨▓ 4000, ╨║╨░╨║ ╨▒╤Л╨╗╨╛
        call setpgmainc000 ;╨▓╨║╨╗╤О╤З╨╕╨╗╨╕ ╤Б╤В╤А╨░╨╜╨╕╤Ж╤Г ╨┐╤А╨╛╨│╤А╨░╨╝╨╝╤Л ╨▓ c000, ╨║╨░╨║ ╨▒╤Л╨╗╨╛

        ;call changescrpg ;╤Б ╤Н╤В╨╛╨│╨╛ ╨╝╨╛╨╝╨╡╨╜╤В╨░ (╤В╨╛╤З╨╜╨╡╨╡, ╤Б ╨┐╤А╨╡╤А╤Л╨▓╨░╨╜╨╕╤П) ╨╝╨╛╨╢╨╡╨╝ ╨▓╨╕╨┤╨╡╤В╤М, ╤З╤В╨╛ ╨╜╨░╤А╨╕╤Б╨╛╨▓╨░╨╗╨╕

        if 0
        ld a,(timer)
        ld (uvoldtimer),a
        endif
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;        
;╨│╨╗╨░╨▓╨╜╤Л╨╣ ╤Ж╨╕╨║╨╗
mainloop

        call prlevel

        ld a,(user_scr0_low) ;ok
        SETPGC000 ;╨▓╨║╨╗╤О╤З╨╕╨╗╨╕ ╤Б╤В╤А╨░╨╜╨╕╤Ж╤Г ╤Б ╨┤╨░╨╜╨╜╤Л╨╝╨╕ ╨▓ c000
        ld a,(user_scr1_low) ;ok
        SETPG4000 ;╨▓╨║╨╗╤О╤З╨╕╨╗╨╕ ╨┐╨╛╨╗-╤Н╨║╤А╨░╨╜╨░ ╨▓ 4000
        ld hl,0xc000
        ld de,0x4000
        ld bc,0x4000
        ldir ;╨┐╨╡╤А╨╡╨▒╤А╨╛╤Б╨╕╨╗╨╕ ╨╜╨░ ╤Н╨║╤А╨░╨╜

        ld a,(user_scr0_high) ;ok
        SETPGC000 ;╨▓╨║╨╗╤О╤З╨╕╨╗╨╕ ╤Б╤В╤А╨░╨╜╨╕╤Ж╤Г ╤Б ╨┤╨░╨╜╨╜╤Л╨╝╨╕ ╨▓ c000
        ld a,(user_scr1_high) ;ok
        SETPG4000 ;╨▓╨║╨╗╤О╤З╨╕╨╗╨╕ ╨┤╤А╤Г╨│╨╕╨╡ ╨┐╨╛╨╗-╤Н╨║╤А╨░╨╜╨░ ╨▓ 4000
        ld hl,0xc000
        ld de,0x4000
        ld bc,0x4000
        ldir ;╨┐╨╡╤А╨╡╨▒╤А╨╛╤Б╨╕╨╗╨╕ ╨╜╨░ ╤Н╨║╤А╨░╨╜

        ;ld e,0
        ;OS_CLS
        ;ld e,1
        ;OS_SETSCREEN
        ld a,1
        ld (curscrnum_int),a
        halt;YIELD

;╨▓╤Л╨▓╨╛╨┤ ╤Д╨╛╨╜╨░ ╨╕╨╗╨╕ ╨▓╨╛╤Б╤Б╤В╨░╨╜╨╛╨▓╨╗╨╡╨╜╨╕╨╡ ╤Д╨╛╨╜╨░ ╨┐╨╛╨┤ ╤Б╨┐╤А╨░╨╣╤В╨░╨╝╨╕
;... ╤А╨╡╨║╨╛╨╝╨╡╨╜╨┤╤Г╤О ╨▓╨╖╤П╤В╤М ╨╕╨╖ sprexamp!
restorer_cursprlist=$+1
        ld hl,sprlist1
        ld de,sprlist1^sprlist2
        ld a,h
        xor d
        ld h,a
        ld a,l
        xor e
        ld l,a
        ld (restorer_cursprlist),hl
       push hl ;curlist

;╨▓╤Л╨▓╨╛╨┤ ╤Б╨┐╤А╨░╨╣╤В╨╛╨▓
;...
        call setpgsscr40008000 ;╨▓╨║╨╗╤О╤З╨╕╨╗╨╕ ╤Б╤В╤А╨░╨╜╨╕╤Ж╤Л ╤Н╨║╤А╨░╨╜╨░
        ld hl,0x4000
        ld de,0x4001
        ld bc,0x7fff
        ld (hl),l;0
        ldir

        ;ld hl,sprlist1
       pop hl
        ld (cursprlistaddr),hl

        call drawpieces

        ;ld e,0
        ;OS_SETSCREEN
        xor a
        ld (curscrnum_int),a

        ld de,scrwid/4 + (256*scrhgt) ;d=hgt,e=wid (/8)
        ld hl,0x4000 ;hl=scr
        call copyimgega_curtodefault ;╨║╨╛╨┐╨╕╤А╤Г╨╡╨╝ ╨║╨░╤А╤В╨╕╨╜╨║╤Г ╨╜╨░ ╤Н╨║╤А╨░╨╜-╨▒╤Г╤Д╨╡╤А

        ;call endkeepspr
        ;jr $

;╨▓╨║╨╗╤О╤З╨╡╨╜╤Л ╤Б╤В╤А╨░╨╜╨╕╤Ж╤Л ╤Н╨║╤А╨░╨╜╨░

key=$+1
        ld a,0

        jr mouseloop_go
mouseloop
;1. ╨▓╤Б╤С ╨▓╤Л╨▓╨╛╨┤╨╕╨╝
;2. ╨╢╨┤╤С╨╝ ╤Б╨╛╨▒╤Л╤В╨╕╨╡
;[3. ╨▓╤Б╤С ╤Б╤В╨╕╤А╨░╨╡╨╝]
;4. ╨╛╨▒╤А╨░╨▒╨░╤В╤Л╨▓╨░╨╡╨╝ ╤Б╨╛╨▒╤Л╤В╨╕╨╡ (╨▒╨╡╨╖ ╨┐╨╡╤А╨╡╤А╨╕╤Б╨╛╨▓╨║╨╕)
;5. ╨▓╤Б╤С ╤Б╤В╨╕╤А╨░╨╡╨╝

        if 1
        ld a,(clickstate)
        or a
        jr z,mouseloop_nomove
        call ahl_coords
        cp 200-32-8
        jr c,mouseloop_nomove
         ld a,1
         ld (invalidatetime),a
       
mouseloop_nomove
        endif

        ld a,(key)
        cp nofocuskey
        call nz,prlevelifneeded

mouseloop_go
;╤Б╨╡╨╣╤З╨░╤Б ╨▓╤Б╤С ╨▓╤Л╨▓╨╡╨┤╨╡╨╜╨╛, ╨║╤А╨╛╨╝╨╡ ╤Б╤В╤А╨╡╨╗╨║╨╕
        ld a,(key)
        cp nofocuskey
        jr z,mouseloop_noprarr

        ld a,(clickstate)
        or a
        jr z,mouseloop_arr
        ld ix,(curpiece)
        ld a,hx
        or a
        jr z,mouseloop_noprarr
        call setpgsscr40008000
        call ahl_coords
        srl h
        rr l
        sub 8
        ld (ix+1),a ;y ╨░╨║╤В╨╕╨▓╨╜╨╛╨╣ ╤З╨░╤Б╤В╨╕ ╨║╤Г╤Б╨╛╤З╨║╨░
        ld a,l ;arrx2
        sub 8/2
        ld (ix+0),a ;x2 ╨░╨║╤В╨╕╨▓╨╜╨╛╨╣ ╤З╨░╤Б╤В╨╕ ╨║╤Г╤Б╨╛╤З╨║╨░
        call drawpiece
        jr mouseloop_noprarr
mouseloop_arr
        call setpgsscr8000c000
        call ahl_coords
        call shapes_memorizearr
        call ahl_coords
        call shapes_prarr8c
mouseloop_noprarr
mainloop_nothing0
        call updatetime
;╨▓ ╤Н╤В╨╛ ╨▓╤А╨╡╨╝╤П ╤Б╤В╤А╨╡╨╗╨║╨░ ╨▓╨╕╨┤╨╜╨░
        YIELD ;halt
        call control
        jr nz,mainloop_something

         ld a,(invalidatetime)
         or a
        jr z,mainloop_nothing0

mainloop_something
;╤З╤В╨╛-╤В╨╛ ╨╕╨╖╨╝╨╡╨╜╨╕╨╗╨╛╤Б╤М
        ld a,(key)
        cp nofocuskey
        jr z,mouseloop_norearr
        ;ld a,(key);(curkey)
        cp key_esc
        jp z,quit
        cp key_enter
        jp z,shuffle
       
        ld a,(clickstate)
        or a
        jr z,mouseloop_rearr
        ld ix,(curpiece)
        ld a,hx
        or a
        jr z,mouseloop_norearr
        call setpgsscr40008000
        ld a,(ix+0) ;x2 ╨░╨║╤В╨╕╨▓╨╜╨╛╨╣ ╤З╨░╤Б╤В╨╕ ╨║╤Г╤Б╨╛╤З╨║╨░
        add a,8/2 ;>=0
        sub (8/2)+(8/2)
        jr nc,$+3
        xor a
        cp scrwid-(5*4)
        jr c,$+4
        ld a,scrwid-(5*4)
        srl a
        srl a
        ld e,a
;e=x/8 >=0
        ld a,(ix+1) ;y ╨░╨║╤В╨╕╨▓╨╜╨╛╨╣ ╤З╨░╤Б╤В╨╕ ╨║╤Г╤Б╨╛╤З╨║╨░
        add a,8 ;>=0
        sub 8+8
        jr nc,$+3
        xor a
        cp scrhgt-32
        jr c,$+4
        ld a,scrhgt-32
        ld c,a
;c=y >=0
        ld b,0
        ld l,c
        ld h,b;0
        add hl,hl ;y*2
        add hl,hl ;y*4
        add hl,bc ;y*5
        add hl,hl ;y*10
        add hl,hl ;y*20
        add hl,hl ;y*40
        ld d,b;0
        add hl,de ;+x/8
        ld bc,0x4000
        add hl,bc
        ;ld hl,0x4000 ;hl=scr
        ;ld de,scrwid/4 + (256*scrhgt) ;d=hgt,e=wid (/8)
        ld de,5 + (256*32) ;d=hgt,e=wid (/8)        
        call copyimgega_defaulttocur

        jr mouseloop_norearr
mouseloop_rearr
        call setpgsscr8000c000
        ld a,(oldarry)
        ld hl,(oldarrx)
        call shapes_rearr
mouseloop_norearr
;╤Б╨╡╨╣╤З╨░╤Б ╨▓╤Б╤С ╨▓╤Л╨▓╨╡╨┤╨╡╨╜╨╛, ╨║╤А╨╛╨╝╨╡ ╤Б╤В╤А╨╡╨╗╨║╨╕

        ld a,(mousebuttons)
        and 1
top_oldmousebuttons=$+1
        ld c,1
        ld (top_oldmousebuttons),a
        xor c
        rra
        jp nc,mouseloop ;╨╜╨╡ ╨╜╨░╨╢╨░╨╗╨╕ ╨╕ ╨╜╨╡ ╨╛╤В╨╢╨░╨╗╨╕
       
        ld a,c ;old
        rra
        jp nc,mouse_unfire ;╨▒╤Л╨╗╨╛ ╨╜╨░╨╢╨░╤В╨╛ (╤В╨╡╨┐╨╡╤А╤М ╨╛╤В╨╢╨░╤В╨╛)
       
;mouse_fire
        call findpiece ;out: CY=not found, ix=curpiece
        jp c,mouseloop ;findpieceq
        ld a,1
        ld (clickstate),a
       
;╨▓╤Л╨▓╨╡╤Б╤В╨╕ ╨▓╤Б╨╡ ╨║╤Г╤Б╨╛╤З╨║╨╕, ╨║╤А╨╛╨╝╨╡ ╤В╨╡╨║╤Г╤Й╨╡╨│╨╛:
       
        jp mouse_q ;mouseloop

mouse_unfire
        xor a
        ld (clickstate),a
        ld ix,(curpiece)
        ld hl,0
        ld (curpiece),hl
        ld a,hx
        or a
        jr z,mouse_unfire_nopiece
        ld (ix+7),1 ;moved
;╨╛╨║╤А╤Г╨│╨╗╨╕╨╝ ╨║╨╛╨╛╤А╨┤╨╕╨╜╨░╤В╤Л ╤В╨╡╨║╤Г╤Й╨╡╨│╨╛ ╨║╤Г╤Б╨╛╤З╨║╨░
        ld a,(ix+0) ;x2
        add a,4/2
        and 0xfc
        ld (ix+0),a
        ld a,(ix+1) ;y
        add a,4
        and 0xf8
        ld (ix+1),a
mouse_unfire_nopiece
;╨┐╤А╨╛╨▓╨╡╤А╨║╨░ x,y = ideal x,y ╨┤╨╗╤П ╨▓╤Б╨╡╤Е ╨║╤Г╤Б╨╛╤З╨║╨╛╨▓
        ld ix,pieces
        ld b,NPIECES
checkwin0
        ld a,(ix+6) ;idealy
        cp (ix+1) ;y
        jr nz,checkwin_no
        ld a,(ix+5) ;idealx2
        cp (ix+0) ;x2
        jr nz,checkwin_no
        ld de,PIECESZ
        add ix,de
        djnz checkwin0
;╨┐╨╛╨▒╨╡╨┤╨░!
        call showwin
        YIELDGETKEYLOOP ;╨╢╨┤╤С╨╝ ╨║╨╜╨╛╨┐╨║╤Г
        call prlevel;ifneeded
        YIELDGETKEYLOOP ;╨╢╨┤╤С╨╝ ╨║╨╜╨╛╨┐╨║╤Г
        jp quit
checkwin_no

mouse_q
        call setpgsmain40008000 ;╨▓╨║╨╗╤О╤З╨╕╨╗╨╕ ╤Б╤В╤А╨░╨╜╨╕╤Ж╤Л ╨┐╤А╨╛╨│╤А╨░╨╝╨╝╤Л ╨▓ 4000,8000, ╨║╨░╨║ ╨▒╤Л╨╗╨╛
        jp mainloop
shuffle
;╤Б╤В╨░╨▓╨╕╨╝ ╤Б╨╗╤Г╤З╨░╨╣╨╜╤Л╨╡ ╨║╨╛╨╛╤А╨┤╨╕╨╜╨░╤В╤Л ╨▓╤Б╨╡╨╝ ╨║╤Г╤Б╨╛╤З╨║╨░╨╝, ╨║╨╛╤В╨╛╤А╤Л╨╡ ╨╡╤Й╤С ╨╜╨╡ ╨┤╨▓╨╕╨│╨░╨╗╨╕
        call genxy
        ld b,0
shuffle0
        push bc
        ;call swappieces ;╨╜╨╡╨╗╤М╨╖╤П ╨╝╨╡╨╜╤П╤В╤М ╨┐╨╛╤А╤П╨┤╨╛╨║! ╨╕╨╜╨░╤З╨╡ ╨╜╨╡╨┐╤А╨░╨▓╨╕╨╗╤М╨╜╨╛ ╤А╨░╨╖╤А╨╡╨╖╨░╨╡╤В╤Б╤П ╨┐╤А╨╕ ╨┐╨╡╤А╨╡╨╖╨░╨│╤А╤Г╨╖╨║╨╡!
        pop bc
        djnz shuffle0
        jp mainloop
       
quit        
        call unreservepages
       
        ;YIELDGETKEYLOOP ;╨╢╨┤╤С╨╝ ╨║╨╜╨╛╨┐╨║╤Г
        call swapimer
         if SAVING
        ld de,filename
        OS_CREATEHANDLE
        push bc
        ld de,SAVEDATA
        ld hl,SAVEDATAsz
        OS_WRITEHANDLE
        pop bc
        OS_CLOSEHANDLE
         endif
        QUIT

drawpieces
;╨▓╤Л╨▓╨╡╤Б╤В╨╕ ╨▓╤Б╨╡ ╨║╤Г╤Б╨╛╤З╨║╨╕, ╨║╤А╨╛╨╝╨╡ ╤В╨╡╨║╤Г╤Й╨╡╨│╨╛
        ld ix,pieces
        ld b,NPIECES
drawpieces0
         push bc
        push ix
        pop de
        ld hl,(curpiece)
        or a
        sbc hl,de
        ;jr z,drawpieces0_skip
         push ix
        call nz,drawpiece
         pop ix
drawpieces0_skip
         ld bc,PIECESZ
         add ix,bc
         pop bc
         djnz drawpieces0
        ret

drawpiece
        ld l,(ix+2) ;pg
        ld h,tpg/256
        ld a,(hl)
        SETPGC000
        ld a,(ix+0) ;x
        add a,sprmaxwid-1-(8/2) ;8=╤А╨░╨╖╨╝╨╡╤А ╨╗╨╡╨▓╨╛╨│╨╛ ╨┐╨╛╨╗╤П ╨║╤Г╤Б╨╛╤З╨║╨░ ╨┐╨░╨╖╨╗╨░
        ld e,a
        ld a,(ix+1) ;y
        add a,-8 ;8=╤А╨░╨╖╨╝╨╡╤А ╨▓╨╡╤А╤Е╨╜╨╡╨│╨╛ ╨┐╨╛╨╗╤П ╨║╤Г╤Б╨╛╤З╨║╨░ ╨┐╨░╨╖╨╗╨░
        ld c,a
        ld l,(ix+3)
        ld h,(ix+4)
        push hl
        pop iy
        ;ld iy,tmp
        ;ld e,60 ;e=x = -(sprmaxwid-1)..159 (╨║╨╛╨┤╨╕╤А╤Г╨╡╤В╤Б╤П ╨║╨░╨║ x+(sprmaxwid-1))
        ;ld c,60 ;c=y = -(sprmaxhgt-1)..199 (╨║╨╛╨┤╨╕╤А╤Г╨╡╤В╤Б╤П ╨║╨░╨║ ╨╡╤Б╤В╤М)
        ;call keepspr
        jp prspr

findpiece
        call ahl_coords
        srl h
        rr l ;l=arrx2
        ld h,a ;h=arry
;╨╕╤Й╨╡╨╝ ╨║╤Г╤Б╨╛╤З╨╡╨║ ╨┐╨╛╨┤ ╤Б╤В╤А╨╡╨╗╨║╨╛╨╣ (╨┐╨╛╤Б╨╗╨╡╨┤╨╜╨╕╨╣ ╨┐╨╛╨┤╤Е╨╛╨┤╤П╤Й╨╕╨╣ ╨▓ ╤Б╨┐╨╕╤Б╨║╨╡)
;╨║╨╛╨╛╤А╨┤╨╕╨╜╨░╤В╤Л ╨║╤Г╤Б╨╛╤З╨║╨░ ╤Г╨║╨░╨╖╤Л╨▓╨░╤О╤В ╨╜╨░ ╨╗╨╡╨▓╤Л╨╣ ╨▓╨╡╤А╤Е╨╜╨╕╨╣ ╤Г╨│╨╛╨╗ ╨░╨║╤В╨╕╨▓╨╜╨╛╨╣ ╤З╨░╤Б╤В╨╕
;╤Б╤В╤А╨╡╨╗╨║╨░ ╨┤╨╛╨╗╨╢╨╜╨░ ╨▒╤Л╤В╤М +0..15 ╨║ ╨╜╨╕╨╝ (╨┐╨╛ x +0..7)
        ld ix,pieces + (NPIECES*PIECESZ)
        ld b,NPIECES
findpiece0
        ld de,-PIECESZ
        add ix,de
        ld a,h ;arry
        sub (ix+1) ;y
        cp 16
        jr nc,findpiece_no
        ld a,l ;arrx2
        sub (ix+0) ;x2
        cp 8
        jr c,findpiece_ok
findpiece_no
        djnz findpiece0
        scf
        ld ix,0
        ret;jr findpieceq
findpiece_ok
        or a
        ld (curpiece),ix
        ret

ahl_coords
        ld a,(arry)
        ld hl,(arrx)
        ret

        if 0
clspg4000
        ld hl,0x4000
        ld d,h
        ld e,l
        inc e
        ld bc,0x3fff
        ld (hl),l;0
        ldir
        ret
        endif

reservepiece ;hl=scr, de=mask-4, out: hl+=2 ;ix+=5
         push hl
         push ix
        push hl ;scr
        ex de,hl ;hl=mask-4
        call getmask
        ld a,(pgscrdata0)
        SETPG4000
        ld a,(pgscrdata1)
        SETPG8000
        pop hl ;scr
        ld de,tmp-2
        call maskpiece
        ld bc,mask10-mask00 ;sprsize
        push bc
        ;jr $
        call reserve_mem
        pop bc
         pop ix
         ld (ix+2),a ;pg
         push bc
         ld c,a
         ld b,tpg/256
         ld a,(bc)
        SETPGC000
         pop bc
        push de
        ld hl,tmp-4
        ldir
        pop de
         pop hl
         inc de
         inc de
         inc de
         inc de ;╤Г╨║╨░╨╖╨░╤В╨╡╨╗╨╕ ╨╜╨░ ╤Б╨┐╤А╨░╨╣╤В╤Л ╨╜╨░ +4
         ld (ix+3),e
         ld (ix+4),d
         ld c,PIECESZ
         add ix,bc
         inc hl
         inc hl
        ret

getmask
        ld a,(pgmain4000)
        SETPG4000
        ld de,tmp-4
        ld bc,mask10-mask00
        ldir
        ret

maskpiece
;0x4000,0x8000 = scrdata
;hl=0x4000+
;de=╤Б╨┐╤А╨░╨╣╤В ╨╝╨░╤Б╨║╨╕ (╨▒╨╡╨╖ ╨╖╨░╨│╨╛╨╗╨╛╨▓╨║╨░): 0,mask, ╨▓ ╨║╨╛╨╜╤Ж╨╡ ╤Б╤В╨╛╨▒╨╗╤Ж╨░ 2-╨▒╨░╨╣╤В╨╜╤Л╨╣ ╨░╨┤╤А╨╡╤Б
;╤В╤Г╨┤╨░ ╨╢╨╡ ╨║╨╗╨░╨┤╤С╨╝ ╤А╨╡╨╖╤Г╨╗╤М╤В╨░╤В ╨╜╨░╨╗╨╛╨╢╨╡╨╜╨╕╤П: and(~mask),or(scrdata&mask)
        ld lx,32/8
maskpiececolumns0
        call maskpiececolumn
        ld bc,0x4000
        add hl,bc
        call maskpiececolumn
        ld bc,0x2000-0x4000
        add hl,bc
        call maskpiececolumn
        ld bc,0x4000
        add hl,bc
        call maskpiececolumn
        ld bc,-0x2000-0x4000+1
        add hl,bc
        dec lx
        jr nz,maskpiececolumns0
        ret

maskpiececolumn
        push hl
        ld bc,40
        ld hx,32
maskpiece1
        inc de
        ld a,(de) ;mask
        dec de
        cpl
        ld (de),a
        cpl
        inc de
        and (hl) ;scrdata
        ld (de),a      
        inc de
        add hl,bc
        dec hx
        jr nz,maskpiece1
        inc de
        inc de
        pop hl
        ret

addgridpg
;pic (0x8000) & grid (0xc000) -> scr (0x4000)
        ld hl,0x8000
        ld bc,0x4000
addgrid0
        ld a,(hl)
        set 6,h
        and (hl)
        res 7,h
        ld (hl),a
        res 6,h
        set 7,h
        cpi
        jp pe,addgrid0
        ret
       
keepspr
cursprlistaddr=$+1
        ld hl,0
        ld (hl),e ;x
        inc hl
        ld (hl),c ;y
        inc hl
        ld a,(iy-1)
        ld (hl),a ;hgt
        inc hl
        ld (cursprlistaddr),hl
        ret
       
endkeepspr
        ld hl,(cursprlistaddr)
        ld (hl),200
        ret

showwin
        ld a,(pgpicdata0)
        SETPGC000 ;╨▓╨║╨╗╤О╤З╨╕╨╗╨╕ ╤Б╤В╤А╨░╨╜╨╕╤Ж╤Г ╤Б ╨┤╨░╨╜╨╜╤Л╨╝╨╕ ╨▓ c000
        ld a,(user_scr0_low) ;ok
        SETPG4000 ;╨▓╨║╨╗╤О╤З╨╕╨╗╨╕ ╨┐╨╛╨╗-╤Н╨║╤А╨░╨╜╨░ ╨▓ 4000
        ld hl,0xc000
        ld de,0x4000
        ld bc,0x4000
        ldir ;╨┐╨╡╤А╨╡╨▒╤А╨╛╤Б╨╕╨╗╨╕ ╨╜╨░ ╤Н╨║╤А╨░╨╜

        ld a,(pgpicdata1)
        SETPGC000 ;╨▓╨║╨╗╤О╤З╨╕╨╗╨╕ ╤Б╤В╤А╨░╨╜╨╕╤Ж╤Г ╤Б ╨┤╨░╨╜╨╜╤Л╨╝╨╕ ╨▓ c000
        ld a,(user_scr0_high) ;ok
        SETPG4000 ;╨▓╨║╨╗╤О╤З╨╕╨╗╨╕ ╨┤╤А╤Г╨│╨╕╨╡ ╨┐╨╛╨╗-╤Н╨║╤А╨░╨╜╨░ ╨▓ 4000
        ld hl,0xc000
        ld de,0x4000
        ld bc,0x4000
        ldir ;╨┐╨╡╤А╨╡╨▒╤А╨╛╤Б╨╕╨╗╨╕ ╨╜╨░ ╤Н╨║╤А╨░╨╜
        ret

sprlist1
        ds 3*128,200
sprlist2
        ds 3*128,200

pal
        ds 32 ;╤В╤Г╤В ╨▒╤Г╨┤╨╡╤В ╨┐╨░╨╗╨╕╤В╤А╨░ ╨║╨░╤А╤В╨╕╨╜╨║╨╕
emptypal
        ds 32,0xff ;╨┐╨░╨╗╨╕╤В╤А╨░, ╨│╨┤╨╡ ╨▓╤Б╨╡ ╤Ж╨▓╨╡╤В╨░ ╤З╤С╤А╨╜╤Л╨╡
        include "pal.ast"

loadfile_in_ahl
;de=╨╕╨╝╤П ╤Д╨░╨╣╨╗╨░
;hl=╨║╤Г╨┤╨░ ╨│╤А╤Г╨╖╨╕╨╝ (0xc000)
;a=╨▓ ╨║╨░╨║╨╛╨╣ ╤Б╤В╤А╨░╨╜╨╕╤Ж╨╡
        SETPGC000 ;╨▓╨║╨╗╤О╤З╨╕╨╗╨╕ ╤Б╤В╤А╨░╨╜╨╕╤Ж╤Г A ╨▓ 0xc000
        push hl ;╨║╤Г╨┤╨░ ╨│╤А╤Г╨╖╨╕╨╝
        OS_OPENHANDLE
        pop de ;╨║╤Г╨┤╨░ ╨│╤А╤Г╨╖╨╕╨╝
        push bc ;b=handle
        ld hl,0x4000 ;╤Б╤В╨╛╨╗╤М╨║╨╛ ╨│╤А╤Г╨╖╨╕╨╝ (╨╡╤Б╨╗╨╕ ╤Б╤В╨╛╨╗╤М╨║╨╛ ╨╡╤Б╤В╤М ╨▓ ╤Д╨░╨╣╨╗╨╡)
        OS_READHANDLE
        pop bc ;b=handle
        OS_CLOSEHANDLE
        jp setpgmainc000 ;╨▓╨║╨╗╤О╤З╨╕╨╗╨╕ ╤Б╤В╤А╨░╨╜╨╕╤Ж╤Г ╨┐╤А╨╛╨│╤А╨░╨╝╨╝╤Л ╨▓ c000, ╨║╨░╨║ ╨▒╤Л╨╗╨╛

primgega_onescreen
;b=hgt,c=wid (/2)
;de=gfx
;hl=scr
primgega0
        push bc
        ld hx,b
        push hl
        ld bc,40
primgegacolumn0
        ld a,(de)
        inc de
        ld (hl),a
        add hl,bc
        dec hx
        jr nz,primgegacolumn0
        pop hl
        ld a,0x9f;0xa0
        cp h
        ld bc,0x4000
        adc hl,bc
        jp pe,primgegacolumn0q ;╨▓ ╨┐╨╛╨╗╨╛╨▓╨╕╨╜╨╡ ╤Б╨╗╤Г╤З╨░╨╡╨▓
;8000->╤Б000 (╨╜╨░╨┤╨╛ 6000) ╨╕╨╗╨╕ a000->e001 (╨╜╨░╨┤╨╛ 4001)
         inc a
        xor h
        ld h,a
primgegacolumn0q
        pop bc
        dec c
        jr nz,primgega0
        ret


copyimgega_curtodefault
;d=hgt,e=wid (/8)
;hl=scr
        call getuser_scr_low_cur
        SETPG4000 ;set "from" page in 4000
        call getuser_scr_low
        SETPGC000 ;set "to" page in c000
        call copyimgegalayer
        call getuser_scr_high_cur
        SETPG16K ;set "from" page in 4000
        call getuser_scr_high
copyimgegaq
        SETPGC000 ;set "to" page in c000
        call copyimgegalayer
        call setpgmainc000
        jp setpgsmain40008000

copyimgega_defaulttocur
;d=hgt,e=wid (/8)
;hl=scr
        call getuser_scr_low
        SETPG4000 ;set "from" page in 4000
        call getuser_scr_low_cur
        SETPGC000 ;set "to" page in c000
        call copyimgegalayer
        call getuser_scr_high
        SETPG4000 ;set "from" page in 4000
        call getuser_scr_high_cur
        jr copyimgegaq ;set "to" page in c000, copy

        if 0
copyimgega_defaulttoshadow
;d=hgt,e=wid (/8)
;hl=scr
        ld a,(pgscrdata0)
        SETPG4000 ;set "from" page in 4000
        call getuser_scr_low
        SETPGC000 ;set "to" page in c000
        call copyimgegalayer
        ld a,(pgscrdata1)
        SETPG4000 ;set "from" page in 4000
        call getuser_scr_high
        jr copyimgegaq ;set "to" page in c000, copy
        endif

copyimgegalayer
        push hl
        ld hx,e ;wid/8
copyimgega0
        push de
        push hl
        ld b,d ;hgt
        ld de,40-0x8000
copyimgegacolumn0
        ld a,(hl)
        set 5,h
        ld c,(hl)
         set 7,h
        ld (hl),c
        res 5,h
        ld (hl),a
        add hl,de
        djnz copyimgegacolumn0
        pop hl
        pop de
        inc hl
        dec hx
        jr nz,copyimgega0
        pop hl
        ret
       
genxy
;╤Б╨╗╤Г╤З╨░╨╣╨╜╤Л╨╡ ╨║╨╛╨╛╤А╨┤╨╕╨╜╨░╤В╤Л (╨╡╤Б╨╗╨╕ ╨╜╨╡ ╨┤╨▓╨╕╨│╨░╨╗╨╕ ╨║╤Г╤Б╨╛╤З╨╡╨║)
        ld ix,pieces ;x,y,pg,addr
        ld b,NPIECES
genxy0
        ld a,(ix+7)
        or a
        jr nz,genxy_moved
         ld c,+(320-16-32)/4
         call rnd
         add a,a
         add a,16/2
        ;ld a,(ix+5) ;ideal x2
         ld (ix+0),a ;x2
         ld c,+(192-16-32)/4
         call rnd
         add a,a
         add a,a
         add a,16
        ;ld a,(ix+6) ;ideal y
         ld (ix+1),a ;y
genxy_moved
        ld de,PIECESZ
        add ix,de
        djnz genxy0
        ret

swappieces
;╤Б╨╗╤Г╤З╨░╨╣╨╜╤Л╨╣ ╨╛╨▒╨╝╨╡╨╜ ╨┐╨╛╤А╤П╨┤╨║╨░ (╨╡╤Б╨╗╨╕ ╨╜╨╡ ╨┤╨▓╨╕╨│╨░╨╗╨╕ ╨║╤Г╤Б╨╛╤З╨╡╨║)
        ld c,NPIECES
        call rnd
        call getpiece_a
        ld bc,7
        add hl,bc
        bit 0,(hl)
        ret nz
        ;sbc hl,bc
        ex de,hl
        ld c,NPIECES
        call rnd
        call getpiece_a
        ld bc,7
        add hl,bc
        bit 0,(hl)
        ret nz
        ;sbc hl,bc
        ld b,PIECESZ
swappieces0
        ld c,(hl)
        ld a,(de)
        ld (hl),a
        ld a,c
        ld (de),a
        dec hl
        dec de
        djnz swappieces0
        ret
getpiece_a
        ld c,a
        ld b,0
        ld hl,pieces
        dup PIECESZ
        add hl,bc
        edup
        ret

rnd
;0..c-1
        ;ld a,r
        push de
        push hl
        call func_rnd
        pop hl
        pop de
rnd0
        sub c
        jr nc,rnd0
        add a,c
        ret

func_rnd
;Patrik Rak
rndseed1=$+1
        ld  hl,0xA280   ; xz -> yw
rndseed2=$+1
        ld  de,0xC0DE   ; yw -> zt
        ld  (rndseed1),de  ; x = y, z = w
        ld  a,e         ; w = w ^ ( w << 3 )
        add a,a
        add a,a
        add a,a
        xor e
        ld  e,a
        ld  a,h         ; t = x ^ (x << 1)
        add a,a
        xor h
        ld  d,a
        rra             ; t = t ^ (t >> 1) ^ w
        xor d
        xor e
        ld  h,l         ; y = z
        ld  l,a         ; w = t
        ld  (rndseed2),hl
        ;ex de,hl
        ;ld hl,0
        ;res 7,c ;int
        ret

pgscrdata0
        db 0
pgscrdata1
        db 0
pgpicdata0
        db 0
pgpicdata1
        db 0
pggriddata0
        db 0
pggriddata1
        db 0

clickstate
        db 0

curpiece
        dw 0

curkey
        db 0
joystate
;bit - button (ZX key)
;7 - A (A)
;6 - B (S)
;5 - Select (Space)
;4 - Start (Enter)
;3 - Up (7)
;2 - Down (6)
;1 - Left (5)
;0 - Right (8)
        db 0

SAVEDATA
level
        db 0
;curx
;       db 0
;cury
;       db 0

cur_h
        db 0
cur_m
        db 0
cur_s
        db 0
cur_f
        db 0

tlevel
ttime
        ;db "TIME "
        db "00:00:00"
ttimeh1=$-8
ttimeh2=$-7
ttimem1=$-5
ttimem2=$-4
ttimes1=$-2
ttimes2=$-1
;nextlevelon=$ ;╤Н╤В╨╛╤В ╤Д╨╗╨░╨│ ╨╜╨░╨┤╨╛ ╤Б╨╛╤Е╤А╨░╨╜╤П╤В╤М
        db 0

;╤Г ╨╜╨░╤Б 20x12 = 240 ╨║╤Г╤Б╨╛╤З╨║╨╛╨▓
pieces
_y=0
        dup 12
_x=0
        dup 20
        db 0,0 ;x,y
        db 0 ;pg (╨▓╨╕╤А╤В╤Г╨░╨╗╤М╨╜╨░╤П ╨▓ tpg)
        dw 0 ;addr
        db _x,_y ;ideal x,y
        db 0 ;0=not moved
_x=_x+8
        edup
_y=_y+16
        edup

SAVEDATAsz=$-SAVEDATA

path
        db "puzzle",0
filenamepic0
        db "0pic0.bmpx",0
filenamepic1
        db "1pic0.bmpx",0
filenamegrid0
        db "0grid.bmpx",0
filenamegrid1
        db "1grid.bmpx",0

filename
        db "puzzle.ini",0

        include "mem.asm"
        include "dynmem.asm"
        include "int.asm"

        include "prspr.asm"

div4signedup
        or a
        jp m,$+5
        add a,3
        sra a
        sra a
        ret

prtext
;bc=╨║╨╛╨╛╤А╨┤╨╕╨╜╨░╤В╤Л
;hl=text
        ld a,(hl)
        or a
        ret z
        call prcharxy
        inc hl
        inc c
        jr prtext

prnum
        ld bc,1000
        call prdig
        ld bc,100
        call prdig
        ld bc,10
        call prdig
        ld bc,1
prdig
        ld a,'0'-1
prdig0
        inc a
        or a
        sbc hl,bc
        jr nc,prdig0
        add hl,bc
        ;push hl
        ;call prchar
        ;pop hl
        ;ret
prchar
;a=code
;de=screen
        push de
        push hl
        call prcharin
        pop hl
        pop de
        inc e
        ret
       
calcscraddr
;bc=yx
;╨╝╨╛╨╢╨╜╨╛ ╨┐╨╛╤А╤В╨╕╤В╤М bc
        ex de,hl
        ld a,c ;x
        ld l,b ;y
        ld h,0
        ld b,h
        ld c,l
        add hl,hl
        add hl,hl
        add hl,bc ;*5
         add hl,hl
         add hl,hl
         add hl,hl ;*40
         add hl,hl
         add hl,hl
         add hl,hl
        add a,l
        ld l,a
        ld a,h
        adc a,0x80
        ld h,a
        ex de,hl
        ret

prcharxy
;a=code
;bc=yx
        push de
        push hl
        push bc
        push af
        call calcscraddr
        pop af
        call prcharin
        pop bc
        pop hl
        pop de
        ret
       
prcharin
        sub 32
        ld l,a
        ld h,0
         add hl,hl
         add hl,hl
         add hl,hl
         add hl,hl
         add hl,hl
        ;ld bc,font-(32*32)
        ;add hl,bc
        ld a,h
        add a,font/256
        ld h,a
prcharin_go
        ex de,hl
       
        ld bc,40
        push hl
        push hl
        dup 8
        ld a,(de) ;font
        ld (hl),a ;scr
        inc de
        add hl,bc
        edup
        pop hl
        set 6,h
        ;ld d,font/256
        dup 8
        ld a,(de) ;font
        ld (hl),a ;scr
        inc de
        add hl,bc
        edup
        pop hl
        set 5,h
        push hl
        ;ld d,font/256
        dup 8
        ld a,(de) ;font
        ld (hl),a ;scr
        inc de
        add hl,bc
        edup
        pop hl
        set 6,h
        ;ld d,font/256
        dup 8
        ld a,(de) ;font
        ld (hl),a ;scr
        inc de
        add hl,bc
        edup        
        ret

        macro SHAPESPROC name
name
        endm

        include "prarrow.asm"

        include "control.asm"

prlevelifneeded
        xor a
invalidatetime=$+1
        cp 0
        ret z
prlevel
        call setpgsscr8000c000
        ;ld a,(level)
        ;inc a
        ;ld hl,tleveldig1
        ;call dectotxt12
        ;ld (tleveldig2),a
        ;ld a,b
        ;ld (tleveldig1),a
        ld a,(cur_h)
        ld hl,ttimeh1
        call dectotxt12
        ld a,(cur_m)
        ld hl,ttimem1
        call dectotxt12
        ld a,(cur_s)
        ld hl,ttimes1
        call dectotxt12
       
         xor a
         ld (invalidatetime),a
       
        ld bc,24*256+20-4
        ld hl,tlevel
        jp prtext

dectotxt12
        ld b,'0'-1
        inc b
        sub 10
        jr nc,$-3
        add a,'0'+10
         ld (hl),b
         inc hl
         ld (hl),a
        ret

updatetime
        OS_GETTIMER ;dehl=timer
        ld de,(oldupdtimer)
        ld (oldupdtimer),hl
        or a
        sbc hl,de ;hl=frames
        ret z
        ld b,h
        ld c,l
updatetime0
        call inctime
        ;dec bc
        ;ld a,b
        ;or c
        ;jr nz,updatetime0
        cpi
        jp pe,updatetime0
        ret
inctime
        ld hl,cur_f
        inc (hl)
        ld a,(hl)
        sub 50
        ret c
        ld (hl),a
         ld a,1
         ld (invalidatetime),a
        ld hl,cur_s
        inc (hl)
        ld a,(hl)
        sub 60
        ret c
        ld (hl),a
        ld hl,cur_m
        inc (hl)
        ld a,(hl)
        sub 60
        ret c
        ld (hl),a
        ld hl,cur_h
        inc (hl)
        ret

oldupdtimer
        dw 0

        align 256
font
        display "font=",font
        incbin "fontgfx"
        align 256
tpg
        ds 256

        ds 4
tmp

        ds 0x3f00-(mask10-mask00)-$

        ds 0x4000-$
        include "spr.ast"

end

        ;display "End=",end
        ;display "Free after end=",/d,#c000-end
        ;display "Size ",/d,end-begin," bytes"
       
        savebin "puzzle.com",begin,end-begin
       
        LABELSLIST "../../../us/user.l",1