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