/* MP_UTILS.C */
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include <dos.h>
#include "defines.h"

extern PLAYER player[MAXFL][MAXSZ];
extern int NP[], order[];
extern char mark[];
extern int no_space;

/* function prototypes */
void
  error_exit(char *),
  hide_cursor(void),
  showcursor(void);

int
  find(char *, int *),
  findn(char *, int *, int *),
  Getint(int, char *, int *),
  getkey(void),
  get_string(char *, char *, char *, int, int);

/* ---------------------------------------------------------------------- */
void
error_exit(char *msg)
{
   cputs(msg);
   showcursor();
   exit(1);
}

/* ---------------------------------------------------------------------- */
int
find(char *pname, int *flight)  /* find flight and index of player 'pname' */
{
  int m,k;

  for (k=0; k<MAXFL; k++) {
    if (NP[k] == 0) continue;
    for (m=0; m<NP[k]; m++) {
      if (strcmp(player[k][m].name,pname) == 0) {
        *flight = k;
        return m;
      }
    }
  }
  return -1;
}

/* ---------------------------------------------------------------------- */
/* find up to 10 players with names starting 'pname*', put in flt[],index[] */
int
findn(char *pname, int flt[], int index[])
{  
  int m, j=-1, k, n = strlen(pname);

  for (k=0; k<MAXFL; k++) {
    if (NP[k] == 0) continue;
    for (m=0; m<NP[k]; m++) {
      if (strncmpi(player[k][m].name, pname, n) == 0) {
        if (++j > 9)
          return -2;
        flt[j] = k;
        index[j] = m;
      }
    }
  }
  return j;
}

/* ---------------------------------------------------------------------- */
/*     extract n pos int values from string s, put them in array vi[]     */
int
Getint(int n, char *s, int vi[])
{
  int cnt=0;
  char *p;

  if (n == 0) return cnt;

  p = s;
  while (cnt < n && *p)
    {
     while (*p && !isdigit(*p)) p++;
     if (*p) {
       vi[cnt] = atoi(p);
       while (isdigit(*p)) p++;
       cnt++;
     }
    }
  return cnt;
}

/* ---------------------------------------------------------------------- */
                 /* basic input loop feeder */
int 
getkey(void)
{
 int k;

 k = getch();
 if (k == 0) k = -getch();
 return k;
}

/* ---------------------------------------------------------------------- */
int
get_string(char *prompt, char *dest, char *mask, int col, int row)
{
   int i, c, pos=0, maxpos=strlen(mask);

   if (maxpos == 0) maxpos = 20;     /* set default string length */
   for (i=0; i<maxpos; i++)
     if (mask[i] == '-') dest[i] = '-'; else dest[i] = 0;

   if (strlen(prompt)) {
     gotoxy(col-strlen(prompt)-2, row);
     cputs(prompt); cputs(": ");
   }
   else
     gotoxy(col,row);
   cputs(mask);
   gotoxy(col,row); showcursor();
   while (kbhit())
     getch();                    /* clear kb buffer */
   while (1) {
     c = getkey();
     if (c == ' ' && no_space)
       continue;
     if (isprint(c) && pos<maxpos) {
       cprintf("%c",c);
       dest[pos] = c;
       while (pos++ < maxpos && mask[pos] == '-')
         ;
       gotoxy(col+pos, row);
     }
     else if (c==8 && pos>0) {
       while (pos-- > 0 && mask[pos] == '-')
         ;
       gotoxy(col+pos, row);
       if (mask[0])
         cputs(mark);          /* if using input mask, restore when bk */
       else
         putch(' ');
       gotoxy(col+pos, row);
     }
     else if (c == CR || c == ESC || c == END)  break;
   }
   hide_cursor();
   for (i=pos; i<maxpos; i++)
     cputs(" ");
   if (c == CR || c == END)
     dest[pos] = '\0';
   else
     dest[0] = '\0';
   if (c == END)
     return -strlen(dest);
   return strlen(dest);
}

/* ------------------------- hide the cursor -------------------------- */
void
hide_cursor()
{
  _AH = 1;
  _CH = 0x20;
  geninterrupt(0x10);
}

/* ------------------------- show the cursor ------------------------------ */
void
showcursor()
{
  _AX = 0x0100;
  _BX = 0;
  _CX = 0x0c0d;
  geninterrupt(0x10);
}

