30 #include "../include/autoconf.h" 45 const char *s1 = *(
const char *
const *)p1;
46 const char *s2 = *(
const char *
const *)p2;
48 return(strcmp(s1, s2));
74 int load_dir(
const char *dir,
char ***namelist,
int skip_dirs)
78 int entries = 0, entry_size = 0;
79 char name[strlen(dir) +
sizeof(dirent::d_name) + 1], **rn = NULL;
87 while ((d =
readdir(dp)) != NULL) {
89 snprintf(name,
sizeof(name),
"%s/%s", dir, d->d_name);
91 if (S_ISDIR(sb.st_mode)) {
96 if (entries == entry_size) {
98 rn =
static_cast<char **
>(realloc(rn,
sizeof(
char *)*entry_size));
136 for (style_map = styles; style_map != NULL; style_map = style_map->
next) {
137 if (!strcmp(style_name, style_map->
path)) {
182 char style_file_path[256];
183 char style_file_full_path[strlen(
settings.
datadir) + 5 +
sizeof(style_file_path)];
185 struct stat file_stat;
186 int i, only_subdirs = 0;
189 if (strstr(dirname,
"..") != NULL || (stylename != NULL && strstr(stylename,
"..") != NULL))
193 if (stylename && strlen(stylename) > 0) {
194 snprintf(style_file_path,
sizeof(style_file_path),
"%s/%s", dirname, stylename);
196 strlcpy(style_file_path, dirname,
sizeof(style_file_path));
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)) {
205 if (style_map == NULL) {
208 char style_dir_full_path[strlen(
settings.
datadir) + 5 +
sizeof(style_file_path)];
211 snprintf(style_dir_full_path,
sizeof(style_dir_full_path),
"%s/maps%s",
settings.
datadir, style_file_path);
216 n =
load_dir(style_dir_full_path, &namelist, 1);
218 n =
load_dir(style_dir_full_path, &namelist, 0);
231 if (difficulty == -1) {
237 p = strchr(style_file_path,
'\0');
238 snprintf(p, style_file_path+
sizeof(style_file_path)-p,
"/%s", namelist[
RANDOM()%n]);
242 int min_dist = 32000, min_index = -1;
245 for (i = 0; i < n; i++) {
247 char *mfile_name = strrchr(namelist[i],
'_');
248 if (mfile_name == NULL) {
253 for (q = 0; q < n; q++) {
259 dist = abs(difficulty - atoi(mfile_name + 1));
260 if (dist < min_dist) {
268 p = strchr(style_file_path,
'\0');
269 snprintf(p, style_file_path+
sizeof(style_file_path)-p,
"/%s", namelist[min_index]);
272 for (i = 0; i < n; i++) {
305 }
while (new_obj == NULL && limit < 1000);
306 return HEAD(new_obj);
326 LOG(
llevDebug,
"free_style_maps: Freed %d maps\n", style_maps);
int load_dir(const char *dir, char ***namelist, int skip_dirs)
This is our own version of scandir/select_regular_files/sort.
#define MAP_STYLE
Active objects shouldn't be put on active list.
void LOG(LogLevel logLevel, const char *format,...)
Logs a message to stderr, or to file.
Random map related variables.
#define MAP_HEIGHT(m)
Map height.
DIR * opendir(const char *)
object * pick_random_object(mapstruct *style)
Picks a random object from a style map.
Global type definitions and header inclusions.
char path[HUGE_BUF]
Filename of the map.
static int pointer_strcmp(const void *p1, const void *p2)
Char comparison for sorting purposes.
mapstruct * first_map
First map.
struct Settings settings
Global settings.
#define HEAD(op)
Returns the head part of an object.
void delete_map(mapstruct *m)
Frees the map, including the mapstruct.
size_t strlcpy(char *dst, const char *src, size_t size)
Portable implementation of strlcpy(3).
const char * datadir
Read only data files.
mapstruct * styles
Loaded styles maps cache, to avoid having to load all the time.
mapstruct * load_style_map(char *style_name)
Loads specified map (or take it from cache list).
#define MAP_WIDTH(m)
Map width.
Only for debugging purposes.
struct dirent * readdir(DIR *)
mapstruct * find_style(const char *dirname, const char *stylename, int difficulty)
Loads and returns the map requested.
mapstruct * mapfile_load(const char *map, int flags)
Opens the file "filename" and reads information about the map from the given file, and stores it in a newly allocated mapstruct.
#define GET_MAP_OB(M, X, Y)
Gets the bottom object on a map.
void free_style_maps(void)
Frees cached style maps.
mapstruct * next
Next map, linked list.