public void ParserProperty(IMyTerminalBlock block) { WriteText($"=== Properties Info ===", true); List <ITerminalProperty> propertyList = new List <ITerminalProperty>(); block.GetProperties(propertyList, null); propertyList.ForEach(delegate(ITerminalProperty property) { if (property.Is <float>()) { WriteText($"{property.Id}={block.GetValueFloat(property.Id)}", true); } if (property.Is <bool>()) { WriteText($"{property.Id}={block.GetValueBool(property.Id)}", true); } }); }
protected void executeBlock(IMyTerminalBlock block) { this.myProgram.Echo(this.blockName + " " + this.action + " " + this.value); if (block == null) { this.haltError = true; return; } switch (this.action) { case "Apply": if (this.value == "On") { this.value = "OnOff_On"; } if (this.value == "Off") { this.value = "OnOff_Off"; } block.ApplyAction(this.value); return; case "Check": if (this.value == "On" || this.value == "Off") { bool onoff = block.GetValueBool("OnOff"); if (this.value == "On" && !onoff) { this.haltError = true; return; } if (this.value == "Off" && onoff) { this.haltError = true; return; } } if (this.value == "Attach" || this.value == "Detach") { bool rotorAttached = ((IMyMotorStator)block).IsAttached; if (this.value == "Attach" && !rotorAttached) { this.haltError = true; return; } if (this.value == "Detach" && rotorAttached) { this.haltError = true; return; } } if (this.value == "Extend" || this.value == "Retract") { PistonStatus pistonExtended = ((IMyPistonBase)block).Status; if (pistonExtended == PistonStatus.Stopped) { this.haltError = true; return; } if (this.value == "Extend" && pistonExtended == PistonStatus.Retracted) { this.haltError = true; return; } if (this.value == "Retract" && pistonExtended == PistonStatus.Extended) { this.haltError = true; return; } } return; case "Velocity": block.SetValue <Single>("Velocity", Single.Parse(this.value)); return; case "MaxLimit": block.SetValue <Single>("UpperLimit", Single.Parse(this.value)); return; case "MinLimit": block.SetValue <Single>("LowerLimit", Single.Parse(this.value)); return; } }
BlockState CheckState(IMyTerminalBlock terminalBlock, BlockType blockType) { switch (blockType) { case BlockType.Cockpit: var cockpit = terminalBlock as IMyCockpit; ////List<ITerminalAction> actions = new List<ITerminalAction>(); ////cockpit.GetActions(actions); ////foreach (var action in actions) ////{ //// Echo(action.Name.ToString()); ////} if (cockpit.IsUnderControl) { return(BlockState.On); } else { return(BlockState.Off); } case BlockType.Connector: var connector = terminalBlock as IMyShipConnector; if (connector.Status == MyShipConnectorStatus.Connected) { return(BlockState.On); } else { return(BlockState.Off); } case BlockType.Door: var door = terminalBlock as IMyDoor; if (door.Status == DoorStatus.Open) { return(BlockState.On); } else if (door.Status == DoorStatus.Open) { return(BlockState.Off); } else { return(BlockState.Between); } case BlockType.Piston: var piston = terminalBlock as IMyPistonBase; if (piston.Status == PistonStatus.Extended) { return(BlockState.On); } else if (piston.Status == PistonStatus.Retracted) { return(BlockState.Off); } else { return(BlockState.Between); } // Basic/default case BlockType.Basic: default: if (terminalBlock.GetValueBool("OnOff")) { return(BlockState.On); } else { return(BlockState.Off); } } }
public void ExecuteInstruction() { if (Instructions.Count <= 0 || Instructions.Count < Line) { return; } Instruction instruction = Instructions[Line]; string name, mode, action, search, property, source; float value1 = 0f; float value2 = 0f; float epsilon = 0.1f; bool bool1 = false; bool bool2 = false; Object var; switch (instruction.Command) { case Command.Comment: Line++; break; case Command.Var: // var <var name> <float value> name = instruction.Fields[0]; ParseFieldFloat(instruction.Fields[1], out value1); Vars.Add(name, value1); Line++; break; case Command.Add: case Command.Sub: case Command.Mul: case Command.Div: // <operator> <var name> <var name|float value> <float value> name = instruction.Fields[0]; ParseFieldFloat(instruction.Fields[1], out value1); ParseFieldFloat(instruction.Fields[2], out value2); float result = 0f; switch (instruction.Command) { case Command.Add: result = value1 + value2; break; case Command.Sub: result = value1 - value2; break; case Command.Mul: result = value1 * value2; break; case Command.Div: result = value1 / value2; break; } Vars[name] = result; Line++; break; case Command.Print: bool append = true; string message = ParseFieldString(instruction.Fields[0]); message = "\n" + message; if (instruction.Fields.Count > 1) { List <string> args = new List <string>(); for (int i = 1; i < instruction.Fields.Count; i++) { float value; ParseFieldFloat(instruction.Fields[1], out value); args.Add(value.ToString()); } myProgram.drawingSurface.WriteText(string.Format(message, args.ToArray()), append); } else { myProgram.drawingSurface.WriteText(message, append); } Line++; break; case Command.Select: // select <var name> <mode> <string value> //myProgram.drawingSurface.WriteText($"\nSelect fields={instruction.Fields.Count}", true); name = instruction.Fields[0]; mode = instruction.Fields[1]; search = instruction.Fields[2]; List <IMyTerminalBlock> blocks = new List <IMyTerminalBlock>(); switch (mode) { case "tag": myProgram.GridTerminalSystem.GetBlocksOfType <IMyTerminalBlock>(blocks, myBlock => myBlock.CustomName.Contains(search)); if (blocks.Count > 0) { Vars.Add(name, blocks); } break; case "group": IMyBlockGroup group = myProgram.GridTerminalSystem.GetBlockGroupWithName(search); group.GetBlocks(blocks); if (blocks.Count > 0) { Vars.Add(name, blocks); } break; default: IMyTerminalBlock block = myProgram.GridTerminalSystem.GetBlockWithName(search); if (block != null) { blocks.Add(block); Vars.Add(name, blocks); } break; } Line++; break; case Command.Action: // action <var name> <action> name = instruction.Fields[0]; action = instruction.Fields[1]; Vars.TryGetValue(name, out var); if (instruction.Fields.Count == 3) { ParseFieldFloat(instruction.Fields[2], out value1); } if (var != null) { blocks = (List <IMyTerminalBlock>)var; blocks.ForEach(delegate(IMyTerminalBlock block) { block.ApplyAction(action); }); } Line++; break; case Command.Set: // set <var name> <property> <value> name = instruction.Fields[0]; property = instruction.Fields[1]; Vars.TryGetValue(name, out var); if (var != null) { if (var is List <IMyTerminalBlock> ) { blocks = (List <IMyTerminalBlock>)var; if (blocks.Count > 0) { IMyTerminalBlock firstBlock = blocks[0]; ITerminalProperty prop = firstBlock.GetProperty(property); //myProgram.drawingSurface.WriteText($"\nProperty={prop.TypeName}", true); switch (prop.TypeName) { case "Single": ParseFieldFloat(instruction.Fields[2], out value1); break; case "Boolean": ParseFieldBool(instruction.Fields[2], out bool1); break; } blocks.ForEach(delegate(IMyTerminalBlock block) { switch (prop.TypeName) { case "Single": block.SetValueFloat(property, value1); break; case "Boolean": block.SetValueBool(property, bool1); break; } }); } } } Line++; break; case Command.Get: // get <var name> <property> <var name> name = instruction.Fields[0]; property = instruction.Fields[1]; source = instruction.Fields[2]; Vars.TryGetValue(source, out var); if (var != null) { if (var is List <IMyTerminalBlock> ) { blocks = (List <IMyTerminalBlock>)var; if (blocks.Count > 0) { IMyTerminalBlock firstBlock = blocks[0]; ITerminalProperty prop = firstBlock.GetProperty(property); switch (prop.TypeName) { case "Single": Vars.Add(name, firstBlock.GetValueFloat(property)); break; case "Boolean": Vars.Add(name, firstBlock.GetValueBool(property)); break; } } } } Line++; break; case Command.Wait: // wait <var name> <property> <value> <epsilon> name = instruction.Fields[0]; property = instruction.Fields[1]; if (instruction.Fields.Count > 3) { ParseFieldFloat(instruction.Fields[3], out epsilon); } Vars.TryGetValue(name, out var); bool isState = true; if (var != null) { if (var is List <IMyTerminalBlock> ) { blocks = (List <IMyTerminalBlock>)var; if (blocks.Count > 0) { ITerminalProperty prop = null; IMyTerminalBlock firstBlock = blocks[0]; prop = firstBlock.GetProperty(property); if (prop == null) { switch (GetAttibutType(property)) { case "Single": ParseFieldFloat(instruction.Fields[2], out value1); break; case "Boolean": ParseFieldBool(instruction.Fields[2], out bool1); break; } } else { switch (prop.TypeName) { case "Single": ParseFieldFloat(instruction.Fields[2], out value1); break; case "Boolean": ParseFieldBool(instruction.Fields[2], out bool1); break; } break; } blocks.ForEach(delegate(IMyTerminalBlock block) { if (prop == null) { switch (GetAttibutType(property)) { case "Single": value2 = GetAttibutFloat(block, property); if (Math.Abs(value2 - value1) > epsilon) { isState = false; } break; case "Boolean": bool2 = GetAttibutBool(block, property); if (bool2 != bool1) { isState = false; } break; } } else { switch (prop.TypeName) { case "Single": value2 = block.GetValueFloat(property); if (Math.Abs(value2 - value1) > epsilon) { isState = false; } break; case "Boolean": bool2 = block.GetValueBool(property); if (bool2 != bool1) { isState = false; } break; } } }); } } } if (isState) { Line++; } break; } }