]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - trek/events.c
2 * Copyright (c) 1980 Regents of the University of California.
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
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 $";
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 int warp
; /* set if called in a time warp */
59 struct event
*ev
, *xsched(), *schedule();
61 register struct quad
*q
;
62 register struct event
*e
;
66 /* if nothing happened, just allow for any Klingons killed */
69 Now
.time
= Now
.resource
/ Now
.klings
;
73 /* indicate that the cloaking device is now working */
76 /* idate is the initial date */
79 /* schedule attacks if resting too long */
80 if (Move
.time
> 0.5 && Move
.resting
)
81 schedule(E_ATTACK
, 0.5, 0, 0, 0);
83 /* scan the event list */
88 /* xdate is the date of the current event */
89 xdate
= idate
+ Move
.time
;
91 /* find the first event that has happened */
92 for (i
= 0; i
< MAXEVENTS
; i
++)
95 if (e
->evcode
== 0 || (e
->evcode
& E_GHOST
))
106 /* find the time between events */
107 rtime
= xdate
- Now
.date
;
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
;
114 /* move us up to the next date */
117 /* check for out of time */
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
);
126 /* if evnum < 0, no events occurred */
130 /* otherwise one did. Find out what it is */
131 switch (e
->evcode
& E_EVENT
)
134 case E_SNOVA
: /* supernova */
135 /* cause the supernova to happen */
137 /* and schedule the next one */
138 xresched(e
, E_SNOVA
, 1);
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
)
147 /* pick a new quadrant */
148 i
= ranf(Now
.klings
) + 1;
149 for (ix
= 0; ix
< NQUADS
; ix
++)
151 for (iy
= 0; iy
< NQUADS
; iy
++)
155 if ((i
-= q
->klings
) <= 0)
162 /* test for LRTB to same quadrant */
163 if (Ship
.quadx
== ix
&& Ship
.quady
== iy
)
166 /* nope, dump him in the new quadrant */
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
);
174 /* truncate the move time */
175 Move
.time
= xdate
- idate
;
179 case E_KATSB
: /* Klingon attacks starbase */
180 /* if out of bases, forget it */
187 /* check for starbase and Klingons in same quadrant */
188 for (i
= 0; i
< Now
.bases
; i
++)
192 /* see if a Klingon exists in this quadrant */
197 /* see if already distressed */
198 for (j
= 0; j
< MAXEVENTS
; j
++)
201 if ((e
->evcode
& E_EVENT
) != E_KDESB
)
203 if (e
->x
== ix
&& e
->y
== iy
)
209 /* got a potential attack */
215 /* not now; wait a while and see if some Klingons move in */
216 reschedule(e
, 0.5 + 3.0 * franf());
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);
223 /* report it if we can */
224 if (!damaged(SSRADIO
))
226 printf("\nUhura: Captain, we have recieved a distress signal\n");
227 printf(" from the starbase in quadrant %d,%d.\n",
232 /* SSRADIO out, make it so we can't see the distress call */
233 /* but it's still there!!! */
234 e
->evcode
|= E_HIDDEN
;
237 case E_KDESB
: /* Klingon destroys starbase */
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)
244 /* are we in the same quadrant? */
245 if (e
->x
== Ship
.quadx
&& e
->y
== Ship
.quady
)
247 /* yep, kill one in this quadrant */
249 killb(Ship
.quadx
, Ship
.quady
);
252 /* kill one in some other quadrant */
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
)
261 /* try a whole bunch of times to find something suitable */
262 for (i
= 0; i
< 100; i
++)
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))
277 /* can't seem to find one; ignore this call */
280 /* got one!! Schedule its enslavement */
282 e
= xsched(E_ENSLV
, 1, ix
, iy
, q
->qsystemname
);
283 q
->qsystemname
= (e
- Event
) | Q_DISTRESSED
;
285 /* tell the captain about it if we can */
286 if (!damaged(SSRADIO
))
288 printf("\nUhura: Captain, starsystem %s in quadrant %d,%d is under attack\n",
289 Systemname
[e
->systemname
], ix
, iy
);
293 /* if we can't tell him, make it invisible */
294 e
->evcode
|= E_HIDDEN
;
297 case E_ENSLV
: /* starsystem is enslaved */
299 /* see if current distress call still active */
300 q
= &Quad
[e
->x
][e
->y
];
303 /* no Klingons, clean up */
304 /* restore the system name */
305 q
->qsystemname
= e
->systemname
;
309 /* play stork and schedule the first baby */
310 e
= schedule(E_REPRO
, Param
.eventdly
[E_REPRO
] * franf(), e
->x
, e
->y
, e
->systemname
);
312 /* report the disaster if we can */
313 if (!damaged(SSRADIO
))
315 printf("\nUhura: We've lost contact with starsystem %s\n",
316 Systemname
[e
->systemname
]);
317 printf(" in quadrant %d,%d.\n",
321 e
->evcode
|= E_HIDDEN
;
324 case E_REPRO
: /* Klingon reproduces */
325 /* see if distress call is still active */
326 q
= &Quad
[e
->x
][e
->y
];
330 q
->qsystemname
= e
->systemname
;
333 xresched(e
, E_REPRO
, 1);
334 /* reproduce one Klingon */
337 if (Now
.klings
== 127)
338 break; /* full right now */
339 if (q
->klings
>= MAXKLQUAD
)
341 /* this quadrant not ok, pick an adjacent one */
342 for (i
= ix
- 1; i
<= ix
+ 1; i
++)
344 if (i
< 0 || i
>= NQUADS
)
346 for (j
= iy
- 1; j
<= iy
+ 1; j
++)
348 if (j
< 0 || j
>= NQUADS
)
351 /* check for this quad ok (not full & no snova) */
352 if (q
->klings
>= MAXKLQUAD
|| q
->stars
< 0)
360 /* cannot create another yet */
365 /* deliver the child */
368 if (ix
== Ship
.quadx
&& iy
== Ship
.quady
)
370 /* we must position Klingon */
372 Sect
[ix
][iy
] = KLINGON
;
373 k
= &Etc
.klingon
[Etc
.nkling
++];
376 k
->power
= Param
.klingpwr
;
378 compkldist(Etc
.klingon
[0].dist
== Etc
.klingon
[0].avgdist
? 0 : 1);
381 /* recompute time left */
382 Now
.time
= Now
.resource
/ Now
.klings
;
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
));
394 case E_ATTACK
: /* Klingons attack during rest period */
408 /* de-damage the device */
409 printf("%s reports repair work on the %s finished.\n",
410 Device
[i
].person
, Device
[i
].name
);
412 /* handle special processing upon fix */
417 Ship
.reserves
= Param
.reserves
;
421 if (Ship
.cond
== DOCKED
)
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");
430 restcancel
= dumpssradio();
439 if (restcancel
&& Move
.resting
&& getynpar("Spock: Shall we cancel our rest period"))
440 Move
.time
= xdate
- idate
;
444 /* unschedule an attack during a rest period */
445 if (e
= Now
.eventptr
[E_ATTACK
])
450 /* eat up energy if cloaked */
452 Ship
.energy
-= Param
.cloakenergy
* Move
.time
;
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
;
459 /* decrement life support reserves */
460 if (damaged(LIFESUP
) && Ship
.cond
!= DOCKED
)
461 Ship
.reserves
-= Move
.time
;