|
|
1.1 ! root 1: /* ! 2: * Copyright (c) 1980 Regents of the University of California. ! 3: * All rights reserved. ! 4: * ! 5: * Redistribution and use in source and binary forms are permitted provided ! 6: * that: (1) source distributions retain this entire copyright notice and ! 7: * comment, and (2) distributions including binaries display the following ! 8: * acknowledgement: ``This product includes software developed by the ! 9: * University of California, Berkeley and its contributors'' in the ! 10: * documentation or other materials provided with the distribution and in ! 11: * all advertising materials mentioning features or use of this software. ! 12: * Neither the name of the University nor the names of its contributors may ! 13: * be used to endorse or promote products derived from this software without ! 14: * specific prior written permission. ! 15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED ! 16: * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF ! 17: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ! 18: */ ! 19: ! 20: #ifndef lint ! 21: static char sccsid[] = "@(#)computer.c 4.8 (Berkeley) 6/1/90"; ! 22: #endif /* not lint */ ! 23: ! 24: # include "trek.h" ! 25: # include "getpar.h" ! 26: # include <stdio.h> ! 27: /* ! 28: ** On-Board Computer ! 29: ** ! 30: ** A computer request is fetched from the captain. The requests ! 31: ** are: ! 32: ** ! 33: ** chart -- print a star chart of the known galaxy. This includes ! 34: ** every quadrant that has ever had a long range or ! 35: ** a short range scan done of it, plus the location of ! 36: ** all starbases. This is of course updated by any sub- ! 37: ** space radio broadcasts (unless the radio is out). ! 38: ** The format is the same as that of a long range scan ! 39: ** except that ".1." indicates that a starbase exists ! 40: ** but we know nothing else. ! 41: ** ! 42: ** trajectory -- gives the course and distance to every know ! 43: ** Klingon in the quadrant. Obviously this fails if the ! 44: ** short range scanners are out. ! 45: ** ! 46: ** course -- gives a course computation from whereever you are ! 47: ** to any specified location. If the course begins ! 48: ** with a slash, the current quadrant is taken. ! 49: ** Otherwise the input is quadrant and sector coordi- ! 50: ** nates of the target sector. ! 51: ** ! 52: ** move -- identical to course, except that the move is performed. ! 53: ** ! 54: ** score -- prints out the current score. ! 55: ** ! 56: ** pheff -- "PHaser EFFectiveness" at a given distance. Tells ! 57: ** you how much stuff you need to make it work. ! 58: ** ! 59: ** warpcost -- Gives you the cost in time and units to move for ! 60: ** a given distance under a given warp speed. ! 61: ** ! 62: ** impcost -- Same for the impulse engines. ! 63: ** ! 64: ** distresslist -- Gives a list of the currently known starsystems ! 65: ** or starbases which are distressed, together with their ! 66: ** quadrant coordinates. ! 67: ** ! 68: ** If a command is terminated with a semicolon, you remain in ! 69: ** the computer; otherwise, you escape immediately to the main ! 70: ** command processor. ! 71: */ ! 72: ! 73: struct cvntab Cputab[] = ! 74: { ! 75: "ch", "art", (int (*)())1, 0, ! 76: "t", "rajectory", (int (*)())2, 0, ! 77: "c", "ourse", (int (*)())3, 0, ! 78: "m", "ove", (int (*)())3, 1, ! 79: "s", "core", (int (*)())4, 0, ! 80: "p", "heff", (int (*)())5, 0, ! 81: "w", "arpcost", (int (*)())6, 0, ! 82: "i", "mpcost", (int (*)())7, 0, ! 83: "d", "istresslist", (int (*)())8, 0, ! 84: 0 ! 85: }; ! 86: ! 87: computer() ! 88: { ! 89: int ix, iy; ! 90: register int i, j; ! 91: int numout; ! 92: int tqx, tqy; ! 93: struct cvntab *r; ! 94: int cost; ! 95: int course; ! 96: double dist, time; ! 97: double warpfact; ! 98: struct quad *q; ! 99: register struct event *e; ! 100: ! 101: if (check_out(COMPUTER)) ! 102: return; ! 103: while (1) ! 104: { ! 105: r = getcodpar("\nRequest", Cputab); ! 106: switch ((int)r->value) ! 107: { ! 108: ! 109: case 1: /* star chart */ ! 110: printf("Computer record of galaxy for all long range sensor scans\n\n"); ! 111: printf(" "); ! 112: /* print top header */ ! 113: for (i = 0; i < NQUADS; i++) ! 114: printf("-%d- ", i); ! 115: printf("\n"); ! 116: for (i = 0; i < NQUADS; i++) ! 117: { ! 118: printf("%d ", i); ! 119: for (j = 0; j < NQUADS; j++) ! 120: { ! 121: if (i == Ship.quadx && j == Ship.quady) ! 122: { ! 123: printf("$$$ "); ! 124: continue; ! 125: } ! 126: q = &Quad[i][j]; ! 127: /* 1000 or 1001 is special case */ ! 128: if (q->scanned >= 1000) ! 129: if (q->scanned > 1000) ! 130: printf(".1. "); ! 131: else ! 132: printf("/// "); ! 133: else ! 134: if (q->scanned < 0) ! 135: printf("... "); ! 136: else ! 137: printf("%3d ", q->scanned); ! 138: } ! 139: printf("%d\n", i); ! 140: } ! 141: printf(" "); ! 142: /* print bottom footer */ ! 143: for (i = 0; i < NQUADS; i++) ! 144: printf("-%d- ", i); ! 145: printf("\n"); ! 146: break; ! 147: ! 148: case 2: /* trajectory */ ! 149: if (check_out(SRSCAN)) ! 150: { ! 151: break; ! 152: } ! 153: if (Etc.nkling <= 0) ! 154: { ! 155: printf("No Klingons in this quadrant\n"); ! 156: break; ! 157: } ! 158: /* for each Klingon, give the course & distance */ ! 159: for (i = 0; i < Etc.nkling; i++) ! 160: { ! 161: printf("Klingon at %d,%d", Etc.klingon[i].x, Etc.klingon[i].y); ! 162: course = kalc(Ship.quadx, Ship.quady, Etc.klingon[i].x, Etc.klingon[i].y, &dist); ! 163: prkalc(course, dist); ! 164: } ! 165: break; ! 166: ! 167: case 3: /* course calculation */ ! 168: if (readdelim('/')) ! 169: { ! 170: tqx = Ship.quadx; ! 171: tqy = Ship.quady; ! 172: } ! 173: else ! 174: { ! 175: ix = getintpar("Quadrant"); ! 176: if (ix < 0 || ix >= NSECTS) ! 177: break; ! 178: iy = getintpar("q-y"); ! 179: if (iy < 0 || iy >= NSECTS) ! 180: break; ! 181: tqx = ix; ! 182: tqy = iy; ! 183: } ! 184: ix = getintpar("Sector"); ! 185: if (ix < 0 || ix >= NSECTS) ! 186: break; ! 187: iy = getintpar("s-y"); ! 188: if (iy < 0 || iy >= NSECTS) ! 189: break; ! 190: course = kalc(tqx, tqy, ix, iy, &dist); ! 191: if (r->value2) ! 192: { ! 193: warp(-1, course, dist); ! 194: break; ! 195: } ! 196: printf("%d,%d/%d,%d to %d,%d/%d,%d", ! 197: Ship.quadx, Ship.quady, Ship.sectx, Ship.secty, tqx, tqy, ix, iy); ! 198: prkalc(course, dist); ! 199: break; ! 200: ! 201: case 4: /* score */ ! 202: score(); ! 203: break; ! 204: ! 205: case 5: /* phaser effectiveness */ ! 206: dist = getfltpar("range"); ! 207: if (dist < 0.0) ! 208: break; ! 209: dist *= 10.0; ! 210: cost = pow(0.90, dist) * 98.0 + 0.5; ! 211: printf("Phasers are %d%% effective at that range\n", cost); ! 212: break; ! 213: ! 214: case 6: /* warp cost (time/energy) */ ! 215: dist = getfltpar("distance"); ! 216: if (dist < 0.0) ! 217: break; ! 218: warpfact = getfltpar("warp factor"); ! 219: if (warpfact <= 0.0) ! 220: warpfact = Ship.warp; ! 221: cost = (dist + 0.05) * warpfact * warpfact * warpfact; ! 222: time = Param.warptime * dist / (warpfact * warpfact); ! 223: printf("Warp %.2f distance %.2f cost %.2f stardates %d (%d w/ shlds up) units\n", ! 224: warpfact, dist, time, cost, cost + cost); ! 225: break; ! 226: ! 227: case 7: /* impulse cost */ ! 228: dist = getfltpar("distance"); ! 229: if (dist < 0.0) ! 230: break; ! 231: cost = 20 + 100 * dist; ! 232: time = dist / 0.095; ! 233: printf("Distance %.2f cost %.2f stardates %d units\n", ! 234: dist, time, cost); ! 235: break; ! 236: ! 237: case 8: /* distresslist */ ! 238: j = 1; ! 239: printf("\n"); ! 240: /* scan the event list */ ! 241: for (i = 0; i < MAXEVENTS; i++) ! 242: { ! 243: e = &Event[i]; ! 244: /* ignore hidden entries */ ! 245: if (e->evcode & E_HIDDEN) ! 246: continue; ! 247: switch (e->evcode & E_EVENT) ! 248: { ! 249: ! 250: case E_KDESB: ! 251: printf("Klingon is attacking starbase in quadrant %d,%d\n", ! 252: e->x, e->y); ! 253: j = 0; ! 254: break; ! 255: ! 256: case E_ENSLV: ! 257: case E_REPRO: ! 258: printf("Starsystem %s in quadrant %d,%d is distressed\n", ! 259: Systemname[e->systemname], e->x, e->y); ! 260: j = 0; ! 261: break; ! 262: } ! 263: } ! 264: if (j) ! 265: printf("No known distress calls are active\n"); ! 266: break; ! 267: ! 268: } ! 269: ! 270: /* skip to next semicolon or newline. Semicolon ! 271: * means get new computer request; newline means ! 272: * exit computer mode. */ ! 273: while ((i = cgetc(0)) != ';') ! 274: { ! 275: if (i == '\0') ! 276: exit(1); ! 277: if (i == '\n') ! 278: { ! 279: ungetc(i, stdin); ! 280: return; ! 281: } ! 282: } ! 283: } ! 284: } ! 285: ! 286: ! 287: /* ! 288: ** Course Calculation ! 289: ** ! 290: ** Computes and outputs the course and distance from position ! 291: ** sqx,sqy/ssx,ssy to tqx,tqy/tsx,tsy. ! 292: */ ! 293: ! 294: kalc(tqx, tqy, tsx, tsy, dist) ! 295: int tqx; ! 296: int tqy; ! 297: int tsx; ! 298: int tsy; ! 299: double *dist; ! 300: { ! 301: double dx, dy; ! 302: double quadsize; ! 303: double angle; ! 304: register int course; ! 305: ! 306: /* normalize to quadrant distances */ ! 307: quadsize = NSECTS; ! 308: dx = (Ship.quadx + Ship.sectx / quadsize) - (tqx + tsx / quadsize); ! 309: dy = (tqy + tsy / quadsize) - (Ship.quady + Ship.secty / quadsize); ! 310: ! 311: /* get the angle */ ! 312: angle = atan2(dy, dx); ! 313: /* make it 0 -> 2 pi */ ! 314: if (angle < 0.0) ! 315: angle += 6.283185307; ! 316: /* convert from radians to degrees */ ! 317: course = angle * 57.29577951 + 0.5; ! 318: dx = dx * dx + dy * dy; ! 319: *dist = sqrt(dx); ! 320: return (course); ! 321: } ! 322: ! 323: ! 324: prkalc(course, dist) ! 325: int course; ! 326: double dist; ! 327: { ! 328: printf(": course %d dist %.3f\n", course, dist); ! 329: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.