/*LINTLIBRARY*/

/*  @(#)remark.c 1.2 90/04/09
 *
 *  Remark making code used by othello program.
 *
 *  Copyright: Chris Miller, 1979, 1981, 1984
 *
 *  Included with this othello game by
 *          Rich Burridge, Sun Microsystems, Australia.
 *
 *  Permission is given to distribute these sources, as long as the
 *  introductory messages are not removed, and no monies are exchanged.
 *
 *  No responsibility is taken for any errors on inaccuracies inherent
 *  either to the comments or the code of this program, but if reported
 *  to me, then an attempt will be made to fix them.
 */

#include "othello.h"
#include "extern.h"


make_remark(machine_piece, value)
int machine_piece,value ;
{
  char mes[100] ;

  value *= machine_piece ;
  if (value < -INFINITY+64)
    {
      last_remark = FIRST_REMARK ;     /* Always remark next time */
      if (aspire == MAXASPIRE)
        SPRINTF(mes,"You should win  (by at most %d)",-value-INFINITY+64) ;
      else STRCPY(mes,"You should win.") ;
    }
  else if (value < -1000)
    {
      if (last_remark != -INFINITY)
        {
          cretin_flag = TRUE ;
          last_remark = -INFINITY ;
          STRCPY(mes,"Only a cretin could lose now.") ;
        }
    }
  else if (value <= 1000) printr(value, mes) ;
  else if (value < INFINITY-63)
    {
      if (last_remark != INFINITY)
        {
          STRCPY(mes,"Resign, you dolt! Resistance is futile.") ;
          last_remark = INFINITY ;
        }
    }
  else
    {
      last_remark = FIRST_REMARK ;
      if (aspire == MAXASPIRE)
        SPRINTF(mes, "I shall win  (by at least %d).", value-INFINITY+64) ;
      else STRCPY(mes, "I shall win") ;
    }
  message(REMARK_MES, mes) ;
}


/*  Set up file pointers to remarks; the format of the remarks file is:
 *
 *  Remarks to the effect that machine is winning, in increasing order of
 *  strength, one per line.
 *     
 *  A line beginning with the character "@".
 *     
 *  Remarks to the effect that human is winning, in increasing order of
 *  strength, one per line.
 */

get_remarks()
{
  long fp = 0l ;
  int ch ;

  open_remfile(rem_file) ;
  if (rem_fp == NULL)
    {
      message(REMARK_MES, "Remarks not available") ;
      return ;
    }

  while ((ch = getc(rem_fp)) != EOF)
    {
      if (ch == '@') break ;
      good_remarks[max_good++] = fp++ ;
      if (max_good == MAX_REMARKS)
  {
    message(REMARK_MES, "Too many remarks") ;
    return ;
  }
      while (ch != '\n')
  {
    ch = getc(rem_fp) ;
    fp++ ;
  }
    }
  max_good-- ;

  fp++ ;
  while (ch != '\n')
    {
      ch = getc(rem_fp) ;
      fp++ ;
    }
 
  while ((ch = getc(rem_fp)) != EOF)
    {
      bad_remarks[max_bad++] = fp++ ;
      if (max_bad == MAX_REMARKS)
  {
    message(REMARK_MES, "Too many remarks") ;
    return ;
  }
      while (ch != '\n')
  {
    ch = fgetc(rem_fp) ;
    fp++ ;
  }
    }
  max_bad-- ;
}
 

open_remfile(remname)     /* Open remark file if present. */
char *remname ;
{
  char *getenv(), name[MAXLINE], *paths, *ptr ;
  int i ;

  i = 0 ;
  if ((rem_fp = fopen(remname, "r")) == NULL)
    {
      paths = getenv("PATH") ;
      if ((ptr = paths) && remname[0] != '/')
        for (;;)
          if (*ptr == ':' || *ptr == 0)
            {
              if (*ptr == 0) break ;
              name[i++] = '/' ;
              name[i] = 0 ;
              STRCAT(name, remname) ;
              if ((rem_fp = fopen(name, "r")) != NULL) return ;
              if (*ptr == '\0') break ;
              *ptr++ ;
              i = 0 ;
            }
          else name[i++] = *ptr++ ;
    }
}


/*  Locate file-pointer and print appropriate line. The strange
 *  computation is to cluster remarks towards low evaluations,
 *  since the evaluation function changes more rapidly with small
 *  changes in position as the evaluation gets larger
 */

printr(n, string)
int n ;
char string[100] ;
{
  int ch ;
  int index ;
  float findex ;
  int sign_n = (n < 0 ? -1 : 1) ;
  char *stringp = string ;

  if (rem_fp == NULL) return ;

  findex = (float) abs(n) / 1000.0 ;
  index = findex * (2-findex) * (n < 0 ? max_bad : max_good) + 0.5 ;

/* Don't make the same remark twice in a row */

  if (index * sign_n == last_remark) return ;

  last_remark = index * sign_n ;
  FSEEK(rem_fp, n < 0 ? bad_remarks[index] : good_remarks[index], 0) ;
  while ((ch = getc(rem_fp)) != EOF)
    {
      if (ch == '\n') break ;
      *stringp++ = ch ;
    }
  *stringp = '\0' ;
}
