ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/style.C
Revision: 1.15
Committed: Sun Dec 31 18:10:41 2006 UTC (17 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.14: +9 -11 lines
Log Message:
- style maps need very special treatment
- cf has a nonzero chance of crashing in a random map

File Contents

# Content
1 /*
2 CrossFire, A Multiplayer game for X-windows
3
4 Copyright (C) 2002 Mark Wedel & Crossfire Development Team
5 Copyright (C) 1992 Frank Tore Johansen
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 The authors can be reached via e-mail at <crossfire@schmorp.de>
22 */
23
24 #include <global.h>
25 #include <random_map.h>
26 #include <dirent.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include "../include/autoconf.h"
30
31 static int
32 pointer_strcmp (const void *p1, const void *p2)
33 {
34 const char *s1 = *(const char **) p1;
35 const char *s2 = *(const char **) p2;
36
37 return (strcmp (s1, s2));
38 }
39
40 /* This is our own version of scandir/select_regular_files/sort.
41 * To support having subdirectories in styles, we need to know
42 * if in fact the directory we read is a subdirectory. However,
43 * we can't get that through the normal dirent entry. So
44 * instead, we do our own where we do have the full directory
45 * path so can do stat calls to see if in fact it is a directory.
46 * dir is the name of the director to scan.
47 * namelist is the array of file names returned. IT needs to be
48 * freed by the caller.
49 * skip_dirs controls our behavioru - if nonzero, we don't
50 * skip any subdirectories - if zero, we store those away,
51 * since there are cases where we want to choose a random
52 * directory.
53 */
54 int
55 load_dir (const char *dir, char ***namelist, int skip_dirs)
56 {
57 DIR *dp;
58 struct dirent *d;
59 int entries = 0, entry_size = 0;
60 char name[NAME_MAX + 1], **rn = NULL;
61 struct stat sb;
62
63 dp = opendir (dir);
64 if (dp == NULL)
65 return -1;
66
67 while ((d = readdir (dp)) != NULL)
68 {
69 sprintf (name, "%s/%s", dir, d->d_name);
70 if (skip_dirs)
71 {
72 stat (name, &sb);
73 if (S_ISDIR (sb.st_mode))
74 {
75 continue;
76 }
77 }
78
79 if (entries == entry_size)
80 {
81 entry_size += 10;
82 rn = (char **) realloc (rn, sizeof (char *) * entry_size);
83 }
84 rn[entries] = strdup (d->d_name);
85 entries++;
86
87 }
88 (void) closedir (dp);
89
90 qsort (rn, entries, sizeof (char *), pointer_strcmp);
91
92 *namelist = rn;
93 return entries;
94 }
95
96 maptile *
97 find_style (const char *dirname, const char *stylename, int difficulty)
98 {
99 char style_file_path[256];
100 char style_file_full_path[256];
101 maptile *style_map = NULL;
102 struct stat file_stat;
103 int i, only_subdirs = 0;
104
105 /* if stylename exists, set style_file_path to that file. */
106 if (stylename && strlen (stylename) > 0)
107 sprintf (style_file_path, "%s/%s", dirname, stylename);
108 else /* otherwise, just use the dirname. We'll pick a random stylefile. */
109 sprintf (style_file_path, "%s", dirname);
110
111 /* 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
114 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
117 if (!style_map) /* maybe we were given a directory! */
118 {
119 char **namelist;
120 int n;
121 char style_dir_full_path[256];
122
123 /* get the names of all the files in that directory */
124 sprintf (style_dir_full_path, "%s/maps%s", settings.datadir, style_file_path);
125
126 /* First, skip subdirectories. If we don't find anything, then try again
127 * without skipping subdirs.
128 */
129 n = load_dir (style_dir_full_path, &namelist, 1);
130
131 if (n <= 0)
132 {
133 n = load_dir (style_dir_full_path, &namelist, 0);
134 only_subdirs = 1;
135 }
136
137 if (n <= 0)
138 return 0; /* nothing to load. Bye. */
139
140 /* Picks a random map. Note that if this is all directories,
141 * we know it won't be able to load, so save a few ticks.
142 * the door handling checks for this failure and handles
143 * it properly.
144 */
145 if (difficulty == -1)
146 { /* pick a random style from this dir. */
147 if (only_subdirs)
148 style_map = 0;
149 else
150 {
151 strcat (style_file_path, "/");
152 strcat (style_file_path, namelist[RANDOM () % n]);
153 style_map = maptile::load_map_sync (style_file_path);
154 }
155 }
156 else
157 { /* find the map closest in difficulty */
158 int min_dist = 32000, min_index = -1;
159
160 for (i = 0; i < n; i++)
161 {
162 int dist;
163 char *mfile_name = strrchr (namelist[i], '_') + 1;
164
165 if ((mfile_name - 1) == NULL)
166 { /* since there isn't a sequence, */
167 int q;
168
169 /*pick one at random to recurse */
170 style_map = find_style (style_file_path, namelist[RANDOM () % n], difficulty);
171 for (q = 0; q < n; q++)
172 free (namelist[q]);
173 free (namelist);
174 return style_map;
175 }
176 else
177 {
178 dist = abs (difficulty - atoi (mfile_name));
179 if (dist < min_dist)
180 {
181 min_dist = dist;
182 min_index = i;
183 }
184 }
185 }
186
187 /* presumably now we've found the "best" match for the
188 difficulty. */
189 strcat (style_file_path, "/");
190 strcat (style_file_path, namelist[min_index]);
191 style_map = maptile::load_map_sync (style_file_path);
192 }
193
194 for (i = 0; i < n; i++)
195 free (namelist[i]);
196
197 free (namelist);
198 }
199
200 if (style_map)
201 style_map->deactivate ();
202
203 return style_map;
204
205 }
206
207 /* picks a random object from a style map.
208 * Redone by MSW so it should be faster and not use static
209 * variables to generate tables.
210 */
211 object *
212 pick_random_object (maptile *style)
213 {
214 /* while returning a null object will result in a crash, that
215 * is actually preferable to an infinite loop. That is because
216 * most servers will automatically restart in case of crash.
217 * Change the logic on getting the random space - shouldn't make
218 * any difference, but this seems clearer to me.
219 */
220 for (int i = 1000; --i; )
221 {
222 object *new_obj = style->at (RANDOM () % style->width, RANDOM () % style->height).bot;
223
224 if (new_obj)
225 return new_obj->head_ ();
226 }
227
228 return 0;
229 }
230