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