/* Copyright (C) 2007 Andre Seidelt, All Rights Reserved. This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. */ import controlP5.*; // settings for the simulation final int WIDTH = 800; final int HEIGHT = 600; final int MUT_RATE = 5; final int MAX_CELLS = 800; final int FOOD_AMMOUNT = 5000; final int BORDER_DIST = 3; final int MIN_COLOR = 50; final int MAX_COLOR = 255; final int MIN_WIDTH = 0; final int MAX_WIDTH = 5; final int MIN_SIZE = 8; final int MAX_SIZE = 20; final int MIN_AGE = 200; final int MAX_AGE = 1000; final int MIN_REP_RATE = 25; final int MAX_REP_RATE = 200; final int MIN_FOOD = 20; final int MAX_FOOD = 100; final int FONT_SIZE = 11; final int ALPHA_VALUE = 200; ////// // no serviceable parts below! ////// // constants for the shapes final int TRIANGLE=0, CIRCLE=1, ELLIPSEH=2, QUADRAT=3, RECTANGULARH=4, ELLIPSEV=5, RECTANGULARV=6; // set containing all cells Set sCells = new HashSet(); // statistics helper class Statistics sStat = new Statistics(); // genome display helper class GenomeDisplay sGenDisplay = new GenomeDisplay(); // cell info helper class CellInfo sInfo = new CellInfo(); // ctlP5 for gui ControlP5 sP5; // font for display PFont sFont; // helper to generate unique generation numbers int sGeneration = 0; // flag which indicates if the simulation is running boolean sRunning = true; // do setup of env void setup() { size(WIDTH, HEIGHT); createGui(); sFont = loadFont("Consolas-11.vlw"); textFont(sFont, FONT_SIZE); frameRate(20); // init world with a single cell new Cell(WIDTH/2, HEIGHT/2); } // live the life of all cells synchronized void draw() { // only count ticks if we are running if(sRunning) { sStat.tick(); } // clear screen background(0); // live all cell-lifes and paint them smooth(); Iterator it = new ArrayList(sCells).iterator(); while(it.hasNext()) { Cell c = (Cell) it.next(); if(sRunning) { c.live(FOOD_AMMOUNT/sCells.size()); } c.paint(); } noSmooth(); // paint all other stuff sInfo.paint(); sStat.paint(); sGenDisplay.paint(); // re-set rect mode for b0rken ctlP5 rectMode(CORNER); } // display cell info or add a new cell at cursor synchronized void mousePressed() { switch(mouseButton) { case RIGHT: new Cell(mouseX, mouseY); break; case LEFT: Cell c = findCell(mouseX, mouseY); sInfo.setCurrent(c); break; } } // generate next generation identifier synchronized int getNextGeneration() { return ++sGeneration; } // find the cell at given cursor position synchronized Cell findCell(int pX, int pY) { Iterator it = sCells.iterator(); while(it.hasNext()) { Cell c = (Cell) it.next(); if(isIn(pX, pY, c)) { return c; } } return null; } // check if the coordinates are in the given cell boolean isIn(int pX, int pY, Cell pCell) { int cellSize = pCell.getSize(); return (pX > pCell.mX-cellSize && pX < pCell.mX+cellSize) && (pY > pCell.mY-cellSize && pY < pCell.mY+cellSize); } // print the given text in multiline mode public void printText(String pText, int pX, int pY) { String lines[] = split(pText, '\n'); for(int i = 0; i < lines.length; i++) { text(lines[i], pX, pY); pY += FONT_SIZE+2; } }