Home | History | Annotate | Line # | Download | only in sail
pl_5.c revision 1.1
      1 /*
      2  * Copyright (c) 1983 Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 static char sccsid[] = "@(#)pl_5.c	5.4 (Berkeley) 6/1/90";
     36 #endif /* not lint */
     37 
     38 #include "player.h"
     39 
     40 #define turnfirst(x) (*x == 'r' || *x == 'l')
     41 
     42 acceptmove()
     43 {
     44 	int ta;
     45 	int ma;
     46 	char af;
     47 	int moved = 0;
     48 	int vma, dir;
     49 	char prompt[60];
     50 	char buf[60], last = '\0';
     51 	register char *p;
     52 
     53 	if (!mc->crew3 || snagged(ms) || !windspeed) {
     54 		Signal("Unable to move", (struct ship *)0);
     55 		return;
     56 	}
     57 
     58 	ta = maxturns(ms, &af);
     59 	ma = maxmove(ms, mf->dir, 0);
     60 	(void) sprintf(prompt, "move (%d,%c%d): ", ma, af ? '\'' : ' ', ta);
     61 	sgetstr(prompt, buf, sizeof buf);
     62 	dir = mf->dir;
     63 	vma = ma;
     64 	for (p = buf; *p; p++)
     65 		switch (*p) {
     66 		case 'l':
     67 			dir -= 2;
     68 		case 'r':
     69 			if (++dir == 0)
     70 				dir = 8;
     71 			else if (dir == 9)
     72 				dir = 1;
     73 			if (last == 't') {
     74 				Signal("Ship can't turn that fast.",
     75 					(struct ship *)0);
     76 				*p-- = '\0';
     77 			}
     78 			last = 't';
     79 			ma--;
     80 			ta--;
     81 			vma = min(ma, maxmove(ms, dir, 0));
     82 			if (ta < 0 && moved || vma < 0 && moved)
     83 				*p-- = '\0';
     84 			break;
     85 		case 'b':
     86 			ma--;
     87 			vma--;
     88 			last = 'b';
     89 			if (ta < 0 && moved || vma < 0 && moved)
     90 				*p-- = '\0';
     91 			break;
     92 		case '0':
     93 		case 'd':
     94 			*p-- = '\0';
     95 			break;
     96 		case '\n':
     97 			*p-- = '\0';
     98 			break;
     99 		case '1': case '2': case '3': case '4':
    100 		case '5': case '6': case '7':
    101 			if (last == '0') {
    102 				Signal("Can't move that fast.",
    103 					(struct ship *)0);
    104 				*p-- = '\0';
    105 			}
    106 			last = '0';
    107 			moved = 1;
    108 			ma -= *p - '0';
    109 			vma -= *p - '0';
    110 			if (ta < 0 && moved || vma < 0 && moved)
    111 				*p-- = '\0';
    112 			break;
    113 		default:
    114 			if (!isspace(*p)) {
    115 				Signal("Input error.", (struct ship *)0);
    116 				*p-- = '\0';
    117 			}
    118 		}
    119 	if (ta < 0 && moved || vma < 0 && moved
    120 	    || af && turnfirst(buf) && moved) {
    121 		Signal("Movement error.", (struct ship *)0);
    122 		if (ta < 0 && moved) {
    123 			if (mf->FS == 1) {
    124 				Write(W_FS, ms, 0, 0, 0, 0, 0);
    125 				Signal("No hands to set full sails.",
    126 					(struct ship *)0);
    127 			}
    128 		} else if (ma >= 0)
    129 			buf[1] = '\0';
    130 	}
    131 	if (af && !moved) {
    132 		if (mf->FS == 1) {
    133 			Write(W_FS, ms, 0, 0, 0, 0, 0);
    134 			Signal("No hands to set full sails.",
    135 				(struct ship *)0);
    136 		}
    137 	}
    138 	if (*buf)
    139 		(void) strcpy(movebuf, buf);
    140 	else
    141 		(void) strcpy(movebuf, "d");
    142 	Write(W_MOVE, ms, 1, (int)movebuf, 0, 0, 0);
    143 	Signal("Helm: %s.", (struct ship *)0, movebuf);
    144 }
    145 
    146 acceptboard()
    147 {
    148 	register struct ship *sp;
    149 	register int n;
    150 	int crew[3];
    151 	int men = 0;
    152 	char c;
    153 
    154 	crew[0] = mc->crew1;
    155 	crew[1] = mc->crew2;
    156 	crew[2] = mc->crew3;
    157 	for (n = 0; n < NBP; n++) {
    158 		if (mf->OBP[n].turnsent)
    159 			    men += mf->OBP[n].mensent;
    160 	}
    161 	for (n = 0; n < NBP; n++) {
    162 		if (mf->DBP[n].turnsent)
    163 			    men += mf->DBP[n].mensent;
    164 	}
    165 	if (men) {
    166 		crew[0] = men/100 ? 0 : crew[0] != 0;
    167 		crew[1] = (men%100)/10 ? 0 : crew[1] != 0;
    168 		crew[2] = men%10 ? 0 : crew[2] != 0;
    169 	} else {
    170 		crew[0] = crew[0] != 0;
    171 		crew[1] = crew[1] != 0;
    172 		crew[2] = crew[2] != 0;
    173 	}
    174 	foreachship(sp) {
    175 		if (sp == ms || sp->file->dir == 0 || range(ms, sp) > 1)
    176 			continue;
    177 		if (ms->nationality == capship(sp)->nationality)
    178 			continue;
    179 		if (meleeing(ms, sp) && crew[2]) {
    180 			c = sgetch("How many more to board the %s (%c%c)? ",
    181 				sp, 1);
    182 			parties(crew, sp, 0, c);
    183 		} else if ((fouled2(ms, sp) || grappled2(ms, sp)) && crew[2]) {
    184 			c = sgetch("Crew sections to board the %s (%c%c) (3 max) ?", sp, 1);
    185 			parties(crew, sp, 0, c);
    186 		}
    187 	}
    188 	if (crew[2]) {
    189 		c = sgetch("How many sections to repel boarders? ",
    190 			(struct ship *)0, 1);
    191 		parties(crew, ms, 1, c);
    192 	}
    193 	blockalarm();
    194 	draw_slot();
    195 	unblockalarm();
    196 }
    197 
    198 parties(crew, to, isdefense, buf)
    199 register struct ship *to;
    200 int crew[3];
    201 char isdefense;
    202 char buf;
    203 {
    204 	register int k, j, men;
    205 	struct BP *ptr;
    206 	int temp[3];
    207 
    208 	for (k = 0; k < 3; k++)
    209 		temp[k] = crew[k];
    210 	if (isdigit(buf)) {
    211 		ptr = isdefense ? to->file->DBP : to->file->OBP;
    212 		for (j = 0; j < NBP && ptr[j].turnsent; j++)
    213 			;
    214 		if (!ptr[j].turnsent && buf > '0') {
    215 			men = 0;
    216 			for (k = 0; k < 3 && buf > '0'; k++) {
    217 				men += crew[k]
    218 					* (k == 0 ? 100 : (k == 1 ? 10 : 1));
    219 				crew[k] = 0;
    220 				if (men)
    221 					buf--;
    222 			}
    223 			if (buf > '0')
    224 				Signal("Sending all crew sections.",
    225 					(struct ship *)0);
    226 			Write(isdefense ? W_DBP : W_OBP, ms, 0,
    227 				j, turn, to->file->index, men);
    228 			if (isdefense) {
    229 				(void) wmove(slot_w, 2, 0);
    230 				for (k=0; k < NBP; k++)
    231 					if (temp[k] && !crew[k])
    232 						(void) waddch(slot_w, k + '1');
    233 					else
    234 						(void) wmove(slot_w, 2, 1 + k);
    235 				(void) mvwaddstr(slot_w, 3, 0, "DBP");
    236 				makesignal(ms, "repelling boarders",
    237 					(struct ship *)0);
    238 			} else {
    239 				(void) wmove(slot_w, 0, 0);
    240 				for (k=0; k < NBP; k++)
    241 					if (temp[k] && !crew[k])
    242 						(void) waddch(slot_w, k + '1');
    243 					else
    244 						(void) wmove(slot_w, 0, 1 + k);
    245 				(void) mvwaddstr(slot_w, 1, 0, "OBP");
    246 				makesignal(ms, "boarding the %s (%c%c)", to);
    247 			}
    248 			blockalarm();
    249 			(void) wrefresh(slot_w);
    250 			unblockalarm();
    251 		} else
    252 			Signal("Sending no crew sections.", (struct ship *)0);
    253 	}
    254 }
    255