note other reasons for pclose to return -1; wait(2) -> wait4(2)
[unix-history] / usr / src / games / trek / schedule.c
CommitLineData
bb0cfa24
DF
1/*
2 * Copyright (c) 1980 Regents of the University of California.
e9fb6bea
KB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
d99e6414
KB
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
bb0cfa24
DF
16 */
17
b8401192 18#ifndef lint
d99e6414 19static char sccsid[] = "@(#)schedule.c 5.3 (Berkeley) %G%";
e9fb6bea 20#endif /* not lint */
b8401192
KM
21
22# include "trek.h"
23
24/*
25** SCHEDULE AN EVENT
26**
27** An event of type 'type' is scheduled for time NOW + 'offset'
28** into the first available slot. 'x', 'y', and 'z' are
29** considered the attributes for this event.
30**
31** The address of the slot is returned.
32*/
33
34struct event *schedule(type, offset, x, y, z)
35int type;
06d69904 36double offset;
b8401192
KM
37char x, y;
38char z;
39{
40 register struct event *e;
41 register int i;
06d69904 42 double date;
b8401192
KM
43
44 date = Now.date + offset;
45 for (i = 0; i < MAXEVENTS; i++)
46 {
47 e = &Event[i];
48 if (e->evcode)
49 continue;
50 /* got a slot */
51# ifdef xTRACE
52 if (Trace)
53 printf("schedule: type %d @ %.2f slot %d parm %d %d %d\n",
54 type, date, i, x, y, z);
55# endif
56 e->evcode = type;
57 e->date = date;
58 e->x = x;
59 e->y = y;
60 e->systemname = z;
61 Now.eventptr[type] = e;
62 return (e);
63 }
64 syserr("Cannot schedule event %d parm %d %d %d", type, x, y, z);
65}
66
67
68/*
69** RESCHEDULE AN EVENT
70**
71** The event pointed to by 'e' is rescheduled to the current
72** time plus 'offset'.
73*/
74
75reschedule(e1, offset)
76struct event *e1;
06d69904 77double offset;
b8401192 78{
06d69904 79 double date;
b8401192
KM
80 register struct event *e;
81
82 e = e1;
83
84 date = Now.date + offset;
85 e->date = date;
86# ifdef xTRACE
87 if (Trace)
88 printf("reschedule: type %d parm %d %d %d @ %.2f\n",
89 e->evcode, e->x, e->y, e->systemname, date);
90# endif
91 return;
92}
93
94
95/*
96** UNSCHEDULE AN EVENT
97**
98** The event at slot 'e' is deleted.
99*/
100
101unschedule(e1)
102struct event *e1;
103{
104 register struct event *e;
105
106 e = e1;
107
108# ifdef xTRACE
109 if (Trace)
110 printf("unschedule: type %d @ %.2f parm %d %d %d\n",
111 e->evcode, e->date, e->x, e->y, e->systemname);
112# endif
113 Now.eventptr[e->evcode & E_EVENT] = 0;
114 e->date = 1e50;
115 e->evcode = 0;
116 return;
117}
118
119
120/*
121** Abreviated schedule routine
122**
123** Parameters are the event index and a factor for the time
124** figure.
125*/
126
127struct event *xsched(ev1, factor, x, y, z)
128int ev1;
129int factor;
130int x, y, z;
131{
132 register int ev;
133
134 ev = ev1;
135 return (schedule(ev, -Param.eventdly[ev] * Param.time * log(franf()) / factor, x, y, z));
136}
137
138
139/*
140** Simplified reschedule routine
141**
142** Parameters are the event index, the initial date, and the
143** division factor. Look at the code to see what really happens.
144*/
145
146xresched(e1, ev1, factor)
147struct event *e1;
148int ev1;
149int factor;
150{
151 register int ev;
152 register struct event *e;
153
154 ev = ev1;
155 e = e1;
156 reschedule(e, -Param.eventdly[ev] * Param.time * log(franf()) / factor);
157}