/
Read.c
168 lines (143 loc) · 3.86 KB
/
Read.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "Map.h"
#include "TileList.h"
#include "Read.h"
int isEmpty(char *line)
{
if(!line)
return 1;
if(line[0] == '\0' || line[0] == '\n')
return 1;
else
return 0;
}
Map *ReadMap(char *filename)
{
if(!filename)
{
printf("Could not read map; null filename. Talk to a MapMerge dev about it, this is an internal error.\n");
return NULL;
}
FILE *mapFile = fopen(filename, "r");
if(!mapFile)
{
printf("Could not read map; invalid filename.\n");
Usage();
return NULL;
}
TileList *tileList = newTileList();
if(!tileList)
{
fclose(mapFile);
return NULL;
}
char buffer[65536]; //Arbitrary size, probably excessive, but far better than insufficient.
char *line;
line = fgets(buffer, sizeof(buffer), mapFile);
while(!isEmpty(line))
{
//printf("Adding %s\n", line);
if(!TileList_AddLine(tileList, line))
{
deleteTileList(tileList);
fclose(mapFile);
return NULL;
}
line = fgets(buffer, sizeof(buffer), mapFile);
}
Map *map = newMap();
if(!map)
{
fclose(mapFile);
return NULL;
}
line = fgets(buffer, sizeof(buffer), mapFile);
int x, y, z;
char c;
while(line)
{
if(line[0] == '(')
{
if(sscanf(line, "(%d,%d,%d) = {%c", &x, &y, &z, &c) != 4 || c != '"')
{
printf("Could not read map; could not read coordinates.\n");
fclose(mapFile);
return NULL;
}
if(x != 1 || y != 1 || z != map->levels + 1)
{
printf("Could not read map; unexpected coordinates.\n");
fclose(mapFile);
return NULL;
}
else
{
x = 0;
y = 0;
z = map->levels;
map->levels++;
map = Map_Resized(map);
if(!map)
{
fclose(mapFile);
return NULL;
}
}
}
else if(isEmpty(line) || strcmp(line, "\"}\n") == 0 || map->levels == 0)
{
}
else
{
int len = strlen(line) - 1;
int width = len/tileList->charsPerTile;
if(map->width == 0)
{
map->width = width;
map->height = 0;
}
else if(map->width != width)
{
printf("Could not read map; width varies.\n");
fclose(mapFile);
return NULL;
}
if(map->levels == 1)
{
map->height++;
map = Map_Resized(map);
if(!map)
{
fclose(mapFile);
return NULL;
}
}
else if(y >= map->height)
{
printf("Could not read map; height varies.\n");
fclose(mapFile);
return NULL;
}
for(x=0; x < width; x++)
{
int i, index = 0;
for(i = 0; i < tileList->charsPerTile; i++)
{
char c = line[tileList->charsPerTile * x + i];
if(c >= 'a' && c <= 'z')
index = index * 52 + c - 'a';
else if(c >= 'A' && c <= 'Z')
index = index * 52 + c - 'A' + 26;
}
Map_SetTile(map, TileList_GetTile(tileList, index), x, y, z);
}
y++;
}
line = fgets(buffer, sizeof(buffer), mapFile);
}
deleteTileList(tileList);
fclose(mapFile);
return map;
}