… | |
… | |
93 | |
93 | |
94 | *namelist = rn; |
94 | *namelist = rn; |
95 | return entries; |
95 | return entries; |
96 | } |
96 | } |
97 | |
97 | |
98 | |
|
|
99 | |
|
|
100 | |
|
|
101 | |
|
|
102 | /* this function loads and returns the map requested. |
|
|
103 | * dirname, for example, is "/styles/wallstyles", stylename, is, |
|
|
104 | * for example, "castle", difficulty is -1 when difficulty is |
|
|
105 | * irrelevant to the style. If dirname is given, but stylename |
|
|
106 | * isn't, and difficult is -1, it returns a random style map. |
|
|
107 | * Otherwise, it tries to match the difficulty given with a style |
|
|
108 | * file, named style_name_# where # is an integer |
|
|
109 | */ |
|
|
110 | |
|
|
111 | maptile * |
|
|
112 | load_style_map (char *style_name) |
|
|
113 | { |
|
|
114 | maptile *style_map = maptile::find_map (style_name, 0); |
|
|
115 | style_map->load (); |
|
|
116 | return style_map; |
|
|
117 | } |
|
|
118 | |
|
|
119 | maptile * |
98 | maptile * |
120 | find_style (const char *dirname, const char *stylename, int difficulty) |
99 | find_style (const char *dirname, const char *stylename, int difficulty) |
121 | { |
100 | { |
122 | char style_file_path[256]; |
101 | char style_file_path[256]; |
123 | char style_file_full_path[256]; |
102 | char style_file_full_path[256]; |
… | |
… | |
132 | sprintf (style_file_path, "%s", dirname); |
111 | sprintf (style_file_path, "%s", dirname); |
133 | |
112 | |
134 | /* is what we were given a directory, or a file? */ |
113 | /* is what we were given a directory, or a file? */ |
135 | sprintf (style_file_full_path, "%s/maps%s", settings.datadir, style_file_path); |
114 | sprintf (style_file_full_path, "%s/maps%s", settings.datadir, style_file_path); |
136 | if (stat (style_file_full_path, &file_stat) == 0 && !S_ISDIR (file_stat.st_mode)) |
115 | if (stat (style_file_full_path, &file_stat) == 0 && !S_ISDIR (file_stat.st_mode)) |
137 | { |
|
|
138 | style_map = load_style_map (style_file_path); |
116 | style_map = maptile::load_map_sync (style_file_path); |
139 | } |
117 | |
140 | if (style_map == NULL) /* maybe we were given a directory! */ |
118 | if (!style_map) /* maybe we were given a directory! */ |
141 | { |
119 | { |
142 | char **namelist; |
120 | char **namelist; |
143 | int n; |
121 | int n; |
144 | char style_dir_full_path[256]; |
122 | char style_dir_full_path[256]; |
145 | |
123 | |
… | |
… | |
166 | * it properly. |
144 | * it properly. |
167 | */ |
145 | */ |
168 | if (difficulty == -1) |
146 | if (difficulty == -1) |
169 | { /* pick a random style from this dir. */ |
147 | { /* pick a random style from this dir. */ |
170 | if (only_subdirs) |
148 | if (only_subdirs) |
171 | style_map = NULL; |
149 | style_map = 0; |
172 | else |
150 | else |
173 | { |
151 | { |
174 | strcat (style_file_path, "/"); |
152 | strcat (style_file_path, "/"); |
175 | strcat (style_file_path, namelist[RANDOM () % n]); |
153 | strcat (style_file_path, namelist[RANDOM () % n]); |
176 | style_map = load_style_map (style_file_path); |
154 | style_map = maptile::load_map_sync (style_file_path); |
177 | } |
155 | } |
178 | } |
156 | } |
179 | else |
157 | else |
180 | { /* find the map closest in difficulty */ |
158 | { /* find the map closest in difficulty */ |
181 | int min_dist = 32000, min_index = -1; |
159 | int min_dist = 32000, min_index = -1; |
… | |
… | |
208 | } |
186 | } |
209 | /* presumably now we've found the "best" match for the |
187 | /* presumably now we've found the "best" match for the |
210 | difficulty. */ |
188 | difficulty. */ |
211 | strcat (style_file_path, "/"); |
189 | strcat (style_file_path, "/"); |
212 | strcat (style_file_path, namelist[min_index]); |
190 | strcat (style_file_path, namelist[min_index]); |
213 | style_map = load_style_map (style_file_path); |
191 | style_map = maptile::load_map_sync (style_file_path); |
214 | } |
192 | } |
|
|
193 | |
215 | for (i = 0; i < n; i++) |
194 | for (i = 0; i < n; i++) |
216 | free (namelist[i]); |
195 | free (namelist[i]); |
|
|
196 | |
217 | free (namelist); |
197 | free (namelist); |
218 | } |
198 | } |
|
|
199 | |
219 | return style_map; |
200 | return style_map; |
220 | |
201 | |
221 | } |
202 | } |
222 | |
203 | |
223 | /* picks a random object from a style map. |
204 | /* picks a random object from a style map. |