Skip to content

Commit

Permalink
Merge pull request PapaJoesSoup#81 from BrettRyland/dev
Browse files Browse the repository at this point in the history
sync
  • Loading branch information
SuicidalInsanity authored Jun 8, 2023
2 parents 81da431 + bb8c57c commit 404342f
Show file tree
Hide file tree
Showing 37 changed files with 427 additions and 113 deletions.
4 changes: 2 additions & 2 deletions BDArmory/BDArmory.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@
<None Include="Distribution\GameData\BDArmory\FX\jetdriveWake.mu" />
<None Include="Distribution\GameData\BDArmory\FX\PenFX.mu" />
<None Include="Distribution\GameData\BDArmory\FX\TorpWake.mu" />
<None Include="Distribution\GameData\BDArmory\KSPedia\bdac addons.ksp" />
<!-- <None Include="Distribution\GameData\BDArmory\KSPedia\bdac addons.ksp" />
<None Include="Distribution\GameData\BDArmory\KSPedia\bdac ammunition.ksp" />
<None Include="Distribution\GameData\BDArmory\KSPedia\bdac control systems.ksp" />
<None Include="Distribution\GameData\BDArmory\KSPedia\bdac countermeasures.ksp" />
Expand All @@ -383,7 +383,7 @@
<None Include="Distribution\GameData\BDArmory\KSPedia\bdac ordinance.ksp" />
<None Include="Distribution\GameData\BDArmory\KSPedia\bdac radar and targeting.ksp" />
<None Include="Distribution\GameData\BDArmory\KSPedia\bdac settings.ksp" />
<None Include="Distribution\GameData\BDArmory\KSPedia\bdac turrets.ksp" />
<None Include="Distribution\GameData\BDArmory\KSPedia\bdac turrets.ksp" /> -->
<None Include="Distribution\GameData\BDArmory\Localization\BDArmory.cfg" />
<None Include="Distribution\GameData\BDArmory\Localization\localization-en-us.cfg" />
<None Include="Distribution\GameData\BDArmory\Localization\localization-zh-cn.cfg" />
Expand Down
18 changes: 9 additions & 9 deletions BDArmory/Competition/Scoring.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public bool RegisterBulletHit(string attacker, string victim, string weaponName
if (ScoreData[victim].lastPersonWhoDamagedMe != attacker)
{
ScoreData[victim].previousLastDamageTime = ScoreData[victim].lastDamageTime;
ScoreData[victim].previousPersonWheDamagedMe = ScoreData[victim].lastPersonWhoDamagedMe;
ScoreData[victim].previousPersonWhoDamagedMe = ScoreData[victim].lastPersonWhoDamagedMe;
}
if (ScoreData[victim].hitCounts.ContainsKey(attacker)) { ++ScoreData[victim].hitCounts[attacker]; }
else { ScoreData[victim].hitCounts[attacker] = 1; }
Expand Down Expand Up @@ -244,7 +244,7 @@ public bool RegisterRocketHit(string attacker, string victim, int partsHit = 1)
if (ScoreData[victim].lastPersonWhoDamagedMe != attacker)
{
ScoreData[victim].previousLastDamageTime = ScoreData[victim].lastDamageTime;
ScoreData[victim].previousPersonWheDamagedMe = ScoreData[victim].lastPersonWhoDamagedMe;
ScoreData[victim].previousPersonWhoDamagedMe = ScoreData[victim].lastPersonWhoDamagedMe;
}
if (ScoreData[victim].rocketPartDamageCounts.ContainsKey(attacker)) { ScoreData[victim].rocketPartDamageCounts[attacker] += partsHit; }
else { ScoreData[victim].rocketPartDamageCounts[attacker] = partsHit; }
Expand Down Expand Up @@ -326,12 +326,12 @@ public bool RegisterRam(string attacker, string victim, double timeOfCollision,
if (ScoreData[victim].lastDamageTime < timeOfCollision && ScoreData[victim].lastPersonWhoDamagedMe != attacker)
{
ScoreData[victim].previousLastDamageTime = ScoreData[victim].lastDamageTime;
ScoreData[victim].previousPersonWheDamagedMe = ScoreData[victim].lastPersonWhoDamagedMe;
ScoreData[victim].previousPersonWhoDamagedMe = ScoreData[victim].lastPersonWhoDamagedMe;
}
else if (ScoreData[victim].previousLastDamageTime < timeOfCollision && !string.IsNullOrEmpty(ScoreData[victim].previousPersonWheDamagedMe) && ScoreData[victim].previousPersonWheDamagedMe != attacker) // Newer than the current previous last damage, but older than the most recent damage from someone else.
else if (ScoreData[victim].previousLastDamageTime < timeOfCollision && !string.IsNullOrEmpty(ScoreData[victim].previousPersonWhoDamagedMe) && ScoreData[victim].previousPersonWhoDamagedMe != attacker) // Newer than the current previous last damage, but older than the most recent damage from someone else.
{
ScoreData[victim].previousLastDamageTime = timeOfCollision;
ScoreData[victim].previousPersonWheDamagedMe = attacker;
ScoreData[victim].previousPersonWhoDamagedMe = attacker;
}
if (ScoreData[victim].rammingPartLossCounts.ContainsKey(attacker)) { ScoreData[victim].rammingPartLossCounts[attacker] += partsLost; }
else { ScoreData[victim].rammingPartLossCounts[attacker] = partsLost; }
Expand Down Expand Up @@ -395,7 +395,7 @@ public bool RegisterMissileHit(string attacker, string victim, int partsHit = 1)
if (ScoreData[victim].lastPersonWhoDamagedMe != attacker)
{
ScoreData[victim].previousLastDamageTime = ScoreData[victim].lastDamageTime;
ScoreData[victim].previousPersonWheDamagedMe = ScoreData[victim].lastPersonWhoDamagedMe;
ScoreData[victim].previousPersonWhoDamagedMe = ScoreData[victim].lastPersonWhoDamagedMe;
}
if (ScoreData[victim].missilePartDamageCounts.ContainsKey(attacker)) { ScoreData[victim].missilePartDamageCounts[attacker] += partsHit; }
else { ScoreData[victim].missilePartDamageCounts[attacker] = partsHit; }
Expand Down Expand Up @@ -488,7 +488,7 @@ public bool RegisterDeath(string vesselName, GMKillReason gmKillReason = GMKillR
return true;
}

if (now - ScoreData[vesselName].lastDamageTime < BDArmorySettings.SCORING_HEADSHOT && ScoreData[vesselName].gmKillReason == GMKillReason.None) // Died shortly after being hit (and not by the GM)
if (now - ScoreData[vesselName].lastDamageTime < BDArmorySettings.SCORING_HEADSHOT && ScoreData[vesselName].gmKillReason == GMKillReason.None && ScoreData[vesselName].lastDamageWasFrom != DamageFrom.Asteroids) // Died shortly after being hit (and not by the GM or asteroids)
{
if (ScoreData[vesselName].previousLastDamageTime < 0) // No-one else hit them => Clean kill
{ ScoreData[vesselName].aliveState = AliveState.CleanKill; }
Expand Down Expand Up @@ -529,7 +529,7 @@ public bool RegisterAsteroidCollision(string victim, int partsDestroyed)
if (ScoreData[victim].lastPersonWhoDamagedMe != attacker)
{
ScoreData[victim].previousLastDamageTime = ScoreData[victim].lastDamageTime;
ScoreData[victim].previousPersonWheDamagedMe = ScoreData[victim].lastPersonWhoDamagedMe;
ScoreData[victim].previousPersonWhoDamagedMe = ScoreData[victim].lastPersonWhoDamagedMe;
}
ScoreData[victim].partsLostToAsteroids += partsDestroyed;
ScoreData[victim].lastDamageTime = now;
Expand Down Expand Up @@ -981,7 +981,7 @@ public struct WaypointReached
public DamageFrom lastDamageWasFrom = DamageFrom.None;
public string lastPersonWhoDamagedMe = "";
public double previousLastDamageTime = -1;
public string previousPersonWheDamagedMe = "";
public string previousPersonWhoDamagedMe = "";
public int deathOrder = -1;
public double deathTime = -1;
public HashSet<DamageFrom> damageTypesTaken = new HashSet<DamageFrom>();
Expand Down
64 changes: 42 additions & 22 deletions BDArmory/Control/BDModulePilotAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,11 @@ Transform velocityTransform
Vector3 upDirection = Vector3.up;

#region Pilot AI Settings GUI

#region PID
[KSPField(isPersistant = true, guiActive = true, guiActiveEditor = true, guiName = "#LOC_BDArmory_SteerFactor", //Steer Factor
groupName = "pilotAI_PID", groupDisplayName = "#LOC_BDArmory_PilotAI_PID", groupStartCollapsed = true),
UI_FloatRange(minValue = 0.1f, maxValue = 20f, stepIncrement = 0.1f, scene = UI_Scene.All)]
public float steerMult = 14f;
//make a combat steer mult and idle steer mult

[KSPField(isPersistant = true, guiActive = true, guiActiveEditor = true, guiName = "#LOC_BDArmory_SteerKi", //Steer Ki
groupName = "pilotAI_PID", groupDisplayName = "#LOC_BDArmory_PilotAI_PID", groupStartCollapsed = true),
Expand All @@ -140,6 +138,18 @@ Transform velocityTransform
public float steerDamping = 5f;

#region Dynamic Damping
//Toggle Dynamic Steer Damping
[KSPField(isPersistant = true, guiActive = true, guiActiveEditor = true, guiName = "#LOC_BDArmory_DynamicSteerDamping", advancedTweakable = true,
groupName = "pilotAI_PID", groupDisplayName = "#LOC_BDArmory_PilotAI_PID", groupStartCollapsed = true),
UI_Toggle(scene = UI_Scene.All, disabledText = "#LOC_BDArmory_Disabled", enabledText = "#LOC_BDArmory_Enabled")]
public bool dynamicSteerDamping = false;

//Toggle 3-Axis Dynamic Steer Damping
[KSPField(isPersistant = true, guiActive = true, guiActiveEditor = true, guiName = "#LOC_BDArmory_3AxisDynamicSteerDamping", advancedTweakable = true,
groupName = "pilotAI_PID", groupDisplayName = "#LOC_BDArmory_PilotAI_PID", groupStartCollapsed = true),
UI_Toggle(enabledText = "#LOC_BDArmory_Enabled", disabledText = "#LOC_BDArmory_Disabled", scene = UI_Scene.All)]
public bool CustomDynamicAxisFields = true;

// Note: min/max is replaced by off-target/on-target in localisation, but the variable names are kept to avoid reconfiguring existing craft.
// Dynamic Damping
[KSPField(guiName = "#LOC_BDArmory_DynamicDamping", groupName = "pilotAI_PID", groupDisplayName = "#LOC_BDArmory_PilotAI_PID", groupStartCollapsed = true), UI_Label(scene = UI_Scene.All)]
Expand Down Expand Up @@ -231,18 +241,6 @@ Transform velocityTransform
groupName = "pilotAI_PID", groupDisplayName = "#LOC_BDArmory_PilotAI_PID", groupStartCollapsed = true),
UI_FloatRange(minValue = 0.1f, maxValue = 10f, stepIncrement = 0.1f, scene = UI_Scene.All)]
public float dynamicSteerDampingRollFactor = 8f;

//Toggle Dynamic Steer Damping
[KSPField(isPersistant = true, guiActive = true, guiActiveEditor = true, guiName = "#LOC_BDArmory_DynamicSteerDamping", advancedTweakable = true,
groupName = "pilotAI_PID", groupDisplayName = "#LOC_BDArmory_PilotAI_PID", groupStartCollapsed = true),
UI_Toggle(scene = UI_Scene.All, disabledText = "#LOC_BDArmory_Disabled", enabledText = "#LOC_BDArmory_Enabled")]
public bool dynamicSteerDamping = false;

//Toggle 3-Axis Dynamic Steer Damping
[KSPField(isPersistant = true, guiActive = true, guiActiveEditor = true, guiName = "#LOC_BDArmory_3AxisDynamicSteerDamping", advancedTweakable = true,
groupName = "pilotAI_PID", groupDisplayName = "#LOC_BDArmory_PilotAI_PID", groupStartCollapsed = true),
UI_Toggle(enabledText = "#LOC_BDArmory_Enabled", disabledText = "#LOC_BDArmory_Disabled", scene = UI_Scene.All)]
public bool CustomDynamicAxisFields = true;
#endregion

#region AutoTuning
Expand Down Expand Up @@ -565,7 +563,7 @@ Transform velocityTransform
public float terrainAvoidanceCriticalAngle = 135f;
float terrainAvoidanceCriticalCosAngle = -0.5f;

[KSPField(isPersistant = true, guiActive = true, guiActiveEditor = true, guiName = "#LOC_BDArmory_TerrainAvoidanceControlSurfaceDeployTime", advancedTweakable = true, // Control surface deployment time.
[KSPField(isPersistant = true, guiActive = true, guiActiveEditor = true, guiName = "#LOC_BDArmory_TerrainAvoidanceVesselReactionTime", advancedTweakable = true, // Vessel reaction time.
groupName = "pilotAI_Terrain", groupDisplayName = "#LOC_BDArmory_PilotAI_Terrain", groupStartCollapsed = true),
UI_FloatRange(minValue = 0f, maxValue = 4f, stepIncrement = 0.1f, scene = UI_Scene.All)]
public float controlSurfaceDeploymentTime = 2f;
Expand Down Expand Up @@ -623,7 +621,6 @@ Transform velocityTransform
{ nameof(maxAllowedGForce), 1000f },
{ nameof(maxAllowedAoA), 180f },
{ nameof(postStallAoA), 180f },
// { nameof(extendMult), 200f },
{ nameof(extendDistanceAirToAir), 20000f },
{ nameof(extendAngleAirToAir), 90f },
{ nameof(extendDistanceAirToGroundGuns), 20000f },
Expand All @@ -635,6 +632,7 @@ Transform velocityTransform
{ nameof(vesselStandoffDistance), 5000f },
{ nameof(turnRadiusTwiddleFactorMin), 10f},
{ nameof(turnRadiusTwiddleFactorMax), 10f},
{ nameof(controlSurfaceDeploymentTime), 10f },
{ nameof(controlSurfaceLag), 1f},
{ nameof(DynamicDampingMin), 100f },
{ nameof(DynamicDampingMax), 100f },
Expand Down Expand Up @@ -727,6 +725,11 @@ void StoreSettings(string vesselName)
var fields = typeof(BDModulePilotAI).GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
foreach (var field in fields)
{
if (field.FieldType == typeof(PIDAutoTuning) || field.FieldType == typeof(Vessel)) // Skip fields that are references to other objects that ought to revert to null.
{
if (BDArmorySettings.DEBUG_AI) Debug.Log($"[BDArmory.BDModulePilotAI]: Skipping {field.Name} of type {field.FieldType} as it's a reference type.");
continue;
}
storedSettings[vesselName].Add(new System.Tuple<string, object>(field.Name, field.GetValue(this)));
}
Events["RestoreSettings"].active = true;
Expand Down Expand Up @@ -1137,7 +1140,7 @@ public void ClampFields(string fieldName)
autoTuningSpeed = Mathf.Clamp(autoTuningSpeed, minSpeed, maxSpeed); // Keep the auto-tuning speed within the combat speed range.
break;
default:
Debug.LogError($"[BDArmory.BDModulePilotAI]: Invalid altitude {fieldName} in ClampAltitudes.");
Debug.LogError($"[BDArmory.BDModulePilotAI]: Invalid field name {fieldName} in ClampFields.");
break;
}
}
Expand All @@ -1160,6 +1163,10 @@ public void ToggleDynamicDampingFields()
DampingFactor.guiActiveEditor = dynamicSteerDamping && !CustomDynamicAxisFields;

// 3-axis dynamic damping
var CustomDynamicAxisToggleField = Fields["CustomDynamicAxisFields"];
CustomDynamicAxisToggleField.guiActive = dynamicSteerDamping;
CustomDynamicAxisToggleField.guiActiveEditor = dynamicSteerDamping;

var DynamicPitchLabel = Fields["PitchLabel"];
var DynamicDampingPitch = Fields["dynamicDampingPitch"];
var DynamicDampingPitchMaxField = Fields["DynamicDampingPitchMax"];
Expand Down Expand Up @@ -1498,6 +1505,13 @@ IEnumerator FixFieldOrdering(string groupName, string startFieldName = null)
dirtyPAW_PID = false;
fixFieldOrderingRunning = false;
}

void PAWFirstOpened(UIPartActionWindow paw, Part p) // Fix the ordering of fields when the PAW is first opened. This is required since KSP messes up the field ordering if the first KSPField is in a collapsed group.
{
if (p != part) return;
dirtyPAW_PID = true;
GameEvents.onPartActionUIShown.Remove(PAWFirstOpened);
}
#endregion

protected override void Start()
Expand Down Expand Up @@ -1566,15 +1580,21 @@ protected override void Start()
Events["RestoreControlSurfaceSettings"].active = true;
}
}
GameEvents.onPartActionUIShown.Add(PAWFirstOpened);
}

protected override void OnDestroy()
{
GameEvents.onPartActionUIShown.Remove(PAWFirstOpened);
GameEvents.onVesselPartCountChanged.Remove(UpdateTerrainAlertDetectionRadius);
if (autoTune && pidAutoTuning is not null) // If we were auto-tuning, revert to the best values and store them.
if (autoTune)
{
pidAutoTuning.RevertPIDValues();
StoreSettings(pidAutoTuning.vesselName);
if (pidAutoTuning is not null) // If we were auto-tuning, revert to the best values and store them.
{
pidAutoTuning.RevertPIDValues();
StoreSettings(pidAutoTuning.vesselName);
}
OtherUtils.SetTimeOverride(false); // Make sure we disable the Time Override if we were auto-tuning.
}
base.OnDestroy();
}
Expand Down Expand Up @@ -2413,7 +2433,7 @@ void FlyToPosition(FlightCtrlState s, Vector3 targetPosition, bool overrideThrot
}
}
else if (belowMinAltitude && !gainAltInhibited)
rollTarget = vessel.upAxis * 100;
rollTarget = Vector3.Lerp(BodyUtils.GetSurfaceNormal(vesselTransform.position), upDirection, (float)vessel.radarAltitude / minAltitude) * 100; // Adjust the roll target smoothly from the surface normal to upwards to avoid clipping wings into terrain on take-off.
else if (!avoidingTerrain && vessel.verticalSpeed < 0 && Vector3.Dot(rollTarget, upDirection) < 0 && Vector3.Dot(rollTarget, vessel.Velocity()) < 0) // If we're not avoiding terrain, heading downwards and the roll target is behind us and downwards, check that a circle arc of radius "turn radius" (scaled by twiddle factor minimum) tilted at angle of rollTarget has enough room to avoid hitting the ground.
{
// The following calculates the altitude required to turn in the direction of the rollTarget based on the current velocity and turn radius.
Expand Down Expand Up @@ -2680,7 +2700,7 @@ void FlyExtend(FlightCtrlState s, Vector3 tPosition)

Vector3 targetDirection = extendVector.normalized * extendDistance;
Vector3 target = vessel.transform.position + targetDirection; // Target extend position horizontally.
target = GetTerrainSurfacePosition(target) + (vessel.upAxis * Mathf.Min(defaultAltitude, MissileGuidance.GetRaycastRadarAltitude(vesselTransform.position))); // Adjust for terrain changes at target extend position.
target += upDirection * (Mathf.Min(defaultAltitude, BodyUtils.GetRadarAltitudeAtPos(vesselTransform.position)) - BodyUtils.GetRadarAltitudeAtPos(target)); // Adjust for terrain changes at target extend position.
target = FlightPosition(target, desiredMinAltitude); // Further adjustments for speed, situation, etc. and desired minimum altitude after extending.
if (regainEnergy)
{
Expand Down
2 changes: 1 addition & 1 deletion BDArmory/Control/MissileFire.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6343,7 +6343,7 @@ void GuardMode()

if (firedMissiles < maxMissilesOnTarget)
{
if (CurrentMissile.TargetingMode == MissileBase.TargetingModes.Radar && _radarsEnabled && !CurrentMissile.radarLOAL && MaxradarLocks < multiMissileTgtNum)
if (CurrentMissile.TargetingMode == MissileBase.TargetingModes.Radar && _radarsEnabled && !CurrentMissile.radarLOAL && MaxradarLocks <= vesselRadarData.GetLockedTargets().Count)
{
launchAuthorized = false; //don't fire SARH if radar can't support the needed radar lock
if (BDArmorySettings.DEBUG_MISSILES) Debug.Log("[BDArmory.MissileFire]: radar lock number exceeded to launch!");
Expand Down
Loading

0 comments on commit 404342f

Please sign in to comment.