Repository navigation
Expand file tree
/
Copy pathmap.c
More file actions
279 lines (266 loc) · 8.9 KB
/
Copy pathmap.c
File metadata and controls
279 lines (266 loc) · 8.9 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//
// Created by flasque on 19/10/2024.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "map.h"
#include "loc.h"
#include "queue.h"
static t_position getBaseStationPosition(t_map map);
static void calculateCosts(t_map map);
static void removeFalseCrevasses(t_map map);
/* definition of local functions */
static t_position getBaseStationPosition(t_map map)
{
t_position pos;
int i = 0;
int found = 0;
while (i < map.y_max && !found)
{
int j = 0;
while (j < map.x_max && !found)
{
if (map.soils[i][j] == BASE_STATION)
{
pos.x = j;
pos.y = i;
found = 1;
}
j++;
}
i++;
}
// if the base station is not found, we exit the program
if (!found)
{
fprintf(stderr, "Error: base station not found in the map\n");
exit(1);
}
return pos;
}
static void removeFalseCrevasses(t_map map)
{
// step 1 : find the minimal cost > 10000 in the costs array where the soil is not a crevasse
int over = 0;
int imin, jmin;
while (!over)
{
int min_cost = COST_UNDEF;
imin = map.y_max;
jmin = map.x_max;
for (int i = 0; i < map.y_max; i++)
{
for (int j = 0; j < map.x_max; j++)
{
if (map.soils[i][j] != CREVASSE && map.costs[i][j] > 10000 && map.costs[i][j] < min_cost)
{
min_cost = map.costs[i][j];
imin = i;
jmin = j;
}
}
}
if (imin < map.y_max && jmin < map.x_max)
{
// step 2 : calculate the costs of the neighbours of the position
t_position pos;
pos.x = jmin;
pos.y = imin;
t_position lp, rp, up, dp;
lp = LEFT(pos);
rp = RIGHT(pos);
up = UP(pos);
dp = DOWN(pos);
int min_neighbour = COST_UNDEF;
if (isValidLocalisation(lp, map.x_max, map.y_max))
{
min_neighbour = (map.costs[lp.y][lp.x] < min_neighbour) ? map.costs[lp.y][lp.x] : min_neighbour;
}
if (isValidLocalisation(rp, map.x_max, map.y_max))
{
min_neighbour = (map.costs[rp.y][rp.x] < min_neighbour) ? map.costs[rp.y][rp.x] : min_neighbour;
}
if (isValidLocalisation(up, map.x_max, map.y_max))
{
min_neighbour = (map.costs[up.y][up.x] < min_neighbour) ? map.costs[up.y][up.x] : min_neighbour;
}
if (isValidLocalisation(dp, map.x_max, map.y_max))
{
min_neighbour = (map.costs[dp.y][dp.x] < min_neighbour) ? map.costs[dp.y][dp.x] : min_neighbour;
}
int self_cost = _soil_cost[map.soils[imin][jmin]];
map.costs[imin][jmin] = (min_neighbour + self_cost < map.costs[imin][jmin]) ? min_neighbour + self_cost : map.costs[imin][jmin];
}
else
{
over = 1;
}
}
}
static void calculateCosts(t_map map)
{
t_position baseStation = getBaseStationPosition(map);
//create a queue to store the positions to visit
t_queue queue = createQueue(map.x_max * map.y_max);
//enqueue the base station
enqueue(&queue, baseStation);
// while the queue is not empty
while (queue.first != queue.last)
{
// dequeue the position
t_position pos = dequeue(&queue);
// get its self cost
int self_cost = _soil_cost[map.soils[pos.y][pos.x]];
// get ts neighbours
t_position lp, rp, up, dp;
lp = LEFT(pos);
rp = RIGHT(pos);
up = UP(pos);
dp = DOWN(pos);
// get the mimimum cost of the neighbours
int min_cost = COST_UNDEF;
if (isValidLocalisation(lp, map.x_max, map.y_max))
{
min_cost = (map.costs[lp.y][lp.x] < min_cost) ? map.costs[lp.y][lp.x] : min_cost;
}
if (isValidLocalisation(rp, map.x_max, map.y_max))
{
min_cost = (map.costs[rp.y][rp.x] < min_cost) ? map.costs[rp.y][rp.x] : min_cost;
}
if (isValidLocalisation(up, map.x_max, map.y_max))
{
min_cost = (map.costs[up.y][up.x] < min_cost) ? map.costs[up.y][up.x] : min_cost;
}
if (isValidLocalisation(dp, map.x_max, map.y_max))
{
min_cost = (map.costs[dp.y][dp.x] < min_cost) ? map.costs[dp.y][dp.x] : min_cost;
}
// the cost of the current position is the minimum cost of the neighbours + 1 or 0 if the soil is a base station
map.costs[pos.y][pos.x] = (map.soils[pos.y][pos.x] == BASE_STATION) ? 0 : min_cost + self_cost;
// enqueue the neighbours if they are not visited yet
if (isValidLocalisation(lp, map.x_max, map.y_max) && map.costs[lp.y][lp.x] == COST_UNDEF)
{
// mark as visited - change the cost to 65534
map.costs[lp.y][lp.x] = COST_UNDEF-1;
enqueue(&queue, lp);
}
if (isValidLocalisation(rp, map.x_max, map.y_max) && map.costs[rp.y][rp.x] == COST_UNDEF)
{
map.costs[rp.y][rp.x] = COST_UNDEF-1;
enqueue(&queue, rp);
}
if (isValidLocalisation(up, map.x_max, map.y_max) && map.costs[up.y][up.x] == COST_UNDEF)
{
map.costs[up.y][up.x] = COST_UNDEF-1;
enqueue(&queue, up);
}
if (isValidLocalisation(dp, map.x_max, map.y_max) && map.costs[dp.y][dp.x] == COST_UNDEF)
{
map.costs[dp.y][dp.x] = COST_UNDEF-1;
enqueue(&queue, dp);
}
}
}
/* definition of exported functions */
t_map createMapFromFile(char *filename)
{
/* rules for the file :
* - the first line contains the number of lines : y dimension (int)
* - the second line contains the number of columns : x dimension (int)
* - the next lines contain the map values (int) separated by spaces : one line = one row
* - the values are the following : 0 = BASE_STATION, 1 = PLAIN, 2 = ERG, 3 = REG, 4 = CREVASSE
*/
t_map map;
int xdim, ydim; // dimensions of the map
char buffer[100]; // buffer for reading the file line by line
FILE *file = fopen(filename,"rt");
if (file == NULL)
{
fprintf(stderr, "Error: cannot open file %s\n", filename);
exit(1);
}
fscanf(file, "%d", &ydim);
fscanf(file, "%d", &xdim);
map.x_max = xdim;
map.y_max = ydim;
map.soils = (t_soil **)malloc(ydim * sizeof(t_soil *));
for (int i = 0; i < ydim; i++)
{
map.soils[i] = (t_soil *)malloc(xdim * sizeof(t_soil));
}
map.costs = (int **)malloc(ydim * sizeof(int *));
for (int i = 0; i < ydim; i++)
{
map.costs[i] = (int *)malloc(xdim * sizeof(int));
}
for (int i = 0; i < ydim; i++)
{
// parse the line to get the values : 0 = BASE_STATION, 1 = PLAIN, 2 = ERG, 3 = REG, 4 = CREVASSE
// values are separated by spaces, so we use sscanf with %d to get the values
for (int j = 0; j < xdim; j++)
{
int value;
fscanf(file, "%d", &value);
map.soils[i][j] = value;
// cost is 0 for BASE_STATION, 65535 for other soils
map.costs[i][j] = (value == BASE_STATION) ? 0 : COST_UNDEF;
}
}
fclose(file);
calculateCosts(map);
removeFalseCrevasses(map);
return map;
}
t_map createTrainingMap()
{
return createMapFromFile("..\\maps\\training.map");
}
void displayMap(t_map map)
{
/** the rules for display are :
* display all soils with 3x3 characters
* characters are : B for base station, '-' for plain, '~' for erg, '^' for reg, ' ' for crevasse
*/
for (int i = 0; i < map.y_max; i++)
{
for (int rep = 0; rep < 3; rep++)
{
for (int j = 0; j < map.x_max; j++)
{
char c[4];
switch (map.soils[i][j])
{
case BASE_STATION:
if (rep == 1)
{
strcpy(c, " B ");
}
else
{
strcpy(c, " ");
}
break;
case PLAIN:
strcpy(c, "---");
break;
case ERG:
strcpy(c, "~~~");
break;
case REG:
strcpy(c, "^^^");
break;
case CREVASSE:
sprintf(c, "%c%c%c", 219, 219, 219);
break;
default:
strcpy(c, "???");
break;
}
printf("%s", c);
}
printf("\n");
}
}
return;
}