Skip to content

Commit

Permalink
Merge pull request #222 from InvalidArgument3/live-server-triage
Browse files Browse the repository at this point in the history
probably fix inf spawning meme
  • Loading branch information
InvalidArgument3 authored Oct 29, 2024
2 parents aa7a0c8 + 112a689 commit b70123d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 44 deletions.
61 changes: 28 additions & 33 deletions Dynamic Asteroids/Data/Scripts/DynamicAsteroids/AsteroidSpawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,6 @@ public void SpawnAsteroids(List<AsteroidZone> zones) {

Log.Info($"Starting asteroid spawn cycle for {zones.Count} zones");

// Update zone positions based on player positions
List<IMyPlayer> players = new List<IMyPlayer>();
MyAPIGateway.Players.GetPlayers(players);
foreach (AsteroidZone zone in zones) {
Expand All @@ -806,44 +805,44 @@ public void SpawnAsteroids(List<AsteroidZone> zones) {

if (zoneOwner != null) {
Vector3D playerPos = zoneOwner.GetPosition();
// Update zone position to follow player
zone.Center = playerPos;
zone.LastActiveTime = DateTime.UtcNow;
}
}

int totalSpawnAttempts = 0;
if (AsteroidSettings.MaxAsteroidCount == 0) {
Log.Info("Asteroid spawning is disabled.");
return;
// Check for and handle excess asteroids
if (zone.ContainedAsteroids.Count > AsteroidSettings.MaxAsteroidsPerZone) {
Log.Warning($"Zone exceeded asteroid limit! Count: {zone.ContainedAsteroids.Count}/{AsteroidSettings.MaxAsteroidsPerZone}");
var excess = zone.ContainedAsteroids.Skip(AsteroidSettings.MaxAsteroidsPerZone).ToList();
foreach (var asteroidId in excess) {
var asteroid = MyEntities.GetEntityById(asteroidId) as AsteroidEntity;
if (asteroid != null) {
RemoveAsteroid(asteroid);
}
zone.ContainedAsteroids.Remove(asteroidId);
}
}
}

int totalAsteroidsSpawned = 0;
int totalZoneSpawnAttempts = 0;
int totalSpawnAttempts = 0;
List<Vector3D> skippedPositions = new List<Vector3D>();
List<Vector3D> spawnedPositions = new List<Vector3D>();

UpdatePlayerMovementData();

foreach (AsteroidZone zone in zones) {
Log.Info($"Checking zone at {zone.Center}:" +
$"\n - Current count: {zone.TotalAsteroidCount}/{AsteroidSettings.MaxAsteroidsPerZone}" +
$"\n - Is marked for removal: {zone.IsMarkedForRemoval}" +
$"\n - Can spawn: {zone.TotalAsteroidCount < AsteroidSettings.MaxAsteroidsPerZone}");
$"\n - Current count: {zone.ContainedAsteroids.Count}/{AsteroidSettings.MaxAsteroidsPerZone}" +
$"\n - Is marked for removal: {zone.IsMarkedForRemoval}");

if (zone.IsMarkedForRemoval) continue;
if (zone.TotalAsteroidCount >= AsteroidSettings.MaxAsteroidsPerZone) continue;
if (zone.ContainedAsteroids.Count >= AsteroidSettings.MaxAsteroidsPerZone) {
Log.Info($"Zone at {zone.Center} is at capacity ({zone.ContainedAsteroids.Count} asteroids)");
continue;
}

int asteroidsSpawned = 0;
int zoneSpawnAttempts = 0;

Log.Info($"Attempting spawn in zone at {zone.Center}, current count: {zone.TotalAsteroidCount}/{AsteroidSettings.MaxAsteroidsPerZone}");

if (zone.TotalAsteroidCount >= AsteroidSettings.MaxAsteroidsPerZone) {
Log.Info($"Zone at {zone.Center} is at capacity ({zone.TotalAsteroidCount} asteroids)");
continue;
}

bool skipSpawning = false;
foreach (IMyPlayer player in players) {
Vector3D playerPosition = player.GetPosition();
Expand All @@ -854,15 +853,15 @@ public void SpawnAsteroids(List<AsteroidZone> zones) {

if (IsPlayerMovingTooFast(player)) {
skipSpawning = true;
zone.CurrentSpeed = data.Speed; // Update zone speed tracking
zone.CurrentSpeed = data.Speed;
Log.Info($"Skipping asteroid spawning for player {player.DisplayName} due to high speed: {data.Speed:F2} m/s > {AsteroidSettings.ZoneSpeedThreshold} m/s");
break;
}
}

if (skipSpawning) continue;

while (zone.AsteroidCount < AsteroidSettings.MaxAsteroidsPerZone &&
while (zone.ContainedAsteroids.Count < AsteroidSettings.MaxAsteroidsPerZone &&
asteroidsSpawned < 10 &&
zoneSpawnAttempts < AsteroidSettings.MaxZoneAttempts &&
totalSpawnAttempts < AsteroidSettings.MaxTotalAttempts) {
Expand Down Expand Up @@ -909,11 +908,6 @@ public void SpawnAsteroids(List<AsteroidZone> zones) {
continue;
}

if (AsteroidSettings.MaxAsteroidCount != -1 && _asteroids.Count >= AsteroidSettings.MaxAsteroidCount) {
Log.Warning($"Maximum asteroid count of {AsteroidSettings.MaxAsteroidCount} reached. No more asteroids will be spawned until existing ones are removed.");
return;
}

float spawnChance = isInRing ?
MathHelper.Lerp(0.1f, 1f, ringInfluence) * AsteroidSettings.MaxRingAsteroidDensityMultiplier :
1f;
Expand All @@ -939,8 +933,8 @@ public void SpawnAsteroids(List<AsteroidZone> zones) {
AsteroidEntity asteroid = AsteroidEntity.CreateAsteroid(newPosition, size, newVelocity, type, rotation);
if (asteroid != null) {
_asteroids.Add(asteroid);
zone.ContainedAsteroids.Add(asteroid.EntityId); // Add to tracking
zone.AsteroidCount++;
_asteroidLookup[asteroid.EntityId] = asteroid;
zone.ContainedAsteroids.Add(asteroid.EntityId);
spawnedPositions.Add(newPosition);

_messageCache.AddMessage(new AsteroidNetworkMessage(
Expand All @@ -951,12 +945,10 @@ public void SpawnAsteroids(List<AsteroidZone> zones) {
Log.Info($"Spawned asteroid {asteroid.EntityId} at {newPosition} with size {size} and type {type}");
}
}

totalAsteroidsSpawned += asteroidsSpawned;
totalZoneSpawnAttempts += zoneSpawnAttempts;
}

if (!AsteroidSettings.EnableLogging) return;

if (skippedPositions.Count > 0) {
Log.Info($"Skipped spawning asteroids due to proximity to vanilla asteroids. Positions: {string.Join(", ", skippedPositions.Select(p => p.ToString()))}");
}
Expand Down Expand Up @@ -1044,7 +1036,10 @@ private void CleanupOrphanedAsteroids() {

if (!isInAnyZone) {
Log.Info($"Found orphaned asteroid {asteroid.EntityId} at {asteroidPosition}");
asteroidsToRemove.Add(asteroid);
if (MyAPIGateway.Session.IsServer) {
NetworkHandler.SendAsteroidRemoval(asteroid.EntityId);
}
RemoveAsteroid(asteroid);
}
}

Expand Down
21 changes: 10 additions & 11 deletions Dynamic Asteroids/Data/Scripts/DynamicAsteroids/MainSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -541,23 +541,22 @@ private void ProcessServerMessage(AsteroidNetworkMessage message, ulong steamId)
private void RemoveAsteroidOnClient(long entityId) {
try {
Log.Info($"Client: Removing asteroid with ID {entityId}");

// Remove from tracking first
_knownAsteroidIds.Remove(entityId);
_serverPositions.Remove(entityId);
_serverRotations.Remove(entityId);

// Then remove the entity
var asteroid = MyEntities.GetEntityById(entityId) as AsteroidEntity;
if (asteroid != null) {
try {
MyEntities.Remove(asteroid);
asteroid.Close();
Log.Info($"Client: Successfully removed asteroid {entityId}");
}
catch (Exception ex) {
Log.Warning($"Error removing asteroid {entityId}: {ex.Message}");
}
MyAPIGateway.Utilities.InvokeOnGameThread(() => {
try {
MyEntities.Remove(asteroid);
asteroid.Close();
Log.Info($"Client: Successfully removed asteroid {entityId}");
}
catch (Exception ex) {
Log.Warning($"Error removing asteroid {entityId}: {ex.Message}");
}
});
}
}
catch (Exception ex) {
Expand Down

0 comments on commit b70123d

Please sign in to comment.