Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <head>
- <script>
- trees = 0;
- function treePlanter(){
- n = Math.random()*100;
- if (treeRatio>n){
- return true;
- }else{
- return false;
- }
- }
- function lumberjackPlanter(){
- n = Math.random()*100;
- if (lumberjackRatio*100>n){
- return true;
- }else{
- return false;
- }
- }
- function mapBuilder(dimension){
- map = [];
- for (i=0;i<dimension;i++){
- map[i] = [];
- for(j=0;j<dimension;j++){
- map[i][j] = treePlanter() ? {type: "T", age: 0}:{type: "G", age: 0};
- if (map[i][j].type!="T"){
- map[i][j] = lumberjackPlanter() ? {type: "L", age: 0}:{type: "G", age: 0};
- }
- }
- }
- return map;
- }
- function lumberAI(xCord, yCord){
- for(i=1;i<dimension;i++){
- var maxX = (xCord+i>dimension-1) ? dimension-1:xCord+i;
- var maxY = (yCord+i>dimension-1) ? dimension-1:yCord+i;
- var minX = (xCord-i<0) ? 0:xCord-i;
- var minY = (yCord-i<0) ? 0:yCord-i;
- var treeLocations = [];
- for(;minY-1<maxY;minY++){
- for(innerX = minX;innerX-1<maxX;innerX++){
- if(map[minY][innerX].type=="T"){
- treeLocations.push({x:innerX, y:minY});
- }
- }
- }
- if(treeLocations.length!=0){
- var location = treeLocations[Math.floor(Math.random()*(treeLocations.length))];
- if(location.x!=xCord){
- newX = (location.x>xCord) ? newX = xCord+1:newX = xCord-1;
- }else{
- newX = xCord;
- }
- if(location.y!=yCord){
- newY = (location.y>yCord) ? newY = yCord+1:newY = yCord-1;
- }else{
- newY = yCord;
- }
- return {x:newX, y:newY};
- }
- }
- }
- function createTable(tableData) {
- var table = document.createElement('table');
- var tableBody = document.createElement('tbody');
- table.id = "gameBody";
- tableData.forEach(function(rowData) {
- var row = document.createElement('tr');
- rowData.forEach(function(cellData) {
- var cell = document.createElement('td');
- cell.style.width = '10px';
- cell.style.height = '10px';
- switch(cellData.type){
- case "G":
- cell.bgColor = '#00FF00';
- break;
- case "T":
- cell.bgColor = '#004d00';
- break;
- case "L":
- cell.bgColor = '#4d3900';
- }
- cell.appendChild(document.createTextNode(''));
- row.appendChild(cell);
- });
- tableBody.appendChild(row);
- });
- table.appendChild(tableBody);
- document.body.appendChild(table);
- }
- function updateTable(tableData) {
- currentTrees = 0;
- currentLumberjacks = 0;
- treeRatio = trees<treeRatio*dimension*dimension;
- console.log(treePlanter());
- console.log(lumberjackPlanter());
- for(var i=0;i<dimension;i++){
- var row = document.getElementById("gameBody").rows[i].cells;
- for(var j=0;j<dimension;j++){
- cell = row[j];
- cellData = tableData[i][j];
- switch(cellData.type){
- case "G":
- if(treeRatio && treePlanter()){
- cell.bgColor = '#004d00';
- cellData.type = "T";
- cellData.age = 0;
- currentTrees++;
- }else{
- cell.bgColor = '#00FF00';
- }
- break;
- case "T":
- if(cellData.age>treeAge){
- cell.bgColor = '#00FF00';
- cellData.type = "G";
- cellData.age = 0;
- }else{
- cell.bgColor = '#004d00';
- currentTrees++;
- }
- break;
- case "L":
- nextPosition = lumberAI(j,i);
- if(tableData[nextPosition.y][nextPosition.x].type=="G"){
- row[nextPosition.x].bgColor = '#ff0000';
- tableData[nextPosition.y][nextPosition.x].type="L";
- tableData[nextPosition.y][nextPosition.x].age = cellData.age;
- cell.bgColor = '#00FF00';
- cellData.type = "G";
- cellData.age = 0;
- }else if (tableData[nextPosition.y][nextPosition.x].type=="T"){
- row[nextPosition.x].bgColor = '#ff0000';
- tableData[nextPosition.y][nextPosition.x].type="L";
- tableData[nextPosition.y][nextPosition.x].age = cellData.age;
- cell.bgColor = '#00FF00';
- cellData.type = "G";
- cellData.age = 0;
- currentTrees--;
- }else{
- cell.bgColor = '#ff0000';
- }
- break;
- }
- cellData.age++;
- }
- }
- trees = currentTrees;
- }
- function mainLoop(){
- updateTable(board);
- requestAnimationFrame(mainLoop);
- }
- </script>
- </head>
- <body>
- <script>
- dimension = 100;
- treeRatio = 0.001;
- lumberjackRatio = 0.001;
- treeAge = 100000;
- board = mapBuilder(dimension);
- createTable(board);
- mainLoop();
- </script>
- </body>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement