C#:
/*
* Copyright (C) 2024 Game4Freak.io
* This mod is provided under the Game4Freak EULA.
* Full legal terms can be found at https://game4freak.io/eula/
*/
using Newtonsoft.Json;
using Oxide.Core.Plugins;
using System.Collections.Generic;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Rocket Damage Modifier", "VisEntities", "1.1.0")]
[Description("Fine-tune exactly how much damage each rocket type deals to players versus structures.")]
public class RocketDamageModifier : RustPlugin
{
#region 3rd Party Dependencies
[PluginReference]
private readonly Plugin RaidableBases;
#endregion 3rd Party Dependencies
#region Fields
private static RocketDamageModifier _plugin;
private static Configuration _config;
#endregion Fields
#region Configuration
private class Configuration
{
[JsonProperty("Version")]
public string Version { get; set; }
[JsonProperty("Only Modify If Fired By Player")]
public bool OnlyModifyIfFiredByPlayer { get; set; }
[JsonProperty("Only Apply In Raidable Bases Zones")]
public bool OnlyApplyInRaidableBasesZones { get; set; }
[JsonProperty("Rockets")]
public Dictionary<string, RocketDamageConfig> Rockets { get; set; }
}
private class RocketDamageConfig
{
[JsonProperty("Player Damage Override Total (-1 = use multiplier)")]
public float PlayerDamageOverrideTotal { get; set; }
[JsonProperty("Player Damage Multiplier (1.0 = vanilla)")]
public float PlayerDamageMultiplier { get; set; }
[JsonProperty("Non Player Damage Override Total (-1 = use multiplier)")]
public float NonPlayerDamageOverrideTotal { get; set; }
[JsonProperty("Non Player Damage Multiplier (1.0 = vanilla)")]
public float NonPlayerDamageMultiplier { get; set; }
}
protected override void LoadConfig()
{
base.LoadConfig();
_config = Config.ReadObject<Configuration>();
if (string.Compare(_config.Version, Version.ToString()) < 0)
UpdateConfig();
SaveConfig();
}
protected override void LoadDefaultConfig()
{
_config = GetDefaultConfig();
}
protected override void SaveConfig()
{
Config.WriteObject(_config, true);
}
private void UpdateConfig()
{
PrintWarning("Config changes detected! Updating...");
Configuration defaultConfig = GetDefaultConfig();
if (string.Compare(_config.Version, "1.0.0") < 0)
_config = defaultConfig;
if (string.Compare(_config.Version, "1.1.0") < 0)
_config.OnlyApplyInRaidableBasesZones = defaultConfig.OnlyApplyInRaidableBasesZones;
PrintWarning("Config update complete! Updated from version " + _config.Version + " to " + Version.ToString());
_config.Version = Version.ToString();
}
private Configuration GetDefaultConfig()
{
return new Configuration
{
Version = Version.ToString(),
OnlyModifyIfFiredByPlayer = true,
OnlyApplyInRaidableBasesZones = false,
Rockets = new Dictionary<string, RocketDamageConfig>
{
["rocket_basic"] = new RocketDamageConfig
{
PlayerDamageOverrideTotal = -1f,
PlayerDamageMultiplier = 1.0f,
NonPlayerDamageOverrideTotal = -1f,
NonPlayerDamageMultiplier = 1.0f
},
["rocket_hv"] = new RocketDamageConfig
{
PlayerDamageOverrideTotal = -1f,
PlayerDamageMultiplier = 1.0f,
NonPlayerDamageOverrideTotal = -1f,
NonPlayerDamageMultiplier = 1.0f
},
["rocket_fire"] = new RocketDamageConfig
{
PlayerDamageOverrideTotal = -1f,
PlayerDamageMultiplier = 1.0f,
NonPlayerDamageOverrideTotal = -1f,
NonPlayerDamageMultiplier = 1.0f
}
}
};
}
#endregion Configuration
#region Oxide Hooks
private void Init()
{
_plugin = this;
}
private void Unload()
{
_config = null;
_plugin = null;
}
private void OnEntityTakeDamage(BaseCombatEntity entity, HitInfo damageInfo)
{
if (entity == null || damageInfo == null)
return;
if (damageInfo.damageTypes == null)
return;
if (_config.Rockets == null || _config.Rockets.Count == 0)
return;
if (_config.OnlyModifyIfFiredByPlayer)
{
BasePlayer attacker = damageInfo.InitiatorPlayer;
if (attacker == null || attacker.IsNpc || !attacker.userID.IsSteamId())
return;
}
string shortName = null;
if (damageInfo.WeaponPrefab != null)
shortName = damageInfo.WeaponPrefab.ShortPrefabName;
if (string.IsNullOrEmpty(shortName))
return;
RocketDamageConfig rocketDamage;
if (!_config.Rockets.TryGetValue(shortName, out rocketDamage) || rocketDamage == null)
return;
if (_config.OnlyApplyInRaidableBasesZones)
{
if (RaidableBases == null || !RaidableBases.IsLoaded)
return;
if (!System.Convert.ToBoolean(RaidableBases.Call("EventTerritory", entity.transform.position)))
return;
}
float currentTotal = damageInfo.damageTypes.Total();
if (currentTotal <= 0f)
return;
BasePlayer player = entity as BasePlayer;
float scale = 1f;
if (player != null)
{
if (rocketDamage.PlayerDamageOverrideTotal >= 0f)
{
scale = rocketDamage.PlayerDamageOverrideTotal / currentTotal;
}
else
{
float multiplier = rocketDamage.PlayerDamageMultiplier;
if (multiplier < 0f)
return;
scale = multiplier;
}
}
else
{
if (rocketDamage.NonPlayerDamageOverrideTotal >= 0f)
{
scale = rocketDamage.NonPlayerDamageOverrideTotal / currentTotal;
}
else
{
float multiplier = rocketDamage.NonPlayerDamageMultiplier;
if (multiplier < 0f)
return;
scale = multiplier;
}
}
if (Mathf.Approximately(scale, 1f))
return;
damageInfo.damageTypes.ScaleAll(scale);
}
#endregion Oxide Hooks
}
}