Skip to content

Commit

Permalink
its all i got
Browse files Browse the repository at this point in the history
  • Loading branch information
InvalidArgument3 committed Oct 12, 2024
1 parent 7038231 commit 5635518
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 222 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public enum AsteroidType
Uraninite
}

public class AsteroidEntity : MyEntity
public class AsteroidEntity : MyEntity, IMyDestroyableObject
{
private static readonly string[] IceAsteroidModels = {
@"Models\IceAsteroid_1.mwm",
Expand Down Expand Up @@ -465,8 +465,42 @@ public void OnDestroy()
}
}

// Required property implementation for `IMyDestroyableObject`
public bool UseDamageSystem => true;

// Required property implementation for `IMyDestroyableObject`
public float Integrity => _integrity;

public float _integrity;

public bool DoDamage(float damage, MyStringHash damageSource, bool sync, MyHitInfo? hitInfo = null, long attackerId = 0,
long realHitEntityId = 0, bool shouldDetonateAmmo = true, MyStringHash? extraInfo = null)
{
try
{
// Ignore all explosion types, as we are manually managing them with ExplosionTracker
if (damageSource == MyDamageType.Explosion)
{
Log.Info($"Ignored explosion damage for Asteroid ID: {EntityId}");
return false; // i wish i had the slightest idea why this was needed what is WRONG WITH DESTROYABLEOBJECTS
}

ReduceIntegrity(damage);

if (_integrity <= 0)
{
OnDestroy();
}

return true;
}
catch (Exception ex)
{
Log.Exception(ex, typeof(AsteroidEntity), "Exception in DoDamage");
return false;
}
}

public void ReduceIntegrity(float damage)
{
_integrity -= damage;
Expand All @@ -475,7 +509,7 @@ public void ReduceIntegrity(float damage)
if (_integrity <= 0)
{
Log.Info("Integrity below or equal to 0, calling OnDestroy");
OnDestroy(); // This will handle splitting or destroying the asteroid
OnDestroy();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,18 @@ public class ExplosionTracker : MySessionComponentBase
private static ExplosionTracker _instance;
public static ExplosionTracker Instance => _instance;

// Make activeExplosions accessible from outside the class
private List<MyExplosionInfo> activeExplosions = new List<MyExplosionInfo>();

public override void BeforeStart()
{
base.BeforeStart();
_instance = this;
}

// the worst workaround imaginable for this stupid mod :)))))))))
public override void UpdateAfterSimulation()
{
base.UpdateAfterSimulation();

// Iterate through the list and handle explosions (simplified)
foreach (var explosion in activeExplosions)
{
try
Expand All @@ -46,7 +44,6 @@ public override void UpdateAfterSimulation()
}
}

// Clear the explosions list after processing all of them
activeExplosions.Clear();
}

Expand Down Expand Up @@ -75,64 +72,51 @@ protected override void UnloadData()

private void OnExplosion(ref MyExplosionInfo explosionInfo)
{
// Register explosion for tracking with proper damage
RegisterExplosion(explosionInfo.ExplosionSphere.Center, explosionInfo.ExplosionSphere.Radius, explosionInfo.Damage);
}

private void HandleNearestAsteroidExplosion(MyExplosionInfo explosion)
{
try
{
// Find the nearest asteroid to the explosion
var nearestAsteroid = FindNearestAsteroid(explosion.ExplosionSphere.Center);

if (nearestAsteroid != null)
{
// Notify that an asteroid has been found
MyAPIGateway.Utilities.ShowNotification($"Found nearest asteroid ID: {nearestAsteroid.EntityId}", 2000, MyFontEnum.Green);
//MyAPIGateway.Utilities.ShowNotification($"Found nearest asteroid ID: {nearestAsteroid.EntityId}", 2000, MyFontEnum.Green);

// Calculate the distance between the explosion center and the asteroid surface
double distanceSquared = Vector3D.DistanceSquared(nearestAsteroid.PositionComp.GetPosition(), explosion.ExplosionSphere.Center);
double distance = Math.Sqrt(distanceSquared);

// Get the approximate radius of the asteroid
float asteroidRadius = nearestAsteroid.PositionComp.LocalVolume.Radius;

// Adjust the distance to account for the asteroid's radius
double effectiveDistance = distance - asteroidRadius;
effectiveDistance = Math.Max(0, effectiveDistance); // Ensure effectiveDistance is not negative

// Calculate the impact factor using a quadratic fall-off model
double impactFactor = 1.0 - Math.Pow(effectiveDistance / explosion.ExplosionSphere.Radius, 2);

// Clamp the impact factor between 0 and 1 manually
if (impactFactor < 0.0) impactFactor = 0.0;
if (impactFactor > 1.0) impactFactor = 1.0;

if (impactFactor > 0)
{
// Apply damage scaled by the impact factor based on distance
float damageToApply = (float)(explosion.Damage * impactFactor);
damageToApply = Math.Min(damageToApply, explosion.Damage); // Ensure damage doesn't exceed the original damage

// Reduce asteroid integrity
nearestAsteroid.ReduceIntegrity(damageToApply);
nearestAsteroid._integrity = Math.Max(0, nearestAsteroid._integrity); // Clamp integrity to 0 to prevent negative values

// Notify about the damage applied
string notificationText = $"Damaged Asteroid ID: {nearestAsteroid.EntityId}, Damage: {damageToApply}, New Integrity: {nearestAsteroid._integrity}";
MyAPIGateway.Utilities.ShowNotification(notificationText, 2000, MyFontEnum.Red);
}
else
{
// Notify that the impact factor was too low
MyAPIGateway.Utilities.ShowNotification($"Impact factor for asteroid ID: {nearestAsteroid.EntityId} is too low, no damage applied", 2000, MyFontEnum.Red);
//MyAPIGateway.Utilities.ShowNotification($"Impact factor for asteroid ID: {nearestAsteroid.EntityId} is too low, no damage applied", 2000, MyFontEnum.Red);
}
}
else
{
// Notify that no asteroid was found
MyAPIGateway.Utilities.ShowNotification("No asteroid found near the explosion.", 2000, MyFontEnum.Red);
//MyAPIGateway.Utilities.ShowNotification("No asteroid found near the explosion.", 2000, MyFontEnum.Red);
}
}
catch (Exception ex)
Expand All @@ -146,7 +130,6 @@ private AsteroidEntity FindNearestAsteroid(Vector3D explosionPosition)
double minDistance = double.MaxValue;
AsteroidEntity nearestAsteroid = null;

// Iterate through all asteroids to find the nearest one
foreach (var entity in MyEntities.GetEntities().OfType<AsteroidEntity>())
{
try
Expand All @@ -164,7 +147,6 @@ private AsteroidEntity FindNearestAsteroid(Vector3D explosionPosition)
}
}

// Logging for debugging
if (nearestAsteroid != null)
{
Log.Info($"Nearest asteroid found with ID: {nearestAsteroid.EntityId} at distance squared: {minDistance}");
Expand Down

This file was deleted.

0 comments on commit 5635518

Please sign in to comment.