move.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* $NetBSD: move.c,v 1.6 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[] = "@(#)move.c 8.1 (Berkeley) 5/31/93";
  34. #else
  35. __RCSID("$NetBSD: move.c,v 1.6 2003/08/07 09:37:52 agc Exp $");
  36. #endif
  37. #endif /* not lint */
  38. #include <stdio.h>
  39. #include <math.h>
  40. #include "trek.h"
  41. /*
  42. ** Move Under Warp or Impulse Power
  43. **
  44. ** `Ramflag' is set if we are to be allowed to ram stars,
  45. ** Klingons, etc. This is passed from warp(), which gets it from
  46. ** either play() or ram(). Course is the course (0 -> 360) at
  47. ** which we want to move. `Speed' is the speed we
  48. ** want to go, and `time' is the expected time. It
  49. ** can get cut short if a long range tractor beam is to occur. We
  50. ** cut short the move so that the user doesn't get docked time and
  51. ** energy for distance which he didn't travel.
  52. **
  53. ** We check the course through the current quadrant to see that he
  54. ** doesn't run into anything. After that, though, space sort of
  55. ** bends around him. Note that this puts us in the awkward posi-
  56. ** tion of being able to be dropped into a sector which is com-
  57. ** pletely surrounded by stars. Oh Well.
  58. **
  59. ** If the SINS (Space Inertial Navigation System) is out, we ran-
  60. ** domize the course accordingly before ever starting to move.
  61. ** We will still move in a straight line.
  62. **
  63. ** Note that if your computer is out, you ram things anyway. In
  64. ** other words, if your computer and sins are both out, you're in
  65. ** potentially very bad shape.
  66. **
  67. ** Klingons get a chance to zap you as you leave the quadrant.
  68. ** By the way, they also try to follow you (heh heh).
  69. **
  70. ** Return value is the actual amount of time used.
  71. **
  72. **
  73. ** Uses trace flag 4.
  74. */
  75. double move(ramflag, course, time, speed)
  76. int ramflag;
  77. int course;
  78. double time;
  79. double speed;
  80. {
  81. double angle;
  82. double x, y, dx, dy;
  83. int ix = 0, iy = 0;
  84. double bigger;
  85. int n;
  86. int i;
  87. double dist;
  88. double sectsize;
  89. double xn;
  90. double evtime;
  91. # ifdef xTRACE
  92. if (Trace)
  93. printf("move: ramflag %d course %d time %.2f speed %.2f\n",
  94. ramflag, course, time, speed);
  95. # endif
  96. sectsize = NSECTS;
  97. /* initialize delta factors for move */
  98. angle = course * 0.0174532925;
  99. if (damaged(SINS))
  100. angle += Param.navigcrud[1] * (franf() - 0.5);
  101. else
  102. if (Ship.sinsbad)
  103. angle += Param.navigcrud[0] * (franf() - 0.5);
  104. dx = -cos(angle);
  105. dy = sin(angle);
  106. bigger = fabs(dx);
  107. dist = fabs(dy);
  108. if (dist > bigger)
  109. bigger = dist;
  110. dx /= bigger;
  111. dy /= bigger;
  112. /* check for long range tractor beams */
  113. /**** TEMPORARY CODE == DEBUGGING ****/
  114. evtime = Now.eventptr[E_LRTB]->date - Now.date;
  115. # ifdef xTRACE
  116. if (Trace)
  117. printf("E.ep = %p, ->evcode = %d, ->date = %.2f, evtime = %.2f\n",
  118. Now.eventptr[E_LRTB], Now.eventptr[E_LRTB]->evcode,
  119. Now.eventptr[E_LRTB]->date, evtime);
  120. # endif
  121. if (time > evtime && Etc.nkling < 3)
  122. {
  123. /* then we got a LRTB */
  124. evtime += 0.005;
  125. time = evtime;
  126. }
  127. else
  128. evtime = -1.0e50;
  129. dist = time * speed;
  130. /* move within quadrant */
  131. Sect[Ship.sectx][Ship.secty] = EMPTY;
  132. x = Ship.sectx + 0.5;
  133. y = Ship.secty + 0.5;
  134. xn = NSECTS * dist * bigger;
  135. n = xn + 0.5;
  136. # ifdef xTRACE
  137. if (Trace)
  138. printf("dx = %.2f, dy = %.2f, xn = %.2f, n = %d\n", dx, dy, xn, n);
  139. # endif
  140. Move.free = 0;
  141. for (i = 0; i < n; i++)
  142. {
  143. ix = (x += dx);
  144. iy = (y += dy);
  145. # ifdef xTRACE
  146. if (Trace)
  147. printf("ix = %d, x = %.2f, iy = %d, y = %.2f\n", ix, x, iy, y);
  148. # endif
  149. if (x < 0.0 || y < 0.0 || x >= sectsize || y >= sectsize)
  150. {
  151. /* enter new quadrant */
  152. dx = Ship.quadx * NSECTS + Ship.sectx + dx * xn;
  153. dy = Ship.quady * NSECTS + Ship.secty + dy * xn;
  154. if (dx < 0.0)
  155. ix = -1;
  156. else
  157. ix = dx + 0.5;
  158. if (dy < 0.0)
  159. iy = -1;
  160. else
  161. iy = dy + 0.5;
  162. # ifdef xTRACE
  163. if (Trace)
  164. printf("New quad: ix = %d, iy = %d\n", ix, iy);
  165. # endif
  166. Ship.sectx = x;
  167. Ship.secty = y;
  168. compkldist(0);
  169. Move.newquad = 2;
  170. attack(0);
  171. checkcond();
  172. Ship.quadx = ix / NSECTS;
  173. Ship.quady = iy / NSECTS;
  174. Ship.sectx = ix % NSECTS;
  175. Ship.secty = iy % NSECTS;
  176. if (ix < 0 || Ship.quadx >= NQUADS || iy < 0 ||
  177. Ship.quady >= NQUADS) {
  178. if (!damaged(COMPUTER)) {
  179. dumpme(0);
  180. } else
  181. lose(L_NEGENB);
  182. }
  183. initquad(0);
  184. n = 0;
  185. break;
  186. }
  187. if (Sect[ix][iy] != EMPTY)
  188. {
  189. /* we just hit something */
  190. if (!damaged(COMPUTER) && ramflag <= 0)
  191. {
  192. ix = x - dx;
  193. iy = y - dy;
  194. printf("Computer reports navigation error; %s stopped at %d,%d\n",
  195. Ship.shipname, ix, iy);
  196. Ship.energy -= Param.stopengy * speed;
  197. break;
  198. }
  199. /* test for a black hole */
  200. if (Sect[ix][iy] == HOLE)
  201. {
  202. /* get dumped elsewhere in the galaxy */
  203. dumpme(1);
  204. initquad(0);
  205. n = 0;
  206. break;
  207. }
  208. ram(ix, iy);
  209. break;
  210. }
  211. }
  212. if (n > 0)
  213. {
  214. dx = Ship.sectx - ix;
  215. dy = Ship.secty - iy;
  216. dist = sqrt(dx * dx + dy * dy) / NSECTS;
  217. time = dist / speed;
  218. if (evtime > time)
  219. time = evtime; /* spring the LRTB trap */
  220. Ship.sectx = ix;
  221. Ship.secty = iy;
  222. }
  223. Sect[Ship.sectx][Ship.secty] = Ship.ship;
  224. compkldist(0);
  225. return (time);
  226. }