/** Updates an area in the list graph.
* Recalculates possibly affected connections, i.e all connectionlines passing trough the bounds of the \a guo will be recalculated
* \astarpro */
public void UpdateArea(GraphUpdateObject guo)
{
if (nodes == null)
{
return;
}
for (int i = 0; i < nodeCount; i++)
{
if (guo.bounds.Contains((Vector3)nodes[i].position))
{
guo.WillUpdateNode(nodes[i]);
guo.Apply(nodes[i]);
}
}
if (guo.updatePhysics)
{
//Use a copy of the bounding box, we should not change the GUO's bounding box since it might be used for other graph updates
Bounds bounds = guo.bounds;
if (thickRaycast)
{
//Expand the bounding box to account for the thick raycast
bounds.Expand(thickRaycastRadius * 2);
}
//Create two temporary arrays used for holding new connections and costs
List <GraphNode> tmp_arr = Pathfinding.Util.ListPool <GraphNode> .Claim();
List <uint> tmp_arr2 = Pathfinding.Util.ListPool <uint> .Claim();
for (int i = 0; i < nodeCount; i++)
{
PointNode node = nodes[i] as PointNode;
Vector3 a = (Vector3)node.position;
List <GraphNode> conn = null;
List <uint> costs = null;
for (int j = 0; j < nodeCount; j++)
{
if (j == i)
{
continue;
}
Vector3 b = (Vector3)nodes[j].position;
if (Polygon.LineIntersectsBounds(bounds, a, b))
{
float dist;
PointNode other = nodes[j] as PointNode;
bool contains = node.ContainsConnection(other);
//Note, the IsValidConnection test will actually only be done once
//no matter what,so there is no performance penalty there
if (!contains && IsValidConnection(node, other, out dist))
{
//Debug.DrawLine (a+Vector3.up*0.1F,b+Vector3.up*0.1F,Color.green);
if (conn == null)
{
tmp_arr.Clear();
tmp_arr2.Clear();
conn = tmp_arr;
costs = tmp_arr2;
conn.AddRange(node.connections);
costs.AddRange(node.connectionCosts);
}
uint cost = (uint)Mathf.RoundToInt(dist * Int3.FloatPrecision);
conn.Add(other);
costs.Add(cost);
}
else if (contains && !IsValidConnection(node, other, out dist))
{
//Debug.DrawLine (a+Vector3.up*0.5F*Random.value,b+Vector3.up*0.5F*Random.value,Color.red);
if (conn == null)
{
tmp_arr.Clear();
tmp_arr2.Clear();
conn = tmp_arr;
costs = tmp_arr2;
conn.AddRange(node.connections);
costs.AddRange(node.connectionCosts);
}
int p = conn.IndexOf(other);
//Shouldn't have to check for it, but who knows what might go wrong
if (p != -1)
{
conn.RemoveAt(p);
costs.RemoveAt(p);
}
}
}
}
if (conn != null)
{
node.connections = conn.ToArray();
node.connectionCosts = costs.ToArray();
}
}
Pathfinding.Util.ListPool <GraphNode> .Release(tmp_arr);
Pathfinding.Util.ListPool <uint> .Release(tmp_arr2);
}
}