Home | History | Annotate | Line # | Download | only in phantasia
io.c revision 1.3
      1  1.3  lukem /*	$NetBSD: io.c,v 1.3 1997/10/13 02:18:25 lukem Exp $	*/
      2  1.2    cgd 
      3  1.1    jtc /*
      4  1.1    jtc  * io.c - input/output routines for Phantasia
      5  1.1    jtc  */
      6  1.1    jtc 
      7  1.1    jtc #include "include.h"
      8  1.1    jtc 
      9  1.3  lukem void
     10  1.1    jtc getstring(cp, mx)
     11  1.3  lukem 	char   *cp;
     12  1.3  lukem 	int     mx;
     13  1.1    jtc {
     14  1.3  lukem 	char   *inptr;		/* pointer into string for next string */
     15  1.3  lukem 	int     x, y;		/* original x, y coordinates on screen */
     16  1.3  lukem 	int     ch;		/* input */
     17  1.3  lukem 
     18  1.3  lukem 	getyx(stdscr, y, x);	/* get coordinates on screen */
     19  1.3  lukem 	inptr = cp;
     20  1.3  lukem 	*inptr = '\0';		/* clear string to start */
     21  1.3  lukem 	--mx;			/* reserve room in string for nul terminator */
     22  1.3  lukem 
     23  1.3  lukem 	do
     24  1.3  lukem 		/* get characters and process */
     25  1.3  lukem 	{
     26  1.3  lukem 		if (Echo)
     27  1.3  lukem 			mvaddstr(y, x, cp);	/* print string on screen */
     28  1.3  lukem 		clrtoeol();	/* clear any data after string */
     29  1.3  lukem 		refresh();	/* update screen */
     30  1.3  lukem 
     31  1.3  lukem 		ch = getchar();	/* get character */
     32  1.3  lukem 
     33  1.3  lukem 		switch (ch) {
     34  1.3  lukem 		case CH_ERASE:	/* back up one character */
     35  1.3  lukem 			if (inptr > cp)
     36  1.3  lukem 				--inptr;
     37  1.3  lukem 			break;
     38  1.3  lukem 
     39  1.3  lukem 		case CH_KILL:	/* back up to original location */
     40  1.3  lukem 			inptr = cp;
     41  1.3  lukem 			break;
     42  1.3  lukem 
     43  1.3  lukem 		case CH_NEWLINE:	/* terminate string */
     44  1.3  lukem 			break;
     45  1.3  lukem 
     46  1.3  lukem 		case CH_REDRAW:/* redraw screen */
     47  1.3  lukem 			clearok(stdscr, TRUE);
     48  1.3  lukem 			continue;
     49  1.3  lukem 
     50  1.3  lukem 		default:	/* put data in string */
     51  1.3  lukem 			if (ch >= ' ' || Wizard)
     52  1.3  lukem 				/* printing char; put in string */
     53  1.3  lukem 				*inptr++ = ch;
     54  1.3  lukem 		}
     55  1.1    jtc 
     56  1.3  lukem 		*inptr = '\0';	/* terminate string */
     57  1.1    jtc 	}
     58  1.3  lukem 	while (ch != CH_NEWLINE && inptr < cp + mx);
     59  1.1    jtc }
     60  1.1    jtc 
     61  1.3  lukem void
     62  1.1    jtc more(where)
     63  1.3  lukem 	int     where;
     64  1.1    jtc {
     65  1.3  lukem 	mvaddstr(where, 0, "-- more --");
     66  1.3  lukem 	getanswer(" ", FALSE);
     67  1.1    jtc }
     68  1.1    jtc 
     69  1.1    jtc double
     70  1.1    jtc infloat()
     71  1.1    jtc {
     72  1.3  lukem 	double  result;		/* return value */
     73  1.1    jtc 
     74  1.3  lukem 	getstring(Databuf, SZ_DATABUF);
     75  1.3  lukem 	if (sscanf(Databuf, "%lf", &result) < 1)
     76  1.3  lukem 		/* no valid number entered */
     77  1.3  lukem 		result = 0.0;
     78  1.1    jtc 
     79  1.3  lukem 	return (result);
     80  1.1    jtc }
     81  1.1    jtc 
     82  1.3  lukem int
     83  1.1    jtc inputoption()
     84  1.1    jtc {
     85  1.3  lukem 	++Player.p_age;		/* increase age */
     86  1.1    jtc 
     87  1.3  lukem 	if (Player.p_ring.ring_type != R_SPOILED)
     88  1.3  lukem 		/* ring ok */
     89  1.3  lukem 		return (getanswer("T ", TRUE));
     90  1.3  lukem 	else
     91  1.3  lukem 		/* bad ring */
     92  1.1    jtc 	{
     93  1.3  lukem 		getanswer(" ", TRUE);
     94  1.3  lukem 		return ((int) ROLL(0.0, 5.0) + '0');
     95  1.1    jtc 	}
     96  1.1    jtc }
     97  1.1    jtc 
     98  1.3  lukem void
     99  1.1    jtc interrupt()
    100  1.1    jtc {
    101  1.3  lukem 	char    line[81];	/* a place to store data already on screen */
    102  1.3  lukem 	int     loop;		/* counter */
    103  1.3  lukem 	int     x, y;		/* coordinates on screen */
    104  1.3  lukem 	int     ch;		/* input */
    105  1.3  lukem 	unsigned savealarm;	/* to save alarm value */
    106  1.1    jtc 
    107  1.1    jtc #ifdef SYS3
    108  1.3  lukem 	signal(SIGINT, SIG_IGN);
    109  1.1    jtc #endif
    110  1.1    jtc #ifdef SYS5
    111  1.3  lukem 	signal(SIGINT, SIG_IGN);
    112  1.1    jtc #endif
    113  1.1    jtc 
    114  1.3  lukem 	savealarm = alarm(0);	/* turn off any alarms */
    115  1.1    jtc 
    116  1.3  lukem 	getyx(stdscr, y, x);	/* save cursor location */
    117  1.1    jtc 
    118  1.3  lukem 	for (loop = 0; loop < 80; ++loop) {	/* save line on screen */
    119  1.3  lukem 		move(4, loop);
    120  1.3  lukem 		line[loop] = inch();
    121  1.3  lukem 	}
    122  1.3  lukem 	line[80] = '\0';	/* nul terminate */
    123  1.3  lukem 
    124  1.3  lukem 	if (Player.p_status == S_INBATTLE || Player.p_status == S_MONSTER)
    125  1.3  lukem 		/* in midst of fighting */
    126  1.3  lukem 	{
    127  1.3  lukem 		mvaddstr(4, 0, "Quitting now will automatically kill your character.  Still want to ? ");
    128  1.3  lukem 		ch = getanswer("NY", FALSE);
    129  1.3  lukem 		if (ch == 'Y')
    130  1.3  lukem 			death("Bailing out");
    131  1.3  lukem 		/* NOTREACHED */
    132  1.3  lukem 	} else {
    133  1.3  lukem 		mvaddstr(4, 0, "Do you really want to quit ? ");
    134  1.3  lukem 		ch = getanswer("NY", FALSE);
    135  1.3  lukem 		if (ch == 'Y')
    136  1.3  lukem 			leavegame();
    137  1.3  lukem 		/* NOTREACHED */
    138  1.3  lukem 	}
    139  1.3  lukem 
    140  1.3  lukem 	mvaddstr(4, 0, line);	/* restore data on screen */
    141  1.3  lukem 	move(y, x);		/* restore cursor */
    142  1.3  lukem 	refresh();
    143  1.1    jtc 
    144  1.1    jtc #ifdef SYS3
    145  1.3  lukem 	signal(SIGINT, interrupt);
    146  1.1    jtc #endif
    147  1.1    jtc #ifdef SYS5
    148  1.3  lukem 	signal(SIGINT, interrupt);
    149  1.1    jtc #endif
    150  1.1    jtc 
    151  1.3  lukem 	alarm(savealarm);	/* restore alarm */
    152  1.1    jtc }
    153  1.1    jtc 
    154  1.3  lukem int
    155  1.1    jtc getanswer(choices, def)
    156  1.3  lukem 	char   *choices;
    157  1.3  lukem 	bool    def;
    158  1.1    jtc {
    159  1.3  lukem 	int     ch;		/* input */
    160  1.3  lukem 	int     loop;		/* counter */
    161  1.3  lukem 	int     oldx, oldy;	/* original coordinates on screen */
    162  1.1    jtc 
    163  1.3  lukem 	getyx(stdscr, oldy, oldx);
    164  1.3  lukem 	alarm(0);		/* make sure alarm is off */
    165  1.1    jtc 
    166  1.3  lukem #if __GNUC__
    167  1.3  lukem 	(void)&loop;		/* XXX quiet gcc */
    168  1.3  lukem #endif
    169  1.3  lukem 	for (loop = 3; loop; --loop)
    170  1.3  lukem 		/* try for 3 times */
    171  1.1    jtc 	{
    172  1.3  lukem 		if (setjmp(Timeoenv) != 0)
    173  1.3  lukem 			/* timed out waiting for response */
    174  1.3  lukem 		{
    175  1.3  lukem 			if (def || loop <= 1)
    176  1.3  lukem 				/* return default answer */
    177  1.3  lukem 				break;
    178  1.3  lukem 			else
    179  1.3  lukem 				/* prompt, and try again */
    180  1.3  lukem 				goto YELL;
    181  1.3  lukem 		} else
    182  1.3  lukem 			/* wait for response */
    183  1.3  lukem 		{
    184  1.3  lukem 			clrtoeol();
    185  1.3  lukem 			refresh();
    186  1.1    jtc #ifdef BSD41
    187  1.3  lukem 			sigset(SIGALRM, catchalarm);
    188  1.1    jtc #else
    189  1.3  lukem 			signal(SIGALRM, catchalarm);
    190  1.1    jtc #endif
    191  1.3  lukem 			/* set timeout */
    192  1.3  lukem 			if (Timeout)
    193  1.3  lukem 				alarm(7);	/* short */
    194  1.3  lukem 			else
    195  1.3  lukem 				alarm(600);	/* long */
    196  1.3  lukem 
    197  1.3  lukem 			ch = getchar();
    198  1.3  lukem 
    199  1.3  lukem 			alarm(0);	/* turn off timeout */
    200  1.3  lukem 
    201  1.3  lukem 			if (ch < 0)
    202  1.3  lukem 				/* caught some signal */
    203  1.3  lukem 			{
    204  1.3  lukem 				++loop;
    205  1.3  lukem 				continue;
    206  1.3  lukem 			} else
    207  1.3  lukem 				if (ch == CH_REDRAW)
    208  1.3  lukem 					/* redraw screen */
    209  1.3  lukem 				{
    210  1.3  lukem 					clearok(stdscr, TRUE);	/* force clear screen */
    211  1.3  lukem 					++loop;	/* don't count this input */
    212  1.3  lukem 					continue;
    213  1.3  lukem 				} else
    214  1.3  lukem 					if (Echo) {
    215  1.3  lukem 						addch(ch);	/* echo character */
    216  1.3  lukem 						refresh();
    217  1.3  lukem 					}
    218  1.3  lukem 			if (islower(ch))
    219  1.3  lukem 				/* convert to upper case */
    220  1.3  lukem 				ch = toupper(ch);
    221  1.3  lukem 
    222  1.3  lukem 			if (def || strchr(choices, ch) != NULL)
    223  1.3  lukem 				/* valid choice */
    224  1.3  lukem 				return (ch);
    225  1.3  lukem 			else
    226  1.3  lukem 				if (!def && loop > 1)
    227  1.3  lukem 					/* bad choice; prompt, and try again */
    228  1.3  lukem 				{
    229  1.3  lukem 			YELL:		mvprintw(oldy + 1, 0, "Please choose one of : [%s]\n", choices);
    230  1.3  lukem 					move(oldy, oldx);
    231  1.3  lukem 					clrtoeol();
    232  1.3  lukem 					continue;
    233  1.3  lukem 				} else
    234  1.3  lukem 					/* return default answer */
    235  1.3  lukem 					break;
    236  1.1    jtc 		}
    237  1.1    jtc 	}
    238  1.1    jtc 
    239  1.3  lukem 	return (*choices);
    240  1.1    jtc }
    241  1.1    jtc 
    242  1.1    jtc void
    243  1.3  lukem catchalarm(dummy)
    244  1.3  lukem 	int dummy;
    245  1.1    jtc {
    246  1.3  lukem 	longjmp(Timeoenv, 1);
    247  1.1    jtc }
    248