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

File Contents

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