shiar: multiplayer, menu
[wormy.git] / worm.asm
1 ; -WonderWorm--v0.9-
2 ; by Matthew Shepcar 30.XII.98
3 ; modified by Jonah Cohen 19.XI .99
4 ;               and Shiar 01.XII.99
5
6 ;to-do:
7 ; * picks not correctly removed
8 ; * linkplay
9 ; * hot pursuit + ctf
10 ; * ending (+stats after multiplayer)
11
12 #include "TI86.inc"
13
14
15 .org _asm_exec_ram
16
17 _divHLby10     = $4044
18 _getcsc        = _getky
19 _clrWindow     = $4a86
20 _runIndicOff   = _runindicoff
21 _flushAllMenus = _flushallmenus
22 _SHRACC        = _shracc
23 _SHLACC        = $438B
24
25   nop
26   jp Start
27   .dw 0,WormMsg
28
29 Start:
30 ; ld (SpSave),sp
31   call _runIndicOff
32   call _flushAllMenus
33   call _clrLCD
34
35   ld  a,r
36   ld  (Seed),a
37   xor a
38   ld  (_asapvar+1),a
39
40 ;-----------------------------
41 ;----- build trig tables -----
42 ;-----------------------------
43
44   ld hl,TrigPrecalc
45   ld de,SinCosTable
46   push de                        ; >> 1
47   ld bc,65
48   ldir
49   dec hl
50   ld b,63
51 MirrorSineWave:
52   dec hl
53   ld a,(hl)
54   ld (de),a
55   inc de
56   djnz MirrorSineWave
57   pop hl                         ; << 0k
58   ld b,128+64
59 NegativeSineWave:
60   xor a
61   sub (hl)
62   ld (de),a
63   inc hl
64   inc de
65   djnz NegativeSineWave
66
67 ;-----------------------------
68 ;---------- menu -------------
69 ;-----------------------------
70
71 DisplayMenu:
72   call _clrLCD
73   ld  hl,txtWelcome
74   call _puts
75   ld  hl,txtMenu
76   ld  de,$0203
77   ld  b,4
78 DispMenuLoop:
79   inc de
80   ld  (_curRow),de
81   call _puts
82   djnz DispMenuLoop
83
84   ld  hl,$0805
85   ld  (_curRow),hl
86   ld  hl,txtGame
87   call _puts
88
89   xor a
90 menudraw:
91   and 3 ;-1>>3; 4>>0
92   push af
93
94   ld  h,0
95   add a,4
96   ld  l,a
97   ld  (_curRow),hl
98   ld  a,5 ;arrow
99   call _putc
100
101   ld  h,0
102   ld  a,1
103 Previous =$-1
104   add a,4
105   ld  l,a
106   ld  (_curRow),hl
107   ld  a,' '
108   call _putc
109
110   pop af
111   push af
112   ld  (Previous),a
113 WKCP:
114   halt \ halt
115   call GET_KEY
116   cp  K_EXIT
117   jp  z,Exit
118   cp  K_ENTER
119   jr  z,select
120   cp  K_SECOND
121   jr  z,select
122   cp  K_UP
123   jr  nz,notup
124   pop af
125   dec a
126   jr  menudraw
127 notup:
128   cp  K_DOWN
129   jr  nz,WKCP
130   pop af
131   inc a
132   jr  menudraw
133
134 select:
135   pop af
136   ld  b,a
137   or  a ;1st
138   jr  z,ThePartyIsOn
139   dec a ;2nd
140   jr  z,Variation
141   dec a ;3rd
142   jp  Exit
143
144 Variation:
145   ld  hl,$0805
146   ld  (_curRow),hl
147   ld  a,0
148 Gametype =$-1
149   ld  hl,txtGame2
150 NEXTtxtGame =$-2
151   inc a
152   and 3
153   jr  nz,okilydokily
154   ld  hl,txtGame
155 okilydokily:
156   ld  (Gametype),a
157   call _puts
158   ld  (NEXTtxtGame),hl
159   ld  a,b
160   jr  menudraw
161
162 singleplayer:
163   inc a
164   ld  (nrworms),a
165   ld  de,LevelsS
166   jr  GameOver
167
168 ThePartyIsOn:
169   ld  a,(Gametype)
170   or  a
171   jr  z,singleplayer
172   ld  a,2
173   ld  (nrworms),a
174   ld  de,LevelsDM
175
176 ;-----------------------------
177 ;--------- game over ---------
178 ;-----------------------------
179
180 GameOver:
181   push de
182   call _clrLCD
183   pop de
184   ld  a,1
185   ld  (Level),a
186   ld  hl,0
187   ld  (worm1+score),hl
188   ld  (worm2+score),hl
189   ld  a,3
190   ld  (worm1+lives),a
191   ld  (worm2+lives),a
192
193 StartLevel:
194   ld  a,(de)
195   ld  (Left),a
196   inc de
197   ld  a,(de)
198   ld  (Speed),a
199   inc de
200   ld  a,15
201   ld  (worm1+grow),a
202   ld  (worm2+grow),a
203 NextLife:
204   push de                        ; >> levelp old
205   ld  b,2
206   ld  hl,worm1
207
208 worminit:
209   push bc                        ; >> 1
210   ld  a,(de)
211   ld  (hl),a   ;d
212   inc de
213   inc hl
214   ld  a,SinCosTable/256
215   ld  (hl),a
216   inc hl
217
218   ld  a,(de)
219   ld  (hl),a   ;y
220   inc de
221   inc hl
222   ld  a,(de)
223   ld  (hl),a   ;x
224   inc de
225   inc hl
226
227   xor a
228   ld  (hl),a   ;y2
229   inc hl
230   ld  (hl),a   ;x2
231
232   ld  bc,20-5
233   add hl,bc
234   pop bc                         ; << 0k
235   djnz worminit
236
237   ld  hl,Worm1
238   ld  (worm1+head),hl
239   ld  (worm1+tail),hl
240   ld  hl,Worm2
241   ld  (worm2+head),hl
242   ld  (worm2+tail),hl
243
244 ;-------- draw level ---------
245
246   ld  a,(de)
247   inc de
248   sub 128
249   ld  (FieldWidth),a
250   ld  a,(de)
251   inc de
252   sub 57
253   ld  (FieldHeight),a
254   add a,57-5
255   push de                        ; >> levelp
256   ld  l,a
257   ld  h,0
258   add hl,hl
259   add hl,hl
260   add hl,hl
261   add hl,hl
262   add hl,hl
263   ex  de,hl
264
265   ld  hl,ScrBuffer
266   push hl                        ; >> 1
267   push de                        ; >> 2
268   ld  de,ScrBuffer+1
269   ld  bc,63
270   ld  (hl),%11111111
271   ldir
272   inc hl
273   ld  (hl),%11000000
274   inc hl
275   ld  b,31
276 ClearLine:
277   ld  (hl),c
278   inc hl
279   djnz ClearLine
280   push hl                        ; >> 3
281
282   ld  a,(FieldWidth)
283   add a,126
284   push af                        ; >> 4
285   and %11111000
286   rra
287   rra
288   rra
289   ld  l,a
290   ld  h,0
291   add hl,de
292   pop af                         ; << 3
293   and %00000111
294   ld  b,a
295   ld  c,0
296   ld  a,%11000000
297   jr  z,NoVertShift
298 VertShift:
299   rra
300   rr  c
301   djnz VertShift
302 NoVertShift:
303   ld  (hl),a
304   inc hl
305   ld  (hl),c
306
307   ex  de,hl
308   pop de                         ; << 2
309   pop bc                         ; << 1
310   ldir
311   pop hl                         ; << 0k
312   ld  c,64
313   ldir
314
315 ;-draw lines-
316
317   pop hl                         ; << levelp
318   ld  a,(hl)
319   inc hl
320   or  a
321   jr  z,NoLines
322 DrawLines:
323   push af                        ; >> 1
324   ld  d,(hl)
325   inc hl
326   ld  e,(hl)
327   inc hl
328   ld  a,(hl)
329   inc hl
330   push hl                        ; >> 2
331   ld  l,(hl)
332   ld  h,a
333   call Line
334   inc d
335   inc h
336   call Line
337   inc e
338   inc l
339   call Line
340   dec d
341   dec h
342   call Line
343   pop hl                         ; << 1
344   inc hl
345   pop af                         ; << 0k
346   dec a
347   jr  nz,DrawLines
348 NoLines:
349
350 ;-----------------------------
351
352   push hl                        ; >> levelp new
353
354   ld  hl,0
355   ld  (_penCol),hl
356   ld  hl,LivesMsg
357   call _vputs
358   ld  a,(worm1+lives)
359   add a,'0'
360   call _vputmap
361
362   ld  a,40
363   ld  (_penCol),a
364   call _vputs ;scoremsg
365   ld  a,100
366   ld  (_penCol),a
367   call _vputs
368   ld  a,1
369 Left =$-1
370   call PutNum
371   ld  ix,worm1
372   call PutScore
373
374   call NewPea
375
376   pop hl                         ; << levelp new
377   push hl                        ; >> levelp new
378   ld  bc,(worm1+pos)
379   call DisplayField
380   ld  hl,LevelMsg
381   ld  a,7
382   push hl                        ; >> 1
383   ld  h,a
384   ld  l,4
385   ld  (_curRow),hl
386   ld  hl,0FDE0h
387   ld  de,0FDE1h
388   ld  (hl),-1
389   ld  bc,0BFh
390   ldir
391   pop hl                         ; << 0k
392   set 3,(iy+5)
393   call _puts
394   ld  a,(Level)
395   cp  10
396   jr  c,LevelBelowTen
397   ld  l,a
398   ld  h,0
399   call _divHLby10
400   push af                        ; >> 1
401   ld  a,l
402   add a,'0'
403   call _putc
404   pop af                         ; << 0k
405 LevelBelowTen:
406   add a,'0'
407   call _putc
408   res 3,(iy+5)
409
410   ld  b,0
411 ReadyDelay:
412   halt
413   djnz ReadyDelay
414
415 ;-----------------------------
416 ;----------- LOOP ------------
417 ;-----------------------------
418
419 GameLoop:
420   ld  bc,(worm1+pos)
421   call DisplayField
422
423   ld  a,0
424 Speed =$-1
425   or  a
426   jr  z,NoDelay
427 Delay:
428   halt
429   dec a
430   jr  nz,Delay
431 NoDelay:
432
433   ld  ix,worm1
434   call HandleWorm
435   ld  ix,worm2
436   ld  a,(nrworms)
437   cp  2
438   call z,HandleWorm
439
440 ;-----------------------------
441 ;---------- keys -------------
442 ;-----------------------------
443
444 HandleKeys:
445   ld  a,%10111111
446   out (1),a
447   in  a,(1)
448   rla
449   jr  c,NotPaused
450   ld  bc,$0103
451   out (c),b
452   halt
453   ld  b,11
454   out (c),b
455
456 NotPaused:
457   rla
458   jp  c,GameLoop
459 Exit2pop:
460   pop hl                         ; << levelp new
461 Exit1pop:
462   pop hl                         ; << levelp old
463   jr  Exit
464
465 Exit5pop:
466   pop hl
467   pop hl
468   pop hl
469   jr  Exit2pop
470
471 WormDead:
472   pop hl                         ; << call
473   pop hl                         ; << levelp new
474   ld  hl,(worm1+head)
475   ld  de,(worm1+tail)
476   sbc hl,de
477   ld  a,l
478   rr  h
479   rra
480   ld  (worm1+grow),a
481
482   ld  hl,(worm2+head)
483   ld  de,(worm2+tail)
484   sbc hl,de
485   ld  a,l
486   rr  h
487   rra
488   ld  (worm2+grow),a
489
490   ld  a,(ix+lives)
491   dec a
492   ld  (ix+lives),a
493   pop de                         ; << levelp old
494   jp  nz,NextLife
495 Exit:
496
497 #if 0
498   ld a,(Eaten)
499   ld hl,HiScore
500   cp (hl)
501   jr c,NotNewHigh
502   ld (hl),a
503   ld hl,_asapvar
504   rst 20h
505   rst 10h
506   call 460Bh
507   ld de,HiScore-_asm_exec_ram+2
508   add hl,de
509   adc a,0
510   call _load_ram_ahl
511   ld a,(HiScore)
512   ld (hl),a
513 NotNewHigh:
514 #endif
515   res 4,(iy+9)
516 ;  ld sp,0
517 SpSave = $-2
518   jp _clrWindow
519
520 ;-----------------------------
521 ;----------- worm ------------
522 ;-----------------------------
523
524 inlink:
525   ld  a,0
526 sendbyte =$-1
527   call SendByte
528   jr  c,receivefirst
529   call receive
530   ret
531 receivefirst:
532   call receive
533   ld  a,(sendbyte)
534   call SendByte
535   ret
536
537 receive:
538 receiveloop:
539   call GET_KEY
540   cp  K_EXIT
541   jp  z,Exit5pop
542   call TryReceiveByte
543   jr  c,receiveloop
544   ld  l,a
545   ret
546
547 inkeys:
548   out (1),a
549   in  a,(1)
550   ld  b,a
551   and (ix+right)
552   jr  z,NotRight
553   ld  a,l
554   add a,8
555   ld  l,a
556 NotRight:
557   ld  a,b
558   and (ix+left)
559   ret z
560   ld  a,l
561   sub 8
562   ld  l,a
563   ret
564
565 ;------- handle worm ---------
566
567 HandleWorm:
568   ld  l,(ix+heading)
569   ld  a,(ix+input)
570   or  a
571   jr  nz,keys
572   call inlink
573   jr  donelydone
574 keys:
575   call inkeys
576 donelydone:
577   ld  a,l
578   ld  (sendbyte),a
579   ld  (ix+heading),l
580   ld  h,(ix+heading+1)
581
582   ld  c,(ix+pos)
583   ld  b,(ix+pos+1)
584   ld  e,(ix+pos2)
585   ld  d,(ix+pos2+1)
586
587 ;-------- move worm ----------
588
589 Wormmove:
590   push bc                        ; >> pos
591   ld  a,(hl)
592   add a,a
593   add a,d
594   ld  d,a
595   bit 7,(hl)
596   jr  z,NotNegX
597   dec b
598 NotNegX:
599   jr  nc,NotMoveX
600   inc b
601 NotMoveX:
602   ld  a,l
603   add a,40h
604   ld  l,a
605   ld  a,(hl)
606   add a,a
607   add a,e
608   ld  e,a
609   bit 7,(hl)
610   jr  z,NotNegY
611   dec c
612 NotNegY:
613   jr  nc,NotMoveY
614   inc c
615 NotMoveY: ;bc=newpos
616
617 ;-check-
618   pop hl                         ; << pos (old)
619   push de                        ; >> pos2
620   ld  a,h
621   sub b
622   and 1
623   ld  h,a
624   ld  a,l
625   sub c
626   and 1
627   add a,h
628   ld  d,4
629   jr  z,GotFour
630   xor 3
631   ld  d,a
632 GotFour:
633   call CheckPixel
634   inc b
635   call CheckPixel
636   inc c
637   call CheckPixel
638   dec b
639   call CheckPixel
640   dec c
641   rl  d
642   pop de                         ; << pos2
643
644   ld  (ix+pos2),e
645   ld  (ix+pos2+1),d
646   ld  (ix+pos),c
647   ld  (ix+pos+1),b
648   jr  nc,Drawworm
649
650 ;--------- worm hit ----------
651
652 Hitworm:
653   ld  hl,0
654 PeaY =$-2
655 PeaX =$-1
656   ld  a,b
657   sub h
658   inc a
659   cp  4
660   jp  nc,WormDead
661   ld  a,c
662   sub l
663   inc a
664   cp  4
665   jp  nc,WormDead
666   push bc                        ; >> 1
667   call DrawPea
668   ld  a,(ix+grow)
669   add a,15
670   ld  (ix+grow),a
671   call NewPea
672   ld  de,10
673   call IncScore
674   ld  a,119
675   ld  (_penCol),a
676   ld hl,Left
677   dec (hl)
678   jr  nz,NotNextLevel
679   ld  hl,Level
680   ld  a,(hl)
681   inc (hl)
682   pop bc                         ; << 0k
683   pop hl                         ; << call
684   pop de                         ; << levelp new
685   pop hl                         ; << levelp old
686   cp  NUM_LEVELS
687   jp  z,Exit
688   ld  a,(Gametype)
689   or  a
690   jp  z,StartLevel
691   jp  Exit
692
693 NotNextLevel:
694   ld  a,(hl)
695   call PutNum
696   pop bc                         ; << 0k
697
698 ;-------- draw worm ----------
699
700 Drawworm:
701   ld  l,(ix+head)
702   ld  h,(ix+head+1)
703   ld  (hl),c
704   inc hl
705   ld  (hl),b
706   inc hl
707   res resbit,h
708   ld  (ix+head),l
709   ld  (ix+head+1),h
710
711   call SetPixel
712   inc b
713   call SetPixel
714   inc c
715   call SetPixel
716   dec b
717   call SetPixel
718
719   ld  a,(ix+grow)
720   dec a
721   jr  z,removetail
722   ld  (ix+grow),a
723   ret
724
725 removetail:
726   ld  l,(ix+tail)
727   ld  h,(ix+tail+1)
728   ld  c,(hl)
729   inc hl
730   ld  b,(hl)
731   inc hl
732   res resbit,h
733   ld  (ix+tail),l
734   ld  (ix+tail+1),h
735
736   call ResPixel
737   inc b
738   call ResPixel
739   inc c
740   call ResPixel
741   dec b
742   jp  ResPixel
743
744 ;-----------------------------
745 ;----------- procs -----------
746 ;-----------------------------
747
748 NewPea:
749 ;random routine
750   ld hl,0
751   ld de,12345
752 Seed =$-2
753   ld a,7921 & 255
754   ld bc,1000h+(7921/256)
755 domult16:
756   add hl,hl
757   rla
758   rl c
759   jr nc,noadd16
760   add hl,de
761 noadd16:
762   djnz domult16
763   inc hl
764   ld (Seed),hl ;seed=(seed*7921+1) MOD 65536
765   ld a,(FieldWidth)
766   add a,123
767   cp h
768   jr c,NewPea
769   inc h
770   inc h
771   ld a,(FieldHeight)
772   add a,51
773   cp l
774   jr c,NewPea
775   inc l
776   inc l
777   ld (PeaY),hl
778   ld d,2
779   push hl                        ; >> 1
780   call CheckPea
781   pop hl                         ; << 0k
782   dec d
783   jr z,NewPea
784 DrawPea:
785   ld d,0
786 CheckPea:
787   ld b,h
788   ld c,l
789   call PeaPixel
790   inc c
791   call PeaPixel
792   inc b
793   call PeaPixel
794   dec c
795 PeaPixel:
796   push de                        ; >> 1
797   call FindPixel
798   pop de                         ; << 0k
799   ld e,a
800   ld a,d
801   or a
802   ld a,e
803   jr z,DrawPeaPixel
804   and (hl)
805   ret z
806   ld d,1
807   ret
808 DrawPeaPixel:
809   xor (hl)
810   ld (hl),a
811   ret
812
813 ;-------- pixelprocs ---------
814
815 ResPixel: ;at bc
816   call FindPixel
817   cpl
818   and (hl)
819   ld  (hl),a
820   ret
821
822 SetPixel: ;at bc
823   call FindPixel
824   or  (hl)
825   ld  (hl),a
826   ret
827
828 CheckPixel: ;at bc in d
829   push de                        ; >> 1
830   call FindPixel
831   and (hl)
832   pop de                         ; << 0k
833   ret z
834   dec d
835   ret
836
837 FindPixel: ;bc to ahl + de gone
838   push de                        ; >> 1
839   push bc                        ; >> 2
840   ld a,b
841   and 7
842   add a,offsets_table & 255
843   ld e,a
844   ld d,offsets_table/256
845   ld h,0
846   ld l,c
847   add hl,hl
848   add hl,hl
849   add hl,hl
850   add hl,hl
851   add hl,hl
852   ld a,b
853   and %11111000
854   rra
855   rra
856   rra
857   or l
858   ld l,a
859   ld a,(de)
860   ld de,ScrBuffer
861   add hl,de
862   pop bc                         ; << 1
863   pop de                         ; << 0k
864   ret
865
866 ;-----------------------------
867
868 IncScore:
869   ld  h,(ix+score+1)
870   ld  l,(ix+score)
871   add hl,de
872   ld  (ix+score+1),h
873   ld  (ix+score),l
874 PutScore:
875   ld  de,_penCol
876   ld  a,79
877   ld  (de),a
878   ld  b,5
879 DoPutScore:
880   push bc                        ; >> 1
881   push de                        ; >> 2
882   call _divHLby10
883   call PutDigit
884   pop de                         ; << 1
885   ld  a,(de)
886   sub 8
887   ld  (de),a
888   pop bc                         ; << 0k
889   djnz DoPutScore
890   ret
891
892 PutNum:
893   ld  l,a
894   ld  h,0
895   call _divHLby10
896   push af                        ; >> 1
897   ld  a,l
898   call PutDigit
899   pop af                         ; << 0k
900 PutDigit:
901   add a,'0'
902   jp  _vputmap
903
904 ;-----------------------------
905
906 offsets_table:
907   .db 128,64,32,16,8,4,2,1
908
909 DisplayField:
910   ld  a,c
911   sub 29
912   jr  nc,NotMinYScroll
913   xor a
914 NotMinYScroll:
915   cp  43
916 FieldHeight =$-1
917   jr  c,NotMaxYScroll
918   ld  a,(FieldHeight)
919 NotMaxYScroll:
920   ld  l,a
921   ld  h,0
922   add hl,hl
923   add hl,hl
924   add hl,hl
925   add hl,hl
926   add hl,hl
927   push bc                        ; >> 1
928   push de                        ; >> 2
929   ld  de,ScrBuffer
930   add hl,de
931   ld  a,b
932   sub 64
933   jr  nc,NotMinXScroll
934   xor a
935 NotMinXScroll:
936   cp 128
937 FieldWidth = $-1
938   jr c,NotMaxXScroll
939   ld a,(FieldWidth)
940 NotMaxXScroll:
941   push af                        ; >> 3
942   and %11111000
943   rra
944   rra
945   rra
946   ld c,a
947   ld b,0
948   ld de,DispBuffer
949   pop af                         ; << 2
950   and %00000111
951   push af                        ; >> 3
952   cp 6
953   jr c,CopyScreen
954   inc c
955 CopyScreen:
956   add hl,bc
957   ld b,57
958 CopyScreenLoop:
959   push bc                        ; >> 4
960   ld bc,16
961   ldir
962   ld c,16
963   add hl,bc
964   pop bc                         ; << 3
965   djnz CopyScreenLoop
966   pop af                         ; << 2
967   ld c,$b7                 ;or a
968 Bit0:
969   jr nz,Bit1
970   halt
971   halt
972   jr AfterShiftDelay
973 Bit1:
974   dec a
975   jr nz,Bit2
976   call ShiftRight1
977   jr AfterShiftDelay
978 Bit2:
979   dec a
980   jr nz,Bit3
981   ld a,2
982   call ShiftRight
983   jr AfterShiftDelay
984 Bit3:
985   dec a
986   jr nz,Bit4
987   call Chunk
988   call ShiftLeft1
989   jr AfterShift
990 Bit4:
991   dec a
992   jr nz,Bit5
993   call Chunk
994   jr AfterShiftDelay
995 Bit5:
996   dec a
997   jr nz,Bit6
998   call Chunk
999   call ShiftRight1
1000   jr AfterShift
1001 Bit6:
1002   dec a
1003   jr nz,Bit7
1004   ld a,2
1005   call ShiftLeft
1006   jr AfterShift
1007 Bit7:
1008   call ShiftLeft
1009 AfterShiftDelay:
1010   halt
1011 AfterShift:
1012   ld hl,DispBuffer
1013   ld de,$fc00+$70
1014   ld bc,1024-$70
1015   ldir
1016   pop de                         ; << 1
1017   pop bc                         ; << 0k
1018   ret
1019
1020 ShiftRight1:
1021   ld a,1
1022 ShiftRight:
1023   ld (ShiftRightCounter),a
1024   ld a,c
1025   ld (ShiftRightChunk),a
1026   ld c,16
1027   add hl,bc
1028   ld b,57
1029 ShiftRightLoop:
1030   push bc                        ; >> 1
1031   ld bc,-32
1032   add hl,bc
1033   ex de,hl
1034   ld a,(de)
1035 ShiftRightChunk:
1036   or a
1037   call c,_SHLACC
1038   ld c,0
1039 ShiftRightCounter = $-1
1040 ShiftRowsLeft:
1041   push hl                        ; >> 2
1042   rla
1043   ld b,16
1044 ShiftRowLeft:
1045   dec hl
1046   rl (hl)
1047   djnz ShiftRowLeft
1048   pop hl                         ; << 1
1049   dec c
1050   jr nz,ShiftRowsLeft
1051   ld bc,-16
1052   add hl,bc
1053   ex de,hl
1054   pop bc                         ; << 0k
1055   djnz ShiftRightLoop
1056   ret
1057
1058 ShiftLeft1:
1059   ld a,1
1060 ShiftLeft:
1061   ld (ShiftLeftCounter),a
1062   ld a,c
1063   ld (ShiftLeftChunk),a
1064   rla
1065   jr nc,ShiftLeftSameByte
1066   dec hl
1067 ShiftLeftSameByte:
1068   ex de,hl
1069   ld bc,-16
1070   add hl,bc
1071   ex de,hl
1072   ld b,57
1073 ShiftLeftLoop:
1074   push bc                        ; >> 1
1075   ld bc,-32
1076   add hl,bc
1077   ex de,hl
1078   ld a,(de)
1079 ShiftLeftChunk:
1080   or a
1081   call c,_SHRACC
1082   ld c,0
1083 ShiftLeftCounter = $-1
1084 ShiftRowsRight:
1085   push hl                        ; >> 2
1086   rra
1087   ld b,16
1088 ShiftRowRight:
1089   rr (hl)
1090   inc hl
1091   djnz ShiftRowRight
1092   pop hl                         ; << 1
1093   dec c
1094   jr nz,ShiftRowsRight
1095   ld bc,-16
1096   add hl,bc
1097   ex de,hl
1098   pop bc                         ; << 0k
1099   djnz ShiftLeftLoop
1100   ret
1101
1102 Chunk:
1103   push hl                        ; >> 1
1104   push de                        ; >> 2
1105   ld c,16
1106   add hl,bc
1107   ld b,57
1108 ChunkScreen:
1109   push bc                        ; >> 3
1110   ld bc,-32
1111   add hl,bc
1112   ex de,hl
1113   ld a,(de)
1114   call _SHRACC
1115   ld b,16
1116 ChunkRow:
1117   dec hl
1118   rld
1119   djnz ChunkRow
1120   ex de,hl
1121   pop bc                         ; << 2
1122   djnz ChunkScreen
1123   pop de                         ; << 1
1124   pop hl                         ; << 0k
1125   ld c,$37                  ;scf
1126   ret
1127
1128 ;-----------------------------
1129 ;----------- link ------------
1130 ;-----------------------------
1131
1132 LINKMASK =3
1133 LINKPORT =7
1134 TIMEOUT  =$1000
1135
1136 LinkPrep:
1137         ex (sp),hl
1138         push bc
1139         push de
1140         set 2,(iy+12h)
1141         ld b,8
1142         jp (hl)
1143
1144 TryReceiveByte:
1145         in  a,(LINKPORT)
1146         and LINKMASK
1147         cp  LINKMASK
1148         scf
1149         ret z
1150 ReceiveByteCont:
1151         call LinkPrep
1152         jr ReceiveCont
1153 ReceiveByte:
1154         call LinkPrep
1155 ReceiveBits:
1156         ld de,TIMEOUT
1157 WaitRecBit:
1158         call CheckLink
1159         jr z,LinkFailed
1160         cp LINKMASK
1161         jr z,WaitRecBit
1162 ReceiveCont:
1163         sub LINKMASK/3*2
1164         ld a,LINKMASK/3*2
1165         ld d,D0LD1H
1166         jr c,ReceiveLow
1167         rra
1168         ld d,D0HD1L
1169 ReceiveLow:
1170         rr c
1171         ld (AckBit),a
1172         ld a,d
1173         out (LINKPORT),a
1174         ld de,TIMEOUT
1175 WaitAckRec:
1176         call CheckLink
1177         cp 0
1178 AckBit =$-1
1179         jr nz,WaitAckRec
1180         ld a,D0HD1H
1181         out (LINKPORT),a
1182         ld d,4
1183 WaitReadyRec:
1184         dec d
1185         jr z,ReadyRec
1186         in a,(LINKPORT)
1187         cp LINKMASK
1188         jr nz,WaitReadyRec
1189 ReadyRec:
1190         djnz ReceiveBits
1191         jr LinkSuccess
1192
1193 SendByte:
1194         call LinkPrep
1195         ld c,a
1196
1197         inc b
1198         jr SendAcked
1199
1200 SendBits:
1201         rr c
1202         ld a,D0LD1H
1203         jr nc,SendLow
1204         ld a,D0HD1L
1205 SendLow:
1206         out (LINKPORT),a
1207         ld de,TIMEOUT
1208 WaitAckSend:
1209         call CheckLink
1210         jr nz,WaitAckSend
1211 SendAcked:
1212         ld a,D0HD1H
1213         out (LINKPORT),a
1214         ld de,TIMEOUT
1215 WaitReadySend:
1216         call CheckLink
1217         cp LINKMASK
1218         jr nz,WaitReadySend
1219         djnz SendBits
1220 LinkSuccess:
1221         or 0
1222 .org $-1
1223 LinkFailed:
1224         scf
1225         ld a,c
1226         res 2,(iy+12h)
1227         pop de
1228         pop bc
1229         pop hl
1230         ret
1231
1232 CheckLink:
1233         pop hl
1234         dec de
1235         ld a,d
1236         or e
1237         jr z,LinkFailed
1238         ld a,0BFh
1239         call _readkeypad
1240         bit 6,a
1241         jr z,LinkFailed
1242         in a,(LINKPORT)
1243         and LINKMASK
1244         jp (hl)
1245
1246 _readkeypad:
1247         out (1),a
1248         in a,(1)
1249         push af
1250         ld a,255
1251         out (1),a
1252         pop af
1253         ret
1254
1255
1256
1257 ;-----------------------------
1258 ;---------- levels -----------
1259 ;-----------------------------
1260
1261 LevelsDM:
1262
1263   .db 8,0
1264   .db $40,30,4,$C0,30,123
1265   .db 0,0
1266 ;  .db 128,57
1267   .db 0
1268
1269 NUM_LEVELS = 9
1270
1271 LevelsS:
1272
1273   .db 5,3           ;5 peas, speed 5
1274   .db 0,4,64,0,0,0  ;d,y,x
1275   .db 0,0           ;field width, height
1276   .db 0             ;no additional lines
1277
1278   .db 8,4
1279   .db $40,14,4,0,0,0
1280   .db 128,57
1281   .db 1
1282   .db 28,28,100,28
1283
1284   .db 9,4
1285   .db $40,8,4,0,0,0
1286   .db 128,57
1287   .db 2
1288   .db 28,14,100,14
1289   .db 28,41,100,41
1290
1291   .db 9,3
1292   .db $40,8,4,0,0,0
1293   .db 128,80
1294   .db 2
1295   .db 64,14,64,66
1296   .db 20,40,108,40
1297
1298   .db 10,3
1299   .db 4,8,$40,0,0,0
1300   .db 128,90
1301   .db 3
1302   .db 18,20,18,70
1303   .db 110,20,110,70
1304   .db 18,45,110,45
1305
1306   .db 7,3
1307   .db 64,4,0,0,0,0
1308   .db 128,86
1309   .db 6
1310   .db 34,13,56,35
1311   .db 34,71,56,49
1312   .db 72,35,94,13
1313   .db 72,49,94,71
1314   .db 16,20,16,64
1315   .db 110,20,110,64
1316
1317   .db 9,2
1318   .db 4,10,$40,0,0,0
1319   .db 128,82
1320   .db 3
1321   .db 0,20,74,20
1322   .db 54,40,128,40
1323   .db 0,60,74,60
1324
1325   .db 12,2
1326   .db 64,4,0,0,0,0
1327   .db 128,90
1328   .db 6
1329   .db 20,16,54,16
1330   .db 74,16,110,16
1331   .db 20,16,20,72
1332   .db 110,16,110,72
1333   .db 20,72,54,72
1334   .db 74,72,110,72
1335
1336   .db 8,2
1337   .db 72,52,$c0,0,0,0
1338   .db 128,128
1339   .db 13
1340   .db 34,56,56,34
1341   .db 34,72,56,94
1342   .db 72,34,94,56
1343   .db 72,94,94,72
1344   .db 46,64,80,64
1345   .db 22,11,22,33
1346   .db 11,22,33,22
1347   .db 22,95,22,117
1348   .db 11,106,33,106
1349   .db 103,11,103,33
1350   .db 91,22,115,22
1351   .db 103,95,103,117
1352   .db 91,106,115,106
1353
1354
1355 ;-----------------------------
1356 ;---------- data -------------
1357 ;-----------------------------
1358
1359 TrigPrecalc:
1360 .db   0,  3,  6,  9, 12, 15, 18, 21
1361 .db  24, 27, 30, 33, 36, 39, 42, 45
1362 .db  48, 51, 54, 57, 59, 62, 65, 67
1363 .db  70, 73, 75, 78, 80, 82, 85, 87
1364 .db  89, 91, 94, 96, 98,100,102,103
1365 .db 105,107,108,110,112,113,114,116
1366 .db 117,118,119,120,121,122,123,123
1367 .db 124,125,125,126,126,126,126,126
1368 .db 127
1369
1370 #include "line.asm"
1371
1372 txtWelcome .db "Welcome to PeaWorm!! ",
1373            .db "by Jonah and Shiar",0
1374 txtMenu    .db "Start Game",0
1375            .db "Game: ",0
1376            .db "Options",0
1377            .db "Quit",0
1378 txtGame    .db "Singleplayer",0
1379 txtGame2   .db "Deathmatch  ",0
1380            .db "Hot Pursuit",0
1381            .db "CTF        ",0
1382 WormMsg  .db "Wonderworm!",0
1383 LevelMsg .db "Level ",0
1384 LivesMsg .db "Lives: ",0
1385 ScoreMsg .db "Score: ",0
1386 LeftMsg  .db "Food: ",0
1387 HiScore  .db 0
1388
1389 nrworms .db 2
1390 worm1 .dw 0,0,0,0,0,0,0,0
1391       .db 0,%11111110,%10,%100
1392 worm2 .dw 0,0,0,0,0,0,0,0
1393       .db 0,0,%10000,%1000
1394 ;     .db 0,%00111111,%10000,%1000
1395
1396               ;set:
1397 heading = 0   ;level*
1398 pos     = 2   ;level*
1399 pos2    = 4   ;level
1400 head    = 6   ;level
1401 tail    = 8   ;level
1402 grow    = 10  ;level
1403 lives   = 13  ;game
1404 score   = 15  ;game
1405 input   = 17  ;
1406 left    = 18  ;
1407 right   = 19  ;
1408
1409 Worm1  = $B000
1410 Worm2  = $B800
1411 resbit = 2
1412
1413 Level =$+1
1414 DispBuffer =$+2
1415
1416 ScrBuffer   = $8200 ;32x256 bytes
1417 SinCosTable = $A200
1418
1419 ;-----------------------------
1420 ;----------- end -------------
1421 ;-----------------------------
1422
1423 .end