bool DoPathFind() { Dynamic targetObj = null; { RTSUnitAI ai = Intellect as RTSUnitAI; if (ai != null) { targetObj = ai.CurrentTask.Entity; } } //remove this unit from the pathfinding grid GetNavigationSystem().RemoveObjectFromMotionMap(this); float radius = Type.Radius; Rect targetRect = new Rect( MovePosition.ToVec2() - new Vec2(radius, radius), MovePosition.ToVec2() + new Vec2(radius, radius)); //remove target unit from the pathfinding grid if (targetObj != null && targetObj != this) { GetNavigationSystem().RemoveObjectFromMotionMap(targetObj); } //TO DO: really need this call? GetNavigationSystem().AddTempClearMotionMap(targetRect); const int maxFieldsDistance = 1000; const int maxFieldsToCheck = 100000; bool found = GetNavigationSystem().FindPath( Type.Radius * 2 * 1.1f, Position.ToVec2(), MovePosition.ToVec2(), maxFieldsDistance, maxFieldsToCheck, true, false, path); GetNavigationSystem().DeleteAllTempClearedMotionMap(); //add target unit to the pathfinding grid if (targetObj != null && targetObj != this) { GetNavigationSystem().AddObjectToMotionMap(targetObj); } //add this unit the the pathfinding grid GetNavigationSystem().AddObjectToMotionMap(this); return(found); }
bool DoPathFind() { Dynamic targetObj = null; { //not true because use Intellect RTSUnitAI ai = Intellect as RTSUnitAI; if (ai != null) { targetObj = ai.CurrentTask.Entity; } } GridPathFindSystem.Instance.RemoveObjectFromMotionMap(this); Bounds bounds = PhysicsModel.GetGlobalBounds(); float radius = Type.Radius; Rect targetRect = new Rect(MovePosition.ToVec2() - new Vec2(radius, radius), MovePosition.ToVec2() + new Vec2(radius, radius)); if (targetObj != null && targetObj != this) { GridPathFindSystem.Instance.RemoveObjectFromMotionMap(targetObj); } GridPathFindSystem.Instance.DoTempClearMotionMap(targetRect); const int maxFieldsDistance = 1000; const int maxFieldsToCheck = 100000; bool found = GridPathFindSystem.Instance.DoFind(Type.Radius * 2 * 1.1f, Position.ToVec2(), MovePosition.ToVec2(), maxFieldsDistance, maxFieldsToCheck, true, false, path); GridPathFindSystem.Instance.RestoreAllTempClearedMotionMap(); if (targetObj != null && targetObj != this) { GridPathFindSystem.Instance.AddObjectToMotionMap(targetObj); } GridPathFindSystem.Instance.AddObjectToMotionMap(this); return(found); }
void TickMove() { //path find control { if (pathFindWaitTime != 0) { pathFindWaitTime -= TickDelta; if (pathFindWaitTime < 0) { pathFindWaitTime = 0; } } if (pathFoundedToPosition != MovePosition.ToVec2() && pathFindWaitTime == 0) { path.Clear(); } if (path.Count == 0) { if (pathFindWaitTime == 0) { if (DoPathFind()) { pathFoundedToPosition = MovePosition.ToVec2(); pathFindWaitTime = .5f; } else { pathFindWaitTime = 1.0f; } } } } if (path.Count == 0) { return; } //line movement to path[ 0 ] { Vec2 destPoint = path[0]; Vec2 diff = destPoint - Position.ToVec2(); if (diff == Vec2.Zero) { path.RemoveAt(0); return; } Radian dir = MathFunctions.ATan16(diff.Y, diff.X); float halfAngle = dir * 0.5f; Quat rot = new Quat(new Vec3(0, 0, MathFunctions.Sin16(halfAngle)), MathFunctions.Cos16(halfAngle)); Rotation = rot; Vec2 dirVector = diff.GetNormalizeFast(); Vec2 dirStep = dirVector * (Type.MaxVelocity * TickDelta); Vec2 newPos = Position.ToVec2() + dirStep; if (Math.Abs(diff.X) <= Math.Abs(dirStep.X) && Math.Abs(diff.Y) <= Math.Abs(dirStep.Y)) { //unit at point newPos = path[0]; path.RemoveAt(0); } GridPathFindSystem.Instance.RemoveObjectFromMotionMap(this); bool free; { float radius = Type.Radius; Rect targetRect = new Rect(newPos - new Vec2(radius, radius), newPos + new Vec2(radius, radius)); free = GridPathFindSystem.Instance.IsFreeInMapMotion(targetRect); } GridPathFindSystem.Instance.AddObjectToMotionMap(this); if (free) { float newZ = GridPathFindSystem.Instance.GetMotionMapHeight(newPos) + Type.Height * .5f; Position = new Vec3(newPos.X, newPos.Y, newZ); } else { path.Clear(); } } }