hack.cmd.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /* $NetBSD: hack.cmd.c,v 1.7 2003/04/02 18:36:35 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.cmd.c,v 1.7 2003/04/02 18:36:35 jsm Exp $");
  64. #endif /* not lint */
  65. #include "hack.h"
  66. #include "extern.h"
  67. #include "def.func_tab.h"
  68. const struct func_tab cmdlist[] = {
  69. { '\020', doredotopl },
  70. { '\022', doredraw },
  71. { '\024', dotele },
  72. #ifdef SUSPEND
  73. { '\032', dosuspend },
  74. #endif /* SUSPEND */
  75. { 'a', doapply },
  76. /* 'A' : UNUSED */
  77. /* 'b', 'B' : go sw */
  78. { 'c', ddocall },
  79. { 'C', do_mname },
  80. { 'd', dodrop },
  81. { 'D', doddrop },
  82. { 'e', doeat },
  83. { 'E', doengrave },
  84. /* 'f', 'F' : multiple go (might become 'fight') */
  85. /* 'g', 'G' : UNUSED */
  86. /* 'h', 'H' : go west */
  87. { 'I', dotypeinv }, /* Robert Viduya */
  88. { 'i', ddoinv },
  89. /* 'j', 'J', 'k', 'K', 'l', 'L', 'm', 'M', 'n', 'N' : move commands */
  90. /* 'o', doopen, */
  91. { 'O', doset },
  92. { 'p', dopay },
  93. { 'P', dowearring },
  94. { 'q', dodrink },
  95. { 'Q', dodone },
  96. { 'r', doread },
  97. { 'R', doremring },
  98. { 's', dosearch },
  99. { 'S', dosave },
  100. { 't', dothrow },
  101. { 'T', doremarm },
  102. /* 'u', 'U' : go ne */
  103. { 'v', doversion },
  104. /* 'V' : UNUSED */
  105. { 'w', dowield },
  106. { 'W', doweararm },
  107. /* 'x', 'X' : UNUSED */
  108. /* 'y', 'Y' : go nw */
  109. { 'z', dozap },
  110. /* 'Z' : UNUSED */
  111. { '<', doup },
  112. { '>', dodown },
  113. { '/', dowhatis },
  114. { '?', dohelp },
  115. #ifdef SHELL
  116. { '!', dosh },
  117. #endif /* SHELL */
  118. { '.', donull },
  119. { ' ', donull },
  120. { ',', dopickup },
  121. { ':', dolook },
  122. { '^', doidtrap },
  123. { '\\', dodiscovered }, /* Robert Viduya */
  124. { WEAPON_SYM, doprwep },
  125. { ARMOR_SYM, doprarm },
  126. { RING_SYM, doprring },
  127. { '$', doprgold },
  128. { '#', doextcmd },
  129. { 0, 0 }
  130. };
  131. const struct ext_func_tab extcmdlist[] = {
  132. { "dip", dodip },
  133. { "pray", dopray },
  134. { (char *) 0, donull }
  135. };
  136. void
  137. rhack(cmd)
  138. const char *cmd;
  139. {
  140. const struct func_tab *tlist = cmdlist;
  141. boolean firsttime = FALSE;
  142. int res;
  143. if (!cmd) {
  144. firsttime = TRUE;
  145. flags.nopick = 0;
  146. cmd = parse();
  147. }
  148. if (!*cmd || (*cmd & 0377) == 0377 ||
  149. (flags.no_rest_on_space && *cmd == ' ')) {
  150. bell();
  151. flags.move = 0;
  152. return; /* probably we just had an interrupt */
  153. }
  154. if (movecmd(*cmd)) {
  155. walk:
  156. if (multi)
  157. flags.mv = 1;
  158. domove();
  159. return;
  160. }
  161. if (movecmd(lowc(*cmd))) {
  162. flags.run = 1;
  163. rush:
  164. if (firsttime) {
  165. if (!multi)
  166. multi = COLNO;
  167. u.last_str_turn = 0;
  168. }
  169. flags.mv = 1;
  170. #ifdef QUEST
  171. if (flags.run >= 4)
  172. finddir();
  173. if (firsttime) {
  174. u.ux0 = u.ux + u.dx;
  175. u.uy0 = u.uy + u.dy;
  176. }
  177. #endif /* QUEST */
  178. domove();
  179. return;
  180. }
  181. if ((*cmd == 'f' && movecmd(cmd[1])) || movecmd(unctrl(*cmd))) {
  182. flags.run = 2;
  183. goto rush;
  184. }
  185. if (*cmd == 'F' && movecmd(lowc(cmd[1]))) {
  186. flags.run = 3;
  187. goto rush;
  188. }
  189. if (*cmd == 'm' && movecmd(cmd[1])) {
  190. flags.run = 0;
  191. flags.nopick = 1;
  192. goto walk;
  193. }
  194. if (*cmd == 'M' && movecmd(lowc(cmd[1]))) {
  195. flags.run = 1;
  196. flags.nopick = 1;
  197. goto rush;
  198. }
  199. #ifdef QUEST
  200. if (*cmd == cmd[1] && (*cmd == 'f' || *cmd == 'F')) {
  201. flags.run = 4;
  202. if (*cmd == 'F')
  203. flags.run += 2;
  204. if (cmd[2] == '-')
  205. flags.run += 1;
  206. goto rush;
  207. }
  208. #endif /* QUEST */
  209. while (tlist->f_char) {
  210. if (*cmd == tlist->f_char) {
  211. res = (*(tlist->f_funct)) ();
  212. if (!res) {
  213. flags.move = 0;
  214. multi = 0;
  215. }
  216. return;
  217. }
  218. tlist++;
  219. }
  220. {
  221. char expcmd[10];
  222. char *cp = expcmd;
  223. while (*cmd && cp - expcmd < (int)sizeof(expcmd) - 2) {
  224. if (*cmd >= 040 && *cmd < 0177)
  225. *cp++ = *cmd++;
  226. else {
  227. *cp++ = '^';
  228. *cp++ = *cmd++ ^ 0100;
  229. }
  230. }
  231. *cp++ = 0;
  232. pline("Unknown command '%s'.", expcmd);
  233. }
  234. multi = flags.move = 0;
  235. }
  236. int
  237. doextcmd()
  238. { /* here after # - now read a full-word
  239. * command */
  240. char buf[BUFSZ];
  241. const struct ext_func_tab *efp = extcmdlist;
  242. pline("# ");
  243. getlin(buf);
  244. clrlin();
  245. if (buf[0] == '\033')
  246. return (0);
  247. while (efp->ef_txt) {
  248. if (!strcmp(efp->ef_txt, buf))
  249. return ((*(efp->ef_funct)) ());
  250. efp++;
  251. }
  252. pline("%s: unknown command.", buf);
  253. return (0);
  254. }
  255. char
  256. lowc(sym)
  257. char sym;
  258. {
  259. return ((sym >= 'A' && sym <= 'Z') ? sym + 'a' - 'A' : sym);
  260. }
  261. char
  262. unctrl(sym)
  263. char sym;
  264. {
  265. return ((sym >= ('A' & 037) && sym <= ('Z' & 037)) ? sym + 0140 : sym);
  266. }
  267. /* 'rogue'-like direction commands */
  268. char sdir[] = "hykulnjb><";
  269. schar xdir[10] = {-1, -1, 0, 1, 1, 1, 0, -1, 0, 0};
  270. schar ydir[10] = {0, -1, -1, -1, 0, 1, 1, 1, 0, 0};
  271. schar zdir[10] = {0, 0, 0, 0, 0, 0, 0, 0, 1, -1};
  272. int
  273. movecmd(sym) /* also sets u.dz, but returns false for <> */
  274. char sym;
  275. {
  276. char *dp;
  277. u.dz = 0;
  278. if (!(dp = strchr(sdir, sym)))
  279. return (0);
  280. u.dx = xdir[dp - sdir];
  281. u.dy = ydir[dp - sdir];
  282. u.dz = zdir[dp - sdir];
  283. return (!u.dz);
  284. }
  285. int
  286. getdir(s)
  287. boolean s;
  288. {
  289. char dirsym;
  290. if (s)
  291. pline("In what direction?");
  292. dirsym = readchar();
  293. if (!movecmd(dirsym) && !u.dz) {
  294. if (!strchr(quitchars, dirsym))
  295. pline("What a strange direction!");
  296. return (0);
  297. }
  298. if (Confusion && !u.dz)
  299. confdir();
  300. return (1);
  301. }
  302. void
  303. confdir()
  304. {
  305. int x = rn2(8);
  306. u.dx = xdir[x];
  307. u.dy = ydir[x];
  308. }
  309. #ifdef QUEST
  310. int
  311. finddir()
  312. {
  313. int i, ui = u.di;
  314. for (i = 0; i <= 8; i++) {
  315. if (flags.run & 1)
  316. ui++;
  317. else
  318. ui += 7;
  319. ui %= 8;
  320. if (i == 8) {
  321. pline("Not near a wall.");
  322. flags.move = multi = 0;
  323. return (0);
  324. }
  325. if (!isroom(u.ux + xdir[ui], u.uy + ydir[ui]))
  326. break;
  327. }
  328. for (i = 0; i <= 8; i++) {
  329. if (flags.run & 1)
  330. ui += 7;
  331. else
  332. ui++;
  333. ui %= 8;
  334. if (i == 8) {
  335. pline("Not near a room.");
  336. flags.move = multi = 0;
  337. return (0);
  338. }
  339. if (isroom(u.ux + xdir[ui], u.uy + ydir[ui]))
  340. break;
  341. }
  342. u.di = ui;
  343. u.dx = xdir[ui];
  344. u.dy = ydir[ui];
  345. return 0;
  346. }
  347. int
  348. isroom(x, y)
  349. int x, y;
  350. { /* what about POOL? */
  351. return (isok(x, y) && (levl[x][y].typ == ROOM ||
  352. (levl[x][y].typ >= LDOOR && flags.run >= 6)));
  353. }
  354. #endif /* QUEST */
  355. int
  356. isok(x, y)
  357. int x, y;
  358. {
  359. /* x corresponds to curx, so x==1 is the first column. Ach. %% */
  360. return (x >= 1 && x <= COLNO - 1 && y >= 0 && y <= ROWNO - 1);
  361. }