abort declaration is not just implicit but incompatibly implicit, so make it explicit.
[rogue-pphs.git] / daemon.c
1 /*
2 * Contains functions for dealing with things that happen in the
3 * future.
4 *
5 * @(#)daemon.c 3.3 (Berkeley) 6/15/81
6 *
7 * Rogue: Exploring the Dungeons of Doom
8 * Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
9 * All rights reserved.
10 *
11 * See the file LICENSE.TXT for full copyright and licensing information.
12 */
13
14 #include "curses.h"
15 #include "rogue.h"
16
17 #define EMPTY 0
18 #define DAEMON -1
19 #define MAXDAEMONS 20
20
21 #define _X_ { EMPTY }
22
23 struct delayed_action d_list[MAXDAEMONS] = {
24 _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_,
25 _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_,
26 };
27
28 /*
29 * d_slot:
30 * Find an empty slot in the daemon/fuse list
31 */
32 struct delayed_action *
33 d_slot()
34 {
35 register int i;
36 register struct delayed_action *dev;
37
38 for (i = 0, dev = d_list; i < MAXDAEMONS; i++, dev++)
39 if (dev->d_type == EMPTY)
40 return dev;
41 debug("Ran out of fuse slots");
42 return NULL;
43 }
44
45 /*
46 * find_slot:
47 * Find a particular slot in the table
48 */
49
50 struct delayed_action *
51 find_slot(func)
52 register int (*func)();
53 {
54 register int i;
55 register struct delayed_action *dev;
56
57 for (i = 0, dev = d_list; i < MAXDAEMONS; i++, dev++)
58 if (dev->d_type != EMPTY && func == dev->d_func)
59 return dev;
60 return NULL;
61 }
62
63 /*
64 * daemon:
65 * Start a daemon, takes a function.
66 */
67
68 start_daemon(func, arg, type)
69 int (*func)(), arg, type;
70 {
71 register struct delayed_action *dev;
72
73 dev = d_slot();
74
75 if (dev != NULL)
76 {
77 dev->d_type = type;
78 dev->d_func = func;
79 dev->d_arg = arg;
80 dev->d_time = DAEMON;
81 }
82 }
83
84 /*
85 * kill_daemon:
86 * Remove a daemon from the list
87 */
88
89 kill_daemon(func)
90 int (*func)();
91 {
92 register struct delayed_action *dev;
93
94 if ((dev = find_slot(func)) == NULL)
95 return;
96 /*
97 * Take it out of the list
98 */
99 dev->d_type = EMPTY;
100 }
101
102 /*
103 * do_daemons:
104 * Run all the daemons that are active with the current flag,
105 * passing the argument to the function.
106 */
107
108 do_daemons(flag)
109 register int flag;
110 {
111 register struct delayed_action *dev;
112
113 /*
114 * Loop through the devil list
115 */
116 for (dev = d_list; dev <= &d_list[MAXDAEMONS-1]; dev++)
117 /*
118 * Executing each one, giving it the proper arguments
119 */
120 if (dev->d_type == flag && dev->d_time == DAEMON)
121 (*dev->d_func)(dev->d_arg);
122 }
123
124 /*
125 * fuse:
126 * Start a fuse to go off in a certain number of turns
127 */
128
129 fuse(func, arg, time, type)
130 int (*func)(), arg, time, type;
131 {
132 register struct delayed_action *wire;
133
134 wire = d_slot();
135
136 if (wire != NULL)
137 {
138 wire->d_type = type;
139 wire->d_func = func;
140 wire->d_arg = arg;
141 wire->d_time = time;
142 }
143 }
144
145 /*
146 * lengthen:
147 * Increase the time until a fuse goes off
148 */
149
150 lengthen(func, xtime)
151 int (*func)();
152 int xtime;
153 {
154 register struct delayed_action *wire;
155
156 if ((wire = find_slot(func)) == NULL)
157 return;
158 wire->d_time += xtime;
159 }
160
161 /*
162 * extinguish:
163 * Put out a fuse
164 */
165
166 extinguish(func)
167 int (*func)();
168 {
169 register struct delayed_action *wire;
170
171 if ((wire = find_slot(func)) == NULL)
172 return;
173 wire->d_type = EMPTY;
174 }
175
176 /*
177 * do_fuses:
178 * Decrement counters and start needed fuses
179 */
180
181 do_fuses(flag)
182 register int flag;
183 {
184 register struct delayed_action *wire;
185
186 /*
187 * Step though the list
188 */
189 for (wire = d_list; wire <= &d_list[MAXDAEMONS-1]; wire++)
190 {
191 /*
192 * Decrementing counters and starting things we want. We also need
193 * to remove the fuse from the list once it has gone off.
194 */
195 if (flag == wire->d_type && wire->d_time > 0 && --wire->d_time == 0)
196 {
197 wire->d_type = EMPTY;
198 (*wire->d_func)(wire->d_arg);
199 }
200 }
201 }