pl_7.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /* $NetBSD: pl_7.c,v 1.27 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_7.c 8.1 (Berkeley) 5/31/93";
  34. #else
  35. __RCSID("$NetBSD: pl_7.c,v 1.27 2003/08/07 09:37:44 agc Exp $");
  36. #endif
  37. #endif /* not lint */
  38. #include <curses.h>
  39. #include <signal.h>
  40. #include <stdarg.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <unistd.h>
  44. #include <string.h>
  45. #include "extern.h"
  46. #include "player.h"
  47. #include "display.h"
  48. static void Scroll(void);
  49. static void endprompt(int);
  50. static void adjustview(void);
  51. /*
  52. * Display interface
  53. */
  54. static char sc_hasprompt;
  55. static const char *sc_prompt;
  56. static const char *sc_buf;
  57. static int sc_line;
  58. WINDOW *view_w;
  59. WINDOW *slot_w;
  60. WINDOW *scroll_w;
  61. WINDOW *stat_w;
  62. WINDOW *turn_w;
  63. int done_curses;
  64. int loaded, fired, changed, repaired;
  65. int dont_adjust;
  66. int viewrow, viewcol;
  67. char movebuf[sizeof SHIP(0)->file->movebuf];
  68. int player;
  69. struct ship *ms; /* memorial structure, &cc->ship[player] */
  70. struct File *mf; /* ms->file */
  71. struct shipspecs *mc; /* ms->specs */
  72. void
  73. initscreen(void)
  74. {
  75. if (!SCREENTEST()) {
  76. printf("Can't sail on this terminal.\n");
  77. exit(1);
  78. }
  79. /* initscr() already done in SCREENTEST() */
  80. view_w = newwin(VIEW_Y, VIEW_X, VIEW_T, VIEW_L);
  81. slot_w = newwin(SLOT_Y, SLOT_X, SLOT_T, SLOT_L);
  82. scroll_w = newwin(SCROLL_Y, SCROLL_X, SCROLL_T, SCROLL_L);
  83. stat_w = newwin(STAT_Y, STAT_X, STAT_T, STAT_L);
  84. turn_w = newwin(TURN_Y, TURN_X, TURN_T, TURN_L);
  85. done_curses++;
  86. leaveok(view_w, 1);
  87. leaveok(slot_w, 1);
  88. leaveok(stat_w, 1);
  89. leaveok(turn_w, 1);
  90. noecho();
  91. cbreak();
  92. }
  93. void
  94. cleanupscreen(void)
  95. {
  96. /* alarm already turned off */
  97. if (done_curses) {
  98. wmove(scroll_w, SCROLL_Y - 1, 0);
  99. wclrtoeol(scroll_w);
  100. draw_screen();
  101. endwin();
  102. }
  103. }
  104. /*ARGSUSED*/
  105. void
  106. newturn(int n __attribute__((__unused__)))
  107. {
  108. repaired = loaded = fired = changed = 0;
  109. movebuf[0] = '\0';
  110. alarm(0);
  111. if (mf->readyL & R_LOADING) {
  112. if (mf->readyL & R_DOUBLE)
  113. mf->readyL = R_LOADING;
  114. else
  115. mf->readyL = R_LOADED;
  116. }
  117. if (mf->readyR & R_LOADING) {
  118. if (mf->readyR & R_DOUBLE)
  119. mf->readyR = R_LOADING;
  120. else
  121. mf->readyR = R_LOADED;
  122. }
  123. if (!hasdriver)
  124. Write(W_DDEAD, SHIP(0), 0, 0, 0, 0);
  125. if (sc_hasprompt) {
  126. wmove(scroll_w, sc_line, 0);
  127. wclrtoeol(scroll_w);
  128. }
  129. if (Sync() < 0)
  130. leave(LEAVE_SYNC);
  131. if (!hasdriver)
  132. leave(LEAVE_DRIVER);
  133. if (sc_hasprompt)
  134. wprintw(scroll_w, "%s%s", sc_prompt, sc_buf);
  135. if (turn % 50 == 0)
  136. Write(W_ALIVE, SHIP(0), 0, 0, 0, 0);
  137. if (mf->FS && (!mc->rig1 || windspeed == 6))
  138. Write(W_FS, ms, 0, 0, 0, 0);
  139. if (mf->FS == 1)
  140. Write(W_FS, ms, 2, 0, 0, 0);
  141. if (mf->struck)
  142. leave(LEAVE_QUIT);
  143. if (mf->captured != 0)
  144. leave(LEAVE_CAPTURED);
  145. if (windspeed == 7)
  146. leave(LEAVE_HURRICAN);
  147. adjustview();
  148. draw_screen();
  149. signal(SIGALRM, newturn);
  150. alarm(7);
  151. }
  152. /*VARARGS2*/
  153. void
  154. Signal(const char *fmt, struct ship *ship, ...)
  155. {
  156. va_list ap;
  157. char format[BUFSIZ];
  158. if (!done_curses)
  159. return;
  160. va_start(ap, ship);
  161. if (*fmt == '\7')
  162. putchar(*fmt++);
  163. fmtship(format, sizeof(format), fmt, ship);
  164. vwprintw(scroll_w, format, ap);
  165. va_end(ap);
  166. Scroll();
  167. }
  168. /*VARARGS2*/
  169. void
  170. Msg(const char *fmt, ...)
  171. {
  172. va_list ap;
  173. if (!done_curses)
  174. return;
  175. va_start(ap, fmt);
  176. if (*fmt == '\7')
  177. putchar(*fmt++);
  178. vwprintw(scroll_w, fmt, ap);
  179. va_end(ap);
  180. Scroll();
  181. }
  182. static void
  183. Scroll(void)
  184. {
  185. if (++sc_line >= SCROLL_Y)
  186. sc_line = 0;
  187. wmove(scroll_w, sc_line, 0);
  188. wclrtoeol(scroll_w);
  189. }
  190. void
  191. prompt(const char *p, struct ship *ship)
  192. {
  193. static char buf[BUFSIZ];
  194. fmtship(buf, sizeof(buf), p, ship);
  195. sc_prompt = buf;
  196. sc_buf = "";
  197. sc_hasprompt = 1;
  198. waddstr(scroll_w, buf);
  199. }
  200. static void
  201. endprompt(int flag)
  202. {
  203. sc_hasprompt = 0;
  204. if (flag)
  205. Scroll();
  206. }
  207. int
  208. sgetch(const char *p, struct ship *ship, int flag)
  209. {
  210. int c;
  211. prompt(p, ship);
  212. blockalarm();
  213. wrefresh(scroll_w);
  214. unblockalarm();
  215. while ((c = wgetch(scroll_w)) == EOF)
  216. ;
  217. if (flag && c >= ' ' && c < 0x7f)
  218. waddch(scroll_w, c);
  219. endprompt(flag);
  220. return c;
  221. }
  222. void
  223. sgetstr(const char *pr, char *buf, int n)
  224. {
  225. int c;
  226. char *p = buf;
  227. prompt(pr, (struct ship *)0);
  228. sc_buf = buf;
  229. for (;;) {
  230. *p = 0;
  231. blockalarm();
  232. wrefresh(scroll_w);
  233. unblockalarm();
  234. while ((c = wgetch(scroll_w)) == EOF)
  235. ;
  236. switch (c) {
  237. case '\n':
  238. case '\r':
  239. endprompt(1);
  240. return;
  241. case '\b':
  242. if (p > buf) {
  243. waddstr(scroll_w, "\b \b");
  244. p--;
  245. }
  246. break;
  247. default:
  248. if (c >= ' ' && c < 0x7f && p < buf + n - 1) {
  249. *p++ = c;
  250. waddch(scroll_w, c);
  251. } else
  252. putchar('\a');
  253. }
  254. }
  255. }
  256. void
  257. draw_screen(void)
  258. {
  259. draw_view();
  260. draw_turn();
  261. draw_stat();
  262. draw_slot();
  263. wrefresh(scroll_w); /* move the cursor */
  264. }
  265. void
  266. draw_view(void)
  267. {
  268. struct ship *sp;
  269. werase(view_w);
  270. foreachship(sp) {
  271. if (sp->file->dir
  272. && sp->file->row > viewrow
  273. && sp->file->row < viewrow + VIEW_Y
  274. && sp->file->col > viewcol
  275. && sp->file->col < viewcol + VIEW_X) {
  276. wmove(view_w, sp->file->row - viewrow,
  277. sp->file->col - viewcol);
  278. waddch(view_w, colours(sp));
  279. wmove(view_w,
  280. sternrow(sp) - viewrow,
  281. sterncol(sp) - viewcol);
  282. waddch(view_w, sterncolour(sp));
  283. }
  284. }
  285. wrefresh(view_w);
  286. }
  287. void
  288. draw_turn(void)
  289. {
  290. wmove(turn_w, 0, 0);
  291. wprintw(turn_w, "%cTurn %d", dont_adjust?'*':'-', turn);
  292. wrefresh(turn_w);
  293. }
  294. void
  295. draw_stat(void)
  296. {
  297. wmove(stat_w, STAT_1, 0);
  298. wprintw(stat_w, "Points %3d\n", mf->points);
  299. wprintw(stat_w, "Fouls %2d\n", fouled(ms));
  300. wprintw(stat_w, "Grapples %2d\n", grappled(ms));
  301. wmove(stat_w, STAT_2, 0);
  302. wprintw(stat_w, " 0 %c(%c)\n",
  303. maxmove(ms, winddir + 3, -1) + '0',
  304. maxmove(ms, winddir + 3, 1) + '0');
  305. waddstr(stat_w, " \\|/\n");
  306. wprintw(stat_w, " -^-%c(%c)\n",
  307. maxmove(ms, winddir + 2, -1) + '0',
  308. maxmove(ms, winddir + 2, 1) + '0');
  309. waddstr(stat_w, " /|\\\n");
  310. wprintw(stat_w, " | %c(%c)\n",
  311. maxmove(ms, winddir + 1, -1) + '0',
  312. maxmove(ms, winddir + 1, 1) + '0');
  313. wprintw(stat_w, " %c(%c)\n",
  314. maxmove(ms, winddir, -1) + '0',
  315. maxmove(ms, winddir, 1) + '0');
  316. wmove(stat_w, STAT_3, 0);
  317. wprintw(stat_w, "Load %c%c %c%c\n",
  318. loadname[mf->loadL], readyname(mf->readyL),
  319. loadname[mf->loadR], readyname(mf->readyR));
  320. wprintw(stat_w, "Hull %2d\n", mc->hull);
  321. wprintw(stat_w, "Crew %2d %2d %2d\n",
  322. mc->crew1, mc->crew2, mc->crew3);
  323. wprintw(stat_w, "Guns %2d %2d\n", mc->gunL, mc->gunR);
  324. wprintw(stat_w, "Carr %2d %2d\n", mc->carL, mc->carR);
  325. wprintw(stat_w, "Rigg %d %d %d ", mc->rig1, mc->rig2, mc->rig3);
  326. if (mc->rig4 < 0)
  327. waddch(stat_w, '-');
  328. else
  329. wprintw(stat_w, "%d", mc->rig4);
  330. wrefresh(stat_w);
  331. }
  332. void
  333. draw_slot(void)
  334. {
  335. if (!boarding(ms, 0)) {
  336. mvwaddstr(slot_w, 0, 0, " ");
  337. mvwaddstr(slot_w, 1, 0, " ");
  338. } else
  339. mvwaddstr(slot_w, 1, 0, "OBP");
  340. if (!boarding(ms, 1)) {
  341. mvwaddstr(slot_w, 2, 0, " ");
  342. mvwaddstr(slot_w, 3, 0, " ");
  343. } else
  344. mvwaddstr(slot_w, 3, 0, "DBP");
  345. wmove(slot_w, SLOT_Y-4, 0);
  346. if (mf->RH)
  347. wprintw(slot_w, "%dRH", mf->RH);
  348. else
  349. waddstr(slot_w, " ");
  350. wmove(slot_w, SLOT_Y-3, 0);
  351. if (mf->RG)
  352. wprintw(slot_w, "%dRG", mf->RG);
  353. else
  354. waddstr(slot_w, " ");
  355. wmove(slot_w, SLOT_Y-2, 0);
  356. if (mf->RR)
  357. wprintw(slot_w, "%dRR", mf->RR);
  358. else
  359. waddstr(slot_w, " ");
  360. #define Y (SLOT_Y/2)
  361. wmove(slot_w, 7, 1);
  362. wprintw(slot_w,"%d", windspeed);
  363. mvwaddch(slot_w, Y, 0, ' ');
  364. mvwaddch(slot_w, Y, 2, ' ');
  365. mvwaddch(slot_w, Y-1, 0, ' ');
  366. mvwaddch(slot_w, Y-1, 1, ' ');
  367. mvwaddch(slot_w, Y-1, 2, ' ');
  368. mvwaddch(slot_w, Y+1, 0, ' ');
  369. mvwaddch(slot_w, Y+1, 1, ' ');
  370. mvwaddch(slot_w, Y+1, 2, ' ');
  371. wmove(slot_w, Y - dr[winddir], 1 - dc[winddir]);
  372. switch (winddir) {
  373. case 1:
  374. case 5:
  375. waddch(slot_w, '|');
  376. break;
  377. case 2:
  378. case 6:
  379. waddch(slot_w, '/');
  380. break;
  381. case 3:
  382. case 7:
  383. waddch(slot_w, '-');
  384. break;
  385. case 4:
  386. case 8:
  387. waddch(slot_w, '\\');
  388. break;
  389. }
  390. mvwaddch(slot_w, Y + dr[winddir], 1 + dc[winddir], '+');
  391. wrefresh(slot_w);
  392. }
  393. void
  394. draw_board(void)
  395. {
  396. int n;
  397. clear();
  398. werase(view_w);
  399. werase(slot_w);
  400. werase(scroll_w);
  401. werase(stat_w);
  402. werase(turn_w);
  403. sc_line = 0;
  404. move(BOX_T, BOX_L);
  405. for (n = 0; n < BOX_X; n++)
  406. addch('-');
  407. move(BOX_B, BOX_L);
  408. for (n = 0; n < BOX_X; n++)
  409. addch('-');
  410. for (n = BOX_T+1; n < BOX_B; n++) {
  411. mvaddch(n, BOX_L, '|');
  412. mvaddch(n, BOX_R, '|');
  413. }
  414. mvaddch(BOX_T, BOX_L, '+');
  415. mvaddch(BOX_T, BOX_R, '+');
  416. mvaddch(BOX_B, BOX_L, '+');
  417. mvaddch(BOX_B, BOX_R, '+');
  418. refresh();
  419. #define WSaIM "Wooden Ships & Iron Men"
  420. wmove(view_w, 2, (VIEW_X - sizeof WSaIM - 1) / 2);
  421. waddstr(view_w, WSaIM);
  422. wmove(view_w, 4, (VIEW_X - strlen(cc->name)) / 2);
  423. waddstr(view_w, cc->name);
  424. wrefresh(view_w);
  425. move(LINE_T, LINE_L);
  426. printw("Class %d %s (%d guns) '%s' (%c%c)",
  427. mc->class,
  428. classname[mc->class],
  429. mc->guns,
  430. ms->shipname,
  431. colours(ms),
  432. sterncolour(ms));
  433. refresh();
  434. }
  435. void
  436. centerview(void)
  437. {
  438. viewrow = mf->row - VIEW_Y / 2;
  439. viewcol = mf->col - VIEW_X / 2;
  440. }
  441. void
  442. upview(void)
  443. {
  444. viewrow -= VIEW_Y / 3;
  445. }
  446. void
  447. downview(void)
  448. {
  449. viewrow += VIEW_Y / 3;
  450. }
  451. void
  452. leftview(void)
  453. {
  454. viewcol -= VIEW_X / 5;
  455. }
  456. void
  457. rightview(void)
  458. {
  459. viewcol += VIEW_X / 5;
  460. }
  461. static void
  462. adjustview(void)
  463. {
  464. if (dont_adjust)
  465. return;
  466. if (mf->row < viewrow + VIEW_Y/4)
  467. viewrow = mf->row - (VIEW_Y - VIEW_Y/4);
  468. else if (mf->row > viewrow + (VIEW_Y - VIEW_Y/4))
  469. viewrow = mf->row - VIEW_Y/4;
  470. if (mf->col < viewcol + VIEW_X/8)
  471. viewcol = mf->col - (VIEW_X - VIEW_X/8);
  472. else if (mf->col > viewcol + (VIEW_X - VIEW_X/8))
  473. viewcol = mf->col - VIEW_X/8;
  474. }