46 lines
1.5 KiB
C
46 lines
1.5 KiB
C
#include "ok_unit.h"
|
|
#include "ok_map.h"
|
|
|
|
int ok_unit_spawn(OkWorld *w, OkFaction f, OkUnitKind k, int x, int y) {
|
|
if (w->unit_count >= OK_MAX_UNITS) return -1;
|
|
if (!ok_map_walkable(w, x, y)) return -1;
|
|
int id = w->unit_count++;
|
|
OkUnit *u = &w->units[id];
|
|
u->x = x; u->y = y; u->faction = f; u->kind = k;
|
|
u->hp = (k == OK_UNIT_SCOUT) ? 40 : 30;
|
|
u->alive = true;
|
|
return id;
|
|
}
|
|
|
|
void ok_unit_step_toward(OkWorld *w, int uid, int tx, int ty) {
|
|
if (uid < 0 || uid >= w->unit_count) return;
|
|
OkUnit *u = &w->units[uid];
|
|
if (!u->alive) return;
|
|
int nx = u->x + (tx > u->x) - (tx < u->x);
|
|
int ny = u->y + (ty > u->y) - (ty < u->y);
|
|
if (ok_map_walkable(w, nx, u->y)) u->x = nx;
|
|
if (ok_map_walkable(w, u->x, ny)) u->y = ny;
|
|
if (w->tiles[u->y][u->x] == OK_TILE_RES) {
|
|
if (u->faction == OK_FACTION_A) w->resources_a += 1;
|
|
else w->resources_b += 1;
|
|
w->tiles[u->y][u->x] = OK_TILE_EMPTY;
|
|
}
|
|
}
|
|
|
|
void ok_world_tick(OkWorld *w) {
|
|
w->tick++;
|
|
for (int i = 0; i < w->unit_count; i++) {
|
|
OkUnit *u = &w->units[i];
|
|
if (!u->alive) continue;
|
|
int tx = (u->faction == OK_FACTION_A) ? (OK_WORLD_W - 3) : 2;
|
|
int ty = (u->faction == OK_FACTION_A) ? (OK_WORLD_H - 3) : 2;
|
|
ok_unit_step_toward(w, i, tx, ty);
|
|
}
|
|
}
|
|
|
|
int ok_world_alive_count(const OkWorld *w, OkFaction f) {
|
|
int n = 0;
|
|
for (int i = 0; i < w->unit_count; i++)
|
|
if (w->units[i].alive && w->units[i].faction == f) n++;
|
|
return n;
|
|
}
|