#include "game.h" #include #include #include double rand2() { float u = (float) rand() / (float) RAND_MAX; return 2.0 * (u - 0.5); } 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; } void game_setup_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_reset_team(team_t *team, uint8_t id) { team->id = id; team->color[0] = 1.; team->color[1] = 1.; team->color[2] = 1.; team->color[3] = 1.f; team->x = 0.; team->y = 0.; team->z = 0.; team->points = 0; team->wins = 0; } void game_reset_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_reset_point(point_t *point) { point->status = 0; point->x = 0.0; point->y = 0.0; point->z = 0.0; } void game_setup(game_t *game, int master, void(*explosion_callback)(double x, double y, double z)) { size_t i; if (master) { for (i = 0; i < GAME_TEAM_COUNT; i++) game_setup_team(&game->team[i], 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]); } else { for (i = 0; i < GAME_TEAM_COUNT; i++) game_reset_team(&game->team[i], 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]); } game->explosion_callback = explosion_callback; game->max_player_id = 0; game->master = master; game->updateTime = 0.0; } 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->id = game->max_player_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; } 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 > 500.0) player->vx = 500.0; if (player->vy > 500.0) player->vy = 500.0; if (player->vz > 500.0) player->vz = 500.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; } } } 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 < 10000.0) { game->explosion_callback(player->x, player->y, player->z); 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->ttl < 0) { if (bomb->status == 1) { game->explosion_callback(bomb->x, bomb->y, bomb->z); bomb->status = 2; } else if (bomb->ttl < 0.8) { _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. / 60.; game->updateTime += dt; while (game->updateTime > delta * 0.8) { 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; }