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