sync.c revision 1.31 1 1.31 dholland /* $NetBSD: sync.c,v 1.31 2009/03/14 22:54:05 dholland Exp $ */
2 1.3 cgd
3 1.1 cgd /*
4 1.3 cgd * Copyright (c) 1983, 1993
5 1.3 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.21 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.6 christos #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.3 cgd #if 0
35 1.5 tls static char sccsid[] = "@(#)sync.c 8.2 (Berkeley) 4/28/95";
36 1.3 cgd #else
37 1.31 dholland __RCSID("$NetBSD: sync.c,v 1.31 2009/03/14 22:54:05 dholland Exp $");
38 1.3 cgd #endif
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.22 jsm #include <sys/stat.h>
42 1.22 jsm
43 1.6 christos #include <fcntl.h>
44 1.5 tls #include <errno.h>
45 1.18 jwise #include <signal.h>
46 1.6 christos #include <stdarg.h>
47 1.17 jwise #include <stdio.h>
48 1.6 christos #include <stdlib.h>
49 1.18 jwise #include <string.h>
50 1.17 jwise #include <time.h>
51 1.6 christos #include <unistd.h>
52 1.5 tls #include "extern.h"
53 1.14 jsm #include "pathnames.h"
54 1.1 cgd
55 1.1 cgd #define BUFSIZE 4096
56 1.1 cgd
57 1.30 dholland /* Message types */
58 1.30 dholland #define W_CAPTAIN 1
59 1.30 dholland #define W_CAPTURED 2
60 1.30 dholland #define W_CLASS 3
61 1.30 dholland #define W_CREW 4
62 1.30 dholland #define W_DBP 5
63 1.30 dholland #define W_DRIFT 6
64 1.30 dholland #define W_EXPLODE 7
65 1.31 dholland /* W_FILE 8 not used */
66 1.30 dholland #define W_FOUL 9
67 1.30 dholland #define W_GUNL 10
68 1.30 dholland #define W_GUNR 11
69 1.30 dholland #define W_HULL 12
70 1.30 dholland #define W_MOVE 13
71 1.30 dholland #define W_OBP 14
72 1.30 dholland #define W_PCREW 15
73 1.30 dholland #define W_UNFOUL 16
74 1.30 dholland #define W_POINTS 17
75 1.30 dholland #define W_QUAL 18
76 1.30 dholland #define W_UNGRAP 19
77 1.30 dholland #define W_RIGG 20
78 1.30 dholland #define W_COL 21
79 1.30 dholland #define W_DIR 22
80 1.30 dholland #define W_ROW 23
81 1.30 dholland #define W_SIGNAL 24
82 1.30 dholland #define W_SINK 25
83 1.30 dholland #define W_STRUCK 26
84 1.30 dholland #define W_TA 27
85 1.30 dholland #define W_ALIVE 28
86 1.30 dholland #define W_TURN 29
87 1.30 dholland #define W_WIND 30
88 1.30 dholland #define W_FS 31
89 1.30 dholland #define W_GRAP 32
90 1.30 dholland #define W_RIG1 33
91 1.30 dholland #define W_RIG2 34
92 1.30 dholland #define W_RIG3 35
93 1.30 dholland #define W_RIG4 36
94 1.30 dholland #define W_BEGIN 37
95 1.30 dholland #define W_END 38
96 1.30 dholland #define W_DDEAD 39
97 1.30 dholland
98 1.30 dholland
99 1.30 dholland static void recv_captain(struct ship *ship, const char *astr);
100 1.30 dholland static void recv_captured(struct ship *ship, long a);
101 1.30 dholland static void recv_class(struct ship *ship, long a);
102 1.30 dholland static void recv_crew(struct ship *ship, long a, long b, long c);
103 1.30 dholland static void recv_dbp(struct ship *ship, long a, long b, long c, long d);
104 1.30 dholland static void recv_drift(struct ship *ship, long a);
105 1.30 dholland static void recv_explode(struct ship *ship, long a);
106 1.30 dholland static void recv_foul(struct ship *ship, long a);
107 1.30 dholland static void recv_gunl(struct ship *ship, long a, long b);
108 1.30 dholland static void recv_gunr(struct ship *ship, long a, long b);
109 1.30 dholland static void recv_hull(struct ship *ship, long a);
110 1.30 dholland static void recv_move(struct ship *ship, const char *astr);
111 1.30 dholland static void recv_obp(struct ship *ship, long a, long b, long c, long d);
112 1.30 dholland static void recv_pcrew(struct ship *ship, long a);
113 1.30 dholland static void recv_unfoul(struct ship *ship, long a, long b);
114 1.30 dholland static void recv_points(struct ship *ship, long a);
115 1.30 dholland static void recv_qual(struct ship *ship, long a);
116 1.30 dholland static void recv_ungrap(struct ship *ship, long a, long b);
117 1.30 dholland static void recv_rigg(struct ship *ship, long a, long b, long c, long d);
118 1.30 dholland static void recv_col(struct ship *ship, long a);
119 1.30 dholland static void recv_dir(struct ship *ship, long a);
120 1.30 dholland static void recv_row(struct ship *ship, long a);
121 1.30 dholland static void recv_signal(struct ship *ship, const char *astr);
122 1.30 dholland static void recv_sink(struct ship *ship, long a);
123 1.30 dholland static void recv_struck(struct ship *ship, long a);
124 1.30 dholland static void recv_ta(struct ship *ship, long a);
125 1.30 dholland static void recv_alive(void);
126 1.30 dholland static void recv_turn(long a);
127 1.30 dholland static void recv_wind(long a, long b);
128 1.30 dholland static void recv_fs(struct ship *ship, long a);
129 1.30 dholland static void recv_grap(struct ship *ship, long a);
130 1.30 dholland static void recv_rig1(struct ship *ship, long a);
131 1.30 dholland static void recv_rig2(struct ship *ship, long a);
132 1.30 dholland static void recv_rig3(struct ship *ship, long a);
133 1.30 dholland static void recv_rig4(struct ship *ship, long a);
134 1.30 dholland static void recv_begin(struct ship *ship);
135 1.30 dholland static void recv_end(struct ship *ship);
136 1.30 dholland static void recv_ddead(void);
137 1.30 dholland
138 1.30 dholland static void Write(int, struct ship *, long, long, long, long);
139 1.30 dholland static void Writestr(int, struct ship *, const char *);
140 1.30 dholland
141 1.28 dholland static int sync_update(int, struct ship *, const char *,
142 1.28 dholland long, long, long, long);
143 1.16 jwise
144 1.14 jsm static const char SF[] = _PATH_SYNC;
145 1.14 jsm static const char LF[] = _PATH_LOCK;
146 1.1 cgd static char sync_buf[BUFSIZE];
147 1.1 cgd static char *sync_bp = sync_buf;
148 1.14 jsm static char sync_lock[sizeof SF];
149 1.14 jsm static char sync_file[sizeof LF];
150 1.1 cgd static long sync_seek;
151 1.1 cgd static FILE *sync_fp;
152 1.1 cgd
153 1.6 christos void
154 1.15 jwise fmtship(char *buf, size_t len, const char *fmt, struct ship *ship)
155 1.6 christos {
156 1.6 christos while (*fmt) {
157 1.6 christos if (len-- == 0) {
158 1.6 christos *buf = '\0';
159 1.6 christos return;
160 1.6 christos }
161 1.7 christos if (*fmt == '$' && fmt[1] == '$') {
162 1.6 christos size_t l = snprintf(buf, len, "%s (%c%c)",
163 1.6 christos ship->shipname, colours(ship), sterncolour(ship));
164 1.6 christos buf += l;
165 1.6 christos len -= l - 1;
166 1.6 christos fmt += 2;
167 1.6 christos }
168 1.6 christos else
169 1.6 christos *buf++ = *fmt++;
170 1.6 christos }
171 1.6 christos
172 1.6 christos if (len > 0)
173 1.6 christos *buf = '\0';
174 1.6 christos }
175 1.6 christos
176 1.6 christos
177 1.1 cgd /*VARARGS3*/
178 1.6 christos void
179 1.6 christos makesignal(struct ship *from, const char *fmt, struct ship *ship, ...)
180 1.6 christos {
181 1.6 christos char message[BUFSIZ];
182 1.6 christos char format[BUFSIZ];
183 1.6 christos va_list ap;
184 1.15 jwise
185 1.6 christos va_start(ap, ship);
186 1.6 christos fmtship(format, sizeof(format), fmt, ship);
187 1.26 dholland vsnprintf(message, sizeof(message), format, ap);
188 1.6 christos va_end(ap);
189 1.30 dholland send_signal(from, message);
190 1.1 cgd }
191 1.1 cgd
192 1.15 jwise /*VARARGS2*/
193 1.7 christos void
194 1.7 christos makemsg(struct ship *from, const char *fmt, ...)
195 1.7 christos {
196 1.7 christos char message[BUFSIZ];
197 1.7 christos va_list ap;
198 1.15 jwise
199 1.7 christos va_start(ap, fmt);
200 1.26 dholland vsnprintf(message, sizeof(message), fmt, ap);
201 1.7 christos va_end(ap);
202 1.30 dholland send_signal(from, message);
203 1.7 christos }
204 1.11 hubertf
205 1.6 christos int
206 1.25 dholland sync_exists(int gamenum)
207 1.1 cgd {
208 1.1 cgd char buf[sizeof sync_file];
209 1.1 cgd struct stat s;
210 1.1 cgd time_t t;
211 1.1 cgd
212 1.26 dholland snprintf(buf, sizeof(buf), SF, gamenum);
213 1.15 jwise time(&t);
214 1.14 jsm setegid(egid);
215 1.14 jsm if (stat(buf, &s) < 0) {
216 1.14 jsm setegid(gid);
217 1.1 cgd return 0;
218 1.14 jsm }
219 1.1 cgd if (s.st_mtime < t - 60*60*2) { /* 2 hours */
220 1.15 jwise unlink(buf);
221 1.26 dholland snprintf(buf, sizeof(buf), LF, gamenum);
222 1.15 jwise unlink(buf);
223 1.14 jsm setegid(gid);
224 1.1 cgd return 0;
225 1.14 jsm } else {
226 1.14 jsm setegid(gid);
227 1.1 cgd return 1;
228 1.14 jsm }
229 1.1 cgd }
230 1.1 cgd
231 1.6 christos int
232 1.15 jwise sync_open(void)
233 1.1 cgd {
234 1.14 jsm struct stat tmp;
235 1.1 cgd if (sync_fp != NULL)
236 1.15 jwise fclose(sync_fp);
237 1.26 dholland snprintf(sync_lock, sizeof(sync_lock), LF, game);
238 1.26 dholland snprintf(sync_file, sizeof(sync_file), SF, game);
239 1.14 jsm setegid(egid);
240 1.14 jsm if (stat(sync_file, &tmp) < 0) {
241 1.14 jsm mode_t omask = umask(002);
242 1.1 cgd sync_fp = fopen(sync_file, "w+");
243 1.15 jwise umask(omask);
244 1.1 cgd } else
245 1.1 cgd sync_fp = fopen(sync_file, "r+");
246 1.14 jsm setegid(gid);
247 1.1 cgd if (sync_fp == NULL)
248 1.1 cgd return -1;
249 1.1 cgd sync_seek = 0;
250 1.1 cgd return 0;
251 1.1 cgd }
252 1.1 cgd
253 1.6 christos void
254 1.25 dholland sync_close(int doremove)
255 1.1 cgd {
256 1.1 cgd if (sync_fp != 0)
257 1.15 jwise fclose(sync_fp);
258 1.25 dholland if (doremove) {
259 1.14 jsm setegid(egid);
260 1.15 jwise unlink(sync_file);
261 1.14 jsm setegid(gid);
262 1.14 jsm }
263 1.1 cgd }
264 1.1 cgd
265 1.30 dholland static void
266 1.15 jwise Write(int type, struct ship *ship, long a, long b, long c, long d)
267 1.1 cgd {
268 1.26 dholland size_t max = sizeof(sync_buf) - (sync_bp - sync_buf);
269 1.30 dholland int shipindex = (ship == NULL) ? 0 : ship->file->index;
270 1.6 christos
271 1.26 dholland snprintf(sync_bp, max, "%d %d 0 %ld %ld %ld %ld\n",
272 1.30 dholland type, shipindex, a, b, c, d);
273 1.11 hubertf while (*sync_bp++)
274 1.11 hubertf ;
275 1.11 hubertf sync_bp--;
276 1.11 hubertf if (sync_bp >= &sync_buf[sizeof sync_buf])
277 1.11 hubertf abort();
278 1.15 jwise sync_update(type, ship, NULL, a, b, c, d);
279 1.11 hubertf }
280 1.11 hubertf
281 1.30 dholland static void
282 1.15 jwise Writestr(int type, struct ship *ship, const char *a)
283 1.11 hubertf {
284 1.26 dholland size_t max = sizeof(sync_buf) - (sync_bp - sync_buf);
285 1.30 dholland int shipindex = (ship == NULL) ? 0 : ship->file->index;
286 1.26 dholland
287 1.30 dholland snprintf(sync_bp, max, "%d %d 1 %s\n", type, shipindex, a);
288 1.1 cgd while (*sync_bp++)
289 1.1 cgd ;
290 1.1 cgd sync_bp--;
291 1.1 cgd if (sync_bp >= &sync_buf[sizeof sync_buf])
292 1.1 cgd abort();
293 1.15 jwise sync_update(type, ship, a, 0, 0, 0, 0);
294 1.1 cgd }
295 1.1 cgd
296 1.6 christos int
297 1.15 jwise Sync(void)
298 1.1 cgd {
299 1.1 cgd sig_t sighup, sigint;
300 1.6 christos int n;
301 1.4 cgd int type, shipnum, isstr;
302 1.11 hubertf char *astr;
303 1.4 cgd long a, b, c, d;
304 1.1 cgd char buf[80];
305 1.1 cgd char erred = 0;
306 1.1 cgd
307 1.1 cgd sighup = signal(SIGHUP, SIG_IGN);
308 1.1 cgd sigint = signal(SIGINT, SIG_IGN);
309 1.1 cgd for (n = TIMEOUT; --n >= 0;) {
310 1.1 cgd #ifdef LOCK_EX
311 1.1 cgd if (flock(fileno(sync_fp), LOCK_EX|LOCK_NB) >= 0)
312 1.1 cgd break;
313 1.1 cgd if (errno != EWOULDBLOCK)
314 1.1 cgd return -1;
315 1.1 cgd #else
316 1.14 jsm setegid(egid);
317 1.14 jsm if (link(sync_file, sync_lock) >= 0) {
318 1.14 jsm setegid(gid);
319 1.1 cgd break;
320 1.14 jsm }
321 1.14 jsm setegid(gid);
322 1.1 cgd if (errno != EEXIST)
323 1.1 cgd return -1;
324 1.1 cgd #endif
325 1.1 cgd sleep(1);
326 1.1 cgd }
327 1.1 cgd if (n <= 0)
328 1.1 cgd return -1;
329 1.15 jwise fseek(sync_fp, sync_seek, SEEK_SET);
330 1.1 cgd for (;;) {
331 1.1 cgd switch (fscanf(sync_fp, "%d%d%d", &type, &shipnum, &isstr)) {
332 1.1 cgd case 3:
333 1.1 cgd break;
334 1.1 cgd case EOF:
335 1.1 cgd goto out;
336 1.1 cgd default:
337 1.1 cgd goto bad;
338 1.1 cgd }
339 1.1 cgd if (shipnum < 0 || shipnum >= cc->vessels)
340 1.1 cgd goto bad;
341 1.1 cgd if (isstr != 0 && isstr != 1)
342 1.1 cgd goto bad;
343 1.1 cgd if (isstr) {
344 1.25 dholland int ch;
345 1.6 christos char *p;
346 1.24 mrg
347 1.1 cgd for (p = buf;;) {
348 1.25 dholland ch = getc(sync_fp);
349 1.27 dholland *p++ = ch;
350 1.25 dholland switch (ch) {
351 1.1 cgd case '\n':
352 1.1 cgd p--;
353 1.1 cgd case EOF:
354 1.1 cgd break;
355 1.1 cgd default:
356 1.1 cgd if (p >= buf + sizeof buf)
357 1.1 cgd p--;
358 1.1 cgd continue;
359 1.1 cgd }
360 1.1 cgd break;
361 1.1 cgd }
362 1.1 cgd *p = 0;
363 1.1 cgd for (p = buf; *p == ' '; p++)
364 1.1 cgd ;
365 1.11 hubertf astr = p;
366 1.11 hubertf a = b = c = d = 0;
367 1.11 hubertf } else {
368 1.28 dholland if (fscanf(sync_fp, "%ld%ld%ld%ld", &a, &b, &c, &d)
369 1.28 dholland != 4)
370 1.1 cgd goto bad;
371 1.11 hubertf astr = NULL;
372 1.11 hubertf }
373 1.11 hubertf if (sync_update(type, SHIP(shipnum), astr, a, b, c, d) < 0)
374 1.1 cgd goto bad;
375 1.1 cgd }
376 1.1 cgd bad:
377 1.1 cgd erred++;
378 1.1 cgd out:
379 1.1 cgd if (!erred && sync_bp != sync_buf) {
380 1.15 jwise fseek(sync_fp, 0L, SEEK_END);
381 1.15 jwise fwrite(sync_buf, sizeof *sync_buf, sync_bp - sync_buf,
382 1.1 cgd sync_fp);
383 1.15 jwise fflush(sync_fp);
384 1.1 cgd sync_bp = sync_buf;
385 1.1 cgd }
386 1.1 cgd sync_seek = ftell(sync_fp);
387 1.1 cgd #ifdef LOCK_EX
388 1.15 jwise flock(fileno(sync_fp), LOCK_UN);
389 1.1 cgd #else
390 1.14 jsm setegid(egid);
391 1.15 jwise unlink(sync_lock);
392 1.14 jsm setegid(gid);
393 1.1 cgd #endif
394 1.15 jwise signal(SIGHUP, sighup);
395 1.15 jwise signal(SIGINT, sigint);
396 1.1 cgd return erred ? -1 : 0;
397 1.1 cgd }
398 1.1 cgd
399 1.16 jwise static int
400 1.28 dholland sync_update(int type, struct ship *ship, const char *astr,
401 1.28 dholland long a, long b, long c, long d)
402 1.1 cgd {
403 1.1 cgd switch (type) {
404 1.30 dholland case W_CAPTAIN: recv_captain(ship, astr); break;
405 1.30 dholland case W_CAPTURED: recv_captured(ship, a); break;
406 1.30 dholland case W_CLASS: recv_class(ship, a); break;
407 1.30 dholland case W_CREW: recv_crew(ship, a, b, c); break;
408 1.30 dholland case W_DBP: recv_dbp(ship, a, b, c, d); break;
409 1.30 dholland case W_DRIFT: recv_drift(ship, a); break;
410 1.30 dholland case W_EXPLODE: recv_explode(ship, a); break;
411 1.30 dholland case W_FOUL: recv_foul(ship, a); break;
412 1.30 dholland case W_GUNL: recv_gunl(ship, a, b); break;
413 1.30 dholland case W_GUNR: recv_gunr(ship, a, b); break;
414 1.30 dholland case W_HULL: recv_hull(ship, a); break;
415 1.30 dholland case W_MOVE: recv_move(ship, astr); break;
416 1.30 dholland case W_OBP: recv_obp(ship, a, b, c, d); break;
417 1.30 dholland case W_PCREW: recv_pcrew(ship, a); break;
418 1.30 dholland case W_UNFOUL: recv_unfoul(ship, a, b); break;
419 1.30 dholland case W_POINTS: recv_points(ship, a); break;
420 1.30 dholland case W_QUAL: recv_qual(ship, a); break;
421 1.30 dholland case W_UNGRAP: recv_ungrap(ship, a, b); break;
422 1.30 dholland case W_RIGG: recv_rigg(ship, a, b, c, d); break;
423 1.30 dholland case W_COL: recv_col(ship, a); break;
424 1.30 dholland case W_DIR: recv_dir(ship, a); break;
425 1.30 dholland case W_ROW: recv_row(ship, a); break;
426 1.30 dholland case W_SIGNAL: recv_signal(ship, astr); break;
427 1.30 dholland case W_SINK: recv_sink(ship, a); break;
428 1.30 dholland case W_STRUCK: recv_struck(ship, a); break;
429 1.30 dholland case W_TA: recv_ta(ship, a); break;
430 1.30 dholland case W_ALIVE: recv_alive(); break;
431 1.30 dholland case W_TURN: recv_turn(a); break;
432 1.30 dholland case W_WIND: recv_wind(a, b); break;
433 1.30 dholland case W_FS: recv_fs(ship, a); break;
434 1.30 dholland case W_GRAP: recv_grap(ship, a); break;
435 1.30 dholland case W_RIG1: recv_rig1(ship, a); break;
436 1.30 dholland case W_RIG2: recv_rig2(ship, a); break;
437 1.30 dholland case W_RIG3: recv_rig3(ship, a); break;
438 1.30 dholland case W_RIG4: recv_rig4(ship, a); break;
439 1.30 dholland case W_BEGIN: recv_begin(ship); break;
440 1.30 dholland case W_END: recv_end(ship); break;
441 1.30 dholland case W_DDEAD: recv_ddead(); break;
442 1.1 cgd default:
443 1.1 cgd fprintf(stderr, "sync_update: unknown type %d\r\n", type);
444 1.1 cgd return -1;
445 1.1 cgd }
446 1.1 cgd return 0;
447 1.1 cgd }
448 1.30 dholland
449 1.30 dholland /*
450 1.30 dholland * Messages to send
451 1.30 dholland */
452 1.30 dholland
453 1.30 dholland void
454 1.30 dholland send_captain(struct ship *ship, const char *astr)
455 1.30 dholland {
456 1.30 dholland Writestr(W_CAPTAIN, ship, astr);
457 1.30 dholland }
458 1.30 dholland
459 1.30 dholland void
460 1.30 dholland send_captured(struct ship *ship, long a)
461 1.30 dholland {
462 1.30 dholland Write(W_CAPTURED, ship, a, 0, 0, 0);
463 1.30 dholland }
464 1.30 dholland
465 1.30 dholland void
466 1.30 dholland send_class(struct ship *ship, long a)
467 1.30 dholland {
468 1.30 dholland Write(W_CLASS, ship, a, 0, 0, 0);
469 1.30 dholland }
470 1.30 dholland
471 1.30 dholland void
472 1.30 dholland send_crew(struct ship *ship, long a, long b, long c)
473 1.30 dholland {
474 1.30 dholland Write(W_CREW, ship, a, b, c, 0);
475 1.30 dholland }
476 1.30 dholland
477 1.30 dholland void
478 1.30 dholland send_dbp(struct ship *ship, long a, long b, long c, long d)
479 1.30 dholland {
480 1.30 dholland Write(W_DBP, ship, a, b, c, d);
481 1.30 dholland }
482 1.30 dholland
483 1.30 dholland void
484 1.30 dholland send_drift(struct ship *ship, long a)
485 1.30 dholland {
486 1.30 dholland Write(W_DRIFT, ship, a, 0, 0, 0);
487 1.30 dholland }
488 1.30 dholland
489 1.30 dholland void
490 1.30 dholland send_explode(struct ship *ship, long a)
491 1.30 dholland {
492 1.30 dholland Write(W_EXPLODE, ship, a, 0, 0, 0);
493 1.30 dholland }
494 1.30 dholland
495 1.30 dholland void
496 1.30 dholland send_foul(struct ship *ship, long a)
497 1.30 dholland {
498 1.30 dholland Write(W_FOUL, ship, a, 0, 0, 0);
499 1.30 dholland }
500 1.30 dholland
501 1.30 dholland void
502 1.30 dholland send_gunl(struct ship *ship, long a, long b)
503 1.30 dholland {
504 1.30 dholland Write(W_GUNL, ship, a, b, 0, 0);
505 1.30 dholland }
506 1.30 dholland
507 1.30 dholland void
508 1.30 dholland send_gunr(struct ship *ship, long a, long b)
509 1.30 dholland {
510 1.30 dholland Write(W_GUNR, ship, a, b, 0, 0);
511 1.30 dholland }
512 1.30 dholland
513 1.30 dholland void
514 1.30 dholland send_hull(struct ship *ship, long a)
515 1.30 dholland {
516 1.30 dholland Write(W_HULL, ship, a, 0, 0, 0);
517 1.30 dholland }
518 1.30 dholland
519 1.30 dholland void
520 1.30 dholland send_move(struct ship *ship, const char *astr)
521 1.30 dholland {
522 1.30 dholland Writestr(W_MOVE, ship, astr);
523 1.30 dholland }
524 1.30 dholland
525 1.30 dholland void
526 1.30 dholland send_obp(struct ship *ship, long a, long b, long c, long d)
527 1.30 dholland {
528 1.30 dholland Write(W_OBP, ship, a, b, c, d);
529 1.30 dholland }
530 1.30 dholland
531 1.30 dholland void
532 1.30 dholland send_pcrew(struct ship *ship, long a)
533 1.30 dholland {
534 1.30 dholland Write(W_PCREW, ship, a, 0, 0, 0);
535 1.30 dholland }
536 1.30 dholland
537 1.30 dholland void
538 1.30 dholland send_unfoul(struct ship *ship, long a, long b)
539 1.30 dholland {
540 1.30 dholland Write(W_UNFOUL, ship, a, b, 0, 0);
541 1.30 dholland }
542 1.30 dholland
543 1.30 dholland void
544 1.30 dholland send_points(struct ship *ship, long a)
545 1.30 dholland {
546 1.30 dholland Write(W_POINTS, ship, a, 0, 0, 0);
547 1.30 dholland }
548 1.30 dholland
549 1.30 dholland void
550 1.30 dholland send_qual(struct ship *ship, long a)
551 1.30 dholland {
552 1.30 dholland Write(W_QUAL, ship, a, 0, 0, 0);
553 1.30 dholland }
554 1.30 dholland
555 1.30 dholland void
556 1.30 dholland send_ungrap(struct ship *ship, long a, long b)
557 1.30 dholland {
558 1.30 dholland Write(W_UNGRAP, ship, a, b, 0, 0);
559 1.30 dholland }
560 1.30 dholland
561 1.30 dholland void
562 1.30 dholland send_rigg(struct ship *ship, long a, long b, long c, long d)
563 1.30 dholland {
564 1.30 dholland Write(W_RIGG, ship, a, b, c, d);
565 1.30 dholland }
566 1.30 dholland
567 1.30 dholland void
568 1.30 dholland send_col(struct ship *ship, long a)
569 1.30 dholland {
570 1.30 dholland Write(W_COL, ship, a, 0, 0, 0);
571 1.30 dholland }
572 1.30 dholland
573 1.30 dholland void
574 1.30 dholland send_dir(struct ship *ship, long a)
575 1.30 dholland {
576 1.30 dholland Write(W_DIR, ship, a, 0, 0, 0);
577 1.30 dholland }
578 1.30 dholland
579 1.30 dholland void
580 1.30 dholland send_row(struct ship *ship, long a)
581 1.30 dholland {
582 1.30 dholland Write(W_ROW, ship, a, 0, 0, 0);
583 1.30 dholland }
584 1.30 dholland
585 1.30 dholland void
586 1.30 dholland send_signal(struct ship *ship, const char *astr)
587 1.30 dholland {
588 1.30 dholland Writestr(W_SIGNAL, ship, astr);
589 1.30 dholland }
590 1.30 dholland
591 1.30 dholland void
592 1.30 dholland send_sink(struct ship *ship, long a)
593 1.30 dholland {
594 1.30 dholland Write(W_SINK, ship, a, 0, 0, 0);
595 1.30 dholland }
596 1.30 dholland
597 1.30 dholland void
598 1.30 dholland send_struck(struct ship *ship, long a)
599 1.30 dholland {
600 1.30 dholland Write(W_STRUCK, ship, a, 0, 0, 0);
601 1.30 dholland }
602 1.30 dholland
603 1.30 dholland void
604 1.30 dholland send_ta(struct ship *ship, long a)
605 1.30 dholland {
606 1.30 dholland Write(W_TA, ship, a, 0, 0, 0);
607 1.30 dholland }
608 1.30 dholland
609 1.30 dholland void
610 1.30 dholland send_alive(void)
611 1.30 dholland {
612 1.30 dholland Write(W_ALIVE, NULL, 0, 0, 0, 0);
613 1.30 dholland }
614 1.30 dholland
615 1.30 dholland void
616 1.30 dholland send_turn(long a)
617 1.30 dholland {
618 1.30 dholland Write(W_TURN, NULL, a, 0, 0, 0);
619 1.30 dholland }
620 1.30 dholland
621 1.30 dholland void
622 1.30 dholland send_wind(long a, long b)
623 1.30 dholland {
624 1.30 dholland Write(W_WIND, NULL, a, b, 0, 0);
625 1.30 dholland }
626 1.30 dholland
627 1.30 dholland void
628 1.30 dholland send_fs(struct ship *ship, long a)
629 1.30 dholland {
630 1.30 dholland Write(W_FS, ship, a, 0, 0, 0);
631 1.30 dholland }
632 1.30 dholland
633 1.30 dholland void
634 1.30 dholland send_grap(struct ship *ship, long a)
635 1.30 dholland {
636 1.30 dholland Write(W_GRAP, ship, a, 0, 0, 0);
637 1.30 dholland }
638 1.30 dholland
639 1.30 dholland void
640 1.30 dholland send_rig1(struct ship *ship, long a)
641 1.30 dholland {
642 1.30 dholland Write(W_RIG1, ship, a, 0, 0, 0);
643 1.30 dholland }
644 1.30 dholland
645 1.30 dholland void
646 1.30 dholland send_rig2(struct ship *ship, long a)
647 1.30 dholland {
648 1.30 dholland Write(W_RIG2, ship, a, 0, 0, 0);
649 1.30 dholland }
650 1.30 dholland
651 1.30 dholland void
652 1.30 dholland send_rig3(struct ship *ship, long a)
653 1.30 dholland {
654 1.30 dholland Write(W_RIG3, ship, a, 0, 0, 0);
655 1.30 dholland }
656 1.30 dholland
657 1.30 dholland void
658 1.30 dholland send_rig4(struct ship *ship, long a)
659 1.30 dholland {
660 1.30 dholland Write(W_RIG4, ship, a, 0, 0, 0);
661 1.30 dholland }
662 1.30 dholland
663 1.30 dholland void
664 1.30 dholland send_begin(struct ship *ship)
665 1.30 dholland {
666 1.30 dholland Write(W_BEGIN, ship, 0, 0, 0, 0);
667 1.30 dholland }
668 1.30 dholland
669 1.30 dholland void
670 1.30 dholland send_end(struct ship *ship)
671 1.30 dholland {
672 1.30 dholland Write(W_END, ship, 0, 0, 0, 0);
673 1.30 dholland }
674 1.30 dholland
675 1.30 dholland void
676 1.30 dholland send_ddead(void)
677 1.30 dholland {
678 1.30 dholland Write(W_DDEAD, NULL, 0, 0, 0, 0);
679 1.30 dholland }
680 1.30 dholland
681 1.30 dholland
682 1.30 dholland /*
683 1.30 dholland * Actions upon message receipt
684 1.30 dholland */
685 1.30 dholland
686 1.30 dholland static void
687 1.30 dholland recv_captain(struct ship *ship, const char *astr)
688 1.30 dholland {
689 1.30 dholland strlcpy(ship->file->captain, astr, sizeof ship->file->captain);
690 1.30 dholland }
691 1.30 dholland
692 1.30 dholland static void
693 1.30 dholland recv_captured(struct ship *ship, long a)
694 1.30 dholland {
695 1.30 dholland if (a < 0)
696 1.30 dholland ship->file->captured = 0;
697 1.30 dholland else
698 1.30 dholland ship->file->captured = SHIP(a);
699 1.30 dholland }
700 1.30 dholland
701 1.30 dholland static void
702 1.30 dholland recv_class(struct ship *ship, long a)
703 1.30 dholland {
704 1.30 dholland ship->specs->class = a;
705 1.30 dholland }
706 1.30 dholland
707 1.30 dholland static void
708 1.30 dholland recv_crew(struct ship *ship, long a, long b, long c)
709 1.30 dholland {
710 1.30 dholland struct shipspecs *s = ship->specs;
711 1.30 dholland
712 1.30 dholland s->crew1 = a;
713 1.30 dholland s->crew2 = b;
714 1.30 dholland s->crew3 = c;
715 1.30 dholland }
716 1.30 dholland
717 1.30 dholland static void
718 1.30 dholland recv_dbp(struct ship *ship, long a, long b, long c, long d)
719 1.30 dholland {
720 1.30 dholland struct BP *p = &ship->file->DBP[a];
721 1.30 dholland
722 1.30 dholland p->turnsent = b;
723 1.30 dholland p->toship = SHIP(c);
724 1.30 dholland p->mensent = d;
725 1.30 dholland }
726 1.30 dholland
727 1.30 dholland static void
728 1.30 dholland recv_drift(struct ship *ship, long a)
729 1.30 dholland {
730 1.30 dholland ship->file->drift = a;
731 1.30 dholland }
732 1.30 dholland
733 1.30 dholland static void
734 1.30 dholland recv_explode(struct ship *ship, long a)
735 1.30 dholland {
736 1.30 dholland if ((ship->file->explode = a) == 2)
737 1.30 dholland ship->file->dir = 0;
738 1.30 dholland }
739 1.30 dholland
740 1.30 dholland static void
741 1.30 dholland recv_foul(struct ship *ship, long a)
742 1.30 dholland {
743 1.30 dholland struct snag *p = &ship->file->foul[a];
744 1.30 dholland
745 1.30 dholland if (SHIP(a)->file->dir == 0)
746 1.30 dholland return;
747 1.30 dholland if (p->sn_count++ == 0)
748 1.30 dholland p->sn_turn = turn;
749 1.30 dholland ship->file->nfoul++;
750 1.30 dholland }
751 1.30 dholland
752 1.30 dholland static void
753 1.30 dholland recv_gunl(struct ship *ship, long a, long b)
754 1.30 dholland {
755 1.30 dholland struct shipspecs *s = ship->specs;
756 1.30 dholland
757 1.30 dholland s->gunL = a;
758 1.30 dholland s->carL = b;
759 1.30 dholland }
760 1.30 dholland
761 1.30 dholland static void
762 1.30 dholland recv_gunr(struct ship *ship, long a, long b)
763 1.30 dholland {
764 1.30 dholland struct shipspecs *s = ship->specs;
765 1.30 dholland
766 1.30 dholland s->gunR = a;
767 1.30 dholland s->carR = b;
768 1.30 dholland }
769 1.30 dholland
770 1.30 dholland static void
771 1.30 dholland recv_hull(struct ship *ship, long a)
772 1.30 dholland {
773 1.30 dholland ship->specs->hull = a;
774 1.30 dholland }
775 1.30 dholland
776 1.30 dholland static void
777 1.30 dholland recv_move(struct ship *ship, const char *astr)
778 1.30 dholland {
779 1.30 dholland strlcpy(ship->file->movebuf, astr, sizeof ship->file->movebuf);
780 1.30 dholland }
781 1.30 dholland
782 1.30 dholland static void
783 1.30 dholland recv_obp(struct ship *ship, long a, long b, long c, long d)
784 1.30 dholland {
785 1.30 dholland struct BP *p = &ship->file->OBP[a];
786 1.30 dholland
787 1.30 dholland p->turnsent = b;
788 1.30 dholland p->toship = SHIP(c);
789 1.30 dholland p->mensent = d;
790 1.30 dholland }
791 1.30 dholland
792 1.30 dholland static void
793 1.30 dholland recv_pcrew(struct ship *ship, long a)
794 1.30 dholland {
795 1.30 dholland ship->file->pcrew = a;
796 1.30 dholland }
797 1.30 dholland
798 1.30 dholland static void
799 1.30 dholland recv_unfoul(struct ship *ship, long a, long b)
800 1.30 dholland {
801 1.30 dholland struct snag *p = &ship->file->foul[a];
802 1.30 dholland
803 1.30 dholland if (p->sn_count > 0) {
804 1.30 dholland if (b) {
805 1.30 dholland ship->file->nfoul -= p->sn_count;
806 1.30 dholland p->sn_count = 0;
807 1.30 dholland } else {
808 1.30 dholland ship->file->nfoul--;
809 1.30 dholland p->sn_count--;
810 1.30 dholland }
811 1.30 dholland }
812 1.30 dholland }
813 1.30 dholland
814 1.30 dholland static void
815 1.30 dholland recv_points(struct ship *ship, long a)
816 1.30 dholland {
817 1.30 dholland ship->file->points = a;
818 1.30 dholland }
819 1.30 dholland
820 1.30 dholland static void
821 1.30 dholland recv_qual(struct ship *ship, long a)
822 1.30 dholland {
823 1.30 dholland ship->specs->qual = a;
824 1.30 dholland }
825 1.30 dholland
826 1.30 dholland static void
827 1.30 dholland recv_ungrap(struct ship *ship, long a, long b)
828 1.30 dholland {
829 1.30 dholland struct snag *p = &ship->file->grap[a];
830 1.30 dholland
831 1.30 dholland if (p->sn_count > 0) {
832 1.30 dholland if (b) {
833 1.30 dholland ship->file->ngrap -= p->sn_count;
834 1.30 dholland p->sn_count = 0;
835 1.30 dholland } else {
836 1.30 dholland ship->file->ngrap--;
837 1.30 dholland p->sn_count--;
838 1.30 dholland }
839 1.30 dholland }
840 1.30 dholland }
841 1.30 dholland
842 1.30 dholland static void
843 1.30 dholland recv_rigg(struct ship *ship, long a, long b, long c, long d)
844 1.30 dholland {
845 1.30 dholland struct shipspecs *s = ship->specs;
846 1.30 dholland
847 1.30 dholland s->rig1 = a;
848 1.30 dholland s->rig2 = b;
849 1.30 dholland s->rig3 = c;
850 1.30 dholland s->rig4 = d;
851 1.30 dholland }
852 1.30 dholland
853 1.30 dholland static void
854 1.30 dholland recv_col(struct ship *ship, long a)
855 1.30 dholland {
856 1.30 dholland ship->file->col = a;
857 1.30 dholland }
858 1.30 dholland
859 1.30 dholland static void
860 1.30 dholland recv_dir(struct ship *ship, long a)
861 1.30 dholland {
862 1.30 dholland ship->file->dir = a;
863 1.30 dholland }
864 1.30 dholland
865 1.30 dholland static void
866 1.30 dholland recv_row(struct ship *ship, long a)
867 1.30 dholland {
868 1.30 dholland ship->file->row = a;
869 1.30 dholland }
870 1.30 dholland
871 1.30 dholland static void
872 1.30 dholland recv_signal(struct ship *ship, const char *astr)
873 1.30 dholland {
874 1.30 dholland if (mode == MODE_PLAYER) {
875 1.30 dholland if (nobells)
876 1.30 dholland Signal("$$: %s", ship, astr);
877 1.30 dholland else
878 1.30 dholland Signal("\a$$: %s", ship, astr);
879 1.30 dholland }
880 1.30 dholland }
881 1.30 dholland
882 1.30 dholland static void
883 1.30 dholland recv_sink(struct ship *ship, long a)
884 1.30 dholland {
885 1.30 dholland if ((ship->file->sink = a) == 2)
886 1.30 dholland ship->file->dir = 0;
887 1.30 dholland }
888 1.30 dholland
889 1.30 dholland static void
890 1.30 dholland recv_struck(struct ship *ship, long a)
891 1.30 dholland {
892 1.30 dholland ship->file->struck = a;
893 1.30 dholland }
894 1.30 dholland
895 1.30 dholland static void
896 1.30 dholland recv_ta(struct ship *ship, long a)
897 1.30 dholland {
898 1.30 dholland ship->specs->ta = a;
899 1.30 dholland }
900 1.30 dholland
901 1.30 dholland static void
902 1.30 dholland recv_alive(void)
903 1.30 dholland {
904 1.30 dholland alive = 1;
905 1.30 dholland }
906 1.30 dholland
907 1.30 dholland static void
908 1.30 dholland recv_turn(long a)
909 1.30 dholland {
910 1.30 dholland turn = a;
911 1.30 dholland }
912 1.30 dholland
913 1.30 dholland static void
914 1.30 dholland recv_wind(long a, long b)
915 1.30 dholland {
916 1.30 dholland winddir = a;
917 1.30 dholland windspeed = b;
918 1.30 dholland }
919 1.30 dholland
920 1.30 dholland static void
921 1.30 dholland recv_fs(struct ship *ship, long a)
922 1.30 dholland {
923 1.30 dholland ship->file->FS = a;
924 1.30 dholland }
925 1.30 dholland
926 1.30 dholland static void
927 1.30 dholland recv_grap(struct ship *ship, long a)
928 1.30 dholland {
929 1.30 dholland struct snag *p = &ship->file->grap[a];
930 1.30 dholland
931 1.30 dholland if (SHIP(a)->file->dir == 0)
932 1.30 dholland return;
933 1.30 dholland if (p->sn_count++ == 0)
934 1.30 dholland p->sn_turn = turn;
935 1.30 dholland ship->file->ngrap++;
936 1.30 dholland }
937 1.30 dholland
938 1.30 dholland static void
939 1.30 dholland recv_rig1(struct ship *ship, long a)
940 1.30 dholland {
941 1.30 dholland ship->specs->rig1 = a;
942 1.30 dholland }
943 1.30 dholland
944 1.30 dholland static void
945 1.30 dholland recv_rig2(struct ship *ship, long a)
946 1.30 dholland {
947 1.30 dholland ship->specs->rig2 = a;
948 1.30 dholland }
949 1.30 dholland
950 1.30 dholland static void
951 1.30 dholland recv_rig3(struct ship *ship, long a)
952 1.30 dholland {
953 1.30 dholland ship->specs->rig3 = a;
954 1.30 dholland }
955 1.30 dholland
956 1.30 dholland static void
957 1.30 dholland recv_rig4(struct ship *ship, long a)
958 1.30 dholland {
959 1.30 dholland ship->specs->rig4 = a;
960 1.30 dholland }
961 1.30 dholland
962 1.30 dholland static void
963 1.30 dholland recv_begin(struct ship *ship)
964 1.30 dholland {
965 1.30 dholland strcpy(ship->file->captain, "begin");
966 1.30 dholland people++;
967 1.30 dholland }
968 1.30 dholland
969 1.30 dholland static void
970 1.30 dholland recv_end(struct ship *ship)
971 1.30 dholland {
972 1.30 dholland *ship->file->captain = 0;
973 1.30 dholland ship->file->points = 0;
974 1.30 dholland people--;
975 1.30 dholland }
976 1.30 dholland
977 1.30 dholland static void
978 1.30 dholland recv_ddead(void)
979 1.30 dholland {
980 1.30 dholland hasdriver = 0;
981 1.30 dholland }
982