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

# User Rev Content
1 elmex 1.1 /*
2 pippijn 1.32 * 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 elmex 1.1
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 root 1.16
34 root 1.42 //#define TREASURE_VERBOSE
35 root 1.7
36 elmex 1.1 #include <global.h>
37     #include <treasure.h>
38     #include <funcpoint.h>
39     #include <loader.h>
40    
41 root 1.41 extern char *spell_mapping[];
42    
43     static treasurelist *first_treasurelist;
44 elmex 1.1
45 root 1.16 static void change_treasure (treasure *t, object *op); /* overrule default values */
46 root 1.41
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 elmex 1.1
58     /*
59     * Initialize global archtype pointers:
60     */
61 root 1.7 void
62     init_archetype_pointers ()
63     {
64 elmex 1.1 int prev_warn = warn_archetypes;
65 root 1.16
66 elmex 1.1 warn_archetypes = 1;
67 root 1.41
68 elmex 1.1 if (ring_arch == NULL)
69 root 1.18 ring_arch = archetype::find ("ring");
70 elmex 1.1 if (amulet_arch == NULL)
71 root 1.18 amulet_arch = archetype::find ("amulet");
72 elmex 1.1 if (staff_arch == NULL)
73 root 1.18 staff_arch = archetype::find ("staff");
74 elmex 1.1 if (crown_arch == NULL)
75 root 1.18 crown_arch = archetype::find ("crown");
76 root 1.41
77 elmex 1.1 warn_archetypes = prev_warn;
78     }
79    
80     /*
81 root 1.41 * Searches for the given treasurelist in the globally linked list
82     * of treasurelists which has been built by load_treasures().
83 elmex 1.1 */
84 root 1.41 treasurelist *
85     treasurelist::find (const char *name)
86     {
87     if (!name)
88     return 0;
89    
90     AUTODECL (i, tl_map.find (name));
91 elmex 1.1
92 root 1.41 if (i == tl_map.end ())
93     return 0;
94    
95     return i->second;
96 elmex 1.1 }
97    
98     /*
99 root 1.41 * Searches for the given treasurelist in the globally linked list
100     * of treasurelists which has been built by load_treasures().
101 elmex 1.1 */
102 root 1.41 treasurelist *
103     treasurelist::get (const char *name)
104 root 1.7 {
105 root 1.41 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 root 1.7
115 root 1.41 tl_map.insert (std::make_pair (tl->name, tl));
116     }
117 elmex 1.1
118 root 1.41 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 elmex 1.1 }
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 root 1.7 static treasure *
137 root 1.42 load_treasure (object_thawer &f)
138 root 1.7 {
139 root 1.41 treasure *t = new treasure;
140 root 1.7 int value;
141    
142     nroftreasures++;
143 root 1.42
144     for (;;)
145 root 1.7 {
146 root 1.42 coroapi::cede_every (10);
147 root 1.7
148 root 1.42 f.next ();
149    
150     switch (f.kw)
151 root 1.16 {
152 root 1.42 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 root 1.16 }
181 elmex 1.1 }
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 root 1.7 static void
189 root 1.16 check_treasurelist (const treasure *t, const treasurelist * tl)
190 elmex 1.1 {
191 root 1.7 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 root 1.41
194 root 1.7 if (t->next)
195     check_treasurelist (t->next, tl);
196 root 1.41
197 root 1.7 if (t->next_yes)
198     check_treasurelist (t->next_yes, tl);
199 root 1.41
200 root 1.7 if (t->next_no)
201     check_treasurelist (t->next_no, tl);
202 elmex 1.1 }
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 root 1.42 bool
210 root 1.43 load_treasure_file (const char *filename)
211 root 1.7 {
212     treasure *t;
213     int comp, line = 0;
214    
215 root 1.42 object_thawer f (filename);
216    
217     if (!f)
218 root 1.7 {
219     LOG (llevError, "Can't open treasure file.\n");
220 root 1.42 return false;
221 root 1.7 }
222 root 1.42
223     f.next ();
224    
225     for (;;)
226 root 1.7 {
227 root 1.42 switch (f.kw)
228 root 1.16 {
229 root 1.42 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 root 1.16
238 root 1.42 if (!tl->items)
239     return false;
240 root 1.16
241 root 1.42 /* 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 root 1.16 {
247 root 1.42 for (t = tl->items; t; t = t->next)
248     {
249 elmex 1.1 #ifdef TREASURE_DEBUG
250 root 1.42 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 root 1.16 }
258     }
259     }
260 root 1.42 break;
261 elmex 1.1
262 root 1.42 case KW_EOF:
263 elmex 1.1 #ifdef TREASURE_DEBUG
264 root 1.42 /* 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 elmex 1.1 #endif
272 root 1.42 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 elmex 1.1 }
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 root 1.7 static void
297 root 1.16 put_treasure (object *op, object *creator, int flags)
298 elmex 1.1 {
299 root 1.7 object *tmp;
300 elmex 1.1
301 root 1.7 /* 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 root 1.29 op->insert_at (creator, creator, INS_NO_MERGE | INS_NO_WALK_ON);
310 root 1.7 }
311     else
312     {
313 root 1.29 op = creator->insert (op);
314 root 1.27
315 root 1.7 if ((flags & GT_APPLY) && QUERY_FLAG (creator, FLAG_MONSTER))
316 root 1.16 monster_check_apply (creator, op);
317 root 1.27
318     if ((flags & GT_UPDATE_INV) && (tmp = creator->in_player ()))
319 root 1.16 esrv_send_item (tmp, op);
320 elmex 1.1 }
321     }
322    
323     /* if there are change_xxx commands in the treasure, we include the changes
324     * in the generated object
325     */
326 root 1.7 static void
327 root 1.12 change_treasure (treasure *t, object *op)
328 elmex 1.1 {
329 root 1.7 /* CMD: change_name xxxx */
330     if (t->change_arch.name)
331 elmex 1.1 {
332 root 1.7 op->name = t->change_arch.name;
333     op->name_pl = t->change_arch.name;
334 elmex 1.1 }
335    
336 root 1.7 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 root 1.41 static void
344 root 1.12 create_all_treasures (treasure *t, object *op, int flag, int difficulty, int tries)
345 root 1.7 {
346 root 1.33 if ((int) t->chance >= 100 || (rndm (100) + 1) < (int) t->chance)
347 elmex 1.1 {
348 root 1.7 if (t->name)
349 root 1.16 {
350 elmex 1.30 if (difficulty >= t->magic)
351 root 1.41 create_treasure (treasurelist::find (t->name), op, flag, difficulty, tries);
352 root 1.16 }
353 root 1.7 else
354 root 1.16 {
355 elmex 1.30 if (t->item && (t->item->clone.invisible != 0 || !(flag & GT_INVISIBLE)))
356 root 1.16 {
357 root 1.41 object *tmp = arch_to_object (t->item);
358    
359 root 1.16 if (t->nrof && tmp->nrof <= 1)
360 root 1.35 tmp->nrof = rndm (t->nrof) + 1;
361    
362 root 1.16 fix_generated_item (tmp, op, difficulty, t->magic, flag);
363     change_treasure (t, tmp);
364     put_treasure (tmp, op, flag);
365     }
366     }
367 root 1.12
368 root 1.41 if (t->next_yes)
369 root 1.16 create_all_treasures (t->next_yes, op, flag, difficulty, tries);
370 elmex 1.1 }
371 root 1.41 else if (t->next_no)
372 root 1.7 create_all_treasures (t->next_no, op, flag, difficulty, tries);
373 root 1.12
374 root 1.41 if (t->next)
375 root 1.7 create_all_treasures (t->next, op, flag, difficulty, tries);
376 elmex 1.1 }
377    
378 root 1.41 static void
379 root 1.35 create_one_treasure (treasurelist *tl, object *op, int flag, int difficulty, int tries)
380 root 1.7 {
381 root 1.35 int value = rndm (tl->total_chance);
382 root 1.7 treasure *t;
383 elmex 1.1
384 root 1.7 if (tries++ > 100)
385     {
386     LOG (llevDebug, "create_one_treasure: tries exceeded 100, returning without making treasure\n");
387     return;
388     }
389 root 1.12
390 root 1.41 for (t = tl->items; t; t = t->next)
391 root 1.7 {
392     value -= t->chance;
393 root 1.12
394 root 1.7 if (value < 0)
395 root 1.16 break;
396 root 1.7 }
397 elmex 1.1
398 root 1.7 if (!t || value >= 0)
399 root 1.41 cleanup ("create_one_treasure: got null object or not able to find treasure\n", 1);
400 root 1.12
401 root 1.7 if (t->name)
402     {
403     if (difficulty >= t->magic)
404 elmex 1.26 {
405 root 1.41 treasurelist *tl = treasurelist::find (t->name);
406 elmex 1.26 if (tl)
407     create_treasure (tl, op, flag, difficulty, tries);
408     }
409 root 1.7 else if (t->nrof)
410 root 1.16 create_one_treasure (tl, op, flag, difficulty, tries);
411 elmex 1.1 }
412 root 1.41 else if (t->item && (t->item->clone.invisible != 0 || flag != GT_INVISIBLE))
413 root 1.7 {
414 root 1.41 if (object *tmp = arch_to_object (t->item))
415     {
416     if (t->nrof && tmp->nrof <= 1)
417     tmp->nrof = rndm (t->nrof) + 1;
418 root 1.12
419 root 1.41 fix_generated_item (tmp, op, difficulty, t->magic, flag);
420     change_treasure (t, tmp);
421     put_treasure (tmp, op, flag);
422     }
423 elmex 1.1 }
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 root 1.7 void
434 elmex 1.30 create_treasure (treasurelist *tl, object *op, int flag, int difficulty, int tries)
435 elmex 1.1 {
436 root 1.41 // empty treasurelists are legal
437     if (!tl->items)
438     return;
439    
440 root 1.7 if (tries++ > 100)
441     {
442     LOG (llevDebug, "createtreasure: tries exceeded 100, returning without making treasure\n");
443     return;
444     }
445 root 1.37
446 elmex 1.30 if (tl->total_chance)
447     create_one_treasure (tl, op, flag, difficulty, tries);
448 root 1.7 else
449 elmex 1.30 create_all_treasures (tl->items, op, flag, difficulty, tries);
450 elmex 1.1 }
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 root 1.7 object *
458 elmex 1.30 generate_treasure (treasurelist *tl, int difficulty)
459 elmex 1.1 {
460 root 1.25 difficulty = clamp (difficulty, 1, settings.max_level);
461    
462 root 1.24 object *ob = object::create (), *tmp;
463 elmex 1.1
464 elmex 1.30 create_treasure (tl, ob, 0, difficulty, 0);
465 elmex 1.1
466 root 1.7 /* Don't want to free the object we are about to return */
467     tmp = ob->inv;
468     if (tmp != NULL)
469 root 1.23 tmp->remove ();
470 root 1.21
471 root 1.7 if (ob->inv)
472 root 1.21 LOG (llevError, "In generate treasure, created multiple objects.\n");
473    
474 root 1.24 ob->destroy ();
475 root 1.7 return tmp;
476 elmex 1.1 }
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 root 1.7 static int difftomagic_list[DIFFLEVELS][MAXMAGIC + 1] = {
486 root 1.16
487 root 1.42 // 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 elmex 1.1 };
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 root 1.7 int
531 root 1.16 level_for_item (const object *op, int difficulty)
532 elmex 1.1 {
533 pippijn 1.14 int olevel = 0;
534 elmex 1.1
535     if (!op->inv)
536     {
537 root 1.7 LOG (llevError, "level_for_item: Object %s has no inventory!\n", &op->name);
538     return 0;
539 elmex 1.1 }
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 root 1.7 int
563     magic_from_difficulty (int difficulty)
564 elmex 1.1 {
565     int percent = 0, magic = 0;
566     int scaled_diff = (int) (((double) difficulty / settings.max_level) * DIFFLEVELS);
567    
568     scaled_diff--;
569    
570 root 1.7 if (scaled_diff < 0)
571 elmex 1.1 scaled_diff = 0;
572    
573     if (scaled_diff >= DIFFLEVELS)
574 root 1.7 scaled_diff = DIFFLEVELS - 1;
575 elmex 1.1
576 root 1.33 percent = rndm (100);
577 elmex 1.1
578 root 1.7 for (magic = 0; magic < (MAXMAGIC + 1); magic++)
579 elmex 1.1 {
580     percent -= difftomagic_list[scaled_diff][magic];
581    
582     if (percent < 0)
583 root 1.16 break;
584 elmex 1.1 }
585    
586     if (magic == (MAXMAGIC + 1))
587     {
588 root 1.7 LOG (llevError, "Warning, table for difficulty (scaled %d) %d bad.\n", scaled_diff, difficulty);
589 elmex 1.1 magic = 0;
590     }
591    
592 root 1.33 magic = (rndm (3)) ? magic : -magic;
593 elmex 1.1 /* 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 root 1.7 void
606 root 1.16 set_abs_magic (object *op, int magic)
607 root 1.7 {
608     if (!magic)
609 elmex 1.1 return;
610    
611 root 1.7 op->magic = magic;
612     if (op->arch)
613     {
614     if (op->type == ARMOUR)
615 root 1.16 ARMOUR_SPEED (op) = (ARMOUR_SPEED (&op->arch->clone) * (100 + magic * 10)) / 100;
616 root 1.7
617 root 1.33 if (magic < 0 && !(rndm (3))) /* You can't just check the weight always */
618 root 1.16 magic = (-magic);
619 root 1.7 op->weight = (op->arch->clone.weight * (100 - magic * 10)) / 100;
620     }
621     else
622     {
623     if (op->type == ARMOUR)
624 root 1.16 ARMOUR_SPEED (op) = (ARMOUR_SPEED (op) * (100 + magic * 10)) / 100;
625 root 1.33 if (magic < 0 && !(rndm (3))) /* You can't just check the weight always */
626 root 1.16 magic = (-magic);
627 root 1.7 op->weight = (op->weight * (100 - magic * 10)) / 100;
628     }
629 elmex 1.1 }
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 root 1.7 static void
637 root 1.16 set_magic (int difficulty, object *op, int max_magic, int flags)
638 elmex 1.1 {
639     int i;
640 root 1.16
641 root 1.7 i = magic_from_difficulty (difficulty);
642 elmex 1.1 if ((flags & GT_ONLY_GOOD) && i < 0)
643 root 1.7 i = -i;
644     if (i > max_magic)
645 elmex 1.1 i = max_magic;
646 root 1.7 set_abs_magic (op, i);
647 elmex 1.1 if (i < 0)
648 root 1.7 SET_FLAG (op, FLAG_CURSED);
649 elmex 1.1 }
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 root 1.7 void
660 root 1.16 set_ring_bonus (object *op, int bonus)
661 root 1.7 {
662 elmex 1.1
663 root 1.35 int r = rndm (bonus > 0 ? 25 : 11);
664 elmex 1.1
665 root 1.7 if (op->type == AMULET)
666     {
667 root 1.33 if (!(rndm (21)))
668     r = 20 + rndm (2);
669 root 1.7 else
670 root 1.16 {
671 root 1.35 if (rndm (2))
672 root 1.16 r = 10;
673     else
674 root 1.33 r = 11 + rndm (9);
675 root 1.16 }
676 root 1.7 }
677    
678     switch (r)
679     {
680 root 1.16 /* 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 root 1.34 int b = 5 + abs (bonus), val, resist = rndm (num_resist_table);
722 root 1.16
723     /* Roughly generate a bonus between 100 and 35 (depending on the bonus) */
724 root 1.34 val = 10 + rndm (b) + rndm (b) + rndm (b) + rndm (b);
725 root 1.16
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 root 1.35 val = 2 * -val - rndm (b);
733 root 1.16 if (val > 35)
734     val = 35; /* Upper limit */
735     b = 0;
736 root 1.34
737 root 1.16 while (op->resist[resist_table[resist]] != 0 && b < 4)
738 root 1.34 resist = rndm (num_resist_table);
739    
740 root 1.16 if (b == 4)
741     return; /* Not able to find a free resistance */
742 root 1.34
743 root 1.16 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 root 1.7 }
780 root 1.34
781 root 1.7 if (bonus > 0)
782     op->value *= 2 * bonus;
783     else
784     op->value = -(op->value * 2 * bonus) / 3;
785 elmex 1.1 }
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 root 1.7 int
796     get_magic (int diff)
797     {
798 elmex 1.1 int i;
799 root 1.16
800 root 1.7 if (diff < 3)
801     diff = 3;
802 root 1.35
803 root 1.7 for (i = 0; i < 4; i++)
804 root 1.35 if (rndm (diff))
805 root 1.7 return i;
806 root 1.35
807 elmex 1.1 return 4;
808     }
809    
810     #define DICE2 (get_magic(2)==2?2:1)
811 root 1.35 #define DICESPELL (rndm (3) + rndm (3) + rndm (3) + rndm (3) + rndm (3))
812 elmex 1.1
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 root 1.16
819 elmex 1.1 /* 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 root 1.16
824 elmex 1.1 /*
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 root 1.7 void
836 root 1.16 fix_generated_item (object *op, object *creator, int difficulty, int max_magic, int flags)
837 elmex 1.1 {
838     int was_magic = op->magic, num_enchantments = 0, save_item_power = 0;
839    
840     if (!creator || creator->type == op->type)
841 root 1.16 creator = op; /*safety & to prevent polymorphed objects giving attributes */
842 elmex 1.1
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 root 1.42 LOG (llevDebug, "fix_generated_item: Unable to generate treasure for %s\n", op->debug_desc ());
852 elmex 1.1
853     /* So the treasure doesn't get created again */
854 root 1.42 op->randomitems = 0;
855 elmex 1.1 }
856    
857     if (difficulty < 1)
858     difficulty = 1;
859    
860 root 1.22 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 elmex 1.1 if (!(flags & GT_MINIMAL))
867     {
868     if (op->arch == crown_arch)
869 root 1.16 {
870     set_magic (difficulty, op, max_magic, flags);
871     num_enchantments = calc_item_power (op, 1);
872     generate_artifact (op, difficulty);
873     }
874 elmex 1.1 else
875 root 1.16 {
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 root 1.35 if ((!was_magic && !rndm (CHANCE_FOR_ARTIFACT))
882     || op->type == HORN
883     || difficulty >= settings.max_level) /* high difficulties always generate an artifact,
884 root 1.16 * used for shop_floors or treasures */
885     generate_artifact (op, difficulty);
886     }
887 elmex 1.1
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 root 1.16 {
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 elmex 1.1 else if (save_item_power)
906 root 1.16 {
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 elmex 1.1 else
914 root 1.16 {
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 elmex 1.1 }
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 root 1.16 /* 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 elmex 1.1 }
943 root 1.16 else if (!op->title) /* Only modify object if not special */
944 elmex 1.1 switch (op->type)
945     {
946 root 1.16 case WEAPON:
947     case ARMOUR:
948     case SHIELD:
949     case HELMET:
950     case CLOAK:
951 root 1.33 if (QUERY_FLAG (op, FLAG_CURSED) && !(rndm (4)))
952 root 1.16 set_ring_bonus (op, -DICE2);
953     break;
954    
955     case BRACERS:
956 root 1.35 if (!rndm (QUERY_FLAG (op, FLAG_CURSED) ? 5 : 20))
957 root 1.16 {
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 root 1.35 op->level = op->inv->level / 2 + rndm (difficulty) + rndm (difficulty);
993 root 1.16 }
994     else
995     {
996     op->name = "potion";
997     op->name_pl = "potions";
998     }
999    
1000 root 1.33 if (!(flags & GT_ONLY_GOOD) && rndm (2))
1001 root 1.16 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 root 1.24 op->destroy ();
1013     op = 0;
1014 root 1.16 break;
1015     }
1016    
1017     if (op->arch != ring_arch && op->arch != amulet_arch) /* It's a special artifact! */
1018     break;
1019    
1020 root 1.33 if (!(flags & GT_ONLY_GOOD) && !(rndm (3)))
1021 root 1.16 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 root 1.33 if (!(rndm (4)))
1029 root 1.16 {
1030 root 1.33 int d = (rndm (2) || QUERY_FLAG (op, FLAG_CURSED)) ? -DICE2 : DICE2;
1031 root 1.16
1032     if (d > 0)
1033     op->value *= 3;
1034    
1035     set_ring_bonus (op, d);
1036    
1037 root 1.33 if (!(rndm (4)))
1038 root 1.16 {
1039 root 1.33 int d = (rndm (3) || QUERY_FLAG (op, FLAG_CURSED)) ? -DICE2 : DICE2;
1040 root 1.16
1041     if (d > 0)
1042     op->value *= 5;
1043     set_ring_bonus (op, d);
1044     }
1045     }
1046    
1047     if (GET_ANIM_ID (op))
1048 root 1.35 SET_ANIMATION (op, rndm (NUM_ANIMATIONS (op)));
1049 root 1.16
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 root 1.33 if (!op->msg && rndm (10))
1058 root 1.16 {
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 root 1.35 op->level = rndm (op->map->difficulty) + rndm (10) + 1;
1064 root 1.16 else
1065 root 1.33 op->level = rndm (20) + 1;
1066 root 1.16 }
1067     else
1068 root 1.35 op->level = rndm (creator->level);
1069 root 1.16
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 elmex 1.1
1154     if (flags & GT_STARTEQUIP)
1155     {
1156 root 1.7 if (op->nrof < 2 && op->type != CONTAINER && op->type != MONEY && !QUERY_FLAG (op, FLAG_IS_THROWN))
1157 root 1.16 SET_FLAG (op, FLAG_STARTEQUIP);
1158 elmex 1.1 else if (op->type != MONEY)
1159 root 1.16 op->value = 0;
1160 elmex 1.1 }
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 root 1.7 static artifactlist *
1178     get_empty_artifactlist (void)
1179     {
1180 root 1.41 return salloc0 <artifactlist> ();
1181 elmex 1.1 }
1182    
1183     /*
1184     * Allocate and return the pointer to an empty artifact structure.
1185     */
1186 root 1.7 static artifact *
1187     get_empty_artifact (void)
1188     {
1189 root 1.41 return salloc0 <artifact> ();
1190 elmex 1.1 }
1191    
1192     /*
1193     * Searches the artifact lists and returns one that has the same type
1194     * of objects on it.
1195     */
1196 root 1.7 artifactlist *
1197     find_artifactlist (int type)
1198     {
1199 root 1.41 for (artifactlist *al = first_artifactlist; al; al = al->next)
1200 root 1.7 if (al->type == type)
1201     return al;
1202 root 1.38
1203     return 0;
1204 elmex 1.1 }
1205    
1206     /*
1207     * For debugging purposes. Dumps all tables.
1208     */
1209 root 1.7 void
1210     dump_artifacts (void)
1211     {
1212 elmex 1.1 artifactlist *al;
1213     artifact *art;
1214     linked_char *next;
1215    
1216 root 1.7 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 root 1.16 {
1222     fprintf (logfile, "Artifact %-30s Difficulty %3d Chance %5d\n", &art->item->name, art->difficulty, art->chance);
1223     if (art->allowed != NULL)
1224     {
1225 root 1.38 fprintf (logfile, "\tallowed combinations:");
1226 root 1.16 for (next = art->allowed; next != NULL; next = next->next)
1227     fprintf (logfile, "%s,", &next->name);
1228     fprintf (logfile, "\n");
1229     }
1230     }
1231 elmex 1.1 }
1232 root 1.7 fprintf (logfile, "\n");
1233 elmex 1.1 }
1234    
1235     /*
1236     * For debugging purposes. Dumps all treasures recursively (see below).
1237     */
1238 root 1.7 void
1239 root 1.16 dump_monster_treasure_rec (const char *name, treasure *t, int depth)
1240 elmex 1.1 {
1241     treasurelist *tl;
1242 root 1.7 int i;
1243 elmex 1.1
1244     if (depth > 100)
1245     return;
1246 root 1.41
1247 elmex 1.30 while (t)
1248 elmex 1.1 {
1249 elmex 1.30 if (t->name)
1250 root 1.16 {
1251     for (i = 0; i < depth; i++)
1252     fprintf (logfile, " ");
1253 root 1.41
1254 root 1.16 fprintf (logfile, "{ (list: %s)\n", &t->name);
1255 root 1.41
1256     tl = treasurelist::find (t->name);
1257 elmex 1.26 if (tl)
1258     dump_monster_treasure_rec (name, tl->items, depth + 2);
1259 root 1.41
1260 root 1.16 for (i = 0; i < depth; i++)
1261     fprintf (logfile, " ");
1262 root 1.41
1263 root 1.16 fprintf (logfile, "} (end of list: %s)\n", &t->name);
1264     }
1265 elmex 1.1 else
1266 root 1.16 {
1267     for (i = 0; i < depth; i++)
1268     fprintf (logfile, " ");
1269 root 1.41
1270 elmex 1.30 if (t->item && t->item->clone.type == FLESH)
1271 root 1.16 fprintf (logfile, "%s's %s\n", name, &t->item->clone.name);
1272     else
1273     fprintf (logfile, "%s\n", &t->item->clone.name);
1274     }
1275 elmex 1.30
1276     if (t->next_yes)
1277 root 1.16 {
1278     for (i = 0; i < depth; i++)
1279     fprintf (logfile, " ");
1280 root 1.41
1281 root 1.16 fprintf (logfile, " (if yes)\n");
1282     dump_monster_treasure_rec (name, t->next_yes, depth + 1);
1283     }
1284 elmex 1.30
1285     if (t->next_no)
1286 root 1.16 {
1287     for (i = 0; i < depth; i++)
1288     fprintf (logfile, " ");
1289 root 1.41
1290 root 1.16 fprintf (logfile, " (if no)\n");
1291     dump_monster_treasure_rec (name, t->next_no, depth + 1);
1292     }
1293 elmex 1.30
1294 elmex 1.1 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 root 1.7 void
1303     dump_monster_treasure (const char *name)
1304 elmex 1.1 {
1305     archetype *at;
1306 root 1.7 int found;
1307 elmex 1.1
1308     found = 0;
1309     fprintf (logfile, "\n");
1310 root 1.38
1311 root 1.7 for (at = first_archetype; at != NULL; at = at->next)
1312     if (!strcasecmp (at->clone.name, name) && at->clone.title == NULL)
1313 elmex 1.1 {
1314 root 1.16 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 root 1.38
1320 root 1.16 fprintf (logfile, "\n");
1321     found++;
1322 elmex 1.1 }
1323 root 1.38
1324 elmex 1.1 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 root 1.7 void
1332     init_artifacts (void)
1333     {
1334     static int has_been_inited = 0;
1335 pippijn 1.39 char filename[MAX_BUF];
1336 root 1.7 artifact *art = NULL;
1337     artifactlist *al;
1338    
1339     if (has_been_inited)
1340     return;
1341     else
1342     has_been_inited = 1;
1343 elmex 1.1
1344 root 1.7 sprintf (filename, "%s/artifacts", settings.datadir);
1345 root 1.38 object_thawer f (filename);
1346 elmex 1.1
1347 root 1.38 if (!f)
1348 root 1.7 return;
1349 elmex 1.1
1350 root 1.38 f.next ();
1351    
1352     for (;;)
1353 root 1.7 {
1354 root 1.38 switch (f.kw)
1355     {
1356     case KW_allowed:
1357     if (!art)
1358     {
1359     art = get_empty_artifact ();
1360     nrofartifacts++;
1361     }
1362 root 1.7
1363 root 1.16 {
1364 root 1.38 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 root 1.16 }
1384 root 1.38 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 root 1.16
1394 root 1.38 case KW_object:
1395 root 1.16 {
1396 root 1.38 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 root 1.16 }
1415 root 1.38 continue;
1416 root 1.10
1417 root 1.38 case KW_EOF:
1418     goto done;
1419 root 1.10
1420 root 1.38 default:
1421     if (!f.parse_error ("artifacts file"))
1422     cleanup ("artifacts file required");
1423     break;
1424 root 1.16 }
1425 root 1.38
1426     f.next ();
1427 root 1.7 }
1428 root 1.3
1429 root 1.38 done:
1430     for (al = first_artifactlist; al; al = al->next)
1431 root 1.7 {
1432 root 1.38 for (art = al->items; art; art = art->next)
1433 root 1.16 {
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 elmex 1.1 #if 0
1440 root 1.7 LOG (llevDebug, "Artifact list type %d has %d total chance\n", al->type, al->total_chance);
1441 elmex 1.1 #endif
1442     }
1443    
1444 root 1.7 LOG (llevDebug, "done.\n");
1445 elmex 1.1 }
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 root 1.7 void
1454 root 1.16 add_abilities (object *op, object *change)
1455 root 1.7 {
1456 pippijn 1.14 int i, tmp;
1457 elmex 1.1
1458 root 1.7 if (change->face != blank_face)
1459     {
1460 elmex 1.1 #ifdef TREASURE_VERBOSE
1461 root 1.42 LOG (llevDebug, "add_abilities change face: %d\n", change->face);
1462 elmex 1.1 #endif
1463 root 1.7 op->face = change->face;
1464 elmex 1.1 }
1465    
1466 root 1.7 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 root 1.16 op->speed = 0.0;
1506 root 1.12
1507 root 1.28 op->set_speed (op->speed);
1508 elmex 1.1 }
1509 root 1.7
1510     if (change->nrof)
1511 root 1.35 op->nrof = rndm (change->nrof) + 1;
1512 root 1.7
1513 root 1.16 op->stats.exp += change->stats.exp; /* Speed modifier */
1514 root 1.7 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 root 1.16 {
1525     object *tmp_obj;
1526    
1527     /* Remove any spells this object currently has in it */
1528     while (op->inv)
1529 root 1.24 op->inv->destroy ();
1530 root 1.16
1531     tmp_obj = arch_to_object (change->other_arch);
1532     insert_ob_in_ob (tmp_obj, op);
1533     }
1534 root 1.7 /* 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 root 1.20 if (change->resist[i])
1577     op->resist[i] += change->resist[i];
1578 root 1.7
1579     if (change->stats.dam)
1580     {
1581     if (change->stats.dam < 0)
1582 root 1.16 op->stats.dam = (-change->stats.dam);
1583 root 1.7 else if (op->stats.dam)
1584 root 1.16 {
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 root 1.7 }
1597    
1598     if (change->weight)
1599     {
1600     if (change->weight < 0)
1601 root 1.16 op->weight = (-change->weight);
1602 root 1.7 else
1603 root 1.16 op->weight = (op->weight * (change->weight)) / 100;
1604 root 1.7 }
1605    
1606     if (change->last_sp)
1607     {
1608     if (change->last_sp < 0)
1609 root 1.16 op->last_sp = (-change->last_sp);
1610 root 1.7 else
1611 root 1.16 op->last_sp = (signed char) (((int) op->last_sp * (int) change->last_sp) / (int) 100);
1612 root 1.7 }
1613    
1614     if (change->gen_sp_armour)
1615     {
1616     if (change->gen_sp_armour < 0)
1617 root 1.16 op->gen_sp_armour = (-change->gen_sp_armour);
1618 root 1.7 else
1619 root 1.16 op->gen_sp_armour = (signed char) (((int) op->gen_sp_armour * ((int) change->gen_sp_armour)) / (int) 100);
1620 root 1.7 }
1621    
1622     op->value *= change->value;
1623    
1624 root 1.36 if (change->materials)
1625     op->materials = change->materials;
1626 root 1.7
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 elmex 1.1 }
1639    
1640 root 1.7 static int
1641 root 1.16 legal_artifact_combination (object *op, artifact * art)
1642 root 1.7 {
1643 elmex 1.1 int neg, success = 0;
1644     linked_char *tmp;
1645     const char *name;
1646    
1647     if (art->allowed == (linked_char *) NULL)
1648 root 1.16 return 1; /* Ie, "all" */
1649 root 1.7 for (tmp = art->allowed; tmp; tmp = tmp->next)
1650     {
1651 elmex 1.1 #ifdef TREASURE_VERBOSE
1652 root 1.42 LOG (llevDebug, "legal_art: %s\n", &tmp->name);
1653 elmex 1.1 #endif
1654 root 1.7 if (*tmp->name == '!')
1655 root 1.16 name = tmp->name + 1, neg = 1;
1656 root 1.7 else
1657 root 1.16 name = tmp->name, neg = 0;
1658 root 1.7
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 root 1.16 return !neg;
1662 root 1.7
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 root 1.16 success = 1;
1668 root 1.7 }
1669 elmex 1.1 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 root 1.7 void
1678 root 1.12 give_artifact_abilities (object *op, object *artifct)
1679 root 1.7 {
1680 elmex 1.1 char new_name[MAX_BUF];
1681    
1682 root 1.7 sprintf (new_name, "of %s", &artifct->name);
1683     op->title = new_name;
1684 root 1.16 add_abilities (op, artifct); /* Give out the bonuses */
1685 elmex 1.1
1686 root 1.16 #if 0 /* Bit verbose, but keep it here until next time I need it... */
1687 elmex 1.1 {
1688 root 1.7 char identified = QUERY_FLAG (op, FLAG_IDENTIFIED);
1689 root 1.16
1690 root 1.7 SET_FLAG (op, FLAG_IDENTIFIED);
1691     LOG (llevDebug, "Generated artifact %s %s [%s]\n", op->name, op->title, describe_item (op, NULL));
1692 elmex 1.1 if (!identified)
1693 root 1.7 CLEAR_FLAG (op, FLAG_IDENTIFIED);
1694 elmex 1.1 }
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 root 1.7 void
1711 root 1.16 generate_artifact (object *op, int difficulty)
1712 root 1.7 {
1713 elmex 1.1 artifactlist *al;
1714     artifact *art;
1715     int i;
1716    
1717 root 1.7 al = find_artifactlist (op->type);
1718    
1719     if (al == NULL)
1720     {
1721 root 1.16 #if 0 /* This is too verbose, usually */
1722 root 1.7 LOG (llevDebug, "Couldn't change %s into artifact - no table.\n", op->name);
1723 elmex 1.1 #endif
1724 root 1.7 return;
1725     }
1726    
1727     for (i = 0; i < ARTIFACT_TRIES; i++)
1728     {
1729 root 1.35 int roll = rndm (al->total_chance);
1730 elmex 1.1
1731 root 1.35 for (art = al->items; art; art = art->next)
1732 root 1.16 {
1733     roll -= art->chance;
1734     if (roll < 0)
1735     break;
1736     }
1737 elmex 1.1
1738 root 1.7 if (art == NULL || roll >= 0)
1739 root 1.16 {
1740 elmex 1.1 #if 1
1741 root 1.16 LOG (llevError, "Got null entry and non zero roll in generate_artifact, type %d\n", op->type);
1742 elmex 1.1 #endif
1743 root 1.16 return;
1744     }
1745 root 1.7 if (!strcmp (art->item->name, "NONE"))
1746 root 1.16 return;
1747 root 1.7 if (FABS (op->magic) < art->item->magic)
1748 root 1.16 continue; /* Not magic enough to be this item */
1749 root 1.7
1750     /* Map difficulty not high enough */
1751     if (difficulty < art->difficulty)
1752 root 1.16 continue;
1753 elmex 1.1
1754 root 1.7 if (!legal_artifact_combination (op, art))
1755 root 1.16 {
1756 elmex 1.1 #ifdef TREASURE_VERBOSE
1757 root 1.42 LOG (llevDebug, "%s of %s was not a legal combination.\n", &op->name, &art->item->name);
1758 elmex 1.1 #endif
1759 root 1.16 continue;
1760     }
1761 root 1.42
1762 root 1.7 give_artifact_abilities (op, art->item);
1763     return;
1764 elmex 1.1 }
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 root 1.7 void
1773 root 1.16 fix_flesh_item (object *item, object *donor)
1774 root 1.7 {
1775     char tmpbuf[MAX_BUF];
1776     int i;
1777    
1778     if (item->type == FLESH && donor)
1779     {
1780     /* change the name */
1781 root 1.16 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 root 1.7
1786     /* weight is FLESH weight/100 * donor */
1787     if ((item->weight = (signed long) (((double) item->weight / (double) 100.0) * (double) donor->weight)) == 0)
1788 root 1.16 item->weight = 1;
1789 root 1.7
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 root 1.16 item->resist[i] = donor->resist[i] / 2;
1801 root 1.7
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 root 1.16 item->type = POISON;
1808 root 1.7 if (donor->attacktype & AT_ACID)
1809 root 1.16 item->stats.hp = -1 * item->stats.food;
1810 root 1.7 SET_FLAG (item, FLAG_NO_STEAL);
1811 elmex 1.1 }
1812     }
1813    
1814     /* special_potion() - so that old potion code is still done right. */
1815    
1816 root 1.7 int
1817 root 1.16 special_potion (object *op)
1818 root 1.7 {
1819     if (op->attacktype)
1820     return 1;
1821 elmex 1.1
1822 root 1.7 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 elmex 1.1
1825 elmex 1.30 for (int i = 0; i < NROFATTACKS; i++)
1826 root 1.7 if (op->resist[i])
1827     return 1;
1828 elmex 1.1
1829 root 1.7 return 0;
1830 elmex 1.1 }
1831    
1832 root 1.7 void
1833 root 1.16 free_treasurestruct (treasure *t)
1834 elmex 1.1 {
1835 root 1.41 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 root 1.8
1839     delete t;
1840 elmex 1.1 }
1841    
1842 root 1.7 void
1843 root 1.16 free_charlinks (linked_char *lc)
1844 elmex 1.1 {
1845 root 1.7 if (lc->next)
1846     free_charlinks (lc->next);
1847    
1848     delete lc;
1849 elmex 1.1 }
1850    
1851 root 1.7 void
1852 root 1.41 free_artifact (artifact *at)
1853 elmex 1.1 {
1854 root 1.41 if (at->next) free_artifact (at->next);
1855     if (at->allowed) free_charlinks (at->allowed);
1856 root 1.7
1857 root 1.21 at->item->destroy (1);
1858 root 1.7
1859 root 1.41 sfree (at);
1860 elmex 1.1 }
1861    
1862 root 1.7 void
1863 root 1.41 free_artifactlist (artifactlist *al)
1864 elmex 1.1 {
1865 root 1.7 artifactlist *nextal;
1866 root 1.15
1867 root 1.21 for (al = first_artifactlist; al; al = nextal)
1868 root 1.7 {
1869     nextal = al->next;
1870 root 1.15
1871 root 1.7 if (al->items)
1872 root 1.16 free_artifact (al->items);
1873 root 1.15
1874 root 1.41 sfree (al);
1875 elmex 1.1 }
1876     }
1877    
1878 root 1.7 void
1879     free_all_treasures (void)
1880     {
1881     treasurelist *tl, *next;
1882 elmex 1.1
1883 root 1.41 for (tl = first_treasurelist; tl; tl = next)
1884     {
1885     clear (tl);
1886 elmex 1.1
1887 root 1.7 next = tl->next;
1888     delete tl;
1889 elmex 1.1 }
1890 root 1.41
1891 root 1.7 free_artifactlist (first_artifactlist);
1892 elmex 1.1 }