aboutsummaryrefslogtreecommitdiff
path: root/src/game/operations.c
diff options
context:
space:
mode:
authorSopár Adrián <adrian.sopar@protonmail.com>2024-06-20 09:28:14 +0200
committerSopár Adrián <adrian.sopar@protonmail.com>2024-06-20 09:28:14 +0200
commit74ea6dc86646cee9915292d73d8c7afef01ef3e0 (patch)
tree9a58866f7765dad8ba56f1f40b1fa031e9d2687d /src/game/operations.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/operations.c')
-rw-r--r--src/game/operations.c144
1 files changed, 144 insertions, 0 deletions
diff --git a/src/game/operations.c b/src/game/operations.c
new file mode 100644
index 0000000..aa9424f
--- /dev/null
+++ b/src/game/operations.c
@@ -0,0 +1,144 @@
+#include "operations.h"
+#include <string.h>
+#include "maze_generator.h"
+#include "maze_solver.h"
+#include "commands.h"
+
+enum op_res move_player(struct game_state *state, int data)
+{
+ if (state->win)
+ return OPR_NONE;
+ enum move move = (enum move)data;
+ if (move == MOVE_UP && is_movable(state->display, 0, -1))
+ {
+ state->new_player_pos.y--;
+ state->steps_taken++;
+ }
+ else if (move == MOVE_DOWN && is_movable(state->display, 0, 1))
+ {
+ state->new_player_pos.y++;
+ state->steps_taken++;
+ }
+ else if (move == MOVE_LEFT && is_movable(state->display, -1, 0))
+ {
+ state->new_player_pos.x--;
+ state->steps_taken++;
+ }
+ else if (move == MOVE_RIGHT && is_movable(state->display, 1, 0))
+ {
+ state->new_player_pos.x++;
+ state->steps_taken++;
+ }
+ return OPR_NONE;
+}
+
+enum op_res move_maze(struct game_state *state, int data)
+{
+ switch ((enum move)data)
+ {
+ case MOVE_UP:
+ state->new_maze_pos.y--;
+ break;
+ case MOVE_DOWN:
+ state->new_maze_pos.y++;
+ break;
+ case MOVE_LEFT:
+ state->new_maze_pos.x--;
+ break;
+ case MOVE_RIGHT:
+ state->new_maze_pos.x++;
+ break;
+ case MOVE_BEGINNING:
+ {
+ state->new_maze_pos.x = 0;
+ state->new_maze_pos.y = 0;
+ }
+ break;
+ }
+ return OPR_NONE;
+}
+
+enum op_res quit(struct game_state *state, int data)
+{
+ state->result = data;
+ return OPR_QUIT;
+}
+
+enum op_res turn_display_switch(struct game_state *state, int data)
+{
+ switch ((enum display)data)
+ {
+ case DISP_PATH:
+ state->display->display_player_path = !(state->display->display_player_path);
+ break;
+ }
+ return OPR_NONE;
+}
+
+enum op_res new_random(struct game_state *state, int data)
+{
+ struct maze_display *display = state->display;
+ generate_maze(display->maze);
+ display->player_pos = display->maze->starting_point;
+ state->new_player_pos = display->player_pos;
+ for (int x = 0; x < display->maze->width; x++)
+ {
+ for (int y = 0; y < display->maze->height; y++)
+ {
+ display->signs[x][y] = RS_NONE;
+ }
+ }
+ state->win = false;
+ state->steps_taken = 0;
+ return OPR_NONE;
+}
+
+enum op_res center(struct game_state *state, int data)
+{
+ state->center = !state->center;
+ if (state->center)
+ state->new_maze_pos = get_player_center(state->display->player_pos);
+ return OPR_NONE;
+}
+
+enum op_res help_by_n_move(struct game_state *state, int data)
+{
+ struct point path[3000];
+ int count = solve_maze(state->display->maze, state->display->player_pos, path);
+ if (count > 0)
+ {
+ state->new_player_pos = path[1];
+ state->steps_taken++;
+ }
+ return OPR_NONE;
+}
+
+enum op_res solve(struct game_state *state, int data)
+{
+ struct point path[3000];
+ int count = solve_maze(state->display->maze, state->display->player_pos, path);
+ for (int i = 0; i < count; i++)
+ {
+ state->display->signs[path[i].x][path[i].y] = RS_SOLVE;
+ }
+ return OPR_NONE;
+}
+
+enum op_res start_command_prompt(struct game_state *state, int data)
+{
+ char command[256];
+ if (get_command((char)data, command))
+ {
+ struct cmd_res response = run_command(state, command + 1);
+ switch (response.type)
+ {
+ case CRT_SUCCESS:
+ break;
+ case CRT_FAIL:
+ break;
+ case CRT_QUIT:
+ return OPR_QUIT;
+ }
+ }
+ return OPR_NONE;
+} \ No newline at end of file