Annotation of early-roguelike/rogue4/daemon.c, Revision 1.1.1.1
1.1 rubenllo 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(void)
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(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: * start_daemon:
64: * Start a daemon, takes a function.
65: */
66: void
67: start_daemon(void (*func)(), int arg, int type)
68: {
69: register struct delayed_action *dev;
70:
71: dev = d_slot();
72: dev->d_type = type;
73: dev->d_func = func;
74: dev->d_arg = arg;
75: dev->d_time = DAEMON;
76: }
77:
78: /*
79: * kill_daemon:
80: * Remove a daemon from the list
81: */
82: void
83: kill_daemon(void (*func)())
84: {
85: register struct delayed_action *dev;
86:
87: if ((dev = find_slot(func)) == NULL)
88: return;
89: /*
90: * Take it out of the list
91: */
92: dev->d_type = EMPTY;
93: }
94:
95: /*
96: * do_daemons:
97: * Run all the daemons that are active with the current flag,
98: * passing the argument to the function.
99: */
100: void
101: do_daemons(int flag)
102: {
103: register struct delayed_action *dev;
104:
105: /*
106: * Loop through the devil list
107: */
108: for (dev = d_list; dev <= &d_list[MAXDAEMONS-1]; dev++)
109: /*
110: * Executing each one, giving it the proper arguments
111: */
112: if (dev->d_type == flag && dev->d_time == DAEMON)
113: (*dev->d_func)(dev->d_arg);
114: }
115:
116: /*
117: * fuse:
118: * Start a fuse to go off in a certain number of turns
119: */
120: void
121: fuse(void (*func)(), int arg, int time, int type)
122: {
123: register struct delayed_action *wire;
124:
125: wire = d_slot();
126: wire->d_type = type;
127: wire->d_func = func;
128: wire->d_arg = arg;
129: wire->d_time = time;
130: }
131:
132: /*
133: * lengthen:
134: * Increase the time until a fuse goes off
135: */
136: void
137: lengthen(void (*func)(), int xtime)
138: {
139: register struct delayed_action *wire;
140:
141: if ((wire = find_slot(func)) == NULL)
142: return;
143: wire->d_time += xtime;
144: }
145:
146: /*
147: * extinguish:
148: * Put out a fuse
149: */
150: void
151: extinguish(void (*func)())
152: {
153: register struct delayed_action *wire;
154:
155: if ((wire = find_slot(func)) == NULL)
156: return;
157: wire->d_type = EMPTY;
158: }
159:
160: /*
161: * do_fuses:
162: * Decrement counters and start needed fuses
163: */
164: void
165: do_fuses(int flag)
166: {
167: register struct delayed_action *wire;
168:
169: /*
170: * Step though the list
171: */
172: for (wire = d_list; wire <= &d_list[MAXDAEMONS-1]; wire++)
173: {
174: /*
175: * Decrementing counters and starting things we want. We also need
176: * to remove the fuse from the list once it has gone off.
177: */
178: if (flag == wire->d_type && wire->d_time > 0 && --wire->d_time == 0)
179: {
180: wire->d_type = EMPTY;
181: (*wire->d_func)(wire->d_arg);
182: }
183: }
184: }
CVSweb