March 1999
- Storytelling vs simulation, Koster, Raph
- Storytelling vs simulation, Eli Stevens {KiZurich}
- Storytelling vs simulation, Mik Clarke
- Storytelling vs simulation, J C Lawrence
- Storytelling vs simulation, Matthew Mihaly
- Storytelling vs simulation, J C Lawrence
- Storytelling vs simulation, Matthew Mihaly
- Storytelling vs simulation, Caliban Tiresias Darklock
- OT: MUSH semantics (was: Influential muds) T. Alexander Popiel
- Storytelling and Gods (fairly long) Matthew Mihaly
- Storytelling and Gods (fairly long) Koster, Raph
- Storytelling and Gods (fairly long) Matthew Mihaly
- Generic event handling Adam Wiggins
A little while back I posted some generic event-handling code in C++.
I got a request for a C version, so here it is.
All of this is completely untested, I just typed it in off the top of
my head, so there may be typos or other compile errors.
Here's the C++ code, as a reminder:
------------------------
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 < e->RipenTime); }
bool RipensBy(Tick time) { return (RipenTime <= 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->Next)
{
if (e->RipensBy(GameTick))
{
e->Ripen();
e->HasRipened = true;
update = true;
}
}
// Second pass, delete all ripened events
if (update)
{
Event *next, *prev = NULL;
for (e = Events; e; e = Next)
{
next = e->Next;
if (!e->HasRipened)
prev = e;
else
{
if (prev)
prev->Next = e->Next;
else
events = e->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 && e->RipensBefore(newEvent); e = e->Next)
prev = e;
// Insert the new event before that event
newEvent->HasRipened = false;
newEvent->Next = e;
if (prev)
prev->Next = newEvent;
else
Events = newEvent;
}
static Tick GetCurrentTime() { return GameTick; }
protected:
static Event *Events;
static Tick GameTick;
};
-----------------------------
Here's the C version:
------------------------
/* If you don't have bool in your C compiler use this */
typedef char bool;
#define true 1
#define false 0
/* end bool section */
typedef float Tick;
#define EVENT_ALPHA 0
#define EVENT_BETA 1
#define EVENT_GAMMA 2
#define NUM_EVENTS (EVENT_GAMMA + 1)
type struct SEvent
{
int Type; /* EVENT_x */
Tick RipenTime; /* time at which event ripens */
void *Data; /* any extra data */
bool HasRipened; /* flag for deletion */
struct SEvent *Next;
} Event;
typedef void (*EventCallback)(void *);
EventCallback EventAlpha;
EventCallback EventBeta;
EventCallback EventGamma;
EventCallback EventCallBacks[NUM_EVENTS] { EventAlpha, EventBeta, EventGamma };
Event *EventList = NULL;
Tick GameTick = 0;
void AddEvent(int type, Tick time, void *data)
{
/* Create the event */
Event *newEvent = (struct Event *)malloc(sizeof(struct Event));
newEvent->Type = type;
newEvent->RipenTime = GameTick + time;
newEvent->Data = data;
/* Descend the list until a later event is found */
Event *e, *prev = NULL;
for (e = events; e && e->RipenTime < newEvent->RipenTime; e = e->Next)
prev = e;
// Insert the new event before that event
newEvent->HasRipened = false;
newEvent->Next = e;
if (prev)
prev->Next = newEvent;
else
Events = newEvent;
}
/* Call the function below during your update loop */
/* The parameter is how many game ticks have passed since the last update */
void EventUpdate(Tick ticks)
{
GameTick += ticks;
bool update = false;
/* Execute each event that is due to ripen */
Event *e;
for (e = Events; e; e = e->Next)
{
if (e->RipenTime <= GameTick)
{
(*EventCallbacks[e->Type])(e->Data);
e->HasRipened = true;
update = true;
}
}
/* Second pass, delete all ripened events */
if (update)
{
Event *next, *prev = NULL;
for (e = Events; e; e = Next)
{
next = e->Next;
if (!e->HasRipened)
prev = e;
else
{
if (prev)
prev->Next = e->Next;
else
events = e->Next;
delete e;
}
}
}
}
-----------------------------
One thing is that this uses event "types", which are probably going to be
handy on a MUD where you're calling the same sort of event all the time.
You could easily get rid of all the type stuff and just pass the callback
function directly if you wanted to make it even more generic.
You could even mix the two (that is, store the callback pointer on the
event structure, but have a special add function that takes a type and
just looks up the callback right then and there and stores it on the event).
Adam W. - How to support 1000+ simultaneous connections, and some philosophy. Ben Greear
- How to support 1000+ simultaneous connections, and some philosophy. Matthew D. Fuller
- How to support 1000+ simultaneous connections, and some philosophy. Chris Gray
- How to support 1000+ simultaneous connections, and some philosophy. Chris Gray
- How to support 1000+ simultaneous connections, and some philosophy. Caliban Tiresias Darklock
- How to support 1000+ simultaneous connections, and some philosophy. Nathan F Yospe
- How to support 1000+ simultaneous connections, and some philosophy. Caliban Tiresias Darklock
- How to support 1000+ simultaneous connections, and some philosophy. Jason Spangler
- How to support 1000+ simultaneous connections, and some philosophy. Oliver Jowett
- How to support 1000+ simultaneous connections, and some philosophy. Oliver Jowett
- How to support 1000+ simultaneous connections, and some philosophy. Chris Gray
- How to support 1000+ simultaneous connections, and some philosophy. Caliban Tiresias Darklock
- How to support 1000+ simultaneous connections, and some philosophy. Chris Gray
- How to support 1000+ simultaneous connections, and some philosophy. Petri Virkkula
- How to support 1000+ simultaneous connections, and some philosophy. Chris Gray
- How to support 1000+ simultaneous connections, and some philosophy. Petri Virkkula
- How to support 1000+ simultaneous connections, and some philosophy. J C Lawrence
- How to support 1000+ simultaneous connections, and some philosophy. Petri Virkkula
- ADMIN: Kanga.Nu outage and other news -- please read J C Lawrence
- Elder Games Martin C Sweitzer
- Elder Games Adam Wiggins
- Elder Games Koster, Raph
- Elder Games Matthew Mihaly
- Elder Games Caliban Tiresias Darklock
- Elder Games Martin C Sweitzer
- Elder Games Matthew Mihaly
- Elder Games Kylotan
- Elder Games Caliban Tiresias Darklock
- Elder Games Kylotan
- Elder Games Koster, Raph
- Elder Games B. Scott Boding
- Elder Games Wes Connell
- Elder Games Caliban Tiresias Darklock
- Elder Games Matthew D. Fuller
- Elder Games B. Scott Boding
- Elder Games Michael Hohensee
- Elder Games Matthew Mihaly
- Elder Games Benjamin D. Wiechel
- Elder Games Chris Gray
- Elder Games Chris Gray
- Elder Games J C Lawrence
- Elder Games Nathan F Yospe
- Elder Games J C Lawrence
- Multiple clients (was How to support 1000+ simultaneous connections) Matthew D. Fuller
- Multiple clients (was How to support 1000+ simultaneous connections) Chris Gray
- Naming and Directories? Mark Gritter
- Naming and Directories? Matthew D. Fuller
- Naming and Directories? Adam Wiggins
- Naming and Directories? Hans-Henrik Staerfeldt
- Naming and Directories? Chris Gray
- Naming and Directories? Mark Gritter
- Naming and Directories? Mik Clarke
- Naming and Directories? Mark Gritter
- Naming and Directories? Nathan F Yospe
- Naming and Directories? Ola Fosheim Grøstad
- Naming and Directories? Nathan F Yospe
- Naming and Directories? Mik Clarke
- Naming and Directories? Mark Gritter
- Naming and Directories? Jon A. Lambert
- Naming and Directories? Hans-Henrik Staerfeldt
- Naming and Directories? Ola Fosheim Grøstad
- Naming and Directories? Chris Gray
- Naming and Directories? Caliban Tiresias Darklock
- Naming and Directories? Ben Greear
- Naming and Directories? Mik Clarke
- Naming and Directories? Chris Gray
- Naming and Directories? Chris Gray
- Naming and Directories? Chris Gray
- Naming and Directories? Jo Dillon
- Naming and Directories? J C Lawrence
- Naming and Directories? Mark Gritter
- Naming and Directories? Ola Fosheim Grøstad
- Naming and Directories? Mark Gritter
- Naming and Directories? Ola Fosheim Grøstad
- Naming and Directories? Chris Gray
- Naming and Directories? J C Lawrence
- Naming and Directories? Jo Dillon
- Naming and Directories? Jay Carlson
- Naming and Directories? Jon A. Lambert
- Naming and Directories? Ola Fosheim Grøstad
- Naming and Directories? J C Lawrence
- Naming and Directories? Ola Fosheim Grøstad
- Balancing a Mud Martin C Sweitzer
- Balancing a Mud Neerenberg, AaronX
- Balancing a Mud Mik Clarke
- Balancing a Mud Martin C Sweitzer
- Multiple clients (was How to support 1000+ simultaneous connec Marc Bowden
- ADMIN: Signature length J C Lawrence
- distributed, _untrusted_ servers Oliver Jowett
- OT ADMIN: Web links to MUD-Dev J C Lawrence
- (fwd) MUD Economies J C Lawrence
- (fwd) MUD Economies J C Lawrence
- (fwd) MUD Economies J C Lawrence
- (fwd) MUD Economies J C Lawrence
- Potential New Laws Benjamin D. Wiechel
- Mud Economies (A simple idea) Wes Connell
- Self-organizing worlds (was: Elder Games) B. Scott Boding
- Self-organizing worlds (was: Elder Games) Mik Clarke
- Self-organizing worlds (was: Elder Games) Koster, Raph
- Self-organizing worlds (was: Elder Games) Nicholas Lee
- Self-organizing worlds (was: Elder Games) Koster, Raph
- Self-organizing worlds (was: Elder Games) B. Scott Boding
- Self-organizing worlds (was: Elder Games) Nicholas Lee
- Self-organizing worlds (was: Elder Games) Ola Fosheim Grøstad
- Self-organizing worlds (was: Elder Games) Martin Keegan
- Self-organizing worlds (was: Elder Games) Ola Fosheim Grøstad
- Self-organizing worlds (was: Elder Games) Nicholas Lee
- Mass Creation OLC Functions (idea from Elder Games) Wes Connell
- Mass Creation OLC Functions (idea from Elder Games) Matthew Mihaly
- Mass Creation OLC Functions (idea from Elder Games) Nathan F Yospe
- Mass Creation OLC Functions (idea from Elder Games) Matthew Mihaly
- Mass Creation OLC Functions (idea from Elder Games) Nathan F Yospe
- Mass Creation OLC Functions (idea from Elder Games) Ola Fosheim Grøstad
- Mass Creation OLC Functions (idea from Elder Games) Nathan F Yospe
- Mass Creation OLC Functions (idea from Elder Games) Ola Fosheim Grøstad
- Mass Creation OLC Functions (idea from Elder Games) Brandon A Downey
- Mass Creation OLC Functions (idea from Elder Games) Adam Wiggins
- Mass Creation OLC Functions (idea from Elder Games) Martin C Sweitzer
- Mass Creation OLC Functions (idea from Elder Games) Quzah [softhome]
- Mass Creation OLC Functions (idea from Elder Games) Richard Woolcock
- Mass Creation OLC Functions (idea from Elder Games) Chris Gray
- Mass Creation OLC Functions (idea from Elder Games) J C Lawrence
- Mass Creation OLC Functions (idea from Elder Games) Christopher Allen
- Mass Creation OLC Functions (idea from Elder Games) Matthew Mihaly
- Mass Creation OLC Functions (idea from Elder Games) Chris Gray
- Mass Creation OLC Functions (idea from Elder Games) J C Lawrence
- Mass Creation OLC Functions (idea from Elder Games) J C Lawrence
- On the topic of Mud AI Leif Hardison
- On the topic of Mud AI Nicholas Lee
- On the topic of Mud AI Andrew Norman
- Unicode, ascii and names Ola Fosheim Grøstad
- Variable-sized structures in C (was: Naming and Directories) T. Alexander Popiel
- Variable-sized structures in C (was: Naming and Directories) Ola Fosheim Grøstad
- Renaming objects. John Hopson
- Renaming objects. David Bennett
- Variable-sized structures in C (was: Naming and Directories) Petri Virkkula
- Self-organizing worlds (was: Elder Games) Koster, Raph
- Self-organizing worlds (was: Elder Games) Ola Fosheim Grøstad
- online economy behavior (was: Self-organizing worlds) Robert Green
- online economy behavior (was: Self-organizing worlds) Matthew Mihaly
- online economy behavior (was: Self-organizing worlds) Adam Wiggins
- online economy behavior (was: Self-organizing worlds) Robert Green
- online economy behavior (was: Self-organizing worlds) Matthew Mihaly
- online economy behavior (was: Self-organizing worlds) Matthew Mihaly
- online economy behavior (was: Self-organizing worlds) Christopher Allen
- Self-organizing worlds (was: Elder Games) Koster, Raph
- Self-organizing worlds (was: Elder Games) Koster, Raph
- Self-organizing worlds (was: Elder Games) Chris Gray
- Self-organizing worlds (was: Elder Games) Benjamin D. Wiechel
- Self-organizing worlds (was: Elder Games) Mik Clarke
- online economy behavior (was: Self-organizing worlds) Chris Gray
- OT: just a little something... Ola Fosheim Grøstad
- (fwd) implications J C Lawrence
- Downtime J C Lawrence
- Getting Started with Mud Server Stormblade
- Getting Started with Mud Server Ross Nicoll
- Getting Started with Mud Server Jim Clark
- Getting Started with Mud Server Ben Greear
- Getting Started with Mud Server Chris Gray
- Getting Started with Mud Server Jo Dillon
- Getting Started with Mud Server Hans-Henrik Staerfeldt
- Terms Ola Fosheim Grøstad