Annotation of researchv10no/games/rogue/daemon.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Contains functions for dealing with things that happen in the
                      3:  * future.
                      4:  *
                      5:  * @(#)daemon.c        3.3 (Berkeley) 6/15/81
                      6:  */
                      7: 
                      8: #include <curses.h>
                      9: #include "rogue.h"
                     10: 
                     11: #define EMPTY 0
                     12: #define DAEMON -1
                     13: #define MAXDAEMONS 20
                     14: 
                     15: #define _X_ { EMPTY }
                     16: 
                     17: struct delayed_action {
                     18:     int d_type;
                     19:     int (*d_func)();
                     20:     int d_arg;
                     21:     int d_time;
                     22: } d_list[MAXDAEMONS] = {
                     23:     _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_,
                     24:     _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, 
                     25: };
                     26: 
                     27: /*
                     28:  * d_slot:
                     29:  *     Find an empty slot in the daemon/fuse list
                     30:  */
                     31: struct delayed_action *
                     32: d_slot()
                     33: {
                     34:     register int i;
                     35:     register struct delayed_action *dev;
                     36: 
                     37:     for (i = 0, dev = d_list; i < MAXDAEMONS; i++, dev++)
                     38:        if (dev->d_type == EMPTY)
                     39:            return dev;
                     40:     debug("Ran out of fuse slots");
                     41:     return NULL;
                     42: }
                     43: 
                     44: /*
                     45:  * find_slot:
                     46:  *     Find a particular slot in the table
                     47:  */
                     48: 
                     49: struct delayed_action *
                     50: find_slot(func)
                     51: register int (*func)();
                     52: {
                     53:     register int i;
                     54:     register struct delayed_action *dev;
                     55: 
                     56:     for (i = 0, dev = d_list; i < MAXDAEMONS; i++, dev++)
                     57:        if (dev->d_type != EMPTY && func == dev->d_func)
                     58:            return dev;
                     59:     return NULL;
                     60: }
                     61: 
                     62: /*
                     63:  * daemon:
                     64:  *     Start a daemon, takes a function.
                     65:  */
                     66: 
                     67: daemon(func, arg, type)
                     68: int (*func)(), arg, type;
                     69: {
                     70:     register struct delayed_action *dev;
                     71: 
                     72:     dev = d_slot();
                     73:     dev->d_type = type;
                     74:     dev->d_func = func;
                     75:     dev->d_arg = arg;
                     76:     dev->d_time = DAEMON;
                     77: }
                     78: 
                     79: /*
                     80:  * kill_daemon:
                     81:  *     Remove a daemon from the list
                     82:  */
                     83: 
                     84: kill_daemon(func)
                     85: int (*func)();
                     86: {
                     87:     register struct delayed_action *dev;
                     88: 
                     89:     if ((dev = find_slot(func)) == NULL)
                     90:        return;
                     91:     /*
                     92:      * Take it out of the list
                     93:      */
                     94:     dev->d_type = EMPTY;
                     95: }
                     96: 
                     97: /*
                     98:  * do_daemons:
                     99:  *     Run all the daemons that are active with the current flag,
                    100:  *     passing the argument to the function.
                    101:  */
                    102: 
                    103: do_daemons(flag)
                    104: register int flag;
                    105: {
                    106:     register struct delayed_action *dev;
                    107: 
                    108:     /*
                    109:      * Loop through the devil list
                    110:      */
                    111:     for (dev = d_list; dev < &d_list[MAXDAEMONS]; dev++)
                    112:        /*
                    113:         * Executing each one, giving it the proper arguments
                    114:         */
                    115:        if (dev->d_type == flag && dev->d_time == DAEMON)
                    116:            (*dev->d_func)(dev->d_arg);
                    117: }
                    118: 
                    119: /*
                    120:  * fuse:
                    121:  *     Start a fuse to go off in a certain number of turns
                    122:  */
                    123: 
                    124: fuse(func, arg, time, type)
                    125: int (*func)(), arg, time, type;
                    126: {
                    127:     register struct delayed_action *wire;
                    128: 
                    129:     wire = d_slot();
                    130:     wire->d_type = type;
                    131:     wire->d_func = func;
                    132:     wire->d_arg = arg;
                    133:     wire->d_time = time;
                    134: }
                    135: 
                    136: /*
                    137:  * lengthen:
                    138:  *     Increase the time until a fuse goes off
                    139:  */
                    140: 
                    141: lengthen(func, xtime)
                    142: int (*func)();
                    143: int xtime;
                    144: {
                    145:     register struct delayed_action *wire;
                    146: 
                    147:     if ((wire = find_slot(func)) == NULL)
                    148:        return;
                    149:     wire->d_time += xtime;
                    150: }
                    151: 
                    152: /*
                    153:  * extinguish:
                    154:  *     Put out a fuse
                    155:  */
                    156: 
                    157: extinguish(func)
                    158: int (*func)();
                    159: {
                    160:     register struct delayed_action *wire;
                    161: 
                    162:     if ((wire = find_slot(func)) == NULL)
                    163:        return;
                    164:     wire->d_type = EMPTY;
                    165: }
                    166: 
                    167: /*
                    168:  * do_fuses:
                    169:  *     Decrement counters and start needed fuses
                    170:  */
                    171: 
                    172: do_fuses(flag)
                    173: register int flag;
                    174: {
                    175:     register struct delayed_action *wire;
                    176: 
                    177:     /*
                    178:      * Step though the list
                    179:      */
                    180:     for (wire = d_list; wire < &d_list[MAXDAEMONS]; wire++)
                    181:     {
                    182:        /*
                    183:         * Decrementing counters and starting things we want.  We also need
                    184:         * to remove the fuse from the list once it has gone off.
                    185:         */
                    186:        if (flag == wire->d_type && wire->d_time > 0 && --wire->d_time == 0)
                    187:        {
                    188:            wire->d_type = EMPTY;
                    189:            (*wire->d_func)(wire->d_arg);
                    190:        }
                    191:      }
                    192: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.