Home | History | Annotate | Line # | Download | only in sail
pl_5.c revision 1.8
      1 /*	$NetBSD: pl_5.c,v 1.8 2001/01/01 21:57:38 jwise 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 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)pl_5.c	8.1 (Berkeley) 5/31/93";
     40 #else
     41 __RCSID("$NetBSD: pl_5.c,v 1.8 2001/01/01 21:57:38 jwise Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include "player.h"
     46 
     47 #define turnfirst(x) (*x == 'r' || *x == 'l')
     48 
     49 void
     50 acceptmove(void)
     51 {
     52 	int ta;
     53 	int ma;
     54 	char af;
     55 	int moved = 0;
     56 	int vma, dir;
     57 	char prompt[60];
     58 	char buf[60], last = '\0';
     59 	char *p;
     60 
     61 	if (!mc->crew3 || snagged(ms) || !windspeed) {
     62 		Msg("Unable to move");
     63 		return;
     64 	}
     65 
     66 	ta = maxturns(ms, &af);
     67 	ma = maxmove(ms, mf->dir, 0);
     68 	sprintf(prompt, "move (%d,%c%d): ", ma, af ? '\'' : ' ', ta);
     69 	sgetstr(prompt, buf, sizeof buf);
     70 	dir = mf->dir;
     71 	vma = ma;
     72 	for (p = buf; *p; p++)
     73 		switch (*p) {
     74 		case 'l':
     75 			dir -= 2;
     76 		case 'r':
     77 			if (++dir == 0)
     78 				dir = 8;
     79 			else if (dir == 9)
     80 				dir = 1;
     81 			if (last == 't') {
     82 				Msg("Ship can't turn that fast.");
     83 				*p-- = '\0';
     84 			}
     85 			last = 't';
     86 			ma--;
     87 			ta--;
     88 			vma = min(ma, maxmove(ms, dir, 0));
     89 			if ((ta < 0 && moved) || (vma < 0 && moved))
     90 				*p-- = '\0';
     91 			break;
     92 		case 'b':
     93 			ma--;
     94 			vma--;
     95 			last = 'b';
     96 			if ((ta < 0 && moved) || (vma < 0 && moved))
     97 				*p-- = '\0';
     98 			break;
     99 		case '0':
    100 		case 'd':
    101 			*p-- = '\0';
    102 			break;
    103 		case '\n':
    104 			*p-- = '\0';
    105 			break;
    106 		case '1': case '2': case '3': case '4':
    107 		case '5': case '6': case '7':
    108 			if (last == '0') {
    109 				Msg("Can't move that fast.");
    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 				Msg("Input error.");
    122 				*p-- = '\0';
    123 			}
    124 		}
    125 	if ((ta < 0 && moved) || (vma < 0 && moved)
    126 	    || (af && turnfirst(buf) && moved)) {
    127 		Msg("Movement error.");
    128 		if (ta < 0 && moved) {
    129 			if (mf->FS == 1) {
    130 				Write(W_FS, ms, 0, 0, 0, 0);
    131 				Msg("No hands to set full sails.");
    132 			}
    133 		} else if (ma >= 0)
    134 			buf[1] = '\0';
    135 	}
    136 	if (af && !moved) {
    137 		if (mf->FS == 1) {
    138 			Write(W_FS, ms, 0, 0, 0, 0);
    139 			Msg("No hands to set full sails.");
    140 		}
    141 	}
    142 	if (*buf)
    143 		strcpy(movebuf, buf);
    144 	else
    145 		strcpy(movebuf, "d");
    146 	Writestr(W_MOVE, ms, movebuf);
    147 	Msg("Helm: %s.", movebuf);
    148 }
    149 
    150 void
    151 acceptboard(void)
    152 {
    153 	struct ship *sp;
    154 	int n;
    155 	int crew[3];
    156 	int men = 0;
    157 	char c;
    158 
    159 	crew[0] = mc->crew1;
    160 	crew[1] = mc->crew2;
    161 	crew[2] = mc->crew3;
    162 	for (n = 0; n < NBP; n++) {
    163 		if (mf->OBP[n].turnsent)
    164 			    men += mf->OBP[n].mensent;
    165 	}
    166 	for (n = 0; n < NBP; n++) {
    167 		if (mf->DBP[n].turnsent)
    168 			    men += mf->DBP[n].mensent;
    169 	}
    170 	if (men) {
    171 		crew[0] = men/100 ? 0 : crew[0] != 0;
    172 		crew[1] = (men%100)/10 ? 0 : crew[1] != 0;
    173 		crew[2] = men%10 ? 0 : crew[2] != 0;
    174 	} else {
    175 		crew[0] = crew[0] != 0;
    176 		crew[1] = crew[1] != 0;
    177 		crew[2] = crew[2] != 0;
    178 	}
    179 	foreachship(sp) {
    180 		if (sp == ms || sp->file->dir == 0 || range(ms, sp) > 1)
    181 			continue;
    182 		if (ms->nationality == capship(sp)->nationality)
    183 			continue;
    184 		if (meleeing(ms, sp) && crew[2]) {
    185 			c = sgetch("How many more to board the $$? ",
    186 				sp, 1);
    187 			parties(crew, sp, 0, c);
    188 		} else if ((fouled2(ms, sp) || grappled2(ms, sp)) && crew[2]) {
    189 			c = sgetch("Crew sections to board the $$ (3 max) ?", sp, 1);
    190 			parties(crew, sp, 0, c);
    191 		}
    192 	}
    193 	if (crew[2]) {
    194 		c = sgetch("How many sections to repel boarders? ",
    195 			(struct ship *)0, 1);
    196 		parties(crew, ms, 1, c);
    197 	}
    198 	blockalarm();
    199 	draw_slot();
    200 	unblockalarm();
    201 }
    202 
    203 void
    204 parties(int *crew, struct ship *to, int isdefense, int buf)
    205 {
    206 	int k, j, men;
    207 	struct BP *ptr;
    208 	int temp[3];
    209 
    210 	for (k = 0; k < 3; k++)
    211 		temp[k] = crew[k];
    212 	if (isdigit(buf)) {
    213 		ptr = isdefense ? to->file->DBP : to->file->OBP;
    214 		for (j = 0; j < NBP && ptr[j].turnsent; j++)
    215 			;
    216 		if (!ptr[j].turnsent && buf > '0') {
    217 			men = 0;
    218 			for (k = 0; k < 3 && buf > '0'; k++) {
    219 				men += crew[k]
    220 					* (k == 0 ? 100 : (k == 1 ? 10 : 1));
    221 				crew[k] = 0;
    222 				if (men)
    223 					buf--;
    224 			}
    225 			if (buf > '0')
    226 				Msg("Sending all crew sections.");
    227 			Write(isdefense ? W_DBP : W_OBP, ms,
    228 				j, turn, to->file->index, men);
    229 			if (isdefense) {
    230 				wmove(slot_w, 2, 0);
    231 				for (k=0; k < NBP; k++)
    232 					if (temp[k] && !crew[k])
    233 						waddch(slot_w, k + '1');
    234 					else
    235 						wmove(slot_w, 2, 1 + k);
    236 				mvwaddstr(slot_w, 3, 0, "DBP");
    237 				makemsg(ms, "repelling boarders");
    238 			} else {
    239 				wmove(slot_w, 0, 0);
    240 				for (k=0; k < NBP; k++)
    241 					if (temp[k] && !crew[k])
    242 						waddch(slot_w, k + '1');
    243 					else
    244 						wmove(slot_w, 0, 1 + k);
    245 				mvwaddstr(slot_w, 1, 0, "OBP");
    246 				makesignal(ms, "boarding the $$", to);
    247 			}
    248 			blockalarm();
    249 			wrefresh(slot_w);
    250 			unblockalarm();
    251 		} else
    252 			Msg("Sending no crew sections.");
    253 	}
    254 }
    255