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