Generally use NULL instead of explicitly casting 0 to some pointer type (part2).
[dragonfly.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
MD
5
6#include "hack.h"
7#include "def.func_tab.h"
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 },
984263bc
MD
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 },
984263bc
MD
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 },
984263bc
MD
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 },
984263bc 51/* 'u', 'U' : go ne */
c7106d58 52 { 'v', doversion },
984263bc 53/* 'V' : UNUSED */
c7106d58
PA
54 { 'w', dowield },
55 { 'W', doweararm },
984263bc
MD
56/* 'x', 'X' : UNUSED */
57/* 'y', 'Y' : go nw */
c7106d58 58 { 'z', dozap },
984263bc 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 },
72 { '\\', dodiscovered }, /* Robert Viduya */
73 { WEAPON_SYM, doprwep },
74 { ARMOR_SYM, doprarm },
75 { RING_SYM, doprring },
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
96 if(!cmd) {
97 firsttime = TRUE;
98 flags.nopick = 0;
99 cmd = parse();
100 }
101 if(!*cmd || (*cmd & 0377) == 0377 ||
102 (flags.no_rest_on_space && *cmd == ' ')){
103 bell();
104 flags.move = 0;
105 return; /* probably we just had an interrupt */
106 }
107 if(movecmd(*cmd)) {
108 walk:
109 if(multi) flags.mv = 1;
110 domove();
111 return;
112 }
113 if(movecmd(lowc(*cmd))) {
114 flags.run = 1;
115 rush:
116 if(firsttime){
117 if(!multi) multi = COLNO;
118 u.last_str_turn = 0;
119 }
120 flags.mv = 1;
121#ifdef QUEST
122 if(flags.run >= 4) finddir();
123 if(firsttime){
124 u.ux0 = u.ux + u.dx;
125 u.uy0 = u.uy + u.dy;
126 }
304d60d9 127#endif /* QUEST */
984263bc
MD
128 domove();
129 return;
130 }
131 if((*cmd == 'f' && movecmd(cmd[1])) || movecmd(unctrl(*cmd))) {
132 flags.run = 2;
133 goto rush;
134 }
135 if(*cmd == 'F' && movecmd(lowc(cmd[1]))) {
136 flags.run = 3;
137 goto rush;
138 }
139 if(*cmd == 'm' && movecmd(cmd[1])) {
140 flags.run = 0;
141 flags.nopick = 1;
142 goto walk;
143 }
144 if(*cmd == 'M' && movecmd(lowc(cmd[1]))) {
145 flags.run = 1;
146 flags.nopick = 1;
147 goto rush;
148 }
149#ifdef QUEST
150 if(*cmd == cmd[1] && (*cmd == 'f' || *cmd == 'F')) {
151 flags.run = 4;
152 if(*cmd == 'F') flags.run += 2;
153 if(cmd[2] == '-') flags.run += 1;
154 goto rush;
155 }
304d60d9 156#endif /* QUEST */
984263bc
MD
157 while(tlist->f_char) {
158 if(*cmd == tlist->f_char){
159 res = (*(tlist->f_funct))();
160 if(!res) {
161 flags.move = 0;
162 multi = 0;
163 }
164 return;
165 }
166 tlist++;
167 }
168 { char expcmd[10];
169 char *cp = expcmd;
c7106d58 170 while(*cmd && cp-expcmd < (int)sizeof(expcmd)-2) {
984263bc
MD
171 if(*cmd >= 040 && *cmd < 0177)
172 *cp++ = *cmd++;
173 else {
174 *cp++ = '^';
175 *cp++ = *cmd++ ^ 0100;
176 }
177 }
178 *cp++ = 0;
179 pline("Unknown command '%s'.", expcmd);
180 }
181 multi = flags.move = 0;
182}
183
c7106d58
PA
184static int
185doextcmd(void) /* here after # - now read a full-word command */
984263bc
MD
186{
187 char buf[BUFSZ];
188 struct ext_func_tab *efp = extcmdlist;
189
190 pline("# ");
191 getlin(buf);
192 clrlin();
193 if(buf[0] == '\033')
194 return(0);
195 while(efp->ef_txt) {
196 if(!strcmp(efp->ef_txt, buf))
197 return((*(efp->ef_funct))());
198 efp++;
199 }
200 pline("%s: unknown command.", buf);
201 return(0);
202}
203
c7106d58
PA
204static char
205lowc(char sym)
984263bc
MD
206{
207 return( (sym >= 'A' && sym <= 'Z') ? sym+'a'-'A' : sym );
208}
209
c7106d58
PA
210static char
211unctrl(char sym)
984263bc
MD
212{
213 return( (sym >= ('A' & 037) && sym <= ('Z' & 037)) ? sym + 0140 : sym );
214}
215
216/* 'rogue'-like direction commands */
217char sdir[] = "hykulnjb><";
218schar xdir[10] = { -1,-1, 0, 1, 1, 1, 0,-1, 0, 0 };
219schar ydir[10] = { 0,-1,-1,-1, 0, 1, 1, 1, 0, 0 };
220schar zdir[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 1,-1 };
221
c7106d58
PA
222bool
223movecmd(char sym) /* also sets u.dz, but returns false for <> */
984263bc
MD
224{
225 char *dp;
226
227 u.dz = 0;
228 if(!(dp = index(sdir, sym))) return(0);
229 u.dx = xdir[dp-sdir];
230 u.dy = ydir[dp-sdir];
231 u.dz = zdir[dp-sdir];
232 return(!u.dz);
233}
234
c7106d58
PA
235bool
236getdir(bool s)
984263bc
MD
237{
238 char dirsym;
239
240 if(s) pline("In what direction?");
241 dirsym = readchar();
242 if(!movecmd(dirsym) && !u.dz) {
243 if(!index(quitchars, dirsym))
244 pline("What a strange direction!");
245 return(0);
246 }
247 if(Confusion && !u.dz)
248 confdir();
249 return(1);
250}
251
c7106d58
PA
252void
253confdir(void)
984263bc
MD
254{
255 int x = rn2(8);
256 u.dx = xdir[x];
257 u.dy = ydir[x];
258}
259
260#ifdef QUEST
c7106d58
PA
261void
262finddir(void)
263{
984263bc
MD
264int i, ui = u.di;
265 for(i = 0; i <= 8; i++){
266 if(flags.run & 1) ui++; else ui += 7;
267 ui %= 8;
268 if(i == 8){
269 pline("Not near a wall.");
270 flags.move = multi = 0;
c7106d58 271 return;
984263bc
MD
272 }
273 if(!isroom(u.ux+xdir[ui], u.uy+ydir[ui]))
274 break;
275 }
276 for(i = 0; i <= 8; i++){
277 if(flags.run & 1) ui += 7; else ui++;
278 ui %= 8;
279 if(i == 8){
280 pline("Not near a room.");
281 flags.move = multi = 0;
c7106d58 282 return;
984263bc
MD
283 }
284 if(isroom(u.ux+xdir[ui], u.uy+ydir[ui]))
285 break;
286 }
287 u.di = ui;
288 u.dx = xdir[ui];
289 u.dy = ydir[ui];
290}
291
c7106d58
PA
292static bool
293isroom(int x, int y)
294{ /* what about POOL? */
984263bc
MD
295 return(isok(x,y) && (levl[x][y].typ == ROOM ||
296 (levl[x][y].typ >= LDOOR && flags.run >= 6)));
297}
304d60d9 298#endif /* QUEST */
984263bc 299
c7106d58
PA
300bool
301isok(int x, int y)
302{
984263bc
MD
303 /* x corresponds to curx, so x==1 is the first column. Ach. %% */
304 return(x >= 1 && x <= COLNO-1 && y >= 0 && y <= ROWNO-1);
305}
c7106d58
PA
306
307/*
308 * done2 is a function that fits into cmdlist[] (int func(void))
309 * and calls done1 which discards its argument.
310 */
311static int
312done2(void)
313{
314 done1(0);
315 return(0);
316}