Example #1
0
    public void Prime(ZACommons commons, EventDriver eventDriver)
    {
        // Wake up batteries
        var batteryGroup = commons.GetBlockGroupWithName(BATTERY_GROUP + MISSILE_GROUP_SUFFIX);

        if (batteryGroup == null)
        {
            throw new Exception("Group missing: " + BATTERY_GROUP + MISSILE_GROUP_SUFFIX);
        }
        var systemsGroup = commons.GetBlockGroupWithName(SYSTEMS_GROUP + MISSILE_GROUP_SUFFIX);

        if (systemsGroup == null)
        {
            throw new Exception("Group missing: " + SYSTEMS_GROUP + MISSILE_GROUP_SUFFIX);
        }

        var batteries = ZACommons.GetBlocksOfType <IMyBatteryBlock>(batteryGroup.Blocks);

        batteries.ForEach(battery =>
        {
            battery.SetValue <bool>("OnOff", true);
            battery.SetValue <bool>("Recharge", false);
            battery.SetValue <bool>("Discharge", true);
        });

        // Activate flight systems
        ZACommons.EnableBlocks(systemsGroup.Blocks, true);

        eventDriver.Schedule(1.0, Release);
    }
    public void Prime(ZACommons commons, EventDriver eventDriver)
    {
        var relayBatteries = commons.GetBlockGroupWithName(BATTERY_GROUP);

        if (relayBatteries == null)
        {
            throw new Exception("Missing group: " + BATTERY_GROUP);
        }
        var relaySystems = commons.GetBlockGroupWithName(SYSTEMS_GROUP);

        if (relaySystems == null)
        {
            throw new Exception("Missing group: " + SYSTEMS_GROUP);
        }

        // Wake up batteries
        var batteries = ZACommons.GetBlocksOfType <IMyBatteryBlock>(relayBatteries.Blocks);

        ZACommons.EnableBlocks(batteries, true);
        ZACommons.SetBatteryRecharge(batteries, false);
        // And activate flight systems
        ZACommons.EnableBlocks(relaySystems.Blocks, true);

        eventDriver.Schedule(1.0, Release);
    }
Example #3
0
    public void Prime(ZACommons commons, EventDriver eventDriver)
    {
        // Wake up batteries
        var batteryGroup = commons.GetBlockGroupWithName(BATTERY_GROUP + MISSILE_GROUP_SUFFIX);
        if (batteryGroup == null)
        {
            throw new Exception("Group missing: " + BATTERY_GROUP + MISSILE_GROUP_SUFFIX);
        }
        var systemsGroup = commons.GetBlockGroupWithName(SYSTEMS_GROUP + MISSILE_GROUP_SUFFIX);
        if (systemsGroup == null)
        {
            throw new Exception("Group missing: " + SYSTEMS_GROUP + MISSILE_GROUP_SUFFIX);
        }

        var batteries = ZACommons.GetBlocksOfType<IMyBatteryBlock>(batteryGroup.Blocks);
        batteries.ForEach(battery =>
                {
                    battery.SetValue<bool>("OnOff", true);
                    battery.SetValue<bool>("Recharge", false);
                    battery.SetValue<bool>("Discharge", true);
                });

        // Activate flight systems
        ZACommons.EnableBlocks(systemsGroup.Blocks, true);

        eventDriver.Schedule(1.0, Release);
    }
    public void Prime(ZACommons commons, EventDriver eventDriver)
    {
        // Wake up batteries
        var batteryGroup = commons.GetBlockGroupWithName(StandardMissile.BATTERY_GROUP + MissileGroupSuffix);

        if (batteryGroup == null)
        {
            throw new Exception("Group missing: " + StandardMissile.BATTERY_GROUP + MissileGroupSuffix);
        }
        var systemsGroup = commons.GetBlockGroupWithName(StandardMissile.SYSTEMS_GROUP + MissileGroupSuffix);

        if (systemsGroup == null)
        {
            throw new Exception("Group missing: " + StandardMissile.SYSTEMS_GROUP + MissileGroupSuffix);
        }

        var batteries = ZACommons.GetBlocksOfType <IMyBatteryBlock>(batteryGroup.Blocks);

        batteries.ForEach(battery =>
        {
            battery.Enabled       = true;
            battery.OnlyDischarge = true;
        });

        // Activate flight systems
        ZACommons.EnableBlocks(systemsGroup.Blocks, true);

        eventDriver.Schedule(0.1 + ReleaseDelay, Release);
    }
    public void Release(ZACommons commons, EventDriver eventDriver)
    {
        // Enable mass
        var group = commons.GetBlockGroupWithName(StandardMissile.MASS_GROUP + MissileGroupSuffix);

        if (group != null)
        {
            ZACommons.EnableBlocks(group.Blocks, true);
        }

        var releaseGroup = commons.GetBlockGroupWithName(StandardMissile.RELEASE_GROUP + MissileGroupSuffix);

        if (releaseGroup == null)
        {
            throw new Exception("Group missing: " + StandardMissile.RELEASE_GROUP + MissileGroupSuffix);
        }

        // Unlock any landing gear
        ZACommons.ForEachBlockOfType <IMyLandingGear>(releaseGroup.Blocks,
                                                      gear => gear.ApplyAction("Unlock"));
        // And then turn everything off (connectors, merge blocks, etc)
        ZACommons.EnableBlocks(releaseGroup.Blocks, false);

        // Initialize flight control
        var shipControl = (ShipControlCommons)commons;

        shipControl.Reset(gyroOverride: true, thrusterEnable: null);

        eventDriver.Schedule(0.1, Demass);
    }
    private void BroadcastMessage(ZACommons commons, string message)
    {
        var updateGroup = commons.GetBlockGroupWithName(TARGET_UPDATE_GROUP);

        if (updateGroup != null)
        {
            var broadcasted = false;
            foreach (var block in updateGroup.Blocks)
            {
                if (block is IMyLaserAntenna)
                {
                    ((IMyLaserAntenna)block).TransmitMessage(message);
                }
                else if (!broadcasted && block is IMyRadioAntenna)
                {
                    // Only if functional and enabled
                    var antenna = (IMyRadioAntenna)block;
                    if (antenna.IsFunctional && antenna.Enabled)
                    {
                        antenna.TransmitMessage(message, TRACKER_ANTENNA_TARGET);
                        broadcasted = true;
                    }
                }
            }
        }
    }
    private void GetShellParameters(ZACommons commons)
    {
        // Some defaults
        ShellSpeed     = 100.0;
        ShellFireDelay = ShellOffset = 0.0;

        var group = commons.GetBlockGroupWithName(FC_FIRE_GROUP);

        if (group != null)
        {
            foreach (var block in group.Blocks)
            {
                if (block is IMyProgrammableBlock)
                {
                    var shellCustomData = new ZACustomData();
                    shellCustomData.Parse(block);
                    ShellSpeed     = shellCustomData.GetDouble("speed");
                    ShellFireDelay = shellCustomData.GetDouble("delay");
                    ShellOffset    = shellCustomData.GetDouble("offset");
                    // Only bother with the 1st one for now
                    return;
                }
            }
        }
    }
Example #8
0
    public void AcquireTarget(ZACommons commons)
    {
        // Find the sole text panel
        var panelGroup = commons.GetBlockGroupWithName("CM Target");
        if (panelGroup == null)
        {
            throw new Exception("Missing group: CM Target");
        }

        var panels = ZACommons.GetBlocksOfType<IMyTextPanel>(panelGroup.Blocks);
        if (panels.Count == 0)
        {
            throw new Exception("Expecting at least 1 text panel");
        }
        var panel = panels[0] as IMyTextPanel; // Just use the first one
        var targetString = panel.GetPublicText();

        // Parse target info
        var parts = targetString.Split(';');
        if (parts.Length != 3)
        {
            throw new Exception("Expecting exactly 3 parts to target info");
        }
        Target = new Vector3D();
        for (int i = 0; i < 3; i++)
        {
            Target.SetDim(i, double.Parse(parts[i]));
        }
    }
Example #9
0
    public void AcquireTarget(ZACommons commons)
    {
        // Find the sole text panel
        var panelGroup = commons.GetBlockGroupWithName("CM Target");

        if (panelGroup == null)
        {
            throw new Exception("Missing group: CM Target");
        }

        var panels = ZACommons.GetBlocksOfType <IMyTextPanel>(panelGroup.Blocks);

        if (panels.Count == 0)
        {
            throw new Exception("Expecting at least 1 text panel");
        }
        var panel        = panels[0] as IMyTextPanel; // Just use the first one
        var targetString = panel.GetPublicText();

        // Parse target info
        var parts = targetString.Split(';');

        if (parts.Length != 3)
        {
            throw new Exception("Expecting exactly 3 parts to target info");
        }
        Target = new Vector3D();
        for (int i = 0; i < 3; i++)
        {
            Target.SetDim(i, double.Parse(parts[i]));
        }
    }
Example #10
0
    public void Demass(ZACommons commons, EventDriver eventDriver)
    {
        var shipControl = (ShipControlCommons)commons;

        var deltaTime            = (eventDriver.TimeSinceStart - InitialTime).TotalSeconds;
        var launcherDelta        = LauncherVelocity * deltaTime;
        var distanceFromLauncher = (shipControl.ReferencePoint -
                                    (InitialPosition + launcherDelta)).LengthSquared();

        if (distanceFromLauncher < DemassDistance * DemassDistance)
        {
            // Not yet
            eventDriver.Schedule(TicksPerRun, Demass);
            return;
        }

        // Disable mass
        var group = commons.GetBlockGroupWithName(MASS_GROUP);

        if (group != null)
        {
            ZACommons.EnableBlocks(group.Blocks, false);
        }

        // Start roll
        shipControl.GyroControl.EnableOverride(true);
        shipControl.GyroControl.SetAxisVelocity(GyroControl.Roll,
                                                MathHelper.Pi);

        // All done
        if (PostLaunch != null)
        {
            PostLaunch(commons, eventDriver);
        }
    }
Example #11
0
    public void Run(ZACommons commons, EventDriver eventDriver)
    {
        // Bleah, might be merged, so use a group
        var payloadGroup = commons.GetBlockGroupWithName(PAYLOAD_GROUP + MISSILE_GROUP_SUFFIX);

        if (payloadGroup == null)
        {
            return;
        }
        var containers = ZACommons.GetBlocksOfType <IMyCargoContainer>(payloadGroup.Blocks);

        if (containers.Count == 0)
        {
            return;
        }

        // Leisurely pace of one stack per container per frame
        bool moved = false;

        for (var e = containers.GetEnumerator(); e.MoveNext();)
        {
            var container = e.Current;
            if (SplitContainerContents(container))
            {
                moved = true;
            }
        }

        if (moved)
        {
            eventDriver.Schedule(1, Run);
        }
    }
Example #12
0
    // Acquire target from CM Target text panel. If anything's wrong,
    // return null.
    private Vector3D? AcquireTarget(ZACommons commons)
    {
        // Find the sole text panel
        var panelGroup = commons.GetBlockGroupWithName("CM Target");
        if (panelGroup == null) return null;

        var panels = ZACommons.GetBlocksOfType<IMyTextPanel>(panelGroup.Blocks);
        if (panels.Count == 0) return null;
        var panel = panels[0] as IMyTextPanel; // Just use the first one
        var targetString = panel.GetPublicText();

        // Parse target info
        var parts = targetString.Split(';');
        if (parts.Length != 3) return null;

        var target = new Vector3D();
        for (int i = 0; i < 3; i++)
        {
            double coord;
            if (!double.TryParse(parts[i], out coord)) return null;
            target.SetDim(i, coord);
        }

        return target;
    }
Example #13
0
 private List<IMyTerminalBlock> GetSequenceBlocks(ZACommons commons,
                                                  string sequence)
 {
     var groupName = SEQUENCER_PREFIX + sequence;
     var group = commons.GetBlockGroupWithName(groupName);
     if (group == null || group.Blocks.Count == 0) return null;
     return group.Blocks;
 }
Example #14
0
    public void Release(ZACommons commons, EventDriver eventDriver)
    {
        var relayRelease = commons.GetBlockGroupWithName(RELEASE_GROUP);
        if (relayRelease == null)
        {
            throw new Exception("Missing group: " + RELEASE_GROUP);
        }
        ZACommons.EnableBlocks(relayRelease.Blocks, false);

        eventDriver.Schedule(1.0, Burn);
    }
    public void Release(ZACommons commons, EventDriver eventDriver)
    {
        var relayRelease = commons.GetBlockGroupWithName(RELEASE_GROUP);

        if (relayRelease == null)
        {
            throw new Exception("Missing group: " + RELEASE_GROUP);
        }
        ZACommons.EnableBlocks(relayRelease.Blocks, false);

        eventDriver.Schedule(1.0, Burn);
    }
Example #16
0
    private List <IMyTerminalBlock> GetSequenceBlocks(ZACommons commons,
                                                      string sequence)
    {
        var groupName = SEQUENCER_PREFIX + sequence;
        var group     = commons.GetBlockGroupWithName(groupName);

        if (group == null || group.Blocks.Count == 0)
        {
            return(null);
        }
        return(group.Blocks);
    }
Example #17
0
    public void Prime(ZACommons commons, EventDriver eventDriver)
    {
        var relayBatteries = commons.GetBlockGroupWithName(BATTERY_GROUP);
        if (relayBatteries == null)
        {
            throw new Exception("Missing group: " + BATTERY_GROUP);
        }
        var relaySystems = commons.GetBlockGroupWithName(SYSTEMS_GROUP);
        if (relaySystems == null)
        {
            throw new Exception("Missing group: " + SYSTEMS_GROUP);
        }

        // Wake up batteries
        var batteries = ZACommons.GetBlocksOfType<IMyBatteryBlock>(relayBatteries.Blocks);
        ZACommons.EnableBlocks(batteries, true);
        ZACommons.SetBatteryRecharge(batteries, false);
        // And activate flight systems
        ZACommons.EnableBlocks(relaySystems.Blocks, true);

        eventDriver.Schedule(1.0, Release);
    }
Example #18
0
    private void KickTimer(ZACommons commons)
    {
        IMyTimerBlock timer = null;

        // Name takes priority over group
        if (TimerName != null)
        {
            var blocks = ZACommons.SearchBlocksOfName(commons.Blocks, TimerName,
                                                      block => block is IMyTimerBlock &&
                                                      ((IMyTimerBlock)block).Enabled);
            if (blocks.Count > 0)
            {
                timer = blocks[0] as IMyTimerBlock;
            }
        }
        if (timer == null && TimerGroup != null)
        {
            var group = commons.GetBlockGroupWithName(TimerGroup);
            if (group != null)
            {
                var blocks = ZACommons.GetBlocksOfType <IMyTimerBlock>(group.Blocks,
                                                                       block => block.CubeGrid == commons.Me.CubeGrid &&
                                                                       ((IMyTimerBlock)block).Enabled);
                timer = blocks.Count > 0 ? (IMyTimerBlock)blocks[0] : null;
            }
        }

        if (timer != null)
        {
            // Rules are simple. If we have something in the tick queue, trigger now.
            // Otherwise, set timer delay appropriately (minimum 1 second) and kick.
            // If you want sub-second accuracy, always use ticks.
            if (TickQueue.First != null)
            {
                timer.ApplyAction("TriggerNow");
            }
            else if (TimeQueue.First != null)
            {
                var next = (float)(TimeQueue.First.Value.When.TotalSeconds - TimeSinceStart.TotalSeconds);
                // Constrain appropriately (not sure if this will be done for us or if it
                // will just throw). Just do it to be safe.
                next = Math.Max(next, timer.GetMinimum <float>("TriggerDelay"));
                next = Math.Min(next, timer.GetMaximum <float>("TriggerDelay"));

                timer.SetValue <float>("TriggerDelay", next);
                timer.ApplyAction("Start");
            }
            // NB If both queues are empty, we stop running
        }
    }
Example #19
0
 private IMyRemoteControl GetRemoteControl(ZACommons commons)
 {
     var remoteGroup = commons.GetBlockGroupWithName(REMOTE_GROUP);
     if (remoteGroup == null)
     {
         throw new Exception("Missing group: " + REMOTE_GROUP);
     }
     var remotes = ZACommons.GetBlocksOfType<IMyRemoteControl>(remoteGroup.Blocks);
     if (remotes.Count != 1)
     {
         throw new Exception("Expecting exactly 1 remote control");
     }
     return (IMyRemoteControl)remotes[0];
 }
Example #20
0
 private IMyCubeBlock GetReference(ZACommons commons, string groupName)
 {
     var group = commons.GetBlockGroupWithName(groupName);
     if (group == null)
     {
         throw new Exception("Missing group: " + groupName);
     }
     var controllers = ZACommons.GetBlocksOfType<IMyShipController>(group.Blocks);
     if (controllers.Count == 0)
     {
         throw new Exception("Expecting at least 1 ship controller in " + groupName);
     }
     return controllers[0];
 }
Example #21
0
    private void TargetAction(ZACommons commons, Vector3D target)
    {
        var targetGroup = commons.GetBlockGroupWithName(RANGEFINDER_TARGET_GROUP);
        if (targetGroup != null)
        {
            var targetString = string.Format(RANGEFINDER_TARGET_FORMAT,
                                             target.GetDim(0),
                                             target.GetDim(1),
                                             target.GetDim(2));

            ZACommons.GetBlocksOfType<IMyTextPanel>(targetGroup.Blocks).ForEach(block => {
                    ((IMyTextPanel)block).WritePublicText(targetString);
                });
        }
    }
Example #22
0
    public void Release(ZACommons commons, EventDriver eventDriver)
    {
        // Enable mass
        var group = commons.GetBlockGroupWithName(MASS_GROUP);

        if (group != null)
        {
            ZACommons.EnableBlocks(group.Blocks, true);
        }

        var releaseGroup = commons.GetBlockGroupWithName(RELEASE_GROUP);

        if (releaseGroup == null)
        {
            throw new Exception("Group missing: " + RELEASE_GROUP);
        }

        // Get one last reading from launcher and determine velocity
        var launcherDelta = ((ShipControlCommons)commons).ReferencePoint -
                            InitialPosition;
        var deltaTime = (eventDriver.TimeSinceStart - InitialTime).TotalSeconds;

        LauncherVelocity = launcherDelta / deltaTime;

        // Turn release group off
        ZACommons.EnableBlocks(releaseGroup.Blocks, false);

        // Statistics
        AccelStartTime     = eventDriver.TimeSinceStart;
        AccelStartPosition = commons.Me.GetPosition();
        AccelLastPosition  = AccelStartPosition;
        AccelResults.Append(string.Format("CoM Distance: {0:F2} m\n", (AccelStartPosition - ((ShipControlCommons)commons).ReferencePoint).Length()));
        eventDriver.Schedule(TicksPerRun, AccelCheck);

        eventDriver.Schedule(1.0, Demass);
    }
Example #23
0
    public void Release(ZACommons commons, EventDriver eventDriver)
    {
        var releaseGroup = commons.GetBlockGroupWithName(RELEASE_GROUP + MISSILE_GROUP_SUFFIX);
        if (releaseGroup == null)
        {
            throw new Exception("Group missing: " + RELEASE_GROUP + MISSILE_GROUP_SUFFIX);
        }

        // Unlock any landing gear
        ZACommons.ForEachBlockOfType<IMyLandingGear>(releaseGroup.Blocks,
                                                     gear => gear.ApplyAction("Unlock"));
        // And then turn everything off (connectors, merge blocks, etc)
        ZACommons.EnableBlocks(releaseGroup.Blocks, false);

        eventDriver.Schedule(1.0, Burn);
    }
Example #24
0
    private void TargetAction(ZACommons commons, Vector3D target)
    {
        var targetGroup = commons.GetBlockGroupWithName(RANGEFINDER_TARGET_GROUP);

        if (targetGroup != null)
        {
            var targetString = string.Format(RANGEFINDER_TARGET_FORMAT,
                                             target.GetDim(0),
                                             target.GetDim(1),
                                             target.GetDim(2));

            ZACommons.GetBlocksOfType <IMyTextPanel>(targetGroup.Blocks).ForEach(block => {
                ((IMyTextPanel)block).WritePublicText(targetString);
            });
        }
    }
    private IMyRemoteControl GetRemoteControl(ZACommons commons)
    {
        var remoteGroup = commons.GetBlockGroupWithName(REMOTE_GROUP);

        if (remoteGroup == null)
        {
            throw new Exception("Missing group: " + REMOTE_GROUP);
        }
        var remotes = ZACommons.GetBlocksOfType <IMyRemoteControl>(remoteGroup.Blocks);

        if (remotes.Count != 1)
        {
            throw new Exception("Expecting exactly 1 remote control");
        }
        return(remotes[0]);
    }
Example #26
0
    private IMyCubeBlock GetReference(ZACommons commons, string groupName)
    {
        var group = commons.GetBlockGroupWithName(groupName);

        if (group == null)
        {
            throw new Exception("Missing group: " + groupName);
        }
        var controllers = ZACommons.GetBlocksOfType <IMyShipController>(group.Blocks);

        if (controllers.Count == 0)
        {
            throw new Exception("Expecting at least 1 ship controller in " + groupName);
        }
        return(controllers[0]);
    }
    private IMyLargeTurretBase GetTurret(ZACommons commons)
    {
        var group = commons.GetBlockGroupWithName(TURRET_TRACKER_TURRET_GROUP);

        if (group == null)
        {
            throw new Exception("Missing group: " + TURRET_TRACKER_TURRET_GROUP);
        }
        var turrets = ZACommons.GetBlocksOfType <IMyLargeTurretBase>(group.Blocks, turret => turret.IsFunctional && turret.Enabled);

        if (turrets.Count < 1)
        {
            throw new Exception("Missing turret in group " + TURRET_TRACKER_TURRET_GROUP);
        }
        return(turrets[0]);
    }
Example #28
0
    private IMyMotorStator GetRotor(ZACommons commons)
    {
        var rotorGroup = commons.GetBlockGroupWithName(RotorGroupName);

        if (rotorGroup == null)
        {
            throw new Exception("Missing group: " + RotorGroupName);
        }
        var rotors = ZACommons.GetBlocksOfType <IMyMotorStator>(rotorGroup.Blocks);

        if (rotors.Count != 1)
        {
            throw new Exception("Expecting exactly 1 rotor in " + RotorGroupName);
        }

        return((IMyMotorStator)rotors[0]);
    }
    public void Release(ZACommons commons, EventDriver eventDriver)
    {
        var releaseGroup = commons.GetBlockGroupWithName(StandardMissile.RELEASE_GROUP + MissileGroupSuffix);

        if (releaseGroup == null)
        {
            throw new Exception("Group missing: " + StandardMissile.RELEASE_GROUP + MissileGroupSuffix);
        }

        // Unlock any landing gear
        ZACommons.ForEachBlockOfType <IMyLandingGear>(releaseGroup.Blocks,
                                                      gear => gear.ApplyAction("Unlock"));
        // And then turn everything off (connectors, merge blocks, etc)
        ZACommons.EnableBlocks(releaseGroup.Blocks, false);

        eventDriver.Schedule(0.5, Burn);
    }
Example #30
0
    private IMyPistonBase GetPiston(ZACommons commons)
    {
        var pistonGroup = commons.GetBlockGroupWithName(PistonGroupName);

        if (pistonGroup == null)
        {
            throw new Exception("Missing group: " + PistonGroupName);
        }
        var pistons = ZACommons.GetBlocksOfType <IMyPistonBase>(pistonGroup.Blocks);

        if (pistons.Count != 1)
        {
            throw new Exception("Expecting exactly 1 piston in " + PistonGroupName);
        }

        return(pistons[0]);
    }
Example #31
0
    public void Run(ZACommons commons, EventDriver eventDriver)
    {
        // Bleah, might be merged, so use a group
        var payloadGroup = commons.GetBlockGroupWithName(PAYLOAD_GROUP + MISSILE_GROUP_SUFFIX);
        if (payloadGroup == null) return;
        var containers = ZACommons.GetBlocksOfType<IMyCargoContainer>(payloadGroup.Blocks);
        if (containers.Count == 0) return;

        // Leisurely pace of one stack per container per frame
        bool moved = false;
        for (var e = containers.GetEnumerator(); e.MoveNext();)
        {
            var container = e.Current;
            if (SplitContainerContents(container)) moved = true;
        }

        if (moved) eventDriver.Schedule(1, Run);
    }
Example #32
0
    private void LaunchDecoy(ZACommons commons, EventDriver eventDriver,
                             string suffix)
    {
        var group = commons.GetBlockGroupWithName("SS Decoy Set" + suffix);

        // Activate decoys
        ZACommons.ForEachBlockOfType <IMyDecoy>(group.Blocks,
                                                block => block.Enabled = true);
        eventDriver.Schedule(DecoyReleaseDelay, (c, e) => {
            var g = c.GetBlockGroupWithName("SS Decoy Set" + suffix);
            // Deactivate merge block
            ZACommons.ForEachBlockOfType <IMyShipMergeBlock>(g.Blocks,
                                                             merge =>
            {
                merge.Enabled = false;
            });
        });
    }
Example #33
0
    public void Release(ZACommons commons, EventDriver eventDriver)
    {
        var releaseGroup = commons.GetBlockGroupWithName(RELEASE_GROUP);
        if (releaseGroup == null)
        {
            throw new Exception("Group missing: " + RELEASE_GROUP);
        }

        // Get one last reading from launcher and determine velocity
        var launcherDelta = ((ShipControlCommons)commons).ReferencePoint -
            InitialPosition;
        var deltaTime = (eventDriver.TimeSinceStart - InitialTime).TotalSeconds;
        LauncherVelocity = launcherDelta / deltaTime;

        // Turn release group off
        ZACommons.EnableBlocks(releaseGroup.Blocks, false);

        eventDriver.Schedule(1.0, Demass);
    }
Example #34
0
    private void PostFeedback(ZACommons commons, string groupName)
    {
        var group = commons.GetBlockGroupWithName(groupName);

        if (group != null)
        {
            foreach (var block in group.Blocks)
            {
                if (block is IMyTimerBlock)
                {
                    ((IMyTimerBlock)block).Trigger();
                }
                else if (block is IMySoundBlock)
                {
                    ((IMySoundBlock)block).Play();
                }
            }
        }
    }
    // Camera stuff

    protected void InitCamera(ZACommons commons, EventDriver eventDriver)
    {
        var systemsGroup = commons.GetBlockGroupWithName(StandardMissile.SYSTEMS_GROUP + MissileGroupSuffix);

        if (systemsGroup == null)
        {
            return;                       // Kinda weird, but don't fret about it here
        }
        // Just grab first camera from group, if any
        // TODO support multiple cameras for more frequent scans
        var cameras = ZACommons.GetBlocksOfType <IMyCameraBlock>(systemsGroup.Blocks, camera => camera.IsFunctional && camera.Enabled);

        if (cameras.Count > 0)
        {
            LocalCamera = cameras[0];
            LocalCamera.EnableRaycast = true;
            eventDriver.Schedule(1, LocalCameraScan);
        }
    }
Example #36
0
 public IMyTerminalBlock SetLauncherReference(ZACommons commons, string groupName,
                                              Base6Directions.Direction direction = Base6Directions.Direction.Forward,
                                              Func<IMyTerminalBlock, bool> condition = null)
 {
     var group = commons.GetBlockGroupWithName(groupName);
     if (group != null)
     {
         for (var e = group.Blocks.GetEnumerator(); e.MoveNext();)
         {
             var block = e.Current;
             if (condition == null || condition(block))
             {
                 // Use first block that matches condition
                 SetLauncherReference(block, direction);
                 return block;
             }
         }
     }
     throw new Exception("Cannot set launcher reference from group: " + groupName);
 }
Example #37
0
    public IMyTerminalBlock SetLauncherReference(ZACommons commons, string groupName,
                                                 Base6Directions.Direction direction     = Base6Directions.Direction.Forward,
                                                 Func <IMyTerminalBlock, bool> condition = null)
    {
        var group = commons.GetBlockGroupWithName(groupName);

        if (group != null)
        {
            foreach (var block in group.Blocks)
            {
                if (condition == null || condition(block))
                {
                    // Use first block that matches condition
                    SetLauncherReference(block, direction);
                    return(block);
                }
            }
        }
        throw new Exception("Cannot set launcher reference from group: " + groupName);
    }
    private IMyCameraBlock GetSightingCamera(ZACommons commons)
    {
        var group = commons.GetBlockGroupWithName(FC_MAIN_CAMERA_GROUP);

        if (group == null)
        {
            return(null);
        }
        if (group.Blocks.Count != 1)
        {
            return(null);
        }
        var camera = group.Blocks[0] as IMyCameraBlock;

        if (camera == null)
        {
            return(null);
        }
        return(camera);
    }
Example #39
0
    private IMyCameraBlock GetMainCamera(ZACommons commons)
    {
        var group = commons.GetBlockGroupWithName(MAIN_CAMERA_GROUP);

        if (group == null)
        {
            throw new Exception("Group missing: " + MAIN_CAMERA_GROUP);
        }
        if (group.Blocks.Count != 1)
        {
            throw new Exception("Expecting exactly 1 block in group " + MAIN_CAMERA_GROUP);
        }
        var camera = group.Blocks[0] as IMyCameraBlock;

        if (camera == null)
        {
            throw new Exception("Expecting camera in group " + MAIN_CAMERA_GROUP);
        }
        return(camera);
    }
Example #40
0
    // Acquire target from CM Target text panel. If anything's wrong,
    // return null.
    private Vector3D?AcquireTarget(ZACommons commons)
    {
        // Find the sole text panel
        var panelGroup = commons.GetBlockGroupWithName("CM Target");

        if (panelGroup == null)
        {
            return(null);
        }

        var panels = ZACommons.GetBlocksOfType <IMyTextPanel>(panelGroup.Blocks);

        if (panels.Count == 0)
        {
            return(null);
        }
        var panel        = panels[0] as IMyTextPanel; // Just use the first one
        var targetString = panel.GetPublicText();

        // Parse target info
        var parts = targetString.Split(';');

        if (parts.Length != 3)
        {
            return(null);
        }

        var target = new Vector3D();

        for (int i = 0; i < 3; i++)
        {
            double coord;
            if (!double.TryParse(parts[i], out coord))
            {
                return(null);
            }
            target.SetDim(i, coord);
        }

        return(target);
    }
    // Use orientation of a block in the given group
    public void SetShipReference(ZACommons commons, string groupName,
                                 Func <IMyTerminalBlock, bool> condition = null)
    {
        var group = commons.GetBlockGroupWithName(groupName);

        if (group != null)
        {
            foreach (var block in group.Blocks)
            {
                if (block.CubeGrid == commons.Me.CubeGrid &&
                    (condition == null || condition(block)))
                {
                    SetShipReference(block);
                    return;
                }
            }
        }
        // Default to grid up/forward
        ShipUp      = Base6Directions.Direction.Up;
        ShipForward = Base6Directions.Direction.Forward;
    }
Example #42
0
    public void Release(ZACommons commons, EventDriver eventDriver)
    {
        var releaseGroup = commons.GetBlockGroupWithName(RELEASE_GROUP);

        if (releaseGroup == null)
        {
            throw new Exception("Group missing: " + RELEASE_GROUP);
        }

        // Get one last reading from launcher and determine velocity
        var launcherDelta = ((ShipControlCommons)commons).ReferencePoint -
                            InitialPosition;
        var deltaTime = (eventDriver.TimeSinceStart - InitialTime).TotalSeconds;

        LauncherVelocity = launcherDelta / deltaTime;

        // Turn release group off
        ZACommons.EnableBlocks(releaseGroup.Blocks, false);

        eventDriver.Schedule(1.0, Demass);
    }
Example #43
0
    private IMyCubeBlock GetReference(ZACommons commons)
    {
        var referenceGroup = commons.GetBlockGroupWithName(RANGEFINDER_REFERENCE_GROUP);

        if (referenceGroup == null)
        {
            Result.Clear();
            Result.Append("Missing group: " + RANGEFINDER_REFERENCE_GROUP);
            return(null);
        }
        var references = ZACommons.GetBlocksOfType <IMyTerminalBlock>(referenceGroup.Blocks,
                                                                      block => block.CubeGrid == commons.Me.CubeGrid);

        if (references.Count == 0)
        {
            Result.Clear();
            Result.Append("Expecting at least 1 block on the same grid: " + RANGEFINDER_REFERENCE_GROUP);
            return(null);
        }
        return(references[0]);
    }
Example #44
0
    public void Run(ZACommons commons, EventDriver eventDriver)
    {
        if (!Triggered)
        {
            var turretGroup = commons.GetBlockGroupWithName(TURRET_DETECTOR_GROUP);
            if (turretGroup != null)
            {
                var turrets = ZACommons.GetBlocksOfType<IMyLargeTurretBase>(turretGroup.Blocks,
                                                                            block => block.CubeGrid == commons.Me.CubeGrid);
                for (var e = turrets.GetEnumerator(); e.MoveNext();)
                {
                    var turret = e.Current as IMyLargeTurretBase;
                    TurretInfo info;
                    if (turretInfos.TryGetValue(turret, out info))
                    {
                        if (turret.Elevation != info.LastElevation ||
                            turret.Azimuth != info.LastAzimuth)
                        {
                            // Trigger
                            ZACommons.StartTimerBlockWithName(commons.Blocks,
                                                              TURRET_DETECTOR_TRIGGER_TIMER_BLOCK_NAME);
                            // And don't trigger again until reset
                            Triggered = true;
                            break;
                        }
                    }
                    else
                    {
                        // Unknown turret
                        // FIXME shouldn't hold references...
                        turretInfos.Add(turret, new TurretInfo(turret));
                    }
                }
            }
        }

        eventDriver.Schedule(RunDelay, Run);
    }
Example #45
0
    private IMyPistonBase GetPiston(ZACommons commons)
    {
        var pistonGroup = commons.GetBlockGroupWithName(PistonGroupName);
        if (pistonGroup == null)
        {
            throw new Exception("Missing group: " + PistonGroupName);
        }
        var pistons = ZACommons.GetBlocksOfType<IMyPistonBase>(pistonGroup.Blocks);
        if (pistons.Count != 1)
        {
            throw new Exception("Expecting exactly 1 piston in " + PistonGroupName);
        }

        return (IMyPistonBase)pistons[0];
    }
Example #46
0
 private IMyCubeBlock GetReference(ZACommons commons)
 {
     var referenceGroup = commons.GetBlockGroupWithName(RANGEFINDER_REFERENCE_GROUP);
     if (referenceGroup == null)
     {
         Result.Clear();
         Result.Append("Missing group: " + RANGEFINDER_REFERENCE_GROUP);
         return null;
     }
     var references = ZACommons.GetBlocksOfType<IMyTerminalBlock>(referenceGroup.Blocks,
                                                                  block => block.CubeGrid == commons.Me.CubeGrid);
     if (references.Count == 0)
     {
         Result.Clear();
         Result.Append("Expecting at least 1 block on the same grid: " + RANGEFINDER_REFERENCE_GROUP);
         return null;
     }
     return references[0];
 }
Example #47
0
    private void _Run(ZACommons commons, EventDriver eventDriver)
    {
        // No source, don't bother
        var stockGroup = commons.GetBlockGroupWithName(STOCKER_SOURCE_NAME);
        if (stockGroup == null) return;

        var toCheck = new Dictionary<IMyTerminalBlock, Dictionary<string, int>>();

        for (var e = commons.GetBlockGroupsWithPrefix(STOCKER_PREFIX).GetEnumerator(); e.MoveNext();)
        {
            var group = e.Current;

            if (group.Name == STOCKER_SOURCE_NAME) continue;

            // Determine count
            var parts = group.Name.Split(new char[] { COUNT_DELIMITER }, 2);
            var count = 1;
            if (parts.Length == 2)
            {
                if (int.TryParse(parts[1], out count))
                {
                    count = Math.Max(count, 1);
                }
                else
                {
                    count = 1;
                }
            }

            // Determine SubtypeName
            var subtypeName = parts[0].Substring(STOCKER_PREFIX.Length).Trim();
            if (subtypeName.Length == 0) continue;

            // Gather destinations and wanted subtypes/counts
            group.Blocks.ForEach(block => {
                    if (!block.IsFunctional || block.GetInventoryCount() == 0) return;

                    Dictionary<string, int> wantedStocks;
                    if (!toCheck.TryGetValue(block, out wantedStocks))
                    {
                        wantedStocks = new Dictionary<string, int>();
                        toCheck.Add(block, wantedStocks);
                    }

                    int wanted;
                    if (wantedStocks.TryGetValue(subtypeName, out wanted))
                    {
                        // Use biggest request
                        wanted = Math.Max(wanted, count);
                    }
                    else
                    {
                        wanted = count;
                    }

                    wantedStocks[subtypeName] = wanted;
                });
        }

        // Determine how many are missing from all destinations
        var missingStocks = new Dictionary<string, LinkedList<MissingStock>>();
        for (var e = toCheck.GetEnumerator(); e.MoveNext();)
        {
            var block = e.Current.Key;
            var wantedStocks = e.Current.Value;

            // Gather current item counts
            var currents = new Dictionary<string, VRage.MyFixedPoint>();
            for (int i = 0; i < block.GetInventoryCount(); i++)
            {
                var inventory = block.GetInventory(i);
                var items = inventory.GetItems();
                items.ForEach(item => {
                        // Only care about wanted items for this block
                        var subtypeName = item.Content.SubtypeName;
                        if (wantedStocks.ContainsKey(subtypeName))
                        {
                            VRage.MyFixedPoint current;
                            if (!currents.TryGetValue(subtypeName, out current))
                            {
                                current = (VRage.MyFixedPoint)0.0f;
                            }
                            current += item.Amount;

                            currents[subtypeName] = current;
                        }
                    });
            }

            // Now figure out what's missing
            for (var f = wantedStocks.GetEnumerator(); f.MoveNext();)
            {
                var subtypeName = f.Current.Key;
                var count = f.Current.Value;

                VRage.MyFixedPoint current;
                if (!currents.TryGetValue(subtypeName, out current))
                {
                    current = (VRage.MyFixedPoint)0.0f;
                }

                if (current < count)
                {
                    // Add to dictionary under SubtypeName & keep track
                    // of this block
                    LinkedList<MissingStock> missing;
                    if (!missingStocks.TryGetValue(subtypeName, out missing))
                    {
                        missing = new LinkedList<MissingStock>();
                        missingStocks.Add(subtypeName, missing);
                    }

                    // NB Assumes first inventory
                    missing.AddLast(new MissingStock(block.GetInventory(0), (VRage.MyFixedPoint)count - current));
                }
            }
        }

        // Nothing missing, nothing to do
        if (missingStocks.Count == 0) return;

        // Now attempt to fill missing blocks
        stockGroup.Blocks.ForEach(source => {
                if (!source.IsFunctional) return;

                for (int i = 0; i < source.GetInventoryCount(); i++)
                {
                    var inventory = source.GetInventory(i);
                    var items = inventory.GetItems();
                    for (int j = items.Count - 1; j >= 0; j--)
                    {
                        var item = items[j];
                        var subtypeName = item.Content.SubtypeName;
                        LinkedList<MissingStock> missing;
                        if (missingStocks.TryGetValue(subtypeName, out missing))
                        {
                            var sourceAmount = item.Amount;
                            while (missing.First != null &&
                                   sourceAmount > (VRage.MyFixedPoint)0.0f)
                            {
                                var dest = missing.First.Value;
                                VRage.MyFixedPoint transferAmount;
                                // Has enough to fully restock?
                                if (sourceAmount >= dest.Amount)
                                {
                                    transferAmount = dest.Amount;
                                    // Assume it will succeed
                                    missing.RemoveFirst();
                                }
                                else
                                {
                                    transferAmount = sourceAmount;
                                    dest.Amount -= transferAmount;
                                }
                                // Move some over
                                dest.Inventory.TransferItemFrom(inventory, j, stackIfPossible: true, amount: transferAmount);
                                // FIXME no error checking

                                sourceAmount -= transferAmount;
                            }
                        }
                    }
                }
            });
    }
Example #48
0
 private void LaunchDecoy(ZACommons commons, EventDriver eventDriver,
                          string suffix)
 {
     var group = commons.GetBlockGroupWithName("SS Decoy Set" + suffix);
     // Activate decoys
     ZACommons.ForEachBlockOfType<IMyTerminalBlock>(group.Blocks,
                                                    block =>
             {
                 if (block.DefinitionDisplayNameText == "Decoy")
                 {
                     block.SetValue<bool>("OnOff", true);
                 }
             });
     eventDriver.Schedule(DecoyReleaseDelay, (c, e) => {
             var g = c.GetBlockGroupWithName("SS Decoy Set" + suffix);
             // Deactivate merge block
             ZACommons.ForEachBlockOfType<IMyShipMergeBlock>(g.Blocks,
                                                             merge =>
                     {
                         merge.SetValue<bool>("OnOff", false);
                     });
         });
 }
Example #49
0
 private bool IsInLauncher(ZACommons commons)
 {
     var launcherRelease = commons.GetBlockGroupWithName("Launcher Release");
     return launcherRelease != null;
 }
Example #50
0
    public void Demass(ZACommons commons, EventDriver eventDriver)
    {
        var shipControl = (ShipControlCommons)commons;

        var deltaTime = (eventDriver.TimeSinceStart - InitialTime).TotalSeconds;
        var launcherDelta = LauncherVelocity * deltaTime;
        var distanceFromLauncher = (shipControl.ReferencePoint -
                                    (InitialPosition + launcherDelta)).LengthSquared();

        if (distanceFromLauncher < DemassDistance * DemassDistance)
        {
            // Not yet
            eventDriver.Schedule(TicksPerRun, Demass);
            return;
        }

        // Disable mass
        var group = commons.GetBlockGroupWithName(MASS_GROUP);
        if (group != null)  ZACommons.EnableBlocks(group.Blocks, false);

        // Start roll
        shipControl.GyroControl.EnableOverride(true);
        shipControl.GyroControl.SetAxisVelocity(GyroControl.Roll,
                                                MathHelper.Pi);

        // All done
        if (PostLaunch != null) PostLaunch(commons, eventDriver);
    }
Example #51
0
    private IMyMotorStator GetRotor(ZACommons commons)
    {
        var rotorGroup = commons.GetBlockGroupWithName(RotorGroupName);
        if (rotorGroup == null)
        {
            throw new Exception("Missing group: " + RotorGroupName);
        }
        var rotors = ZACommons.GetBlocksOfType<IMyMotorStator>(rotorGroup.Blocks);
        if (rotors.Count != 1)
        {
            throw new Exception("Expecting exactly 1 rotor in " + RotorGroupName);
        }

        return (IMyMotorStator)rotors[0];
    }