worm 0.83.0317: peaworm mode, bouncy balls, hiscore
[wormy.git] / worm.asm
1 ; Title                      : Worm
2 ; Version                    : 0.92
3 ; Release Date               : soon (I hope)
4 ; Filename                   : worm.86p (3404)
5 ; Author(s)                  : Shiar
6 ; Email Address              : shiar@mailroom.com
7 ; ICQ UIN                    : #43840958
8 ; Web Page                   : www.games3.net/shiar
9 ; Description                : ruling multiplayer game for 86 (most like Nibbles)
10 ; Where to get this game     : games3.net/shiar (home of Worm)
11 ; Other games by author(s)   : Shiar: Nemesis beta
12 ; Additional Credits to      : Matthew Shepcar (wrote original Peaworm, end'98)
13 ;                              Jonah Cohen (wrote some parts of worm)
14
15 ;-----------------------------
16 ;----------- TO-DO -----------
17 ;-----------------------------
18
19 ;       Shiar 3.III.00
20
21 ;Feel like doing something? (name indicates who's working on it)
22 ;  * linkplay
23 ;  * singleplay ending
24 ;  * titlescreen
25 ;  * make linkplay available for all gametypes (not just deathmatch)
26 ;  * two worms collide with heads -> both should die
27 ;  * game types:
28 ;1)  * ctf: take enemy flag (right-bottom) and return to your flag (left-top)
29 ;1)  * domination?: take control points by running over them and hold them
30 ;1)* sprites for picks instead of blocks
31
32 ;1) Working on a new pickup-system allowing more than one "food" and different pickup-handling
33
34 #include "TI86.inc"
35
36 ;-----------------------------
37 ;------- program start -------
38 ;-----------------------------
39
40 .org _asm_exec_ram
41 start:
42   nop
43   jp Start
44   .dw 1
45   .dw WormMsg
46   .dw WormIcon
47
48 WormIcon:
49   .db 9,2
50   .db %10010111,%01101111
51   .db %10110101,%01001011
52   .db %11110111,%01001001
53   .db %00000011,%10000000
54   .db %00000001,%11100000
55   .db %00111000,%11111000
56   .db %01111110,%00111111
57   .db %11101111,%00001111
58   .db %11000011,%10000000
59
60 Start:
61   ld  (SpSave),sp
62   call _runIndicOff
63   call _flushAllMenus
64   call _clrLCD
65   res 2,(iy+13) ;appAutoScroll
66
67   ld  a,r
68   ld  (Seed),a
69
70 ;-----------------------------
71 ;----- build trig tables -----
72 ;-----------------------------
73
74   ld  hl,TrigPrecalc
75   ld  de,SinCosTable
76   push de                        ; >> 1
77   ld  bc,65
78   ldir
79   dec hl
80   ld  b,63
81 MirrorSineWave:
82   dec hl
83   ld  a,(hl)
84   ld  (de),a
85   inc de
86   djnz MirrorSineWave
87   pop hl                         ; << 0k
88   ld  b,128+64
89 NegativeSineWave:
90   xor a
91   sub (hl)
92   ld  (de),a
93   inc hl
94   inc de
95   djnz NegativeSineWave
96
97 ;-----------------------------
98 ;---------- menu -------------
99 ;-----------------------------
100
101 DisplayMenu:
102   ld  a,2
103   ld  (nrworms),a
104 gomainMenu:
105   call _clrWindow
106   ld  hl,txtWelcome
107   call _puts
108   ld  de,$0205
109   ld  (_curRow),de
110   call _puts ;txtOptions
111   dec e ;$0204
112   ld  (_curRow),de
113   ld  hl,(CURtxtGame)
114   call _puts
115
116   xor a
117 mainMenu:
118   call menucall
119   jr  nz,notselect
120 select:
121   ld  a,b
122   or  a
123   jr  nz,gooptionsMenu
124   jp  LetsGetThisPartyOn
125 notselect
126   cp  K_EXIT
127   jp  z,ExitNoStats
128   cp  K_RIGHT
129   ld  a,b
130   jr  nz,mainMenu
131   or  a
132   jr  z,Variation
133
134 gooptionsMenu:
135   call _clrWindow
136   ld  hl,txtWelcome
137   call _puts
138   ld  hl,txtLevel
139   ld  de,$0205
140   ld  (_curRow),de
141   call _puts ;txtLevel
142   dec e ;$0204
143   ld  (_curRow),de
144   call _puts ;txtWorms
145   ld  a,(nrworms)
146   add a,'0'
147   call _putc
148   xor a
149 optionsMenu:
150   call menucall
151   jr  nz,notopselect
152 opselect:
153   ld  a,b
154   or  a
155   jr  nz,changelevel
156 changeworms:
157   ld  hl,nrworms
158   inc (hl)
159   ld  a,4
160   cp  (hl)
161   jr  nc,gooptionsMenu
162   ld  (hl),2
163   jr  gooptionsMenu
164 changelevel:
165   jr  optionsMenu
166
167 notopselect:
168   cp  K_EXIT
169   jr  z,gomainMenu
170   ld  a,b
171   jr  optionsMenu
172
173 Variation:
174   ld  a,0
175 Gametype =$-1
176   ld  hl,txtGame2
177 NEXTtxtGame =$-2
178   inc a
179   and 7 ;mod 8
180   jr  nz,okilydokily
181   ld  hl,txtGame
182 okilydokily:
183   ld  (Gametype),a
184   ld  de,$0204
185   ld  (_curRow),de
186   ld  (CURtxtGame),hl
187   call _puts
188   ld  (NEXTtxtGame),hl
189   ld  a,b
190   jp  mainMenu
191
192 menucall:
193   push af
194   ld  hl,$0004
195   ld  (_curRow),hl
196   ld  a,' '
197   call _putc
198   ld  hl,$0005
199   ld  (_curRow),hl
200   call _putc
201   pop af
202   push af
203   ld  h,0
204   add a,4
205   ld  l,a
206   ld  (_curRow),hl
207   ld  a,'*'
208   call _putc
209   pop af
210   ld  b,a
211 menukeys:
212   halt \ halt
213   call _getcsc
214   or  a
215   jr  z,menukeys
216   cp  K_UP
217   jr  nz,notup
218 updown:
219   ld  a,b
220   xor 1
221   ld  b,a
222   inc a ;nz
223   ret
224 notup:
225   cp  K_DOWN
226   jr  z,updown
227   cp  K_ENTER
228   ret z
229   cp  K_SECOND
230   ret ;z=select
231
232 ;-----------------------------
233 ;-------- start game ---------
234 ;-----------------------------
235
236 LetsGetThisPartyOn:
237   ld  hl,Gametype
238   ld  a,(hl)
239   add a,a
240   add a,(hl)
241   ld  e,a
242   ld  d,0
243   ld  hl,gamesdata
244   add hl,de
245
246   ld  a,(hl)
247   push af
248   and _datasingl
249   jr  z,notsingle
250   ld  a,1
251   ld  (nrworms),a
252 notsingle:
253   pop af
254   ld  (gameCar),a
255   push af
256
257   and _datascore
258   ld  de,$FF64 ;virt.infinate
259   jr  z,setscorelimit
260   ld  d,0 ;de=100
261 setscorelimit:
262   ld  (scorelimit),de
263
264   inc hl
265   call _ldHLind
266   pop af
267   push hl
268   and _datalink
269   jr  z,GameOver
270
271 linkmatch:
272   call _clrWindow
273
274
275 ; in  a,(7)
276 ; and %11
277 ; cp  %11
278   ld  a,WormVersion
279 ; jr  nz,host
280   call send
281   jr  c,host
282
283 client:
284   ld  hl,txtReceive
285   call _puts
286   call receive
287   cp  WormVersion
288   ret nz
289   jr  multiplayer
290
291 host:
292   ld  hl,txtWaiting
293   call _puts
294   call receive
295   cp  WormVersion
296   ret nz
297   call send
298   ld  hl,SwapPos
299   ld  (hl),$f6
300
301 multiplayer:
302   ld  a,2
303   ld  (nrworms),a
304   ld  de,LevelsDM
305
306 ;-----------------------------
307 ;--------- game over ---------
308 ;-----------------------------
309
310 GameOver:
311   call _clrLCD
312   ld  hl,0
313   ld  (worm1+died),hl
314 ;+died=0 \ +score1=0
315   ld  (worm2+died),hl
316   ld  (worm3+died),hl
317   ld  (worm4+died),hl
318   inc h
319   ld  (worm1+score+1),hl
320 ;+score2=0 \ +delay=1
321   ld  (worm2+score+1),hl
322   ld  (worm3+score+1),hl
323   ld  (worm4+score+1),hl
324   ld  a,l
325   ld  (Level),a
326   ld  hl,worm1set+4
327   ld  de,worm1+lives
328   call _MOV5B ;9xld(de),(hl)
329   call _mov9b
330   ld  hl,worm2set+4
331   ld  de,worm2+lives
332   call _MOV5B
333   call _mov9b
334   ld  hl,worm3set+4
335   ld  de,worm3+lives
336   call _MOV5B
337   call _mov9b
338   ld  hl,worm4set+4
339   ld  de,worm4+lives
340   call _MOV5B
341   call _mov9b
342   pop de
343
344 StartLevel:
345   ex  de,hl
346   ld  de,Left
347   ldi
348   ld  de,Speed
349   ldi
350   ld  a,(hl)
351   ld  (worm1+grow),a
352   ld  (worm2+grow),a
353   ld  (worm3+grow),a
354   ld  (worm4+grow),a
355   ld  (beginsize),a
356   inc hl
357
358   ld  a,(hl)
359   inc hl
360   ld  (nrballs),a
361   or  a
362   jr  z,toobad_noballs
363   ld  de,ballpos
364   ldi
365   ldi
366 toobad_noballs:
367
368 #ifdef 0
369   ld  a,(gameCar)
370   and _datahunt
371   jr  z,nohunter
372   ld  a,huntersize
373   ld  (worm2+grow),a
374 nohunter:
375 #endif
376
377   ex  de,hl
378   ld  (thislevel),de
379   push de
380   ld  hl,worm1set
381   ld  de,worm1+head
382   call _MOV4B
383   ld  hl,worm2set
384   ld  de,worm2+head
385   call _MOV4B
386   ld  hl,worm3set
387   ld  de,worm3+head
388   call _MOV4B
389   ld  hl,worm4set
390   ld  de,worm4+head
391   call _MOV4B
392   pop de
393
394   ld  hl,worm1
395   ld  a,(gameCar)
396   and _datanextl
397   ld  b,1
398   jr  nz,worminit
399   ld  b,4
400 worminit:
401   push bc                        ; >> 1
402   ld  a,(de)
403   ld  (hl),a   ;d
404   inc de
405   inc hl
406   ld  a,SinCosTable/256
407   ld  (hl),a
408   inc hl
409
410   ld  a,(de)
411   ld  (hl),a   ;y
412   inc de
413   inc hl
414   ld  a,(de)
415   ld  (hl),a   ;x
416   inc de
417   inc hl
418
419   xor a
420   ld  (hl),a   ;y2
421   inc hl
422   ld  (hl),a   ;x2
423
424   ld  bc,(worm2-worm1)-5
425   add hl,bc
426   pop bc                         ; << 0k
427   djnz worminit
428
429 ;-------- draw level ---------
430
431   ld  a,(de)
432   inc de
433   sub 128
434   ld  (FieldWidth),a
435   ld  a,(de)
436   inc de
437   sub 57
438   ld  (FieldHeight),a
439   add a,57-5
440   push de                        ; >> levelp
441   ld  l,a
442   ld  h,0
443   add hl,hl
444   add hl,hl
445   add hl,hl
446   add hl,hl
447   add hl,hl
448   ex  de,hl
449
450   ld  hl,ScrBuffer
451   push hl                        ; >> 1
452   push de                        ; >> 2
453   ld  de,ScrBuffer+1
454   ld  bc,63
455   ld  (hl),%11111111
456   ldir
457   inc hl
458   ld  (hl),%11000000
459   inc hl
460   ld  b,31
461 ClearLine:
462   ld  (hl),c
463   inc hl
464   djnz ClearLine
465   push hl                        ; >> 3
466
467   ld  a,(FieldWidth)
468   add a,126
469   push af                        ; >> 4
470   and %11111000
471   rra
472   rra
473   rra
474   ld  l,a
475   ld  h,0
476   add hl,de
477   pop af                         ; << 3
478   and %00000111
479   ld  b,a
480   ld  c,0
481   ld  a,%11000000
482   jr  z,NoVertShift
483 VertShift:
484   rra
485   rr  c
486   djnz VertShift
487 NoVertShift:
488   ld  (hl),a
489   inc hl
490   ld  (hl),c
491
492   ex  de,hl
493   pop de                         ; << 2
494   pop bc                         ; << 1
495   ldir
496   pop hl                         ; << 0k
497   ld  c,64
498   ldir
499
500 ;-draw lines-
501
502   pop hl                         ; << levelp
503   ld  a,(hl)
504   inc hl
505   or  a
506   jr  z,NoLines
507 DrawLines:
508   push af                        ; >> 1
509   call loaddrawdata
510   push hl                        ; >> 2
511   ld  l,(hl)
512   ld  h,a
513   call Line
514   inc d
515   inc h
516   call Line
517   inc e
518   inc l
519   call Line
520   dec d
521   dec h
522   call Line
523   pop hl                         ; << 1
524   inc hl
525   pop af                         ; << 0k
526   dec a
527   jr  nz,DrawLines
528 NoLines:
529
530   ld  a,(hl)
531   inc hl
532   or  a
533   jr  z,noboxes
534 drawboxes:
535   push af
536   call loaddrawdata
537   push hl
538   ld  l,(hl)
539   ld  h,a
540   call drawbox
541   pop hl
542   inc hl
543   pop af
544   dec a
545   jr  nz,drawboxes
546 noboxes:
547
548 ;-----------------------------
549
550   push hl                        ; >> levelp new
551   call showstats
552   ld  a,(gameCar)
553   and _datafood
554   jr  z,nofood
555   call NewPea
556   call DrawPea
557 nofood:
558   ld  bc,(worm1+pos)
559   call DisplayField
560   ld  hl,$FDE0
561   ld  de,$FDE1
562   ld  (hl),%11111111
563   ld  bc,$BF
564   ldir
565   ld  hl,4+(txtposReady*256)
566   ld  (_curRow),hl
567   set 3,(iy+5)
568   ld  hl,txtReady
569   call _puts
570   res 3,(iy+5)
571
572   ld  a,0
573 gameCar =$-1
574   and _datalink
575   jr  z,SwapPos ;no link
576   xor a
577   ld  (worm2+input),a
578   ld  (worm2+left),a
579   ld  (Speed),a
580
581 SwapPos: ;$18 xx -> $F6 xx
582   jr  initfinished
583   inc a
584   ld  (worm2+left),a
585   ld  hl,(worm1+pos)
586   ld  de,(worm2+pos)
587   ld  (worm2+pos),hl
588   ld  (worm1+pos),de
589   ld  a,(worm1+heading)
590   ld  b,a
591   ld  a,(worm2+heading)
592   ld  (worm1+heading),a
593   ld  a,b
594   ld  (worm2+heading),a
595 initfinished:
596
597   ld  b,0
598 ReadyDelay:
599   halt
600   djnz ReadyDelay
601
602 ;-----------------------------
603 ;----------- LOOP ------------
604 ;-----------------------------
605
606 GameLoop:
607   ld  bc,(worm1+pos)
608   call DisplayField
609
610   ld  a,1
611 flashtime =$-1
612   dec a
613   jr  z,noflash
614   ld  (flashtime),a
615   ld  hl,$fc00+(16*7)
616 screeninvertloop:
617   ld  a,(hl)
618   cpl
619   ld  (hl),a
620   inc hl
621   xor a
622   cp  h
623   jr  nz,screeninvertloop
624 noflash:
625
626   ld  a,0
627 Speed =$-1
628   or  a
629   jr  z,NoDelay
630 Delay:
631   halt
632   dec a
633   jr  nz,Delay
634 NoDelay:
635
636   call handlethatneatlittleball
637
638   ld  ix,worm1
639   ld  a,(nrworms)
640   ld  b,a
641 handleworms:
642   push bc
643   call HandleWorm
644   ld  bc,worm2-worm1
645   add ix,bc
646   pop bc
647   djnz handleworms
648
649 ;-----------------------------
650 ;---------- keys -------------
651 ;-----------------------------
652
653 HandleKeys:
654   ld  a,%10111111
655   out (1),a
656   in  a,(1)
657   rla
658   jr  c,NotPaused
659   ld  bc,$0103
660   out (c),b
661   halt
662   ld  b,11
663   out (c),b
664
665 NotPaused:
666   rla
667   jp  c,GameLoop
668   jr  Exit ;&&
669
670 WormDead:
671   ld  a,2
672   ld  (flashtime),a
673
674   ld  h,(ix+tail+1)
675   ld  l,(ix+tail)
676   ld  d,(ix+head+1)
677   ld  e,(ix+head)
678   jr  DoesWormTailEqualsWormHead ;chk4 size=0
679 removewormloop:
680   ld  c,(hl)
681   inc hl
682   ld  b,(hl)
683   inc hl
684   res resbit,h
685   push hl
686   call res4pixels
687   pop hl
688   inc (ix+grow)
689 DoesWormTailEqualsWormHead:
690   call _cphlde
691   jr  nz,removewormloop
692
693   ld  a,(gameCar)
694   and _datasingl
695   jr  nz,safewormsizedone
696   ld  a,0
697 beginsize =$-1
698   ld  (ix+grow),a
699 safewormsizedone:
700
701   ;de=ix+head
702   ld  (ix+tail+1),d
703   ld  (ix+tail),e
704   ld  a,50
705   ld  (ix+delay),a
706
707 thislevel =$+1
708   ld  de,0
709   ld  a,(de)
710   inc de
711   ld  (ix+heading),a
712   ld  a,(de)
713   ld  (ix+pos),a
714   inc de
715   ld  a,(de)
716   ld  (ix+pos+1),a
717
718   inc (ix+died)
719   dec (ix+lives)
720   push af
721   ld  de,10
722   call DecScore
723   pop af
724   ret nz ;HandleWorm done
725   ld  a,(gameCar)
726   and _datalivel
727   ret z
728
729 Exit:
730   ld  sp,0 ;pop all
731 SpSave = $-2
732   call _clrWindow
733   ld  hl,txtGO
734   call _puts
735   ld  hl,txtGame
736 CURtxtGame =$-2
737   call _puts
738   ld  de,0002
739   ld  (_curRow),de
740   call showLevel
741   ld  de,$0B03
742   ld  (_curRow),de
743   ld  hl,txtDied
744   call _puts
745   call _puts ;txtScore
746   xor a
747   ld  (_curCol),a
748
749   ld  b,2
750 nrworms =$-1
751   ld  hl,worm1+died
752 displayWormStats:
753   push bc
754   push hl
755
756   ld  bc,input-died
757   add hl,bc ;+input
758   xor a
759   cp  (hl)
760   jr  nz,NoLinkIndic
761   ld  b,7
762   inc hl ;+left
763   cp  (hl)
764   jr  z,hostLinkIndic
765   dec b
766 hostLinkIndic:
767   ld  a,8
768   ld  (_curCol),a
769   ld  a,b
770   call _putc
771   xor a
772   ld  (_curCol),a
773   dec hl
774 NoLinkIndic:
775   inc hl
776   inc hl
777   inc hl ;+name
778   call _puts
779
780   pop hl
781   push hl
782   ld  a,13
783   ld  (_curCol),a
784   ld  a,(hl) ;worm+died
785   push hl
786   call showA
787   pop hl
788
789   ld  a,16
790   ld  (_curCol),a
791   inc hl ;worm+score
792   call _ldHLind ;ld hl,(hl)
793   call showHL ;worm+score
794
795   pop hl
796   ld  bc,worm2-worm1
797   add hl,bc
798   pop bc
799   djnz displayWormStats
800
801   ld  a,(gameCar)
802   and _datasingl
803   jr  z,hiscorecheckdone
804 checkhiscore:
805   ld  de,(worm1+score)
806 HiScore =$+1
807   ld  hl,0
808   ld  a,e
809   cp  l
810   jr  c,NotNewHigh
811   ld  a,d
812   cp  h
813   jr  c,NotNewHigh
814   ld  (HiScore),de
815   ex  de,hl ;disp.new hiscore
816 NotNewHigh:
817   ex  de,hl
818   ld  hl,txthiscore
819   call _puts
820   ex  de,hl
821   call showHL
822 hiscorecheckdone:
823
824 waitkey:
825   halt
826   halt
827   call _getcsc
828   cp  K_ENTER
829   jp  z,DisplayMenu
830 ;  cp  K_EXIT
831   jr  nz,waitkey
832
833 ;x123456789012345678901
834 ;1----- GAME OVER -----
835 ;2Multiplayer
836 ;3Level 01
837 ;4           Died Score:
838 ;5NameName     03 00070
839 ;6Worm#02 @    05 00120
840 ;7Worm#03      15 00030
841 ;8Snaky   @    00 04820
842
843 ExitNoStats:
844   ld  hl,_asapvar
845   rst 20h ;_ABS_MOV10TOOP1
846   rst 10h ;_FINDSYM
847   call _swapt_ ;_ex_ahl_bde
848   ld  de,4
849   add hl,de
850   adc a,0
851   call _SET_ABS_DEST_ADDR
852
853   xor a
854   ld  hl,start
855   call _SET_ABS_SRC_ADDR
856   ld  hl,end-start
857   call _SET_MM_NUM_BYTES
858   call _MM_LDIR
859
860 ;  xor a
861 ;  ld  (_asapvar+1),a
862   res 4,(iy+9)
863   set 2,(iy+13)
864   jp  _clrWindow
865
866 ;-----------------------------
867 ;----------- worm ------------
868 ;-----------------------------
869
870 inlink:
871   ld  a,0
872 sendbyte =$-1
873   ld  b,(ix+left)
874   dec b
875   jr  z,receivefirst
876   call send
877   call receive
878   ld  l,a
879   ret
880 receivefirst:
881   push af
882   call receive
883   ld   l,a
884   pop  af
885   push hl
886   call send
887   pop  hl
888   ret
889
890 inkeys: ;use jp not call!
891   out (1),a
892   in  a,(1)
893   ld  b,a
894   and (ix+right)
895   jr  z,notright
896   ld  a,l
897   add a,8
898   ld  l,a
899 notright:
900   ld  a,b
901   and (ix+left)
902   jr  z,donediddelydone
903   ld  a,l
904   sub 8
905   ld  l,a
906   jr  donediddelydone
907
908 ;------- handle worm ---------
909
910 HandleWorm:
911   dec (ix+delay)
912   ret nz
913   inc (ix+delay)
914   ld  l,(ix+heading)
915   ld  a,(ix+input)
916   or  a
917   jr  nz,inkeys
918   call inlink
919 donediddelydone:
920   ld  a,l
921   ld  (sendbyte),a
922   ld  (ix+heading),l
923   ld  h,(ix+heading+1)
924
925   ld  c,(ix+pos)
926   ld  b,(ix+pos+1)
927   ld  e,(ix+pos2)
928   ld  d,(ix+pos2+1)
929
930 ;-------- move worm ----------
931
932 Wormmove:
933   push bc                        ; >> pos
934   ld  a,(hl)
935   add a,a
936   add a,d
937   ld  d,a
938   bit 7,(hl)
939   jr  z,notnegX
940   dec b
941 notnegX:
942   jr  nc,notmoveX
943   inc b
944 notmoveX:
945   ld  a,l
946   add a,$40
947   ld  l,a
948   ld  a,(hl)
949   add a,a
950   add a,e
951   ld  e,a
952   bit 7,(hl)
953   jr  z,notnegY
954   dec c
955 notnegY:
956   jr  nc,notmoveY
957   inc c
958 notmoveY: ;bc=newpos
959   ld  (ix+pos2),e
960   ld  (ix+pos2+1),d
961   ld  (ix+pos),c
962   ld  (ix+pos+1),b
963
964 ;-check-
965   pop hl                         ; << pos (old)
966   ld  a,h
967   sub b
968   and 1
969   ld  h,a
970   ld  a,l
971   sub c
972   and 1
973   add a,h
974   ld  d,4
975   jr  z,GotFour
976   xor 3
977   ld  d,a
978 GotFour:
979   call chk4pixels
980   rl  d
981   jp  nc,Drawworm
982
983 ;--------- worm hit ----------
984
985 Hitworm:
986   ld  a,(gameCar)
987   ld  h,a
988   and _datafood
989   jp  z,WormDead
990   ld  a,h
991   and _datahunt
992   call nz,checkhitotherworm
993
994   ld  hl,0
995 PeaY =$-2
996 PeaX =$-1
997   ld  a,b
998   sub h
999   inc a
1000   cp  4
1001   jp  nc,WormDead
1002   ld  a,c
1003   sub l
1004   inc a
1005   cp  4
1006   jp  nc,WormDead
1007   call DrawPea
1008   ld  a,(ix+grow)
1009   add a,15
1010   ld  (ix+grow),a
1011   call NewPea
1012   ld  hl,Left
1013   dec (hl)
1014   push af
1015   ld  de,10
1016   call IncScore
1017   pop af
1018   jr  nz,still_alive_not_dead
1019   ld  a,(gameCar)
1020   and _datafoodl
1021   jr  z,still_alive_not_dead
1022   ld  hl,Level
1023   ld  a,(hl)
1024   inc (hl)
1025   pop hl                         ; << call
1026   pop hl                         ; << call
1027   pop de                         ; << levelp new
1028   cp  NUM_LEVELS
1029   jp  z,Exit
1030   ld  a,(gameCar)
1031   and _datanextl
1032   jp  nz,StartLevel
1033   jp  Exit
1034
1035 ;-----------------------------
1036
1037 checkhitotherworm:
1038   .db $dd,$7d ;ld a,lx
1039   cp  worm2&255
1040   ret nz
1041 ThisIsJustASillyUselessLabel:
1042   ld  hl,(worm1+tail)
1043   ld  de,(worm1+head)
1044 nextotherwormbit:
1045   ld  a,c
1046   sub (hl)
1047   inc hl
1048   inc a
1049   cp  4
1050   jr  nc,nothit1 ;no
1051   ld  a,b
1052   sub (hl)
1053   inc a
1054   cp  4
1055   jr  c,otherwormHIT ;yes
1056 nothit1:
1057   inc hl
1058   res resbit,h
1059   call _cphlde
1060   jr  nz,nextotherwormbit
1061   ret
1062
1063 checkhitlapline:
1064   ld  a,63
1065   sub b
1066   jr  z,nextlaphalf
1067   inc a
1068   ret nz
1069 nextlaphalf:
1070   ld  a,c
1071   and 32 ;y>=32?
1072   jr  nz,nolap
1073   cp  (ix+reserv)
1074   jr  z,nolap
1075   push bc
1076   ld  de,20
1077   call IncScore
1078   pop bc
1079   xor a
1080 nolap:
1081   ld  (ix+reserv),a
1082   ret
1083
1084 otherwormHIT:
1085   push ix
1086   ld  de,10
1087   call IncScore
1088   ld  ix,worm1
1089   call WormDead
1090   pop ix
1091   pop bc
1092 still_alive_not_dead:
1093
1094 ;-------- draw worm ----------
1095
1096 Drawworm:
1097   ld  a,(gameCar)
1098   and _datahunt
1099   call nz,HuntingTimeScore
1100
1101   ld  c,(ix+pos)
1102   ld  b,(ix+pos+1)
1103
1104   ld  a,(gameCar)
1105   and _datalaps
1106   call nz,checkhitlapline
1107
1108   ld  l,(ix+head)
1109   ld  h,(ix+head+1)
1110   ld  (hl),c
1111   inc hl
1112   ld  (hl),b
1113   inc hl
1114   res resbit,h
1115   ld  (ix+head),l
1116   ld  (ix+head+1),h
1117
1118   call set4pixels
1119
1120   ld  a,(ix+grow)
1121   dec a
1122   jr  z,removetail
1123   ld  (ix+grow),a
1124   ret
1125
1126 removetail:
1127   ld  l,(ix+tail)
1128   ld  h,(ix+tail+1)
1129   ld  c,(hl)
1130   inc hl
1131   ld  b,(hl)
1132   inc hl
1133   res resbit,h
1134   ld  (ix+tail),l
1135   ld  (ix+tail+1),h
1136
1137 res4pixels:
1138   call ResPixel
1139   inc b
1140   call ResPixel
1141   inc c
1142   call ResPixel
1143   dec b
1144 ResPixel:
1145   call FindPixel
1146   cpl
1147   and (hl)
1148   ld  (hl),a
1149   ret
1150
1151 ;-----------------------------
1152 ;---------- ball -------------
1153 ;-----------------------------
1154
1155 handlethatneatlittleball:
1156   ld  a,0
1157 nrballs =$-1
1158   or  a
1159   ret z
1160 ballpos =$+1
1161   ld  bc,$0503
1162   call res4pixels
1163   dec c
1164   ld  e,b
1165 ballxmove:
1166   inc b ;=%000100 dec=%000101
1167   call checkballhit
1168   jr  z,ballxdone
1169   ld  b,e
1170   ld  hl,ballxmove
1171   call letsmovetheotherway
1172 ballxdone:
1173   ld  e,c
1174 ballymove:
1175   inc c ;=%001100 dec=%001101
1176   call checkballhit
1177   jr  z,balldone
1178   ld  c,e
1179   ld  hl,ballymove
1180   call letsmovetheotherway
1181 balldone:
1182   ld  (ballpos),bc
1183   jp  set4pixels
1184
1185 letsmovetheotherway:
1186   ld  a,1
1187   xor (hl)
1188   ld  (hl),a
1189   ret
1190
1191 checkballhit:
1192   ld  d,0
1193   call chk4pixels
1194   xor a
1195   cp  d
1196   ret
1197
1198 ;-----------------------------
1199 ;----------- procs -----------
1200 ;-----------------------------
1201
1202 NewPea:
1203 ;random routine
1204   ld hl,0
1205   ld de,12345
1206 Seed =$-2
1207   ld a,7921&255
1208   ld bc,$1000+(7921/256)
1209 domult16:
1210   add hl,hl
1211   rla
1212   rl  c
1213   jr  nc,noadd16
1214   add hl,de
1215 noadd16:
1216   djnz domult16
1217   inc hl
1218   ld  (Seed),hl ;seed=(seed*7921+1)\65536
1219
1220   ld  a,(FieldWidth)
1221   add a,123
1222   cp  h
1223   jr  c,NewPea
1224   inc h
1225   inc h
1226   ld a,(FieldHeight)
1227   add a,51
1228   cp  l
1229   jr  c,NewPea
1230   inc l
1231   inc l
1232   ld  (PeaY),hl
1233   ld  d,2 ;don't draw
1234   push hl
1235   call CheckPea
1236   pop hl
1237   dec d
1238   jr  z,NewPea
1239
1240 DrawPea: ;hl=(PeaY)
1241   ld  d,0 ;draw
1242 CheckPea:
1243   ld  b,h
1244   ld  c,l
1245   call PeaPixel
1246   inc c
1247   call PeaPixel
1248   inc b
1249   call PeaPixel
1250   dec c
1251 PeaPixel:
1252   push de
1253   call FindPixel
1254   pop de
1255   ld  e,a ;>>
1256   ld  a,d
1257   or  a
1258   ld  a,e ;<<
1259   jr  z,DrawPeaPixel ;d=0:draw
1260   and (hl) ;pixel?
1261   ret z
1262   ld  d,1 ;d=1:yes 2:no
1263   ret
1264 DrawPeaPixel:
1265   xor (hl) ;change pixel
1266   ld  (hl),a
1267   ret
1268
1269 ;-------- pixelprocs ---------
1270
1271 set4pixels:
1272   call SetPixel
1273   inc b
1274   call SetPixel
1275   inc c
1276   call SetPixel
1277   dec b
1278 SetPixel: ;at bc
1279   call FindPixel
1280   or  (hl)
1281   ld  (hl),a
1282   ret
1283
1284 chk4pixels: ;&&
1285   call CheckPixel
1286   inc b
1287   call CheckPixel
1288   inc c
1289   call CheckPixel
1290   dec b
1291   call CheckPixel
1292   dec c
1293   ret
1294 CheckPixel: ;at bc in d
1295   call FindPixel
1296   and (hl)
1297   ret z
1298   dec d
1299   ret
1300
1301 ;CLEM's FIND_PIXEL (131 cycles; 28 bytes)
1302 ;            (b,c) to hl:a; destroyes: -)
1303
1304 FindPixel:
1305   ld  h,ScrBuffer/$800
1306   ld  a,c
1307   add a,a
1308   add a,a
1309   ld  l,a    ;hl=4*y + offset/8
1310   ld  a,b
1311   rra
1312   add hl,hl
1313   rra
1314   add hl,hl
1315   add hl,hl  ;hl=32*y + offset
1316   rra        ;a=x/8
1317   or  l
1318   ld  l,a
1319   ld  a,b
1320   and 7
1321   cpl
1322   rlca
1323   rlca
1324   rlca
1325   ld  (FP_Bit),a
1326   xor a
1327 FP_Bit =$+1
1328   set 0,a
1329   ret
1330
1331 ;----------- score -----------
1332
1333 HuntingTimeScore:
1334   .db $dd,$7d ;ld a,lx
1335   cp  worm2&255
1336   ret z ;=worm#2
1337   dec (ix+reserv)
1338   ret nz
1339   ld  de,10
1340
1341 IncScore:
1342   ld  h,(ix+score+1)
1343   ld  l,(ix+score)
1344   add hl,de
1345   jr  scorecommon
1346 DecScore: ;&&&
1347   ld  h,(ix+score+1)
1348   ld  l,(ix+score)
1349   or  a
1350   sbc hl,de
1351   ret c
1352 scorecommon:
1353   ld  (ix+score+1),h
1354   ld  (ix+score),l
1355   ld  de,0
1356 scorelimit =$-2
1357   call _cphlde
1358   jp  nc,Exit
1359
1360 showstats:
1361   push ix
1362   ld  h,0
1363   ld  l,h
1364   ld  (_penCol),hl
1365   ld  a,(nrworms)
1366   ld  b,a
1367   ld  ix,worm1
1368   ld  a,(gameCar)
1369   and _datanextl
1370   jr  nz,showstatsS
1371 showstatloop:
1372   push bc
1373   call showstat
1374   ld  de,worm2-worm1
1375   add ix,de
1376   ld  hl,_penCol
1377   ld  a,(hl)
1378   add a,10
1379   ld  (hl),a
1380   pop bc
1381   djnz showstatloop
1382   pop ix
1383   ret
1384
1385 showstat:
1386   ld  a,(gameCar)
1387   and _datalivel
1388   jr  nz,showlives
1389 showscore:
1390   ld  h,(ix+score+1)
1391   ld  l,(ix+score)
1392   call _D_HL_DECI
1393   jr __vputs
1394 showlives:
1395   ld  a,(ix+lives)
1396   add a,'0'
1397 __vputmap:
1398   push ix
1399   call _vputmap
1400   pop ix
1401   ret
1402
1403 showstatsS:
1404   ld  hl,txtLevel
1405   call __vputs
1406   ld  a,(Level)
1407   cp  10
1408   jr  c,tilllevel9
1409   ld  l,a
1410   ld  h,0
1411   call _divHLby10
1412   push af
1413   ld  a,l
1414   add a,'0'
1415   call __vputmap
1416   pop af
1417 tilllevel9:
1418   add a,'0'
1419   call __vputmap
1420
1421   ld  a,98
1422   ld  (_penCol),a
1423   call showscore
1424   ld  a,123
1425   ld  (_penCol),a
1426   call showlives
1427   ld  a,(gameCar)
1428   and _datafoodl
1429   pop ix
1430   ret z
1431 showleft:
1432   ld  a,31
1433   ld  (_penCol),a
1434   ld  a,0
1435 Left =$-1
1436   push af
1437   call _divAby10
1438   add a,'0'
1439   call __vputmap
1440   pop af
1441   add a,'0'
1442   call __vputmap
1443   ld  hl,txtLeft
1444 __vputs:
1445   push ix
1446   call _vputs
1447   pop ix
1448   ret
1449
1450 showLevel:
1451   ld  hl,txtLevel
1452   call _puts
1453   ld  a,(Level)
1454 showA:
1455   cp  10
1456   jr  c,LevelBelowTen
1457   ld  l,a
1458   ld  h,0
1459   call _divHLby10
1460   push af
1461   ld  a,l
1462   add a,'0'
1463   call _putc
1464   pop af
1465 LevelBelowTen:
1466   add a,'0'
1467   jp  _putc
1468
1469 showHL:
1470   call _D_HL_DECI
1471   jp  _puts
1472
1473 _D_HL_DECI:
1474   ld  de,savestr+4
1475   ld  b,5
1476 ldhld:
1477   call _divHLby10
1478   add a,'0'
1479   ld  (de),a
1480   dec de
1481   djnz ldhld
1482   ld  hl,savestr
1483   ret
1484 savestr:
1485   .db "00000",0
1486
1487 ;-----------------------------
1488
1489 DisplayField:
1490   ld  a,c
1491   sub 29
1492   jr  nc,NotMinYScroll
1493   xor a
1494 NotMinYScroll:
1495   cp  43
1496 FieldHeight =$-1
1497   jr  c,NotMaxYScroll
1498   ld  a,(FieldHeight)
1499 NotMaxYScroll:
1500   ld  l,a
1501   ld  h,0
1502   add hl,hl
1503   add hl,hl
1504   add hl,hl
1505   add hl,hl
1506   add hl,hl
1507   push bc                        ; >> 1
1508   push de                        ; >> 2
1509   ld  de,ScrBuffer
1510   add hl,de
1511   ld  a,b
1512   sub 64
1513   jr  nc,NotMinXScroll
1514   xor a
1515 NotMinXScroll:
1516   cp 128
1517 FieldWidth = $-1
1518   jr c,NotMaxXScroll
1519   ld a,(FieldWidth)
1520 NotMaxXScroll:
1521   push af                        ; >> 3
1522   and %11111000
1523   rra
1524   rra
1525   rra
1526   ld  c,a
1527   ld  b,0
1528   ld  de,DispBuffer
1529   pop af                         ; << 2
1530   and %00000111
1531   push af                        ; >> 3
1532   cp  6
1533   jr  c,CopyScreen
1534   inc c
1535 CopyScreen:
1536   add hl,bc
1537   ld b,57
1538 CopyScreenLoop:
1539   push bc                        ; >> 4
1540   ld bc,16
1541   ldir
1542   ld c,16
1543   add hl,bc
1544   pop bc                         ; << 3
1545   djnz CopyScreenLoop
1546   pop af                         ; << 2
1547   ld  c,$b7 ;or a
1548 Bit0:
1549   jr  nz,Bit1
1550   halt
1551   halt
1552   jr  AfterShiftDelay
1553 Bit1:
1554   dec a
1555   jr nz,Bit2
1556   call ShiftRight1
1557   jr AfterShiftDelay
1558 Bit2:
1559   dec a
1560   jr nz,Bit3
1561   ld a,2
1562   call ShiftRight
1563   jr AfterShiftDelay
1564 Bit3:
1565   dec a
1566   jr nz,Bit4
1567   call Chunk
1568   call ShiftLeft1
1569   jr AfterShift
1570 Bit4:
1571   dec a
1572   jr nz,Bit5
1573   call Chunk
1574   jr AfterShiftDelay
1575 Bit5:
1576   dec a
1577   jr nz,Bit6
1578   call Chunk
1579   call ShiftRight1
1580   jr AfterShift
1581 Bit6:
1582   dec a
1583   jr nz,Bit7
1584   ld a,2
1585   call ShiftLeft
1586   jr AfterShift
1587 Bit7:
1588   call ShiftLeft
1589 AfterShiftDelay:
1590   halt
1591 AfterShift:
1592   ld hl,DispBuffer
1593   ld de,$fc00+$70
1594   ld bc,1024-$70
1595   ldir
1596   pop de                         ; << 1
1597   pop bc                         ; << 0k
1598   ret
1599
1600 ShiftRight1:
1601   ld a,1
1602 ShiftRight:
1603   ld (ShiftRightCounter),a
1604   ld a,c
1605   ld (ShiftRightChunk),a
1606   ld c,16
1607   add hl,bc
1608   ld b,57
1609 ShiftRightLoop:
1610   push bc
1611   ld bc,-32
1612   add hl,bc
1613   ex de,hl
1614   ld a,(de)
1615 ShiftRightChunk:
1616   or a
1617   call c,_SHLACC
1618   ld c,0
1619 ShiftRightCounter = $-1
1620 ShiftRowsLeft:
1621   push hl
1622   rla
1623   ld b,16
1624 ShiftRowLeft:
1625   dec hl
1626   rl (hl)
1627   djnz ShiftRowLeft
1628   pop hl
1629   dec c
1630   jr nz,ShiftRowsLeft
1631   ld bc,-16
1632   add hl,bc
1633   ex de,hl
1634   pop bc
1635   djnz ShiftRightLoop
1636   ret
1637
1638 ShiftLeft1:
1639   ld a,1
1640 ShiftLeft:
1641   ld (ShiftLeftCounter),a
1642   ld a,c
1643   ld (ShiftLeftChunk),a
1644   rla
1645   jr nc,ShiftLeftSameByte
1646   dec hl
1647 ShiftLeftSameByte:
1648   ex de,hl
1649   ld bc,-16
1650   add hl,bc
1651 NewSprite:
1652   ex de,hl
1653   ld b,57
1654 ShiftLeftLoop:
1655   push bc                        ; >> 1
1656   ld bc,-32
1657   add hl,bc
1658   ex de,hl
1659   ld a,(de)
1660 ShiftLeftChunk:
1661   or a
1662   call c,_SHRACC
1663   ld c,0
1664 ShiftLeftCounter = $-1
1665 ShiftRowsRight:
1666   push hl                        ; >> 2
1667   rra
1668   ld b,16
1669 ShiftRowRight:
1670   rr (hl)
1671   inc hl
1672   djnz ShiftRowRight
1673   pop hl                         ; << 1
1674   dec c
1675   jr nz,ShiftRowsRight
1676   ld bc,-16
1677   add hl,bc
1678   ex de,hl
1679   pop bc                         ; << 0k
1680   djnz ShiftLeftLoop
1681   ret
1682
1683 Chunk:
1684   push hl                        ; >> 1
1685   push de                        ; >> 2
1686   ld  c,16
1687   add hl,bc
1688   ld  b,57
1689 ChunkScreen:
1690   push bc                        ; >> 3
1691   ld  bc,-32
1692   add hl,bc
1693   ex  de,hl
1694   ld  a,(de)
1695   call _SHRACC
1696   ld  b,16
1697 ChunkRow:
1698   dec hl
1699   rld
1700   djnz ChunkRow
1701   ex  de,hl
1702   pop bc                         ; << 2
1703   djnz ChunkScreen
1704   pop de                         ; << 1
1705   pop hl                         ; << 0k
1706   ld  c,$37 ;scf
1707   ret
1708
1709 ;----------- draw ------------
1710
1711 loaddrawdata:
1712   ld  d,(hl)
1713   inc hl
1714   ld  e,(hl)
1715   inc hl
1716   ld  a,(hl)
1717   inc hl
1718   ret
1719
1720 Line: ;draw line from de to hl (d<h)
1721   push hl
1722   push de
1723   ld a,d
1724   cp h
1725   jr c,lineOrdered
1726   ex de,hl
1727 lineOrdered:
1728   ld  b,d
1729   ld  c,e
1730   push hl
1731   push bc
1732   call FindPixel
1733   pop bc
1734   pop de
1735 connectedLine:
1736   push hl
1737   ld  h,c
1738   ld  c,a
1739   ld  a,d
1740   sub b
1741   ld  b,a
1742   ld  a,e
1743   jr  nz,LineNotPoint
1744   cp  h
1745   jr  nz,LineNotPoint
1746   pop hl
1747   jr  DoneLine
1748 LineNotPoint:
1749   sub h
1750   ld  de,32
1751   jr  nc,LinePositiveY
1752   neg
1753   ld  de,-32
1754 LinePositiveY:
1755   cp  b
1756   jr  nc,SteepLine
1757   add a,a
1758   ld  (line2sm+1),a
1759   ld  h,a
1760   xor a
1761   sub b
1762   add a,a
1763   ld  (line1sm+1),a
1764   ld  a,h
1765   sub b
1766   pop hl
1767 LineLoopGentle:
1768   push af
1769   ld  a,(hl)
1770   or  c
1771   ld  (hl),a
1772   rrc c
1773   jr  nc,$+3
1774   inc hl
1775   pop af
1776   jp  m,line2sm
1777 line1sm:
1778   add a,0
1779   add hl,de
1780 line2sm:
1781   add a,0
1782   djnz LineLoopGentle
1783 DoneLine:
1784   ld  a,(hl)
1785   or  c
1786   ld  (hl),a
1787   pop de
1788   pop hl
1789   ret
1790 SteepLine:
1791   ld  h,a
1792   neg
1793   add a,a
1794   ld  (line3sm+1),a
1795   ld  a,b
1796   add a,a
1797   ld  (line4sm+1),a
1798   sub h
1799   ld  b,h
1800   pop hl
1801 LineLoopSteep:
1802   push af
1803   ld  a,(hl)
1804   or  c
1805   ld  (hl),a
1806   add hl,de
1807   pop af
1808   jp  m,line4sm
1809 line3sm:
1810   add a,0
1811   rrc c
1812   jr  nc,$+3
1813   inc hl
1814 line4sm:
1815   add a,0
1816   djnz LineLoopSteep
1817   jr  DoneLine
1818
1819 drawbox:
1820  ld  b,h
1821  ld  c,l
1822  call FindPixel
1823 ;  ld  b,a
1824 ;makefirstbyte:
1825 ;  or  b
1826 ;  rr  b
1827 ;  jr  nc,makefirstbyte
1828 ;  or  (hl) ;()
1829 ;  ld  (hl),a
1830
1831   ld  b,e ;yloop
1832 boxloopy:
1833   push bc ;yloop
1834   ld  b,d ;xloop
1835 boxloopx:
1836   ld  a,%11111111
1837   ld  (hl),a
1838   inc hl
1839   djnz boxloopx
1840
1841   ld  b,0
1842   ld  a,16
1843   sub d
1844   ld  c,a
1845   add hl,bc
1846   pop bc ;yloop
1847   djnz boxloopy
1848   ret
1849
1850 ;-----------------------------
1851 ;----------- link ------------
1852 ;-----------------------------
1853 #ifdef 0
1854 TIMEOUT = $1200
1855
1856 send:
1857   push af ;ld b,a
1858   call _getcsc
1859   cp  K_EXIT
1860   jp  z,Exit
1861   pop af ;ld a,b
1862   call SendByte
1863 ;  jr  c,send
1864   ret
1865
1866 linkfail:
1867   jp  Exit
1868
1869 LinkPrep:
1870   ex  (sp),hl
1871   push bc
1872   push de
1873   set 2,(iy+$12) ;cursorblink
1874   ld  b,8
1875   jp  (hl)
1876
1877 receive:
1878   call checklink
1879   jr  z,linkfail
1880   cp  %11
1881   jr  z,receive
1882 ReceiveByteCont:
1883   call LinkPrep
1884   jr  ReceiveCont
1885 ReceiveByte:
1886   call LinkPrep
1887 ReceiveBits:
1888  ld de,TIMEOUT
1889 WaitRecBit:
1890   call checklink
1891   jr  z,LinkFailed
1892   cp  %11
1893   jr  z,WaitRecBit
1894 ReceiveCont:
1895   sub %10
1896   ld  a,%10 
1897   ld  d,D0lD1h
1898   jr  c,ReceiveLow
1899   rra
1900   ld  d,D0hD1l
1901 ReceiveLow:
1902   rr  c
1903   ld  (AckBit),a
1904   ld  a,d
1905   out (7),a
1906  ld de,TIMEOUT
1907 WaitAckRec:
1908   call checklink
1909   cp  0
1910 AckBit =$-1
1911   jr  nz,WaitAckRec
1912   ld  a,D0hD1h
1913   out (7),a
1914   ld  d,4
1915 WaitReadyRec:
1916   dec d
1917   jr  z,ReadyRec
1918   in  a,(7)
1919   cp  %11
1920   jr  nz,WaitReadyRec
1921 ReadyRec:
1922   djnz ReceiveBits
1923   jr  LinkSuccess
1924
1925 SendByte:
1926   call LinkPrep
1927   ld  c,a
1928   inc b
1929   jr  SendAcked
1930 SendBits:
1931   rr  c
1932
1933 sendbit:
1934   ld  a,D0lD1h
1935   jr  nc,sendselected
1936   ld  a,D0hD1l
1937 sendselected:
1938   out (7),a
1939  ld de,TIMEOUT
1940 waitacknowledge:
1941   call checklink
1942   jr  nz,waitacknowledge
1943 SendAcked:
1944   ld  a,D0hD1h
1945   out (7),a
1946  ld de,TIMEOUT
1947 waitsendready:
1948   call checklink
1949   cp  %11
1950   jr  nz,waitsendready
1951
1952   djnz SendBits
1953 LinkSuccess: ;nc
1954   .db $f6 ;first byte of "or n"
1955 LinkFailed:  ;c
1956   scf
1957   ld  a,c
1958   res 2,(iy+$12) ;noblink
1959   pop de
1960   pop bc
1961   pop hl
1962   ret
1963
1964 linkerror:
1965   jp  Exit
1966
1967 checklink:
1968  pop hl
1969  dec de
1970  ld a,d
1971  or e
1972  jr z,LinkFailed
1973   ld  a,$BF ;key
1974   call _readkeypad
1975   bit 6,a
1976   jr  z,linkerror
1977
1978   in  a,(7)
1979   and %11
1980  jp (hl)
1981   ret
1982
1983 _readkeypad:
1984   out (1),a
1985   in  a,(1)
1986   push af
1987   ld  a,%11111111
1988   out (1),a
1989   pop af
1990   ret
1991 #endif
1992
1993 send:
1994 SendByte:
1995   in  a,(7)
1996   and %11
1997   jr  z,nolink
1998   ld  b,8
1999 sendloop:
2000   ld  de,$8000
2001   rl  c
2002   ld  a,%11010100
2003   jr  nc,sendbit
2004   ld  a,%11101000
2005 sendbit:
2006   out (7),a
2007 sendwait1:
2008   call checklink
2009   in  a,(7)
2010   and  %11
2011   jr  nz,sendwait1
2012   ld  a,%11000000
2013   out (7),a
2014 sendwait2:
2015   call checklink
2016   in  a,(7)
2017   and %11
2018   cp  %11
2019   jr  nz,sendwait2
2020   djnz sendloop
2021   xor a
2022   ret
2023 timeout:
2024   scf
2025   ret
2026
2027
2028 receive:
2029   in  a,(7)
2030   and %11
2031   jr  z,nolink
2032   cp  %11
2033   jr  z,receive
2034   ld  b,8
2035 receiveloop:
2036   ld  de,$8000
2037 receivewait1:
2038   call checklink
2039   in  a,(7)
2040   and %11
2041   cp  %11
2042   jr  z,receivewait1
2043   rra
2044   rl  c
2045   rra
2046   ld  a,%11010100
2047   jr  nc,receivebit
2048   ld  a,%11101000
2049 receivebit:
2050   out (7),a
2051 receivewait2:
2052   call checklink
2053   in  a,(7)
2054   and %11
2055   jr  z,receivewait2
2056   ld  a,%11000000
2057   out (7),a
2058   djnz receiveloop
2059   xor a
2060   ret
2061
2062 checklink:
2063   dec de
2064   ld  a,d
2065   or  e
2066   jr  z,timeout
2067   ld  a,$BF ;key
2068   call _readkeypad
2069   bit 6,a
2070   jr  z,timeout
2071   ret
2072 _readkeypad:
2073   out (1),a
2074   in  a,(1)
2075   push af
2076   ld  a,%11111111
2077   out (1),a
2078   pop af
2079   ret
2080 nolink:
2081   jp  Exit
2082
2083 ;-----------------------------
2084 ;---------- levels -----------
2085 ;-----------------------------
2086
2087 LevelsDM:
2088   .db 8,5,15,0
2089   .db $40,30,2,$C0,30,125, $00,2,64,$80,54,64
2090   .db 128,57
2091   .db 0,0
2092
2093 LevelsDM2:
2094   .db 8,5,31,0
2095   .db $40,28,2,$C0,28,125, $00,2,63,$80,54,63
2096   .db 128,57
2097   .db 0
2098   .db 4             ;4 boxes
2099   .db 4,19,16,14    ;xsize,ysize,xpos,ypos
2100   .db 4,19,80,14
2101   .db 4,19,16,34
2102   .db 4,19,80,34
2103
2104 LevelsR:
2105   .db 8,5,24,0
2106   .db $40,3,2,$40,6,2, $40,9,2,$40,12,2
2107   .db 128,57
2108   .db 0
2109   .db 1
2110   .db 8,29,32,20
2111
2112 LevelsH:
2113   .db 8,7,17,0
2114 #ifdef 0
2115 huntersize = 31
2116 #endif
2117   .db $40,28,2,$C0,28,125, $00,2,63,$80,54,63
2118   .db 128,57
2119   .db 0
2120   .db 4
2121   .db 4,19,16,14
2122   .db 4,19,80,14
2123   .db 4,19,16,34
2124   .db 4,19,80,34
2125
2126 NUM_LEVELS = 10
2127
2128 LevelsS:
2129
2130  .db 4,5,10,1 \ .dw $20F
2131  .db 0,2,63
2132  .db 128,57
2133  .db 2
2134  .db 28,14,100,41
2135  .db 28,41,100,14
2136  .db 0
2137
2138   .db 5,4,15,0      ;peas,speed,begin_size,balls
2139   .db 0,2,63        ;d,y,x
2140   .db 128,57        ;field width, height
2141   .db 0             ;no additional lines
2142   .db 0             ;no boxes
2143
2144   .db 8,4,15,0
2145   .db $40,14,2
2146   .db 128,57
2147   .db 1
2148   .db 28,28,100,28  ;line coords: x1,y1,x2,y2
2149   .db 0
2150
2151   .db 9,4,15,0
2152   .db $40,8,2
2153   .db 128,57
2154   .db 2
2155   .db 28,14,100,14
2156   .db 28,41,100,41
2157   .db 0
2158
2159   .db 9,3,15,0
2160   .db $40,8,2
2161   .db 128,80
2162   .db 2
2163   .db 64,14,64,66
2164   .db 20,40,108,40
2165   .db 0
2166
2167   .db 10,3,15,0
2168   .db 2,8,$40
2169   .db 128,90
2170   .db 3
2171   .db 18,20,18,70
2172   .db 110,20,110,70
2173   .db 18,45,110,45
2174   .db 0
2175
2176   .db 7,3,15,0
2177   .db 64,4,0
2178   .db 128,86
2179   .db 6
2180   .db 34,13,56,35
2181   .db 34,71,56,49
2182   .db 72,35,94,13
2183   .db 72,49,94,71
2184   .db 16,20,16,64
2185   .db 110,20,110,64
2186   .db 0
2187
2188   .db 9,2,15,0
2189   .db 4,10,$40
2190   .db 128,82
2191   .db 3
2192   .db 0,20,74,20
2193   .db 54,40,128,40
2194   .db 0,60,74,60
2195   .db 0
2196
2197   .db 12,2,15,0
2198   .db 64,4,0
2199   .db 128,90
2200   .db 6
2201   .db 20,16,54,16
2202   .db 74,16,110,16
2203   .db 20,16,20,72
2204   .db 110,16,110,72
2205   .db 20,72,54,72
2206   .db 74,72,110,72
2207   .db 0
2208
2209   .db 8,2,15,0
2210   .db 72,52,$c0
2211   .db 128,128
2212   .db 13
2213   .db 34,56,56,34
2214   .db 34,72,56,94
2215   .db 72,34,94,56
2216   .db 72,94,94,72
2217   .db 46,64,80,64
2218   .db 22,11,22,33
2219   .db 11,22,33,22
2220   .db 22,95,22,117
2221   .db 11,106,33,106
2222   .db 103,11,103,33
2223   .db 91,22,115,22
2224   .db 103,95,103,117
2225   .db 91,106,115,106
2226   .db 0
2227
2228
2229 ;-----------------------------
2230 ;---------- data -------------
2231 ;-----------------------------
2232
2233 TrigPrecalc:
2234 .db   0,  3,  6,  9, 12, 15, 18, 21
2235 .db  24, 27, 30, 33, 36, 39, 42, 45
2236 .db  48, 51, 54, 57, 59, 62, 65, 67
2237 .db  70, 73, 75, 78, 80, 82, 85, 87
2238 .db  89, 91, 94, 96, 98,100,102,103
2239 .db 105,107,108,110,112,113,114,116
2240 .db 117,118,119,120,121,122,123,123
2241 .db 124,125,125,126,126,126,126,126
2242 .db 127
2243
2244 txtWelcome: .db "Welcome to Worm!! ",
2245             .db "by Shiar",0
2246 txtOptions: .db "Options",0
2247 txtGame:    .db "Singleplayer",0  ;0
2248 txtGame2:   .db "Peaworm     ",0  ;1
2249             .db "Deathmatch",0    ;2
2250             .db "Foodmatch ",0    ;3
2251             .db "LinkMatch",0     ;4 (>options)
2252             .db "Hunting  ",0     ;5
2253             .db "Race   ",0       ;6
2254             .db "CTF ",0          ;7
2255             .db "Domination",0    ;(>=8)
2256 txtWaiting: .db "Waiting...",0
2257 txtReceive: .db "Receiving...",0
2258
2259 WormVersion = 092
2260 WormMsg:   .db "WORM by SHIAR -- test version",0
2261 txtLevel:  .db "Level ",0
2262 txtWorms:  .db "Worms: 0",0 ;follows txtLevel
2263 txtDied:   .db "Died ",0
2264 txtScore:  .db "Score",0  ;follows txtDied
2265 txtLeft:   .db " left",0   ;follows txtScore
2266 txthiscore:.db "HiScore:",0
2267 txtReady:  .db "Prepare!",0
2268 txtposReady = 7
2269 txtGO:     .db "----- GAME OVER -----",0
2270
2271 gamesdata:
2272
2273 _datalink  = %00000001
2274 _datasingl = %00001000 ;singleplayer=1
2275 _datalivel = %00000010 ;ix+lives=0 limit
2276 _datafoodl = %00000100 ;left=0 limit
2277 _datanextl = %00001000 ;next level if left=0
2278 _datafood  = %00010000 ;food present
2279 _datahunt  = %00100000
2280 _datalaps  = %01000000 ;give lap score
2281 _datascore = %10000000 ;score>=100 limit
2282
2283 datasingle: .db %00011110
2284             .dw LevelsS
2285 datapeas:   .db %00011010
2286             .dw LevelsS
2287 datadeathm: .db %00000010
2288             .dw LevelsDM2
2289 datafoodm:  .db %10010000
2290             .dw LevelsDM
2291 datalinkm:  .db %00000011
2292             .dw LevelsDM
2293 datahuntin: .db %10100000
2294             .dw LevelsH
2295 datarace:   .db %11000000
2296             .dw LevelsR
2297 datactf:    .db %00000000
2298             .dw LevelsDM
2299 datadomin:  .db %00000000
2300             .dw LevelsDM
2301
2302
2303 setdata = 18
2304 resbit  = 2             ;and%11111011
2305 worm1set:  .dw $B000,$B000 ;%10110000
2306            .db 3,0,%01111110,%10,%100 ;< >
2307            .db "Worm #1",0
2308 worm2set:  .dw $B800,$B800 ;%10111000
2309            .db 3,0,%00111111,%10000,%1000 ;f1 f2
2310            .db "Worm #2",0
2311 worm3set:  .dw $E800,$E800 ;%11101000 ;$D748+$1000+
2312            .db 3,0,%01011111,%10,%100 ;sto ,
2313            .db "Worm #3",0
2314 worm4set:  .dw $F000,$F000 ;%11110000
2315            .db 3,0,%01111101,%10,%1 ;enter +
2316            .db "Worm #4",0
2317 worm1 = $B400
2318 worm2 = $B430
2319 worm3 = $B460
2320 worm4 = $B490
2321 end:
2322               ;set:
2323 heading = 0   ;level*
2324 pos     = 2   ;level*
2325 pos2    = 4   ;level
2326 grow    = 6   ;level
2327 died    = 8   ;game
2328 score   = 9   ;game
2329 delay   = 11  ;game
2330
2331 head    = 12  ;level
2332 tail    = 14  ;level
2333 lives   = 16  ;game
2334 reserv  = 17  ;loop
2335  ;race:lap
2336  ;hunt:time
2337 input   = 18  ;&
2338 left    = 19  ;&
2339 right   = 20  ;&
2340 name    = 21  ;game
2341
2342 Level =$+1
2343 DispBuffer =$+2 ;912 bytes
2344
2345 ScrBuffer   = $8000 ;-$A1FF (32x256)
2346 SinCosTable = $A200 ;-$A300 (4x64)
2347
2348 ;-----------------------------
2349 ;----------- end -------------
2350 ;-----------------------------
2351
2352 .end