#!/usr/bin/perl -w

# increase/decrease the maximum number of objects in a mission
# 2000-01-23 Tels v0.0.1
# 2000-03-18 Tels v0.0.2 - added decreasing

use Cowgen::Vbr;

print "Mission object increase v0.0.2 2000-03-19\n\n";
print "      (c) Copyright by Tels (bloodgate.com) 1999-2000. All Rights Reserved.\n\n";
print "This increases/decreases the OBJVEC chunk size. This chunk contains a bit for\n";
print "each object and has per default 2048 entries for object IDs. You have to set\n";
print "object_max in Dark.cfg, too. Warning: Decreasing will throw away some objects!\n\n";
print "Usage: perl maxobj.pl inputfile [outputfile number]\n";
print " Number ranges from 2048..8192 and defaults to 4096.\n\n";

# get commandline parameter
$infile = shift || "miss15.mis"; 
$infile .= ".mis" if (($infile !~ /\.mis$/) && (!-e $infile));
$outfile = shift || ""; 
$number = shift || 4096; 

$number = abs($number); 
$number = 2048 if ($number < 2048);
$number = 8192 if ($number > 8192);

print "Loading file $infile...\n";
$vbr = Cowgen::Vbr->create($infile); # suck in
print "Working...\n";
my $objvec = $vbr->getChunk("ObjVec");    
if (!defined $objvec)
  { 
  print "Error: Mission file doesn't contain an ObjVec chunk!\n"; exit;
  }
$cursize = unpack("l",substr($$objvec,28,4));
print " Current maximum number of objects: $cursize";
if ($outfile eq "") { print "\n\n"; exit ; }
print ", wanted new size: $number\n";
$diff = $number - $cursize;
if ($diff <= 0)
  {
  print "Warning: Current number of objects is already greater or equal $number\n"; 
  print "         This will probably remove some objects from the mission!!\n"; 
  $$objvec = substr($$objvec,24,8+($diff>>3)); # strip header and last
  substr ($$objvec,4,4) = pack("L",$number); 
  } 
else
  {
  print " Setting max objects to $number (adding ",$diff >> 3," bytes).\n";
  $$objvec = substr($$objvec,24,length($$objvec)-24); # strip header
  substr ($$objvec,4,4) = pack("L",$number); 
  $$objvec .= "\x00" x ($diff >> 3);
  }
$vbr->putChunk("ObjVec",$objvec,0,2);    # yes, its that easily!
print "Stripping AIPATH, AI_ROOM_DB, ROOM_DB and WR chunks...";
$vbr->delChunk('ROOM_DB', 'AIPATH', 'WR', 'AI_ROOM_DB');
print "done\n";
print "Saving to $outfile...";
$vbr->writeFile($outfile);   # blow out
print "Done.\n";

# EOF

1;

