-
Notifications
You must be signed in to change notification settings - Fork 0
/
Helpers.cs
228 lines (195 loc) · 9.19 KB
/
Helpers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using System;
using System.Collections.Generic;
using System.Linq;
using DaggerfallConnect.FallExe;
using DaggerfallWorkshop;
using DaggerfallWorkshop.Game;
using DaggerfallWorkshop.Game.Formulas;
using DaggerfallWorkshop.Game.Items;
using DaggerfallWorkshop.Utility;
namespace Game.Mods.QuestActionsExtension
{
public static class Helpers
{
// Enchantment point/gold value data for item powers
private static readonly int[] _extraSpellPtsEnchantPts = { 0x1F4, 0x1F4, 0x1F4, 0x1F4, 0xC8, 0xC8, 0xC8, 0x2BC, 0x320, 0x384, 0x3E8 };
private static readonly int[] _potentVsEnchantPts = { 0x320, 0x384, 0x3E8, 0x4B0 };
private static readonly int[] _regensHealthEnchantPts = { 0x0FA0, 0x0BB8, 0x0BB8 };
private static readonly int[] _vampiricEffectEnchantPts = { 0x7D0, 0x3E8 };
private static readonly int[] _increasedWeightAllowanceEnchantPts = { 0x190, 0x258 };
private static readonly int[] _improvesTalentsEnchantPts = { 0x1F4, 0x258, 0x258 };
private static readonly int[] _goodRepWithEnchantPts = { 0x3E8, 0x3E8, 0x3E8, 0x3E8, 0x3E8, 0x1388 };
private static readonly int[][] _enchantmentPtsForItemPowerArrays =
{
null, null, null, _extraSpellPtsEnchantPts, _potentVsEnchantPts, _regensHealthEnchantPts,
_vampiricEffectEnchantPts, _increasedWeightAllowanceEnchantPts, null, null, null, null, null,
_improvesTalentsEnchantPts, _goodRepWithEnchantPts
};
private static readonly ushort[] _enchantmentPointCostsForNonParamTypes = { 0, 0x0F448, 0x0F63C, 0x0FF9C, 0x0FD44, 0, 0, 0, 0x384, 0x5DC, 0x384, 0x64, 0x2BC };
private static int GetValueOfOneEnchantment(DaggerfallEnchantment enchantment)
{
switch (enchantment.type)
{
case EnchantmentTypes.CastWhenUsed:
case EnchantmentTypes.CastWhenHeld:
case EnchantmentTypes.CastWhenStrikes:
// Enchantments that cast a spell. The parameter is the spell index in SPELLS.STD.
return FormulaHelper.GetSpellEnchantPtCost(enchantment.param);
case EnchantmentTypes.RepairsObjects:
case EnchantmentTypes.AbsorbsSpells:
case EnchantmentTypes.EnhancesSkill:
case EnchantmentTypes.FeatherWeight:
case EnchantmentTypes.StrengthensArmor:
// Enchantments that provide an effect that has no parameters
return _enchantmentPointCostsForNonParamTypes[(int)enchantment.type];
case EnchantmentTypes.SoulBound:
// Bound soul
MobileEnemy mobileEnemy = GameObjectHelper.EnemyDict[enchantment.param];
return mobileEnemy.SoulPts; // TODO: Not sure about this. Should be negative? Needs to be tested.
default:
// Enchantments that provide a non-spell effect with a parameter (parameter = when effect applies, what enemies are affected, etc.)
return _enchantmentPtsForItemPowerArrays[(int)enchantment.type][enchantment.param];
}
}
private static int GetValueOfMagicItem(IEnumerable<DaggerfallEnchantment> legacyMagic)
{
return legacyMagic
.Where(enchantment => enchantment.type != EnchantmentTypes.None && enchantment.type < EnchantmentTypes.ItemDeteriorates)
.Sum(GetValueOfOneEnchantment);
}
public static void ApplyMagicTemplate(DaggerfallUnityItem item, int templateIndex)
{
MagicItemTemplate magicItem;
try
{
magicItem = DaggerfallUnity.Instance.ItemHelper.MagicItemTemplates.First(t => t.index == templateIndex);
}
catch
{
throw new Exception($"Item index {templateIndex} does not exist!");
}
if (magicItem.type != MagicItemTypes.RegularMagicItem)
{
throw new Exception($"Item index {templateIndex} is not a RegularMagicItem!");
}
// Replace the regular item name with the magic item name
item.shortName = TextManager.Instance.GetLocalizedMagicItemName((int)magicItem.index, magicItem.name);
// Add the enchantments
item.legacyMagic = magicItem.enchantments.Where(e => e.type != EnchantmentTypes.None).ToArray();
// Set the condition/magic uses
item.maxCondition = magicItem.uses;
item.currentCondition = magicItem.uses;
// Set the value of the item. This is determined by the enchantment point cost/spell-casting cost
// of the enchantments on the item.
var newValue = GetValueOfMagicItem(item.legacyMagic);
if (newValue > 0)
{
item.value = newValue;
}
}
public static void AddEnchantmentToItem(DaggerfallUnityItem item, string enchantmentType, short enchantmentParam)
{
var overrideValue = false;
if (item.legacyMagic == null)
{
item.legacyMagic = new DaggerfallEnchantment[] { };
overrideValue = true;
}
// Check if the length is no more than 10
if (item.legacyMagic.Length >= 10)
{
throw new Exception("Can't apply more than 10 enchantments");
}
if (!Enum.IsDefined(typeof(EnchantmentTypes), enchantmentType))
{
throw new Exception($"Enchantment type {enchantmentType} doesn't exist");
}
EnchantmentTypes type = (EnchantmentTypes)Enum.Parse(typeof(EnchantmentTypes), enchantmentType);
var enchantment = new DaggerfallEnchantment()
{
type = type,
param = enchantmentParam,
};
// Add enchantment to a list
var legacyMagic = item.legacyMagic.Where(lm => lm.type != EnchantmentTypes.None).ToList();
legacyMagic.Add(enchantment);
item.legacyMagic = legacyMagic.ToArray();
// Set the condition/magic uses
// And yes, it's always 1500
item.maxCondition = 1500;
item.currentCondition = 1500;
var enchValue = GetValueOfOneEnchantment(enchantment);
// Set the value of the item. This is determined by the enchantment point cost/spell-casting cost
// of the enchantments on the item.
if (overrideValue)
{
item.value = enchValue;
}
else
{
item.value += enchValue;
}
}
// TODO: Switch to Place.IsPlayerAtDungeonType(), when https://github.com/Interkarma/daggerfall-unity/pull/2614 released!
public static bool IsPlayerAtDungeonType(int p2)
{
// Get component handling player world status and transitions
var playerEnterExit = GameManager.Instance.PlayerEnterExit;
if (!playerEnterExit)
return false;
// Only dungeons
if (!playerEnterExit.IsPlayerInsideDungeon)
return false;
// Any dungeon will do
if (p2 == -1)
return true;
return p2 == (int)GameManager.Instance.PlayerEnterExit.Dungeon.Summary.DungeonType;
}
public static int CalculateLengthOrStackCount(IReadOnlyCollection<DaggerfallUnityItem> itemList)
{
if (itemList == null) return 0;
var count = 0;
foreach (var item in itemList.Where(item => !item.IsSummoned))
{
count += item.IsAStack() ? item.stackCount : 1;
}
return count;
}
public static void RemoveNFromList(ItemCollection storage, List<DaggerfallUnityItem> itemList, int amount)
{
var itemsRemoved = 0;
foreach (var item in itemList)
{
if (itemsRemoved >= amount)
break;
if (item.IsAStack())
{
var stackCount = item.stackCount;
if (stackCount <= amount - itemsRemoved)
{
// Remove the entire stack
storage.RemoveItem(item);
itemsRemoved += stackCount;
}
else
{
// Remove part of the stack
item.stackCount -= (amount - itemsRemoved);
itemsRemoved = amount;
}
}
else
{
// Remove single item
storage.RemoveItem(item);
itemsRemoved++;
}
}
}
public static bool Contains(IEnumerable<DaggerfallUnityItem> items, ItemGroups itemGroup, int itemIndex)
{
var groupIndex = DaggerfallUnity.Instance.ItemHelper.GetGroupIndex(itemGroup, itemIndex);
return items.Where(item => item != null).Any(item => item.ItemGroup == itemGroup && item.GroupIndex == groupIndex);
}
}
}