Skip to content

Instantly share code, notes, and snippets.

@StringEpsilon
Created July 2, 2023 00:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StringEpsilon/e61acdaafb71125a43df07cecf801b37 to your computer and use it in GitHub Desktop.
Save StringEpsilon/e61acdaafb71125a43df07cecf801b37 to your computer and use it in GitHub Desktop.
Lotgd maze generator.
<?
/*
Snippet from Legend of the Green Dragon version 0.9.7.
Code is subject to the GPLv2
License text available at: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
function maze_initalize()
{
global $session;
$session['maze_map'] = '';
$nVisited = 1;
$nTotal = 144;
$nCurr = e_rand(1,$nTotal)-1;
$stack = array();
$maze = array_fill(0, $nTotal, 15);
while( $nVisited < $nTotal ){
$find = array();//pos, drop wallcurr, drop wallthis
$found = 0;
for($i=0; $i < 4; $i++){
switch($i){
case 0:
if( $nCurr % 12 ){//westwand?
if( $maze[ $nCurr-1 ] == 15){
array_push($find, array($nCurr-1, WALL_WEST, WALL_EAST));
$found++;
}
}
break;
case 1:
if( $nCurr < 132 ){ //südwand?
if( $maze[ $nCurr+12 ] == 15 ){
array_push($find, array($nCurr+12, WALL_SOUTH, WALL_NORTH));
$found++;
}
}
break;
case 2:
if( ($nCurr+1) % 12){ //ostwand?
if( $maze[ $nCurr+1 ] == 15 ){
array_push($find, array($nCurr+1, WALL_EAST, WALL_WEST));
$found++;
}
}
break;
case 3:
if( $nCurr > 11 ){ //nordwand?
if( $maze[ $nCurr-12 ] == 15 ){
array_push($find, array($nCurr-12, WALL_NORTH, WALL_SOUTH));
$found++;
}
}
break;
}
}
if($found){
$next = $find[e_rand(0,$found-1)];
$maze[ $next[0] ] ^= $next[2];
$maze[ $nCurr ] ^= $next[1];
array_push($stack, $nCurr);
$nCurr = $next[0];
$nVisited++;
}
else{
$nCurr = array_pop($stack);
}
}
$randomdrop = e_rand(5,15);
for($i=0; $i<$randomdrop; ++$i){
$dropwall = (1<<e_rand(0,3));
$dropfield = e_rand(1,143);
if( !($maze[ $dropfield ] & $dropwall) ){
continue;
}
switch( $dropwall ){
case WALL_NORTH:
if( $dropfield > 11 ){ //nordwand?
$maze[ $dropfield ] ^= $dropwall;
$maze[ $dropfield-12 ] ^= WALL_SOUTH;
}
break;
case WALL_EAST:
if( ($dropfield+1) % 12){ //ostwand?
$maze[ $dropfield ] ^= $dropwall;
$maze[ $dropfield+1 ] ^= WALL_WEST;
}
break;
case WALL_SOUTH:
if( $dropfield < 132 ){ //südwand?
$maze[ $dropfield ] ^= $dropwall;
$maze[ $dropfield+12 ] ^= WALL_NORTH;
}
break;
case WALL_WEST:
if( $dropfield % 12 ){//westwand?
$maze[ $dropfield ] ^= $dropwall;
$maze[ $dropfield-1 ] ^= WALL_EAST;
}
break;
}
}
$maze[ 0 ] ^= WALL_NORTH;
$maze[ 143 ] ^= WALL_SOUTH;
$session['maze_map'] = implode(",", $maze);
user_set_aei(array('maze_map' => $session['maze_map']));
$session['user']['maze_visited'] = implode("", array_fill(0,$nTotal,0));
$session['user']['maze_visited'][ 0 ] = 1;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment