xref: /netbsd-src/games/trek/schedule.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: schedule.c,v 1.6 2006/03/19 00:56:12 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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)schedule.c	8.1 (Berkeley) 5/31/93";
36 #else
37 __RCSID("$NetBSD: schedule.c,v 1.6 2006/03/19 00:56:12 christos Exp $");
38 #endif
39 #endif /* not lint */
40 
41 #include <stdio.h>
42 #include <math.h>
43 #include <err.h>
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 	struct event	*e;
63 	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_GHOST] = e;
84 		return (e);
85 	}
86 	errx(1, "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 void
98 reschedule(e1, offset)
99 struct event	*e1;
100 double		offset;
101 {
102 	double			date;
103 	struct event	*e;
104 
105 	e = e1;
106 
107 	date = Now.date + offset;
108 	e->date = date;
109 #	ifdef xTRACE
110 	if (Trace)
111 		printf("reschedule: type %d parm %d %d %d @ %.2f\n",
112 			e->evcode, e->x, e->y, e->systemname, date);
113 #	endif
114 	return;
115 }
116 
117 
118 /*
119 **  UNSCHEDULE AN EVENT
120 **
121 **	The event at slot 'e' is deleted.
122 */
123 
124 void
125 unschedule(e1)
126 struct event	*e1;
127 {
128 	struct event	*e;
129 
130 	e = e1;
131 
132 #	ifdef xTRACE
133 	if (Trace)
134 		printf("unschedule: type %d @ %.2f parm %d %d %d\n",
135 			e->evcode, e->date, e->x, e->y, e->systemname);
136 #	endif
137 	Now.eventptr[e->evcode & E_EVENT] = 0;
138 	e->date = 1e50;
139 	e->evcode = 0;
140 	return;
141 }
142 
143 
144 /*
145 **  Abreviated schedule routine
146 **
147 **	Parameters are the event index and a factor for the time
148 **	figure.
149 */
150 
151 struct event *xsched(ev1, factor, x, y, z)
152 int	ev1;
153 int	factor;
154 int	x, y, z;
155 {
156 	int	ev;
157 
158 	ev = ev1;
159 	return (schedule(ev, -Param.eventdly[ev] * Param.time * log(franf()) / factor, x, y, z));
160 }
161 
162 
163 /*
164 **  Simplified reschedule routine
165 **
166 **	Parameters are the event index, the initial date, and the
167 **	division factor.  Look at the code to see what really happens.
168 */
169 
170 void
171 xresched(e1, ev1, factor)
172 struct event	*e1;
173 int		ev1;
174 int		factor;
175 {
176 	int		ev;
177 	struct event	*e;
178 
179 	ev = ev1;
180 	e = e1;
181 	reschedule(e, -Param.eventdly[ev] * Param.time * log(franf()) / factor);
182 }
183