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

README (3455B)


      1 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).
      2 
      3 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.
      4 
      5 Prerequisites:
      6 
      7 - GNU make
      8 - modern C compiler (I've tried both clang 19.1.7 and gcc 14.2.0)
      9 - bash
     10 - stuff like 'find', 'basename', 'dirname', 'sed' etc. Everything went out of the box on my MX linux (debian fork)
     11 
     12 Build process:
     13 
     14 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.
     15 
     16 Code structure, briefly:
     17 
     18 TASK.md                 - test task (in russian)
     19 commands_example.txt    - file provided with the task, contains commands to set up the world
     20 main.c                  - entry point
     21 core/                   - directory with basic program structures and functions
     22 core/ecs/               - directory with basic ECS layout structures
     23 core/ecs/component.h    - core component structure that includes autogenerated files from components/
     24 core/parser.c           - parses text file input and calls *commands* from it
     25 commands/               - user commands with filenames repeating commands from commands_example.txt
     26 commands/_registry.h    - autogenerated registry (enum and array) of *commands* to include in code
     27 systems/                - user *systems* that run in the world every turn
     28 components/             - user *components* to build entities from, contain only data, no logic
     29 components/_*           - autogenerated files that are included in core *components* structure
     30 actions/                - individual *actions* from the *action system*, that allows units to behave correspondingly their *components*
     31 
     32 Essential workflow and extending project:
     33 
     34 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).
     35 Commands can create entities, populate them with components - or just add new components to existing entities.
     36 System 'actions' calls whatever actions lie inside of an array of 'action' component of every entity, in order.
     37 Component 'action' can use functions from 'actions/' folder, they're executed in order.
     38 
     39 main.c adds two bare systems to the world: 'actions' and 'death'; they're executed every turn in the order they were added.
     40 
     41 For example the call hierarchy would be something like that:
     42 
     43 main.c
     44 `-> world create etc.
     45     +-> commands parse
     46     |   +-> spawn unit 1
     47     |   |   `-> add components and actions
     48     |   `-> spawn unit 2
     49     |       `-> add components and actions    
     50     `-> main loop
     51         +-> actions system
     52         |   +-> unit 1
     53         |   |   `-> attack_melee
     54         |   |   `-> movement
     55         |   `-> unit 2
     56         |       `-> attack_melee
     57         |       `-> attack_ranged
     58         |       `-> movement
     59         `-> death system
     60             `-> unit 1 died
     61 
     62 
     63 Tools used:
     64 Editor - nano
     65 Environment -  alacritty terminal
     66 OS - MX linux
     67 
     68 **No LLM involved in any capacity at any stages. All mistakes are my own.**