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