add inventory
This commit is contained in:
parent
7279afeb4c
commit
88e49d7ec8
9 changed files with 79 additions and 17 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -1,2 +1,2 @@
|
|||
/elem
|
||||
/elem.exe
|
||||
/bin/elem
|
||||
/bin/elem.exe
|
2
Makefile
2
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
|
4
bin/inv.txt
Normal file
4
bin/inv.txt
Normal file
|
@ -0,0 +1,4 @@
|
|||
air
|
||||
earth
|
||||
fire
|
||||
water
|
|
@ -1,3 +1,4 @@
|
|||
#include "map.h"
|
||||
#include <ctype.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
@ -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;
|
||||
}
|
|
@ -1 +1,3 @@
|
|||
char *get_command(char *command, char *command_re, char **sort_tmp);
|
||||
#include "map.h"
|
||||
int get_command(char *command, char *command_re, char **sort_tmp);
|
||||
int slash_command(char *command, hashmap *inv);
|
|
@ -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++;
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
#include "map.h"
|
||||
void load_elements(hashmap *m, char *table);
|
||||
void load_elements(hashmap *m, char *table, int use_inv);
|
36
src/main.c
36
src/main.c
|
@ -1,9 +1,9 @@
|
|||
#include "command.h"
|
||||
#include "loader.h"
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#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);
|
||||
|
|
Loading…
Reference in a new issue