|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +using Terraria; |
| 8 | +using Terraria.ModLoader; |
| 9 | +using Terraria.ID; |
| 10 | +using System.Reflection; |
| 11 | +using Microsoft.Xna.Framework; |
| 12 | +using System.Security.Cryptography; |
| 13 | + |
| 14 | +namespace ElementalWeaponEnhancements |
| 15 | +{ |
| 16 | + public class ElementalFramework |
| 17 | + { |
| 18 | + internal static Mod mod |
| 19 | + { |
| 20 | + get { return ModLoader.GetMod("ElementalWeaponEnhancements"); } |
| 21 | + } |
| 22 | + |
| 23 | + internal class Data |
| 24 | + { |
| 25 | + internal const double rollChancePickup = 0.25; |
| 26 | + internal const double rollChanceReforge = 0.25; |
| 27 | + internal const double rollChanceCraft = 0.25; |
| 28 | + |
| 29 | + internal static List<Tuple<Mod, string, string, Color>> elementData = new List<Tuple<Mod, string, string, Color>>(); |
| 30 | + internal static List<string> elementDisplayName = new List<string>(); |
| 31 | + |
| 32 | + internal static List<Tuple<Behaviour.OnHitNPC, Behaviour.OnHitPVP, Behaviour.ProjectileOnHitNPC, Behaviour.ProjectileOnHitPVP>> elementOnHit = new List<Tuple<Behaviour.OnHitNPC, Behaviour.OnHitPVP, Behaviour.ProjectileOnHitNPC, Behaviour.ProjectileOnHitPVP>>(); |
| 33 | + internal static List<Tuple<Behaviour.ModifyHitNPC, Behaviour.ModifyHitPVP, Behaviour.ProjectileModifyHitNPC, Behaviour.ProjectileModifyHitPVP>> elementModifyHit = new List<Tuple<Behaviour.ModifyHitNPC, Behaviour.ModifyHitPVP, Behaviour.ProjectileModifyHitNPC, Behaviour.ProjectileModifyHitPVP>>(); |
| 34 | + |
| 35 | + internal static List<Info.CalculateDamage> elementCalculateDamage = new List<Info.CalculateDamage>(); |
| 36 | + internal static List<Tuple<bool, double, double, double>> elementRollChance = new List<Tuple<bool, double, double, double>>(); // pickup reforge craft |
| 37 | + internal static List<int> elementWeight = new List<int>(); |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Contains behaviour delegates |
| 42 | + /// </summary> |
| 43 | + public class Behaviour |
| 44 | + { |
| 45 | + public delegate void OnHitNPC(Item item, Player player, NPC target, int damage, float knockBack, bool crit); |
| 46 | + public delegate void OnHitPVP(Item item, Player player, Player target, int damage, bool crit); |
| 47 | + public delegate Tuple<Item, Player, NPC, int, float, bool, bool> ModifyHitNPC(Item item, Player player, NPC target, int damage, float knockback, bool crit); |
| 48 | + public delegate Tuple<Item, Player, Player, int, bool, bool> ModifyHitPVP(Item item, Player player, Player target, int damage, bool crit); |
| 49 | + public delegate void ProjectileOnHitNPC(Projectile projectile, NPC target, int damage, float knockBack, bool crit); |
| 50 | + public delegate void ProjectileOnHitPVP(Projectile projectile, Player target, int damage, bool crit); |
| 51 | + public delegate Tuple<Projectile, NPC, int, float, bool, bool> ProjectileModifyHitNPC(Projectile projectile, NPC target, int damage, float knokback, bool crit); |
| 52 | + public delegate Tuple<Projectile, Player, int, bool, bool> ProjectileModifyHitPVP(Projectile projectile, Player target, int damage, bool crit); |
| 53 | + } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Contains various misc delegates |
| 57 | + /// </summary> |
| 58 | + public class Info |
| 59 | + { |
| 60 | + public delegate int CalculateDamage(int damage); |
| 61 | + } |
| 62 | + |
| 63 | + public static int CreateElement(Mod mod, string name, Color color) |
| 64 | + { |
| 65 | + if (mod == null || name == null || name.Length == 0) |
| 66 | + return -1; |
| 67 | + |
| 68 | + if (Data.elementData.FindIndex(x => x.Item3 == CreateIdentifier(mod, name)) == -1) |
| 69 | + { |
| 70 | + Data.elementData.Add(new Tuple<Mod, string, string, Color>(mod, CleanString(name), CreateIdentifier(mod, name), color)); |
| 71 | + Data.elementDisplayName.Add(null); |
| 72 | + Data.elementCalculateDamage.Add(null); |
| 73 | + Data.elementRollChance.Add(new Tuple<bool, double, double, double>(false, Data.rollChancePickup, Data.rollChanceReforge, Data.rollChanceCraft)); |
| 74 | + Data.elementWeight.Add(100); // vanilla weight |
| 75 | + Data.elementOnHit.Add(new Tuple<Behaviour.OnHitNPC, Behaviour.OnHitPVP, Behaviour.ProjectileOnHitNPC, Behaviour.ProjectileOnHitPVP>(null, null, null, null)); |
| 76 | + Data.elementModifyHit.Add(new Tuple<Behaviour.ModifyHitNPC, Behaviour.ModifyHitPVP, Behaviour.ProjectileModifyHitNPC, Behaviour.ProjectileModifyHitPVP>(null, null, null, null)); |
| 77 | + return GetElement(mod, name); |
| 78 | + } |
| 79 | + else return -1; |
| 80 | + } |
| 81 | + |
| 82 | + public static void CreateDisplayName(int type, string displayName) |
| 83 | + { |
| 84 | + if (type < 0 || type + 1 > Data.elementData.Count || displayName == null || displayName.Length == 0) |
| 85 | + return; |
| 86 | + |
| 87 | + Data.elementDisplayName[type] = CleanString(displayName); |
| 88 | + } |
| 89 | + |
| 90 | + public static void ModifyDamage(int type, Info.CalculateDamage _delegate) |
| 91 | + { |
| 92 | + if (type < 0 || type + 1 > Data.elementData.Count || _delegate == null) |
| 93 | + return; |
| 94 | + |
| 95 | + Data.elementCalculateDamage[type] = _delegate; |
| 96 | + } |
| 97 | + |
| 98 | + public static void SetWeight(int type, int weight = 100) |
| 99 | + { |
| 100 | + if (type < 0 || type + 1 > Data.elementData.Count) |
| 101 | + return; |
| 102 | + |
| 103 | + if (weight < 1) |
| 104 | + weight = 1; |
| 105 | + |
| 106 | + if (weight > 200) |
| 107 | + weight = 200; |
| 108 | + |
| 109 | + Data.elementWeight[type] = weight; |
| 110 | + } |
| 111 | + |
| 112 | + public static void CreateOnHitBehaviour(int type, Behaviour.OnHitNPC _onHitNPC, Behaviour.OnHitPVP _onHitPVP = null, Behaviour.ProjectileOnHitNPC _projectileOnHitNPC = null, Behaviour.ProjectileOnHitPVP _projectileOnHitPVP = null) |
| 113 | + { |
| 114 | + if (type < 0 || type + 1 > Data.elementData.Count) |
| 115 | + return; |
| 116 | + |
| 117 | + Data.elementOnHit[type] = new Tuple<Behaviour.OnHitNPC, Behaviour.OnHitPVP, Behaviour.ProjectileOnHitNPC, Behaviour.ProjectileOnHitPVP>(_onHitNPC, _onHitPVP, _projectileOnHitNPC, _projectileOnHitPVP); |
| 118 | + } |
| 119 | + |
| 120 | + public static void CreateModifyHitBehaviour(int type, Behaviour.ModifyHitNPC _modifyHitNPC, Behaviour.ModifyHitPVP _modifyHitPVP = null, Behaviour.ProjectileModifyHitNPC _projectileModifyHitNPC = null, Behaviour.ProjectileModifyHitPVP _projectileModifyHitPVP = null) |
| 121 | + { |
| 122 | + if (type < 0 || type + 1 > Data.elementData.Count) |
| 123 | + return; |
| 124 | + |
| 125 | + Data.elementModifyHit[type] = new Tuple<Behaviour.ModifyHitNPC, Behaviour.ModifyHitPVP, Behaviour.ProjectileModifyHitNPC, Behaviour.ProjectileModifyHitPVP>(_modifyHitNPC, _modifyHitPVP, _projectileModifyHitNPC, _projectileModifyHitPVP); |
| 126 | + } |
| 127 | + |
| 128 | + public static List<int> GetElements(Mod mod = null) |
| 129 | + { |
| 130 | + if (!Data.elementData.Any()) |
| 131 | + return null; |
| 132 | + |
| 133 | + List<int> returnList = new List<int>(); |
| 134 | + List<Tuple<Mod, string, string, Color>> elementList = new List<Tuple<Mod, string, string, Color>>(Data.elementData); |
| 135 | + |
| 136 | + if (mod != null) |
| 137 | + { |
| 138 | + elementList = Data.elementData.TakeWhile(x => x.Item1 == mod).ToList(); |
| 139 | + } |
| 140 | + |
| 141 | + if (elementList.Any()) |
| 142 | + { |
| 143 | + foreach (var item in elementList) |
| 144 | + { |
| 145 | + returnList.Add(GetElement(item.Item1, item.Item2)); |
| 146 | + } |
| 147 | + |
| 148 | + return returnList; |
| 149 | + } |
| 150 | + |
| 151 | + return null; |
| 152 | + } |
| 153 | + |
| 154 | + public static int GetElement(Mod mod, string name) |
| 155 | + { |
| 156 | + if (mod == null || name == null || name.Length == 0) |
| 157 | + return -1; |
| 158 | + |
| 159 | + return Data.elementData.FindIndex(x => x.Item1 == mod && x.Item2 == name && x.Item3 == CreateIdentifier(mod, name)); |
| 160 | + } |
| 161 | + |
| 162 | + public static string GetElementName(int type) |
| 163 | + { |
| 164 | + if (type < 0 || type + 1 > Data.elementData.Count) |
| 165 | + return null; |
| 166 | + |
| 167 | + return Data.elementData[type].Item2; |
| 168 | + } |
| 169 | + |
| 170 | + public static string GetDisplayName(int type) |
| 171 | + { |
| 172 | + if (type < 0 || type + 1 > Data.elementData.Count) |
| 173 | + return null; |
| 174 | + |
| 175 | + return Data.elementDisplayName[type]; |
| 176 | + } |
| 177 | + |
| 178 | + public static Mod GetMod(int type) |
| 179 | + { |
| 180 | + if (type < 0 || type + 1 > Data.elementData.Count) |
| 181 | + return null; |
| 182 | + |
| 183 | + return Data.elementData[type].Item1; |
| 184 | + } |
| 185 | + |
| 186 | + public static void ModifyElementModifier(int type, Player player, float modifier) |
| 187 | + { |
| 188 | + if (type < 0 || type + 1 > Data.elementData.Count || type + 1 > player.GetModPlayer<ElementalPlayer>(mod).elementDamage.Count) |
| 189 | + return; |
| 190 | + |
| 191 | + player.GetModPlayer<ElementalPlayer>(mod).elementDamage[type] += (float)modifier; |
| 192 | + } |
| 193 | + |
| 194 | + // Internal |
| 195 | + |
| 196 | + internal static void SetRollChance(int type, double pickup = Data.rollChanceCraft, double reforge = Data.rollChanceReforge, double craft = Data.rollChanceCraft) |
| 197 | + { |
| 198 | + if (type < 0 || type + 1 > Data.elementDisplayName.Count || pickup < 0 || reforge < 0 || craft < 0) |
| 199 | + return; |
| 200 | + |
| 201 | + Data.elementRollChance[type] = new Tuple<bool, double, double, double>(true, pickup, reforge, craft); |
| 202 | + } |
| 203 | + |
| 204 | + internal static string CreateIdentifier(Mod mod, string name) |
| 205 | + { |
| 206 | + string input = mod.Name + "::" + name; |
| 207 | + using (MD5 encrypt = MD5.Create()) |
| 208 | + { |
| 209 | + byte[] hash = encrypt.ComputeHash(Encoding.Default.GetBytes(input)); |
| 210 | + Guid result = new Guid(hash); |
| 211 | + return result.ToString(); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + internal static void ClearData() |
| 216 | + { |
| 217 | + Data.elementData.Clear(); |
| 218 | + Data.elementOnHit.Clear(); |
| 219 | + Data.elementModifyHit.Clear(); |
| 220 | + Data.elementDisplayName.Clear(); |
| 221 | + Data.elementCalculateDamage.Clear(); |
| 222 | + Data.elementRollChance.Clear(); |
| 223 | + Data.elementWeight.Clear(); |
| 224 | + } |
| 225 | + |
| 226 | + internal static string CleanString(string str) |
| 227 | + { |
| 228 | + StringBuilder sb = new StringBuilder(); |
| 229 | + foreach (char c in str) |
| 230 | + { |
| 231 | + if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '.' || c == '_') |
| 232 | + { |
| 233 | + sb.Append(c); |
| 234 | + } |
| 235 | + } |
| 236 | + return sb.ToString(); |
| 237 | + } |
| 238 | + |
| 239 | + internal static int getTotalWeight() |
| 240 | + { |
| 241 | + int _w = 0; |
| 242 | + if (Data.elementWeight.Any()) |
| 243 | + { |
| 244 | + foreach (var item in Data.elementWeight) |
| 245 | + { |
| 246 | + _w += item; |
| 247 | + } |
| 248 | + } |
| 249 | + return _w; |
| 250 | + } |
| 251 | + } |
| 252 | +} |
0 commit comments