Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 07-26-2008, 09:30 PM
DrakilorP2P DrakilorP2P is offline
Registered User
DrakilorP2P's Avatar
Join Date: Apr 2006
Posts: 755
DrakilorP2P is just really niceDrakilorP2P is just really nice
Random dungeon generation with cellular automata

This one should be pretty straightforward. Each cell have a finite amount of states. Every generation, all cells are simultaneously changed according to a set of rules which are dependant on the cell's "neighbourhood."

An example of a cellular automaton is Conway's Game of Life, which you may be familiar with.
Its rules as per copy pasta:
Quote:
1. Any live cell with fewer than two live neighbours dies, as if by loneliness.
2. Any live cell with more than three live neighbours dies, as if by overcrowding.
3. Any live cell with two or three live neighbours lives, unchanged, to the next generation.
4. Any dead cell with exactly three live neighbours comes to life.
PHP Code:
//true: alive cell
//false: dead cell

function onCreated()
{
  
this.grid newGrid();
  
this.grid randomizeGrid(this.grid0.45);
  
  
// Create a glider for use with conway's game of life
  /*this.grid = newGrid();
  this.grid[1][1] = true;
  this.grid[2][2] = true;
  this.grid[0][3] = true;
  this.grid[1][3] = true;
  this.grid[2][3] = true;*/
  
  
scheduleEvent(0.1"loopLimitIsEvil"false);
  
scheduleEvent(0.2"loopLimitIsEvil"false);
  
scheduleEvent(0.3"loopLimitIsEvil"false);
  
scheduleEvent(0.4"loopLimitIsEvil"false);
  
scheduleEvent(0.5"loopLimitIsEvil"false);
  
scheduleEvent(0.6"loopLimitIsEvil"true);
}

function 
onLoopLimitIsEvil(save)
{
  if (
savewriteToFile(this.grid"temp/conway.txt");
  else 
this.grid iterateCellularAutomaton(this.grid"cave");
  
}

function 
newGrid()
{
  
temp.grid = new[64][64];
  
  return 
temp.grid;
}

function 
iterateCellularAutomaton(gridfunctionName)
{
  
temp.newGrid newGrid();
  
  for (
temp.x=0temp.x<64temp.x++) {
    for (
temp.y=0temp.y<64temp.y++) {
      
temp.newGrid[temp.x][temp.y] = makevar(functionName)(gridtemp.xtemp.y);
    }
  }
  
  return 
temp.newGrid;
}

function 
randomizeGrid(gridratio)
{
  
temp.RNG = new MRandomR250();
  
  for (
temp.x=0temp.x<64temp.x++) {
    for (
temp.y=0temp.y<64temp.y++) {
      
grid[temp.x][temp.y] = temp.RNG.randFloat() < ratio;
    }
  }
  
  return 
grid;
}

function 
writeToFile(gridfilePath)
{
  
temp.string "GLEVNW01\n";
  
  for (
temp.y=0temp.y<64temp.y++) {
    
temp.string @= "BOARD 0 " temp." 64 0 ";
    for (
temp.x=0temp.x<64temp.x++) {
      if (
grid[temp.x][temp.y] == truetemp.string @= "Ag";
      else 
temp.string @= "YG";
    }
    
    
temp.string @= "\n";
  }
  
  
temp.string.saveString(filePath0);
}

function 
countNumberOfNeighbours(gridxy)
{
  
//We're assuming that anything outside the level boundaries are walls. No torus for us.
  
temp.numNeighbours 0;
  if (
grid[x-1][y-1] == true || x-|| y-0temp.numNeighbours ++;
  if (
grid[x-1][y] == true || x-0temp.numNeighbours ++;
  if (
grid[x-1][y+1] == true || x-|| y+63temp.numNeighbours ++;
  if (
grid[x][y-1] == true || y-0temp.numNeighbours ++;
  if (
grid[x][y+1] == true || y+63temp.numNeighbours ++;
  if (
grid[x+1][y-1] == true || x+63 || y-0temp.numNeighbours ++;
  if (
grid[x+1][y] == true || x+63temp.numNeighbours ++;
  if (
grid[x+1][y+1] == true || x+63 || y+63temp.numNeighbours ++;
  
  return 
temp.numNeighbours;
}

function 
cave(gridxy)
{
  
temp.numNeighbours countNumberOfNeighbours(gridxy);
  
  if (
grid[x][y] == true && temp.numNeighbours >= 4) return true;
  if (
grid[x][y] == false && temp.numNeighbours >= 5) return true;
  return 
false;
}

//Conway's game of life.
function conwayLife(gridxy)
{
  
temp.numNeighbours countNumberOfNeighbours(gridxy);
  
  if (
grid[x][y] == true && temp.numNeighbours in {23}) return true;
  if (
grid[x][y] == false && temp.numNeighbours == 3) return true;
  return 
false;



The results, to say the least, are repulsive. Unless you're coding GraalHack, you'll need to do some work yourself to get any useful levels.

Obviously though, there are all sorts of other exciting things you can do with this, such as making grass grow.

PHP Code:
function makeGrassGrow(gridxy)
{
  
temp.numNeighbours countNumberOfNeighbours(gridxy);
  
  
temp.RNG = new MRandomR250();
  if (
temp.numNeighbours 0) {
    if (
temp.RNG.randInt(110) == 1) return true;
  }
  if (
temp.numNeighbours 1) {
    if (
temp.RNG.randInt(15) == 1) return false;
  }
  if (
grid[x][y] == true) return true;
  return 
false;

Attached Thumbnails
Click image for larger version

Name:	conway.png
Views:	310
Size:	49.3 KB
ID:	45374  
Reply With Quote
 


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 11:49 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.