#ifndef __I_SVMAIN_H__
#define __I_SVMAIN_H__

#include "actor.h"
#include "d_player.h"
#include "i_net.h"
#include "s_sndseq.h"


#define REMOVECOPRSESTIC TICRATE*80


typedef enum
{
	cs_free,         // can be reused for a new connection
	cs_challenge,    // just got challenge
	cs_connected,    // has been assigned to a client_t, but not in game yet
	cs_spawned       // client is fully in game
} clstate_t;



typedef struct
{
	netadr_t    address;

	clstate_t   cls;
	
	buf_t       netbuf;
	buf_t       reliablebuf;

	// for reliable protocol
	buf_t       relpackets; // save reliable packets here
	int         packetbegin[256]; // the beginning of a packet
	int         packetsize[256]; // the size of a packet
	int         packetseq[256];
	int         sequence;
	int         last_sequence;
	byte        packetnum;

	int         rate;
	int         reliable_bps;   // bytes per second
    int         unreliable_bps;

    int         last_received;   // for timeouts
	int         lastcmdtic;

	int			lastsaytic;		// for flood protection
	
	bool        allow_rcon;     // allow remote admin

} client_t;

extern client_t clients[MAXPLAYERS];

extern short parse_cl;
extern int   maxclients;

void SV_InitNetwork (void);
void SV_SendReconnectSignal();
void SV_ExitLevel();
void SV_LoadLevel();
void SV_NewLevelClientUpdate();

void STACK_ARGS SV_BroadcastPrintf (int level, const char *fmt, ...);
void SV_MidPrint (const char *str, int player);
void SV_CheckTimeouts (void);
void SV_SayLocal(char *s);

void SV_ConnectClient(void);
void SV_SpawnClient(void);
void SV_GetChallenge(void);

void SV_SendDisconnectSignal();
void SV_WriteCommands(void);
void SV_ClearClientsBPS(void);
void SV_SendPacket(client_t *cl);
void SV_AcknowledgePacket(void);
void SV_RunTics (void);
void SV_ParseCommands(void);
short SV_FindClientByAddr(void);
void SV_UpdateFrags (player_t *player);
void SV_RemoveCorpses (void);
void SV_DropClient(int playernum);
void SV_Logfrag (player_t* source, player_t* target);
void SV_SendUserInfo (int player, client_t* cl);

void SV_PlayerTriedToCheat();

void SV_Sound (AActor *mo, byte channel, char *name, byte attenuation);
void SV_Sound (client_t *cl, AActor *mo, byte channel, char *name, byte attenuation);
void SV_Sound (fixed_t x, fixed_t y, byte channel, char *name, byte attenuation);
void SV_SoundAvoidCl (player_t *player, AActor *mo, byte channel, char *name, byte attenuation);
void SV_StartSequence (sector_t *sector, int sequence, seqtype_t type);
void SV_StopSequence(sector_t *sector);

// sv_master.cpp
void SV_UpdateMaster(void);
void SV_InitMaster(void);
void SV_SendServerInfo();

extern char wadname[32];
extern bool pwad;
extern bool masterupdate;

#endif