Skip to content
Snippets Groups Projects
Commit a12c1151 authored by Jake Feddersen's avatar Jake Feddersen
Browse files

Print distance maps the way we were given them

parent 0c5483ae
No related branches found
No related tags found
No related merge requests found
......@@ -142,13 +142,13 @@ void write_int(FILE* f, uint32_t val);
*
* Algorithm is modified from Dr. Sheaffer's Dijkstra code using his provided heap.
*/
static void dijkstra_from_player(dungeon *d, int dist[MAP_HEIGHT][MAP_WIDTH], int hardness_limit);
static void dijkstra_from_player(dungeon *d, uint32_t dist[MAP_HEIGHT][MAP_WIDTH], int hardness_limit);
void draw_dist_map(int player_x, int player_y, int dist[MAP_HEIGHT][MAP_WIDTH]);
void draw_dist_map(dungeon *d, uint32_t dist[MAP_HEIGHT][MAP_WIDTH], int hardness_limit);
int main(int argc, char* argv[]) {
int i;
int load = 0;
int save = 0;
......@@ -178,20 +178,19 @@ int main(int argc, char* argv[]) {
draw_map(&d);
int non_tunneling_dist[MAP_HEIGHT][MAP_WIDTH];
int tunneling_dist[MAP_HEIGHT][MAP_WIDTH];
uint32_t non_tunneling_dist[MAP_HEIGHT][MAP_WIDTH];
uint32_t tunneling_dist[MAP_HEIGHT][MAP_WIDTH];
dijkstra_from_player(&d, non_tunneling_dist, 1);
draw_dist_map(d.player_pos.x, d.player_pos.y, non_tunneling_dist);
draw_dist_map(&d, non_tunneling_dist, 0);
dijkstra_from_player(&d, tunneling_dist, 255);
draw_dist_map(d.player_pos.x, d.player_pos.y, tunneling_dist);
draw_dist_map(&d, tunneling_dist, 255);
free_dungeon(&d);
}
static void dijkstra_from_player(dungeon *d, int dist[MAP_HEIGHT][MAP_WIDTH], int hardness_limit)
static void dijkstra_from_player(dungeon *d, uint32_t dist[MAP_HEIGHT][MAP_WIDTH], int hardness_limit)
{
// Initialize the vairables
static dijkstra_path_t path[MAP_HEIGHT][MAP_WIDTH], *p;
......@@ -225,17 +224,13 @@ static void dijkstra_from_player(dungeon *d, int dist[MAP_HEIGHT][MAP_WIDTH], in
// Initialize all of the heap pointers
// Also initialize the distance map
// Impossible-to-tunnel cells (all rock for non-tunneling monsters
// and border for tunneling monsters get -1, all theoretically
// reachable cells get INT_MAX
for (y = 0; y < MAP_HEIGHT; y++) {
for (x = 0; x < MAP_WIDTH; x++) {
dist[y][x] = INT_MAX;
if (d->hardness[y][x] < hardness_limit) {
path[y][x].hn = heap_insert(&h, &path[y][x]);
dist[y][x] = INT_MAX;
} else {
path[y][x].hn = NULL;
dist[y][x] = -1;
}
}
}
......@@ -245,6 +240,11 @@ static void dijkstra_from_player(dungeon *d, int dist[MAP_HEIGHT][MAP_WIDTH], in
// Visit the current node, and set its distance
p->hn = NULL;
dist[p->pos[0]][p->pos[1]] = p->cost;
// If this node is at infinity distance, don't visit its neighbors; it is disconnected
if (p->cost == INT_MAX) {
continue;
}
// Iterate through all neighbors
for (y = p->pos[0] - 1; y < p->pos[0] + 2; y++) {
......@@ -261,18 +261,18 @@ static void dijkstra_from_player(dungeon *d, int dist[MAP_HEIGHT][MAP_WIDTH], in
}
}
void draw_dist_map(int player_x, int player_y, int dist[MAP_HEIGHT][MAP_WIDTH]) {
void draw_dist_map(dungeon *d, uint32_t dist[MAP_HEIGHT][MAP_WIDTH], int hardness_limit) {
int x, y;
for (y = 0; y < MAP_HEIGHT; y++) {
for (x = 0; x < MAP_WIDTH; x++) {
if (y == player_y && x == player_x)
if (y == d->player_pos.y && x == d->player_pos.x) {
printf("@");
else if (dist[y][x] == INT_MAX)
printf("X");
else if (dist[y][x] >= 0)
printf("%d", dist[y][x] % 10);
else
printf(" ");
} else if (d->hardness[y][x] <= hardness_limit) {
if (dist[y][x] == INT_MAX) printf("X");
else printf("%d", dist[y][x] % 10);
} else {
printf(" ");
}
}
printf("\n");
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment