#ifndef __I_NET_H__
#define __I_NET_H__

#include "doomtype.h"

#define	MAX_UDP_PACKET	8192

#define SERVERPORT  10666
#define CLIENTPORT  10667

#define PLAYER_FULLBRIGHTFRAME 70

#define CHALLENGE 5560020  // challenge
#define LAUNCHER_CHALLENGE 777123  // csdl challenge
#define VERSION 70

#define CM_X      1
#define CM_Y      2
#define CM_Z      4
#define CM_ANGLE  8
#define CM_MOMX   16
#define CM_MOMY   32
#define CM_MOMZ   64
#define CM_FRAME  128

extern int   localport;
extern byte	 net_message_buffer[MAX_UDP_PACKET];
extern int   msg_badread;

// network messages
#define  svc_disconnect          2
#define  svc_dmspawnplayer       4  // [byte]
#define  svc_moveplayer          5  // [byte] [int] [int] [int] [int] [byte]
#define  svc_updatelocalplayer   6  // [int] [int] [int] [int] [int]
#define  svc_svgametic           8  // [int]
#define  svc_updateping          9  // [byte] [byte]
#define  svc_spawnmobj           10 // 
#define  svc_disconnectclient    11
#define  svc_loadmap             12
#define  svc_consoleplayer       13
#define  svc_missilespeedangle   14
#define  svc_explodemissile      15 // [short] - netid
#define  svc_removemobj          16
#define  svc_userinfo            17
#define  svc_movemobj            18 // [short] [int] [int] [int]
#define  svc_spawnplayer         19
#define  svc_damageplayer        20
#define  svc_killmobj            21
#define  svc_firepistol          22 // [byte] - playernum
#define  svc_fireshotgun         23 // [byte] - playernum
#define  svc_firessg             24 // [byte] - playernum
#define  svc_firechaingun        25 // [byte] - playernum
#define  svc_fireweapon          29
#define  svc_sector              30
#define  svc_print               31
#define  svc_gameskill           32
#define  svc_mobjframe           33
#define  svc_updatefrags         34 // [byte] [short]
#define  svc_floorheight         35 // [short] [short]
#define  svc_ceilingheight       36 // [short] [short]
#define  svc_startsound          37
#define  svc_reconnect           38
#define  svc_exitlevel           39
#define  svc_touchspecial        40
#define  svc_changeweapon        41 // [byte]
#define  svc_dmflags             42
#define  svc_corpse              43
#define  svc_missedpacket        44
#define  svc_soundorigin         45
#define  svc_startsectorsequence 46
#define  svc_stopsectorsequence  47
#define  svc_specialsettings	 48	// [NightFang] - Like teamplay! :)
#define  svc_changeswitchtexture 49
#define  svc_mobjangle           50
#define  svc_giveammo            51
// for co-op
#define  svc_mobjstate           70
#define  svc_actor_movedir       71
#define  svc_actor_target        72
#define  svc_movemobj_xy         73


#define  clc_disconnect          3
#define  clc_say                 4
#define  clc_move                5 // send cmds
#define  clc_userinfo            6 // send userinfo
#define  clc_svgametic           7
#define  clc_rate                8
#define  clc_ack                 9
#define  clc_rcon                10
#define  clc_rcon_password       11
#define	 clc_changeteam			 12 // [NightFang] - Change your team


#define  S2C_CHALLENGE           10
#define  S2C_CONNECTION          11
#define  S2C_FULL                12


typedef struct
{
   byte    ip[4];
   unsigned short  port;
   unsigned short  pad;
} netadr_t;

extern  netadr_t  net_from;  // address of who sent the packet


typedef struct buf_s
{
   byte    *data;
   int      maxsize;
   int      cursize;
   bool     overflowed;  // set to true if the buffer size failed
} buf_t;

extern buf_t net_message;

void CloseNetwork (void);
void InitNetCommon(void);
void I_SetPort(netadr_t &addr, int port);
void I_DoSelect (void);

char *NET_AdrToString (netadr_t a);
bool NET_StringToAdr (char *s, netadr_t *a);
bool NET_CompareAdr (netadr_t a, netadr_t b);
int  NET_GetPacket (void);
void NET_SendPacket (int length, byte *data, netadr_t to);
void NET_GetLocalAddress (void);

void SZ_Clear (buf_t *buf);
void SZ_Write (buf_t *b, void *data, int length);
void SZ_Write (buf_t *b, byte *data, int startpos, int length);

void MSG_WriteByte (buf_t *b, int c);
void MSG_WriteShort (buf_t *b, int c);
void MSG_WriteLong (buf_t *b, int c);
void MSG_WriteString (buf_t *b, char *s);

int MSG_ReadByte (void);
int MSG_ReadShort (void);
int MSG_ReadLong (void);
char *MSG_ReadString (void);



#endif
