ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/treasure.C
Revision: 1.120
Committed: Sat Nov 17 23:33:17 2018 UTC (5 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.119: +8 -8 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * 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 (©) 1992 Frank Tore Johansen
7 *
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
10 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * 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 * <http://www.gnu.org/licenses/>.
21 *
22 * The authors can be reached via e-mail to <support@deliantra.net>
23 */
24
25 /* TREASURE_DEBUG does some checking on the treasurelists after loading.
26 * It is useful for finding bugs in the treasures file. Since it only
27 * slows the startup some (and not actual game play), it is by default
28 * left on
29 */
30 #define TREASURE_DEBUG
31
32 /* TREASURE_VERBOSE enables copious output concerning artifact generation */
33
34 //#define TREASURE_VERBOSE
35
36 #include <global.h>
37 #include <treasure.h>
38
39 #include <flat_hash_map.hpp>
40
41 extern char *spell_mapping[];
42
43 static treasurelist *first_treasurelist;
44
45 static void change_treasure (treasure *t, object *op); /* overrule default values */
46
47 typedef ska::flat_hash_map<
48 const char *,
49 treasurelist *,
50 str_hash,
51 str_equal,
52 slice_allocator< std::pair<const char *const, treasurelist *> >
53 > tl_map_t;
54
55 static tl_map_t tl_map;
56
57 //TODO: class method
58 static void free_treasurestruct (treasure *t); // bleh desu
59 static void
60 clear (treasurelist *tl)
61 {
62 if (tl->items)
63 {
64 free_treasurestruct (tl->items);
65 tl->items = 0;
66 }
67
68 tl->total_chance = 0;
69 }
70
71 /*
72 * Searches for the given treasurelist
73 */
74 treasurelist *
75 treasurelist::find (const char *name)
76 {
77 if (!name)
78 return 0;
79
80 auto (i, tl_map.find (name));
81
82 if (i == tl_map.end ())
83 return 0;
84
85 return i->second;
86 }
87
88 /*
89 * Searches for the given treasurelist in the globally linked list
90 * of treasurelists which has been built by load_treasures().
91 */
92 treasurelist *
93 treasurelist::get (const char *name)
94 {
95 treasurelist *tl = find (name);
96
97 if (!tl)
98 {
99 tl = new treasurelist;
100
101 tl->name = name;
102 tl->next = first_treasurelist;
103 first_treasurelist = tl;
104
105 tl_map.insert (std::make_pair (tl->name, tl));
106 }
107
108 return tl;
109 }
110
111 #ifdef TREASURE_DEBUG
112 /* recursived checks the linked list. Treasurelist is passed only
113 * so that the treasure name can be printed out
114 */
115 static void
116 check_treasurelist (const treasure *t, const treasurelist * tl)
117 {
118 if (t->chance >= 100 && t->next_yes && (t->next || t->next_no))
119 LOG (llevError, "Treasurelist %s has element that has 100%% generation, next_yes field as well as next or next_no\n", &tl->name);
120
121 if (t->next)
122 check_treasurelist (t->next, tl);
123
124 if (t->next_yes)
125 check_treasurelist (t->next_yes, tl);
126
127 if (t->next_no)
128 check_treasurelist (t->next_no, tl);
129 }
130 #endif
131
132 /*
133 * Reads the lib/treasure file from disk, and parses the contents
134 * into an internal treasure structure (very linked lists)
135 */
136 static treasure *
137 read_treasure (object_thawer &f)
138 {
139 treasure *t = new treasure;
140
141 f.next ();
142
143 for (;;)
144 {
145 coroapi::cede_to_tick ();
146
147 switch (f.kw)
148 {
149 case KW_arch:
150 t->item = archetype::find (f.get_str ());
151
152 if (!t->item)
153 {
154 f.parse_warn ("treasure references unknown archetype");
155 t->item = archetype::empty;
156 }
157
158 break;
159
160 case KW_list: f.get (t->name); break;
161 case KW_change_name: f.get (t->change_arch.name); break;
162 case KW_change_title: f.get (t->change_arch.title); break;
163 case KW_change_slaying: f.get (t->change_arch.slaying); break;
164 case KW_chance: f.get (t->chance); break;
165 case KW_nrof: f.get (t->nrof); break;
166 case KW_magic: f.get (t->magic); break;
167
168 case KW_yes: t->next_yes = read_treasure (f); continue;
169 case KW_no: t->next_no = read_treasure (f); continue;
170
171 case KW_end:
172 f.next ();
173 return t;
174
175 case KW_more:
176 t->next = read_treasure (f);
177 return t;
178
179 default:
180 if (!f.parse_error ("treasurelist", t->name))
181 goto error;
182
183 return t;
184 }
185
186 f.next ();
187 }
188
189 // not reached
190
191 error:
192 delete t;
193 return 0;
194 }
195
196 /*
197 * Each treasure is parsed with the help of load_treasure().
198 */
199 treasurelist *
200 treasurelist::read (object_thawer &f)
201 {
202 assert (f.kw == KW_treasure || f.kw == KW_treasureone);
203
204 bool one = f.kw == KW_treasureone;
205 treasurelist *tl = treasurelist::get (f.get_str ());
206 clear (tl);
207 tl->items = read_treasure (f);
208 if (!tl->items)
209 return 0;
210
211 /* This is a one of the many items on the list should be generated.
212 * Add up the chance total, and check to make sure the yes & no
213 * fields of the treasures are not being used.
214 */
215 if (one)
216 {
217 for (treasure *t = tl->items; t; t = t->next)
218 {
219 if (t->next_yes || t->next_no)
220 {
221 LOG (llevError, "Treasure %s is one item, but on treasure %s\n", &tl->name, t->item ? &t->item->archname : &t->name);
222 LOG (llevError, " the next_yes or next_no field is set\n");
223 }
224
225 tl->total_chance += t->chance;
226 }
227 }
228
229 return tl;
230 }
231
232 /*
233 * Generates the objects specified by the given treasure.
234 * It goes recursively through the rest of the linked list.
235 * If there is a certain percental chance for a treasure to be generated,
236 * this is taken into consideration.
237 * The second argument specifies for which object the treasure is
238 * being generated.
239 * If flag is GT_INVISIBLE, only invisible objects are generated (ie, only
240 * abilities. This is used by summon spells, thus no summoned monsters
241 * start with equipment, but only their abilities).
242 */
243 static void
244 put_treasure (object *op, object *creator, int flags)
245 {
246 if (flags & GT_ENVIRONMENT)
247 {
248 /* Bit of a hack - spells should never be put onto the map. The entire
249 * treasure stuff is a problem - there is no clear idea of knowing
250 * this is the original object, or if this is an object that should be created
251 * by another object.
252 */
253 //TODO: flag such as objects... as such (no drop, anybody?)
254 if (op->type == SPELL)
255 {
256 op->destroy ();
257 return;
258 }
259
260 op->expand_tail ();
261
262 if (!creator->is_on_map ()
263 || (op->weight && op->blocked (creator->map, creator->x, creator->y)))
264 op->destroy ();
265 else
266 {
267 op->flag [FLAG_OBJ_ORIGINAL] = true;
268 op->insert_at (creator, creator, INS_NO_MERGE | INS_NO_WALK_ON);
269 }
270 }
271 else
272 {
273 op = creator->insert (op);
274
275 if ((flags & GT_APPLY) && creator->flag [FLAG_MONSTER])
276 monster_check_apply (creator, op);
277 }
278 }
279
280 /* if there are change_xxx commands in the treasure, we include the changes
281 * in the generated object
282 */
283 static void
284 change_treasure (treasure *t, object *op)
285 {
286 /* CMD: change_name xxxx */
287 if (t->change_arch.name)
288 {
289 op->name = t->change_arch.name;
290 op->name_pl = t->change_arch.name;
291 }
292
293 if (t->change_arch.title)
294 op->title = t->change_arch.title;
295
296 if (t->change_arch.slaying)
297 op->slaying = t->change_arch.slaying;
298 }
299
300 static void
301 create_all_treasures (treasure *t, object *op, int flag, int difficulty, int tries)
302 {
303 if ((int) t->chance >= 100 || (rndm (100) + 1) < (int) t->chance)
304 {
305 if (t->name)
306 {
307 if (difficulty >= t->magic)
308 if (treasurelist *tl = treasurelist::find (t->name))
309 create_treasure (tl, op, flag, difficulty, tries);
310 else
311 LOG (llevError, "create_all_treasures: undefined reference to treasurelist '%s'.\n", &t->name);
312 }
313 else
314 {
315 if (t->item && (t->item->invisible != 0 || !(flag & GT_INVISIBLE)))
316 {
317 object *tmp = t->item->instance ();
318
319 if (t->nrof && tmp->nrof <= 1)
320 tmp->nrof = rndm (t->nrof) + 1;
321
322 fix_generated_item (tmp, op, difficulty, t->magic, flag);
323 change_treasure (t, tmp);
324 put_treasure (tmp, op, flag);
325 }
326 }
327
328 if (t->next_yes)
329 create_all_treasures (t->next_yes, op, flag, difficulty, tries);
330 }
331 else if (t->next_no)
332 create_all_treasures (t->next_no, op, flag, difficulty, tries);
333
334 if (t->next)
335 create_all_treasures (t->next, op, flag, difficulty, tries);
336 }
337
338 static void
339 create_one_treasure (treasurelist *tl, object *op, int flag, int difficulty, int tries)
340 {
341 int value = rndm (tl->total_chance);
342 treasure *t;
343
344 if (tries++ > 100)
345 {
346 LOG (llevDebug, "create_one_treasure: tries exceeded 100, returning without making treasure\n");
347 return;
348 }
349
350 for (t = tl->items; t; t = t->next)
351 {
352 value -= t->chance;
353
354 if (value < 0)
355 break;
356 }
357
358 if (!t || value >= 0)
359 cleanup ("create_one_treasure: got null object or not able to find treasure\n", 1);
360
361 if (t->name)
362 {
363 if (difficulty >= t->magic)
364 {
365 treasurelist *tl = treasurelist::find (t->name);
366 if (tl)
367 create_treasure (tl, op, flag, difficulty, tries);
368 }
369 else if (t->nrof)
370 create_one_treasure (tl, op, flag, difficulty, tries);
371 }
372 else if (t->item && (t->item->invisible != 0 || flag != GT_INVISIBLE))
373 {
374 if (object *tmp = t->item->instance ())
375 {
376 if (t->nrof && tmp->nrof <= 1)
377 tmp->nrof = rndm (t->nrof) + 1;
378
379 fix_generated_item (tmp, op, difficulty, t->magic, flag);
380 change_treasure (t, tmp);
381 put_treasure (tmp, op, flag);
382 }
383 }
384 }
385
386 void
387 object::create_treasure (treasurelist *tl, int flags)
388 {
389 ::create_treasure (tl, this, flags, map ? map->difficulty : 0);
390 }
391
392 /* This calls the appropriate treasure creation function. tries is passed
393 * to determine how many list transitions or attempts to create treasure
394 * have been made. It is really in place to prevent infinite loops with
395 * list transitions, or so that excessively good treasure will not be
396 * created on weak maps, because it will exceed the number of allowed tries
397 * to do that.
398 */
399 void
400 create_treasure (treasurelist *tl, object *op, int flag, int difficulty, int tries)
401 {
402 // empty treasurelists are legal
403 if (!tl->items)
404 return;
405
406 if (tries++ > 100)
407 {
408 LOG (llevDebug, "createtreasure: tries exceeded 100, returning without making treasure\n");
409 return;
410 }
411
412 if (op->flag [FLAG_TREASURE_ENV])
413 {
414 // do not generate items when there already is something above the object
415 if (op->flag [FLAG_IS_FLOOR] && op->above)
416 return;
417
418 flag |= GT_ENVIRONMENT;
419 }
420
421 if (tl->total_chance)
422 create_one_treasure (tl, op, flag, difficulty, tries);
423 else
424 create_all_treasures (tl->items, op, flag, difficulty, tries);
425 }
426
427 /* This is similar to the old generate treasure function. However,
428 * it instead takes a treasurelist. It is really just a wrapper around
429 * create_treasure. We create a dummy object that the treasure gets
430 * inserted into, and then return that treausre
431 */
432 object *
433 generate_treasure (treasurelist *tl, int difficulty)
434 {
435 difficulty = clamp (difficulty, 1, settings.max_level);
436
437 object *ob = object::create ();
438
439 create_treasure (tl, ob, 0, difficulty, 0);
440
441 /* Don't want to free the object we are about to return */
442 object *tmp = ob->inv;
443 if (tmp)
444 tmp->remove ();
445
446 if (ob->inv)
447 LOG (llevError, "In generate treasure, created multiple objects.\n");
448
449 ob->destroy ();
450
451 return tmp;
452 }
453
454 /*
455 * This is a new way of calculating the chance for an item to have
456 * a specific magical bonus.
457 * The array has two arguments, the difficulty of the level, and the
458 * magical bonus "wanted".
459 */
460
461 static int difftomagic_list[DIFFLEVELS][MAXMAGIC + 1] = {
462 // chance of magic difficulty
463 // +0 +1 +2 +3 +4
464 {95, 2, 2, 1, 0}, // 1
465 {92, 5, 2, 1, 0}, // 2
466 {85, 10, 4, 1, 0}, // 3
467 {80, 14, 4, 2, 0}, // 4
468 {75, 17, 5, 2, 1}, // 5
469 {70, 18, 8, 3, 1}, // 6
470 {65, 21, 10, 3, 1}, // 7
471 {60, 22, 12, 4, 2}, // 8
472 {55, 25, 14, 4, 2}, // 9
473 {50, 27, 16, 5, 2}, // 10
474 {45, 28, 18, 6, 3}, // 11
475 {42, 28, 20, 7, 3}, // 12
476 {40, 27, 21, 8, 4}, // 13
477 {38, 25, 22, 10, 5}, // 14
478 {36, 23, 23, 12, 6}, // 15
479 {33, 21, 24, 14, 8}, // 16
480 {31, 19, 25, 16, 9}, // 17
481 {27, 15, 30, 18, 10}, // 18
482 {20, 12, 30, 25, 13}, // 19
483 {15, 10, 28, 30, 17}, // 20
484 {13, 9, 27, 28, 23}, // 21
485 {10, 8, 25, 28, 29}, // 22
486 { 8, 7, 23, 26, 36}, // 23
487 { 6, 6, 20, 22, 46}, // 24
488 { 4, 5, 17, 18, 56}, // 25
489 { 2, 4, 12, 14, 68}, // 26
490 { 0, 3, 7, 10, 80}, // 27
491 { 0, 0, 3, 7, 90}, // 28
492 { 0, 0, 0, 3, 97}, // 29
493 { 0, 0, 0, 0, 100}, // 30
494 { 0, 0, 0, 0, 100}, // 31
495 };
496
497 /* calculate the appropriate level for wands staves and scrolls.
498 * This code presumes that op has had its spell object created (in op->inv)
499 *
500 * elmex Wed Aug 9 17:44:59 CEST 2006:
501 * Removed multiplicator, too many high-level items were generated on low-difficulty maps.
502 */
503 static int
504 level_for_item (const object *op, int difficulty)
505 {
506 if (!op->inv)
507 {
508 LOG (llevError, "level_for_item: Object %s has no inventory!\n", &op->name);
509 return 0;
510 }
511
512 int olevel = op->inv->level + int (difficulty * (1. - rndm () * rndm () * 2.));
513
514 if (olevel <= 0)
515 olevel = rndm (1, op->inv->level);
516
517 return min (olevel, MAXLEVEL_TREASURE);
518 }
519
520 /*
521 * Based upon the specified difficulty and upon the difftomagic_list array,
522 * a random magical bonus is returned. This is used when determine
523 * the magical bonus created on specific maps.
524 *
525 * elmex Thu Aug 10 18:45:44 CEST 2006:
526 * Scaling difficulty by max_level, as difficulty is a level and not some
527 * weird integer between 1-31.
528 *
529 */
530 static int
531 magic_from_difficulty (int difficulty)
532 {
533 int scaled_diff = lerp (difficulty, 0, settings.max_level, 0, DIFFLEVELS - 1);
534 scaled_diff = clamp (scaled_diff, 0, DIFFLEVELS - 1);
535
536 int percent = rndm (100);
537 int magic;
538
539 for (magic = 0; magic <= MAXMAGIC; magic++)
540 {
541 percent -= difftomagic_list[scaled_diff][magic];
542
543 if (percent < 0)
544 break;
545 }
546
547 if (magic > MAXMAGIC)
548 {
549 LOG (llevError, "Warning, table for difficulty (scaled %d) %d bad.\n", scaled_diff, difficulty);
550 magic = 0;
551 }
552
553 magic = (rndm (3)) ? magic : -magic;
554 /* LOG(llevDebug, "Chose magic %d for difficulty (scaled %d) %d\n", magic, scaled_diff, difficulty); */
555
556 return magic;
557 }
558
559 /*
560 * Sets magical bonus in an object, and recalculates the effect on
561 * the armour variable, and the effect on speed of armour.
562 * This function doesn't work properly, should add use of archetypes
563 * to make it truly absolute.
564 */
565 void
566 set_abs_magic (object *op, int magic)
567 {
568 if (!magic)
569 return;
570
571 op->magic = magic;
572 if (op->arch)
573 {
574 if (op->type == ARMOUR)
575 ARMOUR_SPEED (op) = (ARMOUR_SPEED (op->arch) * (100 + magic * 10)) / 100;
576
577 if (magic < 0 && !(rndm (3))) /* You can't just check the weight always */
578 magic = (-magic);
579
580 op->weight = (op->arch->weight * (100 - magic * 10)) / 100;
581 }
582 else
583 {
584 if (op->type == ARMOUR)
585 ARMOUR_SPEED (op) = (ARMOUR_SPEED (op) * (100 + magic * 10)) / 100;
586
587 if (magic < 0 && !(rndm (3))) /* You can't just check the weight always */
588 magic = (-magic);
589
590 op->weight = (op->weight * (100 - magic * 10)) / 100;
591 }
592 }
593
594 /*
595 * Sets a random magical bonus in the given object based upon
596 * the given difficulty, and the given max possible bonus.
597 */
598
599 static void
600 set_magic (int difficulty, object *op, int max_magic, int flags)
601 {
602 int i = magic_from_difficulty (difficulty);
603
604 if ((flags & GT_ONLY_GOOD) && i < 0)
605 i = -i;
606
607 i = min (i, max_magic);
608
609 set_abs_magic (op, i);
610 if (i < 0)
611 op->set_flag (FLAG_CURSED);
612 }
613
614 /*
615 * Randomly adds one magical ability to the given object.
616 * Modified for Partial Resistance in many ways:
617 * 1) Since rings can have multiple bonuses, if the same bonus
618 * is rolled again, increase it - the bonuses now stack with
619 * other bonuses previously rolled and ones the item might natively have.
620 * 2) Add code to deal with new PR method.
621 */
622 static void
623 set_ring_bonus (object *op, int bonus)
624 {
625 int r = rndm (bonus > 0 ? 25 : 11);
626
627 if (op->type == AMULET)
628 if (!rndm (21))
629 r = 20 + rndm (2);
630 else if (rndm (2))
631 r = 10;
632 else
633 r = 11 + rndm (9);
634
635 switch (r)
636 {
637 /* Redone by MSW 2000-11-26 to have much less code. Also,
638 * bonuses and penalties will stack and add to existing values.
639 * of the item.
640 */
641 case 0:
642 case 1:
643 case 2:
644 case 3:
645 case 4:
646 case 5:
647 case 6:
648 op->stats.stat (r) += bonus;
649 break;
650
651 case 7:
652 op->stats.dam += bonus;
653 break;
654
655 case 8:
656 op->stats.wc += bonus;
657 break;
658
659 case 9:
660 op->stats.food += bonus; /* hunger/sustenance */
661 break;
662
663 case 10:
664 op->stats.ac += bonus;
665 break;
666
667 /* Item that gives protections/vulnerabilities */
668 case 11:
669 case 12:
670 case 13:
671 case 14:
672 case 15:
673 case 16:
674 case 17:
675 case 18:
676 case 19:
677 {
678 int b = 5 + abs (bonus), val, resist = rndm (num_resist_table);
679
680 /* Roughly generate a bonus between 100 and 35 (depending on the bonus) */
681 val = 10 + rndm (b) + rndm (b) + rndm (b) + rndm (b);
682
683 /* Cursed items need to have higher negative values to equal out with
684 * positive values for how protections work out. Put another
685 * little random element in since that they don't always end up with
686 * even values.
687 */
688 if (bonus < 0)
689 val = 2 * -val - rndm (b);
690
691 val = min (35, val); /* Upper limit */
692
693 b = 0;
694
695 while (op->resist[resist_table[resist]] != 0 && b < 4)
696 resist = rndm (num_resist_table);
697
698 if (b == 4)
699 return; /* Not able to find a free resistance */
700
701 op->resist[resist_table[resist]] = val;
702 /* We should probably do something more clever here to adjust value
703 * based on how good a resistance we gave.
704 */
705 break;
706 }
707 case 20:
708 if (op->type == AMULET)
709 {
710 op->set_flag (FLAG_REFL_SPELL);
711 op->value *= 11;
712 }
713 else
714 {
715 op->stats.hp = 1; /* regenerate hit points */
716 op->value *= 4;
717 }
718 break;
719
720 case 21:
721 if (op->type == AMULET)
722 {
723 op->set_flag (FLAG_REFL_MISSILE);
724 op->value *= 9;
725 }
726 else
727 {
728 op->stats.sp = 1; /* regenerate spell points */
729 op->value *= 3;
730 }
731 break;
732
733 case 22:
734 op->stats.exp += bonus; /* Speed! */
735 op->value = op->value * 2 / 3;
736 break;
737 }
738
739 if (bonus > 0)
740 op->value = 2 * op->value * bonus;
741 else
742 op->value = -2 * op->value * bonus / 3;
743 }
744
745 /*
746 * get_magic(diff) will return a random number between 0 and 4.
747 * diff can be any value above 2. The higher the diff-variable, the
748 * higher is the chance of returning a low number.
749 * It is only used in fix_generated_treasure() to set bonuses on
750 * rings and amulets.
751 * Another scheme is used to calculate the magic of weapons and armours.
752 */
753 static int
754 get_magic (int diff)
755 {
756 diff = min (3, diff);
757
758 for (int i = 0; i < 4; i++)
759 if (rndm (diff))
760 return i;
761
762 return 4;
763 }
764
765 /* special_potion() - so that old potion code is still done right. */
766 static int
767 special_potion (object *op)
768 {
769 if (op->attacktype)
770 return 1;
771
772 if (op->stats.Str || op->stats.Dex || op->stats.Con || op->stats.Pow || op->stats.Wis || op->stats.Int || op->stats.Cha)
773 return 1;
774
775 for (int i = 0; i < NROFATTACKS; i++)
776 if (op->resist[i])
777 return 1;
778
779 return 0;
780 }
781
782 static double
783 value_factor_from_spell_item (object *spell, object *item)
784 {
785 double factor =
786 pow ((spell->value > 0 ? spell->value : 1)
787 * spell->level, 1.5);
788
789 if (item) // this if for: wands/staffs/rods:
790 {
791 /* Old crossfire comment ahead:
792 * Add 50 to both level an divisor to keep prices a little more
793 * reasonable. Otherwise, a high level version of a low level
794 * spell can be worth tons a money (eg, level 20 rod, level 2 spell =
795 * 10 time multiplier). This way, the value are a bit more reasonable.
796 */
797
798 factor *= item->level + 50;
799 factor /= item->inv->level + 50;
800 }
801
802 return factor;
803 }
804
805 #define DICE2 (get_magic(2) == 2 ? 2 : 1)
806 #define DICESPELL (rndm (3) + rndm (3) + rndm (3) + rndm (3) + rndm (3))
807
808 /*
809 * fix_generated_item(): This is called after an item is generated, in
810 * order to set it up right. This produced magical bonuses, puts spells
811 * into scrolls/books/wands, makes it unidentified, hides the value, etc.
812 */
813
814 /* 4/28/96 added creator object from which op may now inherit properties based on
815 * op->type. Right now, which stuff the creator passes on is object type
816 * dependant. I know this is a spagetti manuever, but is there a cleaner
817 * way to do this? b.t. */
818
819 /*
820 * ! (flags & GT_ENVIRONMENT):
821 * Automatically calls fix_flesh_item().
822 *
823 * flags:
824 * GT_STARTEQUIP: Sets FLAG_STARTEQIUP on item if appropriate, or clears the item's
825 * value.
826 * GT_MINIMAL: Does minimal processing on the object - just enough to make it
827 * a working object - don't change magic, value, etc, but set it material
828 * type as appropriate, for objects that need spell objects, set those, etc
829 */
830 void
831 fix_generated_item (object *op, object *creator, int difficulty, int max_magic, int flags)
832 {
833 int was_magic = op->magic, num_enchantments = 0, save_item_power = 0;
834
835 if (!creator || creator->type == op->type)
836 creator = op; /*safety & to prevent polymorphed objects giving attributes */
837
838 /* If we make an artifact, this information will be destroyed */
839 save_item_power = op->item_power;
840 op->item_power = 0;
841
842 if (op->randomitems && op->type != SPELL)
843 {
844 create_treasure (op->randomitems, op, flags & ~GT_ENVIRONMENT, difficulty, 0);
845 /* So the treasure doesn't get created again */
846 op->randomitems = 0;
847 }
848
849 max_it (difficulty, 1);
850
851 if (INVOKE_OBJECT (ADD_BONUS, op,
852 ARG_OBJECT (creator != op ? creator : 0),
853 ARG_INT (difficulty), ARG_INT (max_magic),
854 ARG_INT (flags)))
855 return;
856
857 if (!(flags & GT_MINIMAL))
858 {
859 if (IS_ARCH (op->arch, crown))
860 {
861 set_magic (difficulty, op, max_magic, flags);
862 num_enchantments = calc_item_power (op, 1);
863 generate_artifact (op, difficulty);
864 }
865 else
866 {
867 if (!op->magic && max_magic)
868 set_magic (difficulty, op, max_magic, flags);
869
870 num_enchantments = calc_item_power (op, 1);
871
872 if ((!was_magic && !rndm (CHANCE_FOR_ARTIFACT))
873 || op->type == HORN
874 || difficulty >= settings.max_level) /* high difficulties always generate an artifact, used for shop_floors or treasures */
875 generate_artifact (op, difficulty);
876 }
877
878 /* Object was made an artifact. Calculate its item_power rating.
879 * the item_power in the object is what the artfiact adds.
880 */
881 if (op->title)
882 {
883 /* if save_item_power is set, then most likely we started with an
884 * artifact and have added new abilities to it - this is rare, but
885 * but I have seen things like 'strange rings of fire'. So just figure
886 * out the power from the base power plus what this one adds. Note
887 * that since item_power is not quite linear, this actually ends up
888 * being somewhat of a bonus
889 */
890 if (save_item_power)
891 op->item_power = save_item_power + get_power_from_ench (op->item_power);
892 else
893 op->item_power = get_power_from_ench (op->item_power + num_enchantments);
894 }
895 else if (save_item_power)
896 {
897 /* restore the item_power field to the object if we haven't changed it.
898 * we don't care about num_enchantments - that will basically just
899 * have calculated some value from the base attributes of the archetype.
900 */
901 op->item_power = save_item_power;
902 }
903 else
904 {
905 /* item_power was zero. This is suspicious, as it may be because it
906 * was never previously calculated. Let's compute a value and see if
907 * it is non-zero. If it indeed is, then assign it as the new
908 * item_power value.
909 * - gros, 21th of July 2006.
910 */
911 op->item_power = calc_item_power (op, 0);
912 save_item_power = op->item_power; /* Just in case it would get used
913 * again below */
914 }
915 }
916
917 /* materialtype modifications. Note we allow this on artifacts. */
918 select_material (op, difficulty);
919
920 if (flags & GT_MINIMAL)
921 {
922 if (op->type == POTION)
923 /* Handle healing and magic power potions */
924 if (op->stats.sp && !op->randomitems)
925 {
926 object *tmp = archetype::get (spell_mapping [op->stats.sp]);
927 insert_ob_in_ob (tmp, op);
928 op->stats.sp = 0;
929 }
930 }
931 else if (!op->title) /* Only modify object if not special */
932 switch (op->type)
933 {
934 case WEAPON:
935 case ARMOUR:
936 case SHIELD:
937 case HELMET:
938 case CLOAK:
939 if (op->flag [FLAG_CURSED] && !(rndm (4)))
940 set_ring_bonus (op, -DICE2);
941 break;
942
943 case BRACERS:
944 if (!rndm (op->flag [FLAG_CURSED] ? 5 : 20))
945 {
946 set_ring_bonus (op, op->flag [FLAG_CURSED] ? -DICE2 : DICE2);
947 if (!op->flag [FLAG_CURSED])
948 op->value *= 3;
949 }
950 break;
951
952 case POTION:
953 {
954 int too_many_tries = 0;
955
956 /* Handle healing and magic power potions */
957 if (op->stats.sp && !op->randomitems)
958 {
959 object *tmp = archetype::get (spell_mapping[op->stats.sp]);
960 insert_ob_in_ob (tmp, op);
961 op->stats.sp = 0;
962 }
963
964 while (!special_potion (op) && !op->inv)
965 {
966 generate_artifact (op, difficulty);
967 if (too_many_tries++ > 10)
968 break;
969 }
970
971 /* don't want to change value for healing/magic power potions,
972 * since the value set on those is already correct.
973 */
974 if (op->inv && op->randomitems)
975 {
976 /* value multiplier is same as for scrolls */
977 op->value *= op->inv->value;
978 op->level = op->inv->level / 2 + rndm (difficulty) + rndm (difficulty);
979 }
980 else
981 {
982 op->name = shstr_potion;
983 op->name_pl = shstr_potions;
984 }
985
986 if (!(flags & GT_ONLY_GOOD) && rndm (2))
987 op->set_flag (FLAG_CURSED);
988
989 break;
990 }
991
992 case AMULET:
993 if (IS_ARCH (op->arch, amulet))
994 op->value *= 5; /* Since it's not just decoration */
995
996 case RING:
997 if (!IS_ARCH (op->arch, ring) && !IS_ARCH (op->arch, amulet)) /* It's a special artifact! */
998 break;
999
1000 if (!(flags & GT_ONLY_GOOD) && !(rndm (3)))
1001 op->set_flag (FLAG_CURSED);
1002
1003 set_ring_bonus (op, op->flag [FLAG_CURSED] ? -DICE2 : DICE2);
1004
1005 if (op->type != RING) /* Amulets have only one ability */
1006 break;
1007
1008 if (!rndm (4))
1009 {
1010 int d = (rndm (2) || op->flag [FLAG_CURSED]) ? -DICE2 : DICE2;
1011
1012 if (d > 0)
1013 op->value *= 3;
1014
1015 set_ring_bonus (op, d);
1016
1017 if (!rndm (4))
1018 {
1019 int d = (rndm (3) || op->flag [FLAG_CURSED]) ? -DICE2 : DICE2;
1020
1021 if (d > 0)
1022 op->value *= 5;
1023
1024 set_ring_bonus (op, d);
1025 }
1026 }
1027
1028 if (op->animation_id)
1029 op->set_anim_frame (rndm (op->anim_frames ()));
1030
1031 break;
1032
1033 case BOOK:
1034 /* Is it an empty book?, if yes lets make a special·
1035 * msg for it, and tailor its properties based on the·
1036 * creator and/or map level we found it on.
1037 */
1038 if (!op->msg && rndm (10))
1039 {
1040 /* set the book level properly */
1041 if (creator->level == 0 || creator->flag [FLAG_ALIVE])
1042 {
1043 if (op->map && op->map->difficulty)
1044 op->level = rndm (op->map->difficulty) + rndm (10) + 1;
1045 else
1046 op->level = rndm (20) + 1;
1047 }
1048 else
1049 op->level = rndm (creator->level);
1050
1051 tailor_readable_ob (op, (creator && creator->stats.sp) ? creator->stats.sp : -1);
1052 /* books w/ info are worth more! */
1053 op->value *= ((op->level > 10 ? op->level : (op->level + 1) / 2) * ((strlen (op->msg) / 250) + 1));
1054
1055 /* add exp so reading it gives xp (once) */
1056 op->stats.exp = op->value > 10000 ? op->value / 5 : op->value / 10;
1057 }
1058
1059 /* creator related stuff */
1060
1061 /* for library, chained books. Note that some monsters have no_pick
1062 * set - we don't want to set no pick in that case.
1063 */
1064 if (creator->flag [FLAG_NO_PICK] && !creator->flag [FLAG_MONSTER])
1065 op->set_flag (FLAG_NO_PICK);
1066 if (creator->slaying && !op->slaying) /* for check_inv floors */
1067 op->slaying = creator->slaying;
1068 break;
1069
1070 case SPELLBOOK:
1071 op->value *= value_factor_from_spell_item (op->inv, 0);
1072
1073 /* add exp so learning gives xp */
1074 op->level = op->inv->level;
1075 op->stats.exp = op->value;
1076 break;
1077
1078 case WAND:
1079 /* nrof in the treasure list is number of charges,
1080 * not number of wands. So copy that into food (charges),
1081 * and reset nrof.
1082 */
1083 op->stats.food = op->inv->nrof;
1084 op->nrof = 1;
1085 /* If the spell changes by level, choose a random level
1086 * for it.
1087 */
1088 if (op->inv->duration_modifier
1089 || op->inv->dam_modifier
1090 || op->inv->range_modifier)
1091 op->level = level_for_item (op, difficulty);
1092 else
1093 op->level = op->inv->level;
1094
1095 op->value *= value_factor_from_spell_item (op->inv, op);
1096 break;
1097
1098 case ROD:
1099 op->level = level_for_item (op, difficulty);
1100 op->value *= value_factor_from_spell_item (op->inv, op);
1101
1102 /* maxhp is used to denote how many 'charges' the rod holds before */
1103 if (op->stats.maxhp)
1104 op->stats.maxhp *= max (op->inv->stats.sp, op->inv->stats.grace);
1105 else
1106 op->stats.maxhp = 2 * max (op->inv->stats.sp, op->inv->stats.grace);
1107
1108 op->stats.hp = op->stats.maxhp;
1109 break;
1110
1111 case SCROLL:
1112 op->level = level_for_item (op, difficulty);
1113 op->value *= value_factor_from_spell_item (op->inv, op);
1114
1115 /* add exp so reading them properly gives xp */
1116 op->stats.exp = op->value / 5;
1117 op->nrof = op->inv->nrof;
1118 break;
1119
1120 case RUNE:
1121 trap_adjust (op, difficulty);
1122 break;
1123
1124 case TRAP:
1125 trap_adjust (op, difficulty);
1126 break;
1127 } /* switch type */
1128
1129 if (flags & GT_STARTEQUIP)
1130 {
1131 if (op->nrof < 2 && op->type != CONTAINER && op->type != MONEY && !op->flag [FLAG_IS_THROWN])
1132 op->set_flag (FLAG_STARTEQUIP);
1133 else if (op->type != MONEY)
1134 op->value = 0;
1135 }
1136
1137 if (!(flags & GT_ENVIRONMENT))
1138 fix_flesh_item (op, creator);
1139 }
1140
1141 /*
1142 *
1143 *
1144 * CODE DEALING WITH ARTIFACTS STARTS HERE
1145 *
1146 *
1147 */
1148
1149 /*
1150 * Allocate and return the pointer to an empty artifactlist structure.
1151 */
1152 static artifactlist *
1153 get_empty_artifactlist ()
1154 {
1155 return salloc0<artifactlist> ();
1156 }
1157
1158 /*
1159 * Allocate and return the pointer to an empty artifact structure.
1160 */
1161 static artifact *
1162 get_empty_artifact ()
1163 {
1164 return salloc0<artifact> ();
1165 }
1166
1167 /*
1168 * Searches the artifact lists and returns one that has the same type
1169 * of objects on it.
1170 */
1171 artifactlist *
1172 find_artifactlist (int type)
1173 {
1174 for (artifactlist *al = first_artifactlist; al; al = al->next)
1175 if (al->type == type)
1176 return al;
1177
1178 return 0;
1179 }
1180
1181 /*
1182 * Builds up the lists of artifacts from the file in the libdir.
1183 */
1184 void
1185 init_artifacts ()
1186 {
1187 static int has_been_inited = 0;
1188 artifact *art = NULL;
1189 artifactlist *al;
1190
1191 if (has_been_inited)
1192 return;
1193 else
1194 has_been_inited = 1;
1195
1196 object_thawer f (settings.datadir, "artifacts");
1197
1198 if (!f)
1199 return;
1200
1201 for (;;)
1202 {
1203 switch (f.kw)
1204 {
1205 case KW_allowed:
1206 if (!art)
1207 art = get_empty_artifact ();
1208
1209 {
1210 if (!strcmp (f.get_str (), "all"))
1211 break;
1212
1213 const char *cp = f.get_str ();
1214 char *next;
1215 do
1216 {
1217 if ((next = (char *)strchr (cp, ',')))
1218 *next++ = '\0';
1219
1220 linked_char *tmp = new linked_char;
1221
1222 tmp->name = cp;
1223 tmp->next = art->allowed;
1224 art->allowed = tmp;
1225 }
1226 while ((cp = next));
1227 }
1228 break;
1229
1230 case KW_chance:
1231 f.get (art->chance);
1232 break;
1233
1234 case KW_difficulty:
1235 f.get (art->difficulty);
1236 break;
1237
1238 case KW_object:
1239 {
1240 art->item = object::create ();
1241 f.get (art->item->name);
1242 f.next ();
1243
1244 if (!art->item->parse_kv (f))
1245 LOG (llevError, "Init_Artifacts: Could not load object.\n");
1246
1247 al = find_artifactlist (art->item->type);
1248
1249 if (!al)
1250 {
1251 al = get_empty_artifactlist ();
1252 al->type = art->item->type;
1253 al->next = first_artifactlist;
1254 first_artifactlist = al;
1255 }
1256
1257 art->next = al->items;
1258 al->items = art;
1259 art = 0;
1260 }
1261 continue;
1262
1263 case KW_EOF:
1264 goto done;
1265
1266 default:
1267 if (!f.parse_error ("artifacts file"))
1268 cleanup ("artifacts file required");
1269 break;
1270 }
1271
1272 f.next ();
1273 }
1274
1275 done:
1276 for (al = first_artifactlist; al; al = al->next)
1277 {
1278 al->total_chance = 0;
1279
1280 for (art = al->items; art; art = art->next)
1281 {
1282 if (!art->chance)
1283 LOG (llevError, "Warning: artifact with no chance: %s\n", &art->item->name);
1284 else
1285 al->total_chance += art->chance;
1286 }
1287 #if 0
1288 LOG (llevDebug, "Artifact list type %d has %d total chance\n", al->type, al->total_chance);
1289 #endif
1290 }
1291 }
1292
1293 /*
1294 * Used in artifact generation. The bonuses of the first object
1295 * is modified by the bonuses of the second object.
1296 */
1297 void
1298 add_abilities (object *op, object *change)
1299 {
1300 if (change->face != blank_face)
1301 op->face = change->face;
1302
1303 for (int i = 0; i < NUM_STATS; i++)
1304 change_attr_value (&(op->stats), i, change->stats.stat (i));
1305
1306 op->attacktype |= change->attacktype;
1307 op->path_attuned |= change->path_attuned;
1308 op->path_repelled |= change->path_repelled;
1309 op->path_denied |= change->path_denied;
1310 op->move_type |= change->move_type;
1311 op->stats.luck += change->stats.luck;
1312
1313 static const struct copyflags : object::flags_t
1314 {
1315 copyflags ()
1316 {
1317 set (FLAG_CURSED);
1318 set (FLAG_DAMNED);
1319 set (FLAG_LIFESAVE);
1320 set (FLAG_REFL_SPELL);
1321 set (FLAG_STEALTH);
1322 set (FLAG_XRAYS);
1323 set (FLAG_BLIND);
1324 set (FLAG_SEE_IN_DARK);
1325 set (FLAG_REFL_MISSILE);
1326 set (FLAG_MAKE_INVIS);
1327 }
1328 } copyflags;
1329
1330 // we might want to just copy, but or'ing is what the original code did
1331 op->flag |= change->flag & copyflags;
1332
1333 if ((change->flag [FLAG_CURSED] || change->flag [FLAG_DAMNED]) && op->magic > 0)
1334 set_abs_magic (op, -op->magic);
1335
1336 if (change->flag [FLAG_STAND_STILL])
1337 {
1338 op->clr_flag (FLAG_ANIMATE);
1339
1340 /* so artifacts will join */
1341 if (!op->flag [FLAG_ALIVE])
1342 op->speed = 0.;
1343
1344 op->set_speed (op->speed);
1345 }
1346
1347 if (change->nrof)
1348 op->nrof = rndm (change->nrof) + 1;
1349
1350 op->stats.exp += change->stats.exp; /* Speed modifier */
1351 op->stats.wc += change->stats.wc;
1352 op->stats.ac += change->stats.ac;
1353
1354 if (change->other_arch)
1355 {
1356 /* Basically, for horns & potions, the other_arch field is the spell
1357 * to cast. So convert that to into a spell and put it into
1358 * this object.
1359 */
1360 if (op->type == HORN || op->type == POTION)
1361 {
1362 /* Remove any spells this object currently has in it */
1363 op->destroy_inv (false);
1364
1365 object *tmp = change->other_arch->instance ();
1366 insert_ob_in_ob (tmp, op);
1367 }
1368
1369 /* No harm setting this for potions/horns */
1370 op->other_arch = change->other_arch;
1371 }
1372
1373 if (change->stats.hp < 0)
1374 op->stats.hp = -change->stats.hp;
1375 else
1376 op->stats.hp += change->stats.hp;
1377
1378 if (change->stats.maxhp < 0)
1379 op->stats.maxhp = -change->stats.maxhp;
1380 else
1381 op->stats.maxhp += change->stats.maxhp;
1382
1383 if (change->stats.sp < 0)
1384 op->stats.sp = -change->stats.sp;
1385 else
1386 op->stats.sp += change->stats.sp;
1387
1388 if (change->stats.maxsp < 0)
1389 op->stats.maxsp = -change->stats.maxsp;
1390 else
1391 op->stats.maxsp += change->stats.maxsp;
1392
1393 if (change->stats.food < 0)
1394 op->stats.food = -change->stats.food;
1395 else
1396 op->stats.food += change->stats.food;
1397
1398 if (change->level < 0)
1399 op->level = -change->level;
1400 else
1401 op->level += change->level;
1402
1403 if (change->gen_sp_armour < 0)
1404 op->gen_sp_armour = -change->gen_sp_armour;
1405 else
1406 op->gen_sp_armour = op->gen_sp_armour * (int)change->gen_sp_armour / 100;
1407
1408 op->item_power = change->item_power;
1409
1410 for (int i = 0; i < NROFATTACKS; i++)
1411 op->resist[i] += change->resist[i];
1412
1413 if (change->stats.dam)
1414 {
1415 if (change->stats.dam < 0)
1416 op->stats.dam = -change->stats.dam;
1417 else if (op->stats.dam)
1418 {
1419 int tmp = op->stats.dam * change->stats.dam / 10;
1420
1421 if (tmp == op->stats.dam)
1422 {
1423 if (change->stats.dam < 10)
1424 op->stats.dam--;
1425 else
1426 op->stats.dam++;
1427 }
1428 else
1429 op->stats.dam = tmp;
1430 }
1431 }
1432
1433 if (change->weight)
1434 {
1435 if (change->weight < 0)
1436 op->weight = -change->weight;
1437 else
1438 op->weight = op->weight * change->weight / 100;
1439 }
1440
1441 if (change->last_sp)
1442 {
1443 if (change->last_sp < 0)
1444 op->last_sp = -change->last_sp;
1445 else
1446 op->last_sp = op->last_sp * (int)change->last_sp / 100;
1447 }
1448
1449 if (change->gen_sp_armour)
1450 {
1451 if (change->gen_sp_armour < 0)
1452 op->gen_sp_armour = -change->gen_sp_armour;
1453 else
1454 op->gen_sp_armour = op->gen_sp_armour * (int)change->gen_sp_armour / 100;
1455 }
1456
1457 op->value *= change->value;
1458
1459 if (change->materials)
1460 op->materials = change->materials;
1461
1462 if (change->material != MATERIAL_NULL)
1463 op->material = change->material;
1464
1465 if (change->slaying)
1466 op->slaying = change->slaying;
1467
1468 if (change->race)
1469 op->race = change->race;
1470
1471 if (change->msg)
1472 op->msg = change->msg;
1473 }
1474
1475 static int
1476 legal_artifact_combination (object *op, artifact *art)
1477 {
1478 int neg, success = 0;
1479 linked_char *tmp;
1480 const char *name;
1481
1482 if (!art->allowed)
1483 return 1; /* Ie, "all" */
1484
1485 for (tmp = art->allowed; tmp; tmp = tmp->next)
1486 {
1487 #ifdef TREASURE_VERBOSE
1488 LOG (llevDebug, "legal_art: %s\n", &tmp->name);
1489 #endif
1490 if (*tmp->name == '!')
1491 name = tmp->name + 1, neg = 1;
1492 else
1493 name = tmp->name, neg = 0;
1494
1495 /* If we match name, then return the opposite of 'neg' */
1496 if (!strcmp (name, op->arch->archname))
1497 return !neg;
1498
1499 /* Set success as true, since if the match was an inverse, it means
1500 * everything is allowed except what we match
1501 */
1502 else if (neg)
1503 success = 1;
1504 }
1505
1506 return success;
1507 }
1508
1509 /*
1510 * Fixes the given object, giving it the abilities and titles
1511 * it should have due to the second artifact-template.
1512 */
1513
1514 void
1515 give_artifact_abilities (object *op, object *artifct)
1516 {
1517 op->title = format ("of %s", &artifct->name);
1518
1519 add_abilities (op, artifct); /* Give out the bonuses */
1520
1521 #if 0 /* Bit verbose, but keep it here until next time I need it... */
1522 {
1523 char identified = op->flag [FLAG_IDENTIFIED];
1524
1525 op->set_flag (FLAG_IDENTIFIED);
1526 LOG (llevDebug, "Generated artifact %s %s [%s]\n", op->name, op->title, describe_item (op, NULL));
1527 if (!identified)
1528 op->clr_flag (FLAG_IDENTIFIED);
1529 }
1530 #endif
1531 return;
1532 }
1533
1534 /*
1535 * Decides randomly which artifact the object should be
1536 * turned into. Makes sure that the item can become that
1537 * artifact (means magic, difficulty, and Allowed fields properly).
1538 * Then calls give_artifact_abilities in order to actually create
1539 * the artifact.
1540 */
1541
1542 /* Give 1 re-roll attempt per artifact */
1543 #define ARTIFACT_TRIES 2
1544
1545 void
1546 generate_artifact (object *op, int difficulty)
1547 {
1548 artifact *art;
1549
1550 artifactlist *al = find_artifactlist (op->type);
1551
1552 if (al == NULL)
1553 {
1554 #if 0 /* This is too verbose, usually */
1555 LOG (llevDebug, "Couldn't change %s into artifact - no table.\n", op->name);
1556 #endif
1557 return;
1558 }
1559
1560 for (int i = 0; i < ARTIFACT_TRIES; i++)
1561 {
1562 int roll = rndm (al->total_chance);
1563
1564 for (art = al->items; art; art = art->next)
1565 {
1566 roll -= art->chance;
1567 if (roll < 0)
1568 break;
1569 }
1570
1571 if (art == NULL || roll >= 0)
1572 {
1573 #if 1
1574 LOG (llevError, "Got null entry and non zero roll in generate_artifact, type %d\n", op->type);
1575 #endif
1576 return;
1577 }
1578
1579 if (art->item->name == shstr_NONE)
1580 return;
1581
1582 if (fabs (op->magic) < art->item->magic)
1583 continue; /* Not magic enough to be this item */
1584
1585 /* Map difficulty not high enough */
1586 if (difficulty < art->difficulty)
1587 continue;
1588
1589 if (!legal_artifact_combination (op, art))
1590 {
1591 #ifdef TREASURE_VERBOSE
1592 LOG (llevDebug, "%s of %s was not a legal combination.\n", &op->name, &art->item->name);
1593 #endif
1594 continue;
1595 }
1596
1597 give_artifact_abilities (op, art->item);
1598 return;
1599 }
1600 }
1601
1602 /* fix_flesh_item() - objects of type FLESH are similar to type
1603 * FOOD, except they inherit properties (name, food value, etc).
1604 * based on the original owner (or 'donor' if you like). -b.t.
1605 */
1606
1607 void
1608 fix_flesh_item (object *item, object *donor)
1609 {
1610 if (item->type == FLESH && donor)
1611 {
1612 /* change the name */
1613 item->name = format ("%s's %s", &donor->name, &item->name);
1614 item->name_pl = format ("%s's %s", &donor->name, &item->name_pl);
1615
1616 /* weight is FLESH weight/100 * donor */
1617 item->weight = max (1, item->weight * donor->weight / 100);
1618
1619 /* value is multiplied by level of donor */
1620 item->value *= isqrt (donor->level * 2);
1621
1622 /* food value */
1623 item->stats.food += donor->stats.hp / 100 + donor->stats.Con;
1624
1625 /* flesh items inherit some abilities of donor, but not
1626 * full effect.
1627 */
1628 for (int i = 0; i < NROFATTACKS; i++)
1629 item->resist[i] = donor->resist[i] / 2;
1630
1631 /* item inherits donor's level (important for quezals) */
1632 item->level = donor->level;
1633
1634 /* if donor has some attacktypes, the flesh is poisonous */
1635 if (donor->attacktype & AT_POISON)
1636 item->type = POISON;
1637
1638 if (donor->attacktype & AT_ACID)
1639 item->stats.hp = -item->stats.food;
1640
1641 item->set_flag (FLAG_NO_STEAL);
1642 }
1643 }
1644
1645 static void
1646 free_treasurestruct (treasure *t)
1647 {
1648 if (t->next) free_treasurestruct (t->next);
1649 if (t->next_yes) free_treasurestruct (t->next_yes);
1650 if (t->next_no) free_treasurestruct (t->next_no);
1651
1652 delete t;
1653 }
1654
1655 static void
1656 free_charlinks (linked_char *lc)
1657 {
1658 if (lc->next)
1659 free_charlinks (lc->next);
1660
1661 delete lc;
1662 }
1663
1664 static void
1665 free_artifact (artifact *at)
1666 {
1667 if (at->next) free_artifact (at->next);
1668 if (at->allowed) free_charlinks (at->allowed);
1669
1670 at->item->destroy ();
1671
1672 sfree (at);
1673 }
1674