InitialObjectDescriptor {
 objectDescriptorID 1
 audioProfileLevelIndication 1
 visualProfileLevelIndication 1
 sceneProfileLevelIndication 1
 graphicsProfileLevelIndication 1
 ODProfileLevelIndication 1
 esDescr [
  ES_Descriptor {
   ES_ID 1
   decConfigDescr DecoderConfigDescriptor {
    streamType 3
    decSpecificInfo BIFSConfig {
     isCommandStream true
     pixelMetric true
     pixelWidth 400
     pixelHeight 400
    }
   }
  }
 ]
}

OrderedGroup {
 children [
  Background2D {
   backColor 1 1 1
  }
  WorldInfo {
   info [
    "This file demonstrates how to use ECMAScript to make games in BIFS." 
    "GPAC Regression Tests" "$Date: 2007-07-27 09:46:09 $ - $Revision: 1.2 $" "(C) 2002-2004 GPAC Team"
    ]
    title "Arrange"
  }
  Transform2D {
   translation 0 150
   children [
    DEF StartTS TouchSensor {}
    Shape {
     appearance Appearance {
      material Material2D {
       emissiveColor 0.5 0.5 0.5
       filled TRUE
      }
     }
     geometry Rectangle {
      size 100 30
     }
    }
    Shape {
     appearance Appearance {
      material Material2D {
       emissiveColor 1 0 0
       filled TRUE
      }
     }
     geometry Text {
      string ["Start Game"]
      fontStyle FontStyle {
       justify ["MIDDLE" "MIDDLE"]
       size 20
      }
     }
    }
   ]
  }
  DEF TimeSensorTick TimeSensor {
   enabled FALSE
   loop TRUE
  }
  Transform2D {
   translation 0 75
   children [
    Shape {
     appearance DEF TA Appearance {
      material Material2D {
       emissiveColor 1 0 0
       filled TRUE
      }
     }
     geometry DEF TextField Text {
      fontStyle DEF FS FontStyle {
       justify ["MIDDLE" "MIDDLE"]
       size 22
      }
     }
    }
   ]
  }
  Switch {
   choice [
    DEF Hole Shape {
     appearance Appearance {
      material Material2D {
       emissiveColor 1 1 1
       filled TRUE
      }
     }
     geometry DEF R Rectangle {
      size 50 50
     }
    }
   ]
  }
  Transform2D {
   translation 0 -50
   children [
    DEF Table Form {
     size 150 150
     groups [1 -1 2 -1 3 -1 4 -1 5 -1 6 -1 7 -1 8 -1 9 -1 1 2 3 -1 4 5 6 -1 7 8 9 -1]
     constraints ["SHin" "SHin" "SHin" "SVin"]
     groupsIndex [1 2 3 -1 4 5 6 -1 7 8 9 -1 10 11 12 -1]
     children [
      DEF a_1_1 Switch {
       whichChoice 0
       choice [
        Transform2D {
         children [
          DEF TS_1_1 TouchSensor {}
          Shape {
           appearance DEF RA Appearance {
            material Material2D {
             emissiveColor 0 0 0
             filled TRUE
            }
           }
           geometry USE R
          }
          Shape {
           appearance USE TA
           geometry Text {
            string ["1"]
            fontStyle USE FS
           }
          }
         ]
        }
        USE Hole
       ]
      }
      DEF a_1_2 Switch {
       whichChoice 0
       choice [
        Transform2D {
         children [
          DEF TS_1_2 TouchSensor {}
          Shape {
           appearance USE RA
           geometry USE R
          }
          Shape {
           appearance USE TA
           geometry Text {
            string ["2"]
            fontStyle USE FS
           }
          }
         ]
        }
        USE Hole
       ]
      }
      DEF a_1_3 Switch {
       whichChoice 0
       choice [
        Transform2D {
         children [
          DEF TS_1_3 TouchSensor {}
          Shape {
           appearance USE RA
           geometry USE R
          }
          Shape {
           appearance USE TA
           geometry Text {
            string ["3"]
            fontStyle USE FS
           }
          }
         ]
        }
        USE Hole
       ]
      }
      DEF a_2_1 Switch {
       whichChoice 0
       choice [
        Transform2D {
         children [
          DEF TS_2_1 TouchSensor {}
          Shape {
           appearance USE RA
           geometry USE R
          }
          Shape {
           appearance USE TA
           geometry Text {
            string ["4"]
            fontStyle USE FS
           }
          }
         ]
        }
        USE Hole
       ]
      }
      DEF a_2_2 Switch {
       whichChoice 0
       choice [
        Transform2D {
         children [
          DEF TS_2_2 TouchSensor {}
          Shape {
           appearance USE RA
           geometry USE R
          }
          Shape {
           appearance USE TA
           geometry Text {
            string ["5"]
            fontStyle USE FS
           }
          }
         ]
        }
        USE Hole
       ]
      }
      DEF a_2_3 Switch {
       whichChoice 0
       choice [
        Transform2D {
         children [
          DEF TS_2_3 TouchSensor {}
          Shape {
           appearance USE RA
           geometry USE R
          }
          Shape {
           appearance USE TA
           geometry Text {
            string ["6"]
            fontStyle USE FS
           }
          }
         ]
        }
        USE Hole
       ]
      }
      DEF a_3_1 Switch {
       whichChoice 0
       choice [
        Transform2D {
         children [
          DEF TS_3_1 TouchSensor {}
          Shape {
           appearance USE RA
           geometry USE R
          }
          Shape {
           appearance USE TA
           geometry Text {
            string ["7"]
            fontStyle USE FS
           }
          }
         ]
        }
        USE Hole
       ]
      }
      DEF a_3_2 Switch {
       whichChoice 0
       choice [
        Transform2D {
         children [
          DEF TS_3_2 TouchSensor {}
          Shape {
           appearance USE RA
           geometry USE R
          }
          Shape {
           appearance USE TA
           geometry Text {
            string ["8"]
            fontStyle USE FS
           }
          }
         ]
        }
        USE Hole
       ]
      }
      DEF a_3_3 Switch {
       whichChoice 1
       choice [
        Transform2D {
         children [
          DEF TS_3_3 TouchSensor {}
          Shape {
           appearance USE RA
           geometry USE R
          }
          Shape {
           appearance USE TA
           geometry Text {
            string ["9"]
            fontStyle USE FS
           }
          }
         ]
        }
        USE Hole
       ]
      }
     ]
    }
   ]
  }
  DEF SC Script {
   eventIn SFVec2f move
   eventIn SFBool startGame
   eventIn SFTime tickTime
   field SFNode txtField USE TextField
   field SFNode table USE Table
   field SFInt32 gsize 3
   field SFInt32 ghrow 2
   field SFInt32 ghcol 2
   field SFInt32 gtime 0
   field SFInt32 gmoves 0
   field SFInt32 gshuffling 0
   field SFInt32 size 3
   field SFBool isStarted FALSE
   eventOut SFBool hasWin
   url ["javascript:
   	function r(low,hi) {return Math.floor((hi-low)*Math.random()+low); }
   	function r1(hi) {return Math.floor((hi-1)*Math.random()+1); }
   	function r0(hi) {return Math.floor((hi)*Math.random()); }
   	function startGame(value) {if (value) {shuffle();gtime = 0;gmoves = 0;isStarted = !isStarted; hasWin = false;}}
   	function stopGame(){isStarted = !isStarted;}
   	function tickTime(value){showStatus();gtime++;}
   	function checkWin(){if (!isStarted) return; if (!isHole(gsize-1,gsize-1)) return;for (i=0;i<gsize;i++)for (j=0;j<gsize;j++) {if (!(i==gsize-1 && j==gsize-1)) {if (getValue(i,j)!=(i*gsize+j+1).toString()) return;}}hasWin = true;stopGame();txtField.string[0] = '!! CONGRATS !!';txtField.string[1] = 'You have done it in ' + gtime + ' secs ' + 'with ' + gmoves + ' moves!';txtField.string[2] = 'Your speed is ' + Math.round(1000*gmoves/gtime)/1000 + ' moves/sec';}
   	function showStatus() {if (hasWin) return;txtField.string[0] = 'Time: ' + gtime + ' secs   Moves: ' + gmoves;txtField.string[1] = '';txtField.string[2] = '';}
   	function getCell(row, col) {return table.children[row * gsize + col];}
   	function setValue(row,col,val) {v = getCell(row, col);v.whichChoice = 0;v.choice[0].children[2].geometry.string[0] = val;}
   	function getValue(row,col) {v = getCell(row, col);return v.choice[0].children[2].geometry.string[0];}
   	function setHole(row,col) { v = getCell(row, col);v.whichChoice = 1;ghrow = row;ghcol = col;}
   	function getRow(obj){a = obj.id.split('_');return a[1];}
   	function getCol(obj) {a = obj.id.split('_');return a[2];}
   	function isHole(row, col) {return (row==ghrow && col==ghcol) ? true : false;}
   	function getHoleInRow(row) {return (row==ghrow) ? ghcol : -1;}
   	function getHoleInCol(col) {return (col==ghcol) ? ghrow : -1;}
   	function shiftHoleRow(src,dest,row) {src = parseInt(src);dest = parseInt(dest);if (src < dest) {for (i=src;i<dest;i++) {setValue(row,i,getValue(row,i+1));setHole(row,i+1);}}if (dest < src) {for (i=src;i>dest;i--) {setValue(row,i,getValue(row,i-1));setHole(row,i-1);}}}
   	function shiftHoleCol(src,dest,col) {src = parseInt(src);dest = parseInt(dest);if (src < dest) {for (i=src;i<dest;i++) {setValue(i,col,getValue(i+1,col));setHole(i+1,col);}}if (dest < src){for (i=src;i>dest;i--){				    setValue(i,col,getValue(i-1,col));setHole(i-1,col);}}}
   	function moveRowCol(r,c) {				  if (!isStarted && !gshuffling) { return; }if (isHole(r,c)) {return;				  }hc = getHoleInRow(r);if (hc != -1) {shiftHoleRow(hc,c,r);gmoves++;checkWin();return;} hr = getHoleInCol(c);if (hr != -1){shiftHoleCol(hr,r,c);gmoves++;checkWin();return;} }
   	function move(value) {moveRowCol(value.x-1,value.y-1);}
   	function shuffle() {gshuffling =  true;for (i=0;i<gsize;i++) {for (j=0;j<gsize+10;j++) {  if (j==0) {t = r0(gsize);while (t == ghrow) t = r0(gsize); moveRowCol(t, ghcol);} else {t = r0(gsize);while (t == ghcol) t = r0(gsize); moveRowCol(ghrow, t);}}}gshuffling = false;}"   ]
  }
  DEF V_1_1 Valuator {}
  DEF C_1_1 Conditional {
   buffer {
    REPLACE V_1_1.inSFVec2f BY 1 1
   }
  }
  DEF V_1_2 Valuator {}
  DEF C_1_2 Conditional {
   buffer {
    REPLACE V_1_2.inSFVec2f BY 1 2
   }
  }
  DEF V_1_3 Valuator {}
  DEF C_1_3 Conditional {
   buffer {
    REPLACE V_1_3.inSFVec2f BY 1 3
   }
  }
  DEF V_2_1 Valuator {}
  DEF C_2_1 Conditional {
   buffer {
    REPLACE V_2_1.inSFVec2f BY 2 1
   }
  }
  DEF V_2_2 Valuator {}
  DEF C_2_2 Conditional {
   buffer {
    REPLACE V_2_2.inSFVec2f BY 2 2
   }
  }
  DEF V_2_3 Valuator {}
  DEF C_2_3 Conditional {
   buffer {
    REPLACE V_2_3.inSFVec2f BY 2 3
   }
  }
  DEF V_3_1 Valuator {}
  DEF C_3_1 Conditional {
   buffer {
    REPLACE V_3_1.inSFVec2f BY 3 1
   }
  }
  DEF V_3_2 Valuator {}
  DEF C_3_2 Conditional {
   buffer {
    REPLACE V_3_2.inSFVec2f BY 3 2
   }
  }
  DEF V_3_3 Valuator {}
  DEF C_3_3 Conditional {
   buffer {
    REPLACE V_3_3.inSFVec2f BY 3 3
   }
  }
  DEF WinConditional Conditional {
   buffer {
    REPLACE TimeSensorTick.enabled BY FALSE
#    REPLACE TimeSensorTick.stopTime BY 0
   }
  }
  DEF StartConditional Conditional {
   buffer {
    REPLACE TimeSensorTick.enabled BY TRUE
#    REPLACE TimeSensorTick.startTime BY 0
   }
  }
 ]
}

ROUTE StartTS.isActive TO StartConditional.activate
ROUTE StartTS.isActive TO SC.startGame
ROUTE TimeSensorTick.cycleTime TO SC.tickTime
ROUTE SC.hasWin TO WinConditional.activate
ROUTE TS_1_1.isActive TO C_1_1.activate
ROUTE TS_1_2.isActive TO C_1_2.activate
ROUTE TS_1_3.isActive TO C_1_3.activate
ROUTE TS_2_1.isActive TO C_2_1.activate
ROUTE TS_2_2.isActive TO C_2_2.activate
ROUTE TS_2_3.isActive TO C_2_3.activate
ROUTE TS_3_1.isActive TO C_3_1.activate
ROUTE TS_3_2.isActive TO C_3_2.activate
ROUTE TS_3_3.isActive TO C_3_3.activate
ROUTE V_1_1.outSFVec2f TO SC.move
ROUTE V_1_2.outSFVec2f TO SC.move
ROUTE V_1_3.outSFVec2f TO SC.move
ROUTE V_2_1.outSFVec2f TO SC.move
ROUTE V_2_2.outSFVec2f TO SC.move
ROUTE V_2_3.outSFVec2f TO SC.move
ROUTE V_3_1.outSFVec2f TO SC.move
ROUTE V_3_2.outSFVec2f TO SC.move
ROUTE V_3_3.outSFVec2f TO SC.move

