Annotation of early-roguelike/srogue/daemon.c, Revision 1.1.1.1
1.1 rubenllo 1: /*
2: * Contains functions for dealing with things that
3: * happen in the future.
4: *
5: * @(#)daemon.c 9.0 (rdk) 7/17/84
6: *
7: * Super-Rogue
8: * Copyright (C) 1984 Robert D. Kindelberger
9: * All rights reserved.
10: *
11: * Based on "Rogue: Exploring the Dungeons of Doom"
12: * Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
13: * All rights reserved.
14: *
15: * See the file LICENSE.TXT for full copyright and licensing information.
16: */
17:
18: #include "rogue.h"
19: #include "rogue.ext"
20:
21: #define EMPTY 0
22: #define DAEMON -1
23:
24: #define _X_ { 0, 0, 0, 0 }
25:
26: struct delayed_action d_list[MAXDAEMONS] = {
27: _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_,
28: _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_,
29: };
30:
31:
32: /*
33: * d_insert:
34: * Insert a function in the daemon list.
35: */
36: struct delayed_action *
37: d_insert(void (*func)(), int arg, int type, int time)
38: {
39: reg struct delayed_action *dev;
40:
41: if (demoncnt < MAXDAEMONS) {
42: dev = &d_list[demoncnt];
43: dev->d_type = type;
44: dev->d_time = time;
45: dev->d_func = func;
46: dev->d_arg = arg;
47: demoncnt += 1;
48: return dev;
49: }
50: return NULL;
51: }
52:
53: void
54: d_delete(struct delayed_action *wire)
55: {
56: reg struct delayed_action *d1, *d2;
57:
58: for (d1 = d_list; d1 < &d_list[demoncnt]; d1++) {
59: if (wire == d1) {
60: for (d2 = d1 + 1; d2 < &d_list[demoncnt]; d2++)
61: *d1++ = *d2;
62: demoncnt -= 1;
63: d1 = &d_list[demoncnt];
64: d1->d_type = EMPTY;
65: d1->d_func = EMPTY;
66: return;
67: }
68: }
69: }
70: /*
71: * find_slot:
72: * Find a particular slot in the table
73: */
74: struct delayed_action *
75: find_slot(void (*func)())
76: {
77: reg struct delayed_action *dev;
78:
79: for (dev = d_list; dev < &d_list[demoncnt]; dev++)
80: if (dev->d_type != EMPTY && func == dev->d_func)
81: return dev;
82: return NULL;
83: }
84:
85: /*
86: * start_daemon:
87: * Start a daemon, takes a function.
88: */
89: void
90: start_daemon(void (*func)(), int arg, int type)
91: {
92: d_insert(func, arg, type, DAEMON);
93: }
94:
95: /*
96: * do_daemons:
97: * Run all the daemons that are active with the current
98: * flag, passing the argument to the function.
99: */
100: void
101: do_daemons(int flag)
102: {
103: reg struct delayed_action *dev;
104:
105: for (dev = d_list; dev < &d_list[demoncnt]; dev++)
106: if (dev->d_type == flag && dev->d_time == DAEMON)
107: (*dev->d_func)(dev->d_arg);
108: }
109:
110: /*
111: * fuse:
112: * Start a fuse to go off in a certain number of turns
113: */
114: void
115: fuse(void (*func)(), int arg, int time)
116: {
117: d_insert(func, arg, AFTER, time);
118: }
119:
120: /*
121: * lengthen:
122: * Increase the time until a fuse goes off
123: */
124: void
125: lengthen(void (*func)(), int xtime)
126: {
127: reg struct delayed_action *wire;
128:
129: for (wire = d_list; wire < &d_list[demoncnt]; wire++)
130: if (wire->d_type != EMPTY && func == wire->d_func)
131: wire->d_time += xtime;
132: }
133:
134: /*
135: * extinguish:
136: * Put out a fuse. Find all such fuses and kill them.
137: */
138: void
139: extinguish(void (*func)())
140: {
141: reg struct delayed_action *dev;
142:
143: for (dev = d_list; dev < &d_list[demoncnt]; dev++)
144: if (dev->d_type != EMPTY && func == dev->d_func)
145: d_delete(dev);
146: }
147:
148: /*
149: * do_fuses:
150: * Decrement counters and start needed fuses
151: */
152: void
153: do_fuses(void)
154: {
155: reg struct delayed_action *dev;
156:
157: for (dev = d_list; dev < &d_list[demoncnt]; dev++) {
158: if (dev->d_type == AFTER && dev->d_time > DAEMON) {
159: if (--dev->d_time == 0) {
160: (*dev->d_func)(dev->d_arg);
161: d_delete(dev);
162: }
163: }
164: }
165: }
166:
167:
168: /*
169: * activity:
170: * Show wizard number of demaons and memory blocks used
171: */
172: void
173: activity(void)
174: {
175: msg("Daemons = %d : Memory Items = %d : Memory Used = %d",
176: demoncnt,total,md_memused());
177: }
CVSweb