comparison arogue5/daemon.c @ 63:0ed67132cf10

Import Advanced Rogue 5.8 from the Roguelike Restoration Project (r1490)
author elwin
date Thu, 09 Aug 2012 22:58:48 +0000
parents
children f2951c4e28d9
comparison
equal deleted inserted replaced
62:0ef99244acb8 63:0ed67132cf10
1 /*
2 * Contains functions for dealing with things that happen in the future.
3 *
4 * Advanced Rogue
5 * Copyright (C) 1984, 1985 Michael Morgan, Ken Dalka and AT&T
6 * All rights reserved.
7 *
8 * Based on "Rogue: Exploring the Dungeons of Doom"
9 * Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
10 * All rights reserved.
11 *
12 * See the file LICENSE.TXT for full copyright and licensing information.
13 */
14
15 #include "curses.h"
16 #include "rogue.h"
17
18 #define EMPTY 0
19 #define DAEMON -1
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 };
26 struct delayed_action f_list[MAXFUSES] = {
27 _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_,
28 _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_
29 };
30 int demoncnt = 0; /* number of active daemons */
31 int fusecnt = 0;
32
33
34 /*
35 * d_slot:
36 * Find an empty slot in the daemon list
37 */
38 struct delayed_action *
39 d_slot()
40 {
41 reg int i;
42 reg struct delayed_action *dev;
43
44 for (i = 0, dev = d_list; i < MAXDAEMONS; i++, dev++)
45 if (dev->d_type == EMPTY)
46 return dev;
47 return NULL;
48 }
49
50 /*
51 * f_slot:
52 * Find an empty slot in the fuses list
53 */
54 struct delayed_action *
55 f_slot()
56 {
57 reg int i;
58 reg struct delayed_action *dev;
59
60 for (i = 0, dev = f_list; i < MAXFUSES; i++, dev++)
61 if (dev->d_type == EMPTY)
62 return dev;
63 return NULL;
64 }
65
66
67
68 /*
69 * find_slot:
70 * Find a particular slot in the table
71 */
72 struct delayed_action *
73 find_slot(func)
74 reg int (*func)();
75 {
76 reg int i;
77 reg struct delayed_action *dev;
78
79 for (i = 0, dev = f_list; i < MAXFUSES; i++, dev++)
80 if (dev->d_type != EMPTY && func == dev->d_func)
81 return dev;
82 return NULL;
83 }
84
85
86 /*
87 * daemon:
88 * Start a daemon, takes a function.
89 */
90 daemon(func, arg, type)
91 reg int arg, type, (*func)();
92 {
93 reg struct delayed_action *dev;
94
95 dev = d_slot();
96 if (dev != NULL) {
97 dev->d_type = type;
98 dev->d_func = func;
99 dev->d_arg = arg;
100 dev->d_time = DAEMON;
101 demoncnt += 1; /* update count */
102 }
103 }
104
105
106 /*
107 * kill_daemon:
108 * Remove a daemon from the list
109 */
110 kill_daemon(func)
111 reg int (*func)();
112 {
113 reg struct delayed_action *dev;
114 reg int i;
115
116 for (i = 0, dev = d_list; i < MAXDAEMONS; i++, dev++) {
117 if (dev->d_type != EMPTY && func == dev->d_func)
118 break;
119 }
120 if (i >= MAXDAEMONS) return; /* if not found, forget it */
121 /*
122 * Take it out of the list
123 */
124 dev->d_type = EMPTY;
125 dev->d_arg = 0;
126 dev->d_func = NULL;
127 dev->d_time = 0;
128 demoncnt -= 1; /* update count */
129 }
130
131
132 /*
133 * do_daemons:
134 * Run all the daemons that are active with the current flag,
135 * passing the argument to the function.
136 */
137 do_daemons(flag)
138 reg int flag;
139 {
140 reg struct delayed_action *dev;
141
142 /*
143 * Loop through the devil list
144 */
145 for (dev = d_list; dev <= &d_list[MAXDAEMONS-1]; dev++)
146 /*
147 * Executing each one, giving it the proper arguments
148 */
149 if (dev->d_func != NULL && dev->d_type == flag && dev->d_time == DAEMON)
150 (*dev->d_func)(dev->d_arg);
151 }
152
153
154 /*
155 * fuse:
156 * Start a fuse to go off in a certain number of turns
157 */
158 fuse(func, arg, time, type)
159 reg int (*func)(), arg, time, type;
160 {
161 reg struct delayed_action *wire;
162
163 wire = f_slot();
164 if (wire != NULL) {
165 wire->d_type = type;
166 wire->d_func = func;
167 wire->d_arg = arg;
168 wire->d_time = time;
169 fusecnt += 1; /* update count */
170 }
171 }
172
173
174 /*
175 * lengthen:
176 * Increase the time until a fuse goes off
177 */
178 lengthen(func, xtime)
179 reg int (*func)(), xtime;
180 {
181 reg struct delayed_action *wire;
182
183 if ((wire = find_slot(func)) == NULL)
184 return;
185 wire->d_time += xtime;
186 }
187
188
189 /*
190 * extinguish:
191 * Put out a fuse
192 */
193 extinguish(func)
194 reg int (*func)();
195 {
196 reg struct delayed_action *wire;
197
198 if ((wire = find_slot(func)) == NULL)
199 return;
200 wire->d_type = EMPTY;
201 wire->d_func = NULL;
202 wire->d_arg = 0;
203 wire->d_time = 0;
204 fusecnt -= 1;
205 }
206
207
208 /*
209 * do_fuses:
210 * Decrement counters and start needed fuses
211 */
212 do_fuses(flag)
213 reg int flag;
214 {
215 reg struct delayed_action *wire;
216
217 /*
218 * Step though the list
219 */
220 for (wire = f_list; wire <= &f_list[MAXFUSES-1]; wire++) {
221 /*
222 * Decrementing counters and starting things we want. We also need
223 * to remove the fuse from the list once it has gone off.
224 */
225 if(flag == wire->d_type && wire->d_time > 0 &&
226 --wire->d_time == 0) {
227 wire->d_type = EMPTY;
228 if (wire->d_func != NULL)
229 (*wire->d_func)(wire->d_arg);
230 fusecnt -= 1;
231 }
232 }
233 }
234
235 /*
236 * activity:
237 * Show wizard number of demaons and memory blocks used
238 */
239 activity()
240 {
241 sprintf(outstring,"Daemons = %d : Fuses = %d : Memory Items = %d : Memory Used = %d",
242 demoncnt,fusecnt,total,md_memused());
243 msg(outstring);
244 }