aboutsummaryrefslogtreecommitdiff
path: root/src/game/operations.c
blob: 7ba33199f2d133b862f97705f70c78fc7acb2ae8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#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 turn_visible(struct game_state *state, int data)
{
    state->display->visible = !state->display->visible;
    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;
}