/*
 *  inlcmds.c
 */

/*
   Command interface routines specific to the INL robot.  This is a
   replacement for the spagetti compile method used previously in
   commands.c .  By seperating the code it will be possible to list
   only those commands relevant at any given moment.  Something that
   would be a horror to implement in the old scheme..
*/

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#include "defs.h"
#include "struct.h"
#include "data.h"

#include "gencmds.h"
#include "inldefs.h"

const char myname[] = {"INL"};

extern int do_home();
extern int do_away();
extern int do_start();
extern int do_captain();
extern int do_uncaptain();
extern int do_tname();
extern int do_pickside();
extern int do_resetgalaxy();
extern int do_switchside();
extern int do_pause();
extern int do_free();
extern int do_gametime();
extern int do_timeout();
extern int do_confine();

extern int cleanup();


/********* COMMANDS LIST ********
	Note: - The commands *must* be in upper case. All commands are converted
		to upper case before be checked. 
	      - Votes are entered in the Voting list.
	The first field is the command, the second is a description string 
		(up to 50 chars.), lastly is the function to call.
*/

struct command_handler_2 inl_commands[] = {
    { "INL server commands.", C_DESC },
    { "Possible commands are:", C_DESC },
    { "HELP",
		0,
		"Show this help information",
		do_help },     			/* HELP */
    { "GAMETIME",
		0,
		"Shows the game time or sets it. Ex: 'GAMETIME 30 10'",
		do_gametime },			/* GAMETIME */
    { "CAPTAIN",
		0,
		"Make yourself captain of team.",
		do_captain },			/* CAPTAIN */
    { "|Pregame commands:", C_DESC | C_PR_PREGAME },
    { "HOME",
		C_PR_PREGAME,
		"Switch to home team.",
		do_switchside },		/* HOME */
    { "AWAY",
		C_PR_PREGAME,
		"Switch to away team.",
		do_switchside },		/* AWAY */
    { "TNAME",
		C_PR_CAPTAIN | C_PR_PREGAME,
		"Change the name of your team.",
		do_tname },			/* TNAME */
    { "NEWGALAXY",
		C_PR_CAPTAIN | C_PR_PREGAME,
		"Reset the galaxy.",
		do_resetgalaxy },		/* RESETGALAXY */
    { "CONFINE",
		C_PR_CAPTAIN | C_PR_PREGAME,
		"Confine teams from other races cores.",
		do_confine },			/* CONFINE */
    { "START",
		C_PR_CAPTAIN | C_PR_PREGAME,
		"Start tournament.",
		do_start },			/* START */
    { "|Team selection commands:", C_DESC | C_PR_CAPTAIN | C_PR_PREGAME},
    { "FED",
		C_PR_CAPTAIN | C_PR_PREGAME,
		"Change your side to Federation.",
		do_pickside },			/* FED */
    { "ROM",
		C_PR_CAPTAIN | C_PR_PREGAME,
		"Change your side to Romulan.",
		do_pickside },			/* ROM */
    { "KLI",
		C_PR_CAPTAIN | C_PR_PREGAME,
		"Change your side to Klingon.",
		do_pickside },			/* KLI */
    { "ORI",
		C_PR_CAPTAIN | C_PR_PREGAME,
		"Change your side to Orion.",
		do_pickside },			/* ORI */
    { "PASS",
		C_PR_CAPTAIN | C_PR_PREGAME,
		"Pass the first choice of sides to the home team.",
		do_pickside },			/* PASS */
    { "|Captain commands:", C_DESC | C_PR_CAPTAIN},
    { "UNCAPTAIN",
		C_PR_CAPTAIN,
		"Release captain duties.",
		do_uncaptain },			/* UNCAPTAIN */
    { "FREE",
		C_PR_CAPTAIN | C_PLAYER,
		"Frees a slot. Ex: 'FREE 0'",
		do_free },			/* FREE */
    { "PAUSE",
		C_PR_CAPTAIN | C_PR_INGAME,
		"Requests a pause.",
		do_pause },			/* PAUSE */
    { "PAUSENOW",
		C_PR_CAPTAIN | C_PR_INGAME,
		"Imediately pauses the game.",
		do_pause },			/* PAUSENOW */
    { "TOUT",
		C_PR_CAPTAIN | C_PR_INGAME,
		"Call a timeout.",
		do_timeout },			/* TIMEOUT */
    { "CONTINUE",
		C_PR_CAPTAIN | C_PR_INGAME,
		"Requests that the game continues.",
		do_pause },			/* CONTINUE */
#if defined(AUTO_INL)
    { "The following votes can be used:  (M=Majority, T=Team vote)", C_DESC },
    { "EXIT",
	C_VC_ALL,
	"Terminate robot by majority vote.",
	cleanup,
	1, 20, 0 },
#endif /* AUTO_INL */
    { NULL }
    };

extern Inl_stats inl_stat;
extern Team	 inl_teams[];

int
check_command(mess)
     struct message *mess;
{
  int flags = 0;
  int c;
  int num = -1;

  for (c=0; c < INLTEAM; c++)
  {
    if (players[mess->m_from].p_team == inl_teams[c].side)
    {
      num = c;
      break;
    }
  }

  if (num != -1)
  {
    if (inl_teams[num].captain == mess->m_from)
      flags |= C_PR_CAPTAIN;
    if (inl_stat.flags & S_PREGAME)
      flags |= C_PR_PREGAME;
    if (inl_stat.flags & (S_TOURNEY | S_OVERTIME))
      flags |= C_PR_INGAME;
  }
  return check_2_command(mess, inl_commands, flags);
}
