/* $Log:	defs.h,v $
 * Revision 1.4  92/04/01  15:36:00  terence
 * UDP v0.3 mods added
 * 
 * Revision 1.3  92/03/19  21:34:47  terence
 * Added UDP mods (version 0.2, 3/17/92)
 * 
 * Revision 1.2  92/03/11  15:49:41  terence
 * added UDP mods
 *  */

/* defs.h
 */

#ifndef _h_defs
#define _h_defs


#include "../ntserv/copyright.h"
#include "../config.h"

#ifndef NULL
#define NULL	((char *) 0)
#endif

#define ERROR(l,x)	{ if (errorlevel >= l ) { printf x; }}

#define FNAMESIZE	256

#define NODET (char)(-1) /* isae - chain reaction case */

#define RESERVED_SIZE	16

#ifdef RSA
#define RSA_VERSION	"RSA v2.0 SERVER"
#define KEY_SIZE	32
#endif
#define MSG_LEN		80
#define NAME_LEN	16
#define KEYMAP_LEN	96

#ifdef MATH_ERR_CHECK
#   define MATH_ERR_CHECK(v)            \
    {                                    \
	extern int errno;                 \
	if(errno == 33){ /* EDOM */       \
	    char      buf[80];             \
	    sprintf(buf, "%s:%d: %d", __FILE__, __LINE__, v);      \
	    perror(buf);                   \
	}                                 \
    }
#else
#   define MATH_ERR_CHECK(v)    /* nothing */
#endif


#ifdef VOTING
#define PV_TOTAL	30	/* 20 player slots for eject + 10 reserved
				   for further use */
#endif

#define CLUEINTERVAL	9000	/* 30 minute timer */
#define ONEMINUTE       300
#define THREEMINUTES    900
#define FIVEMINUTES     1500
#define TENMINUTES      3000
#define FIFTEENMINUTES  4500

#define DOOSHMSG	254	/* NBT for doosh messages */

#define UNDEF		-1	/* for new_warning()	*/

#define CVOID 		struct player_spacket *

#define MAXPLAYER 20
#define TESTERS 4	/* Priveledged slots for robots and game 'testers' */
#define MAXQUEUE 8	/* Number of different  waitqueues */
#define MAXWAITING 32   /* Number of total people waiting */
#define QNAMESIZE  20   /* Max length of wait queue name */
#define MAXPLANETS 40
#define MAXTORP 8
#define MAXPLASMA 1
#define PKEY 128
#define WINSIDE 500     /* Size of strategic and tactical windows */
#define BORDER 4        /* border width for option windows */
#define PSEUDOSIZE 16
#define CLOAK_PHASES 7  /* number of drawing phases in a cloak engage/disengage */
#define NUMRANKS 9

#define SURREND 4

#ifdef SURRENDER_SHORT /* Length of surrender timer in minutes */
#define SURRLENGTH 20  /* Short timer */
#else
#define SURRLENGTH 30  /* Long Timer */
#endif 

#define NUM_TYPES 8
#define SCOUT 0
#define DESTROYER 1
#define CRUISER 2
#define BATTLESHIP 3
#define ASSAULT 4
#define STARBASE 5
#define SGALAXY 6
#define ATT 7

/* These are configuration definitions */

#define GWIDTH 100000   /* galaxy is 100000 spaces on a side */
#define WARP1 20	/* warp one will move 20 spaces per update */
#define SCALE 40	/* Window will be one pixel for 20 spaces */
#define EXPDIST 350	/* At this range a torp will explode */
#define ZAPPLAYERDIST 390 /* Phaser will hit player if line is this close */
#define ZAPPLASMADIST 270 /* Phaser will hit plasma if line is this close */
#define DAMDIST 2000	/* At this range a torp does damage */
#define SHIPDAMDIST 3000 /* At this range, an exploding ship does damage */
#define PLASDAMDIST 2500 /* At this range, a plasma does damage */
#define DETDIST 1700	/* At this range a torp can be detonated */
#define PHASEDIST 6000	/* At this range a player can do damage with phasers */
#define ENTORBDIST 900	/* At this range a player can orbit a planet */
#define ORBDIST 800	/* A player will orbit at this radius */
#define ORBSPEED 2	/* This is the fastest a person can go into orbit */
#define PFIREDIST 1500	/* At this range a planet will shoot at a player */
#define UPDATE 100000	/* Update time is 100000 micro-seconds */
#define UPDATE_SKIP 2   /* Default client update skip rate (p_timerdelay) */
#define UPDATE_MAXSKIP 10 /* Maximum client update skip rate (p_timerdelay) */
#define AUTOQUIT 60	/* auto logout in 60 secs */
#define VACANT -1       /* indicates vacant port on a starbase */
#define DOCKDIST 600
#define DOCKSPEED 2     /* If base is moving, there will be some
			   finesse involved to dock */
#define NUMPORTS 4
#define SBFUELMIN 10000   /* If starbase's fuel is less than this, it will not
			   refuel docked vessels */
#define TRACTDIST   6000 /* maximum effective tractor beam range */
#define TRACTEHEAT  5    /* ammount tractor beams heat engines */
#define TRACTCOST   20	 /* fuel cost of activated tractor beam */

/* These are memory sections */
#define PLAYER 1
#define MAXMESSAGE 75
#define MAXREVIEWMESSAGE 20

#define timeprod_int()		(int)(status->timeprod/10.0)

#define rosette(x)   ((((x) + 8) / 16) & 15)
/* #define rosette(x)   ((((x) + 256/VIEWS/2) / (256/VIEWS) + VIEWS) % VIEWS) */
/*                      (((x + 8) / 16 + 16)  %  16)  */

/* These are the teams */
/* Note that I used bit types for these mostly for messages and
   war status.  This was probably a mistake.  It meant that Ed
   had to add the 'remap' area to map these (which are used throughout
   the code as the proper team variable) into a nice four team deep
   array for his color stuff.  Oh well.
*/
#define NOBODY 0x0
#define FED 0x1
#define ROM 0x2
#define KLI 0x4
#define ORI 0x8
#define ALLTEAM (FED|ROM|KLI|ORI)
#define MAXTEAM (ALLTEAM)	/* was ALLTEAM (overkill?) 6/22/92 TMC */
				/* no, not overkill. ALLTEAM used to be
				   larger than MAXTEAM which overran bounds
				   in team[], nbt */

#define NUMTEAM 4
/*
** These are random configuration variables
*/

#ifndef TWO_RACE
#define VICTORY 3       /* Number of systems needed to conquer the galaxy */
#else
#define VICTORY 2       /* 2-Race Genocide, isae */
#endif

#define WARNTIME 30	/* Number of updates to have a warning on the screen */
#define MESSTIME 30	/* Number of updates to have a message on the screen */

#define BUILD_SB_TIME   30	/* Minutes to rebuild an SB 4/15/92 TC */
#define MIN_COUP_TIME   30	/* Min/max minutes before a geno'ed race */
#define MAX_COUP_TIME   60	/* can reenter and coup (random) 4/15/92 TC */

#define TARG_PLAYER	0x1	/* Flags for gettarget */
#define TARG_PLANET	0x2
#define TARG_CLOAK	0x4	/* Include cloaked ships in search */
#define TARG_SELF	0x8

/* Environment variable name. added 11/6/92 DRG */
#define ENV_NAME        "NTSERV_PATH"

/* Data files to make the game play across daemon restarts. */

#define N_GLOBAL	".global"
#define N_SCORES	".scores"
#define N_PLFILE	".planets"
#define N_MOTD		".motd"
#define N_MOTD_CLUE	".motd_clue"
#define N_DAEMON	"daemonII"
#define N_ROBOT		"robotII"
#define N_LOGFILENAME	"logfile"
#define N_PLAYERFILE	".players"
#define N_CONQFILE	".conquer"
#define N_SYSDEF_FILE	".sysdef"
#define N_TIME_FILE	".time"
#define N_CLUE_BYPASS	".clue-bypass"
#define N_BANNED_FILE	".banned"
#define N_SCUM_FILE	"god.LOG"
#define N_ERROR_FILE	"ERRORS"
#define N_BYPASS_FILE	".bypass"
#ifdef RSA
#define N_RSA_KEY_FILE	".reserved"
#endif
#ifdef AUTOMOTD
#define N_MAKEMOTD	"/makemotd"
#endif
#ifdef CHECKMESG
#define N_MESGLOG	"mesg.LOG"
#define N_GODLOG	"god.LOG"
#endif

#ifdef FEATURES
#define N_FEATURE_FILE	".features"
#endif

#ifdef ONCHECK
#define N_ON_FILE	"on"
#endif

#define N_ACCESS_FILE	".access"
#define N_NOCOUNT_FILE	".nocount"
#define N_PROG		"ntserv"
#define N_LOGFILE	"log"

#define NO_ROBOT	0

#ifdef BASEPRACTICE
#define N_BASEP		"basep"
#define N_ROBODIR       "og"
#define BASEP_ROBOT	1
#define N_MOTD_BASEP	".motd_basep"
#endif

#ifdef DOGFIGHT
#define N_MARS		"mars"
#define MARS_ROBOT	2
#endif

#define N_PUCK		"puck"
#define PUCK_ROBOT	3

#define N_INL		"inl"
#define INL_ROBOT	4

/* Other stuff that Ed added */

#define ABS(a)			/* abs(a) */ (((a) < 0) ? -(a) : (a))
#ifndef MAX
#define MAX(a,b)		((a) > (b) ? (a) : (b))
#endif


#define planet_(i)       (planets + (i))
/* Note that plasmas and torps are in the same array, plasmas after torps. */
#define plasma_(i)       (torp_(MAXTORP*MAXPLAYER + (i)))
#define player_(i)       (players + (i))
#define torp_(i)         (torps + (i))
                       
#define firstPlayer      (player_(0))
#define lastPlayer       (player_(MAXPLAYER - 1))
                       
#define firstPlanet      (planet_(0))
#define lastPlanet       (planet_(MAXPLANETS - 1))
                       
#define firstTorp        (torp_(0))
#define lastTorp         (torp_(MAXTORP*MAXPLAYER - 1))
                       
#define firstTorpOf(p)   (torp_(MAXTORP*(p)->p_no))
#define lastTorpOf(p)    (torp_(MAXTORP*(p)->p_no + MAXTORP - 1))
                       
#define firstPlasma      (plasma_(0))
#define lastPlasma       (plasma_(MAXPLASMA*MAXPLAYER - 1))

#define firstPlasmaOf(p) (plasma_(MAXPLASMA*(p)->p_no))
#define lastPlasmaOf(p)  (plasma_(MAXPLASMA*(p)->p_no + MAXPLASMA - 1))

                                   
#define badPlayerNo(p)          ((p) < 0 || (p) > (MAXPLAYER-1))

#define enemyPlasmaTorp(t)	enemyTorp(t)
#define enemyTorp(t)		((me->p_team & (t)->t_war) || \
				 ((t)->t_team & me->p_war))
#define friendlyPlasmaTorp(t)	friendlyTorp(t)
#define friendlyTorp(t)		(! enemyTorp(t))
#define myPlasmaTorp(t)		myTorp(t)
#define myTorp(t)               (me->p_no == (t)->t_owner)

#define friendlyPhaser(p)	(me->p_team == players[(p) - phasers].p_team)
#define myPlayer(p)		(me == (p))
#define myPlanet(p)		(me->p_team == (p)->pl_owner)
#define friendlyPlayer(p)	((!(me->p_team & (p)->p_swar)) && \
				 (!((p)->p_team & me->p_war)))
#define isAlive(p)		((p)->p_status == PALIVE)
#define friendlyPlanet(p)	(((p)->pl_info & me->p_team) && \
			         (!((p)->pl_owner & me->p_war)))

#define plasmatorpColor(t)	torpColor(t)
#define torpColor(t)		\
	(myTorp(t) ? myColor : shipCol[remap[players[(t)->t_owner].p_team]])
#define phaserColor(p)		\
	(myPhaser(p) ? myColor : shipCol[remap[players[(p) - phasers].p_team]])
/* 
 * Cloaking phase (and not the cloaking flag) is the factor in determining 
 * the color of the ship.  Color 0 is white (same as 'myColor' used to be).
 */
#define playerColor(p)		\
	(myPlayer(p) ? 		\
	    (cloak_pixels[0][me->p_cloakphase])	\
	    : (cloak_pixels[remap[(p)->p_team]][(p)->p_cloakphase]))
#define planetColor(p)		\
	(((p)->pl_info & me->p_team) ? shipCol[remap[(p)->pl_owner]] : unColor)

#define planetFont(p)		\
	(myPlanet(p) ? bfont : friendlyPlanet(p) ? ifont : dfont)
#define shipFont(p)		\
	(myPlayer(p) ? bfont : friendlyPlayer(p) ? ifont : dfont)
#define bombingRating(p)	\
	((float) (p)->p_stats.st_tarmsbomb * status->timeprod / \
	 ((float) (p)->p_stats.st_tticks * (status->armsbomb?status->armsbomb:1)))
#define planetRating(p)		\
	((float) (p)->p_stats.st_tplanets * status->timeprod / \
	 ((float) (p)->p_stats.st_tticks * status->planets))
#define offenseRating(p)	\
	((float) (p)->p_stats.st_tkills * status->timeprod / \
	 ((float) (p)->p_stats.st_tticks * (status->kills?status->kills:1)))
#define defenseRating(p)	\
	((float) (p)->p_stats.st_tticks * status->losses / \
	 ((p)->p_stats.st_tlosses!=0 ? \
	  ((float) (p)->p_stats.st_tlosses * status->timeprod) : \
	  (status->timeprod)))

/* This macro allows us to time things based upon the SIGALRM signal.
 * Given a number of 1/5 seconds, it will return the number of SIGALRMs we
 *  will receive in that period.
 */
#define efticks(x) (((x)*2)/me->p_timerdelay)

/*
 * UDP control stuff
 */
#ifdef GATEWAY
# define UDP_NUMOPTS	11
# define UDP_GW		UDP_NUMOPTS-1
#else
# define UDP_NUMOPTS	10
#endif
#define UDP_CURRENT	0
#define UDP_STATUS	1
#define UDP_DROPPED	2
#define UDP_SEQUENCE	3
#define UDP_SEND	4
#define UDP_RECV	5
#define UDP_DEBUG	6
#define UDP_FORCE_RESET	7
#define UDP_UPDATE_ALL	8
#define UDP_DONE	9
#define COMM_TCP	0
#define COMM_UDP	1
#define COMM_VERIFY	2
#define COMM_UPDATE	3
#define COMM_MODE	4       /* put this one last */
#define SWITCH_TCP_OK	0
#define SWITCH_UDP_OK	1
#define SWITCH_DENIED	2
#define SWITCH_VERIFY	3
#define CONNMODE_PORT	0
#define CONNMODE_PACKET	1
#define STAT_CONNECTED	0
#define STAT_SWITCH_UDP	1
#define STAT_SWITCH_TCP	2
#define STAT_VERIFY_UDP	3
#define MODE_TCP	0
#define MODE_SIMPLE	1
#define MODE_FAT	2
#define MODE_DOUBLE	3	/* put this one last */

#define UDP_RECENT_INTR	300
#define UDP_UPDATE_WAIT 5	/* 5 second wait */

/* server version of UDPDIAG */
/* (change these to "#define UDPDIAG(x) <return>" for smaller & faster code) */
#define UDPDIAG(x)	{ if (udpAllowed > 1) { printf("UDP: "); printf x; }}
#define V_UDPDIAG(x)	{ if (udpAllowed > 2) { printf("UDP: "); printf x; }}


#ifdef SHORT_PACKETS
#if WINSIDE > 500
#undef SHORT_PACKETS
#endif
#define TEXTE 0
#define PHASER_HIT_TEXT 1
#define BOMB_INEFFECTIVE 2
#define BOMB_TEXT 3
#define BEAMUP_TEXT 4
#define BEAMUP2_TEXT 5
#define BEAMUPSTARBASE_TEXT 6
#define BEAMDOWNSTARBASE_TEXT 7
#define BEAMDOWNPLANET_TEXT 8
#define SBREPORT 9
#define ONEARG_TEXT 10
#define BEAM_D_PLANET_TEXT 11
#define ARGUMENTS 12
#define BEAM_U_TEXT 13
#define LOCKPLANET_TEXT 14
#define LOCKPLAYER_TEXT 15
#define SBRANK_TEXT 16
#define SBDOCKREFUSE_TEXT 17
#define SBDOCKDENIED_TEXT 18
#define SBDOCKREFUSE_TEXT 17
#define SBDOCKDENIED_TEXT 18
#define SBLOCKSTRANGER 19
#define SBLOCKMYTEAM 20
/*      Daemon messages */
#define DMKILL 21
#define KILLARGS 22
#define DMKILLP 23
#define DMBOMB 24
#define DMDEST 25
#define DMTAKE 26
#define DGHOSTKILL 27
/*      INL     messages                */
#define INLDMKILLP 28
#define INLDMKILL 29    /* Because of shiptypes */
#define INLDRESUME 30
#define INLDTEXTE 31
/* Variable warning stuff */
#define STEXTE 32               /* static text that the server needs to send to the client first */
#define SHORT_WARNING 33 /* like CP_S_MESSAGE */
#define STEXTE_STRING 34
#define KILLARGS2 35		/* For Why dead messages */

/* Warning flags , used in socket.c */
#define SVALID   253 /* Mark server messages as valid to send over SP_S_MESSAGE */
#define SINVALID 254    /* Mark server Message as invalid to send over SP_S_MESSAGE */
#define DINVALID 255    /* Mark daemon ( GOD) Message as invalid to send over SP_S_WARNING */

#endif

#endif /* _h_defs */
