aboutsummaryrefslogtreecommitdiff
path: root/src/game/game.c
blob: a9ac239c3264f457aa549ef1bffcea70753dba75 (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
#include "game.h"
#include "color.h"
#include "utilities.h"
#include <string.h>

void draw_line(int color, char *text, size_t length)
{
    int width = getmaxx(stdscr);
    int height = getmaxy(stdscr) - 1;
    char footer[width + 1];
    memset(footer, ' ', width);
    footer[width] = 0;
    //Display:
    attron(COLOR_PAIR(color));
    mvprintw(height, 0, footer);
    strncpy(footer, text, length);
    mvprintw(height, 0, footer);
}

void draw_line_info(int steps_taken, int steps_remaining)
{
    char buffer[getmaxx(stdscr) + 1];
    int length = sprintf(buffer, "%d/%d", steps_taken, steps_remaining);
    draw_line(CI_LINE_INFO, buffer, length);
}

void draw_line_win()
{
    char buffer[getmaxx(stdscr) + 1];
    int length = sprintf(buffer, "\u2714 Win! \u263A");
    draw_line(CI_LINE_SUCCESS, buffer, length);
}

int get_command(char prompt, char *command)
{
    int width = getmaxx(stdscr);
    int height = getmaxy(stdscr) - 1;

    char line[width + 1];
    memset(line, ' ', width);
    line[width] = 0;
    attron(COLOR_PAIR(CI_LINE_CMD));
    curs_set(1);

    char *current = command;
    *(current++) = prompt;
    *current = 0;
    while (1)
    {
        mvaddstr(height, 0, line);
        mvaddstr(height, 0, command);
        refresh();
        wchar_t c = wgetch(stdscr);
        if (c == KEY_BACKSPACE)
        {
            current--;
            *current = 0;
            if (current == command)
            {
                curs_set(0);
                return 0;
            }
        }
        else if (c == 10)
        {
            curs_set(0);
            return 1;
        }
        else
        {
            *(current++) = c;
            *current = 0;
        }
    }
}

struct game_state *create_game_state(struct maze *maze)
{
    struct maze_display *display = malloc(sizeof(struct maze_display));

    display->maze_pos.x = 0;
    display->maze_pos.y = 0;
    display->maze = maze;
    display->player_pos = maze->starting_point;
    display->display_player_path = true;
    //Signs
    int signs_length = sizeof_2d(maze->width, maze->height, sizeof(int));
    display->signs = malloc(signs_length);
    memset(display->signs, (int)RS_NONE, signs_length);
    init2d((void **)display->signs, maze->width, maze->height, sizeof(int));
    display->signs[display->player_pos.x][display->player_pos.y] = RS_PATH;

    struct game_state *state = malloc(sizeof(struct game_state));
    state->display = display;

    state->center = false;
    state->win = false;
    state->steps_taken = 0;
    state->new_maze_pos = display->maze_pos;
    state->new_player_pos = display->player_pos;
    return state;
}

struct array
{
    int width;
    int height;
    int *arr;
};