worm 0.80.0206: partial revert?
[wormy.git] / worm.asm
index 0abad605dc7e56a0cb15cc8478f11a1d7a5883ea..383bab21a0ff9df450ea507923a9be3d72d27e98 100644 (file)
--- a/worm.asm
+++ b/worm.asm
+; Title                      : Worm
+; Version                    : 0.92
+; Release Date               : soon (I hope)
+; Filename                   : worm.86p (3404)
+; Author(s)                  : Shiar
+; Email Address              : shiar@mailroom.com
+; ICQ UIN                    : #43840958
+; Web Page                   : www.games3.net/shiar
+; Description                : ruling multiplayer game for 86 (most like Nibbles)
+; Where to get this game     : games3.net/shiar (home of Worm)
+; Other games by author(s)   : Shiar: Nemesis beta
+; Additional Credits to      : Matthew Shepcar (wrote original Peaworm, end'98)
+;                              Jonah Cohen (wrote some parts of worm)
 
 
-; -WonderWorm--v0.9-
-; by Matthew Shepcar
-; 30th December 1998
+;-----------------------------
+;----------- TO-DO -----------
+;-----------------------------
 
 
-#include asm86.h
-#include ti86asm.inc
-#include ti86abs.inc
+;       Shiar 4.II.00
+
+;Feel like doing something? (name indicates who's working on it)
+;  * linkplay TESTING! (+faster)
+;  * singleplay ending
+;  * titlescreen
+;  * make linkplay available for all gametypes (not just deathmatch)
+;  * two worms collide with heads -> both should die
+;  * bouncing balls :)
+;  * game types:
+;1)  * ctf: take enemy flag (right-bottom) and return to your flag (left-top)
+;1)  * domination?: take control points by running over them and hold them
+;  * AI worms?
+;1)* sprites for picks instead of blocks
+;  * fix linedraw procedure
+;  * save hiscore
+
+;1) Working on a new pickup-system allowing more than one "food" and different pickup-handling
+
+#include "TI86.inc"
+
+;-----------------------------
+;------- program start -------
+;-----------------------------
 
 .org _asm_exec_ram
 
 .org _asm_exec_ram
-                    
+start:
   nop
   jp Start
   nop
   jp Start
-  .dw 0,WormMsg
-Start:
+  .dw 1
+  .dw WormMsg
+  .dw WormIcon
+
+WormIcon:
+  .db 9,2
+  .db %10010111,%01101111
+  .db %10110101,%01001011
+  .db %11110111,%01001001
+  .db %00000011,%10000000
+  .db %00000001,%11100000
+  .db %00111000,%11111000
+  .db %01111110,%00111111
+  .db %11101111,%00001111
+  .db %11000011,%10000000
 
 
-  call _runindicoff
+Start:
+  ld  (SpSave),sp
+  call _runIndicOff
+  call _flushAllMenus
   call _clrLCD
   call _clrLCD
+  res 2,(iy+13) ;appAutoScroll
 
 
-  call BuildTrigTables
+  ld  a,r
+  ld  (Seed),a
 
 
-  ld hl,0FC00h
-  ld de,GreyBuf
-  ld bc,400h
-  ldir
+;-----------------------------
+;----- build trig tables -----
+;-----------------------------
 
 
-  ld hl,IHandler
-  ld bc,IHandlerEnd-IHandler
-  ld de,0BCBCh
+  ld  hl,TrigPrecalc
+  ld  de,SinCosTable
+  push de                        ; >> 1
+  ld  bc,65
   ldir
   ldir
-  ld hl,0BD00h
-  ld (hl),d
-  ld de,0BD01h
-  ld a,d
-  ld bc,256
-  ldir
-  ld i,a
-  im 2
+  dec hl
+  ld  b,63
+MirrorSineWave:
+  dec hl
+  ld  a,(hl)
+  ld  (de),a
+  inc de
+  djnz MirrorSineWave
+  pop hl                         ; << 0k
+  ld  b,128+64
+NegativeSineWave:
+  xor a
+  sub (hl)
+  ld  (de),a
+  inc hl
+  inc de
+  djnz NegativeSineWave
 
 
-  ld a,r
-  ld (Seed),a
+;-----------------------------
+;---------- menu -------------
+;-----------------------------
 
 
-  ld hl,0
-  ld (Score),hl
-  ld a,1
-  ld (Level),a
-  ld a,3
-  ld (Lives),a
-  ld hl,Levels
+DisplayMenu:
+  ld  a,2
+  ld  (nrworms),a
+gomainMenu:
+  call _clrWindow
+  ld  hl,txtWelcome
+  call _puts
+  ld  de,$0205
+  ld  (_curRow),de
+  call _puts ;txtOptions
+  dec e ;$0204
+  ld  (_curRow),de
+  ld  hl,(CURtxtGame)
+  call _puts
+
+  xor a
+mainMenu:
+  call menucall
+  jr  nz,notselect
+select:
+  ld  a,b
+  or  a
+  jr  nz,gooptionsMenu
+  jp  LetsGetThisPartyOn
+notselect
+  cp  K_EXIT
+  jp  z,ExitNoStats
+  cp  K_RIGHT
+  ld  a,b
+  jr  nz,mainMenu
+  or  a
+  jr  z,Variation
+
+gooptionsMenu:
+  call _clrWindow
+  ld  hl,txtWelcome
+  call _puts
+  ld  hl,txtLevel
+  ld  de,$0205
+  ld  (_curRow),de
+  call _puts ;txtLevel
+  dec e ;$0204
+  ld  (_curRow),de
+  call _puts ;txtWorms
+  ld  a,(nrworms)
+  add a,'0'
+  call _putc
+  xor a
+optionsMenu:
+  call menucall
+  jr  nz,notopselect
+opselect:
+  ld  a,b
+  or  a
+  jr  nz,changelevel
+changeworms:
+  ld  hl,nrworms
+  inc (hl)
+  ld  a,4
+  cp  (hl)
+  jr  nc,gooptionsMenu
+  ld  (hl),2
+  jr  gooptionsMenu
+changelevel:
+  jr  optionsMenu
+
+notopselect:
+  cp  K_EXIT
+  jr  z,gomainMenu
+  ld  a,b
+  jr  optionsMenu
+
+Variation:
+  ld  a,0
+Gametype =$-1
+  ld  hl,txtGame2
+NEXTtxtGame =$-2
+  inc a
+  and 7 ;mod 8
+  jr  nz,okilydokily
+  ld  hl,txtGame
+okilydokily:
+  ld  (Gametype),a
+  ld  de,$0204
+  ld  (_curRow),de
+  ld  (CURtxtGame),hl
+  call _puts
+  ld  (NEXTtxtGame),hl
+  ld  a,b
+  jp  mainMenu
+
+menucall:
+  push af
+  ld  hl,$0004
+  ld  (_curRow),hl
+  ld  a,' '
+  call _putc
+  ld  hl,$0005
+  ld  (_curRow),hl
+  call _putc
+  pop af
+  push af
+  ld  h,0
+  add a,4
+  ld  l,a
+  ld  (_curRow),hl
+  ld  a,'*'
+  call _putc
+  pop af
+  ld  b,a
+menukeys:
+  halt \ halt
+  call _getcsc
+  or  a
+  jr  z,menukeys
+  cp  K_UP
+  jr  nz,notup
+updown:
+  ld  a,b
+  xor 1
+  ld  b,a
+  inc a ;nz
+  ret
+notup:
+  cp  K_DOWN
+  jr  z,updown
+  cp  K_ENTER
+  ret z
+  cp  K_SECOND
+  ret ;z=select
+
+;-----------------------------
+;-------- start game ---------
+;-----------------------------
+
+LetsGetThisPartyOn:
+  ld  hl,Gametype
+  ld  a,(hl)
+  add a,a
+  add a,(hl)
+  ld  e,a
+  ld  d,0
+  ld  hl,gamesdata
+  add hl,de
+
+  or  a
+  jr  nz,notsingle
+  inc a
+  ld  (nrworms),a
+notsingle:
+  ld  a,(hl)
+  ld  (gameCar),a
+  push af
+
+  and _datascore
+  ld  de,$FF64 ;virt.infinate
+  jr  z,setscorelimit
+  ld  d,0 ;de=100
+setscorelimit:
+  ld  (scorelimit),de
 
 
-StartLevel:
-  ld a,(hl)
-  ld (Left),a
-  inc hl
-  ld a,(hl)
-  ld (Speed),a
   inc hl
   inc hl
-  ld a,25
-NextLife:
-  ld (GrowAmt),a
+  call _ldHLind
+  pop af
   push hl
   push hl
-  ld d,(hl)
+  and _datalink
+  jr  z,GameOver
+
+linkmatch:
+  call _clrWindow
+  ld  a,WormVersion
+  call SendByte
+  jr  c,host
+
+client:
+  ld  hl,txtReceive
+  call _puts
+  call receive
+  cp  WormVersion
+  ret nz
+  jr  multiplayer
+
+host:
+  ld  hl,txtWaiting
+  call _puts
+  call receive
+  cp  WormVersion
+  ret nz
+  call send
+  ld  hl,SwapPos
+  ld  (hl),$f6
+
+multiplayer:
+  ld  a,2
+  ld  (nrworms),a
+  ld  de,LevelsDM
+
+;-----------------------------
+;--------- game over ---------
+;-----------------------------
+
+GameOver:
+  call _clrLCD
+  ld  hl,0
+  ld  (worm1+died),hl
+;+died=0 \ +score1=0
+  ld  (worm2+died),hl
+  ld  (worm3+died),hl
+  ld  (worm4+died),hl
+  inc h
+  ld  (worm1+score+1),hl
+;+score2=0 \ +delay=1
+  ld  (worm2+score+1),hl
+  ld  (worm3+score+1),hl
+  ld  (worm4+score+1),hl
+  ld  a,l
+  ld  (Level),a
+  ld  hl,worm1set+4
+  ld  de,worm1+lives
+  call _MOV5B ;9xld(de),(hl)
+  call _mov9b
+  ld  hl,worm2set+4
+  ld  de,worm2+lives
+  call _MOV5B
+  call _mov9b
+  ld  hl,worm3set+4
+  ld  de,worm3+lives
+  call _MOV5B
+  call _mov9b
+  ld  hl,worm4set+4
+  ld  de,worm4+lives
+  call _MOV5B
+  call _mov9b
+  pop de
+
+StartLevel:
+  ld  a,(de)
+  ld  (Left),a
+  inc de
+  ld  a,(de)
+  ld  (Speed),a
+  inc de
+  ld  a,(de)
+  ld  (worm1+grow),a
+  ld  (worm2+grow),a
+  ld  (worm3+grow),a
+  ld  (worm4+grow),a
+  inc de
+
+  ld  a,(gameCar)
+  and _datahunt
+  jr  z,nohunter
+  ld  a,huntersize
+  ld  (worm2+grow),a
+nohunter:
+
+  ld  (thislevel),de
+  push de
+  ld  hl,worm1set
+  ld  de,worm1+head
+  call _MOV4B
+  ld  hl,worm2set
+  ld  de,worm2+head
+  call _MOV4B
+  ld  hl,worm3set
+  ld  de,worm3+head
+  call _MOV4B
+  ld  hl,worm4set
+  ld  de,worm4+head
+  call _MOV4B
+  pop de
+
+  ld  hl,worm1
+  ld  a,(gameCar)
+  and _datanextl
+  ld  b,1
+  jr  nz,worminit
+  ld  b,4
+worminit:
+  push bc                        ; >> 1
+  ld  a,(de)
+  ld  (hl),a   ;d
+  inc de
+  inc hl
+  ld  a,SinCosTable/256
+  ld  (hl),a
   inc hl
   inc hl
-  ld e,(hl)
+
+  ld  a,(de)
+  ld  (hl),a   ;y
+  inc de
   inc hl
   inc hl
-  ld a,(hl)
+  ld  a,(de)
+  ld  (hl),a   ;x
+  inc de
   inc hl
   inc hl
-  ld (Heading),a
-  push de
-  ld a,(hl)
+
+  xor a
+  ld  (hl),a   ;y2
   inc hl
   inc hl
+  ld  (hl),a   ;x2
+
+  ld  bc,(worm2-worm1)-5
+  add hl,bc
+  pop bc                         ; << 0k
+  djnz worminit
+
+;-------- draw level ---------
+
+  ld  a,(de)
+  inc de
+  sub 128
+  ld  (FieldWidth),a
+  ld  a,(de)
+  inc de
   sub 57
   sub 57
-  ld (FieldHeight),a
+  ld  (FieldHeight),a
   add a,57-5
   add a,57-5
-  push hl
-  ld l,a
-  ld h,0
+  push de                        ; >> levelp
+  ld  l,a
+  ld  h,0
   add hl,hl
   add hl,hl
   add hl,hl
   add hl,hl
   add hl,hl
   add hl,hl
   add hl,hl
   add hl,hl
-  ex de,hl
+  add hl,hl
+  ex  de,hl
 
 
-  ld hl,ScrBuffer
-  push hl
-  push de
-  ld de,ScrBuffer+1
-  ld bc,31
-  ld (hl),-1
+  ld  hl,ScrBuffer
+  push hl                        ; >> 1
+  push de                        ; >> 2
+  ld  de,ScrBuffer+1
+  ld  bc,63
+  ld  (hl),%11111111
   ldir
   inc hl
   ldir
   inc hl
-  ld (hl),192
+  ld  (hl),%11000000
   inc hl
   inc hl
-  ld b,14
+  ld  b,31
 ClearLine:
 ClearLine:
-  ld (hl),c
+  ld  (hl),c
   inc hl
   djnz ClearLine
   inc hl
   djnz ClearLine
-  ld (hl),3
+  push hl                        ; >> 3
+
+  ld  a,(FieldWidth)
+  add a,126
+  push af                        ; >> 4
+  and %11111000
+  rra
+  rra
+  rra
+  ld  l,a
+  ld  h,0
+  add hl,de
+  pop af                         ; << 3
+  and %00000111
+  ld  b,a
+  ld  c,0
+  ld  a,%11000000
+  jr  z,NoVertShift
+VertShift:
+  rra
+  rr  c
+  djnz VertShift
+NoVertShift:
+  ld  (hl),a
   inc hl
   inc hl
-  ex de,hl
-  pop bc
+  ld  (hl),c
+
+  ex  de,hl
+  pop de                         ; << 2
+  pop bc                         ; << 1
   ldir
   ldir
-  pop hl
-  ld c,32
+  pop hl                         ; << 0k
+  ld  c,64
   ldir
   ldir
-  pop hl
 
 
-  ld a,(hl)
+;-draw lines-
+
+  pop hl                         ; << levelp
+  ld  a,(hl)
   inc hl
   inc hl
-  or a
-  jr z,NoLines
+  or  a
+  jr  z,NoLines
 DrawLines:
 DrawLines:
-  push af
-  ld d,(hl)
-  inc hl
-  ld e,(hl)
-  inc hl
-  ld a,(hl)
-  inc hl
-  push hl
-  ld l,(hl)
-  ld h,a
+  push af                        ; >> 1
+  call loaddrawdata
+  push hl                        ; >> 2
+  ld  l,(hl)
+  ld  h,a
   call Line
   inc d
   inc h
   call Line
   inc d
   inc h
@@ -131,159 +503,446 @@ DrawLines:
   dec d
   dec h
   call Line
   dec d
   dec h
   call Line
-  pop hl
+  pop hl                         ; << 1
   inc hl
   inc hl
-  pop af
+  pop af                         ; << 0k
   dec a
   dec a
-  jr nz,DrawLines
+  jr  nz,DrawLines
 NoLines:
 NoLines:
-  push hl
-
-  ld hl,ScrBuffer
-  ld de,ScrBuffer+BufSize
-  ld bc,BufSize
-  ldir
-
-  ld hl,0
-  ld (_penCol),hl
-  ld hl,LivesMsg
-  call _vputs
-  ld a,(Lives)
-  add a,'0'
-  call _vputmap
 
 
-  ld a,40
-  ld (_penCol),a
-  call _vputs ;scoremsg
-  ld a,100
-  ld (_penCol),a
-  call _vputs
-  ld a,(Left)
-  call PutNum
+  ld  a,(hl)
+  inc hl
+  or  a
+  jr  z,noboxes
+drawboxes:
+  push af
+  call loaddrawdata
+  push hl
+  ld  l,(hl)
+  ld  h,a
+  call drawbox
+  pop hl
+  inc hl
+  pop af
+  dec a
+  jr  nz,drawboxes
+noboxes:
 
 
-  call PutScore
+;-----------------------------
 
 
+  push hl                        ; >> levelp new
+  call showstats
+  ld  a,(gameCar)
+  and _datafood
+  jr  z,nofood
   call NewPea
   call NewPea
-
-  xor a
+  call DrawPea
+nofood:
+  ld  bc,(worm1+pos)
   call DisplayField
   call DisplayField
-  ld hl,LevelMsg
-  ld a,7
-  call DispMsg
-  ld a,(Level)
-  add a,'0'
-  call _putc
+  ld  hl,$FDE0
+  ld  de,$FDE1
+  ld  (hl),%11111111
+  ld  bc,$BF
+  ldir
+  ld  hl,4+(txtposReady*256)
+  ld  (_curRow),hl
+  set 3,(iy+5)
+  ld  hl,txtReady
+  call _puts
   res 3,(iy+5)
 
   res 3,(iy+5)
 
+  ld  a,0
+gameCar =$-1
+  and _datalink
+  jr  z,SwapPos ;no link
   xor a
   xor a
+  ld  (worm2+input),a
+  ld  (worm2+left),a
+  ld  (Speed),a
+
+SwapPos: ;$18 xx -> $F6 xx
+  jr  initfinished
+  inc a
+  ld  (worm2+left),a
+  ld  hl,(worm1+pos)
+  ld  de,(worm2+pos)
+  ld  (worm2+pos),hl
+  ld  (worm1+pos),de
+  ld  a,(worm1+heading)
+  ld  b,a
+  ld  a,(worm2+heading)
+  ld  (worm1+heading),a
+  ld  a,b
+  ld  (worm2+heading),a
+initfinished:
+
+  ld  b,0
 ReadyDelay:
   halt
 ReadyDelay:
   halt
-  dec a
-  jr nz,ReadyDelay
+  djnz ReadyDelay
 
 
-  ld hl,(HeadPos)
-  ld (TailPos),hl
-  pop hl
-  pop bc
-  push hl
-  ld de,0
+;-----------------------------
+;----------- LOOP ------------
+;-----------------------------
 
 GameLoop:
 
 GameLoop:
+  ld  bc,(worm1+pos)
+  call DisplayField
 
 
-  ld a,c
-  sub 29
-  jr nc,NotMinScroll
+  ld  a,1
+flashtime =$-1
+  dec a
+  jr  z,noflash
+  ld  (flashtime),a
+  ld  hl,$fc00+(16*7)
+screeninvertloop:
+  ld  a,(hl)
+  cpl
+  ld  (hl),a
+  inc hl
   xor a
   xor a
-NotMinScroll:
-  cp 43
-FieldHeight =$-1
-  jr c,NotMaxScroll
-  ld a,(FieldHeight)
-NotMaxScroll:
-  call DisplayField
+  cp  h
+  jr  nz,screeninvertloop
+noflash:
 
 
-  ld a,5
+  ld  a,0
 Speed =$-1
 Speed =$-1
+  or  a
+  jr  z,NoDelay
 Delay:
   halt
   dec a
 Delay:
   halt
   dec a
-  jr nz,Delay
+  jr  nz,Delay
+NoDelay:
+
+  ld  ix,worm1
+  ld  a,(nrworms)
+  ld  b,a
+handleworms:
+  push bc
+  call HandleWorm
+  ld  bc,worm2-worm1
+  add ix,bc
+  pop bc
+  djnz handleworms
+
+;-----------------------------
+;---------- keys -------------
+;-----------------------------
+
+HandleKeys:
+  ld  a,%10111111
+  out (1),a
+  in  a,(1)
+  rla
+  jr  c,NotPaused
+  ld  bc,$0103
+  out (c),b
+  halt
+  ld  b,11
+  out (c),b
+
+NotPaused:
+  rla
+  jp  c,GameLoop
+  jr  Exit ;&&
+
+WormDead:
+  ld  a,2
+  ld  (flashtime),a
+
+  ld  h,(ix+tail+1)
+  ld  l,(ix+tail)
+  push hl
+removewormloop:
+  ld  c,(hl)
+  inc hl
+  ld  b,(hl)
+  inc hl
+  res resbit,h
+  push hl
+  call res4pixels
+  pop hl
+  ld  a,(ix+head)
+  cp  l
+  jr  nz,removewormloop
+  ld  a,(ix+head+1)
+  cp  h
+  jr  nz,removewormloop
+
+  ;hl=ix+head
+  pop de ;ix+tail
+  or  a
+  sbc hl,de
+  ld  a,l
+  rr  h
+  rra
+  inc a
+  ld  (ix+grow),a
+
+  ld  de,10
+  call DecScore
+  ld  h,(ix+head+1)
+  ld  l,(ix+head)
+  ld  (ix+tail+1),h
+  ld  (ix+tail),l
+  ld  a,50
+  ld  (ix+delay),a
 
 
-  ld hl,0A000h
-HeadPos =$-2
-  ld (hl),c
+thislevel =$+1
+  ld  de,0
+  ld  a,(de)
+  inc de
+  ld  (ix+heading),a
+  ld  a,(de)
+  ld  (ix+pos),a
+  inc de
+  ld  a,(de)
+  ld  (ix+pos+1),a
+
+  inc (ix+died)
+  dec (ix+lives)
+  ret nz ;HandleWorm done
+  ld  a,(gameCar)
+  and _datalivel
+  ret z
+
+Exit:
+  ld  sp,0 ;pop all
+SpSave = $-2
+  call _clrWindow
+  ld  hl,txtGO
+  call _puts
+  ld  hl,txtGame
+CURtxtGame =$-2
+  call _puts
+  ld  de,0002
+  ld  (_curRow),de
+  call showLevel
+  ld  de,$0B03
+  ld  (_curRow),de
+  ld  hl,txtDied
+  call _puts
+  call _puts ;txtScore
+  xor a
+  ld  (_curCol),a
+
+  ld  b,2
+nrworms =$-1
+  ld  hl,worm1+died
+displayWormStats:
+  push bc
+  push hl
+
+  ld  bc,input-died
+  add hl,bc ;+input
+  xor a
+  cp  (hl)
+  jr  nz,NoLinkIndic
+  ld  b,7
+  inc hl ;+left
+  cp  (hl)
+  jr  z,hostLinkIndic
+  dec b
+hostLinkIndic:
+  ld  a,8
+  ld  (_curCol),a
+  ld  a,b
+  call _putc
+  xor a
+  ld  (_curCol),a
+  dec hl
+NoLinkIndic:
   inc hl
   inc hl
-  ld (hl),b
   inc hl
   inc hl
-  res 4,h
-  ld (HeadPos),hl
+  inc hl ;+name
+  call _puts
+
+  pop hl
+  push hl
+  ld  a,13
+  ld  (_curCol),a
+  ld  a,(hl) ;worm+died
+  push hl
+  call showA
+  pop hl
+
+  ld  a,16
+  ld  (_curCol),a
+  inc hl ;worm+score
+  call _ldHLind ;ld hl,(hl)
+  call showHL ;worm+score
+
+  pop hl
+  ld  bc,worm2-worm1
+  add hl,bc
+  pop bc
+  djnz displayWormStats
+
+waitkey:
+  halt
+  halt
+  call _getcsc
+  cp  K_ENTER
+  jp  z,DisplayMenu
+;  cp  K_EXIT
+  jr  nz,waitkey
+
+;x123456789012345678901
+;1----- GAME OVER -----
+;2Multiplayer
+;3Level 01
+;4           Died Score:
+;5NameName     03 00070
+;6Worm#02 @    05 00120
+;7Worm#03      15 00030
+;8Snaky   @    00 04820
+
+ExitNoStats:
+;  ld  a,(Eaten)
+ xor a
+  ld  hl,HiScore
+  cp  (hl)
+  jr  c,NotNewHigh
+  ld  (hl),a
+NotNewHigh:
+
+  ld  hl,_asapvar
+  rst 20h ;_ABS_MOV10TOOP1
+  rst 10h ;_FINDSYM
+  call _swapt_ ;_ex_ahl_bde
+  ld  de,4
+  add hl,de
+  adc a,0
+  call _SET_ABS_DEST_ADDR
 
 
-  ld hl,SinCosTable
-Heading =$-2
+  xor a        
+  ld  hl,start
+  call _SET_ABS_SRC_ADDR
+  ld  hl,end-start
+  call _SET_MM_NUM_BYTES
+  call _MM_LDIR
 
 
-  ld a,0FEh
+;  xor a
+;  ld  (_asapvar+1),a
+  res 4,(iy+9)
+  set 2,(iy+13)
+  jp  _clrWindow
+
+;-----------------------------
+;----------- worm ------------
+;-----------------------------
+
+inlink:
+  ld  a,0
+sendbyte =$-1
+  ld  b,(ix+left)
+  dec b
+  jr  z,receivefirst
+  call send
+  call receive
+  ld  l,a
+  ret
+receivefirst:
+  push af
+  call receive
+  ld   l,a
+  pop  af
+  push hl
+  call send
+  pop  hl
+  ret
+
+inkeys: ;use jp not call!
   out (1),a
   out (1),a
-  in a,(1)
-  rra
-  rra
-  jr c,NotRight
-  ld a,l
+  in  a,(1)
+  ld  b,a
+  and (ix+right)
+  jr  z,notright
+  ld  a,l
   add a,8
   add a,8
-  ld l,a
-  inc a
-NotRight:
-  rra
-  jr c,NotLeft
-  ld a,l
+  ld  l,a
+notright:
+  ld  a,b
+  and (ix+left)
+  jr  z,donediddelydone
+  ld  a,l
   sub 8
   sub 8
-  ld l,a
-NotLeft:
+  ld  l,a
+  jr  donediddelydone
 
 
-  ld (Heading),hl
+;------- handle worm ---------
 
 
-  push bc
-  ld a,(hl)
+HandleWorm:
+  dec (ix+delay)
+  ret nz
+  inc (ix+delay)
+  ld  l,(ix+heading)
+  ld  a,(ix+input)
+  or  a
+  jr  nz,inkeys
+  call inlink
+donediddelydone:
+  ld  a,l
+  ld  (sendbyte),a
+  ld  (ix+heading),l
+  ld  h,(ix+heading+1)
+
+  ld  c,(ix+pos)
+  ld  b,(ix+pos+1)
+  ld  e,(ix+pos2)
+  ld  d,(ix+pos2+1)
+
+;-------- move worm ----------
+
+Wormmove:
+  push bc                        ; >> pos
+  ld  a,(hl)
   add a,a
   add a,d
   add a,a
   add a,d
-  ld d,a
+  ld  d,a
   bit 7,(hl)
   bit 7,(hl)
-  jr z,NotNegX
+  jr  z,notnegX
   dec b
   dec b
-NotNegX:
-  jr nc,NotMoveX
+notnegX:
+  jr  nc,notmoveX
   inc b
   inc b
-NotMoveX:
-  ld a,l
-  add a,40h
-  ld l,a
-  ld a,(hl)
+notmoveX:
+  ld  a,l
+  add a,$40
+  ld  l,a
+  ld  a,(hl)
   add a,a
   add a,e
   add a,a
   add a,e
-  ld e,a
+  ld  e,a
   bit 7,(hl)
   bit 7,(hl)
-  jr z,NotNegY
+  jr  z,notnegY
   dec c
   dec c
-NotNegY:
-  jr nc,NotMoveY
+notnegY:
+  jr  nc,notmoveY
   inc c
   inc c
-NotMoveY:
+notmoveY: ;bc=newpos
+  ld  (ix+pos2),e
+  ld  (ix+pos2+1),d
+  ld  (ix+pos),c
+  ld  (ix+pos+1),b
 
 
-  pop hl
-  push de
-  ld a,h
+;-check-
+  pop hl                         ; << pos (old)
+  ld  a,h
   sub b
   and 1
   sub b
   and 1
-  ld h,a
-  ld a,l
+  ld  h,a
+  ld  a,l
   sub c
   and 1
   add a,h
   sub c
   and 1
   add a,h
-  ld d,4
-  jr z,GotFour
+  ld  d,4
+  jr  z,GotFour
   xor 3
   xor 3
-  ld d,a
+  ld  d,a
 GotFour:
 GotFour:
-
   call CheckPixel
   inc b
   call CheckPixel
   call CheckPixel
   inc b
   call CheckPixel
@@ -292,52 +951,144 @@ GotFour:
   dec b
   call CheckPixel
   dec c
   dec b
   call CheckPixel
   dec c
-  rl d
-  jr nc,WormNotCrashed
-  ld hl,0
+  rl  d
+  jp  nc,Drawworm
+
+;--------- worm hit ----------
+
+Hitworm:
+  ld  a,(gameCar)
+  ld  h,a
+  and _datafood
+  jp  z,WormDead
+  ld  a,h
+  and _datahunt
+  call nz,checkhitotherworm
+
+  ld  hl,0
 PeaY =$-2
 PeaX =$-1
 PeaY =$-2
 PeaX =$-1
-  ld a,b
+  ld  a,b
   sub h
   inc a
   sub h
   inc a
-  cp 4
-  jp nc,WormDead
-  ld a,c
+  cp  4
+  jp  nc,WormDead
+  ld  a,c
   sub l
   inc a
   sub l
   inc a
-  cp 4
-  jr nc,WormDead
-  push bc
+  cp  4
+  jp  nc,WormDead
   call DrawPea
   call DrawPea
-  ld a,0
-GrowAmt =$-1
+  ld  a,(ix+grow)
   add a,15
   add a,15
-  ld (GrowAmt),a
+  ld  (ix+grow),a
   call NewPea
   call NewPea
-  ld de,10
-  call IncScore
-  ld a,119
-  ld (_penCol),a
-  ld hl,Left
+  ld  hl,Left
   dec (hl)
   dec (hl)
-  jr nz,NotNextLevel
-  ld hl,Level
+  push af
+  ld  de,10
+  call IncScore
+  pop af
+  jr  nz,still_alive_not_dead
+  ld  a,(gameCar)
+  and _datafoodl
+  jr  z,still_alive_not_dead
+  ld  hl,Level
+  ld  a,(hl)
   inc (hl)
   inc (hl)
+  pop hl                         ; << call
+  pop hl                         ; << call
+  pop de                         ; << levelp new
+  cp  NUM_LEVELS
+  jp  z,Exit
+  ld  a,(gameCar)
+  and _datanextl
+  jp  nz,StartLevel
+  jp  Exit
+
+;-----------------------------
+
+checkhitotherworm:
+  .db $dd,$7d ;ld a,lx
+  cp  worm2&255
+  ret nz
+ThisIsJustASillyUselessLabel:
+  ld  hl,(worm1+tail)
+  ld  de,(worm1+head)
+nextotherwormbit:
+  ld  a,c
+  sub (hl)
+  inc hl
+  inc a
+  cp  4
+  jr  nc,nothit1 ;no
+  ld  a,b
+  sub (hl)
+  inc a
+  cp  4
+  jr  c,otherwormHIT ;yes
+nothit1:
+  inc hl
+  res resbit,h
+  call _cphlde
+  jr  nz,nextotherwormbit
+  ret
+
+checkhitlapline:
+  ld  a,63
+  sub b
+  jr  z,nextlaphalf
+  inc a
+  ret nz
+nextlaphalf:
+  ld  a,c
+  and 32 ;y>=32?
+  jr  nz,nolap
+  cp  (ix+reserv)
+  jr  z,nolap
+  push bc
+  ld  de,20
+  call IncScore
   pop bc
   pop bc
-  pop de
-  pop hl
-  pop de
-  jp StartLevel
+  xor a
+nolap:
+  ld  (ix+reserv),a
+  ret
 
 
-NotNextLevel:
-  ld a,(hl)
-  call PutNum
+otherwormHIT:
+  push ix
+  ld  de,10
+  call IncScore
+  ld  ix,worm1
+  call WormDead
+  pop ix
   pop bc
   pop bc
-WormNotCrashed:
+still_alive_not_dead:
+
+;-------- draw worm ----------
+
+Drawworm:
+  ld  a,(gameCar)
+  and _datahunt
+  call nz,HuntingTimeScore
+
+  ld  c,(ix+pos)
+  ld  b,(ix+pos+1)
+
+  ld  a,(gameCar)
+  and _datalaps
+  call nz,checkhitlapline
+
+  ld  l,(ix+head)
+  ld  h,(ix+head+1)
+  ld  (hl),c
+  inc hl
+  ld  (hl),b
+  inc hl
+  res resbit,h
+  ld  (ix+head),l
+  ld  (ix+head+1),h
 
 
-  push bc
-  ld a,3
-WormCol =$-1
   call SetPixel
   inc b
   call SetPixel
   call SetPixel
   inc b
   call SetPixel
@@ -346,119 +1097,79 @@ WormCol =$-1
   dec b
   call SetPixel
 
   dec b
   call SetPixel
 
-;  xor 2
-;  ld (WormCol),a
+  ld  a,(ix+grow)
+  dec a
+  jr  z,removetail
+  ld  (ix+grow),a
+  ret
 
 
-  ld hl,GrowAmt
-  dec (hl)
-  jr nz,GrowWorm
-  inc (hl)
-  ld hl,0A000h
-TailPos =$-2
-  ld c,(hl)
+removetail:
+  ld  l,(ix+tail)
+  ld  h,(ix+tail+1)
+  ld  c,(hl)
   inc hl
   inc hl
-  ld b,(hl)
+  ld  b,(hl)
   inc hl
   inc hl
-  res 4,h
-  ld (TailPos),hl
+  res resbit,h
+  ld  (ix+tail),l
+  ld  (ix+tail+1),h
 
 
+res4pixels:
   call ResPixel
   inc b
   call ResPixel
   inc c
   call ResPixel
   dec b
   call ResPixel
   inc b
   call ResPixel
   inc c
   call ResPixel
   dec b
-  call ResPixel
+  jp  ResPixel
 
 
-GrowWorm:
+;-----------------------------
+;----------- procs -----------
+;-----------------------------
 
 
-  ld a,0BFh
-  out (1),a
-  in a,(1)
-  rla
-  jr c,NotPaused
-  call _getky
-WaitUnpause:
-  halt
-  call _getky
-  or a
-  jr z,WaitUnpause
-  ld a,128
-NotPaused:
-  pop bc
-  pop de
+NewPea:
+;random routine
+  ld hl,0
+  ld de,12345
+Seed =$-2
+  ld a,7921 & 255
+  ld bc,1000h+(7921/256)
+domult16:
+  add hl,hl
   rla
   rla
-  jp c,GameLoop
-  push hl
-WormDead:
-  pop de
-  pop hl
-  ld hl,(HeadPos)
-  ld de,(TailPos)
-  sbc hl,de
-  ld a,l
-  rr h
-  rra
-  ld hl,Lives
-  dec (hl)
-  pop hl
-  jp nz,NextLife
-
-#if 0
-  ld a,(Eaten)
-  ld hl,HiScore
-  cp (hl)
-  jr c,NotNewHigh
-  ld (hl),a
-  ld hl,Varname-1
-  rst 20h
-  rst 10h
-  call 460Bh
-  ld de,HiScore-_asm_exec_ram+2
+  rl  c
+  jr  nc,noadd16
   add hl,de
   add hl,de
-  adc a,0
-  call _load_ram_ahl
-  ld a,(HiScore)
-  ld (hl),a
-NotNewHigh:
-#endif
-
-  im 1
-  ld a,03Ch
-  out (0),a
-  call _clrScrn
-  call _homeup
-  jp _getky
+noadd16:
+  djnz domult16
+  inc hl
+  ld  (Seed),hl ;seed=(seed*7921+1) MOD 65536
 
 
-NewPea:
-  call Random
-  ld a,h
-  and 7Fh
-  cp 77h
-  jr nc,NewPea
-  add a,3
-  ld h,a
+  ld  a,(FieldWidth)
+  add a,123
+  cp  h
+  jr c,NewPea
+  inc h
+  inc h
   ld a,(FieldHeight)
   ld a,(FieldHeight)
-  add a,57-12
-  ld b,a
-  ld a,l
-  and 7Fh
-  cp b
-  jr nc,NewPea
-  add a,5
-  ld l,a
-  ld (PeaY),hl
-  ld d,2
+  add a,51
+  cp  l
+  jr  c,NewPea
+  inc l
+  inc l
+  ld  (PeaY),hl
+  ld  d,2 ;don't draw
   push hl
   call CheckPea
   pop hl
   dec d
   push hl
   call CheckPea
   pop hl
   dec d
-  jr z,NewPea
-DrawPea:
-  ld d,0
+  jr  z,NewPea
+
+DrawPea: ;hl=(PeaY)
+  ld  d,0 ;draw
 CheckPea:
 CheckPea:
-  ld b,h
-  ld c,l
+  ld  b,h
+  ld  c,l
   call PeaPixel
   inc c
   call PeaPixel
   call PeaPixel
   inc c
   call PeaPixel
@@ -469,262 +1180,978 @@ PeaPixel:
   push de
   call FindPixel
   pop de
   push de
   call FindPixel
   pop de
-  ld e,a
-  ld a,d
-  or a
-  ld a,e
-  jr z,DrawPeaPixel
-  and (hl)
+  ld  e,a ;>>
+  ld  a,d
+  or  a
+  ld  a,e ;<<
+  jr  z,DrawPeaPixel ;d=0:draw
+  and (hl) ;pixel?
   ret z
   ret z
-  ld d,1
+  ld  d,1 ;d=1:yes 2:no
   ret
 DrawPeaPixel:
   ret
 DrawPeaPixel:
-  xor (hl)
-  ld (hl),a
+  xor (hl) ;change pixel
+  ld  (hl),a
   ret
 
   ret
 
-Random:
-  ld hl,0
-  ld de,12345
-Seed =$-2
-  ld a,7921 & 255
-  ld bc,1000h+(7921/256)
-domult16:
-  add hl,hl
-  rla
-  rl c
-  jr nc,noadd16
-  add hl,de
-noadd16:
-  djnz domult16
-  inc hl
-  ld (Seed),hl ;seed=(seed*7921+1) MOD 65536
-  ld a,h
-  ret
+;-------- pixelprocs ---------
 
 
-ResPixel:
+ResPixel: ;at bc
   call FindPixel
   cpl
   call FindPixel
   cpl
-  ld d,a
-  and (hl)
-  ld (hl),a
-  ld a,d
-  ld de,BufSize
-  add hl,de
   and (hl)
   and (hl)
-  ld (hl),a
+  ld  (hl),a
   ret
 
   ret
 
-SetPixel:
-  push bc
-  push af
-  push af
+SetPixel: ;at bc
   call FindPixel
   call FindPixel
-  pop de
-  ld e,a
-  rr d
-  jr c,SetGrey1
-  cpl
-  and (hl)
-  ld (hl),a
-SetGrey1:
-  or (hl)
-  ld (hl),a
-  ld bc,BufSize
-  add hl,bc
-  ld a,e
-  rr d
-  jr c,SetGrey2
-  cpl
-  and (hl)
-  ld (hl),a
-SetGrey2:
-  or (hl)
-  ld (hl),a
-  pop af
-  pop bc
+  or  (hl)
+  ld  (hl),a
   ret
 
   ret
 
-CheckPixel:
-  push de
+CheckPixel: ;at bc in d
   call FindPixel
   call FindPixel
-  push af
   and (hl)
   and (hl)
-  ld de,BufSize
-  add hl,de
-  ld e,a
-  pop af
-  and (hl)
-  or e
-  pop de
   ret z
   dec d
   ret
 
   ret z
   dec d
   ret
 
-FindPixel:
+FindPixel: ;bc to ahl + de gone
+  push de
   push bc
   push bc
-  ld a,b
+  ld  a,b
   and 7
   add a,offsets_table & 255
   and 7
   add a,offsets_table & 255
-  ld e,a
-  ld d,offsets_table/256
-  ld h,0
-  ld a,c
-  add a,a
-  add a,a
-  rl h
-  add a,a
-  rl h
-  add a,a
-  rl h
-  srl b
-  srl b
-  srl b
-  or b
-  ld l,a
-  ld a,(de)
-  ld de,ScrBuffer
+  ld  e,a
+  ld  d,offsets_table/256
+  ld  h,0
+  ld  l,c
+  add hl,hl
+  add hl,hl
+  add hl,hl
+  add hl,hl
+  add hl,hl
+  ld  a,b
+  and %11111000
+  rra
+  rra
+  rra
+  or  l
+  ld  l,a
+  ld  a,(de)
+  ld  de,ScrBuffer
   add hl,de
   pop bc
   add hl,de
   pop bc
+  pop de
   ret
 
   ret
 
+;----------- score -----------
+
+HuntingTimeScore:
+  .db $dd,$7d ;ld a,lx
+  cp  worm2&255
+  ret z ;=worm#2
+  dec (ix+reserv)
+  ret nz
+  ld  de,10
+
 IncScore:
 IncScore:
-  ld hl,(Score)
+  ld  h,(ix+score+1)
+  ld  l,(ix+score)
   add hl,de
   add hl,de
-  ld (Score),hl
-PutScore:
-  ld hl,-1
-  ld (_curRow),hl
-  ld a,63
-  ld (_penCol),a
-  ld hl,(Score)
-  xor a
-  call 4A33h
-  dec hl
-  ld b,5
-DoPutScore:
-  ld a,(hl)
-  inc hl
-  cp 32
-  jr nz,NotZeroDigit
-  ld a,'0'
-NotZeroDigit:
+  jr  scorecommon
+DecScore: ;&&&
+  ld  h,(ix+score+1)
+  ld  l,(ix+score)
+  or  a
+  sbc hl,de
+  ret c
+scorecommon:
+  ld  (ix+score+1),h
+  ld  (ix+score),l
+  ld  de,0
+scorelimit =$-2
+  call _cphlde
+  jp  nc,Exit
+
+showstats:
+  push ix
+  ld  h,0
+  ld  l,h
+  ld  (_penCol),hl
+  ld  a,(nrworms)
+  ld  b,a
+  ld  ix,worm1
+  ld  a,(gameCar)
+  and _datanextl
+  jr  nz,showstatsS
+showstatloop:
+  push bc
+;  push ix
+  call showstat
+;  pop ix
+  ld  de,worm2-worm1
+  add ix,de
+  ld  hl,_penCol
+  ld  a,(hl)
+  add a,10
+  ld  (hl),a
+  pop bc
+  djnz showstatloop
+  pop ix
+  ret
+
+showstat:
+  ld  a,(gameCar)
+  and _datalivel
+  jr  nz,showlives
+showscore:
+  ld  h,(ix+score+1)
+  ld  l,(ix+score)
+  call _D_HL_DECI
+  jr __vputs
+showlives:
+  ld  a,(ix+lives)
+  add a,'0'
+__vputmap:
+  push ix
   call _vputmap
   call _vputmap
-  djnz DoPutScore
+  pop ix
+  ret
+
+showstatsS:
+  ld  hl,txtLevel
+  call __vputs
+  ld  a,(Level)
+  cp  10
+  jr  c,tilllevel9
+  ld  l,a
+  ld  h,0
+  call _divHLby10
+  push af
+  ld  a,l
+  add a,'0'
+  call __vputmap
+  pop af
+tilllevel9:
+  add a,'0'
+  call __vputmap
+
+  ld  a,98
+  ld  (_penCol),a
+  call showscore
+  ld  a,123
+  ld  (_penCol),a
+  call showlives
+showleft:
+  ld  a,31
+  ld  (_penCol),a
+  ld  a,0
+Left =$-1
+  push af
+  call _divAby10
+  add a,'0'
+  call __vputmap
+  pop af
+  add a,'0'
+  call __vputmap
+  ld  hl,txtLeft
+  pop ix
+__vputs:
+  push ix
+  call _vputs
+  pop ix
   ret
 
   ret
 
-PutNum:
-  ld l,a
-  ld h,0
-  call UNPACK_HL
+showLevel:
+  ld  hl,txtLevel
+  call _puts
+  ld  a,(Level)
+showA:
+  cp  10
+  jr  c,LevelBelowTen
+  ld  l,a
+  ld  h,0
+  call _divHLby10
   push af
   push af
-  ld a,l
-  call PutDigit
+  ld  a,l
+  add a,'0'
+  call _putc
   pop af
   pop af
-PutDigit:
+LevelBelowTen:
   add a,'0'
   add a,'0'
-  jp _vputmap
+  jp  _putc
+
+showHL:
+  call _D_HL_DECI
+  jp  _puts
+
+_D_HL_DECI:
+  ld  de,savestr+4
+  ld  b,5
+ldhld:
+  call _divHLby10
+  add a,'0'
+  ld  (de),a
+  dec de
+  djnz ldhld
+  ld  hl,savestr
+  ret
+savestr:
+  .db "00000",0
+
+;-----------------------------
 
 offsets_table:
   .db 128,64,32,16,8,4,2,1
 
 
 offsets_table:
   .db 128,64,32,16,8,4,2,1
 
-IHandler:
-  exx
-  ex af,af'
-       in a,(3)
-       bit 1,a
-       jr z,notvbl
-       ld hl,GreyCounter
-       inc (hl)
-       ld a,(hl)
-  cp 3
-       ld a,3Ch
-       jr nz,notpage1
-  ld (hl),0
-  ld a,(GreyBuf/256)-0C0h
-notpage1:
-       out (0),a
-notvbl:
-  jp 3Ah
-IHandlerEnd:
-GreyCounter .db 0
-
 DisplayField:
 DisplayField:
-  ld l,a
-  ld h,0
+  ld  a,c
+  sub 29
+  jr  nc,NotMinYScroll
+  xor a
+NotMinYScroll:
+  cp  43
+FieldHeight =$-1
+  jr  c,NotMaxYScroll
+  ld  a,(FieldHeight)
+NotMaxYScroll:
+  ld  l,a
+  ld  h,0
   add hl,hl
   add hl,hl
   add hl,hl
   add hl,hl
   add hl,hl
   add hl,hl
   add hl,hl
   add hl,hl
-  push bc
-  push de
-  ld de,ScrBuffer
+  add hl,hl
+  push bc                        ; >> 1
+  push de                        ; >> 2
+  ld  de,ScrBuffer
   add hl,de
   add hl,de
-  ld de,0FC70h
-  ld bc,00390h
+  ld  a,b
+  sub 64
+  jr  nc,NotMinXScroll
+  xor a
+NotMinXScroll:
+  cp 128
+FieldWidth = $-1
+  jr c,NotMaxXScroll
+  ld a,(FieldWidth)
+NotMaxXScroll:
+  push af                        ; >> 3
+  and %11111000
+  rra
+  rra
+  rra
+  ld  c,a
+  ld  b,0
+  ld  de,DispBuffer
+  pop af                         ; << 2
+  and %00000111
+  push af                        ; >> 3
+  cp  6
+  jr  c,CopyScreen
+  inc c
+CopyScreen:
+  add hl,bc
+  ld b,57
+CopyScreenLoop:
+  push bc                        ; >> 4
+  ld bc,16
   ldir
   ldir
-  ld de,BufSize-390h
-  add hl,de
-  ld de,GreyBuf+70h
-  ld bc,00390h
+  ld c,16
+  add hl,bc
+  pop bc                         ; << 3
+  djnz CopyScreenLoop
+  pop af                         ; << 2
+  ld  c,$b7 ;or a
+Bit0:
+  jr  nz,Bit1
+  halt
+  halt
+  jr  AfterShiftDelay
+Bit1:
+  dec a
+  jr nz,Bit2
+  call ShiftRight1
+  jr AfterShiftDelay
+Bit2:
+  dec a
+  jr nz,Bit3
+  ld a,2
+  call ShiftRight
+  jr AfterShiftDelay
+Bit3:
+  dec a
+  jr nz,Bit4
+  call Chunk
+  call ShiftLeft1
+  jr AfterShift
+Bit4:
+  dec a
+  jr nz,Bit5
+  call Chunk
+  jr AfterShiftDelay
+Bit5:
+  dec a
+  jr nz,Bit6
+  call Chunk
+  call ShiftRight1
+  jr AfterShift
+Bit6:
+  dec a
+  jr nz,Bit7
+  ld a,2
+  call ShiftLeft
+  jr AfterShift
+Bit7:
+  call ShiftLeft
+AfterShiftDelay:
+  halt
+AfterShift:
+  ld hl,DispBuffer
+  ld de,$fc00+$70
+  ld bc,1024-$70
   ldir
   ldir
-  pop de
+  pop de                         ; << 1
+  pop bc                         ; << 0k
+  ret
+
+ShiftRight1:
+  ld a,1
+ShiftRight:
+  ld (ShiftRightCounter),a
+  ld a,c
+  ld (ShiftRightChunk),a
+  ld c,16
+  add hl,bc
+  ld b,57
+ShiftRightLoop:
+  push bc
+  ld bc,-32
+  add hl,bc
+  ex de,hl
+  ld a,(de)
+ShiftRightChunk:
+  or a
+  call c,_SHLACC
+  ld c,0
+ShiftRightCounter = $-1
+ShiftRowsLeft:
+  push hl
+  rla
+  ld b,16
+ShiftRowLeft:
+  dec hl
+  rl (hl)
+  djnz ShiftRowLeft
+  pop hl
+  dec c
+  jr nz,ShiftRowsLeft
+  ld bc,-16
+  add hl,bc
+  ex de,hl
   pop bc
   pop bc
+  djnz ShiftRightLoop
+  ret
+
+ShiftLeft1:
+  ld a,1
+ShiftLeft:
+  ld (ShiftLeftCounter),a
+  ld a,c
+  ld (ShiftLeftChunk),a
+  rla
+  jr nc,ShiftLeftSameByte
+  dec hl
+ShiftLeftSameByte:
+  ex de,hl
+  ld bc,-16
+  add hl,bc
+NewSprite:
+  ex de,hl
+  ld b,57
+ShiftLeftLoop:
+  push bc                        ; >> 1
+  ld bc,-32
+  add hl,bc
+  ex de,hl
+  ld a,(de)
+ShiftLeftChunk:
+  or a
+  call c,_SHRACC
+  ld c,0
+ShiftLeftCounter = $-1
+ShiftRowsRight:
+  push hl                        ; >> 2
+  rra
+  ld b,16
+ShiftRowRight:
+  rr (hl)
+  inc hl
+  djnz ShiftRowRight
+  pop hl                         ; << 1
+  dec c
+  jr nz,ShiftRowsRight
+  ld bc,-16
+  add hl,bc
+  ex de,hl
+  pop bc                         ; << 0k
+  djnz ShiftLeftLoop
   ret
 
   ret
 
-DispMsg:
+Chunk:
+  push hl                        ; >> 1
+  push de                        ; >> 2
+  ld  c,16
+  add hl,bc
+  ld  b,57
+ChunkScreen:
+  push bc                        ; >> 3
+  ld  bc,-32
+  add hl,bc
+  ex  de,hl
+  ld  a,(de)
+  call _SHRACC
+  ld  b,16
+ChunkRow:
+  dec hl
+  rld
+  djnz ChunkRow
+  ex  de,hl
+  pop bc                         ; << 2
+  djnz ChunkScreen
+  pop de                         ; << 1
+  pop hl                         ; << 0k
+  ld  c,$37 ;scf
+  ret
+
+;----------- draw ------------
+
+loaddrawdata:
+  ld  d,(hl)
+  inc hl
+  ld  e,(hl)
+  inc hl
+  ld  a,(hl)
+  inc hl
+  ret
+
+Line: ;draw line from de to hl (d<h)
   push hl
   push hl
-  ld h,a
-  ld l,4
-  ld (_curRow),hl
-  ld hl,0FDE0h
-  ld de,0FDE1h 
-  ld (hl),-1
-  ld bc,0BFh
-  ldir
+  push de
+  ld a,d
+;  cp h
+;  jr c,lineOrdered
+;  ex de,hl
+lineOrdered:  
+  ld  b,d
+  ld  c,e
+  push hl
+  push bc
+  call FindPixel
+  pop bc
+  pop de
+connectedLine:
+  push hl
+  ld  h,c
+  ld  c,a
+  ld  a,d
+  sub b
+  ld  b,a
+  ld  a,e
+  jr  nz,LineNotPoint
+  cp  h
+  jr  nz,LineNotPoint
   pop hl
   pop hl
-  set 3,(iy+5)
-  jp _puts
+  jr  DoneLine
+LineNotPoint:
+  sub h
+  ld  de,16
+  jr  nc,LinePositiveY
+  neg
+  ld  de,-16
+LinePositiveY:  
+  cp  b
+  jr  nc,SteepLine
+  add a,a
+  ld  (line2sm+1),a
+  ld  h,a
+  xor a
+  sub b
+  add a,a
+  ld  (line1sm+1),a
+  ld  a,h
+  sub b
+  pop hl
+LineLoopGentle: 
+  push af
+  ld  a,(hl)
+  or  c
+  ld  (hl),a
+  rrc c
+  jr  nc,$+3
+  inc hl
+  pop af       
+  jp  m,line2sm
+line1sm:
+  add a,0
+  add hl,de
+line2sm:
+  add a,0
+  djnz LineLoopGentle
+DoneLine:
+  ld  a,(hl)
+  or  c
+  ld  (hl),a
+  pop de
+  pop hl
+  ret
+SteepLine:
+  ld  h,a
+  neg
+  add a,a
+  ld  (line3sm+1),a
+  ld  a,b
+  add a,a
+  ld  (line4sm+1),a
+  sub h
+  ld  b,h
+  pop hl
+LineLoopSteep: 
+  push af
+  ld  a,(hl)
+  or  c
+  ld  (hl),a
+  add hl,de
+  pop af
+  jp  m,line4sm
+line3sm:
+  add a,0
+  rrc c
+  jr  nc,$+3
+  inc hl
+line4sm:
+  add a,0
+  djnz LineLoopSteep
+  jr  DoneLine
+
+drawbox:
+ ld  b,h
+ ld  c,l
+ call FindPixel
+;  ld  b,a
+;makefirstbyte:
+;  or  b
+;  rr  b
+;  jr  nc,makefirstbyte
+;  or  (hl) ;()
+;  ld  (hl),a
+
+  ld  b,e ;yloop
+boxloopy:
+  push bc ;yloop
+  ld  b,d ;xloop
+boxloopx:
+  ld  a,%11111111
+  ld  (hl),a
+  inc hl
+  djnz boxloopx
+
+  ld  b,0
+  ld  a,16
+  sub d
+  ld  c,a
+  add hl,bc
+  pop bc ;yloop
+  djnz boxloopy
+  ret
+
+;-----------------------------
+;----------- link ------------
+;-----------------------------
+
+send:
+  push af ;ld b,a
+  call _getcsc
+  cp  K_EXIT
+  jp  z,Exit
+  pop af ;ld a,b
+  call SendByte
+;  jr  c,send
+  ret
+
+linkfail:
+  jp  Exit
+
+LinkPrep:
+  ex  (sp),hl
+  push bc
+  push de
+  set 2,(iy+$12) ;cursorblink
+  ld  b,8
+  jp  (hl)
+
+receive:
+  call checklink
+  jr  z,linkfail
+  cp  %11
+  jr  z,receive
+ReceiveByteCont:
+  call LinkPrep
+  jr  ReceiveCont
+ReceiveByte:
+  call LinkPrep
+ReceiveBits:
+WaitRecBit:
+  call checklink
+  jr  z,LinkFailed
+  cp  %11
+  jr  z,WaitRecBit
+ReceiveCont:
+  sub %10
+  ld  a,%10 
+  ld  d,D0lD1h
+  jr  c,ReceiveLow
+  rra
+  ld  d,D0hD1l
+ReceiveLow:
+  rr  c
+  ld  (AckBit),a
+  ld  a,d
+  out (7),a
+WaitAckRec:
+  call checklink
+  cp  0
+AckBit =$-1
+  jr  nz,WaitAckRec
+  ld  a,D0hD1h
+  out (7),a
+  ld  d,4
+WaitReadyRec:
+  dec d
+  jr  z,ReadyRec
+  in  a,(7)
+  cp  %11
+  jr  nz,WaitReadyRec
+ReadyRec:
+  djnz ReceiveBits
+  jr  LinkSuccess
+
+SendByte:
+  call LinkPrep
+  ld  c,a
+  inc b
+  jr  SendAcked
+SendBits:
+  rr  c
+
+sendbit:
+  ld  a,D0lD1h
+  jr  nc,sendselected
+  ld  a,D0hD1l
+sendselected:
+  out (7),a
+waitacknowledge:
+  call checklink
+  jr  nz,waitacknowledge
+SendAcked:
+  ld  a,D0hD1h
+  out (7),a
+waitsendready:
+  call checklink
+  cp  %11
+  jr  nz,waitsendready
+
+  djnz SendBits
+LinkSuccess: ;nc
+  .db $f6 ;first byte of "or n"
+LinkFailed:  ;c
+  scf
+  ld  a,c
+  res 2,(iy+$12) ;noblink
+  pop de
+  pop bc
+  pop hl
+  ret
+
+linkerror:
+  jp  Exit
+
+checklink:
+  ld  a,$BF ;key
+  call _readkeypad
+  bit 6,a
+  jr  z,linkerror
+
+  in  a,(7)
+  and %11
+  ret
+
+_readkeypad:
+  out (1),a
+  in  a,(1)
+  push af
+  ld  a,%11111111
+  out (1),a
+  pop af
+  ret
+
+;-----------------------------
+;---------- levels -----------
+;-----------------------------
+
+LevelsDM:
+
+  .db 8,5,15
+  .db $40,30,2,$C0,30,125, $00,2,64,$80,54,64
+  .db 128,57
+  .db 0,0
+
+LevelsDM2:
+
+  .db 8,5,31
+  .db $40,28,2,$C0,28,125, $00,2,63,$80,54,63
+  .db 128,57
+  .db 0
+  .db 4             ;4 boxes
+  .db 4,19,16,14    ;xsize,ysize,xpos,ypos
+  .db 4,19,80,14
+  .db 4,19,16,34
+  .db 4,19,80,34
+
+LevelsR:
+  .db 8,5,24
+  .db $40,3,2,$40,6,2, $40,9,2,$40,12,2
+  .db 128,57
+  .db 0
+  .db 1
+  .db 8,29,32,20
+
+LevelsH:
+
+  .db 8,5,5
+huntersize = 31
+  .db $40,28,2,$C0,28,125, $00,2,63,$80,54,63
+  .db 128,57
+  .db 0
+  .db 4
+  .db 4,19,16,14
+  .db 4,19,80,14
+  .db 4,19,16,34
+  .db 4,19,80,34
 
 
+NUM_LEVELS = 9
 
 
-Levels:
-  .db 5,7           ;5 peas, speed 7
-  .db 64,4,0        ;x,y,d
-  .db 57            ;field height
+LevelsS:
+
+  .db 5,4,15        ;peas,speed,begin_size
+  .db 0,2,63        ;d,y,x
+  .db 128,57        ;field width, height
   .db 0             ;no additional lines
   .db 0             ;no additional lines
+  .db 0             ;no boxes
 
 
-  .db 8,6
-  .db 4,14,40h
-  .db 57
+  .db 8,4,15
+  .db $40,14,2
+  .db 128,57
   .db 1
   .db 1
-  .db 28,28,100,28
+  .db 28,28,100,28  ;line coords: x1,y1,x2,y2
+  .db 0
 
 
-  .db 99,5
-  .db 4,14,40h
-  .db 100
+  .db 9,4,15
+  .db $40,8,2
+  .db 128,57
   .db 2
   .db 2
-  .db 28,28,28,72
-  .db 100,28,100,72
-
-#include trigtab.asm
-#include line.asm
-
-WormMsg .db "Wonderworm!",0
-LevelMsg .db "Level ",0
-LivesMsg .db "Lives: ",0
-ScoreMsg .db "Score: ",0
-LeftMsg  .db "Food: ",0
-HiScore .db 0
-Varname .db 4,"worm"
-
-Left =$
-Lives =$+1
-Level =$+2
-Score =$+3
-
-SinCosTable =09E00h
-ScrBuffer =8200h
-BufSize =0A00h
-GreyBuf =0F400h
-
-.end
+  .db 28,14,100,14
+  .db 28,41,100,41
+  .db 0
+
+  .db 9,3,15
+  .db $40,8,2
+  .db 128,80
+  .db 2
+  .db 64,14,64,66
+  .db 20,40,108,40
+  .db 0
+
+  .db 10,3,15
+  .db 2,8,$40
+  .db 128,90
+  .db 3
+  .db 18,20,18,70
+  .db 110,20,110,70
+  .db 18,45,110,45
+  .db 0
+
+  .db 7,3,15
+  .db 64,4,0
+  .db 128,86
+  .db 6
+  .db 34,13,56,35
+  .db 34,71,56,49
+  .db 72,35,94,13
+  .db 72,49,94,71
+  .db 16,20,16,64
+  .db 110,20,110,64
+  .db 0
+
+  .db 9,2,15
+  .db 4,10,$40
+  .db 128,82
+  .db 3
+  .db 0,20,74,20
+  .db 54,40,128,40
+  .db 0,60,74,60
+  .db 0
+
+  .db 12,2,15
+  .db 64,4,0
+  .db 128,90
+  .db 6
+  .db 20,16,54,16
+  .db 74,16,110,16
+  .db 20,16,20,72
+  .db 110,16,110,72
+  .db 20,72,54,72
+  .db 74,72,110,72
+  .db 0
+
+  .db 8,2,15
+  .db 72,52,$c0
+  .db 128,128
+  .db 13
+  .db 34,56,56,34
+  .db 34,72,56,94
+  .db 72,34,94,56
+  .db 72,94,94,72
+  .db 46,64,80,64
+  .db 22,11,22,33
+  .db 11,22,33,22
+  .db 22,95,22,117
+  .db 11,106,33,106
+  .db 103,11,103,33
+  .db 91,22,115,22
+  .db 103,95,103,117
+  .db 91,106,115,106
+  .db 0
+
+
+;-----------------------------
+;---------- data -------------
+;-----------------------------
+
+TrigPrecalc:
+.db   0,  3,  6,  9, 12, 15, 18, 21
+.db  24, 27, 30, 33, 36, 39, 42, 45
+.db  48, 51, 54, 57, 59, 62, 65, 67
+.db  70, 73, 75, 78, 80, 82, 85, 87
+.db  89, 91, 94, 96, 98,100,102,103
+.db 105,107,108,110,112,113,114,116
+.db 117,118,119,120,121,122,123,123
+.db 124,125,125,126,126,126,126,126
+.db 127
+
+txtWelcome: .db "Welcome to Worm!! ",
+            .db "by Shiar",0
+txtOptions: .db "Options",0
+txtGame:    .db "Singleplayer",0  ;0
+txtGame2:   .db "Deathmatch  ",0  ;1
+            .db "Foodmatch ",0    ;2
+            .db "LinkMatch",0     ;3 (>options)
+            .db "Hunting  ",0     ;4
+            .db "Race   ",0       ;5
+            .db "CTF ",0          ;6
+            .db "Domination",0    ;7
+txtWaiting: .db "Waiting...",0
+txtReceive: .db "Receiving...",0
+
+WormVersion = 092
+WormMsg:  .db "WORM by SHIAR -- test version",0
+txtLevel: .db "Level ",0
+txtWorms: .db "Worms: 0",0 ;follows txtLevel
+txtDied:  .db "Died ",0
+txtScore: .db "Score",0  ;follows txtDied
+txtLeft:  .db " left",0   ;follows txtScore
+txtReady: .db "Prepare!",0
+txtposReady = 7
+txtGO:    .db "----- GAME OVER -----",0
+HiScore:  .db 0
+
+gamesdata:
+
+_datalink  = %00000001
+_datalivel = %00000010 ;ix+lives=0 limit
+_datafoodl = %00000100 ;left=0 limit
+_datanextl = %00001000 ;next level if left=0
+_datafood  = %00010000 ;food present
+_datahunt  = %00100000
+_datalaps  = %01000000 ;give lap score
+_datascore = %10000000 ;score>=100 limit
+
+datasingle: .db %00011110
+            .dw LevelsS
+datadeathm: .db %00000010
+            .dw LevelsDM2
+datafoodm:  .db %10010000
+            .dw LevelsDM
+datalinkm:  .db %00000011
+            .dw LevelsDM
+datahuntin: .db %00110100
+            .dw LevelsH
+datarace:   .db %11000000
+            .dw LevelsR
+datactf:    .db %00000000
+            .dw LevelsDM
+datadomin:  .db %00000000
+            .dw LevelsDM
+
+
+setdata = 18
+resbit  = 2 ;and%11111011
+worm1set:  .dw $B000,$B000 ;%10110000
+           .db 3,0,%01111110,%10,%100 ;< >
+           .db "Worm #1",0
+worm2set:  .dw $B800,$B800 ;%10111000
+           .db 3,0,%00111111,%10000,%1000 ;f1 f2
+           .db "Worm #2",0
+worm3set:  .dw $E800,$E800 ;%11101000 ;$D748+$1000+
+           .db 3,0,%01011111,%10,%100 ;sto ,
+           .db "Worm #3",0
+worm4set:  .dw $F000,$F000 ;%11110000
+           .db 3,0,%01111101,%10,%1 ;enter +
+           .db "Worm #4",0
+worm1 = $B400
+worm2 = $B430
+worm3 = $B460
+worm4 = $B490
+end:
+              ;set:
+heading = 0   ;level*
+pos     = 2   ;level*
+pos2    = 4   ;level
+grow    = 6   ;level
+died    = 8   ;game
+score   = 9   ;game
+delay   = 11  ;game
+
+head    = 12  ;level
+tail    = 14  ;level
+lives   = 16  ;game
+reserv  = 17  ;loop
+ ;race:lap
+ ;hunt:time
+input   = 18  ;&
+left    = 19  ;&
+right   = 20  ;&
+name    = 21  ;game
+
+Level =$+1
+DispBuffer =$+2 ;912 bytes
+
+ScrBuffer   = $8200 ;-$A1FF (32x256)
+SinCosTable = $A200 ;-$A300 (4x64)
+
+;-----------------------------
+;----------- end -------------
+;-----------------------------
+
+.end
\ No newline at end of file