Crossfire Server, Branches 1.12  R18729
style.c
Go to the documentation of this file.
1 /*
2  * static char *rcsid_style_c =
3  * "$Id: style.c 11578 2009-02-23 22:02:27Z lalo $";
4  */
5 
6 /*
7  CrossFire, A Multiplayer game for X-windows
8 
9  Copyright (C) 2002 Mark Wedel & Crossfire Development Team
10  Copyright (C) 1992 Frank Tore Johansen
11 
12  This program is free software; you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation; either version 2 of the License, or
15  (at your option) any later version.
16 
17  This program is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with this program; if not, write to the Free Software
24  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 
26  The authors can be reached via e-mail at crossfire-devel@real-time.com
27 */
28 
36 #include <global.h>
37 #include <random_map.h>
38 #ifndef WIN32 /* ---win32 exclude headers */
39 #include <dirent.h>
40 #include <sys/stat.h>
41 #include <unistd.h>
42 #include "../include/autoconf.h"
43 #endif /* win32 */
44 
53 static int pointer_strcmp(const void *p1, const void *p2) {
54  const char *s1 = *(const char * const *)p1;
55  const char *s2 = *(const char * const *)p2;
56 
57  return(strcmp(s1, s2));
58 }
59 
83 int load_dir(const char *dir, char ***namelist, int skip_dirs) {
84  DIR *dp;
85  struct dirent *d;
86  int entries = 0, entry_size = 0;
87  char name[NAME_MAX+1], **rn = NULL;
88  struct stat sb;
89 
90  dp = opendir(dir);
91  if (dp == NULL)
92  return -1;
93 
94  while ((d = readdir(dp)) != NULL) {
95  if (skip_dirs) {
96  snprintf(name, sizeof(name), "%s/%s", dir, d->d_name);
97  stat(name, &sb);
98  if (S_ISDIR(sb.st_mode)) {
99  continue;
100  }
101  }
102 
103  if (entries == entry_size) {
104  entry_size += 10;
105  rn = realloc(rn, sizeof(char *)*entry_size);
106  }
107  rn[entries] = strdup_local(d->d_name);
108  entries++;
109 
110  }
111  (void)closedir(dp);
112 
113  qsort(rn, entries, sizeof(char *), pointer_strcmp);
114 
115  *namelist = rn;
116  return entries;
117 }
118 
123 
131 mapstruct *load_style_map(char *style_name) {
132  mapstruct *style_map;
133 
134  /* Given a file. See if its in memory */
135  for (style_map = styles; style_map != NULL; style_map = style_map->next) {
136  if (!strcmp(style_name, style_map->path))
137  return style_map;
138  }
139  style_map = load_original_map(style_name, MAP_STYLE);
140  /* Remove it from global list, put it on our local list */
141  if (style_map) {
142  mapstruct *tmp;
143 
144  if (style_map == first_map)
145  first_map = style_map->next;
146  else {
147  for (tmp = first_map; tmp && tmp->next != style_map; tmp = tmp->next)
148  ;
149  if (tmp)
150  tmp->next = style_map->next;
151  }
152  style_map->next = styles;
153  styles = style_map;
154  }
155  return style_map;
156 }
157 
177 mapstruct *find_style(const char *dirname, const char *stylename, int difficulty) {
178  char style_file_path[256];
179  char style_file_full_path[256];
180  mapstruct *style_map = NULL;
181  struct stat file_stat;
182  int i, only_subdirs = 0;
183 
184  /* if stylename exists, set style_file_path to that file.*/
185  if (stylename && strlen(stylename) > 0)
186  snprintf(style_file_path, sizeof(style_file_path), "%s/%s", dirname, stylename);
187  else /* otherwise, just use the dirname. We'll pick a random stylefile.*/
188  snprintf(style_file_path, sizeof(style_file_path), "%s", dirname);
189 
190  /* is what we were given a directory, or a file? */
191  snprintf(style_file_full_path, sizeof(style_file_full_path), "%s/maps%s", settings.datadir, style_file_path);
192  if (stat(style_file_full_path, &file_stat) == 0
193  && !S_ISDIR(file_stat.st_mode)) {
194  style_map = load_style_map(style_file_path);
195  }
196  if (style_map == NULL) { /* maybe we were given a directory! */
197  char **namelist;
198  int n;
199  char style_dir_full_path[256];
200 
201  /* get the names of all the files in that directory */
202  snprintf(style_dir_full_path, sizeof(style_dir_full_path), "%s/maps%s", settings.datadir, style_file_path);
203 
204  /* First, skip subdirectories. If we don't find anything, then try again
205  * without skipping subdirs.
206  */
207  n = load_dir(style_dir_full_path, &namelist, 1);
208  if (n <= 0) {
209  n = load_dir(style_dir_full_path, &namelist, 0);
210  only_subdirs = 1;
211  }
212 
213  if (n <= 0)
214  return NULL; /* nothing to load. Bye. */
215 
216  /* Picks a random map. Note that if this is all directories,
217  * we know it won't be able to load, so save a few ticks.
218  * the door handling checks for this failure and handles
219  * it properly.
220  */
221  if (difficulty == -1) { /* pick a random style from this dir. */
222  if (only_subdirs)
223  style_map = NULL;
224  else {
225  strncat(style_file_path, "/", sizeof(style_file_path));
226  strncat(style_file_path, namelist[RANDOM()%n], sizeof(style_file_path));
227  style_map = load_style_map(style_file_path);
228  }
229  } else { /* find the map closest in difficulty */
230  int min_dist = 32000, min_index = -1;
231 
232  for (i = 0; i < n; i++) {
233  int dist;
234  char *mfile_name = strrchr(namelist[i], '_')+1;
235 
236  if ((mfile_name-1) == NULL) { /* since there isn't a sequence, */
237  int q;
238 
239  /*pick one at random to recurse */
240  style_map = find_style(style_file_path, namelist[RANDOM()%n], difficulty);
241  for (q = 0; q < n; q++)
242  free(namelist[q]);
243  free(namelist);
244  return style_map;
245  } else {
246  dist = abs(difficulty-atoi(mfile_name));
247  if (dist < min_dist) {
248  min_dist = dist;
249  min_index = i;
250  }
251  }
252  }
253  /* presumably now we've found the "best" match for the
254  difficulty. */
255  strncat(style_file_path, "/", sizeof(style_file_path));
256  strncat(style_file_path, namelist[min_index], sizeof(style_file_path));
257  style_map = load_style_map(style_file_path);
258  }
259  for (i = 0; i < n; i++)
260  free(namelist[i]);
261  free(namelist);
262  }
263  return style_map;
264 }
265 
275 object *pick_random_object(mapstruct *style) {
276  int x, y, limit = 0;
277  object *new_obj;
278 
279  /* while returning a null object will result in a crash, that
280  * is actually preferable to an infinite loop. That is because
281  * most servers will automatically restart in case of crash.
282  * Change the logic on getting the random space - shouldn't make
283  * any difference, but this seems clearer to me.
284  */
285  do {
286  limit++;
287  x = RANDOM()%MAP_WIDTH(style);
288  y = RANDOM()%MAP_HEIGHT(style);
289  new_obj = GET_MAP_OB(style, x, y);
290  } while (new_obj == NULL && limit < 1000);
291  if (new_obj->head)
292  return new_obj->head;
293  else
294  return new_obj;
295 }
296 
300 void free_style_maps(void) {
301  mapstruct *next;
302  int style_maps = 0;
303 
304  /* delete_map will try to free it from the linked list,
305  * but won't find it, so we need to do it ourselves
306  */
307  while (styles) {
308  next = styles->next;
309  delete_map(styles);
310  styles = next;
311  style_maps++;
312  }
313  LOG(llevDebug, "free_style_maps: Freed %d maps\n", style_maps);
314 }
char path[HUGE_BUF]
Definition: map.h:384
#define MAP_STYLE
Definition: map.h:119
#define MAP_HEIGHT(m)
Definition: map.h:99
DIR * opendir(const char *)
Definition: win32.c:78
mapstruct * load_original_map(const char *filename, int flags)
Definition: map.c:1226
Definition: win32.h:128
static int pointer_strcmp(const void *p1, const void *p2)
Definition: style.c:53
Definition: win32.h:138
mapstruct * styles
Definition: style.c:122
mapstruct * find_style(const char *dirname, const char *stylename, int difficulty)
Definition: style.c:177
int load_dir(const char *dir, char ***namelist, int skip_dirs)
Definition: style.c:83
char d_name[_MAX_FNAME+1]
Definition: win32.h:132
char * strdup_local(const char *str)
Definition: porting.c:310
object * pick_random_object(mapstruct *style)
Definition: style.c:275
mapstruct * load_style_map(char *style_name)
Definition: style.c:131
const char * datadir
Definition: global.h:334
int snprintf(char *dest, int max, const char *format,...)
Definition: porting.c:498
#define MAP_WIDTH(m)
Definition: map.h:97
struct Settings settings
Definition: init.c:48
struct dirent * readdir(DIR *)
Definition: win32.c:116
void delete_map(mapstruct *m)
Definition: map.c:1745
#define GET_MAP_OB(M, X, Y)
Definition: map.h:193
int closedir(DIR *)
Definition: win32.c:149
struct obj * head
Definition: object.h:154
#define S_ISDIR(x)
Definition: win32.h:80
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:63
struct mapdef * next
Definition: map.h:347
Definition: map.h:346
EXTERN mapstruct * first_map
Definition: global.h:191
void free_style_maps(void)
Definition: style.c:300
#define NAME_MAX
Definition: define.h:75