xref: /dragonfly/games/hack/hack.cmd.c (revision 9317c2d0)
1 /*	$NetBSD: hack.cmd.c,v 1.12 2011/08/06 20:42:43 dholland Exp $	*/
2 
3 /*
4  * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
5  * Amsterdam
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are
10  * met:
11  *
12  * - Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * - Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  *
19  * - Neither the name of the Stichting Centrum voor Wiskunde en
20  * Informatica, nor the names of its contributors may be used to endorse or
21  * promote products derived from this software without specific prior
22  * written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
28  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 /*
38  * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
39  * All rights reserved.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  * 3. The name of the author may not be used to endorse or promote products
50  *    derived from this software without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
53  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
54  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
55  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
56  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
57  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
58  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
59  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
60  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
61  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62  */
63 
64 #include	"hack.h"
65 #include	"extern.h"
66 #include	"def.func_tab.h"
67 
68 static int doextcmd(void);
69 
70 static const struct func_tab cmdlist[] = {
71 	{ '\020', doredotopl },
72 	{ '\022', doredraw },
73 	{ '\024', dotele },
74 #ifdef SUSPEND
75 	{ '\032', dosuspend },
76 #endif	/* SUSPEND */
77 	{ 'a', doapply },
78 	/* 'A' : UNUSED */
79 	/* 'b', 'B' : go sw */
80 	{ 'c', ddocall },
81 	{ 'C', do_mname },
82 	{ 'd', dodrop },
83 	{ 'D', doddrop },
84 	{ 'e', doeat },
85 	{ 'E', doengrave },
86 	/* 'f', 'F' : multiple go (might become 'fight') */
87 	/* 'g', 'G' : UNUSED */
88 	/* 'h', 'H' : go west */
89 	{ 'I', dotypeinv },		/* Robert Viduya */
90 	{ 'i', ddoinv },
91 	/* 'j', 'J', 'k', 'K', 'l', 'L', 'm', 'M', 'n', 'N' : move commands */
92 	/* 'o', doopen,	 */
93 	{ 'O', doset },
94 	{ 'p', dopay },
95 	{ 'P', dowearring },
96 	{ 'q', dodrink },
97 	{ 'Q', dodone },
98 	{ 'r', doread },
99 	{ 'R', doremring },
100 	{ 's', dosearch },
101 	{ 'S', dosave },
102 	{ 't', dothrow },
103 	{ 'T', doremarm },
104 	/* 'u', 'U' : go ne */
105 	{ 'v', doversion },
106 	/* 'V' : UNUSED */
107 	{ 'w', dowield },
108 	{ 'W', doweararm },
109 	/* 'x', 'X' : UNUSED */
110 	/* 'y', 'Y' : go nw */
111 	{ 'z', dozap },
112 	/* 'Z' : UNUSED */
113 	{ '<', doup },
114 	{ '>', dodown },
115 	{ '/', dowhatis },
116 	{ '?', dohelp },
117 #ifdef SHELL
118 	{ '!', dosh },
119 #endif	/* SHELL */
120 	{ '.', donull },
121 	{ ' ', donull },
122 	{ ',', dopickup },
123 	{ ':', dolook },
124 	{ '^', doidtrap },
125 	{ '\\', dodiscovered },	/* Robert Viduya */
126 	{ WEAPON_SYM, doprwep },
127 	{ ARMOR_SYM, doprarm },
128 	{ RING_SYM, doprring },
129 	{ '$', doprgold },
130 	{ '#', doextcmd },
131 	{ 0, 0 }
132 };
133 
134 static const struct ext_func_tab extcmdlist[] = {
135 	{ "dip", dodip },
136 	{ "pray", dopray },
137 	{ NULL, donull }
138 };
139 
140 static char lowc(int);
141 static char unctrl(int);
142 
143 void
144 rhack(const char *cmd)
145 {
146 	const struct func_tab *tlist = cmdlist;
147 	boolean         firsttime = FALSE;
148 	int res;
149 
150 	if (!cmd) {
151 		firsttime = TRUE;
152 		flags.nopick = 0;
153 		cmd = parse();
154 	}
155 	if (!*cmd || (*cmd & 0377) == 0377 ||
156 	    (flags.no_rest_on_space && *cmd == ' ')) {
157 		sound_bell();
158 		flags.move = 0;
159 		return;		/* probably we just had an interrupt */
160 	}
161 	if (movecmd(*cmd)) {
162 walk:
163 		if (multi)
164 			flags.mv = 1;
165 		domove();
166 		return;
167 	}
168 	if (movecmd(lowc(*cmd))) {
169 		flags.run = 1;
170 rush:
171 		if (firsttime) {
172 			if (!multi)
173 				multi = COLNO;
174 			u.last_str_turn = 0;
175 		}
176 		flags.mv = 1;
177 #ifdef QUEST
178 		if (flags.run >= 4)
179 			finddir();
180 		if (firsttime) {
181 			u.ux0 = u.ux + u.dx;
182 			u.uy0 = u.uy + u.dy;
183 		}
184 #endif	/* QUEST */
185 		domove();
186 		return;
187 	}
188 	if ((*cmd == 'f' && movecmd(cmd[1])) || movecmd(unctrl(*cmd))) {
189 		flags.run = 2;
190 		goto rush;
191 	}
192 	if (*cmd == 'F' && movecmd(lowc(cmd[1]))) {
193 		flags.run = 3;
194 		goto rush;
195 	}
196 	if (*cmd == 'm' && movecmd(cmd[1])) {
197 		flags.run = 0;
198 		flags.nopick = 1;
199 		goto walk;
200 	}
201 	if (*cmd == 'M' && movecmd(lowc(cmd[1]))) {
202 		flags.run = 1;
203 		flags.nopick = 1;
204 		goto rush;
205 	}
206 #ifdef QUEST
207 	if (*cmd == cmd[1] && (*cmd == 'f' || *cmd == 'F')) {
208 		flags.run = 4;
209 		if (*cmd == 'F')
210 			flags.run += 2;
211 		if (cmd[2] == '-')
212 			flags.run += 1;
213 		goto rush;
214 	}
215 #endif	/* QUEST */
216 	while (tlist->f_char) {
217 		if (*cmd == tlist->f_char) {
218 			res = (*(tlist->f_funct)) ();
219 			if (!res) {
220 				flags.move = 0;
221 				multi = 0;
222 			}
223 			return;
224 		}
225 		tlist++;
226 	}
227 	{
228 		char            expcmd[10];
229 		char  *cp = expcmd;
230 		while (*cmd && cp - expcmd < (int)sizeof(expcmd) - 2) {
231 			if (*cmd >= 040 && *cmd < 0177)
232 				*cp++ = *cmd++;
233 			else {
234 				*cp++ = '^';
235 				*cp++ = *cmd++ ^ 0100;
236 			}
237 		}
238 		*cp++ = 0;
239 		pline("Unknown command '%s'.", expcmd);
240 	}
241 	multi = flags.move = 0;
242 }
243 
244 static int
245 doextcmd(void)
246 {				/* here after # - now read a full-word
247 				 * command */
248 	char            buf[BUFSZ];
249 	const struct ext_func_tab *efp = extcmdlist;
250 
251 	pline("# ");
252 	getlin(buf);
253 	clrlin();
254 	if (buf[0] == '\033')
255 		return (0);
256 	while (efp->ef_txt) {
257 		if (!strcmp(efp->ef_txt, buf))
258 			return ((*(efp->ef_funct)) ());
259 		efp++;
260 	}
261 	pline("%s: unknown command.", buf);
262 	return (0);
263 }
264 
265 static char
266 lowc(int sym)
267 {
268 	return ((sym >= 'A' && sym <= 'Z') ? sym + 'a' - 'A' : sym);
269 }
270 
271 static char
272 unctrl(int sym)
273 {
274 	return ((sym >= ('A' & 037) && sym <= ('Z' & 037)) ? sym + 0140 : sym);
275 }
276 
277 /* 'rogue'-like direction commands */
278 char            sdir[] = "hykulnjb><";
279 schar           xdir[10] = {-1, -1, 0, 1, 1, 1, 0, -1, 0, 0};
280 schar           ydir[10] = {0, -1, -1, -1, 0, 1, 1, 1, 0, 0};
281 static schar    zdir[10] = {0, 0, 0, 0, 0, 0, 0, 0, 1, -1};
282 
283 int
284 movecmd(int sym)		/* also sets u.dz, but returns false for <> */
285 {
286 	char  *dp;
287 
288 	u.dz = 0;
289 	if (!(dp = strchr(sdir, sym)))
290 		return (0);
291 	u.dx = xdir[dp - sdir];
292 	u.dy = ydir[dp - sdir];
293 	u.dz = zdir[dp - sdir];
294 	return (!u.dz);
295 }
296 
297 int
298 getdir(boolean s)
299 {
300 	char            dirsym;
301 
302 	if (s)
303 		pline("In what direction?");
304 	dirsym = readchar();
305 	if (!movecmd(dirsym) && !u.dz) {
306 		if (!strchr(quitchars, dirsym))
307 			pline("What a strange direction!");
308 		return (0);
309 	}
310 	if (Confusion && !u.dz)
311 		confdir();
312 	return (1);
313 }
314 
315 void
316 confdir(void)
317 {
318 	int x = rn2(8);
319 	u.dx = xdir[x];
320 	u.dy = ydir[x];
321 }
322 
323 #ifdef QUEST
324 int
325 finddir(void)
326 {
327 	int    i, ui = u.di;
328 	for (i = 0; i <= 8; i++) {
329 		if (flags.run & 1)
330 			ui++;
331 		else
332 			ui += 7;
333 		ui %= 8;
334 		if (i == 8) {
335 			pline("Not near a wall.");
336 			flags.move = multi = 0;
337 			return (0);
338 		}
339 		if (!isroom(u.ux + xdir[ui], u.uy + ydir[ui]))
340 			break;
341 	}
342 	for (i = 0; i <= 8; i++) {
343 		if (flags.run & 1)
344 			ui += 7;
345 		else
346 			ui++;
347 		ui %= 8;
348 		if (i == 8) {
349 			pline("Not near a room.");
350 			flags.move = multi = 0;
351 			return (0);
352 		}
353 		if (isroom(u.ux + xdir[ui], u.uy + ydir[ui]))
354 			break;
355 	}
356 	u.di = ui;
357 	u.dx = xdir[ui];
358 	u.dy = ydir[ui];
359 	return 0;
360 }
361 
362 int
363 isroom(int x, int y)
364 {				/* what about POOL? */
365 	return (isok(x, y) && (levl[x][y].typ == ROOM ||
366 			       (levl[x][y].typ >= LDOOR && flags.run >= 6)));
367 }
368 #endif	/* QUEST */
369 
370 int
371 isok(int x, int y)
372 {
373 	/* x corresponds to curx, so x==1 is the first column. Ach. %% */
374 	return (x >= 1 && x <= COLNO - 1 && y >= 0 && y <= ROWNO - 1);
375 }
376