ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/treasure.C
Revision: 1.72
Committed: Sun Apr 20 00:44:12 2008 UTC (16 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.71: +21 -12 lines
Log Message:
reloadable archetypes, maybe

File Contents

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