#!/usr/bin/perl -w

# increase the maximum number of objects in a mission
# 2000-02-04 Tels v0.0.1

use lib '..';
use Cowgen::Vbr;
use Cowgen::Dromed;
use Cowgen::Gamfile;
use Cowgen::Bunch;

print "Dump object sizes v0.0.1 2000-02-04\n\n";
print "      (c) Copyright by Tels (bloodgate.com) 1999-2000. All Rights Reserved.\n\n";
print "Usage: perl objsizes.pl inputfile\n";

# get commandline parameter
$infile = shift || "objsizes.mis"; $infile .= ".mis" if ($infile !~ /\.mis$/);

# this below creates only 145 objects???
# create mission
#print "Creating file $infile...\n";
#$vbr = Cowgen::Vbr->create("allobj.mis"); # template
#$bunch = Cowgen::Bunch->create("objects");
#$r = $gf->archetypes();
#$x = 0; $y = 0; $z = 0;
#foreach (keys %$r)
#  {
#  $x ++; if ($x == 256) {$x = 0; $y++; } if ($y == 256) {$y = 0; $z++; } 
#  $p = { base => "$_", name => "" }; 
#  $name = $gf->archetypeName($_); $base = $gf->archetypeBase($_);
#  next if ($name =~ /^t_fam\//);
#  next if ($base == 0);
#  print "$name $base\n";
#  $bunch->addObject ($x,$y,$z,0,0,0,$p);
#  }
#$bunch->debug();
#$bunch->stuff2vbr($vbr,$gf);
#$vbr->writeFile("data/$infile");

print "Loading file $infile...\n";
$vbr = Cowgen::Vbr->create($infile); # suck in
print "Working...\n\n";

$meta = Cowgen::Gamfile::unpackMetaProp($vbr);

foreach (keys %{$meta})
  {
#  print "$meta->{$_}->{id},$meta->{$_}->{base},$meta->{$_}->{type}\n";
  if ($meta->{$_}->{type} == 0)
    {
    $o->{$meta->{$_}->{id}} = $meta->{$_}->{base};
    }
  }
$count = 0;

my $brlist = $vbr->getChunk("BRLIST");    
fromBin($brlist);

open FILE, "Objsize.tpl" or die "Cant write Objsize.tpl: $!\n";
while (<FILE>) { $tpl .= $_ };
close FILE;
$tpl =~ s/##prop##/$text/;
$copy = "# This file was automatically generated by objsizes.pl at ".scalar localtime()."\n\n";
$tpl =~ s/##copy##/$copy/;

open FILE, ">../Cowgen/Objsize.pm" or die "Cant write ../Cowgen/Objsize.pm: $!\n";
print FILE $tpl;
close FILE;

print "Done. Dumped $count objects.\n";

# EOF

1;

sub fromBin
  {
  # take binary data and convert it to internal format
  my $buf = shift || return;
  my (@f,$ofs,$l,$i,$faces,$br,$fi,@fa);
  if ($$buf !~ /^BRLIST/)
    {
    print "Error: Expecting BRLIST"; return; 
    }
  $ofs = 24; $l = length($$buf);
  $text = " \$prop = {\n";
  while ($ofs < $l)
    {
    @f = unpack ("SSLSCCffffffSSSSCCCCCCCCCCCCCCCCCCCCCCCCCCCCL", substr($$buf,$ofs,76));
    $ofs += 76;
    $br = {}; # make empty thingy 
    #$br->{time} = $f[1]; $br->{id} = $f[0]; $br->{flags} = 0;
    #$s->{time} = $f[1] if ($f[1] > $s->{time});
    #print "f $f[0] $f[1] $f[2]\n";
    $br->{x} = $f[6]; $br->{y} = $f[7]; $br->{z} = $f[8];
    $br->{l} = $f[9]; $br->{w} = $f[10]; $br->{h} = $f[11];
    #$br->{rx} = derot($f[12]); $br->{ry} = derot($f[13]); $br->{rz} = derot($f[14]);
    $br->{texture} = $f[3];
    $br->{group} = $f[43]; 
    if (($f[4] >= 0) && ($f[4] < 128))
      {
      $ofs += $f[39]*10; # brush
      }
    elsif ($f[4] == 0xFD)
      {   
      #$br->{oid} = $f[2];   
      $ww = round(2*$br->{w},10000); $ll = round(2*$br->{l},10000); 
      $hh = round(2*$br->{h},10000); 
      #$ww = 2*$br->{w}; $ll = 2*$br->{l}; $hh = 2*$br->{h}; 
      if (($ww != 0) && ($ll != 0) && (defined $o->{$f[2]}))
        {
        if (!(($ww == 4) && ($ll == 4) && ($hh == 4)))
          {
          $text .= "$o->{$f[2]}=>[$ww,$ll,$hh],\n"; $count++; 
          }
        }
      
#XXXX
#        push (@{$s->{olist}},$br); 
#        $s->{objects}++;
      }
    } # next thingy
  $text .= "};\n\n#EOF\n1;\n";
  return;
  } # done

