]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - phantasia/io.c
1 /* $NetBSD: io.c,v 1.2 1995/03/24 03:58:50 cgd Exp $ */
4 * io.c - input/output routines for Phantasia
9 /************************************************************************
11 / FUNCTION NAME: getstring()
13 / FUNCTION: read a string from operator
15 / AUTHOR: E. A. Estes, 12/4/85
18 / char *cp - pointer to buffer area to fill
19 / int mx - maximum number of characters to put in buffer
23 / MODULES CALLED: wmove(), _filbuf(), clearok(), waddstr(), wrefresh(),
26 / GLOBAL INPUTS: Echo, _iob[], Wizard, *stdscr
28 / GLOBAL OUTPUTS: _iob[]
31 / Read a string from the keyboard.
32 / This routine is specially designed to:
34 / - strip non-printing characters (unless Wizard)
36 / - redraw the screen if CH_REDRAW is entered
37 / - read in only 'mx - 1' characters or less characters
38 / - nul-terminate string, and throw away newline
40 / 'mx' is assumed to be at least 2.
42 /************************************************************************/
48 register char *inptr
; /* pointer into string for next string */
49 int x
, y
; /* original x, y coordinates on screen */
52 getyx(stdscr
, y
, x
); /* get coordinates on screen */
54 *inptr
= '\0'; /* clear string to start */
55 --mx
; /* reserve room in string for nul terminator */
58 /* get characters and process */
61 mvaddstr(y
, x
, cp
); /* print string on screen */
62 clrtoeol(); /* clear any data after string */
63 refresh(); /* update screen */
65 ch
= getchar(); /* get character */
69 case CH_ERASE
: /* back up one character */
74 case CH_KILL
: /* back up to original location */
78 case CH_NEWLINE
: /* terminate string */
81 case CH_REDRAW
: /* redraw screen */
82 clearok(stdscr
, TRUE
);
85 default: /* put data in string */
86 if (ch
>= ' ' || Wizard
)
87 /* printing char; put in string */
91 *inptr
= '\0'; /* terminate string */
93 while (ch
!= CH_NEWLINE
&& inptr
< cp
+ mx
);
96 /************************************************************************
98 / FUNCTION NAME: more()
100 / FUNCTION: pause and prompt player
102 / AUTHOR: E. A. Estes, 12/4/85
105 / int where - line on screen on which to pause
109 / MODULES CALLED: wmove(), waddstr(), getanswer()
111 / GLOBAL INPUTS: *stdscr
113 / GLOBAL OUTPUTS: none
116 / Print a message, and wait for a space character.
118 /************************************************************************/
123 mvaddstr(where
, 0, "-- more --");
124 getanswer(" ", FALSE
);
127 /************************************************************************
129 / FUNCTION NAME: infloat()
131 / FUNCTION: input a floating point number from operator
133 / AUTHOR: E. A. Estes, 12/4/85
137 / RETURN VALUE: floating point number from operator
139 / MODULES CALLED: sscanf(), getstring()
141 / GLOBAL INPUTS: Databuf[]
143 / GLOBAL OUTPUTS: none
146 / Read a string from player, and scan for a floating point
148 / If no valid number is found, return 0.0.
150 /************************************************************************/
155 double result
; /* return value */
157 getstring(Databuf
, SZ_DATABUF
);
158 if (sscanf(Databuf
, "%lf", &result
) < 1)
159 /* no valid number entered */
165 /************************************************************************
167 / FUNCTION NAME: inputoption()
169 / FUNCTION: input an option value from player
171 / AUTHOR: E. A. Estes, 12/4/85
177 / MODULES CALLED: floor(), drandom(), getanswer()
179 / GLOBAL INPUTS: Player
181 / GLOBAL OUTPUTS: Player
184 / Age increases with every move.
185 / Refresh screen, and get a single character option from player.
186 / Return a random value if player's ring has gone bad.
188 /************************************************************************/
192 ++Player
.p_age
; /* increase age */
194 if (Player
.p_ring
.ring_type
!= R_SPOILED
)
196 return(getanswer("T ", TRUE
));
200 getanswer(" ", TRUE
);
201 return((int) ROLL(0.0, 5.0) + '0');
205 /************************************************************************
207 / FUNCTION NAME: interrupt()
209 / FUNCTION: handle interrupt from operator
211 / AUTHOR: E. A. Estes, 12/4/85
217 / MODULES CALLED: fork(), exit(), wait(), death(), alarm(), execl(), wmove(),
218 / getgid(), signal(), getenv(), wclear(), setuid(), getuid(), setgid(),
219 / crmode(), clearok(), waddstr(), cleanup(), wrefresh(), leavegame(),
222 / GLOBAL INPUTS: Player, *stdscr
224 / GLOBAL OUTPUTS: none
227 / Allow player to quit upon hitting the interrupt key.
228 / If the player wants to quit while in battle, he/she automatically
231 /************************************************************************/
235 char line
[81]; /* a place to store data already on screen */
236 register int loop
; /* counter */
237 int x
, y
; /* coordinates on screen */
239 unsigned savealarm
; /* to save alarm value */
242 signal(SIGINT
, SIG_IGN
);
245 signal(SIGINT
, SIG_IGN
);
248 savealarm
= alarm(0); /* turn off any alarms */
250 getyx(stdscr
, y
, x
); /* save cursor location */
252 for (loop
= 0; loop
< 80; ++loop
) /* save line on screen */
257 line
[80] = '\0'; /* nul terminate */
259 if (Player
.p_status
== S_INBATTLE
|| Player
.p_status
== S_MONSTER
)
260 /* in midst of fighting */
262 mvaddstr(4, 0, "Quitting now will automatically kill your character. Still want to ? ");
263 ch
= getanswer("NY", FALSE
);
265 death("Bailing out");
270 mvaddstr(4, 0, "Do you really want to quit ? ");
271 ch
= getanswer("NY", FALSE
);
277 mvaddstr(4, 0, line
); /* restore data on screen */
278 move(y
, x
); /* restore cursor */
282 signal(SIGINT
, interrupt
);
285 signal(SIGINT
, interrupt
);
288 alarm(savealarm
); /* restore alarm */
291 /************************************************************************
293 / FUNCTION NAME: getanswer()
295 / FUNCTION: get an answer from operator
297 / AUTHOR: E. A. Estes, 12/4/85
300 / char *choices - string of (upper case) valid choices
301 / bool def - set if default answer
305 / MODULES CALLED: alarm(), wmove(), waddch(), signal(), setjmp(), strchr(),
306 / _filbuf(), clearok(), toupper(), wrefresh(), mvprintw(), wclrtoeol()
308 / GLOBAL INPUTS: catchalarm(), Echo, _iob[], _ctype[], *stdscr, Timeout,
311 / GLOBAL OUTPUTS: _iob[]
314 / Get a single character answer from operator.
315 / Timeout waiting for response. If we timeout, or the
316 / answer in not in the list of valid choices, print choices,
317 / and wait again, otherwise return the first character in ths
319 / Give up after 3 tries.
321 /************************************************************************/
323 getanswer(choices
, def
)
328 int loop
; /* counter */
329 int oldx
, oldy
; /* original coordinates on screen */
331 getyx(stdscr
, oldy
, oldx
);
332 alarm(0); /* make sure alarm is off */
334 for (loop
= 3; loop
; --loop
)
335 /* try for 3 times */
337 if (setjmp(Timeoenv
) != 0)
338 /* timed out waiting for response */
340 if (def
|| loop
<= 1)
341 /* return default answer */
344 /* prompt, and try again */
348 /* wait for response */
353 sigset(SIGALRM
, catchalarm
);
355 signal(SIGALRM
, catchalarm
);
359 alarm(7); /* short */
361 alarm(600); /* long */
365 alarm(0); /* turn off timeout */
368 /* caught some signal */
373 else if (ch
== CH_REDRAW
)
376 clearok(stdscr
, TRUE
); /* force clear screen */
377 ++loop
; /* don't count this input */
382 addch(ch
); /* echo character */
387 /* convert to upper case */
390 if (def
|| strchr(choices
, ch
) != NULL
)
393 else if (!def
&& loop
> 1)
394 /* bad choice; prompt, and try again */
396 YELL
: mvprintw(oldy
+ 1, 0, "Please choose one of : [%s]\n", choices
);
402 /* return default answer */
410 /************************************************************************
412 / FUNCTION NAME: catchalarm()
414 / FUNCTION: catch timer when waiting for input
416 / AUTHOR: E. A. Estes, 12/4/85
422 / MODULES CALLED: longjmp()
424 / GLOBAL INPUTS: Timeoenv[]
426 / GLOBAL OUTPUTS: none
429 / Come here when the alarm expires while waiting for input.
430 / Simply longjmp() into getanswer().
432 /************************************************************************/
437 longjmp(Timeoenv
, 1);