1999Q1/
<!-- MHonArc v2.4.4 -->
<!--X-Subject: [MUD&#45;Dev] Re: processors -->
<!--X-From-R13: Oqnz Ivttvaf <nqnzNnatry.pbz> -->
<!--X-Date: Mon, 1 Feb 1999 10:53:06 &#45;0800 -->
<!--X-Message-Id: Pine.SGI.3.96.990201104136.18066C&#45;100000#zazu,angel.com -->
<!--X-Content-Type: text/plain -->
<!--X-Reference: E107Ckc&#45;0007kR&#45;00#koala,kanga.nu -->
<!--X-Head-End-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>MUD-Dev message, [MUD-Dev] Re: processors</title>
<!-- meta name="robots" content="noindex,nofollow" -->
<link rev="made" href="mailto:adam#angel,com">
</head>
<body background="/backgrounds/paperback.gif" bgcolor="#ffffff"
      text="#000000" link="#0000FF" alink="#FF0000" vlink="#006000">

  <font size="+4" color="#804040">
    <strong><em>MUD-Dev<br>mailing list archive</em></strong>
  </font>
      
<br>
[&nbsp;<a href="../">Other Periods</a>
&nbsp;|&nbsp;<a href="../../">Other mailing lists</a>
&nbsp;|&nbsp;<a href="/search.php3">Search</a>
&nbsp;]
<br clear=all><hr>
<!--X-Body-Begin-->
<!--X-User-Header-->
<!--X-User-Header-End-->
<!--X-TopPNI-->

Date:&nbsp;
[&nbsp;<a href="msg00343.html">Previous</a>
&nbsp;|&nbsp;<a href="msg00345.html">Next</a>
&nbsp;]
&nbsp;&nbsp;&nbsp;&nbsp;
Thread:&nbsp;
[&nbsp;<a href="msg00341.html">Previous</a>
&nbsp;|&nbsp;<a href="msg00342.html">Next</a>
&nbsp;]
&nbsp;&nbsp;&nbsp;&nbsp;
Index:&nbsp;
[&nbsp;<A HREF="author.html#00344">Author</A>
&nbsp;|&nbsp;<A HREF="#00344">Date</A>
&nbsp;|&nbsp;<A HREF="thread.html#00344">Thread</A>
&nbsp;]

<!--X-TopPNI-End-->
<!--X-MsgBody-->
<!--X-Subject-Header-Begin-->
<H1>[MUD-Dev] Re: processors</H1>
<HR>
<!--X-Subject-Header-End-->
<!--X-Head-of-Message-->
<UL>
<LI><em>To</em>: <A HREF="mailto:mud-dev#kanga,nu">mud-dev#kanga,nu</A></LI>
<LI><em>Subject</em>: [MUD-Dev] Re: processors </LI>
<LI><em>From</em>: Adam Wiggins &lt;<A HREF="mailto:adam#angel,com">adam#angel,com</A>&gt;</LI>
<LI><em>Date</em>: Mon, 1 Feb 1999 10:52:58 -0800 (PST)</LI>
<LI><em>Reply-To</em>: <A HREF="mailto:mud-dev#kanga,nu">mud-dev#kanga,nu</A></LI>
</UL>
<!--X-Head-of-Message-End-->
<!--X-Head-Body-Sep-Begin-->
<HR>
<!--X-Head-Body-Sep-End-->
<!--X-Body-of-Message-->
<PRE>
On Sun, 31 Jan 1999, J C Lawrence wrote:
&gt; diablo &lt;diablo#best,com&gt; wrote:
&gt; &gt; A 3 second task would take 20 seconds, but this only happened with a
&gt; &gt; heavy player load. Our tasks are held in a table indexed to some
&gt; &gt; number (some sort of ticker in linux? I don't know the proper names
&gt; &gt; for anything). That table is polled every time the game searches for
&gt; &gt; player input (it cycles through all our player lines constantly),
&gt; &gt; and if a task is found to be either overdue or ready to go off, it
&gt; &gt; goes off. 
&gt; 
&gt; Ouch.
&gt; 
&gt; Cheaper:
&gt; 
&gt;   1) Keep the table, but sort it by due time and pProcess the table
&gt; only up to the first record that has a due date in the future.  You
&gt; need to take care in your table and sort code to make it minimally
&gt; expensive (choice of sorting algorithm), and in your storage format to
&gt; ensure minimal traversal and ordering expense (no buffer copies, only
&gt; pointer copies if possible).  

Standard event handlers.  I've written them at the start of so many projects
now I think I could do it blind, typing with my nose, and still finish
in less than 90 seconds.

In fact, here we go:

typedef float Tick;

class Event
{
	friend class EventManager;

public:
	Event(Tick time)
	{
		RipenTime = EventManager::GetCurrentTime() + time;
		EventManager::AddEvent(this);
	}

	virtual void Ripen() = 0;

	bool RipensBefore(Event *e)	{ return (RipenTime &lt; e-&gt;RipenTime); }
	bool RipensBy(Tick time)	{ return (RipenTime &lt;= time); }
	
private:
	Tick RipenTime;			// time at which event ripens
	bool HasRipened;		// flag for deletion
	Event *Next;
};

class EventManager
{
public:	
	EventManager() { Events = NULL; GameTick = 0; }
	static void Update()
	{
		bool update = false;

		// Execute each event that is due to ripen
		Event *e;
		for (e = Events; e; e = e-&gt;Next)
		{
			if (e-&gt;RipensBy(GameTick))
			{
				e-&gt;Ripen();
				e-&gt;HasRipened = true;
				update = true;
			}
		}

		// Second pass, delete all ripened events
		if (update)
		{
			Event *next, *prev = NULL;
			for (e = Events; e; e = Next)
			{
				next = e-&gt;Next;

				if (!e-&gt;HasRipened)
					prev = e;
				else
				{
					if (prev)
						prev-&gt;Next = e-&gt;Next;
					else
						events = e-&gt;Next;

					delete e;
				}
			}
		}

		// Increment the game timer
		GameTick += TimeManager::GetTicksEllapsedLastUpdate();
	}

	static void AddEvent(Event *newEvent);
	{
		// Descend the list until a later event is found
		Event *e, *prev = NULL;
		for (e = events; e &amp;&amp; e-&gt;RipensBefore(newEvent); e = e-&gt;Next)
			prev = e;

		// Insert the new event before that event
		newEvent-&gt;HasRipened = false;
		newEvent-&gt;Next = e;
		if (prev)
			prev-&gt;Next = newEvent;
		else
			Events = newEvent;
	}
	static Tick GetCurrentTime()		{ return GameTick; }

protected:
	static Event *Events;
	static Tick GameTick;
};


This could, of course, easily be converted to C (I'd be happy to do so
if anyone desires, it'd take probably another 90 seconds).
Those into design patterns would probably also do the event manager as
a singleton (or perhaps have multiple event managers for different types
of events) rather than using all statics, but I wanted to keep the example
simple.


Adam W.




</PRE>

<!--X-Body-of-Message-End-->
<!--X-MsgBody-End-->
<!--X-Follow-Ups-->
<HR>
<!--X-Follow-Ups-End-->
<!--X-References-->
<UL><LI><STRONG>References</STRONG>:
<UL>
<LI><STRONG><A NAME="00340" HREF="msg00340.html">[MUD-Dev] Re: processors</A></STRONG>
<UL><LI><EM>From:</EM> J C Lawrence &lt;claw#kanga,nu&gt;</LI></UL></LI>
</UL></LI></UL>
<!--X-References-End-->
<!--X-BotPNI-->
<UL>
<LI>Prev by Date:
<STRONG><A HREF="msg00343.html">[MUD-Dev] Re: processors</A></STRONG>
</LI>
<LI>Next by Date:
<STRONG><A HREF="msg00345.html">[MUD-Dev] Re: processors</A></STRONG>
</LI>
<LI>Prev by thread:
<STRONG><A HREF="msg00341.html">[MUD-Dev] Re: processors</A></STRONG>
</LI>
<LI>Next by thread:
<STRONG><A HREF="msg00342.html">[MUD-Dev] Re: processors</A></STRONG>
</LI>
<LI>Index(es):
<UL>
<LI><A HREF="index.html#00344"><STRONG>Date</STRONG></A></LI>
<LI><A HREF="thread.html#00344"><STRONG>Thread</STRONG></A></LI>
</UL>
</LI>
</UL>

<!--X-BotPNI-End-->
<!--X-User-Footer-->
<!--X-User-Footer-End-->
<ul><li>Thread context:
<BLOCKQUOTE><UL>
<LI><STRONG>[MUD-Dev] Re: processors</STRONG>, <EM>(continued)</EM>
<ul compact>
<ul compact>
<LI><strong><A NAME="00331" HREF="msg00331.html">[MUD-Dev] Re: processors</A></strong>, 
diablo <a href="mailto:diablo#best,com">diablo#best,com</a>, Sun 31 Jan 1999, 03:00 GMT
<UL>
<LI><strong><A NAME="00336" HREF="msg00336.html">[MUD-Dev] Re: processors</A></strong>, 
Mik Clarke <a href="mailto:mikclrk#ibm,net">mikclrk#ibm,net</a>, Sun 31 Jan 1999, 18:50 GMT
</LI>
<LI><strong><A NAME="00340" HREF="msg00340.html">[MUD-Dev] Re: processors</A></strong>, 
J C Lawrence <a href="mailto:claw#kanga,nu">claw#kanga,nu</a>, Mon 01 Feb 1999, 06:22 GMT
<UL>
<LI><strong><A NAME="00341" HREF="msg00341.html">[MUD-Dev] Re: processors</A></strong>, 
Marc Hernandez <a href="mailto:marc#ias,jb.com">marc#ias,jb.com</a>, Mon 01 Feb 1999, 07:08 GMT
</LI>
<LI><strong><A NAME="00344" HREF="msg00344.html">[MUD-Dev] Re: processors</A></strong>, 
Adam Wiggins <a href="mailto:adam#angel,com">adam#angel,com</a>, Mon 01 Feb 1999, 18:53 GMT
</LI>
</UL>
</LI>
<LI><strong><A NAME="00342" HREF="msg00342.html">[MUD-Dev] Re: processors</A></strong>, 
Petri Virkkula <a href="mailto:pvirkkul#iki,fi">pvirkkul#iki,fi</a>, Mon 01 Feb 1999, 07:36 GMT
</LI>
</UL>
</LI>
<LI><strong><A NAME="00338" HREF="msg00338.html">[MUD-Dev] Re: processors</A></strong>, 
Greg Underwood <a href="mailto:gunderwood#donet,com">gunderwood#donet,com</a>, Mon 01 Feb 1999, 02:15 GMT
<UL>
<LI><strong><A NAME="00343" HREF="msg00343.html">[MUD-Dev] Re: processors</A></strong>, 
Adam Wiggins <a href="mailto:adam#angel,com">adam#angel,com</a>, Mon 01 Feb 1999, 18:38 GMT
</LI>
</UL>
</LI>
</ul>
</ul>
</LI>
<LI><strong><A NAME="00306" HREF="msg00306.html">[MUD-Dev] Re: Mud reviewing</A></strong>, 
Caliban Tiresias Darklock <a href="mailto:caliban#darklock,com">caliban#darklock,com</a>, Wed 27 Jan 1999, 16:10 GMT
</LI>
</UL></BLOCKQUOTE>

</ul>
<hr>
<center>
[&nbsp;<a href="../">Other Periods</a>
&nbsp;|&nbsp;<a href="../../">Other mailing lists</a>
&nbsp;|&nbsp;<a href="/search.php3">Search</a>
&nbsp;]
</center>
<hr>
</body>
</html>