;Alone Coder 02,28.12.2020,24.01-06.02.2024
;print bc by VadRov
;rotation direction fixed by Ped7g
;-15 bytes by SerzhSoft
;48K or usr0 only! (0x5b00..5b1f must be clean)
SCORE=1
	device zxspectrum128
fieldwid=16 ;15 empty per line
fieldx=8
         org 0x66c6
begin
newfig
         ld hl,0x5800+fieldx
dellns0
	 xor a
         ld d,h:ld e,l
         ld (hl),a ;draw wall
         ld b,fieldwid ;16
         or (hl):dec de:inc hl:djnz $-3
         jr nz,finddellines_nofire
         push hl ;end of line
         ex de,hl
	 ;ld b,3 ;wall from above
         ;ld b,2
         inc b:ld c,l ;lower 2/3!
         ;ld a,h:sub 0x58:ld b,a:ld c,l
	 lddr
	if SCORE
         inc lx ;was 0
	 call 0x200a ;at BC
	 ld c,lx ;was 0
         call 0x1a1b ;print bc
	endif
         pop hl
finddellines_nofire
	 ld a,h
	 cp 0x5b
	 jr c,dellns0
         ;ld a,r:and 7
         ;ld hl,figs:add a,l:ld l,a
         ld hl,figs78	;figs
         ld sp,hl
         ld a,r
         or 0x78
         add a,l
         ld l,a
         ld de,curfig
         ;xor a:ld (de),a:inc e
         ld a,(0x6fed) ;=0
         org $-2
         rld:ld (de),a:inc e
         jr nz,$-4
         ld hl,0x5811-32
	 ;ld sp,oldmyxy+2
pushpush_gameloop
	push hl
	push hl
gameloop
         ld a,_pixel
         call prfig_myxy
         ;halt:halt:halt ;non-blinking
         ld a,_clpixel
         call prfig_myxy ;z! bc=32+5! de=curfig+4!
         ld hl,curfig-4
         ;ld de,oldfig-4
         ldir ;bc=0!
	pop hl ;(myxy)
	 pop de
	 push hl ;(oldmyxy)	 
	 in a,(0xfe) ;a=0! z!
         rra:jr c,$+3:dec l ;'Q'
         rra:jr c,$+3:inc l ;'W'
         jr nz,noautodn ;nz
         exx:rlc c:exx ;was 0x10
         jr nc,noautodn ;nz
	 ;bit 0,(iy+62)
	 ;jr nz,noautodn ;too fast
         ld c,32
         add hl,bc ;nz
	 cp a ;z=godown
noautodn
        push hl ;(myxy)
         rra ;'E'
         jr c,norotfig      
	 ld b,4+4+4;+32
         ld sp,oldfig
rotfig1	 ld hl,curfig
         dec sp:pop af ;oldfig,0[4],oldfigcopy
rotfig2  rra
         rl (hl)
         ;res 4,(hl)
         inc l
         jr nz,rotfig2
         djnz rotfig1
         ld sp,myxy
	 inc l ;nz ;or else sticks to side
norotfig
	 ex af,af' ;' ;z=godown
         ld a,_rdpixel
         call prfig_myxy;z=collsn, bc=32+5! de=curfig+4!
         or a
         jr nz,gameloop ;nocollsn
         ;ld hl,oldfig+4
         ;ld de,curfig+4
         lddr
	pop hl:pop hl ;(oldmyxy)
	;push hl;:push hl ;(myxy)
         ex af,af' ;' ;z=godown
         jr nz,pushpush_gameloop
;stopfig
        ;pop hl;:pop hl ;(oldmyxy)
         ld a,_pixel
         call prfig
         db 0xc3 ;jp newfig=0x66c6
figs     db 0xc6,0x66,0xf0,0x2e
         db 0x47,0x4e,0x4e ;,0x36
_clpixel ld (hl),0x38 ;ld(hl)=0x36
         ret
_pixel   xor a:ld (hl),a
_rdpixel and (hl):ret
prfig_myxy
         ld hl,(myxy)
prfig    ld (_calladdr),a
        if (_rdpixel&0x38) == 0
         or h ;(a&8)!=0 for rdpixel ;NC!
        endif
         ld de,curfig
_lines
         ld c,32+5:add hl,bc
       ex de,hl:ld b,(hl):ex de,hl
         set 4,b ;0b0001pppp
_pixels  call c,_pixel
_calladdr=$-2
         dec hl:srl b ;pixel
         jr nz,_pixels
         ;ld c,32+5:add hl,bc
         inc e:jr nz,_lines
         ld hl,oldfig+4
         ld d,h;inc d ;nz
	 halt ;blinking
         ret ;z
end
;myxy=$+64
;oldmyxy=myxy+2
figs78=(figs&0xff00)+((figs-0x78)&0xff)
myxy=figs78-4
oldmyxy=figs78-2
curfig=0x68fc
oldfig=0x6904

	display "figs=",figs
	display "_rdpixel=",_rdpixel
	display "End=",end
	;display "Free after end=",/d,#c000-end
	display "Size ",/d,end-begin," bytes"
	
	savebin "code.c",begin,end-begin
	
	LABELSLIST "..\us\user.l"
