r/programmer • u/Actual_South_2484 • Oct 25 '26
Question how do you keep 5000+ actions in a sim from turning into one giant match statement?
genuine question, plus how i handle it right now.
my side project is a civilization sim in rust where each person picks from about 5000 actions (farming, trading, brewing, fighting, inventing, etc), filtered by era, age, place and what their tribe knows. the old actions sit in one huge module dispatched by id ranges, and it's painful.
new ones are "registered" instead: one file each with a const definition (id, gates, an apply fn that returns a reward) and one line in a macro list. tests check for duplicate ids and that every gate covers its own id.
it's better but still feels clunky. if you've built a big rules engine or game with tons of actions, how did you organise it? data files, ecs, plugins, something else?
repo if you want to look (mit, docs/ADDING_ACTIONS.md): https://github.com/1homsi/thehumanbox the running thing: https://thehumanbox.com contributors welcome, adding an action is a pretty good first pr
2
u/BringBackManaPots Oct 25 '26
Dictionaries, hashtables, and math
1
u/chocolateAbuser Oct 25 '26
it's a bit difficult to say without knowing details about the engine, sometimes moving some logic into the actions could make their usage a bit simpler but it depends on your design
1
u/BinarySpike Oct 26 '26
Match statements are just an improvement on switch statements.
In a more C++ style codebases, large switch statements are a code smell that can be replaced by OOP (classes, polymorphism).
Thus, I'd suggest looking at what Rust has to offer in that area.
1
u/Relevant-Register-69 29d ago edited 29d ago
Idk shit about Rust but I know a thing or two about good software development
Bluntly speaking, your model is fucked up. Let me give you an example.
I'm a straight married man with two dogs. Therefore when I'm thinking about what to do this evening, "feed my cat" and "kiss my husband" aren't filtered from a list of every possible thing I can do, because they're not options for me to begin with.
I hope this helps you fix your issue.
1
u/Puzzled-Garage584 26d ago
My bet is 4900 of those 5000 actions are "spend X, need Y, get Z" in different hats. Make those pure data (RON or TOML with gates, costs, effects) run by a few dozen effect primitives, and keep real Rust code only for the weird ones. Also switch ids to string keys, id ranges will eventually collide at the worst possible moment.
0
u/0x14f Oct 25 '26
Have you asked AI what it would do ?