Source:NetHack 3.6.4/src/lock.c
Jump to navigation
Jump to search
Below is the full text to lock.c from the source code of NetHack 3.6.4. To link to a particular line, write [[Source:NetHack 3.6.4/src/lock.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 lock.c $NHDT-Date: 1548978605 2019/01/31 23:50:05 $ $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.84 $ */ 2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ 3. /*-Copyright (c) Robert Patrick Rankin, 2011. */ 4. /* NetHack may be freely redistributed. See license for details. */ 5. 6. #include "hack.h" 7. 8. /* at most one of `door' and `box' should be non-null at any given time */ 9. STATIC_VAR NEARDATA struct xlock_s { 10. struct rm *door; 11. struct obj *box; 12. int picktyp, /* key|pick|card for unlock, sharp vs blunt for #force */ 13. chance, usedtime; 14. boolean magic_key; 15. } xlock; 16. 17. /* occupation callbacks */ 18. STATIC_PTR int NDECL(picklock); 19. STATIC_PTR int NDECL(forcelock); 20. 21. STATIC_DCL const char *NDECL(lock_action); 22. STATIC_DCL boolean FDECL(obstructed, (int, int, BOOLEAN_P)); 23. STATIC_DCL void FDECL(chest_shatter_msg, (struct obj *)); 24. 25. boolean 26. picking_lock(x, y) 27. int *x, *y; 28. { 29. if (occupation == picklock) { 30. *x = u.ux + u.dx; 31. *y = u.uy + u.dy; 32. return TRUE; 33. } else { 34. *x = *y = 0; 35. return FALSE; 36. } 37. } 38. 39. boolean 40. picking_at(x, y) 41. int x, y; 42. { 43. return (boolean) (occupation == picklock && xlock.door == &levl[x][y]); 44. } 45. 46. /* produce an occupation string appropriate for the current activity */ 47. STATIC_OVL const char * 48. lock_action() 49. { 50. /* "unlocking"+2 == "locking" */ 51. static const char *actions[] = { 52. "unlocking the door", /* [0] */ 53. "unlocking the chest", /* [1] */ 54. "unlocking the box", /* [2] */ 55. "picking the lock" /* [3] */ 56. }; 57. 58. /* if the target is currently unlocked, we're trying to lock it now */ 59. if (xlock.door && !(xlock.door->doormask & D_LOCKED)) 60. return actions[0] + 2; /* "locking the door" */ 61. else if (xlock.box && !xlock.box->olocked) 62. return xlock.box->otyp == CHEST ? actions[1] + 2 : actions[2] + 2; 63. /* otherwise we're trying to unlock it */ 64. else if (xlock.picktyp == LOCK_PICK) 65. return actions[3]; /* "picking the lock" */ 66. else if (xlock.picktyp == CREDIT_CARD) 67. return actions[3]; /* same as lock_pick */ 68. else if (xlock.door) 69. return actions[0]; /* "unlocking the door" */ 70. else if (xlock.box) 71. return xlock.box->otyp == CHEST ? actions[1] : actions[2]; 72. else 73. return actions[3]; 74. } 75. 76. /* try to open/close a lock */ 77. STATIC_PTR int 78. picklock(VOID_ARGS) 79. { 80. if (xlock.box) { 81. if (xlock.box->where != OBJ_FLOOR 82. || xlock.box->ox != u.ux || xlock.box->oy != u.uy) { 83. return ((xlock.usedtime = 0)); /* you or it moved */ 84. } 85. } else { /* door */ 86. if (xlock.door != &(levl[u.ux + u.dx][u.uy + u.dy])) { 87. return ((xlock.usedtime = 0)); /* you moved */ 88. } 89. switch (xlock.door->doormask) { 90. case D_NODOOR: 91. pline("This doorway has no door."); 92. return ((xlock.usedtime = 0)); 93. case D_ISOPEN: 94. You("cannot lock an open door."); 95. return ((xlock.usedtime = 0)); 96. case D_BROKEN: 97. pline("This door is broken."); 98. return ((xlock.usedtime = 0)); 99. } 100. } 101. 102. if (xlock.usedtime++ >= 50 || nohands(youmonst.data)) { 103. You("give up your attempt at %s.", lock_action()); 104. exercise(A_DEX, TRUE); /* even if you don't succeed */ 105. return ((xlock.usedtime = 0)); 106. } 107. 108. if (rn2(100) >= xlock.chance) 109. return 1; /* still busy */ 110. 111. /* using the Master Key of Thievery finds traps if its bless/curse 112. state is adequate (non-cursed for rogues, blessed for others; 113. checked when setting up 'xlock') */ 114. if ((!xlock.door ? (int) xlock.box->otrapped 115. : (xlock.door->doormask & D_TRAPPED) != 0) 116. && xlock.magic_key) { 117. xlock.chance += 20; /* less effort needed next time */ 118. /* unfortunately we don't have a 'tknown' flag to record 119. "known to be trapped" so declining to disarm and then 120. retrying lock manipulation will find it all over again */ 121. if (yn("You find a trap! Do you want to try to disarm it?") == 'y') { 122. const char *what; 123. boolean alreadyunlocked; 124. 125. /* disarming while using magic key always succeeds */ 126. if (xlock.door) { 127. xlock.door->doormask &= ~D_TRAPPED; 128. what = "door"; 129. alreadyunlocked = !(xlock.door->doormask & D_LOCKED); 130. } else { 131. xlock.box->otrapped = 0; 132. what = (xlock.box->otyp == CHEST) ? "chest" : "box"; 133. alreadyunlocked = !xlock.box->olocked; 134. } 135. You("succeed in disarming the trap. The %s is still %slocked.", 136. what, alreadyunlocked ? "un" : ""); 137. exercise(A_WIS, TRUE); 138. } else { 139. You("stop %s.", lock_action()); 140. exercise(A_WIS, FALSE); 141. } 142. return ((xlock.usedtime = 0)); 143. } 144. 145. You("succeed in %s.", lock_action()); 146. if (xlock.door) { 147. if (xlock.door->doormask & D_TRAPPED) { 148. b_trapped("door", FINGER); 149. xlock.door->doormask = D_NODOOR; 150. unblock_point(u.ux + u.dx, u.uy + u.dy); 151. if (*in_rooms(u.ux + u.dx, u.uy + u.dy, SHOPBASE)) 152. add_damage(u.ux + u.dx, u.uy + u.dy, SHOP_DOOR_COST); 153. newsym(u.ux + u.dx, u.uy + u.dy); 154. } else if (xlock.door->doormask & D_LOCKED) 155. xlock.door->doormask = D_CLOSED; 156. else 157. xlock.door->doormask = D_LOCKED; 158. } else { 159. xlock.box->olocked = !xlock.box->olocked; 160. xlock.box->lknown = 1; 161. if (xlock.box->otrapped) 162. (void) chest_trap(xlock.box, FINGER, FALSE); 163. } 164. exercise(A_DEX, TRUE); 165. return ((xlock.usedtime = 0)); 166. } 167. 168. void 169. breakchestlock(box, destroyit) 170. struct obj *box; 171. boolean destroyit; 172. { 173. if (!destroyit) { /* bill for the box but not for its contents */ 174. struct obj *hide_contents = box->cobj; 175. 176. box->cobj = 0; 177. costly_alteration(box, COST_BRKLCK); 178. box->cobj = hide_contents; 179. box->olocked = 0; 180. box->obroken = 1; 181. box->lknown = 1; 182. } else { /* #force has destroyed this box (at <u.ux,u.uy>) */ 183. struct obj *otmp; 184. struct monst *shkp = (*u.ushops && costly_spot(u.ux, u.uy)) 185. ? shop_keeper(*u.ushops) 186. : 0; 187. boolean costly = (boolean) (shkp != 0), 188. peaceful_shk = costly && (boolean) shkp->mpeaceful; 189. long loss = 0L; 190. 191. pline("In fact, you've totally destroyed %s.", the(xname(box))); 192. /* Put the contents on ground at the hero's feet. */ 193. while ((otmp = box->cobj) != 0) { 194. obj_extract_self(otmp); 195. if (!rn2(3) || otmp->oclass == POTION_CLASS) { 196. chest_shatter_msg(otmp); 197. if (costly) 198. loss += stolen_value(otmp, u.ux, u.uy, peaceful_shk, TRUE); 199. if (otmp->quan == 1L) { 200. obfree(otmp, (struct obj *) 0); 201. continue; 202. } 203. /* this works because we're sure to have at least 1 left; 204. otherwise it would fail since otmp is not in inventory */ 205. useup(otmp); 206. } 207. if (box->otyp == ICE_BOX && otmp->otyp == CORPSE) { 208. otmp->age = monstermoves - otmp->age; /* actual age */ 209. start_corpse_timeout(otmp); 210. } 211. place_object(otmp, u.ux, u.uy); 212. stackobj(otmp); 213. } 214. if (costly) 215. loss += stolen_value(box, u.ux, u.uy, peaceful_shk, TRUE); 216. if (loss) 217. You("owe %ld %s for objects destroyed.", loss, currency(loss)); 218. delobj(box); 219. } 220. } 221. 222. /* try to force a locked chest */ 223. STATIC_PTR int 224. forcelock(VOID_ARGS) 225. { 226. if ((xlock.box->ox != u.ux) || (xlock.box->oy != u.uy)) 227. return ((xlock.usedtime = 0)); /* you or it moved */ 228. 229. if (xlock.usedtime++ >= 50 || !uwep || nohands(youmonst.data)) { 230. You("give up your attempt to force the lock."); 231. if (xlock.usedtime >= 50) /* you made the effort */ 232. exercise((xlock.picktyp) ? A_DEX : A_STR, TRUE); 233. return ((xlock.usedtime = 0)); 234. } 235. 236. if (xlock.picktyp) { /* blade */ 237. if (rn2(1000 - (int) uwep->spe) > (992 - greatest_erosion(uwep) * 10) 238. && !uwep->cursed && !obj_resists(uwep, 0, 99)) { 239. /* for a +0 weapon, probability that it survives an unsuccessful 240. * attempt to force the lock is (.992)^50 = .67 241. */ 242. pline("%sour %s broke!", (uwep->quan > 1L) ? "One of y" : "Y", 243. xname(uwep)); 244. useup(uwep); 245. You("give up your attempt to force the lock."); 246. exercise(A_DEX, TRUE); 247. return ((xlock.usedtime = 0)); 248. } 249. } else /* blunt */ 250. wake_nearby(); /* due to hammering on the container */ 251. 252. if (rn2(100) >= xlock.chance) 253. return 1; /* still busy */ 254. 255. You("succeed in forcing the lock."); 256. exercise(xlock.picktyp ? A_DEX : A_STR, TRUE); 257. /* breakchestlock() might destroy xlock.box; if so, xlock context will 258. be cleared (delobj -> obfree -> maybe_reset_pick); but it might not, 259. so explicitly clear that manually */ 260. breakchestlock(xlock.box, (boolean) (!xlock.picktyp && !rn2(3))); 261. reset_pick(); /* lock-picking context is no longer valid */ 262. 263. return 0; 264. } 265. 266. void 267. reset_pick() 268. { 269. xlock.usedtime = xlock.chance = xlock.picktyp = 0; 270. xlock.magic_key = FALSE; 271. xlock.door = (struct rm *) 0; 272. xlock.box = (struct obj *) 0; 273. } 274. 275. /* level change or object deletion; context may no longer be valid */ 276. void 277. maybe_reset_pick(container) 278. struct obj *container; /* passed from obfree() */ 279. { 280. /* 281. * If a specific container, only clear context if it is for that 282. * particular container (which is being deleted). Other stuff on 283. * the current dungeon level remains valid. 284. * However if 'container' is Null, clear context if not carrying 285. * xlock.box (which might be Null if context is for a door). 286. * Used for changing levels, where a floor container or a door is 287. * being left behind and won't be valid on the new level but a 288. * carried container will still be. There might not be any context, 289. * in which case redundantly clearing it is harmless. 290. */ 291. if (container ? (container == xlock.box) 292. : (!xlock.box || !carried(xlock.box))) 293. reset_pick(); 294. } 295. 296. /* for doapply(); if player gives a direction or resumes an interrupted 297. previous attempt then it costs hero a move even if nothing ultimately 298. happens; when told "can't do that" before being asked for direction 299. or player cancels with ESC while giving direction, it doesn't */ 300. #define PICKLOCK_LEARNED_SOMETHING (-1) /* time passes */ 301. #define PICKLOCK_DID_NOTHING 0 /* no time passes */ 302. #define PICKLOCK_DID_SOMETHING 1 303. 304. /* player is applying a key, lock pick, or credit card */ 305. int 306. pick_lock(pick) 307. struct obj *pick; 308. { 309. int picktyp, c, ch; 310. coord cc; 311. struct rm *door; 312. struct obj *otmp; 313. char qbuf[QBUFSZ]; 314. 315. picktyp = pick->otyp; 316. 317. /* check whether we're resuming an interrupted previous attempt */ 318. if (xlock.usedtime && picktyp == xlock.picktyp) { 319. static char no_longer[] = "Unfortunately, you can no longer %s %s."; 320. 321. if (nohands(youmonst.data)) { 322. const char *what = (picktyp == LOCK_PICK) ? "pick" : "key"; 323. 324. if (picktyp == CREDIT_CARD) 325. what = "card"; 326. pline(no_longer, "hold the", what); 327. reset_pick(); 328. return PICKLOCK_LEARNED_SOMETHING; 329. } else if (u.uswallow || (xlock.box && !can_reach_floor(TRUE))) { 330. pline(no_longer, "reach the", "lock"); 331. reset_pick(); 332. return PICKLOCK_LEARNED_SOMETHING; 333. } else { 334. const char *action = lock_action(); 335. 336. You("resume your attempt at %s.", action); 337. xlock.magic_key = is_magic_key(&youmonst, pick); 338. set_occupation(picklock, action, 0); 339. return PICKLOCK_DID_SOMETHING; 340. } 341. } 342. 343. if (nohands(youmonst.data)) { 344. You_cant("hold %s -- you have no hands!", doname(pick)); 345. return PICKLOCK_DID_NOTHING; 346. } else if (u.uswallow) { 347. You_cant("%sunlock %s.", (picktyp == CREDIT_CARD) ? "" : "lock or ", 348. mon_nam(u.ustuck)); 349. return PICKLOCK_DID_NOTHING; 350. } 351. 352. if (picktyp != LOCK_PICK 353. && picktyp != CREDIT_CARD 354. && picktyp != SKELETON_KEY) { 355. impossible("picking lock with object %d?", picktyp); 356. return PICKLOCK_DID_NOTHING; 357. } 358. ch = 0; /* lint suppression */ 359. 360. if (!get_adjacent_loc((char *) 0, "Invalid location!", u.ux, u.uy, &cc)) 361. return PICKLOCK_DID_NOTHING; 362. 363. if (cc.x == u.ux && cc.y == u.uy) { /* pick lock on a container */ 364. const char *verb; 365. char qsfx[QBUFSZ]; 366. boolean it; 367. int count; 368. 369. if (u.dz < 0) { 370. There("isn't any sort of lock up %s.", 371. Levitation ? "here" : "there"); 372. return PICKLOCK_LEARNED_SOMETHING; 373. } else if (is_lava(u.ux, u.uy)) { 374. pline("Doing that would probably melt %s.", yname(pick)); 375. return PICKLOCK_LEARNED_SOMETHING; 376. } else if (is_pool(u.ux, u.uy) && !Underwater) { 377. pline_The("%s has no lock.", hliquid("water")); 378. return PICKLOCK_LEARNED_SOMETHING; 379. } 380. 381. count = 0; 382. c = 'n'; /* in case there are no boxes here */ 383. for (otmp = level.objects[cc.x][cc.y]; otmp; otmp = otmp->nexthere) 384. if (Is_box(otmp)) { 385. ++count; 386. if (!can_reach_floor(TRUE)) { 387. You_cant("reach %s from up here.", the(xname(otmp))); 388. return PICKLOCK_LEARNED_SOMETHING; 389. } 390. it = 0; 391. if (otmp->obroken) 392. verb = "fix"; 393. else if (!otmp->olocked) 394. verb = "lock", it = 1; 395. else if (picktyp != LOCK_PICK) 396. verb = "unlock", it = 1; 397. else 398. verb = "pick"; 399. 400. /* "There is <a box> here; <verb> <it|its lock>?" */ 401. Sprintf(qsfx, " here; %s %s?", verb, it ? "it" : "its lock"); 402. (void) safe_qbuf(qbuf, "There is ", qsfx, otmp, doname, 403. ansimpleoname, "a box"); 404. otmp->lknown = 1; 405. 406. c = ynq(qbuf); 407. if (c == 'q') 408. return 0; 409. if (c == 'n') 410. continue; 411. 412. if (otmp->obroken) { 413. You_cant("fix its broken lock with %s.", doname(pick)); 414. return PICKLOCK_LEARNED_SOMETHING; 415. } else if (picktyp == CREDIT_CARD && !otmp->olocked) { 416. /* credit cards are only good for unlocking */ 417. You_cant("do that with %s.", 418. an(simple_typename(picktyp))); 419. return PICKLOCK_LEARNED_SOMETHING; 420. } 421. switch (picktyp) { 422. case CREDIT_CARD: 423. ch = ACURR(A_DEX) + 20 * Role_if(PM_ROGUE); 424. break; 425. case LOCK_PICK: 426. ch = 4 * ACURR(A_DEX) + 25 * Role_if(PM_ROGUE); 427. break; 428. case SKELETON_KEY: 429. ch = 75 + ACURR(A_DEX); 430. break; 431. default: 432. ch = 0; 433. } 434. if (otmp->cursed) 435. ch /= 2; 436. 437. xlock.box = otmp; 438. xlock.door = 0; 439. break; 440. } 441. if (c != 'y') { 442. if (!count) 443. There("doesn't seem to be any sort of lock here."); 444. return PICKLOCK_LEARNED_SOMETHING; /* decided against all boxes */ 445. } 446. } else { /* pick the lock in a door */ 447. struct monst *mtmp; 448. 449. if (u.utrap && u.utraptype == TT_PIT) { 450. You_cant("reach over the edge of the pit."); 451. return PICKLOCK_LEARNED_SOMETHING; 452. } 453. 454. door = &levl[cc.x][cc.y]; 455. mtmp = m_at(cc.x, cc.y); 456. if (mtmp && canseemon(mtmp) && M_AP_TYPE(mtmp) != M_AP_FURNITURE 457. && M_AP_TYPE(mtmp) != M_AP_OBJECT) { 458. if (picktyp == CREDIT_CARD 459. && (mtmp->isshk || mtmp->data == &mons[PM_ORACLE])) 460. verbalize("No checks, no credit, no problem."); 461. else 462. pline("I don't think %s would appreciate that.", 463. mon_nam(mtmp)); 464. return PICKLOCK_LEARNED_SOMETHING; 465. } else if (mtmp && is_door_mappear(mtmp)) { 466. /* "The door actually was a <mimic>!" */ 467. stumble_onto_mimic(mtmp); 468. /* mimic might keep the key (50% chance, 10% for PYEC or MKoT) */ 469. maybe_absorb_item(mtmp, pick, 50, 10); 470. return PICKLOCK_LEARNED_SOMETHING; 471. } 472. if (!IS_DOOR(door->typ)) { 473. if (is_drawbridge_wall(cc.x, cc.y) >= 0) 474. You("%s no lock on the drawbridge.", Blind ? "feel" : "see"); 475. else 476. You("%s no door there.", Blind ? "feel" : "see"); 477. return PICKLOCK_LEARNED_SOMETHING; 478. } 479. switch (door->doormask) { 480. case D_NODOOR: 481. pline("This doorway has no door."); 482. return PICKLOCK_LEARNED_SOMETHING; 483. case D_ISOPEN: 484. You("cannot lock an open door."); 485. return PICKLOCK_LEARNED_SOMETHING; 486. case D_BROKEN: 487. pline("This door is broken."); 488. return PICKLOCK_LEARNED_SOMETHING; 489. default: 490. /* credit cards are only good for unlocking */ 491. if (picktyp == CREDIT_CARD && !(door->doormask & D_LOCKED)) { 492. You_cant("lock a door with a credit card."); 493. return PICKLOCK_LEARNED_SOMETHING; 494. } 495. 496. Sprintf(qbuf, "%s it?", 497. (door->doormask & D_LOCKED) ? "Unlock" : "Lock"); 498. 499. c = yn(qbuf); 500. if (c == 'n') 501. return 0; 502. 503. switch (picktyp) { 504. case CREDIT_CARD: 505. ch = 2 * ACURR(A_DEX) + 20 * Role_if(PM_ROGUE); 506. break; 507. case LOCK_PICK: 508. ch = 3 * ACURR(A_DEX) + 30 * Role_if(PM_ROGUE); 509. break; 510. case SKELETON_KEY: 511. ch = 70 + ACURR(A_DEX); 512. break; 513. default: 514. ch = 0; 515. } 516. xlock.door = door; 517. xlock.box = 0; 518. } 519. } 520. context.move = 0; 521. xlock.chance = ch; 522. xlock.picktyp = picktyp; 523. xlock.magic_key = is_magic_key(&youmonst, pick); 524. xlock.usedtime = 0; 525. set_occupation(picklock, lock_action(), 0); 526. return PICKLOCK_DID_SOMETHING; 527. } 528. 529. /* try to force a chest with your weapon */ 530. int 531. doforce() 532. { 533. register struct obj *otmp; 534. register int c, picktyp; 535. char qbuf[QBUFSZ]; 536. 537. if (u.uswallow) { 538. You_cant("force anything from inside here."); 539. return 0; 540. } 541. if (!uwep /* proper type test */ 542. || ((uwep->oclass == WEAPON_CLASS || is_weptool(uwep)) 543. ? (objects[uwep->otyp].oc_skill < P_DAGGER 544. || objects[uwep->otyp].oc_skill == P_FLAIL 545. || objects[uwep->otyp].oc_skill > P_LANCE) 546. : uwep->oclass != ROCK_CLASS)) { 547. You_cant("force anything %s weapon.", 548. !uwep ? "when not wielding a" 549. : (uwep->oclass != WEAPON_CLASS && !is_weptool(uwep)) 550. ? "without a proper" 551. : "with that"); 552. return 0; 553. } 554. if (!can_reach_floor(TRUE)) { 555. cant_reach_floor(u.ux, u.uy, FALSE, TRUE); 556. return 0; 557. } 558. 559. picktyp = is_blade(uwep) && !is_pick(uwep); 560. if (xlock.usedtime && xlock.box && picktyp == xlock.picktyp) { 561. You("resume your attempt to force the lock."); 562. set_occupation(forcelock, "forcing the lock", 0); 563. return 1; 564. } 565. 566. /* A lock is made only for the honest man, the thief will break it. */ 567. xlock.box = (struct obj *) 0; 568. for (otmp = level.objects[u.ux][u.uy]; otmp; otmp = otmp->nexthere) 569. if (Is_box(otmp)) { 570. if (otmp->obroken || !otmp->olocked) { 571. /* force doname() to omit known "broken" or "unlocked" 572. prefix so that the message isn't worded redundantly; 573. since we're about to set lknown, there's no need to 574. remember and then reset its current value */ 575. otmp->lknown = 0; 576. There("is %s here, but its lock is already %s.", 577. doname(otmp), otmp->obroken ? "broken" : "unlocked"); 578. otmp->lknown = 1; 579. continue; 580. } 581. (void) safe_qbuf(qbuf, "There is ", " here; force its lock?", 582. otmp, doname, ansimpleoname, "a box"); 583. otmp->lknown = 1; 584. 585. c = ynq(qbuf); 586. if (c == 'q') 587. return 0; 588. if (c == 'n') 589. continue; 590. 591. if (picktyp) 592. You("force %s into a crack and pry.", yname(uwep)); 593. else 594. You("start bashing it with %s.", yname(uwep)); 595. xlock.box = otmp; 596. xlock.chance = objects[uwep->otyp].oc_wldam * 2; 597. xlock.picktyp = picktyp; 598. xlock.magic_key = FALSE; 599. xlock.usedtime = 0; 600. break; 601. } 602. 603. if (xlock.box) 604. set_occupation(forcelock, "forcing the lock", 0); 605. else 606. You("decide not to force the issue."); 607. return 1; 608. } 609. 610. boolean 611. stumble_on_door_mimic(x, y) 612. int x, y; 613. { 614. struct monst *mtmp; 615. 616. if ((mtmp = m_at(x, y)) && is_door_mappear(mtmp) 617. && !Protection_from_shape_changers) { 618. stumble_onto_mimic(mtmp); 619. return TRUE; 620. } 621. return FALSE; 622. } 623. 624. /* the 'O' command - try to open a door */ 625. int 626. doopen() 627. { 628. return doopen_indir(0, 0); 629. } 630. 631. /* try to open a door in direction u.dx/u.dy */ 632. int 633. doopen_indir(x, y) 634. int x, y; 635. { 636. coord cc; 637. register struct rm *door; 638. boolean portcullis; 639. int res = 0; 640. 641. if (nohands(youmonst.data)) { 642. You_cant("open anything -- you have no hands!"); 643. return 0; 644. } 645. 646. if (u.utrap && u.utraptype == TT_PIT) { 647. You_cant("reach over the edge of the pit."); 648. return 0; 649. } 650. 651. if (x > 0 && y > 0) { 652. cc.x = x; 653. cc.y = y; 654. } else if (!get_adjacent_loc((char *) 0, (char *) 0, u.ux, u.uy, &cc)) 655. return 0; 656. 657. /* open at yourself/up/down */ 658. if ((cc.x == u.ux) && (cc.y == u.uy)) 659. return doloot(); 660. 661. if (stumble_on_door_mimic(cc.x, cc.y)) 662. return 1; 663. 664. /* when choosing a direction is impaired, use a turn 665. regardless of whether a door is successfully targetted */ 666. if (Confusion || Stunned) 667. res = 1; 668. 669. door = &levl[cc.x][cc.y]; 670. portcullis = (is_drawbridge_wall(cc.x, cc.y) >= 0); 671. if (Blind) { 672. int oldglyph = door->glyph; 673. schar oldlastseentyp = lastseentyp[cc.x][cc.y]; 674. 675. feel_location(cc.x, cc.y); 676. if (door->glyph != oldglyph 677. || lastseentyp[cc.x][cc.y] != oldlastseentyp) 678. res = 1; /* learned something */ 679. } 680. 681. if (portcullis || !IS_DOOR(door->typ)) { 682. /* closed portcullis or spot that opened bridge would span */ 683. if (is_db_wall(cc.x, cc.y) || door->typ == DRAWBRIDGE_UP) 684. There("is no obvious way to open the drawbridge."); 685. else if (portcullis || door->typ == DRAWBRIDGE_DOWN) 686. pline_The("drawbridge is already open."); 687. else if (container_at(cc.x, cc.y, TRUE)) 688. pline("%s like something lootable over there.", 689. Blind ? "Feels" : "Seems"); 690. else 691. You("%s no door there.", Blind ? "feel" : "see"); 692. return res; 693. } 694. 695. if (!(door->doormask & D_CLOSED)) { 696. const char *mesg; 697. 698. switch (door->doormask) { 699. case D_BROKEN: 700. mesg = " is broken"; 701. break; 702. case D_NODOOR: 703. mesg = "way has no door"; 704. break; 705. case D_ISOPEN: 706. mesg = " is already open"; 707. break; 708. default: 709. mesg = " is locked"; 710. break; 711. } 712. pline("This door%s.", mesg); 713. return res; 714. } 715. 716. if (verysmall(youmonst.data)) { 717. pline("You're too small to pull the door open."); 718. return res; 719. } 720. 721. /* door is known to be CLOSED */ 722. if (rnl(20) < (ACURRSTR + ACURR(A_DEX) + ACURR(A_CON)) / 3) { 723. pline_The("door opens."); 724. if (door->doormask & D_TRAPPED) { 725. b_trapped("door", FINGER); 726. door->doormask = D_NODOOR; 727. if (*in_rooms(cc.x, cc.y, SHOPBASE)) 728. add_damage(cc.x, cc.y, SHOP_DOOR_COST); 729. } else 730. door->doormask = D_ISOPEN; 731. feel_newsym(cc.x, cc.y); /* the hero knows she opened it */ 732. unblock_point(cc.x, cc.y); /* vision: new see through there */ 733. } else { 734. exercise(A_STR, TRUE); 735. pline_The("door resists!"); 736. } 737. 738. return 1; 739. } 740. 741. STATIC_OVL boolean 742. obstructed(x, y, quietly) 743. register int x, y; 744. boolean quietly; 745. { 746. register struct monst *mtmp = m_at(x, y); 747. 748. if (mtmp && M_AP_TYPE(mtmp) != M_AP_FURNITURE) { 749. if (M_AP_TYPE(mtmp) == M_AP_OBJECT) 750. goto objhere; 751. if (!quietly) { 752. if ((mtmp->mx != x) || (mtmp->my != y)) { 753. /* worm tail */ 754. pline("%s%s blocks the way!", 755. !canspotmon(mtmp) ? Something : s_suffix(Monnam(mtmp)), 756. !canspotmon(mtmp) ? "" : " tail"); 757. } else { 758. pline("%s blocks the way!", 759. !canspotmon(mtmp) ? "Some creature" : Monnam(mtmp)); 760. } 761. } 762. if (!canspotmon(mtmp)) 763. map_invisible(x, y); 764. return TRUE; 765. } 766. if (OBJ_AT(x, y)) { 767. objhere: 768. if (!quietly) 769. pline("%s's in the way.", Something); 770. return TRUE; 771. } 772. return FALSE; 773. } 774. 775. /* the 'C' command - try to close a door */ 776. int 777. doclose() 778. { 779. register int x, y; 780. register struct rm *door; 781. boolean portcullis; 782. int res = 0; 783. 784. if (nohands(youmonst.data)) { 785. You_cant("close anything -- you have no hands!"); 786. return 0; 787. } 788. 789. if (u.utrap && u.utraptype == TT_PIT) { 790. You_cant("reach over the edge of the pit."); 791. return 0; 792. } 793. 794. if (!getdir((char *) 0)) 795. return 0; 796. 797. x = u.ux + u.dx; 798. y = u.uy + u.dy; 799. if ((x == u.ux) && (y == u.uy)) { 800. You("are in the way!"); 801. return 1; 802. } 803. 804. if (!isok(x, y)) 805. goto nodoor; 806. 807. if (stumble_on_door_mimic(x, y)) 808. return 1; 809. 810. /* when choosing a direction is impaired, use a turn 811. regardless of whether a door is successfully targetted */ 812. if (Confusion || Stunned) 813. res = 1; 814. 815. door = &levl[x][y]; 816. portcullis = (is_drawbridge_wall(x, y) >= 0); 817. if (Blind) { 818. int oldglyph = door->glyph; 819. schar oldlastseentyp = lastseentyp[x][y]; 820. 821. feel_location(x, y); 822. if (door->glyph != oldglyph || lastseentyp[x][y] != oldlastseentyp) 823. res = 1; /* learned something */ 824. } 825. 826. if (portcullis || !IS_DOOR(door->typ)) { 827. /* is_db_wall: closed portcullis */ 828. if (is_db_wall(x, y) || door->typ == DRAWBRIDGE_UP) 829. pline_The("drawbridge is already closed."); 830. else if (portcullis || door->typ == DRAWBRIDGE_DOWN) 831. There("is no obvious way to close the drawbridge."); 832. else { 833. nodoor: 834. You("%s no door there.", Blind ? "feel" : "see"); 835. } 836. return res; 837. } 838. 839. if (door->doormask == D_NODOOR) { 840. pline("This doorway has no door."); 841. return res; 842. } else if (obstructed(x, y, FALSE)) { 843. return res; 844. } else if (door->doormask == D_BROKEN) { 845. pline("This door is broken."); 846. return res; 847. } else if (door->doormask & (D_CLOSED | D_LOCKED)) { 848. pline("This door is already closed."); 849. return res; 850. } 851. 852. if (door->doormask == D_ISOPEN) { 853. if (verysmall(youmonst.data) && !u.usteed) { 854. pline("You're too small to push the door closed."); 855. return res; 856. } 857. if (u.usteed 858. || rn2(25) < (ACURRSTR + ACURR(A_DEX) + ACURR(A_CON)) / 3) { 859. pline_The("door closes."); 860. door->doormask = D_CLOSED; 861. feel_newsym(x, y); /* the hero knows she closed it */ 862. block_point(x, y); /* vision: no longer see there */ 863. } else { 864. exercise(A_STR, TRUE); 865. pline_The("door resists!"); 866. } 867. } 868. 869. return 1; 870. } 871. 872. /* box obj was hit with spell or wand effect otmp; 873. returns true if something happened */ 874. boolean 875. boxlock(obj, otmp) 876. struct obj *obj, *otmp; /* obj *is* a box */ 877. { 878. boolean res = 0; 879. 880. switch (otmp->otyp) { 881. case WAN_LOCKING: 882. case SPE_WIZARD_LOCK: 883. if (!obj->olocked) { /* lock it; fix if broken */ 884. pline("Klunk!"); 885. obj->olocked = 1; 886. obj->obroken = 0; 887. if (Role_if(PM_WIZARD)) 888. obj->lknown = 1; 889. else 890. obj->lknown = 0; 891. res = 1; 892. } /* else already closed and locked */ 893. break; 894. case WAN_OPENING: 895. case SPE_KNOCK: 896. if (obj->olocked) { /* unlock; couldn't be broken */ 897. pline("Klick!"); 898. obj->olocked = 0; 899. res = 1; 900. if (Role_if(PM_WIZARD)) 901. obj->lknown = 1; 902. else 903. obj->lknown = 0; 904. } else /* silently fix if broken */ 905. obj->obroken = 0; 906. break; 907. case WAN_POLYMORPH: 908. case SPE_POLYMORPH: 909. /* maybe start unlocking chest, get interrupted, then zap it; 910. we must avoid any attempt to resume unlocking it */ 911. if (xlock.box == obj) 912. reset_pick(); 913. break; 914. } 915. return res; 916. } 917. 918. /* Door/secret door was hit with spell or wand effect otmp; 919. returns true if something happened */ 920. boolean 921. doorlock(otmp, x, y) 922. struct obj *otmp; 923. int x, y; 924. { 925. register struct rm *door = &levl[x][y]; 926. boolean res = TRUE; 927. int loudness = 0; 928. const char *msg = (const char *) 0; 929. const char *dustcloud = "A cloud of dust"; 930. const char *quickly_dissipates = "quickly dissipates"; 931. boolean mysterywand = (otmp->oclass == WAND_CLASS && !otmp->dknown); 932. 933. if (door->typ == SDOOR) { 934. switch (otmp->otyp) { 935. case WAN_OPENING: 936. case SPE_KNOCK: 937. case WAN_STRIKING: 938. case SPE_FORCE_BOLT: 939. door->typ = DOOR; 940. door->doormask = D_CLOSED | (door->doormask & D_TRAPPED); 941. newsym(x, y); 942. if (cansee(x, y)) 943. pline("A door appears in the wall!"); 944. if (otmp->otyp == WAN_OPENING || otmp->otyp == SPE_KNOCK) 945. return TRUE; 946. break; /* striking: continue door handling below */ 947. case WAN_LOCKING: 948. case SPE_WIZARD_LOCK: 949. default: 950. return FALSE; 951. } 952. } 953. 954. switch (otmp->otyp) { 955. case WAN_LOCKING: 956. case SPE_WIZARD_LOCK: 957. if (Is_rogue_level(&u.uz)) { 958. boolean vis = cansee(x, y); 959. /* Can't have real locking in Rogue, so just hide doorway */ 960. if (vis) 961. pline("%s springs up in the older, more primitive doorway.", 962. dustcloud); 963. else 964. You_hear("a swoosh."); 965. if (obstructed(x, y, mysterywand)) { 966. if (vis) 967. pline_The("cloud %s.", quickly_dissipates); 968. return FALSE; 969. } 970. block_point(x, y); 971. door->typ = SDOOR, door->doormask = D_NODOOR; 972. if (vis) 973. pline_The("doorway vanishes!"); 974. newsym(x, y); 975. return TRUE; 976. } 977. if (obstructed(x, y, mysterywand)) 978. return FALSE; 979. /* Don't allow doors to close over traps. This is for pits */ 980. /* & trap doors, but is it ever OK for anything else? */ 981. if (t_at(x, y)) { 982. /* maketrap() clears doormask, so it should be NODOOR */ 983. pline("%s springs up in the doorway, but %s.", dustcloud, 984. quickly_dissipates); 985. return FALSE; 986. } 987. 988. switch (door->doormask & ~D_TRAPPED) { 989. case D_CLOSED: 990. msg = "The door locks!"; 991. break; 992. case D_ISOPEN: 993. msg = "The door swings shut, and locks!"; 994. break; 995. case D_BROKEN: 996. msg = "The broken door reassembles and locks!"; 997. break; 998. case D_NODOOR: 999. msg = 1000. "A cloud of dust springs up and assembles itself into a door!"; 1001. break; 1002. default: 1003. res = FALSE; 1004. break; 1005. } 1006. block_point(x, y); 1007. door->doormask = D_LOCKED | (door->doormask & D_TRAPPED); 1008. newsym(x, y); 1009. break; 1010. case WAN_OPENING: 1011. case SPE_KNOCK: 1012. if (door->doormask & D_LOCKED) { 1013. msg = "The door unlocks!"; 1014. door->doormask = D_CLOSED | (door->doormask & D_TRAPPED); 1015. } else 1016. res = FALSE; 1017. break; 1018. case WAN_STRIKING: 1019. case SPE_FORCE_BOLT: 1020. if (door->doormask & (D_LOCKED | D_CLOSED)) { 1021. if (door->doormask & D_TRAPPED) { 1022. if (MON_AT(x, y)) 1023. (void) mb_trapped(m_at(x, y)); 1024. else if (flags.verbose) { 1025. if (cansee(x, y)) 1026. pline("KABOOM!! You see a door explode."); 1027. else 1028. You_hear("a distant explosion."); 1029. } 1030. door->doormask = D_NODOOR; 1031. unblock_point(x, y); 1032. newsym(x, y); 1033. loudness = 40; 1034. break; 1035. } 1036. door->doormask = D_BROKEN; 1037. if (flags.verbose) { 1038. if (cansee(x, y)) 1039. pline_The("door crashes open!"); 1040. else 1041. You_hear("a crashing sound."); 1042. } 1043. unblock_point(x, y); 1044. newsym(x, y); 1045. /* force vision recalc before printing more messages */ 1046. if (vision_full_recalc) 1047. vision_recalc(0); 1048. loudness = 20; 1049. } else 1050. res = FALSE; 1051. break; 1052. default: 1053. impossible("magic (%d) attempted on door.", otmp->otyp); 1054. break; 1055. } 1056. if (msg && cansee(x, y)) 1057. pline1(msg); 1058. if (loudness > 0) { 1059. /* door was destroyed */ 1060. wake_nearto(x, y, loudness); 1061. if (*in_rooms(x, y, SHOPBASE)) 1062. add_damage(x, y, 0L); 1063. } 1064. 1065. if (res && picking_at(x, y)) { 1066. /* maybe unseen monster zaps door you're unlocking */ 1067. stop_occupation(); 1068. reset_pick(); 1069. } 1070. return res; 1071. } 1072. 1073. STATIC_OVL void 1074. chest_shatter_msg(otmp) 1075. struct obj *otmp; 1076. { 1077. const char *disposition; 1078. const char *thing; 1079. long save_Blinded; 1080. 1081. if (otmp->oclass == POTION_CLASS) { 1082. You("%s %s shatter!", Blind ? "hear" : "see", an(bottlename())); 1083. if (!breathless(youmonst.data) || haseyes(youmonst.data)) 1084. potionbreathe(otmp); 1085. return; 1086. } 1087. /* We have functions for distant and singular names, but not one */ 1088. /* which does _both_... */ 1089. save_Blinded = Blinded; 1090. Blinded = 1; 1091. thing = singular(otmp, xname); 1092. Blinded = save_Blinded; 1093. switch (objects[otmp->otyp].oc_material) { 1094. case PAPER: 1095. disposition = "is torn to shreds"; 1096. break; 1097. case WAX: 1098. disposition = "is crushed"; 1099. break; 1100. case VEGGY: 1101. disposition = "is pulped"; 1102. break; 1103. case FLESH: 1104. disposition = "is mashed"; 1105. break; 1106. case GLASS: 1107. disposition = "shatters"; 1108. break; 1109. case WOOD: 1110. disposition = "splinters to fragments"; 1111. break; 1112. default: 1113. disposition = "is destroyed"; 1114. break; 1115. } 1116. pline("%s %s!", An(thing), disposition); 1117. } 1118. 1119. /*lock.c*/