Code viewer for World: A* Bootstrap World - 3D La...
//A* Bootstrap World - 3D Labyrinth (Maze Network)
//Author: Venkatraman Palani (23266990) [MCM1 - MSc: Computing (AI Major), Module: CA686 Foundations of AI]

/*
	AB.world.newRun = function()
	{
		// Code for Three.js initial drawing of objects.
		// Should include one of:
 		// ABWorld.init2d ( arguments ); 	
 		// ABWorld.init3d ( arguments ); 	
	};


	AB.world.takeAction = function ( action )		 
	{
		// Code for Three.js re-drawing of objects.  
	};


	AB.world.endRun = function()
	{
	};


	AB.world.getState = function()
	{
	  return ( 0 );  				 
	};

	
	AB.world.getScore = function()
 	{
	  return ( 0 );		
	};
*/

class Node {
  constructor(world, idx, jdx) {
    this.world = world;
    this.GRID = world.GRID;
    this.idx = idx;
    this.jdx = jdx;

    this.columns = world.gridSize;
    this.rows = world.gridSize;
    this.squareSize = world.squareSize;

    this.calculatedTotal = 0; // f
    this.stepCost = 0; // g
    this.heuristicEstimate = 0; // h

    this.neighbours = [];
    this.previous = null;

    this.gridWall = false;
    this.mazeWall = false;

    this.isEmpty = false;

    this.tempCube = null;
    this.Cube = null;
  }
  
   addNeighbours() {
    if (this.idx < this.columns - 1) {
      this.neighbours.push(this.GRID[this.idx + 1][this.jdx]);
    }
    if (this.idx > 0) {
      this.neighbours.push(this.GRID[this.idx - 1][this.jdx]);
    }
    if (this.jdx < this.rows - 1) {
      this.neighbours.push(this.GRID[this.idx][this.jdx + 1]);
    }
    if (this.jdx > 0) {
      this.neighbours.push(this.GRID[this.idx][this.jdx - 1]);
    }
  }

  setAsGridWall() {
    this.gridWall = true;
  }

  setAsMazeWall() {
    this.mazeWall = true;
  }

  setAsEmpty() {
    this.isEmpty = true;
  }

  isWall() {
    return this.gridWall || this.mazeWall;
  }

  showTemperoryPath() {
    this.tempShape = new THREE.BoxGeometry(30, 30, 30);
    this.tempCube = new THREE.Mesh(this.tempShape);

    this.tempCube.material = new THREE.MeshBasicMaterial();

    this.tempCube.position.copy(this.translate(this.idx, this.jdx)); // translate my (i,j) grid coordinates to three.js (x,y,z) coordinates
    this.tempCube.material.color.setColorName("yellow");
    this.tempCube.material.opacity = 0.25;
    ABWorld.scene.add(this.tempCube);
  }

  removeTemperoryPath() {
    ABWorld.scene.remove(this.tempCube);
    this.tempShape = null;
    this.tempCube = null;
  }

  showPermanentPath() {
    this.Shape = new THREE.BoxGeometry(30, 30, 30);
    this.Cube = new THREE.Mesh(this.Shape);

    this.Cube.material = new THREE.MeshBasicMaterial();

    this.Cube.position.copy(this.translate(this.idx, this.jdx)); // translate my (i,j) grid coordinates to three.js (x,y,z) coordinates
    this.Cube.material.color.setColorName("green");
    this.Cube.material.opacity = 0.25;
    ABWorld.scene.add(this.Cube);
  }

  removePermanentPath() {
    ABWorld.scene.remove(this.Cube);
    this.Shape = null;
    this.Cube = null;
  }

  translate(idx, jdx) {
    let vector = new THREE.Vector3();

    vector.y = 0;
    vector.x = idx * this.world.squareSize - this.world.MAXPOS / 2;
    vector.z = jdx * this.world.squareSize - this.world.MAXPOS / 2;

    return vector;
  }
}

class Analytics {
  constructor() {
    this.API_URL = "https://api.airtable.com/v0/appaNU1MlDvrEYsnk/Table%201";
    this.API_KEY = "a2V5T1BOeXRjUElpV3pCU0c=";
  }

  sendAnalytics(world, totalSteps, goodSteps, badSteps, score, agentBlocked) {
    let data = {
      "records": [
        {
          "fields": {
            "world": world,
            "totalSteps": totalSteps,
            "goodSteps": goodSteps,
            "badSteps": badSteps,
            "score": score,
            "agentBlocked": agentBlocked
          }
        }
      ]
    };

    $.ajax({
      type: "POST",
      url: this.API_URL,
      contentType: "application/json",
      data: JSON.stringify(data),
      headers: {
        "Authorization": "Bearer " + atob(this.API_KEY)
      },
      success: function() {}
    });
  }
}