#!/bin/awk -f
#
#                               delta
#
#                written by Ken Stevens for chainsaw 3.0
#
# DESCRIPTION:
# This script reads through your production info and tells you whether you
# have a positive or negative delta on iron, dust, lcm, etc...
# I find it useful for balancing production in large economies.
# The output looks something like this:
#
#     ----PRODUCTION DELTA REPORT----
#   com    start        +        -    delta      net
# ----------------------------------------------------
#  food      882      857      267      589     1471
#  iron      719      466      575     -109      610
#   oil       78       27       20        7       85
#  dust      121      102       79       23      144
#   lcm      228      216      116      100      328
# ----------------------------------------------------
#  bars        5       15                         20
#   mil      130      156                        286
#
# INSTALLATION:
# Before you run this script, make sure that you have typed:
# chmod +x delta
# where delta is the name of this file.
#
# HOW TO RUN IT:
# Set the variable MAX_CIV_PER_SECTOR to what the "nation" command tells you
# your max civs per sector is.  (This variable is set at the top of
# the script, just a few lines below here.)
#
# If you use eif, then you should just put the following two lines in
# your .eifrc file:
#
# alias del "cen # >!.delta ; com # >>.delta ; prod # >>.delta ; @delta .delta"
# alias cdel "cen # >!.cdelta ; prod # >>.cdelta ; @delta .cdelta"
#
# Then in eif you can type "del" to get your production delta.  The extra
# feature "cdel" tells you approximately how many updates until your civs
# max out.
#
# If you don't use eif, then all I have to say is "you should".  But I
# may be biased, because my cousin wrote it.
#
# BUG REPORTS:
# mail your bug-reports and comments to:
# stevens@math.utoronto.ca

BEGIN {
  MAX_CIV_PER_SECTOR = 558;

  if (etu=="") etu=60;
  if (maxpop=="") maxpop=550;
}

function num(str)
{
  return substr(str,1,length(str)-1);
}

{
  if ($5 != "1993" && $1 != "sect") {
    if (com) {
      {
	ss += $5;
	sg += $6;
	sp += $7;
	si += $8;
	sd += $9;
	sb += $10;
	so += $11;
	sl += $12;
	sh += $13;
	sr += $14;
      }
    } else if (prod) {
      if ($2 == "m") {i += $5}
      if ($2 == "g") {d += $5}
      if ($2 == "o") {o += $5}
      if ($2 == "%") {p += $5; oc += num($9)}
      if ($2 == "j") {l += $5; ic += num($9)}
      if ($2 == "k") {h += $5; ic += num($9)}
      if ($2 == "l") {lc += num($9)}
      if ($2 == "b") {b += $5; dc += num($9)}
      if ($2 == "i") {s += $5; lc += num($9); hc += num($10)}
      if ($2 == "d") {g += $5; oc += num($9); lc += num($10); hc += ($11)}
      if ($2 == "t") {dc += num($9); oc += num($10); lc += num($11)}
      if ($2 == "r") {dc += num($9); oc += num($10); lc += num($11)}
      if ($2 == "e") {m += $5}
      if ($2 == "a") {f += $5}
      if ($2 == "^") {mountain += 1}
    } else if (cen) {
      if ($7 == "*") {
	sc += $8;
	sm += $9;
	su += $10;
	sf += $11;
      } else {
	sc += $7;
	sm += $8;
	su += $9;
	sf += $10;
      }
    }
  }
}

/CENSUS/ {cen = 1; com = 0; prod = 0; }
/COMMODITIES/ {com = 1; cen = 0; prod = 0; do_delta = 1; }
/PRODUCTION/ {prod = 1; cen = 0; com = 0; }
/sectors/ {if (cen) sectors = $1; }


END {
  if (do_delta) {
    c = sc*etu*5/1000;
    u = su*etu*2.5/1000;
    fc += ((sc+sm+su)*etu/1000)+((c+u)*12/1000);
    print "     ----PRODUCTION DELTA REPORT----";
    printf "%5s %8s %8s %8s %8s %8s\n",
    "com","start", "+", "-", "delta", "net";
    print "----------------------------------------------------";
    if (f||fc) {printf "%5s %8d %8d %8d %8d %8d\n",
		  "food", sf, f, fc, f-fc, sf+f-fc}
    if (i||si) {printf "%5s %8d %8d %8d %8d %8d\n",
		  "iron", si, i, ic, i-ic, si+i-ic}
    if (o||so) {printf "%5s %8d %8d %8d %8d %8d\n",
		  "oil", so, o, oc, o-oc, so+o-oc}
    if (d||sd) {printf "%5s %8d %8d %8d %8d %8d\n",
		  "dust", sd, d, dc, d-dc, sd+d-dc}
    if (l||sl) {printf "%5s %8d %8d %8d %8d %8d\n",
		  "lcm", sl, l, lc, l-lc, sl+l-lc}
    if (h||sh) {printf "%5s %8d %8d %8d %8d %8d\n",
		  "hcm", sh, h, hc, h-hc, sh+h-hc}
    print "----------------------------------------------------";
    if (b||sb) {printf "%5s %8d %8d %8s %8s %8d\n",
		  "bars", sb, b, "", "", sb+b}
    if (p||sp) {printf "%5s %8d %8d %8s %8s %8d\n",
		  "pet", sp, p, "", "", sp+p}
    if (s||ss) {printf "%5s %8d %8d %8s %8s %8d\n",
		  "shell", ss, s, "", "", ss+s}
    if (g||sg) {printf "%5s %8d %8d %8s %8s %8d\n",
		  "guns", sg, g, "", "", sg+g}
    if (c||sc) {printf "%5s %8d %8d %8s %8s %8d\n",
		  "civ", sc, c, "", "", sc+c}
    if (u||su) {printf "%5s %8d %8d %8s %8s %8d\n",
		  "uw", su, u, "", "", su+u}
    if (m||sm) {printf "%5s %8d %8d %8s %8s %8d\n",
		  "mil", sm, m, "", "", sm+m}
  }
  else {
    sectors -= mountains;
    civmax = sectors*maxpop;
    civ = sc + sm;
    uw = su;
    foo = sf;
    fprod = f;
    print "        ----FOOD DELTA REPORT----";
    print "	 start			  food	  food	  food";
    print "update	  food	   civ	    uw	 eaten	  prod	 delta";
    print "-------------------------------------------------------";
    for (i = 1; i <= 10; ++i)
      {
	cbaby = civ*etu*5/1000
	  ubaby = uw*etu*2.5/1000;
	eaten = ((civ+uw)*etu/1000)+((cbaby+ubaby)*12/1000);
	delta = fprod - eaten;
	printf " %2d:\t%6d\t%6d\t%6d\t%6d\t%6d\t%6d\n",
	i, foo, civ, uw, eaten, fprod, delta;
	foo += delta;
	civ += cbaby;
	if (civ > civmax) {civ = civmax}
	uw += ubaby;
      } 
    printf "Maximum civs for %d sectors: %d\n", sectors, civmax;
  }
}
