#!/usr/bin/perl

# Generator for ROTT models

%gadtex=(
	 "bot"=>[65,1,62,62],
	 "midcir"=>[1,65,62,62],
	 "top"=>[1,1,62,62],
	 "mid"=>[0,161,128,6],
	 "botcyl"=>[1,129,62,30],
	 "side"=>[65,129,62,14]
	 );

sub gadtexex ($$$$$) {
    my ($blah,$xm,$ym,$wm,$hm)=(@_);
    my ($x,$y,$w,$h)=(@{$gadtex{$_[0]}});
    $x += $xm*$w;
    $y += $ym*$h;
    $w *= $wm;
    $h *= $hm;
    $x /= 128;
    $y /= 256;
    $w /= 128;
    $h /= 256;
    #print "$_[0] $x $y $w $h\n";
    texture "gadtex.png",$x,$y,$w,$h;
}
sub gadtex ($) {
    gadtexex $_[0],0,0,1,1;
}

model {
    file "gad.tmd";
    usefor 1663 .. 1667;

    my $vsiz=192;
    
    my $vsiza=$vsiz/2.75;
    my $vsizb=($vsiz-(2*$vsiza));
    my $vposa=0;
    my $vposb=$vsiza;
    my $vposc=$vposb+$vsizb;
    my $vposd=$vposc+$vsiza;
    
    my $bvrad=220;
    my $bvsiz=64;
    
    my $bv2rad=120;
    my $bv2siz=96;
    shapes { 
	my ($gadrpoints,$gadbpoints,$gadbpoints2)=@{([6,5,5],[8,6,6],[14,10,8],[18,16,12])[$qual]};
	

	scale [1,1,1.2];
	translate [0,0,-280];
	
#bottom
	circle { 
	    location [0,0,0];
	    radius 400;
	    npoints $gadrpoints;
	    #gadtex "bot",;
	    gadtex "bot";
	    dbname "gad bottom";
	};
#top
	if ($qual>=2) {
	    circle {
		#location [0,0,$vposb];
		radius 400;
		npoints $gadrpoints;
		#rotatex 180;
		translate [0,0,$vposb];
		scale [-1,1,1];
		gadtex "midcir";
		dbname "gad center bottom";
	    };
	    circle {
		location [0,0,$vposc];
		radius 400;
		npoints $gadrpoints;
		gadtex "midcir";
		dbname "gad center top";
	    };

	}
	circle {
	    radius 400;
	    npoints $gadrpoints;
	    scale [-1,1,1];
	    #rotatex 180;
	    translate [0,0,$vposd];
	    gadtex "top";
	    dbname "gad top";
	};
#side
	if ($qual>=2) {

	    cylinder {
		radius 400;
		npoints $gadrpoints;
		height $vsiza;
		gadtexex "side",0.0,($vsiz-$vsiza)/$vsiz,1.0,$vsiza/$vsiz;
		dbname "gad side bottom";
	    };

	    cylinder {
		location [0,0,$vposb];
		radius 300;
		npoints $gadbpoints;
		height $vsizb;
		gadtex "mid";
		dbname "gad center cyl";
	    };
	
	    cylinder {
		location [0,0,$vposc];
		radius 400;
		npoints $gadrpoints;
		height $vsiza;
		gadtexex "side",0.0,0.0,1.0,$vsiza/$vsiz;
		dbname "gad side top";
	    };

	} else {
	    cylinder {
		radius 400;
		npoints $gadrpoints;
		height $vsiz;
		gadtex "side";
		dbname "gad side";
	    };
	}
    
	cylinder {
	    location [0,0,-$bvsiz];
	    radius $bvrad;
	    npoints $gadbpoints;
	    height $bvsiz;
	    gadtex "botcyl";
	    dbname "gad bottom outer cyl";
	    twoside 1;
	};
	cylinder {
	    location [0,0,-$bv2siz];
	    radius $bv2rad;
	    npoints $gadbpoints2;
	    height $bv2siz;
	    gadtex "botcyl";
	    dbname "gad bottom inner cyl";
	    twoside 1;
	};
    };

};

model {
    file "column.tmd";
    usefor 1637, 1638;
    
    my $orad=360;
    my $irad=300;
    my $colheight=1150;

    my $tapersiz=96;

    my $cheight=($colheight-($tapersiz*2));

    shapes {
	my $colpoints=((8,12,26,20)[$qual]);
	#my $colpoints=8;
	translate [0,0,-1024*(240/200)];
	cylinder {
	    location [0,0,$tapersiz];
	    radius $irad;
	    height $cheight;
	    npoints $colpoints;
	    texture "coltex.png", 0,$tapersiz/$colheight, 1,$cheight/$colheight;
	};

	clipcone {
	    location [0,0,$colheight-$tapersiz];
	    radius $irad,$orad;
	    height $tapersiz;
	    npoints $colpoints;
	    texture "coltex.png", 0,0,1,($tapersiz/$colheight);;
	};
	clipcone {
	    radius $orad,$irad;
	    height $tapersiz;
	    npoints $colpoints;
	    texture "coltex.png", 0,(($colheight-$tapersiz)/$colheight),1,($tapersiz/$colheight);
	};
    
	circle {
	    location [0,0,$colheight];
	    #rotatex 180;
	    scale [-1,1,1];
	    radius $orad;
	    texture "coltop.png";
	    npoints $colpoints;
	};
    };
};

@springuse=(
[1383],
[1391],
[1390],
[1389,1384],
[1388,1385],
[1387],
[1386]);
for $n (0..6) {
    model {
	file "spring$n.tmd";
	usefor @{$springuse[$n]};
    
	my $orad=300;
	my $bigcylrad=100;
	my $smcylrad=40;

	my $conebigrad=115;
	my $conesmrad=50;

	my $height=(($n*256)/6);
	
	my $sideheight=24;
	my $conequal=2;
	shapes {
	    my $points=((8,12,26,20)[$qual]);
	    my $usecones=($qual>=$conequal);
	    my $coneheight=$usecones?15:0;

 	    translate [0,0,(-1024*(240/200))+50];
	    
	    circle {
		location [0,0,$height];
		#rotatex 180;
		scale [-1,1,1];
		radius $orad;
		texture "springtop.png";
		npoints $points;
	    };
	    if ($n>0) {
		cylinder {
		    location [0,0,$height-$sideheight];
		    texture "springside.png";
		    height $sideheight;
		    radius $orad;
		    npoints $points;
		}
	    }
	    if ($n>0) {
		cylinder {
		    location [0,0,$coneheight];
		    texture "springside.png";
		    height $height-$coneheight;
		    radius $bigcylrad;
		    npoints $points;
		};
 		clipcone {
		    radius $conebigrad,$bigcylrad;
		    height $coneheight;
		    npoints $points;
		    texture "springside.png";
		} if ($usecones);
		my $disp=$orad*0.7071*0.8;
		for $x (-$disp,$disp) {
		    for $y (-$disp,$disp) {
			cylinder {
			    location [$x,$y,$coneheight];
			    texture "springside.png";
			    height $height-$coneheight;
			    radius $smcylrad;
			    npoints $points;
			};
			clipcone {
			    location [$x,$y,0];
#			    if ($usecones) {
#				print "Cone $x $y\n";
#			    }
			    radius $conesmrad,$smcylrad;
			    height $coneheight;
			    npoints $points;
			    texture "springside.png";
			} if ($usecones);
			
		    }
		}
	    }
	    

	};
    };
}
{
    model {
	file "grate.tmd";
	usefor 1906;
    
	my $orad=340;
	my $border=0.02;
	shapes {
	    my $points=((8,12,26,20)[$qual]);
 	    translate [0,0,(-1024*(240/200))+10];
	    
	    circle {
		location [0,0,0];
		#rotatex 180;
		scale [-1,1,1];
		radius $orad;
		texture "grate.png",$border,$border,1.0-($border*2),1.0-($border*2);
		npoints $points;
	    };
	    

	};
    };
}
for $n (0..3) {
    model {
	file "lava$n.tmd";
	usefor 1889+$n;
    
	my $orad=300;
	my $height=64;
	
	shapes {
	    my $points=((8,12,26,20)[$qual]);
 	    translate [0,0,(-1024*(240/200))];
	    
	    circle {
		location [0,0,0];
		#rotatex 180;
		scale [-1,1,1];
		stencil 1; 
		radius $orad;
		texture "lava$n.png";
		npoints $points;
	    };
	    circle {
		location [0,0,-$height];
		#rotatex 180;
		scale [-1,1,1];
		radius $orad;
		texture "lava$n.png";
		npoints $points;
	    };
	    cylinder {
		location [0,0,-$height];
		scale [-1,1,1];
		
		npoints $points;
		texture "lavaside.png";
		height $height;
		radius $orad;
		dbname "lavapit cyl";
		
	    };
	    

	};
    };
}

{
    model {
	file "pit.tmd";
	usefor 1972;
	
	my $orad=500;
	my $irad=380;
	my $height=400;
 	my $border=0.01;
 	my $border2=(1/256);
	shapes {
	    
	    my $points=((8,12,26,20)[$qual]);

 	    translate [0,0,(-1024*(240/200))];
	    disk {
 		location [0,0,0];
		#rotatex 180;
		scale [-1,1,1];
		radius $irad,$orad;
		texture "pit.png",0.0+$border,0.5+$border,0.5-($border*2),0.5-($border*2);
		npoints $points;
 		dbname "pit disk";
	    };
	    circle {
 		location [0,0,-$height+1];
		#rotatex 180;
		scale [-1,1,1];
		radius $irad+1;
		texture "pit.png",0.7,0.7,0.1,0.1;
		npoints $points;
 		dbname "pit bottom";
	    };
	    circle {
 		location [0,0,0];
		texture "pit.png";
		stencil 1;
		scale [-1,1,1];
		radius $orad-1;
		npoints $points;
 		dbname "pit circle";
	 
	    };
	    cylinder {
		location [0,0,-$height];
		scale [-1,1,1];
		
		npoints $points;
		texture "pit.png",$border2*2,$border2,((96.0*2.0-4)/256.0),(96.0/256.0)-($border2*2);
		height $height;
		radius $irad; # trick to make inside visible
		dbname "pit cyl";
		
	    };
	    

	};
    };
}

{
    model {
	file "rocket.tmd";
	usefor 1713..1728;
	
	pivot [0,0,-512];
	vertrotate;
	shapes {
	    union {
		from_md2 "rocket.md2",0,"rocket.png",(288/512),(195/256);
		scale [30,30,30];
		translate [0,0,-512];
		translate [0,0,-300];
		#rotatez 90;
	    };
	    
	};
    };
    model {
	file "asbestos.tmd";
	usefor 1970;
	
	rotate;
	shapes {
	    union {
		from_md2 "armor.md2",0,"asbestos.png",(312/512),(120/256);
		scale [20,20,20];
		translate [0,0,-512];
		translate [0,0,-500];
		#rotatez 90;
	    };
	    
	};
    };
    model {
	file "bulletarmor.tmd";
	usefor 1969;
	
	rotate;
	shapes {
	    union {
		from_md2 "armor.md2",0,"bulletarmor.png",(312/512),(120/256);
		scale [20,20,20];
		translate [0,0,-512];
		translate [0,0,-500];
		#rotatez 90;
	    };
	    
	};
    };
    model {
	file "gasmask.tmd";
	usefor 1968;
	
	rotate;
	shapes {
	    union {
		from_md2 "gasmask.md2",0,"gasmask.png",(200/256),(76/128);
		scale [20,20,20];
		translate [0,0,-512];
		translate [0,0,-500];
		#rotatez 90;
	    };
	    
	};
    };
    model {
	file "bfg.tmd";
	usefor 1962;
	
	rotate;
	shapes {
	    from_md2 "bfg.md2",0,"bfg.png";
	    scale [10,10,10];
	    translate [0,0,-512];
	    translate [0,0,-600];
	    
	};
    } if (0);
    model {
	file "pistol.tmd";
	usefor 1959;
	
	rotate;
	shapes {
	    union {
		from_md2 "pistol.md2",0,"pistol.png",(308/512),(165/256);
		scale [10,10,10];
		translate [0,0,-512];
		translate [0,0,-600];
		#rotatez 90;
	    };
	    
	};
    } if (0);
}
