xref: /openbsd/games/hack/hack.cmd.c (revision 891d7ab6)
1 /*	$OpenBSD: hack.cmd.c,v 1.7 2009/10/27 23:59:25 deraadt 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	"def.func_tab.h"
66 #include	<ctype.h>
67 
68 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', done2 },
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', NULL }
130 };
131 
132 struct ext_func_tab extcmdlist[] = {
133 	{ "dip", dodip },
134 	{ "pray", dopray },
135 	{ NULL, donull}
136 };
137 
138 extern char quitchars[];
139 
140 static char unctrl(char);
141 
142 void
143 rhack(char *cmd)
144 {
145 	struct func_tab *tlist = cmdlist;
146 	boolean firsttime = FALSE;
147 	int res;
148 
149 	if(!cmd) {
150 		firsttime = TRUE;
151 		flags.nopick = 0;
152 		cmd = parse();
153 	}
154 	if(!*cmd || (*cmd & 0377) == 0377 ||
155 	   (flags.no_rest_on_space && *cmd == ' ')){
156 		hackbell();
157 		flags.move = 0;
158 		return;		/* probably we just had an interrupt */
159 	}
160 	if(movecmd(*cmd)) {
161 	walk:
162 		if(multi) flags.mv = 1;
163 		domove();
164 		return;
165 	}
166 	if(movecmd(tolower(*cmd))) {
167 		flags.run = 1;
168 	rush:
169 		if(firsttime){
170 			if(!multi) multi = COLNO;
171 			u.last_str_turn = 0;
172 		}
173 		flags.mv = 1;
174 #ifdef QUEST
175 		if(flags.run >= 4) finddir();
176 		if(firsttime){
177 			u.ux0 = u.ux + u.dx;
178 			u.uy0 = u.uy + u.dy;
179 		}
180 #endif /* QUEST */
181 		domove();
182 		return;
183 	}
184 	if((*cmd == 'f' && movecmd(cmd[1])) || movecmd(unctrl(*cmd))) {
185 		flags.run = 2;
186 		goto rush;
187 	}
188 	if(*cmd == 'F' && movecmd(tolower(cmd[1]))) {
189 		flags.run = 3;
190 		goto rush;
191 	}
192 	if(*cmd == 'm' && movecmd(cmd[1])) {
193 		flags.run = 0;
194 		flags.nopick = 1;
195 		goto walk;
196 	}
197 	if(*cmd == 'M' && movecmd(tolower(cmd[1]))) {
198 		flags.run = 1;
199 		flags.nopick = 1;
200 		goto rush;
201 	}
202 #ifdef QUEST
203 	if(*cmd == cmd[1] && (*cmd == 'f' || *cmd == 'F')) {
204 		flags.run = 4;
205 		if(*cmd == 'F') flags.run += 2;
206 		if(cmd[2] == '-') flags.run += 1;
207 		goto rush;
208 	}
209 #endif /* QUEST */
210 	while(tlist->f_char) {
211 		if(*cmd == tlist->f_char){
212 			res = (*(tlist->f_funct))();
213 			if(!res) {
214 				flags.move = 0;
215 				multi = 0;
216 			}
217 			return;
218 		}
219 		tlist++;
220 	}
221 	{ char expcmd[10];
222 	  char *cp = expcmd;
223 	  while(*cmd && cp-expcmd < 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 
237 int
238 doextcmd()	/* here after # - now read a full-word command */
239 {
240 	char buf[BUFSZ];
241 	struct ext_func_tab *efp = extcmdlist;
242 
243 	pline("# ");
244 	getlin(buf);
245 	clrlin();
246 	if(buf[0] == '\033')
247 		return(0);
248 	while(efp->ef_txt) {
249 		if(!strcmp(efp->ef_txt, buf))
250 			return((*(efp->ef_funct))());
251 		efp++;
252 	}
253 	pline("%s: unknown command.", buf);
254 	return(0);
255 }
256 
257 static char
258 unctrl(char sym)
259 {
260     return( (sym >= ('A' & 037) && sym <= ('Z' & 037)) ? sym + 0140 : sym );
261 }
262 
263 /* 'rogue'-like direction commands */
264 char sdir[] = "hykulnjb><";
265 schar xdir[10] = { -1,-1, 0, 1, 1, 1, 0,-1, 0, 0 };
266 schar ydir[10] = {  0,-1,-1,-1, 0, 1, 1, 1, 0, 0 };
267 schar zdir[10] = {  0, 0, 0, 0, 0, 0, 0, 0, 1,-1 };
268 
269 int
270 movecmd(char sym)	/* also sets u.dz, but returns false for <> */
271 {
272 	char *dp;
273 
274 	u.dz = 0;
275 	if(!(dp = strchr(sdir, sym))) return(0);
276 	u.dx = xdir[dp-sdir];
277 	u.dy = ydir[dp-sdir];
278 	u.dz = zdir[dp-sdir];
279 	return(!u.dz);
280 }
281 
282 int
283 getdir(boolean s)
284 {
285 	char dirsym;
286 
287 	if(s) pline("In what direction?");
288 	dirsym = readchar();
289 	if(!movecmd(dirsym) && !u.dz) {
290 		if(!strchr(quitchars, dirsym))
291 			pline("What a strange direction!");
292 		return(0);
293 	}
294 	if(Confusion && !u.dz)
295 		confdir();
296 	return(1);
297 }
298 
299 void
300 confdir()
301 {
302 	int x = rn2(8);
303 	u.dx = xdir[x];
304 	u.dy = ydir[x];
305 }
306 
307 #ifdef QUEST
308 void
309 finddir()
310 {
311 	int i, ui = u.di;
312 
313 	for(i = 0; i <= 8; i++){
314 		if(flags.run & 1) ui++; else ui += 7;
315 		ui %= 8;
316 		if(i == 8){
317 			pline("Not near a wall.");
318 			flags.move = multi = 0;
319 			return;
320 		}
321 		if(!isroom(u.ux+xdir[ui], u.uy+ydir[ui]))
322 			break;
323 	}
324 	for(i = 0; i <= 8; i++){
325 		if(flags.run & 1) ui += 7; else ui++;
326 		ui %= 8;
327 		if(i == 8){
328 			pline("Not near a room.");
329 			flags.move = multi = 0;
330 			return;
331 		}
332 		if(isroom(u.ux+xdir[ui], u.uy+ydir[ui]))
333 			break;
334 	}
335 	u.di = ui;
336 	u.dx = xdir[ui];
337 	u.dy = ydir[ui];
338 }
339 
340 int
341 isroom(int x, int y)
342 {		/* what about POOL? */
343 	return(isok(x,y) && (levl[x][y].typ == ROOM ||
344 				(levl[x][y].typ >= LDOOR && flags.run >= 6)));
345 }
346 #endif /* QUEST */
347 
348 int
349 isok(int x, int y)
350 {
351 	/* x corresponds to curx, so x==1 is the first column. Ach. %% */
352 	return(x >= 1 && x <= COLNO-1 && y >= 0 && y <= ROWNO-1);
353 }
354