static Matrix TransformOrientation(Matrix src, IMyMechanicalConnectionBlock connection, IMyCubeBlock reference = null) { //Echo($"\n\nTransform {src.Forward}"); if (connection != null) { //Echo($"Using connection {connection}"); var top = connection.Top; Matrix orientationTop; top.Orientation.GetMatrix(out orientationTop); orientationTop = Matrix.Invert(orientationTop); Matrix orientationConnection; connection.Orientation.GetMatrix(out orientationConnection); src *= orientationTop; src *= orientationConnection; } if (reference != null) { //Echo($"Using reference {reference}"); Matrix orientationReference; reference.Orientation.GetMatrix(out orientationReference); orientationReference = Matrix.Invert(orientationReference); src *= orientationReference; } return(src); }
static Vector3 TransformPosition(Vector3 src, IMyMechanicalConnectionBlock connection, IMyCubeBlock reference = null) { if (connection == null) { return(src); } var top = connection.Top; var pos = GetRelativePosition(src, top); Matrix rotationConnection; connection.Orientation.GetMatrix(out rotationConnection); //rotation = Matrix.Invert(rotation); var translation = reference != null ? connection.Position - reference.Position : connection.Position; rotationConnection.Translation = translation; pos = Vector3.Transform(pos, rotationConnection); if (reference != null) { pos = AlignTo(pos, reference); } return(pos); }
GridNode(IMyCubeGrid grid, IEnumerable <IMyMechanicalConnectionBlock> allConnections, GridNode parentNode) { this.CubeGrid = grid; this.ParentNode = parentNode; this.ConnectionsToParent = allConnections.Where(x => x.CubeGrid.EntityId == parentNode.CubeGrid.EntityId && x.TopGrid.EntityId == grid.EntityId).ToList(); this.ConnectionToParent = ConnectionsToParent.FirstOrDefault(); ChildNodes = GetChildGrids(CubeGrid, allConnections).Select(x => new GridNode(x, allConnections, this)).ToList(); }
private void OnSpawned(HashSet <IMyCubeGrid> grids) { onDone.Invoke(); Vector3D velocity = p.CubeGrid.Physics.LinearVelocity; Vector3D diff = Vector3D.Zero; bool first = true; HashSet <long> gridIds = new HashSet <long>(); foreach (IMyCubeGrid grid in grids) { grid.Physics.LinearVelocity = velocity; if (first) { diff = AccelerateTime(grid, velocity); } else { MatrixD temp = grid.WorldMatrix; temp.Translation += diff; grid.WorldMatrix = temp; } gridIds.Add(grid.EntityId); IMyEntity e = GridBounds.GetOverlappingEntity(grid); if (e != null) { Utilities.Notify(Utilities.GetOverlapString(true, e), Activator); ParallelSpawner.Close(grids); return; } if (grids.Count > 1) { var cubes = ((MyCubeGrid)grid).GetFatBlocks(); foreach (MyCubeBlock cube in cubes) { IMyMechanicalConnectionBlock baseBlock = cube as IMyMechanicalConnectionBlock; if (baseBlock != null) { baseBlock.Attach(); } } } } if (MyAPIGateway.Session.CreativeMode || comps.ConsumeComponents(Activator, Utilities.GetInventories(p))) { ParallelSpawner.Add(grids); } else { ParallelSpawner.Close(grids); } }
/// <summary> /// Called if <see cref="IMyMotorStator.Top" /> changed. /// </summary> /// <param name="base">The base on which the top is changed.</param> private void OnAttachedEntityChanged(IMyMechanicalConnectionBlock @base) { using (Mod.PROFILE ? Profiler.Measure(nameof(SmartRotorBase), nameof(OnAttachedEntityChanged)) : null) { using (Log.BeginMethod(nameof(OnAttachedEntityChanged))) { // hack: until IMyMotorStator.AttachedEntityChanged event is fixed. _lastAttachedState = @base.Top != null; MyAPIGateway.Parallel.Start(PlaceSmartHinge, PlaceSmartHingeCompleted, new PlaceSmartHingeData(Stator.Top)); } } }
public void SaveTorpedo() { IMyMechanicalConnectionBlock mech = Base as IMyMechanicalConnectionBlock; if (mech != null) { Base.CustomData = GridTerminalHelper.BlockListBytePosToBase64(PartsOfInterest, mech.Top); } else { Base.CustomData = GridTerminalHelper.BlockListBytePosToBase64(PartsOfInterest, Base); } }
/// <summary> /// Gets actions for blocks implementing IMyMechanicalConnectionBlock. /// </summary> public static void GetMechActions(IMyTerminalBlock tBlock, List <IScrollableAction> actions) { List <IMyTerminalAction> terminalActions = new List <IMyTerminalAction>(); IMyMechanicalConnectionBlock mechBlock = (IMyMechanicalConnectionBlock)tBlock; IMyPistonBase piston; IMyMotorStator rotor; mechBlock.GetActions(terminalActions); foreach (IMyTerminalAction tAction in terminalActions) // sketchy, but I've done worse already { string tActionName = tAction.Name.ToString(); if (tAction.Name.ToString().StartsWith("Add ")) { actions.Add(new BlockAction( () => tActionName, () => tAction.Apply(mechBlock))); } } actions.Add(new BlockAction( () => "Attach Head", () => mechBlock.Attach())); actions.Add(new BlockAction( () => mechBlock.IsAttached ? "Detach Head (Ready)" : "Detach Head", () => mechBlock.Detach())); piston = mechBlock as IMyPistonBase; if (piston != null) { actions.Add(new BlockAction( () => "Reverse", () => piston.Reverse())); } else { rotor = mechBlock as IMyMotorStator; if (rotor != null) { actions.Add(new BlockAction( () => "Reverse", () => rotor.TargetVelocityRad = -rotor.TargetVelocityRad)); } } }
public bool CollectParts(IMyTerminalBlock block) { IMyMechanicalConnectionBlock mech = Base as IMyMechanicalConnectionBlock; if (mech != null) { if (mech.TopGrid != null) { if (block.CubeGrid != mech.TopGrid) { return(false); } } else { Context.Me.GetSurface(0).ContentType = ContentType.TEXT_AND_IMAGE; Context.Me.GetSurface(0).FontSize = 10; Context.Me.GetSurface(0).FontColor = Color.Red; Context.Me.GetSurface(0).WriteText("ERR NO MECH TOP" + mech.CustomName); return(false); } } if (!Context.Me.IsSameConstructAs(block)) { return(false); } if (DummyTube.AddTorpedoPart(block)) { PartsOfInterest.Add(block); } if (block is IMyRadioAntenna) { PartsOfInterest.Add(block); } if (block.CustomName.Contains("<BASE>") && (block is IMyShipMergeBlock || block is IMyThrust)) { Base = block; } return(false); }
/// <summary> /// Called if <see cref="IMyMotorStator.Top" /> changed. /// </summary> /// <param name="base">The base on which the top is changed.</param> private void OnAttachedEntityChanged(IMyMechanicalConnectionBlock @base) { using (Log.BeginMethod(nameof(OnAttachedEntityChanged))) { Log.Debug($"START {nameof(OnAttachedEntityChanged)}"); if (@base.Top != null) { if (!IsHingeAttached) { MyAPIGateway.Parallel.Start(PlaceSmartHinge, PlaceSmartHingeCompleted, new PlaceSmartHingeData(Stator.Top)); } } else { IsHingeAttached = false; } Log.Debug($"END {nameof(OnAttachedEntityChanged)}"); } }
// return true if it is a new grid bool addToSolarMetagrid(IMyMechanicalConnectionBlock block) { IMyCubeGrid topMetaGridId = this.tmpSolarMetagridAssociations[block.TopGrid]; IMyCubeGrid baseMetaGridId; if (this.tmpSolarMetagridAssociations.TryGetValue(block.CubeGrid, out baseMetaGridId)) { if (baseMetaGridId != topMetaGridId) { this.mergeSolarMetagrids(baseMetaGridId, topMetaGridId); } return(false); } else { this.tmpSolarMetagridAssociations.Add(block.CubeGrid, topMetaGridId); return(true); } }
public void Main(string argument, UpdateType updateSource) { if (argument == "THRUSTERDETACH") { IMyTerminalBlock ThrusterControl = null; List <MyTuple <IMyThrust, int> > DetachThrusters = new List <MyTuple <IMyThrust, int> >(); List <IMyTerminalBlock> blocks = new List <IMyTerminalBlock>(); GridTerminalSystem.GetBlocksOfType <IMyTerminalBlock>(blocks, t => t.CubeGrid == Me.CubeGrid); foreach (var block in blocks) { if (block.CustomName.Contains("[TRPT]")) { ThrusterControl = block; } IMyThrust thrust = block as IMyThrust; if (thrust != null) { var tagindex = block.CustomName.IndexOf("[TRP"); if (tagindex == -1) { continue; } var indexTagEnd = block.CustomName.IndexOf(']', tagindex); if (indexTagEnd == -1) { continue; } int index; var numString = block.CustomName.Substring(tagindex + 4, indexTagEnd - tagindex - 4); if (!int.TryParse(numString, out index)) { continue; } DetachThrusters.Add(MyTuple.Create(thrust, index)); } } if (ThrusterControl == null) { Error("No [TRPT] tagged block for ThrusterDetachControl"); return; } MyIni IniParser = new MyIni(); IniParser.TryParse(ThrusterControl.CustomData); IniParser.DeleteSection("Torpedo"); StringBuilder thrusterData = new StringBuilder(); for (int i = 0; i < DetachThrusters.Count; ++i) { var position = GridTerminalHelper.BlockBytePosToBase64(DetachThrusters[i].Item1, ThrusterControl); thrusterData.Append(DetachThrusters[i].Item2 + "^" + position + ((i != DetachThrusters.Count - 1) ? "," : "")); } IniParser.Set("Torpedo", "ThrusterReleases", thrusterData.ToString()); ThrusterControl.CustomData = IniParser.ToString(); Info("Thruster Control: " + thrusterData.ToString()); return; } BuildProxyTubes(); foreach (var tube in ProxyTubes) { GetParts(tube); } if (argument == "LOAD") { List <IMyTerminalBlock> b = new List <IMyTerminalBlock>(); for (int i = 0; i < ProxyTubes.Count; ++i) { ProxyTube tube = ProxyTubes[i]; IMyMechanicalConnectionBlock mech = tube.Base as IMyMechanicalConnectionBlock; if (mech != null) { GridTerminalHelper.Base64BytePosToBlockList(tube.Base.CustomData, mech.Top, ref b); } else { GridTerminalHelper.Base64BytePosToBlockList(tube.Base.CustomData, tube.Base, ref b); } Echo("Tube" + i + ": " + b.Count().ToString()); } } else { StringBuilder builder = new StringBuilder(256); bool aok = true; for (int i = 0; i < ProxyTubes.Count; ++i) { ProxyTube tube = ProxyTubes[i]; string output; if (tube.CheckTorpedo(out output)) { tube.SaveTorpedo(); builder.AppendLine(i.ToString() + " [AOK] :"); builder.Append(output); } else { builder.AppendLine(i.ToString() + " [ERR] :"); builder.Append(output); aok = false; break; } } if (aok) { Info(builder.ToString()); } else { Error(builder.ToString()); } } ProxyTubes.Clear(); }
/// <summary> /// Called if <see cref="IMyMotorStator.Top" /> changed. /// </summary> /// <param name="base">The base on which the top is changed.</param> private void OnAttachedEntityChanged(IMyMechanicalConnectionBlock @base) { using (Log.BeginMethod(nameof(OnAttachedEntityChanged))) { Log.Debug($"START {nameof(OnAttachedEntityChanged)}"); if (@base.Top != null) { _topGrid = Stator.TopGrid; _topGridId = Stator.TopGrid.EntityId; _topGrid.OnBlockAdded += OnBlockAddedOnHead; _topGrid.OnBlockRemoved += OnBlockRemovedOnHead; _topGrid.OnClose += OnCloseHead; if (Mod.Static.Network == null || Mod.Static.Network.IsServer) { if (!IsPadAttached) { PlacePad(); } else { var blocks = new List <IMySlimBlock>(); Stator.TopGrid.GetBlocks(blocks); var pad = blocks.Where(x => x.BlockDefinition.Id.TypeId == typeof(MyObjectBuilder_LandingGear)).Select(x => x.FatBlock).FirstOrDefault(); if (pad != null) { Pad = (IMyLandingGear)pad; } } // todo: check if it is enough to run this on server. if (Mod.Static.DamageHandler != null) { EnableProtection(); } } else { if (Pad == null) { var blocks = new List <IMySlimBlock>(); Stator.TopGrid.GetBlocks(blocks); var pad = blocks.Where(x => x.BlockDefinition.Id.TypeId == typeof(MyObjectBuilder_LandingGear)).Select(x => x.FatBlock).FirstOrDefault(); if (pad != null) { Pad = (IMyLandingGear)pad; } } } } else { IsPadAttached = false; if (_topGrid != null) { _topGrid.OnBlockAdded -= OnBlockAddedOnHead; _topGrid.OnBlockRemoved -= OnBlockRemovedOnHead; _topGrid.OnClose -= OnCloseHead; _topGrid = null; } } Log.Debug($"END {nameof(OnAttachedEntityChanged)}"); } }
private IMyMechanicalConnectionBlock GetNextConnectionBlock(List <IMyMechanicalConnectionBlock> connectionBlocks, IMyMechanicalConnectionBlock currentBlock) { var targetGrid = currentBlock.TopGrid; for (int i = 0; i < connectionBlocks.Count; i++) { if (connectionBlocks[i].CubeGrid == targetGrid) { return(connectionBlocks[i]); } } return(null); }
private IMyMotorStator GetNextRotorBlock(List <IMyMechanicalConnectionBlock> connectionBlocks, IMyMechanicalConnectionBlock currentBlock) { var currentConnectionBlock = currentBlock; do { currentConnectionBlock = GetNextConnectionBlock(connectionBlocks, currentConnectionBlock); if (currentConnectionBlock is IMyMotorStator) { return(currentConnectionBlock as IMyMotorStator); } } while (currentConnectionBlock != null); return(null); }