comparison srogue/daemon.c @ 36:2128c7dc8a40

Import Super-Rogue 9.0 from the Roguelike Restoration Project (r1490)
author elwin
date Thu, 25 Nov 2010 12:21:41 +0000
parents
children f2951c4e28d9
comparison
equal deleted inserted replaced
35:05018c63a721 36:2128c7dc8a40
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(func, arg, type, time)
38 int arg, type, time, (*func)();
39 {
40 reg struct delayed_action *dev;
41
42 if (demoncnt < MAXDAEMONS) {
43 dev = &d_list[demoncnt];
44 dev->d_type = type;
45 dev->d_time = time;
46 dev->d_func = func;
47 dev->d_arg = arg;
48 demoncnt += 1;
49 return dev;
50 }
51 return NULL;
52 }
53
54 d_delete(wire)
55 struct delayed_action *wire;
56 {
57 reg struct delayed_action *d1, *d2;
58
59 for (d1 = d_list; d1 < &d_list[demoncnt]; d1++) {
60 if (wire == d1) {
61 for (d2 = d1 + 1; d2 < &d_list[demoncnt]; d2++)
62 *d1++ = *d2;
63 demoncnt -= 1;
64 d1 = &d_list[demoncnt];
65 d1->d_type = EMPTY;
66 d1->d_func = EMPTY;
67 return;
68 }
69 }
70 }
71 /*
72 * find_slot:
73 * Find a particular slot in the table
74 */
75 struct delayed_action *
76 find_slot(func)
77 int (*func)();
78 {
79 reg struct delayed_action *dev;
80
81 for (dev = d_list; dev < &d_list[demoncnt]; dev++)
82 if (dev->d_type != EMPTY && func == dev->d_func)
83 return dev;
84 return NULL;
85 }
86
87 /*
88 * daemon:
89 * Start a daemon, takes a function.
90 */
91 daemon(func, arg, type)
92 int arg, type, (*func)();
93 {
94 d_insert(func, arg, type, DAEMON);
95 }
96
97 /*
98 * do_daemons:
99 * Run all the daemons that are active with the current
100 * flag, passing the argument to the function.
101 */
102 do_daemons(flag)
103 int flag;
104 {
105 reg struct delayed_action *dev;
106
107 for (dev = d_list; dev < &d_list[demoncnt]; dev++)
108 if (dev->d_type == flag && dev->d_time == DAEMON)
109 (*dev->d_func)(dev->d_arg);
110 }
111
112 /*
113 * fuse:
114 * Start a fuse to go off in a certain number of turns
115 */
116 fuse(func, arg, time)
117 int (*func)(), arg, time;
118 {
119 d_insert(func, arg, AFTER, time);
120 }
121
122 /*
123 * lengthen:
124 * Increase the time until a fuse goes off
125 */
126 lengthen(func, xtime)
127 int (*func)(), xtime;
128 {
129 reg struct delayed_action *wire;
130
131 for (wire = d_list; wire < &d_list[demoncnt]; wire++)
132 if (wire->d_type != EMPTY && func == wire->d_func)
133 wire->d_time += xtime;
134 }
135
136 /*
137 * extinguish:
138 * Put out a fuse. Find all such fuses and kill them.
139 */
140 extinguish(func)
141 int (*func)();
142 {
143 reg struct delayed_action *dev;
144
145 for (dev = d_list; dev < &d_list[demoncnt]; dev++)
146 if (dev->d_type != EMPTY && func == dev->d_func)
147 d_delete(dev);
148 }
149
150 /*
151 * do_fuses:
152 * Decrement counters and start needed fuses
153 */
154 do_fuses()
155 {
156 reg struct delayed_action *dev;
157
158 for (dev = d_list; dev < &d_list[demoncnt]; dev++) {
159 if (dev->d_type == AFTER && dev->d_time > DAEMON) {
160 if (--dev->d_time == 0) {
161 (*dev->d_func)(dev->d_arg);
162 d_delete(dev);
163 }
164 }
165 }
166 }
167
168
169 /*
170 * activity:
171 * Show wizard number of demaons and memory blocks used
172 */
173 activity()
174 {
175 msg("Daemons = %d : Memory Items = %d : Memory Used = %d",
176 demoncnt,total,sbrk(0));
177 }