forked from rogueforge/rogomatic14
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommand.c
More file actions
410 lines (328 loc) · 9.38 KB
/
command.c
File metadata and controls
410 lines (328 loc) · 9.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* Rog-O-Matic
* Automatically exploring the dungeons of doom.
*
* Copyright (C) 2008 by Anthony Molinaro
* Copyright (C) 1985 by Appel, Jacobson, Hamey, and Mauldin.
*
* This file is part of Rog-O-Matic.
*
* Rog-O-Matic is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rog-O-Matic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Rog-O-Matic. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* command.c:
*
* This file contains all of the functions which send commands to
* Rogue, this file and 'things.c' make up the effector interface.
*/
# include <curses.h>
# include <ctype.h>
# include <string.h>
# include <stdarg.h>
# include <stdlib.h>
# include "types.h"
# include "globals.h"
# define EQUAL 0
/* static declarations */
static int cmdonscreen = 0;
static int commandcount (char *cmd);
static int functionesc (char *cmd);
static char commandarg (char *cmd, int n);
static void adjustpack (char *cmd);
static void bumpsearchcount (void);
static void clearcommand (void);
static void usemsg (char *str, int obj);
/* Move one square in direction 'd' */
void
move1 (int d)
{
command (T_MOVING, "%c", keydir[d]);
}
/* Move in direction 'd' until we find something */
void
fmove (int d)
{
if (version < RV53A) command (T_MOVING, "f%c", keydir[d]);
else command (T_MOVING, "%c", ctrl (keydir[d]));
}
/* Move 'count' squares in direction 'd', with time use mode 'mode' */
void
rmove (int count, int d, int mode)
{
command (mode, "%d%c", count, keydir[d]);
}
/* Move one square in direction 'd' without picking anything up */
void
mmove (int d, int mode)
{
command (mode, "m%c", keydir[d]);
}
/*
* command: Send a command which takes Rogue time to execute. These
* include movement commands, sitting, and physical actions. Actions which
* gather information are sent to Rogue using the 'send' function.
*/
void
command (int tmode, char *f, ...)
{
int times;
char cmd[MU_BUF + 1]; /* rogue command, +1 for paranoia */
va_list ap;
/* zeroize arrays */
memset (cmd, 0, sizeof(cmd)); /* paranoia */
/* Build the command */
va_start (ap, f);
vsnprintf (cmd, MU_BUF, f, ap);
va_end (ap);
debuglog ("command : command (%s)\n",cmd);
/* Echo the command if in transparent mode */
if (transparent) showcommand (cmd);
else if (cmdonscreen) clearcommand ();
/* Figure out whether and in which direction we are moving */
switch ((functionchar (cmd) & 037) | 0100) {
case 'L': movedir = 0; wakemonster (movedir); break;
case 'U': movedir = 1; wakemonster (movedir); break;
case 'K': movedir = 2; wakemonster (movedir); break;
case 'Y': movedir = 3; wakemonster (movedir); break;
case 'H': movedir = 4; wakemonster (movedir); break;
case 'B': movedir = 5; wakemonster (movedir); break;
case 'J': movedir = 6; wakemonster (movedir); break;
case 'N': movedir = 7; wakemonster (movedir); break;
default: movedir = NOTAMOVE;
}
/* If command takes time to execute, mark monsters as sleeping */
/* If they move, wakemonsters will mark them as awake */
if (tmode != T_OTHER)
sleepmonster ();
/* Do time accounting */
times = commandcount (cmd);
if (tmode < T_OTHER || tmode >= T_LISTLEN) tmode = T_OTHER;
turns += times;
timespent[Level].timestamp = turns;
timespent[Level].activity[tmode] += times > 1 ? times : 1;
/* Do the inventory stuff */
if (movedir == NOTAMOVE)
adjustpack (cmd);
else
diddrop = 0;
/* If we have a ring of searching, take that into account */
if (wearing ("searching") != NONE)
bumpsearchcount ();
rogo_send (cmd);
va_end (ap);
}
/*
* commandcount: Return the number of a times a command is to happen.
*/
static int
commandcount (char *cmd)
{
int times = atoi (cmd);
return (max (times, 1));
}
/*
* functionchar: return the function character of a command.
*/
char
functionchar (char *cmd)
{
char *s = cmd;
while (ISDIGIT (*s) || *s == 'f') s++;
return (*s);
}
/*
* functionesc: return true if the next character after
* a function letter is an ESC.
*/
static int
functionesc (char *cmd)
{
char *s = cmd;
while (ISDIGIT (*s) || *s == 'f') s++;
s++;
return ((*s == ESC));
}
/*
* commandarg: return the nth argument of a command.
*/
static char
commandarg (char *cmd, int n)
{
char *s = cmd;
while (ISDIGIT (*s) || *s == 'f') s++;
return (s[n]);
}
/*
* adjustpack: adjust pack in accordance with command.
*/
static void
adjustpack (char *cmd)
{
int neww, obj;
switch (functionchar (cmd)) {
case 'd': if (!diddrop) {
setrc (STUFF | USELESS, atrow, atcol);
deleteinv (OBJECT (commandarg (cmd, 1)));
}
break;
case 'e': removeinv (OBJECT (commandarg (cmd, 1)));
Ms[0] = 'X'; newring = 1;
lastate = turns;
break;
case 'i': doresetinv ();
break;
case 'q': lastobj = OBJECT (commandarg (cmd, 1));
usemsg ("Quaffing", lastobj);
strcpy (lastname, inven[lastobj].str);
useobj (inven[lastobj].str);
removeinv (lastobj);
break;
case 'r': lastobj = OBJECT (commandarg (cmd, 1));
usemsg ("Reading", lastobj);
strcpy (lastname, inven[lastobj].str);
useobj (inven[lastobj].str);
removeinv (lastobj);
break;
case 't': removeinv (OBJECT (commandarg (cmd, 2)));
hitstokill -= 1; /* Don't blame weapon if arrow misses */
break;
case 'w': if (!functionesc (cmd)) {
if (currentweapon != NONE)
forget (currentweapon, INUSE);
neww = OBJECT (commandarg (cmd, 1));
usemsg ("About to wield", neww);
if (commandarg (cmd, 2) == 'w')
{ lastdrop = currentweapon = neww; }
else
{ lastdrop = currentweapon; currentweapon = neww; }
remember (currentweapon, INUSE);
usingarrow = (inven[currentweapon].type == missile);
goodweapon = (weaponclass (currentweapon) >= 100);
badarrow = goodarrow = poorarrow = hitstokill = 0;
newweapon = 1;
setbonuses ();
}
break;
case 'p': case 'z':
lastwand = OBJECT (commandarg (cmd, 2));
usemsg ("Pointing", lastwand);
strcpy (lastname, inven[lastwand].str);
useobj (inven[lastwand].str);
/* Update number of charges */
if (inven[lastwand].charges > 0) {
if (version >= RV52A &&
stlmatch (inven[lastwand].str, "striking"))
inven[lastwand].charges -= 2;
else
inven[lastwand].charges--;
}
hitstokill -= 1; /* Don't blame weapon if wand misses */
break;
case 's': bumpsearchcount ();
break;
case 'P': obj = OBJECT (commandarg (cmd, 1));
usemsg ("Putting on", obj);
if (commandarg (cmd, 2) == 'l') leftring = obj;
else if (commandarg (cmd, 2) == 'r') rightring = obj;
else if (leftring == NONE) leftring = obj;
else rightring = obj;
/* Check for putting on see invisible */
if (streq (inven[obj].str, "see invisible"))
{ beingstalked = 0; putonseeinv = turns; }
remember (obj, INUSE);
setbonuses ();
newarmor = 1;
break;
case 'R': if (commandarg (cmd, 1) == 'l')
{ lastdrop = leftring; leftring = NONE; }
else if (commandarg (cmd, 1) == 'r')
{ lastdrop = rightring; rightring = NONE; }
else if (leftring != NONE)
{ lastdrop = leftring; leftring = NONE; }
else
{ lastdrop = rightring; rightring = NONE; }
usemsg ("Taking off", lastdrop);
forget (lastdrop, INUSE);
setbonuses ();
newarmor = 1;
break;
case 'T': lastdrop = currentarmor;
usemsg ("About to take off", currentarmor);
forget (currentarmor, INUSE);
currentarmor = NONE;
newarmor = 1;
break;
case 'W': currentarmor = OBJECT (commandarg (cmd, 1));
usemsg ("About to wear", currentarmor);
remember (currentarmor, INUSE);
newarmor = 1;
break;
}
}
/*
* bumpsearchcount: Note that we just searched this square.
*/
static void
bumpsearchcount (void)
{
int dr, dc;
for (dr = -1; dr <= 1; dr++)
for (dc = -1; dc <= 1; dc++)
timessearched[atrow+dr][atcol+dc]++;
}
/*
* replaycommand: Find the old command in the log file and send it.
*/
int
replaycommand (void)
{
char oldcmd[128];
getoldcommand (oldcmd);
command (T_OTHER, oldcmd);
return (1);
}
/*
* showcommand: Echo a string in the lower right hand corner.
* clearcommand: Remove the command we showed.
*/
void
showcommand (char *cmd)
{
char *s;
int i = 72;
at (23,72); standout (); printw (" ");
for (s=cmd; *s; s++) {
if ((i + strlen (unctrl(*s))) < 78) {
printw ("%s", unctrl (*s));
}
i += strlen (unctrl(*s));
}
printw (" "); standend (); clrtoeol (); at (row, col); refresh ();
cmdonscreen = 1;
}
static void
clearcommand (void)
{
at (23,72); clrtoeol (); at (row, col);
cmdonscreen = 0;
}
/*
* usemsg: About to use an item, tell the user.
*/
static void
usemsg (char *str, int obj)
{
if (! dwait (D_INFORM, "%s (%s", str, itemstr (obj)))
saynow ("%s (%s", str, itemstr (obj));
}