/
protocolgame.cpp
3078 lines (2653 loc) · 73.9 KB
/
protocolgame.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
////////////////////////////////////////////////////////////////////////
// OpenTibia - an opensource roleplaying game
////////////////////////////////////////////////////////////////////////
// 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 3 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, see <http://www.gnu.org/licenses/>.
////////////////////////////////////////////////////////////////////////
#include "otpch.h"
#include "resources.h"
#include <boost/function.hpp>
#include <iostream>
#include "protocolgame.h"
#include "textlogger.h"
#include "waitlist.h"
#include "player.h"
#include "connection.h"
#include "networkmessage.h"
#include "outputmessage.h"
#include "iologindata.h"
#include "ioban.h"
#include "items.h"
#include "tile.h"
#include "house.h"
#include "actions.h"
#include "creatureevent.h"
#include "quests.h"
#include "chat.h"
#include "configmanager.h"
#include "game.h"
#if defined(WINDOWS) && !defined(__CONSOLE__)
#include "gui.h"
#endif
extern Game g_game;
extern ConfigManager g_config;
extern Actions actions;
extern CreatureEvents* g_creatureEvents;
extern Chat g_chat;
template<class FunctionType>
void ProtocolGame::addGameTaskInternal(uint32_t delay, const FunctionType& func)
{
if(delay > 0)
Dispatcher::getInstance().addTask(createTask(delay, func));
else
Dispatcher::getInstance().addTask(createTask(func));
}
#ifdef __ENABLE_SERVER_DIAGNOSTIC__
uint32_t ProtocolGame::protocolGameCount = 0;
#endif
void ProtocolGame::setPlayer(Player* p)
{
player = p;
}
void ProtocolGame::releaseProtocol()
{
if(player && player->client == this)
player->client = NULL;
Protocol::releaseProtocol();
}
void ProtocolGame::deleteProtocolTask()
{
if(player)
{
g_game.freeThing(player);
player = NULL;
}
Protocol::deleteProtocolTask();
}
bool ProtocolGame::login(const std::string& name, uint32_t id, const std::string& password,
OperatingSystem_t operatingSystem, uint16_t version, bool gamemaster)
{
//dispatcher thread
PlayerVector players = g_game.getPlayersByName(name);
Player* _player = NULL;
if(!players.empty())
_player = players[random_range(0, (players.size() - 1))];
if(!_player || name == "Account Manager" || g_config.getNumber(ConfigManager::ALLOW_CLONES) > (int32_t)players.size())
{
player = new Player(name, this);
player->addRef();
player->setID();
if(!IOLoginData::getInstance()->loadPlayer(player, name, true))
{
disconnectClient(0x14, "Your character could not be loaded.");
return false;
}
Ban ban;
ban.value = player->getID();
ban.param = PLAYERBAN_BANISHMENT;
ban.type = BAN_PLAYER;
if(IOBan::getInstance()->getData(ban) && !player->hasFlag(PlayerFlag_CannotBeBanned))
{
bool deletion = ban.expires < 0;
std::string name_ = "Automatic ";
if(!ban.adminId)
name_ += (deletion ? "deletion" : "banishment");
else
IOLoginData::getInstance()->getNameByGuid(ban.adminId, name_, true);
char buffer[500 + ban.comment.length()];
sprintf(buffer, "Your character has been %s at:\n%s by: %s,\nfor the following reason:\n%s.\nThe action taken was:\n%s.\nThe comment given was:\n%s.\nYour %s%s.",
(deletion ? "deleted" : "banished"), formatDateShort(ban.added).c_str(), name_.c_str(),
getReason(ban.reason).c_str(), getAction(ban.action, false).c_str(), ban.comment.c_str(),
(deletion ? "character won't be undeleted" : "banishment will be lifted at:\n"),
(deletion ? "." : formatDateShort(ban.expires, true).c_str()));
disconnectClient(0x14, buffer);
return false;
}
if(IOBan::getInstance()->isPlayerBanished(player->getGUID(), PLAYERBAN_LOCK) && id != 1)
{
if(g_config.getBool(ConfigManager::NAMELOCK_MANAGER))
{
player->name = "Account Manager";
player->accountManager = MANAGER_NAMELOCK;
player->managerNumber = id;
player->managerString2 = name;
}
else
{
disconnectClient(0x14, "Your character has been namelocked.");
return false;
}
}
else if(player->getName() == "Account Manager" && g_config.getBool(ConfigManager::ACCOUNT_MANAGER))
{
if(id != 1)
{
player->accountManager = MANAGER_ACCOUNT;
player->managerNumber = id;
}
else
player->accountManager = MANAGER_NEW;
}
if(gamemaster && !player->hasCustomFlag(PlayerCustomFlag_GamemasterPrivileges))
{
disconnectClient(0x14, "You are not a gamemaster! Turn off the gamemaster mode in your IP changer.");
return false;
}
if(!player->hasFlag(PlayerFlag_CanAlwaysLogin))
{
if(g_game.getGameState() == GAME_STATE_CLOSING)
{
disconnectClient(0x14, "Gameworld is just going down, please come back later.");
return false;
}
if(g_game.getGameState() == GAME_STATE_CLOSED)
{
disconnectClient(0x14, "Gameworld is currently closed, please come back later.");
return false;
}
}
if(g_config.getBool(ConfigManager::ONE_PLAYER_ON_ACCOUNT) && !player->isAccountManager() &&
!IOLoginData::getInstance()->hasCustomFlag(id, PlayerCustomFlag_CanLoginMultipleCharacters))
{
bool found = false;
PlayerVector tmp = g_game.getPlayersByAccount(id);
for(PlayerVector::iterator it = tmp.begin(); it != tmp.end(); ++it)
{
if((*it)->getName() != name)
continue;
found = true;
break;
}
if(tmp.size() > 0 && !found)
{
disconnectClient(0x14, "You may only login with one character\nof your account at the same time.");
return false;
}
}
if(!WaitingList::getInstance()->login(player))
{
if(OutputMessage_ptr output = OutputMessagePool::getInstance()->getOutputMessage(this, false))
{
TRACK_MESSAGE(output);
std::stringstream ss;
ss << "Too many players online.\n" << "You are ";
int32_t slot = WaitingList::getInstance()->getSlot(player);
if(slot)
{
ss << "at ";
if(slot > 0)
ss << slot;
else
ss << "unknown";
ss << " place on the waiting list.";
}
else
ss << "awaiting connection...";
output->AddByte(0x16);
output->AddString(ss.str());
output->AddByte(WaitingList::getTime(slot));
OutputMessagePool::getInstance()->send(output);
}
getConnection()->close();
return false;
}
if(!IOLoginData::getInstance()->loadPlayer(player, name))
{
disconnectClient(0x14, "Your character could not be loaded.");
return false;
}
player->setOperatingSystem(operatingSystem);
player->setClientVersion(version);
if(!g_game.placeCreature(player, player->getLoginPosition()) && !g_game.placeCreature(player, player->getMasterPosition(), false, true))
{
disconnectClient(0x14, "Temple position is wrong. Contact with the administration.");
return false;
}
player->lastIP = player->getIP();
player->lastLoad = OTSYS_TIME();
player->lastLogin = std::max(time(NULL), player->lastLogin + 1);
m_acceptPackets = true;
return true;
}
else if(_player->client)
{
if(m_eventConnect || !g_config.getBool(ConfigManager::REPLACE_KICK_ON_LOGIN))
{
//A task has already been scheduled just bail out (should not be overriden)
disconnectClient(0x14, "You are already logged in.");
return false;
}
g_chat.removeUserFromAllChannels(_player);
_player->disconnect();
_player->isConnecting = true;
addRef();
m_eventConnect = Scheduler::getInstance().addEvent(createSchedulerTask(
1000, boost::bind(&ProtocolGame::connect, this, _player->getID(), operatingSystem, version)));
return true;
}
addRef();
return connect(_player->getID(), operatingSystem, version);
}
bool ProtocolGame::logout(bool displayEffect, bool forceLogout)
{
//dispatcher thread
if(!player)
return false;
if(!player->isRemoved())
{
if(!forceLogout)
{
if(!IOLoginData::getInstance()->hasCustomFlag(player->getAccount(), PlayerCustomFlag_CanLogoutAnytime))
{
if(player->getTile()->hasFlag(TILESTATE_NOLOGOUT))
{
player->sendCancelMessage(RET_YOUCANNOTLOGOUTHERE);
return false;
}
if(player->hasCondition(CONDITION_INFIGHT))
{
player->sendCancelMessage(RET_YOUMAYNOTLOGOUTDURINGAFIGHT);
return false;
}
if(!g_creatureEvents->playerLogout(player, false)) //let the script handle the error message
return false;
}
else
g_creatureEvents->playerLogout(player, false);
}
else if(!g_creatureEvents->playerLogout(player, true))
return false;
}
else
displayEffect = false;
if(displayEffect && !player->isGhost())
g_game.addMagicEffect(player->getPosition(), MAGIC_EFFECT_POFF);
if(Connection_ptr connection = getConnection())
connection->close();
return g_game.removeCreature(player);
}
bool ProtocolGame::connect(uint32_t playerId, OperatingSystem_t operatingSystem, uint16_t version)
{
unRef();
m_eventConnect = 0;
Player* _player = g_game.getPlayerByID(playerId);
if(!_player || _player->isRemoved() || _player->client)
{
disconnectClient(0x14, "You are already logged in.");
return false;
}
player = _player;
player->addRef();
player->isConnecting = false;
player->client = this;
player->sendCreatureAppear(player);
player->setOperatingSystem(operatingSystem);
player->setClientVersion(version);
player->lastIP = player->getIP();
player->lastLoad = OTSYS_TIME();
player->lastLogin = std::max(time(NULL), player->lastLogin + 1);
m_acceptPackets = true;
return true;
}
void ProtocolGame::disconnect()
{
if(getConnection())
getConnection()->close();
}
void ProtocolGame::disconnectClient(uint8_t error, const char* message)
{
if(OutputMessage_ptr output = OutputMessagePool::getInstance()->getOutputMessage(this, false))
{
TRACK_MESSAGE(output);
output->AddByte(error);
output->AddString(message);
OutputMessagePool::getInstance()->send(output);
}
disconnect();
}
void ProtocolGame::onConnect()
{
if(OutputMessage_ptr output = OutputMessagePool::getInstance()->getOutputMessage(this, false))
{
TRACK_MESSAGE(output);
enableChecksum();
output->AddByte(0x1F);
output->AddU16(random_range(0, 0xFFFF));
output->AddU16(0x00);
output->AddByte(random_range(0, 0xFF));
OutputMessagePool::getInstance()->send(output);
}
}
void ProtocolGame::onRecvFirstMessage(NetworkMessage& msg)
{
parseFirstPacket(msg);
}
bool ProtocolGame::parseFirstPacket(NetworkMessage& msg)
{
if(
#if defined(WINDOWS) && !defined(__CONSOLE__)
!GUI::getInstance()->m_connections ||
#endif
g_game.getGameState() == GAME_STATE_SHUTDOWN)
{
getConnection()->close();
return false;
}
OperatingSystem_t operatingSystem = (OperatingSystem_t)msg.GetU16();
uint16_t version = msg.GetU16();
if(!RSA_decrypt(msg))
{
getConnection()->close();
return false;
}
uint32_t key[4] = {msg.GetU32(), msg.GetU32(), msg.GetU32(), msg.GetU32()};
enableXTEAEncryption();
setXTEAKey(key);
bool gamemaster = msg.GetByte();
std::string name = msg.GetString(), character = msg.GetString(), password = msg.GetString();
msg.SkipBytes(6); //841- wtf?
if(version < CLIENT_VERSION_MIN || version > CLIENT_VERSION_MAX)
{
disconnectClient(0x14, CLIENT_VERSION_STRING);
return false;
}
if(name.empty())
{
if(!g_config.getBool(ConfigManager::ACCOUNT_MANAGER))
{
disconnectClient(0x14, "Invalid account name.");
return false;
}
name = "1";
password = "1";
}
if(g_game.getGameState() < GAME_STATE_NORMAL)
{
disconnectClient(0x14, "Gameworld is just starting up, please wait.");
return false;
}
if(g_game.getGameState() == GAME_STATE_MAINTAIN)
{
disconnectClient(0x14, "Gameworld is under maintenance, please re-connect in a while.");
return false;
}
if(ConnectionManager::getInstance()->isDisabled(getIP(), protocolId))
{
disconnectClient(0x14, "Too many connections attempts from your IP address, please try again later.");
return false;
}
if(IOBan::getInstance()->isIpBanished(getIP()))
{
disconnectClient(0x14, "Your IP is banished!");
return false;
}
uint32_t id = 1;
if(!IOLoginData::getInstance()->getAccountId(name, id))
{
ConnectionManager::getInstance()->addAttempt(getIP(), protocolId, false);
disconnectClient(0x14, "Invalid account name.");
return false;
}
std::string hash;
if(!IOLoginData::getInstance()->getPassword(id, hash, character) || !encryptTest(password, hash))
{
ConnectionManager::getInstance()->addAttempt(getIP(), protocolId, false);
disconnectClient(0x14, "Invalid password.");
return false;
}
Ban ban;
ban.value = id;
ban.type = BAN_ACCOUNT;
if(IOBan::getInstance()->getData(ban) && !IOLoginData::getInstance()->hasFlag(id, PlayerFlag_CannotBeBanned))
{
bool deletion = ban.expires < 0;
std::string name_ = "Automatic ";
if(!ban.adminId)
name_ += (deletion ? "deletion" : "banishment");
else
IOLoginData::getInstance()->getNameByGuid(ban.adminId, name_, true);
char buffer[500 + ban.comment.length()];
sprintf(buffer, "Your account has been %s at:\n%s by: %s,\nfor the following reason:\n%s.\nThe action taken was:\n%s.\nThe comment given was:\n%s.\nYour %s%s.",
(deletion ? "deleted" : "banished"), formatDateShort(ban.added).c_str(), name_.c_str(),
getReason(ban.reason).c_str(), getAction(ban.action, false).c_str(), ban.comment.c_str(),
(deletion ? "account won't be undeleted" : "banishment will be lifted at:\n"),
(deletion ? "." : formatDateShort(ban.expires, true).c_str()));
disconnectClient(0x14, buffer);
return false;
}
ConnectionManager::getInstance()->addAttempt(getIP(), protocolId, true);
Dispatcher::getInstance().addTask(createTask(boost::bind(
&ProtocolGame::login, this, character, id, password, operatingSystem, version, gamemaster)));
return true;
}
void ProtocolGame::parsePacket(NetworkMessage &msg)
{
if(!player || !m_acceptPackets || g_game.getGameState() == GAME_STATE_SHUTDOWN
|| msg.getMessageLength() <= 0)
return;
uint8_t recvbyte = msg.GetByte();
//a dead player cannot performs actions
if(player->isRemoved() && recvbyte != 0x14)
return;
if(player->isAccountManager())
{
switch(recvbyte)
{
case 0x14:
parseLogout(msg);
break;
case 0x96:
parseSay(msg);
break;
default:
sendCancelWalk();
break;
}
}
else
{
switch(recvbyte)
{
case 0x14: // logout
parseLogout(msg);
break;
case 0x1E: // keep alive / ping response
parseReceivePing(msg);
break;
case 0x64: // move with steps
parseAutoWalk(msg);
break;
case 0x65: // move north
case 0x66: // move east
case 0x67: // move south
case 0x68: // move west
parseMove(msg, (Direction)(recvbyte - 0x65));
break;
case 0x69: // stop-autowalk
addGameTask(&Game::playerStopAutoWalk, player->getID());
break;
case 0x6A:
parseMove(msg, NORTHEAST);
break;
case 0x6B:
parseMove(msg, SOUTHEAST);
break;
case 0x6C:
parseMove(msg, SOUTHWEST);
break;
case 0x6D:
parseMove(msg, NORTHWEST);
break;
case 0x6F: // turn north
case 0x70: // turn east
case 0x71: // turn south
case 0x72: // turn west
parseTurn(msg, (Direction)(recvbyte - 0x6F));
break;
case 0x78: // throw item
parseThrow(msg);
break;
case 0x79: // description in shop window
parseLookInShop(msg);
break;
case 0x7A: // player bought from shop
parsePlayerPurchase(msg);
break;
case 0x7B: // player sold to shop
parsePlayerSale(msg);
break;
case 0x7C: // player closed shop window
parseCloseShop(msg);
break;
case 0x7D: // Request trade
parseRequestTrade(msg);
break;
case 0x7E: // Look at an item in trade
parseLookInTrade(msg);
break;
case 0x7F: // Accept trade
parseAcceptTrade(msg);
break;
case 0x80: // close/cancel trade
parseCloseTrade();
break;
case 0x82: // use item
parseUseItem(msg);
break;
case 0x83: // use item
parseUseItemEx(msg);
break;
case 0x84: // battle window
parseBattleWindow(msg);
break;
case 0x85: //rotate item
parseRotateItem(msg);
break;
case 0x87: // close container
parseCloseContainer(msg);
break;
case 0x88: //"up-arrow" - container
parseUpArrowContainer(msg);
break;
case 0x89:
parseTextWindow(msg);
break;
case 0x8A:
parseHouseWindow(msg);
break;
case 0x8C: // throw item
parseLookAt(msg);
break;
case 0x96: // say something
parseSay(msg);
break;
case 0x97: // request channels
parseGetChannels(msg);
break;
case 0x98: // open channel
parseOpenChannel(msg);
break;
case 0x99: // close channel
parseCloseChannel(msg);
break;
case 0x9A: // open priv
parseOpenPriv(msg);
break;
case 0x9B: //process report
parseProcessRuleViolation(msg);
break;
case 0x9C: //gm closes report
parseCloseRuleViolation(msg);
break;
case 0x9D: //player cancels report
parseCancelRuleViolation(msg);
break;
case 0x9E: // close NPC
parseCloseNpc(msg);
break;
case 0xA0: // set attack and follow mode
parseFightModes(msg);
break;
case 0xA1: // attack
parseAttack(msg);
break;
case 0xA2: //follow
parseFollow(msg);
break;
case 0xA3: // invite party
parseInviteToParty(msg);
break;
case 0xA4: // join party
parseJoinParty(msg);
break;
case 0xA5: // revoke party
parseRevokePartyInvite(msg);
break;
case 0xA6: // pass leadership
parsePassPartyLeadership(msg);
break;
case 0xA7: // leave party
parseLeaveParty(msg);
break;
case 0xA8: // share exp
parseSharePartyExperience(msg);
break;
case 0xAA:
parseCreatePrivateChannel(msg);
break;
case 0xAB:
parseChannelInvite(msg);
break;
case 0xAC:
parseChannelExclude(msg);
break;
case 0xBE: // cancel move
parseCancelMove(msg);
break;
case 0xC9: //client request to resend the tile
parseUpdateTile(msg);
break;
case 0xCA: //client request to resend the container (happens when you store more than container maxsize)
parseUpdateContainer(msg);
break;
case 0xD2: // request outfit
if((!player->hasCustomFlag(PlayerCustomFlag_GamemasterPrivileges) || !g_config.getBool(
ConfigManager::DISABLE_OUTFITS_PRIVILEGED)) && (g_config.getBool(ConfigManager::ALLOW_CHANGEOUTFIT)
|| g_config.getBool(ConfigManager::ALLOW_CHANGECOLORS) || g_config.getBool(ConfigManager::ALLOW_CHANGEADDONS)))
parseRequestOutfit(msg);
break;
case 0xD3: // set outfit
if((!player->hasCustomFlag(PlayerCustomFlag_GamemasterPrivileges) || !g_config.getBool(ConfigManager::DISABLE_OUTFITS_PRIVILEGED))
&& (g_config.getBool(ConfigManager::ALLOW_CHANGECOLORS) || g_config.getBool(ConfigManager::ALLOW_CHANGEOUTFIT)))
parseSetOutfit(msg);
break;
case 0xDC:
parseAddVip(msg);
break;
case 0xDD:
parseRemoveVip(msg);
break;
case 0xE6:
parseBugReport(msg);
break;
case 0xE7:
parseViolationWindow(msg);
break;
case 0xE8:
parseDebugAssert(msg);
break;
case 0xF0:
parseQuests(msg);
break;
case 0xF1:
parseQuestInfo(msg);
break;
default:
{
if(g_config.getBool(ConfigManager::BAN_UNKNOWN_BYTES))
{
int64_t banTime = -1;
ViolationAction_t action = ACTION_BANISHMENT;
Account tmp = IOLoginData::getInstance()->loadAccount(player->getAccount(), true);
tmp.warnings++;
if(tmp.warnings >= g_config.getNumber(ConfigManager::WARNINGS_TO_DELETION))
action = ACTION_DELETION;
else if(tmp.warnings >= g_config.getNumber(ConfigManager::WARNINGS_TO_FINALBAN))
{
banTime = time(NULL) + g_config.getNumber(ConfigManager::FINALBAN_LENGTH);
action = ACTION_BANFINAL;
}
else
banTime = time(NULL) + g_config.getNumber(ConfigManager::BAN_LENGTH);
if(IOBan::getInstance()->addAccountBanishment(tmp.number, banTime, 13, action,
"Sending unknown packets to the server.", 0, player->getGUID()))
{
IOLoginData::getInstance()->saveAccount(tmp);
player->sendTextMessage(MSG_INFO_DESCR, "You have been banished.");
g_game.addMagicEffect(player->getPosition(), MAGIC_EFFECT_WRAPS_GREEN);
Scheduler::getInstance().addEvent(createSchedulerTask(1000, boost::bind(
&Game::kickPlayer, &g_game, player->getID(), false)));
}
}
std::stringstream hex, s;
hex << "0x" << std::hex << (int16_t)recvbyte << std::dec;
s << player->getName() << " sent unknown byte: " << hex << std::endl;
LOG_MESSAGE(LOGTYPE_NOTICE, s.str(), "PLAYER")
Logger::getInstance()->eFile(getFilePath(FILE_TYPE_LOG, "bots/" + player->getName() + ".log").c_str(),
"[" + formatDate() + "] Received byte " + hex.str(), false);
break;
}
}
}
}
void ProtocolGame::GetTileDescription(const Tile* tile, NetworkMessage_ptr msg)
{
if(!tile)
return;
int32_t count = 0;
if(tile->ground)
{
msg->AddItem(tile->ground);
count++;
}
const TileItemVector* items = tile->getItemList();
const CreatureVector* creatures = tile->getCreatures();
ItemVector::const_iterator it;
if(items)
{
for(it = items->getBeginTopItem(); (it != items->getEndTopItem() && count < 10); ++it, ++count)
msg->AddItem(*it);
}
if(creatures)
{
for(CreatureVector::const_reverse_iterator cit = creatures->rbegin(); (cit != creatures->rend() && count < 10); ++cit)
{
if(!player->canSeeCreature(*cit))
continue;
bool known;
uint32_t removedKnown;
checkCreatureAsKnown((*cit)->getID(), known, removedKnown);
AddCreature(msg, (*cit), known, removedKnown);
count++;
}
}
if(items)
{
for(it = items->getBeginDownItem(); (it != items->getEndDownItem() && count < 10); ++it, ++count)
msg->AddItem(*it);
}
}
void ProtocolGame::GetMapDescription(int32_t x, int32_t y, int32_t z,
int32_t width, int32_t height, NetworkMessage_ptr msg)
{
int32_t skip = -1, startz, endz, zstep = 0;
if(z > 7)
{
startz = z - 2;
endz = std::min((int32_t)MAP_MAX_LAYERS - 1, z + 2);
zstep = 1;
}
else
{
startz = 7;
endz = 0;
zstep = -1;
}
for(int32_t nz = startz; nz != endz + zstep; nz += zstep)
GetFloorDescription(msg, x, y, nz, width, height, z - nz, skip);
if(skip >= 0)
{
msg->AddByte(skip);
msg->AddByte(0xFF);
//cc += skip;
}
}
void ProtocolGame::GetFloorDescription(NetworkMessage_ptr msg, int32_t x, int32_t y, int32_t z,
int32_t width, int32_t height, int32_t offset, int32_t& skip)
{
Tile* tile = NULL;
for(int32_t nx = 0; nx < width; nx++)
{
for(int32_t ny = 0; ny < height; ny++)
{
if((tile = g_game.getTile(Position(x + nx + offset, y + ny + offset, z))))
{
if(skip >= 0)
{
msg->AddByte(skip);
msg->AddByte(0xFF);
}
skip = 0;
GetTileDescription(tile, msg);
}
else
{
++skip;
if(skip == 0xFF)
{
msg->AddByte(0xFF);
msg->AddByte(0xFF);
skip = -1;
}
}
}
}
}
void ProtocolGame::checkCreatureAsKnown(uint32_t id, bool& known, uint32_t& removedKnown)
{
// loop through the known creature list and check if the given creature is in
for(std::list<uint32_t>::iterator it = knownCreatureList.begin(); it != knownCreatureList.end(); ++it)
{
if((*it) != id)
continue;
// know... make the creature even more known...
knownCreatureList.erase(it);
knownCreatureList.push_back(id);
known = true;
return;
}
// ok, he is unknown...
known = false;
// ... but not in future
knownCreatureList.push_back(id);
// too many known creatures?
if(knownCreatureList.size() > 250)
{
// lets try to remove one from the end of the list
Creature* c = NULL;
for(int32_t n = 0; n < 250; n++)
{
removedKnown = knownCreatureList.front();
if(!(c = g_game.getCreatureByID(removedKnown)) || !canSee(c))
break;
// this creature we can't remove, still in sight, so back to the end
knownCreatureList.pop_front();
knownCreatureList.push_back(removedKnown);
}
// hopefully we found someone to remove :S, we got only 250 tries
// if not... lets kick some players with debug errors :)
knownCreatureList.pop_front();
}
else // we can cache without problems :)
removedKnown = 0;
}
bool ProtocolGame::canSee(const Creature* c) const
{