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.31 by root, Fri Nov 6 13:03:34 2009 UTC vs.
Revision 1.37 by root, Thu Apr 15 03:34:30 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", "formulae"))
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 goto next_object;
250 } 281 }
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 { 282 }
271 build_stringlist (strchr (cp, ' ') + 1, &formula->arch_name, &formula->arch_names); 283
272 check_recipe (formula); 284next_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 } 285 }
281 286
282 LOG (llevDebug, "done.\n"); 287 LOG (llevDebug, "done.\n");
283 close_and_delete (fp, comp);
284 /* Lastly, lets check for problems in formula we got */ 288 /* Lastly, lets check for problems in formula we got */
285 check_formulae (); 289 check_formulae ();
286} 290}
287 291
288/* Find a treasure with a matching name. The 'depth' parameter is 292/* Find a treasure with a matching name. The 'depth' parameter is
321 } 325 }
322 326
323 return 0; 327 return 0;
324} 328}
325 329
326/* If several archetypes have the same name, the value of the first
327 * one with that name will be returned. This happens for the
328 * mushrooms (mushroom_1, mushroom_2 and mushroom_3). For the
329 * monsters' body parts, there may be several monsters with the same
330 * name. This is not a problem if these monsters have the same level
331 * (e.g. sage & c_sage) or if only one of the monsters generates the
332 * body parts that we are looking for (e.g. big_dragon and
333 * big_dragon_worthless). */
334static long
335find_ingred_cost (const char *name)
336{
337 archetype *at2;
338 artifactlist *al;
339 artifact *art;
340 long mult;
341 char *cp;
342 char part1[100];
343 char part2[100];
344
345 /* same as atoi(), but skip number */
346 mult = 0;
347 while (isdigit (*name))
348 {
349 mult = 10 * mult + (*name - '0');
350 name++;
351 }
352
353 if (mult > 0)
354 name++;
355 else
356 mult = 1;
357
358 /* first, try to match the name of an archetype */
359 for_all_archetypes (at)
360 {
361 if (at->title != NULL)
362 {
363 /* inefficient, but who cares? */
364 sprintf (part1, "%s %s", &at->object::name, &at->title);
365 if (!strcasecmp (part1, name))
366 return mult * at->value;
367 }
368 if (!strcasecmp (at->object::name, name))
369 return mult * at->value;
370 }
371
372 /* second, try to match an artifact ("arch of something") */
373 cp = strstr (name, " of ");
374 if (cp != NULL)
375 {
376 strcpy (part1, name);
377 part1[cp - name] = '\0';
378 strcpy (part2, cp + 4);
379
380 /* find the first archetype matching the first part of the name */
381 for_all_archetypes (at)
382 if (at->object::name.eq_nc (part1) && !at->title)
383 {
384 /* find the first artifact derived from that archetype (same type) */
385 for (al = first_artifactlist; al; al = al->next)
386 if (al->type == at->type)
387 {
388 for (art = al->items; art; art = art->next)
389 if (!strcasecmp (art->item->name, part2))
390 return mult * at->value * art->item->value;
391 }
392 }
393 }
394
395 /* third, try to match a body part ("arch's something") */
396 cp = strstr (name, "'s ");
397 if (cp)
398 {
399 strcpy (part1, name);
400 part1[cp - name] = '\0';
401 strcpy (part2, cp + 3);
402 /* examine all archetypes matching the first part of the name */
403 for_all_archetypes (at)
404 if (at->object::name.eq_nc (part1) && !at->title)
405 {
406 if (at->randomitems)
407 {
408 at2 = find_treasure_by_name (at->randomitems->items, part2, 0);
409 if (at2)
410 return mult * at2->value * isqrt (at->level * 2);
411 }
412 }
413 }
414
415 /* failed to find any matching items -- formula should be checked */
416 return -1;
417}
418
419static const char * 330static const char *
420ingred_name (const char *name) 331ingred_name (const char *name)
421{ 332{
422 const char *cp = name; 333 const char *cp = name;
423 334
472 383
473 return 0; 384 return 0;
474} 385}
475 386
476static recipelist * 387static recipelist *
477get_random_recipelist (void) 388get_random_recipelist ()
478{ 389{
479 recipelist *fl = NULL; 390 recipelist *fl = NULL;
480 int number = 0, roll = 0; 391 int number = 0, roll = 0;
481 392
482 /* first, determine # of recipelist we have */ 393 /* first, determine # of recipelist we have */
525 if (r < 0) 436 if (r < 0)
526 break; 437 break;
527 } 438 }
528 } 439 }
529 return rp; 440 return rp;
530}
531
532static void
533free_all_recipes (void)
534{
535 recipelist *fl = formulalist, *flnext;
536 recipe *formula = NULL, *next;
537 linked_char *lchar, *charnext;
538
539 LOG (llevDebug, "Freeing all the recipes\n");
540 for (fl = formulalist; fl != NULL; fl = flnext)
541 {
542 flnext = fl->next;
543
544 for (formula = fl->items; formula != NULL; formula = next)
545 {
546 next = formula->next;
547
548 free (formula->arch_name[0]);
549 free (formula->arch_name);
550
551 for (lchar = formula->ingred; lchar; lchar = charnext)
552 {
553 charnext = lchar->next;
554 delete lchar;
555 }
556 delete formula;
557 }
558
559 delete fl;
560 }
561} 441}
562 442
563/** 443/**
564 * Split a comma separated string list into words. 444 * Split a comma separated string list into words.
565 * 445 *
579 size_t size; 459 size_t size;
580 size_t i; 460 size_t i;
581 461
582 dup = strdup (str); 462 dup = strdup (str);
583 if (dup == NULL) 463 if (dup == NULL)
584 fatal (OUT_OF_MEMORY); 464 fatal ("out of memory");
585 465
586 size = 0; 466 size = 0;
587 for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ",")) 467 for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ","))
588 size++; 468 size++;
589 469
590 *result_list = (char **) malloc (size * sizeof (*result_list)); 470 *result_list = (char **)malloc (size * sizeof (*result_list));
591 471
592 *result_size = size; 472 *result_size = size;
593 473
594 for (i = 0; i < size; i++) 474 for (i = 0; i < size; i++)
595 { 475 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines