Compare commits

...

14 commits
0.5.0 ... main

6 changed files with 127 additions and 61 deletions

View file

@ -1,14 +1,14 @@
# Elemental on Command Line
This is an elemental combination game written in C, with optional multi-player support.
## Clients
- [PenguinMod client](https://studio.penguinmod.com/?fps=200&clones=Infinity&offscreen&size=850x480#7218964246) for playing the game online with a fancy interface
- [Discord client](https://discord.gg/DKZTWyWH3B) for playing the game publicly with other Discord members
- [Web client](https://elem.dervland.net/) for a demo of the multiplayer version
- [Original client](https://git.dervland.net/elemental/elemental-on-terminal/releases) for developers, administrators, and command line fans
## Single-player
A single-player example pack is provided as an example in ``bin/combos.txt``. This can be customized by the user, although internal limitations require the combination string to be in alphabetical order.
## Multi-player
To set up multi-player mode, create a directory ``../elem_data``. Also, create separate directories for each user that plays.
The game can now be played with the following syntax, inside your user directory:
```sh
cd ../your_user_dir
../elemental-command-line/bin/elem your_username
```
To set up multi-player mode, use the guide with the [Web proxy](https://git.dervland.net/elemental/elemental-to-web).

View file

@ -80,7 +80,7 @@ int inv_handler(const void *key, size_t size, uintptr_t val, void *usr) {
if (val2[strlen(val2) - 1] == '\n') {
printf("- user:%s suggested %s", key3, val2);
} else {
printf("- user:%s suggested %s\n", key3, val2);
printf("- user:%s suggested %s\n", key3, val2);
}
free(key3);
@ -103,28 +103,41 @@ int inv_handler(const void *key, size_t size, uintptr_t val, void *usr) {
int polls_handler(const void *key, size_t size, uintptr_t val, void *usr) {
struct verifier *verified = (struct verifier *)usr;
if (((char *)val)[strlen(((char *)val)) - 1] == '\n') {
((char *)val)[strlen(((char *)val)) - 1] = '\0';
char *val2 = (char *)val;
char *val3 = malloc(strlen(val2) + 1);
strcpy(val3, val2);
if (val3[strlen(val3) - 1] == '\n') {
val3[strlen(val3) - 1] = '\0';
}
if (strncmp(verified->name, key, strlen(verified->name)) == 0 &&
strcmp(verified->sugg, (char *)val) == 0)
strcmp(verified->sugg, val3) == 0) {
free(val3);
return -1;
}
free(val3);
return 0;
}
int success_handler(const void *key, size_t size, uintptr_t val, void *usr) {
struct succ *verified = (struct succ *)usr;
if ((char *)val != verified->sugg) {
//((char *)val)[strlen(((char *)val)) - 1] = '\0';
char *val2 = (char *)val;
char *val3 = malloc(strlen(val2) + 1);
strcpy(val3, val2);
if (val3[strlen(val3) - 1] == '\n') {
val3[strlen(val3) - 1] = '\0';
}
if (strcmp(verified->sugg, (char *)val) == 0) {
if (strcmp(verified->sugg, val3) == 0) {
verified->points[0]++;
}
free(val3);
return 0;
}
@ -146,8 +159,8 @@ int handle_pages_int(char *command, char *invs) {
}
}
int suggest_command(char *command, char *command_re, hashmap *polls,
char *name) {
int suggest_command(char *command, char *command_re, hashmap *polls, char *name,
int was_combination) {
char *page = handle_pages(command, "/suggest ");
@ -167,8 +180,17 @@ int suggest_command(char *command, char *command_re, hashmap *polls,
char *key = calloc(MAX_BUF_LENGTH, sizeof(char));
sprintf(key, "%s_%i", name, (int)rand());
struct verifier verified = {.name = name, .sugg = val};
int point_thing = 0;
struct succ succer = {.sugg = val, .points = &point_thing};
hashmap_iterate(polls, success_handler, (void *)&succer);
if (was_combination == 2 && point_thing == 0) {
printf("You cannot create unique polls with elements you do not have.\n");
return 1;
}
struct verifier verified = {.name = name, .sugg = val};
if (hashmap_iterate(polls, polls_handler, (void *)&verified) == -1) {
printf("You already suggested this!\n");
return 1;
@ -176,13 +198,9 @@ int suggest_command(char *command, char *command_re, hashmap *polls,
hashmap_set(polls, key, strlen(key), (uintptr_t)val, 1);
int point_thing = 0;
struct succ succer = {.sugg = val, .points = &point_thing};
hashmap_iterate(polls, success_handler, (void *)&succer);
point_thing++;
if (point_thing == UPVOTE_IN) {
printf("Poll was added into the game!\n");
FILE *fptr;
@ -238,18 +256,24 @@ int polls_command(char *command, hashmap *polls, hashmap *cmd) {
}
int path_command(char *command, hashmap *elements_rev, hashmap *already_done,
int top) {
char *page;
int top, hashmap *inv) {
char *page2;
if (top) {
page = handle_pages(command, "/path ");
if (page == 0)
page2 = handle_pages(command, "/path ");
if (page2 == 0)
return 0;
} else {
page = command;
page2 = command;
}
page[strlen(page) - 1] = '\0';
if (strlen(page) == 0) return 1;
char *page = malloc(strlen(page2) + 1);
strcpy(page, page2);
if (page[strlen(page) - 1] == '\n') {
page[strlen(page) - 1] = '\0';
}
if (strlen(page) == 0) {
return 1;
}
if (top) {
printf("Path of %s:\n", page);
@ -258,38 +282,62 @@ int path_command(char *command, hashmap *elements_rev, hashmap *already_done,
}
uintptr_t result;
hashmap_get(inv, page, strlen(page), &result);
if (top && result != 1) {
printf("You don't have %s.\n", page);
return 1;
}
hashmap_get(already_done, page, strlen(page), &result);
if (result != 0)
if (result != 0) {
return 1;
}
hashmap_set(already_done, page, strlen(page), (uintptr_t)1, 0);
hashmap_get(elements_rev, page, strlen(page) - 1, &result);
if (result == 0 || strlen((char *)result) < 1)
if (result == 0 || strlen((char *)result) < 1) {
return 1;
printf("%s: %s", command, (char *)result);
}
// todo: refactor;
char *tmp = (char *)result, *tmp2 = 0;
if (tmp[strlen(tmp) - 1] == '\n') {
tmp[strlen(tmp) - 1] = '\0';
}
int combos = 0;
for (int i = 1; i < MAX_COMBO_LENGTH; i++) {
combos = i;
tmp2 = tmp;
tmp = strstr(tmp, ";");
if (tmp == 0 || tmp2 == tmp)
break;
if (strlen(tmp) < 2) {
if (tmp2[0] == ';') {
tmp2++;
}
tmp = strstr(tmp2, ";");
if (tmp == 0) {
tmp = &tmp2[strlen(tmp2)];
}
char *tmp23 = malloc(strlen(tmp2) - strlen(tmp) + 2);
memcpy(tmp23, &tmp2[0], strlen(tmp2) - strlen(tmp) + 2);
tmp23[strlen(tmp2) - strlen(tmp) + 1] = '\0';
char *strstrd = strstr(tmp23, ";");
if (strstrd != 0) {
strstrd[0] = '\0';
}
path_command(tmp23, elements_rev, already_done, 0, inv);
if (strlen(tmp23) < 1) {
break;
}
tmp[0] = '\0';
tmp++;
path_command(tmp, elements_rev, already_done, 0);
free(tmp23);
}
printf("<- %s\n-> %s\n", (char *)result, (char *)page);
return 1;
}

View file

@ -2,8 +2,8 @@
int get_command(char *command, char *command_re, char **sort_tmp);
int slash_command(char *command, hashmap *inv);
int suggest_command(char *command, char *command_re, hashmap *polls,
char *name);
char *name, int was_combination);
int help_command(char *command);
int polls_command(char *command, hashmap *polls, hashmap *cmd);
int path_command(char *command, hashmap *elements_rev, hashmap *already_done,
int top);
int top, hashmap *inv);

View file

@ -44,7 +44,17 @@ int load_elements(hashmap *m, char *table, int use_inv) {
char *combo = calloc(strlen(combo_o) + 1, sizeof(char));
strcpy(combo, combo_o);
if (use_inv == 2) {
if (use_inv == 3) {
uintptr_t result;
hashmap_get(m, str, strlen(str) - 1, &result);
char *res = (char *)result;
if (res != 0)
continue;
}
if (use_inv == 2 || use_inv == 3) {
hashmap_set(m, str, strlen(str) - 1, (uintptr_t)combo, 1);
continue;
}

View file

@ -16,11 +16,11 @@ void init_tables(hashmap *elements, hashmap *inv, hashmap *polls,
load_elements(elements, combo_file, 0) ||
load_elements(elements, "bin/" combo_file, 0);
load_elements(elements_rev, "../elem_data/" combo_file, 2);
load_elements(elements_rev, combo_file, 2) ||
load_elements(elements_rev, "bin/" combo_file, 2);
load_elements(elements_rev, "../elem_data/" combo_file, 3);
load_elements(elements_rev, combo_file, 3) ||
load_elements(elements_rev, "bin/" combo_file, 3);
load_elements(polls, "../elem_data/" poll_file, 2);
load_elements(polls, "../elem_data/" poll_file, 3);
if (!do_inv)
return;
@ -50,7 +50,7 @@ int main(int argc, char *argv[]) {
char *command_re = calloc(MAX_BUF_LENGTH, sizeof(char));
char **sort_tmp = calloc(MAX_COMBO_LENGTH, sizeof(char **));
int wasCombination = 0;
int was_combination = 0;
init_tables(elements, inv, polls, elements_rev, 1);
@ -80,18 +80,19 @@ int main(int argc, char *argv[]) {
command[i] = tolower(command[i]);
}
if (wasCombination && suggest_command(command, command_re, polls, name)) {
wasCombination = 0;
if (was_combination &&
suggest_command(command, command_re, polls, name, was_combination)) {
continue;
}
wasCombination = 0;
was_combination = 0;
if (help_command(command))
continue;
if (polls_command(command, polls, elements))
continue;
if (slash_command(command, inv))
continue;
if (path_command(command, elements_rev, already_done, 1))
if (path_command(command, elements_rev, already_done, 1, inv))
continue;
int combos = get_command(command, command_re, sort_tmp);
@ -110,9 +111,6 @@ int main(int argc, char *argv[]) {
}
}
if (failed)
continue;
uintptr_t result;
hashmap_get(elements, command_re, strlen(command_re), &result);
@ -122,12 +120,21 @@ int main(int argc, char *argv[]) {
hashmap_get(elements, command_re, strlen(command_re), &result);
}
if (result == 0 && failed) {
was_combination = 2;
printf("Use /suggest to upvote a pre-existing combination.\n");
continue;
}
if (result == 0) {
wasCombination = 1;
was_combination = 1;
printf("You didn't make anything; use /suggest to suggest an element.\n");
continue;
}
if (failed)
continue;
char *res_str = (char *)result;
uintptr_t result2;

View file

@ -1,5 +1,6 @@
make
CC=x86_64-w64-mingw32-gcc BIN=./bin/elem.exe make
CC=i686-w64-mingw32-gcc BIN=./bin/elem_32.exe make
rm ./elem.tar.gz
tar -cvf ./elem.tar.gz bin
CC=x86_64-w64-mingw32-gcc BIN=bin/elem.exe make
CC=i686-w64-mingw32-gcc BIN=bin/elem_32.exe make
strip --strip-all bin/elem
rm elem.tar.gz
tar -cvf elem.tar.gz bin