ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/style.C
Revision: 1.3
Committed: Sun Sep 10 16:06:37 2006 UTC (17 years, 10 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.2: +196 -165 lines
Log Message:
indent

File Contents

# Content
1
2 /*
3 * static char *rcsid_style_c =
4 * "$Id: style.C,v 1.2 2006-08-29 08:01:36 root Exp $";
5 */
6
7 /*
8 CrossFire, A Multiplayer game for X-windows
9
10 Copyright (C) 2002 Mark Wedel & Crossfire Development Team
11 Copyright (C) 1992 Frank Tore Johansen
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26
27 The authors can be reached via e-mail at crossfire-devel@real-time.com
28 */
29
30
31 #include <global.h>
32 #include <random_map.h>
33 #ifndef WIN32 /* ---win32 exclude headers */
34 # include <dirent.h>
35 # include <sys/stat.h>
36 # include <unistd.h>
37 # include "../include/autoconf.h"
38 #endif /* win32 */
39
40
41 static int
42 pointer_strcmp (const void *p1, const void *p2)
43 {
44 const char *s1 = *(const char **) p1;
45 const char *s2 = *(const char **) p2;
46
47 return (strcmp (s1, s2));
48 }
49
50 /* This is our own version of scandir/select_regular_files/sort.
51 * To support having subdirectories in styles, we need to know
52 * if in fact the directory we read is a subdirectory. However,
53 * we can't get that through the normal dirent entry. So
54 * instead, we do our own where we do have the full directory
55 * path so can do stat calls to see if in fact it is a directory.
56 * dir is the name of the director to scan.
57 * namelist is the array of file names returned. IT needs to be
58 * freed by the caller.
59 * skip_dirs controls our behavioru - if nonzero, we don't
60 * skip any subdirectories - if zero, we store those away,
61 * since there are cases where we want to choose a random
62 * directory.
63 */
64 int
65 load_dir (const char *dir, char ***namelist, int skip_dirs)
66 {
67 DIR *dp;
68 struct dirent *d;
69 int entries = 0, entry_size = 0;
70 char name[NAME_MAX + 1], **rn = NULL;
71 struct stat sb;
72
73 dp = opendir (dir);
74 if (dp == NULL)
75 return -1;
76
77 while ((d = readdir (dp)) != NULL)
78 {
79 sprintf (name, "%s/%s", dir, d->d_name);
80 if (skip_dirs)
81 {
82 stat (name, &sb);
83 if (S_ISDIR (sb.st_mode))
84 {
85 continue;
86 }
87 }
88
89 if (entries == entry_size)
90 {
91 entry_size += 10;
92 rn = (char **) realloc (rn, sizeof (char *) * entry_size);
93 }
94 rn[entries] = strdup_local (d->d_name);
95 entries++;
96
97 }
98 (void) closedir (dp);
99
100 qsort (rn, entries, sizeof (char *), pointer_strcmp);
101
102 *namelist = rn;
103 return entries;
104 }
105
106
107
108
109
110 /* this function loads and returns the map requested.
111 * dirname, for example, is "/styles/wallstyles", stylename, is,
112 * for example, "castle", difficulty is -1 when difficulty is
113 * irrelevant to the style. If dirname is given, but stylename
114 * isn't, and difficult is -1, it returns a random style map.
115 * Otherwise, it tries to match the difficulty given with a style
116 * file, named style_name_# where # is an integer
117 */
118
119 /* remove extern, so visible to command_style_map_info function */
120 mapstruct *styles = NULL;
121
122
123 mapstruct *
124 load_style_map (char *style_name)
125 {
126 mapstruct *style_map;
127
128 /* Given a file. See if its in memory */
129 for (style_map = styles; style_map != NULL; style_map = style_map->next)
130 {
131 if (!strcmp (style_name, style_map->path))
132 return style_map;
133 }
134 style_map = load_original_map (style_name, MAP_STYLE);
135 /* Remove it from global list, put it on our local list */
136 if (style_map)
137 {
138 mapstruct *tmp;
139
140 if (style_map == first_map)
141 first_map = style_map->next;
142 else
143 {
144 for (tmp = first_map; tmp && tmp->next != style_map; tmp = tmp->next);
145 if (tmp)
146 tmp->next = style_map->next;
147 }
148 style_map->next = styles;
149 styles = style_map;
150 }
151 return style_map;
152 }
153
154 mapstruct *
155 find_style (const char *dirname, const char *stylename, int difficulty)
156 {
157 char style_file_path[256];
158 char style_file_full_path[256];
159 mapstruct *style_map = NULL;
160 struct stat file_stat;
161 int i, only_subdirs = 0;
162
163 /* if stylename exists, set style_file_path to that file. */
164 if (stylename && strlen (stylename) > 0)
165 sprintf (style_file_path, "%s/%s", dirname, stylename);
166 else /* otherwise, just use the dirname. We'll pick a random stylefile. */
167 sprintf (style_file_path, "%s", dirname);
168
169 /* is what we were given a directory, or a file? */
170 sprintf (style_file_full_path, "%s/maps%s", settings.datadir, style_file_path);
171 if (stat (style_file_full_path, &file_stat) == 0 && !S_ISDIR (file_stat.st_mode))
172 {
173 style_map = load_style_map (style_file_path);
174 }
175 if (style_map == NULL) /* maybe we were given a directory! */
176 {
177 char **namelist;
178 int n;
179 char style_dir_full_path[256];
180
181 /* get the names of all the files in that directory */
182 sprintf (style_dir_full_path, "%s/maps%s", settings.datadir, style_file_path);
183
184 /* First, skip subdirectories. If we don't find anything, then try again
185 * without skipping subdirs.
186 */
187 n = load_dir (style_dir_full_path, &namelist, 1);
188
189 if (n <= 0)
190 {
191 n = load_dir (style_dir_full_path, &namelist, 0);
192 only_subdirs = 1;
193 }
194
195 if (n <= 0)
196 return 0; /* nothing to load. Bye. */
197
198 /* Picks a random map. Note that if this is all directories,
199 * we know it won't be able to load, so save a few ticks.
200 * the door handling checks for this failure and handles
201 * it properly.
202 */
203 if (difficulty == -1)
204 { /* pick a random style from this dir. */
205 if (only_subdirs)
206 style_map = NULL;
207 else
208 {
209 strcat (style_file_path, "/");
210 strcat (style_file_path, namelist[RANDOM () % n]);
211 style_map = load_style_map (style_file_path);
212 }
213 }
214 else
215 { /* find the map closest in difficulty */
216 int min_dist = 32000, min_index = -1;
217
218 for (i = 0; i < n; i++)
219 {
220 int dist;
221 char *mfile_name = strrchr (namelist[i], '_') + 1;
222
223 if ((mfile_name - 1) == NULL)
224 { /* since there isn't a sequence, */
225 int q;
226
227 /*pick one at random to recurse */
228 style_map = find_style (style_file_path, namelist[RANDOM () % n], difficulty);
229 for (q = 0; q < n; q++)
230 free (namelist[q]);
231 free (namelist);
232 return style_map;
233 }
234 else
235 {
236 dist = abs (difficulty - atoi (mfile_name));
237 if (dist < min_dist)
238 {
239 min_dist = dist;
240 min_index = i;
241 }
242 }
243 }
244 /* presumably now we've found the "best" match for the
245 difficulty. */
246 strcat (style_file_path, "/");
247 strcat (style_file_path, namelist[min_index]);
248 style_map = load_style_map (style_file_path);
249 }
250 for (i = 0; i < n; i++)
251 free (namelist[i]);
252 free (namelist);
253 }
254 return style_map;
255
256 }
257
258
259 /* picks a random object from a style map.
260 * Redone by MSW so it should be faster and not use static
261 * variables to generate tables.
262 */
263 object *
264 pick_random_object (mapstruct *style)
265 {
266 int x, y, limit = 0;
267 object *new_obj;
268
269 /* while returning a null object will result in a crash, that
270 * is actually preferable to an infinite loop. That is because
271 * most servers will automatically restart in case of crash.
272 * Change the logic on getting the random space - shouldn't make
273 * any difference, but this seems clearer to me.
274 */
275 do
276 {
277 limit++;
278 x = RANDOM () % MAP_WIDTH (style);
279 y = RANDOM () % MAP_HEIGHT (style);
280 new_obj = get_map_ob (style, x, y);
281 }
282 while (new_obj == NULL && limit < 1000);
283 if (new_obj->head)
284 return new_obj->head;
285 else
286 return new_obj;
287 }
288
289
290 void
291 free_style_maps (void)
292 {
293 mapstruct *next;
294 int style_maps = 0;
295
296 /* delete_map will try to free it from the linked list,
297 * but won't find it, so we need to do it ourselves
298 */
299 while (styles)
300 {
301 next = styles->next;
302 delete_map (styles);
303 styles = next;
304 style_maps++;
305 }
306 LOG (llevDebug, "free_style_maps: Freed %d maps\n", style_maps);
307 }