sw-battle-test-c

Interesting test task in C instead of C++
git clone git://bsandro.tech/sw-battle-test-c
Log | Files | Refs | README

commit e060a033b81e499aacbbc44ab5361cbc7323f451
parent 9b9106ccb3187a43976359681388f348f98b058c
Author: bsandro <email@bsandro.tech>
Date:   Wed, 30 Sep 2026 02:36:28 +0300

logging out current turn

Diffstat:
Mactions/attack_melee.c | 2+-
Mactions/attack_ranged.c | 2+-
Mactions/movement.c | 2+-
Mcommands/CREATE_MAP.c | 2+-
Mcommands/MARCH.c | 2+-
Mcommands/SPAWN_HUNTER.c | 2+-
Mcommands/SPAWN_SWORDSMAN.c | 2+-
Mcore/world.c | 1+
Mcore/world.h | 1+
Mmain.c | 8++++----
Msystems/death.c | 2+-
11 files changed, 14 insertions(+), 12 deletions(-)

diff --git a/actions/attack_melee.c b/actions/attack_melee.c @@ -18,7 +18,7 @@ bool Action_Attack_Melee(Entity *e, World *w) { //printf("found neighbor enemy %d\n", w->entities[i].id); Component *target = Entity_Find_Component(&w->entities[i], COMPONENT_HEALTH); target->health.hp -= strength; - printf("UNIT_ATTACKED attackerUnitId=%d targetUnitId=%d damage=%d targetHp=%d\n", e->id, w->entities[i].id, strength, target->health.hp); + printf("[%d] UNIT_ATTACKED attackerUnitId=%d targetUnitId=%d damage=%d targetHp=%d\n", w->turn, e->id, w->entities[i].id, strength, target->health.hp); return true; } } diff --git a/actions/attack_ranged.c b/actions/attack_ranged.c @@ -21,7 +21,7 @@ bool Action_Attack_Ranged(Entity *e, World *w) { if (dist>=2&&dist<=r.range) { //printf("found enemy for ranged attack %d\n", w->entities[i].id); Entity_Find_Component(&w->entities[i], COMPONENT_HEALTH)->health.hp -= r.agility; - printf("UNIT_ATTACKED attackerUnitId=%d targetUnitId=%d damage=%d targetHp=%d\n", e->id, w->entities[i].id, r.agility, Entity_Find_Component(&w->entities[i], COMPONENT_HEALTH)->health.hp); + printf("[%d] UNIT_ATTACKED attackerUnitId=%d targetUnitId=%d damage=%d targetHp=%d\n", w->turn, e->id, w->entities[i].id, r.agility, Entity_Find_Component(&w->entities[i], COMPONENT_HEALTH)->health.hp); return true; } } diff --git a/actions/movement.c b/actions/movement.c @@ -19,7 +19,7 @@ bool Action_Movement(Entity *e, World *w) { else if (mt.x<p->position.x) p->position.x--; if (mt.y>p->position.y) p->position.y++; else if (mt.y<p->position.y) p->position.y--; - printf("UNIT_MOVED unitId=%d x=%d y=%d\n", e->id, p->position.x, p->position.y); + printf("[%d] UNIT_MOVED unitId=%d x=%d y=%d\n", w->turn, e->id, p->position.x, p->position.y); return true; } return false; diff --git a/commands/CREATE_MAP.c b/commands/CREATE_MAP.c @@ -3,7 +3,7 @@ static void create_map(World *w, int width, int height) { //printf("create_map(%p,%d,%d)\n", (void *)w, width, height); Map_Init(&w->map, width, height); - printf("MAP_CREATED width=%d height=%d\n", width, height); + printf("[%d] MAP_CREATED width=%d height=%d\n", w->turn, width, height); } bool cmd_create_map(World *w, int argc, char **argv) { diff --git a/commands/MARCH.c b/commands/MARCH.c @@ -13,7 +13,7 @@ static bool march(World *w, int id, int x, int y) { if (actions==NULL) return false; arrput(actions->actions.items, (struct Actions_Item){ .run = Action_Movement }); Component *pos = Entity_Find_Component(e, COMPONENT_POSITION); - printf("MARCH_STARTED unitId=%d x=%d y=%d targetX=%d targetY=%d\n", id, pos->position.x, pos->position.y, x, y); + printf("[%d] MARCH_STARTED unitId=%d x=%d y=%d targetX=%d targetY=%d\n", w->turn, id, pos->position.x, pos->position.y, x, y); return true; } diff --git a/commands/SPAWN_HUNTER.c b/commands/SPAWN_HUNTER.c @@ -35,7 +35,7 @@ static void spawn_hunter(World *w, int id, int x, int y, int hp, int agi, int st World_Add_Entity(w, &hunter); //printf("unit has %ld components:\n", arrlen(hunter.components)); //printf("%d: %dhp\n", hunter.components[0].type, hunter.components[0].health.hp); - printf("UNIT_SPAWNED unitId=%d unitType=Hunter x=%d y=%d\n", id, x, y); + printf("[%d] UNIT_SPAWNED unitId=%d unitType=Hunter x=%d y=%d\n", w->turn, id, x, y); } diff --git a/commands/SPAWN_SWORDSMAN.c b/commands/SPAWN_SWORDSMAN.c @@ -30,7 +30,7 @@ static void spawn_swordsman(World *w, int id, int x, int y, int hp, int str) { World_Add_Entity(w, &swordsman); //printf("unit has %ld components:\n", arrlen(swordsman.components)); //printf("%d: %dhp\n", swordsman.components[0].type, swordsman.components[0].health.hp); - printf("UNIT_SPAWNED unitId=%d unitType=Swordsman x=%d y=%d\n", id, x, y); + printf("[%d] UNIT_SPAWNED unitId=%d unitType=Swordsman x=%d y=%d\n", w->turn, id, x, y); } bool cmd_spawn_swordsman(World *w, int argc, char **argv) { diff --git a/core/world.c b/core/world.c @@ -15,6 +15,7 @@ void World_Add_System(World *w, System *s) { } bool World_Run_Systems(World *w) { + w->turn++; bool ret = false; for (int i=0;i<arrlen(w->systems);++i) { ret |= w->systems[i].run(w); diff --git a/core/world.h b/core/world.h @@ -9,6 +9,7 @@ typedef struct World { Map map; Entity *entities; //@todo hashmap instead of array System *systems; + int turn; } World; World * World_Init(); diff --git a/main.c b/main.c @@ -8,7 +8,7 @@ int main(int argc, char **argv) { World w; - int turn = 0; + w.turn = 1; World_Add_System(&w, &(System){ .run = actions_run }); World_Add_System(&w, &(System){ .run = death_run }); //puts("world create ok"); @@ -34,12 +34,12 @@ int main(int argc, char **argv) { while (World_Run_Systems(&w)) { //printf("[turn %3d] world systems run ok\n", turn++); // safeguard - if (turn >= 1000) { - puts("turns limit reached"); + if (w.turn >= 1000) { + puts("Safeguard: turns limit has been reached."); break; } } - puts("simulation ok"); + //puts("simulation ok"); return 0; } diff --git a/systems/death.c b/systems/death.c @@ -11,7 +11,7 @@ bool death_run(World *w) { Component *c = Entity_Find_Component(&w->entities[i], COMPONENT_HEALTH); if (c==NULL) continue; if (c->health.hp<=0) { - printf("UNIT_DIED unitId=%d\n", w->entities[i].id); + printf("[%d] UNIT_DIED unitId=%d\n", w->turn, w->entities[i].id); arrdel(w->entities, i); i--; // wtf }