cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              var dim = 64;

var field = new field2D(dim);
var past = new field2D(dim);

// three possible states:
var empty = [0,0];
var tree = [0,1];
var burning = [1,0];

function maketree(cell) {
  //cell[0] = 0;
  //cell[1] = 1;
  return [0, 1, 0];
}

function makefire(cell) {
  //cell[0] = 1;
  return [1, 0, 0];
}

function makeempty(cell) {
  //cell[0] = 0;
  //cell[1] = 0;
  
  return [0, 0, 0];
}

function isempty(cell) {
  //return cell == 0;
  return cell[0] == 0 && cell[1] == 0;
}

function istree(cell) {
  //return cell == 0.5;
  return cell[1] == 1;
}

function isburning(cell) {
  //return cell == 1;
  return cell[0] == 1;
}

// the chance of an empty cell regrowing trees by expansion:
var growth_probability = 1/100;
// the chance of an empty cell regrowing trees by random sporing:
var spore_probability = 1/100000;
// the chance of lighting striking a cell:
var lightning_probability = 1/100000;

// how to r}er the scene
function draw(ctx)	{
	// draw the field (by default it fills the canvas):
	field.draw();
}

// the rule for an individual cell (at position x, y) in the field:
function transition(x, y) {

	// check out the neighbors' previous states:
	var N  = past.cell(x  , y+1);
	var NE = past.cell(x+1, y+1);
	var E  = past.cell(x+1, y  );
	var SE = past.cell(x+1, y-1);
	var S  = past.cell(x  , y-1);
	var SW = past.cell(x-1, y-1);
	var W  = past.cell(x-1, y  );
	var NW = past.cell(x-1, y+1);
	
	// true if (any neighbour is a tree:
	var neartree = 
      istree(N) || istree(S) || istree(W) || istree(E) 
  || istree(NE) || istree(NW) || istree(SW) || istree(SE);
	
	// true if (any neighbour is burning:
	var nearburning = 
      isburning(N) || isburning(S) || isburning(W) || isburning(E) 
  || isburning(NW) || isburning(NE) || isburning(SW) || isburning(SE);
	
	// check my own previous state:
	var C = past.cell(x, y);
	
	if (isempty(C)) {
		// are any neighbors trees?
		if (neartree) {			
			// chance of regrowing:
			if (random() < growth_probability) {
				C = tree;
			}
		} else if  (random() < spore_probability) {
			// smaller chance of propagation by seeding:
			C = tree;
		}
	} else if  (istree(C)) {
		// are any neighbors burning?
		if (nearburning) {
			// if (any neighbors are burning, start burning too:
			C = burning;
		
		} else if  (random() < lightning_probability) {		
			// otherwise, there's a small chance of catching fire due to atmostpheric conditions:
			C = burning;
		}
	} else if (isburning(C)) {
		// a burning tree cell becomes an empty cell
		C = empty;
	} 
	
	// return the new state:
	return [C[0], C[1]];
}

function update() {
	var tmp = past;
	past = field;
	field = tmp;
	
	field.set(transition);
} 
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console