/// <summary> /// This is where the guts of it all goes down. /// /// Remember that because of the nature of C#, items within a collection /// CANNOT BE PASSED BY REFERENCE! Thus, if you are making an alteration to any /// set within a method where the set is passed by reference (ref), you will /// need to assign the FuzzySet to a variable, pass that variable, and then set /// the oiginal FuzzySet within the Collection to the altered variable. /// A little bit of frigging around, but it works. /// </summary> public override void CalculateFuzzyLogic() { //Get the vars for this Simulator - dont alter this! _lander = ((LanderSim)Globals.Simulator).SpaceShip; //Assign the sets to variables FuzzySet tResult = new FuzzySet(ThrottleOutputs["ThrottleResult"]); //Clear the ThrottleResult Set so that we can repopulate it with new data tResult.Clear(); tResult.SetRangeWithPoints(0, 100); //WRITE YOUR RULES LIKE THIS.... //(dont forget to pass the //accumulator variable by reference, and also pass the Rule itself //Hot Tip: COMMENT YOUR RULES SO YOU KNOW WHAT THEY DO! // Rule 0: IF Height is High THEN Throttle -> Soft //RuleSets["Rule0"] = Rule.IS(_lander.Y, HeightSets["high"], ref tResult, ThrottleSets["soft"],RuleSets["Rule0"]); /* * * Rules go here.... * * */ //reassign the variables to the sets ThrottleOutputs["ThrottleResult"] = new FuzzySet(tResult); if (!Manual) { //THIS IS THE THROTTLE VALUE SET BY PRESSING THE BUTTONS _lander.Throttle = throttle; //Limit Throttle (incase of ridiculous values) if (_lander.Throttle < 0) _lander.Throttle = 0; if (_lander.Throttle > 100) _lander.Throttle = 100; //IF YOU ARE WRITING RULES FOR THE SIDE-THRUST, // You will need to defuzzify into _lander.Left and _lander.Right // Otherwise, leave these here to control your lander with the function buttons _lander.Left = throttleL; _lander.Right = throttleR; } else { //THIS IS THE THROTTLE VALUE DEFUZZIFIED BY THE RULESETS _lander.Throttle = Operations.DeFuzzifyCOG(ThrottleOutputs["ThrottleResult"]); } //Save the state of the Lander - dont alter this ((LanderSim) (Globals.Simulator)).SpaceShip = _lander; }
public LanderController() { //Obtain the variables affected by processing for this simulator (MoonLander) _lander = ((LanderSim) Globals.Simulator).SpaceShip; }
public void InitSpaceShip() { SpaceShip = new SimVars.MoonLanderVars(); #region Set up the initial state of the MoonLander's physics SpaceShip.X = 250; SpaceShip.Y = 4000; // spaceship location (init y=4,000m) SpaceShip.XVel = 0; SpaceShip.YVel = 0; // spaceship x and y velosity SpaceShip.LandingVelX = SimVars.MoonLanderVars.LANDING_X_BASE * 3; SpaceShip.LandingVelY = SimVars.MoonLanderVars.LANDING_Y_BASE * 3; SpaceShip.Variability = 0; // 0 = none 0.01 = pluss or -1% SpaceShip.Fuel = 8845 / 7; // 0-8,845 kg of Fuel mainly burned. SpaceShip.SafeX = 250; // safe to land here SpaceShip.SafeXWidth = 10001; // width 10000=no rocks if (Difficulty == SimDifficultyEnum.Easy) { } if (Difficulty == SimDifficultyEnum.Medium) { SpaceShip.Y = 4250 + new Random().Next(250); SpaceShip.LandingVelX = SimVars.MoonLanderVars.LANDING_X_BASE * 2; SpaceShip.LandingVelY = SimVars.MoonLanderVars.LANDING_Y_BASE * 2; SpaceShip.Variability = 0.02; // 0 = none 0.01 = pluss or -1% SpaceShip.Fuel = 8845 / 7; } if (Difficulty == SimDifficultyEnum.Hard) { SpaceShip.Y = 4250 + new Random().Next(250); SpaceShip.LandingVelX = SimVars.MoonLanderVars.LANDING_X_BASE * 2; SpaceShip.LandingVelY = SimVars.MoonLanderVars.LANDING_Y_BASE * 2; SpaceShip.Variability = 0.05; // 0 = none 0.01 = pluss or -1% SpaceShip.Fuel = 8845 / 8; SpaceShip.XVel = SpaceShip.LandingVelX + 0.1 + new Random().Next(20) / 20.0; } if (Difficulty == SimDifficultyEnum.VeryHard) { SpaceShip.Y = 4200 + new Random().Next(300); SpaceShip.X = 290 + new Random().Next(50); SpaceShip.LandingVelX = SimVars.MoonLanderVars.LANDING_X_BASE; SpaceShip.LandingVelY = SimVars.MoonLanderVars.LANDING_Y_BASE + SimVars.MoonLanderVars.MOON_GRAVITY / 4; SpaceShip.Variability = 0.05; // 0 = none 0.01 = pluss or -1% SpaceShip.Fuel = 8845 / 8; SpaceShip.XVel = 0.1 + new Random().Next(20) / 20.0; SpaceShip.SafeX = 100 + new Random().Next(160); // safe to land here SpaceShip.SafeXWidth = 60; // width 10000=no rocks } SpaceShip.Right = 0; SpaceShip.Left = 0; // spaceship right and left (RCS) thruster status 0-5 (Hundred newtons) SpaceShip.Throttle = 20; // throtle setting 0-100 (0,10-60 or 90) SpaceShip.DownForce = 0; // main thruster status 0, 5-44 (thousand newtons) SpaceShip.RightForce = 0; // main thruster status 0-5 (Hundred newtons) SpaceShip.LeftForce = 0; // main thruster status 0-5 (Hundred newtons) SpaceShip.MaxFuelIn1Sec = 8845 / 15 / 60.0; // about 9.82 SpaceShip.Mass = 5655 + SpaceShip.Fuel; // 5655 + Fuel SpaceShip.Boom = false; SpaceShip.Landed = false; #endregion State = SimulatorStateEnum.Initialised; }