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.5 by root, Sun Sep 10 16:00:23 2006 UTC vs.
Revision 1.17 by root, Thu Feb 8 03:09:31 2007 UTC

1
2
3/* Basic stuff for use with the alchemy code. Clearly some of this stuff 1/* Basic stuff for use with the alchemy code. Clearly some of this stuff
4 * could go into server/alchemy, but I left it here just in case it proves 2 * could go into server/alchemy, but I left it here just in case it proves
5 * more generally useful. 3 * more generally useful.
6 * 4 *
7 * Nov 1995 - file created by b.t. thomas@astro.psu.edu 5 * Nov 1995 - file created by b.t. thomas@astro.psu.edu
18 * chance 10 16 * chance 10
19 * ingred dust of beholdereye,gem 17 * ingred dust of beholdereye,gem
20 * arch potion_generic 18 * arch potion_generic
21 */ 19 */
22 20
21#include <cctype>
22
23#include <global.h> 23#include <global.h>
24#include <object.h> 24#include <object.h>
25#include <ctype.h>
26 25
27static void build_stringlist (const char *str, char ***result_list, size_t * result_size); 26static void build_stringlist (const char *str, char ***result_list, size_t * result_size);
28 27
29static recipelist *formulalist; 28static recipelist *formulalist;
30 29
89 int result; 88 int result;
90 89
91 result = 1; 90 result = 1;
92 for (i = 0; i < rp->arch_names; i++) 91 for (i = 0; i < rp->arch_names; i++)
93 { 92 {
94 if (find_archetype (rp->arch_name[i]) != NULL) 93 if (archetype::find (rp->arch_name[i]) != NULL)
95 { 94 {
96 artifact *art = locate_recipe_artifact (rp, i); 95 artifact *art = locate_recipe_artifact (rp, i);
97 96
98 if (!art && strcmp (rp->title, "NONE") != 0) 97 if (!art && strcmp (rp->title, "NONE") != 0)
99 { 98 {
100 LOG (llevError, "\nWARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title); 99 LOG (llevError, "WARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title);
101 result = 0; 100 result = 0;
102 } 101 }
103 } 102 }
104 else 103 else
105 { 104 {
106 LOG (llevError, "\nWARNING: Can't find archetype %s for formula %s\n", rp->arch_name[i], &rp->title); 105 LOG (llevError, "WARNING: Can't find archetype %s for formula %s\n", rp->arch_name[i], &rp->title);
107 result = 0; 106 result = 0;
108 } 107 }
109 } 108 }
110 109
111 return result; 110 return result;
135 return; 134 return;
136 else 135 else
137 has_been_done = 1; 136 has_been_done = 1;
138 137
139 sprintf (filename, "%s/formulae", settings.datadir); 138 sprintf (filename, "%s/formulae", settings.datadir);
140 LOG (llevDebug, "Reading alchemical formulae from %s...", filename); 139 LOG (llevDebug, "Reading alchemical formulae from %s...\n", filename);
141 if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL) 140 if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL)
142 { 141 {
143 LOG (llevError, "Can't open %s.\n", filename); 142 LOG (llevError, "Can't open %s.\n", filename);
144 return; 143 return;
145 } 144 }
152 *cp = '\0'; 151 *cp = '\0';
153 cp = buf; 152 cp = buf;
154 while (*cp == ' ') /* Skip blanks */ 153 while (*cp == ' ') /* Skip blanks */
155 cp++; 154 cp++;
156 155
157 if (!strncmp (cp, "Object", 6)) 156 if (!strncmp (cp, "Object", 6) || !strncmp (cp, "object", 6))
158 { 157 {
159 formula = get_empty_formula (); 158 formula = get_empty_formula ();
160 formula->title = strchr (cp, ' ') + 1; 159 formula->title = strchr (cp, ' ') + 1;
161 } 160 }
162 else if (!strncmp (cp, "keycode", 7)) 161 else if (!strncmp (cp, "keycode", 7))
255{ 254{
256 recipelist *fl; 255 recipelist *fl;
257 recipe *check, *formula; 256 recipe *check, *formula;
258 int numb = 1; 257 int numb = 1;
259 258
260 LOG (llevDebug, "Checking formulae lists..."); 259 LOG (llevDebug, "Checking formulae lists...\n");
261 260
262 for (fl = formulalist; fl != NULL; fl = fl->next) 261 for (fl = formulalist; fl != NULL; fl = fl->next)
263 { 262 {
264 for (formula = fl->items; formula != NULL; formula = formula->next) 263 for (formula = fl->items; formula != NULL; formula = formula->next)
265 for (check = formula->next; check != NULL; check = check->next) 264 for (check = formula->next; check != NULL; check = check->next)
299 298
300 for (i = 0; i < formula->arch_names; i++) 299 for (i = 0; i < formula->arch_names; i++)
301 { 300 {
302 const char *string = formula->arch_name[i]; 301 const char *string = formula->arch_name[i];
303 302
304 if (find_archetype (string) != NULL) 303 if (archetype::find (string) != NULL)
305 { 304 {
306 art = locate_recipe_artifact (formula, i); 305 art = locate_recipe_artifact (formula, i);
307 if (!art && strcmp (formula->title, "NONE")) 306 if (!art && strcmp (formula->title, "NONE"))
308 LOG (llevError, "Formula %s has no artifact\n", &formula->title); 307 LOG (llevError, "Formula %s has no artifact\n", &formula->title);
309 else 308 else
356{ 355{
357 treasurelist *tl; 356 treasurelist *tl;
358 archetype *at; 357 archetype *at;
359 358
360 if (depth > 10) 359 if (depth > 10)
361 return NULL; 360 return 0;
362 while (t != NULL) 361
362 while (t)
363 { 363 {
364 if (t->name != NULL) 364 if (t->name)
365 { 365 {
366 tl = find_treasurelist (t->name); 366 tl = find_treasurelist (t->name);
367
368 if (tl)
369 {
367 at = find_treasure_by_name (tl->items, name, depth + 1); 370 at = find_treasure_by_name (tl->items, name, depth + 1);
371
372 if (at)
373 return at;
374 }
375 }
376 else
377 {
378 if (t->item && !strcasecmp (t->item->clone.name, name))
379 return t->item;
380 }
381
382 if (t->next_yes)
383 {
384 at = find_treasure_by_name (t->next_yes, name, depth);
368 if (at != NULL) 385 if (at)
369 return at; 386 return at;
370 } 387 }
371 else 388
372 { 389 if (t->next_no)
373 if (!strcasecmp (t->item->clone.name, name))
374 return t->item;
375 } 390 {
376 if (t->next_yes != NULL)
377 {
378 at = find_treasure_by_name (t->next_yes, name, depth); 391 at = find_treasure_by_name (t->next_no, name, depth);
379 if (at != NULL) 392 if (at)
380 return at; 393 return at;
381 } 394 }
382 if (t->next_no != NULL)
383 {
384 at = find_treasure_by_name (t->next_no, name, depth);
385 if (at != NULL)
386 return at;
387 }
388 t = t->next; 395 t = t->next;
389 } 396 }
390 return NULL; 397 return 0;
391} 398}
392 399
393/* If several archetypes have the same name, the value of the first 400/* If several archetypes have the same name, the value of the first
394 * one with that name will be returned. This happens for the 401 * one with that name will be returned. This happens for the
395 * mushrooms (mushroom_1, mushroom_2 and mushroom_3). For the 402 * mushrooms (mushroom_1, mushroom_2 and mushroom_3). For the
415 while (isdigit (*name)) 422 while (isdigit (*name))
416 { 423 {
417 mult = 10 * mult + (*name - '0'); 424 mult = 10 * mult + (*name - '0');
418 name++; 425 name++;
419 } 426 }
427
420 if (mult > 0) 428 if (mult > 0)
421 name++; 429 name++;
422 else 430 else
423 mult = 1; 431 mult = 1;
432
424 /* first, try to match the name of an archetype */ 433 /* first, try to match the name of an archetype */
425 for (at = first_archetype; at != NULL; at = at->next) 434 for (at = first_archetype; at != NULL; at = at->next)
426 { 435 {
427 if (at->clone.title != NULL) 436 if (at->clone.title != NULL)
428 { 437 {
432 return mult * at->clone.value; 441 return mult * at->clone.value;
433 } 442 }
434 if (!strcasecmp (at->clone.name, name)) 443 if (!strcasecmp (at->clone.name, name))
435 return mult * at->clone.value; 444 return mult * at->clone.value;
436 } 445 }
446
437 /* second, try to match an artifact ("arch of something") */ 447 /* second, try to match an artifact ("arch of something") */
438 cp = strstr (name, " of "); 448 cp = strstr (name, " of ");
439 if (cp != NULL) 449 if (cp != NULL)
440 { 450 {
441 strcpy (part1, name); 451 strcpy (part1, name);
455 if (!strcasecmp (art->item->name, part2)) 465 if (!strcasecmp (art->item->name, part2))
456 return mult * at->clone.value * art->item->value; 466 return mult * at->clone.value * art->item->value;
457 } 467 }
458 } 468 }
459 } 469 }
470
460 /* third, try to match a body part ("arch's something") */ 471 /* third, try to match a body part ("arch's something") */
461 cp = strstr (name, "'s "); 472 cp = strstr (name, "'s ");
462 if (cp != NULL) 473 if (cp)
463 { 474 {
464 strcpy (part1, name); 475 strcpy (part1, name);
465 part1[cp - name] = '\0'; 476 part1[cp - name] = '\0';
466 strcpy (part2, cp + 3); 477 strcpy (part2, cp + 3);
467 /* examine all archetypes matching the first part of the name */ 478 /* examine all archetypes matching the first part of the name */
469 if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL) 480 if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL)
470 { 481 {
471 if (at->clone.randomitems != NULL) 482 if (at->clone.randomitems != NULL)
472 { 483 {
473 at2 = find_treasure_by_name (at->clone.randomitems->items, part2, 0); 484 at2 = find_treasure_by_name (at->clone.randomitems->items, part2, 0);
474 if (at2 != NULL) 485 if (at2)
475 return mult * at2->clone.value * isqrt (at->clone.level * 2); 486 return mult * at2->clone.value * isqrt (at->clone.level * 2);
476 } 487 }
477 } 488 }
478 } 489 }
490
479 /* failed to find any matching items -- formula should be checked */ 491 /* failed to find any matching items -- formula should be checked */
480 return -1; 492 return -1;
481} 493}
482 494
483/* code copied from dump_alchemy() and modified by Raphael Quinet */ 495/* code copied from dump_alchemy() and modified by Raphael Quinet */
506 518
507 for (i = 0; i < formula->arch_names; i++) 519 for (i = 0; i < formula->arch_names; i++)
508 { 520 {
509 const char *string = formula->arch_name[i]; 521 const char *string = formula->arch_name[i];
510 522
511 if ((at = find_archetype (string)) != NULL) 523 if ((at = archetype::find (string)) != NULL)
512 { 524 {
513 art = locate_recipe_artifact (formula, i); 525 art = locate_recipe_artifact (formula, i);
514 if (!art && strcmp (formula->title, "NONE")) 526 if (!art && strcmp (formula->title, "NONE"))
515 LOG (llevError, "Formula %s has no artifact\n", &formula->title); 527 LOG (llevError, "Formula %s has no artifact\n", &formula->title);
516 else 528 else
615 if ((at = find_artifactlist (item->type))) 627 if ((at = find_artifactlist (item->type)))
616 for (art = at->items; art; art = art->next) 628 for (art = at->items; art; art = art->next)
617 if (!strcmp (art->item->name, rp->title)) 629 if (!strcmp (art->item->name, rp->title))
618 break; 630 break;
619 631
620 free_object (item); 632 item->destroy ();
621 633
622 return art; 634 return art;
623} 635}
624 636
625int 637int
643 for (fl = get_formulalist (1); fl; fl = fl->next) 655 for (fl = get_formulalist (1); fl; fl = fl->next)
644 number++; 656 number++;
645 657
646 /* now, randomly choose one */ 658 /* now, randomly choose one */
647 if (number > 0) 659 if (number > 0)
648 roll = RANDOM () % number; 660 roll = rndm (number);
649 661
650 fl = get_formulalist (1); 662 fl = get_formulalist (1);
651 while (roll && fl) 663 while (roll && fl)
652 { 664 {
653 if (fl->next) 665 if (fl->next)
676 if ((fl = get_random_recipelist ()) == NULL) 688 if ((fl = get_random_recipelist ()) == NULL)
677 return rp; 689 return rp;
678 690
679 if (fl->total_chance > 0) 691 if (fl->total_chance > 0)
680 { 692 {
681 r = RANDOM () % fl->total_chance; 693 r = rndm (fl->total_chance);
682 for (rp = fl->items; rp; rp = rp->next) 694 for (rp = fl->items; rp; rp = rp->next)
683 { 695 {
684 r -= rp->chance; 696 r -= rp->chance;
685 if (r < 0) 697 if (r < 0)
686 break; 698 break;
737 char *dup; 749 char *dup;
738 char *p; 750 char *p;
739 size_t size; 751 size_t size;
740 size_t i; 752 size_t i;
741 753
742 dup = strdup_local (str); 754 dup = strdup (str);
743 if (dup == NULL) 755 if (dup == NULL)
744 fatal (OUT_OF_MEMORY); 756 fatal (OUT_OF_MEMORY);
745 757
746 size = 0; 758 size = 0;
747 for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ",")) 759 for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ","))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines