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

3
dist/index.html vendored
View File

@@ -189,6 +189,9 @@
<br>
<label for="look-range" title='How far an eye cell can see (in number of cells)'>Look range:</label>
<input type="number" id="look-range" min="1" max="50" value=20 step="1">
<br>
<label for="food-drop-rate" title='Rate at which food is automatically generated and dropped in the world'>Auto food drop rate:</label>
<input type="number" id="food-drop-rate" value=0 max="1000">
</div>
<div class='right-half'>

2
dist/js/bundle.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -106,6 +106,9 @@ class ControlPanel {
$('#look-range').change(function() {
Hyperparams.lookRange = $('#look-range').val();
});
$('#food-drop-rate').change(function() {
Hyperparams.foodDropProb = $('#food-drop-rate').val();
});
$('#evolved-mutation').change( function() {
if (this.checked) {

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);

View File

@@ -24,6 +24,8 @@ const Hyperparams = {
this.instaKill = false;
this.lookRange = 20;
this.foodDropProb = 0;
},
balanceMutationProbs : function(choice) {