2008-01-28 08:38:53 +03:00
|
|
|
/* $NetBSD: tok.c,v 1.7 2008/01/28 05:38:54 dholland Exp $ */
|
1993-08-02 21:18:41 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/* tok.c Larn is copyrighted 1986 by Noah Morgan. */
|
1997-10-19 00:03:05 +04:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
2008-01-28 08:38:53 +03:00
|
|
|
__RCSID("$NetBSD: tok.c,v 1.7 2008/01/28 05:38:54 dholland Exp $");
|
1997-10-19 00:03:05 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <sys/types.h>
|
1997-10-19 00:03:05 +04:00
|
|
|
#include <string.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <sys/ioctl.h>
|
1997-10-19 00:03:05 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/wait.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "header.h"
|
1997-10-19 00:03:05 +04:00
|
|
|
#include "extern.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-10-19 00:03:05 +04:00
|
|
|
static char lastok = 0;
|
|
|
|
int yrepcount = 0, dayplay = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef FLUSHNO
|
|
|
|
#define FLUSHNO 5
|
1997-10-19 00:03:05 +04:00
|
|
|
#endif /* FLUSHNO */
|
|
|
|
static int flushno = FLUSHNO; /* input queue flushing threshold */
|
|
|
|
#define MAXUM 52 /* maximum number of user re-named monsters */
|
|
|
|
#define MAXMNAME 40 /* max length of a monster re-name */
|
|
|
|
static char usermonster[MAXUM][MAXMNAME]; /* the user named monster
|
|
|
|
* name goes here */
|
|
|
|
static u_char usermpoint = 0; /* the user monster pointer */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
lexical analyzer for larn
|
|
|
|
*/
|
1997-10-19 00:03:05 +04:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
yylex()
|
1997-10-19 00:03:05 +04:00
|
|
|
{
|
|
|
|
char cc;
|
|
|
|
int ic;
|
|
|
|
if (hit2flag) {
|
|
|
|
hit2flag = 0;
|
|
|
|
yrepcount = 0;
|
|
|
|
return (' ');
|
|
|
|
}
|
|
|
|
if (yrepcount > 0) {
|
|
|
|
--yrepcount;
|
|
|
|
return (lastok);
|
|
|
|
} else
|
|
|
|
yrepcount = 0;
|
|
|
|
if (yrepcount == 0) {
|
|
|
|
bottomdo();
|
|
|
|
showplayer();
|
|
|
|
} /* show where the player is */
|
|
|
|
lflush();
|
|
|
|
while (1) {
|
1993-03-21 12:45:37 +03:00
|
|
|
c[BYTESIN]++;
|
|
|
|
if (ckpflag)
|
1997-10-19 00:03:05 +04:00
|
|
|
if ((c[BYTESIN] % 400) == 0) { /* check for periodic
|
|
|
|
* checkpointing */
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef DOCHECKPOINTS
|
1997-10-19 00:03:05 +04:00
|
|
|
savegame(ckpfile);
|
1993-03-21 12:45:37 +03:00
|
|
|
#else
|
1997-10-19 00:03:05 +04:00
|
|
|
wait(0); /* wait for other forks to
|
|
|
|
* finish */
|
|
|
|
if (fork() == 0) {
|
|
|
|
savegame(ckpfile);
|
|
|
|
exit();
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef TIMECHECK
|
1997-10-19 00:03:05 +04:00
|
|
|
if (dayplay == 0)
|
|
|
|
if (playable()) {
|
|
|
|
cursor(1, 19);
|
|
|
|
lprcat("\nSorry, but it is now time for work. Your game has been saved.\n");
|
|
|
|
beep();
|
|
|
|
lflush();
|
|
|
|
savegame(savefilename);
|
|
|
|
wizard = nomove = 1;
|
|
|
|
sleep(4);
|
|
|
|
died(-257);
|
|
|
|
}
|
|
|
|
#endif /* TIMECHECK */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
}
|
1997-10-19 00:03:05 +04:00
|
|
|
do { /* if keyboard input buffer is too big, flush
|
|
|
|
* some of it */
|
|
|
|
ioctl(0, FIONREAD, &ic);
|
|
|
|
if (ic > flushno)
|
|
|
|
read(0, &cc, 1);
|
|
|
|
}
|
|
|
|
while (ic > flushno);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-10-19 00:03:05 +04:00
|
|
|
if (read(0, &cc, 1) != 1)
|
|
|
|
return (lastok = -1);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-10-19 00:03:05 +04:00
|
|
|
if (cc == 'Y' - 64) { /* control Y -- shell escape */
|
|
|
|
resetscroll();
|
|
|
|
clear();/* scrolling region, home, clear, no
|
|
|
|
* attributes */
|
|
|
|
if ((ic = fork()) == 0) { /* child */
|
2006-05-11 04:22:52 +04:00
|
|
|
execl("/bin/csh", "/bin/csh", NULL);
|
1997-10-19 00:03:05 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
wait(0);
|
1997-10-19 00:03:05 +04:00
|
|
|
if (ic < 0) { /* error */
|
|
|
|
write(2, "Can't fork off a shell!\n", 25);
|
|
|
|
sleep(2);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1997-10-19 00:03:05 +04:00
|
|
|
setscroll();
|
|
|
|
return (lastok = 'L' - 64); /* redisplay screen */
|
|
|
|
}
|
|
|
|
if ((cc <= '9') && (cc >= '0')) {
|
|
|
|
yrepcount = yrepcount * 10 + cc - '0';
|
|
|
|
} else {
|
|
|
|
if (yrepcount > 0)
|
|
|
|
--yrepcount;
|
|
|
|
return (lastok = cc);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
1997-10-19 00:03:05 +04:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* flushall() Function to flush all type-ahead in the input buffer
|
|
|
|
*/
|
1997-10-19 00:03:05 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
flushall()
|
1997-10-19 00:03:05 +04:00
|
|
|
{
|
|
|
|
char cc;
|
|
|
|
int ic;
|
|
|
|
for (;;) { /* if keyboard input buffer is too big, flush
|
|
|
|
* some of it */
|
|
|
|
ioctl(0, FIONREAD, &ic);
|
|
|
|
if (ic <= 0)
|
|
|
|
return;
|
|
|
|
while (ic > 0) {
|
|
|
|
read(0, &cc, 1);
|
|
|
|
--ic;
|
|
|
|
} /* gobble up the byte */
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1997-10-19 00:03:05 +04:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
1997-10-19 00:03:05 +04:00
|
|
|
function to set the desired hardness
|
1993-03-21 12:45:37 +03:00
|
|
|
enter with hard= -1 for default hardness, else any desired hardness
|
|
|
|
*/
|
1997-10-19 00:03:05 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
sethard(hard)
|
1997-10-19 00:03:05 +04:00
|
|
|
int hard;
|
|
|
|
{
|
|
|
|
int j, k, i;
|
|
|
|
j = c[HARDGAME];
|
|
|
|
hashewon();
|
|
|
|
if (restorflag == 0) { /* don't set c[HARDGAME] if restoring game */
|
|
|
|
if (hard >= 0)
|
|
|
|
c[HARDGAME] = hard;
|
|
|
|
} else
|
|
|
|
c[HARDGAME] = j;/* set c[HARDGAME] to proper value if
|
|
|
|
* restoring game */
|
|
|
|
|
|
|
|
if ((k = c[HARDGAME]) != 0)
|
|
|
|
for (j = 0; j <= MAXMONST + 8; j++) {
|
|
|
|
i = ((6 + k) * monster[j].hitpoints + 1) / 6;
|
|
|
|
monster[j].hitpoints = (i < 0) ? 32767 : i;
|
|
|
|
i = ((6 + k) * monster[j].damage + 1) / 5;
|
|
|
|
monster[j].damage = (i > 127) ? 127 : i;
|
|
|
|
i = (10 * monster[j].gold) / (10 + k);
|
|
|
|
monster[j].gold = (i > 32767) ? 32767 : i;
|
|
|
|
i = monster[j].armorclass - k;
|
|
|
|
monster[j].armorclass = (i < -127) ? -127 : i;
|
|
|
|
i = (7 * monster[j].experience) / (7 + k) + 1;
|
|
|
|
monster[j].experience = (i <= 0) ? 1 : i;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1997-10-19 00:03:05 +04:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
function to read and process the larn options file
|
|
|
|
*/
|
1997-10-19 00:03:05 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
readopts()
|
1997-10-19 00:03:05 +04:00
|
|
|
{
|
2008-01-28 08:38:53 +03:00
|
|
|
const char *i;
|
1997-10-19 00:03:05 +04:00
|
|
|
int j, k;
|
|
|
|
int flag;
|
|
|
|
flag = 1; /* set to 0 if he specifies a name for his
|
|
|
|
* character */
|
|
|
|
if (lopen(optsfile) < 0) {
|
|
|
|
strcpy(logname, loginname);
|
|
|
|
return; /* user name if no character name */
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
i = " ";
|
1997-10-19 00:03:05 +04:00
|
|
|
while (*i) {
|
2008-01-28 08:38:53 +03:00
|
|
|
if ((i = lgetw()) == NULL)
|
1997-10-19 00:03:05 +04:00
|
|
|
break; /* check for EOF */
|
|
|
|
while ((*i == ' ') || (*i == '\t'))
|
|
|
|
i++; /* eat leading whitespace */
|
|
|
|
switch (*i) {
|
|
|
|
case 'b':
|
|
|
|
if (strcmp(i, "bold-objects") == 0)
|
|
|
|
boldon = 1;
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-10-19 00:03:05 +04:00
|
|
|
case 'e':
|
|
|
|
if (strcmp(i, "enable-checkpointing") == 0)
|
|
|
|
ckpflag = 1;
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-10-19 00:03:05 +04:00
|
|
|
case 'i':
|
|
|
|
if (strcmp(i, "inverse-objects") == 0)
|
|
|
|
boldon = 0;
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-10-19 00:03:05 +04:00
|
|
|
case 'f':
|
|
|
|
if (strcmp(i, "female") == 0)
|
|
|
|
sex = 0; /* male or female */
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-10-19 00:03:05 +04:00
|
|
|
case 'm':
|
|
|
|
if (strcmp(i, "monster:") == 0) { /* name favorite monster */
|
|
|
|
if ((i = lgetw()) == 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
2008-01-28 08:38:53 +03:00
|
|
|
strlcpy(usermonster[usermpoint], i, MAXMNAME);
|
1997-10-19 00:03:05 +04:00
|
|
|
if (usermpoint >= MAXUM)
|
|
|
|
break; /* defined all of em */
|
|
|
|
if (isalpha(j = usermonster[usermpoint][0])) {
|
|
|
|
for (k = 1; k < MAXMONST + 8; k++) /* find monster */
|
|
|
|
if (monstnamelist[k] == j) {
|
|
|
|
monster[k].name = &usermonster[usermpoint++][0];
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1997-10-19 00:03:05 +04:00
|
|
|
}
|
|
|
|
} else if (strcmp(i, "male") == 0)
|
|
|
|
sex = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'n':
|
|
|
|
if (strcmp(i, "name:") == 0) { /* defining players name */
|
|
|
|
if ((i = lgetw()) == 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
2008-01-28 08:38:53 +03:00
|
|
|
strlcpy(logname, i, LOGNAMESIZE);
|
1997-10-19 00:03:05 +04:00
|
|
|
flag = 0;
|
|
|
|
} else if (strcmp(i, "no-introduction") == 0)
|
|
|
|
nowelcome = 1;
|
|
|
|
else if (strcmp(i, "no-beep") == 0)
|
|
|
|
nobeep = 1;
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-10-19 00:03:05 +04:00
|
|
|
case 'p':
|
|
|
|
if (strcmp(i, "process-name:") == 0) {
|
|
|
|
if ((i = lgetw()) == 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
2008-01-28 08:38:53 +03:00
|
|
|
strlcpy(psname, i, PSNAMESIZE);
|
1997-10-19 00:03:05 +04:00
|
|
|
} else if (strcmp(i, "play-day-play") == 0)
|
|
|
|
dayplay = 1;
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-10-19 00:03:05 +04:00
|
|
|
case 's':
|
|
|
|
if (strcmp(i, "savefile:") == 0) { /* defining savefilename */
|
|
|
|
if ((i = lgetw()) == 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
1997-10-19 00:03:05 +04:00
|
|
|
strcpy(savefilename, i);
|
|
|
|
flag = 0;
|
|
|
|
}
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
};
|
|
|
|
}
|
1997-10-19 00:03:05 +04:00
|
|
|
if (flag)
|
|
|
|
strcpy(logname, loginname);
|
|
|
|
}
|