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

Comparing deliantra/server/common/recipe.C (file contents):
Revision 1.32 by root, Fri Nov 6 13:31:47 2009 UTC vs.
Revision 1.39 by root, Fri Apr 16 07:32:03 2010 UTC

1/* 1/*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4 * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen 6 * Copyright (©) 1992 Frank Tore Johansen
7 * 7 *
8 * Deliantra is free software: you can redistribute it and/or modify it under 8 * Deliantra is free software: you can redistribute it and/or modify it under
9 * the terms of the Affero GNU General Public License as published by the 9 * the terms of the Affero GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your 10 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version. 11 * option) any later version.
50static void build_stringlist (const char *str, char ***result_list, size_t * result_size); 50static void build_stringlist (const char *str, char ***result_list, size_t * result_size);
51 51
52static recipelist *formulalist; 52static recipelist *formulalist;
53 53
54static recipelist * 54static recipelist *
55init_recipelist (void) 55init_recipelist ()
56{ 56{
57 recipelist *tl = new recipelist; 57 recipelist *tl = new recipelist;
58 58
59 tl->total_chance = 0; 59 tl->total_chance = 0;
60 tl->number = 0; 60 tl->number = 0;
63 63
64 return tl; 64 return tl;
65} 65}
66 66
67static recipe * 67static recipe *
68get_empty_formula (void) 68get_empty_formula ()
69{ 69{
70 recipe *t = new recipe; 70 recipe *t = new recipe;
71 71
72 t->chance = 0; 72 t->chance = 0;
73 t->index = 0; 73 t->index = 0;
74 t->transmute = 0; 74 t->transmute = 0;
75 t->yield = 0; 75 t->yield = 0;
76 t->diff = 0; 76 t->diff = 0;
77 t->exp = 0; 77 t->exp = 0;
78 t->keycode = 0; 78 t->keycode = 0;
79 t->title = NULL; 79 t->title = 0;
80 t->arch_names = 0; 80 t->arch_names = 0;
81 t->arch_name = NULL; 81 t->arch_name = 0;
82 t->skill = NULL; 82 t->skill = 0;
83 t->cauldron = NULL; 83 t->cauldron = 0;
84 t->ingred = NULL; 84 t->ingred = 0;
85 t->next = NULL; 85 t->next = 0;
86
86 return t; 87 return t;
87} 88}
88 89
89/* get_formulalist() - returns pointer to the formula list */ 90/* get_formulalist() - returns pointer to the formula list */
90recipelist * 91recipelist *
139 * code, it is possible to have multiples of ingredients in a cauldron 140 * code, it is possible to have multiples of ingredients in a cauldron
140 * which could result in an index formula mismatch. We *don't* check for 141 * which could result in an index formula mismatch. We *don't* check for
141 * that possibility here. -b.t. 142 * that possibility here. -b.t.
142 */ 143 */
143static void 144static void
144check_formulae (void) 145check_formulae ()
145{ 146{
146 recipelist *fl; 147 recipelist *fl;
147 recipe *check, *formula; 148 recipe *check, *formula;
148 int numb = 1; 149 int numb = 1;
149 150
169/* 170/*
170 * init_formulae() - Builds up the lists of formula from the file in 171 * init_formulae() - Builds up the lists of formula from the file in
171 * the libdir. -b.t. 172 * the libdir. -b.t.
172 */ 173 */
173void 174void
174init_formulae (void) 175init_formulae ()
175{ 176{
176 static int has_been_done = 0; 177 static int has_been_done = 0;
177 FILE *fp; 178 FILE *fp;
178 char filename[MAX_BUF], buf[MAX_BUF], *cp, *next; 179 char filename[MAX_BUF], buf[MAX_BUF], *cp, *next;
179 recipe *formula = NULL;
180 recipelist *fl = init_recipelist (); 180 recipelist *fl = init_recipelist ();
181 linked_char *tmp; 181 linked_char *tmp;
182 int value, comp; 182 int value, comp;
183 183
184 if (!formulalist) 184 if (!formulalist)
189 else 189 else
190 has_been_done = 1; 190 has_been_done = 1;
191 191
192 sprintf (filename, "%s/formulae", settings.datadir); 192 sprintf (filename, "%s/formulae", settings.datadir);
193 LOG (llevDebug, "Reading alchemical formulae from %s...\n", filename); 193 LOG (llevDebug, "Reading alchemical formulae from %s...\n", filename);
194 if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL) 194
195 object_thawer thawer (filename);
196
197 if (!thawer)
195 { 198 {
196 LOG (llevError, "Can't open %s.\n", filename); 199 LOG (llevError, "Can't open %s.\n", filename);
197 return; 200 return;
198 } 201 }
199 202
200 while (fgets (buf, MAX_BUF, fp) != NULL) 203 while (thawer.kw)
201 { 204 {
202 if (*buf == '#') 205 if (thawer.kw != KW_object)
203 continue; 206 if (!thawer.parse_error ("formulae file"))
204 if ((cp = strchr (buf, '\n')) != NULL) 207 break;
205 *cp = '\0';
206 cp = buf;
207 while (*cp == ' ') /* Skip blanks */
208 cp++;
209 208
210 if (!strncmp (cp, "object", 6))
211 {
212 formula = get_empty_formula (); 209 recipe *formula = get_empty_formula ();
213 formula->title = strchr (cp, ' ') + 1; 210 thawer.get (formula->title);
211
212 for (;;)
214 } 213 {
215 else if (!strncmp (cp, "keycode", 7)) 214 thawer.next ();
216 formula->keycode = strchr (cp, ' ') + 1;
217 else if (sscanf (cp, "trans %d", &value))
218 formula->transmute = (uint16) value;
219 else if (sscanf (cp, "yield %d", &value))
220 formula->yield = (uint16) value;
221 else if (sscanf (cp, "chance %d", &value))
222 formula->chance = (uint16) value;
223 else if (sscanf (cp, "exp %d", &value))
224 formula->exp = (uint16) value;
225 else if (sscanf (cp, "diff %d", &value))
226 formula->diff = (uint16) value;
227 else if (!strncmp (cp, "ingred", 6))
228 {
229 int numb_ingred = 1;
230 215
231 cp = strchr (cp, ' ') + 1; 216 switch (thawer.kw)
232 do
233 { 217 {
234 if ((next = strchr (cp, ',')) != NULL) 218 case KW_keycode: thawer.get (formula->keycode ); break;
219 case KW_trans: thawer.get (formula->transmute); break;
220 case KW_yield: thawer.get (formula->yield ); break;
221 case KW_chance: thawer.get (formula->chance ); break;
222 case KW_exp: thawer.get (formula->exp ); break;
223 case KW_diff: thawer.get (formula->diff ); break;
224 case KW_skill: thawer.get (formula->skill ); break;
225 case KW_cauldron: thawer.get (formula->cauldron ); break;
226
227 case KW_arch:
235 { 228 {
236 *(next++) = '\0'; 229 build_stringlist (thawer.value_nn, &formula->arch_name, &formula->arch_names);
237 numb_ingred++; 230 check_recipe (formula);
238 } 231 }
232 break;
239 233
234 case KW_ingred:
235 if (thawer.value)
236 {
237 int numb_ingred = 1;
238 char *cp = thawer.value;
239
240 do
241 {
242 if ((next = strchr (cp, ',')))
243 {
244 *next++ = '\0';
245 ++numb_ingred;
246 }
247
240 tmp = new linked_char; 248 tmp = new linked_char;
241 249
242 tmp->name = cp; 250 tmp->name = cp;
243 tmp->next = formula->ingred; 251 tmp->next = formula->ingred;
244 formula->ingred = tmp; 252 formula->ingred = tmp;
245 /* each ingredient's ASCII value is coadded. Later on this 253 /* each ingredient's ASCII value is coadded. Later on this
246 * value will be used allow us to search the formula lists 254 * value will be used allow us to search the formula lists
247 * quickly for the right recipe. 255 * quickly for the right recipe.
248 */ 256 */
249 formula->index += strtoint (cp); 257 formula->index += strtoint (cp);
258 }
259 while ((cp = next));
260
261 /* now find the correct (# of ingred ordered) formulalist */
262 fl = formulalist;
263 while (numb_ingred != 1)
264 {
265 if (!fl->next)
266 fl->next = init_recipelist ();
267
268 fl = fl->next;
269 numb_ingred--;
270 }
271
272 fl->total_chance += formula->chance;
273 fl->number++;
274 formula->next = fl->items;
275 fl->items = formula;
276 }
277 break;
278
279 default:
280 delete formula;
281 case KW_EOF:
282 case KW_object:
283 goto next_object;
250 } 284 }
251 while ((cp = next) != NULL);
252
253 /* now find the correct (# of ingred ordered) formulalist */
254 fl = formulalist;
255 while (numb_ingred != 1)
256 {
257 if (!fl->next)
258 fl->next = init_recipelist ();
259
260 fl = fl->next;
261 numb_ingred--;
262 }
263
264 fl->total_chance += formula->chance;
265 fl->number++;
266 formula->next = fl->items;
267 fl->items = formula;
268 }
269 else if (!strncmp (cp, "arch", 4))
270 { 285 }
271 build_stringlist (strchr (cp, ' ') + 1, &formula->arch_name, &formula->arch_names); 286
272 check_recipe (formula); 287next_object: ;
273 }
274 else if (!strncmp (cp, "skill", 5))
275 formula->skill = strchr (cp, ' ') + 1;
276 else if (!strncmp (cp, "cauldron", 8))
277 formula->cauldron = strchr (cp, ' ') + 1;
278 else
279 LOG (llevError, "Unknown input in file %s: %s\n", filename, buf);
280 } 288 }
281 289
282 LOG (llevDebug, "done.\n"); 290 LOG (llevDebug, "done.\n");
283 close_and_delete (fp, comp);
284 /* Lastly, lets check for problems in formula we got */ 291 /* Lastly, lets check for problems in formula we got */
285 check_formulae (); 292 check_formulae ();
286} 293}
287 294
288/* Find a treasure with a matching name. The 'depth' parameter is 295/* Find a treasure with a matching name. The 'depth' parameter is
379 386
380 return 0; 387 return 0;
381} 388}
382 389
383static recipelist * 390static recipelist *
384get_random_recipelist (void) 391get_random_recipelist ()
385{ 392{
386 recipelist *fl = NULL; 393 recipelist *fl = NULL;
387 int number = 0, roll = 0; 394 int number = 0, roll = 0;
388 395
389 /* first, determine # of recipelist we have */ 396 /* first, determine # of recipelist we have */
455 size_t size; 462 size_t size;
456 size_t i; 463 size_t i;
457 464
458 dup = strdup (str); 465 dup = strdup (str);
459 if (dup == NULL) 466 if (dup == NULL)
460 fatal (OUT_OF_MEMORY); 467 fatal ("out of memory");
461 468
462 size = 0; 469 size = 0;
463 for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ",")) 470 for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ","))
464 size++; 471 size++;
465 472
466 *result_list = (char **) malloc (size * sizeof (*result_list)); 473 *result_list = (char **)malloc (size * sizeof (*result_list));
467 474
468 *result_size = size; 475 *result_size = size;
469 476
470 for (i = 0; i < size; i++) 477 for (i = 0; i < size; i++)
471 { 478 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines