-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlugin.cs
More file actions
213 lines (161 loc) · 6.82 KB
/
Plugin.cs
File metadata and controls
213 lines (161 loc) · 6.82 KB
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
using BepInEx;
using BepInEx.Logging;
using FTKRandomizer.Patches;
using GridEditor;
using HarmonyLib;
using System;
using System.Collections.Generic;
namespace FTKRandomizer
{
[BepInPlugin(modGUID, modName, modVersion)]
public class Randomizer : BaseUnityPlugin
{
public class PlayerStats
{
public float toughness;
public float vitality;
public float damageMagic;
public float talent;
public float awareness;
public float quickness;
}
public static class CustomData
{
public static bool SeedInitialized = false;
public static Dictionary<string, object> CustomDB = new Dictionary<string, object>();
public static Dictionary<string, object> WeaponStats = new Dictionary<string, object>();
public static Dictionary<string, object> PlayerStatsInitial = new Dictionary<string, object>();
}
private const string modGUID = "dragitz.FTKRandomizer";
private const string modName = "FTK Randomizer";
private const string modVersion = "1.0.3";
private readonly Harmony harmony = new Harmony(modGUID);
private static Randomizer Instance;
internal ManualLogSource mls;
public class SimpleXorRNG
{
private uint seed;
public SimpleXorRNG(int seed)
{
this.seed = (uint)seed;
}
public uint Next()
{
seed ^= seed << 13;
seed ^= seed >> 17;
seed ^= seed << 5;
return seed;
}
public int NextInt(int min, int max)
{
return (int)(Next() % (max - min)) + min;
}
public double NextDouble()
{
// Divide by (uint.MaxValue + 1) to ensure result is never exactly 1.0
return Next() / ((double)uint.MaxValue + 1.0);
}
}
public static void SetupRandomizer(int seed)
{
var db = FTK_itemsDB.GetDB();
if (db == null)
{
Console.WriteLine("FTK_itemsDB not ready");
return;
}
var rng = new SimpleXorRNG(seed);
FTK_weaponStats2DB fTK_WeaponStats2DB = FTK_weaponStats2DB.GetDB();
foreach (FTK_weaponStats2 item in fTK_WeaponStats2DB.m_Array)
{
// This filters out test and debug items
//if (item.m_FilterDebug) { continue; }
if (item.m_ID == null || item.m_ID.Length < 2 || item.m_ID.Contains("STR_")) { continue; }
// Do not alter important items
if (item.m_ItemRarity == FTK_itemRarityLevel.ID.lore || item.m_ItemRarity == FTK_itemRarityLevel.ID.quest) { continue; }
// Do not alter invalid items
if (item._skilltest == FTK_weaponStats2.SkillType.none || item._skilltest == FTK_weaponStats2.SkillType.COUNT) { continue; }
// Ensure this only applies to weapon
//if (!item.m_IsWeapon) { continue; }
//rng.Next();
// Assign random skill
Array values = Enum.GetValues(typeof(FTK_weaponStats2.SkillType));
FTK_weaponStats2.SkillType skill = (FTK_weaponStats2.SkillType)values.GetValue(rng.NextInt(0, 7));
float buff = 3f;
float multiplier = 1;
float finalBuff = 0.0f;
float originalDamage = item._maxdmg;
if (
skill == FTK_weaponStats2.SkillType.awareness ||
skill == FTK_weaponStats2.SkillType.toughness ||
skill == FTK_weaponStats2.SkillType.luck ||
skill == FTK_weaponStats2.SkillType.vitality ||
skill == FTK_weaponStats2.SkillType.quickness
)
{
if (skill == FTK_weaponStats2.SkillType.luck) { buff = 5; } // luck is fun
if (item.m_ItemRarity == FTK_itemRarityLevel.ID.rare) { multiplier = 1.2f; }
if (item.m_ItemRarity == FTK_itemRarityLevel.ID.artifact) { multiplier = 1.3f; }
finalBuff = buff * multiplier;
}
var dt = new FTK_weaponStats2
{
_skilltest = skill,
_maxdmg = originalDamage + finalBuff,
_slots = rng.NextInt(1, 6)
};
CustomData.WeaponStats[item.m_ID] = dt;
}
//
// Random initial stats
// Two positive - two negative - one random
for (int q = 0; q < 4; q++)
{
float maxValue = 0.02f;
float[] randos = new float[5];
randos[0] = (float)rng.NextDouble() * maxValue;
randos[1] = (float)rng.NextDouble() * maxValue;
randos[2] = (float)(-maxValue * rng.NextDouble());
randos[3] = (float)(-maxValue * rng.NextDouble());
randos[4] = (float)(rng.NextDouble() * (maxValue * 2) - maxValue);
for (int i = 0; i < randos.Length - 1; ++i) // shuffle, thank you https://stackoverflow.com/a/69504187
{
int r = rng.NextInt(i, randos.Length);
(randos[r], randos[i]) = (randos[i], randos[r]);
}
// Randomize all values
var dt = new PlayerStats
{
toughness = randos[0],
vitality = randos[1],
damageMagic = rng.NextInt(-5, 6),
talent = randos[2],
awareness = randos[3],
quickness = randos[4]
};
CustomData.PlayerStatsInitial[q.ToString()] = dt;
}
// Confirm success
CustomData.SeedInitialized = true;
}
void Awake()
{
if (Instance == null)
{
Instance = this;
}
mls = BepInEx.Logging.Logger.CreateLogSource(modGUID);
mls.LogInfo("Hello !!");
harmony.PatchAll(typeof(Randomizer));
harmony.PatchAll(typeof(SetGameModeDifficultyPatch));
harmony.PatchAll(typeof(RandomizeDShopPrices));
harmony.PatchAll(typeof(RandomizeDShopItems));
harmony.PatchAll(typeof(RandomizeDInitialStats));
harmony.PatchAll(typeof(RandomizeAllItemStats));
harmony.PatchAll(typeof(TownEE));
harmony.PatchAll(typeof(LootItems));
harmony.PatchAll(typeof(RandomizeDItemEffects));
//harmony.PatchAll(typeof(TestRandomModifier));
}
}
}