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