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 10-06-2010, 05:13 AM
salesman salesman is offline
Finger lickin' good.
salesman's Avatar
Join Date: Nov 2008
Location: Colorado
Posts: 1,865
salesman has much to be proud ofsalesman has much to be proud ofsalesman has much to be proud ofsalesman has much to be proud ofsalesman has much to be proud ofsalesman has much to be proud ofsalesman has much to be proud of
MazeGenerator

I had an assignment for school to write a maze generator in Java, and decided to convert it to GS2. The purpose of the assignment was to apply a disjoint set data structure to something practical, like maze generating. It might not be the most efficient algorithm, and it might not generate the most difficult mazes, but it is fast and it works. The algorithm is essentially a randomized Kruskal's algorithm, and has an O(n) running time proportional to the number of spaces in your maze.

How it works:
  • You start out with a grid where each space is closed off with walls.
  • You then look at each wall in some random order and have two cases to consider:
    1. There is already some path that connects the two spaces on either side of this wall, leave this wall where it is.
    2. There is no path that connects the two spaces, remove this wall to make a path.
  • We can tell if there is a path between two spaces if they are in the same set, so whenever we remove a wall, we union the two sets containing each space.
  • After looking at each wall, you should be able to reach any space from any other space in the maze because they are all in the same set.

See it in action:

When generating a maze in the video, I'm actually generating 10 mazes with a .05 second sleep between each one to give it a cool "loading" effect. So it really is fast...seemingly instant when generating a full-level maze.

Suggested Reading:
Implementation:
Simply put the code in a DBNPC and call this function:
PHP Code:
  MazeGenerator.generateMaze(level_objstart_xstart_ywidthheight);

  
// EXAMPLE:

  // This code will generate a maze in salesman's level
  // The maze will cover the entire level
  
temp.pl findplayer("salesman");
  
MazeGenerator.generateMaze(temp.pl.level006464); 
When the maze is generated, there will be exactly one path between any two points in the maze. Because of this, you can pick any two points to be your start and end locations of the maze. I recommend choosing opposite corners!

The code:
MazeGenerator.txt

NOTE: I have not tested this on a gmap!
__________________
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 03:06 AM.


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