forked from auqw/Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoreAdvanced.cs
More file actions
4536 lines (4019 loc) · 164 KB
/
CoreAdvanced.cs
File metadata and controls
4536 lines (4019 loc) · 164 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
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
name: null
description: null
tags: null
*/
//cs_include Scripts/CoreBots.cs
//cs_include Scripts/CoreFarms.cs
using System.ComponentModel;
using System.Diagnostics;
using System.Dynamic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using CommunityToolkit.Mvvm.DependencyInjection;
using Newtonsoft.Json;
using Skua.Core.Interfaces;
using Skua.Core.Models;
using Skua.Core.Models.Items;
using Skua.Core.Models.Monsters;
using Skua.Core.Models.Quests;
using Skua.Core.Models.Shops;
using Skua.Core.Options;
using Skua.Core.Utils;
public class CoreAdvanced
{
private IScriptInterface Bot => IScriptInterface.Instance;
private CoreBots Core => CoreBots.Instance;
private static CoreFarms Farm
{
get => _Farm ??= new CoreFarms();
set => _Farm = value;
}
private static CoreFarms _Farm;
public void ScriptMain(IScriptInterface Bot)
{
Core.RunCore();
}
#region Shop
/// <summary>
/// Buys an item by name from a shop, handling requirements such as XP, Rep, Gold, and merge items.
/// Safely respects stack sizes, inventory, and optional merge index.
/// </summary>
public void BuyItem(string map, int shopID, string itemName, int quant = 1, int shopItemID = 0, int index = 0, bool Log = true)
{
if (Core.CheckInventory(itemName, quant))
return;
Core.Join(map);
Bot.Wait.ForMapLoad(map);
Core.JumpWait();
// Get full shop list; do not pre-filter
List<ShopItem> shopItems = Core.GetShopItems(map, shopID);
ShopItem? item = Core.parseShopItem(shopItems, shopID, itemName, shopItemID);
if (item == null)
return;
int shopQuant = item.Quantity > 0 ? item.Quantity : 1;
_BuyItem(map, shopID, item, quant, shopQuant, shopItemID, index, Log);
}
/// <summary>
/// Buys an item by ID from a shop, handling requirements such as XP, Rep, Gold, and merge items.
/// Safely respects stack sizes, inventory, and optional merge index.
/// </summary>
public void BuyItem(string map, int shopID, int itemID, int quant = 1, int shopQuant = 1, int shopItemID = 0, int index = 0, bool Log = true)
{
if (Core.CheckInventory(itemID, quant))
return;
// Inventory space check
if (Bot.Inventory.FreeSlots <= 0 && !Bot.Inventory.Contains(itemID))
{
if (Log) Core.Logger("❌ Inventory full, cannot buy items.");
return;
}
Core.Join(map);
Bot.Wait.ForMapLoad(map);
Core.JumpWait();
// Wait for combat to end
if (Bot.Player.InCombat || Bot.Player.HasTarget)
{
Core.JumpWait();
Bot.Wait.ForCombatExit();
}
// Get full shop list
List<ShopItem> shopItems = Core.GetShopItems(map, shopID);
ShopItem? item = Core.parseShopItem(shopItems, shopID, itemID, shopItemID);
if (item == null)
{
if (Log) Core.Logger($"❌ Item {itemID} not found in shop {shopID} on {map}");
return;
}
// House space check if item is a house-storable category
if (!string.IsNullOrEmpty(item.CategoryString) && Core.CategoryStrings.Contains(item.CategoryString))
{
if (Bot.House.FreeSlots <= 0 && !Bot.House.Contains(itemID))
{
if (Log) Core.Logger("❌ House full, cannot store this item.");
return;
}
}
int effectiveShopQuant = item.Quantity > 0 ? item.Quantity : shopQuant;
_BuyItem(map, shopID, item, quant, effectiveShopQuant, shopItemID, index, Log);
}
private void _BuyItem(string map, int shopID, ShopItem item, int quant = 1, int shopQuant = 1, int shopItemID = 1, int index = 0, bool Log = true)
{
// Quantity per purchase from shop
int itemStack = item.Quantity > 0 ? item.Quantity : 1;
// Handle requirements first
if (item.Requirements != null)
{
foreach (ItemBase req in item.Requirements)
{
int stacksNeeded = (int)Math.Ceiling((double)quant / itemStack);
int totalNeeded = stacksNeeded * req.Quantity;
if (Core.CheckInventory(req.ID, totalNeeded))
continue;
// Special farm cases
if (req.Name.Contains("Gold Voucher"))
{
Farm.Voucher(req.Name, totalNeeded);
continue;
}
if (req.Name == "Dragon Runestone")
{
Farm.DragonRunestone(totalNeeded);
continue;
}
// Try to buy from shop if available
while (!Bot.ShouldExit && !Core.CheckInventory(req.ID, totalNeeded))
{
if (Bot.Map.Name != map)
Core.Join(map);
ShopItem? shopReqItem = Core.GetShopItems(map, shopID)
.FirstOrDefault(x => x.ID == req.ID);
if (shopReqItem != null)
BuyItem(map, shopID, req.ID, totalNeeded, shopReqItem.ShopItemID, Log: Log);
else
{
Core.Logger(
$"Missing requirement: {req.Name} [{req.ID}] in shop {shopID} on map {map}. " +
$"It may be a drop, daily, or special item."
);
return;
}
}
}
}
// Ensure requirements are satisfied before main purchase
GetItemReq(item, quant);
// Rejoin map & load shop safely
if (Bot.Map.Name != map)
Core.Join(map);
List<ShopItem> shopItems = Core.GetShopItems(map, shopID)
.Where(x =>
x.ID == item.ID &&
!(x.Coins && x.Cost > 0) &&
(item.Requirements?.All(r => Core.CheckInventory(r.ID, r.Quantity)) ?? true)
)
.ToList();
ShopItem? mainItem = shopItems.Count > index ? shopItems[index] : shopItems.FirstOrDefault();
if (mainItem == null)
{
Core.Logger($"❌ Failed to find {item.Name} in shop {shopID} on map {map}");
return;
}
// Calculate buy amount respecting stack size and max stack
int currentStock = Bot.Inventory.Items
.Concat(Bot.Bank.Items)
.Concat(Bot.House.Items)
.Concat(Bot.TempInv.Items)
.Where(x => x.ID == mainItem.ID)
.Sum(x => x.Quantity);
int buyAmount = Core._CalcBuyQuantity(mainItem, quant);
if (buyAmount <= 0)
{
Core.Logger($"Cannot buy {mainItem.Name}, max stack reached ({currentStock}/{mainItem.MaxStack})");
return;
}
Core.BuyItem(map, shopID, mainItem.ID, buyAmount, mainItem.ShopItemID != 1 ? mainItem.ShopItemID : shopItemID, Log: Log);
Core.Sleep();
// Verify purchase
if (!Core.CheckInventory(mainItem.ID, quant))
{
Core.Logger($"❌ Failed to buy {mainItem.Name} ({quant}x)");
foreach (var req in mainItem.Requirements.Where(r => r != null && !Core.CheckInventory(r.ID, r.Quantity)))
Core.Logger($"⚠️ Missing requirement: {req.Name} x{req.Quantity}");
}
}
/// <summary>
/// Ensures that all necessary requirements (Experience, Reputation, Gold, and specific items)
/// are met in order to purchase an item. This includes verifying player level, farming or purchasing
/// required reputation, acquiring specific items such as Gold Vouchers and Dragon Runestones,
/// and ensuring enough gold is available for the transaction.
/// </summary>
/// <param name="item">
/// The <see cref="ShopItem"/> object that contains all the details about the item,
/// including its requirements like reputation, level, gold cost, and additional items needed.
/// </param>
/// <param name="quant">
/// The quantity of the item needed for purchase. The default value is 1, but can be adjusted
/// to handle cases where multiple units of the item are required.
/// </param>
public void GetItemReq(ShopItem item, int quant = 1)
{
if (item?.Requirements == null)
{
Core.Logger("Invalid item or missing requirements.");
return;
}
// Ensure required reputation for faction-based items
if (
!string.IsNullOrEmpty(item.Faction)
&& item.Faction != "None"
&& item.RequiredReputation > 0
&& Farm.FactionRank(item.Faction) < item.RequiredReputation
)
{
Core.Logger(
$"Farming reputation for {item.Faction} (Required: {item.RequiredReputation})"
);
runRep(item.Faction, Core.PointsToLevel(item.RequiredReputation));
}
// Level up if the item requires a higher player level
if (item.Level > Bot.Player.Level)
{
Core.Logger($"Farming experience to reach level {item.Level}");
Farm.Experience(Math.Min(item.Level, 100));
}
// Farm gold if the item costs gold and isn't a premium currency purchase
if (!item.Coins && item.Cost > 0)
{
int GoldtoFarm = Math.Min(item.Cost * quant, 100000000); // 100m gold cap
Farm.Gold(GoldtoFarm);
}
// Handle Gold Vouchers (multiple types possible)
if (item.Requirements.Any(x => x != null && x.Name.StartsWith("Gold Voucher")))
{
foreach (
ItemBase req in item.Requirements.Where(x =>
x != null && x.Name.StartsWith("Gold Voucher")
)
)
{
Farm.Voucher(req.Name, req.Quantity);
}
}
// Handle Dragon Runestone farming if required
if (
item.Requirements != null
&& item.Requirements.Any(x => x != null && x.Name.StartsWith("Dragon Runestone"))
)
{
ItemBase? runestoneReq = item.Requirements.FirstOrDefault(x =>
x != null && x.Name == "Dragon Runestone"
);
if (runestoneReq != null)
Farm.DragonRunestone(runestoneReq.Quantity);
}
// Warn if a temp item is missing
if (item.Requirements != null)
foreach (
ItemBase req in item.Requirements.Where(x =>
x != null && x.Temp && x.Quantity > Bot.TempInv.GetQuantity(x.ID)
)
)
Core.Logger(
$"Temp item: {req.Name}, quant needed: {req.Quantity}... did the bot not farm them?"
);
}
private void runRep(string faction, int rank)
{
faction = faction.Replace(" ", "");
Type farmClass = Farm.GetType();
MethodInfo? theMethod = farmClass.GetMethod(faction + "REP");
if (theMethod == null)
{
Core.Logger(
"Failed to find "
+ faction
+ "REP. Make sure you have the correct name and capitalization."
);
return;
}
try
{
switch (faction.ToLower())
{
case "alchemy":
case "blacksmith":
theMethod.Invoke(Farm, new object[] { rank, true });
break;
case "bladeofawe":
theMethod.Invoke(Farm, new object[] { rank, false });
break;
default:
theMethod.Invoke(Farm, new object[] { rank });
break;
}
}
catch
{
Core.Logger(
$"Faction {faction} has invalid paramaters, please report",
messageBox: true,
stopBot: true
);
}
}
#region old StartBuyAllMerge, revert if broke
/// <summary>
/// Buys merge items from a shop based on specified options. Filters ShopItems to ensure uniqueness by ID and ShopItemID,
/// selecting items based on Upgrade requirements and excluding those ending with "insignia".
/// </summary>
/// <param name="map">The map from which the shop is loaded.</param>
/// <param name="shopID">The shop ID to load shop data.</param>
/// <param name="findIngredients">Action determining where to retrieve items.</param>
/// <param name="buyOnlyThis">Optional. Limits purchases to a specific item.</param>
/// <param name="itemBlackList">Optional. List of excluded items.</param>
/// <param name="buyMode">Optional. Specifies buying mode.</param>
/// <param name="Group">Optional. Specifies group selection method.</param>
/// <param name="ShopItemID">Optional. Specifies ShopItem ID.</param>
/// <param name="Log">Optional. Enables logging.</param>
// We'll use this later
// public void StartBuyAllMerge(
// string map,
// int shopID,
// Action findIngredients,
// string? buyOnlyThis = null,
// string[]? itemBlackList = null,
// mergeOptionsEnum? buyMode = null,
// string Group = "First",
// int ShopItemID = 0,
// bool Log = true
// )
// {
// #region Setup and Initialization
// if (
// buyOnlyThis == null
// && buyMode == null
// && Bot.Config != null
// && !Bot.Config.Get<bool>(CoreBots.Instance.SkipOptions)
// )
// Bot.Config!.Configure();
// int mode = 0;
// if (buyOnlyThis != null)
// mode = (int)mergeOptionsEnum.all;
// else if (buyMode != null)
// mode = (int)buyMode;
// else if (
// Bot.Config != null
// && Bot.Config.MultipleOptions.Any(o =>
// o.Value.Any(x => x.Category == "Generic" && x.Name == "mode")
// )
// )
// mode = (int)Bot.Config.Get<mergeOptionsEnum>("Generic", "mode");
// else
// Core.Logger(
// "Invalid setup detected for StartBuyAllMerge. Please report",
// messageBox: true,
// stopBot: true
// );
// matsOnly = mode == 2;
// // HashSet for tracking unique item IDs to prevent redundant operations
// HashSet<int> uniqueItemIds = new(
// new[]
// {
// Bot.Bank.Items.Select(item => item.ID),
// Bot.TempInv.Items.Select(item => item.ID),
// Bot.House.Items.Select(item => item.ID),
// Bot.Inventory.Items.Select(item => item.ID),
// }.SelectMany(id => id)
// );
// // Filter shop items based on various conditions
// List<ShopItem> shopItems = Core.GetShopItems(map, shopID)
// .GroupBy(item => new
// {
// item.Name,
// item.ID,
// item.ShopItemID,
// })
// .Select(group =>
// {
// IOrderedEnumerable<ShopItem> orderedGroup = group.OrderBy(item =>
// item.ShopItemID != group.First().ShopItemID
// );
// return Group == "First" ? orderedGroup.First() : orderedGroup.Last();
// })
// .Where(x => !x.Name.ToLower().EndsWith("insignia"))
// .Where(x => !uniqueItemIds.Contains(x.ID))
// .ToList();
// uniqueItemIds = new HashSet<int>(); // Reset for re-use
// List<ShopItem> items = new();
// bool memSkipped = false;
// // Process shop items based on various conditions
// foreach (ShopItem item in shopItems)
// {
// if (
// miscCatagories.Contains(item.Category)
// || (!string.IsNullOrEmpty(buyOnlyThis) && buyOnlyThis != item.Name)
// || (
// itemBlackList != null
// && itemBlackList.Any(x => x.ToLower() == item.Name.ToLower())
// )
// )
// continue;
// if (
// Core.IsMember
// || !item.Upgrade
// || item.Requirements.Any(x =>
// x != null && Bot.Shops.Items.Any(x => x != null && x.Upgrade && !Core.IsMember)
// )
// )
// {
// if (mode == 3)
// {
// if (Bot.Config!.Get<bool>("Select", $"{item.ID}"))
// items.Add(item);
// }
// else if (mode != 1)
// items.Add(item);
// else if (item.Coins)
// items.Add(item);
// }
// else if (mode == 3 && Bot.Config!.Get<bool>("Select", $"{item.ID}"))
// {
// Core.Logger($"\"{item.Name}\" will be skipped, as you aren't a member.");
// memSkipped = true;
// }
// }
// if (items.Count == 0)
// {
// Core.Logger(
// $"Found {items.Count} items to purchase from shop [{shopID}] on map [{map}]."
// );
// HandleNoItemsFound(mode, memSkipped);
// return;
// }
// #endregion
// int t = 0;
// // Why did we need the `for ( int i = 0; i < 2; i++)`?
// foreach (ShopItem item in items)
// {
// if (Core.CheckInventory(item.ID, toInv: false))
// continue;
// if (item.Upgrade && !Core.IsMember)
// {
// Core.Logger($"Skipping {item.Name} [{item.ID}] as it is member-only.");
// continue;
// }
// if (!matsOnly)
// {
// Core.Logger($"Farming to buy {item.Name} (#{t++}/{items.Count})");
// }
// foreach (ItemBase req in item.Requirements)
// {
// EnsureShopLoaded(map, shopID);
// HandleItemRequirements(req, req.Quantity, findIngredients);
// }
// if (item.Requirements.All(x => x != null && Core.CheckInventory(x.ID, x.Quantity)))
// {
// if (!matsOnly)
// Core.Logger($"Buying {item.Name} (#{t++}/{items.Count})");
// // Attempt to purchase the required quantity of the shop item
// BuyItem(map, shopID, item.ID, shopItemID: item.ShopItemID, Log: Log);
// Bot.Wait.ForPickup(item.ID);
// if (Core.CheckInventory(item.ID, item.Quantity))
// {
// continue;
// }
// else
// {
// IEnumerable<string> missing = item
// .Requirements.Where(x => !Core.CheckInventory(x.ID, x.Quantity))
// .Select(x => $"\"{x.Name} x{x.Quantity}\"");
// Core.Logger(
// $"Failed to meet requirements for {item.Name} [{item.ID}] due to missing: {string.Join(", ", missing)}."
// );
// continue;
// }
// }
// }
// // Helper methods
// bool EnsureShopLoaded(string? map, int shopID)
// {
// if (map == null)
// {
// Core.Logger("Map is null, unable to load shop.");
// return false;
// }
// Core.Join(map);
// Bot.Wait.ForMapLoad(map);
// while (!Bot.ShouldExit && Bot.Shops.ID != shopID)
// {
// Bot.Shops.Load(shopID);
// Bot.Wait.ForActionCooldown(GameActions.LoadShop);
// Bot.Wait.ForTrue(() => Bot.Shops.IsLoaded && Bot.Shops.ID == shopID, 20);
// Core.Sleep(1000);
// if (Bot.Shops.ID == shopID)
// return true;
// }
// return true;
// }
// void HandleItemRequirements(ItemBase? Req, int ReqQuant, Action findIngredients)
// {
// if (Req == null)
// {
// Core.Logger("Requirement item is null, cannot process.");
// return;
// }
// if (Core.CheckInventory(Req.ID, ReqQuant))
// return;
// EnsureShopLoaded(map, shopID);
// ShopItem? wasinshop = Bot.Shops.Items.FirstOrDefault(x => x.ID == Req.ID);
// if (wasinshop != null)
// {
// Core.Logger($"Item: \"{Req.Name} [{Req.ID}\"] is in the shop!");
// while (!Bot.ShouldExit && !Core.CheckInventory(Req.ID, ReqQuant))
// {
// // for requirements that are in the shop, but are just buyable with gold. (excludes ac buyable items)
// if (
// wasinshop.Requirements.Count == 0
// && ((wasinshop.Coins && wasinshop.Cost <= 0) || !wasinshop.Coins)
// ) //|| wasinshop.Name.Contains("Gold Voucher") || wasinshop.Name.Contains("Dragon Runestone"))
// {
// // Otherwise buy the item directly
// BuyItem(
// map,
// shopID,
// Req.ID,
// ReqQuant,
// shopItemID: wasinshop.ShopItemID,
// Log: Log
// );
// Bot.Wait.ForPickup(Req.ID);
// }
// else
// {
// // Items not in the shop, so we have to get it externally
// if (wasinshop.Name.Contains("Dragon Runestone"))
// {
// Farm.DragonRunestone(ReqQuant);
// continue;
// }
// if (wasinshop.Name.Contains("Gold Voucher"))
// {
// Farm.Voucher(wasinshop.Name, ReqQuant);
// continue;
// }
// IngredientWasintheShop(wasinshop, ReqQuant);
// }
// if (Core.CheckInventory(Req.ID, ReqQuant))
// break;
// else
// {
// Core.Logger(
// $"Failed to meet requirements for \"{Req.Name}\" [{Req.ID}] x{ReqQuant}, Retrying the farm (items may have been used)."
// );
// }
// }
// }
// else if (
// Req?.Name?.Contains("Gold Voucher") == true
// || Req?.Name?.Contains("Dragon Runestone") == true
// )
// {
// // Handle special cases for Gold Vouchers and Dragon Runestones
// if (Req.Name?.Contains("Gold Voucher") == true)
// {
// Farm.Voucher(Req.Name, ReqQuant);
// return;
// }
// if (Req.Name?.Contains("Dragon Runestone") == true)
// {
// Farm.DragonRunestone(ReqQuant);
// return;
// }
// }
// else if (wasinshop == null)
// {
// // Items not in the shop, so we have to get it externally
// if (Req != null)
// {
// externalItem = Req;
// externalQuant = Req.Quantity;
// Core.AddDrop(externalItem.ID);
// Core.Logger(
// $"{externalItem.Name} [{externalItem.ID}] is an external item (not from this shop), attempting to farm it from The ingredient list."
// );
// findIngredients();
// Bot.Wait.ForPickup(externalItem.ID);
// }
// else
// {
// Core.Logger("Cannot process null requirement item.");
// return;
// }
// }
// Bot.Wait.ForPickup(Req!.ID);
// }
// void IngredientWasintheShop(ShopItem item, int craftingQ)
// {
// // Ensure we are checking for items in the shop and inventory properly
// if (item == null)
// {
// Core.Logger($"Item not found in the shop.");
// return;
// }
// // If item is already in inventory, no need to continue
// if (Core.CheckInventory(item.ID, item.Quantity))
// return;
// // Ensure shop is loaded before proceeding
// EnsureShopLoaded(map, shopID);
// foreach (ItemBase req in item.Requirements)
// {
// if (Core.CheckInventory(req.ID, req.Quantity))
// continue;
// EnsureShopLoaded(map, shopID);
// int ReqQuant = req.Quantity * craftingQ;
// ShopItem? wasinshop = Bot.Shops.Items.FirstOrDefault(x => x.ID == req.ID);
// if (wasinshop != null && !MergeItemisinShopExceptions.Contains(req.Name))
// {
// Core.Logger($"Item: \"{wasinshop.Name} [{wasinshop.ID}\"] is in the shop.");
// ReqQuant = Math.Min(ReqQuant, wasinshop.MaxStack);
// // for requirements that are in the shop, but are just buyable with gold. (excludes ac buyable items)
// if (wasinshop.Requirements.Count <= 0 && wasinshop.Cost <= 0)
// {
// BuyItem(
// map,
// shopID,
// wasinshop.ID,
// ReqQuant,
// shopItemID: wasinshop.ShopItemID,
// Log: Log
// );
// Bot.Wait.ForPickup(wasinshop.ID);
// }
// else
// {
// // Items not in the shop, so we have to get it externally
// if (req.Name.Contains("Dragon Runestone"))
// {
// Farm.DragonRunestone(ReqQuant);
// continue;
// }
// if (req.Name.Contains("Gold Voucher"))
// {
// Farm.Voucher(req.Name, ReqQuant);
// continue;
// }
// // Core.Logger($"Requirements: {string.Join(", ", wasinshop.Requirements)}");
// IngredientWasintheShop(wasinshop, ReqQuant);
// }
// continue;
// }
// else if (wasinshop == null || MergeItemisinShopExceptions.Contains(req.Name))
// {
// // Items not in the shop, so we have to get it externally
// if (req.Name.Contains("Dragon Runestone"))
// {
// Farm.DragonRunestone(ReqQuant);
// continue;
// }
// if (req.Name.StartsWith("Gold Voucher"))
// {
// Farm.Voucher(req.Name, ReqQuant);
// continue;
// }
// externalItem = req;
// externalQuant = ReqQuant;
// Core.AddDrop(externalItem.ID);
// Core.Logger(
// $"{externalItem.Name} [{externalItem.ID}] is an external item (not a shop item), attempting to farm it from The ingredient list."
// );
// findIngredients();
// }
// }
// EnsureShopLoaded(map, shopID);
// if (item.Requirements.All(x => x != null && Core.CheckInventory(x.ID, x.Quantity)))
// {
// // If all requirements are met, attempt to buy the item
// if (!matsOnly)
// Core.Logger($"Buying {item.Name} [{item.ID}] from the shop.");
// // Attempt to purchase the Requirement of Main / Sub-Main item
// BuyItem(map, shopID, item.ID, craftingQ, shopItemID: item.ShopItemID, Log: Log);
// Bot.Wait.ForPickup(item.ID);
// }
// else
// // If the purchase was unsuccessful, log the failure
// Core.Logger($"Failed to meet requirements for {item.Name} [{item.ID}].");
// }
// void HandleNoItemsFound(int mode, bool memSkipped)
// {
// if (buyOnlyThis != null)
// return;
// switch (mode)
// {
// case 0:
// case 2:
// Core.Logger("The bot fetched 0 items to farm. Something must have gone wrong.");
// break;
// case 1:
// if (shopItems.All(x => !x.Coins))
// Core.Logger(
// "The bot fetched 0 items to farm. This is because none of the items in this shop are AC tagged."
// );
// else
// Core.Logger(
// "The bot fetched 0 items to farm. Something must have gone wrong."
// );
// break;
// case 3:
// if (memSkipped)
// Core.Logger(
// "The bot fetched 0 items to farm. This is because you aren't a member."
// );
// else
// Core.Logger(
// "The bot fetched 0 items to farm. Something must have gone wrong."
// );
// break;
// }
// }
// }
#endregion
/// <summary>
/// Buys merge items from a shop based on specified options. Filters ShopItems to ensure uniqueness by ID and ShopItemID,
/// selecting items based on Upgrade requirements and excluding those ending with "insignia".
/// </summary>
/// <param name="map">The map from which the shop is loaded.</param>
/// <param name="shopID">The shop ID to load shop data.</param>
/// <param name="findIngredients">Action determining where to retrieve items.</param>
/// <param name="buyOnlyThis">Optional. Limits purchases to a specific item.</param>
/// <param name="itemBlackList">Optional. List of excluded items.</param>
/// <param name="buyMode">Optional. Specifies buying mode.</param>
/// <param name="Group">Optional. Specifies group selection method.</param>
/// <param name="ShopItemID">Optional. Specifies ShopItem ID.</param>
/// <param name="Log">Optional. Enables logging.</param>
public void StartBuyAllMerge(
string map,
int shopID,
Action findIngredients,
string? buyOnlyThis = null,
string[]? itemBlackList = null,
mergeOptionsEnum? buyMode = null,
string Group = "First",
int ShopItemID = 0,
bool Log = true
)
{
#region Setup and Initialization
if (
buyOnlyThis == null
&& buyMode == null
&& Bot.Config != null
&& !Bot.Config.Get<bool>(CoreBots.Instance.SkipOptions)
)
Bot.Config!.Configure();
int mode = 0;
if (buyOnlyThis != null)
mode = (int)mergeOptionsEnum.all;
else if (buyMode != null)
mode = (int)buyMode;
else if (
Bot.Config != null
&& Bot.Config.MultipleOptions.Any(o =>
o.Value.Any(x => x.Category == "Generic" && x.Name == "mode")
)
)
mode = (int)Bot.Config.Get<mergeOptionsEnum>("Generic", "mode");
else
Core.Logger(
"Invalid setup detected for StartBuyAllMerge. Please report",
messageBox: true,
stopBot: true
);
matsOnly = mode == 2;
// HashSet for tracking unique item IDs to prevent redundant operations
HashSet<int> uniqueItemIds = new(
new[]
{
Bot.Bank.Items.Select(item => item.ID),
Bot.TempInv.Items.Select(item => item.ID),
Bot.House.Items.Select(item => item.ID),
Bot.Inventory.Items.Select(item => item.ID),
}.SelectMany(id => id)
);
// Filter shop items based on various conditions
List<ShopItem> shopItems = Core.GetShopItems(map, shopID)
.GroupBy(item => new
{
item.Name,
item.ID,
item.ShopItemID,
})
.Select(group =>
{
IOrderedEnumerable<ShopItem> orderedGroup = group.OrderBy(item =>
item.ShopItemID != group.First().ShopItemID
);
return Group == "First" ? orderedGroup.First() : orderedGroup.Last();
})
.Where(x => !x.Name.ToLower().EndsWith("insignia"))
.Where(x => !uniqueItemIds.Contains(x.ID))
.ToList();
uniqueItemIds = new HashSet<int>(); // Reset for re-use
List<ShopItem> items = new();
bool memSkipped = false;
// Process shop items based on various conditions
foreach (ShopItem item in shopItems)
{
if (
miscCatagories.Contains(item.Category)
|| (!string.IsNullOrEmpty(buyOnlyThis) && buyOnlyThis != item.Name)
|| (
itemBlackList != null
&& itemBlackList.Any(x => x.ToLower() == item.Name.ToLower())
)
)
continue;
if (
Core.IsMember
|| !item.Upgrade
|| item.Requirements.Any(x =>
x != null && Bot.Shops.Items.Any(x => x != null && x.Upgrade && !Core.IsMember)
)
)
{
if (mode == 3)
{
if (Bot.Config!.Get<bool>("Select", $"{item.ID}"))
items.Add(item);
}
else if (mode != 1)
items.Add(item);
else if (item.Coins)
items.Add(item);
}
else if (mode == 3 && Bot.Config!.Get<bool>("Select", $"{item.ID}"))
{
Core.Logger($"\"{item.Name}\" will be skipped, as you aren't a member.");
memSkipped = true;
}
}
if (items.Count <= 0)
{
HandleNoItemsFound(mode, memSkipped);
return;
}
#endregion
int t = 0;
foreach (ShopItem item in items)
{
if (Core.CheckInventory(item.ID, toInv: false))
{
Core.Logger($"{item.Name} Owned x{Bot.Inventory.GetQuantity(item.ID)}/{1}");
continue;
}
if (item.Upgrade && !Core.IsMember)
{
Core.Logger($"Skipping {item.Name} [{item.ID}] as it is member-only.");
continue;
}
if (!matsOnly)
{
Core.Logger($"Farming to buy {item.Name} (#{t++}/{items.Count})");
}
// Process all requirements for this item
ProcessItemWithDependencies(item, 1, map, shopID, findIngredients);
// After dependencies are handled, check if we can buy the main item
EnsureShopLoaded(map, shopID);
if (item.Requirements.All(x => x != null && Core.CheckInventory(x.ID, x.Quantity)))
{
if (!matsOnly)
Core.Logger($"Buying {item.Name} (#{t}/{items.Count})");
BuyItem(map, shopID, item.ID, shopItemID: item.ShopItemID, Log: Log);
Bot.Wait.ForPickup(item.ID);
if (!Core.CheckInventory(item.ID, item.Quantity))
{
IEnumerable<string> missing = item
.Requirements.Where(x => x != null && !Core.CheckInventory(x.ID, x.Quantity))
.Select(x => $"\"{x.Name} x{x.Quantity}\"");
Core.Logger(
$"Failed to meet requirements for {item.Name} [{item.ID}] due to missing: {string.Join(", ", missing)}."
);
}
}
}
#region Helper Methods
bool EnsureShopLoaded(string? map, int shopID)
{
if (map == null)
{
Core.Logger("Map is null, unable to load shop.");
return false;
}
Core.Join(map);
Bot.Wait.ForMapLoad(map);
while (!Bot.ShouldExit && Bot.Shops.ID != shopID)
{
Bot.Shops.Load(shopID);
Bot.Wait.ForActionCooldown(GameActions.LoadShop);
Bot.Wait.ForTrue(() => Bot.Shops.IsLoaded && Bot.Shops.ID == shopID, 20);