1 |
elmex |
1.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 |
root |
1.4 |
The authors can be reached via e-mail at <crossfire@schmorp.de> |
22 |
elmex |
1.1 |
*/ |
23 |
|
|
|
24 |
|
|
#include <global.h> |
25 |
|
|
#include <random_map.h> |
26 |
pippijn |
1.6 |
#include <dirent.h> |
27 |
|
|
#include <sys/stat.h> |
28 |
|
|
#include <unistd.h> |
29 |
|
|
#include "../include/autoconf.h" |
30 |
elmex |
1.1 |
|
31 |
root |
1.3 |
static int |
32 |
|
|
pointer_strcmp (const void *p1, const void *p2) |
33 |
elmex |
1.1 |
{ |
34 |
root |
1.3 |
const char *s1 = *(const char **) p1; |
35 |
|
|
const char *s2 = *(const char **) p2; |
36 |
elmex |
1.1 |
|
37 |
root |
1.3 |
return (strcmp (s1, s2)); |
38 |
elmex |
1.1 |
} |
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 |
root |
1.3 |
int |
55 |
|
|
load_dir (const char *dir, char ***namelist, int skip_dirs) |
56 |
elmex |
1.1 |
{ |
57 |
root |
1.3 |
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 |
root |
1.2 |
} |
77 |
|
|
} |
78 |
|
|
|
79 |
root |
1.3 |
if (entries == entry_size) |
80 |
|
|
{ |
81 |
|
|
entry_size += 10; |
82 |
|
|
rn = (char **) realloc (rn, sizeof (char *) * entry_size); |
83 |
root |
1.2 |
} |
84 |
root |
1.7 |
rn[entries] = strdup (d->d_name); |
85 |
root |
1.3 |
entries++; |
86 |
elmex |
1.1 |
|
87 |
|
|
} |
88 |
root |
1.3 |
(void) closedir (dp); |
89 |
elmex |
1.1 |
|
90 |
root |
1.3 |
qsort (rn, entries, sizeof (char *), pointer_strcmp); |
91 |
elmex |
1.1 |
|
92 |
root |
1.3 |
*namelist = rn; |
93 |
|
|
return entries; |
94 |
elmex |
1.1 |
} |
95 |
|
|
|
96 |
root |
1.5 |
maptile * |
97 |
root |
1.3 |
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 |
root |
1.5 |
maptile *style_map = NULL; |
102 |
root |
1.3 |
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 |
root |
1.14 |
|
114 |
root |
1.3 |
if (stat (style_file_full_path, &file_stat) == 0 && !S_ISDIR (file_stat.st_mode)) |
115 |
root |
1.13 |
style_map = maptile::load_map_sync (style_file_path); |
116 |
|
|
|
117 |
|
|
if (!style_map) /* maybe we were given a directory! */ |
118 |
elmex |
1.1 |
{ |
119 |
root |
1.3 |
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 |
root |
1.2 |
} |
136 |
|
|
|
137 |
root |
1.3 |
if (n <= 0) |
138 |
|
|
return 0; /* nothing to load. Bye. */ |
139 |
root |
1.2 |
|
140 |
root |
1.3 |
/* 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 |
root |
1.13 |
style_map = 0; |
149 |
root |
1.3 |
else |
150 |
|
|
{ |
151 |
|
|
strcat (style_file_path, "/"); |
152 |
|
|
strcat (style_file_path, namelist[RANDOM () % n]); |
153 |
root |
1.13 |
style_map = maptile::load_map_sync (style_file_path); |
154 |
root |
1.2 |
} |
155 |
|
|
} |
156 |
root |
1.3 |
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 |
root |
1.2 |
} |
184 |
|
|
} |
185 |
|
|
} |
186 |
root |
1.3 |
/* presumably now we've found the "best" match for the |
187 |
|
|
difficulty. */ |
188 |
|
|
strcat (style_file_path, "/"); |
189 |
|
|
strcat (style_file_path, namelist[min_index]); |
190 |
root |
1.13 |
style_map = maptile::load_map_sync (style_file_path); |
191 |
root |
1.2 |
} |
192 |
root |
1.13 |
|
193 |
root |
1.3 |
for (i = 0; i < n; i++) |
194 |
|
|
free (namelist[i]); |
195 |
root |
1.13 |
|
196 |
root |
1.3 |
free (namelist); |
197 |
elmex |
1.1 |
} |
198 |
root |
1.13 |
|
199 |
elmex |
1.1 |
return style_map; |
200 |
|
|
|
201 |
|
|
} |
202 |
|
|
|
203 |
|
|
/* picks a random object from a style map. |
204 |
|
|
* Redone by MSW so it should be faster and not use static |
205 |
|
|
* variables to generate tables. |
206 |
|
|
*/ |
207 |
root |
1.3 |
object * |
208 |
root |
1.5 |
pick_random_object (maptile *style) |
209 |
root |
1.3 |
{ |
210 |
|
|
int x, y, limit = 0; |
211 |
|
|
object *new_obj; |
212 |
|
|
|
213 |
|
|
/* while returning a null object will result in a crash, that |
214 |
|
|
* is actually preferable to an infinite loop. That is because |
215 |
|
|
* most servers will automatically restart in case of crash. |
216 |
|
|
* Change the logic on getting the random space - shouldn't make |
217 |
|
|
* any difference, but this seems clearer to me. |
218 |
|
|
*/ |
219 |
|
|
do |
220 |
|
|
{ |
221 |
root |
1.14 |
if (limit++ >= 1000) |
222 |
|
|
return create_singularity ("pick_random_object"); |
223 |
|
|
|
224 |
root |
1.9 |
x = RANDOM () % style->width; |
225 |
|
|
y = RANDOM () % style->height; |
226 |
root |
1.8 |
new_obj = GET_MAP_OB (style, x, y); |
227 |
root |
1.3 |
} |
228 |
root |
1.14 |
while (!new_obj); |
229 |
root |
1.12 |
|
230 |
root |
1.14 |
return new_obj->head_ (); |
231 |
elmex |
1.1 |
} |
232 |
|
|
|