… | |
… | |
18 | along with this program; if not, write to the Free Software |
18 | along with this program; if not, write to the Free Software |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 | |
20 | |
21 | The authors can be reached via e-mail at <crossfire@schmorp.de> |
21 | The authors can be reached via e-mail at <crossfire@schmorp.de> |
22 | */ |
22 | */ |
23 | |
|
|
24 | |
23 | |
25 | #include <global.h> |
24 | #include <global.h> |
26 | #include <random_map.h> |
25 | #include <random_map.h> |
27 | #include <dirent.h> |
26 | #include <dirent.h> |
28 | #include <sys/stat.h> |
27 | #include <sys/stat.h> |
29 | #include <unistd.h> |
28 | #include <unistd.h> |
30 | #include "../include/autoconf.h" |
29 | #include "../include/autoconf.h" |
31 | |
|
|
32 | |
30 | |
33 | static int |
31 | static int |
34 | pointer_strcmp (const void *p1, const void *p2) |
32 | pointer_strcmp (const void *p1, const void *p2) |
35 | { |
33 | { |
36 | const char *s1 = *(const char **) p1; |
34 | const char *s1 = *(const char **) p1; |
… | |
… | |
110 | else /* otherwise, just use the dirname. We'll pick a random stylefile. */ |
108 | else /* otherwise, just use the dirname. We'll pick a random stylefile. */ |
111 | sprintf (style_file_path, "%s", dirname); |
109 | sprintf (style_file_path, "%s", dirname); |
112 | |
110 | |
113 | /* is what we were given a directory, or a file? */ |
111 | /* is what we were given a directory, or a file? */ |
114 | sprintf (style_file_full_path, "%s/maps%s", settings.datadir, style_file_path); |
112 | sprintf (style_file_full_path, "%s/maps%s", settings.datadir, style_file_path); |
|
|
113 | |
115 | if (stat (style_file_full_path, &file_stat) == 0 && !S_ISDIR (file_stat.st_mode)) |
114 | if (stat (style_file_full_path, &file_stat) == 0 && !S_ISDIR (file_stat.st_mode)) |
116 | style_map = maptile::load_map_sync (style_file_path); |
115 | style_map = maptile::load_map_sync (style_file_path); |
117 | |
116 | |
118 | if (!style_map) /* maybe we were given a directory! */ |
117 | if (!style_map) /* maybe we were given a directory! */ |
119 | { |
118 | { |
… | |
… | |
182 | min_dist = dist; |
181 | min_dist = dist; |
183 | min_index = i; |
182 | min_index = i; |
184 | } |
183 | } |
185 | } |
184 | } |
186 | } |
185 | } |
|
|
186 | |
187 | /* presumably now we've found the "best" match for the |
187 | /* presumably now we've found the "best" match for the |
188 | difficulty. */ |
188 | difficulty. */ |
189 | strcat (style_file_path, "/"); |
189 | strcat (style_file_path, "/"); |
190 | strcat (style_file_path, namelist[min_index]); |
190 | strcat (style_file_path, namelist[min_index]); |
191 | style_map = maptile::load_map_sync (style_file_path); |
191 | style_map = maptile::load_map_sync (style_file_path); |
… | |
… | |
194 | for (i = 0; i < n; i++) |
194 | for (i = 0; i < n; i++) |
195 | free (namelist[i]); |
195 | free (namelist[i]); |
196 | |
196 | |
197 | free (namelist); |
197 | free (namelist); |
198 | } |
198 | } |
|
|
199 | |
|
|
200 | if (style_map) |
|
|
201 | style_map->deactivate (); |
199 | |
202 | |
200 | return style_map; |
203 | return style_map; |
201 | |
204 | |
202 | } |
205 | } |
203 | |
206 | |
… | |
… | |
206 | * variables to generate tables. |
209 | * variables to generate tables. |
207 | */ |
210 | */ |
208 | object * |
211 | object * |
209 | pick_random_object (maptile *style) |
212 | pick_random_object (maptile *style) |
210 | { |
213 | { |
211 | int x, y, limit = 0; |
|
|
212 | object *new_obj; |
|
|
213 | |
|
|
214 | /* while returning a null object will result in a crash, that |
214 | /* while returning a null object will result in a crash, that |
215 | * is actually preferable to an infinite loop. That is because |
215 | * is actually preferable to an infinite loop. That is because |
216 | * most servers will automatically restart in case of crash. |
216 | * most servers will automatically restart in case of crash. |
217 | * Change the logic on getting the random space - shouldn't make |
217 | * Change the logic on getting the random space - shouldn't make |
218 | * any difference, but this seems clearer to me. |
218 | * any difference, but this seems clearer to me. |
219 | */ |
219 | */ |
220 | do |
220 | for (int i = 1000; --i; ) |
221 | { |
221 | { |
222 | limit++; |
222 | object *new_obj = style->at (RANDOM () % style->width, RANDOM () % style->height).bot; |
223 | x = RANDOM () % style->width; |
223 | |
224 | y = RANDOM () % style->height; |
224 | if (new_obj) |
225 | new_obj = GET_MAP_OB (style, x, y); |
225 | return new_obj->head_ (); |
226 | } |
226 | } |
227 | while (new_obj == NULL && limit < 1000); |
|
|
228 | |
227 | |
229 | if (new_obj->head) |
228 | return 0; |
230 | return new_obj->head; |
|
|
231 | else |
|
|
232 | return new_obj; |
|
|
233 | } |
229 | } |
234 | |
230 | |