-
Notifications
You must be signed in to change notification settings - Fork 1
/
command.c
724 lines (694 loc) · 14.8 KB
/
command.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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
/*
* Read and execute the user commands
*
* @(#)command.c 9.0 (rdk) 7/17/84
*
* Super-Rogue
* Copyright (C) 1984 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 <curses.h>
#include <signal.h>
#include <stdio.h>
#include <unctrl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include "rogue.h"
#include "rogue.ext"
#ifdef __DJGPP__
#include <process.h>
#endif
/*
* command:
* Process the user commands
*/
void command()
{
char ch;
int ntimes = 1; /* Number of player moves */
static char countch, direction, newcount = FALSE;
if (pl_on(ISHASTE))
ntimes++;
/*
* Let the daemons start up
*/
while (ntimes-- > 0) {
do_daemons(BEFORE);
look(TRUE);
if (!running)
door_stop = FALSE;
lastscore = purse;
wmove(cw, hero.y, hero.x);
if (!(running || count))
draw(cw); /* Draw screen */
take = 0;
after = TRUE;
/*
* Read command or continue run
*/
if (wizard)
waswizard = TRUE;
if (player.t_nocmd <= 0) {
player.t_nocmd = 0;
if (running)
ch = runch;
else if (count)
ch = countch;
else {
ch = readchar();
if (mpos != 0 && !running) /* Erase message if its there */
msg("");
}
}
else
ch = '.';
if (player.t_nocmd > 0) {
if (--player.t_nocmd <= 0)
msg("You can move again.");
}
else {
/*
* check for prefixes
*/
if (isdigit(ch)) {
count = 0;
newcount = TRUE;
while (isdigit(ch)) {
count = count * 10 + (ch - '0');
ch = readchar();
}
countch = ch;
/*
* turn off count for commands which don't make sense
* to repeat
*/
switch (ch) {
case 'h': case 'j': case 'k': case 'l':
case 'y': case 'u': case 'b': case 'n':
case 'H': case 'J': case 'K': case 'L':
case 'Y': case 'U': case 'B': case 'N':
case 'q': case 'r': case 's': case 'f':
case 't': case 'C': case 'I': case '.':
case 'z': case 'p':
break;
default:
count = 0;
}
}
switch (ch) {
case 'f':
case 'g':
if (pl_off(ISBLIND)) {
door_stop = TRUE;
firstmove = TRUE;
}
if (count && !newcount)
ch = direction;
else
ch = readchar();
switch (ch) {
case 'h': case 'j': case 'k': case 'l':
case 'y': case 'u': case 'b': case 'n':
ch = toupper(ch);
}
direction = ch;
}
newcount = FALSE;
/*
* execute a command
*/
if (count && !running)
count--;
switch (ch) {
case '!' : shell(); after = FALSE;
when 'h' : do_move(0, -1);
when 'j' : do_move(1, 0);
when 'k' : do_move(-1, 0);
when 'l' : do_move(0, 1);
when 'y' : do_move(-1, -1);
when 'u' : do_move(-1, 1);
when 'b' : do_move(1, -1);
when 'n' : do_move(1, 1);
when 'H' : do_run('h');
when 'J' : do_run('j');
when 'K' : do_run('k');
when 'L' : do_run('l');
when 'Y' : do_run('y');
when 'U' : do_run('u');
when 'B' : do_run('b');
when 'N' : do_run('n');
when 't':
if (!get_dir())
after = FALSE;
else
missile(delta.y, delta.x);
when 'Q' : after = FALSE; quit(-1);
when 'i' : after = FALSE; inventory(pack, 0);
when 'I' : after = FALSE; picky_inven();
when 'd' : drop(NULL);
when 'q' : quaff();
when 'r' : read_scroll();
when 'e' : eat();
when 'w' : wield();
when 'W' : wear();
when 'T' : take_off();
when 'P' : ring_on();
when 'R' : ring_off();
when 'O' : option();
when 'c' : call();
when '>' : after = FALSE; d_level();
when '<' : after = FALSE; u_level();
when '?' : after = FALSE; help();
when '/' : after = FALSE; identify(0);
when 's' : search();
when 'z' : do_zap(FALSE);
when 'p':
if (get_dir())
do_zap(TRUE);
else
after = FALSE;
when 'v': msg("Super Rogue version %s.",release);
when 'D': dip_it();
when CTRL('L') : after = FALSE; restscr(cw);
when CTRL('R') : after = FALSE; msg(huh);
when 'a': after = FALSE; dispmax();
when '@' : if (author())
msg("Hero @ %d,%d : Stairs @ %d,%d",hero.y,hero.x,stairs.y,stairs.x);
when 'S' :
after = FALSE;
if (save_game()) {
wclear(cw);
draw(cw);
endwin();
byebye(0);
}
when '.' : ; /* Rest command */
when ' ' : after = FALSE; /* do nothing */
when '=' :
if (author()) {
activity();
after = FALSE;
}
when CTRL('P') :
after = FALSE;
if (wizard) {
wizard = FALSE;
msg("Not wizard any more");
}
else {
wizard = passwd();
if (wizard) {
msg("Welcome back, Bob!!!!!");
waswizard = TRUE;
}
else
msg("Sorry");
}
when ESCAPE : /* Escape */
door_stop = FALSE;
count = 0;
after = FALSE;
when '#':
if (levtype == POSTLEV) /* buy something */
buy_it();
after = FALSE;
when '$':
if (levtype == POSTLEV) /* price something */
price_it();
after = FALSE;
when '%':
if (levtype == POSTLEV) /* sell something */
sell_it();
after = FALSE;
otherwise :
after = FALSE;
if (wizard) switch (ch) {
case CTRL('A') : ;
when 'C' : create_obj(FALSE);
when CTRL('I') : inventory(lvl_obj, 1);
when CTRL('W') : whatis(NULL);
when CTRL('D') : level++; new_level(NORMLEV);
when CTRL('U') : if (level > 1) level--; new_level(NORMLEV);
when CTRL('F') : displevl();
when CTRL('X') : dispmons();
when CTRL('T') : teleport(rndspot,&player);
when CTRL('E') : msg("food left: %d", food_left);
when CTRL('O') : add_pass();
when 'M' : {
int tlev, whichlev;
prbuf[0] = '\0';
msg("Which level? ");
if (get_str(prbuf,cw) == NORM) {
whichlev = NORMLEV;
tlev = atoi(prbuf);
if (tlev < 1)
level = 1;
if (tlev >= 200) {
tlev -= 199;
whichlev = MAZELEV;
}
else if (tlev >= 100) {
tlev -= 99;
whichlev = POSTLEV;
}
level = tlev;
new_level(whichlev);
}
}
when CTRL('N') : {
struct linked_list *item;
item = get_item("charge", STICK);
if (item != NULL) {
(OBJPTR(item))->o_charges = 10000;
msg("");
}
}
when CTRL('H') : {
int i;
struct linked_list *item;
struct object *obj;
him->s_exp = e_levels[him->s_lvl + 7] + 1;
check_level();
/*
* Give the rogue a very good sword
*/
item = new_thing(FALSE, WEAPON, TWOSWORD);
obj = OBJPTR(item);
obj->o_hplus = 3;
obj->o_dplus = 3;
obj->o_flags = ISKNOW;
i = add_pack(item, TRUE);
if (i)
cur_weapon = obj;
else
discard(item);
/*
* And his suit of armor
*/
item = new_thing(FALSE, ARMOR, PLATEARMOR);
obj = OBJPTR(item);
obj->o_ac = -8;
obj->o_flags = ISKNOW;
i = add_pack(item, TRUE);
if (i)
cur_armor = obj;
else
discard(item);
nochange = FALSE;
}
otherwise:
msg(illegal, unctrl(ch));
count = 0;
}
else {
msg(illegal, unctrl(ch));
count = 0;
}
}
/*
* turn off flags if no longer needed
*/
if (!running)
door_stop = FALSE;
}
/*
* If he ran into something to take, let the
* hero pick it up if not in a trading post.
*/
if (take != 0 && levtype != POSTLEV)
pick_up(take);
if (!running)
door_stop = FALSE;
}
/*
* Kick off the rest if the daemons and fuses
*/
if (after) {
int j;
look(FALSE);
do_daemons(AFTER);
do_fuses();
if (pl_on(ISSLOW))
waste_time();
for (j = LEFT; j <= RIGHT; j++) {
if (cur_ring[j] != NULL) {
if (cur_ring[j]->o_which == R_SEARCH)
search();
else if (cur_ring[j]->o_which == R_TELEPORT)
if (rnd(100) < 5)
teleport(rndspot, &player);
}
}
}
}
/*
* quit:
* Have player make certain, then exit.
*/
void quit(int a)
{
char ch, good;
/*
* Reset the signal in case we got here via an interrupt
*/
if (signal(SIGINT, quit) != quit)
mpos = 0;
msg("Really quit? [y/n/s]");
/* ch = tolower(readchar());*/
ch = readchar();
if (ch == 'y') {
clear();
move(LINES-1, 0);
refresh();
score(purse, CHICKEN, 0);
byebye(0);
}
else if (ch == 's') {
good = save_game();
if (good) {
wclear(cw);
draw(cw);
endwin();
byebye(0);
}
}
else {
signal(SIGINT, quit);
wmove(cw, 0, 0);
wclrtoeol(cw);
draw(cw);
mpos = 0;
count = 0;
nochange = FALSE;
}
}
/*
* search:
* Player gropes about him to find hidden things.
*/
int search()
{
int x, y;
char ch;
/*
* Look all around the hero, if there is something hidden there,
* give him a chance to find it. If its found, display it.
*/
if (pl_on(ISBLIND))
return 0;
for (x = hero.x - 1; x <= hero.x + 1; x++) {
for (y = hero.y - 1; y <= hero.y + 1; y++) {
ch = winat(y, x);
if (isatrap(ch)) { /* see if its a trap */
struct trap *tp;
if ((tp = trap_at(y, x)) == NULL)
break;
if (tp->tr_flags & ISFOUND)
break; /* no message if its seen */
if (mvwinch(cw, y, x) == ch)
break;
if (rnd(100) > (him->s_lvl * 9 + herowis() * 5))
break;
tp->tr_flags |= ISFOUND;
mvwaddch(cw, y, x, tp->tr_type);
count = 0;
running = FALSE;
msg(tr_name(tp->tr_type));
}
else if(ch == SECRETDOOR) {
if (rnd(100) < (him->s_lvl * 4 + herowis() * 5)) {
mvaddch(y, x, DOOR);
count = 0;
}
}
}
}
return 0;
}
/*
* help:
* Give single character help, or the whole mess if he wants it
*/
int help()
{
extern struct h_list helpstr[];
struct h_list *strp;
char helpch;
int cnt;
strp = &helpstr[0];
msg("Character you want help for (* for all): ");
helpch = readchar();
mpos = 0;
/*
* If its not a *, print the right help string
* or an error if he typed a funny character.
*/
if (helpch != '*') {
wmove(cw, 0, 0);
while (strp->h_ch) {
if (strp->h_ch == helpch) {
msg("%s%s", unctrl(strp->h_ch), strp->h_desc);
break;
}
strp++;
}
if (strp->h_ch != helpch)
msg("Unknown character '%s'", unctrl(helpch));
return 0;
}
/*
* Here we print help for everything.
* Then wait before we return to command mode
*/
wclear(hw);
cnt = 0;
while (strp->h_ch) {
mvwaddstr(hw, cnt % 23, cnt > 22 ? 40 : 0, unctrl(strp->h_ch));
waddstr(hw, strp->h_desc);
cnt++;
strp++;
}
wmove(hw, LINES-1, 0);
wprintw(hw,spacemsg);
draw(hw);
wait_for(hw,' ');
wclear(hw);
draw(hw);
wmove(cw, 0, 0);
wclrtoeol(cw);
touchwin(cw);
nochange = FALSE;
return 0;
}
/*
* identify:
* Tell the player what a certain thing is.
*/
char *identify(int what)
{
char ch, *str;
if (what == 0) {
msg("What do you want identified? ");
ch = readchar();
mpos = 0;
if (ch == ESCAPE) {
msg("");
return NULL;
}
}
else
ch = what;
if (isalpha(ch))
str = monsters[midx(ch)].m_name;
else {
switch(ch) {
case '|':
case '-': str = "the wall of a room";
when GOLD: str = "gold";
when STAIRS: str = "passage leading up/down";
when DOOR: str = "door";
when FLOOR: str = "room floor";
when PLAYER: str = "you";
when PASSAGE: str = "passage";
when POST: str = "trading post";
when MAZETRAP: str = "maze trap";
when TRAPDOOR: str = "trapdoor";
when ARROWTRAP: str = "arrow trap";
when SLEEPTRAP: str = "sleeping gas trap";
when BEARTRAP: str = "bear trap";
when TELTRAP: str = "teleport trap";
when DARTTRAP: str = "dart trap";
when POOL: str = "magic pool";
when POTION: str = "potion";
when SCROLL: str = "scroll";
when FOOD: str = "food";
when WEAPON: str = "weapon";
when ' ' : str = "solid rock";
when ARMOR: str = "armor";
when AMULET: str = "The Amulet of Yendor";
when RING: str = "ring";
when STICK: str = "wand or staff";
otherwise:
if (what == 0)
str = "unknown character";
else
str = "a magical ghost";
}
}
if (what == 0)
msg("'%s' : %s", unctrl(ch), str);
return str;
}
/*
* d_level:
* He wants to go down a level
*/
int d_level()
{
if (winat(hero.y, hero.x) != STAIRS)
msg("I see no way down.");
else {
if (pl_on(ISHELD)) {
msg("You are being held.");
return 0;
}
level++;
new_level(NORMLEV);
}
return 0;
}
/*
* u_level:
* He wants to go up a level
*/
int u_level()
{
if (winat(hero.y, hero.x) == STAIRS) {
if (pl_on(ISHELD)) {
msg("You are being held.");
return 0;
}
else { /* player not held here */
if (amulet) {
level--;
if (level == 0)
total_winner();
new_level(NORMLEV);
msg("You feel a wrenching sensation in your gut.");
return 0;
}
}
}
msg("I see no way up.");
return 0;
}
/*
* Let him escape for a while
*/
void shell()
{
int pid;
char *sh;
int ret_status;
/*
* Set the terminal back to original mode
*/
sh = getenv("SHELL");
wclear(hw);
wmove(hw, LINES-1, 0);
draw(hw);
endwin();
in_shell = TRUE;
fflush(stdout);
/*
* Fork and do a shell
*/
#ifndef __DJGPP__
while((pid = fork()) < 0)
sleep(1);
if (pid == 0) {
setuid(playuid); /* Set back to original user */
setgid(playgid);
execl(sh == NULL ? "/bin/sh" : sh, "shell", "-i", 0);
perror("No shelly");
byebye(-1);
}
else {
signal(SIGINT, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
while (wait(&ret_status) != pid)
continue;
signal(SIGINT, quit);
signal(SIGQUIT, endit);
#else
{
char shell[PATH_MAX];
if (sh && *sh)
strncpy(shell,sh,PATH_MAX);
else
sprintf(shell, "%s\\bin\\sh.exe", getenv("DJDIR"));
if (spawnl(P_WAIT,shell, "shell", "-i", 0) == -1)
msg("No shelly: %s", shell);
#endif
printf("\n%s", retstr);
fflush(stdout);
noecho();
crmode();
in_shell = FALSE;
wait_for(cw, '\n');
restscr(cw);
}
}
/*
* call:
* Allow a user to call a potion, scroll, or ring something
*/
int call()
{
struct object *obj;
struct linked_list *item;
char **guess, *elsewise;
int wh;
if ((item = get_item("call", 0)) == NULL)
return 0;
obj = OBJPTR(item);
wh = obj->o_which;
switch (obj->o_type) {
case RING:
guess = r_guess;
elsewise = (r_guess[wh] != NULL ? r_guess[wh] : r_stones[wh]);
when POTION:
guess = p_guess;
elsewise = (p_guess[wh] != NULL ? p_guess[wh] : p_colors[wh]);
when SCROLL:
guess = s_guess;
elsewise = (s_guess[wh] != NULL ? s_guess[wh] : s_names[wh]);
when STICK:
guess = ws_guess;
elsewise =(ws_guess[wh] != NULL ?
ws_guess[wh] : ws_stuff[wh].ws_made);
otherwise:
msg("You can't call %ss anything",obj->o_typname);
return 0;
}
msg("Was called \"%s\"", elsewise);
msg(callit);
if (guess[wh] != NULL)
free(guess[wh]);
strcpy(prbuf, elsewise);
if (get_str(prbuf, cw) == NORM) {
guess[wh] = new(strlen(prbuf) + 1);
strcpy(guess[wh], prbuf);
}
return 0;
}