Files
lifeEngine/dist/bundle.js
MaxRobinsonTheGreat 9f13068cda more control panel stuff
2020-07-12 18:48:51 -06:00

1 line
17 KiB
JavaScript

!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<t;l++){for(var n=[],h=0;h<e;h++){var a=new r(o,l,h,l*i,h*i);n.push(a)}this.grid.push(n)}}fillGrid(t){for(var e of this.grid)for(var i of e)i.setType(t),i.owner=null}cellAt(t,e){return this.isValidLoc(t,e)?this.grid[t][e]:null}setCellType(t,e,i){this.isValidLoc(t,e)&&this.grid[t][e].setType(i)}setCellOwner(t,e,i){this.isValidLoc(t,e)&&(this.grid[t][e].owner=i)}isValidLoc(t,e){return t<this.cols&&e<this.rows&&t>=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;s<this.cells.length;s++){if((o=this.cells[s]).loc_col==t&&o.loc_row==e){o.type!=r.producer&&o.type!=r.mover||(i=!0),this.cells.splice(s,1);break}}if(i)for(var o of(this.is_producer=!1,this.is_producer=!1,this.cells))this.checkProducerMover(o.type)}checkProducerMover(t){t==r.producer&&(this.is_producer=!0),t==r.mover&&(this.is_mover=!0)}inherit(t){for(var e of(this.move_range=t.move_range,this.mutability=t.mutability,t.cells))this.addCell(e.type,e.loc_col,e.loc_row)}foodNeeded(){return this.cells.length}lifespan(){return this.cells.length*l.lifespanMultiplier}reproduce(){var t=new h(0,0,this.env,this);this.mutability;l.useGlobalMutability&&l.globalMutability,100*Math.random()<=this.mutability&&t.mutate(),Math.random()<=.5?t.mutability++:(t.mutability--,t.mutability<1&&(t.mutability=1));var e=this.getRandomDirection(),i=e[0],r=e[1],s=2*Math.floor(2*Math.random()),o=this.c+i*this.cells.length*2+i*s,n=this.r+r*this.cells.length*2+r*s;t.isClear(o,n)&&t.isStraightPath(o,n,this.c,this.r,this)&&(t.c=o,t.r=n,this.env.addOrganism(t),t.updateGrid()),this.food_collected-=this.foodNeeded()}mutate(){var t=Math.floor(100*Math.random());if(t<=l.addProb){var e=r.getRandomLivingType(),i=(Math.floor(3*Math.random()),this.cells[Math.floor(Math.random()*this.cells.length)]),s=o.all[Math.floor(Math.random()*o.all.length)],n=i.loc_col+s[0],h=i.loc_row+s[1];return this.addCell(e,n,h)}if(t<=l.addProb+l.changeProb){var a=this.cells[Math.floor(Math.random()*this.cells.length)];return a.type=r.getRandomLivingType(),this.checkProducerMover(a.type),!0}return t<=l.addProb+l.changeProb+l.removeProb&&this.cells.length>1?(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)}}}]);