!ball damage options
!subs  AddScore, BallHit, Delay, ExitInfo, HandleBorders, HandleInput, HandleWalls, TestWalls, LoadData
!      LoadWalls, MakeBalls, PutBall, Menu, MenuBalls, MenuBallocity, MenuBounce, MenuControl
!      MoveBalls, OnBackKey, Play, Ramp, SaveData, SetScreen, SetVars, wait, ShowHiScores, ShowScore, Sort

mBalls=20
FILE.ROOT t$
isAPK=!IS_IN("rfo-basic",t$)
rdir$="HoverBall/"
file.exists t, rDir$
if !t then file.mkdir rDir$

SCREEN_W=640 : SCREEN_H=400
DIM BallsX[mBalls]
DIM BallsY[mBalls]
DIM BallsXV[mBalls]
DIM BallsYV[mBalls]
DIM BallsNum[mBalls]
DIM oBalls[mBalls]
DIM tBalls[mBalls]
DIM vWalls[4,4], hWalls[4,4], Lines[8]

DIM opt[11]

FN.DEF rand(r)
 r=FLOOR( RND()*r+1 )
 FN.RTN r
FN.END

!debug.on

GOSUB LoadData
GOSUB SetVars
GOSUB SetScreen
GOSUB Menu
GOSUB SaveData
GR.CLS
if ##$<>"" then GR.CLOSE
if ##$<>"" then run ##$
IF isAPK then exit
end

!=====
AddScore:
INPUT "You got a high score!", s$ , "", c
if trim$(s$)="" then return
BUNDLE.CREATE b
BUNDLE.PUT b, "name", s$
BUNDLE.PUT b, "score", score
BUNDLE.PUT b, "date", TIME()
LIST.ADD hScores, b
GOSUB sort
GOSUB ShowHiScores
RETURN

!======
BallHit:
IF BallsNum[i]=1
 gr.modify oBalls[i], "paint", pRed
 cBonus=max(cBonus,0)
 score+=100+cBonus
 cBonus+=bonus
 GR.MODIFY oBonus, "paint", pGreen
 SOUNDPOOL.PLAY n, bloop
 FOR j=2 TO nBalls
  t=--BallsNum[j]
  if t=1 then gr.modify oBalls[j], "paint", pGreen
  GR.MODIFY tBalls[j], "text", INT$(t)
 NEXT j
 balls++
 BallsNum[i]=nBalls-1
 GR.MODIFY tBalls[i], "text",INT$(nBalls-1)
 m=i
 GOSUB PutBall

ELSEIF BumpMode=1 & HitD<20
 swap BallsXV[1], BallsXV[i]
 swap BallsYV[1], BallsYV[i]
 
 SOUNDPOOL.PLAY n, crash
 if HitD<1 then life--: GR.MODIFY tBalls[1], "text", INT$(life)
 HitD=30
ELSEIF BumpMode=2
 halt=HaltDelay
 if HitD<1 then life--: GR.MODIFY tBalls[1], "text", INT$(life)
 HitD=30
 SOUNDPOOL.PLAY n, crash
 BallsXV[1]=0: BallsYV[1]=0
 m=i: GOSUB PutBall
ENDIF

!=========
Delay:
DO
UNTIL TIME()>tLast+tDelay
tLast=TIME()
RETURN

!!ExitInfo:
seconds=(time()-tStart)/1000
 str$(cFrames)+" frames in "+str$(seconds)+" seconds"
 str$(cFrames/seconds)+" average frames per second

? "Screen Width= "+str$(ACTUAL_W)+", scale="+str$(SCALE_X)
? "Screen Hieght= "+str$(ACTUAL_H)+", scale="+str$(SCALE_Y)
!!return

!========
HandleBorders:
s=0
hit=0

IF BorderMode=1 % Bounce off Borders
 FOR i=1 TO nBalls
  IF BallsX[i]<8 THEN BallsX[i]=9: BallsXV[i]*=-1: hit=1
  IF BallsX[i]>DISPLAY_W-8 THEN BallsX[i]=DISPLAY_W-9: BallsXV[i]*=-1: hit=1
  IF BallsY[i]<8 THEN BallsY[i]=9: BallsYV[i]*=-1: hit=1
  IF BallsY[i]>DISPLAY_H-8 THEN BallsY[i]=DISPLAY_H-9: BallsYV[i]*=-1: hit=1
  IF hit=1
   s=boing
   IF i=1
    BallsXV[1]*=Bounce: BallsYV[1]*=Bounce
    IF bHurt=1 & HitD<1 THEN GR.MODIFY tBalls[1], "text",INT$(--life)
    HitD=30
   ENDIF
  ENDIF
 NEXT i
ELSEIF BorderMode=2 %Phase across borders
 FOR i=1 TO nBalls
  IF BallsX[i]<8 THEN BallsX[i]=DISPLAY_W-8: hit=1
  IF BallsX[i]>DISPLAY_W-8 THEN BallsX[i]=8: hit=1
  IF BallsY[i]<8 THEN BallsY[i]=DISPLAY_H-8: hit=1
  IF BallsY[i]>DISPLAY_H-8 THEN BallsY[i]=8: hit=1
  IF hit=1 & i=1 & bHurt=1 & HitD<1 THEN GR.MODIFY tBalls[1], "text",INT$(--life): HitD=30
 NEXT i
ELSEIF BorderMode=3 %Warp across borders
 FOR i=1 TO nBalls
  r=Rand(2)
  IF BallsX[i]<8
   IF r=1 
    BallsX[i]=DISPLAY_W-8: BallsY[i]=rand(DISPLAY_H-40)+20
   ELSEIF BallsYV[i]<0
    BallsX[i]=rand(DISPLAY_W-40)+20: BallsY[i]=DISPLAY_H-8
   ELSE
    BallsX[i]=rand(DISPLAY_W-40)+20: BallsY[i]=8
   ENDIF
  ELSEIF BallsX[i] > DISPLAY_W-8
   IF r=1 
    BallsX[i]=8: BallsY[i]=rand(DISPLAY_H-40)+20
   ELSEIF BallsYV[i]<0
    BallsX[i]=rand(DISPLAY_W-40)+20: BallsY[i]=DISPLAY_H-8
   ELSE
    BallsX[i]=rand(DISPLAY_W-40)+20: BallsY[i]=8
   ENDIF
  ELSEIF BallsY[i] < 8
   IF r=1 
    BallsY[i]=DISPLAY_H-8: BallsX[i]=rand(DISPLAY_W-40)+20
   ELSEIF BallsXV[i]<0
    BallsY[i]=rand(DISPLAY_H-40)+20: BallsX[i]=DISPLAY_W-8
   ELSE
    BallsY[i]=rand(DISPLAY_H-40)+20: BallsX[i]=8
   ENDIF
  ELSEIF BallsY[i]>DISPLAY_H-8
   IF r=1 
    BallsY[i]=8: BallsX[i]=rand(DISPLAY_W-40)+20
   ELSEIF BallsXV[i]<0
    BallsY[i]=rand(DISPLAY_H-40)+20: BallsX[i]=DISPLAY_W-8
   ELSE
    BallsY[i]=rand(DISPLAY_H-40)+20: BallsX[i]=8
   ENDIF
  ENDIF
 NEXT i
ENDIF

IF s>0 THEN SOUNDPOOL.PLAY n,s
RETURN

!======
HandleInput:
IF halt>0 THEN halt--: RETURN
IF cBonus=50 then GR.MODIFY oBonus, "paint", pRed
gr.modify oBonus, "x1",10, "x2", cBonus+10

GR.TOUCH touched, tX, tY
tX/=SCALE_X: tY/=SCALE_Y

IF held=0
 IF touched>0
  !user just touched the screen, set startx/y variables
  held=1
  sX=tX: sY=tY
  GR.MODIFY oPad, "x",tX, "y",tY
  GR.SHOW oPad
  GR.SHOW oThrust
 ENDIF
ELSE
 IF touched=0
  !user just released the touch, exit input
  held=0
  gr.hide oPad
  gr.modify oThrust, "x1",BallsX[1], "y1",BallsY[1], "x2",BallsX[1], "y2",BallsY[1]
  gr.hide oThrust
  RETURN
 ELSE
  !user still touching, alter velocity
  dX=tX-sX
  dY=tY-sY
  ang=atan2(dx,dy)
  mag=hypot

  dx=tX-sX %determine the delta, or distance between the touch and the center of the pad
  dy=tY-sY
  ang=atan2(dx,dy) %calculate angle between the center of the pad and the users touch
  mag=min(hypot(dx,dy),dMax) %determine how far from the center the touch occurred (magnitude)
  
  dX=sin(ang)*mag
  dY=cos(ang)*mag


!  IF dX>dMax THEN dX=dMax
!  IF dX<-dMax THEN dX=-dMax
!  IF dY>dMax THEN dY=dMax
!  IF dY<-dMax THEN dY=-dMax
  gr.modify oThrust, "x1",BallsX[1], "y1",BallsY[1], "x2",BallsX[1]-(dx*2), "y2",BallsY[1]-(dy*2)

  BallsXV[1]+=dX*thrust: BallsYV[1]+=dY*thrust
 ENDIF
ENDIF

RETURN

!========
HandleWalls:
j=1
hit=0
GR.COLOR 255,0,0,0,1
FOR i=1 TO 4
 IF hWalls[i,1]>0
  !    gr.line n, hWalls[i,2], hWalls[i,1], hWalls[i,3], hWalls[i,1]
  FOR j=1 TO nBalls
   IF ABS(BallsY[j]-hWalls[i,1])<16
    IF BallsX[j]>hWalls[i,2] & BallsX[j]<hWalls[i,3] 
     IF (ABS(BallsX[j]-hWalls[i,2])<16 & BallsXV[j]>0) | (ABS(BallsX[j]-hWalls[i,3])<16 & BallsXV[j]<0)
      BallsXV[j]*=-1
      BallsX[j]+=BallsXV[j]
     ELSE
      BallsYV[j]*=-1
      BallsY[j]+=BallsYV[j]
     ENDIF
     hit=1
     SOUNDPOOL.PLAY n, boing
     IF j=1
      BallsXV[1]*=Bounce: BallsYV[1]*=Bounce
      IF wHurt=1 & HitD<1 THEN GR.MODIFY tBalls[1], "text",INT$(--life)
      HitD=30
     ELSE
     ENDIF
    ENDIF
   ENDIF  
  NEXT j
 ENDIF
NEXT i
FOR i=1 TO 4
 IF vWalls[i,1] >0
  !    gr.line n,vWalls[i,1], vWalls[i,2], vWalls[i,1], vWalls[i,3]
  FOR j=1 TO nBalls
   IF ABS(BallsX[j]-vWalls[i,1])<16
    IF BallsY[j]>vWalls[i,2] & BallsY[j]<vWalls[i,3]
     IF (ABS(BallsY[j]-vWalls[i,2])<16 & BallsYV[j]>0) | (ABS(BallsY[j]-vWalls[i,3])<16 & BallsYV[j]<0)
      BallsYV[j]*=-1
      BallsY[j]+=BallsYV[j]
     ELSE
      BallsXV[j]*=-1
      BallsX[j]+=BallsXV[j]
     ENDIF
     hit=1
     SOUNDPOOL.PLAY n, boing
     IF j=1
      BallsXV[1]*=Bounce: BallsYV[1]*=Bounce
      IF wHurt=1 & HitD<1 THEN GR.MODIFY tBalls[1], "text",INT$(--life)
      HitD=30
     ENDIF
    ENDIF
   ENDIF
  NEXT j
 ENDIF
NEXT i
RETURN  

TestWalls:
hit=0
FOR i=1 TO 4
 IF hWalls[i,1]>0
  IF ABS(BallsY[m]-hWalls[i,1])<24
   IF BallsX[m]>hWalls[i,2] & BallsX[m]<hWalls[i,3] 
    hit=1
   ENDIF
  ENDIF
 ENDIF
NEXT i
FOR i=1 TO 4
 IF vWalls[i,1] >0
  IF ABS(BallsX[m]-vWalls[i,1])<24
   IF BallsY[m]>vWalls[i,2] & BallsY[m]<vWalls[i,3]
    hit=1
   ENDIF
  ENDIF
 ENDIF
NEXT i
RETURN  

LoadData:
LIST.CREATE n, lArenas
LIST.CREATE n, lRules
TEXT.OPEN r, hData, rdir$+"arenas.dat"
DO
 TEXT.READLN hData, in$
 in$=LOWER$(TRIM$(in$))
 IF in$="#arena"
  BUNDLE.CREATE nBundle
  TEXT.READLN hData, in$
  BUNDLE.PUT nBundle, "Name", in$
  TEXT.READLN hData, in$
  BUNDLE.PUT nBundle, "hWalls", in$
  TEXT.READLN hData, in$
  BUNDLE.PUT nBundle, "vWalls", in$
  LIST.ADD lArenas, nBundle

 ELSEIF in$="#rule"
  BUNDLE.CREATE nBundle
  TEXT.READLN hData, in$
  BUNDLE.PUT nBundle, "Name", in$
  TEXT.READLN hData, in$
  BUNDLE.PUT nBundle, "Data", in$
  LIST.ADD lRules, nBundle
 ENDIF
UNTIL in$="eof"
LIST.SIZE lArenas, nArenas


LIST.CREATE n, hScores
TEXT.CLOSE r
TEXT.OPEN r, hData, rdir$+"hoverball.dat"
!if hData<1 then popup "failed open"
TEXT.READLN hData, in$
IF in$="quiet" THEN quiet=1

DO
 TEXT.READLN hData, in$
 IF in$="EOF" | in$="" THEN D_U.BREAK
 n$=WORD$(in$,2,"/")
 IF IS_NUMBER(WORD$(in$,1,"/")) & IS_NUMBER(WORD$(in$,3,"/"))
  score=VAL(WORD$(in$,1,"/"))
  t=VAL(WORD$(in$,3,"/"))
  BUNDLE.CREATE b
  BUNDLE.PUT b, "name",n$
  BUNDLE.PUT b, "score",score
  BUNDLE.PUT b, "date",t
  LIST.ADD hScores, b
 ENDIF
UNTIL in$="EOF"
list.size hScores, t
if t<10 then hiscore=10 else hiscore=score
GOSUB sort
TEXT.CLOSE hData
RETURN

!========
LoadWalls:
LIST.GET lArenas, arena, b
DEBUG.DUMP.BUNDLE b
BUNDLE.GET b, "Name", s$
popup using$(,"%s %.0f/%.0f",s$,arena,nArenas),,DISPLAY_H/2,2
GR.PAINT.COPY pWhite
GR.SET.STROKE 4

w=1
BUNDLE.GET b, "hWalls", in$
FOR i=1 TO 4
 t$=WORD$(in$,w++,",")
 IF IS_NUMBER(t$)=0
  hWalls[i,1]=0
 ELSE
  hWalls[i,1]=VAL(t$)
  hWalls[i,2]=VAL(WORD$(in$,w++,","))
  hWalls[i,3]=VAL(WORD$(in$,w++,","))
  GR.LINE hWalls[i,4], hWalls[i,2], hWalls[i,1], hWalls[i,3], hWalls[i,1]
 ENDIF
NEXT i
w=1
BUNDLE.GET b, "vWalls", in$
FOR i=1 TO 4
 t$=WORD$(in$,w++,",")
 IF IS_NUMBER(t$)=0
  vWalls[i,1]=0
 ELSE
  vWalls[i,1]=VAL(t$)
  vWalls[i,2]=VAL(WORD$(in$,w++,","))
  vWalls[i,3]=VAL(WORD$(in$,w++,","))
  GR.LINE hWalls[i,4],vWalls[i,1], vWalls[i,2], vWalls[i,1], vWalls[i,3]
 ENDIF
NEXT i

!gr.set.stroke 8
!gr.color 100,100,100,100,0
!gr.rect n,0,0,DISPLAY_W,DISPLAY_H
GR.RENDER
RETURN

PrepObjects:
GR.SET.STROKE 4
gr.color 255,255,0,0
gr.paint.get pRed
gr.color 255,0,255,0
gr.paint.get pGreen
gr.color 255,0,0,255
gr.paint.get pBlue
gr.color 255,255,255,255
gr.paint.get pWhite
gr.set.stroke 2
gr.text.size 24
gr.text.align 2
gr.color 255,0,0,0
gr.paint.get pBlack

!Bonus and Thrust Lines
gr.paint.copy pBlue
gr.line oBonus,10,10,10+cBonus,10
gr.paint.copy pRed
gr.set.stroke 10
gr.line oThrust,0,0,1,1

!Transparent Control Pad
GR.COLOR 50,0,200,0
GR.CIRCLE oPad,0,0,dMax
GR.HIDE oPad
return


!========
MakeBalls:
gr.paint.copy pBlue
m=1
GOSUB PutBall
GR.CIRCLE oBalls[1],BallsX[1], BallsY[1],16
gr.paint.copy pBlack
GR.TEXT.DRAW tBalls[1],BallsX[1],BallsY[1]+8,int$(life)

gr.paint.copy pGreen
FOR m=2 TO nBalls
 BallsNum[m]=m-1
 GOSUB PutBall
 GR.CIRCLE oBalls[m],BallsX[m], BallsY[m],16

 GR.PAINT.COPY pBlack
 GR.TEXT.DRAW tBalls[m],BallsX[m],BallsY[m]+6,INT$(BallsNum[m])
 GR.PAINT.COPY pRed
NEXT m
RETURN

PutBall:
BallsXV[m]=RND()*vMax*2 - vMax: BallsYV[m]=RND()*vMax*2 - vMax
DO
 hit=0
 BallsX[m]=20+RND()*(DISPLAY_W-40)
 BallsY[m]=20+RND()*(DISPLAY_H-40)
 GOSUB TestWalls

 IF ABS(BallsX[m]-BallsX[1])<50 & ABS(BallsY[m]-BallsY[1])<50 THEN hit=1
UNTIL hit=0 | m=1


RETURN

!========
Menu:
WHILE 1

 t$="Balls: "+INT$(cLives)+" Blue, "+INT$(nBalls-1)+" Red"
 ARRAY.LOAD m$[],"Play", "Bump Mode: "+BumpModes$[BumpMode], "Border Mode: "+BorderModes$[BorderMode]~
 t$, "Ballocity", "Change Arena", "Controls", "Exit"
 DIALOG.SELECT s, m$[], "Hoverball v.1"

 IF s=1
  GOSUB Play
 ELSEIF s=2
  DIALOG.SELECT t, BumpModes$[], "Bump Mode"
  IF t>0
   BumpMode=t
   IF BumpMode=1
    custom=BAND(custom,BNOT(1))
    opt[1]=0
    GOSUB MenuBounce
   ELSEIF BumpMode=2
    custom=BAND(custom,BNOT(1))
    ARRAY.LOAD m$[], "Low", "Normal", "High", "Excessive", "Custom"
    DIALOG.SELECT s, m$[], "Halt Delay"
    IF s=1 THEN HaltDelay=80: opt[1]=5
    IF s=2 THEN HaltDelay=160: opt[1]=10
    IF s=3 THEN HaltDelay=240: opt[1]=15
    IF s=4 THEN HaltDelay=320: opt[1]=20
    IF s=5
     INPUT "Halt Delay", t, 100, cancel
     IF t>0 THEN HaltDelay=t*1.6: custom=BOR(custom,1)
    ENDIF
   ENDIF
  ENDIF
 ELSEIF s=3
  DIALOG.SELECT t, BorderModes$[], "Border Mode"
  IF t>0
   bHurt=0
   BorderMode=t
   IF BorderMode=1
    bHurt=1
    GOSUB MenuBounce
    DIALOG.MESSAGE "Option","Border collision damages ball",t,"Yes","No"
    IF t=1 THEN bHurt=1: opt[2]=0
    IF t=2 THEN bHurt=0: opt[2]=-10
   ENDIF
   DIALOG.MESSAGE "Option","Wall collision damages ball",t,"Yes","No"
   IF t=1 THEN wHurt=1: opt[3]=0
   IF t=2 THEN wHurt=0: opt[3]=-10
  ENDIF
 ELSEIF s=4
  GOSUB MenuBalls
 ELSEIF s=5
  GOSUB MenuBallocity
 ELSEIF s=6
  GOSUB SelectArena
 ELSEIF s=7
  GOSUB MenuControl
 ELSEIF s=8
  W_R.BREAK
 ELSE
  GOSUB ShowHelp
 ENDIF
 ARRAY.SUM sMod, opt[]: sMod+=100
 s$="Score Modifier: "+INT$(sMod)+"%\nCustom flags: "+BIN$(custom)
REPEAT
RETURN

MenuBalls:
ARRAY.LOAD m$[], "one", "three", "six", "twelve", "Custom"
custom=BAND(custom,BNOT(2))
DIALOG.SELECT s, m$[], "Blue Balls (lives)"
IF s=1 THEN cLives=1: opt[4]=20
IF s=2 THEN cLives=3: opt[4]=0
IF s=3 THEN cLives=6: opt[4]=-20
IF s=4 THEN cLives=12: opt[4]=-40
IF s=5
 INPUT "Blue Balls (lives)", t, 3,cancel
 IF t>0 THEN cLives=t: BOR(custom,2)
ENDIF

ARRAY.LOAD m$[], "three", "six", "nine", "twelve", "Custom"
custom=BAND(custom,BNOT(4))
DIALOG.SELECT s, m$[], "Red Balls (enemies)"
IF s=1 THEN nBalls=4: opt[5]=-10
IF s=2 THEN nBalls=7: opt[5]=0
IF s=3 THEN nBalls=10: opt[5]=10
IF s=4 THEN nBalls=13: opt[5]=20
IF s=5
 INPUT "Red Balls (enemies)", t, 6,cancel
 IF t>0 THEN vMax=t: BOR(custom,4)
 IF nBalls>mBalls THEN nBalls=mBalls
ENDIF
RETURN

MenuBallocity:
ARRAY.LOAD m$[], "Low", "Normal", "High", "Excessive", "Custom"
custom=BAND(custom,BNOT(8))
DIALOG.SELECT s, m$[], "Red Ball Speed"
IF s=1 THEN sp=1: opt[6]=-10
IF s=2 THEN sp=1.5: opt[6]=0
IF s=3 THEN sp=2: opt[6]=10
IF s=4 THEN sp=3: opt[6]=20
IF s=5
 INPUT "Red Ball Speed", t, 100, cancel
 IF t>0 THEN sp=t/66: custom=BOR(custom, 8)
ENDIF
ARRAY.LOAD m$[], "Low", "Normal", "High", "Excessive", "Custom"
custom=BAND(custom,BNOT(16))
DIALOG.SELECT s, m$[], "Red Ball Acceleration"
ramping=100
IF s=1 THEN ramping=50: opt[7]=-5
IF s=2 THEN ramping=100: opt[7]=0
IF s=3 THEN ramping=150: opt[7]=5
IF s=4 THEN ramping=300: opt[7]=10
IF s=5
 INPUT "Red Ball Acceleration", t, 100, cancel
 IF t>0 THEN ramping=t: custom=BOR(custom, 16)
ENDIF
ramping=600/(ramping/100)
ARRAY.LOAD m$[], "Low", "Normal", "High", "Excessive", "Custom"
custom=BAND(custom,BNOT(32))
DIALOG.SELECT s, m$[], "Ball Bonus"
bonus=100
IF s=1 THEN bonus=75: opt[8]=15
IF s=2 THEN bonus=100: opt[8]=0
IF s=3 THEN bonus=125: opt[8]=-15
IF s=4 THEN bonus=150: opt[8]=-30
IF s=5
 INPUT "Ball Bonus", t, 100, cancel
 IF t>0 THEN bonus=t: custom=BOR(custom,32)
ENDIF
bonus=bonus*2
DIALOG.MESSAGE "Option","Bogus Bonus Boom",t,"Yes","No"
IF t=1 THEN bonusboom=1: opt[9]=20
IF t=2 THEN bonusboom=0: opt[9]=0
RETURN

MenuBounce:
ARRAY.LOAD m$[], "Low", "Normal", "High", "Excessive", "Custom"
custom=BAND(custom,BNOT(64))
DIALOG.SELECT s, m$[], "Bounce"
IF s=1 THEN Bounce=0.5: opt[10]=-5
IF s=2 THEN Bounce=1: opt[10]=0
IF s=3 THEN Bounce=1.5: opt[10]=5
IF s=4 THEN Bounce=2: opt[10]=10
IF s=5
 INPUT "Bounce Strength", t, 100, cancel
 IF t>0 THEN Bounce=t/100: custom=BOR(custom,64)
ENDIF
RETURN

MenuControl:
thrust=100
ARRAY.LOAD m$[], "Low", "Normal", "High", "Excessive", "Custom"
DIALOG.SELECT s, m$[], "Blue Ball Responsivity"
IF s=1 THEN dMax=80
IF s=2 THEN dMax=60
IF s=3 THEN dMax=30
IF s=4 THEN dMax=20
IF s=5
 INPUT "Blue Ball Responsivity", t, 100, cancel
 IF t>0 THEN dMax=t*.6
ENDIF
ARRAY.LOAD m$[], "Low", "Normal", "High", "Excessive", "Custom"
custom=BAND(custom,BNOT(128))
DIALOG.SELECT s, m$[], "Blue Ball Thrust"
IF s=1 THEN thrust=50: opt[11]=10
IF s=2 THEN thrust=100: opt[11]=0
IF s=3 THEN thrust=150: opt[11]=-10
IF s=4 THEN thrust=300: opt[11]=-20
IF s=5
 INPUT "Red Ball Acceleration", t, 100, cancel
 IF t>0 THEN thrust=t: custom=BOR(custom,128)
ENDIF
thrust=thrust*(60/dMax)
thrust=0.001*thrust/100
RETURN


!========
MoveBalls:
GR.TEXT.ALIGN 2
GR.TEXT.SIZE 16
GR.TEXT.BOLD 1
GR.COLOR 255,0,0,240,2

BallsX[1]+=BallsXV[1]
BallsY[1]+=BallsYV[1]
GR.MODIFY oBalls[1], "x",BallsX[1], "y",BallsY[1]
GR.MODIFY tBalls[1], "x",BallsX[1], "y",BallsY[1]+8

FOR i=2 TO nBalls
 hit=0
 BallsX[i]+=BallsXV[i]
 BallsY[i]+=BallsYV[i]
 GR.MODIFY oBalls[i], "x",BallsX[i], "y",BallsY[i]
 GR.MODIFY tBalls[i], "x",BallsX[i], "y",BallsY[i]+8
 !IF GR_COLLISION(oBalls[1], oBalls[i]) then gosub BallHit
 if hypot(ballsx[i]-ballsx[1],ballsy[i]-ballsy[1]) < 24 then gosub BallHit
NEXT i
RETURN

ONBACKKEY:
life=-1
BACK.RESUME
RETURN


Play:
life=cLives: i=0: cBonus=bonus: score=0: balls=0
vMax=sp: tRamp=Ramping: halt=0

GR.CLS
GOSUB PrepObjects
GOSUB LoadWalls
GOSUB MakeBalls
BallsXV[1]=0: BallsYV[1]=0
GOSUB HandleWalls
GOSUB MoveBalls
GR.RENDER
PAUSE 500
tStart=TIME(): cframes=0

WHILE life>0
 GOSUB HandleInput
 GOSUB MoveBalls
 GOSUB HandleBorders
 GOSUB HandleWalls
 GOSUB Delay
 GR.RENDER
 cFrames++
 HitD--
 IF tRamp-- <1 THEN GOSUB Ramp
 IF cBonus-- <1 & bonusboom=1
  SOUNDPOOL.PLAY n, thwoop
  cBonus=bonus
  GR.MODIFY tBalls[1], "text",INT$(--life)
  GR.MODIFY oBonus, "paint", pGreen
 ENDIF
REPEAT
i=(TIME()-tStart)/1000
popup str$(i)
GOSUB ShowScore

RETURN

Ramp:
vMax*=1.2
FOR i=2 TO nBalls
 BallsXV[i]*=1.2
 BallsYV[i]*=1.2
NEXT i
tRamp=Ramping
!popup "Ramping: "+int$(ramping)+", "+str$(vMax)
RETURN

!=====
SaveData:
TEXT.OPEN w, hData, rdir$+"hoverball.dat"

IF quiet=1
 TEXT.WRITELN hData, "quiet"
ELSE
 TEXT.WRITELN hData, "show"
ENDIF

LIST.SIZE hScores, j
IF j>10 THEN j=10
FOR i=1 TO j
 LIST.GET hScores, i, b
 BUNDLE.GET b, "score",n
 s$=INT$(n)+"/"
 BUNDLE.GET b, "name", t$
 s$+=t$+"/"
 BUNDLE.GET b, "date", n
 s$+=INT$(n)
 TEXT.WRITELN hData, s$
NEXT i
TEXT.CLOSE hData
RETURN

!=====
SetScreen:
IF quiet=0 THEN GOSUB ShowHelp

DISPLAY_W=640
DISPLAY_H=400
tLast=TIME()
tDelay=30

GR.OPEN 0,0,0,0,0,0
GR.SCREEN ACTUAL_W, ACTUAL_H
SCALE_X=ACTUAL_W / DISPLAY_W
SCALE_Y=ACTUAL_H / DISPLAY_H
GR.SCALE SCALE_X, SCALE_Y
GOSUB SelectArena
RETURN

!======
SELECTArena:
gr.cls
gosub PrepObjects
s=0
IF arena>0
 LIST.CREATE s, l
 LIST.ADD l, "Any"
 FOR i=1 TO nArenas
  LIST.GET lArenas, i, b
  BUNDLE.GET b, "Name", s$
  LIST.ADD l, s$
 NEXT i
 DIALOG.SELECT s, l, "Select Map"
ENDIF
IF s<2 THEN s=rand(nArenas)+1:i=0
arena=s-1
GOSUB LoadWalls
GOSUB HandleWalls
GR.RENDER
PAUSE 500
RETURN

!======
SetVars:
SOUNDPOOL.OPEN 4
SOUNDPOOL.LOAD boing, rDir$+"boing.mp3"
SOUNDPOOL.LOAD bloop, rDir$+"bloop.wav"
SOUNDPOOL.LOAD thwoop, rDir$+"thwoop.wav"
SOUNDPOOL.LOAD crash, rDir$+"crash.wav"

BumpMode=1
ARRAY.LOAD BumpModes$[], "Bounce", "Halt"
BorderMode=1: bHurt=1: wHurt=1
ARRAY.LOAD BorderModes$[], "Bounce", "Phase", "Warp"
cLives=3: bonus=100*2: sMod=100
arena=0

nBalls=7: sp=1.5: ramping=600

Bounce=1
dMax=50: thrust=0.0015
RETURN

wait:
DO
 PAUSE 50
 GR.TOUCH touched, x,x
 INKEY$ s$
UNTIL touched=1 | s$<>"@"
RETURN

!======
ShowHelp:
s$="by Michael C Palmer\n\nYou control the Blue Ball with your finger\n"
s$+="Collect the #1 Red Ball to score\nThe other red balls are your enemy\n"
s$+="Bouncing off a wall hurts your ball\nGood Luck!"
DIALOG.MESSAGE "HoverBall v.1", s$,s,"Thanks","No More"
quiet=0
IF s=2 THEN quiet=1
RETURN

!======
ShowHiScores:
out$=""
today=TIME() - (24*60*60*1000)
GR.TEXT.SIZE 18
GR.SET.STROKE 0
gr.text.bold 0
GR.COLOR 255,100,100,100,2
GR.RECT n, 0,0,6400,400
GR.COLOR 255,0,0,0,2
GR.RECT n, 180,100,460,300
GR.COLOR ,255,255,255
GR.RECT n, 183,126,457,297
GR.COLOR 255,255,255,255,2
GR.TEXT.ALIGN 2
GR.TEXT.DRAW n, 320,120, "Hi Scores"
GR.TEXT.SIZE 12: GR.TEXT.BOLD 0
GR.COLOR 255,0,0,0,2
y=140

IF count>10 THEN count=10
FOR i=1 TO count
 LIST.GET hScores, i, b
 BUNDLE.GET b, "score", j
 BUNDLE.GET b, "name", s$
 BUNDLE.GET b, "date", t
 s$=LEFT$(s$,25)
 IF t>today
  s$+=USING$("", " (%tI:%<tM%<Tp)", INT(t))
 ELSE
  s$+=USING$(," (%tm/%<td)", INT(t))
 ENDIF
 GR.TEXT.ALIGN 1
 GR.TEXT.DRAW n, 188,y,s$
 GR.TEXT.ALIGN 3
 GR.TEXT.DRAW n, 452,y,INT$(j)

 Y+=16
NEXT i
GR.RENDER
GOSUB wait
GR.CLS
GOSUB HandleWalls
GR.RENDER
RETURN

!=====
ShowScore:
s$="Caught "+INT$(balls)+" balls in "+INT$(i)+" seconds\n"
out$=""

IF score>0
 out$="Regular Points:\t\t"+INT$(balls*100)+"\n"
 b=score-(balls*100)
 out$+="Bonus Points:\t\t\t"+INT$(b)+" ("  +INT$((b/score)*100)+"%)\n"
 IF custom=0
  out$+="Option Modifier:\t\t"+INT$(sMod)+"%\n"
  score*=sMod/100
  out$+="Total Score:\t\t\t"+INT$(score)+"\n"
 ELSE
  out$+="Score disqualified by custom options\n"
 ENDIF
ENDIF
!out$+="\nAverage FPS: "+INT$(cFrames/i)
DIALOG.MESSAGE s$, out$,s,"Okay"
IF score>hiscore THEN GOSUB addscore

RETURN
!=====
Sort:
hBun=1:t$="":hVal=0
LIST.SIZE hScores, count
FOR i = 1 TO count
 FOR j = i TO count
  LIST.GET hScores, j,b
  BUNDLE.GET b, "score", s
  IF s > hVal THEN hBun = j: hVal=s
 NEXT j

 LIST.GET hScores, hBun, g
 LIST.REMOVE hScores, hBun
 LIST.INSERT hScores, i, g 
 hVal=0
NEXT i
RETURN
