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