Debian Codegraph @

random_maps/style.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2013 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /**
15 * @file
16 * Those functions deal with style for random maps.
17 * @todo
18 * couldn't load_dir() be merged with a function in common library?
19 */
20
21 #include "global.h"
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #ifndef WIN32 /* ---win32 exclude headers */
27 #include <dirent.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include "../include/autoconf.h"
31 #endif /* win32 */
32
33 #include "random_map.h"
34
35 /**
36 * Char comparison for sorting purposes.
37 * @param p1
38 * @param p2
39 * pointers to compare.
40 * @return
41 * return of strcmp() on pointed strings.
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 * This is our own version of scandir/select_regular_files/sort.
53 * To support having subdirectories in styles, we need to know
54 * if in fact the directory we read is a subdirectory. However,
55 * we can't get that through the normal dirent entry. So
56 * instead, we do our own where we do have the full directory
57 * path so can do stat calls to see if in fact it is a directory.
58 * dir is the name of the director to scan.
59 * namelist is the array of file names returned. IT needs to be
60 * freed by the caller.
61 * skip_dirs controls our behavioru - if nonzero, we don't
62 * skip any subdirectories - if zero, we store those away,
63 * since there are cases where we want to choose a random
64 * directory.
65 * @param dir
66 * directory to list.
67 * @param[out] namelist
68 * list of files found.
69 * @param skip_dirs
70 * if non 0, don't list directories.
71 * @return
72 * -1 if dir is invalid, number of files else.
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 /* We don't return -1 for this case here as directory isn't invalid as such.
106 * Most likely directory was empty. In theory it could also be due to
107 * realloc failing to allocate.
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 * Loaded styles maps cache, to avoid having to load all the time.
121 */
122 mapstruct *styles = NULL;
123
124 /**
125 * Loads specified map (or take it from cache list).
126 * @param style_name
127 * map to load.
128 * @return
129 * map.
130 */
131 mapstruct *load_style_map(char *style_name)
132 {
133 mapstruct *style_map;
134
135 /* Given a file. See if its in memory */
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 /* Remove it from global list, put it on our local list */
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 * Loads and returns the map requested.
163 * Dirname, for example, is "/styles/wallstyles", stylename, is,
164 * for example, "castle", difficulty is -1 when difficulty is
165 * irrelevant to the style. If dirname is given, but stylename
166 * isn't, and difficult is -1, it returns a random style map.
167 * Otherwise, it tries to match the difficulty given with a style
168 * file, named style_name_# where # is an integer
169 * @param dirname
170 * where to look.
171 * @param stylename
172 * style to use, can be NULL.
173 * @param difficulty
174 * style difficulty.
175 * @return
176 * style, or NULL if none suitable.
177 * @todo
178 * better document.
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 /* Don't allow escaping from the styles directory. */
189 if (strstr(dirname, "..") != NULL || (stylename != NULL && strstr(stylename, "..") != NULL))
190 return NULL;
191
192 /* if stylename exists, set style_file_path to that file.*/
193 if (stylename && strlen(stylename) > 0) {
194 snprintf(style_file_path, sizeof(style_file_path), "%s/%s", dirname, stylename);
195 } else { /* otherwise, just use the dirname. We'll pick a random stylefile.*/
196 strlcpy(style_file_path, dirname, sizeof(style_file_path));
197 }
198
199 /* is what we were given a directory, or a file? */
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) { /* maybe we were given a directory! */
206 char **namelist;
207 int n;
208 char style_dir_full_path[256];
209
210 /* get the names of all the files in that directory */
211 snprintf(style_dir_full_path, sizeof(style_dir_full_path), "%s/maps%s", settings.datadir, style_file_path);
212
213 /* First, skip subdirectories. If we don't find anything, then try again
214 * without skipping subdirs.
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; /* nothing to load. Bye. */
224 }
225
226 /* Picks a random map. Note that if this is all directories,
227 * we know it won't be able to load, so save a few ticks.
228 * the door handling checks for this failure and handles
229 * it properly.
230 */
231 if (difficulty == -1) { /* pick a random style from this dir. */
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 { /* find the map closest in difficulty */
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) { /* since there isn't a sequence, */
249 int q;
250
251 /*pick one at random to recurse */
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 /* presumably now we've found the "best" match for the
267 difficulty. */
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 * Picks a random object from a style map.
282 * Redone by MSW so it should be faster and not use static
283 * variables to generate tables.
284 * @param style
285 * map to pick from.
286 * @return
287 * random object. Can be NULL.
288 */
289 object *pick_random_object(mapstruct *style)
290 {
291 int x, y, limit = 0;
292 object *new_obj;
293
294 /* while returning a null object will result in a crash, that
295 * is actually preferable to an infinite loop. That is because
296 * most servers will automatically restart in case of crash.
297 * Change the logic on getting the random space - shouldn't make
298 * any difference, but this seems clearer to me.
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 * Frees cached style maps.
311 */
312 void free_style_maps(void)
313 {
314 mapstruct *next;
315 int style_maps = 0;
316
317 /* delete_map will try to free it from the linked list,
318 * but won't find it, so we need to do it ourselves
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 }