From 2fc2ba7b80dc0cd9ea84e248f357a9dd6b9255a1 Mon Sep 17 00:00:00 2001 From: MaxRobinsonTheGreat Date: Thu, 16 Jul 2020 13:29:17 -0600 Subject: [PATCH] Added rotations, health, damage --- README.md | 10 +- dist/bundle.js | 256 ++++++++++++++++++++++++++++++++++++++++- dist/css/style.css | 21 ++-- dist/html/index.html | 13 ++- src/Cell.js | 10 +- src/CellTypes.js | 2 +- src/ControlPanel.js | 11 +- src/Directions.js | 15 +++ src/Environment.js | 6 +- src/Hyperparameters.js | 5 + src/LocalCell.js | 28 +++++ src/Organism.js | 119 +++++++++++++------ 12 files changed, 434 insertions(+), 62 deletions(-) create mode 100644 src/Directions.js diff --git a/README.md b/README.md index 1622d89..9a78e49 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,18 @@ # EvolutionSimulatorV2 Version 2 of the evolution simulator -Requirements: +Requirements to Play: +- browser (preferably chrome) + +Requirements to edit and build: - npm - webpack +## Playing +Included in this repo is an already built version of the game. You can play it simply by opening `index.html` in your browser. + +## Building +If you want to change the source code use any of the following commands: To build minified: `npm run build` To build in dev mode: `npm run build-dev` To build in dev/watch mode: `npm run build-watch` diff --git a/dist/bundle.js b/dist/bundle.js index 3380b98..4d0508d 100644 --- a/dist/bundle.js +++ b/dist/bundle.js @@ -1 +1,255 @@ -!function(t){var e={};function i(r){if(e[r])return e[r].exports;var s=e[r]={i:r,l:!1,exports:{}};return t[r].call(s.exports,s,s.exports,i),s.l=!0,s.exports}i.m=t,i.c=e,i.d=function(t,e,r){i.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},i.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},i.t=function(t,e){if(1&e&&(t=i(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(i.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var s in t)i.d(r,s,function(e){return t[e]}.bind(null,s));return r},i.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(e,"a",e),e},i.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},i.p="",i(i.s=7)}([function(t,e){const i={empty:0,food:1,wall:2,mouth:3,producer:4,mover:5,killer:6,armor:7,colors:["#121D29","green","gray","orange","white","blue","red","purple"],getRandomLivingType:function(){return Math.floor(5*Math.random())+3}};t.exports=i},function(t,e,i){const r=i(2),s=i(0);t.exports=class{constructor(t,e,i,o=s.empty){this.grid=[],this.cols=t,this.rows=e,this.cell_size=i;for(var l=0;l=0&&e>=0}getCenter(){return[Math.floor(this.cols/2),Math.floor(this.rows/2)]}xyToColRow(t,e){var i=Math.floor(t/this.cell_size),r=Math.floor(e/this.cell_size);return i>=this.cols?i=this.cols-1:i<0&&(i=0),r>=this.rows?r=this.rows-1:r<0&&(r=0),[i,r]}}},function(t,e,i){const r=i(0),s=i(3);function o(t,e,i){null!=e&&e.type==r.food&&(i.changeCell(e.col,e.row,r.empty,null),t.owner.food_collected++)}function l(t,e){if(null!=e&&null!=e.owner&&e.owner!=t.owner&&e.owner.living&&e.type!=r.armor){var i=e.type==r.killer;e.owner.die(),i&&t.owner.die()}}t.exports=class{constructor(t,e,i,r,s){this.owner=null,this.setType(t),this.col=e,this.row=i,this.x=r,this.y=s}setType(t){this.type=t}performFunction(t){switch(this.type){case r.mouth:!function(t,e){for(var i of s.edibleNeighbors){var r=e.grid_map.cellAt(t.col+i[0],t.row+i[1]);o(t,r,e)}}(this,t);break;case r.producer:!function(t,e){if(t.owner.is_mover)return;var i=s.foodProdProb;if(100*Math.random()<=i){var o=s.growableNeighbors[Math.floor(Math.random()*s.growableNeighbors.length)],l=o[0],n=o[1],h=e.grid_map.cellAt(t.col+l,t.row+n);if(null!=h&&h.type==r.empty)e.changeCell(t.col+l,t.row+n,r.food,null)}}(this,t);break;case r.killer:!function(t,e){for(var i of s.killableNeighbors){var r=e.grid_map.cellAt(t.col+i[0],t.row+i[1]);l(t,r)}}(this,t)}}getColor(){return r.colors[this.type]}isLiving(){return this.type!=r.empty&&this.type!=r.food&&this.type!=r.wall}}},function(t,e,i){const r=i(4),s={lifespanMultiplier:100,foodProdProb:4,foodProdProbScalar:4,killableNeighbors:r.adjacent,edibleNeighbors:r.adjacent,growableNeighbors:r.adjacent,useGlobalMutability:!1,globalMutability:0,addProb:33,changeProb:33,removeProb:33,calcProducerFoodRatio:function(t=!0){t?this.foodProdProb=100/this.lifespanMultiplier*this.foodProdProbScalar:this.lifespanMultiplier=Math.floor(100/(this.foodProdProb/this.foodProdProbScalar))},balanceMutationProbs:function(t){if(1==t){var e=100-this.addProb;this.changeProb=e/2,this.removeProb=e/2}else if(2==t){e=100-this.changeProb;this.addProb=e/2,this.removeProb=e/2}else{e=100-this.removeProb;this.changeProb=e/2,this.addProb=e/2}}};t.exports=s},function(t,e){t.exports={all:[[0,1],[0,-1],[1,0],[-1,0],[-1,-1],[1,1],[-1,1],[1,-1]],adjacent:[[0,1],[0,-1],[1,0],[-1,0]],corners:[[-1,-1],[1,1],[-1,1],[1,-1]]}},function(t,e){t.exports={None:0,FoodDrop:1,WallDrop:2,ClickKill:3}},function(t,e,i){const r=i(8),s=i(13);t.exports=class{constructor(){this.fps=60,this.env=new r(5),this.controlpanel=new s(this),this.env.OriginOfLife(),this.last_update=Date.now(),this.delta_time=0,this.actual_fps=0,this.running=!1}start(t=60){t<=0&&(t=1),t>300&&(t=300),this.fps=t,this.game_loop=setInterval(function(){this.update()}.bind(this),1e3/t),this.running=!0,this.fps>=60?null!=this.render_loop&&(clearInterval(this.render_loop),this.render_loop=null):this.setRenderLoop()}stop(){clearInterval(this.game_loop),this.running=!1,this.setRenderLoop()}setRenderLoop(){null==this.render_loop&&(this.render_loop=setInterval(function(){this.env.render(),this.controlpanel.update()}.bind(this),1e3/60))}update(){this.delta_time=Date.now()-this.last_update,this.last_update=Date.now(),this.env.update(this.delta_time),this.actual_fps=1/this.delta_time*1e3,null==this.render_loop&&(this.env.render(),this.controlpanel.update())}}},function(t,e,i){"use strict";i.r(e);var r=i(6),s=i.n(r);$("document").ready((function(){(new s.a).start(60)}))},function(t,e,i){i(1);const r=i(9),s=i(1),o=i(10),l=i(0),n=(i(2),i(12));t.exports=class{constructor(t){this.renderer=new r("canvas",this,t),this.controller=new n(this,this.renderer.canvas),this.grid_rows=Math.floor(this.renderer.height/t),this.grid_cols=Math.floor(this.renderer.width/t),this.grid_map=new s(this.grid_cols,this.grid_rows,t),this.renderer.renderFullGrid(),this.organisms=[],this.walls=[],this.total_mutability=0}update(t){var e=[];for(var i in this.organisms){var r=this.organisms[i];r.living&&r.update()||e.push(i)}this.removeOrganisms(e)}render(){this.renderer.renderCells(),this.renderer.renderHighlights()}removeOrganisms(t){for(var e of t.reverse())this.total_mutability-=this.organisms[e].mutability,this.organisms.splice(e,1)}OriginOfLife(){var t=this.grid_map.getCenter(),e=new o(t[0],t[1],this);e.addCell(l.mouth,1,1),e.addCell(l.producer,0,0),e.addCell(l.mouth,-1,-1),this.addOrganism(e)}addOrganism(t){t.updateGrid(),this.total_mutability+=t.mutability,this.organisms.push(t)}averageMutability(){return this.organisms.length<1?0:this.total_mutability/this.organisms.length}changeCell(t,e,i,r){this.grid_map.setCellType(t,e,i),this.grid_map.setCellOwner(t,e,r),this.renderer.addToRender(this.grid_map.cellAt(t,e)),i==l.wall&&this.walls.push(this.grid_map.cellAt(t,e))}clearWalls(){for(var t of this.walls)this.changeCell(t.col,t.row,l.empty,null)}clearOrganisms(){for(var t of this.organisms)t.die();this.organisms=[]}reset(){this.organisms=[],this.grid_map.fillGrid(l.empty),this.renderer.renderFullGrid(),this.total_mutability=0,this.OriginOfLife()}}},function(t,e){t.exports=class{constructor(t,e,i){this.cell_size=i,this.env=e,this.canvas=document.getElementById(t),this.ctx=this.canvas.getContext("2d"),this.canvas.width=$(".env").width(),this.canvas.height=$(".env").height(),this.height=canvas.height,this.width=canvas.width,this.cells_to_render=new Set,this.cells_to_highlight=new Set,this.highlighted_cells=new Set}clear(){this.ctx.fillStyle="white",this.ctx.fillRect(0,0,this.height,this.width)}renderFullGrid(){var t=this.env.grid_map.grid;for(var e of t)for(var i of e)this.ctx.fillStyle=i.getColor(),this.ctx.fillRect(i.x,i.y,this.cell_size,this.cell_size)}renderCells(){for(var t of this.cells_to_render)this.renderCell(t);this.cells_to_render.clear()}renderCell(t){this.ctx.fillStyle=t.getColor(),this.ctx.fillRect(t.x,t.y,this.cell_size,this.cell_size)}renderOrganism(t){for(var e of t.cells){var i=t.getRealCell(e);this.renderCell(i)}}addToRender(t){this.highlighted_cells.has(t)&&this.cells_to_highlight.add(t),this.cells_to_render.add(t)}renderHighlights(){for(var t of this.cells_to_highlight)this.renderCellHighlight(t),this.highlighted_cells.add(t);this.cells_to_highlight.clear()}highlightOrganism(t){for(var e of t.cells){var i=t.getRealCell(e);this.cells_to_highlight.add(i)}}highlightCell(t){this.cells_to_highlight.add(t)}renderCellHighlight(t,e="yellow"){this.renderCell(t),this.ctx.fillStyle=e,this.ctx.globalAlpha=.5,this.ctx.fillRect(t.x,t.y,this.cell_size,this.cell_size),this.ctx.globalAlpha=1,this.highlighted_cells.add(t)}clearAllHighlights(t=!1){for(var e of this.highlighted_cells)this.renderCell(e);this.highlighted_cells.clear(),t&&this.cells_to_highlight.clear()}}},function(t,e,i){const r=i(0),s=(i(2),i(1),i(11)),o=i(4),l=i(3),n=[[0,1],[0,-1],[1,0],[-1,0]];class h{constructor(t,e,i,r=null){this.c=t,this.r=e,this.env=i,this.lifetime=0,this.food_collected=0,this.living=!0,this.cells=[],this.is_producer=!1,this.is_mover=!1,this.direction=this.getRandomDirection(),this.move_count=0,this.move_range=5,this.mutability=5,null!=r&&this.inherit(r)}addCell(t,e,i){for(var r of this.cells)if(r.loc_col==e&&r.loc_row==i)return!1;return this.checkProducerMover(t),this.cells.push(new s(t,e,i)),!0}removeCell(t,e){for(var i=!1,s=0;s1?(this.cells.splice(Math.floor(Math.random()*this.cells.length),1),!0):(this.is_mover&&(this.move_range+=Math.floor(4*Math.random())-2),!1)}attemptMove(t){var e=t[0],i=t[1],s=this.c+e,o=this.r+i;if(this.isClear(s,o)){for(var l of this.cells){var n=this.c+l.loc_col,h=this.r+l.loc_row;this.env.changeCell(n,h,r.empty,null)}return this.c=s,this.r=o,this.updateGrid(),!0}return!1}getRandomDirection(){return n[Math.floor(Math.random()*n.length)]}isStraightPath(t,e,i,r,s){if(t==i){if(e>r){var o=r;r=e,e=o}for(var l=e;l!=r;l++){var n=this.env.grid_map.cellAt(t,l);if(!this.isPassableCell(n,s))return!1}return!0}if(t>i){o=i;i=t,t=o}for(l=t;l!=i;l++){n=this.env.grid_map.cellAt(l,e);if(!this.isPassableCell(n,s))return!1}return!0}isPassableCell(t,e){return null!=t&&(t.type==r.empty||t.owner==this||t.owner==e||t.type==r.food)}isClear(t,e){for(var i of this.cells){var s=this.getRealCell(i,t,e);if(null==s||s.type!=r.empty&&s.owner!=this)return!1}return!0}die(){for(var t of this.cells){var e=this.c+t.loc_col,i=this.r+t.loc_row;this.env.changeCell(e,i,r.food,null)}this.living=!1}updateGrid(){for(var t of this.cells){var e=this.c+t.loc_col,i=this.r+t.loc_row;this.env.changeCell(e,i,t.type,this)}}update(){if(this.lifetime++,this.lifetime>this.lifespan())return this.die(),this.living;for(var t of(this.food_collected>=this.foodNeeded()&&this.reproduce(),this.cells))this.getRealCell(t).performFunction(this.env);if(!this.living)return this.living;if(this.is_mover){this.move_count++;var e=this.attemptMove(this.direction);(this.move_count>this.move_range||!e)&&(this.move_count=0,this.direction=this.getRandomDirection())}return this.living}getRealCell(t,e=this.c,i=this.r){var r=e+t.loc_col,s=i+t.loc_row;return this.env.grid_map.cellAt(r,s)}}t.exports=h},function(t,e,i){i(0);t.exports=class{constructor(t,e,i){this.type=t,this.loc_col=e,this.loc_row=i}}},function(t,e,i){const r=i(5),s=i(0);t.exports=class{constructor(t,e){this.env=t,this.canvas=e,this.mouse_x,this.mouse_y,this.mouse_c,this.mouse_r,this.left_click=!1,this.right_click=!1,this.cur_cell=null,this.cur_org=null,this.mode=r.None,this.defineEvents()}defineEvents(){this.canvas.addEventListener("mousemove",t=>{var e=this.cur_cell,i=this.cur_org;this.mouse_x=t.offsetX,this.mouse_y=t.offsetY;var r=this.env.grid_map.xyToColRow(this.mouse_x,this.mouse_y);this.mouse_c=r[0],this.mouse_r=r[1],this.cur_cell=this.env.grid_map.cellAt(this.mouse_c,this.mouse_r),this.cur_org=this.cur_cell.owner,this.cur_org==i&&this.cur_cell==e||(this.env.renderer.clearAllHighlights(!0),null!=this.cur_org?this.env.renderer.highlightOrganism(this.cur_org):null!=this.cur_cell&&this.env.renderer.highlightCell(this.cur_cell,!0)),this.performModeAction()}),this.canvas.addEventListener("mouseup",function(t){t.preventDefault(),this.left_click=!1,this.right_click=!1}.bind(this)),this.canvas.addEventListener("mousedown",function(t){t.preventDefault(),0==t.button&&(this.left_click=!0),2==t.button&&(this.right_click=!0),this.performModeAction()}.bind(this)),this.canvas.addEventListener("contextmenu",(function(t){t.preventDefault()})),this.canvas.addEventListener("mouseleave",function(){this.right_click=!1,this.left_click=!1}.bind(this))}performModeAction(){var t=this.mode,e=this.right_click,i=this.left_click;if(t!=r.None&&(e||i)){var o=this.cur_cell;if(null==o)return;switch(t){case r.FoodDrop:i&&o.type==s.empty?this.env.changeCell(o.col,o.row,s.food,null):e&&o.type==s.food&&this.env.changeCell(o.col,o.row,s.empty,null);break;case r.WallDrop:!i||o.type!=s.empty&&o.type!=s.food?e&&o.type==s.wall&&this.env.changeCell(o.col,o.row,s.empty,null):this.env.changeCell(o.col,o.row,s.wall,null);break;case r.ClickKill:null!=this.cur_org&&this.cur_org.die()}}}}},function(t,e,i){const r=i(3),s=i(5);i(0);t.exports=class{constructor(t){this.engine=t,this.defineEngineSpeedControls(),this.defineHyperparameterControls(),this.defineModeControls(),this.fps=t.fps,this.organism_record=0,this.env_controller=this.engine.env.controller}defineEngineSpeedControls(){this.slider=document.getElementById("slider"),this.slider.oninput=function(){this.fps=this.slider.value,this.engine.running&&this.changeEngineSpeed(this.fps),$("#fps").text("Target FPS: "+this.fps)}.bind(this),$("#pause-button").click(function(){"Pause"==$("#pause-button").text()&&this.engine.running?($("#pause-button").text("Play"),this.engine.stop()):this.engine.running||($("#pause-button").text("Pause"),console.log(this.fps),this.engine.start(this.fps))}.bind(this))}defineHyperparameterControls(){$("#food-prod-prob").change(function(){var t=$("#food-prod-prob").val();$("#fixed-ratio").is(":checked")?(r.foodProdProb=t,r.calcProducerFoodRatio(!1),$("#lifespan-multiplier").val(r.lifespanMultiplier)):r.foodProdProb=t}.bind(this)),$("#lifespan-multiplier").change(function(){var t=$("#lifespan-multiplier").val();$("#fixed-ratio").is(":checked")?(r.lifespanMultiplier=t,r.calcProducerFoodRatio(!0),$("#food-prod-prob").val(r.foodProdProb)):r.lifespanMultiplier=t}.bind(this)),$(".mut-prob").change((function(){switch(this.id){case"add-prob":r.addProb=this.value,r.balanceMutationProbs(1);break;case"change-prob":r.changeProb=this.value,r.balanceMutationProbs(2);break;case"remove-prob":r.removeProb=this.value,r.balanceMutationProbs(3)}$("#add-prob").val(Math.floor(r.addProb)),$("#change-prob").val(Math.floor(r.changeProb)),$("#remove-prob").val(Math.floor(r.removeProb))}))}defineModeControls(){var t=this;$(".control-mode-button").click((function(){switch(this.id){case"food-button":t.env_controller.mode=s.FoodDrop;break;case"wall-button":t.env_controller.mode=s.WallDrop;break;case"kill-button":t.env_controller.mode=s.ClickKill;break;case"none-button":t.env_controller.mode=s.None}$(".control-mode-button").css("background-color","lightgray"),$("#"+this.id).css("background-color","darkgray")})),$("#reset-env").click(function(){this.engine.env.reset()}.bind(this)),$("#kill-all").click(function(){this.engine.env.clearOrganisms()}.bind(this)),$("#clear-walls").click(function(){this.engine.env.clearWalls()}.bind(this))}changeEngineSpeed(t){this.engine.stop(),this.engine.start(t),this.fps=this.engine.fps}update(){$("#fps-actual").text("Actual FPS: "+Math.floor(this.engine.actual_fps));var t=this.engine.env.organisms.length;$("#org-count").text("Organism count: "+t),t>this.organism_record&&(this.organism_record=t),$("#org-record").text("Highest count: "+this.organism_record),$("#avg-mut").text("Average Mutation Rate: "+Math.round(100*this.engine.env.averageMutability())/100)}}}]); \ No newline at end of file +/******/ (function(modules) { // webpackBootstrap +/******/ // The module cache +/******/ var installedModules = {}; +/******/ +/******/ // The require function +/******/ function __webpack_require__(moduleId) { +/******/ +/******/ // Check if module is in cache +/******/ if(installedModules[moduleId]) { +/******/ return installedModules[moduleId].exports; +/******/ } +/******/ // Create a new module (and put it into the cache) +/******/ var module = installedModules[moduleId] = { +/******/ i: moduleId, +/******/ l: false, +/******/ exports: {} +/******/ }; +/******/ +/******/ // Execute the module function +/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); +/******/ +/******/ // Flag the module as loaded +/******/ module.l = true; +/******/ +/******/ // Return the exports of the module +/******/ return module.exports; +/******/ } +/******/ +/******/ +/******/ // expose the modules object (__webpack_modules__) +/******/ __webpack_require__.m = modules; +/******/ +/******/ // expose the module cache +/******/ __webpack_require__.c = installedModules; +/******/ +/******/ // define getter function for harmony exports +/******/ __webpack_require__.d = function(exports, name, getter) { +/******/ if(!__webpack_require__.o(exports, name)) { +/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter }); +/******/ } +/******/ }; +/******/ +/******/ // define __esModule on exports +/******/ __webpack_require__.r = function(exports) { +/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { +/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); +/******/ } +/******/ Object.defineProperty(exports, '__esModule', { value: true }); +/******/ }; +/******/ +/******/ // create a fake namespace object +/******/ // mode & 1: value is a module id, require it +/******/ // mode & 2: merge all properties of value into the ns +/******/ // mode & 4: return value when already ns object +/******/ // mode & 8|1: behave like require +/******/ __webpack_require__.t = function(value, mode) { +/******/ if(mode & 1) value = __webpack_require__(value); +/******/ if(mode & 8) return value; +/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value; +/******/ var ns = Object.create(null); +/******/ __webpack_require__.r(ns); +/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value }); +/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key)); +/******/ return ns; +/******/ }; +/******/ +/******/ // getDefaultExport function for compatibility with non-harmony modules +/******/ __webpack_require__.n = function(module) { +/******/ var getter = module && module.__esModule ? +/******/ function getDefault() { return module['default']; } : +/******/ function getModuleExports() { return module; }; +/******/ __webpack_require__.d(getter, 'a', getter); +/******/ return getter; +/******/ }; +/******/ +/******/ // Object.prototype.hasOwnProperty.call +/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; +/******/ +/******/ // __webpack_public_path__ +/******/ __webpack_require__.p = ""; +/******/ +/******/ +/******/ // Load entry module and return exports +/******/ return __webpack_require__(__webpack_require__.s = "./src/index.js"); +/******/ }) +/************************************************************************/ +/******/ ({ + +/***/ "./src/Cell.js": +/*!*********************!*\ + !*** ./src/Cell.js ***! + \*********************/ +/*! no static exports found */ +/***/ (function(module, exports, __webpack_require__) { + +eval("const CellTypes = __webpack_require__(/*! ./CellTypes */ \"./src/CellTypes.js\");\r\nconst Hyperparams = __webpack_require__(/*! ./Hyperparameters */ \"./src/Hyperparameters.js\");\r\n\r\n// A cell exists in a grid system.\r\nclass Cell{\r\n constructor(type, col, row, x, y){\r\n this.owner = null;\r\n this.setType(type);\r\n this.col = col;\r\n this.row = row;\r\n this.x = x;\r\n this.y = y;\r\n }\r\n\r\n setType(type) {\r\n this.type = type;\r\n }\r\n\r\n performFunction(env) {\r\n switch(this.type){\r\n case CellTypes.mouth:\r\n eatFood(this, env);\r\n break;\r\n case CellTypes.producer:\r\n growFood(this, env);\r\n break;\r\n case CellTypes.killer:\r\n killNeighbors(this, env);\r\n break;\r\n }\r\n }\r\n\r\n getColor() {\r\n return CellTypes.colors[this.type];\r\n }\r\n\r\n isLiving() {\r\n return this.type != CellTypes.empty && \r\n this.type != CellTypes.food && \r\n this.type != CellTypes.wall;\r\n }\r\n}\r\n\r\nfunction eatFood(self, env){\r\n for (var loc of Hyperparams.edibleNeighbors){\r\n var cell = env.grid_map.cellAt(self.col+loc[0], self.row+loc[1]);\r\n eatNeighborFood(self, cell, env);\r\n }\r\n}\r\n\r\nfunction eatNeighborFood(self, n_cell, env){\r\n if (n_cell == null)\r\n return;\r\n if (n_cell.type == CellTypes.food){\r\n env.changeCell(n_cell.col, n_cell.row, CellTypes.empty, null);\r\n self.owner.food_collected++;\r\n }\r\n}\r\n\r\nfunction growFood(self, env){\r\n if (self.owner.is_mover)\r\n return;\r\n var prob = Hyperparams.foodProdProb;\r\n if (Math.random() * 100 <= prob){\r\n var loc = Hyperparams.growableNeighbors[Math.floor(Math.random() * Hyperparams.growableNeighbors.length)]\r\n var c=loc[0];\r\n var r=loc[1];\r\n var cell = env.grid_map.cellAt(self.col+c, self.row+r);\r\n if (cell != null && cell.type == CellTypes.empty){\r\n env.changeCell(self.col+c, self.row+r, CellTypes.food, null);\r\n return;\r\n }\r\n }\r\n}\r\n\r\nfunction killNeighbors(self, env) {\r\n for (var loc of Hyperparams.killableNeighbors){\r\n var cell = env.grid_map.cellAt(self.col+loc[0], self.row+loc[1]);\r\n killNeighbor(self, cell);\r\n }\r\n}\r\n\r\nfunction killNeighbor(self, n_cell) {\r\n if(n_cell == null || n_cell.owner == null || self.owner == null || n_cell.owner == self.owner || !n_cell.owner.living || n_cell.type == CellTypes.armor) \r\n return;\r\n var is_hit = n_cell.type == CellTypes.killer; // has to be calculated before death\r\n n_cell.owner.harm();\r\n if (is_hit){\r\n self.owner.harm();\r\n }\r\n}\r\n\r\nmodule.exports = Cell;\r\n\n\n//# sourceURL=webpack:///./src/Cell.js?"); + +/***/ }), + +/***/ "./src/CellTypes.js": +/*!**************************!*\ + !*** ./src/CellTypes.js ***! + \**************************/ +/*! no static exports found */ +/***/ (function(module, exports) { + +eval("const CellTypes = {\r\n empty: 0,\r\n food: 1,\r\n wall: 2,\r\n mouth: 3,\r\n producer: 4,\r\n mover: 5,\r\n killer: 6,\r\n armor: 7,\r\n colors: ['#121D29', 'green', 'gray', 'orange', 'white', '#3493eb', 'red', 'purple'],\r\n getRandomLivingType: function() {\r\n return Math.floor(Math.random() * 5) + 3;\r\n }\r\n}\r\n\r\nmodule.exports = CellTypes;\r\n\n\n//# sourceURL=webpack:///./src/CellTypes.js?"); + +/***/ }), + +/***/ "./src/ControlModes.js": +/*!*****************************!*\ + !*** ./src/ControlModes.js ***! + \*****************************/ +/*! no static exports found */ +/***/ (function(module, exports) { + +eval("const Modes = {\r\n None: 0,\r\n FoodDrop: 1,\r\n WallDrop: 2,\r\n ClickKill: 3\r\n}\r\n\r\nmodule.exports = Modes;\n\n//# sourceURL=webpack:///./src/ControlModes.js?"); + +/***/ }), + +/***/ "./src/ControlPanel.js": +/*!*****************************!*\ + !*** ./src/ControlPanel.js ***! + \*****************************/ +/*! no static exports found */ +/***/ (function(module, exports, __webpack_require__) { + +eval("const Hyperparams = __webpack_require__(/*! ./Hyperparameters */ \"./src/Hyperparameters.js\");\r\nconst Modes = __webpack_require__(/*! ./ControlModes */ \"./src/ControlModes.js\");\r\nconst CellTypes = __webpack_require__(/*! ./CellTypes */ \"./src/CellTypes.js\");\r\n\r\nclass ControlPanel {\r\n constructor(engine) {\r\n this.engine = engine;\r\n this.defineEngineSpeedControls();\r\n this.defineHyperparameterControls();\r\n this.defineModeControls();\r\n this.fps = engine.fps;\r\n this.organism_record=0;\r\n this.env_controller = this.engine.env.controller;\r\n }\r\n\r\n defineEngineSpeedControls(){\r\n this.slider = document.getElementById(\"slider\");\r\n this.slider.oninput = function() {\r\n this.fps = this.slider.value\r\n if (this.engine.running) {\r\n this.changeEngineSpeed(this.fps);\r\n \r\n }\r\n $('#fps').text(\"Target FPS: \"+this.fps);\r\n }.bind(this);\r\n $('#pause-button').click(function() {\r\n if ($('#pause-button').text() == \"Pause\" && this.engine.running) {\r\n $('#pause-button').text(\"Play\");\r\n this.engine.stop();\r\n }\r\n else if (!this.engine.running){\r\n $('#pause-button').text(\"Pause\");\r\n this.engine.start(this.fps);\r\n }\r\n }.bind(this));\r\n }\r\n\r\n defineHyperparameterControls() {\r\n $('#food-prod-prob').change(function() {\r\n var food_prob = $('#food-prod-prob').val();\r\n if ($('#fixed-ratio').is(\":checked\")) {\r\n Hyperparams.foodProdProb = food_prob;\r\n Hyperparams.calcProducerFoodRatio(false);\r\n $('#lifespan-multiplier').val(Hyperparams.lifespanMultiplier);\r\n }\r\n else{\r\n Hyperparams.foodProdProb = food_prob;\r\n }\r\n }.bind(this));\r\n $('#lifespan-multiplier').change(function() {\r\n var lifespan = $('#lifespan-multiplier').val();\r\n if ($('#fixed-ratio').is(\":checked\")) {\r\n Hyperparams.lifespanMultiplier = lifespan;\r\n Hyperparams.calcProducerFoodRatio(true);\r\n $('#food-prod-prob').val(Hyperparams.foodProdProb);\r\n }\r\n else {\r\n Hyperparams.lifespanMultiplier = lifespan;\r\n }\r\n }.bind(this));\r\n $('.mut-prob').change( function() {\r\n switch(this.id){\r\n case \"add-prob\":\r\n Hyperparams.addProb = this.value;\r\n Hyperparams.balanceMutationProbs(1);\r\n break;\r\n case \"change-prob\":\r\n Hyperparams.changeProb = this.value;\r\n Hyperparams.balanceMutationProbs(2);\r\n break;\r\n case \"remove-prob\":\r\n Hyperparams.removeProb = this.value;\r\n Hyperparams.balanceMutationProbs(3);\r\n break;\r\n }\r\n $('#add-prob').val(Math.floor(Hyperparams.addProb));\r\n $('#change-prob').val(Math.floor(Hyperparams.changeProb));\r\n $('#remove-prob').val(Math.floor(Hyperparams.removeProb));\r\n });\r\n\r\n $('#mover-rot').change(function() {\r\n Hyperparams.moversCanRotate = this.checked;\r\n });\r\n $('#offspring-rot').change(function() {\r\n Hyperparams.offspringRotate = this.checked;\r\n });\r\n $('#insta-kill').change(function() {\r\n Hyperparams.instaKill = this.checked;\r\n });\r\n }\r\n\r\n defineModeControls() {\r\n var self = this;\r\n $('.control-mode-button').click( function() {\r\n switch(this.id){\r\n case \"food-button\":\r\n self.env_controller.mode = Modes.FoodDrop;\r\n break;\r\n case \"wall-button\":\r\n self.env_controller.mode = Modes.WallDrop;\r\n break;\r\n case \"kill-button\":\r\n self.env_controller.mode = Modes.ClickKill;\r\n break;\r\n case \"none-button\":\r\n self.env_controller.mode = Modes.None;\r\n break;\r\n }\r\n $(\".control-mode-button\" ).css( \"background-color\", \"lightgray\" );\r\n $(\"#\"+this.id).css(\"background-color\", \"darkgray\");\r\n });\r\n\r\n\r\n $('#reset-env').click( function() {\r\n this.engine.env.reset();\r\n }.bind(this));\r\n $('#kill-all').click( function() {\r\n this.engine.env.clearOrganisms();\r\n }.bind(this));\r\n $('#clear-walls').click( function() {\r\n this.engine.env.clearWalls();\r\n }.bind(this));\r\n }\r\n\r\n changeEngineSpeed(change_val) {\r\n this.engine.stop();\r\n this.engine.start(change_val)\r\n this.fps = this.engine.fps;\r\n }\r\n\r\n update() {\r\n $('#fps-actual').text(\"Actual FPS: \" + Math.floor(this.engine.actual_fps));\r\n var org_count = this.engine.env.organisms.length;\r\n $('#org-count').text(\"Organism count: \" + org_count);\r\n if (org_count > this.organism_record) \r\n this.organism_record = org_count;\r\n $('#org-record').text(\"Highest count: \" + this.organism_record);\r\n $('#avg-mut').text(\"Average Mutation Rate: \" + Math.round(this.engine.env.averageMutability() * 100) / 100);\r\n }\r\n\r\n}\r\n\r\n\r\nmodule.exports = ControlPanel;\n\n//# sourceURL=webpack:///./src/ControlPanel.js?"); + +/***/ }), + +/***/ "./src/Directions.js": +/*!***************************!*\ + !*** ./src/Directions.js ***! + \***************************/ +/*! no static exports found */ +/***/ (function(module, exports) { + +eval("const Directions = {\r\n up:0,\r\n down:1,\r\n left:2,\r\n right:3,\r\n scalars:[[0,-1],[0,1],[-1,0],[1,0]],\r\n getRandomDirection: function() {\r\n return Math.floor(Math.random() * 4);\r\n },\r\n getRandomScalar: function() {\r\n return this.scalars[Math.floor(Math.random() * this.scalars.length)];\r\n }\r\n}\r\n\r\nmodule.exports = Directions;\n\n//# sourceURL=webpack:///./src/Directions.js?"); + +/***/ }), + +/***/ "./src/Engine.js": +/*!***********************!*\ + !*** ./src/Engine.js ***! + \***********************/ +/*! no static exports found */ +/***/ (function(module, exports, __webpack_require__) { + +eval("const Environment = __webpack_require__(/*! ./Environment */ \"./src/Environment.js\");\r\nconst ControlPanel = __webpack_require__(/*! ./ControlPanel */ \"./src/ControlPanel.js\");\r\n\r\nconst render_speed = 60;\r\n\r\nclass Engine{\r\n constructor(){\r\n this.fps = 60;\r\n this.env = new Environment(5);\r\n this.controlpanel = new ControlPanel(this);\r\n this.env.OriginOfLife();\r\n this.last_update = Date.now();\r\n this.delta_time = 0;\r\n this.actual_fps = 0;\r\n this.running = false;\r\n }\r\n\r\n start(fps=60) {\r\n if (fps <= 0)\r\n fps = 1;\r\n if (fps > 300)\r\n fps = 300;\r\n this.fps = fps;\r\n this.game_loop = setInterval(function(){this.update();}.bind(this), 1000/fps);\r\n this.running = true;\r\n if (this.fps >= render_speed) {\r\n if (this.render_loop != null) {\r\n clearInterval(this.render_loop);\r\n this.render_loop = null;\r\n }\r\n }\r\n else\r\n this.setRenderLoop();\r\n }\r\n \r\n stop() {\r\n clearInterval(this.game_loop);\r\n this.running = false;\r\n this.setRenderLoop();\r\n }\r\n\r\n setRenderLoop() {\r\n if (this.render_loop == null) {\r\n this.render_loop = setInterval(function(){this.env.render();this.controlpanel.update();}.bind(this), 1000/render_speed);\r\n }\r\n }\r\n\r\n\r\n update() {\r\n this.delta_time = Date.now() - this.last_update;\r\n this.last_update = Date.now();\r\n this.env.update(this.delta_time);\r\n this.actual_fps = 1/this.delta_time*1000;\r\n if(this.render_loop == null){\r\n this.env.render();\r\n this.controlpanel.update();\r\n }\r\n \r\n }\r\n\r\n}\r\n\r\nmodule.exports = Engine;\r\n\n\n//# sourceURL=webpack:///./src/Engine.js?"); + +/***/ }), + +/***/ "./src/Environment.js": +/*!****************************!*\ + !*** ./src/Environment.js ***! + \****************************/ +/*! no static exports found */ +/***/ (function(module, exports, __webpack_require__) { + +eval("const Grid = __webpack_require__(/*! ./GridMap */ \"./src/GridMap.js\");\r\nconst Renderer = __webpack_require__(/*! ./Rendering/Renderer */ \"./src/Rendering/Renderer.js\");\r\nconst GridMap = __webpack_require__(/*! ./GridMap */ \"./src/GridMap.js\");\r\nconst Organism = __webpack_require__(/*! ./Organism */ \"./src/Organism.js\");\r\nconst CellTypes = __webpack_require__(/*! ./CellTypes */ \"./src/CellTypes.js\");\r\nconst Cell = __webpack_require__(/*! ./Cell */ \"./src/Cell.js\");\r\nconst EnvironmentController = __webpack_require__(/*! ./EnvironmentController */ \"./src/EnvironmentController.js\");\r\n\r\nclass Environment{\r\n constructor(cell_size) {\r\n this.renderer = new Renderer('canvas', this, cell_size);\r\n this.controller = new EnvironmentController(this, this.renderer.canvas);\r\n this.grid_rows = Math.floor(this.renderer.height / cell_size);\r\n this.grid_cols = Math.floor(this.renderer.width / cell_size);\r\n this.grid_map = new GridMap(this.grid_cols, this.grid_rows, cell_size);\r\n this.renderer.renderFullGrid();\r\n this.organisms = [];\r\n this.walls = [];\r\n this.total_mutability = 0;\r\n }\r\n\r\n update(delta_time) {\r\n var to_remove = [];\r\n for (var i in this.organisms) {\r\n var org = this.organisms[i];\r\n if (!org.living || !org.update()) {\r\n to_remove.push(i);\r\n }\r\n }\r\n this.removeOrganisms(to_remove);\r\n }\r\n\r\n render() {\r\n this.renderer.renderCells();\r\n this.renderer.renderHighlights();\r\n }\r\n\r\n removeOrganisms(org_indeces) {\r\n for (var i of org_indeces.reverse()){\r\n this.total_mutability -= this.organisms[i].mutability;\r\n this.organisms.splice(i, 1);\r\n }\r\n }\r\n\r\n OriginOfLife() {\r\n var center = this.grid_map.getCenter();\r\n var org = new Organism(center[0], center[1], this);\r\n org.addCell(CellTypes.mouth, 0, 0);\r\n org.addCell(CellTypes.producer, -1, -1);\r\n org.addCell(CellTypes.producer, 1, 1);\r\n this.addOrganism(org);\r\n }\r\n\r\n addOrganism(organism) {\r\n organism.updateGrid();\r\n this.total_mutability += organism.mutability;\r\n this.organisms.push(organism);\r\n }\r\n\r\n averageMutability() {\r\n if (this.organisms.length < 1)\r\n return 0;\r\n return this.total_mutability / this.organisms.length;\r\n }\r\n\r\n changeCell(c, r, type, owner) {\r\n this.grid_map.setCellType(c, r, type);\r\n this.grid_map.setCellOwner(c, r, owner);\r\n this.renderer.addToRender(this.grid_map.cellAt(c, r));\r\n if(type == CellTypes.wall)\r\n this.walls.push(this.grid_map.cellAt(c, r));\r\n }\r\n\r\n clearWalls() {\r\n for(var wall of this.walls)\r\n this.changeCell(wall.col, wall.row, CellTypes.empty, null);\r\n }\r\n\r\n clearOrganisms() {\r\n for (var org of this.organisms)\r\n org.die();\r\n this.organisms = [];\r\n }\r\n\r\n reset() {\r\n this.organisms = [];\r\n this.grid_map.fillGrid(CellTypes.empty);\r\n this.renderer.renderFullGrid();\r\n this.total_mutability = 0;\r\n this.OriginOfLife();\r\n }\r\n}\r\n\r\nmodule.exports = Environment;\r\n\r\n\n\n//# sourceURL=webpack:///./src/Environment.js?"); + +/***/ }), + +/***/ "./src/EnvironmentController.js": +/*!**************************************!*\ + !*** ./src/EnvironmentController.js ***! + \**************************************/ +/*! no static exports found */ +/***/ (function(module, exports, __webpack_require__) { + +eval("const Modes = __webpack_require__(/*! ./ControlModes */ \"./src/ControlModes.js\");\r\nconst CellTypes = __webpack_require__(/*! ./CellTypes */ \"./src/CellTypes.js\");\r\n\r\nclass EnvironmentController{\r\n constructor(env, canvas) {\r\n this.env = env;\r\n this.canvas = canvas;\r\n this.mouse_x;\r\n this.mouse_y;\r\n this.mouse_c;\r\n this.mouse_r;\r\n this.left_click = false;\r\n this.right_click = false;\r\n this.cur_cell = null;\r\n this.cur_org = null;\r\n this.mode = Modes.None;\r\n this.defineEvents();\r\n }\r\n\r\n defineEvents() {\r\n this.canvas.addEventListener('mousemove', e => {\r\n var prev_cell = this.cur_cell;\r\n var prev_org = this.cur_org;\r\n\r\n this.mouse_x = e.offsetX;\r\n this.mouse_y = e.offsetY;\r\n var colRow = this.env.grid_map.xyToColRow(this.mouse_x, this.mouse_y);\r\n this.mouse_c = colRow[0];\r\n this.mouse_r = colRow[1];\r\n this.cur_cell = this.env.grid_map.cellAt(this.mouse_c, this.mouse_r);\r\n this.cur_org = this.cur_cell.owner;\r\n\r\n if (this.cur_org != prev_org || this.cur_cell != prev_cell) {\r\n this.env.renderer.clearAllHighlights(true);\r\n if (this.cur_org != null) {\r\n this.env.renderer.highlightOrganism(this.cur_org);\r\n }\r\n else if (this.cur_cell != null) {\r\n this.env.renderer.highlightCell(this.cur_cell, true);\r\n }\r\n }\r\n this.performModeAction();\r\n });\r\n\r\n this.canvas.addEventListener('mouseup', function(evt) {\r\n evt.preventDefault();\r\n this.left_click=false;\r\n this.right_click=false;\r\n }.bind(this));\r\n\r\n this.canvas.addEventListener('mousedown', function(evt) {\r\n evt.preventDefault();\r\n if (evt.button == 0) {\r\n this.left_click = true;\r\n }\r\n if (evt.button == 2) \r\n this.right_click = true;\r\n this.performModeAction();\r\n }.bind(this));\r\n\r\n this.canvas.addEventListener('contextmenu', function(evt) {\r\n evt.preventDefault();\r\n });\r\n\r\n this.canvas.addEventListener('mouseleave', function(){\r\n this.right_click = false;\r\n this.left_click = false;\r\n }.bind(this));\r\n\r\n }\r\n\r\n performModeAction() {\r\n var mode = this.mode;\r\n var right_click = this.right_click;\r\n var left_click = this.left_click;\r\n if (mode != Modes.None && (right_click || left_click)) {\r\n var cell = this.cur_cell;\r\n if (cell == null){\r\n return;\r\n }\r\n switch(mode) {\r\n case Modes.FoodDrop:\r\n if (left_click && cell.type == CellTypes.empty){\r\n this.env.changeCell(cell.col, cell.row, CellTypes.food, null);\r\n }\r\n else if (right_click && cell.type == CellTypes.food){\r\n this.env.changeCell(cell.col, cell.row, CellTypes.empty, null);\r\n }\r\n break;\r\n case Modes.WallDrop:\r\n if (left_click && (cell.type == CellTypes.empty || cell.type == CellTypes.food)){\r\n this.env.changeCell(cell.col, cell.row, CellTypes.wall, null);\r\n }\r\n else if (right_click && cell.type == CellTypes.wall){\r\n this.env.changeCell(cell.col, cell.row, CellTypes.empty, null);\r\n }\r\n break;\r\n case Modes.ClickKill:\r\n if (this.cur_org != null)\r\n this.cur_org.die();\r\n }\r\n }\r\n }\r\n\r\n\r\n}\r\n\r\nmodule.exports = EnvironmentController;\r\n\n\n//# sourceURL=webpack:///./src/EnvironmentController.js?"); + +/***/ }), + +/***/ "./src/GridMap.js": +/*!************************!*\ + !*** ./src/GridMap.js ***! + \************************/ +/*! no static exports found */ +/***/ (function(module, exports, __webpack_require__) { + +eval("const Cell = __webpack_require__(/*! ./Cell */ \"./src/Cell.js\");\r\nconst CellTypes = __webpack_require__(/*! ./CellTypes */ \"./src/CellTypes.js\");\r\n\r\nclass GridMap {\r\n constructor(cols, rows, cell_size, filltype=CellTypes.empty) {\r\n this.grid = [];\r\n this.cols = cols;\r\n this.rows = rows;\r\n this.cell_size = cell_size;\r\n for(var c=0; c=0 && row>=0;\r\n }\r\n\r\n getCenter(){\r\n return [Math.floor(this.cols/2), Math.floor(this.rows/2)]\r\n }\r\n\r\n xyToColRow(x, y) {\r\n var c = Math.floor(x/this.cell_size);\r\n var r = Math.floor(y/this.cell_size);\r\n if (c >= this.cols)\r\n c = this.cols-1;\r\n else if (c < 0)\r\n c = 0;\r\n if (r >= this.rows)\r\n r = this.rows-1;\r\n else if (r < 0)\r\n r = 0;\r\n return [c, r];\r\n }\r\n}\r\n\r\nmodule.exports = GridMap;\r\n\n\n//# sourceURL=webpack:///./src/GridMap.js?"); + +/***/ }), + +/***/ "./src/Hyperparameters.js": +/*!********************************!*\ + !*** ./src/Hyperparameters.js ***! + \********************************/ +/*! no static exports found */ +/***/ (function(module, exports, __webpack_require__) { + +eval("const Neighbors = __webpack_require__(/*! ./Neighbors */ \"./src/Neighbors.js\");\r\n\r\nconst Hyperparams = {\r\n lifespanMultiplier: 100,\r\n foodProdProb: 4,\r\n foodProdProbScalar: 4,\r\n killableNeighbors: Neighbors.adjacent,\r\n edibleNeighbors: Neighbors.adjacent,\r\n growableNeighbors: Neighbors.adjacent,\r\n \r\n useGlobalMutability: false,\r\n globalMutability: 0,\r\n\r\n addProb: 33,\r\n changeProb: 33,\r\n removeProb: 33,\r\n\r\n moversCanRotate: true,\r\n offspringRotate: true,\r\n\r\n instaKill: false,\r\n\r\n // calculates the optimal ratio where a producer cell is most likely to produce 1 food in its lifespan * a scalar of my choice :)\r\n calcProducerFoodRatio : function(lifespan_fixed=true) {\r\n if (lifespan_fixed) {\r\n // change the foodProdProb\r\n this.foodProdProb = (100 / this.lifespanMultiplier) * this.foodProdProbScalar;\r\n }\r\n else {\r\n // change the lifespanMultiplier\r\n this.lifespanMultiplier = Math.floor(100 / (this.foodProdProb/this.foodProdProbScalar));\r\n }\r\n },\r\n\r\n balanceMutationProbs : function(choice) {\r\n if (choice == 1) {\r\n var remaining = 100 - this.addProb;\r\n this.changeProb = remaining/2;\r\n this.removeProb = remaining/2;\r\n }\r\n else if (choice == 2) {\r\n var remaining = 100 - this.changeProb;\r\n this.addProb = remaining/2;\r\n this.removeProb = remaining/2;\r\n }\r\n else {\r\n var remaining = 100 - this.removeProb;\r\n this.changeProb = remaining/2;\r\n this.addProb = remaining/2;\r\n }\r\n }\r\n}\r\n\r\nmodule.exports = Hyperparams;\n\n//# sourceURL=webpack:///./src/Hyperparameters.js?"); + +/***/ }), + +/***/ "./src/LocalCell.js": +/*!**************************!*\ + !*** ./src/LocalCell.js ***! + \**************************/ +/*! no static exports found */ +/***/ (function(module, exports, __webpack_require__) { + +eval("const CellTypes = __webpack_require__(/*! ./CellTypes */ \"./src/CellTypes.js\");\r\nconst Directions = __webpack_require__(/*! ./Directions */ \"./src/Directions.js\");\r\nconst Hyperparams = __webpack_require__(/*! ./Hyperparameters */ \"./src/Hyperparameters.js\");\r\n\r\n// A local cell is a lightweight container for a cell in an organism. It does not directly exist in the grid \r\nclass LocalCell{\r\n constructor(type, loc_col, loc_row){\r\n this.type = type;\r\n this.loc_col = loc_col;\r\n this.loc_row = loc_row;\r\n }\r\n\r\n rotatedCol(dir){\r\n switch(dir){\r\n case Directions.up:\r\n return this.loc_col;\r\n case Directions.down:\r\n return this.loc_col * -1;\r\n case Directions.left:\r\n return this.loc_row;\r\n case Directions.right:\r\n return this.loc_row * -1;\r\n }\r\n }\r\n\r\n rotatedRow(dir){\r\n switch(dir){\r\n case Directions.up:\r\n return this.loc_row;\r\n case Directions.down:\r\n return this.loc_row * -1;\r\n case Directions.left:\r\n return this.loc_col * -1;\r\n case Directions.right:\r\n return this.loc_col;\r\n }\r\n }\r\n}\r\n\r\nmodule.exports = LocalCell;\r\n\n\n//# sourceURL=webpack:///./src/LocalCell.js?"); + +/***/ }), + +/***/ "./src/Neighbors.js": +/*!**************************!*\ + !*** ./src/Neighbors.js ***! + \**************************/ +/*! no static exports found */ +/***/ (function(module, exports) { + +eval("// contains local cell values for the following:\r\n\r\n//all ...\r\n// .x.\r\n// ...\r\n\r\n//adjacent .\r\n// .x.\r\n// .\r\n\r\n//corners . .\r\n// x\r\n// . .\r\n\r\nconst Neighbors = {\r\n all: [[0, 1],[0, -1],[1, 0],[-1, 0],[-1, -1],[1, 1],[-1, 1],[1, -1]],\r\n adjacent: [[0, 1],[0, -1],[1, 0],[-1, 0]],\r\n corners: [[-1, -1],[1, 1],[-1, 1],[1, -1]]\r\n}\r\n\r\nmodule.exports = Neighbors;\n\n//# sourceURL=webpack:///./src/Neighbors.js?"); + +/***/ }), + +/***/ "./src/Organism.js": +/*!*************************!*\ + !*** ./src/Organism.js ***! + \*************************/ +/*! no static exports found */ +/***/ (function(module, exports, __webpack_require__) { + +eval("const CellTypes = __webpack_require__(/*! ./CellTypes */ \"./src/CellTypes.js\");\r\nconst Cell = __webpack_require__(/*! ./Cell */ \"./src/Cell.js\");\r\nconst GridMap = __webpack_require__(/*! ./GridMap */ \"./src/GridMap.js\");\r\nconst LocalCell = __webpack_require__(/*! ./LocalCell */ \"./src/LocalCell.js\");\r\nconst Neighbors = __webpack_require__(/*! ./Neighbors */ \"./src/Neighbors.js\");\r\nconst Hyperparams = __webpack_require__(/*! ./Hyperparameters */ \"./src/Hyperparameters.js\");\r\nconst Directions = __webpack_require__(/*! ./Directions */ \"./src/Directions.js\");\r\n\r\nconst directions = [[0,1],[0,-1],[1,0],[-1,0]]\r\n\r\nclass Organism {\r\n constructor(col, row, env, parent=null) {\r\n this.c = col;\r\n this.r = row;\r\n this.env = env;\r\n this.lifetime = 0;\r\n this.food_collected = 0;\r\n this.living = true;\r\n this.cells = [];\r\n this.is_producer = false;\r\n this.is_mover = false;\r\n this.direction = Directions.up;\r\n this.rotation = Directions.up;\r\n this.can_rotate = Hyperparams.moversCanRotate;\r\n this.move_count = 0;\r\n this.move_range = 4;\r\n this.mutability = 5;\r\n this.damage = 0;\r\n if (parent != null) {\r\n this.inherit(parent);\r\n }\r\n }\r\n\r\n addCell(type, c, r) {\r\n for (var cell of this.cells) {\r\n if (cell.loc_col == c && cell.loc_row == r)\r\n return false;\r\n }\r\n this.checkProducerMover(type);\r\n this.cells.push(new LocalCell(type, c, r));\r\n return true;\r\n }\r\n\r\n removeCell(c, r) {\r\n if (c == 0 && r == 0)\r\n return false;\r\n var check_change = false;\r\n for (var i=0; i 1) {\r\n cell = this.cells[Math.floor(Math.random() * this.cells.length)];\r\n mutated = this.removeCell(cell.loc_col, cell.loc_row);\r\n }\r\n }\r\n\r\n if (this.is_mover) {\r\n this.move_range += Math.floor(Math.random() * 4) - 2;\r\n if (this.move_range <= 0){\r\n this.move_range = 1;\r\n }\r\n }\r\n return mutated;\r\n }\r\n\r\n attemptMove() {\r\n var direction = Directions.scalars[this.direction];\r\n var direction_c = direction[0];\r\n var direction_r = direction[1];\r\n var new_c = this.c + direction_c;\r\n var new_r = this.r + direction_r;\r\n if (this.isClear(new_c, new_r)) {\r\n for (var cell of this.cells) {\r\n var real_c = this.c + cell.rotatedCol(this.rotation);\r\n var real_r = this.r + cell.rotatedRow(this.rotation);\r\n this.env.changeCell(real_c, real_r, CellTypes.empty, null);\r\n }\r\n this.c = new_c;\r\n this.r = new_r;\r\n this.updateGrid();\r\n return true;\r\n }\r\n return false;\r\n }\r\n\r\n attemptRotate() {\r\n if(!this.can_rotate){\r\n this.direction = Directions.getRandomDirection();\r\n this.move_count = 0;\r\n return true;\r\n }\r\n var new_rotation = Directions.getRandomDirection();\r\n if(this.isClear(this.c, this.r, new_rotation)){\r\n for (var cell of this.cells) {\r\n var real_c = this.c + cell.rotatedCol(this.rotation);\r\n var real_r = this.r + cell.rotatedRow(this.rotation);\r\n this.env.changeCell(real_c, real_r, CellTypes.empty, null);\r\n }\r\n this.rotation = new_rotation;\r\n this.direction = Directions.getRandomDirection();\r\n this.updateGrid();\r\n this.move_count = 0;\r\n return true;\r\n }\r\n return false;\r\n }\r\n\r\n // assumes either c1==c2 or r1==r2, returns true if there is a clear path from point a to b\r\n isStraightPath(c1, r1, c2, r2, parent){\r\n if (c1 == c2) {\r\n if (r1 > r2){\r\n var temp = r2;\r\n r2 = r1;\r\n r1 = temp;\r\n }\r\n for (var i=r1; i!=r2; i++) {\r\n var cell = this.env.grid_map.cellAt(c1, i)\r\n if (!this.isPassableCell(cell, parent)){\r\n return false;\r\n }\r\n }\r\n return true;\r\n }\r\n else {\r\n if (c1 > c2){\r\n var temp = c2;\r\n c2 = c1;\r\n c1 = temp;\r\n }\r\n for (var i=c1; i!=c2; i++) {\r\n var cell = this.env.grid_map.cellAt(i, r1);\r\n if (!this.isPassableCell(cell, parent)){\r\n return false;\r\n }\r\n }\r\n return true;\r\n }\r\n }\r\n\r\n isPassableCell(cell, parent){\r\n return cell != null && (cell.type == CellTypes.empty || cell.owner == this || cell.owner == parent || cell.type == CellTypes.food);\r\n }\r\n\r\n isClear(col, row, rotation=this.rotation) {\r\n for(var loccell of this.cells) {\r\n var cell = this.getRealCell(loccell, col, row, rotation);\r\n if(cell == null || cell.type != CellTypes.empty && cell.owner != this) {\r\n return false;\r\n }\r\n }\r\n return true;\r\n }\r\n\r\n harm() {\r\n this.damage++;\r\n if (this.damage >= this.maxHealth() || Hyperparams.instaKill) {\r\n this.die();\r\n }\r\n }\r\n\r\n die() {\r\n for (var cell of this.cells) {\r\n var real_c = this.c + cell.rotatedCol(this.rotation);\r\n var real_r = this.r + cell.rotatedRow(this.rotation);\r\n this.env.changeCell(real_c, real_r, CellTypes.food, null);\r\n }\r\n this.living = false;\r\n }\r\n\r\n updateGrid() {\r\n for (var cell of this.cells) {\r\n var real_c = this.c + cell.rotatedCol(this.rotation);\r\n var real_r = this.r + cell.rotatedRow(this.rotation);\r\n this.env.changeCell(real_c, real_r, cell.type, this);\r\n }\r\n }\r\n\r\n update() {\r\n this.lifetime++;\r\n if (this.lifetime > this.lifespan()) {\r\n this.die();\r\n return this.living;\r\n }\r\n if (this.food_collected >= this.foodNeeded()) {\r\n this.reproduce();\r\n }\r\n for (var cell of this.cells) {\r\n this.getRealCell(cell).performFunction(this.env);\r\n }\r\n if (!this.living){\r\n return this.living\r\n }\r\n if (this.is_mover) {\r\n this.move_count++;\r\n var moved = this.attemptMove();\r\n if (this.move_count > this.move_range){\r\n this.attemptRotate();\r\n }\r\n }\r\n\r\n return this.living;\r\n }\r\n\r\n getRealCell(local_cell, c=this.c, r=this.r, rotation=this.rotation){\r\n var real_c = c + local_cell.rotatedCol(rotation);\r\n var real_r = r + local_cell.rotatedRow(rotation);\r\n return this.env.grid_map.cellAt(real_c, real_r);\r\n }\r\n\r\n}\r\n\r\nmodule.exports = Organism;\r\n\n\n//# sourceURL=webpack:///./src/Organism.js?"); + +/***/ }), + +/***/ "./src/Rendering/Renderer.js": +/*!***********************************!*\ + !*** ./src/Rendering/Renderer.js ***! + \***********************************/ +/*! no static exports found */ +/***/ (function(module, exports) { + +eval("\r\n// Renderer controls access to a canvas. There is one renderer for each canvas\r\nclass Renderer {\r\n constructor(canvas_id, env, cell_size) {\r\n this.cell_size = cell_size;\r\n this.env = env;\r\n this.canvas = document.getElementById(canvas_id);\r\n this.ctx = this.canvas.getContext(\"2d\");\r\n this.canvas.width = $('.env').width();\r\n this.canvas.height = $('.env').height();\r\n\t\tthis.height = canvas.height;\r\n this.width = canvas.width;\r\n this.cells_to_render = new Set();\r\n this.cells_to_highlight = new Set();\r\n this.highlighted_cells = new Set();\r\n }\r\n\r\n clear() {\r\n this.ctx.fillStyle = 'white';\r\n this.ctx.fillRect(0, 0, this.height, this.width);\r\n }\r\n\r\n renderFullGrid() {\r\n var grid = this.env.grid_map.grid;\r\n for (var col of grid) {\r\n for (var cell of col){\r\n this.ctx.fillStyle = cell.getColor();\r\n this.ctx.fillRect(cell.x, cell.y, this.cell_size, this.cell_size);\r\n }\r\n }\r\n }\r\n\r\n renderCells() {\r\n for (var cell of this.cells_to_render) {\r\n this.renderCell(cell);\r\n }\r\n this.cells_to_render.clear();\r\n }\r\n\r\n renderCell(cell) {\r\n this.ctx.fillStyle = cell.getColor();\r\n this.ctx.fillRect(cell.x, cell.y, this.cell_size, this.cell_size);\r\n }\r\n\r\n renderOrganism(org) {\r\n for(var org_cell of org.cells) {\r\n var cell = org.getRealCell(org_cell);\r\n this.renderCell(cell);\r\n }\r\n }\r\n\r\n addToRender(cell) {\r\n if (this.highlighted_cells.has(cell)){\r\n this.cells_to_highlight.add(cell);\r\n }\r\n this.cells_to_render.add(cell);\r\n }\r\n\r\n renderHighlights() {\r\n for (var cell of this.cells_to_highlight) {\r\n this.renderCellHighlight(cell);\r\n this.highlighted_cells.add(cell);\r\n }\r\n this.cells_to_highlight.clear();\r\n \r\n }\r\n\r\n highlightOrganism(org) {\r\n for(var org_cell of org.cells) {\r\n var cell = org.getRealCell(org_cell);\r\n this.cells_to_highlight.add(cell);\r\n }\r\n }\r\n\r\n highlightCell(cell) {\r\n this.cells_to_highlight.add(cell);\r\n }\r\n\r\n renderCellHighlight(cell, color=\"yellow\") {\r\n this.renderCell(cell);\r\n this.ctx.fillStyle = color;\r\n this.ctx.globalAlpha = 0.5;\r\n this.ctx.fillRect(cell.x, cell.y, this.cell_size, this.cell_size);\r\n this.ctx.globalAlpha = 1;\r\n this.highlighted_cells.add(cell);\r\n }\r\n\r\n clearAllHighlights(clear_to_highlight=false) {\r\n for (var cell of this.highlighted_cells) {\r\n this.renderCell(cell);\r\n }\r\n this.highlighted_cells.clear();\r\n if (clear_to_highlight) {\r\n this.cells_to_highlight.clear();\r\n }\r\n }\r\n}\r\n\r\n// $(\"body\").mousemove(function(e) {\r\n// console.log(\"hello\");\r\n// });\r\n\r\nmodule.exports = Renderer;\r\n\n\n//# sourceURL=webpack:///./src/Rendering/Renderer.js?"); + +/***/ }), + +/***/ "./src/index.js": +/*!**********************!*\ + !*** ./src/index.js ***! + \**********************/ +/*! no exports provided */ +/***/ (function(module, __webpack_exports__, __webpack_require__) { + +"use strict"; +eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _Engine__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./Engine */ \"./src/Engine.js\");\n/* harmony import */ var _Engine__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_Engine__WEBPACK_IMPORTED_MODULE_0__);\n'user strict';\r\n\r\n\r\n\r\n$('document').ready(function(){\r\n var engine = new _Engine__WEBPACK_IMPORTED_MODULE_0___default.a();\r\n engine.start(60);\r\n});\r\n\n\n//# sourceURL=webpack:///./src/index.js?"); + +/***/ }) + +/******/ }); \ No newline at end of file diff --git a/dist/css/style.css b/dist/css/style.css index e74d1d6..f5614f0 100644 --- a/dist/css/style.css +++ b/dist/css/style.css @@ -2,6 +2,8 @@ body{ background: black; margin: 0; padding: 0; + height: 100%; + width: 100%; } canvas { @@ -15,15 +17,18 @@ canvas { } .env { - height: 80vh; - position: static; + position: absolute; + top: 0; + bottom: 300px; /* must correspond to control-panel height*/ + width: 100%; } .control-panel { - height: 20vh; - width: 100vw; + height: 300px; + width: 100%; + bottom: 0; + position: absolute; background-color: gray; - position: fixed; display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(2, 1fr); @@ -35,20 +40,16 @@ canvas { border: 10px; border-color: black; background-color: lightgray; - max-width: 100%; - /* height: 100%; */ } #speed-controller { grid-column: 1; grid-row: 1; - /* background-color: blue; */ } #stats { grid-column: 1; grid-row: 2; - /* background-color: blue; */ } #hyperparameters { @@ -60,8 +61,6 @@ canvas { #abilities { grid-column: 3; grid-row: 1 / 3; - /* background-color: yellow; */ - } .control-mode-button { diff --git a/dist/html/index.html b/dist/html/index.html index 6cd1712..5768a25 100644 --- a/dist/html/index.html +++ b/dist/html/index.html @@ -4,14 +4,15 @@ Evolution Simulator + +
-

Simulation Speed

@@ -48,6 +49,15 @@
+

Organism Rotation

+ + +
+ + +
+ +
@@ -62,6 +72,5 @@
- \ No newline at end of file diff --git a/src/Cell.js b/src/Cell.js index 3b8a725..34d74d8 100644 --- a/src/Cell.js +++ b/src/Cell.js @@ -81,12 +81,12 @@ function killNeighbors(self, env) { } function killNeighbor(self, n_cell) { - if(n_cell == null || n_cell.owner == null || n_cell.owner == self.owner || !n_cell.owner.living || n_cell.type == CellTypes.armor) + if(n_cell == null || n_cell.owner == null || self.owner == null || n_cell.owner == self.owner || !n_cell.owner.living || n_cell.type == CellTypes.armor) return; - var should_die = n_cell.type == CellTypes.killer; // has to be calculated before death - n_cell.owner.die(); - if (should_die){ - self.owner.die(); + var is_hit = n_cell.type == CellTypes.killer; // has to be calculated before death + n_cell.owner.harm(); + if (is_hit){ + self.owner.harm(); } } diff --git a/src/CellTypes.js b/src/CellTypes.js index d579ac8..801a498 100644 --- a/src/CellTypes.js +++ b/src/CellTypes.js @@ -7,7 +7,7 @@ const CellTypes = { mover: 5, killer: 6, armor: 7, - colors: ['#121D29', 'green', 'gray', 'orange', 'white', 'blue', 'red', 'purple'], + colors: ['#121D29', 'green', 'gray', 'orange', 'white', '#3493eb', 'red', 'purple'], getRandomLivingType: function() { return Math.floor(Math.random() * 5) + 3; } diff --git a/src/ControlPanel.js b/src/ControlPanel.js index 7790fc9..462dcb4 100644 --- a/src/ControlPanel.js +++ b/src/ControlPanel.js @@ -30,7 +30,6 @@ class ControlPanel { } else if (!this.engine.running){ $('#pause-button').text("Pause"); - console.log(this.fps) this.engine.start(this.fps); } }.bind(this)); @@ -78,6 +77,16 @@ class ControlPanel { $('#change-prob').val(Math.floor(Hyperparams.changeProb)); $('#remove-prob').val(Math.floor(Hyperparams.removeProb)); }); + + $('#mover-rot').change(function() { + Hyperparams.moversCanRotate = this.checked; + }); + $('#offspring-rot').change(function() { + Hyperparams.offspringRotate = this.checked; + }); + $('#insta-kill').change(function() { + Hyperparams.instaKill = this.checked; + }); } defineModeControls() { diff --git a/src/Directions.js b/src/Directions.js new file mode 100644 index 0000000..9d667a2 --- /dev/null +++ b/src/Directions.js @@ -0,0 +1,15 @@ +const Directions = { + up:0, + down:1, + left:2, + right:3, + scalars:[[0,-1],[0,1],[-1,0],[1,0]], + getRandomDirection: function() { + return Math.floor(Math.random() * 4); + }, + getRandomScalar: function() { + return this.scalars[Math.floor(Math.random() * this.scalars.length)]; + } +} + +module.exports = Directions; \ No newline at end of file diff --git a/src/Environment.js b/src/Environment.js index 5ec56ed..e7d1b10 100644 --- a/src/Environment.js +++ b/src/Environment.js @@ -45,9 +45,9 @@ class Environment{ OriginOfLife() { var center = this.grid_map.getCenter(); var org = new Organism(center[0], center[1], this); - org.addCell(CellTypes.mouth, 1, 1); - org.addCell(CellTypes.producer, 0, 0); - org.addCell(CellTypes.mouth, -1, -1); + org.addCell(CellTypes.mouth, 0, 0); + org.addCell(CellTypes.producer, -1, -1); + org.addCell(CellTypes.producer, 1, 1); this.addOrganism(org); } diff --git a/src/Hyperparameters.js b/src/Hyperparameters.js index ee0e1cd..f745239 100644 --- a/src/Hyperparameters.js +++ b/src/Hyperparameters.js @@ -15,6 +15,11 @@ const Hyperparams = { changeProb: 33, removeProb: 33, + moversCanRotate: true, + offspringRotate: true, + + instaKill: false, + // calculates the optimal ratio where a producer cell is most likely to produce 1 food in its lifespan * a scalar of my choice :) calcProducerFoodRatio : function(lifespan_fixed=true) { if (lifespan_fixed) { diff --git a/src/LocalCell.js b/src/LocalCell.js index ffa09f5..25725e3 100644 --- a/src/LocalCell.js +++ b/src/LocalCell.js @@ -1,4 +1,6 @@ const CellTypes = require("./CellTypes"); +const Directions = require("./Directions"); +const Hyperparams = require("./Hyperparameters"); // A local cell is a lightweight container for a cell in an organism. It does not directly exist in the grid class LocalCell{ @@ -7,6 +9,32 @@ class LocalCell{ this.loc_col = loc_col; this.loc_row = loc_row; } + + rotatedCol(dir){ + switch(dir){ + case Directions.up: + return this.loc_col; + case Directions.down: + return this.loc_col * -1; + case Directions.left: + return this.loc_row; + case Directions.right: + return this.loc_row * -1; + } + } + + rotatedRow(dir){ + switch(dir){ + case Directions.up: + return this.loc_row; + case Directions.down: + return this.loc_row * -1; + case Directions.left: + return this.loc_col * -1; + case Directions.right: + return this.loc_col; + } + } } module.exports = LocalCell; diff --git a/src/Organism.js b/src/Organism.js index a967622..ff7e6f1 100644 --- a/src/Organism.js +++ b/src/Organism.js @@ -4,6 +4,7 @@ const GridMap = require("./GridMap"); const LocalCell = require("./LocalCell"); const Neighbors = require("./Neighbors"); const Hyperparams = require("./Hyperparameters"); +const Directions = require("./Directions"); const directions = [[0,1],[0,-1],[1,0],[-1,0]] @@ -18,10 +19,13 @@ class Organism { this.cells = []; this.is_producer = false; this.is_mover = false; - this.direction = this.getRandomDirection(); + this.direction = Directions.up; + this.rotation = Directions.up; + this.can_rotate = Hyperparams.moversCanRotate; this.move_count = 0; - this.move_range = 5; + this.move_range = 4; this.mutability = 5; + this.damage = 0; if (parent != null) { this.inherit(parent); } @@ -38,6 +42,8 @@ class Organism { } removeCell(c, r) { + if (c == 0 && r == 0) + return false; var check_change = false; for (var i=0; i 1) { - this.cells.splice(Math.floor(Math.random() * this.cells.length), 1); - return true; + cell = this.cells[Math.floor(Math.random() * this.cells.length)]; + mutated = this.removeCell(cell.loc_col, cell.loc_row); } } if (this.is_mover) { this.move_range += Math.floor(Math.random() * 4) - 2; + if (this.move_range <= 0){ + this.move_range = 1; + } } - return false; + return mutated; } - attemptMove(direction) { + attemptMove() { + var direction = Directions.scalars[this.direction]; var direction_c = direction[0]; var direction_r = direction[1]; var new_c = this.c + direction_c; var new_r = this.r + direction_r; if (this.isClear(new_c, new_r)) { for (var cell of this.cells) { - var real_c = this.c + cell.loc_col; - var real_r = this.r + cell.loc_row; + var real_c = this.c + cell.rotatedCol(this.rotation); + var real_r = this.r + cell.rotatedRow(this.rotation); this.env.changeCell(real_c, real_r, CellTypes.empty, null); } this.c = new_c; @@ -173,13 +196,30 @@ class Organism { return false; } - getRandomDirection(){ - return directions[Math.floor(Math.random() * directions.length)]; + attemptRotate() { + if(!this.can_rotate){ + this.direction = Directions.getRandomDirection(); + this.move_count = 0; + return true; + } + var new_rotation = Directions.getRandomDirection(); + if(this.isClear(this.c, this.r, new_rotation)){ + for (var cell of this.cells) { + var real_c = this.c + cell.rotatedCol(this.rotation); + var real_r = this.r + cell.rotatedRow(this.rotation); + this.env.changeCell(real_c, real_r, CellTypes.empty, null); + } + this.rotation = new_rotation; + this.direction = Directions.getRandomDirection(); + this.updateGrid(); + this.move_count = 0; + return true; + } + return false; } // assumes either c1==c2 or r1==r2, returns true if there is a clear path from point a to b isStraightPath(c1, r1, c2, r2, parent){ - // TODO FIX!!! if (c1 == c2) { if (r1 > r2){ var temp = r2; @@ -214,9 +254,9 @@ class Organism { return cell != null && (cell.type == CellTypes.empty || cell.owner == this || cell.owner == parent || cell.type == CellTypes.food); } - isClear(col, row) { + isClear(col, row, rotation=this.rotation) { for(var loccell of this.cells) { - var cell = this.getRealCell(loccell, col, row); + var cell = this.getRealCell(loccell, col, row, rotation); if(cell == null || cell.type != CellTypes.empty && cell.owner != this) { return false; } @@ -224,10 +264,17 @@ class Organism { return true; } + harm() { + this.damage++; + if (this.damage >= this.maxHealth() || Hyperparams.instaKill) { + this.die(); + } + } + die() { for (var cell of this.cells) { - var real_c = this.c + cell.loc_col; - var real_r = this.r + cell.loc_row; + var real_c = this.c + cell.rotatedCol(this.rotation); + var real_r = this.r + cell.rotatedRow(this.rotation); this.env.changeCell(real_c, real_r, CellTypes.food, null); } this.living = false; @@ -235,14 +282,13 @@ class Organism { updateGrid() { for (var cell of this.cells) { - var real_c = this.c + cell.loc_col; - var real_r = this.r + cell.loc_row; + var real_c = this.c + cell.rotatedCol(this.rotation); + var real_r = this.r + cell.rotatedRow(this.rotation); this.env.changeCell(real_c, real_r, cell.type, this); } } update() { - // this.food_collected++; this.lifetime++; if (this.lifetime > this.lifespan()) { this.die(); @@ -259,19 +305,18 @@ class Organism { } if (this.is_mover) { this.move_count++; - var success = this.attemptMove(this.direction); - if (this.move_count > this.move_range || !success){ - this.move_count = 0; - this.direction = this.getRandomDirection() + var moved = this.attemptMove(); + if (this.move_count > this.move_range){ + this.attemptRotate(); } } return this.living; } - getRealCell(local_cell, c=this.c, r=this.r){ - var real_c = c + local_cell.loc_col; - var real_r = r + local_cell.loc_row; + getRealCell(local_cell, c=this.c, r=this.r, rotation=this.rotation){ + var real_c = c + local_cell.rotatedCol(rotation); + var real_r = r + local_cell.rotatedRow(rotation); return this.env.grid_map.cellAt(real_c, real_r); }