#include "game.h"

#include <stdlib.h>
#include <math.h>
#include <stdlib.h>

double rand2() {
	float u = (float) rand() / (float) RAND_MAX;
	return 2.0 * (u - 0.5);
}

void game_reset_team(team_t *team) {
	team->points = 0;
}

void game_reset_player(player_t *player) {
	if (player->team) {
		player->x = player->team->x + 100;
		player->y = player->team->y;
		player->z = player->team->z;
	} else {
		player->x = 0;
		player->y = 0;
		player->z = 0;
	}
	player->vx = 0.;
	player->vy = 0.;
	player->vz = 0.;
}

void game_reset_point(point_t *point) {
	point->status = 1;
	point->x = 1000 + 500.0 * rand2();
	point->y = 500.0 * rand2();
	point->z = 500.0 * rand2();
}

void game_setup_team(team_t *team, uint8_t id) {
	team->id = id;
	team->color[0] = .5f + .5f * (id & (1 << 0));
	team->color[1] = .5f + .5f * (id & (1 << 1));
	team->color[2] = .5f + .5f * (id & (1 << 2));
	team->color[3] = 1.0f;

	team->x = 2000.0 * (id & (1 << 0));
	team->y = 2000.0 * (id & (1 << 1));
	team->z = 2000.0 * (id & (1 << 2));

	team->points = 0;
	team->wins = 0;
}

void game_setup_player(player_t *player) {
	player->id = 0;
	player->status = 0;
	player->team = NULL;
	player->x = 0.0;
	player->y = 0.0;
	player->z = 0.0;
	player->vx = 0.0;
	player->vy = 0.0;
	player->vz = 0.0;
}

void game_setup_point(point_t *point) {
	point->status = 0;
	point->x = 0.0;
	point->y = 0.0;
	point->z = 0.0;
}

void game_setup_bomb(bomb_t *bomb) {
	bomb->status = 0;
	bomb->x = 0.0;
	bomb->y = 0.0;
	bomb->z = 0.0;
}

void game_setup(game_t *game) {
	size_t i;

	for (i = 0; i < GAME_TEAM_COUNT; i++)
		game_setup_team(&game->team[i], i);

	for (i = 0; i < GAME_BOMB_COUNT; i++)
		game_setup_bomb(&game->bomb[i]);

	for (i = 0; i < GAME_PLAYER_COUNT; i++)
		game_setup_player(&game->player[i]);

	for (i = 0; i < GAME_POINT_COUNT; i++)
		game_setup_point(&game->point[i]);

	game->max_player_id = 0;
	game->master = 0;
	game->updateTime = 0.0;
	game->explosion_callback = 0;
	game->local_player = 0;
}

void game_reset(game_t *game) {
	size_t i;

	for (i = 0; i < GAME_TEAM_COUNT; i++)
		game_reset_team(&game->team[i]);

	for (i = 0; i < GAME_BOMB_COUNT; i++)
		game_setup_bomb(&game->bomb[i]);

	for (i = 0; i < GAME_PLAYER_COUNT; i++)
		game_reset_player(&game->player[i]);

	for (i = 0; i < GAME_POINT_COUNT; i++)
		game_reset_point(&game->point[i]);
}

player_t *_game_free_player(game_t *game) {
	size_t i;
	for (i = 0; i < GAME_PLAYER_COUNT; i++) {
		if (game->player[i].status == 0)
			return &game->player[i];
	}
	return NULL;
}

player_t *game_spawn_player(game_t *game, team_t *team) {
	player_t *player = _game_free_player(game);
	player->team = team;
	player->status = 1;
	player->id = game->max_player_id++;
	game_reset_player(player);
	return player;
}

player_t *game_spawn_player_id(game_t *game, team_t *team, uint16_t id) {
	player_t *player = _game_free_player(game);
	player->team = team;
	player->id = id;
	if (game->max_player_id < id)
		game->max_player_id = id;
	player->status = 1;
	player->x = team->x + 100;
	player->y = team->y;
	player->z = team->z;
	player->vx = 0.;
	player->vy = 0.;
	player->vz = 0.;
	return player;
}

void game_update_players(game_t *game, double dt) {
	size_t i;
	for (i = 0; i < GAME_PLAYER_COUNT; i++) {
		player_t *player = &game->player[i];
		if (player->status == 0)
			return;
		if (player->vx > 1000.0)
			player->vx = 1000.0;
		if (player->vy > 1000.0)
			player->vy = 1000.0;
		if (player->vz > 1000.0)
			player->vz = 1000.0;
		player->x += player->vx * dt;
		player->y += player->vy * dt;
		player->z += player->vz * dt;

		double distance2 = pow(player->x - player->team->x, 2) + pow(player->y
				- player->team->y, 2) + pow(player->z - player->team->z, 2);
		if (distance2 < 10000) {
			player->team->points += player->points;
			player->points = 0;
		}

		if (player->x < (-1000 + 10)) {
			player->x = (-1000 + 10);
			player->vx *= -1;
		} else if (player->x > (5000 - 10)) {
			player->x = (5000 - 10);
			player->vx *= -1;
		}

		if (player->y < (-3000 + 10)) {
			player->y = (-3000 + 10);
			player->vy *= -1;
		} else if (player->y > (3000 - 10)) {
			player->y = (3000 - 10);
			player->vy *= -1;
		}

		if (player->z < (-3000 + 10)) {
			player->z = (-3000 + 10);
			player->vz *= -1;
		} else if (player->z > (3000 - 10)) {
			player->z = (3000 - 10);
			player->vz *= -1;
		}
	}
}

void _explode_bomb(game_t *game, bomb_t *bomb) {
	size_t i, j;
	for (i = 0; i < GAME_PLAYER_COUNT; i++) {
		player_t *player = &game->player[i];
		if (player->status == 0)
			continue;
		double distance2 = pow(player->x - bomb->x, 2) + pow(player->y
				- bomb->y, 2) + pow(player->z - bomb->z, 2);
		if (distance2 < pow(150., 2.)) {
			if (game->explosion_callback)
				game->explosion_callback(player->x, player->y, player->z,
						game->explosion_callback_data);
			player->x = player->team->x + 100;
			player->y = player->team->y;
			player->z = player->team->z;
			player->vx = 0.;
			player->vy = 0.;
			player->vz = 0.;
			for (j = 0; j < player->points; j++) {
				point_t *point = game_spawn_point(game);
				point->x = player->x + 20.0 * rand2();
				point->y = player->x + 20.0 * rand2();
				point->z = player->x + 20.0 * rand2();
			}
			player->points = 0;
		}
	}
}

void game_update_bombs(game_t *game, double dt) {
	size_t i;
	for (i = 0; i < GAME_BOMB_COUNT; i++) {
		bomb_t *bomb = &game->bomb[i];
		if (bomb->status == 0)
			continue;
		bomb->x += bomb->vx * dt;
		bomb->y += bomb->vy * dt;
		bomb->z += bomb->vz * dt;
		bomb->ttl -= dt;

		if (bomb->x < (-1000 + 10)) {
			bomb->x = (-1000 + 10);
			bomb->ttl = -0.1;
		} else if (bomb->x > (5000 - 10)) {
			bomb->x = (5000 - 10);
			bomb->ttl = -0.1;
		} else if (bomb->y < (-3000 + 10)) {
			bomb->y = (-3000 + 10);
			bomb->ttl = -0.1;
		} else if (bomb->y > (3000 - 10)) {
			bomb->y = (3000 - 10);
			bomb->ttl = -0.1;
		} else if (bomb->z < (-3000 + 10)) {
			bomb->z = (-3000 + 10);
			bomb->ttl = -0.1;
		} else if (bomb->z > (3000 - 10)) {
			bomb->z = (3000 - 10);
			bomb->ttl = -0.1;
		}

		if (bomb->ttl < 0) {
			if (bomb->status == 1) {
				if (game->explosion_callback)
					game->explosion_callback(bomb->x, bomb->y, bomb->z,
							game->explosion_callback_data);
				bomb->status = 2;
			} else if (bomb->ttl < -0.2) {
				if (game->master) {
					_explode_bomb(game, bomb);
				}
				bomb->status = 0;
			}
		}
	}
}

void _update_point(game_t *game, point_t *point) {
	size_t i;
	for (i = 0; i < GAME_PLAYER_COUNT; i++) {
		player_t *player = &game->player[i];
		if (player->status == 0)
			continue;
		double distance2 = pow(player->x - point->x, 2) + pow(player->y
				- point->y, 2) + pow(player->z - point->z, 2);
		if (distance2 > 1000.0)
			continue;
		point->status = 0;
		player->points += 1;
	}

}

void game_update_points(game_t *game, double dt) {
	size_t i;
	for (i = 0; i < GAME_POINT_COUNT; i++) {
		point_t *point = &game->point[i];
		if (point->status == 0)
			continue;
		_update_point(game, point);
	}
}

void game_update(game_t *game, double dt) {
	const double delta = 1. / 120.;
	game->updateTime += dt;
	while (game->updateTime > delta) {
		game_update_players(game, delta);
		game_update_bombs(game, delta);
		game_update_points(game, delta);
		game->updateTime -= delta;
	}
}

size_t game_active_team_players(game_t *game, team_t *team) {
	size_t i, count = 0;
	for (i = 0; i < GAME_PLAYER_COUNT; i++) {
		if (game->player[i].status == 0)
			continue;
		if (game->player[i].team == team)
			count++;
	}
	return count;
}

team_t *game_team_with_least_players(game_t *game) {
	size_t i, count = -1;
	team_t *team = NULL;
	for (i = 0; i < GAME_TEAM_COUNT; i++) {
		size_t players = game_active_team_players(game, &game->team[i]);
		if (players < count) {
			count = players;
			team = &game->team[i];
		}
	}
	return team;
}

team_t *game_team(game_t *game, uint16_t id) {
	size_t i;
	for (i = 0; i < GAME_TEAM_COUNT; i++) {
		if (game->team[i].id == id) {
			return &game->team[i];
		}
	}
	return NULL;
}

player_t *game_player(game_t *game, uint16_t id) {
	size_t i;
	for (i = 0; i < GAME_PLAYER_COUNT; i++) {
		if (game->player[i].id == id) {
			return &game->player[i];
		}
	}
	return NULL;
}

bomb_t *game_spawn_bomb(game_t *game) {
	size_t i;
	for (i = 0; i < GAME_BOMB_COUNT; i++) {
		if (game->bomb[i].status == 0) {
			game->bomb[i].status = 1;
			return &game->bomb[i];
		}
	}
	return NULL;
}

point_t *game_spawn_point(game_t *game) {
	size_t i;
	for (i = 0; i < GAME_POINT_COUNT; i++) {
		if (game->point[i].status == 0) {
			game->point[i].status = 1;
			return &game->point[i];
		}
	}
	return NULL;
}

void game_setup_explosion_callback(game_t *game, void(*explosion_callback)(
		double x, double y, double z, void *data), void *data) {
	game->explosion_callback = explosion_callback;
	game->explosion_callback_data = data;
}

void game_set_master(game_t *game, int master) {
	game->master = master;
}
