xref: /openbsd/games/trek/schedule.c (revision df930be7)
1 /*	$NetBSD: schedule.c,v 1.3 1995/04/22 10:59:23 cgd Exp $	*/
2 
3 /*
4  * Copyright (c) 1980, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)schedule.c	8.1 (Berkeley) 5/31/93";
39 #else
40 static char rcsid[] = "$NetBSD: schedule.c,v 1.3 1995/04/22 10:59:23 cgd Exp $";
41 #endif
42 #endif /* not lint */
43 
44 # include	"trek.h"
45 
46 /*
47 **  SCHEDULE AN EVENT
48 **
49 **	An event of type 'type' is scheduled for time NOW + 'offset'
50 **	into the first available slot.  'x', 'y', and 'z' are
51 **	considered the attributes for this event.
52 **
53 **	The address of the slot is returned.
54 */
55 
56 struct event *schedule(type, offset, x, y, z)
57 int	type;
58 double	offset;
59 char	x, y;
60 char	z;
61 {
62 	register struct event	*e;
63 	register int		i;
64 	double			date;
65 
66 	date = Now.date + offset;
67 	for (i = 0; i < MAXEVENTS; i++)
68 	{
69 		e = &Event[i];
70 		if (e->evcode)
71 			continue;
72 		/* got a slot */
73 #		ifdef xTRACE
74 		if (Trace)
75 			printf("schedule: type %d @ %.2f slot %d parm %d %d %d\n",
76 				type, date, i, x, y, z);
77 #		endif
78 		e->evcode = type;
79 		e->date = date;
80 		e->x = x;
81 		e->y = y;
82 		e->systemname = z;
83 		Now.eventptr[type] = e;
84 		return (e);
85 	}
86 	syserr("Cannot schedule event %d parm %d %d %d", type, x, y, z);
87 }
88 
89 
90 /*
91 **  RESCHEDULE AN EVENT
92 **
93 **	The event pointed to by 'e' is rescheduled to the current
94 **	time plus 'offset'.
95 */
96 
97 reschedule(e1, offset)
98 struct event	*e1;
99 double		offset;
100 {
101 	double			date;
102 	register struct event	*e;
103 
104 	e = e1;
105 
106 	date = Now.date + offset;
107 	e->date = date;
108 #	ifdef xTRACE
109 	if (Trace)
110 		printf("reschedule: type %d parm %d %d %d @ %.2f\n",
111 			e->evcode, e->x, e->y, e->systemname, date);
112 #	endif
113 	return;
114 }
115 
116 
117 /*
118 **  UNSCHEDULE AN EVENT
119 **
120 **	The event at slot 'e' is deleted.
121 */
122 
123 unschedule(e1)
124 struct event	*e1;
125 {
126 	register struct event	*e;
127 
128 	e = e1;
129 
130 #	ifdef xTRACE
131 	if (Trace)
132 		printf("unschedule: type %d @ %.2f parm %d %d %d\n",
133 			e->evcode, e->date, e->x, e->y, e->systemname);
134 #	endif
135 	Now.eventptr[e->evcode & E_EVENT] = 0;
136 	e->date = 1e50;
137 	e->evcode = 0;
138 	return;
139 }
140 
141 
142 /*
143 **  Abreviated schedule routine
144 **
145 **	Parameters are the event index and a factor for the time
146 **	figure.
147 */
148 
149 struct event *xsched(ev1, factor, x, y, z)
150 int	ev1;
151 int	factor;
152 int	x, y, z;
153 {
154 	register int	ev;
155 
156 	ev = ev1;
157 	return (schedule(ev, -Param.eventdly[ev] * Param.time * log(franf()) / factor, x, y, z));
158 }
159 
160 
161 /*
162 **  Simplified reschedule routine
163 **
164 **	Parameters are the event index, the initial date, and the
165 **	division factor.  Look at the code to see what really happens.
166 */
167 
168 xresched(e1, ev1, factor)
169 struct event	*e1;
170 int		ev1;
171 int		factor;
172 {
173 	register int		ev;
174 	register struct event	*e;
175 
176 	ev = ev1;
177 	e = e1;
178 	reschedule(e, -Param.eventdly[ev] * Param.time * log(franf()) / factor);
179 }
180