Fix issues with exploding items from own hand (#645)

* Fix crash in CameraRecoilComponent from NaN values

Doesn't proceed with camera shake if either component of recoil is NaN.

* Log NaN recoil value in CameraRecoilComponent

* Fix ExplosionHelper passing NaN recoil values when exploding from hand

With other commits CameraRecoilComponent now won't crash from NaN recoil values. Still want to fix this so explosions shake the camera. Just sets a value slightly greater than 0.0 for distance if it is (0.0, 0.0)
This commit is contained in:
moneyl
2020-02-08 14:46:12 -05:00
committed by GitHub
parent eb7c80ba7a
commit a2a3e5e2e4
2 changed files with 18 additions and 2 deletions

View File

@@ -1,9 +1,10 @@
using System; using System;
using Content.Shared.GameObjects.Components.Mobs; using Content.Shared.GameObjects.Components.Mobs;
using Robust.Client.GameObjects; using Robust.Client.GameObjects;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Network; using Robust.Shared.Interfaces.Network;
using Robust.Shared.Log;
using Robust.Shared.Maths; using Robust.Shared.Maths;
namespace Content.Client.GameObjects.Components.Mobs namespace Content.Client.GameObjects.Components.Mobs
@@ -42,6 +43,12 @@ namespace Content.Client.GameObjects.Components.Mobs
public override void Kick(Vector2 recoil) public override void Kick(Vector2 recoil)
{ {
if (float.IsNaN(recoil.X) || float.IsNaN(recoil.Y))
{
Logger.Error($"CameraRecoilComponent on entity {Owner.Uid} passed a NaN recoil value. Ignoring.");
return;
}
// Use really bad math to "dampen" kicks when we're already kicked. // Use really bad math to "dampen" kicks when we're already kicked.
var existing = _currentKick.Length; var existing = _currentKick.Length;
var dampen = existing/KickMagnitudeMax; var dampen = existing/KickMagnitudeMax;

View File

@@ -20,6 +20,12 @@ namespace Content.Server.Explosions
{ {
public static class ExplosionHelper public static class ExplosionHelper
{ {
/// <summary>
/// Distance used for camera shake when distance from explosion is (0.0, 0.0).
/// Avoids getting NaN values down the line from doing math on (0.0, 0.0).
/// </summary>
private static Vector2 _epicenterDistance = (0.1f, 0.1f);
public static void SpawnExplosion(GridCoordinates coords, int devastationRange, int heavyImpactRange, int lightImpactRange, int flashRange) public static void SpawnExplosion(GridCoordinates coords, int devastationRange, int heavyImpactRange, int lightImpactRange, int flashRange)
{ {
var tileDefinitionManager = IoCManager.Resolve<ITileDefinitionManager>(); var tileDefinitionManager = IoCManager.Resolve<ITileDefinitionManager>();
@@ -130,8 +136,11 @@ namespace Content.Server.Explosions
var playerPos = player.AttachedEntity.Transform.WorldPosition; var playerPos = player.AttachedEntity.Transform.WorldPosition;
var delta = coords.ToMapPos(mapManager) - playerPos; var delta = coords.ToMapPos(mapManager) - playerPos;
var distance = delta.LengthSquared; //Change if zero. Will result in a NaN later breaking camera shake if not changed
if (delta.EqualsApprox((0.0f, 0.0f)))
delta = _epicenterDistance;
var distance = delta.LengthSquared;
var effect = 1 / (1 + 0.2f * distance); var effect = 1 / (1 + 0.2f * distance);
if (effect > 0.01f) if (effect > 0.01f)
{ {