misc.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* $NetBSD: misc.c,v 1.15 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[] = "@(#)misc.c 8.2 (Berkeley) 4/28/95";
  34. #else
  35. __RCSID("$NetBSD: misc.c,v 1.15 2004/11/05 21:30:32 dsl Exp $");
  36. #endif
  37. #endif /* not lint */
  38. #include <sys/file.h>
  39. #include <ctype.h>
  40. #include <stdio.h>
  41. #include <unistd.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include "extern.h"
  45. #include "pathnames.h"
  46. #define distance(x,y) (abs(x) >= abs(y) ? abs(x) + abs(y)/2 : abs(y) + abs(x)/2)
  47. static int angle(int, int);
  48. /* XXX */
  49. int
  50. range(struct ship *from, struct ship *to)
  51. {
  52. int bow1r, bow1c, bow2r, bow2c;
  53. int stern1r, stern1c, stern2c, stern2r;
  54. int bb, bs, sb, ss, result;
  55. if (!to->file->dir)
  56. return -1;
  57. stern1r = bow1r = from->file->row;
  58. stern1c = bow1c = from->file->col;
  59. stern2r = bow2r = to->file->row;
  60. stern2c = bow2c = to->file->col;
  61. result = bb = distance(bow2r - bow1r, bow2c - bow1c);
  62. if (bb < 5) {
  63. stern2r += dr[to->file->dir];
  64. stern2c += dc[to->file->dir];
  65. stern1r += dr[from->file->dir];
  66. stern1c += dc[from->file->dir];
  67. bs = distance((bow2r - stern1r), (bow2c - stern1c));
  68. sb = distance((bow1r - stern2r), (bow1c - stern2c));
  69. ss = distance((stern2r - stern1r) ,(stern2c - stern1c));
  70. result = min(bb, min(bs, min(sb, ss)));
  71. }
  72. return result;
  73. }
  74. struct ship *
  75. closestenemy(struct ship *from, int side, int anyship)
  76. {
  77. struct ship *sp;
  78. char a;
  79. int olddist = 30000, dist;
  80. struct ship *closest = 0;
  81. a = capship(from)->nationality;
  82. foreachship(sp) {
  83. if (sp == from)
  84. continue;
  85. if (sp->file->dir == 0)
  86. continue;
  87. if (a == capship(sp)->nationality && !anyship)
  88. continue;
  89. if (side && gunsbear(from, sp) != side)
  90. continue;
  91. dist = range(from, sp);
  92. if (dist < olddist) {
  93. closest = sp;
  94. olddist = dist;
  95. }
  96. }
  97. return closest;
  98. }
  99. static int
  100. angle(int dr, int dc)
  101. {
  102. int i;
  103. if (dc >= 0 && dr > 0)
  104. i = 0;
  105. else if (dr <= 0 && dc > 0)
  106. i = 2;
  107. else if (dc <= 0 && dr < 0)
  108. i = 4;
  109. else
  110. i = 6;
  111. dr = abs(dr);
  112. dc = abs(dc);
  113. if ((i == 0 || i == 4) && dc * 2.4 > dr) {
  114. i++;
  115. if (dc > dr * 2.4)
  116. i++;
  117. } else if ((i == 2 || i == 6) && dr * 2.4 > dc) {
  118. i++;
  119. if (dr > dc * 2.4)
  120. i++;
  121. }
  122. return i % 8 + 1;
  123. }
  124. /* checks for target bow or stern */
  125. int
  126. gunsbear(struct ship *from, struct ship *to)
  127. {
  128. int Dr, Dc, i;
  129. int ang;
  130. Dr = from->file->row - to->file->row;
  131. Dc = to->file->col - from->file->col;
  132. for (i = 2; i; i--) {
  133. if ((ang = angle(Dr, Dc) - from->file->dir + 1) < 1)
  134. ang += 8;
  135. if (ang >= 2 && ang <= 4)
  136. return 'r';
  137. if (ang >= 6 && ang <= 7)
  138. return 'l';
  139. Dr += dr[to->file->dir];
  140. Dc += dc[to->file->dir];
  141. }
  142. return 0;
  143. }
  144. /* returns true if fromship is shooting at onship's starboard side */
  145. int
  146. portside(struct ship *from, struct ship *on, int quick)
  147. {
  148. int ang;
  149. int Dr, Dc;
  150. Dr = from->file->row - on->file->row;
  151. Dc = on->file->col - from->file->col;
  152. if (quick == -1) {
  153. Dr += dr[on->file->dir];
  154. Dc += dc[on->file->dir];
  155. }
  156. ang = angle(Dr, Dc);
  157. if (quick != 0)
  158. return ang;
  159. ang = (ang + 4 - on->file->dir - 1) % 8 + 1;
  160. return ang < 5;
  161. }
  162. int
  163. colours(struct ship *sp)
  164. {
  165. char flag = '\0';
  166. if (sp->file->struck)
  167. flag = '!';
  168. if (sp->file->explode)
  169. flag = '#';
  170. if (sp->file->sink)
  171. flag = '~';
  172. if (sp->file->struck)
  173. return flag;
  174. flag = *countryname[capship(sp)->nationality];
  175. return sp->file->FS ? flag : tolower((unsigned char)flag);
  176. }
  177. void
  178. logger(struct ship *s)
  179. {
  180. FILE *fp;
  181. int persons;
  182. int n;
  183. struct logs log[NLOG];
  184. float net;
  185. struct logs *lp;
  186. setegid(egid);
  187. if ((fp = fopen(_PATH_LOGFILE, "r+")) == NULL) {
  188. setegid(gid);
  189. return;
  190. }
  191. setegid(gid);
  192. #ifdef LOCK_EX
  193. if (flock(fileno(fp), LOCK_EX) < 0)
  194. return;
  195. #endif
  196. net = (float)s->file->points / s->specs->pts;
  197. persons = getw(fp);
  198. n = fread((char *)log, sizeof(struct logs), NLOG, fp);
  199. for (lp = &log[n]; lp < &log[NLOG]; lp++)
  200. lp->l_name[0] = lp->l_uid = lp->l_shipnum
  201. = lp->l_gamenum = lp->l_netpoints = 0;
  202. rewind(fp);
  203. if (persons < 0)
  204. putw(1, fp);
  205. else
  206. putw(persons + 1, fp);
  207. for (lp = log; lp < &log[NLOG]; lp++)
  208. if (net > (float)lp->l_netpoints
  209. / scene[lp->l_gamenum].ship[lp->l_shipnum].specs->pts) {
  210. fwrite((char *)log, sizeof (struct logs), lp - log, fp);
  211. strcpy(log[NLOG-1].l_name, s->file->captain);
  212. log[NLOG-1].l_uid = getuid();
  213. log[NLOG-1].l_shipnum = s->file->index;
  214. log[NLOG-1].l_gamenum = game;
  215. log[NLOG-1].l_netpoints = s->file->points;
  216. fwrite((char *)&log[NLOG-1], sizeof (struct logs), 1, fp);
  217. fwrite((char *)lp, sizeof (struct logs), &log[NLOG-1] - lp, fp);
  218. break;
  219. }
  220. #ifdef LOCK_EX
  221. flock(fileno(fp), LOCK_UN);
  222. #endif
  223. fclose(fp);
  224. }