driver.c revision 1.33 1 1.33 dholland /* $NetBSD: driver.c,v 1.33 2014/03/29 23:44:38 dholland Exp $ */
2 1.1 mrg /*
3 1.9 wiz * Copyright (c) 1983-2003, Regents of the University of California.
4 1.9 wiz * All rights reserved.
5 1.9 wiz *
6 1.9 wiz * Redistribution and use in source and binary forms, with or without
7 1.9 wiz * modification, are permitted provided that the following conditions are
8 1.9 wiz * met:
9 1.9 wiz *
10 1.9 wiz * + Redistributions of source code must retain the above copyright
11 1.9 wiz * notice, this list of conditions and the following disclaimer.
12 1.9 wiz * + Redistributions in binary form must reproduce the above copyright
13 1.9 wiz * notice, this list of conditions and the following disclaimer in the
14 1.9 wiz * documentation and/or other materials provided with the distribution.
15 1.9 wiz * + Neither the name of the University of California, San Francisco nor
16 1.9 wiz * the names of its contributors may be used to endorse or promote
17 1.9 wiz * products derived from this software without specific prior written
18 1.9 wiz * permission.
19 1.9 wiz *
20 1.9 wiz * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 1.9 wiz * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.9 wiz * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 1.9 wiz * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 1.9 wiz * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 1.9 wiz * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 1.9 wiz * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.9 wiz * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.9 wiz * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.9 wiz * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 1.9 wiz * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 mrg */
32 1.1 mrg
33 1.2 lukem #include <sys/cdefs.h>
34 1.2 lukem #ifndef lint
35 1.33 dholland __RCSID("$NetBSD: driver.c,v 1.33 2014/03/29 23:44:38 dholland Exp $");
36 1.2 lukem #endif /* not lint */
37 1.2 lukem
38 1.33 dholland #include <sys/types.h>
39 1.33 dholland #include <sys/socket.h>
40 1.17 dholland #include <sys/stat.h>
41 1.17 dholland #include <sys/time.h>
42 1.17 dholland #include <err.h>
43 1.17 dholland #include <errno.h>
44 1.17 dholland #include <signal.h>
45 1.17 dholland #include <stdlib.h>
46 1.17 dholland #include <unistd.h>
47 1.31 dholland
48 1.31 dholland #include "hunt.h"
49 1.31 dholland #include "pathnames.h"
50 1.17 dholland
51 1.28 dholland #ifdef INTERNET
52 1.30 dholland static uint16_t Test_port = TEST_PORT;
53 1.28 dholland #else
54 1.31 dholland static const char Sock_name[] = PATH_HUNTSOCKET;
55 1.31 dholland static const char Stat_name[] = PATH_STATSOCKET;
56 1.28 dholland #endif
57 1.28 dholland
58 1.20 dholland static SOCKET Daemon;
59 1.17 dholland
60 1.17 dholland #ifdef INTERNET
61 1.20 dholland static int Test_socket; /* test socket to answer datagrams */
62 1.24 dholland static bool inetd_spawned; /* invoked via inetd */
63 1.24 dholland static bool standard_port = true; /* true if listening on standard port */
64 1.20 dholland static u_short sock_port; /* port # of tcp listen socket */
65 1.20 dholland static u_short stat_port; /* port # of statistics tcp socket */
66 1.17 dholland #define DAEMON_SIZE (sizeof Daemon)
67 1.17 dholland #else
68 1.17 dholland #define DAEMON_SIZE (sizeof Daemon - 1)
69 1.17 dholland #endif
70 1.17 dholland
71 1.29 dholland #ifdef VOLCANO
72 1.29 dholland static int volcano = 0; /* Explosion size */
73 1.29 dholland #endif
74 1.29 dholland
75 1.29 dholland static int Status; /* stat socket */
76 1.29 dholland
77 1.17 dholland static void clear_scores(void);
78 1.24 dholland static bool havechar(PLAYER *, int);
79 1.17 dholland static void init(void);
80 1.17 dholland static void makeboots(void);
81 1.17 dholland static void send_stats(void);
82 1.24 dholland static void zap(PLAYER *, bool, int);
83 1.2 lukem
84 1.1 mrg
85 1.1 mrg /*
86 1.1 mrg * main:
87 1.1 mrg * The main program.
88 1.1 mrg */
89 1.2 lukem int
90 1.26 dholland main(int ac, char **av)
91 1.1 mrg {
92 1.17 dholland PLAYER *pp;
93 1.17 dholland #ifdef INTERNET
94 1.17 dholland u_short msg;
95 1.22 christos short reply;
96 1.17 dholland socklen_t namelen;
97 1.17 dholland SOCKET test;
98 1.17 dholland #endif
99 1.24 dholland static bool first = true;
100 1.24 dholland static bool server = false;
101 1.17 dholland int c, i;
102 1.17 dholland const int linger = 90 * 1000;
103 1.1 mrg
104 1.2 lukem while ((c = getopt(ac, av, "sp:")) != -1) {
105 1.1 mrg switch (c) {
106 1.1 mrg case 's':
107 1.24 dholland server = true;
108 1.1 mrg break;
109 1.17 dholland #ifdef INTERNET
110 1.1 mrg case 'p':
111 1.24 dholland standard_port = false;
112 1.1 mrg Test_port = atoi(optarg);
113 1.1 mrg break;
114 1.17 dholland #endif
115 1.1 mrg default:
116 1.1 mrg erred:
117 1.1 mrg fprintf(stderr, "Usage: %s [-s] [-p port]\n", av[0]);
118 1.1 mrg exit(1);
119 1.1 mrg }
120 1.1 mrg }
121 1.1 mrg if (optind < ac)
122 1.1 mrg goto erred;
123 1.1 mrg
124 1.1 mrg init();
125 1.1 mrg
126 1.1 mrg
127 1.1 mrg again:
128 1.1 mrg do {
129 1.1 mrg errno = 0;
130 1.8 mycroft while (poll(fdset, 3+MAXPL+MAXMON, INFTIM) < 0)
131 1.1 mrg {
132 1.1 mrg if (errno != EINTR)
133 1.32 dholland complain(LOG_WARNING, "poll");
134 1.1 mrg errno = 0;
135 1.1 mrg }
136 1.17 dholland #ifdef INTERNET
137 1.8 mycroft if (fdset[2].revents & POLLIN) {
138 1.1 mrg namelen = DAEMON_SIZE;
139 1.15 dholland (void) recvfrom(Test_socket, &msg, sizeof msg,
140 1.1 mrg 0, (struct sockaddr *) &test, &namelen);
141 1.1 mrg switch (ntohs(msg)) {
142 1.1 mrg case C_MESSAGE:
143 1.1 mrg if (Nplayer <= 0)
144 1.1 mrg break;
145 1.1 mrg reply = htons((u_short) Nplayer);
146 1.15 dholland (void) sendto(Test_socket, &reply,
147 1.1 mrg sizeof reply, 0,
148 1.1 mrg (struct sockaddr *) &test, DAEMON_SIZE);
149 1.1 mrg break;
150 1.1 mrg case C_SCORES:
151 1.1 mrg reply = htons(stat_port);
152 1.15 dholland (void) sendto(Test_socket, &reply,
153 1.1 mrg sizeof reply, 0,
154 1.1 mrg (struct sockaddr *) &test, DAEMON_SIZE);
155 1.1 mrg break;
156 1.1 mrg case C_PLAYER:
157 1.1 mrg case C_MONITOR:
158 1.1 mrg if (msg == C_MONITOR && Nplayer <= 0)
159 1.1 mrg break;
160 1.1 mrg reply = htons(sock_port);
161 1.15 dholland (void) sendto(Test_socket, &reply,
162 1.1 mrg sizeof reply, 0,
163 1.1 mrg (struct sockaddr *) &test, DAEMON_SIZE);
164 1.1 mrg break;
165 1.1 mrg }
166 1.1 mrg }
167 1.17 dholland #endif
168 1.8 mycroft {
169 1.8 mycroft for (pp = Player, i = 0; pp < End_player; pp++, i++)
170 1.8 mycroft if (havechar(pp, i + 3)) {
171 1.1 mrg execute(pp);
172 1.1 mrg pp->p_nexec++;
173 1.1 mrg }
174 1.17 dholland #ifdef MONITOR
175 1.8 mycroft for (pp = Monitor, i = 0; pp < End_monitor; pp++, i++)
176 1.8 mycroft if (havechar(pp, i + MAXPL + 3)) {
177 1.1 mrg mon_execute(pp);
178 1.1 mrg pp->p_nexec++;
179 1.1 mrg }
180 1.17 dholland #endif
181 1.1 mrg moveshots();
182 1.8 mycroft for (pp = Player, i = 0; pp < End_player; )
183 1.1 mrg if (pp->p_death[0] != '\0')
184 1.24 dholland zap(pp, true, i + 3);
185 1.1 mrg else
186 1.8 mycroft pp++, i++;
187 1.17 dholland #ifdef MONITOR
188 1.8 mycroft for (pp = Monitor, i = 0; pp < End_monitor; )
189 1.1 mrg if (pp->p_death[0] != '\0')
190 1.24 dholland zap(pp, false, i + MAXPL + 3);
191 1.1 mrg else
192 1.8 mycroft pp++, i++;
193 1.17 dholland #endif
194 1.1 mrg }
195 1.8 mycroft if (fdset[0].revents & POLLIN)
196 1.1 mrg if (answer()) {
197 1.27 dholland if (first) {
198 1.27 dholland /* announce start of game? */
199 1.27 dholland }
200 1.24 dholland first = false;
201 1.1 mrg }
202 1.8 mycroft if (fdset[1].revents & POLLIN)
203 1.1 mrg send_stats();
204 1.8 mycroft for (pp = Player, i = 0; pp < End_player; pp++, i++) {
205 1.8 mycroft if (fdset[i + 3].revents & POLLIN)
206 1.1 mrg sendcom(pp, READY, pp->p_nexec);
207 1.1 mrg pp->p_nexec = 0;
208 1.1 mrg (void) fflush(pp->p_output);
209 1.1 mrg }
210 1.17 dholland #ifdef MONITOR
211 1.8 mycroft for (pp = Monitor, i = 0; pp < End_monitor; pp++, i++) {
212 1.8 mycroft if (fdset[i + MAXPL + 3].revents & POLLIN)
213 1.1 mrg sendcom(pp, READY, pp->p_nexec);
214 1.1 mrg pp->p_nexec = 0;
215 1.1 mrg (void) fflush(pp->p_output);
216 1.1 mrg }
217 1.17 dholland #endif
218 1.1 mrg } while (Nplayer > 0);
219 1.1 mrg
220 1.8 mycroft if (poll(fdset, 3+MAXPL+MAXMON, linger) > 0) {
221 1.1 mrg goto again;
222 1.1 mrg }
223 1.1 mrg if (server) {
224 1.1 mrg clear_scores();
225 1.1 mrg makemaze();
226 1.1 mrg clearwalls();
227 1.17 dholland #ifdef BOOTS
228 1.1 mrg makeboots();
229 1.17 dholland #endif
230 1.24 dholland first = true;
231 1.1 mrg goto again;
232 1.1 mrg }
233 1.1 mrg
234 1.17 dholland #ifdef MONITOR
235 1.8 mycroft for (pp = Monitor, i = 0; pp < End_monitor; i++)
236 1.24 dholland zap(pp, false, i + MAXPL + 3);
237 1.17 dholland #endif
238 1.1 mrg cleanup(0);
239 1.2 lukem /* NOTREACHED */
240 1.2 lukem return(0);
241 1.1 mrg }
242 1.1 mrg
243 1.1 mrg /*
244 1.1 mrg * init:
245 1.1 mrg * Initialize the global parameters.
246 1.1 mrg */
247 1.2 lukem static void
248 1.16 dholland init(void)
249 1.1 mrg {
250 1.17 dholland int i;
251 1.17 dholland #ifdef INTERNET
252 1.17 dholland SOCKET test_port;
253 1.17 dholland int msg;
254 1.17 dholland socklen_t len;
255 1.17 dholland #endif
256 1.1 mrg
257 1.17 dholland #ifndef DEBUG
258 1.17 dholland #ifdef TIOCNOTTY
259 1.1 mrg (void) ioctl(fileno(stdout), TIOCNOTTY, NULL);
260 1.17 dholland #endif
261 1.1 mrg (void) setpgrp(getpid(), getpid());
262 1.1 mrg (void) signal(SIGHUP, SIG_IGN);
263 1.1 mrg (void) signal(SIGINT, SIG_IGN);
264 1.1 mrg (void) signal(SIGQUIT, SIG_IGN);
265 1.1 mrg (void) signal(SIGTERM, cleanup);
266 1.17 dholland #endif
267 1.1 mrg
268 1.4 mrg (void) chdir("/var/tmp"); /* just in case it core dumps */
269 1.1 mrg (void) umask(0); /* No privacy at all! */
270 1.1 mrg (void) signal(SIGPIPE, SIG_IGN);
271 1.1 mrg
272 1.17 dholland #ifdef LOG
273 1.7 lukem openlog("huntd", LOG_PID, LOG_DAEMON);
274 1.17 dholland #endif
275 1.1 mrg
276 1.1 mrg /*
277 1.1 mrg * Initialize statistics socket
278 1.1 mrg */
279 1.17 dholland #ifdef INTERNET
280 1.1 mrg Daemon.sin_family = SOCK_FAMILY;
281 1.1 mrg Daemon.sin_addr.s_addr = INADDR_ANY;
282 1.1 mrg Daemon.sin_port = 0;
283 1.17 dholland #else
284 1.1 mrg Daemon.sun_family = SOCK_FAMILY;
285 1.1 mrg (void) strcpy(Daemon.sun_path, Stat_name);
286 1.17 dholland #endif
287 1.1 mrg
288 1.1 mrg Status = socket(SOCK_FAMILY, SOCK_STREAM, 0);
289 1.1 mrg if (bind(Status, (struct sockaddr *) &Daemon, DAEMON_SIZE) < 0) {
290 1.1 mrg if (errno == EADDRINUSE)
291 1.1 mrg exit(0);
292 1.1 mrg else {
293 1.32 dholland complain(LOG_ERR, "bind");
294 1.1 mrg cleanup(1);
295 1.1 mrg }
296 1.1 mrg }
297 1.1 mrg (void) listen(Status, 5);
298 1.1 mrg
299 1.17 dholland #ifdef INTERNET
300 1.1 mrg len = sizeof (SOCKET);
301 1.1 mrg if (getsockname(Status, (struct sockaddr *) &Daemon, &len) < 0) {
302 1.32 dholland complain(LOG_ERR, "getsockname");
303 1.1 mrg exit(1);
304 1.1 mrg }
305 1.1 mrg stat_port = ntohs(Daemon.sin_port);
306 1.17 dholland #endif
307 1.1 mrg
308 1.1 mrg /*
309 1.1 mrg * Initialize main socket
310 1.1 mrg */
311 1.17 dholland #ifdef INTERNET
312 1.1 mrg Daemon.sin_family = SOCK_FAMILY;
313 1.1 mrg Daemon.sin_addr.s_addr = INADDR_ANY;
314 1.1 mrg Daemon.sin_port = 0;
315 1.17 dholland #else
316 1.1 mrg Daemon.sun_family = SOCK_FAMILY;
317 1.1 mrg (void) strcpy(Daemon.sun_path, Sock_name);
318 1.17 dholland #endif
319 1.1 mrg
320 1.1 mrg Socket = socket(SOCK_FAMILY, SOCK_STREAM, 0);
321 1.17 dholland #if defined(INTERNET)
322 1.1 mrg msg = 1;
323 1.1 mrg if (setsockopt(Socket, SOL_SOCKET, SO_USELOOPBACK, &msg, sizeof msg)<0)
324 1.32 dholland complain(LOG_WARNING, "setsockopt loopback");
325 1.17 dholland #endif
326 1.1 mrg if (bind(Socket, (struct sockaddr *) &Daemon, DAEMON_SIZE) < 0) {
327 1.1 mrg if (errno == EADDRINUSE)
328 1.1 mrg exit(0);
329 1.1 mrg else {
330 1.32 dholland complain(LOG_ERR, "bind");
331 1.1 mrg cleanup(1);
332 1.1 mrg }
333 1.1 mrg }
334 1.1 mrg (void) listen(Socket, 5);
335 1.1 mrg
336 1.17 dholland #ifdef INTERNET
337 1.1 mrg len = sizeof (SOCKET);
338 1.1 mrg if (getsockname(Socket, (struct sockaddr *) &Daemon, &len) < 0) {
339 1.32 dholland complain(LOG_ERR, "getsockname");
340 1.1 mrg exit(1);
341 1.1 mrg }
342 1.1 mrg sock_port = ntohs(Daemon.sin_port);
343 1.17 dholland #endif
344 1.1 mrg
345 1.1 mrg /*
346 1.12 elad * Initialize minimal poll mask
347 1.1 mrg */
348 1.8 mycroft fdset[0].fd = Socket;
349 1.8 mycroft fdset[0].events = POLLIN;
350 1.8 mycroft fdset[1].fd = Status;
351 1.8 mycroft fdset[1].events = POLLIN;
352 1.1 mrg
353 1.17 dholland #ifdef INTERNET
354 1.1 mrg len = sizeof (SOCKET);
355 1.1 mrg if (getsockname(0, (struct sockaddr *) &test_port, &len) >= 0
356 1.1 mrg && test_port.sin_family == AF_INET) {
357 1.24 dholland inetd_spawned = true;
358 1.1 mrg Test_socket = 0;
359 1.1 mrg if (test_port.sin_port != htons((u_short) Test_port)) {
360 1.24 dholland standard_port = false;
361 1.1 mrg Test_port = ntohs(test_port.sin_port);
362 1.1 mrg }
363 1.1 mrg } else {
364 1.1 mrg test_port = Daemon;
365 1.1 mrg test_port.sin_port = htons((u_short) Test_port);
366 1.1 mrg
367 1.1 mrg Test_socket = socket(SOCK_FAMILY, SOCK_DGRAM, 0);
368 1.1 mrg if (bind(Test_socket, (struct sockaddr *) &test_port,
369 1.1 mrg DAEMON_SIZE) < 0) {
370 1.32 dholland complain(LOG_ERR, "bind");
371 1.1 mrg exit(1);
372 1.1 mrg }
373 1.1 mrg (void) listen(Test_socket, 5);
374 1.1 mrg }
375 1.1 mrg
376 1.8 mycroft fdset[2].fd = Test_socket;
377 1.8 mycroft fdset[2].events = POLLIN;
378 1.17 dholland #else
379 1.8 mycroft fdset[2].fd = -1;
380 1.17 dholland #endif
381 1.1 mrg
382 1.23 dholland srandom(time(NULL));
383 1.1 mrg makemaze();
384 1.17 dholland #ifdef BOOTS
385 1.1 mrg makeboots();
386 1.17 dholland #endif
387 1.1 mrg
388 1.1 mrg for (i = 0; i < NASCII; i++)
389 1.24 dholland See_over[i] = true;
390 1.24 dholland See_over[DOOR] = false;
391 1.24 dholland See_over[WALL1] = false;
392 1.24 dholland See_over[WALL2] = false;
393 1.24 dholland See_over[WALL3] = false;
394 1.17 dholland #ifdef REFLECT
395 1.24 dholland See_over[WALL4] = false;
396 1.24 dholland See_over[WALL5] = false;
397 1.17 dholland #endif
398 1.1 mrg
399 1.1 mrg }
400 1.1 mrg
401 1.17 dholland #ifdef BOOTS
402 1.1 mrg /*
403 1.1 mrg * makeboots:
404 1.1 mrg * Put the boots in the maze
405 1.1 mrg */
406 1.2 lukem static void
407 1.16 dholland makeboots(void)
408 1.1 mrg {
409 1.17 dholland int x, y;
410 1.17 dholland PLAYER *pp;
411 1.1 mrg
412 1.1 mrg do {
413 1.1 mrg x = rand_num(WIDTH - 1) + 1;
414 1.1 mrg y = rand_num(HEIGHT - 1) + 1;
415 1.1 mrg } while (Maze[y][x] != SPACE);
416 1.1 mrg Maze[y][x] = BOOT_PAIR;
417 1.1 mrg for (pp = Boot; pp < &Boot[NBOOTS]; pp++)
418 1.1 mrg pp->p_flying = -1;
419 1.1 mrg }
420 1.17 dholland #endif
421 1.1 mrg
422 1.1 mrg
423 1.1 mrg /*
424 1.1 mrg * checkdam:
425 1.1 mrg * Check the damage to the given player, and see if s/he is killed
426 1.1 mrg */
427 1.2 lukem void
428 1.16 dholland checkdam(PLAYER *ouch, PLAYER *gotcha, IDENT *credit, int amt,
429 1.16 dholland char this_shot_type)
430 1.1 mrg {
431 1.17 dholland const char *cp;
432 1.1 mrg
433 1.1 mrg if (ouch->p_death[0] != '\0')
434 1.1 mrg return;
435 1.17 dholland #ifdef BOOTS
436 1.13 dholland if (this_shot_type == SLIME)
437 1.1 mrg switch (ouch->p_nboots) {
438 1.1 mrg default:
439 1.1 mrg break;
440 1.1 mrg case 1:
441 1.1 mrg amt = (amt + 1) / 2;
442 1.1 mrg break;
443 1.1 mrg case 2:
444 1.1 mrg if (gotcha != NULL)
445 1.1 mrg message(gotcha, "He has boots on!");
446 1.1 mrg return;
447 1.1 mrg }
448 1.17 dholland #endif
449 1.1 mrg ouch->p_damage += amt;
450 1.1 mrg if (ouch->p_damage <= ouch->p_damcap) {
451 1.14 dholland (void) snprintf(Buf, sizeof(Buf), "%2d", ouch->p_damage);
452 1.1 mrg cgoto(ouch, STAT_DAM_ROW, STAT_VALUE_COL);
453 1.1 mrg outstr(ouch, Buf, 2);
454 1.1 mrg return;
455 1.1 mrg }
456 1.1 mrg
457 1.1 mrg /* Someone DIED */
458 1.13 dholland switch (this_shot_type) {
459 1.1 mrg default:
460 1.1 mrg cp = "Killed";
461 1.1 mrg break;
462 1.17 dholland #ifdef FLY
463 1.1 mrg case FALL:
464 1.1 mrg cp = "Killed on impact";
465 1.1 mrg break;
466 1.17 dholland #endif
467 1.1 mrg case KNIFE:
468 1.1 mrg cp = "Stabbed to death";
469 1.1 mrg ouch->p_ammo = 0; /* No exploding */
470 1.1 mrg break;
471 1.1 mrg case SHOT:
472 1.1 mrg cp = "Shot to death";
473 1.1 mrg break;
474 1.1 mrg case GRENADE:
475 1.1 mrg case SATCHEL:
476 1.1 mrg case BOMB:
477 1.1 mrg cp = "Bombed";
478 1.1 mrg break;
479 1.1 mrg case MINE:
480 1.1 mrg case GMINE:
481 1.1 mrg cp = "Blown apart";
482 1.1 mrg break;
483 1.17 dholland #ifdef OOZE
484 1.1 mrg case SLIME:
485 1.1 mrg cp = "Slimed";
486 1.1 mrg if (credit != NULL)
487 1.1 mrg credit->i_slime++;
488 1.1 mrg break;
489 1.17 dholland #endif
490 1.17 dholland #ifdef VOLCANO
491 1.1 mrg case LAVA:
492 1.1 mrg cp = "Baked";
493 1.1 mrg break;
494 1.17 dholland #endif
495 1.17 dholland #ifdef DRONE
496 1.1 mrg case DSHOT:
497 1.1 mrg cp = "Eliminated";
498 1.1 mrg break;
499 1.17 dholland #endif
500 1.1 mrg }
501 1.1 mrg if (credit == NULL) {
502 1.14 dholland (void) snprintf(ouch->p_death, sizeof(ouch->p_death),
503 1.14 dholland "| %s by %s |", cp,
504 1.13 dholland (this_shot_type == MINE || this_shot_type == GMINE) ?
505 1.1 mrg "a mine" : "act of God");
506 1.1 mrg return;
507 1.1 mrg }
508 1.1 mrg
509 1.14 dholland (void) snprintf(ouch->p_death, sizeof(ouch->p_death),
510 1.14 dholland "| %s by %s |", cp, credit->i_name);
511 1.1 mrg
512 1.1 mrg if (ouch == gotcha) { /* No use killing yourself */
513 1.1 mrg credit->i_kills--;
514 1.1 mrg credit->i_bkills++;
515 1.1 mrg }
516 1.1 mrg else if (ouch->p_ident->i_team == ' '
517 1.1 mrg || ouch->p_ident->i_team != credit->i_team) {
518 1.1 mrg credit->i_kills++;
519 1.1 mrg credit->i_gkills++;
520 1.1 mrg }
521 1.1 mrg else {
522 1.1 mrg credit->i_kills--;
523 1.1 mrg credit->i_bkills++;
524 1.1 mrg }
525 1.1 mrg credit->i_score = credit->i_kills / (double) credit->i_entries;
526 1.1 mrg ouch->p_ident->i_deaths++;
527 1.1 mrg if (ouch->p_nchar == 0)
528 1.1 mrg ouch->p_ident->i_stillb++;
529 1.1 mrg if (gotcha == NULL)
530 1.1 mrg return;
531 1.1 mrg gotcha->p_damcap += STABDAM;
532 1.1 mrg gotcha->p_damage -= STABDAM;
533 1.1 mrg if (gotcha->p_damage < 0)
534 1.1 mrg gotcha->p_damage = 0;
535 1.14 dholland (void) snprintf(Buf, sizeof(Buf), "%2d/%2d", gotcha->p_damage,
536 1.14 dholland gotcha->p_damcap);
537 1.1 mrg cgoto(gotcha, STAT_DAM_ROW, STAT_VALUE_COL);
538 1.1 mrg outstr(gotcha, Buf, 5);
539 1.14 dholland (void) snprintf(Buf, sizeof(Buf), "%3d",
540 1.14 dholland (gotcha->p_damcap - MAXDAM) / 2);
541 1.1 mrg cgoto(gotcha, STAT_KILL_ROW, STAT_VALUE_COL);
542 1.1 mrg outstr(gotcha, Buf, 3);
543 1.14 dholland (void) snprintf(Buf, sizeof(Buf), "%5.2f", gotcha->p_ident->i_score);
544 1.1 mrg for (ouch = Player; ouch < End_player; ouch++) {
545 1.1 mrg cgoto(ouch, STAT_PLAY_ROW + 1 + (gotcha - Player),
546 1.1 mrg STAT_NAME_COL);
547 1.1 mrg outstr(ouch, Buf, 5);
548 1.1 mrg }
549 1.17 dholland #ifdef MONITOR
550 1.1 mrg for (ouch = Monitor; ouch < End_monitor; ouch++) {
551 1.1 mrg cgoto(ouch, STAT_PLAY_ROW + 1 + (gotcha - Player),
552 1.1 mrg STAT_NAME_COL);
553 1.1 mrg outstr(ouch, Buf, 5);
554 1.1 mrg }
555 1.17 dholland #endif
556 1.1 mrg }
557 1.1 mrg
558 1.1 mrg /*
559 1.1 mrg * zap:
560 1.1 mrg * Kill off a player and take him out of the game.
561 1.1 mrg */
562 1.2 lukem static void
563 1.24 dholland zap(PLAYER *pp, bool was_player, int i)
564 1.1 mrg {
565 1.17 dholland int n, len;
566 1.17 dholland BULLET *bp;
567 1.17 dholland PLAYER *np;
568 1.17 dholland int x, y;
569 1.1 mrg
570 1.1 mrg if (was_player) {
571 1.1 mrg if (pp->p_undershot)
572 1.1 mrg fixshots(pp->p_y, pp->p_x, pp->p_over);
573 1.24 dholland drawplayer(pp, false);
574 1.1 mrg Nplayer--;
575 1.1 mrg }
576 1.1 mrg
577 1.1 mrg len = strlen(pp->p_death); /* Display the cause of death */
578 1.1 mrg x = (WIDTH - len) / 2;
579 1.1 mrg cgoto(pp, HEIGHT / 2, x);
580 1.1 mrg outstr(pp, pp->p_death, len);
581 1.8 mycroft for (n = 1; n < len; n++)
582 1.8 mycroft pp->p_death[n] = '-';
583 1.1 mrg pp->p_death[0] = '+';
584 1.1 mrg pp->p_death[len - 1] = '+';
585 1.1 mrg cgoto(pp, HEIGHT / 2 - 1, x);
586 1.1 mrg outstr(pp, pp->p_death, len);
587 1.1 mrg cgoto(pp, HEIGHT / 2 + 1, x);
588 1.1 mrg outstr(pp, pp->p_death, len);
589 1.1 mrg cgoto(pp, HEIGHT, 0);
590 1.1 mrg
591 1.17 dholland #ifdef MONITOR
592 1.1 mrg if (was_player) {
593 1.17 dholland #endif
594 1.1 mrg for (bp = Bullets; bp != NULL; bp = bp->b_next) {
595 1.1 mrg if (bp->b_owner == pp)
596 1.1 mrg bp->b_owner = NULL;
597 1.1 mrg if (bp->b_x == pp->p_x && bp->b_y == pp->p_y)
598 1.1 mrg bp->b_over = SPACE;
599 1.1 mrg }
600 1.1 mrg
601 1.8 mycroft n = rand_num(pp->p_ammo);
602 1.1 mrg x = rand_num(pp->p_ammo);
603 1.8 mycroft if (x > n)
604 1.8 mycroft n = x;
605 1.1 mrg if (pp->p_ammo == 0)
606 1.1 mrg x = 0;
607 1.8 mycroft else if (n == pp->p_ammo - 1) {
608 1.1 mrg x = pp->p_ammo;
609 1.1 mrg len = SLIME;
610 1.1 mrg }
611 1.1 mrg else {
612 1.1 mrg for (x = MAXBOMB - 1; x > 0; x--)
613 1.8 mycroft if (n >= shot_req[x])
614 1.1 mrg break;
615 1.1 mrg for (y = MAXSLIME - 1; y > 0; y--)
616 1.8 mycroft if (n >= slime_req[y])
617 1.1 mrg break;
618 1.1 mrg if (y >= 0 && slime_req[y] > shot_req[x]) {
619 1.1 mrg x = slime_req[y];
620 1.1 mrg len = SLIME;
621 1.1 mrg }
622 1.1 mrg else if (x != 0) {
623 1.1 mrg len = shot_type[x];
624 1.1 mrg x = shot_req[x];
625 1.1 mrg }
626 1.1 mrg }
627 1.1 mrg if (x > 0) {
628 1.1 mrg (void) add_shot(len, pp->p_y, pp->p_x, pp->p_face, x,
629 1.24 dholland NULL, true, SPACE);
630 1.14 dholland (void) snprintf(Buf, sizeof(Buf), "%s detonated.",
631 1.1 mrg pp->p_ident->i_name);
632 1.1 mrg for (np = Player; np < End_player; np++)
633 1.1 mrg message(np, Buf);
634 1.17 dholland #ifdef MONITOR
635 1.1 mrg for (np = Monitor; np < End_monitor; np++)
636 1.1 mrg message(np, Buf);
637 1.17 dholland #endif
638 1.17 dholland #ifdef BOOTS
639 1.1 mrg while (pp->p_nboots-- > 0) {
640 1.1 mrg for (np = Boot; np < &Boot[NBOOTS]; np++)
641 1.1 mrg if (np->p_flying < 0)
642 1.1 mrg break;
643 1.1 mrg if (np >= &Boot[NBOOTS])
644 1.2 lukem err(1, "Too many boots");
645 1.24 dholland np->p_undershot = false;
646 1.1 mrg np->p_x = pp->p_x;
647 1.1 mrg np->p_y = pp->p_y;
648 1.1 mrg np->p_flying = rand_num(20);
649 1.1 mrg np->p_flyx = 2 * rand_num(6) - 5;
650 1.1 mrg np->p_flyy = 2 * rand_num(6) - 5;
651 1.1 mrg np->p_over = SPACE;
652 1.1 mrg np->p_face = BOOT;
653 1.1 mrg showexpl(np->p_y, np->p_x, BOOT);
654 1.1 mrg }
655 1.17 dholland #endif
656 1.1 mrg }
657 1.17 dholland #ifdef BOOTS
658 1.1 mrg else if (pp->p_nboots > 0) {
659 1.1 mrg if (pp->p_nboots == 2)
660 1.1 mrg Maze[pp->p_y][pp->p_x] = BOOT_PAIR;
661 1.1 mrg else
662 1.1 mrg Maze[pp->p_y][pp->p_x] = BOOT;
663 1.1 mrg if (pp->p_undershot)
664 1.1 mrg fixshots(pp->p_y, pp->p_x,
665 1.1 mrg Maze[pp->p_y][pp->p_x]);
666 1.1 mrg }
667 1.17 dholland #endif
668 1.1 mrg
669 1.17 dholland #ifdef VOLCANO
670 1.1 mrg volcano += pp->p_ammo - x;
671 1.1 mrg if (rand_num(100) < volcano / 50) {
672 1.1 mrg do {
673 1.1 mrg x = rand_num(WIDTH / 2) + WIDTH / 4;
674 1.1 mrg y = rand_num(HEIGHT / 2) + HEIGHT / 4;
675 1.1 mrg } while (Maze[y][x] != SPACE);
676 1.1 mrg (void) add_shot(LAVA, y, x, LEFTS, volcano,
677 1.24 dholland NULL, true, SPACE);
678 1.1 mrg for (np = Player; np < End_player; np++)
679 1.1 mrg message(np, "Volcano eruption.");
680 1.1 mrg volcano = 0;
681 1.1 mrg }
682 1.17 dholland #endif
683 1.1 mrg
684 1.17 dholland #ifdef DRONE
685 1.1 mrg if (rand_num(100) < 2) {
686 1.1 mrg do {
687 1.1 mrg x = rand_num(WIDTH / 2) + WIDTH / 4;
688 1.1 mrg y = rand_num(HEIGHT / 2) + HEIGHT / 4;
689 1.1 mrg } while (Maze[y][x] != SPACE);
690 1.1 mrg add_shot(DSHOT, y, x, rand_dir(),
691 1.1 mrg shot_req[MINDSHOT +
692 1.1 mrg rand_num(MAXBOMB - MINDSHOT)],
693 1.24 dholland NULL, false, SPACE);
694 1.1 mrg }
695 1.17 dholland #endif
696 1.1 mrg
697 1.1 mrg sendcom(pp, ENDWIN);
698 1.1 mrg (void) putc(' ', pp->p_output);
699 1.1 mrg (void) fclose(pp->p_output);
700 1.1 mrg
701 1.1 mrg End_player--;
702 1.1 mrg if (pp != End_player) {
703 1.1 mrg memcpy(pp, End_player, sizeof (PLAYER));
704 1.8 mycroft fdset[i] = fdset[End_player - Player + 3];
705 1.8 mycroft fdset[End_player - Player + 3].fd = -1;
706 1.14 dholland (void) snprintf(Buf, sizeof(Buf), "%5.2f%c%-10.10s %c",
707 1.1 mrg pp->p_ident->i_score, stat_char(pp),
708 1.1 mrg pp->p_ident->i_name, pp->p_ident->i_team);
709 1.8 mycroft n = STAT_PLAY_ROW + 1 + (pp - Player);
710 1.1 mrg for (np = Player; np < End_player; np++) {
711 1.8 mycroft cgoto(np, n, STAT_NAME_COL);
712 1.1 mrg outstr(np, Buf, STAT_NAME_LEN);
713 1.1 mrg }
714 1.17 dholland #ifdef MONITOR
715 1.1 mrg for (np = Monitor; np < End_monitor; np++) {
716 1.8 mycroft cgoto(np, n, STAT_NAME_COL);
717 1.1 mrg outstr(np, Buf, STAT_NAME_LEN);
718 1.1 mrg }
719 1.17 dholland #endif
720 1.8 mycroft } else
721 1.8 mycroft fdset[i].fd = -1;
722 1.1 mrg
723 1.1 mrg /* Erase the last player */
724 1.8 mycroft n = STAT_PLAY_ROW + 1 + Nplayer;
725 1.1 mrg for (np = Player; np < End_player; np++) {
726 1.8 mycroft cgoto(np, n, STAT_NAME_COL);
727 1.1 mrg ce(np);
728 1.1 mrg }
729 1.17 dholland #ifdef MONITOR
730 1.1 mrg for (np = Monitor; np < End_monitor; np++) {
731 1.8 mycroft cgoto(np, n, STAT_NAME_COL);
732 1.1 mrg ce(np);
733 1.1 mrg }
734 1.1 mrg }
735 1.1 mrg else {
736 1.1 mrg sendcom(pp, ENDWIN);
737 1.1 mrg (void) putc(LAST_PLAYER, pp->p_output);
738 1.1 mrg (void) fclose(pp->p_output);
739 1.1 mrg
740 1.1 mrg End_monitor--;
741 1.1 mrg if (pp != End_monitor) {
742 1.1 mrg memcpy(pp, End_monitor, sizeof (PLAYER));
743 1.8 mycroft fdset[i] = fdset[End_monitor - Monitor + MAXPL + 3];
744 1.8 mycroft fdset[End_monitor - Monitor + MAXPL + 3].fd = -1;
745 1.14 dholland (void) snprintf(Buf, sizeof(Buf), "%5.5s %-10.10s %c",
746 1.14 dholland " ",
747 1.1 mrg pp->p_ident->i_name, pp->p_ident->i_team);
748 1.8 mycroft n = STAT_MON_ROW + 1 + (pp - Player);
749 1.1 mrg for (np = Player; np < End_player; np++) {
750 1.8 mycroft cgoto(np, n, STAT_NAME_COL);
751 1.1 mrg outstr(np, Buf, STAT_NAME_LEN);
752 1.1 mrg }
753 1.1 mrg for (np = Monitor; np < End_monitor; np++) {
754 1.8 mycroft cgoto(np, n, STAT_NAME_COL);
755 1.1 mrg outstr(np, Buf, STAT_NAME_LEN);
756 1.1 mrg }
757 1.8 mycroft } else
758 1.8 mycroft fdset[i].fd = -1;
759 1.1 mrg
760 1.1 mrg /* Erase the last monitor */
761 1.8 mycroft n = STAT_MON_ROW + 1 + (End_monitor - Monitor);
762 1.1 mrg for (np = Player; np < End_player; np++) {
763 1.8 mycroft cgoto(np, n, STAT_NAME_COL);
764 1.1 mrg ce(np);
765 1.1 mrg }
766 1.1 mrg for (np = Monitor; np < End_monitor; np++) {
767 1.8 mycroft cgoto(np, n, STAT_NAME_COL);
768 1.1 mrg ce(np);
769 1.1 mrg }
770 1.1 mrg }
771 1.17 dholland #endif
772 1.1 mrg }
773 1.1 mrg
774 1.1 mrg /*
775 1.1 mrg * rand_num:
776 1.1 mrg * Return a random number in a given range.
777 1.1 mrg */
778 1.2 lukem int
779 1.16 dholland rand_num(int range)
780 1.1 mrg {
781 1.23 dholland return (range == 0 ? 0 : random() % range);
782 1.1 mrg }
783 1.1 mrg
784 1.1 mrg /*
785 1.1 mrg * havechar:
786 1.1 mrg * Check to see if we have any characters in the input queue; if
787 1.24 dholland * we do, read them, stash them away, and return true; else return
788 1.24 dholland * false.
789 1.1 mrg */
790 1.24 dholland static bool
791 1.16 dholland havechar(PLAYER *pp, int i)
792 1.1 mrg {
793 1.1 mrg
794 1.1 mrg if (pp->p_ncount < pp->p_nchar)
795 1.24 dholland return true;
796 1.8 mycroft if (!(fdset[i].revents & POLLIN))
797 1.24 dholland return false;
798 1.1 mrg check_again:
799 1.30 dholland pp->p_nchar = read(pp->p_fd, pp->p_cbuf, sizeof pp->p_cbuf);
800 1.30 dholland if (pp->p_nchar < 0 && errno == EINTR) {
801 1.30 dholland goto check_again;
802 1.30 dholland } else if (pp->p_nchar <= 0) {
803 1.1 mrg if (errno == EINTR)
804 1.1 mrg pp->p_cbuf[0] = 'q';
805 1.1 mrg }
806 1.1 mrg pp->p_ncount = 0;
807 1.24 dholland return true;
808 1.1 mrg }
809 1.1 mrg
810 1.1 mrg /*
811 1.1 mrg * cleanup:
812 1.1 mrg * Exit with the given value, cleaning up any droppings lying around
813 1.1 mrg */
814 1.19 dholland void
815 1.30 dholland cleanup(int exitval)
816 1.1 mrg {
817 1.17 dholland PLAYER *pp;
818 1.1 mrg
819 1.1 mrg for (pp = Player; pp < End_player; pp++) {
820 1.1 mrg cgoto(pp, HEIGHT, 0);
821 1.1 mrg sendcom(pp, ENDWIN);
822 1.1 mrg (void) putc(LAST_PLAYER, pp->p_output);
823 1.1 mrg (void) fclose(pp->p_output);
824 1.1 mrg }
825 1.17 dholland #ifdef MONITOR
826 1.1 mrg for (pp = Monitor; pp < End_monitor; pp++) {
827 1.1 mrg cgoto(pp, HEIGHT, 0);
828 1.1 mrg sendcom(pp, ENDWIN);
829 1.1 mrg (void) putc(LAST_PLAYER, pp->p_output);
830 1.1 mrg (void) fclose(pp->p_output);
831 1.1 mrg }
832 1.17 dholland #endif
833 1.1 mrg (void) close(Socket);
834 1.17 dholland #ifdef AF_UNIX_HACK
835 1.1 mrg (void) unlink(Sock_name);
836 1.17 dholland #endif
837 1.1 mrg
838 1.30 dholland exit(exitval);
839 1.1 mrg }
840 1.1 mrg
841 1.1 mrg /*
842 1.1 mrg * send_stats:
843 1.1 mrg * Print stats to requestor
844 1.1 mrg */
845 1.2 lukem static void
846 1.16 dholland send_stats(void)
847 1.1 mrg {
848 1.17 dholland IDENT *ip;
849 1.17 dholland FILE *fp;
850 1.17 dholland int s;
851 1.17 dholland SOCKET sockstruct;
852 1.17 dholland socklen_t socklen;
853 1.1 mrg
854 1.1 mrg /*
855 1.1 mrg * Get the output stream ready
856 1.1 mrg */
857 1.17 dholland #ifdef INTERNET
858 1.1 mrg socklen = sizeof sockstruct;
859 1.17 dholland #else
860 1.1 mrg socklen = sizeof sockstruct - 1;
861 1.17 dholland #endif
862 1.1 mrg s = accept(Status, (struct sockaddr *) &sockstruct, &socklen);
863 1.1 mrg if (s < 0) {
864 1.1 mrg if (errno == EINTR)
865 1.1 mrg return;
866 1.32 dholland complain(LOG_WARNING, "accept");
867 1.1 mrg return;
868 1.1 mrg }
869 1.1 mrg fp = fdopen(s, "w");
870 1.1 mrg if (fp == NULL) {
871 1.32 dholland complain(LOG_WARNING, "fdopen");
872 1.1 mrg (void) close(s);
873 1.1 mrg return;
874 1.1 mrg }
875 1.1 mrg
876 1.1 mrg /*
877 1.1 mrg * Send output to requestor
878 1.1 mrg */
879 1.1 mrg fputs("Name\t\tScore\tDucked\tAbsorb\tFaced\tShot\tRobbed\tMissed\tSlimeK\n", fp);
880 1.1 mrg for (ip = Scores; ip != NULL; ip = ip->i_next) {
881 1.1 mrg fprintf(fp, "%s\t", ip->i_name);
882 1.1 mrg if (strlen(ip->i_name) < 8)
883 1.1 mrg putc('\t', fp);
884 1.1 mrg fprintf(fp, "%.2f\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
885 1.1 mrg ip->i_score, ip->i_ducked, ip->i_absorbed,
886 1.1 mrg ip->i_faced, ip->i_shot, ip->i_robbed,
887 1.1 mrg ip->i_missed, ip->i_slime);
888 1.1 mrg }
889 1.1 mrg fputs("\n\nName\t\tEnemy\tFriend\tDeaths\tStill\tSaved\n", fp);
890 1.1 mrg for (ip = Scores; ip != NULL; ip = ip->i_next) {
891 1.1 mrg if (ip->i_team == ' ') {
892 1.1 mrg fprintf(fp, "%s\t", ip->i_name);
893 1.1 mrg if (strlen(ip->i_name) < 8)
894 1.1 mrg putc('\t', fp);
895 1.1 mrg }
896 1.1 mrg else {
897 1.1 mrg fprintf(fp, "%s[%c]\t", ip->i_name, ip->i_team);
898 1.1 mrg if (strlen(ip->i_name) + 3 < 8)
899 1.1 mrg putc('\t', fp);
900 1.1 mrg }
901 1.1 mrg fprintf(fp, "%d\t%d\t%d\t%d\t%d\n",
902 1.1 mrg ip->i_gkills, ip->i_bkills, ip->i_deaths,
903 1.1 mrg ip->i_stillb, ip->i_saved);
904 1.1 mrg }
905 1.1 mrg
906 1.1 mrg (void) fclose(fp);
907 1.1 mrg }
908 1.1 mrg
909 1.1 mrg /*
910 1.1 mrg * clear_scores:
911 1.1 mrg * Clear out the scores so the next session start clean
912 1.1 mrg */
913 1.2 lukem static void
914 1.16 dholland clear_scores(void)
915 1.1 mrg {
916 1.17 dholland IDENT *ip, *nextip;
917 1.1 mrg
918 1.1 mrg for (ip = Scores; ip != NULL; ip = nextip) {
919 1.1 mrg nextip = ip->i_next;
920 1.15 dholland (void) free(ip);
921 1.1 mrg }
922 1.1 mrg Scores = NULL;
923 1.1 mrg }
924