ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/treasure.C
Revision: 1.88
Committed: Fri Nov 6 12:27:05 2009 UTC (14 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.87: +32 -30 lines
Log Message:
remove all protos from include/*proto.h for functions that are effectively static

File Contents

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