#! /usr/bin/perl -w

# perl script to generate csv (comma seperated variables) file
# last modified: 14.04.2013

use strict;
use List::Util qw( min max );

my $filename = "Shiplist.data"; # $ARGV[0];
my @lines;
my $eof = 0;
my @eos; # stores index of end of ship description
my @nob; my @not; # number of Beam slots and Torpedo slots. Mainly for statistical reasons
my $i = 0; my $j = 0;
my @data;
my @subd; # subset of sliced array or list
my @basic;
my @header = ("race","name","desc","class","size","agility","bio","energy","comp","engineering","propulsion","weapon","industry","titan","D","duranium","crystal","iridium","deritium","build_only","hull","material","double_hull","ablative","polarisation","max_shield","shield_type","reg_shield","speed","range","scanpower","scanrange","cloak","storage","colo","station","maintenance","special_1","special_2","obsolotes","beam","torpedos");

print "convert $filename to csv\n";

open(FILE, "<$filename") || die "can not find file $filename\n";
@lines = <FILE>;
close(FILE);

chomp(@lines);

# get index from shipdata file
foreach(@lines){
	if($_ =~ /END_OF_SHIPDATA/){push(@eos, $i);}
	$i++;
}

# check for Beam slots
for($i=0; $i<=$#eos;$i++){
	if($i == 0){
		@subd = @lines[0 .. $eos[0]];
		$nob[0] = how_many_labels("Beam",\@subd);
		$not[0] = how_many_labels("Torpedo",\@subd);
	}else{
		@subd = @lines[$eos[$i-1] .. $eos[$i] ];
		$nob[$i] = how_many_labels("Beam",\@subd);
		$not[$i] = how_many_labels("Torpedo",\@subd);
	}

}

my $bmax = max @nob; my $tmax = max @not;
print "max Beams : $bmax\n";
print "max torpedos : $tmax\n";


# extract useful information here
for($i=0; $i<=$#eos;$i++) {
	if($i==0){
		@subd = @lines[0 .. ($eos[0]-1)];
		print "$i $subd[1] $nob[$i] $not[$i]\n";
		$basic[$i] = [ @subd[0 .. 39] ]; 
		$basic[$i][40] = extract_beam(@lines[0 .. $eos[0]] );
		$basic[$i][41] = extract_torpedo(@lines[0 .. $eos[0]] );
	}else{
		@subd = @lines[($eos[$i-1]+1) .. ($eos[$i]-1) ];
		print "$i $subd[1] $nob[$i] $not[$i]\n";
		$basic[$i] = [ @subd[0 .. 39] ];
		$basic[$i][40] = extract_beam(@lines[$eos[$i-1] .. $eos[$i] ]);
		$basic[$i][41] = extract_torpedo(@lines[$eos[$i-1] .. $eos[$i] ]);
	}
}

# write shipdata to csv file
open(FILE2, ">ships.csv") || die "can not open ships.csv\n";
for($i=0;$i<$#header;$i++){
	print FILE2 "$header[$i]; ";
}
print FILE2 "$header[$#header]\n";
for($i=0;$i<=$#basic;$i++){
	for($j=0;$j < $#{$basic[$i]};$j++){
		print FILE2 "$basic[$i][$j]; ";
	}
	print FILE2 "$basic[$i][$#{$basic[$i]}]\n";
}
close(FILE2);
       


# determine how many entries with a specific leabel are given
sub how_many_labels{
	use strict;
	my ($label) = @_[0];
	my (@array) = @{$_[1]};
	my $j=0;

	foreach(@array){ if($_ =~ /($label)/){ $j++;} }
	return($j);
}

# extract weapon information
sub extract_beam{
# example first entry from Shiplist.data	
#$Beam$
# 0  5 # beam level?
# 1 5 # beam damage?
# 2 3 # number of beams at mountpoint
# 3  Laser Emitter # type
# 4 0 # Modulating
# 5  0 # Piercing
# 6  10 # Bonus from system (what system)?
# 7  3 # beamtime?
# 8  7 # recharge time
# 9  1 # number of shots (?)
# 10  0 # mountposition
# 11  55 # angle 
	use Math::Round;
	use strict;
	my (@array) = @_;
	my @pob; # start positions of Beam entries
	my @subd;
	my $j=0;

#	foreach(@array){print "$_";}
#	die;
	# find all seperate beam mount points
	for($j=0;$j<=$#array;$j++){
		if($array[$j] =~ /\$Beam/){ push(@pob,$j);}
	}

	# now go through all beam entries
	my $damage = 0;
	for($j=0;$j<=$#pob;$j++){
		@subd = @array[($pob[$j]+1) .. ($pob[$j]+12)];
		print "No. of Beams : $subd[2]\n";
		$damage = $damage + $subd[2] * $subd[9] * $subd[7] * round(100 / ($subd[7]+$subd[8])) * $subd[1];
	}
	print "DAMAGE : $damage\n";
	return($damage);
}

# extract torpedo information
sub extract_torpedo{
# example first entry from Shiplist.data	
#$Torpedo$
# 0  0 # Torpedo type ??
# 1  1 # Torpedoes per shot
# 2  50 # firerate
# 3  2 # number of tubes
# 4  Human Mod 2 Tube # tubes
# 5  0 # micro only ??
# 6  50 # accuracy
# 7  0 # mount position ??
# 8  75 # angle
	use Math::Round;
	use strict;
	my (@array) = @_;
	my @pob; # start positions of Torpedo entries
	my @subd;
	my $j=0;

#	foreach(@array){print "$_";}
#	die;
	# find all seperate beam mount points
	for($j=0;$j<=$#array;$j++){
		if($array[$j] =~ /\$Torpedo/){ push(@pob,$j);}
	}

	# now go through all beam entries
	my $damage = 0;
	for($j=0;$j<=$#pob;$j++){
		@subd = @array[($pob[$j]+1) .. ($pob[$j]+9)];
		print "No. of Beams : $subd[2]\n";
		$damage = $damage + $subd[1] * $subd[3] * round(100 / $subd[2]);
	}
	print "FIRED TORPEDOS : $damage\n";
	return($damage);
}
