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