organism saving/loading

This commit is contained in:
MaxRobinsonTheGreat
2022-04-10 13:07:10 -05:00
parent 8005b5312c
commit dabeb4463d
7 changed files with 118 additions and 23 deletions

View File

@@ -1,14 +1,19 @@
const CellStates = require("./Cell/CellStates");
const BodyCellFactory = require("./Cell/BodyCells/BodyCellFactory");
const SerializeHelper = require("../Utils/SerializeHelper");
class Anatomy {
constructor(owner) {
this.owner = owner;
this.birth_distance = 4;
this.clear();
}
clear() {
this.cells = [];
this.is_producer = false;
this.is_mover = false;
this.has_eyes = false;
this.birth_distance = 4;
}
canAddCellAt(c, r) {
@@ -118,6 +123,24 @@ class Anatomy {
}
return true;
}
serialize() {
let anatomy = SerializeHelper.copyNonObjects(this);
anatomy.cells = [];
for (let cell of this.cells) {
let newcell = SerializeHelper.copyNonObjects(cell);
newcell.state = {name: cell.state.name};
anatomy.cells.push(newcell)
}
return anatomy;
}
loadRaw(anatomy) {
this.clear();
for (let cell of anatomy.cells){
this.addInheritCell(cell);
}
}
}
module.exports = Anatomy;

View File

@@ -5,6 +5,7 @@ const Directions = require("./Directions");
const Anatomy = require("./Anatomy");
const Brain = require("./Perception/Brain");
const FossilRecord = require("../Stats/FossilRecord");
const SerializeHelper = require("../Utils/SerializeHelper");
class Organism {
constructor(col, row, env, parent=null) {
@@ -38,10 +39,8 @@ class Organism {
//deep copy parent cells
this.anatomy.addInheritCell(c);
}
if(parent.anatomy.is_mover) {
for (var i in parent.brain.decisions) {
this.brain.decisions[i] = parent.brain.decisions[i];
}
if(parent.anatomy.is_mover && parent.anatomy.has_eyes) {
this.brain.copy(parent.brain);
}
}
@@ -104,7 +103,6 @@ class Organism {
var new_c = this.c + (direction_c*basemovement) + (direction_c*offset);
var new_r = this.r + (direction_r*basemovement) + (direction_r*offset);
// console.log(org.isClear(new_c, new_r, org.rotation, true))
if (org.isClear(new_c, new_r, org.rotation, true) && org.isStraightPath(new_c, new_r, this.c, this.r, this)){
org.c = new_c;
org.r = new_r;
@@ -125,7 +123,6 @@ class Organism {
let changed = false;
let removed = false;
if (this.calcRandomChance(Hyperparams.addProb)) {
// console.log('add')
let branch = this.anatomy.getRandomCell();
let state = CellStates.getRandomLivingType();//branch.state;
let growth_direction = Neighbors.all[Math.floor(Math.random() * Neighbors.all.length)]
@@ -321,6 +318,26 @@ class Organism {
return this.env.grid_map.cellAt(real_c, real_r);
}
serialize() {
let org = SerializeHelper.copyNonObjects(this);
org.anatomy = this.anatomy.serialize();
if (this.brain)
org.brain = this.brain.serialize();
return org;
}
loadRaw(org) {
for (let key in org)
if (typeof org[key] !== 'object')
this[key] = org[key];
this.anatomy.loadRaw(org.anatomy)
console.log(org)
if (org.brain) {
console.log('load brain')
this.brain.copy(org.brain)
}
}
}
module.exports = Organism;

View File

@@ -20,16 +20,18 @@ class Brain {
this.observations = [];
// corresponds to CellTypes
this.decisions = [];
this.decisions[CellStates.empty.name] = Decision.neutral;
this.decisions = {};
for (let cell of CellStates.all) {
this.decisions[cell.name] = Decision.neutral;
}
this.decisions[CellStates.food.name] = Decision.chase;
this.decisions[CellStates.wall.name] = Decision.neutral;
this.decisions[CellStates.mouth.name] = Decision.neutral;
this.decisions[CellStates.producer.name] = Decision.neutral;
this.decisions[CellStates.mover.name] = Decision.neutral;
this.decisions[CellStates.killer.name] = Decision.retreat;
this.decisions[CellStates.armor.name] = Decision.neutral;
this.decisions[CellStates.eye.name] = Decision.neutral;
}
copy(brain) {
for (let dec in brain.decisions) {
this.decisions[dec] = brain.decisions[dec];
}
}
randomizeDecisions(randomize_all=false) {
@@ -58,9 +60,7 @@ class Brain {
continue;
}
if (obs.distance < closest) {
// console.log(obs.cell.state)
decision = this.decisions[obs.cell.state.name];
// console.log(decision)
move_direction = obs.direction;
closest = obs.distance;
}
@@ -81,6 +81,10 @@ class Brain {
this.decisions[CellStates.getRandomName()] = Decision.getRandom();
this.decisions[CellStates.empty.name] = Decision.neutral; // if the empty cell has a decision it gets weird
}
serialize() {
return {decisions: this.decisions};
}
}
Brain.Decision = Decision;