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 Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console