dsIIr4/bin/
dsIIr4/extra/creremote/
dsIIr4/extra/wolfpaw/
dsIIr4/lib/cmds/admins/
dsIIr4/lib/cmds/common/
dsIIr4/lib/cmds/creators/include/
dsIIr4/lib/cmds/creators/include/SCCS/
dsIIr4/lib/daemon/services/
dsIIr4/lib/doc/
dsIIr4/lib/domains/Ylsrim/
dsIIr4/lib/domains/Ylsrim/adm/
dsIIr4/lib/domains/Ylsrim/armor/
dsIIr4/lib/domains/Ylsrim/broken/
dsIIr4/lib/domains/Ylsrim/fish/
dsIIr4/lib/domains/Ylsrim/meal/
dsIIr4/lib/domains/Ylsrim/npc/
dsIIr4/lib/domains/Ylsrim/virtual/
dsIIr4/lib/domains/Ylsrim/weapon/
dsIIr4/lib/domains/campus/adm/
dsIIr4/lib/domains/campus/etc/
dsIIr4/lib/domains/campus/meals/
dsIIr4/lib/domains/campus/npc/
dsIIr4/lib/domains/campus/save/
dsIIr4/lib/domains/campus/txt/
dsIIr4/lib/domains/campus/txt/ai/charles/
dsIIr4/lib/domains/campus/txt/ai/charles/bak2/
dsIIr4/lib/domains/campus/txt/ai/charles/bak2/bak1/
dsIIr4/lib/domains/campus/txt/ai/charly/
dsIIr4/lib/domains/campus/txt/ai/charly/bak/
dsIIr4/lib/domains/campus/txt/jenny/
dsIIr4/lib/domains/default/creator/
dsIIr4/lib/domains/default/doors/
dsIIr4/lib/domains/default/etc/
dsIIr4/lib/domains/default/virtual/
dsIIr4/lib/domains/default/weap/
dsIIr4/lib/domains/town/virtual/
dsIIr4/lib/lib/comp/
dsIIr4/lib/lib/lvs/
dsIIr4/lib/lib/user/
dsIIr4/lib/lib/virtual/
dsIIr4/lib/log/
dsIIr4/lib/obj/book_source/
dsIIr4/lib/obj/include/
dsIIr4/lib/realms/template/
dsIIr4/lib/realms/template/adm/
dsIIr4/lib/realms/template/area/armor/
dsIIr4/lib/realms/template/area/npc/
dsIIr4/lib/realms/template/area/obj/
dsIIr4/lib/realms/template/area/room/
dsIIr4/lib/realms/template/area/weap/
dsIIr4/lib/realms/template/bak/
dsIIr4/lib/realms/template/cmds/
dsIIr4/lib/save/
dsIIr4/lib/save/kills/o/
dsIIr4/lib/secure/cfg/classes/
dsIIr4/lib/secure/cmds/creators/include/
dsIIr4/lib/secure/cmds/players/
dsIIr4/lib/secure/cmds/players/include/
dsIIr4/lib/secure/daemon/include/
dsIIr4/lib/secure/lib/
dsIIr4/lib/secure/lib/include/
dsIIr4/lib/secure/lib/net/include/
dsIIr4/lib/secure/lib/std/
dsIIr4/lib/secure/modules/
dsIIr4/lib/secure/npc/
dsIIr4/lib/secure/obj/include/
dsIIr4/lib/secure/room/
dsIIr4/lib/secure/save/
dsIIr4/lib/secure/save/boards/
dsIIr4/lib/secure/save/players/g/
dsIIr4/lib/secure/tmp/
dsIIr4/lib/secure/verbs/creators/
dsIIr4/lib/shadows/
dsIIr4/lib/spells/
dsIIr4/lib/std/board/
dsIIr4/lib/std/lib/
dsIIr4/lib/tmp/
dsIIr4/lib/verbs/admins/include/
dsIIr4/lib/verbs/common/
dsIIr4/lib/verbs/common/include/
dsIIr4/lib/verbs/creators/include/
dsIIr4/lib/verbs/players/include/SCCS/
dsIIr4/lib/verbs/rooms/
dsIIr4/lib/verbs/rooms/include/
dsIIr4/lib/www/
dsIIr4/v22.2b14-dsouls2/
dsIIr4/v22.2b14-dsouls2/ChangeLog.old/
dsIIr4/v22.2b14-dsouls2/Win32/
dsIIr4/v22.2b14-dsouls2/compat/
dsIIr4/v22.2b14-dsouls2/compat/simuls/
dsIIr4/v22.2b14-dsouls2/include/
dsIIr4/v22.2b14-dsouls2/mudlib/
dsIIr4/v22.2b14-dsouls2/testsuite/
dsIIr4/v22.2b14-dsouls2/testsuite/clone/
dsIIr4/v22.2b14-dsouls2/testsuite/command/
dsIIr4/v22.2b14-dsouls2/testsuite/data/
dsIIr4/v22.2b14-dsouls2/testsuite/etc/
dsIIr4/v22.2b14-dsouls2/testsuite/include/
dsIIr4/v22.2b14-dsouls2/testsuite/inherit/
dsIIr4/v22.2b14-dsouls2/testsuite/inherit/master/
dsIIr4/v22.2b14-dsouls2/testsuite/log/
dsIIr4/v22.2b14-dsouls2/testsuite/single/
dsIIr4/v22.2b14-dsouls2/testsuite/single/tests/compiler/
dsIIr4/v22.2b14-dsouls2/testsuite/single/tests/efuns/
dsIIr4/v22.2b14-dsouls2/testsuite/single/tests/operators/
dsIIr4/v22.2b14-dsouls2/testsuite/u/
dsIIr4/v22.2b14-dsouls2/tmp/
dsIIr4/win32/
#include "std.h"
#include "call_out.h"
#include "backend.h"
#include "comm.h"
#include "port.h"
#include "eoperators.h"

#define DBG(x) debug(call_out, x)

/*
 * This file implements delayed calls of functions.
 * Static functions can not be called this way.
 *
 * Allocate the structures several in one chunk, to get rid of malloc
 * overhead.
 */

#define CHUNK_SIZE	20

typedef struct pending_call_s {
    int delta;
    union string_or_func function;
    object_t *ob;
    array_t *vs;
    struct pending_call_s *next;
#ifdef THIS_PLAYER_IN_CALL_OUT
    object_t *command_giver;
#endif
#ifdef CALLOUT_HANDLES
    int handle;
#endif
} pending_call_t;

static pending_call_t *call_list[CALLOUT_CYCLE_SIZE];
static pending_call_t *call_list_free;
static int num_call;
#ifdef CALLOUT_HANDLES
static int unique = 0;
#endif

static void free_call PROT((pending_call_t *));
static void free_called_call PROT((pending_call_t *));
void remove_all_call_out PROT((object_t *));

/*
 * Free a call out structure.
 */
static void free_called_call P1(pending_call_t *, cop)
{
    cop->next = call_list_free;
    if (cop->ob) {
	free_string(cop->function.s);
	free_object(cop->ob, "free_call");
    } else {
	free_funp(cop->function.f);
    }
    cop->function.s = 0;
#ifdef THIS_PLAYER_IN_CALL_OUT
    if (cop->command_giver)
	free_object(cop->command_giver, "free_call");
#endif
    cop->ob = 0;
    call_list_free = cop;
}

INLINE_STATIC void free_call P1(pending_call_t *, cop)
{
    if (cop->vs)
	free_array(cop->vs);
    free_called_call(cop);
}

/*
 * Setup a new call out.
 */
#ifdef CALLOUT_HANDLES
int
#else
void
#endif
new_call_out P5(object_t *, ob, svalue_t *, fun, int, delay, 
		int, num_args, svalue_t *, arg)
{
    pending_call_t *cop, **copp;
    int tm;

    if (delay < 0)
	delay = 0;

    DBG(("new_call_out: /%s delay %i", ob->name, delay));
    
    if (!call_list_free) {
	int i;

	call_list_free = CALLOCATE(CHUNK_SIZE, pending_call_t,
				   TAG_CALL_OUT, "new_call_out: call_list_free");
	for (i = 0; i < CHUNK_SIZE - 1; i++)
	    call_list_free[i].next = &call_list_free[i + 1];
	call_list_free[CHUNK_SIZE - 1].next = 0;
	num_call += CHUNK_SIZE;
    }
    cop = call_list_free;
    call_list_free = call_list_free->next;

    if (fun->type == T_STRING) {
	DBG(("  function: %s", fun->u.string));
	cop->function.s = make_shared_string(fun->u.string);
	cop->ob = ob;
	add_ref(ob, "call_out");
    } else {
	DBG(("  function: <function>"));
	cop->function.f = fun->u.fp;
	fun->u.fp->hdr.ref++;
	cop->ob = 0;
    }
#ifdef THIS_PLAYER_IN_CALL_OUT
    cop->command_giver = command_giver;	/* save current user context */
    if (command_giver)
	add_ref(command_giver, "new_call_out");	/* Bump its ref */
#endif
    if (num_args > 0) {
	cop->vs = allocate_empty_array(num_args);
	memcpy(cop->vs->item, arg, sizeof(svalue_t) * num_args);
    } else
	cop->vs = 0;

    /* Find out which slot this one fits in */
    tm = (delay + current_time) & (CALLOUT_CYCLE_SIZE - 1);
    /* number of cycles */
    delay = delay / CALLOUT_CYCLE_SIZE;

    DBG(("Current time: %i  Executes at: %i  Slot: %i  Delay: %i",
	   current_time, current_time + delay, tm, delay));

    for (copp = &call_list[tm]; *copp; copp = &(*copp)->next) {
	if ((*copp)->delta > delay) {
	    (*copp)->delta -= delay;
	    cop->delta = delay;
	    cop->next = *copp;
	    *copp = cop;
#ifdef CALLOUT_HANDLES
	    tm += CALLOUT_CYCLE_SIZE * ++unique;
	    cop->handle = tm;
	    return tm;
#else
	    return;
#endif
	}
	delay -= (*copp)->delta;
    }
    *copp = cop;
    cop->delta = delay;
    cop->next = 0;
#ifdef CALLOUT_HANDLES
    tm += CALLOUT_CYCLE_SIZE * ++unique;
    cop->handle = tm;
    return tm;
#endif
}

/*
 * See if there are any call outs to be called. Set the 'command_giver'
 * if it is a living object. Check for shadowing objects, which may also
 * be living objects.
 */
void call_out()
{
    int extra, real_time;
    static pending_call_t *cop = 0;
    error_context_t econ;
    VOLATILE int tm;
    
    current_interactive = 0;

    /* could be still allocated if an error occured during a call_out */
    if (cop) {
	free_called_call(cop);
	cop = 0;
    }

    real_time = get_current_time();
    DBG(("Calling call_outs: current_time: %i real_time: %i difference: %i",
	   current_time, real_time, real_time - current_time));
    
    /* Slowly advance the clock forward towards real_time, doing call_outs
     * as we go.
     */
    save_context(&econ);
    while (1) {
	tm = current_time & (CALLOUT_CYCLE_SIZE - 1);
	DBG(("   slot %i", tm));
	while (call_list[tm] && call_list[tm]->delta == 0) {
	    object_t *ob, *new_command_giver;
	    
	    /*
	     * Move the first call_out out of the chain.
	     */
	    cop = call_list[tm];
	    call_list[tm] = call_list[tm]->next;
	    ob = (cop->ob ? cop->ob : cop->function.f->hdr.owner);

	    DBG(("      /%s", (ob ? ob->name : "(null)")));

	    if (!ob || (ob->flags & O_DESTRUCTED)) {
		DBG(("         (destructed)"));
		free_call(cop);
		cop = 0;
	    } else {
		if (SETJMP(econ.context)) {
		    restore_context(&econ);
		    if (max_eval_error) {
			debug_message("Maximum evaluation cost reached while trying to process call_outs\n");
			pop_context(&econ);
			return;
		    }
		} else {
		    object_t *ob;
		    
		    ob = cop->ob;
#ifndef NO_SHADOWS
		    if (ob)
			while (ob->shadowing)
			    ob = ob->shadowing;
#endif
		    new_command_giver = 0;
#ifdef THIS_PLAYER_IN_CALL_OUT
		    if (cop->command_giver &&
			!(cop->command_giver->flags & O_DESTRUCTED)) {
			new_command_giver = cop->command_giver;
		    } else if (ob && (ob->flags & O_LISTENER)) {
			new_command_giver = ob;
		    }
		    if (new_command_giver)
			DBG(("         command_giver: /%s", new_command_giver->name));
#endif
		    save_command_giver(new_command_giver);
		    /* current object no longer set */
		    
		    if (cop->vs) {
			array_t *vec = cop->vs;
			svalue_t *svp = vec->item + vec->size;
			
			while (svp-- > vec->item) {
			    if (svp->type == T_OBJECT && 
				(svp->u.ob->flags & O_DESTRUCTED)) {
				free_object(svp->u.ob, "call_out");
				*svp = const0u;
			    }
			}
			/* cop->vs is ref one */
			extra = cop->vs->size;
			transfer_push_some_svalues(cop->vs->item, extra);
			free_empty_array(cop->vs);
		    } else
			extra = 0;
		    
		    if (cop->ob) {
			if (cop->function.s[0] == APPLY___INIT_SPECIAL_CHAR)
			    error("Illegal function name\n");
			
			(void) apply(cop->function.s, cop->ob, extra,
				     ORIGIN_INTERNAL);
		    } else {
			(void) call_function_pointer(cop->function.f, extra);
		    }

		    restore_command_giver();
		}
		free_called_call(cop);
		cop = 0;
	    }
	}
	/* Ok, no more scheduled call_outs for current_time */
	if (current_time < real_time) {
	    /* Time marches onward! */
	    if (call_list[tm])
		call_list[tm]->delta--;
	    current_time++;
	    DBG(("   current_time = %i", current_time));
	} else {
	    /* We're done! */
	    break;
	}
    }
    DBG(("Done."));
    pop_context(&econ);
}

static int time_left P2(int, slot, int, delay) {
    int current_slot = current_time & (CALLOUT_CYCLE_SIZE - 1);
    if (slot >= current_slot) {
	return (slot - current_slot) + delay * CALLOUT_CYCLE_SIZE;
    } else {
	return (slot - current_slot) + (delay + 1) * CALLOUT_CYCLE_SIZE;
    }
}

/*
 * Throw away a call out. First call to this function is discarded.
 * The time left until execution is returned.
 * -1 is returned if no call out pending.
 */
int remove_call_out P2(object_t *, ob, char *, fun)
{
    pending_call_t **copp, *cop;
    int delay;
    int i;
    
    if (!ob) return -1;

    DBG(("remove_call_out: /%s \"%s\"", ob->name, fun));

    for (i = 0; i < CALLOUT_CYCLE_SIZE; i++) {
	delay = 0;
	for (copp = &call_list[i]; *copp; copp = &(*copp)->next) {
	    DBG(("   Slot: %i\n", i));
	    delay += (*copp)->delta;
	    if ((*copp)->ob == ob && strcmp((*copp)->function.s, fun) == 0) {
		cop = *copp;
		if (cop->next)
		    cop->next->delta += cop->delta;
		*copp = cop->next;
		free_call(cop);
		DBG(("   found."));
		return time_left(i, delay);
	    }
	}
    }
    DBG(("   not found."));
    return -1;
}

#ifdef CALLOUT_HANDLES
int remove_call_out_by_handle P1(int, handle)
{
    pending_call_t **copp, *cop;
    int delay = 0;

    DBG(("remove_call_out_by_handle: handle: %i slot: %i",
	   handle, handle & (CALLOUT_CYCLE_SIZE - 1)));

    for (copp = &call_list[handle & (CALLOUT_CYCLE_SIZE - 1)]; *copp; copp = &(*copp)->next) {
	delay += (*copp)->delta;
	if ((*copp)->handle == handle) {
	    cop = *copp;
	    if (cop->next)
		cop->next->delta += cop->delta;
	    *copp = cop->next;
	    free_call(cop);
	    return time_left(handle & (CALLOUT_CYCLE_SIZE - 1), delay);
	}
    }
    return -1;
}

int find_call_out_by_handle P1(int, handle) 
{
    pending_call_t *cop;
    int delay = 0;
    
    DBG(("find_call_out_by_handle: handle: %i slot: %i",
	   handle, handle & (CALLOUT_CYCLE_SIZE - 1)));

    for (cop = call_list[handle & (CALLOUT_CYCLE_SIZE - 1)]; cop; cop = cop->next) {
	delay += cop->delta;
	if (cop->handle == handle) 
	    return time_left(handle & (CALLOUT_CYCLE_SIZE - 1), delay);
    }
    return -1;
}
#endif
	
int find_call_out P2(object_t *, ob, char *, fun)
{
    pending_call_t *cop;
    int delay;
    int i;

    if (!ob) return -1;

    DBG(("find_call_out: /%s \"%s\"", ob->name, fun));

    for (i = 0; i < CALLOUT_CYCLE_SIZE; i++) {
	delay = 0;
	DBG(("   Slot: %i", i));
	for (cop = call_list[i]; cop; cop = cop->next) {
	    delay += cop->delta;
	    if (cop->ob == ob && strcmp(cop->function.s, fun) == 0) 
		return time_left(i, delay);
	}
    }
    return -1;
}

int print_call_out_usage P2(outbuffer_t *, ob, int, verbose)
{
    int i, j;
    pending_call_t *cop;

    for (i = 0, j = 0; j < CALLOUT_CYCLE_SIZE; j++)
	for (cop = call_list[j]; cop; cop = cop->next)
	    i++;

    if (verbose == 1) {
	outbuf_add(ob, "Call out information:\n");
	outbuf_add(ob, "---------------------\n");
	outbuf_addv(ob, "Number of allocated call outs: %8d, %8d bytes\n",
		    num_call, num_call * sizeof(pending_call_t));
	outbuf_addv(ob, "Current length: %d\n", i);
    } else {
	if (verbose != -1)
	    outbuf_addv(ob, "call out:\t\t\t%8d %8d (current length %d)\n", num_call,
			num_call * sizeof(pending_call_t), i);
    }
    return (int) (num_call * sizeof(pending_call_t));
}

#ifdef DEBUGMALLOC_EXTENSIONS
void mark_call_outs()
{
    pending_call_t *cop;
    int i;
    
    for (i = 0; i < CALLOUT_CYCLE_SIZE; i++) {
	for (cop = call_list[i]; cop; cop = cop->next) {
	    if (cop->vs)
		cop->vs->extra_ref++;
	    if (cop->ob) {
		cop->ob->extra_ref++;
		EXTRA_REF(BLOCK(cop->function.s))++;
	    } else {
		cop->function.f->hdr.extra_ref++;
	    }
#ifdef THIS_PLAYER_IN_CALL_OUT
	    if (cop->command_giver)
		cop->command_giver->extra_ref++;
#endif
	}
    }
}
#endif

/*
 * Construct an array of all pending call_outs. Every item in the array
 * consists of 3 items (but only if the object not is destructed):
 * 0:	The object.
 * 1:	The function (string).
 * 2:	The delay.
 */
array_t *get_all_call_outs()
{
    int i, j, delay, tm;
    pending_call_t *cop;
    array_t *v;

    for (i = 0, j = 0; j < CALLOUT_CYCLE_SIZE; j++)
	for (cop = call_list[j]; cop; cop = cop->next) {
	    object_t *ob = (cop->ob ? cop->ob : cop->function.f->hdr.owner);
	    if (ob && !(ob->flags & O_DESTRUCTED))
		i++;
	}
    
    v = allocate_empty_array(i);
    tm = current_time & (CALLOUT_CYCLE_SIZE-1);

    for (i = 0, j = 0; j < CALLOUT_CYCLE_SIZE; j++) {
	delay = 0;
	for (cop = call_list[j]; cop; cop = cop->next) {
	    array_t *vv;
	    object_t *ob;
	    
	    delay += cop->delta;
	    ob = (cop->ob ? cop->ob : cop->function.f->hdr.owner);
	    if (!ob || (ob->flags & O_DESTRUCTED))
		continue;
	    vv = allocate_empty_array(3);
	    if (cop->ob) {
		vv->item[0].type = T_OBJECT;
		vv->item[0].u.ob = cop->ob;
		add_ref(cop->ob, "get_all_call_outs");
		vv->item[1].type = T_STRING;
		vv->item[1].subtype = STRING_SHARED;
		vv->item[1].u.string = make_shared_string(cop->function.s);
	    } else {
		vv->item[0].type = T_OBJECT;
		vv->item[0].u.ob = cop->function.f->hdr.owner;
		add_ref(cop->function.f->hdr.owner, "get_all_call_outs");
		vv->item[1].type = T_STRING;
		vv->item[1].subtype = STRING_SHARED;
		vv->item[1].u.string = make_shared_string("<function>");
	    }
	    vv->item[2].type = T_NUMBER;
	    vv->item[2].u.number = time_left(j, delay);
	    
	    v->item[i].type = T_ARRAY;
	    v->item[i++].u.arr = vv;	/* Ref count is already 1 */
	}
    }
    return v;
}

void
remove_all_call_out P1(object_t *, obj)
{
    pending_call_t **copp, *cop;
    int i;
    
    for (i = 0; i < CALLOUT_CYCLE_SIZE; i++) {
	copp = &call_list[i];
	while (*copp) {
	    if ( ((*copp)->ob &&
		  (((*copp)->ob == obj) || ((*copp)->ob->flags & O_DESTRUCTED))) ||
		 (!(*copp)->ob &&
		  ((*copp)->function.f->hdr.owner == obj ||
                   !(*copp)->function.f->hdr.owner ||
		   (*copp)->function.f->hdr.owner->flags & O_DESTRUCTED)) )
		{
		    cop = *copp;
		    if (cop->next)
			cop->next->delta += cop->delta;
		    *copp = cop->next;
		    free_call(cop);
		} else
		    copp = &(*copp)->next;
	}
    }
}

void reclaim_call_outs() {
    pending_call_t *cop;
    int i;
    
    remove_all_call_out(0); /* removes call_outs to destructed objects */
    
#ifdef THIS_PLAYER_IN_CALL_OUT
    for (i = 0; i < CALLOUT_CYCLE_SIZE; i++) {
	cop = call_list[i];
	while (cop) {
	    if (cop->command_giver && (cop->command_giver->flags & O_DESTRUCTED)) {
		free_object(cop->command_giver, "reclaim_call_outs");
		cop->command_giver = 0;
	    }
	    cop = cop->next;
	}
    }
#endif
}