void Start() { GameControl.control.player = gameObject; sensitivity = GameControl.control.GetSensitivity(); //ensure only standard thruster group is active SetGroup(immunity, critical); SetGroup(standard, immunity); current = standard; }
void FixedUpdate() { //continuous forward movement transform.GetComponent <Rigidbody2D>().velocity = transform.up * GameControl.control.GetMoveSpeed(); //reorientation of player in relation to accelerometer input OrientPlayer(); if (GameControl.control.GetHealth() <= GameControl.control.GetDamage()) { if (!damaged) { damaged = true; recent = current; current = critical; SetGroup(current, recent); } } else { if (damaged) { damaged = false; recent = current; current = standard; SetGroup(current, recent); } } //boost animation upon gaining immunity if (GameControl.control.GetImmunity()) { if (!boosting) { boosting = true; recent = current; current = immunity; SetGroup(current, recent); } } else { if (boosting) { boosting = false; recent = current; current = standard; SetGroup(current, recent); } } }
private void SetGroup(ThrusterGroup active, ThrusterGroup inactive) { //toggle main thrusters active.main.thruster.Play(true); active.main.trail.SetActive(true); inactive.main.thruster.Stop(true); inactive.main.trail.SetActive(false); //deactivate all by primary axillary of current active SetThruster(inactive.right.primary, inactive.right.secondary); SetThruster(active.right.secondary, inactive.right.primary); SetThruster(active.right.primary, active.right.secondary); SetThruster(inactive.left.primary, inactive.left.secondary); SetThruster(active.left.secondary, inactive.left.primary); SetThruster(active.left.primary, active.left.secondary); }
public void SetThrusterForces(ThrusterGroup thrusterGroup, double thrustToApply) { var thrustProportion = thrustToApply / thrusterGroup.MaxThrust; var hasDetected = false; foreach (var thisThruster in thrusterGroup.thrusters) { if (thisThruster.IsWorking) { thisThruster.ThrustOverride = (float)(thisThruster.MaxThrust * thrustProportion); } else if (!hasDetected) { parent_program.systemsAnalyzer.basicDataGatherRequired = true; parent_program.shipIOHandler.Echo("Warning:\nThruster damage detected, recomputing."); hasDetected = true; } } }
public ThrusterControl(IMyShipController rc, UpdateFrequency tickSpeed, List <IMyThrust> thrusters, Mode mode = Mode.Min) { if (rc == null) { throw new Exception("Ship controller null."); } double factor = 1; if (tickSpeed == UpdateFrequency.Update10) { factor = 10; } else if (tickSpeed == UpdateFrequency.Update100) { factor = 100; } double secondsPerTick = (1.0 / 60) * factor; this.rc = rc; Dictionary <Base6Directions.Direction, ThrusterGroup> temp = new Dictionary <Base6Directions.Direction, ThrusterGroup>(6); foreach (IMyThrust t in thrusters) { t.ThrustOverride = 0; t.Enabled = true; if (temp.ContainsKey(t.Orientation.Forward)) { temp [t.Orientation.Forward].Add(t); } else { ThrusterGroup newThrust = new ThrusterGroup(mode); newThrust.Add(t); temp.Add(t.Orientation.Forward, newThrust); } } thrust = temp.Values.ToList(); pid = new VectorPID(thrustP / factor, thrustI / factor, thrustD / factor, thrustDecay / factor, secondsPerTick); }
/// <summary> /// Solves the required thrusts to go in targetDirection at max speed. This takes gravity into account. /// </summary> /// <param name="g">Gravity and unknown forces</param> /// <param name="targetDirection">The target direction to go in</param> /// <param name="maxPercentageThrustToUse">The maximum amount of thrust devoted to going in the target direction.</param> /// <returns></returns> public ThrusterGroup SolveMaxThrust(Vector3D minus_g, Vector3D targetDirection, double maxPercentageThrustToUse = 1) { //parent_program.Echo("Starting"); Base6Directions.Direction actual2Di; Base6Directions.Direction actual3Di; double t2c; double t3c; double Lambda; ThrusterGroup t1; ThrusterGroup t2; ThrusterGroup t3; if (maxPercentageThrustToUse > 1) { maxPercentageThrustToUse = 1; } else if (maxPercentageThrustToUse < 0) { maxPercentageThrustToUse = 0; } foreach (var entry in thrusterGroups) { //parent_program.Echo("Loading " + entry.Key.ToString()); t1 = entry.Value; if (t1.LocalThrustDirection == Base6Directions.Direction.Up || t1.LocalThrustDirection == Base6Directions.Direction.Down) { t2 = thrusterGroups[Base6Directions.Direction.Left]; t3 = thrusterGroups[Base6Directions.Direction.Forward]; } else if (t1.LocalThrustDirection == Base6Directions.Direction.Left || t1.LocalThrustDirection == Base6Directions.Direction.Right) { t2 = thrusterGroups[Base6Directions.Direction.Up]; t3 = thrusterGroups[Base6Directions.Direction.Forward]; } else if (t1.LocalThrustDirection == Base6Directions.Direction.Forward || t1.LocalThrustDirection == Base6Directions.Direction.Backward) { t2 = thrusterGroups[Base6Directions.Direction.Up]; t3 = thrusterGroups[Base6Directions.Direction.Left]; } else { parent_program.shipIOHandler.Error( "Encountered unusual thruster direction.\nIf you've gotten this error in particular,\nplease report it to the script owner, Spug."); t2 = thrusterGroups[Base6Directions.Direction.Up]; t3 = thrusterGroups[Base6Directions.Direction.Left]; } //var mat = new double[,] { //{ t2.WorldThrustDirection.X, t3.WorldThrustDirection.X, -targetDirection.X }, //{ t2.WorldThrustDirection.Y, t3.WorldThrustDirection.Y, -targetDirection.Y }, //{ t2.WorldThrustDirection.Z, t3.WorldThrustDirection.Z, -targetDirection.Z }, //}; t1.matrixM[0, 0] = t2.WorldThrustDirection.X; t1.matrixM[0, 1] = t3.WorldThrustDirection.X; t1.matrixM[0, 2] = -targetDirection.X; t1.matrixM[1, 0] = t2.WorldThrustDirection.Y; t1.matrixM[1, 1] = t3.WorldThrustDirection.Y; t1.matrixM[1, 2] = -targetDirection.Y; t1.matrixM[2, 0] = t2.WorldThrustDirection.Z; t1.matrixM[2, 1] = t3.WorldThrustDirection.Z; t1.matrixM[2, 2] = -targetDirection.Z; t1.ANS[0] = -t1.MaxThrust * t1.WorldThrustDirection.X * maxPercentageThrustToUse - minus_g.X; t1.ANS[1] = -t1.MaxThrust * t1.WorldThrustDirection.Y * maxPercentageThrustToUse - minus_g.Y; t1.ANS[2] = -t1.MaxThrust * t1.WorldThrustDirection.Z * maxPercentageThrustToUse - minus_g.Z; PID.ComputeCoefficients(t1.matrixM, t1.ANS); t2c = t1.ANS[0]; t3c = t1.ANS[1]; Lambda = t1.ANS[2]; actual2Di = t2.LocalThrustDirection; actual3Di = t3.LocalThrustDirection; if (t2c < 0) { actual2Di = Base6Directions.GetOppositeDirection(t2.LocalThrustDirection); t2c *= -1; } if (t3c < 0) { actual3Di = Base6Directions.GetOppositeDirection(t3.LocalThrustDirection); t3c *= -1; } //// Publish the results t1.finalThrustForces.X = t1.MaxThrust * maxPercentageThrustToUse; t1.finalThrustForces.Y = t2c; t1.finalThrustForces.Z = t3c; t1.lambdaResult = Lambda; t1.finalThrusterGroups[0] = t1; t1.finalThrusterGroups[1] = thrusterGroups[actual2Di]; t1.finalThrusterGroups[2] = thrusterGroups[actual3Di]; t1.finalThrusterGroups[3] = thrusterGroups[Base6Directions.GetOppositeDirection(t1.LocalThrustDirection)]; t1.finalThrusterGroups[4] = thrusterGroups[Base6Directions.GetOppositeDirection(t2.LocalThrustDirection)]; t1.finalThrusterGroups[5] = thrusterGroups[Base6Directions.GetOppositeDirection(t3.LocalThrustDirection)]; //parent_program.Echo("Completed " + entry.Key.ToString()); } ThrusterGroup bestCandidate = null; double bestCandidateLambda = -999999999; foreach (var entry in thrusterGroups) { if (entry.Value.lambdaResult > bestCandidateLambda) { if (entry.Value.finalThrustForces.Y <= entry.Value.finalThrusterGroups[1].MaxThrust + 1 && entry.Value.finalThrustForces.Z <= entry.Value.finalThrusterGroups[2].MaxThrust + 1) { bestCandidate = entry.Value; bestCandidateLambda = entry.Value.lambdaResult; } } } //parent_program.Echo("Finished"); //parent_program.Echo("Best: " + bestCandidate.LocalThrustDirection.ToString()); return(bestCandidate); }
public void Populate6ThrusterGroups() { thrusterGroups = new Dictionary <Base6Directions.Direction, ThrusterGroup>(); ForwardThrust = new ThrusterGroup(parent_program, Base6Directions.Direction.Forward, cockpit); UpThrust = new ThrusterGroup(parent_program, Base6Directions.Direction.Up, cockpit); LeftThrust = new ThrusterGroup(parent_program, Base6Directions.Direction.Left, cockpit); BackwardThrust = new ThrusterGroup(parent_program, Base6Directions.Direction.Backward, cockpit); DownThrust = new ThrusterGroup(parent_program, Base6Directions.Direction.Down, cockpit); RightThrust = new ThrusterGroup(parent_program, Base6Directions.Direction.Right, cockpit); thrusterGroups.Add(Base6Directions.Direction.Forward, ForwardThrust); thrusterGroups.Add(Base6Directions.Direction.Up, UpThrust); thrusterGroups.Add(Base6Directions.Direction.Left, LeftThrust); thrusterGroups.Add(Base6Directions.Direction.Backward, BackwardThrust); thrusterGroups.Add(Base6Directions.Direction.Down, DownThrust); thrusterGroups.Add(Base6Directions.Direction.Right, RightThrust); Vector3D thrusterDirection; double forwardDot = 0; double upDot = 0; double leftDot = 0; //Base6Directions.Direction var unusedDirections = new List <Base6Directions.Direction>(); unusedDirections.Add(Base6Directions.Direction.Forward); unusedDirections.Add(Base6Directions.Direction.Up); unusedDirections.Add(Base6Directions.Direction.Left); unusedDirections.Add(Base6Directions.Direction.Backward); unusedDirections.Add(Base6Directions.Direction.Down); unusedDirections.Add(Base6Directions.Direction.Right); foreach (var thisThruster in thrusters) { if (thisThruster.IsWorking && blockIsOnMyGrid(thisThruster)) { thrusterDirection = -thisThruster.WorldMatrix.Forward; forwardDot = Vector3D.Dot(thrusterDirection, cockpit.WorldMatrix.Forward); upDot = Vector3D.Dot(thrusterDirection, cockpit.WorldMatrix.Up); leftDot = Vector3D.Dot(thrusterDirection, cockpit.WorldMatrix.Left); var foundDirection = Base6Directions.Direction.Forward; var unset = true; if (forwardDot >= 0.97) { foundDirection = Base6Directions.Direction.Forward; unset = false; } else if (leftDot >= 0.97) { foundDirection = Base6Directions.Direction.Left; unset = false; } else if (upDot >= 0.97) { foundDirection = Base6Directions.Direction.Up; unset = false; } else if (forwardDot <= -0.97) { foundDirection = Base6Directions.Direction.Backward; unset = false; } else if (leftDot <= -0.97) { foundDirection = Base6Directions.Direction.Right; unset = false; } else if (upDot <= -0.97) { foundDirection = Base6Directions.Direction.Down; unset = false; } if (!unset) { thrusterGroups[foundDirection].AddThruster(thisThruster); if (unusedDirections.Contains(foundDirection)) { unusedDirections.Remove(foundDirection); } } } } string err_string = ""; if (unusedDirections.Count == 6) { err_string = "Sorry " + parent_program.your_title + ", I couldn't seem to find any thrusters on your ship."; if (off_thrusters_count > 0) { err_string += "\nI have detected that some thrusters\nare disabled. Could this be the problem?"; } parent_program.shipIOHandler.Error(err_string); } else if (unusedDirections.Count == 1) { err_string = "Sorry " + parent_program.your_title + ", it's required that all 6 directions have at least one thruster.\nThe missing direction might be " + unusedDirections[0] + "."; if (off_thrusters_count > 0) { err_string += "\nI have detected that some thrusters\nare disabled. Could this be the problem?"; } parent_program.shipIOHandler.Error(err_string); // } else if (unusedDirections.Count > 1) { var total_string = ""; foreach (var di in unusedDirections) { total_string += di + "-"; } err_string = "Sorry " + parent_program.your_title + ", it's required that all 6 directions have at least one thruster.\nIt seems the missing directions might be: " + total_string; if (off_thrusters_count > 0) { err_string += "\nI have detected that some thrusters\nare disabled. Could this be the problem?"; } parent_program.shipIOHandler.Error(err_string); // } }
void SpawnShip(TextAsset file) { GameObject shipBase = (GameObject)Instantiate(shipCorePrefab, transform.position, transform.rotation); ShipScript ss = shipBase.GetComponent <ShipScript>(); myShip = ss; Transform targ = shipBase.transform.FindChild("CameraPos/CamObj/WeaponsTarget"); int offset = 0; //string[] sts = shipFilePlayer.text.Split('\n'); StreamReader sr = new StreamReader(Application.dataPath + "/" + shipPath); string[] sts = sr.ReadToEnd().Split('\n'); List <string> stringss = new List <string>(); foreach (string s in sts) { string result = Regex.Replace(s, @"\r\n?|\n", ""); stringss.Add(result); } string[] strings = stringss.ToArray(); string shipName = strings[offset]; print(strings[offset]); offset += 1; shipBase.name = shipName; string[] colorPrimary = strings[offset].Split(','); print(strings[offset]); offset += 1; Color cPrimary = new Color(float.Parse(colorPrimary[0]) / 255, float.Parse(colorPrimary[1]) / 255, float.Parse(colorPrimary[2]) / 255, 255); ss.shipColorMain = cPrimary; string[] colorPower = strings[offset].Split(','); print(strings[offset]); offset += 1; Color cPower = new Color(float.Parse(colorPower[0]) / 255, float.Parse(colorPower[1]) / 255, float.Parse(colorPower[2]) / 255, 255); ss.shipColorPower = cPower; string[] colorThrust = strings[offset].Split(','); print(strings[offset]); offset += 1; Color cThrust = new Color(float.Parse(colorThrust[0]) / 255, float.Parse(colorThrust[1]) / 255, float.Parse(colorThrust[2]) / 255, 255); ss.shipColorThrust = cThrust; string[] colorSpecial = strings[offset].Split(','); print(strings[offset]); offset += 1; Color cSpecial = new Color(float.Parse(colorSpecial[0]) / 255, float.Parse(colorSpecial[1]) / 255, float.Parse(colorSpecial[2]) / 255, 255); ss.shipColorSpecial = cSpecial; string[] posOff = strings[offset].Split(','); offset += 1; Vector3 poff = new Vector3(float.Parse(posOff[0]), float.Parse(posOff[1]), float.Parse(posOff[2])); int numSeats = int.Parse(strings[offset]); print(strings[offset]); offset += 1; ss.seats = new BridgeSeat[numSeats]; for (int i = 0; i < numSeats; i++) { ss.seats[i] = new BridgeSeat(); ss.seats[i].name = strings[offset]; ss.seats[i].weaponTarget = targ; print(strings[offset]); offset += 1; ss.seats[i].availableThrusts = new List <int>(); int numThrustsSeat = int.Parse(strings[offset]); print(strings[offset]); offset += 1; for (int n = 0; n < numThrustsSeat; n++) { ss.seats[i].availableThrusts.Add(int.Parse(strings[offset])); print(strings[offset]); offset += 1; } ss.seats[i].weaponSet = int.Parse(strings[offset]); print("WEAPON SET: " + strings[offset]); offset += 1; ss.seats[i].powerSet = int.Parse(strings[offset]); print("POWER SET: " + strings[offset]); offset += 1; int numSeatModules = int.Parse(strings[offset]); print("NUM SEAT MODULES: " + strings[offset]); offset += 1; ss.seats[i].moduleSlots = new int[numSeatModules]; for (int n = 0; n < numSeatModules; n++) { ss.seats[i].moduleSlots[n] = int.Parse(strings[offset]); print(strings[offset]); offset += 1; } } int numThrusts = int.Parse(strings[offset]); print(strings[offset]); offset += 1; ss.tgs = new List <ThrusterGroup>(); for (int i = 0; i < numThrusts; i++) { ThrusterGroup tg = new ThrusterGroup(); tg.name = strings[offset]; print(strings[offset]); offset += 1; tg.axis = strings[offset]; print(strings[offset]); offset += 1; tg.negThrust = new List <Thruster>(); tg.posThrust = new List <Thruster>(); ss.tgs.Add(tg); } int numWeps = int.Parse(strings[offset]); print(strings[offset]); offset += 1; ss.wp = new List <WeaponPair>(); for (int i = 0; i < numWeps; i++) { string wepSetName = strings[offset]; print(strings[offset]); offset += 1; string weaponPrimary = strings[offset]; print(strings[offset]); offset += 1; string weaponSecondary = strings[offset]; print(strings[offset]); offset += 1; WeaponPreset wep1 = weapons[weaponPrimary]; WeaponPreset wep2 = weapons[weaponSecondary]; WeaponPair wp = new WeaponPair(); wp.weapons1 = new List <Weapon>(); wp.weapons2 = new List <Weapon>(); wp.ammo1 = wep1.ammo; wp.ammoMax1 = wep1.ammo; wp.ammo2 = wep2.ammo; wp.ammoMax2 = wep2.ammo; wp.firerate1 = wep1.firerate; wp.reloadSpeed1 = wep1.reloadSpeed; wp.firerate2 = wep2.firerate; wp.reloadSpeed2 = wep2.reloadSpeed; wp.name = wepSetName; ss.wp.Add(wp); } int numPowerSys = int.Parse(strings[offset]); print(strings[offset]); offset += 1; ss.powerSys = new List <PowerSystem>(); for (int i = 0; i < numPowerSys; i++) { string powName = strings[offset]; print(strings[offset]); offset += 1; int numModules = int.Parse(strings[offset]); print(strings[offset]); offset += 1; PowerSystem ps = new PowerSystem(); ps.name = powName; ps.slots = new List <AuxPowerSlot>(); for (int n = 0; n < numModules; n++) { string modType = strings[offset]; print(strings[offset]); offset += 1; float mod = float.Parse(strings[offset]); print(strings[offset]); offset += 1; AuxPowerSlot aps = new AuxPowerSlot(); if (modType == "Shield") { aps.mtype = AuxPowerSlot.ModuleType.Shield; } else if (modType == "Thrust") { aps.mtype = AuxPowerSlot.ModuleType.Thrust; } else if (modType == "Weapon") { aps.mtype = AuxPowerSlot.ModuleType.Weapon; } aps.mod = mod; ps.slots.Add(aps); } ss.powerSys.Add(ps); } int numModulesReal = int.Parse(strings[offset]); print(strings[offset]); offset += 1; ss.slots = new List <ModuleSlot>(); for (int i = 0; i < numModulesReal; i++) { string modName = strings[offset]; print(strings[offset]); offset += 1; string modType = strings[offset]; print(strings[offset]); offset += 1; int powerPower = int.Parse(strings[offset]); print(strings[offset]); offset += 1; int timePower = int.Parse(strings[offset]); print(strings[offset]); offset += 1; int coolPower = int.Parse(strings[offset]); print(strings[offset]); offset += 1; float power = 1 + (powerPower - 20) * 0.05f; float time = 1 + (timePower - 10) * 0.1f; float cool = 1 + (coolPower - 20) * -0.025f; ModuleSlot ms = new ModuleSlot(); ms.name = modName; ms.modPower = power; ms.modTime = time; ms.modCooldown = cool; ms.mtype = modules[modType]; ms.modules = new List <Module>(); ss.slots.Add(ms); } float offsetX = float.Parse(strings[offset]); print(strings[offset]); offset += 1; float offsetY = float.Parse(strings[offset]); print(strings[offset]); offset += 1; float offsetZ = float.Parse(strings[offset]); print(strings[offset]); offset += 1; shipBase.transform.GetChild(0).localPosition = new Vector3(offsetX, offsetY, offsetZ); float camrange = float.Parse(strings[offset]); print(strings[offset]); offset += 1; shipBase.transform.FindChild("CameraPos/CamObj/Main Camera").localPosition = new Vector3(0, 0, -camrange); float range = float.Parse(strings[offset]); print(strings[offset]); offset += 1; targ.localPosition = new Vector3(0, 0, range); int numBlocks = int.Parse(strings[offset]); print(strings[offset]); offset += 1; ss.blocks = new List <Block>(); for (int i = 0; i < numBlocks; i++) { string[] bb = strings[offset].Split('/'); print(strings[offset]); offset += 1; string blockType = bb[0]; string[] position = bb[1].Split(','); string[] rotation = bb[2].Split(','); Vector3 pos = new Vector3(float.Parse(position[0]), float.Parse(position[1]), float.Parse(position[2])); Vector3 ea = new Vector3(float.Parse(rotation[0]), float.Parse(rotation[1]), float.Parse(rotation[2])); Quaternion rot = new Quaternion(); rot.eulerAngles = ea; GameObject blockObj = (GameObject)Instantiate(blocks[blockType], Vector3.zero, Quaternion.identity); blockObj.transform.parent = shipBase.transform; blockObj.transform.localPosition = pos + poff; blockObj.transform.localRotation = rot; Block b = blockObj.GetComponent <Block>(); b.powerSys = int.Parse(bb[3]); if (b is Weapon) { Weapon w = (Weapon)b; w.weaponGroupID = int.Parse(bb[4]); w.oneTwo = int.Parse(bb[5]) == 1; } if (b is Module) { Module m = (Module)b; m.moduleSlot = int.Parse(bb[4]); } if (b is AuxModule) { AuxModule am = (AuxModule)b; am.moduleSlot = int.Parse(bb[4]); } if (b is Thruster) { Thruster t = (Thruster)b; t.thrustGroup = int.Parse(bb[4]); t.posNeg = int.Parse(bb[5]) == 1; } } sr.Close(); }