#!/usr/bin/perl -w

# Maze - build a random maze

# (c) Copyright by Tels 1999-2000. All Rights Reserved.
# 1999-12-05 new version using vroom, shorter, more clear and nearly perfect

use Cowgen::Bunch;                  # for a bunch of brushes
use Cowgen::Dromed;                 # for some Thief constants and specifics
use Cowgen::Texture;                # to get texture mappings
use Cowgen::Architecture::Basics;   # to get stairs etc
use Cowgen::VWorld::Room;     

print "Build a maze, call with 'perl maze sizeX sizeY'\n\n";

$vbr = Cowgen::Vbr->create("houses.mis");
$txt = Cowgen::Texture->create($vbr->getTXT());

# get commandline parameter
$outfile = "data/maze.mis"; $x = shift || 4; $y = shift || 4;
$x = 4 if ($x < 4) || ($x > 32);
$y = 4 if ($y < 4) || ($y > 32);

print "Creating maze with dimensions $x"."x$y\n";

my $bunch = Cowgen::Bunch->create("Maze");

my $stones = $txt->weightedList ( { bricks => 1, stone => 1, dirt => 1, 
  gray => 2, odstone => 2, stnwal3 => 1, rufgry => 0.5, brick1 => 1, clnbrick => 1,
  stone2s2 => 1, stonebb => 1, bluestnc => 1,
  } );
my $text = randTexture($stones);
$bunch->setDefTexture($text);
print "Using Texture ",$txt->textureLName($text)," for walls.\n";
$text = $txt->texture("wood");
print "Using Texture ",$txt->textureLName($text)," for doors.\n";

# blockthick and blocktexture are ignored for fake things
# door/window type is defined later on
$mydoor = { floortexture => $txt->texture("tile"), texture => $txt->texture("wdplanko"), 
        innertexture => $txt->texture("wdplanko"), framethick => 0.25,
        blocktexture => $txt->texture("wood"), blockthick => 0.5,
        height => 8, width => 4
        };

$maze = cell ( { div => inX(), room => {} } );

my ($xi,$yi);
for ($xi = 0; $xi < $x; $xi++)
  {
  $celly = cell ( { div => inY(), size => 16 } );
  for ($yi = 0; $yi < $x; $yi++) 
    {
    $room = { wallthick => 0.5, texture => randTexture ($stones), gridsize => 0.25,
      ceilingthick => int(rand(4))+0.5, floorthick => int(rand(3))+0.5, };
    $facing = aSouth(); 
    $facing = aNorth() if (rand() < 0.25);
    $facing = aEast() if (rand() < 0.25);
    $facing = aWest() if (rand() < 0.25);
    $things = [ openDoor($facing,$mydoor) ];
    if (rand() < 0.7)
      {
      do
        {
        $facing1 = aSouth(); 
        $facing1 = aNorth() if (rand() < 0.25);
        $facing1 = aEast() if (rand() < 0.25);
        $facing1 = aWest() if (rand() < 0.25);
        } while ($facing1 != $facing);
      push (@$things, openDoor($facing,$mydoor));
      }
    $cellx = cell ( { size => 16, room => $room, things => $things,
     } );
    pushCell ($celly, $cellx);
    }
  pushCell ($maze, $celly);
  }

$bunch = worldAsBunch(128,0,0,$x*16,$y*16,20,$maze);

$bunch->autoRoom(); $bunch->debug(); 
$vbr->missionEAX ( eax => eax_stoneRoom() );
$vbr->saveMission($gf,$bunch,$outfile);

print "Done, enjoy maze.vbr!\n";

# EOF
1;
