From 9f13068cdaedaa99320596ff4868812e745d88dc Mon Sep 17 00:00:00 2001 From: MaxRobinsonTheGreat Date: Sun, 12 Jul 2020 18:48:51 -0600 Subject: [PATCH] more control panel stuff --- dist/bundle.js | 245 +---------------------------------- dist/html/index.html | 18 ++- src/Cell.js | 8 +- src/CellTypes.js | 2 +- src/ControlPanel.js | 36 ++++- src/Environment.js | 33 ++++- src/EnvironmentController.js | 2 +- src/GridMap.js | 3 +- src/Hyperparameters.js | 35 ++++- src/Organism.js | 28 ++-- 10 files changed, 135 insertions(+), 275 deletions(-) diff --git a/dist/bundle.js b/dist/bundle.js index 3459b69..3380b98 100644 --- a/dist/bundle.js +++ b/dist/bundle.js @@ -1,244 +1 @@ -/******/ (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\nvar 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 // console.log(prob)\r\n for (var loc of Hyperparams.growableNeighbors){\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 && Math.random() * 100 <= prob){\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 || n_cell.owner == self.owner || !n_cell.owner.living || n_cell.type == CellTypes.armor) \r\n return;\r\n var should_die = n_cell.type == CellTypes.killer; // has to be calculated before death\r\n n_cell.owner.die();\r\n if (should_die){\r\n self.owner.die();\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', 'blue', '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("var Hyperparams = __webpack_require__(/*! ./Hyperparameters */ \"./src/Hyperparameters.js\");\r\nvar 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 console.log(this.fps)\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 }\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 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 // this.env_controller.performModeAction();\r\n }\r\n\r\n}\r\n\r\n\r\nmodule.exports = ControlPanel;\n\n//# sourceURL=webpack:///./src/ControlPanel.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 }\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.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, 1, 1);\r\n org.addCell(CellTypes.producer, 0, 0);\r\n // org.addCell(CellTypes.mouth, 1, -1);\r\n this.addOrganism(org);\r\n }\r\n\r\n addOrganism(organism) {\r\n organism.updateGrid();\r\n this.organisms.push(organism);\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 }\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("var 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\nvar Hyperparams = {\r\n lifespanMultiplier: 100,\r\n foodProdProb: 1,\r\n killableNeighbors: Neighbors.adjacent,\r\n edibleNeighbors: Neighbors.adjacent,\r\n growableNeighbors: Neighbors.adjacent,\r\n\r\n\r\n // calculates the optimal ratio where a producer cell is most likely to produce 1 food in its lifespan.\r\n calcProducerFoodRatio : function(lifespan_fixed=true) {\r\n if (lifespan_fixed) {\r\n // change the foodProdProb\r\n this.foodProdProb = 100 / this.lifespanMultiplier;\r\n }\r\n else {\r\n // change the lifespanMultiplier\r\n this.lifespanMultiplier = Math.floor(100 / this.foodProdProb);\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\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\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 { producer } = __webpack_require__(/*! ./CellTypes */ \"./src/CellTypes.js\");\r\nconst Neighbors = __webpack_require__(/*! ./Neighbors */ \"./src/Neighbors.js\");\r\nvar Hyperparams = __webpack_require__(/*! ./Hyperparameters */ \"./src/Hyperparameters.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 = this.getRandomDirection();\r\n this.move_count = 0;\r\n this.move_range = 5;\r\n this.mutability = 5;\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 var check_change = false;\r\n for (var i=0; i 1) {\r\n this.cells.splice(Math.floor(Math.random() * this.cells.length), 1);\r\n return true;\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 }\r\n return false;\r\n }\r\n\r\n attemptMove(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.loc_col;\r\n var real_r = this.r + cell.loc_row;\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 getRandomDirection(){\r\n return directions[Math.floor(Math.random() * directions.length)];\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 // TODO FIX!!!\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) {\r\n for(var loccell of this.cells) {\r\n var cell = this.getRealCell(loccell, col, row);\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 die() {\r\n for (var cell of this.cells) {\r\n var real_c = this.c + cell.loc_col;\r\n var real_r = this.r + cell.loc_row;\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.loc_col;\r\n var real_r = this.r + cell.loc_row;\r\n this.env.changeCell(real_c, real_r, cell.type, this);\r\n }\r\n }\r\n\r\n update() {\r\n // this.food_collected++;\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 success = this.attemptMove(this.direction);\r\n if (this.move_count > this.move_range || !success){\r\n this.move_count = 0;\r\n this.direction = this.getRandomDirection()\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){\r\n var real_c = c + local_cell.loc_col;\r\n var real_r = r + local_cell.loc_row;\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 +!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 diff --git a/dist/html/index.html b/dist/html/index.html index 51554a1..6cd1712 100644 --- a/dist/html/index.html +++ b/dist/html/index.html @@ -25,17 +25,29 @@

Stats

Organism count:

Highest count:

+

Average Mutation Rate:

Hyperparameters

- +
+

+

Mutation Probabilities

+ + +
+ + +
+ + +
@@ -44,6 +56,10 @@ +

+ + +
diff --git a/src/Cell.js b/src/Cell.js index 4a1d7f5..3b8a725 100644 --- a/src/Cell.js +++ b/src/Cell.js @@ -1,5 +1,5 @@ const CellTypes = require("./CellTypes"); -var Hyperparams = require("./Hyperparameters"); +const Hyperparams = require("./Hyperparameters"); // A cell exists in a grid system. class Cell{ @@ -61,12 +61,12 @@ function growFood(self, env){ if (self.owner.is_mover) return; var prob = Hyperparams.foodProdProb; - // console.log(prob) - for (var loc of Hyperparams.growableNeighbors){ + if (Math.random() * 100 <= prob){ + var loc = Hyperparams.growableNeighbors[Math.floor(Math.random() * Hyperparams.growableNeighbors.length)] var c=loc[0]; var r=loc[1]; var cell = env.grid_map.cellAt(self.col+c, self.row+r); - if (cell != null && cell.type == CellTypes.empty && Math.random() * 100 <= prob){ + if (cell != null && cell.type == CellTypes.empty){ env.changeCell(self.col+c, self.row+r, CellTypes.food, null); return; } diff --git a/src/CellTypes.js b/src/CellTypes.js index 8d2d7a5..d579ac8 100644 --- a/src/CellTypes.js +++ b/src/CellTypes.js @@ -8,7 +8,7 @@ const CellTypes = { killer: 6, armor: 7, colors: ['#121D29', 'green', 'gray', 'orange', 'white', 'blue', 'red', 'purple'], - getRandomLivingType: function(){ + getRandomLivingType: function() { return Math.floor(Math.random() * 5) + 3; } } diff --git a/src/ControlPanel.js b/src/ControlPanel.js index 1805365..7790fc9 100644 --- a/src/ControlPanel.js +++ b/src/ControlPanel.js @@ -1,5 +1,5 @@ -var Hyperparams = require("./Hyperparameters"); -var Modes = require("./ControlModes"); +const Hyperparams = require("./Hyperparameters"); +const Modes = require("./ControlModes"); const CellTypes = require("./CellTypes"); class ControlPanel { @@ -59,6 +59,25 @@ class ControlPanel { Hyperparams.lifespanMultiplier = lifespan; } }.bind(this)); + $('.mut-prob').change( function() { + switch(this.id){ + case "add-prob": + Hyperparams.addProb = this.value; + Hyperparams.balanceMutationProbs(1); + break; + case "change-prob": + Hyperparams.changeProb = this.value; + Hyperparams.balanceMutationProbs(2); + break; + case "remove-prob": + Hyperparams.removeProb = this.value; + Hyperparams.balanceMutationProbs(3); + break; + } + $('#add-prob').val(Math.floor(Hyperparams.addProb)); + $('#change-prob').val(Math.floor(Hyperparams.changeProb)); + $('#remove-prob').val(Math.floor(Hyperparams.removeProb)); + }); } defineModeControls() { @@ -81,6 +100,17 @@ class ControlPanel { $(".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(change_val) { @@ -96,7 +126,7 @@ class ControlPanel { if (org_count > this.organism_record) this.organism_record = org_count; $('#org-record').text("Highest count: " + this.organism_record); - // this.env_controller.performModeAction(); + $('#avg-mut').text("Average Mutation Rate: " + Math.round(this.engine.env.averageMutability() * 100) / 100); } } diff --git a/src/Environment.js b/src/Environment.js index 1cf1823..5ec56ed 100644 --- a/src/Environment.js +++ b/src/Environment.js @@ -15,6 +15,8 @@ class Environment{ this.grid_map = new GridMap(this.grid_cols, this.grid_rows, cell_size); this.renderer.renderFullGrid(); this.organisms = []; + this.walls = []; + this.total_mutability = 0; } update(delta_time) { @@ -35,6 +37,7 @@ class Environment{ removeOrganisms(org_indeces) { for (var i of org_indeces.reverse()){ + this.total_mutability -= this.organisms[i].mutability; this.organisms.splice(i, 1); } } @@ -44,19 +47,47 @@ class Environment{ 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, -1, -1); this.addOrganism(org); } addOrganism(organism) { organism.updateGrid(); + this.total_mutability += organism.mutability; this.organisms.push(organism); } + averageMutability() { + if (this.organisms.length < 1) + return 0; + return this.total_mutability / this.organisms.length; + } + changeCell(c, r, type, owner) { this.grid_map.setCellType(c, r, type); this.grid_map.setCellOwner(c, r, owner); this.renderer.addToRender(this.grid_map.cellAt(c, r)); + if(type == CellTypes.wall) + this.walls.push(this.grid_map.cellAt(c, r)); + } + + clearWalls() { + for(var wall of this.walls) + this.changeCell(wall.col, wall.row, CellTypes.empty, null); + } + + clearOrganisms() { + for (var org of this.organisms) + org.die(); + this.organisms = []; + } + + reset() { + this.organisms = []; + this.grid_map.fillGrid(CellTypes.empty); + this.renderer.renderFullGrid(); + this.total_mutability = 0; + this.OriginOfLife(); } } diff --git a/src/EnvironmentController.js b/src/EnvironmentController.js index a15e967..8c7b239 100644 --- a/src/EnvironmentController.js +++ b/src/EnvironmentController.js @@ -1,4 +1,4 @@ -var Modes = require("./ControlModes"); +const Modes = require("./ControlModes"); const CellTypes = require("./CellTypes"); class EnvironmentController{ diff --git a/src/GridMap.js b/src/GridMap.js index 153879d..5d353cd 100644 --- a/src/GridMap.js +++ b/src/GridMap.js @@ -19,9 +19,10 @@ class GridMap { } fillGrid(type) { - for (var col of grid) { + for (var col of this.grid) { for (var cell of col){ cell.setType(type); + cell.owner = null; } } } diff --git a/src/Hyperparameters.js b/src/Hyperparameters.js index 68ae58f..ee0e1cd 100644 --- a/src/Hyperparameters.js +++ b/src/Hyperparameters.js @@ -1,22 +1,47 @@ const Neighbors = require("./Neighbors"); -var Hyperparams = { +const Hyperparams = { lifespanMultiplier: 100, - foodProdProb: 1, + foodProdProb: 4, + foodProdProbScalar: 4, killableNeighbors: Neighbors.adjacent, edibleNeighbors: Neighbors.adjacent, growableNeighbors: Neighbors.adjacent, + + useGlobalMutability: false, + globalMutability: 0, + addProb: 33, + changeProb: 33, + removeProb: 33, - // calculates the optimal ratio where a producer cell is most likely to produce 1 food in its lifespan. + // 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) { // change the foodProdProb - this.foodProdProb = 100 / this.lifespanMultiplier; + this.foodProdProb = (100 / this.lifespanMultiplier) * this.foodProdProbScalar; } else { // change the lifespanMultiplier - this.lifespanMultiplier = Math.floor(100 / this.foodProdProb); + this.lifespanMultiplier = Math.floor(100 / (this.foodProdProb/this.foodProdProbScalar)); + } + }, + + balanceMutationProbs : function(choice) { + if (choice == 1) { + var remaining = 100 - this.addProb; + this.changeProb = remaining/2; + this.removeProb = remaining/2; + } + else if (choice == 2) { + var remaining = 100 - this.changeProb; + this.addProb = remaining/2; + this.removeProb = remaining/2; + } + else { + var remaining = 100 - this.removeProb; + this.changeProb = remaining/2; + this.addProb = remaining/2; } } } diff --git a/src/Organism.js b/src/Organism.js index 11cc0fa..a967622 100644 --- a/src/Organism.js +++ b/src/Organism.js @@ -2,9 +2,8 @@ const CellTypes = require("./CellTypes"); const Cell = require("./Cell"); const GridMap = require("./GridMap"); const LocalCell = require("./LocalCell"); -const { producer } = require("./CellTypes"); const Neighbors = require("./Neighbors"); -var Hyperparams = require("./Hyperparameters"); +const Hyperparams = require("./Hyperparameters"); const directions = [[0,1],[0,-1],[1,0],[-1,0]] @@ -89,7 +88,10 @@ class Organism { //produce mutated child //check nearby locations (is there room and a direct path) var org = new Organism(0, 0, this.env, this); - if (Math.random() * 100 <= 3) { + var prob = this.mutability; + if (Hyperparams.useGlobalMutability) + prob = Hyperparams.globalMutability; + if (Math.random() * 100 <= this.mutability) { org.mutate(); } if (Math.random() <= 0.5) @@ -120,27 +122,25 @@ class Organism { } mutate() { - var choice = Math.floor(Math.random() * 3); - if (choice == 0) { + var choice = Math.floor(Math.random() * 100); + if (choice <= Hyperparams.addProb) { // add cell var type = CellTypes.getRandomLivingType(); var num_to_add = Math.floor(Math.random() * 3) + 1; - // for (var i=0; i 1) { this.cells.splice(Math.floor(Math.random() * this.cells.length), 1);