public void Move() { if (Globals.UpdateCount < m_nextUpdate) { return; } m_nextUpdate = Globals.UpdateCount + 100ul; if (m_sourceInventory.Count == 0) { FindSourceInventory(); if (m_sourceInventory.Count == 0) { Log.DebugLog("No source inventories found", Logger.severity.WARNING); m_navSet.OnTaskComplete_NavWay(); return; } } if (m_shoppingList.Count != 0) { MyAPIGateway.Utilities.TryInvokeOnGameThread(m_currentTask); } else { Log.DebugLog("Shopping finished, proceed to checkout", Logger.severity.INFO); m_navSet.OnTaskComplete_NavWay(); } }
/// <summary> /// Test a path between current position and destination. /// </summary> private void TestPath(Vector3D destination, MyEntity ignoreEntity, byte runId, bool isAlternate, bool tryAlternates, bool slowDown = false) { m_logger.debugLog("m_navBlock == null", Logger.severity.FATAL, condition: m_navBlock == null); if (runId != m_runId) { m_logger.debugLog("destination changed, abort", Logger.severity.DEBUG); return; } if (!lock_testPath.TryAcquireExclusive()) { m_logger.debugLog("Already running, requeue (destination:" + destination + ", ignoreEntity: " + ignoreEntity.getBestName() + ", runId :" + runId + ", isAlternate: " + isAlternate + ", tryAlternates: " + tryAlternates + ", slowDown: " + slowDown + ")"); LockedQueue <Action> queue = isAlternate ? m_pathLow : m_pathHigh; queue.Enqueue(() => TestPath(destination, ignoreEntity, runId, isAlternate, tryAlternates)); return; } try { if (m_grid != m_navBlock.Grid) { m_logger.debugLog("Grid has changed, from " + m_grid.getBestName() + " to " + m_navBlock.Grid.getBestName() + ", nav block: " + m_navBlock.Block.getBestName(), Logger.severity.WARNING); return; } m_logger.debugLog("Running, (destination:" + destination + ", ignoreEntity: " + ignoreEntity.getBestName() + ", runId :" + runId + ", isAlternate: " + isAlternate + ", tryAlternates: " + tryAlternates + ", slowDown: " + slowDown + ")"); MyEntity obstructing; Vector3? pointOfObstruction; if (!isAlternate && !m_ignoreAsteroid) { if (slowDown) { if ((m_planetCheckSpeed.CurrentState & PlanetChecker.State.Blocked) != 0) { float speed = Vector3.Distance(m_planetCheckSpeed.ObstructionPoint, m_navBlock.WorldPosition) * 0.1f; if (speed < 1f) { speed = 1f; } m_navSet.Settings_Task_NavWay.SpeedTarget = speed; m_logger.debugLog("Path blocked by planet, set SpeedTarget to " + speed + ", obstructed by planet", Logger.severity.TRACE); return; } } else { if ((m_planetCheckDest.CurrentState & PlanetChecker.State.Blocked) != 0 && // planet checker is using an older displacement so verify that obstruction is closer than destination Vector3D.DistanceSquared(m_navBlock.WorldPosition, m_planetCheckDest.ObstructionPoint) < Vector3D.DistanceSquared(m_navBlock.WorldPosition, destination)) { m_logger.debugLog("path blocked by planet, to destination: " + (destination - m_navBlock.WorldPosition) + ", to obstruction: " + (m_planetCheckDest.ObstructionPoint - m_navBlock.WorldPosition)); if (m_pathState < PathState.Searching) { m_pathState = PathState.Searching; } obstructing = m_planetCheckDest.gravcomp; Vector3 direction = Vector3.Normalize(m_navBlock.WorldPosition - obstructing.GetCentre()); pointOfObstruction = m_planetCheckDest.ObstructionPoint + direction * 1e3f; float distance = Vector3.Distance(m_navBlock.WorldPosition, pointOfObstruction.Value); MoveObstruction = obstructing; m_pathHigh.Clear(); ClearAltPath(); if ((m_planetCheckDest.CurrentState & PlanetChecker.State.BlockedPath) != 0) { FindAlternate_AroundObstruction(pointOfObstruction.Value - m_navBlock.WorldPosition, new Vector3[] { direction }, 1e4f, runId); } else // blocked by gravity { FindAlternate_AroundObstruction(pointOfObstruction.Value - m_navBlock.WorldPosition, new Vector3[] { direction }, 1e6f, runId); } m_pathLow.Enqueue(() => { if (m_altPath_AlternatesFound != 0) { SetWaypoint(); } RunItem(); }); m_pathLow.Enqueue(() => m_pathState = PathState.Path_Blocked); return; } } } // for alternates, check that it can be better than current value if (isAlternate) { float distToWaypointSquared = (float)Vector3D.DistanceSquared(m_navBlock.WorldPosition, destination); if (distToWaypointSquared * WaypointDistanceBias * WaypointDistanceBias > m_altPath_PathValue * m_altPath_PathValue) { m_logger.debugLog("no point in checking alternate path, bias is too high", Logger.severity.TRACE); m_logger.debugLog("no alternate, yet path value is set", Logger.severity.ERROR, condition: m_altPath_AlternatesFound == 0); IncrementAlternatesFound(); return; } } if (m_pathChecker.TestFast(m_navBlock, destination, m_ignoreAsteroid, ignoreEntity, m_landing)) { m_logger.debugLog("path is clear (fast)", Logger.severity.TRACE); PathClear(ref destination, runId, isAlternate, slowDown); return; } if (m_pathChecker.TestSlow(out obstructing, out pointOfObstruction)) { m_logger.debugLog("path is clear (slow)", Logger.severity.TRACE); PathClear(ref destination, runId, isAlternate, slowDown); return; } if (runId != m_runId) { m_logger.debugLog("destination changed, abort", Logger.severity.DEBUG); return; } if (slowDown) { float speed = Vector3.Distance(pointOfObstruction.Value, m_navBlock.WorldPosition) * 0.1f; if (speed < 1f) { speed = 1f; } IMyEntity destEntity = m_navSet.Settings_Current.DestinationEntity; if (destEntity != null) { destEntity = destEntity.GetTopMostParent(); } if (obstructing.GetTopMostParent() == destEntity) { m_navSet.Settings_Task_NavWay.SpeedMaxRelative = speed; m_logger.debugLog("Set SpeedMaxRelative to " + speed + ", obstructing: " + obstructing.getBestName() + ", DestinationEntity: " + m_navSet.Settings_Current.DestinationEntity.getBestName(), Logger.severity.TRACE); } else { m_navSet.Settings_Task_NavWay.SpeedTarget = speed; m_logger.debugLog("Set SpeedTarget to " + speed + ", obstructing: " + obstructing.getBestName() + ", DestinationEntity: " + m_navSet.Settings_Current.DestinationEntity.getBestName(), Logger.severity.TRACE); } return; } if (m_pathState < PathState.Searching) { m_pathState = PathState.Searching; } m_logger.debugLog("path is blocked by " + obstructing.getBestName() + " at " + pointOfObstruction + ", ignoreEntity: " + ignoreEntity.getBestName(), isAlternate ? Logger.severity.TRACE : Logger.severity.DEBUG); m_logger.debugLog("grid: " + obstructing.GetTopMostParent().DisplayName, isAlternate ? Logger.severity.TRACE : Logger.severity.DEBUG, condition: obstructing is IMyCubeBlock); if (isAlternate && m_altPath_AlternatesFound != 0) { IncrementAlternatesFound(); } if (tryAlternates) { //float angle = m_navSet.Settings_Current.DistanceAngle; //if (angle > 0.1f && CanRotate) //{ // m_logger.debugLog("wait for rotation", "TestPath()"); // return; //} if (m_navSet.Settings_Task_NavEngage.NavigatorMover != m_navSet.Settings_Current.NavigatorMover) { m_logger.debugLog("obstructed while flying to a waypoint, throwing it out and starting over", Logger.severity.DEBUG); m_navSet.OnTaskComplete_NavWay(); return; } ClearAltPath(); MoveObstruction = obstructing; TryAlternates(runId, pointOfObstruction.Value, obstructing); } } finally { lock_testPath.ReleaseExclusive(); RunItem(); } }