hack.trap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /* $NetBSD: hack.trap.c,v 1.7 2003/04/02 18:36:41 jsm Exp $ */
  2. /*
  3. * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
  4. * Amsterdam
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * - Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. *
  14. * - Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * - Neither the name of the Stichting Centrum voor Wiskunde en
  19. * Informatica, nor the names of its contributors may be used to endorse or
  20. * promote products derived from this software without specific prior
  21. * written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  24. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  25. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  26. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  27. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  30. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  31. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. /*
  36. * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
  37. * All rights reserved.
  38. *
  39. * Redistribution and use in source and binary forms, with or without
  40. * modification, are permitted provided that the following conditions
  41. * are met:
  42. * 1. Redistributions of source code must retain the above copyright
  43. * notice, this list of conditions and the following disclaimer.
  44. * 2. Redistributions in binary form must reproduce the above copyright
  45. * notice, this list of conditions and the following disclaimer in the
  46. * documentation and/or other materials provided with the distribution.
  47. * 3. The name of the author may not be used to endorse or promote products
  48. * derived from this software without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  51. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  52. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  53. * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  54. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  55. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  56. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  57. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  58. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  59. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  60. */
  61. #include <sys/cdefs.h>
  62. #ifndef lint
  63. __RCSID("$NetBSD: hack.trap.c,v 1.7 2003/04/02 18:36:41 jsm Exp $");
  64. #endif /* not lint */
  65. #include <stdlib.h>
  66. #include "hack.h"
  67. #include "extern.h"
  68. #include "def.mkroom.h"
  69. const char vowels[] = "aeiou";
  70. const char *const traps[] = {
  71. " bear trap",
  72. "n arrow trap",
  73. " dart trap",
  74. " trapdoor",
  75. " teleportation trap",
  76. " pit",
  77. " sleeping gas trap",
  78. " piercer",
  79. " mimic"
  80. };
  81. struct trap *
  82. maketrap(x, y, typ)
  83. int x, y, typ;
  84. {
  85. struct trap *ttmp;
  86. ttmp = newtrap();
  87. ttmp->ttyp = typ;
  88. ttmp->tseen = 0;
  89. ttmp->once = 0;
  90. ttmp->tx = x;
  91. ttmp->ty = y;
  92. ttmp->ntrap = ftrap;
  93. ftrap = ttmp;
  94. return (ttmp);
  95. }
  96. void
  97. dotrap(trap)
  98. struct trap *trap;
  99. {
  100. int ttype = trap->ttyp;
  101. nomul(0);
  102. if (trap->tseen && !rn2(5) && ttype != PIT)
  103. pline("You escape a%s.", traps[ttype]);
  104. else {
  105. trap->tseen = 1;
  106. switch (ttype) {
  107. case SLP_GAS_TRAP:
  108. pline("A cloud of gas puts you to sleep!");
  109. nomul(-rnd(25));
  110. break;
  111. case BEAR_TRAP:
  112. if (Levitation) {
  113. pline("You float over a bear trap.");
  114. break;
  115. }
  116. u.utrap = 4 + rn2(4);
  117. u.utraptype = TT_BEARTRAP;
  118. pline("A bear trap closes on your foot!");
  119. break;
  120. case PIERC:
  121. deltrap(trap);
  122. if (makemon(PM_PIERCER, u.ux, u.uy)) {
  123. pline("A piercer suddenly drops from the ceiling!");
  124. if (uarmh)
  125. pline("Its blow glances off your helmet.");
  126. else
  127. (void) thitu(3, d(4, 6), "falling piercer");
  128. }
  129. break;
  130. case ARROW_TRAP:
  131. pline("An arrow shoots out at you!");
  132. if (!thitu(8, rnd(6), "arrow")) {
  133. mksobj_at(ARROW, u.ux, u.uy);
  134. fobj->quan = 1;
  135. }
  136. break;
  137. case TRAPDOOR:
  138. if (!xdnstair) {
  139. pline("A trap door in the ceiling opens and a rock falls on your head!");
  140. if (uarmh)
  141. pline("Fortunately, you are wearing a helmet!");
  142. losehp(uarmh ? 2 : d(2, 10), "falling rock");
  143. mksobj_at(ROCK, u.ux, u.uy);
  144. fobj->quan = 1;
  145. stackobj(fobj);
  146. if (Invisible)
  147. newsym(u.ux, u.uy);
  148. } else {
  149. int newlevel = dlevel + 1;
  150. while (!rn2(4) && newlevel < 29)
  151. newlevel++;
  152. pline("A trap door opens up under you!");
  153. if (Levitation || u.ustuck) {
  154. pline("For some reason you don't fall in.");
  155. break;
  156. }
  157. goto_level(newlevel, FALSE);
  158. }
  159. break;
  160. case DART_TRAP:
  161. pline("A little dart shoots out at you!");
  162. if (thitu(7, rnd(3), "little dart")) {
  163. if (!rn2(6))
  164. poisoned("dart", "poison dart");
  165. } else {
  166. mksobj_at(DART, u.ux, u.uy);
  167. fobj->quan = 1;
  168. }
  169. break;
  170. case TELEP_TRAP:
  171. if (trap->once) {
  172. deltrap(trap);
  173. newsym(u.ux, u.uy);
  174. vtele();
  175. } else {
  176. newsym(u.ux, u.uy);
  177. tele();
  178. }
  179. break;
  180. case PIT:
  181. if (Levitation) {
  182. pline("A pit opens up under you!");
  183. pline("You don't fall in!");
  184. break;
  185. }
  186. pline("You fall into a pit!");
  187. u.utrap = rn1(6, 2);
  188. u.utraptype = TT_PIT;
  189. losehp(rnd(6), "fall into a pit");
  190. selftouch("Falling, you");
  191. break;
  192. default:
  193. impossible("You hit a trap of type %u", trap->ttyp);
  194. }
  195. }
  196. }
  197. int
  198. mintrap(mtmp)
  199. struct monst *mtmp;
  200. {
  201. struct trap *trap = t_at(mtmp->mx, mtmp->my);
  202. int wasintrap = mtmp->mtrapped;
  203. if (!trap) {
  204. mtmp->mtrapped = 0; /* perhaps teleported? */
  205. } else if (wasintrap) {
  206. if (!rn2(40))
  207. mtmp->mtrapped = 0;
  208. } else {
  209. int tt = trap->ttyp;
  210. int in_sight = cansee(mtmp->mx, mtmp->my);
  211. if (mtmp->mtrapseen & (1 << tt)) {
  212. /* he has been in such a trap - perhaps he escapes */
  213. if (rn2(4))
  214. return (0);
  215. }
  216. mtmp->mtrapseen |= (1 << tt);
  217. switch (tt) {
  218. case BEAR_TRAP:
  219. if (strchr(mlarge, mtmp->data->mlet)) {
  220. if (in_sight)
  221. pline("%s is caught in a bear trap!",
  222. Monnam(mtmp));
  223. else if (mtmp->data->mlet == 'o')
  224. pline("You hear the roaring of an angry bear!");
  225. mtmp->mtrapped = 1;
  226. }
  227. break;
  228. case PIT:
  229. /* there should be a mtmp/data -> floating */
  230. if (!strchr("EywBfk'& ", mtmp->data->mlet)) { /* ab */
  231. mtmp->mtrapped = 1;
  232. if (in_sight)
  233. pline("%s falls in a pit!", Monnam(mtmp));
  234. }
  235. break;
  236. case SLP_GAS_TRAP:
  237. if (!mtmp->msleep && !mtmp->mfroz) {
  238. mtmp->msleep = 1;
  239. if (in_sight)
  240. pline("%s suddenly falls asleep!",
  241. Monnam(mtmp));
  242. }
  243. break;
  244. case TELEP_TRAP:
  245. rloc(mtmp);
  246. if (in_sight && !cansee(mtmp->mx, mtmp->my))
  247. pline("%s suddenly disappears!",
  248. Monnam(mtmp));
  249. break;
  250. case ARROW_TRAP:
  251. if (in_sight) {
  252. pline("%s is hit by an arrow!",
  253. Monnam(mtmp));
  254. }
  255. mtmp->mhp -= 3;
  256. break;
  257. case DART_TRAP:
  258. if (in_sight) {
  259. pline("%s is hit by a dart!",
  260. Monnam(mtmp));
  261. }
  262. mtmp->mhp -= 2;
  263. /* not mondied here !! */
  264. break;
  265. case TRAPDOOR:
  266. if (!xdnstair) {
  267. mtmp->mhp -= 10;
  268. if (in_sight)
  269. pline("A trap door in the ceiling opens and a rock hits %s!", monnam(mtmp));
  270. break;
  271. }
  272. if (mtmp->data->mlet != 'w') {
  273. fall_down(mtmp);
  274. if (in_sight)
  275. pline("Suddenly, %s disappears out of sight.", monnam(mtmp));
  276. return (2); /* no longer on this level */
  277. }
  278. break;
  279. case PIERC:
  280. break;
  281. default:
  282. impossible("Some monster encountered a strange trap.");
  283. }
  284. }
  285. return (mtmp->mtrapped);
  286. }
  287. void
  288. selftouch(arg)
  289. const char *arg;
  290. {
  291. if (uwep && uwep->otyp == DEAD_COCKATRICE) {
  292. pline("%s touch the dead cockatrice.", arg);
  293. pline("You turn to stone.");
  294. killer = objects[uwep->otyp].oc_name;
  295. done("died");
  296. }
  297. }
  298. void
  299. float_up()
  300. {
  301. if (u.utrap) {
  302. if (u.utraptype == TT_PIT) {
  303. u.utrap = 0;
  304. pline("You float up, out of the pit!");
  305. } else {
  306. pline("You float up, only your leg is still stuck.");
  307. }
  308. } else
  309. pline("You start to float in the air!");
  310. }
  311. void
  312. float_down()
  313. {
  314. struct trap *trap;
  315. pline("You float gently to the ground.");
  316. if ((trap = t_at(u.ux, u.uy)) != NULL)
  317. switch (trap->ttyp) {
  318. case PIERC:
  319. break;
  320. case TRAPDOOR:
  321. if (!xdnstair || u.ustuck)
  322. break;
  323. /* fall into next case */
  324. default:
  325. dotrap(trap);
  326. }
  327. pickup(1);
  328. }
  329. void
  330. vtele()
  331. {
  332. struct mkroom *croom;
  333. for (croom = &rooms[0]; croom->hx >= 0; croom++)
  334. if (croom->rtype == VAULT) {
  335. int x, y;
  336. x = rn2(2) ? croom->lx : croom->hx;
  337. y = rn2(2) ? croom->ly : croom->hy;
  338. if (teleok(x, y)) {
  339. teleds(x, y);
  340. return;
  341. }
  342. }
  343. tele();
  344. }
  345. void
  346. tele()
  347. {
  348. coord cc;
  349. int nux, nuy;
  350. if (Teleport_control) {
  351. pline("To what position do you want to be teleported?");
  352. cc = getpos(1, "the desired position"); /* 1: force valid */
  353. /*
  354. * possible extensions: introduce a small error if magic
  355. * power is low; allow transfer to solid rock
  356. */
  357. if (teleok(cc.x, cc.y)) {
  358. teleds(cc.x, cc.y);
  359. return;
  360. }
  361. pline("Sorry ...");
  362. }
  363. do {
  364. nux = rnd(COLNO - 1);
  365. nuy = rn2(ROWNO);
  366. } while (!teleok(nux, nuy));
  367. teleds(nux, nuy);
  368. }
  369. void
  370. teleds(nux, nuy)
  371. int nux, nuy;
  372. {
  373. if (Punished)
  374. unplacebc();
  375. unsee();
  376. u.utrap = 0;
  377. u.ustuck = 0;
  378. u.ux = nux;
  379. u.uy = nuy;
  380. setsee();
  381. if (Punished)
  382. placebc(1);
  383. if (u.uswallow) {
  384. u.uswldtim = u.uswallow = 0;
  385. docrt();
  386. }
  387. nomul(0);
  388. if (levl[nux][nuy].typ == POOL && !Levitation)
  389. drown();
  390. (void) inshop();
  391. pickup(1);
  392. if (!Blind)
  393. read_engr_at(u.ux, u.uy);
  394. }
  395. int
  396. teleok(x, y)
  397. int x, y;
  398. { /* might throw him into a POOL */
  399. return (isok(x, y) && !IS_ROCK(levl[x][y].typ) && !m_at(x, y) &&
  400. !sobj_at(ENORMOUS_ROCK, x, y) && !t_at(x, y)
  401. );
  402. /* Note: gold is permitted (because of vaults) */
  403. }
  404. int
  405. dotele()
  406. {
  407. if (
  408. #ifdef WIZARD
  409. !wizard &&
  410. #endif /* WIZARD */
  411. (!Teleportation || u.ulevel < 6 ||
  412. (pl_character[0] != 'W' && u.ulevel < 10))) {
  413. pline("You are not able to teleport at will.");
  414. return (0);
  415. }
  416. if (u.uhunger <= 100 || u.ustr < 6) {
  417. pline("You miss the strength for a teleport spell.");
  418. return (1);
  419. }
  420. tele();
  421. morehungry(100);
  422. return (1);
  423. }
  424. void
  425. placebc(attach)
  426. int attach;
  427. {
  428. if (!uchain || !uball) {
  429. impossible("Where are your chain and ball??");
  430. return;
  431. }
  432. uball->ox = uchain->ox = u.ux;
  433. uball->oy = uchain->oy = u.uy;
  434. if (attach) {
  435. uchain->nobj = fobj;
  436. fobj = uchain;
  437. if (!carried(uball)) {
  438. uball->nobj = fobj;
  439. fobj = uball;
  440. }
  441. }
  442. }
  443. void
  444. unplacebc()
  445. {
  446. if (!carried(uball)) {
  447. freeobj(uball);
  448. unpobj(uball);
  449. }
  450. freeobj(uchain);
  451. unpobj(uchain);
  452. }
  453. void
  454. level_tele()
  455. {
  456. int newlevel;
  457. if (Teleport_control) {
  458. char buf[BUFSZ];
  459. do {
  460. pline("To what level do you want to teleport? [type a number] ");
  461. getlin(buf);
  462. } while (!digit(buf[0]) && (buf[0] != '-' || !digit(buf[1])));
  463. newlevel = atoi(buf);
  464. } else {
  465. newlevel = 5 + rn2(20); /* 5 - 24 */
  466. if (dlevel == newlevel) {
  467. if (!xdnstair)
  468. newlevel--;
  469. else
  470. newlevel++;
  471. }
  472. }
  473. if (newlevel >= 30) {
  474. if (newlevel > MAXLEVEL)
  475. newlevel = MAXLEVEL;
  476. pline("You arrive at the center of the earth ...");
  477. pline("Unfortunately it is here that hell is located.");
  478. if (Fire_resistance) {
  479. pline("But the fire doesn't seem to harm you.");
  480. } else {
  481. pline("You burn to a crisp.");
  482. dlevel = maxdlevel = newlevel;
  483. killer = "visit to the hell";
  484. done("burned");
  485. }
  486. }
  487. if (newlevel < 0) {
  488. newlevel = 0;
  489. pline("You are now high above the clouds ...");
  490. if (Levitation) {
  491. pline("You float gently down to earth.");
  492. done("escaped");
  493. }
  494. pline("Unfortunately, you don't know how to fly.");
  495. pline("You fall down a few thousand feet and break your neck.");
  496. dlevel = 0;
  497. killer = "fall";
  498. done("died");
  499. }
  500. goto_level(newlevel, FALSE); /* calls done("escaped") if
  501. * newlevel==0 */
  502. }
  503. void
  504. drown()
  505. {
  506. pline("You fall into a pool!");
  507. pline("You can't swim!");
  508. if (rn2(3) < u.uluck + 2) {
  509. /* most scrolls become unreadable */
  510. struct obj *obj;
  511. for (obj = invent; obj; obj = obj->nobj)
  512. if (obj->olet == SCROLL_SYM && rn2(12) > u.uluck)
  513. obj->otyp = SCR_BLANK_PAPER;
  514. /* we should perhaps merge these scrolls ? */
  515. pline("You attempt a teleport spell."); /* utcsri!carroll */
  516. (void) dotele();
  517. if (levl[u.ux][u.uy].typ != POOL)
  518. return;
  519. }
  520. pline("You drown ...");
  521. killer = "pool of water";
  522. done("drowned");
  523. }