xref: /csrg-svn/games/trek/schedule.c (revision 34205)
121292Sdist /*
221292Sdist  * Copyright (c) 1980 Regents of the University of California.
3*34205Sbostic  * All rights reserved.
4*34205Sbostic  *
5*34205Sbostic  * Redistribution and use in source and binary forms are permitted
6*34205Sbostic  * provided that this notice is preserved and that due credit is given
7*34205Sbostic  * to the University of California at Berkeley. The name of the University
8*34205Sbostic  * may not be used to endorse or promote products derived from this
9*34205Sbostic  * software without specific prior written permission. This software
10*34205Sbostic  * is provided ``as is'' without express or implied warranty.
1121292Sdist  */
1221292Sdist 
1311692Smckusick #ifndef lint
14*34205Sbostic static char sccsid[] = "@(#)schedule.c	5.2 (Berkeley) 05/05/88";
15*34205Sbostic #endif /* not lint */
1611692Smckusick 
1711692Smckusick # include	"trek.h"
1811692Smckusick 
1911692Smckusick /*
2011692Smckusick **  SCHEDULE AN EVENT
2111692Smckusick **
2211692Smckusick **	An event of type 'type' is scheduled for time NOW + 'offset'
2311692Smckusick **	into the first available slot.  'x', 'y', and 'z' are
2411692Smckusick **	considered the attributes for this event.
2511692Smckusick **
2611692Smckusick **	The address of the slot is returned.
2711692Smckusick */
2811692Smckusick 
2911692Smckusick struct event *schedule(type, offset, x, y, z)
3011692Smckusick int	type;
3112738Slayer double	offset;
3211692Smckusick char	x, y;
3311692Smckusick char	z;
3411692Smckusick {
3511692Smckusick 	register struct event	*e;
3611692Smckusick 	register int		i;
3712738Slayer 	double			date;
3811692Smckusick 
3911692Smckusick 	date = Now.date + offset;
4011692Smckusick 	for (i = 0; i < MAXEVENTS; i++)
4111692Smckusick 	{
4211692Smckusick 		e = &Event[i];
4311692Smckusick 		if (e->evcode)
4411692Smckusick 			continue;
4511692Smckusick 		/* got a slot */
4611692Smckusick #		ifdef xTRACE
4711692Smckusick 		if (Trace)
4811692Smckusick 			printf("schedule: type %d @ %.2f slot %d parm %d %d %d\n",
4911692Smckusick 				type, date, i, x, y, z);
5011692Smckusick #		endif
5111692Smckusick 		e->evcode = type;
5211692Smckusick 		e->date = date;
5311692Smckusick 		e->x = x;
5411692Smckusick 		e->y = y;
5511692Smckusick 		e->systemname = z;
5611692Smckusick 		Now.eventptr[type] = e;
5711692Smckusick 		return (e);
5811692Smckusick 	}
5911692Smckusick 	syserr("Cannot schedule event %d parm %d %d %d", type, x, y, z);
6011692Smckusick }
6111692Smckusick 
6211692Smckusick 
6311692Smckusick /*
6411692Smckusick **  RESCHEDULE AN EVENT
6511692Smckusick **
6611692Smckusick **	The event pointed to by 'e' is rescheduled to the current
6711692Smckusick **	time plus 'offset'.
6811692Smckusick */
6911692Smckusick 
7011692Smckusick reschedule(e1, offset)
7111692Smckusick struct event	*e1;
7212738Slayer double		offset;
7311692Smckusick {
7412738Slayer 	double			date;
7511692Smckusick 	register struct event	*e;
7611692Smckusick 
7711692Smckusick 	e = e1;
7811692Smckusick 
7911692Smckusick 	date = Now.date + offset;
8011692Smckusick 	e->date = date;
8111692Smckusick #	ifdef xTRACE
8211692Smckusick 	if (Trace)
8311692Smckusick 		printf("reschedule: type %d parm %d %d %d @ %.2f\n",
8411692Smckusick 			e->evcode, e->x, e->y, e->systemname, date);
8511692Smckusick #	endif
8611692Smckusick 	return;
8711692Smckusick }
8811692Smckusick 
8911692Smckusick 
9011692Smckusick /*
9111692Smckusick **  UNSCHEDULE AN EVENT
9211692Smckusick **
9311692Smckusick **	The event at slot 'e' is deleted.
9411692Smckusick */
9511692Smckusick 
9611692Smckusick unschedule(e1)
9711692Smckusick struct event	*e1;
9811692Smckusick {
9911692Smckusick 	register struct event	*e;
10011692Smckusick 
10111692Smckusick 	e = e1;
10211692Smckusick 
10311692Smckusick #	ifdef xTRACE
10411692Smckusick 	if (Trace)
10511692Smckusick 		printf("unschedule: type %d @ %.2f parm %d %d %d\n",
10611692Smckusick 			e->evcode, e->date, e->x, e->y, e->systemname);
10711692Smckusick #	endif
10811692Smckusick 	Now.eventptr[e->evcode & E_EVENT] = 0;
10911692Smckusick 	e->date = 1e50;
11011692Smckusick 	e->evcode = 0;
11111692Smckusick 	return;
11211692Smckusick }
11311692Smckusick 
11411692Smckusick 
11511692Smckusick /*
11611692Smckusick **  Abreviated schedule routine
11711692Smckusick **
11811692Smckusick **	Parameters are the event index and a factor for the time
11911692Smckusick **	figure.
12011692Smckusick */
12111692Smckusick 
12211692Smckusick struct event *xsched(ev1, factor, x, y, z)
12311692Smckusick int	ev1;
12411692Smckusick int	factor;
12511692Smckusick int	x, y, z;
12611692Smckusick {
12711692Smckusick 	register int	ev;
12811692Smckusick 
12911692Smckusick 	ev = ev1;
13011692Smckusick 	return (schedule(ev, -Param.eventdly[ev] * Param.time * log(franf()) / factor, x, y, z));
13111692Smckusick }
13211692Smckusick 
13311692Smckusick 
13411692Smckusick /*
13511692Smckusick **  Simplified reschedule routine
13611692Smckusick **
13711692Smckusick **	Parameters are the event index, the initial date, and the
13811692Smckusick **	division factor.  Look at the code to see what really happens.
13911692Smckusick */
14011692Smckusick 
14111692Smckusick xresched(e1, ev1, factor)
14211692Smckusick struct event	*e1;
14311692Smckusick int		ev1;
14411692Smckusick int		factor;
14511692Smckusick {
14611692Smckusick 	register int		ev;
14711692Smckusick 	register struct event	*e;
14811692Smckusick 
14911692Smckusick 	ev = ev1;
15011692Smckusick 	e = e1;
15111692Smckusick 	reschedule(e, -Param.eventdly[ev] * Param.time * log(franf()) / factor);
15211692Smckusick }
153