Merge pull request #80 from MaxRobinsonTheGreat/develop

Update 1.0.2
This commit is contained in:
Max Robinson
2021-12-21 18:59:34 -06:00
committed by GitHub
21 changed files with 532 additions and 188 deletions

View File

@@ -1,5 +1,32 @@
# Changelog # Changelog
## 1.0.2 (12/21/2021)
### UI Enhancements:
- New tab "World Controls"
- Relocated grid controls and auto reset to this tab
- Button to generate random walls with perlin noise
- Button to reset the environment with many randomly generated organisms
- Option to not clear walls on reset
- Option to pause on total extinction
- "Simulation controls" tab renamed to "Evolution Controls"
- Button to save/load Evolution Controls in a `.json` file
- Button to randomize the organism in the editor window
- Can now use drag view tool while rendering is off
- Reorganized "About" tab and left panel, embedded explanation video
### Simulation Enhancements:
- New evolution control `Extra Mover Reproduction Cost`, which adds additional food cost for movers to reproduce
- Combined `Movers can rotate` and `Offspring rotate` evolution controls into `Rotation enabled`
- Fully max out simulation speed when slider is all the way to the right
### Bug Fixes:
- Armor is no longer ignored when checking for clear reproduction space
- Chart data is now properly loaded/discarded when paused
Thanks to contributors: @Chrispykins @M4YX0R
## 1.0.1 (12/4/2021) ## 1.0.1 (12/4/2021)
### UI Enhancements: ### UI Enhancements:

32
dist/css/style.css vendored
View File

@@ -57,11 +57,15 @@ body{
img { img {
image-rendering: -moz-crisp-edges; image-rendering: -moz-crisp-edges;
image-rendering: -webkit-crisp-edges; image-rendering: -webkit-crisp-edges;
image-rendering: pixelated; image-rendering: pixelated;
image-rendering: crisp-edges; image-rendering: crisp-edges;
width: 60%; object-fit: cover;
width: 85%;
max-width: 500px;
max-height: 40%;
border-radius: 10px; border-radius: 10px;
overflow: hidden;
} }
button { button {
@@ -75,6 +79,7 @@ button {
display: inline-block; display: inline-block;
font-size: 16px; font-size: 16px;
min-width: 30px; min-width: 30px;
margin: 2px;
} }
button:hover{ button:hover{
background-color: #81d2c7; background-color: #81d2c7;
@@ -133,6 +138,10 @@ button:active{
background-color: #81d2c7; background-color: #81d2c7;
color: black; color: black;
} }
.open-tab {
background-color: #66a39b;
color: black;
}
.tab { .tab {
grid-template-columns: repeat(2, 1fr); grid-template-columns: repeat(2, 1fr);
@@ -255,4 +264,19 @@ button:active{
} }
#maximize-hot-control { #maximize-hot-control {
right: 10px; right: 10px;
}
.grid-size-in {
width: 75px;
}
#video {
height: 200px;
margin: auto;
margin-bottom: 0;
padding-bottom: 0;
}
#reset-with-editor-org{
margin-top: 5px;
} }

127
dist/index.html vendored
View File

@@ -24,56 +24,34 @@
<button class="edit-mode-btn wall-drop" id="wall-drop" title="Drop/Remove Wall. Hotkey: D"><i class="fa fa-square"></i></button> <button class="edit-mode-btn wall-drop" id="wall-drop" title="Drop/Remove Wall. Hotkey: D"><i class="fa fa-square"></i></button>
<button class="edit-mode-btn food-drop selected" id="food-drop" title="Drop/Remove Food. Hotkey: F"><i class="fa fa-cutlery"></i></button> <button class="edit-mode-btn food-drop selected" id="food-drop" title="Drop/Remove Food. Hotkey: F"><i class="fa fa-cutlery"></i></button>
<button class="edit-mode-btn click-kill" id="click-kill" title="Click to kill. Hotkey: G"><i class="fa fa-bolt"></i></button> <button class="edit-mode-btn click-kill" id="click-kill" title="Click to kill. Hotkey: G"><i class="fa fa-bolt"></i></button>
<button id="clear-walls" title="Clear All Walls. Hotkey: B"><i class="fa fa-window-close"></i></button>
</div> </div>
<h2>Simulation Speed</h2> <img src="./img/title.png" alt="Life Engine">
<h3>Simulation Speed</h3>
<input id="slider" type="range" min="1" max="300" value="60"> <input id="slider" type="range" min="1" max="300" value="60">
<button class='pause-button' title="Play/Pause. Hotkey: Spacebar"><i class="fa fa-pause"></i></button> <button class='pause-button' title="Play/Pause. Hotkey: Spacebar"><i class="fa fa-pause"></i></button>
<button class="headless" title="Toggle rendering. Hotkey: H"><i class="fa fa-eye-slash"></i></button> <button class="headless" title="Toggle rendering. Hotkey: H"><i class="fa fa-eye-slash"></i></button>
<p id='fps'>Target FPS: 60</p> <p id='fps'>Target FPS: 60</p>
<p id='fps-actual'></p> <p id='fps-actual'></p>
<button id='reset-env' title='Restarts simulation with default organism.'>Reset</button> <button id='reset-env' title='Restarts simulation with default organism.'>Reset</button>
<button id='clear-env' title="Removes all organisms and walls. Will disable 'reset on extinction'"">Clear</button> <button id='clear-env' title="Removes all organisms.">Clear</button>
<br>
<label for="auto-reset">Reset on total extinction</label>
<input type="checkbox" id="auto-reset" checked>
<br>
<p id='reset-count'>Auto reset count: </p>
<h3>Grid Size</h3>
<label for="cell-size">Cell Size:</label>
<input type="number" id="cell-size" min="1" max="100" value=5 step="1">
<label for="fill-window">Fill Window</label>
<input type="checkbox" id="fill-window" checked>
<div class='col-row-input'>
<label for="col-input">Columns:</label>
<input type="number" id="col-input" min="1" value=100 step="1">
<label for="row-input">Rows:</label>
<input type="number" id="row-input" min="1" value=100 step="1">
</div>
<br>
<button id='resize'>Resize and Reset</button>
</div> </div>
<div id='tab-container' class='control-set'> <div id='tab-container' class='control-set'>
<div class="tabnav"> <div class="tabnav">
<p class='tabnav-item' id='about'>About</p> <p class='tabnav-item open-tab' id='about'>About</p>
<p class='tabnav-item' id='editor'>Editor</p> <p class='tabnav-item' id='editor'>Editor</p>
<p class='tabnav-item' id='hyperparameters'>Simulation Controls</p> <p class='tabnav-item' id='world-controls'>World Controls</p>
<p class='tabnav-item' id='stats'>Stats</p> <p class='tabnav-item' id='hyperparameters'>Evolution Controls</p>
<p class='tabnav-item' id='challenges'>Challenges</p> <p class='tabnav-item' id='stats'>Statistics</p>
<button id="minimize" title="Minimze Control Panel."><i class="fa fa-minus-square"></i></button> <button id="minimize" title="Minimze Control Panel."><i class="fa fa-minus-square"></i></button>
</div> </div>
<div id='about' class='tab'> <div id='about' class='tab'>
<div class='left-half'> <div class='left-half'>
<img src="./img/title.png" alt="Life Engine"> <h3>The Life Engine</h3>
<p>The Life Engine is a virtual ecosystem that allows organisms to grow, spread, and compete.</p> <p>The Life Engine is a virtual ecosystem that allows organisms to reproduce, compete, and evolve.</p>
<p>Each organism is made up by a structure of cells, which provide different benefits based on their color.</p> <p>Each organism is made up of different colored cells. Hover over each color to learn what it does.</p>
</div>
<div class='right-half'>
<h4>Cell Types</h4>
<div id='cell-legend'> <div id='cell-legend'>
<div class='cell-legend-type' id='mouth' title="Mouth: Eats adjacent food."></div> <div class='cell-legend-type' id='mouth' title="Mouth: Eats adjacent food."></div>
<div class='cell-legend-type' id='producer' title="Producer: Produces adjacent food."></div> <div class='cell-legend-type' id='producer' title="Producer: Produces adjacent food."></div>
@@ -83,14 +61,13 @@
<div class='cell-legend-type' id='eye' title="Eye: Observes cells and decides movement."></div> <div class='cell-legend-type' id='eye' title="Eye: Observes cells and decides movement."></div>
<div class='cell-legend-type' id='food' title="Food: Not part of an organism. Once an organism has eaten enough food, it will reproduce."></div> <div class='cell-legend-type' id='food' title="Food: Not part of an organism. Once an organism has eaten enough food, it will reproduce."></div>
<div class='cell-legend-type' id='wall' title="Wall: Not part of an organism. Blocks movement and reproduction."></div> <div class='cell-legend-type' id='wall' title="Wall: Not part of an organism. Blocks movement and reproduction."></div>
</div> </div><br>
<br> </div>
<p>Hover over each color to learn what it does. For a more in depth explanation of the simulation, view the <div class='right-half'>
<a href="https://github.com/MaxRobinsonTheGreat/EvolutionSimulatorV2#readme">readme</a> and you can explore the source code. <iframe id="video" src="https://www.youtube.com/embed/iSAKEnRfles"></iframe>
</p>
</div> </div>
<div class='icon-links'> <div class='icon-links'>
<a href="https://www.youtube.com/channel/UCwBhBDsqiQflTMLy2epbQVw"><i class="fa fa-youtube"></i></a> <a href=https://github.com/MaxRobinsonTheGreat/EvolutionSimulatorV2 title='View the code'><i class="fa fa-github"></i></a>
<a href="https://twitter.com/max_romana"><i class="fa fa-twitter"></i></a> <a href="https://twitter.com/max_romana"><i class="fa fa-twitter"></i></a>
</div> </div>
</div> </div>
@@ -118,6 +95,8 @@
</div> </div>
<button id='clear-editor'>Clear</button> <button id='clear-editor'>Clear</button>
<button class='randomize-button' id='generate-random' title="Randomizes organism"><i class="fa fa-random"></i></button>
<br>
</div> </div>
</div> </div>
@@ -147,7 +126,7 @@
<label for="mutation-rate-edit" title='Probability that offspring of this organism will mutate'>Mutation Rate:</label> <label for="mutation-rate-edit" title='Probability that offspring of this organism will mutate'>Mutation Rate:</label>
<input type="number" id="mutation-rate-edit" min="1" max="100" value=3 step="1"> <input type="number" id="mutation-rate-edit" min="1" max="100" value=3 step="1">
</div> </div>
<br> <!-- <br> -->
<div class='brain-details'> <div class='brain-details'>
<h4>Brain</h4> <h4>Brain</h4>
<label for="observation-type-edit">Observation: </label> <label for="observation-type-edit">Observation: </label>
@@ -172,23 +151,55 @@
<p class='retreat-types'>Move Away From: killer</p> <p class='retreat-types'>Move Away From: killer</p>
</div> </div>
</div> </div>
<button id='reset-with-editor-org' title='Reset the environment with the organism in the editor'>Reset with Editor Organism</button>
</div>
</div>
<div id='world-controls' class='tab'>
<div class='left-half'>
<h3>Grid Size</h3>
<label for="cell-size">Cell Size:</label>
<input type="number" id="cell-size" min="1" max="100" value=5 step="1">
<label for="fill-window">Fill Window</label>
<input type="checkbox" id="fill-window" checked>
<div class='col-row-input'>
<label for="col-input">Columns:</label>
<input type="number" class="grid-size-in" id="col-input" min="1" value=100 step="1">
<label for="row-input">Rows:</label>
<input type="number" class="grid-size-in" id="row-input" min="1" value=100 step="1">
</div>
<button id='resize'>Resize and Reset</button>
<h3>Reset Options</h3>
<label for="auto-reset">Reset on total extinction</label>
<input type="checkbox" id="auto-reset" checked>
<p id='reset-count'>Auto reset count: </p>
<label for="auto-pause" title='Will override reset on extinction'>Pause on total extinction</label>
<input type="checkbox" id="auto-pause">
<br>
<button class='randomize-button reset-random' title="Generate many random organisms in the world.">Reset with Random Organisms</button>
<label for="num-random-orgs" title='Number of random organisms to generate'>Num to generate:</label>
<input type="number" id="num-random-orgs" min="1" value=100 step="1">
</div>
<div class='right-half'>
<br>
<button id='random-walls' title="Generates random walls.">Generate random walls</button> <br>
<button id="clear-walls" title="Clear All Walls. Hotkey: B">Clear all walls</button>
<br>
<label for="clear-walls-reset" title='When on, walls will be cleared when the environment resets'>Clear walls on reset</label>
<input type="checkbox" id="clear-walls-reset">
</div> </div>
</div> </div>
<div id='hyperparameters' class='tab'> <div id='hyperparameters' class='tab'>
<div class='left-half'> <div class='left-half'>
<h2>Simulation Controls</h2> <h2>Evolution Controls</h2>
<label for="food-prod-prob" title='The probability that a producer cell will produce food each tick.'>Probability of producing food:</label> <label for="food-prod-prob" title='The probability that a producer cell will produce food each tick.'>Probability of producing food:</label>
<input type="number" id="food-prod-prob" min=".001" max="100" value=4 step="1"> <input type="number" id="food-prod-prob" min=".001" max="100" value=4 step="1">
<br> <br>
<label for="lifespan-multiplier" title='An organism lives for this many ticks per cell in its body.'>Lifespan multiplier:</label> <label for="lifespan-multiplier" title='An organism lives for this many ticks per cell in its body.'>Lifespan multiplier:</label>
<input type="number" id="lifespan-multiplier" min="1" max="10000" value=100 step="1"> <input type="number" id="lifespan-multiplier" min="1" max="10000" value=100 step="1">
<br> <br>
<label for="mover-rot" title='Movers rotate randomly when they change directions.'>Movers can rotate</label> <label for="rot-enabled" title='Organisms rotate when born and while moving.'>Rotation Enabled</label>
<input type="checkbox" id="mover-rot" checked> <input type="checkbox" id="rot-enabled" checked>
<br>
<label for="offspring-rot" title='Offspring will randomly rotate'>Offspring rotate</label>
<input type="checkbox" id="offspring-rot" checked>
<br> <br>
<label for="insta-kill" title='When on, killer cells immediatly kill organisms they touch. When off, organisms have as much health as they have cells and only take 1 damage from killer cells.'>One touch kill</label> <label for="insta-kill" title='When on, killer cells immediatly kill organisms they touch. When off, organisms have as much health as they have cells and only take 1 damage from killer cells.'>One touch kill</label>
<input type="checkbox" id="insta-kill"> <input type="checkbox" id="insta-kill">
@@ -198,6 +209,9 @@
<br> <br>
<label for="food-drop-rate" title='Rate at which food is automatically generated and dropped in the world'>Auto food drop rate:</label> <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"> <input type="number" id="food-drop-rate" value=0 max="1000">
<br>
<label for="extra-mover-cost" title='Additional food cost for movers to reproduce'>Extra mover reproduction cost:</label>
<input type="number" id="extra-mover-cost" value=0 max="1000" step="1">
</div> </div>
<div class='right-half'> <div class='right-half'>
@@ -221,7 +235,10 @@
<label for="food-blocks" title='When on, reproduction will fail if offspring intersect with food. When off, offspring will remove blocking food.'>Food blocks reproduction</label> <label for="food-blocks" title='When on, reproduction will fail if offspring intersect with food. When off, offspring will remove blocking food.'>Food blocks reproduction</label>
<input type="checkbox" id="food-blocks" checked> <input type="checkbox" id="food-blocks" checked>
<br> <br>
<button id='save-controls' title="Save all Evolution Controls in a .json file">Save & Download</button>
<button id='load-controls' title="Load saved Evolution Controls with a .json file">Load</button>
<a id="download-el" style="display: none;"></a>
<input id="upload-el" style="display: none;" type="file">
</div> </div>
</div> </div>
<div id='stats' class='tab'> <div id='stats' class='tab'>
@@ -246,22 +263,6 @@
</div> </div>
</div> </div>
<div id='challenges' class='tab'>
<div class='left-half'>
<h2>Challenges</h2>
<button class='challenge-btn' value='Without editing any organisms, evolve a static producer organism with 15 cells. '>Megaflora</button>
<button class='challenge-btn' value='Without editing any organisms, evolve a mover organism with 10 cells.'>Megafauna</button>
<button class='challenge-btn' value='Use the editor to create a new organism that dominates the current ecosystem and causes many species to go extinct.'>Invasive Species</button>
<button class='challenge-btn' value='Either through editing or evolving, create two different species that have a mutually beneficial relationship.'>Symbiosis</button>
<button class='challenge-btn' value='Evolve a stable ecosystem with 10 significantly different species.'>Biodiversity</button>
<button class='challenge-btn' value='Evolve a stable ecosystem without any killer cells.'>Utopia</button>
</div>
<div class='right-half'>
<h2 id='challenge-title'>Select a Challenge</h2>
<br>
<p id='challenge-description'>Challenge yourself to create interesting ecosystems and organisms. There is no formal way to win or lose, its just for fun! </p>
</div>
</div>
</div> </div>
</div> </div>
<div class='hot-controls'> <div class='hot-controls'>

View File

@@ -6,7 +6,7 @@ const Modes = {
Select: 4, Select: 4,
Edit: 5, Edit: 5,
Clone: 6, Clone: 6,
Drag: 7 Drag: 7,
} }
module.exports = Modes; module.exports = Modes;

View File

@@ -1,6 +1,8 @@
const Hyperparams = require("../Hyperparameters"); const Hyperparams = require("../Hyperparameters");
const Modes = require("./ControlModes"); const Modes = require("./ControlModes");
const StatsPanel = require("../Stats/StatsPanel"); const StatsPanel = require("../Stats/StatsPanel");
const RandomOrganismGenerator = require("../Organism/RandomOrganismGenerator")
const WorldConfig = require("../WorldConfig");
class ControlPanel { class ControlPanel {
constructor(engine) { constructor(engine) {
@@ -8,11 +10,10 @@ class ControlPanel {
this.defineMinMaxControls(); this.defineMinMaxControls();
this.defineHotkeys(); this.defineHotkeys();
this.defineEngineSpeedControls(); this.defineEngineSpeedControls();
this.defineGridSizeControls();
this.defineTabNavigation(); this.defineTabNavigation();
this.defineHyperparameterControls(); this.defineHyperparameterControls();
this.defineWorldControls();
this.defineModeControls(); this.defineModeControls();
this.defineChallenges();
this.fps = engine.fps; this.fps = engine.fps;
this.organism_record=0; this.organism_record=0;
this.env_controller = this.engine.env.controller; this.env_controller = this.engine.env.controller;
@@ -114,38 +115,57 @@ class ControlPanel {
defineEngineSpeedControls(){ defineEngineSpeedControls(){
this.slider = document.getElementById("slider"); this.slider = document.getElementById("slider");
this.slider.oninput = function() { this.slider.oninput = function() {
this.fps = this.slider.value const max_fps = 300;
this.fps = this.slider.value;
if (this.fps>=max_fps) this.fps = 1000;
if (this.engine.running) { if (this.engine.running) {
this.changeEngineSpeed(this.fps); this.changeEngineSpeed(this.fps);
} }
$('#fps').text("Target FPS: "+this.fps); let text = this.fps >= max_fps ? 'MAX' : this.fps;
$('#fps').text("Target FPS: "+text);
}.bind(this); }.bind(this);
$('.pause-button').click(function() { $('.pause-button').click(function() {
$('.pause-button').find("i").toggleClass("fa fa-pause"); // toggle pause
$('.pause-button').find("i").toggleClass("fa fa-play"); this.setPaused(this.engine.running);
this.paused = !this.paused;
if (this.engine.running) {
this.engine.stop();
}
else if (!this.engine.running){
this.engine.start(this.fps);
}
}.bind(this)); }.bind(this));
$('.headless').click(function() { $('.headless').click(function() {
$('.headless').find("i").toggleClass("fa fa-eye"); $('.headless').find("i").toggleClass("fa fa-eye");
$('.headless').find("i").toggleClass("fa fa-eye-slash"); $('.headless').find("i").toggleClass("fa fa-eye-slash");
if (Hyperparams.headless){ if (WorldConfig.headless){
$('#headless-notification').css('display', 'none'); $('#headless-notification').css('display', 'none');
this.engine.env.renderFull(); this.engine.env.renderFull();
} }
else { else {
$('#headless-notification').css('display', 'block'); $('#headless-notification').css('display', 'block');
} }
Hyperparams.headless = !Hyperparams.headless; WorldConfig.headless = !WorldConfig.headless;
}.bind(this)); }.bind(this));
} }
defineGridSizeControls() { defineTabNavigation() {
this.tab_id = 'about';
var self = this;
$('.tabnav-item').click(function() {
$('.tab').css('display', 'none');
var tab = '#'+this.id+'.tab';
$(tab).css('display', 'grid');
$('.tabnav-item').removeClass('open-tab')
$('#'+this.id+'.tabnav-item').addClass('open-tab');
self.engine.organism_editor.is_active = (this.id == 'editor');
self.stats_panel.stopAutoRender();
if (this.id === 'stats') {
self.stats_panel.startAutoRender();
}
else if (this.id === 'editor') {
self.editor_controller.refreshDetailsPanel();
}
self.tab_id = this.id;
});
}
defineWorldControls() {
$('#fill-window').change(function() { $('#fill-window').change(function() {
if (this.checked) if (this.checked)
$('.col-row-input').css('display' ,'none'); $('.col-row-input').css('display' ,'none');
@@ -168,24 +188,23 @@ class ControlPanel {
this.stats_panel.reset(); this.stats_panel.reset();
}.bind(this)); }.bind(this));
}
defineTabNavigation() { $('#auto-reset').change(function() {
this.tab_id = 'about'; WorldConfig.auto_reset = this.checked;
var self = this; });
$('.tabnav-item').click(function() { $('#auto-pause').change(function() {
$('.tab').css('display', 'none'); WorldConfig.auto_pause = this.checked;
var tab = '#'+this.id+'.tab'; });
$(tab).css('display', 'grid'); $('#clear-walls-reset').change(function() {
self.engine.organism_editor.is_active = (this.id == 'editor'); WorldConfig.clear_walls_on_reset = this.checked;
self.stats_panel.stopAutoRender(); });
if (this.id === 'stats') { $('#reset-with-editor-org').click( () => {
self.stats_panel.startAutoRender(); let env = this.engine.env;
} if (!env.reset(true, false)) return;
else if (this.id === 'editor') { let center = env.grid_map.getCenter();
self.editor_controller.refreshDetailsPanel(); let org = this.editor_controller.env.getCopyOfOrg();
} this.env_controller.add_new_species = true;
self.tab_id = this.id; this.env_controller.dropOrganism(org, center[0], center[1])
}); });
} }
@@ -197,11 +216,8 @@ class ControlPanel {
Hyperparams.lifespanMultiplier = $('#lifespan-multiplier').val(); Hyperparams.lifespanMultiplier = $('#lifespan-multiplier').val();
}.bind(this)); }.bind(this));
$('#mover-rot').change(function() { $('#rot-enabled').change(function() {
Hyperparams.moversCanRotate = this.checked; Hyperparams.rotationEnabled = this.checked;
});
$('#offspring-rot').change(function() {
Hyperparams.offspringRotate = this.checked;
}); });
$('#insta-kill').change(function() { $('#insta-kill').change(function() {
Hyperparams.instaKill = this.checked; Hyperparams.instaKill = this.checked;
@@ -212,6 +228,9 @@ class ControlPanel {
$('#food-drop-rate').change(function() { $('#food-drop-rate').change(function() {
Hyperparams.foodDropProb = $('#food-drop-rate').val(); Hyperparams.foodDropProb = $('#food-drop-rate').val();
}); });
$('#extra-mover-cost').change(function() {
Hyperparams.extraMoverFoodCost = parseInt($('#extra-mover-cost').val());
});
$('#evolved-mutation').change( function() { $('#evolved-mutation').change( function() {
if (this.checked) { if (this.checked) {
@@ -252,14 +271,40 @@ class ControlPanel {
$('#reset-rules').click(() => { $('#reset-rules').click(() => {
this.setHyperparamDefaults(); this.setHyperparamDefaults();
}); });
$('#save-controls').click(() => {
let data = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(Hyperparams));
let downloadEl = document.getElementById('download-el');
downloadEl.setAttribute("href", data);
downloadEl.setAttribute("download", "controls.json");
downloadEl.click();
});
$('#load-controls').click(() => {
$('#upload-el').click();
});
$('#upload-el').change((e)=>{
let files = e.target.files;
if (!files.length) {return;};
let reader = new FileReader();
reader.onload = (e) => {
let result=JSON.parse(e.target.result);
Hyperparams.loadJsonObj(result);
this.updateHyperparamUIValues();
// have to clear the value so change() will be triggered if the same file is uploaded again
$('#upload-el')[0].value = '';
};
reader.readAsText(files[0]);
});
} }
setHyperparamDefaults() { setHyperparamDefaults() {
Hyperparams.setDefaults(); Hyperparams.setDefaults();
this.updateHyperparamUIValues();
}
updateHyperparamUIValues(){
$('#food-prod-prob').val(Hyperparams.foodProdProb); $('#food-prod-prob').val(Hyperparams.foodProdProb);
$('#lifespan-multiplier').val(Hyperparams.lifespanMultiplier); $('#lifespan-multiplier').val(Hyperparams.lifespanMultiplier);
$('#mover-rot').prop('checked', Hyperparams.moversCanRotate); $('#rot-enabled').prop('checked', Hyperparams.rotationEnabled);
$('#offspring-rot').prop('checked', Hyperparams.offspringRotate);
$('#insta-kill').prop('checked', Hyperparams.instaKill); $('#insta-kill').prop('checked', Hyperparams.instaKill);
$('#evolved-mutation').prop('checked', !Hyperparams.useGlobalMutability); $('#evolved-mutation').prop('checked', !Hyperparams.useGlobalMutability);
$('#add-prob').val(Hyperparams.addProb); $('#add-prob').val(Hyperparams.addProb);
@@ -268,6 +313,7 @@ class ControlPanel {
$('#movers-produce').prop('checked', Hyperparams.moversCanProduce); $('#movers-produce').prop('checked', Hyperparams.moversCanProduce);
$('#food-blocks').prop('checked', Hyperparams.foodBlocksReproduction); $('#food-blocks').prop('checked', Hyperparams.foodBlocksReproduction);
$('#food-drop-rate').val(Hyperparams.foodDropProb); $('#food-drop-rate').val(Hyperparams.foodDropProb);
$('#extra-mover-cost').val(Hyperparams.extraMoverFoodCost);
$('#look-range').val(Hyperparams.lookRange); $('#look-range').val(Hyperparams.lookRange);
if (!Hyperparams.useGlobalMutability) { if (!Hyperparams.useGlobalMutability) {
@@ -311,7 +357,6 @@ class ControlPanel {
$('.edit-mode-btn').removeClass('selected'); $('.edit-mode-btn').removeClass('selected');
$('.'+this.id).addClass('selected'); $('.'+this.id).addClass('selected');
}); });
$('.reset-view').click( function(){ $('.reset-view').click( function(){
this.env_controller.resetView(); this.env_controller.resetView();
}.bind(this)); }.bind(this));
@@ -324,19 +369,24 @@ class ControlPanel {
$('#clear-env').click( () => { $('#clear-env').click( () => {
env.reset(true, false); env.reset(true, false);
this.stats_panel.reset(); this.stats_panel.reset();
env.auto_reset = false;
$('#auto-reset').prop('checked', false);;
});
$('#auto-reset').change(function() {
env.auto_reset = this.checked;
}); });
$('#random-walls').click( function() {
this.env_controller.randomizeWalls();
}.bind(this));
$('#clear-walls').click( function() { $('#clear-walls').click( function() {
this.engine.env.clearWalls(); this.engine.env.clearWalls();
}.bind(this)); }.bind(this));
$('#clear-editor').click( function() { $('#clear-editor').click( function() {
this.engine.organism_editor.clear(); this.engine.organism_editor.clear();
this.editor_controller.setEditorPanel(); this.editor_controller.setEditorPanel();
}.bind(this)) }.bind(this));
$('#generate-random').click( function() {
this.engine.organism_editor.createRandom();
this.editor_controller.refreshDetailsPanel();
}.bind(this));
$('.reset-random').click( function() {
this.engine.organism_editor.resetWithRandomOrgs(this.engine.env);
}.bind(this));
window.onbeforeunload = function (e) { window.onbeforeunload = function (e) {
e = e || window.event; e = e || window.event;
@@ -348,11 +398,22 @@ class ControlPanel {
}; };
} }
defineChallenges() { setPaused(paused) {
$('.challenge-btn').click(function() {
$('#challenge-title').text($(this).text()); if (paused) {
$('#challenge-description').text($(this).val());
}); $('.pause-button').find("i").removeClass("fa-pause");
$('.pause-button').find("i").addClass("fa-play");
if (this.engine.running)
this.engine.stop();
}
else if (!paused) {
$('.pause-button').find("i").addClass("fa-pause");
$('.pause-button').find("i").removeClass("fa-play");
if (!this.engine.running)
this.engine.start(this.fps);
}
} }
setMode(mode) { setMode(mode) {
@@ -383,7 +444,7 @@ class ControlPanel {
} }
updateHeadlessIcon(delta_time) { updateHeadlessIcon(delta_time) {
if (this.paused) if (!this.engine.running)
return; return;
const min_opacity = 0.4; const min_opacity = 0.4;
var op = this.headless_opacity + (this.opacity_change_rate*delta_time/1000); var op = this.headless_opacity + (this.opacity_change_rate*delta_time/1000);
@@ -403,7 +464,7 @@ class ControlPanel {
$('#fps-actual').text("Actual FPS: " + Math.floor(this.engine.actual_fps)); $('#fps-actual').text("Actual FPS: " + Math.floor(this.engine.actual_fps));
$('#reset-count').text("Auto reset count: " + this.engine.env.reset_count); $('#reset-count').text("Auto reset count: " + this.engine.env.reset_count);
this.stats_panel.updateDetails(); this.stats_panel.updateDetails();
if (Hyperparams.headless) if (WorldConfig.headless)
this.updateHeadlessIcon(delta_time); this.updateHeadlessIcon(delta_time);
} }

View File

@@ -112,6 +112,7 @@ class EditorController extends CanvasController{
clearDetailsPanel() { clearDetailsPanel() {
$('#organism-details').css('display', 'none'); $('#organism-details').css('display', 'none');
$('#edit-organism-details').css('display', 'none'); $('#edit-organism-details').css('display', 'none');
$('#randomize-organism-details').css('display', 'none');
} }
refreshDetailsPanel() { refreshDetailsPanel() {
@@ -213,6 +214,11 @@ class EditorController extends CanvasController{
var reaction = this.env.organism.brain.decisions[name]; var reaction = this.env.organism.brain.decisions[name];
$('#reaction-edit').val(reaction); $('#reaction-edit').val(reaction);
} }
setRandomizePanel() {
this.clearDetailsPanel();
$('#randomize-organism-details').css('display', 'block');
}
} }
module.exports = EditorController; module.exports = EditorController;

View File

@@ -4,7 +4,8 @@ const Modes = require("./ControlModes");
const CellStates = require("../Organism/Cell/CellStates"); const CellStates = require("../Organism/Cell/CellStates");
const Neighbors = require("../Grid/Neighbors"); const Neighbors = require("../Grid/Neighbors");
const FossilRecord = require("../Stats/FossilRecord"); const FossilRecord = require("../Stats/FossilRecord");
const Hyperparams = require("../Hyperparameters"); const WorldConfig = require("../WorldConfig");
const Perlin = require("../Utils/Perlin");
class EnvironmentController extends CanvasController{ class EnvironmentController extends CanvasController{
constructor(env, canvas) { constructor(env, canvas) {
@@ -51,8 +52,34 @@ class EnvironmentController extends CanvasController{
this.scale = 1; this.scale = 1;
} }
/*
Iterate over grid from 0,0 to env.num_cols,env.num_rows and create random walls using perlin noise to create a more organic shape.
*/
randomizeWalls(thickness=1) {
this.env.clearWalls();
const noise_threshold = -0.017;
let avg_noise = 0;
let resolution = 20;
Perlin.seed();
for (let r = 0; r < this.env.num_rows; r++) {
for (let c = 0; c < this.env.num_cols; c++) {
let xval = c/this.env.num_cols*(resolution/this.env.renderer.cell_size*(this.env.num_cols/this.env.num_rows));
let yval = r/this.env.num_rows*(resolution/this.env.renderer.cell_size*(this.env.num_rows/this.env.num_cols));
let noise = Perlin.get(xval, yval);
avg_noise += noise/(this.env.num_rows*this.env.num_cols);
if (noise > noise_threshold && noise < noise_threshold + thickness/resolution) {
let cell = this.env.grid_map.cellAt(c, r);
if (cell != null) {
if(cell.owner != null) cell.owner.die();
this.env.changeCell(c, r, CellStates.wall, null);
}
}
}
}
}
updateMouseLocation(offsetX, offsetY){ updateMouseLocation(offsetX, offsetY){
super.updateMouseLocation(offsetX, offsetY); super.updateMouseLocation(offsetX, offsetY);
} }
@@ -71,7 +98,7 @@ class EnvironmentController extends CanvasController{
} }
performModeAction() { performModeAction() {
if (Hyperparams.headless) if (WorldConfig.headless && this.mode != Modes.Drag)
return; return;
var mode = this.mode; var mode = this.mode;
var right_click = this.right_click; var right_click = this.right_click;
@@ -114,21 +141,7 @@ class EnvironmentController extends CanvasController{
case Modes.Clone: case Modes.Clone:
if (this.org_to_clone != null){ if (this.org_to_clone != null){
var new_org = new Organism(this.mouse_c, this.mouse_r, this.env, this.org_to_clone); this.dropOrganism(this.org_to_clone, this.mouse_c, this.mouse_r);
if (this.add_new_species){
FossilRecord.addSpeciesObj(new_org.species);
new_org.species.start_tick = this.env.total_ticks;
this.add_new_species = false;
new_org.species.population = 0;
}
else if (this.org_to_clone.species.extinct){
FossilRecord.resurrect(this.org_to_clone.species);
}
if (new_org.isClear(this.mouse_c, this.mouse_r)){
this.env.addOrganism(new_org);
new_org.species.addPop();
}
} }
break; break;
case Modes.Drag: case Modes.Drag:
@@ -152,6 +165,29 @@ class EnvironmentController extends CanvasController{
} }
} }
dropOrganism(organism, col, row) {
// close the organism and drop it in the world
var new_org = new Organism(col, row, this.env, organism);
if (new_org.isClear(col, row)) {
if (this.add_new_species){
FossilRecord.addSpeciesObj(new_org.species);
new_org.species.start_tick = this.env.total_ticks;
this.add_new_species = false;
new_org.species.population = 0;
}
else if (this.org_to_clone.species.extinct){
FossilRecord.resurrect(this.org_to_clone.species);
}
this.env.addOrganism(new_org);
new_org.species.addPop();
return true;
}
return false;
}
dropCellType(col, row, state, killBlocking=false) { dropCellType(col, row, state, killBlocking=false) {
for (var loc of Neighbors.allSelf){ for (var loc of Neighbors.allSelf){
var c=col + loc[0]; var c=col + loc[0];

View File

@@ -5,6 +5,7 @@ const Renderer = require('../Rendering/Renderer');
const CellStates = require('../Organism/Cell/CellStates'); const CellStates = require('../Organism/Cell/CellStates');
const EditorController = require("../Controllers/EditorController"); const EditorController = require("../Controllers/EditorController");
const Species = require('../Stats/Species'); const Species = require('../Stats/Species');
const RandomOrganismGenerator = require('../Organism/RandomOrganismGenerator')
class OrganismEditor extends Environment{ class OrganismEditor extends Environment{
constructor() { constructor() {
@@ -87,6 +88,31 @@ class OrganismEditor extends Environment{
this.organism.updateGrid(); this.organism.updateGrid();
this.organism.species = new Species(this.organism.anatomy, null, 0); this.organism.species = new Species(this.organism.anatomy, null, 0);
} }
createRandom() {
this.grid_map.fillGrid(CellStates.empty);
this.organism = RandomOrganismGenerator.generate(this);
this.organism.updateGrid();
this.organism.species = new Species(this.organism.anatomy, null, 0);
}
resetWithRandomOrgs(env) {
let reset_confirmed = env.reset(true, false);
if (!reset_confirmed) return;
let numOrganisms = parseInt($('#num-random-orgs').val());
let size = Math.ceil(8);
for (let i=0; i<numOrganisms; i++) {
let newOrganism = RandomOrganismGenerator.generate(this);
newOrganism.species = new Species(newOrganism.anatomy, null, 0);
var col = Math.floor(size + (Math.random() * (env.grid_map.cols-(size*2)) ) );
var row = Math.floor(size + (Math.random() * (env.grid_map.rows-(size*2)) ) );
env.controller.add_new_species = true;
env.controller.dropOrganism(newOrganism, col, row);
}
}
} }
module.exports = OrganismEditor; module.exports = OrganismEditor;

View File

@@ -6,19 +6,19 @@ const CellStates = require('../Organism/Cell/CellStates');
const EnvironmentController = require('../Controllers/EnvironmentController'); const EnvironmentController = require('../Controllers/EnvironmentController');
const Hyperparams = require('../Hyperparameters.js'); const Hyperparams = require('../Hyperparameters.js');
const FossilRecord = require('../Stats/FossilRecord'); const FossilRecord = require('../Stats/FossilRecord');
const WorldConfig = require('../WorldConfig');
class WorldEnvironment extends Environment{ class WorldEnvironment extends Environment{
constructor(cell_size) { constructor(cell_size) {
super(); super();
this.renderer = new Renderer('env-canvas', 'env', cell_size); this.renderer = new Renderer('env-canvas', 'env', cell_size);
this.controller = new EnvironmentController(this, this.renderer.canvas); this.controller = new EnvironmentController(this, this.renderer.canvas);
var grid_rows = Math.ceil(this.renderer.height / cell_size); this.num_rows = Math.ceil(this.renderer.height / cell_size);
var grid_cols = Math.ceil(this.renderer.width / cell_size); this.num_cols = Math.ceil(this.renderer.width / cell_size);
this.grid_map = new GridMap(grid_cols, grid_rows, cell_size); this.grid_map = new GridMap(this.num_cols, this.num_rows, cell_size);
this.organisms = []; this.organisms = [];
this.walls = []; this.walls = [];
this.total_mutability = 0; this.total_mutability = 0;
this.auto_reset = true;
this.largest_cell_count = 0; this.largest_cell_count = 0;
this.reset_count = 0; this.reset_count = 0;
this.total_ticks = 0; this.total_ticks = 0;
@@ -45,7 +45,7 @@ class WorldEnvironment extends Environment{
} }
render() { render() {
if (Hyperparams.headless) { if (WorldConfig.headless) {
this.renderer.cells_to_render.clear(); this.renderer.cells_to_render.clear();
return; return;
} }
@@ -58,13 +58,18 @@ class WorldEnvironment extends Environment{
} }
removeOrganisms(org_indeces) { removeOrganisms(org_indeces) {
let start_pop = this.organisms.length;
for (var i of org_indeces.reverse()){ for (var i of org_indeces.reverse()){
this.total_mutability -= this.organisms[i].mutability; this.total_mutability -= this.organisms[i].mutability;
this.organisms.splice(i, 1); this.organisms.splice(i, 1);
} }
if (this.organisms.length == 0 && this.auto_reset){ if (this.organisms.length === 0 && start_pop > 0) {
this.reset_count++; if (WorldConfig.auto_pause)
this.reset(false); $('.pause-button')[0].click();
else if(WorldConfig.auto_reset) {
this.reset_count++;
this.reset(false);
}
} }
} }
@@ -104,7 +109,8 @@ class WorldEnvironment extends Environment{
clearWalls() { clearWalls() {
for(var wall of this.walls){ for(var wall of this.walls){
if (this.grid_map.cellAt(wall.col, wall.row).state == CellStates.wall) let wcell = this.grid_map.cellAt(wall.col, wall.row);
if (wcell && wcell.state == CellStates.wall)
this.changeCell(wall.col, wall.row, CellStates.empty, null); this.changeCell(wall.col, wall.row, CellStates.empty, null);
} }
} }
@@ -132,17 +138,17 @@ class WorldEnvironment extends Environment{
reset(confirm_reset=true, reset_life=true) { reset(confirm_reset=true, reset_life=true) {
if (confirm_reset && !confirm('The current environment will be lost. Proceed?')) if (confirm_reset && !confirm('The current environment will be lost. Proceed?'))
return; return false;
this.organisms = []; this.organisms = [];
this.grid_map.fillGrid(CellStates.empty); this.grid_map.fillGrid(CellStates.empty, !WorldConfig.clear_walls_on_reset);
this.renderer.renderFullGrid(this.grid_map.grid); this.renderer.renderFullGrid(this.grid_map.grid);
this.total_mutability = 0; this.total_mutability = 0;
this.total_ticks = 0; this.total_ticks = 0;
FossilRecord.clear_record(); FossilRecord.clear_record();
if (reset_life) if (reset_life)
this.OriginOfLife(); this.OriginOfLife();
return true;
} }
resizeGridColRow(cell_size, cols, rows) { resizeGridColRow(cell_size, cols, rows) {
@@ -154,9 +160,9 @@ class WorldEnvironment extends Environment{
resizeFillWindow(cell_size) { resizeFillWindow(cell_size) {
this.renderer.cell_size = cell_size; this.renderer.cell_size = cell_size;
this.renderer.fillWindow('env'); this.renderer.fillWindow('env');
var cols = Math.ceil(this.renderer.width / cell_size); this.num_cols = Math.ceil(this.renderer.width / cell_size);
var rows = Math.ceil(this.renderer.height / cell_size); this.num_rows = Math.ceil(this.renderer.height / cell_size);
this.grid_map.resize(cols, rows, cell_size); this.grid_map.resize(this.num_cols, this.num_rows, cell_size);
} }
} }

View File

@@ -21,9 +21,10 @@ class GridMap {
} }
} }
fillGrid(state) { fillGrid(state, ignore_walls=false) {
for (var col of this.grid) { for (var col of this.grid) {
for (var cell of col) { for (var cell of col) {
if (ignore_walls && cell.state===CellStates.wall) continue;
cell.setType(state); cell.setType(state);
cell.owner = null; cell.owner = null;
cell.cell_owner = null; cell.cell_owner = null;

View File

@@ -2,8 +2,6 @@ const Neighbors = require("./Grid/Neighbors");
const Hyperparams = { const Hyperparams = {
setDefaults: function() { setDefaults: function() {
this.headless = false;
this.lifespanMultiplier = 100; this.lifespanMultiplier = 100;
this.foodProdProb = 5; this.foodProdProb = 5;
this.killableNeighbors = Neighbors.adjacent; this.killableNeighbors = Neighbors.adjacent;
@@ -16,8 +14,7 @@ const Hyperparams = {
this.changeProb = 33; this.changeProb = 33;
this.removeProb = 33; this.removeProb = 33;
this.moversCanRotate = true; this.rotationEnabled = true;
this.offspringRotate = true;
this.foodBlocksReproduction = true; this.foodBlocksReproduction = true;
this.moversCanProduce = false; this.moversCanProduce = false;
@@ -27,7 +24,15 @@ const Hyperparams = {
this.lookRange = 20; this.lookRange = 20;
this.foodDropProb = 0; this.foodDropProb = 0;
this.extraMoverFoodCost = 0;
}, },
loadJsonObj(obj) {
for (let key in obj) {
this[key] = obj[key];
}
}
} }
Hyperparams.setDefaults(); Hyperparams.setDefaults();

View File

@@ -91,6 +91,22 @@ class Anatomy {
getRandomCell() { getRandomCell() {
return this.cells[Math.floor(Math.random() * this.cells.length)]; return this.cells[Math.floor(Math.random() * this.cells.length)];
} }
getNeighborsOfCell(col, row) {
var neighbors = [];
for (var x = -1; x <= 1; x++) {
for (var y = -1; y <= 1; y++) {
var neighbor = this.getLocalCell(col + x, row + y);
if (neighbor)
neighbors.push(neighbor)
}
}
return neighbors;
}
} }
module.exports = Anatomy; module.exports = Anatomy;

View File

@@ -95,6 +95,7 @@ const CellStates = {
return this.living[Math.floor(Math.random() * this.living.length)]; return this.living[Math.floor(Math.random() * this.living.length)];
} }
} }
CellStates.defineLists(); CellStates.defineLists();
module.exports = CellStates; module.exports = CellStates;

View File

@@ -5,7 +5,7 @@ const Hyperparams = require("../../Hyperparameters");
class Cell{ class Cell{
constructor(state, col, row, x, y){ constructor(state, col, row, x, y){
this.owner = null; // owner organism this.owner = null; // owner organism
this.cell_owner = null; // owner cell of ^that organism this.cell_owner = null; // specific body cell of the owner organism that occupies this grid cell
this.setType(state); this.setType(state);
this.col = col; this.col = col;
this.row = row; this.row = row;

View File

@@ -17,7 +17,7 @@ class Organism {
this.anatomy = new Anatomy(this) this.anatomy = new Anatomy(this)
this.direction = Directions.down; // direction of movement this.direction = Directions.down; // direction of movement
this.rotation = Directions.up; // direction of rotation this.rotation = Directions.up; // direction of rotation
this.can_rotate = Hyperparams.moversCanRotate; this.can_rotate = Hyperparams.rotationEnabled;
this.move_count = 0; this.move_count = 0;
this.move_range = 4; this.move_range = 4;
this.ignore_brain_for = 0; this.ignore_brain_for = 0;
@@ -47,11 +47,10 @@ class Organism {
// amount of food required before it can reproduce // amount of food required before it can reproduce
foodNeeded() { foodNeeded() {
return this.anatomy.cells.length; return this.anatomy.is_mover ? this.anatomy.cells.length + Hyperparams.extraMoverFoodCost : this.anatomy.cells.length;
} }
lifespan() { lifespan() {
// console.log(Hyperparams.lifespanMultiplier)
return this.anatomy.cells.length * Hyperparams.lifespanMultiplier; return this.anatomy.cells.length * Hyperparams.lifespanMultiplier;
} }
@@ -63,7 +62,7 @@ class Organism {
//produce mutated child //produce mutated child
//check nearby locations (is there room and a direct path) //check nearby locations (is there room and a direct path)
var org = new Organism(0, 0, this.env, this); var org = new Organism(0, 0, this.env, this);
if(Hyperparams.offspringRotate){ if(Hyperparams.rotationEnabled){
org.rotation = Directions.getRandomDirection(); org.rotation = Directions.getRandomDirection();
} }
var prob = this.mutability; var prob = this.mutability;
@@ -118,7 +117,7 @@ class Organism {
org.species.addPop(); org.species.addPop();
} }
} }
this.food_collected -= this.foodNeeded(); Math.max(this.food_collected -= this.foodNeeded(), 0);
} }
@@ -237,14 +236,13 @@ class Organism {
return cell != null && (cell.state == CellStates.empty || cell.owner == this || cell.owner == parent || cell.state == CellStates.food); return cell != null && (cell.state == CellStates.empty || cell.owner == this || cell.owner == parent || cell.state == CellStates.food);
} }
isClear(col, row, rotation=this.rotation, ignore_armor=false) { isClear(col, row, rotation=this.rotation) {
for(var loccell of this.anatomy.cells) { for(var loccell of this.anatomy.cells) {
var cell = this.getRealCell(loccell, col, row, rotation); var cell = this.getRealCell(loccell, col, row, rotation);
if (cell==null) { if (cell==null) {
return false; return false;
} }
// console.log(cell.owner == this) if (cell.owner==this || cell.state==CellStates.empty || (!Hyperparams.foodBlocksReproduction && cell.state==CellStates.food)){
if (cell.owner==this || cell.state==CellStates.empty || (!Hyperparams.foodBlocksReproduction && cell.state==CellStates.food) || (ignore_armor && loccell.state==CellStates.armor && cell.state==CellStates.food)){
continue; continue;
} }
return false; return false;

View File

@@ -32,8 +32,12 @@ class Brain {
this.decisions[CellStates.eye.name] = Decision.neutral; this.decisions[CellStates.eye.name] = Decision.neutral;
} }
randomizeDecisions() { randomizeDecisions(randomize_all=false) {
// randomize the non obvious decisions // randomize the non obvious decisions
if (randomize_all) {
this.decisions[CellStates.food.name] = Decision.getRandom();
this.decisions[CellStates.killer.name] = Decision.getRandom();
}
this.decisions[CellStates.mouth.name] = Decision.getRandom(); this.decisions[CellStates.mouth.name] = Decision.getRandom();
this.decisions[CellStates.producer.name] = Decision.getRandom(); this.decisions[CellStates.producer.name] = Decision.getRandom();
this.decisions[CellStates.mover.name] = Decision.getRandom(); this.decisions[CellStates.mover.name] = Decision.getRandom();
@@ -79,4 +83,6 @@ class Brain {
} }
} }
Brain.Decision = Decision;
module.exports = Brain; module.exports = Brain;

View File

@@ -0,0 +1,68 @@
const CellStates = require("./Cell/CellStates");
const Organism = require("./Organism");
const Brain = require("./Perception/Brain")
class RandomOrganismGenerator {
static generate(env) {
var center = env.grid_map.getCenter();
var organism = new Organism(center[0], center[1], env, null);
organism.anatomy.addDefaultCell(CellStates.mouth, 0, 0);
var outermostLayer = RandomOrganismGenerator.organismLayers;
var x, y;
// iterate from center to edge of organism
// layer 0 is the central cell of the organism
for (var layer = 1; layer <= outermostLayer; layer++) {
var someCellSpawned = false;
var spawnChance = RandomOrganismGenerator.cellSpawnChance * 1 - ((layer - 1) / outermostLayer);
// top
y = -layer;
for (x = -layer; x <= layer; x++)
someCellSpawned = RandomOrganismGenerator.trySpawnCell(organism, x, y, spawnChance);
// bottom
y = layer;
for (x = -layer; x <= layer; x++)
someCellSpawned = RandomOrganismGenerator.trySpawnCell(organism, x, y, spawnChance);
// left
x = -layer;
for (y = -layer + 1; y <= layer - 1; y++)
someCellSpawned = RandomOrganismGenerator.trySpawnCell(organism, x, y, spawnChance);
// right
x = layer;
for (y = -layer + 1; y < layer - 1; y++)
someCellSpawned = RandomOrganismGenerator.trySpawnCell(organism, x, y, spawnChance);
if (!someCellSpawned)
break;
}
// randomize the organism's brain
organism.brain.randomizeDecisions(true);
return organism;
}
static trySpawnCell(organism, x, y, spawnChance) {
var neighbors = organism.anatomy.getNeighborsOfCell(x, y);
if (neighbors.length && Math.random() < spawnChance) {
organism.anatomy.addRandomizedCell(CellStates.getRandomLivingType(), x, y);
return true;
}
return false;
}
}
RandomOrganismGenerator.organismLayers = 4;
RandomOrganismGenerator.cellSpawnChance = 0.75;
module.exports = RandomOrganismGenerator;

View File

@@ -45,28 +45,33 @@ class ChartController {
} }
updateData() { updateData() {
var r_len = FossilRecord.tick_record.length; let record_size = FossilRecord.tick_record.length;
var newest_t = -1; let data_points = this.data[0].dataPoints;
var oldest_t = 0; let newest_t = -1;
if (this.data[0].dataPoints.length>0) { if (data_points.length>0) {
newest_t = this.data[0].dataPoints[this.data[0].dataPoints.length-1].x; newest_t = this.data[0].dataPoints[data_points.length-1].x;
newest_t = this.data[0].dataPoints[0].x;
} }
if (newest_t < FossilRecord.tick_record[r_len-1]) { let to_add = 0;
this.addNewest(); let cur_t = FossilRecord.tick_record[record_size-1];
// first count up the number of new datapoints the chart is missing
while (cur_t !== newest_t) {
to_add++;
cur_t = FossilRecord.tick_record[record_size-to_add-1]
} }
if (oldest_t < FossilRecord.tick_record[0]) { // then add them in order
this.addNewest(to_add)
// remove oldest datapoints until the chart is the same size as the saved records
while (data_points.length > FossilRecord.tick_record.length) {
this.removeOldest(); this.removeOldest();
} }
} }
addNewest() { addNewest(to_add) {
var i = FossilRecord.tick_record.length-1; for (let i=to_add; i>0; i--) {
this.addDataPoint(i); let j = FossilRecord.tick_record.length-i;
} this.addDataPoint(j);
}
addDataPoint(i) {
alert("Must override addDataPoint")
} }
removeOldest() { removeOldest() {
@@ -75,6 +80,10 @@ class ChartController {
} }
} }
addDataPoint(i) {
alert("Must override addDataPoint")
}
clear() { clear() {
this.data.length = 0; this.data.length = 0;
this.chart.render(); this.chart.render();

View File

@@ -82,8 +82,7 @@ const FossilRecord = {
this.species_counts.push(this.extant_species.length); this.species_counts.push(this.extant_species.length);
this.av_mut_rates.push(this.env.averageMutability()); this.av_mut_rates.push(this.env.averageMutability());
this.calcCellCountAverages(); this.calcCellCountAverages();
while (this.tick_record.length > this.record_size_limit) {
if (this.tick_record.length > this.record_size_limit) {
this.tick_record.shift(); this.tick_record.shift();
this.pop_counts.shift(); this.pop_counts.shift();
this.species_counts.shift(); this.species_counts.shift();

46
src/Utils/Perlin.js Normal file
View File

@@ -0,0 +1,46 @@
let perlin = {
rand_vect: function(){
let theta = Math.random() * 2 * Math.PI;
return {x: Math.cos(theta), y: Math.sin(theta)};
},
dot_prod_grid: function(x, y, vx, vy){
let g_vect;
let d_vect = {x: x - vx, y: y - vy};
if (this.gradients[[vx,vy]]){
g_vect = this.gradients[[vx,vy]];
} else {
g_vect = this.rand_vect();
this.gradients[[vx, vy]] = g_vect;
}
return d_vect.x * g_vect.x + d_vect.y * g_vect.y;
},
smootherstep: function(x){
return 6*x**5 - 15*x**4 + 10*x**3;
},
interp: function(x, a, b){
return a + this.smootherstep(x) * (b-a);
},
seed: function(){
this.gradients = {};
this.memory = {};
},
get: function(x, y) {
if (this.memory.hasOwnProperty([x,y]))
return this.memory[[x,y]];
let xf = Math.floor(x);
let yf = Math.floor(y);
//interpolate
let tl = this.dot_prod_grid(x, y, xf, yf);
let tr = this.dot_prod_grid(x, y, xf+1, yf);
let bl = this.dot_prod_grid(x, y, xf, yf+1);
let br = this.dot_prod_grid(x, y, xf+1, yf+1);
let xt = this.interp(x-xf, tl, tr);
let xb = this.interp(x-xf, bl, br);
let v = this.interp(y-yf, xt, xb);
this.memory[[x,y]] = v;
return v;
}
}
perlin.seed();
module.exports = perlin;

8
src/WorldConfig.js Normal file
View File

@@ -0,0 +1,8 @@
const WorldConfig = {
headless: false,
clear_walls_on_reset: false,
auto_reset: true,
auto_pause: false,
}
module.exports = WorldConfig;