Annotation of early-roguelike/arogue5/daemon.c, Revision 1.1.1.1
1.1 rubenllo 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(void)
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(void)
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(void (*func)())
74: {
75: reg int i;
76: reg struct delayed_action *dev;
77:
78: for (i = 0, dev = f_list; i < MAXFUSES; i++, dev++)
79: if (dev->d_type != EMPTY && func == dev->d_func)
80: return dev;
81: return NULL;
82: }
83:
84:
85: /*
86: * start_daemon:
87: * Start a daemon, takes a function.
88: */
89: void
90: start_daemon(void (*func)(), void *arg, int type)
91: {
92: reg struct delayed_action *dev;
93:
94: dev = d_slot();
95: if (dev != NULL) {
96: dev->d_type = type;
97: dev->d_func = func;
98: dev->d_arg = arg;
99: dev->d_time = DAEMON;
100: demoncnt += 1; /* update count */
101: }
102: }
103:
104:
105: /*
106: * kill_daemon:
107: * Remove a daemon from the list
108: */
109: void
110: kill_daemon(void (*func)())
111: {
112: reg struct delayed_action *dev;
113: reg int i;
114:
115: for (i = 0, dev = d_list; i < MAXDAEMONS; i++, dev++) {
116: if (dev->d_type != EMPTY && func == dev->d_func)
117: break;
118: }
119: if (i >= MAXDAEMONS) return; /* if not found, forget it */
120: /*
121: * Take it out of the list
122: */
123: dev->d_type = EMPTY;
124: dev->d_arg = NULL;
125: dev->d_func = NULL;
126: dev->d_time = 0;
127: demoncnt -= 1; /* update count */
128: }
129:
130:
131: /*
132: * do_daemons:
133: * Run all the daemons that are active with the current flag,
134: * passing the argument to the function.
135: */
136: void
137: do_daemons(int flag)
138: {
139: reg struct delayed_action *dev;
140:
141: /*
142: * Loop through the devil list
143: */
144: for (dev = d_list; dev <= &d_list[MAXDAEMONS-1]; dev++)
145: /*
146: * Executing each one, giving it the proper arguments
147: */
148: if (dev->d_func != NULL && dev->d_type == flag && dev->d_time == DAEMON)
149: (*dev->d_func)(dev->d_arg);
150: }
151:
152:
153: /*
154: * fuse:
155: * Start a fuse to go off in a certain number of turns
156: */
157: void
158: fuse(void (*func)(), void *arg, int time, int type)
159: {
160: reg struct delayed_action *wire;
161:
162: wire = f_slot();
163: if (wire != NULL) {
164: wire->d_type = type;
165: wire->d_func = func;
166: wire->d_arg = arg;
167: wire->d_time = time;
168: fusecnt += 1; /* update count */
169: }
170: }
171:
172:
173: /*
174: * lengthen:
175: * Increase the time until a fuse goes off
176: */
177: void
178: lengthen(void (*func)(), int xtime)
179: {
180: reg struct delayed_action *wire;
181:
182: if ((wire = find_slot(func)) == NULL)
183: return;
184: wire->d_time += xtime;
185: }
186:
187:
188: /*
189: * extinguish:
190: * Put out a fuse
191: */
192: void
193: extinguish(void (*func)())
194: {
195: reg struct delayed_action *wire;
196:
197: if ((wire = find_slot(func)) == NULL)
198: return;
199: wire->d_type = EMPTY;
200: wire->d_func = NULL;
201: wire->d_arg = NULL;
202: wire->d_time = 0;
203: fusecnt -= 1;
204: }
205:
206:
207: /*
208: * do_fuses:
209: * Decrement counters and start needed fuses
210: */
211: void
212: do_fuses(int flag)
213: {
214: reg struct delayed_action *wire;
215:
216: /*
217: * Step though the list
218: */
219: for (wire = f_list; wire <= &f_list[MAXFUSES-1]; wire++) {
220: /*
221: * Decrementing counters and starting things we want. We also need
222: * to remove the fuse from the list once it has gone off.
223: */
224: if(flag == wire->d_type && wire->d_time > 0 &&
225: --wire->d_time == 0) {
226: wire->d_type = EMPTY;
227: if (wire->d_func != NULL)
228: (*wire->d_func)(wire->d_arg);
229: fusecnt -= 1;
230: }
231: }
232: }
233:
234: /*
235: * activity:
236: * Show wizard number of demaons and memory blocks used
237: */
238: void
239: activity(void)
240: {
241: sprintf(outstring,"Daemons = %d : Fuses = %d : Memory Items = %d : Memory Used = %ld",
242: demoncnt,fusecnt,total,md_memused());
243: msg(outstring);
244: }
CVSweb