Skip to content

Commit 8efd416

Browse files
committed
⭐ V0.0.5 💎 Dynamic Framework
1 parent 8f6da69 commit 8efd416

19 files changed

+555
-817
lines changed

ElementalFramework.cs

Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
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+
}

ElementalInfo.cs

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class ElementalInfo : ItemInfo
3131
public Item elementalItem { get; private set; }
3232

3333
// Set elemental properties
34-
public void SetProperties(bool enabled, int type, int damage, Item item)
34+
internal void SetProperties(bool enabled, int type, int damage, Item item)
3535
{
3636
enhanced = enabled;
3737
elementalType = type;
@@ -41,7 +41,7 @@ public void SetProperties(bool enabled, int type, int damage, Item item)
4141
}
4242

4343
// Reset properties
44-
public void ResetProperties()
44+
internal void ResetProperties()
4545
{
4646
enhanced = false;
4747
elementalType = 0;
@@ -52,36 +52,64 @@ public void ResetProperties()
5252
// Get a real damage value, calculated with a player's elemental modifier
5353
public int GetRealDamage(Player player)
5454
{
55-
return (int)Math.Ceiling((elementalDamage * player.GetModPlayer<ElementalPlayer>(mod).elementDamage[elementalType]));
55+
return (int)Math.Ceiling(((double)elementalDamage * player.GetModPlayer<ElementalPlayer>(mod).elementDamage[elementalType]));
5656
}
5757

5858
// Calculate a damage value
59-
public int CalculateDamage(ref int refDamage)
59+
internal int CalculateDamage(ref int refDamage)
6060
{
6161
return (int)(Main.rand.Next((int)Math.Ceiling(refDamage * 0.10f), (int)Math.Ceiling(refDamage * 0.50f)));
6262
}
6363

6464
// Get a new damage value
65-
public void CalculateNewDamage()
65+
internal int CalculateNewDamage(ref int refDamage)
6666
{
67-
elementalDamage = CalculateDamage(ref elementalItem.damage);
67+
int _damage = (refDamage == -1) ? elementalItem.damage : refDamage;
68+
return (ElementalFramework.Data.elementCalculateDamage[elementalType] != null) ? ElementalFramework.Data.elementCalculateDamage[elementalType].Invoke(_damage) : CalculateDamage(ref _damage);
6869
}
6970

7071
// Calculate a new element. The element can not be the element it was prior to changing it.
7172
public void CalculateNewElement()
7273
{
73-
var elementList = ElementalWeaponEnhancements.elementData.ToList();
74-
elementList.Remove(ElementalWeaponEnhancements.elementData[elementalType]);
74+
var elementList = ElementalFramework.Data.elementData.ToList();
75+
elementList.Remove(ElementalFramework.Data.elementData[elementalType]);
7576
if (elementList.Any())
7677
{
77-
int random = Main.rand.Next(0, (int)elementList.Count);
78+
int _weight = Main.rand.Next(0, ElementalFramework.getTotalWeight());
79+
int _selected = _CalculateElement(elementList, _weight);
80+
/*int random = Main.rand.Next(0, (int)elementList.Count);
7881
//The zero-based index of the first occurrence of item within the entire List<T>, if found; otherwise, –1.
79-
int newElement = ElementalWeaponEnhancements.elementData.FindIndex(x => x.Item3 == elementList[random].Item3);
82+
int newElement = ElementalFramework.Data.elementData.FindIndex(x => x.Item3 == elementList[random].Item3);
8083
if (newElement != -1)
8184
{
8285
elementalType = newElement;
86+
}*/
87+
88+
if (_selected != -1)
89+
{
90+
elementalType = _selected;
91+
}
92+
}
93+
}
94+
95+
// Get element by weight
96+
internal int _CalculateElement(List<Tuple<Mod, string, string, Color>> _data, int _weight)
97+
{
98+
int _selected = -1;
99+
foreach (var item in _data)
100+
{
101+
int _thisWeight = ElementalFramework.Data.elementWeight[ElementalFramework.GetElement(item.Item1, item.Item2)];
102+
103+
if (_weight < _thisWeight)
104+
{
105+
_selected = ElementalFramework.Data.elementData.FindIndex(x => x.Item3 == item.Item3);
106+
break;
83107
}
108+
109+
_weight = _weight - _thisWeight;
84110
}
111+
112+
return _selected;
85113
}
86114
}
87115
}

0 commit comments

Comments
 (0)