dr_2.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* $NetBSD: dr_2.c,v 1.19 2003/08/07 09:37:41 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[] = "@(#)dr_2.c 8.1 (Berkeley) 5/31/93";
  34. #else
  35. __RCSID("$NetBSD: dr_2.c,v 1.19 2003/08/07 09:37:41 agc Exp $");
  36. #endif
  37. #endif /* not lint */
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include "extern.h"
  42. #include "driver.h"
  43. #define couldwin(f,t) (f->specs->crew2 > t->specs->crew2 * 1.5)
  44. static int str_end(const char *);
  45. static int score(struct ship *, struct ship *, char *, int);
  46. static void move_ship(struct ship *, const char *, unsigned char *, short *, short *, char *);
  47. static void try(struct ship *, struct ship *, char *, char *, int, int, int, int, int, int *, int);
  48. static void rmend(char *);
  49. const int dtab[] = {0,1,1,2,3,4,4,5}; /* diagonal distances in x==y */
  50. void
  51. thinkofgrapples(void)
  52. {
  53. struct ship *sp, *sq;
  54. char friendly;
  55. foreachship(sp) {
  56. if (sp->file->captain[0] || sp->file->dir == 0)
  57. continue;
  58. foreachship(sq) {
  59. friendly = sp->nationality == capship(sq)->nationality;
  60. if (!friendly) {
  61. if (sp->file->struck || sp->file->captured != 0)
  62. continue;
  63. if (range(sp, sq) != 1)
  64. continue;
  65. if (grappled2(sp, sq))
  66. if (is_toughmelee(sp, sq, 0, 0))
  67. ungrap(sp, sq);
  68. else
  69. grap(sp, sq);
  70. else if (couldwin(sp, sq)) {
  71. grap(sp, sq);
  72. sp->file->loadwith = L_GRAPE;
  73. }
  74. } else
  75. ungrap(sp, sq);
  76. }
  77. }
  78. }
  79. void
  80. checkup(void)
  81. {
  82. struct ship *sp, *sq;
  83. char explode, sink;
  84. foreachship(sp) {
  85. if (sp->file->dir == 0)
  86. continue;
  87. explode = sp->file->explode;
  88. sink = sp->file->sink;
  89. if (explode != 1 && sink != 1)
  90. continue;
  91. if (dieroll() < 5)
  92. continue;
  93. Write(sink == 1 ? W_SINK : W_EXPLODE, sp, 2, 0, 0, 0);
  94. Write(W_DIR, sp, 0, 0, 0, 0);
  95. if (snagged(sp))
  96. foreachship(sq)
  97. cleansnag(sp, sq, 1);
  98. if (sink != 1) {
  99. makemsg(sp, "exploding!");
  100. foreachship(sq) {
  101. if (sp != sq && sq->file->dir && range(sp, sq) < 4)
  102. table(sp, sq, RIGGING, L_EXPLODE, sp->specs->guns/13, 6);
  103. }
  104. } else
  105. makemsg(sp, "sinking!");
  106. }
  107. }
  108. void
  109. prizecheck(void)
  110. {
  111. struct ship *sp;
  112. foreachship(sp) {
  113. if (sp->file->captured == 0)
  114. continue;
  115. if (sp->file->struck || sp->file->dir == 0)
  116. continue;
  117. if (sp->specs->crew1 + sp->specs->crew2 + sp->specs->crew3 > sp->file->pcrew * 6) {
  118. Writestr(W_SIGNAL, sp, "prize crew overthrown");
  119. Write(W_POINTS, sp->file->captured, sp->file->captured->file->points - 2 * sp->specs->pts, 0, 0, 0);
  120. Write(W_CAPTURED, sp, -1, 0, 0, 0);
  121. }
  122. }
  123. }
  124. static int
  125. str_end(const char *str)
  126. {
  127. const char *p;
  128. for (p = str; *p; p++)
  129. ;
  130. return p == str ? 0 : p[-1];
  131. }
  132. void
  133. closeon(struct ship *from, struct ship *to, char *command, int ta, int ma, int af)
  134. {
  135. int high;
  136. char temp[10];
  137. temp[0] = command[0] = '\0';
  138. high = -30000;
  139. try(from, to, command, temp, ma, ta, af, ma, from->file->dir, &high, 0);
  140. }
  141. static int
  142. score(struct ship *ship, struct ship *to, char *movement, int onlytemp)
  143. {
  144. char drift;
  145. int row, col, dir, total, ran;
  146. struct File *fp = ship->file;
  147. if ((dir = fp->dir) == 0)
  148. return 0;
  149. row = fp->row;
  150. col = fp->col;
  151. drift = fp->drift;
  152. move_ship(ship, movement, &fp->dir, &fp->row, &fp->col, &drift);
  153. if (!*movement)
  154. strcpy(movement, "d");
  155. ran = range(ship, to);
  156. total = -50 * ran;
  157. if (ran < 4 && gunsbear(ship, to))
  158. total += 60;
  159. if ((ran = portside(ship, to, 1) - fp->dir) == 4 || ran == -4)
  160. total = -30000;
  161. if (!onlytemp) {
  162. fp->row = row;
  163. fp->col = col;
  164. fp->dir = dir;
  165. }
  166. return total;
  167. }
  168. static void
  169. move_ship(struct ship *ship, const char *p, unsigned char *dir, short *row, short *col, char *drift)
  170. {
  171. int dist;
  172. char moved = 0;
  173. for (; *p; p++) {
  174. switch (*p) {
  175. case 'r':
  176. if (++*dir == 9)
  177. *dir = 1;
  178. break;
  179. case 'l':
  180. if (--*dir == 0)
  181. *dir = 8;
  182. break;
  183. case '1': case '2': case '3': case '4':
  184. case '5': case '6': case '7':
  185. moved++;
  186. if (*dir % 2 == 0)
  187. dist = dtab[*p - '0'];
  188. else
  189. dist = *p - '0';
  190. *row -= dr[*dir] * dist;
  191. *col -= dc[*dir] * dist;
  192. break;
  193. }
  194. }
  195. if (!moved) {
  196. if (windspeed != 0 && ++*drift > 2) {
  197. if ((ship->specs->class >= 3 && !snagged(ship))
  198. || (turn & 1) == 0) {
  199. *row -= dr[winddir];
  200. *col -= dc[winddir];
  201. }
  202. }
  203. } else
  204. *drift = 0;
  205. }
  206. static void
  207. try(struct ship *f, struct ship *t, char *command, char *temp, int ma, int ta, int af, int vma, int dir, int *high, int rakeme)
  208. {
  209. int new, n;
  210. char st[4];
  211. #define rakeyou (gunsbear(f, t) && !gunsbear(t, f))
  212. if ((n = str_end(temp)) < '1' || n > '9')
  213. for (n = 1; vma - n >= 0; n++) {
  214. sprintf(st, "%d", n);
  215. strcat(temp, st);
  216. new = score(f, t, temp, rakeme);
  217. if (new > *high && (!rakeme || rakeyou)) {
  218. *high = new;
  219. strcpy(command, temp);
  220. }
  221. try(f, t, command, temp, ma-n, ta, af, vma-n,
  222. dir, high, rakeme);
  223. rmend(temp);
  224. }
  225. if ((ma > 0 && ta > 0 && (n = str_end(temp)) != 'l' && n != 'r') || !strlen(temp)) {
  226. strcat(temp, "r");
  227. new = score(f, t, temp, rakeme);
  228. if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))) {
  229. *high = new;
  230. strcpy(command, temp);
  231. }
  232. try(f, t, command, temp, ma-1, ta-1, af, min(ma-1, maxmove(f, (dir == 8 ? 1 : dir+1), 0)), (dir == 8 ? 1 : dir+1), high, rakeme);
  233. rmend(temp);
  234. }
  235. if ((ma > 0 && ta > 0 && (n = str_end(temp)) != 'l' && n != 'r') || !strlen(temp)){
  236. strcat(temp, "l");
  237. new = score(f, t, temp, rakeme);
  238. if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))){
  239. *high = new;
  240. strcpy(command, temp);
  241. }
  242. try(f, t, command, temp, ma-1, ta-1, af, (min(ma-1,maxmove(f, (dir-1 ? dir-1 : 8), 0))), (dir-1 ? dir -1 : 8), high, rakeme);
  243. rmend(temp);
  244. }
  245. }
  246. static void
  247. rmend(char *str)
  248. {
  249. char *p;
  250. for (p = str; *p; p++)
  251. ;
  252. if (p != str)
  253. *--p = 0;
  254. }