|
|
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 |
… | |
… | |
94 | } |
95 | } |
95 | |
96 | |
96 | maptile * |
97 | maptile * |
97 | find_style (const char *dirname, const char *stylename, int difficulty) |
98 | find_style (const char *dirname, const char *stylename, int difficulty) |
98 | { |
99 | { |
99 | char style_file_path[256]; |
100 | char style_file_path[1024]; |
100 | char style_file_full_path[256]; |
101 | char style_file_full_path[1024]; |
101 | maptile *style_map = NULL; |
102 | maptile *style_map = NULL; |
102 | struct stat file_stat; |
103 | struct stat file_stat; |
103 | int i, only_subdirs = 0; |
104 | int i, only_subdirs = 0; |
104 | |
105 | |
105 | /* if stylename exists, set style_file_path to that file. */ |
106 | /* if stylename exists, set style_file_path to that file. */ |
… | |
… | |
110 | |
111 | |
111 | /* is what we were given a directory, or a file? */ |
112 | /* is what we were given a directory, or a file? */ |
112 | 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); |
113 | |
114 | |
114 | 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)) |
115 | style_map = maptile::load_map_sync (style_file_path); |
116 | style_map = maptile::find_sync (style_file_path); |
116 | |
117 | |
117 | if (!style_map) /* maybe we were given a directory! */ |
118 | if (!style_map) /* maybe we were given a directory! */ |
118 | { |
119 | { |
119 | char **namelist; |
120 | char **namelist; |
120 | int n; |
121 | int n; |
121 | char style_dir_full_path[256]; |
122 | char style_dir_full_path[256]; |
122 | |
123 | |
… | |
… | |
148 | style_map = 0; |
149 | style_map = 0; |
149 | else |
150 | else |
150 | { |
151 | { |
151 | strcat (style_file_path, "/"); |
152 | strcat (style_file_path, "/"); |
152 | strcat (style_file_path, namelist[RANDOM () % n]); |
153 | strcat (style_file_path, namelist[RANDOM () % n]); |
153 | style_map = maptile::load_map_sync (style_file_path); |
154 | style_map = maptile::find_sync (style_file_path); |
154 | } |
155 | } |
155 | } |
156 | } |
156 | else |
157 | else |
157 | { /* find the map closest in difficulty */ |
158 | { /* find the map closest in difficulty */ |
158 | int min_dist = 32000, min_index = -1; |
159 | int min_dist = 32000, min_index = -1; |
… | |
… | |
181 | min_dist = dist; |
182 | min_dist = dist; |
182 | min_index = i; |
183 | min_index = i; |
183 | } |
184 | } |
184 | } |
185 | } |
185 | } |
186 | } |
|
|
187 | |
186 | /* presumably now we've found the "best" match for the |
188 | /* presumably now we've found the "best" match for the |
187 | difficulty. */ |
189 | difficulty. */ |
188 | strcat (style_file_path, "/"); |
190 | strcat (style_file_path, "/"); |
189 | strcat (style_file_path, namelist[min_index]); |
191 | strcat (style_file_path, namelist[min_index]); |
190 | style_map = maptile::load_map_sync (style_file_path); |
192 | style_map = maptile::find_sync (style_file_path); |
191 | } |
193 | } |
192 | |
194 | |
193 | for (i = 0; i < n; i++) |
195 | for (i = 0; i < n; i++) |
194 | free (namelist[i]); |
196 | free (namelist[i]); |
195 | |
197 | |
196 | free (namelist); |
198 | free (namelist); |
|
|
199 | } |
|
|
200 | |
|
|
201 | if (style_map) |
|
|
202 | { |
|
|
203 | style_map->load_sync (); |
|
|
204 | style_map->deactivate (); |
197 | } |
205 | } |
198 | |
206 | |
199 | return style_map; |
207 | return style_map; |
200 | |
208 | |
201 | } |
209 | } |
… | |
… | |
205 | * variables to generate tables. |
213 | * variables to generate tables. |
206 | */ |
214 | */ |
207 | object * |
215 | object * |
208 | pick_random_object (maptile *style) |
216 | pick_random_object (maptile *style) |
209 | { |
217 | { |
210 | int x, y, limit = 0; |
|
|
211 | object *new_obj; |
|
|
212 | |
|
|
213 | /* while returning a null object will result in a crash, that |
218 | /* while returning a null object will result in a crash, that |
214 | * is actually preferable to an infinite loop. That is because |
219 | * is actually preferable to an infinite loop. That is because |
215 | * most servers will automatically restart in case of crash. |
220 | * most servers will automatically restart in case of crash. |
216 | * Change the logic on getting the random space - shouldn't make |
221 | * Change the logic on getting the random space - shouldn't make |
217 | * any difference, but this seems clearer to me. |
222 | * any difference, but this seems clearer to me. |
218 | */ |
223 | */ |
219 | do |
224 | for (int i = 1000; --i;) |
220 | { |
|
|
221 | if (limit++ >= 1000) |
|
|
222 | return create_singularity ("pick_random_object"); |
|
|
223 | |
|
|
224 | x = RANDOM () % style->width; |
|
|
225 | y = RANDOM () % style->height; |
|
|
226 | new_obj = GET_MAP_OB (style, x, y); |
|
|
227 | } |
225 | { |
228 | while (!new_obj); |
226 | object *new_obj = style->at (RANDOM () % style->width, RANDOM () % style->height).bot; |
229 | |
227 | |
|
|
228 | if (new_obj) |
230 | return new_obj->head_ (); |
229 | return new_obj->head_ (); |
231 | } |
230 | } |
232 | |
231 | |
|
|
232 | // instead of crashing in the unlikely case, try to return *something* |
|
|
233 | return get_archetype ("blocked"); |
|
|
234 | } |