pl_5.c 6.3 KB

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