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.11 by root, Wed Dec 27 13:13:47 2006 UTC vs.
Revision 1.13 by root, Sun Dec 31 10:28:36 2006 UTC

93 93
94 *namelist = rn; 94 *namelist = rn;
95 return entries; 95 return entries;
96} 96}
97 97
98
99
100
101
102/* this function loads and returns the map requested.
103 * dirname, for example, is "/styles/wallstyles", stylename, is,
104 * for example, "castle", difficulty is -1 when difficulty is
105 * irrelevant to the style. If dirname is given, but stylename
106 * isn't, and difficult is -1, it returns a random style map.
107 * Otherwise, it tries to match the difficulty given with a style
108 * file, named style_name_# where # is an integer
109 */
110
111/* remove extern, so visible to command_style_map_info function */
112maptile *styles = NULL;
113
114
115maptile *
116load_style_map (char *style_name)
117{
118 maptile *style_map;
119
120 /* Given a file. See if its in memory */
121 for (style_map = styles; style_map; style_map = style_map->next)
122 if (!strcmp (style_name, style_map->path))
123 return style_map;
124
125 style_map = load_original_map (style_name, MAP_STYLE);
126
127 /* Remove it from global list, put it on our local list */
128 if (style_map)
129 {
130 style_map->unlink ();
131
132 style_map->next = styles;
133 styles = style_map;
134 }
135
136 return style_map;
137}
138
139maptile * 98maptile *
140find_style (const char *dirname, const char *stylename, int difficulty) 99find_style (const char *dirname, const char *stylename, int difficulty)
141{ 100{
142 char style_file_path[256]; 101 char style_file_path[256];
143 char style_file_full_path[256]; 102 char style_file_full_path[256];
152 sprintf (style_file_path, "%s", dirname); 111 sprintf (style_file_path, "%s", dirname);
153 112
154 /* is what we were given a directory, or a file? */ 113 /* is what we were given a directory, or a file? */
155 sprintf (style_file_full_path, "%s/maps%s", settings.datadir, style_file_path); 114 sprintf (style_file_full_path, "%s/maps%s", settings.datadir, style_file_path);
156 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))
157 {
158 style_map = load_style_map (style_file_path); 116 style_map = maptile::load_map_sync (style_file_path);
159 } 117
160 if (style_map == NULL) /* maybe we were given a directory! */ 118 if (!style_map) /* maybe we were given a directory! */
161 { 119 {
162 char **namelist; 120 char **namelist;
163 int n; 121 int n;
164 char style_dir_full_path[256]; 122 char style_dir_full_path[256];
165 123
186 * it properly. 144 * it properly.
187 */ 145 */
188 if (difficulty == -1) 146 if (difficulty == -1)
189 { /* pick a random style from this dir. */ 147 { /* pick a random style from this dir. */
190 if (only_subdirs) 148 if (only_subdirs)
191 style_map = NULL; 149 style_map = 0;
192 else 150 else
193 { 151 {
194 strcat (style_file_path, "/"); 152 strcat (style_file_path, "/");
195 strcat (style_file_path, namelist[RANDOM () % n]); 153 strcat (style_file_path, namelist[RANDOM () % n]);
196 style_map = load_style_map (style_file_path); 154 style_map = maptile::load_map_sync (style_file_path);
197 } 155 }
198 } 156 }
199 else 157 else
200 { /* find the map closest in difficulty */ 158 { /* find the map closest in difficulty */
201 int min_dist = 32000, min_index = -1; 159 int min_dist = 32000, min_index = -1;
228 } 186 }
229 /* presumably now we've found the "best" match for the 187 /* presumably now we've found the "best" match for the
230 difficulty. */ 188 difficulty. */
231 strcat (style_file_path, "/"); 189 strcat (style_file_path, "/");
232 strcat (style_file_path, namelist[min_index]); 190 strcat (style_file_path, namelist[min_index]);
233 style_map = load_style_map (style_file_path); 191 style_map = maptile::load_map_sync (style_file_path);
234 } 192 }
193
235 for (i = 0; i < n; i++) 194 for (i = 0; i < n; i++)
236 free (namelist[i]); 195 free (namelist[i]);
196
237 free (namelist); 197 free (namelist);
238 } 198 }
199
239 return style_map; 200 return style_map;
240 201
241} 202}
242
243 203
244/* picks a random object from a style map. 204/* picks a random object from a style map.
245 * Redone by MSW so it should be faster and not use static 205 * Redone by MSW so it should be faster and not use static
246 * variables to generate tables. 206 * variables to generate tables.
247 */ 207 */
263 x = RANDOM () % style->width; 223 x = RANDOM () % style->width;
264 y = RANDOM () % style->height; 224 y = RANDOM () % style->height;
265 new_obj = GET_MAP_OB (style, x, y); 225 new_obj = GET_MAP_OB (style, x, y);
266 } 226 }
267 while (new_obj == NULL && limit < 1000); 227 while (new_obj == NULL && limit < 1000);
228
268 if (new_obj->head) 229 if (new_obj->head)
269 return new_obj->head; 230 return new_obj->head;
270 else 231 else
271 return new_obj; 232 return new_obj;
272} 233}
273 234
274
275void
276free_style_maps (void)
277{
278 maptile *next;
279 int style_maps = 0;
280
281 /* delete_map will try to free it from the linked list,
282 * but won't find it, so we need to do it ourselves
283 */
284 while (styles)
285 {
286 next = styles->next;
287 delete_map (styles);
288 styles = next;
289 style_maps++;
290 }
291 LOG (llevDebug, "free_style_maps: Freed %d maps\n", style_maps);
292}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines