-
Notifications
You must be signed in to change notification settings - Fork 0
/
LansAutoSummon.cs
470 lines (385 loc) · 14.2 KB
/
LansAutoSummon.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
using LansUILib;
using LansUILib.ui;
using Microsoft.Xna.Framework;
using MonoMod.Cil;
using ReLogic.Utilities;
using System;
using System.Collections.Generic;
using System.Reflection;
using Terraria;
using Terraria.Audio;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
using static Terraria.ModLoader.ModContent;
namespace LansAutoSummon
{
public class LansAutoSummon : Mod
{
public static LansAutoSummon inst;
public bool soundIsSentry = false;
public LansAutoSummon()
{
inst = this;
}
public override void Load()
{
base.Load();
Terraria.Audio.IL_SoundPlayer.Play += DisableSoundIfSentry;
}
public void DisableSoundIfSentry(ILContext iLContext)
{
LansUILib.InjectUtils.InjectSkipOnBooleanWithReturnValue(iLContext, ReturnSoundIsSentry, ReturnInvalidSoundSlot);
}
public static bool ReturnSoundIsSentry()
{
return inst.soundIsSentry;
}
public static object ReturnInvalidSoundSlot()
{
return SlotId.Invalid;
}
public override void Unload()
{
base.Unload();
inst = null;
}
}
public class ExpandingInventory
{
public List<SummonSlot> slots = new List<SummonSlot>();
public SummonSlot emptySlot = new SummonSlot();
public bool dirty;
protected string saveTag;
public ExpandingInventory(string saveTag)
{
this.saveTag = saveTag;
}
public void LoadData(TagCompound tag)
{
if (tag.ContainsKey(this.saveTag))
{
slots = (List<SummonSlot>)tag.GetList<SummonSlot>(this.saveTag);
}
}
public void SaveData(TagCompound tag)
{
tag[this.saveTag] = slots;
}
}
public class TrackValue<T>
{
public Func<T> valueFunc;
public T lastValue;
public bool Dirty = true;
public event Action<T> valueChanged;
public TrackValue(Func<T> valueFunc)
{
this.valueFunc = valueFunc;
}
public void Check()
{
Dirty = false;
var v = valueFunc();
if(!v.Equals(lastValue))
{
Dirty = true;
lastValue = v;
valueChanged?.Invoke(v);
}
}
}
public class FixPlayer : ModPlayer
{
public ExpandingInventory summonItems = new ExpandingInventory("summonSlots");
public ExpandingInventory sentryItems = new ExpandingInventory("sentrySlots");
public ExpandingInventory otherItems = new ExpandingInventory("otherSlots");
public bool isSpawning = false;
public TrackValue<float> minionCount;
public TrackValue<int> minionMaxCount;
public TrackValue<int> sentryCount;
public TrackValue<int> sentryMaxCount;
public List<TrackValue<object>> trackValues = new List<TrackValue<object>>();
public bool autoSummonEnabled = true;
public bool tempSummonDisabled = false;
public bool forceSummon = false;
public int currentSentrySlotId = 0;
public override void Initialize()
{
base.Initialize();
minionCount = new TrackValue<float>(getCurrentMinionCount);
minionMaxCount = new TrackValue<int>(delegate() { return Player.maxMinions; });
sentryCount = new TrackValue<int>(getCurrentSentryCount);
sentryMaxCount = new TrackValue<int>(delegate () { return Player.maxTurrets; });
}
public override void LoadData(TagCompound tag)
{
base.LoadData(tag);
summonItems.LoadData(tag);
sentryItems.LoadData(tag);
otherItems.LoadData(tag);
if (tag.ContainsKey("autoSummon"))
{
autoSummonEnabled = tag.GetBool("autoSummon");
}
}
public override void SaveData(TagCompound tag)
{
base.SaveData(tag);
summonItems.SaveData(tag);
sentryItems.SaveData(tag);
otherItems.SaveData(tag);
tag["autoSummon"] = autoSummonEnabled;
}
protected float getCurrentMinionCount()
{
float minCount = 0;
for (int i = 0; i < 1000; i++)
{
if (Main.projectile[i].active && Main.projectile[i].minion && Main.projectile[i].owner == this.Player.whoAmI)
{
minCount += Main.projectile[i].minionSlots;
}
}
return minCount;
}
protected int getCurrentSentryCount()
{
var turrets = 0;
for (int i = 0; i < 1000; i++)
{
if (Main.projectile[i].active && Main.projectile[i].WipableTurret && Main.projectile[i].owner == this.Player.whoAmI)
turrets += 1;
}
return turrets;
}
public void CancelAllMinions()
{
for (int i = 0; i < Main.projectile.Length; i++)
{
if (Main.projectile[i].active && Main.projectile[i].minion && Main.projectile[i].owner == this.Player.whoAmI && !Main.projectile[i].sentry)
{
Main.projectile[i].Kill();
}
}
}
protected bool shouldSummon()
{
// Do nothing if dead
if (this.Player.dead)
{
Player.GetModPlayer<FixPlayer>().tempSummonDisabled = false;
return false;
}
// Do notthing if holding an item
if (Main.mouseItem != null && !Main.mouseItem.IsAir) {
return false;
}
// Do nothing if in middle of swing
if(this.Player.itemAnimation != 0)
{
return false;
}
if (forceSummon)
{
return true;
}
return autoSummonEnabled && !tempSummonDisabled && (summonItems.dirty || isSpawning || minionCount.Dirty || minionMaxCount.Dirty);
}
protected bool shouldSummonSentry()
{
// Do nothing if dead
if (this.Player.dead)
{
Player.GetModPlayer<FixPlayer>().tempSummonDisabled = false;
return false;
}
// Do notthing if holding an item
if (Main.mouseItem != null && !Main.mouseItem.IsAir)
{
return false;
}
// Do nothing if in middle of swing
if (this.Player.itemAnimation != 0)
{
return false;
}
// remove sentries to far away
for (int i = 0; i < 1000; i++)
{
if (Main.projectile[i].active && Main.projectile[i].WipableTurret)
{
if(Vector2.DistanceSquared(Main.projectile[i].Center, Player.Center) > 16*50*16*50)
{
LansAutoSummon.inst.soundIsSentry = true;
Main.projectile[i].Kill();
LansAutoSummon.inst.soundIsSentry = false;
}
}
}
return Player.maxTurrets != getCurrentSentryCount();
}
protected void summon(Item summonItem)
{
var lastScreenPosition = Main.screenPosition + Vector2.Zero;
Main.screenPosition.X = Player.Center.X - Main.MouseScreen.X + Random.Shared.Next(0, 10) * 16 - 5 * 16;
Main.screenPosition.Y = Player.Center.Y - Main.MouseScreen.Y + Random.Shared.Next(0, 10) * 16 - 5 * 16;
var oldItem = this.Player.inventory[this.Player.selectedItem];
this.Player.inventory[this.Player.selectedItem] = summonItem;
var oldControlUseItem = this.Player.controlUseItem;
var oldreleaseUseItem = this.Player.releaseUseItem;
var olditemAnimation = this.Player.itemAnimation;
var olditemTime = this.Player.itemTime;
var olditemAnimationMax = this.Player.itemAnimationMax;
var olditemLocation = this.Player.itemLocation;
var olditemRotation = this.Player.itemRotation;
var olddirection = this.Player.direction;
var oldtoolTime = this.Player.toolTime;
var oldchannel = this.Player.channel;
var oldattackCD = this.Player.attackCD;
var realUseTime = this.Player.HeldItem.useTime;
var realMana = this.Player.HeldItem.mana;
var realSound = this.Player.HeldItem.UseSound;
//this.Player.HeldItem.useTime = 0;
this.Player.HeldItem.mana = 0;
this.Player.HeldItem.UseSound = null;
this.Player.controlUseItem = true;
//this.Player.releaseUseItem = true;
this.Player.itemAnimation = 0;
this.Player.ItemCheck();
this.Player.controlUseItem = false;
while (this.Player.itemAnimation > 0)
{
//this.Player.releaseUseItem = true;
this.Player.ItemCheck();
}
//this.Player.controlUseItem = true;
//this.Player.releaseUseItem = true;
//this.Player.ItemCheck(this.Player.whoAmI);
//this.Player.itemAnimation = 2;
//this.Player.ItemCheck(this.Player.whoAmI);
//this.Player.HeldItem.useTime = realUseTime;
this.Player.HeldItem.mana = realMana;
this.Player.HeldItem.UseSound = realSound;
this.Player.controlUseItem = oldControlUseItem;
this.Player.releaseUseItem = oldreleaseUseItem;
this.Player.itemAnimation = olditemAnimation;
this.Player.itemTime = olditemTime;
this.Player.itemAnimationMax = olditemAnimationMax;
this.Player.itemLocation = olditemLocation;
this.Player.itemRotation = olditemRotation;
this.Player.direction = olddirection;
this.Player.toolTime = oldtoolTime;
this.Player.channel = oldchannel;
this.Player.attackCD = oldattackCD;
this.Player.inventory[this.Player.selectedItem] = oldItem;
Main.screenPosition.X = lastScreenPosition.X;
Main.screenPosition.Y = lastScreenPosition.Y;
}
protected void trySummon(Item summonItem, int summonCount, bool fill, int retryCount)
{
var currentMinionCount = getCurrentMinionCount();
int currentSummonCount = 0;
int currentRetryCount = 0;
while (true)
{
//this.Player.maxMinions
Projectile obj = new Projectile();
obj.SetDefaults(summonItem.shoot);
if (currentMinionCount + obj.minionSlots > this.Player.maxMinions)
{
Main.NewText($"LansAutoSummon: Cannot summon {summonItem.Name} max minions reached. Current: {currentMinionCount} Max: {this.Player.maxMinions} Needed: {obj.minionSlots}");
break;
}
summon(summonItem);
var newCount = getCurrentMinionCount();
if (newCount != currentMinionCount)
{
currentMinionCount = newCount;
currentSummonCount += 1;
currentRetryCount = 0;
}
else
{
currentRetryCount += 1;
}
if (currentRetryCount >= retryCount)
{
break;
}
if (currentSummonCount >= summonCount && !fill)
{
break;
}
}
}
public static bool ItemIsValidSummonItem(Item item)
{
if (item != null && !item.IsAir)
{
if (item.shoot > 0)
{
Projectile obj = new Projectile();
obj.SetDefaults(item.shoot);
if (item.active && (obj.minion|| obj.sentry)) //&& !item.sentry)
{
return true;
}
}
}
return false;
}
public override void OnRespawn()
{
base.OnRespawn();
isSpawning = true;
}
public override void PlayerConnect()
{
base.PlayerConnect();
isSpawning = true;
}
public override void PostUpdate()
{
base.PostUpdate();
if (!Main.dedServ && this.Player.whoAmI == Main.myPlayer)
{
if (shouldSummon())
{
forceSummon = false;
summonItems.dirty = false;
CancelAllMinions();
foreach (var summon in summonItems.slots)
{
if (summon.fill)
{
trySummon(summon.summonWeapon.Item, 1, false, 5);
break;
}
}
foreach (var summon in summonItems.slots)
{
trySummon(summon.summonWeapon.Item, summon.count, summon.fill, 5);
}
isSpawning = false;
}
if (sentryItems.slots.Count > 0)
{
if (shouldSummonSentry())
{
if(currentSentrySlotId >= sentryItems.slots.Count)
{
currentSentrySlotId = 0;
}
trySummon(sentryItems.slots[currentSentrySlotId].summonWeapon.Item, 1, false, 1);
currentSentrySlotId++;
}
}
minionCount.Check();
minionMaxCount.Check();
sentryCount.Check();
sentryMaxCount.Check();
}
}
}
}