105 lines
3.3 KiB
C
105 lines
3.3 KiB
C
#ifndef DATA_H
|
|
#define DATA_H
|
|
|
|
#define SET_SIZE 32 // base set size of 32, if tiles go beyond, the set is realloc'd with this amount
|
|
|
|
/* optionally usable "types" to define for TablePair type property */
|
|
#define T_CHAR 1
|
|
#define T_SHORT 2
|
|
#define T_INT 3
|
|
#define T_LONG 4;
|
|
#define T_LONG_INT 5
|
|
#define T_LONG_LONG 6
|
|
#define T_FLOAT 7
|
|
#define T_DOUBLE 8
|
|
#define T_LONG_DOUBLE 9
|
|
#define T_STRING 20
|
|
|
|
#define T_PROTO_TILESET 100
|
|
#define T_PROTO_TILEDATA 101
|
|
#define T_PROTO_INVENTORY 102
|
|
|
|
struct Data {
|
|
int set_count;
|
|
int size;
|
|
int *id; // 0 = player, 1 = npc, etc.
|
|
struct TileSetData **set;
|
|
};
|
|
|
|
struct TileData *getTileDataByKey(struct Data *data, char *key);
|
|
struct TileData *getTileDataById(struct Data *data, int tileset_id, int tile_id);
|
|
/*
|
|
Sets work by having a basic linear TileData pointer array wherein tiles are referenced by their IDs. In addition to this, to allow for accessing tiles by name, a Table is used that provides a name->id table.
|
|
*/
|
|
struct TileSetData {
|
|
int tid;
|
|
int size;
|
|
int tile_count;
|
|
struct Table *keys;
|
|
struct TileData **tile;
|
|
};
|
|
|
|
struct TileData {
|
|
int tid; // tileset id, inherited from TileSetData during load
|
|
int id; // id of tile
|
|
int count; // count of key=>value pairs
|
|
int size; // max amount of entries
|
|
struct Table *table;
|
|
};
|
|
|
|
struct Range {
|
|
int min;
|
|
int max;
|
|
};
|
|
|
|
/*
|
|
InventoryData works similar to Tile Chains
|
|
*/
|
|
struct InventoryData {
|
|
// NOTE: should we even have tid/id?
|
|
int id;
|
|
int tid;
|
|
struct Range count;
|
|
float chance;
|
|
char *name;
|
|
struct InventoryData *next;
|
|
};
|
|
|
|
struct InventoryData *loadInventoryData(char *memory, int *offset, int *depth);
|
|
|
|
struct Data *loadDataFromMemory(char *memory, int size);
|
|
struct TileSetData *loadTileSetData(int tileset_id, char *memory, int *offset, int *depth);
|
|
struct TileData *loadTileData(int tile_id, char *memory, int *offset, int *depth);
|
|
void freeTileData(struct TileData *tile_data);
|
|
|
|
/*
|
|
Table(s) work by first getting an index from a key limited by modulo Table->size. Then it checks to see if the TablePair[index]'s key matches the wanted key and returns the value if so. If not, it runs the same check against TablePair[index]'s TablePair pointer, continuing down the line until either a match is found or the pointer is NULL.
|
|
This is a more flexible system than TablePair "chains" or having the index increase until the match is found. An increase in Table size will consume more memory, but run more efficiently than a small size, as it does not need to check down the pointer line to the same extent.
|
|
*/
|
|
struct Table {
|
|
int size;
|
|
struct TablePair **pair;
|
|
};
|
|
|
|
struct TablePair {
|
|
int type;
|
|
char *key;
|
|
void *value;
|
|
struct TablePair *next;
|
|
};
|
|
|
|
struct Table *newTable(int size);
|
|
void freeTable(struct Table *table);
|
|
|
|
int getTableIndex(struct Table *table, char *key);
|
|
|
|
int addTablePair(struct Table *table, char *key, void *value, int value_size, int type);
|
|
int addTablePairPointer(struct Table *table, char *key, void *value, int type);
|
|
void setTablePair(struct TablePair *table_pair, char *key, void *value, int value_size, int type);
|
|
void setTablePairPointer(struct TablePair *table_pair, char *key, void *value, int type);
|
|
struct TablePair *getTablePair(struct Table *table, char *key);
|
|
void *getTablePairValue(struct Table *table, char *key);
|
|
void freeTablePair(struct TablePair *table_pair);
|
|
|
|
#endif
|