-
-
Notifications
You must be signed in to change notification settings - Fork 669
/
Copy pathplayer.cpp
5512 lines (4601 loc) · 129 KB
/
player.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
/**
* The Forgotten Server - a free and open-source MMORPG server emulator
* Copyright (C) 2019 Mark Samman <mark.samman@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "otpch.h"
#include <bitset>
#include "creatures/players/player.h"
#include "items/bed.h"
#include "creatures/interactions/chat.h"
#include "creatures/combat/combat.h"
#include "config/configmanager.h"
#include "lua/creature/creatureevent.h"
#include "lua/creature/events.h"
#include "game/game.h"
#include "io/iologindata.h"
#include "creatures/monsters/monster.h"
#include "creatures/monsters/monsters.h"
#include "lua/creature/movement.h"
#include "game/scheduling/scheduler.h"
#include "items/weapons/weapons.h"
#include "io/iobestiary.h"
extern ConfigManager g_config;
extern Game g_game;
extern Chat* g_chat;
extern Vocations g_vocations;
extern MoveEvents* g_moveEvents;
extern Weapons* g_weapons;
extern CreatureEvents* g_creatureEvents;
extern Events* g_events;
extern Imbuements* g_imbuements;
extern Monsters g_monsters;
MuteCountMap Player::muteCountMap;
uint32_t Player::playerAutoID = 0x10010000;
Player::Player(ProtocolGame_ptr p) :
Creature(),
lastPing(OTSYS_TIME()),
lastPong(lastPing),
inbox(new Inbox(ITEM_INBOX)),
client(std::move(p)) {
inbox->incrementReferenceCounter();
}
Player::~Player()
{
for (Item* item : inventory) {
if (item) {
item->setParent(nullptr);
item->decrementReferenceCounter();
}
}
for (const auto& it : depotLockerMap) {
it.second->removeInbox(inbox);
it.second->decrementReferenceCounter();
}
for (const auto& it : rewardMap) {
it.second->decrementReferenceCounter();
}
for (const auto& it : quickLootContainers) {
it.second->decrementReferenceCounter();
}
inbox->decrementReferenceCounter();
setWriteItem(nullptr);
setEditHouse(nullptr);
logged = false;
}
bool Player::setVocation(uint16_t vocId)
{
Vocation* voc = g_vocations.getVocation(vocId);
if (!voc) {
return false;
}
vocation = voc;
Condition* condition = getCondition(CONDITION_REGENERATION, CONDITIONID_DEFAULT);
if (condition) {
condition->setParam(CONDITION_PARAM_HEALTHGAIN, vocation->getHealthGainAmount());
condition->setParam(CONDITION_PARAM_HEALTHTICKS, vocation->getHealthGainTicks() * 1000);
condition->setParam(CONDITION_PARAM_MANAGAIN, vocation->getManaGainAmount());
condition->setParam(CONDITION_PARAM_MANATICKS, vocation->getManaGainTicks() * 1000);
}
g_game.addPlayerVocation(this);
return true;
}
bool Player::isPushable() const
{
if (hasFlag(PlayerFlag_CannotBePushed)) {
return false;
}
return Creature::isPushable();
}
std::string Player::getDescription(int32_t lookDistance) const
{
std::ostringstream s;
if (lookDistance == -1) {
s << "yourself.";
if (group->access) {
s << " You are " << group->name << '.';
} else if (vocation->getId() != VOCATION_NONE) {
s << " You are " << vocation->getVocDescription() << '.';
} else {
s << " You have no vocation.";
}
} else {
s << name;
if (!group->access) {
s << " (Level " << level << ')';
}
s << '.';
if (sex == PLAYERSEX_FEMALE) {
s << " She";
} else {
s << " He";
}
if (group->access) {
s << " is " << group->name << '.';
} else if (vocation->getId() != VOCATION_NONE) {
s << " is " << vocation->getVocDescription() << '.';
} else {
s << " has no vocation.";
}
}
if (party) {
if (lookDistance == -1) {
s << " Your party has ";
} else if (sex == PLAYERSEX_FEMALE) {
s << " She is in a party with ";
} else {
s << " He is in a party with ";
}
size_t memberCount = party->getMemberCount() + 1;
if (memberCount == 1) {
s << "1 member and ";
} else {
s << memberCount << " members and ";
}
size_t invitationCount = party->getInvitationCount();
if (invitationCount == 1) {
s << "1 pending invitation.";
} else {
s << invitationCount << " pending invitations.";
}
}
if (guild && guildRank) {
size_t memberCount = guild->getMemberCount();
if (memberCount >= 1000) {
s << "";
return s.str();
}
if (lookDistance == -1) {
s << " You are ";
} else if (sex == PLAYERSEX_FEMALE) {
s << " She is ";
} else {
s << " He is ";
}
s << guildRank->name << " of the " << guild->getName();
if (!guildNick.empty()) {
s << " (" << guildNick << ')';
}
if (memberCount == 1) {
s << ", which has 1 member, " << guild->getMembersOnline().size() << " of them online.";
} else {
s << ", which has " << memberCount << " members, " << guild->getMembersOnline().size() << " of them online.";
}
}
return s.str();
}
Item* Player::getInventoryItem(Slots_t slot) const
{
if (slot < CONST_SLOT_FIRST || slot > CONST_SLOT_LAST) {
return nullptr;
}
return inventory[slot];
}
void Player::addConditionSuppressions(uint32_t addConditions)
{
conditionSuppressions |= addConditions;
}
void Player::removeConditionSuppressions(uint32_t removeConditions)
{
conditionSuppressions &= ~removeConditions;
}
Item* Player::getWeapon(Slots_t slot, bool ignoreAmmo) const
{
Item* item = inventory[slot];
if (!item) {
return nullptr;
}
WeaponType_t weaponType = item->getWeaponType();
if (weaponType == WEAPON_NONE || weaponType == WEAPON_SHIELD || weaponType == WEAPON_AMMO) {
return nullptr;
}
if (!ignoreAmmo && weaponType == WEAPON_DISTANCE) {
const ItemType& it = Item::items[item->getID()];
if (it.ammoType != AMMO_NONE) {
Item* quiver = inventory[CONST_SLOT_RIGHT];
if (!quiver || quiver->getWeaponType() != WEAPON_QUIVER)
return nullptr;
Container* container = quiver->getContainer();
if (!container)
return nullptr;
bool found = false;
for (Item* ammoItem : container->getItemList()) {
if (ammoItem->getAmmoType() == it.ammoType) {
item = ammoItem;
found = true;
break;
}
}
if (!found)
return nullptr;
}
}
return item;
}
Item* Player::getWeapon(bool ignoreAmmo/* = false*/) const
{
Item* item = getWeapon(CONST_SLOT_LEFT, ignoreAmmo);
if (item) {
return item;
}
item = getWeapon(CONST_SLOT_RIGHT, ignoreAmmo);
if (item) {
return item;
}
return nullptr;
}
WeaponType_t Player::getWeaponType() const
{
Item* item = getWeapon();
if (!item) {
return WEAPON_NONE;
}
return item->getWeaponType();
}
int32_t Player::getWeaponSkill(const Item* item) const
{
if (!item) {
return getSkillLevel(SKILL_FIST);
}
int32_t attackSkill;
WeaponType_t weaponType = item->getWeaponType();
switch (weaponType) {
case WEAPON_SWORD: {
attackSkill = getSkillLevel(SKILL_SWORD);
break;
}
case WEAPON_CLUB: {
attackSkill = getSkillLevel(SKILL_CLUB);
break;
}
case WEAPON_AXE: {
attackSkill = getSkillLevel(SKILL_AXE);
break;
}
case WEAPON_DISTANCE: {
attackSkill = getSkillLevel(SKILL_DISTANCE);
break;
}
default: {
attackSkill = 0;
break;
}
}
return attackSkill;
}
int32_t Player::getArmor() const
{
int32_t armor = 0;
static const Slots_t armorSlots[] = {CONST_SLOT_HEAD, CONST_SLOT_NECKLACE, CONST_SLOT_ARMOR, CONST_SLOT_LEGS, CONST_SLOT_FEET, CONST_SLOT_RING};
for (Slots_t slot : armorSlots) {
Item* inventoryItem = inventory[slot];
if (inventoryItem) {
armor += inventoryItem->getArmor();
}
}
return static_cast<int32_t>(armor * vocation->armorMultiplier);
}
void Player::getShieldAndWeapon(const Item*& shield, const Item*& weapon) const
{
shield = nullptr;
weapon = nullptr;
for (uint32_t slot = CONST_SLOT_RIGHT; slot <= CONST_SLOT_LEFT; slot++) {
Item* item = inventory[slot];
if (!item) {
continue;
}
switch (item->getWeaponType()) {
case WEAPON_NONE:
break;
case WEAPON_SHIELD: {
if (!shield || (shield && item->getDefense() > shield->getDefense())) {
shield = item;
}
break;
}
default: { // weapons that are not shields
weapon = item;
break;
}
}
}
}
int32_t Player::getDefense() const
{
int32_t defenseSkill = getSkillLevel(SKILL_FIST);
int32_t defenseValue = 7;
const Item* weapon;
const Item* shield;
try {
getShieldAndWeapon(shield, weapon);
}
catch (const std::exception &e) {
SPDLOG_ERROR("{} got exception {}", getName(), e.what());
}
if (weapon) {
defenseValue = weapon->getDefense() + weapon->getExtraDefense();
defenseSkill = getWeaponSkill(weapon);
}
if (shield) {
defenseValue = weapon != nullptr ? shield->getDefense() + weapon->getExtraDefense() : shield->getDefense();
defenseSkill = getSkillLevel(SKILL_SHIELD);
}
if (defenseSkill == 0) {
switch (fightMode) {
case FIGHTMODE_ATTACK:
case FIGHTMODE_BALANCED:
return 1;
case FIGHTMODE_DEFENSE:
return 2;
}
}
return (defenseSkill / 4. + 2.23) * defenseValue * 0.15 * getDefenseFactor() * vocation->defenseMultiplier;
}
float Player::getAttackFactor() const
{
switch (fightMode) {
case FIGHTMODE_ATTACK: return 1.0f;
case FIGHTMODE_BALANCED: return 0.75f;
case FIGHTMODE_DEFENSE: return 0.5f;
default: return 1.0f;
}
}
float Player::getDefenseFactor() const
{
switch (fightMode) {
case FIGHTMODE_ATTACK: return (OTSYS_TIME() - lastAttack) < getAttackSpeed() ? 0.5f : 1.0f;
case FIGHTMODE_BALANCED: return (OTSYS_TIME() - lastAttack) < getAttackSpeed() ? 0.75f : 1.0f;
case FIGHTMODE_DEFENSE: return 1.0f;
default: return 1.0f;
}
}
uint32_t Player::getClientIcons() const
{
uint32_t icons = 0;
for (Condition* condition : conditions) {
if (!isSuppress(condition->getType())) {
icons |= condition->getIcons();
}
}
if (pzLocked) {
icons |= ICON_REDSWORDS;
}
if (tile->hasFlag(TILESTATE_PROTECTIONZONE)) {
icons |= ICON_PIGEON;
client->sendRestingStatus(1);
// Don't show ICON_SWORDS if player is in protection zone.
if (hasBitSet(ICON_SWORDS, icons)) {
icons &= ~ICON_SWORDS;
}
} else {
client->sendRestingStatus(0);
}
// Game client debugs with 10 or more icons
// so let's prevent that from happening.
std::bitset<32> icon_bitset(static_cast<uint64_t>(icons));
for (size_t pos = 0, bits_set = icon_bitset.count(); bits_set >= 10; ++pos) {
if (icon_bitset[pos]) {
icon_bitset.reset(pos);
--bits_set;
}
}
return icon_bitset.to_ulong();
}
void Player::updateInventoryWeight()
{
if (hasFlag(PlayerFlag_HasInfiniteCapacity)) {
return;
}
inventoryWeight = 0;
for (int i = CONST_SLOT_FIRST; i <= CONST_SLOT_LAST; ++i) {
const Item* item = inventory[i];
if (item) {
inventoryWeight += item->getWeight();
}
}
}
void Player::setTraining(bool value) {
for (const auto& it : g_game.getPlayers()) {
if (!this->isInGhostMode() || it.second->isAccessPlayer()) {
it.second->notifyStatusChange(this, value ? VIPSTATUS_TRAINING : VIPSTATUS_ONLINE, false);
}
}
setExerciseTraining(value);
}
void Player::addSkillAdvance(skills_t skill, uint64_t count)
{
uint64_t currReqTries = vocation->getReqSkillTries(skill, skills[skill].level);
uint64_t nextReqTries = vocation->getReqSkillTries(skill, skills[skill].level + 1);
if (currReqTries >= nextReqTries) {
//player has reached max skill
return;
}
g_events->eventPlayerOnGainSkillTries(this, skill, count);
if (count == 0) {
return;
}
bool sendUpdateSkills = false;
while ((skills[skill].tries + count) >= nextReqTries) {
count -= nextReqTries - skills[skill].tries;
skills[skill].level++;
skills[skill].tries = 0;
skills[skill].percent = 0;
std::ostringstream ss;
ss << "You advanced to " << getSkillName(skill) << " level " << skills[skill].level << '.';
sendTextMessage(MESSAGE_EVENT_ADVANCE, ss.str());
g_creatureEvents->playerAdvance(this, skill, (skills[skill].level - 1), skills[skill].level);
sendUpdateSkills = true;
currReqTries = nextReqTries;
nextReqTries = vocation->getReqSkillTries(skill, skills[skill].level + 1);
if (currReqTries >= nextReqTries) {
count = 0;
break;
}
}
skills[skill].tries += count;
uint32_t newPercent;
if (nextReqTries > currReqTries) {
newPercent = Player::getPercentLevel(skills[skill].tries, nextReqTries);
} else {
newPercent = 0;
}
if (skills[skill].percent != newPercent) {
skills[skill].percent = newPercent;
sendUpdateSkills = true;
}
if (sendUpdateSkills) {
sendSkills();
sendStats();
}
}
void Player::setVarStats(stats_t stat, int32_t modifier)
{
varStats[stat] += modifier;
switch (stat) {
case STAT_MAXHITPOINTS: {
if (getHealth() > getMaxHealth()) {
Creature::changeHealth(getMaxHealth() - getHealth());
} else {
g_game.addCreatureHealth(this);
}
break;
}
case STAT_MAXMANAPOINTS: {
if (getMana() > getMaxMana()) {
Creature::changeMana(getMaxMana() - getMana());
}
else {
g_game.addPlayerMana(this);
}
break;
}
default: {
break;
}
}
}
int32_t Player::getDefaultStats(stats_t stat) const
{
switch (stat) {
case STAT_MAXHITPOINTS: return healthMax;
case STAT_MAXMANAPOINTS: return manaMax;
case STAT_MAGICPOINTS: return getBaseMagicLevel();
default: return 0;
}
}
void Player::addContainer(uint8_t cid, Container* container)
{
if (cid > 0xF) {
return;
}
if (!container) {
return;
}
if (container->getID() == ITEM_BROWSEFIELD) {
container->incrementReferenceCounter();
}
auto it = openContainers.find(cid);
if (it != openContainers.end()) {
OpenContainer& openContainer = it->second;
Container* oldContainer = openContainer.container;
if (oldContainer->getID() == ITEM_BROWSEFIELD) {
oldContainer->decrementReferenceCounter();
}
openContainer.container = container;
openContainer.index = 0;
} else {
OpenContainer openContainer;
openContainer.container = container;
openContainer.index = 0;
openContainers[cid] = openContainer;
}
}
void Player::closeContainer(uint8_t cid)
{
auto it = openContainers.find(cid);
if (it == openContainers.end()) {
return;
}
OpenContainer openContainer = it->second;
Container* container = openContainer.container;
openContainers.erase(it);
if (container && container->getID() == ITEM_BROWSEFIELD) {
container->decrementReferenceCounter();
}
}
void Player::setContainerIndex(uint8_t cid, uint16_t index)
{
auto it = openContainers.find(cid);
if (it == openContainers.end()) {
return;
}
it->second.index = index;
}
Container* Player::getContainerByID(uint8_t cid)
{
auto it = openContainers.find(cid);
if (it == openContainers.end()) {
return nullptr;
}
return it->second.container;
}
int8_t Player::getContainerID(const Container* container) const
{
for (const auto& it : openContainers) {
if (it.second.container == container) {
return it.first;
}
}
return -1;
}
uint16_t Player::getContainerIndex(uint8_t cid) const
{
auto it = openContainers.find(cid);
if (it == openContainers.end()) {
return 0;
}
return it->second.index;
}
bool Player::canOpenCorpse(uint32_t ownerId) const
{
return getID() == ownerId || (party && party->canOpenCorpse(ownerId));
}
uint16_t Player::getLookCorpse() const
{
if (sex == PLAYERSEX_FEMALE) {
return ITEM_FEMALE_CORPSE;
} else {
return ITEM_MALE_CORPSE;
}
}
void Player::addStorageValue(const uint32_t key, const int32_t value, const bool isLogin/* = false*/)
{
if (IS_IN_KEYRANGE(key, RESERVED_RANGE)) {
if (IS_IN_KEYRANGE(key, OUTFITS_RANGE)) {
outfits.emplace_back(
value >> 16,
value & 0xFF
);
return;
} else if (IS_IN_KEYRANGE(key, MOUNTS_RANGE)) {
// do nothing
} else if (IS_IN_KEYRANGE(key, FAMILIARS_RANGE)) {
familiars.emplace_back(
value >> 16);
return;
} else {
SPDLOG_WARN("Unknown reserved key: {} for player: {}", key, getName());
return;
}
}
if (value != -1) {
int32_t oldValue;
getStorageValue(key, oldValue);
storageMap[key] = value;
if (!isLogin) {
auto currentFrameTime = g_dispatcher.getDispatcherCycle();
g_events->eventOnStorageUpdate(this, key, value, oldValue, currentFrameTime);
}
} else {
storageMap.erase(key);
}
}
bool Player::getStorageValue(const uint32_t key, int32_t& value) const
{
auto it = storageMap.find(key);
if (it == storageMap.end()) {
value = -1;
return false;
}
value = it->second;
return true;
}
bool Player::canSee(const Position& pos) const
{
if (!client) {
return false;
}
return client->canSee(pos);
}
bool Player::canSeeCreature(const Creature* creature) const
{
if (creature == this) {
return true;
}
if (creature->isInGhostMode() && !group->access) {
return false;
}
if (!creature->getPlayer() && !canSeeInvisibility() && creature->isInvisible()) {
return false;
}
return true;
}
bool Player::canWalkthrough(const Creature* creature) const
{
if (group->access || creature->isInGhostMode()) {
return true;
}
const Player* player = creature->getPlayer();
const Monster* monster = creature->getMonster();
const Npc* npc = creature->getNpc();
if (monster) {
if (!monster->isPet()) {
return false;
}
return true;
}
if (player) {
const Tile* playerTile = player->getTile();
if (!playerTile || (!playerTile->hasFlag(TILESTATE_NOPVPZONE) && !playerTile->hasFlag(TILESTATE_PROTECTIONZONE) && player->getLevel() > static_cast<uint32_t>(g_config.getNumber(PROTECTION_LEVEL)) && g_game.getWorldType() != WORLD_TYPE_NO_PVP)) {
return false;
}
const Item* playerTileGround = playerTile->getGround();
if (!playerTileGround || !playerTileGround->hasWalkStack()) {
return false;
}
Player* thisPlayer = const_cast<Player*>(this);
if ((OTSYS_TIME() - lastWalkthroughAttempt) > 2000) {
thisPlayer->setLastWalkthroughAttempt(OTSYS_TIME());
return false;
}
if (creature->getPosition() != lastWalkthroughPosition) {
thisPlayer->setLastWalkthroughPosition(creature->getPosition());
return false;
}
thisPlayer->setLastWalkthroughPosition(creature->getPosition());
return true;
} else if (npc) {
const Tile* tile = npc->getTile();
const HouseTile* houseTile = dynamic_cast<const HouseTile*>(tile);
return (houseTile != nullptr);
}
return false;
}
bool Player::canWalkthroughEx(const Creature* creature) const
{
if (group->access) {
return true;
}
const Monster* monster = creature->getMonster();
if (monster) {
if (!monster->isPet()) {
return false;
}
return true;
}
const Player* player = creature->getPlayer();
const Npc* npc = creature->getNpc();
if (player) {
const Tile* playerTile = player->getTile();
return playerTile && (playerTile->hasFlag(TILESTATE_NOPVPZONE) || playerTile->hasFlag(TILESTATE_PROTECTIONZONE) || player->getLevel() <= static_cast<uint32_t>(g_config.getNumber(PROTECTION_LEVEL)) || g_game.getWorldType() == WORLD_TYPE_NO_PVP);
} else if (npc) {
const Tile* tile = npc->getTile();
const HouseTile* houseTile = dynamic_cast<const HouseTile*>(tile);
return (houseTile != nullptr);
} else {
return false;
}
}
void Player::onReceiveMail() const
{
if (isNearDepotBox()) {
sendTextMessage(MESSAGE_EVENT_ADVANCE, "New mail has arrived.");
}
}
Container* Player::setLootContainer(ObjectCategory_t category, Container* container, bool loading /* = false*/)
{
Container* previousContainer = nullptr;
auto it = quickLootContainers.find(category);
if (it != quickLootContainers.end() && !loading) {
previousContainer = (*it).second;
uint32_t flags = previousContainer->getIntAttr(ITEM_ATTRIBUTE_QUICKLOOTCONTAINER);
flags &= ~(1 << category);
if (flags == 0) {
previousContainer->removeAttribute(ITEM_ATTRIBUTE_QUICKLOOTCONTAINER);
} else {
previousContainer->setIntAttr(ITEM_ATTRIBUTE_QUICKLOOTCONTAINER, flags);
}
previousContainer->decrementReferenceCounter();
quickLootContainers.erase(it);
}
if (container) {
previousContainer = container;
quickLootContainers[category] = container;
container->incrementReferenceCounter();
if (!loading) {
uint32_t flags = container->getIntAttr(ITEM_ATTRIBUTE_QUICKLOOTCONTAINER);
container->setIntAttr(ITEM_ATTRIBUTE_QUICKLOOTCONTAINER, flags | static_cast<uint32_t>(1 << category));
}
}
return previousContainer;
}
Container* Player::getLootContainer(ObjectCategory_t category) const
{
if (category != OBJECTCATEGORY_DEFAULT && !isPremium()) {
category = OBJECTCATEGORY_DEFAULT;
}
auto it = quickLootContainers.find(category);
if (it != quickLootContainers.end()) {
return (*it).second;
}
if (category != OBJECTCATEGORY_DEFAULT) {
// firstly, fallback to default
return getLootContainer(OBJECTCATEGORY_DEFAULT);
}
return nullptr;
}
void Player::checkLootContainers(const Item* item)
{
if (!item) {
return;
}
const Container* container = item->getContainer();
if (!container) {
return;
}
bool shouldSend = false;
auto it = quickLootContainers.begin();
while (it != quickLootContainers.end()) {
Container* lootContainer = (*it).second;
bool remove = false;
if (item->getHoldingPlayer() != this && (item == lootContainer || container->isHoldingItem(lootContainer))) {
remove = true;
}
if (remove) {
shouldSend = true;
it = quickLootContainers.erase(it);
lootContainer->decrementReferenceCounter();
lootContainer->removeAttribute(ITEM_ATTRIBUTE_QUICKLOOTCONTAINER);
} else {
++it;
}
}
if (shouldSend) {
sendLootContainers();
}
}
bool Player::isNearDepotBox() const
{
const Position& pos = getPosition();
for (int32_t cx = -1; cx <= 1; ++cx) {
for (int32_t cy = -1; cy <= 1; ++cy) {
Tile* posTile = g_game.map.getTile(pos.x + cx, pos.y + cy, pos.z);
if (!posTile) {
continue;
}
if (posTile->hasFlag(TILESTATE_DEPOT)) {
return true;
}
}
}
return false;
}
DepotChest* Player::getDepotBox()
{
DepotChest* depotBoxs = new DepotChest(ITEM_DEPOT);
depotBoxs->incrementReferenceCounter();
depotBoxs->setMaxDepotItems(getMaxDepotItems());
for (uint32_t index = 1; index <= 18; ++index) {
depotBoxs->internalAddThing(getDepotChest(19 - index, true));
}
return depotBoxs;
}
DepotChest* Player::getDepotChest(uint32_t depotId, bool autoCreate)
{
auto it = depotChests.find(depotId);
if (it != depotChests.end()) {
return it->second;
}
if (!autoCreate) {
return nullptr;
}
DepotChest* depotChest;
if (depotId > 0 && depotId < 18) {
depotChest = new DepotChest(ITEM_DEPOT_NULL + depotId);
}
else {
depotChest = new DepotChest(ITEM_DEPOT_XVIII);
}
depotChest->incrementReferenceCounter();
depotChests[depotId] = depotChest;
return depotChest;
}
DepotLocker* Player::getDepotLocker(uint32_t depotId)
{
auto it = depotLockerMap.find(depotId);
if (it != depotLockerMap.end()) {
inbox->setParent(it->second);
for (uint8_t i = g_config.getNumber(DEPOT_BOXES); i > 0; i--) {
if (DepotChest* depotBox = getDepotChest(i, false)) {
depotBox->setParent(it->second->getItemByIndex(0)->getContainer());
}
}
return it->second;
}
DepotLocker* depotLocker = new DepotLocker(ITEM_LOCKER);
depotLocker->setDepotId(depotId);
depotLocker->internalAddThing(Item::CreateItem(ITEM_MARKET));
depotLocker->internalAddThing(inbox);
depotLocker->internalAddThing(Item::CreateItem(ITEM_SUPPLY_STASH));
Container* depotChest = Item::CreateItemAsContainer(ITEM_DEPOT, g_config.getNumber(DEPOT_BOXES));
for (uint8_t i = g_config.getNumber(DEPOT_BOXES); i > 0; i--) {
DepotChest* depotBox = getDepotChest(i, true);
depotChest->internalAddThing(depotBox);