… | |
… | |
22 | */ |
22 | */ |
23 | |
23 | |
24 | |
24 | |
25 | #include <global.h> |
25 | #include <global.h> |
26 | #include <random_map.h> |
26 | #include <random_map.h> |
27 | #ifndef WIN32 /* ---win32 exclude headers */ |
|
|
28 | # include <dirent.h> |
27 | #include <dirent.h> |
29 | # include <sys/stat.h> |
28 | #include <sys/stat.h> |
30 | # include <unistd.h> |
29 | #include <unistd.h> |
31 | # include "../include/autoconf.h" |
30 | #include "../include/autoconf.h" |
32 | #endif /* win32 */ |
|
|
33 | |
31 | |
34 | |
32 | |
35 | static int |
33 | static int |
36 | pointer_strcmp (const void *p1, const void *p2) |
34 | pointer_strcmp (const void *p1, const void *p2) |
37 | { |
35 | { |
… | |
… | |
83 | if (entries == entry_size) |
81 | if (entries == entry_size) |
84 | { |
82 | { |
85 | entry_size += 10; |
83 | entry_size += 10; |
86 | rn = (char **) realloc (rn, sizeof (char *) * entry_size); |
84 | rn = (char **) realloc (rn, sizeof (char *) * entry_size); |
87 | } |
85 | } |
88 | rn[entries] = strdup_local (d->d_name); |
86 | rn[entries] = strdup (d->d_name); |
89 | entries++; |
87 | entries++; |
90 | |
88 | |
91 | } |
89 | } |
92 | (void) closedir (dp); |
90 | (void) closedir (dp); |
93 | |
91 | |
… | |
… | |
109 | * Otherwise, it tries to match the difficulty given with a style |
107 | * Otherwise, it tries to match the difficulty given with a style |
110 | * file, named style_name_# where # is an integer |
108 | * file, named style_name_# where # is an integer |
111 | */ |
109 | */ |
112 | |
110 | |
113 | /* remove extern, so visible to command_style_map_info function */ |
111 | /* remove extern, so visible to command_style_map_info function */ |
114 | mapstruct *styles = NULL; |
112 | maptile *styles = NULL; |
115 | |
113 | |
116 | |
114 | |
117 | mapstruct * |
115 | maptile * |
118 | load_style_map (char *style_name) |
116 | load_style_map (char *style_name) |
119 | { |
117 | { |
120 | mapstruct *style_map; |
118 | maptile *style_map; |
121 | |
119 | |
122 | /* Given a file. See if its in memory */ |
120 | /* Given a file. See if its in memory */ |
123 | for (style_map = styles; style_map != NULL; style_map = style_map->next) |
121 | for (style_map = styles; style_map != NULL; style_map = style_map->next) |
124 | { |
122 | { |
125 | if (!strcmp (style_name, style_map->path)) |
123 | if (!strcmp (style_name, style_map->path)) |
… | |
… | |
127 | } |
125 | } |
128 | style_map = load_original_map (style_name, MAP_STYLE); |
126 | style_map = load_original_map (style_name, MAP_STYLE); |
129 | /* Remove it from global list, put it on our local list */ |
127 | /* Remove it from global list, put it on our local list */ |
130 | if (style_map) |
128 | if (style_map) |
131 | { |
129 | { |
132 | mapstruct *tmp; |
130 | maptile *tmp; |
133 | |
131 | |
134 | if (style_map == first_map) |
132 | if (style_map == first_map) |
135 | first_map = style_map->next; |
133 | first_map = style_map->next; |
136 | else |
134 | else |
137 | { |
135 | { |
… | |
… | |
143 | styles = style_map; |
141 | styles = style_map; |
144 | } |
142 | } |
145 | return style_map; |
143 | return style_map; |
146 | } |
144 | } |
147 | |
145 | |
148 | mapstruct * |
146 | maptile * |
149 | find_style (const char *dirname, const char *stylename, int difficulty) |
147 | find_style (const char *dirname, const char *stylename, int difficulty) |
150 | { |
148 | { |
151 | char style_file_path[256]; |
149 | char style_file_path[256]; |
152 | char style_file_full_path[256]; |
150 | char style_file_full_path[256]; |
153 | mapstruct *style_map = NULL; |
151 | maptile *style_map = NULL; |
154 | struct stat file_stat; |
152 | struct stat file_stat; |
155 | int i, only_subdirs = 0; |
153 | int i, only_subdirs = 0; |
156 | |
154 | |
157 | /* if stylename exists, set style_file_path to that file. */ |
155 | /* if stylename exists, set style_file_path to that file. */ |
158 | if (stylename && strlen (stylename) > 0) |
156 | if (stylename && strlen (stylename) > 0) |
… | |
… | |
253 | /* picks a random object from a style map. |
251 | /* picks a random object from a style map. |
254 | * Redone by MSW so it should be faster and not use static |
252 | * Redone by MSW so it should be faster and not use static |
255 | * variables to generate tables. |
253 | * variables to generate tables. |
256 | */ |
254 | */ |
257 | object * |
255 | object * |
258 | pick_random_object (mapstruct *style) |
256 | pick_random_object (maptile *style) |
259 | { |
257 | { |
260 | int x, y, limit = 0; |
258 | int x, y, limit = 0; |
261 | object *new_obj; |
259 | object *new_obj; |
262 | |
260 | |
263 | /* while returning a null object will result in a crash, that |
261 | /* while returning a null object will result in a crash, that |
… | |
… | |
267 | * any difference, but this seems clearer to me. |
265 | * any difference, but this seems clearer to me. |
268 | */ |
266 | */ |
269 | do |
267 | do |
270 | { |
268 | { |
271 | limit++; |
269 | limit++; |
272 | x = RANDOM () % MAP_WIDTH (style); |
270 | x = RANDOM () % style->width; |
273 | y = RANDOM () % MAP_HEIGHT (style); |
271 | y = RANDOM () % style->height; |
274 | new_obj = get_map_ob (style, x, y); |
272 | new_obj = GET_MAP_OB (style, x, y); |
275 | } |
273 | } |
276 | while (new_obj == NULL && limit < 1000); |
274 | while (new_obj == NULL && limit < 1000); |
277 | if (new_obj->head) |
275 | if (new_obj->head) |
278 | return new_obj->head; |
276 | return new_obj->head; |
279 | else |
277 | else |
… | |
… | |
282 | |
280 | |
283 | |
281 | |
284 | void |
282 | void |
285 | free_style_maps (void) |
283 | free_style_maps (void) |
286 | { |
284 | { |
287 | mapstruct *next; |
285 | maptile *next; |
288 | int style_maps = 0; |
286 | int style_maps = 0; |
289 | |
287 | |
290 | /* delete_map will try to free it from the linked list, |
288 | /* delete_map will try to free it from the linked list, |
291 | * but won't find it, so we need to do it ourselves |
289 | * but won't find it, so we need to do it ourselves |
292 | */ |
290 | */ |