#!/bin/awk -f
#
#	pathfind
#
#	Finds path costs to distribution points.  If fed into simu, will
#	dramatically speed up simulation, at the cost of some accuracy.
#		-the harmless conquerer


BEGIN {
  SUBSEP=",";
}

#
#	Headers for various multi-line read modes
#
/ *DUMP SECTOR/ { mode="dump"; stat=1; next; }

#
#	Parse continuation lines of multi-line modes
#
{
  if (mode=="dump") {
    if (stat==1) {
      for (i=1;i<=NF;i++) header[i]=$i;
      nheader=NF;
      stat=2;
      dsects=0;
      for (i in order) delete order[i];
      next;
    } else if (NF != nheader) {
      stat=0;
      mode="";
    } else if (stat==2) {
      for (i=1;i<=nheader;i++) temp[header[i]] = $i;
      sect = temp["x"] "," temp["y"];
      if (temp["own"]=="") temp["own"]=myno;
      for (i=1;i<=nheader;i++) val[sect,header[i]] = $i;
      sects[sect]="1";
      order[dsects++] = sect;
      next;
    }
    mode="";
  }
}




END {
  for (i=0;i<dsects;i++) {
    sect=order[i];
    printf("bestpath %s %d,%d\n",sect,val[sect,"dist_x"],val[sect,"dist_y"]);
    printf("bestpath %d,%d %s\n",val[sect,"dist_x"],val[sect,"dist_y"],sect);
  }
}
