make it more robust
[unix-history] / usr / src / games / sail / dr_5.c
CommitLineData
b5f0675e 1/*
1e008c14
DF
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
b5f0675e
EW
5 */
6
3df86079 7#ifndef lint
1e008c14
DF
8static char sccsid[] = "@(#)dr_5.c 5.1 (Berkeley) %G%";
9#endif not lint
b5f0675e 10
3df86079
CL
11#include "externs.h"
12
13subtract(from, totalfrom, crewfrom, fromcap, pcfrom)
b3a57661
EW
14struct ship *from, *fromcap;
15int pcfrom;
3df86079
CL
16register int totalfrom, crewfrom[3];
17{
18 register int n;
19
b3a57661
EW
20 if (fromcap == from && totalfrom) { /* if not captured */
21 for (n = 0; n < 3; n++) {
22 if (totalfrom > crewfrom[n]) {
3df86079
CL
23 totalfrom -= crewfrom[n];
24 crewfrom[n] = 0;
b3a57661 25 } else {
3df86079
CL
26 crewfrom[n] -= totalfrom;
27 totalfrom = 0;
28 }
3df86079 29 }
b3a57661
EW
30 Write(W_CREW, from, 0, crewfrom[0], crewfrom[1], crewfrom[2], 0);
31 } else if (totalfrom) {
3df86079
CL
32 pcfrom -= totalfrom;
33 pcfrom = pcfrom < 0 ? 0 : pcfrom;
b3a57661 34 Write(W_PCREW, from, 0, pcfrom, 0, 0, 0);
3df86079
CL
35 }
36}
37
b3a57661
EW
38mensent(from, to, crew, captured, pc, isdefense)
39struct ship *from, *to, **captured;
40int crew[3], *pc;
41char isdefense;
3df86079
CL
42{ /* returns # of crew squares sent */
43 int men = 0;
44 register int n;
45 int c1, c2, c3;
b3a57661 46 register struct BP *bp;
3df86079 47
b3a57661
EW
48 *pc = from->file->pcrew;
49 *captured = from->file->captured;
50 crew[0] = from->specs->crew1;
51 crew[1] = from->specs->crew2;
52 crew[2] = from->specs->crew3;
53 bp = isdefense ? from->file->DBP : from->file->OBP;
54 for (n=0; n < NBP; n++, bp++) {
55 if (bp->turnsent && bp->toship == to)
56 men += bp->mensent;
3df86079 57 }
b3a57661 58 if (men) {
3df86079
CL
59 c1 = men/100 ? crew[0] : 0;
60 c2 = (men%100)/10 ? crew[1] : 0;
61 c3 = men/10 ? crew[2] : 0;
b3a57661 62 c3 = *captured == 0 ? crew[2] : *pc;
3df86079
CL
63 } else
64 c1 = c2 = c3 = 0;
65 return(c1 + c2 + c3);
66}