pl_main.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* $NetBSD: pl_main.c,v 1.16 2003/08/07 09:37:44 agc 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_main.c 8.1 (Berkeley) 5/31/93";
  34. #else
  35. __RCSID("$NetBSD: pl_main.c,v 1.16 2003/08/07 09:37:44 agc Exp $");
  36. #endif
  37. #endif /* not lint */
  38. #include <setjmp.h>
  39. #include <signal.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <unistd.h>
  44. #include "extern.h"
  45. #include "player.h"
  46. #include "restart.h"
  47. static void initialize(void);
  48. /*ARGSUSED*/
  49. int
  50. pl_main(void)
  51. {
  52. initialize();
  53. Msg("Aye aye, Sir");
  54. play();
  55. return 0; /* for lint, play() never returns */
  56. }
  57. static void
  58. initialize(void)
  59. {
  60. struct File *fp;
  61. struct ship *sp;
  62. char captain[80];
  63. char message[60];
  64. int load;
  65. int n;
  66. char *nameptr;
  67. int nat[NNATION];
  68. if (game < 0) {
  69. puts("Choose a scenario:\n");
  70. puts("\n\tNUMBER\tSHIPS\tIN PLAY\tTITLE");
  71. for (n = 0; n < NSCENE; n++) {
  72. /* ( */
  73. printf("\t%d):\t%d\t%s\t%s\n", n, scene[n].vessels,
  74. sync_exists(n) ? "YES" : "no",
  75. scene[n].name);
  76. }
  77. reprint:
  78. printf("\nScenario number? ");
  79. fflush(stdout);
  80. scanf("%d", &game);
  81. while (getchar() != '\n')
  82. ;
  83. }
  84. if (game < 0 || game >= NSCENE) {
  85. puts("Very funny.");
  86. exit(1);
  87. }
  88. cc = &scene[game];
  89. ls = SHIP(cc->vessels);
  90. for (n = 0; n < NNATION; n++)
  91. nat[n] = 0;
  92. foreachship(sp) {
  93. if (sp->file == NULL &&
  94. (sp->file = (struct File *)calloc(1, sizeof (struct File))) == NULL) {
  95. puts("OUT OF MEMORY");
  96. exit(1);
  97. }
  98. sp->file->index = sp - SHIP(0);
  99. sp->file->stern = nat[sp->nationality]++;
  100. sp->file->dir = sp->shipdir;
  101. sp->file->row = sp->shiprow;
  102. sp->file->col = sp->shipcol;
  103. }
  104. windspeed = cc->windspeed;
  105. winddir = cc->winddir;
  106. signal(SIGHUP, choke);
  107. signal(SIGINT, choke);
  108. hasdriver = sync_exists(game);
  109. if (sync_open() < 0) {
  110. perror("sail: syncfile");
  111. exit(1);
  112. }
  113. if (hasdriver) {
  114. puts("Synchronizing with the other players...");
  115. fflush(stdout);
  116. if (Sync() < 0)
  117. leave(LEAVE_SYNC);
  118. }
  119. for (;;) {
  120. foreachship(sp)
  121. if (sp->file->captain[0] == 0 && !sp->file->struck
  122. && sp->file->captured == 0)
  123. break;
  124. if (sp >= ls) {
  125. puts("All ships taken in that scenario.");
  126. foreachship(sp)
  127. free((char *)sp->file);
  128. sync_close(0);
  129. people = 0;
  130. goto reprint;
  131. }
  132. if (randomize) {
  133. player = sp - SHIP(0);
  134. } else {
  135. printf("%s\n\n", cc->name);
  136. foreachship(sp)
  137. printf(" %2d: %-10s %-15s (%-2d pts) %s\n",
  138. sp->file->index,
  139. countryname[sp->nationality],
  140. sp->shipname,
  141. sp->specs->pts,
  142. saywhat(sp, 1));
  143. printf("\nWhich ship (0-%d)? ", cc->vessels-1);
  144. fflush(stdout);
  145. if (scanf("%d", &player) != 1 || player < 0
  146. || player >= cc->vessels) {
  147. while (getchar() != '\n')
  148. ;
  149. puts("Say what?");
  150. player = -1;
  151. } else
  152. while (getchar() != '\n')
  153. ;
  154. }
  155. if (player < 0)
  156. continue;
  157. if (Sync() < 0)
  158. leave(LEAVE_SYNC);
  159. fp = SHIP(player)->file;
  160. if (fp->captain[0] || fp->struck || fp->captured != 0)
  161. puts("That ship is taken.");
  162. else
  163. break;
  164. }
  165. ms = SHIP(player);
  166. mf = ms->file;
  167. mc = ms->specs;
  168. Write(W_BEGIN, ms, 0, 0, 0, 0);
  169. if (Sync() < 0)
  170. leave(LEAVE_SYNC);
  171. signal(SIGCHLD, child);
  172. if (!hasdriver)
  173. switch (fork()) {
  174. case 0:
  175. longjmp(restart, MODE_DRIVER);
  176. /*NOTREACHED*/
  177. case -1:
  178. perror("fork");
  179. leave(LEAVE_FORK);
  180. break;
  181. default:
  182. hasdriver++;
  183. }
  184. printf("Your ship is the %s, a %d gun %s (%s crew).\n",
  185. ms->shipname, mc->guns, classname[mc->class],
  186. qualname[mc->qual]);
  187. if ((nameptr = (char *) getenv("SAILNAME")) && *nameptr)
  188. strncpy(captain, nameptr, sizeof captain);
  189. else {
  190. printf("Your name, Captain? ");
  191. fflush(stdout);
  192. fgets(captain, sizeof captain, stdin);
  193. if (!*captain)
  194. strcpy(captain, "no name");
  195. else
  196. captain[strlen(captain) - 1] = '\0';
  197. }
  198. captain[sizeof captain - 1] = '\0';
  199. Writestr(W_CAPTAIN, ms, captain);
  200. for (n = 0; n < 2; n++) {
  201. char buf[10];
  202. printf("\nInitial broadside %s (grape, chain, round, double): ",
  203. n ? "right" : "left");
  204. fflush(stdout);
  205. scanf("%s", buf);
  206. switch (*buf) {
  207. case 'g':
  208. load = L_GRAPE;
  209. break;
  210. case 'c':
  211. load = L_CHAIN;
  212. break;
  213. case 'r':
  214. load = L_ROUND;
  215. break;
  216. case 'd':
  217. load = L_DOUBLE;
  218. break;
  219. default:
  220. load = L_ROUND;
  221. }
  222. if (n) {
  223. mf->loadR = load;
  224. mf->readyR = R_LOADED|R_INITIAL;
  225. } else {
  226. mf->loadL = load;
  227. mf->readyL = R_LOADED|R_INITIAL;
  228. }
  229. }
  230. initscreen();
  231. draw_board();
  232. snprintf(message, sizeof message, "Captain %s assuming command",
  233. captain);
  234. Writestr(W_SIGNAL, ms, message);
  235. newturn(0);
  236. }