get rid of sibuf; requires change to user interface because of
[unix-history] / usr / src / games / trek / events.c
CommitLineData
b6f0a7e4
DF
1/*
2 * Copyright (c) 1980 Regents of the University of California.
e9fb6bea
KB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of California at Berkeley. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
b6f0a7e4
DF
11 */
12
bab4d182 13#ifndef lint
e9fb6bea
KB
14static char sccsid[] = "@(#)events.c 5.2 (Berkeley) %G%";
15#endif /* not lint */
bab4d182
KM
16
17# include "trek.h"
18
19/*
20** CAUSE TIME TO ELAPSE
21**
22** This routine does a hell of a lot. It elapses time, eats up
23** energy, regenerates energy, processes any events that occur,
24** and so on.
25*/
26
27
28events(warp)
29int warp; /* set if called in a time warp */
30{
31 register int i;
32 int j;
33 struct kling *k;
34 double rtime;
35 double xdate;
36 double idate;
06d69904 37 struct event *ev, *xsched(), *schedule();
bab4d182
KM
38 int ix, iy;
39 register struct quad *q;
40 register struct event *e;
41 int evnum;
42 int restcancel;
43
44 /* if nothing happened, just allow for any Klingons killed */
45 if (Move.time <= 0.0)
46 {
47 Now.time = Now.resource / Now.klings;
48 return (0);
49 }
50
51 /* indicate that the cloaking device is now working */
52 Ship.cloakgood = 1;
53
54 /* idate is the initial date */
55 idate = Now.date;
56
57 /* schedule attacks if resting too long */
58 if (Move.time > 0.5 && Move.resting)
59 schedule(E_ATTACK, 0.5, 0, 0, 0);
60
61 /* scan the event list */
62 while (1)
63 {
64 restcancel = 0;
65 evnum = -1;
66 /* xdate is the date of the current event */
67 xdate = idate + Move.time;
68
69 /* find the first event that has happened */
70 for (i = 0; i < MAXEVENTS; i++)
71 {
72 e = &Event[i];
73 if (e->evcode == 0 || (e->evcode & E_GHOST))
74 continue;
75 if (e->date < xdate)
76 {
77 xdate = e->date;
78 ev = e;
79 evnum = i;
80 }
81 }
82 e = ev;
83
84 /* find the time between events */
85 rtime = xdate - Now.date;
86
87 /* decrement the magic "Federation Resources" pseudo-variable */
06d69904 88 Now.resource -= Now.klings * rtime;
bab4d182
KM
89 /* and recompute the time left */
90 Now.time = Now.resource / Now.klings;
91
92 /* move us up to the next date */
93 Now.date = xdate;
94
95 /* check for out of time */
96 if (Now.time <= 0.0)
97 lose(L_NOTIME);
98# ifdef xTRACE
99 if (evnum >= 0 && Trace)
100 printf("xdate = %.2f, evcode %d params %d %d %d\n",
101 xdate, e->evcode, e->x, e->y, e->systemname);
102# endif
103
104 /* if evnum < 0, no events occurred */
105 if (evnum < 0)
106 break;
107
108 /* otherwise one did. Find out what it is */
109 switch (e->evcode & E_EVENT)
110 {
111
112 case E_SNOVA: /* supernova */
113 /* cause the supernova to happen */
114 snova(-1);
115 /* and schedule the next one */
116 xresched(e, E_SNOVA, 1);
117 break;
118
119 case E_LRTB: /* long range tractor beam */
120 /* schedule the next one */
121 xresched(e, E_LRTB, Now.klings);
122 /* LRTB cannot occur if we are docked */
123 if (Ship.cond != DOCKED)
124 {
125 /* pick a new quadrant */
126 i = ranf(Now.klings) + 1;
127 for (ix = 0; ix < NQUADS; ix++)
128 {
129 for (iy = 0; iy < NQUADS; iy++)
130 {
131 q = &Quad[ix][iy];
132 if (q->stars >= 0)
06d69904 133 if ((i -= q->klings) <= 0)
bab4d182
KM
134 break;
135 }
136 if (i <= 0)
137 break;
138 }
139
140 /* test for LRTB to same quadrant */
141 if (Ship.quadx == ix && Ship.quady == iy)
142 break;
143
144 /* nope, dump him in the new quadrant */
145 Ship.quadx = ix;
146 Ship.quady = iy;
147 printf("\n%s caught in long range tractor beam\n", Ship.shipname);
148 printf("*** Pulled to quadrant %d,%d\n", Ship.quadx, Ship.quady);
149 Ship.sectx = ranf(NSECTS);
150 Ship.secty = ranf(NSECTS);
151 initquad(0);
152 /* truncate the move time */
153 Move.time = xdate - idate;
154 }
155 break;
156
157 case E_KATSB: /* Klingon attacks starbase */
158 /* if out of bases, forget it */
159 if (Now.bases <= 0)
160 {
161 unschedule(e);
162 break;
163 }
164
165 /* check for starbase and Klingons in same quadrant */
166 for (i = 0; i < Now.bases; i++)
167 {
168 ix = Now.base[i].x;
169 iy = Now.base[i].y;
170 /* see if a Klingon exists in this quadrant */
171 q = &Quad[ix][iy];
172 if (q->klings <= 0)
173 continue;
174
175 /* see if already distressed */
176 for (j = 0; j < MAXEVENTS; j++)
177 {
178 e = &Event[j];
179 if ((e->evcode & E_EVENT) != E_KDESB)
180 continue;
181 if (e->x == ix && e->y == iy)
182 break;
183 }
184 if (j < MAXEVENTS)
185 continue;
186
187 /* got a potential attack */
188 break;
189 }
190 e = ev;
191 if (i >= Now.bases)
192 {
193 /* not now; wait a while and see if some Klingons move in */
194 reschedule(e, 0.5 + 3.0 * franf());
195 break;
196 }
197 /* schedule a new attack, and a destruction of the base */
198 xresched(e, E_KATSB, 1);
199 e = xsched(E_KDESB, 1, ix, iy, 0);
200
201 /* report it if we can */
202 if (!damaged(SSRADIO))
203 {
204 printf("\nUhura: Captain, we have recieved a distress signal\n");
205 printf(" from the starbase in quadrant %d,%d.\n",
206 ix, iy);
207 restcancel++;
208 }
209 else
210 /* SSRADIO out, make it so we can't see the distress call */
211 /* but it's still there!!! */
06d69904 212 e->evcode |= E_HIDDEN;
bab4d182
KM
213 break;
214
215 case E_KDESB: /* Klingon destroys starbase */
216 unschedule(e);
217 q = &Quad[e->x][e->y];
218 /* if the base has mysteriously gone away, or if the Klingon
219 got tired and went home, ignore this event */
220 if (q->bases <=0 || q->klings <= 0)
221 break;
222 /* are we in the same quadrant? */
223 if (e->x == Ship.quadx && e->y == Ship.quady)
224 {
225 /* yep, kill one in this quadrant */
226 printf("\nSpock: ");
227 killb(Ship.quadx, Ship.quady);
228 }
229 else
230 /* kill one in some other quadrant */
231 killb(e->x, e->y);
232 break;
233
234 case E_ISSUE: /* issue a distress call */
235 xresched(e, E_ISSUE, 1);
236 /* if we already have too many, throw this one away */
237 if (Ship.distressed >= MAXDISTR)
238 break;
239 /* try a whole bunch of times to find something suitable */
240 for (i = 0; i < 100; i++)
241 {
242 ix = ranf(NQUADS);
243 iy = ranf(NQUADS);
244 q = &Quad[ix][iy];
245 /* need a quadrant which is not the current one,
246 which has some stars which are inhabited and
247 not already under attack, which is not
248 supernova'ed, and which has some Klingons in it */
249 if (!((ix == Ship.quadx && iy == Ship.quady) || q->stars < 0 ||
06d69904
KL
250 (q->qsystemname & Q_DISTRESSED) ||
251 (q->qsystemname & Q_SYSTEM) == 0 || q->klings <= 0))
bab4d182
KM
252 break;
253 }
254 if (i >= 100)
255 /* can't seem to find one; ignore this call */
256 break;
257
258 /* got one!! Schedule its enslavement */
06d69904
KL
259 Ship.distressed++;
260 e = xsched(E_ENSLV, 1, ix, iy, q->qsystemname);
261 q->qsystemname = (e - Event) | Q_DISTRESSED;
bab4d182
KM
262
263 /* tell the captain about it if we can */
264 if (!damaged(SSRADIO))
265 {
266 printf("\nUhura: Captain, starsystem %s in quadrant %d,%d is under attack\n",
267 Systemname[e->systemname], ix, iy);
268 restcancel++;
269 }
270 else
271 /* if we can't tell him, make it invisible */
06d69904 272 e->evcode |= E_HIDDEN;
bab4d182
KM
273 break;
274
275 case E_ENSLV: /* starsystem is enslaved */
276 unschedule(e);
277 /* see if current distress call still active */
278 q = &Quad[e->x][e->y];
279 if (q->klings <= 0)
280 {
281 /* no Klingons, clean up */
282 /* restore the system name */
06d69904 283 q->qsystemname = e->systemname;
bab4d182
KM
284 break;
285 }
286
287 /* play stork and schedule the first baby */
288 e = schedule(E_REPRO, Param.eventdly[E_REPRO] * franf(), e->x, e->y, e->systemname);
289
290 /* report the disaster if we can */
291 if (!damaged(SSRADIO))
292 {
293 printf("\nUhura: We've lost contact with starsystem %s\n",
294 Systemname[e->systemname]);
295 printf(" in quadrant %d,%d.\n",
296 e->x, e->y);
297 }
298 else
06d69904 299 e->evcode |= E_HIDDEN;
bab4d182
KM
300 break;
301
302 case E_REPRO: /* Klingon reproduces */
303 /* see if distress call is still active */
304 q = &Quad[e->x][e->y];
305 if (q->klings <= 0)
306 {
307 unschedule(e);
06d69904 308 q->qsystemname = e->systemname;
bab4d182
KM
309 break;
310 }
311 xresched(e, E_REPRO, 1);
312 /* reproduce one Klingon */
313 ix = e->x;
314 iy = e->y;
315 if (Now.klings == 127)
316 break; /* full right now */
317 if (q->klings >= MAXKLQUAD)
318 {
319 /* this quadrant not ok, pick an adjacent one */
320 for (i = ix - 1; i <= ix + 1; i++)
321 {
322 if (i < 0 || i >= NQUADS)
323 continue;
324 for (j = iy - 1; j <= iy + 1; j++)
325 {
326 if (j < 0 || j >= NQUADS)
327 continue;
328 q = &Quad[i][j];
329 /* check for this quad ok (not full & no snova) */
330 if (q->klings >= MAXKLQUAD || q->stars < 0)
331 continue;
332 break;
333 }
334 if (j <= iy + 1)
335 break;
336 }
337 if (j > iy + 1)
338 /* cannot create another yet */
339 break;
340 ix = i;
341 iy = j;
342 }
343 /* deliver the child */
06d69904
KL
344 q->klings++;
345 Now.klings++;
bab4d182
KM
346 if (ix == Ship.quadx && iy == Ship.quady)
347 {
348 /* we must position Klingon */
349 sector(&ix, &iy);
350 Sect[ix][iy] = KLINGON;
351 k = &Etc.klingon[Etc.nkling++];
352 k->x = ix;
353 k->y = iy;
354 k->power = Param.klingpwr;
355 k->srndreq = 0;
356 compkldist(Etc.klingon[0].dist == Etc.klingon[0].avgdist ? 0 : 1);
357 }
358
359 /* recompute time left */
360 Now.time = Now.resource / Now.klings;
361 break;
362
363 case E_SNAP: /* take a snapshot of the galaxy */
364 xresched(e, E_SNAP, 1);
06d69904
KL
365 i = (int) Etc.snapshot;
366 i = bmove(Quad, i, sizeof (Quad));
367 i = bmove(Event, i, sizeof (Event));
9623cd28 368 i = bmove(&Now, i, sizeof (Now));
bab4d182
KM
369 Game.snap = 1;
370 break;
371
372 case E_ATTACK: /* Klingons attack during rest period */
373 if (!Move.resting)
374 {
375 unschedule(e);
376 break;
377 }
378 attack(1);
379 reschedule(e, 0.5);
380 break;
381
382 case E_FIXDV:
383 i = e->systemname;
384 unschedule(e);
385
386 /* de-damage the device */
387 printf("%s reports repair work on the %s finished.\n",
388 Device[i].person, Device[i].name);
389
390 /* handle special processing upon fix */
391 switch (i)
392 {
393
394 case LIFESUP:
395 Ship.reserves = Param.reserves;
396 break;
397
398 case SINS:
399 if (Ship.cond == DOCKED)
400 break;
401 printf("Spock has tried to recalibrate your Space Internal Navigation System,\n");
402 printf(" but he has no standard base to calibrate to. Suggest you get\n");
403 printf(" to a starbase immediately so that you can properly recalibrate.\n");
404 Ship.sinsbad = 1;
405 break;
406
407 case SSRADIO:
408 restcancel = dumpssradio();
409 break;
410 }
411 break;
412
413 default:
414 break;
415 }
416
417 if (restcancel && Move.resting && getynpar("Spock: Shall we cancel our rest period"))
418 Move.time = xdate - idate;
419
420 }
421
422 /* unschedule an attack during a rest period */
423 if (e = Now.eventptr[E_ATTACK])
424 unschedule(e);
425
426 if (!warp)
427 {
428 /* eat up energy if cloaked */
429 if (Ship.cloaked)
06d69904 430 Ship.energy -= Param.cloakenergy * Move.time;
bab4d182
KM
431
432 /* regenerate resources */
433 rtime = 1.0 - exp(-Param.regenfac * Move.time);
06d69904
KL
434 Ship.shield += (Param.shield - Ship.shield) * rtime;
435 Ship.energy += (Param.energy - Ship.energy) * rtime;
bab4d182
KM
436
437 /* decrement life support reserves */
438 if (damaged(LIFESUP) && Ship.cond != DOCKED)
06d69904 439 Ship.reserves -= Move.time;
bab4d182
KM
440 }
441 return (0);
442}