1#!/usr/local/bin/perl
2#
3# Copyright (C) 2002-2003 by Johan van Selst and Marc Olzheim
4#
5# Show the cost of building things in a specific sector
6#
7#alias lcost "show l b >! tmp/buildcost.sb; ldump ${1:-*} type x y eff >! tmp/buildcost.dam; dump ${1:-*} x y lcm hcm mil avail >! tmp/buildcost.stat; @/usr/local/lib/eif/buildcost.pl land"
8#alias pcost "show p b >! tmp/buildcost.sb; pdump ${1:-*} type x y eff laun >! tmp/buildcost.dam; dump ${1:-*} x y lcm hcm mil avail >! tmp/buildcost.stat; @/usr/local/lib/eif/buildcost.pl plane"
9#alias scost "show s b >! tmp/buildcost.sb; sdump ${1:-*} type x y eff >! tmp/buildcost.dam; dump ${1:-*} x y lcm hcm mil avail >! tmp/buildcost.stat; @/usr/local/lib/eif/buildcost.pl ship"
10#
11
12chdir "tmp" || die "No tmp/ dir";
13
14if ($#ARGV != 0) { die "Usage: $0 <land|plane|ship>"; }
15
16my %buildcost;
17
18open SB, "<buildcost.sb" || die "Should not happen !";
19while (<SB>)
20{
21#	lcm hcm avail tech for ships
22#	lcm hcm guns avail for units
23#	lcm hcm crew avail for planes
24	if (/^(\S+)\s+(\S+ )+\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/) {
25		my @arr = ($3,$4,$5,$6);
26		$buildcost{$1} = \@arr;
27	}
28}
29close SB;
30
31my %tlcm, %thcm, %tmil, %tavail, %str;
32open DAM, "<buildcost.dam" || die "Should not happen !";
33if ($ARGV[0] eq "plane")
34{
35	while (<DAM>)
36	{
37		next unless /^(\d+)\s+(\S+)\s+(-?\d+)\s+(-?\d+)\s+(\d+)\s+N$/;
38
39		if ($5 < 100) {
40			my $fact = (100-$5) * 0.01;
41			($lcm, $hcm, $mil, $avail) = @{$buildcost{$2}};
42			$tlcm{"$3,$4"} += $fact * $lcm;
43			$thcm{"$3,$4"} += $fact * $hcm;
44			$tmil{"$3,$4"} += $fact * $mil;
45			$tavail{"$3,$4"} += $fact * $avail;
46			$str{"$3,$4"} .= " ($5% $1($2))";
47		}
48	}
49}
50else
51{
52	while (<DAM>)
53	{
54		next unless /^(\d+)\s+(\S+)\s+(-?\d+)\s+(-?\d+)\s+(\d+)$/;
55
56		if ($5 < 100) {
57			my $fact = (100-$5) * 0.01;
58			if ($ARGV[0] eq "ship")
59			{
60				($lcm, $hcm, $avail, $unused) = @{$buildcost{$2}};
61			}
62			else
63			{
64				($lcm, $hcm, $unused, $avail) = @{$buildcost{$2}};
65			}
66			$tlcm{"$3,$4"} += $fact * $lcm;
67			$thcm{"$3,$4"} += $fact * $hcm;
68			$tavail{"$3,$4"} += $fact * $avail;
69			$str{"$3,$4"} .= " ($5% $1($2))";
70		}
71	}
72}
73close DAM;
74
75$alcm = $ahcm = $aavail = $amil = 0;
76my %hlcm, %hhcm, %hmil, %avail;
77
78open STAT, "<buildcost.stat" || die "Should not happen !";
79while (<STAT>)
80{
81	next unless /^-?\d+\s+-?\d+\s+/;
82	($x, $y, $des, $sdes, $alcm, $ahcm, $amil, $aavail) = split /\s+/;
83	$hlcm{"$x,$y"} = $alcm;
84	$hhcm{"$x,$y"} = $ahcm;
85	$hmil{"$x,$y"} = $amil;
86	$havail{"$x,$y"} = $aavail;
87	$des{"$x,$y"} = $des;
88	$sdes{"$x,$y"} = $sdes;
89}
90close STAT;
91
92sub perc
93{
94	($t, $a) = @_;
95	return sprintf "\x1b[%dm%d/%d\x1b[0m", $t > $a ? 31 : 32, $t, $a;
96}
97
98foreach $i (keys %str)
99{
100	next unless ($des{$i});
101
102	print ucfirst($ARGV[0]) . "s in $i ($des{$i}";
103	if (! ($sdes{$i} eq "_"))
104	{
105		print "$sdes{$i}";
106	}
107	print "): $str{$i}\n";
108	if ($ARGV[0] eq "plane")
109	{
110		printf "Building costs:  %s lcm   %s hcm   %s mil   %s avail\n\n",
111			perc($tlcm{$i}, $hlcm{$i}), perc($thcm{$i}, $hhcm{$i}), perc($tmil{$i}, $hmil{$i}), perc($tavail{$i}, $havail{$i});
112	}
113	else
114	{
115		printf "Building costs:  %s lcm   %s hcm   %s avail\n\n",
116			perc($tlcm{$i}, $hlcm{$i}), perc($thcm{$i}, $hhcm{$i}), perc($tavail{$i}, $havail{$i});
117	}
118}
119
120