comparison rogue5/daemon.c @ 33:f502bf60e6e4

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