Added eye editing stuff
This commit is contained in:
@@ -8,7 +8,8 @@ const CellTypes = {
|
||||
killer: 6,
|
||||
armor: 7,
|
||||
eye: 8,
|
||||
colors: ['#121D29', 'green', 'gray', 'orange', 'white', '#3493EB', 'red', 'purple', '#8D73A3'],
|
||||
colors: ['#121D29', 'green', 'gray', 'orange', 'white', '#3493EB', 'red', 'purple', '#d4bb3f'],
|
||||
names: ['Empty', 'Food', 'Wall', 'Mouth', 'Producer', 'Mover', 'Killer', 'Armor', 'Eye'],
|
||||
getRandomLivingType: function() {
|
||||
return Math.floor(Math.random() * 6) + 3;
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ class LocalCell{
|
||||
this.loc_col = loc_col;
|
||||
this.loc_row = loc_row;
|
||||
if (this.type == CellTypes.eye){
|
||||
this.eye = new Eye(this);
|
||||
this.eye = new Eye();
|
||||
if (eye != null) {
|
||||
this.eye.direction = eye.direction;
|
||||
}
|
||||
|
||||
@@ -21,6 +21,13 @@ const Directions = {
|
||||
case this.right:
|
||||
return this.left;
|
||||
}
|
||||
},
|
||||
rotateRight: function(dir) {
|
||||
dir++;
|
||||
if (dir > 3){
|
||||
dir = 0;
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ class Organism {
|
||||
this.cells = [];
|
||||
this.is_producer = false;
|
||||
this.is_mover = false;
|
||||
this.has_eyes = false;
|
||||
this.direction = Directions.down;
|
||||
this.rotation = Directions.up;
|
||||
this.can_rotate = Hyperparams.moversCanRotate;
|
||||
@@ -42,7 +43,7 @@ class Organism {
|
||||
}
|
||||
}
|
||||
|
||||
this.checkProducerMover(type);
|
||||
this.checkTypeChange(type);
|
||||
this.cells.push(new LocalCell(type, c, r, eye));
|
||||
return true;
|
||||
}
|
||||
@@ -65,7 +66,7 @@ class Organism {
|
||||
this.is_producer = false;
|
||||
this.is_producer = false;
|
||||
for (var cell of this.cells) {
|
||||
this.checkProducerMover(cell.type);
|
||||
this.checkTypeChange(cell.type);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
@@ -80,11 +81,13 @@ class Organism {
|
||||
return null;
|
||||
}
|
||||
|
||||
checkProducerMover(type) {
|
||||
checkTypeChange(type) {
|
||||
if (type == CellTypes.producer)
|
||||
this.is_producer = true;
|
||||
if (type == CellTypes.mover)
|
||||
this.is_mover = true;
|
||||
if (type == CellTypes.eye)
|
||||
this.has_eyes = true;
|
||||
}
|
||||
|
||||
inherit(parent) {
|
||||
@@ -93,11 +96,17 @@ class Organism {
|
||||
this.birth_distance = parent.birth_distance;
|
||||
for (var c of parent.cells){
|
||||
//deep copy parent cells
|
||||
if (c.type == CellTypes.eye)
|
||||
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);
|
||||
}
|
||||
if(parent.is_mover) {
|
||||
for (var i in parent.brain.decisions) {
|
||||
this.brain.decisions[i] = parent.brain.decisions[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// amount of food required before it can reproduce
|
||||
@@ -180,9 +189,9 @@ class Organism {
|
||||
}
|
||||
cell.type = CellTypes.getRandomLivingType();
|
||||
if (cell.type == CellTypes.eye) {
|
||||
cell.eye = new Eye(cell);
|
||||
cell.eye = new Eye();
|
||||
}
|
||||
this.checkProducerMover(cell.type);
|
||||
this.checkTypeChange(cell.type);
|
||||
mutated = true;
|
||||
}
|
||||
else if (choice <= Hyperparams.addProb + Hyperparams.changeProb + Hyperparams.removeProb){
|
||||
@@ -204,6 +213,9 @@ class Organism {
|
||||
if (this.birth_distance < 1)
|
||||
this.birth_distance = 1;
|
||||
}
|
||||
if (this.is_mover && this.has_eyes && Math.random() * 100 <= 10) {
|
||||
this.brain.mutate();
|
||||
}
|
||||
return mutated;
|
||||
}
|
||||
|
||||
@@ -249,6 +261,11 @@ class Organism {
|
||||
return false;
|
||||
}
|
||||
|
||||
changeDirection(dir) {
|
||||
this.direction = dir;
|
||||
this.move_count = 0;
|
||||
}
|
||||
|
||||
// assumes either c1==c2 or r1==r2, returns true if there is a clear path from point a to b
|
||||
isStraightPath(c1, r1, c2, r2, parent){
|
||||
if (c1 == c2) {
|
||||
@@ -320,7 +337,7 @@ 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){
|
||||
if (cell.type == CellTypes.eye) {
|
||||
this.getRealCell(cell).direction = cell.eye.getAbsoluteDirection(this.rotation);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,10 @@ const Directions = require("../Directions");
|
||||
const Decision = {
|
||||
neutral: 0,
|
||||
retreat: 1,
|
||||
chase: 2
|
||||
chase: 2,
|
||||
getRandom: function(){
|
||||
return Math.floor(Math.random() * 3);
|
||||
}
|
||||
}
|
||||
|
||||
class Brain {
|
||||
@@ -16,7 +19,7 @@ class Brain {
|
||||
// corresponds to CellTypes
|
||||
this.decisions = [
|
||||
Decision.neutral, // empty
|
||||
Decision.chase, // food
|
||||
Decision.chase, // food
|
||||
Decision.neutral, // wall
|
||||
Decision.neutral, // mouth
|
||||
Decision.neutral, // producer
|
||||
@@ -27,19 +30,19 @@ class Brain {
|
||||
];
|
||||
}
|
||||
|
||||
observe(observation){
|
||||
observe(observation) {
|
||||
this.observations.push(observation);
|
||||
}
|
||||
|
||||
decide(){
|
||||
decide() {
|
||||
var decision = Decision.neutral;
|
||||
var closest = Hyperparams.lookRange + 1;
|
||||
var move_direction = 0;
|
||||
for (var obs of this.observations) {
|
||||
if (obs.cell == null || obs.cell.owner == this.owner){
|
||||
if (obs.cell == null || obs.cell.owner == this.owner) {
|
||||
continue;
|
||||
}
|
||||
if (obs.distance < closest){
|
||||
if (obs.distance < closest) {
|
||||
decision = this.decisions[obs.cell.type];
|
||||
move_direction = obs.direction;
|
||||
closest = obs.distance;
|
||||
@@ -47,17 +50,20 @@ class Brain {
|
||||
}
|
||||
this.observations = [];
|
||||
if (decision == Decision.chase) {
|
||||
this.owner.direction = move_direction;
|
||||
this.owner.move_count = 0;
|
||||
this.owner.changeDirection(move_direction);
|
||||
return true;
|
||||
}
|
||||
else if (decision == Decision.retreat) {
|
||||
this.owner.direction = Directions.getOppositeDirection(move_direction);
|
||||
this.owner.move_count = 0;
|
||||
this.owner.changeDirection(Directions.getOppositeDirection(move_direction));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
mutate() {
|
||||
var selection = Math.floor(Math.random() * (this.decisions.length-1))+1;
|
||||
this.decisions[selection] = Decision.getRandom();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Brain;
|
||||
@@ -4,12 +4,11 @@ const CellTypes = require("../Cell/CellTypes");
|
||||
const Observation = require("./Observation")
|
||||
|
||||
class Eye {
|
||||
constructor(loc_cell, direction=-1) {
|
||||
constructor(direction=-1) {
|
||||
this.direction = direction;
|
||||
if (direction == -1){
|
||||
this.direction = Directions.getRandomDirection();
|
||||
}
|
||||
this.loc_cell = loc_cell
|
||||
}
|
||||
|
||||
getAbsoluteDirection(parent_dir) {
|
||||
@@ -44,7 +43,7 @@ class Eye {
|
||||
col+=addCol;
|
||||
row+=addRow;
|
||||
cell = env.grid_map.cellAt(col, row);
|
||||
if (cell == null){
|
||||
if (cell == null) {
|
||||
break;
|
||||
}
|
||||
if (cell.type != CellTypes.empty){
|
||||
|
||||
Reference in New Issue
Block a user