;The Fish Adventures of Mr. Fish
;Version 1.0 3/95

code1key equ 12

MAXLIVES=7

;these 3 lines don't matter anymore
MINBOUND=60+35+16
MAXBOUND=184-35-16
BARSIZE =17+4

SKYCOLOR=150
WATERCOLOR=144


fishx equ 128 
fishy equ 129 
fishd equ 130
ofishy equ 131
ztmp1  equ 132
ztmp2  equ 134

crabx equ 135
craby equ 136
ocraby equ 137 
crabd  equ 138

pirx equ 139
piry equ 140
opiry equ 141 
pird  equ 142
pird2 equ 143


oscrollflag equ 144
drawcount equ 145
deadflag  equ 146
fishspeed equ 147 ;your fish's speed*4
xscrollflag equ 148
leftbound equ 149
rightbound equ 150

snd0adr equ 151
snd1adr equ 153
snd2adr equ 155
snd3adr equ 157
snd0dur equ 159
snd1dur equ 160
snd2dur equ 161
snd3dur equ 162
snd0vol equ 163
snd1vol equ 164
snd2vol equ 165
snd3vol equ 166
sfishx  equ 167
spirx   equ 168
scrabx  equ 169
sfishy  equ 170
sfishd  equ 171
pauseflag equ 172
sdeadflag equ 173
baby1x  equ 174
baby2x  equ 175
baby3x  equ 176
baby4x  equ 177
code1   equ 178 ;not used
autotail equ 179    ;cartoon
noeraseflag equ 180 ;cartoon
bigflag equ 181 ;cartoon
obigflag equ 182 ;cartoon
bigcflag equ 183 ;cartoon
obigcflag equ 184 ;cartoon

;O.S. Equates
SETVBV equ $E45C
RANDOM equ $D20A

;Hardware Equates
DMACTL equ $D400
CHBASE equ $D409
WSYNC  equ $D40A
VCOUNT equ $D40B
COLPM0 equ $D012
COLPM1 equ $D013
COLPM2 equ $D014
COLPM3 equ $D015
COLPF0 equ $D016
COLPF1 equ $D017
COLPF2 equ $D018
COLPF3 equ $D019
COLBK  equ $D01A
HSCROL equ $D404
PRIOR  equ $D01B
SIZEP0 equ $D008
SIZEP1 equ $D009
SIZEP2 equ $D00A
SIZEP3 equ $D00B
SIZEM  equ $D00C
HPOSP0 equ $D000
HPOSP1 equ $D001
HPOSP2 equ $D002
HPOSP3 equ $D003
HPOSM0 equ $D004
HPOSM1 equ $D005
HPOSM2 equ $D006
HPOSM3 equ $D007

AUDF1  equ $D200
AUDC1  equ $D201
AUDF2  equ $D202
AUDC2  equ $D203
AUDF3  equ $D204
AUDC3  equ $D205
AUDF4  equ $D206
AUDC4  equ $D207
AUDCTL equ $D208
SKCTL  equ $D20F

;Player Missile Graphics Memory
pmmem equ 49152-2048
missile equ pmmem+768
play0 equ pmmem+1024
play1 equ pmmem+1024+256
play2 equ pmmem+1024+512
play3 equ pmmem+1024+768
pmmem2 equ 49152-4096
play0b equ pmmem2+1024
play1b equ pmmem2+1024+256
play2b equ pmmem2+1024+512
play3b equ pmmem2+1024+768
missile2 equ pmmem2+768


  org $2000

  include FONT.ASM

screen ds 22*1024

toplines
 sbyte " Player One  Pond Fish Clam  High Score "
 sbyte "   OOOOOO     OO   OO   OO     OOOOOO   "

  org $8000

dlist
  db 112,112,112,64+2
  dw toplines
  db 2+128,16
scrolltab ;22 three byte entries
  echo 22
  if %i=3 or %i=21
  db 64+4+128+16
  else
  db 64+4+16
  endif
  dw screen+%i*1024
  endm
  db 112,65
  dw dlist

titledlist
  db 112,112,112
  db 64+2
  dw toplines
  db 2
  db 112,112,112,112,112
  db 64+6
  dw titlemsg
  db 112,2,112,7,112,112
  db 2,2,2,2,2,2,112,112
  db 2,2,65
  dw titledlist

msgdlist
  db 112,112,112,112,112,112,112
  db 112,112,112,112,112,112,64+7
msgadr
  dw readymsg
  db 65
  dw msgdlist

readymsg
  sbyte "     r e a d y Ã    "
gameovermsg
  sbyte "      game over     "
pausemsg
  sbyte "     game paused    "

titlemsg
  sbyte "the fish-adventures "
  sbyte "                   of                   "

  sbyte "      MR. FISH      "

  sbyte "                                  "
  sbyte "      PressSelectto select pond.      "
  sbyte "                                  "
  sbyte "                                   "
  sbyte "     PressStartor button to Play!     "
  sbyte "                                   "
  sbyte " (C)1995 Ken Siders-free/NOT to be sold "
  sbyte " cp576@cleveland.freenet.edu  ver. 1.00 "

adrtabl
  echo 22
  db low [screen+%i*1024]
  endm

adrtabh
  echo 22
  db high [screen+%i*1024]
  endm

dli1
;set hardware reg for 756/710/709/559
  pha
  lda 559
  ora #1
  sta WSYNC
  sta DMACTL
  lda #high font
  sta CHBASE
  lda #34
  sta COLPF2
  lda #200
  sta COLPF1
  lda #low dli2
  sta 512
  lda #high dli2
  sta 513
  pla
  rti
;set water color in backfround
;set read color for 00 pixels
;set player priority to 08
dli2
  pha
  lda #WATERCOLOR
  sta WSYNC
  sta COLBK ;water color for background
  lda #70 ;set red color in "11" pixels
  sta colpf3
  lda #8+16 ;missiles red
  sta PRIOR
  lda #low dli3
  sta 512
  lda #high dli3
  sta 513
  pla
  rti
;bottom of screen color
dli3
  pha
  lda #34
  sta WSYNC
  sta COLBK
  lda #low dli1
  sta 512
  lda #high dli1
  sta 513
  lda #0
  sta 77
  pla
  rti

  include VBI.ASM
; vbi

go
  lda #0
  sta 559
  sta bigflag
  sta obigflag
  sta bigcflag
  sta obigcflag
  sta snd0adr+1
  sta snd1adr+1
  sta snd2adr+1
  sta snd3adr+1
  sta snd0dur
  sta snd1dur
  sta snd2dur
  sta snd3dur
  sta AUDCTL
  sta 20
  sta ofishy
  sta ocraby
  sta opiry
  sta fishy
  sta piry
  sta craby
  sta scrollflag
  sta fishspeed
  sta scrollpos+1
  sta scrollpos
  lda #1
  sta code1
  lda #3
  sta SKCTL
  lda #127
  sta fishx
  sta pirx
  sta crabx
  lda #1
  sta pird
  sta crabd
  sta pird2
  sta pond
  sta level
  lda #$10
  sta maxclams
  sta clams
  lda #0
  sta lives
:lpgo1
  lda 20
  cmp #5
  bne :lpgo1

********************
* Start Game       *
********************
startgame
  jsr showscore
  lda #0
  sta baby1x
  sta baby2x
  sta baby3x
  sta baby4x
  sta fishx
  sta crabx
  sta pirx
  lda #200
  sta 709
  lda #12
  sta 708
  lda #WATERCOLOR
  sta 712
  sta 710
  lda #0
  lda #low titledlist
  sta 560
  lda #high titledlist
  sta 561
  lda #62
  sta 559
:wait1start
  lda 20
  and #32
  lsr a! lsr a! lsr a! lsr a
;   ora #2
  sta 755
  lda 53279
  cmp #5
  bne :not2sel
  inc level
  ldx level
  lda clamtab-1,x
  sta clams
  sed
  clc
  lda pond
  adc #1
  sta pond
  cmp #$17
  bcc :okw2
  lda #1
  sta pond
  sta level
:okw2
  cld
  jsr showscore
  lda #0
  sta 20
:waitrel
  lda 53279
  cmp #5
  bne :not2sel
  lda 20
  and #32
  beq :waitrel
:not2sel
  lda 644
  beq gottrig
  lda 53279
  cmp #6
  bne :wait1start
  dec level
  sed ! sec ! lda pond ! sbc #1
  sta pond ! cld
:wait2start
  lda 20
  and #32
  lsr a! lsr a! lsr a! lsr a
;   ora #2
  sta 755
  lda 53279
  cmp #6
  beq :wait2start
  jmp new2game
gottrig
  lda 644
  beq gottrig
newgame
new2game
  lda #-2
  sta cartoonnum
  lda #255
  sta 764
  lda #2
  sta 755
  lda #0
  sta 559
  sta pauseflag
  lda #MINBOUND
  sta leftbound
  lda #MAXBOUND
  sta rightbound
  lda #BARSIZE
  sta barwide
  lda #0
  sta score
  sta score+1
  sta score+2
  sta levelscore
  sta levelscore+1
  sta levelscore+2
  lda #8
  sta maxclams
  lda #MAXLIVES
  sta lives
newlevel
  lda #64
  sta 54286
  lda #0
  sta 559
  sta noeraseflag
  sta bigflag
  sta bigcflag
  sta autotail
  sta fishx
  sta crabx
  sta pirx
  sta fishscore
  sta fishscore+1
  sta 20
  lda 20
  cmp #5
  bne *-2-2
  sed
  clc
  lda pond
  adc #1
  sta pond
  cld
  inc level
  lda #low dlist
  sta 560
  lda #high dlist
  sta 561
  lda #224
  sta 756
  lda #SKYCOLOR
  sta 712
  sta 710
  lda #12
  sta 709
  lda #20
  sta 708
;place water color in 711 initially
  lda #WATERCOLOR
  sta 711
;set player priorities above water
  lda #1
  sta 623
;set difficulty for level
  ldx level
  cpx #17
  bcc :ok7t
  ldx #16
:ok7t
  dex
  lda leftboundtab,x
  sta leftbound
  lda rightboundtab,x
  sta rightbound
  lda barwidetab,x
  sta barwide
  lda clamtab,x
  sta maxclams
  sta clams
  lda tpirtab,x
  sta turbopir
  lda pirprobtab,x
  sta pirprob
  lda crabprobtab,x
  sta crabprob


************************************
* Clear Out Screen+pm Memory       *
************************************
  lda #low screen
  sta 203
  lda #high screen
  sta 204
  lda #low [22*1024]
  sta 205
  lda #high [22*1024]
  sta 206
  jsr xzeromem
  lda #low [pmmem+768]
  sta 203
  lda #high [pmmem+768]
  sta 204
  lda #low [1024+256]
  sta 205
  lda #high [1024+256]
  sta 206
  jsr xzeromem
  lda #low [pmmem2+768]
  sta 203
  lda #high [pmmem2+768]
  sta 204
  lda #low [1024+256]
  sta 205
  lda #high [1024+256]
  sta 206
  jsr xzeromem
************************************
* Draw Bottom of Screen            *
* and waves
************************************
  lda #126+128
  sta 0
  ldx #0
:drawbottom
;drawtop
  lda 0
  eor #1
  sta 0
  sta screen+1024*3,x
  sta screen+1024*3+256,x
  sta screen+1024*3+512,x
  sta screen+1024*3+768,x
;draw bottom
  lda RANDOM
  and #3
  clc
  adc #71
  sta screen+1024*21,x
  lda RANDOM
  and #3
  clc
  adc #71
  sta screen+1024*21+256,x
  lda RANDOM
  and #3
  clc
  adc #71
  sta screen+1024*21+512,x
  lda RANDOM
  and #3
  clc
  adc #71
  sta screen+1024*21+768,x
  inx
  bne :drawbottom
***********************************
*                                 *
***********************************
;install vbi
  lda :exitvbi
  cmp #255
  bne :exitset
  cmp :exitvbi+1
  bne :exitset
  lda 546+2
  sta :exitvbi
  lda 547+2
  sta :exitvbi+1
:exitset
  ldy #low vbi
  ldx #high vbi
  lda #7 ;def vbi
  jsr SETVBV

  lda #low dli1
  sta 512
  lda #high dli1
  sta 513
  lda #192
  sta 54286
; enable screen and player missile
; graphics
  lda #high pmmem
  sta 54279
  lda #3
  sta 53277
;set player widths
  lda #0
  sta SIZEP0
  sta SIZEP1
  sta SIZEP2 ;changed in vbi
  sta SIZEP3

  ldy #0
  lda #0
  jsr drawbarrier
  sec
  lda #low 1023
  sbc barwide
  tay
  lda #high 1023
  sbc #0
  jsr drawbarrier

  lda #40
  sta drawcount
:lpdplnt
  jsr rdrawplant1
  dec drawcount ;^^^
  bne :lpdplnt

  lda #20
  sta drawcount
:lpd2plnt
  jsr rdrawplant2
  dec drawcount ;^^^
  bne :lpd2plnt

  lda #12
  sta drawcount
:lpdcrl
  jsr rdrawcoral1
  dec drawcount
  bne :lpdcrl

  lda maxclams
  sta drawcount
:lpxdclam
  jsr rdrawclam
  sed
  sec
  lda drawcount
  sbc #1
  sta drawcount
  cld
  bne :lpxdclam
  lda #0
  sta scrollpos+1
  lda #12
  sta scrollpos
  jmp startlevel

showmsg
  sta msgadr
  sty msgadr+1
  ldy #64
  sty 54286
  ldy #0
  sty 559
  sty deadflag
  sty deadtype
  sty 20
:lpw76
  ldy 20
  beq :lpw76
  cmp #low gameovermsg
  bne :notsk85
  inc deadflag
:notsk85
  ldy #0
  sty fishd
  lda #64
  sta 54286
  lda #64
  sta fishx
  lda #255-64-8
  sta pirx
  lda #1
  sta pird
  lda #114
  sta fishy
  lda #112
  sta piry
  lda #119
  sta crabx
  lda #96
  sta craby
  lda #WATERCOLOR
  sta 712
  lda #low msgdlist
  sta 560
  lda #high msgdlist
  sta 561
  lda #62
  sta 559
  rts

startlevel
  lda #low readymsg
  ldy #high readymsg
  jsr showmsg
  lda #0
  sta 20
  lda 20
  cmp #60*3
  bcc *-2-2
  lda #0
  sta 559
  lda #SKYCOLOR
  sta 712
  lda #low dlist
  sta 560
  lda #high dlist
  sta 561
  lda #192
  sta 54286
  lda #127
  sta fishx
  sta fishy
  lda #0
  sta fishspeed
  sta fishd ;right
  lda #203
  sta craby
  lda #0
  sta 559
  sta crabx
  sta pirx
  sta deadflag
  lda code1
  bne :norss
  lda #0
  sta scrollpos+1
  lda #12
  sta scrollpos
:norss
  lda #1
  sta scrollflag
  lda #1
  sta xscrollflag
  lda #62
  sta 559
mainloop
  lda #0
  sta 20
  lda deadflag
  beq :ndf
  lda zz
  beq :nd1
  lda deadtype
  cmp #2
  bcc :dead01
  lda fishy
  cmp #83-9
  bcc :nd1
  dec fishy
  jmp :nd1
:dead01
  lda fishy
  cmp #203
  bcs :nd1
  inc fishy
:nd1 lda #15
  bne checkup ;always
:ndf lda 632
checkup
  lsr a
  bcs checkdn
  ldx fishy
  cpx #83
  bcc checkdn
  dec fishy
checkdn
  lsr a
  bcs checklf
  ldx fishy
  cpx #203
  bcs checklf
  inc fishy
checklf
  lsr a
  bcs checkrt
  ldx #1
  stx fishd ;fish left
checkrt
  lsr a
  bcs donecheck
  ldx #0
  stx fishd ;fish right
donecheck


  inc zz2
  lda zz2
  and #31
  sta zz2
  lda zz
  eor #3
  sta zz
  lda fishspeed
  beq :none2
  bpl posspeed
negspeed
  lda zz2
  bne :noinc
  inc fishspeed
:noinc
  lda fishspeed
  eor #255
  sec
  adc #0
  sta 0
  and #3
  cmp zz
  bcc *+2+2
  dec fishx
  lda 0
  lsr a
  lsr a
  and #3
  tax
  beq :none2
:lp02
  dec fishx
  dex
  bne :lp02
:none2
  jmp checkboundries

posspeed
  lda zz2
  bne :nodec
  dec fishspeed
:nodec
  lda fishspeed
  sta 0
  and #3
  cmp zz
  bcc *+2+2
  inc fishx
  lda 0
  lsr a
  lsr a
  and #3
  tax
  beq :none
:lp01
  inc fishx
  dex
  bne :lp01
:none
  jmp checkboundries

checkboundries
  ldx deadflag
  bne notleft ;no button response if dead
  ldx fishd
  bne notright
  ldx 644
  cpx lasttrig
  beq notleft
  stx lasttrig
  cpx #1
  beq notleft
  inc fishspeed
  ldx fishspeed
  bmi notleft ;slowing down
  cpx #16
  bcc notleft
  dec fishspeed
notright
  ldx fishd
  cpx #1
  bne notleft
  ldx 644
  cpx lasttrig
  beq notleft
  stx lasttrig
  cpx #1
  beq notleft
  dec fishspeed
  lda fishspeed
  eor #255
  tax
  inx
  bmi notleft ;slowing down
  cpx #16
  bcc notleft
  inc fishspeed
notleft
:lp001
  ldx fishx
  cpx leftbound
  bcs :ok001
  inc fishx
  dec scrollflag
  dec xscrollflag
  jmp :lp001
:ok001
:lp002
  ldx fishx
  cpx rightbound
  bcc :ok002
  dec fishx
  inc scrollflag
  inc xscrollflag
  jmp :lp002
:ok002
;handle movement of the crab
  lda crabx
  bne movecrab
  lda RANDOM
  cmp #4
  bcs nomovecrab
  lda #255
  sta crabx
  lda #1
  sta crabd
  lda RANDOM
  bpl movecrab
  lda #255
  sta crabd
  lda #1
  sta crabx
movecrab
  lda zz
  beq :nomc
  lda xscrollflag
  clc
  adc crabd ;+1 or -1
  eor #255
  tax
  inx
  txa
  bmi :neg01
:pos01
  clc
  adc crabx
  sta crabx
  bcc nomovecrab
  lda #0
  sta crabx
  beq nomovecrab ;always
:neg01
  clc
  adc crabx
  sta crabx
  bcs nomovecrab
  lda #0
  sta crabx
;     beq nomovecrab ;always

;     bvc nomovecrab ;@@@
;     cmp #16
;     bcs nomovecrab ;value is ok
;     lda #0
;     sta crabx ;turn off crab
:nomc
nomovecrab

;handle movement of the piranha
  lda turbopir
  bne :movepir
  lda zz
  bne :movepir ;@@@
  jmp nomovepir; :nomp
:movepir
  lda pirx
  bne movepir
  lda RANDOM
  cmp pirprob
  bcc *+5
  jmp nomovepir
;turn on the piranha
;make sound
  lda #255
  sta pirx
  lda #1
  sta pird
  lda RANDOM
  bpl movepir
  lda #255
  sta pird
  lda #1
  sta pirx
;generate a y value 87 to 195
redo lda RANDOM
  cmp #87
  bcc redo
  cmp #196
  bcs redo
  sta piry
;generate a y dir
redo2 lda RANDOM
  and #3
  cmp #3
  beq redo2
  tax
  dex ;makes number -1,0,or 1
  stx pird2
movepir
;small chance to change x dir
  lda RANDOM
  cmp #32
  bcs :nochpird
  lda RANDOM
  bne :nochpird
  lda pird
  eor #255
  tax
  inx
  stx pird
:nochpird
;slightly greater chance to change
;y direction
  lda RANDOM
  cmp #2
  bcs :noch2pird
redo3 lda RANDOM
  and #3
  cmp #3
  beq redo3
  tax
  dex
  stx pird2
:noch2pird
  lda xscrollflag
  clc
  adc pird ;+1 or -1
  eor #255
  tax
  inx
  txa
  bmi :neg02
:pos02
  clc
  adc pirx
  sta pirx
  bcc changepiry
  lda #0
  sta pirx
  beq changepiry ;always
:neg02
  clc
  adc pirx
  sta pirx
  bcs changepiry
  lda #0
  sta pirx
;     cmp #16
;     bcs changepiry ;value is ok
;     lda #0
;     sta pirx ;turn off piranha
;     beq nomovepir ;always
changepiry
  clc
  lda piry
  adc pird2
  sta piry
  cmp #87
  bcc :bady
  cmp #196-16
  bcc nomovepir ;value is ok
  bcs :bady2
:bady
  inc piry
  inc piry ;compensate for next
:bady2
  dec piry
  lda pird2
  eor #255
  tax
  inx
  stx pird2
  jmp changepiry ;redo y movement
:nomp
nomovepir

  lda zz
  beq :noupdt
  lda #0
  sta xscrollflag
:noupdt

;animate bubbles from clams
  lda zz
  and #63
  bne :nobubble
  ldy bubbledata
  ldx #0
:lpbubble
  lda bubbledata+1,x
  sta bubbledata,x
  inx
  cpx #15
  bne :lpbubble
  sty bubbledata+15
:nobubble


**********************************
* Check for Good Collisions      *
**********************************

;check for fish to closed clam collision
  lda zz
  beq *+5
:jnotclam jmp :notclam
  lda deadflag
  bne :jnotclam
  ldy fishx
  ldx fishy
  jsr locate
  cmp #128+22
  bne :not2cl
  ldx ztmp1
  bne *+4
  dec ztmp1+1
  dec ztmp1
  jmp :onclam
:not2cl
  cmp #128+21
  bne :jnotclam
:onclam
  lda #low clamsound
  sta snd0adr
  lda #high clamsound
  sta snd0adr+1
  jsr drawopen
  sed
  sec
  lda clams
  sbc #1
  sta clams
  clc
  lda fishscore
  adc #$50
  sta fishscore
  lda fishscore+1
  adc #0
  sta fishscore+1
  clc
  lda score
  adc fishscore
  sta score
  lda score+1
  adc fishscore+1
  sta score+1
  lda score+2
  adc #0
  sta score+2
  cld
  inc try
  lda try
  and #3
  sta try
  asl a
  tax
  lda trytab,x
  sta :vc1
  lda trytab+1,x
  sta :vc1+1
:vc1 equ *+1
  jmp $FFFF

trytab dw :try1,:try2,:try3,:try4

:try1
  inc baby1x ;disable baby movement
  ldy fishy ! iny ! iny ! iny ! iny
  ldx #5
:lpdr1baby
  lda babydata,x
  ora missile,y
  sta missile,y
  iny
  dex
  bpl :lpdr1baby
  lda fishx
  sta baby1x
  jmp :notclam
:try2
  inc baby2x ;disable baby movement
  ldy fishy ! iny ! iny ! iny ! iny
  ldx #5
:lpdr2baby
  lda babydata,x
  asl a ! asl a ! asl a ! asl a
  ora missile,y
  sta missile,y
  iny
  dex
  bpl :lpdr2baby
  lda fishx
  sta baby2x
  jmp :notclam
:try3
  inc baby3x ;disable baby movement
  ldy fishy ! iny ! iny ! iny ! iny
  ldx #5
:lpdr3baby
  lda babydata,x
  ora missile2,y
  sta missile2,y
  iny
  dex
  bpl :lpdr3baby
  lda fishx
  sta baby3x
  jmp :notclam
:try4
  inc baby4x ;disable baby movement
  ldy fishy ! iny ! iny ! iny ! iny
  ldx #5
:lpdr4baby
  lda babydata,x
  asl a ! asl a ! asl a ! asl a
  ora missile2,y
  sta missile2,y
  iny
  dex
  bpl :lpdr4baby
  lda fishx
  sta baby4x
:try5

:notclam

**********************************
* check for death-ful collisions *
**********************************
;check for piranha mouth touching
;your fish
  lda pird
  bmi :chk1
:chk0 ;piranha is going left
  lda pirx
  sec 
  sbc fishx
  cmp #16
  bcs :not1dead
  bcc :chk2
:chk1 ;piranha is going right
  lda fishx
  sec
  sbc pirx
  cmp #16
  bcs :not1dead
:chk2
  sec
  lda fishy
  sbc piry
  cmp #16
  bcc :dead6
  sec
  lda piry
  sbc fishy
  cmp #6
  bcs :not1dead
:dead6 jmp dead
:not1dead

;check for crab claws touching
;your fish

  sec
  lda crabx
  sbc fishx
  cmp #15
  bcc :chk4
  sec
  lda fishx
  sbc crabx
  cmp #15
  bcs :not2dead
:chk4
  sec
  lda craby
  sbc fishy
  cmp #13
  bcs :not2dead
  jmp dead
:not2dead
  jmp notdead
dead
  lda deadflag
  bne :nodeadsnd
  lda #low deadsnd
  sta snd0adr
  lda #high deadsnd
  sta snd0adr+1
  lda RANDOM
  and #3
  cmp #3
  beq *-2-2-3
  sta deadtype
:nodeadsnd
  lda #1
  sta deadflag
notdead
  lda deadflag
  beq noreplay
  lda deadtype
  cmp #2
  bcc :not02
  lda fishy
  cmp #83+1
  bcc replayx
  bcs noreplay
:not02
  lda fishy
  cmp #203
  bcc noreplay
replayx
  lda fishspeed
  bne noreplay
  inc deadflag
  lda deadflag
  cmp #70
  bcc noreplay
:startlev
  sed
  sec
  lda lives
  sbc #1
  sta lives
  cld
  lda lives
  bmi xgameover
  jmp startlevel
xgameover
  lda #0
  sta lives
  jsr showscore
  lda #low gameovermsg
  ldy #high gameovermsg
  jsr showmsg
  sta 20
  lda 20
  cmp #240
  bne *-2-2
  jmp startgame
noreplay
  lda 764
  cmp #33
  beq *+5
  jmp :notpaused
  lda #255
  sta 764
  inc pauseflag
  lda #0
  sta 559
  sta 20
  lda 20
  beq *-2
  lda fishx
  sta sfishx
  lda fishy
  sta sfishy
  lda fishd
  sta sfishd
  lda pirx
  sta spirx
  lda crabx
  sta scrabx
  lda deadflag
  sta sdeadflag   
  lda #low pausemsg
  ldy #high pausemsg
  jsr showmsg
:lpunp
  lda 764
  cmp #33
  bne :lpunp
  lda #0
  sta 559
  sta 20
  lda 20
  beq *-2
  lda sfishx
  sta fishx
  lda sfishy
  sta fishy
  lda sfishd
  sta fishd
  lda spirx
  sta pirx
  lda scrabx
  sta crabx
  lda sdeadflag
  sta deadflag
  lda #203
  sta craby   
  lda #low dlist
  sta 560
  lda #high dlist
  sta 561
  lda #SKYCOLOR
  sta 712
  sta 710
  lda #12
  sta 709
  lda #20
  sta 708
  lda #WATERCOLOR
  sta 711
  lda #192
  sta 54286
  lda #62
  sta 559
  lda #255
  sta 764
  dec pauseflag
:notpaused
  jsr animateplant
  lda 53279
  cmp #6
  bne :notstart
  lda 53279
  cmp #6
  beq *-5
  dec level
  sed
  lda pond
  sbc #1
  sta pond
  cld
  jmp newgame
:notstart
  lda 53279
  cmp #3
  bne :notoption
  lda 53279
  cmp #3
  beq *-5
  lda #0
  sta 53279
  jmp startgame
:notoption
  lda 53279
  cmp #5
  bne :notselect
  jmp startgame
:notselect
:qq1 lda 20
  beq :qq1
  lda scrollflag
  bne *-3
  jsr showscore
;
  lda 764
  cmp #248
  beq :newlevel
  lda clams
  beq :newlevel
  jmp mainloop
:newlevel
  lda #255
  sta 764
  lda #low clearsnd
  sta snd0adr
  lda #high clearsnd
  sta snd0adr+1
  lda #0
  sta 20
  sed
  clc
  lda levelscore
  adc #low $1000
  sta levelscore
  lda levelscore+1
  adc #high $1000
  sta levelscore+1
  lda levelscore+2
  adc #0
  sta levelscore+2
  clc
  lda score
  adc levelscore
  sta score
  lda score+1
  adc levelscore+1
  sta score+1
  lda score+2
  adc levelscore+2
  sta score+2
  cld
  jsr showscore
  lda 20
  cmp #120
  bne *-4
  jsr cartoon
  jmp newlevel


********************************
  include MEM.ASM
  include SOUND.ASM
********************************

;y/a is x cord
drawbarrier
  sta 0
  clc
  tya
  adc #low[screen+3*1024]
  sta ztmp2
  lda 0
  adc #high[screen+3*1024]
  sta ztmp2+1
  lda barwide
  pha
  ldy #0
:lpdrbar
  lda ztmp2 ! sta ztmp1
  lda ztmp2+1 ! sta ztmp1+1
  jsr :draw1
  iny
  dec barwide
  bne :lpdrbar
  pla
  sta barwide
  rts

:draw1
  ldx #21-3
:lpdbr
  lda RANDOM
  and #1
  clc
  adc #87+2
  sta (ztmp1),y
  clc
  lda ztmp1+1
  adc #4
  sta ztmp1+1
  dex
  bne :lpdbr
  lda #66
  sta (ztmp1),y
  rts

rdrawplant2
  ldx #21
  jsr randcommon
drawplant2
  clc
  adc adrtabl-6,x
  sta ztmp1
  tya
  adc adrtabh-6,x
  sta ztmp1+1
  ldx #35
  ldy #0
:lpd2pl
  txa
  sta (ztmp1),y
  iny
  inx
  txa
  sta (ztmp1),y
  iny
  inx
  txa
  sta (ztmp1),y
  dey
  dey
  clc
  lda ztmp1+1
  adc #4
  sta ztmp1+1
  inx
  cpx #53
  bne :lpd2pl
  lda #66
  sta (ztmp1),y
  iny
  sta (ztmp1),y
  iny
  sta (ztmp1),y
  rts

rdrawplant1
  ldx #21
  jsr randcommon
drawplant1
  clc
  adc adrtabl-6,x
  sta ztmp1
  tya
  adc adrtabh-6,x
  sta ztmp1+1
  ldx #1
  ldy #0
:lpdpl
  txa
  sta (ztmp1),y
  iny
  inx
  txa
  sta (ztmp1),y
  dey
  clc
  lda ztmp1+1
  adc #4
  sta ztmp1+1
  inx
  cpx #13
  bne :lpdpl
  lda #66
  sta (ztmp1),y
  iny
  sta (ztmp1),y
  rts
rdrawcoral1
  ldx #21
  jsr randcommon
drawcoral1
  clc
  adc adrtabl-4,x
  sta ztmp1
  tya
  adc adrtabh-4,x
  sta ztmp1+1
  ldx #13+128
:lpdcl
  txa
  sta (ztmp1),y
  iny
  inx
  txa
  sta (ztmp1),y
  dey
  clc
  lda ztmp1+1
  adc #4
  sta ztmp1+1
  inx
  cpx #21+128
  bne :lpdcl
  lda #66
  sta (ztmp1),y
  iny
  sta (ztmp1),y
  rts

;generate a random number 0 to 1023
;ensures there are 4 blank spaces
;horiz and returns in a and y
randcommon
  lda RANDOM
  sta ztmp1
  lda RANDOM
  and #1+2 ;0 to 1023
  sta ztmp1+1
;see if 4 blank spaces
  clc
  lda adrtabl-1,x
  adc ztmp1
  sta ztmp2
  lda adrtabh-1,x
  adc ztmp1+1
  sta ztmp2+1
  ldy #0
  lda (ztmp2),y
  bne randcommon
  iny
  lda (ztmp2),y
  bne randcommon
  iny
  lda (ztmp2),y
  bne randcommon
  iny
  lda (ztmp2),y
  bne randcommon
;we got a good byte
  lda ztmp1
  ldy ztmp1+1
  rts

;draws opened clam, ztmp1 must point
;to upper left char (unlike other
;draw routines)
drawopen
  ldx #27+128
  ldy #1
  lda ztmp1
  bne *+2+2
  dec ztmp1+1
  dec ztmp1
:lpdclosed
  txa
  sta (ztmp1),y
  iny
  inx
  txa
  sta (ztmp1),y
  iny
  inx
  txa
  sta (ztmp1),y
  dey
  dey
  clc
  lda ztmp1+1
  adc #4
  sta ztmp1+1
  inx
  cpx #33+128
  bne :lpdclosed
  sec
  lda ztmp1
  sbc #low [2048+4]
  sta ztmp1
  lda ztmp1+1
  sbc #high [2048+4]
  sta ztmp1+1
  ldx #21-5
  iny! iny! iny
:lp2bub
  lda (ztmp1),y
  cmp #33
  bcc :noer1
  cmp #35
  bcs :noer1
  lda #0
  sta (ztmp1),y
:noer1
  sec
  lda ztmp1+1
  sbc #4
  sta ztmp1+1
  dex
  bne :lp2bub
  rts

rdrawclam
  ldx #21
  jsr randcommon
drawclam
  stx 0
  clc
  adc adrtabl-2,x
  sta ztmp1
  tya
  adc adrtabh-2,x
  sta ztmp1+1
  ldx #21+128
  ldy #1
:lpdclam
  txa
  sta (ztmp1),y
  iny
  inx
  txa
  sta (ztmp1),y
  iny
  inx
  txa
  sta (ztmp1),y
  dey
  dey
  clc
  lda ztmp1+1
  adc #4
  sta ztmp1+1
  inx
  cpx #27+128
  bne :lpdclam
  lda #66
  sta (ztmp1),y
  iny
  sta (ztmp1),y
  iny
  sta (ztmp1),y
  sec
  lda ztmp1
  sbc #low [2048]
  sta ztmp1
  lda ztmp1+1
  sbc #high [2048]
  sta ztmp1+1
  lda #32
  sta 1
  ldx 0
  ldy #0
  dex! dex! dex! dex! dex
:lpbub
  lda 1
  eor #1
  sta 1
  clc
  adc #1
  sta (ztmp1),y
  sec
  lda ztmp1+1
  sbc #4
  sta ztmp1+1
  dex
  bne :lpbub
  rts

  include DATA.ASM

*********************************

animateplant
;zz2 ranges 0 to 31 
  lda zz2
  and #15
  bne :noanimate
  lda zz3
  eor #2
  sta zz3
  tax
  lda RANDOM
  bmi :noanimate
  lda planttab,x
  sta ztmp1
  lda planttab+1,x
  sta ztmp1+1
  ldy #0
:lpanimate
  lda (ztmp1),y
  sta plantdata,y
  iny
  cpy #12*8
  bne :lpanimate
  lda zz3
  tax
  lda plant2tab,x
  sta ztmp1
  lda plant2tab+1,x
  sta ztmp1+1
  ldy #0
:lp2animate
  lda (ztmp1),y
  sta plant2data,y
  iny
  cpy #18*8
  bne :lp2animate
:noanimate
  rts

;returns char at xloc y and yloc x
locate
  txa
  sec
  sbc #32+8+8+1
  lsr a
  lsr a
  lsr a
  tax
  sec
  tya
  sbc #32
  clc
  adc scrollpos
  sta ztmp1
  lda scrollpos+1
  adc #0
  lsr a
  ror ztmp1
  lsr a
  ror ztmp1
  sta ztmp1+1
  clc
  lda ztmp1
  adc adrtabl,x
  sta ztmp1
  lda adrtabh,x
  adc ztmp1+1
  sta ztmp1+1
  ldy #0
  lda (ztmp1),y
  rts

showscore
  lda pond
  ldx #14
  jsr shownum
  lda clams
  ldx #24
  jsr shownum
  lda score+2
  ldx #3
  jsr shownum
  lda score+1
  ldx #5
  jsr shownum
  lda score+0
  ldx #7
  jsr shownum
  ldx #19
  lda lives
  jsr shownum
  lda score+2
  cmp hiscore+2
  beq *+4
  bcs :newhigh
  lda score+1
  cmp hiscore+1
  beq *+4
  bcs :newhigh
  lda score
  cmp hiscore
  beq *+4
  bcs :newhigh
  rts
:newhigh
  lda score
  sta hiscore
  ldx #35
  jsr shownum
  lda score+1
  sta hiscore+1
  ldx #33
  jsr shownum
  lda score+2
  sta hiscore+2
  ldx #31
  jsr shownum
  rts

shownum
  pha
  and #$F
  clc
  adc #scode '0'
  cmp #scode '0'
  bne *+2+2
  lda #scode 'O'
  sta toplines+41,x ; +25
  pla
  lsr a ! lsr a ! lsr a ! lsr a
  clc
  adc #scode '0'
  cmp #scode '0'
  bne *+2+2
  lda #scode 'O'
  sta toplines+40,x ;+24
  rts

wavecounter db 0
lasttrig db 1
bitpos db 3
scrollpos dw 12
bytepos  dw 0
scrollflag dw 0
zz db 0
zz2 db 0
zz3 db 0
score blkb 3 ;bcd
hiscore blkb 3 ;bcd
maxclams db 0 ;bcd
lives db 0 ;bcd
clams db 0
pond  db 0
fishscore dw 0
levelscore blkb 3
barwide db BARSIZE
level db 0
turbopir db 0
pirprob db 0
crabprob db 0
deadtype db 0
try db 0

     include CARTOON.ASM
     include CARTOON1.ASM
     include CARTOON2.ASM
     include CARTOON3.ASM
     include CARTOON4.ASM

     msg "top=$",*

     end go




