ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/treasure.C
Revision: 1.121
Committed: Sat Nov 17 23:40:00 2018 UTC (5 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.120: +1 -0 lines
Log Message:
copyright update 2018

File Contents

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