-
Notifications
You must be signed in to change notification settings - Fork 0
/
CopyPasteTowersMod.cs
176 lines (149 loc) · 5.65 KB
/
CopyPasteTowersMod.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
using System;
using Il2CppAssets.Scripts;
using Il2CppAssets.Scripts.Models.Towers;
using Il2CppAssets.Scripts.Simulation.Input;
using Il2CppAssets.Scripts.Simulation.Towers;
using Il2CppAssets.Scripts.Unity;
using Il2CppAssets.Scripts.Unity.UI_New.InGame;
using Il2CppAssets.Scripts.Unity.UI_New.InGame.TowerSelectionMenu;
using BTD_Mod_Helper;
using BTD_Mod_Helper.Api.ModOptions;
using BTD_Mod_Helper.Extensions;
using CopyPasteTowers;
using HarmonyLib;
using Il2CppNinjaKiwi.Common;
using MelonLoader;
using UnityEngine;
using Vector2 = UnityEngine.Vector2;
using Vector3 = Il2CppAssets.Scripts.Simulation.SMath.Vector3;
[assembly: MelonInfo(typeof(CopyPasteTowersMod), ModHelperData.Name, ModHelperData.Version, ModHelperData.RepoOwner)]
[assembly: MelonGame("Ninja Kiwi", "BloonsTD6")]
namespace CopyPasteTowers;
public class CopyPasteTowersMod : BloonsTD6Mod
{
private static TowerModel? clipboard;
private static double cost;
private static int payForIt;
private static bool justPastedTower;
private static bool lastCopyWasCut;
private static TargetType? targetType;
private static readonly ModSettingHotkey CopyHotkey = new(KeyCode.C, HotkeyModifier.Ctrl);
private static readonly ModSettingHotkey PasteHotkey = new(KeyCode.V, HotkeyModifier.Ctrl);
private static readonly ModSettingHotkey CutHotkey = new(KeyCode.X, HotkeyModifier.Ctrl);
public override void OnLateUpdate()
{
if (!InGame.instance)
{
return;
}
if (TowerSelectionMenu.instance)
{
var selectedTower = TowerSelectionMenu.instance.selectedTower;
if (selectedTower is { IsParagon: false } && !selectedTower.tower.towerModel.IsHero())
{
lastCopyWasCut = CutHotkey.JustPressed();
if (CutHotkey.JustPressed() || CopyHotkey.JustPressed())
{
CopyTower(selectedTower.tower);
if (CutHotkey.JustPressed())
{
TowerSelectionMenu.instance.Sell();
lastCopyWasCut = true;
}
else
{
lastCopyWasCut = false;
}
}
}
}
if (PasteHotkey.JustPressed() ||
justPastedTower && (PasteHotkey.IsPressed() || Input.GetKey(KeyCode.LeftShift)))
{
PasteTower();
}
justPastedTower = false;
if (--payForIt < 0) payForIt = 0;
}
private static void CopyTower(Tower tower)
{
var inGameModel = InGame.instance.GetGameModel();
clipboard = inGameModel.GetTowerWithName(tower.towerModel.name);
cost = CalculateCost(clipboard);
var name = LocalizationManager.Instance.GetText(tower.towerModel.name);
Game.instance.ShowMessage($"Copied {name}\n\nTotal Cost is ${(int) cost}");
targetType = tower.TargetType;
}
private static double CalculateCost(TowerModel towerModel, Vector3 pos = default)
{
var inGameModel = InGame.instance.GetGameModel();
var towerManager = InGame.instance.GetTowerManager();
var total = 0.0;
var discountMult = 0f;
if (pos != default)
{
var zoneDiscount =
towerManager.GetZoneDiscount(towerModel, pos, 0, 0, InGame.instance.bridge.MyPlayerNumber);
discountMult = towerManager.GetDiscountMultiplier(zoneDiscount);
}
total += (1 - discountMult) * towerModel.cost;
foreach (var appliedUpgrade in towerModel.appliedUpgrades)
{
var upgrade = inGameModel.GetUpgrade(appliedUpgrade);
discountMult = 0f;
if (pos != default)
{
var zoneDiscount = towerManager.GetZoneDiscount(towerModel, pos, upgrade.path, upgrade.tier,
InGame.instance.bridge.MyPlayerNumber);
discountMult = towerManager.GetDiscountMultiplier(zoneDiscount);
}
total += upgrade.cost * (1 - discountMult);
}
return total;
}
private static double CalculateCost(Tower tower) => CalculateCost(tower.towerModel, tower.Position);
private static void PasteTower()
{
var inputManager = InGame.instance.InputManager;
if (clipboard == null || inputManager.IsInPlacementMode || InGame.instance.GetCash() < cost)
{
return;
}
inputManager.EnterPlacementMode(clipboard, new Action<Vector2>(pos =>
{
try
{
payForIt = 30;
inputManager.CreatePlacementTower(pos);
}
catch (Exception e)
{
ModHelper.Error<CopyPasteTowersMod>(e);
}
}), new ObjectId { data = (uint) InGame.instance.bridge.GetInputId() });
}
[HarmonyPatch(typeof(Tower), nameof(Tower.OnPlace))]
internal static class Tower_OnPlace
{
[HarmonyPrefix]
private static void Prefix(Tower __instance)
{
if (payForIt > 0)
{
__instance.worth = (float) CalculateCost(__instance);
InGame.instance.AddCash(-__instance.worth + __instance.towerModel.cost);
payForIt = 0;
justPastedTower = true;
if (lastCopyWasCut)
{
var tts = __instance.GetTowerToSim();
TowerSelectionMenu.instance.SelectTower(tts);
}
if (targetType != null)
{
__instance.SetTargetType(targetType);
}
}
}
}
}