arrays - Is there a more efficient way to store an n-ary tree in a file in C? -


say i'm writing adventure game. map built of tiles of different types. have tiles form paths, , tiles form doors, , on.

i use struct describe type , content of tile, , other tiles connects.

then i'll make quadruple-linked list connect them together.

but struct describe room have far more elements 1 describe door, many elements in door struct redundant. make smaller door struct, structs can point structs of same type*, couldn't connect room struct door struct. redundancy may negligible wondered if there's way.

another option using array of structs, i'd have lots of 'padding' structs wasting more space. array make reading , re-building map file easier.

is there way around limitation struct can point struct of same type? or there common solution problem haven't mentioned?

one idea had each tile have pointers every other type of tile. redundant, lesser redundancy option above.

*by mean typically in linked list, structs contain pointers struct of same type they're in.

you don't have have uniform struct describing everything. instead, following (this writing own c++ virtual tables in c, , used).

your basic tile struct can this:

struct tile {     // common tile stuff     ...     enum tile_type type;     void *type_info; }; 

so in struct store stuff that's common every tile type. make other structs other types: 1 room, 1 path, etc. within object of tile, make enum describe actual type, , store pointer concrete type within void *.


there many links describing variations of technique. here's one.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -