worm 0.91.0524: further link improvements
[wormy.git] / worm.z80
index b4b4373104cca44d88adf7a46617005fb310b216..22e6fec05c517897bfb9482deec68ed6210d3bc1 100644 (file)
--- a/worm.z80
+++ b/worm.z80
@@ -1,7 +1,7 @@
 ; Title                      : Worm
 ; Title                      : Worm
-; Version                    : 90%
-; Release Date               : april 2000???
-; Filename                   : worm.86p (4kb)
+; Version                    : 91%
+; Release Date               : may 2000
+; Filename                   : worm.86p (5kb)
 ; Author(s)                  : Shiar
 ; Email Address              : shiar@mailroom.com
 ; ICQ UIN                    : #43840958
 ; Author(s)                  : Shiar
 ; Email Address              : shiar@mailroom.com
 ; ICQ UIN                    : #43840958
 ;----------- TO-DO -----------
 ;-----------------------------
 
 ;----------- TO-DO -----------
 ;-----------------------------
 
-; 90% = DONE
+; 91% = DONE
 
 
-;  3% * linkplay
+;  2% * fix deaths linkplay and transmit game/level data
 ;  2% * make linkplay available for all gametypes (not just deathmatch)
 ;  2% * make linkplay available for all gametypes (not just deathmatch)
-;     * 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
+;  1% * fix pea XOR problem in ctf (+dom?)
+;     * fix wormstop
+;  1% * domination?: take control points by running over them and hold them
+;     * customizable keys
 ;     * complete readme (+custom level info)
 ;  2% * ... (pollish, &&&)
 
 ;     * complete readme (+custom level info)
 ;  2% * ... (pollish, &&&)
 
 ;-----------------------------
 ;-----------------------------
 
 ;-----------------------------
 ;-----------------------------
 
-#define buffer  ;use display buffer (otherwise write directly to screen)
+#define buffer      ;use display buffer (otherwise write directly to screen)
+#define readymask   ;"grays" out the field before starting a level
+
+;#define readytext  ;displays "prepare" before level starts
+;#define invincible ;worms cannot die =)
 
 #define cal call
 #define psh push
 
 #define cal call
 #define psh push
@@ -72,6 +77,8 @@ _RAM_PAGE_7        = $47F3
 _PTEMP_END         = $D29A ;end of VAT
 _load_ram_ahl      = $462F ;ahl->page+hl
 _writeb_inc_ahl    = $5567 ;ld (ahl),c
 _PTEMP_END         = $D29A ;end of VAT
 _load_ram_ahl      = $462F ;ahl->page+hl
 _writeb_inc_ahl    = $5567 ;ld (ahl),c
+_jforce            = $409C ;TI-OS stack restored
+_EXLP              = $4493 ;swap (hl),(de) b times
 
 ;-----------------------------
 ;------- data  storage -------
 
 ;-----------------------------
 ;------- data  storage -------
@@ -115,7 +122,7 @@ DispBuffer  = $FC70
 
 .org _asm_exec_ram
 
 
 .org _asm_exec_ram
 
-WormVersion = 090
+WormVersion = 091
 
 start:
   nop
 
 start:
   nop
@@ -125,7 +132,7 @@ start:
   .dw WormIcon
 
 WormMsg:
   .dw WormIcon
 
 WormMsg:
-  .db "WORM by SHIAR -- pre-beta 90%",0
+  .db "WORM by SHIAR -- pre-beta 91%",0
 WormIcon:
   .db 9,2
   .db %10010110,%01101111
 WormIcon:
   .db 9,2
   .db %10010110,%01101111
@@ -139,7 +146,7 @@ WormIcon:
   .db %11000011,%10000000
 
 levelhead  = 'w'
   .db %11000011,%10000000
 
 levelhead  = 'w'
-levelhead2 = 90 ;worm levels header = "90"
+levelhead2 = 91 ;worm levels header = "91"
 
 Start:
   ld  (SpSave),sp
 
 Start:
   ld  (SpSave),sp
@@ -381,10 +388,10 @@ disptitleloop:
   cal _puts ;---
   dec e ;$0206
   ld  (_curRow),de
   cal _puts ;---
   dec e ;$0206
   ld  (_curRow),de
-  cal _puts ;Level
+  cal _puts ;Level <00>
   dec e ;$0205
   ld  (_curRow),de
   dec e ;$0205
   ld  (_curRow),de
-  cal _puts ;Worms: 2
+  cal _puts ;Worms <2>
   dec e ;$0204
   ld  (_curRow),de
   ld  hl,(CURtxtGame)
   dec e ;$0204
   ld  (_curRow),de
   ld  hl,(CURtxtGame)
@@ -404,6 +411,8 @@ select:
 notselect
   cp  K_EXIT
   jp  z,ExitNoStats
 notselect
   cp  K_EXIT
   jp  z,ExitNoStats
+  cp  K_LEFT
+  jr  z,selleft
   cp  K_RIGHT
   ld  a,b
   jr  nz,mainMenu
   cp  K_RIGHT
   ld  a,b
   jr  nz,mainMenu
@@ -421,18 +430,32 @@ changelevel:
   ld  hl,nrlevels
   add hl,de
   ld  d,(hl) ;max level for sel.game
   ld  hl,nrlevels
   add hl,de
   ld  d,(hl) ;max level for sel.game
+curlevel =$+1
   ld  a,1
   ld  a,1
-curlevel =$-1
   inc a
   cp  d
   jr  c,changedlevel
   inc a
   cp  d
   jr  c,changedlevel
-  ld  a,1
+  ld  a,d
 changedlevel:
   ld  (curlevel),a
 changedlevel:
   ld  (curlevel),a
-dispcurlevel:
-  ld  hl,$0806
+_dispcurlevel:
+  ld  hl,$0906
   ld  (_curRow),hl
   cal showA
   ld  (_curRow),hl
   cal showA
+_mainMenu:
+  jr  mainMenu
+
+selleft:
+  ld  a,b
+  dec a
+  jr  z,bchangenrworms
+  dec a
+  jr  nz,mainMenu
+
+bchangelevel:
+  ld  a,(curlevel)
+  dec a
+  jr  nz,changedlevel
   jr  mainMenu
 
 changegame:
   jr  mainMenu
 
 changegame:
@@ -454,33 +477,42 @@ okilydokily:
   cal _puts
   ld  (NEXTtxtGame),hl
 howmanyworms:
   cal _puts
   ld  (NEXTtxtGame),hl
 howmanyworms:
+  ld  hl,nrworms
   ld  a,(Gametype)
   cp  2
   ld  a,(Gametype)
   cp  2
-  ld  a,1
-  jr  c,oneworm
-nrworms =$+1
-  ld  a,2
-oneworm:
-  jr  dispnrworms ;jr mainMenu
+  ld  (hl),1
+  jr  c,dispnrworms
+  inc (hl) ;2
+  jr  dispnrworms ;mainMenu
 
 changenrworms:
   ld  a,(Gametype)
 
 changenrworms:
   ld  a,(Gametype)
-  cp  2
-  jr  c,mainMenu ;type 0/1
+  cp  2 ;&&
+  jr  c,_mainMenu ;type 0/1
   ld  hl,nrworms
   ld  a,(hl)
   inc a
   cp  5
   ld  hl,nrworms
   ld  a,(hl)
   inc a
   cp  5
-  jr  c,changednrworms
-  ld  a,1
+  jr  nc,dispcurlevel
 changednrworms:
   ld  (hl),a
 dispnrworms:
 changednrworms:
   ld  (hl),a
 dispnrworms:
+  ld  a,(hl) ;hl=nrworms
   ld  hl,$0905
   ld  (_curRow),hl
   ld  hl,$0905
   ld  (_curRow),hl
-  cal showA
+  add a,'0'
+  cal _putc
+dispcurlevel
   ld  a,(curlevel)
   ld  a,(curlevel)
-  jr  dispcurlevel ;mainMenu
+  jr  _dispcurlevel ;mainMenu
+
+bchangenrworms:
+  ld  hl,nrworms
+  ld  a,(hl)
+  dec a
+  cp  2 ;2-4
+  jr  nc,changednrworms
+  jr  dispcurlevel
 
 menucall:
   psh bc
 
 menucall:
   psh bc
@@ -534,20 +566,25 @@ changeworms:
   cp  2
   ld  a,1
   jr  c,wormnrname
   cp  2
   ld  a,1
   jr  c,wormnrname
-  ld  a,(nrworms)
+nrworms =$+1
+  ld  a,1
 wormnrname:
   add a,'0'
   cal _putc
   sub '0'
 wormnrname:
   add a,'0'
   cal _putc
   sub '0'
+
+  ld  d,a  ;1x
   add a,a  ;2x
   ld  e,a
   add a,a  ;4x
   add a,a  ;8x
   add a,a ;16x
   add a,e ;18x
   add a,a  ;2x
   ld  e,a
   add a,a  ;4x
   add a,a  ;8x
   add a,a ;16x
   add a,e ;18x
+  add a,d ;19x
   ld  e,a
   ld  e,a
-  ld  d,0
-  ld  ix,worm1name-18
+  ld  d,0 ;de=a
+
+  ld  ix,worm1name-19
   add ix,de
   ld  a,maxnamelength
   cal entername
   add ix,de
   ld  a,maxnamelength
   cal entername
@@ -556,7 +593,7 @@ wormnrname:
 
 entername:
   ld  h,1
 
 entername:
   ld  h,1
-  ld  l,h
+  ld  l,h ;)
   ld  (_curRow),hl
   ld  (namelength),a
 enternameloop:
   ld  (_curRow),hl
   ld  (namelength),a
 enternameloop:
@@ -620,12 +657,12 @@ waitnokeypressed:
 
 chartable:
   .db 0,".<>!",0,0,0,0  ;down,L,R,up
 
 chartable:
   .db 0,".<>!",0,0,0,0  ;down,L,R,up
-  .db 0,"xtoje0",0      ;enter..clear
-  .db " wsnid9",0       ;(-)..custom
-  .db "zvrmhc8",0       ;dot..del
-  .db "yuqlgb7#"        ;0..xvar
-  .db 0,"-pkfa6'"       ;on..alpha
-  .db "54321*",0,0      ;F5..more
+  .db 0,"XTOJE0",0      ;enter..clear
+  .db " WSNID9",0       ;(-)..custom
+  .db "ZVRMHC8",0       ;dot..del
+  .db "YUQLGB7#"        ;0..xvar
+  .db $D9,"-PKFA6'"     ;on..alpha
+  .db "54321*",0,$D0    ;F5..more
 
 ;--proc
 
 
 ;--proc
 
@@ -656,6 +693,17 @@ skipworms:
 skipballs:
   inc hl
   dnz skipballs
 skipballs:
   inc hl
   dnz skipballs
+
+skipflags:
+  ld  a,(Gametype)
+  cp  gamectf
+  jr  nz,noflagstoskip
+  inc hl
+  inc hl
+  inc hl
+  inc hl
+noflagstoskip:
+
   cal skiplines ;lines
 skiplines: ;boxes
   ld  a,(hl) ;lines/boxes
   cal skiplines ;lines
 skiplines: ;boxes
   ld  a,(hl) ;lines/boxes
@@ -663,6 +711,7 @@ skiplines: ;boxes
   add a,a
   inc a
   ld  b,a ;4x(hl)+1
   add a,a
   inc a
   ld  b,a ;4x(hl)+1
+
 skiplb
   inc hl
   dnz skiplb
 skiplb
   inc hl
   dnz skiplb
@@ -722,28 +771,68 @@ linkmatch:
   cal _clrWindow
   ld  a,WormVersion
   cal send
   cal _clrWindow
   ld  a,WormVersion
   cal send
-  jr  c,client ;2nd
+  jr  nc,client ;2nd
 
 host:
   ld  hl,txtWaiting
   cal _puts
   cal receive
   cp  WormVersion
 
 host:
   ld  hl,txtWaiting
   cal _puts
   cal receive
   cp  WormVersion
-  jp  nz,LinkBreak
+;  jp  nz,linkiniterror
   cal send
   cal send
+ jr c,linkiniterror
+
+;  cal sendname
+;  cal receivename
 
 
-  ld  hl,SwapPos
-  ld  (hl),$f6
+  ld  a,$18
   jr  multiplayer
 
   jr  multiplayer
 
+linkiniterror:
+  pop hl
+  jp  DisplayMenu
+
+sendname:
+  ld  hl,worm1+name
+  ld  b,maxnamelength
+sendnameloop:
+  ld  a,(hl)
+  inc hl
+  psh bc
+  psh hl
+  cal send
+  pop hl
+  pop bc
+;  dnz sendnameloop
+  ret
+receivename:
+  ld  hl,worm2+name
+  ld  b,maxnamelength
+recvnameloop:
+  psh bc
+  psh hl
+  cal receive
+  pop hl
+  pop bc
+  ld  (hl),a
+  inc hl
+;  dnz recvnameloop
+  ret
+
 client:
   ld  hl,txtReceive
   cal _puts
 client:
   ld  hl,txtReceive
   cal _puts
-  cal receive
+  cal Qreceive
+  jp  c,linkiniterror
   cp  WormVersion
   cp  WormVersion
-  jp  nz,LinkBreak
+;  jp  nz,linkiniterror
+
+;  cal receivename
+;  cal sendname
 
 
+  ld  a,$f6
 multiplayer:
 multiplayer:
+  ld  (SwapPos),a
   ld  a,2
   ld  (nrworms),a
 
   ld  a,2
   ld  (nrworms),a
 
@@ -753,29 +842,25 @@ multiplayer:
 
 GameOver:
   cal _clrLCD
 
 GameOver:
   cal _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  hl,worm1set
   ld  hl,worm1set
-  ld  de,worm1+head
+  ld  de,worm1
   ld  a,4 ;4x (all worms)
 createwormsloop:
   ld  a,4 ;4x (all worms)
 createwormsloop:
-  ld  bc,19
-  ldir ;copy 19 bytes
   ex  de,hl
   ex  de,hl
-  ld  bc,head
+  ld  bc,died
   add hl,bc
   add hl,bc
-  ex  de,hl ;de=wormX+head
-  dec a     ;loop
+  ld  (hl),0 ;died=0
+  inc hl
+  ld  (hl),0 ;score=0
+  inc hl
+  ld  (hl),0 ;score+1=0
+  inc hl
+  ld  (hl),2 ;delay=2
+  inc hl
+  ex  de,hl  ;de=wormX+head
+  ld  bc,19
+  ldir       ;copy 19 bytes
+  dec a      ;loop
   jr  nz,createwormsloop
 
   ld  a,(Gametype)
   jr  nz,createwormsloop
 
   ld  a,(Gametype)
@@ -792,10 +877,7 @@ StartLevel:
   jp  nz,nextlevel
 
   psh hl
   jp  nz,nextlevel
 
   psh hl
-  ld  b,150
-waitsomemore:
-  halt
-  dnz waitsomemore
+  cal releasekeys
   cal _clrWindow
   pop hl
 ;show end msg or smtn
   cal _clrWindow
   pop hl
 ;show end msg or smtn
@@ -849,7 +931,9 @@ setsprite:
 toobad_noballs:
 
   ex  de,hl
 toobad_noballs:
 
   ex  de,hl
+#ifndef invincible
   ld  (thislevel),de
   ld  (thislevel),de
+#endif
 
   ld  hl,worm1
   ld  a,(gameCar)
 
   ld  hl,worm1
   ld  a,(gameCar)
@@ -949,9 +1033,21 @@ NoVertShift:
   ld  c,64
   ldir
 
   ld  c,64
   ldir
 
-;-draw lines-
 
   pop hl                         ; << levelp
 
   pop hl                         ; << levelp
+  ld  a,(Gametype)
+  cp  gamectf
+  jr  nz,noctf
+  ld  de,peaspos
+  ld  bc,4
+  ldir
+  psh hl
+  cal DrawAllPeas
+  pop hl
+noctf:
+
+;-draw lines-
+
   ld  a,(hl)
   inc hl
   or  a
   ld  a,(hl)
   inc hl
   or  a
@@ -1001,16 +1097,6 @@ noboxes:
 
   psh hl                        ; >> levelp new
   cal showstats
 
   psh hl                        ; >> levelp new
   cal showstats
-  ld  a,(Gametype)
-  cp  gamectf
-  jr  nz,noctf
-  ld  hl,$1010
-  ld  (peaspos),hl
-  cal DrawPea
-  ld  hl,$2010
-  ld  (peaspos+3),hl
-  cal DrawPea
-noctf:
   ld  a,(gameCar)
   and _datafood
   jr  z,nofood
   ld  a,(gameCar)
   and _datafood
   jr  z,nofood
@@ -1018,6 +1104,27 @@ noctf:
 nofood:
   ld  bc,(worm1+pos)
   cal DisplayField
 nofood:
   ld  bc,(worm1+pos)
   cal DisplayField
+
+#ifdef readymask
+  ld  hl,$FC70
+  ld  d,%10101010
+  ld  c,56
+maskloop:
+  ld  a,d
+  xor %11111111
+  ld  d,a
+  ld  b,$10
+maskline:
+  ld  a,(hl)
+  or  d
+  ld  (hl),a
+  inc hl
+  dnz maskline
+  dec c
+  jr  nz,maskloop
+#endif
+
+#ifdef readytext
   ld  hl,$FDE0
   ld  de,$FDE1
   ld  (hl),%11111111
   ld  hl,$FDE0
   ld  de,$FDE1
   ld  (hl),%11111111
@@ -1029,36 +1136,38 @@ nofood:
   ld  hl,txtReady
   cal _puts
   res 3,(iy+5)
   ld  hl,txtReady
   cal _puts
   res 3,(iy+5)
+#endif
 
   ld  a,0
 gameCar =$-1
   and _datalink
 
   ld  a,0
 gameCar =$-1
   and _datalink
-  jr  z,SwapPos ;no link
+  jr  z,initfinished ;no link
+
   xor a
   xor a
-  ld  (worm2+input),a
+  ld  (worm2+input),a ;worm 2 via link
   ld  (worm2+left),a
   ld  (worm2+left),a
-  ld  (Speed),a
-
+  ld  (Speed),a ;max.speed
 SwapPos: ;$18 xx -> $F6 xx
 SwapPos: ;$18 xx -> $F6 xx
+         ; jr xx ->  or xx
   jr  initfinished
   inc a
   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
+  ld  (worm2+left),a ;1
+  ld  hl,worm1
+  ld  de,worm2
+  ld  b,4 ;+heading +pos
+  cal _EXLP ;swap positions
+;&&& over link
+  ld  hl,worm1+name
+  ld  de,worm2+name
+  ld  b,maxnamelength
+  cal _EXLP ;swap positions
 initfinished:
 
 initfinished:
 
-  ld  b,0
+  ld  b,startdelay
 ReadyDelay:
   halt
   dnz ReadyDelay
 ReadyDelay:
   halt
   dnz ReadyDelay
+  cal releasekeys
 
 ;-----------------------------
 ;----------- LOOP ------------
 
 ;-----------------------------
 ;----------- LOOP ------------
@@ -1133,6 +1242,9 @@ CheckExit:
   jr  Exit
 
 WormDead:
   jr  Exit
 
 WormDead:
+#ifdef invincible
+  jp  stopworm
+#else
   ld  a,2
   ld  (flashtime),a
   ld  (ix+delay),respawndelay
   ld  a,2
   ld  (flashtime),a
   ld  (ix+delay),respawndelay
@@ -1161,10 +1273,13 @@ thislevel =$+1
   ld  a,$A7 ;exit@end of turn
   ld  (CheckExit),a ;set exit state
   ret ;finish turn
   ld  a,$A7 ;exit@end of turn
   ld  (CheckExit),a ;set exit state
   ret ;finish turn
+#endif
 
 Exit:
   ld  sp,0 ;pop all
 SpSave = $-2
 
 Exit:
   ld  sp,0 ;pop all
 SpSave = $-2
+  ld  a,D0HD1H
+  out (7),a
   cal _clrWindow
   ld  hl,txtGO
   cal _puts
   cal _clrWindow
   ld  hl,txtGO
   cal _puts
@@ -1192,13 +1307,13 @@ displayWormStats:
   ld  bc,input-died
   add hl,bc ;+input
   xor a
   ld  bc,input-died
   add hl,bc ;+input
   xor a
-  cp  (hl)
+  cp  (hl)  ;input=0 = link
   jr  nz,NoLinkIndic
   jr  nz,NoLinkIndic
-  ld  b,7
+  ld  b,7 ;{DOWN}
   inc hl ;+left
   cp  (hl)
   jr  z,hostLinkIndic
   inc hl ;+left
   cp  (hl)
   jr  z,hostLinkIndic
-  dec b
+  dec b   ;{UP}
 hostLinkIndic:
   ld  a,8
   ld  (_curCol),a
 hostLinkIndic:
   ld  a,8
   ld  (_curCol),a
@@ -1218,9 +1333,9 @@ NoLinkIndic:
   ld  a,13
   ld  (_curCol),a
   ld  a,(hl) ;worm+died
   ld  a,13
   ld  (_curCol),a
   ld  a,(hl) ;worm+died
-  psh hl
   cal showA
   pop hl
   cal showA
   pop hl
+  psh hl
 
   ld  a,16
   ld  (_curCol),a
 
   ld  a,16
   ld  (_curCol),a
@@ -1240,7 +1355,6 @@ NoLinkIndic:
 checkhilevel:
   ld  hl,nrlevels
   ld  a,(Level)
 checkhilevel:
   ld  hl,nrlevels
   ld  a,(Level)
-  inc a
   cp  (hl)
   jr  c,hilevelcheckdone
   ld  (hl),a    ;save local
   cp  (hl)
   jr  c,hilevelcheckdone
   ld  (hl),a    ;save local
@@ -1287,13 +1401,16 @@ NotNewHigh: ;de=current hiscore
   cal showHL
 hiscorecheckdone:
 
   cal showHL
 hiscorecheckdone:
 
+  cal releasekeys
 waitkey:
   halt
   halt
   cal GET_KEY
   cp  K_ENTER
   jp  z,DisplayMenu
 waitkey:
   halt
   halt
   cal GET_KEY
   cp  K_ENTER
   jp  z,DisplayMenu
-;  cp  K_EXIT
+  cp  K_SECOND
+  jp  z,DisplayMenu
+  cp  K_EXIT
   jr  nz,waitkey
 
 ;x123456789012345678901
   jr  nz,waitkey
 
 ;x123456789012345678901
@@ -1323,6 +1440,7 @@ ExitNoStats:
   cal _SET_MM_NUM_BYTES
   cal _mm_ldir
 
   cal _SET_MM_NUM_BYTES
   cal _mm_ldir
 
+  cal releasekeys
   res 4,(iy+9)
   set 2,(iy+13)
   jp  _clrWindow
   res 4,(iy+9)
   set 2,(iy+13)
   jp  _clrWindow
@@ -1347,53 +1465,6 @@ addlevelposition:
 ;----------- worm ------------
 ;-----------------------------
 
 ;----------- worm ------------
 ;-----------------------------
 
-inlink:
-  ld  a,0
-sendbyte =$-1
-  ld  b,(ix+left)
-  dec b
-  jr  z,receivefirst
-  cal send
-  cal receive
-  ld  l,a
-  ret
-receivefirst:
-  psh af
-  cal receive
-  ld   l,a
-  pop  af
-  psh hl
-  cal send
-  pop  hl
-  ret
-
-inkeys: ;use jp not call!
-  out (1),a ;nop\nop
-  in  a,(1)
-  ld  b,a
-  and (ix+right)
-  jr  z,notright
-  ld  a,l
-  add a,8
-  ld  l,a
-notright:
-  ld  a,b
-  and (ix+left)
-  ret z
-  ld  a,l
-  sub 8
-  ld  l,a
-  ret
-
-chkinput:
-  ld  bc,donediddelydone
-  psh bc ;ret-dest.
-inputcall:
-  ld  a,(ix+input)
-  or  a
-  jr  nz,inkeys
-  jr  inlink
-
 respawncheck:
   cp  respawndelay-1
   jr  nz,unnamedlabel
 respawncheck:
   cp  respawndelay-1
   jr  nz,unnamedlabel
@@ -1438,11 +1509,65 @@ unnamedlabel:
 respawndue:
   ld  l,a
   cal inputcall
 respawndue:
   ld  l,a
   cal inputcall
+  ld  (sendbyte),a
   ld  a,h ;previous
   cp  l   ;changed?
   ret z
   ld  a,h ;previous
   cp  l   ;changed?
   ret z
+  ld  (ix+delay),a ;=0
+  ret
 saverespawncounter:
   ld  (ix+delay),a
 saverespawncounter:
   ld  (ix+delay),a
+  jr  inputcall
+;  ld  a,(ix+input)
+;  or  a
+;  jr  z,inlink
+;  ret
+
+inkeys: ;use jp not call!
+  out (1),a ;nop\nop
+  in  a,(1)
+  ld  b,a
+  and (ix+right)
+  jr  z,notright
+  ld  a,l
+  add a,8
+  ld  l,a
+notright:
+  ld  a,b
+  and (ix+left)
+  ret z
+  ld  a,l
+  sub 8
+  ld  l,a
+  ret
+
+inputcall:
+  ld  a,(ix+input)
+  or  a
+  jr  nz,inkeys
+
+inlink:
+  ld  a,0
+sendbyte =$-1
+  ld  b,(ix+left)
+  dec b
+  jr  z,receivefirst
+  psh hl
+  cal send
+  cal receive
+  pop hl
+  ld  l,a
+  ret
+receivefirst:
+  psh af
+  psh hl
+  cal receive
+  pop hl
+  ld  l,a
+  pop af
+  psh hl
+  cal send
+  pop hl
   ret
 
 ;------- handle worm ---------
   ret
 
 ;------- handle worm ---------
@@ -1450,10 +1575,10 @@ saverespawncounter:
 HandleWorm:
   ld  a,(ix+delay)
   dec a
 HandleWorm:
   ld  a,(ix+delay)
   dec a
-  jr  nz,respawncheck
+  jp  nz,respawncheck
 
   ld  l,(ix+heading)
 
   ld  l,(ix+heading)
-  jr  chkinput
+  cal inputcall
 donediddelydone:
   ld  a,l
   ld  (sendbyte),a
 donediddelydone:
   ld  a,l
   ld  (sendbyte),a
@@ -1583,6 +1708,7 @@ peagrowth =$-1
   pop hl                         ; << call
   pop hl                         ; << call
   pop hl                         ; << levelp new
   pop hl                         ; << call
   pop hl                         ; << call
   pop hl                         ; << levelp new
+  ld  (ix+delay),2
   jp  StartLevel
 
 chkpeahit: ;hl=peapos
   jp  StartLevel
 
 chkpeahit: ;hl=peapos
@@ -1605,37 +1731,65 @@ flagcaptured:
   ld  de,30
   cal IncScore
   pop hl
   ld  de,30
   cal IncScore
   pop hl
+sillylabel:
   cal WormDead
 DrawAllPeas:
   ld  hl,(peaspos)
   cal DrawPea
   cal WormDead
 DrawAllPeas:
   ld  hl,(peaspos)
   cal DrawPea
-  ld  hl,(peaspos+3)
+  ld  hl,(peaspos+2)
   jp  DrawPea
 
 multiple_peas:
   jp  DrawPea
 
 multiple_peas:
-  ld  hl,(peaspos)
+  ld  hl,(peaspos) ;1st pea
+  psh hl
   ld  a,(ix+reserv)
   ld  e,a ;push a
   and %01
   ld  a,(ix+reserv)
   ld  e,a ;push a
   and %01
-  jr  nz,corrrectpea
-  ld  hl,(peaspos+3)
-corrrectpea:
+  jr  nz,sel_ownpea
+  ld  hl,(peaspos+2) ;2nd pea
+sel_ownpea:
   cal chkpeahit
   cal chkpeahit
-  jp  nc,WormDead
+  pop hl ;(peapos) 1st pea
+  jp  c,stopworm ;own pea hit
+
+  ld  a,e ;peek a (that x86 asm for pop\push ;)
+  and %01
+  jr  z,sel_otherpea
+  ld  hl,(peaspos+2) ;2nd pea
+sel_otherpea:
+  cal chkpeahit
+  jp  c,hitflag
+
+;no peas hit
+  ld  a,e
+  cal flagtoken
+  jp  z,WormDead
+  ld  b,a ;%10
+  srl b   ;%01
+  add a,b ;%11
+  ld  (ix+reserv),a
+  cal DrawPea ;restore own flag
+  jr  sillylabel ;inv both\die
+
+hitflag: ;correct pea hit
   ld  a,e ;pop a
   xor %01 ;0=1;1=0
   ld  (ix+reserv),a
   ld  a,e ;pop a
   xor %01 ;0=1;1=0
   ld  (ix+reserv),a
-  and %01
-  add a,a
-  ld  b,a
-  ld  a,e
-  and %10
-  cp  b
+  cal flagtoken
   psh af ;safe z-flag
   cal DrawPea ;remove
   pop af
   jr  z,flagcaptured
   psh af ;safe z-flag
   cal DrawPea ;remove
   pop af
   jr  z,flagcaptured
-  jr  Drawworm
+  jr  stopworm
+
+flagtoken:
+  and %01 ;current
+  add a,a ;<< for cp
+  ld  b,a ;in b
+  ld  a,e
+  and %10 ;own
+  cp  b   ;same?
+  ret ;Z=yes: no flag taken
 
 ;-----------------------------
 
 
 ;-----------------------------
 
@@ -1699,8 +1853,9 @@ chkworm:
  pop ix
   ret z ;not hit
   pop bc ;call
  pop ix
   ret z ;not hit
   pop bc ;call
-previouspos =$+1
+stopworm:
   ld  bc,0
   ld  bc,0
+previouspos =$-2
   ld  de,0
   ld  (ix+pos),c
   ld  (ix+pos+1),b
   ld  de,0
   ld  (ix+pos),c
   ld  (ix+pos+1),b
@@ -1860,6 +2015,16 @@ checkballhit:
 ;----------- procs -----------
 ;-----------------------------
 
 ;----------- procs -----------
 ;-----------------------------
 
+releasekeys:
+  halt
+  ld  a,%10000000    ;all key-masks
+  out (1),a
+  in  a,(1)
+  inc a              ;cp %11111111 (no keys pressed)
+  jr  nz,releasekeys ;keep waitin
+  cal GET_KEY        ;clear buffer
+  ret
+
 resbit:
   ld  a,h
   and (ix+storepos)
 resbit:
   ld  a,h
   and (ix+storepos)
@@ -2535,12 +2700,68 @@ boxloop:
 
 TIMEOUT = $1000
 
 
 TIMEOUT = $1000
 
-receive:
-  cal GET_KEY
-  cp  K_EXIT
-  jp  z,LinkBreak
+checklink:
+  pop hl
+  dec de
+  ld  a,d
+  or  e
+  jr  z,LinkFailed
+
+  ld  a,$BF
+  out (1),a
+; nop \ nop
+  in  a,(1)
+  psh af
+  ld  a,%11111111
+  out (1),a
+  pop af
+  bit 6,a
+  jp  z,Exit
+
   in  a,(7)
   and %11
   in  a,(7)
   and %11
+  jp  (hl)
+
+;--------------
+;---- SEND ---- 8 bits in a
+;-------------- destr:bcdehl
+send:
+  ld  c,a
+  ld  b,8+1        ;bits to send
+  jr  checksend
+sendloop:
+  rr  c            ;bit to send (in cf)
+  ld  a,D0LD1H     ;0: lower white
+  jr  nc,sendbit
+  ld  a,D0HD1L     ;1: lower red
+sendbit:
+  out (7),a        ;lower one (send bit)
+  ld  de,TIMEOUT
+waitacksend:
+  cal checklink    ;other calc must lower other whire
+  jr  nz,waitacksend
+checksend:
+  ld  a,D0HD1H     ;ok to raise both
+  out (7),a
+  ld  de,TIMEOUT
+finishsend:
+  cal checklink
+  cp  %11          ;both raised (by other calc)
+  jr  nz,finishsend
+  dnz sendloop     ;repeat for all bits
+ .db  $F6 ;or NN   ;nc
+LinkFailed:
+  scf              ;c=error
+  ret
+
+;--------------
+;---- RECV ---- 8 bits to a
+;-------------- destr:bcdehl
+receive:
+ halt ;&&&
+  ld  de,TIMEOUT
+  cal checklink
+  jr  z,receive
   cp  %11
   jr  z,receive
   cal Qreceive
   cp  %11
   jr  z,receive
   cal Qreceive
@@ -2548,104 +2769,39 @@ receive:
   ret
 
 Qreceive:
   ret
 
 Qreceive:
-  ld  b,8
-  jr  ReceiveCont
+  ld  b,8          ;bits to receive
 receiveloop:
   ld  de,TIMEOUT
 WaitRecBit:
 receiveloop:
   ld  de,TIMEOUT
 WaitRecBit:
-  cal CheckLink
-  jr  z,LinkFailed
+  cal checklink
+  jr  z,LinkFailed ;both low = error
   cp  %11
   cp  %11
-  jr  z,WaitRecBit
-ReceiveCont:
-  sub 2
-  ld  a,2
+  jr  z,WaitRecBit ;both on = nothing sent
+recvbit:
+  rra              ;received bit in cf
+  ld  a,%10
   ld  d,D0LD1H
   ld  d,D0LD1H
-  jr  c,ReceiveLow
-  rra
-  ld  d,D0HD1L
-ReceiveLow:
-  rr c
-  ld (AckBit),a
-  ld a,d
-  out (7),a
+  jr  c,savebit    ;lower white as well
+  rra ;ld a,%01
+  ld  d,D0HD1L     ;lower red
+savebit:
+  rr  c            ;save bit
+  ld  (AckBit),a
+  ld  a,d
+  out (7),a        ;lower other (both low)
   ld  de,TIMEOUT
   ld  de,TIMEOUT
-WaitAckRec:
-  cal CheckLink
+waitackrecv:
+  cal checklink
+;  jr  z,waitackrecv ;wait for one to go high
   cp  0
 AckBit =$-1
   cp  0
 AckBit =$-1
-  jr  nz,WaitAckRec
+  jr  nz,waitackrecv
   ld  a,D0HD1H
   ld  a,D0HD1H
-  out (7),a
-  ld  d,4
-WaitReadyRec:
-  dec d
-  jr  z,ReadyRec
-  in  a,(7)
-  cp  %11
-  jr  nz,WaitReadyRec
-ReadyRec:
-  dnz receiveloop
-  jr  LinkSuccess
-
-send:
-  ld  b,9
-  ld  c,a
-  jr  SendAcked
-SendBits:
-  rr  c
-  ld  a,D0LD1H
-  jr  nc,SendLow
-  ld  a,D0HD1L
-SendLow:
-  out (7),a
-  ld  de,TIMEOUT
-WaitAckSend:
-  cal CheckLink
-  jr  nz,WaitAckSend
-SendAcked:
-  ld  a,D0HD1H
-  out (7),a
-  ld  de,TIMEOUT
-WaitReadySend:
-  cal CheckLink
-  cp  %11
-  jr  nz,WaitReadySend
-  dnz SendBits
-LinkSuccess:
-  ld  a,c ;pop a
-  ret
-
-LinkFailed:
-  scf
-  ld a,c
+  out (7),a        ;raise both
+  dnz receiveloop  ;repeat for all bits
+  or  a            ;nc
+  ld  a,c          ;a=byte received
   ret
   ret
-LinkBreak:
-  ld  a,D0HD1H
-  out (7),a
-  jp  Exit
-
-CheckLink:
-  pop hl
-  dec de
-  ld a,d
-  or e
-  jr z,LinkFailed
-
-  ld  a,$BF
-  out (1),a
-  nop \ nop
-  in  a,(1)
-  psh af
-  ld  a,%11111111
-  out (1),a
-  pop af
-  bit 6,a
-
-  in  a,(7)
-  and %11
-  jp  (hl)
-
 
 ;-----------------------------
 ;---------- levels -----------
 
 ;-----------------------------
 ;---------- levels -----------
@@ -2690,8 +2846,8 @@ wtPicture:
 
 txtMenu:    .db "by Shiar",0
             .db "Have fun!",0 ;4th menu item
 
 txtMenu:    .db "by Shiar",0
             .db "Have fun!",0 ;4th menu item
-            .db "Level 00",0  ;3rd
-            .db "Worms: 0",0  ;2nd
+            .db "Level ",$CF,"??",5,0 ;3rd
+            .db "Worms ",$CF,"?",5,0  ;2nd
 txtGame:    .db "Singleplayer",0  ;0 (1st)
 txtGame2:   .db "Peaworm     ",0  ;1 (next 1st)
             .db "Deathmatch",0    ;2
 txtGame:    .db "Singleplayer",0  ;0 (1st)
 txtGame2:   .db "Peaworm     ",0  ;1 (next 1st)
             .db "Deathmatch",0    ;2
@@ -2775,7 +2931,6 @@ worm4set:  .dw worm4p,worm4p
            .db %11111011,3,0,%01111101,%10,%1 ;enter +
 worm4name: .db "worm #04",0
 
            .db %11111011,3,0,%01111101,%10,%1 ;enter +
 worm4name: .db "worm #04",0
 
-hipeaworm: .dw 0
 end:
 
 defsprsize   = 4
 end:
 
 defsprsize   = 4
@@ -2807,6 +2962,7 @@ right    = 21
 name     = 22
 wormsize = 31
 
 name     = 22
 wormsize = 31
 
+startdelay    = 30
 respawndelay  = 30
 maxnamelength = 8+1
 
 respawndelay  = 30
 maxnamelength = 8+1