Graal Forums  

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

Reply
 
Thread Tools Search this Thread Display Modes
  #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
  #2  
Old 07-26-2008, 09:54 PM
napo_p2p napo_p2p is offline
oh snaps
napo_p2p's Avatar
Join Date: Sep 2003
Location: Pismo Beach, California
Posts: 2,118
napo_p2p has a spectacular aura aboutnapo_p2p has a spectacular aura about
Send a message via AIM to napo_p2p Send a message via MSN to napo_p2p
Nifty! I like.
__________________
Scito hoc super omnia.
Haec vita est tua una sola.
Dum vita superest, utere maxime quoque puncto, momento, et hora quae habes.
Tempus neminem non manet.
Noli manere tempus.
Carpe Diem

Seize the Day.
Reply With Quote
  #3  
Old 07-26-2008, 11:55 PM
Mark Sir Link Mark Sir Link is offline
Kevin Azite
Mark Sir Link's Avatar
Join Date: Sep 2005
Posts: 1,489
Mark Sir Link is just really niceMark Sir Link is just really nice
Send a message via AIM to Mark Sir Link
gosper glider please
Reply With Quote
Reply


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 02:39 PM.


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