xref: /netbsd/games/trek/schedule.c (revision bf9ec67e)
1 /*	$NetBSD: schedule.c,v 1.4 1997/10/12 21:25:11 christos 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 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)schedule.c	8.1 (Berkeley) 5/31/93";
40 #else
41 __RCSID("$NetBSD: schedule.c,v 1.4 1997/10/12 21:25:11 christos Exp $");
42 #endif
43 #endif /* not lint */
44 
45 #include <stdio.h>
46 #include <math.h>
47 #include <err.h>
48 #include "trek.h"
49 
50 /*
51 **  SCHEDULE AN EVENT
52 **
53 **	An event of type 'type' is scheduled for time NOW + 'offset'
54 **	into the first available slot.  'x', 'y', and 'z' are
55 **	considered the attributes for this event.
56 **
57 **	The address of the slot is returned.
58 */
59 
60 struct event *schedule(type, offset, x, y, z)
61 int	type;
62 double	offset;
63 char	x, y;
64 char	z;
65 {
66 	struct event	*e;
67 	int		i;
68 	double			date;
69 
70 	date = Now.date + offset;
71 	for (i = 0; i < MAXEVENTS; i++)
72 	{
73 		e = &Event[i];
74 		if (e->evcode)
75 			continue;
76 		/* got a slot */
77 #		ifdef xTRACE
78 		if (Trace)
79 			printf("schedule: type %d @ %.2f slot %d parm %d %d %d\n",
80 				type, date, i, x, y, z);
81 #		endif
82 		e->evcode = type;
83 		e->date = date;
84 		e->x = x;
85 		e->y = y;
86 		e->systemname = z;
87 		Now.eventptr[type] = e;
88 		return (e);
89 	}
90 	errx(1, "Cannot schedule event %d parm %d %d %d", type, x, y, z);
91 }
92 
93 
94 /*
95 **  RESCHEDULE AN EVENT
96 **
97 **	The event pointed to by 'e' is rescheduled to the current
98 **	time plus 'offset'.
99 */
100 
101 void
102 reschedule(e1, offset)
103 struct event	*e1;
104 double		offset;
105 {
106 	double			date;
107 	struct event	*e;
108 
109 	e = e1;
110 
111 	date = Now.date + offset;
112 	e->date = date;
113 #	ifdef xTRACE
114 	if (Trace)
115 		printf("reschedule: type %d parm %d %d %d @ %.2f\n",
116 			e->evcode, e->x, e->y, e->systemname, date);
117 #	endif
118 	return;
119 }
120 
121 
122 /*
123 **  UNSCHEDULE AN EVENT
124 **
125 **	The event at slot 'e' is deleted.
126 */
127 
128 void
129 unschedule(e1)
130 struct event	*e1;
131 {
132 	struct event	*e;
133 
134 	e = e1;
135 
136 #	ifdef xTRACE
137 	if (Trace)
138 		printf("unschedule: type %d @ %.2f parm %d %d %d\n",
139 			e->evcode, e->date, e->x, e->y, e->systemname);
140 #	endif
141 	Now.eventptr[e->evcode & E_EVENT] = 0;
142 	e->date = 1e50;
143 	e->evcode = 0;
144 	return;
145 }
146 
147 
148 /*
149 **  Abreviated schedule routine
150 **
151 **	Parameters are the event index and a factor for the time
152 **	figure.
153 */
154 
155 struct event *xsched(ev1, factor, x, y, z)
156 int	ev1;
157 int	factor;
158 int	x, y, z;
159 {
160 	int	ev;
161 
162 	ev = ev1;
163 	return (schedule(ev, -Param.eventdly[ev] * Param.time * log(franf()) / factor, x, y, z));
164 }
165 
166 
167 /*
168 **  Simplified reschedule routine
169 **
170 **	Parameters are the event index, the initial date, and the
171 **	division factor.  Look at the code to see what really happens.
172 */
173 
174 void
175 xresched(e1, ev1, factor)
176 struct event	*e1;
177 int		ev1;
178 int		factor;
179 {
180 	int		ev;
181 	struct event	*e;
182 
183 	ev = ev1;
184 	e = e1;
185 	reschedule(e, -Param.eventdly[ev] * Param.time * log(franf()) / factor);
186 }
187