-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMQ2AASpend.cpp
More file actions
1423 lines (1200 loc) · 37.3 KB
/
MQ2AASpend.cpp
File metadata and controls
1423 lines (1200 loc) · 37.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
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
// MQ2AASpend.cpp : Redguides exclusive ini based auto AA purchaser
// v1.0 - Sym - 04-23-2012
// v1.1 - Sym - 05-26-2012 - Updated to spends aa on normal level ding and basic error checking on ini string
// v2.0 - Eqmule 11-23-2015 - Updated to not rely on the UI or macrocalls.
// v2.1 - Eqmule 04-23-2016 - Updated to bruteforce AA in the correct order.
// v2.1 - Eqmule 05-18-2016 - Added a feature to bruteforce Bonus AA first.
// v2.2 - Eqmule 07-22-2016 - Added string safety.
// v2.3 - Eqmule 08-30-2016 - Fixed buy aa so it will take character level into account when buying aa's
// v2.4 - Eqmule 09-25-2016 - Fixed buy aa so it will not try to buy aa's with an ID of 0 (like Hidden Communion of the Cheetah)
// v2.5 - Eqmule 12-17-2016 - Added SpendOrder as a ini setting. Defualt is still class,focus,arch,gen,special.
// v2.6 - Sic 01-04-2020 - Updated Bonus to reflect AutoGrant as of 12/19/2019 gives autogrant up to and including TBM, xpac #22
#include <mq/Plugin.h>
PreSetup("MQ2AASpend");
PLUGIN_VERSION(2.6);
// The Alternate Ability length converted to decimal is length 10 for a total of 20ish chars, add some padding in case that changes.
constexpr int MAX_BUYLINE = 32;
// AutoGrant is now an "Automatic Script" per DBG current expansion - 4.
const int iAutoGrantExpansion = NUM_EXPANSIONS - 4;
struct AAInfo
{
std::string name;
std::string level;
AAInfo(std::string name_, std::string level_)
: name(std::move(name_)), level(std::move(level_)) {}
};
bool bInitDone = false;
bool bDebug = false;
//----------------------------------------------------------------------------
// AA Settings
std::vector<AAInfo> vAAList;
bool bAutoSpend = false;
bool bAutoSpendNow = false;
bool bBruteForce = false;
bool bBruteForceBonusFirst = false;
int iBankPoints = 0;
int doBruteForce = 0;
int doBruteForceBonusFirst = 0;
bool doAutoSpend = false;
const std::vector<int> DefaultSpendOrder = { 3, 5, 2, 1, 4 }; // default order is: class, focus, arch, gen, special
const std::string DefaultSpendOrderString = "35214";
std::vector<int> SpendOrder = DefaultSpendOrder;
std::string SpendOrderString = DefaultSpendOrderString;
static void UpdateSpendOrder();
//----------------------------------------------------------------------------
// Merc AA Settings
#if HAS_MERCENARY_AA
std::vector<AAInfo> vMercAAList;
bool bUseCurrentMercType = false;
bool bAutoSpendMerc = false;
bool bBruteForceMerc = false;
int doBruteForceMerc = 0;
bool doAutoSpendMerc = false;
const std::vector<std::string> MercCategories = {
"", // 0
"General", // 1
"Tank", // 2
"Healer", // 3
"Melee DPS", // 4
"Caster DPS" // 5
};
const std::vector<int> DefaultMercSpendOrder = { 3, 2, 4, 5, 1 };
const std::string DefaultMercSpendOrderString = "32451";
std::string MercSpendOrderString = DefaultMercSpendOrderString;
std::vector<int> MercSpendOrder = DefaultMercSpendOrder;
static int GetCurrentMercenaryType();
static void UpdateMercSpendOrder();
static std::string GetMercSpendOrderDisplayString();
static const MercenaryAbilitiesData* FindMercAbilityByName(std::string_view abilityName);
#endif // HAS_MERCENARY_AA
void ShowHelp()
{
WriteChatf("\atMQ2AASpend :: v%1.2f :: by Sym\ax", MQ2Version);
WriteChatf("/aaspend :: Lists command syntax");
if (bInitDone)
{
WriteChatf("/aaspend status :: Shows current status");
WriteChatf("/aaspend add \"AA Name\" maxlevel :: Adds AA Name to ini file, will not purchase past max level. Use M to specify max level");
WriteChatf("/aaspend del \"AA Name\" :: Deletes AA Name from ini file");
WriteChatf("/aaspend buy \"AA Name\" :: Buys a particular AA. Ignores bank setting.");
WriteChatf("/aaspend brute on|off|now :: Buys first available ability on ding, or now if specified. Default \ar*OFF*\ax");
WriteChatf("/aaspend bonus on|off|now :: Buys first available ability BONUS AA on ding, or now if specified. Default \ar*OFF*\ax");
WriteChatf("/aaspend auto on|off|now :: Autospends AA's based on ini on ding, or now if specified. Default \ag*ON*\ax");
WriteChatf("/aaspend bank # :: Keeps this many AA points banked before auto/brute spending. Default \ay0\ax.");
WriteChatf("/aaspend order ##### :: Sets the Spend Order.\ay Default is 35214 (class, focus, arch, gen, special)\ax.");
#if HAS_MERCENARY_AA
WriteChatf("MQ2AASpend Merc commands:");
WriteChatf("/aaspend mercadd \"AA Name\" maxlevel :: Adds Merc AA Name to ini file, will not purchase past max level. Use M to specify max level");
WriteChatf("/aaspend mercdel \"AA Name\" :: Deletes Merc AA Name from ini file");
WriteChatf("/aaspend mercbuy \"AA Name\" :: Buys a particular Merc AA.");
WriteChatf("/aaspend mercbrute on|off|now :: Brute mode for Merc AA. Default \ar*OFF*\ax");
WriteChatf("/aaspend mercauto on|off|now :: Autospends Merc AA's based on ini on ding, or now if specified. Default \ar*OFF*\ax");
WriteChatf("/aaspend mercorder ##### :: Sets the Merc Spend Order.\ay Default is 32451 (general=1, tank=2, healer=3, melee=4, caster=5)\ax.");
WriteChatf("/aaspend mercorder auto on|off :: Set the Merc Spend Order to always prioritize your current merc type. Default \ar*OFF*\ax");
#endif // HAS_MERCENARY_AA
}
}
void ShowStatus()
{
WriteChatf("\atMQ2AASpend :: Status\ax");
WriteChatf("Brute Force Mode: %s", bBruteForce ? "\agon\ax" : "\aroff\ax");
WriteChatf("Brute Force Bonus AA First: %s", bBruteForceBonusFirst ? "\agon\ax" : "\aroff\ax");
WriteChatf("Auto Spend Mode: %s", bAutoSpend ? "\agon\ax" : "\aroff\ax");
WriteChatf("Banking \ay%d\ax points", iBankPoints);
UpdateSpendOrder();
WriteChatf("Spend Order: \ag%s\ax", SpendOrderString.c_str());
WriteChatf("INI has \ay%d\ax abilities listed", vAAList.size());
#if HAS_MERCENARY_AA
WriteChatf("\atMQ2AASpend :: Merc Status\ax");
WriteChatf("Merc Brute Prioritize Merc Type: %s - Will prioritize AA for your active mercenary type", bUseCurrentMercType ? "\agon\ax" : "\aroff\ax");
WriteChatf("Merc Brute Force Mode: %s", bBruteForceMerc ? "\agon\ax" : "\aroff\ax");
WriteChatf("Merc Auto Spend Mode: %s", bAutoSpendMerc ? "\agon\ax" : "\aroff\ax");
std::string displayString = GetMercSpendOrderDisplayString();
WriteChatf("Merc Spend Order: \ag%s\ax", displayString.c_str());
WriteChatf("INI has \ay%d\ax merc abilities listed", vMercAAList.size());
#endif // HAS_MERCENARY_AA
}
static void UpdateSpendOrder()
{
if (SpendOrderString.empty())
{
SpendOrder = DefaultSpendOrder;
SpendOrderString = DefaultSpendOrderString;
return;
}
SpendOrder.clear();
for (char ch : SpendOrderString)
{
int val = ch - '0';
if (val >= 0 && val <= 10)
SpendOrder.push_back(val);
}
}
#if HAS_MERCENARY_AA
// SpendOrderString -> SpendOrder with auto merc type factored in
static void UpdateMercSpendOrder()
{
std::vector<int> tempOrder;
if (MercSpendOrderString.empty())
{
tempOrder = DefaultMercSpendOrder;
MercSpendOrderString = DefaultMercSpendOrderString;
}
else
{
for (char ch : MercSpendOrderString)
{
int val = ch - '0';
if (val >= 1 && val <= 5)
tempOrder.push_back(val);
}
}
// Rebuild from the new order
MercSpendOrder.clear();
// insert current merc type first if its available
if (bUseCurrentMercType)
{
int mercType = GetCurrentMercenaryType();
if (mercType != 0)
{
MercSpendOrder.push_back(mercType);
}
}
for (int value : tempOrder)
{
if (std::find(MercSpendOrder.begin(), MercSpendOrder.end(), value) == MercSpendOrder.end())
{
MercSpendOrder.push_back(value);
}
}
}
std::string GetMercSpendOrderDisplayString()
{
UpdateMercSpendOrder();
// this string is literally just for display purposes.
std::string spendOrderString;
if (bUseCurrentMercType)
{
int mercType = GetCurrentMercenaryType();
if (mercType != 0)
{
spendOrderString = fmt::format("\ay(active: {})\ax", MercCategories[mercType]);
}
}
for (char ch : MercSpendOrderString)
{
int index = ch - '0';
if (index >= 1 && index <= (int)MercCategories.size())
{
if (!spendOrderString.empty())
spendOrderString.append(", ");
spendOrderString.append(MercCategories[index]);
}
}
return spendOrderString;
}
#endif // HAS_MERCENARY_AA
void Update_INIFileName()
{
sprintf_s(INIFileName, "%s\\%s_%s.ini", gPathConfig, GetServerShortName(), pLocalPC->Name);
}
void SaveINI()
{
Update_INIFileName();
// write settings
std::string sectionName = "MQ2AASpend_Settings";
WritePrivateProfileSection(sectionName, "", INIFileName);
WritePrivateProfileBool(sectionName, "AutoSpend", bAutoSpend, INIFileName);
WritePrivateProfileBool(sectionName, "BruteForce", bBruteForce, INIFileName);
WritePrivateProfileBool(sectionName, "BruteForceBonusFirst", bBruteForceBonusFirst, INIFileName);
WritePrivateProfileInt(sectionName, "BankPoints", iBankPoints, INIFileName);
UpdateSpendOrder();
WritePrivateProfileString(sectionName, "SpendOrder", SpendOrderString, INIFileName);
#if HAS_MERCENARY_AA
WritePrivateProfileBool(sectionName, "MercAutoSpend", bAutoSpendMerc, INIFileName);
WritePrivateProfileBool(sectionName, "MercBruteForce", bBruteForceMerc, INIFileName);
UpdateMercSpendOrder();
WritePrivateProfileString(sectionName, "MercSpendOrder", MercSpendOrderString, INIFileName);
WritePrivateProfileBool(sectionName, "MercOrderAuto", bUseCurrentMercType, INIFileName);
#endif // HAS_MERCENARY_AA
// write all abilites
sectionName = "MQ2AASpend_AAList";
WritePrivateProfileSection(sectionName, "", INIFileName);
for (size_t a = 0; a < vAAList.size(); a++)
{
AAInfo& info = vAAList[a];
WritePrivateProfileString(sectionName, std::to_string(a),
fmt::format("{}|{}", info.name, info.level), INIFileName);
}
#if HAS_MERCENARY_AA
// write merc abilities
sectionName = "MQ2AASpend_MercAAList";
WritePrivateProfileSection(sectionName, "", INIFileName);
for (size_t a = 0; a < vMercAAList.size(); a++)
{
AAInfo& info = vMercAAList[a];
WritePrivateProfileString(sectionName, std::to_string(a),
fmt::format("{}|{}", info.name, info.level), INIFileName);
}
#endif // HAS_MERCENARY_AA
if (bInitDone) WriteChatf("\atMQ2AASpend :: Settings updated\ax");
}
static std::vector<AAInfo> LoadAAInfo(const std::string& section)
{
std::vector<std::string> AAList = GetPrivateProfileKeys(section, INIFileName);
// clear lists
std::vector<AAInfo> aaInfo;
int count = 0;
// loop through all entries in the list. values look like:
// 1=Gate|M
// 2=Innate Agility|5
for (const std::string& keyName : AAList)
{
std::string item = GetPrivateProfileString(section, keyName, std::string(), INIFileName);
// split entries on =
std::vector<std::string_view> pieces = mq::split_view(item, '|');
if (pieces.empty())
{
WriteChatf("\arMQ2AASpend :: %s=%s isn't in valid format, skipping...\ax", keyName.c_str(), item.c_str());
continue;
}
if (count++ > 500)
{
WriteChatf("\arMQ2AASpend :: There is a limit of 500 abilities in the ini file. Remove some maxxed entries if you want to add new abilities.\ax");
break;
}
for (size_t i = 0; i < pieces.size();)
{
// Odd entries are the names. Add it to the list
std::string_view name = pieces[i++];
// next is maxlevel of ability. Add it to the level list.
if (i < pieces.size())
{
std::string_view level = pieces[i++];
aaInfo.emplace_back(std::string(name), std::string(level));
}
}
}
return aaInfo;
}
void LoadINI()
{
Update_INIFileName();
// get settings
std::string sectionName = "MQ2AASpend_Settings";
bAutoSpend = GetPrivateProfileBool(sectionName, "AutoSpend", true, INIFileName);
bBruteForce = GetPrivateProfileBool(sectionName, "BruteForce", false, INIFileName);
bBruteForceBonusFirst = GetPrivateProfileBool(sectionName, "BruteForceBonusFirst", false, INIFileName);
iBankPoints = GetPrivateProfileInt(sectionName, "BankPoints", 0, INIFileName);
SpendOrderString = GetPrivateProfileString(sectionName, "SpendOrder", DefaultSpendOrderString, INIFileName);
UpdateSpendOrder();
vAAList = LoadAAInfo("MQ2AASpend_AAList");
#if HAS_MERCENARY_AA
bAutoSpendMerc = GetPrivateProfileBool(sectionName, "MercAutoSpend", false, INIFileName);
bBruteForceMerc = GetPrivateProfileBool(sectionName, "MercBruteForce", false, INIFileName);
bUseCurrentMercType = GetPrivateProfileBool(sectionName, "MercOrderAuto", false, INIFileName);
MercSpendOrderString = GetPrivateProfileString(sectionName, "MercSpendOrder", DefaultMercSpendOrderString, INIFileName);
UpdateMercSpendOrder();
vMercAAList = LoadAAInfo("MQ2AASpend_MercAAList");
#endif // HAS_MERCENARY_AA
// flag first load init as done
SaveINI();
bInitDone = true;
}
void SpendAAFromINI()
{
if (!pLocalPlayer) return;
if (!bAutoSpendNow && GetPcProfile()->AAPoints < iBankPoints) return;
if (vAAList.empty()) return;
bool bBuy = true;
int aaType = 0;
int aaCost = 0;
int curLevel = 0;
int maxLevel = 0;
int level = pLocalPlayer->Level;
for (const AAInfo& info : vAAList)
{
const std::string& vRef = info.name;
const std::string& vLevelRef = info.level;
if (bDebug)
WriteChatf("MQ2AASpend :: Have %s from ini, checking...", vRef.c_str());
bBuy = true;
int index = GetAAIndexByName((PCHAR)vRef.c_str());
if (CAltAbilityData* pAbility = GetAAById(index, level))
{
aaType = pAbility->Type;
if (!aaType)
{
if (bDebug)
WriteChatf("MQ2AASpend :: %s is \arNOT available\ax for purchase.", vRef.c_str());
continue;
}
aaCost = pAbility->Cost;
curLevel = pAbility->CurrentRank;
maxLevel = pAbility->MaxRank;
if (CAltAbilityData* pNextAbility = GetAAById(pAbility->NextGroupAbilityId, level))
pAbility = pNextAbility;
if (!pAltAdvManager->CanTrainAbility(pLocalPC, pAbility))
{
if (bDebug)
WriteChatf("MQ2AASpend :: %s is \arNOT available\ax for purchase.", vRef.c_str());
continue;
}
if (bDebug)
WriteChatf("MQ2AASpend :: %s is \agavailable\ax for purchase. Cost is %d, tab is %d", vRef.c_str(), aaCost, aaType);
if (bDebug)
WriteChatf("MQ2AASpend :: %s current level is %d", vRef.c_str(), curLevel);
if (curLevel == maxLevel)
{
if (bDebug)
WriteChatf("MQ2AASpend :: %s is currently maxxed, skipping", vRef.c_str());
continue;
}
else
{
if (!_stricmp(vLevelRef.c_str(), "M"))
{
bBuy = true;
}
else
{
int b = GetIntFromString(vLevelRef.c_str(), MAX_PC_LEVEL + 1);
if (curLevel >= b)
{
if (bDebug)
WriteChatf("MQ2AASpend :: %s max level has been reached per ini setting, skipping", vRef.c_str());
continue;
}
}
}
if (GetPcProfile()->AAPoints >= aaCost)
{
if (bBuy)
{
WriteChatf("MQ2AASpend :: Attempting to purchase level %d of %s for %d point%s.", curLevel + 1, vRef.c_str(), aaCost, aaCost > 1 ? "s" : "");
if (!bDebug)
{
char szCommand[MAX_BUYLINE] = { 0 };
#if !IS_EXPANSION_LEVEL(EXPANSION_LEVEL_COTF)
sprintf_s(szCommand, "/alt buy %d", pAbility->Index);
#else
sprintf_s(szCommand, "/alt buy %d", pAbility->GroupID);
#endif
DoCommand(pLocalPlayer, szCommand);
}
else
{
WriteChatf("MQ2AASpend :: Debugging so I wont actually buy %s", vRef.c_str());
}
break;
}
}
else
{
WriteChatf("MQ2AASpend :: Not enough points to buy %s, skipping", vRef.c_str());
}
}
}
}
CAltAbilityData* GetFirstPurchasableAA(bool bBonus, bool bNext)
{
if (!pLocalPlayer || !pLocalPC) return nullptr;
constexpr uint32_t ABILITY_MAP_SIZE = 5;
using AbilityMap = std::map<int, CAltAbilityData*>;
std::array<AbilityMap, ABILITY_MAP_SIZE> abilityMaps;
int level = pLocalPlayer->Level;
CAltAbilityData* pPrevAbility = nullptr;
for (int nAbility = 0; nAbility < NUM_ALT_ABILITIES; nAbility++)
{
if (CAltAbilityData* pAbility = GetAAById(nAbility, level))
{
pPrevAbility = pAbility;
CAltAbilityData* pNextAbility = GetAAById(pAbility->NextGroupAbilityId, level);
if (pNextAbility)
pAbility = pNextAbility;
if (bBonus)
{
if (pAbility->Expansion <= iAutoGrantExpansion)
continue;
}
// Check if we can train it.
if (pAltAdvManager->CanTrainAbility(pLocalPC, pAbility))
{
if (pAbility->GroupID == 0)
continue;// hidden communion of the cheetah is not valid and has this id, but is still sent down to the client
if (bDebug)
{
WriteChatf("Adding \ay%s\ax (expansion:\ag%d\ax) to the \ao%d\ax map",
pAbility->GetNameString(), pAbility->Expansion, pAbility->Type);
}
if (bNext)
pPrevAbility = pAbility;
int type = pAbility->Type - 1;
if (type >= 0 && type < ABILITY_MAP_SIZE)
{
abilityMaps[type][pAbility->GroupID] = pPrevAbility;
}
else
{
WriteChatf("Type %d Not Found in GetFirstPurchasableAA.", pAbility->Type);
}
}
}
}
// spend in the specified order:
for (int order : SpendOrder)
{
if (order >= 1 && order <= ABILITY_MAP_SIZE)
{
AbilityMap& abilityMap = abilityMaps[order - 1];
if (!abilityMap.empty())
{
return abilityMap.begin()->second;
}
}
}
if (!bBruteForce)
WriteChatf("I couldn't find any AA's to purchase, maybe you have them all?");
return nullptr;
}
bool BuySingleAA(CAltAbilityData* pAbility)
{
if (!pLocalPlayer) return false;
if (!pAbility) return false;
const char* AAName = pAbility->GetNameString();
if (pAbility->Type == 0)
{
WriteChatf("MQ2AASpend :: Unable to purchase %s", AAName);
return false;
}
if (CAltAbilityData* pNextAbility = GetAAById(pAbility->NextGroupAbilityId, pLocalPlayer->Level))
pAbility = pNextAbility;
if (!pAltAdvManager->CanTrainAbility(pLocalPC, pAbility))
{
WriteChatf("MQ2AASpend :: You can't train %s for some reason, aborting", AAName);
return false;
}
int aaCost = pAbility->Cost;
if (aaCost <= GetPcProfile()->AAPoints)
{
WriteChatf("MQ2AASpend :: Attempting to purchase level %d of %s for %d point%s.",
pAbility->CurrentRank, AAName, aaCost, aaCost > 1 ? "s" : "");
if (!bDebug)
{
char szCommand[MAX_BUYLINE] = { 0 };
#if !IS_EXPANSION_LEVEL(EXPANSION_LEVEL_COTF)
sprintf_s(szCommand, "/alt buy %d", pAbility->Index);
#else
sprintf_s(szCommand, "/alt buy %d", pAbility->GroupID);
#endif
DoCommand(pLocalPlayer, szCommand);
}
else
{
WriteChatf("MQ2AASpend :: Debugging so I wont actually buy %s", AAName);
}
return true;
}
else
{
WriteChatf("MQ2AASpend :: Not enough points to buy %s, aborting", AAName);
}
return false;
}
void BruteForcePurchase(int mode, bool bonusMode)
{
if (mode != 2 && GetPcProfile()->AAPoints < iBankPoints)
return;
DebugSpew("MQ2AASpend :: Starting Brute Force Purchase");
if (CAltAbilityData* pBruteAbility = GetFirstPurchasableAA(bonusMode, false))
{
BuySingleAA(pBruteAbility);
}
DebugSpew("MQ2AASpend :: Brute Force Purchase Complete");
}
//----------------------------------------------------------------------------
#if HAS_MERCENARY_AA
static int GetCurrentMercenaryType()
{
if (!pMercManager) return 0;
if (!pMercManager->currentMercenary.hasMercenary)
return 0;
return pMercManager->currentMercenary.mercenaryInfo.type;
}
static int MercTypeToInt(std::string_view str)
{
for (int i = 1; i < (int)MercCategories.size(); ++i)
{
if (ci_equals(MercCategories[i], str))
return i;
}
return 0;
}
struct SortByPriorityList
{
SortByPriorityList(std::vector<int>& priority_)
: priority(priority_) {}
int indexOfType(int type) const
{
for (int i = 0; i < (int)priority.size(); ++i)
{
if (priority[i] == type)
return i;
}
return 100;
}
bool operator()(const MercenaryAbilitiesData* a, const MercenaryAbilitiesData* b) const
{
int indexA = indexOfType(a->Type);
int indexB = indexOfType(b->Type);
if (indexA == indexB)
{
// Sort by name if they are the same type
return _strcmpi(a->GetNameString(), b->GetNameString()) < 0;
}
return indexA < indexB;
}
std::vector<int>& priority;
};
// Returns the Ability ID of the first purchasable Mercenary AA for the current configuration.
const MercenaryAbilitiesData* GetFirstPurchasableMercAA()
{
if (!pLocalPC)
return nullptr;
std::vector<const MercenaryAbilitiesData*> unownedAbilities;
int currentPoints = pLocalPC->MercAAPoints;
// Get list of purchasable AA
MercenaryAbilityGroup* group = pMercAltAbilities->MercenaryAbilityGroups.GetFirst();
while (group)
{
int groupId = pMercAltAbilities->MercenaryAbilityGroups.GetKey(group);
if (const MercenaryAbilitiesData* unownedAbility = pMercAltAbilities->GetFirstUnownedAbilityByGroupId(groupId))
{
// Filter our level requirements here.
if (GetPcProfile()->Level >= unownedAbility->MinPlayerLevel)
{
// If its free and trainable, grab it!
if (unownedAbility->Cost == 0)
{
if (pMercAltAbilities->CanTrainAbility(unownedAbility->AbilityID))
return unownedAbility;
}
unownedAbilities.push_back(unownedAbility);
}
}
group = pMercAltAbilities->MercenaryAbilityGroups.GetNext(group);
}
if (unownedAbilities.empty())
return nullptr;
UpdateMercSpendOrder();
// Sort the list of abilities by the type priority given by the ordering.
std::sort(unownedAbilities.begin(), unownedAbilities.end(), SortByPriorityList(MercSpendOrder));
if (bDebug)
{
WriteChatf("Priority: %s", MercSpendOrderString.c_str());
for (int value : MercSpendOrder)
WriteChatf(" %d", value);
for (auto ability : unownedAbilities)
{
WriteChatf("Merc Ability: \ag%s\ax - \ao%d\ax - \ay%s (%d)\ax", ability->GetNameString(), ability->Cost, ability->GetTypeString(), ability->Type);
}
}
// is the first item purchasable?
const MercenaryAbilitiesData* firstAbility = unownedAbilities[0];
if (pMercAltAbilities->CanTrainAbility(firstAbility->AbilityID))
return firstAbility;
if (bDebug)
{
WriteChatf("MQ2AASpend :: (Merc) Can't buy top priority ability (%s %d). Checking for requirements...",
firstAbility->GetNameString(), firstAbility->GroupRank);
}
// first item isn't trainable. If it has requirements that aren't met, then we should consider those instead.
if (!firstAbility->IsRequirementsMet())
{
for (const MercenaryAbilityReq& requirement : firstAbility->AbilityReqs)
{
if (!firstAbility->IsRequirementMet(requirement))
{
const MercenaryAbilitiesData* ability = pMercAltAbilities->GetFirstUnownedAbilityByGroupId(requirement.ReqGroupID);
if (ability && ability->GroupRank <= requirement.ReqGroupRank)
{
if (bDebug)
{
WriteChatf("MQ2AASpend :: (Merc) We are missing a requirement: %s (%d)",
ability->GetNameString(), ability->GroupRank);
}
if (pMercAltAbilities->CanTrainAbility(ability->AbilityID))
{
return ability;
}
else if (bDebug)
{
WriteChatf("MQ2AASpend :: (Merc) Can't train that ability! Keep looking...");
}
}
}
}
}
// didn't find something to buy.
return nullptr;
}
const MercenaryAbilitiesData* FindMercAbilityByName(std::string_view abilityName)
{
// Find AA By Name
for (auto& groupNode : pMercAltAbilities->MercenaryAbilityGroups)
{
const MercenaryAbilityGroup& groupInfo = groupNode.value();
if (const int* abilityId = groupInfo.GetFirst())
{
const MercenaryAbilitiesData* mercAbility = pMercAltAbilities->GetMercenaryAbility(*abilityId);
if (!mercAbility) continue;
// Check if this ability matches the name.
if (!ci_equals(mercAbility->GetNameString(), abilityName))
continue;
// Ability exists, lets return it.
return mercAbility;
}
}
return nullptr;
}
bool BuySingleMercAA(const MercenaryAbilitiesData* pAbility)
{
if (!pLocalPlayer) return false;
if (!pAbility) return false;
const char* AAName = pAbility->GetNameString();
if (!pMercAltAbilities->CanTrainAbility(pAbility->AbilityID))
{
WriteChatf("MQ2AASpend :: (Merc) You can't train %s for some reason, aborting", AAName);
return false;
}
int aaCost = pAbility->Cost;
if (aaCost <= pLocalPC->MercAAPoints)
{
WriteChatf("MQ2AASpend :: (Merc) Attempting to purchase level %d of %s for %d point%s.",
pAbility->GroupRank, AAName, aaCost, aaCost > 1 ? "s" : "");
if (!bDebug)
{
pMercAltAbilities->BuyAbility(pAbility->AbilityID);
}
else
{
WriteChatf("MQ2AASpend :: (Merc) Debugging so I wont actually buy %s", AAName);
}
return true;
}
else
{
WriteChatf("MQ2AASpend :: (Merc) Not enough points to buy %s, aborting", AAName);
}
return false;
}
void BruteForcePurchaseMercAA(int mode)
{
DebugSpew("MQ2AASpend :: Starting Brute Force Merc Purchase");
if (const MercenaryAbilitiesData* pBruteAbility = GetFirstPurchasableMercAA())
{
BuySingleMercAA(pBruteAbility);
}
DebugSpew("MQ2AASpend :: Brute Force Purchase Merc Complete");
}
void SpendMercAAFromINI()
{
if (!pLocalPlayer) return;
if (vMercAAList.empty()) return;
for (const AAInfo& info : vMercAAList)
{
if (bDebug)
WriteChatf("MQ2AASpend :: (Merc) Have %s from ini, checking...", info.name.c_str());
if (const MercenaryAbilitiesData* pAbility = FindMercAbilityByName(info.name))
{
pAbility = pMercAltAbilities->GetFirstUnownedAbilityByGroupId(pAbility->GroupID);
if (!pAbility)
{
if (bDebug)
WriteChatf("MQ2AASpend :: %s is \arNOT available\ax for purchase (none available).", info.name.c_str());
continue;
}
if (!pMercAltAbilities->CanTrainAbility(pAbility->AbilityID))
{
if (bDebug)
WriteChatf("MQ2AASpend :: %s is \arNOT available\ax for purchase (cannot train).", info.name.c_str());
continue;
}
int aaCost = pAbility->Cost;
int curLevel = pAbility->GroupRank - 1; // get previous rank since this is the one we don't own.
if (bDebug)
{
WriteChatf("MQ2AASpend :: %s is \agavailable\ax for purchase. Cost is %d, type is %s", info.name.c_str(), aaCost, pAbility->GetTypeString());
WriteChatf("MQ2AASpend :: %s current level is %d", info.name.c_str(), curLevel);
}
if (!ci_equals(info.level, "M"))
{
int wantedLevel = GetIntFromString(info.level.c_str(), 1000);
if (curLevel >= wantedLevel)
{
if (bDebug)
WriteChatf("MQ2AASpend :: %s max level has been reached per ini setting, skipping", info.name.c_str());
continue;
}
}
BuySingleMercAA(pAbility);
break;
}
}
}
#endif // HAS_MERCENARY_AA
//----------------------------------------------------------------------------
PLUGIN_API void SetGameState(int GameState)
{
if (GameState == GAMESTATE_INGAME)
{
if (!bInitDone) LoadINI();
}
else if (GameState != GAMESTATE_LOGGINGIN)
{
if (bInitDone) bInitDone = false;
}
}
static void ProcessAddCommand(const char* arg1, const char* arg2, const char* arg3, std::vector<AAInfo>& aaList)
{
if (!_strcmpi(arg2, "") || !_strcmpi(arg3, ""))
{
WriteChatf("Usage: /aaspend %s \"AA Name\" maxlevel", arg1);
WriteChatf(" maxlevel can be a number or m or M for max available.");
}
else
{
for (const AAInfo& info : aaList)
{
if (ci_equals(arg2, info.name))
{
WriteChatf("MQ2AASpend :: Ability %s already exists", info.name.c_str());
return;
}
}
aaList.emplace_back(arg2, arg3);
WriteChatf("MQ2AASpend :: Added %s levels of %s to ini", arg3, arg2);
SaveINI();
}
}
static void ProcessDeleteCommand(const char* arg1, const char* arg2, std::vector<AAInfo>& aaList)
{
if (!_strcmpi(arg2, ""))
{
WriteChatf("Usage: /aaspend %s \"AA Name\"", arg1);
}
else
{
auto iter = std::find_if(aaList.begin(), aaList.end(), [&](const AAInfo& info) { return ci_equals(arg2, info.name); });
if (iter != aaList.end())
{
aaList.erase(iter);
WriteChatf("MQ2AASpend :: Deleted ability %s from ini", arg2);
SaveINI();
}
else
{
WriteChatf("MQ2AASpend :: Abilty %s not found in ini", arg2);
}
}
}
void SpendCommand(PSPAWNINFO pChar, PCHAR szLine)
{
CHAR szArg1[MAX_STRING] = { 0 };
CHAR szArg2[MAX_STRING] = { 0 };
CHAR szArg3[MAX_STRING] = { 0 };
GetArg(szArg1, szLine, 1);
GetArg(szArg2, szLine, 2);
GetArg(szArg3, szLine, 3);
if (!_strcmpi(szArg1, ""))
{
ShowHelp();
return;
}
if (!_strcmpi(szArg1, "debug"))
{
if (!_strcmpi(szArg2, "on"))
{
bDebug = true;
}
else if (!_strcmpi(szArg2, "off"))
{
bDebug = false;
}
WriteChatf("MQ2AASpend :: Debug is %s", bDebug ? "\agON\ax" : "\arOFF\ax");
return;
}
if (!_strcmpi(szArg1, "status"))
{
ShowStatus();
return;
}
if (!_strcmpi(szArg1, "load"))
{
LoadINI();
return;
}
if (!_strcmpi(szArg1, "save"))