'use strict'

//let network = require('./network')
let simulation = require('./simulation')
let _ = require('underscore')

let run = () => {
    let time = new Date();
    process.stdout.write('\n');
    let results = simulation.step();
    let duration = ((new Date()) - time)/1000;

    process.stdout.write(render(results) + '\n');

    process.stdout.write('(took ' + duration + 's)\n');
}

let render = (grid) => {
    // grid is [x][y]
    let transposed = _.zip.apply(this, grid)
    let rows = _.map(transposed, (row) => {
	return _.map(row, renderCell).join('')
    }); 

    return rows.join('\n');
}

let renderCell = (cell) => {
    if(cell.plant) {
	let size = cell.plant.size;
	if(size < 1) return '.';	    
	if(size < 3) return ',';
	if(size < 6) return '+';
	if(size < 12) return '*';
	return '#';
    }
    else {
	return ' ';
    }
}

setInterval(run, 1000)

/*
class PutCommand {
    constructor(params) {
	this.object = params.object
	this.coordinates = params.coordinates
    }
    
    apply (world) {
	world.put(this.object, this.coordinates)
    }
}

let commands = {
    'put': PutCommand
}

network.onCommand((commandParams) => {
    let commandKlass = commands[commandParams.name]
    let command = new commandKlass(commandParams)
    
    command.apply(simulation)
})

world.onHistory((records) => {
    network.putHistory(records)
})
*/

@@ -159,17 +159,16 @@
 w Date()
-;
 %0A    pro
@@ -190,17 +190,16 @@
 te('%5Cn')
-;
 %0A    let
@@ -226,17 +226,16 @@
 n.step()
-;
 %0A    let
@@ -272,17 +272,16 @@
 me)/1000
-;
 %0A%0A    pr
@@ -322,17 +322,16 @@
  + '%5Cn')
-;
 %0A%0A    pr
@@ -374,25 +374,24 @@
 on + 's)%5Cn')
-;
 %0A%7D%0A%0Alet rend
@@ -594,17 +594,16 @@
 in('%5Cn')
-;
 %0A%7D%0A%0Alet 
@@ -675,17 +675,16 @@
 ant.size
-;
 %0A%09if(siz
@@ -731,17 +731,16 @@
 turn ','
-;
 %0A%09if(siz
@@ -756,17 +756,16 @@
 turn '+'
-;
 %0A%09if(siz
@@ -782,17 +782,16 @@
 turn '*'
-;
 %0A%09return
@@ -794,17 +794,16 @@
 turn '#'
-;
 %0A    %7D%0A 
@@ -823,17 +823,16 @@
 turn ' '
-;
 %0A    %7D%0A%7D

remove semicolons