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