public MyStructuralIntegrity(MyCubeGrid cubeGrid) { m_cubeGrid = cubeGrid; m_cubeGrid.OnBlockAdded += cubeGrid_OnBlockAdded; switch (1) { case 0: m_simulator = new MyJacobianConstraintSimulator(m_cubeGrid.GetBlocks().Count); break; case 1: m_simulator = new MyAdvancedStaticSimulator(m_cubeGrid); break; case 2: m_simulator = new MyOndraSimulator(m_cubeGrid); break; case 3: m_simulator = new MyOndraSimulator2(m_cubeGrid); break; case 4: m_simulator = new MyOndraSimulator3(m_cubeGrid); break; } foreach (var block in m_cubeGrid.GetBlocks()) { cubeGrid_OnBlockAdded(block); } }
static float GetActiveBlockCount <T>(MyCubeGrid grid, bool includePassive = false) where T : MyFunctionalBlock { if (grid == null) { return(0); } int num = 0; foreach (var block in grid.GetBlocks()) { var b = block.FatBlock as T; if (b != null && (includePassive || b.IsWorking)) { num++; } } return(num); }
public static ulong GetBattlePoints(MyCubeGrid grid) { ulong points = 0; foreach (var block in grid.GetBlocks()) { MyCompoundCubeBlock compoundBlock = block.FatBlock as MyCompoundCubeBlock; if (compoundBlock != null) { foreach (var blockInCompound in compoundBlock.GetBlocks()) points += GetBattlePoints(blockInCompound); } else { points += GetBattlePoints(block); } } return points; }
private static float GetActiveBlockCount <T>(MyCubeGrid grid, bool includePassive = false) where T : MyFunctionalBlock { if (grid == null) { return(0f); } int num = 0; using (HashSet <MySlimBlock> .Enumerator enumerator = grid.GetBlocks().GetEnumerator()) { while (enumerator.MoveNext()) { T fatBlock = enumerator.Current.FatBlock as T; if ((fatBlock != null) && (includePassive || fatBlock.IsWorking)) { num++; } } } return((float)num); }
public static bool CheckMergeConnectivity(MyCubeGrid hitGrid, MyCubeGrid gridToMerge, Vector3I gridOffset) { // CH: Beware, this funtion seems horribly inefficient! Think twice before using it (e.g. don't use it in a 10000x loop) or optimize it :-) MatrixI mergeTransform = hitGrid.CalculateMergeTransform(gridToMerge, gridOffset); Quaternion mergeOri; mergeTransform.GetBlockOrientation().GetQuaternion(out mergeOri); foreach (var block in gridToMerge.GetBlocks()) { Quaternion ori; Vector3I pos = Vector3I.Transform(block.Position, mergeTransform); block.Orientation.GetQuaternion(out ori); //Matrix mergeGridMatrix = Matrix.CreateFromQuaternion(mergeOri); //Matrix oriMatrix = Matrix.CreateFromQuaternion(ori); //Matrix result = oriMatrix * mergeGridMatrix; //Quaternion quatFromMatrix = Quaternion.CreateFromRotationMatrix(result); // Seems that quaternion multiplication is reversed to matrix multiplication! ori = mergeOri * ori; var mountPoints = block.BlockDefinition.GetBuildProgressModelMountPoints(block.BuildLevelRatio); if (MyCubeGrid.CheckConnectivity(hitGrid, block.BlockDefinition, mountPoints, ref ori, ref pos)) return true; } return false; }
public static bool ShouldBeStatic(MyCubeGrid grid) { if (grid.GridSizeEnum == MyCubeSize.Small && MyCubeGridSmallToLargeConnection.Static != null && MyCubeGridSmallToLargeConnection.Static.TestGridSmallToLargeConnection(grid)) return true; foreach (var block in grid.GetBlocks()) { if (IsInVoxels(block)) return true; } return false; }
public bool AddGrid(MyCubeGrid grid) { //TODO: create static list List<Vector3I> lst = new List<Vector3I>(); foreach (var block in grid.GetBlocks()) { if (block.FatBlock is MyCompoundCubeBlock) { bool added = false; foreach (var subb in (block.FatBlock as MyCompoundCubeBlock).GetBlocks()) { if (AddBlock(subb)) { if (!added) lst.Add(block.Position); added = true; } } } else { if (AddBlock(block)) { lst.Add(block.Position); } } } if (lst.Count > 0) { grid.RazeBlocks(lst); return true; } return false; }
static MyTrashRemovalFlags GetTrashState(MyCubeGrid grid, MyTrashRemovalSettings settings, out float metric,bool checkGroup = false) { metric = -1; HashSet<MySlimBlock> blocks = grid.GetBlocks(); if (blocks != null && blocks.Count > settings.BlockCountThreshold) { metric = settings.BlockCountThreshold; return MyTrashRemovalFlags.WithBlockCount; } if (!settings.HasFlag(MyTrashRemovalFlags.Fixed) && grid.IsStatic) return MyTrashRemovalFlags.Fixed; bool isAccelerating = false; bool isLinearMoving = false; bool isStationary = true; if (grid.Physics != null) { isAccelerating = grid.Physics.AngularAcceleration.AbsMax() > 0.05f || grid.Physics.LinearAcceleration.AbsMax() > 0.05f; isStationary = grid.Physics.AngularVelocity.AbsMax() < 0.05f && grid.Physics.LinearVelocity.AbsMax() < 0.05f; isLinearMoving = !isAccelerating && !isStationary; } if (!settings.HasFlag(MyTrashRemovalFlags.Stationary) && isStationary) return MyTrashRemovalFlags.Stationary; if (!settings.HasFlag(MyTrashRemovalFlags.Linear) && isLinearMoving) return MyTrashRemovalFlags.Linear; if (!settings.HasFlag(MyTrashRemovalFlags.Accelerating) && isAccelerating) return MyTrashRemovalFlags.Accelerating; if (grid.GridSystems != null) { bool isPowered = grid.GridSystems.ResourceDistributor.ResourceStateByType(MyResourceDistributorComponent.ElectricityId) != MyResourceStateEnum.NoPower; if (!settings.HasFlag(MyTrashRemovalFlags.Powered) && isPowered) return MyTrashRemovalFlags.Powered; if (!settings.HasFlag(MyTrashRemovalFlags.Controlled) && grid.GridSystems.ControlSystem.IsControlled) return MyTrashRemovalFlags.Controlled; if (!settings.HasFlag(MyTrashRemovalFlags.WithProduction) && grid.GridSystems.ControlSystem.IsControlled) return MyTrashRemovalFlags.WithProduction; } if (!settings.HasFlag(MyTrashRemovalFlags.WithMedBay) && grid.BlocksCounters.GetValueOrDefault(typeof(MyObjectBuilder_MedicalRoom)) > 0) return MyTrashRemovalFlags.WithMedBay; if (IsCloseToPlayerOrCamera(grid, settings.PlayerDistanceThreshold)) { metric = settings.PlayerDistanceThreshold; return MyTrashRemovalFlags.DistanceFromPlayer; } if (checkGroup && MyCubeGridGroups.Static.Physical != null) { var physicalGroup = MyCubeGridGroups.Static.Physical.GetGroup(grid); if (physicalGroup != null) { foreach (var cubeGrid in physicalGroup.Nodes) { if (cubeGrid.NodeData == null || cubeGrid.NodeData.Physics == null || cubeGrid.NodeData.Physics.Shape == null || cubeGrid.NodeData == grid) continue; var subGridReason = GetTrashState(cubeGrid.NodeData, settings, out metric, false); if (subGridReason != MyTrashRemovalFlags.None) { return subGridReason; } } } } return MyTrashRemovalFlags.None; }
public MyOndraSimulator3(MyCubeGrid grid) { m_grid = grid; m_selectedGrid = m_grid; SelectedCube = m_grid.GetBlocks().First().Position; }
public MyAdvancedStaticSimulator(MyCubeGrid grid) { m_grid = grid; m_selectedGrid = m_grid; if (m_grid.BlocksCount > 0) SelectedCube = m_grid.GetBlocks().First().Position; }
public bool AddGrid(MyCubeGrid grid) { HashSet<Sandbox.Game.Entities.Cube.MySlimBlock> lst = new HashSet<Sandbox.Game.Entities.Cube.MySlimBlock>(); foreach(var b in grid.GetBlocks()) { if(b.FatBlock is MyCompoundCubeBlock) { foreach(var subb in (b.FatBlock as MyCompoundCubeBlock).GetBlocks()) { if (AddBlock(subb)) lst.Add(b); } } else { if (AddBlock(b)) lst.Add(b); } } foreach(var b in lst) { grid.RemoveBlock(b); } return lst.Count > 0; //grid.Close(); }
public bool AddGrid(MyCubeGrid grid) { HashSet<Sandbox.Game.Entities.Cube.MySlimBlock> lst = new HashSet<Sandbox.Game.Entities.Cube.MySlimBlock>(); foreach (var block in grid.GetBlocks()) { if (block.FatBlock is MyCompoundCubeBlock) { bool added = false; foreach (var subb in (block.FatBlock as MyCompoundCubeBlock).GetBlocks()) { if (AddBlock(subb)) { added = true; } } if (added) { lst.Add(block); } } else { if (AddBlock(block)) { lst.Add(block); } } } foreach (var b in lst) { Vector3I pos = b.Position; Vector3UByte size = new Vector3UByte(1, 1, 1); grid.RazeBlocks(ref pos, ref size); // grid.RemoveBlock(b, true); not synced } return lst.Count > 0; }
static bool ShouldBeStatic(MyCubeGrid grid, MyCubeGrid.MyTestDynamicReason testReason) { if (grid.GridSizeEnum == MyCubeSize.Small && MyCubeGridSmallToLargeConnection.Static != null && MyCubeGridSmallToLargeConnection.Static.TestGridSmallToLargeConnection(grid)) return true; if (testReason == MyTestDynamicReason.GridSplit) { foreach (var block in grid.GetBlocks()) { if (IsInVoxels(block)) return true; } return false; } return grid.IsStatic; }
public static bool CheckValidBlocksRotation(Matrix gridLocalMatrix, MyCubeGrid grid) { Matrix blockLocalMatrix; bool retval = true; foreach (var block in grid.GetBlocks()) { MyCompoundCubeBlock compoundBlock = block.FatBlock as MyCompoundCubeBlock; if (compoundBlock != null) { foreach (var blockInCompound in compoundBlock.GetBlocks()) { blockInCompound.Orientation.GetMatrix(out blockLocalMatrix); blockLocalMatrix = blockLocalMatrix * gridLocalMatrix; retval = retval && CheckValidBlockRotation(blockLocalMatrix, blockInCompound.BlockDefinition.Direction, blockInCompound.BlockDefinition.Rotation); if (!retval) break; } } else { block.Orientation.GetMatrix(out blockLocalMatrix); blockLocalMatrix = blockLocalMatrix * gridLocalMatrix; retval = retval && CheckValidBlockRotation(blockLocalMatrix, block.BlockDefinition.Direction, block.BlockDefinition.Rotation); } if (!retval) break; } return retval; }
static MyTrashRemovalFlags GetTrashState(MyCubeGrid grid, MyTrashRemovalSettings settings, out float metric, bool checkGroup = false) { metric = -1; HashSet <MySlimBlock> blocks = grid.GetBlocks(); if (blocks != null && blocks.Count > settings.BlockCountThreshold) { metric = settings.BlockCountThreshold; return(MyTrashRemovalFlags.WithBlockCount); } if (!settings.HasFlag(MyTrashRemovalFlags.Fixed) && grid.IsStatic) { return(MyTrashRemovalFlags.Fixed); } bool isAccelerating = false; bool isLinearMoving = false; bool isStationary = true; if (grid.Physics != null) { isAccelerating = grid.Physics.AngularAcceleration.AbsMax() > 0.05f || grid.Physics.LinearAcceleration.AbsMax() > 0.05f; isStationary = grid.Physics.AngularVelocity.AbsMax() < 0.05f && grid.Physics.LinearVelocity.AbsMax() < 0.05f; isLinearMoving = !isAccelerating && !isStationary; } if (!settings.HasFlag(MyTrashRemovalFlags.Stationary) && isStationary) { return(MyTrashRemovalFlags.Stationary); } if (!settings.HasFlag(MyTrashRemovalFlags.Linear) && isLinearMoving) { return(MyTrashRemovalFlags.Linear); } if (!settings.HasFlag(MyTrashRemovalFlags.Accelerating) && isAccelerating) { return(MyTrashRemovalFlags.Accelerating); } if (grid.GridSystems != null) { bool isPowered = grid.GridSystems.ResourceDistributor.ResourceStateByType(MyResourceDistributorComponent.ElectricityId) != MyResourceStateEnum.NoPower; if (!settings.HasFlag(MyTrashRemovalFlags.Powered) && isPowered) { return(MyTrashRemovalFlags.Powered); } if (!settings.HasFlag(MyTrashRemovalFlags.Controlled) && grid.GridSystems.ControlSystem.IsControlled) { return(MyTrashRemovalFlags.Controlled); } if (!settings.HasFlag(MyTrashRemovalFlags.WithProduction) && grid.GridSystems.ControlSystem.IsControlled) { return(MyTrashRemovalFlags.WithProduction); } } if (!settings.HasFlag(MyTrashRemovalFlags.WithMedBay) && grid.BlocksCounters.GetValueOrDefault(typeof(MyObjectBuilder_MedicalRoom)) > 0) { return(MyTrashRemovalFlags.WithMedBay); } if (IsCloseToPlayerOrCamera(grid, settings.PlayerDistanceThreshold)) { metric = settings.PlayerDistanceThreshold; return(MyTrashRemovalFlags.DistanceFromPlayer); } if (checkGroup && MyCubeGridGroups.Static.Physical != null) { var physicalGroup = MyCubeGridGroups.Static.Physical.GetGroup(grid); if (physicalGroup != null) { foreach (var cubeGrid in physicalGroup.Nodes) { if (cubeGrid.NodeData == null || cubeGrid.NodeData.Physics == null || cubeGrid.NodeData.Physics.Shape == null || cubeGrid.NodeData == grid) { continue; } var subGridReason = GetTrashState(cubeGrid.NodeData, settings, out metric, false); if (subGridReason != MyTrashRemovalFlags.None) { return(subGridReason); } } } } return(MyTrashRemovalFlags.None); }
public static float GetMetric(MyEntityCyclingOrder order, MyEntity entity) { MyCubeGrid grid = entity as MyCubeGrid; MyPhysicsComponentBase physics = entity.Physics; switch (order) { case MyEntityCyclingOrder.Characters: return((entity is MyCharacter) ? ((float)1) : ((float)0)); case MyEntityCyclingOrder.BiggestGrids: return((grid != null) ? ((float)grid.GetBlocks().Count) : ((float)0)); case MyEntityCyclingOrder.Fastest: if (physics == null) { return(0f); } return((float)Math.Round((double)physics.LinearVelocity.Length(), 2)); case MyEntityCyclingOrder.BiggestDistanceFromPlayers: if ((entity is MyVoxelBase) || (entity is MySafeZone)) { return(0f); } return(GetPlayerDistance(entity)); case MyEntityCyclingOrder.MostActiveDrills: return(GetActiveBlockCount <MyShipDrill>(grid, false)); case MyEntityCyclingOrder.MostActiveReactors: return(GetActiveBlockCount <MyReactor>(grid, false)); case MyEntityCyclingOrder.MostActiveProductionBuildings: return(GetActiveBlockCount <MyProductionBlock>(grid, false)); case MyEntityCyclingOrder.MostActiveSensors: return(GetActiveBlockCount <MySensorBlock>(grid, false)); case MyEntityCyclingOrder.MostActiveThrusters: return(GetActiveBlockCount <MyThrust>(grid, false)); case MyEntityCyclingOrder.MostWheels: return(GetActiveBlockCount <MyMotorSuspension>(grid, true)); case MyEntityCyclingOrder.StaticObjects: int num1; if (((entity.Physics == null) || (entity.Physics.IsPhantom || (entity.Physics.AngularVelocity.AbsMax() >= 0.05f))) || (entity.Physics.LinearVelocity.AbsMax() >= 0.05f)) { num1 = 0; } else { num1 = 1; } return((float)num1); case MyEntityCyclingOrder.FloatingObjects: return((entity is MyFloatingObject) ? ((float)1) : ((float)0)); case MyEntityCyclingOrder.Planets: return((entity is MyPlanet) ? ((float)1) : ((float)0)); case MyEntityCyclingOrder.OwnerLoginTime: return(GetOwnerLoginTime(grid)); } return(0f); }