/// <summary> /// Removes generated blocks in the given locations. /// </summary> protected void RemoveGeneratedBlock(MyStringId generatedBlockType, MyGeneratedBlockLocation[] locations) { if (locations == null) { return; } foreach (var location in locations) { RemoveBlock(location, null, generatedBlockType); { // Also try to postpone block remove to possible splits MyGeneratedBlockLocation locToRemove = location; locToRemove.GeneratedBlockType = generatedBlockType; m_removeLocationsForGridSplits.Add(locToRemove); } // Also process added locations if (m_addLocations.Count > 0) { // Check the same remove/add blocks m_addLocations.RemoveWhere(delegate(MyGeneratedBlockLocation loc) { return(MyGeneratedBlockLocation.IsSameGeneratedBlockLocation(loc, location, generatedBlockType)); }); } } }
public static bool IsSameGeneratedBlockLocation(MyGeneratedBlockLocation blockLocAdded, MyGeneratedBlockLocation blockLocRemoved) { bool result = blockLocAdded.BlockDefinition == blockLocRemoved.BlockDefinition; result = result && blockLocAdded.Position == blockLocRemoved.Position; result = result && blockLocAdded.Orientation == blockLocRemoved.Orientation; return result; }
public static bool IsSameGeneratedBlockLocation(MyGeneratedBlockLocation blockLocAdded, MyGeneratedBlockLocation blockLocRemoved, MyStringId generatedBlockType) { bool result = blockLocAdded.BlockDefinition.GeneratedBlockType == generatedBlockType; result = result && blockLocAdded.Position == blockLocRemoved.Position; result = result && blockLocAdded.Orientation == blockLocRemoved.Orientation; return(result); }
private void UpdateInternal() { Debug.Assert(MyFakes.ENABLE_GENERATED_BLOCKS); if (m_addLocations.Count > 0) { // Remove other grid blocks. m_addLocations.RemoveWhere(delegate(MyGeneratedBlockLocation loc) { return(loc.RefBlock != null && loc.RefBlock.CubeGrid != m_grid); }); // Check if the block can be placed to grid - must be before check the same remove/add m_addLocations.RemoveWhere(delegate(MyGeneratedBlockLocation loc) { return(!m_grid.CanAddCube(loc.Position, loc.Orientation, loc.BlockDefinition, ignoreSame: true)); }); // Check the same remove/add blocks m_addLocations.RemoveWhere(delegate(MyGeneratedBlockLocation loc) { MyGeneratedBlockLocation?remLocSame = null; foreach (var remLoc in m_removeLocations) { if (MyGeneratedBlockLocation.IsSameGeneratedBlockLocation(loc, remLoc)) { remLocSame = remLoc; break; } } if (remLocSame.HasValue) { m_removeLocations.Remove(remLocSame.Value); return(true); } return(false); }); } if (m_removeLocations.Count > 0) { RemoveBlocks(); } if (m_addLocations.Count > 0) { AddBlocks(); } m_addLocations.Clear(); m_removeLocations.Clear(); m_removeLocationsForGridSplits.Clear(); m_splitGridInfos.Clear(); }
private void ProcessChangedGrid(MyCubeGrid newGrid) { Vector3I gridOffset = Vector3I.Round((m_grid.PositionComp.GetPosition() - newGrid.PositionComp.GetPosition()) / m_grid.GridSize); Vector3 fw = (Vector3)Vector3D.TransformNormal(m_grid.WorldMatrix.Forward, newGrid.PositionComp.WorldMatrixNormalizedInv); Vector3 up = (Vector3)Vector3D.TransformNormal(m_grid.WorldMatrix.Up, newGrid.PositionComp.WorldMatrixNormalizedInv); Base6Directions.Direction fwDir = Base6Directions.GetClosestDirection(fw); Base6Directions.Direction upDir = Base6Directions.GetClosestDirection(up); if (upDir == fwDir) { upDir = Base6Directions.GetPerpendicular(fwDir); } MatrixI transform = new MatrixI(ref gridOffset, fwDir, upDir); MyGridInfo gridInfo = new MyGridInfo(); gridInfo.Grid = newGrid; gridInfo.Transform = transform; m_splitGridInfos.Add(gridInfo); // Remove from split grid if (m_removeLocationsForGridSplits.Count > 0) { List <int> indexesToRemove = new List <int>(); for (int i = 0; i < m_removeLocationsForGridSplits.Count; ++i) { MyGeneratedBlockLocation location = m_removeLocationsForGridSplits[i]; Debug.Assert(location.GeneratedBlockType != MyStringId.NullOrEmpty); RemoveBlock(location, gridInfo, location.GeneratedBlockType); } } // Add to split grid List <MySlimBlock> newGridBlocks = new List <MySlimBlock>(); m_addLocations.RemoveWhere(delegate(MyGeneratedBlockLocation loc) { if (loc.RefBlock != null && loc.RefBlock.CubeGrid == newGrid) { newGridBlocks.Add(loc.RefBlock); return(true); } return(false); }); foreach (var newGridBlock in newGridBlocks) { Debug.Assert(newGrid == newGridBlock.CubeGrid); newGridBlock.CubeGrid.AdditionalModelGenerators.ForEach(g => g.UpdateAfterGridSpawn(newGridBlock)); } }
public override bool Equals(object ob) { if (ob is MyGeneratedBlockLocation) { MyGeneratedBlockLocation other = (MyGeneratedBlockLocation)ob; return(IsSameGeneratedBlockLocation(this, other)); } else { return(false); } }
private void ProcessChangedGrid(MyCubeGrid newGrid) { Vector3I position = Vector3I.Round((this.m_grid.PositionComp.GetPosition() - newGrid.PositionComp.GetPosition()) / ((double)this.m_grid.GridSize)); Vector3 vec = (Vector3)Vector3D.TransformNormal(this.m_grid.WorldMatrix.Forward, newGrid.PositionComp.WorldMatrixNormalizedInv); Base6Directions.Direction closestDirection = Base6Directions.GetClosestDirection(vec); Base6Directions.Direction up = Base6Directions.GetClosestDirection((Vector3)Vector3D.TransformNormal(this.m_grid.WorldMatrix.Up, newGrid.PositionComp.WorldMatrixNormalizedInv)); if (up == closestDirection) { up = Base6Directions.GetPerpendicular(closestDirection); } MyGridInfo item = new MyGridInfo { Grid = newGrid, Transform = new MatrixI(ref position, closestDirection, up) }; this.m_splitGridInfos.Add(item); if (this.m_removeLocationsForGridSplits.Count > 0) { List <int> list1 = new List <int>(); for (int i = 0; i < this.m_removeLocationsForGridSplits.Count; i++) { MyGeneratedBlockLocation location = this.m_removeLocationsForGridSplits[i]; this.RemoveBlock(location, item, location.GeneratedBlockType); } } List <MySlimBlock> newGridBlocks = new List <MySlimBlock>(); this.m_addLocations.RemoveWhere(delegate(MyGeneratedBlockLocation loc) { if ((loc.RefBlock == null) || !ReferenceEquals(loc.RefBlock.CubeGrid, newGrid)) { return(false); } newGridBlocks.Add(loc.RefBlock); return(true); }); using (List <MySlimBlock> .Enumerator enumerator = newGridBlocks.GetEnumerator()) { while (enumerator.MoveNext()) { MySlimBlock newGridBlock; newGridBlock.CubeGrid.AdditionalModelGenerators.ForEach(g => g.UpdateAfterGridSpawn(newGridBlock)); } } }
private bool RemoveBlock(MyGeneratedBlockLocation location, MyGridInfo gridInfo, MyStringId generatedBlockType) { MySlimBlock slimBlock = null; if (gridInfo != null) { Vector3I position = Vector3I.Transform(location.Position, gridInfo.Transform); slimBlock = gridInfo.Grid.GetCubeBlock(position); } else { slimBlock = m_grid.GetCubeBlock(location.Position); } if (slimBlock != null) { if (slimBlock.FatBlock is MyCompoundCubeBlock) { MyCompoundCubeBlock compoundBlock = slimBlock.FatBlock as MyCompoundCubeBlock; var compoundBlocks = compoundBlock.GetBlocks(); for (int i = 0; i < compoundBlocks.Count; ++i) { MySlimBlock block = compoundBlocks[i]; Debug.Assert(block != null); if (block.BlockDefinition.IsGeneratedBlock && block.BlockDefinition.GeneratedBlockType == generatedBlockType && block.Orientation == location.Orientation) { ushort?id = compoundBlock.GetBlockId(block); m_removeLocations.Add(new MyGeneratedBlockLocation(null, block.BlockDefinition, block.Position, block.Orientation, blockIdInCompound: id, gridInfo: gridInfo)); return(true); } } } else if (slimBlock.BlockDefinition.IsGeneratedBlock && slimBlock.BlockDefinition.GeneratedBlockType == generatedBlockType && slimBlock.Orientation == location.Orientation) { m_removeLocations.Add(new MyGeneratedBlockLocation(null, slimBlock.BlockDefinition, slimBlock.Position, slimBlock.Orientation, gridInfo: gridInfo)); return(true); } } return(false); }
private bool RemoveBlock(MyGeneratedBlockLocation location, MyGridInfo gridInfo, MyStringId generatedBlockType) { MySlimBlock cubeBlock = null; if (gridInfo == null) { cubeBlock = this.m_grid.GetCubeBlock(location.Position); } else { Vector3I pos = Vector3I.Transform(location.Position, gridInfo.Transform); cubeBlock = gridInfo.Grid.GetCubeBlock(pos); } if (cubeBlock != null) { if (!(cubeBlock.FatBlock is MyCompoundCubeBlock)) { if ((cubeBlock.BlockDefinition.IsGeneratedBlock && (cubeBlock.BlockDefinition.GeneratedBlockType == generatedBlockType)) && (cubeBlock.Orientation == location.Orientation)) { ushort?blockIdInCompound = null; this.m_removeLocations.Add(new MyGeneratedBlockLocation(null, cubeBlock.BlockDefinition, cubeBlock.Position, cubeBlock.Orientation, blockIdInCompound, gridInfo)); return(true); } } else { MyCompoundCubeBlock fatBlock = cubeBlock.FatBlock as MyCompoundCubeBlock; ListReader <MySlimBlock> blocks = fatBlock.GetBlocks(); for (int i = 0; i < blocks.Count; i++) { MySlimBlock block3 = blocks[i]; if ((block3.BlockDefinition.IsGeneratedBlock && (block3.BlockDefinition.GeneratedBlockType == generatedBlockType)) && (block3.Orientation == location.Orientation)) { ushort?blockId = fatBlock.GetBlockId(block3); this.m_removeLocations.Add(new MyGeneratedBlockLocation(null, block3.BlockDefinition, block3.Position, block3.Orientation, blockId, gridInfo)); return(true); } } } } return(false); }
protected void RemoveGeneratedBlock(MyStringId generatedBlockType, List <MyGeneratedBlockLocation> locations) { if ((locations != null) && (locations.Count != 0)) { using (List <MyGeneratedBlockLocation> .Enumerator enumerator = locations.GetEnumerator()) { while (enumerator.MoveNext()) { MyGeneratedBlockLocation location; this.RemoveBlock(location, null, generatedBlockType); MyGeneratedBlockLocation item = location; item.GeneratedBlockType = generatedBlockType; this.m_removeLocationsForGridSplits.Add(item); if (this.m_addLocations.Count > 0) { this.m_addLocations.RemoveWhere(loc => MyGeneratedBlockLocation.IsSameGeneratedBlockLocation(loc, location, generatedBlockType)); } } } } }
private void UpdateInternal() { if (this.m_addLocations.Count > 0) { this.m_addLocations.RemoveWhere(loc => (loc.RefBlock != null) && !ReferenceEquals(loc.RefBlock.CubeGrid, this.m_grid)); this.m_addLocations.RemoveWhere(loc => !this.m_grid.CanAddCube(loc.Position, new MyBlockOrientation?(loc.Orientation), loc.BlockDefinition, true)); this.m_addLocations.RemoveWhere(delegate(MyGeneratedBlockLocation loc) { MyGeneratedBlockLocation?nullable = null; foreach (MyGeneratedBlockLocation location in this.m_removeLocations) { if (MyGeneratedBlockLocation.IsSameGeneratedBlockLocation(loc, location)) { nullable = new MyGeneratedBlockLocation?(location); break; } } if (nullable == null) { return(false); } this.m_removeLocations.Remove(nullable.Value); return(true); }); } if (this.m_removeLocations.Count > 0) { this.RemoveBlocks(true); } if (this.m_addLocations.Count > 0) { this.AddBlocks(); } this.m_addLocations.Clear(); this.m_removeLocations.Clear(); this.m_removeLocationsForGridSplits.Clear(); this.m_splitGridInfos.Clear(); }
public virtual void UpdateAfterSimulation() { Debug.Assert(MyFakes.ENABLE_GENERATED_BLOCKS); if (m_addLocations.Count > 0) { // Remove ather grid blocks. m_addLocations.RemoveWhere(delegate(MyGeneratedBlockLocation loc) { return(loc.RefBlock != null && loc.RefBlock.CubeGrid != m_grid); }); // Check the same remove/add blocks m_addLocations.RemoveWhere(delegate(MyGeneratedBlockLocation loc) { MyGeneratedBlockLocation?remLocSame = null; foreach (var remLoc in m_removeLocations) { if (MyGeneratedBlockLocation.IsSameGeneratedBlockLocation(loc, remLoc)) { remLocSame = remLoc; break; } } if (remLocSame.HasValue) { m_removeLocations.Remove(remLocSame.Value); return(true); } return(false); }); // Check if there is already placed the same block in the grid m_addLocations.RemoveWhere(delegate(MyGeneratedBlockLocation loc) { MySlimBlock existingBlock = m_grid.GetCubeBlock(loc.Position); if (existingBlock != null) { if (existingBlock.FatBlock is MyCompoundCubeBlock) { MyCompoundCubeBlock compoundBlock = existingBlock.FatBlock as MyCompoundCubeBlock; foreach (var blockInCompound in compoundBlock.GetBlocks()) { if (blockInCompound.BlockDefinition == loc.BlockDefinition && blockInCompound.Orientation == loc.Orientation) { return(true); } } } else { if (existingBlock.BlockDefinition == loc.BlockDefinition && existingBlock.Orientation == loc.Orientation) { return(true); } } } return(false); }); } if (m_removeLocations.Count > 0) { RemoveBlocks(); } if (m_addLocations.Count > 0) { AddBlocks(); } m_addLocations.Clear(); m_removeLocations.Clear(); m_removeLocationsForGridSplits.Clear(); m_splitGridInfos.Clear(); }
private bool RemoveBlock(MyGeneratedBlockLocation location, MyGridInfo gridInfo, MyStringId generatedBlockType) { MySlimBlock slimBlock = null; if (gridInfo != null) { Vector3I position = Vector3I.Transform(location.Position, gridInfo.Transform); slimBlock = gridInfo.Grid.GetCubeBlock(position); } else { slimBlock = m_grid.GetCubeBlock(location.Position); } if (slimBlock != null) { if (slimBlock.FatBlock is MyCompoundCubeBlock) { MyCompoundCubeBlock compoundBlock = slimBlock.FatBlock as MyCompoundCubeBlock; var compoundBlocks = compoundBlock.GetBlocks(); for (int i = 0; i < compoundBlocks.Count; ++i) { MySlimBlock block = compoundBlocks[i]; Debug.Assert(block != null); if (block.BlockDefinition.IsGeneratedBlock && block.BlockDefinition.GeneratedBlockType == generatedBlockType && block.Orientation == location.Orientation) { ushort? id = compoundBlock.GetBlockId(block); m_removeLocations.Add(new MyGeneratedBlockLocation(null, block.BlockDefinition, block.Position, block.Orientation, blockIdInCompound: id, gridInfo: gridInfo)); return true; } } } else if (slimBlock.BlockDefinition.IsGeneratedBlock && slimBlock.BlockDefinition.GeneratedBlockType == generatedBlockType && slimBlock.Orientation == location.Orientation) { m_removeLocations.Add(new MyGeneratedBlockLocation(null, slimBlock.BlockDefinition, slimBlock.Position, slimBlock.Orientation, gridInfo: gridInfo)); return true; } } return false; }
/// <summary> /// Removes generated blocks in the given locations. /// </summary> protected void RemoveGeneratedBlock(MyStringId generatedBlockType, MyGeneratedBlockLocation[] locations) { if (locations == null) return; foreach (var location in locations) { RemoveBlock(location, null, generatedBlockType); { // Also try to postpone block remove to possible splits MyGeneratedBlockLocation locToRemove = location; locToRemove.GeneratedBlockType = generatedBlockType; m_removeLocationsForGridSplits.Add(locToRemove); } // Also process added locations if (m_addLocations.Count > 0) { // Check the same remove/add blocks m_addLocations.RemoveWhere(delegate(MyGeneratedBlockLocation loc) { return MyGeneratedBlockLocation.IsSameGeneratedBlockLocation(loc, location, generatedBlockType); }); } } }