hack.apply.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /* $NetBSD: hack.apply.c,v 1.8 2004/01/27 20:30:29 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.apply.c,v 1.8 2004/01/27 20:30:29 jsm Exp $");
  64. #endif /* not lint */
  65. #include "hack.h"
  66. #include "extern.h"
  67. #include "def.edog.h"
  68. #include "def.mkroom.h"
  69. static void use_camera(struct obj *);
  70. static int in_ice_box(struct obj *);
  71. static int ck_ice_box(struct obj *);
  72. static int out_ice_box(struct obj *);
  73. static void use_ice_box(struct obj *);
  74. static struct monst *bchit(int, int , int , int);
  75. static void use_whistle(struct obj *);
  76. static void use_magic_whistle(struct obj *);
  77. static int dig(void);
  78. static int use_pick_axe(struct obj *);
  79. int
  80. doapply()
  81. {
  82. struct obj *obj;
  83. int res = 1;
  84. obj = getobj("(", "use or apply");
  85. if (!obj)
  86. return (0);
  87. switch (obj->otyp) {
  88. case EXPENSIVE_CAMERA:
  89. use_camera(obj);
  90. break;
  91. case ICE_BOX:
  92. use_ice_box(obj);
  93. break;
  94. case PICK_AXE:
  95. res = use_pick_axe(obj);
  96. break;
  97. case MAGIC_WHISTLE:
  98. if (pl_character[0] == 'W' || u.ulevel > 9) {
  99. use_magic_whistle(obj);
  100. break;
  101. }
  102. /* fall into next case */
  103. case WHISTLE:
  104. use_whistle(obj);
  105. break;
  106. case CAN_OPENER:
  107. if (!carrying(TIN)) {
  108. pline("You have no can to open.");
  109. goto xit;
  110. }
  111. pline("You cannot open a tin without eating its contents.");
  112. pline("In order to eat, use the 'e' command.");
  113. if (obj != uwep)
  114. pline("Opening the tin will be much easier if you wield the can-opener.");
  115. goto xit;
  116. default:
  117. pline("Sorry, I don't know how to use that.");
  118. xit:
  119. nomul(0);
  120. return (0);
  121. }
  122. nomul(0);
  123. return (res);
  124. }
  125. /* ARGSUSED */
  126. static void
  127. use_camera(obj) /* */
  128. struct obj *obj __attribute__((__unused__));
  129. {
  130. struct monst *mtmp;
  131. if (!getdir(1)) { /* ask: in what direction? */
  132. flags.move = multi = 0;
  133. return;
  134. }
  135. if (u.uswallow) {
  136. pline("You take a picture of %s's stomach.", monnam(u.ustuck));
  137. return;
  138. }
  139. if (u.dz) {
  140. pline("You take a picture of the %s.",
  141. (u.dz > 0) ? "floor" : "ceiling");
  142. return;
  143. }
  144. if ((mtmp = bchit(u.dx, u.dy, COLNO, '!')) != NULL) {
  145. if (mtmp->msleep) {
  146. mtmp->msleep = 0;
  147. pline("The flash awakens %s.", monnam(mtmp)); /* a3 */
  148. } else if (mtmp->data->mlet != 'y')
  149. if (mtmp->mcansee || mtmp->mblinded) {
  150. int tmp = dist(mtmp->mx, mtmp->my);
  151. int tmp2;
  152. if (cansee(mtmp->mx, mtmp->my))
  153. pline("%s is blinded by the flash!", Monnam(mtmp));
  154. setmangry(mtmp);
  155. if (tmp < 9 && !mtmp->isshk && rn2(4)) {
  156. mtmp->mflee = 1;
  157. if (rn2(4))
  158. mtmp->mfleetim = rnd(100);
  159. }
  160. if (tmp < 3)
  161. mtmp->mcansee = mtmp->mblinded = 0;
  162. else {
  163. tmp2 = mtmp->mblinded;
  164. tmp2 += rnd(1 + 50 / tmp);
  165. if (tmp2 > 127)
  166. tmp2 = 127;
  167. mtmp->mblinded = tmp2;
  168. mtmp->mcansee = 0;
  169. }
  170. }
  171. }
  172. }
  173. static
  174. struct obj *current_ice_box;/* a local variable of use_ice_box, to be
  175. * used by its local procedures in/ck_ice_box */
  176. static int
  177. in_ice_box(obj)
  178. struct obj *obj;
  179. {
  180. if (obj == current_ice_box ||
  181. (Punished && (obj == uball || obj == uchain))) {
  182. pline("You must be kidding.");
  183. return (0);
  184. }
  185. if (obj->owornmask & (W_ARMOR | W_RING)) {
  186. pline("You cannot refrigerate something you are wearing.");
  187. return (0);
  188. }
  189. if (obj->owt + current_ice_box->owt > 70) {
  190. pline("It won't fit.");
  191. return (1); /* be careful! */
  192. }
  193. if (obj == uwep) {
  194. if (uwep->cursed) {
  195. pline("Your weapon is welded to your hand!");
  196. return (0);
  197. }
  198. setuwep((struct obj *) 0);
  199. }
  200. current_ice_box->owt += obj->owt;
  201. freeinv(obj);
  202. obj->o_cnt_id = current_ice_box->o_id;
  203. obj->nobj = fcobj;
  204. fcobj = obj;
  205. obj->age = moves - obj->age; /* actual age */
  206. return (1);
  207. }
  208. static int
  209. ck_ice_box(obj)
  210. struct obj *obj;
  211. {
  212. return (obj->o_cnt_id == current_ice_box->o_id);
  213. }
  214. static int
  215. out_ice_box(obj)
  216. struct obj *obj;
  217. {
  218. struct obj *otmp;
  219. if (obj == fcobj)
  220. fcobj = fcobj->nobj;
  221. else {
  222. for (otmp = fcobj; otmp->nobj != obj; otmp = otmp->nobj)
  223. if (!otmp->nobj)
  224. panic("out_ice_box");
  225. otmp->nobj = obj->nobj;
  226. }
  227. current_ice_box->owt -= obj->owt;
  228. obj->age = moves - obj->age; /* simulated point of time */
  229. (void) addinv(obj);
  230. return 0;
  231. }
  232. static void
  233. use_ice_box(obj)
  234. struct obj *obj;
  235. {
  236. int cnt = 0;
  237. struct obj *otmp;
  238. current_ice_box = obj; /* for use by in/out_ice_box */
  239. for (otmp = fcobj; otmp; otmp = otmp->nobj)
  240. if (otmp->o_cnt_id == obj->o_id)
  241. cnt++;
  242. if (!cnt)
  243. pline("Your ice-box is empty.");
  244. else {
  245. pline("Do you want to take something out of the ice-box? [yn] ");
  246. if (readchar() == 'y')
  247. if (askchain(fcobj, (char *) 0, 0, out_ice_box, ck_ice_box, 0))
  248. return;
  249. pline("That was all. Do you wish to put something in? [yn] ");
  250. if (readchar() != 'y')
  251. return;
  252. }
  253. /* call getobj: 0: allow cnt; #: allow all types; %: expect food */
  254. otmp = getobj("0#%", "put in");
  255. if (!otmp || !in_ice_box(otmp))
  256. flags.move = multi = 0;
  257. }
  258. static struct monst *
  259. bchit(ddx, ddy, range, sym)
  260. int ddx, ddy, range;
  261. char sym;
  262. {
  263. struct monst *mtmp = (struct monst *) 0;
  264. int bchx = u.ux, bchy = u.uy;
  265. if (sym)
  266. Tmp_at(-1, sym);/* open call */
  267. while (range--) {
  268. bchx += ddx;
  269. bchy += ddy;
  270. if ((mtmp = m_at(bchx, bchy)) != NULL)
  271. break;
  272. if (!ZAP_POS(levl[bchx][bchy].typ)) {
  273. bchx -= ddx;
  274. bchy -= ddy;
  275. break;
  276. }
  277. if (sym)
  278. Tmp_at(bchx, bchy);
  279. }
  280. if (sym)
  281. Tmp_at(-1, -1);
  282. return (mtmp);
  283. }
  284. /* ARGSUSED */
  285. static void
  286. use_whistle(obj)
  287. struct obj *obj __attribute__((__unused__));
  288. {
  289. struct monst *mtmp = fmon;
  290. pline("You produce a high whistling sound.");
  291. while (mtmp) {
  292. if (dist(mtmp->mx, mtmp->my) < u.ulevel * 20) {
  293. if (mtmp->msleep)
  294. mtmp->msleep = 0;
  295. if (mtmp->mtame)
  296. EDOG(mtmp)->whistletime = moves;
  297. }
  298. mtmp = mtmp->nmon;
  299. }
  300. }
  301. /* ARGSUSED */
  302. static void
  303. use_magic_whistle(obj)
  304. struct obj *obj __attribute__((__unused__));
  305. {
  306. struct monst *mtmp = fmon;
  307. pline("You produce a strange whistling sound.");
  308. while (mtmp) {
  309. if (mtmp->mtame)
  310. mnexto(mtmp);
  311. mtmp = mtmp->nmon;
  312. }
  313. }
  314. static int dig_effort; /* effort expended on current pos */
  315. static uchar dig_level;
  316. static coord dig_pos;
  317. static boolean dig_down;
  318. static int
  319. dig()
  320. {
  321. struct rm *lev;
  322. int dpx = dig_pos.x, dpy = dig_pos.y;
  323. /* perhaps a nymph stole his pick-axe while he was busy digging */
  324. /* or perhaps he teleported away */
  325. if (u.uswallow || !uwep || uwep->otyp != PICK_AXE ||
  326. dig_level != dlevel ||
  327. ((dig_down && (dpx != u.ux || dpy != u.uy)) ||
  328. (!dig_down && dist(dpx, dpy) > 2)))
  329. return (0);
  330. dig_effort += 10 + abon() + uwep->spe + rn2(5);
  331. if (dig_down) {
  332. if (!xdnstair) {
  333. pline("The floor here seems too hard to dig in.");
  334. return (0);
  335. }
  336. if (dig_effort > 250) {
  337. dighole();
  338. return (0); /* done with digging */
  339. }
  340. if (dig_effort > 50) {
  341. struct trap *ttmp = t_at(dpx, dpy);
  342. if (!ttmp) {
  343. ttmp = maketrap(dpx, dpy, PIT);
  344. ttmp->tseen = 1;
  345. pline("You have dug a pit.");
  346. u.utrap = rn1(4, 2);
  347. u.utraptype = TT_PIT;
  348. return (0);
  349. }
  350. }
  351. } else if (dig_effort > 100) {
  352. const char *digtxt;
  353. struct obj *obj;
  354. lev = &levl[dpx][dpy];
  355. if ((obj = sobj_at(ENORMOUS_ROCK, dpx, dpy)) != NULL) {
  356. fracture_rock(obj);
  357. digtxt = "The rock falls apart.";
  358. } else if (!lev->typ || lev->typ == SCORR) {
  359. lev->typ = CORR;
  360. digtxt = "You succeeded in cutting away some rock.";
  361. } else if (lev->typ == HWALL || lev->typ == VWALL
  362. || lev->typ == SDOOR) {
  363. lev->typ = xdnstair ? DOOR : ROOM;
  364. digtxt = "You just made an opening in the wall.";
  365. } else
  366. digtxt = "Now what exactly was it that you were digging in?";
  367. mnewsym(dpx, dpy);
  368. prl(dpx, dpy);
  369. pline(digtxt); /* after mnewsym & prl */
  370. return (0);
  371. } else {
  372. if (IS_WALL(levl[dpx][dpy].typ)) {
  373. int rno = inroom(dpx, dpy);
  374. if (rno >= 0 && rooms[rno].rtype >= 8) {
  375. pline("This wall seems too hard to dig into.");
  376. return (0);
  377. }
  378. }
  379. pline("You hit the rock with all your might.");
  380. }
  381. return (1);
  382. }
  383. /* When will hole be finished? Very rough indication used by shopkeeper. */
  384. int
  385. holetime()
  386. {
  387. return ((occupation == dig) ? (250 - dig_effort) / 20 : -1);
  388. }
  389. void
  390. dighole()
  391. {
  392. struct trap *ttmp = t_at(u.ux, u.uy);
  393. if (!xdnstair) {
  394. pline("The floor here seems too hard to dig in.");
  395. } else {
  396. if (ttmp)
  397. ttmp->ttyp = TRAPDOOR;
  398. else
  399. ttmp = maketrap(u.ux, u.uy, TRAPDOOR);
  400. ttmp->tseen = 1;
  401. pline("You've made a hole in the floor.");
  402. if (!u.ustuck) {
  403. if (inshop())
  404. shopdig(1);
  405. pline("You fall through ...");
  406. if (u.utraptype == TT_PIT) {
  407. u.utrap = 0;
  408. u.utraptype = 0;
  409. }
  410. goto_level(dlevel + 1, FALSE);
  411. }
  412. }
  413. }
  414. static int
  415. use_pick_axe(obj)
  416. struct obj *obj;
  417. {
  418. char dirsyms[12];
  419. char *dsp = dirsyms, *sdp = sdir;
  420. struct monst *mtmp;
  421. struct rm *lev;
  422. int rx, ry, res = 0;
  423. if (obj != uwep) {
  424. if (uwep && uwep->cursed) {
  425. /* Andreas Bormann - ihnp4!decvax!mcvax!unido!ab */
  426. pline("Since your weapon is welded to your hand,");
  427. pline("you cannot use that pick-axe.");
  428. return (0);
  429. }
  430. pline("You now wield %s.", doname(obj));
  431. setuwep(obj);
  432. res = 1;
  433. }
  434. while (*sdp) {
  435. (void) movecmd(*sdp); /* sets u.dx and u.dy and u.dz */
  436. rx = u.ux + u.dx;
  437. ry = u.uy + u.dy;
  438. if (u.dz > 0 || (u.dz == 0 && isok(rx, ry) &&
  439. (IS_ROCK(levl[rx][ry].typ)
  440. || sobj_at(ENORMOUS_ROCK, rx, ry))))
  441. *dsp++ = *sdp;
  442. sdp++;
  443. }
  444. *dsp = 0;
  445. pline("In what direction do you want to dig? [%s] ", dirsyms);
  446. if (!getdir(0)) /* no txt */
  447. return (res);
  448. if (u.uswallow && attack(u.ustuck)) /* return(1) */
  449. ;
  450. else if (u.dz < 0)
  451. pline("You cannot reach the ceiling.");
  452. else if (u.dz == 0) {
  453. if (Confusion)
  454. confdir();
  455. rx = u.ux + u.dx;
  456. ry = u.uy + u.dy;
  457. if ((mtmp = m_at(rx, ry)) && attack(mtmp))
  458. return (1);
  459. if (!isok(rx, ry)) {
  460. pline("Clash!");
  461. return (1);
  462. }
  463. lev = &levl[rx][ry];
  464. if (lev->typ == DOOR)
  465. pline("Your %s against the door.",
  466. aobjnam(obj, "clang"));
  467. else if (!IS_ROCK(lev->typ)
  468. && !sobj_at(ENORMOUS_ROCK, rx, ry)) {
  469. /* ACCESSIBLE or POOL */
  470. pline("You swing your %s through thin air.",
  471. aobjnam(obj, (char *) 0));
  472. } else {
  473. if (dig_pos.x != rx || dig_pos.y != ry
  474. || dig_level != dlevel || dig_down) {
  475. dig_down = FALSE;
  476. dig_pos.x = rx;
  477. dig_pos.y = ry;
  478. dig_level = dlevel;
  479. dig_effort = 0;
  480. pline("You start digging.");
  481. } else
  482. pline("You continue digging.");
  483. occupation = dig;
  484. occtxt = "digging";
  485. }
  486. } else if (Levitation) {
  487. pline("You cannot reach the floor.");
  488. } else {
  489. if (dig_pos.x != u.ux || dig_pos.y != u.uy
  490. || dig_level != dlevel || !dig_down) {
  491. dig_down = TRUE;
  492. dig_pos.x = u.ux;
  493. dig_pos.y = u.uy;
  494. dig_level = dlevel;
  495. dig_effort = 0;
  496. pline("You start digging in the floor.");
  497. if (inshop())
  498. shopdig(0);
  499. } else
  500. pline("You continue digging in the floor.");
  501. occupation = dig;
  502. occtxt = "digging";
  503. }
  504. return (1);
  505. }