Skip to content

Commit

Permalink
decrese food supply/generation,change timer
Browse files Browse the repository at this point in the history
timer now adapts to the loop execution time to better performance
  • Loading branch information
exi committed Feb 17, 2011
1 parent aa68c8a commit d1e85b3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
31 changes: 16 additions & 15 deletions sim.js
Expand Up @@ -6,6 +6,7 @@ var zoom, camx, camy;

var bots;
var food;
var maxfood = 100;

var fooddelay;
var foodrate;
Expand Down Expand Up @@ -37,6 +38,15 @@ function draw()
ctx.restore();
}

function generateFood() {
fooddelay = foodrate;

var x = Math.random()*worldsize*2-worldsize;
var y = Math.random()*worldsize*2-worldsize;

food.push( new plant( x, y ) );
}

function loop()
{
var update = false;
Expand Down Expand Up @@ -80,15 +90,9 @@ function loop()
if( bots.length < 5 )
bots.push( new neurobot( worldsize*2.0*(Math.random()-0.5), worldsize*2.0*(Math.random()-0.5), Math.PI*2*Math.random() ) );

if( ( fooddelay <= 0 ) && ( food.length < 200 ) )
if( ( fooddelay <= 0 ) && ( food.length < maxfood ) )
{
fooddelay = foodrate;

var ang = Math.PI*2*Math.random();
var dist = Math.random();
dist = dist*foodradius;

food.push( new plant( Math.cos(ang)*dist, Math.sin(ang)*dist ) );
generateFood();
}

if( fooddelay > 0 )
Expand All @@ -115,6 +119,7 @@ function loop()
do_stats();
statdelay = 10;
}
timer = setTimeout(loop, 1);
}

function resetsim()
Expand Down Expand Up @@ -144,13 +149,9 @@ function resetsim()
bots.push( new neurobot( worldsize*2.0*(Math.random()-0.5), worldsize*2.0*(Math.random()-0.5), Math.PI*2*Math.random() ) );

food = new Array();
for( var i=0; i<200; i++ )
for( var i=0; i<maxfood; i++ )
{
var ang = Math.PI*2*Math.random();
var dist = Math.random();
dist = dist*foodradius;

food.push( new plant( Math.cos(ang)*dist, Math.sin(ang)*dist, 150 ) );
generateFood();
}

fooddelay = 0;
Expand Down Expand Up @@ -185,7 +186,7 @@ function init()
statdelay = 0;

running = true;
timer = setInterval( loop, 20 );
loop();
}
}

10 changes: 8 additions & 2 deletions stats.js
Expand Up @@ -50,14 +50,20 @@ function botstats( braindump )
for( var y=0; y<bots[selected].braindepth; y++ )
{
var act = Math.round( bots[selected].brain.neurons[y][x].activation*255.0 );
bctx.fillStyle = "rgb( "+act+", "+act+", "+act+" )";
if( act >= 0)
bctx.fillStyle = "rgb( 0, "+act+", 0 )";
else
bctx.fillStyle = "rgb( "+Math.abs(act)+", 0, 0 )";
bctx.fillRect( x*blockwidth, y*10+10, blockwidth, 10 );
}

blockwidth = Math.floor(canvaswidth/bots[selected].output.length);
for( var x=0; x<bots[selected].output.length; x++ ) {
var act = Math.round( bots[selected].output[x]*255.0 );
bctx.fillStyle = "rgb( "+act+", "+act+", "+act+" )";
if( act >= 0)
bctx.fillStyle = "rgb( 0, "+act+", 0 )";
else
bctx.fillStyle = "rgb( "+Math.abs(act)+", 0, 0 )";
bctx.fillRect( x*blockwidth, 10*bots[selected].braindepth+10, blockwidth, 10 );
}
}
Expand Down

0 comments on commit d1e85b3

Please sign in to comment.