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