Oskar1121

Untitled

Sep 13th, 2018
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 82.10 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////
  2. // OpenTibia - an opensource roleplaying game
  3. ////////////////////////////////////////////////////////////////////////
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ////////////////////////////////////////////////////////////////////////
  17. #include "otpch.h"
  18. #include "resources.h"
  19.  
  20. #include <boost/function.hpp>
  21. #include <iostream>
  22.  
  23. #include "protocolgame.h"
  24. #include "textlogger.h"
  25.  
  26. #include "waitlist.h"
  27. #include "player.h"
  28.  
  29. #include "connection.h"
  30. #include "networkmessage.h"
  31. #include "outputmessage.h"
  32.  
  33. #include "iologindata.h"
  34. #include "ioban.h"
  35.  
  36. #include "items.h"
  37. #include "tile.h"
  38. #include "house.h"
  39.  
  40. #include "actions.h"
  41. #include "creatureevent.h"
  42. #include "quests.h"
  43.  
  44. #include "chat.h"
  45. #include "configmanager.h"
  46. #include "game.h"
  47.  
  48. #if defined(WINDOWS) && !defined(__CONSOLE__)
  49. #include "gui.h"
  50. #endif
  51.  
  52. extern Game g_game;
  53. extern ConfigManager g_config;
  54. extern Actions actions;
  55. extern CreatureEvents* g_creatureEvents;
  56. extern Chat g_chat;
  57.  
  58. template<class FunctionType>
  59. void ProtocolGame::addGameTaskInternal(uint32_t delay, const FunctionType& func)
  60. {
  61. if(delay > 0)
  62. Dispatcher::getInstance().addTask(createTask(delay, func));
  63. else
  64. Dispatcher::getInstance().addTask(createTask(func));
  65. }
  66.  
  67. #ifdef __ENABLE_SERVER_DIAGNOSTIC__
  68. uint32_t ProtocolGame::protocolGameCount = 0;
  69. #endif
  70.  
  71. void ProtocolGame::setPlayer(Player* p)
  72. {
  73. player = p;
  74. }
  75.  
  76. void ProtocolGame::releaseProtocol()
  77. {
  78. if(player && player->client == this)
  79. player->client = NULL;
  80.  
  81. Protocol::releaseProtocol();
  82. }
  83.  
  84. void ProtocolGame::deleteProtocolTask()
  85. {
  86. if(player)
  87. {
  88. g_game.freeThing(player);
  89. player = NULL;
  90. }
  91.  
  92. Protocol::deleteProtocolTask();
  93. }
  94.  
  95. bool ProtocolGame::login(const std::string& name, uint32_t id, const std::string& password,
  96. OperatingSystem_t operatingSystem, uint16_t version, bool gamemaster)
  97. {
  98. //dispatcher thread
  99. PlayerVector players = g_game.getPlayersByName(name);
  100. Player* _player = NULL;
  101. if(!players.empty())
  102. _player = players[random_range(0, (players.size() - 1))];
  103.  
  104. if(!_player || name == "Account Manager" || g_config.getNumber(ConfigManager::ALLOW_CLONES) > (int32_t)players.size())
  105. {
  106. player = new Player(name, this);
  107. player->addRef();
  108.  
  109. player->setID();
  110. if(!IOLoginData::getInstance()->loadPlayer(player, name, true))
  111. {
  112. disconnectClient(0x14, "Your character could not be loaded.");
  113. return false;
  114. }
  115.  
  116. Ban ban;
  117. ban.value = player->getID();
  118. ban.param = PLAYERBAN_BANISHMENT;
  119.  
  120. ban.type = BAN_PLAYER;
  121. if(IOBan::getInstance()->getData(ban) && !player->hasFlag(PlayerFlag_CannotBeBanned))
  122. {
  123. bool deletion = ban.expires < 0;
  124. std::string name_ = "Automatic ";
  125. if(!ban.adminId)
  126. name_ += (deletion ? "deletion" : "banishment");
  127. else
  128. IOLoginData::getInstance()->getNameByGuid(ban.adminId, name_, true);
  129.  
  130. char buffer[500 + ban.comment.length()];
  131. sprintf(buffer, "Your character has been %s at:\n%s by: %s,\nfor the following reason:\n%s.\nThe action taken was:\n%s.\nThe comment given was:\n%s.\nYour %s%s.",
  132. (deletion ? "deleted" : "banished"), formatDateShort(ban.added).c_str(), name_.c_str(),
  133. getReason(ban.reason).c_str(), getAction(ban.action, false).c_str(), ban.comment.c_str(),
  134. (deletion ? "character won't be undeleted" : "banishment will be lifted at:\n"),
  135. (deletion ? "." : formatDateShort(ban.expires, true).c_str()));
  136.  
  137. disconnectClient(0x14, buffer);
  138. return false;
  139. }
  140.  
  141. if(IOBan::getInstance()->isPlayerBanished(player->getGUID(), PLAYERBAN_LOCK) && id != 1)
  142. {
  143. if(g_config.getBool(ConfigManager::NAMELOCK_MANAGER))
  144. {
  145. player->name = "Account Manager";
  146. player->accountManager = MANAGER_NAMELOCK;
  147.  
  148. player->managerNumber = id;
  149. player->managerString2 = name;
  150. }
  151. else
  152. {
  153. disconnectClient(0x14, "Your character has been namelocked.");
  154. return false;
  155. }
  156. }
  157. else if(player->getName() == "Account Manager" && g_config.getBool(ConfigManager::ACCOUNT_MANAGER))
  158. {
  159. if(id != 1)
  160. {
  161. player->accountManager = MANAGER_ACCOUNT;
  162. player->managerNumber = id;
  163. }
  164. else
  165. player->accountManager = MANAGER_NEW;
  166. }
  167.  
  168. if(gamemaster && !player->hasCustomFlag(PlayerCustomFlag_GamemasterPrivileges))
  169. {
  170. disconnectClient(0x14, "You are not a gamemaster! Turn off the gamemaster mode in your IP changer.");
  171. return false;
  172. }
  173.  
  174. if(!player->hasFlag(PlayerFlag_CanAlwaysLogin))
  175. {
  176. if(g_game.getGameState() == GAME_STATE_CLOSING)
  177. {
  178. disconnectClient(0x14, "Gameworld is just going down, please come back later.");
  179. return false;
  180. }
  181.  
  182. if(g_game.getGameState() == GAME_STATE_CLOSED)
  183. {
  184. disconnectClient(0x14, "Gameworld is currently closed, please come back later.");
  185. return false;
  186. }
  187. }
  188.  
  189. if(g_config.getBool(ConfigManager::ONE_PLAYER_ON_ACCOUNT) && !player->isAccountManager() &&
  190. !IOLoginData::getInstance()->hasCustomFlag(id, PlayerCustomFlag_CanLoginMultipleCharacters))
  191. {
  192. bool found = false;
  193. PlayerVector tmp = g_game.getPlayersByAccount(id);
  194. for(PlayerVector::iterator it = tmp.begin(); it != tmp.end(); ++it)
  195. {
  196. if((*it)->getName() != name)
  197. continue;
  198.  
  199. found = true;
  200. break;
  201. }
  202.  
  203. if(tmp.size() > 0 && !found)
  204. {
  205. disconnectClient(0x14, "You may only login with one character\nof your account at the same time.");
  206. return false;
  207. }
  208. }
  209.  
  210. if(!WaitingList::getInstance()->login(player))
  211. {
  212. if(OutputMessage_ptr output = OutputMessagePool::getInstance()->getOutputMessage(this, false))
  213. {
  214. TRACK_MESSAGE(output);
  215. std::stringstream ss;
  216. ss << "Too many players online.\n" << "You are ";
  217.  
  218. int32_t slot = WaitingList::getInstance()->getSlot(player);
  219. if(slot)
  220. {
  221. ss << "at ";
  222. if(slot > 0)
  223. ss << slot;
  224. else
  225. ss << "unknown";
  226.  
  227. ss << " place on the waiting list.";
  228. }
  229. else
  230. ss << "awaiting connection...";
  231.  
  232. output->AddByte(0x16);
  233. output->AddString(ss.str());
  234. output->AddByte(WaitingList::getTime(slot));
  235. OutputMessagePool::getInstance()->send(output);
  236. }
  237.  
  238. getConnection()->close();
  239. return false;
  240. }
  241.  
  242. if(!IOLoginData::getInstance()->loadPlayer(player, name))
  243. {
  244. disconnectClient(0x14, "Your character could not be loaded.");
  245. return false;
  246. }
  247.  
  248. player->setOperatingSystem(operatingSystem);
  249. player->setClientVersion(version);
  250. if(!g_game.placeCreature(player, player->getLoginPosition()) && !g_game.placeCreature(player, player->getMasterPosition(), false, true))
  251. {
  252. disconnectClient(0x14, "Temple position is wrong. Contact with the administration.");
  253. return false;
  254.  
  255. }
  256.  
  257. player->lastIP = player->getIP();
  258. player->lastLoad = OTSYS_TIME();
  259. player->lastLogin = std::max(time(NULL), player->lastLogin + 1);
  260.  
  261. m_acceptPackets = true;
  262. return true;
  263. }
  264. else if(_player->client)
  265. {
  266. if(m_eventConnect || !g_config.getBool(ConfigManager::REPLACE_KICK_ON_LOGIN))
  267. {
  268. //A task has already been scheduled just bail out (should not be overriden)
  269. disconnectClient(0x14, "You are already logged in.");
  270. return false;
  271. }
  272.  
  273. g_chat.removeUserFromAllChannels(_player);
  274. _player->disconnect();
  275. _player->isConnecting = true;
  276.  
  277. addRef();
  278. m_eventConnect = Scheduler::getInstance().addEvent(createSchedulerTask(
  279. 1000, boost::bind(&ProtocolGame::connect, this, _player->getID(), operatingSystem, version)));
  280. return true;
  281. }
  282.  
  283. addRef();
  284. return connect(_player->getID(), operatingSystem, version);
  285. }
  286.  
  287. bool ProtocolGame::logout(bool displayEffect, bool forceLogout)
  288. {
  289. //dispatcher thread
  290. if(!player)
  291. return false;
  292.  
  293. if(!player->isRemoved())
  294. {
  295. if(!forceLogout)
  296. {
  297. if(!IOLoginData::getInstance()->hasCustomFlag(player->getAccount(), PlayerCustomFlag_CanLogoutAnytime))
  298. {
  299. if(player->getTile()->hasFlag(TILESTATE_NOLOGOUT))
  300. {
  301. player->sendCancelMessage(RET_YOUCANNOTLOGOUTHERE);
  302. return false;
  303. }
  304.  
  305. if(player->hasCondition(CONDITION_INFIGHT))
  306. {
  307. player->sendCancelMessage(RET_YOUMAYNOTLOGOUTDURINGAFIGHT);
  308. return false;
  309. }
  310.  
  311. if(!g_creatureEvents->playerLogout(player, false)) //let the script handle the error message
  312. return false;
  313. }
  314. else
  315. g_creatureEvents->playerLogout(player, false);
  316. }
  317. else if(!g_creatureEvents->playerLogout(player, true))
  318. return false;
  319. }
  320. else
  321. displayEffect = false;
  322.  
  323. if(displayEffect && !player->isGhost())
  324. g_game.addMagicEffect(player->getPosition(), MAGIC_EFFECT_POFF);
  325.  
  326. if(Connection_ptr connection = getConnection())
  327. connection->close();
  328.  
  329. return g_game.removeCreature(player);
  330. }
  331.  
  332. bool ProtocolGame::connect(uint32_t playerId, OperatingSystem_t operatingSystem, uint16_t version)
  333. {
  334. unRef();
  335. m_eventConnect = 0;
  336.  
  337. Player* _player = g_game.getPlayerByID(playerId);
  338. if(!_player || _player->isRemoved() || _player->client)
  339. {
  340. disconnectClient(0x14, "You are already logged in.");
  341. return false;
  342. }
  343.  
  344. player = _player;
  345. player->addRef();
  346. player->isConnecting = false;
  347.  
  348. player->client = this;
  349. player->sendCreatureAppear(player);
  350.  
  351. player->setOperatingSystem(operatingSystem);
  352. player->setClientVersion(version);
  353.  
  354. player->lastIP = player->getIP();
  355. player->lastLoad = OTSYS_TIME();
  356. player->lastLogin = std::max(time(NULL), player->lastLogin + 1);
  357.  
  358. m_acceptPackets = true;
  359. return true;
  360. }
  361.  
  362. void ProtocolGame::disconnect()
  363. {
  364. if(getConnection())
  365. getConnection()->close();
  366. }
  367.  
  368. void ProtocolGame::disconnectClient(uint8_t error, const char* message)
  369. {
  370. if(OutputMessage_ptr output = OutputMessagePool::getInstance()->getOutputMessage(this, false))
  371. {
  372. TRACK_MESSAGE(output);
  373. output->AddByte(error);
  374. output->AddString(message);
  375. OutputMessagePool::getInstance()->send(output);
  376. }
  377.  
  378. disconnect();
  379. }
  380.  
  381. void ProtocolGame::onConnect()
  382. {
  383. if(OutputMessage_ptr output = OutputMessagePool::getInstance()->getOutputMessage(this, false))
  384. {
  385. TRACK_MESSAGE(output);
  386. enableChecksum();
  387.  
  388. output->AddByte(0x1F);
  389. output->AddU16(random_range(0, 0xFFFF));
  390. output->AddU16(0x00);
  391. output->AddByte(random_range(0, 0xFF));
  392.  
  393. OutputMessagePool::getInstance()->send(output);
  394. }
  395. }
  396.  
  397. void ProtocolGame::onRecvFirstMessage(NetworkMessage& msg)
  398. {
  399. parseFirstPacket(msg);
  400. }
  401.  
  402. bool ProtocolGame::parseFirstPacket(NetworkMessage& msg)
  403. {
  404. if(
  405. #if defined(WINDOWS) && !defined(__CONSOLE__)
  406. !GUI::getInstance()->m_connections ||
  407. #endif
  408. g_game.getGameState() == GAME_STATE_SHUTDOWN)
  409. {
  410. getConnection()->close();
  411. return false;
  412. }
  413.  
  414. OperatingSystem_t operatingSystem = (OperatingSystem_t)msg.GetU16();
  415. uint16_t version = msg.GetU16();
  416. if(!RSA_decrypt(msg))
  417. {
  418. getConnection()->close();
  419. return false;
  420. }
  421.  
  422. uint32_t key[4] = {msg.GetU32(), msg.GetU32(), msg.GetU32(), msg.GetU32()};
  423. enableXTEAEncryption();
  424. setXTEAKey(key);
  425.  
  426. bool gamemaster = msg.GetByte();
  427. std::string name = msg.GetString(), character = msg.GetString(), password = msg.GetString();
  428.  
  429. msg.SkipBytes(6); //841- wtf?
  430. if(version < CLIENT_VERSION_MIN || version > CLIENT_VERSION_MAX)
  431. {
  432. disconnectClient(0x14, CLIENT_VERSION_STRING);
  433. return false;
  434. }
  435.  
  436. if(name.empty())
  437. {
  438. if(!g_config.getBool(ConfigManager::ACCOUNT_MANAGER))
  439. {
  440. disconnectClient(0x14, "Invalid account name.");
  441. return false;
  442. }
  443.  
  444. name = "1";
  445. password = "1";
  446. }
  447.  
  448. if(g_game.getGameState() < GAME_STATE_NORMAL)
  449. {
  450. disconnectClient(0x14, "Gameworld is just starting up, please wait.");
  451. return false;
  452. }
  453.  
  454. if(g_game.getGameState() == GAME_STATE_MAINTAIN)
  455. {
  456. disconnectClient(0x14, "Gameworld is under maintenance, please re-connect in a while.");
  457. return false;
  458. }
  459.  
  460. if(ConnectionManager::getInstance()->isDisabled(getIP(), protocolId))
  461. {
  462. disconnectClient(0x14, "Too many connections attempts from your IP address, please try again later.");
  463. return false;
  464. }
  465.  
  466. if(IOBan::getInstance()->isIpBanished(getIP()))
  467. {
  468. disconnectClient(0x14, "Your IP is banished!");
  469. return false;
  470. }
  471.  
  472. uint32_t id = 1;
  473. if(!IOLoginData::getInstance()->getAccountId(name, id))
  474. {
  475. ConnectionManager::getInstance()->addAttempt(getIP(), protocolId, false);
  476. disconnectClient(0x14, "Invalid account name.");
  477. return false;
  478. }
  479.  
  480. std::string hash;
  481. if(!IOLoginData::getInstance()->getPassword(id, hash, character) || !encryptTest(password, hash))
  482. {
  483. ConnectionManager::getInstance()->addAttempt(getIP(), protocolId, false);
  484. disconnectClient(0x14, "Invalid password.");
  485. return false;
  486. }
  487.  
  488. Ban ban;
  489. ban.value = id;
  490.  
  491. ban.type = BAN_ACCOUNT;
  492. if(IOBan::getInstance()->getData(ban) && !IOLoginData::getInstance()->hasFlag(id, PlayerFlag_CannotBeBanned))
  493. {
  494. bool deletion = ban.expires < 0;
  495. std::string name_ = "Automatic ";
  496. if(!ban.adminId)
  497. name_ += (deletion ? "deletion" : "banishment");
  498. else
  499. IOLoginData::getInstance()->getNameByGuid(ban.adminId, name_, true);
  500.  
  501. char buffer[500 + ban.comment.length()];
  502. sprintf(buffer, "Your account has been %s at:\n%s by: %s,\nfor the following reason:\n%s.\nThe action taken was:\n%s.\nThe comment given was:\n%s.\nYour %s%s.",
  503. (deletion ? "deleted" : "banished"), formatDateShort(ban.added).c_str(), name_.c_str(),
  504. getReason(ban.reason).c_str(), getAction(ban.action, false).c_str(), ban.comment.c_str(),
  505. (deletion ? "account won't be undeleted" : "banishment will be lifted at:\n"),
  506. (deletion ? "." : formatDateShort(ban.expires, true).c_str()));
  507.  
  508. disconnectClient(0x14, buffer);
  509. return false;
  510. }
  511.  
  512. ConnectionManager::getInstance()->addAttempt(getIP(), protocolId, true);
  513. Dispatcher::getInstance().addTask(createTask(boost::bind(
  514. &ProtocolGame::login, this, character, id, password, operatingSystem, version, gamemaster)));
  515. return true;
  516. }
  517.  
  518. void ProtocolGame::parsePacket(NetworkMessage &msg)
  519. {
  520. if(!player || !m_acceptPackets || g_game.getGameState() == GAME_STATE_SHUTDOWN
  521. || msg.getMessageLength() <= 0)
  522. return;
  523.  
  524. uint8_t recvbyte = msg.GetByte();
  525. //a dead player cannot performs actions
  526. if(player->isRemoved() && recvbyte != 0x14)
  527. return;
  528.  
  529. if(player->isAccountManager())
  530. {
  531. switch(recvbyte)
  532. {
  533. case 0x14:
  534. parseLogout(msg);
  535. break;
  536.  
  537. case 0x96:
  538. parseSay(msg);
  539. break;
  540.  
  541. default:
  542. sendCancelWalk();
  543. break;
  544. }
  545. }
  546. else
  547. {
  548. switch(recvbyte)
  549. {
  550. case 0x14: // logout
  551. parseLogout(msg);
  552. break;
  553.  
  554. case 0x1E: // keep alive / ping response
  555. parseReceivePing(msg);
  556. break;
  557.  
  558. case 0x64: // move with steps
  559. parseAutoWalk(msg);
  560. break;
  561.  
  562. case 0x65: // move north
  563. case 0x66: // move east
  564. case 0x67: // move south
  565. case 0x68: // move west
  566. parseMove(msg, (Direction)(recvbyte - 0x65));
  567. break;
  568.  
  569. case 0x69: // stop-autowalk
  570. addGameTask(&Game::playerStopAutoWalk, player->getID());
  571. break;
  572.  
  573. case 0x6A:
  574. parseMove(msg, NORTHEAST);
  575. break;
  576.  
  577. case 0x6B:
  578. parseMove(msg, SOUTHEAST);
  579. break;
  580.  
  581. case 0x6C:
  582. parseMove(msg, SOUTHWEST);
  583. break;
  584.  
  585. case 0x6D:
  586. parseMove(msg, NORTHWEST);
  587. break;
  588.  
  589. case 0x6F: // turn north
  590. case 0x70: // turn east
  591. case 0x71: // turn south
  592. case 0x72: // turn west
  593. parseTurn(msg, (Direction)(recvbyte - 0x6F));
  594. break;
  595.  
  596. case 0x78: // throw item
  597. parseThrow(msg);
  598. break;
  599.  
  600. case 0x79: // description in shop window
  601. parseLookInShop(msg);
  602. break;
  603.  
  604. case 0x7A: // player bought from shop
  605. parsePlayerPurchase(msg);
  606. break;
  607.  
  608. case 0x7B: // player sold to shop
  609. parsePlayerSale(msg);
  610. break;
  611.  
  612. case 0x7C: // player closed shop window
  613. parseCloseShop(msg);
  614. break;
  615.  
  616. case 0x7D: // Request trade
  617. parseRequestTrade(msg);
  618. break;
  619.  
  620. case 0x7E: // Look at an item in trade
  621. parseLookInTrade(msg);
  622. break;
  623.  
  624. case 0x7F: // Accept trade
  625. parseAcceptTrade(msg);
  626. break;
  627.  
  628. case 0x80: // close/cancel trade
  629. parseCloseTrade();
  630. break;
  631.  
  632. case 0x82: // use item
  633. parseUseItem(msg);
  634. break;
  635.  
  636. case 0x83: // use item
  637. parseUseItemEx(msg);
  638. break;
  639.  
  640. case 0x84: // battle window
  641. parseBattleWindow(msg);
  642. break;
  643.  
  644. case 0x85: //rotate item
  645. parseRotateItem(msg);
  646. break;
  647.  
  648. case 0x87: // close container
  649. parseCloseContainer(msg);
  650. break;
  651.  
  652. case 0x88: //"up-arrow" - container
  653. parseUpArrowContainer(msg);
  654. break;
  655.  
  656. case 0x89:
  657. parseTextWindow(msg);
  658. break;
  659.  
  660. case 0x8A:
  661. parseHouseWindow(msg);
  662. break;
  663.  
  664. case 0x8C: // throw item
  665. parseLookAt(msg);
  666. break;
  667.  
  668. case 0x96: // say something
  669. parseSay(msg);
  670. break;
  671.  
  672. case 0x97: // request channels
  673. parseGetChannels(msg);
  674. break;
  675.  
  676. case 0x98: // open channel
  677. parseOpenChannel(msg);
  678. break;
  679.  
  680. case 0x99: // close channel
  681. parseCloseChannel(msg);
  682. break;
  683.  
  684. case 0x9A: // open priv
  685. parseOpenPriv(msg);
  686. break;
  687.  
  688. case 0x9B: //process report
  689. parseProcessRuleViolation(msg);
  690. break;
  691.  
  692. case 0x9C: //gm closes report
  693. parseCloseRuleViolation(msg);
  694. break;
  695.  
  696. case 110:
  697. parseAddMarketItem(msg);
  698. break;
  699.  
  700. case 115:
  701. parseUpdateMarket(msg);
  702. break;
  703.  
  704. case 116:
  705. parseUpdateOffer(msg);
  706. break;
  707.  
  708. case 117:
  709. parseUpdateMarketStatus(msg);
  710. break;
  711.  
  712. case 118:
  713. parseOpenReofferWindow(msg);
  714. break;
  715.  
  716. case 70:
  717. parseLookMarketItem(msg);
  718. break;
  719.  
  720. case 0x9D: //player cancels report
  721. parseCancelRuleViolation(msg);
  722. break;
  723.  
  724. case 0x9E: // close NPC
  725. parseCloseNpc(msg);
  726. break;
  727.  
  728. case 0xA0: // set attack and follow mode
  729. parseFightModes(msg);
  730. break;
  731.  
  732. case 0xA1: // attack
  733. parseAttack(msg);
  734. break;
  735.  
  736. case 0xA2: //follow
  737. parseFollow(msg);
  738. break;
  739.  
  740. case 0xA3: // invite party
  741. parseInviteToParty(msg);
  742. break;
  743.  
  744. case 0xA4: // join party
  745. parseJoinParty(msg);
  746. break;
  747.  
  748. case 0xA5: // revoke party
  749. parseRevokePartyInvite(msg);
  750. break;
  751.  
  752. case 0xA6: // pass leadership
  753. parsePassPartyLeadership(msg);
  754. break;
  755.  
  756. case 0xA7: // leave party
  757. parseLeaveParty(msg);
  758. break;
  759.  
  760. case 0xA8: // share exp
  761. parseSharePartyExperience(msg);
  762. break;
  763.  
  764. case 0xAA:
  765. parseCreatePrivateChannel(msg);
  766. break;
  767.  
  768. case 0xAB:
  769. parseChannelInvite(msg);
  770. break;
  771.  
  772. case 0xAC:
  773. parseChannelExclude(msg);
  774. break;
  775.  
  776. case 0xBE: // cancel move
  777. parseCancelMove(msg);
  778. break;
  779.  
  780. case 0xC9: //client request to resend the tile
  781. parseUpdateTile(msg);
  782. break;
  783.  
  784. case 0xCA: //client request to resend the container (happens when you store more than container maxsize)
  785. parseUpdateContainer(msg);
  786. break;
  787.  
  788. case 0xD2: // request outfit
  789. if((!player->hasCustomFlag(PlayerCustomFlag_GamemasterPrivileges) || !g_config.getBool(
  790. ConfigManager::DISABLE_OUTFITS_PRIVILEGED)) && (g_config.getBool(ConfigManager::ALLOW_CHANGEOUTFIT)
  791. || g_config.getBool(ConfigManager::ALLOW_CHANGECOLORS) || g_config.getBool(ConfigManager::ALLOW_CHANGEADDONS)))
  792. parseRequestOutfit(msg);
  793. break;
  794.  
  795. case 0xD3: // set outfit
  796. if((!player->hasCustomFlag(PlayerCustomFlag_GamemasterPrivileges) || !g_config.getBool(ConfigManager::DISABLE_OUTFITS_PRIVILEGED))
  797. && (g_config.getBool(ConfigManager::ALLOW_CHANGECOLORS) || g_config.getBool(ConfigManager::ALLOW_CHANGEOUTFIT)))
  798. parseSetOutfit(msg);
  799. break;
  800.  
  801. case 0xDC:
  802. parseAddVip(msg);
  803. break;
  804.  
  805. case 0xDD:
  806. parseRemoveVip(msg);
  807. break;
  808.  
  809. case 0xE6:
  810. parseBugReport(msg);
  811. break;
  812.  
  813. case 0xE7:
  814. parseViolationWindow(msg);
  815. break;
  816.  
  817. case 0xE8:
  818. parseDebugAssert(msg);
  819. break;
  820.  
  821. case 0xF0:
  822. parseQuests(msg);
  823. break;
  824.  
  825. case 0xF1:
  826. parseQuestInfo(msg);
  827. break;
  828.  
  829. default:
  830. {
  831. /*if(g_config.getBool(ConfigManager::BAN_UNKNOWN_BYTES))
  832. {
  833. int64_t banTime = -1;
  834. ViolationAction_t action = ACTION_BANISHMENT;
  835. Account tmp = IOLoginData::getInstance()->loadAccount(player->getAccount(), true);
  836.  
  837. tmp.warnings++;
  838. if(tmp.warnings >= g_config.getNumber(ConfigManager::WARNINGS_TO_DELETION))
  839. action = ACTION_DELETION;
  840. else if(tmp.warnings >= g_config.getNumber(ConfigManager::WARNINGS_TO_FINALBAN))
  841. {
  842. banTime = time(NULL) + g_config.getNumber(ConfigManager::FINALBAN_LENGTH);
  843. action = ACTION_BANFINAL;
  844. }
  845. else
  846. banTime = time(NULL) + g_config.getNumber(ConfigManager::BAN_LENGTH);
  847.  
  848. if(IOBan::getInstance()->addAccountBanishment(tmp.number, banTime, 13, action,
  849. "Sending unknown packets to the server.", 0, player->getGUID()))
  850. {
  851. IOLoginData::getInstance()->saveAccount(tmp);
  852. player->sendTextMessage(MSG_INFO_DESCR, "You have been banished.");
  853.  
  854. g_game.addMagicEffect(player->getPosition(), MAGIC_EFFECT_WRAPS_GREEN);
  855. Scheduler::getInstance().addEvent(createSchedulerTask(1000, boost::bind(
  856. &Game::kickPlayer, &g_game, player->getID(), false)));
  857. }
  858. }
  859.  
  860. std::stringstream hex, s;
  861. hex << "0x" << std::hex << (int16_t)recvbyte << std::dec;
  862. s << player->getName() << " sent unknown byte: " << hex << std::endl;
  863.  
  864. LOG_MESSAGE(LOGTYPE_NOTICE, s.str(), "PLAYER")
  865. Logger::getInstance()->eFile(getFilePath(FILE_TYPE_LOG, "bots/" + player->getName() + ".log").c_str(),
  866. "[" + formatDate() + "] Received byte " + hex.str(), false);*/
  867. break;
  868. }
  869. }
  870. }
  871. }
  872.  
  873. void ProtocolGame::GetTileDescription(const Tile* tile, NetworkMessage_ptr msg)
  874. {
  875. if(!tile)
  876. return;
  877.  
  878. int32_t count = 0;
  879. if(tile->ground)
  880. {
  881. msg->AddItem(tile->ground);
  882. count++;
  883. }
  884.  
  885. const TileItemVector* items = tile->getItemList();
  886. const CreatureVector* creatures = tile->getCreatures();
  887.  
  888. ItemVector::const_iterator it;
  889. if(items)
  890. {
  891. for(it = items->getBeginTopItem(); (it != items->getEndTopItem() && count < 10); ++it, ++count)
  892. msg->AddItem(*it);
  893. }
  894.  
  895. if(creatures)
  896. {
  897. for(CreatureVector::const_reverse_iterator cit = creatures->rbegin(); (cit != creatures->rend() && count < 10); ++cit)
  898. {
  899. if(!player->canSeeCreature(*cit))
  900. continue;
  901.  
  902. bool known;
  903. uint32_t removedKnown;
  904. checkCreatureAsKnown((*cit)->getID(), known, removedKnown);
  905.  
  906. AddCreature(msg, (*cit), known, removedKnown);
  907. count++;
  908. }
  909. }
  910.  
  911. if(items)
  912. {
  913. for(it = items->getBeginDownItem(); (it != items->getEndDownItem() && count < 10); ++it, ++count)
  914. msg->AddItem(*it);
  915. }
  916. }
  917.  
  918. void ProtocolGame::GetMapDescription(int32_t x, int32_t y, int32_t z,
  919. int32_t width, int32_t height, NetworkMessage_ptr msg)
  920. {
  921. int32_t skip = -1, startz, endz, zstep = 0;
  922. if(z > 7)
  923. {
  924. startz = z - 2;
  925. endz = std::min((int32_t)MAP_MAX_LAYERS - 1, z + 2);
  926. zstep = 1;
  927. }
  928. else
  929. {
  930. startz = 7;
  931. endz = 0;
  932. zstep = -1;
  933. }
  934.  
  935. for(int32_t nz = startz; nz != endz + zstep; nz += zstep)
  936. GetFloorDescription(msg, x, y, nz, width, height, z - nz, skip);
  937.  
  938. if(skip >= 0)
  939. {
  940. msg->AddByte(skip);
  941. msg->AddByte(0xFF);
  942. //cc += skip;
  943. }
  944. }
  945.  
  946. void ProtocolGame::GetFloorDescription(NetworkMessage_ptr msg, int32_t x, int32_t y, int32_t z,
  947. int32_t width, int32_t height, int32_t offset, int32_t& skip)
  948. {
  949. Tile* tile = NULL;
  950. for(int32_t nx = 0; nx < width; nx++)
  951. {
  952. for(int32_t ny = 0; ny < height; ny++)
  953. {
  954. if((tile = g_game.getTile(Position(x + nx + offset, y + ny + offset, z))))
  955. {
  956. if(skip >= 0)
  957. {
  958. msg->AddByte(skip);
  959. msg->AddByte(0xFF);
  960. }
  961.  
  962. skip = 0;
  963. GetTileDescription(tile, msg);
  964. }
  965. else
  966. {
  967. ++skip;
  968. if(skip == 0xFF)
  969. {
  970. msg->AddByte(0xFF);
  971. msg->AddByte(0xFF);
  972. skip = -1;
  973. }
  974. }
  975. }
  976. }
  977. }
  978.  
  979. void ProtocolGame::checkCreatureAsKnown(uint32_t id, bool& known, uint32_t& removedKnown)
  980. {
  981. // loop through the known creature list and check if the given creature is in
  982. for(std::list<uint32_t>::iterator it = knownCreatureList.begin(); it != knownCreatureList.end(); ++it)
  983. {
  984. if((*it) != id)
  985. continue;
  986.  
  987. // know... make the creature even more known...
  988. knownCreatureList.erase(it);
  989. knownCreatureList.push_back(id);
  990.  
  991. known = true;
  992. return;
  993. }
  994.  
  995. // ok, he is unknown...
  996. known = false;
  997. // ... but not in future
  998. knownCreatureList.push_back(id);
  999. // too many known creatures?
  1000. if(knownCreatureList.size() > 250)
  1001. {
  1002. // lets try to remove one from the end of the list
  1003. Creature* c = NULL;
  1004. for(int32_t n = 0; n < 250; n++)
  1005. {
  1006. removedKnown = knownCreatureList.front();
  1007. if(!(c = g_game.getCreatureByID(removedKnown)) || !canSee(c))
  1008. break;
  1009.  
  1010. // this creature we can't remove, still in sight, so back to the end
  1011. knownCreatureList.pop_front();
  1012. knownCreatureList.push_back(removedKnown);
  1013. }
  1014.  
  1015. // hopefully we found someone to remove :S, we got only 250 tries
  1016. // if not... lets kick some players with debug errors :)
  1017. knownCreatureList.pop_front();
  1018. }
  1019. else // we can cache without problems :)
  1020. removedKnown = 0;
  1021. }
  1022.  
  1023. bool ProtocolGame::canSee(const Creature* c) const
  1024. {
  1025. return !c->isRemoved() && player->canSeeCreature(c) && canSee(c->getPosition());
  1026. }
  1027.  
  1028. bool ProtocolGame::canSee(const Position& pos) const
  1029. {
  1030. return canSee(pos.x, pos.y, pos.z);
  1031. }
  1032.  
  1033. bool ProtocolGame::canSee(uint16_t x, uint16_t y, uint16_t z) const
  1034. {
  1035. #ifdef __DEBUG__
  1036. if(z < 0 || z >= MAP_MAX_LAYERS)
  1037. std::cout << "[Warning - ProtocolGame::canSee] Z-value is out of range!" << std::endl;
  1038. #endif
  1039.  
  1040. const Position& myPos = player->getPosition();
  1041. if(myPos.z <= 7)
  1042. {
  1043. //we are on ground level or above (7 -> 0), view is from 7 -> 0
  1044. if(z > 7)
  1045. return false;
  1046. }
  1047. else if(myPos.z >= 8 && std::abs(myPos.z - z) > 2) //we are underground (8 -> 15), view is +/- 2 from the floor we stand on
  1048. return false;
  1049.  
  1050. //negative offset means that the action taken place is on a lower floor than ourself
  1051. int32_t offsetz = myPos.z - z;
  1052. return ((x >= myPos.x - 14 + offsetz) && (x <= myPos.x + 15 + offsetz) &&
  1053. (y >= myPos.y - 8 + offsetz) && (y <= myPos.y + 9 + offsetz));
  1054. }
  1055.  
  1056. //********************** Parse methods *******************************//
  1057. void ProtocolGame::parseLogout(NetworkMessage& msg)
  1058. {
  1059. Dispatcher::getInstance().addTask(createTask(boost::bind(&ProtocolGame::logout, this, true, false)));
  1060. }
  1061.  
  1062. void ProtocolGame::parseCreatePrivateChannel(NetworkMessage& msg)
  1063. {
  1064. addGameTask(&Game::playerCreatePrivateChannel, player->getID());
  1065. }
  1066.  
  1067. void ProtocolGame::parseChannelInvite(NetworkMessage& msg)
  1068. {
  1069. const std::string name = msg.GetString();
  1070. addGameTask(&Game::playerChannelInvite, player->getID(), name);
  1071. }
  1072.  
  1073. void ProtocolGame::parseChannelExclude(NetworkMessage& msg)
  1074. {
  1075. const std::string name = msg.GetString();
  1076. addGameTask(&Game::playerChannelExclude, player->getID(), name);
  1077. }
  1078.  
  1079. void ProtocolGame::parseGetChannels(NetworkMessage& msg)
  1080. {
  1081. addGameTask(&Game::playerRequestChannels, player->getID());
  1082. }
  1083.  
  1084. void ProtocolGame::parseOpenChannel(NetworkMessage& msg)
  1085. {
  1086. uint16_t channelId = msg.GetU16();
  1087. addGameTask(&Game::playerOpenChannel, player->getID(), channelId);
  1088. }
  1089.  
  1090. void ProtocolGame::parseCloseChannel(NetworkMessage& msg)
  1091. {
  1092. uint16_t channelId = msg.GetU16();
  1093. addGameTask(&Game::playerCloseChannel, player->getID(), channelId);
  1094. }
  1095.  
  1096. void ProtocolGame::parseOpenPriv(NetworkMessage& msg)
  1097. {
  1098. const std::string receiver = msg.GetString();
  1099. addGameTask(&Game::playerOpenPrivateChannel, player->getID(), receiver);
  1100. }
  1101.  
  1102. void ProtocolGame::parseProcessRuleViolation(NetworkMessage& msg)
  1103. {
  1104. const std::string reporter = msg.GetString();
  1105. addGameTask(&Game::playerProcessRuleViolation, player->getID(), reporter);
  1106. }
  1107.  
  1108. void ProtocolGame::parseCloseRuleViolation(NetworkMessage& msg)
  1109. {
  1110. const std::string reporter = msg.GetString();
  1111. addGameTask(&Game::playerCloseRuleViolation, player->getID(), reporter);
  1112. }
  1113.  
  1114. void ProtocolGame::parseCancelRuleViolation(NetworkMessage& msg)
  1115. {
  1116. addGameTask(&Game::playerCancelRuleViolation, player->getID());
  1117. }
  1118.  
  1119. void ProtocolGame::parseCloseNpc(NetworkMessage& msg)
  1120. {
  1121. addGameTask(&Game::playerCloseNpcChannel, player->getID());
  1122. }
  1123.  
  1124. void ProtocolGame::parseCancelMove(NetworkMessage& msg)
  1125. {
  1126. addGameTask(&Game::playerCancelAttackAndFollow, player->getID());
  1127. }
  1128.  
  1129. void ProtocolGame::parseReceivePing(NetworkMessage& msg)
  1130. {
  1131. addGameTask(&Game::playerReceivePing, player->getID());
  1132. }
  1133.  
  1134. void ProtocolGame::parseAutoWalk(NetworkMessage& msg)
  1135. {
  1136. // first we get all directions...
  1137. std::list<Direction> path;
  1138. size_t dirCount = msg.GetByte();
  1139. for(size_t i = 0; i < dirCount; ++i)
  1140. {
  1141. uint8_t rawDir = msg.GetByte();
  1142. Direction dir = SOUTH;
  1143. switch(rawDir)
  1144. {
  1145. case 1:
  1146. dir = EAST;
  1147. break;
  1148. case 2:
  1149. dir = NORTHEAST;
  1150. break;
  1151. case 3:
  1152. dir = NORTH;
  1153. break;
  1154. case 4:
  1155. dir = NORTHWEST;
  1156. break;
  1157. case 5:
  1158. dir = WEST;
  1159. break;
  1160. case 6:
  1161. dir = SOUTHWEST;
  1162. break;
  1163. case 7:
  1164. dir = SOUTH;
  1165. break;
  1166. case 8:
  1167. dir = SOUTHEAST;
  1168. break;
  1169. default:
  1170. continue;
  1171. }
  1172.  
  1173. path.push_back(dir);
  1174. }
  1175.  
  1176. addGameTask(&Game::playerAutoWalk, player->getID(), path);
  1177. }
  1178.  
  1179. void ProtocolGame::parseMove(NetworkMessage& msg, Direction dir)
  1180. {
  1181. addGameTask(&Game::playerMove, player->getID(), dir);
  1182. }
  1183.  
  1184. void ProtocolGame::parseTurn(NetworkMessage& msg, Direction dir)
  1185. {
  1186. addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerTurn, player->getID(), dir);
  1187. }
  1188.  
  1189. void ProtocolGame::parseRequestOutfit(NetworkMessage& msg)
  1190. {
  1191. addGameTask(&Game::playerRequestOutfit, player->getID());
  1192. }
  1193.  
  1194. void ProtocolGame::parseSetOutfit(NetworkMessage& msg)
  1195. {
  1196. Outfit_t newOutfit = player->defaultOutfit;
  1197. if(g_config.getBool(ConfigManager::ALLOW_CHANGEOUTFIT))
  1198. newOutfit.lookType = msg.GetU16();
  1199. else
  1200. msg.SkipBytes(2);
  1201.  
  1202. if(g_config.getBool(ConfigManager::ALLOW_CHANGECOLORS))
  1203. {
  1204. newOutfit.lookHead = msg.GetByte();
  1205. newOutfit.lookBody = msg.GetByte();
  1206. newOutfit.lookLegs = msg.GetByte();
  1207. newOutfit.lookFeet = msg.GetByte();
  1208. }
  1209. else
  1210. msg.SkipBytes(4);
  1211.  
  1212. if(g_config.getBool(ConfigManager::ALLOW_CHANGEADDONS))
  1213. newOutfit.lookAddons = msg.GetByte();
  1214. else
  1215. msg.SkipBytes(1);
  1216.  
  1217. addGameTask(&Game::playerChangeOutfit, player->getID(), newOutfit);
  1218. }
  1219.  
  1220. void ProtocolGame::parseUseItem(NetworkMessage& msg)
  1221. {
  1222. Position pos = msg.GetPosition();
  1223. uint16_t spriteId = msg.GetSpriteId();
  1224. int16_t stackpos = msg.GetByte();
  1225. uint8_t index = msg.GetByte();
  1226. bool isHotkey = (pos.x == 0xFFFF && !pos.y && !pos.z);
  1227. addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerUseItem, player->getID(), pos, stackpos, index, spriteId, isHotkey);
  1228. }
  1229.  
  1230. void ProtocolGame::parseUseItemEx(NetworkMessage& msg)
  1231. {
  1232. Position fromPos = msg.GetPosition();
  1233. uint16_t fromSpriteId = msg.GetSpriteId();
  1234. int16_t fromStackpos = msg.GetByte();
  1235. Position toPos = msg.GetPosition();
  1236. uint16_t toSpriteId = msg.GetU16();
  1237. int16_t toStackpos = msg.GetByte();
  1238. bool isHotkey = (fromPos.x == 0xFFFF && !fromPos.y && !fromPos.z);
  1239. addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerUseItemEx, player->getID(),
  1240. fromPos, fromStackpos, fromSpriteId, toPos, toStackpos, toSpriteId, isHotkey);
  1241. }
  1242.  
  1243. void ProtocolGame::parseBattleWindow(NetworkMessage& msg)
  1244. {
  1245. Position fromPos = msg.GetPosition();
  1246. uint16_t spriteId = msg.GetSpriteId();
  1247. int16_t fromStackpos = msg.GetByte();
  1248. uint32_t creatureId = msg.GetU32();
  1249. bool isHotkey = (fromPos.x == 0xFFFF && !fromPos.y && !fromPos.z);
  1250. addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerUseBattleWindow, player->getID(), fromPos, fromStackpos, creatureId, spriteId, isHotkey);
  1251. }
  1252.  
  1253. void ProtocolGame::parseCloseContainer(NetworkMessage& msg)
  1254. {
  1255. uint8_t cid = msg.GetByte();
  1256. addGameTask(&Game::playerCloseContainer, player->getID(), cid);
  1257. }
  1258.  
  1259. void ProtocolGame::parseUpArrowContainer(NetworkMessage& msg)
  1260. {
  1261. uint8_t cid = msg.GetByte();
  1262. addGameTask(&Game::playerMoveUpContainer, player->getID(), cid);
  1263. }
  1264.  
  1265. void ProtocolGame::parseUpdateTile(NetworkMessage& msg)
  1266. {
  1267. Position pos = msg.GetPosition();
  1268. //addGameTask(&Game::playerUpdateTile, player->getID(), pos);
  1269. }
  1270.  
  1271. void ProtocolGame::parseUpdateContainer(NetworkMessage& msg)
  1272. {
  1273. uint8_t cid = msg.GetByte();
  1274. addGameTask(&Game::playerUpdateContainer, player->getID(), cid);
  1275. }
  1276.  
  1277. void ProtocolGame::parseThrow(NetworkMessage& msg)
  1278. {
  1279. Position fromPos = msg.GetPosition();
  1280. uint16_t spriteId = msg.GetSpriteId();
  1281. int16_t fromStackpos = msg.GetByte();
  1282. Position toPos = msg.GetPosition();
  1283. uint8_t count = msg.GetByte();
  1284. if(toPos != fromPos)
  1285. addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerMoveThing,
  1286. player->getID(), fromPos, spriteId, fromStackpos, toPos, count);
  1287. }
  1288.  
  1289. void ProtocolGame::parseLookAt(NetworkMessage& msg)
  1290. {
  1291. Position pos = msg.GetPosition();
  1292. uint16_t spriteId = msg.GetSpriteId();
  1293. int16_t stackpos = msg.GetByte();
  1294. addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerLookAt, player->getID(), pos, spriteId, stackpos);
  1295. }
  1296.  
  1297. void ProtocolGame::parseSay(NetworkMessage& msg)
  1298. {
  1299. std::string receiver;
  1300. uint16_t channelId = 0;
  1301.  
  1302. SpeakClasses type = (SpeakClasses)msg.GetByte();
  1303. switch(type)
  1304. {
  1305. case SPEAK_PRIVATE:
  1306. case SPEAK_PRIVATE_RED:
  1307. case SPEAK_RVR_ANSWER:
  1308. receiver = msg.GetString();
  1309. break;
  1310.  
  1311. case SPEAK_CHANNEL_Y:
  1312. case SPEAK_CHANNEL_RN:
  1313. case SPEAK_CHANNEL_RA:
  1314. channelId = msg.GetU16();
  1315. break;
  1316.  
  1317. default:
  1318. break;
  1319. }
  1320.  
  1321. const std::string text = msg.GetString();
  1322. if(text.length() > 255) //client limit
  1323. {
  1324. std::stringstream s;
  1325. s << text.length();
  1326.  
  1327. Logger::getInstance()->eFile("bots/" + player->getName() + ".log", "Attempt to send message with size " + s.str() + " - client is limited to 255 characters.", true);
  1328. return;
  1329. }
  1330.  
  1331. addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerSay, player->getID(), channelId, type, receiver, text);
  1332. }
  1333.  
  1334. void ProtocolGame::parseFightModes(NetworkMessage& msg)
  1335. {
  1336. uint8_t rawFightMode = msg.GetByte(); //1 - offensive, 2 - balanced, 3 - defensive
  1337. uint8_t rawChaseMode = msg.GetByte(); //0 - stand while fightning, 1 - chase opponent
  1338. uint8_t rawSecureMode = msg.GetByte(); //0 - can't attack unmarked, 1 - can attack unmarked
  1339.  
  1340. chaseMode_t chaseMode = CHASEMODE_STANDSTILL;
  1341. if(rawChaseMode == 1)
  1342. chaseMode = CHASEMODE_FOLLOW;
  1343.  
  1344. fightMode_t fightMode = FIGHTMODE_ATTACK;
  1345. if(rawFightMode == 2)
  1346. fightMode = FIGHTMODE_BALANCED;
  1347. else if(rawFightMode == 3)
  1348. fightMode = FIGHTMODE_DEFENSE;
  1349.  
  1350. secureMode_t secureMode = SECUREMODE_OFF;
  1351. if(rawSecureMode == 1)
  1352. secureMode = SECUREMODE_ON;
  1353.  
  1354. addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerSetFightModes, player->getID(), fightMode, chaseMode, secureMode);
  1355. }
  1356.  
  1357. void ProtocolGame::parseAttack(NetworkMessage& msg)
  1358. {
  1359. uint32_t creatureId = msg.GetU32();
  1360. addGameTask(&Game::playerSetAttackedCreature, player->getID(), creatureId);
  1361. }
  1362.  
  1363. void ProtocolGame::parseFollow(NetworkMessage& msg)
  1364. {
  1365. uint32_t creatureId = msg.GetU32();
  1366. addGameTask(&Game::playerFollowCreature, player->getID(), creatureId);
  1367. }
  1368.  
  1369. void ProtocolGame::parseTextWindow(NetworkMessage& msg)
  1370. {
  1371. uint32_t windowTextId = msg.GetU32();
  1372. const std::string newText = msg.GetString();
  1373. addGameTask(&Game::playerWriteItem, player->getID(), windowTextId, newText);
  1374. }
  1375.  
  1376. void ProtocolGame::parseHouseWindow(NetworkMessage &msg)
  1377. {
  1378. uint8_t doorId = msg.GetByte();
  1379. uint32_t id = msg.GetU32();
  1380. const std::string text = msg.GetString();
  1381. addGameTask(&Game::playerUpdateHouseWindow, player->getID(), doorId, id, text);
  1382. }
  1383.  
  1384. void ProtocolGame::parseLookInShop(NetworkMessage &msg)
  1385. {
  1386. uint16_t id = msg.GetU16();
  1387. uint16_t count = msg.GetByte();
  1388. addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerLookInShop, player->getID(), id, count);
  1389. }
  1390.  
  1391. void ProtocolGame::parsePlayerPurchase(NetworkMessage &msg)
  1392. {
  1393. uint16_t id = msg.GetU16();
  1394. uint16_t count = msg.GetByte();
  1395. uint16_t amount = msg.GetByte();
  1396. bool ignoreCap = msg.GetByte();
  1397. bool inBackpacks = msg.GetByte();
  1398. addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerPurchaseItem, player->getID(), id, count, amount, ignoreCap, inBackpacks);
  1399. }
  1400.  
  1401. void ProtocolGame::parsePlayerSale(NetworkMessage &msg)
  1402. {
  1403. uint16_t id = msg.GetU16();
  1404. uint16_t count = msg.GetByte();
  1405. uint16_t amount = msg.GetByte();
  1406. addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerSellItem, player->getID(), id, count, amount);
  1407. }
  1408.  
  1409. void ProtocolGame::parseCloseShop(NetworkMessage &msg)
  1410. {
  1411. addGameTask(&Game::playerCloseShop, player->getID());
  1412. }
  1413.  
  1414. void ProtocolGame::parseRequestTrade(NetworkMessage& msg)
  1415. {
  1416. Position pos = msg.GetPosition();
  1417. uint16_t spriteId = msg.GetSpriteId();
  1418. int16_t stackpos = msg.GetByte();
  1419. uint32_t playerId = msg.GetU32();
  1420. addGameTask(&Game::playerRequestTrade, player->getID(), pos, stackpos, playerId, spriteId);
  1421. }
  1422.  
  1423. void ProtocolGame::parseAcceptTrade(NetworkMessage& msg)
  1424. {
  1425. addGameTask(&Game::playerAcceptTrade, player->getID());
  1426. }
  1427.  
  1428. void ProtocolGame::parseLookInTrade(NetworkMessage& msg)
  1429. {
  1430. bool counter = msg.GetByte();
  1431. int32_t index = msg.GetByte();
  1432. addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerLookInTrade, player->getID(), counter, index);
  1433. }
  1434.  
  1435. void ProtocolGame::parseCloseTrade()
  1436. {
  1437. addGameTask(&Game::playerCloseTrade, player->getID());
  1438. }
  1439.  
  1440. void ProtocolGame::parseAddVip(NetworkMessage& msg)
  1441. {
  1442. const std::string name = msg.GetString();
  1443. if(name.size() > 32)
  1444. return;
  1445.  
  1446. addGameTask(&Game::playerRequestAddVip, player->getID(), name);
  1447. }
  1448.  
  1449. void ProtocolGame::parseRemoveVip(NetworkMessage& msg)
  1450. {
  1451. uint32_t guid = msg.GetU32();
  1452. addGameTask(&Game::playerRequestRemoveVip, player->getID(), guid);
  1453. }
  1454.  
  1455. void ProtocolGame::parseRotateItem(NetworkMessage& msg)
  1456. {
  1457. Position pos = msg.GetPosition();
  1458. uint16_t spriteId = msg.GetSpriteId();
  1459. int16_t stackpos = msg.GetByte();
  1460. addGameTaskTimed(DISPATCHER_TASK_EXPIRATION, &Game::playerRotateItem, player->getID(), pos, stackpos, spriteId);
  1461. }
  1462.  
  1463. void ProtocolGame::parseDebugAssert(NetworkMessage& msg)
  1464. {
  1465. if(m_debugAssertSent)
  1466. return;
  1467.  
  1468. std::stringstream s;
  1469. s << "----- " << formatDate() << " - " << player->getName() << " (" << convertIPAddress(getIP())
  1470. << ") -----" << std::endl << msg.GetString() << std::endl << msg.GetString()
  1471. << std::endl << msg.GetString() << std::endl << msg.GetString()
  1472. << std::endl << std::endl;
  1473.  
  1474. m_debugAssertSent = true;
  1475. Logger::getInstance()->iFile(LOGFILE_CLIENT_ASSERTION, s.str(), false);
  1476. }
  1477.  
  1478. void ProtocolGame::parseBugReport(NetworkMessage& msg)
  1479. {
  1480. std::string comment = msg.GetString();
  1481. addGameTask(&Game::playerReportBug, player->getID(), comment);
  1482. }
  1483.  
  1484. void ProtocolGame::parseInviteToParty(NetworkMessage& msg)
  1485. {
  1486. uint32_t targetId = msg.GetU32();
  1487. addGameTask(&Game::playerInviteToParty, player->getID(), targetId);
  1488. }
  1489.  
  1490. void ProtocolGame::parseJoinParty(NetworkMessage& msg)
  1491. {
  1492. uint32_t targetId = msg.GetU32();
  1493. addGameTask(&Game::playerJoinParty, player->getID(), targetId);
  1494. }
  1495.  
  1496. void ProtocolGame::parseRevokePartyInvite(NetworkMessage& msg)
  1497. {
  1498. uint32_t targetId = msg.GetU32();
  1499. addGameTask(&Game::playerRevokePartyInvitation, player->getID(), targetId);
  1500. }
  1501.  
  1502. void ProtocolGame::parsePassPartyLeadership(NetworkMessage& msg)
  1503. {
  1504. uint32_t targetId = msg.GetU32();
  1505. addGameTask(&Game::playerPassPartyLeadership, player->getID(), targetId);
  1506. }
  1507.  
  1508. void ProtocolGame::parseLeaveParty(NetworkMessage& msg)
  1509. {
  1510. addGameTask(&Game::playerLeaveParty, player->getID());
  1511. }
  1512.  
  1513. void ProtocolGame::parseSharePartyExperience(NetworkMessage& msg)
  1514. {
  1515. bool activate = msg.GetByte();
  1516. uint8_t unknown = msg.GetByte(); //TODO: find out what is this byte
  1517. addGameTask(&Game::playerSharePartyExperience, player->getID(), activate, unknown);
  1518. }
  1519.  
  1520. void ProtocolGame::parseQuests(NetworkMessage& msg)
  1521. {
  1522. addGameTask(&Game::playerQuests, player->getID());
  1523. }
  1524.  
  1525. void ProtocolGame::parseQuestInfo(NetworkMessage& msg)
  1526. {
  1527. uint16_t questId = msg.GetU16();
  1528. addGameTask(&Game::playerQuestInfo, player->getID(), questId);
  1529. }
  1530.  
  1531. void ProtocolGame::parseViolationWindow(NetworkMessage& msg)
  1532. {
  1533. std::string target = msg.GetString();
  1534. uint8_t reason = msg.GetByte();
  1535. ViolationAction_t action = (ViolationAction_t)msg.GetByte();
  1536. std::string comment = msg.GetString();
  1537. std::string statement = msg.GetString();
  1538. uint32_t statementId = (uint32_t)msg.GetU16();
  1539. bool ipBanishment = msg.GetByte();
  1540. addGameTask(&Game::playerViolationWindow, player->getID(), target, reason, action, comment, statement, statementId, ipBanishment);
  1541. }
  1542.  
  1543. //********************** Send methods *******************************//
  1544. void ProtocolGame::sendOpenPrivateChannel(const std::string& receiver)
  1545. {
  1546. NetworkMessage_ptr msg = getOutputBuffer();
  1547. if(msg)
  1548. {
  1549. TRACK_MESSAGE(msg);
  1550. msg->AddByte(0xAD);
  1551. msg->AddString(receiver);
  1552. }
  1553. }
  1554.  
  1555. void ProtocolGame::sendCreatureOutfit(const Creature* creature, const Outfit_t& outfit)
  1556. {
  1557. if(!canSee(creature))
  1558. return;
  1559.  
  1560. NetworkMessage_ptr msg = getOutputBuffer();
  1561. if(msg)
  1562. {
  1563. TRACK_MESSAGE(msg);
  1564. msg->AddByte(0x8E);
  1565. msg->AddU32(creature->getID());
  1566. AddCreatureOutfit(msg, creature, outfit);
  1567. }
  1568. }
  1569.  
  1570. void ProtocolGame::sendCreatureLight(const Creature* creature)
  1571. {
  1572. if(!canSee(creature))
  1573. return;
  1574.  
  1575. NetworkMessage_ptr msg = getOutputBuffer();
  1576. if(msg)
  1577. {
  1578. TRACK_MESSAGE(msg);
  1579. AddCreatureLight(msg, creature);
  1580. }
  1581. }
  1582.  
  1583. void ProtocolGame::sendWorldLight(const LightInfo& lightInfo)
  1584. {
  1585. NetworkMessage_ptr msg = getOutputBuffer();
  1586. if(msg)
  1587. {
  1588. TRACK_MESSAGE(msg);
  1589. AddWorldLight(msg, lightInfo);
  1590. }
  1591. }
  1592.  
  1593. void ProtocolGame::sendCreatureShield(const Creature* creature)
  1594. {
  1595. if(!canSee(creature))
  1596. return;
  1597.  
  1598. NetworkMessage_ptr msg = getOutputBuffer();
  1599. if(msg)
  1600. {
  1601. TRACK_MESSAGE(msg);
  1602. msg->AddByte(0x91);
  1603. msg->AddU32(creature->getID());
  1604. msg->AddByte(player->getPartyShield(creature));
  1605. }
  1606. }
  1607.  
  1608. void ProtocolGame::sendCreatureSkull(const Creature* creature)
  1609. {
  1610. if(!canSee(creature))
  1611. return;
  1612.  
  1613. NetworkMessage_ptr msg = getOutputBuffer();
  1614. if(msg)
  1615. {
  1616. TRACK_MESSAGE(msg);
  1617. msg->AddByte(0x90);
  1618. msg->AddU32(creature->getID());
  1619. msg->AddByte(player->getSkullClient(creature));
  1620. }
  1621. }
  1622.  
  1623. void ProtocolGame::sendCreatureSquare(const Creature* creature, SquareColor_t color)
  1624. {
  1625. if(!canSee(creature))
  1626. return;
  1627.  
  1628. NetworkMessage_ptr msg = getOutputBuffer();
  1629. if(msg)
  1630. {
  1631. TRACK_MESSAGE(msg);
  1632. msg->AddByte(0x86);
  1633. msg->AddU32(creature->getID());
  1634. msg->AddByte((uint8_t)color);
  1635. }
  1636. }
  1637.  
  1638. void ProtocolGame::sendTutorial(uint8_t tutorialId)
  1639. {
  1640. NetworkMessage_ptr msg = getOutputBuffer();
  1641. if(msg)
  1642. {
  1643. TRACK_MESSAGE(msg);
  1644. msg->AddByte(0xDC);
  1645. msg->AddByte(tutorialId);
  1646. }
  1647. }
  1648.  
  1649. void ProtocolGame::sendAddMarker(const Position& pos, MapMarks_t markType, const std::string& desc)
  1650. {
  1651. NetworkMessage_ptr msg = getOutputBuffer();
  1652. if(msg)
  1653. {
  1654. TRACK_MESSAGE(msg);
  1655. msg->AddByte(0xDD);
  1656. msg->AddPosition(pos);
  1657. msg->AddByte(markType);
  1658. msg->AddString(desc);
  1659. }
  1660. }
  1661.  
  1662. void ProtocolGame::sendReLoginWindow()
  1663. {
  1664. NetworkMessage_ptr msg = getOutputBuffer();
  1665. if(msg)
  1666. {
  1667. TRACK_MESSAGE(msg);
  1668. msg->AddByte(0x28);
  1669. }
  1670. }
  1671.  
  1672. void ProtocolGame::sendStats()
  1673. {
  1674. NetworkMessage_ptr msg = getOutputBuffer();
  1675. if(msg)
  1676. {
  1677. TRACK_MESSAGE(msg);
  1678. AddPlayerStats(msg);
  1679. }
  1680. }
  1681.  
  1682. void ProtocolGame::sendTextMessage(MessageClasses mClass, const std::string& message)
  1683. {
  1684. NetworkMessage_ptr msg = getOutputBuffer();
  1685. if(msg)
  1686. {
  1687. TRACK_MESSAGE(msg);
  1688. AddTextMessage(msg, mClass, message);
  1689. }
  1690. }
  1691.  
  1692. void ProtocolGame::sendClosePrivate(uint16_t channelId)
  1693. {
  1694. NetworkMessage_ptr msg = getOutputBuffer();
  1695. if(msg)
  1696. {
  1697. TRACK_MESSAGE(msg);
  1698. if(channelId == CHANNEL_GUILD || channelId == CHANNEL_PARTY)
  1699. g_chat.removeUserFromChannel(player, channelId);
  1700.  
  1701. msg->AddByte(0xB3);
  1702. msg->AddU16(channelId);
  1703. }
  1704. }
  1705.  
  1706. void ProtocolGame::sendCreatePrivateChannel(uint16_t channelId, const std::string& channelName)
  1707. {
  1708. NetworkMessage_ptr msg = getOutputBuffer();
  1709. if(msg)
  1710. {
  1711. TRACK_MESSAGE(msg);
  1712. msg->AddByte(0xB2);
  1713. msg->AddU16(channelId);
  1714. msg->AddString(channelName);
  1715. }
  1716. }
  1717.  
  1718. void ProtocolGame::sendChannelsDialog()
  1719. {
  1720. NetworkMessage_ptr msg = getOutputBuffer();
  1721. if(msg)
  1722. {
  1723. TRACK_MESSAGE(msg);
  1724. msg->AddByte(0xAB);
  1725. ChannelList list = g_chat.getChannelList(player);
  1726. msg->AddByte(list.size());
  1727. for(ChannelList::iterator it = list.begin(); it != list.end(); ++it)
  1728. {
  1729. if(ChatChannel* channel = (*it))
  1730. {
  1731. msg->AddU16(channel->getId());
  1732. msg->AddString(channel->getName());
  1733. }
  1734. }
  1735. }
  1736. }
  1737.  
  1738. void ProtocolGame::sendChannel(uint16_t channelId, const std::string& channelName)
  1739. {
  1740. NetworkMessage_ptr msg = getOutputBuffer();
  1741. if(msg)
  1742. {
  1743. TRACK_MESSAGE(msg);
  1744. msg->AddByte(0xAC);
  1745. msg->AddU16(channelId);
  1746. msg->AddString(channelName);
  1747. }
  1748. }
  1749.  
  1750. void ProtocolGame::sendRuleViolationsChannel(uint16_t channelId)
  1751. {
  1752. NetworkMessage_ptr msg = getOutputBuffer();
  1753. if(msg)
  1754. {
  1755. TRACK_MESSAGE(msg);
  1756. msg->AddByte(0xAE);
  1757. msg->AddU16(channelId);
  1758. for(RuleViolationsMap::const_iterator it = g_game.getRuleViolations().begin(); it != g_game.getRuleViolations().end(); ++it)
  1759. {
  1760. RuleViolation& rvr = *it->second;
  1761. if(rvr.isOpen && rvr.reporter)
  1762. AddCreatureSpeak(msg, rvr.reporter, SPEAK_RVR_CHANNEL, rvr.text, channelId, rvr.time);
  1763. }
  1764. }
  1765. }
  1766.  
  1767. void ProtocolGame::sendRemoveReport(const std::string& name)
  1768. {
  1769. NetworkMessage_ptr msg = getOutputBuffer();
  1770. if(msg)
  1771. {
  1772. TRACK_MESSAGE(msg);
  1773. msg->AddByte(0xAF);
  1774. msg->AddString(name);
  1775. }
  1776. }
  1777.  
  1778. void ProtocolGame::sendRuleViolationCancel(const std::string& name)
  1779. {
  1780. NetworkMessage_ptr msg = getOutputBuffer();
  1781. if(msg)
  1782. {
  1783. TRACK_MESSAGE(msg);
  1784. msg->AddByte(0xB0);
  1785. msg->AddString(name);
  1786. }
  1787. }
  1788.  
  1789. void ProtocolGame::sendLockRuleViolation()
  1790. {
  1791. NetworkMessage_ptr msg = getOutputBuffer();
  1792. if(msg)
  1793. {
  1794. TRACK_MESSAGE(msg);
  1795. msg->AddByte(0xB1);
  1796. }
  1797. }
  1798.  
  1799. void ProtocolGame::sendIcons(int32_t icons)
  1800. {
  1801. NetworkMessage_ptr msg = getOutputBuffer();
  1802. if(msg)
  1803. {
  1804. TRACK_MESSAGE(msg);
  1805. msg->AddByte(0xA2);
  1806. msg->AddU16(icons);
  1807. }
  1808. }
  1809.  
  1810. void ProtocolGame::sendContainer(uint32_t cid, const Container* container, bool hasParent)
  1811. {
  1812. NetworkMessage_ptr msg = getOutputBuffer();
  1813. if(msg)
  1814. {
  1815. TRACK_MESSAGE(msg);
  1816. msg->AddByte(0x6E);
  1817. msg->AddByte(cid);
  1818.  
  1819. msg->AddItemId(container);
  1820. msg->AddString(container->getName());
  1821. msg->AddByte(container->capacity());
  1822.  
  1823. msg->AddByte(hasParent ? 0x01 : 0x00);
  1824. msg->AddByte(std::min(container->size(), (uint32_t)255));
  1825.  
  1826. ItemList::const_iterator cit = container->getItems();
  1827. for(uint32_t i = 0; cit != container->getEnd() && i < 255; ++cit, ++i)
  1828. msg->AddItem(*cit);
  1829. }
  1830. }
  1831.  
  1832. void ProtocolGame::sendShop(const ShopInfoList& shop)
  1833. {
  1834. NetworkMessage_ptr msg = getOutputBuffer();
  1835. if(msg)
  1836. {
  1837. TRACK_MESSAGE(msg);
  1838. msg->AddByte(0x7A);
  1839. msg->AddByte(std::min(shop.size(), (size_t)255));
  1840.  
  1841. ShopInfoList::const_iterator it = shop.begin();
  1842. for(uint32_t i = 0; it != shop.end() && i < 255; ++it, ++i)
  1843. AddShopItem(msg, (*it));
  1844. }
  1845. }
  1846.  
  1847. void ProtocolGame::sendCloseShop()
  1848. {
  1849. NetworkMessage_ptr msg = getOutputBuffer();
  1850. if(msg)
  1851. {
  1852. TRACK_MESSAGE(msg);
  1853. msg->AddByte(0x7C);
  1854. }
  1855. }
  1856.  
  1857. void ProtocolGame::sendGoods(const ShopInfoList& shop)
  1858. {
  1859. NetworkMessage_ptr msg = getOutputBuffer();
  1860. if(msg)
  1861. {
  1862. TRACK_MESSAGE(msg);
  1863. msg->AddByte(0x7B);
  1864. msg->AddU32(g_game.getMoney(player));
  1865.  
  1866. std::map<uint32_t, uint32_t> goodsMap;
  1867. if(shop.size() >= 5)
  1868. {
  1869. for(ShopInfoList::const_iterator sit = shop.begin(); sit != shop.end(); ++sit)
  1870. {
  1871. if(sit->sellPrice < 0)
  1872. continue;
  1873.  
  1874. int8_t subType = -1;
  1875. if(sit->subType)
  1876. {
  1877. const ItemType& it = Item::items[sit->itemId];
  1878. if(it.hasSubType() && !it.stackable)
  1879. subType = sit->subType;
  1880. }
  1881.  
  1882. uint32_t count = player->__getItemTypeCount(sit->itemId, subType);
  1883. if(count > 0)
  1884. goodsMap[sit->itemId] = count;
  1885. }
  1886. }
  1887. else
  1888. {
  1889. std::map<uint32_t, uint32_t> tmpMap;
  1890. player->__getAllItemTypeCount(tmpMap);
  1891. for(ShopInfoList::const_iterator sit = shop.begin(); sit != shop.end(); ++sit)
  1892. {
  1893. if(sit->sellPrice < 0)
  1894. continue;
  1895.  
  1896. int8_t subType = -1;
  1897. if(sit->subType)
  1898. {
  1899. const ItemType& it = Item::items[sit->itemId];
  1900. if(it.hasSubType() && !it.stackable)
  1901. subType = sit->subType;
  1902. }
  1903.  
  1904. if(subType != -1)
  1905. {
  1906. uint32_t count = player->__getItemTypeCount(sit->itemId, subType);
  1907. if(count > 0)
  1908. goodsMap[sit->itemId] = count;
  1909. }
  1910. else
  1911. goodsMap[sit->itemId] = tmpMap[sit->itemId];
  1912. }
  1913. }
  1914.  
  1915. msg->AddByte(std::min(goodsMap.size(), (size_t)255));
  1916. std::map<uint32_t, uint32_t>::const_iterator it = goodsMap.begin();
  1917. for(uint32_t i = 0; it != goodsMap.end() && i < 255; ++it, ++i)
  1918. {
  1919. msg->AddItemId(it->first);
  1920. msg->AddByte(std::min(it->second, (uint32_t)255));
  1921. }
  1922. }
  1923. }
  1924.  
  1925. void ProtocolGame::sendTradeItemRequest(const Player* player, const Item* item, bool ack)
  1926. {
  1927. NetworkMessage_ptr msg = getOutputBuffer();
  1928. if(msg)
  1929. {
  1930. TRACK_MESSAGE(msg);
  1931. if(ack)
  1932. msg->AddByte(0x7D);
  1933. else
  1934. msg->AddByte(0x7E);
  1935.  
  1936. msg->AddString(player->getName());
  1937. if(const Container* container = item->getContainer())
  1938. {
  1939. msg->AddByte(container->getItemHoldingCount() + 1);
  1940. msg->AddItem(item);
  1941. for(ContainerIterator it = container->begin(); it != container->end(); ++it)
  1942. msg->AddItem(*it);
  1943. }
  1944. else
  1945. {
  1946. msg->AddByte(1);
  1947. msg->AddItem(item);
  1948. }
  1949. }
  1950. }
  1951.  
  1952. void ProtocolGame::sendCloseTrade()
  1953. {
  1954. NetworkMessage_ptr msg = getOutputBuffer();
  1955. if(msg)
  1956. {
  1957. TRACK_MESSAGE(msg);
  1958. msg->AddByte(0x7F);
  1959. }
  1960. }
  1961.  
  1962. void ProtocolGame::sendCloseContainer(uint32_t cid)
  1963. {
  1964. NetworkMessage_ptr msg = getOutputBuffer();
  1965. if(msg)
  1966. {
  1967. TRACK_MESSAGE(msg);
  1968. msg->AddByte(0x6F);
  1969. msg->AddByte(cid);
  1970. }
  1971. }
  1972.  
  1973. void ProtocolGame::sendCreatureTurn(const Creature* creature, int16_t stackpos)
  1974. {
  1975. if(stackpos >= 10 || !canSee(creature))
  1976. return;
  1977.  
  1978. NetworkMessage_ptr msg = getOutputBuffer();
  1979. if(msg)
  1980. {
  1981. TRACK_MESSAGE(msg);
  1982. msg->AddByte(0x6B);
  1983. msg->AddPosition(creature->getPosition());
  1984. msg->AddByte(stackpos);
  1985. msg->AddU16(0x63); /*99*/
  1986. msg->AddU32(creature->getID());
  1987. msg->AddByte(creature->getDirection());
  1988. }
  1989. }
  1990.  
  1991. void ProtocolGame::sendCreatureSay(const Creature* creature, SpeakClasses type, const std::string& text, Position* pos/* = NULL*/)
  1992. {
  1993. NetworkMessage_ptr msg = getOutputBuffer();
  1994. if(msg)
  1995. {
  1996. TRACK_MESSAGE(msg);
  1997. AddCreatureSpeak(msg, creature, type, text, 0, 0, pos);
  1998. }
  1999. }
  2000.  
  2001. void ProtocolGame::sendToChannel(const Creature* creature, SpeakClasses type, const std::string& text, uint16_t channelId, uint32_t time /*= 0*/)
  2002. {
  2003. NetworkMessage_ptr msg = getOutputBuffer();
  2004. if(msg)
  2005. {
  2006. TRACK_MESSAGE(msg);
  2007. AddCreatureSpeak(msg, creature, type, text, channelId, time);
  2008. }
  2009. }
  2010.  
  2011. void ProtocolGame::sendCancel(const std::string& message)
  2012. {
  2013. NetworkMessage_ptr msg = getOutputBuffer();
  2014. if(msg)
  2015. {
  2016. TRACK_MESSAGE(msg);
  2017. AddTextMessage(msg, MSG_STATUS_SMALL, message);
  2018. }
  2019. }
  2020.  
  2021. void ProtocolGame::sendCancelTarget()
  2022. {
  2023. NetworkMessage_ptr msg = getOutputBuffer();
  2024. if(msg)
  2025. {
  2026. TRACK_MESSAGE(msg);
  2027. msg->AddByte(0xA3);
  2028. }
  2029. }
  2030.  
  2031. void ProtocolGame::sendChangeSpeed(const Creature* creature, uint32_t speed)
  2032. {
  2033. if(!canSee(creature))
  2034. return;
  2035.  
  2036. NetworkMessage_ptr msg = getOutputBuffer();
  2037. if(msg)
  2038. {
  2039. TRACK_MESSAGE(msg);
  2040. msg->AddByte(0x8F);
  2041. msg->AddU32(creature->getID());
  2042. msg->AddU16(speed);
  2043. }
  2044. }
  2045.  
  2046. void ProtocolGame::sendCancelWalk()
  2047. {
  2048. NetworkMessage_ptr msg = getOutputBuffer();
  2049. if(msg)
  2050. {
  2051. TRACK_MESSAGE(msg);
  2052. msg->AddByte(0xB5);
  2053. msg->AddByte(player->getDirection());
  2054. }
  2055. }
  2056.  
  2057. void ProtocolGame::sendSkills()
  2058. {
  2059. NetworkMessage_ptr msg = getOutputBuffer();
  2060. if(msg)
  2061. {
  2062. TRACK_MESSAGE(msg);
  2063. AddPlayerSkills(msg);
  2064. }
  2065. }
  2066.  
  2067. void ProtocolGame::sendPing()
  2068. {
  2069. NetworkMessage_ptr msg = getOutputBuffer();
  2070. if(msg)
  2071. {
  2072. TRACK_MESSAGE(msg);
  2073. msg->AddByte(0x1E);
  2074. }
  2075. }
  2076.  
  2077. void ProtocolGame::sendDistanceShoot(const Position& from, const Position& to, uint8_t type)
  2078. {
  2079. if(type >= SHOOT_EFFECT_NONE || (!canSee(from) && !canSee(to)))
  2080. return;
  2081.  
  2082. NetworkMessage_ptr msg = getOutputBuffer();
  2083. if(msg)
  2084. {
  2085. TRACK_MESSAGE(msg);
  2086. AddDistanceShoot(msg, from, to, type);
  2087. }
  2088. }
  2089.  
  2090. void ProtocolGame::sendMagicEffect(const Position& pos, uint16_t type)
  2091. {
  2092.  
  2093. NetworkMessage_ptr msg = getOutputBuffer();
  2094. if(msg)
  2095. {
  2096. TRACK_MESSAGE(msg);
  2097. AddMagicEffect(msg, pos, type);
  2098. }
  2099. }
  2100.  
  2101. void ProtocolGame::sendAnimatedText(const Position& pos, uint8_t color, std::string text)
  2102. {
  2103. if(!canSee(pos))
  2104. return;
  2105.  
  2106. NetworkMessage_ptr msg = getOutputBuffer();
  2107. if(msg)
  2108. {
  2109. TRACK_MESSAGE(msg);
  2110. AddAnimatedText(msg, pos, color, text);
  2111. }
  2112. }
  2113.  
  2114. void ProtocolGame::sendCreatureHealth(const Creature* creature)
  2115. {
  2116. if(!canSee(creature))
  2117. return;
  2118.  
  2119. NetworkMessage_ptr msg = getOutputBuffer();
  2120. if(msg)
  2121. {
  2122. TRACK_MESSAGE(msg);
  2123. AddCreatureHealth(msg, creature);
  2124. }
  2125. }
  2126.  
  2127. void ProtocolGame::sendFYIBox(const std::string& message)
  2128. {
  2129. if(message.empty() || message.length() > 1018) //Prevent client debug when message is empty or length is > 1018 (not confirmed)
  2130. {
  2131. std::cout << "[Warning - ProtocolGame::sendFYIBox] Trying to send an empty or too huge message." << std::endl;
  2132. return;
  2133. }
  2134.  
  2135. NetworkMessage_ptr msg = getOutputBuffer();
  2136. if(msg)
  2137. {
  2138. TRACK_MESSAGE(msg);
  2139. msg->AddByte(0x15);
  2140. msg->AddString(message);
  2141. }
  2142. }
  2143.  
  2144. //tile
  2145. void ProtocolGame::sendAddTileItem(const Tile* tile, const Position& pos, uint32_t stackpos, const Item* item)
  2146. {
  2147. if(!canSee(pos))
  2148. return;
  2149.  
  2150. NetworkMessage_ptr msg = getOutputBuffer();
  2151. if(msg)
  2152. {
  2153. TRACK_MESSAGE(msg);
  2154. AddTileItem(msg, pos, stackpos, item);
  2155. }
  2156. }
  2157.  
  2158. void ProtocolGame::sendUpdateTileItem(const Tile* tile, const Position& pos, uint32_t stackpos, const Item* item)
  2159. {
  2160. if(!canSee(pos))
  2161. return;
  2162.  
  2163. NetworkMessage_ptr msg = getOutputBuffer();
  2164. if(msg)
  2165. {
  2166. TRACK_MESSAGE(msg);
  2167. UpdateTileItem(msg, pos, stackpos, item);
  2168. }
  2169. }
  2170.  
  2171. void ProtocolGame::sendRemoveTileItem(const Tile* tile, const Position& pos, uint32_t stackpos)
  2172. {
  2173. if(!canSee(pos))
  2174. return;
  2175.  
  2176. NetworkMessage_ptr msg = getOutputBuffer();
  2177. if(msg)
  2178. {
  2179. TRACK_MESSAGE(msg);
  2180. RemoveTileItem(msg, pos, stackpos);
  2181. }
  2182. }
  2183.  
  2184. void ProtocolGame::sendUpdateTile(const Tile* tile, const Position& pos)
  2185. {
  2186. if(!canSee(pos))
  2187. return;
  2188.  
  2189. NetworkMessage_ptr msg = getOutputBuffer();
  2190. if(msg)
  2191. {
  2192. TRACK_MESSAGE(msg);
  2193. msg->AddByte(0x69);
  2194. msg->AddPosition(pos);
  2195. if(tile)
  2196. {
  2197. GetTileDescription(tile, msg);
  2198. msg->AddByte(0x00);
  2199. msg->AddByte(0xFF);
  2200. }
  2201. else
  2202. {
  2203. msg->AddByte(0x01);
  2204. msg->AddByte(0xFF);
  2205. }
  2206. }
  2207. }
  2208.  
  2209. void ProtocolGame::sendAddCreature(const Creature* creature, const Position& pos, uint32_t stackpos)
  2210. {
  2211. if(!canSee(creature))
  2212. return;
  2213.  
  2214. NetworkMessage_ptr msg = getOutputBuffer();
  2215. if(!msg)
  2216. return;
  2217.  
  2218. TRACK_MESSAGE(msg);
  2219. if(creature != player)
  2220. {
  2221. AddTileCreature(msg, pos, stackpos, creature);
  2222. return;
  2223. }
  2224.  
  2225. msg->AddByte(0x0A);
  2226. msg->AddU32(player->getID());
  2227. msg->AddU16(0x32);
  2228.  
  2229. msg->AddByte(player->hasFlag(PlayerFlag_CanReportBugs));
  2230. if(Group* group = player->getGroup())
  2231. {
  2232. int32_t reasons = group->getViolationReasons();
  2233. if(reasons > 1)
  2234. {
  2235. msg->AddByte(0x0B);
  2236. for(int32_t i = 0; i < 20; ++i)
  2237. {
  2238. if(i < 4)
  2239. msg->AddByte(group->getNameViolationFlags());
  2240. else if(i < reasons)
  2241. msg->AddByte(group->getStatementViolationFlags());
  2242. else
  2243. msg->AddByte(0x00);
  2244. }
  2245. }
  2246. }
  2247.  
  2248. AddMapDescription(msg, pos);
  2249. for(int32_t i = SLOT_FIRST; i < SLOT_LAST; ++i)
  2250. AddInventoryItem(msg, (slots_t)i, player->getInventoryItem((slots_t)i));
  2251.  
  2252. AddPlayerStats(msg);
  2253. AddPlayerSkills(msg);
  2254.  
  2255. //gameworld light-settings
  2256. LightInfo lightInfo;
  2257. g_game.getWorldLightInfo(lightInfo);
  2258. AddWorldLight(msg, lightInfo);
  2259.  
  2260. //player light level
  2261. AddCreatureLight(msg, creature);
  2262. player->sendIcons();
  2263. for(VIPListSet::iterator it = player->VIPList.begin(); it != player->VIPList.end(); it++)
  2264. {
  2265. std::string vipName;
  2266. if(IOLoginData::getInstance()->getNameByGuid((*it), vipName))
  2267. {
  2268. Player* tmpPlayer = g_game.getPlayerByName(vipName);
  2269. sendVIP((*it), vipName, (tmpPlayer && player->canSeeCreature(tmpPlayer)));
  2270. }
  2271. }
  2272. }
  2273.  
  2274. void ProtocolGame::sendRemoveCreature(const Creature* creature, const Position& pos, uint32_t stackpos)
  2275. {
  2276. if(!canSee(pos))
  2277. return;
  2278.  
  2279. NetworkMessage_ptr msg = getOutputBuffer();
  2280. if(msg)
  2281. {
  2282. TRACK_MESSAGE(msg);
  2283. RemoveTileItem(msg, pos, stackpos);
  2284. }
  2285. }
  2286.  
  2287. void ProtocolGame::sendMoveCreature(const Creature* creature, const Tile* newTile, const Position& newPos,
  2288. uint32_t newStackpos, const Tile* oldTile, const Position& oldPos, uint32_t oldStackpos, bool teleport)
  2289. {
  2290. if(creature == player)
  2291. {
  2292. NetworkMessage_ptr msg = getOutputBuffer();
  2293. if(msg)
  2294. {
  2295. TRACK_MESSAGE(msg);
  2296. if(teleport || oldStackpos >= 10)
  2297. {
  2298. RemoveTileItem(msg, oldPos, oldStackpos);
  2299. AddMapDescription(msg, newPos);
  2300. }
  2301. else
  2302. {
  2303. if(oldPos.z != 7 || newPos.z < 8)
  2304. {
  2305. msg->AddByte(0x6D);
  2306. msg->AddPosition(oldPos);
  2307. msg->AddByte(oldStackpos);
  2308. msg->AddPosition(newPos);
  2309. }
  2310. else
  2311. RemoveTileItem(msg, oldPos, oldStackpos);
  2312.  
  2313. if(newPos.z > oldPos.z)
  2314. MoveDownCreature(msg, creature, newPos, oldPos, oldStackpos);
  2315. else if(newPos.z < oldPos.z)
  2316. MoveUpCreature(msg, creature, newPos, oldPos, oldStackpos);
  2317.  
  2318. if(oldPos.y > newPos.y) // north, for old x
  2319. {
  2320. msg->AddByte(0x65);
  2321. GetMapDescription(oldPos.x - 14, newPos.y - 8, newPos.z, (14 * 2) + 2, 1, msg);
  2322. }
  2323. else if(oldPos.y < newPos.y) // south, for old x
  2324. {
  2325. msg->AddByte(0x67);
  2326. GetMapDescription(oldPos.x - 14, newPos.y + 9, newPos.z, (14 * 2) + 2, 1, msg);
  2327. }
  2328.  
  2329. if(oldPos.x < newPos.x) // east, [with new y]
  2330. {
  2331. msg->AddByte(0x66);
  2332. GetMapDescription(newPos.x + 15, newPos.y - 8, newPos.z, 1, (8 * 2) + 2, msg);
  2333. }
  2334. else if(oldPos.x > newPos.x) // west, [with new y]
  2335. {
  2336. msg->AddByte(0x68);
  2337. GetMapDescription(newPos.x - 14, newPos.y - 8, newPos.z, 1, (8 * 2) + 2, msg);
  2338. }
  2339. }
  2340. }
  2341. }
  2342. else if(canSee(oldPos) && canSee(newPos))
  2343. {
  2344. if(!player->canSeeCreature(creature))
  2345. return;
  2346.  
  2347. NetworkMessage_ptr msg = getOutputBuffer();
  2348. if(msg)
  2349. {
  2350. TRACK_MESSAGE(msg);
  2351. if(!teleport && (oldPos.z != 7 || newPos.z < 8) && oldStackpos < 10)
  2352. {
  2353. msg->AddByte(0x6D);
  2354. msg->AddPosition(oldPos);
  2355. msg->AddByte(oldStackpos);
  2356. msg->AddPosition(newPos);
  2357. }
  2358. else
  2359. {
  2360. RemoveTileItem(msg, oldPos, oldStackpos);
  2361. AddTileCreature(msg, newPos, newStackpos, creature);
  2362. }
  2363. }
  2364. }
  2365. else if(canSee(oldPos))
  2366. {
  2367. if(!player->canSeeCreature(creature))
  2368. return;
  2369.  
  2370. NetworkMessage_ptr msg = getOutputBuffer();
  2371. if(msg)
  2372. {
  2373. TRACK_MESSAGE(msg);
  2374. RemoveTileItem(msg, oldPos, oldStackpos);
  2375. }
  2376. }
  2377. else if(canSee(newPos) && player->canSeeCreature(creature))
  2378. {
  2379. NetworkMessage_ptr msg = getOutputBuffer();
  2380. if(msg)
  2381. {
  2382. TRACK_MESSAGE(msg);
  2383. AddTileCreature(msg, newPos, newStackpos, creature);
  2384. }
  2385. }
  2386. }
  2387.  
  2388. //inventory
  2389. void ProtocolGame::sendAddInventoryItem(slots_t slot, const Item* item)
  2390. {
  2391. NetworkMessage_ptr msg = getOutputBuffer();
  2392. if(msg)
  2393. {
  2394. TRACK_MESSAGE(msg);
  2395. AddInventoryItem(msg, slot, item);
  2396. }
  2397. }
  2398.  
  2399. void ProtocolGame::sendUpdateInventoryItem(slots_t slot, const Item* item)
  2400. {
  2401. NetworkMessage_ptr msg = getOutputBuffer();
  2402. if(msg)
  2403. {
  2404. TRACK_MESSAGE(msg);
  2405. UpdateInventoryItem(msg, slot, item);
  2406. }
  2407. }
  2408.  
  2409. void ProtocolGame::sendRemoveInventoryItem(slots_t slot)
  2410. {
  2411. NetworkMessage_ptr msg = getOutputBuffer();
  2412. if(msg)
  2413. {
  2414. TRACK_MESSAGE(msg);
  2415. RemoveInventoryItem(msg, slot);
  2416. }
  2417. }
  2418.  
  2419. //containers
  2420. void ProtocolGame::sendAddContainerItem(uint8_t cid, const Item* item)
  2421. {
  2422. NetworkMessage_ptr msg = getOutputBuffer();
  2423. if(msg)
  2424. {
  2425. TRACK_MESSAGE(msg);
  2426. AddContainerItem(msg, cid, item);
  2427. }
  2428. }
  2429.  
  2430. void ProtocolGame::sendUpdateContainerItem(uint8_t cid, uint8_t slot, const Item* item)
  2431. {
  2432. NetworkMessage_ptr msg = getOutputBuffer();
  2433. if(msg)
  2434. {
  2435. TRACK_MESSAGE(msg);
  2436. UpdateContainerItem(msg, cid, slot, item);
  2437. }
  2438. }
  2439.  
  2440. void ProtocolGame::sendRemoveContainerItem(uint8_t cid, uint8_t slot)
  2441. {
  2442. NetworkMessage_ptr msg = getOutputBuffer();
  2443. if(msg)
  2444. {
  2445. TRACK_MESSAGE(msg);
  2446. RemoveContainerItem(msg, cid, slot);
  2447. }
  2448. }
  2449.  
  2450. void ProtocolGame::sendTextWindow(uint32_t windowTextId, Item* item, uint16_t maxLen, bool canWrite)
  2451. {
  2452. NetworkMessage_ptr msg = getOutputBuffer();
  2453. if(msg)
  2454. {
  2455. TRACK_MESSAGE(msg);
  2456. msg->AddByte(0x96);
  2457. msg->AddU32(windowTextId);
  2458. msg->AddItemId(item);
  2459. if(canWrite)
  2460. {
  2461. msg->AddU16(maxLen);
  2462. msg->AddString(item->getText());
  2463. }
  2464. else
  2465. {
  2466. msg->AddU16(item->getText().size());
  2467. msg->AddString(item->getText());
  2468. }
  2469.  
  2470. const std::string& writer = item->getWriter();
  2471. if(writer.size())
  2472. msg->AddString(writer);
  2473. else
  2474. msg->AddString("");
  2475.  
  2476. time_t writtenDate = item->getDate();
  2477. if(writtenDate > 0)
  2478. msg->AddString(formatDate(writtenDate));
  2479. else
  2480. msg->AddString("");
  2481. }
  2482. }
  2483.  
  2484. void ProtocolGame::sendTextWindow(uint32_t windowTextId, uint32_t itemId, const std::string& text)
  2485. {
  2486. NetworkMessage_ptr msg = getOutputBuffer();
  2487. if(msg)
  2488. {
  2489. TRACK_MESSAGE(msg);
  2490. msg->AddByte(0x96);
  2491. msg->AddU32(windowTextId);
  2492. msg->AddItemId(itemId);
  2493.  
  2494. msg->AddU16(text.size());
  2495. msg->AddString(text);
  2496.  
  2497. msg->AddString("");
  2498. msg->AddString("");
  2499. }
  2500. }
  2501.  
  2502. void ProtocolGame::sendHouseWindow(uint32_t windowTextId, House* _house,
  2503. uint32_t listId, const std::string& text)
  2504. {
  2505. NetworkMessage_ptr msg = getOutputBuffer();
  2506. if(msg)
  2507. {
  2508. TRACK_MESSAGE(msg);
  2509. msg->AddByte(0x97);
  2510. msg->AddByte(0x00);
  2511. msg->AddU32(windowTextId);
  2512. msg->AddString(text);
  2513. }
  2514. }
  2515.  
  2516. void ProtocolGame::sendOutfitWindow()
  2517. {
  2518. NetworkMessage_ptr msg = getOutputBuffer();
  2519. if(msg)
  2520. {
  2521. TRACK_MESSAGE(msg);
  2522. msg->AddByte(0xC8);
  2523. AddCreatureOutfit(msg, player, player->getDefaultOutfit(), true);
  2524.  
  2525. std::list<Outfit> outfitList;
  2526. for(OutfitMap::iterator it = player->outfits.begin(); it != player->outfits.end(); ++it)
  2527. {
  2528. if(player->canWearOutfit(it->first, it->second.addons))
  2529. outfitList.push_back(it->second);
  2530. }
  2531.  
  2532. if(outfitList.size())
  2533. {
  2534. msg->AddByte((size_t)std::min((size_t)OUTFITS_MAX_NUMBER, outfitList.size()));
  2535. std::list<Outfit>::iterator it = outfitList.begin();
  2536. for(int32_t i = 0; it != outfitList.end() && i < OUTFITS_MAX_NUMBER; ++it, ++i)
  2537. {
  2538. msg->AddU16(it->lookType);
  2539. msg->AddString(it->name);
  2540. if(player->hasCustomFlag(PlayerCustomFlag_CanWearAllAddons))
  2541. msg->AddByte(0x03);
  2542. else if(!g_config.getBool(ConfigManager::ADDONS_PREMIUM) || player->isPremium())
  2543. msg->AddByte(it->addons);
  2544. else
  2545. msg->AddByte(0x00);
  2546. }
  2547. }
  2548. else
  2549. {
  2550. msg->AddByte(1);
  2551. msg->AddU16(player->getDefaultOutfit().lookType);
  2552. msg->AddString("Outfit");
  2553. msg->AddByte(player->getDefaultOutfit().lookAddons);
  2554. }
  2555.  
  2556. player->hasRequestedOutfit(true);
  2557. }
  2558. }
  2559.  
  2560. void ProtocolGame::sendQuests()
  2561. {
  2562. NetworkMessage_ptr msg = getOutputBuffer();
  2563. if(msg)
  2564. {
  2565. TRACK_MESSAGE(msg);
  2566. msg->AddByte(0xF0);
  2567.  
  2568. msg->AddU16(Quests::getInstance()->getQuestCount(player));
  2569. for(QuestList::const_iterator it = Quests::getInstance()->getFirstQuest(); it != Quests::getInstance()->getLastQuest(); ++it)
  2570. {
  2571. if(!(*it)->isStarted(player))
  2572. continue;
  2573.  
  2574. msg->AddU16((*it)->getId());
  2575. msg->AddString((*it)->getName());
  2576. msg->AddByte((*it)->isCompleted(player));
  2577. }
  2578. }
  2579. }
  2580.  
  2581. void ProtocolGame::sendQuestInfo(Quest* quest)
  2582. {
  2583. NetworkMessage_ptr msg = getOutputBuffer();
  2584. if(msg)
  2585. {
  2586. TRACK_MESSAGE(msg);
  2587. msg->AddByte(0xF1);
  2588. msg->AddU16(quest->getId());
  2589.  
  2590. msg->AddByte(quest->getMissionCount(player));
  2591. for(MissionList::const_iterator it = quest->getFirstMission(); it != quest->getLastMission(); ++it)
  2592. {
  2593. if(!(*it)->isStarted(player))
  2594. continue;
  2595.  
  2596. msg->AddString((*it)->getName(player));
  2597. msg->AddString((*it)->getDescription(player));
  2598. }
  2599. }
  2600. }
  2601.  
  2602. void ProtocolGame::sendVIPLogIn(uint32_t guid)
  2603. {
  2604. NetworkMessage_ptr msg = getOutputBuffer();
  2605. if(msg)
  2606. {
  2607. TRACK_MESSAGE(msg);
  2608. msg->AddByte(0xD3);
  2609. msg->AddU32(guid);
  2610. }
  2611. }
  2612.  
  2613. void ProtocolGame::sendVIPLogOut(uint32_t guid)
  2614. {
  2615. NetworkMessage_ptr msg = getOutputBuffer();
  2616. if(msg)
  2617. {
  2618. TRACK_MESSAGE(msg);
  2619. msg->AddByte(0xD4);
  2620. msg->AddU32(guid);
  2621. }
  2622. }
  2623.  
  2624. void ProtocolGame::sendVIP(uint32_t guid, const std::string& name, bool isOnline)
  2625. {
  2626. NetworkMessage_ptr msg = getOutputBuffer();
  2627. if(msg)
  2628. {
  2629. TRACK_MESSAGE(msg);
  2630. msg->AddByte(0xD2);
  2631. msg->AddU32(guid);
  2632. msg->AddString(name);
  2633. msg->AddByte(isOnline ? 1 : 0);
  2634. }
  2635. }
  2636.  
  2637. ////////////// Add common messages
  2638. void ProtocolGame::AddMapDescription(NetworkMessage_ptr msg, const Position& pos)
  2639. {
  2640. msg->AddByte(0x64);
  2641. msg->AddPosition(player->getPosition());
  2642. GetMapDescription(pos.x - 14, pos.y - 8, pos.z, (14 * 2) + 2, (8 * 2) + 2, msg);
  2643. }
  2644.  
  2645. void ProtocolGame::AddTextMessage(NetworkMessage_ptr msg, MessageClasses mclass, const std::string& message)
  2646. {
  2647. msg->AddByte(0xB4);
  2648. msg->AddByte(mclass);
  2649. msg->AddString(message);
  2650. }
  2651.  
  2652. void ProtocolGame::AddAnimatedText(NetworkMessage_ptr msg, const Position& pos,
  2653. uint8_t color, const std::string& text)
  2654. {
  2655. msg->AddByte(0x84);
  2656. msg->AddPosition(pos);
  2657. msg->AddByte(color);
  2658. msg->AddString(text);
  2659. }
  2660.  
  2661. void ProtocolGame::AddMagicEffect(NetworkMessage_ptr msg,const Position& pos, uint16_t type)
  2662. {
  2663. msg->AddByte(0x83);
  2664. msg->AddPosition(pos);
  2665. msg->AddU16(type + 1);
  2666. }
  2667.  
  2668. void ProtocolGame::AddDistanceShoot(NetworkMessage_ptr msg, const Position& from, const Position& to,
  2669. uint8_t type)
  2670. {
  2671. msg->AddByte(0x85);
  2672. msg->AddPosition(from);
  2673. msg->AddPosition(to);
  2674. msg->AddByte(type + 1);
  2675. }
  2676.  
  2677. void ProtocolGame::AddCreature(NetworkMessage_ptr msg, const Creature* creature, bool known, uint32_t remove)
  2678. {
  2679. if(!known)
  2680. {
  2681. msg->AddU16(0x61);
  2682. msg->AddU32(remove);
  2683. msg->AddU32(creature->getID());
  2684. msg->AddString(creature->getHideName() ? "" : creature->getName());
  2685. }
  2686. else
  2687. {
  2688. msg->AddU16(0x62);
  2689. msg->AddU32(creature->getID());
  2690. }
  2691.  
  2692. if(!creature->getHideHealth())
  2693. msg->AddByte((int32_t)std::ceil(((float)creature->getHealth()) * 100 / std::max(creature->getMaxHealth(), (int32_t)1)));
  2694. else
  2695. msg->AddByte(0x00);
  2696.  
  2697. msg->AddByte((uint8_t)creature->getDirection());
  2698. AddCreatureOutfit(msg, creature, creature->getCurrentOutfit());
  2699.  
  2700. LightInfo lightInfo;
  2701. creature->getCreatureLight(lightInfo);
  2702. msg->AddByte(player->hasCustomFlag(PlayerCustomFlag_HasFullLight) ? 0xFF : lightInfo.level);
  2703. msg->AddByte(lightInfo.color);
  2704.  
  2705. msg->AddU16(creature->getStepSpeed());
  2706. msg->AddByte(player->getSkullClient(creature));
  2707. msg->AddByte(player->getPartyShield(creature));
  2708. if(!known)
  2709. msg->AddByte(0x00); // war emblem
  2710.  
  2711. msg->AddByte(!player->canWalkthrough(creature));
  2712. }
  2713.  
  2714. void ProtocolGame::AddPlayerStats(NetworkMessage_ptr msg)
  2715. {
  2716. msg->AddByte(0xA0);
  2717. msg->AddU16((uint16_t)std::ceil(((float)player->getHealth()) * 100 / std::max(player->getMaxHealth(), (int32_t)1)));
  2718. msg->AddU16((uint16_t)100);
  2719. msg->AddU32(uint32_t(player->getFreeCapacity() * 100));
  2720. uint64_t experience = player->getExperience();
  2721. if(experience > 0x7FFFFFFF) // client debugs after 2,147,483,647 exp
  2722. msg->AddU32(0x7FFFFFFF);
  2723. else
  2724. msg->AddU32(experience);
  2725.  
  2726. msg->AddU16(player->getPlayerInfo(PLAYERINFO_LEVEL));
  2727. msg->AddByte(player->getPlayerInfo(PLAYERINFO_LEVELPERCENT));
  2728. msg->AddU16((uint16_t)std::ceil(((float)player->getMana()) * 100 / std::max(player->getMaxMana(), (int32_t)1)));
  2729. msg->AddU16((uint16_t)100);
  2730. msg->AddByte(player->getPlayerInfo(PLAYERINFO_MAGICLEVEL));
  2731. msg->AddByte(player->getPlayerInfo(PLAYERINFO_MAGICLEVELPERCENT));
  2732. msg->AddByte(player->getPlayerInfo(PLAYERINFO_SOUL));
  2733. msg->AddU16(player->getStaminaMinutes());
  2734. }
  2735.  
  2736. void ProtocolGame::AddPlayerSkills(NetworkMessage_ptr msg)
  2737. {
  2738. msg->AddByte(0xA1);
  2739. msg->AddByte(player->getSkill(SKILL_FIST, SKILL_LEVEL));
  2740. msg->AddByte(player->getSkill(SKILL_FIST, SKILL_PERCENT));
  2741. msg->AddByte(player->getSkill(SKILL_CLUB, SKILL_LEVEL));
  2742. msg->AddByte(player->getSkill(SKILL_CLUB, SKILL_PERCENT));
  2743. msg->AddByte(player->getSkill(SKILL_SWORD, SKILL_LEVEL));
  2744. msg->AddByte(player->getSkill(SKILL_SWORD, SKILL_PERCENT));
  2745. msg->AddByte(player->getSkill(SKILL_AXE, SKILL_LEVEL));
  2746. msg->AddByte(player->getSkill(SKILL_AXE, SKILL_PERCENT));
  2747. msg->AddByte(player->getSkill(SKILL_DIST, SKILL_LEVEL));
  2748. msg->AddByte(player->getSkill(SKILL_DIST, SKILL_PERCENT));
  2749. msg->AddByte(player->getSkill(SKILL_SHIELD, SKILL_LEVEL));
  2750. msg->AddByte(player->getSkill(SKILL_SHIELD, SKILL_PERCENT));
  2751. msg->AddByte(player->getSkill(SKILL_FISH, SKILL_LEVEL));
  2752. msg->AddByte(player->getSkill(SKILL_FISH, SKILL_PERCENT));
  2753. }
  2754.  
  2755. void ProtocolGame::AddCreatureSpeak(NetworkMessage_ptr msg, const Creature* creature, SpeakClasses type,
  2756. std::string text, uint16_t channelId, uint32_t time/*= 0*/, Position* pos/* = NULL*/)
  2757. {
  2758. msg->AddByte(0xAA);
  2759. if(creature)
  2760. {
  2761. const Player* speaker = creature->getPlayer();
  2762. if(speaker)
  2763. {
  2764. msg->AddU32(++g_chat.statement);
  2765. g_chat.statementMap[g_chat.statement] = text;
  2766. }
  2767. else
  2768. msg->AddU32(0x00);
  2769.  
  2770. if(creature->getSpeakType() != SPEAK_CLASS_NONE)
  2771. type = creature->getSpeakType();
  2772.  
  2773. switch(type)
  2774. {
  2775. case SPEAK_CHANNEL_RA:
  2776. msg->AddString("");
  2777. break;
  2778. case SPEAK_RVR_ANSWER:
  2779. msg->AddString("Gamemaster");
  2780. break;
  2781. default:
  2782. msg->AddString(!creature->getHideName() ? creature->getName() : "");
  2783. break;
  2784. }
  2785.  
  2786. if(speaker && type != SPEAK_RVR_ANSWER && !speaker->isAccountManager()
  2787. && !speaker->hasCustomFlag(PlayerCustomFlag_HideLevel))
  2788. msg->AddU16(speaker->getPlayerInfo(PLAYERINFO_LEVEL));
  2789. else
  2790. msg->AddU16(0x00);
  2791.  
  2792. }
  2793. else
  2794. {
  2795. msg->AddU32(0x00);
  2796. msg->AddString("");
  2797. msg->AddU16(0x00);
  2798. }
  2799.  
  2800. msg->AddByte(type);
  2801. switch(type)
  2802. {
  2803. case SPEAK_SAY:
  2804. case SPEAK_WHISPER:
  2805. case SPEAK_YELL:
  2806. case SPEAK_MONSTER_SAY:
  2807. case SPEAK_MONSTER_YELL:
  2808. case SPEAK_PRIVATE_NP:
  2809. {
  2810. if(pos)
  2811. msg->AddPosition(*pos);
  2812. else if(creature)
  2813. msg->AddPosition(creature->getPosition());
  2814. else
  2815. msg->AddPosition(Position(0,0,7));
  2816.  
  2817. break;
  2818. }
  2819.  
  2820. case SPEAK_CHANNEL_Y:
  2821. case SPEAK_CHANNEL_RN:
  2822. case SPEAK_CHANNEL_RA:
  2823. case SPEAK_CHANNEL_O:
  2824. case SPEAK_CHANNEL_W:
  2825. msg->AddU16(channelId);
  2826. break;
  2827.  
  2828. case SPEAK_RVR_CHANNEL:
  2829. {
  2830. msg->AddU32(uint32_t(OTSYS_TIME() / 1000 & 0xFFFFFFFF) - time);
  2831. break;
  2832. }
  2833.  
  2834. default:
  2835. break;
  2836. }
  2837.  
  2838. msg->AddString(text);
  2839. }
  2840.  
  2841. void ProtocolGame::AddCreatureHealth(NetworkMessage_ptr msg,const Creature* creature)
  2842. {
  2843. msg->AddByte(0x8C);
  2844. msg->AddU32(creature->getID());
  2845. if(!creature->getHideHealth())
  2846. msg->AddByte((int32_t)std::ceil(((float)creature->getHealth()) * 100 / std::max(creature->getMaxHealth(), (int32_t)1)));
  2847. else
  2848. msg->AddByte(0x00);
  2849. }
  2850.  
  2851. void ProtocolGame::AddCreatureOutfit(NetworkMessage_ptr msg, const Creature* creature, const Outfit_t& outfit, bool outfitWindow/* = false*/)
  2852. {
  2853. if(outfitWindow || !creature->getPlayer() || (!creature->isInvisible() && (!creature->isGhost()
  2854. || !g_config.getBool(ConfigManager::GHOST_INVISIBLE_EFFECT))))
  2855. {
  2856. msg->AddU16(outfit.lookType);
  2857. if(outfit.lookType)
  2858. {
  2859. msg->AddByte(outfit.lookHead);
  2860. msg->AddByte(outfit.lookBody);
  2861. msg->AddByte(outfit.lookLegs);
  2862. msg->AddByte(outfit.lookFeet);
  2863. msg->AddByte(outfit.lookAddons);
  2864. }
  2865. else if(outfit.lookTypeEx)
  2866. msg->AddItemId(outfit.lookTypeEx);
  2867. else
  2868. msg->AddU16(outfit.lookTypeEx);
  2869. }
  2870. else
  2871. msg->AddU32(0x00);
  2872. }
  2873.  
  2874. void ProtocolGame::AddWorldLight(NetworkMessage_ptr msg, const LightInfo& lightInfo)
  2875. {
  2876. msg->AddByte(0x82);
  2877. msg->AddByte((player->hasCustomFlag(PlayerCustomFlag_HasFullLight) ? 0xFF : lightInfo.level));
  2878. msg->AddByte(lightInfo.color);
  2879. }
  2880.  
  2881. void ProtocolGame::AddCreatureLight(NetworkMessage_ptr msg, const Creature* creature)
  2882. {
  2883. LightInfo lightInfo;
  2884. creature->getCreatureLight(lightInfo);
  2885. msg->AddByte(0x8D);
  2886. msg->AddU32(creature->getID());
  2887. msg->AddByte((player->hasCustomFlag(PlayerCustomFlag_HasFullLight) ? 0xFF : lightInfo.level));
  2888. msg->AddByte(lightInfo.color);
  2889. }
  2890.  
  2891. //tile
  2892. void ProtocolGame::AddTileItem(NetworkMessage_ptr msg, const Position& pos, uint32_t stackpos, const Item* item)
  2893. {
  2894. if(stackpos >= 10)
  2895. return;
  2896.  
  2897. msg->AddByte(0x6A);
  2898. msg->AddPosition(pos);
  2899. msg->AddByte(stackpos);
  2900. msg->AddItem(item);
  2901. }
  2902.  
  2903. void ProtocolGame::AddTileCreature(NetworkMessage_ptr msg, const Position& pos, uint32_t stackpos, const Creature* creature)
  2904. {
  2905. if(stackpos >= 10)
  2906. return;
  2907.  
  2908. msg->AddByte(0x6A);
  2909. msg->AddPosition(pos);
  2910. msg->AddByte(stackpos);
  2911.  
  2912. bool known;
  2913. uint32_t removedKnown;
  2914. checkCreatureAsKnown(creature->getID(), known, removedKnown);
  2915. AddCreature(msg, creature, known, removedKnown);
  2916. }
  2917.  
  2918. void ProtocolGame::UpdateTileItem(NetworkMessage_ptr msg, const Position& pos, uint32_t stackpos, const Item* item)
  2919. {
  2920. if(stackpos >= 10)
  2921. return;
  2922.  
  2923. msg->AddByte(0x6B);
  2924. msg->AddPosition(pos);
  2925. msg->AddByte(stackpos);
  2926. msg->AddItem(item);
  2927. }
  2928.  
  2929. void ProtocolGame::RemoveTileItem(NetworkMessage_ptr msg, const Position& pos, uint32_t stackpos)
  2930. {
  2931. if(stackpos >= 10)
  2932. return;
  2933.  
  2934. msg->AddByte(0x6C);
  2935. msg->AddPosition(pos);
  2936. msg->AddByte(stackpos);
  2937. }
  2938.  
  2939. void ProtocolGame::MoveUpCreature(NetworkMessage_ptr msg, const Creature* creature,
  2940. const Position& newPos, const Position& oldPos, uint32_t oldStackpos)
  2941. {
  2942. if(creature != player)
  2943. return;
  2944.  
  2945. msg->AddByte(0xBE); //floor change up
  2946. if(newPos.z == 7) //going to surface
  2947. {
  2948. int32_t skip = -1;
  2949. GetFloorDescription(msg, oldPos.x - 14, oldPos.y - 8, 5, (14 * 2) + 2, (8 * 2) + 2, 3, skip); //(floor 7 and 6 already set)
  2950. GetFloorDescription(msg, oldPos.x - 14, oldPos.y - 8, 4, (14 * 2) + 2, (8 * 2) + 2, 4, skip);
  2951. GetFloorDescription(msg, oldPos.x - 14, oldPos.y - 8, 3, (14 * 2) + 2, (8 * 2) + 2, 5, skip);
  2952. GetFloorDescription(msg, oldPos.x - 14, oldPos.y - 8, 2, (14 * 2) + 2, (8 * 2) + 2, 6, skip);
  2953. GetFloorDescription(msg, oldPos.x - 14, oldPos.y - 8, 1, (14 * 2) + 2, (8 * 2) + 2, 7, skip);
  2954. GetFloorDescription(msg, oldPos.x - 14, oldPos.y - 8, 0, (14 * 2) + 2, (8 * 2) + 2, 8, skip);
  2955. if(skip >= 0)
  2956. {
  2957. msg->AddByte(skip);
  2958. msg->AddByte(0xFF);
  2959. }
  2960. }
  2961. else if(newPos.z > 7) //underground, going one floor up (still underground)
  2962. {
  2963. int32_t skip = -1;
  2964. GetFloorDescription(msg, oldPos.x - 14, oldPos.y - 8, oldPos.z - 3, (14 * 2) + 2, (8 * 2) + 2, 3, skip);
  2965. if(skip >= 0)
  2966. {
  2967. msg->AddByte(skip);
  2968. msg->AddByte(0xFF);
  2969. }
  2970. }
  2971.  
  2972. //moving up a floor up makes us out of sync
  2973. //west
  2974. msg->AddByte(0x68);
  2975. GetMapDescription(oldPos.x - 14, oldPos.y + 1 - 8, newPos.z, 1, (8 * 2) + 2, msg);
  2976.  
  2977. //north
  2978. msg->AddByte(0x65);
  2979. GetMapDescription(oldPos.x - 14, oldPos.y - 8, newPos.z, (14 * 2) + 2, 1, msg);
  2980. }
  2981.  
  2982. void ProtocolGame::MoveDownCreature(NetworkMessage_ptr msg, const Creature* creature,
  2983. const Position& newPos, const Position& oldPos, uint32_t oldStackpos)
  2984. {
  2985. if(creature != player)
  2986. return;
  2987.  
  2988. msg->AddByte(0xBF); //floor change down
  2989. if(newPos.z == 8) //going from surface to underground
  2990. {
  2991. int32_t skip = -1;
  2992. GetFloorDescription(msg, oldPos.x - 14, oldPos.y - 8, newPos.z, (14 * 2) + 2, (8 * 2) + 2, -1, skip);
  2993. GetFloorDescription(msg, oldPos.x - 14, oldPos.y - 8, newPos.z + 1, (14 * 2) + 2, (8 * 2) + 2, -2, skip);
  2994. GetFloorDescription(msg, oldPos.x - 14, oldPos.y - 8, newPos.z + 2, (14 * 2) + 2, (8 * 2) + 2, -3, skip);
  2995. if(skip >= 0)
  2996. {
  2997. msg->AddByte(skip);
  2998. msg->AddByte(0xFF);
  2999. }
  3000. }
  3001. else if(newPos.z > oldPos.z && newPos.z > 8 && newPos.z < 14) //going further down
  3002. {
  3003. int32_t skip = -1;
  3004. GetFloorDescription(msg, oldPos.x - 14, oldPos.y - 8, newPos.z + 2, (14 * 2) + 2, (8 * 2) + 2, -3, skip);
  3005. if(skip >= 0)
  3006. {
  3007. msg->AddByte(skip);
  3008. msg->AddByte(0xFF);
  3009. }
  3010. }
  3011.  
  3012. //moving down a floor makes us out of sync
  3013. //east
  3014. msg->AddByte(0x66);
  3015. GetMapDescription(oldPos.x + 15, oldPos.y - 1 - 8, newPos.z, 1, (8 * 2) + 2, msg);
  3016.  
  3017. //south
  3018. msg->AddByte(0x67);
  3019. GetMapDescription(oldPos.x - 14, oldPos.y + 9, newPos.z, (14 * 2) + 2, 1, msg);
  3020. }
  3021.  
  3022. //inventory
  3023. void ProtocolGame::AddInventoryItem(NetworkMessage_ptr msg, slots_t slot, const Item* item)
  3024. {
  3025. if(item)
  3026. {
  3027. msg->AddByte(0x78);
  3028. msg->AddByte(slot);
  3029. msg->AddItem(item);
  3030. }
  3031. else
  3032. RemoveInventoryItem(msg, slot);
  3033. }
  3034.  
  3035. void ProtocolGame::RemoveInventoryItem(NetworkMessage_ptr msg, slots_t slot)
  3036. {
  3037. msg->AddByte(0x79);
  3038. msg->AddByte(slot);
  3039. }
  3040.  
  3041. void ProtocolGame::UpdateInventoryItem(NetworkMessage_ptr msg, slots_t slot, const Item* item)
  3042. {
  3043. AddInventoryItem(msg, slot, item);
  3044. }
  3045.  
  3046. //containers
  3047. void ProtocolGame::AddContainerItem(NetworkMessage_ptr msg, uint8_t cid, const Item* item)
  3048. {
  3049. msg->AddByte(0x70);
  3050. msg->AddByte(cid);
  3051. msg->AddItem(item);
  3052. }
  3053.  
  3054. void ProtocolGame::UpdateContainerItem(NetworkMessage_ptr msg, uint8_t cid, uint8_t slot, const Item* item)
  3055. {
  3056. msg->AddByte(0x71);
  3057. msg->AddByte(cid);
  3058. msg->AddByte(slot);
  3059. msg->AddItem(item);
  3060. }
  3061.  
  3062. void ProtocolGame::RemoveContainerItem(NetworkMessage_ptr msg, uint8_t cid, uint8_t slot)
  3063. {
  3064. msg->AddByte(0x72);
  3065. msg->AddByte(cid);
  3066. msg->AddByte(slot);
  3067. }
  3068.  
  3069. void ProtocolGame::sendChannelMessage(std::string author, std::string text, SpeakClasses type, uint8_t channel)
  3070. {
  3071. NetworkMessage_ptr msg = getOutputBuffer();
  3072. if(msg)
  3073. {
  3074. TRACK_MESSAGE(msg);
  3075. msg->AddByte(0xAA);
  3076. msg->AddU32(0x00);
  3077. msg->AddString(author);
  3078. msg->AddU16(0x00);
  3079. msg->AddByte(type);
  3080. msg->AddU16(channel);
  3081. msg->AddString(text);
  3082. }
  3083. }
  3084.  
  3085. void ProtocolGame::AddShopItem(NetworkMessage_ptr msg, const ShopInfo item)
  3086. {
  3087. const ItemType& it = Item::items[item.itemId];
  3088. msg->AddU16(it.clientId);
  3089. if(it.isSplash() || it.isFluidContainer())
  3090. msg->AddByte(fluidMap[item.subType % 8]);
  3091. else if(it.stackable || it.charges)
  3092. msg->AddByte(item.subType);
  3093. else
  3094. msg->AddByte(0x01);
  3095.  
  3096. msg->AddString(item.itemName);
  3097. msg->AddU32(uint32_t(it.weight * 100));
  3098. msg->AddU32(item.buyPrice);
  3099. msg->AddU32(item.sellPrice);
  3100. }
  3101.  
  3102. void ProtocolGame::sendReofferContainer(uint32_t cid, const Container* container, ReofferList reofferList, bool showOffers)
  3103. {
  3104. NetworkMessage_ptr msg = getOutputBuffer();
  3105. if(msg)
  3106. {
  3107. TRACK_MESSAGE(msg);
  3108. msg->AddByte(116);
  3109. msg->AddByte(cid);
  3110. msg->AddByte(showOffers ? 0x01 : 0x00);
  3111.  
  3112. msg->AddItemId(container);
  3113. msg->AddByte(reofferList.size());
  3114.  
  3115. for(ReofferList::iterator it = reofferList.begin(); it != reofferList.end(); ++it)
  3116. msg->AddItem(*it);
  3117. }
  3118. }
  3119.  
  3120. void ProtocolGame::sendMarketStatus(UpdateType_t status)
  3121. {
  3122. NetworkMessage_ptr msg = getOutputBuffer();
  3123. if(msg)
  3124. {
  3125. TRACK_MESSAGE(msg);
  3126. msg->AddByte(137);
  3127. msg->AddByte(status);
  3128. switch(status)
  3129. {
  3130. case UPDATE_BALANCE:
  3131. {
  3132. msg->AddU32(player->getBalance());
  3133. msg->AddU32(player->getPremiumBalance());
  3134. break;
  3135. }
  3136. case UPDATE_NOT_ENOUGH_MONEY:
  3137. break;
  3138.  
  3139. default:
  3140. break;
  3141. }
  3142. }
  3143. }
  3144.  
  3145. void ProtocolGame::sendRemoveOffer(uint16_t id)
  3146. {
  3147. NetworkMessage_ptr msg = getOutputBuffer();
  3148. if(msg)
  3149. {
  3150. TRACK_MESSAGE(msg);
  3151. msg->AddByte(136);
  3152. msg->AddU16(id);
  3153. }
  3154. }
  3155.  
  3156. void ProtocolGame::sendUpdateOffer(uint16_t id, uint16_t count)
  3157. {
  3158. NetworkMessage_ptr msg = getOutputBuffer();
  3159. if(msg)
  3160. {
  3161. TRACK_MESSAGE(msg);
  3162. msg->AddByte(115);
  3163. msg->AddU16(id);
  3164. msg->AddU16(count);
  3165. }
  3166. }
  3167.  
  3168. void ProtocolGame::parseLookMarketItem(NetworkMessage& msg)
  3169. {
  3170. uint16_t var = msg.GetU16();
  3171. uint16_t id = msg.GetU16();
  3172. addGameTask(&Game::playerLookMarketItem, player->getID(), var, id);
  3173. }
  3174.  
  3175. void ProtocolGame::sendMarketOffer(OfferVector& offerVector, UpdateType_t type, double size)
  3176. {
  3177. NetworkMessage_ptr msg = getOutputBuffer();
  3178. if(msg)
  3179. {
  3180. TRACK_MESSAGE(msg);
  3181. msg->AddByte(128);
  3182. msg->AddU16(type);
  3183. msg->AddU16((uint16_t)std::ceil(size / (double)g_config.getNumber(ConfigManager::MARKET_OFFER_PER_PAGE)));
  3184. msg->AddU16(offerVector.size());
  3185. if(offerVector.empty())
  3186. return;
  3187.  
  3188. for(OfferVector::iterator it = offerVector.begin(); it != offerVector.end(); ++it)
  3189. {
  3190. msg->AddU16((*it)->getId());
  3191.  
  3192. Item* item = (*it)->getItem();
  3193. if(!item)
  3194. {
  3195. const ItemType &lit = Item::items[(*it)->getItemId()];
  3196. msg->AddU16(lit.clientId);
  3197. if(lit.stackable/* || lit.isRune()*/)
  3198. msg->AddByte((*it)->getCount());
  3199. else if(lit.isSplash() || lit.isFluidContainer())
  3200. msg->AddByte(fluidMap[(*it)->getCount() % 8]);
  3201.  
  3202. msg->AddU16(0);
  3203. }
  3204. else
  3205. msg->AddItem(item);
  3206.  
  3207. std::string className;
  3208. if(item)
  3209. msg->AddString(item->getName());
  3210. else
  3211. msg->AddString((*it)->getName());
  3212.  
  3213. if(className.empty())
  3214. className = "normal";
  3215.  
  3216. msg->AddString(className);
  3217. msg->AddU32((*it)->getTime() - (OTSYS_TIME() / 1000));
  3218. msg->AddU64((*it)->getPrice());
  3219. msg->AddU64((*it)->getPremiumPrice());
  3220. msg->AddString((*it)->getDescription());
  3221. msg->AddString((*it)->getOwner());
  3222. msg->AddString((*it)->getBuyer());
  3223. msg->AddByte((*it)->hasReoffers() ? 0x01 : 0x00);
  3224. }
  3225. }
  3226. }
  3227.  
  3228. void ProtocolGame::parseAddMarketItem(NetworkMessage& msg)
  3229. {
  3230. Position pos = msg.GetPosition();
  3231. uint16_t spriteId = msg.GetSpriteId();
  3232. int16_t stackpos = msg.GetByte();
  3233.  
  3234. uint16_t count = msg.GetU16();
  3235. uint64_t price = msg.GetU64();
  3236. uint64_t premiumPrice = msg.GetU64();
  3237. std::string description = msg.GetString();
  3238.  
  3239. addGameTask(&Game::addMarketItem, player->getID(), pos, spriteId, stackpos, count, price, premiumPrice, description);
  3240. }
  3241.  
  3242. void ProtocolGame::parseUpdateMarketStatus(NetworkMessage& msg)
  3243. {
  3244. bool status = msg.GetByte() == 1;
  3245. addGameTask(&Game::updateMarketStatus, player->getID(), status);
  3246. }
  3247.  
  3248. void ProtocolGame::parseOpenReofferWindow(NetworkMessage& msg)
  3249. {
  3250. bool close = msg.GetByte() == 1;
  3251. uint16_t id = msg.GetU16();
  3252. ReofferList reofferList;
  3253. addGameTask(&Game::openReofferWindow, player->getID(), close, id, reofferList, false);
  3254. }
  3255.  
  3256. void ProtocolGame::parseUpdateOffer(NetworkMessage& msg)
  3257. {
  3258. uint16_t id = msg.GetU16();
  3259. uint16_t count = msg.GetU16();
  3260. MarketAction_t type = (MarketAction_t)msg.GetByte();
  3261.  
  3262. addGameTask(&Game::updateOffer, player->getID(), id, count, type);
  3263. }
  3264.  
  3265. void ProtocolGame::parseUpdateMarket(NetworkMessage& msg)
  3266. {
  3267. uint16_t type = msg.GetU16();
  3268. uint16_t page = msg.GetU16();
  3269. uint16_t category = (uint16_t)msg.GetByte();
  3270. CategorySort_t sort = (CategorySort_t)msg.GetByte();
  3271. const std::string search = msg.GetString();
  3272.  
  3273. addGameTask(&Game::updateMarket, player->getID(), type, page, category, sort, search);
  3274. }
Advertisement
Add Comment
Please, Sign In to add comment