Added eye cell

This commit is contained in:
MaxRobinsonTheGreat
2020-08-10 19:19:52 -06:00
parent a6cfc916c2
commit 6003686a13
8 changed files with 107 additions and 17 deletions

View File

@@ -7,9 +7,10 @@ const CellTypes = {
mover: 5,
killer: 6,
armor: 7,
colors: ['#121D29', 'green', 'gray', 'orange', 'white', '#3493eb', 'red', 'purple'],
eye: 8,
colors: ['#121D29', 'green', 'gray', 'orange', 'white', '#3493EB', 'red', 'purple', '#8D73A3'],
getRandomLivingType: function() {
return Math.floor(Math.random() * 5) + 3;
return Math.floor(Math.random() * 6) + 3;
}
}

View File

@@ -1,15 +1,24 @@
const CellTypes = require("./CellTypes");
const Directions = require("../Directions");
const Hyperparams = require("../../Hyperparameters");
const Eye = require("../Eye.js");
// A local cell is a lightweight container for a cell in an organism. It does not directly exist in the grid
class LocalCell{
constructor(type, loc_col, loc_row){
constructor(type, loc_col, loc_row, eye=null){
this.type = type;
this.loc_col = loc_col;
this.loc_row = loc_row;
if (this.type == CellTypes.eye){
this.eye = new Eye(this);
if (eye != null) {
this.eye.direction = eye.direction;
}
}
}
rotatedCol(dir){
switch(dir){
case Directions.up:

View File

@@ -1,8 +1,8 @@
const Directions = {
up:0,
down:1,
left:2,
right:3,
right:1,
down:2,
left:3,
scalars:[[0,-1],[0,1],[-1,0],[1,0]],
getRandomDirection: function() {
return Math.floor(Math.random() * 4);

20
src/Organism/Eye.js Normal file
View File

@@ -0,0 +1,20 @@
const Directions = require("./Directions");
class Eye {
constructor(loc_cell, direction=-1) {
this.direction = direction;
if (direction == -1){
this.direction = Directions.getRandomDirection();
}
this.loc_cell = loc_cell
}
getAbsoluteDirection(parent_dir) {
var dir = parent_dir + this.direction;
if (dir > 3)
dir -= 4;
return dir;
}
}
module.exports = Eye;

View File

@@ -5,6 +5,7 @@ const LocalCell = require("./Cell/LocalCell");
const Neighbors = require("../Grid/Neighbors");
const Hyperparams = require("../Hyperparameters");
const Directions = require("./Directions");
const Eye = require("./Eye");
const directions = [[0,1],[0,-1],[1,0],[-1,0]]
@@ -32,7 +33,7 @@ class Organism {
}
}
addCell(type, c, r) {
addCell(type, c, r, eye=null) {
for (var cell of this.cells) {
if (cell.loc_col == c && cell.loc_row == r){
return false;
@@ -40,7 +41,7 @@ class Organism {
}
this.checkProducerMover(type);
this.cells.push(new LocalCell(type, c, r));
this.cells.push(new LocalCell(type, c, r, eye));
return true;
}
@@ -90,7 +91,10 @@ class Organism {
this.birth_distance = parent.birth_distance;
for (var c of parent.cells){
//deep copy parent cells
this.addCell(c.type, c.loc_col, c.loc_row);
if (c.type == CellTypes.eye)
this.addCell(c.type, c.loc_col, c.loc_row, c.eye);
else
this.addCell(c.type, c.loc_col, c.loc_row);
}
}
@@ -169,14 +173,20 @@ class Organism {
else if (choice <= Hyperparams.addProb + Hyperparams.changeProb){
// change cell
var cell = this.cells[Math.floor(Math.random() * this.cells.length)];
if (cell.type == CellTypes.eye) {
delete cell.eye;
}
cell.type = CellTypes.getRandomLivingType();
if (cell.type == CellTypes.eye) {
cell.eye = new Eye(cell);
}
this.checkProducerMover(cell.type);
mutated = true;
}
else if (choice <= Hyperparams.addProb + Hyperparams.changeProb + Hyperparams.removeProb){
// remove cell
if(this.cells.length > 1) {
cell = this.cells[Math.floor(Math.random() * this.cells.length)];
var cell = this.cells[Math.floor(Math.random() * this.cells.length)];
mutated = this.removeCell(cell.loc_col, cell.loc_row);
}
}
@@ -308,6 +318,9 @@ class Organism {
var real_c = this.c + cell.rotatedCol(this.rotation);
var real_r = this.r + cell.rotatedRow(this.rotation);
this.env.changeCell(real_c, real_r, cell.type, this);
if (cell.type == CellTypes.eye){
this.getRealCell(cell).direction = cell.eye.getAbsoluteDirection(this.rotation);
}
}
}
@@ -322,9 +335,12 @@ class Organism {
}
for (var cell of this.cells) {
this.getRealCell(cell).performFunction(this.env);
}
if (!this.living){
return this.living
if (!this.living){
return this.living
}
if (cell.type == CellTypes.eye && cell.eye == null){
console.log("whoe nellie");
}
}
if (this.is_mover) {
this.move_count++;