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.

master
kts 2013-12-21 02:38:08 -08:00
parent 0005a93d65
commit c7f3ac15d3
10 changed files with 323 additions and 3 deletions

View File

@ -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

6
data.c
View File

@ -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;

BIN
pack_data 100755

Binary file not shown.

132
pack_data.c 100644
View File

@ -0,0 +1,132 @@
#include <stdio.h>
#include <stdlib.h>
#define TITLE "--- ts data packer ---"
#define USAGE "Usage is:\n\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]);
}
}

View File

@ -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");

4
xibalba/doors 100644
View File

@ -0,0 +1,4 @@
0 {
name wooden door
state 0
}

7
xibalba/floors 100644
View File

@ -0,0 +1,7 @@
0 {
name stone floor
}
1 {
name wood floor
}

46
xibalba/players 100644
View File

@ -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
}

7
xibalba/walls 100644
View File

@ -0,0 +1,7 @@
0 {
name stone wall
}
1 {
name wood wall
}

108
xibalba/xibalba.tsd 100644
View File

@ -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
}
}