]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - trek/events.c
1 /* $NetBSD: events.c,v 1.3 1995/04/22 10:58:50 cgd Exp $ */
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static char sccsid
[] = "@(#)events.c 8.1 (Berkeley) 5/31/93";
40 static char rcsid
[] = "$NetBSD: events.c,v 1.3 1995/04/22 10:58:50 cgd Exp $";
47 ** CAUSE TIME TO ELAPSE
49 ** This routine does a hell of a lot. It elapses time, eats up
50 ** energy, regenerates energy, processes any events that occur,
56 int warp
; /* set if called in a time warp */
64 struct event
*ev
, *xsched(), *schedule();
66 register struct quad
*q
;
67 register struct event
*e
;
71 /* if nothing happened, just allow for any Klingons killed */
74 Now
.time
= Now
.resource
/ Now
.klings
;
78 /* indicate that the cloaking device is now working */
81 /* idate is the initial date */
84 /* schedule attacks if resting too long */
85 if (Move
.time
> 0.5 && Move
.resting
)
86 schedule(E_ATTACK
, 0.5, 0, 0, 0);
88 /* scan the event list */
93 /* xdate is the date of the current event */
94 xdate
= idate
+ Move
.time
;
96 /* find the first event that has happened */
97 for (i
= 0; i
< MAXEVENTS
; i
++)
100 if (e
->evcode
== 0 || (e
->evcode
& E_GHOST
))
111 /* find the time between events */
112 rtime
= xdate
- Now
.date
;
114 /* decrement the magic "Federation Resources" pseudo-variable */
115 Now
.resource
-= Now
.klings
* rtime
;
116 /* and recompute the time left */
117 Now
.time
= Now
.resource
/ Now
.klings
;
119 /* move us up to the next date */
122 /* check for out of time */
126 if (evnum
>= 0 && Trace
)
127 printf("xdate = %.2f, evcode %d params %d %d %d\n",
128 xdate
, e
->evcode
, e
->x
, e
->y
, e
->systemname
);
131 /* if evnum < 0, no events occurred */
135 /* otherwise one did. Find out what it is */
136 switch (e
->evcode
& E_EVENT
)
139 case E_SNOVA
: /* supernova */
140 /* cause the supernova to happen */
142 /* and schedule the next one */
143 xresched(e
, E_SNOVA
, 1);
146 case E_LRTB
: /* long range tractor beam */
147 /* schedule the next one */
148 xresched(e
, E_LRTB
, Now
.klings
);
149 /* LRTB cannot occur if we are docked */
150 if (Ship
.cond
!= DOCKED
)
152 /* pick a new quadrant */
153 i
= ranf(Now
.klings
) + 1;
154 for (ix
= 0; ix
< NQUADS
; ix
++)
156 for (iy
= 0; iy
< NQUADS
; iy
++)
160 if ((i
-= q
->klings
) <= 0)
167 /* test for LRTB to same quadrant */
168 if (Ship
.quadx
== ix
&& Ship
.quady
== iy
)
171 /* nope, dump him in the new quadrant */
174 printf("\n%s caught in long range tractor beam\n", Ship
.shipname
);
175 printf("*** Pulled to quadrant %d,%d\n", Ship
.quadx
, Ship
.quady
);
176 Ship
.sectx
= ranf(NSECTS
);
177 Ship
.secty
= ranf(NSECTS
);
179 /* truncate the move time */
180 Move
.time
= xdate
- idate
;
184 case E_KATSB
: /* Klingon attacks starbase */
185 /* if out of bases, forget it */
192 /* check for starbase and Klingons in same quadrant */
193 for (i
= 0; i
< Now
.bases
; i
++)
197 /* see if a Klingon exists in this quadrant */
202 /* see if already distressed */
203 for (j
= 0; j
< MAXEVENTS
; j
++)
206 if ((e
->evcode
& E_EVENT
) != E_KDESB
)
208 if (e
->x
== ix
&& e
->y
== iy
)
214 /* got a potential attack */
220 /* not now; wait a while and see if some Klingons move in */
221 reschedule(e
, 0.5 + 3.0 * franf());
224 /* schedule a new attack, and a destruction of the base */
225 xresched(e
, E_KATSB
, 1);
226 e
= xsched(E_KDESB
, 1, ix
, iy
, 0);
228 /* report it if we can */
229 if (!damaged(SSRADIO
))
231 printf("\nUhura: Captain, we have recieved a distress signal\n");
232 printf(" from the starbase in quadrant %d,%d.\n",
237 /* SSRADIO out, make it so we can't see the distress call */
238 /* but it's still there!!! */
239 e
->evcode
|= E_HIDDEN
;
242 case E_KDESB
: /* Klingon destroys starbase */
244 q
= &Quad
[e
->x
][e
->y
];
245 /* if the base has mysteriously gone away, or if the Klingon
246 got tired and went home, ignore this event */
247 if (q
->bases
<=0 || q
->klings
<= 0)
249 /* are we in the same quadrant? */
250 if (e
->x
== Ship
.quadx
&& e
->y
== Ship
.quady
)
252 /* yep, kill one in this quadrant */
254 killb(Ship
.quadx
, Ship
.quady
);
257 /* kill one in some other quadrant */
261 case E_ISSUE
: /* issue a distress call */
262 xresched(e
, E_ISSUE
, 1);
263 /* if we already have too many, throw this one away */
264 if (Ship
.distressed
>= MAXDISTR
)
266 /* try a whole bunch of times to find something suitable */
267 for (i
= 0; i
< 100; i
++)
272 /* need a quadrant which is not the current one,
273 which has some stars which are inhabited and
274 not already under attack, which is not
275 supernova'ed, and which has some Klingons in it */
276 if (!((ix
== Ship
.quadx
&& iy
== Ship
.quady
) || q
->stars
< 0 ||
277 (q
->qsystemname
& Q_DISTRESSED
) ||
278 (q
->qsystemname
& Q_SYSTEM
) == 0 || q
->klings
<= 0))
282 /* can't seem to find one; ignore this call */
285 /* got one!! Schedule its enslavement */
287 e
= xsched(E_ENSLV
, 1, ix
, iy
, q
->qsystemname
);
288 q
->qsystemname
= (e
- Event
) | Q_DISTRESSED
;
290 /* tell the captain about it if we can */
291 if (!damaged(SSRADIO
))
293 printf("\nUhura: Captain, starsystem %s in quadrant %d,%d is under attack\n",
294 Systemname
[e
->systemname
], ix
, iy
);
298 /* if we can't tell him, make it invisible */
299 e
->evcode
|= E_HIDDEN
;
302 case E_ENSLV
: /* starsystem is enslaved */
304 /* see if current distress call still active */
305 q
= &Quad
[e
->x
][e
->y
];
308 /* no Klingons, clean up */
309 /* restore the system name */
310 q
->qsystemname
= e
->systemname
;
314 /* play stork and schedule the first baby */
315 e
= schedule(E_REPRO
, Param
.eventdly
[E_REPRO
] * franf(), e
->x
, e
->y
, e
->systemname
);
317 /* report the disaster if we can */
318 if (!damaged(SSRADIO
))
320 printf("\nUhura: We've lost contact with starsystem %s\n",
321 Systemname
[e
->systemname
]);
322 printf(" in quadrant %d,%d.\n",
326 e
->evcode
|= E_HIDDEN
;
329 case E_REPRO
: /* Klingon reproduces */
330 /* see if distress call is still active */
331 q
= &Quad
[e
->x
][e
->y
];
335 q
->qsystemname
= e
->systemname
;
338 xresched(e
, E_REPRO
, 1);
339 /* reproduce one Klingon */
342 if (Now
.klings
== 127)
343 break; /* full right now */
344 if (q
->klings
>= MAXKLQUAD
)
346 /* this quadrant not ok, pick an adjacent one */
347 for (i
= ix
- 1; i
<= ix
+ 1; i
++)
349 if (i
< 0 || i
>= NQUADS
)
351 for (j
= iy
- 1; j
<= iy
+ 1; j
++)
353 if (j
< 0 || j
>= NQUADS
)
356 /* check for this quad ok (not full & no snova) */
357 if (q
->klings
>= MAXKLQUAD
|| q
->stars
< 0)
365 /* cannot create another yet */
370 /* deliver the child */
373 if (ix
== Ship
.quadx
&& iy
== Ship
.quady
)
375 /* we must position Klingon */
377 Sect
[ix
][iy
] = KLINGON
;
378 k
= &Etc
.klingon
[Etc
.nkling
++];
381 k
->power
= Param
.klingpwr
;
383 compkldist(Etc
.klingon
[0].dist
== Etc
.klingon
[0].avgdist
? 0 : 1);
386 /* recompute time left */
387 Now
.time
= Now
.resource
/ Now
.klings
;
390 case E_SNAP
: /* take a snapshot of the galaxy */
391 xresched(e
, E_SNAP
, 1);
392 i
= (int) Etc
.snapshot
;
393 i
= bmove(Quad
, i
, sizeof (Quad
));
394 i
= bmove(Event
, i
, sizeof (Event
));
395 i
= bmove(&Now
, i
, sizeof (Now
));
399 case E_ATTACK
: /* Klingons attack during rest period */
413 /* de-damage the device */
414 printf("%s reports repair work on the %s finished.\n",
415 Device
[i
].person
, Device
[i
].name
);
417 /* handle special processing upon fix */
422 Ship
.reserves
= Param
.reserves
;
426 if (Ship
.cond
== DOCKED
)
428 printf("Spock has tried to recalibrate your Space Internal Navigation System,\n");
429 printf(" but he has no standard base to calibrate to. Suggest you get\n");
430 printf(" to a starbase immediately so that you can properly recalibrate.\n");
435 restcancel
= dumpssradio();
444 if (restcancel
&& Move
.resting
&& getynpar("Spock: Shall we cancel our rest period"))
445 Move
.time
= xdate
- idate
;
449 /* unschedule an attack during a rest period */
450 if (e
= Now
.eventptr
[E_ATTACK
])
455 /* eat up energy if cloaked */
457 Ship
.energy
-= Param
.cloakenergy
* Move
.time
;
459 /* regenerate resources */
460 rtime
= 1.0 - exp(-Param
.regenfac
* Move
.time
);
461 Ship
.shield
+= (Param
.shield
- Ship
.shield
) * rtime
;
462 Ship
.energy
+= (Param
.energy
- Ship
.energy
) * rtime
;
464 /* decrement life support reserves */
465 if (damaged(LIFESUP
) && Ship
.cond
!= DOCKED
)
466 Ship
.reserves
-= Move
.time
;