Example #1
0
        // Evaluate and return the value of the part of an expression that this instance represents
        public object GetValue()
        {
            if (EvalDlg != null)
            {
                EvalDlg();
            }

            if (SpecialValue != null)
            {
                if (string.IsNullOrEmpty(Suffix))
                {
                    return(SpecialValue.ToString());
                }
                else
                {
                    return(SpecialValue.GetSuffix(Suffix));
                }
            }

            if (LeftSide == null)
            {
                if (Variable != null && !IsStatic)
                {
                    return(Variable.Value);
                }
                else
                {
                    return(Value);
                }
            }
            else
            {
                if (Operator == "AND")
                {
                    return(LeftSide.Bool() && RightSide.Bool());
                }
                if (Operator == "OR")
                {
                    return(LeftSide.Bool() || RightSide.Bool());
                }

                if (LeftSide.Value is String || RightSide.Value is String)
                {
                    if (Operator == "+")
                    {
                        return(LeftSide.Value.ToString() + RightSide.Value.ToString());
                    }

                    if (Operator == "==")
                    {
                        return(LeftSide.Value.ToString() == RightSide.Value.ToString());
                    }
                    if (Operator == "=")
                    {
                        return(LeftSide.Value.ToString() == RightSide.Value.ToString());
                    }
                    if (Operator == "!=")
                    {
                        return(LeftSide.Value.ToString() != RightSide.Value.ToString());
                    }
                }

                if (LeftSide.Value is float || RightSide.Value is float)
                {
                    if (Operator == "+")
                    {
                        return(LeftSide.Float() + RightSide.Float());
                    }
                    if (Operator == "-")
                    {
                        return(LeftSide.Float() - RightSide.Float());
                    }
                    if (Operator == "/")
                    {
                        return(LeftSide.Float() / RightSide.Float());
                    }
                    if (Operator == "*")
                    {
                        return(LeftSide.Float() * RightSide.Float());
                    }
                    if (Operator == "^")
                    {
                        return((float)Math.Pow(LeftSide.Double(), RightSide.Double()));
                    }

                    if (Operator == "<")
                    {
                        return(LeftSide.Float() < RightSide.Float());
                    }
                    if (Operator == ">")
                    {
                        return(LeftSide.Float() > RightSide.Float());
                    }
                    if (Operator == "<=")
                    {
                        return(LeftSide.Float() <= RightSide.Float());
                    }
                    if (Operator == ">=")
                    {
                        return(LeftSide.Float() >= RightSide.Float());
                    }
                    if (Operator == "==")
                    {
                        return(LeftSide.Float() == RightSide.Float());
                    }
                    if (Operator == "=")
                    {
                        return(LeftSide.Float() == RightSide.Float());
                    }
                    if (Operator == "!=")
                    {
                        return(LeftSide.Float() != RightSide.Float());
                    }
                }

                if (LeftSide.Value is Direction && RightSide.Value is Direction)
                {
                    if (Operator == "*")
                    {
                        return((Direction)LeftSide.GetValue() * (Direction)RightSide.GetValue());
                    }
                    if (Operator == "+")
                    {
                        return((Direction)LeftSide.GetValue() + (Direction)RightSide.GetValue());
                    }
                    if (Operator == "-")
                    {
                        return((Direction)LeftSide.GetValue() + (Direction)RightSide.GetValue());
                    }
                }
            }

            throw new kOSException("Expression error.");
        }
Example #2
0
        // Evaluate and return the value of the part of an expression that this instance represents
        public object GetValue()
        {
            if (LeftSide == null)
            {
                if (Variable != null && !IsStatic)
                {
                    return(Variable.Value);
                }
                else
                {
                    return(Value);
                }
            }
            else
            {
                if (LeftSide.Value is String || RightSide.Value is String)
                {
                    if (Operator == "+")
                    {
                        return(LeftSide.Value.ToString() + RightSide.Value.ToString());
                    }
                }

                if (LeftSide.Value is float || RightSide.Value is float)
                {
                    if (Operator == "+")
                    {
                        return(LeftSide.Float() + RightSide.Float());
                    }
                    if (Operator == "-")
                    {
                        return(LeftSide.Float() - RightSide.Float());
                    }
                    if (Operator == "/")
                    {
                        return(LeftSide.Float() / RightSide.Float());
                    }
                    if (Operator == "*")
                    {
                        return(LeftSide.Float() * RightSide.Float());
                    }
                    if (Operator == "^")
                    {
                        return((float)Math.Pow(LeftSide.Double(), RightSide.Double()));
                    }

                    if (Operator == "<")
                    {
                        return(LeftSide.Float() < RightSide.Float());
                    }
                    if (Operator == ">")
                    {
                        return(LeftSide.Float() > RightSide.Float());
                    }
                    if (Operator == "<=")
                    {
                        return(LeftSide.Float() <= RightSide.Float());
                    }
                    if (Operator == ">=")
                    {
                        return(LeftSide.Float() >= RightSide.Float());
                    }
                    if (Operator == "==")
                    {
                        return(LeftSide.Float() == RightSide.Float());
                    }
                    if (Operator == "=")
                    {
                        return(LeftSide.Float() == RightSide.Float());
                    }
                    if (Operator == "!=")
                    {
                        return(LeftSide.Float() != RightSide.Float());
                    }
                }

                if (LeftSide.Value is Direction && RightSide.Value is Direction)
                {
                    if (Operator == "*")
                    {
                        return((Direction)LeftSide.Value * (Direction)RightSide.Value);
                    }
                    if (Operator == "+")
                    {
                        return((Direction)LeftSide.Value + (Direction)RightSide.Value);
                    }
                    if (Operator == "-")
                    {
                        return((Direction)LeftSide.Value + (Direction)RightSide.Value);
                    }
                }
            }

            throw new kOSException("Expression error.");
        }
Example #3
0
            public void OnFlyByWire(ref FlightCtrlState c)
            {
                Expression e = cpu.GetDeepestChildContext().GetLock(propertyName);

                if (e != null)
                {
                    Value = e.GetValue();

                    if (propertyName == "throttle")
                    {
                        c.mainThrottle = (float)e.Double();
                    }

                    if (propertyName == "wheelthrottle")
                    {
                        c.wheelThrottle = (float)Utils.Clamp(e.Double(), -1, 1);
                    }

                    if (propertyName == "steering")
                    {
                        if (Value is String && ((string)Value).ToUpper() == "KILL")
                        {
                            SteeringHelper.KillRotation(c, vessel);
                        }
                        else if (Value is Direction)
                        {
                            SteeringHelper.SteerShipToward((Direction)Value, c, vessel);
                        }
                        else if (Value is Vector)
                        {
                            SteeringHelper.SteerShipToward(((Vector)Value).ToDirection(), c, vessel);
                        }
                        else if (Value is Node)
                        {
                            SteeringHelper.SteerShipToward(((Node)Value).GetBurnVector().ToDirection(), c, vessel);
                        }
                    }

                    if (propertyName == "wheelsteering")
                    {
                        float bearing = 0;

                        if (Value is VesselTarget)
                        {
                            bearing = VesselUtils.GetTargetBearing(vessel, ((VesselTarget)Value).target);
                        }
                        else if (Value is GeoCoordinates)
                        {
                            bearing = ((GeoCoordinates)Value).GetBearing(vessel);
                        }
                        else if (Value is double)
                        {
                            bearing = (float)(Math.Round((double)Value) - Mathf.Round(FlightGlobals.ship_heading));
                        }

                        if (vessel.horizontalSrfSpeed > 0.1f)
                        {
                            if (Mathf.Abs(VesselUtils.AngleDelta(VesselUtils.GetHeading(vessel), VesselUtils.GetVelocityHeading(vessel))) <= 90)
                            {
                                c.wheelSteer = Mathf.Clamp(bearing / -10, -1, 1);
                            }
                            else
                            {
                                c.wheelSteer = -Mathf.Clamp(bearing / -10, -1, 1);
                            }
                        }
                    }

                    if (cpu.GetLock(name) == null)
                    {
                        locked = false;
                    }
                }
            }