forked from opentibiabr/canary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparty.cpp
858 lines (713 loc) · 25.5 KB
/
party.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
/**
* Canary - A free and open-source MMORPG server emulator
* Copyright (©) 2019-2024 OpenTibiaBR <opentibiabr@outlook.com>
* Repository: https://github.com/opentibiabr/canary
* License: https://github.com/opentibiabr/canary/blob/main/LICENSE
* Contributors: https://github.com/opentibiabr/canary/graphs/contributors
* Website: https://docs.opentibiabr.com/
*/
#include <utility>
#include "pch.hpp"
#include "creatures/players/grouping/party.hpp"
#include "game/game.hpp"
#include "lua/creature/events.hpp"
#include "lua/callbacks/event_callback.hpp"
#include "lua/callbacks/events_callbacks.hpp"
std::shared_ptr<Party> Party::create(std::shared_ptr<Player> leader) {
auto party = std::make_shared<Party>();
party->m_leader = leader;
leader->setParty(party);
if (g_configManager().getBoolean(PARTY_AUTO_SHARE_EXPERIENCE, __FUNCTION__)) {
party->setSharedExperience(leader, true);
}
return party;
}
void Party::disband() {
if (!g_events().eventPartyOnDisband(getParty())) {
return;
}
if (!g_callbacks().checkCallback(EventCallback_t::partyOnDisband, &EventCallback::partyOnDisband, getParty())) {
return;
}
auto currentLeader = getLeader();
if (!currentLeader) {
return;
}
m_leader.reset();
currentLeader->setParty(nullptr);
currentLeader->sendClosePrivate(CHANNEL_PARTY);
g_game().updatePlayerShield(currentLeader);
g_game().updatePlayerHelpers(currentLeader);
currentLeader->sendCreatureSkull(currentLeader);
currentLeader->sendTextMessage(MESSAGE_PARTY_MANAGEMENT, "Your party has been disbanded.");
for (const auto &invitee : getInvitees()) {
invitee->removePartyInvitation(getParty());
currentLeader->sendCreatureShield(invitee);
}
inviteList.clear();
auto members = getMembers();
for (const auto &member : members) {
member->setParty(nullptr);
member->sendClosePrivate(CHANNEL_PARTY);
member->sendTextMessage(MESSAGE_PARTY_MANAGEMENT, "Your party has been disbanded.");
}
for (const auto &member : members) {
g_game().updatePlayerShield(member);
for (const auto &otherMember : members) {
otherMember->sendCreatureSkull(member);
}
member->sendCreatureSkull(currentLeader);
currentLeader->sendCreatureSkull(member);
g_game().updatePlayerHelpers(member);
}
memberList.clear();
membersData.clear();
}
bool Party::leaveParty(std::shared_ptr<Player> player) {
if (!player) {
return false;
}
auto leader = getLeader();
if (!leader) {
return false;
}
if (player->getParty().get() != this && leader != player) {
return false;
}
if (!g_events().eventPartyOnLeave(getParty(), player)) {
return false;
}
if (!g_callbacks().checkCallback(EventCallback_t::partyOnLeave, &EventCallback::partyOnLeave, getParty(), player)) {
return false;
}
bool missingLeader = false;
if (leader == player) {
if (!memberList.empty()) {
if (memberList.size() == 1 && inviteList.empty()) {
missingLeader = true;
} else {
auto newLeader = memberList.front();
while (!newLeader) {
memberList.erase(memberList.begin());
if (memberList.empty()) {
missingLeader = true;
break;
}
newLeader = memberList.front();
}
if (newLeader) {
passPartyLeadership(newLeader);
}
}
} else {
missingLeader = true;
}
}
// since we already passed the leadership, we remove the player from the list
auto it = std::find(memberList.begin(), memberList.end(), player);
if (it != memberList.end()) {
memberList.erase(it);
}
player->setParty(nullptr);
player->sendClosePrivate(CHANNEL_PARTY);
g_game().updatePlayerShield(player);
g_game().updatePlayerHelpers(player);
for (const auto &member : getMembers()) {
member->sendCreatureSkull(player);
player->sendPlayerPartyIcons(member);
member->sendPartyCreatureUpdate(player);
g_game().updatePlayerHelpers(member);
}
player->sendTextMessage(MESSAGE_PARTY_MANAGEMENT, "You have left the party.");
updateSharedExperience();
clearPlayerPoints(player);
std::ostringstream ss;
ss << player->getName() << " has left the party.";
broadcastPartyMessage(MESSAGE_PARTY_MANAGEMENT, ss.str());
if (missingLeader || empty()) {
disband();
}
player->sendCreatureSkull(player);
leader->sendCreatureSkull(player);
player->sendPlayerPartyIcons(leader);
leader->sendPartyCreatureUpdate(player);
return true;
}
bool Party::passPartyLeadership(std::shared_ptr<Player> player) {
auto leader = getLeader();
if (!leader || !player || leader == player || player->getParty().get() != this) {
return false;
}
// Remove it before to broadcast the message correctly
auto it = std::find(memberList.begin(), memberList.end(), player);
if (it != memberList.end()) {
memberList.erase(it);
}
std::ostringstream ss;
ss << player->getName() << " is now the leader of the party.";
broadcastPartyMessage(MESSAGE_PARTY_MANAGEMENT, ss.str(), true);
auto oldLeader = leader;
m_leader = player;
memberList.insert(memberList.begin(), oldLeader);
updateSharedExperience();
updateTrackerAnalyzer();
for (const auto &member : getMembers()) {
member->sendPartyCreatureShield(oldLeader);
member->sendPartyCreatureShield(player);
}
for (const auto &invitee : getInvitees()) {
invitee->sendCreatureShield(oldLeader);
invitee->sendCreatureShield(player);
}
player->sendPartyCreatureShield(oldLeader);
player->sendPartyCreatureShield(player);
player->sendTextMessage(MESSAGE_PARTY_MANAGEMENT, "You are now the leader of the party.");
return true;
}
bool Party::joinParty(const std::shared_ptr<Player> &player) {
auto leader = getLeader();
if (!leader) {
return false;
}
if (!g_events().eventPartyOnJoin(getParty(), player)) {
return false;
}
if (!g_callbacks().checkCallback(EventCallback_t::partyOnJoin, &EventCallback::partyOnJoin, getParty(), player)) {
return false;
}
auto it = std::find(inviteList.begin(), inviteList.end(), player);
if (it == inviteList.end()) {
return false;
}
inviteList.erase(it);
std::ostringstream ss;
ss << player->getName() << " has joined the party.";
broadcastPartyMessage(MESSAGE_PARTY_MANAGEMENT, ss.str());
player->setParty(getParty());
g_game().updatePlayerShield(player);
for (const auto &member : getMembers()) {
member->sendCreatureSkull(player);
member->sendPlayerPartyIcons(player);
player->sendPlayerPartyIcons(member);
}
leader->sendCreatureSkull(player);
player->sendCreatureSkull(player);
leader->sendPlayerPartyIcons(player);
player->sendPlayerPartyIcons(leader);
memberList.push_back(player);
g_game().updatePlayerHelpers(player);
updatePlayerStatus(player);
player->removePartyInvitation(getParty());
updateSharedExperience();
const std::string &leaderName = leader->getName();
ss.str(std::string());
ss << "You have joined " << leaderName << "'" << (leaderName.back() == 's' ? "" : "s") << " party. Open the party channel to communicate with your companions.";
player->sendTextMessage(MESSAGE_PARTY_MANAGEMENT, ss.str());
updateTrackerAnalyzer();
return true;
}
bool Party::removeInvite(const std::shared_ptr<Player> &player, bool removeFromPlayer /* = true*/) {
auto leader = getLeader();
if (!leader) {
return false;
}
auto it = std::find(inviteList.begin(), inviteList.end(), player);
if (it == inviteList.end()) {
return false;
}
inviteList.erase(it);
leader->sendCreatureShield(player);
player->sendCreatureShield(leader);
if (removeFromPlayer) {
player->removePartyInvitation(getParty());
}
if (empty()) {
disband();
} else {
for (const auto &member : getMembers()) {
g_game().updatePlayerHelpers(member);
}
g_game().updatePlayerHelpers(leader);
}
return true;
}
void Party::revokeInvitation(const std::shared_ptr<Player> &player) {
auto leader = getLeader();
if (!leader) {
return;
}
std::ostringstream ss;
ss << leader->getName() << " has revoked " << leader->getPossessivePronoun() << " invitation.";
player->sendTextMessage(MESSAGE_PARTY_MANAGEMENT, ss.str());
ss.str(std::string());
ss << "Invitation for " << player->getName() << " has been revoked.";
leader->sendTextMessage(MESSAGE_PARTY_MANAGEMENT, ss.str());
removeInvite(player);
}
bool Party::invitePlayer(const std::shared_ptr<Player> &player) {
auto leader = getLeader();
if (!leader) {
return false;
}
if (isPlayerInvited(player)) {
return false;
}
std::ostringstream ss;
ss << player->getName() << " has been invited to join the party (Share range: " << getMinLevel() << "-" << getMaxLevel() << ").";
if (empty()) {
ss << " Open the party channel to communicate with your members.";
g_game().updatePlayerShield(leader);
leader->sendCreatureSkull(leader);
}
leader->sendTextMessage(MESSAGE_PARTY_MANAGEMENT, ss.str());
inviteList.push_back(player);
for (const auto &member : getMembers()) {
g_game().updatePlayerHelpers(member);
}
g_game().updatePlayerHelpers(leader);
leader->sendCreatureShield(player);
player->sendCreatureShield(leader);
player->addPartyInvitation(getParty());
ss.str(std::string());
ss << leader->getName() << " has invited you to " << leader->getPossessivePronoun() << " party (Share range: " << getMinLevel() << "-" << getMaxLevel() << ").";
player->sendTextMessage(MESSAGE_PARTY_MANAGEMENT, ss.str());
return true;
}
bool Party::isPlayerInvited(const std::shared_ptr<Player> &player) const {
return std::find(inviteList.begin(), inviteList.end(), player) != inviteList.end();
}
void Party::updateAllPartyIcons() {
auto leader = getLeader();
if (!leader) {
return;
}
auto members = getMembers();
for (const auto &member : members) {
for (const auto &otherMember : members) {
member->sendPartyCreatureShield(otherMember);
}
member->sendPartyCreatureShield(leader);
leader->sendPartyCreatureShield(member);
}
leader->sendPartyCreatureShield(leader);
updateTrackerAnalyzer();
}
void Party::broadcastPartyMessage(MessageClasses msgClass, const std::string &msg, bool sendToInvitations /*= false*/) {
auto leader = getLeader();
if (!leader) {
return;
}
for (const auto &member : getMembers()) {
member->sendTextMessage(msgClass, msg);
}
leader->sendTextMessage(msgClass, msg);
if (sendToInvitations) {
for (const auto &invitee : getInvitees()) {
invitee->sendTextMessage(msgClass, msg);
}
}
}
void Party::updateSharedExperience() {
if (sharedExpActive) {
bool result = getSharedExperienceStatus() == SHAREDEXP_OK;
if (result != sharedExpEnabled) {
sharedExpEnabled = result;
updateAllPartyIcons();
}
}
}
const char* Party::getSharedExpReturnMessage(SharedExpStatus_t value) {
switch (value) {
case SHAREDEXP_OK:
return "Shared Experience is now active.";
case SHAREDEXP_TOOFARAWAY:
return "Shared Experience has been activated, but some members of your party are too far away.";
case SHAREDEXP_LEVELDIFFTOOLARGE:
return "Shared Experience has been activated, but the level spread of your party is too wide.";
case SHAREDEXP_MEMBERINACTIVE:
return "Shared Experience has been activated, but some members of your party are inactive.";
case SHAREDEXP_EMPTYPARTY:
return "Shared Experience has been activated, but you are alone in your party.";
default:
return "An error occured. Unable to activate shared experience.";
}
}
bool Party::setSharedExperience(std::shared_ptr<Player> player, bool newSharedExpActive, bool silent /*= false*/) {
auto leader = getLeader();
if (!player || leader != player) {
return false;
}
if (this->sharedExpActive == newSharedExpActive) {
return true;
}
this->sharedExpActive = newSharedExpActive;
if (newSharedExpActive) {
SharedExpStatus_t sharedExpStatus = getSharedExperienceStatus();
this->sharedExpEnabled = sharedExpStatus == SHAREDEXP_OK;
if (!silent) {
leader->sendTextMessage(MESSAGE_PARTY_MANAGEMENT, getSharedExpReturnMessage(sharedExpStatus));
}
} else {
if (!silent) {
leader->sendTextMessage(MESSAGE_PARTY_MANAGEMENT, "Shared Experience has been deactivated.");
}
}
updateAllPartyIcons();
return true;
}
void Party::shareExperience(uint64_t experience, std::shared_ptr<Creature> target /* = nullptr*/) {
auto leader = getLeader();
if (!leader) {
return;
}
uint64_t shareExperience = experience;
g_events().eventPartyOnShareExperience(getParty(), shareExperience);
g_callbacks().executeCallback(EventCallback_t::partyOnShareExperience, &EventCallback::partyOnShareExperience, getParty(), shareExperience);
for (const auto &member : getMembers()) {
member->onGainSharedExperience(shareExperience, target);
}
leader->onGainSharedExperience(shareExperience, target);
}
bool Party::canUseSharedExperience(std::shared_ptr<Player> player) {
return getMemberSharedExperienceStatus(std::move(player)) == SHAREDEXP_OK;
}
SharedExpStatus_t Party::getMemberSharedExperienceStatus(std::shared_ptr<Player> player) {
auto leader = getLeader();
if (!leader) {
return SHAREDEXP_EMPTYPARTY;
}
if (memberList.empty()) {
return SHAREDEXP_EMPTYPARTY;
}
uint32_t minLevel = getMinLevel();
if (player->getLevel() < minLevel) {
return SHAREDEXP_LEVELDIFFTOOLARGE;
}
if (!Position::areInRange<30, 30, 1>(leader->getPosition(), player->getPosition())) {
return SHAREDEXP_TOOFARAWAY;
}
if (!player->hasFlag(PlayerFlags_t::NotGainInFight)) {
if (!isPlayerActive(player)) {
return SHAREDEXP_MEMBERINACTIVE;
}
}
return SHAREDEXP_OK;
}
float Party::shareRangeMultiplier() const {
return g_configManager().getFloat(PARTY_SHARE_RANGE_MULTIPLIER, __FUNCTION__);
}
uint32_t Party::getHighestLevel() {
auto leader = getLeader();
if (!leader) {
return 0;
}
uint32_t highestLevel = leader->getLevel();
for (const auto &member : getMembers()) {
if (member->getLevel() > highestLevel) {
highestLevel = member->getLevel();
}
}
return highestLevel;
}
uint32_t Party::getMinLevel() {
return static_cast<uint32_t>(std::ceil(static_cast<float>(getHighestLevel()) / shareRangeMultiplier()));
}
uint32_t Party::getLowestLevel() {
auto leader = getLeader();
if (!leader) {
return 0;
}
uint32_t lowestLevel = leader->getLevel();
for (const auto &member : getMembers()) {
if (member->getLevel() < lowestLevel) {
lowestLevel = member->getLevel();
}
}
return lowestLevel;
}
uint32_t Party::getMaxLevel() {
return static_cast<uint32_t>(std::floor(static_cast<float>(getLowestLevel()) * shareRangeMultiplier()));
}
bool Party::isPlayerActive(std::shared_ptr<Player> player) {
auto it = ticksMap.find(player->getID());
if (it == ticksMap.end()) {
return false;
}
uint64_t timeDiff = OTSYS_TIME() - it->second;
return timeDiff <= 2 * 60 * 1000;
}
SharedExpStatus_t Party::getSharedExperienceStatus() {
auto leader = getLeader();
if (!leader) {
return SHAREDEXP_EMPTYPARTY;
}
SharedExpStatus_t leaderStatus = getMemberSharedExperienceStatus(leader);
if (leaderStatus != SHAREDEXP_OK) {
return leaderStatus;
}
for (const auto &member : getMembers()) {
SharedExpStatus_t memberStatus = getMemberSharedExperienceStatus(member);
if (memberStatus != SHAREDEXP_OK) {
return memberStatus;
}
}
return SHAREDEXP_OK;
}
void Party::updatePlayerTicks(std::shared_ptr<Player> player, uint32_t points) {
if (points != 0 && !player->hasFlag(PlayerFlags_t::NotGainInFight)) {
ticksMap[player->getID()] = OTSYS_TIME();
updateSharedExperience();
}
}
void Party::clearPlayerPoints(std::shared_ptr<Player> player) {
auto it = ticksMap.find(player->getID());
if (it != ticksMap.end()) {
ticksMap.erase(it);
updateSharedExperience();
}
}
bool Party::canOpenCorpse(uint32_t ownerId) const {
auto leader = getLeader();
if (!leader) {
return false;
}
if (std::shared_ptr<Player> player = g_game().getPlayerByID(ownerId)) {
return leader->getID() == ownerId || player->getParty().get() == this;
}
return false;
}
void Party::showPlayerStatus(std::shared_ptr<Player> player, std::shared_ptr<Player> member, bool showStatus) {
player->sendPartyCreatureShowStatus(member, showStatus);
member->sendPartyCreatureShowStatus(player, showStatus);
if (showStatus) {
for (const auto &summon : member->getSummons()) {
player->sendPartyCreatureShowStatus(summon, showStatus);
player->sendPartyCreatureHealth(summon, std::ceil((static_cast<double>(summon->getHealth()) / std::max<int32_t>(summon->getMaxHealth(), 1)) * 100));
}
for (const auto &summon : player->getSummons()) {
member->sendPartyCreatureShowStatus(summon, showStatus);
member->sendPartyCreatureHealth(summon, std::ceil((static_cast<double>(summon->getHealth()) / std::max<int32_t>(summon->getMaxHealth(), 1)) * 100));
}
player->sendPartyCreatureHealth(member, std::ceil((static_cast<double>(member->getHealth()) / std::max<int32_t>(member->getMaxHealth(), 1)) * 100));
member->sendPartyCreatureHealth(player, std::ceil((static_cast<double>(player->getHealth()) / std::max<int32_t>(player->getMaxHealth(), 1)) * 100));
player->sendPartyPlayerMana(member, std::ceil((static_cast<double>(member->getMana()) / std::max<int32_t>(member->getMaxMana(), 1)) * 100));
member->sendPartyPlayerMana(player, std::ceil((static_cast<double>(player->getMana()) / std::max<int32_t>(player->getMaxMana(), 1)) * 100));
} else {
for (const auto &summon : player->getSummons()) {
member->sendPartyCreatureShowStatus(summon, showStatus);
}
for (const auto &summon : member->getSummons()) {
player->sendPartyCreatureShowStatus(summon, showStatus);
}
}
}
void Party::updatePlayerStatus(std::shared_ptr<Player> player) {
auto leader = getLeader();
if (!leader) {
return;
}
int32_t maxDistance = g_configManager().getNumber(PARTY_LIST_MAX_DISTANCE, __FUNCTION__);
for (const auto &member : getMembers()) {
bool condition = (maxDistance == 0 || (Position::getDistanceX(player->getPosition(), member->getPosition()) <= maxDistance && Position::getDistanceY(player->getPosition(), member->getPosition()) <= maxDistance));
if (condition) {
showPlayerStatus(player, member, true);
} else {
showPlayerStatus(player, member, false);
}
}
bool condition = (maxDistance == 0 || (Position::getDistanceX(player->getPosition(), leader->getPosition()) <= maxDistance && Position::getDistanceY(player->getPosition(), leader->getPosition()) <= maxDistance));
if (condition) {
showPlayerStatus(player, leader, true);
} else {
showPlayerStatus(player, leader, false);
}
}
void Party::updatePlayerStatus(std::shared_ptr<Player> player, const Position &oldPos, const Position &newPos) {
auto leader = getLeader();
if (!leader) {
return;
}
int32_t maxDistance = g_configManager().getNumber(PARTY_LIST_MAX_DISTANCE, __FUNCTION__);
if (maxDistance != 0) {
for (const auto &member : getMembers()) {
bool condition1 = (Position::getDistanceX(oldPos, member->getPosition()) <= maxDistance && Position::getDistanceY(oldPos, member->getPosition()) <= maxDistance);
bool condition2 = (Position::getDistanceX(newPos, member->getPosition()) <= maxDistance && Position::getDistanceY(newPos, member->getPosition()) <= maxDistance);
if (condition1 && !condition2) {
showPlayerStatus(player, member, false);
} else if (!condition1 && condition2) {
showPlayerStatus(player, member, true);
}
}
bool condition1 = (Position::getDistanceX(oldPos, leader->getPosition()) <= maxDistance && Position::getDistanceY(oldPos, leader->getPosition()) <= maxDistance);
bool condition2 = (Position::getDistanceX(newPos, leader->getPosition()) <= maxDistance && Position::getDistanceY(newPos, leader->getPosition()) <= maxDistance);
if (condition1 && !condition2) {
showPlayerStatus(player, leader, false);
} else if (!condition1 && condition2) {
showPlayerStatus(player, leader, true);
}
}
}
void Party::updatePlayerHealth(std::shared_ptr<Player> player, std::shared_ptr<Creature> target, uint8_t healthPercent) {
auto leader = getLeader();
if (!leader) {
return;
}
int32_t maxDistance = g_configManager().getNumber(PARTY_LIST_MAX_DISTANCE, __FUNCTION__);
auto playerPosition = player->getPosition();
auto leaderPosition = leader->getPosition();
for (const auto &member : getMembers()) {
auto memberPosition = member->getPosition();
bool condition = (maxDistance == 0 || (Position::getDistanceX(playerPosition, memberPosition) <= maxDistance && Position::getDistanceY(playerPosition, memberPosition) <= maxDistance));
if (condition) {
member->sendPartyCreatureHealth(target, healthPercent);
}
}
bool condition = (maxDistance == 0 || (Position::getDistanceX(playerPosition, leaderPosition) <= maxDistance && Position::getDistanceY(playerPosition, leaderPosition) <= maxDistance));
if (condition) {
leader->sendPartyCreatureHealth(target, healthPercent);
}
}
void Party::updatePlayerMana(std::shared_ptr<Player> player, uint8_t manaPercent) {
auto leader = getLeader();
if (!leader) {
return;
}
int32_t maxDistance = g_configManager().getNumber(PARTY_LIST_MAX_DISTANCE, __FUNCTION__);
for (const auto &member : getMembers()) {
bool condition = (maxDistance == 0 || (Position::getDistanceX(player->getPosition(), member->getPosition()) <= maxDistance && Position::getDistanceY(player->getPosition(), member->getPosition()) <= maxDistance));
if (condition) {
member->sendPartyPlayerMana(player, manaPercent);
}
}
bool condition = (maxDistance == 0 || (Position::getDistanceX(player->getPosition(), leader->getPosition()) <= maxDistance && Position::getDistanceY(player->getPosition(), leader->getPosition()) <= maxDistance));
if (condition) {
leader->sendPartyPlayerMana(player, manaPercent);
}
}
void Party::updatePlayerVocation(std::shared_ptr<Player> player) {
auto leader = getLeader();
if (!leader) {
return;
}
int32_t maxDistance = g_configManager().getNumber(PARTY_LIST_MAX_DISTANCE, __FUNCTION__);
for (const auto &member : getMembers()) {
bool condition = (maxDistance == 0 || (Position::getDistanceX(player->getPosition(), member->getPosition()) <= maxDistance && Position::getDistanceY(player->getPosition(), member->getPosition()) <= maxDistance));
if (condition) {
member->sendPartyPlayerVocation(player);
}
}
bool condition = (maxDistance == 0 || (Position::getDistanceX(player->getPosition(), leader->getPosition()) <= maxDistance && Position::getDistanceY(player->getPosition(), leader->getPosition()) <= maxDistance));
if (condition) {
leader->sendPartyPlayerVocation(player);
}
}
void Party::updateTrackerAnalyzer() {
auto leader = getLeader();
if (!leader) {
return;
}
for (const auto &member : getMembers()) {
member->updatePartyTrackerAnalyzer();
}
leader->updatePartyTrackerAnalyzer();
}
void Party::addPlayerLoot(std::shared_ptr<Player> player, std::shared_ptr<Item> item) {
auto leader = getLeader();
if (!leader) {
return;
}
auto playerAnalyzer = getPlayerPartyAnalyzerStruct(player->getID());
if (!playerAnalyzer) {
playerAnalyzer = std::make_shared<PartyAnalyzer>(player->getID(), player->getName());
membersData.push_back(playerAnalyzer);
}
uint32_t count = std::max<uint32_t>(1, item->getItemCount());
if (auto it = playerAnalyzer->lootMap.find(item->getID()); it != playerAnalyzer->lootMap.end()) {
(*it).second += count;
} else {
playerAnalyzer->lootMap.insert({ item->getID(), count });
}
if (priceType == LEADER_PRICE) {
playerAnalyzer->lootPrice += leader->getItemCustomPrice(item->getID()) * count;
} else {
std::map<uint16_t, uint64_t> itemMap { { item->getID(), count } };
playerAnalyzer->lootPrice += g_game().getItemMarketPrice(itemMap, false);
}
updateTrackerAnalyzer();
}
void Party::addPlayerSupply(std::shared_ptr<Player> player, std::shared_ptr<Item> item) {
auto leader = getLeader();
if (!leader) {
return;
}
std::shared_ptr<PartyAnalyzer> playerAnalyzer = getPlayerPartyAnalyzerStruct(player->getID());
if (!playerAnalyzer) {
playerAnalyzer = std::make_shared<PartyAnalyzer>(player->getID(), player->getName());
membersData.push_back(playerAnalyzer);
}
if (auto it = playerAnalyzer->supplyMap.find(item->getID()); it != playerAnalyzer->supplyMap.end()) {
(*it).second += 1;
} else {
playerAnalyzer->supplyMap.insert({ item->getID(), 1 });
}
if (priceType == LEADER_PRICE) {
playerAnalyzer->supplyPrice += leader->getItemCustomPrice(item->getID(), true);
} else {
std::map<uint16_t, uint64_t> itemMap { { item->getID(), 1 } };
playerAnalyzer->supplyPrice += g_game().getItemMarketPrice(itemMap, true);
}
updateTrackerAnalyzer();
}
void Party::addPlayerDamage(std::shared_ptr<Player> player, uint64_t amount) {
auto playerAnalyzer = getPlayerPartyAnalyzerStruct(player->getID());
if (!playerAnalyzer) {
playerAnalyzer = std::make_shared<PartyAnalyzer>(player->getID(), player->getName());
membersData.push_back(playerAnalyzer);
}
playerAnalyzer->damage += amount;
updateTrackerAnalyzer();
}
void Party::addPlayerHealing(std::shared_ptr<Player> player, uint64_t amount) {
auto playerAnalyzer = getPlayerPartyAnalyzerStruct(player->getID());
if (!playerAnalyzer) {
playerAnalyzer = std::make_shared<PartyAnalyzer>(player->getID(), player->getName());
membersData.push_back(playerAnalyzer);
}
playerAnalyzer->healing += amount;
updateTrackerAnalyzer();
}
void Party::switchAnalyzerPriceType() {
auto leader = getLeader();
if (!leader) {
return;
}
priceType = priceType == LEADER_PRICE ? MARKET_PRICE : LEADER_PRICE;
reloadPrices();
updateTrackerAnalyzer();
}
void Party::resetAnalyzer() {
trackerTime = time(nullptr);
membersData.clear();
updateTrackerAnalyzer();
}
void Party::reloadPrices() {
auto leader = getLeader();
if (!leader) {
return;
}
for (const auto &analyzer : membersData) {
if (priceType == MARKET_PRICE) {
analyzer->lootPrice = g_game().getItemMarketPrice(analyzer->lootMap, false);
analyzer->supplyPrice = g_game().getItemMarketPrice(analyzer->supplyMap, true);
continue;
}
analyzer->lootPrice = 0;
for (const auto it : analyzer->lootMap) {
analyzer->lootPrice += leader->getItemCustomPrice(it.first) * it.second;
}
analyzer->supplyPrice = 0;
for (const auto it : analyzer->supplyMap) {
analyzer->supplyPrice += leader->getItemCustomPrice(it.first, true) * it.second;
}
}
}