Mercurial > hg > early-roguelike
comparison xrogue/daemon.c @ 133:e6179860cb76
Import XRogue 8.0 from the Roguelike Restoration Project (r1490)
| author | John "Elwin" Edwards |
|---|---|
| date | Tue, 21 Apr 2015 08:55:20 -0400 |
| parents | |
| children | cfa9d1609b78 |
comparison
equal
deleted
inserted
replaced
| 124:d10fc4a065ac | 133:e6179860cb76 |
|---|---|
| 1 /* | |
| 2 daemon.c - functions for dealing with things that happen in the future | |
| 3 | |
| 4 XRogue: Expeditions into the Dungeons of Doom | |
| 5 Copyright (C) 1991 Robert Pietkivitch | |
| 6 All rights reserved. | |
| 7 | |
| 8 Based on "Advanced Rogue" | |
| 9 Copyright (C) 1984, 1985 Michael Morgan, Ken Dalka and AT&T | |
| 10 All rights reserved. | |
| 11 | |
| 12 Based on "Rogue: Exploring the Dungeons of Doom" | |
| 13 Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman | |
| 14 All rights reserved. | |
| 15 | |
| 16 See the file LICENSE.TXT for full copyright and licensing information. | |
| 17 */ | |
| 18 | |
| 19 #include <curses.h> | |
| 20 #include "rogue.h" | |
| 21 | |
| 22 #define EMPTY 0 | |
| 23 #define DAEMON -1 | |
| 24 | |
| 25 #define _X_ { EMPTY } | |
| 26 | |
| 27 struct delayed_action { | |
| 28 int d_type; | |
| 29 int (*d_func)(); | |
| 30 VOID *d_arg; | |
| 31 int d_time; | |
| 32 } ; | |
| 33 struct delayed_action d_list[MAXDAEMONS] = { | |
| 34 _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_ | |
| 35 }; | |
| 36 struct delayed_action f_list[MAXFUSES] = { | |
| 37 _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, | |
| 38 _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_ | |
| 39 }; | |
| 40 | |
| 41 int demoncnt = 0; /* number of active daemons */ | |
| 42 int fusecnt = 0; | |
| 43 | |
| 44 | |
| 45 /* | |
| 46 * d_slot: | |
| 47 * Find an empty slot in the daemon list | |
| 48 */ | |
| 49 struct delayed_action * | |
| 50 d_slot() | |
| 51 { | |
| 52 reg int i; | |
| 53 reg struct delayed_action *dev; | |
| 54 | |
| 55 for (i = 0, dev = d_list; i < MAXDAEMONS; i++, dev++) | |
| 56 if (dev->d_type == EMPTY) | |
| 57 return dev; | |
| 58 return NULL; | |
| 59 } | |
| 60 | |
| 61 /* | |
| 62 * f_slot: | |
| 63 * Find an empty slot in the fuses list | |
| 64 */ | |
| 65 struct delayed_action * | |
| 66 f_slot() | |
| 67 { | |
| 68 reg int i; | |
| 69 reg struct delayed_action *dev; | |
| 70 | |
| 71 for (i = 0, dev = f_list; i < MAXFUSES; i++, dev++) | |
| 72 if (dev->d_type == EMPTY) | |
| 73 return dev; | |
| 74 return NULL; | |
| 75 } | |
| 76 | |
| 77 /* | |
| 78 * find_slot: | |
| 79 * Find a particular slot in the table | |
| 80 */ | |
| 81 | |
| 82 struct delayed_action * | |
| 83 find_slot(func) | |
| 84 reg int (*func)(); | |
| 85 { | |
| 86 reg int i; | |
| 87 reg struct delayed_action *dev; | |
| 88 | |
| 89 for (i = 0, dev = f_list; i < MAXFUSES; i++, dev++) | |
| 90 if (dev->d_type != EMPTY && func == dev->d_func) | |
| 91 return dev; | |
| 92 return NULL; | |
| 93 } | |
| 94 | |
| 95 /* | |
| 96 * daemon: | |
| 97 * Start a daemon, takes a function. | |
| 98 */ | |
| 99 | |
| 100 daemon(dfunc, arg, type) | |
| 101 reg VOID *arg; | |
| 102 reg int type, (*dfunc)(); | |
| 103 { | |
| 104 reg struct delayed_action *dev; | |
| 105 | |
| 106 dev = d_slot(); | |
| 107 if (dev != NULL) { | |
| 108 dev->d_type = type; | |
| 109 dev->d_func = dfunc; | |
| 110 dev->d_arg = arg; | |
| 111 dev->d_time = DAEMON; | |
| 112 demoncnt += 1; /* update count */ | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 /* | |
| 117 * kill_daemon: | |
| 118 * Remove a daemon from the list | |
| 119 */ | |
| 120 | |
| 121 kill_daemon(dfunc) | |
| 122 reg int (*dfunc)(); | |
| 123 { | |
| 124 reg struct delayed_action *dev; | |
| 125 reg int i; | |
| 126 | |
| 127 for (i = 0, dev = d_list; i < MAXDAEMONS; i++, dev++) { | |
| 128 if (dev->d_type != EMPTY && dfunc == dev->d_func) | |
| 129 break; | |
| 130 } | |
| 131 if (i >= MAXDAEMONS) return; /* if not found, forget it */ | |
| 132 /* | |
| 133 * Take it out of the list | |
| 134 */ | |
| 135 dev->d_type = EMPTY; | |
| 136 dev->d_arg = NULL; | |
| 137 dev->d_func = NULL; | |
| 138 dev->d_time = 0; | |
| 139 | |
| 140 demoncnt -= 1; /* update count */ | |
| 141 } | |
| 142 | |
| 143 /* | |
| 144 * do_daemons: | |
| 145 * Run all the daemons that are active with the current flag, | |
| 146 * passing the argument to the function. | |
| 147 */ | |
| 148 | |
| 149 do_daemons(flag) | |
| 150 reg int flag; | |
| 151 { | |
| 152 struct delayed_action *dev; | |
| 153 int i; | |
| 154 | |
| 155 /* | |
| 156 * Loop through the devil list | |
| 157 */ | |
| 158 for (i = 0; i < MAXDAEMONS; i++) | |
| 159 { | |
| 160 dev = &d_list[i]; | |
| 161 /* | |
| 162 * Executing each one, giving it the proper arguments | |
| 163 */ | |
| 164 if ((dev->d_type == flag) && (dev->d_time == DAEMON) && (dev->d_func != NULL)) | |
| 165 (*dev->d_func)(dev->d_arg); | |
| 166 } | |
| 167 } | |
| 168 | |
| 169 /* | |
| 170 * fuse: | |
| 171 * Start a fuse to go off in a certain number of turns | |
| 172 */ | |
| 173 | |
| 174 fuse(dfunc, arg, time, type) | |
| 175 VOID *arg; | |
| 176 reg int (*dfunc)(), time, type; | |
| 177 { | |
| 178 reg struct delayed_action *wire; | |
| 179 | |
| 180 wire = f_slot(); | |
| 181 if (wire != NULL) { | |
| 182 wire->d_type = type; | |
| 183 wire->d_func = dfunc; | |
| 184 wire->d_arg = arg; | |
| 185 wire->d_time = time; | |
| 186 fusecnt += 1; /* update count */ | |
| 187 } | |
| 188 } | |
| 189 | |
| 190 /* | |
| 191 * lengthen: | |
| 192 * Increase the time until a fuse goes off | |
| 193 */ | |
| 194 | |
| 195 lengthen(dfunc, xtime) | |
| 196 reg int (*dfunc)(), xtime; | |
| 197 { | |
| 198 reg struct delayed_action *wire; | |
| 199 | |
| 200 if ((wire = find_slot(dfunc)) == NULL) | |
| 201 return; | |
| 202 wire->d_time += xtime; | |
| 203 } | |
| 204 | |
| 205 /* | |
| 206 * extinguish: | |
| 207 * Put out a fuse | |
| 208 */ | |
| 209 | |
| 210 extinguish(dfunc) | |
| 211 reg int (*dfunc)(); | |
| 212 { | |
| 213 reg struct delayed_action *wire; | |
| 214 | |
| 215 if ((wire = find_slot(dfunc)) == NULL) | |
| 216 return; | |
| 217 wire->d_type = EMPTY; | |
| 218 wire->d_func = NULL; | |
| 219 wire->d_arg = NULL; | |
| 220 wire->d_time = 0; | |
| 221 fusecnt -= 1; | |
| 222 } | |
| 223 | |
| 224 /* | |
| 225 * do_fuses: | |
| 226 * Decrement counters and start needed fuses | |
| 227 */ | |
| 228 | |
| 229 do_fuses(flag) | |
| 230 reg int flag; | |
| 231 { | |
| 232 struct delayed_action *wire; | |
| 233 int i; | |
| 234 | |
| 235 /* | |
| 236 * Step though the list | |
| 237 */ | |
| 238 for (i = 0; i < MAXFUSES; i++) { | |
| 239 wire = &f_list[i]; | |
| 240 /* | |
| 241 * Decrementing counters and starting things we want. We also need | |
| 242 * to remove the fuse from the list once it has gone off. | |
| 243 */ | |
| 244 if(flag == wire->d_type && wire->d_time > 0 && | |
| 245 --wire->d_time == 0) { | |
| 246 wire->d_type = EMPTY; | |
| 247 if (wire->d_func != NULL) | |
| 248 (*wire->d_func)(wire->d_arg); | |
| 249 fusecnt -= 1; | |
| 250 } | |
| 251 } | |
| 252 } | |
| 253 | |
| 254 /* | |
| 255 * activity: | |
| 256 * Show wizard number of demaons and memory blocks used | |
| 257 */ | |
| 258 | |
| 259 activity() | |
| 260 { | |
| 261 msg("Daemons = %d : Fuses = %d : Memory Items = %d : Memory Used = %d", | |
| 262 demoncnt,fusecnt,total,md_memused()); | |
| 263 } | |
| 264 |
