Esempio n. 1
0
/*
 * showexpl:
 *	Show the explosions as they currently are
 */
void
showexpl(int y, int x, char type)
{
	PLAYER *pp;
	EXPL *ep;

	if (y < 0 || y >= HEIGHT)
		return;
	if (x < 0 || x >= WIDTH)
		return;
	ep = malloc(sizeof(*ep));
	ep->e_y = y;
	ep->e_x = x;
	ep->e_char = type;
	ep->e_next = NULL;
	if (Last_expl == NULL)
		Expl[0] = ep;
	else
		Last_expl->e_next = ep;
	Last_expl = ep;
	for (pp = Player; pp < End_player; pp++) {
		if (pp->p_maze[y][x] == type)
			continue;
		pp->p_maze[y][x] = type;
		cgoto(pp, y, x);
		outch(pp, type);
	}
#ifdef MONITOR
	for (pp = Monitor; pp < End_monitor; pp++) {
		if (pp->p_maze[y][x] == type)
			continue;
		pp->p_maze[y][x] = type;
		cgoto(pp, y, x);
		outch(pp, type);
	}
#endif
	switch (Maze[y][x]) {
	  case WALL1:
	  case WALL2:
	  case WALL3:
#ifdef RANDOM
	  case DOOR:
#endif
#ifdef REFLECT
	  case WALL4:
	  case WALL5:
#endif
		if (y >= UBOUND && y < DBOUND && x >= LBOUND && x < RBOUND)
			remove_wall(y, x);
		break;
	}
}
Esempio n. 2
0
/*
 * showexpl:
 *	Show the explosions as they currently are
 */
void
showexpl(int y, int x, char type)
{
	PLAYER	*pp;
	EXPL	*ep;

	if (y < 0 || y >= HEIGHT)
		return;
	if (x < 0 || x >= WIDTH)
		return;
	ep = (EXPL *) malloc(sizeof (EXPL));	/* NOSTRICT */
	if (ep == NULL) {
		logit(LOG_ERR, "malloc");
		return;
	}
	ep->e_y = y;
	ep->e_x = x;
	ep->e_char = type;
	ep->e_next = NULL;
	if (Last_expl == NULL)
		Expl[0] = ep;
	else
		Last_expl->e_next = ep;
	Last_expl = ep;
	for (pp = Player; pp < End_player; pp++) {
		if (pp->p_maze[y][x] == type)
			continue;
		pp->p_maze[y][x] = type;
		cgoto(pp, y, x);
		outch(pp, type);
	}
	for (pp = Monitor; pp < End_monitor; pp++) {
		if (pp->p_maze[y][x] == type)
			continue;
		pp->p_maze[y][x] = type;
		cgoto(pp, y, x);
		outch(pp, type);
	}
	switch (Maze[y][x]) {
	  case WALL1:
	  case WALL2:
	  case WALL3:
	  case DOOR:
	  case WALL4:
	  case WALL5:
		if (y >= UBOUND && y < DBOUND && x >= LBOUND && x < RBOUND)
			remove_wall(y, x);
		break;
	}
}
Esempio n. 3
0
int makeinit(fa *f, int anchor)
{
    int i, k;

    f->curstat = 2;
    f->out[2] = 0;
    f->reset = 0;
    k = *(f->re[0].lfollow);
    xfree(f->posns[2]);
    if ((f->posns[2] = (int *) calloc(1, (k+1)*sizeof(int))) == NULL)
        overflo("out of space in makeinit");
    for (i=0; i <= k; i++) {
        (f->posns[2])[i] = (f->re[0].lfollow)[i];
    }
    if ((f->posns[2])[1] == f->accept)
        f->out[2] = 1;
    for (i=0; i < NCHARS; i++)
        f->gototab[2][i] = 0;
    f->curstat = cgoto(f, 2, HAT);
    if (anchor) {
        *f->posns[2] = k-1;	/* leave out position 0 */
        for (i=0; i < k; i++) {
            (f->posns[0])[i] = (f->posns[2])[i];
        }

        f->out[0] = f->out[2];
        if (f->curstat != 2)
            --(*f->posns[f->curstat]);
    }
    return f->curstat;
}
Esempio n. 4
0
/*
 * rollexpl:
 *	Roll the explosions over, so the next one in the list is at the
 *	top
 */
void
rollexpl(void)
{
	EXPL *ep;
	PLAYER *pp;
	int y, x;
	char c;
	EXPL *nextep;

	for (ep = Expl[EXPLEN - 1]; ep != NULL; ep = nextep) {
		nextep = ep->e_next;
		y = ep->e_y;
		x = ep->e_x;
		if (y < UBOUND || y >= DBOUND || x < LBOUND || x >= RBOUND)
			c = Maze[y][x];
		else
			c = SPACE;
		for (pp = Player; pp < End_player; pp++)
			if (pp->p_maze[y][x] == ep->e_char) {
				pp->p_maze[y][x] = c;
				cgoto(pp, y, x);
				outch(pp, c);
			}
#ifdef MONITOR
		for (pp = Monitor; pp < End_monitor; pp++)
			check(pp, y, x);
#endif
		free(ep);
	}
	for (x = EXPLEN - 1; x > 0; x--)
		Expl[x] = Expl[x - 1];
	Last_expl = Expl[0] = NULL;
}
Esempio n. 5
0
static int
playagain(void)
{
    int j;
    ship_t *ss;

    for (ss = cpuship; ss < cpuship + SHIPTYPES; ss++)
	for (j = 0; j < ss->length; j++) {
	    cgoto(ss->y + j * yincr[ss->dir], ss->x + j * xincr[ss->dir]);
	    (void) addch((chtype) ss->symbol);
	}

    if (awinna())
	++cpuwon;
    else
	++plywon;
    j = 18 + (int) strlen(name);
    if (plywon >= 10)
	++j;
    if (cpuwon >= 10)
	++j;
    (void) mvprintw(1, (COLWIDTH - j) / 2,
		    "%s: %d     Computer: %d", name, plywon, cpuwon);

    prompt(2, (awinna())? "Want to be humiliated again, %s [yn]? "
	   : "Going to give me a chance for revenge, %s [yn]? ", name);
    return (sgetc("YN") == 'Y');
}
Esempio n. 6
0
/*
 * rollexpl:
 *	Roll the explosions over, so the next one in the list is at the
 *	top
 */
void
rollexpl(void)
{
	EXPL	*ep;
	PLAYER	*pp;
	int	y, x;
	char	c;
	EXPL	*nextep;

	for (ep = Expl[EXPLEN - 1]; ep != NULL; ep = nextep) {
		nextep = ep->e_next;
		y = ep->e_y;
		x = ep->e_x;
		if (y < UBOUND || y >= DBOUND || x < LBOUND || x >= RBOUND)
			c = Maze[y][x];
		else
			c = SPACE;
		for (pp = Player; pp < End_player; pp++)
			if (pp->p_maze[y][x] == ep->e_char) {
				pp->p_maze[y][x] = c;
				cgoto(pp, y, x);
				outch(pp, c);
			}
		for (pp = Monitor; pp < End_monitor; pp++)
			check(pp, y, x);
		free((char *) ep);
	}
	memmove(&Expl[1], &Expl[0], (EXPLEN - 1) * sizeof Expl[0]);
	/* for (x = EXPLEN - 1; x > 0; x--)
		Expl[x] = Expl[x - 1]; */
	Last_expl = Expl[0] = NULL;
}
Esempio n. 7
0
int pmatch(fa *f, const char *p0)	/* longest match, for sub */
{
    int s, ns;
    uschar *p = (uschar *) p0;
    uschar *q;
    int i, k;

    /* s = f->reset ? makeinit(f,1) : f->initstat; */
    if (f->reset) {
        f->initstat = s = makeinit(f,1);
    } else {
        s = f->initstat;
    }
    patbeg = (char *) p;
    patlen = -1;
    do {
        q = p;
        do {
            if (f->out[s])		/* final state */
                patlen = q-p;
            /* assert(*q < NCHARS); */
            if ((ns = f->gototab[s][*q]) != 0)
                s = ns;
            else
                s = cgoto(f, s, *q);
            if (s == 1) {	/* no transition */
                if (patlen >= 0) {
                    patbeg = (char *) p;
                    return(1);
                }
                else
                    goto nextin;	/* no match */
            }
        } while (*q++ != 0);
        if (f->out[s])
            patlen = q-p-1;	/* don't count $ */
        if (patlen >= 0) {
            patbeg = (char *) p;
            return(1);
        }
nextin:
        s = 2;
        if (f->reset) {
            for (i = 2; i <= f->curstat; i++)
                xfree(f->posns[i]);
            k = *f->posns[0];
            if ((f->posns[2] = (int *) calloc(1, (k+1)*sizeof(int))) == NULL)
                overflo("out of space in pmatch");
            for (i = 0; i <= k; i++)
                (f->posns[2])[i] = (f->posns[0])[i];
            f->initstat = f->curstat = 2;
            f->out[2] = f->out[0];
            for (i = 0; i < NCHARS; i++)
                f->gototab[2][i] = 0;
        }
    } while (*p++ != 0);
    return (0);
}
Esempio n. 8
0
static bool
plyturn(void)
{
    ship_t *ss;
    bool hit;
    NCURSES_CONST char *m = NULL;

    prompt(1, "Where do you want to shoot? ", "");
    for (;;) {
	(void) getcoord(COMPUTER);
	if (hits[PLAYER][curx][cury]) {
	    prompt(1, "You shelled this spot already! Try again.", "");
	    beep();
	} else
	    break;
    }
    hit = IS_SHIP(board[COMPUTER][curx][cury]);
    hits[PLAYER][curx][cury] = (hit ? MARK_HIT : MARK_MISS);
    cgoto(cury, curx);
#ifdef A_COLOR
    if (has_colors()) {
	if (hit)
	    attron(COLOR_PAIR(COLOR_RED));
	else
	    attron(COLOR_PAIR(COLOR_GREEN));
    }
#endif /* A_COLOR */
    (void) addch((chtype) hits[PLAYER][curx][cury]);
#ifdef A_COLOR
    attrset(0);
#endif /* A_COLOR */

    prompt(1, "You %s.", hit ? "scored a hit" : "missed");
    if (hit && (ss = hitship(curx, cury))) {
	switch (rnd(5)) {
	case 0:
	    m = " You sank my %s!";
	    break;
	case 1:
	    m = " I have this sinking feeling about my %s....";
	    break;
	case 2:
	    m = " My %s has gone to Davy Jones's locker!";
	    break;
	case 3:
	    m = " Glub, glub -- my %s is headed for the bottom!";
	    break;
	case 4:
	    m = " You'll pick up survivors from my %s, I hope...!";
	    break;
	}
	(void) printw(m, ss->name);
	(void) beep();
	return (awinna() == -1);
    }
    return (hit);
}
Esempio n. 9
0
/*
 * fire_slime:
 *	Fire a slime shot in the given direction
 */
static void
fire_slime(PLAYER *pp, int req_index)
{
	if (pp == NULL)
		return;
#ifdef DEBUG
	if (req_index < 0 || req_index >= MAXSLIME)
		message(pp, "What you do?");
#endif
	while (req_index >= 0 && pp->p_ammo < slime_req[req_index])
		req_index--;
	if (req_index < 0) {
		message(pp, "Not enough charges.");
		return;
	}
	if (pp->p_ncshot > MAXNCSHOT)
		return;
	if (pp->p_ncshot++ == MAXNCSHOT) {
		cgoto(pp, STAT_GUN_ROW, STAT_VALUE_COL);
		outstr(pp, "   ", 3);
	}
	pp->p_ammo -= slime_req[req_index];
	(void) snprintf(Buf, sizeof(Buf), "%3d", pp->p_ammo);
	cgoto(pp, STAT_AMMO_ROW, STAT_VALUE_COL);
	outstr(pp, Buf, 3);

	add_shot(SLIME, pp->p_y, pp->p_x, pp->p_face,
		slime_req[req_index] * SLIME_FACTOR, pp, false, pp->p_face);
	pp->p_undershot = true;

	/*
	 * Show the object to everyone
	 */
	showexpl(pp->p_y, pp->p_x, SLIME);
	for (pp = Player; pp < End_player; pp++)
		sendcom(pp, REFRESH);
#ifdef MONITOR
	for (pp = Monitor; pp < End_monitor; pp++)
		sendcom(pp, REFRESH);
#endif
}
Esempio n. 10
0
int nematch(fa *f, char *p0)	/* non-empty match, for sub */
{
	int s, ns;
	uschar *p = (uschar *) p0;
	uschar *q;
	int i, k;

	s = f->reset ? makeinit(f,1) : f->initstat;
	patlen = -1;
	while (*p) {
		q = p;
		do {
			if (f->out[s])		/* final state */
				patlen = q-p;
			if ((ns = f->gototab[s][*q]) != 0)
				s = ns;
			else
				s = cgoto(f, s, *q);
			if (s == 1)	/* no transition */
				if (patlen > 0) {
					patbeg = (char *) p;
					return(1);
				} else
					goto nnextin;	/* no nonempty match */
		} while (*q++ != 0);
		if (f->out[s])
			patlen = q-p-1;	/* don't count $ */
		if (patlen > 0 ) {
			patbeg = (char *) p;
			return(1);
		}
	nnextin:
		s = 2;
		if (f->reset) {
			for (i = 2; i <= f->curstat; i++)
				xfree(f->posns[i]);
			k = *f->posns[0];			
			if ((f->posns[2] = (int *) calloc(1, (k+1)*sizeof(int))) == NULL)
				overflo("out of state space");
			for (i = 0; i <= k; i++)
				(f->posns[2])[i] = (f->posns[0])[i];
			f->initstat = f->curstat = 2;
			f->out[2] = f->out[0];
			for (i = 0; i < NCHARS; i++)
				f->gototab[2][i] = 0;
		}
		p++;
	}
	return (0);
}
Esempio n. 11
0
/**
 * Initialize the terminal of a player.
 * @param[in] pp A player.
 * [PSR]
 */
void stmonitor(PLAYER *pp) {
	int line;
	PLAYER *npp;

	memcpy(pp->p_maze, maze, sizeof maze);

	drawmaze(pp);

	(void) sprintf(gen_buf, "%5.5s%c%-10.10s %c", " ", stat_char(pp),
			pp->p_ident->i_name, pp->p_ident->i_team);
	line = STAT_MON_ROW + 1 + (pp - monitor);
	for (npp = player; npp < end_player; npp++) {
		cgoto(npp, line, STAT_NAME_COL);
		outstr(npp, gen_buf, STAT_NAME_LEN);
	}
	for (npp = monitor; npp < end_monitor; npp++) {
		cgoto(npp, line, STAT_NAME_COL);
		outstr(npp, gen_buf, STAT_NAME_LEN);
	}

	sendcom(pp, REFRESH);
	sendcom(pp, READY, 0);
	(void) fflush(pp->p_output);
}
Esempio n. 12
0
/*
 * scan:
 *	Turn on or increase length of a scan
 */
static void
scan(PLAYER *pp)
{
	if (pp->p_ammo <= 0) {
		message(pp, "No more charges");
		return;
	}
	(void) snprintf(Buf, sizeof(Buf), "%3d", --pp->p_ammo);
	cgoto(pp, STAT_AMMO_ROW, STAT_VALUE_COL);
	outstr(pp, Buf, 3);

	pp->p_scan += SCANLEN;

	if (pp->p_cloak >= 0)
		pp->p_cloak = -1;

	showstat(pp);
}
Esempio n. 13
0
int match(fa *f, char *p0)	/* shortest match ? */
{
	int s, ns;
	uschar *p = (uschar *) p0;

	s = f->reset ? makeinit(f,0) : f->initstat;
	if (f->out[s])
		return(1);
	do {
		if ((ns = f->gototab[s][*p]) != 0)
			s = ns;
		else
			s = cgoto(f, s, *p);
		if (f->out[s])
			return(1);
	} while (*p++ != 0);
	return(0);
}
Esempio n. 14
0
/*
 * cleanup:
 *	Exit with the given value, cleaning up any droppings lying around
 */
void
cleanup(int eval)
{
	PLAYER	*pp;

	/* Place their cursor in a friendly position: */
	cgoto(ALL_PLAYERS, HEIGHT, 0);

	/* Send them all the ENDWIN command: */
	sendcom(ALL_PLAYERS, ENDWIN, LAST_PLAYER);

	/* And close their connections: */
	for (pp = Player; pp < End_player; pp++)
		(void) fclose(pp->p_output);
	for (pp = Monitor; pp < End_monitor; pp++)
		(void) fclose(pp->p_output);

	/* Close the server socket: */
	(void) close(Socket);

	/* The end: */
	logx(LOG_INFO, "game over");
	exit(eval);
}
Esempio n. 15
0
/*
 * pickup:
 *	check whether the object blew up or whether he picked it up
 */
static void
pickup(PLAYER *pp, int y, int x, int prob, int obj)
{
	int req;

	switch (obj) {
	  case MINE:
		req = BULREQ;
		break;
	  case GMINE:
		req = GRENREQ;
		break;
	  default:
		abort();
	}
	if (rand_num(100) < prob)
		add_shot(obj, y, x, LEFTS, req, NULL, true, pp->p_face);
	else {
		pp->p_ammo += req;
		(void) snprintf(Buf, sizeof(Buf), "%3d", pp->p_ammo);
		cgoto(pp, STAT_AMMO_ROW, STAT_VALUE_COL);
		outstr(pp, Buf, 3);
	}
}
Esempio n. 16
0
/*
 * cloak:
 *	Turn on or increase length of a cloak
 */
static void
cloak(PLAYER *pp)
{
	if (pp->p_ammo <= 0) {
		message(pp, "No more charges");
		return;
	}
#ifdef BOOTS
	if (pp->p_nboots > 0) {
		message(pp, "Boots are too noisy to cloak!");
		return;
	}
#endif
	(void) snprintf(Buf, sizeof(Buf), "%3d", --pp->p_ammo);
	cgoto(pp, STAT_AMMO_ROW, STAT_VALUE_COL);
	outstr(pp, Buf, 3);

	pp->p_cloak += CLOAKLEN;

	if (pp->p_scan >= 0)
		pp->p_scan = -1;

	showstat(pp);
}
Esempio n. 17
0
static int
getcoord(int atcpu)
{
    int ny, nx, c;

    if (atcpu)
	cgoto(cury, curx);
    else
	pgoto(cury, curx);
    (void) refresh();
    for (;;) {
	if (atcpu) {
	    (void) mvprintw(CYBASE + BDEPTH + 1, CXBASE + 11, "(%d, %c)",
			    curx, 'A' + cury);
	    cgoto(cury, curx);
	} else {
	    (void) mvprintw(PYBASE + BDEPTH + 1, PXBASE + 11, "(%d, %c)",
			    curx, 'A' + cury);
	    pgoto(cury, curx);
	}

	switch (c = getch()) {
	case 'k':
	case '8':
	case KEY_UP:
	    ny = cury + BDEPTH - 1;
	    nx = curx;
	    break;
	case 'j':
	case '2':
	case KEY_DOWN:
	    ny = cury + 1;
	    nx = curx;
	    break;
	case 'h':
	case '4':
	case KEY_LEFT:
	    ny = cury;
	    nx = curx + BWIDTH - 1;
	    break;
	case 'l':
	case '6':
	case KEY_RIGHT:
	    ny = cury;
	    nx = curx + 1;
	    break;
	case 'y':
	case '7':
	case KEY_A1:
	    ny = cury + BDEPTH - 1;
	    nx = curx + BWIDTH - 1;
	    break;
	case 'b':
	case '1':
	case KEY_C1:
	    ny = cury + 1;
	    nx = curx + BWIDTH - 1;
	    break;
	case 'u':
	case '9':
	case KEY_A3:
	    ny = cury + BDEPTH - 1;
	    nx = curx + 1;
	    break;
	case 'n':
	case '3':
	case KEY_C3:
	    ny = cury + 1;
	    nx = curx + 1;
	    break;
	case FF:
	    nx = curx;
	    ny = cury;
	    (void) clearok(stdscr, TRUE);
	    (void) refresh();
	    break;
#ifdef NCURSES_MOUSE_VERSION
	case KEY_MOUSE:
	    {
		MEVENT myevent;

		getmouse(&myevent);
		if (atcpu
		    && myevent.y >= CY(0) && myevent.y <= CY(BDEPTH)
		    && myevent.x >= CX(0) && myevent.x <= CX(BDEPTH)) {
		    curx = CXINV(myevent.x);
		    cury = CYINV(myevent.y);
		    return (' ');
		} else {
		    beep();
		    continue;
		}
	    }
	    /* no fall through */
#endif /* NCURSES_MOUSE_VERSION */

	default:
	    if (atcpu)
		(void) mvaddstr(CYBASE + BDEPTH + 1, CXBASE + 11, "      ");
	    else
		(void) mvaddstr(PYBASE + BDEPTH + 1, PXBASE + 11, "      ");
	    return (c);
	}

	curx = nx % BWIDTH;
	cury = ny % BDEPTH;
    }
}
Esempio n. 18
0
static ship_t *
hitship(int x, int y)
/* register a hit on the targeted ship */
{
    ship_t *sb, *ss;
    char sym;
    int oldx, oldy;

    getyx(stdscr, oldy, oldx);
    sb = (turn) ? plyship : cpuship;
    if ((sym = board[OTHER][x][y]) == 0)
	return ((ship_t *) NULL);
    for (ss = sb; ss < sb + SHIPTYPES; ++ss)
	if (ss->symbol == sym) {
	    if (++ss->hits < ss->length)	/* still afloat? */
		return ((ship_t *) NULL);
	    else {		/* sunk! */
		int i, j;

		if (!closepack)
		    for (j = -1; j <= 1; j++) {
			int bx = ss->x + j * xincr[(ss->dir + 2) % 8];
			int by = ss->y + j * yincr[(ss->dir + 2) % 8];

			for (i = -1; i <= ss->length; ++i) {
			    int x1, y1;

			    x1 = bx + i * xincr[ss->dir];
			    y1 = by + i * yincr[ss->dir];
			    if (ONBOARD(x1, y1)) {
				hits[turn][x1][y1] = MARK_MISS;
				if (turn % 2 == PLAYER) {
				    cgoto(y1, x1);
#ifdef A_COLOR
				    if (has_colors())
					attron(COLOR_PAIR(COLOR_GREEN));
#endif /* A_COLOR */
				    (void) addch(MARK_MISS);
#ifdef A_COLOR
				    (void) attrset(0);
#endif /* A_COLOR */
				} else {
				    pgoto(y1, x1);
				    (void) addch(SHOWSPLASH);
				}
			    }
			}
		    }

		for (i = 0; i < ss->length; ++i) {
		    int x1 = ss->x + i * xincr[ss->dir];
		    int y1 = ss->y + i * yincr[ss->dir];

		    hits[turn][x1][y1] = ss->symbol;
		    if (turn % 2 == PLAYER) {
			cgoto(y1, x1);
			(void) addch((chtype) (ss->symbol));
		    } else {
			pgoto(y1, x1);
#ifdef A_COLOR
			if (has_colors())
			    attron(COLOR_PAIR(COLOR_RED));
#endif /* A_COLOR */
			(void) addch(SHOWHIT);
#ifdef A_COLOR
			(void) attrset(0);
#endif /* A_COLOR */
		    }
		}

		(void) move(oldy, oldx);
		return (ss);
	    }
	}
    (void) move(oldy, oldx);
    return ((ship_t *) NULL);
}
Esempio n. 19
0
/**
 * Manages the messages submission.
 * \return True in case of success, false instead.
 * [PSR]
 */
int answer() {
	PLAYER *pp;
	int newsock;
	static unsigned long mode; /* Changed from u_long. [PSR] */
	static char name[NAMELEN];
	static char team;
	static int enter_status;
	static unsigned int socklen; /* Edited from static int in order to match accept() parameter. [PSR] */
	static unsigned long machine; /* Edited from u_long in order to match accept() parameter. [PSR] */
	static u_int32_t uid;
	static SOCKET sockstruct;
	char *cp1, *cp2;
	int flags;
	u_int32_t version;
	int i;

# ifdef INTERNET
	socklen = sizeof sockstruct;
# else
	socklen = sizeof sockstruct - 1;
# endif
	errno = 0;
	newsock = accept(main_socket, (struct sockaddr *) &sockstruct, &socklen);
	if (newsock < 0) {
		if (errno == EINTR) {
			return false;
		}
# ifdef LOG
		iso_syslog(LOG_ERR, "accept: %m");
# else
		perror("accept");
# endif
		cleanup(1);
	}

# ifdef INTERNET
	machine = ntohl(((struct sockaddr_in *) &sockstruct)->sin_addr.s_addr);
# else
	if (machine == 0) {
		machine = gethostid();
	}
# endif
	version = htonl((u_int32_t) HUNT_VERSION);
	write_and_push(newsock, (char *) &version, LONGLEN);

	/* Authentication. [PSR] */
#ifdef INTERNET
	if(password_hash!=NULL) { /* A password has been set. [PSR] */
		write_and_push(newsock, &c_auth, SHORTLEN);
		bool auth = false;
		for(i=0; i<MAXATTEMPT && !auth; i++) { /* 3 password attempts. [PSR] */
			safe_read(newsock, &client_psw, (13 + 1) * sizeof(char));
			if(strcmp(client_psw, password_hash)!=0) { /* Authentication failed. New request. [PSR] */
				write_and_push(newsock, &c_auth, SHORTLEN);
			} else { /* Authentication successful. [PSR]*/
				write_and_push(newsock, &c_auth_success, SHORTLEN);
				auth = true;
			}
		}
		if(!auth) {
			write_and_push(newsock, &c_refuse, SHORTLEN);
			safe_close(newsock);
			return false;
		}
	} else { /* No authentication. [PSR] */
		write_and_push(newsock, &c_auth_success, SHORTLEN);
	}
#endif

	safe_read(newsock, (char *) &uid, LONGLEN);
	uid = ntohl((unsigned long) uid);
	safe_read(newsock, name, NAMELEN);
	safe_read(newsock, &team, 1);
	safe_read(newsock, (char *) &enter_status, LONGLEN);
	enter_status = ntohl((unsigned long) enter_status);
	safe_read(newsock, ttyname_glob, NAMELEN);
	safe_read(newsock, (char *) &mode, sizeof mode);
	mode = ntohl(mode);

	/*
	 * Turn off blocking I/O, so a slow or dead terminal won't stop
	 * the game.  All subsequent reads check how many bytes they read.
	 */
	flags = fcntl(newsock, F_GETFL, 0);
	flags |= O_NDELAY;
	(void) fcntl(newsock, F_SETFL, flags);

	/*
	 * Make sure the name contains only printable characters
	 * since we use control characters for cursor control
	 * between driver and player processes
	 */
	for (cp1 = cp2 = name; *cp1 != '\0'; cp1++) {
		if (isprint((unsigned char)*cp1) || *cp1 == ' ') {
			*cp2++ = *cp1;
		}
	}
	*cp2 = '\0';

# ifdef INTERNET
	if (mode == C_MESSAGE) {
		char buf[BUFSIZ + 1];
		int n;

		if (team == ' ') {
			(void) sprintf(buf, "%s: ", name);
		}
		else {
			(void) sprintf(buf, "%s[%c]: ", name, team);
		}
		n = strlen(buf);
		for (pp = player; pp < end_player; pp++) {
			cgoto(pp, HEIGHT, 0);
			outstr(pp, buf, n);
		}
		while ((n = read(newsock, buf, BUFSIZ)) > 0)
		for (pp = player; pp < end_player; pp++) {
			outstr(pp, buf, n);
		}
		for (pp = player; pp < end_player; pp++) {
			ce(pp);
			sendcom(pp, REFRESH);
			sendcom(pp, READY, 0);
			(void) fflush(pp->p_output);
		}
		safe_close(newsock);
		return false;
	}
	else
# endif
# ifdef MONITOR
	if (mode == C_MONITOR) {
		if (end_monitor < &monitor[MAXMON]) {
			pp = end_monitor++;
			i = pp - monitor + MAXPL + 3;
		} else {
			socklen = 0;
			write_and_push(newsock, (char *) &socklen, sizeof socklen);
			safe_close(newsock);
			return false;
		}
	}
	else
# endif
	if (end_player < &player[MAXPL]) {
		pp = end_player++;
		i = pp - player + 3;
	} else {
		socklen = 0;
		write_and_push(newsock, (char *) &socklen, sizeof socklen);
		safe_close(newsock);
		return false;
	}

#ifdef MONITOR
	if (mode == C_MONITOR && team == ' ') {
		team = '*';
	}
#endif
	pp->p_ident = get_ident(machine, uid, name, team);
	pp->p_output = fdopen(newsock, "w");
	pp->p_death[0] = '\0';
	pp->p_fd = newsock;
	fdset[i].fd = newsock;
	fdset[i].events = POLLIN;

	pp->p_y = 0;
	pp->p_x = 0;

# ifdef MONITOR
	if (mode == C_MONITOR) {
		stmonitor(pp);
	}
	else
# endif
	stplayer(pp, enter_status);
	return true;
}
Esempio n. 20
0
/**
 * Initializes a player status.
 * @param[out] newpp The new player to initialize.
 * @param[in] enter_status The enter mode of a player.
 * [PSR]
 */
void stplayer(PLAYER *newpp, int enter_status) {
	int x, y;
	PLAYER *pp;

	nplayer++;

	for (y = 0; y < UBOUND; y++) {
		for (x = 0; x < WIDTH; x++) {
			newpp->p_maze[y][x] = maze[y][x];
		}
	}
	for (; y < DBOUND; y++) {
		for (x = 0; x < LBOUND; x++) {
			newpp->p_maze[y][x] = maze[y][x];
		}
		for (; x < RBOUND; x++) {
			newpp->p_maze[y][x] = SPACE;
		}
		for (; x < WIDTH; x++) {
			newpp->p_maze[y][x] = maze[y][x];
		}
	}
	for (; y < HEIGHT; y++) {
		for (x = 0; x < WIDTH; x++) {
			newpp->p_maze[y][x] = maze[y][x];
		}
	}

	do {
		x = rand_num(WIDTH - 1) + 1;
		y = rand_num(HEIGHT - 1) + 1;
	} while (maze[y][x] != SPACE);
	newpp->p_over = SPACE;
	newpp->p_x = x;
	newpp->p_y = y;
	newpp->p_undershot = false;

# ifdef FLY
	if (enter_status == Q_FLY) {
		newpp->p_flying = rand_num(20);
		newpp->p_flyx = 2 * rand_num(6) - 5;
		newpp->p_flyy = 2 * rand_num(6) - 5;
		newpp->p_face = FLYER;
	}
	else
# endif
	{
# ifdef FLY
		newpp->p_flying = -1;
# endif
		newpp->p_face = rand_dir();
	}
	newpp->p_damage = 0;
	newpp->p_damcap = MAXDAM;
	newpp->p_nchar = 0;
	newpp->p_ncount = 0;
	newpp->p_nexec = 0;
	newpp->p_ammo = ISHOTS;
# ifdef BOOTS
	newpp->p_nboots = 0;
# endif
	if (enter_status == Q_SCAN) {
		newpp->p_scan = SCANLEN;
		newpp->p_cloak = 0;
	} else {
		newpp->p_scan = 0;
		newpp->p_cloak = CLOAKLEN;
	}
	newpp->p_ncshot = 0;

	do {
		x = rand_num(WIDTH - 1) + 1;
		y = rand_num(HEIGHT - 1) + 1;
	} while (maze[y][x] != SPACE);
	maze[y][x] = GMINE;
# ifdef MONITOR
	for (pp = monitor; pp < end_monitor; pp++) {
		check(pp, y, x);
	}
# endif

	do {
		x = rand_num(WIDTH - 1) + 1;
		y = rand_num(HEIGHT - 1) + 1;
	} while (maze[y][x] != SPACE);
	maze[y][x] = MINE;
# ifdef MONITOR
	for (pp = monitor; pp < end_monitor; pp++) {
		check(pp, y, x);
	}
# endif

	(void) sprintf(gen_buf, "%5.2f%c%-10.10s %c", newpp->p_ident->i_score,
			stat_char(newpp), newpp->p_ident->i_name, newpp->p_ident->i_team);
	y = STAT_PLAY_ROW + 1 + (newpp - player);
	for (pp = player; pp < end_player; pp++) {
		if (pp != newpp) {
			char smallbuf[10];

			pp->p_ammo += NSHOTS;
			newpp->p_ammo += NSHOTS;
			cgoto(pp, y, STAT_NAME_COL);
			outstr(pp, gen_buf, STAT_NAME_LEN);
			(void) sprintf(smallbuf, "%3d", pp->p_ammo);
			cgoto(pp, STAT_AMMO_ROW, STAT_VALUE_COL);
			outstr(pp, smallbuf, 3);
		}
	}
# ifdef MONITOR
	for (pp = monitor; pp < end_monitor; pp++) {
		cgoto(pp, y, STAT_NAME_COL);
		outstr(pp, gen_buf, STAT_NAME_LEN);
	}
# endif

	drawmaze(newpp);
	drawplayer(newpp, true);
	look(newpp);
# ifdef	FLY
	if (enter_status == Q_FLY) {
		/* Make sure that the position you enter in will be erased */
		showexpl(newpp->p_y, newpp->p_x, FLYER);
	}
# endif
	sendcom(newpp, REFRESH);
	sendcom(newpp, READY, 0);
	(void) fflush(newpp->p_output);
}
Esempio n. 21
0
void
main(int argc, char **argv)
{
	int i;

	ARGBEGIN {
# ifdef DEBUG
		case 'd': debug++; break;
		case 'y': yydebug = TRUE; break;
# endif
		case 't': case 'T':
			Binit(&fout, 1, OWRITE);
			errorf= 2;
			foutopen = 1;
			break;
		case 'v': case 'V':
			report = 1;
			break;
		case 'n': case 'N':
			report = 0;
			break;
		case '9':
			nine = 1;
			break;
		default:
			warning("Unknown option %c", ARGC());
	} ARGEND
	sargc = argc;
	sargv = argv;
	if (argc > 0){
		yyfile = argv[fptr++];
		fin = Bopen(yyfile, OREAD);
		if(fin == 0)
			error ("%s - can't open file: %r", yyfile);
		sargc--;
		sargv++;
	}
	else {
		yyfile = "/fd/0";
		fin = myalloc(sizeof(Biobuf), 1);
		if(fin == 0)
			exits("core");
		Binit(fin, 0, OREAD);
	}
	if(Bgetc(fin) == Beof)		/* no input */
		exits(0);
	Bseek(fin, 0, 0);
	gch();
		/* may be gotten: def, subs, sname, stchar, ccl, dchar */
	get1core();
		/* may be gotten: name, left, right, nullstr, parent, ptr */
	strcpy((char*)sp, "INITIAL");
	sname[0] = sp;
	sp += strlen("INITIAL") + 1;
	sname[1] = 0;
	if(yyparse()) exits("error");	/* error return code */
		/* may be disposed of: def, subs, dchar */
	free1core();
		/* may be gotten: tmpstat, foll, positions, gotof, nexts, nchar, state, atable, sfall, cpackflg */
	get2core();
	ptail();
	mkmatch();
# ifdef DEBUG
	if(debug) pccl();
# endif
	sect  = ENDSECTION;
	if(tptr>0)cfoll(tptr-1);
# ifdef DEBUG
	if(debug)pfoll();
# endif
	cgoto();
# ifdef DEBUG
	if(debug){
		print("Print %d states:\n",stnum+1);
		for(i=0;i<=stnum;i++)stprt(i);
		}
# endif
		/* may be disposed of: positions, tmpstat, foll, state, name, left, right, parent, ccl, stchar, sname */
		/* may be gotten: verify, advance, stoff */
	free2core();
	get3core();
	layout();
		/* may be disposed of: verify, advance, stoff, nexts, nchar,
			gotof, atable, ccpackflg, sfall */
# ifdef DEBUG
	free3core();
# endif
	fother = Bopen(cname,OREAD);
	if(fother == 0)
		error("Lex driver missing, file %s: %r",cname);
	while ( (i=Bgetc(fother)) != Beof)
		Bputc(&fout, i);

	Bterm(fother);
	Bterm(&fout);
	if(
# ifdef DEBUG
		debug   ||
# endif
			report == 1)statistics();
	if (fin)
		Bterm(fin);
	exits(0);	/* success return code */
}
Esempio n. 22
0
int
main(int argc, char **argv)
{
	int i;
	int c;
	char *apath = NULL;
	char *ypath;
	Boolean eoption = 0, woption = 0;

	sargv = argv;
	sargc = argc;
	(void) setlocale(LC_ALL, "");
#ifdef DEBUG
	while ((c = getopt(argc, argv, "dyctvnewVQ:Y:")) != EOF) {
#else
	while ((c = getopt(argc, argv, "ctvnewVQ:Y:")) != EOF) {
#endif
		switch (c) {
#ifdef DEBUG
			case 'd':
				debug++;
				break;
			case 'y':
				yydebug = TRUE;
				break;
#endif
			case 'V':
				(void) fprintf(stderr, "lex: %s %s\n",
				    (const char *)SGU_PKG,
				    (const char *)SGU_REL);
				break;
			case 'Q':
				v_stmp = optarg;
				if (*v_stmp != 'y' && *v_stmp != 'n')
					error(
					"lex: -Q should be followed by [y/n]");
				break;
			case 'Y':
				apath = (char *)malloc(strlen(optarg) +
				    sizeof ("/nceucform") + 1);
				if (apath == NULL)
					error("No available memory "
					    "for directory name.");
				else
					apath = strcpy(apath, optarg);
				break;
			case 'c':
				ratfor = FALSE;
				break;
			case 't':
				fout = stdout;
				break;
			case 'v':
				report = 1;
				break;
			case 'n':
				report = 0;
				break;
			case 'w':
			case 'W':
				woption = 1;
				handleeuc = 1;
				widecio = 1;
				break;
			case 'e':
			case 'E':
				eoption = 1;
				handleeuc = 1;
				widecio = 0;
				break;
			default:
				(void) fprintf(stderr,
				"Usage: lex [-ewctvnV] [-Y directory] "
				"[-Q(y/n)] [file]\n");
				exit(1);
		}
	}
	if (woption && eoption) {
		error(
		"You may not specify both -w and -e simultaneously.");
	}
	no_input = argc - optind;
	if (no_input) {
		/* XCU4: recognize "-" file operand for stdin */
		if (strcmp(argv[optind], "-") == 0)
			fin = stdin;
		else {
			fin = fopen(argv[optind], "r");
			if (fin == NULL)
				error(
				"Can't open input file -- %s", argv[optind]);
		}
	} else
		fin = stdin;

	/* may be gotten: def, subs, sname, schar, ccl, dchar */
	(void) gch();

	/* may be gotten: name, left, right, nullstr, parent */
	get1core();

	scopy(L_INITIAL, sp);
	sname[0] = sp;
	sp += slength(L_INITIAL) + 1;
	sname[1] = 0;

	/* XCU4: %x exclusive start */
	exclusive[0] = 0;

	if (!handleeuc) {
		/*
		 * Set ZCH and ncg to their default values
		 * as they may be needed to handle %t directive.
		 */
		ZCH = ncg = NCH; /* ncg behaves as constant in this mode. */
	}

	/* may be disposed of: def, subs, dchar */
	if (yyparse())
		exit(1);	/* error return code */

	if (handleeuc) {
		ncg = ncgidtbl * 2;
		ZCH = ncg;
		if (ncg >= MAXNCG)
			error(
			"Too complex rules -- requires too many char groups.");
		sortcgidtbl();
	}
	repbycgid(); /* Call this even in ASCII compat. mode. */

	/*
	 * maybe get:
	 *		tmpstat, foll, positions, gotof, nexts,
	 *		nchar, state, atable, sfall, cpackflg
	 */
	free1core();
	get2core();
	ptail();
	mkmatch();
#ifdef DEBUG
	if (debug)
		pccl();
#endif
	sect  = ENDSECTION;
	if (tptr > 0)
		cfoll(tptr-1);
#ifdef DEBUG
	if (debug)
		pfoll();
#endif
	cgoto();
#ifdef DEBUG
	if (debug) {
		(void) printf("Print %d states:\n", stnum + 1);
		for (i = 0; i <= stnum; i++)
			stprt(i);
	}
#endif
	/*
	 * may be disposed of:
	 *		positions, tmpstat, foll, state, name,
	 *		left, right, parent, ccl, schar, sname
	 * maybe get:	 verify, advance, stoff
	 */
	free2core();
	get3core();
	layout();
	/*
	 * may be disposed of:
	 *		verify, advance, stoff, nexts, nchar,
	 *		gotof, atable, ccpackflg, sfall
	 */

#ifdef DEBUG
	free3core();
#endif

	if (handleeuc) {
		if (ratfor)
			error("Ratfor is not supported by -w or -e option.");
		ypath = EUCNAME;
	}
	else
		ypath = ratfor ? RATNAME : CNAME;

	if (apath != NULL)
		ypath = strcat(apath, strrchr(ypath, '/'));
	fother = fopen(ypath, "r");
	if (fother == NULL)
		error("Lex driver missing, file %s", ypath);
	while ((i = getc(fother)) != EOF)
		(void) putc((char)i, fout);
	(void) fclose(fother);
	(void) fclose(fout);
	free(apath);
	if (report == 1)
		statistics();
	(void) fclose(stdout);
	(void) fclose(stderr);
	return (0);	/* success return code */
}

static void
get1core(void)
{
	/*LINTED: E_BAD_PTR_CAST_ALIGN*/
	ccptr =	ccl = (CHR *)myalloc(CCLSIZE, sizeof (*ccl));
	/*LINTED: E_BAD_PTR_CAST_ALIGN*/
	pcptr = pchar = (CHR *)myalloc(pchlen, sizeof (*pchar));
	/*LINTED: E_BAD_PTR_CAST_ALIGN*/
	def = (CHR **)myalloc(DEFSIZE, sizeof (*def));
	/*LINTED: E_BAD_PTR_CAST_ALIGN*/
	subs = (CHR **)myalloc(DEFSIZE, sizeof (*subs));
	/*LINTED: E_BAD_PTR_CAST_ALIGN*/
	dp = dchar = (CHR *)myalloc(DEFCHAR, sizeof (*dchar));
	/*LINTED: E_BAD_PTR_CAST_ALIGN*/
	sname = (CHR **)myalloc(STARTSIZE, sizeof (*sname));
	/* XCU4: exclusive start array */
	/*LINTED: E_BAD_PTR_CAST_ALIGN*/
	exclusive = (int *)myalloc(STARTSIZE, sizeof (*exclusive));
	/*LINTED: E_BAD_PTR_CAST_ALIGN*/
	sp = schar = (CHR *)myalloc(STARTCHAR, sizeof (*schar));
	if (ccl == 0 || def == 0 ||
	    pchar == 0 || subs == 0 || dchar == 0 ||
	    sname == 0 || exclusive == 0 || schar == 0)
		error("Too little core to begin");
}
Esempio n. 23
0
int
answer()
{
	PLAYER			*pp;
	int			newsock;
	static u_long		mode;
	static char		name[NAMELEN];
	static char		team;
	static int		enter_status;
	static int		socklen;
	static u_long		machine;
	static u_int32_t	uid;
	static SOCKET		sockstruct;
	char			*cp1, *cp2;
	int			flags;
	u_int32_t		version;
	int			i;

# ifdef INTERNET
	socklen = sizeof sockstruct;
# else
	socklen = sizeof sockstruct - 1;
# endif
	errno = 0;
	newsock = accept(Socket, (struct sockaddr *) &sockstruct, &socklen);
	if (newsock < 0)
	{
		if (errno == EINTR)
			return FALSE;
# ifdef LOG
		syslog(LOG_ERR, "accept: %m");
# else
		perror("accept");
# endif
		cleanup(1);
	}

# ifdef INTERNET
	machine = ntohl(((struct sockaddr_in *) &sockstruct)->sin_addr.s_addr);
# else
	if (machine == 0)
		machine = gethostid();
# endif
	version = htonl((u_int32_t) HUNT_VERSION);
	(void) write(newsock, (char *) &version, LONGLEN);
	(void) read(newsock, (char *) &uid, LONGLEN);
	uid = ntohl((unsigned long) uid);
	(void) read(newsock, name, NAMELEN);
	(void) read(newsock, &team, 1);
	(void) read(newsock, (char *) &enter_status, LONGLEN);
	enter_status = ntohl((unsigned long) enter_status);
	(void) read(newsock, Ttyname, NAMELEN);
	(void) read(newsock, (char *) &mode, sizeof mode);
	mode = ntohl(mode);

	/*
	 * Turn off blocking I/O, so a slow or dead terminal won't stop
	 * the game.  All subsequent reads check how many bytes they read.
	 */
	flags = fcntl(newsock, F_GETFL, 0);
	flags |= O_NDELAY;
	(void) fcntl(newsock, F_SETFL, flags);

	/*
	 * Make sure the name contains only printable characters
	 * since we use control characters for cursor control
	 * between driver and player processes
	 */
	for (cp1 = cp2 = name; *cp1 != '\0'; cp1++)
		if (isprint((unsigned char)*cp1) || *cp1 == ' ')
			*cp2++ = *cp1;
	*cp2 = '\0';

# ifdef INTERNET
	if (mode == C_MESSAGE) {
		char	buf[BUFSIZ + 1];
		int	n;

		if (team == ' ')
			(void) sprintf(buf, "%s: ", name);
		else
			(void) sprintf(buf, "%s[%c]: ", name, team);
		n = strlen(buf);
		for (pp = Player; pp < End_player; pp++) {
			cgoto(pp, HEIGHT, 0);
			outstr(pp, buf, n);
		}
		while ((n = read(newsock, buf, BUFSIZ)) > 0)
			for (pp = Player; pp < End_player; pp++)
				outstr(pp, buf, n);
		for (pp = Player; pp < End_player; pp++) {
			ce(pp);
			sendcom(pp, REFRESH);
			sendcom(pp, READY, 0);
			(void) fflush(pp->p_output);
		}
		(void) close(newsock);
		return FALSE;
	}
	else
# endif
# ifdef MONITOR
	if (mode == C_MONITOR)
		if (End_monitor < &Monitor[MAXMON]) {
			pp = End_monitor++;
			i = pp - Monitor + MAXPL + 3;
		} else {
			socklen = 0;
			(void) write(newsock, (char *) &socklen,
				sizeof socklen);
			(void) close(newsock);
			return FALSE;
		}
	else
# endif
		if (End_player < &Player[MAXPL]) {
			pp = End_player++;
			i = pp - Player + 3;
		} else {
			socklen = 0;
			(void) write(newsock, (char *) &socklen,
				sizeof socklen);
			(void) close(newsock);
			return FALSE;
		}

#ifdef MONITOR
	if (mode == C_MONITOR && team == ' ')
		team = '*';
#endif
	pp->p_ident = get_ident(machine, uid, name, team);
	pp->p_output = fdopen(newsock, "w");
	pp->p_death[0] = '\0';
	pp->p_fd = newsock;
	fdset[i].fd = newsock;
	fdset[i].events = POLLIN;

	pp->p_y = 0;
	pp->p_x = 0;

# ifdef MONITOR
	if (mode == C_MONITOR)
		stmonitor(pp);
	else
# endif
		stplayer(pp, enter_status);
	return TRUE;
}
Esempio n. 24
0
static int
getcoord(int atcpu)
{
    int ny, nx, c;

    if (atcpu)
	cgoto(cury,curx);
    else
	pgoto(cury, curx);
    refresh();
    for (;;)
    {
	if (atcpu)
	{
	    mvprintw(CYBASE + BDEPTH+1, CXBASE+11, "(%d, %c)", curx, 'A'+cury);
	    cgoto(cury, curx);
	}
	else
	{
	    mvprintw(PYBASE + BDEPTH+1, PXBASE+11, "(%d, %c)", curx, 'A'+cury);
	    pgoto(cury, curx);
	}

	switch(c = getch())
	{
	case 'k': case '8':
#ifdef KEY_MIN
	case KEY_UP:
#endif /* KEY_MIN */
	    ny = cury+BDEPTH-1; nx = curx;
	    break;
	case 'j': case '2':
#ifdef KEY_MIN
	case KEY_DOWN:
#endif /* KEY_MIN */
	    ny = cury+1;        nx = curx;
	    break;
	case 'h': case '4':
#ifdef KEY_MIN
	case KEY_LEFT:
#endif /* KEY_MIN */
	    ny = cury;          nx = curx+BWIDTH-1;
	    break;
	case 'l': case '6':
#ifdef KEY_MIN
	case KEY_RIGHT:
#endif /* KEY_MIN */
	    ny = cury;          nx = curx+1;
	    break;
	case 'y': case '7':
#ifdef KEY_MIN
	case KEY_A1:
#endif /* KEY_MIN */
	    ny = cury+BDEPTH-1; nx = curx+BWIDTH-1;
	    break;
	case 'b': case '1':
#ifdef KEY_MIN
	case KEY_C1:
#endif /* KEY_MIN */
	    ny = cury+1;        nx = curx+BWIDTH-1;
	    break;
	case 'u': case '9':
#ifdef KEY_MIN
	case KEY_A3:
#endif /* KEY_MIN */
	    ny = cury+BDEPTH-1; nx = curx+1;
	    break;
	case 'n': case '3':
#ifdef KEY_MIN
	case KEY_C3:
#endif /* KEY_MIN */
	    ny = cury+1;        nx = curx+1;
	    break;
	case FF:
	    nx = curx; ny = cury;
	    clearok(stdscr, TRUE);
	    refresh();
	    break;
	default:
	    if (atcpu)
		mvaddstr(CYBASE + BDEPTH + 1, CXBASE + 11, "      ");
	    else
		mvaddstr(PYBASE + BDEPTH + 1, PXBASE + 11, "      ");
	    return(c);
	}

	curx = nx % BWIDTH;
	cury = ny % BDEPTH;
    }
}
Esempio n. 25
0
/* a hit on the targeted ship */
static ship_t *
hitship(int x, int y)
{
    ship_t *sb, *ss;
    char sym;
    int oldx, oldy;

    getyx(stdscr, oldy, oldx);
    sb = (turn) ? plyship : cpuship;
    if(!(sym = board[OTHER][x][y]))
	return(NULL);
    for(ss = sb; ss < sb + SHIPTYPES; ++ss)
	if(ss->symbol == sym)
	{
	    if (++ss->hits < ss->length) {	/* still afloat? */
			return(NULL);
	    } else { /* sunk */
		int i, j;

		if (!closepack) {
		    for (j = -1; j <= 1; j++) {
				int bx = ss->x + j * xincr[(ss->dir + 2) % 8];
				int by = ss->y + j * yincr[(ss->dir + 2) % 8];

				for (i = -1; i <= ss->length; ++i) {
			    	int cx, cy;

			    	cx = bx + i * xincr[ss->dir];
			    	cy = by + i * yincr[ss->dir];
			    	if (ONBOARD(cx, cy)) {
						hits[turn][cx][cy] = MARK_MISS;
						if (turn % 2 == PLAYER) {
				    		cgoto(cy, cx);
#ifdef A_COLOR
				    		if (has_colors())
								attron(COLOR_PAIR(COLOR_GREEN));
#endif /* A_COLOR */

				    		addch(MARK_MISS);
#ifdef A_COLOR
				    		attrset(0);
#endif /* A_COLOR */
						}
			    	}
				}
	    	}
		}

		for (i = 0; i < ss->length; ++i)
		{
		    int dx = ss->x + i * xincr[ss->dir];
		    int dy = ss->y + i * yincr[ss->dir];

		    hits[turn][dx][dy] = ss->symbol;
		    if (turn % 2 == PLAYER)
		    {
			cgoto(dy, dx);
			addch(ss->symbol);
		    }
		}

		move(oldy, oldx);
		return(ss);
	    }
	}
    move(oldy, oldx);
    return(NULL);
}
Esempio n. 26
0
/*
 * zap:
 *	Kill off a player and take them out of the game.
 *	The 'was_player' flag indicates that the player was not
 *	a monitor and needs extra cleaning up.
 */
static void
zap(PLAYER *pp, FLAG was_player)
{
	int	len;
	BULLET	*bp;
	PLAYER	*np;
	int	x, y;
	int	savefd;

	if (was_player) {
		/* If they died from a shot, clean up shrapnel */
		if (pp->p_undershot)
			fixshots(pp->p_y, pp->p_x, pp->p_over);
		/* Let the player see their last position: */
		drawplayer(pp, FALSE);
		/* Remove from game: */
		Nplayer--;
	}

	/* Display the cause of death in the centre of the screen: */
	len = strlen(pp->p_death);
	x = (WIDTH - len) / 2;
	outyx(pp, HEIGHT / 2, x, "%s", pp->p_death);

	/* Put some horizontal lines around and below the death message: */
	memset(pp->p_death + 1, '-', len - 2);
	pp->p_death[0] = '+';
	pp->p_death[len - 1] = '+';
	outyx(pp, HEIGHT / 2 - 1, x, "%s", pp->p_death);
	outyx(pp, HEIGHT / 2 + 1, x, "%s", pp->p_death);

	/* Move to bottom left */
	cgoto(pp, HEIGHT, 0);

	savefd = pp->p_fd;

	if (was_player) {
		int	expl_charge;
		int	expl_type;
		int	ammo_exploding;

		/* Check all the bullets: */
		for (bp = Bullets; bp != NULL; bp = bp->b_next) {
			if (bp->b_owner == pp)
				/* Zapped players can't own bullets: */
				bp->b_owner = NULL;
			if (bp->b_x == pp->p_x && bp->b_y == pp->p_y)
				/* Bullets over the player are now over air: */
				bp->b_over = SPACE;
		}

		/* Explode a random fraction of the player's ammo: */
		ammo_exploding = rand_num(pp->p_ammo);

		/* Determine the type and amount of detonation: */
		expl_charge = rand_num(ammo_exploding + 1);
		if (pp->p_ammo == 0)
			/* Ignore the no-ammo case: */
			expl_charge = expl_type = 0;
		else if (ammo_exploding >= pp->p_ammo - 1) {
			/* Maximal explosions always appear as slime: */
			expl_charge = pp->p_ammo;
			expl_type = SLIME;
		} else {
			/*
			 * Figure out the best effective explosion
			 * type to use, given the amount of charge
			 */
			int btype, stype;
			for (btype = MAXBOMB - 1; btype > 0; btype--)
				if (expl_charge >= shot_req[btype])
					break;
			for (stype = MAXSLIME - 1; stype > 0; stype--)
				if (expl_charge >= slime_req[stype])
					break;
			/* Pick the larger of the bomb or slime: */
			if (btype >= 0 && stype >= 0) {
				if (shot_req[btype] > slime_req[btype])
					btype = -1;
			}
			if (btype >= 0)  {
				expl_type = shot_type[btype];
				expl_charge = shot_req[btype];
			} else
				expl_type = SLIME;
		}

		if (expl_charge > 0) {
			char buf[BUFSIZ];

			/* Detonate: */
			(void) add_shot(expl_type, pp->p_y, pp->p_x,
			    pp->p_face, expl_charge, NULL,
			    TRUE, SPACE);

			/* Explain what the explosion is about. */
			snprintf(buf, sizeof buf, "%s detonated.",
				pp->p_ident->i_name);
			message(ALL_PLAYERS, buf);

			while (pp->p_nboots-- > 0) {
				/* Throw one of the boots away: */
				for (np = Boot; np < &Boot[NBOOTS]; np++)
					if (np->p_flying < 0)
						break;
#ifdef DIAGNOSTIC
				if (np >= &Boot[NBOOTS])
					err(1, "Too many boots");
#endif
				/* Start the boots from where the player is */
				np->p_undershot = FALSE;
				np->p_x = pp->p_x;
				np->p_y = pp->p_y;
				/* Throw for up to 20 steps */
				np->p_flying = rand_num(20);
				np->p_flyx = 2 * rand_num(6) - 5;
				np->p_flyy = 2 * rand_num(6) - 5;
				np->p_over = SPACE;
				np->p_face = BOOT;
				showexpl(np->p_y, np->p_x, BOOT);
			}
		}
		/* No explosion. Leave the player's boots behind. */
		else if (pp->p_nboots > 0) {
			if (pp->p_nboots == 2)
				Maze[pp->p_y][pp->p_x] = BOOT_PAIR;
			else
				Maze[pp->p_y][pp->p_x] = BOOT;
			if (pp->p_undershot)
				fixshots(pp->p_y, pp->p_x,
					Maze[pp->p_y][pp->p_x]);
		}

		/* Any unexploded ammo builds up in the volcano: */
		volcano += pp->p_ammo - expl_charge;

		/* Volcano eruption: */
		if (conf_volcano && rand_num(100) < volcano /
		    conf_volcano_max) {
			/* Erupt near the middle of the map */
			do {
				x = rand_num(WIDTH / 2) + WIDTH / 4;
				y = rand_num(HEIGHT / 2) + HEIGHT / 4;
			} while (Maze[y][x] != SPACE);

			/* Convert volcano charge into lava: */
			(void) add_shot(LAVA, y, x, LEFTS, volcano,
				NULL, TRUE, SPACE);
			volcano = 0;

			/* Tell eveyone what's happening */
			message(ALL_PLAYERS, "Volcano eruption.");
		}

		/* Drone: */
		if (conf_drone && rand_num(100) < 2) {
			/* Find a starting place near the middle of the map: */
			do {
				x = rand_num(WIDTH / 2) + WIDTH / 4;
				y = rand_num(HEIGHT / 2) + HEIGHT / 4;
			} while (Maze[y][x] != SPACE);

			/* Start the drone going: */
			add_shot(DSHOT, y, x, rand_dir(),
				shot_req[conf_mindshot +
				rand_num(MAXBOMB - conf_mindshot)],
				NULL, FALSE, SPACE);
		}

		/* Tell the zapped player's client to shut down. */
		sendcom(pp, ENDWIN, ' ');
		(void) fclose(pp->p_output);

		/* Close up the gap in the Player array: */
		End_player--;
		if (pp != End_player) {
			/* Move the last player into the gap: */
			memcpy(pp, End_player, sizeof *pp);
			outyx(ALL_PLAYERS,
				STAT_PLAY_ROW + 1 + (pp - Player),
				STAT_NAME_COL,
				"%5.2f%c%-10.10s %c",
				pp->p_ident->i_score, stat_char(pp),
				pp->p_ident->i_name, pp->p_ident->i_team);
		}

		/* Erase the last player from the display: */
		cgoto(ALL_PLAYERS, STAT_PLAY_ROW + 1 + Nplayer, STAT_NAME_COL);
		ce(ALL_PLAYERS);
	}
	else {
		/* Zap a monitor */

		/* Close the session: */
		sendcom(pp, ENDWIN, LAST_PLAYER);
		(void) fclose(pp->p_output);

		/* shuffle the monitor table */
		End_monitor--;
		if (pp != End_monitor) {
			memcpy(pp, End_monitor, sizeof *pp);
			outyx(ALL_PLAYERS,
				STAT_MON_ROW + 1 + (pp - Player), STAT_NAME_COL,
				"%5.5s %-10.10s %c", " ",
				pp->p_ident->i_name, pp->p_ident->i_team);
		}

		/* Erase the last monitor in the list */
		cgoto(ALL_PLAYERS,
			STAT_MON_ROW + 1 + (End_monitor - Monitor),
			STAT_NAME_COL);
		ce(ALL_PLAYERS);
	}

	/* Update the file descriptor sets used by select: */
	FD_CLR(savefd, &Fds_mask);
	if (Num_fds == savefd + 1) {
		Num_fds = Socket;
		if (Server_socket > Socket)
			Num_fds = Server_socket;
		for (np = Player; np < End_player; np++)
			if (np->p_fd > Num_fds)
				Num_fds = np->p_fd;
		for (np = Monitor; np < End_monitor; np++)
			if (np->p_fd > Num_fds)
				Num_fds = np->p_fd;
		Num_fds++;
	}
}
Esempio n. 27
0
/*
 * move_player:
 *	Execute a move in the given direction
 */
static void
move_player(PLAYER *pp, int dir)
{
	PLAYER *newp;
	int x, y;
	bool moved;
	BULLET *bp;

	y = pp->p_y;
	x = pp->p_x;

	switch (dir) {
	  case LEFTS:
		x--;
		break;
	  case RIGHT:
		x++;
		break;
	  case ABOVE:
		y--;
		break;
	  case BELOW:
		y++;
		break;
	}

	moved = false;
	switch (Maze[y][x]) {
	  case SPACE:
#ifdef RANDOM
	  case DOOR:
#endif
		moved = true;
		break;
	  case WALL1:
	  case WALL2:
	  case WALL3:
#ifdef REFLECT
	  case WALL4:
	  case WALL5:
#endif
		break;
	  case MINE:
	  case GMINE:
		if (dir == pp->p_face)
			pickup(pp, y, x, 2, Maze[y][x]);
		else if (opposite(dir, pp->p_face))
			pickup(pp, y, x, 95, Maze[y][x]);
		else
			pickup(pp, y, x, 50, Maze[y][x]);
		Maze[y][x] = SPACE;
		moved = true;
		break;
	  case SHOT:
	  case GRENADE:
	  case SATCHEL:
	  case BOMB:
#ifdef OOZE
	  case SLIME:
#endif
#ifdef DRONE
	  case DSHOT:
#endif
		bp = is_bullet(y, x);
		if (bp != NULL)
			bp->b_expl = true;
		Maze[y][x] = SPACE;
		moved = true;
		break;
	  case LEFTS:
	  case RIGHT:
	  case ABOVE:
	  case BELOW:
		if (dir != pp->p_face)
			sendcom(pp, BELL);
		else {
			newp = play_at(y, x);
			checkdam(newp, pp, pp->p_ident, STABDAM, KNIFE);
		}
		break;
#ifdef FLY
	  case FLYER:
		newp = play_at(y, x);
		message(newp, "Oooh, there's a short guy waving at you!");
		message(pp, "You couldn't quite reach him!");
		break;
#endif
#ifdef BOOTS
	  case BOOT:
	  case BOOT_PAIR:
		if (Maze[y][x] == BOOT)
			pp->p_nboots++;
		else
			pp->p_nboots += 2;
		for (newp = Boot; newp < &Boot[NBOOTS]; newp++) {
			if (newp->p_flying < 0)
				continue;
			if (newp->p_y == y && newp->p_x == x) {
				newp->p_flying = -1;
				if (newp->p_undershot)
					fixshots(y, x, newp->p_over);
			}
		}
		if (pp->p_nboots == 2)
			message(pp, "Wow!  A pair of boots!");
		else
			message(pp, "You can hobble around on one boot.");
		Maze[y][x] = SPACE;
		moved = true;
		break;
#endif
	}
	if (moved) {
		if (pp->p_ncshot > 0)
			if (--pp->p_ncshot == MAXNCSHOT) {
				cgoto(pp, STAT_GUN_ROW, STAT_VALUE_COL);
				outstr(pp, " ok", 3);
			}
		if (pp->p_undershot) {
			fixshots(pp->p_y, pp->p_x, pp->p_over);
			pp->p_undershot = false;
		}
		drawplayer(pp, false);
		pp->p_over = Maze[y][x];
		pp->p_y = y;
		pp->p_x = x;
		drawplayer(pp, true);
	}
}