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