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

Comparing deliantra/server/server/alchemy.C (file contents):
Revision 1.52 by root, Thu Apr 15 02:51:39 2010 UTC vs.
Revision 1.66 by root, Fri Nov 26 17:43:04 2021 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 (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
4 * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 5 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team 6 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992 Frank Tore Johansen 7 * Copyright (©) 1992 Frank Tore Johansen
7 * 8 *
8 * Deliantra is free software: you can redistribute it and/or modify it under 9 * 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 10 * 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 11 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version. 12 * option) any later version.
12 * 13 *
13 * This program is distributed in the hope that it will be useful, 14 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details. 17 * GNU General Public License for more details.
17 * 18 *
18 * You should have received a copy of the Affero GNU General Public License 19 * You should have received a copy of the Affero GNU General Public License
19 * and the GNU General Public License along with this program. If not, see 20 * and the GNU General Public License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>. 21 * <http://www.gnu.org/licenses/>.
21 * 22 *
22 * The authors can be reached via e-mail to <support@deliantra.net> 23 * The authors can be reached via e-mail to <support@deliantra.net>
23 */ 24 */
24 25
25/* March 96 - Laid down original code. -b.t. thomas@astro.psu.edu */ 26/* March 96 - Laid down original code. -b.t. thomas@astro.psu.edu */
26 27
63 64
64/** Returns a random selection from cauldron_effect[] */ 65/** Returns a random selection from cauldron_effect[] */
65static const char * 66static const char *
66cauldron_sound () 67cauldron_sound ()
67{ 68{
68 int size = sizeof (cauldron_effect) / sizeof (char *); 69 return cauldron_effect [rndm (ecb_array_length (cauldron_effect))];
69
70 return cauldron_effect[rndm (0, size - 1)];
71} 70}
72 71
73/** 72/**
74 * Recipe value of the entire contents of a container. 73 * Recipe value of the entire contents of a container.
75 * This appears to just generate a hash value, which I guess for now works 74 * This appears to just generate a hash value, which I guess for now works
76 * ok, but the possibility of duplicate hashes is certainly possible - msw 75 * ok, but the possibility of duplicate hashes is certainly possible - msw
77 */ 76 */
78static int 77static int
79content_recipe_value (object *op) 78content_recipe_value (object *op)
80{ 79{
81 char name[MAX_BUF];
82 object *tmp = op->inv; 80 object *tmp = op->inv;
83 int tval = 0, formula = 0; 81 int formula = 0;
84 82
85 while (tmp) 83 while (tmp)
86 { 84 {
87 tval = 0; 85 const char *name = tmp->title
88 assign (name, tmp->name);
89 if (tmp->title)
90 sprintf (name, "%s %s", &tmp->name, &tmp->title); 86 ? (const char *)format ("%s %s", &tmp->name, &tmp->title)
87 : tmp->name;
88
91 tval = (strtoint (name) * (tmp->nrof ? tmp->nrof : 1)); 89 int tval = strtoint (name) * tmp->number_of ();
92#ifdef ALCHEMY_DEBUG 90#ifdef ALCHEMY_DEBUG
93 LOG (llevDebug, "Got ingredient %d %s(%d)\n", tmp->nrof ? tmp->nrof : 1, name, tval); 91 LOG (llevDebug, "Got ingredient %d %s(%d)\n", tmp->nrof ? tmp->nrof : 1, name, tval);
94#endif 92#endif
95 formula += tval; 93 formula += tval;
96 tmp = tmp->below; 94 tmp = tmp->below;
97 } 95 }
98#ifdef ALCHEMY_DEBUG 96#ifdef ALCHEMY_DEBUG
99 LOG (llevDebug, " Formula value=%d\n", formula); 97 LOG (llevDebug, " Formula value=%d\n", formula);
100#endif 98#endif
99
101 return formula; 100 return formula;
102} 101}
103 102
104/** 103/**
105 * Returns total number of items in op 104 * Returns total number of items in op
135 */ 134 */
136static object * 135static object *
137find_transmution_ob (object *first_ingred, recipe *rp, size_t *rp_arch_index, int create_item) 136find_transmution_ob (object *first_ingred, recipe *rp, size_t *rp_arch_index, int create_item)
138{ 137{
139 object *prod_item = 0; 138 object *prod_item = 0;
140 bool found = false;
141 *rp_arch_index = 0; 139 *rp_arch_index = 0;
142 140
143 if (rp->transmute) /* look for matching ingredient/prod archs */ 141 if (rp->transmute) /* look for matching ingredient/prod archs */
144 for (object *item = first_ingred; item; item = item->below) 142 for (object *item = first_ingred; item; item = item->below)
145 { 143 {
168 &prod_item->arch->archname, prod_item->nrof); 166 &prod_item->arch->archname, prod_item->nrof);
169 } 167 }
170#endif 168#endif
171 if (!prod_item) 169 if (!prod_item)
172 *rp_arch_index = rndm (rp->arch_names); 170 *rp_arch_index = rndm (rp->arch_names);
173 prod_item = get_archetype (rp->arch_name[*rp_arch_index]); 171 prod_item = archetype::get (rp->arch_name[*rp_arch_index]);
174 } 172 }
175 173
176#ifdef ALCHEMY_DEBUG 174#ifdef ALCHEMY_DEBUG
177 LOG (llevDebug, "recipe calls for%stransmution.\n", rp->transmute ? " " : " no "); 175 LOG (llevDebug, "recipe calls for%stransmution.\n", rp->transmute ? " " : " no ");
178 if (prod_item != NULL) 176 if (prod_item != NULL)
234} 232}
235 233
236/* 234/*
237 * All but object "save_item" are elimentated from 235 * All but object "save_item" are elimentated from
238 * the container list. Note we have to becareful to remove the inventories 236 * the container list. Note we have to becareful to remove the inventories
239 * of objects in the cauldron inventory (ex icecube has stuff in it). 237 * of objects in the cauldron inventory (ex icecube has stuff in it).
240 */ 238 */
241static void 239static void
242remove_contents (object *first_ob, object *save_item) 240remove_contents (object *first_ob, object *save_item)
243{ 241{
244 // this cries for a cleaner rewrite, removing save_item first possibly 242 // this cries for a cleaner rewrite, removing save_item first possibly
289 item->nrof = nrof; 287 item->nrof = nrof;
290 } 288 }
291} 289}
292 290
293/** 291/**
294 * Essentially a wrapper for make_item_from_recipe() and 292 * Essentially a wrapper for make_item_from_recipe() and
295 * insert_ob_in_ob. If the caster has some alchemy skill, then they might 293 * insert_ob_in_ob. If the caster has some alchemy skill, then they might
296 * gain some exp from (successfull) fabrication of the product. 294 * gain some exp from (successfull) fabrication of the product.
297 * If nbatches==-1, don't give exp for this creation (random generation/ 295 * If nbatches==-1, don't give exp for this creation (random generation/
298 * failed recipe) 296 * failed recipe)
299 */ 297 */
300static object * 298static object *
301attempt_recipe (object *caster, object *cauldron, int ability, recipe *rp, int nbatches) 299attempt_recipe (object *caster, object *cauldron, int ability, recipe *rp, int nbatches)
342 { 340 {
343 remove_contents (cauldron->inv, item); 341 remove_contents (cauldron->inv, item);
344 /* Recalc carrying of the cauldron, in case recipe did not conserve mass */ 342 /* Recalc carrying of the cauldron, in case recipe did not conserve mass */
345 cauldron->update_weight (); 343 cauldron->update_weight ();
346 /* adj lvl, nrof on caster level */ 344 /* adj lvl, nrof on caster level */
347 adjust_product (item, ability, rp->yield ? (rp->yield * batches) : batches); 345 adjust_product (item, ability, rp->yield ? rp->yield * batches : batches);
346
348 if (!item->env && (item = insert_ob_in_ob (item, cauldron)) == NULL) 347 if (!item->env && (item = insert_ob_in_ob (item, cauldron)) == NULL)
349 { 348 {
350 new_draw_info (NDI_UNIQUE, 0, caster, "Nothing happened."); 349 new_draw_info (NDI_UNIQUE, 0, caster, "Nothing happened.");
351 /* new_draw_info_format(NDI_UNIQUE, 0,caster, 350 /* new_draw_info_format(NDI_UNIQUE, 0,caster,
352 "Your spell causes the %s to explode!",&cauldron->name); */ 351 "Your spell causes the %s to explode!",&cauldron->name); */
389 if (rndm (0, 2)) 388 if (rndm (0, 2))
390 { /* slag created */ 389 { /* slag created */
391 object *tmp = cauldron->inv; 390 object *tmp = cauldron->inv;
392 int weight = 0; 391 int weight = 0;
393 392
394 tmp = get_archetype (shstr_rock); 393 tmp = archetype::get (shstr_rock);
395 tmp->weight = weight; 394 tmp->weight = weight;
396 tmp->value = 0; 395 tmp->value = 0;
397 tmp->material = name_to_material (shstr_stone); 396 tmp->material = name_to_material (shstr_stone);
398 tmp->name = shstr_slag; 397 tmp->name = shstr_slag;
399 tmp->name_pl = shstr_slags; 398 tmp->name_pl = shstr_slags;
466 465
467 remove_contents (cauldron->inv, NULL); 466 remove_contents (cauldron->inv, NULL);
468 switch (rndm (0, 2)) 467 switch (rndm (0, 2))
469 { 468 {
470 case 0: 469 case 0:
471 tmp = get_archetype (shstr_bomb); 470 tmp = archetype::get (shstr_bomb);
472 tmp->stats.dam = random_roll (1, level, op, PREFER_LOW); 471 tmp->stats.dam = random_roll (1, level, op, PREFER_LOW);
473 tmp->stats.hp = random_roll (1, level, op, PREFER_LOW); 472 tmp->stats.hp = random_roll (1, level, op, PREFER_LOW);
474 new_draw_info_format (NDI_UNIQUE, 0, op, "The %s creates a bomb!", &cauldron->name); 473 new_draw_info_format (NDI_UNIQUE, 0, op, "The %s creates a bomb!", &cauldron->name);
475 break; 474 break;
476 475
477 default: 476 default:
478 tmp = get_archetype (shstr_fireball); 477 tmp = archetype::get (shstr_fireball);
479 tmp->stats.dam = random_roll (1, level, op, PREFER_LOW) / 5 + 1; 478 tmp->stats.dam = random_roll (1, level, op, PREFER_LOW) / 5 + 1;
480 tmp->stats.hp = random_roll (1, level, op, PREFER_LOW) / 10 + 2; 479 tmp->stats.hp = random_roll (1, level, op, PREFER_LOW) / 10 + 2;
481 new_draw_info_format (NDI_UNIQUE, 0, op, "The %s erupts in flame!", &cauldron->name); 480 new_draw_info_format (NDI_UNIQUE, 0, op, "The %s erupts in flame!", &cauldron->name);
482 break; 481 break;
483 } 482 }
489 new_draw_info_format (NDI_UNIQUE, 0, op, "The %s %s.", &cauldron->name, cauldron_sound ()); 488 new_draw_info_format (NDI_UNIQUE, 0, op, "The %s %s.", &cauldron->name, cauldron_sound ());
490 remove_contents (cauldron->inv, NULL); 489 remove_contents (cauldron->inv, NULL);
491 } 490 }
492 else if (level < 80) 491 else if (level < 80)
493 { /* MAJOR FIRE */ 492 { /* MAJOR FIRE */
494 object *fb = get_archetype (SP_MED_FIREBALL); 493 object *fb = archetype::get (SP_MED_FIREBALL);
495 494
496 remove_contents (cauldron->inv, NULL); 495 remove_contents (cauldron->inv, NULL);
497 fire_arch_from_position (cauldron, cauldron, cauldron->x, cauldron->y, 0, fb); 496 fire_arch_from_position (cauldron, cauldron, cauldron->x, cauldron->y, 0, fb);
498 fb->destroy (); 497 fb->destroy ();
499 new_draw_info_format (NDI_UNIQUE, 0, op, "The %s erupts in flame!", &cauldron->name); 498 new_draw_info_format (NDI_UNIQUE, 0, op, "The %s erupts in flame!", &cauldron->name);
538 } 537 }
539 else if (level == 151) 538 else if (level == 151)
540 { /* CREATE RANDOM ARTIFACT */ 539 { /* CREATE RANDOM ARTIFACT */
541 object *tmp; 540 object *tmp;
542 541
543 /* this is meant to be better than prior possiblity, 542 /* this is meant to be better than prior possiblity,
544 * in this one, we allow *any* valid alchemy artifact 543 * in this one, we allow *any* valid alchemy artifact
545 * to be made (rather than only those on the given 544 * to be made (rather than only those on the given
546 * formulalist) */ 545 * formulalist) */
547 if (!rp) 546 if (!rp)
548 rp = get_random_recipe ((recipelist *) NULL); 547 rp = get_random_recipe ((recipelist *) NULL);
549 548
550 if (rp && (tmp = get_archetype (rp->arch_name [rndm (rp->arch_names)]))) 549 if (rp && (tmp = archetype::get (rp->arch_name [rndm (rp->arch_names)])))
551 { 550 {
552 generate_artifact (tmp, random_roll (1, op->level / 2 + 1, op, PREFER_HIGH) + 1); 551 generate_artifact (tmp, random_roll (1, op->level / 2 + 1, op, PREFER_HIGH) + 1);
553 if ((tmp = insert_ob_in_ob (tmp, cauldron))) 552 if ((tmp = insert_ob_in_ob (tmp, cauldron)))
554 { 553 {
555 remove_contents (cauldron->inv, tmp); 554 remove_contents (cauldron->inv, tmp);
557 } 556 }
558 } 557 }
559 } 558 }
560 else 559 else
561 { /* MANA STORM - watch out!! */ 560 { /* MANA STORM - watch out!! */
562 object *tmp = get_archetype (LOOSE_MANA); 561 object *tmp = archetype::get (LOOSE_MANA);
563 562
564 new_draw_info (NDI_UNIQUE, 0, op, "You unwisely release potent forces!"); 563 new_draw_info (NDI_UNIQUE, 0, op, "You unwisely release potent forces!");
565 remove_contents (cauldron->inv, NULL); 564 remove_contents (cauldron->inv, NULL);
566 cast_magic_storm (op, tmp, level); 565 cast_magic_storm (op, tmp, level);
567 } 566 }
572 * could be if the user fails to concoct a recipe properly. Factors include 571 * could be if the user fails to concoct a recipe properly. Factors include
573 * the number of ingredients, the length of the name of each ingredient, 572 * the number of ingredients, the length of the name of each ingredient,
574 * the user's effective level, the user's Int and the enchantment on the 573 * the user's effective level, the user's Int and the enchantment on the
575 * mixing device (aka "cauldron"). Higher values of 'danger' indicate more 574 * mixing device (aka "cauldron"). Higher values of 'danger' indicate more
576 * danger. Note that we assume that we have had the caster ready the alchemy 575 * danger. Note that we assume that we have had the caster ready the alchemy
577 * skill *before* this routine is called. (no longer auto-readies that skill) 576 * skill *before* this routine is called. (no longer auto-readies that skill)
578 * -b.t. 577 * -b.t.
579 */ 578 */
580static int 579static int
581calc_alch_danger (object *caster, object *cauldron, recipe *rp) 580calc_alch_danger (object *caster, object *cauldron, object *skill, recipe *rp)
582{ 581{
583 object *item;
584 char name[MAX_BUF];
585 int danger = 0, nrofi = 0; 582 int danger = 0;
586 583
587 /* Knowing alchemy skill reduces yer risk */ 584 /* Knowing alchemy skill reduces yer risk */
588 danger -= caster->chosen_skill ? caster->chosen_skill->level : caster->level; 585 danger -= skill->level;
589
590 if (!caster->chosen_skill)
591 LOG (llevError | logBacktrace, "calc_alch_danger called without a chosen skill, caster %s, cauldron %s\n",
592 caster->debug_desc (), cauldron->debug_desc ());
593 586
594 /* better cauldrons reduce risk */ 587 /* better cauldrons reduce risk */
595 danger -= cauldron->magic; 588 danger -= cauldron->magic;
596 589
597 /* Higher Int, lower the risk */ 590 /* Higher Int, lower the risk */
599 592
600 /* Ingredients. Longer names usually mean rarer stuff. 593 /* Ingredients. Longer names usually mean rarer stuff.
601 * Thus the backfire is worse. Also, more ingredients 594 * Thus the backfire is worse. Also, more ingredients
602 * means we are attempting a more powerfull potion, 595 * means we are attempting a more powerfull potion,
603 * and thus the backfire will be worse. */ 596 * and thus the backfire will be worse. */
604 for (item = cauldron->inv; item; item = item->below) 597 for (object *item = cauldron->inv; item; item = item->below)
605 { 598 {
606 assign (name, item->name); 599 const char *name = item->title
607 if (item->title)
608 sprintf (name, "%s %s", &item->name, &item->title); 600 ? format ("%s %s", &item->name, &item->title)
601 : &item->name;
602
609 danger += (strtoint (name) / 1000) + 3; 603 danger += strtoint (name) / 1000 + 3;
610 nrofi++;
611 } 604 }
612 605
613 if (rp == NULL) 606 if (!rp)
614 danger += 110; 607 danger += 110;
615 else 608 else
616 danger += rp->diff * 3; 609 danger += rp->diff * 3;
617 610
618 /* Using a bad device is *majorly* stupid */ 611 /* Using a bad device is *majorly* stupid */
619 if (cauldron->flag [FLAG_CURSED]) 612 if (cauldron->flag [FLAG_CURSED]) danger += 80;
620 danger += 80;
621 if (cauldron->flag [FLAG_DAMNED]) 613 if (cauldron->flag [FLAG_DAMNED]) danger += 200;
622 danger += 200;
623 614
624#ifdef ALCHEMY_DEBUG 615#ifdef ALCHEMY_DEBUG
625 LOG (llevDebug, "calc_alch_danger() returned danger=%d\n", danger); 616 LOG (llevDebug, "calc_alch_danger() returned danger=%d\n", danger);
626#endif 617#endif
627 618
805 return result; 796 return result;
806} 797}
807 798
808/** 799/**
809 * Main part of the ALCHEMY code. From this we call fctns 800 * Main part of the ALCHEMY code. From this we call fctns
810 * that take a look at the contents of the 'cauldron' and, using these ingredients, 801 * that take a look at the contents of the 'cauldron' and, using these ingredients,
811 * we construct an integer formula value which is referenced (randomly) against a 802 * we construct an integer formula value which is referenced (randomly) against a
812 * formula list (the formula list chosen is based on the # contents of the cauldron). 803 * formula list (the formula list chosen is based on the # contents of the cauldron).
813 * 804 *
814 * If we get a match between the recipe indicated in cauldron contents and a 805 * If we get a match between the recipe indicated in cauldron contents and a
815 * randomly chosen one, an item is created and experience awarded. Otherwise 806 * randomly chosen one, an item is created and experience awarded. Otherwise
816 * various failure effects are possible (getting worse and worse w/ # cauldron 807 * various failure effects are possible (getting worse and worse w/ # cauldron
817 * ingredients). Note that the 'item' to be made can be *anything* listed on 808 * ingredients). Note that the 'item' to be made can be *anything* listed on
818 * the artifacts list in lib/artifacts which has a recipe listed in lib/formulae. 809 * the artifacts list in lib/artifacts which has a recipe listed in lib/formulae.
819 * 810 *
820 * To those wondering why I am using the funky formula index method: 811 * To those wondering why I am using the funky formula index method:
821 * 1) I want to match recipe to ingredients regardless of ordering. 812 * 1) I want to match recipe to ingredients regardless of ordering.
822 * 2) I want a fast search for the 'right' recipe. 813 * 2) I want a fast search for the 'right' recipe.
823 * 814 *
824 * Note: it is just possible that a totally different combination of 815 * Note: it is just possible that a totally different combination of
825 * ingredients will result in a match with a given recipe. This is not a bug! 816 * ingredients will result in a match with a given recipe. This is not a bug!
826 * There is no good reason (in my mind) why alchemical processes have to be 817 * There is no good reason (in my mind) why alchemical processes have to be
827 * unique -- such a 'feature' is one reason why players might want to experiment 818 * unique -- such a 'feature' is one reason why players might want to experiment
828 * around. :) 819 * around. :)
835 recipe *rp = NULL; 826 recipe *rp = NULL;
836 float success_chance; 827 float success_chance;
837 int numb, ability = 1; 828 int numb, ability = 1;
838 int formula = 0; 829 int formula = 0;
839 float ave_chance; 830 float ave_chance;
840 object *item, *skop; 831 object *item;
841 832
842 if (caster->type != PLAYER) 833 if (caster->type != PLAYER)
843 return; /* only players for now */ 834 return; /* only players for now */
844 835
845 if (get_map_flags (caster->map, NULL, caster->x, caster->y, NULL, NULL) & P_SAFE) 836 if (get_map_flags (caster->map, NULL, caster->x, caster->y, NULL, NULL) & P_SAFE)
898 889
899 /* create the object **FIRST**, then decide whether to keep it. */ 890 /* create the object **FIRST**, then decide whether to keep it. */
900 if ((item = attempt_recipe (caster, cauldron, ability, rp, formula / rp->index)) != NULL) 891 if ((item = attempt_recipe (caster, cauldron, ability, rp, formula / rp->index)) != NULL)
901 { 892 {
902 /* compute base chance of recipe success */ 893 /* compute base chance of recipe success */
894 //TODO: (marc) diff is documenmted as percentage difficulty, not as divisor.
903 success_chance = ((float) ability / (float) (rp->diff * (item->level + 2))); 895 success_chance = ((float)ability / (float)(rp->diff * (item->level + 2)));
904 if (ave_chance == 0) 896 if (ave_chance == 0)
905 ave_chance = 1; 897 ave_chance = 1;
906 898
907#ifdef ALCHEMY_DEBUG 899#ifdef ALCHEMY_DEBUG
908 LOG (llevDebug, "percent success chance = %f ab%d / diff%d*lev%d\n", success_chance, ability, rp->diff, item->level); 900 LOG (llevDebug, "percent success chance = %f ab%d / diff%d*lev%d\n", success_chance, ability, rp->diff, item->level);
930 } 922 }
931 } 923 }
932 } 924 }
933 925
934 /* if we get here, we failed!! */ 926 /* if we get here, we failed!! */
935 alchemy_failure_effect (caster, cauldron, rp, calc_alch_danger (caster, cauldron, rp)); 927 alchemy_failure_effect (caster, cauldron, rp, calc_alch_danger (caster, cauldron, skill, rp));
936} 928}
937 929

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines