Added body cell functionality

This commit is contained in:
MaxRobinsonTheGreat
2020-08-17 23:35:47 -06:00
parent 3589df3919
commit 3f05fbe7f9
24 changed files with 598 additions and 294 deletions

View File

@@ -0,0 +1,81 @@
const CellStates = require("../CellStates");
const BodyCell = require("./BodyCell");
const Hyperparams = require("../../../Hyperparameters");
const Directions = require("../../Directions");
const Observation = require("../../Perception/Observation")
class EyeCell extends BodyCell{
constructor(org, loc_col, loc_row){
super(CellStates.eye, org, loc_col, loc_row);
this.org.has_eyes = true;
}
initInherit(parent) {
// deep copy parent values
super.initInherit(parent);
this.direction = parent.direction;
}
initRandom() {
// initialize values randomly
this.direction = Directions.getRandomDirection();
}
initDefault() {
// initialize to default values
this.direction = Directions.up;
}
getAbsoluteDirection() {
var dir = this.org.rotation + this.direction;
if (dir > 3)
dir -= 4;
return dir;
}
performFunction() {
var obs = this.look();
this.org.brain.observe(obs);
}
look() {
var env = this.org.env;
var direction = this.getAbsoluteDirection();
var addCol = 0;
var addRow = 0;
switch(direction) {
case Directions.up:
addRow = -1;
break;
case Directions.down:
addRow = 1;
break;
case Directions.right:
addCol = 1;
break;
case Directions.left:
addCol = -1;
break;
}
var start_col = this.getRealCol();
var start_row = this.getRealRow();
var col = start_col;
var row = start_row;
var cell = null;
for (var i=0; i<Hyperparams.lookRange; i++){
col+=addCol;
row+=addRow;
cell = env.grid_map.cellAt(col, row);
if (cell == null) {
break;
}
if (cell.state != CellStates.empty){
var distance = Math.abs(start_col-col) + Math.abs(start_row-row);
return new Observation(cell, distance, direction);
}
}
return new Observation(cell, Hyperparams.lookRange, direction);
}
}
module.exports = EyeCell;