#!/usr/bin/perl -w

# (c) Copyright by Tels 1999. All Rights Reserved.

# [09/29/99] v0.0.7 - first version
# [09/30/99] v0.0.8 - textures
# 1999-12-01 v0.0.9 - rotating, write as mis
# 2000-01-28 v0.1.0 - added bunch-merge test, create object

use Cowgen::Bunch;                  # for a bunch of brushes
use Cowgen::Dromed;                 # for some Thief constants and specifics
use Cowgen::Architecture::Building; # for stairs
use Cowgen::Texture; 
use Cowgen::Architecture::Basics; 
use Cowgen::Gamfile;

print "Demo script: Benchmarking\n\n";

my $f = shift || 1;
print "Using factor $f (400 Mhz: f should be 1, 200 Mhz: f should be 0.5 etc)\n";

$starttime = time;
$gam = Cowgen::Gamfile->create();
$gam->setStyle (thief()); # set style to Thief

# get commandline parameter
$outfile = shift || "benchmark.mis";
$vbr = Cowgen::Vbr->create("houses.mis");

# create an empty bag
my $bunch = Cowgen::Bunch->create("Benchmark");

$time=time;
print "Start: ",scalar localtime($time),"\n";

$c = 200000 *$f; my (@l);
for ($i=0; $i < $c; $i++)
  {
  @l = rotatePoint (8,9,7, 0,0,0, 90,180,270);
  }
$now = time;
print "After rotating $c points in 3D space:\n";
print " took ",$now-$time," seconds\n"; $time = time;

$c = 4096*$f; 
my $core = Cowgen::Texture->create($vbr->getTXT());
print "Loaded ", $core->numTextures() ," textures.\n";

# make a list for weighted list
foreach (keys %{$vbr->getTXT()})
  {
  $h->{$_} = 1;
  }
$hl = $core->weightedList($h); 
for ($i=0; $i<$c; $i++)
  {
  $name = randTexture ($hl);
  $name = $core->textureName($name);
  }
$now = time;
print "After selecting $c random textures and getting their name:\n";
print " took ",$now-$time," seconds\n"; $time = time;

# merge some bunches
undef $bunch; $bunch = Cowgen::Bunch->create("Benchmark");
$time=time;
print "Start: ",scalar localtime($time),"\n";
$c = 4096*$f; 
for ($i=0; $i<$c; $i++)
  {
  $b = Cowgen::Bunch->create("Benchmark1");
  $b->addBrush (air(),cube(),$i*4,0,0,3,3,3);
  $b->addBrush (solid(),wedge(),$i*4,4,0,3,3,3);
  $bunch->merge ($b);
  }
$now = time;
print "After merging $c bunches together:\n";
print " took ",$now-$time," seconds\n"; $time = time;

# add some objects
undef $bunch; $bunch = Cowgen::Bunch->create("Benchmark");
$time=time;
print "Start: ",scalar localtime($time),"\n";
$c = 8192*$f; 
for ($i=0; $i<$c; $i++)
  {
  $p = { base => $gam->obj("torch"), name => "object##num##",};
  $bunch->addObject ($i,0,0,0,0,0,$p);
  }
$now = time;
print "After adding $c objects:\n";
print " took ",$now-$time," seconds\n"; $time = time;

# add some brushes
undef $bunch; $bunch = Cowgen::Bunch->create("Benchmark");
$time=time;
print "Start: ",scalar localtime($time),"\n";
$c = 2096*$f; 
for ($i=0; $i<$c; $i++)
  {
  $bunch->addBrush (air(),cube(),$i*8,0,0,6,6,6);
  }
$now = time;
print "After adding $c air cubes:\n";
print " took ",$now-$time," seconds\n"; $time = time;


$bunch->autoRoom();
$now = time;
print "After autoRoom:\n";
print " took ",$now-$time," seconds\n"; $time = time;

# later: $buch->empty();
undef $bunch; $bunch = Cowgen::Bunch->create("Benchmark");
$c = 2048*$f; 
for ($i=0; $i<$c; $i++)
  {
  $bunch->addBrush (air()+MROOM,cube(),$i*8,0,0,6,6,6);
  }
$now = time;
$bunch->autoRoom();
$now = time;
print "After autoRoom (this time with MROOM around the cubes):\n";
print " took ",$now-$time," seconds\n"; $time = time;

undef $bunch; $bunch = Cowgen::Bunch->create("Benchmark");
$c = 2048*$f; 
for ($i=0; $i<$c; $i++)
  {
  $br = $bunch->addBrush (air(),cube(),$i*8,0,0,6,6,6);
  $bunch->setTexture ($br,0,1);
  $bunch->setTexture ($br,1,2);
  $bunch->setTexture ($br,2,3);
  $bunch->setTexture ($br,3,4);
  }
$now = time;
print "After adding $c solid cubes and setting 4 faces with texture:\n";
print " took ",$now-$time," seconds\n"; $time = time;

undef $bunch; $bunch = Cowgen::Bunch->create("Benchmark");
$c = 2048*$f; 
for ($i=0; $i<$c; $i++)
  {
  $br = $bunch->addBrush (air(),cube(),$i*8,0,0,6,6,6);
  $bunch->setTexture ($br,0,1,1,2,2,3,3,4);
  }
$now = time;
print "After adding $c solid cubes and setting 4 faces with texture (new method):\n";
print " took ",$now-$time," seconds\n"; $time = time;

undef $bunch; $bunch = Cowgen::Bunch->create("Benchmark");
$c = 2048*$f; 
for ($i=0; $i<$c; $i++)
  {
  $br = $bunch->addBrush (air(),cube(),$i*8,0,0,6,6,6,1,2,3,4);
  }
$now = time;
print "After adding $c solid cubes and setting 4 faces with texture (direkt):\n";
print " took ",$now-$time," seconds\n"; $time = time;

undef $bunch; $bunch = Cowgen::Bunch->create("Benchmark");
$c = 56*$f; 
for ($i=0; $i<$c; $i++)
  {
  $s = spiralstairs ($i*28,$i*20,0,12,8,6,2,1,8,0,10,basroom(),1,2,3,4);
  $bunch->merge($s);
  }
$now = time;
print "After adding $c spiralstairs (got ",$bunch->numBrushes," brushes):\n";
print " took ",$now-$time," seconds\n"; $time = time;

$vbr->saveMission($gf,$bunch,$outfile);
$now = time;
print "After writing:\n";
print " took ",$now-$time," seconds\n"; $time = time;

$stoptime = time;
print "Done: ",scalar localtime($time),", took ",$stoptime-$starttime," seconds.\n";

# EOF

