2015-05-08 15:24:40 -04:00
|
|
|
/*
|
|
|
|
|
* daemon.c - functions for dealing with things that happen in the future.
|
|
|
|
|
*
|
|
|
|
|
* Advanced Rogue
|
|
|
|
|
* Copyright (C) 1984, 1985, 1986 Michael Morgan, Ken Dalka and AT&T
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Based on "Rogue: Exploring the Dungeons of Doom"
|
|
|
|
|
* Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* See the file LICENSE.TXT for full copyright and licensing information.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Contains functions for dealing with things that happen in the
|
|
|
|
|
* future.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "curses.h"
|
|
|
|
|
#include "rogue.h"
|
|
|
|
|
|
|
|
|
|
#define EMPTY 0
|
|
|
|
|
#define DAEMON -1
|
|
|
|
|
#define MAXDAEMONS 10
|
|
|
|
|
#define MAXFUSES 20
|
|
|
|
|
|
|
|
|
|
#define _X_ { EMPTY }
|
|
|
|
|
|
|
|
|
|
struct delayed_action d_list[MAXDAEMONS] = {
|
|
|
|
|
_X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_
|
|
|
|
|
};
|
|
|
|
|
struct delayed_action f_list[MAXFUSES] = {
|
|
|
|
|
_X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_,
|
|
|
|
|
_X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_, _X_
|
|
|
|
|
};
|
|
|
|
|
int demoncnt = 0; /* number of active daemons */
|
|
|
|
|
int fusecnt = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* d_slot:
|
|
|
|
|
* Find an empty slot in the daemon list
|
|
|
|
|
*/
|
|
|
|
|
struct delayed_action *
|
2016-02-19 21:02:28 -05:00
|
|
|
d_slot(void)
|
2015-05-08 15:24:40 -04:00
|
|
|
{
|
|
|
|
|
reg int i;
|
|
|
|
|
reg struct delayed_action *dev;
|
|
|
|
|
|
|
|
|
|
for (i = 0, dev = d_list; i < MAXDAEMONS; i++, dev++)
|
|
|
|
|
if (dev->d_type == EMPTY)
|
|
|
|
|
return dev;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* f_slot:
|
|
|
|
|
* Find an empty slot in the fuses list
|
|
|
|
|
*/
|
|
|
|
|
struct delayed_action *
|
2016-02-19 21:02:28 -05:00
|
|
|
f_slot(void)
|
2015-05-08 15:24:40 -04:00
|
|
|
{
|
|
|
|
|
reg int i;
|
|
|
|
|
reg struct delayed_action *dev;
|
|
|
|
|
|
|
|
|
|
for (i = 0, dev = f_list; i < MAXFUSES; i++, dev++)
|
|
|
|
|
if (dev->d_type == EMPTY)
|
|
|
|
|
return dev;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* find_slot:
|
|
|
|
|
* Find a particular slot in the table
|
|
|
|
|
*/
|
|
|
|
|
struct delayed_action *
|
2016-03-05 20:49:37 -05:00
|
|
|
find_slot(void (*func)())
|
2015-05-08 15:24:40 -04:00
|
|
|
{
|
|
|
|
|
reg int i;
|
|
|
|
|
reg struct delayed_action *dev;
|
|
|
|
|
|
|
|
|
|
for (i = 0, dev = f_list; i < MAXFUSES; i++, dev++)
|
|
|
|
|
if (dev->d_type != EMPTY && func == dev->d_func)
|
|
|
|
|
return dev;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2015-05-28 10:21:43 -04:00
|
|
|
* start_daemon:
|
2015-05-08 15:24:40 -04:00
|
|
|
* Start a daemon, takes a function.
|
|
|
|
|
*/
|
2016-02-19 21:02:28 -05:00
|
|
|
void
|
2016-03-11 17:40:00 -05:00
|
|
|
start_daemon(void (*func)(), void *arg, int type)
|
2015-05-08 15:24:40 -04:00
|
|
|
{
|
|
|
|
|
reg struct delayed_action *dev;
|
|
|
|
|
|
|
|
|
|
dev = d_slot();
|
|
|
|
|
if (dev != NULL) {
|
|
|
|
|
dev->d_type = type;
|
|
|
|
|
dev->d_func = func;
|
2016-03-11 17:40:00 -05:00
|
|
|
dev->d_.varg = arg;
|
2015-05-08 15:24:40 -04:00
|
|
|
dev->d_time = DAEMON;
|
|
|
|
|
demoncnt += 1; /* update count */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* kill_daemon:
|
|
|
|
|
* Remove a daemon from the list
|
|
|
|
|
*/
|
2016-02-19 21:02:28 -05:00
|
|
|
void
|
2016-03-05 20:49:37 -05:00
|
|
|
kill_daemon(void (*func)())
|
2015-05-08 15:24:40 -04:00
|
|
|
{
|
|
|
|
|
reg struct delayed_action *dev;
|
|
|
|
|
reg int i;
|
|
|
|
|
|
|
|
|
|
for (i = 0, dev = d_list; i < MAXDAEMONS; i++, dev++) {
|
|
|
|
|
if (dev->d_type != EMPTY && func == dev->d_func)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (i >= MAXDAEMONS) return; /* if not found, forget it */
|
|
|
|
|
/*
|
|
|
|
|
* Take it out of the list
|
|
|
|
|
*/
|
|
|
|
|
dev->d_type = EMPTY;
|
|
|
|
|
dev->d_.arg = 0;
|
|
|
|
|
dev->d_func = NULL;
|
|
|
|
|
dev->d_time = 0;
|
|
|
|
|
demoncnt -= 1; /* update count */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* do_daemons:
|
|
|
|
|
* Run all the daemons that are active with the current flag,
|
|
|
|
|
* passing the argument to the function.
|
|
|
|
|
*/
|
2016-02-19 21:02:28 -05:00
|
|
|
void
|
|
|
|
|
do_daemons(int flag)
|
2015-05-08 15:24:40 -04:00
|
|
|
{
|
|
|
|
|
reg struct delayed_action *dev;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Loop through the devil list
|
|
|
|
|
*/
|
|
|
|
|
for (dev = d_list; dev <= &d_list[MAXDAEMONS-1]; dev++)
|
|
|
|
|
/*
|
|
|
|
|
* Executing each one, giving it the proper arguments
|
|
|
|
|
*/
|
|
|
|
|
if (dev->d_type == flag && dev->d_time == DAEMON)
|
2016-03-11 17:40:00 -05:00
|
|
|
(*dev->d_func)(dev->d_.varg);
|
2015-05-08 15:24:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* fuse:
|
|
|
|
|
* Start a fuse to go off in a certain number of turns
|
|
|
|
|
*/
|
2016-02-19 21:02:28 -05:00
|
|
|
void
|
2016-03-11 17:40:00 -05:00
|
|
|
fuse(void (*func)(), void *arg, int time, int type)
|
2015-05-08 15:24:40 -04:00
|
|
|
{
|
|
|
|
|
reg struct delayed_action *wire;
|
|
|
|
|
|
|
|
|
|
wire = f_slot();
|
|
|
|
|
if (wire != NULL) {
|
|
|
|
|
wire->d_type = type;
|
|
|
|
|
wire->d_func = func;
|
2016-03-11 17:40:00 -05:00
|
|
|
if (func == changeclass || func == res_strength)
|
|
|
|
|
wire->d_.arg = *(int *) arg;
|
|
|
|
|
else
|
|
|
|
|
wire->d_.varg = arg;
|
2015-05-08 15:24:40 -04:00
|
|
|
wire->d_time = time;
|
|
|
|
|
fusecnt += 1; /* update count */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* lengthen:
|
|
|
|
|
* Increase the time until a fuse goes off
|
|
|
|
|
*/
|
2016-02-19 21:02:28 -05:00
|
|
|
void
|
2016-03-05 20:49:37 -05:00
|
|
|
lengthen(void (*func)(), int xtime)
|
2015-05-08 15:24:40 -04:00
|
|
|
{
|
|
|
|
|
reg struct delayed_action *wire;
|
|
|
|
|
|
|
|
|
|
if ((wire = find_slot(func)) == NULL)
|
|
|
|
|
return;
|
|
|
|
|
wire->d_time += xtime;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* extinguish:
|
|
|
|
|
* Put out a fuse
|
|
|
|
|
*/
|
2016-02-19 21:02:28 -05:00
|
|
|
void
|
2016-03-05 20:49:37 -05:00
|
|
|
extinguish(void (*func)())
|
2015-05-08 15:24:40 -04:00
|
|
|
{
|
|
|
|
|
reg struct delayed_action *wire;
|
|
|
|
|
|
|
|
|
|
if ((wire = find_slot(func)) == NULL)
|
|
|
|
|
return;
|
|
|
|
|
wire->d_type = EMPTY;
|
|
|
|
|
wire->d_func = NULL;
|
|
|
|
|
wire->d_.arg = 0;
|
|
|
|
|
wire->d_time = 0;
|
|
|
|
|
fusecnt -= 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* do_fuses:
|
|
|
|
|
* Decrement counters and start needed fuses
|
|
|
|
|
*/
|
2016-02-19 21:02:28 -05:00
|
|
|
void
|
|
|
|
|
do_fuses(int flag)
|
2015-05-08 15:24:40 -04:00
|
|
|
{
|
|
|
|
|
reg struct delayed_action *wire;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Step though the list
|
|
|
|
|
*/
|
|
|
|
|
for (wire = f_list; wire <= &f_list[MAXFUSES-1]; wire++) {
|
|
|
|
|
/*
|
|
|
|
|
* Decrementing counters and starting things we want. We also need
|
|
|
|
|
* to remove the fuse from the list once it has gone off.
|
|
|
|
|
*/
|
|
|
|
|
if(flag == wire->d_type && wire->d_time > 0 &&
|
|
|
|
|
--wire->d_time == 0) {
|
|
|
|
|
wire->d_type = EMPTY;
|
2016-03-11 17:40:00 -05:00
|
|
|
if (wire->d_func == changeclass || wire->d_func == res_strength)
|
2015-05-08 15:24:40 -04:00
|
|
|
(*wire->d_func)(wire->d_.arg);
|
2016-03-11 17:40:00 -05:00
|
|
|
else if (wire->d_func != NULL)
|
|
|
|
|
(*wire->d_func)(wire->d_.varg);
|
2015-05-08 15:24:40 -04:00
|
|
|
fusecnt -= 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* activity:
|
|
|
|
|
* Show wizard number of demaons and memory blocks used
|
|
|
|
|
*/
|
2016-02-19 21:02:28 -05:00
|
|
|
void
|
|
|
|
|
activity(void)
|
2015-05-08 15:24:40 -04:00
|
|
|
{
|
|
|
|
|
msg("Daemons = %d : Fuses = %d : Memory Items = %d : Memory Used = %d",
|
2016-02-19 21:02:28 -05:00
|
|
|
demoncnt,fusecnt,total,md_memused());
|
2015-05-08 15:24:40 -04:00
|
|
|
}
|