87 lines
1.9 KiB
C
87 lines
1.9 KiB
C
#include <player.h>
|
|
#include <building.h>
|
|
#include <unit.h>
|
|
#include <terrain.h>
|
|
#include <stddef.h>
|
|
#include <stdlib.h>
|
|
|
|
void player_update(struct player *const p)
|
|
{
|
|
for (size_t i = 0; i < sizeof p->units / sizeof *p->units; i++)
|
|
{
|
|
struct unit *const u = &p->units[i];
|
|
|
|
unit_update(u);
|
|
}
|
|
}
|
|
|
|
int player_create_unit(const struct unit_cfg *const cfg, struct player *const pl)
|
|
{
|
|
enum {N = sizeof pl->units / sizeof *pl->units};
|
|
|
|
if (pl->pop < N)
|
|
{
|
|
for (size_t i = 0; i < N; i++)
|
|
{
|
|
struct unit *const u = &pl->units[i];
|
|
struct instance *const i = &u->instance;
|
|
|
|
if (!i->alive)
|
|
{
|
|
unit_create(cfg, u);
|
|
pl->pop++;
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
|
|
int player_create_building(const struct building_cfg *const cfg, struct player *const pl)
|
|
{
|
|
enum {N = sizeof pl-> buildings / sizeof *pl->buildings};
|
|
|
|
if (pl->bpop < N)
|
|
{
|
|
for (size_t i = 0; i < N; i++)
|
|
{
|
|
struct building *const b = &pl->buildings[i];
|
|
struct instance *const i = &b->instance;
|
|
|
|
if (!i->alive)
|
|
{
|
|
building_create(cfg, b);
|
|
pl->bpop++;
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
|
|
int player_init(const struct player_cfg *const cfg, struct player *const pl)
|
|
{
|
|
for (size_t i = 0; i < sizeof pl->units / sizeof *pl->units; i++)
|
|
{
|
|
const struct unit_cfg ucfg =
|
|
{
|
|
.type = rand() & 1 ? UNIT_CFG_TYPE_WALKER : UNIT_CFG_TYPE_CAR,
|
|
.x = rand() % (MAP_W - 32),
|
|
.y = rand() % (MAP_H - 32),
|
|
};
|
|
|
|
if (player_create_unit(&ucfg, pl))
|
|
{
|
|
fprintf(stderr, "%s: player_create_unit failed\n", __func__);
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
pl->alive = true;
|
|
pl->color = cfg->color;
|
|
pl->team = cfg->team;
|
|
return 0;
|
|
}
|