| 1 | /* |
| 2 | * PROPRIETARY INFORMATION. This software is proprietary to POWDER |
| 3 | * Development, and is not to be reproduced, transmitted, or disclosed |
| 4 | * in any way without written permission. |
| 5 | * |
| 6 | * Produced by: Jeff Lait |
| 7 | * |
| 8 | * POWDER Development |
| 9 | * |
| 10 | * NAME: artifact.cpp ( POWDER Library, C++ ) |
| 11 | * |
| 12 | * COMMENTS: |
| 13 | * This code manages the list of allocated artifacts, and the code |
| 14 | * required to build artifacts. |
| 15 | */ |
| 16 | |
| 17 | #include "artifact.h" |
| 18 | #include "rand.h" |
| 19 | #include "grammar.h" |
| 20 | |
| 21 | // This is the root artifact pointer. Disgusting O(N) search for |
| 22 | // each artifact, but likely cheaper than rebuilding from the string. |
| 23 | // Theoritically, total number of artifacts is low. |
| 24 | ARTIFACT *glb_artifacts = 0; |
| 25 | |
| 26 | int glb_artifactseed = 0; |
| 27 | |
| 28 | |
| 29 | ARTIFACT::ARTIFACT() |
| 30 | { |
| 31 | // We want to ensure all of the attacks are fully initialized. |
| 32 | // This is more difficult than one may think, as their data structure |
| 33 | // changes when source.txt does. |
| 34 | // By initializing from safe attacks, new attack bits won't |
| 35 | // be uninitalized when our build code doesn't know about them. |
| 36 | memcpy(&attack, &glb_attackdefs[ATTACK_MISUSED], sizeof(ATTACK_DEF)); |
| 37 | memcpy(&thrownattack, &glb_attackdefs[ATTACK_MISTHROWN], sizeof(ATTACK_DEF)); |
| 38 | |
| 39 | hasattack = false; |
| 40 | hasthrownattack = false; |
| 41 | |
| 42 | acbonus = 0; |
| 43 | lightradius = 0; |
| 44 | iscarryintrinsic = 0; |
| 45 | intrinsics = 0; |
| 46 | name = 0; |
| 47 | baseitem = ITEM_NONE; |
| 48 | next = 0; |
| 49 | } |
| 50 | |
| 51 | ARTIFACT::~ARTIFACT() |
| 52 | { |
| 53 | if (intrinsics) |
| 54 | free(intrinsics); |
| 55 | if (name) |
| 56 | free(name); |
| 57 | delete next; |
| 58 | } |
| 59 | |
| 60 | // |
| 61 | // Artifact functions |
| 62 | // |
| 63 | |
| 64 | int |
| 65 | artifact_rand() |
| 66 | { |
| 67 | // This is straight from K & R |
| 68 | glb_artifactseed = (1103515245 * glb_artifactseed + 12345); |
| 69 | glb_artifactseed &= 32767; |
| 70 | |
| 71 | return glb_artifactseed; |
| 72 | } |
| 73 | |
| 74 | // Seeds the artifact RNG with the given name. |
| 75 | // We don't want to just reseed our main RNG as we don't have |
| 76 | // an efficient seed/unseed procedure. (getseed will truncate |
| 77 | // the RNG state to 32 bits) |
| 78 | void |
| 79 | artifact_seed(const char *name) |
| 80 | { |
| 81 | glb_artifactseed = 0; |
| 82 | while (*name) |
| 83 | { |
| 84 | glb_artifactseed *= 37; |
| 85 | glb_artifactseed += *name; |
| 86 | name++; |
| 87 | } |
| 88 | |
| 89 | glb_artifactseed = glb_artifactseed & 32767; |
| 90 | |
| 91 | // We want to iterate the random number generator a few times |
| 92 | // to remove the highly dependent variables |
| 93 | // (as we are seeding from a string hash, our initial values |
| 94 | // are likely strongly correlated) |
| 95 | // cf: http://home1.gte.net/deleyd/random/random4.html |
| 96 | artifact_rand(); |
| 97 | artifact_rand(); |
| 98 | artifact_rand(); |
| 99 | } |
| 100 | |
| 101 | int |
| 102 | artifact_randchoice(int num) |
| 103 | { |
| 104 | int v; |
| 105 | |
| 106 | // Choice of 0 or 1 is always 0. |
| 107 | if (num < 2) return 0; |
| 108 | |
| 109 | v = abs(artifact_rand()); |
| 110 | v %= num; |
| 111 | |
| 112 | return v; |
| 113 | } |
| 114 | |
| 115 | bool |
| 116 | artifact_randchance(int percentage) |
| 117 | { |
| 118 | int percent; |
| 119 | |
| 120 | percent = artifact_randchoice(100); |
| 121 | // We want strict less than so percentage 0 will never pass, |
| 122 | // and percentage 99 will pass only one in 100. |
| 123 | return percent < percentage; |
| 124 | } |
| 125 | |
| 126 | void |
| 127 | artifact_init() |
| 128 | { |
| 129 | delete glb_artifacts; |
| 130 | glb_artifacts = 0; |
| 131 | } |
| 132 | |
| 133 | BUF |
| 134 | artifact_buildname(ITEM_NAMES /*definition*/) |
| 135 | { |
| 136 | BUF buf; |
| 137 | |
| 138 | // A collection of self-serving syllables, often dredged from |
| 139 | // r.g.r.d. |
| 140 | // Should divide into different categories. Some are true syllables |
| 141 | // (such as "oma") which should be appended together. Other are |
| 142 | // miniwords (like "shock") that should be joined with hyphens or |
| 143 | // mixed case (as in: ShockFrost) |
| 144 | const char *syllables[] = |
| 145 | { |
| 146 | "shock", |
| 147 | "frost", |
| 148 | "amy", |
| 149 | "wang", |
| 150 | "whiz", |
| 151 | "wor", |
| 152 | "brask", |
| 153 | "trey", |
| 154 | "klay", |
| 155 | "oma", |
| 156 | "upa", |
| 157 | "relat", |
| 158 | "froody", |
| 159 | "bar", |
| 160 | "baz", |
| 161 | "zub", |
| 162 | "tree", |
| 163 | "orc", |
| 164 | "smash", |
| 165 | "yyz", |
| 166 | "were", |
| 167 | "slug", |
| 168 | 0 |
| 169 | }; |
| 170 | |
| 171 | int num_syllable, syl; |
| 172 | |
| 173 | for (num_syllable = 0; syllables[num_syllable]; num_syllable++); |
| 174 | |
| 175 | // 2-3 syllables. |
| 176 | // Ideally we should sometimes hyphenate these. |
| 177 | // This would all, I think, be much easier in German than English. |
| 178 | |
| 179 | syl = rand_choice(num_syllable); |
| 180 | buf.strcpy(syllables[syl]); |
| 181 | syl = rand_choice(num_syllable); |
| 182 | buf.strcat(syllables[syl]); |
| 183 | // A strong effect can be achieved by doubling the last |
| 184 | // syllable, so increase the probability of that (Orcsmashsmash) |
| 185 | if (!rand_choice(3)) |
| 186 | { |
| 187 | if (rand_choice(3)) |
| 188 | buf.strcat(syllables[rand_choice(num_syllable)]); |
| 189 | else |
| 190 | buf.strcat(syllables[syl]); |
| 191 | } |
| 192 | |
| 193 | return (gram_capitalize(buf)); |
| 194 | } |
| 195 | |
| 196 | // |
| 197 | // Either looks up the artifact, or builds it from scratch and adds it |
| 198 | // to our cache. |
| 199 | // The artifact data structure tells the ITEM::get functions how to |
| 200 | // overload the items behaviour. |
| 201 | // Just started a nice glass of wine with those code. |
| 202 | // Be warned. Music just stopped. Must restart it. New song is: |
| 203 | // Gypsy Kings: Bem, Bem, Maria |
| 204 | // |
| 205 | ARTIFACT * |
| 206 | artifact_buildartifact(const char *name, ITEM_NAMES baseitem) |
| 207 | { |
| 208 | // Search for an existing artifact definition. |
| 209 | ARTIFACT *art; |
| 210 | ITEMTYPE_NAMES itemtype; |
| 211 | |
| 212 | if (!name) |
| 213 | return 0; |
| 214 | |
| 215 | for (art = glb_artifacts; art; art = art->next) |
| 216 | { |
| 217 | if (baseitem == art->baseitem && !strcmp(art->name, name)) |
| 218 | { |
| 219 | return art; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // No artifact found. Build a new one. |
| 224 | art = new ARTIFACT(); |
| 225 | art->next = glb_artifacts; |
| 226 | glb_artifacts = art; |
| 227 | |
| 228 | art->name = strdup(name); |
| 229 | art->baseitem = baseitem; |
| 230 | |
| 231 | itemtype = (ITEMTYPE_NAMES) glb_itemdefs[baseitem].itemtype; |
| 232 | |
| 233 | // Now onto Guns & Roses: You're Crazy. |
| 234 | INTRINSIC_NAMES int_good[] = |
| 235 | { |
| 236 | INTRINSIC_FAST, |
| 237 | INTRINSIC_UNCHANGING, |
| 238 | INTRINSIC_RESISTSTONING, |
| 239 | INTRINSIC_RESISTFIRE, |
| 240 | INTRINSIC_RESISTCOLD, |
| 241 | INTRINSIC_RESISTSHOCK, |
| 242 | INTRINSIC_RESISTACID, |
| 243 | INTRINSIC_RESISTPOISON, |
| 244 | // INTRINSIC_RESISTPHYSICAL, // I think this is too powerful. |
| 245 | INTRINSIC_TELEPORTCONTROL, |
| 246 | INTRINSIC_REGENERATION, |
| 247 | INTRINSIC_REFLECTION, |
| 248 | INTRINSIC_SEARCH, // In someways, this is bad :> |
| 249 | INTRINSIC_INVISIBLE, |
| 250 | INTRINSIC_SEEINVISIBLE, |
| 251 | INTRINSIC_TELEPATHY, |
| 252 | INTRINSIC_NOBREATH, |
| 253 | INTRINSIC_LIFESAVE, // You don't want this on +7 sword :> |
| 254 | // Especially if it kicks in prior to |
| 255 | // your amulet :> |
| 256 | INTRINSIC_RESISTSLEEP, |
| 257 | INTRINSIC_FREEDOM, |
| 258 | INTRINSIC_WATERWALK, |
| 259 | INTRINSIC_JUMP, |
| 260 | INTRINSIC_WARNING, |
| 261 | INTRINSIC_DIG, |
| 262 | INTRINSIC_NONE |
| 263 | }; |
| 264 | |
| 265 | INTRINSIC_NAMES int_bad[] = |
| 266 | { |
| 267 | INTRINSIC_SLOW, |
| 268 | INTRINSIC_TELEPORT, |
| 269 | INTRINSIC_TELEFIXED, |
| 270 | INTRINSIC_POSITIONREVEALED, |
| 271 | INTRINSIC_MISSINGFINGER, |
| 272 | INTRINSIC_MAGICDRAIN, |
| 273 | INTRINSIC_BLEED, |
| 274 | INTRINSIC_VULNSILVER, |
| 275 | INTRINSIC_VULNFIRE, |
| 276 | INTRINSIC_VULNCOLD, |
| 277 | INTRINSIC_VULNACID, |
| 278 | INTRINSIC_VULNSHOCK, |
| 279 | INTRINSIC_VULNPHYSICAL, |
| 280 | INTRINSIC_BLIND, |
| 281 | INTRINSIC_STRANGLE, |
| 282 | INTRINSIC_TIRED, |
| 283 | INTRINSIC_AFLAME, // Look forward to fireresist + aflame :> |
| 284 | INTRINSIC_POLYMORPH, |
| 285 | INTRINSIC_NOISY, |
| 286 | INTRINSIC_DEAF, |
| 287 | INTRINSIC_CONFUSED, |
| 288 | INTRINSIC_AMNESIA, |
| 289 | INTRINSIC_NOREGEN, |
| 290 | INTRINSIC_NONE |
| 291 | }; |
| 292 | |
| 293 | int numgood, numbad; |
| 294 | |
| 295 | // There is a cool sizeof() method that could simplify this. |
| 296 | // Too lazy to figure it out, however. |
| 297 | for (numgood = 0; int_good[numgood] != INTRINSIC_NONE; numgood++); |
| 298 | for (numbad = 0; int_bad[numbad] != INTRINSIC_NONE; numbad++); |
| 299 | |
| 300 | // Ensure we start picking well |
| 301 | artifact_seed(name); |
| 302 | |
| 303 | // There is a 90% chance of one bonus intrinsic. |
| 304 | // After that, we have a 10% chance per intrinsic. |
| 305 | char intbuf[100]; |
| 306 | int numints; |
| 307 | |
| 308 | numints = 0; |
| 309 | if (artifact_randchance(90)) |
| 310 | { |
| 311 | do |
| 312 | { |
| 313 | intbuf[numints++] = int_good[artifact_randchoice(numgood)]; |
| 314 | } while (artifact_randchance(10) && numints < 99); |
| 315 | } |
| 316 | |
| 317 | // There is a cumalative 10% chance of a bad intrinsic. |
| 318 | while (artifact_randchance(10) && numints < 99) |
| 319 | { |
| 320 | intbuf[numints++] = int_bad[artifact_randchoice(numbad)]; |
| 321 | } |
| 322 | |
| 323 | // If we had any intrinsics, we want to add them to art. |
| 324 | if (numints) |
| 325 | { |
| 326 | intbuf[numints++] = '\0'; |
| 327 | art->intrinsics = strdup(intbuf); |
| 328 | } |
| 329 | |
| 330 | // Determine light radius. Artifacts lacking intrinsics |
| 331 | // will always get light. Light is really cool! |
| 332 | // NOTE: Because of this, a lit artifact has a 50% chance |
| 333 | // of having no intrinsics! |
| 334 | if (!numints || artifact_randchance(10)) |
| 335 | { |
| 336 | art->lightradius = 2; |
| 337 | // 30% for each higher amount. |
| 338 | while (art->lightradius < 6 && artifact_randchance(30)) |
| 339 | { |
| 340 | art->lightradius++; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | // Determine if we get carry intrinsic. This is *very* powerful. |
| 345 | // Be careful about giving this. |
| 346 | if (numints && artifact_randchance(1)) |
| 347 | art->iscarryintrinsic = 1; |
| 348 | |
| 349 | // Armour types get an AC bonus. |
| 350 | // Weapons have a 10% chance of a bonus. Extra bonuses |
| 351 | // occur with a 30% chance. |
| 352 | int acbonus = 0; |
| 353 | bool weapac = artifact_randchance(10); |
| 354 | do |
| 355 | { |
| 356 | acbonus++; |
| 357 | } while (artifact_randchance(30) && (acbonus < 99)); |
| 358 | |
| 359 | if (itemtype == ITEMTYPE_ARMOUR || |
| 360 | (itemtype == ITEMTYPE_WEAPON && weapac)) |
| 361 | { |
| 362 | art->acbonus = acbonus; |
| 363 | } |
| 364 | |
| 365 | // Determine bonus attacks. |
| 366 | // Weapons always get a bonus attack. Other things |
| 367 | // a 10% chance. |
| 368 | int numverbs, verb; |
| 369 | |
| 370 | // Possible verb/element combinations |
| 371 | const char *verbs[] = |
| 372 | { |
| 373 | "pulverize", |
| 374 | "burn", |
| 375 | "freeze", |
| 376 | "zap", |
| 377 | "dissolve", |
| 378 | "blind", |
| 379 | "drain", |
| 380 | 0 |
| 381 | }; |
| 382 | |
| 383 | const ELEMENT_NAMES elements[] = |
| 384 | { |
| 385 | ELEMENT_PHYSICAL, |
| 386 | ELEMENT_FIRE, |
| 387 | ELEMENT_COLD, |
| 388 | ELEMENT_SHOCK, |
| 389 | ELEMENT_ACID, |
| 390 | ELEMENT_LIGHT, |
| 391 | ELEMENT_DEATH |
| 392 | }; |
| 393 | |
| 394 | for (numverbs = 0; verbs[numverbs]; numverbs++); |
| 395 | |
| 396 | art->hasattack = false; |
| 397 | if (itemtype == ITEMTYPE_WEAPON || artifact_randchance(10)) |
| 398 | { |
| 399 | art->hasattack = true; |
| 400 | |
| 401 | verb = artifact_randchoice(numverbs); |
| 402 | art->attack.verb = verbs[verb]; |
| 403 | |
| 404 | // A random damage amount. |
| 405 | art->attack.damage.myNumdie = artifact_randchoice(2) + 1; |
| 406 | art->attack.damage.mySides = artifact_randchoice(8) + 1; |
| 407 | art->attack.damage.myBonus = artifact_randchoice(4); |
| 408 | |
| 409 | // Inherit our base item's bonus to hit |
| 410 | art->attack.bonustohit = glb_attackdefs[glb_itemdefs[baseitem].attack].bonustohit; |
| 411 | // And get a bit extra... |
| 412 | while (artifact_randchance(30) && (art->attack.bonustohit < 99)) |
| 413 | { |
| 414 | art->attack.bonustohit++; |
| 415 | } |
| 416 | |
| 417 | art->attack.element = elements[verb]; |
| 418 | |
| 419 | // Chain the attack. |
| 420 | if (glb_itemdefs[baseitem].attack != ATTACK_MISUSED) |
| 421 | art->attack.sameattack = glb_itemdefs[baseitem].attack; |
| 422 | } |
| 423 | |
| 424 | // Determine thrown attack. |
| 425 | // Weapons have a 10% chance. Thrown weapons 100% chance. |
| 426 | art->hasthrownattack = false; |
| 427 | if (itemtype == ITEMTYPE_WEAPON && |
| 428 | (glb_itemdefs[baseitem].thrownattack != ATTACK_MISTHROWN || |
| 429 | artifact_randchance(10))) |
| 430 | { |
| 431 | art->hasthrownattack = true; |
| 432 | |
| 433 | // Determine the range... |
| 434 | art->thrownattack.range = glb_attackdefs[glb_itemdefs[baseitem].thrownattack].range; |
| 435 | while (artifact_randchance(30) && (art->thrownattack.range < 99)) |
| 436 | { |
| 437 | art->thrownattack.range++; |
| 438 | } |
| 439 | |
| 440 | // Inherit our base item's bonus to hit |
| 441 | art->thrownattack.bonustohit = glb_attackdefs[glb_itemdefs[baseitem].thrownattack].bonustohit; |
| 442 | // And get a bit extra... |
| 443 | while (artifact_randchance(30) && (art->thrownattack.bonustohit < 99)) |
| 444 | { |
| 445 | art->thrownattack.bonustohit++; |
| 446 | } |
| 447 | |
| 448 | // A random damage amount. |
| 449 | art->thrownattack.damage.myNumdie = artifact_randchoice(2) + 1; |
| 450 | art->thrownattack.damage.mySides = artifact_randchoice(8) + 1; |
| 451 | art->thrownattack.damage.myBonus = artifact_randchoice(4); |
| 452 | |
| 453 | // Chain the attack. |
| 454 | if (glb_itemdefs[baseitem].thrownattack != ATTACK_MISTHROWN) |
| 455 | art->thrownattack.sameattack = glb_itemdefs[baseitem].thrownattack; |
| 456 | } |
| 457 | |
| 458 | // TODO: Zappable artifacts! |
| 459 | |
| 460 | |
| 461 | return art; |
| 462 | } |