/*
 * network.h
 *
 *  Created on: 12.01.2011
 *      Author: gmueller
 */

#ifndef NETWORK_H_
#define NETWORK_H_

#include "game.h"
#include "common.h"

#include <enet/enet.h>

#include <string>

#define MESSAGE_PLAYER_SPAWN 0
#define MESSAGE_PLAYER_KILL 1
#define MESSAGE_ACCEPT 2
#define MESSAGE_PLAYER_UPDATE 3
#define MESSAGE_PLAYER_ACCELERATE 4
#define MESSAGE_BOMB_DROP 5
#define MESSAGE_BOMB_UPDATE 6
#define MESSAGE_POINT_UPDATE 7
#define MESSAGE_TEAM_UPDATE 8

typedef struct message_t {
	uint16_t msg_id;
} message_t;

typedef struct player_spawn_message_t {
	uint16_t msg_id;
	uint8_t team_id;
	uint16_t player_id;
} player_spawn_message_t;

typedef struct player_kill_message_t {
	uint16_t msg_id;
	uint16_t player_id;
} player_kill_message_t;

typedef struct accept_message_t {
	uint16_t msg_id;
	uint16_t player_id;
} accept_message_t;

typedef struct player_update_message_t {
	uint16_t msg_id;
	uint16_t player_id;
	unsigned int session;
	double x, y, z;
	double vx, vy, vz;
	uint16_t points;
} player_update_message_t;

typedef struct player_accelerate_message_t {
	uint16_t msg_id;
	uint16_t player_id;
	double x, y, z;
} player_accelerate_message_t;

typedef struct bomb_drop_meesage_t {
	uint16_t msg_id;
	double x, y, z;
	double vx, vy, vz;
	double ttl;
} bomb_drop_meesage_t;

typedef struct bomb_update_meesage_t {
	uint16_t msg_id;
	uint16_t bomb_index;
	double x, y, z;
	double vx, vy, vz;
	double ttl;
} bomb_update_meesage_t;

typedef struct point_update_mesage_t {
	uint16_t msg_id;
	uint16_t point_index;
	uint8_t status;
	double x, y, z;
} point_update_meesage_t;

struct team_update_message_t {
	uint16_t msg_id;
	uint16_t team_id;
	uint16_t points;
	uint16_t wins;
};

class Network {
public:
	Network(game_t *game);
	void initialize(const std::string &host);
	void shutdown();
	void dispatch(uint8_t *data, size_t length);
	void service(uint32_t timeout);
	void sendGameUpdates();
	void sendMessage(uint8_t *data, size_t length);
protected:
	game_t *game;
	ENetHost *host;
	ENetPeer *client_peer;
};

#endif /* NETWORK_H_ */
