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