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