2 * io.c - input/output routines for Phantasia
4 * $FreeBSD: src/games/phantasia/io.c,v 1.6 1999/11/16 02:57:33 billf Exp $
5 * $DragonFly: src/games/phantasia/io.c,v 1.3 2005/05/31 00:06:26 swildner Exp $
11 /* functions which we need to know about */
13 extern void death(const char *);
14 extern void leavegame(void);
16 extern double drandom(void);
18 void catchalarm(void);
19 int getanswer(const char *, bool);
20 void getstring(char *, int);
22 int inputoption(void);
27 * FUNCTION: read a string from operator
30 * char *cp - pointer to buffer area to fill
31 * int mx - maximum number of characters to put in buffer
33 * GLOBAL INPUTS: Echo, _iob[], Wizard, *stdscr
35 * GLOBAL OUTPUTS: _iob[]
38 * Read a string from the keyboard.
39 * This routine is specially designed to:
41 * - strip non-printing characters (unless Wizard)
43 * - redraw the screen if CH_REDRAW is entered
44 * - read in only 'mx - 1' characters or less characters
45 * - nul-terminate string, and throw away newline
47 * 'mx' is assumed to be at least 2.
51 getstring(char *cp
, int mx
)
53 char *inptr
; /* pointer into string for next string */
54 int x
, y
; /* original x, y coordinates on screen */
57 getyx(stdscr
, y
, x
); /* get coordinates on screen */
59 *inptr
= '\0'; /* clear string to start */
60 --mx
; /* reserve room in string for nul terminator */
63 /* get characters and process */
65 mvaddstr(y
, x
, cp
); /* print string on screen */
66 clrtoeol(); /* clear any data after string */
67 refresh(); /* update screen */
69 ch
= getchar(); /* get character */
72 case CH_ERASE
: /* back up one character */
77 case CH_KILL
: /* back up to original location */
81 case CH_NEWLINE
: /* terminate string */
84 case CH_REDRAW
: /* redraw screen */
85 clearok(stdscr
, TRUE
);
88 default: /* put data in string */
89 if (ch
>= ' ' || Wizard
)
90 /* printing char; put in string */
94 *inptr
= '\0'; /* terminate string */
95 } while (ch
!= CH_NEWLINE
&& inptr
< cp
+ mx
);
99 * FUNCTION: pause and prompt player
102 * int where - line on screen on which to pause
104 * GLOBAL INPUTS: *stdscr
107 * Print a message, and wait for a space character.
113 mvaddstr(where
, 0, "-- more --");
114 getanswer(" ", FALSE
);
118 * FUNCTION: input a floating point number from operator
120 * RETURN VALUE: floating point number from operator
122 * GLOBAL INPUTS: Databuf[]
125 * Read a string from player, and scan for a floating point
127 * If no valid number is found, return 0.0.
133 double result
; /* return value */
135 getstring(Databuf
, SZ_DATABUF
);
136 if (sscanf(Databuf
, "%lf", &result
) < 1)
137 /* no valid number entered */
144 * FUNCTION: input an option value from player
146 * GLOBAL INPUTS: Player
148 * GLOBAL OUTPUTS: Player
151 * Age increases with every move.
152 * Refresh screen, and get a single character option from player.
153 * Return a random value if player's ring has gone bad.
159 ++Player
.p_age
; /* increase age */
161 if (Player
.p_ring
.ring_type
!= R_SPOILED
)
163 return (getanswer("T ", TRUE
));
166 getanswer(" ", TRUE
);
167 return ((int)ROLL(0.0, 5.0) + '0');
172 * FUNCTION: handle interrupt from operator
174 * GLOBAL INPUTS: Player, *stdscr
177 * Allow player to quit upon hitting the interrupt key.
178 * If the player wants to quit while in battle, he/she automatically
185 char line
[81]; /* a place to store data already on screen */
186 int loop
; /* counter */
187 int x
, y
; /* coordinates on screen */
189 unsigned savealarm
; /* to save alarm value */
192 signal(SIGINT
, SIG_IGN
);
195 signal(SIGINT
, SIG_IGN
);
198 savealarm
= alarm(0); /* turn off any alarms */
200 getyx(stdscr
, y
, x
); /* save cursor location */
202 for (loop
= 0; loop
< 80; ++loop
) { /* save line on screen */
206 line
[80] = '\0'; /* nul terminate */
208 if (Player
.p_status
== S_INBATTLE
|| Player
.p_status
== S_MONSTER
) {
209 /* in midst of fighting */
210 mvaddstr(4, 0, "Quitting now will automatically kill your character. Still want to ? ");
211 ch
= getanswer("NY", FALSE
);
213 death("Bailing out");
216 mvaddstr(4, 0, "Do you really want to quit ? ");
217 ch
= getanswer("NY", FALSE
);
223 mvaddstr(4, 0, line
); /* restore data on screen */
224 move(y
, x
); /* restore cursor */
228 signal(SIGINT
, interrupt
);
231 signal(SIGINT
, interrupt
);
234 alarm(savealarm
); /* restore alarm */
238 * FUNCTION: get an answer from operator
241 * char *choices - string of (upper case) valid choices
242 * bool def - set if default answer
244 * GLOBAL INPUTS: catchalarm(), Echo, _iob[], _ctype[], *stdscr, Timeout,
247 * GLOBAL OUTPUTS: _iob[]
250 * Get a single character answer from operator.
251 * Timeout waiting for response. If we timeout, or the
252 * answer in not in the list of valid choices, print choices,
253 * and wait again, otherwise return the first character in ths
255 * Give up after 3 tries.
259 getanswer(const char *choices
, bool def
)
262 volatile int loop
; /* counter */
263 volatile int oldx
, oldy
; /* original coordinates on screen */
265 getyx(stdscr
, oldy
, oldx
);
266 alarm(0); /* make sure alarm is off */
268 for (loop
= 3; loop
; --loop
) {
269 /* try for 3 times */
270 if (setjmp(Timeoenv
) != 0) {
271 /* timed out waiting for response */
272 if (def
|| loop
<= 1)
273 /* return default answer */
276 /* prompt, and try again */
279 /* wait for response */
283 sigset(SIGALRM
, catchalarm
);
285 signal(SIGALRM
, (sig_t
)catchalarm
);
289 alarm(7); /* short */
291 alarm(600); /* long */
295 alarm(0); /* turn off timeout */
298 /* caught some signal */
301 } else if (ch
== CH_REDRAW
) {
303 clearok(stdscr
, TRUE
); /* force clear screen */
304 ++loop
; /* don't count this input */
307 addch(ch
); /* echo character */
312 /* convert to upper case */
315 if (def
|| strchr(choices
, ch
) != NULL
)
318 else if (!def
&& loop
> 1) {
319 /* bad choice; prompt, and try again */
320 YELL
: mvprintw(oldy
+ 1, 0,
321 "Please choose one of : [%s]\n", choices
);
326 /* return default answer */
335 * FUNCTION: catch timer when waiting for input
337 * GLOBAL INPUTS: Timeoenv[]
340 * Come here when the alarm expires while waiting for input.
341 * Simply longjmp() into getanswer().
347 longjmp(Timeoenv
, 1);