static char *rcsid = "$Header: /plaid/homes/zeus/Blind-2.47/RCS/Blind.c,v 2.47 1998/04/15 21:34:17 zeus Exp zeus $";

/*
 * $Log: Blind.c,v $
 * Revision 2.47  1998/04/15 21:34:17  zeus
 * *** empty log message ***
 *
 * Revision 2.46.0.1  1997/05/27 21:03:51  zeus
 * Revision 2.46  1997/04/18 12:06:12  zeus
 * Revision 2.45  1996/12/31  23:40:54  zeus
 * Revision 2.44  1995/12/31  19:45:18  zeus
 * Revision 2.43  1995/03/20  01:43:00  zeus
 * Revision 2.42  1994/05/17  13:43:15  bampton
 * Revision 2.41  1994/04/08  03:11:28  bampton
 * Revision 2.40  1994/03/09  20:22:55  bampton
 * Revision 2.38  1993/09/27  17:15:54  bampton
 * Revision 2.37  1993/09/07  20:50:32  bampton
 * Revision 2.36  1993/08/19  16:32:29  bampton
 * Revision 2.34  1993/08/09  23:06:20  bampton
 * Revision 2.33  1993/08/03  23:10:13  bampton
 * Revision 2.30  1993/07/14  14:59:26  bampton
 * Revision 0.0  1993/07/14  13:41:34  bampton RCS prototype.
 *
 */

/*
 * Galaxy game Copyright 1991 by Russell Wallace. This is a text-based
 * strategy game for 2 or more players, suitable for    playing directly or
 * by mail. Written in standard ANSI C, should compile & run on any system.
 * See documentation for details. Will work with 16-bit ints but 32-bit ints
 * recommended as 16-bit limits some quantities.  This code may be freely
 * used and distributed non-commercially provided that this message is not
 * altered or omitted. It may not be sold or used commercially without prior
 * written permission from the author.
 */

/* Blind Galaxy Copyright 1992, 1993, 1994, 1995, 1996, 1997 by Howard Bampton 
   bampton@cs.utk.edu 
 */

#define NOTIMEB

#include "common.h"
#include "prototypes.h"
#include "new-game.h"
#include "turn-g.h"
#include "exit.h"
#include "report-g.h"

int main (int argc, char *argv[]);
void blind_rcsid (void);
void Banner (void);

extern player *players;
extern battle *battles;
extern bombing *bombings;
extern double galaxysize;
extern planet *planets;
extern int turn;
extern shiptype *shiptypes;
extern int file;
extern char buf[], buf2[];
extern FILE *pr;
extern int randseed;
extern planetdefense *defender;

#ifdef GAMENAME
char gamename[MAXTEXT];

#endif

#ifdef PREPROC
extern int bad;
extern int turn_run;

#endif

int nplanets;

void
blind_rcsid (void)
{
  printf ("%s\n", rcsid);
}

int
main (int argc, char *argv[])
{

  float version = VER;

#ifndef PREPROC
  planet *p;

#ifdef REPORTDIR
  char reportd[] = REPORTDIR;

#endif
  int i;
  player *P;
  char fileext[MAXTEXT * 2];

#endif

  printf ("Galaxy v2.9 by Russell Wallace  " __DATE__ "\n");
  printf ("Blind  v%4.2f\n", version);
  if (opengame () >= 0)
  {
    loadgame ();
    Banner ();
  }

  /* MAIN INPUT LOOP */

  for (;;)
  {

#ifndef PREPROC
    printf ("> ");
#endif

    /* Use supplied comd line args as commands, if provided. */
    if (argc > 1)
    {
      hstrncpy (buf, argv[1], MAXSTRLEN);       /* Bugfix 5/12/97 */
      argc--;
      argv++;
    }
    else
      getstr ();

    switch ((unsigned) tolower (buf[0]))
    {

#ifndef PREPROC
    case '+':                   /* redo seed */
      printf ("New random seed?");
      randseed = geti ();

#if HPUX9 || SOLARIS || USESRAND48
      srand48 ((long) randseed);
#else
      srandom (SRANDTYPE randseed);
#endif

      break;
    case '*':                   /* print renamed planets */
      for (p = planets; p; p = p->next)
        if (strcmp (p->num, p->name) != 0)
          printf ("%s\t%s\n", p->num, p->name);
      break;
#endif

    case 'x':                   /* exit, no changes */
      return 0;
    case 'q':

#ifdef PREPROC
      /* Preprocessor doesn't save game.... */
      return (bad);
#else
      savegame ();

#ifdef BACKUPS
      {
        char tmp[MAXSTRLEN];

        sprintf (tmp, "cp blindrc blindrc.%d.bak", turn);
        system (tmp);
      }
#endif

      return 0;

    case 'a':                   /* Get all reports */
      strcpy (fileext, ".r");
      printf ("Filename extension? [.r] ");
      getstr ();
      if (buf[0])
      {
        hstrncpy (fileext, buf, MAXTEXT);
        if (buf[0] != '.')
          sprintf (fileext, ".%s", buf);
      }
      for (P = players; P; P = P->next)
      {
        i = strlen (P->name);
        while (i && P->name[i - 1] != ' ' && P->name[i - 1] != '\'')
          i--;

#ifdef REPORTDIR
        sprintf (buf, "%s/%s%s", reportd, P->name + i, fileext);
#else
        sprintf (buf, "%s%s", P->name + i, fileext);
#endif

        pr = fopen (buf, "w");
        if (pr == 0)
        {
          printf ("Can't open %s\n", buf);
          break;
        }
        printf ("Writing report %s\n", buf);
        report (P);
        fclose (pr);
      }
      break;

    case 'r':                   /* Get single report */
      P = inputplayer (players, "Player?");
      if (P)
      {
        inputpr ();
        report (P);
        if (pr && (pr != stdout))
          fclose (pr);
      }
      break;
    case 's':                   /* Player dip status */
      {
        player *P1, *P2;

        P1 = inputplayer (players, "Asking Player? ");
        P2 = inputplayer (players, "about Player? ");
        dip_status (P1, P2);
      }
      break;
    case 'k':                   /* full echo status for all players */
      inputpr ();
      player_status ();
      if (pr && (pr != stdout))
        fclose (pr);
      break;
    case 'd':                   /* Overall Battle Summary */
      inputpr ();
      gm_battle ();
      if (pr && (pr != stdout))
        fclose (pr);
      break;
    case 'c':                   /* Overall Bombing Summary */
      inputpr ();
      gm_bombing ();
      if (pr && (pr != stdout))
        fclose (pr);
      break;
    case 'f':                   /* full listing for all planets */
      inputpr ();
      showfull ();
      if (pr && (pr != stdout))
        fclose (pr);
      break;
    case 'l':
      inputpr ();
      planet_list ();           /* Show planet sizes, loc's and such */
      if (pr && (pr != stdout))
        fclose (pr);
      break;
    case 'm':
      gm_map ();
      break;
    case 'g':
      go ();
      break;
    case 'y':
      inputpr ();
      forward_list ();
      if (pr && (pr != stdout))
        fclose (pr);
      break;
    case 'b':
      for (P = players; P; P = P->next)
      {
        if (abs (P->lastorders) == turn + 1)
          printf ("%s has orders for this turn\n", P->name);
      }
      break;
#endif

    case 't':
      do
      {
        enterturn (NORUN);
      }
      while (!feof (stdin));
      break;
    case 'e':
      enterturn (RUN);

#ifdef PREPROC
      {
        player *P = NULL;

        if (!turn_run)
        {
          for (P = players; P; P = P->next)
          {
            if (turn < abs (P->lastorders))
              break;
          }
          if (!P)               /* No orders at all */
            bad = EMPTYORDERS;
        }
      }
      break;

    case 'f':
      enterturn (NORUN);
#endif

      break;

    case 'v':
      version_info ();
      break;

    case 'z':
      full_version ();
      break;

#ifndef PREPROC
    default:
      printf (
               "A = Report on All Players\n"
               "R = Report for One Player\n"
               "E = Enter Turn\n"
               "T = Enter Turns until EOF\n"
               "G = Go\n"
               "Q = Quit\n"
               "X = Exit without saving\n"
               "\n"
               "* = List renamed systems\n"
               "+ = New random seed\n"
               "B = Current order status\n"
               "C = Current Bombing Summary\n"
               "D = Current Battle Summary\n"
               "F = Full report on all systems\n"
               "K = Kurrent players status\n"
               "L = pLanet info\n"
               "M = Map\n"
               "S = Diplomatic status\n"
               "V = Version info\n"
               "Y = Forwarding list\n"
               "Z = RCS Version info\n"
        );
#else
    default:
      printf ("E = Enter Turn\n"
              "F = Enter Turn (check orders only)\n"
              "Q = Quit\n");
#endif
    }
  }
}


void
Banner (void)
{

#ifdef PREPROC
  printf ("Preprocessor version\n");
  printf ("Turn %d\n", turn);
#else

  double size;
  planet *p;

#ifdef GAMENAME
  printf ("\nGamename:%s\n", gamename);
#endif

  size = 0;
  for (p = planets; p; p = p->next)
  {
    size += p->size;
  }
  printf ("\nGalaxy Size ");
  fpf (stdout, galaxysize);
  printf ("\n%d Systems with Total Size ", nplanets);
  fpf (stdout, size);
  printf ("\nTurn:%d Seed:%d", turn, randseed);
  putchar ('\n');
#endif
}
