comparison rogue4/daemon.c @ 12:9535a08ddc39

Import Rogue 5.2 from the Roguelike Restoration Project (r1490)
author edwarj4
date Sat, 24 Oct 2009 16:52:52 +0000
parents
children f2951c4e28d9
comparison
equal deleted inserted replaced
11:949d558c2162 12:9535a08ddc39
1 /*
2 * Contains functions for dealing with things that happen in the
3 * future.
4 *
5 * @(#)daemon.c 4.4 (Berkeley) 1/12/82
6 *
7 * Rogue: Exploring the Dungeons of Doom
8 * Copyright (C) 1980, 1981, 1982 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
20 #define _X_ { EMPTY }
21
22 struct delayed_action d_list[MAXDAEMONS] = {
23 _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_,
24 _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_,
25 };
26
27 /*
28 * d_slot:
29 * Find an empty slot in the daemon/fuse list
30 */
31 struct delayed_action *
32 d_slot()
33 {
34 register int i;
35 register struct delayed_action *dev;
36
37 for (i = 0, dev = d_list; i < MAXDAEMONS; i++, dev++)
38 if (dev->d_type == EMPTY)
39 return dev;
40 #ifdef WIZARD
41 debug("Ran out of fuse slots");
42 #endif
43 return NULL;
44 }
45
46 /*
47 * find_slot:
48 * Find a particular slot in the table
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 daemon(func, arg, type)
68 int (*func)(), arg, type;
69 {
70 register struct delayed_action *dev;
71
72 dev = d_slot();
73 dev->d_type = type;
74 dev->d_func = func;
75 dev->d_arg = arg;
76 dev->d_time = DAEMON;
77 }
78
79 /*
80 * kill_daemon:
81 * Remove a daemon from the list
82 */
83 kill_daemon(func)
84 int (*func)();
85 {
86 register struct delayed_action *dev;
87
88 if ((dev = find_slot(func)) == NULL)
89 return;
90 /*
91 * Take it out of the list
92 */
93 dev->d_type = EMPTY;
94 }
95
96 /*
97 * do_daemons:
98 * Run all the daemons that are active with the current flag,
99 * passing the argument to the function.
100 */
101 do_daemons(flag)
102 register int flag;
103 {
104 register struct delayed_action *dev;
105
106 /*
107 * Loop through the devil list
108 */
109 for (dev = d_list; dev <= &d_list[MAXDAEMONS-1]; dev++)
110 /*
111 * Executing each one, giving it the proper arguments
112 */
113 if (dev->d_type == flag && dev->d_time == DAEMON)
114 (*dev->d_func)(dev->d_arg);
115 }
116
117 /*
118 * fuse:
119 * Start a fuse to go off in a certain number of turns
120 */
121 fuse(func, arg, time, type)
122 int (*func)(), arg, time, type;
123 {
124 register struct delayed_action *wire;
125
126 wire = d_slot();
127 wire->d_type = type;
128 wire->d_func = func;
129 wire->d_arg = arg;
130 wire->d_time = time;
131 }
132
133 /*
134 * lengthen:
135 * Increase the time until a fuse goes off
136 */
137 lengthen(func, xtime)
138 int (*func)();
139 int xtime;
140 {
141 register struct delayed_action *wire;
142
143 if ((wire = find_slot(func)) == NULL)
144 return;
145 wire->d_time += xtime;
146 }
147
148 /*
149 * extinguish:
150 * Put out a fuse
151 */
152 extinguish(func)
153 int (*func)();
154 {
155 register struct delayed_action *wire;
156
157 if ((wire = find_slot(func)) == NULL)
158 return;
159 wire->d_type = EMPTY;
160 }
161
162 /*
163 * do_fuses:
164 * Decrement counters and start needed fuses
165 */
166 do_fuses(flag)
167 register int flag;
168 {
169 register struct delayed_action *wire;
170
171 /*
172 * Step though the list
173 */
174 for (wire = d_list; wire <= &d_list[MAXDAEMONS-1]; wire++)
175 {
176 /*
177 * Decrementing counters and starting things we want. We also need
178 * to remove the fuse from the list once it has gone off.
179 */
180 if (flag == wire->d_type && wire->d_time > 0 && --wire->d_time == 0)
181 {
182 wire->d_type = EMPTY;
183 (*wire->d_func)(wire->d_arg);
184 }
185 }
186 }