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 getstring(char *, int);
21 int inputoption(void);
23 int getanswer(const char *, bool);
24 void catchalarm(void);
26 /************************************************************************
28 / FUNCTION NAME: getstring()
30 / FUNCTION: read a string from operator
32 / AUTHOR: E. A. Estes, 12/4/85
35 / char *cp - pointer to buffer area to fill
36 / int mx - maximum number of characters to put in buffer
40 / MODULES CALLED: wmove(), _filbuf(), clearok(), waddstr(), wrefresh(),
43 / GLOBAL INPUTS: Echo, _iob[], Wizard, *stdscr
45 / GLOBAL OUTPUTS: _iob[]
48 / Read a string from the keyboard.
49 / This routine is specially designed to:
51 / - strip non-printing characters (unless Wizard)
53 / - redraw the screen if CH_REDRAW is entered
54 / - read in only 'mx - 1' characters or less characters
55 / - nul-terminate string, and throw away newline
57 / 'mx' is assumed to be at least 2.
59 *************************************************************************/
62 getstring(char *cp
, int mx
)
64 char *inptr
; /* pointer into string for next string */
65 int x
, y
; /* original x, y coordinates on screen */
68 getyx(stdscr
, y
, x
); /* get coordinates on screen */
70 *inptr
= '\0'; /* clear string to start */
71 --mx
; /* reserve room in string for nul terminator */
74 /* get characters and process */
77 mvaddstr(y
, x
, cp
); /* print string on screen */
78 clrtoeol(); /* clear any data after string */
79 refresh(); /* update screen */
81 ch
= getchar(); /* get character */
85 case CH_ERASE
: /* back up one character */
90 case CH_KILL
: /* back up to original location */
94 case CH_NEWLINE
: /* terminate string */
97 case CH_REDRAW
: /* redraw screen */
98 clearok(stdscr
, TRUE
);
101 default: /* put data in string */
102 if (ch
>= ' ' || Wizard
)
103 /* printing char; put in string */
107 *inptr
= '\0'; /* terminate string */
109 while (ch
!= CH_NEWLINE
&& inptr
< cp
+ mx
);
112 /************************************************************************
114 / FUNCTION NAME: more()
116 / FUNCTION: pause and prompt player
118 / AUTHOR: E. A. Estes, 12/4/85
121 / int where - line on screen on which to pause
125 / MODULES CALLED: wmove(), waddstr(), getanswer()
127 / GLOBAL INPUTS: *stdscr
129 / GLOBAL OUTPUTS: none
132 / Print a message, and wait for a space character.
134 *************************************************************************/
139 mvaddstr(where
, 0, "-- more --");
140 getanswer(" ", FALSE
);
143 /************************************************************************
145 / FUNCTION NAME: infloat()
147 / FUNCTION: input a floating point number from operator
149 / AUTHOR: E. A. Estes, 12/4/85
153 / RETURN VALUE: floating point number from operator
155 / MODULES CALLED: sscanf(), getstring()
157 / GLOBAL INPUTS: Databuf[]
159 / GLOBAL OUTPUTS: none
162 / Read a string from player, and scan for a floating point
164 / If no valid number is found, return 0.0.
166 *************************************************************************/
171 double result
; /* return value */
173 getstring(Databuf
, SZ_DATABUF
);
174 if (sscanf(Databuf
, "%lf", &result
) < 1)
175 /* no valid number entered */
181 /************************************************************************
183 / FUNCTION NAME: inputoption()
185 / FUNCTION: input an option value from player
187 / AUTHOR: E. A. Estes, 12/4/85
193 / MODULES CALLED: floor(), drandom(), getanswer()
195 / GLOBAL INPUTS: Player
197 / GLOBAL OUTPUTS: Player
200 / Age increases with every move.
201 / Refresh screen, and get a single character option from player.
202 / Return a random value if player's ring has gone bad.
204 *************************************************************************/
209 ++Player
.p_age
; /* increase age */
211 if (Player
.p_ring
.ring_type
!= R_SPOILED
)
213 return(getanswer("T ", TRUE
));
217 getanswer(" ", TRUE
);
218 return((int) ROLL(0.0, 5.0) + '0');
222 /************************************************************************
224 / FUNCTION NAME: interrupt()
226 / FUNCTION: handle interrupt from operator
228 / AUTHOR: E. A. Estes, 12/4/85
234 / MODULES CALLED: fork(), exit(), wait(), death(), alarm(), execl(), wmove(),
235 / getgid(), signal(), getenv(), wclear(), setuid(), getuid(), setgid(),
236 / crmode(), clearok(), waddstr(), cleanup(), wrefresh(), leavegame(),
239 / GLOBAL INPUTS: Player, *stdscr
241 / GLOBAL OUTPUTS: none
244 / Allow player to quit upon hitting the interrupt key.
245 / If the player wants to quit while in battle, he/she automatically
248 *************************************************************************/
253 char line
[81]; /* a place to store data already on screen */
254 int loop
; /* counter */
255 int x
, y
; /* coordinates on screen */
257 unsigned savealarm
; /* to save alarm value */
260 signal(SIGINT
, SIG_IGN
);
263 signal(SIGINT
, SIG_IGN
);
266 savealarm
= alarm(0); /* turn off any alarms */
268 getyx(stdscr
, y
, x
); /* save cursor location */
270 for (loop
= 0; loop
< 80; ++loop
) /* save line on screen */
275 line
[80] = '\0'; /* nul terminate */
277 if (Player
.p_status
== S_INBATTLE
|| Player
.p_status
== S_MONSTER
)
278 /* in midst of fighting */
280 mvaddstr(4, 0, "Quitting now will automatically kill your character. Still want to ? ");
281 ch
= getanswer("NY", FALSE
);
283 death("Bailing out");
288 mvaddstr(4, 0, "Do you really want to quit ? ");
289 ch
= getanswer("NY", FALSE
);
295 mvaddstr(4, 0, line
); /* restore data on screen */
296 move(y
, x
); /* restore cursor */
300 signal(SIGINT
, interrupt
);
303 signal(SIGINT
, interrupt
);
306 alarm(savealarm
); /* restore alarm */
309 /************************************************************************
311 / FUNCTION NAME: getanswer()
313 / FUNCTION: get an answer from operator
315 / AUTHOR: E. A. Estes, 12/4/85
318 / char *choices - string of (upper case) valid choices
319 / bool def - set if default answer
323 / MODULES CALLED: alarm(), wmove(), waddch(), signal(), setjmp(), strchr(),
324 / _filbuf(), clearok(), toupper(), wrefresh(), mvprintw(), wclrtoeol()
326 / GLOBAL INPUTS: catchalarm(), Echo, _iob[], _ctype[], *stdscr, Timeout,
329 / GLOBAL OUTPUTS: _iob[]
332 / Get a single character answer from operator.
333 / Timeout waiting for response. If we timeout, or the
334 / answer in not in the list of valid choices, print choices,
335 / and wait again, otherwise return the first character in ths
337 / Give up after 3 tries.
339 *************************************************************************/
342 getanswer(const char *choices
, bool def
)
345 volatile int loop
; /* counter */
346 volatile int oldx
, oldy
; /* original coordinates on screen */
348 getyx(stdscr
, oldy
, oldx
);
349 alarm(0); /* make sure alarm is off */
351 for (loop
= 3; loop
; --loop
)
352 /* try for 3 times */
354 if (setjmp(Timeoenv
) != 0)
355 /* timed out waiting for response */
357 if (def
|| loop
<= 1)
358 /* return default answer */
361 /* prompt, and try again */
365 /* wait for response */
370 sigset(SIGALRM
, catchalarm
);
372 signal(SIGALRM
, (sig_t
)catchalarm
);
376 alarm(7); /* short */
378 alarm(600); /* long */
382 alarm(0); /* turn off timeout */
385 /* caught some signal */
390 else if (ch
== CH_REDRAW
)
393 clearok(stdscr
, TRUE
); /* force clear screen */
394 ++loop
; /* don't count this input */
399 addch(ch
); /* echo character */
404 /* convert to upper case */
407 if (def
|| strchr(choices
, ch
) != NULL
)
410 else if (!def
&& loop
> 1)
411 /* bad choice; prompt, and try again */
413 YELL
: mvprintw(oldy
+ 1, 0, "Please choose one of : [%s]\n", choices
);
419 /* return default answer */
427 /************************************************************************
429 / FUNCTION NAME: catchalarm()
431 / FUNCTION: catch timer when waiting for input
433 / AUTHOR: E. A. Estes, 12/4/85
439 / MODULES CALLED: longjmp()
441 / GLOBAL INPUTS: Timeoenv[]
443 / GLOBAL OUTPUTS: none
446 / Come here when the alarm expires while waiting for input.
447 / Simply longjmp() into getanswer().
449 *************************************************************************/
454 longjmp(Timeoenv
, 1);