forked from ike3/mangosbot-bots
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRandomPlayerbotFactory.cpp
1082 lines (905 loc) · 36.1 KB
/
RandomPlayerbotFactory.cpp
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
#include "Config/Config.h"
#include "../botpch.h"
#include "playerbot.h"
#include "PlayerbotAIConfig.h"
#include "PlayerbotFactory.h"
#include "AccountMgr.h"
#include "ObjectMgr.h"
#include "DatabaseEnv.h"
#include "PlayerbotAI.h"
#include "Player.h"
#include "RandomPlayerbotFactory.h"
#include "SystemConfig.h"
#include "Social/SocialMgr.h"
#ifndef MANGOSBOT_ZERO
#ifdef CMANGOS
#include "Arena/ArenaTeam.h"
#endif
#ifdef MANGOS
#include "ArenaTeam.h"
#endif
#endif
map<uint8, vector<uint8> > RandomPlayerbotFactory::availableRaces;
RandomPlayerbotFactory::RandomPlayerbotFactory(uint32 accountId) : accountId(accountId)
{
availableRaces[CLASS_WARRIOR].push_back(RACE_HUMAN);
availableRaces[CLASS_WARRIOR].push_back(RACE_NIGHTELF);
availableRaces[CLASS_WARRIOR].push_back(RACE_GNOME);
availableRaces[CLASS_WARRIOR].push_back(RACE_DWARF);
availableRaces[CLASS_WARRIOR].push_back(RACE_ORC);
availableRaces[CLASS_WARRIOR].push_back(RACE_UNDEAD);
availableRaces[CLASS_WARRIOR].push_back(RACE_TAUREN);
availableRaces[CLASS_WARRIOR].push_back(RACE_TROLL);
#ifndef MANGOSBOT_ZERO
availableRaces[CLASS_WARRIOR].push_back(RACE_DRAENEI);
#endif
availableRaces[CLASS_PALADIN].push_back(RACE_HUMAN);
availableRaces[CLASS_PALADIN].push_back(RACE_DWARF);
#ifndef MANGOSBOT_ZERO
availableRaces[CLASS_PALADIN].push_back(RACE_DRAENEI);
availableRaces[CLASS_PALADIN].push_back(RACE_BLOODELF);
#endif
availableRaces[CLASS_ROGUE].push_back(RACE_HUMAN);
availableRaces[CLASS_ROGUE].push_back(RACE_DWARF);
availableRaces[CLASS_ROGUE].push_back(RACE_NIGHTELF);
availableRaces[CLASS_ROGUE].push_back(RACE_GNOME);
availableRaces[CLASS_ROGUE].push_back(RACE_ORC);
availableRaces[CLASS_ROGUE].push_back(RACE_TROLL);
#ifndef MANGOSBOT_ZERO
availableRaces[CLASS_ROGUE].push_back(RACE_BLOODELF);
#endif
availableRaces[CLASS_PRIEST].push_back(RACE_HUMAN);
availableRaces[CLASS_PRIEST].push_back(RACE_DWARF);
availableRaces[CLASS_PRIEST].push_back(RACE_NIGHTELF);
availableRaces[CLASS_PRIEST].push_back(RACE_TROLL);
availableRaces[CLASS_PRIEST].push_back(RACE_UNDEAD);
#ifndef MANGOSBOT_ZERO
availableRaces[CLASS_PRIEST].push_back(RACE_DRAENEI);
availableRaces[CLASS_PRIEST].push_back(RACE_BLOODELF);
#endif
availableRaces[CLASS_MAGE].push_back(RACE_HUMAN);
availableRaces[CLASS_MAGE].push_back(RACE_GNOME);
availableRaces[CLASS_MAGE].push_back(RACE_UNDEAD);
availableRaces[CLASS_MAGE].push_back(RACE_TROLL);
#ifndef MANGOSBOT_ZERO
availableRaces[CLASS_MAGE].push_back(RACE_DRAENEI);
availableRaces[CLASS_MAGE].push_back(RACE_BLOODELF);
#endif
availableRaces[CLASS_WARLOCK].push_back(RACE_HUMAN);
availableRaces[CLASS_WARLOCK].push_back(RACE_GNOME);
availableRaces[CLASS_WARLOCK].push_back(RACE_UNDEAD);
availableRaces[CLASS_WARLOCK].push_back(RACE_ORC);
#ifndef MANGOSBOT_ZERO
availableRaces[CLASS_WARLOCK].push_back(RACE_BLOODELF);
#endif
availableRaces[CLASS_SHAMAN].push_back(RACE_ORC);
availableRaces[CLASS_SHAMAN].push_back(RACE_TAUREN);
availableRaces[CLASS_SHAMAN].push_back(RACE_TROLL);
#ifndef MANGOSBOT_ZERO
availableRaces[CLASS_SHAMAN].push_back(RACE_DRAENEI);
#endif
availableRaces[CLASS_HUNTER].push_back(RACE_DWARF);
availableRaces[CLASS_HUNTER].push_back(RACE_NIGHTELF);
availableRaces[CLASS_HUNTER].push_back(RACE_ORC);
availableRaces[CLASS_HUNTER].push_back(RACE_TAUREN);
availableRaces[CLASS_HUNTER].push_back(RACE_TROLL);
#ifndef MANGOSBOT_ZERO
availableRaces[CLASS_HUNTER].push_back(RACE_DRAENEI);
availableRaces[CLASS_HUNTER].push_back(RACE_BLOODELF);
#endif
availableRaces[CLASS_DRUID].push_back(RACE_NIGHTELF);
availableRaces[CLASS_DRUID].push_back(RACE_TAUREN);
#ifdef MANGOSBOT_TWO
availableRaces[CLASS_DEATH_KNIGHT].push_back(RACE_NIGHTELF);
availableRaces[CLASS_DEATH_KNIGHT].push_back(RACE_TAUREN);
availableRaces[CLASS_DEATH_KNIGHT].push_back(RACE_HUMAN);
availableRaces[CLASS_DEATH_KNIGHT].push_back(RACE_ORC);
availableRaces[CLASS_DEATH_KNIGHT].push_back(RACE_UNDEAD);
availableRaces[CLASS_DEATH_KNIGHT].push_back(RACE_TROLL);
availableRaces[CLASS_DEATH_KNIGHT].push_back(RACE_BLOODELF);
availableRaces[CLASS_DEATH_KNIGHT].push_back(RACE_DRAENEI);
availableRaces[CLASS_DEATH_KNIGHT].push_back(RACE_GNOME);
availableRaces[CLASS_DEATH_KNIGHT].push_back(RACE_DWARF);
#endif
}
bool RandomPlayerbotFactory::isAvailableRace(uint8 cls, uint8 race)
{
if (race == RACE_GOBLIN)
return false;
#ifdef MANGOSBOT_TWO
else if (cls == 10)
#else
else if (cls == 10 || cls == 6)
#endif
return false;
return std::find(availableRaces[cls].begin(), availableRaces[cls].end(), race) != availableRaces[cls].end();
}
uint8 RandomPlayerbotFactory::GetRandomClass()
{
uint32 classProb[MAX_CLASSES] = { 0 };
for (uint32 race = 1; race < MAX_RACES; ++race)
{
for (uint32 cls = 1; cls < MAX_CLASSES; ++cls)
{
classProb[cls] += sPlayerbotAIConfig.classRaceProbability[cls][race];
}
}
uint32 randomProb = urand(0, sPlayerbotAIConfig.classRaceProbabilityTotal);
for (uint32 cls = 1; cls < MAX_CLASSES; ++cls)
{
if (classProb[cls] > 0 && randomProb < classProb[cls])
return cls;
randomProb -= classProb[cls];
}
for (uint32 cls = 1; cls < MAX_CLASSES; ++cls)
{
if (classProb[cls] > 0)
return cls;
}
return 1;
}
uint8 RandomPlayerbotFactory::GetRandomRace(uint8 cls)
{
uint32 totalClassProb = 0;
for (uint32 race = 1; race < MAX_RACES; ++race)
{
totalClassProb += sPlayerbotAIConfig.classRaceProbability[cls][race];
}
uint32 randomProb = urand(0, totalClassProb);
for (uint32 race = 1; race < MAX_RACES; ++race)
{
if (sPlayerbotAIConfig.classRaceProbability[cls][race] > 0 && randomProb < sPlayerbotAIConfig.classRaceProbability[cls][race])
return race;
randomProb -= sPlayerbotAIConfig.classRaceProbability[cls][race];
}
return availableRaces[cls].front();
}
bool RandomPlayerbotFactory::CreateRandomBot(uint8 cls, unordered_map<uint8, vector<string>>& names)
{
sLog.outDebug( "Creating new random bot for class %d", cls);
uint8 gender = rand() % 2 ? GENDER_MALE : GENDER_FEMALE;
uint8 race = GetRandomRace(cls);
string name;
if(names.empty())
name = CreateRandomBotName(gender);
else
{
if (names[gender].empty())
return false;
uint32 i = urand(0, names[gender].size() - 1);
name = names[gender][i];
swap(names[gender][i], names[gender].back());
names[gender].pop_back();
}
if (name.empty())
return false;
vector<uint8> skinColors, facialHairTypes;
vector<pair<uint8,uint8>> faces, hairs;
for (CharSectionsMap::const_iterator itr = sCharSectionMap.begin(); itr != sCharSectionMap.end(); ++itr)
{
CharSectionsEntry const* entry = itr->second;
if (entry->Race != race || entry->Gender != gender)
continue;
#ifndef MANGOSBOT_TWO
switch (entry->BaseSection)
{
case SECTION_TYPE_SKIN:
skinColors.push_back(entry->ColorIndex);
break;
case SECTION_TYPE_FACE:
faces.push_back(pair<uint8,uint8>(entry->VariationIndex, entry->ColorIndex));
break;
case SECTION_TYPE_FACIAL_HAIR:
facialHairTypes.push_back(entry->ColorIndex);
break;
case SECTION_TYPE_HAIR:
hairs.push_back(pair<uint8,uint8>(entry->VariationIndex, entry->ColorIndex));
break;
}
#else
switch (entry->BaseSection)
{
case SECTION_TYPE_SKIN:
skinColors.push_back(entry->Color);
break;
case SECTION_TYPE_FACE:
faces.push_back(pair<uint8, uint8>(entry->VariationIndex, entry->Color));
break;
case SECTION_TYPE_FACIAL_HAIR:
facialHairTypes.push_back(entry->Color);
break;
case SECTION_TYPE_HAIR:
hairs.push_back(pair<uint8, uint8>(entry->VariationIndex, entry->Color));
break;
}
#endif
}
uint8 skinColor = skinColors[urand(0, skinColors.size() - 1)];
pair<uint8,uint8> face = faces[urand(0, faces.size() - 1)];
pair<uint8,uint8> hair = hairs[urand(0, hairs.size() - 1)];
bool excludeCheck = (race == RACE_TAUREN) || (gender == GENDER_FEMALE && race != RACE_NIGHTELF && race != RACE_UNDEAD);
#ifndef MANGOSBOT_TWO
uint8 facialHair = excludeCheck ? 0 : facialHairTypes[urand(0, facialHairTypes.size() - 1)];
#else
uint8 facialHair = 0;
#endif
//TODO vector crash on cmangos TWO when creating one of the first bot characters, need a fix
WorldSession* session = new WorldSession(accountId, NULL, SEC_PLAYER,
#ifdef MANGOSBOT_TWO
2, 0, LOCALE_enUS, "", 0, 0, false);
#endif
#ifdef MANGOSBOT_ONE
2, 0, LOCALE_enUS, "", 0, 0, false);
#endif
#ifdef MANGOSBOT_ZERO
0, LOCALE_enUS, "", 0);
#endif
session->SetNoAnticheat();
Player* player = new Player(session);
if (!player || !session)
{
sLog.outError("BOTS: Unable to create session or player for random acc %d - name: \"%s\"; race: %u; class: %u", accountId, name.c_str(), race, cls);
return false;
}
if (!player->Create(sObjectMgr.GeneratePlayerLowGuid(), name, race, cls, gender,
face.second, // skinColor,
face.first,
hair.first,
hair.second, // hairColor,
facialHair, 0))
{
player->DeleteFromDB(player->GetObjectGuid(), accountId, true, true);
delete session;
delete player;
sLog.outError("Unable to create random bot for account %d - name: \"%s\"; race: %u; class: %u",
accountId, name.c_str(), race, cls);
return false;
}
player->setCinematic(2);
player->SetAtLoginFlag(AT_LOGIN_NONE);
//player->SetSemaphoreTeleportFar(true); //Fake teleport to delay sql save
//player->SaveToDB();
//player->SetSemaphoreTeleportFar(false);
sObjectAccessor.AddObject(player);
sLog.outDebug( "Random bot created for account %d - name: \"%s\"; race: %u; class: %u",
accountId, name.c_str(), race, cls);
return true;
}
string RandomPlayerbotFactory::CreateRandomBotName(uint8 gender)
{
QueryResult* result = CharacterDatabase.Query("SELECT MAX(name_id) FROM ai_playerbot_names");
if (!result)
{
sLog.outError("No more names left for random bots");
return "";
}
Field *fields = result->Fetch();
uint32 maxId = fields[0].GetUInt32();
delete result;
result = CharacterDatabase.PQuery("SELECT n.name FROM ai_playerbot_names n LEFT OUTER JOIN characters e ON e.name = n.name WHERE e.guid IS NULL and n.gender = '%u' order by rand() limit 1", gender);
if (!result)
{
sLog.outError("No more names left for random bots");
return "";
}
fields = result->Fetch();
string bname = fields[0].GetString();
delete result;
return bname;
}
inline string GetNamePostFix(int32 nr)
{
string ret;
string str("abcdefghijklmnopqrstuvwxyz");
while (nr >= 0)
{
int32 let = nr % 26;
ret = str[let] + ret;
nr /= 26;
nr--;
}
return ret;
}
void RandomPlayerbotFactory::CreateRandomBots()
{
// check if scheduled for delete
bool delAccs = false;
bool delFriends = false;
QueryResult* results = PlayerbotDatabase.Query(
"select value from ai_playerbot_random_bots where event = 'bot_delete'");
if (results)
{
delAccs = true;
Field* fields = results->Fetch();
uint32 deleteType = fields[0].GetUInt32();
if (deleteType > 1)
delFriends = true;
delete results;
}
if (sPlayerbotAIConfig.deleteRandomBotAccounts || delAccs)
{
std::list<uint32> botAccounts;
std::list<uint32> botFriends;
for (uint32 accountNumber = 0; accountNumber < sPlayerbotAIConfig.randomBotAccountCount; ++accountNumber)
{
ostringstream out; out << sPlayerbotAIConfig.randomBotAccountPrefix << accountNumber;
string accountName = out.str();
QueryResult* results = LoginDatabase.PQuery("SELECT id FROM account where username = '%s'", accountName.c_str());
if (!results)
continue;
Field* fields = results->Fetch();
uint32 accountId = fields[0].GetUInt32();
delete results;
botAccounts.push_back(accountId);
}
if (!delFriends)
sLog.outString("Deleting random bot characters without friends/guild...");
else
sLog.outString("Deleting all random bot characters...");
// load list of friends
if (!delFriends)
{
QueryResult* result = CharacterDatabase.PQuery("SELECT friend FROM character_social WHERE flags='%u'", SOCIAL_FLAG_FRIEND);
if (result)
{
do
{
Field* fields = result->Fetch();
uint32 guidlo = fields[0].GetUInt32();
botFriends.push_back(guidlo);
} while (result->NextRow());
delete result;
}
}
QueryResult* results = LoginDatabase.PQuery("SELECT id FROM account where username like '%s%%'", sPlayerbotAIConfig.randomBotAccountPrefix.c_str());
if (results)
{
BarGoLink bar(results->GetRowCount());
do
{
Field* fields = results->Fetch();
uint32 accId = fields[0].GetUInt32();
if (!delFriends)
{
// existing characters list
QueryResult* result = CharacterDatabase.PQuery("SELECT guid FROM characters WHERE account='%u'", accId);
if (result)
{
do
{
Field* fields = result->Fetch();
uint32 guidlo = fields[0].GetUInt32();
ObjectGuid guid = ObjectGuid(HIGHGUID_PLAYER, guidlo);
// if bot is someone's friend - don't delete it
if ((find(botFriends.begin(), botFriends.end(), guidlo) != botFriends.end()) && !delFriends)
continue;
// if bot is in someone's guild - don't delete it
uint32 guildId = Player::GetGuildIdFromDB(guid);
if (guildId && !delFriends)
{
Guild* guild = sGuildMgr.GetGuildById(guildId);
uint32 accountId = sObjectMgr.GetPlayerAccountIdByGUID(guild->GetLeaderGuid());
if (find(botAccounts.begin(), botAccounts.end(), accountId) == botAccounts.end())
continue;
}
Player::DeleteFromDB(guid, accId, false, true); // no need to update realm characters
//dels.push_back(std::async([guid, accId] {Player::DeleteFromDB(guid, accId, false, true); }));
} while (result->NextRow());
delete result;
}
bar.step();
}
else
{
bar.step();
sAccountMgr.DeleteAccount(accId);
}
} while (results->NextRow());
delete results;
}
PlayerbotDatabase.Execute("DELETE FROM ai_playerbot_random_bots");
sLog.outString("Random bot characters deleted");
}
int totalAccCount = sPlayerbotAIConfig.randomBotAccountCount;
sLog.outString("Creating random bot accounts...");
vector<std::future<void>> account_creations;
BarGoLink bar(totalAccCount);
for (uint32 accountNumber = 0; accountNumber < sPlayerbotAIConfig.randomBotAccountCount; ++accountNumber)
{
ostringstream out; out << sPlayerbotAIConfig.randomBotAccountPrefix << accountNumber;
string accountName = out.str();
QueryResult* results = LoginDatabase.PQuery("SELECT id FROM account where username = '%s'", accountName.c_str());
if (results)
{
delete results;
continue;
}
string password = "";
if (sPlayerbotAIConfig.randomBotRandomPassword)
{
for (int i = 0; i < 10; i++)
{
password += (char)urand('!', 'z');
}
}
else
password = accountName;
#ifndef MANGOSBOT_ZERO
uint8 max_expansion = MAX_EXPANSION;
account_creations.push_back(std::async([accountName, password, max_expansion] {sAccountMgr.CreateAccount(accountName, password, max_expansion); }));
#else
account_creations.push_back(std::async([accountName, password] {sAccountMgr.CreateAccount(accountName, password); }));
#endif
sLog.outDebug("Account %s created for random bots", accountName.c_str());
bar.step();
}
BarGoLink bar3(account_creations.size());
for (uint32 i = 0; i < account_creations.size(); i++)
{
bar3.step();
account_creations[i].wait();
}
//LoginDatabase.PExecute("UPDATE account SET expansion = '%u' where username like '%s%%'", 2, sPlayerbotAIConfig.randomBotAccountPrefix.c_str());
int totalRandomBotChars = 0;
int totalCharCount = sPlayerbotAIConfig.randomBotAccountCount
#ifdef MANGOSBOT_TWO
* 10;
#else
* 9;
#endif
sLog.outString("Loading available names...");
unordered_map<uint8,vector<string>> freeNames, allNames;
unordered_map<string, bool> used;
QueryResult* result = CharacterDatabase.PQuery("SELECT n.gender, n.name, e.guid FROM ai_playerbot_names n LEFT OUTER JOIN characters e ON e.name = n.name");
if (!result)
{
sLog.outError("No more names left for random bots");
return;
}
do
{
Field* fields = result->Fetch();
uint8 gender = fields[0].GetUInt8();
string bname = fields[1].GetString();
uint32 guidlo = fields[2].GetUInt32();
if(!guidlo)
freeNames[gender].push_back(bname);
allNames[gender].push_back(bname);
used[bname] = false;
} while (result->NextRow());
delete result;
for (uint8 gender = 0; gender < 2; gender++)
{
int32 postItt = 0;
vector<string> newNames;
if (totalCharCount < freeNames[gender].size())
continue;
uint32 namesNeeded = totalCharCount - freeNames[gender].size();
BarGoLink bar(namesNeeded);
while(namesNeeded)
{
string post = GetNamePostFix(postItt);
for (auto name : allNames[gender])
{
if (name.size() + post.size() > 12)
continue;
string newName = name + post;
if (used.find(newName) != used.end())
continue;
used[newName] = false;
newNames.push_back(newName);
namesNeeded--;
bar.step();
if (!namesNeeded)
break;
}
postItt++;
}
freeNames[gender].insert(freeNames[gender].end(), newNames.begin(), newNames.end());
}
sLog.outString("Creating random bot characters...");
BarGoLink bar1(totalCharCount);
for (uint32 accountNumber = 0; accountNumber < sPlayerbotAIConfig.randomBotAccountCount; ++accountNumber)
{
ostringstream out; out << sPlayerbotAIConfig.randomBotAccountPrefix << accountNumber;
string accountName = out.str();
QueryResult* results = LoginDatabase.PQuery("SELECT id FROM account where username = '%s'", accountName.c_str());
if (!results)
continue;
Field* fields = results->Fetch();
uint32 accountId = fields[0].GetUInt32();
delete results;
sPlayerbotAIConfig.randomBotAccounts.push_back(accountId);
int count = sAccountMgr.GetCharactersCount(accountId);
#ifdef MANGOSBOT_TWO
if (count >= 10)
#else
if (count >= 9)
#endif
{
totalRandomBotChars += count;
continue;
}
RandomPlayerbotFactory factory(accountId);
for (uint8 cls = CLASS_WARRIOR; cls < MAX_CLASSES - count; ++cls)
{
// skip nonexistent classes
if (!((1 << (cls - 1)) & CLASSMASK_ALL_PLAYABLE) || !sChrClassesStore.LookupEntry(cls))
continue;
#ifdef MANGOSBOT_TWO
if (cls != 10)
#else
if (cls != 10 && cls != 6)
#endif
{
uint8 rclss = factory.GetRandomClass();
factory.CreateRandomBot(rclss, freeNames);
bar1.step();
}
}
totalRandomBotChars += sAccountMgr.GetCharactersCount(accountId);
}
vector<std::future<void>> bot_creations;
BarGoLink bar2(sObjectAccessor.GetPlayers().size());
for (auto pl : sObjectAccessor.GetPlayers())
{
Player* player = pl.second;
account_creations.push_back(std::async([player] {player->SaveToDB(); }));
}
for (uint32 i = 0; i < sObjectAccessor.GetPlayers().size(); i++)
{
bar2.step();
account_creations[i].wait();
}
sLog.outString("%zu random bot accounts with %d characters available", sPlayerbotAIConfig.randomBotAccounts.size(), totalRandomBotChars);
}
void RandomPlayerbotFactory::CreateRandomGuilds()
{
vector<uint32> randomBots;
map<uint32, vector<uint32>> charAccGuids;
QueryResult* charAccounts = CharacterDatabase.PQuery(
"select `account`, `guid` from `characters`");
if (charAccounts)
{
do
{
Field* fields = charAccounts->Fetch();
uint32 accId = fields[0].GetUInt32();
uint32 guid = fields[1].GetUInt32();
charAccGuids[accId].push_back(guid);
} while (charAccounts->NextRow());
delete charAccounts;
}
if (charAccGuids.empty())
return;
for (auto charAcc : sPlayerbotAIConfig.randomBotAccounts)
{
if (!charAccGuids[charAcc].empty())
for (auto charGuid : charAccGuids[charAcc])
randomBots.push_back(charGuid);
}
if (randomBots.empty())
return;
if (sPlayerbotAIConfig.deleteRandomBotGuilds && !sRandomPlayerbotMgr.guildsDeleted)
{
sLog.outString("Deleting random bot guilds...");
uint32 counter = 0;
for (vector<uint32>::iterator i = randomBots.begin(); i != randomBots.end(); ++i)
{
ObjectGuid leader(HIGHGUID_PLAYER, *i);
Guild* guild = sGuildMgr.GetGuildByLeader(leader);
if (guild)
{
guild->Disband();
counter++;
}
}
sLog.outString("%d Random bot guilds deleted", counter);
sRandomPlayerbotMgr.guildsDeleted = true;
}
if (!sPlayerbotAIConfig.randomBotGuildCount)
return;
uint32 guildNumber = 0;
vector<ObjectGuid> availableLeaders;
for (vector<uint32>::iterator i = randomBots.begin(); i != randomBots.end(); ++i)
{
ObjectGuid leader(HIGHGUID_PLAYER, *i);
Guild* guild = sGuildMgr.GetGuildByLeader(leader);
if (guild)
{
if (find(sPlayerbotAIConfig.randomBotGuilds.begin(), sPlayerbotAIConfig.randomBotGuilds.end(), guild->GetId()) == sPlayerbotAIConfig.randomBotGuilds.end())
{
++guildNumber;
sPlayerbotAIConfig.randomBotGuilds.push_back(guild->GetId());
}
}
else
{
Player* player = sObjectMgr.GetPlayer(leader);
if (player && !player->GetGuildId() && player->GetLevel() >= 10)
availableLeaders.push_back(leader);
}
}
if (availableLeaders.empty())
{
sLog.outError("No leaders for random guilds available");
return;
}
uint32 attempts = 0;
uint32 maxNewGuilds = sPlayerbotAIConfig.randomBotGuildCount - sPlayerbotAIConfig.randomBotGuilds.size();
bool newGuilds = false;
for (; guildNumber < maxNewGuilds; ++guildNumber)
{
attempts++;
if (attempts > std::min(uint32(5), sPlayerbotAIConfig.randomBotGuildCount))
break;
if (sPlayerbotAIConfig.randomBotGuilds.size() >= sPlayerbotAIConfig.randomBotGuildCount)
break;
string guildName = CreateRandomGuildName();
if (guildName.empty())
continue;
int index = urand(0, availableLeaders.size() - 1);
ObjectGuid leader = availableLeaders[index];
Player* player = sObjectMgr.GetPlayer(leader);
if (!player || player->GetGuildId())
continue;
Guild* guild = new Guild();
if (!guild->Create(player, guildName))
{
sLog.outError("Error creating random guild %s", guildName.c_str());
continue;
}
sGuildMgr.AddGuild(guild);
// create random emblem
uint32 st, cl, br, bc, bg;
bg = urand(0, 51);
bc = urand(0, 17);
cl = urand(0, 17);
br = urand(0, 7);
st = urand(0, 180);
guild->SetEmblem(st, cl, br, bc, bg);
guild->SetGINFO(std::to_string(urand(10, 30)));
sPlayerbotAIConfig.randomBotGuilds.push_back(guild->GetId());
sLog.outBasic("Random Guild <%s>, GM: %s", guildName.c_str(), player->GetName());
newGuilds = true;
}
if (newGuilds)
sLog.outString("Total Random Guilds: %d", sPlayerbotAIConfig.randomBotGuilds.size());
}
string RandomPlayerbotFactory::CreateRandomGuildName()
{
QueryResult* result = CharacterDatabase.Query("SELECT MAX(name_id) FROM ai_playerbot_guild_names");
if (!result)
{
sLog.outError("No more names left for random guilds");
return "";
}
Field *fields = result->Fetch();
uint32 maxId = fields[0].GetUInt32();
delete result;
uint32 id = urand(0, maxId);
result = CharacterDatabase.PQuery("SELECT n.name FROM ai_playerbot_guild_names n "
"LEFT OUTER JOIN guild e ON e.name = n.name "
"WHERE e.guildid IS NULL AND n.name_id >= '%u' LIMIT 1", id);
if (!result)
{
sLog.outError("No more names left for random guilds");
return "";
}
fields = result->Fetch();
string gname = fields[0].GetString();
delete result;
return gname;
}
#ifndef MANGOSBOT_ZERO
void RandomPlayerbotFactory::CreateRandomArenaTeams()
{
vector<uint32> randomBots;
QueryResult* results = PlayerbotDatabase.PQuery(
"select `bot` from ai_playerbot_random_bots where event = 'add'");
if (results)
{
do
{
Field* fields = results->Fetch();
uint32 bot = fields[0].GetUInt32();
randomBots.push_back(bot);
} while (results->NextRow());
delete results;
}
if (sPlayerbotAIConfig.deleteRandomBotArenaTeams && !sRandomPlayerbotMgr.arenaTeamsDeleted)
{
sLog.outString("Deleting random bot arena teams...");
for (vector<uint32>::iterator i = randomBots.begin(); i != randomBots.end(); ++i)
{
ObjectGuid captain(HIGHGUID_PLAYER, *i);
ArenaTeam* arenateam = sObjectMgr.GetArenaTeamByCaptain(captain);
if (arenateam)
//sObjectMgr.RemoveArenaTeam(arenateam->GetId());
arenateam->Disband(NULL);
}
sLog.outString("Random bot arena teams deleted");
sRandomPlayerbotMgr.arenaTeamsDeleted = true;
}
uint32 arenaTeamNumber = 0;
std::map<uint32, uint32> teamsNumber;
std::map<uint32, uint32> maxTeamsNumber;
maxTeamsNumber[ARENA_TYPE_2v2] = (uint32)(sPlayerbotAIConfig.randomBotArenaTeamCount * 0.4f);
maxTeamsNumber[ARENA_TYPE_3v3] = (uint32)(sPlayerbotAIConfig.randomBotArenaTeamCount * 0.3f);
maxTeamsNumber[ARENA_TYPE_5v5] = (uint32)(sPlayerbotAIConfig.randomBotArenaTeamCount * 0.3f);
vector<ObjectGuid> availableCaptains;
for (vector<uint32>::iterator i = randomBots.begin(); i != randomBots.end(); ++i)
{
ObjectGuid captain(HIGHGUID_PLAYER, *i);
ArenaTeam* arenateam = sObjectMgr.GetArenaTeamByCaptain(captain);
if (arenateam)
{
teamsNumber[arenateam->GetType()]++;
sPlayerbotAIConfig.randomBotArenaTeams.push_back(arenateam->GetId());
}
Player* player = sObjectMgr.GetPlayer(captain);
if (player)
{
if (player->GetLevel() < 70)
continue;
uint8 slot = ArenaTeam::GetSlotByType(ArenaType(ARENA_TYPE_2v2));
if (player->GetArenaTeamId(slot))
continue;
slot = ArenaTeam::GetSlotByType(ArenaType(ARENA_TYPE_3v3));
if (player->GetArenaTeamId(slot))
continue;
slot = ArenaTeam::GetSlotByType(ArenaType(ARENA_TYPE_5v5));
if (player->GetArenaTeamId(slot))
continue;
availableCaptains.push_back(captain);
}
}
uint32 attempts = 0;
for (; arenaTeamNumber < sPlayerbotAIConfig.randomBotArenaTeamCount; ++arenaTeamNumber)
{
if (attempts > sPlayerbotAIConfig.randomBotArenaTeamCount)
break;
ArenaType randomType = ARENA_TYPE_2v2;
switch (urand(0, 2))
{
case 0:
randomType = ARENA_TYPE_2v2;
break;
case 1:
randomType = ARENA_TYPE_3v3;
break;
case 2:
randomType = ARENA_TYPE_5v5;
break;
}
string arenaTeamName = CreateRandomArenaTeamName();
if (arenaTeamName.empty())
continue;
if (availableCaptains.empty())
{
sLog.outError("No captains for random arena teams available");
continue;
}
int index = urand(0, availableCaptains.size() - 1);
ObjectGuid captain = availableCaptains[index];
Player* player = sObjectMgr.GetPlayer(captain);
if (!player)
{
sLog.outError("Cannot find player for captain %d", (uint64)captain);
continue;
}
if (player->GetLevel() < 70)
{
sLog.outError("Bot %d must be level 70 to create an arena team", (uint64)captain);
continue;
}
QueryResult* results = CharacterDatabase.PQuery("SELECT `type` FROM ai_playerbot_arena_team_names WHERE name = '%s'", arenaTeamName.c_str());
if (!results)
{
sLog.outError("No valid types for arena teams");
return;
}
Field *fields = results->Fetch();
uint8 slot = fields[0].GetUInt32();
delete results;
std::string arenaTypeName;
ArenaType type = ARENA_TYPE_2v2;
switch (slot)
{
case 2:
type = ARENA_TYPE_2v2;
arenaTypeName = "2v2";
break;
case 3:
type = ARENA_TYPE_3v3;
arenaTypeName = "3v3";
break;
case 5:
type = ARENA_TYPE_5v5;
arenaTypeName = "5v5";
break;
}
attempts++;
if (type != randomType)
continue;
if (teamsNumber[type] >= maxTeamsNumber[type])
continue;
if (player->GetArenaTeamId(ArenaTeam::GetSlotByType(type)))
continue;
ArenaTeam* arenateam = new ArenaTeam();
if (!arenateam->Create(player->GetObjectGuid(), type, arenaTeamName))
{
sLog.outError("Error creating arena team %s", arenaTeamName.c_str());
continue;
}
arenateam->SetCaptain(player->GetObjectGuid());
sLog.outBasic("Bot #%d %s:%d <%s>: captain of random Arena %s team - %s", player->GetGUIDLow(), player->GetTeam() == ALLIANCE ? "A" : "H", player->GetLevel(), player->GetName(), arenaTypeName.c_str(), arenateam->GetName().c_str());
// set random emblem
uint32 backgroundColor = urand(0xFF000000, 0xFFFFFFFF), emblemStyle = urand(0, 101), emblemColor = urand(0xFF000000, 0xFFFFFFFF), borderStyle = urand(0, 5), borderColor = urand(0xFF000000, 0xFFFFFFFF);