diff --git a/.gitignore b/.gitignore index ecf54ad..7024c52 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -/elem -/elem.exe \ No newline at end of file +/bin/elem +/bin/elem.exe \ No newline at end of file diff --git a/Makefile b/Makefile index 75f31d0..29d7bb4 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CC?=gcc -BIN?=elem +BIN?=bin/elem make: src/main.c $(CC) -o $(BIN) src/map.c src/loader.c src/command.c src/main.c \ No newline at end of file diff --git a/combos.txt b/bin/combos.txt similarity index 100% rename from combos.txt rename to bin/combos.txt diff --git a/bin/inv.txt b/bin/inv.txt new file mode 100644 index 0000000..07496fd --- /dev/null +++ b/bin/inv.txt @@ -0,0 +1,4 @@ +air +earth +fire +water diff --git a/src/command.c b/src/command.c index f4e0db4..4f2999a 100644 --- a/src/command.c +++ b/src/command.c @@ -1,3 +1,4 @@ +#include "map.h" #include #include #include @@ -22,8 +23,36 @@ int sort_comp(const void *a, const void *b) { return strcmp(*(char **)a, *(char **)b); } -char *get_command(char *command, char *command_re, char **sort_tmp) { - fgets(command, MAX_BUF_LENGTH - 1, stdin); +int inv_handler(const void *key, unsigned long size, unsigned long val, + void *usr) { + if (val == 0) + return 0; + char *key2 = (char *)key; + + if (key2[strlen(key2) - 1] == '\n') { + printf("- %s", (char *)key); + } else { + printf("- %s\n", (char *)key); + } + return 1; +} + +int slash_command(char *command, hashmap *inv) { + + if (command[0] == '/') { + command++; + } else { + return 0; + } + + if (strcmp(command, "inv\n") == 0) { + printf("Your inventory:\n"); + hashmap_iterate(inv, inv_handler, "inv"); + } + return 1; +} + +int get_command(char *command, char *command_re, char **sort_tmp) { while (1) { int get_out = 0; @@ -44,7 +73,7 @@ char *get_command(char *command, char *command_re, char **sort_tmp) { int combos = 0; for (int i = 1; i < MAX_COMBO_LENGTH; i++) { combos = i; - sort_tmp[i] = strstr(sort_tmp[i-1], ";"); + sort_tmp[i] = strstr(sort_tmp[i - 1], ";"); if (sort_tmp[i] == 0) break; sort_tmp[i][0] = '\0'; @@ -64,5 +93,5 @@ char *get_command(char *command, char *command_re, char **sort_tmp) { command_re_tmp += strlen(last); } - return command_re; + return combos; } \ No newline at end of file diff --git a/src/command.h b/src/command.h index aead233..d5c61f4 100644 --- a/src/command.h +++ b/src/command.h @@ -1 +1,3 @@ -char *get_command(char *command, char *command_re, char **sort_tmp); \ No newline at end of file +#include "map.h" +int get_command(char *command, char *command_re, char **sort_tmp); +int slash_command(char *command, hashmap *inv); \ No newline at end of file diff --git a/src/loader.c b/src/loader.c index e712c63..1385fb5 100644 --- a/src/loader.c +++ b/src/loader.c @@ -6,7 +6,7 @@ #define MAX_FILE_SIZE 1024 * 16 -void load_elements(hashmap *m, char *table) { +void load_elements(hashmap *m, char *table, int use_inv) { FILE *fptr; fptr = fopen(table, "r"); @@ -18,6 +18,11 @@ void load_elements(hashmap *m, char *table) { if (!fgets(str, MAX_FILE_SIZE, fptr)) break; + if (use_inv) { + hashmap_set(m, str, strlen(str) - 2, (uintptr_t)1); + continue; + } + char *combo = strstr(str, ";"); combo[0] = '\0'; combo++; diff --git a/src/loader.h b/src/loader.h index 2f1bdde..40ac584 100644 --- a/src/loader.h +++ b/src/loader.h @@ -1,2 +1,2 @@ #include "map.h" -void load_elements(hashmap *m, char *table); \ No newline at end of file +void load_elements(hashmap *m, char *table, int use_inv); \ No newline at end of file diff --git a/src/main.c b/src/main.c index 2e5ed4d..6eb9252 100644 --- a/src/main.c +++ b/src/main.c @@ -1,9 +1,9 @@ +#include "command.h" +#include "loader.h" #include #include #include #include -#include "command.h" -#include "loader.h" #define MAX_BUF_LENGTH 1024 #define MAX_COMBO_LENGTH 1024 @@ -16,23 +16,45 @@ int main(int argc, char *argv[]) { char *command_re = calloc(MAX_BUF_LENGTH, sizeof(char)); char **sort_tmp = calloc(MAX_COMBO_LENGTH, sizeof(char **)); - load_elements(elements, "combos.txt"); + load_elements(elements, "bin/combos.txt", 0); + load_elements(inv, "bin/inv.txt", 1); + printf("Welcome to Elemental on Command Line!\n"); while (1) { // todo: separate into functions - command_re = get_command(command, command_re, sort_tmp); + printf("\n"); + fgets(command, MAX_BUF_LENGTH - 1, stdin); + int slashRan = slash_command(command,inv); + if (slashRan) continue; + + int combos = get_command(command, command_re, sort_tmp); + + int failed = 0; + for (int i = 0; i < combos; i++) { + uintptr_t result; + hashmap_get(inv, sort_tmp[i], strlen(sort_tmp[i]) - 1, &result); + if (result != 1) { + printf("You don't have %s.\n", sort_tmp[i]); + failed = 1; + continue; + } + } + + if (failed) continue; uintptr_t result; hashmap_get(elements, command_re, strlen(command_re), &result); - printf("%s\n",command_re); - if (result == 0) { printf("You didn't make anything.\n"); continue; } - printf("You made %s!\n", (char *)result); + + char *res_str = (char *)result; + + hashmap_set(inv, res_str, strlen(res_str) - 1, (uintptr_t)1); + printf("You made %s!\n", res_str); } // free(command);