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