Beispiel #1
0
        public void Main(string argument, UpdateType updateSource)
        {
            Log("IslPrinterProjectorControl v1.0\n", true);
            if (argument == "swap")
            {
                Log("Swapping X anz Z axis ...");
                _SwapAxis = !_SwapAxis;
            }

            List <IMyShipController> controllers = new List <IMyShipController>();

            GridTerminalSystem.GetBlocksOfType(controllers, o => o.CustomName.Contains(CONTROLLER_TAG));

            List <IMyProjector> projectors = new List <IMyProjector>();

            GridTerminalSystem.GetBlocksOfType(projectors, o => o.CustomName.Contains(PROJECTOR_TAG));

            bool valid = true;

            if (controllers.Count == 0)
            {
                Log("Error: Cannot find controller.");
                valid = false;
            }
            else if (controllers.Count > 1)
            {
                Log("Warning: Found more than one controller. First one ('" + controllers[0].CustomName + "') will be used.");
            }
            else
            {
                Log("Found controller '" + controllers[0].CustomName + "'");
            }

            IMyShipController controller = controllers[0];

            if (projectors.Count == 0)
            {
                Log("Error: Cannot find projector.");
                valid = false;
            }
            else if (projectors.Count > 1)
            {
                Log("Warning: Found more than one projector. First one ('" + projectors[0].CustomName + "') will be used.");
            }
            else
            {
                Log("Found projector '" + projectors[0].CustomName + "'");
            }

            IMyProjector projector = projectors[0];

            List <IMyTerminalBlock> displays = new List <IMyTerminalBlock>();

            GridTerminalSystem.GetBlocksOfType(displays, o => o is IMyTextSurfaceProvider && (o.CustomName.Contains(DISPLAY_REMAINING_TAG) || o.CustomName.Contains(DISPLAY_OFFSET_TAG)));

            List <IMyTextSurface> remainingSurfaces = new List <IMyTextSurface>();
            List <IMyTextSurface> offsetSurfaces    = new List <IMyTextSurface>();

            foreach (IMyTerminalBlock display in displays)
            {
                if (display.CustomName.Contains(DISPLAY_REMAINING_TAG))
                {
                    int index = GetTextSurfaceIndex(display);
                    remainingSurfaces.Add(((IMyTextSurfaceProvider)display).GetSurface(index));
                    Log("Found remaining display '" + display.CustomName + "'. Using surface #" + index);
                }

                if (display.CustomName.Contains(DISPLAY_OFFSET_TAG))
                {
                    int index = GetTextSurfaceIndex(display);
                    offsetSurfaces.Add(((IMyTextSurfaceProvider)display).GetSurface(index));
                    Log("Found offset display '" + display.CustomName + "'. Using surface #" + index);
                }
            }

            if (!valid)
            {
                return;
            }

            Vector3 move = controller.MoveIndicator;
            Vector2 turn = controller.RotationIndicator;
            float   roll = controller.RollIndicator;

            bool hasInput = move.X + move.Y + move.Z + turn.X + turn.Y + roll != 0;

            if (!_InputHandled && hasInput)
            {
                _InputHandled = true;

                Vector3I pos = projector.ProjectionOffset;

                if (move.X < 0)
                {
                    if (_SwapAxis)
                    {
                        pos.Z += 1;
                    }
                    else
                    {
                        pos.X += 1;
                    }
                }

                if (move.X > 0)
                {
                    if (_SwapAxis)
                    {
                        pos.Z -= 1;
                    }
                    else
                    {
                        pos.X -= 1;
                    }
                }

                if (move.Y < 0)
                {
                    pos.Y += 1;
                }

                if (move.Y > 0)
                {
                    pos.Y -= 1;
                }

                if (move.Z < 0)
                {
                    if (_SwapAxis)
                    {
                        pos.X -= 1;
                    }
                    else
                    {
                        pos.Z += 1;
                    }
                }

                if (move.Z > 0)
                {
                    if (_SwapAxis)
                    {
                        pos.X += 1;
                    }
                    else
                    {
                        pos.Z -= 1;
                    }
                }

                projector.ProjectionOffset = pos;

                Vector3I rot = projector.ProjectionRotation;

                if (turn.X > 0)
                {
                    rot.X = UpdateRot(rot.X, 1);
                }

                if (turn.X < 0)
                {
                    rot.X = UpdateRot(rot.X, -1);
                }

                if (turn.Y > 0)
                {
                    rot.Y = UpdateRot(rot.Y, 1);
                }

                if (turn.Y < 0)
                {
                    rot.Y = UpdateRot(rot.Y, -1);
                }

                if (roll > 0)
                {
                    rot.Z = UpdateRot(rot.Z, 1);
                }

                if (roll < 0)
                {
                    rot.Z = UpdateRot(rot.Z, -1);
                }

                projector.ProjectionRotation = rot;

                projector.UpdateOffsetAndRotation();
            }

            if (_InputHandled && !hasInput)
            {
                _InputHandled = false;
            }

            if (remainingSurfaces.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                // MyDefinitionBase is prohibited but 'var' works ...
                foreach (var pair in projector.RemainingBlocksPerType.OrderByDescending(o => o.Value))
                {
                    sb.Append(pair.Value).Append("x ").Append(GetBlockName(pair.Key.ToString())).AppendLine();
                }

                foreach (IMyTextSurface surface in remainingSurfaces)
                {
                    surface.WriteText(sb);
                }
            }

            if (offsetSurfaces.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("Horizontal offset: ").AppendLine(projector.ProjectionOffset.X.ToString("+#;-#;0"));
                sb.Append("Vertical offset: ").AppendLine(projector.ProjectionOffset.Y.ToString("+#;-#;0"));
                sb.Append("Forward offset: ").AppendLine(projector.ProjectionOffset.Z.ToString("+#;-#;0"));
                sb.Append("Pitch: ").AppendLine(ToDeg(projector.ProjectionRotation.X));
                sb.Append("Yaw: ").AppendLine(ToDeg(projector.ProjectionRotation.Y));
                sb.Append("Roll: ").AppendLine(ToDeg(projector.ProjectionRotation.Z));

                foreach (IMyTextSurface surface in offsetSurfaces)
                {
                    surface.WriteText(sb);
                }
            }
        }
Beispiel #2
0
        public void Main(string argumentString = "CHECK")
        {
            var arguments = argumentString.Split(' ');
            var argument  = arguments[0] == "" ? "CHECK" : arguments[0];

            Echo((executionCounter++).ToString());

            if (argument != "CHECK" || _state != _previousState)
            {
                // Keep previous output on check
                _output.Clear();
            }
            _previousState = _state;

            if (argument == "PARSE")
            {
                Runtime.UpdateFrequency = UpdateFrequency.None;
                MyIniParseResult result;
                if (!_ini.TryParse(Me.CustomData, out result))
                {
                    Echo($"CustomData parsing error: \nLine {result.LineNo}");
                    return;
                }

                var calculatorAssemblerName = _ini.Get("general", "CalculatorAssembler").ToString(_calculatorAssemblerName);
                var lcdBlueprintInfoName    = _ini.Get("general", "LcdBlueprintInfo").ToString(_lcdBlueprintInfoName);
                var lcdComponentStatusName  = _ini.Get("general", "LcdComponentStatus").ToString(_lcdComponentStatusName);
                var lcdWeldStatusName       = _ini.Get("general", "LcdWeldStatus").ToString(_lcdWeldStatusName);
                var pistonGroupName         = _ini.Get("general", "PistonGroup").ToString(_pistonGroupName);
                var productionAssemblerName = _ini.Get("general", "ProductionAssembler").ToString(_productionAssemblerName);
                var projectors         = _ini.Get("general", "Projectors").ToString("Large:WeldWallLargeProjector");
                var retractVelocity    = _ini.Get("general", "RetractVelocity").ToDouble(_retractVelocity);
                var weldEndedTimerName = _ini.Get("general", "WeldEndedTimer").ToString(_weldEndedTimerName);
                var weldReadyTimerName = _ini.Get("general", "WeldReadyTimer").ToString(_weldReadyTimerName);
                var weldVelocity       = _ini.Get("general", "WeldVelocity").ToDouble(_weldVelocity);
                var welderGroupName    = _ini.Get("general", "WelderGroup").ToString(_welderGroupName);

                var lookup = new LookupHelper {
                    GridTerminalSystem = GridTerminalSystem,
                };

                _calculatorAssembler = lookup.GetBlockWithName <IMyAssembler>(calculatorAssemblerName, true);
                _lcdBlueprintInfo    = lookup.GetBlockWithName <IMyTextPanel>(lcdBlueprintInfoName, true);
                _lcdComponentStatus  = lookup.GetBlockWithName <IMyTextPanel>(lcdComponentStatusName, true);
                _lcdWeldStatus       = lookup.GetBlockWithName <IMyTextPanel>(lcdWeldStatusName);
                _pistons             = lookup.GetBlocksInGroup <IMyExtendedPistonBase>(pistonGroupName, true);
                _productionAssembler = lookup.GetBlockWithName <IMyAssembler>(productionAssemblerName, true);
                _weldEndedTimer      = lookup.GetBlockWithName <IMyTimerBlock>(weldEndedTimerName);
                _weldReadyTimer      = lookup.GetBlockWithName <IMyTimerBlock>(weldReadyTimerName);
                _welders             = lookup.GetBlocksInGroup <IMyShipWelder>(welderGroupName, true);

                _projectors = projectors
                              .Split(' ')
                              .Select(part => part.Split(':'))
                              .ToDictionary(split => split[0], split => lookup.GetBlockWithName <IMyProjector>(split[1], true));

                var firstProjectorEntry = _projectors.FirstOrDefault();
                _projector = firstProjectorEntry.Value;

                if (_projector == null)
                {
                    Echo("No projector found");
                    return;
                }
                else
                {
                    _projector.Enabled = true;
                    _projectorName     = firstProjectorEntry.Key;
                }

                ExtractBlueprints();
                Runtime.UpdateFrequency = UpdateFrequency.Update100;
            }

            if (_projector == null)
            {
                Echo("No projector found");
                return;
            }

            var currentBpId = _state == "Preparing" || _state == "Welding"
                ? _previousBlueprint.ID
                : GetProjectorBlueprintId(_projector);
            var currentBlueprint = _blueprints.GetValueOrDefault(currentBpId);

            if (currentBlueprint != _previousBlueprint && currentBlueprint != null)
            {
                _state = "CheckBlueprint";

                _projector.ProjectionOffset   = currentBlueprint.ProjectionOffset;
                _projector.ProjectionRotation = currentBlueprint.ProjectionRotation;
                _projector.UpdateOffsetAndRotation();

                UpdateComponentList(currentBlueprint);
            }

            _previousBlueprint = currentBlueprint;

            switch (argument)
            {
            case "CHECK":
                if (currentBlueprint == null)
                {
                    break;
                }
                if (_state == "Preparing")
                {
                    if (_lcdComponentStatus.GetText().Trim() == "")
                    {
                        if (_weldReadyTimer == null)
                        {
                            Main("Weld");
                        }
                        else
                        {
                            _weldReadyTimer.ApplyAction("TriggerNow");
                        }
                    }
                }
                else if (_state == "Welding")
                {
                    if (_pistons.TrueForAll(p => p.MaxLimit == p.CurrentPosition))
                    {
                        _welders.ForEach(w => w.Enabled = false);
                        _weldEndedTimer?.ApplyAction("TriggerNow");
                        _state = "CheckBlueprint";
                    }
                }

                var buildProgress = _projector.DetailedInfo.Substring(_projector.DetailedInfo.IndexOf("Build progress"));
                if (_lcdWeldStatus != null)
                {
                    _lcdWeldStatus.CustomData = Utils.ToMmasterLcd(buildProgress);
                }

                break;

            case "PREPARE":
                if (currentBlueprint == null)
                {
                    _output.Add("Can't prepare when no blueprint is loaded");
                    break;
                }

                UpdateComponentList(currentBlueprint);
                AssembleMissing();
                _output.Add("Assembly of missing items in progress");
                _state = "Preparing";

                break;

            case "SAVE_OFFSET":
                if (currentBlueprint == null)
                {
                    currentBlueprint = new BlueprintInfo()
                    {
                        ID   = currentBpId,
                        Name = currentBpId,
                    };
                    UpdateComponentList(currentBlueprint);
                    _blueprints.Add(currentBpId, currentBlueprint);
                }

                currentBlueprint.ProjectionOffset   = _projector.ProjectionOffset;
                currentBlueprint.ProjectionRotation = _projector.ProjectionRotation;
                currentBlueprint.Save(_ini);
                Me.CustomData = _ini.ToString();

                break;

            case "SET_ACTIVE":
                IMyProjector newActiveProjector = _projectors.GetValueOrDefault(arguments[1]);

                if (newActiveProjector == null)
                {
                    _output.Add($"Projector {arguments[1]} not found");
                }
                else
                {
                    _projectors.Values.ToList().ForEach(projector => projector.Enabled = false);
                    _projector         = newActiveProjector;
                    _projectorName     = arguments[1];
                    _projector.Enabled = true;
                }

                break;

            case "STOP":
                Stop();
                break;

            case "RETRACT":
                _pistons.ForEach(p => p.Enabled = true);
                DistributePistonVelocity(-_retractVelocity);
                break;

            case "WELD":
                if (currentBlueprint == null)
                {
                    _output.Add("Can't start welding when no blueprint is loaded");
                    break;
                }

                _welders.ForEach(w => w.Enabled = true);
                _pistons.ForEach(p => p.Enabled = true);
                DistributePistonVelocity(_weldVelocity);

                _state = "Welding";
                break;
            }


            _lcdBlueprintInfo.WriteText($@"
{(currentBlueprint == null ? "Unknown blueprint" : (currentBlueprint.Name ?? "Unnamed blueprint"))}
Blueprint ID: {currentBpId}
State: {_state}
Active projector: {_projectorName}

{String.Join("\n", _output.ToArray())}
".Trim());
        }