aboutsummaryrefslogtreecommitdiff
path: root/src/game/entry.c
blob: 50cf78cd8ccb9a9d446c09df9d5a637dc17de190 (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
151
152
#include "entry.h"
#include "game.h"
#include <stdlib.h>
#include <string.h>
#include <ncurses.h>
#include "utilities.h"
#include "color.h"
#include "maze_solver.h"

static inline int mvaddwch(int y, int x, wchar_t wch)
{
    wchar_t arr[2];
    arr[0] = wch;
    arr[1] = 0;
    return mvaddwstr(y, x, arr);
}

static inline int addwch(wchar_t wch)
{
    wchar_t arr[2];
    arr[0] = wch;
    arr[1] = 0;
    return addwstr(arr);
}

static void darw_block(struct block *block)
{
    attron(COLOR_PAIR(block->color));
    if (block->bold)
        attron(A_BOLD);
    addwch(block->chr);
    if (block->bold)
        attroff(A_BOLD);
}

static void mv_draw_block(int y, int x, struct block *block)
{
    int width = getmaxx(stdscr);
    int height = getmaxy(stdscr) - 1;
    if (x >= 0 && x < width && y >= 0 && y < height)
    {
        move(y, x);
        darw_block(block);
    }
}

void draw_maze(struct maze_display *maze_draw, struct game_blocks *blocks)
{
    int width = getmaxx(stdscr);
    int height = getmaxy(stdscr) - 1;
    enum signs path_sign;
    for (int y = 0; y < maze_draw->maze->height; y++)
    {
        int real_y = maze_draw->maze_pos.y + y;
        if (real_y >= 0 && real_y < height)
        {
            if (maze_draw->maze_pos.x >= 0)
                move(real_y, maze_draw->maze_pos.x);
            for (int x = 0; x < maze_draw->maze->width && y < width; x++)
            {
                int real_x = maze_draw->maze_pos.x + x;
                if (real_x == 0 && x > 0)
                {
                    move(real_y, 0);
                }
                if (real_x >= 0 && real_x < width)
                {
                    path_sign = maze_draw->signs[x][y];
                    if (path_sign == RS_NONE || (path_sign == RS_PATH && !maze_draw->display_player_path)) //Maze
                    {
                        if (maze_draw->maze->map[x][y])
                            darw_block(&(blocks->wall));
                        else
                            darw_block(&(blocks->road));
                    }
                    else
                    {
                        if (path_sign == RS_PATH)
                            darw_block(&(blocks->path));
                        else if (path_sign == RS_SOLVE)
                            darw_block(&(blocks->solve));
                    }
                }
            }
        }
    }
    //Player
    mv_draw_block(
        maze_draw->maze_pos.y + maze_draw->player_pos.y,
        maze_draw->maze_pos.x + maze_draw->player_pos.x, &(blocks->player));
    mv_draw_block(
        maze_draw->maze_pos.y + maze_draw->maze->starting_point.y,
        maze_draw->maze_pos.x + maze_draw->maze->starting_point.x, &(blocks->start));
    mv_draw_block(
        maze_draw->maze_pos.y + maze_draw->maze->end_point.y,
        maze_draw->maze_pos.x + maze_draw->maze->end_point.x, &(blocks->target));
}

static void own_clear()
{
    attron(COLOR_PAIR(CI_DEFAULT));
    int width = getmaxx(stdscr);
    int height = getmaxy(stdscr) - 1;
    for (int y = 0; y < height; y++)
        for (int x = 0; x < width; x++)
            mvaddch(y, x, ' ');
}

enum game_res game(struct app *app)
{
    curs_set(0);
    noecho();
    struct game_state *state = create_game_state(app->maze);

    while (1)
    {
        if (state->display->player_pos.x != state->new_player_pos.x ||
            state->display->player_pos.y != state->new_player_pos.y) //Does player moved?
        {
            state->display->signs[state->display->player_pos.x][state->display->player_pos.y] = RS_PATH;
            state->display->player_pos = state->new_player_pos;
            if (state->center)
                state->new_maze_pos = get_player_center(state->display->player_pos);
            if (state->display->player_pos.x == state->display->maze->end_point.x &&
                state->display->player_pos.y == state->display->maze->end_point.y) //Win?
                state->win = true;
        }
        state->display->maze_pos = state->new_maze_pos;
        //Draw!
        own_clear();
        draw_maze(state->display, &(app->conf->blocks));
        if (state->win)
            draw_line_win();
        else
            draw_line_info(state->steps_taken, solve_maze(app->maze, state->display->player_pos, NULL));
        wrefresh(stdscr);
        //Get user input!
        wchar_t c = wgetch(stdscr);
        struct binding *last_binding = &(app->conf->bindings[app->conf->count - 1]);
        for (struct binding *binding = app->conf->bindings; binding <= last_binding; binding++)
        {
            if ((*(binding->cname) != 0 && strcmp(keyname(c), binding->cname) == 0) || c == binding->c)
            {
                if (binding->operation(state, binding->param) == OPR_QUIT)
                {
                    return state->result;
                }
            }
        }
    }
    return GR_QUIT;
}