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