help.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* $NetBSD: help.c,v 1.7 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[] = "@(#)help.c 8.1 (Berkeley) 5/31/93";
  34. #else
  35. __RCSID("$NetBSD: help.c,v 1.7 2003/08/07 09:37:52 agc Exp $");
  36. #endif
  37. #endif /* not lint */
  38. #include <stdio.h>
  39. #include <math.h>
  40. #include <unistd.h>
  41. #include "trek.h"
  42. /*
  43. ** call starbase for help
  44. **
  45. ** First, the closest starbase is selected. If there is a
  46. ** a starbase in your own quadrant, you are in good shape.
  47. ** This distance takes quadrant distances into account only.
  48. **
  49. ** A magic number is computed based on the distance which acts
  50. ** as the probability that you will be rematerialized. You
  51. ** get three tries.
  52. **
  53. ** When it is determined that you should be able to be remater-
  54. ** ialized (i.e., when the probability thing mentioned above
  55. ** comes up positive), you are put into that quadrant (anywhere).
  56. ** Then, we try to see if there is a spot adjacent to the star-
  57. ** base. If not, you can't be rematerialized!!! Otherwise,
  58. ** it drops you there. It only tries five times to find a spot
  59. ** to drop you. After that, it's your problem.
  60. */
  61. const char *const Cntvect[3] =
  62. {"first", "second", "third"};
  63. /*ARGSUSED*/
  64. void
  65. help(v)
  66. int v __attribute__((__unused__));
  67. {
  68. int i;
  69. double dist, x;
  70. int dx = 0, dy = 0;
  71. int j, l = 0;
  72. /* check to see if calling for help is reasonable ... */
  73. if (Ship.cond == DOCKED) {
  74. printf("Uhura: But Captain, we're already docked\n");
  75. return;
  76. }
  77. /* or possible */
  78. if (damaged(SSRADIO)) {
  79. out(SSRADIO);
  80. return;
  81. }
  82. if (Now.bases <= 0) {
  83. printf("Uhura: I'm not getting any response from starbase\n");
  84. return;
  85. }
  86. /* tut tut, there goes the score */
  87. Game.helps += 1;
  88. /* find the closest base */
  89. dist = 1e50;
  90. if (Quad[Ship.quadx][Ship.quady].bases <= 0)
  91. {
  92. /* there isn't one in this quadrant */
  93. for (i = 0; i < Now.bases; i++)
  94. {
  95. /* compute distance */
  96. dx = Now.base[i].x - Ship.quadx;
  97. dy = Now.base[i].y - Ship.quady;
  98. x = dx * dx + dy * dy;
  99. x = sqrt(x);
  100. /* see if better than what we already have */
  101. if (x < dist)
  102. {
  103. dist = x;
  104. l = i;
  105. }
  106. }
  107. /* go to that quadrant */
  108. Ship.quadx = Now.base[l].x;
  109. Ship.quady = Now.base[l].y;
  110. initquad(1);
  111. }
  112. else
  113. {
  114. dist = 0.0;
  115. }
  116. /* dematerialize the Enterprise */
  117. Sect[Ship.sectx][Ship.secty] = EMPTY;
  118. printf("Starbase in %d,%d responds\n", Ship.quadx, Ship.quady);
  119. /* this next thing acts as a probability that it will work */
  120. x = pow(1.0 - pow(0.94, dist), 0.3333333);
  121. /* attempt to rematerialize */
  122. for (i = 0; i < 3; i++)
  123. {
  124. sleep(2);
  125. printf("%s attempt to rematerialize ", Cntvect[i]);
  126. if (franf() > x)
  127. {
  128. /* ok, that's good. let's see if we can set her down */
  129. for (j = 0; j < 5; j++)
  130. {
  131. dx = Etc.starbase.x + ranf(3) - 1;
  132. if (dx < 0 || dx >= NSECTS)
  133. continue;
  134. dy = Etc.starbase.y + ranf(3) - 1;
  135. if (dy < 0 || dy >= NSECTS || Sect[dx][dy] != EMPTY)
  136. continue;
  137. break;
  138. }
  139. if (j < 5)
  140. {
  141. /* found an empty spot */
  142. printf("succeeds\n");
  143. Ship.sectx = dx;
  144. Ship.secty = dy;
  145. Sect[dx][dy] = Ship.ship;
  146. dock(0);
  147. compkldist(0);
  148. return;
  149. }
  150. /* the starbase must have been surrounded */
  151. }
  152. printf("fails\n");
  153. }
  154. /* one, two, three strikes, you're out */
  155. lose(L_NOHELP);
  156. }