using System;
using System.Collections.Generic;
using System.Linq;
using Facepunch;
using Newtonsoft.Json;
using Oxide.Core;
using Oxide.Core.Configuration;
using Rust;
using UnityEngine;
using Random = UnityEngine.Random;
using Time = UnityEngine.Time;
#region Chat Commands
[ChatCommand("dun")]
private void CreateDungeonCommand(BasePlayer player, string command, string[] args)
{
if (!IsAdmin(player))
return;
var result = AutoSpawnDungeon();
NotifyPlayer(player, result);
}
[ChatCommand("removeinactivedungeons")]
private void RemoveInactiveDungeonCommand(BasePlayer player, string command, string[] args)
{
if (!IsAdmin(player))
return;
var removedDungeons = RemoveInactiveDungeons();
NotifyPlayerOnRemoval(player, removedDungeons, "NoInactiveDungeonsToRemove");
}
[ChatCommand("removedungeons")]
private void RemoveDungeonCommand(BasePlayer player, string command, string[] args)
{
if (!IsAdmin(player))
return;
var removedDungeons = RemoveAllDungeons();
NotifyPlayerOnRemoval(player, removedDungeons, "NoDungeonsToRemove");
}
#endregion
#region Initialization
private void OnServerInitialized()
{
_instance = this;
CleanupExistingEntities();
InitializeTierNameMap();
LoadData();
if (_configData.DungeonSpawn.EnableAutoSpawn)
{
StartAutoSpawnTimer();
}
StartDungeonCheckTimer();
StartUpdateMarkersTimer();
}
private void CleanupExistingEntities()
{
foreach (
var entityType in new Type[]
{
typeof(VendingMachineMapMarker),
typeof(MapMarkerGenericRadius),
typeof(BasePortal),
typeof(ProceduralDynamicDungeon),
typeof(AutoTurret),
typeof(NPCPlayer),
typeof(StorageContainer),
}
)
{
foreach (var obj in UnityEngine.Object.FindObjectsOfType(entityType))
{
if (obj is BaseEntity baseEntity && baseEntity.OwnerID == OwnerID)
{
baseEntity.Kill(BaseNetworkable.DestroyMode.None);
}
}
}
}
private void InitializeTierNameMap()
{
_tierNameMap = new Dictionary<DungeonTierConfig, string>
{
{ _configData.Tiers.Easy, "Easy" },
{ _configData.Tiers.Normal, "Normal" },
{ _configData.Tiers.Medium, "Medium" },
{ _configData.Tiers.Hard, "Hard" },
{ _configData.Tiers.Nightmare, "Nightmare" },
};
}
private void LoadData()
{
_dataFile = Interface.Oxide.DataFileSystem.GetFile("Dungeons_Data");
_dungeonData = _dataFile.ReadObject<DungeonData>() ?? new DungeonData();
}
private void SaveData()
{
_dataFile.WriteObject(_dungeonData);
}
#endregion
#region Hooks
private object CanEntityTakeDamage(BaseCombatEntity victim, HitInfo hitInfo)
{
if (victim == null || hitInfo == null)
return null;
if (hitInfo.Initiator == null)
return null;
if (IsValidSteamPlayer(hitInfo.Initiator as BasePlayer) && IsDungeonEntity(victim))
return true;
if (IsDungeonEntity(hitInfo.Initiator) && IsDungeonEntity(victim))
return true;
return null;
}
private object OnEntityTakeDamage(BaseCombatEntity victim, HitInfo hitInfo)
{
if (victim == null || hitInfo == null)
return null;
if (hitInfo.Initiator == null)
return null;
if (IsValidSteamPlayer(hitInfo.Initiator as BasePlayer) && IsDungeonEntity(victim))
return null;
if (IsDungeonEntity(hitInfo.Initiator) && IsDungeonEntity(victim))
return true;
return null;
}
private void OnServerSave() => SaveData();
#endregion
#region Dungeon Management
private void StartUpdateMarkersTimer()
{
_updateMarkersTimer = timer.Every(30f, UpdateMarkers);
}
private void UpdateMarkers()
{
var mapMarkers = UnityEngine.Object.FindObjectsOfType<MapMarkerGenericRadius>();
foreach (var marker in mapMarkers.Where(marker => marker.IsValid()))
{
marker.SendUpdate();
}
}
private void StartAutoSpawnTimer()
{
_autoSpawnTimer = timer.Every(
_configData.DungeonSpawn.AutoSpawnCycleInterval,
() =>
{
var result = AutoSpawnDungeon();
LogDungeonSpawnResult(result);
}
);
}
private void StartDungeonCheckTimer()
{
_dungeonCheckTimer = timer.Every(
60f,
() =>
{
var removedDungeons = RemoveInactiveDungeons();
if (removedDungeons.Count > 0)
{
foreach (var dungeon in removedDungeons)
{
LogDungeonRemoval(dungeon);
}
}
}
);
}
private enum DungeonSpawnResult
{
Success,
MaxActiveDungeonsReached,
NoValidSpawnLocation,
}
private DungeonSpawnResult AutoSpawnDungeon()
{
if (_dungeonData.ActiveDungeons.Count >= _configData.DungeonSpawn.MaxTotalActiveDungeons)
{
return DungeonSpawnResult.MaxActiveDungeonsReached;
}
if (TryFindDungeonSpawnPoint(out var position, out var rotation))
{
var selectedTier = CreateDungeon(position, rotation);
if (selectedTier != null)
{
var tierName = _tierNameMap[selectedTier];
CreateDungeonMarkers(position, tierName);
NotifyPlayersOfNewDungeon(position, tierName);
return DungeonSpawnResult.Success;
}
}
return DungeonSpawnResult.NoValidSpawnLocation;
}
private DungeonTierConfig CreateDungeon(Vector3 position, Quaternion rotation)
{
string prefabPath = GetRandomDungeonPrefab();
if (string.IsNullOrEmpty(prefabPath))
return null;
var dungeonPortal = GameManager.server.CreateEntity(prefabPath, position, rotation) as BasePortal;
if (dungeonPortal == null)
return null;
dungeonPortal.OwnerID = OwnerID;
dungeonPortal.Spawn();
var proceduralDungeon = (dungeonPortal as XmasDungeon)?.dungeonInstance.Get(true) ?? (dungeonPortal as HalloweenDungeon)?.dungeonInstance.Get(true);
if (!(dungeonPortal is XmasDungeon xmasDungeon || dungeonPortal is HalloweenDungeon halloweenDungeon))
{
dungeonPortal.Kill(BaseNetworkable.DestroyMode.None);
return null;
}
if (proceduralDungeon == null || proceduralDungeon.spawnedCells.Count < 1)
{
dungeonPortal.Kill(BaseNetworkable.DestroyMode.None);
return null;
}
var selectedTier = GetDungeonTier(proceduralDungeon.spawnedCells.Count);
var activeDungeon = new ActiveDungeon
{
PortalId = dungeonPortal.net.ID.Value,
DynamicDungeonId = proceduralDungeon.net.ID.Value,
Position = position,
TierName = _tierNameMap[selectedTier],
EntityIds = new List<ulong>(),
};
NextTick(() => PopulateDungeon(dungeonPortal, selectedTier, activeDungeon));
_dungeonData.ActiveDungeons.Add(activeDungeon);
return selectedTier;
}
private void PopulateDungeon(BasePortal dungeonPortal, DungeonTierConfig tierConfig, ActiveDungeon activeDungeon)
{
var proceduralDungeon = (dungeonPortal as XmasDungeon)?.dungeonInstance.Get(true) ?? (dungeonPortal as HalloweenDungeon)?.dungeonInstance.Get(true);
if (proceduralDungeon == null)
{
Puts("Procedural dungeon instance is null.");
return;
}
var allSpawnEntries = GenerateSpawnEntries(tierConfig);
foreach (var cell in proceduralDungeon.spawnedCells)
{
PopulateEntitiesInCell(cell, allSpawnEntries, tierConfig, activeDungeon);
}
ValidateAndAttemptRetries(proceduralDungeon, tierConfig, activeDungeon, allSpawnEntries);
}
private void PopulateEntitiesInCell(ProceduralDungeonCell cell, List<SpawnGroup.SpawnEntry> spawnEntries, DungeonTierConfig tierConfig, ActiveDungeon activeDungeon)
{
foreach (var group in cell.spawnGroups)
{
int availableSlots = group.maxPopulation - group.spawnInstances.Count;
if (availableSlots <= 0)
continue;
for (int i = 0; i < availableSlots && spawnEntries.Count > 0; i++)
{
var entry = spawnEntries[0];
Vector3 spawnPosition;
Quaternion spawnRotation;
if (TryGetSpawnPosition(group, out spawnPosition, out spawnRotation))
{
var entity = GameManager.server.CreateEntity(entry.prefab.resourcePath, spawnPosition, spawnRotation);
if (entity != null)
{
entity.OwnerID = OwnerID;
entity.Spawn();
if (entity is NPCPlayer)
{
activeDungeon.NpcIds.Add(entity.net.ID.Value);
}
if (entity is AutoTurret turret)
{
ConfigureTurret(turret, tierConfig.AutoTurretConfig);
turret.gameObject.AddComponent<TurretMonoBehaviour>();
}
if (entity is StorageContainer storage)
{
ConfigureStorage(storage);
}
activeDungeon.EntityIds.Add(entity.net.ID.Value);
group.spawnInstances.Add(entity.GetComponent<SpawnPointInstance>());
}
spawnEntries.RemoveAt(0);
}
}
}
}
private bool TryGetSpawnPosition(SpawnGroup group, out Vector3 position, out Quaternion rotation)
{
foreach (var spawnPoint in group.spawnPoints)
{
if (spawnPoint != null && spawnPoint.IsAvailableTo(null))
{
spawnPoint.GetLocation(out position, out rotation);
return true;
}
}
position = Vector3.zero;
rotation = Quaternion.identity;
return false;
}
private void ValidateAndAttemptRetries(ProceduralDynamicDungeon proceduralDungeon, DungeonTierConfig tierConfig, ActiveDungeon activeDungeon, List<SpawnGroup.SpawnEntry> remainingEntries)
{
int retries = 3;
while (remainingEntries.Count > 0 && retries > 0)
{
Puts($"Retrying placement for {remainingEntries.Count} missing entities.");
foreach (var cell in proceduralDungeon.spawnedCells)
{
PopulateEntitiesInCell(cell, remainingEntries, tierConfig, activeDungeon);
}
retries--;
}
int entitiesPlaced = activeDungeon.EntityIds.Count;
int expectedEntities = CalculateTotalEntities(tierConfig);
if (entitiesPlaced < expectedEntities)
{
Puts($"Warning: Only placed {entitiesPlaced}/{expectedEntities} entities.");
}
else
{
Puts("All entities placed successfully.");
}
}
private int CalculateTotalEntities(DungeonTierConfig tierConfig)
{
int total = tierConfig.TotalLootBoxes;
total += tierConfig.NpcSpawnConfigs.Sum(npcConfig => npcConfig.Total);
total += tierConfig.AutoTurretConfig.Total;
return total;
}
private object OnPortalUse(BasePlayer player, BasePortal portal)
{
if (_dungeonData.ActiveDungeons.All(d => d.PortalId != portal.net.ID.Value))
{
return null;
}
var proceduralDungeon = (portal as XmasDungeon)?.dungeonInstance.Get(true) ?? (portal as HalloweenDungeon)?.dungeonInstance.Get(true);
if (proceduralDungeon == null)
{
return null;
}
foreach (var cell in proceduralDungeon.spawnedCells)
{
foreach (var group in cell.spawnGroups)
{
for (int num = group.spawnInstances.Count - 1; num >= 0; num--)
{
SpawnPointInstance spawnPointInstance = group.spawnInstances[num];
BaseEntity entity = spawnPointInstance?.gameObject?.ToBaseEntity();
if (entity != null && !IsDungeonEntity(entity) && (entity is NPCDwelling || entity is ScarecrowNPC || entity is LootContainer))
{
entity.Kill();
}
}
}
}
return null;
}
private List<SpawnGroup.SpawnEntry> GenerateSpawnEntries(DungeonTierConfig tierConfig)
{
var allSpawnEntries = new List<SpawnGroup.SpawnEntry>();
AddNpcEntries(tierConfig, allSpawnEntries);
AddTurretEntries(tierConfig, allSpawnEntries);
AddLootEntries(tierConfig, allSpawnEntries);
return allSpawnEntries.OrderBy(_ => Random.value).ToList();
}
private void AddNpcEntries(DungeonTierConfig tierConfig, List<SpawnGroup.SpawnEntry> spawnEntries)
{
foreach (var config in tierConfig.NpcSpawnConfigs)
{
if (GameManifest.pathToGuid.TryGetValue(config.PrefabName, out var guid))
{
spawnEntries.AddRange(
Enumerable.Repeat(
new SpawnGroup.SpawnEntry
{
prefab = new GameObjectRef { guid = guid },
weight = 1,
mobile = true,
},
config.Total
)
);
}
}
}
private void AddTurretEntries(DungeonTierConfig tierConfig, List<SpawnGroup.SpawnEntry> spawnEntries)
{
var prefabName = "assets/prefabs/npc/autoturret/autoturret_deployed.prefab";
if (GameManifest.pathToGuid.TryGetValue(prefabName, out var turretGuid))
{
spawnEntries.AddRange(
Enumerable.Repeat(
new SpawnGroup.SpawnEntry
{
prefab = new GameObjectRef { guid = turretGuid },
weight = 1,
mobile = false,
},
tierConfig.AutoTurretConfig.Total
)
);
}
}
private void AddLootEntries(DungeonTierConfig tierConfig, List<SpawnGroup.SpawnEntry> spawnEntries)
{
var prefabName = "assets/prefabs/deployable/woodenbox/woodbox_deployed.prefab";
if (GameManifest.pathToGuid.TryGetValue(prefabName, out var boxGuid))
{
spawnEntries.AddRange(
Enumerable.Repeat(
new SpawnGroup.SpawnEntry
{
prefab = new GameObjectRef { guid = boxGuid },
weight = 1,
mobile = false,
},
tierConfig.TotalLootBoxes
)
);
}
}
private void ConfigureTurret(AutoTurret turret, TurretConfig turretConfig)
{
turret.health = turretConfig.Health;
turret.SetMaxHealth(turret.health);
var weapon = ItemManager.CreateByName(turretConfig.WeaponShortName);
weapon?.MoveToContainer(turret.inventory, 0);
var laserSight = ItemManager.CreateByName("weapon.mod.lasersight");
laserSight?.MoveToContainer(weapon.contents);
turret.UpdateAttachedWeapon();
}
private void ConfigureStorage(StorageContainer storage)
{
storage.skinID = _configData.LootBoxConfig.SmallWoodBoxSkinID;
AddCodeLock(storage);
PopulateStorage(storage.inventory, _configData.LootBoxConfig.LootItems);
}
private void AddCodeLock(StorageContainer storage)
{
var prefabName = "assets/prefabs/locks/keypad/lock.code.prefab";
var codeLock = GameManager.server.CreateEntity(prefabName) as CodeLock;
if (codeLock == null)
return;
codeLock.SetParent(storage, storage.GetSlotAnchorName(BaseEntity.Slot.Lock));
codeLock.Spawn();
codeLock.code = Random.Range(1000, 9999).ToString();
codeLock.hasCode = true;
codeLock.SetFlag(BaseEntity.Flags.Locked, true);
}
private void PopulateStorage(ItemContainer container, List<ItemConfig> lootItems)
{
var shuffledItems = lootItems.OrderBy(_ => Random.value).ToList();
var differentItemsCount = 0;
foreach (var itemConfig in shuffledItems)
{
if (differentItemsCount >= _configData.LootBoxConfig.MaxDifferentItemsPerBox)
{
break;
}
if (Random.value * 100f <= itemConfig.InclusionChancePercentage)
{
var itemDefinition = ItemManager.FindItemDefinition(itemConfig.ShortName);
if (itemDefinition != null)
{
var amount = Random.Range(itemConfig.MinimumAmount, itemConfig.MaximumAmount + 1);
if (container.itemList.Count < container.capacity)
{
var item = ItemManager.Create(itemDefinition, amount);
item.MoveToContainer(container);
differentItemsCount++;
}
}
}
}
}
private List<ActiveDungeon> RemoveInactiveDungeons()
{
var removedDungeons = new List<ActiveDungeon>();
foreach (var dungeon in _dungeonData.ActiveDungeons.ToList())
{
if (CanBeRemoved(dungeon))
{
removedDungeons.Add(dungeon);
DestroyDungeon(dungeon);
}
}
return removedDungeons;
}
private void DestroyDungeon(ActiveDungeon dungeon)
{
DestroyEntities(dungeon);
RemoveDungeonMarkers(dungeon);
_dungeonData.ActiveDungeons.Remove(dungeon);
var portal = BaseNetworkable.serverEntities.Find(new NetworkableId(dungeon.PortalId)) as BasePortal;
portal?.Kill(BaseNetworkable.DestroyMode.None);
SaveData();
}
private void DestroyEntities(ActiveDungeon dungeon)
{
foreach (var entityId in dungeon.EntityIds)
{
var entity = BaseNetworkable.serverEntities.Find(new NetworkableId(entityId)) as BaseEntity;
entity?.Kill(BaseNetworkable.DestroyMode.None);
}
dungeon.EntityIds.Clear();
}
private void RemoveDungeonMarkers(ActiveDungeon dungeon)
{
foreach (var marker in _dungeonData.ActiveDungeonMarkers.Where(m => m.Position == dungeon.Position).ToList())
{
var vendingMarker = BaseNetworkable.serverEntities.Find(new NetworkableId(marker.VendingMarker)) as BaseEntity;
var radiusMarker = BaseNetworkable.serverEntities.Find(new NetworkableId(marker.RadiusMarker)) as BaseEntity;
vendingMarker?.Kill(BaseNetworkable.DestroyMode.None);
radiusMarker?.Kill(BaseNetworkable.DestroyMode.None);
_dungeonData.ActiveDungeonMarkers.Remove(marker);
}
}
private IEnumerable<ActiveDungeon> RemoveAllDungeons()
{
var removedDungeons = new List<ActiveDungeon>(_dungeonData.ActiveDungeons);
foreach (var dungeon in removedDungeons)
{
DestroyDungeon(dungeon);
}
return removedDungeons;
}
#endregion
#region Utility Methods
private static bool IsValidSteamPlayer(BasePlayer targetPlayer) => targetPlayer != null && targetPlayer.userID.IsSteamId();
private void NotifyPlayersOfNewDungeon(Vector3 position, string tierName)
{
var gridPosition = PhoneController.PositionToGridCoord(position);
foreach (var player in BasePlayer.activePlayerList)
{
var message = Msg(player.UserIDString, "DungeonAppeared", tierName, gridPosition);
player.ChatMessage(message);
player.ShowToast(GameTip.Styles.Blue_Normal, message);
}
}
private void NotifyPlayer(BasePlayer player, DungeonSpawnResult result)
{
var messageKey = result switch
{
DungeonSpawnResult.Success => "DungeonSpawnedSuccess",
DungeonSpawnResult.MaxActiveDungeonsReached => "MaxActiveDungeonsReached",
DungeonSpawnResult.NoValidSpawnLocation => "DungeonSpawnFailed",
_ => null,
};
player.ChatMessage(Msg(player.UserIDString, messageKey));
}
private void NotifyPlayerOnRemoval(BasePlayer player, IEnumerable<ActiveDungeon> removedDungeons, string noDungeonMessage)
{
if (!removedDungeons.Any())
{
player.ChatMessage(Msg(player.UserIDString, noDungeonMessage));
return;
}
foreach (var dungeon in removedDungeons)
{
var gridPosition = PhoneController.PositionToGridCoord(dungeon.Position);
player.ChatMessage(Msg(player.UserIDString, "DungeonRemovedDetail", dungeon.TierName, gridPosition));
}
}
private void LogDungeonSpawnResult(DungeonSpawnResult result)
{
var logMessage = result switch
{
DungeonSpawnResult.Success => "Dungeon spawned successfully.",
DungeonSpawnResult.MaxActiveDungeonsReached => "Cannot spawn: Maximum number of active dungeons reached.",
DungeonSpawnResult.NoValidSpawnLocation => "Could not find a valid location to spawn a new dungeon.",
_ => null,
};
if (logMessage != null)
Puts(logMessage);
}
private void LogDungeonRemoval(ActiveDungeon dungeon)
{
var gridPosition = PhoneController.PositionToGridCoord(dungeon.Position);
Puts($"Removed inactive dungeon of tier {dungeon.TierName} at grid {gridPosition}.");
}
#endregion
#region Helper and Utility Methods
private bool IsAdmin(BasePlayer player) => player != null && player.IsAdmin;
private bool IsDungeonEntity(BaseEntity entity) => entity != null && _dungeonData.ActiveDungeons.Any(d => d.EntityIds.Contains(entity.net.ID.Value));
private string GetRandomDungeonPrefab()
{
var prefabs = new List<string>();
if (_configData.DungeonSpawn.EnableXmasDungeon)
prefabs.Add("assets/prefabs/missions/portal/xmasportalentry.prefab");
if (_configData.DungeonSpawn.EnableHalloweenDungeon)
prefabs.Add("assets/prefabs/missions/portal/halloweenportalentry.prefab");
return prefabs.Count > 0 ? prefabs[Random.Range(0, prefabs.Count)] : null;
}
private static bool IsValidPosition(Vector3 position, out Vector3 suitablePosition, out Quaternion suitableRotation)
{
suitablePosition = Vector3.zero;
suitableRotation = Quaternion.identity;
if (!CollidersChecks.GetTerrainInfo(position, out var hitInfo))
return false;
if (CollidersChecks.InsideRock(position, 10f))
return false;
if (CollidersChecks.InWater(position))
return false;
if (CollidersChecks.OnRoadOrRail(position))
return false;
if (CollidersChecks.InNoBuildZone(position, 10f))
return false;
return SetSuitablePositionAndRotation(position, hitInfo, out suitablePosition, out suitableRotation);
}
private static bool SetSuitablePositionAndRotation(Vector3 position, RaycastHit hitInfo, out Vector3 suitablePosition, out Quaternion suitableRotation)
{
suitablePosition = hitInfo.point;
suitableRotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
return true;
}
private void CreateDungeonMarkers(Vector3 position, string tierName)
{
var prefabVendingMarker = "assets/prefabs/deployable/vendingmachine/vending_mapmarker.prefab";
var vendingMarker = CreateMapMarker<VendingMachineMapMarker>(prefabVendingMarker, position);
vendingMarker.OwnerID = OwnerID;
vendingMarker.markerShopName = $"Dungeon: {tierName}";
vendingMarker.SendNetworkUpdate();
var prefabRadiusMarker = "assets/prefabs/tools/map/genericradiusmarker.prefab";
var radiusMarker = CreateMapMarker<MapMarkerGenericRadius>(prefabRadiusMarker, position);
radiusMarker.OwnerID = OwnerID;
radiusMarker.alpha = 0.75f;
radiusMarker.radius = 0.5f;
radiusMarker.color2 = GetMarkerColorByTier(tierName);
radiusMarker.SendUpdate();
radiusMarker.SendNetworkUpdate();
_dungeonData.ActiveDungeonMarkers.Add(
new DungeonMarker
{
VendingMarker = vendingMarker.net.ID.Value,
RadiusMarker = radiusMarker.net.ID.Value,
Position = position,
Tier = tierName,
}
);
}
private T CreateMapMarker<T>(string prefab, Vector3 position)
where T : BaseEntity
{
var entity = GameManager.server.CreateEntity(prefab, position) as T;
entity?.Spawn();
return entity;
}
private static Color GetMarkerColorByTier(string tierName)
{
return tierName switch
{
"Easy" => Color.green,
"Normal" or "Medium" => Color.yellow,
"Hard" => Color.red,
"Nightmare" => Color.black,
_ => Color.white,
};
}
private bool CanBeRemoved(ActiveDungeon dungeon)
{
return AreAllNpcsGone(dungeon) && NoPlayersInside(dungeon);
}
private bool AreAllNpcsGone(ActiveDungeon dungeon)
{
return dungeon.NpcIds.All(npcId =>
{
var npc = BaseNetworkable.serverEntities.Find(new NetworkableId(npcId)) as BaseEntity;
return npc == null || npc.IsDestroyed;
});
}
private bool NoPlayersInside(ActiveDungeon dungeon)
{
var dynamicDungeon = BaseNetworkable.serverEntities.Find(new NetworkableId(dungeon.DynamicDungeonId)) as ProceduralDynamicDungeon;
return dynamicDungeon == null || !dynamicDungeon.ContainsAnyPlayers();
}
private bool TryFindDungeonSpawnPoint(out Vector3 position, out Quaternion rotation)
{
var mapSize = World.Size;
for (var attempt = 0; attempt < 1000; attempt++)
{
var candidatePosition = new Vector3(Random.Range(-mapSize / 2, mapSize / 2), 0, Random.Range(-mapSize / 2, mapSize / 2));
if (IsValidPosition(candidatePosition, out position, out rotation) && IsSafeFromOtherDungeons(position) && !IsCloseToMonuments(position))
{
return true;
}
}
position = Vector3.zero;
rotation = Quaternion.identity;
return false;
}
private bool IsSafeFromOtherDungeons(Vector3 position)
{
return _dungeonData.ActiveDungeons.All(dungeon => Vector3.Distance(position, dungeon.Position) >= _configData.DungeonSpawn.MinDistanceBetweenDungeons);
}
private bool IsCloseToMonuments(Vector3 position)
{
return TerrainMeta.Path.Monuments.Any(mon => Vector3.Distance(position, mon.transform.position) < _configData.DungeonSpawn.MinDistanceFromMonuments);
}
private DungeonTierConfig GetDungeonTier(int cellCount)
{
return cellCount switch
{
< 5 => _configData.Tiers.Easy,
< 8 => _configData.Tiers.Normal,
< 12 => _configData.Tiers.Medium,
< 16 => _configData.Tiers.Hard,
_ => _configData.Tiers.Nightmare,
};
}
#endregion
#region Cleanup
private void Unload()
{
RemoveAllDungeons();
_autoSpawnTimer?.Destroy();
_dungeonCheckTimer?.Destroy();
_updateMarkersTimer?.Destroy();
}
#endregion
#region Configuration
private class ConfigData
{
[JsonProperty(PropertyName = "Dungeon Spawn Settings")]
public DungeonSpawnSettings DungeonSpawn { get; set; } = new DungeonSpawnSettings();
[JsonProperty(PropertyName = "Tiers")]
public DungeonTiers Tiers { get; set; } = new DungeonTiers();
[JsonProperty(PropertyName = "Loot Box Config")]
public LootBoxConfig LootBoxConfig { get; set; } = new LootBoxConfig();
[JsonProperty(PropertyName = "Version")]
public VersionNumber Version { get; set; }
}
private class DungeonTiers
{
[JsonProperty(PropertyName = "Easy")]
public DungeonTierConfig Easy { get; set; } =
new DungeonTierConfig
{
TotalLootBoxes = 1,
NpcSpawnConfigs = new List<NpcSpawnConfig>
{
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_full_pistol.prefab", Total = 1 },
},
AutoTurretConfig = new TurretConfig
{
Total = 1,
Health = 300,
WeaponShortName = "pistol.revolver",
},
};
[JsonProperty(PropertyName = "Normal")]
public DungeonTierConfig Normal { get; set; } =
new DungeonTierConfig
{
TotalLootBoxes = 2,
NpcSpawnConfigs = new List<NpcSpawnConfig>
{
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_full_pistol.prefab", Total = 3 },
},
AutoTurretConfig = new TurretConfig
{
Total = 2,
Health = 600,
WeaponShortName = "smg.2",
},
};
[JsonProperty(PropertyName = "Medium")]
public DungeonTierConfig Medium { get; set; } =
new DungeonTierConfig
{
TotalLootBoxes = 4,
NpcSpawnConfigs = new List<NpcSpawnConfig>
{
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_full_pistol.prefab", Total = 4 },
},
AutoTurretConfig = new TurretConfig
{
Total = 3,
Health = 1000,
WeaponShortName = "smg.mp5",
},
};
[JsonProperty(PropertyName = "Hard")]
public DungeonTierConfig Hard { get; set; } =
new DungeonTierConfig
{
TotalLootBoxes = 5,
NpcSpawnConfigs = new List<NpcSpawnConfig>
{
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_full_pistol.prefab", Total = 4 },
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_full_pistol.prefab", Total = 3 },
},
AutoTurretConfig = new TurretConfig
{
Total = 4,
Health = 1500,
WeaponShortName = "rifle.ak",
},
};
[JsonProperty(PropertyName = "Nightmare")]
public DungeonTierConfig Nightmare { get; set; } =
new DungeonTierConfig
{
TotalLootBoxes = 6,
NpcSpawnConfigs = new List<NpcSpawnConfig>
{
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_full_pistol.prefab", Total = 5 },
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_full_pistol.prefab", Total = 5 },
},
AutoTurretConfig = new TurretConfig
{
Total = 6,
Health = 2000,
WeaponShortName = "rifle.ak",
},
};
}
private class DungeonTierConfig
{
[JsonProperty(PropertyName = "Maximum Loot Boxes")]
public int TotalLootBoxes { get; set; }
[JsonProperty(PropertyName = "NPC Spawn Configs", ObjectCreationHandling = ObjectCreationHandling.Replace)]
public List<NpcSpawnConfig> NpcSpawnConfigs { get; set; }
[JsonProperty(PropertyName = "Auto Turret Config")]
public TurretConfig AutoTurretConfig { get; set; } = new TurretConfig();
}
public class LootBoxConfig
{
[JsonProperty(PropertyName = "Small Wood Box Skin ID")]
public ulong SmallWoodBoxSkinID { get; set; } = 2998755525;
[JsonProperty(PropertyName = "Max Different Items Per Box")]
public int MaxDifferentItemsPerBox { get; set; } = 6;
[JsonProperty(PropertyName = "Loot Items", ObjectCreationHandling = ObjectCreationHandling.Replace)]
public List<ItemConfig> LootItems { get; set; } =
new List<ItemConfig>
{
new ItemConfig
{
ShortName = "ammo.rifle",
InclusionChancePercentage = 15,
MinimumAmount = 100,
MaximumAmount = 300,
},
};
}
private class NpcSpawnConfig
{
[JsonProperty(PropertyName = "Prefab")]
public string PrefabName { get; set; }
[JsonProperty(PropertyName = "Maximum")]
public int Total { get; set; }
}
private class TurretConfig
{
[JsonProperty(PropertyName = "Maximum")]
public int Total { get; set; } = 3;
[JsonProperty(PropertyName = "Health")]
public float Health { get; set; } = 1000f;
[JsonProperty(PropertyName = "Weapon Short Name")]
public string WeaponShortName { get; set; } = "rifle.ak";
}
private class DungeonSpawnSettings
{
[JsonProperty(PropertyName = "Enable Xmas Dungeon")]
public bool EnableXmasDungeon { get; set; } = true;
[JsonProperty(PropertyName = "Enable Halloween Dungeon")]
public bool EnableHalloweenDungeon { get; set; } = true;
[JsonProperty(PropertyName = "Max Total Active Dungeons")]
public int MaxTotalActiveDungeons { get; set; } = 4;
[JsonProperty(PropertyName = "Min Distance From Monuments")]
public float MinDistanceFromMonuments { get; set; } = 100f;
[JsonProperty(PropertyName = "Min Distance Between Dungeons")]
public float MinDistanceBetweenDungeons { get; set; } = 50f;
[JsonProperty(PropertyName = "Auto Spawn Cycle Interval")]
public float AutoSpawnCycleInterval { get; set; } = 60f;
[JsonProperty(PropertyName = "Enable Auto Spawn")]
public bool EnableAutoSpawn { get; set; } = true;
}
public class ItemConfig
{
[JsonProperty(PropertyName = "ShortName")]
public string ShortName { get; set; }
[JsonProperty(PropertyName = "Inclusion Chance Percentage")]
public float InclusionChancePercentage { get; set; }
[JsonProperty(PropertyName = "Minimum Amount")]
public int MinimumAmount { get; set; }
[JsonProperty(PropertyName = "Maximum Amount")]
public int MaximumAmount { get; set; }
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
_configData = Config.ReadObject<ConfigData>() ?? new ConfigData();
}
catch (Exception ex)
{
PrintError($"The configuration file is corrupt: {ex}");
LoadDefaultConfig();
}
SaveConfig();
}
protected override void LoadDefaultConfig()
{
PrintWarning("Creating a new configuration file.");
_configData = new ConfigData { Version = Version };
}
protected override void SaveConfig()
{
Config.WriteObject(_configData, true);
}
#endregion
#region Localization
protected override void LoadDefaultMessages()
{
lang.RegisterMessages(
new Dictionary<string, string>
{
["NoInactiveDungeonsToRemove"] = "There are no inactive dungeons to remove.",
["NoDungeonsToRemove"] = "There are no dungeons to remove.",
["DungeonRemovedDetail"] = "Removed dungeon of tier {0} at grid {1}.",
["AttemptSpawnDungeons"] = "Attempting to spawn dungeons.",
["DungeonAppeared"] = "A {0} Dungeon has appeared at grid {1}!",
["DungeonSpawnFailed"] = "Could not find a valid location to spawn a new dungeon.",
["DungeonSpawnedSuccess"] = "Dungeon spawned successfully.",
["MaxActiveDungeonsReached"] = "Cannot spawn: Maximum number of active dungeons reached.",
},
this
);
}
private string Msg(string userId, string key, params object[] args)
{
return string.Format(lang.GetMessage(key, this, userId), args);
}
#endregion
#region Data Classes
private class DungeonData
{
public List<ActiveDungeon> ActiveDungeons { get; set; } = new List<ActiveDungeon>();
public List<DungeonMarker> ActiveDungeonMarkers { get; set; } = new List<DungeonMarker>();
}
private class ActiveDungeon
{
public ulong PortalId { get; set; }
public ulong DynamicDungeonId { get; set; }
public Vector3 Position { get; set; }
public string TierName { get; set; }
public List<ulong> EntityIds { get; set; } = new List<ulong>();
public List<ulong> NpcIds { get; set; } = new List<ulong>();
}
private class DungeonMarker
{
public ulong VendingMarker { get; set; }
public ulong RadiusMarker { get; set; }
public Vector3 Position { get; set; }
public string Tier { get; set; }
}
#endregion
#region Collider Checks
public static class CollidersChecks
{
public static bool InsideRock(Vector3 position, float radius)
{
List<Collider> colliders = Pool.GetList<Collider>();
Vis.Colliders(position, radius, colliders, Layers.Mask.World, QueryTriggerInteraction.Ignore);
bool isInsideRock = colliders.Any(collider =>
collider.name.Contains("rock", StringComparison.OrdinalIgnoreCase)
|| collider.name.Contains("cliff", StringComparison.OrdinalIgnoreCase)
|| collider.name.Contains("formation", StringComparison.OrdinalIgnoreCase)
);
Pool.FreeList(ref colliders);
return isInsideRock;
}
public static bool GetTerrainInfo(Vector3 position, out RaycastHit hitInfo)
{
return Physics.Linecast(position + Vector3.up, position - Vector3.up, out hitInfo, Layers.Mask.Terrain);
}
public static bool InWater(Vector3 position) => WaterLevel.Test(position, false, false);
public static bool OnRoadOrRail(Vector3 position) => (TerrainMeta.TopologyMap.GetTopology(position) & (int)TerrainTopology.Enum.Road) != 0;
public static bool InNoBuildZone(Vector3 position, float radius) => Physics.CheckSphere(position, radius, Layers.Mask.Prevent_Building, QueryTriggerInteraction.Ignore);
}
#endregion
#region Turret MonoBehaviour
public class TurretMonoBehaviour : MonoBehaviour
{
private AutoTurret _turret;
private const float SearchDistance = 15f;
private const int MaxReserveAmmo = 1000;
private void Awake()
{
InitializeTurret();
}
private void InitializeTurret()
{
_turret = GetComponent<AutoTurret>();
var triggerCollider = _turret.targetTrigger.GetComponent<SphereCollider>();
triggerCollider.enabled = false;
_turret.SetPeacekeepermode(false);
_turret.InitiateStartup();
_turret.SetIsOnline(true);
_turret.CancelInvoke(_turret.ServerTick);
_turret.SetTarget(null);
_turret.InvokeRepeating(PerformTurretCycle, 1f, 0.01f);
_turret.InvokeRepeating(ScanForTargets, 2f, 1f);
_turret.isLootable = false;
_turret.dropFloats = false;
_turret.dropsLoot = false;
_turret.SendNetworkUpdateImmediate();
InvokeRepeating(nameof(RefillAmmo), 2f, 30f);
}
private void RefillAmmo()
{
if (_turret.AttachedWeapon is not BaseProjectile baseProjectile || baseProjectile.primaryMagazine?.ammoType == null)
return;
var ammoType = baseProjectile.primaryMagazine.ammoType;
int currentAmmoCount = CalculateCurrentAmmoCount(ammoType);
if (currentAmmoCount < MaxReserveAmmo)
{
int ammoNeeded = MaxReserveAmmo - currentAmmoCount;
Item ammoItem = ItemManager.Create(ammoType, ammoNeeded);
ammoItem?.MoveToContainer(_turret.inventory);
_turret.UpdateTotalAmmo();
_turret.EnsureReloaded();
_turret.SendNetworkUpdateImmediate();
}
}
private int CalculateCurrentAmmoCount(ItemDefinition ammoType)
{
return _turret.inventory.itemList.Where(item => item.info == ammoType).Sum(item => item.amount);
}
private void ScanForTargets()
{
var entityContents = _turret.targetTrigger.entityContents ??= new HashSet<BaseEntity>();
entityContents.Clear();
int foundTargets = BaseEntity.Query.Server.GetPlayersInSphereFast(transform.position, SearchDistance, AIBrainSenses.playerQueryResults, IsTargetValid);
if (foundTargets == 0)
return;
_turret.authDirty = true;
for (int i = 0; i < foundTargets; i++)
{
var player = AIBrainSenses.playerQueryResults[i];
if (Interface.CallHook("OnEntityEnter", _turret.targetTrigger, player) != null || player.IsSleeping() || (player.InSafeZone() && !player.IsHostile()))
continue;
entityContents.Add(player);
}
}
private bool IsTargetValid(BasePlayer player) => player != null && !player.IsNpc;
private void PerformTurretCycle()
{
if (_turret.isClient || _turret.IsDestroyed)
return;
float deltaTime = (float)_turret.timeSinceLastServerTick;
_turret.timeSinceLastServerTick = 0f;
if (_turret.IsOnline() && !_turret.IsBeingControlled)
{
if (!_turret.HasTarget())
{
_turret.IdleTick(deltaTime);
}
else
{
ExecuteTargetEngagement();
}
}
_turret.UpdateFacingToTarget(deltaTime);
UpdateAmmoStatus();
}
private void ExecuteTargetEngagement()
{
if (Time.realtimeSinceStartup >= _turret.nextVisCheck)
{
_turret.nextVisCheck = Time.realtimeSinceStartup + Random.Range(0.2f, 0.3f);
_turret.targetVisible = _turret.ObjectVisible(_turret.target);
if (_turret.targetVisible)
_turret.lastTargetSeenTime = Time.realtimeSinceStartup;
}
_turret.EnsureReloaded();
if (ShouldFireAtTarget())
{
var weapon = _turret.GetAttachedWeapon();
FireWeapon(weapon);
}
ValidateTargetEngagement();
}
private bool ShouldFireAtTarget()
{
return Time.time >= _turret.nextShotTime
&& _turret.targetVisible
&& Mathf.Abs(_turret.AngleToTarget(_turret.target, _turret.currentAmmoGravity != 0f)) < _turret.GetMaxAngleForEngagement();
}
private void FireWeapon(BaseProjectile weapon)
{
if (weapon == null)
{
_turret.nextShotTime = Time.time + 1f;
return;
}
if (weapon.primaryMagazine.contents > 0)
{
_turret.FireAttachedGun(_turret.AimOffset(_turret.target), _turret.aimCone, null, _turret.PeacekeeperMode() ? _turret.target : null);
float delay = weapon.isSemiAuto ? weapon.repeatDelay * 1.5f : weapon.repeatDelay;
delay = weapon.ScaleRepeatDelay(delay);
_turret.nextShotTime = Time.time + delay;
}
else
{
_turret.nextShotTime = Time.time + 5f;
}
}
private void ValidateTargetEngagement()
{
var targetPlayer = _turret.target as BasePlayer;
if (_turret.target != null && (!IsValidSteamPlayer(targetPlayer) || _turret.target.IsDead() || TimeExceedsLastSeen() || DistanceExceedsSightRange() || ShouldTargetBeReset()))
{
_turret.SetTarget(null);
}
}
private bool TimeExceedsLastSeen() => Time.realtimeSinceStartup - _turret.lastTargetSeenTime > 3f;
private bool DistanceExceedsSightRange() => Vector3.Distance(_turret.transform.position, _turret.target.transform.position) > _turret.sightRange;
private bool ShouldTargetBeReset() => _turret.PeacekeeperMode() && !_turret.IsEntityHostile(_turret.target);
private void UpdateAmmoStatus()
{
if (_turret.totalAmmoDirty && Time.time > _turret.nextAmmoCheckTime)
{
_turret.UpdateTotalAmmo();
_turret.totalAmmoDirty = false;
_turret.nextAmmoCheckTime = Time.time + 0.5f;
}
}
private void OnDestroy()
{
CancelInvoke(nameof(RefillAmmo));
}
}
#endregion
}
}
Assistant
The script you've provided is a Rust plugin for the Oxide modding platform, named "Dungeons", authored by Marte6. It adds dungeon mechanics to a Rust server, handling the auto-spawn of dungeons, their management, and their associated entities such as NPCs, turrets, and loot. Here's an overview of its functionalities:
Chat Commands: There are several admin commands implemented for managing dungeons:
/dun: Spawns a dungeon.
/removeinactivedungeons: Removes any dungeons without players and with all NPCs dead.
/removedungeons: Removes all active dungeons.
Dungeon Management:
The plugin automatically spawns dungeons at configured intervals and checks for inactive ones to remove them.
Dungeons consist of procedural setups with different tiers (Easy, Normal, Medium, Hard, Nightmare), each with specific configurations for NPCs, turrets, and loot boxes.
Spawns are represented with map markers for players to find.
Entity Configuration: Each dungeon setup involves specifying different types of entities:
NPCs: Using various NPC prefabs with defined spawn quantities.
Turrets: AutoTurrets can be configured with specific weapons and ammo refill mechanics.
Loot Boxes: Configurable loot items with defined chances and quantities.
Dungeon Location Validation:
Checks are performed to find valid, spawnable locations on the map, including avoiding proximity to other dungeons or undesired terrain features.
Server Events and Hooks:
The plugin uses Oxide hooks to interact with entity damage events, portal use, and server save operations to maintain the integrity of dungeon operations.
Turret Behavior: Custom behavior for AutoTurrets, including ammo refilling and target acquisition, using Unity's MonoBehaviour for repetitive actions.
Data Persistence: Dungeon data is stored in server files for consistency across server restarts.
Configuration and Localization: Settings are customizable through a configuration file, and messages are localizable.
This script is tailored for server administrators seeking to enhance PvE gameplay by incorporating dynamic dungeons with varying difficulty levels and reward potential. Admins can configure the plugin via JSON for fine-tuning according to their server's needs, and it requires understanding Unity's game engine concepts, particularly regarding NPCs and in-game physics, as well as Rust's modding framework.
User
quero ajustar as funções:
code
Assistant
Para ajustar as funções de modo que elas primeiro coletem todas as localizações de spawn necessárias, e só depois tentem colocar as entidades, você pode seguir uma estratégia de coleta e alocação de locais em duas etapas. Primeiramente, você coletará todas as posições de spawn possíveis antes de começar a colocar entidades. Aqui está uma maneira de implementar isso:
Coletar todos os locais de spawn disponíveis.
Associar entidades aos locais coletados.
Caso não existam locais suficientes, reutilizar os existentes para adicionar mais entidades.
Aqui está o código ajustado para refletir essa abordagem:
csharp
User
refatore e melhores essas e me mande:
code
Assistant
Para refatorar as funções e melhorar a eficiência do seu algoritmo de criação e distribuição de entidades em dungeons, podemos otimizar o fluxo e a legibilidade do código. Aqui está a versão refatorada:
csharp
User
The name 'tierConfig' does not exist in the current contextCS0103
using System;
using System.Collections.Generic;
using System.Linq;
using Facepunch;
using Newtonsoft.Json;
using Oxide.Core;
using Oxide.Core.Configuration;
using Rust;
using UnityEngine;
using Random = UnityEngine.Random;
using Time = UnityEngine.Time;
The error CS0103: The name 'tierConfig' does not exist in the current context means that the variable tierConfig is being referenced without it being declared or passed in the given context. In the CreateDungeon method, tierConfig should logically correspond to selectedTier, which is of type DungeonTierConfig.
Here's a corrected and refactored version of the relevant sections, ensuring that the variable tierConfig is properly instantiated:
csharp
User
algo mais para melhorar?
using System;
using System.Collections.Generic;
using System.Linq;
using Facepunch;
using Newtonsoft.Json;
using Oxide.Core;
using Oxide.Core.Configuration;
using Rust;
using UnityEngine;
using Random = UnityEngine.Random;
using Time = UnityEngine.Time;
Your Rust plugin Dungeons script is quite comprehensive and already well-structured. However, there are some areas where you could improve clarity, maintainability, and potentially performance. Here are some suggestions:
Code Comments and Documentation
Add Comments: Each function and critical section of your code should have clear comments describing what it does. This helps with maintainability and readability, especially for others who might work with your code.
Error Handling
Add Error Handling: Consider adding logging in critical parts of your code where it interacts with the game world (e.g., entity creation). This can help diagnose problems if something goes wrong. For example, add logging when a prefab is missing or a certain entity fails to spawn.
Optimization
Reduce Redundant Check: In PopulateDungeon, you're determining spawn locations even when there aren't enough allSpawnEntries. You can optimize by first checking if there are enough spawn entries before proceeding.
Caching Resources: For recurring resource access like prefab paths, consider caching the result if these accesses are computationally expensive or slow.
Simplified Code
Use LINQ Wisely: Where applicable, use LINQ for more readable and concise expressions, like filtering or accumulating lists.
Functionality Separation: Consider refactoring larger methods such as PopulateDungeon by breaking them into smaller, more manageable sub-methods for clarity.
Threading
Asynchronous Tasks: If there are tasks like data loading that could take a substantial amount of time or might block the main thread, consider running them asynchronously.
Improved Logic or Design Patterns
Switch to Dictionary-Based Strategies: When dealing with configurations and actions based on dungeon tiers, you can use a dictionary to map tiers to functions or configurations for better organization and easier extensions.
Plugin Structure
Lifecycle Management: Ensure proper cleanup and resource management on unload (Unload method). This includes unsubscribing from events and destroying timers appropriately to prevent memory leaks or dangling references.
Code Example with Improvements
Below is a conceptual example of how you might address some of the above points:
User
sóme diga o que modificar
Assistant
Based on the provided script, here are targeted suggestions for modifications you can implement to improve your code:
Enhance Error Handling:
Add checks and error logs when crucial operations have potential to fail, like when creating entities or reading configuration files. This helps with troubleshooting any issues during runtime.
Comments and Documentation:
Add comments to complex sections of code to clarify functionality, especially within methods like PopulateDungeon and CreateDungeonMarkers. This aids in readability and future debugging.
Refactor Long Methods:
Split larger methods such as PopulateDungeon into smaller helper methods. This promotes a single responsibility principle, improving readability and testability.
Optimize Spawn Logic:
Before attempting to collect spawn locations or place entities, ensure you need to perform these operations. If entries are abundant, distributing entities across locations in advance minimizes runtime checks.
Generic Method Usage:
Use generic methods for repeated tasks like entity setup or marker creation, as seen in CreateMapMarker<T>. This decreases redundancy and centralizes logic.
Consolidate Configuration Access:
Extract configuration retrievals into helper properties or methods, which makes powerful setup adjustments easier and creates a separation between configuration knowledge and business logic.
Asynchronous Data Handling:
Long-running tasks, such as data loading or heavy computations (if applicable), should be considered for asynchronous handling to avoid blocking the main server thread.
Here’s an illustrative way to refactor using some of these suggestions:
private DungeonTierConfig CreateDungeon(Vector3 position, Quaternion rotation)
{
string prefabPath = GetRandomDungeonPrefab();
if (string.IsNullOrEmpty(prefabPath))
return null;
var dungeonPortal = GameManager.server.CreateEntity(prefabPath, position, rotation) as BasePortal;
if (dungeonPortal == null)
return null;
dungeonPortal.OwnerID = OwnerID;
dungeonPortal.Spawn();
var proceduralDungeon = (dungeonPortal as XmasDungeon)?.dungeonInstance.Get(true) ?? (dungeonPortal as HalloweenDungeon)?.dungeonInstance.Get(true);
if (!(dungeonPortal is XmasDungeon xmasDungeon || dungeonPortal is HalloweenDungeon halloweenDungeon))
{
dungeonPortal.Kill(BaseNetworkable.DestroyMode.None);
return null;
}
if (proceduralDungeon == null || proceduralDungeon.spawnedCells.Count < 1)
{
dungeonPortal.Kill(BaseNetworkable.DestroyMode.None);
return null;
}
var selectedTier = GetDungeonTier(proceduralDungeon.spawnedCells.Count);
var activeDungeon = new ActiveDungeon
{
PortalId = dungeonPortal.net.ID.Value,
DynamicDungeonId = proceduralDungeon.net.ID.Value,
Position = position,
TierName = _tierNameMap[selectedTier],
EntityIds = new List<ulong>(),
};
NextTick(() => PopulateDungeon(dungeonPortal, selectedTier, activeDungeon));
_dungeonData.ActiveDungeons.Add(activeDungeon);
return selectedTier;
}
private void PopulateDungeon(BasePortal dungeonPortal, DungeonTierConfig tierConfig, ActiveDungeon activeDungeon)
{
var proceduralDungeon = (dungeonPortal as XmasDungeon)?.dungeonInstance.Get(true) ?? (dungeonPortal as HalloweenDungeon)?.dungeonInstance.Get(true);
if (proceduralDungeon == null)
{
Puts("Procedural dungeon instance is null.");
return;
}
var allSpawnEntries = GenerateSpawnEntries(tierConfig);
foreach (var cell in proceduralDungeon.spawnedCells)
{
PopulateEntitiesInCell(cell, allSpawnEntries, tierConfig, activeDungeon);
}
ValidateAndAttemptRetries(proceduralDungeon, tierConfig, activeDungeon, allSpawnEntries);
}
private void PopulateEntitiesInCell(ProceduralDungeonCell cell, List<SpawnGroup.SpawnEntry> spawnEntries, DungeonTierConfig tierConfig, ActiveDungeon activeDungeon)
{
foreach (var group in cell.spawnGroups)
{
int availableSlots = group.maxPopulation - group.spawnInstances.Count;
if (availableSlots <= 0)
continue;
for (int i = 0; i < availableSlots && spawnEntries.Count > 0; i++)
{
var entry = spawnEntries[0];
Vector3 spawnPosition;
Quaternion spawnRotation;
if (TryGetSpawnPosition(group, out spawnPosition, out spawnRotation))
{
var entity = GameManager.server.CreateEntity(entry.prefab.resourcePath, spawnPosition, spawnRotation);
if (entity != null)
{
entity.OwnerID = OwnerID;
entity.Spawn();
if (entity is NPCPlayer)
{
activeDungeon.NpcIds.Add(entity.net.ID.Value);
}
if (entity is AutoTurret turret)
{
ConfigureTurret(turret, tierConfig.AutoTurretConfig);
turret.gameObject.AddComponent<TurretMonoBehaviour>();
}
if (entity is StorageContainer storage)
{
ConfigureStorage(storage);
}
activeDungeon.EntityIds.Add(entity.net.ID.Value);
group.spawnInstances.Add(entity.GetComponent<SpawnPointInstance>());
}
spawnEntries.RemoveAt(0);
}
}
}
}
private bool TryGetSpawnPosition(SpawnGroup group, out Vector3 position, out Quaternion rotation)
{
foreach (var spawnPoint in group.spawnPoints)
{
if (spawnPoint != null && spawnPoint.IsAvailableTo(null))
{
spawnPoint.GetLocation(out position, out rotation);
return true;
}
}
position = Vector3.zero;
rotation = Quaternion.identity;
return false;
}
private void ValidateAndAttemptRetries(ProceduralDynamicDungeon proceduralDungeon, DungeonTierConfig tierConfig, ActiveDungeon activeDungeon, List<SpawnGroup.SpawnEntry> remainingEntries)
{
int retries = 3;
while (remainingEntries.Count > 0 && retries > 0)
{
Puts($"Retrying placement for {remainingEntries.Count} missing entities.");
foreach (var cell in proceduralDungeon.spawnedCells)
{
PopulateEntitiesInCell(cell, remainingEntries, tierConfig, activeDungeon);
}
retries--;
}
int entitiesPlaced = activeDungeon.EntityIds.Count;
int expectedEntities = CalculateTotalEntities(tierConfig);
if (entitiesPlaced < expectedEntities)
{
Puts($"Warning: Only placed {entitiesPlaced}/{expectedEntities} entities.");
}
else
{
Puts("All entities placed successfully.");
}
}
private int CalculateTotalEntities(DungeonTierConfig tierConfig)
{
int total = tierConfig.TotalLootBoxes;
total += tierConfig.NpcSpawnConfigs.Sum(npcConfig => npcConfig.Total);
total += tierConfig.AutoTurretConfig.Total;
return total;
}
--
pra primeiro pegar todos spawn locations que preciso, conforme a quantidade de entidades que preciso colocar, e depois com os spawn locations armazenados em uma var, tentar colocar as entidades que preciso neles, se não tiver o suficiente, colocar mais de uma entidade em cada conforme precisar
private void PopulateDungeon(BasePortal dungeonPortal, DungeonTierConfig tierConfig, ActiveDungeon activeDungeon)
{
var proceduralDungeon = (dungeonPortal as XmasDungeon)?.dungeonInstance.Get(true) ?? (dungeonPortal as HalloweenDungeon)?.dungeonInstance.Get(true);
if (proceduralDungeon == null)
{
Puts("Procedural dungeon instance is null.");
return;
}
// Coletar todos os locais de spawn possíveis
var spawnLocations = new List<(Vector3 Position, Quaternion Rotation)>();
foreach (var cell in proceduralDungeon.spawnedCells)
{
foreach (var group in cell.spawnGroups)
{
foreach (var spawnPoint in group.spawnPoints)
{
if (spawnPoint != null && spawnPoint.IsAvailableTo(null))
{
Vector3 position;
Quaternion rotation;
spawnPoint.GetLocation(out position, out rotation);
spawnLocations.Add((position, rotation));
}
}
}
}
// Calcular o total de entidades que precisamos colocar
int totalEntitiesNeeded = CalculateTotalEntities(tierConfig);
// Garantir que temos locais de spawn suficientes
if (spawnLocations.Count == 0)
{
Puts("No valid spawn locations available.");
return;
}
// Reutilizar locais de spawn, se necessário
var allSpawnEntries = GenerateSpawnEntries(tierConfig);
int locationIndex = 0;
while (allSpawnEntries.Count > 0)
{
var entry = allSpawnEntries[0];
var (spawnPosition, spawnRotation) = spawnLocations[locationIndex];
// Criar a entidade na localização de spawn e configurá-la
var entity = GameManager.server.CreateEntity(entry.prefab.resourcePath, spawnPosition, spawnRotation);
if (entity != null)
{
entity.OwnerID = OwnerID;
entity.Spawn();
if (entity is NPCPlayer)
{
activeDungeon.NpcIds.Add(entity.net.ID.Value);
}
if (entity is AutoTurret turret)
{
ConfigureTurret(turret, tierConfig.AutoTurretConfig);
turret.gameObject.AddComponent<TurretMonoBehaviour>();
}
if (entity is StorageContainer storage)
{
ConfigureStorage(storage);
}
activeDungeon.EntityIds.Add(entity.net.ID.Value);
}
allSpawnEntries.RemoveAt(0);
locationIndex = (locationIndex + 1) % spawnLocations.Count; // Circula pelos locais de spawn disponíveis
}
Puts($"Placed {activeDungeon.EntityIds.Count}/{totalEntitiesNeeded} entities.");
}
private int CalculateTotalEntities(DungeonTierConfig tierConfig)
{
int total = tierConfig.TotalLootBoxes;
total += tierConfig.NpcSpawnConfigs.Sum(npcConfig => npcConfig.Total);
total += tierConfig.AutoTurretConfig.Total;
return total;
}
Explicação:
Coleta de Locais de Spawn: Primeiro, coletamos todos os locais de spawn disponíveis das células do calabouço. Isso é armazenado na lista spawnLocations.
Distribuição de Entidades: Usamos um loop que itera sobre a lista de spawnEntries, que são as entidades que precisamos criar. Para cada entrada, criamos uma entidade na localização de spawn corrente.
Reutilização de Locais: Se ficarmos sem novos locais de spawn, voltamos ao início da lista de spawnLocations e continuamos alocando entidades. Isso é feito utilizando um índice (locationIndex) que circula através dos locais disponíveis, garantindo que todos são utilizados uniformemente.
Esse método garante que você tente colocar todas as entidades no jogo, mesmo que não haja locais de spawn dedicados suficientes, maximizando o uso dos locais disponíveis.
private DungeonSpawnResult AutoSpawnDungeon()
{
if (_dungeonData.ActiveDungeons.Count >= _configData.DungeonSpawn.MaxTotalActiveDungeons)
{
return DungeonSpawnResult.MaxActiveDungeonsReached;
}
if (TryFindDungeonSpawnPoint(out var position, out var rotation))
{
var selectedTier = CreateDungeon(position, rotation);
if (selectedTier != null)
{
var tierName = _tierNameMap[selectedTier];
CreateDungeonMarkers(position, tierName);
NotifyPlayersOfNewDungeon(position, tierName);
return DungeonSpawnResult.Success;
}
}
return DungeonSpawnResult.NoValidSpawnLocation;
}
private DungeonTierConfig CreateDungeon(Vector3 position, Quaternion rotation)
{
string prefabPath = GetRandomDungeonPrefab();
if (string.IsNullOrEmpty(prefabPath))
return null;
var dungeonPortal = GameManager.server.CreateEntity(prefabPath, position, rotation) as BasePortal;
if (dungeonPortal == null)
return null;
dungeonPortal.OwnerID = OwnerID;
dungeonPortal.Spawn();
var proceduralDungeon = (dungeonPortal as XmasDungeon)?.dungeonInstance.Get(true) ?? (dungeonPortal as HalloweenDungeon)?.dungeonInstance.Get(true);
if (!(dungeonPortal is XmasDungeon xmasDungeon || dungeonPortal is HalloweenDungeon halloweenDungeon))
{
dungeonPortal.Kill(BaseNetworkable.DestroyMode.None);
return null;
}
if (proceduralDungeon == null || proceduralDungeon.spawnedCells.Count < 1)
{
dungeonPortal.Kill(BaseNetworkable.DestroyMode.None);
return null;
}
var selectedTier = GetDungeonTier(proceduralDungeon.spawnedCells.Count);
var activeDungeon = new ActiveDungeon
{
PortalId = dungeonPortal.net.ID.Value,
DynamicDungeonId = proceduralDungeon.net.ID.Value,
Position = position,
TierName = _tierNameMap[selectedTier],
EntityIds = new List<ulong>(),
};
NextTick(() => PopulateDungeon(dungeonPortal, selectedTier, activeDungeon));
_dungeonData.ActiveDungeons.Add(activeDungeon);
return selectedTier;
}
private void PopulateDungeon(BasePortal dungeonPortal, DungeonTierConfig tierConfig, ActiveDungeon activeDungeon)
{
var proceduralDungeon = (dungeonPortal as XmasDungeon)?.dungeonInstance.Get(true) ?? (dungeonPortal as HalloweenDungeon)?.dungeonInstance.Get(true);
if (proceduralDungeon == null)
{
Puts("Procedural dungeon instance is null.");
return;
}
// Coletar todos os locais de spawn possíveis
var spawnLocations = new List<(Vector3 Position, Quaternion Rotation)>();
foreach (var cell in proceduralDungeon.spawnedCells)
{
foreach (var group in cell.spawnGroups)
{
foreach (var spawnPoint in group.spawnPoints)
{
if (spawnPoint != null && spawnPoint.IsAvailableTo(null))
{
Vector3 position;
Quaternion rotation;
spawnPoint.GetLocation(out position, out rotation);
spawnLocations.Add((position, rotation));
}
}
}
}
// Calcular o total de entidades que precisamos colocar
int totalEntitiesNeeded = CalculateTotalEntities(tierConfig);
// Garantir que temos locais de spawn suficientes
if (spawnLocations.Count == 0)
{
Puts("No valid spawn locations available.");
return;
}
// Reutilizar locais de spawn, se necessário
var allSpawnEntries = GenerateSpawnEntries(tierConfig);
int locationIndex = 0;
while (allSpawnEntries.Count > 0)
{
var entry = allSpawnEntries[0];
var (spawnPosition, spawnRotation) = spawnLocations[locationIndex];
// Criar a entidade na localização de spawn e configurá-la
var entity = GameManager.server.CreateEntity(entry.prefab.resourcePath, spawnPosition, spawnRotation);
if (entity != null)
{
entity.OwnerID = OwnerID;
entity.Spawn();
if (entity is NPCPlayer)
{
activeDungeon.NpcIds.Add(entity.net.ID.Value);
}
if (entity is AutoTurret turret)
{
ConfigureTurret(turret, tierConfig.AutoTurretConfig);
turret.gameObject.AddComponent<TurretMonoBehaviour>();
}
if (entity is StorageContainer storage)
{
ConfigureStorage(storage);
}
activeDungeon.EntityIds.Add(entity.net.ID.Value);
}
allSpawnEntries.RemoveAt(0);
locationIndex = (locationIndex + 1) % spawnLocations.Count; // Circula pelos locais de spawn disponíveis
}
Puts($"Placed {activeDungeon.EntityIds.Count}/{totalEntitiesNeeded} entities.");
}
private int CalculateTotalEntities(DungeonTierConfig tierConfig)
{
int total = tierConfig.TotalLootBoxes;
total += tierConfig.NpcSpawnConfigs.Sum(npcConfig => npcConfig.Total);
total += tierConfig.AutoTurretConfig.Total;
return total;
}
private void PopulateEntitiesInCell(ProceduralDungeonCell cell, List<SpawnGroup.SpawnEntry> spawnEntries, DungeonTierConfig tierConfig, ActiveDungeon activeDungeon)
{
foreach (var group in cell.spawnGroups)
{
int availableSlots = group.maxPopulation - group.spawnInstances.Count;
if (availableSlots <= 0)
continue;
for (int i = 0; i < availableSlots && spawnEntries.Count > 0; i++)
{
var entry = spawnEntries[0];
Vector3 spawnPosition;
Quaternion spawnRotation;
if (TryGetSpawnPosition(group, out spawnPosition, out spawnRotation))
{
var entity = GameManager.server.CreateEntity(entry.prefab.resourcePath, spawnPosition, spawnRotation);
if (entity != null)
{
entity.OwnerID = OwnerID;
entity.Spawn();
if (entity is NPCPlayer)
{
activeDungeon.NpcIds.Add(entity.net.ID.Value);
}
if (entity is AutoTurret turret)
{
ConfigureTurret(turret, tierConfig.AutoTurretConfig);
turret.gameObject.AddComponent<TurretMonoBehaviour>();
}
if (entity is StorageContainer storage)
{
ConfigureStorage(storage);
}
activeDungeon.EntityIds.Add(entity.net.ID.Value);
group.spawnInstances.Add(entity.GetComponent<SpawnPointInstance>());
}
spawnEntries.RemoveAt(0);
}
}
}
}
private bool TryGetSpawnPosition(SpawnGroup group, out Vector3 position, out Quaternion rotation)
{
foreach (var spawnPoint in group.spawnPoints)
{
if (spawnPoint != null && spawnPoint.IsAvailableTo(null))
{
spawnPoint.GetLocation(out position, out rotation);
return true;
}
}
position = Vector3.zero;
rotation = Quaternion.identity;
return false;
}
private void ValidateAndAttemptRetries(ProceduralDynamicDungeon proceduralDungeon, DungeonTierConfig tierConfig, ActiveDungeon activeDungeon, List<SpawnGroup.SpawnEntry> remainingEntries)
{
int retries = 3;
while (remainingEntries.Count > 0 && retries > 0)
{
Puts($"Retrying placement for {remainingEntries.Count} missing entities.");
foreach (var cell in proceduralDungeon.spawnedCells)
{
PopulateEntitiesInCell(cell, remainingEntries, tierConfig, activeDungeon);
}
retries--;
}
int entitiesPlaced = activeDungeon.EntityIds.Count;
int expectedEntities = CalculateTotalEntities(tierConfig);
if (entitiesPlaced < expectedEntities)
{
Puts($"Warning: Only placed {entitiesPlaced}/{expectedEntities} entities.");
}
else
{
Puts("All entities placed successfully.");
}
}
private DungeonSpawnResult AutoSpawnDungeon()
{
if (_dungeonData.ActiveDungeons.Count >= _configData.DungeonSpawn.MaxTotalActiveDungeons)
{
return DungeonSpawnResult.MaxActiveDungeonsReached;
}
if (TryFindDungeonSpawnPoint(out var position, out var rotation))
{
var selectedTier = CreateDungeon(position, rotation);
if (selectedTier != null)
{
var tierName = _tierNameMap[selectedTier];
CreateDungeonMarkers(position, tierName);
NotifyPlayersOfNewDungeon(position, tierName);
return DungeonSpawnResult.Success;
}
}
return DungeonSpawnResult.NoValidSpawnLocation;
}
private DungeonTierConfig CreateDungeon(Vector3 position, Quaternion rotation)
{
string prefabPath = GetRandomDungeonPrefab();
if (string.IsNullOrEmpty(prefabPath))
return null;
if (!(GameManager.server.CreateEntity(prefabPath, position, rotation) is BasePortal dungeonPortal))
return null;
dungeonPortal.OwnerID = OwnerID;
dungeonPortal.Spawn();
var proceduralDungeon = GetProceduralDungeon(dungeonPortal);
if (proceduralDungeon == null || proceduralDungeon.spawnedCells.Count < 1)
{
dungeonPortal.Kill(BaseNetworkable.DestroyMode.None);
return null;
}
var selectedTier = GetDungeonTier(proceduralDungeon.spawnedCells.Count);
var activeDungeon = new ActiveDungeon
{
PortalId = dungeonPortal.net.ID.Value,
DynamicDungeonId = proceduralDungeon.net.ID.Value,
Position = position,
TierName = _tierNameMap[selectedTier],
EntityIds = new List<ulong>(),
};
NextTick(() => PopulateDungeon(proceduralDungeon, tierConfig, activeDungeon));
_dungeonData.ActiveDungeons.Add(activeDungeon);
return selectedTier;
}
private ProceduralDynamicDungeon GetProceduralDungeon(BasePortal dungeonPortal)
{
return (dungeonPortal as XmasDungeon)?.dungeonInstance.Get(true) ?? (dungeonPortal as HalloweenDungeon)?.dungeonInstance.Get(true);
}
private void PopulateDungeon(ProceduralDynamicDungeon proceduralDungeon, DungeonTierConfig tierConfig, ActiveDungeon activeDungeon)
{
var spawnLocations = GetAvailableSpawnLocations(proceduralDungeon);
if (spawnLocations.Count == 0)
{
Puts("No valid spawn locations available.");
return;
}
var allSpawnEntries = GenerateSpawnEntries(tierConfig);
int spawnIndex = 0;
foreach (var entry in allSpawnEntries)
{
var (position, rotation) = spawnLocations[spawnIndex];
var entity = GameManager.server.CreateEntity(entry.prefab.resourcePath, position, rotation);
if (entity != null)
{
InitializeEntity(entity, tierConfig, activeDungeon);
}
spawnIndex = (spawnIndex + 1) % spawnLocations.Count; // Circulate existing spawn locations
}
Puts($"Placed {activeDungeon.EntityIds.Count}/{allSpawnEntries.Count} entities.");
}
private List<(Vector3 Position, Quaternion Rotation)> GetAvailableSpawnLocations(ProceduralDynamicDungeon proceduralDungeon)
{
var locations = new List<(Vector3, Quaternion)>();
foreach (var cell in proceduralDungeon.spawnedCells)
{
foreach (var group in cell.spawnGroups)
{
foreach (var spawnPoint in group.spawnPoints)
{
if (spawnPoint != null && spawnPoint.IsAvailableTo(null))
{
spawnPoint.GetLocation(out var position, out var rotation);
locations.Add((position, rotation));
}
}
}
}
return locations;
}
private void InitializeEntity(BaseEntity entity, DungeonTierConfig tierConfig, ActiveDungeon activeDungeon)
{
entity.OwnerID = OwnerID;
entity.Spawn();
if (entity is NPCPlayer)
{
activeDungeon.NpcIds.Add(entity.net.ID.Value);
}
else if (entity is AutoTurret turret)
{
ConfigureTurret(turret, tierConfig.AutoTurretConfig);
turret.gameObject.AddComponent<TurretMonoBehaviour>();
}
else if (entity is StorageContainer storage)
{
ConfigureStorage(storage);
}
activeDungeon.EntityIds.Add(entity.net.ID.Value);
}
private int CalculateTotalEntities(DungeonTierConfig tierConfig)
{
return tierConfig.TotalLootBoxes + tierConfig.NpcSpawnConfigs.Sum(npcConfig => npcConfig.Total) + tierConfig.AutoTurretConfig.Total;
}
private void ValidateAndAttemptRetries(ProceduralDynamicDungeon proceduralDungeon, DungeonTierConfig tierConfig, ActiveDungeon activeDungeon, List<SpawnGroup.SpawnEntry> remainingEntries)
{
int retries = 3;
while (remainingEntries.Count > 0 && retries > 0)
{
Puts($"Retrying placement for {remainingEntries.Count} missing entities.");
PopulateDungeon(proceduralDungeon, tierConfig, activeDungeon);
retries--;
}
int entitiesPlaced = activeDungeon.EntityIds.Count;
int expectedEntities = CalculateTotalEntities(tierConfig);
if (entitiesPlaced < expectedEntities)
{
Puts($"Warning: Only placed {entitiesPlaced}/{expectedEntities} entities.");
}
else
{
Puts("All entities placed successfully.");
}
}
Detalhes da Refatoração:
Encapsulamento: Utilizamos funções auxiliares (GetProceduralDungeon, GetAvailableSpawnLocations, InitializeEntity) para simplificar a lógica e melhorar a legibilidade.
Uso de Linq: Para calcular o número total de entidades necessárias e para iterar nas spawn entries.
Loop de Distribuição: O loop de distribuição de entidades foi simplificado para um único ponto onde as entidades são criadas e inicializadas, reduzindo a repetição de código e mantendo a lógica encapsulada dentro da função InitializeEntity.
Validação e Reutilização: É feita uma verificação clara dos locais de spawn disponíveis e uma reutilização eficiente de locais, que também é encapsulada.
Este ajuste visa a eficiência do código, sua legibilidade e manutenção, facilitando futuras alterações e entendimentos do fluxo de spawn e configuração de dungeons.
#region Chat Commands
[ChatCommand("dun")]
private void CreateDungeonCommand(BasePlayer player, string command, string[] args)
{
if (!IsAdmin(player))
return;
var result = AutoSpawnDungeon();
NotifyPlayer(player, result);
}
[ChatCommand("removeinactivedungeons")]
private void RemoveInactiveDungeonCommand(BasePlayer player, string command, string[] args)
{
if (!IsAdmin(player))
return;
var removedDungeons = RemoveInactiveDungeons();
NotifyPlayerOnRemoval(player, removedDungeons, "NoInactiveDungeonsToRemove");
}
[ChatCommand("removedungeons")]
private void RemoveDungeonCommand(BasePlayer player, string command, string[] args)
{
if (!IsAdmin(player))
return;
var removedDungeons = RemoveAllDungeons();
NotifyPlayerOnRemoval(player, removedDungeons, "NoDungeonsToRemove");
}
#endregion
#region Initialization
private void OnServerInitialized()
{
_instance = this;
CleanupExistingEntities();
InitializeTierNameMap();
LoadData();
if (_configData.DungeonSpawn.EnableAutoSpawn)
{
StartAutoSpawnTimer();
}
StartDungeonCheckTimer();
StartUpdateMarkersTimer();
}
private void CleanupExistingEntities()
{
foreach (
var entityType in new Type[]
{
typeof(VendingMachineMapMarker),
typeof(MapMarkerGenericRadius),
typeof(BasePortal),
typeof(ProceduralDynamicDungeon),
typeof(AutoTurret),
typeof(NPCPlayer),
typeof(StorageContainer),
}
)
{
foreach (var obj in UnityEngine.Object.FindObjectsOfType(entityType))
{
if (obj is BaseEntity baseEntity && baseEntity.OwnerID == OwnerID)
{
baseEntity.Kill(BaseNetworkable.DestroyMode.None);
}
}
}
}
private void InitializeTierNameMap()
{
_tierNameMap = new Dictionary<DungeonTierConfig, string>
{
{ _configData.Tiers.Easy, "Easy" },
{ _configData.Tiers.Normal, "Normal" },
{ _configData.Tiers.Medium, "Medium" },
{ _configData.Tiers.Hard, "Hard" },
{ _configData.Tiers.Nightmare, "Nightmare" },
};
}
private void LoadData()
{
_dataFile = Interface.Oxide.DataFileSystem.GetFile("Dungeons_Data");
_dungeonData = _dataFile.ReadObject<DungeonData>() ?? new DungeonData();
}
private void SaveData()
{
_dataFile.WriteObject(_dungeonData);
}
#endregion
#region Hooks
private object CanEntityTakeDamage(BaseCombatEntity victim, HitInfo hitInfo)
{
if (victim == null || hitInfo == null)
return null;
if (hitInfo.Initiator == null)
return null;
if (IsValidSteamPlayer(hitInfo.Initiator as BasePlayer) && IsDungeonEntity(victim))
return true;
if (IsDungeonEntity(hitInfo.Initiator) && IsDungeonEntity(victim))
return true;
return null;
}
private object OnEntityTakeDamage(BaseCombatEntity victim, HitInfo hitInfo)
{
if (victim == null || hitInfo == null)
return null;
if (hitInfo.Initiator == null)
return null;
if (IsValidSteamPlayer(hitInfo.Initiator as BasePlayer) && IsDungeonEntity(victim))
return null;
if (IsDungeonEntity(hitInfo.Initiator) && IsDungeonEntity(victim))
return true;
return null;
}
private void OnServerSave() => SaveData();
#endregion
#region Dungeon Management
private void StartUpdateMarkersTimer()
{
_updateMarkersTimer = timer.Every(30f, UpdateMarkers);
}
private void UpdateMarkers()
{
var mapMarkers = UnityEngine.Object.FindObjectsOfType<MapMarkerGenericRadius>();
foreach (var marker in mapMarkers.Where(marker => marker.IsValid()))
{
marker.SendUpdate();
}
}
private void StartAutoSpawnTimer()
{
_autoSpawnTimer = timer.Every(
_configData.DungeonSpawn.AutoSpawnCycleInterval,
() =>
{
var result = AutoSpawnDungeon();
LogDungeonSpawnResult(result);
}
);
}
private void StartDungeonCheckTimer()
{
_dungeonCheckTimer = timer.Every(
60f,
() =>
{
var removedDungeons = RemoveInactiveDungeons();
if (removedDungeons.Count > 0)
{
foreach (var dungeon in removedDungeons)
{
LogDungeonRemoval(dungeon);
}
}
}
);
}
private enum DungeonSpawnResult
{
Success,
MaxActiveDungeonsReached,
NoValidSpawnLocation,
}
private DungeonSpawnResult AutoSpawnDungeon()
{
if (_dungeonData.ActiveDungeons.Count >= _configData.DungeonSpawn.MaxTotalActiveDungeons)
{
return DungeonSpawnResult.MaxActiveDungeonsReached;
}
if (TryFindDungeonSpawnPoint(out var position, out var rotation))
{
var selectedTier = CreateDungeon(position, rotation);
if (selectedTier != null)
{
var tierName = _tierNameMap[selectedTier];
CreateDungeonMarkers(position, tierName);
NotifyPlayersOfNewDungeon(position, tierName);
return DungeonSpawnResult.Success;
}
}
return DungeonSpawnResult.NoValidSpawnLocation;
}
private DungeonTierConfig CreateDungeon(Vector3 position, Quaternion rotation)
{
string prefabPath = GetRandomDungeonPrefab();
if (string.IsNullOrEmpty(prefabPath))
return null;
if (!(GameManager.server.CreateEntity(prefabPath, position, rotation) is BasePortal dungeonPortal))
return null;
dungeonPortal.OwnerID = OwnerID;
dungeonPortal.Spawn();
var proceduralDungeon = GetProceduralDungeon(dungeonPortal);
if (proceduralDungeon == null || proceduralDungeon.spawnedCells.Count < 1)
{
dungeonPortal.Kill(BaseNetworkable.DestroyMode.None);
return null;
}
var selectedTier = GetDungeonTier(proceduralDungeon.spawnedCells.Count);
var activeDungeon = new ActiveDungeon
{
PortalId = dungeonPortal.net.ID.Value,
DynamicDungeonId = proceduralDungeon.net.ID.Value,
Position = position,
TierName = _tierNameMap[selectedTier],
EntityIds = new List<ulong>(),
};
NextTick(() => PopulateDungeon(proceduralDungeon, tierConfig, activeDungeon));
_dungeonData.ActiveDungeons.Add(activeDungeon);
return selectedTier;
}
private ProceduralDynamicDungeon GetProceduralDungeon(BasePortal dungeonPortal)
{
return (dungeonPortal as XmasDungeon)?.dungeonInstance.Get(true) ?? (dungeonPortal as HalloweenDungeon)?.dungeonInstance.Get(true);
}
private void PopulateDungeon(ProceduralDynamicDungeon proceduralDungeon, DungeonTierConfig tierConfig, ActiveDungeon activeDungeon)
{
var spawnLocations = GetAvailableSpawnLocations(proceduralDungeon);
if (spawnLocations.Count == 0)
{
Puts("No valid spawn locations available.");
return;
}
var allSpawnEntries = GenerateSpawnEntries(tierConfig);
int spawnIndex = 0;
foreach (var entry in allSpawnEntries)
{
var (position, rotation) = spawnLocations[spawnIndex];
var entity = GameManager.server.CreateEntity(entry.prefab.resourcePath, position, rotation);
if (entity != null)
{
InitializeEntity(entity, tierConfig, activeDungeon);
}
spawnIndex = (spawnIndex + 1) % spawnLocations.Count; // Circulate existing spawn locations
}
Puts($"Placed {activeDungeon.EntityIds.Count}/{allSpawnEntries.Count} entities.");
}
private List<(Vector3 Position, Quaternion Rotation)> GetAvailableSpawnLocations(ProceduralDynamicDungeon proceduralDungeon)
{
var locations = new List<(Vector3, Quaternion)>();
foreach (var cell in proceduralDungeon.spawnedCells)
{
foreach (var group in cell.spawnGroups)
{
foreach (var spawnPoint in group.spawnPoints)
{
if (spawnPoint != null && spawnPoint.IsAvailableTo(null))
{
spawnPoint.GetLocation(out var position, out var rotation);
locations.Add((position, rotation));
}
}
}
}
return locations;
}
private void InitializeEntity(BaseEntity entity, DungeonTierConfig tierConfig, ActiveDungeon activeDungeon)
{
entity.OwnerID = OwnerID;
entity.Spawn();
if (entity is NPCPlayer)
{
activeDungeon.NpcIds.Add(entity.net.ID.Value);
}
else if (entity is AutoTurret turret)
{
ConfigureTurret(turret, tierConfig.AutoTurretConfig);
turret.gameObject.AddComponent<TurretMonoBehaviour>();
}
else if (entity is StorageContainer storage)
{
ConfigureStorage(storage);
}
activeDungeon.EntityIds.Add(entity.net.ID.Value);
}
private int CalculateTotalEntities(DungeonTierConfig tierConfig)
{
return tierConfig.TotalLootBoxes + tierConfig.NpcSpawnConfigs.Sum(npcConfig => npcConfig.Total) + tierConfig.AutoTurretConfig.Total;
}
private void ValidateAndAttemptRetries(ProceduralDynamicDungeon proceduralDungeon, DungeonTierConfig tierConfig, ActiveDungeon activeDungeon, List<SpawnGroup.SpawnEntry> remainingEntries)
{
int retries = 3;
while (remainingEntries.Count > 0 && retries > 0)
{
Puts($"Retrying placement for {remainingEntries.Count} missing entities.");
PopulateDungeon(proceduralDungeon, tierConfig, activeDungeon);
retries--;
}
int entitiesPlaced = activeDungeon.EntityIds.Count;
int expectedEntities = CalculateTotalEntities(tierConfig);
if (entitiesPlaced < expectedEntities)
{
Puts($"Warning: Only placed {entitiesPlaced}/{expectedEntities} entities.");
}
else
{
Puts("All entities placed successfully.");
}
}
private object OnPortalUse(BasePlayer player, BasePortal portal)
{
if (_dungeonData.ActiveDungeons.All(d => d.PortalId != portal.net.ID.Value))
{
return null;
}
var proceduralDungeon = (portal as XmasDungeon)?.dungeonInstance.Get(true) ?? (portal as HalloweenDungeon)?.dungeonInstance.Get(true);
if (proceduralDungeon == null)
{
return null;
}
foreach (var cell in proceduralDungeon.spawnedCells)
{
foreach (var group in cell.spawnGroups)
{
for (int num = group.spawnInstances.Count - 1; num >= 0; num--)
{
SpawnPointInstance spawnPointInstance = group.spawnInstances[num];
BaseEntity entity = spawnPointInstance?.gameObject?.ToBaseEntity();
if (entity != null && !IsDungeonEntity(entity) && (entity is NPCDwelling || entity is ScarecrowNPC || entity is LootContainer))
{
entity.Kill();
}
}
}
}
return null;
}
private List<SpawnGroup.SpawnEntry> GenerateSpawnEntries(DungeonTierConfig tierConfig)
{
var allSpawnEntries = new List<SpawnGroup.SpawnEntry>();
AddNpcEntries(tierConfig, allSpawnEntries);
AddTurretEntries(tierConfig, allSpawnEntries);
AddLootEntries(tierConfig, allSpawnEntries);
return allSpawnEntries.OrderBy(_ => Random.value).ToList();
}
private void AddNpcEntries(DungeonTierConfig tierConfig, List<SpawnGroup.SpawnEntry> spawnEntries)
{
foreach (var config in tierConfig.NpcSpawnConfigs)
{
if (GameManifest.pathToGuid.TryGetValue(config.PrefabName, out var guid))
{
spawnEntries.AddRange(
Enumerable.Repeat(
new SpawnGroup.SpawnEntry
{
prefab = new GameObjectRef { guid = guid },
weight = 1,
mobile = true,
},
config.Total
)
);
}
}
}
private void AddTurretEntries(DungeonTierConfig tierConfig, List<SpawnGroup.SpawnEntry> spawnEntries)
{
var prefabName = "assets/prefabs/npc/autoturret/autoturret_deployed.prefab";
if (GameManifest.pathToGuid.TryGetValue(prefabName, out var turretGuid))
{
spawnEntries.AddRange(
Enumerable.Repeat(
new SpawnGroup.SpawnEntry
{
prefab = new GameObjectRef { guid = turretGuid },
weight = 1,
mobile = false,
},
tierConfig.AutoTurretConfig.Total
)
);
}
}
private void AddLootEntries(DungeonTierConfig tierConfig, List<SpawnGroup.SpawnEntry> spawnEntries)
{
var prefabName = "assets/prefabs/deployable/woodenbox/woodbox_deployed.prefab";
if (GameManifest.pathToGuid.TryGetValue(prefabName, out var boxGuid))
{
spawnEntries.AddRange(
Enumerable.Repeat(
new SpawnGroup.SpawnEntry
{
prefab = new GameObjectRef { guid = boxGuid },
weight = 1,
mobile = false,
},
tierConfig.TotalLootBoxes
)
);
}
}
private void ConfigureTurret(AutoTurret turret, TurretConfig turretConfig)
{
turret.health = turretConfig.Health;
turret.SetMaxHealth(turret.health);
var weapon = ItemManager.CreateByName(turretConfig.WeaponShortName);
weapon?.MoveToContainer(turret.inventory, 0);
var laserSight = ItemManager.CreateByName("weapon.mod.lasersight");
laserSight?.MoveToContainer(weapon.contents);
turret.UpdateAttachedWeapon();
}
private void ConfigureStorage(StorageContainer storage)
{
storage.skinID = _configData.LootBoxConfig.SmallWoodBoxSkinID;
AddCodeLock(storage);
PopulateStorage(storage.inventory, _configData.LootBoxConfig.LootItems);
}
private void AddCodeLock(StorageContainer storage)
{
var prefabName = "assets/prefabs/locks/keypad/lock.code.prefab";
var codeLock = GameManager.server.CreateEntity(prefabName) as CodeLock;
if (codeLock == null)
return;
codeLock.SetParent(storage, storage.GetSlotAnchorName(BaseEntity.Slot.Lock));
codeLock.Spawn();
codeLock.code = Random.Range(1000, 9999).ToString();
codeLock.hasCode = true;
codeLock.SetFlag(BaseEntity.Flags.Locked, true);
}
private void PopulateStorage(ItemContainer container, List<ItemConfig> lootItems)
{
var shuffledItems = lootItems.OrderBy(_ => Random.value).ToList();
var differentItemsCount = 0;
foreach (var itemConfig in shuffledItems)
{
if (differentItemsCount >= _configData.LootBoxConfig.MaxDifferentItemsPerBox)
{
break;
}
if (Random.value * 100f <= itemConfig.InclusionChancePercentage)
{
var itemDefinition = ItemManager.FindItemDefinition(itemConfig.ShortName);
if (itemDefinition != null)
{
var amount = Random.Range(itemConfig.MinimumAmount, itemConfig.MaximumAmount + 1);
if (container.itemList.Count < container.capacity)
{
var item = ItemManager.Create(itemDefinition, amount);
item.MoveToContainer(container);
differentItemsCount++;
}
}
}
}
}
private List<ActiveDungeon> RemoveInactiveDungeons()
{
var removedDungeons = new List<ActiveDungeon>();
foreach (var dungeon in _dungeonData.ActiveDungeons.ToList())
{
if (CanBeRemoved(dungeon))
{
removedDungeons.Add(dungeon);
DestroyDungeon(dungeon);
}
}
return removedDungeons;
}
private void DestroyDungeon(ActiveDungeon dungeon)
{
DestroyEntities(dungeon);
RemoveDungeonMarkers(dungeon);
_dungeonData.ActiveDungeons.Remove(dungeon);
var portal = BaseNetworkable.serverEntities.Find(new NetworkableId(dungeon.PortalId)) as BasePortal;
portal?.Kill(BaseNetworkable.DestroyMode.None);
SaveData();
}
private void DestroyEntities(ActiveDungeon dungeon)
{
foreach (var entityId in dungeon.EntityIds)
{
var entity = BaseNetworkable.serverEntities.Find(new NetworkableId(entityId)) as BaseEntity;
entity?.Kill(BaseNetworkable.DestroyMode.None);
}
dungeon.EntityIds.Clear();
}
private void RemoveDungeonMarkers(ActiveDungeon dungeon)
{
foreach (var marker in _dungeonData.ActiveDungeonMarkers.Where(m => m.Position == dungeon.Position).ToList())
{
var vendingMarker = BaseNetworkable.serverEntities.Find(new NetworkableId(marker.VendingMarker)) as BaseEntity;
var radiusMarker = BaseNetworkable.serverEntities.Find(new NetworkableId(marker.RadiusMarker)) as BaseEntity;
vendingMarker?.Kill(BaseNetworkable.DestroyMode.None);
radiusMarker?.Kill(BaseNetworkable.DestroyMode.None);
_dungeonData.ActiveDungeonMarkers.Remove(marker);
}
}
private IEnumerable<ActiveDungeon> RemoveAllDungeons()
{
var removedDungeons = new List<ActiveDungeon>(_dungeonData.ActiveDungeons);
foreach (var dungeon in removedDungeons)
{
DestroyDungeon(dungeon);
}
return removedDungeons;
}
#endregion
#region Utility Methods
private static bool IsValidSteamPlayer(BasePlayer targetPlayer) => targetPlayer != null && targetPlayer.userID.IsSteamId();
private void NotifyPlayersOfNewDungeon(Vector3 position, string tierName)
{
var gridPosition = PhoneController.PositionToGridCoord(position);
foreach (var player in BasePlayer.activePlayerList)
{
var message = Msg(player.UserIDString, "DungeonAppeared", tierName, gridPosition);
player.ChatMessage(message);
player.ShowToast(GameTip.Styles.Blue_Normal, message);
}
}
private void NotifyPlayer(BasePlayer player, DungeonSpawnResult result)
{
var messageKey = result switch
{
DungeonSpawnResult.Success => "DungeonSpawnedSuccess",
DungeonSpawnResult.MaxActiveDungeonsReached => "MaxActiveDungeonsReached",
DungeonSpawnResult.NoValidSpawnLocation => "DungeonSpawnFailed",
_ => null,
};
player.ChatMessage(Msg(player.UserIDString, messageKey));
}
private void NotifyPlayerOnRemoval(BasePlayer player, IEnumerable<ActiveDungeon> removedDungeons, string noDungeonMessage)
{
if (!removedDungeons.Any())
{
player.ChatMessage(Msg(player.UserIDString, noDungeonMessage));
return;
}
foreach (var dungeon in removedDungeons)
{
var gridPosition = PhoneController.PositionToGridCoord(dungeon.Position);
player.ChatMessage(Msg(player.UserIDString, "DungeonRemovedDetail", dungeon.TierName, gridPosition));
}
}
private void LogDungeonSpawnResult(DungeonSpawnResult result)
{
var logMessage = result switch
{
DungeonSpawnResult.Success => "Dungeon spawned successfully.",
DungeonSpawnResult.MaxActiveDungeonsReached => "Cannot spawn: Maximum number of active dungeons reached.",
DungeonSpawnResult.NoValidSpawnLocation => "Could not find a valid location to spawn a new dungeon.",
_ => null,
};
if (logMessage != null)
Puts(logMessage);
}
private void LogDungeonRemoval(ActiveDungeon dungeon)
{
var gridPosition = PhoneController.PositionToGridCoord(dungeon.Position);
Puts($"Removed inactive dungeon of tier {dungeon.TierName} at grid {gridPosition}.");
}
#endregion
#region Helper and Utility Methods
private bool IsAdmin(BasePlayer player) => player != null && player.IsAdmin;
private bool IsDungeonEntity(BaseEntity entity) => entity != null && _dungeonData.ActiveDungeons.Any(d => d.EntityIds.Contains(entity.net.ID.Value));
private string GetRandomDungeonPrefab()
{
var prefabs = new List<string>();
if (_configData.DungeonSpawn.EnableXmasDungeon)
prefabs.Add("assets/prefabs/missions/portal/xmasportalentry.prefab");
if (_configData.DungeonSpawn.EnableHalloweenDungeon)
prefabs.Add("assets/prefabs/missions/portal/halloweenportalentry.prefab");
return prefabs.Count > 0 ? prefabs[Random.Range(0, prefabs.Count)] : null;
}
private static bool IsValidPosition(Vector3 position, out Vector3 suitablePosition, out Quaternion suitableRotation)
{
suitablePosition = Vector3.zero;
suitableRotation = Quaternion.identity;
if (!CollidersChecks.GetTerrainInfo(position, out var hitInfo))
return false;
if (CollidersChecks.InsideRock(position, 10f))
return false;
if (CollidersChecks.InWater(position))
return false;
if (CollidersChecks.OnRoadOrRail(position))
return false;
if (CollidersChecks.InNoBuildZone(position, 10f))
return false;
return SetSuitablePositionAndRotation(position, hitInfo, out suitablePosition, out suitableRotation);
}
private static bool SetSuitablePositionAndRotation(Vector3 position, RaycastHit hitInfo, out Vector3 suitablePosition, out Quaternion suitableRotation)
{
suitablePosition = hitInfo.point;
suitableRotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
return true;
}
private void CreateDungeonMarkers(Vector3 position, string tierName)
{
var prefabVendingMarker = "assets/prefabs/deployable/vendingmachine/vending_mapmarker.prefab";
var vendingMarker = CreateMapMarker<VendingMachineMapMarker>(prefabVendingMarker, position);
vendingMarker.OwnerID = OwnerID;
vendingMarker.markerShopName = $"Dungeon: {tierName}";
vendingMarker.SendNetworkUpdate();
var prefabRadiusMarker = "assets/prefabs/tools/map/genericradiusmarker.prefab";
var radiusMarker = CreateMapMarker<MapMarkerGenericRadius>(prefabRadiusMarker, position);
radiusMarker.OwnerID = OwnerID;
radiusMarker.alpha = 0.75f;
radiusMarker.radius = 0.5f;
radiusMarker.color2 = GetMarkerColorByTier(tierName);
radiusMarker.SendUpdate();
radiusMarker.SendNetworkUpdate();
_dungeonData.ActiveDungeonMarkers.Add(
new DungeonMarker
{
VendingMarker = vendingMarker.net.ID.Value,
RadiusMarker = radiusMarker.net.ID.Value,
Position = position,
Tier = tierName,
}
);
}
private T CreateMapMarker<T>(string prefab, Vector3 position)
where T : BaseEntity
{
var entity = GameManager.server.CreateEntity(prefab, position) as T;
entity?.Spawn();
return entity;
}
private static Color GetMarkerColorByTier(string tierName)
{
return tierName switch
{
"Easy" => Color.green,
"Normal" or "Medium" => Color.yellow,
"Hard" => Color.red,
"Nightmare" => Color.black,
_ => Color.white,
};
}
private bool CanBeRemoved(ActiveDungeon dungeon)
{
return AreAllNpcsGone(dungeon) && NoPlayersInside(dungeon);
}
private bool AreAllNpcsGone(ActiveDungeon dungeon)
{
return dungeon.NpcIds.All(npcId =>
{
var npc = BaseNetworkable.serverEntities.Find(new NetworkableId(npcId)) as BaseEntity;
return npc == null || npc.IsDestroyed;
});
}
private bool NoPlayersInside(ActiveDungeon dungeon)
{
var dynamicDungeon = BaseNetworkable.serverEntities.Find(new NetworkableId(dungeon.DynamicDungeonId)) as ProceduralDynamicDungeon;
return dynamicDungeon == null || !dynamicDungeon.ContainsAnyPlayers();
}
private bool TryFindDungeonSpawnPoint(out Vector3 position, out Quaternion rotation)
{
var mapSize = World.Size;
for (var attempt = 0; attempt < 1000; attempt++)
{
var candidatePosition = new Vector3(Random.Range(-mapSize / 2, mapSize / 2), 0, Random.Range(-mapSize / 2, mapSize / 2));
if (IsValidPosition(candidatePosition, out position, out rotation) && IsSafeFromOtherDungeons(position) && !IsCloseToMonuments(position))
{
return true;
}
}
position = Vector3.zero;
rotation = Quaternion.identity;
return false;
}
private bool IsSafeFromOtherDungeons(Vector3 position)
{
return _dungeonData.ActiveDungeons.All(dungeon => Vector3.Distance(position, dungeon.Position) >= _configData.DungeonSpawn.MinDistanceBetweenDungeons);
}
private bool IsCloseToMonuments(Vector3 position)
{
return TerrainMeta.Path.Monuments.Any(mon => Vector3.Distance(position, mon.transform.position) < _configData.DungeonSpawn.MinDistanceFromMonuments);
}
private DungeonTierConfig GetDungeonTier(int cellCount)
{
return cellCount switch
{
< 5 => _configData.Tiers.Easy,
< 8 => _configData.Tiers.Normal,
< 12 => _configData.Tiers.Medium,
< 16 => _configData.Tiers.Hard,
_ => _configData.Tiers.Nightmare,
};
}
#endregion
#region Cleanup
private void Unload()
{
RemoveAllDungeons();
_autoSpawnTimer?.Destroy();
_dungeonCheckTimer?.Destroy();
_updateMarkersTimer?.Destroy();
}
#endregion
#region Configuration
private class ConfigData
{
[JsonProperty(PropertyName = "Dungeon Spawn Settings")]
public DungeonSpawnSettings DungeonSpawn { get; set; } = new DungeonSpawnSettings();
[JsonProperty(PropertyName = "Tiers")]
public DungeonTiers Tiers { get; set; } = new DungeonTiers();
[JsonProperty(PropertyName = "Loot Box Config")]
public LootBoxConfig LootBoxConfig { get; set; } = new LootBoxConfig();
[JsonProperty(PropertyName = "Version")]
public VersionNumber Version { get; set; }
}
private class DungeonTiers
{
[JsonProperty(PropertyName = "Easy")]
public DungeonTierConfig Easy { get; set; } =
new DungeonTierConfig
{
TotalLootBoxes = 1,
NpcSpawnConfigs = new List<NpcSpawnConfig>
{
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_full_pistol.prefab", Total = 1 },
},
AutoTurretConfig = new TurretConfig
{
Total = 1,
Health = 300,
WeaponShortName = "pistol.revolver",
},
};
[JsonProperty(PropertyName = "Normal")]
public DungeonTierConfig Normal { get; set; } =
new DungeonTierConfig
{
TotalLootBoxes = 2,
NpcSpawnConfigs = new List<NpcSpawnConfig>
{
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_full_pistol.prefab", Total = 3 },
},
AutoTurretConfig = new TurretConfig
{
Total = 2,
Health = 600,
WeaponShortName = "smg.2",
},
};
[JsonProperty(PropertyName = "Medium")]
public DungeonTierConfig Medium { get; set; } =
new DungeonTierConfig
{
TotalLootBoxes = 4,
NpcSpawnConfigs = new List<NpcSpawnConfig>
{
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_full_pistol.prefab", Total = 4 },
},
AutoTurretConfig = new TurretConfig
{
Total = 3,
Health = 1000,
WeaponShortName = "smg.mp5",
},
};
[JsonProperty(PropertyName = "Hard")]
public DungeonTierConfig Hard { get; set; } =
new DungeonTierConfig
{
TotalLootBoxes = 5,
NpcSpawnConfigs = new List<NpcSpawnConfig>
{
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_full_pistol.prefab", Total = 4 },
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_full_pistol.prefab", Total = 3 },
},
AutoTurretConfig = new TurretConfig
{
Total = 4,
Health = 1500,
WeaponShortName = "rifle.ak",
},
};
[JsonProperty(PropertyName = "Nightmare")]
public DungeonTierConfig Nightmare { get; set; } =
new DungeonTierConfig
{
TotalLootBoxes = 6,
NpcSpawnConfigs = new List<NpcSpawnConfig>
{
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_full_pistol.prefab", Total = 5 },
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_full_pistol.prefab", Total = 5 },
},
AutoTurretConfig = new TurretConfig
{
Total = 6,
Health = 2000,
WeaponShortName = "rifle.ak",
},
};
}
private class DungeonTierConfig
{
[JsonProperty(PropertyName = "Maximum Loot Boxes")]
public int TotalLootBoxes { get; set; }
[JsonProperty(PropertyName = "NPC Spawn Configs", ObjectCreationHandling = ObjectCreationHandling.Replace)]
public List<NpcSpawnConfig> NpcSpawnConfigs { get; set; }
[JsonProperty(PropertyName = "Auto Turret Config")]
public TurretConfig AutoTurretConfig { get; set; } = new TurretConfig();
}
public class LootBoxConfig
{
[JsonProperty(PropertyName = "Small Wood Box Skin ID")]
public ulong SmallWoodBoxSkinID { get; set; } = 2998755525;
[JsonProperty(PropertyName = "Max Different Items Per Box")]
public int MaxDifferentItemsPerBox { get; set; } = 6;
[JsonProperty(PropertyName = "Loot Items", ObjectCreationHandling = ObjectCreationHandling.Replace)]
public List<ItemConfig> LootItems { get; set; } =
new List<ItemConfig>
{
new ItemConfig
{
ShortName = "ammo.rifle",
InclusionChancePercentage = 15,
MinimumAmount = 100,
MaximumAmount = 300,
},
};
}
private class NpcSpawnConfig
{
[JsonProperty(PropertyName = "Prefab")]
public string PrefabName { get; set; }
[JsonProperty(PropertyName = "Maximum")]
public int Total { get; set; }
}
private class TurretConfig
{
[JsonProperty(PropertyName = "Maximum")]
public int Total { get; set; } = 3;
[JsonProperty(PropertyName = "Health")]
public float Health { get; set; } = 1000f;
[JsonProperty(PropertyName = "Weapon Short Name")]
public string WeaponShortName { get; set; } = "rifle.ak";
}
private class DungeonSpawnSettings
{
[JsonProperty(PropertyName = "Enable Xmas Dungeon")]
public bool EnableXmasDungeon { get; set; } = true;
[JsonProperty(PropertyName = "Enable Halloween Dungeon")]
public bool EnableHalloweenDungeon { get; set; } = true;
[JsonProperty(PropertyName = "Max Total Active Dungeons")]
public int MaxTotalActiveDungeons { get; set; } = 4;
[JsonProperty(PropertyName = "Min Distance From Monuments")]
public float MinDistanceFromMonuments { get; set; } = 100f;
[JsonProperty(PropertyName = "Min Distance Between Dungeons")]
public float MinDistanceBetweenDungeons { get; set; } = 50f;
[JsonProperty(PropertyName = "Auto Spawn Cycle Interval")]
public float AutoSpawnCycleInterval { get; set; } = 60f;
[JsonProperty(PropertyName = "Enable Auto Spawn")]
public bool EnableAutoSpawn { get; set; } = true;
}
public class ItemConfig
{
[JsonProperty(PropertyName = "ShortName")]
public string ShortName { get; set; }
[JsonProperty(PropertyName = "Inclusion Chance Percentage")]
public float InclusionChancePercentage { get; set; }
[JsonProperty(PropertyName = "Minimum Amount")]
public int MinimumAmount { get; set; }
[JsonProperty(PropertyName = "Maximum Amount")]
public int MaximumAmount { get; set; }
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
_configData = Config.ReadObject<ConfigData>() ?? new ConfigData();
}
catch (Exception ex)
{
PrintError($"The configuration file is corrupt: {ex}");
LoadDefaultConfig();
}
SaveConfig();
}
protected override void LoadDefaultConfig()
{
PrintWarning("Creating a new configuration file.");
_configData = new ConfigData { Version = Version };
}
protected override void SaveConfig()
{
Config.WriteObject(_configData, true);
}
#endregion
#region Localization
protected override void LoadDefaultMessages()
{
lang.RegisterMessages(
new Dictionary<string, string>
{
["NoInactiveDungeonsToRemove"] = "There are no inactive dungeons to remove.",
["NoDungeonsToRemove"] = "There are no dungeons to remove.",
["DungeonRemovedDetail"] = "Removed dungeon of tier {0} at grid {1}.",
["AttemptSpawnDungeons"] = "Attempting to spawn dungeons.",
["DungeonAppeared"] = "A {0} Dungeon has appeared at grid {1}!",
["DungeonSpawnFailed"] = "Could not find a valid location to spawn a new dungeon.",
["DungeonSpawnedSuccess"] = "Dungeon spawned successfully.",
["MaxActiveDungeonsReached"] = "Cannot spawn: Maximum number of active dungeons reached.",
},
this
);
}
private string Msg(string userId, string key, params object[] args)
{
return string.Format(lang.GetMessage(key, this, userId), args);
}
#endregion
#region Data Classes
private class DungeonData
{
public List<ActiveDungeon> ActiveDungeons { get; set; } = new List<ActiveDungeon>();
public List<DungeonMarker> ActiveDungeonMarkers { get; set; } = new List<DungeonMarker>();
}
private class ActiveDungeon
{
public ulong PortalId { get; set; }
public ulong DynamicDungeonId { get; set; }
public Vector3 Position { get; set; }
public string TierName { get; set; }
public List<ulong> EntityIds { get; set; } = new List<ulong>();
public List<ulong> NpcIds { get; set; } = new List<ulong>();
}
private class DungeonMarker
{
public ulong VendingMarker { get; set; }
public ulong RadiusMarker { get; set; }
public Vector3 Position { get; set; }
public string Tier { get; set; }
}
#endregion
#region Collider Checks
public static class CollidersChecks
{
public static bool InsideRock(Vector3 position, float radius)
{
List<Collider> colliders = Pool.GetList<Collider>();
Vis.Colliders(position, radius, colliders, Layers.Mask.World, QueryTriggerInteraction.Ignore);
bool isInsideRock = colliders.Any(collider =>
collider.name.Contains("rock", StringComparison.OrdinalIgnoreCase)
|| collider.name.Contains("cliff", StringComparison.OrdinalIgnoreCase)
|| collider.name.Contains("formation", StringComparison.OrdinalIgnoreCase)
);
Pool.FreeList(ref colliders);
return isInsideRock;
}
public static bool GetTerrainInfo(Vector3 position, out RaycastHit hitInfo)
{
return Physics.Linecast(position + Vector3.up, position - Vector3.up, out hitInfo, Layers.Mask.Terrain);
}
public static bool InWater(Vector3 position) => WaterLevel.Test(position, false, false);
public static bool OnRoadOrRail(Vector3 position) => (TerrainMeta.TopologyMap.GetTopology(position) & (int)TerrainTopology.Enum.Road) != 0;
public static bool InNoBuildZone(Vector3 position, float radius) => Physics.CheckSphere(position, radius, Layers.Mask.Prevent_Building, QueryTriggerInteraction.Ignore);
}
#endregion
#region Turret MonoBehaviour
public class TurretMonoBehaviour : MonoBehaviour
{
private AutoTurret _turret;
private const float SearchDistance = 15f;
private const int MaxReserveAmmo = 1000;
private void Awake()
{
InitializeTurret();
}
private void InitializeTurret()
{
_turret = GetComponent<AutoTurret>();
var triggerCollider = _turret.targetTrigger.GetComponent<SphereCollider>();
triggerCollider.enabled = false;
_turret.SetPeacekeepermode(false);
_turret.InitiateStartup();
_turret.SetIsOnline(true);
_turret.CancelInvoke(_turret.ServerTick);
_turret.SetTarget(null);
_turret.InvokeRepeating(PerformTurretCycle, 1f, 0.01f);
_turret.InvokeRepeating(ScanForTargets, 2f, 1f);
_turret.isLootable = false;
_turret.dropFloats = false;
_turret.dropsLoot = false;
_turret.SendNetworkUpdateImmediate();
InvokeRepeating(nameof(RefillAmmo), 2f, 30f);
}
private void RefillAmmo()
{
if (_turret.AttachedWeapon is not BaseProjectile baseProjectile || baseProjectile.primaryMagazine?.ammoType == null)
return;
var ammoType = baseProjectile.primaryMagazine.ammoType;
int currentAmmoCount = CalculateCurrentAmmoCount(ammoType);
if (currentAmmoCount < MaxReserveAmmo)
{
int ammoNeeded = MaxReserveAmmo - currentAmmoCount;
Item ammoItem = ItemManager.Create(ammoType, ammoNeeded);
ammoItem?.MoveToContainer(_turret.inventory);
_turret.UpdateTotalAmmo();
_turret.EnsureReloaded();
_turret.SendNetworkUpdateImmediate();
}
}
private int CalculateCurrentAmmoCount(ItemDefinition ammoType)
{
return _turret.inventory.itemList.Where(item => item.info == ammoType).Sum(item => item.amount);
}
private void ScanForTargets()
{
var entityContents = _turret.targetTrigger.entityContents ??= new HashSet<BaseEntity>();
entityContents.Clear();
int foundTargets = BaseEntity.Query.Server.GetPlayersInSphereFast(transform.position, SearchDistance, AIBrainSenses.playerQueryResults, IsTargetValid);
if (foundTargets == 0)
return;
_turret.authDirty = true;
for (int i = 0; i < foundTargets; i++)
{
var player = AIBrainSenses.playerQueryResults[i];
if (Interface.CallHook("OnEntityEnter", _turret.targetTrigger, player) != null || player.IsSleeping() || (player.InSafeZone() && !player.IsHostile()))
continue;
entityContents.Add(player);
}
}
private bool IsTargetValid(BasePlayer player) => player != null && !player.IsNpc;
private void PerformTurretCycle()
{
if (_turret.isClient || _turret.IsDestroyed)
return;
float deltaTime = (float)_turret.timeSinceLastServerTick;
_turret.timeSinceLastServerTick = 0f;
if (_turret.IsOnline() && !_turret.IsBeingControlled)
{
if (!_turret.HasTarget())
{
_turret.IdleTick(deltaTime);
}
else
{
ExecuteTargetEngagement();
}
}
_turret.UpdateFacingToTarget(deltaTime);
UpdateAmmoStatus();
}
private void ExecuteTargetEngagement()
{
if (Time.realtimeSinceStartup >= _turret.nextVisCheck)
{
_turret.nextVisCheck = Time.realtimeSinceStartup + Random.Range(0.2f, 0.3f);
_turret.targetVisible = _turret.ObjectVisible(_turret.target);
if (_turret.targetVisible)
_turret.lastTargetSeenTime = Time.realtimeSinceStartup;
}
_turret.EnsureReloaded();
if (ShouldFireAtTarget())
{
var weapon = _turret.GetAttachedWeapon();
FireWeapon(weapon);
}
ValidateTargetEngagement();
}
private bool ShouldFireAtTarget()
{
return Time.time >= _turret.nextShotTime
&& _turret.targetVisible
&& Mathf.Abs(_turret.AngleToTarget(_turret.target, _turret.currentAmmoGravity != 0f)) < _turret.GetMaxAngleForEngagement();
}
private void FireWeapon(BaseProjectile weapon)
{
if (weapon == null)
{
_turret.nextShotTime = Time.time + 1f;
return;
}
if (weapon.primaryMagazine.contents > 0)
{
_turret.FireAttachedGun(_turret.AimOffset(_turret.target), _turret.aimCone, null, _turret.PeacekeeperMode() ? _turret.target : null);
float delay = weapon.isSemiAuto ? weapon.repeatDelay * 1.5f : weapon.repeatDelay;
delay = weapon.ScaleRepeatDelay(delay);
_turret.nextShotTime = Time.time + delay;
}
else
{
_turret.nextShotTime = Time.time + 5f;
}
}
private void ValidateTargetEngagement()
{
var targetPlayer = _turret.target as BasePlayer;
if (_turret.target != null && (!IsValidSteamPlayer(targetPlayer) || _turret.target.IsDead() || TimeExceedsLastSeen() || DistanceExceedsSightRange() || ShouldTargetBeReset()))
{
_turret.SetTarget(null);
}
}
private bool TimeExceedsLastSeen() => Time.realtimeSinceStartup - _turret.lastTargetSeenTime > 3f;
private bool DistanceExceedsSightRange() => Vector3.Distance(_turret.transform.position, _turret.target.transform.position) > _turret.sightRange;
private bool ShouldTargetBeReset() => _turret.PeacekeeperMode() && !_turret.IsEntityHostile(_turret.target);
private void UpdateAmmoStatus()
{
if (_turret.totalAmmoDirty && Time.time > _turret.nextAmmoCheckTime)
{
_turret.UpdateTotalAmmo();
_turret.totalAmmoDirty = false;
_turret.nextAmmoCheckTime = Time.time + 0.5f;
}
}
private void OnDestroy()
{
CancelInvoke(nameof(RefillAmmo));
}
}
#endregion
}
}
private DungeonSpawnResult AutoSpawnDungeon()
{
if (_dungeonData.ActiveDungeons.Count >= _configData.DungeonSpawn.MaxTotalActiveDungeons)
{
return DungeonSpawnResult.MaxActiveDungeonsReached;
}
if (TryFindDungeonSpawnPoint(out var position, out var rotation))
{
var selectedTier = CreateDungeon(position, rotation);
if (selectedTier != null)
{
var tierName = _tierNameMap[selectedTier];
CreateDungeonMarkers(position, tierName);
NotifyPlayersOfNewDungeon(position, tierName);
return DungeonSpawnResult.Success;
}
}
return DungeonSpawnResult.NoValidSpawnLocation;
}
private DungeonTierConfig CreateDungeon(Vector3 position, Quaternion rotation)
{
string prefabPath = GetRandomDungeonPrefab();
if (string.IsNullOrEmpty(prefabPath))
return null;
if (!(GameManager.server.CreateEntity(prefabPath, position, rotation) is BasePortal dungeonPortal))
return null;
dungeonPortal.OwnerID = OwnerID;
dungeonPortal.Spawn();
var proceduralDungeon = GetProceduralDungeon(dungeonPortal);
if (proceduralDungeon == null || proceduralDungeon.spawnedCells.Count < 1)
{
dungeonPortal.Kill(BaseNetworkable.DestroyMode.None);
return null;
}
var selectedTier = GetDungeonTier(proceduralDungeon.spawnedCells.Count);
var activeDungeon = new ActiveDungeon
{
PortalId = dungeonPortal.net.ID.Value,
DynamicDungeonId = proceduralDungeon.net.ID.Value,
Position = position,
TierName = _tierNameMap[selectedTier],
EntityIds = new List<ulong>(),
};
// Pass selectedTier to PopulateDungeon instead of tierConfig
NextTick(() => PopulateDungeon(proceduralDungeon, selectedTier, activeDungeon));
_dungeonData.ActiveDungeons.Add(activeDungeon);
return selectedTier;
}
private void PopulateDungeon(ProceduralDynamicDungeon proceduralDungeon, DungeonTierConfig tierConfig, ActiveDungeon activeDungeon)
{
var spawnLocations = GetAvailableSpawnLocations(proceduralDungeon);
if (spawnLocations.Count == 0)
{
Puts("No valid spawn locations available.");
return;
}
var allSpawnEntries = GenerateSpawnEntries(tierConfig);
int spawnIndex = 0;
foreach (var entry in allSpawnEntries)
{
var (position, rotation) = spawnLocations[spawnIndex];
var entity = GameManager.server.CreateEntity(entry.prefab.resourcePath, position, rotation);
if (entity != null)
{
InitializeEntity(entity, tierConfig, activeDungeon);
}
spawnIndex = (spawnIndex + 1) % spawnLocations.Count; // Circulate existing spawn locations
}
Puts($"Placed {activeDungeon.EntityIds.Count}/{allSpawnEntries.Count} entities.");
}
// Other utility methods remain unchanged...
Explanation of Changes:
Variable Replacement: The method signature for PopulateDungeon now uses tierConfig consistently by passing selectedTier directly from CreateDungeon.
Variable Consistency: Make sure that tierConfig represents the configuration specific to the tier of the dungeon currently being created.
Refactoring for Clarity: By clarifying which variable represents the dungeon tier throughout the functions, the code becomes more readable and less error-prone.
This should resolve the CS0103 error by ensuring that tierConfig is well-defined and present wherever it is used.
#region Chat Commands
[ChatCommand("dun")]
private void CreateDungeonCommand(BasePlayer player, string command, string[] args)
{
if (!IsAdmin(player))
return;
var result = AutoSpawnDungeon();
NotifyPlayer(player, result);
}
[ChatCommand("removeinactivedungeons")]
private void RemoveInactiveDungeonCommand(BasePlayer player, string command, string[] args)
{
if (!IsAdmin(player))
return;
var removedDungeons = RemoveInactiveDungeons();
NotifyPlayerOnRemoval(player, removedDungeons, "NoInactiveDungeonsToRemove");
}
[ChatCommand("removedungeons")]
private void RemoveDungeonCommand(BasePlayer player, string command, string[] args)
{
if (!IsAdmin(player))
return;
var removedDungeons = RemoveAllDungeons();
NotifyPlayerOnRemoval(player, removedDungeons, "NoDungeonsToRemove");
}
#endregion
#region Initialization
private void OnServerInitialized()
{
_instance = this;
CleanupExistingEntities();
InitializeTierNameMap();
LoadData();
if (_configData.DungeonSpawn.EnableAutoSpawn)
{
StartAutoSpawnTimer();
}
StartDungeonCheckTimer();
StartUpdateMarkersTimer();
}
private void CleanupExistingEntities()
{
foreach (
var entityType in new Type[]
{
typeof(VendingMachineMapMarker),
typeof(MapMarkerGenericRadius),
typeof(BasePortal),
typeof(ProceduralDynamicDungeon),
typeof(AutoTurret),
typeof(NPCPlayer),
typeof(StorageContainer),
}
)
{
foreach (var obj in UnityEngine.Object.FindObjectsOfType(entityType))
{
if (obj is BaseEntity baseEntity && baseEntity.OwnerID == OwnerID)
{
baseEntity.Kill(BaseNetworkable.DestroyMode.None);
}
}
}
}
private void InitializeTierNameMap()
{
_tierNameMap = new Dictionary<DungeonTierConfig, string>
{
{ _configData.Tiers.Easy, "Easy" },
{ _configData.Tiers.Normal, "Normal" },
{ _configData.Tiers.Medium, "Medium" },
{ _configData.Tiers.Hard, "Hard" },
{ _configData.Tiers.Nightmare, "Nightmare" },
};
}
private void LoadData()
{
_dataFile = Interface.Oxide.DataFileSystem.GetFile("Dungeons_Data");
_dungeonData = _dataFile.ReadObject<DungeonData>() ?? new DungeonData();
}
private void SaveData()
{
_dataFile.WriteObject(_dungeonData);
}
#endregion
#region Hooks
private object CanEntityTakeDamage(BaseCombatEntity victim, HitInfo hitInfo)
{
if (victim == null || hitInfo == null)
return null;
if (hitInfo.Initiator == null)
return null;
if (IsValidSteamPlayer(hitInfo.Initiator as BasePlayer) && IsDungeonEntity(victim))
return true;
if (IsDungeonEntity(hitInfo.Initiator) && IsDungeonEntity(victim))
return true;
return null;
}
private object OnEntityTakeDamage(BaseCombatEntity victim, HitInfo hitInfo)
{
if (victim == null || hitInfo == null)
return null;
if (hitInfo.Initiator == null)
return null;
if (IsValidSteamPlayer(hitInfo.Initiator as BasePlayer) && IsDungeonEntity(victim))
return null;
if (IsDungeonEntity(hitInfo.Initiator) && IsDungeonEntity(victim))
return true;
return null;
}
private void OnServerSave() => SaveData();
#endregion
#region Dungeon Management
private void StartUpdateMarkersTimer()
{
_updateMarkersTimer = timer.Every(30f, UpdateMarkers);
}
private void UpdateMarkers()
{
var mapMarkers = UnityEngine.Object.FindObjectsOfType<MapMarkerGenericRadius>();
foreach (var marker in mapMarkers.Where(marker => marker.IsValid()))
{
marker.SendUpdate();
}
}
private void StartAutoSpawnTimer()
{
_autoSpawnTimer = timer.Every(
_configData.DungeonSpawn.AutoSpawnCycleInterval,
() =>
{
var result = AutoSpawnDungeon();
LogDungeonSpawnResult(result);
}
);
}
private void StartDungeonCheckTimer()
{
_dungeonCheckTimer = timer.Every(
60f,
() =>
{
var removedDungeons = RemoveInactiveDungeons();
if (removedDungeons.Count > 0)
{
foreach (var dungeon in removedDungeons)
{
LogDungeonRemoval(dungeon);
}
}
}
);
}
private enum DungeonSpawnResult
{
Success,
MaxActiveDungeonsReached,
NoValidSpawnLocation,
}
private DungeonSpawnResult AutoSpawnDungeon()
{
if (_dungeonData.ActiveDungeons.Count >= _configData.DungeonSpawn.MaxTotalActiveDungeons)
{
return DungeonSpawnResult.MaxActiveDungeonsReached;
}
if (TryFindDungeonSpawnPoint(out var position, out var rotation))
{
var selectedTier = CreateDungeon(position, rotation);
if (selectedTier != null)
{
var tierName = _tierNameMap[selectedTier];
CreateDungeonMarkers(position, tierName);
NotifyPlayersOfNewDungeon(position, tierName);
return DungeonSpawnResult.Success;
}
}
return DungeonSpawnResult.NoValidSpawnLocation;
}
private DungeonTierConfig CreateDungeon(Vector3 position, Quaternion rotation)
{
string prefabPath = GetRandomDungeonPrefab();
if (string.IsNullOrEmpty(prefabPath))
return null;
if (!(GameManager.server.CreateEntity(prefabPath, position, rotation) is BasePortal dungeonPortal))
return null;
dungeonPortal.OwnerID = OwnerID;
dungeonPortal.Spawn();
var proceduralDungeon = GetProceduralDungeon(dungeonPortal);
if (proceduralDungeon == null || proceduralDungeon.spawnedCells.Count < 1)
{
dungeonPortal.Kill(BaseNetworkable.DestroyMode.None);
return null;
}
var selectedTier = GetDungeonTier(proceduralDungeon.spawnedCells.Count);
var activeDungeon = new ActiveDungeon
{
PortalId = dungeonPortal.net.ID.Value,
DynamicDungeonId = proceduralDungeon.net.ID.Value,
Position = position,
TierName = _tierNameMap[selectedTier],
EntityIds = new List<ulong>(),
};
// Pass selectedTier to PopulateDungeon instead of tierConfig
NextTick(() => PopulateDungeon(proceduralDungeon, selectedTier, activeDungeon));
_dungeonData.ActiveDungeons.Add(activeDungeon);
return selectedTier;
}
private ProceduralDynamicDungeon GetProceduralDungeon(BasePortal dungeonPortal)
{
return (dungeonPortal as XmasDungeon)?.dungeonInstance.Get(true) ?? (dungeonPortal as HalloweenDungeon)?.dungeonInstance.Get(true);
}
private void PopulateDungeon(ProceduralDynamicDungeon proceduralDungeon, DungeonTierConfig tierConfig, ActiveDungeon activeDungeon)
{
var spawnLocations = GetAvailableSpawnLocations(proceduralDungeon);
if (spawnLocations.Count == 0)
{
Puts("No valid spawn locations available.");
return;
}
var allSpawnEntries = GenerateSpawnEntries(tierConfig);
int spawnIndex = 0;
foreach (var entry in allSpawnEntries)
{
var (position, rotation) = spawnLocations[spawnIndex];
var entity = GameManager.server.CreateEntity(entry.prefab.resourcePath, position, rotation);
if (entity != null)
{
InitializeEntity(entity, tierConfig, activeDungeon);
}
spawnIndex = (spawnIndex + 1) % spawnLocations.Count; // Circulate existing spawn locations
}
Puts($"Placed {activeDungeon.EntityIds.Count}/{allSpawnEntries.Count} entities.");
}
private List<(Vector3 Position, Quaternion Rotation)> GetAvailableSpawnLocations(ProceduralDynamicDungeon proceduralDungeon)
{
var locations = new List<(Vector3, Quaternion)>();
foreach (var cell in proceduralDungeon.spawnedCells)
{
foreach (var group in cell.spawnGroups)
{
foreach (var spawnPoint in group.spawnPoints)
{
if (spawnPoint != null && spawnPoint.IsAvailableTo(null))
{
spawnPoint.GetLocation(out var position, out var rotation);
locations.Add((position, rotation));
}
}
}
}
return locations;
}
private void InitializeEntity(BaseEntity entity, DungeonTierConfig tierConfig, ActiveDungeon activeDungeon)
{
entity.OwnerID = OwnerID;
entity.Spawn();
if (entity is NPCPlayer)
{
activeDungeon.NpcIds.Add(entity.net.ID.Value);
}
else if (entity is AutoTurret turret)
{
ConfigureTurret(turret, tierConfig.AutoTurretConfig);
turret.gameObject.AddComponent<TurretMonoBehaviour>();
}
else if (entity is StorageContainer storage)
{
ConfigureStorage(storage);
}
activeDungeon.EntityIds.Add(entity.net.ID.Value);
}
private int CalculateTotalEntities(DungeonTierConfig tierConfig)
{
return tierConfig.TotalLootBoxes + tierConfig.NpcSpawnConfigs.Sum(npcConfig => npcConfig.Total) + tierConfig.AutoTurretConfig.Total;
}
private void ValidateAndAttemptRetries(ProceduralDynamicDungeon proceduralDungeon, DungeonTierConfig tierConfig, ActiveDungeon activeDungeon, List<SpawnGroup.SpawnEntry> remainingEntries)
{
int retries = 3;
while (remainingEntries.Count > 0 && retries > 0)
{
Puts($"Retrying placement for {remainingEntries.Count} missing entities.");
PopulateDungeon(proceduralDungeon, tierConfig, activeDungeon);
retries--;
}
int entitiesPlaced = activeDungeon.EntityIds.Count;
int expectedEntities = CalculateTotalEntities(tierConfig);
if (entitiesPlaced < expectedEntities)
{
Puts($"Warning: Only placed {entitiesPlaced}/{expectedEntities} entities.");
}
else
{
Puts("All entities placed successfully.");
}
}
private object OnPortalUse(BasePlayer player, BasePortal portal)
{
if (_dungeonData.ActiveDungeons.All(d => d.PortalId != portal.net.ID.Value))
{
return null;
}
var proceduralDungeon = (portal as XmasDungeon)?.dungeonInstance.Get(true) ?? (portal as HalloweenDungeon)?.dungeonInstance.Get(true);
if (proceduralDungeon == null)
{
return null;
}
foreach (var cell in proceduralDungeon.spawnedCells)
{
foreach (var group in cell.spawnGroups)
{
for (int num = group.spawnInstances.Count - 1; num >= 0; num--)
{
SpawnPointInstance spawnPointInstance = group.spawnInstances[num];
BaseEntity entity = spawnPointInstance?.gameObject?.ToBaseEntity();
if (entity != null && !IsDungeonEntity(entity) && (entity is NPCDwelling || entity is ScarecrowNPC || entity is LootContainer))
{
entity.Kill();
}
}
}
}
return null;
}
private List<SpawnGroup.SpawnEntry> GenerateSpawnEntries(DungeonTierConfig tierConfig)
{
var allSpawnEntries = new List<SpawnGroup.SpawnEntry>();
AddNpcEntries(tierConfig, allSpawnEntries);
AddTurretEntries(tierConfig, allSpawnEntries);
AddLootEntries(tierConfig, allSpawnEntries);
return allSpawnEntries.OrderBy(_ => Random.value).ToList();
}
private void AddNpcEntries(DungeonTierConfig tierConfig, List<SpawnGroup.SpawnEntry> spawnEntries)
{
foreach (var config in tierConfig.NpcSpawnConfigs)
{
if (GameManifest.pathToGuid.TryGetValue(config.PrefabName, out var guid))
{
spawnEntries.AddRange(
Enumerable.Repeat(
new SpawnGroup.SpawnEntry
{
prefab = new GameObjectRef { guid = guid },
weight = 1,
mobile = true,
},
config.Total
)
);
}
}
}
private void AddTurretEntries(DungeonTierConfig tierConfig, List<SpawnGroup.SpawnEntry> spawnEntries)
{
var prefabName = "assets/prefabs/npc/autoturret/autoturret_deployed.prefab";
if (GameManifest.pathToGuid.TryGetValue(prefabName, out var turretGuid))
{
spawnEntries.AddRange(
Enumerable.Repeat(
new SpawnGroup.SpawnEntry
{
prefab = new GameObjectRef { guid = turretGuid },
weight = 1,
mobile = false,
},
tierConfig.AutoTurretConfig.Total
)
);
}
}
private void AddLootEntries(DungeonTierConfig tierConfig, List<SpawnGroup.SpawnEntry> spawnEntries)
{
var prefabName = "assets/prefabs/deployable/woodenbox/woodbox_deployed.prefab";
if (GameManifest.pathToGuid.TryGetValue(prefabName, out var boxGuid))
{
spawnEntries.AddRange(
Enumerable.Repeat(
new SpawnGroup.SpawnEntry
{
prefab = new GameObjectRef { guid = boxGuid },
weight = 1,
mobile = false,
},
tierConfig.TotalLootBoxes
)
);
}
}
private void ConfigureTurret(AutoTurret turret, TurretConfig turretConfig)
{
turret.health = turretConfig.Health;
turret.SetMaxHealth(turret.health);
var weapon = ItemManager.CreateByName(turretConfig.WeaponShortName);
weapon?.MoveToContainer(turret.inventory, 0);
var laserSight = ItemManager.CreateByName("weapon.mod.lasersight");
laserSight?.MoveToContainer(weapon.contents);
turret.UpdateAttachedWeapon();
}
private void ConfigureStorage(StorageContainer storage)
{
storage.skinID = _configData.LootBoxConfig.SmallWoodBoxSkinID;
AddCodeLock(storage);
PopulateStorage(storage.inventory, _configData.LootBoxConfig.LootItems);
}
private void AddCodeLock(StorageContainer storage)
{
var prefabName = "assets/prefabs/locks/keypad/lock.code.prefab";
var codeLock = GameManager.server.CreateEntity(prefabName) as CodeLock;
if (codeLock == null)
return;
codeLock.SetParent(storage, storage.GetSlotAnchorName(BaseEntity.Slot.Lock));
codeLock.Spawn();
codeLock.code = Random.Range(1000, 9999).ToString();
codeLock.hasCode = true;
codeLock.SetFlag(BaseEntity.Flags.Locked, true);
}
private void PopulateStorage(ItemContainer container, List<ItemConfig> lootItems)
{
var shuffledItems = lootItems.OrderBy(_ => Random.value).ToList();
var differentItemsCount = 0;
foreach (var itemConfig in shuffledItems)
{
if (differentItemsCount >= _configData.LootBoxConfig.MaxDifferentItemsPerBox)
{
break;
}
if (Random.value * 100f <= itemConfig.InclusionChancePercentage)
{
var itemDefinition = ItemManager.FindItemDefinition(itemConfig.ShortName);
if (itemDefinition != null)
{
var amount = Random.Range(itemConfig.MinimumAmount, itemConfig.MaximumAmount + 1);
if (container.itemList.Count < container.capacity)
{
var item = ItemManager.Create(itemDefinition, amount);
item.MoveToContainer(container);
differentItemsCount++;
}
}
}
}
}
private List<ActiveDungeon> RemoveInactiveDungeons()
{
var removedDungeons = new List<ActiveDungeon>();
foreach (var dungeon in _dungeonData.ActiveDungeons.ToList())
{
if (CanBeRemoved(dungeon))
{
removedDungeons.Add(dungeon);
DestroyDungeon(dungeon);
}
}
return removedDungeons;
}
private void DestroyDungeon(ActiveDungeon dungeon)
{
DestroyEntities(dungeon);
RemoveDungeonMarkers(dungeon);
_dungeonData.ActiveDungeons.Remove(dungeon);
var portal = BaseNetworkable.serverEntities.Find(new NetworkableId(dungeon.PortalId)) as BasePortal;
portal?.Kill(BaseNetworkable.DestroyMode.None);
SaveData();
}
private void DestroyEntities(ActiveDungeon dungeon)
{
foreach (var entityId in dungeon.EntityIds)
{
var entity = BaseNetworkable.serverEntities.Find(new NetworkableId(entityId)) as BaseEntity;
entity?.Kill(BaseNetworkable.DestroyMode.None);
}
dungeon.EntityIds.Clear();
}
private void RemoveDungeonMarkers(ActiveDungeon dungeon)
{
foreach (var marker in _dungeonData.ActiveDungeonMarkers.Where(m => m.Position == dungeon.Position).ToList())
{
var vendingMarker = BaseNetworkable.serverEntities.Find(new NetworkableId(marker.VendingMarker)) as BaseEntity;
var radiusMarker = BaseNetworkable.serverEntities.Find(new NetworkableId(marker.RadiusMarker)) as BaseEntity;
vendingMarker?.Kill(BaseNetworkable.DestroyMode.None);
radiusMarker?.Kill(BaseNetworkable.DestroyMode.None);
_dungeonData.ActiveDungeonMarkers.Remove(marker);
}
}
private IEnumerable<ActiveDungeon> RemoveAllDungeons()
{
var removedDungeons = new List<ActiveDungeon>(_dungeonData.ActiveDungeons);
foreach (var dungeon in removedDungeons)
{
DestroyDungeon(dungeon);
}
return removedDungeons;
}
#endregion
#region Utility Methods
private static bool IsValidSteamPlayer(BasePlayer targetPlayer) => targetPlayer != null && targetPlayer.userID.IsSteamId();
private void NotifyPlayersOfNewDungeon(Vector3 position, string tierName)
{
var gridPosition = PhoneController.PositionToGridCoord(position);
foreach (var player in BasePlayer.activePlayerList)
{
var message = Msg(player.UserIDString, "DungeonAppeared", tierName, gridPosition);
player.ChatMessage(message);
player.ShowToast(GameTip.Styles.Blue_Normal, message);
}
}
private void NotifyPlayer(BasePlayer player, DungeonSpawnResult result)
{
var messageKey = result switch
{
DungeonSpawnResult.Success => "DungeonSpawnedSuccess",
DungeonSpawnResult.MaxActiveDungeonsReached => "MaxActiveDungeonsReached",
DungeonSpawnResult.NoValidSpawnLocation => "DungeonSpawnFailed",
_ => null,
};
player.ChatMessage(Msg(player.UserIDString, messageKey));
}
private void NotifyPlayerOnRemoval(BasePlayer player, IEnumerable<ActiveDungeon> removedDungeons, string noDungeonMessage)
{
if (!removedDungeons.Any())
{
player.ChatMessage(Msg(player.UserIDString, noDungeonMessage));
return;
}
foreach (var dungeon in removedDungeons)
{
var gridPosition = PhoneController.PositionToGridCoord(dungeon.Position);
player.ChatMessage(Msg(player.UserIDString, "DungeonRemovedDetail", dungeon.TierName, gridPosition));
}
}
private void LogDungeonSpawnResult(DungeonSpawnResult result)
{
var logMessage = result switch
{
DungeonSpawnResult.Success => "Dungeon spawned successfully.",
DungeonSpawnResult.MaxActiveDungeonsReached => "Cannot spawn: Maximum number of active dungeons reached.",
DungeonSpawnResult.NoValidSpawnLocation => "Could not find a valid location to spawn a new dungeon.",
_ => null,
};
if (logMessage != null)
Puts(logMessage);
}
private void LogDungeonRemoval(ActiveDungeon dungeon)
{
var gridPosition = PhoneController.PositionToGridCoord(dungeon.Position);
Puts($"Removed inactive dungeon of tier {dungeon.TierName} at grid {gridPosition}.");
}
#endregion
#region Helper and Utility Methods
private bool IsAdmin(BasePlayer player) => player != null && player.IsAdmin;
private bool IsDungeonEntity(BaseEntity entity) => entity != null && _dungeonData.ActiveDungeons.Any(d => d.EntityIds.Contains(entity.net.ID.Value));
private string GetRandomDungeonPrefab()
{
var prefabs = new List<string>();
if (_configData.DungeonSpawn.EnableXmasDungeon)
prefabs.Add("assets/prefabs/missions/portal/xmasportalentry.prefab");
if (_configData.DungeonSpawn.EnableHalloweenDungeon)
prefabs.Add("assets/prefabs/missions/portal/halloweenportalentry.prefab");
return prefabs.Count > 0 ? prefabs[Random.Range(0, prefabs.Count)] : null;
}
private static bool IsValidPosition(Vector3 position, out Vector3 suitablePosition, out Quaternion suitableRotation)
{
suitablePosition = Vector3.zero;
suitableRotation = Quaternion.identity;
if (!CollidersChecks.GetTerrainInfo(position, out var hitInfo))
return false;
if (CollidersChecks.InsideRock(position, 10f))
return false;
if (CollidersChecks.InWater(position))
return false;
if (CollidersChecks.OnRoadOrRail(position))
return false;
if (CollidersChecks.InNoBuildZone(position, 10f))
return false;
return SetSuitablePositionAndRotation(position, hitInfo, out suitablePosition, out suitableRotation);
}
private static bool SetSuitablePositionAndRotation(Vector3 position, RaycastHit hitInfo, out Vector3 suitablePosition, out Quaternion suitableRotation)
{
suitablePosition = hitInfo.point;
suitableRotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
return true;
}
private void CreateDungeonMarkers(Vector3 position, string tierName)
{
var prefabVendingMarker = "assets/prefabs/deployable/vendingmachine/vending_mapmarker.prefab";
var vendingMarker = CreateMapMarker<VendingMachineMapMarker>(prefabVendingMarker, position);
vendingMarker.OwnerID = OwnerID;
vendingMarker.markerShopName = $"Dungeon: {tierName}";
vendingMarker.SendNetworkUpdate();
var prefabRadiusMarker = "assets/prefabs/tools/map/genericradiusmarker.prefab";
var radiusMarker = CreateMapMarker<MapMarkerGenericRadius>(prefabRadiusMarker, position);
radiusMarker.OwnerID = OwnerID;
radiusMarker.alpha = 0.75f;
radiusMarker.radius = 0.5f;
radiusMarker.color2 = GetMarkerColorByTier(tierName);
radiusMarker.SendUpdate();
radiusMarker.SendNetworkUpdate();
_dungeonData.ActiveDungeonMarkers.Add(
new DungeonMarker
{
VendingMarker = vendingMarker.net.ID.Value,
RadiusMarker = radiusMarker.net.ID.Value,
Position = position,
Tier = tierName,
}
);
}
private T CreateMapMarker<T>(string prefab, Vector3 position)
where T : BaseEntity
{
var entity = GameManager.server.CreateEntity(prefab, position) as T;
entity?.Spawn();
return entity;
}
private static Color GetMarkerColorByTier(string tierName)
{
return tierName switch
{
"Easy" => Color.green,
"Normal" or "Medium" => Color.yellow,
"Hard" => Color.red,
"Nightmare" => Color.black,
_ => Color.white,
};
}
private bool CanBeRemoved(ActiveDungeon dungeon)
{
return AreAllNpcsGone(dungeon) && NoPlayersInside(dungeon);
}
private bool AreAllNpcsGone(ActiveDungeon dungeon)
{
return dungeon.NpcIds.All(npcId =>
{
var npc = BaseNetworkable.serverEntities.Find(new NetworkableId(npcId)) as BaseEntity;
return npc == null || npc.IsDestroyed;
});
}
private bool NoPlayersInside(ActiveDungeon dungeon)
{
var dynamicDungeon = BaseNetworkable.serverEntities.Find(new NetworkableId(dungeon.DynamicDungeonId)) as ProceduralDynamicDungeon;
return dynamicDungeon == null || !dynamicDungeon.ContainsAnyPlayers();
}
private bool TryFindDungeonSpawnPoint(out Vector3 position, out Quaternion rotation)
{
var mapSize = World.Size;
for (var attempt = 0; attempt < 1000; attempt++)
{
var candidatePosition = new Vector3(Random.Range(-mapSize / 2, mapSize / 2), 0, Random.Range(-mapSize / 2, mapSize / 2));
if (IsValidPosition(candidatePosition, out position, out rotation) && IsSafeFromOtherDungeons(position) && !IsCloseToMonuments(position))
{
return true;
}
}
position = Vector3.zero;
rotation = Quaternion.identity;
return false;
}
private bool IsSafeFromOtherDungeons(Vector3 position)
{
return _dungeonData.ActiveDungeons.All(dungeon => Vector3.Distance(position, dungeon.Position) >= _configData.DungeonSpawn.MinDistanceBetweenDungeons);
}
private bool IsCloseToMonuments(Vector3 position)
{
return TerrainMeta.Path.Monuments.Any(mon => Vector3.Distance(position, mon.transform.position) < _configData.DungeonSpawn.MinDistanceFromMonuments);
}
private DungeonTierConfig GetDungeonTier(int cellCount)
{
return cellCount switch
{
< 5 => _configData.Tiers.Easy,
< 8 => _configData.Tiers.Normal,
< 12 => _configData.Tiers.Medium,
< 16 => _configData.Tiers.Hard,
_ => _configData.Tiers.Nightmare,
};
}
#endregion
#region Cleanup
private void Unload()
{
RemoveAllDungeons();
_autoSpawnTimer?.Destroy();
_dungeonCheckTimer?.Destroy();
_updateMarkersTimer?.Destroy();
}
#endregion
#region Configuration
private class ConfigData
{
[JsonProperty(PropertyName = "Dungeon Spawn Settings")]
public DungeonSpawnSettings DungeonSpawn { get; set; } = new DungeonSpawnSettings();
[JsonProperty(PropertyName = "Tiers")]
public DungeonTiers Tiers { get; set; } = new DungeonTiers();
[JsonProperty(PropertyName = "Loot Box Config")]
public LootBoxConfig LootBoxConfig { get; set; } = new LootBoxConfig();
[JsonProperty(PropertyName = "Version")]
public VersionNumber Version { get; set; }
}
private class DungeonTiers
{
[JsonProperty(PropertyName = "Easy")]
public DungeonTierConfig Easy { get; set; } =
new DungeonTierConfig
{
TotalLootBoxes = 1,
NpcSpawnConfigs = new List<NpcSpawnConfig>
{
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_full_pistol.prefab", Total = 1 },
},
AutoTurretConfig = new TurretConfig
{
Total = 1,
Health = 300,
WeaponShortName = "pistol.revolver",
},
};
[JsonProperty(PropertyName = "Normal")]
public DungeonTierConfig Normal { get; set; } =
new DungeonTierConfig
{
TotalLootBoxes = 2,
NpcSpawnConfigs = new List<NpcSpawnConfig>
{
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_full_pistol.prefab", Total = 3 },
},
AutoTurretConfig = new TurretConfig
{
Total = 2,
Health = 600,
WeaponShortName = "smg.2",
},
};
[JsonProperty(PropertyName = "Medium")]
public DungeonTierConfig Medium { get; set; } =
new DungeonTierConfig
{
TotalLootBoxes = 4,
NpcSpawnConfigs = new List<NpcSpawnConfig>
{
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_full_pistol.prefab", Total = 4 },
},
AutoTurretConfig = new TurretConfig
{
Total = 3,
Health = 1000,
WeaponShortName = "smg.mp5",
},
};
[JsonProperty(PropertyName = "Hard")]
public DungeonTierConfig Hard { get; set; } =
new DungeonTierConfig
{
TotalLootBoxes = 5,
NpcSpawnConfigs = new List<NpcSpawnConfig>
{
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_full_pistol.prefab", Total = 4 },
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_full_pistol.prefab", Total = 3 },
},
AutoTurretConfig = new TurretConfig
{
Total = 4,
Health = 1500,
WeaponShortName = "rifle.ak",
},
};
[JsonProperty(PropertyName = "Nightmare")]
public DungeonTierConfig Nightmare { get; set; } =
new DungeonTierConfig
{
TotalLootBoxes = 6,
NpcSpawnConfigs = new List<NpcSpawnConfig>
{
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_full_pistol.prefab", Total = 5 },
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_full_pistol.prefab", Total = 5 },
},
AutoTurretConfig = new TurretConfig
{
Total = 6,
Health = 2000,
WeaponShortName = "rifle.ak",
},
};
}
private class DungeonTierConfig
{
[JsonProperty(PropertyName = "Maximum Loot Boxes")]
public int TotalLootBoxes { get; set; }
[JsonProperty(PropertyName = "NPC Spawn Configs", ObjectCreationHandling = ObjectCreationHandling.Replace)]
public List<NpcSpawnConfig> NpcSpawnConfigs { get; set; }
[JsonProperty(PropertyName = "Auto Turret Config")]
public TurretConfig AutoTurretConfig { get; set; } = new TurretConfig();
}
public class LootBoxConfig
{
[JsonProperty(PropertyName = "Small Wood Box Skin ID")]
public ulong SmallWoodBoxSkinID { get; set; } = 2998755525;
[JsonProperty(PropertyName = "Max Different Items Per Box")]
public int MaxDifferentItemsPerBox { get; set; } = 6;
[JsonProperty(PropertyName = "Loot Items", ObjectCreationHandling = ObjectCreationHandling.Replace)]
public List<ItemConfig> LootItems { get; set; } =
new List<ItemConfig>
{
new ItemConfig
{
ShortName = "ammo.rifle",
InclusionChancePercentage = 15,
MinimumAmount = 100,
MaximumAmount = 300,
},
};
}
private class NpcSpawnConfig
{
[JsonProperty(PropertyName = "Prefab")]
public string PrefabName { get; set; }
[JsonProperty(PropertyName = "Maximum")]
public int Total { get; set; }
}
private class TurretConfig
{
[JsonProperty(PropertyName = "Maximum")]
public int Total { get; set; } = 3;
[JsonProperty(PropertyName = "Health")]
public float Health { get; set; } = 1000f;
[JsonProperty(PropertyName = "Weapon Short Name")]
public string WeaponShortName { get; set; } = "rifle.ak";
}
private class DungeonSpawnSettings
{
[JsonProperty(PropertyName = "Enable Xmas Dungeon")]
public bool EnableXmasDungeon { get; set; } = true;
[JsonProperty(PropertyName = "Enable Halloween Dungeon")]
public bool EnableHalloweenDungeon { get; set; } = true;
[JsonProperty(PropertyName = "Max Total Active Dungeons")]
public int MaxTotalActiveDungeons { get; set; } = 4;
[JsonProperty(PropertyName = "Min Distance From Monuments")]
public float MinDistanceFromMonuments { get; set; } = 100f;
[JsonProperty(PropertyName = "Min Distance Between Dungeons")]
public float MinDistanceBetweenDungeons { get; set; } = 50f;
[JsonProperty(PropertyName = "Auto Spawn Cycle Interval")]
public float AutoSpawnCycleInterval { get; set; } = 60f;
[JsonProperty(PropertyName = "Enable Auto Spawn")]
public bool EnableAutoSpawn { get; set; } = true;
}
public class ItemConfig
{
[JsonProperty(PropertyName = "ShortName")]
public string ShortName { get; set; }
[JsonProperty(PropertyName = "Inclusion Chance Percentage")]
public float InclusionChancePercentage { get; set; }
[JsonProperty(PropertyName = "Minimum Amount")]
public int MinimumAmount { get; set; }
[JsonProperty(PropertyName = "Maximum Amount")]
public int MaximumAmount { get; set; }
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
_configData = Config.ReadObject<ConfigData>() ?? new ConfigData();
}
catch (Exception ex)
{
PrintError($"The configuration file is corrupt: {ex}");
LoadDefaultConfig();
}
SaveConfig();
}
protected override void LoadDefaultConfig()
{
PrintWarning("Creating a new configuration file.");
_configData = new ConfigData { Version = Version };
}
protected override void SaveConfig()
{
Config.WriteObject(_configData, true);
}
#endregion
#region Localization
protected override void LoadDefaultMessages()
{
lang.RegisterMessages(
new Dictionary<string, string>
{
["NoInactiveDungeonsToRemove"] = "There are no inactive dungeons to remove.",
["NoDungeonsToRemove"] = "There are no dungeons to remove.",
["DungeonRemovedDetail"] = "Removed dungeon of tier {0} at grid {1}.",
["AttemptSpawnDungeons"] = "Attempting to spawn dungeons.",
["DungeonAppeared"] = "A {0} Dungeon has appeared at grid {1}!",
["DungeonSpawnFailed"] = "Could not find a valid location to spawn a new dungeon.",
["DungeonSpawnedSuccess"] = "Dungeon spawned successfully.",
["MaxActiveDungeonsReached"] = "Cannot spawn: Maximum number of active dungeons reached.",
},
this
);
}
private string Msg(string userId, string key, params object[] args)
{
return string.Format(lang.GetMessage(key, this, userId), args);
}
#endregion
#region Data Classes
private class DungeonData
{
public List<ActiveDungeon> ActiveDungeons { get; set; } = new List<ActiveDungeon>();
public List<DungeonMarker> ActiveDungeonMarkers { get; set; } = new List<DungeonMarker>();
}
private class ActiveDungeon
{
public ulong PortalId { get; set; }
public ulong DynamicDungeonId { get; set; }
public Vector3 Position { get; set; }
public string TierName { get; set; }
public List<ulong> EntityIds { get; set; } = new List<ulong>();
public List<ulong> NpcIds { get; set; } = new List<ulong>();
}
private class DungeonMarker
{
public ulong VendingMarker { get; set; }
public ulong RadiusMarker { get; set; }
public Vector3 Position { get; set; }
public string Tier { get; set; }
}
#endregion
#region Collider Checks
public static class CollidersChecks
{
public static bool InsideRock(Vector3 position, float radius)
{
List<Collider> colliders = Pool.GetList<Collider>();
Vis.Colliders(position, radius, colliders, Layers.Mask.World, QueryTriggerInteraction.Ignore);
bool isInsideRock = colliders.Any(collider =>
collider.name.Contains("rock", StringComparison.OrdinalIgnoreCase)
|| collider.name.Contains("cliff", StringComparison.OrdinalIgnoreCase)
|| collider.name.Contains("formation", StringComparison.OrdinalIgnoreCase)
);
Pool.FreeList(ref colliders);
return isInsideRock;
}
public static bool GetTerrainInfo(Vector3 position, out RaycastHit hitInfo)
{
return Physics.Linecast(position + Vector3.up, position - Vector3.up, out hitInfo, Layers.Mask.Terrain);
}
public static bool InWater(Vector3 position) => WaterLevel.Test(position, false, false);
public static bool OnRoadOrRail(Vector3 position) => (TerrainMeta.TopologyMap.GetTopology(position) & (int)TerrainTopology.Enum.Road) != 0;
public static bool InNoBuildZone(Vector3 position, float radius) => Physics.CheckSphere(position, radius, Layers.Mask.Prevent_Building, QueryTriggerInteraction.Ignore);
}
#endregion
#region Turret MonoBehaviour
public class TurretMonoBehaviour : MonoBehaviour
{
private AutoTurret _turret;
private const float SearchDistance = 15f;
private const int MaxReserveAmmo = 1000;
private void Awake()
{
InitializeTurret();
}
private void InitializeTurret()
{
_turret = GetComponent<AutoTurret>();
var triggerCollider = _turret.targetTrigger.GetComponent<SphereCollider>();
triggerCollider.enabled = false;
_turret.SetPeacekeepermode(false);
_turret.InitiateStartup();
_turret.SetIsOnline(true);
_turret.CancelInvoke(_turret.ServerTick);
_turret.SetTarget(null);
_turret.InvokeRepeating(PerformTurretCycle, 1f, 0.01f);
_turret.InvokeRepeating(ScanForTargets, 2f, 1f);
_turret.isLootable = false;
_turret.dropFloats = false;
_turret.dropsLoot = false;
_turret.SendNetworkUpdateImmediate();
InvokeRepeating(nameof(RefillAmmo), 2f, 30f);
}
private void RefillAmmo()
{
if (_turret.AttachedWeapon is not BaseProjectile baseProjectile || baseProjectile.primaryMagazine?.ammoType == null)
return;
var ammoType = baseProjectile.primaryMagazine.ammoType;
int currentAmmoCount = CalculateCurrentAmmoCount(ammoType);
if (currentAmmoCount < MaxReserveAmmo)
{
int ammoNeeded = MaxReserveAmmo - currentAmmoCount;
Item ammoItem = ItemManager.Create(ammoType, ammoNeeded);
ammoItem?.MoveToContainer(_turret.inventory);
_turret.UpdateTotalAmmo();
_turret.EnsureReloaded();
_turret.SendNetworkUpdateImmediate();
}
}
private int CalculateCurrentAmmoCount(ItemDefinition ammoType)
{
return _turret.inventory.itemList.Where(item => item.info == ammoType).Sum(item => item.amount);
}
private void ScanForTargets()
{
var entityContents = _turret.targetTrigger.entityContents ??= new HashSet<BaseEntity>();
entityContents.Clear();
int foundTargets = BaseEntity.Query.Server.GetPlayersInSphereFast(transform.position, SearchDistance, AIBrainSenses.playerQueryResults, IsTargetValid);
if (foundTargets == 0)
return;
_turret.authDirty = true;
for (int i = 0; i < foundTargets; i++)
{
var player = AIBrainSenses.playerQueryResults[i];
if (Interface.CallHook("OnEntityEnter", _turret.targetTrigger, player) != null || player.IsSleeping() || (player.InSafeZone() && !player.IsHostile()))
continue;
entityContents.Add(player);
}
}
private bool IsTargetValid(BasePlayer player) => player != null && !player.IsNpc;
private void PerformTurretCycle()
{
if (_turret.isClient || _turret.IsDestroyed)
return;
float deltaTime = (float)_turret.timeSinceLastServerTick;
_turret.timeSinceLastServerTick = 0f;
if (_turret.IsOnline() && !_turret.IsBeingControlled)
{
if (!_turret.HasTarget())
{
_turret.IdleTick(deltaTime);
}
else
{
ExecuteTargetEngagement();
}
}
_turret.UpdateFacingToTarget(deltaTime);
UpdateAmmoStatus();
}
private void ExecuteTargetEngagement()
{
if (Time.realtimeSinceStartup >= _turret.nextVisCheck)
{
_turret.nextVisCheck = Time.realtimeSinceStartup + Random.Range(0.2f, 0.3f);
_turret.targetVisible = _turret.ObjectVisible(_turret.target);
if (_turret.targetVisible)
_turret.lastTargetSeenTime = Time.realtimeSinceStartup;
}
_turret.EnsureReloaded();
if (ShouldFireAtTarget())
{
var weapon = _turret.GetAttachedWeapon();
FireWeapon(weapon);
}
ValidateTargetEngagement();
}
private bool ShouldFireAtTarget()
{
return Time.time >= _turret.nextShotTime
&& _turret.targetVisible
&& Mathf.Abs(_turret.AngleToTarget(_turret.target, _turret.currentAmmoGravity != 0f)) < _turret.GetMaxAngleForEngagement();
}
private void FireWeapon(BaseProjectile weapon)
{
if (weapon == null)
{
_turret.nextShotTime = Time.time + 1f;
return;
}
if (weapon.primaryMagazine.contents > 0)
{
_turret.FireAttachedGun(_turret.AimOffset(_turret.target), _turret.aimCone, null, _turret.PeacekeeperMode() ? _turret.target : null);
float delay = weapon.isSemiAuto ? weapon.repeatDelay * 1.5f : weapon.repeatDelay;
delay = weapon.ScaleRepeatDelay(delay);
_turret.nextShotTime = Time.time + delay;
}
else
{
_turret.nextShotTime = Time.time + 5f;
}
}
private void ValidateTargetEngagement()
{
var targetPlayer = _turret.target as BasePlayer;
if (_turret.target != null && (!IsValidSteamPlayer(targetPlayer) || _turret.target.IsDead() || TimeExceedsLastSeen() || DistanceExceedsSightRange() || ShouldTargetBeReset()))
{
_turret.SetTarget(null);
}
}
private bool TimeExceedsLastSeen() => Time.realtimeSinceStartup - _turret.lastTargetSeenTime > 3f;
private bool DistanceExceedsSightRange() => Vector3.Distance(_turret.transform.position, _turret.target.transform.position) > _turret.sightRange;
private bool ShouldTargetBeReset() => _turret.PeacekeeperMode() && !_turret.IsEntityHostile(_turret.target);
private void UpdateAmmoStatus()
{
if (_turret.totalAmmoDirty && Time.time > _turret.nextAmmoCheckTime)
{
_turret.UpdateTotalAmmo();
_turret.totalAmmoDirty = false;
_turret.nextAmmoCheckTime = Time.time + 0.5f;
}
}
private void OnDestroy()
{
CancelInvoke(nameof(RefillAmmo));
}
}
#endregion
}
}
csharp
private void StartDungeonTimers()
{
StartAutoSpawnTimer();
StartDungeonCheckTimer();
StartUpdateMarkersTimer();
}
private void LoadData()
{
try
{
_dungeonData = Interface.Oxide.DataFileSystem.GetFile("Dungeons_Data").ReadObject<DungeonData>() ?? new DungeonData();
}
catch (Exception ex)
{
PrintError($"Failed to load dungeon data: {ex.Message}");
_dungeonData = new DungeonData(); // Ensure we have some data structure to work with
}
}
private void StartAutoSpawnTimer()
{
_autoSpawnTimer = timer.Every(_configData.DungeonSpawn.AutoSpawnCycleInterval, AttemptAutoSpawn);
}
private void AttemptAutoSpawn()
{
var result = AutoSpawnDungeon();
LogDungeonSpawnResult(result);
}
private List<(Vector3, Quaternion)> GetValidSpawnLocations(ProceduralDynamicDungeon dungeon)
{
return dungeon.spawnedCells.SelectMany(cell => cell.spawnGroups)
.SelectMany(group => group.spawnPoints)
.Where(spawnPoint => spawnPoint != null && spawnPoint.IsAvailableTo(null))
.Select(spawnPoint => {
spawnPoint.GetLocation(out var position, out var rotation);
return (position, rotation);
})
.ToList();
}
// Other helper methods can be similarly broken down and documented...
Documentation
Besides inline comments, maintaining proper schedule for your configuration and data structures with JSON schema could help in better managing the evolving needs of plugins.
Implementing these suggestions should lead to a more robust and easier-to-maintain Rust plugin that complies with best coding practices.
private DungeonTierConfig CreateDungeon(Vector3 position, Quaternion rotation)
{
var prefabPath = GetRandomDungeonPrefab();
if (string.IsNullOrEmpty(prefabPath))
{
Puts("Error: Prefab path is empty.");
return null;
}
var dungeonPortal = GameManager.server.CreateEntity(prefabPath, position, rotation) as BasePortal;
if (!ValidateDungeonPortal(dungeonPortal))
return null;
var selectedTier = SetupDungeon(dungeonPortal, position, rotation);
if (selectedTier != null)
{
SetupMarkersForDungeon(position, selectedTier);
}
return selectedTier;
}
private bool ValidateDungeonPortal(BasePortal dungeonPortal)
{
if (dungeonPortal == null)
{
Puts("Error: Dungeon portal creation failed.");
return false;
}
return true;
}
private DungeonTierConfig SetupDungeon(BasePortal dungeonPortal, Vector3 position, Quaternion rotation)
{
dungeonPortal.OwnerID = OwnerID;
dungeonPortal.Spawn();
var proceduralDungeon = GetProceduralDungeon(dungeonPortal);
if (proceduralDungeon == null || proceduralDungeon.spawnedCells.Count < 1)
{
dungeonPortal.Kill(BaseNetworkable.DestroyMode.None);
Puts("Error: Procedural dungeon setup failed.");
return null;
}
var selectedTier = GetDungeonTier(proceduralDungeon.spawnedCells.Count);
var activeDungeon = RegisterActiveDungeon(dungeonPortal, proceduralDungeon, position, selectedTier);
NextTick(() => PopulateDungeon(proceduralDungeon, selectedTier, activeDungeon));
return selectedTier;
}
private void SetupMarkersForDungeon(Vector3 position, DungeonTierConfig selectedTier)
{
var tierName = _tierNameMap[selectedTier];
CreateDungeonMarkers(position, tierName);
NotifyPlayersOfNewDungeon(position, tierName);
}
private ActiveDungeon RegisterActiveDungeon(BasePortal portal, ProceduralDynamicDungeon dungeon, Vector3 position, DungeonTierConfig tierConfig)
{
var activeDungeon = new ActiveDungeon
{
PortalId = portal.net.ID.Value,
DynamicDungeonId = dungeon.net.ID.Value,
Position = position,
TierName = _tierNameMap[tierConfig],
EntityIds = new List<ulong>(),
};
_dungeonData.ActiveDungeons.Add(activeDungeon);
return activeDungeon;
}
// Further refactor similarly...
Implementing these changes not only facilitates ease of maintenance and debugging, but also boosts performance where feasible by eliminating unnecessary checks or optimally distributing workload.