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