2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 * @(#)events.c 8.1 (Berkeley) 5/31/93
34 * $FreeBSD: src/games/trek/events.c,v 1.4 1999/11/30 03:49:47 billf Exp $
35 * $DragonFly: src/games/trek/events.c,v 1.4 2008/04/20 13:44:24 swildner Exp $
42 ** CAUSE TIME TO ELAPSE
44 ** This routine does a hell of a lot. It elapses time, eats up
45 ** energy, regenerates energy, processes any events that occur,
51 /* t_warp: set if called in a time warp */
59 struct event
*ev
= NULL
;
67 /* if nothing happened, just allow for any Klingons killed */
70 Now
.time
= Now
.resource
/ Now
.klings
;
74 /* indicate that the cloaking device is now working */
77 /* idate is the initial date */
80 /* schedule attacks if resting too long */
81 if (Move
.time
> 0.5 && Move
.resting
)
82 schedule(E_ATTACK
, 0.5, 0, 0, 0);
84 /* scan the event list */
89 /* xdate is the date of the current event */
90 xdate
= idate
+ Move
.time
;
92 /* find the first event that has happened */
93 for (i
= 0; i
< MAXEVENTS
; i
++)
96 if (e
->evcode
== 0 || (e
->evcode
& E_GHOST
))
107 /* find the time between events */
108 rtime
= xdate
- Now
.date
;
110 /* decrement the magic "Federation Resources" pseudo-variable */
111 Now
.resource
-= Now
.klings
* rtime
;
112 /* and recompute the time left */
113 Now
.time
= Now
.resource
/ Now
.klings
;
115 /* move us up to the next date */
118 /* check for out of time */
122 if (evnum
>= 0 && Trace
)
123 printf("xdate = %.2f, evcode %d params %d %d %d\n",
124 xdate
, e
->evcode
, e
->x
, e
->y
, e
->systemname
);
127 /* if evnum < 0, no events occurred */
131 /* otherwise one did. Find out what it is */
132 switch (e
->evcode
& E_EVENT
)
135 case E_SNOVA
: /* supernova */
136 /* cause the supernova to happen */
138 /* and schedule the next one */
139 xresched(e
, E_SNOVA
, 1);
142 case E_LRTB
: /* long range tractor beam */
143 /* schedule the next one */
144 xresched(e
, E_LRTB
, Now
.klings
);
145 /* LRTB cannot occur if we are docked */
146 if (Ship
.cond
!= DOCKED
)
148 /* pick a new quadrant */
149 i
= ranf(Now
.klings
) + 1;
150 for (ix
= 0; ix
< NQUADS
; ix
++)
152 for (iy
= 0; iy
< NQUADS
; iy
++)
156 if ((i
-= q
->klings
) <= 0)
163 /* test for LRTB to same quadrant */
164 if (Ship
.quadx
== ix
&& Ship
.quady
== iy
)
167 /* nope, dump him in the new quadrant */
170 printf("\n%s caught in long range tractor beam\n", Ship
.shipname
);
171 printf("*** Pulled to quadrant %d,%d\n", Ship
.quadx
, Ship
.quady
);
172 Ship
.sectx
= ranf(NSECTS
);
173 Ship
.secty
= ranf(NSECTS
);
175 /* truncate the move time */
176 Move
.time
= xdate
- idate
;
180 case E_KATSB
: /* Klingon attacks starbase */
181 /* if out of bases, forget it */
188 /* check for starbase and Klingons in same quadrant */
189 for (i
= 0; i
< Now
.bases
; i
++)
193 /* see if a Klingon exists in this quadrant */
198 /* see if already distressed */
199 for (j
= 0; j
< MAXEVENTS
; j
++)
202 if ((e
->evcode
& E_EVENT
) != E_KDESB
)
204 if (e
->x
== ix
&& e
->y
== iy
)
210 /* got a potential attack */
216 /* not now; wait a while and see if some Klingons move in */
217 reschedule(e
, 0.5 + 3.0 * franf());
220 /* schedule a new attack, and a destruction of the base */
221 xresched(e
, E_KATSB
, 1);
222 e
= xsched(E_KDESB
, 1, ix
, iy
, 0);
224 /* report it if we can */
225 if (!damaged(SSRADIO
))
227 printf("\nUhura: Captain, we have received a distress signal\n");
228 printf(" from the starbase in quadrant %d,%d.\n",
233 /* SSRADIO out, make it so we can't see the distress call */
234 /* but it's still there!!! */
235 e
->evcode
|= E_HIDDEN
;
238 case E_KDESB
: /* Klingon destroys starbase */
240 q
= &Quad
[e
->x
][e
->y
];
241 /* if the base has mysteriously gone away, or if the Klingon
242 got tired and went home, ignore this event */
243 if (q
->bases
<=0 || q
->klings
<= 0)
245 /* are we in the same quadrant? */
246 if (e
->x
== Ship
.quadx
&& e
->y
== Ship
.quady
)
248 /* yep, kill one in this quadrant */
250 killb(Ship
.quadx
, Ship
.quady
);
253 /* kill one in some other quadrant */
257 case E_ISSUE
: /* issue a distress call */
258 xresched(e
, E_ISSUE
, 1);
259 /* if we already have too many, throw this one away */
260 if (Ship
.distressed
>= MAXDISTR
)
262 /* try a whole bunch of times to find something suitable */
263 for (i
= 0; i
< 100; i
++)
268 /* need a quadrant which is not the current one,
269 which has some stars which are inhabited and
270 not already under attack, which is not
271 supernova'ed, and which has some Klingons in it */
272 if (!((ix
== Ship
.quadx
&& iy
== Ship
.quady
) || q
->stars
< 0 ||
273 (q
->qsystemname
& Q_DISTRESSED
) ||
274 (q
->qsystemname
& Q_SYSTEM
) == 0 || q
->klings
<= 0))
278 /* can't seem to find one; ignore this call */
281 /* got one!! Schedule its enslavement */
283 e
= xsched(E_ENSLV
, 1, ix
, iy
, q
->qsystemname
);
284 q
->qsystemname
= (e
- Event
) | Q_DISTRESSED
;
286 /* tell the captain about it if we can */
287 if (!damaged(SSRADIO
))
289 printf("\nUhura: Captain, starsystem %s in quadrant %d,%d is under attack\n",
290 Systemname
[e
->systemname
], ix
, iy
);
294 /* if we can't tell him, make it invisible */
295 e
->evcode
|= E_HIDDEN
;
298 case E_ENSLV
: /* starsystem is enslaved */
300 /* see if current distress call still active */
301 q
= &Quad
[e
->x
][e
->y
];
304 /* no Klingons, clean up */
305 /* restore the system name */
306 q
->qsystemname
= e
->systemname
;
310 /* play stork and schedule the first baby */
311 e
= schedule(E_REPRO
, Param
.eventdly
[E_REPRO
] * franf(), e
->x
, e
->y
, e
->systemname
);
313 /* report the disaster if we can */
314 if (!damaged(SSRADIO
))
316 printf("\nUhura: We've lost contact with starsystem %s\n",
317 Systemname
[e
->systemname
]);
318 printf(" in quadrant %d,%d.\n",
322 e
->evcode
|= E_HIDDEN
;
325 case E_REPRO
: /* Klingon reproduces */
326 /* see if distress call is still active */
327 q
= &Quad
[e
->x
][e
->y
];
331 q
->qsystemname
= e
->systemname
;
334 xresched(e
, E_REPRO
, 1);
335 /* reproduce one Klingon */
338 if (Now
.klings
== 127)
339 break; /* full right now */
340 if (q
->klings
>= MAXKLQUAD
)
342 /* this quadrant not ok, pick an adjacent one */
343 for (i
= ix
- 1; i
<= ix
+ 1; i
++)
345 if (i
< 0 || i
>= NQUADS
)
347 for (j
= iy
- 1; j
<= iy
+ 1; j
++)
349 if (j
< 0 || j
>= NQUADS
)
352 /* check for this quad ok (not full & no snova) */
353 if (q
->klings
>= MAXKLQUAD
|| q
->stars
< 0)
361 /* cannot create another yet */
366 /* deliver the child */
369 if (ix
== Ship
.quadx
&& iy
== Ship
.quady
)
371 /* we must position Klingon */
373 Sect
[ix
][iy
] = KLINGON
;
374 k
= &Etc
.klingon
[Etc
.nkling
++];
377 k
->power
= Param
.klingpwr
;
379 compkldist(Etc
.klingon
[0].dist
== Etc
.klingon
[0].avgdist
? 0 : 1);
382 /* recompute time left */
383 Now
.time
= Now
.resource
/ Now
.klings
;
386 case E_SNAP
: /* take a snapshot of the galaxy */
387 xresched(e
, E_SNAP
, 1);
389 s
= bmove(Quad
, s
, sizeof (Quad
));
390 s
= bmove(Event
, s
, sizeof (Event
));
391 s
= bmove(&Now
, s
, sizeof (Now
));
395 case E_ATTACK
: /* Klingons attack during rest period */
409 /* de-damage the device */
410 printf("%s reports repair work on the %s finished.\n",
411 Device
[i
].person
, Device
[i
].name
);
413 /* handle special processing upon fix */
418 Ship
.reserves
= Param
.reserves
;
422 if (Ship
.cond
== DOCKED
)
424 printf("Spock has tried to recalibrate your Space Internal Navigation System,\n");
425 printf(" but he has no standard base to calibrate to. Suggest you get\n");
426 printf(" to a starbase immediately so that you can properly recalibrate.\n");
431 restcancel
= dumpssradio();
440 if (restcancel
&& Move
.resting
&& getynpar("Spock: Shall we cancel our rest period"))
441 Move
.time
= xdate
- idate
;
445 /* unschedule an attack during a rest period */
446 if ((e
= Now
.eventptr
[E_ATTACK
]))
451 /* eat up energy if cloaked */
453 Ship
.energy
-= Param
.cloakenergy
* Move
.time
;
455 /* regenerate resources */
456 rtime
= 1.0 - exp(-Param
.regenfac
* Move
.time
);
457 Ship
.shield
+= (Param
.shield
- Ship
.shield
) * rtime
;
458 Ship
.energy
+= (Param
.energy
- Ship
.energy
) * rtime
;
460 /* decrement life support reserves */
461 if (damaged(LIFESUP
) && Ship
.cond
!= DOCKED
)
462 Ship
.reserves
-= Move
.time
;