net - Add some #defines from FreeBSD
[dragonfly.git] / games / phantasia / io.c
blob42da0523aca7c3a9b1963ebf232ec39b4dde2d1d
1 /*
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 $
6 */
8 #include <string.h>
9 #include "include.h"
11 /* functions which we need to know about */
12 /* misc.c */
13 extern void death(const char *);
14 extern void leavegame(void);
15 /* phantglobs.c */
16 extern double drandom(void);
18 void catchalarm(void);
19 int getanswer(const char *, bool);
20 void getstring(char *, int);
21 double infloat(void);
22 int inputoption(void);
23 void interrupt(void);
24 void more(int);
27 * FUNCTION: read a string from operator
29 * ARGUMENTS:
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[]
37 * DESCRIPTION:
38 * Read a string from the keyboard.
39 * This routine is specially designed to:
41 * - strip non-printing characters (unless Wizard)
42 * - echo, if desired
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.
50 void
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 */
55 int ch; /* input */
57 getyx(stdscr, y, x); /* get coordinates on screen */
58 inptr = cp;
59 *inptr = '\0'; /* clear string to start */
60 --mx; /* reserve room in string for nul terminator */
62 do {
63 /* get characters and process */
64 if (Echo)
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 */
71 switch (ch) {
72 case CH_ERASE: /* back up one character */
73 if (inptr > cp)
74 --inptr;
75 break;
77 case CH_KILL: /* back up to original location */
78 inptr = cp;
79 break;
81 case CH_NEWLINE: /* terminate string */
82 break;
84 case CH_REDRAW: /* redraw screen */
85 clearok(stdscr, TRUE);
86 continue;
88 default: /* put data in string */
89 if (ch >= ' ' || Wizard)
90 /* printing char; put in string */
91 *inptr++ = ch;
94 *inptr = '\0'; /* terminate string */
95 } while (ch != CH_NEWLINE && inptr < cp + mx);
99 * FUNCTION: pause and prompt player
101 * ARGUMENTS:
102 * int where - line on screen on which to pause
104 * GLOBAL INPUTS: *stdscr
106 * DESCRIPTION:
107 * Print a message, and wait for a space character.
110 void
111 more(int where)
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[]
124 * DESCRIPTION:
125 * Read a string from player, and scan for a floating point
126 * number.
127 * If no valid number is found, return 0.0.
130 double
131 infloat(void)
133 double result; /* return value */
135 getstring(Databuf, SZ_DATABUF);
136 if (sscanf(Databuf, "%lf", &result) < 1)
137 /* no valid number entered */
138 result = 0.0;
140 return (result);
144 * FUNCTION: input an option value from player
146 * GLOBAL INPUTS: Player
148 * GLOBAL OUTPUTS: Player
150 * DESCRIPTION:
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.
157 inputoption(void)
159 ++Player.p_age; /* increase age */
161 if (Player.p_ring.ring_type != R_SPOILED)
162 /* ring ok */
163 return (getanswer("T ", TRUE));
164 else {
165 /* bad ring */
166 getanswer(" ", TRUE);
167 return ((int)ROLL(0.0, 5.0) + '0');
172 * FUNCTION: handle interrupt from operator
174 * GLOBAL INPUTS: Player, *stdscr
176 * DESCRIPTION:
177 * Allow player to quit upon hitting the interrupt key.
178 * If the player wants to quit while in battle, he/she automatically
179 * dies.
182 void
183 interrupt(void)
185 char line[81]; /* a place to store data already on screen */
186 int loop; /* counter */
187 int x, y; /* coordinates on screen */
188 int ch; /* input */
189 unsigned savealarm; /* to save alarm value */
191 #ifdef SYS3
192 signal(SIGINT, SIG_IGN);
193 #endif
194 #ifdef SYS5
195 signal(SIGINT, SIG_IGN);
196 #endif
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 */
203 move(4, loop);
204 line[loop] = inch();
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);
212 if (ch == 'Y')
213 death("Bailing out");
214 /* NOTREACHED */
215 } else {
216 mvaddstr(4, 0, "Do you really want to quit ? ");
217 ch = getanswer("NY", FALSE);
218 if (ch == 'Y')
219 leavegame();
220 /* NOTREACHED */
223 mvaddstr(4, 0, line); /* restore data on screen */
224 move(y, x); /* restore cursor */
225 refresh();
227 #ifdef SYS3
228 signal(SIGINT, interrupt);
229 #endif
230 #ifdef SYS5
231 signal(SIGINT, interrupt);
232 #endif
234 alarm(savealarm); /* restore alarm */
238 * FUNCTION: get an answer from operator
240 * ARGUMENTS:
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,
245 * Timeoenv[]
247 * GLOBAL OUTPUTS: _iob[]
249 * DESCRIPTION:
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
254 * list of choices.
255 * Give up after 3 tries.
259 getanswer(const char *choices, bool def)
261 int ch; /* input */
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 */
274 break;
275 else
276 /* prompt, and try again */
277 goto YELL;
278 } else {
279 /* wait for response */
280 clrtoeol();
281 refresh();
282 #ifdef BSD41
283 sigset(SIGALRM, catchalarm);
284 #else
285 signal(SIGALRM, (sig_t)catchalarm);
286 #endif
287 /* set timeout */
288 if (Timeout)
289 alarm(7); /* short */
290 else
291 alarm(600); /* long */
293 ch = getchar();
295 alarm(0); /* turn off timeout */
297 if (ch < 0) {
298 /* caught some signal */
299 ++loop;
300 continue;
301 } else if (ch == CH_REDRAW) {
302 /* redraw screen */
303 clearok(stdscr, TRUE); /* force clear screen */
304 ++loop; /* don't count this input */
305 continue;
306 } else if (Echo) {
307 addch(ch); /* echo character */
308 refresh();
311 if (islower(ch))
312 /* convert to upper case */
313 ch = toupper(ch);
315 if (def || strchr(choices, ch) != NULL)
316 /* valid choice */
317 return (ch);
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);
322 move(oldy, oldx);
323 clrtoeol();
324 continue;
325 } else
326 /* return default answer */
327 break;
331 return (*choices);
335 * FUNCTION: catch timer when waiting for input
337 * GLOBAL INPUTS: Timeoenv[]
339 * DESCRIPTION:
340 * Come here when the alarm expires while waiting for input.
341 * Simply longjmp() into getanswer().
344 void
345 catchalarm(void)
347 longjmp(Timeoenv, 1);