Merge branch 'vendor/BZIP'
[games.git] / games / hack / hack.cmd.c
CommitLineData
984263bc
MD
1/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2/* hack.cmd.c - version 1.0.3 */
3/* $FreeBSD: src/games/hack/hack.cmd.c,v 1.4 1999/11/16 10:26:35 marcel Exp $ */
c7106d58 4/* $DragonFly: src/games/hack/hack.cmd.c,v 1.4 2006/08/21 19:45:32 pavalos Exp $ */
984263bc 5
6693db17
SW
6#include "hack.h"
7#include "def.func_tab.h"
984263bc 8
c7106d58
PA
9static int doextcmd(void);
10static char lowc(char);
11static char unctrl(char);
12#ifdef QUEST
13static bool isroom(int, int);
14#endif
15static int done2(void);
984263bc
MD
16
17struct func_tab cmdlist[]={
c7106d58
PA
18 { '\020', doredotopl },
19 { '\022', doredraw },
20 { '\024', dotele },
984263bc 21#ifdef SUSPEND
c7106d58 22 { '\032', dosuspend },
304d60d9 23#endif /* SUSPEND */
c7106d58 24 { 'a', doapply },
6693db17
SW
25 /*'A' : UNUSED */
26 /*'b', 'B' : go sw */
c7106d58
PA
27 { 'c', ddocall },
28 { 'C', do_mname },
29 { 'd', dodrop },
30 { 'D', doddrop },
31 { 'e', doeat },
32 { 'E', doengrave },
6693db17
SW
33 /*'f', 'F' : multiple go (might become 'fight') */
34 /*'g', 'G' : UNUSED */
35 /*'h', 'H' : go west */
c7106d58
PA
36 { 'I', dotypeinv }, /* Robert Viduya */
37 { 'i', ddoinv },
6693db17
SW
38 /*'j', 'J', 'k', 'K', 'l', 'L', 'm', 'M', 'n', 'N' : move commands */
39 /*'o', doopen, */
c7106d58
PA
40 { 'O', doset },
41 { 'p', dopay },
42 { 'P', dowearring },
43 { 'q', dodrink },
44 { 'Q', done2 },
45 { 'r', doread },
46 { 'R', doremring },
47 { 's', dosearch },
48 { 'S', dosave },
49 { 't', dothrow },
50 { 'T', doremarm },
6693db17 51 /*'u', 'U' : go ne */
c7106d58 52 { 'v', doversion },
6693db17 53 /*'V' : UNUSED */
c7106d58
PA
54 { 'w', dowield },
55 { 'W', doweararm },
6693db17
SW
56 /*'x', 'X' : UNUSED */
57 /*'y', 'Y' : go nw */
c7106d58 58 { 'z', dozap },
6693db17 59 /*'Z' : UNUSED */
c7106d58
PA
60 { '<', doup },
61 { '>', dodown },
62 { '/', dowhatis },
63 { '?', dohelp },
984263bc 64#ifdef SHELL
c7106d58 65 { '!', dosh },
304d60d9 66#endif /* SHELL */
c7106d58
PA
67 { '.', donull },
68 { ' ', donull },
69 { ',', dopickup },
70 { ':', dolook },
71 { '^', doidtrap },
6693db17
SW
72 { '\\', dodiscovered }, /* Robert Viduya */
73 { WEAPON_SYM, doprwep },
74 { ARMOR_SYM, doprarm },
75 { RING_SYM, doprring },
c7106d58
PA
76 { '$', doprgold },
77 { '#', doextcmd },
78 { 0, 0 }
984263bc
MD
79};
80
81struct ext_func_tab extcmdlist[] = {
c7106d58
PA
82 { "dip", dodip },
83 { "pray", dopray },
902ec341 84 { NULL, donull }
984263bc
MD
85};
86
c7106d58 87extern char quitchars[];
984263bc 88
c7106d58
PA
89void
90rhack(const char *cmd)
984263bc
MD
91{
92 struct func_tab *tlist = cmdlist;
93 boolean firsttime = FALSE;
94 int res;
95
6693db17 96 if (!cmd) {
984263bc
MD
97 firsttime = TRUE;
98 flags.nopick = 0;
99 cmd = parse();
100 }
6693db17
SW
101 if (!*cmd || (*cmd & 0377) == 0377 ||
102 (flags.no_rest_on_space && *cmd == ' ')) {
984263bc
MD
103 bell();
104 flags.move = 0;
105 return; /* probably we just had an interrupt */
106 }
6693db17
SW
107 if (movecmd(*cmd)) {
108walk:
109 if (multi)
110 flags.mv = 1;
984263bc
MD
111 domove();
112 return;
113 }
6693db17 114 if (movecmd(lowc(*cmd))) {
984263bc 115 flags.run = 1;
6693db17
SW
116rush:
117 if (firsttime) {
118 if (!multi)
119 multi = COLNO;
984263bc
MD
120 u.last_str_turn = 0;
121 }
122 flags.mv = 1;
123#ifdef QUEST
6693db17
SW
124 if (flags.run >= 4)
125 finddir();
126 if (firsttime) {
984263bc
MD
127 u.ux0 = u.ux + u.dx;
128 u.uy0 = u.uy + u.dy;
129 }
304d60d9 130#endif /* QUEST */
984263bc
MD
131 domove();
132 return;
133 }
6693db17 134 if ((*cmd == 'f' && movecmd(cmd[1])) || movecmd(unctrl(*cmd))) {
984263bc
MD
135 flags.run = 2;
136 goto rush;
137 }
6693db17 138 if (*cmd == 'F' && movecmd(lowc(cmd[1]))) {
984263bc
MD
139 flags.run = 3;
140 goto rush;
141 }
6693db17 142 if (*cmd == 'm' && movecmd(cmd[1])) {
984263bc
MD
143 flags.run = 0;
144 flags.nopick = 1;
145 goto walk;
146 }
6693db17 147 if (*cmd == 'M' && movecmd(lowc(cmd[1]))) {
984263bc
MD
148 flags.run = 1;
149 flags.nopick = 1;
150 goto rush;
151 }
152#ifdef QUEST
6693db17 153 if (*cmd == cmd[1] && (*cmd == 'f' || *cmd == 'F')) {
984263bc 154 flags.run = 4;
6693db17
SW
155 if (*cmd == 'F')
156 flags.run += 2;
157 if (cmd[2] == '-')
158 flags.run += 1;
984263bc
MD
159 goto rush;
160 }
304d60d9 161#endif /* QUEST */
6693db17
SW
162 while (tlist->f_char) {
163 if (*cmd == tlist->f_char) {
984263bc 164 res = (*(tlist->f_funct))();
6693db17 165 if (!res) {
984263bc
MD
166 flags.move = 0;
167 multi = 0;
168 }
169 return;
170 }
171 tlist++;
172 }
6693db17
SW
173 {
174 char expcmd[10];
175 char *cp = expcmd;
176 while (*cmd && cp - expcmd < (int)sizeof(expcmd) - 2) {
177 if (*cmd >= 040 && *cmd < 0177)
178 *cp++ = *cmd++;
179 else {
180 *cp++ = '^';
181 *cp++ = *cmd++ ^ 0100;
182 }
984263bc 183 }
6693db17
SW
184 *cp++ = 0;
185 pline("Unknown command '%s'.", expcmd);
984263bc
MD
186 }
187 multi = flags.move = 0;
188}
189
c7106d58 190static int
6693db17 191doextcmd(void) /* here after # - now read a full-word command */
984263bc
MD
192{
193 char buf[BUFSZ];
194 struct ext_func_tab *efp = extcmdlist;
195
196 pline("# ");
197 getlin(buf);
198 clrlin();
6693db17
SW
199 if (buf[0] == '\033')
200 return (0);
201 while (efp->ef_txt) {
202 if (!strcmp(efp->ef_txt, buf))
203 return ((*(efp->ef_funct))());
984263bc
MD
204 efp++;
205 }
206 pline("%s: unknown command.", buf);
6693db17 207 return (0);
984263bc
MD
208}
209
c7106d58
PA
210static char
211lowc(char sym)
984263bc 212{
6693db17 213 return ((sym >= 'A' && sym <= 'Z') ? sym + 'a' - 'A' : sym);
984263bc
MD
214}
215
c7106d58
PA
216static char
217unctrl(char sym)
984263bc 218{
6693db17 219 return ((sym >= ('A' & 037) && sym <= ('Z' & 037)) ? sym + 0140 : sym);
984263bc
MD
220}
221
222/* 'rogue'-like direction commands */
223char sdir[] = "hykulnjb><";
6693db17
SW
224schar xdir[10] = { -1, -1, 0, 1, 1, 1, 0, -1, 0, 0 };
225schar ydir[10] = { 0, -1, -1, -1, 0, 1, 1, 1, 0, 0 };
226schar zdir[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, -1 };
984263bc 227
c7106d58 228bool
6693db17 229movecmd(char sym) /* also sets u.dz, but returns false for <> */
984263bc
MD
230{
231 char *dp;
232
233 u.dz = 0;
6693db17
SW
234 if (!(dp = strchr(sdir, sym)))
235 return (0);
236 u.dx = xdir[dp - sdir];
237 u.dy = ydir[dp - sdir];
238 u.dz = zdir[dp - sdir];
239 return (!u.dz);
984263bc
MD
240}
241
c7106d58
PA
242bool
243getdir(bool s)
984263bc
MD
244{
245 char dirsym;
246
6693db17
SW
247 if (s)
248 pline("In what direction?");
984263bc 249 dirsym = readchar();
6693db17
SW
250 if (!movecmd(dirsym) && !u.dz) {
251 if (!strchr(quitchars, dirsym))
984263bc 252 pline("What a strange direction!");
6693db17 253 return (0);
984263bc 254 }
6693db17 255 if (Confusion && !u.dz)
984263bc 256 confdir();
6693db17 257 return (1);
984263bc
MD
258}
259
c7106d58
PA
260void
261confdir(void)
984263bc
MD
262{
263 int x = rn2(8);
6693db17 264
984263bc
MD
265 u.dx = xdir[x];
266 u.dy = ydir[x];
267}
268
269#ifdef QUEST
c7106d58
PA
270void
271finddir(void)
272{
6693db17
SW
273 int i, ui = u.di;
274
275 for (i = 0; i <= 8; i++) {
276 if (flags.run & 1)
277 ui++;
278 else
279 ui += 7;
984263bc 280 ui %= 8;
6693db17 281 if (i == 8) {
984263bc
MD
282 pline("Not near a wall.");
283 flags.move = multi = 0;
c7106d58 284 return;
984263bc 285 }
6693db17 286 if (!isroom(u.ux + xdir[ui], u.uy + ydir[ui]))
984263bc
MD
287 break;
288 }
6693db17
SW
289 for (i = 0; i <= 8; i++) {
290 if (flags.run & 1)
291 ui += 7;
292 else
293 ui++;
984263bc 294 ui %= 8;
6693db17 295 if (i == 8) {
984263bc
MD
296 pline("Not near a room.");
297 flags.move = multi = 0;
c7106d58 298 return;
984263bc 299 }
6693db17 300 if (isroom(u.ux + xdir[ui], u.uy + ydir[ui]))
984263bc
MD
301 break;
302 }
303 u.di = ui;
304 u.dx = xdir[ui];
305 u.dy = ydir[ui];
306}
307
c7106d58
PA
308static bool
309isroom(int x, int y)
6693db17
SW
310{ /* what about POOL? */
311 return (isok(x, y) && (levl[x][y].typ == ROOM ||
312 (levl[x][y].typ >= LDOOR && flags.run >= 6)));
984263bc 313}
304d60d9 314#endif /* QUEST */
984263bc 315
c7106d58
PA
316bool
317isok(int x, int y)
318{
984263bc 319 /* x corresponds to curx, so x==1 is the first column. Ach. %% */
6693db17 320 return (x >= 1 && x <= COLNO - 1 && y >= 0 && y <= ROWNO - 1);
984263bc 321}
c7106d58
PA
322
323/*
324 * done2 is a function that fits into cmdlist[] (int func(void))
325 * and calls done1 which discards its argument.
326 */
327static int
328done2(void)
329{
330 done1(0);
6693db17 331 return (0);
c7106d58 332}