Discussion:
Modeling events that occur in a game world
(too old to reply)
Aaron J. M.
2007-04-21 17:24:32 UTC
Permalink
I need a way of representing and tracking different events that occur
in a game
I'm working on. I have a world composed of one or more Maps. Each
Map location
(coordinate) may or may not have a Creature. A Creature is controlled
by an AI,
which may also be the player's UI, though the Creatures themselves are
not
necessarily aware of their AIs. Creatures may move to different
coordinates or
attack other Creatures.

Right now the two events I know I'll want to keep track of is when a
Creature
attacks another Creature and when a Creature dies, and I see myself
coming up
with new events as my game grows more intricate. Different parts of
my game
would be interested in different kinds of events. The graphical
display would
be interested in all events so that it may display an appropriate
animation.
More importantly, the different AIs would need to observe recent
events to
modify their behaviour. If a Creature is attacked, the AI controlling
it has a
vested interest in knowing about it.

I'm having trouble modeling this because the different kinds of events
would
keep potentially arbitrary information, making it hard to generalize.
An attack
event would know the attacking Creature, the target Creature, and the
amount of
damage done. A death event would just know the Creature that died. I
might
also implement Creatures grabbing and throwing other Creatures, so a
throw event
would need to know the throwing Creature, the thrown Creature, and
where the
thrown Creature landed. For this reason I can't really make them
subclasses of
a single Event class, since they hold such different information.

So how would I model these events and let my system handle and respond
to them?

Thanks
Leslie Sanford
2007-04-21 17:48:30 UTC
Permalink
"Aaron J. M."

<snip>
Post by Aaron J. M.
So how would I model these events and let my system handle and respond
to them?
Perhaps have each event represented by a unique ID? The ID would just be
an integer representing the event. So the events wouldn't necessarily be
represented by a class hierarchy.

In your app you could have an event queue. Objects that are capable of
generating events have access to the queue and can post events to it.
Objects that are interested in events can register with the queue to be
notified when certain events occur, e.g. tell me when a creature is
slain, etc.

So when a creature in your game does something interesting, it posts an
event to the event queue. Objects that are interested in this event are
notified when the event queue processes the event.

As far as accompanying data for each event, you may have to do some
casting. When an event is enqueued, some accompanying data is enqueued
with it in a generic form, e.g. a void pointer in C or an Object in C#
or Java. Later when the event is dequeued, this generic object is cast
to the proper type. It would require either the event queue or the
receiver of the event know how to unpack the data.
Aaron J. M.
2007-04-21 20:21:40 UTC
Permalink
Post by Leslie Sanford
Perhaps have each event represented by a unique ID? The ID would just be
an integer representing the event. So the events wouldn't necessarily be
represented by a class hierarchy.
In your app you could have an event queue.
<snip>
As far as accompanying data for each event, you may have to do some
casting.
<snip>
Later when the event is dequeued, this generic object is cast
to the proper type. It would require either the event queue or the
receiver of the event know how to unpack the data.
An event queue was what I thought up at first. I thought of having a
generic
Event class that goes into this queue, and there'd be subclasses like
AttackEvent, DeathEvent, BurningRobotAppearsEvent, etc. The problem
with that
is how the information attached to each specific event is lost. The
Visitor
pattern looked like it may address this, considering that there are
several
potential EventVisitors such as the AI or the GUI. However, not all
parts of
the system are likely to be interested in every single type of event,
especially
if I come up with several kinds of events.

Your idea for events having unique IDs is interesting. It doesn't
help with
data carried with the event though. Rather than have a unique ID and
try to
unpack the information like you suggested, it may seem simpler to just
check the
type of the event object directly and then cast it. That may lead to
a lot of
case statements though.
Leslie Sanford
2007-04-22 00:57:45 UTC
Permalink
Post by Aaron J. M.
An event queue was what I thought up at first. I thought of having a
generic Event class that goes into this queue, and there'd be
subclasses like AttackEvent, DeathEvent, BurningRobotAppearsEvent,
etc. The problem with that is how the information attached to each
specific event is lost. The Visitor pattern looked like it may
address this, considering that there are several potential
EventVisitors such as the AI or the GUI. However, not all parts of
the system are likely to be interested in every single type of event,
especially if I come up with several kinds of events.
Your idea for events having unique IDs is interesting. It doesn't
help with data carried with the event though. Rather than have a
unique ID and try to unpack the information like you suggested, it may
seem simpler to just check the type of the event object directly and
then cast it. That may lead to a lot of case statements though.
Unpacking the information would be the responsibility of the objects
receiving the event. It would look something like this in C#:

enum EventID
{
Attack,
Defeat,
RobotAppears,

// Other event ID's...
}

public class GameEventArgs : EventArgs // EventArgs is a .NET base class
// for all event classes.
{
private EventID id;

public GameEventArgs(EventID id)
{
this.id = id;
}

public EventID ID
{
get
{
return id;
}
}
}

public class AttackEventArgs : GameEventArgs
{
public AttackEventArgs() : base(EventID.Attack)
{
}

// Other stuff...
}

And we could have several more event classes that are derived from the
GameEventArgs class.

We have our event queue and objects interested in registering to be
notified when an event occurs; we could do something like this:

EventQueue eventQueue = new EventQueue();
Creature someCreature = new Creature();

eventQueue.Register(EventID.Attack, someCreature);

The Creature class would implement an interface, as would all classes
that are interested in receiving events:

public interface EventListener
{
void ProcessEvent(GameEventArgs e);
}

When the event queue dequeues an event, it checks the event's ID and
notifies all objects interested in that event by passing the
GameEventArgs object to them. It's then up to the receiver of the event
to unpack it, i.e. cast it to its specific type:

public class Creature : EventListener
{
public void ProcessEvent(GameEventArgs e)
{
switch(e.ID)
{
case EventID.Attack:
{
AttackEventArgs attackEvent = (AttackEventArgs)e;

// Do something in response to the attack...
}
break;

default:
Debug.Fail("Unknown event");
break;
}
}
}

Each class knows what events it's interested in, so it switches on only
those event ID's.

BTW, it's best if event objects are immutable, that way there's no harm
in passing the same event object to multiple receivers.

If having each class implement a switch on the ID seems excessive to
you, as it may to some, you can still use the Visitor approach but give
Visitor class do nothing implementations for each of the visit methods.
Derived classes only override those methods specific to the events they
are interested in. A drawback to this approach is that if you're using a
language that only supports single inheritance, you lose the ability to
have your class derived from another class as it has to derive from the
Visitor class.
Dmitry A. Kazakov
2007-04-22 07:17:52 UTC
Permalink
I need a way of representing and tracking different events that occur in a game
I'm working on. I have a world composed of one or more Maps. Each Map location
(coordinate) may or may not have a Creature. A Creature is controlled by an AI,
which may also be the player's UI, though the Creatures themselves are not
necessarily aware of their AIs. Creatures may move to different coordinates or
attack other Creatures.
Right now the two events I know I'll want to keep track of is when a Creature
attacks another Creature and when a Creature dies, and I see myself coming up
with new events as my game grows more intricate. Different parts of my game
would be interested in different kinds of events. The graphical display would
be interested in all events so that it may display an appropriate animation.
More importantly, the different AIs would need to observe recent events to
modify their behaviour. If a Creature is attacked, the AI controlling it has a
vested interest in knowing about it.
I'm having trouble modeling this because the different kinds of events would
keep potentially arbitrary information, making it hard to generalize. An attack
event would know the attacking Creature, the target Creature, and the amount of
damage done. A death event would just know the Creature that died. I might
also implement Creatures grabbing and throwing other Creatures, so a throw event
would need to know the throwing Creature, the thrown Creature, and where the
thrown Creature landed. For this reason I can't really make them subclasses of
a single Event class, since they hold such different information.
Why are they events? I would say, that death, under attack, flight etc are
not events, they are states. If creatures are models of physical objects,
then the only event they need is a timer or scheduling event. The object
reads the sensors, changes its state and writes the actuators.
So how would I model these events and let my system handle and respond to them?
Multiple-dispatching operations, if your language can it. Otherwise you
have to emulate multiple dispatch.
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
Aaron J. M.
2007-04-22 13:40:06 UTC
Permalink
Post by Leslie Sanford
When the event queue dequeues an event, it checks the event's ID and
notifies all objects interested in that event by passing the
GameEventArgs object to them. It's then up to the receiver of the event
public class Creature : EventListener
{
public void ProcessEvent(GameEventArgs e)
{
switch(e.ID)
{
{
AttackEventArgs attackEvent = (AttackEventArgs)e;
// Do something in response to the attack...
}
break;
Debug.Fail("Unknown event");
break;
}
}
}
O.K. However, if I'm going to have to do casting and have a bunch of
switch
statements anyway, what's wrong with:

if e instanceof AttackEventArgs
AttackEventArgs attackEvent = (AttackEventArgs)e
else if ...

I'm only asking because using instanceof seems a bit easier. Would
the ID
number be better for performance though?
Post by Leslie Sanford
Why are they events? I would say, that death, under attack, flight etc are
not events, they are states. If creatures are models of physical objects,
then the only event they need is a timer or scheduling event. The object
reads the sensors, changes its state and writes the actuators.
So what you're suggesting is that my "events" should become internal
to the
creature, right? For example, if a creature starts attacking
something, it should
keep track of what it's attacking and how much damage it did by
itself, where this
information would be visible to other objects that can observe the
creature.

However, what if I have events that are not generated by creatures?
For example,
I may decide to have an earthquake happen for some reason, and I want
some
creature AIs to be able to tell that an earthquake happened so that
they can get
scared off. How would I model that?
Leslie Sanford
2007-04-22 15:07:24 UTC
Permalink
Post by Aaron J. M.
O.K. However, if I'm going to have to do casting and have a bunch of
if e instanceof AttackEventArgs
AttackEventArgs attackEvent = (AttackEventArgs)e
else if ...
I'm only asking because using instanceof seems a bit easier. Would
the ID number be better for performance though?
When the event queue dequeues an event and needs to notify the objects
that have registered for the event, it will need a key of some type to
retreive the list of registered objects. An integer would be ideal. So
when the event queue dequeues an event, it uses the event's ID to
retreive a list of objects interested in that event:

GameEventArgs e = queue.Dequeue();
List list = listeners.GetValue(e.ID);

foreach(Listener l in list)
{
l.ProcessEvent(e);
}

When an object registers to be notified when a specific type of event
occurs, it is placed in the event queue's dictionary of listeners using
the event's ID as the key. Something like this:

public void Register(int eventID, GameListener listener)
{
List list = listeners.GetValue(eventID);

list.Add(listener);
}

However, whether you use the ID number or instanceof in the switch
statements in the listener's ProcessEvent method is up to you. I don't
know if one is more efficient than the other.
Dmitry A. Kazakov
2007-04-22 15:29:14 UTC
Permalink
Post by Aaron J. M.
Post by Dmitry A. Kazakov
Why are they events? I would say, that death, under attack, flight etc are
not events, they are states. If creatures are models of physical objects,
then the only event they need is a timer or scheduling event. The object
reads the sensors, changes its state and writes the actuators.
So what you're suggesting is that my "events" should become internal
to the creature, right? For example, if a creature starts attacking
something, it should keep track of what it's attacking and how much damage it did by
itself,
Sure. It will consume endurance, mana, whatever depending on what and how
long it does.
Post by Aaron J. M.
where this information would be visible to other objects that can observe the
creature.
Not necessarily. Others would sense the creature using their own sensors,
which might be less inaccurate than the self-feeling, or reverse. (What
irritates me in some RPGs is that when you do something, like stealing a
thing, all NPCs become instantly aware of your action. Why should they?)
Post by Aaron J. M.
However, what if I have events that are not generated by creatures? For example,
I may decide to have an earthquake happen for some reason, and I want some
creature AIs to be able to tell that an earthquake happened so that they can get
scared off. How would I model that?
The creatures will have an earth sensor.
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
Aaron J. M.
2007-04-23 02:04:45 UTC
Permalink
Post by H. S. Lahman
These are events but the thing I would push back on is why you need to
"keep track" of them. Normally events are just messages that announce
that somebody did something that somehow changes the state of the
application solution. That message needs to be routed to someone who
cares what happened and has an appropriate response to it. That response
changes the state of the application, a message is issued to announce
it, and someone who cares about it responds, and so on...
So once the event is consumed (received by someone who cares and has an
appropriate response) it essentially ceases to exist.
Does this apply when I have multiple objects interested in the same
event?
For example, if a creature attacks another creature, any other
creatures
who are friends with the target and are within range to see the attack
should get mad at the attacker (or afraid, if the damage done was high
enough). If the creature controlled by the player could have seen the
attack, an appropriate animation should be displayed on the screen and
an
appropriate sound be played.
Post by H. S. Lahman
Games usually have some degree of concurrency (e.g., the Physics
subsystem is figuring out current movement while the Rendering subsystem
is throwing stuff on the display for the last movement). Concurrency is
usually handled with asynchronous messaging. Asynchronous messaging is
best managed with object state machines that capture sequencing rules as
transitions. Object state machines come full circle to events and event
queues.
When you say concurrency, you don't mean threads do you? Or would it
matter?
(For the record, this is a single-threaded application that will be
done in
Python.) I also don't quite grasp how state-machines become involved
here.
Post by H. S. Lahman
So your event might persist temporarily on the event queue between the
time it is generated and the time it is consumed. But there it is simply
a message {event ID, <data packet>}. Whoever generates the event knows
how to encode any data packet and whoever receives the event will know
how to decode the data packet. All such dispatching is done based on the
event ID through a state transition table.
Is the state transition table for deciding how to encode/decode a data
packet
or for choosing where to send the event?
Post by H. S. Lahman
The event doesn't do anything; it is just a message. The receiver of the
event does something in response to the condition raised in the event
sender. The event ID tells the receiver which behavior to map to the
event. That behavior will decode the data packet for parametric values.
When I talked about event objects in my first post, I was speaking of
them as
immutable information holders, with the difference between them being
the
information held.

An AI would know how to respond to a creature attacking something, and
the
graphics would know how to animate an attack. The first crucial thing
is
letting the parts know that such a thing happened in the first place.
The
second thing is letting the parts know the specifics they need to know
to do
their response. An AI needs to know what creatures were involved in
an attack
(especially if the creature it controls or one of its friends were the
target)
and how much damage was done (to tell if it is strong enough to deal
with the
attacker). The graphics also needs these details so it knows what
animation
to render and where.

Generating these events would be somewhat easy. A creature would
generate an
event for most of its notable atomic actions, such as attacking or
dying, and
know what information to put into these events. (Remember that I'm
separating
the creatures from the AI/UI. Generally, a Creature can be told to do
things,
and an AI or UI makes decisions/responds to input to figure out what a
Creature should do.)
Post by H. S. Lahman
Since that is a matter of private method implementation, one /could/
class Event
{
EventID id;
Buffer* dataPacket; // just contiguous bytes on the heap
}
and let individual senders/receivers encode/decode the buffer. However,
to preserve the developer's sanity in an elaboration environment, you
probably need some more explicit definition of data packet in terms of
types. One way to do that is to subclass Buffer. Another is to make
Buffer an XML string. Yet another is to provide a specification object
whose attributes provide parametric values that drive the encode/decode.
(The category on Invariants and Parametric Polymorphism on my blog
provide some examples of this.)
Whoa. Is Buffer available in Python? ;)

I thought your posts about Invariants on your blog were interesting,
but I
could only follow the Depreciation example. I think there was also an
example
somewhere else in your blog about how to model a tree of Node objects
and
avoiding the checks for null in the case of leaf Nodes. I think the
gist of
it was creating a subclass of Node called Leaf, and also give Node an
attribute value that clients could check instead of checking for
null. Is
that right?
Post by H. S. Lahman
The real issue here is that when a Creature grabs another that changes
the state of the application and other objects need to know that
happened. So the grabbing Creature would send messages to whoever that
is -- the other Creature, the AI, the Physics subsystem, the Rendering
subsystem, etc..
Exactly. You're taking decoupling into account though, right? I
don't
want direct references to the Physics or Rendering systems in Creature
just to
let them know whenever a Creature sits on a whoopie cushion.
Post by H. S. Lahman
Note that throwing the Creature is another change in the application
state that is different than grabbing it. That will yield another set of
messages that may or may not go to the same objects (i.e., different
objects may care about throwing than about grabbing).
Yes. Another example would be if I had a door opening. AIs wouldn't
*really*
need to be told directly since they would tell the door opened by
looking at
the map, but a creaking sound should be played by the sound system.
Post by H. S. Lahman
Not necessarily. Others would sense the creature using their own sensors,
which might be less inaccurate than the self-feeling, or reverse. (What
irritates me in some RPGs is that when you do something, like stealing a
thing, all NPCs become instantly aware of your action. Why should they?)
Could you explain how these sensors would work/go? With the event
model I've
been wrestling with above an AI would poll the event queue and pick an
action
for its Creature depending on any events it had interest in. The
Creature's
actions would generate more actions to go on the queue. Of course
there has
to be a way of getting rid of old events that everyone has already
seen
before.

As for the thievery example, I'd add a line-of-sight check to
eliminate any
omniscience. ;)



Thinking about Sanford's and Lahman's messages made me think about the
possibility of a concrete Event object that, along with an ID, has a
map of
key/value pairs. The ID number would specify what keys are available.
Event generators would know the ID number and the keys to put into an
event
and what data to associate with each key (this could be made easier
with a
factory of some kind). Event listeners can tell what an event's type
is and
get its information without any downcasting. How does that sound?


P.S. about Lahman's statement about the peer-to-peer collaboration of
object
in an OO system:
Now, I am aware that a lot of your work involved translation and
executable
UML, so I gather you didn't have to worry so much about the stack-
based
execution of so-called 3GL languages. I unfortunately don't have
access to
these technologies, and probably won't ever until there's a "gcc-mda"
command
on most Unix machines. So I'm still going to have to worry about the
order in
which things execute for a while now. ;)
Dmitry A. Kazakov
2007-04-23 08:20:25 UTC
Permalink
Post by Dmitry A. Kazakov
Not necessarily. Others would sense the creature using their own sensors,
which might be less inaccurate than the self-feeling, or reverse. (What
irritates me in some RPGs is that when you do something, like stealing a
thing, all NPCs become instantly aware of your action. Why should they?)
Could you explain how these sensors would work/go? With the event model I've
been wrestling with above an AI would poll the event queue and pick an action
for its Creature depending on any events it had interest in.
The difference is that you attach the queue to the object. But in "reality"
objects don't communicate directly except than when they are talking to
each other, which is also an abstraction of some complex short-range
process. The objects communicate with the local environment. You already
have it in the form of the map where the objects are located. It is a form
of communication. I guess the objects will have get-my-X,Y,Z-co-ordinates
and set-my-X,Y,Z-co-ordinates. The first is "sensor", the second is
"actuator." You could implement the most of physics this way, by adding
forces applied at the position X,Y,Z, terrain constraints etc.
The Creature's
actions would generate more actions to go on the queue.
This has a problem of determining the queue into which the action need to
be placed. An object has to know too much about what it is going to do
that. This is why the event abstraction is not that good for more or less
elaborated physics. It would make the design of objects way too complex.
When an object is thrown, it is not a collaboration between it and the
thrower. It just flies, it need not to know what has thrown it. You could
still use it for higher-order things like speech, which are hard-wired
rather than simulated. I mean it is a trade-off, more complex your
simulation becomes, less the model of objects "talking" to each other pays
off.
Of course there has
to be a way of getting rid of old events that everyone has already seen before.
If you want broadcast events, then in your model you just put the event
into each and every queue. You don't need to get rid of it, it is the
problem of somebody who reads the queue out. You will need handles (smart
pointers) to the event parameters which aren't marshaled, to prevent them
from disappearing before the event gets dequeued. And you will have a hell
with race condition problems, which are typical for any event-based system.

An alternative design for broadcast / multicast events is the blackboard.
You put the event there and everybody scans the blackboard for the news.
The blackboard is a large ring buffer which gets overridden. Events in the
buffer have the sequence number so that the consumers can detect the last
event when they scan the buffer. This has less problems with non-marshaled
parameters, which live until the ring buffer overrun. But it consumes more
memory and race conditions are all yours anyway.
As for the thievery example, I'd add a line-of-sight check to eliminate any
omniscience. ;)
I.e. you will need to filter the events.
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
Aaron J. M.
2007-04-25 21:07:40 UTC
Permalink
Just in case you got this more than once, sorry. Google Groups seem
to be
giving me a hard time.
Post by Dmitry A. Kazakov
This is why the event abstraction is not that good for more or less
elaborated physics. It would make the design of objects way too complex.
When an object is thrown, it is not a collaboration between it and the
thrower. It just flies, it need not to know what has thrown it.
Yes, the actual motion of a thrown creature needs no collaboration
with the
thrower or anything like that. But what about the AI controlling the
creature?
To be an effective AI, it needs to
1) Tell that the creature was thrown in the first place.
2) Tell who the thrower was, so that it knows who to retaliate
against.

Basically, I don't need events for the creatures or other game objects
themselves, but for the AIs controlling them. I need the AIs be able
to
know when things in the world happen. This has more to do with
capturing
the semantic information of what caused something to happen and who
were
involved. Two Creatures attacking each other is fairly
straightforward.
Allowing the AI controlling Creature X be able to tell that Creature Y
has
attacked its friend Creature Z which should make the AI tell X to
attack Y
is what I'm having trouble with.

This discussion is turning a little too abstract for my taste, so
here's
some pseudo-code. This will ignore the player and graphics system.

Maps are a set of locations (Loc objects), where one Creature may be
at any
location. It knows what Creatures are at what locations on it, and
Creatures may be placed at or removed from certain locations.

class Map
{
list<Creature> getAllCreatures()
Creature getCreature(Loc loc)
void removeCreature(Loc loc)
void addCreature(Creature cr, Loc loc)
}

Creatures are things that exist on a Map, have statistics (health,
attack,
etc.) and interact with each other. It also knows where it is in the
world
for the benefit of its AI (though I haven't finalized how this
relationship
will work yet, so the movement code here may not be the best design).

class Creature
{
private int health, attack

public Loc getLoc()
public Map getMap()

public void takeDamage(int atck)
{
this.health -= atck
if this.health <= 0:
getMap().removeCreature(getLoc())
}

public void attack(Creature target)
{
target.takeDamage(attack)
}

public void move(Map m, Loc loc)
}

AIs encapsulate all the decision-making behavior for a Creature. They
look
at the map their "body" is on and choose an action for it.

class AI
{
private Creature body

public void takeAction()
{
if toManyEnemies( body.getMap() ):

body.move( aPlaceThatIsFarAwayFromTheNasties( body.getMap() ) )
else if somethingHereToHunt( body.getMap() ):
body.attack( aGoodTarget( body.getMap() ) )
}

private bool toManyEnemies(Map map)
private Loc aPlaceThatIsFarAwayFromTheNasties(Map map)
private bool somethingHereToHunt(Map map)
private Creature aGoodTarget(Map map)
}

Game is a container for all the Maps (which have Creatures), and AIs,
and
controls in what order the AIs act (the main loop).

class Game
{
private list<Map> allMaps
private list<AI> allAIs

public void loop()
{
while gameNotOver():
for a in allAIs:
a.takeAction()
}

private bool gameNotOver()
}

The missing link has to do with AIs knowing things that have happened
to
their own Creatures or to any other Creatures they'd be interested in.
This information does not have to do with Creature actions like
attacks
themselves, but with AIs being able to infer what has happened and who
were
involved so that they know who to blame.

Sorry if I sound a bit dense, but I'm really trying to understand what
you
people are saying, and I would really like to solve this problem.
Thanks
for bearing with me.
H. S. Lahman
2007-04-22 15:28:57 UTC
Permalink
Responding to M....

Basically I agree with Sanford AND Kazakov...
Post by Aaron J. M.
I need a way of representing and tracking different events that occur
in a game I'm working on. I have a world composed of one or more
Maps. Each Map location (coordinate) may or may not have a Creature.
A Creature is controlled by an AI, which may also be the player's UI,
though the Creatures themselves are not necessarily aware of their
AIs. Creatures may move to different coordinates or attack other
Creatures.
Right now the two events I know I'll want to keep track of is when a
Creature attacks another Creature and when a Creature dies, and I see
myself coming up with new events as my game grows more intricate.
Different parts of my game would be interested in different kinds of
events. The graphical display would be interested in all events so
that it may display an appropriate animation. More importantly, the
different AIs would need to observe recent events to modify their
behaviour. If a Creature is attacked, the AI controlling it has a
vested interest in knowing about it.
These are events but the thing I would push back on is why you need to
"keep track" of them. Normally events are just messages that announce
that somebody did something that somehow changes the state of the
application solution. That message needs to be routed to someone who
cares what happened and has an appropriate response to it. That response
changes the state of the application, a message is issued to announce
it, and someone who cares about it responds, and so on...

So once the event is consumed (received by someone who cares and has an
appropriate response) it essentially ceases to exist.

Games usually have some degree of concurrency (e.g., the Physics
subsystem is figuring out current movement while the Rendering subsystem
is throwing stuff on the display for the last movement). Concurrency is
usually handled with asynchronous messaging. Asynchronous messaging is
best managed with object state machines that capture sequencing rules as
transitions. Object state machines come full circle to events and event
queues.

So your event might persist temporarily on the event queue between the
time it is generated and the time it is consumed. But there it is simply
a message {event ID, <data packet>}. Whoever generates the event knows
how to encode any data packet and whoever receives the event will know
how to decode the data packet. All such dispatching is done based on the
event ID through a state transition table.
Post by Aaron J. M.
I'm having trouble modeling this because the different kinds of
events would keep potentially arbitrary information, making it hard
to generalize. An attack event would know the attacking Creature, the
target Creature, and the amount of damage done. A death event would
just know the Creature that died. I might also implement Creatures
grabbing and throwing other Creatures, so a throw event would need to
know the throwing Creature, the thrown Creature, and where the thrown
Creature landed. For this reason I can't really make them subclasses
of a single Event class, since they hold such different information.
The event doesn't do anything; it is just a message. The receiver of the
event does something in response to the condition raised in the event
sender. The event ID tells the receiver which behavior to map to the
event. That behavior will decode the data packet for parametric values.

Since that is a matter of private method implementation, one /could/
define an event as simply:

class Event
{
public:
EventID id;
Buffer* dataPacket; // just contiguous bytes on the heap
}

and let individual senders/receivers encode/decode the buffer. However,
to preserve the developer's sanity in an elaboration environment, you
probably need some more explicit definition of data packet in terms of
types. One way to do that is to subclass Buffer. Another is to make
Buffer an XML string. Yet another is to provide a specification object
whose attributes provide parametric values that drive the encode/decode.
(The category on Invariants and Parametric Polymorphism on my blog
provide some examples of this.)

The real issue here is that when a Creature grabs another that changes
the state of the application and other objects need to know that
happened. So the grabbing Creature would send messages to whoever that
is -- the other Creature, the AI, the Physics subsystem, the Rendering
subsystem, etc..

Note that throwing the Creature is another change in the application
state that is different than grabbing it. That will yield another set of
messages that may or may not go to the same objects (i.e., different
objects may care about throwing than about grabbing).

The bottom line here is that an OO application consists of a passle of
logically indivisible behaviors owned by a gaggle of objects or
subsystems. The flow of control is defined by connecting the dots of
atomic behaviors with messages on a peer-to-peer basis (i.e., a direct
collaboration between the object that triggered the new application
state and the object that has a response to that new condition).


*************
There is nothing wrong with me that could
not be cured by a capful of Drano.

H. S. Lahman
***@pathfindermda.com
Pathfinder Solutions
http://www.pathfindermda.com
blog: http://pathfinderpeople.blogs.com/hslahman
"Model-Based Translation: The Next Step in Agile Development". Email
***@pathfindermda.com for your copy.
Pathfinder is hiring:
http://www.pathfindermda.com/about_us/careers_pos3.php.
(888)OOA-PATH
Alvin Ryder
2007-04-24 03:12:06 UTC
Permalink
Post by Aaron J. M.
I need a way of representing and tracking different events that occur
in a game
I'm working on. I have a world composed of one or more Maps. Each
Map location
(coordinate) may or may not have a Creature. A Creature is controlled
by an AI,
which may also be the player's UI, though the Creatures themselves are
not
necessarily aware of their AIs. Creatures may move to different
coordinates or
attack other Creatures.
Right now the two events I know I'll want to keep track of is when a
Creature
attacks another Creature and when a Creature dies, and I see myself
coming up
with new events as my game grows more intricate. Different parts of
my game
would be interested in different kinds of events. The graphical
display would
be interested in all events so that it may display an appropriate
animation.
More importantly, the different AIs would need to observe recent
events to
modify their behaviour. If a Creature is attacked, the AI controlling
it has a
vested interest in knowing about it.
I'm having trouble modeling this because the different kinds of events
would
keep potentially arbitrary information, making it hard to generalize.
An attack
event would know the attacking Creature, the target Creature, and the
amount of
damage done. A death event would just know the Creature that died. I
might
also implement Creatures grabbing and throwing other Creatures, so a
throw event
would need to know the throwing Creature, the thrown Creature, and
where the
thrown Creature landed. For this reason I can't really make them
subclasses of
a single Event class, since they hold such different information.
So how would I model these events and let my system handle and respond
to them?
Thanks
Hi,

Hmm, looks like google gobbled up my first reply, drats.

I reckon all you need is simple and direct object calls, where an
object is said to "send a message to another object" but these are
just *ordinary method calls*, as in
[code]
bigMonster.takesHit (zapper.inflictPain());
[/code]

The backbone of all game engines I've worked with is a "game loop" and
not an Event Message architecture. Inside the game loop you
essentially just
1. *poll* for inputs
2. read incoming network traffic,
3. adjust the model accordingly (calcs, ai, physics...), then
4. output to network and
5. finally feed the renderer...

An Event Message / Queue architecture is an extra level of
indirection, which might not sound like much but I'll bet it's ten
times more work and it'll slow the engine down too much. To thump out
60 frames per second you get all of 16.7 milliseconds to do
everything, no time to waste. If the queue builds up with too many
messages you'll have an ugly controller lag situation. You loose
immediacy.

Actually Microsoft Windows is based on an Event Message / Queue
architecture, you move the mouse, MouseEvents are generated and put in
a Queue(s), interested apps can read the Events and react accordingly.
Mind you I reckon you can use something similar for games but I can't
imagine what you'll gain?

Even if your game is more of a simulation rather than a real-time one,
direct object calls would still be the way to go, think Simula.

Not to confuse matters; most game engines actually employ a scripting
language which sits on top of everything, these may have some event
related stuff in them but they are more of an artist and game designer
friendly wrapper rather than an Event Message backbone.

You're using Python, is that with some OpenGL or Direct/X wrapper or
something? Maybe Python can act as your scripting language around an
engine with speed in all the right places (usually C/C++/assembler
based). I know one really good engine that uses a dialect of LISP as
its outer layer ... so why not Python, it might be really cool.

HTH,
Cheers.
Aaron J. M.
2007-04-25 02:44:45 UTC
Permalink
Post by Dmitry A. Kazakov
This is why the event abstraction is not that good for more or less
elaborated physics. It would make the design of objects way too complex.
When an object is thrown, it is not a collaboration between it and the
thrower. It just flies, it need not to know what has thrown it.
Yes, the actual motion of a thrown creature needs no collaboration
with the
thrower or anything like that. But what about the AI controlling the
creature?
To be an effective AI, it needs to
1) Tell that the creature was thrown in the first place.
2) Tell who the thrower was, so that it knows who to retaliate
against.

Basically, I don't need events for the creatures or other game objects
themselves,
but for the AIs controlling them. I need the AIs be able to know when
things in
the world happen. This has little to do with the actual physics of
how the events
take place and more to do with capturing the semantic information of
what caused
something to happen and who were involved. Two Creatures attacking
each other is
fairly straightforward. Allowing the AI controlling Creature X be
able to tell that
Creature Y has attacked its friend Creature Z which should make the AI
tell X to
attack Y is what I'm having trouble with.

This discussion is turning a little too abstract for my taste, so
here's some
pseudo-code. This will ignore the player and graphics system.

Maps are a set of locations (Loc objects), where one Creature may be
at any
location. It knows what Creatures are at what locations on it, and
Creatures may be
placed at or removed from certain locations.

class Map:
list<Creature> getAllCreatures()
Creature getCreature(Loc loc)
void removeCreature(Loc loc)
void addCreature(Creature cr, Loc loc)

Creatures are things that exist on a Map, have statistics (health,
attack, etc.)
and interact with each other. It also knows where it is in the world
for the
benefit of its AI (though I haven't finalized how this relationship
will work
yet, so the movement code here may not be the best design).

class Creature:
private int health, attack

public Loc getLoc()
public Map getMap()

public void takeDamage(int atck):
this.health -= atck
if this.health <= 0:
getMap().removeCreature(getLoc())

public void attack(Creature target):
target.takeDamage(attack)

public void move(Map m, Loc loc):
getMap().removeCreature(getLoc())
// Set the Creature's Map and Loc to m and loc somehow.
m.addCreature(this, loc)

AIs encapsulate all the decision-making behavior for a Creature. They
look at the
map their "body" is on and choose an action for it.

class AI:
private Creature body

public void takeAction():
if toManyEnemies( body.getMap() ):

body.move( aPlaceThatIsFarAwayFromTheNasties( body.getMap() ) )
else if somethingHereToHunt( body.getMap() ):
body.attack( aGoodTarget( body.getMap() ) )

private bool toManyEnemies(Map map)
private Loc aPlaceThatIsFarAwayFromTheNasties(Map map)
private bool somethingHereToHunt(Map map)
private Creature aGoodTarget(Map map)

Game is a container for all the Maps (which have Creatures), and AIs,
and controls
in what order the AIs act (the main loop).

class Game:
private list<Map> allMaps
private list<AI> allAIs

public void loop():
while gameNotOver():
for a in allAIs:
a.takeAction()

private bool gameNotOver()

The missing link has to do with AIs knowing things that have happened
to their own
Creatures or to any other Creatures they'd be interested in. This
information does
not have to do with Creature actions like attacks themselves, but with
AIs being
able to infer what has happened and who were involved so that they
know who to
blame.

Sorry if I sound a bit dense, but I'm really trying to understand what
you people
are saying, and I would really like to solve this problem. Thanks for
bearing
with me.
Post by Dmitry A. Kazakov
You're using Python, is that with some OpenGL or Direct/X wrapper or
something? Maybe Python can act as your scripting language around an
engine with speed in all the right places (usually C/C++/assembler
based). I know one really good engine that uses a dialect of LISP as
its outer layer ... so why not Python, it might be really cool.
I'm actually writing the whole thing in Python, using Pygame (a
wrapper for SDL)
for graphics and input.
Dmitry A. Kazakov
2007-04-25 08:24:06 UTC
Permalink
Post by Dmitry A. Kazakov
This is why the event abstraction is not that good for more or less
elaborated physics. It would make the design of objects way too complex.
When an object is thrown, it is not a collaboration between it and the
thrower. It just flies, it need not to know what has thrown it.
Yes, the actual motion of a thrown creature needs no collaboration with the
thrower or anything like that. But what about the AI controlling the creature?
To be an effective AI, it needs to
1) Tell that the creature was thrown in the first place.
2) Tell who the thrower was, so that it knows who to retaliate against.
I don't think so. The AI should know if there is an aggressive action
against the creature it controls. Whether throwing is considered aggressive
depends on many factors. I suppose, a high mage should be allowed to throw
allied gnomes at his opponents without risking of being retaliated. But he
will be punished if he tried to do that with a dragon.

[...]
The missing link has to do with AIs knowing things that have happened to their own
Creatures or to any other Creatures they'd be interested in. This information does
not have to do with Creature actions like attacks themselves, but with AIs being
able to infer what has happened and who were involved so that they know who to
blame.
The awareness of actions should IMO come from the map. So the AI should
read from the map: there is an accelerating force being applied to my
creature. So the physics starts to work first. If AI considers this as a
damage (some creatures might actually enjoy being propelled), then it
should scan the neighborhood for the cause of that unfortunate event. In
other words it should create a hypothesis of what has happened. That can be
simplified as building a list of nearby creatures with some factors of
confidence in their aggressive/friendly behavior. Also the creature
possesses knowledge (memory) about some of these creatures (confidence
factors of My_Ally, Stronger_Than_Me, Tastes_Good etc). This knowledge base
should definitely change with the creature's experience. Further the
creature has self-awareness, i.e. the knowledge of short-term tactic
decisions ("I'll kill that") and long-term strategic decisions ("We are on
our way to X") made by the AI before. This all is the input data the AI
uses to make its move.

(Events can be used as shortcuts in this admittedly complex process. But
they are like black magic. I would use them sparely for only very
high-level things.)
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
Aaron J. M.
2007-04-27 22:36:01 UTC
Permalink
Post by Dmitry A. Kazakov
The awareness of actions should IMO come from the map. So the AI should
read from the map: there is an accelerating force being applied to my
creature. So the physics starts to work first. If AI considers this as a
damage (some creatures might actually enjoy being propelled), then it
should scan the neighborhood for the cause of that unfortunate event. In
other words it should create a hypothesis of what has happened.
This sounds you mean the AI should only guess about the source of an
attack
instead of know about it, which doesn't seem all that reliable.

I was hesitant to put knowledge about actions into the map because I
wanted it
to just hold creatures with locations and some terrain data. It would
make
sense to put physics information into the map though.

However, I'm not really simulating physics since my game turn-based.
All
actions (attacking, moving, throwing, etc.) takes exactly one turn for
one
creature to perform. Attacking is mainly one creature reducing the
number of hit
points in another creature, and throwing is mainly a matter of one
creature
getting "teleported" to a nearby location and some of its hit points
taken away.
Dmitry A. Kazakov
2007-04-28 08:03:22 UTC
Permalink
Post by Aaron J. M.
Post by Dmitry A. Kazakov
The awareness of actions should IMO come from the map. So the AI should
read from the map: there is an accelerating force being applied to my
creature. So the physics starts to work first. If AI considers this as a
damage (some creatures might actually enjoy being propelled), then it
should scan the neighborhood for the cause of that unfortunate event. In
other words it should create a hypothesis of what has happened.
This sounds you mean the AI should only guess about the source of an
attack instead of know about it, which doesn't seem all that reliable.
Sure, that the idea. You could blind a grue with the light of a torch
before attacking it. Otherwise any potential cause of blinding should send
an event to every potential subject of blinding. With the number of causes
and subjects it will geometrically explode. My point is that the
event-based design will cease to scale as the complexity will grow. You
have to find a balance for your concrete game. At some point a closer
simulation of "reality" starts to pay off.
Post by Aaron J. M.
However, I'm not really simulating physics since my game turn-based. All
actions (attacking, moving, throwing, etc.) takes exactly one turn for one
creature to perform. Attacking is mainly one creature reducing the number of hit
points in another creature, and throwing is mainly a matter of one creature
getting "teleported" to a nearby location and some of its hit points
taken away.
I never could understand why people develop and play turn-based
simulations, but that's aside. Actually to be turn-based is just a way of
scheduling which ideally should be irrelevant to how the things work. I.e.
you should be able to convert it to real-time and back (to the
simulated-time). I bet H.S. Lahman would say you same.
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
Aaron J. M.
2007-04-25 20:58:26 UTC
Permalink
Post by Dmitry A. Kazakov
This is why the event abstraction is not that good for more or less
elaborated physics. It would make the design of objects way too complex.
When an object is thrown, it is not a collaboration between it and the
thrower. It just flies, it need not to know what has thrown it.
Yes, the actual motion of a thrown creature needs no collaboration
with the
thrower or anything like that. But what about the AI controlling the
creature?
To be an effective AI, it needs to
1) Tell that the creature was thrown in the first place.
2) Tell who the thrower was, so that it knows who to retaliate
against.

Basically, I don't need events for the creatures or other game objects
themselves, but for the AIs controlling them. I need the AIs be able
to
know when things in the world happen. This has more to do with
capturing
the semantic information of what caused something to happen and who
were
involved. Two Creatures attacking each other is fairly
straightforward.
Allowing the AI controlling Creature X be able to tell that Creature Y
has
attacked its friend Creature Z which should make the AI tell X to
attack Y
is what I'm having trouble with.

This discussion is turning a little too abstract for my taste, so
here's
some pseudo-code. This will ignore the player and graphics system.

Maps are a set of locations (Loc objects), where one Creature may be
at any
location. It knows what Creatures are at what locations on it, and
Creatures may be placed at or removed from certain locations.

class Map
{
list<Creature> getAllCreatures()
Creature getCreature(Loc loc)
void removeCreature(Loc loc)
void addCreature(Creature cr, Loc loc)
}

Creatures are things that exist on a Map, have statistics (health,
attack,
etc.) and interact with each other. It also knows where it is in the
world
for the benefit of its AI (though I haven't finalized how this
relationship
will work yet, so the movement code here may not be the best design).

class Creature
{
private int health, attack

public Loc getLoc()
public Map getMap()

public void takeDamage(int atck)
{
this.health -= atck
if this.health <= 0:
getMap().removeCreature(getLoc())
}

public void attack(Creature target)
{
target.takeDamage(attack)
}

public void move(Map m, Loc loc)
}

AIs encapsulate all the decision-making behavior for a Creature. They
look
at the map their "body" is on and choose an action for it.

class AI
{
private Creature body

public void takeAction()
{
if toManyEnemies( body.getMap() ):

body.move( aPlaceThatIsFarAwayFromTheNasties( body.getMap() ) )
else if somethingHereToHunt( body.getMap() ):
body.attack( aGoodTarget( body.getMap() ) )
}

private bool toManyEnemies(Map map)
private Loc aPlaceThatIsFarAwayFromTheNasties(Map map)
private bool somethingHereToHunt(Map map)
private Creature aGoodTarget(Map map)
}

Game is a container for all the Maps (which have Creatures), and AIs,
and
controls in what order the AIs act (the main loop).

class Game
{
private list<Map> allMaps
private list<AI> allAIs

public void loop()
{
while gameNotOver():
for a in allAIs:
a.takeAction()
}

private bool gameNotOver()
}

The missing link has to do with AIs knowing things that have happened
to
their own Creatures or to any other Creatures they'd be interested in.
This information does not have to do with Creature actions like
attacks
themselves, but with AIs being able to infer what has happened and who
were
involved so that they know who to blame.

Sorry if I sound a bit dense, but I'm really trying to understand what
you
people are saying, and I would really like to solve this problem.
Thanks
for bearing with me.
topmind
2007-04-25 00:19:59 UTC
Permalink
Post by Aaron J. M.
I need a way of representing and tracking different events that occur
in a game
I'm working on. I have a world composed of one or more Maps. Each
Map location
(coordinate) may or may not have a Creature. A Creature is controlled
by an AI,
which may also be the player's UI, though the Creatures themselves are
not
necessarily aware of their AIs. Creatures may move to different
coordinates or
attack other Creatures.
Right now the two events I know I'll want to keep track of is when a
Creature
attacks another Creature and when a Creature dies, and I see myself
coming up
with new events as my game grows more intricate. Different parts of
my game
would be interested in different kinds of events. The graphical
display would
be interested in all events so that it may display an appropriate
animation.
More importantly, the different AIs would need to observe recent
events to
modify their behaviour. If a Creature is attacked, the AI controlling
it has a
vested interest in knowing about it.
I'm having trouble modeling this because the different kinds of events
would
keep potentially arbitrary information, making it hard to generalize.
An attack
event would know the attacking Creature, the target Creature, and the
amount of
damage done. A death event would just know the Creature that died. I
might
also implement Creatures grabbing and throwing other Creatures, so a
throw event
would need to know the throwing Creature, the thrown Creature, and
where the
thrown Creature landed. For this reason I can't really make them
subclasses of
a single Event class, since they hold such different information.
So how would I model these events and let my system handle and respond
to them?
Thanks
With all that to track, have you considered using a database? I
realize that performance with a database might not be as predictable
as a RAM-only solution, but depending on the game, it might not matter
much. Maybe bats or lightning can fill in the times when the DB is
slow doing garbage collection etc. I would try to make the DB
processing and game engine be somewhat indepedent processes so that
something can still be visually happening while the DB is "thinking".
SqLite is a light-duty DB open-source C-based engine that may be
embeddable in a game.

Do you really want to build a complex interrelating structure(s) with
objects instead of a DB? You would end up reinventing a lot of DB-like
idioms from scratch if you did it with classes.

Just an idea.

-T-
Alvin Ryder
2007-04-27 00:32:30 UTC
Permalink
Post by topmind
Post by Aaron J. M.
I need a way of representing and tracking different events that occur
in a game
I'm working on. I have a world composed of one or more Maps. Each
Map location
(coordinate) may or may not have a Creature. A Creature is controlled
by an AI,
which may also be the player's UI, though the Creatures themselves are
not
necessarily aware of their AIs. Creatures may move to different
coordinates or
attack other Creatures.
Right now the two events I know I'll want to keep track of is when a
Creature
attacks another Creature and when a Creature dies, and I see myself
coming up
with new events as my game grows more intricate. Different parts of
my game
would be interested in different kinds of events. The graphical
display would
be interested in all events so that it may display an appropriate
animation.
More importantly, the different AIs would need to observe recent
events to
modify their behaviour. If a Creature is attacked, the AI controlling
it has a
vested interest in knowing about it.
I'm having trouble modeling this because the different kinds of events
would
keep potentially arbitrary information, making it hard to generalize.
An attack
event would know the attacking Creature, the target Creature, and the
amount of
damage done. A death event would just know the Creature that died. I
might
also implement Creatures grabbing and throwing other Creatures, so a
throw event
would need to know the throwing Creature, the thrown Creature, and
where the
thrown Creature landed. For this reason I can't really make them
subclasses of
a single Event class, since they hold such different information.
So how would I model these events and let my system handle and respond
to them?
Thanks
With all that to track, have you considered using a database? I
realize that performance with a database might not be as predictable
as a RAM-only solution, but depending on the game, it might not matter
much. Maybe bats or lightning can fill in the times when the DB is
slow doing garbage collection etc. I would try to make the DB
processing and game engine be somewhat indepedent processes so that
something can still be visually happening while the DB is "thinking".
SqLite is a light-duty DB open-source C-based engine that may be
embeddable in a game.
While great for business applications, the biggest issue is the
relational model itself. It is not good at representing every domain.
For instance it struggles with temporal, spatial and multimedia
databases.

Try these queries with your RDBMS. Create a simple 2D world using
points and lines, let there be areas. One area could be a poison pond,
just a simple polygon like (1,1), (1,7), (7,7), (7,1) will do.

Now answer queries like:-

-1. "Have you fallen into the poison pond"?
is point (0,0) within the pond, is (3,3) within it?

-2. "There are two polygons do they intersect"? (collision)

-3. Create a road with corners; "how far is it from
point A to point B if you travel only via the road"?

Without a spatial join things are a tad awkward.

Graph theory has been mixed with set theory and predicate logic, there
are Geographical Information Systems (GIS) and there are Spatial
Database, these push in the right direction but practically probably
not enough at this point.

I wonder what those NavMan or in-car navigational gizmos use. Does
anyone know?
Post by topmind
Do you really want to build a complex interrelating structure(s) with
objects instead of a DB? You would end up reinventing a lot of DB-like
idioms from scratch if you did it with classes.
Just an idea.
-T-
This is a case of jumping out of the frying pan into ragging fires.
It's not a case of DB versus OOP, there is a third player, the
graphics pipeline.

Games are built on mathematics, API's like OpenGL or DirectX, GPU's,
art, animation, ai, characters, plots, physics, sound, music and a
heap of other disciplines. The least of your concerns are DB-like
idioms versus interrelated structures ;-)

For biz apps I let the relational model serve as the cornerstone and
OOP as the subservient technology. Many OO programmers invert this and
suppress relational technology but if you don't treat the database
nice, it'll bite back hard.

Similarly for game apps I let the graphics pipeline rule and OOP is
yet again subservient. Again inverting that relationship leads to
nothing but trouble.

Attempting to drive games by relational thinking is another example of
letting the wrong technology lead. There's different horses for
different courses.

Cheers.
topmind
2007-04-27 15:29:04 UTC
Permalink
Post by Alvin Ryder
Post by topmind
Post by Aaron J. M.
I need a way of representing and tracking different events that occur
in a game
I'm working on. I have a world composed of one or more Maps. Each
Map location
(coordinate) may or may not have a Creature. A Creature is controlled
by an AI,
which may also be the player's UI, though the Creatures themselves are
not
necessarily aware of their AIs. Creatures may move to different
coordinates or
attack other Creatures.
Right now the two events I know I'll want to keep track of is when a
Creature
attacks another Creature and when a Creature dies, and I see myself
coming up
with new events as my game grows more intricate. Different parts of
my game
would be interested in different kinds of events. The graphical
display would
be interested in all events so that it may display an appropriate
animation.
More importantly, the different AIs would need to observe recent
events to
modify their behaviour. If a Creature is attacked, the AI controlling
it has a
vested interest in knowing about it.
I'm having trouble modeling this because the different kinds of events
would
keep potentially arbitrary information, making it hard to generalize.
An attack
event would know the attacking Creature, the target Creature, and the
amount of
damage done. A death event would just know the Creature that died. I
might
also implement Creatures grabbing and throwing other Creatures, so a
throw event
would need to know the throwing Creature, the thrown Creature, and
where the
thrown Creature landed. For this reason I can't really make them
subclasses of
a single Event class, since they hold such different information.
So how would I model these events and let my system handle and respond
to them?
Thanks
With all that to track, have you considered using a database? I
realize that performance with a database might not be as predictable
as a RAM-only solution, but depending on the game, it might not matter
much. Maybe bats or lightning can fill in the times when the DB is
slow doing garbage collection etc. I would try to make the DB
processing and game engine be somewhat indepedent processes so that
something can still be visually happening while the DB is "thinking".
SqLite is a light-duty DB open-source C-based engine that may be
embeddable in a game.
While great for business applications, the biggest issue is the
relational model itself. It is not good at representing every domain.
For instance it struggles with temporal, spatial and multimedia
databases.
Are you talking relational *theory* itself, or current implementations
(Oracle, etc.)? I will agree that many DB vendors focus too heavily on
cloning Oracle, but I have yet to see a problem with the concept of
relational itself.
Post by Alvin Ryder
Try these queries with your RDBMS. Create a simple 2D world using
points and lines, let there be areas. One area could be a poison pond,
just a simple polygon like (1,1), (1,7), (7,7), (7,1) will do.
I was not considering geometrical construction, but more game
character management, which is what the original post was concerned
with. Although I think a relational engine could probably be tuned for
geometry and CAD, that is not what I had in mind here.

I suspect that gaming chips will eventually take over this role
anyhow, similar to having dedicated graphics chips but with an
expanded role. Gaming chips will start to process physics also, not
just rendering. It does not make a lot of sense to me to have one
model of a scene in the CPU and another similar model in the GPU for
rendering. From a performance and abstraction standpoint, it seems to
me that the "physics engine" should be with the rendering engine. The
CPU would then handle only general proximity and orientation, and
events like "player stepped on (made contact with) Panel 7", analogus
to "onClick" or "rollOver" events in GUI's. The calculations of actual
contact would usually be in the "physics chip".
Post by Alvin Ryder
Now answer queries like:-
-1. "Have you fallen into the poison pond"?
is point (0,0) within the pond, is (3,3) within it?
-2. "There are two polygons do they intersect"? (collision)
-3. Create a road with corners; "how far is it from
point A to point B if you travel only via the road"?
Without a spatial join things are a tad awkward.
Graph theory has been mixed with set theory and predicate logic, there
are Geographical Information Systems (GIS) and there are Spatial
Database, these push in the right direction but practically probably
not enough at this point.
I wonder what those NavMan or in-car navigational gizmos use. Does
anyone know?
Post by topmind
Do you really want to build a complex interrelating structure(s) with
objects instead of a DB? You would end up reinventing a lot of DB-like
idioms from scratch if you did it with classes.
Just an idea.
-T-
This is a case of jumping out of the frying pan into ragging fires.
It's not a case of DB versus OOP, there is a third player, the
graphics pipeline.
Games are built on mathematics, API's like OpenGL or DirectX, GPU's,
art, animation, ai, characters, plots, physics, sound, music and a
heap of other disciplines. The least of your concerns are DB-like
idioms versus interrelated structures ;-)
You still have to manage all the characters and their attributes such
as energy level, goals, etc.
Post by Alvin Ryder
For biz apps I let the relational model serve as the cornerstone and
OOP as the subservient technology. Many OO programmers invert this and
suppress relational technology but if you don't treat the database
nice, it'll bite back hard.
Similarly for game apps I let the graphics pipeline rule and OOP is
yet again subservient. Again inverting that relationship leads to
nothing but trouble.
Attempting to drive games by relational thinking is another example of
letting the wrong technology lead. There's different horses for
different courses.
I think you misunderstood the role I suggested for a DB.
Post by Alvin Ryder
Cheers.
-T-
Alvin Ryder
2007-04-28 00:47:09 UTC
Permalink
Post by topmind
Post by Alvin Ryder
Post by topmind
Post by Aaron J. M.
I need a way of representing and tracking different events that occur
in a game
I'm working on. I have a world composed of one or more Maps. Each
Map location
(coordinate) may or may not have a Creature. A Creature is controlled
by an AI,
which may also be the player's UI, though the Creatures themselves are
not
necessarily aware of their AIs. Creatures may move to different
coordinates or
attack other Creatures.
Right now the two events I know I'll want to keep track of is when a
Creature
attacks another Creature and when a Creature dies, and I see myself
coming up
with new events as my game grows more intricate. Different parts of
my game
would be interested in different kinds of events. The graphical
display would
be interested in all events so that it may display an appropriate
animation.
More importantly, the different AIs would need to observe recent
events to
modify their behaviour. If a Creature is attacked, the AI controlling
it has a
vested interest in knowing about it.
I'm having trouble modeling this because the different kinds of events
would
keep potentially arbitrary information, making it hard to generalize.
An attack
event would know the attacking Creature, the target Creature, and the
amount of
damage done. A death event would just know the Creature that died. I
might
also implement Creatures grabbing and throwing other Creatures, so a
throw event
would need to know the throwing Creature, the thrown Creature, and
where the
thrown Creature landed. For this reason I can't really make them
subclasses of
a single Event class, since they hold such different information.
So how would I model these events and let my system handle and respond
to them?
Thanks
With all that to track, have you considered using a database? I
realize that performance with a database might not be as predictable
as a RAM-only solution, but depending on the game, it might not matter
much. Maybe bats or lightning can fill in the times when the DB is
slow doing garbage collection etc. I would try to make the DB
processing and game engine be somewhat indepedent processes so that
something can still be visually happening while the DB is "thinking".
SqLite is a light-duty DB open-source C-based engine that may be
embeddable in a game.
While great for business applications, the biggest issue is the
relational model itself. It is not good at representing every domain.
For instance it struggles with temporal, spatial and multimedia
databases.
Are you talking relational *theory* itself, or current implementations
(Oracle, etc.)? I will agree that many DB vendors focus too heavily on
cloning Oracle, but I have yet to see a problem with the concept of
relational itself.
Set theory and predicate logic go a long way, especially for biz apps
but for geographical and spatial databases the theory needs to be
extended. You'd want to, at least, add a "spatial join" operator to
the relational algebra. While you're at it may as well throw in a heap
of graph theory.

Ironically Oracle do have a Spatial DB but I've never used it.
Post by topmind
Post by Alvin Ryder
Try these queries with your RDBMS. Create a simple 2D world using
points and lines, let there be areas. One area could be a poison pond,
just a simple polygon like (1,1), (1,7), (7,7), (7,1) will do.
I was not considering geometrical construction, but more game
character management, which is what the original post was concerned
with. Although I think a relational engine could probably be tuned for
geometry and CAD, that is not what I had in mind here.
Oops my mistake. When I considered the subject, years ago, I was
thinking about all or at least most aspects of game engine design.
Post by topmind
I suspect that gaming chips will eventually take over this role
anyhow, similar to having dedicated graphics chips but with an
expanded role. Gaming chips will start to process physics also, not
just rendering. It does not make a lot of sense to me to have one
model of a scene in the CPU and another similar model in the GPU for
rendering. From a performance and abstraction standpoint, it seems to
me that the "physics engine" should be with the rendering engine. The
CPU would then handle only general proximity and orientation, and
events like "player stepped on (made contact with) Panel 7", analogus
to "onClick" or "rollOver" events in GUI's. The calculations of actual
contact would usually be in the "physics chip".
Yep, those chips are all available now.
Post by topmind
Post by Alvin Ryder
Now answer queries like:-
-1. "Have you fallen into the poison pond"?
is point (0,0) within the pond, is (3,3) within it?
-2. "There are two polygons do they intersect"? (collision)
-3. Create a road with corners; "how far is it from
point A to point B if you travel only via the road"?
Without a spatial join things are a tad awkward.
Graph theory has been mixed with set theory and predicate logic, there
are Geographical Information Systems (GIS) and there are Spatial
Database, these push in the right direction but practically probably
not enough at this point.
I wonder what those NavMan or in-car navigational gizmos use. Does
anyone know?
Post by topmind
Do you really want to build a complex interrelating structure(s) with
objects instead of a DB? You would end up reinventing a lot of DB-like
idioms from scratch if you did it with classes.
Just an idea.
-T-
This is a case of jumping out of the frying pan into ragging fires.
It's not a case of DB versus OOP, there is a third player, the
graphics pipeline.
Games are built on mathematics, API's like OpenGL or DirectX, GPU's,
art, animation, ai, characters, plots, physics, sound, music and a
heap of other disciplines. The least of your concerns are DB-like
idioms versus interrelated structures ;-)
You still have to manage all the characters and their attributes such
as energy level, goals, etc.
Actually that is not a bad idea at all. An alternate and perfectly
legitimate way to describe a relational database is "a collection of
true valued predicates". Leaning more in its predicate logic roots you
could form the basis of an ai engine. Games are wanting more and more
ai so the idea has merit IMHO.
Post by topmind
Post by Alvin Ryder
For biz apps I let the relational model serve as the cornerstone and
OOP as the subservient technology. Many OO programmers invert this and
suppress relational technology but if you don't treat the database
nice, it'll bite back hard.
Similarly for game apps I let the graphics pipeline rule and OOP is
yet again subservient. Again inverting that relationship leads to
nothing but trouble.
Attempting to drive games by relational thinking is another example of
letting the wrong technology lead. There's different horses for
different courses.
I think you misunderstood the role I suggested for a DB.
Yes that's true my apologies. The keyword is "role", in that case I
agree the relational model can certainly play a part. Game engines are
like technology pigs, they gobble up just about any and every theory,
there's plenty of room for set theory and predicate logic in there.

Cheers.
Jordan Marr
2007-04-27 19:00:45 UTC
Permalink
Post by Alvin Ryder
Try these queries with your RDBMS. Create a simple 2D world using
points and lines, let there be areas. One area could be a poison pond,
just a simple polygon like (1,1), (1,7), (7,7), (7,1) will do.
Now answer queries like:-
-1. "Have you fallen into the poison pond"?
is point (0,0) within the pond, is (3,3) within it?
-2. "There are two polygons do they intersect"? (collision)
-3. Create a road with corners; "how far is it from
point A to point B if you travel only via the road"?
Without a spatial join things are a tad awkward.
This reminds me of a project I worked on recently involving a LPS
(local positioning system). The system consisted of sensors that were
placed inside a building, and tags worn by people or objects that
enabled them to be tracked by the software. We were creating guidance
software for blind people. Part of this involved mapping out the
building with things like walls, and other objects that people needed
to be routed around.

My plan was to use AutoCAD to either create maps visually, or import
existing blueprint layouts. Then the software could simply delegate
all spatial needs to AutoCAD. To go along with Topmind, AutoCAD is
simply an in-memory database of geometry (and it's good at what it
does).

But my boss wanted to manually create maps by walking around and
recording bounding box coordinates for each wall and object in a room,
and then save that array of coordinates to SQL Server... modeled as:
Object (1 to *) Coordinates.
And he didn't just save the bounding box coordinates, he saved every
single coordinate within that bounding box rounded to the nearest inch
within the coordinates table for each object. So that meant that the
process for adding a new object was this:

1) using LPS hardware, manually write down the array of XYZ bounding
box coordinates
2) enter coordinates into a script to create a list of all points
inside box
3) create an object entry in object table
4) create 1 zillion related coordinates entries for that object

Very "awkward", as you said.

He then created a webservice to bounce current position against the
coordinates table to return an object.

If we had used my method, adding a new object would have been as
simple as drawing it in AutoCAD. The software would have determined
spatial information by bouncing your current location against the CAD
layout and returning the names of nearby objects.

Jordan
Alvin Ryder
2007-04-28 08:58:15 UTC
Permalink
Post by Jordan Marr
Post by Alvin Ryder
Try these queries with your RDBMS. Create a simple 2D world using
points and lines, let there be areas. One area could be a poison pond,
just a simple polygon like (1,1), (1,7), (7,7), (7,1) will do.
Now answer queries like:-
-1. "Have you fallen into the poison pond"?
is point (0,0) within the pond, is (3,3) within it?
-2. "There are two polygons do they intersect"? (collision)
-3. Create a road with corners; "how far is it from
point A to point B if you travel only via the road"?
Without a spatial join things are a tad awkward.
This reminds me of a project I worked on recently involving a LPS
(local positioning system). The system consisted of sensors that were
placed inside a building, and tags worn by people or objects that
enabled them to be tracked by the software. We were creating guidance
software for blind people. Part of this involved mapping out the
building with things like walls, and other objects that people needed
to be routed around.
My plan was to use AutoCAD to either create maps visually, or import
existing blueprint layouts. Then the software could simply delegate
all spatial needs to AutoCAD. To go along with Topmind, AutoCAD is
simply an in-memory database of geometry (and it's good at what it
does).
But my boss wanted to manually create maps by walking around and
recording bounding box coordinates for each wall and object in a room,
Object (1 to *) Coordinates.
And he didn't just save the bounding box coordinates, he saved every
single coordinate within that bounding box rounded to the nearest inch
within the coordinates table for each object. So that meant that the
1) using LPS hardware, manually write down the array of XYZ bounding
box coordinates
2) enter coordinates into a script to create a list of all points
inside box
3) create an object entry in object table
4) create 1 zillion related coordinates entries for that object
Very "awkward", as you said.
He then created a webservice to bounce current position against the
coordinates table to return an object.
If we had used my method, adding a new object would have been as
simple as drawing it in AutoCAD. The software would have determined
spatial information by bouncing your current location against the CAD
layout and returning the names of nearby objects.
Jordan
That sounds like a very interesting and cool project.

Yes I agree, using a tool like Auto CAD then exporting is exactly how
games are built. The pretties are done with cousins of Auto CAD like
3d Studio Max or MAYA, there is usually an exporter involved then you
import into your game engine and render. The first version of Doom had
an in built map editor but these days most engines rely on external
specialist pretty picture painting software. You let them do their
bit, you let the engine do its bit.

Otherwise it sounds like your project is all working now so "all is
well that ends well", even if it isn't so schmick ;-)

Cheers.

topmind
2007-04-25 14:44:56 UTC
Permalink
Post by Aaron J. M.
I need a way of representing and tracking different events that occur
in a game
I'm working on. I have a world composed of one or more Maps. Each
Map location
(coordinate) may or may not have a Creature. A Creature is controlled
by an AI,
which may also be the player's UI, though the Creatures themselves are
not
necessarily aware of their AIs. Creatures may move to different
coordinates or
attack other Creatures.
Right now the two events I know I'll want to keep track of is when a
Creature
attacks another Creature and when a Creature dies, and I see myself
coming up
with new events as my game grows more intricate. Different parts of
my game
would be interested in different kinds of events. The graphical
display would
be interested in all events so that it may display an appropriate
animation.
More importantly, the different AIs would need to observe recent
events to
modify their behaviour. If a Creature is attacked, the AI controlling
it has a
vested interest in knowing about it.
I'm having trouble modeling this because the different kinds of events
would
keep potentially arbitrary information, making it hard to generalize.
An attack
event would know the attacking Creature, the target Creature, and the
amount of
damage done. A death event would just know the Creature that died. I
might
also implement Creatures grabbing and throwing other Creatures, so a
throw event
would need to know the throwing Creature, the thrown Creature, and
where the
thrown Creature landed. For this reason I can't really make them
subclasses of
a single Event class, since they hold such different information.
So how would I model these events and let my system handle and respond
to them?
Thanks
(I apologize if this double-posts. It didn't appear to take the first
time, but I can't be sure.)

With all that to track, have you considered using a database? I
realize that performance with a database might not be as predictable
as a RAM-only solution, but depending on the game, it might not matter
much. Maybe bats or lightning can fill in the times when the DB is
slow doing garbage collection etc. I would try to make the DB
processing and game engine be somewhat indepedent processes so that
something can still be visually happening while the DB is "thinking".
SqLite is a light-duty DB open-source C-based engine that may be
embeddable in a game.

Do you really want to build a complex interrelating structure(s) with
objects instead of a DB? You would end up reinventing a lot of DB-like
idioms from scratch if you did it with classes.

Just an idea.

-T-
Jordan Marr
2007-04-27 05:21:56 UTC
Permalink
Post by topmind
Do you really want to build a complex interrelating structure(s) with
objects instead of a DB? You would end up reinventing a lot of DB-like
idioms from scratch if you did it with classes.
But you would avoid constantly accessing the slow hard drive!

Gamers have always gone out of their way to avoid hard drive access as much
as possible. Virtual memory == choppy game play and low framerates! That's
why most gamers who know anything about computers go heavy on the RAM to
avoid hard drive access as much as possible.

Maybe if the OP was programming solitaire.. no wait, i'd still choose OO /
RAM over RDBMS / HardDrive for solitaire!

Jordan
topmind
2007-04-27 15:39:12 UTC
Permalink
Post by Jordan Marr
Post by topmind
Do you really want to build a complex interrelating structure(s) with
objects instead of a DB? You would end up reinventing a lot of DB-like
idioms from scratch if you did it with classes.
But you would avoid constantly accessing the slow hard drive!
DB != disk. A good DB only goes to disk when RAM is not available.
Post by Jordan Marr
Gamers have always gone out of their way to avoid hard drive access as much
as possible. Virtual memory == choppy game play and low framerates! That's
why most gamers who know anything about computers go heavy on the RAM to
avoid hard drive access as much as possible.
But this tends to model how humans and animals think. Outside of raw
instinct (run like hell!), we tend to stop for a second or so to think
and sometimes get confused temporarily. I did not suggest that
everything be hooked up to the DB. The general strategy and planning
would sort of be processed in the background, in an independent
process from the here-and-now game engine. The primary (visable)
process would generally take care of instinct-oriented behavior, but
strategy etc. would come from the DB-driven strategy engine. Even
though your enemy might be running for cover at the moment, he/she/it
should still be working on a counter-attack strategy.
Post by Jordan Marr
Maybe if the OP was programming solitaire.. no wait, i'd still choose OO /
RAM over RDBMS / HardDrive for solitaire!
Jordan
-T-
Jordan Marr
2007-04-27 18:36:04 UTC
Permalink
Post by topmind
Post by Jordan Marr
But you would avoid constantly accessing the slow hard drive!
DB != disk. A good DB only goes to disk when RAM is not available.
Which DBs can you imbed completely in memory like this?
Post by topmind
I did not suggest that
everything be hooked up to the DB. The general strategy and planning
would sort of be processed in the background, in an independent
process from the here-and-now game engine. The primary (visable)
process would generally take care of instinct-oriented behavior, but
strategy etc. would come from the DB-driven strategy engine. Even
though your enemy might be running for cover at the moment, he/she/it
should still be working on a counter-attack strategy.
I see what you're saying, and if you had a small footprint /
completely in-memory DB, this could work nicely. Still though, it
seems easier to me to just objects and strategy pattern. This would
be all in memory, and it would avoid the chore of hooking up to a
database. I would rather use objects that represent the problem
domain than table structures that represent a database. After all, my
ogre is implementing a strategy, not a table!

jordan
topmind
2007-04-27 21:13:02 UTC
Permalink
Post by Jordan Marr
Post by topmind
Post by Jordan Marr
But you would avoid constantly accessing the slow hard drive!
DB != disk. A good DB only goes to disk when RAM is not available.
Which DBs can you imbed completely in memory like this?
Completely? Is that really the goal? Anyhow, relational theory does
not dictate implementation. Whether there is an existing
implementation that uses RAM exactly the way you want, I don't know.
Post by Jordan Marr
Post by topmind
I did not suggest that
everything be hooked up to the DB. The general strategy and planning
would sort of be processed in the background, in an independent
process from the here-and-now game engine. The primary (visable)
process would generally take care of instinct-oriented behavior, but
strategy etc. would come from the DB-driven strategy engine. Even
though your enemy might be running for cover at the moment, he/she/it
should still be working on a counter-attack strategy.
I see what you're saying, and if you had a small footprint /
completely in-memory DB, this could work nicely. Still though, it
seems easier to me to just objects and strategy pattern. This would
be all in memory, and it would avoid the chore of hooking up to a
database.
In biz apps a database is usually available and assumed available. If
one is not used to such in a game system, I suppose it could be time-
consuming at first. But if you wish to make lots of games with the
same engine, then the up-front config time may pay off.
Post by Jordan Marr
I would rather use objects that represent the problem
domain than table structures that represent a database. After all, my
ogre is implementing a strategy, not a table!
You assume they are mutually-exclusive. I find apps easier to manage
when the stuff that can readily be represented in a declarative form
is put into a declarative form. Behavioral coding is then reserved
mostly just for the *exceptions* (oddballs) that need custom
fiddling.

When stuff is tablized, I can search, sort, and sift it as I please to
study any given aspect or relationship I want. Code is much more
difficult to do that. If people had such good memories, we wouldn't
need databases. If you are a structure savant who memorizes where
everything is and who doesn't need analysis and sifting tools, I
applaud you.

Plus it is easier to do many collection-oriented operations with a DB
than hopping around pointer loops in code. It is easier to do "math"
on declarative info than behaviorally-encoded info. To me, the
navigational/OO approach reminds me of Go To's.
Post by Jordan Marr
jordan
-T-
Loading...