enum{XSIZ=255, YSIZ=XSIZ} // size of forest enum{EMPTY=0, TREE=1, BURN=2} // possible states of a cell static prob_ignite = .000001; // very rare, but remember we have many trees. static prob_spread = .25; // Fire spread speed/probability if neighbor on fire static prob_sprout = 0.25; // probability of new tree to sprout static MAX_NEIGHBORS = 6; // tree refuses to sprout if overcrowded static forest[2][YSIZ][XSIZ]; // state of pixel static fuel[2][YSIZ][XSIZ]; // stores fuel (wood) 0-255 static heat[2][YSIZ][XSIZ]; // tree refuses to spout if heat!=0, also, sets draw color. static arr_numburn[YSIZ][XSIZ]; // number of burning trees for this cell static arr_numtree[YSIZ][XSIZ]; // number of neighbor trees for this cell static xoff[8] = {-1,+0,+1,-1,/*NA*/1,-1,+0,+1}; // offsets to find 8-connected neighbors static yoff[8] = {-1,-1,-1,+0,/*NA*/0,+1,+1,+1}; () { // Main in evaldraw scripts is a unnamed function. static otim; tim = klock(); // Time since program start in seconds. dt=tim-otim; // Deltatime. 1/dt is FPS. 0 in first frame. otim=tim; // store old time for next dt. simulate(); // simulate and draw are coupled, since draw also ping-pongs state. draw(); setcol(0); fillrect(0,YSIZ,XSIZ,15); setcol(0xffffff); moveto(0,YSIZ); printf("%4.0ffps generation %5.0f", 1 /dt, numframes); if (bstatus>0) setFire(mousx,mousy); }// end main draw() { for(y=0; y XSIZ-1)continue; if (ypos<0 || ypos > YSIZ-1)continue; cell = forest[1][ypos][xpos]; if (cell==BURN) numburn++; else if (cell==TREE) numtree++; } arr_numburn[y][x] = numburn; arr_numtree[y][x] = numtree; } } fillrect(x0,y0,w,h) { x0=int(x0); y0=int(y0); w=int(w) + 1; h=int(h); for(y=y0;y<=y0+h;y++) { moveto(x0,y); lineto(x0+w,y); } } simulate() { for(y=0; y 0 && rand < prob_spread) setFire(x,y); else if(cellfuel < 255) fuel[1][y][x] = cellfuel + 1; } else if (cell == EMPTY) { if ( celltemp > 0 ) heat[1][y][x] = celltemp - 1; else if (numburn==0 && rand < prob_sprout && numtree <= MAX_NEIGHBORS) setTree(x,y); } } } // end sim setFire(x,y) { forest[1][y][x] = BURN; } setTree(x,y) { forest[1][y][x] = TREE; }