#!/usr/local/bin/perl
#
# hungry: see which sectors may be in food trouble
#
# Uses a dump file in .dump, and a version file in ver
#
# Assumes Berkeley 1.1 (pl 5, if that matters)
#
# Apr 91 Jim Gillogly, Banzai Institute

open(foo, "dump") || die("I was assuming you'd do \"dump # >! dump\".\n");
open(ver, "ver")   || die("I need the version: \"ver >! ver\".\n");

printf " Sector des    civs    mil      uw       Food\n";

while (<ver>)   # Extract game variables from the version.
{       $fcrate  = $2/$1 if (/(\d+) civilians* will harvest (\S*) food per etu/);
	$obrate  = $2/$1 if (/(\d+) civilians* will give birth to (\S*) babies/);
	$uwbrate = $2/$1 if (/(\d+) uncomp.* birth to (\S*) babies/);
	$eatrate = $2/$1 if (/(\d+) people eat (\S*) units of food/);
	$babyeat = $2/$1 if (/(\d+) bab.* eat (\S*) units/);
	$fgrate  = $1 if (/can grow (\S*) food per etu/);
	$etu     = $1 if (/update consists of (\S*) empire time units/);
}
close(ver);

while (<foo>)           # Check sectors in the dump one at a time
{
	$date = $1 if /^(.*:\d\d:*)$/;
	next if /sector/;
	if (/x y/)      # Find the categories when we see header line
	{       chop;           # Skip the newline;
		s/\*/star/;     # What kind of field name is '*'??
		$nfields = 1 + s/ /, \$/g;      # Turn them into variables
		s/^/ \$/;                       # Including the 1st
		$splitter = "(" . $_ . ") = split"; # To eval each line.
		next;

	}

	# This bit is deucedly clever.  Handles unknown dump formats.

	$matches = eval($splitter);
	next if $matches < 10;          # 10 is a random number > 5

#       We just broke up the line into variables, like $eff, $gold, etc.

# Efficiency would get updated here, but the innards doc doesn't seem to
# reflect reality very well. Ignore for now.

	$oldfood = $food;
	$munch -= $food;

	$dd = $etu * $fert * $fgrate / 100;     # Amt grown in etu time units
	$dtemp = $work * $fcrate; # (avail??)   # Amount to harvest
	$dd = $dtemp if ($dtemp < $dd);         # Not enough people there
	$foodtmp = $food + $dd;
	# Ignore sanctuaries
	$dd = $etu * ($civ + $mil + $uw) * $eatrate;
	$food = $foodtmp - $dd;
	if ( $dd > $foodtmp )
	{
	print STDERR "$x,$y will starve: $dd > $foodtmp.\n";
	}
	{
	if ($des != 'w')
	{	$food = 999 if $food > 999;
	}
	$q = $etu * $civ * $obrate;
	if ($q > $food / (2 * $babyeat))
	{       $q = $food / (2 * $babyeat);
	}
	if ($q > 999 - $civ)
	{       $q = 999 - $civ;
		print STDERR "Sector $x,$y will max out on civs this time.\n";
	}
	$oldciv = $civ;
	$civ += $q;
	if ( 0 > $civ )
	{	$civ = 0;
	}
	}
	$food -= $q * $babyeat;
	$q = $etu * $uw * $uwbrate;
	if ($q > $food / (2 * $babyeat))
	{       $q = $food / (2 * $babyeat);
	}
	if ($q > 999 - $uw)
	{       $q = 999 - $uw;
		print STDERR "Sector $x,$y will max out on uws this time.\n";
	}
	$uw = 0 if ( $uw < 0 );
	$olduw = $uw;
	$uw += $q;
	if ( 0 > $uw )
	{	$uw = 0;
	}
	$food -= $q * $babyeat;
	print STDERR "\tNot enough food to encourage growth in $x,$y.\n"
		if ($food <= 0);

	printf "%7s %s %4d->%4d %4d %4d->%4d %4d->%4d\n",
	     "$x,$y", $des, $oldciv, $civ, $mil, $olduw, $uw, $oldfood, $food;

	$munch += $food;        # Count left-over loaves and fishes
	$total += $food;
	$toldc += $oldciv;
	$tciv += $civ;
	$toldu += $olduw;
	$tuw += $uw;
	$tmil += $mil;
}

close(foo);

printf "Food budget for this update:  %.2f\n", $munch;
printf "Total food (without agribiz): %.2f\n", $total;
printf "Total civs current - %d",$toldc;
if ($toldc != 0){
   printf", and projected - %d : %.2f %%\n", $tciv, (($tciv-$toldc)/$toldc)*100;
} else {printf "\n";}
printf "Total uws  current - %d",$toldu;
if ($toldu!=0){
   printf", and projected - %d : %.2f %%\n",$tuw,(($tuw-$toldu)/$toldu)*100;
} else {printf "\n";}
printf "Total mil  current - %d\n",$tmil;
