1 /**
2 * \file obj-make.c
3 * \brief Object generation functions.
4 *
5 * Copyright (c) 1987-2007 Angband contributors
6 *
7 * This work is free software; you can redistribute it and/or modify it
8 * under the terms of either:
9 *
10 * a) the GNU General Public License as published by the Free Software
11 * Foundation, version 2, or
12 *
13 * b) the "Angband licence":
14 * This software may be copied and distributed for educational, research,
15 * and not for profit purposes provided that this copyright and statement
16 * are included in all such copies. Other copyrights may also apply.
17 */
18
19 #include "angband.h"
20 #include "alloc.h"
21 #include "cave.h"
22 #include "effects.h"
23 #include "init.h"
24 #include "obj-chest.h"
25 #include "obj-curse.h"
26 #include "obj-gear.h"
27 #include "obj-knowledge.h"
28 #include "obj-make.h"
29 #include "obj-pile.h"
30 #include "obj-power.h"
31 #include "obj-slays.h"
32 #include "obj-tval.h"
33 #include "obj-util.h"
34
35 /**
36 * Stores cumulative probability distribution for objects at each level. The
37 * value at ilv * (z_info->k_max + 1) + itm is the probablity, out of
38 * obj_alloc[ilv * (z_info->k_max + 1) + z_info->k_max], that an item whose
39 * index is less than itm occurs at level, ilv.
40 */
41 static u32b *obj_alloc;
42
43 /**
44 * Has the same layout and interpretation as obj_alloc, but only items that
45 * are good or better contribute to the cumulative probability distribution.
46 */
47 static u32b *obj_alloc_great;
48
49 /**
50 * Store the total allocation value for each tval by level. The value at
51 * ilv * TV_MAX + tval is the total for tval at the level, ilv.
52 */
53 static u32b *obj_total_tval;
54
55 /**
56 * Same layout and interpretation as obj_total_tval, but only items that are
57 * good or better contribute.
58 */
59 static u32b *obj_total_tval_great;
60
61 static s16b alloc_ego_size = 0;
62 static alloc_entry *alloc_ego_table;
63
64 struct money {
65 char *name;
66 int type;
67 };
68
69 static struct money *money_type;
70 static int num_money_types;
71
72 /*
73 * Initialize object allocation info
74 */
alloc_init_objects(void)75 static void alloc_init_objects(void) {
76 int item, lev;
77 int k_max = z_info->k_max;
78
79 /* Allocate */
80 obj_alloc = mem_alloc((z_info->max_obj_depth + 1) * (k_max + 1) * sizeof(*obj_alloc));
81 obj_alloc_great = mem_alloc((z_info->max_obj_depth + 1) * (k_max + 1) * sizeof(*obj_alloc_great));
82 obj_total_tval = mem_zalloc((z_info->max_obj_depth + 1) * TV_MAX * sizeof(*obj_total_tval));
83 obj_total_tval_great = mem_zalloc((z_info->max_obj_depth + 1) * TV_MAX * sizeof(*obj_total_tval));
84
85 /* The cumulative chance starts at zero for each level. */
86 for (lev = 0; lev <= z_info->max_obj_depth; lev++) {
87 obj_alloc[lev * (k_max + 1)] = 0;
88 obj_alloc_great[lev * (k_max + 1)] = 0;
89 }
90
91 /* Fill the cumulative probability tables */
92 for (item = 0; item < k_max; item++) {
93 const struct object_kind *kind = &k_info[item];
94
95 int min = kind->alloc_min;
96 int max = kind->alloc_max;
97
98 /* Go through all the dungeon levels */
99 for (lev = 0; lev <= z_info->max_obj_depth; lev++) {
100 int rarity = kind->alloc_prob;
101
102 /* Add to the cumulative prob. in the standard table */
103 if ((lev < min) || (lev > max)) rarity = 0;
104 assert(rarity >= 0 && obj_alloc[(lev * (k_max + 1)) + item] <= (u32b)-1 - rarity);
105 obj_alloc[(lev * (k_max + 1)) + item + 1] =
106 obj_alloc[(lev * (k_max + 1)) + item] + rarity;
107
108 obj_total_tval[lev * TV_MAX + kind->tval] += rarity;
109
110 /* Add to the cumulative prob. in the "great" table */
111 if (!kind_is_good(kind)) rarity = 0;
112 obj_alloc_great[(lev * (k_max + 1)) + item + 1] =
113 obj_alloc_great[(lev * (k_max + 1)) + item] + rarity;
114
115 obj_total_tval_great[lev * TV_MAX + kind->tval] += rarity;
116 }
117 }
118 }
119
120 /*
121 * Initialize ego-item allocation info
122 *
123 * The ego allocation probabilities table (alloc_ego_table) is sorted in
124 * order of minimum depth. Precisely why, I'm not sure! But that is what
125 * the code below is doing with the arrays 'num' and 'level_total'. -AS
126 */
alloc_init_egos(void)127 static void alloc_init_egos(void) {
128 int *num = mem_zalloc((z_info->max_obj_depth + 1) * sizeof(int));
129 int *level_total = mem_zalloc((z_info->max_obj_depth + 1) * sizeof(int));
130
131 int i;
132
133 for (i = 0; i < z_info->e_max; i++) {
134 struct ego_item *ego = &e_info[i];
135
136 if (ego->alloc_prob) {
137 /* Count the entries */
138 alloc_ego_size++;
139
140 /* Group by level */
141 num[ego->alloc_min]++;
142 }
143 }
144
145 /* Collect the level indexes */
146 for (i = 1; i < z_info->max_obj_depth; i++)
147 num[i] += num[i - 1];
148
149 /* Allocate the alloc_ego_table */
150 alloc_ego_table = mem_zalloc(alloc_ego_size * sizeof(alloc_entry));
151
152 /* Scan the ego-items */
153 for (i = 0; i < z_info->e_max; i++) {
154 struct ego_item *ego = &e_info[i];
155
156 /* Count valid pairs */
157 if (ego->alloc_prob) {
158 int min_level = ego->alloc_min;
159
160 /* Skip entries preceding our locale */
161 int y = (min_level > 0) ? num[min_level - 1] : 0;
162
163 /* Skip previous entries at this locale */
164 int z = y + level_total[min_level];
165
166 /* Load the entry */
167 alloc_ego_table[z].index = i;
168 alloc_ego_table[z].level = min_level; /* Unused */
169 alloc_ego_table[z].prob1 = ego->alloc_prob;
170 alloc_ego_table[z].prob2 = ego->alloc_prob;
171 alloc_ego_table[z].prob3 = ego->alloc_prob;
172
173 /* Another entry complete for this locale */
174 level_total[min_level]++;
175 }
176 }
177
178 mem_free(level_total);
179 mem_free(num);
180 }
181
182 /*
183 * Initialize money info
184 */
init_money_svals(void)185 static void init_money_svals(void)
186 {
187 int *money_svals;
188 int i;
189
190 /* Count the money types and make a list */
191 num_money_types = tval_sval_count("gold");
192 money_type = mem_zalloc(num_money_types * sizeof(struct money));
193 money_svals = mem_zalloc(num_money_types * sizeof(struct money));
194 tval_sval_list("gold", money_svals, num_money_types);
195
196 /* List the money types */
197 for (i = 0; i < num_money_types; i++) {
198 struct object_kind *kind = lookup_kind(TV_GOLD, money_svals[i]);
199 money_type[i].name = string_make(kind->name);
200 money_type[i].type = money_svals[i];
201 }
202
203 mem_free(money_svals);
204 }
205
init_obj_make(void)206 static void init_obj_make(void) {
207 alloc_init_objects();
208 alloc_init_egos();
209 init_money_svals();
210 }
211
cleanup_obj_make(void)212 static void cleanup_obj_make(void) {
213 int i;
214 for (i = 0; i < num_money_types; i++) {
215 string_free(money_type[i].name);
216 }
217 mem_free(money_type);
218 mem_free(alloc_ego_table);
219 mem_free(obj_total_tval_great);
220 mem_free(obj_total_tval);
221 mem_free(obj_alloc_great);
222 mem_free(obj_alloc);
223 }
224
225 /*** Make an ego item ***/
226
227 /**
228 * This is a safe way to choose a random new flag to add to an object.
229 * It takes the existing flags and an array of new flags,
230 * and returns an entry from newf, or 0 if there are no
231 * new flags available.
232 */
get_new_attr(bitflag flags[OF_SIZE],bitflag newf[OF_SIZE])233 static int get_new_attr(bitflag flags[OF_SIZE], bitflag newf[OF_SIZE])
234 {
235 size_t i;
236 int options = 0, flag = 0;
237
238 for (i = of_next(newf, FLAG_START); i != FLAG_END; i = of_next(newf, i + 1))
239 {
240 /* skip this one if the flag is already present */
241 if (of_has(flags, i)) continue;
242
243 /* each time we find a new possible option, we have a 1-in-N chance of
244 * choosing it and an (N-1)-in-N chance of keeping a previous one */
245 if (one_in_(++options)) flag = i;
246 }
247
248 return flag;
249 }
250
251 /**
252 * Get a random new base resist on an item
253 */
random_base_resist(struct object * obj,int * resist)254 static int random_base_resist(struct object *obj, int *resist)
255 {
256 int i, r, count = 0;
257
258 /* Count the available base resists */
259 for (i = ELEM_BASE_MIN; i < ELEM_HIGH_MIN; i++)
260 if (obj->el_info[i].res_level == 0) count++;
261
262 if (count == 0) return false;
263
264 /* Pick one */
265 r = randint0(count);
266
267 /* Find the one we picked */
268 for (i = ELEM_BASE_MIN; i < ELEM_HIGH_MIN; i++) {
269 if (obj->el_info[i].res_level != 0) continue;
270 if (r == 0) {
271 *resist = i;
272 return true;
273 }
274 r--;
275 }
276
277 return false;
278 }
279
280 /**
281 * Get a random new high resist on an item
282 */
random_high_resist(struct object * obj,int * resist)283 static int random_high_resist(struct object *obj, int *resist)
284 {
285 int i, r, count = 0;
286
287 /* Count the available high resists */
288 for (i = ELEM_HIGH_MIN; i < ELEM_HIGH_MAX; i++)
289 if (obj->el_info[i].res_level == 0) count++;
290
291 if (count == 0) return false;
292
293 /* Pick one */
294 r = randint0(count);
295
296 /* Find the one we picked */
297 for (i = ELEM_HIGH_MIN; i < ELEM_HIGH_MAX; i++) {
298 if (obj->el_info[i].res_level != 0) continue;
299 if (r == 0) {
300 *resist = i;
301 return true;
302 }
303 r--;
304 }
305
306 return false;
307 }
308
309
310 /**
311 * Return the index, i, into the cumulative probability table, tbl, such that
312 * tbl[i] <= p < tbl[i + 1]. p must be less than tbl[n - 1] and tbl[0] must be
313 * zero.
314 */
binary_search_probtable(const u32b * tbl,int n,u32b p)315 static int binary_search_probtable(const u32b *tbl, int n, u32b p)
316 {
317 int ilow = 0, ihigh = n;
318
319 assert(tbl[0] == 0 && tbl[n - 1] > p);
320 while (1) {
321 int imid;
322
323 if (ilow == ihigh - 1) {
324 assert(tbl[ilow] <= p && tbl[ihigh] > p);
325 return ilow;
326 }
327 imid = (ilow + ihigh) / 2;
328 if (tbl[imid] <= p) {
329 ilow = imid;
330 } else {
331 ihigh = imid;
332 }
333 }
334 }
335
336
337 /**
338 * Select an ego-item that fits the object's tval and sval.
339 */
ego_find_random(struct object * obj,int level)340 static struct ego_item *ego_find_random(struct object *obj, int level)
341 {
342 int i;
343 long total = 0L;
344
345 alloc_entry *table = alloc_ego_table;
346
347 /* Go through all possible ego items and find ones which fit this item */
348 for (i = 0; i < alloc_ego_size; i++) {
349 struct ego_item *ego = &e_info[table[i].index];
350
351 /* Reset any previous probability of this type being picked */
352 table[i].prob3 = 0;
353
354 if (level <= ego->alloc_max) {
355 int ood_chance = MAX(2, (ego->alloc_min - level) / 3);
356 if (level >= ego->alloc_min || one_in_(ood_chance)) {
357 struct poss_item *poss;
358
359 for (poss = ego->poss_items; poss; poss = poss->next)
360 if (poss->kidx == obj->kind->kidx) {
361 table[i].prob3 = table[i].prob2;
362 break;
363 }
364
365 /* Total */
366 total += table[i].prob3;
367 }
368 }
369 }
370
371 if (total) {
372 long value = randint0(total);
373 for (i = 0; i < alloc_ego_size; i++) {
374 /* Found the entry */
375 if (value < table[i].prob3) {
376 return &e_info[table[i].index];
377 } else {
378 /* Decrement */
379 value = value - table[i].prob3;
380 }
381 }
382 }
383
384 return NULL;
385 }
386
387
388 /**
389 * Apply generation magic to an ego-item.
390 */
ego_apply_magic(struct object * obj,int level)391 void ego_apply_magic(struct object *obj, int level)
392 {
393 int i, x, resist = 0, pick = 0;
394 bitflag newf[OF_SIZE];
395
396 /* Resist or power? */
397 if (kf_has(obj->ego->kind_flags, KF_RAND_RES_POWER))
398 pick = randint1(3);
399
400 /* Extra powers */
401 if (kf_has(obj->ego->kind_flags, KF_RAND_SUSTAIN)) {
402 create_obj_flag_mask(newf, false, OFT_SUST, OFT_MAX);
403 of_on(obj->flags, get_new_attr(obj->flags, newf));
404 } else if (kf_has(obj->ego->kind_flags, KF_RAND_POWER) || (pick == 1)) {
405 create_obj_flag_mask(newf, false, OFT_PROT, OFT_MISC, OFT_MAX);
406 of_on(obj->flags, get_new_attr(obj->flags, newf));
407 } else if (kf_has(obj->ego->kind_flags, KF_RAND_BASE_RES) || (pick > 1)) {
408 /* Get a base resist if available, mark it as random */
409 if (random_base_resist(obj, &resist)) {
410 obj->el_info[resist].res_level = 1;
411 obj->el_info[resist].flags |= EL_INFO_RANDOM | EL_INFO_IGNORE;
412 }
413 } else if (kf_has(obj->ego->kind_flags, KF_RAND_HI_RES)) {
414 /* Get a high resist if available, mark it as random */
415 if (random_high_resist(obj, &resist)) {
416 obj->el_info[resist].res_level = 1;
417 obj->el_info[resist].flags |= EL_INFO_RANDOM | EL_INFO_IGNORE;
418 }
419 }
420
421 /* Apply extra obj->ego bonuses */
422 obj->to_h += randcalc(obj->ego->to_h, level, RANDOMISE);
423 obj->to_d += randcalc(obj->ego->to_d, level, RANDOMISE);
424 obj->to_a += randcalc(obj->ego->to_a, level, RANDOMISE);
425
426 /* Apply modifiers */
427 for (i = 0; i < OBJ_MOD_MAX; i++) {
428 x = randcalc(obj->ego->modifiers[i], level, RANDOMISE);
429 obj->modifiers[i] += x;
430 }
431
432 /* Apply flags */
433 of_union(obj->flags, obj->ego->flags);
434 of_diff(obj->flags, obj->ego->flags_off);
435
436 /* Add slays, brands and curses */
437 copy_slays(&obj->slays, obj->ego->slays);
438 copy_brands(&obj->brands, obj->ego->brands);
439 copy_curses(obj, obj->ego->curses);
440
441 /* Add resists */
442 for (i = 0; i < ELEM_MAX; i++) {
443 /* Take the larger of ego and base object resist levels */
444 obj->el_info[i].res_level =
445 MAX(obj->ego->el_info[i].res_level, obj->el_info[i].res_level);
446
447 /* Union of flags so as to know when ignoring is notable */
448 obj->el_info[i].flags |= obj->ego->el_info[i].flags;
449 }
450
451 /* Add effect (ego effect will trump object effect, when there are any) */
452 if (obj->ego->effect) {
453 obj->effect = obj->ego->effect;
454 obj->time = obj->ego->time;
455 }
456
457 return;
458 }
459
460 /**
461 * Apply minimum standards for ego-items.
462 */
ego_apply_minima(struct object * obj)463 static void ego_apply_minima(struct object *obj)
464 {
465 int i;
466
467 if (!obj->ego) return;
468
469 if (obj->ego->min_to_h != NO_MINIMUM &&
470 obj->to_h < obj->ego->min_to_h)
471 obj->to_h = obj->ego->min_to_h;
472 if (obj->ego->min_to_d != NO_MINIMUM &&
473 obj->to_d < obj->ego->min_to_d)
474 obj->to_d = obj->ego->min_to_d;
475 if (obj->ego->min_to_a != NO_MINIMUM &&
476 obj->to_a < obj->ego->min_to_a)
477 obj->to_a = obj->ego->min_to_a;
478
479 for (i = 0; i < OBJ_MOD_MAX; i++) {
480 if (obj->modifiers[i] < obj->ego->min_modifiers[i])
481 obj->modifiers[i] = obj->ego->min_modifiers[i];
482 }
483 }
484
485
486 /**
487 * Try to find an ego-item for an object, setting obj->ego if successful and
488 * applying various bonuses.
489 */
make_ego_item(struct object * obj,int level)490 static void make_ego_item(struct object *obj, int level)
491 {
492 /* Cannot further improve artifacts or ego items */
493 if (obj->artifact || obj->ego) return;
494
495 /* Occasionally boost the generation level of an item */
496 if (level > 0 && one_in_(z_info->great_ego)) {
497 level = 1 + (level * z_info->max_depth / randint1(z_info->max_depth));
498
499 /* Ensure valid allocation level */
500 if (level >= z_info->max_depth)
501 level = z_info->max_depth - 1;
502 }
503
504 /* Try to get a legal ego type for this item */
505 obj->ego = ego_find_random(obj, level);
506
507 /* Actually apply the ego template to the item */
508 if (obj->ego)
509 ego_apply_magic(obj, level);
510
511 return;
512 }
513
514
515 /*** Make an artifact ***/
516
517 /**
518 * Copy artifact data to a normal object.
519 */
copy_artifact_data(struct object * obj,const struct artifact * art)520 void copy_artifact_data(struct object *obj, const struct artifact *art)
521 {
522 int i;
523 struct object_kind *kind = lookup_kind(art->tval, art->sval);
524
525 /* Extract the data */
526 for (i = 0; i < OBJ_MOD_MAX; i++)
527 obj->modifiers[i] = art->modifiers[i];
528 obj->ac = art->ac;
529 obj->dd = art->dd;
530 obj->ds = art->ds;
531 obj->to_a = art->to_a;
532 obj->to_h = art->to_h;
533 obj->to_d = art->to_d;
534 obj->weight = art->weight;
535
536 /* Activations can come from the artifact or the kind */
537 if (art->activation) {
538 obj->activation = art->activation;
539 obj->time = art->time;
540 } else if (kind->activation) {
541 obj->activation = kind->activation;
542 obj->time = kind->time;
543 }
544
545 /* Fix for artifact lights */
546 of_off(obj->flags, OF_TAKES_FUEL);
547 of_off(obj->flags, OF_BURNS_OUT);
548
549 /* Timeouts are always 0 */
550 obj->timeout = 0;
551
552 of_union(obj->flags, art->flags);
553 copy_slays(&obj->slays, art->slays);
554 copy_brands(&obj->brands, art->brands);
555 copy_curses(obj, art->curses);
556 for (i = 0; i < ELEM_MAX; i++) {
557 /* Take the larger of artifact and base object resist levels */
558 obj->el_info[i].res_level =
559 MAX(art->el_info[i].res_level, obj->el_info[i].res_level);
560
561 /* Union of flags so as to know when ignoring is notable */
562 obj->el_info[i].flags |= art->el_info[i].flags;
563 }
564 }
565
566
567 /**
568 * Mega-Hack -- Attempt to create one of the "Special Objects".
569 *
570 * We are only called from "make_object()"
571 *
572 * Note -- see "make_artifact()" and "apply_magic()".
573 *
574 * We *prefer* to create the special artifacts in order, but this is
575 * normally outweighed by the "rarity" rolls for those artifacts.
576 */
make_artifact_special(int level,int tval)577 static struct object *make_artifact_special(int level, int tval)
578 {
579 int i;
580 struct object *new_obj;
581
582 /* No artifacts, do nothing */
583 if (OPT(player, birth_no_artifacts)) return NULL;
584
585 /* No artifacts in the town */
586 if (!player->depth) return NULL;
587
588 /* Check the special artifacts */
589 for (i = 0; i < z_info->a_max; ++i) {
590 struct artifact *art = &a_info[i];
591 struct object_kind *kind = lookup_kind(art->tval, art->sval);
592
593 /* Skip "empty" artifacts */
594 if (!art->name) continue;
595
596 /* Make sure the kind was found */
597 if (!kind) continue;
598
599 /* Make sure it's the right tval (if given) */
600 if (tval && (tval != art->tval)) continue;
601
602 /* Skip non-special artifacts */
603 if (!kf_has(kind->kind_flags, KF_INSTA_ART)) continue;
604
605 /* Cannot make an artifact twice */
606 if (art->created) continue;
607
608 /* Enforce minimum "depth" (loosely) */
609 if (art->alloc_min > player->depth) {
610 /* Get the "out-of-depth factor" */
611 int d = (art->alloc_min - player->depth) * 2;
612
613 /* Roll for out-of-depth creation */
614 if (randint0(d) != 0) continue;
615 }
616
617 /* Enforce maximum depth (strictly) */
618 if (art->alloc_max < player->depth) continue;
619
620 /* Artifact "rarity roll" */
621 if (randint1(100) > art->alloc_prob) continue;
622
623 /* Enforce minimum "object" level (loosely) */
624 if (kind->level > level) {
625 /* Get the "out-of-depth factor" */
626 int d = (kind->level - level) * 5;
627
628 /* Roll for out-of-depth creation */
629 if (randint0(d) != 0) continue;
630 }
631
632 /* Assign the template */
633 new_obj = object_new();
634 object_prep(new_obj, kind, art->alloc_min, RANDOMISE);
635
636 /* Mark the item as an artifact */
637 new_obj->artifact = art;
638
639 /* Copy across all the data from the artifact struct */
640 copy_artifact_data(new_obj, art);
641
642 /* Mark the artifact as "created" */
643 art->created = true;
644
645 /* Success */
646 return new_obj;
647 }
648
649 /* Failure */
650 return NULL;
651 }
652
653
654 /**
655 * Attempt to change an object into an artifact. If the object is already
656 * set to be an artifact, use that, or otherwise use a suitable randomly-
657 * selected artifact.
658 *
659 * This routine should only be called by "apply_magic()"
660 *
661 * Note -- see "make_artifact_special()" and "apply_magic()"
662 */
make_artifact(struct object * obj)663 static bool make_artifact(struct object *obj)
664 {
665 int i;
666
667 /* Make sure birth no artifacts isn't set */
668 if (OPT(player, birth_no_artifacts)) return false;
669
670 /* No artifacts in the town */
671 if (!player->depth) return false;
672
673 /* Paranoia -- no "plural" artifacts */
674 if (obj->number != 1) return false;
675
676 /* Check the artifact list (skip the "specials") */
677 for (i = 0; !obj->artifact && i < z_info->a_max; i++) {
678 struct artifact *art = &a_info[i];
679 struct object_kind *kind = lookup_kind(art->tval, art->sval);
680
681 /* Skip "empty" items */
682 if (!art->name) continue;
683
684 /* Make sure the kind was found */
685 if (!kind) continue;
686
687 /* Skip special artifacts */
688 if (kf_has(kind->kind_flags, KF_INSTA_ART)) continue;
689
690 /* Cannot make an artifact twice */
691 if (art->created) continue;
692
693 /* Must have the correct fields */
694 if (art->tval != obj->tval) continue;
695 if (art->sval != obj->sval) continue;
696
697 /* XXX XXX Enforce minimum "depth" (loosely) */
698 if (art->alloc_min > player->depth)
699 {
700 /* Get the "out-of-depth factor" */
701 int d = (art->alloc_min - player->depth) * 2;
702
703 /* Roll for out-of-depth creation */
704 if (randint0(d) != 0) continue;
705 }
706
707 /* Enforce maximum depth (strictly) */
708 if (art->alloc_max < player->depth) continue;
709
710 /* We must make the "rarity roll" */
711 if (randint1(100) > art->alloc_prob) continue;
712
713 /* Mark the item as an artifact */
714 obj->artifact = art;
715 }
716
717 if (obj->artifact) {
718 copy_artifact_data(obj, obj->artifact);
719 obj->artifact->created = true;
720 return true;
721 }
722
723 return false;
724 }
725
726
727 /**
728 * Create a fake artifact directly from a blank object
729 *
730 * This function is used for describing artifacts, and for creating them for
731 * debugging.
732 *
733 * Since this is now in no way marked as fake, we must make sure this function
734 * is never used to create an actual game object
735 */
make_fake_artifact(struct object * obj,const struct artifact * artifact)736 bool make_fake_artifact(struct object *obj, const struct artifact *artifact)
737 {
738 struct object_kind *kind;
739
740 /* Don't bother with empty artifacts */
741 if (!artifact->tval) return false;
742
743 /* Get the "kind" index */
744 kind = lookup_kind(artifact->tval, artifact->sval);
745 if (!kind) return false;
746
747 /* Create the artifact */
748 object_prep(obj, kind, 0, MAXIMISE);
749 obj->artifact = (struct artifact *)artifact;
750 copy_artifact_data(obj, artifact);
751
752 return (true);
753 }
754
755
756 /*** Apply magic to an item ***/
757
758 /**
759 * Apply magic to a weapon.
760 */
apply_magic_weapon(struct object * obj,int level,int power)761 static void apply_magic_weapon(struct object *obj, int level, int power)
762 {
763 if (power <= 0)
764 return;
765
766 obj->to_h += randint1(5) + m_bonus(5, level);
767 obj->to_d += randint1(5) + m_bonus(5, level);
768
769 if (power > 1) {
770 obj->to_h += m_bonus(10, level);
771 obj->to_d += m_bonus(10, level);
772
773 if (tval_is_melee_weapon(obj)) {
774 /* Super-charge the damage dice */
775 while ((obj->dd * obj->ds > 0) && one_in_(4 * obj->dd * obj->ds)) {
776 /* More dice or sides means more likely to get still more */
777 if (randint0(obj->dd + obj->ds) < obj->dd) {
778 int newdice = randint1(2 + obj->dd/obj->ds);
779 while (((obj->dd + 1) * obj->ds <= 40) && newdice) {
780 if (!one_in_(3)) {
781 obj->dd++;
782 }
783 newdice--;
784 }
785 } else {
786 int newsides = randint1(2 + obj->ds/obj->dd);
787 while ((obj->dd * (obj->ds + 1) <= 40) && newsides) {
788 if (!one_in_(3)) {
789 obj->ds++;
790 }
791 newsides--;
792 }
793 }
794 }
795 } else if (tval_is_ammo(obj)) {
796 /* Up to two chances to enhance damage dice. */
797 if (one_in_(6) == 1) {
798 obj->ds++;
799 if (one_in_(10) == 1) {
800 obj->ds++;
801 }
802 }
803 }
804 }
805 }
806
807
808 /**
809 * Apply magic to armour
810 */
apply_magic_armour(struct object * obj,int level,int power)811 static void apply_magic_armour(struct object *obj, int level, int power)
812 {
813 if (power <= 0)
814 return;
815
816 obj->to_a += randint1(5) + m_bonus(5, level);
817 if (power > 1)
818 obj->to_a += m_bonus(10, level);
819 }
820
821
822 /**
823 * Wipe an object clean and make it a standard object of the specified kind.
824 */
object_prep(struct object * obj,struct object_kind * k,int lev,aspect rand_aspect)825 void object_prep(struct object *obj, struct object_kind *k, int lev,
826 aspect rand_aspect)
827 {
828 int i;
829
830 /* Clean slate */
831 memset(obj, 0, sizeof(*obj));
832
833 /* Assign the kind and copy across data */
834 obj->kind = k;
835 obj->tval = k->tval;
836 obj->sval = k->sval;
837 obj->ac = k->ac;
838 obj->dd = k->dd;
839 obj->ds = k->ds;
840 obj->weight = k->weight;
841 obj->effect = k->effect;
842 obj->time = k->time;
843
844 /* Default number */
845 obj->number = 1;
846
847 /* Copy flags */
848 of_copy(obj->flags, k->base->flags);
849 of_copy(obj->flags, k->flags);
850
851 /* Assign modifiers */
852 for (i = 0; i < OBJ_MOD_MAX; i++)
853 obj->modifiers[i] = randcalc(k->modifiers[i], lev, rand_aspect);
854
855 /* Assign charges (wands/staves only) */
856 if (tval_can_have_charges(obj))
857 obj->pval = randcalc(k->charge, lev, rand_aspect);
858
859 /* Assign pval for food, oil and launchers */
860 if (tval_is_edible(obj) || tval_is_potion(obj) || tval_is_fuel(obj) ||
861 tval_is_launcher(obj))
862 obj->pval
863 = randcalc(k->pval, lev, rand_aspect);
864
865 /* Default fuel */
866 if (tval_is_light(obj)) {
867 if (of_has(obj->flags, OF_BURNS_OUT))
868 obj->timeout = z_info->fuel_torch;
869 else if (of_has(obj->flags, OF_TAKES_FUEL))
870 obj->timeout = z_info->default_lamp;
871 }
872
873 /* Default magic */
874 obj->to_h = randcalc(k->to_h, lev, rand_aspect);
875 obj->to_d = randcalc(k->to_d, lev, rand_aspect);
876 obj->to_a = randcalc(k->to_a, lev, rand_aspect);
877
878 /* Default slays, brands and curses */
879 copy_slays(&obj->slays, k->slays);
880 copy_brands(&obj->brands, k->brands);
881 copy_curses(obj, k->curses);
882
883 /* Default resists */
884 for (i = 0; i < ELEM_MAX; i++) {
885 obj->el_info[i].res_level = k->el_info[i].res_level;
886 obj->el_info[i].flags = k->el_info[i].flags;
887 obj->el_info[i].flags |= k->base->el_info[i].flags;
888 }
889 }
890
891 /**
892 * Attempt to apply curses to an object, with a corresponding increase in
893 * generation level of the object
894 */
apply_curse(struct object * obj,int lev)895 static int apply_curse(struct object *obj, int lev)
896 {
897 int pick, max_curses = randint1(4);
898 int power = randint1(9) + 10 * m_bonus(9, lev);
899 int new_lev = lev;
900
901 if (of_has(obj->flags, OF_BLESSED)) return lev;
902
903 while (max_curses--) {
904 /* Try to curse it */
905 int tries = 3;
906 while (tries--) {
907 pick = randint1(z_info->curse_max - 1);
908 if (curses[pick].poss[obj->tval]) {
909 if (append_object_curse(obj, pick, power)) {
910 new_lev += randint1(1 + power / 10);
911 }
912 break;
913 }
914 }
915 }
916
917 return new_lev;
918 }
919
920 /**
921 * Applying magic to an object, which includes creating ego-items, and applying
922 * random bonuses,
923 *
924 * The `good` argument forces the item to be at least `good`, and the `great`
925 * argument does likewise. Setting `allow_artifacts` to true allows artifacts
926 * to be created here.
927 *
928 * If `good` or `great` are not set, then the `lev` argument controls the
929 * quality of item.
930 *
931 * Returns 0 if a normal object, 1 if a good object, 2 if an ego item, 3 if an
932 * artifact.
933 */
apply_magic(struct object * obj,int lev,bool allow_artifacts,bool good,bool great,bool extra_roll)934 int apply_magic(struct object *obj, int lev, bool allow_artifacts, bool good,
935 bool great, bool extra_roll)
936 {
937 int i;
938 s16b power = 0;
939
940 /* Chance of being `good` and `great` */
941 /* This has changed over the years:
942 * 3.0.0: good = MIN(75, lev + 10); great = MIN(20, lev / 2);
943 * 3.3.0: good = (lev + 2) * 3; great = MIN(lev / 4 + lev, 50);
944 * 3.4.0: good = (2 * lev) + 5
945 * 3.4 was in between 3.0 and 3.3, 3.5 attempts to keep the same
946 * area under the curve as 3.4, but make the generation chances
947 * flatter. This depresses good items overall since more items
948 * are created deeper.
949 * This change is meant to go in conjunction with the changes
950 * to ego item allocation levels. (-fizzix)
951 */
952 int good_chance = (33 + lev);
953 int great_chance = 30;
954
955 /* Roll for "good" */
956 if (good || (randint0(100) < good_chance)) {
957 power = 1;
958
959 /* Roll for "great" */
960 if (great || (randint0(100) < great_chance))
961 power = 2;
962 }
963
964 /* Roll for artifact creation */
965 if (allow_artifacts) {
966 int rolls = 0;
967
968 /* Get one roll if excellent */
969 if (power >= 2) rolls = 1;
970
971 /* Get two rolls if forced great */
972 if (great) rolls = 2;
973
974 /* Give some extra rolls for uniques and acq scrolls */
975 if (extra_roll) rolls += 2;
976
977 /* Roll for artifacts if allowed */
978 for (i = 0; i < rolls; i++)
979 if (make_artifact(obj)) return 3;
980 }
981
982 /* Try to make an ego item */
983 if (power == 2)
984 make_ego_item(obj, lev);
985
986 /* Give it a chance to be cursed */
987 if (one_in_(20) && tval_is_wearable(obj)) {
988 lev = apply_curse(obj, lev);
989 }
990
991 /* Apply magic */
992 if (tval_is_weapon(obj)) {
993 apply_magic_weapon(obj, lev, power);
994 } else if (tval_is_armor(obj)) {
995 apply_magic_armour(obj, lev, power);
996 } else if (tval_is_ring(obj)) {
997 if (obj->sval == lookup_sval(obj->tval, "Speed")) {
998 /* Super-charge the ring */
999 while (one_in_(2))
1000 obj->modifiers[OBJ_MOD_SPEED]++;
1001 }
1002 } else if (tval_is_chest(obj)) {
1003 /* Get a random, level-dependent set of chest traps */
1004 obj->pval = pick_chest_traps(obj);
1005 }
1006
1007 /* Apply minima from ego items if necessary */
1008 ego_apply_minima(obj);
1009
1010 return power;
1011 }
1012
1013
1014 /*** Generate a random object ***/
1015
1016 /**
1017 * Hack -- determine if a template is "good".
1018 *
1019 * Note that this test only applies to the object *kind*, so it is
1020 * possible to choose a kind which is "good", and then later cause
1021 * the actual object to be cursed. We do explicitly forbid objects
1022 * which are known to be boring or which start out somewhat damaged.
1023 */
kind_is_good(const struct object_kind * kind)1024 bool kind_is_good(const struct object_kind *kind)
1025 {
1026 /* Some item types are (almost) always good */
1027 switch (kind->tval)
1028 {
1029 /* Armor -- Good unless damaged */
1030 case TV_HARD_ARMOR:
1031 case TV_SOFT_ARMOR:
1032 case TV_DRAG_ARMOR:
1033 case TV_SHIELD:
1034 case TV_CLOAK:
1035 case TV_BOOTS:
1036 case TV_GLOVES:
1037 case TV_HELM:
1038 case TV_CROWN:
1039 {
1040 if (randcalc(kind->to_a, 0, MINIMISE) < 0) return (false);
1041 return true;
1042 }
1043
1044 /* Weapons -- Good unless damaged */
1045 case TV_BOW:
1046 case TV_SWORD:
1047 case TV_HAFTED:
1048 case TV_POLEARM:
1049 case TV_DIGGING:
1050 {
1051 if (randcalc(kind->to_h, 0, MINIMISE) < 0) return (false);
1052 if (randcalc(kind->to_d, 0, MINIMISE) < 0) return (false);
1053 return true;
1054 }
1055
1056 /* Ammo -- Arrows/Bolts are good */
1057 case TV_BOLT:
1058 case TV_ARROW:
1059 {
1060 return true;
1061 }
1062 }
1063
1064 /* Anything with the GOOD flag */
1065 if (kf_has(kind->kind_flags, KF_GOOD))
1066 return true;
1067
1068 /* Assume not good */
1069 return (false);
1070 }
1071
1072
1073 /**
1074 * Choose an object kind of a given tval given a dungeon level.
1075 */
get_obj_num_by_kind(int level,bool good,int tval)1076 static struct object_kind *get_obj_num_by_kind(int level, bool good, int tval)
1077 {
1078 const u32b *objects;
1079 u32b total, value;
1080 int item;
1081
1082 assert(level >= 0 && level <= z_info->max_obj_depth);
1083 assert(tval >= 0 && tval < TV_MAX);
1084 if (good) {
1085 objects = obj_alloc_great + level * (z_info->k_max + 1);
1086 total = obj_total_tval_great[level * TV_MAX + tval];
1087 } else {
1088 objects = obj_alloc + level * (z_info->k_max + 1);
1089 total = obj_total_tval[level * TV_MAX + tval];
1090 }
1091
1092 /* No appropriate items of that tval */
1093 if (!total) return NULL;
1094
1095 /* Pick an object */
1096 value = randint0(total);
1097
1098 /*
1099 * Find it. Having a loop to calculate the cumulative probability
1100 * here with only the tval and applying a binary search was slower
1101 * for a test of getting a TV_SWORD from 4.2's available objects.
1102 * So continue to use the O(N) search.
1103 */
1104 for (item = 0; item < z_info->k_max; item++) {
1105 if (objkind_byid(item)->tval == tval) {
1106 u32b prob = objects[item + 1] - objects[item];
1107
1108 if (value < prob) break;
1109 value -= prob;
1110 }
1111 }
1112
1113 /* Return the item index */
1114 return objkind_byid(item);
1115 }
1116
1117 /**
1118 * Choose an object kind given a dungeon level to choose it for.
1119 * If tval = 0, we can choose an object of any type.
1120 * Otherwise we can only choose one of the given tval.
1121 */
get_obj_num(int level,bool good,int tval)1122 struct object_kind *get_obj_num(int level, bool good, int tval)
1123 {
1124 const u32b *objects;
1125 u32b value;
1126 int item;
1127
1128 /* Occasional level boost */
1129 if ((level > 0) && one_in_(z_info->great_obj))
1130 /* What a bizarre calculation */
1131 level = 1 + (level * z_info->max_obj_depth / randint1(z_info->max_obj_depth));
1132
1133 /* Paranoia */
1134 level = MIN(level, z_info->max_obj_depth);
1135 level = MAX(level, 0);
1136
1137 if (tval)
1138 return get_obj_num_by_kind(level, good, tval);
1139
1140 objects = (good ? obj_alloc_great : obj_alloc) +
1141 level * (z_info->k_max + 1);
1142
1143 /* Pick an object. */
1144 if (! objects[z_info->k_max]) {
1145 return NULL;
1146 }
1147 value = randint0(objects[z_info->k_max]);
1148
1149 /* Find it with a binary search. */
1150 item = binary_search_probtable(objects, z_info->k_max + 1, value);
1151
1152 /* Return the item index */
1153 return objkind_byid(item);
1154 }
1155
1156
1157 /**
1158 * Attempt to make an object
1159 *
1160 * \param c is the current dungeon level.
1161 * \param lev is the creation level of the object (not necessarily == depth).
1162 * \param good is whether the object is to be good
1163 * \param great is whether the object is to be great
1164 * \param extra_roll is whether we get an extra roll in apply_magic()
1165 * \param value is the value to be returned to the calling function
1166 * \param tval is the desired tval, or 0 if we allow any tval
1167 *
1168 * \return a pointer to the newly allocated object, or NULL on failure.
1169 */
make_object(struct chunk * c,int lev,bool good,bool great,bool extra_roll,s32b * value,int tval)1170 struct object *make_object(struct chunk *c, int lev, bool good, bool great,
1171 bool extra_roll, s32b *value, int tval)
1172 {
1173 int base, tries = 3;
1174 struct object_kind *kind = NULL;
1175 struct object *new_obj;
1176
1177 /* Try to make a special artifact */
1178 if (one_in_(good ? 10 : 1000)) {
1179 new_obj = make_artifact_special(lev, tval);
1180 if (new_obj) {
1181 if (value) *value = object_value_real(new_obj, 1);
1182 return new_obj;
1183 }
1184
1185 /* If we failed to make an artifact, the player gets a good item */
1186 good = true;
1187 }
1188
1189 /* Base level for the object */
1190 base = (good ? (lev + 10) : lev);
1191
1192 /* Try to choose an object kind; reject most books the player can't read */
1193 while (tries) {
1194 kind = get_obj_num(base, good || great, tval);
1195 if (kind && tval_is_book_k(kind) && !obj_kind_can_browse(kind)) {
1196 if (one_in_(5)) break;
1197 kind = NULL;
1198 tries--;
1199 continue;
1200 } else {
1201 break;
1202 }
1203 }
1204 if (!kind)
1205 return NULL;
1206
1207 /* Make the object, prep it and apply magic */
1208 new_obj = object_new();
1209 object_prep(new_obj, kind, lev, RANDOMISE);
1210 apply_magic(new_obj, lev, true, good, great, extra_roll);
1211
1212 /* Generate multiple items */
1213 if (!new_obj->artifact && kind->gen_mult_prob >= randint1(100))
1214 new_obj->number = randcalc(kind->stack_size, lev, RANDOMISE);
1215
1216 if (new_obj->number > new_obj->kind->base->max_stack)
1217 new_obj->number = new_obj->kind->base->max_stack;
1218
1219 /* Get the value */
1220 if (value)
1221 *value = object_value_real(new_obj, new_obj->number);
1222
1223 /* Boost of 20% per level OOD for uncursed objects */
1224 if ((!new_obj->curses) && (kind->alloc_min > c->depth)) {
1225 if (value) *value += (kind->alloc_min - c->depth) * (*value / 5);
1226 }
1227
1228 return new_obj;
1229 }
1230
1231
1232 /**
1233 * Scatter some objects near the player
1234 */
acquirement(struct loc grid,int level,int num,bool great)1235 void acquirement(struct loc grid, int level, int num, bool great)
1236 {
1237 struct object *nice_obj;
1238
1239 /* Acquirement */
1240 while (num--) {
1241 /* Make a good (or great) object (if possible) */
1242 nice_obj = make_object(cave, level, true, great, true, NULL, 0);
1243 if (!nice_obj) continue;
1244
1245 nice_obj->origin = ORIGIN_ACQUIRE;
1246 nice_obj->origin_depth = player->depth;
1247
1248 /* Drop the object */
1249 drop_near(cave, &nice_obj, 0, grid, true, false);
1250 }
1251 }
1252
1253
1254 /*** Make a gold item ***/
1255
1256 /**
1257 * Get a money kind by name, or level-appropriate
1258 */
money_kind(const char * name,int value)1259 struct object_kind *money_kind(const char *name, int value)
1260 {
1261 int rank;
1262 /* (Roughly) the largest possible gold drop at max depth - the precise
1263 * value is derivable from the calculations in make_gold(), but this is
1264 * near enough */
1265 int max_gold_drop = 3 * z_info->max_depth + 30;
1266
1267 /* Check for specified treasure variety */
1268 for (rank = 0; rank < num_money_types; rank++)
1269 if (streq(name, money_type[rank].name))
1270 break;
1271
1272 /* Pick a treasure variety scaled by level */
1273 if (rank == num_money_types)
1274 rank = (((value * 100) / max_gold_drop) * num_money_types) / 100;
1275
1276 /* Do not create illegal treasure types */
1277 if (rank >= num_money_types) rank = num_money_types - 1;
1278
1279 return lookup_kind(TV_GOLD, money_type[rank].type);
1280 }
1281
1282 /**
1283 * Make a money object
1284 *
1285 * \param lev the dungeon level
1286 * \param coin_type the name of the type of money object to make
1287 * \return a pointer to the newly minted cash (cannot fail)
1288 */
make_gold(int lev,char * coin_type)1289 struct object *make_gold(int lev, char *coin_type)
1290 {
1291 /* This average is 16 at dlev0, 80 at dlev40, 176 at dlev100. */
1292 int avg = (16 * lev)/10 + 16;
1293 int spread = lev + 10;
1294 int value = rand_spread(avg, spread);
1295 struct object *new_gold = mem_zalloc(sizeof(*new_gold));
1296
1297 /* Increase the range to infinite, moving the average to 110% */
1298 while (one_in_(100) && value * 10 <= SHRT_MAX)
1299 value *= 10;
1300
1301 /* Prepare a gold object */
1302 object_prep(new_gold, money_kind(coin_type, value), lev, RANDOMISE);
1303
1304 /* If we're playing with no_selling, increase the value */
1305 if (OPT(player, birth_no_selling) && player->depth) {
1306 value *= 5;
1307 }
1308
1309 /* Cap gold at max short (or alternatively make pvals s32b) */
1310 if (value >= SHRT_MAX) {
1311 value = SHRT_MAX - randint0(200);
1312 }
1313
1314 new_gold->pval = value;
1315
1316 return new_gold;
1317 }
1318
1319 struct init_module obj_make_module = {
1320 .name = "object/obj-make",
1321 .init = init_obj_make,
1322 .cleanup = cleanup_obj_make
1323 };
1324