PageRenderTime 146ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 1ms

/src/object/randart.c

https://bitbucket.org/ekolis/jackband
C | 3283 lines | 2280 code | 441 blank | 562 comment | 707 complexity | a2069a83996d7cacbdedc6489a21b7bb MD5 | raw file
  1. /*
  2. * File: randart.c
  3. * Purpose: Random artifact generation
  4. *
  5. * Copyright (c) 1998 Greg Wooledge, Ben Harrison, Robert Ruhlmann
  6. * Copyright (c) 2001 Chris Carr, Chris Robertson
  7. *
  8. * This work is free software; you can redistribute it and/or modify it
  9. * under the terms of either:
  10. *
  11. * a) the GNU General Public License as published by the Free Software
  12. * Foundation, version 2, or
  13. *
  14. * b) the "Angband licence":
  15. * This software may be copied and distributed for educational, research,
  16. * and not for profit purposes provided that this copyright and statement
  17. * are included in all such copies. Other copyrights may also apply.
  18. */
  19. #include "angband.h"
  20. #include "object/tvalsval.h"
  21. #include "init.h"
  22. #include "effects.h"
  23. /*
  24. * Original random artifact generator (randart) by Greg Wooledge.
  25. * Updated by Chris Carr / Chris Robertson 2001-2010.
  26. */
  27. #define MAX_TRIES 200
  28. #define BUFLEN 1024
  29. #define MIN_NAME_LEN 5
  30. #define MAX_NAME_LEN 9
  31. #define S_WORD 26
  32. #define E_WORD S_WORD
  33. /*
  34. * Inhibiting factors for large bonus values
  35. * "HIGH" values use INHIBIT_WEAK
  36. * "VERYHIGH" values use INHIBIT_STRONG
  37. */
  38. #define INHIBIT_STRONG (one_in_(6))
  39. #define INHIBIT_WEAK (one_in_(2))
  40. /*
  41. * Power rating below which uncursed randarts cannot aggravate
  42. * (so that aggravate is found only on endgame-quality items or
  43. * cursed items)
  44. */
  45. #define AGGR_POWER 300
  46. /*
  47. * Numerical index values for the different learned probabilities
  48. * These are to make the code more readable.
  49. * ToDo: turn these into an enum
  50. */
  51. #define ART_IDX_BOW_SHOTS 0
  52. #define ART_IDX_BOW_MIGHT 1
  53. #define ART_IDX_BOW_BRAND 80
  54. #define ART_IDX_BOW_SLAY 81
  55. #define ART_IDX_WEAPON_HIT 2
  56. #define ART_IDX_WEAPON_DAM 3
  57. #define ART_IDX_NONWEAPON_HIT 4
  58. #define ART_IDX_NONWEAPON_DAM 5
  59. #define ART_IDX_NONWEAPON_HIT_DAM 6
  60. #define ART_IDX_NONWEAPON_BRAND 78
  61. #define ART_IDX_NONWEAPON_SLAY 79
  62. #define ART_IDX_NONWEAPON_BLOWS 83
  63. #define ART_IDX_NONWEAPON_SHOTS 84
  64. #define ART_IDX_MELEE_BLESS 7
  65. #define ART_IDX_MELEE_BRAND 8
  66. #define ART_IDX_MELEE_SLAY 76
  67. #define ART_IDX_MELEE_SINV 9
  68. #define ART_IDX_MELEE_BLOWS 10
  69. #define ART_IDX_MELEE_AC 11
  70. #define ART_IDX_MELEE_DICE 12
  71. #define ART_IDX_MELEE_WEIGHT 13
  72. #define ART_IDX_MELEE_TUNN 14
  73. #define ART_IDX_ALLARMOR_WEIGHT 15
  74. #define ART_IDX_BOOT_AC 16
  75. #define ART_IDX_BOOT_FEATHER 17
  76. #define ART_IDX_BOOT_STEALTH 18
  77. #define ART_IDX_BOOT_SPEED 19
  78. #define ART_IDX_GLOVE_AC 20
  79. #define ART_IDX_GLOVE_FA 21
  80. #define ART_IDX_GLOVE_DEX 22
  81. #define ART_IDX_HELM_AC 23
  82. #define ART_IDX_HELM_RBLIND 24
  83. #define ART_IDX_HELM_ESP 25
  84. #define ART_IDX_HELM_SINV 26
  85. #define ART_IDX_HELM_WIS 27
  86. #define ART_IDX_HELM_INT 28
  87. #define ART_IDX_SHIELD_AC 29
  88. #define ART_IDX_SHIELD_LRES 30
  89. #define ART_IDX_CLOAK_AC 31
  90. #define ART_IDX_CLOAK_STEALTH 32
  91. #define ART_IDX_ARMOR_AC 33
  92. #define ART_IDX_ARMOR_STEALTH 34
  93. #define ART_IDX_ARMOR_HLIFE 35
  94. #define ART_IDX_ARMOR_CON 36
  95. #define ART_IDX_ARMOR_LRES 37
  96. #define ART_IDX_ARMOR_ALLRES 38
  97. #define ART_IDX_ARMOR_HRES 39
  98. #define ART_IDX_GEN_STAT 40
  99. #define ART_IDX_GEN_SUST 41
  100. #define ART_IDX_GEN_STEALTH 42
  101. #define ART_IDX_GEN_SEARCH 43
  102. #define ART_IDX_GEN_INFRA 44
  103. #define ART_IDX_GEN_SPEED 45
  104. #define ART_IDX_GEN_IMMUNE 46
  105. #define ART_IDX_GEN_FA 47
  106. #define ART_IDX_GEN_HLIFE 48
  107. #define ART_IDX_GEN_FEATHER 49
  108. #define ART_IDX_GEN_LIGHT 50
  109. #define ART_IDX_GEN_SINV 51
  110. #define ART_IDX_GEN_ESP 52
  111. #define ART_IDX_GEN_SDIG 53
  112. #define ART_IDX_GEN_REGEN 54
  113. #define ART_IDX_GEN_LRES 55
  114. #define ART_IDX_GEN_RPOIS 56
  115. #define ART_IDX_GEN_RFEAR 57
  116. #define ART_IDX_GEN_RLIGHT 58
  117. #define ART_IDX_GEN_RDARK 59
  118. #define ART_IDX_GEN_RBLIND 60
  119. #define ART_IDX_GEN_RCONF 61
  120. #define ART_IDX_GEN_RSOUND 62
  121. #define ART_IDX_GEN_RSHARD 63
  122. #define ART_IDX_GEN_RNEXUS 64
  123. #define ART_IDX_GEN_RNETHER 65
  124. #define ART_IDX_GEN_RCHAOS 66
  125. #define ART_IDX_GEN_RDISEN 67
  126. #define ART_IDX_GEN_AC 68
  127. #define ART_IDX_GEN_TUNN 69
  128. #define ART_IDX_GEN_ACTIV 82
  129. /* Supercharged abilities - treated differently in algorithm */
  130. #define ART_IDX_MELEE_DICE_SUPER 70
  131. #define ART_IDX_BOW_SHOTS_SUPER 71
  132. #define ART_IDX_BOW_MIGHT_SUPER 72
  133. #define ART_IDX_GEN_SPEED_SUPER 73
  134. #define ART_IDX_MELEE_BLOWS_SUPER 77
  135. #define ART_IDX_GEN_AC_SUPER 85
  136. /* Aggravation - weapon and nonweapon */
  137. #define ART_IDX_WEAPON_AGGR 74
  138. #define ART_IDX_NONWEAPON_AGGR 75
  139. /* Total of abilities */
  140. #define ART_IDX_TOTAL 86
  141. /* Tallies of different ability types */
  142. /* ToDo: use N_ELEMENTS for these */
  143. #define ART_IDX_BOW_COUNT 4
  144. #define ART_IDX_WEAPON_COUNT 3
  145. #define ART_IDX_NONWEAPON_COUNT 8
  146. #define ART_IDX_MELEE_COUNT 9
  147. #define ART_IDX_ALLARMOR_COUNT 1
  148. #define ART_IDX_BOOT_COUNT 4
  149. #define ART_IDX_GLOVE_COUNT 3
  150. #define ART_IDX_HELM_COUNT 6
  151. #define ART_IDX_SHIELD_COUNT 2
  152. #define ART_IDX_CLOAK_COUNT 2
  153. #define ART_IDX_ARMOR_COUNT 7
  154. #define ART_IDX_GEN_COUNT 31
  155. #define ART_IDX_HIGH_RESIST_COUNT 12
  156. /* Arrays of indices by item type, used in frequency generation */
  157. static s16b art_idx_bow[] =
  158. {ART_IDX_BOW_SHOTS, ART_IDX_BOW_MIGHT, ART_IDX_BOW_BRAND, ART_IDX_BOW_SLAY};
  159. static s16b art_idx_weapon[] =
  160. {ART_IDX_WEAPON_HIT, ART_IDX_WEAPON_DAM, ART_IDX_WEAPON_AGGR};
  161. static s16b art_idx_nonweapon[] =
  162. {ART_IDX_NONWEAPON_HIT, ART_IDX_NONWEAPON_DAM, ART_IDX_NONWEAPON_HIT_DAM,
  163. ART_IDX_NONWEAPON_AGGR, ART_IDX_NONWEAPON_BRAND, ART_IDX_NONWEAPON_SLAY,
  164. ART_IDX_NONWEAPON_BLOWS, ART_IDX_NONWEAPON_SHOTS};
  165. static s16b art_idx_melee[] =
  166. {ART_IDX_MELEE_BLESS, ART_IDX_MELEE_SINV, ART_IDX_MELEE_BRAND, ART_IDX_MELEE_SLAY,
  167. ART_IDX_MELEE_BLOWS, ART_IDX_MELEE_AC, ART_IDX_MELEE_DICE,
  168. ART_IDX_MELEE_WEIGHT, ART_IDX_MELEE_TUNN};
  169. static s16b art_idx_allarmor[] =
  170. {ART_IDX_ALLARMOR_WEIGHT};
  171. static s16b art_idx_boot[] =
  172. {ART_IDX_BOOT_AC, ART_IDX_BOOT_FEATHER, ART_IDX_BOOT_STEALTH, ART_IDX_BOOT_SPEED};
  173. static s16b art_idx_glove[] =
  174. {ART_IDX_GLOVE_AC, ART_IDX_GLOVE_FA, ART_IDX_GLOVE_DEX};
  175. static s16b art_idx_headgear[] =
  176. {ART_IDX_HELM_AC, ART_IDX_HELM_RBLIND, ART_IDX_HELM_ESP, ART_IDX_HELM_SINV,
  177. ART_IDX_HELM_WIS, ART_IDX_HELM_INT};
  178. static s16b art_idx_shield[] =
  179. {ART_IDX_SHIELD_AC, ART_IDX_SHIELD_LRES};
  180. static s16b art_idx_cloak[] =
  181. {ART_IDX_CLOAK_AC, ART_IDX_CLOAK_STEALTH};
  182. static s16b art_idx_armor[] =
  183. {ART_IDX_ARMOR_AC, ART_IDX_ARMOR_STEALTH, ART_IDX_ARMOR_HLIFE, ART_IDX_ARMOR_CON,
  184. ART_IDX_ARMOR_LRES, ART_IDX_ARMOR_ALLRES, ART_IDX_ARMOR_HRES};
  185. static s16b art_idx_gen[] =
  186. {ART_IDX_GEN_STAT, ART_IDX_GEN_SUST, ART_IDX_GEN_STEALTH,
  187. ART_IDX_GEN_SEARCH, ART_IDX_GEN_INFRA, ART_IDX_GEN_SPEED,
  188. ART_IDX_GEN_IMMUNE, ART_IDX_GEN_FA, ART_IDX_GEN_HLIFE,
  189. ART_IDX_GEN_FEATHER, ART_IDX_GEN_LIGHT, ART_IDX_GEN_SINV,
  190. ART_IDX_GEN_ESP, ART_IDX_GEN_SDIG, ART_IDX_GEN_REGEN,
  191. ART_IDX_GEN_LRES, ART_IDX_GEN_RPOIS, ART_IDX_GEN_RFEAR,
  192. ART_IDX_GEN_RLIGHT, ART_IDX_GEN_RDARK, ART_IDX_GEN_RBLIND,
  193. ART_IDX_GEN_RCONF, ART_IDX_GEN_RSOUND, ART_IDX_GEN_RSHARD,
  194. ART_IDX_GEN_RNEXUS, ART_IDX_GEN_RNETHER, ART_IDX_GEN_RCHAOS,
  195. ART_IDX_GEN_RDISEN, ART_IDX_GEN_AC, ART_IDX_GEN_TUNN,
  196. ART_IDX_GEN_ACTIV};
  197. static s16b art_idx_high_resist[] =
  198. {ART_IDX_GEN_RPOIS, ART_IDX_GEN_RFEAR,
  199. ART_IDX_GEN_RLIGHT, ART_IDX_GEN_RDARK, ART_IDX_GEN_RBLIND,
  200. ART_IDX_GEN_RCONF, ART_IDX_GEN_RSOUND, ART_IDX_GEN_RSHARD,
  201. ART_IDX_GEN_RNEXUS, ART_IDX_GEN_RNETHER, ART_IDX_GEN_RCHAOS,
  202. ART_IDX_GEN_RDISEN};
  203. /* Initialize the data structures for learned probabilities */
  204. static s16b artprobs[ART_IDX_TOTAL];
  205. s16b *baseprobs;
  206. static s16b art_bow_total = 0;
  207. static s16b art_melee_total = 0;
  208. static s16b art_boot_total = 0;
  209. static s16b art_glove_total = 0;
  210. static s16b art_headgear_total = 0;
  211. static s16b art_shield_total = 0;
  212. static s16b art_cloak_total = 0;
  213. static s16b art_armor_total = 0;
  214. static s16b art_other_total = 0;
  215. static s16b art_total = 0;
  216. /*
  217. * Working arrays for holding frequency values - global to avoid repeated
  218. * allocation of memory
  219. */
  220. static s16b art_freq[ART_IDX_TOTAL]; /* artifact attributes */
  221. s16b *base_freq; /* base items */
  222. /*
  223. * Mean start and increment values for to_hit, to_dam and AC. Update these
  224. * if the algorithm changes. They are used in frequency generation.
  225. */
  226. static s16b mean_hit_increment = 4;
  227. static s16b mean_dam_increment = 4;
  228. static s16b mean_hit_startval = 10;
  229. static s16b mean_dam_startval = 10;
  230. static s16b mean_ac_startval = 15;
  231. static s16b mean_ac_increment = 5;
  232. /*
  233. * Pointer for logging file
  234. */
  235. static ang_file *log_file = NULL;
  236. /*
  237. * Store the original artifact power ratings
  238. */
  239. static s32b *base_power;
  240. static s16b max_power;
  241. static s16b min_power;
  242. static s16b avg_power;
  243. static s16b var_power;
  244. /*
  245. * Store the original base item levels
  246. */
  247. static byte *base_item_level;
  248. /*
  249. * Store the original base item rarities
  250. */
  251. static byte *base_item_prob;
  252. /*
  253. * Store the original artifact rarities
  254. */
  255. static byte *base_art_alloc;
  256. /* Global just for convenience. */
  257. static int verbose = 1;
  258. /*
  259. * Object flag names
  260. */
  261. static const char *flag_names[] =
  262. {
  263. #define OF(a, b) #a,
  264. #include "list-object-flags.h"
  265. #undef OF
  266. ""
  267. };
  268. /*
  269. * Use W. Sheldon Simms' random name generator.
  270. */
  271. static errr init_names(void)
  272. {
  273. char buf[BUFLEN];
  274. size_t name_size;
  275. char *a_base;
  276. char *a_next;
  277. int i;
  278. /* Temporary space for names, while reading and randomizing them. */
  279. char **names;
  280. /* Allocate the "names" array */
  281. /* ToDo: Make sure the memory is freed correctly in case of errors */
  282. names = C_ZNEW(z_info->a_max, char *);
  283. for (i = 0; i < z_info->a_max; i++)
  284. {
  285. char word[MAX_NAME_LEN + 1];
  286. randname_make(RANDNAME_TOLKIEN, MIN_NAME_LEN, MAX_NAME_LEN,
  287. word, sizeof word);
  288. word[0] = toupper((unsigned char) word[0]);
  289. if (one_in_(3))
  290. strnfmt(buf, sizeof(buf), "'%s'", word);
  291. else
  292. strnfmt(buf, sizeof(buf), "of %s", word);
  293. names[i] = string_make(buf);
  294. }
  295. /* Special cases -- keep these three names separate. */
  296. string_free(names[ART_POWER - 1]);
  297. string_free(names[ART_GROND - 1]);
  298. string_free(names[ART_MORGOTH - 1]);
  299. names[ART_POWER - 1] = string_make("of Power (The One Ring)");
  300. names[ART_GROND - 1] = string_make("'Grond'");
  301. names[ART_MORGOTH - 1] = string_make("of Morgoth");
  302. /* Convert our names array into an a_name structure for later use. */
  303. name_size = 0;
  304. for (i = 1; i < z_info->a_max; i++)
  305. {
  306. name_size += strlen(names[i-1]) + 2; /* skip first char */
  307. }
  308. a_base = C_ZNEW(name_size, char);
  309. a_next = a_base + 1; /* skip first char */
  310. for (i = 1; i < z_info->a_max; i++)
  311. {
  312. my_strcpy(a_next, names[i-1], name_size - 1);
  313. if (a_info[i].tval > 0) /* skip unused! */
  314. a_info[i].name = a_next - a_base;
  315. a_next += strlen(names[i-1]) + 1;
  316. }
  317. /* Free the old names */
  318. FREE(a_name);
  319. for (i = 0; i < z_info->a_max; i++)
  320. {
  321. string_free(names[i]);
  322. }
  323. /* Free the "names" array */
  324. FREE(names);
  325. /* Store the names */
  326. a_name = a_base;
  327. a_head.name_ptr = a_base;
  328. a_head.name_size = name_size;
  329. /* Success */
  330. return (0);
  331. }
  332. /*
  333. * Return the artifact power, by generating a "fake" object based on the
  334. * artifact, and calling the common object_power function
  335. */
  336. static s32b artifact_power(int a_idx)
  337. {
  338. object_type obj;
  339. LOG_PRINT("********** ENTERING EVAL POWER ********\n");
  340. LOG_PRINT1("Artifact index is %d\n", a_idx);
  341. if(!make_fake_artifact(&obj, a_idx))
  342. {
  343. return 0;
  344. }
  345. return object_power(&obj, verbose, log_file, TRUE);
  346. }
  347. /*
  348. * Store the original artifact power ratings as a baseline
  349. */
  350. static void store_base_power (void)
  351. {
  352. int i, j;
  353. artifact_type *a_ptr;
  354. object_kind *k_ptr;
  355. s16b k_idx;
  356. int *fake_power;
  357. max_power = 0;
  358. min_power = 32767;
  359. var_power = 0;
  360. fake_power = C_ZNEW(z_info->a_max, int);
  361. j = 0;
  362. for(i = 0; i < z_info->a_max; i++, j++)
  363. {
  364. base_power[i] = artifact_power(i);
  365. /* capture power stats, ignoring cursed and uber arts */
  366. if (base_power[i] > max_power && base_power[i] < INHIBIT_POWER)
  367. max_power = base_power[i];
  368. if (base_power[i] < min_power && base_power[i] > 0)
  369. min_power = base_power[i];
  370. if (base_power[i] > 0 && base_power[i] < INHIBIT_POWER)
  371. fake_power[j] = (int)base_power[i];
  372. else
  373. j--;
  374. a_ptr = &a_info[i];
  375. k_idx = lookup_kind(a_ptr->tval, a_ptr->sval);
  376. k_ptr = &k_info[k_idx];
  377. base_item_level[i] = k_ptr->level;
  378. base_item_prob[i] = k_ptr->alloc_prob;
  379. base_art_alloc[i] = a_ptr->alloc_prob;
  380. }
  381. avg_power = mean(fake_power, j);
  382. var_power = variance(fake_power, j);
  383. LOG_PRINT2("Max power is %d, min is %d\n", max_power, min_power);
  384. LOG_PRINT2("Mean is %d, variance is %d\n", avg_power, var_power);
  385. /* Store the number of different types, for use later */
  386. /* ToDo: replace this with full combination tracking */
  387. for (i = 0; i < z_info->a_max; i++)
  388. {
  389. switch (a_info[i].tval)
  390. {
  391. case TV_SWORD:
  392. case TV_POLEARM:
  393. case TV_HAFTED:
  394. art_melee_total++; break;
  395. case TV_BOW:
  396. art_bow_total++; break;
  397. case TV_SOFT_ARMOR:
  398. case TV_HARD_ARMOR:
  399. case TV_DRAG_ARMOR:
  400. art_armor_total++; break;
  401. case TV_SHIELD:
  402. art_shield_total++; break;
  403. case TV_CLOAK:
  404. art_cloak_total++; break;
  405. case TV_HELM:
  406. case TV_CROWN:
  407. art_headgear_total++; break;
  408. case TV_GLOVES:
  409. art_glove_total++; break;
  410. case TV_BOOTS:
  411. art_boot_total++; break;
  412. default:
  413. art_other_total++;
  414. }
  415. }
  416. art_total = art_melee_total + art_bow_total + art_armor_total +
  417. art_shield_total + art_cloak_total + art_headgear_total +
  418. art_glove_total + art_boot_total + art_other_total;
  419. }
  420. /*
  421. * Randomly select a base item type (tval,sval). Assign the various fields
  422. * corresponding to that choice.
  423. *
  424. * The return value gives the index of the new item type. The method is
  425. * passed a pointer to a rarity value in order to return the rarity of the
  426. * new item.
  427. */
  428. static s16b choose_item(int a_idx)
  429. {
  430. artifact_type *a_ptr = &a_info[a_idx];
  431. int tval = 0;
  432. int sval = 0;
  433. object_kind *k_ptr;
  434. int i;
  435. s16b k_idx, r;
  436. /*
  437. * Pick a base item from the cumulative frequency table.
  438. *
  439. * Although this looks hideous, it provides for easy addition of
  440. * future artifact types, simply by removing the tvals from this
  441. * loop.
  442. *
  443. * N.B. Could easily generate lights, rings and amulets this way if
  444. * the whole special/flavour issue was sorted out (see ticket #1014)
  445. * Note that Carlammas and Barahir have the same sval as Grond/Morgoth
  446. */
  447. while (tval == 0 || tval == TV_SKELETON || tval == TV_BOTTLE ||
  448. tval == TV_JUNK || tval == TV_SPIKE || tval == TV_CHEST ||
  449. tval == TV_SHOT || tval == TV_ARROW || tval == TV_BOLT ||
  450. tval == TV_STAFF || tval == TV_WAND || tval == TV_ROD ||
  451. tval == TV_SCROLL || tval == TV_POTION || tval == TV_FLASK ||
  452. tval == TV_FOOD || tval == TV_MAGIC_BOOK || tval ==
  453. TV_PRAYER_BOOK || tval == TV_GOLD || tval == TV_LIGHT ||
  454. tval == TV_AMULET || tval == TV_RING || sval == SV_GROND ||
  455. sval == SV_MORGOTH)
  456. {
  457. r = randint1(base_freq[z_info->k_max - 1]);
  458. i = 0;
  459. while (r > base_freq[i])
  460. {
  461. i++;
  462. }
  463. tval = k_info[i].tval;
  464. sval = k_info[i].sval;
  465. }
  466. LOG_PRINT2("Creating tval %d sval %d\n", tval, sval);
  467. k_idx = lookup_kind(tval, sval);
  468. k_ptr = &k_info[k_idx];
  469. a_ptr->tval = k_ptr->tval;
  470. a_ptr->sval = k_ptr->sval;
  471. a_ptr->pval = randcalc(k_ptr->pval, 0, MINIMISE);
  472. a_ptr->to_h = randcalc(k_ptr->to_h, 0, MINIMISE);
  473. a_ptr->to_d = randcalc(k_ptr->to_d, 0, MINIMISE);
  474. a_ptr->to_a = randcalc(k_ptr->to_a, 0, MINIMISE);
  475. a_ptr->ac = k_ptr->ac;
  476. a_ptr->dd = k_ptr->dd;
  477. a_ptr->ds = k_ptr->ds;
  478. a_ptr->weight = k_ptr->weight;
  479. of_copy(a_ptr->flags, k_ptr->flags);
  480. a_ptr->effect = 0;
  481. /* Artifacts ignore everything */
  482. flags_set(a_ptr->flags, OF_SIZE, OF_IGNORE_MASK, FLAG_END);
  483. /* Assign basic stats to the artifact based on its artifact level. */
  484. /*
  485. * CR, 2001-09-03: changed to a simpler version to match the hit-dam
  486. * parsing algorithm. We use random ranges averaging mean_hit_startval
  487. * and mean_dam_startval, but permitting variation of 50% to 150%.
  488. * Level-dependent term has been removed for the moment.
  489. */
  490. switch (a_ptr->tval)
  491. {
  492. case TV_BOW:
  493. case TV_DIGGING:
  494. case TV_HAFTED:
  495. case TV_SWORD:
  496. case TV_POLEARM:
  497. a_ptr->to_h += (s16b)(mean_hit_startval / 2 +
  498. randint0(mean_hit_startval) );
  499. a_ptr->to_d += (s16b)(mean_dam_startval / 2 +
  500. randint0(mean_dam_startval) );
  501. LOG_PRINT2("Assigned basic stats, to_hit: %d, to_dam: %d\n", a_ptr->to_h, a_ptr->to_d);
  502. break;
  503. case TV_BOOTS:
  504. case TV_GLOVES:
  505. case TV_HELM:
  506. case TV_CROWN:
  507. case TV_SHIELD:
  508. case TV_CLOAK:
  509. case TV_SOFT_ARMOR:
  510. case TV_HARD_ARMOR:
  511. case TV_DRAG_ARMOR:
  512. /* CR: adjusted this to work with parsing logic */
  513. a_ptr->to_a += (s16b)(mean_ac_startval / 2 +
  514. randint0(mean_ac_startval) );
  515. LOG_PRINT1("Assigned basic stats, AC bonus: %d\n", a_ptr->to_a);
  516. break;
  517. }
  518. /* Done - return the index of the new object kind. */
  519. return k_idx;
  520. }
  521. /*
  522. * We've just added an ability which uses the pval bonus. Make sure it's
  523. * not zero. If it's currently negative, leave it negative (heh heh).
  524. */
  525. static void do_pval(artifact_type *a_ptr)
  526. {
  527. int factor = 1;
  528. /* Track whether we have blows, might or shots on this item */
  529. if (of_has(a_ptr->flags, OF_BLOWS)) factor++;
  530. if (of_has(a_ptr->flags, OF_MIGHT)) factor++;
  531. if (of_has(a_ptr->flags, OF_SHOTS)) factor++;
  532. if (a_ptr->pval == 0)
  533. {
  534. /* Blows, might, shots handled separately */
  535. if (factor > 1)
  536. {
  537. a_ptr->pval = (s16b)randint1(2);
  538. /* Give it a shot at +3 */
  539. if (INHIBIT_STRONG) a_ptr->pval = 3;
  540. }
  541. else a_ptr->pval = (s16b)randint1(4);
  542. LOG_PRINT1("Assigned initial pval, value is: %d\n", a_ptr->pval);
  543. }
  544. else if (a_ptr->pval < 0)
  545. {
  546. if (one_in_(2))
  547. {
  548. a_ptr->pval--;
  549. LOG_PRINT1("Decreasing pval by 1, new value is: %d\n", a_ptr->pval);
  550. }
  551. }
  552. else if (one_in_(a_ptr->pval * factor))
  553. {
  554. /*
  555. * CR: made this a bit rarer and diminishing with higher pval -
  556. * also rarer if item has blows/might/shots already
  557. */
  558. a_ptr->pval++;
  559. LOG_PRINT1("Increasing pval by 1, new value is: %d\n", a_ptr->pval);
  560. }
  561. }
  562. static void remove_contradictory(artifact_type *a_ptr)
  563. {
  564. if (of_has(a_ptr->flags, OF_AGGRAVATE)) of_off(a_ptr->flags, OF_STEALTH);
  565. if (of_has(a_ptr->flags, OF_IM_ACID)) of_off(a_ptr->flags, OF_RES_ACID);
  566. if (of_has(a_ptr->flags, OF_IM_ELEC)) of_off(a_ptr->flags, OF_RES_ELEC);
  567. if (of_has(a_ptr->flags, OF_IM_FIRE)) of_off(a_ptr->flags, OF_RES_FIRE);
  568. if (of_has(a_ptr->flags, OF_IM_COLD)) of_off(a_ptr->flags, OF_RES_COLD);
  569. if (a_ptr->pval < 0)
  570. {
  571. if (of_has(a_ptr->flags, OF_STR)) of_off(a_ptr->flags, OF_SUST_STR);
  572. if (of_has(a_ptr->flags, OF_INT)) of_off(a_ptr->flags, OF_SUST_INT);
  573. if (of_has(a_ptr->flags, OF_WIS)) of_off(a_ptr->flags, OF_SUST_WIS);
  574. if (of_has(a_ptr->flags, OF_DEX)) of_off(a_ptr->flags, OF_SUST_DEX);
  575. if (of_has(a_ptr->flags, OF_CON)) of_off(a_ptr->flags, OF_SUST_CON);
  576. if (of_has(a_ptr->flags, OF_CHR)) of_off(a_ptr->flags, OF_SUST_CHR);
  577. of_off(a_ptr->flags, OF_BLOWS);
  578. }
  579. if (of_has(a_ptr->flags, OF_LIGHT_CURSE)) of_off(a_ptr->flags, OF_BLESSED);
  580. if (of_has(a_ptr->flags, OF_KILL_DRAGON)) of_off(a_ptr->flags, OF_SLAY_DRAGON);
  581. if (of_has(a_ptr->flags, OF_KILL_DEMON)) of_off(a_ptr->flags, OF_SLAY_DEMON);
  582. if (of_has(a_ptr->flags, OF_KILL_UNDEAD)) of_off(a_ptr->flags, OF_SLAY_UNDEAD);
  583. if (of_has(a_ptr->flags, OF_DRAIN_EXP)) of_off(a_ptr->flags, OF_HOLD_LIFE);
  584. }
  585. /*
  586. * Adjust the parsed frequencies for any peculiarities of the
  587. * algorithm. For example, if stat bonuses and sustains are
  588. * being added in a correlated fashion, it will tend to push
  589. * the frequencies up for both of them. In this method we
  590. * compensate for cases like this by applying corrective
  591. * scaling.
  592. */
  593. static void adjust_freqs(void)
  594. {
  595. /*
  596. * Enforce minimum values for any frequencies that might potentially
  597. * be missing in the standard set, especially supercharged ones.
  598. * Numbers here represent the average number of times this ability
  599. * would appear if the entire randart set was eligible to receive
  600. * it (so in the case of a bow ability: if the set was all bows).
  601. *
  602. * Note that low numbers here for very specialized abilities could
  603. * mean that there's a good chance this ability will not appear in
  604. * a given randart set. If this is a problem, raise the number.
  605. */
  606. if (artprobs[ART_IDX_GEN_RFEAR] < 5)
  607. artprobs[ART_IDX_GEN_RFEAR] = 5;
  608. if (artprobs[ART_IDX_MELEE_DICE_SUPER] < 5)
  609. artprobs[ART_IDX_MELEE_DICE_SUPER] = 5;
  610. if (artprobs[ART_IDX_BOW_SHOTS_SUPER] < 5)
  611. artprobs[ART_IDX_BOW_SHOTS_SUPER] = 5;
  612. if (artprobs[ART_IDX_BOW_MIGHT_SUPER] < 5)
  613. artprobs[ART_IDX_BOW_MIGHT_SUPER] = 5;
  614. if (artprobs[ART_IDX_MELEE_BLOWS_SUPER] < 5)
  615. artprobs[ART_IDX_MELEE_BLOWS_SUPER] = 5;
  616. if (artprobs[ART_IDX_GEN_SPEED_SUPER] < 5)
  617. artprobs[ART_IDX_GEN_SPEED_SUPER] = 5;
  618. if (artprobs[ART_IDX_GEN_AC] < 5)
  619. artprobs[ART_IDX_GEN_AC] = 5;
  620. if (artprobs[ART_IDX_GEN_TUNN] < 5)
  621. artprobs[ART_IDX_GEN_TUNN] = 5;
  622. if (artprobs[ART_IDX_NONWEAPON_BRAND] < 2)
  623. artprobs[ART_IDX_NONWEAPON_BRAND] = 2;
  624. if (artprobs[ART_IDX_NONWEAPON_SLAY] < 2)
  625. artprobs[ART_IDX_NONWEAPON_SLAY] = 2;
  626. if (artprobs[ART_IDX_BOW_BRAND] < 2)
  627. artprobs[ART_IDX_BOW_BRAND] = 2;
  628. if (artprobs[ART_IDX_BOW_SLAY] < 2)
  629. artprobs[ART_IDX_BOW_SLAY] = 2;
  630. if (artprobs[ART_IDX_NONWEAPON_BLOWS] < 2)
  631. artprobs[ART_IDX_NONWEAPON_BLOWS] = 2;
  632. if (artprobs[ART_IDX_NONWEAPON_SHOTS] < 2)
  633. artprobs[ART_IDX_NONWEAPON_SHOTS] = 2;
  634. if (artprobs[ART_IDX_GEN_AC_SUPER] < 5)
  635. artprobs[ART_IDX_GEN_AC_SUPER] = 5;
  636. if (artprobs[ART_IDX_MELEE_AC] < 5)
  637. artprobs[ART_IDX_MELEE_AC] = 5;
  638. /* Cut aggravation frequencies in half since they're used twice */
  639. artprobs[ART_IDX_NONWEAPON_AGGR] /= 2;
  640. artprobs[ART_IDX_WEAPON_AGGR] /= 2;
  641. }
  642. /*
  643. * Parse the list of artifacts and count up the frequencies of the various
  644. * abilities. This is used to give dynamic generation probabilities.
  645. */
  646. static void parse_frequencies(void)
  647. {
  648. int i, j;
  649. const artifact_type *a_ptr;
  650. object_kind *k_ptr;
  651. s32b temp, temp2;
  652. s16b k_idx;
  653. LOG_PRINT("\n****** BEGINNING GENERATION OF FREQUENCIES\n\n");
  654. /* Zero the frequencies for artifact attributes */
  655. for (i = 0; i < ART_IDX_TOTAL; i++)
  656. {
  657. artprobs[i] = 0;
  658. }
  659. /*
  660. * Initialise the frequencies for base items so that each item could
  661. * be chosen - we check for illegal items during choose_item()
  662. */
  663. for (i = 0; i < z_info->k_max; i++)
  664. {
  665. baseprobs[i] = 1;
  666. }
  667. /* Go through the list of all artifacts */
  668. for (i = 0; i < z_info->a_max; i++)
  669. {
  670. LOG_PRINT1("Current artifact index is %d\n", i);
  671. a_ptr = &a_info[i];
  672. /* Special cases -- don't parse these! */
  673. if ((i == ART_POWER) ||
  674. (i == ART_GROND) ||
  675. (i == ART_MORGOTH))
  676. continue;
  677. /* Also don't parse cursed items */
  678. if (base_power[i] < 0) continue;
  679. /* Get a pointer to the base item for this artifact */
  680. k_idx = lookup_kind(a_ptr->tval, a_ptr->sval);
  681. k_ptr = &k_info[k_idx];
  682. /* Add the base item to the baseprobs array */
  683. baseprobs[k_idx]++;
  684. LOG_PRINT1("Base item is %d\n", k_idx);
  685. /* Count up the abilities for this artifact */
  686. if (a_ptr->tval == TV_BOW)
  687. {
  688. if(of_has(a_ptr->flags, OF_SHOTS))
  689. {
  690. /* Do we have 3 or more extra shots? (Unlikely) */
  691. if(a_ptr->pval > 2)
  692. {
  693. LOG_PRINT("Adding 1 for supercharged shots (3 or more!)\n");
  694. (artprobs[ART_IDX_BOW_SHOTS_SUPER])++;
  695. }
  696. else {
  697. LOG_PRINT("Adding 1 for extra shots\n");
  698. (artprobs[ART_IDX_BOW_SHOTS])++;
  699. }
  700. }
  701. if(of_has(a_ptr->flags, OF_MIGHT))
  702. {
  703. /* Do we have 3 or more extra might? (Unlikely) */
  704. if(a_ptr->pval > 2)
  705. {
  706. LOG_PRINT("Adding 1 for supercharged might (3 or more!)\n");
  707. (artprobs[ART_IDX_BOW_MIGHT_SUPER])++;
  708. }
  709. else {
  710. LOG_PRINT("Adding 1 for extra might\n");
  711. (artprobs[ART_IDX_BOW_MIGHT])++;
  712. }
  713. }
  714. /* Brands or slays - count all together */
  715. if (flags_test(a_ptr->flags, OF_SIZE, OF_ALL_SLAY_MASK, FLAG_END))
  716. {
  717. const slay_t *s_ptr;
  718. /* We have some brands or slays - count them */
  719. temp = 0;
  720. temp2 = 0;
  721. for (s_ptr = slay_table; s_ptr->slay_flag; s_ptr++)
  722. {
  723. if (of_has(a_ptr->flags, s_ptr->slay_flag))
  724. {
  725. bitflag slay_kill_mask[OF_SIZE], brand_mask[OF_SIZE];
  726. flags_init(brand_mask, OF_SIZE, OF_BRAND_MASK, FLAG_END);
  727. flags_init(slay_kill_mask, OF_SIZE, OF_SLAY_MASK, OF_KILL_MASK, FLAG_END);
  728. if (of_has(slay_kill_mask, s_ptr->slay_flag)) temp++;
  729. if (of_has(brand_mask, s_ptr->slay_flag)) temp2++;
  730. }
  731. }
  732. LOG_PRINT1("Adding %d for slays\n", temp);
  733. LOG_PRINT1("Adding %d for brands\n", temp2);
  734. /* Add these to the frequency count */
  735. artprobs[ART_IDX_BOW_SLAY] += temp;
  736. artprobs[ART_IDX_BOW_BRAND] += temp2;
  737. }
  738. }
  739. /* Handle hit / dam ratings - are they higher than normal? */
  740. /* Also handle other weapon/nonweapon abilities */
  741. if (a_ptr->tval == TV_BOW || a_ptr->tval == TV_DIGGING ||
  742. a_ptr->tval == TV_HAFTED || a_ptr->tval == TV_POLEARM ||
  743. a_ptr->tval == TV_SWORD)
  744. {
  745. if (a_ptr->to_h - randcalc(k_ptr->to_h, 0, MINIMISE) - mean_hit_startval > 0)
  746. {
  747. temp = (a_ptr->to_d - randcalc(k_ptr->to_d, 0, MINIMISE) - mean_dam_startval) /
  748. mean_dam_increment;
  749. if (temp > 0)
  750. {
  751. LOG_PRINT1("Adding %d instances of extra to-hit bonus for weapon\n", temp);
  752. (artprobs[ART_IDX_WEAPON_HIT]) += temp;
  753. }
  754. }
  755. else if (a_ptr->to_h - randcalc(k_ptr->to_h, 0, MINIMISE) - mean_hit_startval < 0)
  756. {
  757. temp = ( -(a_ptr->to_d - randcalc(k_ptr->to_d, 0, MINIMISE) - mean_dam_startval) ) /
  758. mean_dam_increment;
  759. if (temp > 0)
  760. {
  761. LOG_PRINT1("Subtracting %d instances of extra to-hit bonus for weapon\n", temp);
  762. (artprobs[ART_IDX_WEAPON_HIT]) -= temp;
  763. }
  764. }
  765. if (a_ptr->to_d - randcalc(k_ptr->to_d, 0, MINIMISE) - mean_dam_startval > 0)
  766. {
  767. temp = (a_ptr->to_d - randcalc(k_ptr->to_d, 0, MINIMISE) - mean_dam_startval) /
  768. mean_dam_increment;
  769. if (temp > 0)
  770. {
  771. LOG_PRINT1("Adding %d instances of extra to-dam bonus for weapon\n", temp);
  772. (artprobs[ART_IDX_WEAPON_DAM]) += temp;
  773. }
  774. }
  775. else if (a_ptr->to_d - randcalc(k_ptr->to_d, 0, MINIMISE) - mean_dam_startval < 0)
  776. {
  777. temp = ( -(a_ptr->to_d - randcalc(k_ptr->to_d, 0, MINIMISE) - mean_dam_startval)) /
  778. mean_dam_increment;
  779. if (temp > 0)
  780. {
  781. LOG_PRINT1("Subtracting %d instances of extra to-dam bonus for weapon\n", temp);
  782. (artprobs[ART_IDX_WEAPON_DAM]) -= temp;
  783. }
  784. }
  785. /* Aggravation */
  786. if (of_has(a_ptr->flags, OF_AGGRAVATE))
  787. {
  788. LOG_PRINT("Adding 1 for aggravation - weapon\n");
  789. (artprobs[ART_IDX_WEAPON_AGGR])++;
  790. }
  791. /* End weapon stuff */
  792. }
  793. else
  794. {
  795. if ( (a_ptr->to_h - randcalc(k_ptr->to_h, 0, MINIMISE) > 0) &&
  796. (a_ptr->to_h - randcalc(k_ptr->to_h, 0, MINIMISE) == a_ptr->to_d - randcalc(k_ptr->to_d, 0, MINIMISE)) )
  797. {
  798. /* Special case: both hit and dam bonuses present and equal */
  799. temp = (a_ptr->to_d - randcalc(k_ptr->to_d, 0, MINIMISE)) / mean_dam_increment;
  800. if (temp > 0)
  801. {
  802. LOG_PRINT1("Adding %d instances of extra to-hit and to-dam bonus for non-weapon\n", temp);
  803. (artprobs[ART_IDX_NONWEAPON_HIT_DAM]) += temp;
  804. }
  805. }
  806. else
  807. {
  808. /* Uneven bonuses - handle separately */
  809. if (a_ptr->to_h - randcalc(k_ptr->to_h, 0, MINIMISE) > 0)
  810. {
  811. temp = (a_ptr->to_d - randcalc(k_ptr->to_d, 0, MINIMISE)) / mean_dam_increment;
  812. if (temp > 0)
  813. {
  814. LOG_PRINT1("Adding %d instances of extra to-hit bonus for non-weapon\n", temp);
  815. (artprobs[ART_IDX_NONWEAPON_HIT]) += temp;
  816. }
  817. }
  818. if (a_ptr->to_d - randcalc(k_ptr->to_d, 0, MINIMISE) > 0)
  819. {
  820. temp = (a_ptr->to_d - randcalc(k_ptr->to_d, 0, MINIMISE)) / mean_dam_increment;
  821. if (temp > 0)
  822. {
  823. LOG_PRINT1("Adding %d instances of extra to-dam bonus for non-weapon\n", temp);
  824. (artprobs[ART_IDX_NONWEAPON_DAM]) += temp;
  825. }
  826. }
  827. }
  828. /* Aggravation */
  829. if (of_has(a_ptr->flags, OF_AGGRAVATE))
  830. {
  831. LOG_PRINT("Adding 1 for aggravation - nonweapon\n");
  832. (artprobs[ART_IDX_NONWEAPON_AGGR])++;
  833. }
  834. /* Brands or slays - count all together */
  835. if (flags_test(a_ptr->flags, OF_SIZE, OF_ALL_SLAY_MASK, FLAG_END))
  836. {
  837. const slay_t *s_ptr;
  838. /* We have some brands or slays - count them */
  839. temp = 0;
  840. temp2 = 0;
  841. for (s_ptr = slay_table; s_ptr->slay_flag; s_ptr++)
  842. {
  843. if (of_has(a_ptr->flags, s_ptr->slay_flag))
  844. {
  845. bitflag slay_kill_mask[OF_SIZE], brand_mask[OF_SIZE];
  846. flags_init(brand_mask, OF_SIZE, OF_BRAND_MASK, FLAG_END);
  847. flags_init(slay_kill_mask, OF_SIZE, OF_SLAY_MASK, FLAG_END);
  848. flags_set(slay_kill_mask, OF_SIZE, OF_KILL_MASK, FLAG_END);
  849. if (of_has(slay_kill_mask, s_ptr->slay_flag)) temp++;
  850. if (of_has(brand_mask, s_ptr->slay_flag)) temp2++;
  851. }
  852. }
  853. LOG_PRINT1("Adding %d for slays\n", temp);
  854. LOG_PRINT1("Adding %d for brands\n", temp2);
  855. /* Add these to the frequency count */
  856. artprobs[ART_IDX_NONWEAPON_SLAY] += temp;
  857. artprobs[ART_IDX_NONWEAPON_BRAND] += temp2;
  858. }
  859. if (of_has(a_ptr->flags, OF_BLOWS))
  860. {
  861. LOG_PRINT("Adding 1 for extra blows on nonweapon\n");
  862. (artprobs[ART_IDX_NONWEAPON_BLOWS])++;
  863. }
  864. if (of_has(a_ptr->flags, OF_SHOTS))
  865. {
  866. LOG_PRINT("Adding 1 for extra shots on nonweapon\n");
  867. (artprobs[ART_IDX_NONWEAPON_SHOTS])++;
  868. }
  869. }
  870. if (a_ptr->tval == TV_DIGGING || a_ptr->tval == TV_HAFTED ||
  871. a_ptr->tval == TV_POLEARM || a_ptr->tval == TV_SWORD)
  872. {
  873. /* Blessed weapon? */
  874. if (of_has(a_ptr->flags, OF_BLESSED))
  875. {
  876. LOG_PRINT("Adding 1 for blessed weapon\n");
  877. (artprobs[ART_IDX_MELEE_BLESS])++;
  878. }
  879. /* See invisible? */
  880. if (of_has(a_ptr->flags, OF_SEE_INVIS))
  881. {
  882. LOG_PRINT("Adding 1 for see invisible (weapon case)\n");
  883. (artprobs[ART_IDX_MELEE_SINV])++;
  884. }
  885. /* Does this weapon have extra blows? */
  886. if (of_has(a_ptr->flags, OF_BLOWS))
  887. {
  888. /* Do we have 3 or more extra blows? (Unlikely) */
  889. if(a_ptr->pval > 2)
  890. {
  891. LOG_PRINT("Adding 1 for supercharged blows (3 or more!)\n");
  892. (artprobs[ART_IDX_MELEE_BLOWS_SUPER])++;
  893. }
  894. else {
  895. LOG_PRINT("Adding 1 for extra blows\n");
  896. (artprobs[ART_IDX_MELEE_BLOWS])++;
  897. }
  898. }
  899. /* Does this weapon have an unusual bonus to AC? */
  900. if ( (a_ptr->to_a - randcalc(k_ptr->to_a, 0, MAXIMISE)) > 0)
  901. {
  902. temp = (a_ptr->to_a - randcalc(k_ptr->to_a, 0, MAXIMISE)) / mean_ac_increment;
  903. if (temp > 0)
  904. {
  905. LOG_PRINT1("Adding %d instances of extra AC bonus for weapon\n", temp);
  906. (artprobs[ART_IDX_MELEE_AC]) += temp;
  907. }
  908. }
  909. /* Check damage dice - are they more than normal? */
  910. if (a_ptr->dd > k_ptr->dd)
  911. {
  912. /* Difference of 3 or more? */
  913. if ( (a_ptr->dd - k_ptr->dd) > 2)
  914. {
  915. LOG_PRINT("Adding 1 for super-charged damage dice!\n");
  916. (artprobs[ART_IDX_MELEE_DICE_SUPER])++;
  917. }
  918. else
  919. {
  920. LOG_PRINT("Adding 1 for extra damage dice.\n");
  921. (artprobs[ART_IDX_MELEE_DICE])++;
  922. }
  923. }
  924. /* Check weight - is it different from normal? */
  925. if (a_ptr->weight != k_ptr->weight)
  926. {
  927. LOG_PRINT("Adding 1 for unusual weight.\n");
  928. (artprobs[ART_IDX_MELEE_WEIGHT])++;
  929. }
  930. /* Check for tunnelling ability */
  931. if (of_has(a_ptr->flags, OF_TUNNEL))
  932. {
  933. LOG_PRINT("Adding 1 for tunnelling bonus.\n");
  934. (artprobs[ART_IDX_MELEE_TUNN])++;
  935. }
  936. /* Brands or slays - count all together */
  937. if (flags_test(a_ptr->flags, OF_SIZE, OF_ALL_SLAY_MASK, FLAG_END))
  938. {
  939. const slay_t *s_ptr;
  940. /* We have some brands or slays - count them */
  941. temp = 0;
  942. temp2 = 0;
  943. for (s_ptr = slay_table; s_ptr->slay_flag; s_ptr++)
  944. {
  945. if (of_has(a_ptr->flags, s_ptr->slay_flag))
  946. {
  947. bitflag slay_kill_mask[OF_SIZE], brand_mask[OF_SIZE];
  948. flags_init(brand_mask, OF_SIZE, OF_BRAND_MASK, FLAG_END);
  949. flags_init(slay_kill_mask, OF_SIZE, OF_SLAY_MASK, FLAG_END);
  950. flags_set(slay_kill_mask, OF_SIZE, OF_KILL_MASK, FLAG_END);
  951. if (of_has(slay_kill_mask, s_ptr->slay_flag)) temp++;
  952. if (of_has(brand_mask, s_ptr->slay_flag)) temp2++;
  953. }
  954. }
  955. LOG_PRINT1("Adding %d for slays\n", temp);
  956. LOG_PRINT1("Adding %d for brands\n", temp2);
  957. /* Add these to the frequency count */
  958. artprobs[ART_IDX_MELEE_SLAY] += temp;
  959. artprobs[ART_IDX_MELEE_BRAND] += temp2;
  960. }
  961. /* End of weapon-specific stuff */
  962. }
  963. else
  964. {
  965. /* Check for tunnelling ability */
  966. if (of_has(a_ptr->flags, OF_TUNNEL))
  967. {
  968. LOG_PRINT("Adding 1 for tunnelling bonus - general.\n");
  969. (artprobs[ART_IDX_GEN_TUNN])++;
  970. }
  971. }
  972. /*
  973. * Count up extra AC bonus values.
  974. * Could also add logic to subtract for lower values here, but it's
  975. * probably not worth the trouble since it's so rare.
  976. */
  977. if ( (a_ptr->to_a - randcalc(k_ptr->to_a, 0, MINIMISE) - mean_ac_startval) > 0)
  978. {
  979. temp = (a_ptr->to_a - randcalc(k_ptr->to_a, 0, MINIMISE) - mean_ac_startval) /
  980. mean_ac_increment;
  981. if (temp > 0)
  982. {
  983. if (a_ptr->to_a > 20)
  984. {
  985. LOG_PRINT1("Adding %d for supercharged AC\n", temp);
  986. (artprobs[ART_IDX_GEN_AC_SUPER])++;
  987. }
  988. else if (a_ptr->tval == TV_BOOTS)
  989. {
  990. LOG_PRINT1("Adding %d for AC bonus - boots\n", temp);
  991. (artprobs[ART_IDX_BOOT_AC]) += temp;
  992. }
  993. else if (a_ptr->tval == TV_GLOVES)
  994. {
  995. LOG_PRINT1("Adding %d for AC bonus - gloves\n", temp);
  996. (artprobs[ART_IDX_GLOVE_AC]) += temp;
  997. }
  998. else if (a_ptr->tval == TV_HELM || a_ptr->tval == TV_CROWN)
  999. {
  1000. LOG_PRINT1("Adding %d for AC bonus - headgear\n", temp);
  1001. (artprobs[ART_IDX_HELM_AC]) += temp;
  1002. }
  1003. else if (a_ptr->tval == TV_SHIELD)
  1004. {
  1005. LOG_PRINT1("Adding %d for AC bonus - shield\n", temp);
  1006. (artprobs[ART_IDX_SHIELD_AC]) += temp;
  1007. }
  1008. else if (a_ptr->tval == TV_CLOAK)
  1009. {
  1010. LOG_PRINT1("Adding %d for AC bonus - cloak\n", temp);
  1011. (artprobs[ART_IDX_CLOAK_AC]) += temp;
  1012. }
  1013. else if (a_ptr->tval == TV_SOFT_ARMOR || a_ptr->tval == TV_HARD_ARMOR ||
  1014. a_ptr->tval == TV_DRAG_ARMOR)
  1015. {
  1016. LOG_PRINT1("Adding %d for AC bonus - body armor\n", temp);
  1017. (artprobs[ART_IDX_ARMOR_AC]) += temp;
  1018. }
  1019. else
  1020. {
  1021. LOG_PRINT1("Adding %d for AC bonus - general\n", temp);
  1022. (artprobs[ART_IDX_GEN_AC]) += temp;
  1023. }
  1024. }
  1025. }
  1026. /* Generic armor abilities */
  1027. if (a_ptr->tval == TV_BOOTS || a_ptr->tval == TV_GLOVES ||
  1028. a_ptr->tval == TV_HELM || a_ptr->tval == TV_CROWN ||
  1029. a_ptr->tval == TV_SHIELD || a_ptr->tval == TV_CLOAK ||
  1030. a_ptr->tval == TV_SOFT_ARMOR || a_ptr->tval == TV_HARD_ARMOR ||
  1031. a_ptr->tval == TV_DRAG_ARMOR)
  1032. {
  1033. /* Check weight - is it different from normal? */
  1034. /* ToDo: count higher and lower separately */
  1035. if (a_ptr->weight != k_ptr->weight)
  1036. {
  1037. LOG_PRINT("Adding 1 for unusual weight.\n");
  1038. (artprobs[ART_IDX_ALLARMOR_WEIGHT])++;
  1039. }
  1040. /* Done with generic armor abilities */
  1041. }
  1042. /*
  1043. * General abilities. This section requires a bit more work
  1044. * than the others, because we have to consider cases where
  1045. * a certain ability might be found in a particular item type.
  1046. * For example, ESP is commonly found on headgear, so when
  1047. * we count ESP we must add it to either the headgear or
  1048. * general tally, depending on the base item. This permits
  1049. * us to have general abilities appear more commonly on a
  1050. * certain item type.
  1051. */
  1052. if (flags_test(a_ptr->flags, OF_SIZE, OF_STR, OF_INT, OF_WIS,
  1053. OF_DEX, OF_CON, OF_CHR, FLAG_END))
  1054. {
  1055. /* Stat bonus case. Add up the number of individual
  1056. bonuses */
  1057. temp = 0;
  1058. if (of_has(a_ptr->flags, OF_STR)) temp++;
  1059. if (of_has(a_ptr->flags, OF_INT)) temp++;
  1060. if (of_has(a_ptr->flags, OF_WIS)) temp++;
  1061. if (of_has(a_ptr->flags, OF_DEX)) temp++;
  1062. if (of_has(a_ptr->flags, OF_CON)) temp++;
  1063. if (of_has(a_ptr->flags, OF_CHR)) temp++;
  1064. /* Handle a few special cases separately. */
  1065. if((a_ptr->tval == TV_HELM || a_ptr->tval == TV_CROWN) &&
  1066. (of_has(a_ptr->flags, OF_WIS) || of_has(a_ptr->flags, OF_INT)))
  1067. {
  1068. /* Handle WIS and INT on helms and crowns */
  1069. if(of_has(a_ptr->flags, OF_WIS))
  1070. {
  1071. LOG_PRINT("Adding 1 for WIS bonus on headgear.\n");
  1072. (artprobs[ART_IDX_HELM_WIS])++;
  1073. /* Counted this one separately so subtract it here */
  1074. temp--;
  1075. }
  1076. if(of_has(a_ptr->flags, OF_INT))
  1077. {
  1078. LOG_PRINT("Adding 1 for INT bonus on headgear.\n");
  1079. (artprobs[ART_IDX_HELM_INT])++;
  1080. /* Counted this one separately so subtract it here */
  1081. temp--;
  1082. }
  1083. }
  1084. else if ((a_ptr->tval == TV_SOFT_ARMOR ||
  1085. a_ptr->tval == TV_HARD_ARMOR ||
  1086. a_ptr->tval == TV_DRAG_ARMOR) && of_has(a_ptr->flags, OF_CON))
  1087. {
  1088. /* Handle CON bonus on armor */
  1089. LOG_PRINT("Adding 1 for CON bonus on body armor.\n");
  1090. (artprobs[ART_IDX_ARMOR_CON])++;
  1091. /* Counted this one separately so subtract it here */
  1092. temp--;
  1093. }
  1094. else if (a_ptr->tval == TV_GLOVES && of_has(a_ptr->flags, OF_DEX))
  1095. {
  1096. /* Handle DEX bonus on gloves */
  1097. LOG_PRINT("Adding 1 for DEX bonus on gloves.\n");
  1098. (artprobs[ART_IDX_GLOVE_DEX])++;
  1099. /* Counted this one separately so subtract it here */
  1100. temp--;
  1101. }
  1102. /* Now the general case */
  1103. if (temp > 0)
  1104. {
  1105. /* There are some bonuses that weren't handled above */
  1106. LOG_PRINT1("Adding %d for stat bonuses - general.\n", temp);
  1107. (artprobs[ART_IDX_GEN_STAT]) += temp;
  1108. /* Done with stat bonuses */
  1109. }
  1110. }
  1111. if (flags_test(a_ptr->flags, OF_SIZE, OF_SUST_STR, OF_SUST_INT,
  1112. OF_SUST_WIS, OF_SUST_DEX, OF_SUST_CON,
  1113. OF_SUST_CHR, FLAG_END))
  1114. {
  1115. /* Now do sustains, in a similar manner */
  1116. temp = 0;
  1117. if (of_has(a_ptr->flags, OF_SUST_STR)) temp++;
  1118. if (of_has(a_ptr->flags, OF_SUST_INT)) temp++;
  1119. if (of_has(a_ptr->flags, OF_SUST_WIS)) temp++;
  1120. if (of_has(a_ptr->flags, OF_SUST_DEX)) temp++;
  1121. if (of_has(a_ptr->flags, OF_SUST_CON)) temp++;
  1122. if (of_has(a_ptr->flags, OF_SUST_CHR)) temp++;
  1123. LOG_PRINT1("Adding %d for stat sustains.\n", temp);
  1124. (artprobs[ART_IDX_GEN_SUST]) += temp;
  1125. }
  1126. if (of_has(a_ptr->flags, OF_STEALTH))
  1127. {
  1128. /* Handle stealth, including a couple of special cases */
  1129. if(a_ptr->tval == TV_BOOTS)
  1130. {
  1131. LOG_PRINT("Adding 1 for stealth bonus on boots.\n");
  1132. (artprobs[ART_IDX_BOOT_STEALTH])++;
  1133. }
  1134. else if (a_ptr->tval == TV_CLOAK)
  1135. {
  1136. LOG_PRINT("Adding 1 for stealth bonus on cloak.\n");
  1137. (artprobs[ART_IDX_CLOAK_STEALTH])++;
  1138. }
  1139. else if (a_ptr->tval == TV_SOFT_ARMOR ||
  1140. a_ptr->tval == TV_HARD_ARMOR || a_ptr->tval == TV_DRAG_ARMOR)
  1141. {
  1142. LOG_PRINT("Adding 1 for stealth bonus on armor.\n");
  1143. (artprobs[ART_IDX_ARMOR_STEALTH])++;
  1144. }
  1145. else
  1146. {
  1147. /* General case */
  1148. LOG_PRINT("Adding 1 for stealth bonus - general.\n");
  1149. (artprobs[ART_IDX_GEN_STEALTH])++;
  1150. }
  1151. /* Done with stealth */
  1152. }
  1153. if (of_has(a_ptr->flags, OF_SEARCH))
  1154. {
  1155. /* Handle searching bonus - fully generic this time */
  1156. LOG_PRINT("Adding 1 for search bonus - general.\n");
  1157. (artprobs[ART_IDX_GEN_SEARCH])++;
  1158. }
  1159. if (of_has(a_ptr->flags, OF_INFRA))
  1160. {
  1161. /* Handle infravision bonus - fully generic */
  1162. LOG_PRINT("Adding 1 for infravision bonus - general.\n");
  1163. (artprobs[ART_IDX_GEN_INFRA])++;
  1164. }
  1165. if (of_has(a_ptr->flags, OF_SPEED))
  1166. {
  1167. /*
  1168. * Speed - boots handled separately.
  1169. * This is something of a special case in that we use the same
  1170. * frequency for the supercharged value and the normal value.
  1171. * We get away with this by using a somewhat lower average value
  1172. * for the supercharged ability than in the basic set (around
  1173. * +7 or +8 - c.f. Ringil and the others at +10 and upwards).
  1174. * This then allows us to add an equal number of
  1175. * small bonuses around +3 or so without unbalancing things.
  1176. */
  1177. if (a_ptr->pval > 7)
  1178. {
  1179. /* Supercharge case */
  1180. LOG_PRINT("Adding 1 for supercharged speed bonus!\n");
  1181. (artprobs[ART_IDX_GEN_SPEED_SUPER])++;
  1182. }
  1183. else if(a_ptr->tval == TV_BOOTS)
  1184. {
  1185. /* Handle boots separately */
  1186. LOG_PRINT("Adding 1 for normal speed bonus on boots.\n");
  1187. (artprobs[ART_IDX_BOOT_SPEED])++;
  1188. }
  1189. else
  1190. {
  1191. LOG_PRINT("Adding 1 for normal speed bonus - general.\n");
  1192. (artprobs[ART_IDX_GEN_SPEED])++;
  1193. }
  1194. /* Done with speed */
  1195. }
  1196. if (flags_test(a_ptr->flags, OF_SIZE, OF_IM_ACID, OF_IM_ELEC,
  1197. OF_IM_FIRE, OF_IM_COLD, FLAG_END))
  1198. {
  1199. /* Count up immunities for this item, if any */
  1200. temp = 0;
  1201. if (of_has(a_ptr->flags, OF_IM_ACID)) temp++;
  1202. if (of_has(a_ptr->flags, OF_IM_ELEC)) temp++;
  1203. if (of_has(a_ptr->flags, OF_IM_FIRE)) temp++;
  1204. if (of_has(a_ptr->flags, OF_IM_COLD)) temp++;
  1205. LOG_PRINT1("Adding %d for immunities.\n", temp);
  1206. (artprobs[ART_IDX_GEN_IMMUNE]) += temp;
  1207. }
  1208. if (of_has(a_ptr->flags, OF_FREE_ACT))
  1209. {
  1210. /* Free action - handle gloves separately */
  1211. if(a_ptr->tval == TV_GLOVES)
  1212. {
  1213. LOG_PRINT("Adding 1 for free action on gloves.\n");
  1214. (artprobs[ART_IDX_GLOVE_FA])++;
  1215. }
  1216. else
  1217. {
  1218. LOG_PRINT("Adding 1 for free action - general.\n");
  1219. (artprobs[ART_IDX_GEN_FA])++;
  1220. }
  1221. }
  1222. if (of_has(a_ptr->flags, OF_HOLD_LIFE))
  1223. {
  1224. /* Hold life - do body armor separately */
  1225. if( (a_ptr->tval == TV_SOFT_ARMOR) || (a_ptr->tval == TV_HARD_ARMOR) ||
  1226. (a_ptr->tval == TV_DRAG_ARMOR))
  1227. {
  1228. LOG_PRINT("Adding 1 for hold life on armor.\n");
  1229. (artprobs[ART_IDX_ARMOR_HLIFE])++;
  1230. }
  1231. else
  1232. {
  1233. LOG_PRINT("Adding 1 for hold life - general.\n");
  1234. (artprobs[ART_IDX_GEN_HLIFE])++;
  1235. }
  1236. }
  1237. if (of_has(a_ptr->flags, OF_FEATHER))
  1238. {
  1239. /* Feather fall - handle boots separately */
  1240. if(a_ptr->tval == TV_BOOTS)
  1241. {
  1242. LOG_PRINT("Adding 1 for feather fall on boots.\n");
  1243. (artprobs[ART_IDX_BOOT_FEATHER])++;
  1244. }
  1245. else
  1246. {
  1247. LOG_PRINT("Adding 1 for feather fall - general.\n");
  1248. (artprobs[ART_IDX_GEN_FEATHER])++;
  1249. }
  1250. }
  1251. if (of_has(a_ptr->flags, OF_LIGHT))
  1252. {
  1253. /* Handle permanent light */
  1254. LOG_PRINT("Adding 1 for permanent light - general.\n");
  1255. (artprobs[ART_IDX_GEN_LIGHT])++;
  1256. }
  1257. if (of_has(a_ptr->flags, OF_SEE_INVIS))
  1258. {
  1259. /*
  1260. * Handle see invisible - do helms / crowns separately
  1261. * (Weapons were done already so exclude them)
  1262. */
  1263. if( !(a_ptr->tval == TV_DIGGING || a_ptr->tval == TV_HAFTED ||
  1264. a_ptr->tval == TV_POLEARM || a_ptr->tval == TV_SWORD))
  1265. {
  1266. if (a_ptr->tval == TV_HELM || a_ptr->tval == TV_CROWN)
  1267. {
  1268. LOG_PRINT("Adding 1 for see invisible - headgear.\n");
  1269. (artprobs[ART_IDX_HELM_SINV])++;
  1270. }
  1271. else
  1272. {
  1273. LOG_PRINT("Adding 1 for see invisible - general.\n");
  1274. (artprobs[ART_IDX_GEN_SINV])++;
  1275. }
  1276. }
  1277. }
  1278. if (of_has(a_ptr->flags, OF_TELEPATHY))
  1279. {
  1280. /* ESP case. Handle helms/crowns separately. */
  1281. if(a_ptr->tval == TV_HELM || a_ptr->tval == TV_CROWN)
  1282. {
  1283. LOG_PRINT("Adding 1 for ESP on headgear.\n");
  1284. (artprobs[ART_IDX_HELM_ESP])++;
  1285. }
  1286. else
  1287. {
  1288. LOG_PRINT("Adding 1 for ESP - general.\n");
  1289. (artprobs[ART_IDX_GEN_ESP])++;
  1290. }
  1291. }
  1292. if (of_has(a_ptr->flags, OF_SLOW_DIGEST))
  1293. {
  1294. /* Slow digestion case - generic. */
  1295. LOG_PRINT("Adding 1 for slow digestion - general.\n");
  1296. (artprobs[ART_IDX_GEN_SDIG])++;
  1297. }
  1298. if (of_has(a_ptr->flags, OF_REGEN))
  1299. {
  1300. /* Regeneration case - generic. */
  1301. LOG_PRINT("Adding 1 for regeneration - general.\n");
  1302. (artprobs[ART_IDX_GEN_REGEN])++;
  1303. }
  1304. if (flags_test(a_ptr->flags, OF_SIZE, OF_RES_ACID, OF_RES_ELEC,
  1305. OF_RES_FIRE, OF_RES_COLD, FLAG_END))
  1306. {
  1307. /* Count up low resists (not the type, just the number) */
  1308. temp = 0;
  1309. if (of_has(a_ptr->flags, OF_RES_ACID)) temp++;
  1310. if (of_has(a_ptr->flags, OF_RES_ELEC)) temp++;
  1311. if (of_has(a_ptr->flags, OF_RES_FIRE)) temp++;
  1312. if (of_has(a_ptr->flags, OF_RES_COLD)) temp++;
  1313. /* Shields treated separately */
  1314. if (a_ptr->tval == TV_SHIELD)
  1315. {
  1316. LOG_PRINT1("Adding %d for low resists on shield.\n", temp);
  1317. (artprobs[ART_IDX_SHIELD_LRES]) += temp;
  1318. }
  1319. else if (a_ptr->tval == TV_SOFT_ARMOR ||
  1320. a_ptr->tval == TV_HARD_ARMOR || a_ptr->tval == TV_DRAG_ARMOR)
  1321. {
  1322. /* Armor also treated separately */
  1323. if (temp == 4)
  1324. {
  1325. /* Special case: armor has all four low resists */
  1326. LOG_PRINT("Adding 1 for ALL LOW RESISTS on body armor.\n");
  1327. (artprobs[ART_IDX_ARMOR_ALLRES])++;
  1328. }
  1329. else
  1330. {
  1331. /* Just tally up the resists as usual */
  1332. LOG_PRINT1("Adding %d for low resists on body armor.\n", temp);
  1333. (artprobs[ART_IDX_ARMOR_LRES]) += temp;
  1334. }
  1335. }
  1336. else
  1337. {
  1338. /* General case */
  1339. LOG_PRINT1("Adding %d for low resists - general.\n", temp);
  1340. (artprobs[ART_IDX_GEN_LRES]) += temp;
  1341. }
  1342. /* Done with low resists */
  1343. }
  1344. /*
  1345. * If the item is body armor then count up all the high resists before
  1346. * going through them individually. High resists are an important
  1347. * component of body armor so we track probability for them separately.
  1348. * The proportions of the high resists will be determined by the
  1349. * generic frequencies - this number just tracks the total.
  1350. */
  1351. if (a_ptr->tval == TV_SOFT_ARMOR ||
  1352. a_ptr->tval == TV_HARD_ARMOR || a_ptr->tval == TV_DRAG_ARMOR)
  1353. {
  1354. temp = 0;
  1355. if (of_has(a_ptr->flags, OF_RES_POIS)) temp++;
  1356. if (of_has(a_ptr->flags, OF_RES_FEAR)) temp++;
  1357. if (of_has(a_ptr->flags, OF_RES_LIGHT)) temp++;
  1358. if (of_has(a_ptr->flags, OF_RES_DARK)) temp++;
  1359. if (of_has(a_ptr->flags, OF_RES_BLIND)) temp++;
  1360. if (of_has(a_ptr->flags, OF_RES_CONFU)) temp++;
  1361. if (of_has(a_ptr->flags, OF_RES_SOUND)) temp++;
  1362. if (of_has(a_ptr->flags, OF_RES_SHARD)) temp++;
  1363. if (of_has(a_ptr->flags, OF_RES_NEXUS)) temp++;
  1364. if (of_has(a_ptr->flags, OF_RES_NETHR)) temp++;
  1365. if (of_has(a_ptr->flags, OF_RES_CHAOS)) temp++;
  1366. if (of_has(a_ptr->flags, OF_RES_DISEN)) temp++;
  1367. LOG_PRINT1("Adding %d for high resists on body armor.\n", temp);
  1368. (artprobs[ART_IDX_ARMOR_HRES]) += temp;
  1369. }
  1370. /* Now do the high resists individually */
  1371. if (of_has(a_ptr->flags, OF_RES_POIS))
  1372. {
  1373. /* Resist poison ability */
  1374. LOG_PRINT("Adding 1 for resist poison - general.\n");
  1375. (artprobs[ART_IDX_GEN_RPOIS])++;
  1376. }
  1377. if (of_has(a_ptr->flags, OF_RES_FEAR))
  1378. {
  1379. /* Resist fear ability */
  1380. LOG_PRINT("Adding 1 for resist fear - general.\n");
  1381. (artprobs[ART_IDX_GEN_RFEAR])++;
  1382. }
  1383. if (of_has(a_ptr->flags, OF_RES_LIGHT))
  1384. {
  1385. /* Resist light ability */
  1386. LOG_PRINT("Adding 1 for resist light - general.\n");
  1387. (artprobs[ART_IDX_GEN_RLIGHT])++;
  1388. }
  1389. if (of_has(a_ptr->flags, OF_RES_DARK))
  1390. {
  1391. /* Resist dark ability */
  1392. LOG_PRINT("Adding 1 for resist dark - general.\n");
  1393. (artprobs[ART_IDX_GEN_RDARK])++;
  1394. }
  1395. if (of_has(a_ptr->flags, OF_RES_BLIND))
  1396. {
  1397. /* Resist blind ability - helms/crowns are separate */
  1398. if(a_ptr->tval == TV_HELM || a_ptr->tval == TV_CROWN)
  1399. {
  1400. LOG_PRINT("Adding 1 for resist blindness - headgear.\n");
  1401. (artprobs[ART_IDX_HELM_RBLIND])++;
  1402. }
  1403. else
  1404. {
  1405. /* General case */
  1406. LOG_PRINT("Adding 1 for resist blindness - general.\n");
  1407. (artprobs[ART_IDX_GEN_RBLIND])++;
  1408. }
  1409. }
  1410. if (of_has(a_ptr->flags, OF_RES_CONFU))
  1411. {
  1412. /* Resist confusion ability */
  1413. LOG_PRINT("Adding 1 for resist confusion - general.\n");
  1414. (artprobs[ART_IDX_GEN_RCONF])++;
  1415. }
  1416. if (of_has(a_ptr->flags, OF_RES_SOUND))
  1417. {
  1418. /* Resist sound ability */
  1419. LOG_PRINT("Adding 1 for resist sound - general.\n");
  1420. (artprobs[ART_IDX_GEN_RSOUND])++;
  1421. }
  1422. if (of_has(a_ptr->flags, OF_RES_SHARD))
  1423. {
  1424. /* Resist shards ability */
  1425. LOG_PRINT("Adding 1 for resist shards - general.\n");
  1426. (artprobs[ART_IDX_GEN_RSHARD])++;
  1427. }
  1428. if (of_has(a_ptr->flags, OF_RES_NEXUS))
  1429. {
  1430. /* Resist nexus ability */
  1431. LOG_PRINT("Adding 1 for resist nexus - general.\n");
  1432. (artprobs[ART_IDX_GEN_RNEXUS])++;
  1433. }
  1434. if (of_has(a_ptr->flags, OF_RES_NETHR))
  1435. {
  1436. /* Resist nether ability */
  1437. LOG_PRINT("Adding 1 for resist nether - general.\n");
  1438. (artprobs[ART_IDX_GEN_RNETHER])++;
  1439. }
  1440. if (of_has(a_ptr->flags, OF_RES_CHAOS))
  1441. {
  1442. /* Resist chaos ability */
  1443. LOG_PRINT("Adding 1 for resist chaos - general.\n");
  1444. (artprobs[ART_IDX_GEN_RCHAOS])++;
  1445. }
  1446. if (of_has(a_ptr->flags, OF_RES_DISEN))
  1447. {
  1448. /* Resist disenchantment ability */
  1449. LOG_PRINT("Adding 1 for resist disenchantment - general.\n");
  1450. (artprobs[ART_IDX_GEN_RDISEN])++;
  1451. }
  1452. if (a_ptr->effect)
  1453. {
  1454. /* Activation */
  1455. LOG_PRINT("Adding 1 for activation.\n");
  1456. (artprobs[ART_IDX_GEN_ACTIV])++;
  1457. }
  1458. /* Done with parsing of frequencies for this item */
  1459. }
  1460. /* End for loop */
  1461. if (verbose)
  1462. {
  1463. /* Print out some of the abilities, to make sure that everything's fine */
  1464. for (i = 0; i < ART_IDX_TOTAL; i++)
  1465. {
  1466. file_putf(log_file, "Frequency of ability %d: %d\n", i, artprobs[i]);
  1467. }
  1468. for (i = 0; i < z_info->k_max; i++)
  1469. {
  1470. file_putf(log_file, "Frequency of item %d: %d\n", i, baseprobs[i]);
  1471. }
  1472. }
  1473. /*
  1474. * Rescale the abilities so that dependent / independent abilities are
  1475. * comparable. We do this by rescaling the frequencies for item-dependent
  1476. * abilities as though the entire set was made up of that item type. For
  1477. * example, if one bow out of three has extra might, and there are 120
  1478. * artifacts in the full set, we rescale the frequency for extra might to
  1479. * 40 (if we had 120 randart bows, about 40 would have extra might).
  1480. *
  1481. * This will allow us to compare the frequencies of all ability types,
  1482. * no matter what the dependency. We assume that generic abilities (like
  1483. * resist fear in the current version) don't need rescaling. This
  1484. * introduces some inaccuracy in cases where specific instances of an
  1485. * ability (like INT bonus on helms) have been counted separately -
  1486. * ideally we should adjust for this in the general case. However, as
  1487. * long as this doesn't occur too often, it shouldn't be a big issue.
  1488. *
  1489. * The following loops look complicated, but they are simply equivalent
  1490. * to going through each of the relevant ability types one by one.
  1491. */
  1492. /* Bow-only abilities */
  1493. for (i = 0; i < ART_IDX_BOW_COUNT; i++)
  1494. {
  1495. artprobs[art_idx_bow[i]] = (artprobs[art_idx_bow[i]] * art_total)
  1496. / art_bow_total;
  1497. }
  1498. /* All weapon abilities */
  1499. for (i = 0; i < ART_IDX_WEAPON_COUNT; i++)
  1500. {
  1501. artprobs[art_idx_weapon[i]] = (artprobs[art_idx_weapon[i]] *
  1502. art_total) / (art_bow_total + art_melee_total);
  1503. }
  1504. /* Corresponding non-weapon abilities */
  1505. temp = art_total - art_melee_total - art_bow_total;
  1506. for (i = 0; i < ART_IDX_NONWEAPON_COUNT; i++)
  1507. {
  1508. artprobs[art_idx_nonweapon[i]] = (artprobs[art_idx_nonweapon[i]] *
  1509. art_total) / temp;
  1510. }
  1511. /* All melee weapon abilities */
  1512. for (i = 0; i < ART_IDX_MELEE_COUNT; i++)
  1513. {
  1514. artprobs[art_idx_melee[i]] = (artprobs[art_idx_melee[i]] *
  1515. art_total) / art_melee_total;
  1516. }
  1517. /* All general armor abilities */
  1518. temp = art_armor_total + art_boot_total + art_shield_total +
  1519. art_headgear_total + art_cloak_total + art_glove_total;
  1520. for (i = 0; i < ART_IDX_ALLARMOR_COUNT; i++)
  1521. {
  1522. artprobs[art_idx_allarmor[i]] = (artprobs[art_idx_allarmor[i]] *
  1523. art_total) / temp;
  1524. }
  1525. /* Boots */
  1526. for (i = 0; i < ART_IDX_BOOT_COUNT; i++)
  1527. {
  1528. artprobs[art_idx_boot[i]] = (artprobs[art_idx_boot[i]] *
  1529. art_total) / art_boot_total;
  1530. }
  1531. /* Gloves */
  1532. for (i = 0; i < ART_IDX_GLOVE_COUNT; i++)
  1533. {
  1534. artprobs[art_idx_glove[i]] = (artprobs[art_idx_glove[i]] *
  1535. art_total) / art_glove_total;
  1536. }
  1537. /* Headgear */
  1538. for (i = 0; i < ART_IDX_HELM_COUNT; i++)
  1539. {
  1540. artprobs[art_idx_headgear[i]] = (artprobs[art_idx_headgear[i]] *
  1541. art_total) / art_headgear_total;
  1542. }
  1543. /* Shields */
  1544. for (i = 0; i < ART_IDX_SHIELD_COUNT; i++)
  1545. {
  1546. artprobs[art_idx_shield[i]] = (artprobs[art_idx_shield[i]] *
  1547. art_total) / art_shield_total;
  1548. }
  1549. /* Cloaks */
  1550. for (i = 0; i < ART_IDX_CLOAK_COUNT; i++)
  1551. {
  1552. artprobs[art_idx_cloak[i]] = (artprobs[art_idx_cloak[i]] *
  1553. art_total) / art_cloak_total;
  1554. }
  1555. /* Body armor */
  1556. for (i = 0; i < ART_IDX_ARMOR_COUNT; i++)
  1557. {
  1558. artprobs[art_idx_armor[i]] = (artprobs[art_idx_armor[i]] *
  1559. art_total) / art_armor_total;
  1560. }
  1561. /*
  1562. * All others are general case and don't need to be rescaled,
  1563. * unless the algorithm is getting too clever about separating
  1564. * out individual cases (in which case some logic should be
  1565. * added for them in the following method call).
  1566. */
  1567. /* Perform any additional rescaling and adjustment, if required. */
  1568. adjust_freqs();
  1569. /* Log the final frequencies to check that everything's correct */
  1570. for(i = 0; i < ART_IDX_TOTAL; i++)
  1571. {
  1572. LOG_PRINT2( "Rescaled frequency of ability %d: %d\n", i, artprobs[i]);
  1573. }
  1574. /* Build a cumulative frequency table for the base items */
  1575. for (i = 0; i < z_info->k_max; i++)
  1576. {
  1577. for (j = i; j < z_info->k_max; j++)
  1578. {
  1579. base_freq[j] += baseprobs[i];
  1580. }
  1581. }
  1582. /* Print out the frequency table, for verification */
  1583. for (i = 0; i < z_info->k_max; i++)
  1584. {
  1585. LOG_PRINT2("Cumulative frequency of item %d is: %d\n", i, base_freq[i]);
  1586. }
  1587. }
  1588. /*
  1589. * Adds a flag to an artifact. Returns true when canges were made.
  1590. */
  1591. static bool add_flag(artifact_type *a_ptr, int flag)
  1592. {
  1593. if (of_has(a_ptr->flags, flag))
  1594. return FALSE;
  1595. of_on(a_ptr->flags, flag);
  1596. LOG_PRINT1("Adding ability: %s\n", flag_names[flag]);
  1597. return TRUE;
  1598. }
  1599. /*
  1600. * Adds a flag and pval to an artifact. Always attempts
  1601. * to increase the pval.
  1602. */
  1603. static void add_pval_flag(artifact_type *a_ptr, int flag)
  1604. {
  1605. of_on(a_ptr->flags, flag);
  1606. do_pval(a_ptr);
  1607. LOG_PRINT2("Adding ability: %s (now %+d)\n", flag_names[flag], a_ptr->pval);
  1608. }
  1609. /*
  1610. * Adds a flag and a pval to an artifact, but won't increase
  1611. * the pval if the flag is present. Returns true when changes were made.
  1612. */
  1613. static bool add_fixed_pval_flag(artifact_type *a_ptr, int flag)
  1614. {
  1615. if (of_has(a_ptr->flags, flag))
  1616. return FALSE;
  1617. of_on(a_ptr->flags, flag);
  1618. do_pval(a_ptr);
  1619. LOG_PRINT2("Adding ability: %s (now %+d)\n", flag_names[flag], a_ptr->pval);
  1620. return TRUE;
  1621. }
  1622. /*
  1623. * Adds a flag and an initial pval to an artifact. Returns true
  1624. * when the flag was not present.
  1625. */
  1626. static bool add_first_pval_flag(artifact_type *a_ptr, int flag)
  1627. {
  1628. of_on(a_ptr->flags, flag);
  1629. if (a_ptr->pval == 0)
  1630. {
  1631. a_ptr->pval = (s16b)randint1(4);
  1632. LOG_PRINT2("Adding ability: %s (first time) (now %+d)\n",
  1633. flag_names[flag], a_ptr->pval);
  1634. return TRUE;
  1635. }
  1636. do_pval(a_ptr);
  1637. LOG_PRINT2("Adding ability: %s (now %+d)\n", flag_names[flag], a_ptr->pval);
  1638. return FALSE;
  1639. }
  1640. static void add_stat(artifact_type *a_ptr)
  1641. {
  1642. int r;
  1643. bool success = FALSE;
  1644. /* Hack: break out if all stats are raised to avoid an infinite loop */
  1645. if (flags_test_all(a_ptr->flags, OF_SIZE, OF_STR, OF_INT, OF_WIS,
  1646. OF_DEX, OF_CON, OF_CHR, FLAG_END))
  1647. return;
  1648. /* Make sure we add one that hasn't been added yet */
  1649. while (!success)
  1650. {
  1651. r = randint0(6);
  1652. if (r == 0) success = add_fixed_pval_flag(a_ptr, OF_STR);
  1653. else if (r == 1) success = add_fixed_pval_flag(a_ptr, OF_INT);
  1654. else if (r == 2) success = add_fixed_pval_flag(a_ptr, OF_WIS);
  1655. else if (r == 3) success = add_fixed_pval_flag(a_ptr, OF_DEX);
  1656. else if (r == 4) success = add_fixed_pval_flag(a_ptr, OF_CON);
  1657. else if (r == 5) success = add_fixed_pval_flag(a_ptr, OF_CHR);
  1658. }
  1659. }
  1660. static void add_sustain(artifact_type *a_ptr)
  1661. {
  1662. int r;
  1663. bool success = FALSE;
  1664. /* Hack: break out if all stats are sustained to avoid an infinite loop */
  1665. if (flags_test_all(a_ptr->flags, OF_SIZE, OF_SUST_STR, OF_SUST_INT,
  1666. OF_SUST_WIS, OF_SUST_DEX, OF_SUST_CON, OF_SUST_CHR, FLAG_END))
  1667. return;
  1668. while (!success)
  1669. {
  1670. r = randint0(6);
  1671. if (r == 0) success = add_flag(a_ptr, OF_SUST_STR);
  1672. else if (r == 1) success = add_flag(a_ptr, OF_SUST_INT);
  1673. else if (r == 2) success = add_flag(a_ptr, OF_SUST_WIS);
  1674. else if (r == 3) success = add_flag(a_ptr, OF_SUST_DEX);
  1675. else if (r == 4) success = add_flag(a_ptr, OF_SUST_CON);
  1676. else if (r == 5) success = add_flag(a_ptr, OF_SUST_CHR);
  1677. }
  1678. }
  1679. static void add_low_resist(artifact_type *a_ptr)
  1680. {
  1681. int r;
  1682. bool success = FALSE;
  1683. /* Hack - if all low resists added already, exit to avoid infinite loop */
  1684. if(flags_test_all(a_ptr->flags, OF_SIZE, OF_RES_ACID, OF_RES_ELEC,
  1685. OF_RES_FIRE, OF_RES_COLD, FLAG_END))
  1686. return;
  1687. while (!success)
  1688. {
  1689. r = randint0(4);
  1690. if (r == 0) success = add_flag(a_ptr, OF_RES_ACID);
  1691. else if (r == 1) success = add_flag(a_ptr, OF_RES_ELEC);
  1692. else if (r == 2) success = add_flag(a_ptr, OF_RES_FIRE);
  1693. else if (r == 3) success = add_flag(a_ptr, OF_RES_COLD);
  1694. }
  1695. }
  1696. static void add_high_resist(artifact_type *a_ptr)
  1697. {
  1698. /* Add a high resist, according to the generated frequency distribution. */
  1699. int r, i, temp;
  1700. int count = 0;
  1701. bool success = FALSE;
  1702. temp = 0;
  1703. for (i = 0; i < ART_IDX_HIGH_RESIST_COUNT; i++)
  1704. {
  1705. temp += artprobs[art_idx_high_resist[i]];
  1706. }
  1707. /* The following will fail (cleanly) if all high resists already added */
  1708. while ( (!success) && (count < MAX_TRIES) )
  1709. {
  1710. /* Randomize from 1 to this total amount */
  1711. r = randint1(temp);
  1712. /* Determine which (weighted) resist this number corresponds to */
  1713. temp = artprobs[art_idx_high_resist[0]];
  1714. i = 0;
  1715. while (r > temp && i < ART_IDX_HIGH_RESIST_COUNT)
  1716. {
  1717. temp += artprobs[art_idx_high_resist[i]];
  1718. i++;
  1719. }
  1720. /* Now i should give us the index of the correct high resist */
  1721. if (i == 0) success = add_flag(a_ptr, OF_RES_POIS);
  1722. else if (i == 1) success = add_flag(a_ptr, OF_RES_FEAR);
  1723. else if (i == 2) success = add_flag(a_ptr, OF_RES_LIGHT);
  1724. else if (i == 3) success = add_flag(a_ptr, OF_RES_DARK);
  1725. else if (i == 4) success = add_flag(a_ptr, OF_RES_BLIND);
  1726. else if (i == 5) success = add_flag(a_ptr, OF_RES_CONFU);
  1727. else if (i == 6) success = add_flag(a_ptr, OF_RES_SOUND);
  1728. else if (i == 7) success = add_flag(a_ptr, OF_RES_SHARD);
  1729. else if (i == 8) success = add_flag(a_ptr, OF_RES_NEXUS);
  1730. else if (i == 9) success = add_flag(a_ptr, OF_RES_NETHR);
  1731. else if (i == 10) success = add_flag(a_ptr, OF_RES_CHAOS);
  1732. else if (i == 11) success = add_flag(a_ptr, OF_RES_DISEN);
  1733. count++;
  1734. }
  1735. }
  1736. static void add_slay(artifact_type *a_ptr, bool brand)
  1737. {
  1738. /* The last slay entry is a NULL slay_t */
  1739. int size = num_slays() - 1;
  1740. int count = 0;
  1741. const slay_t *s_ptr;
  1742. for(count = 0; count < MAX_TRIES; count++)
  1743. {
  1744. s_ptr = &slay_table[randint0(size)];
  1745. if (brand && s_ptr->brand && !of_has(a_ptr->flags, s_ptr->slay_flag))
  1746. {
  1747. of_on(a_ptr->flags, s_ptr->slay_flag);
  1748. LOG_PRINT1("Adding brand: %s\n", s_ptr->brand);
  1749. return;
  1750. }
  1751. if (!brand && !s_ptr->brand && !of_has(a_ptr->flags, s_ptr->slay_flag))
  1752. {
  1753. of_on(a_ptr->flags, s_ptr->slay_flag);
  1754. LOG_PRINT1("Adding slay: %s\n", s_ptr->desc);
  1755. return;
  1756. }
  1757. }
  1758. }
  1759. static void add_damage_dice(artifact_type *a_ptr)
  1760. {
  1761. /* CR 2001-09-02: changed this to increments 1 or 2 only */
  1762. a_ptr->dd += (byte)randint1(2);
  1763. /* if (a_ptr->dd > 9)
  1764. a_ptr->dd = 9; */
  1765. LOG_PRINT1("Adding ability: extra damage dice (now %d dice)\n", a_ptr->dd);
  1766. }
  1767. static void add_to_hit(artifact_type *a_ptr, int fixed, int random)
  1768. {
  1769. /* Inhibit above certain threshholds */
  1770. if (a_ptr->to_h > VERYHIGH_TO_HIT)
  1771. {
  1772. if (!INHIBIT_STRONG)
  1773. {
  1774. LOG_PRINT1("Failed to add to-hit, value of %d is too high\n", a_ptr->to_h);
  1775. return;
  1776. }
  1777. }
  1778. else if (a_ptr->to_h > HIGH_TO_HIT)
  1779. {
  1780. if (!INHIBIT_WEAK)
  1781. {
  1782. LOG_PRINT1("Failed to add to-hit, value of %d is too high\n", a_ptr->to_h);
  1783. return;
  1784. }
  1785. }
  1786. a_ptr->to_h += (s16b)(fixed + randint0(random));
  1787. if (a_ptr->to_h > 0) of_on(a_ptr->flags, OF_SHOW_MODS);
  1788. LOG_PRINT1("Adding ability: extra to_h (now %+d)\n", a_ptr->to_h);
  1789. }
  1790. static void add_to_dam(artifact_type *a_ptr, int fixed, int random)
  1791. {
  1792. /* Inhibit above certain threshholds */
  1793. if (a_ptr->to_d > VERYHIGH_TO_DAM)
  1794. {
  1795. if (!INHIBIT_STRONG)
  1796. {
  1797. LOG_PRINT1("Failed to add to-dam, value of %d is too high\n", a_ptr->to_d);
  1798. return;
  1799. }
  1800. }
  1801. else if (a_ptr->to_h > HIGH_TO_DAM)
  1802. {
  1803. if (!INHIBIT_WEAK)
  1804. {
  1805. LOG_PRINT1("Failed to add to-dam, value of %d is too high\n", a_ptr->to_d);
  1806. return;
  1807. }
  1808. }
  1809. a_ptr->to_d += (s16b)(fixed + randint0(random));
  1810. if (a_ptr->to_d > 0) of_on(a_ptr->flags, OF_SHOW_MODS);
  1811. LOG_PRINT1("Adding ability: extra to_dam (now %+d)\n", a_ptr->to_d);
  1812. }
  1813. static void add_to_AC(artifact_type *a_ptr, int fixed, int random)
  1814. {
  1815. /* Inhibit above certain threshholds */
  1816. if (a_ptr->to_a > VERYHIGH_TO_AC)
  1817. {
  1818. if (!INHIBIT_STRONG)
  1819. {
  1820. LOG_PRINT1("Failed to add to-AC, value of %d is too high\n", a_ptr->to_a);
  1821. return;
  1822. }
  1823. }
  1824. else if (a_ptr->to_h > HIGH_TO_AC)
  1825. {
  1826. if (!INHIBIT_WEAK)
  1827. {
  1828. LOG_PRINT1("Failed to add to-AC, value of %d is too high\n", a_ptr->to_a);
  1829. return;
  1830. }
  1831. }
  1832. a_ptr->to_a += (s16b)(fixed + randint0(random));
  1833. LOG_PRINT1("Adding ability: AC bonus (new bonus is %+d)\n", a_ptr->to_a);
  1834. }
  1835. static void add_weight_mod(artifact_type *a_ptr)
  1836. {
  1837. a_ptr->weight = (a_ptr->weight * 9) / 10;
  1838. LOG_PRINT1("Adding ability: lower weight (new weight is %d)\n", a_ptr->weight);
  1839. }
  1840. /*
  1841. * Add a random immunity to this artifact
  1842. * ASSUMPTION: All immunities are equally likely.
  1843. * ToDo: replace with lookup once immunities are abstracted
  1844. */
  1845. static void add_immunity(artifact_type *a_ptr)
  1846. {
  1847. int imm_type = randint0(4);
  1848. switch(imm_type)
  1849. {
  1850. case 0:
  1851. {
  1852. of_on(a_ptr->flags, OF_IM_ACID);
  1853. LOG_PRINT("Adding ability: immunity to acid\n");
  1854. break;
  1855. }
  1856. case 1:
  1857. {
  1858. of_on(a_ptr->flags, OF_IM_ELEC);
  1859. LOG_PRINT("Adding ability: immunity to lightning\n");
  1860. break;
  1861. }
  1862. case 2:
  1863. {
  1864. of_on(a_ptr->flags, OF_IM_FIRE);
  1865. LOG_PRINT("Adding ability: immunity to fire\n");
  1866. break;
  1867. }
  1868. case 3:
  1869. {
  1870. of_on(a_ptr->flags, OF_IM_COLD);
  1871. LOG_PRINT("Adding ability: immunity to cold\n");
  1872. break;
  1873. }
  1874. }
  1875. }
  1876. /* Add an activation (called only if artifact does not yet have one) */
  1877. static void add_activation(artifact_type *a_ptr, s32b target_power)
  1878. {
  1879. int i, x, p, max_effect = 0;
  1880. int count = 0;
  1881. /* Work out the maximum allowed effect power */
  1882. for (i = 0; i < EF_MAX; i++)
  1883. {
  1884. if (effect_power(i) > max_effect && effect_power(i) <
  1885. INHIBIT_POWER)
  1886. max_effect = effect_power(i);
  1887. }
  1888. /* Select an effect at random */
  1889. while (count < MAX_TRIES)
  1890. {
  1891. x = randint0(EF_MAX);
  1892. p = effect_power(x);
  1893. /*
  1894. * Check that activation is useful but not exploitable,
  1895. * and roughly proportionate to the overall power
  1896. */
  1897. if (p < INHIBIT_POWER && 100 * p / max_effect > 50 *
  1898. target_power / max_power && 100 * p / max_effect < 200
  1899. * target_power / max_power)
  1900. {
  1901. LOG_PRINT1("Adding activation effect %d\n", x);
  1902. a_ptr->effect = x;
  1903. a_ptr->time.base = (p * 8);
  1904. a_ptr->time.dice = (p > 5 ? p / 5 : 1);
  1905. a_ptr->time.sides = p;
  1906. return;
  1907. }
  1908. count++;
  1909. }
  1910. }
  1911. /*
  1912. * Build a suitable frequency table for this item, based on the generated
  1913. * frequencies. The frequencies for any abilities that don't apply for
  1914. * this item type will be set to zero. First parameter is the artifact
  1915. * for which to generate the frequency table.
  1916. *
  1917. * The second input parameter is a pointer to an array that the function
  1918. * will use to store the frequency table. The array must have size
  1919. * ART_IDX_TOTAL.
  1920. *
  1921. * The resulting frequency table is cumulative for ease of use in the
  1922. * weighted randomization algorithm.
  1923. */
  1924. static void build_freq_table(artifact_type *a_ptr, s16b *freq)
  1925. {
  1926. int i,j;
  1927. s16b f_temp[ART_IDX_TOTAL];
  1928. /* First, set everything to zero */
  1929. for (i = 0; i < ART_IDX_TOTAL; i++)
  1930. {
  1931. f_temp[i] = 0;
  1932. freq[i] = 0;
  1933. }
  1934. /* Now copy over appropriate frequencies for applicable abilities */
  1935. /* Bow abilities */
  1936. if (a_ptr->tval == TV_BOW)
  1937. {
  1938. for (j = 0; j < ART_IDX_BOW_COUNT; j++)
  1939. {
  1940. f_temp[art_idx_bow[j]] = artprobs[art_idx_bow[j]];
  1941. }
  1942. }
  1943. /* General weapon abilities */
  1944. if (a_ptr->tval == TV_BOW || a_ptr->tval == TV_DIGGING ||
  1945. a_ptr->tval == TV_HAFTED || a_ptr->tval == TV_POLEARM ||
  1946. a_ptr->tval == TV_SWORD)
  1947. {
  1948. for (j = 0; j < ART_IDX_WEAPON_COUNT; j++)
  1949. {
  1950. f_temp[art_idx_weapon[j]] = artprobs[art_idx_weapon[j]];
  1951. }
  1952. }
  1953. /* General non-weapon abilities */
  1954. else
  1955. {
  1956. for (j = 0; j < ART_IDX_NONWEAPON_COUNT; j++)
  1957. {
  1958. f_temp[art_idx_nonweapon[j]] = artprobs[art_idx_nonweapon[j]];
  1959. }
  1960. }
  1961. /* General melee abilities */
  1962. if (a_ptr->tval == TV_DIGGING || a_ptr->tval == TV_HAFTED ||
  1963. a_ptr->tval == TV_POLEARM || a_ptr->tval == TV_SWORD)
  1964. {
  1965. for (j = 0; j < ART_IDX_MELEE_COUNT; j++)
  1966. {
  1967. f_temp[art_idx_melee[j]] = artprobs[art_idx_melee[j]];
  1968. }
  1969. }
  1970. /* General armor abilities */
  1971. if ( a_ptr->tval == TV_BOOTS || a_ptr->tval == TV_GLOVES ||
  1972. a_ptr->tval == TV_HELM || a_ptr->tval == TV_CROWN ||
  1973. a_ptr->tval == TV_SHIELD || a_ptr->tval == TV_CLOAK ||
  1974. a_ptr->tval == TV_SOFT_ARMOR || a_ptr->tval == TV_HARD_ARMOR ||
  1975. a_ptr->tval == TV_DRAG_ARMOR)
  1976. {
  1977. for (j = 0; j < ART_IDX_ALLARMOR_COUNT; j++)
  1978. {
  1979. f_temp[art_idx_allarmor[j]] = artprobs[art_idx_allarmor[j]];
  1980. }
  1981. }
  1982. /* Boot abilities */
  1983. if (a_ptr->tval == TV_BOOTS)
  1984. {
  1985. for (j = 0; j < ART_IDX_BOOT_COUNT; j++)
  1986. {
  1987. f_temp[art_idx_boot[j]] = artprobs[art_idx_boot[j]];
  1988. }
  1989. }
  1990. /* Glove abilities */
  1991. if (a_ptr->tval == TV_GLOVES)
  1992. {
  1993. for (j = 0; j < ART_IDX_GLOVE_COUNT; j++)
  1994. {
  1995. f_temp[art_idx_glove[j]] = artprobs[art_idx_glove[j]];
  1996. }
  1997. }
  1998. /* Headgear abilities */
  1999. if (a_ptr->tval == TV_HELM || a_ptr->tval == TV_CROWN)
  2000. {
  2001. for (j = 0; j < ART_IDX_HELM_COUNT; j++)
  2002. {
  2003. f_temp[art_idx_headgear[j]] = artprobs[art_idx_headgear[j]];
  2004. }
  2005. }
  2006. /* Shield abilities */
  2007. if (a_ptr->tval == TV_SHIELD)
  2008. {
  2009. for (j = 0; j < ART_IDX_SHIELD_COUNT; j++)
  2010. {
  2011. f_temp[art_idx_shield[j]] = artprobs[art_idx_shield[j]];
  2012. }
  2013. }
  2014. /* Cloak abilities */
  2015. if (a_ptr->tval == TV_CLOAK)
  2016. {
  2017. for (j = 0; j < ART_IDX_CLOAK_COUNT; j++)
  2018. {
  2019. f_temp[art_idx_cloak[j]] = artprobs[art_idx_cloak[j]];
  2020. }
  2021. }
  2022. /* Armor abilities */
  2023. if (a_ptr->tval == TV_SOFT_ARMOR || a_ptr->tval == TV_HARD_ARMOR ||
  2024. a_ptr->tval == TV_DRAG_ARMOR)
  2025. {
  2026. for (j = 0; j < ART_IDX_ARMOR_COUNT; j++)
  2027. {
  2028. f_temp[art_idx_armor[j]] = artprobs[art_idx_armor[j]];
  2029. }
  2030. }
  2031. /* General abilities - no constraint */
  2032. for (j = 0; j < ART_IDX_GEN_COUNT; j++)
  2033. {
  2034. f_temp[art_idx_gen[j]] = artprobs[art_idx_gen[j]];
  2035. }
  2036. /*
  2037. * Now we have the correct individual frequencies, we build a cumulative
  2038. * frequency table for them.
  2039. */
  2040. for (i = 0; i < ART_IDX_TOTAL; i++)
  2041. {
  2042. for (j = i; j < ART_IDX_TOTAL; j++)
  2043. {
  2044. freq[j] += f_temp[i];
  2045. }
  2046. }
  2047. /* Done - the freq array holds the desired frequencies. */
  2048. /* Print out the frequency table, for verification */
  2049. for (i = 0; i < ART_IDX_TOTAL; i++)
  2050. LOG_PRINT2("Cumulative frequency of ability %d is: %d\n", i, freq[i]);
  2051. }
  2052. /*
  2053. * Choose a random ability using weights based on the given cumulative
  2054. * frequency table. A pointer to the frequency array (which must be of size
  2055. * ART_IDX_TOTAL) is passed as a parameter. The function returns a number
  2056. * representing the index of the ability chosen.
  2057. */
  2058. static int choose_ability (s16b *freq_table)
  2059. {
  2060. int r, ability;
  2061. /* Generate a random number between 1 and the last value in the table */
  2062. r = randint1(freq_table[ART_IDX_TOTAL-1]);
  2063. /* Find the entry in the table that this number represents. */
  2064. ability = 0;
  2065. while (r > freq_table[ability])
  2066. ability++;
  2067. LOG_PRINT1("Ability chosen was number: %d\n", ability);
  2068. /*
  2069. * The ability variable is now the index of the first value in the table
  2070. * greater than or equal to r, which is what we want.
  2071. */
  2072. return ability;
  2073. }
  2074. /*
  2075. * Add an ability given by the index r. This is mostly just a long case
  2076. * statement.
  2077. *
  2078. * Note that this method is totally general and imposes no restrictions on
  2079. * appropriate item type for a given ability. This is assumed to have
  2080. * been done already.
  2081. */
  2082. static void add_ability_aux(artifact_type *a_ptr, int r, s32b target_power)
  2083. {
  2084. switch(r)
  2085. {
  2086. case ART_IDX_BOW_SHOTS:
  2087. case ART_IDX_NONWEAPON_SHOTS:
  2088. add_pval_flag(a_ptr, OF_SHOTS);
  2089. break;
  2090. case ART_IDX_BOW_MIGHT:
  2091. add_pval_flag(a_ptr, OF_MIGHT);
  2092. break;
  2093. case ART_IDX_WEAPON_HIT:
  2094. case ART_IDX_NONWEAPON_HIT:
  2095. add_to_hit(a_ptr, 1, 2 * mean_hit_increment);
  2096. break;
  2097. case ART_IDX_WEAPON_DAM:
  2098. case ART_IDX_NONWEAPON_DAM:
  2099. add_to_dam(a_ptr, 1, 2 * mean_dam_increment);
  2100. break;
  2101. case ART_IDX_NONWEAPON_HIT_DAM:
  2102. add_to_hit(a_ptr, 1, 2 * mean_hit_increment);
  2103. add_to_dam(a_ptr, 1, 2 * mean_dam_increment);
  2104. break;
  2105. case ART_IDX_WEAPON_AGGR:
  2106. case ART_IDX_NONWEAPON_AGGR:
  2107. if (target_power > AGGR_POWER)
  2108. {
  2109. add_flag(a_ptr, OF_AGGRAVATE);
  2110. }
  2111. break;
  2112. case ART_IDX_MELEE_BLESS:
  2113. add_flag(a_ptr, OF_BLESSED);
  2114. break;
  2115. case ART_IDX_BOW_BRAND:
  2116. case ART_IDX_MELEE_BRAND:
  2117. case ART_IDX_NONWEAPON_BRAND:
  2118. add_slay(a_ptr, TRUE);
  2119. break;
  2120. case ART_IDX_BOW_SLAY:
  2121. case ART_IDX_MELEE_SLAY:
  2122. case ART_IDX_NONWEAPON_SLAY:
  2123. add_slay(a_ptr, FALSE);
  2124. break;
  2125. case ART_IDX_MELEE_SINV:
  2126. case ART_IDX_HELM_SINV:
  2127. case ART_IDX_GEN_SINV:
  2128. add_flag(a_ptr, OF_SEE_INVIS);
  2129. break;
  2130. case ART_IDX_MELEE_BLOWS:
  2131. case ART_IDX_NONWEAPON_BLOWS:
  2132. add_pval_flag(a_ptr, OF_BLOWS);
  2133. break;
  2134. case ART_IDX_MELEE_AC:
  2135. case ART_IDX_BOOT_AC:
  2136. case ART_IDX_GLOVE_AC:
  2137. case ART_IDX_HELM_AC:
  2138. case ART_IDX_SHIELD_AC:
  2139. case ART_IDX_CLOAK_AC:
  2140. case ART_IDX_ARMOR_AC:
  2141. case ART_IDX_GEN_AC:
  2142. add_to_AC(a_ptr, 1, 2 * mean_ac_increment);
  2143. break;
  2144. case ART_IDX_MELEE_DICE:
  2145. add_damage_dice(a_ptr);
  2146. break;
  2147. case ART_IDX_MELEE_WEIGHT:
  2148. case ART_IDX_ALLARMOR_WEIGHT:
  2149. add_weight_mod(a_ptr);
  2150. break;
  2151. case ART_IDX_MELEE_TUNN:
  2152. case ART_IDX_GEN_TUNN:
  2153. add_pval_flag(a_ptr, OF_TUNNEL);
  2154. break;
  2155. case ART_IDX_BOOT_FEATHER:
  2156. case ART_IDX_GEN_FEATHER:
  2157. add_flag(a_ptr, OF_FEATHER);
  2158. break;
  2159. case ART_IDX_BOOT_STEALTH:
  2160. case ART_IDX_CLOAK_STEALTH:
  2161. case ART_IDX_ARMOR_STEALTH:
  2162. case ART_IDX_GEN_STEALTH:
  2163. add_pval_flag(a_ptr, OF_STEALTH);
  2164. break;
  2165. case ART_IDX_BOOT_SPEED:
  2166. case ART_IDX_GEN_SPEED:
  2167. add_first_pval_flag(a_ptr, OF_SPEED);
  2168. break;
  2169. case ART_IDX_GLOVE_FA:
  2170. case ART_IDX_GEN_FA:
  2171. add_flag(a_ptr, OF_FREE_ACT);
  2172. break;
  2173. case ART_IDX_GLOVE_DEX:
  2174. add_fixed_pval_flag(a_ptr, OF_DEX);
  2175. break;
  2176. case ART_IDX_HELM_RBLIND:
  2177. case ART_IDX_GEN_RBLIND:
  2178. add_flag(a_ptr, OF_RES_BLIND);
  2179. break;
  2180. case ART_IDX_HELM_ESP:
  2181. case ART_IDX_GEN_ESP:
  2182. add_flag(a_ptr, OF_TELEPATHY);
  2183. break;
  2184. case ART_IDX_HELM_WIS:
  2185. add_fixed_pval_flag(a_ptr, OF_WIS);
  2186. break;
  2187. case ART_IDX_HELM_INT:
  2188. add_fixed_pval_flag(a_ptr, OF_INT);
  2189. break;
  2190. case ART_IDX_SHIELD_LRES:
  2191. case ART_IDX_ARMOR_LRES:
  2192. case ART_IDX_GEN_LRES:
  2193. add_low_resist(a_ptr);
  2194. break;
  2195. case ART_IDX_ARMOR_HLIFE:
  2196. case ART_IDX_GEN_HLIFE:
  2197. add_flag(a_ptr, OF_HOLD_LIFE);
  2198. break;
  2199. case ART_IDX_ARMOR_CON:
  2200. add_fixed_pval_flag(a_ptr, OF_CON);
  2201. break;
  2202. case ART_IDX_ARMOR_ALLRES:
  2203. add_flag(a_ptr, OF_RES_ACID);
  2204. add_flag(a_ptr, OF_RES_ELEC);
  2205. add_flag(a_ptr, OF_RES_FIRE);
  2206. add_flag(a_ptr, OF_RES_COLD);
  2207. break;
  2208. case ART_IDX_ARMOR_HRES:
  2209. add_high_resist(a_ptr);
  2210. break;
  2211. case ART_IDX_GEN_STAT:
  2212. add_stat(a_ptr);
  2213. break;
  2214. case ART_IDX_GEN_SUST:
  2215. add_sustain(a_ptr);
  2216. break;
  2217. case ART_IDX_GEN_SEARCH:
  2218. add_pval_flag(a_ptr, OF_SEARCH);
  2219. break;
  2220. case ART_IDX_GEN_INFRA:
  2221. add_pval_flag(a_ptr, OF_INFRA);
  2222. break;
  2223. case ART_IDX_GEN_IMMUNE:
  2224. add_immunity(a_ptr);
  2225. break;
  2226. case ART_IDX_GEN_LIGHT:
  2227. add_flag(a_ptr, OF_LIGHT);
  2228. break;
  2229. case ART_IDX_GEN_SDIG:
  2230. add_flag(a_ptr, OF_SLOW_DIGEST);
  2231. break;
  2232. case ART_IDX_GEN_REGEN:
  2233. add_flag(a_ptr, OF_REGEN);
  2234. break;
  2235. case ART_IDX_GEN_RPOIS:
  2236. add_flag(a_ptr, OF_RES_POIS);
  2237. break;
  2238. case ART_IDX_GEN_RFEAR:
  2239. add_flag(a_ptr, OF_RES_FEAR);
  2240. break;
  2241. case ART_IDX_GEN_RLIGHT:
  2242. add_flag(a_ptr, OF_RES_LIGHT);
  2243. break;
  2244. case ART_IDX_GEN_RDARK:
  2245. add_flag(a_ptr, OF_RES_DARK);
  2246. break;
  2247. case ART_IDX_GEN_RCONF:
  2248. add_flag(a_ptr, OF_RES_CONFU);
  2249. break;
  2250. case ART_IDX_GEN_RSOUND:
  2251. add_flag(a_ptr, OF_RES_SOUND);
  2252. break;
  2253. case ART_IDX_GEN_RSHARD:
  2254. add_flag(a_ptr, OF_RES_SHARD);
  2255. break;
  2256. case ART_IDX_GEN_RNEXUS:
  2257. add_flag(a_ptr, OF_RES_NEXUS);
  2258. break;
  2259. case ART_IDX_GEN_RNETHER:
  2260. add_flag(a_ptr, OF_RES_NETHR);
  2261. break;
  2262. case ART_IDX_GEN_RCHAOS:
  2263. add_flag(a_ptr, OF_RES_CHAOS);
  2264. break;
  2265. case ART_IDX_GEN_RDISEN:
  2266. add_flag(a_ptr, OF_RES_DISEN);
  2267. break;
  2268. case ART_IDX_GEN_ACTIV:
  2269. if (!a_ptr->effect) add_activation(a_ptr, target_power);
  2270. break;
  2271. }
  2272. }
  2273. /*
  2274. * Randomly select an extra ability to be added to the artifact in question.
  2275. */
  2276. static void add_ability(artifact_type *a_ptr, s32b target_power)
  2277. {
  2278. int r;
  2279. /* Choose a random ability using the frequency table previously defined*/
  2280. r = choose_ability(art_freq);
  2281. /* Add the appropriate ability */
  2282. add_ability_aux(a_ptr, r, target_power);
  2283. /* Now remove contradictory or redundant powers. */
  2284. remove_contradictory(a_ptr);
  2285. /* Adding WIS to sharp weapons always blesses them */
  2286. if (of_has(a_ptr->flags, OF_WIS) && (a_ptr->tval == TV_SWORD || a_ptr->tval == TV_POLEARM))
  2287. {
  2288. add_flag(a_ptr, OF_BLESSED);
  2289. }
  2290. }
  2291. /*
  2292. * Try to supercharge this item by running through the list of the supercharge
  2293. * abilities and attempting to add each in turn. An artifact only gets one
  2294. * chance at each of these up front (if applicable).
  2295. */
  2296. static void try_supercharge(artifact_type *a_ptr, s32b target_power)
  2297. {
  2298. /* Huge damage dice or +3 blows - melee weapon only */
  2299. if (a_ptr->tval == TV_DIGGING || a_ptr->tval == TV_HAFTED ||
  2300. a_ptr->tval == TV_POLEARM || a_ptr->tval == TV_SWORD)
  2301. {
  2302. if (randint0(z_info->a_max) < artprobs[ART_IDX_MELEE_DICE_SUPER])
  2303. {
  2304. a_ptr->dd += 3 + randint0(4);
  2305. /* if (a_ptr->dd > 9) a_ptr->dd = 9; */
  2306. LOG_PRINT1("Supercharging damage dice! (Now %d dice)\n", a_ptr->dd);
  2307. }
  2308. else if (randint0(z_info->a_max) < artprobs[ART_IDX_MELEE_BLOWS_SUPER])
  2309. {
  2310. of_on(a_ptr->flags, OF_BLOWS);
  2311. a_ptr->pval = 3;
  2312. LOG_PRINT("Supercharging melee blows! (+3 blows)\n");
  2313. }
  2314. }
  2315. /* Bows - +3 might or +3 shots */
  2316. if (a_ptr->tval == TV_BOW)
  2317. {
  2318. if (randint0(z_info->a_max) < artprobs[ART_IDX_BOW_SHOTS_SUPER])
  2319. {
  2320. of_on(a_ptr->flags, OF_SHOTS);
  2321. a_ptr->pval = 3;
  2322. LOG_PRINT("Supercharging shots for bow! (3 extra shots)\n");
  2323. }
  2324. else if (randint0(z_info->a_max) < artprobs[ART_IDX_BOW_MIGHT_SUPER])
  2325. {
  2326. of_on(a_ptr->flags, OF_MIGHT);
  2327. a_ptr->pval = 3;
  2328. LOG_PRINT("Supercharging might for bow! (3 extra might)\n");
  2329. }
  2330. }
  2331. /* Big speed bonus - any item (potentially) but more likely on boots */
  2332. if (randint0(z_info->a_max) < artprobs[ART_IDX_GEN_SPEED_SUPER] ||
  2333. (a_ptr->tval == TV_BOOTS && randint0(z_info->a_max) <
  2334. artprobs[ART_IDX_BOOT_SPEED]))
  2335. {
  2336. of_on(a_ptr->flags, OF_SPEED);
  2337. a_ptr->pval = 5 + randint0(6);
  2338. if (INHIBIT_WEAK) a_ptr->pval += randint1(3);
  2339. if (INHIBIT_STRONG) a_ptr->pval += 1 + randint1(6);
  2340. LOG_PRINT1("Supercharging speed for this item! (New speed bonus is %d)\n", a_ptr->pval);
  2341. }
  2342. /* Big AC bonus */
  2343. if (randint0(z_info->a_max) < artprobs[ART_IDX_GEN_AC_SUPER])
  2344. {
  2345. a_ptr->to_a += 19 + randint1(11);
  2346. if (INHIBIT_WEAK) a_ptr->to_a += randint1(10);
  2347. if (INHIBIT_STRONG) a_ptr->to_a += randint1(20);
  2348. LOG_PRINT1("Supercharging AC! New AC bonus is %d\n", a_ptr->to_a);
  2349. }
  2350. /* Aggravation */
  2351. if (a_ptr->tval == TV_BOW || a_ptr->tval == TV_DIGGING ||
  2352. a_ptr->tval == TV_HAFTED || a_ptr->tval == TV_POLEARM ||
  2353. a_ptr->tval == TV_SWORD)
  2354. {
  2355. if ((randint0(z_info->a_max) < artprobs[ART_IDX_WEAPON_AGGR]) &&
  2356. (target_power > AGGR_POWER))
  2357. {
  2358. of_on(a_ptr->flags, OF_AGGRAVATE);
  2359. LOG_PRINT("Adding aggravation\n");
  2360. }
  2361. }
  2362. else
  2363. {
  2364. if ((randint0(z_info->a_max) < artprobs[ART_IDX_NONWEAPON_AGGR]) &&
  2365. (target_power > AGGR_POWER))
  2366. {
  2367. of_on(a_ptr->flags, OF_AGGRAVATE);
  2368. LOG_PRINT("Adding aggravation\n");
  2369. }
  2370. }
  2371. }
  2372. /*
  2373. * Make it bad, or if it's already bad, make it worse!
  2374. */
  2375. static void do_curse(artifact_type *a_ptr)
  2376. {
  2377. if (one_in_(7))
  2378. of_on(a_ptr->flags, OF_AGGRAVATE);
  2379. if (one_in_(4))
  2380. of_on(a_ptr->flags, OF_DRAIN_EXP);
  2381. if (one_in_(7))
  2382. of_on(a_ptr->flags, OF_TELEPORT);
  2383. if ((a_ptr->pval > 0) && one_in_(2))
  2384. a_ptr->pval = -a_ptr->pval;
  2385. if ((a_ptr->to_a > 0) && one_in_(2))
  2386. a_ptr->to_a = -a_ptr->to_a;
  2387. if ((a_ptr->to_h > 0) && one_in_(2))
  2388. a_ptr->to_h = -a_ptr->to_h;
  2389. if ((a_ptr->to_d > 0) && one_in_(4))
  2390. a_ptr->to_d = -a_ptr->to_d;
  2391. if (of_has(a_ptr->flags, OF_LIGHT_CURSE))
  2392. {
  2393. if (one_in_(2)) of_on(a_ptr->flags, OF_HEAVY_CURSE);
  2394. return;
  2395. }
  2396. of_on(a_ptr->flags, OF_LIGHT_CURSE);
  2397. if (one_in_(4))
  2398. of_on(a_ptr->flags, OF_HEAVY_CURSE);
  2399. }
  2400. /*
  2401. * Note the three special cases (One Ring, Grond, Morgoth).
  2402. */
  2403. static void scramble_artifact(int a_idx)
  2404. {
  2405. artifact_type *a_ptr = &a_info[a_idx];
  2406. artifact_type a_old;
  2407. object_kind *k_ptr;
  2408. s32b power;
  2409. int tries = 0;
  2410. s16b k_idx;
  2411. byte alloc_old, base_alloc_old, alloc_new;
  2412. s32b ap = 0;
  2413. bool curse_me = FALSE;
  2414. bool success = FALSE;
  2415. /* Special cases -- don't randomize these! */
  2416. if ((a_idx == ART_POWER) ||
  2417. (a_idx == ART_GROND) ||
  2418. (a_idx == ART_MORGOTH))
  2419. return;
  2420. /* Skip unused artifacts, too! */
  2421. if (a_ptr->tval == 0) return;
  2422. /* Evaluate the original artifact to determine the power level. */
  2423. power = base_power[a_idx];
  2424. /* If it has a restricted ability then don't randomize it. */
  2425. if (power > INHIBIT_POWER)
  2426. {
  2427. LOG_PRINT1("Skipping artifact number %d - too powerful to randomize!", a_idx);
  2428. return;
  2429. }
  2430. if (power < 0) curse_me = TRUE;
  2431. LOG_PRINT("+++++++++++++++++ CREATING NEW ARTIFACT ++++++++++++++++++\n");
  2432. LOG_PRINT2("Artifact %d: power = %d\n", a_idx, power);
  2433. /*
  2434. * Flip the sign on power if it's negative, since it's only used for base
  2435. * item choice
  2436. */
  2437. if (power < 0) power = -power;
  2438. if (a_idx >= ART_MIN_NORMAL)
  2439. {
  2440. /*
  2441. * Normal artifact - choose a random base item type. Not too
  2442. * powerful, so we'll have to add something to it. Not too
  2443. * weak, for the opposite reason.
  2444. *
  2445. * CR 7/15/2001 - lowered the upper limit so that we get at
  2446. * least a few powers (from 8/10 to 6/10) but permit anything
  2447. * more than 20 below the target power
  2448. */
  2449. int count = 0;
  2450. s32b ap2;
  2451. /* Capture the rarity of the original base item and artifact */
  2452. alloc_old = base_art_alloc[a_idx];
  2453. base_alloc_old = base_item_prob[a_idx];
  2454. do
  2455. {
  2456. /* Get the new item kind */
  2457. k_idx = choose_item(a_idx);
  2458. k_ptr = &k_info[k_idx];
  2459. /*
  2460. * Hack: if power is positive but very low, and if we're not having
  2461. * any luck finding a base item, curse it once. This helps ensure
  2462. * that we get a base item for borderline cases like Wormtongue.
  2463. */
  2464. if (power > 0 && power < 10 && count > MAX_TRIES / 2)
  2465. {
  2466. LOG_PRINT("Cursing base item to help get a match.\n");
  2467. do_curse(a_ptr);
  2468. }
  2469. ap2 = artifact_power(a_idx);
  2470. count++;
  2471. /*
  2472. * Calculate the proper rarity based on the new type. We attempt
  2473. * to preserve the 'effective rarity' which is equal to the
  2474. * artifact rarity multiplied by the base item rarity.
  2475. */
  2476. alloc_new = alloc_old * base_alloc_old
  2477. / k_ptr->alloc_prob;
  2478. if (alloc_new > 99) alloc_new = 99;
  2479. if (alloc_new < 1) alloc_new = 1;
  2480. LOG_PRINT2("Old allocs are base %d, art %d\n", base_alloc_old, alloc_old);
  2481. LOG_PRINT2("New allocs are base %d, art %d\n", k_ptr->alloc_prob, alloc_new);
  2482. } while ( (count < MAX_TRIES) &&
  2483. (((ap2 > (power * 6) / 10 + 1) && (power-ap2 < 20)) ||
  2484. (ap2 < (power / 10))) );
  2485. /* Got an item - set the new rarity */
  2486. a_ptr->alloc_prob = alloc_new;
  2487. if (count >= MAX_TRIES)
  2488. {
  2489. msg_print("Warning! Couldn't get appropriate power level on base item.");
  2490. LOG_PRINT("Warning! Couldn't get appropriate power level on base item.\n");
  2491. }
  2492. }
  2493. else
  2494. {
  2495. /* Special artifact (light source, ring, or amulet) */
  2496. /* Keep the item kind */
  2497. k_idx = lookup_kind(a_ptr->tval, a_ptr->sval);
  2498. k_ptr = &k_info[k_idx];
  2499. /* Clear the following fields; leave the rest alone */
  2500. a_ptr->pval = 0;
  2501. a_ptr->to_h = a_ptr->to_d = a_ptr->to_a = 0;
  2502. of_wipe(a_ptr->flags);
  2503. /* Clear the activations for rings and amulets but not lights */
  2504. if (a_ptr->tval != TV_LIGHT) a_ptr->effect = 0;
  2505. /* Artifacts ignore everything */
  2506. flags_set(a_ptr->flags, OF_SIZE, OF_IGNORE_MASK, FLAG_END);
  2507. LOG_PRINT1("Alloc prob is %d\n", a_ptr->alloc_prob);
  2508. }
  2509. /* Got a base item. */
  2510. /* Generate the cumulative frequency table for this item type */
  2511. build_freq_table(a_ptr, art_freq);
  2512. /* Copy artifact info temporarily. */
  2513. a_old = *a_ptr;
  2514. /* Give this artifact a shot at being supercharged */
  2515. try_supercharge(a_ptr, power);
  2516. ap = artifact_power(a_idx);
  2517. if (ap > (power * 23) / 20 + 1)
  2518. {
  2519. /* too powerful -- put it back */
  2520. *a_ptr = a_old;
  2521. LOG_PRINT("--- Supercharge is too powerful! Rolling back.\n");
  2522. }
  2523. /* First draft: add two abilities, then curse it three times. */
  2524. if (curse_me)
  2525. {
  2526. /* Copy artifact info temporarily. */
  2527. a_old = *a_ptr;
  2528. do
  2529. {
  2530. add_ability(a_ptr, power);
  2531. add_ability(a_ptr, power);
  2532. do_curse(a_ptr);
  2533. do_curse(a_ptr);
  2534. do_curse(a_ptr);
  2535. remove_contradictory(a_ptr);
  2536. ap = artifact_power(a_idx);
  2537. /* Accept if it doesn't have any inhibited abilities */
  2538. if (ap < INHIBIT_POWER) success = TRUE;
  2539. /* Otherwise go back and try again */
  2540. else
  2541. {
  2542. LOG_PRINT("Inhibited ability added - rolling back.\n");
  2543. *a_ptr = a_old;
  2544. }
  2545. }
  2546. while (!success);
  2547. /* Cursed items never have any resale value */
  2548. a_ptr->cost = 0;
  2549. }
  2550. else
  2551. {
  2552. /*
  2553. * Select a random set of abilities which roughly matches the
  2554. * original's in terms of overall power/usefulness.
  2555. */
  2556. for (tries = 0; tries < MAX_TRIES; tries++)
  2557. {
  2558. /* Copy artifact info temporarily. */
  2559. a_old = *a_ptr;
  2560. add_ability(a_ptr, power);
  2561. ap = artifact_power(a_idx);
  2562. /* CR 11/14/01 - pushed both limits up by about 5% */
  2563. if (ap > (power * 23) / 20 + 1)
  2564. {
  2565. /* too powerful -- put it back */
  2566. *a_ptr = a_old;
  2567. LOG_PRINT("--- Too powerful! Rolling back.\n");
  2568. continue;
  2569. }
  2570. else if (ap >= (power * 19) / 20) /* just right */
  2571. {
  2572. /* CC 11/02/09 - add rescue for crappy weapons */
  2573. if ((a_ptr->tval == TV_DIGGING || a_ptr->tval == TV_HAFTED ||
  2574. a_ptr->tval == TV_POLEARM || a_ptr->tval == TV_SWORD
  2575. || a_ptr->tval == TV_BOW) && (a_ptr->to_d < 10))
  2576. {
  2577. a_ptr->to_d += randint0(10);
  2578. LOG_PRINT1("Redeeming crappy weapon: +dam now %d\n", a_ptr->to_d);
  2579. }
  2580. break;
  2581. }
  2582. /* Stop if we're going negative, so we don't overload
  2583. the artifact with great powers to compensate. */
  2584. /* Removed CR 11/10/01 */
  2585. /*
  2586. else if ((ap < 0) && (ap < (-(power * 1)) / 10))
  2587. {
  2588. break;
  2589. }
  2590. */
  2591. } /* end of power selection */
  2592. if (verbose && tries >= MAX_TRIES)
  2593. {
  2594. /*
  2595. * We couldn't generate an artifact within the number of permitted
  2596. * iterations. Show a warning message.
  2597. */
  2598. msg_format("Warning! Couldn't get appropriate power level on artifact.");
  2599. LOG_PRINT("Warning! Couldn't get appropriate power level on artifact.\n");
  2600. message_flush();
  2601. }
  2602. }
  2603. /* Set depth and rarity info according to power */
  2604. /* This is currently very tricky for special artifacts */
  2605. LOG_PRINT2("Old depths are min %d, max %d\n", a_ptr->alloc_min, a_ptr->alloc_max);
  2606. LOG_PRINT1("Alloc prob is %d\n", a_ptr->alloc_prob);
  2607. /* flip cursed items to avoid overflows */
  2608. if (ap < 0) ap = -ap;
  2609. if (a_idx < ART_MIN_NORMAL)
  2610. {
  2611. a_ptr->alloc_max = 127;
  2612. if (ap > avg_power)
  2613. {
  2614. a_ptr->alloc_prob = (max_power - ap) / 100;
  2615. a_ptr->alloc_min = MAX(50, (ap * 100 / max_power));
  2616. }
  2617. else if (ap > 40)
  2618. {
  2619. a_ptr->alloc_prob = MAX(3, (avg_power - ap) / 20);
  2620. a_ptr->alloc_min = MAX(25, (ap / 4));
  2621. }
  2622. else /* Just the Phial */
  2623. {
  2624. a_ptr->alloc_prob = 50 - ap;
  2625. a_ptr->alloc_min = 5;
  2626. }
  2627. }
  2628. else
  2629. {
  2630. LOG_PRINT1("k_ptr->alloc_prob is %d\n", k_ptr->alloc_prob);
  2631. a_ptr->alloc_max = MIN(127, (ap * 4) / 5);
  2632. a_ptr->alloc_min = MAX(1, (ap * 100 / max_power));
  2633. /* Hugely powerful artifacts or base items have tot prob 1 */
  2634. if (((ap * ap) > ((avg_power * avg_power) + (2 * var_power))) ||
  2635. (k_ptr->alloc_prob < 10))
  2636. a_ptr->alloc_prob = 100 / k_ptr->alloc_prob;
  2637. /* The rest of the good have total prob 1-3 */
  2638. else if (ap > avg_power)
  2639. a_ptr->alloc_prob = (max_power - ap) / k_ptr->alloc_prob;
  2640. /* The best of the rest have total prob 4-10 */
  2641. else if ((ap * ap) > ((avg_power * avg_power) - var_power))
  2642. a_ptr->alloc_prob = (100 / k_ptr->alloc_prob) *
  2643. (avg_power - ap + 80) / 10;
  2644. /* The weak ones have prob >10 */
  2645. else
  2646. a_ptr->alloc_prob = (100 / k_ptr->alloc_prob) *
  2647. (avg_power - ap) / 4;
  2648. }
  2649. /* sanity check */
  2650. if (a_ptr->alloc_prob > 99) a_ptr->alloc_prob = 99;
  2651. if (a_ptr->alloc_prob < 1) a_ptr->alloc_prob = 1;
  2652. LOG_PRINT2("New depths are min %d, max %d\n", a_ptr->alloc_min, a_ptr->alloc_max);
  2653. LOG_PRINT1("Power-based alloc_prob is %d\n", a_ptr->alloc_prob);
  2654. /* Restore some flags */
  2655. if (a_ptr->tval == TV_LIGHT) of_on(a_ptr->flags, OF_NO_FUEL);
  2656. if (a_idx < ART_MIN_NORMAL) of_on(a_ptr->flags, OF_INSTA_ART);
  2657. /*
  2658. * Add OF_HIDE_TYPE to all artifacts with nonzero pval because we're
  2659. * too lazy to find out which ones need it and which ones don't.
  2660. */
  2661. if (a_ptr->pval)
  2662. of_on(a_ptr->flags, OF_HIDE_TYPE);
  2663. /* Success */
  2664. LOG_PRINT(">>>>>>>>>>>>>>>>>>>>>>>>>> ARTIFACT COMPLETED <<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
  2665. LOG_PRINT2("Number of tries for artifact %d was: %d\n", a_idx, tries);
  2666. }
  2667. /*
  2668. * Return TRUE if the whole set of random artifacts meets certain
  2669. * criteria. Return FALSE if we fail to meet those criteria (which will
  2670. * restart the whole process).
  2671. */
  2672. static bool artifacts_acceptable(void)
  2673. {
  2674. int swords = 5, polearms = 5, blunts = 5, bows = 4;
  2675. int bodies = 5, shields = 4, cloaks = 4, hats = 4;
  2676. int gloves = 4, boots = 4;
  2677. int i;
  2678. for (i = ART_MIN_NORMAL; i < z_info->a_max; i++)
  2679. {
  2680. switch (a_info[i].tval)
  2681. {
  2682. case TV_SWORD:
  2683. swords--; break;
  2684. case TV_POLEARM:
  2685. polearms--; break;
  2686. case TV_HAFTED:
  2687. blunts--; break;
  2688. case TV_BOW:
  2689. bows--; break;
  2690. case TV_SOFT_ARMOR:
  2691. case TV_HARD_ARMOR:
  2692. case TV_DRAG_ARMOR:
  2693. bodies--; break;
  2694. case TV_SHIELD:
  2695. shields--; break;
  2696. case TV_CLOAK:
  2697. cloaks--; break;
  2698. case TV_HELM:
  2699. case TV_CROWN:
  2700. hats--; break;
  2701. case TV_GLOVES:
  2702. gloves--; break;
  2703. case TV_BOOTS:
  2704. boots--; break;
  2705. }
  2706. }
  2707. LOG_PRINT1("Deficit amount for swords is %d\n", swords);
  2708. LOG_PRINT1("Deficit amount for polearms is %d\n", polearms);
  2709. LOG_PRINT1("Deficit amount for blunts is %d\n", blunts);
  2710. LOG_PRINT1("Deficit amount for bows is %d\n", bows);
  2711. LOG_PRINT1("Deficit amount for bodies is %d\n", bodies);
  2712. LOG_PRINT1("Deficit amount for shields is %d\n", shields);
  2713. LOG_PRINT1("Deficit amount for cloaks is %d\n", cloaks);
  2714. LOG_PRINT1("Deficit amount for hats is %d\n", hats);
  2715. LOG_PRINT1("Deficit amount for gloves is %d\n", gloves);
  2716. LOG_PRINT1("Deficit amount for boots is %d\n", boots);
  2717. if (swords > 0 || polearms > 0 || blunts > 0 || bows > 0 ||
  2718. bodies > 0 || shields > 0 || cloaks > 0 || hats > 0 ||
  2719. gloves > 0 || boots > 0)
  2720. {
  2721. if (verbose)
  2722. {
  2723. char types[256];
  2724. strnfmt(types, sizeof(types), "%s%s%s%s%s%s%s%s%s%s",
  2725. swords > 0 ? " swords" : "",
  2726. polearms > 0 ? " polearms" : "",
  2727. blunts > 0 ? " blunts" : "",
  2728. bows > 0 ? " bows" : "",
  2729. bodies > 0 ? " body-armors" : "",
  2730. shields > 0 ? " shields" : "",
  2731. cloaks > 0 ? " cloaks" : "",
  2732. hats > 0 ? " hats" : "",
  2733. gloves > 0 ? " gloves" : "",
  2734. boots > 0 ? " boots" : "");
  2735. msg_format("Restarting generation process: not enough%s", types);
  2736. LOG_PRINT1("Restarting generation process: not enough%s", types);
  2737. }
  2738. return FALSE;
  2739. }
  2740. else
  2741. {
  2742. return TRUE;
  2743. }
  2744. }
  2745. static errr scramble(void)
  2746. {
  2747. /* If our artifact set fails to meet certain criteria, we start over. */
  2748. do
  2749. {
  2750. int a_idx;
  2751. /* Generate all the artifacts. */
  2752. for (a_idx = 1; a_idx < z_info->a_max; a_idx++)
  2753. {
  2754. scramble_artifact(a_idx);
  2755. }
  2756. } while (!artifacts_acceptable()); /* end of all artifacts */
  2757. /* Success */
  2758. return (0);
  2759. }
  2760. static errr do_randart_aux(bool full)
  2761. {
  2762. errr result;
  2763. /* Generate random names */
  2764. if ((result = init_names()) != 0) return (result);
  2765. if (full)
  2766. {
  2767. /* Randomize the artifacts */
  2768. if ((result = scramble()) != 0) return (result);
  2769. }
  2770. /* Success */
  2771. return (0);
  2772. }
  2773. /*
  2774. * Randomize the artifacts
  2775. *
  2776. * The full flag toggles between just randomizing the names and
  2777. * complete randomization of the artifacts.
  2778. */
  2779. errr do_randart(u32b randart_seed, bool full)
  2780. {
  2781. errr err;
  2782. /* Prepare to use the Angband "simple" RNG. */
  2783. Rand_value = randart_seed;
  2784. Rand_quick = TRUE;
  2785. /* Only do all the following if full randomization requested */
  2786. if (full)
  2787. {
  2788. /* Allocate the various "original powers" arrays */
  2789. base_power = C_ZNEW(z_info->a_max, s32b);
  2790. base_item_level = C_ZNEW(z_info->a_max, byte);
  2791. base_item_prob = C_ZNEW(z_info->a_max, byte);
  2792. base_art_alloc = C_ZNEW(z_info->a_max, byte);
  2793. baseprobs = C_ZNEW(z_info->k_max, s16b);
  2794. base_freq = C_ZNEW(z_info->k_max, s16b);
  2795. /* Open the log file for writing */
  2796. if (verbose)
  2797. {
  2798. char buf[1024];
  2799. path_build(buf, sizeof(buf), ANGBAND_DIR_USER,
  2800. "randart.log");
  2801. log_file = file_open(buf, MODE_WRITE, FTYPE_TEXT);
  2802. if (!log_file)
  2803. {
  2804. msg_print("Error - can't open randart.log for writing.");
  2805. exit(1);
  2806. }
  2807. }
  2808. /* Store the original power ratings */
  2809. store_base_power();
  2810. /* Determine the generation probabilities */
  2811. parse_frequencies();
  2812. }
  2813. /* Generate the random artifact (names) */
  2814. err = do_randart_aux(full);
  2815. /* Only do all the following if full randomization requested */
  2816. if (full)
  2817. {
  2818. /* Just for fun, look at the frequencies on the finished items */
  2819. /* Remove this prior to release */
  2820. store_base_power();
  2821. parse_frequencies();
  2822. /* Close the log file */
  2823. if (verbose)
  2824. {
  2825. if (!file_close(log_file))
  2826. {
  2827. msg_print("Error - can't close randart.log file.");
  2828. exit(1);
  2829. }
  2830. }
  2831. /* Free the "original powers" arrays */
  2832. FREE(base_power);
  2833. FREE(base_item_level);
  2834. FREE(base_item_prob);
  2835. FREE(base_art_alloc);
  2836. FREE(baseprobs);
  2837. FREE(base_freq);
  2838. }
  2839. /* When done, resume use of the Angband "complex" RNG. */
  2840. Rand_quick = FALSE;
  2841. return (err);
  2842. }