Added auto food generation

This commit is contained in:
Max Robinson
2021-02-03 16:39:51 -07:00
parent b906d14c91
commit 56b27c65b6
5 changed files with 29 additions and 1 deletions

View File

@@ -4,6 +4,8 @@ const GridMap = require('../Grid/GridMap');
const Organism = require('../Organism/Organism');
const CellStates = require('../Organism/Cell/CellStates');
const EnvironmentController = require('../Controllers/EnvironmentController');
const Hyperparams = require('../Hyperparameters.js');
const Cell = require('../Organism/Cell/GridCell');
class WorldEnvironment extends Environment{
constructor(cell_size) {
@@ -29,6 +31,9 @@ class WorldEnvironment extends Environment{
to_remove.push(i);
}
}
if (Hyperparams.foodDropProb > 0) {
this.generateFood();
}
this.removeOrganisms(to_remove);
}
@@ -94,6 +99,21 @@ class WorldEnvironment extends Environment{
this.organisms = [];
}
generateFood() {
var num_food = Math.max(Math.floor(this.grid_map.cols*this.grid_map.rows*Hyperparams.foodDropProb/50000), 1)
var prob = Hyperparams.foodDropProb;
for (var i=0; i<num_food; i++) {
if (Math.random() <= prob){
var c=Math.floor(Math.random() * this.grid_map.cols);
var r=Math.floor(Math.random() * this.grid_map.rows);
if (this.grid_map.cellAt(c, r).state == CellStates.empty){
this.changeCell(c, r, CellStates.food, null);
}
}
}
}
reset(clear_walls=true) {
this.organisms = [];
this.grid_map.fillGrid(CellStates.empty);