Source:NetHack 3.6.2/src/ball.c
Jump to navigation
Jump to search
Below is the full text to ball.c from the source code of NetHack 3.6.2. To link to a particular line, write [[Source:NetHack 3.6.2/src/ball.c#line12]], for example.
The NetHack General Public License applies to screenshots, source code and other content from NetHack.
This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.
1. /* NetHack 3.6 ball.c $NHDT-Date: 1557088406 2019/05/05 20:33:26 $ $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.36 $ */ 2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ 3. /*-Copyright (c) David Cohrs, 2006. */ 4. /* NetHack may be freely redistributed. See license for details. */ 5. 6. /* Ball & Chain 7. * =============================================================*/ 8. 9. #include "hack.h" 10. 11. STATIC_DCL int NDECL(bc_order); 12. STATIC_DCL void NDECL(litter); 13. 14. void 15. ballrelease(showmsg) 16. boolean showmsg; 17. { 18. if (carried(uball)) { 19. if (showmsg) 20. pline("Startled, you drop the iron ball."); 21. if (uwep == uball) 22. setuwep((struct obj *) 0); 23. if (uswapwep == uball) 24. setuswapwep((struct obj *) 0); 25. if (uquiver == uball) 26. setuqwep((struct obj *) 0); 27. /* [this used to test 'if (uwep != uball)' but that always passes 28. after the setuwep() above] */ 29. freeinv(uball); /* remove from inventory but don't place on floor */ 30. encumber_msg(); 31. } 32. } 33. 34. /* ball&chain might hit hero when falling through a trap door */ 35. void 36. ballfall() 37. { 38. boolean gets_hit; 39. 40. gets_hit = (((uball->ox != u.ux) || (uball->oy != u.uy)) 41. && ((uwep == uball) ? FALSE : (boolean) rn2(5))); 42. ballrelease(TRUE); 43. if (gets_hit) { 44. int dmg = rn1(7, 25); 45. 46. pline_The("iron ball falls on your %s.", body_part(HEAD)); 47. if (uarmh) { 48. if (is_metallic(uarmh)) { 49. pline("Fortunately, you are wearing a hard helmet."); 50. dmg = 3; 51. } else if (flags.verbose) 52. pline("%s does not protect you.", Yname2(uarmh)); 53. } 54. losehp(Maybe_Half_Phys(dmg), "crunched in the head by an iron ball", 55. NO_KILLER_PREFIX); 56. } 57. } 58. 59. /* 60. * To make this work, we have to mess with the hero's mind. The rules for 61. * ball&chain are: 62. * 63. * 1. If the hero can see them, fine. 64. * 2. If the hero can't see either, it isn't seen. 65. * 3. If either is felt it is seen. 66. * 4. If either is felt and moved, it disappears. 67. * 68. * If the hero can see, then when a move is done, the ball and chain are 69. * first picked up, the positions under them are corrected, then they 70. * are moved after the hero moves. Not too bad. 71. * 72. * If the hero is blind, then she can "feel" the ball and/or chain at any 73. * time. However, when the hero moves, the felt ball and/or chain become 74. * unfelt and whatever was felt "under" the ball&chain appears. Pretty 75. * nifty, but it requires that the ball&chain "remember" what was under 76. * them --- i.e. they pick-up glyphs when they are felt and drop them when 77. * moved (and felt). When swallowed, the ball&chain are pulled completely 78. * off of the dungeon, but are still on the object chain. They are placed 79. * under the hero when she is expelled. 80. */ 81. 82. /* 83. * from you.h 84. * int u.bglyph glyph under the ball 85. * int u.cglyph glyph under the chain 86. * int u.bc_felt mask for ball/chain being felt 87. * #define BC_BALL 0x01 bit mask in u.bc_felt for ball 88. * #define BC_CHAIN 0x02 bit mask in u.bc_felt for chain 89. * int u.bc_order ball & chain order 90. * 91. * u.bc_felt is also manipulated in display.c and read.c, the others only 92. * in this file. None of these variables are valid unless the player is 93. * Blind. 94. */ 95. 96. /* values for u.bc_order */ 97. #define BCPOS_DIFFER 0 /* ball & chain at different positions */ 98. #define BCPOS_CHAIN 1 /* chain on top of ball */ 99. #define BCPOS_BALL 2 /* ball on top of chain */ 100. 101. /* 102. * Place the ball & chain under the hero. Make sure that the ball & chain 103. * variables are set (actually only needed when blind, but what the heck). 104. * It is assumed that when this is called, the ball and chain are NOT 105. * attached to the object list. 106. * 107. * Should not be called while swallowed except on waterlevel. 108. */ 109. void 110. placebc() 111. { 112. if (!uchain || !uball) { 113. impossible("Where are your ball and chain?"); 114. return; 115. } 116. 117. (void) flooreffects(uchain, u.ux, u.uy, ""); /* chain might rust */ 118. 119. if (carried(uball)) { /* the ball is carried */ 120. u.bc_order = BCPOS_DIFFER; 121. } else { 122. /* ball might rust -- already checked when carried */ 123. (void) flooreffects(uball, u.ux, u.uy, ""); 124. place_object(uball, u.ux, u.uy); 125. u.bc_order = BCPOS_CHAIN; 126. } 127. 128. place_object(uchain, u.ux, u.uy); 129. 130. u.bglyph = u.cglyph = levl[u.ux][u.uy].glyph; /* pick up glyph */ 131. 132. newsym(u.ux, u.uy); 133. } 134. 135. void 136. unplacebc() 137. { 138. if (u.uswallow) { 139. if (Is_waterlevel(&u.uz)) { 140. /* we need to proceed with the removal from the floor 141. * so that movebubbles() processing will disregard it as 142. * intended. Ignore all the vision stuff. 143. */ 144. if (!carried(uball)) 145. obj_extract_self(uball); 146. obj_extract_self(uchain); 147. } 148. /* ball&chain not unplaced while swallowed */ 149. return; 150. } 151. 152. if (!carried(uball)) { 153. obj_extract_self(uball); 154. if (Blind && (u.bc_felt & BC_BALL)) /* drop glyph */ 155. levl[uball->ox][uball->oy].glyph = u.bglyph; 156. 157. newsym(uball->ox, uball->oy); 158. } 159. obj_extract_self(uchain); 160. if (Blind && (u.bc_felt & BC_CHAIN)) /* drop glyph */ 161. levl[uchain->ox][uchain->oy].glyph = u.cglyph; 162. 163. newsym(uchain->ox, uchain->oy); 164. u.bc_felt = 0; /* feel nothing */ 165. } 166. 167. /* 168. * Return the stacking of the hero's ball & chain. This assumes that the 169. * hero is being punished. 170. */ 171. STATIC_OVL int 172. bc_order() 173. { 174. struct obj *obj; 175. 176. if (uchain->ox != uball->ox || uchain->oy != uball->oy || carried(uball) 177. || u.uswallow) 178. return BCPOS_DIFFER; 179. 180. for (obj = level.objects[uball->ox][uball->oy]; obj; 181. obj = obj->nexthere) { 182. if (obj == uchain) 183. return BCPOS_CHAIN; 184. if (obj == uball) 185. return BCPOS_BALL; 186. } 187. impossible("bc_order: ball&chain not in same location!"); 188. return BCPOS_DIFFER; 189. } 190. 191. /* 192. * set_bc() 193. * 194. * The hero is either about to go blind or already blind and just punished. 195. * Set up the ball and chain variables so that the ball and chain are "felt". 196. */ 197. void 198. set_bc(already_blind) 199. int already_blind; 200. { 201. int ball_on_floor = !carried(uball); 202. 203. u.bc_order = bc_order(); /* get the order */ 204. u.bc_felt = ball_on_floor ? BC_BALL | BC_CHAIN : BC_CHAIN; /* felt */ 205. 206. if (already_blind || u.uswallow) { 207. u.cglyph = u.bglyph = levl[u.ux][u.uy].glyph; 208. return; 209. } 210. 211. /* 212. * Since we can still see, remove the ball&chain and get the glyph that 213. * would be beneath them. Then put the ball&chain back. This is pretty 214. * disgusting, but it will work. 215. */ 216. remove_object(uchain); 217. if (ball_on_floor) 218. remove_object(uball); 219. 220. newsym(uchain->ox, uchain->oy); 221. u.cglyph = levl[uchain->ox][uchain->oy].glyph; 222. 223. if (u.bc_order == BCPOS_DIFFER) { /* different locations */ 224. place_object(uchain, uchain->ox, uchain->oy); 225. newsym(uchain->ox, uchain->oy); 226. if (ball_on_floor) { 227. newsym(uball->ox, uball->oy); /* see under ball */ 228. u.bglyph = levl[uball->ox][uball->oy].glyph; 229. place_object(uball, uball->ox, uball->oy); 230. newsym(uball->ox, uball->oy); /* restore ball */ 231. } 232. } else { 233. u.bglyph = u.cglyph; 234. if (u.bc_order == BCPOS_CHAIN) { 235. place_object(uball, uball->ox, uball->oy); 236. place_object(uchain, uchain->ox, uchain->oy); 237. } else { 238. place_object(uchain, uchain->ox, uchain->oy); 239. place_object(uball, uball->ox, uball->oy); 240. } 241. newsym(uball->ox, uball->oy); 242. } 243. } 244. 245. /* 246. * move_bc() 247. * 248. * Move the ball and chain. This is called twice for every move. The first 249. * time to pick up the ball and chain before the move, the second time to 250. * place the ball and chain after the move. If the ball is carried, this 251. * function should never have BC_BALL as part of its control. 252. * 253. * Should not be called while swallowed. 254. */ 255. void 256. move_bc(before, control, ballx, bally, chainx, chainy) 257. int before, control; 258. xchar ballx, bally, chainx, chainy; /* only matter !before */ 259. { 260. if (Blind) { 261. /* 262. * The hero is blind. Time to work hard. The ball and chain that 263. * are attached to the hero are very special. The hero knows that 264. * they are attached, so when they move, the hero knows that they 265. * aren't at the last position remembered. This is complicated 266. * by the fact that the hero can "feel" the surrounding locations 267. * at any time, hence, making one or both of them show up again. 268. * So, we have to keep track of which is felt at any one time and 269. * act accordingly. 270. */ 271. if (!before) { 272. if ((control & BC_CHAIN) && (control & BC_BALL)) { 273. /* 274. * Both ball and chain moved. If felt, drop glyph. 275. */ 276. if (u.bc_felt & BC_BALL) 277. levl[uball->ox][uball->oy].glyph = u.bglyph; 278. if (u.bc_felt & BC_CHAIN) 279. levl[uchain->ox][uchain->oy].glyph = u.cglyph; 280. u.bc_felt = 0; 281. 282. /* Pick up glyph at new location. */ 283. u.bglyph = levl[ballx][bally].glyph; 284. u.cglyph = levl[chainx][chainy].glyph; 285. 286. movobj(uball, ballx, bally); 287. movobj(uchain, chainx, chainy); 288. } else if (control & BC_BALL) { 289. if (u.bc_felt & BC_BALL) { 290. if (u.bc_order == BCPOS_DIFFER) { /* ball by itself */ 291. levl[uball->ox][uball->oy].glyph = u.bglyph; 292. } else if (u.bc_order == BCPOS_BALL) { 293. if (u.bc_felt & BC_CHAIN) { /* know chain is there */ 294. map_object(uchain, 0); 295. } else { 296. levl[uball->ox][uball->oy].glyph = u.bglyph; 297. } 298. } 299. u.bc_felt &= ~BC_BALL; /* no longer feel the ball */ 300. } 301. 302. /* Pick up glyph at new position. */ 303. u.bglyph = (ballx != chainx || bally != chainy) 304. ? levl[ballx][bally].glyph 305. : u.cglyph; 306. 307. movobj(uball, ballx, bally); 308. } else if (control & BC_CHAIN) { 309. if (u.bc_felt & BC_CHAIN) { 310. if (u.bc_order == BCPOS_DIFFER) { 311. levl[uchain->ox][uchain->oy].glyph = u.cglyph; 312. } else if (u.bc_order == BCPOS_CHAIN) { 313. if (u.bc_felt & BC_BALL) { 314. map_object(uball, 0); 315. } else { 316. levl[uchain->ox][uchain->oy].glyph = u.cglyph; 317. } 318. } 319. u.bc_felt &= ~BC_CHAIN; 320. } 321. /* Pick up glyph at new position. */ 322. u.cglyph = (ballx != chainx || bally != chainy) 323. ? levl[chainx][chainy].glyph 324. : u.bglyph; 325. 326. movobj(uchain, chainx, chainy); 327. } 328. 329. u.bc_order = bc_order(); /* reset the order */ 330. } 331. 332. } else { 333. /* 334. * The hero is not blind. To make this work correctly, we need to 335. * pick up the ball and chain before the hero moves, then put them 336. * in their new positions after the hero moves. 337. */ 338. if (before) { 339. if (!control) { 340. /* 341. * Neither ball nor chain is moving, so remember which was 342. * on top until !before. Use the variable u.bc_order 343. * since it is only valid when blind. 344. */ 345. u.bc_order = bc_order(); 346. } 347. 348. remove_object(uchain); 349. newsym(uchain->ox, uchain->oy); 350. if (!carried(uball)) { 351. remove_object(uball); 352. newsym(uball->ox, uball->oy); 353. } 354. } else { 355. int on_floor = !carried(uball); 356. 357. if ((control & BC_CHAIN) 358. || (!control && u.bc_order == BCPOS_CHAIN)) { 359. /* If the chain moved or nothing moved & chain on top. */ 360. if (on_floor) 361. place_object(uball, ballx, bally); 362. place_object(uchain, chainx, chainy); /* chain on top */ 363. } else { 364. place_object(uchain, chainx, chainy); 365. if (on_floor) 366. place_object(uball, ballx, bally); 367. /* ball on top */ 368. } 369. newsym(chainx, chainy); 370. if (on_floor) 371. newsym(ballx, bally); 372. } 373. } 374. } 375. 376. /* return TRUE if the caller needs to place the ball and chain down again 377. * 378. * Should not be called while swallowed. Should be called before movement, 379. * because we might want to move the ball or chain to the hero's old 380. * position. 381. * 382. * It is called if we are moving. It is also called if we are teleporting 383. * *if* the ball doesn't move and we thus must drag the chain. It is not 384. * called for ordinary teleportation. 385. * 386. * allow_drag is only used in the ugly special case where teleporting must 387. * drag the chain, while an identical-looking movement must drag both the ball 388. * and chain. 389. */ 390. boolean 391. drag_ball(x, y, bc_control, ballx, bally, chainx, chainy, cause_delay, 392. allow_drag) 393. xchar x, y; 394. int *bc_control; 395. xchar *ballx, *bally, *chainx, *chainy; 396. boolean *cause_delay; 397. boolean allow_drag; 398. { 399. struct trap *t = (struct trap *) 0; 400. boolean already_in_rock; 401. 402. *ballx = uball->ox; 403. *bally = uball->oy; 404. *chainx = uchain->ox; 405. *chainy = uchain->oy; 406. *bc_control = 0; 407. *cause_delay = FALSE; 408. 409. if (dist2(x, y, uchain->ox, uchain->oy) <= 2) { /* nothing moved */ 410. move_bc(1, *bc_control, *ballx, *bally, *chainx, *chainy); 411. return TRUE; 412. } 413. 414. /* only need to move the chain? */ 415. if (carried(uball) || distmin(x, y, uball->ox, uball->oy) <= 2) { 416. xchar oldchainx = uchain->ox, oldchainy = uchain->oy; 417. 418. *bc_control = BC_CHAIN; 419. move_bc(1, *bc_control, *ballx, *bally, *chainx, *chainy); 420. if (carried(uball)) { 421. /* move chain only if necessary */ 422. if (distmin(x, y, uchain->ox, uchain->oy) > 1) { 423. *chainx = u.ux; 424. *chainy = u.uy; 425. } 426. return TRUE; 427. } 428. #define CHAIN_IN_MIDDLE(chx, chy) \ 429. (distmin(x, y, chx, chy) <= 1 \ 430. && distmin(chx, chy, uball->ox, uball->oy) <= 1) 431. #define IS_CHAIN_ROCK(x, y) \ 432. (IS_ROCK(levl[x][y].typ) \ 433. || (IS_DOOR(levl[x][y].typ) \ 434. && (levl[x][y].doormask & (D_CLOSED | D_LOCKED)))) 435. /* Don't ever move the chain into solid rock. If we have to, then instead 436. * undo the move_bc() and jump to the drag ball code. Note that this also 437. * means the "cannot carry and drag" message will not appear, since unless we 438. * moved at least two squares there is no possibility of the chain position 439. * being in solid rock. 440. */ 441. #define SKIP_TO_DRAG \ 442. { \ 443. *chainx = oldchainx; \ 444. *chainy = oldchainy; \ 445. move_bc(0, *bc_control, *ballx, *bally, *chainx, *chainy); \ 446. goto drag; \ 447. } 448. if (IS_CHAIN_ROCK(u.ux, u.uy) || IS_CHAIN_ROCK(*chainx, *chainy) 449. || IS_CHAIN_ROCK(uball->ox, uball->oy)) 450. already_in_rock = TRUE; 451. else 452. already_in_rock = FALSE; 453. 454. switch (dist2(x, y, uball->ox, uball->oy)) { 455. /* two spaces diagonal from ball, move chain inbetween */ 456. case 8: 457. *chainx = (uball->ox + x) / 2; 458. *chainy = (uball->oy + y) / 2; 459. if (IS_CHAIN_ROCK(*chainx, *chainy) && !already_in_rock) 460. SKIP_TO_DRAG; 461. break; 462. 463. /* player is distance 2/1 from ball; move chain to one of the 464. * two spaces between 465. * @ 466. * __ 467. * 0 468. */ 469. case 5: { 470. xchar tempx, tempy, tempx2, tempy2; 471. 472. /* find position closest to current position of chain */ 473. /* no effect if current position is already OK */ 474. if (abs(x - uball->ox) == 1) { 475. tempx = x; 476. tempx2 = uball->ox; 477. tempy = tempy2 = (uball->oy + y) / 2; 478. } else { 479. tempx = tempx2 = (uball->ox + x) / 2; 480. tempy = y; 481. tempy2 = uball->oy; 482. } 483. if (IS_CHAIN_ROCK(tempx, tempy) && !IS_CHAIN_ROCK(tempx2, tempy2) 484. && !already_in_rock) { 485. if (allow_drag) { 486. /* Avoid pathological case *if* not teleporting: 487. * 0 0_ 488. * _X move northeast -----> X@ 489. * @ 490. */ 491. if (dist2(u.ux, u.uy, uball->ox, uball->oy) == 5 492. && dist2(x, y, tempx, tempy) == 1) 493. SKIP_TO_DRAG; 494. /* Avoid pathological case *if* not teleporting: 495. * 0 0 496. * _X move east -----> X_ 497. * @ @ 498. */ 499. if (dist2(u.ux, u.uy, uball->ox, uball->oy) == 4 500. && dist2(x, y, tempx, tempy) == 2) 501. SKIP_TO_DRAG; 502. } 503. *chainx = tempx2; 504. *chainy = tempy2; 505. } else if (!IS_CHAIN_ROCK(tempx, tempy) 506. && IS_CHAIN_ROCK(tempx2, tempy2) && !already_in_rock) { 507. if (allow_drag) { 508. if (dist2(u.ux, u.uy, uball->ox, uball->oy) == 5 509. && dist2(x, y, tempx2, tempy2) == 1) 510. SKIP_TO_DRAG; 511. if (dist2(u.ux, u.uy, uball->ox, uball->oy) == 4 512. && dist2(x, y, tempx2, tempy2) == 2) 513. SKIP_TO_DRAG; 514. } 515. *chainx = tempx; 516. *chainy = tempy; 517. } else if (IS_CHAIN_ROCK(tempx, tempy) 518. && IS_CHAIN_ROCK(tempx2, tempy2) && !already_in_rock) { 519. SKIP_TO_DRAG; 520. } else if (dist2(tempx, tempy, uchain->ox, uchain->oy) 521. < dist2(tempx2, tempy2, uchain->ox, uchain->oy) 522. || ((dist2(tempx, tempy, uchain->ox, uchain->oy) 523. == dist2(tempx2, tempy2, uchain->ox, uchain->oy)) 524. && rn2(2))) { 525. *chainx = tempx; 526. *chainy = tempy; 527. } else { 528. *chainx = tempx2; 529. *chainy = tempy2; 530. } 531. break; 532. } 533. 534. /* ball is two spaces horizontal or vertical from player; move*/ 535. /* chain inbetween *unless* current chain position is OK */ 536. case 4: 537. if (CHAIN_IN_MIDDLE(uchain->ox, uchain->oy)) 538. break; 539. *chainx = (x + uball->ox) / 2; 540. *chainy = (y + uball->oy) / 2; 541. if (IS_CHAIN_ROCK(*chainx, *chainy) && !already_in_rock) 542. SKIP_TO_DRAG; 543. break; 544. 545. /* ball is one space diagonal from player. Check for the 546. * following special case: 547. * @ 548. * _ moving southwest becomes @_ 549. * 0 0 550. * (This will also catch teleporting that happens to resemble 551. * this case, but oh well.) Otherwise fall through. 552. */ 553. case 2: 554. if (dist2(x, y, uball->ox, uball->oy) == 2 555. && dist2(x, y, uchain->ox, uchain->oy) == 4) { 556. if (uchain->oy == y) 557. *chainx = uball->ox; 558. else 559. *chainy = uball->oy; 560. if (IS_CHAIN_ROCK(*chainx, *chainy) && !already_in_rock) 561. SKIP_TO_DRAG; 562. break; 563. } 564. /* fall through */ 565. case 1: 566. case 0: 567. /* do nothing if possible */ 568. if (CHAIN_IN_MIDDLE(uchain->ox, uchain->oy)) 569. break; 570. /* otherwise try to drag chain to player's old position */ 571. if (CHAIN_IN_MIDDLE(u.ux, u.uy)) { 572. *chainx = u.ux; 573. *chainy = u.uy; 574. break; 575. } 576. /* otherwise use player's new position (they must have 577. teleported, for this to happen) */ 578. *chainx = x; 579. *chainy = y; 580. break; 581. 582. default: 583. impossible("bad chain movement"); 584. break; 585. } 586. #undef SKIP_TO_DRAG 587. #undef CHAIN_IN_MIDDLE 588. return TRUE; 589. } 590. 591. drag: 592. 593. if (near_capacity() > SLT_ENCUMBER && dist2(x, y, u.ux, u.uy) <= 2) { 594. You("cannot %sdrag the heavy iron ball.", 595. invent ? "carry all that and also " : ""); 596. nomul(0); 597. return FALSE; 598. } 599. 600. if ((is_pool(uchain->ox, uchain->oy) 601. /* water not mere continuation of previous water */ 602. && (levl[uchain->ox][uchain->oy].typ == POOL 603. || !is_pool(uball->ox, uball->oy) 604. || levl[uball->ox][uball->oy].typ == POOL)) 605. || ((t = t_at(uchain->ox, uchain->oy)) 606. && (is_pit(t->ttyp) || is_hole(t->ttyp)))) { 607. if (Levitation) { 608. You_feel("a tug from the iron ball."); 609. if (t) 610. t->tseen = 1; 611. } else { 612. struct monst *victim; 613. 614. You("are jerked back by the iron ball!"); 615. if ((victim = m_at(uchain->ox, uchain->oy)) != 0) { 616. int tmp; 617. int dieroll = rnd(20); 618. 619. tmp = -2 + Luck + find_mac(victim); 620. tmp += omon_adj(victim, uball, TRUE); 621. 622. if (tmp >= dieroll) 623. (void) hmon(victim, uball, HMON_DRAGGED, dieroll); 624. else 625. miss(xname(uball), victim); 626. 627. } /* now check again in case mon died */ 628. if (!m_at(uchain->ox, uchain->oy)) { 629. u.ux = uchain->ox; 630. u.uy = uchain->oy; 631. newsym(u.ux0, u.uy0); 632. } 633. nomul(0); 634. 635. *bc_control = BC_BALL; 636. move_bc(1, *bc_control, *ballx, *bally, *chainx, *chainy); 637. *ballx = uchain->ox; 638. *bally = uchain->oy; 639. move_bc(0, *bc_control, *ballx, *bally, *chainx, *chainy); 640. spoteffects(TRUE); 641. return FALSE; 642. } 643. } 644. 645. *bc_control = BC_BALL | BC_CHAIN; 646. 647. move_bc(1, *bc_control, *ballx, *bally, *chainx, *chainy); 648. if (dist2(x, y, u.ux, u.uy) > 2) { 649. /* Awful case: we're still in range of the ball, so we thought we 650. * could only move the chain, but it turned out that the target 651. * square for the chain was rock, so we had to drag it instead. 652. * But we can't drag it either, because we teleported and are more 653. * than one square from our old position. Revert to the teleport 654. * behavior. 655. */ 656. *ballx = *chainx = x; 657. *bally = *chainy = y; 658. } else { 659. xchar newchainx = u.ux, newchainy = u.uy; 660. 661. /* 662. * Generally, chain moves to hero's previous location and ball 663. * moves to chain's previous location, except that we try to 664. * keep the chain directly between the hero and the ball. But, 665. * take the simple approach if the hero's previous location or 666. * the potential between location is inaccessible. 667. */ 668. if (dist2(x, y, uchain->ox, uchain->oy) == 4 669. && !IS_CHAIN_ROCK(newchainx, newchainy)) { 670. newchainx = (x + uchain->ox) / 2; 671. newchainy = (y + uchain->oy) / 2; 672. if (IS_CHAIN_ROCK(newchainx, newchainy)) { 673. /* don't let chain move to inaccessible location */ 674. newchainx = u.ux; 675. newchainy = u.uy; 676. } 677. } 678. 679. *ballx = uchain->ox; 680. *bally = uchain->oy; 681. *chainx = newchainx; 682. *chainy = newchainy; 683. } 684. #undef IS_CHAIN_ROCK 685. *cause_delay = TRUE; 686. return TRUE; 687. } 688. 689. /* 690. * drop_ball() 691. * 692. * The punished hero drops or throws her iron ball. If the hero is 693. * blind, we must reset the order and glyph. Check for side effects. 694. * This routine expects the ball to be already placed. 695. * 696. * Should not be called while swallowed. 697. */ 698. void 699. drop_ball(x, y) 700. xchar x, y; 701. { 702. if (Blind) { 703. /* get the order */ 704. u.bc_order = bc_order(); 705. /* pick up glyph */ 706. u.bglyph = (u.bc_order) ? u.cglyph : levl[x][y].glyph; 707. } 708. 709. if (x != u.ux || y != u.uy) { 710. static const char *pullmsg = "The ball pulls you out of the %s!"; 711. struct trap *t; 712. long side; 713. 714. if (u.utrap 715. && u.utraptype != TT_INFLOOR && u.utraptype != TT_BURIEDBALL) { 716. switch (u.utraptype) { 717. case TT_PIT: 718. pline(pullmsg, "pit"); 719. break; 720. case TT_WEB: 721. pline(pullmsg, "web"); 722. pline_The("web is destroyed!"); 723. deltrap(t_at(u.ux, u.uy)); 724. break; 725. case TT_LAVA: 726. pline(pullmsg, hliquid("lava")); 727. break; 728. case TT_BEARTRAP: 729. side = rn2(3) ? LEFT_SIDE : RIGHT_SIDE; 730. pline(pullmsg, "bear trap"); 731. set_wounded_legs(side, rn1(1000, 500)); 732. if (!u.usteed) { 733. Your("%s %s is severely damaged.", 734. (side == LEFT_SIDE) ? "left" : "right", 735. body_part(LEG)); 736. losehp(Maybe_Half_Phys(2), 737. "leg damage from being pulled out of a bear trap", 738. KILLED_BY); 739. } 740. break; 741. } 742. reset_utrap(TRUE); 743. fill_pit(u.ux, u.uy); 744. } 745. 746. u.ux0 = u.ux; 747. u.uy0 = u.uy; 748. if (!Levitation && !MON_AT(x, y) && !u.utrap 749. && (is_pool(x, y) 750. || ((t = t_at(x, y)) 751. && (is_pit(t->ttyp) 752. || is_hole(t->ttyp))))) { 753. u.ux = x; 754. u.uy = y; 755. } else { 756. u.ux = x - u.dx; 757. u.uy = y - u.dy; 758. } 759. vision_full_recalc = 1; /* hero has moved, recalculate vision later */ 760. 761. if (Blind) { 762. /* drop glyph under the chain */ 763. if (u.bc_felt & BC_CHAIN) 764. levl[uchain->ox][uchain->oy].glyph = u.cglyph; 765. u.bc_felt = 0; /* feel nothing */ 766. /* pick up new glyph */ 767. u.cglyph = (u.bc_order) ? u.bglyph : levl[u.ux][u.uy].glyph; 768. } 769. movobj(uchain, u.ux, u.uy); /* has a newsym */ 770. if (Blind) { 771. u.bc_order = bc_order(); 772. } 773. newsym(u.ux0, u.uy0); /* clean up old position */ 774. if (u.ux0 != u.ux || u.uy0 != u.uy) { 775. spoteffects(TRUE); 776. sokoban_guilt(); 777. } 778. } 779. } 780. 781. /* ball&chain cause hero to randomly lose stuff from inventory */ 782. STATIC_OVL void 783. litter() 784. { 785. struct obj *otmp, *nextobj = 0; 786. int capacity = weight_cap(); 787. 788. for (otmp = invent; otmp; otmp = nextobj) { 789. nextobj = otmp->nobj; 790. if ((otmp != uball) && (rnd(capacity) <= (int) otmp->owt)) { 791. if (canletgo(otmp, "")) { 792. You("drop %s and %s %s down the stairs with you.", 793. yname(otmp), (otmp->quan == 1L) ? "it" : "they", 794. otense(otmp, "fall")); 795. dropx(otmp); 796. encumber_msg(); /* drop[xyz]() probably ought to to this... */ 797. } 798. } 799. } 800. } 801. 802. void 803. drag_down() 804. { 805. boolean forward; 806. uchar dragchance = 3; 807. 808. /* 809. * Assume that the ball falls forward if: 810. * 811. * a) the character is wielding it, or 812. * b) the character has both hands available to hold it (i.e. is 813. * not wielding any weapon), or 814. * c) (perhaps) it falls forward out of his non-weapon hand 815. */ 816. forward = carried(uball) && (uwep == uball || !uwep || !rn2(3)); 817. 818. if (carried(uball)) 819. You("lose your grip on the iron ball."); 820. 821. cls(); /* previous level is still displayed although you 822. went down the stairs. Avoids bug C343-20 */ 823. 824. if (forward) { 825. if (rn2(6)) { 826. pline_The("iron ball drags you downstairs!"); 827. losehp(Maybe_Half_Phys(rnd(6)), 828. "dragged downstairs by an iron ball", NO_KILLER_PREFIX); 829. litter(); 830. } 831. } else { 832. if (rn2(2)) { 833. pline_The("iron ball smacks into you!"); 834. losehp(Maybe_Half_Phys(rnd(20)), "iron ball collision", 835. KILLED_BY_AN); 836. exercise(A_STR, FALSE); 837. dragchance -= 2; 838. } 839. if ((int) dragchance >= rnd(6)) { 840. pline_The("iron ball drags you downstairs!"); 841. losehp(Maybe_Half_Phys(rnd(3)), 842. "dragged downstairs by an iron ball", NO_KILLER_PREFIX); 843. exercise(A_STR, FALSE); 844. litter(); 845. } 846. } 847. } 848. 849. void 850. bc_sanity_check() 851. { 852. int otyp; 853. unsigned save_nameknown; 854. const char *onam; 855. 856. if (Punished && (!uball || !uchain)) { 857. impossible("Punished without %s%s%s?", 858. !uball ? "iron ball" : "", 859. (!uball && !uchain) ? " and " : "", 860. !uchain ? "attached chain" : ""); 861. } else if (!Punished && (uball || uchain)) { 862. impossible("Attached %s%s%s without being Punished?", 863. uchain ? "chain" : "", 864. (uchain && uball) ? " and " : "", 865. uball ? "iron ball" : ""); 866. } 867. /* ball is free when swallowed, changing levels, other times? */ 868. if (uball && (uball->otyp != HEAVY_IRON_BALL 869. || (uball->where != OBJ_FLOOR 870. && uball->where != OBJ_INVENT 871. && uball->where != OBJ_FREE) 872. || (uball->owornmask & W_BALL) == 0L 873. || (uball->owornmask & ~(W_BALL | W_WEAPON)) != 0L)) { 874. otyp = uball->otyp; 875. if (otyp < STRANGE_OBJECT || otyp >= NUM_OBJECTS 876. || !OBJ_NAME(objects[otyp])) { 877. onam = "glorkum"; 878. } else { 879. save_nameknown = objects[otyp].oc_name_known; 880. objects[otyp].oc_name_known = 1; 881. onam = simple_typename(otyp); 882. objects[otyp].oc_name_known = save_nameknown; 883. } 884. impossible("uball: type %d (%s), where %d, wornmask=0x%08lx", 885. otyp, onam, uball->where, uball->owornmask); 886. } 887. /* similar check to ball except can't be in inventory */ 888. if (uchain && (uchain->otyp != IRON_CHAIN 889. || (uchain->where != OBJ_FLOOR 890. && uchain->where != OBJ_FREE) 891. /* [could simplify this to owornmask != W_CHAIN] */ 892. || (uchain->owornmask & W_CHAIN) == 0L 893. || (uchain->owornmask & ~W_CHAIN) != 0L)) { 894. otyp = uchain->otyp; 895. if (otyp < STRANGE_OBJECT || otyp >= NUM_OBJECTS 896. || !OBJ_NAME(objects[otyp])) { 897. onam = "glorkum"; 898. } else { 899. save_nameknown = objects[otyp].oc_name_known; 900. objects[otyp].oc_name_known = 1; 901. onam = simple_typename(otyp); 902. objects[otyp].oc_name_known = save_nameknown; 903. } 904. impossible("uchain: type %d (%s), where %d, wornmask=0x%08lx", 905. otyp, onam, uchain->where, uchain->owornmask); 906. } 907. /* [check bc_order too?] */ 908. } 909. 910. /*ball.c*/