| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include "global.h" |
| 22 |
|
| 23 |
#include <stdlib.h> |
| 24 |
#include <string.h> |
| 25 |
|
| 26 |
#ifndef WIN32 |
| 27 |
#include <dirent.h> |
| 28 |
#include <sys/stat.h> |
| 29 |
#include <unistd.h> |
| 30 |
#include "../include/autoconf.h" |
| 31 |
#endif |
| 32 |
|
| 33 |
#include "random_map.h" |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
static int pointer_strcmp(const void *p1, const void *p2) |
| 44 |
{ |
| 45 |
const char *s1 = *(const char * const *)p1; |
| 46 |
const char *s2 = *(const char * const *)p2; |
| 47 |
|
| 48 |
return(strcmp(s1, s2)); |
| 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 |
int load_dir(const char *dir, char ***namelist, int skip_dirs) |
| 75 |
{ |
| 76 |
DIR *dp; |
| 77 |
struct dirent *d; |
| 78 |
int entries = 0, entry_size = 0; |
| 79 |
char name[NAME_MAX+1], **rn = NULL; |
| 80 |
struct stat sb; |
| 81 |
|
| 82 |
dp = opendir(dir); |
| 83 |
if (dp == NULL) { |
| 84 |
return -1; |
| 85 |
} |
| 86 |
|
| 87 |
while ((d = readdir(dp)) != NULL) { |
| 88 |
if (skip_dirs) { |
| 89 |
snprintf(name, sizeof(name), "%s/%s", dir, d->d_name); |
| 90 |
stat(name, &sb); |
| 91 |
if (S_ISDIR(sb.st_mode)) { |
| 92 |
continue; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
if (entries == entry_size) { |
| 97 |
entry_size += 10; |
| 98 |
rn = realloc(rn, sizeof(char *)*entry_size); |
| 99 |
} |
| 100 |
rn[entries] = strdup_local(d->d_name); |
| 101 |
entries++; |
| 102 |
} |
| 103 |
(void)closedir(dp); |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
if (rn == NULL) { |
| 110 |
return 0; |
| 111 |
} |
| 112 |
|
| 113 |
qsort(rn, entries, sizeof(char *), pointer_strcmp); |
| 114 |
|
| 115 |
*namelist = rn; |
| 116 |
return entries; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
mapstruct *styles = NULL; |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
mapstruct *load_style_map(char *style_name) |
| 132 |
{ |
| 133 |
mapstruct *style_map; |
| 134 |
|
| 135 |
|
| 136 |
for (style_map = styles; style_map != NULL; style_map = style_map->next) { |
| 137 |
if (!strcmp(style_name, style_map->path)) { |
| 138 |
return style_map; |
| 139 |
} |
| 140 |
} |
| 141 |
style_map = mapfile_load(style_name, MAP_STYLE); |
| 142 |
|
| 143 |
if (style_map) { |
| 144 |
mapstruct *tmp; |
| 145 |
|
| 146 |
if (style_map == first_map) { |
| 147 |
first_map = style_map->next; |
| 148 |
} else { |
| 149 |
for (tmp = first_map; tmp && tmp->next != style_map; tmp = tmp->next) |
| 150 |
; |
| 151 |
if (tmp) { |
| 152 |
tmp->next = style_map->next; |
| 153 |
} |
| 154 |
} |
| 155 |
style_map->next = styles; |
| 156 |
styles = style_map; |
| 157 |
} |
| 158 |
return style_map; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
mapstruct *find_style(const char *dirname, const char *stylename, int difficulty) |
| 181 |
{ |
| 182 |
char style_file_path[256]; |
| 183 |
char style_file_full_path[256]; |
| 184 |
mapstruct *style_map = NULL; |
| 185 |
struct stat file_stat; |
| 186 |
int i, only_subdirs = 0; |
| 187 |
|
| 188 |
|
| 189 |
if (strstr(dirname, "..") != NULL || (stylename != NULL && strstr(stylename, "..") != NULL)) |
| 190 |
return NULL; |
| 191 |
|
| 192 |
|
| 193 |
if (stylename && strlen(stylename) > 0) { |
| 194 |
snprintf(style_file_path, sizeof(style_file_path), "%s/%s", dirname, stylename); |
| 195 |
} else { |
| 196 |
strlcpy(style_file_path, dirname, sizeof(style_file_path)); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
snprintf(style_file_full_path, sizeof(style_file_full_path), "%s/maps%s", settings.datadir, style_file_path); |
| 201 |
if (stat(style_file_full_path, &file_stat) == 0 |
| 202 |
&& !S_ISDIR(file_stat.st_mode)) { |
| 203 |
style_map = load_style_map(style_file_path); |
| 204 |
} |
| 205 |
if (style_map == NULL) { |
| 206 |
char **namelist; |
| 207 |
int n; |
| 208 |
char style_dir_full_path[256]; |
| 209 |
|
| 210 |
|
| 211 |
snprintf(style_dir_full_path, sizeof(style_dir_full_path), "%s/maps%s", settings.datadir, style_file_path); |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
n = load_dir(style_dir_full_path, &namelist, 1); |
| 217 |
if (n <= 0) { |
| 218 |
n = load_dir(style_dir_full_path, &namelist, 0); |
| 219 |
only_subdirs = 1; |
| 220 |
} |
| 221 |
|
| 222 |
if (n <= 0) { |
| 223 |
return NULL; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
if (difficulty == -1) { |
| 232 |
if (only_subdirs) { |
| 233 |
style_map = NULL; |
| 234 |
} else { |
| 235 |
char *p; |
| 236 |
|
| 237 |
p = strchr(style_file_path, '\0'); |
| 238 |
snprintf(p, style_file_path+sizeof(style_file_path)-p, "/%s", namelist[RANDOM()%n]); |
| 239 |
style_map = load_style_map(style_file_path); |
| 240 |
} |
| 241 |
} else { |
| 242 |
int min_dist = 32000, min_index = -1; |
| 243 |
char *p; |
| 244 |
|
| 245 |
for (i = 0; i < n; i++) { |
| 246 |
int dist; |
| 247 |
char *mfile_name = strrchr(namelist[i], '_'); |
| 248 |
if (mfile_name == NULL) { |
| 249 |
int q; |
| 250 |
|
| 251 |
|
| 252 |
style_map = find_style(style_file_path, namelist[RANDOM()%n], difficulty); |
| 253 |
for (q = 0; q < n; q++) { |
| 254 |
free(namelist[q]); |
| 255 |
} |
| 256 |
free(namelist); |
| 257 |
return style_map; |
| 258 |
} else { |
| 259 |
dist = abs(difficulty - atoi(mfile_name + 1)); |
| 260 |
if (dist < min_dist) { |
| 261 |
min_dist = dist; |
| 262 |
min_index = i; |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
p = strchr(style_file_path, '\0'); |
| 269 |
snprintf(p, style_file_path+sizeof(style_file_path)-p, "/%s", namelist[min_index]); |
| 270 |
style_map = load_style_map(style_file_path); |
| 271 |
} |
| 272 |
for (i = 0; i < n; i++) { |
| 273 |
free(namelist[i]); |
| 274 |
} |
| 275 |
free(namelist); |
| 276 |
} |
| 277 |
return style_map; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
object *pick_random_object(mapstruct *style) |
| 290 |
{ |
| 291 |
int x, y, limit = 0; |
| 292 |
object *new_obj; |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
do { |
| 301 |
limit++; |
| 302 |
x = RANDOM()%MAP_WIDTH(style); |
| 303 |
y = RANDOM()%MAP_HEIGHT(style); |
| 304 |
new_obj = GET_MAP_OB(style, x, y); |
| 305 |
} while (new_obj == NULL && limit < 1000); |
| 306 |
return HEAD(new_obj); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
void free_style_maps(void) |
| 313 |
{ |
| 314 |
mapstruct *next; |
| 315 |
int style_maps = 0; |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
while (styles) { |
| 321 |
next = styles->next; |
| 322 |
delete_map(styles); |
| 323 |
styles = next; |
| 324 |
style_maps++; |
| 325 |
} |
| 326 |
LOG(llevDebug, "free_style_maps: Freed %d maps\n", style_maps); |
| 327 |
} |