1 | /* |
1 | /* |
2 | * static char *rcsid_style_c = |
2 | * static char *rcsid_style_c = |
3 | * "$Id: style.C,v 1.1 2006/08/13 17:16:03 elmex Exp $"; |
3 | * "$Id: style.C,v 1.2 2006/08/29 08:01:36 root Exp $"; |
4 | */ |
4 | */ |
5 | |
5 | |
6 | /* |
6 | /* |
7 | CrossFire, A Multiplayer game for X-windows |
7 | CrossFire, A Multiplayer game for X-windows |
8 | |
8 | |
… | |
… | |
67 | char name[NAME_MAX+1], **rn=NULL; |
67 | char name[NAME_MAX+1], **rn=NULL; |
68 | struct stat sb; |
68 | struct stat sb; |
69 | |
69 | |
70 | dp = opendir (dir); |
70 | dp = opendir (dir); |
71 | if (dp == NULL) |
71 | if (dp == NULL) |
72 | return -1; |
72 | return -1; |
73 | |
73 | |
74 | while ((d = readdir (dp)) != NULL) { |
74 | while ((d = readdir (dp)) != NULL) { |
75 | sprintf(name, "%s/%s", dir, d->d_name); |
75 | sprintf(name, "%s/%s", dir, d->d_name); |
76 | if (skip_dirs) { |
76 | if (skip_dirs) { |
77 | stat(name, &sb); |
77 | stat(name, &sb); |
78 | if (S_ISDIR(sb.st_mode)) { |
78 | if (S_ISDIR(sb.st_mode)) { |
79 | continue; |
79 | continue; |
80 | } |
80 | } |
81 | } |
81 | } |
82 | |
82 | |
83 | if (entries == entry_size) { |
83 | if (entries == entry_size) { |
84 | entry_size+=10; |
84 | entry_size+=10; |
85 | rn = (char **) realloc(rn, sizeof(char*) * entry_size); |
85 | rn = (char **) realloc(rn, sizeof(char*) * entry_size); |
86 | } |
86 | } |
87 | rn[entries] = strdup_local(d->d_name); |
87 | rn[entries] = strdup_local(d->d_name); |
88 | entries++; |
88 | entries++; |
89 | |
89 | |
90 | } |
90 | } |
91 | (void) closedir (dp); |
91 | (void) closedir (dp); |
92 | |
92 | |
93 | qsort(rn, entries, sizeof(char*), pointer_strcmp); |
93 | qsort(rn, entries, sizeof(char*), pointer_strcmp); |
… | |
… | |
117 | { |
117 | { |
118 | mapstruct *style_map; |
118 | mapstruct *style_map; |
119 | |
119 | |
120 | /* Given a file. See if its in memory */ |
120 | /* Given a file. See if its in memory */ |
121 | 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) { |
122 | if (!strcmp(style_name, style_map->path)) return style_map; |
122 | if (!strcmp(style_name, style_map->path)) return style_map; |
123 | } |
123 | } |
124 | style_map = load_original_map(style_name,MAP_STYLE); |
124 | style_map = load_original_map(style_name,MAP_STYLE); |
125 | /* Remove it from global list, put it on our local list */ |
125 | /* Remove it from global list, put it on our local list */ |
126 | if (style_map) { |
126 | if (style_map) { |
127 | mapstruct *tmp; |
127 | mapstruct *tmp; |
128 | |
128 | |
129 | if (style_map == first_map) |
129 | if (style_map == first_map) |
130 | first_map = style_map->next; |
130 | first_map = style_map->next; |
131 | else { |
131 | else { |
132 | for (tmp = first_map; tmp && tmp->next != style_map; tmp = tmp->next); |
132 | for (tmp = first_map; tmp && tmp->next != style_map; tmp = tmp->next); |
133 | if(tmp) |
133 | if(tmp) |
134 | tmp->next = style_map->next; |
134 | tmp->next = style_map->next; |
135 | } |
135 | } |
136 | style_map->next = styles; |
136 | style_map->next = styles; |
137 | styles = style_map; |
137 | styles = style_map; |
138 | } |
138 | } |
139 | return style_map; |
139 | return style_map; |
140 | } |
140 | } |
141 | |
141 | |
142 | mapstruct *find_style(const char *dirname,const char *stylename,int difficulty) { |
142 | mapstruct *find_style(const char *dirname,const char *stylename,int difficulty) { |
… | |
… | |
146 | struct stat file_stat; |
146 | struct stat file_stat; |
147 | int i, only_subdirs=0; |
147 | int i, only_subdirs=0; |
148 | |
148 | |
149 | /* if stylename exists, set style_file_path to that file.*/ |
149 | /* if stylename exists, set style_file_path to that file.*/ |
150 | if(stylename && strlen(stylename)>0) |
150 | if(stylename && strlen(stylename)>0) |
151 | sprintf(style_file_path,"%s/%s",dirname,stylename); |
151 | sprintf(style_file_path,"%s/%s",dirname,stylename); |
152 | else /* otherwise, just use the dirname. We'll pick a random stylefile.*/ |
152 | else /* otherwise, just use the dirname. We'll pick a random stylefile.*/ |
153 | sprintf(style_file_path,"%s",dirname); |
153 | sprintf(style_file_path,"%s",dirname); |
154 | |
154 | |
155 | /* is what we were given a directory, or a file? */ |
155 | /* is what we were given a directory, or a file? */ |
156 | sprintf(style_file_full_path,"%s/maps%s",settings.datadir,style_file_path); |
156 | sprintf(style_file_full_path,"%s/maps%s",settings.datadir,style_file_path); |
157 | if (stat(style_file_full_path, &file_stat) == 0 |
157 | if (stat(style_file_full_path, &file_stat) == 0 |
158 | && !S_ISDIR(file_stat.st_mode)) { |
158 | && !S_ISDIR(file_stat.st_mode)) { |
159 | style_map=load_style_map(style_file_path); |
159 | style_map=load_style_map(style_file_path); |
160 | } |
160 | } |
161 | if(style_map == NULL) /* maybe we were given a directory! */ |
161 | if(style_map == NULL) /* maybe we were given a directory! */ |
162 | { |
162 | { |
163 | char **namelist; |
163 | char **namelist; |
164 | int n; |
164 | int n; |
165 | char style_dir_full_path[256]; |
165 | char style_dir_full_path[256]; |
166 | |
166 | |
167 | /* get the names of all the files in that directory */ |
167 | /* get the names of all the files in that directory */ |
168 | sprintf(style_dir_full_path,"%s/maps%s",settings.datadir,style_file_path); |
168 | sprintf(style_dir_full_path,"%s/maps%s",settings.datadir,style_file_path); |
169 | |
169 | |
170 | /* First, skip subdirectories. If we don't find anything, then try again |
170 | /* First, skip subdirectories. If we don't find anything, then try again |
171 | * without skipping subdirs. |
171 | * without skipping subdirs. |
172 | */ |
172 | */ |
173 | n = load_dir(style_dir_full_path, &namelist, 1); |
|
|
174 | |
|
|
175 | if (n<=0) { |
|
|
176 | n = load_dir(style_dir_full_path, &namelist, 0); |
173 | n = load_dir(style_dir_full_path, &namelist, 1); |
|
|
174 | |
|
|
175 | if (n<=0) { |
|
|
176 | n = load_dir(style_dir_full_path, &namelist, 0); |
177 | only_subdirs=1; |
177 | only_subdirs=1; |
178 | } |
178 | } |
179 | |
179 | |
180 | if (n<=0) return 0; /* nothing to load. Bye. */ |
180 | if (n<=0) return 0; /* nothing to load. Bye. */ |
181 | |
181 | |
182 | /* Picks a random map. Note that if this is all directories, |
182 | /* Picks a random map. Note that if this is all directories, |
183 | * we know it won't be able to load, so save a few ticks. |
183 | * we know it won't be able to load, so save a few ticks. |
184 | * the door handling checks for this failure and handles |
184 | * the door handling checks for this failure and handles |
185 | * it properly. |
185 | * it properly. |
186 | */ |
186 | */ |
187 | if(difficulty==-1) { /* pick a random style from this dir. */ |
187 | if(difficulty==-1) { /* pick a random style from this dir. */ |
188 | if (only_subdirs) |
188 | if (only_subdirs) |
189 | style_map=NULL; |
189 | style_map=NULL; |
190 | else { |
190 | else { |
191 | strcat(style_file_path,"/"); |
191 | strcat(style_file_path,"/"); |
192 | strcat(style_file_path,namelist[RANDOM()%n]); |
192 | strcat(style_file_path,namelist[RANDOM()%n]); |
193 | style_map = load_style_map(style_file_path); |
193 | style_map = load_style_map(style_file_path); |
194 | } |
194 | } |
195 | } |
195 | } |
196 | else { /* find the map closest in difficulty */ |
196 | else { /* find the map closest in difficulty */ |
197 | int min_dist=32000,min_index=-1; |
197 | int min_dist=32000,min_index=-1; |
198 | |
198 | |
199 | for(i=0;i<n;i++) { |
199 | for(i=0;i<n;i++) { |
200 | int dist; |
200 | int dist; |
201 | char *mfile_name = strrchr(namelist[i],'_')+1; |
201 | char *mfile_name = strrchr(namelist[i],'_')+1; |
202 | |
202 | |
203 | if((mfile_name-1) == NULL) { /* since there isn't a sequence, */ |
203 | if((mfile_name-1) == NULL) { /* since there isn't a sequence, */ |
204 | int q; |
204 | int q; |
205 | /*pick one at random to recurse */ |
205 | /*pick one at random to recurse */ |
206 | style_map= find_style(style_file_path, |
206 | style_map= find_style(style_file_path, |
207 | namelist[RANDOM()%n],difficulty); |
207 | namelist[RANDOM()%n],difficulty); |
208 | for (q=0; q<n; q++) |
208 | for (q=0; q<n; q++) |
209 | free(namelist[q]); |
209 | free(namelist[q]); |
210 | free(namelist); |
210 | free(namelist); |
211 | return style_map; |
211 | return style_map; |
212 | } else { |
212 | } else { |
213 | dist = abs(difficulty-atoi(mfile_name)); |
213 | dist = abs(difficulty-atoi(mfile_name)); |
214 | if(dist<min_dist) { |
214 | if(dist<min_dist) { |
215 | min_dist = dist; |
215 | min_dist = dist; |
216 | min_index = i; |
216 | min_index = i; |
217 | } |
217 | } |
218 | } |
218 | } |
219 | } |
219 | } |
220 | /* presumably now we've found the "best" match for the |
220 | /* presumably now we've found the "best" match for the |
221 | difficulty. */ |
221 | difficulty. */ |
222 | strcat(style_file_path,"/"); |
222 | strcat(style_file_path,"/"); |
223 | strcat(style_file_path,namelist[min_index]); |
223 | strcat(style_file_path,namelist[min_index]); |
224 | style_map = load_style_map(style_file_path); |
224 | style_map = load_style_map(style_file_path); |
225 | } |
225 | } |
226 | for (i=0; i<n; i++) |
226 | for (i=0; i<n; i++) |
227 | free(namelist[i]); |
227 | free(namelist[i]); |
228 | free(namelist); |
228 | free(namelist); |
229 | } |
229 | } |
230 | return style_map; |
230 | return style_map; |
231 | |
231 | |
232 | } |
232 | } |
233 | |
233 | |
… | |
… | |
245 | * most servers will automatically restart in case of crash. |
245 | * most servers will automatically restart in case of crash. |
246 | * Change the logic on getting the random space - shouldn't make |
246 | * Change the logic on getting the random space - shouldn't make |
247 | * any difference, but this seems clearer to me. |
247 | * any difference, but this seems clearer to me. |
248 | */ |
248 | */ |
249 | do { |
249 | do { |
250 | limit++; |
250 | limit++; |
251 | x = RANDOM() % MAP_WIDTH(style); |
251 | x = RANDOM() % MAP_WIDTH(style); |
252 | y = RANDOM() % MAP_HEIGHT(style); |
252 | y = RANDOM() % MAP_HEIGHT(style); |
253 | new_obj = get_map_ob(style,x,y); |
253 | new_obj = get_map_ob(style,x,y); |
254 | } while (new_obj == NULL && limit<1000); |
254 | } while (new_obj == NULL && limit<1000); |
255 | if (new_obj->head) return new_obj->head; |
255 | if (new_obj->head) return new_obj->head; |
256 | else return new_obj; |
256 | else return new_obj; |
257 | } |
257 | } |
258 | |
258 | |
259 | |
259 | |
260 | void free_style_maps(void) |
260 | void free_style_maps(void) |
261 | { |
261 | { |
262 | mapstruct *next; |
262 | mapstruct *next; |
263 | int style_maps=0; |
263 | int style_maps=0; |
264 | |
264 | |
265 | /* delete_map will try to free it from the linked list, |
265 | /* delete_map will try to free it from the linked list, |
266 | * but won't find it, so we need to do it ourselves |
266 | * but won't find it, so we need to do it ourselves |
267 | */ |
267 | */ |
268 | while (styles) { |
268 | while (styles) { |
269 | next = styles->next; |
269 | next = styles->next; |
270 | delete_map(styles); |
270 | delete_map(styles); |
271 | styles=next; |
271 | styles=next; |
272 | style_maps++; |
272 | style_maps++; |
273 | } |
273 | } |
274 | LOG(llevDebug,"free_style_maps: Freed %d maps\n", style_maps); |
274 | LOG(llevDebug,"free_style_maps: Freed %d maps\n", style_maps); |
275 | } |
275 | } |
276 | |
276 | |