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