ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/treasure.C
Revision: 1.118
Committed: Fri Nov 18 04:44:13 2016 UTC (7 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.117: +3 -1 lines
Log Message:
*** empty log message ***

File Contents

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