-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBagPlayer.cs
More file actions
345 lines (299 loc) · 11.3 KB
/
BagPlayer.cs
File metadata and controls
345 lines (299 loc) · 11.3 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
using androLib;
using androLib.Common.Utility;
using androLib.UI;
using Microsoft.Xna.Framework;
using Mono.Cecil.Cil;
using MonoMod.Cil;
using Steamworks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Terraria;
using Terraria.Audio;
using Terraria.DataStructures;
using Terraria.GameContent;
using Terraria.Graphics;
using Terraria.Graphics.Renderers;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
using Terraria.UI;
using VacuumBags.Common.Globals;
using VacuumBags.Items;
namespace VacuumBags
{
public class BagPlayer : ModPlayer {
#region General Overrides
public override void Load() {
AndroMod.OnResetGameCounter += () => {
honeyWetResetTime = 0;
nextFullCheckTime = 0;
};
}
public override void PostUpdateMiscEffects() {
BannerBag.PostUpdateMiscEffects(Player);
}
public override void ResetEffects() {
bagPlaceItem = null;
bagItemChecked = false;
bagSmartSelectItem = null;
bagSmartSelectItemChecked = false;
inventoryFoundIn = null;
foundInventoryIndex = Storage.ItemNotFound;
bagFoundIn = -1;
TouchingStation = false;
}
public override void PreUpdate() {
LastTouchingStation = TouchingStation;
TouchingStation = false;
}
public override void PostUpdateBuffs() {
if (Main.netMode == NetmodeID.Server)
return;
if (Player.honeyWet)
honeyWetResetTime = Main.GameUpdateCount + HoneyBuffTime;
UpdateHoneyBuff();
ExquisitePotionFlask.PostUpdateBuffs(Player);
}
public override void PostUpdate() {
TrashCan.TrashCheck();
}
public override void Kill(double damage, int hitDirection, bool pvp, PlayerDeathReason damageSource) {
honeyWetResetTime = 0;
ExquisitePotionFlask.OnKilled(Player);
}
public override void OnRespawn() {
ExquisitePotionFlask.OnRespawn(Player);
}
#endregion
#region Bag/Item Swap
private static bool bagItemChecked = false;
private static bool bagSmartSelectItemChecked = false;
private static int lastToolStrategyID(Player player) => player != null ? (int)(typeof(Player).GetField("_lastSmartCursorToolStrategy", BindingFlags.NonPublic | BindingFlags.Instance)?.GetValue(player) ?? -1) : -1;
public static ref Item BagPlaceItem(Player player) {
if (!bagItemChecked)
bagPlaceItem = GetBagPlaceItem(player);
return ref bagPlaceItem;
}
private static Item bagPlaceItem = null;
public static ref Item BagSmartSelectItem(Player player, Func<Item, bool> itemCondition) {
if (!bagSmartSelectItemChecked)
bagSmartSelectItem = GetBagSmartSelectItem(player, itemCondition);
return ref bagSmartSelectItem;
}
private static Item bagSmartSelectItem = null;
private static IList<Item> inventoryFoundIn = null;
private static int foundInventoryIndex = Storage.ItemNotFound;
private static int bagFoundIn = -1;
private static List<KeyValuePair<Func<int>, Func<Player, Item>>> choseFromBagFunctions = new() {
new(ModContent.ItemType<BuildersBox>, BuildersBox.ChooseItemFromBox),
new(ModContent.ItemType<WallEr>, WallEr.ChooseItemFromWallEr),
new(ModContent.ItemType<JarOfDirt>, JarOfDirt.ChooseItemFromJar),
new(ModContent.ItemType<SlayersSack>, SlayersSack.ChooseRopeFromSack),
new(ModContent.ItemType<MechanicsToolbelt>, MechanicsToolbelt.ChoosePlacableItemFromBelt)
};
private static List<KeyValuePair<Func<int>, Func<Player, Func<Item, bool>, Item>>> chooseFromBagForQuickSelectFunctions = new() {
new(ModContent.ItemType<SlayersSack>, SlayersSack.ChooseTorchFromSack)
};
private static Item GetBagPlaceItem(Player player) {
bagItemChecked = true;
int bagType = Main.LocalPlayer.HeldItem.type;
foreach (KeyValuePair<Func<int>, Func<Player, Item>> choseFromBagPair in choseFromBagFunctions) {
if (choseFromBagPair.Key() == bagType)
return choseFromBagPair.Value(player);
}
return null;
}
private static bool ValidateStoredBagInfo => bagFoundIn == -1 && foundInventoryIndex > Storage.ItemNotFound && inventoryFoundIn != null && ReferenceEquals(inventoryFoundIn?[foundInventoryIndex], Main.LocalPlayer.inventory[foundInventoryIndex]);
private static Item GetBagSmartSelectItem(Player player, Func<Item, bool> itemCondition) {
bagSmartSelectItemChecked = true;
foreach (KeyValuePair<Func<int>, Func<Player, Func<Item, bool>, Item>> choseFromBagPair in chooseFromBagForQuickSelectFunctions) {
int bagType = choseFromBagPair.Key();
if (StorageManager.HasRequiredItemToUseStorageFromBagType(player, bagType, out inventoryFoundIn, out foundInventoryIndex, out bagFoundIn)) {
if (ValidateStoredBagInfo)
return choseFromBagPair.Value(player, itemCondition);
}
}
return null;
}
internal static void OnItemCheck_Inner(On_Player.orig_ItemCheck_Inner orig, Player self) {
Player player = self;
DoBagItemSwap(player, () => orig(self), player.selectedItem == player.inventory.Length - 1);
}
internal static void On_Main_DrawInterface_40_InteractItemIcon(On_Main.orig_DrawInterface_40_InteractItemIcon orig, Main self) {
Player player = Main.LocalPlayer;
DoBagItemSwap(player, () => orig(self));
}
internal static void On_SmartCursorHelper_SmartCursorLookup(On_SmartCursorHelper.orig_SmartCursorLookup orig, Player player) {
DoBagItemSwap(player, () => orig(player));
}
internal static void On_PlayerDrawLayers_DrawPlayer_27_HeldItem(On_PlayerDrawLayers.orig_DrawPlayer_27_HeldItem orig, ref PlayerDrawSet drawinfo) {
Player player = drawinfo.drawPlayer;
if (player.whoAmI != Main.myPlayer || Main.gameMenu) {
orig(ref drawinfo);
return;
}
drawinfo.heldItem = player.HeldItem;
PlayerDrawSet l = drawinfo;
DoBagItemSwap(player, () => {
l.heldItem = player.HeldItem;
orig(ref l);
});
drawinfo = l;
}
private static void DoBagItemSwap(Player player, Action orig, bool swapMouseItem = false) {
bool temp = !player.controlTorch && player.nonTorch > -1 && player.itemAnimation == 0;
int lastToolStrategy = lastToolStrategyID(player);
if (lastToolStrategy > ToolStrategyID.None) {
int tempnonTorch = player.nonTorch;
int tempselectedItem = player.selectedItem;
bool swapSmartSelect = BagSmartSelectItem(player, ToolStrategyID.ToolStrategyConditions[lastToolStrategy]) != null;
if (!swapSmartSelect && lastToolStrategy == ToolStrategyID.Light)
swapSmartSelect = BagSmartSelectItem(player, ToolStrategyID.ToolStrategyConditions[ToolStrategyID.Light2ndPassGlowStickOnly]) != null;
if (swapSmartSelect && ValidateStoredBagInfo) {
if (player.nonTorch == -1)
player.nonTorch = player.selectedItem;
player.selectedItem = foundInventoryIndex;
ref Item beingReplaced = ref player.inventory[player.selectedItem];
ref Item toReplace = ref bagSmartSelectItem;
SwapAndCallOriginal(ref beingReplaced, ref toReplace, () => orig(), swapMouseItem);
return;
}
}
bool swap = BagPlaceItem(player) != null;
if (swap) {
ref Item beingReplaced = ref player.inventory[player.selectedItem];
ref Item toReplace = ref bagPlaceItem;
SwapAndCallOriginal(ref beingReplaced, ref toReplace, () => orig(), swapMouseItem);
return;
}
orig();
}
public static void SwapAndCallOriginal(ref Item beingReplaced, ref Item toReplace, Action orig, bool swapMouseItem = false) {
Utils.Swap(ref beingReplaced, ref toReplace);
orig();
Utils.Swap(ref beingReplaced, ref toReplace);
if (swapMouseItem && Main.LocalPlayer.itemAnimation == 0)
Main.mouseItem = beingReplaced.Clone();
}
#endregion
#region Honey Buff
public const int HoneyBuffTime = 1800;
public const int AOEHoneyBuffTime = 2;
public const int TicksPerSecond = 60;
public bool TouchingStation = false;
public bool LastTouchingStation = false;
public bool NearPortableStation = false;
private uint honeyWetResetTime = 0;
private int lastHoneyBucketLocation = -1;
private uint nextFullCheckTime = 0;
private void UpdateNextHoneyFullCheckTime() {
nextFullCheckTime = Main.GameUpdateCount + TicksPerSecond;
}
private void UpdatePlayerPortableStorageOverlap() {
if (lastHoneyBucketLocation == -1)
return;
Player player = Player;
Point p = new Point(0, 2);
Vector2 vector = p.ToVector2();
List<Point> tilesIn2 = Collision.GetTilesIn(player.TopLeft, player.BottomRight + vector);
for (int j = 0; j < tilesIn2.Count; j++) {
Point point2 = tilesIn2[j];
Tile tile2 = Main.tile[point2.X, point2.Y];
if (tile2.HasTile && tile2.TileType == GlobalBagTile.PortableStationType) {
if (!LastTouchingStation)
SoundEngine.PlaySound(SoundID.SplashWeak);
TouchingStation = true;
break;
}
}
}
public void UpdateHoneyBuff() {
UpdatePlayerPortableStorageOverlap();
int honeyBuffIndex = Player.FindBuffIndex(BuffID.Honey);
bool hadBuff = honeyBuffIndex != -1;
bool? fromStation = HoneyCheck();
if (!hadBuff) {
honeyWetResetTime = 0;
honeyBuffIndex = Player.FindBuffIndex(BuffID.Honey);
}
bool hasHoney = honeyBuffIndex != -1;
if (!hasHoney || lastHoneyBucketLocation < 0)
return;
int context = fromStation == true || honeyWetResetTime < Main.GameUpdateCount || fromStation == null && Player.buffTime[honeyBuffIndex] <= AOEHoneyBuffTime ? ItemSlotContextID.BrightGreenSelected : ItemSlotContextID.Purple;
StorageManager.BagUIs[PortableStation.Instance.BagStorageID].AddSelectedItemSlot(lastHoneyBucketLocation, context);
}
private bool? HoneyCheck() {
if (!VacuumBags.serverConfig.PortableStationCanGiveHoneyBuff)
return false;
if (honeyWetResetTime > Main.GameUpdateCount + HoneyBuffTime && !TouchingStation)
return false;
if (!Player.TryGetModPlayer(out StoragePlayer storagePlayer))
return false;
bool doOnTouchBuff = false;
bool doAOEBuff = false;
bool bucketsActSame = !VacuumBags.serverConfig.PortableStationMustBeTouchedToGetHoneyBuff;
Item[] inv = storagePlayer.Storages[PortableStation.Instance.BagStorageID].Items;
bool doFullCheck = nextFullCheckTime <= Main.GameUpdateCount;
if (!doFullCheck) {
if (lastHoneyBucketLocation > -1) {
Item item = inv[lastHoneyBucketLocation];
if (item.type == ItemID.HoneyBucket) {
doOnTouchBuff = true;
if (bucketsActSame)
doAOEBuff = true;
}
else if (item.type == ItemID.BottomlessHoneyBucket) {
doOnTouchBuff = true;
doAOEBuff = true;
}
else {
doFullCheck = true;
}
if (doAOEBuff)
UpdateNextHoneyFullCheckTime();
}
}
if (doFullCheck) {
//Full check
lastHoneyBucketLocation = -1;
for (int i = 0; i < inv.Length; i++) {
Item item = inv[i];
if (item.type == ItemID.HoneyBucket && lastHoneyBucketLocation == -1) {
lastHoneyBucketLocation = i;
doOnTouchBuff = true;
if (bucketsActSame) {
doAOEBuff = true;
break;
}
}
else if (item.type == ItemID.BottomlessHoneyBucket) {
doOnTouchBuff = true;
doAOEBuff = true;
lastHoneyBucketLocation = i;
break;
}
}
UpdateNextHoneyFullCheckTime();
}
if (!doOnTouchBuff)
return false;
if (TouchingStation) {
Player.AddBuff(BuffID.Honey, HoneyBuffTime);
honeyWetResetTime = 0;
return true;
}
if (!doAOEBuff)
return false;
if (NearPortableStation) {
Player.AddBuff(BuffID.Honey, AOEHoneyBuffTime);
return null;
}
return false;
}
#endregion
}
}