timesynk/common/data.h

143 lines
4.9 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_POINTER 30
#define T_TABLE 50
#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 TileSetData *getTileSetData(struct Data *data, int tileset_id);
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 TileData *newTileData(int size);
struct TileData *duplicateTileData(struct TileData *data);
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 *newInventoryData();
struct InventoryData *loadInventoryData(char *memory, int *offset, int *depth);
void freeInventoryData(struct InventoryData *inventory_data);
void setInventoryDataName(struct InventoryData *inventory, const char *name);
struct Data *loadDataFromMemory(char *memory, int size);
void freeData(struct Data *data);
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;
int count;
struct TablePair **pair;
};
struct TablePair {
int type;
int value_size;
char *key;
void *value;
struct TablePair *next;
};
struct Table *newTable(int size);
struct TablePair *duplicateTablePair(struct TablePair *from);
void freeTable(struct Table *table);
int getTableIndex(struct Table *table, const char *key);
int addTablePairAtIndex(struct Table *table, int index, void *value, int value_size, int type);
int addTablePair(struct Table *table, const char *key, void *value, int value_size, int type);
int addTablePairPointer(struct Table *table, const char *key, void *value, int type);
int addTablePairInt(struct Table *table, const char *key, int value);
int addTablePairFloat(struct Table *table, const char *key, float value);
int addTablePairChar(struct Table *table, const char *key, char value);
int addTablePairString(struct Table *table, const char *key, char *value);
void setTablePair(struct TablePair *table_pair, const char *key, void *value, int value_size, int type);
void setTablePairAtIndex(struct TablePair *table_pair, int index, void *value, int value_size, int type);
void setTablePairPointer(struct TablePair *table_pair, const char *key, void *value, int type);
int dumpTable(struct Table *table, int depth);
struct TablePair *getTablePair(struct Table *table, const char *key);
void *getTablePairValue(struct Table *table, const char *key);
int getTablePairValueInt(struct Table *table, const char *key);
float getTablePairValueFloat(struct Table *table, const char *key);
char getTablePairValueChar(struct Table *table, const char *key);
char *getTablePairValueString(struct Table *table, const char *key);
struct Table *getTablePairValueTable(struct Table *table, const char *key);
int deleteTablePair(struct Table *table, char *key);
void freeTablePair(struct TablePair *table_pair);
int saveDataToFile(struct Data* data, const char *filename);
int loadConfig_r(struct Table *table, char *memory, int size, int *offset);
#endif