-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeliumHelperMod.cs
More file actions
270 lines (242 loc) · 8.86 KB
/
HeliumHelperMod.cs
File metadata and controls
270 lines (242 loc) · 8.86 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
using MelonLoader;
using BTD_Mod_Helper;
using HeliumHelper;
using Il2CppAssets.Scripts.Unity.UI_New.InGame;
using BTD_Mod_Helper.Extensions;
using HarmonyLib;
using System.Collections.Generic;
using Newtonsoft.Json;
using Il2CppAssets.Scripts.Models.Profile;
using Il2CppAssets.Scripts.Simulation.Towers.Weapons;
using BTD_Mod_Helper.Api;
using HeliumHelper.WendellTower;
using Il2CppAssets.Scripts.Unity;
using HeliumHelper.UI;
using Il2CppAssets.Scripts.Simulation.Bloons;
using Il2CppAssets.Scripts.Simulation.Towers.Behaviors.Abilities;
using Il2CppAssets.Scripts.Unity.Bridge;
using Il2CppAssets.Scripts.Unity.UI_New.InGame.TowerSelectionMenu;
using Il2CppAssets.Scripts.Simulation.Towers.Projectiles;
using Il2CppAssets.Scripts.Simulation.Towers;
using Il2CppAssets.Scripts.Simulation.Track;
using Il2CppAssets.Scripts.Models.Towers;
using Il2CppAssets.Scripts.Models.Bloons;
using System.Linq;
using UnityEngine;
[assembly: MelonInfo(typeof(HeliumHelper.HeliumHelperMod), ModHelperData.Name, ModHelperData.Version, ModHelperData.RepoOwner)]
[assembly: MelonGame("Ninja Kiwi", "BloonsTD6")]
namespace HeliumHelper;
public class HeliumHelperMod : BloonsTD6Mod
{
public static bool AutoSend = true;
public static float AutoSendSpeed = 2;
#region Saving
public const string WendellBloonsID = "WendellBloons";
public static Dictionary<string, int> WendellBloons = [];
public const string AutoSendID = "WendellAutoSendBloons";
public const string AutoSendSpeedID = "WendellAutoSendBloonsSpeed";
[HarmonyPatch(typeof(Map), nameof(Map.GetSaveData))]
static class Map_GetSaveData
{
[HarmonyPostfix]
public static void Postfix(MapSaveDataModel mapData)
{
var json = JsonConvert.SerializeObject(WendellBloons);
if (!mapData.metaData.TryAdd(WendellBloonsID, json))
{
mapData.metaData[WendellBloonsID] = json;
}
json = JsonConvert.SerializeObject(AutoSend);
if (!mapData.metaData.TryAdd(AutoSendID, json))
{
mapData.metaData[AutoSendID] = json;
}
json = JsonConvert.SerializeObject(AutoSendSpeed);
if (!mapData.metaData.TryAdd(AutoSendSpeedID, json))
{
mapData.metaData[AutoSendSpeedID] = json;
}
}
}
[HarmonyPatch(typeof(Map), nameof(Map.SetSaveData))]
static class Map_SetSaveData
{
[HarmonyPostfix]
public static void Postfix(MapSaveDataModel mapData)
{
if (mapData.metaData.TryGetValue(WendellBloonsID, out var data))
{
WendellBloons = JsonConvert.DeserializeObject<Dictionary<string, int>>(data)!;
}
if (mapData.metaData.TryGetValue(AutoSendID, out data))
{
AutoSend = JsonConvert.DeserializeObject<bool>(data)!;
}
if (mapData.metaData.TryGetValue(AutoSendSpeedID, out data))
{
AutoSendSpeed = JsonConvert.DeserializeObject<float>(data)!;
}
}
}
#endregion
#region Hooks
public override void OnLateUpdate()
{
if (InGame.instance != null && InGame.instance.bridge != null && InGame.instance.GetTowers().Any(twr => twr.towerModel.baseId == ModContent.TowerID<Wendell>()))
{
if (WendellBloonsMenu.autoSendEnabled && WendellBloonsMenu.nextAutoSendTime <= Time.time && WendellBloons.Count > 0)
{
WendellBloonsMenu.nextAutoSendTime = Time.time + AutoSendSpeed;
List<BloonModel> bloons = [];
foreach (var id in WendellBloons.Keys)
{
bloons.Add(Game.instance.model.bloonsByName[id]);
}
string bloon = bloons.OrderBy(bm => bm.danger).ToList()[bloons.Count - 1].id;
SpawnBloonButton.SpawnProjectile(bloon);
WendellBloons[bloon] -= 1;
if (WendellBloons[bloon] <= 0)
{
WendellBloons.Remove(bloon);
}
}
}
}
public override void OnWeaponFire(Weapon weapon)
{
var tower = weapon.attack.tower;
var proj = weapon.weaponModel.projectile;
if (tower.towerModel.baseId == ModContent.TowerID<Wendell>())
{
if (proj.id == "Wendell_Befriend")
{
tower.GetUnityDisplayNode().animationComponent.SetTrigger("Befriend");
}
}
}
public override void OnTowerUpgraded(Tower tower, string upgradeName, TowerModel newBaseTowerModel)
{
if (tower.towerModel.baseId != ModContent.TowerID<Wendell>())
{
return;
}
int level = tower.towerModel.tiers[0];
if (level >= 5)
{
AutoSend = true;
AutoSendSpeed = 2;
}
if (level >= 13)
{
AutoSendSpeed = 1;
}
if (level >= 17)
{
AutoSendSpeed = .5f;
}
if (level >= 20)
{
AutoSendSpeed = .1f;
}
}
public override void OnMatchEnd()
{
WendellBloons.Clear();
AutoSend = false;
AutoSendSpeed = 2;
WendellBloonsMenu.autoSendEnabled = false;
}
public override void OnRestart()
{
WendellBloons.Clear();
AutoSend = false;
AutoSendSpeed = 2;
WendellBloonsMenu.autoSendEnabled = false;
}
#endregion
#region Common Methods and Patches
public static void BefriendBloon(string id, bool updateMenu = false)
{
WendellBloons.TryAdd(id, 0);
WendellBloons[id] += 1;
if (WendellBloonsMenu.instance != null && updateMenu)
{
if (WendellBloonsMenu.instance.updateToken != null)
{
MelonCoroutines.Stop(WendellBloonsMenu.instance.updateToken);
}
WendellBloonsMenu.instance.updateToken = MelonCoroutines.Start(WendellBloonsMenu.instance.UpdateButtons());
}
}
[HarmonyPatch(typeof(TowerSelectionMenu), nameof(TowerSelectionMenu.SelectTower))]
static class TowerSelectionMenu_Open
{
[HarmonyPostfix]
static void Postfix(TowerSelectionMenu __instance, TowerToSimulation tower)
{
if (tower.tower.towerModel.baseId == ModContent.TowerID<Wendell>())
{
ModContent.GetInstance<Wendell>().OnSelect(tower.tower, __instance);
}
else
{
if (WendellBloonsButton.instance != null)
{
WendellBloonsButton.instance.Close();
}
}
}
}
[HarmonyPatch(typeof(Bloon), nameof(Bloon.PreCheckDamageOutcome))]
static class Bloon_PreCheckDamageOutcome
{
static void Postfix(Bloon __instance, Projectile projectile, Tower tower, ref Il2CppSystem.ValueTuple<bool, bool, bool> __result, float damageAmount)
{
if (tower == null || projectile == null)
{
return;
}
if (projectile.projectileModel.id == "Wendell_Befriend")
{
if (__instance.bloonModel.layerNumber <= projectile.projectileModel.GetDamageModel().damage && !__instance.bloonModel.isBoss)
{
ModHelper.Log<HeliumHelperMod>("Befriend");
ModContent.GetInstance<Wendell>().OnBefriend(tower, __instance);
__instance.health -= 50000000;
}
__instance.health += (int)damageAmount;
}
}
[HarmonyPatch(typeof(Ability), nameof(Ability.Activate))]
static class Ability_Cast
{
[HarmonyPostfix]
public static void Postfix(Ability __instance)
{
if (__instance.abilityModel.name == "AbilityModel_Treaty_Lvl10")
{
foreach (var bloon in InGame.instance.GetBloons())
{
if (bloon.bloonModel.layerNumber <= 6)
{
ModContent.GetInstance<Wendell>().OnBefriend(__instance.tower, bloon);
bloon.Damage(1999999999, null, true, true, false);
}
}
}
else if (__instance.abilityModel.name == "AbilityModel_Treaty_Lvl20")
{
foreach (var bloon in InGame.instance.GetBloons())
{
if (bloon.bloonModel.layerNumber <= 11)
{
ModContent.GetInstance<Wendell>().OnBefriend(__instance.tower, bloon);
bloon.Damage(1999999999, null, true, true, false);
}
}
}
}
}
#endregion
}
}