ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/treasure.C
Revision: 1.125
Committed: Sat Oct 8 21:54:05 2022 UTC (19 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.124: +4 -1 lines
Log Message:
*** empty log message ***

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