ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/style.C
(Generate patch)

Comparing deliantra/server/random_maps/style.C (file contents):
Revision 1.13 by root, Sun Dec 31 10:28:36 2006 UTC vs.
Revision 1.22 by root, Sat Jan 27 02:19:37 2007 UTC

1/* 1/*
2 CrossFire, A Multiplayer game for X-windows 2 * CrossFire, A Multiplayer game for X-windows
3 3 *
4 * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team
4 Copyright (C) 2002 Mark Wedel & Crossfire Development Team 5 * Copyright (C) 2002 Mark Wedel & Crossfire Development Team
5 Copyright (C) 1992 Frank Tore Johansen 6 * Copyright (C) 1992 Frank Tore Johansen
6 7 *
7 This program is free software; you can redistribute it and/or modify 8 * 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 * 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 * the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version. 11 * (at your option) any later version.
11 12 *
12 This program is distributed in the hope that it will be useful, 13 * This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details. 16 * GNU General Public License for more details.
16 17 *
17 You should have received a copy of the GNU General Public License 18 * 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 * along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 21 *
21 The authors can be reached via e-mail at <crossfire@schmorp.de> 22 * The authors can be reached via e-mail at <crossfire@schmorp.de>
22*/ 23 */
23
24 24
25#include <global.h> 25#include <global.h>
26#include <random_map.h> 26#include <random_map.h>
27#include <dirent.h> 27#include <dirent.h>
28#include <sys/stat.h> 28#include <sys/stat.h>
29#include <unistd.h> 29#include <unistd.h>
30#include "../include/autoconf.h" 30#include "../include/autoconf.h"
31
32 31
33static int 32static int
34pointer_strcmp (const void *p1, const void *p2) 33pointer_strcmp (const void *p1, const void *p2)
35{ 34{
36 const char *s1 = *(const char **) p1; 35 const char *s1 = *(const char **) p1;
96} 95}
97 96
98maptile * 97maptile *
99find_style (const char *dirname, const char *stylename, int difficulty) 98find_style (const char *dirname, const char *stylename, int difficulty)
100{ 99{
101 char style_file_path[256]; 100 char style_file_path[1024];
102 char style_file_full_path[256]; 101 char style_file_full_path[1024];
103 maptile *style_map = NULL; 102 maptile *style_map = NULL;
104 struct stat file_stat; 103 struct stat file_stat;
105 int i, only_subdirs = 0; 104 int i, only_subdirs = 0;
106 105
107 /* if stylename exists, set style_file_path to that file. */ 106 /* if stylename exists, set style_file_path to that file. */
110 else /* otherwise, just use the dirname. We'll pick a random stylefile. */ 109 else /* otherwise, just use the dirname. We'll pick a random stylefile. */
111 sprintf (style_file_path, "%s", dirname); 110 sprintf (style_file_path, "%s", dirname);
112 111
113 /* is what we were given a directory, or a file? */ 112 /* is what we were given a directory, or a file? */
114 sprintf (style_file_full_path, "%s/maps%s", settings.datadir, style_file_path); 113 sprintf (style_file_full_path, "%s/maps%s", settings.datadir, style_file_path);
114
115 if (stat (style_file_full_path, &file_stat) == 0 && !S_ISDIR (file_stat.st_mode)) 115 if (stat (style_file_full_path, &file_stat) == 0 && !S_ISDIR (file_stat.st_mode))
116 style_map = maptile::load_map_sync (style_file_path); 116 style_map = maptile::find_sync (style_file_path);
117 117
118 if (!style_map) /* maybe we were given a directory! */ 118 if (!style_map) /* maybe we were given a directory! */
119 { 119 {
120 char **namelist; 120 char **namelist;
121 int n; 121 int n;
122 char style_dir_full_path[256]; 122 char style_dir_full_path[256];
123 123
148 if (only_subdirs) 148 if (only_subdirs)
149 style_map = 0; 149 style_map = 0;
150 else 150 else
151 { 151 {
152 strcat (style_file_path, "/"); 152 strcat (style_file_path, "/");
153 strcat (style_file_path, namelist[RANDOM () % n]); 153 strcat (style_file_path, namelist[rndm (n)]);
154 style_map = maptile::load_map_sync (style_file_path); 154 style_map = maptile::find_sync (style_file_path);
155 } 155 }
156 } 156 }
157 else 157 else
158 { /* find the map closest in difficulty */ 158 { /* find the map closest in difficulty */
159 int min_dist = 32000, min_index = -1; 159 int min_dist = 32000, min_index = -1;
166 if ((mfile_name - 1) == NULL) 166 if ((mfile_name - 1) == NULL)
167 { /* since there isn't a sequence, */ 167 { /* since there isn't a sequence, */
168 int q; 168 int q;
169 169
170 /*pick one at random to recurse */ 170 /*pick one at random to recurse */
171 style_map = find_style (style_file_path, namelist[RANDOM () % n], difficulty); 171 style_map = find_style (style_file_path, namelist[rndm (n)], difficulty);
172 for (q = 0; q < n; q++) 172 for (q = 0; q < n; q++)
173 free (namelist[q]); 173 free (namelist[q]);
174 free (namelist); 174 free (namelist);
175 return style_map; 175 return style_map;
176 } 176 }
182 min_dist = dist; 182 min_dist = dist;
183 min_index = i; 183 min_index = i;
184 } 184 }
185 } 185 }
186 } 186 }
187
187 /* presumably now we've found the "best" match for the 188 /* presumably now we've found the "best" match for the
188 difficulty. */ 189 difficulty. */
189 strcat (style_file_path, "/"); 190 strcat (style_file_path, "/");
190 strcat (style_file_path, namelist[min_index]); 191 strcat (style_file_path, namelist[min_index]);
191 style_map = maptile::load_map_sync (style_file_path); 192 style_map = maptile::find_sync (style_file_path);
192 } 193 }
193 194
194 for (i = 0; i < n; i++) 195 for (i = 0; i < n; i++)
195 free (namelist[i]); 196 free (namelist[i]);
196 197
197 free (namelist); 198 free (namelist);
199 }
200
201 if (style_map)
202 {
203 style_map->load_sync ();
204 style_map->deactivate ();
198 } 205 }
199 206
200 return style_map; 207 return style_map;
201 208
202} 209}
206 * variables to generate tables. 213 * variables to generate tables.
207 */ 214 */
208object * 215object *
209pick_random_object (maptile *style) 216pick_random_object (maptile *style)
210{ 217{
211 int x, y, limit = 0;
212 object *new_obj;
213
214 /* while returning a null object will result in a crash, that 218 /* while returning a null object will result in a crash, that
215 * is actually preferable to an infinite loop. That is because 219 * is actually preferable to an infinite loop. That is because
216 * most servers will automatically restart in case of crash. 220 * most servers will automatically restart in case of crash.
217 * Change the logic on getting the random space - shouldn't make 221 * Change the logic on getting the random space - shouldn't make
218 * any difference, but this seems clearer to me. 222 * any difference, but this seems clearer to me.
219 */ 223 */
220 do 224 for (int i = 1000; --i;)
221 {
222 limit++;
223 x = RANDOM () % style->width;
224 y = RANDOM () % style->height;
225 new_obj = GET_MAP_OB (style, x, y);
226 } 225 {
227 while (new_obj == NULL && limit < 1000); 226 object *new_obj = style->at (rndm (style->width), rndm (style->height)).bot;
228 227
229 if (new_obj->head) 228 if (new_obj)
230 return new_obj->head; 229 return new_obj->head_ ();
231 else 230 }
232 return new_obj;
233}
234 231
232 // instead of crashing in the unlikely case, try to return *something*
233 return get_archetype ("blocked");
234}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines