klmove.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* $NetBSD: klmove.c,v 1.5 2003/08/07 09:37:52 agc Exp $ */
  2. /*
  3. * Copyright (c) 1980, 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[] = "@(#)klmove.c 8.1 (Berkeley) 5/31/93";
  34. #else
  35. __RCSID("$NetBSD: klmove.c,v 1.5 2003/08/07 09:37:52 agc Exp $");
  36. #endif
  37. #endif /* not lint */
  38. #include <stdio.h>
  39. #include "trek.h"
  40. /*
  41. ** Move Klingons Around
  42. **
  43. ** This is a largely incomprehensible block of code that moves
  44. ** Klingons around in a quadrant. It was written in a very
  45. ** "program as you go" fashion, and is a prime candidate for
  46. ** rewriting.
  47. **
  48. ** The flag `fl' is zero before an attack, one after an attack,
  49. ** and two if you are leaving a quadrant. This serves to
  50. ** change the probability and distance that it moves.
  51. **
  52. ** Basically, what it will try to do is to move a certain number
  53. ** of steps either toward you or away from you. It will avoid
  54. ** stars whenever possible. Nextx and nexty are the next
  55. ** sector to move to on a per-Klingon basis; they are roughly
  56. ** equivalent to Ship.sectx and Ship.secty for the starship. Lookx and
  57. ** looky are the sector that you are going to look at to see
  58. ** if you can move their. Dx and dy are the increment. Fudgex
  59. ** and fudgey are the things you change around to change your
  60. ** course around stars.
  61. */
  62. void
  63. klmove(fl)
  64. int fl;
  65. {
  66. int n;
  67. struct kling *k;
  68. double dx, dy;
  69. int nextx, nexty;
  70. int lookx, looky;
  71. int motion;
  72. int fudgex, fudgey;
  73. int qx, qy;
  74. double bigger;
  75. int i;
  76. # ifdef xTRACE
  77. if (Trace)
  78. printf("klmove: fl = %d, Etc.nkling = %d\n", fl, Etc.nkling);
  79. # endif
  80. for (n = 0; n < Etc.nkling; n++)
  81. {
  82. k = &Etc.klingon[n];
  83. i = 100;
  84. if (fl)
  85. i = 100.0 * k->power / Param.klingpwr;
  86. if (ranf(i) >= Param.moveprob[2 * Move.newquad + fl])
  87. continue;
  88. /* compute distance to move */
  89. motion = ranf(75) - 25;
  90. motion *= k->avgdist * Param.movefac[2 * Move.newquad + fl];
  91. /* compute direction */
  92. dx = Ship.sectx - k->x + ranf(3) - 1;
  93. dy = Ship.secty - k->y + ranf(3) - 1;
  94. bigger = dx;
  95. if (dy > bigger)
  96. bigger = dy;
  97. if (bigger == 0.0)
  98. bigger = 1.0;
  99. dx = dx / bigger + 0.5;
  100. dy = dy / bigger + 0.5;
  101. if (motion < 0)
  102. {
  103. motion = -motion;
  104. dx = -dx;
  105. dy = -dy;
  106. }
  107. fudgex = fudgey = 1;
  108. /* try to move the klingon */
  109. nextx = k->x;
  110. nexty = k->y;
  111. for (; motion > 0; motion--)
  112. {
  113. lookx = nextx + dx;
  114. looky = nexty + dy;
  115. if (lookx < 0 || lookx >= NSECTS || looky < 0 || looky >= NSECTS)
  116. {
  117. /* new quadrant */
  118. qx = Ship.quadx;
  119. qy = Ship.quady;
  120. if (lookx < 0)
  121. qx -= 1;
  122. else
  123. if (lookx >= NSECTS)
  124. qx += 1;
  125. if (looky < 0)
  126. qy -= 1;
  127. else
  128. if (looky >= NSECTS)
  129. qy += 1;
  130. if (qx < 0 || qx >= NQUADS || qy < 0 || qy >= NQUADS ||
  131. Quad[qx][qy].stars < 0 || Quad[qx][qy].klings > MAXKLQUAD - 1)
  132. break;
  133. if (!damaged(SRSCAN))
  134. {
  135. printf("Klingon at %d,%d escapes to quadrant %d,%d\n",
  136. k->x, k->y, qx, qy);
  137. motion = Quad[qx][qy].scanned;
  138. if (motion >= 0 && motion < 1000)
  139. Quad[qx][qy].scanned += 100;
  140. motion = Quad[Ship.quadx][Ship.quady].scanned;
  141. if (motion >= 0 && motion < 1000)
  142. Quad[Ship.quadx][Ship.quady].scanned -= 100;
  143. }
  144. Sect[k->x][k->y] = EMPTY;
  145. Quad[qx][qy].klings += 1;
  146. Etc.nkling -= 1;
  147. *k = Etc.klingon[Etc.nkling];
  148. Quad[Ship.quadx][Ship.quady].klings -= 1;
  149. k = 0;
  150. break;
  151. }
  152. if (Sect[lookx][looky] != EMPTY)
  153. {
  154. lookx = nextx + fudgex;
  155. if (lookx < 0 || lookx >= NSECTS)
  156. lookx = nextx + dx;
  157. if (Sect[lookx][looky] != EMPTY)
  158. {
  159. fudgex = -fudgex;
  160. looky = nexty + fudgey;
  161. if (looky < 0 || looky >= NSECTS || Sect[lookx][looky] != EMPTY)
  162. {
  163. fudgey = -fudgey;
  164. break;
  165. }
  166. }
  167. }
  168. nextx = lookx;
  169. nexty = looky;
  170. }
  171. if (k && (k->x != nextx || k->y != nexty))
  172. {
  173. if (!damaged(SRSCAN))
  174. printf("Klingon at %d,%d moves to %d,%d\n",
  175. k->x, k->y, nextx, nexty);
  176. Sect[k->x][k->y] = EMPTY;
  177. Sect[k->x = nextx][k->y = nexty] = KLINGON;
  178. }
  179. }
  180. compkldist(0);
  181. }