[BACK]Return to daemon.c CVS log [TXT][DIR] Up to [contributed] / early-roguelike / xrogue

Annotation of early-roguelike/xrogue/daemon.c, Revision 1.1.1.1

1.1       rubenllo    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 d_list[MAXDAEMONS] = {
                     28:         _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_
                     29: };
                     30: struct delayed_action f_list[MAXFUSES] = {
                     31:         _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_,
                     32:         _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_
                     33: };
                     34:
                     35: int demoncnt = 0;        /* number of active daemons */
                     36: int fusecnt = 0;
                     37:
                     38:
                     39: /*
                     40:  * d_slot:
                     41:  *      Find an empty slot in the daemon list
                     42:  */
                     43: struct delayed_action *
                     44: d_slot(void)
                     45: {
                     46:         reg int i;
                     47:         reg struct delayed_action *dev;
                     48:
                     49:         for (i = 0, dev = d_list; i < MAXDAEMONS; i++, dev++)
                     50:                 if (dev->d_type == EMPTY)
                     51:                         return dev;
                     52:         return NULL;
                     53: }
                     54:
                     55: /*
                     56:  * f_slot:
                     57:  *      Find an empty slot in the fuses list
                     58:  */
                     59: struct delayed_action *
                     60: f_slot(void)
                     61: {
                     62:         reg int i;
                     63:         reg struct delayed_action *dev;
                     64:
                     65:         for (i = 0, dev = f_list; i < MAXFUSES; i++, dev++)
                     66:                 if (dev->d_type == EMPTY)
                     67:                         return dev;
                     68:         return NULL;
                     69: }
                     70:
                     71: /*
                     72:  * find_slot:
                     73:  *      Find a particular slot in the table
                     74:  */
                     75:
                     76: struct delayed_action *
                     77: find_slot(void (*func)())
                     78: {
                     79:         reg int i;
                     80:         reg struct delayed_action *dev;
                     81:
                     82:         for (i = 0, dev = f_list; i < MAXFUSES; i++, dev++)
                     83:                 if (dev->d_type != EMPTY && func == dev->d_func)
                     84:                         return dev;
                     85:         return NULL;
                     86: }
                     87:
                     88: /*
                     89:  * start_daemon:
                     90:  *      Start a daemon, takes a function.
                     91:  */
                     92:
                     93: void
                     94: start_daemon(void (*dfunc)(), void *arg, int type)
                     95: {
                     96:         reg struct delayed_action *dev;
                     97:
                     98:         dev = d_slot();
                     99:         if (dev != NULL) {
                    100:                 dev->d_type = type;
                    101:                 dev->d_func = dfunc;
                    102:                 dev->d_arg.vp = arg;
                    103:                 dev->d_time = DAEMON;
                    104:                 demoncnt += 1;                  /* update count */
                    105:         }
                    106: }
                    107:
                    108: /*
                    109:  * kill_daemon:
                    110:  *      Remove a daemon from the list
                    111:  */
                    112:
                    113: void
                    114: kill_daemon(void (*dfunc)())
                    115: {
                    116:         reg struct delayed_action *dev;
                    117:         reg int i;
                    118:
                    119:         for (i = 0, dev = d_list; i < MAXDAEMONS; i++, dev++) {
                    120:                 if (dev->d_type != EMPTY && dfunc == dev->d_func)
                    121:                         break;
                    122:         }
                    123:         if (i >= MAXDAEMONS) return; /* if not found, forget it */
                    124:         /*
                    125:          * Take it out of the list
                    126:          */
                    127:         dev->d_type = EMPTY;
                    128:                dev->d_arg.vp  = NULL;
                    129:                dev->d_func = NULL;
                    130:                dev->d_time = 0;
                    131:
                    132:         demoncnt -= 1;                  /* update count */
                    133: }
                    134:
                    135: /*
                    136:  * do_daemons:
                    137:  *      Run all the daemons that are active with the current flag,
                    138:  *      passing the argument to the function.
                    139:  */
                    140:
                    141: void
                    142: do_daemons(int flag)
                    143: {
                    144:         struct delayed_action *dev;
                    145:         int i;
                    146:
                    147:         /*
                    148:          * Loop through the devil list
                    149:          */
                    150:         for (i = 0; i < MAXDAEMONS; i++)
                    151:         {
                    152:             dev = &d_list[i];
                    153:         /*
                    154:          * Executing each one, giving it the proper arguments
                    155:          */
                    156:                 if ((dev->d_type == flag) && (dev->d_time == DAEMON) && (dev->d_func != NULL))
                    157:                         (*dev->d_func)(dev->d_arg.vp);
                    158:         }
                    159: }
                    160:
                    161: /*
                    162:  * fuse:
                    163:  *      Start a fuse to go off in a certain number of turns
                    164:  */
                    165:
                    166: void
                    167: fuse(void (*dfunc)(), void *arg, int time, int 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 = dfunc;
                    175:                 if (dfunc == changeclass || dfunc == res_strength)
                    176:                         wire->d_arg.i = *(int *) arg;
                    177:                 else
                    178:                         wire->d_arg.vp = arg;
                    179:                 wire->d_time = time;
                    180:                 fusecnt += 1;                   /* update count */
                    181:         }
                    182: }
                    183:
                    184: /*
                    185:  * lengthen:
                    186:  *      Increase the time until a fuse goes off
                    187:  */
                    188:
                    189: void
                    190: lengthen(void (*dfunc)(), int xtime)
                    191: {
                    192:         reg struct delayed_action *wire;
                    193:
                    194:         if ((wire = find_slot(dfunc)) == NULL)
                    195:                 return;
                    196:         wire->d_time += xtime;
                    197: }
                    198:
                    199: /*
                    200:  * extinguish:
                    201:  *      Put out a fuse
                    202:  */
                    203:
                    204: void
                    205: extinguish(void (*dfunc)())
                    206: {
                    207:         reg struct delayed_action *wire;
                    208:
                    209:         if ((wire = find_slot(dfunc)) == NULL)
                    210:                 return;
                    211:         wire->d_type = EMPTY;
                    212:                wire->d_func = NULL;
                    213:                wire->d_arg.vp = NULL;
                    214:                wire->d_time = 0;
                    215:         fusecnt -= 1;
                    216: }
                    217:
                    218: /*
                    219:  * do_fuses:
                    220:  *      Decrement counters and start needed fuses
                    221:  */
                    222:
                    223: void
                    224: do_fuses(int flag)
                    225: {
                    226:     struct delayed_action *wire;
                    227:     int i;
                    228:
                    229:     /*
                    230:      * Step though the list
                    231:      */
                    232:     for (i = 0; i < MAXFUSES; i++) {
                    233:         wire = &f_list[i];
                    234:         /*
                    235:          * Decrementing counters and starting things we want.  We also need
                    236:          * to remove the fuse from the list once it has gone off.
                    237:          */
                    238:         if(flag == wire->d_type && wire->d_time > 0 &&
                    239:               --wire->d_time == 0) {
                    240:             wire->d_type = EMPTY;
                    241:             if (*wire->d_func == changeclass || *wire->d_func == res_strength)
                    242:                 (*wire->d_func)(wire->d_arg.i);
                    243:             else if (wire->d_func != NULL)
                    244:                 (*wire->d_func)(wire->d_arg.vp);
                    245:             fusecnt -= 1;
                    246:         }
                    247:     }
                    248: }
                    249:
                    250: /*
                    251:  * activity:
                    252:  *      Show wizard number of demaons and memory blocks used
                    253:  */
                    254:
                    255: void
                    256: activity(void)
                    257: {
                    258:         msg("Daemons = %d : Fuses = %d : Memory Items = %d : Memory Used = %d",
                    259:             demoncnt,fusecnt,total,md_memused());
                    260: }
                    261:

CVSweb