Neighbor List

From AI Product Manuals
Revision as of 13:33, 7 October 2021 by Davemark (talk | contribs) (Initial Entries)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search
This page is part of the IAUS Manual.BrainBehaviorBehavior TypeDecisionAI Entity

This page is part of the Brain

Concept

The Neighbor List is a list of weak pointers to the AI Entities that the owner agent can take into account when making a decision.

Because we will be walking through the possible neighbors for every Behavior that has Context Type - Single Target or Context Type - Single Target on Self, it is helpful to keep this list handy. By constructing the list once at the beginning of the Think Cycle, it keeps us from having to keep asking the engine for entities within the range of our interest.

In a general sense, it is usually based on a radius around the owning character. However, there can be exceptions such as using the Landmark tag on an AI Entity. In this case, an AI Entity with the Landmark tag will be included regardless of distance (or using a different radius).

Implementation

Code

The Neighbor List is kept in the Brain class. For readability, it is typedef'd:

//////////////////////////////////////////////////////////////////////////
// Entity for keeping track of relevant neighbors while making decisions 
// so that they don't need to be reacquired by the engine

typedef AiEntityWP NeighborEntity;
typedef std::list<NeighborEntity> NeighborList;

It is then held on each individual Brain object as NeighborList currentNeighbors;.

At the beginning of the think cycle, the neighbor list is cleared and rebuilt. In order to do this, it needs to request the object list from the game engine and run it through various checks to see if the AI Entity qualifies and a pointer to it should be added to the list.

void Brain::RebuildNeighborList()
{		
	CurrentNeighbors().clear();
	
	//Get game entities from the world
	EntityList* list = gameWorld->GetEntityList();
	if (!list)
		return;

	for (AiEntityWP thisEntity : *list)
	{
		if (auto entity = thisEntity.lock())
		{
			float distance = 0.f;
			if (auto parent = parentAiEntity.lock())
				distance = entity->Position().Distance(parent->Position());
			
			// If this is landmark or within range
			if ((entity->HasTag(EntityTag::Landmark) || distance < MAX_NEIGHBOR_DIST)
			{
				CurrentNeighbors().push_back(thisEntity);
			}
		}
	}
}

Note that the maximum neighbor distance is defined at the top of the Brain class for ease of changing.

Data

There is no data reference for a neighbor list. However, there may be data settings that change what the criteria are for something to be included in a neighbor list (for example, a different type of world object type or entity tag). If so, this would have be supported by changing the code above to account for it.


Website, Manuals wiki, and Product Code ©2021, Intrinsic Algorithm LLC