-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevelGrid.c
152 lines (138 loc) · 4.57 KB
/
levelGrid.c
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 "levelGrid.h"
Grid* createGrid(int width, int height, char** grid) {
Grid* newGrid = NULL;
/* Allocate Grid memory */
newGrid = (Grid*) malloc(sizeof(Grid));
if (newGrid == NULL)
errorFatal("createGrid newGrid malloc...");
newGrid->data = (char**) malloc(height * sizeof(char*));
if (newGrid->data == NULL)
errorFatal("createGrid newGrid data height malloc...");
for (int i = 0; i < height; i++) {
(newGrid->data)[i] = (char*) malloc(width * sizeof(char));
if ((newGrid->data)[i] == NULL)
errorFatal("createGrid newGrid data width malloc...");
}
/* Fill the new Grid object data */
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
(newGrid->data)[i][j] = grid[i][j];
}
}
newGrid->height = height;
newGrid->width = width;
/* Return the new object pointer */
return newGrid;
}
Grid* loadGrid(const char* fileName) {
Grid* newGrid = NULL;
/* Open grid file for reading */
//printf("Filename: %s\n", fileName);
FILE* inputFile = fopen(fileName, "r");
if (inputFile == NULL)
errorFatal("loadGrid inputFile fopen...");
int lineLength = 200;
char line[lineLength];
int lineNumber = 0;
int tmpWidth;
int tmpHeight;
char** tmpData;
/* Read file lines */
while (fgets(line, lineLength, inputFile) != NULL) {
/* Check file type */
if (lineNumber == 0) {
line[strlen(line)-1] = '\0';
if (strcmp(line, "grid file") != 0)
errorFatal("loadGrid incorectFile...");
}
/* Get grid dimensions */
else if (lineNumber == 1) {
sscanf(line, "%d %d", &tmpHeight, &tmpWidth);
//printf("Width: %d\nHeight: %d\n", tmpWidth, tmpHeight);
/* Create temporary grid data */
tmpData = (char**) malloc(tmpHeight * sizeof(char*));
if (tmpData == NULL)
errorFatal("loadGrid tmpData malloc...");
for (int i = 0; i < tmpHeight; i++) {
tmpData[i] = (char*) malloc(tmpWidth * sizeof(char));
if (tmpData[i] == NULL)
errorFatal("loadGrid tmpData[i] malloc...");
}
}
/* Read lines */
else {
int i = lineNumber - 2;
strncpy(tmpData[i], line, tmpWidth);
}
//printf("%s", line);
lineNumber++;
}
// printf("Read grid:\n");
// for (int i = 0; i < tmpHeight; i++) {
// for (int j = 0; j < tmpWidth; j++)
// printf("%c", tmpData[i][j]);
// printf("\n");
// }
/* Convert to correct grid format */
int emptyCells = 0;
int blockedCells = 0;
int unusedCells = 0;
int unknownCells = 0;
for (int i = 0; i < tmpHeight; i++) {
for (int j = 0; j < tmpWidth; j++) {
if (tmpData[i][j] == ' ') {
tmpData[i][j] = emptyCell;
emptyCells++;
} else if (tmpData[i][j] == '#') {
tmpData[i][j] = blockedCell;
blockedCells++;
} else if (tmpData[i][j] == '.') {
tmpData[i][j] = unusedCell;
unusedCells++;
} else {
tmpData[i][j] = unknownCell;
unknownCells++;
}
}
}
newGrid = createGrid(tmpWidth, tmpHeight, tmpData);
newGrid->emptyCells = emptyCells;
newGrid->blockedCells = blockedCells;
newGrid->unusedCells = unusedCells;
newGrid->unknownCells = unknownCells;
free(tmpData);
// printf("Data in grid:\n");
// for (int i = 0; i < newGrid->height; i++) {
// for (int j = 0; j < newGrid->width; j++)
// printf("%d", (newGrid->data)[i][j]);
// printf("\n");
// }
fclose(inputFile);
return newGrid;
}
// TODO: Create a graph for navigation and path finding
// typedef enum {
// upLeftDirection = 0, upDirection, upRightDirection,
// leftDirection, rightDirection,
// downLeftDirection, downDirection, downRightDirection = 7
// } Direction;
//
// #define UP [i - 1][j]
// #define DOWN [i + 1][j]
// #define LEFT [i][j - 1]
// #define RIGHT [i][j + 1]
// #define UP_LEFT [i - 1][j - 1]
// #define UP_RIGHT [i - 1][j + 1]
// #define DOWN_LEFT [i + 1][j - 1]
// #define DOWN_RIGHT [i + 1][j + 1]
//
// #define UNUSED_CELL -1
// Graph* createGraph(Grid* grid) {
//
// }
// Node* findNode(int y, int x, Graph* graph) {
//
// }
// void findPath(Grid* grid, Graph* graph, int startX, int startY, int targetX, int targetY) {
//
// }