ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/treasure.C
Revision: 1.43
Committed: Mon Apr 16 11:09:30 2007 UTC (17 years, 2 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.42: +1 -4 lines
Log Message:
load archetypes and treasures from perl, make terasures reloadable

File Contents

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