-
Notifications
You must be signed in to change notification settings - Fork 4
/
options.c
550 lines (503 loc) · 10.6 KB
/
options.c
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/*
options.c - This file has all the code for the option command.
Last Modified: Dec 29, 1990
UltraRogue
Copyright (C) 1984, 1985, 1986, 1987, 1990 Herb Chong
All rights reserved.
Based on "Advanced Rogue"
Copyright (C) 1983, 1984 Michael Morgan, Ken Dalka and AT&T
All rights reserved.
Based on "Super-Rogue"
Copyright (C) 1982, 1983 Robert D. Kindelberger
All rights reserved.
Based on "Rogue: Exploring the Dungeons of Doom"
Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
All rights reserved.
See the file LICENSE.TXT for full copyright and licensing information.
*/
#include <ctype.h>
#include "rogue.h"
#define NUM_OPTS (sizeof optlist / sizeof (OPTION))
#define EQSTR(a, b, c) (strncmp(a, b, c) == 0)
/*
* description of an option and what to do with it
*/
struct optstruct {
char *o_name; /* option name */
char *o_prompt; /* prompt for interactive entry */
bool *o_opt; /* pointer to thing to set */
int (*o_putfunc) ();/* function to print value */
int (*o_getfunc) ();/* function to get value interactively */
};
typedef struct optstruct OPTION;
int put_bool(), get_bool(), put_str(), get_str(), put_abil(), get_abil(),
put_inv(), get_inv();
OPTION optlist[] = {
{"terse", "Terse output (terse): ",
&terse, put_bool, get_bool},
{"flush", "Flush typeahead during battle (flush): ",
&fight_flush, put_bool, get_bool},
{"jump", "Show position only at end of run (jump): ",
&jump, put_bool, get_bool},
{"inven", "Style of inventories (inven): ",
&inv_type, put_inv, get_inv},
{"askme", "Ask me about unidentified things (askme): ",
&askme, put_bool, get_bool},
{"doorstop", "Stop running when adjacent (doorstop): ",
&doorstop, put_bool, get_bool},
{"name", "Name (name): ",
whoami, put_str, get_str},
{"fruit", "Fruit (fruit): ",
fruit, put_str, get_str},
{"file", "Save file (file): ",
file_name, put_str, get_str},
{"score", "Score file (score): ",
score_file, put_str, get_str},
{"class", "Character class (class): ",
&char_type, put_abil, get_abil}
};
/*
* print and then set options from the terminal
*/
option()
{
OPTION *op;
int retval;
wclear(hw);
touchwin(hw);
/*
* Display current values of options
*/
for (op = optlist; op < &optlist[NUM_OPTS]; op++) {
waddstr(hw, op->o_prompt);
(*op->o_putfunc) (op->o_opt, hw);
waddch(hw, '\n');
}
/*
* Set values
*/
wmove(hw, 0, 0);
for (op = optlist; op < &optlist[NUM_OPTS]; op++) {
waddstr(hw, op->o_prompt);
if ((retval = (*op->o_getfunc) (op->o_opt, hw)))
if (retval == QUIT)
break;
else if (op > optlist) { /* MINUS */
wmove(hw, (op - optlist) - 1, 0);
op -= 2;
}
else { /* trying to back up beyond the top */
putchar('\007');
wmove(hw, 0, 0);
op--;
}
}
/*
* Switch back to original screen
*/
mvwaddstr(hw, LINES - 1, 0, spacemsg);
wrefresh(hw);
wait_for(' ');
clearok(cw, TRUE);
touchwin(cw);
}
/*
* put out a boolean
*/
put_bool(b, win)
bool *b;
WINDOW *win;
{
waddstr(win, *b ? "True" : "False");
}
/*
* put out a string
*/
put_str(str, win)
char *str;
WINDOW *win;
{
waddstr(win, str);
}
/*
* print the character type
*/
put_abil(ability, win)
bool *ability;
WINDOW *win;
{
char *abil;
switch (*ability) {
case C_FIGHTER:
abil = "Fighter";
break;
case C_MAGICIAN:
abil = "Magic User";
break;
case C_CLERIC:
abil = "Cleric";
break;
case C_THIEF:
abil = "Thief";
break;
case C_PALADIN:
abil = "Paladin";
break;
case C_RANGER:
abil = "Ranger";
break;
case C_ILLUSION:
abil = "Illusionist";
break;
case C_ASSASIN:
abil = "Assasin";
break;
case C_NINJA:
abil = "Ninja";
break;
case C_DRUID:
abil = "Druid";
break;
default:
abil = "(unknown)";
}
waddstr(win, abil);
}
/*
* allow changing a boolean option and print it out
*/
get_bool(bp, win)
bool *bp;
WINDOW *win;
{
int oy, ox;
bool op_bad;
op_bad = TRUE;
getyx(win, oy, ox);
waddstr(win, *bp ? "True" : "False");
while (op_bad) {
wmove(win, oy, ox);
wrefresh(win);
switch (readchar()) {
case 't':
case 'T':
*bp = TRUE;
op_bad = FALSE;
break;
case 'f':
case 'F':
*bp = FALSE;
op_bad = FALSE;
break;
case '\n':
case '\r':
op_bad = FALSE;
break;
case '\033':
case '\007':
return QUIT;
case '-':
return MINUS;
default:
mvwaddstr(win, oy, ox + 10, "(T or F)");
}
}
wmove(win, oy, ox);
wclrtoeol(win);
waddstr(win, *bp ? "True" : "False");
waddch(win, '\n');
return NORM;
}
/*
* set a string option
*/
get_str(opt, win)
char *opt;
WINDOW *win;
{
char *sp;
int c, oy, ox;
char buf[2 * LINELEN];
wrefresh(win);
getyx(win, oy, ox);
/*
* loop reading in the string, and put it in a temporary buffer
*/
for (sp = buf;
(c = readchar()) != '\n' &&
c != '\r' &&
c != '\033' &&
c != '\007' &&
sp < &buf[LINELEN - 1];
wclrtoeol(win), wrefresh(win)) {
if (c == -1)
continue;
else if (c == '\b') {
if (sp > buf) {
int i;
sp--;
for (i = strlen(unctrl(*sp)); i; i--)
waddch(win, '\b');
}
continue;
}
else if (c == '\0') {
sp = buf;
wmove(win, oy, ox);
continue;
}
else if (sp == buf && c == '-' && win == hw)
break;
*sp++ = c;
waddstr(win, unctrl(c));
}
*sp = '\0';
if (sp > buf) /* only change option if something has been
* typed */
strcpy(opt, buf, strlen(buf));
wmove(win, oy, ox);
waddstr(win, opt);
waddch(win, '\n');
wrefresh(win);
if (win == cw)
mpos += sp - buf;
if (c == '-')
return MINUS;
else if (c == '\033' || c == '\007')
return QUIT;
else
return NORM;
}
/*
* The ability field is read-only
*/
get_abil(abil, win)
bool *abil;
WINDOW *win;
{
int oy, ox, ny, nx;
bool op_bad;
op_bad = TRUE;
getyx(win, oy, ox);
put_abil(abil, win);
getyx(win, ny, nx);
while (op_bad) {
wmove(win, oy, ox);
wrefresh(win);
switch (readchar()) {
case '\n':
case '\r':
op_bad = FALSE;
break;
case '\033':
case '\007':
return QUIT;
case '-':
return MINUS;
default:
mvwaddstr(win, ny, nx + 5,
"(no change allowed)");
}
}
wmove(win, ny, nx + 5);
wclrtoeol(win);
wmove(win, ny, nx);
waddch(win, '\n');
return NORM;
}
/*
* parse options from string, usually taken from the environment. the string
* is a series of comma seperated values, with booleans being stated as
* "name" (true) or "noname" (false), and strings being "name=....", with the
* string being defined up to a comma or the end of the entire option string.
*/
parse_opts(str)
char *str;
{
char *sp;
OPTION *op;
int len;
while (*str) {
for (sp = str; isalpha(*sp); sp++)
continue;
len = sp - str;
/*
* Look it up and deal with it
*/
for (op = optlist; op < &optlist[NUM_OPTS]; op++)
if (EQSTR(str, op->o_name, len)) {
if (op->o_putfunc == put_bool)
*(bool *) op->o_opt = TRUE;
else { /* string option */
char *start;
char value[80];
/*
* Skip to start of string value
*/
for (str = sp + 1; *str == '='; str++)
continue;
start = (char *) value;
/*
* Skip to end of string value
*/
for (sp = str + 1; *sp && *sp != ',';
sp++)
continue;
strcpy(start, str, sp - str);
/*
* Put the value into the option
* field
*/
if (op->o_putfunc != put_abil &&
op->o_putfunc != put_inv)
strcpy(op->o_opt, value);
if (op->o_putfunc == put_inv) {
int len = strlen(value);
if (isupper(value[0]))
value[0] = tolower(value[0]);
if (EQSTR(value, "overwrite",
len))
*op->o_opt = INV_OVER;
if (EQSTR(value, "slow", len))
*op->o_opt = INV_SLOW;
if (EQSTR(value, "clear", len))
*op->o_opt = INV_CLEAR;
}
else if (*op->o_opt == -1) {
int len = strlen(value);
if (isupper(value[0]))
value[0] = tolower(value[0]);
if (EQSTR(value, "fighter",
len))
*op->o_opt = C_FIGHTER;
else if (EQSTR(value, "magic",
min(len, 5)))
*op->o_opt = C_MAGICIAN;
else if (EQSTR(value, "illus",
min(len, 5)))
*op->o_opt = C_ILLUSION;
else if (EQSTR(value, "cleric",
len))
*op->o_opt = C_CLERIC;
else if (EQSTR(value, "thief",
len))
*op->o_opt = C_THIEF;
else if (EQSTR(value, "paladin",
len))
*op->o_opt = C_PALADIN;
else if (EQSTR(value, "ranger",
len))
*op->o_opt = C_RANGER;
else if (EQSTR(value, "assasin",
len))
*op->o_opt = C_ASSASIN;
else if (EQSTR(value, "druid",
len))
*op->o_opt = C_DRUID;
else if (EQSTR(value, "ninja",
len))
*op->o_opt = C_NINJA;
}
}
break;
}
else if (op->o_putfunc == put_bool
&& EQSTR(str, "no", 2) &&
EQSTR(str + 2, op->o_name, len - 2)) {
*(bool *) op->o_opt = FALSE;
break;
}
/*
* skip to start of next option name
*/
while (*sp && !isalpha(*sp))
sp++;
str = sp;
}
}
/*
* print the inventory type
*/
put_inv(inv, win)
bool *inv;
WINDOW *win;
{
char *style;
switch (*inv) {
case INV_OVER:
style = "Overwrite";
break;
case INV_SLOW:
style = "Slow";
break;
case INV_CLEAR:
style = "Clear Screen";
break;
default:
style = "(unknown)";
}
waddstr(win, style);
}
/*
* The inventory field.
*/
get_inv(inv, win)
bool *inv;
WINDOW *win;
{
int oy, ox, ny, nx;
bool op_bad;
op_bad = TRUE;
getyx(win, oy, ox);
put_inv(inv, win);
getyx(win, ny, nx);
while (op_bad) {
wmove(win, oy, ox);
wrefresh(win);
switch (readchar()) {
case '\n':
case '\r':
op_bad = FALSE;
break;
case '\033':
case '\007':
return QUIT;
case '-':
return MINUS;
case 'O':
case 'o':
*inv = INV_OVER;
op_bad = FALSE;
break;
case 'S':
case 's':
*inv = INV_SLOW;
op_bad = FALSE;
break;
case 'C':
case 'c':
*inv = INV_CLEAR;
op_bad = FALSE;
break;
default:
mvwaddstr(win, ny, nx + 5, "(Use: o, s, or c)");
}
}
wmove(win, oy, ox);
wclrtoeol(win);
switch (*inv) {
case INV_SLOW:
waddstr(win, "Slow\n");
break;
case INV_CLEAR:
waddstr(win, "Clear Screen\n");
break;
case INV_OVER:
waddstr(win, "Overwrite\n");
break;
default:
waddstr(win, "Unknown\n");
break;
}
return NORM;
}