#!/usr/bin/perl -w

# This is the package Inside. It contains some basic stuff to fill houses

# usage: script insides

# (c) Copyright by Tels 1999. All Rights Reserved. Do NOT distribute.
# Updates and support on bloodgate.com/thief
# Thanx to Doug Church at TLG

# 1999-11-07 v0.0.1 - from scratch
# 1999-11-13 v0.0.2 - added: fireplace from Bilding, roof
#                            cornershelf
#                     fixed: shelf, table, counter use now rotateByFacing
# 1999-11-24 v0.0.3 - fixed: privy uses now rotateByFacing
# 1999-12-23 v0.0.4 - added: bed

package Cowgen::Architecture::Inside;
require Exporter;
@ISA    = qw(Exporter);
@EXPORT = qw( 
             table bed counter shelf privy fireplace roof
             );
#@EXPORT_OK = qw();

#doc: table bed counter shelf privy fireplace roof

use Cowgen::Dromed;
use Cowgen::Bunch;

sub table
  { 
  # create a table
  # named parameters: thick, legs (2 or 4), support (true/false only for 
  # 4-legged tables), legwidth, leglength (only if legs == 4; defaults 
  # to legwidth),
  # facing (aSouth() aso), texture, toptexture, leglengthspacing, 
  # legwidthspacing
  my ($x,$y,$z,$w,$l,$h,$p) = @_;
  my $b = Cowgen::Bunch->create("table##num##");
  
  # table top
  my $thick = $p->{thick} || 1; my $text = $p->{texture} || -1;
  my $brush = $b->addBrush (solid(),cube(),$x,$y,$z+$h/2-$thick/2,$w,$l,
      $thick,$text);
  $b->setTexture ($brush,top(),$p->{toptexture}) if (defined $p->{toptexture});
  # legs
  my $lh = $h-$thick; my $z1 = $z-$thick/2;
  my $ly = $p->{leglengthspacing} || 1; 
  my $lx = $p->{legwidthspacing} || 1; 
  $lw = $p->{legwidth} || 1; $ll = $p->{leglength} || $lw; 
  if ($p->{legs} == 2)   
    {
    $b->addBrush (solid(),cube(),$x,$y+$w/2-$ly-$lw/2,$z1,$ll,$l-$lx*2,$lh,$text);
    $b->addBrush (solid(),cube(),$x,$y-$w/2+$ly+$lw/2,$z1,$ll,$l-$lx*2,$lh,$text);
    }
  else
    {
    $b->addBrush (solid(),cube(),$x+$l/2-$lx,$y+$w/2-$ly-$lw/2,$z1,$ll,$lw,$lh,$text);
    $b->addBrush (solid(),cube(),$x+$l/2-$lx,$y-$w/2+$ly+$lw/2,$z1,$ll,$lw,$lh,$text);
    $b->addBrush (solid(),cube(),$x-$l/2+$lx,$y+$w/2-$ly-$lw/2,$z1,$ll,$lw,$lh,$text);
    $b->addBrush (solid(),cube(),$x-$l/2+$lx,$y-$w/2+$ly+$lw/2,$z1,$ll,$lw,$lh,$text);
    if ((defined $p->{support}) && ($p->{support} == true()))
      {
      $b->addBrush (solid(),cube(),$x,$y-$w/2+$ly+$lw/2,$z-$h/2+$lh/2,$ll,$l-$ly*2-$lw,$lw,$text);
      $b->addBrush (solid(),cube(),$x,$y+$w/2-$ly-$lw/2,$z-$h/2+$lh/2,$ll,$l-$ly*2-$lw,$lw,$text);
      }
    }
  $b->rotateByFacing ($x,$y,$z, $facing);
  return $b; # table
  }

sub bed
  {
  # creates a bed with 4 spiked posts
  # named parameters: postwidth (width of posts, 0 to disable),
  # texture (outside and default), facing, toptexture (texture of top face),
  # posttexture (texture of posts), woodthick
  # (width of wood at head/foot side), bodyheight (height of bedbody),
  # woodheight (height of wood at foot/head side, 0 = disable),
  # woodtexture (texture of wood at foot/head side),
  # floorheight (height from floor to bedbody)
  my ($x,$y,$z,$w,$l,$h,$p) = @_;
  my $txt = $p->{texture} || -1;
  my $ptxt = $p->{posttexture} || $txt;
  my $ttxt = $p->{toptexture} || $txt;
  my $wtxt = $p->{woodtexture} || $ptxt;
  my $facing = $p->{facing} || aSouth();
  my $pw = $p->{postwidth}; $pw = 1 if (!defined $pw);
  my $wt = $p->{woodthick} || 0.25;
  my $bh = $p->{bodyheight} || 1;
  my $wh = $p->{woodheight}; $wh = $bh*2 if (!defined $wh);
  my $fh = $p->{floorheight} || 0; $fh = 0 if ($pw <= 0); 
  # no posts, no floorspace
  my $b = Cowgen::Bunch->create("bed##num##");
  $br = $b->addBrush (solid(),cube(),$x,$y,$z-$h/2+$bh/2+$fh,$w,$l,$bh,$txt);     
  $br = $b->addBrush (solid(),cube(),$x,$y,$z-$h/2+$bh+$fh,$w-0.5,$l-$pw*2-0.5,0.5,$ttxt);
  $b->setTexture ($br,top(),$ttxt) if ($ttxt != $txt);
  # wood at head/foot
  if ($wh > 0)
    {
    my $ww = $w-$pw*2;                                                          
    $b->addBrush (solid,cube(),$x-$l/2+$wt/2,$y,$z-$h/2+($bh+$wh)/2+$fh,$ww,$wt,
     $wh+$bh,$wtxt);                                                                     
    $b->addBrush (solid,cube(),$x+$l/2-$pw/2,$y,$z-$h/2+$bh+0.25+$wh/4+$fh,$ww,$wt,$wh/2,$wtxt);
    }
  if ($pw > 0)
    {
    my $z2 = $z-0.5;                                                                
    $b->addBrush (solid(),cube(),$x+$l/2-$pw/2,$y+$w/2-$pw/2,$z2,$pw,$pw,$h-1,
     $ptxt); 
    $b->addBrush (solid(),cube(),$x-$l/2+$pw/2,$y+$w/2-$pw/2,$z2,$pw,$pw,$h-1,
     $ptxt);
    $b->addBrush (solid(),cube(),$x+$l/2-$pw/2,$y-$w/2+$pw/2,$z2,$pw,$pw,$h-1,
     $ptxt); 
    $b->addBrush (solid(),cube(),$x-$l/2+$pw/2,$y-$w/2+$pw/2,$z2,$pw,$pw,$h-1,
     $ptxt);
    $b->setDefAlign(ALIGN_BY_SIDES); $b->setFaces(4);                             
    $b->addBrush (solid(),pyramid(),$x+$l/2-$pw/2,$y+$w/2-$pw/2,$z+$h/2-0.5,$pw,$pw,1,$ptxt); 
    $b->addBrush (solid(),pyramid(),$x-$l/2+$pw/2,$y+$w/2-$pw/2,$z+$h/2-0.5,$pw,$pw,1,$ptxt);
    $b->addBrush (solid(),pyramid(),$x+$l/2-$pw/2,$y-$w/2+$pw/2,$z+$h/2-0.5,$pw,$pw,1,$ptxt);
    $b->addBrush (solid(),pyramid(),$x-$l/2+$pw/2,$y-$w/2+$pw/2,$z+$h/2-0.5,$pw,$pw,1,$ptxt);
    }
  $b->rotateByFacing($x,$y,$z,$facing);
  return $b; # bed
  }      

sub counter
  { 
  # create a counter with an open back (for a bar or something)
  # named parameters: thick (thickness of walls), 
  # wallthick (thickness of backwall),
  # rows, cols, texture, toptexture, facing (aNorth/aSouth/aWest/aEast)
  my ($x,$y,$z,$w,$l,$h,$p) = @_;
  my $b = Cowgen::Bunch->create("counter##num##");
  my ($t,$cl,$ch,$cw,$xa,$ya,$za,$dxa,$dya,$dcw,$dcl);

  # lay counter
  my $thick = $p->{thick} || 1; my $wthick = $p->{wallthick} || $thick; 
  my $texture = $p->{texture}; my $facing = $p->{facing} || aSouth();
  
  my $brush = $b->addBrush (solid(),cube(),$x,$y,$z,$w,$l,$h,$texture); 
  $b->setTexture ($brush,top(),$p->{toptexture}) if (defined $p->{toptexture});

  my $rows = $p->{rows} || 0;
  my $cols = $p->{cols} || 0;
  if (($rows > 0) && ($cols > 0))
    {
    $ch = ($h-$thick*2) / $rows; $cw = ($w-$thick*2) / $cols;   
    $cl = ($l-$wthick);
    $za = $z - $h/2 + $thick + $ch/2;
    for ($ry = 0; $ry < $rows; $ry++)
      {
      $ya = $y - $w/2 + $thick + $cw/2;
      for ($rx = 0; $rx < $cols; $rx++)
        {
        $b->addBrush (air()+MNOROOM,cube(),$x+$wthick/2,$ya,$za,
                      $cw-$thick,$cl, $ch-$thick,$texture); 
        $ya += $cw;
        }
      $za += $ch;
      }
    }
  $b->rotateByFacing ($x,$y,$z, $facing);
  return $b; # counter
  }

sub cornershelf
  {
  # create a corner shelf with supporting pyramid, facing initially
  # southeast 
  # named parameters: thick (thickness of board), supportwidth
  # (width of supporting pyramid), supportlength, 
  # supporttexture, boardtexture, facing (aNorth aso)
  my ($x,$y,$z,$w,$l,$h,$p) = @_;
  my $b = Cowgen::Bunch->create("shelf##num##");
  my $bt = $p->{boardtexture} || -1; 
  my $st = $p->{supporttexture} || $bt;
  my $sh = $h-$thick;
  my $sw = $p->{supportweidth} || ($w / 4);
  my $sl = $p->{supportlength} || ($l / 4);
  my $facing = $p->{facing} || aSouth();

  # board
  $b->addBrush (solid(),cube(),$x,$y,$z+$h/2-$thick/2,$w,$l,$thick,$bt);
  $b->addBrush (solid()+ALIGN_BY_SIDES,pyramid()+foursided(),
     $x-$w/2,$y-$l/2,$z-$h/2+$sh/2,$sw,$sl,$sh,$st,180,0,0);
  $b->rotateByFacing ($x,$y,$z, $facing);
  return $b; # cornershelf
  }

sub shelf
  {
  # create a shelf with supporting wedges
  # named parameters: thick (thickness of board), supportspacing (distance
  # from sides), supportwidth, supportlength
  # supporttexture, boardtexture, facing (aNorth aso)
  my ($x,$y,$z,$w,$l,$h,$p) = @_;
  my $b = Cowgen::Bunch->create("shelf##num##");

  my $bt = $p->{boardtexture} || -1; 
  my $wt = $p->{supporttexture} || $bt;
  my $thick = $p->{thick} || 1; 
  $thick = $h if ($thick > $h); 
  my $wl = $p->{supportlength} || ($l - 0.25);
  $wl = $l - 0.25 if ($wl > $l);
  my $ww = $p->{supportwidth} || 0.5; 
  $ww = $l / 4 if ($ww > $l/4);
  my $ws = $p->{supportspacing} || 1; 
  $ws = $l/4 if ($ws > $l / 4);
  my $wh = $h-$thick; # wedge height
  my $facing = $p->{facing} || aSouth();
  my ($dxa,$dya);
  $b->addBrush (solid(),cube(),$x,$y,$z+$h/2-$thick/2,$w,$l,$thick,$bt);
#debug:
#  $b->addBrush (air(),cube(),$x,$y,$z,$w,$l,$h); 
  $dya = $y-$w/2+$ws; $dxa = $x-$l/2+$wl/2; 
  $b->addBrush (solid(),wedge(),$dxa,$dya,$z-$thick/2,$wl,$ww,$wh,
                $wt,0,180,270); 
  $dya = $y+$w/2-$ws; $dxa = $x-$l/2+$wl/2; 
  $b->addBrush (solid(),wedge(),$dxa,$dya,$z-$thick/2,$wl,$ww,$wh,
                $wt,0,180,270); 
  $b->rotateByFacing ($x,$y,$z, $facing);
  return $b; # shelf
  }

sub privy
  {
  # create a privy
  # named parameters: diameter (of hole), waterheight
  # texture, facing (aNorth aso)
  my ($x,$y,$z,$w,$l,$h,$p) = @_;
  my $b = Cowgen::Bunch->create("privy##num##");

  my $facing = $p->{facing} || aSouth();
  my $wh = $p->{waterheight} || 0;
  $b->addBrush (solid(),cube(),$x,$y,$z,$w,$l,$h,$p->{texture});
  $b->setFaces(10);
  $b->addBrush (air()+MNOROOM,cylinder(),$x,$y,$z,$p->{diameter},$p->{diameter},$h,$p->{texture}); 
  if ($wh > 0)
    {
    $b->addBrush (water(),cylinder(),$x,$y,$z-$h/2+$wh/2,$p->{diameter},$p->{diameter},$wh,
    $p->{texture}); 
    }
  $b->rotateByFacing ($x,$y,$z, $facing);
  return $b; # privy
  }

sub fireplace
  {
  # create a big fireplace
  # and odepth is the depth of the open part in front, oheight is the height
  # of the sims from the bottom. try 0,0,0,16,16,16,2,4,3 for example
  # named parameters: wallthick, opendepth, openheight, outertexture,
  # innertexture, facing
  my ($x,$y,$z,$w,$l,$h,$p) = @_;
  my $outertext = $p->{outertexture};
  my $innertext = $p->{innertexture} || -1;
  my $odepth = $p->{opendepth} || 4;
  my $oheight = $p->{openheight} || 3;
  my $thick = $p->{thick} || 1;
  my $facing = $p->{facing} || aSouth();
  my $b = Cowgen::Bunch->create("fireplace##num##");
  $b->addBrush (solid(),cube(),$x,$y,$z,$w,$l,$h,$outertext);
  $b->addBrush (air()+MNOROOM,cube(),$x,$y,$z,$w-$thick*2,
           $l-$thick*2, $h-$thick*2, $innertext);
  $b->addBrush (air()+MNOROOM,cube(),$x+$l/2-$odepth/2,$y,
           $z+$oheight/2,$w,$odepth,$h-$oheight,$outertext);
  $b->rotateByFacing ($x,$y,$z, $facing);
  return $b; # fireplace
  }                  

sub roof
  {
  # create a simple roof by using bwedge() or bsimple(), bsimple is one 
  # wedge, bwedge two wedges, and 0 is centered pyramid
  # named parameters: flags (bsimple(); bwedge() or 0 meaning pyramid),
  # texture, facing, sidetexture (defaults to texture)
  my ($x,$y,$z,$w,$l,$h,$p) = @_;
  my $t = $p->{texture} || -1;
  my $st = $p->{sidetexture} || $t;
  my $facing = $p->{facing} || aSouth();
  my $flags = $p->{flags} || 0;
  my $b = Cowgen::Bunch->create("roof##num##");
  if (($flags & bsimple()) !=0 ) 
    {
    $b->addBrush (solid(),wedge(),$x,$y,$z,$w,$l,$h,$t);
    }
  elsif (($flags & bwedge()) !=0 ) 
    {
    $b->addBrush (solid(),wedge(),$x,$y-$w/4,$z,$w,$l/2,$h,$t,0,0,90);
    $b->addBrush (solid(),wedge(),$x,$y+$w/4,$z,$w,$l/2,$h,$t,0,0,270);
    }
  else
    {
    $b->addBrush (solid()+aligned_by_sides(),pyramid()+foursided(),
     $x,$y,$z,$w,$l,$h,$t);
    }
  $b->rotateByFacing ($x,$y,$z, $facing);
  return $b; # roof
  }                  

1;

