+ case FDISTR: /* inhabited system issues distress call */
+ unschedule(FDISTR);
+ /* try a whole bunch of times to find something suitable */
+ i = 100;
+ do {
+ /* need a quadrant which is not the current one,
+ which has some stars which are inhabited and
+ not already under attack, which is not
+ supernova'ed, and which has some Klingons in it */
+ w = iran(GALSIZE);
+ q = &game.state.galaxy[w.x][w.y];
+ } while (--i &&
+ (same(game.quadrant, w) || q->planet == NOPLANET ||
+ q->supernova || q->status!=secure || q->klingons<=0));
+ if (i == 0) {
+ /* can't seem to find one; ignore this call */
+ if (idebug)
+ prout("=== Couldn't find location for distress event.");
+ break;
+ }
+
+ /* got one!! Schedule its enslavement */
+ ev = schedule(FENSLV, expran(game.intime));
+ ev->quadrant = w;
+ q->status = distressed;
+
+ /* tell the captain about it if we can */
+ if (!damaged(DRADIO) || game.condit == IHDOCKED)
+ {
+ prout("Uhura- Captain, %s in %s reports it is under attack",
+ systemname(q->planet), cramlc(quadrant, w));
+ prout("by a Klingon invasion fleet.");
+ if (cancelrest())
+ return;
+ }
+ break;
+ case FENSLV: /* starsystem is enslaved */
+ ev = unschedule(FENSLV);
+ /* see if current distress call still active */
+ q = &game.state.galaxy[ev->quadrant.x][ev->quadrant.y];
+ if (q->klingons <= 0) {
+ q->status = secure;
+ break;
+ }
+ q->status = enslaved;
+
+ /* play stork and schedule the first baby */
+ ev2 = schedule(FREPRO, expran(2.0 * game.intime));
+ ev2->quadrant = ev->quadrant;
+
+ /* report the disaster if we can */
+ if (!damaged(DRADIO) || game.condit == IHDOCKED)
+ {
+ prout("Uhura- We've lost contact with starsystem %s",
+ systemname(q->planet));
+ prout("in %s.\n", cramlc(quadrant, ev->quadrant));
+ }
+ break;
+ case FREPRO: /* Klingon reproduces */
+ /*
+ * If we ever switch to a real event queue, we'll need to
+ * explicitly retrieve and restore the x and y.
+ */
+ ev = schedule(FREPRO, expran(1.0 * game.intime));
+ /* see if current distress call still active */
+ q = &game.state.galaxy[ev->quadrant.x][ev->quadrant.y];
+ if (q->klingons <= 0) {
+ q->status = secure;
+ break;
+ }
+ if (game.state.remkl >=MAXKLGAME)
+ break; /* full right now */
+ /* reproduce one Klingon */
+ w = ev->quadrant;
+ if (game.klhere >= MAXKLQUAD) {
+ /* this quadrant not ok, pick an adjacent one */
+ for (i = w.x - 1; i <= w.x + 1; i++)
+ {
+ for (j = w.y - 1; j <= w.y + 1; j++)
+ {
+ if (!VALID_QUADRANT(i, j))
+ continue;
+ q = &game.state.galaxy[w.x][w.y];
+ /* check for this quad ok (not full & no snova) */
+ if (q->klingons >= MAXKLQUAD || q->supernova)
+ continue;
+ goto foundit;
+ }
+ }
+ break; /* search for eligible quadrant failed */
+ foundit:
+ w.x = i;
+ w.y = j;
+ }
+
+ /* deliver the child */
+ game.state.remkl++;
+ q->klingons++;
+ if (same(game.quadrant, w))
+ newkling(++game.klhere);
+
+ /* recompute time left */
+ game.state.remtime = game.state.remres/(game.state.remkl+4*game.state.remcom);
+ /* report the disaster if we can */
+ if (!damaged(DRADIO) || game.condit == IHDOCKED)
+ {
+ if (same(game.quadrant, w)) {
+ prout("Spock- sensors indicate the Klingons have");
+ prout("launched a warship from %s.",systemname(q->planet));
+ } else {
+ prout("Uhura- Starfleet reports increased Klingon activity");
+ if (q->planet != NOPLANET)
+ proutn("near %s", systemname(q->planet));
+ prout("in %s.\n", cramlc(quadrant, w));
+ }
+ }
+ break;