I have a C program (it's a small roguelike that I wrote twelve years ago). I would like to translate it into Rust. The part of the design I'm completely stuck on how to translate is the way items and monsters are represented.
An item in the game world has some per-instance data (what specific kind of item it is, where it is, how many of it are in the slot) and some per-kind data (what that kind of item is called, what broad category of item it's in, what it's on-screen representation is, how powerful it is if it's a weapon or armour, etc).
In the original C implementation, this is implemented as two structs:
-
permobj
, containing the per-kind data; there is a singular global array of these, calledpermobjs
, which is initialized at compile-time. -
obj
, containing the per-instance data, one field of which is an index intopermobjs
(because references by array index are much easier to serialize than references by pointer).
(Monsters have an equivalent arrangement.)
The "singular global array" model the C program uses for the per-kind data seems like it would not be sane to implement in Rust, because static
objects have to have their type explicitly specified in full - which, for an array, means knowing exactly how many entries there are; I am not willing to hand-count my items and monsters, and "compile, see how many entries the error message says the array should have, adjust" is not a sensible workflow.
I'm very attached to the compile-time initialization, though, simply because the only language I've ever met where writing text-processing code was pleasant was Perl.