Skip to content

Commit

Permalink
Additional Anti-Grief Protections for De-spawning Starting Units
Browse files Browse the repository at this point in the history
- Changes behavior so only the first starting unit is deleted to prevent players from de-spawning Crab armies
  • Loading branch information
data-bomb committed Dec 22, 2024
1 parent 7429b0c commit ed14a0a
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions Si_AntiGrief/Si_AntiGrief.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ You should have received a copy of the GNU General Public License
using System.Linq;
using UnityEngine;

[assembly: MelonInfo(typeof(AntiGrief), "Anti-Grief", "1.4.0", "databomb", "https://github.com/data-bomb/Silica")]
[assembly: MelonInfo(typeof(AntiGrief), "Anti-Grief", "1.4.1", "databomb", "https://github.com/data-bomb/Silica")]
[assembly: MelonGame("Bohemia Interactive", "Silica")]
[assembly: MelonOptionalDependencies("Admin Mod")]

Expand All @@ -49,6 +49,8 @@ public class AntiGrief : MelonMod
static MelonPreferences_Entry<bool> _StructureAntiGrief_IgnoreNodes = null!;
static MelonPreferences_Entry<bool> _BlockShrimpControllers = null!;

static uint[] playerTransferCount = new uint[] {};

private const string ModCategory = "Silica";

public override void OnInitializeMelon()
Expand All @@ -58,6 +60,9 @@ public override void OnInitializeMelon()
_NegativeKills_Penalty_Ban ??= _modCategory.CreateEntry<bool>("Grief_NegativeKills_Penalty_Ban", true);
_StructureAntiGrief_IgnoreNodes ??= _modCategory.CreateEntry<bool>("Grief_IgnoreFriendlyNodesDestroyed", true);
_BlockShrimpControllers ??= _modCategory.CreateEntry<bool>("Grief_BlockShrimpTakeOver", false);

playerTransferCount = new uint[NetworkGameServer.GetPlayersMax()+1];
Array.Fill(playerTransferCount, 0u);
}

public override void OnLateInitializeMelon()
Expand Down Expand Up @@ -168,6 +173,27 @@ public static void Postfix(Target __0, GameObject __1)
}
}

[HarmonyPatch(typeof(GameMode), nameof(GameMode.SpawnUnitForPlayer), new Type[] { typeof(Player), typeof(GameObject), typeof(Vector3), typeof(Quaternion) })]
private static class CommanderManager_Patch_GameMode_SpawnUnitForPlayer
{
public static void Postfix(GameMode __instance, Unit __result, Player __0, UnityEngine.GameObject __1, UnityEngine.Vector3 __2, UnityEngine.Quaternion __3)
{
try
{
if (__0 == null)
{
return;
}

playerTransferCount[__0.GetIndex()] = 0;
}
catch (Exception error)
{
HelperMethods.PrintError(error, "Failed to run GameMode::SpawnUnitForPlayer");
}
}
}

public void OnRequestEnterUnit(object? sender, OnRequestEnterUnitArgs args)
{
try
Expand Down Expand Up @@ -209,6 +235,8 @@ public void OnRequestEnterUnit(object? sender, OnRequestEnterUnitArgs args)
MelonLogger.Msg("Player " + player.PlayerName + " found switching from spawn unit. Taking action to prevent inf creatures.");
DeletePriorUnit(player, player.ControlledUnit);
}

playerTransferCount[player.GetIndex()]++;
}
catch (Exception error)
{
Expand Down Expand Up @@ -238,7 +266,18 @@ public static bool ShouldDeletePriorUnit(Player player)
return false;
}

return GameMode.CurrentGameMode.GetCanDeletePlayerControlledUnit(player, currentUnit);
if (GameMode.CurrentGameMode.GetCanDeletePlayerControlledUnit(player, currentUnit))
{
// we don't want the user to be able to delete an endless supply of starting units
if (playerTransferCount[player.GetIndex()] >= 1)
{
return false;
}

return true;
}

return false;
}

// hook DestroyAllUnitsForPlayer pre and override game code to prevent aliens from dying
Expand Down

0 comments on commit ed14a0a

Please sign in to comment.