wormy 0.93.1411
[wormy.git] / wormy.z80
index 447f511268a54736db81ba3124be2f8ae0a66935..3dbe7ee4306fe26af268bd5d50b4510d6f505b68 100644 (file)
--- a/wormy.z80
+++ b/wormy.z80
@@ -40,7 +40,7 @@
 ;-----------------------------
 
 #define buffer      ;use display buffer (otherwise write directly to screen)
-#define readymask   ;"grays" out the field before starting a level
+#define readymask   ;"greys" out the field before starting a level
 ;#define hlines     ;horizontal-line routine for more speed drawing boxes
 
 ;#define readytext  ;displays "prepare" before level starts
@@ -136,7 +136,7 @@ start:
   .dw WormIcon
 
 WormTxt:
-  .db "WORMY by SHIAR -- alpha 93%",0
+  .db "WORMY by SHIAR -- beta 93%",0
 WormIcon:
   .db 9,2
   .db %10010110,%01101111
@@ -452,11 +452,16 @@ NegativeSineWave:
 ;-----------------------------
 
 DisplayMenu: ;---draw menu---
+  cal linkok
   cal _clrWindow
   ld  de,$FC40 ;(0,4)
   ld  hl,wtPicture
   ld  bc,16*16
   ldir
+  ld  hl,$FC00+$160
+  cal hr
+  ld  hl,$FC00+$3E0
+  cal hr
 
   ld  hl,txtMenu
   ld  ix,posMenu
@@ -473,6 +478,14 @@ dispmenuloop:
   ld  b,0 ;b=menu#
   jr  _dispmenusets ;mainMenu
 
+hr: ;draw horizontal line at hl
+  ld  b,16
+hrloop:
+  ld  (hl),-1
+  inc hl
+  dnz hrloop
+  ret
+
 ;--- menu loop ---
 
 mainMenu:
@@ -1021,22 +1034,23 @@ linkmatch:
   cp  wormVclient
   jr  z,client
   cp  wormVhost
-  jr  nz,linkiniterror
+  jr  z,host
+  pop hl ;error
+  jp  DisplayMenu ;return to menu
+
 host:
   ld  c,wormVclient
   cal Qsend
   ld  a,$18
   jr  multiplayer
-
-linkiniterror:
-  pop hl
-  jp  DisplayMenu
-
 client:
   ld  hl,txtReceive
   cal _puts
   ld  a,$E6
 multiplayer:
+;  cal linkok
+  ld a,D0LD1L
+  out (7),a
   ld  (SwapPos),a
   ld  a,2
   ld  (nrworms),a
@@ -1046,8 +1060,6 @@ multiplayer:
 ;-----------------------------
 
 GameOver:
-  ld  a,%11000000
-  out (7),a ;link normal
   cal _clrLCD
   ld  hl,worm1set
   ld  de,worm1
@@ -1315,15 +1327,41 @@ gameCar =$-1
   and _datalink
   jr  z,initfinished ;no link
   xor a
-  ld  (Speed),a ;max.speed
 SwapPos: ;$18 xx -> $E6 xx
          ; jr xx -> and xx
   jr  sethost
 setclient:
-  ld  (worm1+left),a ;worm 1 via link
+  ld  (worm1+left),a ;worm 1...
+  ld  (worm3+left),a ;and worm 3 via link
+  cal Crecv
+  ld  a,c
+  ld  (worm2+name+0),a
+  cal Crecv
+  ld  a,c
+  ld  (worm2+name+1),a
+  cal Crecv
+  ld  a,c
+  ld  (worm2+name+2),a
+  cal Crecv
+  ld  a,c
+  ld  (worm2+name+3),a
+  cal Crecv
+  ld  a,c
+  ld  (worm2+name+4),a
   jr  initfinished
 sethost:
-  ld  (worm2+left),a ;worm 2 via link
+  ld  (worm2+left),a ;worm 2+4..
+  ld  (worm4+left),a ;..over link
+  ld  c,'T'
+  cal Qsend
+  ld  c,'e'
+  cal Qsend
+  ld  c,'s'
+  cal Qsend
+  ld  c,'t'
+  cal Qsend
+  ld  c,0
+  cal Qsend
 initfinished:
 
   ld  b,startdelay
@@ -1366,6 +1404,12 @@ Delay:
   jr  nz,Delay
 NoDelay:
 
+  ld  hl,turn
+  dec (hl)
+  jr  nz,nextturnok
+  ld  (hl),3 ;2
+nextturnok:
+
   ld  (handledworm),a ;=0
 
   ld  a,0
@@ -1442,7 +1486,21 @@ handledworm =$-2
   ret nz ;HandleWorm done
   ld  a,(gameCar)
   and _datalivel
-  ret z
+  ret z ;don't end game
+  ld  a,(Gametype)
+  cp  gametron
+  jr  nz,anyworm ;if not Tron, quit at any worm's death
+  ld  a,(nrworms)
+  ld  b,a ;# of worms
+  ld  hl,worm1+lives-(worm2-worm1)
+  ld  de,worm2-worm1
+  xor a ;check for 0 lives
+checklives:
+  add hl,de ;next worm
+  cp  (hl) ;lives==0?
+  ret nz ;any >0: don't exit
+  dnz checklives
+anyworm:
   ld  a,$A7 ;exit@end of turn
   ld  (CheckExit),a ;set exit state
   ret ;finish turn
@@ -1451,8 +1509,8 @@ handledworm =$-2
 Exit:
   ld  sp,0 ;pop all
 SpSave = $-2
-  ld  a,D0HD1H
-  out (7),a
+  ld  a,D0LD1L
+  out (7),a    ;both wires low = game over signal
   cal _clrWindow
   ld  hl,txtGO
   cal _puts
@@ -1607,6 +1665,7 @@ waitkey:
 ;8Snaky   @    00 04820
 
 ExitNoStats:
+  cal linkok
   ld  hl,_asapvar
   rst 20h ;_ABS_MOV10TOOP1
   rst 10h ;_FINDSYM
@@ -1718,10 +1777,6 @@ respawndue:
 saverespawncounter:
   ld  (ix+delay),a
   jr  inputcall
-;  ld  a,(ix+input)
-;  or  a
-;  jr  z,inlink
-;  ret
 
 chkkey: ;key=a
   dec a
@@ -1747,18 +1802,19 @@ keybit:
   dnz keybit ;check match (cf set)
   ret
 
+turnspeed = 8
 inkeys: ;use jp not call!
   cal chkkey
   jr  nc,notright
   ld  a,l
-  add a,8
+  add a,turnspeed
   ld  l,a
 notright:
   ld  a,(ix+right)
   cal chkkey
   ret nc
   ld  a,l
-  sub 8
+  sub turnspeed
   ld  l,a
   ret
 inputcall:
@@ -1770,25 +1826,35 @@ inputcall:
   and _datalink
   ret z ;no link
   ld  c,l ;send our keys
-  psh hl
-  cal Qsend ;Csend
-  pop hl
-  ret
+  jp  Qsend
 inlink:
-  psh hl
   cal Qrecv ;Crecv
-  pop hl
   ld  l,c
   ret
 
 ;------- handle worm ---------
 
 HandleWorm:
+  ld  a,(gameCar)
+  cpl
+  and _datalivel
+  add a,(ix+lives)
+  or  a
+  ret z ;dead
   ld  a,(ix+delay)
   dec a
   jp  nz,respawncheck
 
   ld  a,(Gametype)
+  cp  gamedeathm ;&&&sub
+  jr  nz,nogrow
+  ld a,1 ;xor a
+  cp  0 ;a=0
+turn =$-1
+  jr  nz,nogrow
+  inc (ix+grow)
+nogrow:
+
   cp  gametron
   jr  nz,notron
   ld  de,1
@@ -3068,174 +3134,59 @@ circledraw:            ;destr:de
 
 
 
-;**** link routines ****
-;these are identical in concept to the routines used in ZTetris, ZPong, and probably
-;every other link game out there.  However, these are commented :)
-losses: .dw 0
-Csend:
-  ld  b,32
-csendwait:
-  nop
-  dnz csendwait
-  cal Qsend
-  jr  c,Csend
-  ret
-Qsend:
-send:
-;inputs: c=byte to send, both wires must be high by default
-;outputs: b=8-number of bits sent, both wires high, goes to game over on certain conditions
-;destroys: af,bc,de
-       in a,(7)
-       and %00000011
-       jr z,killlink           ;if both lines low, get out of here (game over signal)
-       ld b,8                          ;sending 8 bits
-sendloop:
-       ld de,$8000                     ;error timer
-       rl c                            ;force high bit into carry
-       ld a,%11010100          ;lower the red wire by default (sending 0) ($d4)
-       jr nc,selected          ;if high bit was 0, use above value
-       ld a,%11101000          ;if 1, lower the white wire instead ($e8)
-selected:
-       out (7),a                       ;put it out the link port
-waitconfirm:
-       call linktimer
-       in a,(7)                        ;see what the wires are doing
-       and %00000011           ;check lower 2 bits
-       jr nz,waitconfirm       ;if both bits 0, data was received ok (both wires low)
-       ld a,%11000000
-       out (7),a                       ;give the ok to raise both wires (one will be done automatically) ($c0)
-waitconfirm2:
-       call linktimer
-       in a,(7)
-       and %00000011
-       cp 3
-       jr nz,waitconfirm2      ;wait for other calc to raise the other wire
-       djnz sendloop           ;continue sending
-       xor a                           ;reset c; ld a,0
-       ld (losses),a           ;reset number of losses
-       ret
-
-
-Crecv:
-  cal receive
-  jr  c,Crecv
-  ret
-Qrecv:
-receive:
-;inputs: both wires must be high by default
-;outputs: c=byte received, b=8-number of bits received, both wires high
-;      goes to game over on certain conditions
-;destroys: af,bc,de
-       in a,(7)
-       and %00000011
-       jr z,killlink           ;game over signal applies to receiving too
-       ld b,8                          ;receiving 8 bits
-receiveloop:
-       ld de,$8000                     ;error timer
-waitreceive:
-       call linktimer
-       in a,(7)
-       and %11
-       cp 3                            ;if bits 0 and 1 set, both wires are high
-       jr z,waitreceive        ;wait for one of the wires to go low
-       rra                                     ;check red wire status
-       rl c                            ;if set, red is high (thus white is low), so put the correct bit into c
-       rra                                     ;now check white wire (since the flags are destroyed)
-       ld a,%11010100          ;$d4
-       jr nc,selected2         ;if white is low, lower red and vice versa              
-       ld a,%11101000          ;$e8
-selected2:
-       out (7),a                       ;so now both wires are low
-waitreceive2:
-       call linktimer
-       in a,(7)
-       and %11
-       jr z,waitreceive2       ;wait for the wire we didn't lower to go high again (the other will remain low)
-       ld a,%11000000          ;$c0
-       out (7),a                       ;raise both wires since the other calc will have given the ok sign
-       djnz receiveloop        ;if not done, wait for next bit
-       xor a                           ;reset c
-       ld (losses),a           ;reset number of losses
-       ret
-
-linktimer:                             ;leave if we have to wait too long (and return an error)
-       dec de
-       ld a,d
-       or e                            ;see if de is 0
-       ret nz                          ;if not, keep going as usual
-                                               ;otherwise we have to deal with a link failure
-       ld a,%11000000          ;$c0
-       out (7),a                       ;reset link status (both high)
-       ld a,(losses)           ;see how many consecutive losses we have
-       inc a
-       ld (losses),a           ;add this loss to it
-       pop de                          ;fix the stack to leave the link routine (de is ok to destroy)
-       cp 20                           ;see if this is the 20th consecutive failure
-       ret c                           ;if not, keep playing
-                                               ;otherwise, all is probably lost...
-
-killlink2:
-       jr linkend
-killlink:
-linkend:
-  jp  z,Exit
-
-
-
-
-
-
-
-
-
-#ifdef 0
+
+
+
 ;-----------------------------
 ;----------- link ------------
 ;-----------------------------
 
-timeout = $800
+linkok:
+  ld  a,D0HD1H
+  out (7),a    ;raise both wires = link ok
+  ret
+
+timeout = $8000
+lossout = 20
 
-checklink:
-  dec de
+checklink:             ;load wires in A and check for timeout
+  dec de               ;decrease timer
   ld  a,d
   or  e
-  jr  z,linkerror
-
-  ld  a,$BF
-  out (1),a
-  in  a,(1)
-  bit 6,a
-  jp  z,Exit
-
+  jr  nz,linkfine      ;ok if de>0
+;de==0 = timeout
+  cal linkok
+  ld a,0               ;losses so far
+linklosses =$-1
+  inc a                ;and this is another one
+  ld (linklosses),a
+  pop de               ;return from link
+  cp  lossout          ;quit if too many errors
+  ret c                ;otherwise just continue
+linkerr:
+  jp  Exit
+linkfine:
   in  a,(7)
   and %11
   ret
 
-linkerror:
-  scf
-  ld  a,%11
-  pop hl
-  ret
-
 ;--------------
 ;---- SEND ----
 ;--------------
 
-Csend:         ;--- send 8 bits in A --- destr:abcdehl ---
-  ld  c,a
-Csendloop:
+Csend:
+  ld  b,32
+csendwait:
+  nop
+  dnz csendwait
   cal Qsend
-  ret nc               ;NC = all ok
-  ld  a,D0HD1H
-  out (7),a            ;both high
-  jr  Csendloop        ;CF = error
+  jr  c,Csend
+  ret
 
-Qsend:         ;--- try to send 8 bits in C; CF=error --- destr:abcdehl ---
-  ld  de,timeout
-  cal checklink
-  cp  %11              ;are they?
-  scf
-  ret nz               ;nope, wait
+Qsend:         ;try to send 8 bits in C; CF=error --- destr:abcde
+  in  a,(7)
+  and %11              ;both wires low = exit signal
+  jr  z,linkerr        ;error otherwise
   ld  b,8              ;bits to send
 sendloop:
   ld  de,timeout
@@ -3254,9 +3205,10 @@ sendfinish:
   cal checklink
   cp  %11              ;both raised (by other calc)
   jr  nz,sendfinish
-  nop \ nop
+; nop \ nop
   dnz sendloop         ;repeat for all bits
   xor a                        ;nc...
+  ld (linklosses),a    ;reset number of losses
   ret                  ;=ok
 
 ;--------------
@@ -3266,17 +3218,12 @@ sendfinish:
 Crecv:         ;--- receive 8 bits into A/C --- destr:abcdehl ---
   cal Qrecv
   ret nc               ;return if all went ok
-  ld  a,D0HD1H
-  out (7),a            ;raise both on error
   jr  Crecv            ;and try again
 
-Qrecv:         ;--- receive 8 bits into A/C; CF=error --- destr:abcdehl ---
-  ld  de,timeout
-  cal checklink
-  jp  z,Exit           ;both low = error, quit
-  cp  %11
-  scf
-  ret z                        ;both high = nothing yet, wait
+Qrecv:         ;--- receive 8 bits into A/C; CF=error --- destr:abcde ---
+  in  a,(7)
+  and %11
+  jr  z,linkerr        ;both low = error, quit
   ld  b,8              ;bits to receive
 recvloop:
   ld  de,timeout
@@ -3299,9 +3246,9 @@ recvwaitack:
 recvfinish:
   dnz recvloop         ;repeat for all bits
   xor a                        ;nc=no error
-  ld  a,c              ;result in a
+  ld (linklosses),a    ;reset number of losses
   ret
-#endif
+
 
 ;-----------------------------
 ;---------- levels -----------
@@ -3354,22 +3301,22 @@ LevelDefC: ;ctf
 
 wdPicture = 16
 wtPicture:
-.db %01001010,%00101001,%00000111,%10000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%01110000,%00000000,%00000000,%00000000,%01000000,%00000000
-.db %10001010,%10101000,%10001111,%10000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%01111000,%00000000,%00000000,%00000000,%10100100,%01000000
-.db %10101010,%10101010,%10011100,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00011100,%00001110,%00000000,%00000000,%10101010,%10100000
-.db %01010001,%01000101,%00011000,%00011111,%00000000,%01111111,%11000000,%00111100,%00011110,%01111000,%00001100,%00011111,%00000000,%00000000,%11001001,%10100000
-.db %00000000,%00000000,%00111000,%00111111,%10000000,%11111111,%11100000,%11111110,%00111111,%11111100,%00001100,%00111011,%10000000,%00000011,%10001000,%00100000
-.db %01101010,%10010011,%00110000,%01110001,%11000001,%11000000,%01110001,%11100110,%01110011,%11001110,%00001100,%00110001,%10000000,%00000000,%10010000,%00100000
-.db %10001010,%00101010,%10110000,%01100000,%11000001,%10000000,%00111001,%10000000,%01100111,%11100110,%00001110,%01110001,%10000000,%00000000,%01100000,%01000000
-.db %11101110,%10111011,%00110000,%01100000,%11000001,%10110000,%00011011,%10000000,%11101110,%01110111,%00001110,%01110001,%10000011,%11100000,%00000011,%10000000
-.db %00101010,%10101010,%10110000,%01110001,%11000001,%10110000,%00011011,%00000000,%11001100,%00110011,%00000111,%11100001,%10001100,%00011000,%00010000,%00000000
-.db %11001010,%10101010,%10110000,%00110001,%10000001,%10110000,%00011011,%00000000,%11001100,%00110011,%00000011,%11000011,%10010000,%00000100,%00100011,%00001100
-.db %00000000,%00000000,%00111000,%00111011,%10000011,%10110000,%00011011,%00000000,%11001110,%01110011,%00000000,%00000011,%00010011,%00000100,%00100100,%10010010
-.db %00111001,%11000111,%00011000,%00011111,%00000011,%00110000,%00011011,%00000000,%11000111,%11100011,%10000000,%00000011,%00001100,%10000100,%00101000,%10010100
-.db %01100101,%00101000,%00011100,%00001110,%00000111,%00111000,%00111011,%10000000,%11000011,%11000001,%10000000,%00000111,%00000000,%01001110,%00101000,%10011000
-.db %01001101,%11001011,%00001110,%00111111,%10001110,%00011100,%01110001,%10000000,%11100000,%00000001,%11100000,%00001110,%00000000,%01010101,%00101000,%10011000
-.db %00111001,%01100111,%00000111,%11111011,%11111100,%00001111,%11100001,%11000000,%01100000,%00000000,%11111111,%11111100,%00000000,%10010101,%00100110,%00010100
-.db %00000000,%00000000,%00000011,%11100000,%11111000,%00000111,%11000000,%11000000,%01100000,%00000000,%00111111,%11111000,%11111111,%00001000,%11000001,%11100011
+.db %00000000,%00000000,%00000111,%10000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%01110000,%00000000,%00000000,%00000000,%00000000,%00000000
+.db %00000000,%00000000,%00001111,%10000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%01111000,%00000000,%00000000,%00000110,%01111011,%00100000
+.db %00000000,%00000000,%00011100,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00011100,%00001110,%00000000,%00001001,%00010011,%01000000
+.db %00000000,%00000000,%00011000,%00011111,%00000000,%01111111,%11000000,%00111100,%00011110,%01111000,%00001100,%00011111,%00000000,%10100111,%00110000,%10000000
+.db %00000000,%00000000,%00111000,%00111111,%10000000,%11111111,%11100000,%11111110,%00111111,%11111100,%00001100,%00111011,%00000000,%10100001,%00001001,%01100000
+.db %00000000,%00000000,%00110000,%01110001,%11000001,%11000000,%01110001,%11100110,%01110011,%11001110,%00001100,%00110001,%10000000,%01001110,%01110010,%01100000
+.db %00000000,%00000000,%00110000,%01100000,%11000001,%10000000,%00111001,%10000000,%01100111,%11100110,%00001110,%01110001,%10000000,%00000000,%00000000,%00000000
+.db %00000000,%00000000,%00110000,%01100000,%11000001,%10110000,%00011011,%10000000,%11101110,%01110111,%00001110,%01110001,%10000000,%00000000,%00000000,%00000000
+.db %00000000,%00000000,%00110000,%01110001,%11000001,%10110000,%00011011,%00000000,%11001100,%00110011,%00000111,%11100001,%10000000,%00000000,%00000000,%00000000
+.db %00000000,%00000000,%00110000,%00110001,%10000001,%10110000,%00011011,%00000000,%11001100,%00110011,%00000011,%11000011,%10000000,%00000000,%00000000,%00000000
+.db %00000000,%00000000,%00111000,%00111011,%10000011,%10110000,%00011011,%00000000,%11001110,%01110011,%00000000,%00000011,%00110101,%01001001,%10000000,%00000000
+.db %00000000,%00000000,%00011000,%00011111,%00000011,%00110000,%00011011,%00000000,%11000111,%11100011,%10000000,%00000011,%01000101,%00010101,%01000011,%01100011
+.db %00000000,%00000000,%00011100,%00001110,%00000111,%00111000,%00111011,%10000000,%11000011,%11000001,%10000000,%00000111,%01100101,%01010101,%01000101,%01010100
+.db %00000000,%00000000,%00001110,%00111111,%10001110,%00011100,%01110001,%10000000,%11100000,%00000001,%11100000,%00001110,%00110111,%01011101,%10010101,%01100101
+.db %00000000,%00000000,%00000111,%11111011,%11111100,%00001111,%11100001,%11000000,%01100000,%00000000,%11111111,%11111100,%00010101,%01010101,%01000101,%01010101
+.db %00000000,%00000000,%00000011,%11100000,%11111000,%00000111,%11000000,%11000000,%01100000,%00000000,%00111111,%11111000,%01100101,%01010101,%01000110,%01010011
 
 txtMenu:  .db "Mode",0  ;1st menu item
          .db "Level",0 ;2nd
@@ -3449,13 +3396,13 @@ datasingle: .db %01011110,3,1,1 ;3 lives (<must b unique)
 gamepeas     =  1
 datapeas:   .db %01011010,1,1,1 ;1 "
 gametron     =  2
-datatron:   .db %01000010,1,2,1
+datatron:   .db %01000010,1,1,1
 gamedeathm   =  3
 datadeathm: .db %01000010,3,2,1
 gamefoodm    =  4
 datafoodm:  .db %11010000,10,2,1 ;10 score limit (=100)
 gamelinkm    =  5
-datalinkm:  .db %01000011,3,2,1
+datalinkm:  .db %11010001,3,2,1
 gamerace     =  6
 datarace:   .db %10000000,10,2,1
 gamectf      =  7