Welcome to Keen Software House Forums! Log in or Sign up to interact with the KSH community.
  1. You are currently browsing our forum as a guest. Create your own forum account to access all forum functionality.

Safe return script (suggestion)

Discussion in 'Programming Questions and Suggestions' started by Bruce LeedleLeedleLeedleLee, Jun 18, 2016.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. Bruce LeedleLeedleLeedleLee Junior Engineer

    Messages:
    813
    I am completely incompetent with any other programming language than java, so i ask you guys for a script.

    What it should do:
    Run every few seconds, and check if there's a cockpit on the ship.
    If there is one, do nothing.
    If the cockpit is missing, set a tagged remote control to autopilot on, so it sets the ship on a course to go, if the cockpit is destroyed.

    I'm sorry if the script, or the suggestion already exists, but my search didn't find anything.
     
  2. Malware Master Engineer

    Messages:
    9,866
    c# isn't all that different from java. If you know one, learning the other is easy as pie.
     
  3. Bruce LeedleLeedleLeedleLee Junior Engineer

    Messages:
    813
    My Java is school level, and really rusty. I can do sorting stuff, but nothing much apart from that^^
     
  4. Malware Master Engineer

    Messages:
    9,866
    Usage:
    Install a remote control. Make sure it's set to "one way" and whatever other configuration you want.
    Install a programmable block and a timer. Set the timer to your desired update rate and have it call itself and the programmable block with no argument (do NOT use run with default argument).
    Install the script below into the programmable block.
    Configure the script by:
    Copy your desired GPS coordinate from the GPS screen into the argument box and press RUN (GPS coords look like this: GPS:Target GPS:-66.92:2.97:-12.33:) If you did it right, the script will tell you.
    Clear the box and type watch:<your block name>, and press RUN. Once again the script will tell you if you did it right.
    Clear the box and type rc:<your remote control name>, and press run. You know the drill by now :)

    Code:
    string _blockName; 
    string _remoteName;  
    WaypointInfo _waypoint; 
    bool _isValid;
    bool _isTriggered; 
    
    public Program() { 
        var parts = Storage.Split(';'); 
        if (parts.Length == 3) { 
            _isValid = WaypointInfo.TryParse(parts[0], out _waypoint); 
            _blockName = parts[1]; 
            if (_blockName.Length == 0)
                _blockName = null;
            _remoteName = parts[2];
            if (_remoteName.Length == 0)
                _remoteName = null;
        }
    } 
    
    public void Save() { 
        Storage = _waypoint.ToString() + ";" + _blockName + ";" + _remoteName; 
    } 
    
    public void Main(string argument) { 
        if (argument != "") {  
            argument = argument.Trim();
            if (argument == "?") {
                Echo ("Remote: " + (_remoteName ?? "<undefined>")); 
                Echo ("Block: " + (_blockName ?? "<undefined>"));
                Echo("Target: " + _waypoint);
            } else if (argument.StartsWith("GPS:", StringComparison.CurrentCultureIgnoreCase))
                UpdateTargetGps(argument);
            else if (argument.StartsWith("WATCH:", StringComparison.CurrentCultureIgnoreCase)) 
                SetWatchedBlock(argument.Substring(6).Trim()); 
            else if (argument.StartsWith("RC:", StringComparison.CurrentCultureIgnoreCase))  
                SetRemoteControlBlock(argument.Substring(3).Trim());  
            else {
                Echo("Usage:");
                Echo("    ? - display current setup");
                Echo("or"); 
                Echo("    <gpsstring> - update target gps");
                Echo("or");
                Echo("    WATCH:<targetblock> - select block to monitor");
                Echo("or"); 
                Echo("    RC:<targetblock> - select remote control"); 
            } 
        } else {  
            RunSafetyCheck();  
        }  
    } 
    
    void UpdateTargetGps(string argument)  { 
            WaypointInfo newWaypoint;  
            if (WaypointInfo.TryParse(TARGET_GPS, out newWaypoint)) {  
                _waypoint = newWaypoint;  
                _isValid = true;  
                Echo("GPS successfully updated to");  
                Echo(_waypoint.ToString());  
            } else {  
                Echo("Invalid GPS input");  
            }  
    } 
    
    void SetWatchedBlock(string argument) { 
        var block = GridTerminalSystem.GetBlockWithName(argument) as IMyTerminalBlock; 
        if (block != null) {
            _blockName = block.CustomName; 
            Echo("Target block assigned");
            Echo(_blockName);
        } else {
            Echo("Invalid block");
        }
    } 
    
    void SetRemoteControlBlock(string argument) {  
        var block = GridTerminalSystem.GetBlockWithName(argument) as IMyRemoteControl;
        if (block != null) { 
            _remoteName = block.CustomName;  
            Echo("Remote control assigned"); 
            Echo(_remoteName); 
        } else { 
            Echo("Invalid block"); 
        } 
    }  
    void RunSafetyCheck() {
        if (_blockName == null || _isTriggered)
            return;
        var block = GridTerminalSystem.GetBlockWithName(_blockName) as IMyTerminalBlock;
        if (block == null || !block.IsFunctional) {
            Engage();
            return;
        }
    }
    
    public void Engage() {
        var remote = GridTerminalSystem.GetBlockWithName(_remoteName) as IMyRemoteControl;
        Echo("Safety Override Engaged"); 
        _isTriggered = true; 
    
        if (remote == null) {
            Echo("ERROR no remote found");
            return;
        }
    
        remote.ClearWaypoints();
        remote.AddWaypoint(_waypoint.Position, _waypoint.Name);
        remote.SetValue("AutoPilot", true);  
    }
    
    public struct WaypointInfo { 
        public static bool TryParse(string gps, out WaypointInfo waypoint) { 
            if (string.IsNullOrEmpty(gps)) { 
                waypoint = new WaypointInfo(); 
                return false; 
            } 
            var parts  = TARGET_GPS.Split(':');  
            if (parts.Length != 6) { 
                waypoint = new WaypointInfo(); 
                return false; 
            } 
            if (!string.Equals(parts[0], "gps", StringComparison.CurrentCultureIgnoreCase)) { 
                waypoint = new WaypointInfo();  
                return false; 
            } 
            double x, y, z; 
            if (!double.TryParse(parts[2], out x) || !double.TryParse(parts[3], out y) || !double.TryParse(parts[4], out z)) { 
                waypoint = new WaypointInfo();  
                return false;  
            } 
            waypoint = new WaypointInfo(parts[1], new Vector3D(x, y, z)); 
            return true; 
       } 
    
        public readonly string Name; 
        public readonly Vector3D Position; 
        public WaypointInfo(string name, Vector3D position) { 
            Name = name ?? ""; 
            Position = position; 
        } 
    
        public override string ToString() { 
            if (Name == null) 
                return ""; 
            else 
                return "GPS:" + Name + ":" + Position.X + ":" + Position.Y + ":" + Position.Z + ":"; 
        } 
    }
    
     
    • Like Like x 1
  5. Bruce LeedleLeedleLeedleLee Junior Engineer

    Messages:
    813
Thread Status:
This last post in this thread was made more than 31 days old.