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