forked from illectro/BDArmory
-
Notifications
You must be signed in to change notification settings - Fork 32
Laser Update #785
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
josuenos
wants to merge
2
commits into
BrettRyland:dev
Choose a base branch
from
josuenos:dev
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Laser Update #785
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -808,6 +808,7 @@ public float GetEngageRange() | |
| public float tanAngle = 0.0001f; | ||
| //Angle of divergeance/2. Theoretical minimum value calculated using θ = (1.22 L/RL)/2, | ||
| //where L is laser's wavelength and RL is the radius of the mirror (=gun). | ||
| private float microwaveDirectivity; // Directivity (gain) for microwave weapons, calculated from tanAngle variable | ||
|
|
||
| //audioclip paths | ||
| [KSPField] | ||
|
|
@@ -2679,7 +2680,7 @@ private void LaserBeam(string vesselname) | |
| if (!useRippleFire || !pulseLaser || fireState.Length == 1 || (useRippleFire && i == barrelIndex)) | ||
| { | ||
| float damage = 0; | ||
| float initialDamage = laserDamage * 0.425f; | ||
| float initialDamage = laserDamage; | ||
| Transform tf = fireTransforms[i]; | ||
| LineRenderer lr = laserRenderers[i]; | ||
| Vector3 rayDirection = tf.forward; | ||
|
|
@@ -2735,9 +2736,10 @@ private void LaserBeam(string vesselname) | |
|
|
||
| KerbalEVA eva = hit.collider.gameObject.GetComponentUpwards<KerbalEVA>(); | ||
| Part p = eva ? eva.part : hit.collider.gameObject.GetComponentInParent<Part>(); | ||
| float distance = hit.distance; | ||
| initialDamage = (laserDamage > 0) ? LaserDamage(laserDamage, tanAngle, distance, tf.position, hit.point) : laserDamage; | ||
| if (p && p.vessel && p.vessel != vessel) | ||
| { | ||
| float distance = hit.distance; | ||
| if (instagib) | ||
| { | ||
| p.AddInstagibDamage(); | ||
|
|
@@ -2754,7 +2756,7 @@ private void LaserBeam(string vesselname) | |
| ///////////////////////////////////////////////// | ||
| if (!VesselModuleRegistry.IgnoredVesselTypes.Contains(p.vessel.vesselType)) | ||
| { | ||
| float EMPDamage = laserDamage * (pulseLaser ? 1 : TimeWarp.fixedDeltaTime) * (BDArmorySettings.DMG_MULTIPLIER / 100); | ||
| float EMPDamage = initialDamage * (pulseLaser ? 1 : TimeWarp.fixedDeltaTime) * (BDArmorySettings.DMG_MULTIPLIER / 100); | ||
| Part closestCommand = null; | ||
| float distToCommandSqr = float.PositiveInfinity; //lets find out which command part is closest to the hit | ||
| Vector3 commandDir = Vector3.zero; | ||
|
|
@@ -2884,9 +2886,6 @@ private void LaserBeam(string vesselname) | |
| HitpointTracker armor = p.GetComponent<HitpointTracker>(); | ||
| if (laserDamage > 0) | ||
| { | ||
| var angularSpread = tanAngle * distance; //Scales down the damage based on the increased surface area of the area being hit by the laser. Think flashlight on a wall. | ||
| initialDamage = laserDamage / (1 + Mathf.PI * angularSpread * angularSpread) * 0.425f; | ||
|
|
||
| if (armor != null)// technically, lasers shouldn't do damage until armor gone, but that would require localized armor tracking instead of the monolithic model currently used | ||
| { | ||
| damage = (initialDamage * (pulseLaser ? 1 : TimeWarp.fixedDeltaTime)) * Mathf.Clamp((1 - (BDAMath.Sqrt(armor.Diffusivity * (armor.Density / 1000)) * armor.Armor) / initialDamage), 0.005f, 1); //old calc lacked a clamp, could potentially become negative damage | ||
|
|
@@ -2985,13 +2984,11 @@ private void LaserBeam(string vesselname) | |
| else | ||
| { | ||
| if (electroLaser || HeatRay) continue; | ||
| var angularSpread = tanAngle * hit.distance; //Scales down the damage based on the increased surface area of the area being hit by the laser. Think flashlight on a wall. | ||
| initialDamage = laserDamage / (1 + Mathf.PI * angularSpread * angularSpread) * 0.425f; | ||
| if (!BDArmorySettings.PAINTBALL_MODE) ProjectileUtils.CheckBuildingHit(hit, initialDamage, pulseLaser); | ||
| if (HEpulses) | ||
| { | ||
| ExplosionFx.CreateExplosion(hit.point, | ||
| (laserDamage / 10000), | ||
| (initialDamage / 10000), | ||
| explModelPath, explSoundPath, ExplosionSourceType.Bullet, 1, null, vessel.vesselName, null); | ||
| } | ||
| } | ||
|
|
@@ -3011,7 +3008,7 @@ private void LaserBeam(string vesselname) | |
| if (HEpulses) | ||
| { | ||
| ExplosionFx.CreateExplosion(tf.position + rayDirection * raycastDistance, | ||
| (laserDamage / 10000), | ||
| (initialDamage / 10000), | ||
| explModelPath, explSoundPath, ExplosionSourceType.Bullet, 1, null, vessel.vesselName, null); | ||
| } | ||
| } | ||
|
|
@@ -3028,6 +3025,21 @@ private void LaserBeam(string vesselname) | |
| } | ||
| } | ||
|
|
||
| // Adjusts laser damage based on tanAngle, distance, firing and hit points | ||
| private float LaserDamage(float laserDamage, float tanAngle, float distance, Vector3 firingPoint, Vector3 hitPoint) | ||
| { | ||
| bool underwater = FlightGlobals.currentMainBody.ocean && FlightGlobals.getAltitudeAtPos(firingPoint) < 0 || FlightGlobals.currentMainBody.ocean && FlightGlobals.getAltitudeAtPos(hitPoint) < 0; | ||
| float firingDensity = (float)FlightGlobals.getAtmDensity(FlightGlobals.getStaticPressure(firingPoint), FlightGlobals.getExternalTemperature(firingPoint)); | ||
| float hitDensity = (float)FlightGlobals.getAtmDensity(FlightGlobals.getStaticPressure(hitPoint), FlightGlobals.getExternalTemperature(hitPoint)); | ||
| float atmDensity = (firingDensity + hitDensity) / 2f; // Average densities instead of complicated integral along firing path | ||
|
|
||
| float gamma = underwater ? BDArmorySettings.LASER_WATER_GAMMA : BDArmorySettings.LASER_ATM_GAMMA; | ||
| float normDensity = underwater ? 1f : Mathf.Max(0f, atmDensity / 1.225f); | ||
| float transmission = Mathf.Exp(-gamma * distance * normDensity); | ||
| float angularSpread = tanAngle * distance; //Scales down the damage based on the increased surface area of the area being hit by the laser. Think flashlight on a wall. | ||
| return laserDamage * transmission / (1 + Mathf.PI * angularSpread * angularSpread); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
|
||
| //Conic AoE 'beam' for AoE beam weapons - tractor beams, microwave EMP, heatrays, etc. | ||
| private void MicrowaveBeam(string vesselname) | ||
| { | ||
|
|
@@ -3041,7 +3053,7 @@ private void MicrowaveBeam(string vesselname) | |
| } | ||
| WeaponFX(); | ||
| float damage = 0; | ||
| float initialDamage = laserDamage * 0.425f; | ||
| float initialDamage = laserDamage; | ||
| var beamLength = engageRangeMax / 1000; | ||
| var beamAngle = Mathf.Tan(beamFOV * Mathf.Deg2Rad) * beamLength; //assumes a default 1km long, 45deg angle cone model | ||
| //Also, TOD - add conic AoE ingo to the GetInfo weapon infocard | ||
|
|
@@ -3066,19 +3078,18 @@ private void MicrowaveBeam(string vesselname) | |
| if (VesselModuleRegistry.IgnoredVesselTypes.Contains(loadedvessels.Current.vesselType)) continue; | ||
|
|
||
| if (Vector3.Angle(loadedvessels.Current.CoM - fireTransforms[0].transform.position, fireTransforms[0].forward) > beamFOV / 2f) continue; | ||
| if (loadedvessels.Current.IsUnderwater()) continue; //would microwaves work underwater...? | ||
| if (loadedvessels.Current.IsUnderwater()) continue; // Microwaves don't work underwater | ||
| if (!friendlyFire) //don't affect friendly targets. Something something phased array dynamic beam shaping | ||
| { | ||
| var wms = VesselModuleRegistry.GetModule<MissileFire>(loadedvessels.Current); | ||
| if (wms == null || wms.Team == WeaponManager.Team) continue; | ||
| } | ||
| if (!loadedvessels.Current.Splashed && vessel.IsUnderwater()) //not sure if a water transition should serve as a barrier, but easily commented out | ||
| if (!loadedvessels.Current.Splashed && vessel.IsUnderwater()) //water transition is barrier | ||
| continue; | ||
| if (loadedvessels.Current == vessel) continue; | ||
| float distance = (loadedvessels.Current.CoM - fireTransforms[0].transform.position).magnitude; | ||
| if (distance > maxTargetingRange) continue; | ||
| var angularSpread = tanAngle * distance; //Scales down the damage based on the increased surface area of the area being hit by the laser. Think flashlight on a wall. | ||
| initialDamage = (laserDamage * 0.425f) / (1 + Mathf.PI * angularSpread * angularSpread); | ||
| initialDamage = MicrowaveDamage(laserDamage, microwaveDirectivity, distance); | ||
|
|
||
| if (electroLaser) | ||
| { | ||
|
|
@@ -3178,7 +3189,7 @@ private void MicrowaveBeam(string vesselname) | |
| if (HEpulses) | ||
| { | ||
| ExplosionFx.CreateExplosion(h.point, | ||
| (laserDamage / 10000), | ||
| (initialDamage / 10000), | ||
| explModelPath, explSoundPath, ExplosionSourceType.Bullet, 1, null, vessel.vesselName, null, Hitpart: hitPart); | ||
| } | ||
| if (HeatRay) | ||
|
|
@@ -3250,6 +3261,15 @@ private void MicrowaveBeam(string vesselname) | |
| } | ||
| } | ||
|
|
||
| // Adjusts microwwave damage based on tanAngle, distance, firing and directivity (gain) | ||
| private float MicrowaveDamage(float baseDamage, float directivity, float distance) | ||
| { | ||
| float wavelength = 0.124913524166667f; // 2.4 GHz wavelength, 299792458f / 2400000000 | ||
| float sqrTerm = wavelength / (4 * Mathf.PI * Mathf.Max(distance, 1f)); | ||
| float finalDamage = baseDamage * Mathf.Min(directivity * (sqrTerm * sqrTerm), 1000f); // Clamp max damage at very short ranges, https://en.wikipedia.org/wiki/Friis_transmission_equation | ||
| return finalDamage; | ||
| } | ||
|
|
||
| public void SetupLaserSpecifics() | ||
| { | ||
| //chargeSound = SoundUtils.GetAudioClip(chargeSoundPath); | ||
|
|
@@ -3259,6 +3279,7 @@ public void SetupLaserSpecifics() | |
| } | ||
| Color laserColor = GUIUtils.ParseColor255(projectileColor); | ||
| laserColor.a = laserColor.a / 2; | ||
| microwaveDirectivity = 2f / (1 - Mathf.Cos(Mathf.Atan(tanAngle))); // Directivity for microwaves, based on conical beam https://en.wikipedia.org/wiki/Directivity#Relation_to_beam_width | ||
| if (conicAoE) | ||
| { | ||
| var cone = GameDatabase.Instance.GetModel(laserModelPath); | ||
|
|
@@ -6144,8 +6165,7 @@ IEnumerator KillIncomingProjectile(PooledBullet shell, PooledRocket rocket) | |
| if (delayTime < 0) | ||
| { | ||
| delayTime = rocket != null ? 0.5f : (shell.bulletMass * (1 - Mathf.Clamp(shell.tntMass / shell.bulletMass, 0f, 0.95f) / 2)); //for shells, laser delay time is based on shell mass/HEratio. The heavier the shell, the more mass to burn through. Don't expect to stop sabots via laser APS | ||
| var angularSpread = tanAngle * targetDistance; | ||
| delayTime /= ((laserDamage / (1 + Mathf.PI * angularSpread * angularSpread) * 0.425f) / 100); | ||
| delayTime /= LaserDamage(laserDamage, tanAngle, targetDistance, part.rb.position, targetPosition) / 100f; | ||
| if (delayTime < TimeWarp.fixedDeltaTime) delayTime = 0; | ||
| } | ||
| yield return new WaitForSeconds(delayTime); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this can be correct. The base laser damage is essentially the damage at point blank range, which should already take into account the gain (e.g.,
base damage ∝ Pt*Gt, where gain isG=ηD), so directivity shouldn't be an extra factor here, otherwise a non-divergent beam would transmit infinite power.(I think the definition of gain here actually breaks down when the directivity is so high compared to the base beam width since the only way to avoid the gain going to infinity for a constant power input and constant radiation efficiency is if the beam width narrows to 0.)
However, without this factor, the formula at https://en.wikipedia.org/wiki/Friis_transmission_equation#Contemporary_formula becomes
damage = base damage * Gr² * (λ/4πd)², and since the article also states that the conditiond >> λis required (and presumablyGr=1since the target isn't a receiving antenna), then that factor is miniscule.