comparison rogue3/daemon.c @ 0:527e2150eaf0

Import Rogue 3.6 from the Roguelike Restoration Project (r1490)
author edwarj4
date Tue, 13 Oct 2009 13:33:34 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:527e2150eaf0
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(void (*func)())
52 {
53 register int i;
54 register struct delayed_action *dev;
55
56 for (i = 0, dev = d_list; i < MAXDAEMONS; i++, dev++)
57 if (dev->d_type != EMPTY && func == dev->d_func)
58 return dev;
59 return NULL;
60 }
61
62 /*
63 * daemon:
64 * Start a daemon, takes a function.
65 */
66
67 void
68 start_daemon(void (*func)(), int arg, int type)
69 {
70 register struct delayed_action *dev;
71
72 dev = d_slot();
73
74 if (dev != NULL)
75 {
76 dev->d_type = type;
77 dev->d_func = func;
78 dev->d_arg = arg;
79 dev->d_time = DAEMON;
80 }
81 }
82
83 /*
84 * kill_daemon:
85 * Remove a daemon from the list
86 */
87
88 void
89 kill_daemon(void (*func)())
90 {
91 register struct delayed_action *dev;
92
93 if ((dev = find_slot(func)) == NULL)
94 return;
95 /*
96 * Take it out of the list
97 */
98 dev->d_type = EMPTY;
99 }
100
101 /*
102 * do_daemons:
103 * Run all the daemons that are active with the current flag,
104 * passing the argument to the function.
105 */
106
107 void
108 do_daemons(int flag)
109 {
110 register struct delayed_action *dev;
111
112 /*
113 * Loop through the devil list
114 */
115 for (dev = d_list; dev <= &d_list[MAXDAEMONS-1]; dev++)
116 /*
117 * Executing each one, giving it the proper arguments
118 */
119 if (dev->d_type == flag && dev->d_time == DAEMON)
120 (*dev->d_func)(dev->d_arg);
121 }
122
123 /*
124 * fuse:
125 * Start a fuse to go off in a certain number of turns
126 */
127
128 void
129 fuse(void (*func)(), int arg, int time, int type)
130 {
131 register struct delayed_action *wire;
132
133 wire = d_slot();
134
135 if (wire != NULL)
136 {
137 wire->d_type = type;
138 wire->d_func = func;
139 wire->d_arg = arg;
140 wire->d_time = time;
141 }
142 }
143
144 /*
145 * lengthen:
146 * Increase the time until a fuse goes off
147 */
148
149 void
150 lengthen(void (*func)(), int xtime)
151 {
152 register struct delayed_action *wire;
153
154 if ((wire = find_slot(func)) == NULL)
155 return;
156 wire->d_time += xtime;
157 }
158
159 /*
160 * extinguish:
161 * Put out a fuse
162 */
163
164 void
165 extinguish(void (*func)())
166 {
167 register struct delayed_action *wire;
168
169 if ((wire = find_slot(func)) == NULL)
170 return;
171 wire->d_type = EMPTY;
172 }
173
174 /*
175 * do_fuses:
176 * Decrement counters and start needed fuses
177 */
178
179 void
180 do_fuses(int flag)
181 {
182 register struct delayed_action *wire;
183
184 /*
185 * Step though the list
186 */
187 for (wire = d_list; wire <= &d_list[MAXDAEMONS-1]; wire++)
188 {
189 /*
190 * Decrementing counters and starting things we want. We also need
191 * to remove the fuse from the list once it has gone off.
192 */
193 if (flag == wire->d_type && wire->d_time > 0 && --wire->d_time == 0)
194 {
195 wire->d_type = EMPTY;
196 (*wire->d_func)(wire->d_arg);
197 }
198 }
199 }