  #compact
  % filename "t.exe"
 'uses RTL32
  % Title    "Bubbles:    (left-click & right-click)"
  % Animated
  % ScaleUp
 '% PlaceCentral
 '% AnchorCentral
  uses ConsoleG
  %side 8

  indexbase 1

  type celltype
  int  a,n
  int  x,y,z
  float px,py,pz
  float tx,ty,tz
  end type

  sys sseed=GetTickCount
  celltype cell[side*side*side]
  sys      vsphere

  sub initcells()
  ===============  
  int r,x,y,z
  'seed=0x2468abcd
  seed=sseed
  celltype *c
  @c=@cell
  for z=1 to side
    for y=1 to side
      for x= 1 to side
        r=irnd(1,4)
        c.x=x
        c.y=y
        c.z=z
        c.a=r
        c.n=z*64+y*side+x-side
        c.px= rnd()*32
        c.py= rnd()*32
        c.pz=-arnd()*100+10
        @c+=sizeof cell
      next
    next
  next
  end sub

  initcells()

  function GameLogic(sys i,optional a) as sys
  ===========================================
  '
  if not i then exit function
  cell[i].a=0
  return 1
  end function


  sub render(sys i)
  =================
  sys a
  celltype c at (@cell[i])
  a=c.a
  if a then
    glpushmatrix
    move c.px+c.tx, c.py+c.ty, c.pz+c.tz
    if pick then
      Picklabel i
      go vsphere 'crude bounding sphere
    else
      select a
      case 1 : Material RedShiny
      case 2 : Material BlueShiny
      case 3 : Material WhiteShiny
      case 4 : Material Gold
      end select
      'REDUCE TRANSITION DISTANCE
      'if c.tx then c.tx*=.75
      'if c.ty then c.ty*=.75
      'if c.tz then c.tz*=.75
      go sphere
    end if
    glpopmatrix
   end if
  end sub

  sub main()
  ==========
  if not vsphere then
    'CRUDE SPHERE
    vsphere   = CompileList : Spheric 1,1,45 : glEndList
  end if
  '
  static float az
  static sys   pki,count,pix[4]
  glClearColor 0,0.10,0,0
 'Fog          0,0.10,0,0.05 'FOR SENSE OF DISTANCE
  picklabel 0
  '
  pushstate
  shading 'opposite of 'flat'
  '
  'DISPLAY TOP SECTION
  pushstate
    move 2
    color .90,.90,.90,.99
    scale 2 : print3d "Popped  id: " pki "  count: " count
  popstate
  '
  move 16,-16,-4
  rotatez az : az+=.03125
  scale 2
  '
  'DISPLAY
  sys a,i,x,y
  static sys en=side*side*side
  if pick then
    flat
  else
    shading
  end if
  for i=1 to en
    render i
  next
  '
  popstate 'whole
  '
  '
  if picked then
    GameLogic(picked)
    pki=cell[picked].n
    count++
    picked=0
  end if
  '
  end sub

  EndScript
