Compare commits

..

21 commits
0.4.9 ... main

Author SHA1 Message Date
fac2179c23
hopefully fix the paging issue 2025-04-17 09:43:20 -04:00
9b196f0b13
fix weird newline issue 2025-04-17 09:27:13 -04:00
be291628b8
fix weird interference with polls 2025-04-14 20:36:53 -04:00
881d3602e7
clean up I/O database 2025-04-12 09:44:14 -04:00
d0553e6575
redirect guide 2025-04-09 12:14:48 -04:00
6b5452c923
clarity; link to the releases page 2025-04-09 12:12:49 -04:00
5a6bb6e982
improve the guide 2025-04-07 11:04:24 -04:00
090d4d5d8d
i put this in the wrong place so now it doesn't actually reduce the size 2025-04-05 15:49:21 -04:00
1f70dd7000
ugh 2025-04-03 13:48:03 -04:00
bab758e93f
avoid duping polls 2025-04-03 13:46:08 -04:00
f55db7a7fd
fix polls randomly coiming back 2025-04-03 13:29:03 -04:00
988ac26e44
allow suggesting polls of elems you don't have, and allow suggestion correction 2025-04-03 11:49:48 -04:00
735915db89
strip useless debug symbols 2025-04-03 11:18:32 -04:00
e22c9d42da
fix 2025-04-02 20:37:18 -04:00
ed50d4daba
reverse path order 2025-04-02 20:32:44 -04:00
7a63fcb376
don't show paths of elements you don't have 2025-04-02 01:28:18 -04:00
053b04133a
fix paths 2025-04-01 22:00:06 -04:00
060963039c
fix weird skips 2025-04-01 21:14:53 -04:00
f1bc13d4e2
add paths 2025-04-01 21:10:25 -04:00
e6c412e661
reduce size allocated for blocks 2025-03-31 21:38:33 -04:00
d356b5423e
parse users differently 2025-03-30 02:43:52 -04:00
9 changed files with 284 additions and 92 deletions

1
.gitignore vendored
View file

@ -5,3 +5,4 @@
/inv_users.txt
/polls.txt
/elem.tar.gz
/combos.txt

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

@ -1,9 +1,11 @@
#include "map.h"
#include <time.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "loader.h"
#include "main.h"
// huge cleanup operation soon
@ -77,9 +79,9 @@ int inv_handler(const void *key, size_t size, uintptr_t val, void *usr) {
}
if (val2[strlen(val2) - 1] == '\n') {
printf("- %s suggested %s", key3, val2);
printf("- user:%s suggested %s", key3, val2);
} else {
printf("- %s suggested %s\n", key3, val2);
printf("- user:%s suggested %s\n", key3, val2);
}
free(key3);
@ -102,34 +104,52 @@ 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;
}
char *handle_pages(char *command, char *invs) {
char *data;
char *data2;
if (strncmp(command, invs, strlen(invs)) == 0) {
return &command[strlen(invs)];
data = &command[strlen(invs)];
data2 = malloc(strlen(data) + 1);
memcpy(data2,data,strlen(data) + 1);
return data2;
} else {
return 0;
}
@ -145,8 +165,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, hashmap *combos, char *name,
int was_combination) {
char *page = handle_pages(command, "/suggest ");
@ -163,11 +183,22 @@ int suggest_command(char *command, char *command_re, hashmap *polls,
char *val = calloc(MAX_BUF_LENGTH, sizeof(char));
sprintf(val, "%s;%s", page, command_re);
srand(time(NULL));
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;
@ -175,22 +206,15 @@ 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;
fptr = fopen("../elem_data/" combo_file, "a");
if (fptr == NULL)
return 1;
fwrite(val, sizeof(char), strlen(val), fptr);
fwrite("\n", sizeof(char), 1, fptr);
fclose(fptr);
hashmap_set(combos, command_re, strlen(command_re), (uintptr_t)page, 0);
write_elements(combos, "../elem_data/" COMBO_FILE, 0);
return 1;
}
@ -200,14 +224,7 @@ int suggest_command(char *command, char *command_re, hashmap *polls,
FILE *fptr;
fptr = fopen("../elem_data/" poll_file, "a");
if (fptr == NULL)
return 1;
fwrite(key, sizeof(char), strlen(key), fptr);
fwrite(";", sizeof(char), 1, fptr);
fwrite(val, sizeof(char), strlen(val), fptr);
fwrite("\n", sizeof(char), 1, fptr);
fclose(fptr);
write_elements(polls, "../elem_data/" POLL_FILE, 3);
return 1;
}
@ -236,6 +253,92 @@ int polls_command(char *command, hashmap *polls, hashmap *cmd) {
return 1;
}
int path_command(char *command, hashmap *elements_rev, hashmap *already_done,
int top, hashmap *inv) {
char *page2;
if (top) {
page2 = handle_pages(command, "/path ");
if (page2 == 0)
return 0;
} else {
page2 = command;
}
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);
hashmap_free(already_done);
already_done = hashmap_create();
}
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) {
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) {
return 1;
}
// 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;
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;
}
free(tmp23);
}
printf("<- %s\n-> %s\n", (char *)result, (char *)page);
return 1;
}
int slash_command(char *command, hashmap *inv) {
int page = handle_pages_int(command, "/inv ");

View file

@ -2,6 +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);
hashmap *combos, 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, hashmap *inv);

View file

@ -6,7 +6,58 @@
#include "main.h"
int load_elements(hashmap *m, char *table, int use_inv) {
struct write_struct {
FILE *fptr;
int mode;
};
int entry_handler(const void *key, size_t size, uintptr_t val, void *usr) {
struct write_struct *usr2 = (struct write_struct *)usr;
char *val2 = (char *)val;
char *key2 = (char *)key;
if (usr2->mode == 0) {
fwrite(val2, sizeof(char), strlen(val2), usr2->fptr);
fwrite(";", sizeof(char), 1, usr2->fptr);
fwrite(key2, sizeof(char), strlen(key2), usr2->fptr);
if (key2[strlen(key2) - 1] != '\n') {
fwrite("\n", sizeof(char), 1, usr2->fptr);
}
} else if (usr2->mode == 1) {
fwrite(key2, sizeof(char), strlen(key2), usr2->fptr);
if (key2[strlen(key2) - 1] != '\n') {
fwrite("\n", sizeof(char), 1, usr2->fptr);
}
} else {
fwrite(key2, sizeof(char), strlen(key2), usr2->fptr);
fwrite(";", sizeof(char), 1, usr2->fptr);
fwrite(val2, sizeof(char), strlen(val2), usr2->fptr);
if (val2[strlen(val2) - 1] != '\n') {
fwrite("\n", sizeof(char), 1, usr2->fptr);
}
}
return 1;
}
void write_elements(hashmap *m, char *table, int mode) {
FILE *fptr;
fptr = fopen(table, "w");
if (fptr == NULL)
return;
struct write_struct writer = {.fptr = fptr, .mode = mode};
hashmap_iterate(m, entry_handler, (void *)&writer);
//fwrite("\n", sizeof(char), 1, fptr);
fclose(fptr);
}
int load_elements(hashmap *m, char *table, int mode) {
FILE *fptr;
fptr = fopen(table, "r");
@ -15,19 +66,28 @@ int load_elements(hashmap *m, char *table, int use_inv) {
return 0;
char *str;
char *str2;
int did_something = 0;
int lines_get = 0;
while (1) {
str = calloc(MAX_FILE_SIZE, sizeof(char));
if (!fgets(str, MAX_FILE_SIZE, fptr)) {
free(str);
str2 = calloc(MAX_FILE_SIZE, sizeof(char));
if (!fgets(str2, MAX_FILE_SIZE, fptr)) {
free(str2);
break;
}
lines_get++;
str = calloc(strlen(str2) + 1, sizeof(char));
strcpy(str, str2);
free(str2);
did_something = 1;
if (use_inv == 1) {
if (mode == 1) {
hashmap_set(m, str, strlen(str) - 1, (uintptr_t)1, 0);
continue;
}
@ -36,10 +96,20 @@ int load_elements(hashmap *m, char *table, int use_inv) {
combo_o[0] = '\0';
combo_o++;
char *combo = calloc(strlen(combo_o)+1,sizeof(char));
strcpy(combo,combo_o);
char *combo = calloc(strlen(combo_o) + 1, sizeof(char));
strcpy(combo, combo_o);
if (use_inv == 2) {
if (mode == 3) {
uintptr_t result;
hashmap_get(m, str, strlen(str) - 1, &result);
char *res = (char *)result;
if (res != 0)
continue;
}
if (mode == 2 || mode == 3) {
hashmap_set(m, str, strlen(str) - 1, (uintptr_t)combo, 1);
continue;
}

View file

@ -1,2 +1,3 @@
#include "map.h"
int load_elements(hashmap *m, char *table, int use_inv);
int load_elements(hashmap *m, char *table, int mode);
void write_elements(hashmap *m, char *table, int mode);

View file

@ -1,5 +1,6 @@
#include "command.h"
#include "loader.h"
#include "map.h"
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
@ -10,21 +11,26 @@
// todo: spacing in combos, clean init_tables
void init_tables(hashmap *elements, hashmap *inv, hashmap *polls, int do_inv) {
load_elements(elements, "../elem_data/" combo_file, 0);
load_elements(elements, combo_file, 0) ||
load_elements(elements, "bin/" combo_file, 0);
void init_tables(hashmap *elements, hashmap *inv, hashmap *polls,
hashmap *elements_rev, int do_inv) {
load_elements(elements, "../elem_data/" COMBO_FILE, 0);
load_elements(elements, COMBO_FILE, 0) ||
load_elements(elements, "bin/" COMBO_FILE, 0);
load_elements(polls, "../elem_data/" poll_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, 3);
if (!do_inv)
return;
load_elements(inv, "../elem_data/" inv_base_file, 1) ||
load_elements(inv, inv_base_file, 1) ||
load_elements(inv, "bin/" inv_base_file, 1);
load_elements(inv, "../elem_data/" INV_BASE_FILE, 1) ||
load_elements(inv, INV_BASE_FILE, 1) ||
load_elements(inv, "bin/" INV_BASE_FILE, 1);
load_elements(inv, inv_file, 1);
load_elements(inv, INV_FILE, 1);
}
int main(int argc, char *argv[]) {
@ -36,6 +42,8 @@ int main(int argc, char *argv[]) {
}
hashmap *elements = hashmap_create();
hashmap *elements_rev = hashmap_create();
hashmap *already_done = hashmap_create();
hashmap *inv = hashmap_create();
hashmap *polls = hashmap_create();
@ -43,11 +51,13 @@ 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, 1);
init_tables(elements, inv, polls, elements_rev, 1);
printf("%s, welcome to Elemental on Command Line!\nType /help for commands.\n", name);
printf("user:%s, welcome to Elemental on Command Line!\nType /help for "
"commands.\n",
name);
int newline = 1;
while (1) {
// todo: separate into functions
@ -71,18 +81,24 @@ 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, elements, name, was_combination)) {
continue;
}
wasCombination = 0;
was_combination = 0;
if (help_command(command))
continue;
if (polls_command(command, polls, elements))
continue;
if (polls_command(command, polls, elements)) {
hashmap_free(polls);
polls = hashmap_create();
init_tables(elements, inv, polls, elements_rev, 0);
}
if (slash_command(command, inv))
continue;
if (path_command(command, elements_rev, already_done, 1, inv))
continue;
int combos = get_command(command, command_re, sort_tmp);
if (combos < 2)
@ -99,24 +115,30 @@ int main(int argc, char *argv[]) {
}
}
if (failed)
continue;
uintptr_t result;
hashmap_get(elements, command_re, strlen(command_re), &result);
if (result == 0) {
init_tables(elements, inv, polls, 0);
init_tables(elements, inv, polls, elements_rev, 0);
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;
@ -130,15 +152,7 @@ int main(int argc, char *argv[]) {
hashmap_set(inv, res_str, strlen(res_str), (uintptr_t)1, 0);
printf("You made %s!\n", res_str);
FILE *fptr;
fptr = fopen(inv_file, "a");
if (fptr == NULL)
continue;
fwrite(res_str, sizeof(char), strlen(res_str), fptr);
fwrite("\n", sizeof(char), 1, fptr);
fclose(fptr);
write_elements(inv, INV_FILE, 1);
}
// free(command);

View file

@ -1,9 +1,9 @@
#define inv_file "inv.txt"
#define inv_base_file "inv_base.txt"
#define combo_file "combos.txt"
#define poll_file "polls.txt"
#define polls_lock_file "polls_lock.txt"
#define lb_file "lb.txt"
#define INV_FILE "inv.txt"
#define INV_BASE_FILE "inv_base.txt"
#define COMBO_FILE "combos.txt"
#define POLL_FILE "polls.txt"
#define POLLS_LOCK_FILE "polls_lock.txt"
#define LB_FILE "lb.txt"
#define MAX_BUF_LENGTH 1024
#define MAX_COMBO_LENGTH 1024

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