dr_3.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /* $NetBSD: dr_3.c,v 1.15 2003/08/07 09:37:42 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_3.c 8.1 (Berkeley) 5/31/93";
  34. #else
  35. __RCSID("$NetBSD: dr_3.c,v 1.15 2003/08/07 09:37:42 agc Exp $");
  36. #endif
  37. #endif /* not lint */
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include "extern.h"
  41. #include "driver.h"
  42. static int stillmoving(int);
  43. static int is_isolated(struct ship *);
  44. static int push(struct ship *, struct ship *);
  45. static void step(struct ship *, int, char *);
  46. /* move all comp ships */
  47. void
  48. moveall(void)
  49. {
  50. struct ship *sp, *sq;
  51. int n;
  52. int k, l;
  53. int row[NSHIP], col[NSHIP], dir[NSHIP], drift[NSHIP];
  54. char moved[NSHIP];
  55. /*
  56. * first try to create moves for OUR ships
  57. */
  58. foreachship(sp) {
  59. struct ship *closest;
  60. int ma, ta;
  61. char af;
  62. if (sp->file->captain[0] || sp->file->dir == 0)
  63. continue;
  64. if (!sp->file->struck && windspeed && !snagged(sp)
  65. && sp->specs->crew3) {
  66. ta = maxturns(sp, &af);
  67. ma = maxmove(sp, sp->file->dir, 0);
  68. closest = closestenemy(sp, 0, 0);
  69. if (closest == 0)
  70. *sp->file->movebuf = '\0';
  71. else
  72. closeon(sp, closest, sp->file->movebuf,
  73. ta, ma, af);
  74. } else
  75. *sp->file->movebuf = '\0';
  76. }
  77. /*
  78. * Then execute the moves for ALL ships (dead ones too),
  79. * checking for collisions and snags at each step.
  80. * The old positions are saved in row[], col[], dir[].
  81. * At the end, we compare and write out the changes.
  82. */
  83. n = 0;
  84. foreachship(sp) {
  85. if (snagged(sp))
  86. strcpy(sp->file->movebuf, "d");
  87. else
  88. if (*sp->file->movebuf != 'd')
  89. strcat(sp->file->movebuf, "d");
  90. row[n] = sp->file->row;
  91. col[n] = sp->file->col;
  92. dir[n] = sp->file->dir;
  93. drift[n] = sp->file->drift;
  94. moved[n] = 0;
  95. n++;
  96. }
  97. /*
  98. * Now resolve collisions.
  99. * This is the tough part.
  100. */
  101. for (k = 0; stillmoving(k); k++) {
  102. /*
  103. * Step once.
  104. * And propagate the nulls at the end of sp->file->movebuf.
  105. */
  106. n = 0;
  107. foreachship(sp) {
  108. if (!sp->file->movebuf[k])
  109. sp->file->movebuf[k+1] = '\0';
  110. else if (sp->file->dir)
  111. step(sp, sp->file->movebuf[k], &moved[n]);
  112. n++;
  113. }
  114. /*
  115. * The real stuff.
  116. */
  117. n = 0;
  118. foreachship(sp) {
  119. if (sp->file->dir == 0 || is_isolated(sp))
  120. goto cont1;
  121. l = 0;
  122. foreachship(sq) {
  123. char snap = 0;
  124. if (sp == sq)
  125. goto cont2;
  126. if (sq->file->dir == 0)
  127. goto cont2;
  128. if (!push(sp, sq))
  129. goto cont2;
  130. if (snagged2(sp, sq) && range(sp, sq) > 1)
  131. snap++;
  132. if (!range(sp, sq) && !fouled2(sp, sq)) {
  133. makesignal(sp, "collision with $$", sq);
  134. if (dieroll() < 4) {
  135. makesignal(sp, "fouled with $$",
  136. sq);
  137. Write(W_FOUL, sp, l, 0, 0, 0);
  138. Write(W_FOUL, sq, n, 0, 0, 0);
  139. }
  140. snap++;
  141. }
  142. if (snap) {
  143. sp->file->movebuf[k + 1] = 0;
  144. sq->file->movebuf[k + 1] = 0;
  145. sq->file->row = sp->file->row - 1;
  146. if (sp->file->dir == 1
  147. || sp->file->dir == 5)
  148. sq->file->col =
  149. sp->file->col - 1;
  150. else
  151. sq->file->col = sp->file->col;
  152. sq->file->dir = sp->file->dir;
  153. }
  154. cont2:
  155. l++;
  156. }
  157. cont1:
  158. n++;
  159. }
  160. }
  161. /*
  162. * Clear old moves. And write out new pos.
  163. */
  164. n = 0;
  165. foreachship(sp) {
  166. if (sp->file->dir != 0) {
  167. *sp->file->movebuf = 0;
  168. if (row[n] != sp->file->row)
  169. Write(W_ROW, sp, sp->file->row, 0, 0, 0);
  170. if (col[n] != sp->file->col)
  171. Write(W_COL, sp, sp->file->col, 0, 0, 0);
  172. if (dir[n] != sp->file->dir)
  173. Write(W_DIR, sp, sp->file->dir, 0, 0, 0);
  174. if (drift[n] != sp->file->drift)
  175. Write(W_DRIFT, sp, sp->file->drift, 0, 0, 0);
  176. }
  177. n++;
  178. }
  179. }
  180. static int
  181. stillmoving(int k)
  182. {
  183. struct ship *sp;
  184. foreachship(sp)
  185. if (sp->file->movebuf[k])
  186. return 1;
  187. return 0;
  188. }
  189. static int
  190. is_isolated(struct ship *ship)
  191. {
  192. struct ship *sp;
  193. foreachship(sp) {
  194. if (ship != sp && range(ship, sp) <= 10)
  195. return 0;
  196. }
  197. return 1;
  198. }
  199. static int
  200. push(struct ship *from, struct ship *to)
  201. {
  202. int bs, sb;
  203. sb = to->specs->guns;
  204. bs = from->specs->guns;
  205. if (sb > bs)
  206. return 1;
  207. if (sb < bs)
  208. return 0;
  209. return from < to;
  210. }
  211. static void
  212. step(struct ship *sp, int com, char *moved)
  213. {
  214. int dist;
  215. switch (com) {
  216. case 'r':
  217. if (++sp->file->dir == 9)
  218. sp->file->dir = 1;
  219. break;
  220. case 'l':
  221. if (--sp->file->dir == 0)
  222. sp->file->dir = 8;
  223. break;
  224. case '0': case '1': case '2': case '3':
  225. case '4': case '5': case '6': case '7':
  226. if (sp->file->dir % 2 == 0)
  227. dist = dtab[com - '0'];
  228. else
  229. dist = com - '0';
  230. sp->file->row -= dr[sp->file->dir] * dist;
  231. sp->file->col -= dc[sp->file->dir] * dist;
  232. *moved = 1;
  233. break;
  234. case 'b':
  235. break;
  236. case 'd':
  237. if (!*moved) {
  238. if (windspeed != 0 && ++sp->file->drift > 2 &&
  239. ((sp->specs->class >= 3 && !snagged(sp))
  240. || (turn & 1) == 0)) {
  241. sp->file->row -= dr[winddir];
  242. sp->file->col -= dc[winddir];
  243. }
  244. } else
  245. sp->file->drift = 0;
  246. break;
  247. }
  248. }
  249. void
  250. sendbp(struct ship *from, struct ship *to, int sections, int isdefense)
  251. {
  252. int n;
  253. struct BP *bp;
  254. bp = isdefense ? from->file->DBP : from->file->OBP;
  255. for (n = 0; n < NBP && bp[n].turnsent; n++)
  256. ;
  257. if (n < NBP && sections) {
  258. Write(isdefense ? W_DBP : W_OBP, from,
  259. n, turn, to->file->index, sections);
  260. if (isdefense)
  261. makemsg(from, "repelling boarders");
  262. else
  263. makesignal(from, "boarding the $$", to);
  264. }
  265. }
  266. int
  267. is_toughmelee(struct ship *ship, struct ship *to, int isdefense, int count)
  268. {
  269. struct BP *bp;
  270. int obp = 0;
  271. int n, OBP = 0, DBP = 0, dbp = 0;
  272. int qual;
  273. qual = ship->specs->qual;
  274. bp = isdefense ? ship->file->DBP : ship->file->OBP;
  275. for (n = 0; n < NBP; n++, bp++) {
  276. if (bp->turnsent && (to == bp->toship || isdefense)) {
  277. obp += bp->mensent / 100
  278. ? ship->specs->crew1 * qual : 0;
  279. obp += (bp->mensent % 100)/10
  280. ? ship->specs->crew2 * qual : 0;
  281. obp += bp->mensent % 10
  282. ? ship->specs->crew3 * qual : 0;
  283. }
  284. }
  285. if (count || isdefense)
  286. return obp;
  287. OBP = is_toughmelee(to, ship, 0, count + 1);
  288. dbp = is_toughmelee(ship, to, 1, count + 1);
  289. DBP = is_toughmelee(to, ship, 1, count + 1);
  290. if (OBP > obp + 10 || OBP + DBP >= obp + dbp + 10)
  291. return 1;
  292. else
  293. return 0;
  294. }
  295. void
  296. reload(void)
  297. {
  298. struct ship *sp;
  299. foreachship(sp) {
  300. sp->file->loadwith = 0;
  301. }
  302. }
  303. void
  304. checksails(void)
  305. {
  306. struct ship *sp;
  307. int rig, full;
  308. struct ship *close;
  309. foreachship(sp) {
  310. if (sp->file->captain[0] != 0)
  311. continue;
  312. rig = sp->specs->rig1;
  313. if (windspeed == 6 || (windspeed == 5 && sp->specs->class > 4))
  314. rig = 0;
  315. if (rig && sp->specs->crew3) {
  316. close = closestenemy(sp, 0, 0);
  317. if (close != 0) {
  318. if (range(sp, close) > 9)
  319. full = 1;
  320. else
  321. full = 0;
  322. } else
  323. full = 0;
  324. } else
  325. full = 0;
  326. if ((sp->file->FS != 0) != full)
  327. Write(W_FS, sp, full, 0, 0, 0);
  328. }
  329. }