NetBSD/games/phantasia/io.c

248 lines
4.7 KiB
C
Raw Normal View History

/* $NetBSD: io.c,v 1.5 1999/09/08 21:45:29 jsm Exp $ */
1995-03-24 06:58:08 +03:00
1994-10-22 00:19:39 +03:00
/*
* io.c - input/output routines for Phantasia
*/
#include "include.h"
void
1994-10-22 00:19:39 +03:00
getstring(cp, mx)
char *cp;
int mx;
1994-10-22 00:19:39 +03:00
{
char *inptr; /* pointer into string for next string */
int x, y; /* original x, y coordinates on screen */
int ch; /* input */
1994-10-22 00:19:39 +03:00
getyx(stdscr, y, x); /* get coordinates on screen */
inptr = cp;
*inptr = '\0'; /* clear string to start */
--mx; /* reserve room in string for nul terminator */
1994-10-22 00:19:39 +03:00
do
/* get characters and process */
1994-10-22 00:19:39 +03:00
{
if (Echo)
mvaddstr(y, x, cp); /* print string on screen */
clrtoeol(); /* clear any data after string */
refresh(); /* update screen */
ch = getchar(); /* get character */
switch (ch) {
case CH_ERASE: /* back up one character */
if (inptr > cp)
--inptr;
break;
case CH_KILL: /* back up to original location */
inptr = cp;
break;
case CH_NEWLINE: /* terminate string */
break;
case CH_REDRAW:/* redraw screen */
clearok(stdscr, TRUE);
continue;
default: /* put data in string */
if (ch >= ' ' || Wizard)
/* printing char; put in string */
*inptr++ = ch;
}
*inptr = '\0'; /* terminate string */
1994-10-22 00:19:39 +03:00
}
while (ch != CH_NEWLINE && inptr < cp + mx);
1994-10-22 00:19:39 +03:00
}
void
1994-10-22 00:19:39 +03:00
more(where)
int where;
1994-10-22 00:19:39 +03:00
{
mvaddstr(where, 0, "-- more --");
getanswer(" ", FALSE);
1994-10-22 00:19:39 +03:00
}
double
infloat()
{
double result; /* return value */
1994-10-22 00:19:39 +03:00
getstring(Databuf, SZ_DATABUF);
if (sscanf(Databuf, "%lf", &result) < 1)
/* no valid number entered */
result = 0.0;
1994-10-22 00:19:39 +03:00
return (result);
1994-10-22 00:19:39 +03:00
}
int
1994-10-22 00:19:39 +03:00
inputoption()
{
++Player.p_age; /* increase age */
1994-10-22 00:19:39 +03:00
if (Player.p_ring.ring_type != R_SPOILED)
/* ring ok */
return (getanswer("T ", TRUE));
else
/* bad ring */
1994-10-22 00:19:39 +03:00
{
getanswer(" ", TRUE);
return ((int) ROLL(0.0, 5.0) + '0');
1994-10-22 00:19:39 +03:00
}
}
void
1994-10-22 00:19:39 +03:00
interrupt()
{
char line[81]; /* a place to store data already on screen */
int loop; /* counter */
int x, y; /* coordinates on screen */
int ch; /* input */
unsigned savealarm; /* to save alarm value */
1994-10-22 00:19:39 +03:00
#ifdef SYS3
signal(SIGINT, SIG_IGN);
1994-10-22 00:19:39 +03:00
#endif
#ifdef SYS5
signal(SIGINT, SIG_IGN);
1994-10-22 00:19:39 +03:00
#endif
savealarm = alarm(0); /* turn off any alarms */
1994-10-22 00:19:39 +03:00
getyx(stdscr, y, x); /* save cursor location */
1994-10-22 00:19:39 +03:00
for (loop = 0; loop < 80; ++loop) { /* save line on screen */
move(4, loop);
line[loop] = inch();
1994-10-22 00:19:39 +03:00
}
line[80] = '\0'; /* nul terminate */
1994-10-22 00:19:39 +03:00
if (Player.p_status == S_INBATTLE || Player.p_status == S_MONSTER)
/* in midst of fighting */
1994-10-22 00:19:39 +03:00
{
mvaddstr(4, 0, "Quitting now will automatically kill your character. Still want to ? ");
ch = getanswer("NY", FALSE);
if (ch == 'Y')
death("Bailing out");
/* NOTREACHED */
} else {
mvaddstr(4, 0, "Do you really want to quit ? ");
ch = getanswer("NY", FALSE);
if (ch == 'Y')
leavegame();
/* NOTREACHED */
1994-10-22 00:19:39 +03:00
}
mvaddstr(4, 0, line); /* restore data on screen */
move(y, x); /* restore cursor */
refresh();
1994-10-22 00:19:39 +03:00
#ifdef SYS3
signal(SIGINT, interrupt);
1994-10-22 00:19:39 +03:00
#endif
#ifdef SYS5
signal(SIGINT, interrupt);
1994-10-22 00:19:39 +03:00
#endif
alarm(savealarm); /* restore alarm */
1994-10-22 00:19:39 +03:00
}
int
1994-10-22 00:19:39 +03:00
getanswer(choices, def)
const char *choices;
bool def;
1994-10-22 00:19:39 +03:00
{
int ch; /* input */
int loop; /* counter */
int oldx, oldy; /* original coordinates on screen */
1994-10-22 00:19:39 +03:00
getyx(stdscr, oldy, oldx);
alarm(0); /* make sure alarm is off */
1994-10-22 00:19:39 +03:00
#if __GNUC__
(void)&loop; /* XXX quiet gcc */
#endif
for (loop = 3; loop; --loop)
/* try for 3 times */
1994-10-22 00:19:39 +03:00
{
if (setjmp(Timeoenv) != 0)
/* timed out waiting for response */
{
if (def || loop <= 1)
/* return default answer */
break;
else
/* prompt, and try again */
goto YELL;
} else
/* wait for response */
{
clrtoeol();
refresh();
1994-10-22 00:19:39 +03:00
#ifdef BSD41
sigset(SIGALRM, catchalarm);
1994-10-22 00:19:39 +03:00
#else
signal(SIGALRM, catchalarm);
1994-10-22 00:19:39 +03:00
#endif
/* set timeout */
if (Timeout)
alarm(7); /* short */
else
alarm(600); /* long */
ch = getchar();
alarm(0); /* turn off timeout */
if (ch < 0)
/* caught some signal */
{
++loop;
continue;
} else
if (ch == CH_REDRAW)
/* redraw screen */
{
clearok(stdscr, TRUE); /* force clear screen */
++loop; /* don't count this input */
continue;
} else
if (Echo) {
addch(ch); /* echo character */
refresh();
}
if (islower(ch))
/* convert to upper case */
ch = toupper(ch);
if (def || strchr(choices, ch) != NULL)
/* valid choice */
return (ch);
else
if (!def && loop > 1)
/* bad choice; prompt, and try again */
{
YELL: mvprintw(oldy + 1, 0, "Please choose one of : [%s]\n", choices);
move(oldy, oldx);
clrtoeol();
continue;
} else
/* return default answer */
break;
1994-10-22 00:19:39 +03:00
}
}
return (*choices);
1994-10-22 00:19:39 +03:00
}
void
catchalarm(dummy)
int dummy __attribute__((__unused__));
1994-10-22 00:19:39 +03:00
{
longjmp(Timeoenv, 1);
1994-10-22 00:19:39 +03:00
}