#!/usr/bin/perl
$CONVERT_SOME_NOISES_TO_SIMPLER_STRING = 1;
#
$DEBUGPRINTS = 0;
$TESTPRINTS = 0;
#
$dostuff = 0;
$endnow = 0;
$classname = "";
$islocationline = 0;
$isambientsoundline = 0;
$origin_x = 0;
$origin_y = 0;
$origin_z = 0;
$noise = '';

#The point of this is to iterate through a t3d file and look for things to print in the entity format

while (1) {
$beforeequals = "";
$afterequals = "";
$islocationline = 0;
$isambientsoundline = 0;

$thing = <STDIN>;
chomp($thing);

	if ($thing eq 'End Actor') {
				if ($DEBUGPRINTS) { print("    **** Says End Actor\n"); }
		$endnow = 1;
	} elsif (index(lc($thing), lc('Begin Actor')) != -1) {
			if ($DEBUGPRINTS) {  print "ignoring begin actor line so does not conflict with ambient sound\n"; }
	} elsif (index(lc($thing), lc('Location')) != -1) {
   				if ($DEBUGPRINTS) {  print "$thing contains Location\n"; }
		$islocationline = 1;
	} elsif (index(lc($thing), lc('AmbientSound')) != -1) {
   				if ($DEBUGPRINTS) {  print "$thing contains AmbientSound\n"; }
		$isambientsoundline = 1;
	} else {
				if ($DEBUGPRINTS) {  print("    _____Nothing\n"); }
		#$dostuff = 0;
	}

	if ($islocationline) {
		($beforeequals, $afterequals) = split("=",$thing,2);
				if ($DEBUGPRINTS) {  print("AFTEREQUALSLOCATIONLINE   ***$afterequals\n"); }
		$afterequals =~ s/\(//;
		$afterequals =~ s/\)//;
				if ($DEBUGPRINTS) {  print("                      2   ***$afterequals\n\n\n"); }
			
		
		$origin_x = 0;
		$origin_y = 0;
		$origin_z = 0;
		
		@tokens = split(/,/, $afterequals);
		foreach my $token (@tokens) {
		    		if ($DEBUGPRINTS) {  print("$token\n"); }
		    ($beforeequals, $afterequals) = split("=",$token,2);
		   		if ($DEBUGPRINTS) {   print("	$beforeequals\n"); }
		   		if ($DEBUGPRINTS) {   print("	$afterequals\n"); }
		    if ($beforeequals eq 'X') {
		    	$origin_x = $afterequals;
		    } elsif ($beforeequals eq 'Y') {
		    	$origin_y = $afterequals;
		    } elsif ($beforeequals eq 'Z') {
		    	$origin_z = $afterequals;
		    }
		}
	} elsif ($isambientsoundline) {
		($beforeequals, $afterequals) = split("=",$thing,2);
				if ($DEBUGPRINTS)  { print("ESASAFTEREQUALSAMBIENTSOUND  ***$afterequals\n"); }
		$afterequals =~ s/Sound\'//;
		$afterequals =~ s/\'//;
				if ($DEBUGPRINTS)  { print("                      2   ***$afterequals\n\n\n"); }
		
		
		$noise = '';
		#$noise = chomp($afterequals);
		$noise = $afterequals;
		
			if ($CONVERT_SOME_NOISES_TO_SIMPLER_STRING) {
				$afterequals =~ s/Ambancient\.Looping\.//;
				$afterequals =~ s/Ambmodern\.Looping\.//;
				$afterequals =~ s/Amboutside\.Looping\.//;
				$noise = 'sound/UNR_'."$afterequals";
			}
		
		if ($dostuff == 4) { 
			$dostuff = 7; 
			$classname = 'target_speaker'; 
		} #Change to ambient sound part instead
	} else {
		#($beforeequals, $afterequals) = split("=",$thing,2);
		($beforeequals, $afterequals) = split("=",$thing);
		#print("   ***$beforeequals\n\n\n");
						if ($DEBUGPRINTS)  { print("   ***$afterequals\n\n\n"); }
		
		if ($beforeequals eq 'Begin Actor Class') {
			$classname = "";
						if ($DEBUGPRINTS)  { print("    **** Eays Begin Actor Class\n"); }
			$TWObeforespace = "";
			$TWOafterspace = "";
			
			#($TWObeforespace, $TWOafterspace) = split("\s+",$afterequals,2);
			#($TWObeforespace, $TWOafterspace) = split("/x",$afterequals,2);
			($TWObeforespace, $TWOafterspace) = split(" Name",$afterequals,2); #hack
			$classname = $TWObeforespace;
				if ($classname eq 'Light')
					{
						$dostuff = 4;
					}
				elsif ($classname eq 'RocketCan')
					{
						$classname = 'item_rockets';
						$dostuff = 5;
					}
				elsif ($classname eq 'Eightball')
					{
						$classname = 'weapon_ballrktlauncher';
						$dostuff = 5;
					}
				elsif ($classname eq 'Sludge')
					{
						$classname = 'item_toxic';
						$dostuff = 5;
					}
				elsif ($classname eq 'GESBioRifle')
					{
						$classname = 'weapon_gbserifle';
						$dostuff = 5;
					}
				elsif ($classname eq 'Minigun')
					{
						$classname = 'weapon_cmbn9mgun';
						$dostuff = 5;
					}
				elsif ($classname eq 'AutoMag')
					{
						$classname = 'weapon_pistol';
						$dostuff = 5;
					}
				elsif ($classname eq 'Shellbox')
					{
						$classname = 'item_bullets';
						$dostuff = 5;
					}
				elsif ($classname eq 'Rifle')
					{
						$classname = 'weapon_t17mmrifle';
						$dostuff = 5;
					}
				elsif ($classname eq 'RifleRound')
					{
						$classname = 'item_bullets';
						$dostuff = 6; #special case, more information printed
					}
				elsif ($classname eq 'Razorjack') #not tested
					{
						$classname = 'weapon_razorprjctr';
						$dostuff = 5;
					}
				elsif ($classname eq 'DispersionPistol') #not tested
					{
						$classname = 'weapon_dsprsnpstl';
						$dostuff = 5;
					}
				elsif ($classname eq 'ASMD')
					{
						$classname = 'weapon_asdwrifle';
						$dostuff = 5;
					}
				elsif ($classname eq 'ASMDAmmo')
					{
						$classname = 'item_cells';
						$dostuff = 5;
					}
				elsif ($classname eq 'Armor')
					{
						$classname = 'item_armor';
						$dostuff = 5;
					}
				elsif ($classname eq 'NaliFruit')
					{
						$classname = 'item_health_small';
						$dostuff = 5;
					}
				elsif ($classname eq 'FlakCannon')
					{
						$classname = 'weapon_fragmentationcannon';
						$dostuff = 5;
					}
				elsif ($classname eq 'Stinger')
					{
						$classname = 'weapon_strkr';
						$dostuff = 5;
					}
				elsif ($classname eq 'PlayerStart')
					{
						$classname = 'info_player_start';
						$dostuff = 5;
					}
				elsif ($classname eq 'AmbientSound')
					{
						$classname = 'target_speaker';
						$dostuff = 7;	#needs more info pasted
					}
				elsif ($classname eq 'PulseGun') #not tested
					{
						$classname = 'weapon_electricrifle';
						$dostuff = 5;
					}
				elsif ($classname eq 'UT_Eightball') #not tested
					{
						$classname = 'weapon_nineballlauncher';
						$dostuff = 5;
					}
				#substitutions
				elsif ($classname eq 'UT_FlakCannon') #not tested #substitutions
					{
						$classname = 'weapon_fragmentationcannon';
						$dostuff = 5;
					}
				elsif ($classname eq 'Ripper') #not tested #substitutions
					{
						$classname = 'weapon_razorprjctr';
						$dostuff = 5;
					}
				elsif ($classname eq 'ShockRifle') #not tested #substitutions
					{
						$classname = 'weapon_asdwrifle';
						$dostuff = 5;
					}
				elsif ($classname eq 'Enforcer') #not tested #substitutions
					{
						$classname = 'weapon_pistol';
						$dostuff = 5;
					}
				elsif ($classname eq 'UT_BioRifle') #not tested #substitutions
					{
						$classname = 'weapon_gbserifle';
						$dostuff = 5;
					}
				elsif ($classname eq 'SniperRifle') #not tested #substitutions
					{
						$classname = 'weapon_lrr';
						$dostuff = 5;
					}
				elsif ($classname eq 'Minigun2') #not tested 
					{
						$classname = 'weapon_minigunii';
						$dostuff = 5;
					}
				elsif ($classname eq 'WarheadLauncher') #not tested 
					{
						$classname = 'weapon_rdmrrocket';
						$dostuff = 5;
					}
				#elsif ($classname eq 'ImpactHammer') #not tested 
				else 
					{	
					$dostuff = 3;
				}
		}
	}

#First a test massage
if ($dostuff == 3 && $endnow == 1 && $TESTPRINTS) {
my $message = <<"END_MESSAGE";
ee{
ee	classname "$classname" xxxxxx
ee}
END_MESSAGE

print $message;
$classname = "";
#then another test message
} elsif ($dostuff == 4 && $endnow == 1 && $TESTPRINTS) {
my $message = <<"ENX_MESSAGE";
**{
**	"classname" "$classname" xxxxxx
**	"origin" "$origin_x $origin_y $origin_z"
**	"spawnflags" "1" wonder what that does to a light
**}
ENX_MESSAGE
 
print $message;
$classname = "";
#this is the real deal below
} elsif ($dostuff == 5 && $endnow == 1) {
my $message = <<"ENY_MESSAGE";
{
	"classname" "$classname"
	"origin" "$origin_x $origin_y $origin_z"
	"spawnflags" "1"
}
ENY_MESSAGE
 
print $message;
$classname = "";
#this is for ambientsounds below
} elsif ($dostuff == 7 && $endnow == 1) {
my $message = <<"ENZ_MESSAGE";
{
	"classname" "$classname"
	"origin" "$origin_x $origin_y $origin_z"
	"noise" "$noise.wav"
}
ENZ_MESSAGE
 
print $message;
$classname = "";
#This is a special case for a rifle round for the t17mmrifle
} elsif ($dostuff == 6 && $endnow == 1) {
my $message = <<"ENX2_MESSAGE";
{
	"classname" "$classname"
	"origin" "$origin_x $origin_y $origin_z"
	"spawnflags" "1"
	"model" "models/items/casingriflebullet.iqm"
	"scale" "1.46"
	"ammo_nails" "3"
}
ENX2_MESSAGE
 
print $message;
$classname = "";
}
#completed

	#reset variables for the next block
	if ($endnow == 1) {
		$classname = "";
		$beforeequals = "";
		$afterequals = "";
		$endnow = 0;
		$dostuff = 0;
		$origin_x = 0;
		$origin_y = 0;
		$origin_z = 0;
		$noise = '';
	}

}
