2011-01-05 14:00:09 +00:00
|
|
|
#include "game.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2011-01-06 12:51:52 +00:00
|
|
|
#include <math.h>
|
2011-01-05 14:00:09 +00:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
void game_setup_player(player_t *player) {
|
|
|
|
player->id = 0;
|
|
|
|
player->status = 0;
|
|
|
|
player->team = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
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_PLAYER_COUNT; i++)
|
|
|
|
game_setup_player(&game->player[i]);
|
|
|
|
|
|
|
|
game->max_player_id = 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;
|
|
|
|
}
|
|
|
|
|
2011-01-06 12:51:52 +00:00
|
|
|
player_t *game_spawn_player_id(game_t *game, team_t *team, uint16_t id) {
|
2011-01-05 14:00:09 +00:00
|
|
|
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];
|
2011-01-06 12:51:52 +00:00
|
|
|
if (player->status == 0)
|
2011-01-05 14:00:09 +00:00
|
|
|
return;
|
|
|
|
player->x += player->vx * dt;
|
|
|
|
player->y += player->vy * dt;
|
|
|
|
player->z += player->vz * dt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-06 12:51:52 +00:00
|
|
|
void _explode_bomb(game_t *game, bomb_t *bomb, void(*explosion_callback)(
|
|
|
|
double x, double y, double z)) {
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < GAME_PLAYER_COUNT; i++) {
|
|
|
|
player_t *player = &game->player[i];
|
|
|
|
if (player->status == 0)
|
|
|
|
return;
|
|
|
|
double distance2 = pow(player->x - bomb->x, 2) + pow(player->y
|
|
|
|
- bomb->y, 2) + pow(player->z - bomb->z, 2);
|
|
|
|
if (distance2 < 10000.0) {
|
|
|
|
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.;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void game_update_bombs(game_t *game, double dt, void(*explosion_callback)(
|
|
|
|
double x, double y, double z)) {
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < GAME_BOMB_COUNT; i++) {
|
|
|
|
bomb_t *bomb = &game->bomb[i];
|
|
|
|
if (bomb->status == 0)
|
|
|
|
return;
|
|
|
|
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) {
|
|
|
|
explosion_callback(bomb->x, bomb->y, bomb->z);
|
|
|
|
bomb->status = 2;
|
|
|
|
} else if (bomb->ttl < 0.8) {
|
|
|
|
_explode_bomb(game, bomb, explosion_callback);
|
|
|
|
bomb->status = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-01-05 14:00:09 +00:00
|
|
|
|
|
|
|
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++) {
|
2011-01-06 12:51:52 +00:00
|
|
|
if (game->player[i].status == 0)
|
2011-01-05 14:00:09 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-01-06 12:51:52 +00:00
|
|
|
team_t *game_team(game_t *game, uint16_t id) {
|
2011-01-05 14:00:09 +00:00
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < GAME_TEAM_COUNT; i++) {
|
|
|
|
if (game->team[i].id == id) {
|
|
|
|
return &game->team[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-01-06 12:51:52 +00:00
|
|
|
player_t *game_player(game_t *game, uint16_t id) {
|
2011-01-05 14:00:09 +00:00
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < GAME_PLAYER_COUNT; i++) {
|
|
|
|
if (game->player[i].id == id) {
|
|
|
|
return &game->player[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-01-06 12:51:52 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|