Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Questions about AI


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 ultramailman   Prime Members   -  Reputation: 1581

Like
2Likes
Like

Posted 27 October 2012 - 11:55 PM

Hello

I am making a 2d adventure/rpg game right now, and I just got the world/room thing working. Now the player (a white brick) can walk around two rooms made of identical tiles (blue bricks), and collision seem to work fine.

Now it is time to add some npc that can also walk around, do things, etc. The game is made of several rooms, and each room has a linked list of game objects. The game objects however know nothing about the surrounding.

I thought of passing a pointer of the room to the object, but that sound's like it can get messy because of the circular dependency.

Then I thought I'd use a sensory object that is basically a rectangle and a list, and it would follow the npc object around. The world update function would then update each object normally, and add any objects touching the sensory rectangle to that list.

My questions:

1. What is a good way to give awareness to npc objects?

2. If I want to add path finding to the AI later, is the linked list a good way to store game objects? game objects include tiles, player, npc, and every game object has a position, velocity, collision rectangle.

3. (don't know what to ask yet)

Sponsor:

#2 Katie   Members   -  Reputation: 1359

Like
3Likes
Like

Posted 28 October 2012 - 02:35 AM

"I thought of passing a pointer of the room to the object, but that sound's like it can get messy because of the circular dependency."

It's not a 'circular dependency', it's a circular reference. Pointers can but do not necessarily convey memory ownership.

It's fairly typical in these sorts of games that the objects know which room structure they belong to and the room has references to all the objects contained in it. The room comes with nice utility functions such as "iterate over all contents except object X" or "iterate over all contents except the ones in this list" or "iterate over all the contents whose ID exceeds N" so that an object can cause things to happen to other objects more easily. Also you're going to have finder methods; "Find me all the objects which are touching object A". "Iterate over all objects within R of X,Y".


A circular dependency would imply that you're expect the room to delete the objects in it when they're not needed (probably the case) and also that the objects are expecting to delete the room they're in for some reason (which would be nuts. Not least because many objects would delete the room several times).

The directed and necessarily acyclic graph of memory ownership is not the same as the possibly cyclical graph of object references.

#3 Lauris Kaplinski   Members   -  Reputation: 841

Like
0Likes
Like

Posted 28 October 2012 - 03:13 AM

As Katie told, be no afraid of circular references.

In your example - sooner or later you want not only to detect collisions but plan actions (pathfinding, discovery etc.). To do such actions your NPC needs the whole room/level, not only parts that are touching.

As of your second question - single linked list is most probably not enough. You need at least one spatially organized container (2D or 3D array, octree, quadtree...) that allows you to make fast spatial lookups (check whether certain grid spot is free, check what is at neighboring slot...). In addition to that you may want to have additional containers - for example all dynamic objects (mobs) can be in linked list, so you can quickly iterate over them.
Having single object referenced from many containers is acceptable - you only have to have clear idea who owns it (i.e. who is responsible of creating/deleting it).
Lauris Kaplinski

First technology demo of my game Shinya is out: http://lauris.kaplinski.com/shinya
Khayyam 3D - a freeware poser and scene builder application: http://khayyam.kaplinski.com/

#4 ultramailman   Prime Members   -  Reputation: 1581

Like
0Likes
Like

Posted 28 October 2012 - 01:39 PM

"I thought of passing a pointer of the room to the object, but that sound's like it can get messy because of the circular dependency."

It's not a 'circular dependency', it's a circular reference. Pointers can but do not necessarily convey memory ownership.

It's fairly typical in these sorts of games that the objects know which room structure they belong to and the room has references to all the objects contained in it. The room comes with nice utility functions such as "iterate over all contents except object X" or "iterate over all contents except the ones in this list" or "iterate over all the contents whose ID exceeds N" so that an object can cause things to happen to other objects more easily. Also you're going to have finder methods; "Find me all the objects which are touching object A". "Iterate over all objects within R of X,Y".


A circular dependency would imply that you're expect the room to delete the objects in it when they're not needed (probably the case) and also that the objects are expecting to delete the room they're in for some reason (which would be nuts. Not least because many objects would delete the room several times).

The directed and necessarily acyclic graph of memory ownership is not the same as the possibly cyclical graph of object references.


Thanks, I'll try that then.

Also, by circular dependency I meant the need to #include "gameobj.h" in world.h, and #include "world.h" in gameobj.h to allow rooms contain game objects, and at the same time allow game objects to have a pointer to rooms.

But hey, I guess thats what include guards are for ;o

As Katie told, be no afraid of circular references.

In your example - sooner or later you want not only to detect collisions but plan actions (pathfinding, discovery etc.). To do such actions your NPC needs the whole room/level, not only parts that are touching.

As of your second question - single linked list is most probably not enough. You need at least one spatially organized container (2D or 3D array, octree, quadtree...) that allows you to make fast spatial lookups (check whether certain grid spot is free, check what is at neighboring slot...). In addition to that you may want to have additional containers - for example all dynamic objects (mobs) can be in linked list, so you can quickly iterate over them.
Having single object referenced from many containers is acceptable - you only have to have clear idea who owns it (i.e. who is responsible of creating/deleting it).


Whew, thats a lot of containers. I'd love to use a 2D array, but my game objects can be in any position (they are not confined to a fixed block). That leaves me with a 2D array of linked lists of objects, or a quadtree of objects, or something else.

#5 Álvaro   Crossbones+   -  Reputation: 13649

Like
0Likes
Like

Posted 28 October 2012 - 07:29 PM

But hey, I guess thats what include guards are for ;o


You may also have to use forward declarations. Wikipedia has an example of how to implement circular dependencies in C++.

#6 ultramailman   Prime Members   -  Reputation: 1581

Like
0Likes
Like

Posted 28 October 2012 - 11:16 PM


But hey, I guess thats what include guards are for ;o


You may also have to use forward declarations. Wikipedia has an example of how to implement circular dependencies in C++.


Thanks.

Now I find myself with two more options:
1. pass the objects the linked list instead.
2. pass nothing. The linked list I am using is actually embedded inside the object already, as was required by this particular linked list implementation.

#7 Lauris Kaplinski   Members   -  Reputation: 841

Like
0Likes
Like

Posted 29 October 2012 - 04:48 AM

Whew, thats a lot of containers. I'd love to use a 2D array, but my game objects can be in any position (they are not confined to a fixed block). That leaves me with a 2D array of linked lists of objects, or a quadtree of objects, or something else.


Is it also true of your tiles? Because in most 2D dungeon-syled games level details (walls) are the most frequent colliders by an order of magnitude. Thus it may make sense to keep level details and movable objects in separate containers if it simplifies collision testing.

Otherwise either quadtree or array of lists seem reasonable. The latter is probably easier to implement. Just make your container interface abstract enough so you can change implementation details later if you find that your initial implementation is not flexible/fast enough.
Lauris Kaplinski

First technology demo of my game Shinya is out: http://lauris.kaplinski.com/shinya
Khayyam 3D - a freeware poser and scene builder application: http://khayyam.kaplinski.com/

#8 ultramailman   Prime Members   -  Reputation: 1581

Like
0Likes
Like

Posted 29 October 2012 - 07:14 PM


Is it also true of your tiles? Because in most 2D dungeon-syled games level details (walls) are the most frequent colliders by an order of magnitude. Thus it may make sense to keep level details and movable objects in separate containers if it simplifies collision testing.


The game right now makes no distinction between tiles and game objects. Tiles are merely game objects whose "movable" flag is set to 0. I did however added two more linked list, one for the movable objects and the other for unmovable objects, like you suggested. I am not seeing any improvements because I only have around 60 objects in one room right now, but I am sure it will be helpful as I add more movable objects.

Otherwise either quadtree or array of lists seem reasonable. The latter is probably easier to implement. Just make your container interface abstract enough so you can change implementation details later if you find that your initial implementation is not flexible/fast enough.


I think I will go with the quadtree, it feels like even if it is harder to implement, it will be more useful in the future. And I think it will be abstract enough, like using functions to read and modify it, using convenience macros to iterate through it, etc.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS