const CLOCKTICK = 100; // speed of run - move things every n milliseconds
const MAXSTEPS = 1000; // length of a run before final score
//---- global constants: -------------------------------------------------------
const gridsize = 20 ; // number of squares along side of world
const NOBOXES = Math.trunc ( (gridsize * gridsize) / 800);
// density of maze - number of internal boxes
// (bug) use trunc or can get a non-integer
const squaresize = 30; // size of square in pixels
const MAXPOS = gridsize * squaresize; // length of one side in pixels
const SKYCOLOR = 0xf4a460; // a number, not a string
const BLANKCOLOR = 0x00ff00 ;
const grey = 0x808080;
const red = SKYCOLOR // make objects this color until texture arrives (from asynchronous file read)
const show3d = true; // Switch between 3d and 2d view (both using Three.js)
const startRadiusConst = MAXPOS * 0.8 ; // distance from centre to start the camera at
const skyboxConst = MAXPOS * 3 ; // where to put skybox
const maxRadiusConst = MAXPOS * 10 ; // maximum distance from camera we will render things
//--- Mind can pick one of these actions -----------------
const ACTION_LEFT = 0;
const ACTION_RIGHT = 1;
const ACTION_UP = 2;
const ACTION_DOWN = 3;
const ACTION_STAYSTILL = 4;
// in initial view, (smaller-larger) on i axis is aligned with (left-right)
// in initial view, (smaller-larger) on j axis is aligned with (away from you - towards you)
// contents of a grid square
const GRID_BLANK = 0;
const GRID_WALL = 1;
const GRID_MAZE = 2;
const GRID_BOTTOM = 3;
const GRID_BOTTOM2 = 4;
const GRID_STONE = 5
// --- some useful random functions -------------------------------------------
function randomfloatAtoB ( A, B )
{
return ( A + ( Math.random() * (B-A) ) );
}
function randomintAtoB ( A, B )
{
return ( Math.round ( randomfloatAtoB ( A, B ) ) );
}
function randomBoolean()
{
if ( Math.random() < 0.5 ) { return false; }
else { return true; }
}
//---- start of World class -------------------------------------------------------
function World() {
// most of World can be private
// regular "var" syntax means private variables:
var BOXHEIGHT; // 3d or 2d box height
var GRID = new Array(gridsize);
var GRID2 = new Array(gridsize);
var GRID3 = new Array(gridsize);// can query GRID about whether squares are occupied, will in fact be initialised as a 2D array
var WALLS = new Array ( 4 * gridsize );
var WALLS2 = new Array ( 4 * gridsize );// need to keep handles to wall and maze objects so can find them later to paint them
var MAZE = new Array ( NOBOXES );
var STONE = new Array ( NOBOXES );
var BOTTOM = new Array (gridsize )
var BOTTOM2 = new Array (gridsize )
var theagent, theenemy,theSheep, bullet;
// enemy and agent position on squares
var ei, ej, ai, aj, si, sj,bi,bj;
var badsteps;
var goodsteps;
var step;
var self = this; // needed for private fn to call public fn - see below
// regular "function" syntax means private functions:
function initGrid()
{
for (var i = 0; i < gridsize ; i++)
{
GRID[i] = new Array(gridsize); // each element is an array
for (var j = 0; j < gridsize ; j++)
{
GRID[i][j] = GRID_BLANK ;
}
}
}
function initGrid2()
{
for (var i = 0; i < gridsize ; i++)
{
GRID2[i] = new Array(gridsize); // each element is an array
for (var j = 0; j < gridsize ; j++)
{
GRID2[i][j] = GRID_BLANK ;
}
}
}
function initGrid3()
{
for (var i = 0; i < gridsize ; i++)
{
GRID3[i] = new Array(gridsize); // each element is an array
for (var j = 0; j < gridsize ; j++)
{
GRID3[i][j] = GRID_BLANK ;
}
}
}
function occupied ( i, j ) // is this square occupied
{
if ( ( ei == i +1 ) && ( ej == j+1 ) ) return true; // variable objects
if ( ( ai == i -1 ) && ( aj == j+1 ) ) return true;
if ( ( si == i +1 ) && ( sj == j-1 ) ) return true;
if ( GRID[i][j] == GRID_WALL) return true;
if ( GRID[i][j] == GRID_MAZE) return true;// fixed objects
if ( GRID[i][j] == GRID_STONE) return true;
return false;
}
// logically, coordinates are: y=0, x and z all positive (no negative)
// logically my dimensions are all positive 0 to MAXPOS
// to centre everything on origin, subtract (MAXPOS/2) from all dimensions
function translate ( x )
{
return ( x - ( MAXPOS/2 ) );
}
//--- skybox ----------------------------------------------------------------------------------------------
function initSkybox()
{
// x,y,z positive and negative faces have to be in certain order in the array
// mountain skybox, credit:
// http://stemkoski.github.io/Three.js/Skybox.html
// var materialArray = [
//( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/adrian/trees.jpg" ), side: THREE.BackSide } ) ),
//( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture("/uploads/adrian/trees.jpg" ), side: THREE.BackSide } ) ),
//( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/adrian/sky.jpeg" ), side: THREE.BackSide } ) ),
//( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/adrian/grass.jpg" ), side: THREE.BackSide } ) ),
//( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/adrian/trees.jpg" ), side: THREE.BackSide } ) ),
//( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/adrian/trees.jpg" ), side: THREE.BackSide } ) ),
// ];
var materialArray = [
( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/seanhutchinson/skyrender0001.bmp" ), side: THREE.BackSide } ) ),
( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/seanhutchinson/skyrender0004.bmp" ), side: THREE.BackSide } ) ),
( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/seanhutchinson/skyrender0003.bmp" ), side: THREE.BackSide } ) ),
( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/seanhutchinson/skyrender0006.bmp" ), side: THREE.BackSide } ) ),
( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/seanhutchinson/skyrender0005.bmp" ), side: THREE.BackSide } ) ),
( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture("/uploads/seanhutchinson/skyrender0002.bmp"), side: THREE.BackSide } ) ),
];
var skyGeometry = new THREE.CubeGeometry ( skyboxConst, skyboxConst, skyboxConst );
var skyMaterial = new THREE.MeshFaceMaterial ( materialArray );
var theskybox = new THREE.Mesh ( skyGeometry, skyMaterial );
threeworld.scene.add( theskybox ); // We are inside a giant Cube
}
// This does the file read the old way using loadTexture.
// (todo) Change to asynchronous TextureLoader. A bit complex:
// Make blank skybox. Start 6 asynch file loads to call 6 return functions.
// Each return function checks if all 6 loaded yet. Once all 6 loaded, paint the skybox.
function loadtextures2()
{
var loader8 = new THREE.TextureLoader();
loader8.load ( '/uploads/adrian/silver.png', function ( thetexture ) {
thetexture.minFilter = THREE.LinearFilter;
bullet.material = new THREE.MeshBasicMaterial( { map: thetexture } );
} );
}
function loadTextures()
{
var manager = new THREE.LoadingManager();
var loader = new THREE.OBJLoader( manager );
loader.load( "/uploads/starter/skelet.obj", buildenemy );
loader.load( "/uploads/starter/male02.obj", buildsheep );
var loader1 = new THREE.TextureLoader();
loader1.load ( '/uploads/adrian/blue.jpg', function ( thetexture ) {
thetexture.minFilter = THREE.LinearFilter;
paintWalls ( new THREE.MeshBasicMaterial( { map: thetexture } ) );
} );
var loader7 = new THREE.TextureLoader();
loader7 .load ( '/uploads/adrian/blue.jpg', function ( thetexture ) {
thetexture.minFilter = THREE.LinearFilter;
paintWalls2 ( new THREE.MeshBasicMaterial( { map: thetexture } ) );
} );
var loader6 = new THREE.TextureLoader();
loader6.load ( '/uploads/adrian/water.jpg', function ( thetexture ) {
thetexture.minFilter = THREE.LinearFilter;
paintBottom2 ( new THREE.MeshBasicMaterial( { map: thetexture } ) );
} );
var loader2 = new THREE.TextureLoader();
loader2.load ( '/uploads/adrian/brick.jpg', function ( thetexture ) {
thetexture.minFilter = THREE.LinearFilter;
paintMaze ( new THREE.MeshBasicMaterial( { map: thetexture } ) );
} );
var loader3 = new THREE.TextureLoader();
loader3.load ( '/uploads/adrian/cat.jpg', function ( thetexture ) {
thetexture.minFilter = THREE.LinearFilter;
theagent.material = new THREE.MeshBasicMaterial( { map: thetexture } );
} );
var loader4 = new THREE.TextureLoader();
loader4.load ( '/uploads/adrian/mouce.png', function ( thetexture ) {
thetexture.minFilter = THREE.LinearFilter;
theenemy.material = new THREE.MeshBasicMaterial( { map: thetexture } );
} );
var loader5 = new THREE.TextureLoader();
loader5.load ( '/uploads/adrian/bm.png', function ( thetexture ) {
thetexture.minFilter = THREE.LinearFilter;
theSheep.material = new THREE.MeshBasicMaterial( { map: thetexture } );
} );
}
function buildenemy ( object )
{
object.scale.multiplyScalar ( .75 ); // make 3d object n times bigger
object.traverse( paintEnemy );
theenemy = object;
threeworld.scene.add( theenemy );
}
function buildsheep ( object )
{
object.scale.multiplyScalar ( .3 ); // make 3d object n times bigger
object.traverse( paintSheep );
theSheep = object;
threeworld.scene.add( theSheep );
}
function paintEnemy ( child )
{
if ( child instanceof THREE.Mesh )
{
child.material.map = THREE.ImageUtils.loadTexture( "/uploads/starter/ghost.3.png" );
}
}
function paintSheep ( child )
{
if ( child instanceof THREE.Mesh )
{
child.material.map = THREE.ImageUtils.loadTexture( "/uploads/starter/ghost.3.png" );
}
}
function drawEnemy() // given ei, ej, draw it
{
if ( theenemy )
{
var x = translate ( ei * squaresize );
var z = translate ( ej * squaresize );
var y = ( 11);
theenemy.position.x = x;
theenemy.position.y = y;
theenemy.position.z = z;
threeworld.lookat.copy ( theenemy.position ); // if camera moving, look back at where the enemy is
threeworld.lookat.y = ( 0); // point camera higher up
}
}
function drawSheep() // given ei, ej, draw it
{
if ( theSheep )
{
var x = translate ( si * squaresize );
var z = translate ( sj * squaresize );
var y = (-100);
theenemy.position.x = x;
theenemy.position.y = y;
theenemy.position.z = z;
threeworld.lookat.copy ( theSheep.position ); // if camera moving, look back at where the enemy is
threeworld.lookat.y = ( 0 ); // point camera higher up
}
}
// --- add fixed objects ----------------------------------------
function initLogicalWalls() // set up logical walls in data structure, whether doing graphical run or not
{
for (var i = 0; i < gridsize ; i++)
for (var j = 0; j < gridsize ; j++)
if ( ( i===0 ) || ( i==gridsize-1 ) || ( j===0 ) || ( j==gridsize-1 ) )
{
GRID[i][j] = GRID_WALL ;
}
}
function initLogicalWalls2() // set up logical walls in data structure, whether doing graphical run or not
{
for (var i = 0; i < gridsize ; i++)
for (var j = 0; j < gridsize ; j++)
if ( ( i===0 ) || ( i==gridsize-1 ) || ( j===0 ) || ( j==gridsize-1 ) )
{
GRID3[i][j] = GRID_WALL ;
}
}
function initLogicalBottom() // set up logical walls in data structure, whether doing graphical run or not
{
for (var i = 0; i < gridsize ; i++)
for (var j = 14; j < gridsize ; j++)
GRID2[i][j] = GRID_BOTTOM;
}
function initLogicalBottom2() // set up logical walls in data structure, whether doing graphical run or not
{
for (var i = 0; i < gridsize ; i++)
for (var j = 0; j < 14 ; j++)
GRID2[i][j] = GRID_BOTTOM2;
}
function initThreeWalls() // graphical run only, set up blank boxes, painted later
{
var t = 0;
for (var i = 0; i < gridsize ; i++)
for (var j = 0; j < gridsize ; j++)
if ( GRID[i][j] == GRID_WALL )
{
var shape = new THREE.BoxGeometry( squaresize, BOXHEIGHT, squaresize );
var thecube = new THREE.Mesh( shape );
thecube.material.color.setHex( BLANKCOLOR );
thecube.position.x = translate ( i * squaresize ); // translate my simple (i,j) block-numbering coordinates to three.js (x,y,z) coordinates
thecube.position.z = translate ( j * squaresize );
thecube.position.y = 30;
threeworld.scene.add(thecube);
WALLS[t] = thecube; // save it for later
t++;
}
}
function initThreeWalls2() // graphical run only, set up blank boxes, painted later
{
var t = 0;
for (var i = 0; i < gridsize ; i++)
for (var j = 0; j < gridsize ; j++)
if ( GRID3[i][j] == GRID_WALL )
{
var shape = new THREE.BoxGeometry( squaresize, BOXHEIGHT, squaresize );
var thecube = new THREE.Mesh( shape );
thecube.material.color.setHex( BLANKCOLOR );
thecube.position.x = translate ( i * squaresize ); // translate my simple (i,j) block-numbering coordinates to three.js (x,y,z) coordinates
thecube.position.z = translate ( j * squaresize );
thecube.position.y = -150;
threeworld.scene.add(thecube);
WALLS2[t] = thecube; // save it for later
t++;
}
}
function initThreeBottom() // graphical run only, set up blank boxes, painted later
{
var t = 0;
for (var i = 0; i < gridsize ; i++)
for (var j = 14; j < gridsize ; j++)
if ( GRID2[i][j] == GRID_BOTTOM )
{
var shape = new THREE.BoxGeometry( squaresize, BOXHEIGHT, squaresize );
var thecube = new THREE.Mesh( shape );
thecube.material.color.setHex( red );
thecube.position.x = translate ( i * squaresize ); // translate my simple (i,j) block-numbering coordinates to three.js (x,y,z) coordinates
thecube.position.z = translate ( j * squaresize );
thecube.position.y = 0;
threeworld.scene.add(thecube);
BOTTOM[t] = thecube; // save it for later
t++;
}
}
function initThreeBottom2() // graphical run only, set up blank boxes, painted later
{
var a = 0;
for (var i = 0; i < gridsize ; i++)
for (var j = 0; j < 14 ; j++)
if ( GRID2[i][j] == GRID_BOTTOM2 )
{
var shape = new THREE.BoxGeometry( squaresize, BOXHEIGHT, squaresize );
var thecube = new THREE.Mesh( shape );
thecube.material.color.setHex( BLANKCOLOR );
thecube.position.x = translate ( i * squaresize ); // translate my simple (i,j) block-numbering coordinates to three.js (x,y,z) coordinates
thecube.position.z = translate ( j * squaresize );
thecube.position.y = 0;
threeworld.scene.add(thecube);
BOTTOM2[a] = thecube; // save it for later
a++;
}
}
function paintWalls ( material )
{
for ( var i = 0; i < WALLS.length; i++ )
{
if ( WALLS[i] ) WALLS[i].material = material;
}
}
function paintWalls2 ( material )
{
for ( var i = 0; i < WALLS.length; i++ )
{
if ( WALLS2[i] ) WALLS2[i].material = material;
}
}
function paintBottom2 ( material )
{
for ( var i = 0; i < BOTTOM2.length; i++ )
{
if ( BOTTOM2[i] ) BOTTOM2[i].material = material;
}
}
function initLogicalMaze()
{
GRID[2][4] = GRID_MAZE ;
GRID[4][4] = GRID_MAZE ;
GRID[8][6] = GRID_MAZE ;
GRID[12][3] = GRID_MAZE ;
GRID[18][6] = GRID_MAZE ;
GRID[10][2] = GRID_MAZE ;
GRID[4][10] = GRID_MAZE ;
GRID[16][10] = GRID_MAZE ;
for ( var c=1 ; c <= NOBOXES ; c++ )
{
var i = randomintAtoB(1,gridsize-2); // inner squares are 1 to gridsize-2
var j = randomintAtoB(1,gridsize-2);
GRID[i][j] = GRID_MAZE ;
}
}
function initLogicalStone()
{
GRID[1][14] = GRID_STONE ;
GRID[2][14] = GRID_STONE ;
GRID[3][14] = GRID_STONE ;
GRID[4][14] = GRID_STONE ;
GRID[5][14] = GRID_STONE ;
GRID[6][14] = GRID_STONE;
GRID[7][14] = GRID_STONE ;
GRID[4][14] = GRID_STONE ;
GRID[5][14] = GRID_STONE ;
GRID[6][14] = GRID_STONE ;
GRID[7][14] = GRID_STONE ;
GRID[8][14] = GRID_STONE ;
GRID[9][14] = GRID_STONE ;
GRID[10][14] = GRID_STONE ;
GRID[11][14] = GRID_STONE ;
GRID[12][14] = GRID_STONE ;
GRID[13][14] = GRID_STONE ;
GRID[14][14] = GRID_STONE ;
GRID[15][14] = GRID_STONE;
GRID[16][14] = GRID_STONE ;
GRID[17][14] = GRID_STONE ;
GRID[18][14] = GRID_STONE ;
}
function initThreeMaze()
{
var t = 0;
for (var i = 0; i < gridsize ; i++)
for (var j = 0; j < 13 ; j++)
if ( GRID[i][j] == GRID_MAZE )
{
var shape = new THREE.BoxGeometry( squaresize, BOXHEIGHT, squaresize );
var thecube = new THREE.Mesh( shape );
thecube.material.color.setHex( BLANKCOLOR );
thecube.position.x = translate ( i * squaresize );
thecube.position.z = translate ( j * squaresize );
thecube.position.y = 30;
threeworld.scene.add(thecube);
MAZE[t] = thecube; // save it for later
t++;
}
}
function initThreeStone()
{
var t = 0;
for (var i = 0; i < gridsize ; i++)
for (var j = 13; j < gridsize ; j++)
if ( GRID[i][j] == GRID_STONE )
{
var shape = new THREE.BoxGeometry( 30, 10, 30 );
var thecube = new THREE.Mesh( shape );
thecube.material.color.setHex( grey );
thecube.position.x = translate ( i * squaresize );
thecube.position.z = translate ( j * squaresize );
thecube.position.y = 15;
threeworld.scene.add(thecube);
STONE[t] = thecube; // save it for later
t++;
}
}
function paintMaze ( material )
{
for ( var i = 0; i < MAZE.length; i++ )
{
if ( MAZE[i] ) MAZE[i].material = material;
}
}
// --- enemy functions -----------------------------------
function drawbullet() // given ei, ej, draw it
{
var x = translate ( bi * squaresize );
var z = translate ( bj * squaresize );
var y = 30;
bullet.position.x = x;
bullet.position.y = y;
bullet.position.z = z;
threeworld.scene.add(bullet);
threeworld.lookat.copy ( bullet.position ); // if camera moving, look back at where the enemy is
}
function drawSheep() // given ei, ej, draw it
{
var x = translate ( si * squaresize );
var z = translate ( sj * squaresize );
var y = 17;
theSheep.position.x = x;
theSheep.position.y = y;
theSheep.position.z = z;
threeworld.scene.add(theSheep);
threeworld.lookat.copy ( theSheep.position ); // if camera moving, look back at where the enemy is
}
function initLogicalEnemy()
{
// start in random location:
var i, j;
do
{
i = 16;
j = 5;
}
while ( occupied(i,j) ); // search for empty square
ei = i;
ej = j;
}
function initLogicalbullet()
{
// start in random location:
var i, j;
do
{
i = ai;
j = aj;
}
while ( occupied(i,j) ); // search for empty square
bi = i;
bj = j;
}
function initLogicalSheep()
{
// start in random location:
var i, j;
do
{
i = 3;
j = 7;
}
while ( occupied(i,j) ); // search for empty square
si = i;
sj = j;
}
function initThreebullet()
{
var shape = new THREE.BoxGeometry( 5, 5, 5 );
bullet = new THREE.Mesh( shape );
bullet.material.color.setHex( BLANKCOLOR );
drawbullet();
}
function initThreeSheep()
{
var shape = new THREE.BoxGeometry( squaresize, BOXHEIGHT, squaresize );
theSheep = new THREE.Mesh( shape );
theSheep.material.color.setHex( BLANKCOLOR );
drawSheep();
}
var x =0;
// --- second enemy functions -----------------------------------
function moveLogicalEnemy()
{
// move towards agent
// put some randomness in so it won't get stuck with barriers
var i, j;
if ( ei > ai ) i = randomintAtoB(ei, ei+1);
if ( ei == ai ) i = ei;
if ( ei < ai ) i = randomintAtoB(ei-1, ei);
if ( ej > aj ) j = randomintAtoB(ej, ej+1);
if ( ej == aj ) j = ej;
if ( ej < aj ) j = randomintAtoB(ej-1, ej);
if (ei == 1 || ei == gridsize-2)
{
ei = 14;
ej = 7;
}
if (ej == 1 || ej == gridsize-1)
{
ei = 7;
ej = 7;
}
if (ei > 16 )
{
ei = 7;
ej = 7;
}
if ( ! occupied(i,j) ) // if no obstacle then move, else just miss a turn
{
ei = i;
ej = j;
}
}
function moveLogicalbullet()
{
// move towards agent
// put some randomness in so it won't get stuck with barriers
var i, j;
i = bi
j = bj -1
if (bi == 1 || bi == gridsize-2)
{
bi = 5;
bj = 2;
}
if (bj == 1 || bj == gridsize-2)
{
bi = 5;
bj = 2;
}
// if no obstacle then move, else just miss a turn
bi = i;
bj = j;
}
function moveLogicalSheep()
{
// move towards agent
// put some randomness in so it won't get stuck with barriers
var i, j;
if ( si > ai ) i = randomintAtoB(si, si+1);
if ( si == ai ) i = si;
if ( si < ai ) i = randomintAtoB(si-1, si);
if ( sj > aj ) j = randomintAtoB(sj, sj+1);
if ( sj == aj ) j = sj;
if ( sj < aj ) j = randomintAtoB(sj-1, sj);
if (si == 1 || si == gridsize-2)
{
si = 10;
sj = 5;
}
if (sj == 1 || sj == gridsize-2)
{
si = 10;
sj = 5;
}
if ( ! occupied(i,j) ) // if no obstacle then move, else just miss a turn
{
si = i;
sj = j;
}
}
if (si > 16 )
{
si = 10;
sj = 10;
}
// --- agent functions -----------------------------------
function drawAgent() // given ai, aj, draw it
{
var x = translate ( ai * squaresize );
var z = translate ( aj * squaresize );
var y = 30;
theagent.position.x = x;
theagent.position.y = y;
theagent.position.z = z;
threeworld.scene.add(theagent);
threeworld.follow.copy ( theagent.position ); // follow vector = agent position (for camera following agent)
}
function initLogicalAgent()
{
// start in random location:
var i, j;
do
{
i = 9;
j = 15;
}
while ( occupied(i,j) ); // search for empty square
ai = i;
aj = j;
}
function initThreeAgent()
{
var shape = new THREE.BoxGeometry( squaresize, BOXHEIGHT, squaresize );
theagent = new THREE.Mesh( shape );
theagent.material.color.setHex( BLANKCOLOR );
drawAgent();
}
function moveLogicalAgent( a ) // this is called by the infrastructure that gets action a from the Mind
{
var i = ai;
var j = aj;
if ( a == ACTION_LEFT ) i--;
else if ( a == ACTION_RIGHT ) i++;
else if ( a == ACTION_UP ) j++;
else if ( a == ACTION_DOWN ) j--;
if ( ! occupied(i,j) )
{
ai = i;
aj = j;
}
}
var dbullet = false;
function keyHandler(e)
// user control
// Note that this.takeAction(a) is constantly running at same time, redrawing the screen.
{
if (e.keyCode == 37) moveLogicalAgent ( ACTION_LEFT );
if (e.keyCode == 38) moveLogicalAgent ( ACTION_DOWN );
if (e.keyCode == 39) moveLogicalAgent ( ACTION_RIGHT );
if (e.keyCode == 40) moveLogicalAgent ( ACTION_UP );
if (e.keyCode == 32)
{
dbullet = true;
initThreebullet();
initLogicalbullet();
loadtextures2();
}
}
// --- score: -----------------------------------
function badstep() // is the enemy within one square of the agent
{
if ( ( Math.abs(ei - ai) < 2 ) && ( Math.abs(ej - aj) < 2 ) ) return true;
else return false;
}
var eb = false;
function enemyBlocked() // agent is blocked on all sides, run over
{
if(ei == bi && ej == bj)
{
eb = true;
return true;
}
else {
return false;
}
}
var sb = false;
function sheepBlocked()
{
if(si == bi && sj == bj)
{
sb = true;
return true;
}
else
{
return false;
}
}
var bm = false;
function hitMaze()
{
if((bi == 2 && bj == 4)||(bi == 8 && bj == 6)||(bi == 12 && bj == 3)||(bi == 18 && bj == 6)||(bi == 10 && bj == 2)||(bi == 4 && bj == 10)||(bi == 16 && bj == 10))
{
bm = true;
return true;
}
else
return false;
}
function count()
{
var x = x + 1;
}
function updateStatusBefore(a)
// this is called before anyone has moved on this step, agent has just proposed an action
// update status to show old state and proposed move
{
var x = self.getState();
var status = " Step: <b> " + step + " </b> x = (" + x.toString() + ") a = (" + a + ") ";
$("#user_span3").html( status );
}
function updateStatusAfter() // agent and enemy have moved, can calculate score
{
// new state after both have moved
var y = self.getState();
var status = " y = (" + y.toString() + ") <BR> ";
$("#user_span4").html( status );
var score = self.getScore();
var status = " Bad steps: " + badsteps +
" Good steps: " + goodsteps +
" Score: " + score.toFixed(2) + "% ";
$("#user_span5").html( status );
}
//--- public functions / interface / API ----------------------------------------------------------
this.endCondition; // If set to true, run will end.
this.newRun = function()
{
// (subtle bug) must reset variables like these inside newRun (in case do multiple runs)
this.endCondition = false;
badsteps = 0;
goodsteps = 0;
step = 0;
// for all runs:
initGrid();
initGrid2();
initGrid3();
initLogicalWalls();
initLogicalMaze();
initLogicalStone();
initLogicalBottom();
initLogicalBottom2();
initLogicalAgent();
if( eb != true && bm != true)
{
initLogicalEnemy();
}
if( sb != true && bm!= true)
{
initLogicalSheep();
}
// for graphical runs only:
if ( true )
{
if ( show3d )
{
BOXHEIGHT = squaresize;
threeworld.init3d ( startRadiusConst, maxRadiusConst, SKYCOLOR );
}
else
{
BOXHEIGHT = 1;
threeworld.init2d ( startRadiusConst, maxRadiusConst, SKYCOLOR );
}
initSkybox();
initMusic();
// Set up objects first:
initThreeWalls();
initThreeWalls2();
initThreeMaze();
initThreeStone();
initThreeBottom();
initThreeBottom2();
initThreeAgent();
// Then paint them with textures - asynchronous load of textures from files.
// The texture file loads return at some unknown future time in some unknown order.
// Because of the unknown order, it is probably best to make objects first and later paint them, rather than have the objects made when the file reads return.
// It is safe to paint objects in random order, but might not be safe to create objects in random order.
loadTextures();
document.onkeydown = keyHandler;
}
};
this.getState = function()
{
var x = [ ai, aj, ei, ej, si ,sj ];
return ( x );
};
this.takeAction = function ( a )
{
step++;
if ( true )
updateStatusBefore(a); // show status line before moves
moveLogicalAgent(a);
if ( ( step % 5 ) == 0 && !eb )
{ // slow the enemy down to every nth step
moveLogicalEnemy();
}
if ( ( step % 300))
{
moveLogicalbullet();
}
if(enemyBlocked() || sheepBlocked())
{
x = x+1
if(eb == true && sb == true)
{
this.endCondition = true;
}
}
if(hitMaze())
{
this.endCondition2 = true;
$("#user_span6").html( " <font color=red> <B> You Hit a Stone you lose </B> </font> " );
}
if ( ( step % 5 ) == 0 && !sheepBlocked() )
moveLogicalSheep();
if ( badstep() )
badsteps++;
else
goodsteps++;
if ( true )
{
drawAgent();
if (dbullet == true)
drawbullet();
if( sb != true && bm != true)
{
drawSheep();
}
if( eb != true && bm != true)
{
drawEnemy();
}
updateStatusAfter(); // show status line after moves
}
if ( enemyBlocked() || sheepBlocked() ) // if agent blocked in, game over
{
$("#user_span6").html( " <font color=red> <B> Shot animal 1 :(! </B> </font> " );
goodsteps = 0; // you score zero as far as database is concerned
if ( true )
{
musicPause();
soundAlarm();
}
}
};
this.endRun = function()
{
if ( true )
{
if ( this.endCondition )
$("#user_span6").html( " <font color=red> <B> Shot animal2 you win </B> </font> " );
}
};
this.getScore = function()
{
return ( ( goodsteps / step ) * 100 );
};
}
//---- end of World class -------------------------------------------------------
// --- music and sound effects ----------------------------------------
// credits:
// http://www.dl-sounds.com/royalty-free/defense-line/
// http://soundbible.com/1542-Air-Horn.html
function initMusic()
{
// put music element in one of the spans
var x = "<audio id=theaudio src=/uploads/starter/Defense.Line.mp3 autoplay loop> </audio>" ;
$("#user_span1").html( x );
}
function musicPlay()
{
// jQuery does not seem to parse pause() etc. so find the element the old way:
document.getElementById('theaudio').play();
}
function musicPause()
{
document.getElementById('theaudio').pause();
}
function soundAlarm()
{
var x = "<audio src=/uploads/starter/air.horn.mp3 autoplay > </audio>";
$("#user_span2").html( x );
}