Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions RetakesPlugin/Events/RoundEventHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public RoundEventHandlers(RetakesPlugin plugin, GameManager gameManager, SpawnMa
_enableFallbackAllocation = enableFallbackAllocation;
_enableFallbackBombsiteAnnouncement = enableFallbackBombsiteAnnouncement;
_random = random;

Logger.LogInfo("RoundEventHandlers", $"EnableFallbackAllocation inicializado a: {_enableFallbackAllocation}");
}

Expand Down Expand Up @@ -107,7 +107,7 @@ public HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info)
Logger.LogDebug("Round", "Warmup round, skipping.");
if (_showSpawnsCommand?.ShowingSpawnsForBombsite != null && _plugin.MapConfigService != null)
{
SpawnService.ShowSpawns(null!, _plugin.MapConfigService.GetSpawnsClone(), _showSpawnsCommand.ShowingSpawnsForBombsite);
SpawnService.ShowSpawns(_plugin, _plugin.MapConfigService.GetSpawnsClone(), _showSpawnsCommand.ShowingSpawnsForBombsite);
Logger.LogDebug("Round", $"Re-showing spawns for bombsite {_showSpawnsCommand.ShowingSpawnsForBombsite}");
}

Expand Down
9 changes: 3 additions & 6 deletions RetakesPlugin/RetakesPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace RetakesPlugin;
[MinimumApiVersion(345)]
public class RetakesPlugin : BasePlugin, IPluginConfig<BaseConfigs>
{
public const string Version = "3.0.3";
public const string Version = "3.0.4";

#region Plugin Info
public override string ModuleName => "Retakes Plugin";
Expand Down Expand Up @@ -141,12 +141,9 @@ private void OnMapStart(string mapName)
{
Utils.Logger.LogInfo("MapStart", $"Map started: {mapName}");

SpawnService.ClearAllSpawnModels();
SpawnService.Reset();

AddTimer(1.0f, () =>
{
ServerHelper.ExecuteRetakesConfiguration(ModuleDirectory);
});
AddTimer(1.0f, ServerHelper.ExecuteRetakesConfiguration);

InitializeServices(mapName);
}
Expand Down
64 changes: 40 additions & 24 deletions RetakesPlugin/Services/SpawnService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace RetakesPlugin.Services;

public static class SpawnService
{
private static readonly List<CBaseEntity> _spawnModels = new();
private static readonly List<uint> _spawnModelIndexes = new();

public static int ShowSpawns(RetakesPlugin plugin, List<Spawn> spawns, Bombsite? bombsite)
{
Expand Down Expand Up @@ -39,7 +39,7 @@ public static void ShowSpawn(Spawn spawn)
{
// Create player model
var model = Utilities.CreateEntityByName<CDynamicProp>("prop_dynamic");
if (model == null)
if (model == null || !model.IsValid)
{
Logger.LogError("SpawnService", "Failed to create prop_dynamic entity for spawn visualization");
return;
Expand Down Expand Up @@ -68,24 +68,26 @@ public static void ShowSpawn(Spawn spawn)

// Apply color and glow
model.Render = Color.FromArgb(200, teamColor.R, teamColor.G, teamColor.B);
model.Glow.GlowColorOverride = teamColor;
model.Glow.GlowRange = 2000;
model.Glow.GlowTeam = -1;
model.Glow.GlowType = 3;
model.Glow.GlowRangeMin = 25;
if (model.Glow != null)
{
model.Glow.GlowColorOverride = teamColor;
model.Glow.GlowRange = 2000;
model.Glow.GlowType = 3;
model.Glow.GlowRangeMin = 25;
}

// Position the model
model.Teleport(spawn.Vector, spawn.QAngle, new Vector(0, 0, 0));
if (model.Index != 0) _spawnModelIndexes.Add(model.Index);

_spawnModels.Add(model);
CreateSpawnLabel(spawn);

Logger.LogDebug("SpawnService", $"Created spawn model for {spawn.Team} at {spawn.Vector}");
}
catch (Exception ex)
{
Logger.LogError("SpawnService", $"Error creating spawn model: {ex.Message}");
model?.Remove();
if (model.IsValid) model.Remove();
}
}

Expand Down Expand Up @@ -128,7 +130,7 @@ private static void CreateSpawnLabel(Spawn spawn)
spawnText.Teleport(textPos, textAngle);
spawnText.DispatchSpawn();

_spawnModels.Add(spawnText);
if (spawnText.Index != 0) _spawnModelIndexes.Add(spawnText.Index);
}
catch (Exception ex)
{
Expand All @@ -140,19 +142,27 @@ public static void RemoveSpawnBeam(Spawn spawn)
{
try
{
var modelsToRemove = _spawnModels
.Where(entity => entity != null && entity.IsValid && IsEntityAtPosition(entity, spawn.Vector))
.ToList();
var toRemove = new List<uint>();
foreach (var index in _spawnModelIndexes)
{
var entity = Utilities.GetEntityFromIndex<CBaseEntity>((int)index);
if (entity == null || !entity.IsValid) continue;

foreach (var model in modelsToRemove)
if (IsEntityAtPosition(entity, spawn.Vector))
{
entity.Remove();
toRemove.Add(index);
}
}

foreach (var index in toRemove)
{
model.Remove();
_spawnModels.Remove(model);
_spawnModelIndexes.Remove(index);
}

if (modelsToRemove.Any())
if (toRemove.Count > 0)
{
Logger.LogDebug("SpawnService", $"Removed {modelsToRemove.Count} spawn visualization entities");
Logger.LogDebug("SpawnService", $"Removed {toRemove.Count} spawn visualization entities");
}
}
catch (Exception ex)
Expand All @@ -171,25 +181,24 @@ private static bool IsEntityAtPosition(CBaseEntity entity, Vector position)
Math.Pow(entity.AbsOrigin.Z - position.Z, 2)
);

return distance < 50.0; // Within 50 units
return distance < 50.0;
}

public static void ClearAllSpawnModels()
{
try
{
int clearedCount = 0;
foreach (var model in _spawnModels.ToList())
for (int i = _spawnModelIndexes.Count - 1; i >= 0; i--)
{
if (model != null && model.IsValid)
var entity = Utilities.GetEntityFromIndex<CBaseEntity>((int)_spawnModelIndexes[i]);
if (entity != null && entity.IsValid)
{
model.Remove();
entity.Remove();
clearedCount++;
}
}

_spawnModels.Clear();

if (clearedCount > 0)
{
Logger.LogInfo("SpawnService", $"Cleared {clearedCount} spawn visualization models");
Expand All @@ -199,5 +208,12 @@ public static void ClearAllSpawnModels()
{
Logger.LogError("SpawnService", $"Error clearing spawn models: {ex.Message}");
}
finally
{
ClearIndexes();
}
}

public static void Reset() => ClearIndexes();
private static void ClearIndexes() => _spawnModelIndexes.Clear();
}
16 changes: 8 additions & 8 deletions RetakesPlugin/Utils/ServerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ namespace RetakesPlugin.Utils;

public static class ServerHelper
{
private const string RetakesCfgDirectory = "/../../../../cfg/cs2-retakes";
private const string RetakesCfgPath = $"{RetakesCfgDirectory}/retakes.cfg";
private static string RetakesCfgDirectory => Path.Combine(Server.GameDirectory, "csgo", "cfg", "cs2-retakes");
private static string RetakesCfgPath => Path.Combine(RetakesCfgDirectory, "retakes.cfg");

public static void ExecuteRetakesConfiguration(string moduleDirectory)
public static void ExecuteRetakesConfiguration()
{
if (!File.Exists(moduleDirectory + RetakesCfgPath))
if (!File.Exists(RetakesCfgPath))
{
CreateRetakesConfig(moduleDirectory);
CreateRetakesConfig();
}

Server.ExecuteCommand("exec cs2-retakes/retakes.cfg");
Logger.LogInfo("Server", "Retakes configuration executed");
}

private static void CreateRetakesConfig(string moduleDirectory)
private static void CreateRetakesConfig()
{
try
{
Directory.CreateDirectory(moduleDirectory + RetakesCfgDirectory);
Directory.CreateDirectory(RetakesCfgDirectory);

var retakesCfg = File.Create(moduleDirectory + RetakesCfgPath);
var retakesCfg = File.Create(RetakesCfgPath);

var retakesCfgContents = @"
// Things you shouldn't change:
Expand Down
Loading