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 529ba76fc59fbfcc018e8983943901d2b123b990
parent 49f41b0f46038df870a67ed9cf8cfd5fbb3f90e5
Author: bsandro <email@bsandro.tech>
Date:   Wed, 30 Sep 2026 02:17:32 +0300

logs somewhat resemble desired output

Diffstat:
Mactions/attack_melee.c | 10++++++----
Mactions/attack_ranged.c | 5+++--
Mactions/movement.c | 4++--
Mcommands/CREATE_MAP.c | 2+-
Mcommands/MARCH.c | 3++-
Mcommands/SPAWN_HUNTER.c | 7++++---
Mcommands/SPAWN_SWORDSMAN.c | 7++++---
Mcore/ecs/entity.c | 2+-
Mcore/world.c | 4+++-
Mmain.c | 4++--
Msystems/actions.c | 4++--
Msystems/death.c | 4++--
12 files changed, 32 insertions(+), 24 deletions(-)

diff --git a/actions/attack_melee.c b/actions/attack_melee.c @@ -7,16 +7,18 @@ #include "core/world.h" bool Action_Attack_Melee(Entity *e, World *w) { - printf("unit %d try melee attack\n", e->id); + //printf("unit %d try melee attack\n", e->id); int strength = Entity_Find_Component(e, COMPONENT_MELEE)->melee.strength; //@todo NULL check - int hp = Entity_Find_Component(e, COMPONENT_HEALTH)->health.hp; //@todo NULL check + //int hp = Entity_Find_Component(e, COMPONENT_HEALTH)->health.hp; //@todo NULL check Position p = Entity_Find_Component(e, COMPONENT_POSITION)->position; //@todo NULL check for (int i=0;i<arrlen(w->entities);++i) { if (w->entities[i].id==e->id) continue; Position pe = Entity_Find_Component(&w->entities[i], COMPONENT_POSITION)->position; if (abs(pe.x-p.x)<=1 && abs(pe.y-p.y)<=1) { - printf("found neighbor enemy %d\n", w->entities[i].id); - Entity_Find_Component(&w->entities[i], COMPONENT_HEALTH)->health.hp -= strength; + //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); return true; } } diff --git a/actions/attack_ranged.c b/actions/attack_ranged.c @@ -8,7 +8,7 @@ #include "core/world.h" bool Action_Attack_Ranged(Entity *e, World *w) { - printf("unit %d try ranged attack\n", e->id); + //printf("unit %d try ranged attack\n", e->id); Ranged r = Entity_Find_Component(e, COMPONENT_RANGED)->ranged; //@todo NULL check int hp = Entity_Find_Component(e, COMPONENT_HEALTH)->health.hp; //@todo NULL check Position p = Entity_Find_Component(e, COMPONENT_POSITION)->position; //@todo NULL check @@ -18,8 +18,9 @@ bool Action_Attack_Ranged(Entity *e, World *w) { // taxicab distance to keep it simple for now int dist = abs(pe.x-p.x) + abs(pe.y-p.y); if (dist>=2&&dist<=r.range) { - printf("found enemy for ranged attack %d\n", w->entities[i].id); + //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); return true; } } diff --git a/actions/movement.c b/actions/movement.c @@ -8,7 +8,7 @@ #include "core/world.h" bool Action_Movement(Entity *e, World *w) { - printf("unit %d try movement\n", e->id); + //printf("unit %d try movement\n", e->id); MovementTarget mt = Entity_Find_Component(e, COMPONENT_MOVEMENT_TARGET)->movement_target; Component *p = Entity_Find_Component(e, COMPONENT_POSITION); // melee action already should've taken care of any neighbor enemies so we don't check for that @@ -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 %d moved from %d:%d to %d:%d (target is %d:%d)\n", e->id, old.x, old.y, p->position.x, p->position.y, mt.x, mt.y); + printf("UNIT_MOVED unitId=%d x=%d y=%d\n", 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); - puts("map init ok"); + printf("CREATE_MAP width=%d height=%d\n", width, height); } bool cmd_create_map(World *w, int argc, char **argv) { diff --git a/commands/MARCH.c b/commands/MARCH.c @@ -12,7 +12,8 @@ static bool march(World *w, int id, int x, int y) { Component *actions = Entity_Find_Component(e, COMPONENT_ACTIONS); if (actions==NULL) return false; arrput(actions->actions.items, (struct Actions_Item){ .run = Action_Movement }); - puts("march ok"); + 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); return true; } diff --git a/commands/SPAWN_HUNTER.c b/commands/SPAWN_HUNTER.c @@ -6,7 +6,7 @@ #include "actions/movement.h" static void spawn_hunter(World *w, int id, int x, int y, int hp, int agi, int str, int range) { - puts("spawn hunter"); + //puts("spawn hunter"); Entity hunter = {0}; hunter.id = id; Entity_Add_Component(&hunter, (Component){ @@ -33,8 +33,9 @@ static void spawn_hunter(World *w, int id, int x, int y, int hp, int agi, int st .actions = actions }); 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 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); } diff --git a/commands/SPAWN_SWORDSMAN.c b/commands/SPAWN_SWORDSMAN.c @@ -6,7 +6,7 @@ #include "actions/movement.h" static void spawn_swordsman(World *w, int id, int x, int y, int hp, int str) { - puts("spawn swordsman"); + //puts("spawn swordsman"); Entity swordsman = {0}; swordsman.id = id; Entity_Add_Component(&swordsman, (Component){ @@ -28,8 +28,9 @@ static void spawn_swordsman(World *w, int id, int x, int y, int hp, int str) { .actions = actions }); 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 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); } bool cmd_spawn_swordsman(World *w, int argc, char **argv) { diff --git a/core/ecs/entity.c b/core/ecs/entity.c @@ -14,7 +14,7 @@ Entity * Entity_New(int id) { } bool Entity_Add_Component(Entity *e, Component c) { - printf("%d add component %d\n", e->id, c.type); + //printf("%d add component %d\n", e->id, c.type); arrput(e->components, c); return true; } diff --git a/core/world.c b/core/world.c @@ -24,7 +24,9 @@ bool World_Run_Systems(World *w) { Entity * World_Get_Entity(World *w, int id) { for (int i=0;i<arrlen(w->entities);++i) { - if (w->entities[i].id == id) return &w->entities[i]; + if (w->entities[i].id == id) { + return &w->entities[i]; + } } return NULL; } diff --git a/main.c b/main.c @@ -11,7 +11,7 @@ int main(int argc, char **argv) { int turn = 0; World_Add_System(&w, &(System){ .run = actions_run }); World_Add_System(&w, &(System){ .run = death_run }); - puts("world create ok"); + //puts("world create ok"); if (argc>1) { FILE *f = fopen(argv[1], "r"); @@ -32,7 +32,7 @@ int main(int argc, char **argv) { } while (World_Run_Systems(&w)) { - printf("[turn %3d] world systems run ok\n", turn++); + //printf("[turn %3d] world systems run ok\n", turn++); // safeguard if (turn >= 1000) { puts("turns limit reached"); diff --git a/systems/actions.c b/systems/actions.c @@ -6,14 +6,14 @@ #include "core/world.h" bool actions_run(World *w) { - printf("actions_run world %p\n", (void *)w); + //printf("actions_run world %p\n", (void *)w); bool ret = false; for (int i=0;i<arrlen(w->entities);++i) { Component *c = Entity_Find_Component(&w->entities[i], COMPONENT_ACTIONS); if (c==NULL) continue; for (int j=0;j<arrlen(c->actions.items);++j) { if (c->actions.items[j].run(&w->entities[i], w)) { - puts("action ok"); + //puts("action ok"); ret = true; break; } diff --git a/systems/death.c b/systems/death.c @@ -6,12 +6,12 @@ #include "core/world.h" bool death_run(World *w) { - printf("death_run world %p\n", (void *)w); + //printf("death_run world %p\n", (void *)w); for (int i=0;i<arrlen(w->entities);++i) { Component *c = Entity_Find_Component(&w->entities[i], COMPONENT_HEALTH); if (c==NULL) continue; if (c->health.hp<=0) { - printf("entity %d died\n", w->entities[i].id); + printf("UNIT_DIED unitId=%d\n", w->entities[i].id); arrdel(w->entities, i); i--; // wtf }