1 /* $OpenBSD: help.c,v 1.3 1999/07/31 18:48:59 pjanzen Exp $ */ 2 /* $NetBSD: help.c,v 1.3 1995/04/22 10:59:01 cgd Exp $ */ 3 4 /* 5 * Copyright (c) 1980, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #ifndef lint 38 #if 0 39 static char sccsid[] = "@(#)help.c 8.1 (Berkeley) 5/31/93"; 40 #else 41 static char rcsid[] = "$OpenBSD: help.c,v 1.3 1999/07/31 18:48:59 pjanzen Exp $"; 42 #endif 43 #endif /* not lint */ 44 45 #include <stdio.h> 46 #include <math.h> 47 #include <unistd.h> 48 #include "trek.h" 49 50 /* 51 ** call starbase for help 52 ** 53 ** First, the closest starbase is selected. If there is a 54 ** a starbase in your own quadrant, you are in good shape. 55 ** This distance takes quadrant distances into account only. 56 ** 57 ** A magic number is computed based on the distance which acts 58 ** as the probability that you will be rematerialized. You 59 ** get three tries. 60 ** 61 ** When it is determined that you should be able to be remater- 62 ** ialized (i.e., when the probability thing mentioned above 63 ** comes up positive), you are put into that quadrant (anywhere). 64 ** Then, we try to see if there is a spot adjacent to the star- 65 ** base. If not, you can't be rematerialized!!! Otherwise, 66 ** it drops you there. It only tries five times to find a spot 67 ** to drop you. After that, it's your problem. 68 */ 69 70 const char *const Cntvect[3] = 71 {"first", "second", "third"}; 72 73 void 74 help(v) 75 int v; 76 { 77 register int i; 78 double dist, x; 79 register int dx = 0, dy = 0; 80 int j, l = 0; 81 82 /* check to see if calling for help is reasonable ... */ 83 if (Ship.cond == DOCKED) 84 { 85 printf("Uhura: But Captain, we're already docked\n"); 86 return; 87 } 88 89 /* or possible */ 90 if (damaged(SSRADIO)) 91 { 92 out(SSRADIO); 93 return; 94 } 95 if (Now.bases <= 0) 96 { 97 printf("Uhura: I'm not getting any response from starbase\n"); 98 return; 99 } 100 101 /* tut tut, there goes the score */ 102 Game.helps += 1; 103 104 /* find the closest base */ 105 dist = 1e50; 106 if (Quad[Ship.quadx][Ship.quady].bases <= 0) 107 { 108 /* there isn't one in this quadrant */ 109 for (i = 0; i < Now.bases; i++) 110 { 111 /* compute distance */ 112 dx = Now.base[i].x - Ship.quadx; 113 dy = Now.base[i].y - Ship.quady; 114 x = dx * dx + dy * dy; 115 x = sqrt(x); 116 117 /* see if better than what we already have */ 118 if (x < dist) 119 { 120 dist = x; 121 l = i; 122 } 123 } 124 125 /* go to that quadrant */ 126 Ship.quadx = Now.base[l].x; 127 Ship.quady = Now.base[l].y; 128 initquad(1); 129 } 130 else 131 { 132 dist = 0.0; 133 } 134 135 /* dematerialize the Enterprise */ 136 Sect[Ship.sectx][Ship.secty] = EMPTY; 137 printf("Starbase in %d,%d responds\n", Ship.quadx, Ship.quady); 138 139 /* this next thing acts as a probability that it will work */ 140 x = pow(1.0 - pow(0.94, dist), 0.3333333); 141 142 /* attempt to rematerialize */ 143 for (i = 0; i < 3; i++) 144 { 145 sleep(2); 146 printf("%s attempt to rematerialize ", Cntvect[i]); 147 if (franf() > x) 148 { 149 /* ok, that's good. let's see if we can set her down */ 150 for (j = 0; j < 5; j++) 151 { 152 dx = Etc.starbase.x + ranf(3) - 1; 153 if (dx < 0 || dx >= NSECTS) 154 continue; 155 dy = Etc.starbase.y + ranf(3) - 1; 156 if (dy < 0 || dy >= NSECTS || Sect[dx][dy] != EMPTY) 157 continue; 158 break; 159 } 160 if (j < 5) 161 { 162 /* found an empty spot */ 163 printf("succeeds\n"); 164 Ship.sectx = dx; 165 Ship.secty = dy; 166 Sect[dx][dy] = Ship.ship; 167 dock(0); 168 compkldist(0); 169 return; 170 } 171 /* the starbase must have been surrounded */ 172 } 173 printf("fails\n"); 174 } 175 176 /* one, two, three strikes, you're out */ 177 lose(L_NOHELP); 178 } 179