Home | History | Annotate | Line # | Download | only in huntd
answer.c revision 1.19
      1 /*	$NetBSD: answer.c,v 1.19 2014/03/29 23:44:38 dholland Exp $	*/
      2 /*
      3  * Copyright (c) 1983-2003, Regents of the University of California.
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions are
      8  * met:
      9  *
     10  * + Redistributions of source code must retain the above copyright
     11  *   notice, this list of conditions and the following disclaimer.
     12  * + Redistributions in binary form must reproduce the above copyright
     13  *   notice, this list of conditions and the following disclaimer in the
     14  *   documentation and/or other materials provided with the distribution.
     15  * + Neither the name of the University of California, San Francisco nor
     16  *   the names of its contributors may be used to endorse or promote
     17  *   products derived from this software without specific prior written
     18  *   permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
     21  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     23  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 #ifndef lint
     35 __RCSID("$NetBSD: answer.c,v 1.19 2014/03/29 23:44:38 dholland Exp $");
     36 #endif /* not lint */
     37 
     38 #include <sys/types.h>
     39 #include <sys/socket.h>
     40 #include <ctype.h>
     41 #include <errno.h>
     42 #include <fcntl.h>
     43 #include <stdlib.h>
     44 #include <unistd.h>
     45 #include "hunt.h"
     46 
     47 #define SCOREDECAY	15
     48 
     49 static char Ttyname[NAMELEN];
     50 
     51 static IDENT *get_ident(uint32_t, uint32_t, char *, char);
     52 static void stmonitor(PLAYER *);
     53 static void stplayer(PLAYER *, int);
     54 
     55 int
     56 answer(void)
     57 {
     58 	PLAYER *pp;
     59 	int newsock;
     60 	static u_long mode;
     61 	static char name[NAMELEN];
     62 	static char team;
     63 	static int enter_status;
     64 	static socklen_t socklen;
     65 	static uint32_t machine;
     66 	static uint32_t uid;
     67 	static SOCKET sockstruct;
     68 	char *cp1, *cp2;
     69 	int flags;
     70 	uint32_t version;
     71 	int i;
     72 
     73 #ifdef INTERNET
     74 	socklen = sizeof sockstruct;
     75 #else
     76 	socklen = sizeof sockstruct - 1;
     77 #endif
     78 	errno = 0;
     79 	newsock = accept(Socket, (struct sockaddr *) &sockstruct, &socklen);
     80 	if (newsock < 0)
     81 	{
     82 		if (errno == EINTR)
     83 			return false;
     84 		complain(LOG_ERR, "accept");
     85 		cleanup(1);
     86 	}
     87 
     88 #ifdef INTERNET
     89 	machine = ntohl(((struct sockaddr_in *) &sockstruct)->sin_addr.s_addr);
     90 #else
     91 	if (machine == 0)
     92 		machine = gethostid();
     93 #endif
     94 	version = htonl((uint32_t) HUNT_VERSION);
     95 	(void) write(newsock, &version, LONGLEN);
     96 	(void) read(newsock, &uid, LONGLEN);
     97 	uid = ntohl(uid);
     98 	(void) read(newsock, name, NAMELEN);
     99 	(void) read(newsock, &team, 1);
    100 	(void) read(newsock, &enter_status, LONGLEN);
    101 	enter_status = ntohl((unsigned long) enter_status);
    102 	(void) read(newsock, Ttyname, NAMELEN);
    103 	(void) read(newsock, &mode, sizeof mode);
    104 	mode = ntohl(mode);
    105 
    106 	/*
    107 	 * Ensure null termination.
    108 	 */
    109 	name[sizeof(name)-1] = '\0';
    110 	Ttyname[sizeof(Ttyname)-1] = '\0';
    111 
    112 	/*
    113 	 * Turn off blocking I/O, so a slow or dead terminal won't stop
    114 	 * the game.  All subsequent reads check how many bytes they read.
    115 	 */
    116 	flags = fcntl(newsock, F_GETFL, 0);
    117 	flags |= O_NDELAY;
    118 	(void) fcntl(newsock, F_SETFL, flags);
    119 
    120 	/*
    121 	 * Make sure the name contains only printable characters
    122 	 * since we use control characters for cursor control
    123 	 * between driver and player processes
    124 	 */
    125 	for (cp1 = cp2 = name; *cp1 != '\0'; cp1++)
    126 		if (isprint((unsigned char)*cp1) || *cp1 == ' ')
    127 			*cp2++ = *cp1;
    128 	*cp2 = '\0';
    129 
    130 #ifdef INTERNET
    131 	if (mode == C_MESSAGE) {
    132 		char	buf[BUFSIZ + 1];
    133 		int	n;
    134 
    135 		if (team == ' ')
    136 			(void) snprintf(buf, sizeof(buf), "%s: ", name);
    137 		else
    138 			(void) snprintf(buf, sizeof(buf), "%s[%c]: ", name,
    139 					team);
    140 		n = strlen(buf);
    141 		for (pp = Player; pp < End_player; pp++) {
    142 			cgoto(pp, HEIGHT, 0);
    143 			outstr(pp, buf, n);
    144 		}
    145 		while ((n = read(newsock, buf, BUFSIZ)) > 0)
    146 			for (pp = Player; pp < End_player; pp++)
    147 				outstr(pp, buf, n);
    148 		for (pp = Player; pp < End_player; pp++) {
    149 			ce(pp);
    150 			sendcom(pp, REFRESH);
    151 			sendcom(pp, READY, 0);
    152 			(void) fflush(pp->p_output);
    153 		}
    154 		(void) close(newsock);
    155 		return false;
    156 	}
    157 	else
    158 #endif
    159 #ifdef MONITOR
    160 	if (mode == C_MONITOR)
    161 		if (End_monitor < &Monitor[MAXMON]) {
    162 			pp = End_monitor++;
    163 			i = pp - Monitor + MAXPL + 3;
    164 		} else {
    165 			socklen = 0;
    166 			(void) write(newsock, &socklen,
    167 				sizeof socklen);
    168 			(void) close(newsock);
    169 			return false;
    170 		}
    171 	else
    172 #endif
    173 		if (End_player < &Player[MAXPL]) {
    174 			pp = End_player++;
    175 			i = pp - Player + 3;
    176 		} else {
    177 			socklen = 0;
    178 			(void) write(newsock, &socklen,
    179 				sizeof socklen);
    180 			(void) close(newsock);
    181 			return false;
    182 		}
    183 
    184 #ifdef MONITOR
    185 	if (mode == C_MONITOR && team == ' ')
    186 		team = '*';
    187 #endif
    188 	pp->p_ident = get_ident(machine, uid, name, team);
    189 	pp->p_output = fdopen(newsock, "w");
    190 	pp->p_death[0] = '\0';
    191 	pp->p_fd = newsock;
    192 	fdset[i].fd = newsock;
    193 	fdset[i].events = POLLIN;
    194 
    195 	pp->p_y = 0;
    196 	pp->p_x = 0;
    197 
    198 #ifdef MONITOR
    199 	if (mode == C_MONITOR)
    200 		stmonitor(pp);
    201 	else
    202 #endif
    203 		stplayer(pp, enter_status);
    204 	return true;
    205 }
    206 
    207 #ifdef MONITOR
    208 static void
    209 stmonitor(PLAYER *pp)
    210 {
    211 	int line;
    212 	PLAYER *npp;
    213 
    214 	memcpy(pp->p_maze, Maze, sizeof Maze);
    215 
    216 	drawmaze(pp);
    217 
    218 	(void) snprintf(Buf, sizeof(Buf), "%5.5s%c%-10.10s %c", " ",
    219 		stat_char(pp),
    220 		pp->p_ident->i_name, pp->p_ident->i_team);
    221 	line = STAT_MON_ROW + 1 + (pp - Monitor);
    222 	for (npp = Player; npp < End_player; npp++) {
    223 		cgoto(npp, line, STAT_NAME_COL);
    224 		outstr(npp, Buf, STAT_NAME_LEN);
    225 	}
    226 	for (npp = Monitor; npp < End_monitor; npp++) {
    227 		cgoto(npp, line, STAT_NAME_COL);
    228 		outstr(npp, Buf, STAT_NAME_LEN);
    229 	}
    230 
    231 	sendcom(pp, REFRESH);
    232 	sendcom(pp, READY, 0);
    233 	(void) fflush(pp->p_output);
    234 }
    235 #endif
    236 
    237 static void
    238 stplayer(PLAYER *newpp, int enter_status)
    239 {
    240 	int x, y;
    241 	PLAYER *pp;
    242 
    243 	Nplayer++;
    244 
    245 	for (y = 0; y < UBOUND; y++)
    246 		for (x = 0; x < WIDTH; x++)
    247 			newpp->p_maze[y][x] = Maze[y][x];
    248 	for (     ; y < DBOUND; y++) {
    249 		for (x = 0; x < LBOUND; x++)
    250 			newpp->p_maze[y][x] = Maze[y][x];
    251 		for (     ; x < RBOUND; x++)
    252 			newpp->p_maze[y][x] = SPACE;
    253 		for (     ; x < WIDTH;  x++)
    254 			newpp->p_maze[y][x] = Maze[y][x];
    255 	}
    256 	for (     ; y < HEIGHT; y++)
    257 		for (x = 0; x < WIDTH; x++)
    258 			newpp->p_maze[y][x] = Maze[y][x];
    259 
    260 	do {
    261 		x = rand_num(WIDTH - 1) + 1;
    262 		y = rand_num(HEIGHT - 1) + 1;
    263 	} while (Maze[y][x] != SPACE);
    264 	newpp->p_over = SPACE;
    265 	newpp->p_x = x;
    266 	newpp->p_y = y;
    267 	newpp->p_undershot = false;
    268 
    269 #ifdef FLY
    270 	if (enter_status == Q_FLY) {
    271 		newpp->p_flying = rand_num(20);
    272 		newpp->p_flyx = 2 * rand_num(6) - 5;
    273 		newpp->p_flyy = 2 * rand_num(6) - 5;
    274 		newpp->p_face = FLYER;
    275 	}
    276 	else
    277 #endif
    278 	{
    279 		newpp->p_flying = -1;
    280 		newpp->p_face = rand_dir();
    281 	}
    282 	newpp->p_damage = 0;
    283 	newpp->p_damcap = MAXDAM;
    284 	newpp->p_nchar = 0;
    285 	newpp->p_ncount = 0;
    286 	newpp->p_nexec = 0;
    287 	newpp->p_ammo = ISHOTS;
    288 #ifdef BOOTS
    289 	newpp->p_nboots = 0;
    290 #endif
    291 	if (enter_status == Q_SCAN) {
    292 		newpp->p_scan = SCANLEN;
    293 		newpp->p_cloak = 0;
    294 	}
    295 	else {
    296 		newpp->p_scan = 0;
    297 		newpp->p_cloak = CLOAKLEN;
    298 	}
    299 	newpp->p_ncshot = 0;
    300 
    301 	do {
    302 		x = rand_num(WIDTH - 1) + 1;
    303 		y = rand_num(HEIGHT - 1) + 1;
    304 	} while (Maze[y][x] != SPACE);
    305 	Maze[y][x] = GMINE;
    306 #ifdef MONITOR
    307 	for (pp = Monitor; pp < End_monitor; pp++)
    308 		check(pp, y, x);
    309 #endif
    310 
    311 	do {
    312 		x = rand_num(WIDTH - 1) + 1;
    313 		y = rand_num(HEIGHT - 1) + 1;
    314 	} while (Maze[y][x] != SPACE);
    315 	Maze[y][x] = MINE;
    316 #ifdef MONITOR
    317 	for (pp = Monitor; pp < End_monitor; pp++)
    318 		check(pp, y, x);
    319 #endif
    320 
    321 	(void) snprintf(Buf, sizeof(Buf), "%5.2f%c%-10.10s %c",
    322 		newpp->p_ident->i_score,
    323 		stat_char(newpp), newpp->p_ident->i_name,
    324 		newpp->p_ident->i_team);
    325 	y = STAT_PLAY_ROW + 1 + (newpp - Player);
    326 	for (pp = Player; pp < End_player; pp++) {
    327 		if (pp != newpp) {
    328 			char	smallbuf[16];
    329 
    330 			pp->p_ammo += NSHOTS;
    331 			newpp->p_ammo += NSHOTS;
    332 			cgoto(pp, y, STAT_NAME_COL);
    333 			outstr(pp, Buf, STAT_NAME_LEN);
    334 			(void) snprintf(smallbuf, sizeof(smallbuf),
    335 					"%3d", pp->p_ammo);
    336 			cgoto(pp, STAT_AMMO_ROW, STAT_VALUE_COL);
    337 			outstr(pp, smallbuf, 3);
    338 		}
    339 	}
    340 #ifdef MONITOR
    341 	for (pp = Monitor; pp < End_monitor; pp++) {
    342 		cgoto(pp, y, STAT_NAME_COL);
    343 		outstr(pp, Buf, STAT_NAME_LEN);
    344 	}
    345 #endif
    346 
    347 	drawmaze(newpp);
    348 	drawplayer(newpp, true);
    349 	look(newpp);
    350 #ifdef FLY
    351 	if (enter_status == Q_FLY)
    352 		/* Make sure that the position you enter in will be erased */
    353 		showexpl(newpp->p_y, newpp->p_x, FLYER);
    354 #endif
    355 	sendcom(newpp, REFRESH);
    356 	sendcom(newpp, READY, 0);
    357 	(void) fflush(newpp->p_output);
    358 }
    359 
    360 /*
    361  * rand_dir:
    362  *	Return a random direction
    363  */
    364 int
    365 rand_dir(void)
    366 {
    367 	switch (rand_num(4)) {
    368 	  case 0:
    369 		return LEFTS;
    370 	  case 1:
    371 		return RIGHT;
    372 	  case 2:
    373 		return BELOW;
    374 	  case 3:
    375 		return ABOVE;
    376 	}
    377 	/* NOTREACHED */
    378 	return(-1);
    379 }
    380 
    381 /*
    382  * get_ident:
    383  *	Get the score structure of a player
    384  */
    385 static IDENT *
    386 get_ident(uint32_t machine, uint32_t uid, char *name, char team)
    387 {
    388 	IDENT *ip;
    389 	static IDENT punt;
    390 
    391 	for (ip = Scores; ip != NULL; ip = ip->i_next)
    392 		if (ip->i_machine == machine
    393 		&&  ip->i_uid == uid
    394 		&&  ip->i_team == team
    395 		&&  strncmp(ip->i_name, name, NAMELEN) == 0)
    396 			break;
    397 
    398 	if (ip != NULL) {
    399 		if (ip->i_entries < SCOREDECAY)
    400 			ip->i_entries++;
    401 		else
    402 			ip->i_kills = (ip->i_kills * (SCOREDECAY - 1))
    403 				/ SCOREDECAY;
    404 		ip->i_score = ip->i_kills / (double) ip->i_entries;
    405 	}
    406 	else {
    407 		ip = malloc(sizeof(*ip));
    408 		if (ip == NULL) {
    409 			/* Fourth down, time to punt */
    410 			ip = &punt;
    411 		}
    412 		ip->i_machine = machine;
    413 		ip->i_team = team;
    414 		ip->i_uid = uid;
    415 		strncpy(ip->i_name, name, NAMELEN);
    416 		ip->i_kills = 0;
    417 		ip->i_entries = 1;
    418 		ip->i_score = 0;
    419 		ip->i_absorbed = 0;
    420 		ip->i_faced = 0;
    421 		ip->i_shot = 0;
    422 		ip->i_robbed = 0;
    423 		ip->i_slime = 0;
    424 		ip->i_missed = 0;
    425 		ip->i_ducked = 0;
    426 		ip->i_gkills = ip->i_bkills = ip->i_deaths = 0;
    427 		ip->i_stillb = ip->i_saved = 0;
    428 		ip->i_next = Scores;
    429 		Scores = ip;
    430 	}
    431 
    432 	return ip;
    433 }
    434