/// <summary> /// Creates the parameter for the block and runs the program. /// </summary> private void HandleDetected() { if (m_progBlock.IsRunning) { return; } StringBuilder parameter = new StringBuilder(); bool first = true; RelayStorage store = m_networkClient.GetStorage(); if (store == null) { return; } store.ForEachLastSeen((LastSeen seen) => { ExtensionsRelations.Relations relations = (m_progBlock as IMyCubeBlock).getRelationsTo(seen.Entity, ExtensionsRelations.Relations.Enemy).highestPriority(); bool friendly = ExtensionsRelations.toIsFriendly(relations); string bestName = friendly ? seen.Entity.getBestName() : seen.HostileName(); TimeSpan sinceSeen; Vector3D predictedPosition = seen.predictPosition(out sinceSeen); if (first) { first = false; } else { parameter.Append(entitySeparator); } parameter.Append(seen.Entity.EntityId); parameter.Append(fieldSeparator); parameter.Append((byte)relations); parameter.Append(fieldSeparator); parameter.Append((byte)seen.Type); parameter.Append(fieldSeparator); parameter.Append(bestName); parameter.Append(fieldSeparator); parameter.Append(seen.isRecent_Radar()); parameter.Append(fieldSeparator); parameter.Append(seen.isRecent_Jam()); parameter.Append(fieldSeparator); parameter.Append((int)sinceSeen.TotalSeconds); parameter.Append(fieldSeparator); parameter.Append(Math.Round(predictedPosition.X, 1)); parameter.Append(fieldSeparator); parameter.Append(Math.Round(predictedPosition.Y, 1)); parameter.Append(fieldSeparator); parameter.Append(Math.Round(predictedPosition.Z, 1)); parameter.Append(fieldSeparator); parameter.Append(Math.Round(seen.LastKnownVelocity.X, 1)); parameter.Append(fieldSeparator); parameter.Append(Math.Round(seen.LastKnownVelocity.Y, 1)); parameter.Append(fieldSeparator); parameter.Append(Math.Round(seen.LastKnownVelocity.Z, 1)); parameter.Append(fieldSeparator); if (seen.RadarInfoIsRecent()) { parameter.Append(seen.Info.Volume); } else { parameter.Append(0f); } if (!friendly && seen.Type == LastSeen.EntityType.Grid && m_blockCountList_sb.Length > 2 && seen.isRecent() && m_blockCountList_btl != null) { int[] blockCounts = m_blockCountList_btl.Count(CubeGridCache.GetFor((IMyCubeGrid)seen.Entity)); if (blockCounts.Length != 0) { parameter.Append(fieldSeparator); parameter.Append(string.Join(fieldSeparator.ToString(), blockCounts)); } } }); if (parameter.Length == 0) { Log.DebugLog("no detected entities"); return; } //Log.DebugLog("parameters:\n" + parameter.ToString().Replace(string.Empty + entitySeparator, entitySeparator + "\n")); if (!m_progBlock.TryRun(parameter.ToString())) { Log.AlwaysLog("Failed to run program", Logger.severity.INFO); } }
/// <summary> /// Tests if a section of space can be travelled without hitting the specified entity. /// </summary> /// <param name="entity">The potential obstruction</param> /// <param name="ignoreBlock">Null or the block autopilot is trying to connect with.</param> /// <param name="input"><see cref="TestInput"/></param> /// <param name="result"><see cref="GridTestResult"/></param> /// <returns>True if the specified entity obstructs the path.</returns> public bool ObstructedBy(MyEntity entity, MyCubeBlock ignoreBlock, ref TestInput input, out GridTestResult result) { //Logger.DebugLog("checking: " + entity.getBestName() + ", offset: " + offset + ", rejection vector: " + rejectionVector + ", rejection distance: " + rejectionDistance); #if DEBUG if (!input.Direction.IsValid() || Math.Abs(1f - input.Direction.LengthSquared()) > 0.01f) { throw new Exception("rejection vector is invalid. entity: " + entity.nameWithId() + ", input: " + input); } #endif result = GridTestResult.Default; result.Distance = input.Length; MyCubeGrid grid = entity as MyCubeGrid; if (grid != null) { // check for dangerous tools on grid CubeGridCache cache = CubeGridCache.GetFor(grid); if (cache == null) { return(false); } Profiler.StartProfileBlock("Checking Tools"); foreach (MyShipDrill drill in cache.BlocksOfType(typeof(MyObjectBuilder_Drill))) { if (drill.IsShooting) { if (SphereTest(drill, ref input, ref result)) { Profiler.EndProfileBlock(); return(true); } } } foreach (MyShipGrinder grinder in cache.BlocksOfType(typeof(MyObjectBuilder_ShipGrinder))) { if (grinder.IsShooting) { if (SphereTest(grinder, ref input, ref result)) { Profiler.EndProfileBlock(); return(true); } } } Profiler.EndProfileBlock(); if (ExtensionsRelations.canConsiderFriendly(Controller.CubeBlock, grid) && EndangerGrid(grid, ref input, ref result)) { Logger.DebugLog("Movement would endanger: " + grid.getBestName()); return(true); } Profiler.StartProfileBlock("RejectionIntersects"); if (RejectionIntersects(grid, ignoreBlock, ref input, ref result)) { Profiler.EndProfileBlock(); return(true); } Profiler.EndProfileBlock(); } else { return(SphereTest(entity, ref input, ref result)); } return(false); }