c85020a14e66ef810906a7b6d3d808e4c789b553
[rogue-pphs.git] / command.c
1 /*
2 * Read and execute the user commands
3 *
4 * @(#)command.c 3.45 (Berkeley) 6/15/81
5 *
6 * Rogue: Exploring the Dungeons of Doom
7 * Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
8 * All rights reserved.
9 *
10 * See the file LICENSE.TXT for full copyright and licensing information.
11 */
12
13 #include "curses.h"
14 #include <stdlib.h>
15 #include <ctype.h>
16 #include <signal.h>
17 #include <string.h>
18 #include "rogue.h"
19
20 /*
21 * command:
22 * Process the user commands
23 */
24
25 command()
26 {
27 register char ch;
28 register int ntimes = 1; /* Number of player moves */
29 static char countch, direction, newcount = FALSE;
30
31
32 if (on(player, ISHASTE)) ntimes++;
33 /*
34 * Let the daemons start up
35 */
36 do_daemons(BEFORE);
37 do_fuses(BEFORE);
38 while (ntimes--)
39 {
40 look(TRUE);
41 if (!running)
42 door_stop = FALSE;
43 status();
44 lastscore = purse;
45 wmove(cw, hero.y, hero.x);
46 if (!((running || count) && jump))
47 draw(cw); /* Draw screen */
48 take = 0;
49 after = TRUE;
50 /*
51 * Read command or continue run
52 */
53 if (wizard)
54 waswizard = TRUE;
55 if (!no_command)
56 {
57 if (running) ch = runch;
58 else if (count) ch = countch;
59 else
60 {
61 ch = readchar(cw);
62 if (mpos != 0 && !running) /* Erase message if its there */
63 msg("");
64 }
65 }
66 else ch = ' ';
67 if (no_command)
68 {
69 if (--no_command == 0)
70 msg("You can move again.");
71 }
72 else
73 {
74 /*
75 * check for prefixes
76 */
77 if (isdigit(ch))
78 {
79 count = 0;
80 newcount = TRUE;
81 while (isdigit(ch))
82 {
83 count = count * 10 + (ch - '0');
84 ch = readchar(cw);
85 }
86 countch = ch;
87 /*
88 * turn off count for commands which don't make sense
89 * to repeat
90 */
91 switch (ch) {
92 case 'h': case 'j': case 'k': case 'l':
93 case 'y': case 'u': case 'b': case 'n':
94 case 'H': case 'J': case 'K': case 'L':
95 case 'Y': case 'U': case 'B': case 'N':
96 case 'q': case 'r': case 's': case 'f':
97 case 't': case 'C': case 'I': case ' ':
98 case 'z': case 'p':
99 break;
100 default:
101 count = 0;
102 }
103 }
104 newcount = FALSE;
105 /*
106 * execute a command
107 */
108 if (count && !running)
109 count--;
110 switch (ch)
111 {
112 case '!' : shell();
113 when 'h' : do_move(0, -1);
114 when 'j' : do_move(1, 0);
115 when 'k' : do_move(-1, 0);
116 when 'l' : do_move(0, 1);
117 when 'y' : do_move(-1, -1);
118 when 'u' : do_move(-1, 1);
119 when 'b' : do_move(1, -1);
120 when 'n' : do_move(1, 1);
121 when 'H' : do_run('h');
122 when 'J' : do_run('j');
123 when 'K' : do_run('k');
124 when 'L' : do_run('l');
125 when 'Y' : do_run('y');
126 when 'U' : do_run('u');
127 when 'B' : do_run('b');
128 when 'N' : do_run('n');
129 when 't':
130 if (!get_dir())
131 after = FALSE;
132 else
133 missile(delta.y, delta.x);
134 when 'Q' : after = FALSE; quit(0);
135 when 'i' : after = FALSE; inventory(pack, 0);
136 when 'I' : after = FALSE; picky_inven();
137 when 'd' : drop();
138 when 'q' : quaff();
139 when 'r' : read_scroll();
140 when 'e' : eat();
141 when 'w' : wield();
142 when 'W' : wear();
143 when 'T' : take_off();
144 when 'P' : ring_on();
145 when 'R' : ring_off();
146 when 'o' : option();
147 when 'c' : call();
148 when 'f':
149 if (!get_dir())
150 after = FALSE;
151 else
152 flirt(delta.y, delta.x);
153 when '>' : after = FALSE; d_level();
154 when '<' : after = FALSE; u_level();
155 when '?' : after = FALSE; help();
156 when '/' : after = FALSE; identify();
157 when 's' : search();
158 when 'z' : do_zap(FALSE);
159 when 'p':
160 if (get_dir())
161 do_zap(TRUE);
162 else
163 after = FALSE;
164 when 'v' : msg("Rogue version %s. (mctesq was here)", release);
165 when CTRL('L') : after = FALSE; clearok(curscr,TRUE);draw(curscr);
166 when CTRL('R') : after = FALSE; msg(huh);
167 when 'S' :
168 after = FALSE;
169 if (save_game())
170 {
171 wmove(cw, LINES-1, 0);
172 wclrtoeol(cw);
173 draw(cw);
174 endwin();
175 exit(0);
176 }
177 when ' ' : ; /* Rest command */
178 when CTRL('P') :
179 after = FALSE;
180 if (wizard)
181 {
182 wizard = FALSE;
183 msg("Not wizard any more");
184 }
185 else
186 {
187 if (wizard = passwd())
188 {
189 msg("You are suddenly as smart as Ken Arnold in dungeon #%d", dnum);
190 wizard = TRUE;
191 waswizard = TRUE;
192 }
193 else
194 msg("Sorry");
195 }
196 when ESCAPE : /* Escape */
197 door_stop = FALSE;
198 count = 0;
199 after = FALSE;
200 otherwise :
201 after = FALSE;
202 if (wizard) switch (ch)
203 {
204 case '@' : msg("@ %d,%d", hero.y, hero.x);
205 when 'C' : create_obj();
206 when CTRL('I') : inventory(lvl_obj, 0);
207 when CTRL('W') : whatis();
208 when CTRL('D') : level++; new_level();
209 when CTRL('U') : level--; new_level();
210 when CTRL('F') : show_win(stdscr, "--More (level map)--");
211 when CTRL('X') : show_win(mw, "--More (monsters)--");
212 when CTRL('T') : teleport();
213 when CTRL('E') : msg("food left: %d", food_left);
214 when CTRL('A') : msg("%d things in your pack", inpack);
215 when CTRL('C') : add_pass();
216 when CTRL('N') :
217 {
218 register struct linked_list *item;
219
220 if ((item = get_item("charge", STICK)) != NULL)
221 ((struct object *) ldata(item))->o_charges = 10000;
222 }
223 when CTRL('H') :
224 {
225 register int i;
226 register struct linked_list *item;
227 register struct object *obj;
228
229 for (i = 0; i < 9; i++)
230 raise_level();
231 /*
232 * Give the rogue a sword (+1,+1)
233 */
234 item = new_item(sizeof *obj);
235 obj = (struct object *) ldata(item);
236 obj->o_type = WEAPON;
237 obj->o_which = TWOSWORD;
238 init_weapon(obj, SWORD);
239 obj->o_hplus = 1;
240 obj->o_dplus = 1;
241 add_pack(item, TRUE);
242 cur_weapon = obj;
243 /*
244 * And his suit of armor
245 */
246 item = new_item(sizeof *obj);
247 obj = (struct object *) ldata(item);
248 obj->o_type = ARMOR;
249 obj->o_which = PLATE_MAIL;
250 obj->o_ac = -5;
251 obj->o_flags |= ISKNOW;
252 cur_armor = obj;
253 add_pack(item, TRUE);
254 }
255 otherwise :
256 msg("Illegal command '%s'.", unctrl(ch));
257 count = 0;
258 }
259 else
260 {
261 msg("Illegal command '%s'.", unctrl(ch));
262 count = 0;
263 }
264 }
265 /*
266 * turn off flags if no longer needed
267 */
268 if (!running)
269 door_stop = FALSE;
270 }
271 /*
272 * If he ran into something to take, let him pick it up.
273 */
274 if (take != 0)
275 pick_up(take);
276 if (!running)
277 door_stop = FALSE;
278 }
279 /*
280 * Kick off the rest if the daemons and fuses
281 */
282 if (after)
283 {
284 look(FALSE);
285 do_daemons(AFTER);
286 do_fuses(AFTER);
287 if (ISRING(LEFT, R_SEARCH))
288 search();
289 else if (ISRING(LEFT, R_TELEPORT) && rnd(100) < 2)
290 teleport();
291 if (ISRING(RIGHT, R_SEARCH))
292 search();
293 else if (ISRING(RIGHT, R_TELEPORT) && rnd(100) < 2)
294 teleport();
295 }
296 }
297
298 /*
299 * quit:
300 * Have player make certain, then exit.
301 */
302
303 void
304 quit(int p)
305 {
306 /*
307 * Reset the signal in case we got here via an interrupt
308 */
309 if (signal(SIGINT, quit) != &quit)
310 mpos = 0;
311 msg("Really quit?");
312 draw(cw);
313 if (readchar(cw) == 'y')
314 {
315 clear();
316 move(LINES-1, 0);
317 draw(stdscr);
318 endwin();
319 score(purse, 1, 0);
320 exit(0);
321 }
322 else
323 {
324 signal(SIGINT, quit);
325 wmove(cw, 0, 0);
326 wclrtoeol(cw);
327 status();
328 draw(cw);
329 mpos = 0;
330 count = 0;
331 }
332 }
333
334 /*
335 * search:
336 * Player gropes about him to find hidden things.
337 */
338
339 search()
340 {
341 register int x, y;
342 register char ch;
343
344 /*
345 * Look all around the hero, if there is something hidden there,
346 * give him a chance to find it. If its found, display it.
347 */
348 if (on(player, ISBLIND))
349 return;
350 for (x = hero.x - 1; x <= hero.x + 1; x++)
351 for (y = hero.y - 1; y <= hero.y + 1; y++)
352 {
353 ch = winat(y, x);
354 switch (ch)
355 {
356 case SECRETDOOR:
357 if (rnd(100) < 20) {
358 mvaddch(y, x, DOOR);
359 count = 0;
360 }
361 break;
362 case TRAP:
363 {
364 register struct trap *tp;
365
366 if (mvwinch(cw, y, x) == TRAP)
367 break;
368 if (rnd(100) > 50)
369 break;
370 tp = trap_at(y, x);
371 tp->tr_flags |= ISFOUND;
372 mvwaddch(cw, y, x, TRAP);
373 count = 0;
374 running = FALSE;
375 msg(tr_name(tp->tr_type));
376 }
377 }
378 }
379 }
380
381 /*
382 * help:
383 * Give single character help, or the whole mess if he wants it
384 */
385
386 help()
387 {
388 register struct h_list *strp = helpstr;
389 register char helpch;
390 register int cnt;
391
392 msg("Character you want help for (* for all): ");
393 helpch = readchar(cw);
394 mpos = 0;
395 /*
396 * If its not a *, print the right help string
397 * or an error if he typed a funny character.
398 */
399 if (helpch != '*')
400 {
401 wmove(cw, 0, 0);
402 while (strp->h_ch)
403 {
404 if (strp->h_ch == helpch)
405 {
406 msg("%s%s", unctrl(strp->h_ch), strp->h_desc);
407 break;
408 }
409 strp++;
410 }
411 if (strp->h_ch != helpch)
412 msg("Unknown character '%s'", unctrl(helpch));
413 return;
414 }
415 /*
416 * Here we print help for everything.
417 * Then wait before we return to command mode
418 */
419 wclear(hw);
420 cnt = 0;
421 while (strp->h_ch)
422 {
423 mvwaddstr(hw, cnt % 23, cnt > 22 ? 40 : 0, unctrl(strp->h_ch));
424 waddstr(hw, strp->h_desc);
425 cnt++;
426 strp++;
427 }
428 wmove(hw, LINES-1, 0);
429 wprintw(hw, "--Press space to continue--");
430 draw(hw);
431 wait_for(hw,' ');
432 wclear(hw);
433 draw(hw);
434 wmove(cw, 0, 0);
435 wclrtoeol(cw);
436 status();
437 touchwin(cw);
438 }
439
440 /*
441 * identify:
442 * Tell the player what a certain thing is.
443 */
444
445 identify()
446 {
447 register char ch, *str;
448
449 msg("What do you want identified? ");
450 ch = readchar(cw);
451 mpos = 0;
452 if (ch == ESCAPE)
453 {
454 msg("");
455 return;
456 }
457 if (isalpha(ch) && isupper(ch))
458 str = monsters[ch-'A'].m_name;
459 else switch(ch)
460 {
461 case '|':
462 case '-':
463 str = "wall of a room";
464 when GOLD: str = "gold";
465 when STAIRS : str = "passage leading down";
466 when DOOR: str = "door";
467 when FLOOR: str = "room floor";
468 when PLAYER: str = "you";
469 when PASSAGE: str = "passage";
470 when TRAP: str = "trap";
471 when POTION: str = "potion";
472 when SCROLL: str = "scroll";
473 when FOOD: str = "food";
474 when WEAPON: str = "weapon";
475 when ' ' : str = "solid rock";
476 when ARMOR: str = "armor";
477 when AMULET: str = "The Amulet of Yendor";
478 when RING: str = "ring";
479 when STICK: str = "wand or staff";
480 otherwise: str = "unknown character";
481 }
482 msg("'%s' : %s", unctrl(ch), str);
483 }
484
485 /*
486 * d_level:
487 * He wants to go down a level
488 */
489
490 d_level()
491 {
492 if (winat(hero.y, hero.x) != STAIRS)
493 msg("I see no way down.");
494 else
495 {
496 level++;
497 new_level();
498 }
499 }
500
501 /*
502 * u_level:
503 * He wants to go up a level
504 */
505
506 u_level()
507 {
508 if (winat(hero.y, hero.x) == STAIRS)
509 {
510 if (amulet)
511 {
512 level--;
513 if (level == 0)
514 total_winner();
515 new_level();
516 msg("You feel a wrenching sensation in your gut.");
517 return;
518 }
519 }
520 msg("I see no way up.");
521 }
522
523 /*
524 * Let him escape for a while
525 */
526
527 shell()
528 {
529 /*
530 * Set the terminal back to original mode
531 */
532 wclear(hw);
533 wmove(hw, LINES-1, 0);
534 draw(hw);
535 endwin();
536 in_shell = TRUE;
537 fflush(stdout);
538
539 md_shellescape();
540
541 printf("\n[Press return to continue]");
542 fflush(stdout);
543 noecho();
544 crmode();
545 in_shell = FALSE;
546 wait_for(cw,'\n');
547 clearok(cw, TRUE);
548 touchwin(cw);
549 draw(cw);
550 }
551
552 /*
553 * allow a user to call a potion, scroll, or ring something
554 */
555 call()
556 {
557 register struct object *obj;
558 register struct linked_list *item;
559 register char **guess, *elsewise;
560 register bool *know;
561
562 item = get_item("call", CALLABLE);
563 /*
564 * Make certain that it is somethings that we want to wear
565 */
566 if (item == NULL)
567 return;
568 obj = (struct object *) ldata(item);
569 switch (obj->o_type)
570 {
571 case RING:
572 guess = r_guess;
573 know = r_know;
574 elsewise = (r_guess[obj->o_which] != NULL ?
575 r_guess[obj->o_which] : r_stones[obj->o_which]);
576 when POTION:
577 guess = p_guess;
578 know = p_know;
579 elsewise = (p_guess[obj->o_which] != NULL ?
580 p_guess[obj->o_which] : p_colors[obj->o_which]);
581 when SCROLL:
582 guess = s_guess;
583 know = s_know;
584 elsewise = (s_guess[obj->o_which] != NULL ?
585 s_guess[obj->o_which] : s_names[obj->o_which]);
586 when STICK:
587 guess = ws_guess;
588 know = ws_know;
589 elsewise = (ws_guess[obj->o_which] != NULL ?
590 ws_guess[obj->o_which] : ws_made[obj->o_which]);
591 otherwise:
592 msg("You can't call that anything");
593 return;
594 }
595 if (know[obj->o_which])
596 {
597 msg("That has already been identified");
598 return;
599 }
600 if (terse)
601 addmsg("C");
602 else
603 addmsg("Was c");
604 msg("alled \"%s\"", elsewise);
605 if (terse)
606 msg("Call it: ");
607 else
608 msg("What do you want to call it? ");
609 strcpy(prbuf, elsewise);
610 if (get_str(prbuf, cw) == NORM)
611 {
612 if (guess[obj->o_which] != NULL)
613 free(guess[obj->o_which]);
614 guess[obj->o_which] = malloc((unsigned int) strlen(prbuf) + 1);
615 if (guess[obj->o_which] != NULL)
616 strcpy(guess[obj->o_which], prbuf);
617 }
618 }