#!/usr/bin/perl -w

# test script for the new CoW stuff

$| = 1;
use strict;

use Cowgen::Bunch;
use Cowgen::Vbr;
use Cowgen::Dromed;
use Cowgen::Gamfile;
use Cowgen::Texture;

my $outfile = "oocow.mis";
my $infile = "houses.mis";
#my $infile = "dark2.mis";

require "CoW.pl";

my $world = construct();

my $wall = 2;
my $wood = 3;

my $vbr = Cowgen::Vbr->create($infile);
my $gf = Cowgen::Gamfile->create(ucfirst($vbr->gamesysName()));
my $terrain = Cowgen::Bunch->create ( $outfile, $vbr, $gf );
my $things = Cowgen::Bunch->create ( $outfile, $vbr, $gf );

print "$world\n";
print $world->as_string();

print "The world contains:\n";
my $cnt = $world->count(1);
foreach (keys %$cnt)
  {  
  my $n = "$_: "; $n .= ' ' while (length($n) < 32);
  print " $n $cnt->{$_}\n";
  }
my $todo = $cnt->{'CoW::Cell'}; my $done = 0;
print "Now generating...";

# first do world, than traverse it's childs
$world->traverse( 
   { 
     callback => \&generate, 
     'CoW::Light' => \&g_light,  
     'CoW::Brush' => \&g_brush,
     'CoW::Cell' => \&g_cell,
   }, 1, $terrain, $things 
 );

print "\n";

$terrain->merge($things);
$terrain->stuff2vbr($vbr,$gf);
$terrain->autoRoom();
$vbr->writeFile($outfile);

print "Written $outfile, enjoy!\n";

1;

# EOF

##############################################################################

sub generate
  {
  my ($thing,$terrain,$things) = @_;

  #print "At $thing:\n"; 
  #print " x:  $thing->{x}  y:  $thing->{y}  z:  $thing->{z}\n";
  #print " w:  $thing->{w}  l:  $thing->{l}  h:  $thing->{h}\n" 
  # if ref $thing ne 'CoW::Light';
  #print " rx: $thing->{rx} ry: $thing->{ry} rz: $thing->{rz}\n";
  #print " kind:: $thing->{kind}\n"
  # if ref $thing eq 'CoW::Light';
  }

sub g_brush
  {
  my ($thing,$terrain,$things) = @_;
  # need to make plain numbers from possible coords
  $things->addBrush($thing->{kind},$thing->{shape},
   "$thing->{x}", "$thing->{y}", "$thing->{z}",
   "$thing->{w}", "$thing->{l}", "$thing->{h}",
   $wood,
   "$thing->{rx}", "$thing->{ry}", "$thing->{rz}");
  }

sub g_cell
  {
  my ($thing,$terrain,$things) = @_;
  $done++;
  print "\rNow generating..."; print int(1000*$done/$todo)/10,"%   \b\b\b";
  # need to make plain numbers from possible coords
  $terrain->addBrush($thing->{kind},$thing->{shape},
   "$thing->{x}", "$thing->{y}", "$thing->{z}",
   "$thing->{w}", "$thing->{l}", "$thing->{h}",
   $wall,
   "$thing->{rx}", "$thing->{ry}", "$thing->{rz}");
  }

sub g_light
  {
  # need to make plain numbers from possible coords
  my ($thing,$terrain,$things) = @_;
  my $l = { h => $thing->hue(), s => $thing->saturation(), 
         bright => $thing->bright(),
       }; 
  $terrain->addLight($thing->{kind},
   "$thing->{x}", "$thing->{y}", "$thing->{z}",
   $l,
   "$thing->{rx}", "$thing->{ry}", "$thing->{rz}");
  }

