forked from otland/forgottenserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparty.cpp
460 lines (370 loc) · 12 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
// Copyright 2022 The Forgotten Server Authors. All rights reserved.
// Use of this source code is governed by the GPL-2.0 License that can be found in the LICENSE file.
#include "otpch.h"
#include "party.h"
#include "game.h"
#include "configmanager.h"
#include "events.h"
#include <fmt/format.h>
extern Game g_game;
extern ConfigManager g_config;
extern Events* g_events;
Party::Party(Player* leader) : leader(leader)
{
leader->setParty(this);
}
void Party::disband()
{
if (!g_events->eventPartyOnDisband(this)) {
return;
}
Player* currentLeader = leader;
leader = nullptr;
currentLeader->setParty(nullptr);
currentLeader->sendClosePrivate(CHANNEL_PARTY);
g_game.updatePlayerShield(currentLeader);
currentLeader->sendCreatureSkull(currentLeader);
currentLeader->sendTextMessage(MESSAGE_INFO_DESCR, "Your party has been disbanded.");
for (Player* invitee : inviteList) {
invitee->removePartyInvitation(this);
currentLeader->sendCreatureShield(invitee);
}
inviteList.clear();
for (Player* member : memberList) {
member->setParty(nullptr);
member->sendClosePrivate(CHANNEL_PARTY);
member->sendTextMessage(MESSAGE_INFO_DESCR, "Your party has been disbanded.");
}
for (Player* member : memberList) {
g_game.updatePlayerShield(member);
for (Player* otherMember : memberList) {
otherMember->sendCreatureSkull(member);
}
member->sendCreatureSkull(currentLeader);
currentLeader->sendCreatureSkull(member);
}
memberList.clear();
delete this;
}
bool Party::leaveParty(Player* player)
{
if (!player) {
return false;
}
if (player->getParty() != this && leader != player) {
return false;
}
if (!g_events->eventPartyOnLeave(this, player)) {
return false;
}
bool missingLeader = false;
if (leader == player) {
if (!memberList.empty()) {
if (memberList.size() == 1 && inviteList.empty()) {
missingLeader = true;
} else {
passPartyLeadership(memberList.front());
}
} 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);
for (Player* member : memberList) {
member->sendCreatureSkull(player);
player->sendPlayerPartyIcons(member);
}
leader->sendCreatureSkull(player);
player->sendCreatureSkull(player);
player->sendPlayerPartyIcons(leader);
// remove pending invitation icons from the screen
for (Player* invitee : inviteList) {
player->sendCreatureShield(invitee);
}
player->sendTextMessage(MESSAGE_INFO_DESCR, "You have left the party.");
updateSharedExperience();
clearPlayerPoints(player);
broadcastPartyMessage(MESSAGE_INFO_DESCR, fmt::format("{:s} has left the party.", player->getName()));
if (missingLeader || empty()) {
disband();
}
return true;
}
bool Party::passPartyLeadership(Player* player)
{
if (!player || leader == player || player->getParty() != 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);
}
broadcastPartyMessage(MESSAGE_INFO_DESCR, fmt::format("{:s} is now the leader of the party.", player->getName()), true);
Player* oldLeader = leader;
leader = player;
memberList.insert(memberList.begin(), oldLeader);
updateSharedExperience();
for (Player* member : memberList) {
member->sendCreatureShield(oldLeader);
member->sendCreatureShield(leader);
}
for (Player* invitee : inviteList) {
invitee->sendCreatureShield(oldLeader);
invitee->sendCreatureShield(leader);
}
leader->sendCreatureShield(oldLeader);
leader->sendCreatureShield(leader);
player->sendTextMessage(MESSAGE_INFO_DESCR, "You are now the leader of the party.");
return true;
}
bool Party::joinParty(Player& player)
{
// check if lua scripts allow the player to join
if (!g_events->eventPartyOnJoin(this, &player)) {
return false;
}
// first player accepted the invitation
// the party gets officially formed
// the leader can no longer take invitations from others
if (memberList.empty()) {
leader->clearPartyInvitations();
}
// add player to the party
memberList.push_back(&player);
player.setParty(this);
broadcastPartyMessage(MESSAGE_INFO_DESCR, fmt::format("{:s} has joined the party.", player.getName()));
// remove player pending invitations to this and other parties
player.clearPartyInvitations();
// update player icon on the screen
g_game.updatePlayerShield(&player);
// update player-member party icons
for (Player* member : memberList) {
member->sendCreatureSkull(&player);
player.sendPlayerPartyIcons(member);
}
// update player-leader party icons
leader->sendCreatureSkull(&player);
player.sendPlayerPartyIcons(leader);
// update player own skull
player.sendCreatureSkull(&player);
// show the new member who else is invited
for (Player* invitee : inviteList) {
player.sendCreatureShield(invitee);
}
// check the party eligibility for shared experience
updateSharedExperience();
const std::string& leaderName = leader->getName();
player.sendTextMessage(MESSAGE_INFO_DESCR, fmt::format("You have joined {:s}'{:s} party. Open the party channel to communicate with your companions.", leaderName, leaderName.back() == 's' ? "" : "s"));
return true;
}
bool Party::removeInvite(Player& player, bool removeFromPlayer/* = true*/)
{
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(this);
}
if (empty()) {
disband();
}
return true;
}
void Party::revokeInvitation(Player& player)
{
player.sendTextMessage(MESSAGE_INFO_DESCR, fmt::format("{:s} has revoked {:s} invitation.", leader->getName(), leader->getSex() == PLAYERSEX_FEMALE ? "her" : "his"));
leader->sendTextMessage(MESSAGE_INFO_DESCR, fmt::format("Invitation for {:s} has been revoked.", player.getName()));
removeInvite(player);
}
bool Party::invitePlayer(Player& player)
{
if (isPlayerInvited(&player)) {
return false;
}
if (empty()) {
leader->sendTextMessage(MESSAGE_INFO_DESCR, fmt::format("{:s} has been invited. Open the party channel to communicate with your members.", player.getName()));
g_game.updatePlayerShield(leader);
leader->sendCreatureSkull(leader);
} else {
leader->sendTextMessage(MESSAGE_INFO_DESCR, fmt::format("{:s} has been invited.", player.getName()));
}
// add player to invite lists
inviteList.push_back(&player);
player.addPartyInvitation(this);
// update leader-invitee party status
leader->sendCreatureShield(&player);
player.sendCreatureShield(leader);
// update the invitation status for other members
for (Player* member : memberList) {
member->sendCreatureShield(&player);
}
player.sendTextMessage(MESSAGE_INFO_DESCR, fmt::format("{:s} has invited you to {:s} party.", leader->getName(), leader->getSex() == PLAYERSEX_FEMALE ? "her" : "his"));
return true;
}
bool Party::isPlayerInvited(const Player* player) const
{
return std::find(inviteList.begin(), inviteList.end(), player) != inviteList.end();
}
void Party::updateAllPartyIcons()
{
for (Player* member : memberList) {
for (Player* otherMember : memberList) {
member->sendCreatureShield(otherMember);
}
member->sendCreatureShield(leader);
leader->sendCreatureShield(member);
}
leader->sendCreatureShield(leader);
}
void Party::broadcastPartyMessage(MessageClasses msgClass, const std::string& msg, bool sendToInvitations /*= false*/)
{
for (Player* member : memberList) {
member->sendTextMessage(msgClass, msg);
}
leader->sendTextMessage(msgClass, msg);
if (sendToInvitations) {
for (Player* invitee : inviteList) {
invitee->sendTextMessage(msgClass, msg);
}
}
}
void Party::updateSharedExperience()
{
if (sharedExpActive) {
bool result = getSharedExperienceStatus() == SHAREDEXP_OK;
if (result != sharedExpEnabled) {
sharedExpEnabled = result;
updateAllPartyIcons();
}
}
}
namespace {
const char* 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(Player* player, bool sharedExpActive)
{
if (!player || leader != player) {
return false;
}
if (this->sharedExpActive == sharedExpActive) {
return true;
}
this->sharedExpActive = sharedExpActive;
if (sharedExpActive) {
SharedExpStatus_t sharedExpStatus = getSharedExperienceStatus();
this->sharedExpEnabled = sharedExpStatus == SHAREDEXP_OK;
leader->sendTextMessage(MESSAGE_INFO_DESCR, getSharedExpReturnMessage(sharedExpStatus));
} else {
leader->sendTextMessage(MESSAGE_INFO_DESCR, "Shared Experience has been deactivated.");
}
updateAllPartyIcons();
return true;
}
void Party::shareExperience(uint64_t experience, Creature* source/* = nullptr*/)
{
uint64_t shareExperience = experience;
g_events->eventPartyOnShareExperience(this, shareExperience);
for (Player* member : memberList) {
member->onGainSharedExperience(shareExperience, source);
}
leader->onGainSharedExperience(shareExperience, source);
}
bool Party::canUseSharedExperience(const Player* player) const
{
return getMemberSharedExperienceStatus(player) == SHAREDEXP_OK;
}
SharedExpStatus_t Party::getMemberSharedExperienceStatus(const Player* player) const
{
if (memberList.empty()) {
return SHAREDEXP_EMPTYPARTY;
}
uint32_t highestLevel = leader->getLevel();
for (Player* member : memberList) {
if (member->getLevel() > highestLevel) {
highestLevel = member->getLevel();
}
}
uint32_t minLevel = static_cast<uint32_t>(std::ceil((static_cast<float>(highestLevel) * 2) / 3));
if (player->getLevel() < minLevel) {
return SHAREDEXP_LEVELDIFFTOOLARGE;
}
if (!Position::areInRange<EXPERIENCE_SHARE_RANGE, EXPERIENCE_SHARE_RANGE, EXPERIENCE_SHARE_FLOORS>(leader->getPosition(), player->getPosition())) {
return SHAREDEXP_TOOFARAWAY;
}
if (!player->hasFlag(PlayerFlag_NotGainInFight)) {
//check if the player has healed/attacked anything recently
auto it = ticksMap.find(player->getID());
if (it == ticksMap.end()) {
return SHAREDEXP_MEMBERINACTIVE;
}
uint64_t timeDiff = OTSYS_TIME() - it->second;
if (timeDiff > static_cast<uint64_t>(g_config.getNumber(ConfigManager::PZ_LOCKED))) {
return SHAREDEXP_MEMBERINACTIVE;
}
}
return SHAREDEXP_OK;
}
SharedExpStatus_t Party::getSharedExperienceStatus()
{
SharedExpStatus_t leaderStatus = getMemberSharedExperienceStatus(leader);
if (leaderStatus != SHAREDEXP_OK) {
return leaderStatus;
}
for (Player* member : memberList) {
SharedExpStatus_t memberStatus = getMemberSharedExperienceStatus(member);
if (memberStatus != SHAREDEXP_OK) {
return memberStatus;
}
}
return SHAREDEXP_OK;
}
void Party::updatePlayerTicks(Player* player, uint32_t points)
{
if (points != 0 && !player->hasFlag(PlayerFlag_NotGainInFight)) {
ticksMap[player->getID()] = OTSYS_TIME();
updateSharedExperience();
}
}
void Party::clearPlayerPoints(Player* player)
{
auto it = ticksMap.find(player->getID());
if (it != ticksMap.end()) {
ticksMap.erase(it);
updateSharedExperience();
}
}
bool Party::canOpenCorpse(uint32_t ownerId) const
{
if (Player* player = g_game.getPlayerByID(ownerId)) {
return leader->getID() == ownerId || player->getParty() == this;
}
return false;
}