/******************************************************************************
 Copyright (c) 2000-2001 Richard Woolcock

 Permission is hereby granted, free of charge, to any person obtaining a copy 
 of this software and associated documentation files (the "Software"), to deal 
 in the Software without restriction, including without limitation the rights 
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 copies of the Software, and to permit persons to whom the Software is 
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all 
 copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 SOFTWARE.
 ******************************************************************************/

/******************************************************************************
 File Name        : sockets.h
 ******************************************************************************
 Description      : Contains the socket handling code.
 ******************************************************************************/

#ifndef SOCKETS_HEADER
#define SOCKETS_HEADER

/******************************************************************************
 Required literals.
 ******************************************************************************/

#define MAX_BUF        4096 /* Storage size used for many char arrays */
#define MAX_TRAIN        30 /* Max number of trains allowed */
#define PORT           3001 /* The port number of the mud */
#define MAX_TECHNIQUES   10 /* The max number of techniques stored */

/******************************************************************************
 Required types.
 ******************************************************************************/

typedef enum
{
   FALSE,
   TRUE
} bool;

typedef enum
{
   MALE,
   FEMALE
} sex_t;

typedef enum
{
   TO_USER,
   TO_ROOM,
   TO_WORLD,
   TO_ALL
} out_t;

typedef enum
{
   CROWD=1,
   CITIZEN=2,
   TRAINING=4,
   GLADIATOR=8,
   CHALLENGER=16,
   FIGHTING=32,
   CORRUPTED_PFILE=64,
   ALL=127
} status_t;

typedef enum
{
   AP_LEFT_HAND,
   AP_RIGHT_HAND,
   AP_EYES,
   AP_LEGS,
   AP_MAX
} ap_t;

typedef enum
{
   TABLE_HANDS,
   TABLE_LEGS,
   TABLE_EYES,
   TABLE_MAX /* This should always be last */
} table_t;

typedef struct body body_t;
typedef struct connection conn_t;

struct body
{
   status_t eStatus;
   conn_t*  pstConn;
   body_t*  pstOpponent;
   char     a_chName[16];
   char     a_chPassword[16];
   char     a_chTechniques[AP_MAX][MAX_TECHNIQUES+1];
   char  *  szPrompt;
   int      iStats[5];
   sex_t    eSex;
   int      iRoom;
   int      iWin;
   int      iLoss;
   int      iKill;
   int      iDam;
   table_t  eTable[AP_MAX];     /* Combat table being used     */
   int      iCmbtIndex[AP_MAX]; /* Index of combat table       */
   int      iSpeed[AP_MAX];     /* Speed per location.         */
   int      iActions[AP_MAX];   /* Action points per location. */
   bool     bBusy[AP_MAX];      /* Is the hand/legs/eyes busy? */
   void (*  pfnAttack[AP_MAX])( body_t* pstBody, ap_t eLocation );
   body_t*  pstPrev;
   body_t*  pstNext;
};

struct connection
{
   body_t*  pstBody;
   char     a_chInBuf[MAX_BUF];
   char     a_chOutBuf[MAX_BUF];
   int      iSocket;
   int      iInLen;
   int      iOutLen;
   int      iIp;
   conn_t*  pstPrev;
   conn_t*  pstNext;
};

/******************************************************************************
 Required global variables.
 ******************************************************************************/

extern body_t     stBodyEmpty;   /* Empty body structure for initialisation */
extern conn_t     stConnEmpty;   /* Empty connection structure for initialisation */
extern conn_t     *pstFirstConn; /* Connection list */
extern body_t     *pstFirstBody; /* Body list */
extern fd_set      a_sFd;        /* Array of socket flags */
extern bool        bShutdown;    /* When TRUE, shut mud down */
extern FILE       *pFile;        /* File pointer */

/******************************************************************************
 Required operation prototypes.
 ******************************************************************************/

/* Socket functions */
int    InitSocket       ( void );
void   CloseSocket      ( int iSocket );

/* Input functions */
bool    GetInput        ( conn_t* pstConn );
void    ParseInput      ( conn_t* pstConn );

/* Output functions */
void    PutOutput       ( conn_t* pstConn, out_t eOut, char*szTxt, ... );
void    SendToBody      ( body_t* pstBody, out_t eOut, char*szTxt, ... );
void    FlushOutput     ( conn_t* pstConn );
void    PutPrompt       ( conn_t* pstConn );

/* Connection functions */
bool    NewConnection   ( int     iSocket );
void    CloseConnection ( conn_t* pstConn );

#endif /* SOCKETS_HEADER */