/
User.cpp
6116 lines (5177 loc) · 155 KB
/
User.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 "stdafx.h"
#include "Map.h"
#include "KnightsManager.h"
#include "KingSystem.h"
#include "MagicInstance.h"
#include "DBAgent.h"
#include <algorithm>
#include "../shared/DateTime.h"
using namespace std;
CUser::CUser(uint16_t socketID, SocketMgr *mgr) : KOSocket(socketID, mgr, -1, 16384, 3172), Unit(UnitPlayer)
{
m_bHasCheckedClientVersion = false;
}
/**
* @brief Executes the connect action.
*/
void CUser::OnConnect()
{
KOSocket::OnConnect();
Initialize();
}
/**
* @brief Initializes this object.
*/
void CUser::Initialize()
{
Unit::Initialize();
m_strUserID.clear();
m_strAccountID.clear();
m_bLogout = 0;
m_bAuthority = 1;
m_sBind = -1;
m_state = GAME_STATE_CONNECTED;
m_bSelectedCharacter = false;
m_bStoreOpen = false;
m_bPartyLeader = false;
m_bIsChicken = false;
m_bIsHidingHelmet = false;
m_bMining = false;
m_bPremiumMerchant = false;
m_bInParty = false;
m_tLastMiningAttempt = 0;
m_bMerchantState = MERCHANT_STATE_NONE;
m_bInvisibilityType = INVIS_NONE;
m_sDirection = 0;
m_sItemMaxHp = m_sItemMaxMp = 0;
m_sItemWeight = 0;
m_sItemAc = 0;
m_sExpGainAmount = m_bNPGainAmount = m_bNoahGainAmount = 100;
m_bItemExpGainAmount = m_bItemNoahGainAmount = 0;
m_bItemNPBonus = m_bSkillNPBonus = 0;
m_byAPBonusAmount = 0;
memset(&m_byAPClassBonusAmount, 0, sizeof(m_byAPClassBonusAmount));
memset(&m_byAcClassBonusAmount, 0, sizeof(m_byAcClassBonusAmount));
memset(&m_bStats, 0, sizeof(m_bStats));
memset(&m_sStatItemBonuses, 0, sizeof(m_sStatItemBonuses));
memset(&m_bStatBuffs, 0, sizeof(m_bStatBuffs));
memset(&m_bstrSkill, 0, sizeof(m_bstrSkill));
m_bPlayerAttackAmount = 100;
m_bAddWeaponDamage = 0;
m_bPctArmourAc = 100;
m_sAddArmourAc = 0;
m_sItemHitrate = 100;
m_sItemEvasionrate = 100;
m_sSpeed = 0;
m_bAuthority = AUTHORITY_PLAYER;
m_bLevel = 1;
m_iExp = 0;
m_iBank = m_iGold = 0;
m_iLoyalty = m_iLoyaltyMonthly = 0;
m_iMannerPoint = 0;
m_sHp = m_sMp = m_sSp = 0;
m_iMaxHp = 0;
m_iMaxMp = 1;
m_iMaxExp = 0;
m_sMaxWeight = 0;
m_sMaxWeightBonus = 0;
m_bResHpType = USER_STANDING;
m_bWarp = false;
m_sMerchantsSocketID = -1;
m_sChallengeUser = -1;
m_sPartyIndex = -1;
m_sExchangeUser = -1;
m_bRequestingChallenge = 0;
m_bChallengeRequested = 0;
m_bExchangeOK = 0x00;
m_bBlockPrivateChat = false;
m_sPrivateChatUser = -1;
m_bNeedParty = 0x01;
m_tHPLastTimeNormal = 0; // For Automatic HP recovery.
m_tHPStartTimeNormal = 0;
m_bHPAmountNormal = 0;
m_bHPDurationNormal = 0;
m_bHPIntervalNormal = 5;
m_tGameStartTimeSavedMagic = 0;
m_fSpeedHackClientTime = 0;
m_fSpeedHackServerTime = 0;
m_bSpeedHackCheck = 0;
m_tBlinkExpiryTime = 0;
m_bAbnormalType = ABNORMAL_NORMAL; // User starts out in normal size.
m_nOldAbnormalType = m_bAbnormalType;
m_sWhoKilledMe = -1;
m_iLostExp = 0;
m_tLastTrapAreaTime = 0;
memset(m_iSelMsgEvent, -1, MAX_MESSAGE_EVENT);
m_sEventNid = m_sEventSid = -1;
m_nQuestHelperID = 0;
m_bZoneChangeFlag = false;
m_bRegeneType = 0;
m_tLastRegeneTime = 0;
m_bZoneChangeSameZone = false;
m_transformationType = TransformationNone;
m_sTransformID = 0;
m_tTransformationStartTime = 0;
m_sTransformationDuration = 0;
memset(&m_bKillCounts, 0, sizeof(m_bKillCounts));
m_sEventDataIndex = 0;
m_pKnightsUser = nullptr;
m_sRivalID = -1;
m_tRivalExpiryTime = 0;
m_byAngerGauge = 0;
m_bWeaponsDisabled = false;
m_teamColour = TeamColourNone;
m_iLoyaltyDaily = 0;
m_iLoyaltyPremiumBonus = 0;
m_KillCount = 0;
m_DeathCount = 0;
m_LastX = 0;
m_LastZ = 0;
}
/**
* @brief Executes the disconnect action.
*/
void CUser::OnDisconnect()
{
KOSocket::OnDisconnect();
g_pMain->RemoveSessionNames(this);
if (isInGame())
{
UserInOut(INOUT_OUT);
if (isInParty())
if (isPartyLeader())
{
_PARTY_GROUP * pParty = g_pMain->GetPartyPtr(GetPartyID());
PartyPromote(pParty->uid[1]);
}
PartyRemove(GetSocketID());
if (isInClan())
{
CKnights *pKnights = g_pMain->GetClanPtr(GetClanID());
_KNIGHTS_ALLIANCE *pAllianceKnights = g_pMain->GetAlliancePtr(pKnights->GetAllianceID());
if (pAllianceKnights != nullptr)
pKnights->OnLogoutAlliance(this);
else if (pKnights != nullptr)
pKnights->OnLogout(this);
}
ResetWindows();
if (hasRival())
RemoveRival();
}
LogOut();
}
/**
* @brief Handles an incoming user packet.
*
* @param pkt The packet.
*
* @return true if it succeeds, false if it fails.
*/
bool CUser::HandlePacket(Packet & pkt)
{
uint8_t command;
pkt >> command;
TRACE("[SID=%d] Packet: %X (len=%d)\n", GetSocketID(), command, pkt.wpos());
// If crypto's not been enabled yet, force the version packet to be sent.
//if (!isCryptoEnabled())
/*
if(!m_bHasCheckedClientVersion)
{
if (command == WIZ_VERSION_CHECK)
VersionCheck(pkt);
m_bHasCheckedClientVersion = true;
return true;
}*/
// If we're not authed yet, forced us to before we can do anything else.
// NOTE: We're checking the account ID store here because it's only set on successful auth,
// at which time the other account ID will be cleared out (yes, it's messy -- need to clean it up).
/*else*/ if (m_strAccountID.empty())
{
// TEMP
if (command == WIZ_VERSION_CHECK)
VersionCheck(pkt);
if (command == WIZ_LOGIN)
LoginProcess(pkt);
return true;
}
// If we haven't logged in yet, don't let us hit in-game packets.
// TODO: Make sure we support all packets in the loading stage (and rewrite this logic considerably better).
else if (!m_bSelectedCharacter)
{
switch (command)
{
case WIZ_SEL_NATION:
SelNationToAgent(pkt);
break;
case WIZ_ALLCHAR_INFO_REQ:
AllCharInfoToAgent();
break;
case WIZ_CHANGE_HAIR:
ChangeHair(pkt);
break;
case WIZ_NEW_CHAR:
NewCharToAgent(pkt);
break;
case WIZ_DEL_CHAR:
DelCharToAgent(pkt);
break;
case WIZ_SEL_CHAR:
SelCharToAgent(pkt);
break;
case WIZ_SPEEDHACK_CHECK:
SpeedHackTime(pkt);
break;
default:
TRACE("[SID=%d] Unhandled packet (%X) prior to selecting character\n", GetSocketID(), command);
break;
}
return true;
}
// Otherwise, assume we're authed & in-game.
switch (command)
{
case WIZ_GAMESTART:
GameStart(pkt);
break;
case WIZ_TIMENOTIFY: // @Demircivi: added this case in order to fix disconnecting because of ping packets which are sent by the Client.
break;
case WIZ_SERVER_INDEX:
SendServerIndex();
break;
case WIZ_RENTAL:
RentalSystem(pkt);
break;
case WIZ_SKILLDATA:
SkillDataProcess(pkt);
break;
case WIZ_MOVE:
MoveProcess(pkt);
break;
case WIZ_ROTATE:
Rotate(pkt);
break;
case WIZ_ATTACK:
Attack(pkt);
break;
case WIZ_CHAT:
Chat(pkt);
break;
case WIZ_CHAT_TARGET:
ChatTargetSelect(pkt);
break;
case WIZ_REGENE:
Regene(pkt.read<uint8_t>()); // respawn type
break;
case WIZ_REQ_USERIN:
RequestUserIn(pkt);
break;
case WIZ_REQ_NPCIN:
RequestNpcIn(pkt);
break;
case WIZ_WARP:
if (isGM())
RecvWarp(pkt);
break;
case WIZ_ITEM_MOVE:
ItemMove(pkt);
break;
case WIZ_NPC_EVENT:
NpcEvent(pkt);
break;
case WIZ_ITEM_TRADE:
ItemTrade(pkt);
break;
case WIZ_TARGET_HP:
{
uint16_t uid = pkt.read<uint16_t>();
uint8_t echo = pkt.read<uint8_t>();
m_targetID = uid;
SendTargetHP(echo, uid);
}
break;
case WIZ_BUNDLE_OPEN_REQ:
BundleOpenReq(pkt);
break;
case WIZ_ITEM_GET:
ItemGet(pkt);
break;
case WIZ_ZONE_CHANGE:
RecvZoneChange(pkt);
break;
case WIZ_POINT_CHANGE:
PointChange(pkt);
break;
case WIZ_STATE_CHANGE:
StateChange(pkt);
break;
case WIZ_PARTY:
PartyProcess(pkt);
break;
case WIZ_EXCHANGE:
ExchangeProcess(pkt);
break;
case WIZ_QUEST:
QuestV2PacketProcess(pkt);
break;
case WIZ_MERCHANT:
MerchantProcess(pkt);
break;
case WIZ_MAGIC_PROCESS:
CMagicProcess::MagicPacket(pkt, this);
break;
case WIZ_SKILLPT_CHANGE:
SkillPointChange(pkt);
break;
case WIZ_OBJECT_EVENT:
ObjectEvent(pkt);
break;
case WIZ_TIME:
case WIZ_WEATHER:
UpdateGameWeather(pkt);
break;
case WIZ_CLASS_CHANGE:
ClassChange(pkt);
break;
case WIZ_CONCURRENTUSER:
CountConcurrentUser();
break;
case WIZ_DATASAVE:
UserDataSaveToAgent();
break;
case WIZ_ITEM_REPAIR:
ItemRepair(pkt);
break;
case WIZ_KNIGHTS_PROCESS:
CKnightsManager::PacketProcess(this, pkt);
break;
case WIZ_ITEM_REMOVE:
ItemRemove(pkt);
break;
case WIZ_OPERATOR:
OperatorCommand(pkt);
break;
case WIZ_SPEEDHACK_CHECK:
SpeedHackTime(pkt);
break;
case WIZ_WAREHOUSE:
WarehouseProcess(pkt);
break;
case WIZ_HOME:
Home();
break;
case WIZ_FRIEND_PROCESS:
FriendProcess(pkt);
break;
case WIZ_WARP_LIST:
SelectWarpList(pkt);
break;
case WIZ_VIRTUAL_SERVER:
ServerChangeOk(pkt);
break;
case WIZ_PARTY_BBS:
PartyBBS(pkt);
break;
case WIZ_MAP_EVENT:
break;
case WIZ_CLIENT_EVENT:
ClientEvent(pkt.read<uint16_t>());
break;
case WIZ_SELECT_MSG:
RecvSelectMsg(pkt);
break;
case WIZ_ITEM_UPGRADE:
ItemUpgradeProcess(pkt);
break;
case WIZ_EVENT:
TempleProcess(pkt);
break;
case WIZ_SHOPPING_MALL: // letter system's used in here too
ShoppingMall(pkt);
break;
case WIZ_NAME_CHANGE:
HandleNameChange(pkt);
break;
case WIZ_KING:
CKingSystem::PacketProcess(this, pkt);
break;
case WIZ_HELMET:
HandleHelmet(pkt);
break;
case WIZ_CAPE:
HandleCapeChange(pkt);
break;
case WIZ_CHALLENGE:
HandleChallenge(pkt);
break;
case WIZ_RANK:
HandlePlayerRankings(pkt);
break;
case WIZ_MINING:
HandleMiningSystem(pkt);
break;
case WIZ_SIEGE:
SiegeWarFareNpc(pkt);
break;
case WIZ_LOGOSSHOUT:
LogosShout(pkt);
default:
TRACE("[SID=%d] Unknown packet %X\n", GetSocketID(), command);
return false;
}
Update();
return true;
}
/**
* @brief Updates timed player data, e.g. skills & save requests.
*/
void CUser::Update()
{
if (m_tGameStartTimeSavedMagic != 0 && (UNIXTIME - m_tGameStartTimeSavedMagic) >= 2)
{
m_tGameStartTimeSavedMagic = 0;
// Restore scrolls...
InitType4();
RecastSavedMagic();
}
if (!isBlinking() && m_tHPLastTimeNormal != 0 && (UNIXTIME - m_tHPLastTimeNormal) > m_bHPIntervalNormal)
HPTimeChange(); // For Sitdown/Standup HP restoration.
// Handles DOT/HOT skills (not COLD skills though.)
if (m_bType3Flag)
HPTimeChangeType3();
// Check for expired type 4 buffs
Type4Duration();
// Expire any timed out saved skills.
CheckSavedMagic();
if (isTransformed())
CMagicProcess::CheckExpiredType6Skills(this);
// Check for expired type 9/visibility skills
CMagicProcess::CheckExpiredType9Skills(this);
if (isBlinking()) // Should you stop blinking?
BlinkTimeCheck();
if (hasRival() && hasRivalryExpired())
RemoveRival();
if ((UNIXTIME - m_lastSaveTime) >= PLAYER_SAVE_INTERVAL)
{
m_lastSaveTime = UNIXTIME; // this is set by UpdateUser(), however it may result in multiple requests unless it's set first.
UserDataSaveToAgent();
}
for (int i = 0; i < WAREHOUSE_MAX; i++)
{
_ITEM_DATA *pItem = &m_sWarehouseArray[i];
if(pItem == nullptr)
continue;
if (pItem->nExpirationTime < (uint32_t)UNIXTIME && pItem->nExpirationTime != 0)
memset(pItem, 0, sizeof(_ITEM_DATA));
}
for (int i = 0; i < INVENTORY_TOTAL; i++)
{
_ITEM_DATA *pItem = &m_sItemArray[i];
if(pItem == nullptr)
continue;
if (pItem->nExpirationTime < (uint32_t)UNIXTIME && pItem->nExpirationTime != 0)
memset(pItem, 0, sizeof(_ITEM_DATA));
}
}
void CUser::SetRival(CUser * pRival)
{
if (pRival == nullptr
|| hasRival())
return;
Packet result(WIZ_PVP, uint8_t(PVPAssignRival));
CKnights * pKnights = nullptr;
result << pRival->GetID()
<< GetCoins() << GetLoyalty();
if (pRival->isInClan()
&& (pKnights = g_pMain->GetClanPtr(pRival->GetClanID())))
result << pKnights->GetName();
else
result << uint16_t(0); // 0 length clan name;
result << pRival->GetName();
m_sRivalID = pRival->GetID();
m_tRivalExpiryTime = UNIXTIME + RIVALRY_DURATION;
Send(&result);
}
/**
* @brief Removes our rivalry state.
*/
void CUser::RemoveRival()
{
if (!hasRival())
return;
// Reset our rival data
m_tRivalExpiryTime = 0;
m_sRivalID = -1;
// Send the packet to let the client know that our rivalry has ended
Packet result(WIZ_PVP);
result << uint8_t(PVPRemoveRival);
Send(&result);
}
/**
* @brief Adjusts a player's loyalty (NP) and sends the loyalty
* change packet.
*
* @param nChangeAmount The amount to adjust the loyalty points by.
* @param bIsKillReward When set to true, enables the use of NP-modifying buffs
* and includes monthly NP gains.
*/
void CUser::SendLoyaltyChange(int32_t nChangeAmount /*= 0*/, bool bIsKillReward /* false */, bool bIsBonusReward /* false */, bool bIsAddLoyaltyMonthly /* true */)
{
Packet result(WIZ_LOYALTY_CHANGE);
uint32_t nClanLoyaltyAmount = 0;
int32_t nChangeAmountLoyaltyMonthly = nChangeAmount;
// If we're taking NP, we need to prevent us from hitting values below 0.
if (nChangeAmount < 0)
{
// Negate the value so it becomes positive (i.e. -50 -> 50)
// so we can determine if we're trying to take more NP than we have.
uint32_t amt = -nChangeAmount; /* avoids unsigned/signed comparison warning */
if (amt > m_iLoyalty)
m_iLoyalty = 0;
else
m_iLoyalty += nChangeAmount;
// We should only adjust monthly NP when NP was lost when killing a player.
if (bIsKillReward)
{
if (GetZoneID() == ZONE_ARDREAM || GetZoneID() == ZONE_RONARK_LAND_BASE)
bIsAddLoyaltyMonthly = false;
if (bIsAddLoyaltyMonthly)
{
if (nChangeAmountLoyaltyMonthly > 40)
nChangeAmountLoyaltyMonthly += 20;
else if (nChangeAmountLoyaltyMonthly >= 20 && nChangeAmountLoyaltyMonthly < 40)
nChangeAmountLoyaltyMonthly += 10;
if (amt > m_iLoyaltyMonthly)
m_iLoyaltyMonthly = 0;
else
m_iLoyaltyMonthly += nChangeAmountLoyaltyMonthly;
}
}
}
// We're simply adding NP here.
else
{
// If you're using an NP modifying buff then add the bonus
nChangeAmount = m_bNPGainAmount * nChangeAmount / 100;
// Add on any additional NP earned because of a global NP event.
// NOTE: They officially check to see if the NP is <= 100,000.
nChangeAmount = nChangeAmount * (100 + g_pMain->m_byNPEventAmount) / 100;
// We should only apply NP bonuses when NP was gained as a reward for killing a player.
if (bIsKillReward)
{
// Add on any additional NP gained from items/skills.
nChangeAmount += m_bItemNPBonus + m_bSkillNPBonus;
// Add monument bonus.
if (isInPKZone() && GetPVPMonumentNation() == GetNation())
nChangeAmount += PVP_MONUMENT_NP_BONUS;
}
if (m_iLoyalty + nChangeAmount > LOYALTY_MAX)
m_iLoyalty = LOYALTY_MAX;
else
m_iLoyalty += nChangeAmount;
if (isInPKZone() && !bIsBonusReward)
{
if (GetZoneID() == ZONE_ARDREAM || GetZoneID() == ZONE_RONARK_LAND_BASE)
bIsAddLoyaltyMonthly = false;
m_iLoyaltyDaily += nChangeAmount;
UpdatePlayerRank();
}
//// We should only apply additional monthly NP when NP was gained as a reward for killing a player.
if (!bIsBonusReward)
{
if (bIsAddLoyaltyMonthly)
{
if (nChangeAmountLoyaltyMonthly > 40)
nChangeAmountLoyaltyMonthly -= 20;
else if (nChangeAmountLoyaltyMonthly >= 20 && nChangeAmountLoyaltyMonthly < 40)
nChangeAmountLoyaltyMonthly -= 10;
if (m_iLoyaltyMonthly + nChangeAmountLoyaltyMonthly > LOYALTY_MAX)
m_iLoyaltyMonthly = LOYALTY_MAX;
else
m_iLoyaltyMonthly += nChangeAmountLoyaltyMonthly;
}
}
if (bIsKillReward)
{
if (GetPremiumProperty(PremiumBonusLoyalty) > 0)
{
m_iLoyalty += GetPremiumProperty(PremiumBonusLoyalty);
if (bIsAddLoyaltyMonthly)
m_iLoyaltyMonthly += GetPremiumProperty(PremiumBonusLoyalty);
m_iLoyaltyPremiumBonus += GetPremiumProperty(PremiumBonusLoyalty);
}
}
CKnights * pKnights = g_pMain->GetClanPtr(GetClanID());
if (pKnights && pKnights->m_byFlag >= ClanTypeAccredited5 && pKnights->GetClanPointMethod() == 0 && !bIsBonusReward)
{
if (pKnights->m_sMembers > 0 && pKnights->m_sMembers <= MAX_CLAN_USERS)
{
if (pKnights->m_sMembers <= 5)
nClanLoyaltyAmount = 1;
else if (pKnights->m_sMembers <= 10)
nClanLoyaltyAmount = 2;
else if (pKnights->m_sMembers <= 15)
nClanLoyaltyAmount = 3;
else if (pKnights->m_sMembers <= 20)
nClanLoyaltyAmount = 4;
else if (pKnights->m_sMembers <= 25)
nClanLoyaltyAmount = 5;
else if (pKnights->m_sMembers <= 30)
nClanLoyaltyAmount = 6;
else if (pKnights->m_sMembers > 30)
nClanLoyaltyAmount = 7;
m_iLoyalty -= nClanLoyaltyAmount;
CKnightsManager::AddUserDonatedNP(GetClanID(), m_strUserID, nClanLoyaltyAmount, true);
}
}
}
result << uint8_t(LOYALTY_NATIONAL_POINTS)
<< m_iLoyalty << m_iLoyaltyMonthly
<< uint32_t(0) // Clan donations(? Donations made by this user? For the clan overall?)
<< nClanLoyaltyAmount; // Premium NP(? Additional NP gained?)
Send(&result);
// Player is give first np, second exp and third meat dumpling etc.
if (bIsKillReward && nChangeAmount > 0)
{
if (isInPKZone() || GetMap()->isWarZone())
ExpChange(PVP_BONUS_EXP, true);
// Additionally, we should receive a "Meat dumpling"
if (isInPKZone())
GiveItem(ITEM_MEAT_DUMPLING);
}
}
/**
* @brief Get a player loyalty reward.
*
* @param isMonthly Monthly reward.
*/
uint8_t CUser::GetRankReward(bool isMonthly)
{
enum RankErrorCodes
{
NoRank = 0,
RewardSuccessfull = 1,
RewardAlreadyTaken = 2
};
int8_t nRank = -1;
int32_t nGoldAmount = 0;
Guard lock(g_pMain->m_userRankingsLock);
string strUserID = GetName();
STRTOUPPER(strUserID);
UserNameRankMap::iterator itr;
if (isMonthly)
{
itr = g_pMain->m_UserPersonalRankMap.find(strUserID);
nRank = itr != g_pMain->m_UserPersonalRankMap.end() ? int8_t(itr->second->nRank) : -1;
} else {
itr = g_pMain->m_UserKnightsRankMap.find(strUserID);
nRank = itr != g_pMain->m_UserKnightsRankMap.end() ? int8_t(itr->second->nRank) : -1;
}
nRank = 1;
if (nRank > 0 && nRank <= 100)
{
if (nRank == 1)
nGoldAmount = 1000000;
else if (nRank >= 2 && nRank <= 4)
nGoldAmount = 700000;
else if (nRank >= 5 && nRank <= 10)
nGoldAmount = 500000;
else if (nRank >= 11 && nRank <= 40)
nGoldAmount = 300000;
else if (nRank >= 41 && nRank <= 100)
nGoldAmount = 200000;
else
nGoldAmount = 0;
if (nGoldAmount > 0)
{
if (GetUserDailyOp(isMonthly ? DAILY_USER_PERSONAL_RANK_REWARD : DAILY_USER_RANK_REWARD) == 0)
return RewardAlreadyTaken;
GoldGain(nGoldAmount);
return RewardSuccessfull;
}
}
return NoRank;
}
/**
* @brief Changes a player's fame.
*
* @param bFame The fame.
*/
void CUser::ChangeFame(uint8_t bFame)
{
Packet result(WIZ_AUTHORITY_CHANGE);
m_bFame = bFame;
result << uint8_t(COMMAND_AUTHORITY) << GetSocketID() << GetFame();
SendToRegion(&result);
}
/**
* @brief Sends the server index.
*/
void CUser::SendServerIndex()
{
Packet result(WIZ_SERVER_INDEX);
result << uint16_t(1) << uint16_t(g_pMain->m_nServerNo);
Send(&result);
}
/**
* @brief Packet handler for skillbar requests.
*
* @param pkt The packet.
*/
void CUser::SkillDataProcess(Packet & pkt)
{
uint8_t opcode = pkt.read<uint8_t>();
switch (opcode)
{
case SKILL_DATA_SAVE:
SkillDataSave(pkt);
break;
case SKILL_DATA_LOAD:
SkillDataLoad();
break;
}
}
/**
* @brief Packet handler for saving a skillbar.
*
* @param pkt The packet.
*/
void CUser::SkillDataSave(Packet & pkt)
{
uint16_t sCount = pkt.read<uint16_t>();
if (sCount == 0 || sCount > 64)
return;
Packet result(WIZ_SKILLDATA);
result << uint8_t(SKILL_DATA_SAVE) << sCount;
for (int i = 0; i < sCount; i++)
result << pkt.read<uint32_t>();
g_pMain->AddDatabaseRequest(result, this);
}
/**
* @brief Packet handler for loading a skillbar.
*/
void CUser::SkillDataLoad()
{
Packet result(WIZ_SKILLDATA);
result << uint8_t(SKILL_DATA_LOAD);
g_pMain->AddDatabaseRequest(result, this);
}
/**
* @brief Initiates a database request to save the character's information.
*/
void CUser::UserDataSaveToAgent()
{
if (!isInGame())
return;
Packet result(WIZ_DATASAVE);
result << GetAccountName() << GetName();
g_pMain->AddDatabaseRequest(result, this);
}
/**
* @brief Logs a player out.
*/
void CUser::LogOut()
{
if (m_strUserID.empty())
return;
Packet result(AG_USER_LOG_OUT);
result << GetID() << GetName();
Send_AIServer(&result);
result.Initialize(WIZ_LOGOUT);
m_deleted = true; // make this session unusable until the logout is complete
g_pMain->AddDatabaseRequest(result, this);
}
/**
* @brief Sends the player's information on initial login.
*/
void CUser::SendMyInfo()
{
C3DMap* pMap = GetMap();
CKnights* pKnights = nullptr;
if (!pMap->IsValidPosition(GetX(), GetZ(), 0.0f))
{
int16_t x = 0, z = 0;
GetStartPosition(x, z);
m_curx = (float)x;
m_curz = (float)z;
}
QuestDataRequest();
Packet result(WIZ_MYINFO);
// Load up our user rankings (for our NP symbols).
g_pMain->GetUserRank(this);
// Are we the King? Let's see, shall we?
CKingSystem * pData = g_pMain->m_KingSystemArray.GetData(GetNation());
if (pData != nullptr
&& STRCASECMP(pData->m_strKingName.c_str(), m_strUserID.c_str()) == 0)
m_bRank = 1; // We're da King, man.
else
m_bRank = 0; // totally not da King.
result.SByte();
result << GetSocketID()
<< GetName()
<< GetSPosX() << GetSPosZ() << GetSPosY()
<< GetNation()
<< m_bRace << m_sClass << m_bFace
<< m_nHair
<< m_bRank << m_bTitle
<< GetLevel()
<< int8_t(m_sPoints) // NOTE: int16_t to int8_t
<< uint32_t(m_iMaxExp) << uint32_t(m_iExp)
<< GetLoyalty() << GetMonthlyLoyalty()
<< m_bCity << GetClanID() ;
if (isInClan())
pKnights = g_pMain->GetClanPtr(GetClanID());
if (pKnights == nullptr)
{
//result << uint8_t(0) << uint16_t(0) << uint8_t(0) << uint8_t(0);
result << uint64_t(0) << uint8_t(0) << uint16_t(-1);
}
else
{
if (pKnights->m_sAlliance != 0)
{
pKnights->OnLoginAlliance(this);
}
else
pKnights->OnLogin(this);
CKnights *aKnights = g_pMain->GetClanPtr(pKnights->GetAllianceID());
result
<< GetFame()
<< pKnights->GetAllianceID()
<< pKnights->m_byFlag
<< pKnights->m_strName
<< pKnights->m_byGrade << pKnights->m_byRanking
<< uint16_t(pKnights->m_sMarkVersion)
<< pKnights->GetCapeID(aKnights);
/* NOTE(srmeier): 1068 packet
result
<< GetFame()
//<< pKnights->GetAllianceID()
//<< pKnights->m_byFlag
<< pKnights->m_strName
<< pKnights->m_byGrade << pKnights->m_byRanking ;
//<< uint16_t(pKnights->m_sMarkVersion)
//<< pKnights->GetCapeID(aKnights);