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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
#include "entry.h"
#include "game.h"
#include <stdlib.h>
#include <stdbool.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);
}
}
bool visible(struct maze* maze, struct point player, struct point cell)
{
if (player.x == cell.x && player.y == cell.y)
return true;
else if (player.x == cell.x)
{
int step = player.y - cell.y >= 0 ? -1 : 1;
for (int i = player.y + step; i != cell.y; i += step)
if (maze->map[cell.x][i])
return false;
return true;
}
else if (player.y == cell.y)
{
int step = player.x - cell.x >= 0 ? -1 : 1;
for (int i = player.x + step; i != cell.x; i += step)
if (maze->map[i][cell.y])
return false;
return true;
}
else if (player.y == cell.y - 1)
return visible(maze, player, relative_point(cell, 0, -1));
else if (player.y == cell.y + 1)
return visible(maze, player, relative_point(cell, 0, +1));
else if (player.x == cell.x - 1)
return visible(maze, player, relative_point(cell, -1, 0));
else if (player.x == cell.x + 1)
return visible(maze, player, relative_point(cell, +1, 0));
return false;
}
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;
struct point cell;// A cell of the maze
for (cell.y = 0; cell.y < maze_draw->maze->height; cell.y++)
{
int real_y = maze_draw->maze_pos.y + cell.y;
if (real_y >= 0 && real_y < height)
{
if (maze_draw->maze_pos.x >= 0)
move(real_y, maze_draw->maze_pos.x);
for (cell.x = 0; cell.x < maze_draw->maze->width && cell.y < width; cell.x++)
{
int real_x = maze_draw->maze_pos.x + cell.x;
if (real_x == 0 && cell.x > 0)
{
move(real_y, 0);
}
if (real_x >= 0 && real_x < width)
{
path_sign = maze_draw->signs[cell.x][cell.y];
if (!visible(maze_draw->maze, maze_draw->player_pos, cell) && !(maze_draw->visible))
{
darw_block(&(blocks->hidden));
}
else if (path_sign == RS_NONE || (path_sign == RS_PATH && !maze_draw->display_player_path)) //Maze
{
if (maze_draw->maze->map[cell.x][cell.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));
if (visible(maze_draw->maze, maze_draw->player_pos, maze_draw->maze->starting_point) && maze_draw->visible)
{
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));
}
if (visible(maze_draw->maze, maze_draw->player_pos, maze_draw->maze->end_point) && maze_draw->visible)
{
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(struct block* block)
{
attron(COLOR_PAIR(block->color));
//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, block->chr);
}
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(&(app->conf->blocks.background));
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;
}
|