From c7f3ac15d387ea7cead15d4871a07086f7fc8d30 Mon Sep 17 00:00:00 2001 From: kts Date: Sat, 21 Dec 2013 02:38:08 -0800 Subject: [PATCH] added pack_tiles.c, providing pack_tiles, a program that takes a directory as an argument and creates a new file (directory/directory.tsd) from cleaned files 'doors', 'floors', 'npcs', 'walls', 'equips', 'items', and 'players' in that directory. At the moment it can be used on the xibalba directory to create 'xibalba/xibalba.tsd', which can then be tested with test/t2d.c, providing the internal filename referenced is changed. At the moment the Data system does not properly deal with inventories so any inventory/equipment declaration is ignored. --- Makefile | 3 + data.c | 6 +- pack_data | Bin 0 -> 8920 bytes pack_data.c | 132 ++++++++++++++++++++++++++++++++++++++++++++ test/t2d.c | 13 ++++- xibalba/doors | 4 ++ xibalba/floors | 7 +++ xibalba/players | 46 +++++++++++++++ xibalba/walls | 7 +++ xibalba/xibalba.tsd | 108 ++++++++++++++++++++++++++++++++++++ 10 files changed, 323 insertions(+), 3 deletions(-) create mode 100755 pack_data create mode 100644 pack_data.c create mode 100644 xibalba/doors create mode 100644 xibalba/floors create mode 100644 xibalba/players create mode 100644 xibalba/walls create mode 100644 xibalba/xibalba.tsd diff --git a/Makefile b/Makefile index 38c4a53..38bf272 100644 --- a/Makefile +++ b/Makefile @@ -42,6 +42,9 @@ clean: pack_tiles: pack_tiles.c $(CC) pack_tiles.c -o pack_tiles +pack_data: pack_data.c + $(CC) pack_data.c -o pack_data + tiles: pack_tiles ./pack_tiles $(CC) $(CFLAGS) -c tiles/tiles.c diff --git a/data.c b/data.c index 8f6fcd7..304a752 100644 --- a/data.c +++ b/data.c @@ -170,8 +170,10 @@ struct TileData *loadTileData(int id, char *memory, int *offset, int *depth) { case '\n': var_value[i] = '\0'; if (mode == 2) { - addTablePair(tile_data->table, var_name, var_value, strlen(var_value)+1, T_STRING); - printf("%s(%d)=>%s\n", var_name, getTableIndex(tile_data->table, var_name), (char*)getTablePair(tile_data->table, var_name)->value); + if (*depth == 2) { + addTablePair(tile_data->table, var_name, var_value, strlen(var_value)+1, T_STRING); + printf("%s(%d)=>%s\n", var_name, getTableIndex(tile_data->table, var_name), (char*)getTablePair(tile_data->table, var_name)->value); + } } i = 0; mode = 0; diff --git a/pack_data b/pack_data new file mode 100755 index 0000000000000000000000000000000000000000..0e36b77e3f6675302842ae98aa8faa7d0e3773b5 GIT binary patch literal 8920 zcmeHNU1%KF6+W}OTK)J(N}{-Gt%y;u;*>^`orJPgWU(t&bd42NbvDE_smH6`S?!?N zpYF_Bt7eppO-mhW8a)`A23yTT5q=159`ay_nw#f(yw*p${R1qFVjV z%$?oQNPTHu3fX}(_ndRjIrp6J-aD&1qkI4O*Pn>!eoz-E1lmUwc?|7OK@upYD6@(0 zPW@o&^!Fe&uAT`*?m%+YUt=D9l!hVybnMIrrM_e<)5A{Q!j4 z$<;BmDdMzzQxs#VI9Jvc!>r96XE~U|Lu|ZBbbwo~*SQd*L$E!Hk~Pe#R+<-zJ!0I2 z>tVz|+l&$51qR7PIw1l%jf}g8Ljwb@Uto~Sm>I{=xhcwC{&ZR3*vP->!8PZW^juT3 zM-2E=KVoqEUt;JP56X&JMa*4!T!(KAF}U@|;5j`A;tLq?`k6jCb80GivauG9LCv+x zpwI?x?8CO+#qs*we))N`1l@PGT_SIv9+gs^DdXhKU z3f9(@!R>YX$JqFIkh( z{+imBFNM~uj_}&Dovok1)9J$H&aLHF!fV$4uqWFyc0_L27hL-j3$R`2ytS-_*S!As z-rxQL`E(#3+nU%{=lCyw5?Wh%iC$Y-rK?W91C+F1qO0TB*|CdJ zyN422x8FvrSsoL4O31Uv{+M%Y)5Q2G*r(juwkUVIRYrBH6pSI6R{jo`kTyW|T{2Xu`pkMMB?i{qe zxjb~LzMOv^c(r}U>jVFg#|yg~DT;XYIQ5IUdX(+NxHqi`;~sfm%->zqdN)PkEqg%f zgS`~L7QXG}WutE4w<-Ky5p$G+kIk4l{gdtM_|fPVb43=ln>==6H9YIZx`4Xa-m&Wx zwT0T(t@zwmy)=HVUv!Xb?vO=1NBqe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\t%s my_data_dir\nWhere my_data_dir is a directory with at least the following:\n tile data files:\n\tequips\n\titems\n\tplayers\n\twalls\n\tfloors\n\tnpcs\n\tdoors\nThese files will be cleaned of comments and unnecessary white space before being combined into a single file named 'data'.\n" +#define PROCESSING "-> processing %s\n" +#define WRITING "--> appending buffer to %s\n" +#define PROC_TARGET "--> copying %s to buffer and cleaning\n" +#define CANNOT_OPEN "ERR: %s either doesn't exist or is empty\n" +#define CANNOT_WRITE "ERR: couldn't open %s for writing\n" +#define DONE "-> finished writing %s\n" + +int fileToMemory(char **buffer, char *file_name) { + int i, n, pos = 0; + char t_buf[16]; + + FILE *file = fopen(file_name, "r"); + if (file == NULL) + return -1; + fseek(file, 0, SEEK_END); + int size = ftell(file); + fseek(file, 0, SEEK_SET); + + char *new_buffer = malloc(size); + while ((n = fread(t_buf, 1, 16, file))) { + for (i = 0; i < n; i++) { + new_buffer[pos++] = t_buf[i]; + } + } + fclose(file); + *buffer = new_buffer; + return size; +} + +#define FILE_COUNT 8 + +char files[FILE_COUNT][15] = { + "equips", + "items", + "players", + "walls", + "floors", + "npcs", + "doors" +}; + +#define MODE_NO_FIRST_CHAR 0 +#define MODE_NORMAL 1 +#define MODE_CLEAR_LINE 2 + +int main(int argc, char **argv) { + printf("%s\n", TITLE); + char *buffer; + char file_name[63]; + char final_file[63]; + sprintf(final_file, "%s/%s.tsd", argv[1], argv[1]); + + remove(final_file); + + FILE *output_file = fopen(final_file, "w"); + if (output_file == NULL) { + printf(CANNOT_WRITE, final_file); + return -1; + } + + if (argc > 1) { + printf(PROCESSING, argv[1]); + int i; + for (i = 0;i < FILE_COUNT;i++) { + if (files[i][0] != '\0') { + sprintf(file_name, "%s/%s", argv[1], files[i]); + int file_chars = 0; + if ((file_chars = fileToMemory(&buffer, file_name)) > 0) { + printf(PROC_TARGET, file_name); + int buffer_offset = 0; + int buffer_2_offset = 0; + char buffer_2[file_chars]; + int mode = 0; + while (buffer[buffer_offset] != '\0') { + switch (buffer[buffer_offset]) { + case ' ': + if (mode == MODE_NORMAL) { + buffer_2[buffer_2_offset++] = buffer[buffer_offset++]; + } else { + buffer_offset++; + } + break; + case '\n': + if (mode == MODE_NO_FIRST_CHAR) { + buffer_offset++; + } else if (mode == MODE_CLEAR_LINE) { + mode = MODE_NO_FIRST_CHAR; + buffer_offset++; + } else { + mode = MODE_NO_FIRST_CHAR; + buffer_2[buffer_2_offset++] = buffer[buffer_offset++]; + } + break; + case ';': + if (mode == MODE_NO_FIRST_CHAR) { + mode = MODE_CLEAR_LINE; + buffer_offset++; + } + break; + default: + if (mode == MODE_NO_FIRST_CHAR) { + mode = MODE_NORMAL; + } else if (mode == MODE_CLEAR_LINE) { + buffer_offset++; + } else { + buffer_2[buffer_2_offset++] = buffer[buffer_offset++]; + } + break; + } + } + buffer_2[buffer_2_offset] = '\0'; + // write cleaned buffer to file + printf(WRITING, final_file); + fprintf(output_file, "%d {\n", i); + fprintf(output_file, "%s", buffer_2); + fprintf(output_file, "}\n"); + } else { + printf(CANNOT_OPEN, file_name); + } + } + } + fclose(output_file); + printf(DONE, final_file); + } else { + printf(USAGE, argv[0]); + } +} diff --git a/test/t2d.c b/test/t2d.c index 6db1ea3..2c84398 100644 --- a/test/t2d.c +++ b/test/t2d.c @@ -27,25 +27,35 @@ int fileToMemory(char **buffer, char *file_name) { int main() { char *buffer; - int size = fileToMemory(&buffer, "tiles.txt"); + int size = fileToMemory(&buffer, "xibalba.tsd"); struct Data *data = loadDataFromMemory(buffer, size); free(buffer); printf("---- total tile sets: %d\n", data->set_count); + int bytes = 0; + + bytes += sizeof(struct Data) + (sizeof(int) * data->size) + (sizeof(struct TileSetData)*data->size); + int count = 0; while (count < data->size) { int count_2 = 0; if (data->set[count]) { + bytes += sizeof(struct TileSetData) + (sizeof(struct TileData*)*data->set[count]->size); + bytes += sizeof(struct Table); while (count_2 < data->set[count]->size) { if (data->set[count]->tile[count_2]) { + bytes += sizeof(struct TileData); printf("- TILE %d:%d\n", count, count_2); struct Table *table = data->set[count]->tile[count_2]->table; int i = 0; while (i < table->size) { struct TablePair *table_pair = table->pair[i]; while (table_pair != NULL) { + bytes += sizeof(struct TablePair); + bytes += sizeof(*table_pair->value); + bytes += sizeof(*table_pair->key); if (table_pair->type == T_STRING) printf("%s=>%s!\n", table_pair->key, table_pair->value); table_pair = table_pair->next; @@ -58,6 +68,7 @@ int main() { } count++; } + printf("estimated bytes: %d\n", bytes); printf("getTileDataByKey 'big key'\n"); struct TileData *tile = getTileDataByKey(data, "big key"); diff --git a/xibalba/doors b/xibalba/doors new file mode 100644 index 0000000..32b59d6 --- /dev/null +++ b/xibalba/doors @@ -0,0 +1,4 @@ +0 { + name wooden door + state 0 +} diff --git a/xibalba/floors b/xibalba/floors new file mode 100644 index 0000000..35d3653 --- /dev/null +++ b/xibalba/floors @@ -0,0 +1,7 @@ +0 { + name stone floor +} + +1 { + name wood floor +} diff --git a/xibalba/players b/xibalba/players new file mode 100644 index 0000000..c073f7e --- /dev/null +++ b/xibalba/players @@ -0,0 +1,46 @@ +0 { + name warrior + race human + vision 16 + stats S8D8C8I8W8c8 + slots H1F1N2S2A2a2h2D8w2T1W1L2l2f2 +} + +1 { + name mage +} + +7 { + name medicine man + race manitou + stats S8D8C8I8W8c8 + slots H1F1N2S2A2a2h2D8w2T1W1L2l2f2 +} + +12 { + name shock trooper + race ibexian + stats S10D8C10I6W6c6 + slots H1F1N2S2A2a2h2D8w2T1W1L2l2o2 +} + +17 { + name barbarian + race ibexian + stats S12D10C10I4W4c4 + slots H1F1N2S2A2a2h2D8w2T1W1L2l2o2 +} + +18 { + name warrior + race quosqoy + stats S8D8C10I8W8c8 + slots H1F1N2S2A2a2h2D8w2T1W1L2l2f2 +} + +28 { + name gunner + race quosqo + stats S6D8C10I10W8c8 + slots H1F1N2S2A2a2h2D8w2T1W1L2l2f2 +} diff --git a/xibalba/walls b/xibalba/walls new file mode 100644 index 0000000..f7071dd --- /dev/null +++ b/xibalba/walls @@ -0,0 +1,7 @@ +0 { + name stone wall +} + +1 { + name wood wall +} diff --git a/xibalba/xibalba.tsd b/xibalba/xibalba.tsd new file mode 100644 index 0000000..3d61932 --- /dev/null +++ b/xibalba/xibalba.tsd @@ -0,0 +1,108 @@ +0 { +0 { +name small macana +required_slots w1 +damage_mod P1d4 +skill slashing +} +1 { +name large macana +required_slots w2 +damage_mod P1d8 +skill slashing +} +16 { +name cloth armour +required_slots T1 +armour_mod P1 +} +} +1 { +0 { +name small key +type 0 +} +} +2 { +0 { +name warrior +race human +vision 16 +stats S8D8C8I8W8c8 +slots H1F1N2S2A2a2h2D8w2T1W1L2l2f2 +} +1 { +name mage +} +7 { +name medicine man +race manitou +stats S8D8C8I8W8c8 +slots H1F1N2S2A2a2h2D8w2T1W1L2l2f2 +} +12 { +name shock trooper +race ibexian +stats S10D8C10I6W6c6 +slots H1F1N2S2A2a2h2D8w2T1W1L2l2o2 +} +17 { +name barbarian +race ibexian +stats S12D10C10I4W4c4 +slots H1F1N2S2A2a2h2D8w2T1W1L2l2o2 +} +18 { +name warrior +race quosqoy +stats S8D8C10I8W8c8 +slots H1F1N2S2A2a2h2D8w2T1W1L2l2f2 +} +28 { +name gunner +race quosqo +stats S6D8C10I10W8c8 +slots H1F1N2S2A2a2h2D8w2T1W1L2l2f2 +} +} +3 { +0 { +name stone wall +} +1 { +name wood wall +} +} +4 { +0 { +name stone floor +} +1 { +name wood floor +} +} +5 { +0 { +name nupi +faction chthonic +party nupii +vision 4 +behavior 0 +hp 2+1d4 +stats S6D6C8I4W4C4 +slots T1w2 +equipment { +small macana, 1, 10 +cloth armour, 1, 25 +} +inventory { +gold, 1-3, 5 +} +} +} +6 { +0 { +name wooden door +state 0 +} +}