xref: /original-bsd/games/trek/schedule.c (revision a910c8b7)
1 #ifndef lint
2 static char sccsid[] = "@(#)schedule.c	4.2	(Berkeley)	05/27/83";
3 #endif not lint
4 
5 # include	"trek.h"
6 
7 /*
8 **  SCHEDULE AN EVENT
9 **
10 **	An event of type 'type' is scheduled for time NOW + 'offset'
11 **	into the first available slot.  'x', 'y', and 'z' are
12 **	considered the attributes for this event.
13 **
14 **	The address of the slot is returned.
15 */
16 
17 struct event *schedule(type, offset, x, y, z)
18 int	type;
19 double	offset;
20 char	x, y;
21 char	z;
22 {
23 	register struct event	*e;
24 	register int		i;
25 	double			date;
26 
27 	date = Now.date + offset;
28 	for (i = 0; i < MAXEVENTS; i++)
29 	{
30 		e = &Event[i];
31 		if (e->evcode)
32 			continue;
33 		/* got a slot */
34 #		ifdef xTRACE
35 		if (Trace)
36 			printf("schedule: type %d @ %.2f slot %d parm %d %d %d\n",
37 				type, date, i, x, y, z);
38 #		endif
39 		e->evcode = type;
40 		e->date = date;
41 		e->x = x;
42 		e->y = y;
43 		e->systemname = z;
44 		Now.eventptr[type] = e;
45 		return (e);
46 	}
47 	syserr("Cannot schedule event %d parm %d %d %d", type, x, y, z);
48 }
49 
50 
51 /*
52 **  RESCHEDULE AN EVENT
53 **
54 **	The event pointed to by 'e' is rescheduled to the current
55 **	time plus 'offset'.
56 */
57 
58 reschedule(e1, offset)
59 struct event	*e1;
60 double		offset;
61 {
62 	double			date;
63 	register struct event	*e;
64 
65 	e = e1;
66 
67 	date = Now.date + offset;
68 	e->date = date;
69 #	ifdef xTRACE
70 	if (Trace)
71 		printf("reschedule: type %d parm %d %d %d @ %.2f\n",
72 			e->evcode, e->x, e->y, e->systemname, date);
73 #	endif
74 	return;
75 }
76 
77 
78 /*
79 **  UNSCHEDULE AN EVENT
80 **
81 **	The event at slot 'e' is deleted.
82 */
83 
84 unschedule(e1)
85 struct event	*e1;
86 {
87 	register struct event	*e;
88 
89 	e = e1;
90 
91 #	ifdef xTRACE
92 	if (Trace)
93 		printf("unschedule: type %d @ %.2f parm %d %d %d\n",
94 			e->evcode, e->date, e->x, e->y, e->systemname);
95 #	endif
96 	Now.eventptr[e->evcode & E_EVENT] = 0;
97 	e->date = 1e50;
98 	e->evcode = 0;
99 	return;
100 }
101 
102 
103 /*
104 **  Abreviated schedule routine
105 **
106 **	Parameters are the event index and a factor for the time
107 **	figure.
108 */
109 
110 struct event *xsched(ev1, factor, x, y, z)
111 int	ev1;
112 int	factor;
113 int	x, y, z;
114 {
115 	register int	ev;
116 
117 	ev = ev1;
118 	return (schedule(ev, -Param.eventdly[ev] * Param.time * log(franf()) / factor, x, y, z));
119 }
120 
121 
122 /*
123 **  Simplified reschedule routine
124 **
125 **	Parameters are the event index, the initial date, and the
126 **	division factor.  Look at the code to see what really happens.
127 */
128 
129 xresched(e1, ev1, factor)
130 struct event	*e1;
131 int		ev1;
132 int		factor;
133 {
134 	register int		ev;
135 	register struct event	*e;
136 
137 	ev = ev1;
138 	e = e1;
139 	reschedule(e, -Param.eventdly[ev] * Param.time * log(franf()) / factor);
140 }
141