#! /usr/bin/perl -P

# Plague predicter.
# usage: plague [-t threshold] nation commod census
# Gives the sectors with high probability of developing
# plague.
#
# Example: plague -t 25 ../empdata/{census,commod,nation}
#

#include "empinit.perl"
do 'getopt.pl';

$THRESH = 0.5;	# 50% likelihood or greater by default.
do Getopt('t');
if ($opt_t) {$THRESH = $opt_t/100;}

do empinit('nation', 'commod', 'census');

if ($tech <= 10.0) {
    print "You can only get plague with a tech factor greater than 10.0\n";
    exit 0;
}

;# Grab the relevant values from each sector.  Compute the plague
;# factor.  If it is greater than the threshold, save it in a
;# list for later.
foreach $sec (@sec_list) {
    $civ = $sectors{$sec, CIV};
    $mil = $sectors{$sec, MIL};
    $uw = $sectors{$sec, UW};
    $iron = $csectors{$sec, CIRON};
    $petrol = $csectors{$sec, CPET};
    $effic = $sectors{$sec, EFF};
    $mobil = $sectors{$sec, MOB};
    $plague = (($civ + $mil + $uw) / 999) 
	    * (($tech + ($iron + $petrol)/10 + 100)/
	    ($research + $effic + $mobil + 100));
    if ($plague > $THRESH) {
	push(@plaguelist, $sec);
	$plagues{$sec} = $plague;
    }
}

if ($#plaguelist == -1) {
    # If the list is empty...
    
    print "Your sectors are safe.\n";
    exit 0;
} else {
    print "Plague report:\n";
    # Otherwise, sort the list in order of plague, and print
    # it out.

    @plagues = sort byplaguefactor @plaguelist;
    foreach $sec (@plagues) {
	printf "%s: %2d%%\n", $sec, $plagues{$sec}*100;
    }
}

sub byplaguefactor {
    $plagues{$a} > $plagues{$b} ? -1 : $plagues{$a} < $plagues{$b} ? 1 : 0;
}
