Advertisement
Guest User

Untitled

a guest
May 24th, 2022
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 92.37 KB | None | 0 0
  1. /**
  2.  * The Forgotten Server - a free and open-source MMORPG server emulator
  3.  * Copyright (C) 2019  Mark Samman <mark.samman@gmail.com>
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License along
  16.  * with this program; if not, write to the Free Software Foundation, Inc.,
  17.  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18.  */
  19.  
  20. #include "otpch.h"
  21.  
  22. #include "protocolgame.h"
  23.  
  24. #include "outputmessage.h"
  25.  
  26. #include "player.h"
  27.  
  28. #include "configmanager.h"
  29. #include "actions.h"
  30. #include "game.h"
  31. #include "iologindata.h"
  32. #include "iomarket.h"
  33. #include "ban.h"
  34. #include "scheduler.h"
  35.  
  36. #include <fmt/format.h>
  37.  
  38. extern ConfigManager g_config;
  39. extern Actions actions;
  40. extern CreatureEvents* g_creatureEvents;
  41. extern Chat* g_chat;
  42.  
  43. namespace {
  44.  
  45. using WaitList = std::deque<std::pair<int64_t, uint32_t>>; // (timeout, player guid)
  46.  
  47. WaitList priorityWaitList, waitList;
  48.  
  49. std::tuple<WaitList&, WaitList::iterator, WaitList::size_type> findClient(const Player& player) {
  50.     const auto fn = [&](const WaitList::value_type& it) { return it.second == player.getGUID(); };
  51.  
  52.     auto it = std::find_if(priorityWaitList.begin(), priorityWaitList.end(), fn);
  53.     if (it != priorityWaitList.end()) {
  54.         return std::make_tuple(std::ref(priorityWaitList), it, std::distance(it, priorityWaitList.end()) + 1);
  55.     }
  56.  
  57.     it = std::find_if(waitList.begin(), waitList.end(), fn);
  58.     if (it != waitList.end()) {
  59.         return std::make_tuple(std::ref(waitList), it, priorityWaitList.size() + std::distance(it, waitList.end()) + 1);
  60.     }
  61.  
  62.     return std::make_tuple(std::ref(waitList), waitList.end(), priorityWaitList.size() + waitList.size());
  63. }
  64.  
  65. uint8_t getWaitTime(std::size_t slot)
  66. {
  67.     if (slot < 5) {
  68.         return 5;
  69.     } else if (slot < 10) {
  70.         return 10;
  71.     } else if (slot < 20) {
  72.         return 20;
  73.     } else if (slot < 50) {
  74.         return 60;
  75.     } else {
  76.         return 120;
  77.     }
  78. }
  79.  
  80. int64_t getTimeout(std::size_t slot)
  81. {
  82.     // timeout is set to 15 seconds longer than expected retry attempt
  83.     return getWaitTime(slot) + 15;
  84. }
  85.  
  86. void cleanupList(WaitList& list)
  87. {
  88.     int64_t time = OTSYS_TIME();
  89.  
  90.     auto it = list.begin();
  91.     while (it != list.end()) {
  92.         if (it->first <= time) {
  93.             it = list.erase(it);
  94.         } else {
  95.             ++it;
  96.         }
  97.     }
  98. }
  99.  
  100. std::size_t clientLogin(const Player& player)
  101. {
  102.     // Currentslot = position in wait list, 0 for direct access
  103.     if (player.hasFlag(PlayerFlag_CanAlwaysLogin) || player.getAccountType() >= ACCOUNT_TYPE_GAMEMASTER) {
  104.         return 0;
  105.     }
  106.  
  107.     cleanupList(priorityWaitList);
  108.     cleanupList(waitList);
  109.  
  110.     uint32_t maxPlayers = static_cast<uint32_t>(g_config.getNumber(ConfigManager::MAX_PLAYERS));
  111.     if (maxPlayers == 0 || (priorityWaitList.empty() && waitList.empty() && g_game.getPlayersOnline() < maxPlayers)) {
  112.         return 0;
  113.     }
  114.  
  115.     auto result = findClient(player);
  116.     if (std::get<1>(result) != std::get<0>(result).end()) {
  117.         auto currentSlot = std::get<2>(result);
  118.         // If server has capacity for this client, let him in even though his current slot might be higher than 0.
  119.         if ((g_game.getPlayersOnline() + currentSlot) <= maxPlayers) {
  120.             std::get<0>(result).erase(std::get<1>(result));
  121.             return 0;
  122.         }
  123.  
  124.         //let them wait a bit longer
  125.         std::get<1>(result)->second = OTSYS_TIME() + (getTimeout(currentSlot) * 1000);
  126.         return currentSlot;
  127.     }
  128.  
  129.     auto currentSlot = priorityWaitList.size();
  130.     if (player.isPremium()) {
  131.         priorityWaitList.emplace_back(OTSYS_TIME() + (getTimeout(++currentSlot) * 1000), player.getGUID());
  132.     } else {
  133.         currentSlot += waitList.size();
  134.         waitList.emplace_back(OTSYS_TIME() + (getTimeout(++currentSlot) * 1000), player.getGUID());
  135.     }
  136.     return currentSlot;
  137. }
  138.  
  139. }
  140.  
  141. void ProtocolGame::release()
  142. {
  143.     //dispatcher thread
  144.     if (player && player->client == shared_from_this()) {
  145.         player->client.reset();
  146.         player->decrementReferenceCounter();
  147.         player = nullptr;
  148.     }
  149.  
  150.     OutputMessagePool::getInstance().removeProtocolFromAutosend(shared_from_this());
  151.     Protocol::release();
  152. }
  153.  
  154. void ProtocolGame::login(const std::string& name, uint32_t accountId, OperatingSystem_t operatingSystem)
  155. {
  156.     //dispatcher thread
  157.     Player* foundPlayer = g_game.getPlayerByName(name);
  158.     if (!foundPlayer || g_config.getBoolean(ConfigManager::ALLOW_CLONES)) {
  159.         player = new Player(getThis());
  160.         player->setName(name);
  161.  
  162.         player->incrementReferenceCounter();
  163.         player->setID();
  164.  
  165.         if (!IOLoginData::preloadPlayer(player, name)) {
  166.             disconnectClient("Your character could not be loaded.");
  167.             return;
  168.         }
  169.  
  170.         if (IOBan::isPlayerNamelocked(player->getGUID())) {
  171.             disconnectClient("Your character has been namelocked.");
  172.             return;
  173.         }
  174.  
  175.         if (g_game.getGameState() == GAME_STATE_CLOSING && !player->hasFlag(PlayerFlag_CanAlwaysLogin)) {
  176.             disconnectClient("The game is just going down.\nPlease try again later.");
  177.             return;
  178.         }
  179.  
  180.         if (g_game.getGameState() == GAME_STATE_CLOSED && !player->hasFlag(PlayerFlag_CanAlwaysLogin)) {
  181.             disconnectClient("Server is currently closed.\nPlease try again later.");
  182.             return;
  183.         }
  184.  
  185.         if (g_config.getBoolean(ConfigManager::ONE_PLAYER_ON_ACCOUNT) && player->getAccountType() < ACCOUNT_TYPE_GAMEMASTER && g_game.getPlayerByAccount(player->getAccount())) {
  186.             disconnectClient("You may only login with one character\nof your account at the same time.");
  187.             return;
  188.         }
  189.  
  190.         if (!player->hasFlag(PlayerFlag_CannotBeBanned)) {
  191.             BanInfo banInfo;
  192.             if (IOBan::isAccountBanned(accountId, banInfo)) {
  193.                 if (banInfo.reason.empty()) {
  194.                     banInfo.reason = "(none)";
  195.                 }
  196.  
  197.                 if (banInfo.expiresAt > 0) {
  198.                     disconnectClient(fmt::format("Your account has been banned until {:s} by {:s}.\n\nReason specified:\n{:s}", formatDateShort(banInfo.expiresAt), banInfo.bannedBy, banInfo.reason));
  199.                 } else {
  200.                     disconnectClient(fmt::format("Your account has been permanently banned by {:s}.\n\nReason specified:\n{:s}", banInfo.bannedBy, banInfo.reason));
  201.                 }
  202.                 return;
  203.             }
  204.         }
  205.  
  206.         if (std::size_t currentSlot = clientLogin(*player)) {
  207.             uint8_t retryTime = getWaitTime(currentSlot);
  208.             auto output = OutputMessagePool::getOutputMessage();
  209.             output->addByte(0x16);
  210.             output->addString(fmt::format("Too many players online.\nYou are at place {:d} on the waiting list.", currentSlot));
  211.             output->addByte(retryTime);
  212.             send(output);
  213.             disconnect();
  214.             return;
  215.         }
  216.  
  217.         if (!IOLoginData::loadPlayerById(player, player->getGUID())) {
  218.             disconnectClient("Your character could not be loaded.");
  219.             return;
  220.         }
  221.  
  222.         player->setOperatingSystem(operatingSystem);
  223.  
  224.         if (!g_game.placeCreature(player, player->getLoginPosition())) {
  225.             if (!g_game.placeCreature(player, player->getTemplePosition(), false, true)) {
  226.                 disconnectClient("Temple position is wrong. Contact the administrator.");
  227.                 return;
  228.             }
  229.         }
  230.  
  231.         if (operatingSystem >= CLIENTOS_OTCLIENT_LINUX) {
  232.             player->registerCreatureEvent("ExtendedOpcode");
  233.         }
  234.  
  235.         player->lastIP = player->getIP();
  236.         player->lastLoginSaved = std::max<time_t>(time(nullptr), player->lastLoginSaved + 1);
  237.         acceptPackets = true;
  238.     } else {
  239.         if (eventConnect != 0 || !g_config.getBoolean(ConfigManager::REPLACE_KICK_ON_LOGIN)) {
  240.             //Already trying to connect
  241.             disconnectClient("You are already logged in.");
  242.             return;
  243.         }
  244.  
  245.         if (foundPlayer->client) {
  246.             foundPlayer->disconnect();
  247.             foundPlayer->isConnecting = true;
  248.  
  249.             eventConnect = g_scheduler.addEvent(createSchedulerTask(1000, std::bind(&ProtocolGame::connect, getThis(), foundPlayer->getID(), operatingSystem)));
  250.         } else {
  251.             connect(foundPlayer->getID(), operatingSystem);
  252.         }
  253.     }
  254.     OutputMessagePool::getInstance().addProtocolToAutosend(shared_from_this());
  255. }
  256.  
  257. void ProtocolGame::connect(uint32_t playerId, OperatingSystem_t operatingSystem)
  258. {
  259.     eventConnect = 0;
  260.  
  261.     Player* foundPlayer = g_game.getPlayerByID(playerId);
  262.     if (!foundPlayer || foundPlayer->client) {
  263.         disconnectClient("You are already logged in.");
  264.         return;
  265.     }
  266.  
  267.     if (isConnectionExpired()) {
  268.         //ProtocolGame::release() has been called at this point and the Connection object
  269.         //no longer exists, so we return to prevent leakage of the Player.
  270.         return;
  271.     }
  272.  
  273.     player = foundPlayer;
  274.     player->incrementReferenceCounter();
  275.  
  276.     g_chat->removeUserFromAllChannels(*player);
  277.     //player->clearModalWindows();
  278.     player->setOperatingSystem(operatingSystem);
  279.     player->isConnecting = false;
  280.  
  281.     player->client = getThis();
  282.     sendAddCreature(player, player->getPosition(), 0, false);
  283.     player->lastIP = player->getIP();
  284.     player->lastLoginSaved = std::max<time_t>(time(nullptr), player->lastLoginSaved + 1);
  285.     player->resetIdleTime();
  286.     acceptPackets = true;
  287. }
  288.  
  289. void ProtocolGame::logout(bool displayEffect, bool forced)
  290. {
  291.     //dispatcher thread
  292.     if (!player) {
  293.         return;
  294.     }
  295.  
  296.     if (!player->isRemoved()) {
  297.         if (!forced) {
  298.             if (!player->isAccessPlayer()) {
  299.                 if (player->getTile()->hasFlag(TILESTATE_NOLOGOUT)) {
  300.                     player->sendCancelMessage(RETURNVALUE_YOUCANNOTLOGOUTHERE);
  301.                     return;
  302.                 }
  303.  
  304.                 if (!player->getTile()->hasFlag(TILESTATE_PROTECTIONZONE) && player->hasCondition(CONDITION_INFIGHT)) {
  305.                     player->sendCancelMessage(RETURNVALUE_YOUMAYNOTLOGOUTDURINGAFIGHT);
  306.                     return;
  307.                 }
  308.             }
  309.  
  310.             //scripting event - onLogout
  311.             if (!g_creatureEvents->playerLogout(player)) {
  312.                 //Let the script handle the error message
  313.                 return;
  314.             }
  315.         }
  316.  
  317.         if (displayEffect && player->getHealth() > 0 && !player->isInGhostMode()) {
  318.             g_game.addMagicEffect(player->getPosition(), CONST_ME_POFF);
  319.         }
  320.     }
  321.  
  322.     disconnect();
  323.  
  324.     g_game.removeCreature(player);
  325. }
  326.  
  327. void ProtocolGame::onRecvFirstMessage(NetworkMessage& msg)
  328. {
  329.     if (g_game.getGameState() == GAME_STATE_SHUTDOWN) {
  330.         disconnect();
  331.         return;
  332.     }
  333.  
  334.     OperatingSystem_t operatingSystem = static_cast<OperatingSystem_t>(msg.get<uint16_t>());
  335.     version = msg.get<uint16_t>();
  336.  
  337.     //msg.skipBytes(7); // U32 client version, U8 client type, U16 dat revision
  338.  
  339.     if (!Protocol::RSA_decrypt(msg)) {
  340.         disconnect();
  341.         return;
  342.     }
  343.  
  344.     xtea::key key;
  345.     key[0] = msg.get<uint32_t>();
  346.     key[1] = msg.get<uint32_t>();
  347.     key[2] = msg.get<uint32_t>();
  348.     key[3] = msg.get<uint32_t>();
  349.     enableXTEAEncryption();
  350.     setXTEAKey(std::move(key));
  351.  
  352.     if (operatingSystem >= CLIENTOS_OTCLIENT_LINUX) {
  353.         NetworkMessage opcodeMessage;
  354.         opcodeMessage.addByte(0x32);
  355.         opcodeMessage.addByte(0x00);
  356.         opcodeMessage.add<uint16_t>(0x00);
  357.         writeToOutputBuffer(opcodeMessage);
  358.     }
  359.  
  360.     msg.skipBytes(1); // gamemaster flag
  361.  
  362.     /*std::string sessionKey = msg.getString();
  363.  
  364.     auto sessionArgs = explodeString(sessionKey, "\n", 4);
  365.     if (sessionArgs.size() != 4) {
  366.         disconnect();
  367.         return;
  368.     }
  369.  
  370.     std::string& accountName = sessionArgs[0];
  371.     std::string& password = sessionArgs[1];
  372.     std::string& token = sessionArgs[2];
  373.     uint32_t tokenTime = 0;
  374.     try {
  375.         tokenTime = std::stoul(sessionArgs[3]);
  376.     } catch (const std::invalid_argument&) {
  377.         disconnectClient("Malformed token packet.");
  378.         return;
  379.     } catch (const std::out_of_range&) {
  380.         disconnectClient("Token time is too long.");
  381.         return;
  382.     }*/
  383.  
  384.     uint32_t accountNumber = msg.get<uint32_t>();
  385.     if (accountNumber == 0) {
  386.         disconnectClient("You must enter your account number.");
  387.         return;
  388.     }
  389.  
  390.     std::string accountName = std::to_string(accountNumber);
  391.     if (accountName.empty()) {
  392.         disconnectClient("You must enter your account number.");
  393.         return;
  394.     }
  395.  
  396.     std::string characterName = msg.getString();
  397.     const std::string& password = msg.getString();
  398.  
  399.     std::string token = "";
  400.     uint32_t tokenTime = 0;
  401.  
  402.     /*uint32_t timeStamp = msg.get<uint32_t>();
  403.     uint8_t randNumber = msg.getByte();
  404.     if (challengeTimestamp != timeStamp || challengeRandom != randNumber) {
  405.         disconnect();
  406.         return;
  407.     }*/
  408.  
  409.     if (version < CLIENT_VERSION_MIN || version > CLIENT_VERSION_MAX) {
  410.         disconnectClient(fmt::format("Only clients with protocol {:s} allowed!", CLIENT_VERSION_STR));
  411.         return;
  412.     }
  413.  
  414.     if (g_game.getGameState() == GAME_STATE_STARTUP) {
  415.         disconnectClient("Gameworld is starting up. Please wait.");
  416.         return;
  417.     }
  418.  
  419.     if (g_game.getGameState() == GAME_STATE_MAINTAIN) {
  420.         disconnectClient("Gameworld is under maintenance. Please re-connect in a while.");
  421.         return;
  422.     }
  423.  
  424.     BanInfo banInfo;
  425.     if (IOBan::isIpBanned(getIP(), banInfo)) {
  426.         if (banInfo.reason.empty()) {
  427.             banInfo.reason = "(none)";
  428.         }
  429.  
  430.         disconnectClient(fmt::format("Your IP has been banned until {:s} by {:s}.\n\nReason specified:\n{:s}", formatDateShort(banInfo.expiresAt), banInfo.bannedBy, banInfo.reason));
  431.         return;
  432.     }
  433.  
  434.     uint32_t accountId = IOLoginData::gameworldAuthentication(accountName, password, characterName, token, tokenTime);
  435.     if (accountId == 0) {
  436.         disconnectClient("Account name or password is not correct.");
  437.         return;
  438.     }
  439.  
  440.     g_dispatcher.addTask(createTask(std::bind(&ProtocolGame::login, getThis(), characterName, accountId, operatingSystem)));
  441. }
  442.  
  443. void ProtocolGame::onConnect()
  444. {
  445.     /*auto output = OutputMessagePool::getOutputMessage();
  446.     static std::random_device rd;
  447.     static std::ranlux24 generator(rd());
  448.     static std::uniform_int_distribution<uint16_t> randNumber(0x00, 0xFF);
  449.  
  450.     // Skip checksum
  451.     output->skipBytes(sizeof(uint32_t));
  452.  
  453.     // Packet length & type
  454.     output->add<uint16_t>(0x0006);
  455.     output->addByte(0x1F);
  456.  
  457.     // Add timestamp & random number
  458.     challengeTimestamp = static_cast<uint32_t>(time(nullptr));
  459.     output->add<uint32_t>(challengeTimestamp);
  460.  
  461.     challengeRandom = randNumber(generator);
  462.     output->addByte(challengeRandom);
  463.  
  464.     // Go back and write checksum
  465.     output->skipBytes(-12);
  466.     output->add<uint32_t>(adlerChecksum(output->getOutputBuffer() + sizeof(uint32_t), 8));
  467.  
  468.     send(output);*/
  469. }
  470.  
  471. void ProtocolGame::disconnectClient(const std::string& message) const
  472. {
  473.     auto output = OutputMessagePool::getOutputMessage();
  474.     output->addByte(0x14);
  475.     output->addString(message);
  476.     send(output);
  477.     disconnect();
  478. }
  479.  
  480. void ProtocolGame::writeToOutputBuffer(const NetworkMessage& msg)
  481. {
  482.     auto out = getOutputBuffer(msg.getLength());
  483.     out->append(msg);
  484. }
  485.  
  486. void ProtocolGame::parsePacket(NetworkMessage& msg)
  487. {
  488.     if (!acceptPackets || g_game.getGameState() == GAME_STATE_SHUTDOWN || msg.getLength() == 0) {
  489.         return;
  490.     }
  491.  
  492.     uint8_t recvbyte = msg.getByte();
  493.  
  494.     if (!player) {
  495.         if (recvbyte == 0x0F) {
  496.             disconnect();
  497.         }
  498.  
  499.         return;
  500.     }
  501.  
  502.     //a dead player can not performs actions
  503.     if (player->isRemoved() || player->getHealth() <= 0) {
  504.         if (recvbyte == 0x0F) {
  505.             disconnect();
  506.             return;
  507.         }
  508.  
  509.         if (recvbyte != 0x14) {
  510.             return;
  511.         }
  512.     }
  513.  
  514.     switch (recvbyte) {
  515.         case 0x14: g_dispatcher.addTask(createTask(std::bind(&ProtocolGame::logout, getThis(), true, false))); break;
  516.         case 0x1D: addGameTask(&Game::playerReceivePingBack, player->getID()); break;
  517.         case 0x1E: addGameTask(&Game::playerReceivePing, player->getID()); break;
  518.         case 0x32: parseExtendedOpcode(msg); break; //otclient extended opcode
  519.         case 0x64: parseAutoWalk(msg); break;
  520.         case 0x65: addGameTask(&Game::playerMove, player->getID(), DIRECTION_NORTH); break;
  521.         case 0x66: addGameTask(&Game::playerMove, player->getID(), DIRECTION_EAST); break;
  522.         case 0x67: addGameTask(&Game::playerMove, player->getID(), DIRECTION_SOUTH); break;
  523.         case 0x68: addGameTask(&Game::playerMove, player->getID(), DIRECTION_WEST); break;
  524.         case 0x69: addGameTask(&Game::playerStopAutoWalk, player->getID()); break;
  525.         case 0x6A: addGameTask(&Game::playerMove, player->getID(), DIRECTION_NORTHEAST); break;
  526.         case 0x6B: addGameTask(&Game::playerMove, player->getID(), DIRECTION_SOUTHEAST); break;
  527.         case 0x6C: addGameTask(&Game::playerMove, player->getID(), DIRECTION_SOUTHWEST); break;
  528.         case 0x6D: addGameTask(&Game::playerMove, player->getID(), DIRECTION_NORTHWEST); break;
  529.         case 0x6F: addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerTurn, player->getID(), DIRECTION_NORTH); break;
  530.         case 0x70: addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerTurn, player->getID(), DIRECTION_EAST); break;
  531.         case 0x71: addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerTurn, player->getID(), DIRECTION_SOUTH); break;
  532.         case 0x72: addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerTurn, player->getID(), DIRECTION_WEST); break;
  533.         case 0x77: parseEquipObject(msg); break;
  534.         case 0x78: parseThrow(msg); break;
  535.         case 0x79: parseLookInShop(msg); break;
  536.         case 0x7A: parsePlayerPurchase(msg); break;
  537.         case 0x7B: parsePlayerSale(msg); break;
  538.         case 0x7C: addGameTask(&Game::playerCloseShop, player->getID()); break;
  539.         case 0x7D: parseRequestTrade(msg); break;
  540.         case 0x7E: parseLookInTrade(msg); break;
  541.         case 0x7F: addGameTask(&Game::playerAcceptTrade, player->getID()); break;
  542.         case 0x80: addGameTask(&Game::playerCloseTrade, player->getID()); break;
  543.         case 0x82: parseUseItem(msg); break;
  544.         case 0x83: parseUseItemEx(msg); break;
  545.         case 0x84: parseUseWithCreature(msg); break;
  546.         case 0x85: parseRotateItem(msg); break;
  547.         case 0x87: parseCloseContainer(msg); break;
  548.         case 0x88: parseUpArrowContainer(msg); break;
  549.         case 0x89: parseTextWindow(msg); break;
  550.         case 0x8A: parseHouseWindow(msg); break;
  551.         case 0x8B: parseWrapItem(msg); break;
  552.         case 0x8C: parseLookAt(msg); break;
  553.         case 0x8D: parseLookInBattleList(msg); break;
  554.         case 0x8E: /* join aggression */ break;
  555.         case 0x96: parseSay(msg); break;
  556.         case 0x97: addGameTask(&Game::playerRequestChannels, player->getID()); break;
  557.         case 0x98: parseOpenChannel(msg); break;
  558.         case 0x99: parseCloseChannel(msg); break;
  559.         case 0x9A: parseOpenPrivateChannel(msg); break;
  560.         case 0x9E: addGameTask(&Game::playerCloseNpcChannel, player->getID()); break;
  561.         case 0xA0: parseFightModes(msg); break;
  562.         case 0xA1: parseAttack(msg); break;
  563.         case 0xA2: parseFollow(msg); break;
  564.         case 0xA3: parseInviteToParty(msg); break;
  565.         case 0xA4: parseJoinParty(msg); break;
  566.         case 0xA5: parseRevokePartyInvite(msg); break;
  567.         case 0xA6: parsePassPartyLeadership(msg); break;
  568.         case 0xA7: addGameTask(&Game::playerLeaveParty, player->getID()); break;
  569.         case 0xA8: parseEnableSharedPartyExperience(msg); break;
  570.         case 0xAA: addGameTask(&Game::playerCreatePrivateChannel, player->getID()); break;
  571.         case 0xAB: parseChannelInvite(msg); break;
  572.         case 0xAC: parseChannelExclude(msg); break;
  573.         case 0xBE: addGameTask(&Game::playerCancelAttackAndFollow, player->getID()); break;
  574.         case 0xC9: /* update tile */ break;
  575.         case 0xCA: parseUpdateContainer(msg); break;
  576.         //case 0xCB: parseBrowseField(msg); break;
  577.         case 0xCC: parseSeekInContainer(msg); break;
  578.         case 0xD2: addGameTask(&Game::playerRequestOutfit, player->getID()); break;
  579.         case 0xD3: parseSetOutfit(msg); break;
  580.         case 0xD4: parseToggleMount(msg); break;
  581.         case 0xDC: parseAddVip(msg); break;
  582.         case 0xDD: parseRemoveVip(msg); break;
  583.         case 0xDE: parseEditVip(msg); break;
  584.         case 0xE6: parseBugReport(msg); break;
  585.         case 0xE7: /* thank you */ break;
  586.         case 0xE8: parseDebugAssert(msg); break;
  587.         case 0xF0: addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerShowQuestLog, player->getID()); break;
  588.         case 0xF1: parseQuestLine(msg); break;
  589.         case 0xF2: parseRuleViolationReport(msg); break;
  590.         case 0xF3: /* get object info */ break;
  591.         /*case 0xF4: parseMarketLeave(); break;
  592.         case 0xF5: parseMarketBrowse(msg); break;
  593.         case 0xF6: parseMarketCreateOffer(msg); break;
  594.         case 0xF7: parseMarketCancelOffer(msg); break;
  595.         case 0xF8: parseMarketAcceptOffer(msg); break;*/
  596.         case 0xF9: parseModalWindowAnswer(msg); break;
  597.  
  598.         default:
  599.             // std::cout << "Player: " << player->getName() << " sent an unknown packet header: 0x" << std::hex << static_cast<uint16_t>(recvbyte) << std::dec << "!" << std::endl;
  600.             break;
  601.     }
  602.  
  603.     if (msg.isOverrun()) {
  604.         disconnect();
  605.     }
  606. }
  607.  
  608. void ProtocolGame::GetTileDescription(const Tile* tile, NetworkMessage& msg)
  609. {
  610.     //msg.add<uint16_t>(0x00); //environmental effects
  611.  
  612.     int32_t count;
  613.     Item* ground = tile->getGround();
  614.     if (ground) {
  615.         msg.addItem(ground);
  616.         count = 1;
  617.     } else {
  618.         count = 0;
  619.     }
  620.  
  621.     const TileItemVector* items = tile->getItemList();
  622.     if (items) {
  623.         for (auto it = items->getBeginTopItem(), end = items->getEndTopItem(); it != end; ++it) {
  624.             msg.addItem(*it);
  625.  
  626.             if (++count == 10) {
  627.                 break;
  628.             }
  629.         }
  630.     }
  631.  
  632.     const CreatureVector* creatures = tile->getCreatures();
  633.     if (creatures) {
  634.         for (auto it = creatures->rbegin(), end = creatures->rend(); it != end; ++it) {
  635.             const Creature* creature = (*it);
  636.             if (!player->canSeeCreature(creature)) {
  637.                 continue;
  638.             }
  639.  
  640.             bool known;
  641.             uint32_t removedKnown;
  642.             checkCreatureAsKnown(creature->getID(), known, removedKnown);
  643.             AddCreature(msg, creature, known, removedKnown);
  644.             ++count;
  645.         }
  646.     }
  647.  
  648.     if (items && count < 10) {
  649.         for (auto it = items->getBeginDownItem(), end = items->getEndDownItem(); it != end; ++it) {
  650.             msg.addItem(*it);
  651.  
  652.             if (++count == 10) {
  653.                 return;
  654.             }
  655.         }
  656.     }
  657. }
  658.  
  659. void ProtocolGame::GetMapDescription(int32_t x, int32_t y, int32_t z, int32_t width, int32_t height, NetworkMessage& msg)
  660. {
  661.     int32_t skip = -1;
  662.     int32_t startz, endz, zstep;
  663.  
  664.     if (z > 7) {
  665.         startz = z - 2;
  666.         endz = std::min<int32_t>(MAP_MAX_LAYERS - 1, z + 2);
  667.         zstep = 1;
  668.     } else {
  669.         startz = 7;
  670.         endz = 0;
  671.         zstep = -1;
  672.     }
  673.  
  674.     for (int32_t nz = startz; nz != endz + zstep; nz += zstep) {
  675.         GetFloorDescription(msg, x, y, nz, width, height, z - nz, skip);
  676.     }
  677.  
  678.     if (skip >= 0) {
  679.         msg.addByte(skip);
  680.         msg.addByte(0xFF);
  681.     }
  682. }
  683.  
  684. void ProtocolGame::GetFloorDescription(NetworkMessage& msg, int32_t x, int32_t y, int32_t z, int32_t width, int32_t height, int32_t offset, int32_t& skip)
  685. {
  686.     for (int32_t nx = 0; nx < width; nx++) {
  687.         for (int32_t ny = 0; ny < height; ny++) {
  688.             Tile* tile = g_game.map.getTile(x + nx + offset, y + ny + offset, z);
  689.             if (tile) {
  690.                 if (skip >= 0) {
  691.                     msg.addByte(skip);
  692.                     msg.addByte(0xFF);
  693.                 }
  694.  
  695.                 skip = 0;
  696.                 GetTileDescription(tile, msg);
  697.             } else if (skip == 0xFE) {
  698.                 msg.addByte(0xFF);
  699.                 msg.addByte(0xFF);
  700.                 skip = -1;
  701.             } else {
  702.                 ++skip;
  703.             }
  704.         }
  705.     }
  706. }
  707.  
  708. void ProtocolGame::checkCreatureAsKnown(uint32_t id, bool& known, uint32_t& removedKnown)
  709. {
  710.     auto result = knownCreatureSet.insert(id);
  711.     if (!result.second) {
  712.         known = true;
  713.         return;
  714.     }
  715.  
  716.     known = false;
  717.  
  718.     //if (knownCreatureSet.size() > 1300) {
  719.     if (knownCreatureSet.size() > 150) {
  720.         // Look for a creature to remove
  721.         for (auto it = knownCreatureSet.begin(), end = knownCreatureSet.end(); it != end; ++it) {
  722.             Creature* creature = g_game.getCreatureByID(*it);
  723.             if (!canSee(creature)) {
  724.                 removedKnown = *it;
  725.                 knownCreatureSet.erase(it);
  726.                 return;
  727.             }
  728.         }
  729.  
  730.         // Bad situation. Let's just remove anyone.
  731.         auto it = knownCreatureSet.begin();
  732.         if (*it == id) {
  733.             ++it;
  734.         }
  735.  
  736.         removedKnown = *it;
  737.         knownCreatureSet.erase(it);
  738.     } else {
  739.         removedKnown = 0;
  740.     }
  741. }
  742.  
  743. bool ProtocolGame::canSee(const Creature* c) const
  744. {
  745.     if (!c || !player || c->isRemoved()) {
  746.         return false;
  747.     }
  748.  
  749.     if (!player->canSeeCreature(c)) {
  750.         return false;
  751.     }
  752.  
  753.     return canSee(c->getPosition());
  754. }
  755.  
  756. bool ProtocolGame::canSee(const Position& pos) const
  757. {
  758.     return canSee(pos.x, pos.y, pos.z);
  759. }
  760.  
  761. bool ProtocolGame::canSee(int32_t x, int32_t y, int32_t z) const
  762. {
  763.     if (!player) {
  764.         return false;
  765.     }
  766.  
  767.     const Position& myPos = player->getPosition();
  768.     if (myPos.z <= 7) {
  769.         //we are on ground level or above (7 -> 0)
  770.         //view is from 7 -> 0
  771.         if (z > 7) {
  772.             return false;
  773.         }
  774.     } else { // if (myPos.z >= 8) {
  775.         //we are underground (8 -> 15)
  776.         //view is +/- 2 from the floor we stand on
  777.         if (std::abs(myPos.getZ() - z) > 2) {
  778.             return false;
  779.         }
  780.     }
  781.  
  782.     //negative offset means that the action taken place is on a lower floor than ourself
  783.     int32_t offsetz = myPos.getZ() - z;
  784.     if ((x >= myPos.getX() - Map::maxClientViewportX + offsetz) && (x <= myPos.getX() + (Map::maxClientViewportX + 1) + offsetz) &&
  785.         (y >= myPos.getY() - Map::maxClientViewportY + offsetz) && (y <= myPos.getY() + (Map::maxClientViewportY + 1) + offsetz)) {
  786.         return true;
  787.     }
  788.     return false;
  789. }
  790.  
  791. // Parse methods
  792. void ProtocolGame::parseChannelInvite(NetworkMessage& msg)
  793. {
  794.     const std::string name = msg.getString();
  795.     addGameTask(&Game::playerChannelInvite, player->getID(), name);
  796. }
  797.  
  798. void ProtocolGame::parseChannelExclude(NetworkMessage& msg)
  799. {
  800.     const std::string name = msg.getString();
  801.     addGameTask(&Game::playerChannelExclude, player->getID(), name);
  802. }
  803.  
  804. void ProtocolGame::parseOpenChannel(NetworkMessage& msg)
  805. {
  806.     uint16_t channelId = msg.get<uint16_t>();
  807.     addGameTask(&Game::playerOpenChannel, player->getID(), channelId);
  808. }
  809.  
  810. void ProtocolGame::parseCloseChannel(NetworkMessage& msg)
  811. {
  812.     uint16_t channelId = msg.get<uint16_t>();
  813.     addGameTask(&Game::playerCloseChannel, player->getID(), channelId);
  814. }
  815.  
  816. void ProtocolGame::parseOpenPrivateChannel(NetworkMessage& msg)
  817. {
  818.     const std::string receiver = msg.getString();
  819.     addGameTask(&Game::playerOpenPrivateChannel, player->getID(), receiver);
  820. }
  821.  
  822. void ProtocolGame::parseAutoWalk(NetworkMessage& msg)
  823. {
  824.     uint8_t numdirs = msg.getByte();
  825.     //if (numdirs == 0 || (msg.getBufferPosition() + numdirs) != (msg.getLength() + 8)) {
  826.     if (numdirs == 0 || (msg.getBufferPosition() + numdirs) != (msg.getLength() + 4)) {
  827.         return;
  828.     }
  829.  
  830.     msg.skipBytes(numdirs);
  831.  
  832.     std::vector<Direction> path;
  833.     path.reserve(numdirs);
  834.  
  835.     for (uint8_t i = 0; i < numdirs; ++i) {
  836.         uint8_t rawdir = msg.getPreviousByte();
  837.         switch (rawdir) {
  838.             case 1: path.push_back(DIRECTION_EAST); break;
  839.             case 2: path.push_back(DIRECTION_NORTHEAST); break;
  840.             case 3: path.push_back(DIRECTION_NORTH); break;
  841.             case 4: path.push_back(DIRECTION_NORTHWEST); break;
  842.             case 5: path.push_back(DIRECTION_WEST); break;
  843.             case 6: path.push_back(DIRECTION_SOUTHWEST); break;
  844.             case 7: path.push_back(DIRECTION_SOUTH); break;
  845.             case 8: path.push_back(DIRECTION_SOUTHEAST); break;
  846.             default: break;
  847.         }
  848.     }
  849.  
  850.     if (path.empty()) {
  851.         return;
  852.     }
  853.  
  854.     addGameTask(&Game::playerAutoWalk, player->getID(), std::move(path));
  855. }
  856.  
  857. void ProtocolGame::parseSetOutfit(NetworkMessage& msg)
  858. {
  859.     Outfit_t newOutfit;
  860.     newOutfit.lookType = msg.get<uint16_t>();
  861.     newOutfit.lookHead = msg.getByte();
  862.     newOutfit.lookBody = msg.getByte();
  863.     newOutfit.lookLegs = msg.getByte();
  864.     newOutfit.lookFeet = msg.getByte();
  865.     newOutfit.lookAddons = msg.getByte();
  866.     //newOutfit.lookMount = msg.get<uint16_t>();
  867.     addGameTask(&Game::playerChangeOutfit, player->getID(), newOutfit);
  868. }
  869.  
  870. void ProtocolGame::parseToggleMount(NetworkMessage& msg)
  871. {
  872.     bool mount = msg.getByte() != 0;
  873.     addGameTask(&Game::playerToggleMount, player->getID(), mount);
  874. }
  875.  
  876. void ProtocolGame::parseUseItem(NetworkMessage& msg)
  877. {
  878.     Position pos = msg.getPosition();
  879.     uint16_t spriteId = msg.get<uint16_t>();
  880.     uint8_t stackpos = msg.getByte();
  881.     uint8_t index = msg.getByte();
  882.     addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerUseItem, player->getID(), pos, stackpos, index, spriteId);
  883. }
  884.  
  885. void ProtocolGame::parseUseItemEx(NetworkMessage& msg)
  886. {
  887.     Position fromPos = msg.getPosition();
  888.     uint16_t fromSpriteId = msg.get<uint16_t>();
  889.     uint8_t fromStackPos = msg.getByte();
  890.     Position toPos = msg.getPosition();
  891.     uint16_t toSpriteId = msg.get<uint16_t>();
  892.     uint8_t toStackPos = msg.getByte();
  893.     addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerUseItemEx, player->getID(), fromPos, fromStackPos, fromSpriteId, toPos, toStackPos, toSpriteId);
  894. }
  895.  
  896. void ProtocolGame::parseUseWithCreature(NetworkMessage& msg)
  897. {
  898.     Position fromPos = msg.getPosition();
  899.     uint16_t spriteId = msg.get<uint16_t>();
  900.     uint8_t fromStackPos = msg.getByte();
  901.     uint32_t creatureId = msg.get<uint32_t>();
  902.     addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerUseWithCreature, player->getID(), fromPos, fromStackPos, creatureId, spriteId);
  903. }
  904.  
  905. void ProtocolGame::parseCloseContainer(NetworkMessage& msg)
  906. {
  907.     uint8_t cid = msg.getByte();
  908.     addGameTask(&Game::playerCloseContainer, player->getID(), cid);
  909. }
  910.  
  911. void ProtocolGame::parseUpArrowContainer(NetworkMessage& msg)
  912. {
  913.     uint8_t cid = msg.getByte();
  914.     addGameTask(&Game::playerMoveUpContainer, player->getID(), cid);
  915. }
  916.  
  917. void ProtocolGame::parseUpdateContainer(NetworkMessage& msg)
  918. {
  919.     uint8_t cid = msg.getByte();
  920.     addGameTask(&Game::playerUpdateContainer, player->getID(), cid);
  921. }
  922.  
  923. void ProtocolGame::parseThrow(NetworkMessage& msg)
  924. {
  925.     Position fromPos = msg.getPosition();
  926.     uint16_t spriteId = msg.get<uint16_t>();
  927.     uint8_t fromStackpos = msg.getByte();
  928.     Position toPos = msg.getPosition();
  929.     uint8_t count = msg.getByte();
  930.  
  931.     if (toPos != fromPos) {
  932.         addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerMoveThing, player->getID(), fromPos, spriteId, fromStackpos, toPos, count);
  933.     }
  934. }
  935.  
  936. void ProtocolGame::parseLookAt(NetworkMessage& msg)
  937. {
  938.     Position pos = msg.getPosition();
  939.     msg.skipBytes(2); // spriteId
  940.     uint8_t stackpos = msg.getByte();
  941.     addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerLookAt, player->getID(), pos, stackpos);
  942. }
  943.  
  944. void ProtocolGame::parseLookInBattleList(NetworkMessage& msg)
  945. {
  946.     uint32_t creatureId = msg.get<uint32_t>();
  947.     addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerLookInBattleList, player->getID(), creatureId);
  948. }
  949.  
  950. void ProtocolGame::parseSay(NetworkMessage& msg)
  951. {
  952.     std::string receiver;
  953.     uint16_t channelId;
  954.  
  955.     SpeakClasses type = static_cast<SpeakClasses>(msg.getByte());
  956.     switch (type) {
  957.         //case TALKTYPE_PRIVATE_TO:
  958.         //case TALKTYPE_PRIVATE_RED_TO:
  959.         case TALKTYPE_PRIVATE:
  960.         case TALKTYPE_PRIVATE_RED:
  961.             receiver = msg.getString();
  962.             channelId = 0;
  963.             break;
  964.  
  965.         case TALKTYPE_CHANNEL_Y:
  966.         case TALKTYPE_CHANNEL_R1:
  967.         case TALKTYPE_CHANNEL_R2:
  968.             channelId = msg.get<uint16_t>();
  969.             break;
  970.  
  971.         default:
  972.             channelId = 0;
  973.             break;
  974.     }
  975.  
  976.     const std::string text = msg.getString();
  977.     if (text.length() > 255) {
  978.         return;
  979.     }
  980.  
  981.     addGameTask(&Game::playerSay, player->getID(), channelId, type, receiver, text);
  982. }
  983.  
  984. void ProtocolGame::parseFightModes(NetworkMessage& msg)
  985. {
  986.     uint8_t rawFightMode = msg.getByte(); // 1 - offensive, 2 - balanced, 3 - defensive
  987.     uint8_t rawChaseMode = msg.getByte(); // 0 - stand while fighting, 1 - chase opponent
  988.     uint8_t rawSecureMode = msg.getByte(); // 0 - can't attack unmarked, 1 - can attack unmarked
  989.     // uint8_t rawPvpMode = msg.getByte(); // pvp mode introduced in 10.0
  990.  
  991.     fightMode_t fightMode;
  992.     if (rawFightMode == 1) {
  993.         fightMode = FIGHTMODE_ATTACK;
  994.     } else if (rawFightMode == 2) {
  995.         fightMode = FIGHTMODE_BALANCED;
  996.     } else {
  997.         fightMode = FIGHTMODE_DEFENSE;
  998.     }
  999.  
  1000.     addGameTask(&Game::playerSetFightModes, player->getID(), fightMode, rawChaseMode != 0, rawSecureMode != 0);
  1001. }
  1002.  
  1003. void ProtocolGame::parseAttack(NetworkMessage& msg)
  1004. {
  1005.     uint32_t creatureId = msg.get<uint32_t>();
  1006.     // msg.get<uint32_t>(); creatureId (same as above)
  1007.     addGameTask(&Game::playerSetAttackedCreature, player->getID(), creatureId);
  1008. }
  1009.  
  1010. void ProtocolGame::parseFollow(NetworkMessage& msg)
  1011. {
  1012.     uint32_t creatureId = msg.get<uint32_t>();
  1013.     // msg.get<uint32_t>(); creatureId (same as above)
  1014.     addGameTask(&Game::playerFollowCreature, player->getID(), creatureId);
  1015. }
  1016.  
  1017. void ProtocolGame::parseEquipObject(NetworkMessage& msg)
  1018. {
  1019.     uint16_t spriteId = msg.get<uint16_t>();
  1020.     // msg.get<uint8_t>();
  1021.  
  1022.     addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerEquipItem, player->getID(), spriteId);
  1023. }
  1024.  
  1025. void ProtocolGame::parseTextWindow(NetworkMessage& msg)
  1026. {
  1027.     uint32_t windowTextId = msg.get<uint32_t>();
  1028.     const std::string newText = msg.getString();
  1029.     addGameTask(&Game::playerWriteItem, player->getID(), windowTextId, newText);
  1030. }
  1031.  
  1032. void ProtocolGame::parseHouseWindow(NetworkMessage& msg)
  1033. {
  1034.     uint8_t doorId = msg.getByte();
  1035.     uint32_t id = msg.get<uint32_t>();
  1036.     const std::string text = msg.getString();
  1037.     addGameTask(&Game::playerUpdateHouseWindow, player->getID(), doorId, id, text);
  1038. }
  1039.  
  1040. void ProtocolGame::parseWrapItem(NetworkMessage& msg)
  1041. {
  1042.     Position pos = msg.getPosition();
  1043.     uint16_t spriteId = msg.get<uint16_t>();
  1044.     uint8_t stackpos = msg.getByte();
  1045.     addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerWrapItem, player->getID(), pos, stackpos, spriteId);
  1046. }
  1047.  
  1048. void ProtocolGame::parseLookInShop(NetworkMessage& msg)
  1049. {
  1050.     uint16_t id = msg.get<uint16_t>();
  1051.     uint8_t count = msg.getByte();
  1052.     addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerLookInShop, player->getID(), id, count);
  1053. }
  1054.  
  1055. void ProtocolGame::parsePlayerPurchase(NetworkMessage& msg)
  1056. {
  1057.     uint16_t id = msg.get<uint16_t>();
  1058.     uint8_t count = msg.getByte();
  1059.     uint8_t amount = msg.getByte();
  1060.     bool ignoreCap = msg.getByte() != 0;
  1061.     bool inBackpacks = msg.getByte() != 0;
  1062.     addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerPurchaseItem, player->getID(), id, count, amount, ignoreCap, inBackpacks);
  1063. }
  1064.  
  1065. void ProtocolGame::parsePlayerSale(NetworkMessage& msg)
  1066. {
  1067.     uint16_t id = msg.get<uint16_t>();
  1068.     uint8_t count = msg.getByte();
  1069.     uint8_t amount = msg.getByte();
  1070.     bool ignoreEquipped = msg.getByte() != 0;
  1071.     addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerSellItem, player->getID(), id, count, amount, ignoreEquipped);
  1072. }
  1073.  
  1074. void ProtocolGame::parseRequestTrade(NetworkMessage& msg)
  1075. {
  1076.     Position pos = msg.getPosition();
  1077.     uint16_t spriteId = msg.get<uint16_t>();
  1078.     uint8_t stackpos = msg.getByte();
  1079.     uint32_t playerId = msg.get<uint32_t>();
  1080.     addGameTask(&Game::playerRequestTrade, player->getID(), pos, stackpos, playerId, spriteId);
  1081. }
  1082.  
  1083. void ProtocolGame::parseLookInTrade(NetworkMessage& msg)
  1084. {
  1085.     bool counterOffer = (msg.getByte() == 0x01);
  1086.     uint8_t index = msg.getByte();
  1087.     addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerLookInTrade, player->getID(), counterOffer, index);
  1088. }
  1089.  
  1090. void ProtocolGame::parseAddVip(NetworkMessage& msg)
  1091. {
  1092.     const std::string name = msg.getString();
  1093.     addGameTask(&Game::playerRequestAddVip, player->getID(), name);
  1094. }
  1095.  
  1096. void ProtocolGame::parseRemoveVip(NetworkMessage& msg)
  1097. {
  1098.     uint32_t guid = msg.get<uint32_t>();
  1099.     addGameTask(&Game::playerRequestRemoveVip, player->getID(), guid);
  1100. }
  1101.  
  1102. void ProtocolGame::parseEditVip(NetworkMessage& msg)
  1103. {
  1104.     uint32_t guid = msg.get<uint32_t>();
  1105.     const std::string description = msg.getString();
  1106.     uint32_t icon = std::min<uint32_t>(10, msg.get<uint32_t>()); // 10 is max icon in 9.63
  1107.     bool notify = msg.getByte() != 0;
  1108.     addGameTask(&Game::playerRequestEditVip, player->getID(), guid, description, icon, notify);
  1109. }
  1110.  
  1111. void ProtocolGame::parseRotateItem(NetworkMessage& msg)
  1112. {
  1113.     Position pos = msg.getPosition();
  1114.     uint16_t spriteId = msg.get<uint16_t>();
  1115.     uint8_t stackpos = msg.getByte();
  1116.     addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerRotateItem, player->getID(), pos, stackpos, spriteId);
  1117. }
  1118.  
  1119. void ProtocolGame::parseRuleViolationReport(NetworkMessage& msg)
  1120. {
  1121.     uint8_t reportType = msg.getByte();
  1122.     uint8_t reportReason = msg.getByte();
  1123.     const std::string& targetName = msg.getString();
  1124.     const std::string& comment = msg.getString();
  1125.     std::string translation;
  1126.     if (reportType == REPORT_TYPE_NAME) {
  1127.         translation = msg.getString();
  1128.     } else if (reportType == REPORT_TYPE_STATEMENT) {
  1129.         translation = msg.getString();
  1130.         msg.get<uint32_t>(); // statement id, used to get whatever player have said, we don't log that.
  1131.     }
  1132.  
  1133.     addGameTask(&Game::playerReportRuleViolation, player->getID(), targetName, reportType, reportReason, comment, translation);
  1134. }
  1135.  
  1136. void ProtocolGame::parseBugReport(NetworkMessage& msg)
  1137. {
  1138.     uint8_t category = msg.getByte();
  1139.     std::string message = msg.getString();
  1140.  
  1141.     Position position;
  1142.     if (category == BUG_CATEGORY_MAP) {
  1143.         position = msg.getPosition();
  1144.     }
  1145.  
  1146.     addGameTask(&Game::playerReportBug, player->getID(), message, position, category);
  1147. }
  1148.  
  1149. void ProtocolGame::parseDebugAssert(NetworkMessage& msg)
  1150. {
  1151.     if (debugAssertSent) {
  1152.         return;
  1153.     }
  1154.  
  1155.     debugAssertSent = true;
  1156.  
  1157.     std::string assertLine = msg.getString();
  1158.     std::string date = msg.getString();
  1159.     std::string description = msg.getString();
  1160.     std::string comment = msg.getString();
  1161.     addGameTask(&Game::playerDebugAssert, player->getID(), assertLine, date, description, comment);
  1162. }
  1163.  
  1164. void ProtocolGame::parseInviteToParty(NetworkMessage& msg)
  1165. {
  1166.     uint32_t targetId = msg.get<uint32_t>();
  1167.     addGameTask(&Game::playerInviteToParty, player->getID(), targetId);
  1168. }
  1169.  
  1170. void ProtocolGame::parseJoinParty(NetworkMessage& msg)
  1171. {
  1172.     uint32_t targetId = msg.get<uint32_t>();
  1173.     addGameTask(&Game::playerJoinParty, player->getID(), targetId);
  1174. }
  1175.  
  1176. void ProtocolGame::parseRevokePartyInvite(NetworkMessage& msg)
  1177. {
  1178.     uint32_t targetId = msg.get<uint32_t>();
  1179.     addGameTask(&Game::playerRevokePartyInvitation, player->getID(), targetId);
  1180. }
  1181.  
  1182. void ProtocolGame::parsePassPartyLeadership(NetworkMessage& msg)
  1183. {
  1184.     uint32_t targetId = msg.get<uint32_t>();
  1185.     addGameTask(&Game::playerPassPartyLeadership, player->getID(), targetId);
  1186. }
  1187.  
  1188. void ProtocolGame::parseEnableSharedPartyExperience(NetworkMessage& msg)
  1189. {
  1190.     bool sharedExpActive = msg.getByte() == 1;
  1191.     addGameTask(&Game::playerEnableSharedPartyExperience, player->getID(), sharedExpActive);
  1192. }
  1193.  
  1194. void ProtocolGame::parseQuestLine(NetworkMessage& msg)
  1195. {
  1196.     uint16_t questId = msg.get<uint16_t>();
  1197.     addGameTask(&Game::playerShowQuestLine, player->getID(), questId);
  1198. }
  1199.  
  1200. /*
  1201. void ProtocolGame::parseMarketLeave()
  1202. {
  1203.     addGameTask(&Game::playerLeaveMarket, player->getID());
  1204. }
  1205.  
  1206. void ProtocolGame::parseMarketBrowse(NetworkMessage& msg)
  1207. {
  1208.     uint16_t browseId = msg.get<uint16_t>();
  1209.  
  1210.     if (browseId == MARKETREQUEST_OWN_OFFERS) {
  1211.         addGameTask(&Game::playerBrowseMarketOwnOffers, player->getID());
  1212.     } else if (browseId == MARKETREQUEST_OWN_HISTORY) {
  1213.         addGameTask(&Game::playerBrowseMarketOwnHistory, player->getID());
  1214.     } else {
  1215.         addGameTask(&Game::playerBrowseMarket, player->getID(), browseId);
  1216.     }
  1217. }
  1218.  
  1219. void ProtocolGame::parseMarketCreateOffer(NetworkMessage& msg)
  1220. {
  1221.     uint8_t type = msg.getByte();
  1222.     uint16_t spriteId = msg.get<uint16_t>();
  1223.     uint16_t amount = msg.get<uint16_t>();
  1224.     uint32_t price = msg.get<uint32_t>();
  1225.     bool anonymous = (msg.getByte() != 0);
  1226.     addGameTask(&Game::playerCreateMarketOffer, player->getID(), type, spriteId, amount, price, anonymous);
  1227. }
  1228.  
  1229. void ProtocolGame::parseMarketCancelOffer(NetworkMessage& msg)
  1230. {
  1231.     uint32_t timestamp = msg.get<uint32_t>();
  1232.     uint16_t counter = msg.get<uint16_t>();
  1233.     addGameTask(&Game::playerCancelMarketOffer, player->getID(), timestamp, counter);
  1234. }
  1235.  
  1236. void ProtocolGame::parseMarketAcceptOffer(NetworkMessage& msg)
  1237. {
  1238.     uint32_t timestamp = msg.get<uint32_t>();
  1239.     uint16_t counter = msg.get<uint16_t>();
  1240.     uint16_t amount = msg.get<uint16_t>();
  1241.     addGameTask(&Game::playerAcceptMarketOffer, player->getID(), timestamp, counter, amount);
  1242. }
  1243. */
  1244. void ProtocolGame::parseModalWindowAnswer(NetworkMessage& msg)
  1245. {
  1246.     uint32_t id = msg.get<uint32_t>();
  1247.     uint8_t button = msg.getByte();
  1248.     uint8_t choice = msg.getByte();
  1249.     addGameTask(&Game::playerAnswerModalWindow, player->getID(), id, button, choice);
  1250. }
  1251. /*
  1252. void ProtocolGame::parseBrowseField(NetworkMessage& msg)
  1253. {
  1254.     const Position& pos = msg.getPosition();
  1255.     addGameTask(&Game::playerBrowseField, player->getID(), pos);
  1256. }*/
  1257.  
  1258. void ProtocolGame::parseSeekInContainer(NetworkMessage& msg)
  1259. {
  1260.     uint8_t containerId = msg.getByte();
  1261.     uint16_t index = msg.get<uint16_t>();
  1262.     addGameTask(&Game::playerSeekInContainer, player->getID(), containerId, index);
  1263. }
  1264.  
  1265. // Send methods
  1266. void ProtocolGame::sendOpenPrivateChannel(const std::string& receiver)
  1267. {
  1268.     NetworkMessage msg;
  1269.     msg.addByte(0xAD);
  1270.     msg.addString(receiver);
  1271.     writeToOutputBuffer(msg);
  1272. }
  1273.  
  1274. void ProtocolGame::sendChannelEvent(uint16_t channelId, const std::string& playerName, ChannelEvent_t channelEvent)
  1275. {
  1276.     NetworkMessage msg;
  1277.     msg.addByte(0xF3);
  1278.     msg.add<uint16_t>(channelId);
  1279.     msg.addString(playerName);
  1280.     msg.addByte(channelEvent);
  1281.     writeToOutputBuffer(msg);
  1282. }
  1283.  
  1284. void ProtocolGame::sendCreatureOutfit(const Creature* creature, const Outfit_t& outfit)
  1285. {
  1286.     if (!canSee(creature)) {
  1287.         return;
  1288.     }
  1289.  
  1290.     NetworkMessage msg;
  1291.     msg.addByte(0x8E);
  1292.     msg.add<uint32_t>(creature->getID());
  1293.     AddOutfit(msg, outfit);
  1294.     writeToOutputBuffer(msg);
  1295. }
  1296.  
  1297. void ProtocolGame::sendCreatureLight(const Creature* creature)
  1298. {
  1299.     if (!canSee(creature)) {
  1300.         return;
  1301.     }
  1302.  
  1303.     NetworkMessage msg;
  1304.     AddCreatureLight(msg, creature);
  1305.     writeToOutputBuffer(msg);
  1306. }
  1307.  
  1308. void ProtocolGame::sendWorldLight(LightInfo lightInfo)
  1309. {
  1310.     NetworkMessage msg;
  1311.     AddWorldLight(msg, lightInfo);
  1312.     writeToOutputBuffer(msg);
  1313. }
  1314.  
  1315. void ProtocolGame::sendCreatureWalkthrough(const Creature* creature, bool walkthrough)
  1316. {
  1317.     if (!canSee(creature)) {
  1318.         return;
  1319.     }
  1320.  
  1321.     NetworkMessage msg;
  1322.     msg.addByte(0x92);
  1323.     msg.add<uint32_t>(creature->getID());
  1324.     msg.addByte(walkthrough ? 0x00 : 0x01);
  1325.     writeToOutputBuffer(msg);
  1326. }
  1327.  
  1328. void ProtocolGame::sendCreatureShield(const Creature* creature)
  1329. {
  1330.     if (!canSee(creature)) {
  1331.         return;
  1332.     }
  1333.  
  1334.     NetworkMessage msg;
  1335.     msg.addByte(0x91);
  1336.     msg.add<uint32_t>(creature->getID());
  1337.     msg.addByte(player->getPartyShield(creature->getPlayer()));
  1338.     writeToOutputBuffer(msg);
  1339. }
  1340.  
  1341. void ProtocolGame::sendCreatureSkull(const Creature* creature)
  1342. {
  1343.     if (g_game.getWorldType() != WORLD_TYPE_PVP) {
  1344.         return;
  1345.     }
  1346.  
  1347.     if (!canSee(creature)) {
  1348.         return;
  1349.     }
  1350.  
  1351.     NetworkMessage msg;
  1352.     msg.addByte(0x90);
  1353.     msg.add<uint32_t>(creature->getID());
  1354.     msg.addByte(player->getSkullClient(creature));
  1355.     writeToOutputBuffer(msg);
  1356. }
  1357.  
  1358. void ProtocolGame::sendCreatureType(uint32_t creatureId, uint8_t creatureType)
  1359. {
  1360.     NetworkMessage msg;
  1361.     msg.addByte(0x95);
  1362.     msg.add<uint32_t>(creatureId);
  1363.     msg.addByte(creatureType);
  1364.     writeToOutputBuffer(msg);
  1365. }
  1366.  
  1367. void ProtocolGame::sendCreatureHelpers(uint32_t creatureId, uint16_t helpers)
  1368. {
  1369.     NetworkMessage msg;
  1370.     msg.addByte(0x94);
  1371.     msg.add<uint32_t>(creatureId);
  1372.     msg.add<uint16_t>(helpers);
  1373.     writeToOutputBuffer(msg);
  1374. }
  1375.  
  1376. void ProtocolGame::sendCreatureSquare(const Creature* creature, SquareColor_t color)
  1377. {
  1378.     if (!canSee(creature)) {
  1379.         return;
  1380.     }
  1381.  
  1382.     NetworkMessage msg;
  1383.     msg.addByte(0x86);
  1384.     msg.add<uint32_t>(creature->getID());
  1385.     //msg.addByte(0x01);
  1386.     msg.addByte(color);
  1387.     writeToOutputBuffer(msg);
  1388. }
  1389.  
  1390. void ProtocolGame::sendTutorial(uint8_t tutorialId)
  1391. {
  1392.     NetworkMessage msg;
  1393.     msg.addByte(0xDC);
  1394.     msg.addByte(tutorialId);
  1395.     writeToOutputBuffer(msg);
  1396. }
  1397.  
  1398. void ProtocolGame::sendAddMarker(const Position& pos, uint8_t markType, const std::string& desc)
  1399. {
  1400.     NetworkMessage msg;
  1401.     msg.addByte(0xDD);
  1402.     msg.addPosition(pos);
  1403.     msg.addByte(markType);
  1404.     msg.addString(desc);
  1405.     writeToOutputBuffer(msg);
  1406. }
  1407.  
  1408. void ProtocolGame::sendReLoginWindow(uint8_t unfairFightReduction)
  1409. {
  1410.     NetworkMessage msg;
  1411.     msg.addByte(0x28);
  1412.     msg.addByte(0x00);
  1413.     msg.addByte(unfairFightReduction);
  1414.     writeToOutputBuffer(msg);
  1415. }
  1416.  
  1417. void ProtocolGame::sendStats()
  1418. {
  1419.     NetworkMessage msg;
  1420.     AddPlayerStats(msg);
  1421.     writeToOutputBuffer(msg);
  1422. }
  1423.  
  1424. void ProtocolGame::sendBasicData()
  1425. {
  1426.     NetworkMessage msg;
  1427.     msg.addByte(0x9F);
  1428.     if (player->isPremium()) {
  1429.         msg.addByte(1);
  1430.         msg.add<uint32_t>(g_config.getBoolean(ConfigManager::FREE_PREMIUM) ? 0 : player->premiumEndsAt);
  1431.     } else {
  1432.         msg.addByte(0);
  1433.         msg.add<uint32_t>(0);
  1434.     }
  1435.     msg.addByte(player->getVocation()->getClientId());
  1436.     msg.add<uint16_t>(0xFF); // number of known spells
  1437.     for (uint8_t spellId = 0x00; spellId < 0xFF; spellId++) {
  1438.         msg.addByte(spellId);
  1439.     }
  1440.     writeToOutputBuffer(msg);
  1441. }
  1442.  
  1443. void ProtocolGame::sendTextMessage(const TextMessage& message)
  1444. {
  1445.     NetworkMessage msg;
  1446.     /*msg.addByte(0xB4);
  1447.     msg.addByte(message.type);
  1448.     switch (message.type) {
  1449.         case MESSAGE_DAMAGE_DEALT:
  1450.         case MESSAGE_DAMAGE_RECEIVED:
  1451.         case MESSAGE_DAMAGE_OTHERS: {
  1452.             msg.addPosition(message.position);
  1453.             msg.add<uint32_t>(message.primary.value);
  1454.             msg.addByte(message.primary.color);
  1455.             msg.add<uint32_t>(message.secondary.value);
  1456.             msg.addByte(message.secondary.color);
  1457.             break;
  1458.         }
  1459.         case MESSAGE_HEALED:
  1460.         case MESSAGE_HEALED_OTHERS:
  1461.         case MESSAGE_EXPERIENCE:
  1462.         case MESSAGE_EXPERIENCE_OTHERS: {
  1463.             msg.addPosition(message.position);
  1464.             msg.add<uint32_t>(message.primary.value);
  1465.             msg.addByte(message.primary.color);
  1466.             break;
  1467.         }
  1468.         case MESSAGE_GUILD:
  1469.         case MESSAGE_PARTY_MANAGEMENT:
  1470.         case MESSAGE_PARTY:
  1471.             msg.add<uint16_t>(message.channelId);
  1472.             break;
  1473.         default: {
  1474.             break;
  1475.         }
  1476.     }*/
  1477.     msg.addByte(0xB4);
  1478.     msg.addByte(message.type);
  1479.     msg.addString(message.text);
  1480.     writeToOutputBuffer(msg);
  1481. }
  1482.  
  1483. void ProtocolGame::sendColoredText(const ColoredText& coloredText)
  1484. {
  1485.     if (!canSee(coloredText.position)) {
  1486.         return;
  1487.     }
  1488.  
  1489.     NetworkMessage msg;
  1490.     msg.addByte(0x84);
  1491.     msg.addPosition(coloredText.position);
  1492.     msg.addByte(coloredText.color);
  1493.     msg.addString(coloredText.text);
  1494.     writeToOutputBuffer(msg);
  1495. }
  1496.  
  1497. void ProtocolGame::sendClosePrivate(uint16_t channelId)
  1498. {
  1499.     NetworkMessage msg;
  1500.     msg.addByte(0xB3);
  1501.     msg.add<uint16_t>(channelId);
  1502.     writeToOutputBuffer(msg);
  1503. }
  1504.  
  1505. void ProtocolGame::sendCreatePrivateChannel(uint16_t channelId, const std::string& channelName)
  1506. {
  1507.     NetworkMessage msg;
  1508.     msg.addByte(0xB2);
  1509.     msg.add<uint16_t>(channelId);
  1510.     msg.addString(channelName);
  1511.     /*msg.add<uint16_t>(0x01);
  1512.     msg.addString(player->getName());
  1513.     msg.add<uint16_t>(0x00);
  1514.     */
  1515.     writeToOutputBuffer(msg);
  1516. }
  1517.  
  1518. void ProtocolGame::sendChannelsDialog()
  1519. {
  1520.     NetworkMessage msg;
  1521.     msg.addByte(0xAB);
  1522.  
  1523.     const ChannelList& list = g_chat->getChannelList(*player);
  1524.     msg.addByte(list.size());
  1525.     for (ChatChannel* channel : list) {
  1526.         msg.add<uint16_t>(channel->getId());
  1527.         msg.addString(channel->getName());
  1528.     }
  1529.  
  1530.     writeToOutputBuffer(msg);
  1531. }
  1532.  
  1533. void ProtocolGame::sendChannel(uint16_t channelId, const std::string& channelName, const UsersMap*, const InvitedMap*)
  1534. {
  1535.     NetworkMessage msg;
  1536.     msg.addByte(0xAC);
  1537.  
  1538.     msg.add<uint16_t>(channelId);
  1539.     msg.addString(channelName);
  1540.  
  1541.     /*if (channelUsers) {
  1542.         msg.add<uint16_t>(channelUsers->size());
  1543.         for (const auto& it : *channelUsers) {
  1544.             msg.addString(it.second->getName());
  1545.         }
  1546.     } else {
  1547.         msg.add<uint16_t>(0x00);
  1548.     }
  1549.  
  1550.     if (invitedUsers) {
  1551.         msg.add<uint16_t>(invitedUsers->size());
  1552.         for (const auto& it : *invitedUsers) {
  1553.             msg.addString(it.second->getName());
  1554.         }
  1555.     } else {
  1556.         msg.add<uint16_t>(0x00);
  1557.     }*/
  1558.     writeToOutputBuffer(msg);
  1559. }
  1560.  
  1561. void ProtocolGame::sendChannelMessage(const std::string& author, const std::string& text, SpeakClasses type, uint16_t channel)
  1562. {
  1563.     NetworkMessage msg;
  1564.     msg.addByte(0xAA);
  1565.     msg.add<uint32_t>(0x00);
  1566.     msg.addString(author);
  1567.     msg.add<uint16_t>(0x00);
  1568.     msg.addByte(type);
  1569.     msg.add<uint16_t>(channel);
  1570.     msg.addString(text);
  1571.     writeToOutputBuffer(msg);
  1572. }
  1573.  
  1574. void ProtocolGame::sendIcons(uint16_t icons)
  1575. {
  1576.     NetworkMessage msg;
  1577.     msg.addByte(0xA2);
  1578.     //msg.add<uint16_t>(icons);
  1579.     msg.addByte(icons);
  1580.     writeToOutputBuffer(msg);
  1581. }
  1582.  
  1583. void ProtocolGame::sendContainer(uint8_t cid, const Container* container, bool hasParent, uint16_t firstIndex)
  1584. {
  1585.     NetworkMessage msg;
  1586.     msg.addByte(0x6E);
  1587.  
  1588.     msg.addByte(cid);
  1589.  
  1590.     /*if (container->getID() == ITEM_BROWSEFIELD) {
  1591.         msg.addItem(ITEM_BAG, 1);
  1592.         msg.addString("Browse Field");
  1593.     } else {
  1594.         msg.addItem(container);
  1595.         msg.addString(container->getName());
  1596.     }*/
  1597.  
  1598.     msg.addItem(container);
  1599.     msg.addString(container->getName());
  1600.  
  1601.     msg.addByte(container->capacity());
  1602.  
  1603.     msg.addByte(hasParent ? 0x01 : 0x00);
  1604.  
  1605.     msg.addByte(std::min<uint32_t>(0xFF, container->size()));
  1606.  
  1607.     uint32_t i = 0;
  1608.     const ItemDeque& itemList = container->getItemList();
  1609.     for (ItemDeque::const_iterator it = itemList.begin() + firstIndex, end = itemList.end(); i < 0xFF && it != end; ++it, ++i) {
  1610.         msg.addItem(*it);
  1611.     }
  1612.  
  1613.     /*msg.addByte(container->isUnlocked() ? 0x01 : 0x00); // Drag and drop
  1614.     msg.addByte(container->hasPagination() ? 0x01 : 0x00); // Pagination
  1615.  
  1616.     uint32_t containerSize = container->size();
  1617.     msg.add<uint16_t>(containerSize);
  1618.     msg.add<uint16_t>(firstIndex);
  1619.     if (firstIndex < containerSize) {
  1620.         uint8_t itemsToSend = std::min<uint32_t>(std::min<uint32_t>(container->capacity(), containerSize - firstIndex), std::numeric_limits<uint8_t>::max());
  1621.  
  1622.         msg.addByte(itemsToSend);
  1623.         for (auto it = container->getItemList().begin() + firstIndex, end = it + itemsToSend; it != end; ++it) {
  1624.             msg.addItem(*it);
  1625.         }
  1626.     } else {
  1627.         msg.addByte(0x00);
  1628.     }*/
  1629.     writeToOutputBuffer(msg);
  1630. }
  1631.  
  1632. void ProtocolGame::sendShop(Npc* npc, const ShopInfoList& itemList)
  1633. {
  1634.     NetworkMessage msg;
  1635.     msg.addByte(0x7A);
  1636.     msg.addString(npc->getName());
  1637.  
  1638.     uint16_t itemsToSend = std::min<size_t>(itemList.size(), std::numeric_limits<uint16_t>::max());
  1639.     msg.add<uint16_t>(itemsToSend);
  1640.  
  1641.     uint16_t i = 0;
  1642.     for (auto it = itemList.begin(); i < itemsToSend; ++it, ++i) {
  1643.         AddShopItem(msg, *it);
  1644.     }
  1645.  
  1646.     writeToOutputBuffer(msg);
  1647. }
  1648.  
  1649. void ProtocolGame::sendCloseShop()
  1650. {
  1651.     NetworkMessage msg;
  1652.     msg.addByte(0x7C);
  1653.     writeToOutputBuffer(msg);
  1654. }
  1655.  
  1656. void ProtocolGame::sendSaleItemList(const std::list<ShopInfo>& shop)
  1657. {
  1658.     NetworkMessage msg;
  1659.     msg.addByte(0x7B);
  1660.     msg.add<uint64_t>(player->getMoney() + player->getBankBalance());
  1661.  
  1662.     std::map<uint16_t, uint32_t> saleMap;
  1663.  
  1664.     if (shop.size() <= 5) {
  1665.         // For very small shops it's not worth it to create the complete map
  1666.         for (const ShopInfo& shopInfo : shop) {
  1667.             if (shopInfo.sellPrice == 0) {
  1668.                 continue;
  1669.             }
  1670.  
  1671.             int8_t subtype = -1;
  1672.  
  1673.             const ItemType& itemType = Item::items[shopInfo.itemId];
  1674.             if (itemType.hasSubType() && !itemType.stackable) {
  1675.                 subtype = (shopInfo.subType == 0 ? -1 : shopInfo.subType);
  1676.             }
  1677.  
  1678.             uint32_t count = player->getItemTypeCount(shopInfo.itemId, subtype);
  1679.             if (count > 0) {
  1680.                 saleMap[shopInfo.itemId] = count;
  1681.             }
  1682.         }
  1683.     } else {
  1684.         // Large shop, it's better to get a cached map of all item counts and use it
  1685.         // We need a temporary map since the finished map should only contain items
  1686.         // available in the shop
  1687.         std::map<uint32_t, uint32_t> tempSaleMap;
  1688.         player->getAllItemTypeCount(tempSaleMap);
  1689.  
  1690.         // We must still check manually for the special items that require subtype matches
  1691.         // (That is, fluids such as potions etc., actually these items are very few since
  1692.         // health potions now use their own ID)
  1693.         for (const ShopInfo& shopInfo : shop) {
  1694.             if (shopInfo.sellPrice == 0) {
  1695.                 continue;
  1696.             }
  1697.  
  1698.             int8_t subtype = -1;
  1699.  
  1700.             const ItemType& itemType = Item::items[shopInfo.itemId];
  1701.             if (itemType.hasSubType() && !itemType.stackable) {
  1702.                 subtype = (shopInfo.subType == 0 ? -1 : shopInfo.subType);
  1703.             }
  1704.  
  1705.             if (subtype != -1) {
  1706.                 uint32_t count;
  1707.                 if (itemType.isFluidContainer() || itemType.isSplash()) {
  1708.                     count = player->getItemTypeCount(shopInfo.itemId, subtype); // This shop item requires extra checks
  1709.                 } else {
  1710.                     count = subtype;
  1711.                 }
  1712.  
  1713.                 if (count > 0) {
  1714.                     saleMap[shopInfo.itemId] = count;
  1715.                 }
  1716.             } else {
  1717.                 std::map<uint32_t, uint32_t>::const_iterator findIt = tempSaleMap.find(shopInfo.itemId);
  1718.                 if (findIt != tempSaleMap.end() && findIt->second > 0) {
  1719.                     saleMap[shopInfo.itemId] = findIt->second;
  1720.                 }
  1721.             }
  1722.         }
  1723.     }
  1724.  
  1725.     uint8_t itemsToSend = std::min<size_t>(saleMap.size(), std::numeric_limits<uint8_t>::max());
  1726.     msg.addByte(itemsToSend);
  1727.  
  1728.     uint8_t i = 0;
  1729.     for (std::map<uint16_t, uint32_t>::const_iterator it = saleMap.begin(); i < itemsToSend; ++it, ++i) {
  1730.         msg.addItemId(it->first);
  1731.         msg.addByte(std::min<uint32_t>(it->second, std::numeric_limits<uint8_t>::max()));
  1732.     }
  1733.  
  1734.     writeToOutputBuffer(msg);
  1735. }
  1736.  
  1737. /*
  1738. void ProtocolGame::sendMarketEnter(uint32_t depotId)
  1739. {
  1740.     NetworkMessage msg;
  1741.     msg.addByte(0xF6);
  1742.  
  1743.     msg.add<uint64_t>(player->getBankBalance());
  1744.     msg.addByte(std::min<uint32_t>(IOMarket::getPlayerOfferCount(player->getGUID()), std::numeric_limits<uint8_t>::max()));
  1745.  
  1746.     DepotChest* depotChest = player->getDepotChest(depotId, false);
  1747.     if (!depotChest) {
  1748.         msg.add<uint16_t>(0x00);
  1749.         writeToOutputBuffer(msg);
  1750.         return;
  1751.     }
  1752.  
  1753.     player->setInMarket(true);
  1754.  
  1755.     std::map<uint16_t, uint32_t> depotItems;
  1756.     std::forward_list<Container*> containerList { depotChest, player->getInbox() };
  1757.  
  1758.     do {
  1759.         Container* container = containerList.front();
  1760.         containerList.pop_front();
  1761.  
  1762.         for (Item* item : container->getItemList()) {
  1763.             Container* c = item->getContainer();
  1764.             if (c && !c->empty()) {
  1765.                 containerList.push_front(c);
  1766.                 continue;
  1767.             }
  1768.  
  1769.             const ItemType& itemType = Item::items[item->getID()];
  1770.             if (itemType.wareId == 0) {
  1771.                 continue;
  1772.             }
  1773.  
  1774.             if (c && (!itemType.isContainer() || c->capacity() != itemType.maxItems)) {
  1775.                 continue;
  1776.             }
  1777.  
  1778.             if (!item->hasMarketAttributes()) {
  1779.                 continue;
  1780.             }
  1781.  
  1782.             depotItems[itemType.wareId] += Item::countByType(item, -1);
  1783.         }
  1784.     } while (!containerList.empty());
  1785.  
  1786.     uint16_t itemsToSend = std::min<size_t>(depotItems.size(), std::numeric_limits<uint16_t>::max());
  1787.     msg.add<uint16_t>(itemsToSend);
  1788.  
  1789.     uint16_t i = 0;
  1790.     for (std::map<uint16_t, uint32_t>::const_iterator it = depotItems.begin(); i < itemsToSend; ++it, ++i) {
  1791.         msg.add<uint16_t>(it->first);
  1792.         msg.add<uint16_t>(std::min<uint32_t>(0xFFFF, it->second));
  1793.     }
  1794.  
  1795.     writeToOutputBuffer(msg);
  1796. }
  1797. */
  1798.  
  1799. void ProtocolGame::sendMarketLeave()
  1800. {
  1801.     NetworkMessage msg;
  1802.     msg.addByte(0xF7);
  1803.     writeToOutputBuffer(msg);
  1804. }
  1805.  
  1806. void ProtocolGame::sendMarketBrowseItem(uint16_t itemId, const MarketOfferList& buyOffers, const MarketOfferList& sellOffers)
  1807. {
  1808.     NetworkMessage msg;
  1809.  
  1810.     msg.addByte(0xF9);
  1811.     msg.addItemId(itemId);
  1812.  
  1813.     msg.add<uint32_t>(buyOffers.size());
  1814.     for (const MarketOffer& offer : buyOffers) {
  1815.         msg.add<uint32_t>(offer.timestamp);
  1816.         msg.add<uint16_t>(offer.counter);
  1817.         msg.add<uint16_t>(offer.amount);
  1818.         msg.add<uint32_t>(offer.price);
  1819.         msg.addString(offer.playerName);
  1820.     }
  1821.  
  1822.     msg.add<uint32_t>(sellOffers.size());
  1823.     for (const MarketOffer& offer : sellOffers) {
  1824.         msg.add<uint32_t>(offer.timestamp);
  1825.         msg.add<uint16_t>(offer.counter);
  1826.         msg.add<uint16_t>(offer.amount);
  1827.         msg.add<uint32_t>(offer.price);
  1828.         msg.addString(offer.playerName);
  1829.     }
  1830.  
  1831.     writeToOutputBuffer(msg);
  1832. }
  1833.  
  1834. void ProtocolGame::sendMarketAcceptOffer(const MarketOfferEx& offer)
  1835. {
  1836.     NetworkMessage msg;
  1837.     msg.addByte(0xF9);
  1838.     msg.addItemId(offer.itemId);
  1839.  
  1840.     if (offer.type == MARKETACTION_BUY) {
  1841.         msg.add<uint32_t>(0x01);
  1842.         msg.add<uint32_t>(offer.timestamp);
  1843.         msg.add<uint16_t>(offer.counter);
  1844.         msg.add<uint16_t>(offer.amount);
  1845.         msg.add<uint32_t>(offer.price);
  1846.         msg.addString(offer.playerName);
  1847.         msg.add<uint32_t>(0x00);
  1848.     } else {
  1849.         msg.add<uint32_t>(0x00);
  1850.         msg.add<uint32_t>(0x01);
  1851.         msg.add<uint32_t>(offer.timestamp);
  1852.         msg.add<uint16_t>(offer.counter);
  1853.         msg.add<uint16_t>(offer.amount);
  1854.         msg.add<uint32_t>(offer.price);
  1855.         msg.addString(offer.playerName);
  1856.     }
  1857.  
  1858.     writeToOutputBuffer(msg);
  1859. }
  1860.  
  1861. void ProtocolGame::sendMarketBrowseOwnOffers(const MarketOfferList& buyOffers, const MarketOfferList& sellOffers)
  1862. {
  1863.     NetworkMessage msg;
  1864.     msg.addByte(0xF9);
  1865.     msg.add<uint16_t>(MARKETREQUEST_OWN_OFFERS);
  1866.  
  1867.     msg.add<uint32_t>(buyOffers.size());
  1868.     for (const MarketOffer& offer : buyOffers) {
  1869.         msg.add<uint32_t>(offer.timestamp);
  1870.         msg.add<uint16_t>(offer.counter);
  1871.         msg.addItemId(offer.itemId);
  1872.         msg.add<uint16_t>(offer.amount);
  1873.         msg.add<uint32_t>(offer.price);
  1874.     }
  1875.  
  1876.     msg.add<uint32_t>(sellOffers.size());
  1877.     for (const MarketOffer& offer : sellOffers) {
  1878.         msg.add<uint32_t>(offer.timestamp);
  1879.         msg.add<uint16_t>(offer.counter);
  1880.         msg.addItemId(offer.itemId);
  1881.         msg.add<uint16_t>(offer.amount);
  1882.         msg.add<uint32_t>(offer.price);
  1883.     }
  1884.  
  1885.     writeToOutputBuffer(msg);
  1886. }
  1887.  
  1888. void ProtocolGame::sendMarketCancelOffer(const MarketOfferEx& offer)
  1889. {
  1890.     NetworkMessage msg;
  1891.     msg.addByte(0xF9);
  1892.     msg.add<uint16_t>(MARKETREQUEST_OWN_OFFERS);
  1893.  
  1894.     if (offer.type == MARKETACTION_BUY) {
  1895.         msg.add<uint32_t>(0x01);
  1896.         msg.add<uint32_t>(offer.timestamp);
  1897.         msg.add<uint16_t>(offer.counter);
  1898.         msg.addItemId(offer.itemId);
  1899.         msg.add<uint16_t>(offer.amount);
  1900.         msg.add<uint32_t>(offer.price);
  1901.         msg.add<uint32_t>(0x00);
  1902.     } else {
  1903.         msg.add<uint32_t>(0x00);
  1904.         msg.add<uint32_t>(0x01);
  1905.         msg.add<uint32_t>(offer.timestamp);
  1906.         msg.add<uint16_t>(offer.counter);
  1907.         msg.addItemId(offer.itemId);
  1908.         msg.add<uint16_t>(offer.amount);
  1909.         msg.add<uint32_t>(offer.price);
  1910.     }
  1911.  
  1912.     writeToOutputBuffer(msg);
  1913. }
  1914.  
  1915. void ProtocolGame::sendMarketBrowseOwnHistory(const HistoryMarketOfferList& buyOffers, const HistoryMarketOfferList& sellOffers)
  1916. {
  1917.     uint32_t i = 0;
  1918.     std::map<uint32_t, uint16_t> counterMap;
  1919.     uint32_t buyOffersToSend = std::min<uint32_t>(buyOffers.size(), 810 + std::max<int32_t>(0, 810 - sellOffers.size()));
  1920.     uint32_t sellOffersToSend = std::min<uint32_t>(sellOffers.size(), 810 + std::max<int32_t>(0, 810 - buyOffers.size()));
  1921.  
  1922.     NetworkMessage msg;
  1923.     msg.addByte(0xF9);
  1924.     msg.add<uint16_t>(MARKETREQUEST_OWN_HISTORY);
  1925.  
  1926.     msg.add<uint32_t>(buyOffersToSend);
  1927.     for (auto it = buyOffers.begin(); i < buyOffersToSend; ++it, ++i) {
  1928.         msg.add<uint32_t>(it->timestamp);
  1929.         msg.add<uint16_t>(counterMap[it->timestamp]++);
  1930.         msg.addItemId(it->itemId);
  1931.         msg.add<uint16_t>(it->amount);
  1932.         msg.add<uint32_t>(it->price);
  1933.         msg.addByte(it->state);
  1934.     }
  1935.  
  1936.     counterMap.clear();
  1937.     i = 0;
  1938.  
  1939.     msg.add<uint32_t>(sellOffersToSend);
  1940.     for (auto it = sellOffers.begin(); i < sellOffersToSend; ++it, ++i) {
  1941.         msg.add<uint32_t>(it->timestamp);
  1942.         msg.add<uint16_t>(counterMap[it->timestamp]++);
  1943.         msg.addItemId(it->itemId);
  1944.         msg.add<uint16_t>(it->amount);
  1945.         msg.add<uint32_t>(it->price);
  1946.         msg.addByte(it->state);
  1947.     }
  1948.  
  1949.     writeToOutputBuffer(msg);
  1950. }
  1951.  
  1952. void ProtocolGame::sendMarketDetail(uint16_t itemId)
  1953. {
  1954.     NetworkMessage msg;
  1955.     msg.addByte(0xF8);
  1956.     msg.addItemId(itemId);
  1957.  
  1958.     const ItemType& it = Item::items[itemId];
  1959.     if (it.armor != 0) {
  1960.         msg.addString(std::to_string(it.armor));
  1961.     } else {
  1962.         msg.add<uint16_t>(0x00);
  1963.     }
  1964.  
  1965.     if (it.attack != 0) {
  1966.         // TODO: chance to hit, range
  1967.         // example:
  1968.         // "attack +x, chance to hit +y%, z fields"
  1969.         if (it.abilities && it.abilities->elementType != COMBAT_NONE && it.abilities->elementDamage != 0) {
  1970.             msg.addString(fmt::format("{:d} physical +{:d} {:s}", it.attack, it.abilities->elementDamage, getCombatName(it.abilities->elementType)));
  1971.         } else {
  1972.             msg.addString(std::to_string(it.attack));
  1973.         }
  1974.     } else {
  1975.         msg.add<uint16_t>(0x00);
  1976.     }
  1977.  
  1978.     if (it.isContainer()) {
  1979.         msg.addString(std::to_string(it.maxItems));
  1980.     } else {
  1981.         msg.add<uint16_t>(0x00);
  1982.     }
  1983.  
  1984.     if (it.defense != 0) {
  1985.         if (it.extraDefense != 0) {
  1986.             msg.addString(fmt::format("{:d} {:+d}", it.defense, it.extraDefense));
  1987.         } else {
  1988.             msg.addString(std::to_string(it.defense));
  1989.         }
  1990.     } else {
  1991.         msg.add<uint16_t>(0x00);
  1992.     }
  1993.  
  1994.     if (!it.description.empty()) {
  1995.         const std::string& descr = it.description;
  1996.         if (descr.back() == '.') {
  1997.             msg.addString(std::string(descr, 0, descr.length() - 1));
  1998.         } else {
  1999.             msg.addString(descr);
  2000.         }
  2001.     } else {
  2002.         msg.add<uint16_t>(0x00);
  2003.     }
  2004.  
  2005.     if (it.decayTime != 0) {
  2006.         msg.addString(fmt::format("{:d} seconds", it.decayTime));
  2007.     } else {
  2008.         msg.add<uint16_t>(0x00);
  2009.     }
  2010.  
  2011.     if (it.abilities) {
  2012.         std::ostringstream ss;
  2013.         bool separator = false;
  2014.  
  2015.         for (size_t i = 0; i < COMBAT_COUNT; ++i) {
  2016.             if (it.abilities->absorbPercent[i] == 0) {
  2017.                 continue;
  2018.             }
  2019.  
  2020.             if (separator) {
  2021.                 ss << ", ";
  2022.             } else {
  2023.                 separator = true;
  2024.             }
  2025.  
  2026.             ss << getCombatName(indexToCombatType(i)) << ' ' << std::showpos << it.abilities->absorbPercent[i] << std::noshowpos << '%';
  2027.         }
  2028.  
  2029.         msg.addString(ss.str());
  2030.     } else {
  2031.         msg.add<uint16_t>(0x00);
  2032.     }
  2033.  
  2034.     if (it.minReqLevel != 0) {
  2035.         msg.addString(std::to_string(it.minReqLevel));
  2036.     } else {
  2037.         msg.add<uint16_t>(0x00);
  2038.     }
  2039.  
  2040.     if (it.minReqMagicLevel != 0) {
  2041.         msg.addString(std::to_string(it.minReqMagicLevel));
  2042.     } else {
  2043.         msg.add<uint16_t>(0x00);
  2044.     }
  2045.  
  2046.     msg.addString(it.vocationString);
  2047.  
  2048.     msg.addString(it.runeSpellName);
  2049.  
  2050.     if (it.abilities) {
  2051.         std::ostringstream ss;
  2052.         bool separator = false;
  2053.  
  2054.         for (uint8_t i = SKILL_FIRST; i <= SKILL_LAST; i++) {
  2055.             if (!it.abilities->skills[i]) {
  2056.                 continue;
  2057.             }
  2058.  
  2059.             if (separator) {
  2060.                 ss << ", ";
  2061.             } else {
  2062.                 separator = true;
  2063.             }
  2064.  
  2065.             ss << getSkillName(i) << ' ' << std::showpos << it.abilities->skills[i] << std::noshowpos;
  2066.         }
  2067.  
  2068.         if (it.abilities->stats[STAT_MAGICPOINTS] != 0) {
  2069.             if (separator) {
  2070.                 ss << ", ";
  2071.             } else {
  2072.                 separator = true;
  2073.             }
  2074.  
  2075.             ss << "magic level " << std::showpos << it.abilities->stats[STAT_MAGICPOINTS] << std::noshowpos;
  2076.         }
  2077.  
  2078.         if (it.abilities->speed != 0) {
  2079.             if (separator) {
  2080.                 ss << ", ";
  2081.             }
  2082.  
  2083.             ss << "speed " << std::showpos << (it.abilities->speed >> 1) << std::noshowpos;
  2084.         }
  2085.  
  2086.         msg.addString(ss.str());
  2087.     } else {
  2088.         msg.add<uint16_t>(0x00);
  2089.     }
  2090.  
  2091.     if (it.charges != 0) {
  2092.         msg.addString(std::to_string(it.charges));
  2093.     } else {
  2094.         msg.add<uint16_t>(0x00);
  2095.     }
  2096.  
  2097.     std::string weaponName = getWeaponName(it.weaponType);
  2098.  
  2099.     if (it.slotPosition & SLOTP_TWO_HAND) {
  2100.         if (!weaponName.empty()) {
  2101.             weaponName += ", two-handed";
  2102.         } else {
  2103.             weaponName = "two-handed";
  2104.         }
  2105.     }
  2106.  
  2107.     msg.addString(weaponName);
  2108.  
  2109.     if (it.weight != 0) {
  2110.         std::ostringstream ss;
  2111.         if (it.weight < 10) {
  2112.             ss << "0.0" << it.weight;
  2113.         } else if (it.weight < 100) {
  2114.             ss << "0." << it.weight;
  2115.         } else {
  2116.             std::string weightString = std::to_string(it.weight);
  2117.             weightString.insert(weightString.end() - 2, '.');
  2118.             ss << weightString;
  2119.         }
  2120.         ss << " oz";
  2121.         msg.addString(ss.str());
  2122.     } else {
  2123.         msg.add<uint16_t>(0x00);
  2124.     }
  2125.  
  2126.     MarketStatistics* statistics = IOMarket::getInstance().getPurchaseStatistics(itemId);
  2127.     if (statistics) {
  2128.         msg.addByte(0x01);
  2129.         msg.add<uint32_t>(statistics->numTransactions);
  2130.         msg.add<uint32_t>(std::min<uint64_t>(std::numeric_limits<uint32_t>::max(), statistics->totalPrice));
  2131.         msg.add<uint32_t>(statistics->highestPrice);
  2132.         msg.add<uint32_t>(statistics->lowestPrice);
  2133.     } else {
  2134.         msg.addByte(0x00);
  2135.     }
  2136.  
  2137.     statistics = IOMarket::getInstance().getSaleStatistics(itemId);
  2138.     if (statistics) {
  2139.         msg.addByte(0x01);
  2140.         msg.add<uint32_t>(statistics->numTransactions);
  2141.         msg.add<uint32_t>(std::min<uint64_t>(std::numeric_limits<uint32_t>::max(), statistics->totalPrice));
  2142.         msg.add<uint32_t>(statistics->highestPrice);
  2143.         msg.add<uint32_t>(statistics->lowestPrice);
  2144.     } else {
  2145.         msg.addByte(0x00);
  2146.     }
  2147.  
  2148.     writeToOutputBuffer(msg);
  2149. }
  2150.  
  2151. void ProtocolGame::sendQuestLog()
  2152. {
  2153.     NetworkMessage msg;
  2154.     msg.addByte(0xF0);
  2155.     msg.add<uint16_t>(g_game.quests.getQuestsCount(player));
  2156.  
  2157.     for (const Quest& quest : g_game.quests.getQuests()) {
  2158.         if (quest.isStarted(player)) {
  2159.             msg.add<uint16_t>(quest.getID());
  2160.             msg.addString(quest.getName());
  2161.             msg.addByte(quest.isCompleted(player));
  2162.         }
  2163.     }
  2164.  
  2165.     writeToOutputBuffer(msg);
  2166. }
  2167.  
  2168. void ProtocolGame::sendQuestLine(const Quest* quest)
  2169. {
  2170.     NetworkMessage msg;
  2171.     msg.addByte(0xF1);
  2172.     msg.add<uint16_t>(quest->getID());
  2173.     msg.addByte(quest->getMissionsCount(player));
  2174.  
  2175.     for (const Mission& mission : quest->getMissions()) {
  2176.         if (mission.isStarted(player)) {
  2177.             msg.addString(mission.getName(player));
  2178.             msg.addString(mission.getDescription(player));
  2179.         }
  2180.     }
  2181.  
  2182.     writeToOutputBuffer(msg);
  2183. }
  2184.  
  2185. void ProtocolGame::sendTradeItemRequest(const std::string& traderName, const Item* item, bool ack)
  2186. {
  2187.     NetworkMessage msg;
  2188.  
  2189.     if (ack) {
  2190.         msg.addByte(0x7D);
  2191.     } else {
  2192.         msg.addByte(0x7E);
  2193.     }
  2194.  
  2195.     msg.addString(traderName);
  2196.  
  2197.     if (const Container* tradeContainer = item->getContainer()) {
  2198.         std::list<const Container*> listContainer {tradeContainer};
  2199.         std::list<const Item*> itemList {tradeContainer};
  2200.         while (!listContainer.empty()) {
  2201.             const Container* container = listContainer.front();
  2202.             listContainer.pop_front();
  2203.  
  2204.             for (Item* containerItem : container->getItemList()) {
  2205.                 Container* tmpContainer = containerItem->getContainer();
  2206.                 if (tmpContainer) {
  2207.                     listContainer.push_back(tmpContainer);
  2208.                 }
  2209.                 itemList.push_back(containerItem);
  2210.             }
  2211.         }
  2212.  
  2213.         msg.addByte(itemList.size());
  2214.         for (const Item* listItem : itemList) {
  2215.             msg.addItem(listItem);
  2216.         }
  2217.     } else {
  2218.         msg.addByte(0x01);
  2219.         msg.addItem(item);
  2220.     }
  2221.     writeToOutputBuffer(msg);
  2222. }
  2223.  
  2224. void ProtocolGame::sendCloseTrade()
  2225. {
  2226.     NetworkMessage msg;
  2227.     msg.addByte(0x7F);
  2228.     writeToOutputBuffer(msg);
  2229. }
  2230.  
  2231. void ProtocolGame::sendCloseContainer(uint8_t cid)
  2232. {
  2233.     NetworkMessage msg;
  2234.     msg.addByte(0x6F);
  2235.     msg.addByte(cid);
  2236.     writeToOutputBuffer(msg);
  2237. }
  2238.  
  2239. void ProtocolGame::sendCreatureTurn(const Creature* creature, uint32_t stackPos)
  2240. {
  2241.     if (!canSee(creature)) {
  2242.         return;
  2243.     }
  2244.  
  2245.     NetworkMessage msg;
  2246.     msg.addByte(0x6B);
  2247.     if (stackPos >= 10) {
  2248.         msg.add<uint16_t>(0xFFFF);
  2249.         msg.add<uint32_t>(creature->getID());
  2250.     } else {
  2251.         msg.addPosition(creature->getPosition());
  2252.         msg.addByte(stackPos);
  2253.     }
  2254.  
  2255.     msg.add<uint16_t>(0x63);
  2256.     msg.add<uint32_t>(creature->getID());
  2257.     msg.addByte(creature->getDirection());
  2258.     //msg.addByte(player->canWalkthroughEx(creature) ? 0x00 : 0x01);
  2259.     writeToOutputBuffer(msg);
  2260. }
  2261.  
  2262. void ProtocolGame::sendCreatureSay(const Creature* creature, SpeakClasses type, const std::string& text, const Position* pos/* = nullptr*/)
  2263. {
  2264.     NetworkMessage msg;
  2265.     msg.addByte(0xAA);
  2266.  
  2267.     static uint32_t statementId = 0;
  2268.     msg.add<uint32_t>(++statementId);
  2269.  
  2270.     msg.addString(creature->getName());
  2271.  
  2272.     //Add level only for players
  2273.     /*if (const Player* speaker = creature->getPlayer()) {
  2274.         msg.add<uint16_t>(speaker->getLevel());
  2275.     } else {
  2276.         msg.add<uint16_t>(0x00);
  2277.     }*/
  2278.  
  2279.     msg.addByte(type);
  2280.     if (pos) {
  2281.         msg.addPosition(*pos);
  2282.     } else {
  2283.         msg.addPosition(creature->getPosition());
  2284.     }
  2285.  
  2286.     msg.addString(text);
  2287.     writeToOutputBuffer(msg);
  2288. }
  2289.  
  2290. void ProtocolGame::sendToChannel(const Creature* creature, SpeakClasses type, const std::string& text, uint16_t channelId)
  2291. {
  2292.     NetworkMessage msg;
  2293.     msg.addByte(0xAA);
  2294.  
  2295.     static uint32_t statementId = 0;
  2296.     msg.add<uint32_t>(++statementId);
  2297.     if (!creature) {
  2298.         msg.add<uint32_t>(0x00);
  2299.     } else {
  2300.         msg.addString(creature->getName());
  2301.         //Add level only for players
  2302.         /*if (const Player* speaker = creature->getPlayer()) {
  2303.             msg.add<uint16_t>(speaker->getLevel());
  2304.         } else {
  2305.             msg.add<uint16_t>(0x00);
  2306.         }*/
  2307.     }
  2308.  
  2309.     msg.addByte(type);
  2310.     msg.add<uint16_t>(channelId);
  2311.     msg.addString(text);
  2312.     writeToOutputBuffer(msg);
  2313. }
  2314.  
  2315. void ProtocolGame::sendPrivateMessage(const Player* speaker, SpeakClasses type, const std::string& text)
  2316. {
  2317.     NetworkMessage msg;
  2318.     msg.addByte(0xAA);
  2319.     static uint32_t statementId = 0;
  2320.     msg.add<uint32_t>(++statementId);
  2321.     if (speaker) {
  2322.         msg.addString(speaker->getName());
  2323.         //msg.add<uint16_t>(speaker->getLevel());
  2324.     } else {
  2325.         msg.add<uint32_t>(0x00);
  2326.     }
  2327.     msg.addByte(type);
  2328.     msg.addString(text);
  2329.     writeToOutputBuffer(msg);
  2330. }
  2331.  
  2332. void ProtocolGame::sendCancelTarget()
  2333. {
  2334.     NetworkMessage msg;
  2335.     msg.addByte(0xA3);
  2336.     //msg.add<uint32_t>(0x00);
  2337.     writeToOutputBuffer(msg);
  2338. }
  2339.  
  2340. void ProtocolGame::sendChangeSpeed(const Creature* creature, uint32_t speed)
  2341. {
  2342.     NetworkMessage msg;
  2343.     msg.addByte(0x8F);
  2344.     msg.add<uint32_t>(creature->getID());
  2345.     //msg.add<uint16_t>(creature->getBaseSpeed() / 2);
  2346.     //msg.add<uint16_t>(speed / 2);
  2347.     msg.add<uint16_t>(speed);
  2348.     writeToOutputBuffer(msg);
  2349. }
  2350.  
  2351. void ProtocolGame::sendCancelWalk()
  2352. {
  2353.     NetworkMessage msg;
  2354.     msg.addByte(0xB5);
  2355.     msg.addByte(player->getDirection());
  2356.     writeToOutputBuffer(msg);
  2357. }
  2358.  
  2359. void ProtocolGame::sendSkills()
  2360. {
  2361.     NetworkMessage msg;
  2362.     AddPlayerSkills(msg);
  2363.     writeToOutputBuffer(msg);
  2364. }
  2365.  
  2366. void ProtocolGame::sendPing()
  2367. {
  2368.     NetworkMessage msg;
  2369.     //msg.addByte(0x1D);
  2370.     msg.addByte(0x1E);
  2371.     writeToOutputBuffer(msg);
  2372. }
  2373.  
  2374. void ProtocolGame::sendPingBack()
  2375. {
  2376.     NetworkMessage msg;
  2377.     msg.addByte(0x1E);
  2378.     writeToOutputBuffer(msg);
  2379. }
  2380.  
  2381. void ProtocolGame::sendDistanceShoot(const Position& from, const Position& to, uint8_t type)
  2382. {
  2383.     NetworkMessage msg;
  2384.     msg.addByte(0x85);
  2385.     msg.addPosition(from);
  2386.     msg.addPosition(to);
  2387.     msg.addByte(type);
  2388.     writeToOutputBuffer(msg);
  2389. }
  2390.  
  2391. void ProtocolGame::sendMagicEffect(const Position& pos, uint8_t type)
  2392. {
  2393.     if (!canSee(pos)) {
  2394.         return;
  2395.     }
  2396.  
  2397.     NetworkMessage msg;
  2398.     msg.addByte(0x83);
  2399.     msg.addPosition(pos);
  2400.     msg.addByte(type);
  2401.     writeToOutputBuffer(msg);
  2402. }
  2403.  
  2404. void ProtocolGame::sendCreatureHealth(const Creature* creature)
  2405. {
  2406.     NetworkMessage msg;
  2407.     msg.addByte(0x8C);
  2408.     msg.add<uint32_t>(creature->getID());
  2409.  
  2410.     if (creature->isHealthHidden()) {
  2411.         msg.addByte(0x00);
  2412.     } else {
  2413.         msg.addByte(std::ceil((static_cast<double>(creature->getHealth()) / std::max<int32_t>(creature->getMaxHealth(), 1)) * 100));
  2414.     }
  2415.     writeToOutputBuffer(msg);
  2416. }
  2417.  
  2418. void ProtocolGame::sendFYIBox(const std::string& message)
  2419. {
  2420.     NetworkMessage msg;
  2421.     msg.addByte(0x15);
  2422.     msg.addString(message);
  2423.     writeToOutputBuffer(msg);
  2424. }
  2425.  
  2426. //tile
  2427. void ProtocolGame::sendMapDescription(const Position& pos)
  2428. {
  2429.     NetworkMessage msg;
  2430.     msg.addByte(0x64);
  2431.     msg.addPosition(player->getPosition());
  2432.     GetMapDescription(pos.x - Map::maxClientViewportX, pos.y - Map::maxClientViewportY, pos.z, (Map::maxClientViewportX * 2) + 2, (Map::maxClientViewportY * 2) + 2, msg);
  2433.     writeToOutputBuffer(msg);
  2434. }
  2435.  
  2436. void ProtocolGame::sendAddTileItem(const Position& pos, uint32_t, const Item* item)
  2437. {
  2438.     if (!canSee(pos)) {
  2439.         return;
  2440.     }
  2441.  
  2442.     NetworkMessage msg;
  2443.     msg.addByte(0x6A);
  2444.     msg.addPosition(pos);
  2445.     //msg.addByte(stackpos);
  2446.     msg.addItem(item);
  2447.     writeToOutputBuffer(msg);
  2448. }
  2449.  
  2450. void ProtocolGame::sendUpdateTileItem(const Position& pos, uint32_t stackpos, const Item* item)
  2451. {
  2452.     if (!canSee(pos)) {
  2453.         return;
  2454.     }
  2455.  
  2456.     NetworkMessage msg;
  2457.     msg.addByte(0x6B);
  2458.     msg.addPosition(pos);
  2459.     msg.addByte(stackpos);
  2460.     msg.addItem(item);
  2461.     writeToOutputBuffer(msg);
  2462. }
  2463.  
  2464. void ProtocolGame::sendRemoveTileThing(const Position& pos, uint32_t stackpos)
  2465. {
  2466.     if (!canSee(pos)) {
  2467.         return;
  2468.     }
  2469.  
  2470.     NetworkMessage msg;
  2471.     RemoveTileThing(msg, pos, stackpos);
  2472.     writeToOutputBuffer(msg);
  2473. }
  2474.  
  2475. void ProtocolGame::sendUpdateTileCreature(const Position& pos, uint32_t stackpos, const Creature* creature)
  2476. {
  2477.     if (!canSee(pos)) {
  2478.         return;
  2479.     }
  2480.  
  2481.     NetworkMessage msg;
  2482.     msg.addByte(0x6B);
  2483.     msg.addPosition(pos);
  2484.     msg.addByte(stackpos);
  2485.  
  2486.     bool known;
  2487.     uint32_t removedKnown;
  2488.     checkCreatureAsKnown(creature->getID(), known, removedKnown);
  2489.     AddCreature(msg, creature, false, removedKnown);
  2490.  
  2491.     writeToOutputBuffer(msg);
  2492. }
  2493.  
  2494. void ProtocolGame::sendRemoveTileCreature(const Creature* creature, const Position& pos, uint32_t stackpos)
  2495. {
  2496.     if (stackpos < 10) {
  2497.         if (!canSee(pos)) {
  2498.             return;
  2499.         }
  2500.  
  2501.         NetworkMessage msg;
  2502.         RemoveTileThing(msg, pos, stackpos);
  2503.         writeToOutputBuffer(msg);
  2504.         return;
  2505.     }
  2506.  
  2507.     NetworkMessage msg;
  2508.     msg.addByte(0x6C);
  2509.     msg.add<uint16_t>(0xFFFF);
  2510.     msg.add<uint32_t>(creature->getID());
  2511.     writeToOutputBuffer(msg);
  2512. }
  2513.  
  2514. void ProtocolGame::sendUpdateTile(const Tile* tile, const Position& pos)
  2515. {
  2516.     if (!canSee(pos)) {
  2517.         return;
  2518.     }
  2519.  
  2520.     NetworkMessage msg;
  2521.     msg.addByte(0x69);
  2522.     msg.addPosition(pos);
  2523.  
  2524.     if (tile) {
  2525.         GetTileDescription(tile, msg);
  2526.         msg.addByte(0x00);
  2527.         msg.addByte(0xFF);
  2528.     } else {
  2529.         msg.addByte(0x01);
  2530.         msg.addByte(0xFF);
  2531.     }
  2532.  
  2533.     writeToOutputBuffer(msg);
  2534. }
  2535.  
  2536. void ProtocolGame::sendPendingStateEntered()
  2537. {
  2538.     NetworkMessage msg;
  2539.     msg.addByte(0x0A);
  2540.     writeToOutputBuffer(msg);
  2541. }
  2542.  
  2543. void ProtocolGame::sendEnterWorld()
  2544. {
  2545.     NetworkMessage msg;
  2546.     msg.addByte(0x0F);
  2547.     writeToOutputBuffer(msg);
  2548. }
  2549.  
  2550. void ProtocolGame::sendFightModes()
  2551. {
  2552.     NetworkMessage msg;
  2553.     msg.addByte(0xA7);
  2554.     msg.addByte(player->fightMode);
  2555.     msg.addByte(player->chaseMode);
  2556.     msg.addByte(player->secureMode);
  2557.     //msg.addByte(PVP_MODE_DOVE);
  2558.     writeToOutputBuffer(msg);
  2559. }
  2560.  
  2561. void ProtocolGame::sendAddCreature(const Creature* creature, const Position& pos, int32_t stackpos, bool isLogin)
  2562. {
  2563.     if (!canSee(pos)) {
  2564.         return;
  2565.     }
  2566.  
  2567.     if (creature != player) {
  2568.         // stack pos is always real index now, so it can exceed the limit
  2569.         // if stack pos exceeds the limit, we need to refresh the tile instead
  2570.         // 1. this is a rare case, and is only triggered by forcing summon in a position
  2571.         // 2. since no stackpos will be send to the client about that creature, removing
  2572.         //    it must be done with its id if its stackpos remains >= 10. this is done to
  2573.         //    add creatures to battle list instead of rendering on screen
  2574.         /*if (stackpos >= 10) {
  2575.             // @todo: should we avoid this check?
  2576.             if (const Tile* tile = creature->getTile()) {
  2577.                 sendUpdateTile(tile, pos);
  2578.             }
  2579.         } else*/
  2580.         if (stackpos != -1) {
  2581.             // if stackpos is -1, the client will automatically detect it
  2582.             NetworkMessage msg;
  2583.             msg.addByte(0x6A);
  2584.             msg.addPosition(pos);
  2585.             //msg.addByte(stackpos);
  2586.  
  2587.             bool known;
  2588.             uint32_t removedKnown;
  2589.             checkCreatureAsKnown(creature->getID(), known, removedKnown);
  2590.             AddCreature(msg, creature, known, removedKnown);
  2591.             writeToOutputBuffer(msg);
  2592.         }
  2593.  
  2594.         if (isLogin) {
  2595.             sendMagicEffect(pos, CONST_ME_TELEPORT);
  2596.         }
  2597.         return;
  2598.     }
  2599.  
  2600.     NetworkMessage msg;
  2601.     //msg.addByte(0x17);
  2602.     msg.addByte(0x0A);
  2603.  
  2604.     msg.add<uint32_t>(player->getID());
  2605.     //msg.add<uint16_t>(0x32); // beat duration (50)
  2606.     msg.addByte(0x32); // beat duration (50)
  2607.     msg.addByte(0x00);
  2608.  
  2609.     /*msg.addDouble(Creature::speedA, 3);
  2610.     msg.addDouble(Creature::speedB, 3);
  2611.     msg.addDouble(Creature::speedC, 3);*/
  2612.  
  2613.     // can report bugs?
  2614.     if (player->getAccountType() >= ACCOUNT_TYPE_TUTOR) {
  2615.         msg.addByte(0x01);
  2616.     } else {
  2617.         msg.addByte(0x00);
  2618.     }
  2619.  
  2620.     //msg.addByte(0x00); // can change pvp framing option
  2621.     //msg.addByte(0x00); // expert mode button enabled
  2622.  
  2623.     //msg.add<uint16_t>(0x00); // URL (string) to ingame store images
  2624.     //msg.add<uint16_t>(25); // premium coin package size
  2625.  
  2626.     if (player->getAccountType() >= ACCOUNT_TYPE_GAMEMASTER) {
  2627.         msg.addByte(0x0B);
  2628.         for (uint8_t i = 0; i < 32; i++) {
  2629.             msg.addByte(0xFF);
  2630.         }
  2631.     }
  2632.  
  2633.     writeToOutputBuffer(msg);
  2634.  
  2635.     //sendPendingStateEntered();
  2636.     //sendEnterWorld();
  2637.     sendMapDescription(pos);
  2638.  
  2639.     if (isLogin) {
  2640.         sendMagicEffect(pos, CONST_ME_TELEPORT);
  2641.     }
  2642.  
  2643.     for (int i = CONST_SLOT_FIRST; i <= CONST_SLOT_LAST; ++i) {
  2644.         sendInventoryItem(static_cast<slots_t>(i), player->getInventoryItem(static_cast<slots_t>(i)));
  2645.     }
  2646.  
  2647.     //sendInventoryItem(CONST_SLOT_STORE_INBOX, player->getStoreInbox()->getItem());
  2648.  
  2649.     sendStats();
  2650.     sendSkills();
  2651.  
  2652.     //gameworld light-settings
  2653.     sendWorldLight(g_game.getWorldLightInfo());
  2654.  
  2655.     //player light level
  2656.     sendCreatureLight(creature);
  2657.  
  2658.     sendVIPEntries();
  2659.  
  2660.     //sendBasicData();
  2661.     player->sendIcons();
  2662. }
  2663.  
  2664. void ProtocolGame::sendMoveCreature(const Creature* creature, const Position& newPos, int32_t newStackPos, const Position& oldPos, int32_t oldStackPos, bool teleport)
  2665. {
  2666.     if (creature == player) {
  2667.         if (teleport) {
  2668.             sendRemoveTileCreature(creature, oldPos, oldStackPos);
  2669.             sendMapDescription(newPos);
  2670.         } else {
  2671.             NetworkMessage msg;
  2672.             if (oldPos.z == 7 && newPos.z >= 8) {
  2673.                 RemoveTileCreature(msg, creature, oldPos, oldStackPos);
  2674.             } else {
  2675.                 msg.addByte(0x6D);
  2676.                 if (oldStackPos < 10) {
  2677.                     msg.addPosition(oldPos);
  2678.                     msg.addByte(oldStackPos);
  2679.                 } else {
  2680.                     msg.add<uint16_t>(0xFFFF);
  2681.                     msg.add<uint32_t>(creature->getID());
  2682.                 }
  2683.                 msg.addPosition(newPos);
  2684.             }
  2685.  
  2686.             if (newPos.z > oldPos.z) {
  2687.                 MoveDownCreature(msg, creature, newPos, oldPos);
  2688.             } else if (newPos.z < oldPos.z) {
  2689.                 MoveUpCreature(msg, creature, newPos, oldPos);
  2690.             }
  2691.  
  2692.             if (oldPos.y > newPos.y) { // north, for old x
  2693.                 msg.addByte(0x65);
  2694.                 GetMapDescription(oldPos.x - Map::maxClientViewportX, newPos.y - Map::maxClientViewportY, newPos.z, (Map::maxClientViewportX * 2) + 2, 1, msg);
  2695.             } else if (oldPos.y < newPos.y) { // south, for old x
  2696.                 msg.addByte(0x67);
  2697.                 GetMapDescription(oldPos.x - Map::maxClientViewportX, newPos.y + (Map::maxClientViewportY + 1), newPos.z, (Map::maxClientViewportX * 2) + 2, 1, msg);
  2698.             }
  2699.  
  2700.             if (oldPos.x < newPos.x) { // east, [with new y]
  2701.                 msg.addByte(0x66);
  2702.                 GetMapDescription(newPos.x + (Map::maxClientViewportX + 1), newPos.y - Map::maxClientViewportY, newPos.z, 1, (Map::maxClientViewportY * 2) + 2, msg);
  2703.             } else if (oldPos.x > newPos.x) { // west, [with new y]
  2704.                 msg.addByte(0x68);
  2705.                 GetMapDescription(newPos.x - Map::maxClientViewportX, newPos.y - Map::maxClientViewportY, newPos.z, 1, (Map::maxClientViewportY * 2) + 2, msg);
  2706.             }
  2707.             writeToOutputBuffer(msg);
  2708.         }
  2709.     } else if (canSee(oldPos) && canSee(creature->getPosition())) {
  2710.         if (teleport || (oldPos.z == 7 && newPos.z >= 8)) {
  2711.             sendRemoveTileCreature(creature, oldPos, oldStackPos);
  2712.             sendAddCreature(creature, newPos, newStackPos, false);
  2713.         } else {
  2714.             NetworkMessage msg;
  2715.             msg.addByte(0x6D);
  2716.             if (oldStackPos < 10) {
  2717.                 msg.addPosition(oldPos);
  2718.                 msg.addByte(oldStackPos);
  2719.             } else {
  2720.                 msg.add<uint16_t>(0xFFFF);
  2721.                 msg.add<uint32_t>(creature->getID());
  2722.             }
  2723.             msg.addPosition(creature->getPosition());
  2724.             writeToOutputBuffer(msg);
  2725.         }
  2726.     } else if (canSee(oldPos)) {
  2727.         sendRemoveTileCreature(creature, oldPos, oldStackPos);
  2728.     } else if (canSee(creature->getPosition())) {
  2729.         sendAddCreature(creature, newPos, newStackPos, false);
  2730.     }
  2731. }
  2732.  
  2733. void ProtocolGame::sendInventoryItem(slots_t slot, const Item* item)
  2734. {
  2735.     NetworkMessage msg;
  2736.     if (item) {
  2737.         msg.addByte(0x78);
  2738.         msg.addByte(slot);
  2739.         msg.addItem(item);
  2740.     } else {
  2741.         msg.addByte(0x79);
  2742.         msg.addByte(slot);
  2743.     }
  2744.     writeToOutputBuffer(msg);
  2745. }
  2746.  
  2747. void ProtocolGame::sendItems()
  2748. {
  2749.     NetworkMessage msg;
  2750.     msg.addByte(0xF5);
  2751.  
  2752.     const std::vector<uint16_t>& inventory = Item::items.getInventory();
  2753.     msg.add<uint16_t>(inventory.size() + 11);
  2754.     for (uint16_t i = 1; i <= 11; i++) {
  2755.         msg.add<uint16_t>(i);
  2756.         msg.addByte(0); //always 0
  2757.         msg.add<uint16_t>(1); // always 1
  2758.     }
  2759.  
  2760.     for (auto clientId : inventory) {
  2761.         msg.add<uint16_t>(clientId);
  2762.         msg.addByte(0); //always 0
  2763.         msg.add<uint16_t>(1);
  2764.     }
  2765.  
  2766.     writeToOutputBuffer(msg);
  2767. }
  2768.  
  2769. void ProtocolGame::sendAddContainerItem(uint8_t cid, uint16_t, const Item* item)
  2770. {
  2771.     NetworkMessage msg;
  2772.     msg.addByte(0x70);
  2773.     msg.addByte(cid);
  2774.     //msg.add<uint16_t>(slot);
  2775.     msg.addItem(item);
  2776.     writeToOutputBuffer(msg);
  2777. }
  2778.  
  2779. void ProtocolGame::sendUpdateContainerItem(uint8_t cid, uint16_t slot, const Item* item)
  2780. {
  2781.     NetworkMessage msg;
  2782.     msg.addByte(0x71);
  2783.     msg.addByte(cid);
  2784.     //msg.add<uint16_t>(slot);
  2785.     msg.addByte(slot);
  2786.     msg.addItem(item);
  2787.     writeToOutputBuffer(msg);
  2788. }
  2789.  
  2790. void ProtocolGame::sendRemoveContainerItem(uint8_t cid, uint16_t slot, const Item*)
  2791. {
  2792.     NetworkMessage msg;
  2793.     msg.addByte(0x72);
  2794.     msg.addByte(cid);
  2795.     //msg.add<uint16_t>(slot);
  2796.     /*if (lastItem) {
  2797.         msg.addItem(lastItem);
  2798.     } else {
  2799.         msg.add<uint16_t>(0x00);
  2800.     }*/
  2801.     msg.addByte(slot);
  2802.     writeToOutputBuffer(msg);
  2803. }
  2804.  
  2805. void ProtocolGame::sendTextWindow(uint32_t windowTextId, Item* item, uint16_t maxlen, bool canWrite)
  2806. {
  2807.     NetworkMessage msg;
  2808.     msg.addByte(0x96);
  2809.     msg.add<uint32_t>(windowTextId);
  2810.     msg.addItem(item);
  2811.  
  2812.     if (canWrite) {
  2813.         msg.add<uint16_t>(maxlen);
  2814.         msg.addString(item->getText());
  2815.     } else {
  2816.         const std::string& text = item->getText();
  2817.         msg.add<uint16_t>(text.size());
  2818.         msg.addString(text);
  2819.     }
  2820.  
  2821.     msg.addString(item->getWriter());
  2822.     /*if (!writer.empty()) {
  2823.         msg.addString(writer);
  2824.     } else {
  2825.         msg.add<uint16_t>(0x00);
  2826.     }*/
  2827.  
  2828.     /*time_t writtenDate = item->getDate();
  2829.     if (writtenDate != 0) {
  2830.         msg.addString(formatDateShort(writtenDate));
  2831.     } else {
  2832.         msg.add<uint16_t>(0x00);
  2833.     }*/
  2834.  
  2835.     writeToOutputBuffer(msg);
  2836. }
  2837.  
  2838. void ProtocolGame::sendTextWindow(uint32_t windowTextId, uint32_t itemId, const std::string& text)
  2839. {
  2840.     NetworkMessage msg;
  2841.     msg.addByte(0x96);
  2842.     msg.add<uint32_t>(windowTextId);
  2843.     msg.addItem(itemId, 1);
  2844.     msg.add<uint16_t>(text.size());
  2845.     msg.addString(text);
  2846.     //msg.add<uint16_t>(0x00);
  2847.     //msg.add<uint16_t>(0x00);
  2848.     msg.addString("");
  2849.     writeToOutputBuffer(msg);
  2850. }
  2851.  
  2852. void ProtocolGame::sendHouseWindow(uint32_t windowTextId, const std::string& text)
  2853. {
  2854.     NetworkMessage msg;
  2855.     msg.addByte(0x97);
  2856.     msg.addByte(0x00);
  2857.     msg.add<uint32_t>(windowTextId);
  2858.     msg.addString(text);
  2859.     writeToOutputBuffer(msg);
  2860. }
  2861.  
  2862. void ProtocolGame::sendOutfitWindow()
  2863. {
  2864.     const auto& outfits = Outfits::getInstance().getOutfits(player->getSex());
  2865.     if (outfits.size() == 0) {
  2866.         return;
  2867.     }
  2868.  
  2869.     NetworkMessage msg;
  2870.     msg.addByte(0xC8);
  2871.  
  2872.     Outfit_t currentOutfit = player->getDefaultOutfit();
  2873.     if (currentOutfit.lookType == 0) {
  2874.         Outfit_t newOutfit;
  2875.         newOutfit.lookType = outfits.front().lookType;
  2876.         currentOutfit = newOutfit;
  2877.     }
  2878.  
  2879.     /*Mount* currentMount = g_game.mounts.getMountByID(player->getCurrentMount());
  2880.     if (currentMount) {
  2881.         currentOutfit.lookMount = currentMount->clientId;
  2882.     }*/
  2883.  
  2884.     AddOutfit(msg, currentOutfit);
  2885.  
  2886.     std::vector<ProtocolOutfit> protocolOutfits;
  2887.     if (player->isAccessPlayer()) {
  2888.         static const std::string gamemasterOutfitName = "Gamemaster";
  2889.         protocolOutfits.emplace_back(gamemasterOutfitName, 75, 0);
  2890.     }
  2891.  
  2892.     protocolOutfits.reserve(outfits.size());
  2893.     for (const Outfit& outfit : outfits) {
  2894.         uint8_t addons;
  2895.         if (!player->getOutfitAddons(outfit, addons)) {
  2896.             continue;
  2897.         }
  2898.  
  2899.         protocolOutfits.emplace_back(outfit.name, outfit.lookType, addons);
  2900.         if (protocolOutfits.size() == std::numeric_limits<uint8_t>::max()) { // Game client currently doesn't allow more than 255 outfits
  2901.             break;
  2902.         }
  2903.     }
  2904.  
  2905.     msg.addByte(protocolOutfits.size());
  2906.     for (const ProtocolOutfit& outfit : protocolOutfits) {
  2907.         msg.add<uint16_t>(outfit.lookType);
  2908.         msg.addString(outfit.name);
  2909.         msg.addByte(outfit.addons);
  2910.     }
  2911.     /*
  2912.     std::vector<const Mount*> mounts;
  2913.     for (const Mount& mount : g_game.mounts.getMounts()) {
  2914.         if (player->hasMount(&mount)) {
  2915.             mounts.push_back(&mount);
  2916.         }
  2917.     }
  2918.  
  2919.     msg.addByte(mounts.size());
  2920.     for (const Mount* mount : mounts) {
  2921.         msg.add<uint16_t>(mount->clientId);
  2922.         msg.addString(mount->name);
  2923.     }*/
  2924.  
  2925.     switch (player->getSex()) {
  2926.         case PLAYERSEX_FEMALE: {
  2927.             msg.add<uint16_t>(136);
  2928.             if (player->isPremium()) {
  2929.                 msg.add<uint16_t>(142);
  2930.             } else {
  2931.                 msg.add<uint16_t>(139);
  2932.             }
  2933.  
  2934.             break;
  2935.         }
  2936.  
  2937.         case PLAYERSEX_MALE: {
  2938.             msg.add<uint16_t>(128);
  2939.             if (player->isPremium()) {
  2940.                 msg.add<uint16_t>(134);
  2941.             } else {
  2942.                 msg.add<uint16_t>(131);
  2943.             }
  2944.  
  2945.             break;
  2946.         }
  2947.  
  2948.         default: {
  2949.             msg.add<uint16_t>(128);
  2950.             msg.add<uint16_t>(134);
  2951.         }
  2952.     }
  2953.  
  2954.     writeToOutputBuffer(msg);
  2955. }
  2956.  
  2957. void ProtocolGame::sendUpdatedVIPStatus(uint32_t guid, VipStatus_t newStatus)
  2958. {
  2959.     NetworkMessage msg;
  2960.     //msg.addByte(0xD3);
  2961.     msg.addByte(newStatus ? 0xD3 : 0xD4);
  2962.     msg.add<uint32_t>(guid);
  2963.     //msg.addByte(newStatus);
  2964.     writeToOutputBuffer(msg);
  2965. }
  2966.  
  2967. void ProtocolGame::sendVIP(uint32_t guid, const std::string& name, const std::string&, uint32_t, bool, VipStatus_t status)
  2968. {
  2969.     NetworkMessage msg;
  2970.     msg.addByte(0xD2);
  2971.     msg.add<uint32_t>(guid);
  2972.     msg.addString(name);
  2973.     //msg.addString(description);
  2974.     //msg.add<uint32_t>(std::min<uint32_t>(10, icon));
  2975.     //msg.addByte(notify ? 0x01 : 0x00);
  2976.     msg.addByte(status);
  2977.     writeToOutputBuffer(msg);
  2978. }
  2979.  
  2980. void ProtocolGame::sendVIPEntries()
  2981. {
  2982.     const std::forward_list<VIPEntry>& vipEntries = IOLoginData::getVIPEntries(player->getAccount());
  2983.  
  2984.     for (const VIPEntry& entry : vipEntries) {
  2985.         VipStatus_t vipStatus = VIPSTATUS_ONLINE;
  2986.  
  2987.         Player* vipPlayer = g_game.getPlayerByGUID(entry.guid);
  2988.  
  2989.         if (!vipPlayer || !player->canSeeCreature(vipPlayer)) {
  2990.             vipStatus = VIPSTATUS_OFFLINE;
  2991.         }
  2992.  
  2993.         sendVIP(entry.guid, entry.name, entry.description, entry.icon, entry.notify, vipStatus);
  2994.     }
  2995. }
  2996.  
  2997. void ProtocolGame::sendSpellCooldown(uint8_t spellId, uint32_t time)
  2998. {
  2999.     NetworkMessage msg;
  3000.     msg.addByte(0xA4);
  3001.     msg.addByte(spellId);
  3002.     msg.add<uint32_t>(time);
  3003.     writeToOutputBuffer(msg);
  3004. }
  3005.  
  3006. void ProtocolGame::sendSpellGroupCooldown(SpellGroup_t groupId, uint32_t time)
  3007. {
  3008.     NetworkMessage msg;
  3009.     msg.addByte(0xA5);
  3010.     msg.addByte(groupId);
  3011.     msg.add<uint32_t>(time);
  3012.     writeToOutputBuffer(msg);
  3013. }
  3014.  
  3015. void ProtocolGame::sendModalWindow(const ModalWindow& modalWindow)
  3016. {
  3017.     NetworkMessage msg;
  3018.     msg.addByte(0xFA);
  3019.  
  3020.     msg.add<uint32_t>(modalWindow.id);
  3021.     msg.addString(modalWindow.title);
  3022.     msg.addString(modalWindow.message);
  3023.  
  3024.     msg.addByte(modalWindow.buttons.size());
  3025.     for (const auto& it : modalWindow.buttons) {
  3026.         msg.addString(it.first);
  3027.         msg.addByte(it.second);
  3028.     }
  3029.  
  3030.     msg.addByte(modalWindow.choices.size());
  3031.     for (const auto& it : modalWindow.choices) {
  3032.         msg.addString(it.first);
  3033.         msg.addByte(it.second);
  3034.     }
  3035.  
  3036.     msg.addByte(modalWindow.defaultEscapeButton);
  3037.     msg.addByte(modalWindow.defaultEnterButton);
  3038.     msg.addByte(modalWindow.priority ? 0x01 : 0x00);
  3039.  
  3040.     writeToOutputBuffer(msg);
  3041. }
  3042.  
  3043. ////////////// Add common messages
  3044. void ProtocolGame::AddCreature(NetworkMessage& msg, const Creature* creature, bool known, uint32_t remove)
  3045. {
  3046.     //CreatureType_t creatureType = creature->getType();
  3047.  
  3048.     const Player* otherPlayer = creature->getPlayer();
  3049.  
  3050.     if (known) {
  3051.         msg.add<uint16_t>(0x62);
  3052.         msg.add<uint32_t>(creature->getID());
  3053.     } else {
  3054.         msg.add<uint16_t>(0x61);
  3055.         msg.add<uint32_t>(remove);
  3056.         msg.add<uint32_t>(creature->getID());
  3057.         //msg.addByte(creatureType);
  3058.         msg.addString(creature->getName());
  3059.     }
  3060.  
  3061.     if (creature->isHealthHidden()) {
  3062.         msg.addByte(0x00);
  3063.     } else {
  3064.         msg.addByte(std::ceil((static_cast<double>(creature->getHealth()) / std::max<int32_t>(creature->getMaxHealth(), 1)) * 100));
  3065.     }
  3066.  
  3067.     msg.addByte(creature->getDirection());
  3068.  
  3069.     if (!creature->isInGhostMode() && !creature->isInvisible()) {
  3070.         AddOutfit(msg, creature->getCurrentOutfit());
  3071.     } else {
  3072.         static Outfit_t outfit;
  3073.         AddOutfit(msg, outfit);
  3074.     }
  3075.  
  3076.     LightInfo lightInfo = creature->getCreatureLight();
  3077.     msg.addByte(player->isAccessPlayer() ? 0xFF : lightInfo.level);
  3078.     msg.addByte(lightInfo.color);
  3079.  
  3080.     //msg.add<uint16_t>(creature->getStepSpeed() / 2);
  3081.     msg.add<uint16_t>(creature->getStepSpeed());
  3082.  
  3083.     msg.addByte(player->getSkullClient(creature));
  3084.     msg.addByte(player->getPartyShield(otherPlayer));
  3085.  
  3086.     /*if (!known) {
  3087.         msg.addByte(player->getGuildEmblem(otherPlayer));
  3088.     }
  3089.  
  3090.     if (creatureType == CREATURETYPE_MONSTER) {
  3091.         const Creature* master = creature->getMaster();
  3092.         if (master) {
  3093.             const Player* masterPlayer = master->getPlayer();
  3094.             if (masterPlayer) {
  3095.                 if (masterPlayer == player) {
  3096.                     creatureType = CREATURETYPE_SUMMON_OWN;
  3097.                 } else {
  3098.                     creatureType = CREATURETYPE_SUMMON_OTHERS;
  3099.                 }
  3100.             }
  3101.         }
  3102.     }
  3103.  
  3104.     msg.addByte(creatureType); // Type (for summons)
  3105.     msg.addByte(creature->getSpeechBubble());
  3106.     msg.addByte(0xFF); // MARK_UNMARKED
  3107.    
  3108.  
  3109.     if (otherPlayer) {
  3110.         msg.add<uint16_t>(otherPlayer->getHelpers());
  3111.     } else {
  3112.         msg.add<uint16_t>(0x00);
  3113.     }
  3114.  
  3115.     msg.addByte(player->canWalkthroughEx(creature) ? 0x00 : 0x01);
  3116.     */
  3117. }
  3118.  
  3119. void ProtocolGame::AddPlayerStats(NetworkMessage& msg)
  3120. {
  3121.     msg.addByte(0xA0);
  3122.  
  3123.     msg.add<uint16_t>(std::min<int32_t>(player->getHealth(), std::numeric_limits<uint16_t>::max()));
  3124.     msg.add<uint16_t>(std::min<int32_t>(player->getMaxHealth(), std::numeric_limits<uint16_t>::max()));
  3125.  
  3126.     //msg.add<uint32_t>(player->getFreeCapacity());
  3127.     //msg.add<uint32_t>(player->getCapacity());
  3128.     msg.add<uint16_t>(player->getFreeCapacity() / 100);
  3129.  
  3130.     //msg.add<uint64_t>(player->getExperience());
  3131.     msg.add<uint32_t>(std::min<uint32_t>(player->getExperience(), 0x7FFFFFFF)); // tibia client debugs if value is higher than 0x7FFFFFFF
  3132.  
  3133.     msg.add<uint16_t>(player->getLevel());
  3134.     msg.addByte(player->getLevelPercent());
  3135.  
  3136.     /*msg.add<uint16_t>(100); // base xp gain rate
  3137.     msg.add<uint16_t>(0); // xp voucher
  3138.     msg.add<uint16_t>(0); // low level bonus
  3139.     msg.add<uint16_t>(0); // xp boost
  3140.     msg.add<uint16_t>(100); // stamina multiplier (100 = x1.0)
  3141.     */
  3142.  
  3143.     msg.add<uint16_t>(std::min<int32_t>(player->getMana(), std::numeric_limits<uint16_t>::max()));
  3144.     msg.add<uint16_t>(std::min<int32_t>(player->getMaxMana(), std::numeric_limits<uint16_t>::max()));
  3145.  
  3146.     msg.addByte(std::min<uint32_t>(player->getMagicLevel(), std::numeric_limits<uint8_t>::max()));
  3147.     //msg.addByte(std::min<uint32_t>(player->getBaseMagicLevel(), std::numeric_limits<uint8_t>::max()));
  3148.     msg.addByte(player->getMagicLevelPercent());
  3149.  
  3150.     msg.addByte(player->getSoul());
  3151.  
  3152.     /*msg.add<uint16_t>(player->getStaminaMinutes());
  3153.  
  3154.     msg.add<uint16_t>(player->getBaseSpeed() / 2);
  3155.  
  3156.     Condition* condition = player->getCondition(CONDITION_REGENERATION, CONDITIONID_DEFAULT);
  3157.     msg.add<uint16_t>(condition ? condition->getTicks() / 1000 : 0x00);
  3158.  
  3159.     msg.add<uint16_t>(player->getOfflineTrainingTime() / 60 / 1000);
  3160.  
  3161.     msg.add<uint16_t>(0); // xp boost time (seconds)
  3162.     msg.addByte(0); // enables exp boost in the store
  3163.     */
  3164. }
  3165.  
  3166. void ProtocolGame::AddPlayerSkills(NetworkMessage& msg)
  3167. {
  3168.     msg.addByte(0xA1);
  3169.  
  3170.     for (uint8_t i = SKILL_FIRST; i <= SKILL_LAST; ++i) {
  3171.         //msg.add<uint16_t>(std::min<int32_t>(player->getSkillLevel(i), std::numeric_limits<uint16_t>::max()));
  3172.         //msg.add<uint16_t>(player->getBaseSkill(i));
  3173.         msg.addByte(std::min<int32_t>(player->getSkillLevel(i), std::numeric_limits<uint8_t>::max()));
  3174.         msg.addByte(player->getSkillPercent(i));
  3175.     }
  3176.  
  3177.     /*for (uint8_t i = SPECIALSKILL_FIRST; i <= SPECIALSKILL_LAST; ++i) {
  3178.         msg.add<uint16_t>(std::min<int32_t>(100, player->varSpecialSkills[i]));
  3179.         msg.add<uint16_t>(0);
  3180.     }*/
  3181. }
  3182.  
  3183. void ProtocolGame::AddOutfit(NetworkMessage& msg, const Outfit_t& outfit)
  3184. {
  3185.     msg.add<uint16_t>(outfit.lookType);
  3186.  
  3187.     if (outfit.lookType != 0) {
  3188.         msg.addByte(outfit.lookHead);
  3189.         msg.addByte(outfit.lookBody);
  3190.         msg.addByte(outfit.lookLegs);
  3191.         msg.addByte(outfit.lookFeet);
  3192.         //msg.addByte(outfit.lookAddons);
  3193.     } else {
  3194.         msg.addItemId(outfit.lookTypeEx);
  3195.     }
  3196.  
  3197.     //msg.add<uint16_t>(outfit.lookMount);
  3198. }
  3199.  
  3200. void ProtocolGame::AddWorldLight(NetworkMessage& msg, LightInfo lightInfo)
  3201. {
  3202.     msg.addByte(0x82);
  3203.     msg.addByte((player->isAccessPlayer() ? 0xFF : lightInfo.level));
  3204.     msg.addByte(lightInfo.color);
  3205. }
  3206.  
  3207. void ProtocolGame::AddCreatureLight(NetworkMessage& msg, const Creature* creature)
  3208. {
  3209.     LightInfo lightInfo = creature->getCreatureLight();
  3210.  
  3211.     msg.addByte(0x8D);
  3212.     msg.add<uint32_t>(creature->getID());
  3213.     msg.addByte((player->isAccessPlayer() ? 0xFF : lightInfo.level));
  3214.     msg.addByte(lightInfo.color);
  3215. }
  3216.  
  3217. //tile
  3218. void ProtocolGame::RemoveTileThing(NetworkMessage& msg, const Position& pos, uint32_t stackpos)
  3219. {
  3220.     if (stackpos >= 10) {
  3221.         return;
  3222.     }
  3223.  
  3224.     msg.addByte(0x6C);
  3225.     msg.addPosition(pos);
  3226.     msg.addByte(stackpos);
  3227. }
  3228.  
  3229. void ProtocolGame::RemoveTileCreature(NetworkMessage& msg, const Creature* creature, const Position& pos, uint32_t stackpos)
  3230. {
  3231.     if (stackpos < 10) {
  3232.         RemoveTileThing(msg, pos, stackpos);
  3233.         return;
  3234.     }
  3235.  
  3236.     msg.addByte(0x6C);
  3237.     msg.add<uint16_t>(0xFFFF);
  3238.     msg.add<uint32_t>(creature->getID());
  3239. }
  3240.  
  3241. void ProtocolGame::MoveUpCreature(NetworkMessage& msg, const Creature* creature, const Position& newPos, const Position& oldPos)
  3242. {
  3243.     if (creature != player) {
  3244.         return;
  3245.     }
  3246.  
  3247.     //floor change up
  3248.     msg.addByte(0xBE);
  3249.  
  3250.     //going to surface
  3251.     if (newPos.z == 7) {
  3252.         int32_t skip = -1;
  3253.  
  3254.         // floor 7 and 6 already set
  3255.         for (int i = 5; i >= 0; --i) {
  3256.             GetFloorDescription(msg, oldPos.x - Map::maxClientViewportX, oldPos.y - Map::maxClientViewportY, i, (Map::maxClientViewportX * 2) + 2, (Map::maxClientViewportY * 2) + 2, 8 - i, skip);
  3257.         }
  3258.         if (skip >= 0) {
  3259.             msg.addByte(skip);
  3260.             msg.addByte(0xFF);
  3261.         }
  3262.     }
  3263.     //underground, going one floor up (still underground)
  3264.     else if (newPos.z > 7) {
  3265.         int32_t skip = -1;
  3266.         GetFloorDescription(msg, oldPos.x - Map::maxClientViewportX, oldPos.y - Map::maxClientViewportY, oldPos.getZ() - 3, (Map::maxClientViewportX * 2) + 2, (Map::maxClientViewportY * 2) + 2, 3, skip);
  3267.  
  3268.         if (skip >= 0) {
  3269.             msg.addByte(skip);
  3270.             msg.addByte(0xFF);
  3271.         }
  3272.     }
  3273.  
  3274.     //moving up a floor up makes us out of sync
  3275.     //west
  3276.     msg.addByte(0x68);
  3277.     GetMapDescription(oldPos.x - Map::maxClientViewportX, oldPos.y - (Map::maxClientViewportY - 1), newPos.z, 1, (Map::maxClientViewportY * 2) + 2, msg);
  3278.  
  3279.     //north
  3280.     msg.addByte(0x65);
  3281.     GetMapDescription(oldPos.x - Map::maxClientViewportX, oldPos.y - Map::maxClientViewportY, newPos.z, (Map::maxClientViewportX * 2) + 2, 1, msg);
  3282. }
  3283.  
  3284. void ProtocolGame::MoveDownCreature(NetworkMessage& msg, const Creature* creature, const Position& newPos, const Position& oldPos)
  3285. {
  3286.     if (creature != player) {
  3287.         return;
  3288.     }
  3289.  
  3290.     //floor change down
  3291.     msg.addByte(0xBF);
  3292.  
  3293.     //going from surface to underground
  3294.     if (newPos.z == 8) {
  3295.         int32_t skip = -1;
  3296.  
  3297.         for (int i = 0; i < 3; ++i) {
  3298.             GetFloorDescription(msg, oldPos.x - Map::maxClientViewportX, oldPos.y - Map::maxClientViewportY, newPos.z + i, (Map::maxClientViewportX * 2) + 2, (Map::maxClientViewportY * 2) + 2, -i - 1, skip);
  3299.         }
  3300.         if (skip >= 0) {
  3301.             msg.addByte(skip);
  3302.             msg.addByte(0xFF);
  3303.         }
  3304.     }
  3305.     //going further down
  3306.     else if (newPos.z > oldPos.z && newPos.z > 8 && newPos.z < 14) {
  3307.         int32_t skip = -1;
  3308.         GetFloorDescription(msg, oldPos.x - Map::maxClientViewportX, oldPos.y - Map::maxClientViewportY, newPos.z + 2, (Map::maxClientViewportX * 2) + 2, (Map::maxClientViewportY * 2) + 2, -3, skip);
  3309.  
  3310.         if (skip >= 0) {
  3311.             msg.addByte(skip);
  3312.             msg.addByte(0xFF);
  3313.         }
  3314.     }
  3315.  
  3316.     //moving down a floor makes us out of sync
  3317.     //east
  3318.     msg.addByte(0x66);
  3319.     GetMapDescription(oldPos.x + (Map::maxClientViewportX + 1), oldPos.y - (Map::maxClientViewportY + 1), newPos.z, 1, (Map::maxClientViewportY * 2) + 2, msg);
  3320.  
  3321.     //south
  3322.     msg.addByte(0x67);
  3323.     GetMapDescription(oldPos.x - Map::maxClientViewportX, oldPos.y + (Map::maxClientViewportY + 1), newPos.z, (Map::maxClientViewportX * 2) + 2, 1, msg);
  3324. }
  3325.  
  3326. void ProtocolGame::AddShopItem(NetworkMessage& msg, const ShopInfo& item)
  3327. {
  3328.     const ItemType& it = Item::items[item.itemId];
  3329.     msg.add<uint16_t>(it.clientId);
  3330.  
  3331.     if (it.isSplash() || it.isFluidContainer()) {
  3332.         msg.addByte(serverFluidToClient(item.subType));
  3333.     } else {
  3334.         msg.addByte(0x00);
  3335.     }
  3336.  
  3337.     msg.addString(item.realName);
  3338.     msg.add<uint32_t>(it.weight);
  3339.     msg.add<uint32_t>(item.buyPrice);
  3340.     msg.add<uint32_t>(item.sellPrice);
  3341. }
  3342.  
  3343. void ProtocolGame::parseExtendedOpcode(NetworkMessage& msg)
  3344. {
  3345.     uint8_t opcode = msg.getByte();
  3346.     const std::string& buffer = msg.getString();
  3347.  
  3348.     // process additional opcodes via lua script event
  3349.     addGameTask(&Game::parsePlayerExtendedOpcode, player->getID(), opcode, buffer);
  3350. }
  3351.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement