win() { long s; extern long score(); extern struct cvntab Skitab[]; register struct cvntab *p; sleep(1); printf("\nCongratulations, you have saved the Federation\n"); Move.endgame = 1; /* print and return the score */ s = score(); /* decide if she gets a promotion */ if (Game.helps == 0 && Game.killb == 0 && Game.killinhab == 0 && 5 * Game.kills + Game.deaths < 100 && s >= 1000 && Ship.ship == ENTERPRISE) { printf("In fact, you are promoted one step in rank,\n"); if (Game.skill >= 6) printf("to the exalted rank of Commodore Emeritus\n"); else { p = &Skitab[Game.skill - 1]; printf("from %s%s ", p->abrev, p->full); p++; printf("to %s%s\n", p->abrev, p->full); } } /* clean out input, and request new game */ skiptonl(0); reset(); }
void play() { while (1) { Move.free = 1; Move.time = 0.0; Move.shldchg = 0; Move.newquad = 0; Move.resting = 0; skiptonl(0); const struct cvntab* r = getcodpar("\nCommand", Comtab); (*r->value)(r->value2); events(0); attack(0); checkcond(); } }
void lose(int why) { Game.killed = 1; sleep(1); printf("\n%s\n", Losemsg[why - 1]); switch (why) { case L_NOTIME: Game.killed = 0; break; } Move.endgame = -1; score(); skiptonl(0); longjmp(env, 1); }
double getfltpar(const char *s) { int i; double d; while (1) { if (testnl() && s) printf("%s: ", s); i = scanf("%lf", &d); if (i < 0) exit(1); if (i > 0 && testterm()) return (d); printf("invalid input; please enter a double\n"); skiptonl(0); } }
int getintpar(const char *s) { int i; int n; while (1) { if (testnl() && s) printf("%s: ", s); i = scanf("%d", &n); if (i < 0) exit(1); if (i > 0 && testterm()) return (n); printf("invalid input; please enter an integer\n"); skiptonl(0); } }
phaser() { register int i; int j; register struct kling *k; double dx, dy; double anglefactor, distfactor; register struct banks *b; int manual, flag, extra; int hit; double tot; int n; int hitreqd[NBANKS]; struct banks bank[NBANKS]; struct cvntab *ptr; if (Ship.cond == DOCKED) return(printf("Phasers cannot fire through starbase shields\n")); if (damaged(PHASER)) return (out(PHASER)); if (Ship.shldup) return (printf("Sulu: Captain, we cannot fire through shields.\n")); if (Ship.cloaked) { printf("Sulu: Captain, surely you must realize that we cannot fire\n"); printf(" phasers with the cloaking device up.\n"); return; } /* decide if we want manual or automatic mode */ manual = 0; if (testnl()) { if (damaged(COMPUTER)) { printf(Device[COMPUTER].name); manual++; } else if (damaged(SRSCAN)) { printf(Device[SRSCAN].name); manual++; } if (manual) printf(" damaged, manual mode selected\n"); } if (!manual) { ptr = getcodpar("Manual or automatic", Matab); manual = (int) ptr->value; } if (!manual && damaged(COMPUTER)) { printf("Computer damaged, manual selected\n"); skiptonl(0); manual++; } /* initialize the bank[] array */ flag = 1; for (i = 0; i < NBANKS; i++) bank[i].units = 0; if (manual) { /* collect manual mode statistics */ while (flag) { printf("%d units available\n", Ship.energy); extra = 0; flag = 0; for (i = 0; i < NBANKS; i++) { b = &bank[i]; printf("\nBank %d:\n", i); hit = getintpar("units"); if (hit < 0) return; if (hit == 0) break; extra += hit; if (extra > Ship.energy) { printf("available energy exceeded. "); skiptonl(0); flag++; break; } b->units = hit; hit = getintpar("course"); if (hit < 0 || hit > 360) return; b->angle = hit * 0.0174532925; b->spread = getfltpar("spread"); if (b->spread < 0 || b->spread > 1) return; } Ship.energy -= extra; } extra = 0; } else { /* automatic distribution of power */ if (Etc.nkling <= 0) return (printf("Sulu: But there are no Klingons in this quadrant\n")); printf("Phasers locked on target. "); while (flag) { printf("%d units available\n", Ship.energy); hit = getintpar("Units to fire"); if (hit <= 0) return; if (hit > Ship.energy) { printf("available energy exceeded. "); skiptonl(0); continue; } flag = 0; Ship.energy -= hit; extra = hit; n = Etc.nkling; if (n > NBANKS) n = NBANKS; tot = n * (n + 1) / 2; for (i = 0; i < n; i++) { k = &Etc.klingon[i]; b = &bank[i]; distfactor = k->dist; anglefactor = ALPHA * BETA * OMEGA / (distfactor * distfactor + EPSILON); anglefactor *= GAMMA; distfactor = k->power; distfactor /= anglefactor; hitreqd[i] = distfactor + 0.5; dx = Ship.sectx - k->x; dy = k->y - Ship.secty; b->angle = atan2(dy, dx); b->spread = 0.0; b->units = ((n - i) / tot) * extra; # ifdef xTRACE if (Trace) { printf("b%d hr%d u%d df%.2f af%.2f\n", i, hitreqd[i], b->units, distfactor, anglefactor); } # endif extra -= b->units; hit = b->units - hitreqd[i]; if (hit > 0) { extra += hit; b->units -= hit; } } /* give out any extra energy we might have around */ if (extra > 0) { for (i = 0; i < n; i++) { b = &bank[i]; hit = hitreqd[i] - b->units; if (hit <= 0) continue; if (hit >= extra) { b->units += extra; extra = 0; break; } b->units = hitreqd[i]; extra -= hit; } if (extra > 0) printf("%d units overkill\n", extra); } } } # ifdef xTRACE if (Trace) { for (i = 0; i < NBANKS; i++) { b = &bank[i]; printf("b%d u%d", i, b->units); if (b->units > 0) printf(" a%.2f s%.2f\n", b->angle, b->spread); else printf("\n"); } } # endif /* actually fire the shots */ Move.free = 0; for (i = 0; i < NBANKS; i++) { b = &bank[i]; if (b->units <= 0) { continue; } printf("\nPhaser bank %d fires:\n", i); n = Etc.nkling; k = Etc.klingon; for (j = 0; j < n; j++) { if (b->units <= 0) break; /* ** The formula for hit is as follows: ** ** zap = OMEGA * [(sigma + ALPHA) * (rho + BETA)] ** / (dist ** 2 + EPSILON)] ** * [cos(delta * sigma) + GAMMA] ** * hit ** ** where sigma is the spread factor, ** rho is a random number (0 -> 1), ** GAMMA is a crud factor for angle (essentially ** cruds up the spread factor), ** delta is the difference in radians between the ** angle you are shooting at and the actual ** angle of the klingon, ** ALPHA scales down the significance of sigma, ** BETA scales down the significance of rho, ** OMEGA is the magic number which makes everything ** up to "* hit" between zero and one, ** dist is the distance to the klingon ** hit is the number of units in the bank, and ** zap is the amount of the actual hit. ** ** Everything up through dist squared should maximize ** at 1.0, so that the distance factor is never ** greater than one. Conveniently, cos() is ** never greater than one, but the same restric- ** tion applies. */ distfactor = BETA + franf(); distfactor *= ALPHA + b->spread; distfactor *= OMEGA; anglefactor = k->dist; distfactor /= anglefactor * anglefactor + EPSILON; distfactor *= b->units; dx = Ship.sectx - k->x; dy = k->y - Ship.secty; anglefactor = atan2(dy, dx) - b->angle; anglefactor = cos((anglefactor * b->spread) + GAMMA); if (anglefactor < 0.0) { k++; continue; } hit = anglefactor * distfactor + 0.5; k->power -= hit; printf("%d unit hit on Klingon", hit); if (!damaged(SRSCAN)) printf(" at %d,%d", k->x, k->y); printf("\n"); b->units -= hit; if (k->power <= 0) { killk(k->x, k->y); continue; } k++; } } /* compute overkill */ for (i = 0; i < NBANKS; i++) extra += bank[i].units; if (extra > 0) printf("\n%d units expended on empty space\n", extra); }
extern void scanerror(char *s) { skiptonl(); /* flush up to newline */ yyerror(s); errset = prerror = TRUE; }
struct cvntab * getcodpar(const char *s, struct cvntab tab[]) { char input[100]; struct cvntab *r; int flag; char *p; const char *q; int c; int f; flag = 0; while (1) { flag |= (f = testnl()); if (flag) printf("%s: ", s); if (f) cgetc(0); /* throw out the newline */ scanf("%*[ \t;]"); if ((c = scanf("%[^ \t;\n]", input)) < 0) exit(1); if (c == 0) continue; flag = 1; /* if command list, print four per line */ if (input[0] == '?' && input[1] == 0) { c = 4; for (r = tab; r->abrev; r++) { strcpy(input, r->abrev); strcat(input, r->full); printf("%14.14s", input); if (--c > 0) continue; c = 4; printf("\n"); } if (c != 4) printf("\n"); continue; } /* search for in table */ for (r = tab; r->abrev; r++) { p = input; for (q = r->abrev; *q; q++) if (*p++ != *q) break; if (!*q) { for (q = r->full; *p && *q; q++, p++) if (*p != *q) break; if (!*p || !*q) break; } } /* check for not found */ if (!r->abrev) { printf("invalid input; ? for valid inputs\n"); skiptonl(0); } else return (r); } }