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

/*
 * $Log: addplayer.c,v $
 * Revision 2.47  1998/04/15 21:29:39  zeus
 * *** empty log message ***
 *
 * Revision 2.46  1997/04/18 12:06:12  zeus
 * *** empty log message ***
 *
 * 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:09  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.39  1993/10/28  22:31:20  bampton
 * Revision 2.37  1993/09/07  20:50:32  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:40  bampton RCS
 * 
 */

#define IO
#define NOTIMEB
#include "common.h"
#include "prototypes.h"
#include "misc-g.h"
#include "addplayer.h"
#include "options.h"

#ifndef MINSEP
#define MINSEP 20.0
#endif

extern player *players;
extern battle *battles;
extern double galaxysize;
extern planet *planets;
extern char buf[MAXSTRLEN];
extern int turn;
extern int randseed;
char err_tmp[2 * MAXSTRLEN + 1];

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

planet *
place_planet (void)
{
  int i;
  double x1, y1, x2 = 0, y2 = 0;
  planet *p;

  do
  {
    i = 0;
    x1 = frand (galaxysize);
    y1 = frand (galaxysize);
    for (p = planets; p && !i; p = p->next)
      /* Keep homeworlds separated by MINSEP LY */
      if (p->owner)
      {
        x2 = min (fabs (x1 - p->x), galaxysize - fabs (x1 - p->x));
        y2 = min (fabs (y1 - p->y), galaxysize - fabs (y1 - p->y));

        if (x2 * x2 + y2 * y2 <= MINSEP * MINSEP)
        {
          i = 1;
        }
      }
  }
  while (i);
  p = addplanet ();
  p->x = x1;
  p->y = y1;
  return (p);
}

void
addplayer (void)
{
  planet *p;
  player *P;
  char buf2[MAXSTRLEN];

  if (turn)
    printf ("Warning!!! Game in progress! Turn %d", turn);
  printf ("Player name? ");
  getstr ();
  if (!buf[0])
    return;
  if (nametop (players, buf))
  {
    printf ("Player name %s is already in use.\n", buf);
    return;
  }
  if (!legal_name (buf, EMPIRE))
  {
    printf (err_tmp);
    return;
  }
  strcpy (buf2, buf);           /* don't overwrite the player name */

#ifndef RANDPLACE
  p = inputplanet (planets, "System?");
  if (p)
    if (p->owner)
    {                           /* Safety feature, HJB */
      printf ("Planet %s is already owned.\n", p->name);
      return;
    }
    else
      return;
#endif

  P = alloc (sizeof (player));  /* actually add player */
  addlist (&players, P);
  strcpy (P->name, buf2);

#ifdef RANDPLACE
  p = place_planet ();
#else
  /* Manual placement .... */
  printf ("New X-coord? ");
  p->x = getf ();
  printf ("New Y-coord? ");
  p->y = getf ();
#endif

  p->owner = P;
  p->size = MAXPOP;
  p->resources = 10.0;
  p->pop = MAXPOP;
  p->ind = MAXPOP;
  p->cap = p->mat = p->col = 0.0;
  p->spent = p->inprogress = 0.0;
  P->homex = p->x;
  P->homey = p->y;              /* GE */
  P->tech.drive = 1.0;
  P->tech.guns = 1.0;
  P->tech.shields = 1.0;
  P->tech.cargo = 1.0;
  P->opts1 = MAP | FULL | PROD | BATTLESUM | FULLTYPE | MAPPLUS | BOMBSUM;
  P->opts2 = SHOWOPT | SORTPLANETS | TECHPLUS;
  P->opts3 = P->lastorders = 0;
  P->x = 0;
  P->y = 0;
  P->size = galaxysize;
  P->othertechs = alloc (sizeof (techinfo));
  P->othertechs->tech.drive = 0.0;
  P->othertechs->tech.guns = 0.0;
  P->othertechs->tech.shields = 0.0;
  P->othertechs->tech.cargo = 0.0;
  P->othertechs->next = NULL;
  P->othertechs->whoose = NULL;

  sortplanets ();
}


planet *
inputplanet (planet * head, char *prompt)
{
  planet *p;

  printf ("%s%c", prompt, lf);
  getstr ();
  if (buf[0])
  {
    if ((p = nametoplan (head, buf)) != 0)
      return p;
    printf ("System \"%s\" not found.\n", buf);
  }
  else
    printf ("No planet given\n");
  return 0;
}
