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 1681025a03cec6232e0554cc33af3ddb7410ca59
parent 6e4566f2f0fbab792afa3c8459fc8c444c451941
Author: bsandro <email@bsandro.tech>
Date:   Wed, 16 Sep 2026 00:07:03 +0300

basic ranged attack

Diffstat:
Mactions/attack_melee.c | 2+-
Aactions/attack_ranged.c | 28++++++++++++++++++++++++++++
Aactions/attack_ranged.h | 6++++++
Mcommands/SPAWN_SWORDSMAN.c | 32+++++++++++++++++++-------------
Dcomponents/unit.h | 6------
5 files changed, 54 insertions(+), 20 deletions(-)

diff --git a/actions/attack_melee.c b/actions/attack_melee.c @@ -7,7 +7,7 @@ #include "core/world.h" bool Action_Attack_Melee(Entity *e, World *w) { - printf("unit %d try melee\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 Position p = Entity_Find_Component(e, COMPONENT_POSITION)->position; //@todo NULL check diff --git a/actions/attack_ranged.c b/actions/attack_ranged.c @@ -0,0 +1,28 @@ +#include <stdlib.h> +#include <stdbool.h> +#include <stdio.h> +#include "stb_ds.h" +#include "actions/attack_ranged.h" +#include "components/ranged.h" +#include "core/ecs/entity.h" +#include "core/world.h" + +bool Action_Attack_Ranged(Entity *e, World *w) { + 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 + 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; + // 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); + Entity_Find_Component(&w->entities[i], COMPONENT_HEALTH)->health.hp -= r.agility; + return true; + } + } + return false; +} + diff --git a/actions/attack_ranged.h b/actions/attack_ranged.h @@ -0,0 +1,6 @@ +#pragma once + +struct Entity; // fwd +struct World; // fwd + +bool Action_Attack_Ranged(struct Entity *e, struct World *w); diff --git a/commands/SPAWN_SWORDSMAN.c b/commands/SPAWN_SWORDSMAN.c @@ -2,34 +2,40 @@ #include "core/command.h" #include "core/ecs/entity.h" #include "actions/attack_melee.h" +#include "actions/attack_ranged.h" #include "actions/movement.h" -void spawn_swordsman2(World *w) { +static void spawn_ranger(World *w) { //@todo parse va_args - puts("spawn swordsman 2"); - Entity swordsman = {0}; - swordsman.id = 2; - Entity_Add_Component(&swordsman, (Component){ + puts("spawn ranger"); + Entity ranger = {0}; + ranger.id = 2; + Entity_Add_Component(&ranger, (Component){ .type = COMPONENT_HEALTH, .health = { .hp=30 } }); - Entity_Add_Component(&swordsman, (Component){ + Entity_Add_Component(&ranger, (Component){ .type = COMPONENT_MELEE, .melee = { .strength=1 } }); - Entity_Add_Component(&swordsman, (Component){ + Entity_Add_Component(&ranger, (Component){ .type = COMPONENT_POSITION, .position = { .x=1, .y=1 } }); + Entity_Add_Component(&ranger, (Component){ + .type = COMPONENT_RANGED, + .ranged = { .agility=1, .range=6 } + }); Actions actions = {0}; arrput(actions.items, (struct Actions_Item){ .run = Action_Attack_Melee }); - Entity_Add_Component(&swordsman, (Component){ + arrput(actions.items, (struct Actions_Item){ .run = Action_Attack_Ranged }); + Entity_Add_Component(&ranger, (Component){ .type = COMPONENT_ACTIONS, .actions = actions }); - World_Add_Entity(w, &swordsman); - printf("entity has %ld components:\n", arrlen(swordsman.components)); - printf("%d: %dhp\n", swordsman.components[0].type, swordsman.components[0].health.hp); + World_Add_Entity(w, &ranger); + printf("unit has %ld components:\n", arrlen(ranger.components)); + printf("%d: %dhp\n", ranger.components[0].type, ranger.components[0].health.hp); } void cmd_spawn_swordsman(World *w, ...) { @@ -61,8 +67,8 @@ void cmd_spawn_swordsman(World *w, ...) { .actions = actions }); World_Add_Entity(w, &swordsman); - printf("entity has %ld components:\n", arrlen(swordsman.components)); + printf("unit has %ld components:\n", arrlen(swordsman.components)); printf("%d: %dhp\n", swordsman.components[0].type, swordsman.components[0].health.hp); - spawn_swordsman2(w); + spawn_ranger(w); } diff --git a/components/unit.h b/components/unit.h @@ -1,6 +0,0 @@ -#pragma once - -typedef struct Unit { - int x; - int y; -} Unit;