diff options
| author | Sopár Adrián <adrian.sopar@protonmail.com> | 2024-06-20 09:28:14 +0200 |
|---|---|---|
| committer | Sopár Adrián <adrian.sopar@protonmail.com> | 2024-06-20 09:28:14 +0200 |
| commit | 74ea6dc86646cee9915292d73d8c7afef01ef3e0 (patch) | |
| tree | 9a58866f7765dad8ba56f1f40b1fa031e9d2687d /src/game/commands.c | |
First commit. This is mostly the state of the project as I left it around the end of 2019.HEADmaster
Diffstat (limited to 'src/game/commands.c')
| -rw-r--r-- | src/game/commands.c | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/game/commands.c b/src/game/commands.c new file mode 100644 index 0000000..7e0e865 --- /dev/null +++ b/src/game/commands.c @@ -0,0 +1,61 @@ +#include "commands.h" +#include <string.h> + +static struct cmd_res command_switch(struct game_state *state, int argc, char **argv); + +struct cmd_res run_command(struct game_state *state, char *command) +{ + int argc = 0; + char *argv[MAX_ARGS_COUNT]; + char *arg = command; + char *space; + int arg_length; + while ((space = strchr(arg, ' ')) != NULL) + { + if (*arg != ' ') + { + arg_length = space - arg; + argv[argc] = malloc(arg_length + 1); + strncpy(argv[argc], arg, arg_length); + argv[argc][arg_length] = 0; + arg = space + 1; + argc++; + } + } + //Last arg + arg_length = strlen(arg); + argv[argc] = malloc(arg_length + 1); + strncpy(argv[argc], arg, arg_length); + argv[argc][arg_length] = 0; + argc++; + + struct cmd_res response = command_switch(state, argc, argv); + for (int i = 0; i < argc; i++) + free(argv[i]); + return response; +} + +static struct cmd_res nothing(struct game_state *state, int argc, char **argv) +{ + struct cmd_res response; + response.type = CRT_FAIL; + strncpy(response.text, "There is no command you entered!", MAX_CR_TEXT_LENGTH); + return response; +} + +static struct cmd_res quit(struct game_state *state, int argc, char **argv) +{ + state->result = GR_QUIT; + struct cmd_res response; + response.type = CRT_QUIT; + *(response.text) = 0; + return response; +} + +static struct cmd_res command_switch(struct game_state *state, int argc, char **argv) +{ + if (!strcmp("", *argv)) + return nothing(state, argc, argv); + else if (!strcmp("q", *argv)) + return quit(state, argc, argv); +}
\ No newline at end of file |
