ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/treasure.C
Revision: 1.121
Committed: Sat Nov 17 23:40:00 2018 UTC (5 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.120: +1 -0 lines
Log Message:
copyright update 2018

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