Events/
/* I've noticed a lack of a fully event driven engine out there, so I'll
   upload mine to you wonderful MudBytes people!
 */

/* This is the first time I've actually written a snippet in years! So lets
   see if I do better than some of my other ones :). First off, if you're going
   to use it, I'd -love- to hear from you at DavionKalhen@Gmail.Com or DavionKalhen
   on aim! Not required, at all. As for giving me credit, mention me somewhere,
   sometime for something ;)
 */

//Begin mods to MERC.H
	//Add with other typedefs
	
	typedef struct	event_data			EVENT_DATA;
	
	//Add this to merc.h where ever you want.
	#define EVENT_PRINT		0
	#define SECONDS(s) (s * PULSE_PER_SECOND)
	#define HALFSEC(s) (s * (PULSE_PER_SECOND/2))
	#define TYPE_CHAR		0
	#define TYPE_OBJ		1
	#define HAS_EVENT(ch)   ( (ch)->event_list )
	#ifndef MSL
	#define MSL MAX_STRING_LENGTH
	#endif
	/******************************************
	* One Event                              *
	******************************************/
	struct event_data
	{	CHAR_DATA	*	ch;
		OBJ_DATA	*	obj;
		int			type;
		EVENT_DATA	*	next;		//The globle linked list.
		EVENT_DATA  	*	next_event;	//The list on a the character/object.
		EVENT_DATA	*	prev;
		EVENT_DATA	*	prev_event;
		char		*	string;
		int			value;
		void		*	arg;
		int			delay;
		int			event;
		DO_FUN		*	do_fun;
	};
	struct event_type
	{	char *name;
		int event;
		char **cmdlist;
	};
	extern const   struct  event_type              event_table[];
	
	//Add both these lines to your 'struct obj_data' as well as your 'struct char_data'
	//in merc.h
	
		EVENT_DATA		*	event_list;
		EVENT_DATA		*	event_last;
	
	//Add this with your global variables (look for 'extern')
	
	extern EVENT_DATA *event_list;
	extern EVENT_DATA *event_last;
	
	//Add this to your prototype list (Look for comm.c or any .c file in merc.h
	
	/* event.c */
	#define ED	EVENT_DATA
	void		event_update		args(());
	void		purge_all_events	args((CHAR_DATA *ch));
	void		event_to_char		args((CHAR_DATA *ch, char *string, void *arg, int value, int type, int delay ));
	void		execute_char_event	args((CHAR_DATA * ch, EVENT_DATA * event));
	void		execute_obj_event	args((OBJ_DATA *pObj, EVENT_DATA *event));
	void		event_movement		args((CHAR_DATA *ch ));
	ED	*	get_specific_event	args(( CHAR_DATA *ch, int type ));
	bool		check_eventcmd		args((CHAR_DATA *ch, char *cmd ));
	void		free_event		args((CHAR_DATA *ch, OBJ_DATA *pObj, EVENT_DATA *event ));
	ED	*	event_new 		args(());
	void 		event_to_obj		args((OBJ_DATA *obj, CHAR_DATA *ch, char *string, void *arg, int value, int type, int delay ));
	void		purge_obj_event		args((OBJ_DATA *));
	void 		eprintf_to_char		args(( CHAR_DATA *ch, int delay, char *fmt, ... ));
	#undef ED

//End of Mods to MERC.H

//Begin Mods to UPDATE.C
	//Add this in update_handler just after the variable definitions (ie. static int pulse_area)
		event_update();

//End Mods to UPDATE.C

//Begin Mods to INTERP.C

	//Right ABOVE the "Log and snoop" comment in interpret() add
	
		if( HAS_EVENT(ch) )
		{       if(!check_eventcmd(ch, cmd_table[cmd].name) )
			{       send_to_char("You're a bit busy at the moment.\n\r",ch);
				return;
			}
		}
//End Mods to INTERP.C

//Begin Mods to RECYCLE.C
	
	//Add to new_char() with other variables
	ch->event_list = ch->event_last = NULL;
	
	//Add to free_char() under free_pcdata(ch);
	purge_all_events(ch);

//End Mods to RECYCLE.C


/* Ok! That's pretty much it. Just throw event.c into your src directory
   and add event.o to your Makefile, and run a clean make. To test things,
   add this new function (make sure to add to the interp table and to interp.h)
 */

void do_testevent(CHAR_DATA *ch, char *argument)
{	eprintf_to_char(ch, SECONDS(10), "It's been 10 seconds! Run %s.\n\r", ch->name);
	send_to_char("Text should appear in 10 seconds. Take it's advice.\n\r",ch);
	return;
}

/* If you get text after 10 seconds, it worked! If not, drop me a line :) */