I am based on OpenAI's GPT-4 architecture. How can I assist you today?
User
using System.Collections.Generic;
using Facepunch;
using Newtonsoft.Json;
using Rust;
using UnityEngine;
using Random = UnityEngine.Random;
namespace Oxide.Plugins
{
[Info("Dungeons", "Marte6", "1.0.0")]
public class Dungeons : RustPlugin
{
private static PluginConfig _config;
private Timer _autoSpawnTimer;
private const string DungeonPrefabPath = "assets/prefabs/missions/portal/xmasportalentry.prefab";
private XmasDungeon _currentDungeon;
private List _spawnedEntities = new List();
code
#region Initialization and Configuration
private void Init()
{
LoadConfig();
if (_config.EnableAutoSpawn)
{
StartAutoSpawnTimer();
}
}
protected override void LoadConfig()
{
base.LoadConfig();
_config = Config.ReadObject<PluginConfig>() ?? CreateDefaultConfig();
if (IsConfigOutdated())
{
UpdateConfig();
}
SaveConfig();
}
protected override void LoadDefaultConfig()
{
_config = CreateDefaultConfig();
}
protected override void SaveConfig()
{
Config.WriteObject(_config, true);
}
private PluginConfig CreateDefaultConfig()
{
return new PluginConfig
{
Version = Version.ToString(),
EnableAutoSpawn = true,
AutoSpawnIntervalMinutes = 30f,
MinSpawnDistance = 6f,
MaxSpawnDistance = 20f,
MaxSpawnAttempts = 5,
NpcSpawnConfigs = new List<NpcSpawnConfig>
{
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/tunneldweller/npc_tunneldweller.prefab", EntitysPerCell = 2 },
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_heavy.prefab", EntitysPerCell = 2 },
},
AutoTurretConfigs = new List<AutoTurretConfigs>
{
new AutoTurretConfigs { PrefabName = "assets/prefabs/npc/autoturret/autoturret_deployed.prefab", EntitysPerCell = 2 },
},
};
}
private bool IsConfigOutdated()
{
return string.Compare(_config.Version, Version.ToString()) < 0;
}
private void UpdateConfig()
{
PrintWarning("Config changes detected! Updating...");
var defaultConfig = CreateDefaultConfig();
if (IsConfigOutdated())
{
_config = defaultConfig;
}
PrintWarning($"Config update complete! Updated from version {_config.Version} to {Version.ToString()}");
_config.Version = Version.ToString();
}
#endregion
#region Dungeon Management
[ChatCommand("dun")]
private void CreateDungeonCommand(BasePlayer player, string command, string[] args)
{
RemoveExistingDungeon();
if (TryFindDungeonSpawnPoint(player.transform.position, out var position, out var rotation))
{
_currentDungeon = CreateDungeon(position, rotation);
player.ChatMessage("Christmas Dungeon created!");
}
else
{
player.ChatMessage("Could not find a suitable location for the Christmas Dungeon.");
}
}
private void StartAutoSpawnTimer()
{
_autoSpawnTimer = timer.Every(_config.AutoSpawnIntervalMinutes * 60f, AutoSpawnDungeon);
}
private void AutoSpawnDungeon()
{
RemoveExistingDungeon();
for (var attempt = 0; attempt < _config.MaxSpawnAttempts; attempt++)
{
foreach (var player in BasePlayer.activePlayerList)
{
if (!TryFindDungeonSpawnPoint(player.transform.position, out var position, out var rotation))
continue;
_currentDungeon = CreateDungeon(position, rotation);
NotifyPlayersOfDungeonLocation(position);
return;
}
}
Puts("Failed to auto-spawn dungeon after max attempts.");
}
private void NotifyPlayersOfDungeonLocation(Vector3 position)
{
foreach (var player in BasePlayer.activePlayerList)
{
player.ChatMessage($"A Christmas Dungeon has appeared at coordinates: ({position.x:F0}, {position.z:F0})!");
}
}
private XmasDungeon CreateDungeon(Vector3 position, Quaternion rotation)
{
var entity = GameManager.server.CreateEntity(DungeonPrefabPath, position, rotation) as XmasDungeon;
if (entity == null)
{
Puts("Failed to create dungeon entity.");
return null;
}
entity.Spawn();
IntegrateEntitiesIntoDungeon(entity);
return entity;
}
private void IntegrateEntitiesIntoDungeon(XmasDungeon dungeon)
{
var proceduralDungeon = dungeon.dungeonInstance.Get(serverside: true);
if (proceduralDungeon == null)
{
Puts("Failed to get procedural dungeon instance.");
return;
}
int cellCount = proceduralDungeon.spawnedCells.Count;
if (cellCount == 0)
{
Puts("No cells found in dungeon for entities.");
return;
}
// Create a single list of all entities to be spawned
var allSpawnEntries = new List<SpawnGroup.SpawnEntry>();
// Add NPCs
foreach (var config in _config.NpcSpawnConfigs)
{
if (GameManifest.pathToGuid.TryGetValue(config.PrefabName, out var guid))
{
for (int i = 0; i < config.EntitysPerCell; i++)
{
allSpawnEntries.Add(
new SpawnGroup.SpawnEntry
{
prefab = new GameObjectRef { guid = guid },
weight = 1,
mobile = true,
}
);
}
}
}
// Add Auto Turrets
foreach (var config in _config.AutoTurretConfigs)
{
if (GameManifest.pathToGuid.TryGetValue(config.PrefabName, out var guid))
{
for (int i = 0; i < config.EntitysPerCell; i++)
{
allSpawnEntries.Add(
new SpawnGroup.SpawnEntry
{
prefab = new GameObjectRef { guid = guid },
weight = 1,
mobile = false,
}
);
}
}
}
// Integrate all entities into each cell
foreach (var cell in proceduralDungeon.spawnedCells)
{
SetupSpawnGroup(cell, allSpawnEntries);
}
}
private void SetupSpawnGroup(ProceduralDungeonCell cell, List<SpawnGroup.SpawnEntry> allSpawnEntries)
{
var spawnGroup = cell.gameObject.AddComponent<SpawnGroup>();
spawnGroup.prefabs = allSpawnEntries;
spawnGroup.maxPopulation = 100;
spawnGroup.numToSpawnPerTickMin = 0;
spawnGroup.numToSpawnPerTickMax = 0;
spawnGroup.fillOnSpawn = true;
spawnGroup.wantsInitialSpawn = true;
spawnGroup.SpawnInitial();
foreach (var instance in spawnGroup.spawnInstances)
{
var entity = instance.GetComponent<BaseEntity>();
if (entity != null && !_spawnedEntities.Contains(entity))
{
_spawnedEntities.Add(entity);
Puts($"Entity {entity.ShortPrefabName} spawned at {entity.transform.position}");
if (entity is AutoTurret autoTurret)
{
ConfigureAutoTurret(autoTurret);
}
}
}
}
private void ConfigureAutoTurret(AutoTurret autoTurret)
{
autoTurret.health = 1000f;
var weapon = ItemManager.CreateByName("rifle.ak");
weapon?.MoveToContainer(autoTurret.inventory, 0);
var ammo = ItemManager.CreateByName("ammo.rifle", 128);
ammo.MoveToContainer(autoTurret.inventory);
autoTurret.UpdateTotalAmmo();
autoTurret.EnsureReloaded();
autoTurret.InitializeHealth(1000f, 1000f);
autoTurret.SetPeacekeepermode(false);
}
private Vector3 GetRandomPositionNearby(Vector3 center, float minRadius, float maxRadius)
{
Vector2 randomCircle = Random.insideUnitCircle * Random.Range(minRadius, maxRadius);
return new Vector3(center.x + randomCircle.x, center.y, center.z + randomCircle.y);
}
#endregion
#region Utility and Cleanup
private bool TryFindDungeonSpawnPoint(Vector3 center, out Vector3 suitablePosition, out Quaternion suitableRotation)
{
suitablePosition = Vector3.zero;
suitableRotation = Quaternion.identity;
for (var attempt = 0; attempt < _config.MaxSpawnAttempts; attempt++)
{
var potentialPosition = TerrainUtilities.GetRandomPosition(center, _config.MinSpawnDistance, _config.MaxSpawnDistance);
if (!TerrainUtilities.TryGetTerrainInfo(potentialPosition, 5f, out var hitInfo) || !IsPositionSuitable(potentialPosition))
{
continue;
}
suitablePosition = hitInfo.point;
suitableRotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
return true;
}
return false;
}
private bool IsPositionSuitable(Vector3 position)
{
return !TerrainUtilities.ContainsRock(position, 2f)
&& !TerrainUtilities.IsInRadTown(position)
&& !TerrainUtilities.ContainsEntitiesNearby(position, 4f, TerrainUtilities.EntityLayerMask)
&& !TerrainUtilities.IsInWater(position)
&& !TerrainUtilities.IsOnRoadOrRail(position)
&& !TerrainUtilities.IsInNoBuildZone(position, 2f);
}
private void RemoveExistingDungeon()
{
if (_currentDungeon != null)
{
foreach (var entity in _spawnedEntities)
{
if (entity != null && !entity.IsDestroyed)
{
entity.Kill(BaseNetworkable.DestroyMode.None);
}
}
_spawnedEntities.Clear();
_currentDungeon.Kill(BaseNetworkable.DestroyMode.None);
_currentDungeon = null;
}
}
private void Unload()
{
RemoveExistingDungeon();
if (_autoSpawnTimer != null)
{
_autoSpawnTimer.Destroy();
}
}
#endregion
#region Configuration Classes
private class PluginConfig
{
[JsonProperty("Version")]
public string Version { get; set; }
[JsonProperty("Auto Spawn Enabled")]
public bool EnableAutoSpawn { get; set; }
[JsonProperty("Auto Spawn Interval Minutes")]
public float AutoSpawnIntervalMinutes { get; set; }
[JsonProperty("Min Spawn Distance")]
public float MinSpawnDistance { get; set; }
[JsonProperty("Max Spawn Distance")]
public float MaxSpawnDistance { get; set; }
[JsonProperty("Max Spawn Attempts")]
public int MaxSpawnAttempts { get; set; }
[JsonProperty("NPC Spawn Configs")]
public List<NpcSpawnConfig> NpcSpawnConfigs { get; set; }
[JsonProperty("Auto Turret Configs")]
public List<AutoTurretConfigs> AutoTurretConfigs { get; set; }
}
private class NpcSpawnConfig
{
[JsonProperty("Prefab")]
public string PrefabName { get; set; }
[JsonProperty("Entitys Per Cell")]
public int EntitysPerCell { get; set; }
}
private class AutoTurretConfigs
{
[JsonProperty("Prefab")]
public string PrefabName { get; set; }
[JsonProperty("Entitys Per Cell")]
public int EntitysPerCell { get; set; }
}
#endregion
}
public static class TerrainUtilities
{
public const int EntityLayerMask = Layers.Mask.Deployed | Layers.Mask.Construction;
public const int GroundLayerMask = Layers.Mask.Terrain | Layers.Mask.World | Layers.Mask.Default;
public static bool OnTopology(Vector3 position, TerrainTopology.Enum topology)
{
return (TerrainMeta.TopologyMap.GetTopology(position) & (int)topology) != 0;
}
public static bool IsOnRoadOrRail(Vector3 position)
{
return OnTopology(position, TerrainTopology.Enum.Road | TerrainTopology.Enum.Roadside | TerrainTopology.Enum.Rail | TerrainTopology.Enum.Railside);
}
public static bool IsInNoBuildZone(Vector3 position, float radius)
{
return Physics.CheckSphere(position, radius, Layers.Mask.Prevent_Building);
}
public static bool IsInWater(Vector3 position)
{
return WaterLevel.Test(position, false, true);
}
public static bool ContainsRock(Vector3 position, float radius)
{
var colliders = Pool.GetList<Collider>();
Vis.Colliders(position, radius, colliders, GroundLayerMask, QueryTriggerInteraction.Ignore);
bool result = colliders.Exists(collider =>
collider.name.Contains("rock", System.StringComparison.OrdinalIgnoreCase)
|| collider.name.Contains("cliff", System.StringComparison.OrdinalIgnoreCase)
|| collider.name.Contains("formation", System.StringComparison.OrdinalIgnoreCase)
);
Pool.FreeList(ref colliders);
return result;
}
public static bool IsInRadTown(Vector3 position)
{
foreach (var monumentInfo in TerrainMeta.Path.Monuments)
{
if (monumentInfo.IsInBounds(position) && monumentInfo.shouldDisplayOnMap)
{
return true;
}
}
return OnTopology(position, TerrainTopology.Enum.Monument);
}
public static bool ContainsEntitiesNearby(Vector3 position, float radius, int mask)
{
var hitColliders = Pool.GetList<Collider>();
GamePhysics.OverlapSphere(position, radius, hitColliders, mask, QueryTriggerInteraction.Ignore);
bool result = hitColliders.Exists(collider => collider.gameObject.ToBaseEntity() != null);
Pool.FreeList(ref hitColliders);
return result;
}
public static bool TryGetTerrainInfo(Vector3 startPosition, float range, out RaycastHit hitInfo)
{
return Physics.Linecast(startPosition + new Vector3(0f, range, 0f), startPosition - new Vector3(0f, range, 0f), out hitInfo, GroundLayerMask);
}
public static Vector3 GetRandomPosition(Vector3 center, float minRadius, float maxRadius)
{
Vector3 randomDirection = Random.onUnitSphere;
randomDirection.y = 0;
float randomDistance = Random.Range(minRadius, maxRadius);
Vector3 randomPosition = center + randomDirection * randomDistance;
randomPosition.y = TerrainMeta.HeightMap.GetHeight(randomPosition);
return randomPosition;
}
}
}
Assistant
This code appears to be a plugin for a game like Rust, designed to add dungeon features to the game. The plugin, named Dungeons, allows the game to automatically or manually spawn a dungeon at various locations on the map. Here’s a breakdown of some key components and functionalities:
Key Components
Plugin Initialization and Configuration:
The plugin uses a configuration file to determine its settings, such as whether automatic spawning is enabled and the interval for automatic spawning.
Configuration classes (e.g., PluginConfig, NpcSpawnConfig, AutoTurretConfigs) are defined, allowing for structured saving and loading of settings.
Dungeon Management:
Init method initializes the plugin and starts the auto-spawn timer if enabled.
CreateDungeonCommand is a chat command that allows players to manually create a dungeon at their current position.
AutoSpawnDungeon attempts to automatically spawn a dungeon at random positions near active players.
CreateDungeon handles the creation and spawning logic for the dungeon using predefined prefab paths.
Entity Integration:
Entities such as NPCs and auto turrets are integrated into the dungeon cells.
The method IntegrateEntitiesIntoDungeon populates each dungeon cell with the entities defined in the configuration.
ConfigureAutoTurret sets up auto turrets with specified health and ammo.
Utility and Cleanup:
Utility methods are provided to find suitable spawn points, check terrain suitability, and remove existing dungeons.
RemoveExistingDungeon ensures clean removal of any existing dungeon and its entities to prevent overlap or excessive resources usage.
Terrain Utilities:
A static class TerrainUtilities provides various methods to assess terrain features and suitability for spawning dungeons, such as checking for rocks, rad towns, water, and other obstacles.
Considerations
Error Handling and Safety: When working with game plugins, especially in a multiplayer environment, ensuring safety and error handling is crucial. This code attempts multiple spawn attempts and checks various conditions to ensure safe spawning.
User
quero alterar esse plugin Dungeons, somente a parte das configurações, o modo que são atribuidas pra ficarem do mesmo modo que o plugin abaixo:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Facepunch;
using Newtonsoft.Json;
using Oxide.Core;
using Oxide.Core.Plugins;
using Rust;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Building Grades", "Default/Arainrr", "1.0.13")]
[Description("Allows players to easily upgrade or downgrade an entire building")]
public class BuildingGrades : RustPlugin
{
#region Fields
code
Assistant
To refactor the Dungeons plugin's configuration structure to match the one used in Building Grades, you'll need to modify the configuration setup by introducing a new ConfigData class, and refactor the existing methods for loading, saving, and utilizing the configuration. Below is an outline of how to accomplish this:
Step 1: Define a ConfigData Class
You first need to create a new class ConfigData inside your Dungeons plugin:
csharp
private class ConfigData
{
[JsonProperty(PropertyName = "Version")]
public string Version { get; set; } = "1.0.0";
[JsonProperty(PropertyName = "Auto Spawn Settings")]
public AutoSpawnSettings AutoSpawn { get; set; } = new AutoSpawnSettings();
[JsonProperty(PropertyName = "NPC Spawn Configs")]
public List<NpcSpawnConfig> NpcSpawnConfigs { get; set; } = new List<NpcSpawnConfig>
{
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/tunneldweller/npc_tunneldweller.prefab", EntitysPerCell = 2 },
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_heavy.prefab", EntitysPerCell = 2 },
};
[JsonProperty(PropertyName = "Auto Turret Configs")]
public List<AutoTurretConfig> AutoTurretConfigs { get; set; } = new List<AutoTurretConfig>
{
new AutoTurretConfig { PrefabName = "assets/prefabs/npc/autoturret/autoturret_deployed.prefab", EntitysPerCell = 2 },
};
}
private class AutoSpawnSettings
{
[JsonProperty(PropertyName = "Enable Auto Spawn")]
public bool EnableAutoSpawn { get; set; } = true;
[JsonProperty(PropertyName = "Auto Spawn Interval Minutes")]
public float AutoSpawnIntervalMinutes { get; set; } = 30f;
[JsonProperty(PropertyName = "Min Spawn Distance")]
public float MinSpawnDistance { get; set; } = 6f;
[JsonProperty(PropertyName = "Max Spawn Distance")]
public float MaxSpawnDistance { get; set; } = 20f;
[JsonProperty(PropertyName = "Max Spawn Attempts")]
public int MaxSpawnAttempts { get; set; } = 5;
}
private class NpcSpawnConfig
{
[JsonProperty("Prefab")]
public string PrefabName { get; set; }
[JsonProperty("Entitys Per Cell")]
public int EntitysPerCell { get; set; }
}
private class AutoTurretConfig
{
[JsonProperty("Prefab")]
public string PrefabName { get; set; }
[JsonProperty("Entitys Per Cell")]
public int EntitysPerCell { get; set; }
}
User
me mande o plugin completo
Assistant
Sure, I can help you incorporate the updated configuration system into your Dungeons plugin. Here's the revised plugin with a configuration structure similar to the one used in the Building Grades plugin:
csharp
User
cada vez que o plugin reiniciar duplica as configurações:
{
"Version": "1.0.0",
"Auto Spawn Settings": {
"Enable Auto Spawn": true,
"Auto Spawn Interval Minutes": 30.0,
"Min Spawn Distance": 6.0,
"Max Spawn Distance": 20.0,
"Max Spawn Attempts": 5
},
"NPC Spawn Configs": [
{
"Prefab": "assets/rust.ai/agents/npcplayer/humannpc/tunneldweller/npc_tunneldweller.prefab",
"Entitys Per Cell": 2
},
{
"Prefab": "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_heavy.prefab",
"Entitys Per Cell": 2
},
{
"Prefab": "assets/rust.ai/agents/npcplayer/humannpc/tunneldweller/npc_tunneldweller.prefab",
"Entitys Per Cell": 2
},
{
"Prefab": "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_heavy.prefab",
"Entitys Per Cell": 2
},
{
"Prefab": "assets/rust.ai/agents/npcplayer/humannpc/tunneldweller/npc_tunneldweller.prefab",
"Entitys Per Cell": 2
},
{
"Prefab": "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_heavy.prefab",
"Entitys Per Cell": 2
},
{
"Prefab": "assets/rust.ai/agents/npcplayer/humannpc/tunneldweller/npc_tunneldweller.prefab",
"Entitys Per Cell": 2
},
{
"Prefab": "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_heavy.prefab",
"Entitys Per Cell": 2
}
],
"Auto Turret Configs": [
{
"Prefab": "assets/prefabs/npc/autoturret/autoturret_deployed.prefab",
"Entitys Per Cell": 2
},
{
"Prefab": "assets/prefabs/npc/autoturret/autoturret_deployed.prefab",
"Entitys Per Cell": 2
},
{
"Prefab": "assets/prefabs/npc/autoturret/autoturret_deployed.prefab",
"Entitys Per Cell": 2
},
{
"Prefab": "assets/prefabs/npc/autoturret/autoturret_deployed.prefab",
"Entitys Per Cell": 2
}
]
}
ajuste nele atualizado que estoumandando isso:
using System;
using System.Collections.Generic;
using Facepunch;
using Newtonsoft.Json;
using Rust;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Dungeons", "Marte6", "1.0.0")]
public class Dungeons : RustPlugin
{
private ConfigData _configData;
private Timer _autoSpawnTimer;
private const string DungeonPrefabPath = "assets/prefabs/missions/portal/xmasportalentry.prefab";
private XmasDungeon _currentDungeon;
private List _spawnedEntities = new List();
code
Assistant
The issue you're experiencing, where configurations are duplicated on plugin reload, is typically caused by not properly initializing the configuration list properties or writing the configuration multiple times during the execution flow. To resolve this, let's ensure that the configuration lists (NpcSpawnConfigs and AutoTurretConfigs) are initialized correctly only once and are not appended upon each load. Let's adjust the code to handle this correctly:
Adjusted Plugin Code
csharp
User
lembre que quero usar o mesmo modo daqui:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Facepunch;
using Newtonsoft.Json;
using Oxide.Core;
using Oxide.Core.Plugins;
using Rust;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Building Grades", "Default/Arainrr", "1.0.13")]
[Description("Allows players to easily upgrade or downgrade an entire building")]
public class BuildingGrades : RustPlugin
{
#region Fields
code
Assistant
To refactor the Dungeons plugin to use a configuration structure similar to the Building Grades plugin, you'll want to structure the configuration setup and access uniformly. Let's go through the refactored code with the configuration management akin to Building Grades:
Dungeons Plugin with Updated Configuration
csharp
User
o que faz isso?
[JsonObject(MemberSerialization.OptIn)]
Assistant
The [JsonObject(MemberSerialization.OptIn)] attribute is part of the Newtonsoft.Json library, commonly referred to as Json.NET, which is a popular JSON framework for .NET applications.
What It Does
When you decorate a class with [JsonObject(MemberSerialization.OptIn)], you explicitly specify that only the members (fields or properties) of the class that are also annotated with [JsonProperty] (or a similar attribute) will be serialized or deserialized to/from JSON.
Key Points
Opt-In Serialization: Only members marked with [JsonProperty] or a similar attribute will be included in the JSON serialization/deserialization process.
Control and Security: This approach gives you more control over which members are serialized, which is useful for preventing sensitive or unnecessary data from being exposed.
Example
Here's an example to illustrate how it works:
csharp
using Newtonsoft.Json;
[JsonObject(MemberSerialization.OptIn)]
public class User
{
[JsonProperty] // This attribute indicates that 'Username' will be serialized.
public string Username { get; set; }
public string Password { get; set; } // This will not be serialized due to OptIn policy.
[JsonProperty]
public int Age { get; set; }
public string Address { get; set; } // This will not be serialized.
}
Username and Age are serialized because they are marked with [JsonProperty].
Password and Address are not serialized because they are not explicitly marked with [JsonProperty].
Benefits
Selective Serialization: You can prevent accidental exposure of class members in JSON output, which is particularly important for security-sensitive information.
Reduced Payload: Unnecessary data is omitted from your JSON, reducing payload size and potentially improving performance.
Usage Scenario
Using [JsonObject(MemberSerialization.OptIn)] is useful when you want to ensure that only specific parts of your class are exposed via JSON. It gives you explicit control over what gets serialized, making your serialization code cleaner and safer.
User
existem algum modo de não precisar declarar as classes:
private class NpcSpawnConfig
{
[JsonProperty("Prefab")]
public string PrefabName { get; set; }
code
[JsonProperty("Entitys Per Cell")]
public int EntitysPerCell { get; set; }
}
private class AutoTurretConfig
{
[JsonProperty("Prefab")]
public string PrefabName { get; set; }
[JsonProperty("Entitys Per Cell")]
public int EntitysPerCell { get; set; }
}
separadas?
em
code
Assistant
If you want to avoid declaring similar classes separately and reduce redundancy, you can use a more generic approach by defining a single class that can serve multiple purposes. In this case, since NpcSpawnConfig and AutoTurretConfig are structurally identical, you can merge them into a single reusable class, perhaps named SpawnConfig, and use it for both NPCs and turrets.
Here's how you could implement it:
Combined Configuration Class
csharp
Not yet included in a published release.
Performance: Spawning entities and performing terrain checks can be resource-intensive. This plugin balances these operations by utilizing a timer and limiting the number of spawn attempts.
Customizability: With structured configuration, server administrators can easily adjust the dungeon's behavior and entity settings, providing flexibility in gameplay.
Overall, this plugin serves as a robust system for adding additional gameplay features to a game server. It demonstrates handling configuration, command execution, entity management, and terrain interaction within the context of a game modding framework.
[PluginReference]
private readonly Plugin Friends, Clans, NoEscape, RustTranslationAPI;
private const string PermissionUpPrefix = "buildinggrades.up.";
private const string PermissionDownPrefx = "buildinggrades.down.";
private const string PermissionUse = "buildinggrades.use";
private const string PermissionUpAll = PermissionUpPrefix + "all";
private const string PermissionDownAll = PermissionDownPrefx + "all";
private const string PermissionNoCost = "buildinggrades.nocost";
private const string PermissionAdmin = "buildinggrades.admin";
private static BuildingGrades _instance;
private BuildingGradeAssistant _assistant = new BuildingGradeAssistant();
private readonly Dictionary<string, HashSet<uint>> _categories = new Dictionary<string, HashSet<uint>>(StringComparer.OrdinalIgnoreCase);
private static readonly List<BuildingGrade.Enum> ValidGrades = new List<BuildingGrade.Enum>
{
BuildingGrade.Enum.Twigs, BuildingGrade.Enum.Wood, BuildingGrade.Enum.Stone, BuildingGrade.Enum.Metal, BuildingGrade.Enum.TopTier
};
#endregion Fields
#region Oxide Hooks
private void Init()
{
_instance = this;
foreach (var kvp in _configData.Permissions)
{
if (!permission.PermissionExists(kvp.Key, this))
{
permission.RegisterPermission(kvp.Key, this);
}
}
permission.RegisterPermission(PermissionUpAll, this);
permission.RegisterPermission(PermissionDownAll, this);
permission.RegisterPermission(PermissionNoCost, this);
permission.RegisterPermission(PermissionAdmin, this);
if (!permission.PermissionExists(PermissionUse, this))
{
permission.RegisterPermission(PermissionUse, this);
}
foreach (var validGrade in ValidGrades)
{
if (validGrade > BuildingGrade.Enum.Twigs)
{
permission.RegisterPermission(PermissionUpPrefix + (int)validGrade, this);
}
if (validGrade < BuildingGrade.Enum.TopTier)
{
permission.RegisterPermission(PermissionDownPrefx + (int)validGrade, this);
}
}
cmd.AddChatCommand(_configData.Chat.UpgradeCommand, this, nameof(CmdUpgrade));
cmd.AddChatCommand(_configData.Chat.DowngradeCommand, this, nameof(CmdDowngrade));
cmd.AddChatCommand(_configData.Chat.UpgradeAllCommand, this, nameof(CmdUpgradeAll));
cmd.AddChatCommand(_configData.Chat.DowngradeAllCommand, this, nameof(CmdDowngradeAll));
if (!_configData.Global.UseRaidBlocker)
{
Unsubscribe(nameof(OnRaidBlock));
}
if (!_configData.Global.UseCombatBlocker)
{
Unsubscribe(nameof(OnCombatBlock));
}
}
private void OnServerInitialized()
{
foreach (var entry in _configData.Categories)
{
var values = new HashSet<uint>();
foreach (var prefab in entry.Value)
{
var item = StringPool.Get(prefab);
if (item == 0u)
{
continue;
}
values.Add(item);
}
_categories.Add(entry.Key, values);
}
}
private void Unload()
{
_assistant.Unload();
_instance = null;
}
private void OnRaidBlock(BasePlayer player)
{
OnCombatBlock(player);
}
private void OnCombatBlock(BasePlayer player)
{
if (_assistant.IsRunning())
{
_assistant.BlockPlayer(player.userID);
}
}
#endregion Oxide Hooks
#region Chat Commands
private void CmdUpgrade(BasePlayer player, string command, string[] args)
{
HandleCommand(player, args, true, false);
}
private void CmdDowngrade(BasePlayer player, string command, string[] args)
{
HandleCommand(player, args, false, false);
}
private void CmdUpgradeAll(BasePlayer player, string command, string[] args)
{
HandleCommand(player, args, true, true);
}
private void CmdDowngradeAll(BasePlayer player, string command, string[] args)
{
HandleCommand(player, args, false, true);
}
#endregion Chat Commands
#region Methods
private void HandleCommand(BasePlayer player, string[] args, bool isUpgrade, bool isAll)
{
if (!permission.UserHasPermission(player.UserIDString, PermissionUse))
{
Print(player, Lang("NotAllowed", player.UserIDString));
return;
}
if (_assistant.IsRunning())
{
Print(player, Lang("AlreadyProcess", player.UserIDString));
return;
}
if (IsPlayerBlocked(player))
{
return;
}
HashSet<uint> filter = null;
var targetGrade = BuildingGrade.Enum.None;
if (args.Length > 0)
{
if (!_categories.TryGetValue(args[0], out filter))
{
if (!Enum.TryParse(args[0], true, out targetGrade) || !ValidGrades.Contains(targetGrade) || isUpgrade ? targetGrade <= BuildingGrade.Enum.Twigs : targetGrade >= BuildingGrade.Enum.TopTier)
{
Print(player, Lang("UnknownGrade", player.UserIDString));
return;
}
if (!HasGradePermission(player, targetGrade, isUpgrade))
{
Print(player, Lang("NotAllowed", player.UserIDString));
return;
}
if (args.Length > 1 && !_categories.TryGetValue(args[1], out filter))
{
Print(player, Lang("UnknownCategory", player.UserIDString, string.Join(", ", _configData.Categories.Keys)));
return;
}
}
}
else
{
if (!_assistant.FindPlayerGrantedGrades(player, isUpgrade))
{
Print(player, Lang("NotAllowed", player.UserIDString));
return;
}
}
var permissionSettings = GetPermissionSettings(player);
if (permissionSettings.Cooldown > 0f && !(_configData.Global.CooldownExclude && player.IsAdmin))
{
var timeLeft = _assistant.GetCooldownLeft(player.userID, permissionSettings.Cooldown);
if (timeLeft > 0f)
{
Print(player, Lang("OnCooldown", player.UserIDString, Mathf.CeilToInt(timeLeft)));
return;
}
}
var targetBuildingBlock = GetBuildingBlockLookingAt(player, permissionSettings);
if (targetBuildingBlock == null)
{
Print(player, Lang("NotLookingAt", player.UserIDString));
return;
}
var isAdmin = permission.UserHasPermission(player.UserIDString, PermissionAdmin);
if (!isAdmin && player.IsBuildingBlocked(targetBuildingBlock.WorldSpaceBounds()))
{
Print(player, Lang("BuildingBlocked", player.UserIDString));
return;
}
_assistant.Run(targetBuildingBlock, player, targetGrade, filter, permissionSettings, isUpgrade, isAll, isAdmin);
}
private PermissionSettings GetPermissionSettings(BasePlayer player)
{
var priority = 0;
PermissionSettings permissionSettings = null;
foreach (var entry in _configData.Permissions)
{
if (entry.Value.Priority >= priority && permission.UserHasPermission(player.UserIDString, entry.Key))
{
priority = entry.Value.Priority;
permissionSettings = entry.Value;
}
}
return permissionSettings ?? new PermissionSettings();
}
private bool HasGradePermission(BasePlayer player, BuildingGrade.Enum grade, bool isUpgrade)
{
return isUpgrade
? permission.UserHasPermission(player.UserIDString, PermissionUpAll) ||
permission.UserHasPermission(player.UserIDString, PermissionUpPrefix + (int)grade)
: permission.UserHasPermission(player.UserIDString, PermissionDownAll) ||
permission.UserHasPermission(player.UserIDString, PermissionDownPrefx + (int)grade);
}
#region AreFriends
private bool AreFriends(ulong playerId, ulong friendId)
{
if (!playerId.IsSteamId())
{
return false;
}
if (playerId == friendId)
{
return true;
}
if (_configData.Global.UseTeams && SameTeam(playerId, friendId))
{
return true;
}
if (_configData.Global.UseFriends && HasFriend(playerId, friendId))
{
return true;
}
if (_configData.Global.UseClans && SameClan(playerId, friendId))
{
return true;
}
return false;
}
private static bool SameTeam(ulong playerId, ulong friendId)
{
if (!RelationshipManager.TeamsEnabled())
{
return false;
}
var playerTeam = RelationshipManager.ServerInstance.FindPlayersTeam(playerId);
if (playerTeam == null)
{
return false;
}
var friendTeam = RelationshipManager.ServerInstance.FindPlayersTeam(friendId);
if (friendTeam == null)
{
return false;
}
return playerTeam == friendTeam;
}
private bool HasFriend(ulong playerId, ulong friendId)
{
if (Friends == null)
{
return false;
}
return (bool)Friends.Call("HasFriend", playerId, friendId);
}
private bool SameClan(ulong playerId, ulong friendId)
{
if (Clans == null)
{
return false;
}
//Clans
var isMember = Clans.Call("IsClanMember", playerId.ToString(), friendId.ToString());
if (isMember != null)
{
return (bool)isMember;
}
//Rust:IO Clans
var playerClan = Clans.Call("GetClanOf", playerId);
if (playerClan == null)
{
return false;
}
var friendClan = Clans.Call("GetClanOf", friendId);
if (friendClan == null)
{
return false;
}
return (string)playerClan == (string)friendClan;
}
#endregion AreFriends
#region PlayerIsBlocked
private bool IsPlayerBlocked(BasePlayer player)
{
if (NoEscape == null)
{
return false;
}
if (_configData.Global.UseRaidBlocker && IsRaidBlocked(player.UserIDString))
{
Print(player, Lang("RaidBlocked", player.UserIDString));
return true;
}
if (_configData.Global.UseCombatBlocker && IsCombatBlocked(player.UserIDString))
{
Print(player, Lang("CombatBlocked", player.UserIDString));
return true;
}
return false;
}
private bool IsRaidBlocked(string playerId)
{
return (bool)NoEscape.Call("IsRaidBlocked", playerId);
}
private bool IsCombatBlocked(string playerId)
{
return (bool)NoEscape.Call("IsCombatBlocked", playerId);
}
#endregion PlayerIsBlocked
#region RustTranslationAPI
private string GetItemTranslationByShortName(string language, string itemShortName)
{
return (string)RustTranslationAPI.Call("GetItemTranslationByShortName", language, itemShortName);
}
private string GetItemDisplayName(string language, ItemDefinition itemDefinition)
{
if (RustTranslationAPI != null)
{
var displayName = GetItemTranslationByShortName(language, itemDefinition.shortname);
if (!string.IsNullOrEmpty(displayName))
{
return displayName;
}
}
return itemDefinition.displayName.english;
}
#endregion RustTranslationAPI
#endregion Methods
#region Assistant
private class BuildingGradeAssistant
{
private BuildingGrades Plugin => _instance;
private Coroutine _changeGradeCoroutine;
private readonly Dictionary<ulong, bool> _tempFriends = new Dictionary<ulong, bool>();
private readonly HashSet<BuildingBlock> _allBuildingBlocks = new HashSet<BuildingBlock>();
private readonly List<BuildingGrade.Enum> _tempGrantedGrades = new List<BuildingGrade.Enum>();
private readonly List<Item> _collect = new List<Item>();
private readonly Hash<int, float> _takeOutItems = new Hash<int, float>();
private readonly Hash<ItemDefinition, int> _missingItems = new Hash<ItemDefinition, int>();
private HashSet<ulong> _blockedPlayers = new HashSet<ulong>();
private readonly Dictionary<ulong, float> _cooldowns = new Dictionary<ulong, float>();
private BuildingBlock _processingBuildingBlock;
internal bool IsRunning()
{
return _changeGradeCoroutine != null;
}
internal void Unload()
{
if (_changeGradeCoroutine != null)
{
ServerMgr.Instance.StopCoroutine(_changeGradeCoroutine);
}
}
internal void BlockPlayer(ulong playerId)
{
_blockedPlayers.Add(playerId);
}
internal bool IsProcessingBuildingBlock(BuildingBlock buildingBlock)
{
if (buildingBlock == null)
{
return false;
}
return _processingBuildingBlock == buildingBlock;
}
internal void Run(BuildingBlock sourceEntity, BasePlayer player, BuildingGrade.Enum targetGrade, HashSet<uint> filter, PermissionSettings permissionSettings, bool isUpgrade, bool isAll, bool isAdmin)
{
if (IsRunning())
{
return;
}
_changeGradeCoroutine = ServerMgr.Instance.StartCoroutine(StartChangeBuildingGrade(sourceEntity, player, targetGrade, filter, permissionSettings, isUpgrade, isAll, isAdmin));
}
internal float GetCooldownLeft(ulong playerId, float cooldown)
{
float lastUse;
if (_cooldowns.TryGetValue(playerId, out lastUse))
{
return cooldown - (Time.realtimeSinceStartup - lastUse);
}
return 0f;
}
internal bool FindPlayerGrantedGrades(BasePlayer player, bool isUpgrade)
{
var all = Plugin.permission.UserHasPermission(player.UserIDString, isUpgrade ? PermissionUpAll : PermissionDownAll);
if (all)
{
_tempGrantedGrades.AddRange(ValidGrades);
return _tempGrantedGrades.Count > 0;
}
foreach (var validGrade in ValidGrades)
{
if (isUpgrade)
{
if (validGrade > BuildingGrade.Enum.Twigs && Plugin.permission.UserHasPermission(player.UserIDString, PermissionUpPrefix + (int)validGrade))
{
_tempGrantedGrades.Add(validGrade);
}
}
else
{
if (validGrade < BuildingGrade.Enum.TopTier && Plugin.permission.UserHasPermission(player.UserIDString, PermissionDownPrefx + (int)validGrade))
{
_tempGrantedGrades.Add(validGrade);
}
}
}
return _tempGrantedGrades.Count > 0;
}
private IEnumerator StartChangeBuildingGrade(BuildingBlock sourceEntity, BasePlayer player, BuildingGrade.Enum targetGrade, HashSet<uint> filter, PermissionSettings permissionSettings, bool isUpgrade, bool isAll, bool isAdmin)
{
_blockedPlayers.Clear();
yield return GetAllBuildingBlocks(sourceEntity, filter, isAll);
//if (pay) {
// FindUpgradeCosts(targetGrade);
// if (!CanAffordUpgrade(player)) {
// Clear();
// yield break;
// }
// PayForUpgrade(costs, player);
//}
Plugin.Print(player, Plugin.Lang(isUpgrade ? "StartUpgrade" : "StartDowngrade", player.UserIDString));
var playerId = player.userID;
yield return isUpgrade ? UpgradeBuildingBlocks(player, targetGrade, permissionSettings, isAdmin) : DowngradeBuildingBlocks(player, targetGrade, permissionSettings, isAdmin);
_tempFriends.Clear();
_allBuildingBlocks.Clear();
_tempGrantedGrades.Clear();
_blockedPlayers.Clear();
_cooldowns[playerId] = Time.realtimeSinceStartup;
_processingBuildingBlock = null;
_changeGradeCoroutine = null;
}
private bool ShouldInterrupt(ulong playerId)
{
return _blockedPlayers.Contains(playerId);
}
#region Upgrade
private IEnumerator UpgradeBuildingBlocks(BasePlayer player, BuildingGrade.Enum targetGrade, PermissionSettings permissionSettings, bool isAdmin)
{
var pay = permissionSettings.Pay && !Plugin.permission.UserHasPermission(player.UserIDString, PermissionNoCost);
int current = 0, success = 0;
var autoGrade = targetGrade == BuildingGrade.Enum.None;
foreach (var buildingBlock in _allBuildingBlocks)
{
if (buildingBlock == null || buildingBlock.IsDestroyed)
{
continue;
}
if (ShouldInterrupt(player.userID))
{
break;
}
var grade = targetGrade;
if (CheckBuildingGrade(buildingBlock, true, ref grade))
{
if (!autoGrade || _tempGrantedGrades.Contains(grade))
{
_processingBuildingBlock = buildingBlock;
if (TryUpgradeToGrade(buildingBlock, player, grade, pay, isAdmin))
{
success++;
}
_processingBuildingBlock = null;
}
}
if (++current % Plugin._configData.Global.PerFrame == 0)
{
yield return CoroutineEx.waitForEndOfFrame;
}
}
foreach (var item in _collect)
{
_takeOutItems[item.info.itemid] += item.amount;
item.Remove();
}
foreach (var entry in _takeOutItems)
{
player.Command("note.inv " + entry.Key + " " + entry.Value * -1f);
}
if (player != null && player.IsConnected)
{
if (_missingItems.Count > 0)
{
var stringBuilder = Pool.Get<StringBuilder>();
stringBuilder.Clear();
var language = Plugin.lang.GetLanguage(player.UserIDString);
foreach (var entry in _missingItems)
{
stringBuilder.AppendLine(Plugin.Lang("MissingItemsFormat", player.UserIDString, Plugin.GetItemDisplayName(language, entry.Key), entry.Value));
}
var missingResources = stringBuilder.ToString();
stringBuilder.Clear();
Pool.FreeUnmanaged(ref stringBuilder);
Plugin.Print(player, success > 0 ? Plugin.Lang("UpgradeNotEnoughItemsSuccess", player.UserIDString, success, missingResources) : Plugin.Lang("UpgradeNotEnoughItems", player.UserIDString, missingResources));
}
else
{
Plugin.Print(player, success > 0 ? Plugin.Lang("FinishedUpgrade", player.UserIDString, success) : Plugin.Lang("NotUpgraded", player.UserIDString));
}
}
_collect.Clear();
_takeOutItems.Clear();
_missingItems.Clear();
}
private bool TryUpgradeToGrade(BuildingBlock buildingBlock, BasePlayer player, BuildingGrade.Enum targetGrade, bool pay, bool isAdmin)
{
if (!CanUpgrade(buildingBlock, player, targetGrade, pay, isAdmin))
{
return false;
}
var oldGrade = buildingBlock.grade;
SetBuildingBlockGrade(buildingBlock, targetGrade);
Interface.CallHook("OnStructureGradeUpdated", buildingBlock, player, oldGrade, targetGrade);
return true;
}
private bool CanUpgrade(BuildingBlock buildingBlock, BasePlayer player, BuildingGrade.Enum targetGrade, bool pay, bool isAdmin)
{
if (player == null || !player.CanInteract())
{
return false;
}
var constructionGrade = buildingBlock.blockDefinition.GetGrade(targetGrade, 0);
if (constructionGrade == null)
{
return false;
}
if (!isAdmin)
{
if (Interface.CallHook("OnStructureUpgrade", buildingBlock, player, targetGrade, 0) != null)
{
return false;
}
if (buildingBlock.SecondsSinceAttacked < 30f)
{
return false;
}
if (!buildingBlock.CanChangeToGrade(targetGrade, 0, player))
{
return false;
}
if (!HasAccess(player, buildingBlock))
{
return false;
}
}
if (pay)
{
//if (!buildingBlock.CanAffordUpgrade(targetGrade, player)) {
// return false;
//}
//buildingBlock.PayForUpgrade(constructionGrade, player);
if (!CanAffordUpgrade(buildingBlock, constructionGrade, player, targetGrade))
{
return false;
}
PayForUpgrade(buildingBlock, constructionGrade, player);
}
return true;
}
public bool CanAffordUpgrade(BuildingBlock buildingBlock, ConstructionGrade constructionGrade, BasePlayer player, BuildingGrade.Enum grade)
{
var obj = Interface.CallHook("CanAffordUpgrade", player, buildingBlock, grade);
if (obj is bool)
{
return (bool)obj;
}
var flag = true;
foreach (var item in constructionGrade.CostToBuild())
{
var missingAmount = item.amount - player.inventory.GetAmount(item.itemid);
if (missingAmount > 0f)
{
flag = false;
_missingItems[item.itemDef] += (int)missingAmount;
}
}
return flag;
}
public void PayForUpgrade(BuildingBlock buildingBlock, ConstructionGrade constructionGrade, BasePlayer player)
{
if (Interface.CallHook("OnPayForUpgrade", player, buildingBlock, constructionGrade) != null)
{
return;
}
foreach (var item in constructionGrade.CostToBuild())
{
player.inventory.Take(_collect, item.itemid, (int)item.amount);
//player.Command("note.inv " + item.itemid + " " + item.amount * -1f);
}
}
#endregion Upgrade
#region Downgrade
private IEnumerator DowngradeBuildingBlocks(BasePlayer player, BuildingGrade.Enum targetGrade, PermissionSettings permissionSettings, bool isAdmin)
{
int current = 0, success = 0;
var autoGrade = targetGrade == BuildingGrade.Enum.None;
foreach (var buildingBlock in _allBuildingBlocks)
{
if (buildingBlock == null || buildingBlock.IsDestroyed)
{
continue;
}
if (ShouldInterrupt(player.userID))
{
break;
}
var grade = targetGrade;
if (CheckBuildingGrade(buildingBlock, false, ref grade))
{
if (!autoGrade || _tempGrantedGrades.Contains(grade))
{
_processingBuildingBlock = buildingBlock;
if (TryDowngradeToGrade(buildingBlock, player, grade /*, permissionSettings.Refund*/, isAdmin: isAdmin))
{
success++;
}
_processingBuildingBlock = null;
}
}
if (current++ % Plugin._configData.Global.PerFrame == 0)
{
yield return CoroutineEx.waitForEndOfFrame;
}
}
if (player != null && player.IsConnected)
{
Plugin.Print(player, success > 0 ? Plugin.Lang("FinishedDowngrade", player.UserIDString, success) : Plugin.Lang("NotDowngraded", player.UserIDString));
}
}
private bool TryDowngradeToGrade(BuildingBlock buildingBlock, BasePlayer player, BuildingGrade.Enum targetGrade, bool refund = false, bool isAdmin = false)
{
if (!CanDowngrade(buildingBlock, player, targetGrade, refund, isAdmin))
{
return false;
}
//if (refund)
//{
// foreach (var itemAmount in buildingBlock.currentGrade.costToBuild)
// {
// var item = ItemManager.CreateByItemID(itemAmount.itemid, (int)itemAmount.amount);
// player.GiveItem(item);
// }
//}
var oldGrade = buildingBlock.grade;
SetBuildingBlockGrade(buildingBlock, targetGrade);
Interface.CallHook("OnStructureGradeUpdated", buildingBlock, player, oldGrade, targetGrade);
return true;
}
private bool CanDowngrade(BuildingBlock buildingBlock, BasePlayer player, BuildingGrade.Enum targetGrade, bool refund, bool isAdmin)
{
if (player == null || !player.CanInteract())
{
return false;
}
var constructionGrade = buildingBlock.blockDefinition.GetGrade(targetGrade, 0);
if (constructionGrade == null)
{
return false;
}
if (!isAdmin)
{
if (Interface.CallHook("OnStructureUpgrade", buildingBlock, player, targetGrade, 0) != null)
{
return false;
}
if (buildingBlock.SecondsSinceAttacked < 30f)
{
return false;
}
var obj = Interface.CallHook("CanChangeGrade", player, buildingBlock, targetGrade);
if (obj is bool)
{
return (bool)obj;
}
if (player.IsBuildingBlocked(buildingBlock.transform.position, buildingBlock.transform.rotation, buildingBlock.bounds))
{
return false;
}
if (buildingBlock.IsUpgradeBlocked())
{
return false;
}
if (!HasAccess(player, buildingBlock))
{
return false;
}
}
return true;
}
#endregion Downgrade
#region Methods
private bool HasAccess(BasePlayer player, BuildingBlock buildingBlock)
{
bool flag;
if (_tempFriends.TryGetValue(buildingBlock.OwnerID, out flag))
{
return flag;
}
var areFriends = Plugin.AreFriends(buildingBlock.OwnerID, player.userID);
_tempFriends.Add(buildingBlock.OwnerID, areFriends);
return areFriends;
}
private IEnumerator GetAllBuildingBlocks(BuildingBlock sourceEntity, HashSet<uint> filter, bool isAll)
{
Func<BuildingBlock, bool> func = x => filter == null || filter.Contains(x.prefabID);
if (isAll)
{
yield return GetNearbyEntities(sourceEntity, _allBuildingBlocks, Layers.Mask.Construction, func);
}
else
{
var building = sourceEntity.GetBuilding();
if (building != null)
{
foreach (var buildingBlock in building.buildingBlocks)
{
if (func(buildingBlock))
{
_allBuildingBlocks.Add(buildingBlock);
}
}
}
}
}
//#region TryPay
//private readonly Hash<int, int> costs = new Hash<int, int>();
//private readonly Hash<string , int> missingDictionary = new Hash<string, int>();
//private readonly HashSet<BuildingBlock> toRemove = new HashSet<BuildingBlock>();
//private bool TryPay(BasePlayer player, BuildingGrade.Enum targetGrade, out string missingResources) {
// FindUpgradeCosts(player, targetGrade);
// if (!CanPay(player, out missingResources)) {
// return false;
// }
// var collect = Pool.GetList<Item>();
// foreach (var entry in costs) {
// player.inventory.Take(collect, entry.Key, entry.Value);
// player.Command(string.Concat("note.inv ", entry.Key, " ", entry.Value * -1f));
// }
// foreach (var item in collect) {
// item.Remove();
// }
// Pool.FreeList(ref collect);
// missingResources = null;
// return true;
//}
//private bool CanPay(BasePlayer player, out string missingResources) {
// foreach (var entry in costs) {
// if (entry.Value <= 0) continue;
// var missingAmount = entry.Value - player.inventory.GetAmount(entry.Key);
// if (missingAmount <= 0) continue;
// var displayName= ItemManager.FindItemDefinition(entry.Key)?.displayName.english;
// if (string.IsNullOrEmpty(displayName)) displayName = entry.Key.ToString();
// missingDictionary[displayName] += missingAmount;
// }
// if (missingDictionary.Count > 0) {
// StringBuilder stringBuilder = Pool.Get<StringBuilder>();
// stringBuilder.Clear();
// foreach (var entry in missingDictionary) {
// stringBuilder.AppendLine(Lang("MissingResourceFormat", player.UserIDString, entry.Key, entry.Value));
// }
// missingResources = stringBuilder.ToString();
// stringBuilder.Clear();
// missingDictionary.Clear();
// Pool.Free(ref stringBuilder);
// return false;
// }
// missingResources = null;
// return true;
//}
//private void FindUpgradeCosts(BasePlayer player, BuildingGrade.Enum targetGrade) {
// var autoGrade = targetGrade == BuildingGrade.Enum.None;
// foreach (var buildingBlock in allBuildingBlocks) {
// if (buildingBlock == null || buildingBlock.IsDestroyed) {
// toRemove.Add(buildingBlock);
// continue;
// }
// BuildingGrade.Enum grade = targetGrade;
// if (CheckBuildingGrade(buildingBlock, false, ref grade)) {
// if (!autoGrade || tempGrantedGrades.Contains(grade)) {
// if (!CanUpgrade(buildingBlock, player, grade)) {
// toRemove.Add(buildingBlock);
// continue;
// }
// }
// }
// var costToBuild = buildingBlock.blockDefinition.grades[(int)grade].costToBuild;
// foreach (var itemAmount in costToBuild) {
// costs[itemAmount.itemid] += (int)itemAmount.amount;
// }
// }
// foreach (var buildingBlock in toRemove) {
// allBuildingBlocks.Remove(buildingBlock);
// }
// toRemove.Clear();
//}
//#endregion TryPay
#endregion Methods
}
#endregion Assistant
#region Helpers
private static BuildingBlock GetBuildingBlockLookingAt(BasePlayer player, PermissionSettings permissionSettings)
{
RaycastHit raycastHit;
var flag = Physics.Raycast(player.eyes.HeadRay(), out raycastHit, permissionSettings.Distance, Layers.Mask.Construction);
return flag ? raycastHit.GetEntity() as BuildingBlock : null;
}
private static bool CheckBuildingGrade(BuildingBlock buildingBlock, bool isUpgrade, ref BuildingGrade.Enum targetGrade)
{
if (buildingBlock.blockDefinition == null)
{
return false;
}
var grades = buildingBlock.blockDefinition.grades;
if (grades == null)
{
return false;
}
var grade = buildingBlock.grade;
if (IsValidGrade(grades, (int)targetGrade))
{
return isUpgrade ? grade < targetGrade : grade > targetGrade;
}
targetGrade = grade + (isUpgrade ? 1 : -1);
return IsValidGrade(grades, (int)targetGrade);
}
private static bool IsValidGrade(ConstructionGrade[] grades, int targetGrade)
{
return targetGrade >= 0 && targetGrade < grades.Length && grades[targetGrade] != null;
}
private static void SetBuildingBlockGrade(BuildingBlock buildingBlock, BuildingGrade.Enum targetGrade)
{
buildingBlock.skinID = 0; // to avoid upgrade/downgrade to twig when using building skin
buildingBlock.SetGrade(targetGrade);
buildingBlock.SetHealthToMax();
buildingBlock.StartBeingRotatable();
buildingBlock.SendNetworkUpdate();
buildingBlock.UpdateSkin();
buildingBlock.ResetUpkeepTime();
buildingBlock.UpdateSurroundingEntities();
BuildingManager.server.GetBuilding(buildingBlock.buildingID)?.Dirty();
if (targetGrade > BuildingGrade.Enum.Twigs)
{
Effect.server.Run("assets/bundled/prefabs/fx/build/promote_" + targetGrade.ToString().ToLower() + ".prefab", buildingBlock, 0u, Vector3.zero, Vector3.zero);
}
}
private static IEnumerator GetNearbyEntities<T>(T sourceEntity, HashSet<T> entities, int layers, Func<T, bool> filter = null) where T : BaseEntity
{
var current = 0;
var checkFrom = Pool.Get<Queue<Vector3>>();
var nearbyEntities = Pool.Get<List<T>>();
checkFrom.Enqueue(sourceEntity.transform.position);
while (checkFrom.Count > 0)
{
nearbyEntities.Clear();
var position = checkFrom.Dequeue();
Vis.Entities(position, 3f, nearbyEntities, layers);
for (var i = 0; i < nearbyEntities.Count; i++)
{
var entity = nearbyEntities[i];
if (filter != null && !filter(entity))
{
continue;
}
if (!entities.Add(entity))
{
continue;
}
checkFrom.Enqueue(entity.transform.position);
}
if (++current % _instance._configData.Global.PerFrame == 0)
{
yield return CoroutineEx.waitForEndOfFrame;
}
}
Pool.FreeUnmanaged(ref checkFrom);
Pool.FreeUnmanaged(ref nearbyEntities);
}
#endregion Helpers
#region API
private bool IsProcessingBuildingBlock(BuildingBlock buildingBlock)
{
return _assistant.IsProcessingBuildingBlock(buildingBlock);
}
#endregion
#region ConfigurationFile
private ConfigData _configData;
private class ConfigData
{
[JsonProperty(PropertyName = "Settings")]
public GlobalSettings Global { get; set; } = new GlobalSettings();
[JsonProperty(PropertyName = "Chat Settings")]
public ChatSettings Chat { get; set; } = new ChatSettings();
[JsonProperty(PropertyName = "Permission Settings")]
public Dictionary<string, PermissionSettings> Permissions { get; set; } = new Dictionary<string, PermissionSettings>
{
[PermissionUse] = new PermissionSettings
{
Priority = 0,
Pay = true,
//Refund = false,
Cooldown = 60,
Distance = 10
}
};
[JsonProperty(PropertyName = "Building Block Categories", ObjectCreationHandling = ObjectCreationHandling.Replace)]
public Dictionary<string, HashSet<string>> Categories { get; set; } = new Dictionary<string, HashSet<string>>
{
["foundation"] = new HashSet<string>
{
"assets/prefabs/building core/foundation/foundation.prefab",
"assets/prefabs/building core/foundation.steps/foundation.steps.prefab",
"assets/prefabs/building core/foundation.triangle/foundation.triangle.prefab"
},
["wall"] = new HashSet<string>
{
"assets/prefabs/building core/wall/wall.prefab",
"assets/prefabs/building core/wall.doorway/wall.doorway.prefab",
"assets/prefabs/building core/wall.frame/wall.frame.prefab",
"assets/prefabs/building core/wall.half/wall.half.prefab",
"assets/prefabs/building core/wall.low/wall.low.prefab",
"assets/prefabs/building core/wall.window/wall.window.prefab"
},
["floor"] = new HashSet<string>
{
"assets/prefabs/building core/floor/floor.prefab",
"assets/prefabs/building core/floor.frame/floor.frame.prefab",
"assets/prefabs/building core/floor.triangle/floor.triangle.prefab",
"assets/prefabs/building core/floor.triangle.frame/floor.triangle.frame.prefab"
},
["stair"] = new HashSet<string>
{
"assets/prefabs/building core/stairs.l/block.stair.lshape.prefab",
"assets/prefabs/building core/stairs.spiral/block.stair.spiral.prefab",
"assets/prefabs/building core/stairs.spiral.triangle/block.stair.spiral.triangle.prefab",
"assets/prefabs/building core/stairs.u/block.stair.ushape.prefab"
},
["roof"] = new HashSet<string>
{
"assets/prefabs/building core/roof/roof.prefab",
"assets/prefabs/building core/roof.triangle/roof.triangle.prefab"
},
["ramp"] = new HashSet<string>
{
"assets/prefabs/building core/ramp/ramp.prefab"
}
};
}
private class GlobalSettings
{
[JsonProperty(PropertyName = "Use Teams")]
public bool UseTeams { get; set; }
[JsonProperty(PropertyName = "Use Clans")]
public bool UseClans { get; set; } = true;
[JsonProperty(PropertyName = "Use Friends")]
public bool UseFriends { get; set; } = true;
[JsonProperty(PropertyName = "Use Raid Blocker (Need NoEscape Plugin)")]
public bool UseRaidBlocker { get; set; }
[JsonProperty(PropertyName = "Use Combat Blocker (Need NoEscape Plugin)")]
public bool UseCombatBlocker { get; set; }
[JsonProperty(PropertyName = "Cooldown Exclude Admins")]
public bool CooldownExclude { get; set; } = true;
[JsonProperty(PropertyName = "Upgrade/Downgrade Per Frame")]
public int PerFrame { get; set; } = 10;
}
private class ChatSettings
{
[JsonProperty(PropertyName = "Upgrade Chat Command")]
public string UpgradeCommand { get; set; } = "up";
[JsonProperty(PropertyName = "Downgrade Chat Command")]
public string DowngradeCommand { get; set; } = "down";
[JsonProperty(PropertyName = "Upgrade All Chat Command")]
public string UpgradeAllCommand { get; set; } = "upall";
[JsonProperty(PropertyName = "Downgrade All Chat Command")]
public string DowngradeAllCommand { get; set; } = "downall";
[JsonProperty(PropertyName = "Chat Prefix")]
public string Prefix { get; set; } = "<color=#00FFFF>[BuildingGrades]</color>: ";
[JsonProperty(PropertyName = "Chat SteamID Icon")]
public ulong SteamIdIcon { get; set; } = 0;
}
public class PermissionSettings
{
[JsonProperty(PropertyName = "Priority")]
public int Priority { get; set; }
[JsonProperty(PropertyName = "Distance")]
public float Distance { get; set; } = 4f;
[JsonProperty(PropertyName = "Cooldown")]
public float Cooldown { get; set; } = 60f;
[JsonProperty(PropertyName = "Pay")]
public bool Pay { get; set; } = true;
// [JsonProperty(PropertyName = "Refund")]
// public bool Refund { get; set; }
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
_configData = Config.ReadObject<ConfigData>();
if (_configData == null)
{
LoadDefaultConfig();
}
}
catch (Exception ex)
{
PrintError($"The configuration file is corrupted. \n{ex}");
LoadDefaultConfig();
}
SaveConfig();
}
protected override void LoadDefaultConfig()
{
PrintWarning("Creating a new configuration file");
_configData = new ConfigData();
}
protected override void SaveConfig()
{
Config.WriteObject(_configData);
}
#endregion ConfigurationFile
#region LanguageFile
private void Print(BasePlayer player, string message)
{
Player.Message(player, message, _configData.Chat.Prefix, _configData.Chat.SteamIdIcon);
}
private string Lang(string key, string id = null, params object[] args)
{
try
{
return string.Format(lang.GetMessage(key, this, id), args);
}
catch (Exception)
{
PrintError($"Error in the language formatting of '{key}'. (userid: {id}. lang: {lang.GetLanguage(id)}. args: {string.Join(" ,", args)})");
throw;
}
}
protected override void LoadDefaultMessages()
{
lang.RegisterMessages(new Dictionary<string, string>
{
["NotAllowed"] = "<color=#FF1919>You do not have permission to use this command.</color>",
["UnknownGrade"] = "<color=#FF1919>Unknown grade.</color>",
["UnknownCategory"] = "<color=#FF1919>Unknown category.</color> Available categories: <color=#009EFF>{0}</color>",
["NotLookingAt"] = "<color=#FF1919>You are not looking at a building block.</color>",
["RaidBlocked"] = "<color=#FF1919>You may not do that while raid blocked.</color>.",
["CombatBlocked"] = "<color=#FF1919>You may not do that while combat blocked.</color>.",
["OnCooldown"] = "You must wait <color=#FF1919>{0}</color> seconds before you can use this command.",
["BuildingBlocked"] = "You can't use this command if you don't have the building privileges.",
["MissingItemsFormat"] = "* <color=#FF1919>{0}</color> x{1}",
["StartUpgrade"] = "Start running upgrade, please wait.",
["StartDowngrade"] = "Start running downgrade, please wait.",
["AlreadyProcess"] = "There is already a process already running, please wait.",
["UpgradeNotEnoughItems"] = "None of the buildings were upgraded. You are missing: \n{0}",
["UpgradeNotEnoughItemsSuccess"] = "<color=#FF1919>{0}</color> building blocks were upgraded. Some buildings cannot be upgraded, you are missing: \n{1}",
["NotUpgraded"] = "None of the buildings were upgraded.",
["NotDowngraded"] = "None of the buildings were downgraded.",
["FinishedUpgrade"] = "<color=#FF1919>{0}</color> building blocks were upgraded.",
["FinishedDowngrade"] = "<color=#FF1919>{0}</color> building blocks were downgraded."
}, this);
lang.RegisterMessages(new Dictionary<string, string>
{
["NotAllowed"] = "<color=#FF1919>您没有使用该命令的权限</color>",
["UnknownGrade"] = "<color=#FF1919>未知的建筑等级</color>",
["UnknownCategory"] = "<color=#FF1919>未知的建筑类型</color> 可用的建筑类型: <color=#009EFF>{0}</color>",
["NotLookingAt"] = "<color=#FF1919>您需要看着一个建筑</color>",
["RaidBlocked"] = "<color=#FF1919>您被突袭阻止了,不能使用该命令</color>",
["CombatBlocked"] = "<color=#FF1919>您被战斗阻止了,不能使用该命令</color>",
["OnCooldown"] = "您必须等待 <color=#FF1919>{0}</color> 秒后才能使用该命令",
["BuildingBlocked"] = "您不能在没有领地权的地方使用该命令",
["MissingItemsFormat"] = "* <color=#FF1919>{0}</color> x{1}",
["StartUpgrade"] = "开始运行升级您的建筑,请稍等片刻",
["StartDowngrade"] = "开始运行降级您的建筑,请稍等片刻",
["AlreadyProcess"] = "已经有一个线程正在运行,请稍等片刻",
["UpgradeNotEnoughItems"] = "没有可以升级的建筑。您还需要: \n{0}",
["UpgradeNotEnoughItemsSuccess"] = "<color=#FF1919>{0}</color> 个建筑成功升级了。有些建筑无法升级,您还需要: \n{1}",
["NotUpgraded"] = "没有可以升级的建筑",
["NotDowngraded"] = "没有可以降级的建筑",
["FinishedUpgrade"] = "<color=#FF1919>{0}</color> 个建筑成功升级了",
["FinishedDowngrade"] = "<color=#FF1919>{0}</color> 个建筑成功降级了"
}, this, "zh-CN");
lang.RegisterMessages(new Dictionary<string, string>
{
["NotAllowed"] = "<color=#FF1919>Ви не маєте дозволу на використання цієї команди.</color>",
["UnknownGrade"] = "<color=#FF1919>Невідоме поліпшення.</color>",
["UnknownCategory"] = "<color=#FF1919>Невідома категорія.</color> Доступні категорії: <color=#009EFF>{0}</color>",
["NotLookingAt"] = "<color=#FF1919>Ви не дивитеся на будівельний блок.</color>",
["RaidBlocked"] = "<color=#FF1919>Ви не можете цього робити, поки рейд заблоковано.</color>.",
["CombatBlocked"] = "<color=#FF1919>Ви не можете цього робити, коли бій заблоковано.</color>.",
["OnCooldown"] = "Ви повинні почекати <color=#FF1919>{0}</color> секунд, перш ніж ви зможете використовувати цю команду.",
["BuildingBlocked"] = "Ви не можете використовувати цю команду, якщо у вас немає привілеїв будівництва.",
["MissingItemsFormat"] = "* <color=#FF1919>{0}</color> x{1}",
["StartUpgrade"] = "Почніть запуск оновлення, зачекайте.",
["StartDowngrade"] = "Почніть роботу з пониженням, зачекайте.",
["AlreadyProcess"] = "Процес уже запущений, зачекайте.",
["UpgradeNotEnoughItems"] = "Жодна з будівель не була модернізована. Ви пропали: \n{0}",
["UpgradeNotEnoughItemsSuccess"] = "<color=#FF1919>{0}</color> будівельні блоки були модернізовані. Деякі будівлі неможливо покращити, ви пропали: \n{1}",
["NotUpgraded"] = "Жодна з будівель не була модернізована.",
["NotDowngraded"] = "Жодна з будівель не була понижена.",
["FinishedUpgrade"] = "<color=#FF1919>{0}</color> будівельні блоки були модернізовані.",
["FinishedDowngrade"] = "<color=#FF1919>{0}</color> будівельні блоки були знижені."
}, this, "uk");
}
#endregion LanguageFile
}
}
Step 2: Modify the LoadConfig and SaveConfig Methods
Update your configuration methods to handle the new ConfigData structure. Here's how you can rewrite these methods:
Refactor the rest of your plugin to use _configData instead of accessing configuration variables directly:
csharp
private void Init()
{
if (_configData.AutoSpawn.EnableAutoSpawn)
{
StartAutoSpawnTimer();
}
}
private void StartAutoSpawnTimer()
{
_autoSpawnTimer = timer.Every(_configData.AutoSpawn.AutoSpawnIntervalMinutes * 60f, AutoSpawnDungeon);
}
// Update other relevant parts of your code similarly by accessing through _configData
Step 4: Ensure Consistency
Ensure that any method requiring configuration data accesses it through the _configData object.
Validate that the structured data facilitates any necessary updates, modifications, or reads from the file system.
Check that all default values you want loaded in absence of custom configurations are properly initialized in the class declarations.
The modified configuration setup introduces better organization by grouping settings logically, making it easier to manage, expand, and maintain.
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Oxide.Core;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Dungeons", "Marte6", "1.0.0")]
public class Dungeons : RustPlugin
{
private ConfigData _configData;
private Timer _autoSpawnTimer;
private const string DungeonPrefabPath = "assets/prefabs/missions/portal/xmasportalentry.prefab";
private XmasDungeon _currentDungeon;
private List<BaseEntity> _spawnedEntities = new List<BaseEntity>();
#region Initialization and Configuration
private void Init()
{
LoadConfig();
if (_configData.AutoSpawn.EnableAutoSpawn)
{
StartAutoSpawnTimer();
}
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
_configData = Config.ReadObject<ConfigData>();
if (_configData == null)
{
LoadDefaultConfig();
}
}
catch (Exception ex)
{
PrintError($"The configuration file is corrupted. \n{ex}");
LoadDefaultConfig();
}
SaveConfig();
}
protected override void LoadDefaultConfig()
{
PrintWarning("Creating a new configuration file");
_configData = new ConfigData();
}
protected override void SaveConfig()
{
Config.WriteObject(_configData, true);
}
private class ConfigData
{
[JsonProperty(PropertyName = "Version")]
public string Version { get; set; } = "1.0.0";
[JsonProperty(PropertyName = "Auto Spawn Settings")]
public AutoSpawnSettings AutoSpawn { get; set; } = new AutoSpawnSettings();
[JsonProperty(PropertyName = "NPC Spawn Configs")]
public List<NpcSpawnConfig> NpcSpawnConfigs { get; set; } = new List<NpcSpawnConfig>
{
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/tunneldweller/npc_tunneldweller.prefab", EntitysPerCell = 2 },
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_heavy.prefab", EntitysPerCell = 2 },
};
[JsonProperty(PropertyName = "Auto Turret Configs")]
public List<AutoTurretConfig> AutoTurretConfigs { get; set; } = new List<AutoTurretConfig>
{
new AutoTurretConfig { PrefabName = "assets/prefabs/npc/autoturret/autoturret_deployed.prefab", EntitysPerCell = 2 },
};
}
private class AutoSpawnSettings
{
[JsonProperty(PropertyName = "Enable Auto Spawn")]
public bool EnableAutoSpawn { get; set; } = true;
[JsonProperty(PropertyName = "Auto Spawn Interval Minutes")]
public float AutoSpawnIntervalMinutes { get; set; } = 30f;
[JsonProperty(PropertyName = "Min Spawn Distance")]
public float MinSpawnDistance { get; set; } = 6f;
[JsonProperty(PropertyName = "Max Spawn Distance")]
public float MaxSpawnDistance { get; set; } = 20f;
[JsonProperty(PropertyName = "Max Spawn Attempts")]
public int MaxSpawnAttempts { get; set; } = 5;
}
private class NpcSpawnConfig
{
[JsonProperty("Prefab")]
public string PrefabName { get; set; }
[JsonProperty("Entitys Per Cell")]
public int EntitysPerCell { get; set; }
}
private class AutoTurretConfig
{
[JsonProperty("Prefab")]
public string PrefabName { get; set; }
[JsonProperty("Entitys Per Cell")]
public int EntitysPerCell { get; set; }
}
#endregion
#region Dungeon Management
[ChatCommand("dun")]
private void CreateDungeonCommand(BasePlayer player, string command, string[] args)
{
RemoveExistingDungeon();
if (TryFindDungeonSpawnPoint(player.transform.position, out var position, out var rotation))
{
_currentDungeon = CreateDungeon(position, rotation);
player.ChatMessage("Christmas Dungeon created!");
}
else
{
player.ChatMessage("Could not find a suitable location for the Christmas Dungeon.");
}
}
private void StartAutoSpawnTimer()
{
_autoSpawnTimer = timer.Every(_configData.AutoSpawn.AutoSpawnIntervalMinutes * 60f, AutoSpawnDungeon);
}
private void AutoSpawnDungeon()
{
RemoveExistingDungeon();
for (var attempt = 0; attempt < _configData.AutoSpawn.MaxSpawnAttempts; attempt++)
{
foreach (var player in BasePlayer.activePlayerList)
{
if (!TryFindDungeonSpawnPoint(player.transform.position, out var position, out var rotation))
continue;
_currentDungeon = CreateDungeon(position, rotation);
NotifyPlayersOfDungeonLocation(position);
return;
}
}
Puts("Failed to auto-spawn dungeon after max attempts.");
}
private void NotifyPlayersOfDungeonLocation(Vector3 position)
{
foreach (var player in BasePlayer.activePlayerList)
{
player.ChatMessage($"A Christmas Dungeon has appeared at coordinates: ({position.x:F0}, {position.z:F0})!");
}
}
private XmasDungeon CreateDungeon(Vector3 position, Quaternion rotation)
{
var entity = GameManager.server.CreateEntity(DungeonPrefabPath, position, rotation) as XmasDungeon;
if (entity == null)
{
Puts("Failed to create dungeon entity.");
return null;
}
entity.Spawn();
IntegrateEntitiesIntoDungeon(entity);
return entity;
}
private void IntegrateEntitiesIntoDungeon(XmasDungeon dungeon)
{
var proceduralDungeon = dungeon.dungeonInstance.Get(serverside: true);
if (proceduralDungeon == null)
{
Puts("Failed to get procedural dungeon instance.");
return;
}
int cellCount = proceduralDungeon.spawnedCells.Count;
if (cellCount == 0)
{
Puts("No cells found in dungeon for entities.");
return;
}
var allSpawnEntries = new List<SpawnGroup.SpawnEntry>();
foreach (var config in _configData.NpcSpawnConfigs)
{
if (GameManifest.pathToGuid.TryGetValue(config.PrefabName, out var guid))
{
for (int i = 0; i < config.EntitysPerCell; i++)
{
allSpawnEntries.Add(new SpawnGroup.SpawnEntry
{
prefab = new GameObjectRef { guid = guid },
weight = 1,
mobile = true,
});
}
}
}
foreach (var config in _configData.AutoTurretConfigs)
{
if (GameManifest.pathToGuid.TryGetValue(config.PrefabName, out var guid))
{
for (int i = 0; i < config.EntitysPerCell; i++)
{
allSpawnEntries.Add(new SpawnGroup.SpawnEntry
{
prefab = new GameObjectRef { guid = guid },
weight = 1,
mobile = false,
});
}
}
}
foreach (var cell in proceduralDungeon.spawnedCells)
{
SetupSpawnGroup(cell, allSpawnEntries);
}
}
private void SetupSpawnGroup(ProceduralDungeonCell cell, List<SpawnGroup.SpawnEntry> allSpawnEntries)
{
var spawnGroup = cell.gameObject.AddComponent<SpawnGroup>();
spawnGroup.prefabs = allSpawnEntries;
spawnGroup.maxPopulation = 100;
spawnGroup.numToSpawnPerTickMin = 0;
spawnGroup.numToSpawnPerTickMax = 0;
spawnGroup.fillOnSpawn = true;
spawnGroup.wantsInitialSpawn = true;
spawnGroup.SpawnInitial();
foreach (var instance in spawnGroup.spawnInstances)
{
var entity = instance.GetComponent<BaseEntity>();
if (entity != null && !_spawnedEntities.Contains(entity))
{
_spawnedEntities.Add(entity);
Puts($"Entity {entity.ShortPrefabName} spawned at {entity.transform.position}");
if (entity is AutoTurret autoTurret)
{
ConfigureAutoTurret(autoTurret);
}
}
}
}
private void ConfigureAutoTurret(AutoTurret autoTurret)
{
autoTurret.health = 1000f;
var weapon = ItemManager.CreateByName("rifle.ak");
weapon?.MoveToContainer(autoTurret.inventory, 0);
var ammo = ItemManager.CreateByName("ammo.rifle", 128);
ammo.MoveToContainer(autoTurret.inventory);
autoTurret.UpdateTotalAmmo();
autoTurret.EnsureReloaded();
autoTurret.InitializeHealth(1000f, 1000f);
autoTurret.SetPeacekeepermode(false);
}
private Vector3 GetRandomPositionNearby(Vector3 center, float minRadius, float maxRadius)
{
Vector2 randomCircle = UnityEngine.Random.insideUnitCircle * UnityEngine.Random.Range(minRadius, maxRadius);
return new Vector3(center.x + randomCircle.x, center.y, center.z + randomCircle.y);
}
#endregion
#region Utility and Cleanup
private bool TryFindDungeonSpawnPoint(Vector3 center, out Vector3 suitablePosition, out Quaternion suitableRotation)
{
suitablePosition = Vector3.zero;
suitableRotation = Quaternion.identity;
for (var attempt = 0; attempt < _configData.AutoSpawn.MaxSpawnAttempts; attempt++)
{
var potentialPosition = TerrainUtilities.GetRandomPosition(center, _configData.AutoSpawn.MinSpawnDistance, _configData.AutoSpawn.MaxSpawnDistance);
if (!TerrainUtilities.TryGetTerrainInfo(potentialPosition, 5f, out var hitInfo) || !IsPositionSuitable(potentialPosition))
{
continue;
}
suitablePosition = hitInfo.point;
suitableRotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
return true;
}
return false;
}
private bool IsPositionSuitable(Vector3 position)
{
return !TerrainUtilities.ContainsRock(position, 2f)
&& !TerrainUtilities.IsInRadTown(position)
&& !TerrainUtilities.ContainsEntitiesNearby(position, 4f, TerrainUtilities.EntityLayerMask)
&& !TerrainUtilities.IsInWater(position)
&& !TerrainUtilities.IsOnRoadOrRail(position)
&& !TerrainUtilities.IsInNoBuildZone(position, 2f);
}
private void RemoveExistingDungeon()
{
if (_currentDungeon != null)
{
foreach (var entity in _spawnedEntities)
{
if (entity != null && !entity.IsDestroyed)
{
entity.Kill(BaseNetworkable.DestroyMode.None);
}
}
_spawnedEntities.Clear();
_currentDungeon.Kill(BaseNetworkable.DestroyMode.None);
_currentDungeon = null;
}
}
private void Unload()
{
RemoveExistingDungeon();
_autoSpawnTimer?.Destroy();
}
#endregion
}
public static class TerrainUtilities
{
public const int EntityLayerMask = Layers.Mask.Deployed | Layers.Mask.Construction;
public const int GroundLayerMask = Layers.Mask.Terrain | Layers.Mask.World | Layers.Mask.Default;
public static bool OnTopology(Vector3 position, TerrainTopology.Enum topology)
{
return (TerrainMeta.TopologyMap.GetTopology(position) & (int)topology) != 0;
}
public static bool IsOnRoadOrRail(Vector3 position)
{
return OnTopology(position, TerrainTopology.Enum.Road | TerrainTopology.Enum.Roadside | TerrainTopology.Enum.Rail | TerrainTopology.Enum.Railside);
}
public static bool IsInNoBuildZone(Vector3 position, float radius)
{
return Physics.CheckSphere(position, radius, Layers.Mask.Prevent_Building);
}
public static bool IsInWater(Vector3 position)
{
return WaterLevel.Test(position, false, true);
}
public static bool ContainsRock(Vector3 position, float radius)
{
var colliders = Pool.GetList<Collider>();
Vis.Colliders(position, radius, colliders, GroundLayerMask, QueryTriggerInteraction.Ignore);
bool result = colliders.Exists(collider =>
collider.name.Contains("rock", StringComparison.OrdinalIgnoreCase)
|| collider.name.Contains("cliff", StringComparison.OrdinalIgnoreCase)
|| collider.name.Contains("formation", StringComparison.OrdinalIgnoreCase)
);
Pool.FreeList(ref colliders);
return result;
}
public static bool IsInRadTown(Vector3 position)
{
foreach (var monumentInfo in TerrainMeta.Path.Monuments)
{
if (monumentInfo.IsInBounds(position) && monumentInfo.shouldDisplayOnMap)
{
return true;
}
}
return OnTopology(position, TerrainTopology.Enum.Monument);
}
public static bool ContainsEntitiesNearby(Vector3 position, float radius, int mask)
{
var hitColliders = Pool.GetList<Collider>();
GamePhysics.OverlapSphere(position, radius, hitColliders, mask, QueryTriggerInteraction.Ignore);
bool result = hitColliders.Exists(collider => collider.gameObject.ToBaseEntity() != null);
Pool.FreeList(ref hitColliders);
return result;
}
public static bool TryGetTerrainInfo(Vector3 startPosition, float range, out RaycastHit hitInfo)
{
return Physics.Linecast(startPosition + new Vector3(0f, range, 0f), startPosition - new Vector3(0f, range, 0f), out hitInfo, GroundLayerMask);
}
public static Vector3 GetRandomPosition(Vector3 center, float minRadius, float maxRadius)
{
Vector3 randomDirection = UnityEngine.Random.onUnitSphere;
randomDirection.y = 0;
float randomDistance = UnityEngine.Random.Range(minRadius, maxRadius);
Vector3 randomPosition = center + randomDirection * randomDistance;
randomPosition.y = TerrainMeta.HeightMap.GetHeight(randomPosition);
return randomPosition;
}
}
}
Key Changes
ConfigData Class: A new class to encapsulate all configuration settings.
Auto Spawn Settings: Grouped related settings under AutoSpawnSettings.
Loading and Saving Configuration: Updated methods to read/write the new ConfigData format.
Usage of Configuration: Modified the rest of the plugin to access settings through the _configData field.
This setup organizes configuration data in a structured manner and makes it easier to adapt or expand related settings. Adjust any specific settings or parameters based on your needs or gameplay balance as required.
#region Initialization and Configuration
private void Init()
{
LoadConfig();
if (_configData.AutoSpawn.EnableAutoSpawn)
{
StartAutoSpawnTimer();
}
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
_configData = Config.ReadObject<ConfigData>();
if (_configData == null)
{
LoadDefaultConfig();
}
}
catch (Exception ex)
{
PrintError($"The configuration file is corrupted. \n{ex}");
LoadDefaultConfig();
}
SaveConfig();
}
protected override void LoadDefaultConfig()
{
PrintWarning("Creating a new configuration file");
_configData = new ConfigData();
}
protected override void SaveConfig()
{
Config.WriteObject(_configData, true);
}
private class ConfigData
{
[JsonProperty(PropertyName = "Version")]
public string Version { get; set; } = "1.0.0";
[JsonProperty(PropertyName = "Auto Spawn Settings")]
public AutoSpawnSettings AutoSpawn { get; set; } = new AutoSpawnSettings();
[JsonProperty(PropertyName = "NPC Spawn Configs")]
public List<NpcSpawnConfig> NpcSpawnConfigs { get; set; } =
new List<NpcSpawnConfig>
{
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/tunneldweller/npc_tunneldweller.prefab", EntitysPerCell = 2 },
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_heavy.prefab", EntitysPerCell = 2 },
};
[JsonProperty(PropertyName = "Auto Turret Configs")]
public List<AutoTurretConfig> AutoTurretConfigs { get; set; } =
new List<AutoTurretConfig>
{
new AutoTurretConfig { PrefabName = "assets/prefabs/npc/autoturret/autoturret_deployed.prefab", EntitysPerCell = 2 },
};
}
private class AutoSpawnSettings
{
[JsonProperty(PropertyName = "Enable Auto Spawn")]
public bool EnableAutoSpawn { get; set; } = true;
[JsonProperty(PropertyName = "Auto Spawn Interval Minutes")]
public float AutoSpawnIntervalMinutes { get; set; } = 30f;
[JsonProperty(PropertyName = "Min Spawn Distance")]
public float MinSpawnDistance { get; set; } = 6f;
[JsonProperty(PropertyName = "Max Spawn Distance")]
public float MaxSpawnDistance { get; set; } = 20f;
[JsonProperty(PropertyName = "Max Spawn Attempts")]
public int MaxSpawnAttempts { get; set; } = 5;
}
private class NpcSpawnConfig
{
[JsonProperty("Prefab")]
public string PrefabName { get; set; }
[JsonProperty("Entitys Per Cell")]
public int EntitysPerCell { get; set; }
}
private class AutoTurretConfig
{
[JsonProperty("Prefab")]
public string PrefabName { get; set; }
[JsonProperty("Entitys Per Cell")]
public int EntitysPerCell { get; set; }
}
#endregion
#region Dungeon Management
[ChatCommand("dun")]
private void CreateDungeonCommand(BasePlayer player, string command, string[] args)
{
RemoveExistingDungeon();
if (TryFindDungeonSpawnPoint(player.transform.position, out var position, out var rotation))
{
_currentDungeon = CreateDungeon(position, rotation);
player.ChatMessage("Christmas Dungeon created!");
}
else
{
player.ChatMessage("Could not find a suitable location for the Christmas Dungeon.");
}
}
private void StartAutoSpawnTimer()
{
_autoSpawnTimer = timer.Every(_configData.AutoSpawn.AutoSpawnIntervalMinutes * 60f, AutoSpawnDungeon);
}
private void AutoSpawnDungeon()
{
RemoveExistingDungeon();
for (var attempt = 0; attempt < _configData.AutoSpawn.MaxSpawnAttempts; attempt++)
{
foreach (var player in BasePlayer.activePlayerList)
{
if (!TryFindDungeonSpawnPoint(player.transform.position, out var position, out var rotation))
continue;
_currentDungeon = CreateDungeon(position, rotation);
NotifyPlayersOfDungeonLocation(position);
return;
}
}
Puts("Failed to auto-spawn dungeon after max attempts.");
}
private void NotifyPlayersOfDungeonLocation(Vector3 position)
{
foreach (var player in BasePlayer.activePlayerList)
{
player.ChatMessage($"A Christmas Dungeon has appeared at coordinates: ({position.x:F0}, {position.z:F0})!");
}
}
private XmasDungeon CreateDungeon(Vector3 position, Quaternion rotation)
{
var entity = GameManager.server.CreateEntity(DungeonPrefabPath, position, rotation) as XmasDungeon;
if (entity == null)
{
Puts("Failed to create dungeon entity.");
return null;
}
entity.Spawn();
IntegrateEntitiesIntoDungeon(entity);
return entity;
}
private void IntegrateEntitiesIntoDungeon(XmasDungeon dungeon)
{
var proceduralDungeon = dungeon.dungeonInstance.Get(serverside: true);
if (proceduralDungeon == null)
{
Puts("Failed to get procedural dungeon instance.");
return;
}
int cellCount = proceduralDungeon.spawnedCells.Count;
if (cellCount == 0)
{
Puts("No cells found in dungeon for entities.");
return;
}
var allSpawnEntries = new List<SpawnGroup.SpawnEntry>();
foreach (var config in _configData.NpcSpawnConfigs)
{
if (GameManifest.pathToGuid.TryGetValue(config.PrefabName, out var guid))
{
for (int i = 0; i < config.EntitysPerCell; i++)
{
allSpawnEntries.Add(
new SpawnGroup.SpawnEntry
{
prefab = new GameObjectRef { guid = guid },
weight = 1,
mobile = true,
}
);
}
}
}
foreach (var config in _configData.AutoTurretConfigs)
{
if (GameManifest.pathToGuid.TryGetValue(config.PrefabName, out var guid))
{
for (int i = 0; i < config.EntitysPerCell; i++)
{
allSpawnEntries.Add(
new SpawnGroup.SpawnEntry
{
prefab = new GameObjectRef { guid = guid },
weight = 1,
mobile = false,
}
);
}
}
}
foreach (var cell in proceduralDungeon.spawnedCells)
{
SetupSpawnGroup(cell, allSpawnEntries);
}
}
private void SetupSpawnGroup(ProceduralDungeonCell cell, List<SpawnGroup.SpawnEntry> allSpawnEntries)
{
var spawnGroup = cell.gameObject.AddComponent<SpawnGroup>();
spawnGroup.prefabs = allSpawnEntries;
spawnGroup.maxPopulation = 100;
spawnGroup.numToSpawnPerTickMin = 0;
spawnGroup.numToSpawnPerTickMax = 0;
spawnGroup.fillOnSpawn = true;
spawnGroup.wantsInitialSpawn = true;
spawnGroup.SpawnInitial();
foreach (var instance in spawnGroup.spawnInstances)
{
var entity = instance.GetComponent<BaseEntity>();
if (entity != null && !_spawnedEntities.Contains(entity))
{
_spawnedEntities.Add(entity);
Puts($"Entity {entity.ShortPrefabName} spawned at {entity.transform.position}");
if (entity is AutoTurret autoTurret)
{
ConfigureAutoTurret(autoTurret);
}
}
}
}
private void ConfigureAutoTurret(AutoTurret autoTurret)
{
autoTurret.health = 1000f;
var weapon = ItemManager.CreateByName("rifle.ak");
weapon?.MoveToContainer(autoTurret.inventory, 0);
var ammo = ItemManager.CreateByName("ammo.rifle", 128);
ammo.MoveToContainer(autoTurret.inventory);
autoTurret.UpdateTotalAmmo();
autoTurret.EnsureReloaded();
autoTurret.InitializeHealth(1000f, 1000f);
autoTurret.SetPeacekeepermode(false);
}
private Vector3 GetRandomPositionNearby(Vector3 center, float minRadius, float maxRadius)
{
Vector2 randomCircle = UnityEngine.Random.insideUnitCircle * UnityEngine.Random.Range(minRadius, maxRadius);
return new Vector3(center.x + randomCircle.x, center.y, center.z + randomCircle.y);
}
#endregion
#region Utility and Cleanup
private bool TryFindDungeonSpawnPoint(Vector3 center, out Vector3 suitablePosition, out Quaternion suitableRotation)
{
suitablePosition = Vector3.zero;
suitableRotation = Quaternion.identity;
for (var attempt = 0; attempt < _configData.AutoSpawn.MaxSpawnAttempts; attempt++)
{
var potentialPosition = TerrainUtilities.GetRandomPosition(center, _configData.AutoSpawn.MinSpawnDistance, _configData.AutoSpawn.MaxSpawnDistance);
if (!TerrainUtilities.TryGetTerrainInfo(potentialPosition, 5f, out var hitInfo) || !IsPositionSuitable(potentialPosition))
{
continue;
}
suitablePosition = hitInfo.point;
suitableRotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
return true;
}
return false;
}
private bool IsPositionSuitable(Vector3 position)
{
return !TerrainUtilities.ContainsRock(position, 2f)
&& !TerrainUtilities.IsInRadTown(position)
&& !TerrainUtilities.ContainsEntitiesNearby(position, 4f, TerrainUtilities.EntityLayerMask)
&& !TerrainUtilities.IsInWater(position)
&& !TerrainUtilities.IsOnRoadOrRail(position)
&& !TerrainUtilities.IsInNoBuildZone(position, 2f);
}
private void RemoveExistingDungeon()
{
if (_currentDungeon != null)
{
foreach (var entity in _spawnedEntities)
{
if (entity != null && !entity.IsDestroyed)
{
entity.Kill(BaseNetworkable.DestroyMode.None);
}
}
_spawnedEntities.Clear();
_currentDungeon.Kill(BaseNetworkable.DestroyMode.None);
_currentDungeon = null;
}
}
private void Unload()
{
RemoveExistingDungeon();
_autoSpawnTimer?.Destroy();
}
#endregion
}
public static class TerrainUtilities
{
public const int EntityLayerMask = Layers.Mask.Deployed | Layers.Mask.Construction;
public const int GroundLayerMask = Layers.Mask.Terrain | Layers.Mask.World | Layers.Mask.Default;
public static bool OnTopology(Vector3 position, TerrainTopology.Enum topology)
{
return (TerrainMeta.TopologyMap.GetTopology(position) & (int)topology) != 0;
}
public static bool IsOnRoadOrRail(Vector3 position)
{
return OnTopology(position, TerrainTopology.Enum.Road | TerrainTopology.Enum.Roadside | TerrainTopology.Enum.Rail | TerrainTopology.Enum.Railside);
}
public static bool IsInNoBuildZone(Vector3 position, float radius)
{
return Physics.CheckSphere(position, radius, Layers.Mask.Prevent_Building);
}
public static bool IsInWater(Vector3 position)
{
return WaterLevel.Test(position, false, true);
}
public static bool ContainsRock(Vector3 position, float radius)
{
var colliders = Pool.GetList<Collider>();
Vis.Colliders(position, radius, colliders, GroundLayerMask, QueryTriggerInteraction.Ignore);
bool result = colliders.Exists(collider =>
collider.name.Contains("rock", StringComparison.OrdinalIgnoreCase)
|| collider.name.Contains("cliff", StringComparison.OrdinalIgnoreCase)
|| collider.name.Contains("formation", StringComparison.OrdinalIgnoreCase)
);
Pool.FreeList(ref colliders);
return result;
}
public static bool IsInRadTown(Vector3 position)
{
foreach (var monumentInfo in TerrainMeta.Path.Monuments)
{
if (monumentInfo.IsInBounds(position) && monumentInfo.shouldDisplayOnMap)
{
return true;
}
}
return OnTopology(position, TerrainTopology.Enum.Monument);
}
public static bool ContainsEntitiesNearby(Vector3 position, float radius, int mask)
{
var hitColliders = Pool.GetList<Collider>();
GamePhysics.OverlapSphere(position, radius, hitColliders, mask, QueryTriggerInteraction.Ignore);
bool result = hitColliders.Exists(collider => collider.gameObject.ToBaseEntity() != null);
Pool.FreeList(ref hitColliders);
return result;
}
public static bool TryGetTerrainInfo(Vector3 startPosition, float range, out RaycastHit hitInfo)
{
return Physics.Linecast(startPosition + new Vector3(0f, range, 0f), startPosition - new Vector3(0f, range, 0f), out hitInfo, GroundLayerMask);
}
public static Vector3 GetRandomPosition(Vector3 center, float minRadius, float maxRadius)
{
Vector3 randomDirection = UnityEngine.Random.onUnitSphere;
randomDirection.y = 0;
float randomDistance = UnityEngine.Random.Range(minRadius, maxRadius);
Vector3 randomPosition = center + randomDirection * randomDistance;
randomPosition.y = TerrainMeta.HeightMap.GetHeight(randomPosition);
return randomPosition;
}
}
}
using System;
using System.Collections.Generic;
using Facepunch;
using Newtonsoft.Json;
using Rust;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Dungeons", "Marte6", "1.0.0")]
public class Dungeons : RustPlugin
{
private ConfigData _configData;
private Timer _autoSpawnTimer;
private const string DungeonPrefabPath = "assets/prefabs/missions/portal/xmasportalentry.prefab";
private XmasDungeon _currentDungeon;
private List<BaseEntity> _spawnedEntities = new List<BaseEntity>();
#region Initialization and Configuration
private void Init()
{
LoadConfig();
if (_configData.AutoSpawn.EnableAutoSpawn)
{
StartAutoSpawnTimer();
}
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
_configData = Config.ReadObject<ConfigData>();
if (_configData == null || _configData.Version != "1.0.0")
{
PrintWarning("Existing configuration is outdated or missing. Creating default configuration.");
_configData = GetDefaultConfig();
SaveConfig();
}
}
catch (Exception ex)
{
PrintError($"The configuration file is corrupted. \n{ex}");
LoadDefaultConfig();
}
}
protected override void LoadDefaultConfig()
{
PrintWarning("Creating a new configuration file");
_configData = GetDefaultConfig();
SaveConfig();
}
protected override void SaveConfig()
{
Config.WriteObject(_configData, true);
}
private ConfigData GetDefaultConfig() => new ConfigData
{
Version = "1.0.0",
AutoSpawn = new AutoSpawnSettings
{
EnableAutoSpawn = true,
AutoSpawnIntervalMinutes = 30f,
MinSpawnDistance = 6f,
MaxSpawnDistance = 20f,
MaxSpawnAttempts = 5
},
NpcSpawnConfigs = new List<NpcSpawnConfig>
{
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/tunneldweller/npc_tunneldweller.prefab", EntitysPerCell = 2 },
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_heavy.prefab", EntitysPerCell = 2 },
},
AutoTurretConfigs = new List<AutoTurretConfig>
{
new AutoTurretConfig { PrefabName = "assets/prefabs/npc/autoturret/autoturret_deployed.prefab", EntitysPerCell = 2 },
}
};
private class ConfigData
{
[JsonProperty(PropertyName = "Version")]
public string Version { get; set; }
[JsonProperty(PropertyName = "Auto Spawn Settings")]
public AutoSpawnSettings AutoSpawn { get; set; }
[JsonProperty(PropertyName = "NPC Spawn Configs")]
public List<NpcSpawnConfig> NpcSpawnConfigs { get; set; }
[JsonProperty(PropertyName = "Auto Turret Configs")]
public List<AutoTurretConfig> AutoTurretConfigs { get; set; }
}
private class AutoSpawnSettings
{
[JsonProperty(PropertyName = "Enable Auto Spawn")]
public bool EnableAutoSpawn { get; set; }
[JsonProperty(PropertyName = "Auto Spawn Interval Minutes")]
public float AutoSpawnIntervalMinutes { get; set; }
[JsonProperty(PropertyName = "Min Spawn Distance")]
public float MinSpawnDistance { get; set; }
[JsonProperty(PropertyName = "Max Spawn Distance")]
public float MaxSpawnDistance { get; set; }
[JsonProperty(PropertyName = "Max Spawn Attempts")]
public int MaxSpawnAttempts { get; set; }
}
private class NpcSpawnConfig
{
[JsonProperty("Prefab")]
public string PrefabName { get; set; }
[JsonProperty("Entitys Per Cell")]
public int EntitysPerCell { get; set; }
}
private class AutoTurretConfig
{
[JsonProperty("Prefab")]
public string PrefabName { get; set; }
[JsonProperty("Entitys Per Cell")]
public int EntitysPerCell { get; set; }
}
#endregion
#region Dungeon Management
[ChatCommand("dun")]
private void CreateDungeonCommand(BasePlayer player, string command, string[] args)
{
RemoveExistingDungeon();
if (TryFindDungeonSpawnPoint(player.transform.position, out var position, out var rotation))
{
_currentDungeon = CreateDungeon(position, rotation);
player.ChatMessage("Christmas Dungeon created!");
}
else
{
player.ChatMessage("Could not find a suitable location for the Christmas Dungeon.");
}
}
private void StartAutoSpawnTimer()
{
_autoSpawnTimer = timer.Every(_configData.AutoSpawn.AutoSpawnIntervalMinutes * 60f, AutoSpawnDungeon);
}
private void AutoSpawnDungeon()
{
RemoveExistingDungeon();
for (var attempt = 0; attempt < _configData.AutoSpawn.MaxSpawnAttempts; attempt++)
{
foreach (var player in BasePlayer.activePlayerList)
{
if (!TryFindDungeonSpawnPoint(player.transform.position, out var position, out var rotation))
continue;
_currentDungeon = CreateDungeon(position, rotation);
NotifyPlayersOfDungeonLocation(position);
return;
}
}
Puts("Failed to auto-spawn dungeon after max attempts.");
}
private void NotifyPlayersOfDungeonLocation(Vector3 position)
{
foreach (var player in BasePlayer.activePlayerList)
{
player.ChatMessage($"A Christmas Dungeon has appeared at coordinates: ({position.x:F0}, {position.z:F0})!");
}
}
private XmasDungeon CreateDungeon(Vector3 position, Quaternion rotation)
{
var entity = GameManager.server.CreateEntity(DungeonPrefabPath, position, rotation) as XmasDungeon;
if (entity == null)
{
Puts("Failed to create dungeon entity.");
return null;
}
entity.Spawn();
IntegrateEntitiesIntoDungeon(entity);
return entity;
}
private void IntegrateEntitiesIntoDungeon(XmasDungeon dungeon)
{
var proceduralDungeon = dungeon.dungeonInstance.Get(serverside: true);
if (proceduralDungeon == null)
{
Puts("Failed to get procedural dungeon instance.");
return;
}
int cellCount = proceduralDungeon.spawnedCells.Count;
if (cellCount == 0)
{
Puts("No cells found in dungeon for entities.");
return;
}
var allSpawnEntries = new List<SpawnGroup.SpawnEntry>();
foreach (var config in _configData.NpcSpawnConfigs)
{
if (GameManifest.pathToGuid.TryGetValue(config.PrefabName, out var guid))
{
for (int i = 0; i < config.EntitysPerCell; i++)
{
allSpawnEntries.Add(new SpawnGroup.SpawnEntry
{
prefab = new GameObjectRef { guid = guid },
weight = 1,
mobile = true,
});
}
}
}
foreach (var config in _configData.AutoTurretConfigs)
{
if (GameManifest.pathToGuid.TryGetValue(config.PrefabName, out var guid))
{
for (int i = 0; i < config.EntitysPerCell; i++)
{
allSpawnEntries.Add(new SpawnGroup.SpawnEntry
{
prefab = new GameObjectRef { guid = guid },
weight = 1,
mobile = false,
});
}
}
}
foreach (var cell in proceduralDungeon.spawnedCells)
{
SetupSpawnGroup(cell, allSpawnEntries);
}
}
private void SetupSpawnGroup(ProceduralDungeonCell cell, List<SpawnGroup.SpawnEntry> allSpawnEntries)
{
var spawnGroup = cell.gameObject.AddComponent<SpawnGroup>();
spawnGroup.prefabs = allSpawnEntries;
spawnGroup.maxPopulation = 100;
spawnGroup.numToSpawnPerTickMin = 0;
spawnGroup.numToSpawnPerTickMax = 0;
spawnGroup.fillOnSpawn = true;
spawnGroup.wantsInitialSpawn = true;
spawnGroup.SpawnInitial();
foreach (var instance in spawnGroup.spawnInstances)
{
var entity = instance.GetComponent<BaseEntity>();
if (entity != null && !_spawnedEntities.Contains(entity))
{
_spawnedEntities.Add(entity);
Puts($"Entity {entity.ShortPrefabName} spawned at {entity.transform.position}");
if (entity is AutoTurret autoTurret)
{
ConfigureAutoTurret(autoTurret);
}
}
}
}
private void ConfigureAutoTurret(AutoTurret autoTurret)
{
autoTurret.health = 1000f;
var weapon = ItemManager.CreateByName("rifle.ak");
weapon?.MoveToContainer(autoTurret.inventory, 0);
var ammo = ItemManager.CreateByName("ammo.rifle", 128);
ammo.MoveToContainer(autoTurret.inventory);
autoTurret.UpdateTotalAmmo();
autoTurret.EnsureReloaded();
autoTurret.InitializeHealth(1000f, 1000f);
autoTurret.SetPeacekeepermode(false);
}
private Vector3 GetRandomPositionNearby(Vector3 center, float minRadius, float maxRadius)
{
Vector2 randomCircle = UnityEngine.Random.insideUnitCircle * UnityEngine.Random.Range(minRadius, maxRadius);
return new Vector3(center.x + randomCircle.x, center.y, center.z + randomCircle.y);
}
#endregion
#region Utility and Cleanup
private bool TryFindDungeonSpawnPoint(Vector3 center, out Vector3 suitablePosition, out Quaternion suitableRotation)
{
suitablePosition = Vector3.zero;
suitableRotation = Quaternion.identity;
for (var attempt = 0; attempt < _configData.AutoSpawn.MaxSpawnAttempts; attempt++)
{
var potentialPosition = TerrainUtilities.GetRandomPosition(center, _configData.AutoSpawn.MinSpawnDistance, _configData.AutoSpawn.MaxSpawnDistance);
if (!TerrainUtilities.TryGetTerrainInfo(potentialPosition, 5f, out var hitInfo) || !IsPositionSuitable(potentialPosition))
{
continue;
}
suitablePosition = hitInfo.point;
suitableRotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
return true;
}
return false;
}
private bool IsPositionSuitable(Vector3 position)
{
return !TerrainUtilities.ContainsRock(position, 2f)
&& !TerrainUtilities.IsInRadTown(position)
&& !TerrainUtilities.ContainsEntitiesNearby(position, 4f, TerrainUtilities.EntityLayerMask)
&& !TerrainUtilities.IsInWater(position)
&& !TerrainUtilities.IsOnRoadOrRail(position)
&& !TerrainUtilities.IsInNoBuildZone(position, 2f);
}
private void RemoveExistingDungeon()
{
if (_currentDungeon != null)
{
foreach (var entity in _spawnedEntities)
{
if (entity != null && !entity.IsDestroyed)
{
entity.Kill(BaseNetworkable.DestroyMode.None);
}
}
_spawnedEntities.Clear();
_currentDungeon.Kill(BaseNetworkable.DestroyMode.None);
_currentDungeon = null;
}
}
private void Unload()
{
RemoveExistingDungeon();
_autoSpawnTimer?.Destroy();
}
#endregion
}
public static class TerrainUtilities
{
public const int EntityLayerMask = Layers.Mask.Deployed | Layers.Mask.Construction;
public const int GroundLayerMask = Layers.Mask.Terrain | Layers.Mask.World | Layers.Mask.Default;
public static bool OnTopology(Vector3 position, TerrainTopology.Enum topology)
{
return (TerrainMeta.TopologyMap.GetTopology(position) & (int)topology) != 0;
}
public static bool IsOnRoadOrRail(Vector3 position)
{
return OnTopology(position, TerrainTopology.Enum.Road | TerrainTopology.Enum.Roadside | TerrainTopology.Enum.Rail | TerrainTopology.Enum.Railside);
}
public static bool IsInNoBuildZone(Vector3 position, float radius)
{
return Physics.CheckSphere(position, radius, Layers.Mask.Prevent_Building);
}
public static bool IsInWater(Vector3 position)
{
return WaterLevel.Test(position, false, true);
}
public static bool ContainsRock(Vector3 position, float radius)
{
var colliders = Pool.GetList<Collider>();
Vis.Colliders(position, radius, colliders, GroundLayerMask, QueryTriggerInteraction.Ignore);
bool result = colliders.Exists(collider =>
collider.name.Contains("rock", StringComparison.OrdinalIgnoreCase)
|| collider.name.Contains("cliff", StringComparison.OrdinalIgnoreCase)
|| collider.name.Contains("formation", StringComparison.OrdinalIgnoreCase)
);
Pool.FreeList(ref colliders);
return result;
}
public static bool IsInRadTown(Vector3 position)
{
foreach (var monumentInfo in TerrainMeta.Path.Monuments)
{
if (monumentInfo.IsInBounds(position) && monumentInfo.shouldDisplayOnMap)
{
return true;
}
}
return OnTopology(position, TerrainTopology.Enum.Monument);
}
public static bool ContainsEntitiesNearby(Vector3 position, float radius, int mask)
{
var hitColliders = Pool.GetList<Collider>();
GamePhysics.OverlapSphere(position, radius, hitColliders, mask, QueryTriggerInteraction.Ignore);
bool result = hitColliders.Exists(collider => collider.gameObject.ToBaseEntity() != null);
Pool.FreeList(ref hitColliders);
return result;
}
public static bool TryGetTerrainInfo(Vector3 startPosition, float range, out RaycastHit hitInfo)
{
return Physics.Linecast(startPosition + new Vector3(0f, range, 0f), startPosition - new Vector3(0f, range, 0f), out hitInfo, GroundLayerMask);
}
public static Vector3 GetRandomPosition(Vector3 center, float minRadius, float maxRadius)
{
Vector3 randomDirection = UnityEngine.Random.onUnitSphere;
randomDirection.y = 0;
float randomDistance = UnityEngine.Random.Range(minRadius, maxRadius);
Vector3 randomPosition = center + randomDirection * randomDistance;
randomPosition.y = TerrainMeta.HeightMap.GetHeight(randomPosition);
return randomPosition;
}
}
}
Key Fixes and Improvements
Default Configuration Management:
Used a GetDefaultConfig method to provide default configuration settings and ensure they're applied only when necessary.
Check if the configuration version is correct before overwriting it.
Version Checking:
Added a version check in the configuration loading to prevent duplication when the plugin is reloaded with an unchanged configuration version.
This improved setup should prevent configurations from being duplicated upon plugin restarts. Make sure to delete the existing configuration file and let the plugin recreate it to avoid retaining any duplicates that have already been written.
[PluginReference]
private readonly Plugin Friends, Clans, NoEscape, RustTranslationAPI;
private const string PermissionUpPrefix = "buildinggrades.up.";
private const string PermissionDownPrefx = "buildinggrades.down.";
private const string PermissionUse = "buildinggrades.use";
private const string PermissionUpAll = PermissionUpPrefix + "all";
private const string PermissionDownAll = PermissionDownPrefx + "all";
private const string PermissionNoCost = "buildinggrades.nocost";
private const string PermissionAdmin = "buildinggrades.admin";
private static BuildingGrades _instance;
private BuildingGradeAssistant _assistant = new BuildingGradeAssistant();
private readonly Dictionary<string, HashSet<uint>> _categories = new Dictionary<string, HashSet<uint>>(StringComparer.OrdinalIgnoreCase);
private static readonly List<BuildingGrade.Enum> ValidGrades = new List<BuildingGrade.Enum>
{
BuildingGrade.Enum.Twigs, BuildingGrade.Enum.Wood, BuildingGrade.Enum.Stone, BuildingGrade.Enum.Metal, BuildingGrade.Enum.TopTier
};
#endregion Fields
#region Oxide Hooks
private void Init()
{
_instance = this;
foreach (var kvp in _configData.Permissions)
{
if (!permission.PermissionExists(kvp.Key, this))
{
permission.RegisterPermission(kvp.Key, this);
}
}
permission.RegisterPermission(PermissionUpAll, this);
permission.RegisterPermission(PermissionDownAll, this);
permission.RegisterPermission(PermissionNoCost, this);
permission.RegisterPermission(PermissionAdmin, this);
if (!permission.PermissionExists(PermissionUse, this))
{
permission.RegisterPermission(PermissionUse, this);
}
foreach (var validGrade in ValidGrades)
{
if (validGrade > BuildingGrade.Enum.Twigs)
{
permission.RegisterPermission(PermissionUpPrefix + (int)validGrade, this);
}
if (validGrade < BuildingGrade.Enum.TopTier)
{
permission.RegisterPermission(PermissionDownPrefx + (int)validGrade, this);
}
}
cmd.AddChatCommand(_configData.Chat.UpgradeCommand, this, nameof(CmdUpgrade));
cmd.AddChatCommand(_configData.Chat.DowngradeCommand, this, nameof(CmdDowngrade));
cmd.AddChatCommand(_configData.Chat.UpgradeAllCommand, this, nameof(CmdUpgradeAll));
cmd.AddChatCommand(_configData.Chat.DowngradeAllCommand, this, nameof(CmdDowngradeAll));
if (!_configData.Global.UseRaidBlocker)
{
Unsubscribe(nameof(OnRaidBlock));
}
if (!_configData.Global.UseCombatBlocker)
{
Unsubscribe(nameof(OnCombatBlock));
}
}
private void OnServerInitialized()
{
foreach (var entry in _configData.Categories)
{
var values = new HashSet<uint>();
foreach (var prefab in entry.Value)
{
var item = StringPool.Get(prefab);
if (item == 0u)
{
continue;
}
values.Add(item);
}
_categories.Add(entry.Key, values);
}
}
private void Unload()
{
_assistant.Unload();
_instance = null;
}
private void OnRaidBlock(BasePlayer player)
{
OnCombatBlock(player);
}
private void OnCombatBlock(BasePlayer player)
{
if (_assistant.IsRunning())
{
_assistant.BlockPlayer(player.userID);
}
}
#endregion Oxide Hooks
#region Chat Commands
private void CmdUpgrade(BasePlayer player, string command, string[] args)
{
HandleCommand(player, args, true, false);
}
private void CmdDowngrade(BasePlayer player, string command, string[] args)
{
HandleCommand(player, args, false, false);
}
private void CmdUpgradeAll(BasePlayer player, string command, string[] args)
{
HandleCommand(player, args, true, true);
}
private void CmdDowngradeAll(BasePlayer player, string command, string[] args)
{
HandleCommand(player, args, false, true);
}
#endregion Chat Commands
#region Methods
private void HandleCommand(BasePlayer player, string[] args, bool isUpgrade, bool isAll)
{
if (!permission.UserHasPermission(player.UserIDString, PermissionUse))
{
Print(player, Lang("NotAllowed", player.UserIDString));
return;
}
if (_assistant.IsRunning())
{
Print(player, Lang("AlreadyProcess", player.UserIDString));
return;
}
if (IsPlayerBlocked(player))
{
return;
}
HashSet<uint> filter = null;
var targetGrade = BuildingGrade.Enum.None;
if (args.Length > 0)
{
if (!_categories.TryGetValue(args[0], out filter))
{
if (!Enum.TryParse(args[0], true, out targetGrade) || !ValidGrades.Contains(targetGrade) || isUpgrade ? targetGrade <= BuildingGrade.Enum.Twigs : targetGrade >= BuildingGrade.Enum.TopTier)
{
Print(player, Lang("UnknownGrade", player.UserIDString));
return;
}
if (!HasGradePermission(player, targetGrade, isUpgrade))
{
Print(player, Lang("NotAllowed", player.UserIDString));
return;
}
if (args.Length > 1 && !_categories.TryGetValue(args[1], out filter))
{
Print(player, Lang("UnknownCategory", player.UserIDString, string.Join(", ", _configData.Categories.Keys)));
return;
}
}
}
else
{
if (!_assistant.FindPlayerGrantedGrades(player, isUpgrade))
{
Print(player, Lang("NotAllowed", player.UserIDString));
return;
}
}
var permissionSettings = GetPermissionSettings(player);
if (permissionSettings.Cooldown > 0f && !(_configData.Global.CooldownExclude && player.IsAdmin))
{
var timeLeft = _assistant.GetCooldownLeft(player.userID, permissionSettings.Cooldown);
if (timeLeft > 0f)
{
Print(player, Lang("OnCooldown", player.UserIDString, Mathf.CeilToInt(timeLeft)));
return;
}
}
var targetBuildingBlock = GetBuildingBlockLookingAt(player, permissionSettings);
if (targetBuildingBlock == null)
{
Print(player, Lang("NotLookingAt", player.UserIDString));
return;
}
var isAdmin = permission.UserHasPermission(player.UserIDString, PermissionAdmin);
if (!isAdmin && player.IsBuildingBlocked(targetBuildingBlock.WorldSpaceBounds()))
{
Print(player, Lang("BuildingBlocked", player.UserIDString));
return;
}
_assistant.Run(targetBuildingBlock, player, targetGrade, filter, permissionSettings, isUpgrade, isAll, isAdmin);
}
private PermissionSettings GetPermissionSettings(BasePlayer player)
{
var priority = 0;
PermissionSettings permissionSettings = null;
foreach (var entry in _configData.Permissions)
{
if (entry.Value.Priority >= priority && permission.UserHasPermission(player.UserIDString, entry.Key))
{
priority = entry.Value.Priority;
permissionSettings = entry.Value;
}
}
return permissionSettings ?? new PermissionSettings();
}
private bool HasGradePermission(BasePlayer player, BuildingGrade.Enum grade, bool isUpgrade)
{
return isUpgrade
? permission.UserHasPermission(player.UserIDString, PermissionUpAll) ||
permission.UserHasPermission(player.UserIDString, PermissionUpPrefix + (int)grade)
: permission.UserHasPermission(player.UserIDString, PermissionDownAll) ||
permission.UserHasPermission(player.UserIDString, PermissionDownPrefx + (int)grade);
}
#region AreFriends
private bool AreFriends(ulong playerId, ulong friendId)
{
if (!playerId.IsSteamId())
{
return false;
}
if (playerId == friendId)
{
return true;
}
if (_configData.Global.UseTeams && SameTeam(playerId, friendId))
{
return true;
}
if (_configData.Global.UseFriends && HasFriend(playerId, friendId))
{
return true;
}
if (_configData.Global.UseClans && SameClan(playerId, friendId))
{
return true;
}
return false;
}
private static bool SameTeam(ulong playerId, ulong friendId)
{
if (!RelationshipManager.TeamsEnabled())
{
return false;
}
var playerTeam = RelationshipManager.ServerInstance.FindPlayersTeam(playerId);
if (playerTeam == null)
{
return false;
}
var friendTeam = RelationshipManager.ServerInstance.FindPlayersTeam(friendId);
if (friendTeam == null)
{
return false;
}
return playerTeam == friendTeam;
}
private bool HasFriend(ulong playerId, ulong friendId)
{
if (Friends == null)
{
return false;
}
return (bool)Friends.Call("HasFriend", playerId, friendId);
}
private bool SameClan(ulong playerId, ulong friendId)
{
if (Clans == null)
{
return false;
}
//Clans
var isMember = Clans.Call("IsClanMember", playerId.ToString(), friendId.ToString());
if (isMember != null)
{
return (bool)isMember;
}
//Rust:IO Clans
var playerClan = Clans.Call("GetClanOf", playerId);
if (playerClan == null)
{
return false;
}
var friendClan = Clans.Call("GetClanOf", friendId);
if (friendClan == null)
{
return false;
}
return (string)playerClan == (string)friendClan;
}
#endregion AreFriends
#region PlayerIsBlocked
private bool IsPlayerBlocked(BasePlayer player)
{
if (NoEscape == null)
{
return false;
}
if (_configData.Global.UseRaidBlocker && IsRaidBlocked(player.UserIDString))
{
Print(player, Lang("RaidBlocked", player.UserIDString));
return true;
}
if (_configData.Global.UseCombatBlocker && IsCombatBlocked(player.UserIDString))
{
Print(player, Lang("CombatBlocked", player.UserIDString));
return true;
}
return false;
}
private bool IsRaidBlocked(string playerId)
{
return (bool)NoEscape.Call("IsRaidBlocked", playerId);
}
private bool IsCombatBlocked(string playerId)
{
return (bool)NoEscape.Call("IsCombatBlocked", playerId);
}
#endregion PlayerIsBlocked
#region RustTranslationAPI
private string GetItemTranslationByShortName(string language, string itemShortName)
{
return (string)RustTranslationAPI.Call("GetItemTranslationByShortName", language, itemShortName);
}
private string GetItemDisplayName(string language, ItemDefinition itemDefinition)
{
if (RustTranslationAPI != null)
{
var displayName = GetItemTranslationByShortName(language, itemDefinition.shortname);
if (!string.IsNullOrEmpty(displayName))
{
return displayName;
}
}
return itemDefinition.displayName.english;
}
#endregion RustTranslationAPI
#endregion Methods
#region Assistant
private class BuildingGradeAssistant
{
private BuildingGrades Plugin => _instance;
private Coroutine _changeGradeCoroutine;
private readonly Dictionary<ulong, bool> _tempFriends = new Dictionary<ulong, bool>();
private readonly HashSet<BuildingBlock> _allBuildingBlocks = new HashSet<BuildingBlock>();
private readonly List<BuildingGrade.Enum> _tempGrantedGrades = new List<BuildingGrade.Enum>();
private readonly List<Item> _collect = new List<Item>();
private readonly Hash<int, float> _takeOutItems = new Hash<int, float>();
private readonly Hash<ItemDefinition, int> _missingItems = new Hash<ItemDefinition, int>();
private HashSet<ulong> _blockedPlayers = new HashSet<ulong>();
private readonly Dictionary<ulong, float> _cooldowns = new Dictionary<ulong, float>();
private BuildingBlock _processingBuildingBlock;
internal bool IsRunning()
{
return _changeGradeCoroutine != null;
}
internal void Unload()
{
if (_changeGradeCoroutine != null)
{
ServerMgr.Instance.StopCoroutine(_changeGradeCoroutine);
}
}
internal void BlockPlayer(ulong playerId)
{
_blockedPlayers.Add(playerId);
}
internal bool IsProcessingBuildingBlock(BuildingBlock buildingBlock)
{
if (buildingBlock == null)
{
return false;
}
return _processingBuildingBlock == buildingBlock;
}
internal void Run(BuildingBlock sourceEntity, BasePlayer player, BuildingGrade.Enum targetGrade, HashSet<uint> filter, PermissionSettings permissionSettings, bool isUpgrade, bool isAll, bool isAdmin)
{
if (IsRunning())
{
return;
}
_changeGradeCoroutine = ServerMgr.Instance.StartCoroutine(StartChangeBuildingGrade(sourceEntity, player, targetGrade, filter, permissionSettings, isUpgrade, isAll, isAdmin));
}
internal float GetCooldownLeft(ulong playerId, float cooldown)
{
float lastUse;
if (_cooldowns.TryGetValue(playerId, out lastUse))
{
return cooldown - (Time.realtimeSinceStartup - lastUse);
}
return 0f;
}
internal bool FindPlayerGrantedGrades(BasePlayer player, bool isUpgrade)
{
var all = Plugin.permission.UserHasPermission(player.UserIDString, isUpgrade ? PermissionUpAll : PermissionDownAll);
if (all)
{
_tempGrantedGrades.AddRange(ValidGrades);
return _tempGrantedGrades.Count > 0;
}
foreach (var validGrade in ValidGrades)
{
if (isUpgrade)
{
if (validGrade > BuildingGrade.Enum.Twigs && Plugin.permission.UserHasPermission(player.UserIDString, PermissionUpPrefix + (int)validGrade))
{
_tempGrantedGrades.Add(validGrade);
}
}
else
{
if (validGrade < BuildingGrade.Enum.TopTier && Plugin.permission.UserHasPermission(player.UserIDString, PermissionDownPrefx + (int)validGrade))
{
_tempGrantedGrades.Add(validGrade);
}
}
}
return _tempGrantedGrades.Count > 0;
}
private IEnumerator StartChangeBuildingGrade(BuildingBlock sourceEntity, BasePlayer player, BuildingGrade.Enum targetGrade, HashSet<uint> filter, PermissionSettings permissionSettings, bool isUpgrade, bool isAll, bool isAdmin)
{
_blockedPlayers.Clear();
yield return GetAllBuildingBlocks(sourceEntity, filter, isAll);
//if (pay) {
// FindUpgradeCosts(targetGrade);
// if (!CanAffordUpgrade(player)) {
// Clear();
// yield break;
// }
// PayForUpgrade(costs, player);
//}
Plugin.Print(player, Plugin.Lang(isUpgrade ? "StartUpgrade" : "StartDowngrade", player.UserIDString));
var playerId = player.userID;
yield return isUpgrade ? UpgradeBuildingBlocks(player, targetGrade, permissionSettings, isAdmin) : DowngradeBuildingBlocks(player, targetGrade, permissionSettings, isAdmin);
_tempFriends.Clear();
_allBuildingBlocks.Clear();
_tempGrantedGrades.Clear();
_blockedPlayers.Clear();
_cooldowns[playerId] = Time.realtimeSinceStartup;
_processingBuildingBlock = null;
_changeGradeCoroutine = null;
}
private bool ShouldInterrupt(ulong playerId)
{
return _blockedPlayers.Contains(playerId);
}
#region Upgrade
private IEnumerator UpgradeBuildingBlocks(BasePlayer player, BuildingGrade.Enum targetGrade, PermissionSettings permissionSettings, bool isAdmin)
{
var pay = permissionSettings.Pay && !Plugin.permission.UserHasPermission(player.UserIDString, PermissionNoCost);
int current = 0, success = 0;
var autoGrade = targetGrade == BuildingGrade.Enum.None;
foreach (var buildingBlock in _allBuildingBlocks)
{
if (buildingBlock == null || buildingBlock.IsDestroyed)
{
continue;
}
if (ShouldInterrupt(player.userID))
{
break;
}
var grade = targetGrade;
if (CheckBuildingGrade(buildingBlock, true, ref grade))
{
if (!autoGrade || _tempGrantedGrades.Contains(grade))
{
_processingBuildingBlock = buildingBlock;
if (TryUpgradeToGrade(buildingBlock, player, grade, pay, isAdmin))
{
success++;
}
_processingBuildingBlock = null;
}
}
if (++current % Plugin._configData.Global.PerFrame == 0)
{
yield return CoroutineEx.waitForEndOfFrame;
}
}
foreach (var item in _collect)
{
_takeOutItems[item.info.itemid] += item.amount;
item.Remove();
}
foreach (var entry in _takeOutItems)
{
player.Command("note.inv " + entry.Key + " " + entry.Value * -1f);
}
if (player != null && player.IsConnected)
{
if (_missingItems.Count > 0)
{
var stringBuilder = Pool.Get<StringBuilder>();
stringBuilder.Clear();
var language = Plugin.lang.GetLanguage(player.UserIDString);
foreach (var entry in _missingItems)
{
stringBuilder.AppendLine(Plugin.Lang("MissingItemsFormat", player.UserIDString, Plugin.GetItemDisplayName(language, entry.Key), entry.Value));
}
var missingResources = stringBuilder.ToString();
stringBuilder.Clear();
Pool.FreeUnmanaged(ref stringBuilder);
Plugin.Print(player, success > 0 ? Plugin.Lang("UpgradeNotEnoughItemsSuccess", player.UserIDString, success, missingResources) : Plugin.Lang("UpgradeNotEnoughItems", player.UserIDString, missingResources));
}
else
{
Plugin.Print(player, success > 0 ? Plugin.Lang("FinishedUpgrade", player.UserIDString, success) : Plugin.Lang("NotUpgraded", player.UserIDString));
}
}
_collect.Clear();
_takeOutItems.Clear();
_missingItems.Clear();
}
private bool TryUpgradeToGrade(BuildingBlock buildingBlock, BasePlayer player, BuildingGrade.Enum targetGrade, bool pay, bool isAdmin)
{
if (!CanUpgrade(buildingBlock, player, targetGrade, pay, isAdmin))
{
return false;
}
var oldGrade = buildingBlock.grade;
SetBuildingBlockGrade(buildingBlock, targetGrade);
Interface.CallHook("OnStructureGradeUpdated", buildingBlock, player, oldGrade, targetGrade);
return true;
}
private bool CanUpgrade(BuildingBlock buildingBlock, BasePlayer player, BuildingGrade.Enum targetGrade, bool pay, bool isAdmin)
{
if (player == null || !player.CanInteract())
{
return false;
}
var constructionGrade = buildingBlock.blockDefinition.GetGrade(targetGrade, 0);
if (constructionGrade == null)
{
return false;
}
if (!isAdmin)
{
if (Interface.CallHook("OnStructureUpgrade", buildingBlock, player, targetGrade, 0) != null)
{
return false;
}
if (buildingBlock.SecondsSinceAttacked < 30f)
{
return false;
}
if (!buildingBlock.CanChangeToGrade(targetGrade, 0, player))
{
return false;
}
if (!HasAccess(player, buildingBlock))
{
return false;
}
}
if (pay)
{
//if (!buildingBlock.CanAffordUpgrade(targetGrade, player)) {
// return false;
//}
//buildingBlock.PayForUpgrade(constructionGrade, player);
if (!CanAffordUpgrade(buildingBlock, constructionGrade, player, targetGrade))
{
return false;
}
PayForUpgrade(buildingBlock, constructionGrade, player);
}
return true;
}
public bool CanAffordUpgrade(BuildingBlock buildingBlock, ConstructionGrade constructionGrade, BasePlayer player, BuildingGrade.Enum grade)
{
var obj = Interface.CallHook("CanAffordUpgrade", player, buildingBlock, grade);
if (obj is bool)
{
return (bool)obj;
}
var flag = true;
foreach (var item in constructionGrade.CostToBuild())
{
var missingAmount = item.amount - player.inventory.GetAmount(item.itemid);
if (missingAmount > 0f)
{
flag = false;
_missingItems[item.itemDef] += (int)missingAmount;
}
}
return flag;
}
public void PayForUpgrade(BuildingBlock buildingBlock, ConstructionGrade constructionGrade, BasePlayer player)
{
if (Interface.CallHook("OnPayForUpgrade", player, buildingBlock, constructionGrade) != null)
{
return;
}
foreach (var item in constructionGrade.CostToBuild())
{
player.inventory.Take(_collect, item.itemid, (int)item.amount);
//player.Command("note.inv " + item.itemid + " " + item.amount * -1f);
}
}
#endregion Upgrade
#region Downgrade
private IEnumerator DowngradeBuildingBlocks(BasePlayer player, BuildingGrade.Enum targetGrade, PermissionSettings permissionSettings, bool isAdmin)
{
int current = 0, success = 0;
var autoGrade = targetGrade == BuildingGrade.Enum.None;
foreach (var buildingBlock in _allBuildingBlocks)
{
if (buildingBlock == null || buildingBlock.IsDestroyed)
{
continue;
}
if (ShouldInterrupt(player.userID))
{
break;
}
var grade = targetGrade;
if (CheckBuildingGrade(buildingBlock, false, ref grade))
{
if (!autoGrade || _tempGrantedGrades.Contains(grade))
{
_processingBuildingBlock = buildingBlock;
if (TryDowngradeToGrade(buildingBlock, player, grade /*, permissionSettings.Refund*/, isAdmin: isAdmin))
{
success++;
}
_processingBuildingBlock = null;
}
}
if (current++ % Plugin._configData.Global.PerFrame == 0)
{
yield return CoroutineEx.waitForEndOfFrame;
}
}
if (player != null && player.IsConnected)
{
Plugin.Print(player, success > 0 ? Plugin.Lang("FinishedDowngrade", player.UserIDString, success) : Plugin.Lang("NotDowngraded", player.UserIDString));
}
}
private bool TryDowngradeToGrade(BuildingBlock buildingBlock, BasePlayer player, BuildingGrade.Enum targetGrade, bool refund = false, bool isAdmin = false)
{
if (!CanDowngrade(buildingBlock, player, targetGrade, refund, isAdmin))
{
return false;
}
//if (refund)
//{
// foreach (var itemAmount in buildingBlock.currentGrade.costToBuild)
// {
// var item = ItemManager.CreateByItemID(itemAmount.itemid, (int)itemAmount.amount);
// player.GiveItem(item);
// }
//}
var oldGrade = buildingBlock.grade;
SetBuildingBlockGrade(buildingBlock, targetGrade);
Interface.CallHook("OnStructureGradeUpdated", buildingBlock, player, oldGrade, targetGrade);
return true;
}
private bool CanDowngrade(BuildingBlock buildingBlock, BasePlayer player, BuildingGrade.Enum targetGrade, bool refund, bool isAdmin)
{
if (player == null || !player.CanInteract())
{
return false;
}
var constructionGrade = buildingBlock.blockDefinition.GetGrade(targetGrade, 0);
if (constructionGrade == null)
{
return false;
}
if (!isAdmin)
{
if (Interface.CallHook("OnStructureUpgrade", buildingBlock, player, targetGrade, 0) != null)
{
return false;
}
if (buildingBlock.SecondsSinceAttacked < 30f)
{
return false;
}
var obj = Interface.CallHook("CanChangeGrade", player, buildingBlock, targetGrade);
if (obj is bool)
{
return (bool)obj;
}
if (player.IsBuildingBlocked(buildingBlock.transform.position, buildingBlock.transform.rotation, buildingBlock.bounds))
{
return false;
}
if (buildingBlock.IsUpgradeBlocked())
{
return false;
}
if (!HasAccess(player, buildingBlock))
{
return false;
}
}
return true;
}
#endregion Downgrade
#region Methods
private bool HasAccess(BasePlayer player, BuildingBlock buildingBlock)
{
bool flag;
if (_tempFriends.TryGetValue(buildingBlock.OwnerID, out flag))
{
return flag;
}
var areFriends = Plugin.AreFriends(buildingBlock.OwnerID, player.userID);
_tempFriends.Add(buildingBlock.OwnerID, areFriends);
return areFriends;
}
private IEnumerator GetAllBuildingBlocks(BuildingBlock sourceEntity, HashSet<uint> filter, bool isAll)
{
Func<BuildingBlock, bool> func = x => filter == null || filter.Contains(x.prefabID);
if (isAll)
{
yield return GetNearbyEntities(sourceEntity, _allBuildingBlocks, Layers.Mask.Construction, func);
}
else
{
var building = sourceEntity.GetBuilding();
if (building != null)
{
foreach (var buildingBlock in building.buildingBlocks)
{
if (func(buildingBlock))
{
_allBuildingBlocks.Add(buildingBlock);
}
}
}
}
}
//#region TryPay
//private readonly Hash<int, int> costs = new Hash<int, int>();
//private readonly Hash<string , int> missingDictionary = new Hash<string, int>();
//private readonly HashSet<BuildingBlock> toRemove = new HashSet<BuildingBlock>();
//private bool TryPay(BasePlayer player, BuildingGrade.Enum targetGrade, out string missingResources) {
// FindUpgradeCosts(player, targetGrade);
// if (!CanPay(player, out missingResources)) {
// return false;
// }
// var collect = Pool.GetList<Item>();
// foreach (var entry in costs) {
// player.inventory.Take(collect, entry.Key, entry.Value);
// player.Command(string.Concat("note.inv ", entry.Key, " ", entry.Value * -1f));
// }
// foreach (var item in collect) {
// item.Remove();
// }
// Pool.FreeList(ref collect);
// missingResources = null;
// return true;
//}
//private bool CanPay(BasePlayer player, out string missingResources) {
// foreach (var entry in costs) {
// if (entry.Value <= 0) continue;
// var missingAmount = entry.Value - player.inventory.GetAmount(entry.Key);
// if (missingAmount <= 0) continue;
// var displayName= ItemManager.FindItemDefinition(entry.Key)?.displayName.english;
// if (string.IsNullOrEmpty(displayName)) displayName = entry.Key.ToString();
// missingDictionary[displayName] += missingAmount;
// }
// if (missingDictionary.Count > 0) {
// StringBuilder stringBuilder = Pool.Get<StringBuilder>();
// stringBuilder.Clear();
// foreach (var entry in missingDictionary) {
// stringBuilder.AppendLine(Lang("MissingResourceFormat", player.UserIDString, entry.Key, entry.Value));
// }
// missingResources = stringBuilder.ToString();
// stringBuilder.Clear();
// missingDictionary.Clear();
// Pool.Free(ref stringBuilder);
// return false;
// }
// missingResources = null;
// return true;
//}
//private void FindUpgradeCosts(BasePlayer player, BuildingGrade.Enum targetGrade) {
// var autoGrade = targetGrade == BuildingGrade.Enum.None;
// foreach (var buildingBlock in allBuildingBlocks) {
// if (buildingBlock == null || buildingBlock.IsDestroyed) {
// toRemove.Add(buildingBlock);
// continue;
// }
// BuildingGrade.Enum grade = targetGrade;
// if (CheckBuildingGrade(buildingBlock, false, ref grade)) {
// if (!autoGrade || tempGrantedGrades.Contains(grade)) {
// if (!CanUpgrade(buildingBlock, player, grade)) {
// toRemove.Add(buildingBlock);
// continue;
// }
// }
// }
// var costToBuild = buildingBlock.blockDefinition.grades[(int)grade].costToBuild;
// foreach (var itemAmount in costToBuild) {
// costs[itemAmount.itemid] += (int)itemAmount.amount;
// }
// }
// foreach (var buildingBlock in toRemove) {
// allBuildingBlocks.Remove(buildingBlock);
// }
// toRemove.Clear();
//}
//#endregion TryPay
#endregion Methods
}
#endregion Assistant
#region Helpers
private static BuildingBlock GetBuildingBlockLookingAt(BasePlayer player, PermissionSettings permissionSettings)
{
RaycastHit raycastHit;
var flag = Physics.Raycast(player.eyes.HeadRay(), out raycastHit, permissionSettings.Distance, Layers.Mask.Construction);
return flag ? raycastHit.GetEntity() as BuildingBlock : null;
}
private static bool CheckBuildingGrade(BuildingBlock buildingBlock, bool isUpgrade, ref BuildingGrade.Enum targetGrade)
{
if (buildingBlock.blockDefinition == null)
{
return false;
}
var grades = buildingBlock.blockDefinition.grades;
if (grades == null)
{
return false;
}
var grade = buildingBlock.grade;
if (IsValidGrade(grades, (int)targetGrade))
{
return isUpgrade ? grade < targetGrade : grade > targetGrade;
}
targetGrade = grade + (isUpgrade ? 1 : -1);
return IsValidGrade(grades, (int)targetGrade);
}
private static bool IsValidGrade(ConstructionGrade[] grades, int targetGrade)
{
return targetGrade >= 0 && targetGrade < grades.Length && grades[targetGrade] != null;
}
private static void SetBuildingBlockGrade(BuildingBlock buildingBlock, BuildingGrade.Enum targetGrade)
{
buildingBlock.skinID = 0; // to avoid upgrade/downgrade to twig when using building skin
buildingBlock.SetGrade(targetGrade);
buildingBlock.SetHealthToMax();
buildingBlock.StartBeingRotatable();
buildingBlock.SendNetworkUpdate();
buildingBlock.UpdateSkin();
buildingBlock.ResetUpkeepTime();
buildingBlock.UpdateSurroundingEntities();
BuildingManager.server.GetBuilding(buildingBlock.buildingID)?.Dirty();
if (targetGrade > BuildingGrade.Enum.Twigs)
{
Effect.server.Run("assets/bundled/prefabs/fx/build/promote_" + targetGrade.ToString().ToLower() + ".prefab", buildingBlock, 0u, Vector3.zero, Vector3.zero);
}
}
private static IEnumerator GetNearbyEntities<T>(T sourceEntity, HashSet<T> entities, int layers, Func<T, bool> filter = null) where T : BaseEntity
{
var current = 0;
var checkFrom = Pool.Get<Queue<Vector3>>();
var nearbyEntities = Pool.Get<List<T>>();
checkFrom.Enqueue(sourceEntity.transform.position);
while (checkFrom.Count > 0)
{
nearbyEntities.Clear();
var position = checkFrom.Dequeue();
Vis.Entities(position, 3f, nearbyEntities, layers);
for (var i = 0; i < nearbyEntities.Count; i++)
{
var entity = nearbyEntities[i];
if (filter != null && !filter(entity))
{
continue;
}
if (!entities.Add(entity))
{
continue;
}
checkFrom.Enqueue(entity.transform.position);
}
if (++current % _instance._configData.Global.PerFrame == 0)
{
yield return CoroutineEx.waitForEndOfFrame;
}
}
Pool.FreeUnmanaged(ref checkFrom);
Pool.FreeUnmanaged(ref nearbyEntities);
}
#endregion Helpers
#region API
private bool IsProcessingBuildingBlock(BuildingBlock buildingBlock)
{
return _assistant.IsProcessingBuildingBlock(buildingBlock);
}
#endregion
#region ConfigurationFile
private ConfigData _configData;
private class ConfigData
{
[JsonProperty(PropertyName = "Settings")]
public GlobalSettings Global { get; set; } = new GlobalSettings();
[JsonProperty(PropertyName = "Chat Settings")]
public ChatSettings Chat { get; set; } = new ChatSettings();
[JsonProperty(PropertyName = "Permission Settings")]
public Dictionary<string, PermissionSettings> Permissions { get; set; } = new Dictionary<string, PermissionSettings>
{
[PermissionUse] = new PermissionSettings
{
Priority = 0,
Pay = true,
//Refund = false,
Cooldown = 60,
Distance = 10
}
};
[JsonProperty(PropertyName = "Building Block Categories", ObjectCreationHandling = ObjectCreationHandling.Replace)]
public Dictionary<string, HashSet<string>> Categories { get; set; } = new Dictionary<string, HashSet<string>>
{
["foundation"] = new HashSet<string>
{
"assets/prefabs/building core/foundation/foundation.prefab",
"assets/prefabs/building core/foundation.steps/foundation.steps.prefab",
"assets/prefabs/building core/foundation.triangle/foundation.triangle.prefab"
},
["wall"] = new HashSet<string>
{
"assets/prefabs/building core/wall/wall.prefab",
"assets/prefabs/building core/wall.doorway/wall.doorway.prefab",
"assets/prefabs/building core/wall.frame/wall.frame.prefab",
"assets/prefabs/building core/wall.half/wall.half.prefab",
"assets/prefabs/building core/wall.low/wall.low.prefab",
"assets/prefabs/building core/wall.window/wall.window.prefab"
},
["floor"] = new HashSet<string>
{
"assets/prefabs/building core/floor/floor.prefab",
"assets/prefabs/building core/floor.frame/floor.frame.prefab",
"assets/prefabs/building core/floor.triangle/floor.triangle.prefab",
"assets/prefabs/building core/floor.triangle.frame/floor.triangle.frame.prefab"
},
["stair"] = new HashSet<string>
{
"assets/prefabs/building core/stairs.l/block.stair.lshape.prefab",
"assets/prefabs/building core/stairs.spiral/block.stair.spiral.prefab",
"assets/prefabs/building core/stairs.spiral.triangle/block.stair.spiral.triangle.prefab",
"assets/prefabs/building core/stairs.u/block.stair.ushape.prefab"
},
["roof"] = new HashSet<string>
{
"assets/prefabs/building core/roof/roof.prefab",
"assets/prefabs/building core/roof.triangle/roof.triangle.prefab"
},
["ramp"] = new HashSet<string>
{
"assets/prefabs/building core/ramp/ramp.prefab"
}
};
}
private class GlobalSettings
{
[JsonProperty(PropertyName = "Use Teams")]
public bool UseTeams { get; set; }
[JsonProperty(PropertyName = "Use Clans")]
public bool UseClans { get; set; } = true;
[JsonProperty(PropertyName = "Use Friends")]
public bool UseFriends { get; set; } = true;
[JsonProperty(PropertyName = "Use Raid Blocker (Need NoEscape Plugin)")]
public bool UseRaidBlocker { get; set; }
[JsonProperty(PropertyName = "Use Combat Blocker (Need NoEscape Plugin)")]
public bool UseCombatBlocker { get; set; }
[JsonProperty(PropertyName = "Cooldown Exclude Admins")]
public bool CooldownExclude { get; set; } = true;
[JsonProperty(PropertyName = "Upgrade/Downgrade Per Frame")]
public int PerFrame { get; set; } = 10;
}
private class ChatSettings
{
[JsonProperty(PropertyName = "Upgrade Chat Command")]
public string UpgradeCommand { get; set; } = "up";
[JsonProperty(PropertyName = "Downgrade Chat Command")]
public string DowngradeCommand { get; set; } = "down";
[JsonProperty(PropertyName = "Upgrade All Chat Command")]
public string UpgradeAllCommand { get; set; } = "upall";
[JsonProperty(PropertyName = "Downgrade All Chat Command")]
public string DowngradeAllCommand { get; set; } = "downall";
[JsonProperty(PropertyName = "Chat Prefix")]
public string Prefix { get; set; } = "<color=#00FFFF>[BuildingGrades]</color>: ";
[JsonProperty(PropertyName = "Chat SteamID Icon")]
public ulong SteamIdIcon { get; set; } = 0;
}
public class PermissionSettings
{
[JsonProperty(PropertyName = "Priority")]
public int Priority { get; set; }
[JsonProperty(PropertyName = "Distance")]
public float Distance { get; set; } = 4f;
[JsonProperty(PropertyName = "Cooldown")]
public float Cooldown { get; set; } = 60f;
[JsonProperty(PropertyName = "Pay")]
public bool Pay { get; set; } = true;
// [JsonProperty(PropertyName = "Refund")]
// public bool Refund { get; set; }
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
_configData = Config.ReadObject<ConfigData>();
if (_configData == null)
{
LoadDefaultConfig();
}
}
catch (Exception ex)
{
PrintError($"The configuration file is corrupted. \n{ex}");
LoadDefaultConfig();
}
SaveConfig();
}
protected override void LoadDefaultConfig()
{
PrintWarning("Creating a new configuration file");
_configData = new ConfigData();
}
protected override void SaveConfig()
{
Config.WriteObject(_configData);
}
#endregion ConfigurationFile
#region LanguageFile
private void Print(BasePlayer player, string message)
{
Player.Message(player, message, _configData.Chat.Prefix, _configData.Chat.SteamIdIcon);
}
private string Lang(string key, string id = null, params object[] args)
{
try
{
return string.Format(lang.GetMessage(key, this, id), args);
}
catch (Exception)
{
PrintError($"Error in the language formatting of '{key}'. (userid: {id}. lang: {lang.GetLanguage(id)}. args: {string.Join(" ,", args)})");
throw;
}
}
protected override void LoadDefaultMessages()
{
lang.RegisterMessages(new Dictionary<string, string>
{
["NotAllowed"] = "<color=#FF1919>You do not have permission to use this command.</color>",
["UnknownGrade"] = "<color=#FF1919>Unknown grade.</color>",
["UnknownCategory"] = "<color=#FF1919>Unknown category.</color> Available categories: <color=#009EFF>{0}</color>",
["NotLookingAt"] = "<color=#FF1919>You are not looking at a building block.</color>",
["RaidBlocked"] = "<color=#FF1919>You may not do that while raid blocked.</color>.",
["CombatBlocked"] = "<color=#FF1919>You may not do that while combat blocked.</color>.",
["OnCooldown"] = "You must wait <color=#FF1919>{0}</color> seconds before you can use this command.",
["BuildingBlocked"] = "You can't use this command if you don't have the building privileges.",
["MissingItemsFormat"] = "* <color=#FF1919>{0}</color> x{1}",
["StartUpgrade"] = "Start running upgrade, please wait.",
["StartDowngrade"] = "Start running downgrade, please wait.",
["AlreadyProcess"] = "There is already a process already running, please wait.",
["UpgradeNotEnoughItems"] = "None of the buildings were upgraded. You are missing: \n{0}",
["UpgradeNotEnoughItemsSuccess"] = "<color=#FF1919>{0}</color> building blocks were upgraded. Some buildings cannot be upgraded, you are missing: \n{1}",
["NotUpgraded"] = "None of the buildings were upgraded.",
["NotDowngraded"] = "None of the buildings were downgraded.",
["FinishedUpgrade"] = "<color=#FF1919>{0}</color> building blocks were upgraded.",
["FinishedDowngrade"] = "<color=#FF1919>{0}</color> building blocks were downgraded."
}, this);
lang.RegisterMessages(new Dictionary<string, string>
{
["NotAllowed"] = "<color=#FF1919>您没有使用该命令的权限</color>",
["UnknownGrade"] = "<color=#FF1919>未知的建筑等级</color>",
["UnknownCategory"] = "<color=#FF1919>未知的建筑类型</color> 可用的建筑类型: <color=#009EFF>{0}</color>",
["NotLookingAt"] = "<color=#FF1919>您需要看着一个建筑</color>",
["RaidBlocked"] = "<color=#FF1919>您被突袭阻止了,不能使用该命令</color>",
["CombatBlocked"] = "<color=#FF1919>您被战斗阻止了,不能使用该命令</color>",
["OnCooldown"] = "您必须等待 <color=#FF1919>{0}</color> 秒后才能使用该命令",
["BuildingBlocked"] = "您不能在没有领地权的地方使用该命令",
["MissingItemsFormat"] = "* <color=#FF1919>{0}</color> x{1}",
["StartUpgrade"] = "开始运行升级您的建筑,请稍等片刻",
["StartDowngrade"] = "开始运行降级您的建筑,请稍等片刻",
["AlreadyProcess"] = "已经有一个线程正在运行,请稍等片刻",
["UpgradeNotEnoughItems"] = "没有可以升级的建筑。您还需要: \n{0}",
["UpgradeNotEnoughItemsSuccess"] = "<color=#FF1919>{0}</color> 个建筑成功升级了。有些建筑无法升级,您还需要: \n{1}",
["NotUpgraded"] = "没有可以升级的建筑",
["NotDowngraded"] = "没有可以降级的建筑",
["FinishedUpgrade"] = "<color=#FF1919>{0}</color> 个建筑成功升级了",
["FinishedDowngrade"] = "<color=#FF1919>{0}</color> 个建筑成功降级了"
}, this, "zh-CN");
lang.RegisterMessages(new Dictionary<string, string>
{
["NotAllowed"] = "<color=#FF1919>Ви не маєте дозволу на використання цієї команди.</color>",
["UnknownGrade"] = "<color=#FF1919>Невідоме поліпшення.</color>",
["UnknownCategory"] = "<color=#FF1919>Невідома категорія.</color> Доступні категорії: <color=#009EFF>{0}</color>",
["NotLookingAt"] = "<color=#FF1919>Ви не дивитеся на будівельний блок.</color>",
["RaidBlocked"] = "<color=#FF1919>Ви не можете цього робити, поки рейд заблоковано.</color>.",
["CombatBlocked"] = "<color=#FF1919>Ви не можете цього робити, коли бій заблоковано.</color>.",
["OnCooldown"] = "Ви повинні почекати <color=#FF1919>{0}</color> секунд, перш ніж ви зможете використовувати цю команду.",
["BuildingBlocked"] = "Ви не можете використовувати цю команду, якщо у вас немає привілеїв будівництва.",
["MissingItemsFormat"] = "* <color=#FF1919>{0}</color> x{1}",
["StartUpgrade"] = "Почніть запуск оновлення, зачекайте.",
["StartDowngrade"] = "Почніть роботу з пониженням, зачекайте.",
["AlreadyProcess"] = "Процес уже запущений, зачекайте.",
["UpgradeNotEnoughItems"] = "Жодна з будівель не була модернізована. Ви пропали: \n{0}",
["UpgradeNotEnoughItemsSuccess"] = "<color=#FF1919>{0}</color> будівельні блоки були модернізовані. Деякі будівлі неможливо покращити, ви пропали: \n{1}",
["NotUpgraded"] = "Жодна з будівель не була модернізована.",
["NotDowngraded"] = "Жодна з будівель не була понижена.",
["FinishedUpgrade"] = "<color=#FF1919>{0}</color> будівельні блоки були модернізовані.",
["FinishedDowngrade"] = "<color=#FF1919>{0}</color> будівельні блоки були знижені."
}, this, "uk");
}
#endregion LanguageFile
}
}
using System;
using System.Collections.Generic;
using Facepunch;
using Newtonsoft.Json;
using Rust;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Dungeons", "Marte6", "1.0.0")]
public class Dungeons : RustPlugin
{
private ConfigData _configData;
private Timer _autoSpawnTimer;
private const string DungeonPrefabPath = "assets/prefabs/missions/portal/xmasportalentry.prefab";
private XmasDungeon _currentDungeon;
private List<BaseEntity> _spawnedEntities = new List<BaseEntity>();
#region Configuration
private class ConfigData
{
[JsonProperty(PropertyName = "Version")]
public string Version { get; set; } = "1.0.0";
[JsonProperty(PropertyName = "Auto Spawn Settings")]
public AutoSpawnSettings AutoSpawn { get; set; } = new AutoSpawnSettings();
[JsonProperty(PropertyName = "NPC Spawn Configs", ObjectCreationHandling = ObjectCreationHandling.Replace)]
public List<NpcSpawnConfig> NpcSpawnConfigs { get; set; } = new List<NpcSpawnConfig>
{
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/tunneldweller/npc_tunneldweller.prefab", EntitysPerCell = 2 },
new NpcSpawnConfig { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_heavy.prefab", EntitysPerCell = 2 },
};
[JsonProperty(PropertyName = "Auto Turret Configs", ObjectCreationHandling = ObjectCreationHandling.Replace)]
public List<AutoTurretConfig> AutoTurretConfigs { get; set; } = new List<AutoTurretConfig>
{
new AutoTurretConfig { PrefabName = "assets/prefabs/npc/autoturret/autoturret_deployed.prefab", EntitysPerCell = 2 },
};
}
private class AutoSpawnSettings
{
[JsonProperty(PropertyName = "Enable Auto Spawn")]
public bool EnableAutoSpawn { get; set; } = true;
[JsonProperty(PropertyName = "Auto Spawn Interval Minutes")]
public float AutoSpawnIntervalMinutes { get; set; } = 30f;
[JsonProperty(PropertyName = "Min Spawn Distance")]
public float MinSpawnDistance { get; set; } = 6f;
[JsonProperty(PropertyName = "Max Spawn Distance")]
public float MaxSpawnDistance { get; set; } = 20f;
[JsonProperty(PropertyName = "Max Spawn Attempts")]
public int MaxSpawnAttempts { get; set; } = 5;
}
private class NpcSpawnConfig
{
public string PrefabName { get; set; }
public int EntitysPerCell { get; set; }
}
private class AutoTurretConfig
{
public string PrefabName { get; set; }
public int EntitysPerCell { get; set; }
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
_configData = Config.ReadObject<ConfigData>();
if (_configData == null)
{
LoadDefaultConfig();
}
}
catch (Exception ex)
{
PrintError($"The configuration file is corrupted: {ex}");
LoadDefaultConfig();
}
SaveConfig();
}
protected override void LoadDefaultConfig()
{
PrintWarning("Creating a new configuration file");
_configData = new ConfigData();
}
protected override void SaveConfig()
{
Config.WriteObject(_configData, true);
}
#endregion Configuration
#region Initialization
private void Init()
{
if (_configData.AutoSpawn.EnableAutoSpawn)
{
StartAutoSpawnTimer();
}
}
#endregion Initialization
#region Dungeon Management
[ChatCommand("dun")]
private void CreateDungeonCommand(BasePlayer player, string command, string[] args)
{
RemoveExistingDungeon();
if (TryFindDungeonSpawnPoint(player.transform.position, out var position, out var rotation))
{
_currentDungeon = CreateDungeon(position, rotation);
player.ChatMessage("Christmas Dungeon created!");
}
else
{
player.ChatMessage("Could not find a suitable location for the Christmas Dungeon.");
}
}
private void StartAutoSpawnTimer()
{
_autoSpawnTimer = timer.Every(_configData.AutoSpawn.AutoSpawnIntervalMinutes * 60f, AutoSpawnDungeon);
}
private void AutoSpawnDungeon()
{
RemoveExistingDungeon();
for (var attempt = 0; attempt < _configData.AutoSpawn.MaxSpawnAttempts; attempt++)
{
foreach (var player in BasePlayer.activePlayerList)
{
if (!TryFindDungeonSpawnPoint(player.transform.position, out var position, out var rotation))
continue;
_currentDungeon = CreateDungeon(position, rotation);
NotifyPlayersOfDungeonLocation(position);
return;
}
}
Puts("Failed to auto-spawn dungeon after max attempts.");
}
private void NotifyPlayersOfDungeonLocation(Vector3 position)
{
foreach (var player in BasePlayer.activePlayerList)
{
player.ChatMessage($"A Christmas Dungeon has appeared at coordinates: ({position.x:F0}, {position.z:F0})!");
}
}
private XmasDungeon CreateDungeon(Vector3 position, Quaternion rotation)
{
var entity = GameManager.server.CreateEntity(DungeonPrefabPath, position, rotation) as XmasDungeon;
if (entity == null)
{
Puts("Failed to create dungeon entity.");
return null;
}
entity.Spawn();
IntegrateEntitiesIntoDungeon(entity);
return entity;
}
private void IntegrateEntitiesIntoDungeon(XmasDungeon dungeon)
{
var proceduralDungeon = dungeon.dungeonInstance.Get(serverside: true);
if (proceduralDungeon == null)
{
Puts("Failed to get procedural dungeon instance.");
return;
}
int cellCount = proceduralDungeon.spawnedCells.Count;
if (cellCount == 0)
{
Puts("No cells found in dungeon for entities.");
return;
}
var allSpawnEntries = new List<SpawnGroup.SpawnEntry>();
foreach (var config in _configData.NpcSpawnConfigs)
{
if (GameManifest.pathToGuid.TryGetValue(config.PrefabName, out var guid))
{
for (int i = 0; i < config.EntitysPerCell; i++)
{
allSpawnEntries.Add(new SpawnGroup.SpawnEntry
{
prefab = new GameObjectRef { guid = guid },
weight = 1,
mobile = true,
});
}
}
}
foreach (var config in _configData.AutoTurretConfigs)
{
if (GameManifest.pathToGuid.TryGetValue(config.PrefabName, out var guid))
{
for (int i = 0; i < config.EntitysPerCell; i++)
{
allSpawnEntries.Add(new SpawnGroup.SpawnEntry
{
prefab = new GameObjectRef { guid = guid },
weight = 1,
mobile = false,
});
}
}
}
foreach (var cell in proceduralDungeon.spawnedCells)
{
SetupSpawnGroup(cell, allSpawnEntries);
}
}
private void SetupSpawnGroup(ProceduralDungeonCell cell, List<SpawnGroup.SpawnEntry> allSpawnEntries)
{
var spawnGroup = cell.gameObject.AddComponent<SpawnGroup>();
spawnGroup.prefabs = allSpawnEntries;
spawnGroup.maxPopulation = 100;
spawnGroup.numToSpawnPerTickMin = 0;
spawnGroup.numToSpawnPerTickMax = 0;
spawnGroup.fillOnSpawn = true;
spawnGroup.wantsInitialSpawn = true;
spawnGroup.SpawnInitial();
foreach (var instance in spawnGroup.spawnInstances)
{
var entity = instance.GetComponent<BaseEntity>();
if (entity != null && !_spawnedEntities.Contains(entity))
{
_spawnedEntities.Add(entity);
Puts($"Entity {entity.ShortPrefabName} spawned at {entity.transform.position}");
if (entity is AutoTurret autoTurret)
{
ConfigureAutoTurret(autoTurret);
}
}
}
}
private void ConfigureAutoTurret(AutoTurret autoTurret)
{
autoTurret.health = 1000f;
var weapon = ItemManager.CreateByName("rifle.ak");
weapon?.MoveToContainer(autoTurret.inventory, 0);
var ammo = ItemManager.CreateByName("ammo.rifle", 128);
ammo.MoveToContainer(autoTurret.inventory);
autoTurret.UpdateTotalAmmo();
autoTurret.EnsureReloaded();
autoTurret.InitializeHealth(1000f, 1000f);
autoTurret.SetPeacekeepermode(false);
}
private Vector3 GetRandomPositionNearby(Vector3 center, float minRadius, float maxRadius)
{
Vector2 randomCircle = UnityEngine.Random.insideUnitCircle * UnityEngine.Random.Range(minRadius, maxRadius);
return new Vector3(center.x + randomCircle.x, center.y, center.z + randomCircle.y);
}
#endregion Dungeon Management
#region Utility and Cleanup
private bool TryFindDungeonSpawnPoint(Vector3 center, out Vector3 suitablePosition, out Quaternion suitableRotation)
{
suitablePosition = Vector3.zero;
suitableRotation = Quaternion.identity;
for (var attempt = 0; attempt < _configData.AutoSpawn.MaxSpawnAttempts; attempt++)
{
var potentialPosition = TerrainUtilities.GetRandomPosition(center, _configData.AutoSpawn.MinSpawnDistance, _configData.AutoSpawn.MaxSpawnDistance);
if (!TerrainUtilities.TryGetTerrainInfo(potentialPosition, 5f, out var hitInfo) || !IsPositionSuitable(potentialPosition))
{
continue;
}
suitablePosition = hitInfo.point;
suitableRotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
return true;
}
return false;
}
private bool IsPositionSuitable(Vector3 position)
{
return !TerrainUtilities.ContainsRock(position, 2f)
&& !TerrainUtilities.IsInRadTown(position)
&& !TerrainUtilities.ContainsEntitiesNearby(position, 4f, TerrainUtilities.EntityLayerMask)
&& !TerrainUtilities.IsInWater(position)
&& !TerrainUtilities.IsOnRoadOrRail(position)
&& !TerrainUtilities.IsInNoBuildZone(position, 2f);
}
private void RemoveExistingDungeon()
{
if (_currentDungeon != null)
{
foreach (var entity in _spawnedEntities)
{
if (entity != null && !entity.IsDestroyed)
{
entity.Kill(BaseNetworkable.DestroyMode.None);
}
}
_spawnedEntities.Clear();
_currentDungeon.Kill(BaseNetworkable.DestroyMode.None);
_currentDungeon = null;
}
}
private void Unload()
{
RemoveExistingDungeon();
_autoSpawnTimer?.Destroy();
}
#endregion Utility and Cleanup
}
public static class TerrainUtilities
{
public const int EntityLayerMask = Layers.Mask.Deployed | Layers.Mask.Construction;
public const int GroundLayerMask = Layers.Mask.Terrain | Layers.Mask.World | Layers.Mask.Default;
public static bool OnTopology(Vector3 position, TerrainTopology.Enum topology)
{
return (TerrainMeta.TopologyMap.GetTopology(position) & (int)topology) != 0;
}
public static bool IsOnRoadOrRail(Vector3 position)
{
return OnTopology(position, TerrainTopology.Enum.Road | TerrainTopology.Enum.Roadside | TerrainTopology.Enum.Rail | TerrainTopology.Enum.Railside);
}
public static bool IsInNoBuildZone(Vector3 position, float radius)
{
return Physics.CheckSphere(position, radius, Layers.Mask.Prevent_Building);
}
public static bool IsInWater(Vector3 position)
{
return WaterLevel.Test(position, false, true);
}
public static bool ContainsRock(Vector3 position, float radius)
{
var colliders = Pool.GetList<Collider>();
Vis.Colliders(position, radius, colliders, GroundLayerMask, QueryTriggerInteraction.Ignore);
bool result = colliders.Exists(collider =>
collider.name.Contains("rock", StringComparison.OrdinalIgnoreCase)
|| collider.name.Contains("cliff", StringComparison.OrdinalIgnoreCase)
|| collider.name.Contains("formation", StringComparison.OrdinalIgnoreCase)
);
Pool.FreeList(ref colliders);
return result;
}
public static bool IsInRadTown(Vector3 position)
{
foreach (var monumentInfo in TerrainMeta.Path.Monuments)
{
if (monumentInfo.IsInBounds(position) && monumentInfo.shouldDisplayOnMap)
{
return true;
}
}
return OnTopology(position, TerrainTopology.Enum.Monument);
}
public static bool ContainsEntitiesNearby(Vector3 position, float radius, int mask)
{
var hitColliders = Pool.GetList<Collider>();
GamePhysics.OverlapSphere(position, radius, hitColliders, mask, QueryTriggerInteraction.Ignore);
bool result = hitColliders.Exists(collider => collider.gameObject.ToBaseEntity() != null);
Pool.FreeList(ref hitColliders);
return result;
}
public static bool TryGetTerrainInfo(Vector3 startPosition, float range, out RaycastHit hitInfo)
{
return Physics.Linecast(startPosition + new Vector3(0f, range, 0f), startPosition - new Vector3(0f, range, 0f), out hitInfo, GroundLayerMask);
}
public static Vector3 GetRandomPosition(Vector3 center, float minRadius, float maxRadius)
{
Vector3 randomDirection = UnityEngine.Random.onUnitSphere;
randomDirection.y = 0;
float randomDistance = UnityEngine.Random.Range(minRadius, maxRadius);
Vector3 randomPosition = center + randomDirection * randomDistance;
randomPosition.y = TerrainMeta.HeightMap.GetHeight(randomPosition);
return randomPosition;
}
}
}
Key Features of the Updated Configuration
Config Structure: Implements a class-based configuration with default values that prevent duplication.
ObjectCreationHandling.Replace: Used in the JSON property attributes to ensure that lists and dictionaries are replaced upon loading, avoiding duplication.
Consistent Configuration Management: Similar to Building Grades, this approach provides a structured and consistent configuration management style.
Versioning: Basic versioning is included to manage future updates easily.
This setup aligns with the design principles used in the Building Grades plugin and addresses the issues you've encountered with configuration duplication. Ensure to test and verify each configuration entry during initial execution to prevent issues. If there's any version mismatch or corruption, the configuration will reset to defaults.
#region Configuration
private class ConfigData
{
[JsonProperty(PropertyName = "Version")]
public string Version { get; set; } = "1.0.0";
[JsonProperty(PropertyName = "Auto Spawn Settings")]
public AutoSpawnSettings AutoSpawn { get; set; } = new AutoSpawnSettings();
[JsonProperty(PropertyName = "NPC Spawn Configs", ObjectCreationHandling = ObjectCreationHandling.Replace)]
public List<NpcSpawnConfig> NpcSpawnConfigs { get; set; } =
new List<NpcSpawnConfig>
{
new() { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/tunneldweller/npc_tunneldweller.prefab", EntitysPerCell = 2 },
new() { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_heavy.prefab", EntitysPerCell = 2 },
};
[JsonProperty(PropertyName = "Auto Turret Configs", ObjectCreationHandling = ObjectCreationHandling.Replace)]
public List<AutoTurretConfig> AutoTurretConfigs { get; set; } =
new List<AutoTurretConfig>
{
new() { PrefabName = "assets/prefabs/npc/autoturret/autoturret_deployed.prefab", EntitysPerCell = 2 },
};
}
private class AutoSpawnSettings
{
[JsonProperty(PropertyName = "Enable Auto Spawn")]
public bool EnableAutoSpawn { get; set; } = true;
[JsonProperty(PropertyName = "Auto Spawn Interval Minutes")]
public float AutoSpawnIntervalMinutes { get; set; } = 30f;
[JsonProperty(PropertyName = "Min Spawn Distance")]
public float MinSpawnDistance { get; set; } = 6f;
[JsonProperty(PropertyName = "Max Spawn Distance")]
public float MaxSpawnDistance { get; set; } = 20f;
[JsonProperty(PropertyName = "Max Spawn Attempts")]
public int MaxSpawnAttempts { get; set; } = 5;
}
private class NpcSpawnConfig
{
[JsonProperty("Prefab")]
public string PrefabName { get; set; }
[JsonProperty("Entitys Per Cell")]
public int EntitysPerCell { get; set; }
}
private class AutoTurretConfig
{
[JsonProperty("Prefab")]
public string PrefabName { get; set; }
[JsonProperty("Entitys Per Cell")]
public int EntitysPerCell { get; set; }
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
_configData = Config.ReadObject<ConfigData>();
if (_configData == null)
{
LoadDefaultConfig();
}
}
catch (Exception ex)
{
PrintError($"The configuration file is corrupted: {ex}");
LoadDefaultConfig();
}
SaveConfig();
}
protected override void LoadDefaultConfig()
{
PrintWarning("Creating a new configuration file");
_configData = new ConfigData();
}
protected override void SaveConfig()
{
Config.WriteObject(_configData, true);
}
#endregion Configuration
using System;
using System.Collections.Generic;
using Facepunch;
using Newtonsoft.Json;
using Rust;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Dungeons", "Marte6", "1.0.0")]
public class Dungeons : RustPlugin
{
private ConfigData _configData;
private Timer _autoSpawnTimer;
private const string DungeonPrefabPath = "assets/prefabs/missions/portal/xmasportalentry.prefab";
private XmasDungeon _currentDungeon;
private List<BaseEntity> _spawnedEntities = new List<BaseEntity>();
#region Configuration
private class ConfigData
{
[JsonProperty(PropertyName = "Version")]
public string Version { get; set; } = "1.0.0";
[JsonProperty(PropertyName = "Auto Spawn Settings")]
public AutoSpawnSettings AutoSpawn { get; set; } = new AutoSpawnSettings();
// Using the same SpawnConfig class for both NPCs and turrets.
[JsonProperty(PropertyName = "NPC Spawn Configs", ObjectCreationHandling = ObjectCreationHandling.Replace)]
public List<SpawnConfig> NpcSpawnConfigs { get; set; } = new List<SpawnConfig>
{
new() { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/tunneldweller/npc_tunneldweller.prefab", EntitysPerCell = 2 },
new() { PrefabName = "assets/rust.ai/agents/npcplayer/humannpc/scientist/scientistnpc_heavy.prefab", EntitysPerCell = 2 },
};
[JsonProperty(PropertyName = "Auto Turret Configs", ObjectCreationHandling = ObjectCreationHandling.Replace)]
public List<SpawnConfig> AutoTurretConfigs { get; set; } = new List<SpawnConfig>
{
new() { PrefabName = "assets/prefabs/npc/autoturret/autoturret_deployed.prefab", EntitysPerCell = 2 },
};
}
private class AutoSpawnSettings
{
[JsonProperty(PropertyName = "Enable Auto Spawn")]
public bool EnableAutoSpawn { get; set; } = true;
[JsonProperty(PropertyName = "Auto Spawn Interval Minutes")]
public float AutoSpawnIntervalMinutes { get; set; } = 30f;
[JsonProperty(PropertyName = "Min Spawn Distance")]
public float MinSpawnDistance { get; set; } = 6f;
[JsonProperty(PropertyName = "Max Spawn Distance")]
public float MaxSpawnDistance { get; set; } = 20f;
[JsonProperty(PropertyName = "Max Spawn Attempts")]
public int MaxSpawnAttempts { get; set; } = 5;
}
// Combined class for both NPC and turret configurations
private class SpawnConfig
{
[JsonProperty("Prefab")]
public string PrefabName { get; set; }
[JsonProperty("Entitys Per Cell")]
public int EntitysPerCell { get; set; }
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
_configData = Config.ReadObject<ConfigData>();
if (_configData == null)
{
LoadDefaultConfig();
}
}
catch (Exception ex)
{
PrintError($"The configuration file is corrupted: {ex}");
LoadDefaultConfig();
}
SaveConfig();
}
protected override void LoadDefaultConfig()
{
PrintWarning("Creating a new configuration file");
_configData = new ConfigData();
}
protected override void SaveConfig()
{
Config.WriteObject(_configData, true);
}
#endregion Configuration
#region Initialization
private void Init()
{
if (_configData.AutoSpawn.EnableAutoSpawn)
{
StartAutoSpawnTimer();
}
}
#endregion Initialization
#region Dungeon Management
[ChatCommand("dun")]
private void CreateDungeonCommand(BasePlayer player, string command, string[] args)
{
RemoveExistingDungeon();
if (TryFindDungeonSpawnPoint(player.transform.position, out var position, out var rotation))
{
_currentDungeon = CreateDungeon(position, rotation);
player.ChatMessage("Christmas Dungeon created!");
}
else
{
player.ChatMessage("Could not find a suitable location for the Christmas Dungeon.");
}
}
private void StartAutoSpawnTimer()
{
_autoSpawnTimer = timer.Every(_configData.AutoSpawn.AutoSpawnIntervalMinutes * 60f, AutoSpawnDungeon);
}
private void AutoSpawnDungeon()
{
RemoveExistingDungeon();
for (var attempt = 0; attempt < _configData.AutoSpawn.MaxSpawnAttempts; attempt++)
{
foreach (var player in BasePlayer.activePlayerList)
{
if (!TryFindDungeonSpawnPoint(player.transform.position, out var position, out var rotation))
continue;
_currentDungeon = CreateDungeon(position, rotation);
NotifyPlayersOfDungeonLocation(position);
return;
}
}
Puts("Failed to auto-spawn dungeon after max attempts.");
}
private void NotifyPlayersOfDungeonLocation(Vector3 position)
{
foreach (var player in BasePlayer.activePlayerList)
{
player.ChatMessage($"A Christmas Dungeon has appeared at coordinates: ({position.x:F0}, {position.z:F0})!");
}
}
private XmasDungeon CreateDungeon(Vector3 position, Quaternion rotation)
{
var entity = GameManager.server.CreateEntity(DungeonPrefabPath, position, rotation) as XmasDungeon;
if (entity == null)
{
Puts("Failed to create dungeon entity.");
return null;
}
entity.Spawn();
IntegrateEntitiesIntoDungeon(entity);
return entity;
}
private void IntegrateEntitiesIntoDungeon(XmasDungeon dungeon)
{
var proceduralDungeon = dungeon.dungeonInstance.Get(serverside: true);
if (proceduralDungeon == null)
{
Puts("Failed to get procedural dungeon instance.");
return;
}
int cellCount = proceduralDungeon.spawnedCells.Count;
if (cellCount == 0)
{
Puts("No cells found in dungeon for entities.");
return;
}
var allSpawnEntries = new List<SpawnGroup.SpawnEntry>();
foreach (var config in _configData.NpcSpawnConfigs)
{
if (GameManifest.pathToGuid.TryGetValue(config.PrefabName, out var guid))
{
for (int i = 0; i < config.EntitysPerCell; i++)
{
allSpawnEntries.Add(new SpawnGroup.SpawnEntry
{
prefab = new GameObjectRef { guid = guid },
weight = 1,
mobile = true,
});
}
}
}
foreach (var config in _configData.AutoTurretConfigs)
{
if (GameManifest.pathToGuid.TryGetValue(config.PrefabName, out var guid))
{
for (int i = 0; i < config.EntitysPerCell; i++)
{
allSpawnEntries.Add(new SpawnGroup.SpawnEntry
{
prefab = new GameObjectRef { guid = guid },
weight = 1,
mobile = false,
});
}
}
}
foreach (var cell in proceduralDungeon.spawnedCells)
{
SetupSpawnGroup(cell, allSpawnEntries);
}
}
private void SetupSpawnGroup(ProceduralDungeonCell cell, List<SpawnGroup.SpawnEntry> allSpawnEntries)
{
var spawnGroup = cell.gameObject.AddComponent<SpawnGroup>();
spawnGroup.prefabs = allSpawnEntries;
spawnGroup.maxPopulation = 100;
spawnGroup.numToSpawnPerTickMin = 0;
spawnGroup.numToSpawnPerTickMax = 0;
spawnGroup.fillOnSpawn = true;
spawnGroup.wantsInitialSpawn = true;
spawnGroup.SpawnInitial();
foreach (var instance in spawnGroup.spawnInstances)
{
var entity = instance.GetComponent<BaseEntity>();
if (entity != null && !_spawnedEntities.Contains(entity))
{
_spawnedEntities.Add(entity);
Puts($"Entity {entity.ShortPrefabName} spawned at {entity.transform.position}");
if (entity is AutoTurret autoTurret)
{
ConfigureAutoTurret(autoTurret);
}
}
}
}
private void ConfigureAutoTurret(AutoTurret autoTurret)
{
autoTurret.health = 1000f;
var weapon = ItemManager.CreateByName("rifle.ak");
weapon?.MoveToContainer(autoTurret.inventory, 0);
var ammo = ItemManager.CreateByName("ammo.rifle", 128);
ammo.MoveToContainer(autoTurret.inventory);
autoTurret.UpdateTotalAmmo();
autoTurret.EnsureReloaded();
autoTurret.InitializeHealth(1000f, 1000f);
autoTurret.SetPeacekeepermode(false);
}
private Vector3 GetRandomPositionNearby(Vector3 center, float minRadius, float maxRadius)
{
Vector2 randomCircle = UnityEngine.Random.insideUnitCircle * UnityEngine.Random.Range(minRadius, maxRadius);
return new Vector3(center.x + randomCircle.x, center.y, center.z + randomCircle.y);
}
#endregion Dungeon Management
#region Utility and Cleanup
private bool TryFindDungeonSpawnPoint(Vector3 center, out Vector3 suitablePosition, out Quaternion suitableRotation)
{
suitablePosition = Vector3.zero;
suitableRotation = Quaternion.identity;
for (var attempt = 0; attempt < _configData.AutoSpawn.MaxSpawnAttempts; attempt++)
{
var potentialPosition = TerrainUtilities.GetRandomPosition(center, _configData.AutoSpawn.MinSpawnDistance, _configData.AutoSpawn.MaxSpawnDistance);
if (!TerrainUtilities.TryGetTerrainInfo(potentialPosition, 5f, out var hitInfo) || !IsPositionSuitable(potentialPosition))
{
continue;
}
suitablePosition = hitInfo.point;
suitableRotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
return true;
}
return false;
}
private bool IsPositionSuitable(Vector3 position)
{
return !TerrainUtilities.ContainsRock(position, 2f)
&& !TerrainUtilities.IsInRadTown(position)
&& !TerrainUtilities.ContainsEntitiesNearby(position, 4f, TerrainUtilities.EntityLayerMask)
&& !TerrainUtilities.IsInWater(position)
&& !TerrainUtilities.IsOnRoadOrRail(position)
&& !TerrainUtilities.IsInNoBuildZone(position, 2f);
}
private void RemoveExistingDungeon()
{
if (_currentDungeon != null)
{
foreach (var entity in _spawnedEntities)
{
if (entity != null && !entity.IsDestroyed)
{
entity.Kill(BaseNetworkable.DestroyMode.None);
}
}
_spawnedEntities.Clear();
_currentDungeon.Kill(BaseNetworkable.DestroyMode.None);
_currentDungeon = null;
}
}
private void Unload()
{
RemoveExistingDungeon();
_autoSpawnTimer?.Destroy();
}
#endregion Utility and Cleanup
}
public static class TerrainUtilities
{
public const int EntityLayerMask = Layers.Mask.Deployed | Layers.Mask.Construction;
public const int GroundLayerMask = Layers.Mask.Terrain | Layers.Mask.World | Layers.Mask.Default;
public static bool OnTopology(Vector3 position, TerrainTopology.Enum topology)
{
return (TerrainMeta.TopologyMap.GetTopology(position) & (int)topology) != 0;
}
public static bool IsOnRoadOrRail(Vector3 position)
{
return OnTopology(position, TerrainTopology.Enum.Road | TerrainTopology.Enum.Roadside | TerrainTopology.Enum.Rail | TerrainTopology.Enum.Railside);
}
public static bool IsInNoBuildZone(Vector3 position, float radius)
{
return Physics.CheckSphere(position, radius, Layers.Mask.Prevent_Building);
}
public static bool IsInWater(Vector3 position)
{
return WaterLevel.Test(position, false, true);
}
public static bool ContainsRock(Vector3 position, float radius)
{
var colliders = Pool.GetList<Collider>();
Vis.Colliders(position, radius, colliders, GroundLayerMask, QueryTriggerInteraction.Ignore);
bool result = colliders.Exists(collider =>
collider.name.Contains("rock", StringComparison.OrdinalIgnoreCase)
|| collider.name.Contains("cliff", StringComparison.OrdinalIgnoreCase)
|| collider.name.Contains("formation", StringComparison.OrdinalIgnoreCase)
);
Pool.FreeList(ref colliders);
return result;
}
public static bool IsInRadTown(Vector3 position)
{
foreach (var monumentInfo in TerrainMeta.Path.Monuments)
{
if (monumentInfo.IsInBounds(position) && monumentInfo.shouldDisplayOnMap)
{
return true;
}
}
return OnTopology(position, TerrainTopology.Enum.Monument);
}
public static bool ContainsEntitiesNearby(Vector3 position, float radius, int mask)
{
var hitColliders = Pool.GetList<Collider>();
GamePhysics.OverlapSphere(position, radius, hitColliders, mask, QueryTriggerInteraction.Ignore);
bool result = hitColliders.Exists(collider => collider.gameObject.ToBaseEntity() != null);
Pool.FreeList(ref hitColliders);
return result;
}
public static bool TryGetTerrainInfo(Vector3 startPosition, float range, out RaycastHit hitInfo)
{
return Physics.Linecast(startPosition + new Vector3(0f, range, 0f), startPosition - new Vector3(0f, range, 0f), out hitInfo, GroundLayerMask);
}
public static Vector3 GetRandomPosition(Vector3 center, float minRadius, float maxRadius)
{
Vector3 randomDirection = UnityEngine.Random.onUnitSphere;
randomDirection.y = 0;
float randomDistance = UnityEngine.Random.Range(minRadius, maxRadius);
Vector3 randomPosition = center + randomDirection * randomDistance;
randomPosition.y = TerrainMeta.HeightMap.GetHeight(randomPosition);
return randomPosition;
}
}
}
Explanation
SpawnConfig Unified: A single class SpawnConfig is used to encapsulate both NPC and turret configurations since they share the same structure. This reduces redundancy.
List Initialization: When initializing the lists of spawn configurations (NpcSpawnConfigs and AutoTurretConfigs), the SpawnConfig class is utilized to handle both cases.
This approach streamlines your code by eliminating repeated class definitions while maintaining clarity and functionality.