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 6b59f703287769e21f2142632da4f00f185c6a64
parent e060a033b81e499aacbbc44ab5361cbc7323f451
Author: bsandro <email@bsandro.tech>
Date:   Wed, 30 Sep 2026 09:41:56 +0300

new readme

Diffstat:
AREADME | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
RREADME.md -> TASK.md | 0
2 files changed, 61 insertions(+), 0 deletions(-)

diff --git a/README b/README @@ -0,0 +1,61 @@ +So my friend did this test task for some employer in C++, I found it interesting enough to repeat in good 'ole C (albeit it is not that old since I'm using C11+GNU). + +Since C doesn't have templating system (which would be crazy useful here) and you can't do everything with macroses and they're also pain to debug I decided to go back to roots and remembered old motto "If you program in C, you program for UNIX". So I took the whole operating system toolchain as my development toolchain and it became significantly easier and more fun. + +Prerequisites: + +- GNU make +- modern C compiler (I've tried both clang 19.1.7 and gcc 14.2.0) +- bash +- stuff like 'find', 'basename', 'dirname', 'sed' etc. Everything went out of the box on my MX linux (debian fork) + +Build process: + +So I wanted for modules to be registered dynamically when created, so C files in 'commands' and 'components' are gathered on build stage via scripts and some proxy files and structures are generated inplace. + +Code structure, briefly: + +TASK.md - test task (in russian) +commands_example.txt - file provided with the task, contains commands to set up the world +main.c - entry point +core/ - directory with basic program structures and functions +core/ecs/ - directory with basic ECS layout structures +core/ecs/component.h - core component structure that includes autogenerated files from components/ +core/parser.c - parses text file input and calls *commands* from it +commands/ - user commands with filenames repeating commands from commands_example.txt +commands/_registry.h - autogenerated registry (enum and array) of *commands* to include in code +systems/ - user *systems* that run in the world every turn +components/ - user *components* to build entities from, contain only data, no logic +components/_* - autogenerated files that are included in core *components* structure +actions/ - individual *actions* from the *action system*, that allows units to behave correspondingly their *components* + +Essential workflow and extending project: + +So new commands go into commands/ directory, only creating a file and filling it with logic is needed (+ parsing the input that is array of strings). +Commands can create entities, populate them with components - or just add new components to existing entities. +System 'actions' calls whatever actions lie inside of an array of 'action' component of every entity, in order. +Component 'action' can use functions from 'actions/' folder, they're executed in order. + +main.c adds two bare systems to the world: 'actions' and 'death'; they're executed every turn in the order they were added. + +For example the call hierarchy would be something like that: + +main.c +`-> world create etc. + +-> commands parse + | +-> spawn unit 1 + | | `-> add components and actions + | `-> spawn unit 2 + | `-> add components and actions + `-> main loop + +-> actions system + | +-> unit 1 + | | `-> attack_melee + | | `-> movement + | `-> unit 2 + | `-> attack_melee + | `-> attack_ranged + | `-> movement + `-> death system + `-> unit 1 died + diff --git a/README.md b/TASK.md