No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

game.cpp 44KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679
  1. /*
  2. * Copyright (c) 2010-2015 OTClient <https://github.com/edubart/otclient>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #include "game.h"
  23. #include "localplayer.h"
  24. #include "map.h"
  25. #include "tile.h"
  26. #include "creature.h"
  27. #include "container.h"
  28. #include "statictext.h"
  29. #include <framework/core/eventdispatcher.h>
  30. #include <framework/ui/uimanager.h>
  31. #include <framework/core/application.h>
  32. #include "luavaluecasts.h"
  33. #include "protocolgame.h"
  34. #include "protocolcodes.h"
  35. Game g_game;
  36. Game::Game()
  37. {
  38. m_protocolVersion = 0;
  39. m_clientCustomOs = -1;
  40. m_clientVersion = 0;
  41. m_online = false;
  42. m_denyBotCall = false;
  43. m_dead = false;
  44. m_serverBeat = 50;
  45. m_seq = 0;
  46. m_ping = -1;
  47. m_pingDelay = 1000;
  48. m_canReportBugs = false;
  49. m_fightMode = Otc::FightBalanced;
  50. m_chaseMode = Otc::DontChase;
  51. m_pvpMode = Otc::WhiteDove;
  52. m_safeFight = true;
  53. }
  54. void Game::init()
  55. {
  56. resetGameStates();
  57. }
  58. void Game::terminate()
  59. {
  60. resetGameStates();
  61. m_protocolGame = nullptr;
  62. }
  63. void Game::resetGameStates()
  64. {
  65. m_online = false;
  66. m_denyBotCall = false;
  67. m_dead = false;
  68. m_serverBeat = 50;
  69. m_seq = 0;
  70. m_ping = -1;
  71. m_canReportBugs = false;
  72. m_fightMode = Otc::FightBalanced;
  73. m_chaseMode = Otc::DontChase;
  74. m_pvpMode = Otc::WhiteDove;
  75. m_safeFight = true;
  76. m_followingCreature = nullptr;
  77. m_attackingCreature = nullptr;
  78. m_localPlayer = nullptr;
  79. m_pingSent = 0;
  80. m_pingReceived = 0;
  81. m_unjustifiedPoints = UnjustifiedPoints();
  82. for(auto& it : m_containers) {
  83. const ContainerPtr& container = it.second;
  84. if(container)
  85. container->onClose();
  86. }
  87. if(m_pingEvent) {
  88. m_pingEvent->cancel();
  89. m_pingEvent = nullptr;
  90. }
  91. if(m_walkEvent) {
  92. m_walkEvent->cancel();
  93. m_walkEvent = nullptr;
  94. }
  95. if(m_checkConnectionEvent) {
  96. m_checkConnectionEvent->cancel();
  97. m_checkConnectionEvent = nullptr;
  98. }
  99. m_containers.clear();
  100. m_vips.clear();
  101. m_gmActions.clear();
  102. g_map.resetAwareRange();
  103. }
  104. void Game::processConnectionError(const boost::system::error_code& ec)
  105. {
  106. // connection errors only have meaning if we still have a protocol
  107. if(m_protocolGame) {
  108. // eof = end of file, a clean disconnect
  109. if(ec != asio::error::eof)
  110. g_lua.callGlobalField("g_game", "onConnectionError", ec.message(), ec.value());
  111. processDisconnect();
  112. }
  113. }
  114. void Game::processDisconnect()
  115. {
  116. if(isOnline())
  117. processGameEnd();
  118. if(m_protocolGame) {
  119. m_protocolGame->disconnect();
  120. m_protocolGame = nullptr;
  121. }
  122. }
  123. void Game::processUpdateNeeded(const std::string& signature)
  124. {
  125. g_lua.callGlobalField("g_game", "onUpdateNeeded", signature);
  126. }
  127. void Game::processLoginError(const std::string& error)
  128. {
  129. g_lua.callGlobalField("g_game", "onLoginError", error);
  130. }
  131. void Game::processLoginAdvice(const std::string& message)
  132. {
  133. g_lua.callGlobalField("g_game", "onLoginAdvice", message);
  134. }
  135. void Game::processLoginWait(const std::string& message, int time)
  136. {
  137. g_lua.callGlobalField("g_game", "onLoginWait", message, time);
  138. }
  139. void Game::processLoginToken(bool unknown)
  140. {
  141. g_lua.callGlobalField("g_game", "onLoginToken", unknown);
  142. }
  143. void Game::processLogin()
  144. {
  145. g_lua.callGlobalField("g_game", "onLogin");
  146. }
  147. void Game::processPendingGame()
  148. {
  149. m_localPlayer->setPendingGame(true);
  150. g_lua.callGlobalField("g_game", "onPendingGame");
  151. m_protocolGame->sendEnterGame();
  152. }
  153. void Game::processEnterGame()
  154. {
  155. m_localPlayer->setPendingGame(false);
  156. g_lua.callGlobalField("g_game", "onEnterGame");
  157. }
  158. void Game::processGameStart()
  159. {
  160. m_online = true;
  161. // synchronize fight modes with the server
  162. m_protocolGame->sendChangeFightModes(m_fightMode, m_chaseMode, m_safeFight, m_pvpMode);
  163. // NOTE: the entire map description and local player information is not known yet (bot call is allowed here)
  164. enableBotCall();
  165. g_lua.callGlobalField("g_game", "onGameStart");
  166. disableBotCall();
  167. if(g_game.getFeature(Otc::GameClientPing) || g_game.getFeature(Otc::GameExtendedClientPing)) {
  168. m_pingEvent = g_dispatcher.scheduleEvent([this] {
  169. g_game.ping();
  170. }, m_pingDelay);
  171. }
  172. m_checkConnectionEvent = g_dispatcher.cycleEvent([this] {
  173. if(!g_game.isConnectionOk() && !m_connectionFailWarned) {
  174. g_lua.callGlobalField("g_game", "onConnectionFailing", true);
  175. m_connectionFailWarned = true;
  176. } else if(g_game.isConnectionOk() && m_connectionFailWarned) {
  177. g_lua.callGlobalField("g_game", "onConnectionFailing", false);
  178. m_connectionFailWarned = false;
  179. }
  180. }, 1000);
  181. }
  182. void Game::processGameEnd()
  183. {
  184. m_online = false;
  185. g_lua.callGlobalField("g_game", "onGameEnd");
  186. if(m_connectionFailWarned) {
  187. g_lua.callGlobalField("g_game", "onConnectionFailing", false);
  188. m_connectionFailWarned = false;
  189. }
  190. // reset game state
  191. resetGameStates();
  192. m_worldName = "";
  193. m_characterName = "";
  194. // clean map creatures
  195. g_map.cleanDynamicThings();
  196. }
  197. void Game::processDeath(int deathType, int penality)
  198. {
  199. m_dead = true;
  200. m_localPlayer->stopWalk();
  201. g_lua.callGlobalField("g_game", "onDeath", deathType, penality);
  202. }
  203. void Game::processGMActions(const std::vector<uint8>& actions)
  204. {
  205. m_gmActions = actions;
  206. g_lua.callGlobalField("g_game", "onGMActions", actions);
  207. }
  208. void Game::processPlayerHelpers(int helpers)
  209. {
  210. g_lua.callGlobalField("g_game", "onPlayerHelpersUpdate", helpers);
  211. }
  212. void Game::processPlayerModes(Otc::FightModes fightMode, Otc::ChaseModes chaseMode, bool safeMode, Otc::PVPModes pvpMode)
  213. {
  214. m_fightMode = fightMode;
  215. m_chaseMode = chaseMode;
  216. m_safeFight = safeMode;
  217. m_pvpMode = pvpMode;
  218. g_lua.callGlobalField("g_game", "onFightModeChange", fightMode);
  219. g_lua.callGlobalField("g_game", "onChaseModeChange", chaseMode);
  220. g_lua.callGlobalField("g_game", "onSafeFightChange", safeMode);
  221. g_lua.callGlobalField("g_game", "onPVPModeChange", pvpMode);
  222. }
  223. void Game::processPing()
  224. {
  225. g_lua.callGlobalField("g_game", "onPing");
  226. enableBotCall();
  227. m_protocolGame->sendPingBack();
  228. disableBotCall();
  229. }
  230. void Game::processPingBack()
  231. {
  232. m_pingReceived++;
  233. if(m_pingReceived == m_pingSent)
  234. m_ping = m_pingTimer.elapsed_millis();
  235. else
  236. g_logger.error("got an invalid ping from server");
  237. g_lua.callGlobalField("g_game", "onPingBack", m_ping);
  238. m_pingEvent = g_dispatcher.scheduleEvent([this] {
  239. g_game.ping();
  240. }, m_pingDelay);
  241. }
  242. void Game::processTextMessage(Otc::MessageMode mode, const std::string& text)
  243. {
  244. g_lua.callGlobalField("g_game", "onTextMessage", mode, text);
  245. }
  246. void Game::processTalk(const std::string& name, int level, Otc::MessageMode mode, const std::string& text, int channelId, const Position& pos)
  247. {
  248. g_lua.callGlobalField("g_game", "onTalk", name, level, mode, text, channelId, pos);
  249. }
  250. void Game::processOpenContainer(int containerId, const ItemPtr& containerItem, const std::string& name, int capacity, bool hasParent, const std::vector<ItemPtr>& items, bool isUnlocked, bool hasPages, int containerSize, int firstIndex)
  251. {
  252. ContainerPtr previousContainer = getContainer(containerId);
  253. ContainerPtr container = ContainerPtr(new Container(containerId, capacity, name, containerItem, hasParent, isUnlocked, hasPages, containerSize, firstIndex));
  254. m_containers[containerId] = container;
  255. container->onAddItems(items);
  256. // we might want to close a container here
  257. enableBotCall();
  258. container->onOpen(previousContainer);
  259. disableBotCall();
  260. if(previousContainer)
  261. previousContainer->onClose();
  262. }
  263. void Game::processCloseContainer(int containerId)
  264. {
  265. ContainerPtr container = getContainer(containerId);
  266. if(!container) {
  267. return;
  268. }
  269. m_containers[containerId] = nullptr;
  270. container->onClose();
  271. }
  272. void Game::processContainerAddItem(int containerId, const ItemPtr& item, int slot)
  273. {
  274. ContainerPtr container = getContainer(containerId);
  275. if(!container) {
  276. return;
  277. }
  278. container->onAddItem(item, slot);
  279. }
  280. void Game::processContainerUpdateItem(int containerId, int slot, const ItemPtr& item)
  281. {
  282. ContainerPtr container = getContainer(containerId);
  283. if(!container) {
  284. return;
  285. }
  286. container->onUpdateItem(slot, item);
  287. }
  288. void Game::processContainerRemoveItem(int containerId, int slot, const ItemPtr& lastItem)
  289. {
  290. ContainerPtr container = getContainer(containerId);
  291. if(!container) {
  292. return;
  293. }
  294. container->onRemoveItem(slot, lastItem);
  295. }
  296. void Game::processInventoryChange(int slot, const ItemPtr& item)
  297. {
  298. if(item)
  299. item->setPosition(Position(65535, slot, 0));
  300. m_localPlayer->setInventoryItem((Otc::InventorySlot)slot, item);
  301. }
  302. void Game::processChannelList(const std::vector<std::tuple<int, std::string> >& channelList)
  303. {
  304. g_lua.callGlobalField("g_game", "onChannelList", channelList);
  305. }
  306. void Game::processOpenChannel(int channelId, const std::string& name)
  307. {
  308. g_lua.callGlobalField("g_game", "onOpenChannel", channelId, name);
  309. }
  310. void Game::processOpenPrivateChannel(const std::string& name)
  311. {
  312. g_lua.callGlobalField("g_game", "onOpenPrivateChannel", name);
  313. }
  314. void Game::processOpenOwnPrivateChannel(int channelId, const std::string& name)
  315. {
  316. g_lua.callGlobalField("g_game", "onOpenOwnPrivateChannel", channelId, name);
  317. }
  318. void Game::processCloseChannel(int channelId)
  319. {
  320. g_lua.callGlobalField("g_game", "onCloseChannel", channelId);
  321. }
  322. void Game::processRuleViolationChannel(int channelId)
  323. {
  324. g_lua.callGlobalField("g_game", "onRuleViolationChannel", channelId);
  325. }
  326. void Game::processRuleViolationRemove(const std::string& name)
  327. {
  328. g_lua.callGlobalField("g_game", "onRuleViolationRemove", name);
  329. }
  330. void Game::processRuleViolationCancel(const std::string& name)
  331. {
  332. g_lua.callGlobalField("g_game", "onRuleViolationCancel", name);
  333. }
  334. void Game::processRuleViolationLock()
  335. {
  336. g_lua.callGlobalField("g_game", "onRuleViolationLock");
  337. }
  338. void Game::processVipAdd(uint id, const std::string& name, uint status, const std::string& description, int iconId, bool notifyLogin)
  339. {
  340. m_vips[id] = Vip(name, status, description, iconId, notifyLogin);
  341. g_lua.callGlobalField("g_game", "onAddVip", id, name, status, description, iconId, notifyLogin);
  342. }
  343. void Game::processVipStateChange(uint id, uint status)
  344. {
  345. std::get<1>(m_vips[id]) = status;
  346. g_lua.callGlobalField("g_game", "onVipStateChange", id, status);
  347. }
  348. void Game::processTutorialHint(int id)
  349. {
  350. g_lua.callGlobalField("g_game", "onTutorialHint", id);
  351. }
  352. void Game::processAddAutomapFlag(const Position& pos, int icon, const std::string& message)
  353. {
  354. g_lua.callGlobalField("g_game", "onAddAutomapFlag", pos, icon, message);
  355. }
  356. void Game::processRemoveAutomapFlag(const Position& pos, int icon, const std::string& message)
  357. {
  358. g_lua.callGlobalField("g_game", "onRemoveAutomapFlag", pos, icon, message);
  359. }
  360. void Game::processOpenOutfitWindow(const Outfit& currentOufit, const std::vector<std::tuple<int, std::string, int> >& outfitList,
  361. const std::vector<std::tuple<int, std::string> >& mountList)
  362. {
  363. // create virtual creature outfit
  364. CreaturePtr virtualOutfitCreature = CreaturePtr(new Creature);
  365. virtualOutfitCreature->setDirection(Otc::South);
  366. Outfit outfit = currentOufit;
  367. outfit.setMount(0);
  368. virtualOutfitCreature->setOutfit(outfit);
  369. // creature virtual mount outfit
  370. CreaturePtr virtualMountCreature = nullptr;
  371. if(getFeature(Otc::GamePlayerMounts))
  372. {
  373. virtualMountCreature = CreaturePtr(new Creature);
  374. virtualMountCreature->setDirection(Otc::South);
  375. Outfit mountOutfit;
  376. mountOutfit.setId(0);
  377. int mount = currentOufit.getMount();
  378. if(mount > 0)
  379. mountOutfit.setId(mount);
  380. virtualMountCreature->setOutfit(mountOutfit);
  381. }
  382. g_lua.callGlobalField("g_game", "onOpenOutfitWindow", virtualOutfitCreature, outfitList, virtualMountCreature, mountList);
  383. }
  384. void Game::processOpenNpcTrade(const std::vector<std::tuple<ItemPtr, std::string, int, int, int> >& items)
  385. {
  386. g_lua.callGlobalField("g_game", "onOpenNpcTrade", items);
  387. }
  388. void Game::processPlayerGoods(int money, const std::vector<std::tuple<ItemPtr, int> >& goods)
  389. {
  390. g_lua.callGlobalField("g_game", "onPlayerGoods", money, goods);
  391. }
  392. void Game::processCloseNpcTrade()
  393. {
  394. g_lua.callGlobalField("g_game", "onCloseNpcTrade");
  395. }
  396. void Game::processOwnTrade(const std::string& name, const std::vector<ItemPtr>& items)
  397. {
  398. g_lua.callGlobalField("g_game", "onOwnTrade", name, items);
  399. }
  400. void Game::processCounterTrade(const std::string& name, const std::vector<ItemPtr>& items)
  401. {
  402. g_lua.callGlobalField("g_game", "onCounterTrade", name, items);
  403. }
  404. void Game::processCloseTrade()
  405. {
  406. g_lua.callGlobalField("g_game", "onCloseTrade");
  407. }
  408. void Game::processEditText(uint id, int itemId, int maxLength, const std::string& text, const std::string& writer, const std::string& date)
  409. {
  410. g_lua.callGlobalField("g_game", "onEditText", id, itemId, maxLength, text, writer, date);
  411. }
  412. void Game::processEditList(uint id, int doorId, const std::string& text)
  413. {
  414. g_lua.callGlobalField("g_game", "onEditList", id, doorId, text);
  415. }
  416. void Game::processQuestLog(const std::vector<std::tuple<int, std::string, bool> >& questList)
  417. {
  418. g_lua.callGlobalField("g_game", "onQuestLog", questList);
  419. }
  420. void Game::processQuestLine(int questId, const std::vector<std::tuple<std::string, std::string> >& questMissions)
  421. {
  422. g_lua.callGlobalField("g_game", "onQuestLine", questId, questMissions);
  423. }
  424. void Game::processModalDialog(uint32 id, std::string title, std::string message, std::vector<std::tuple<int, std::string> > buttonList, int enterButton, int escapeButton, std::vector<std::tuple<int, std::string> > choiceList, bool priority)
  425. {
  426. g_lua.callGlobalField("g_game", "onModalDialog", id, title, message, buttonList, enterButton, escapeButton, choiceList, priority);
  427. }
  428. void Game::processAttackCancel(uint seq)
  429. {
  430. if(isAttacking() && (seq == 0 || m_seq == seq))
  431. cancelAttack();
  432. }
  433. void Game::processWalkCancel(Otc::Direction direction)
  434. {
  435. m_localPlayer->cancelWalk(direction);
  436. }
  437. void Game::loginWorld(const std::string& account, const std::string& password, const std::string& worldName, const std::string& worldHost, int worldPort, const std::string& characterName, const std::string& authenticatorToken, const std::string& sessionKey)
  438. {
  439. if(m_protocolGame || isOnline())
  440. stdext::throw_exception("Unable to login into a world while already online or logging.");
  441. if(m_protocolVersion == 0)
  442. stdext::throw_exception("Must set a valid game protocol version before logging.");
  443. // reset the new game state
  444. resetGameStates();
  445. m_localPlayer = LocalPlayerPtr(new LocalPlayer);
  446. m_localPlayer->setName(characterName);
  447. m_protocolGame = ProtocolGamePtr(new ProtocolGame);
  448. m_protocolGame->login(account, password, worldHost, (uint16)worldPort, characterName, authenticatorToken, sessionKey);
  449. m_characterName = characterName;
  450. m_worldName = worldName;
  451. }
  452. void Game::cancelLogin()
  453. {
  454. // send logout even if the game has not started yet, to make sure that the player doesn't stay logged there
  455. if(m_protocolGame)
  456. m_protocolGame->sendLogout();
  457. processDisconnect();
  458. }
  459. void Game::forceLogout()
  460. {
  461. if(!isOnline())
  462. return;
  463. m_protocolGame->sendLogout();
  464. processDisconnect();
  465. }
  466. void Game::safeLogout()
  467. {
  468. if(!isOnline())
  469. return;
  470. m_protocolGame->sendLogout();
  471. }
  472. bool Game::walk(Otc::Direction direction, bool dash)
  473. {
  474. if(!canPerformGameAction())
  475. return false;
  476. // must cancel follow before any new walk
  477. if(isFollowing())
  478. cancelFollow();
  479. // must cancel auto walking, and wait next try
  480. if(m_localPlayer->isAutoWalking() || m_localPlayer->isServerWalking()) {
  481. m_protocolGame->sendStop();
  482. if(m_localPlayer->isAutoWalking())
  483. m_localPlayer->stopAutoWalk();
  484. return false;
  485. }
  486. if(dash) {
  487. if(m_localPlayer->isWalking() && m_dashTimer.ticksElapsed() < std::max<int>(m_localPlayer->getStepDuration(false, direction) - m_ping, 30))
  488. return false;
  489. }
  490. else {
  491. // check we can walk and add new walk event if false
  492. if(!m_localPlayer->canWalk(direction)) {
  493. if(m_lastWalkDir != direction) {
  494. // must add a new walk event
  495. float ticks = m_localPlayer->getStepTicksLeft();
  496. if(ticks <= 0) { ticks = 1; }
  497. if(m_walkEvent) {
  498. m_walkEvent->cancel();
  499. m_walkEvent = nullptr;
  500. }
  501. m_walkEvent = g_dispatcher.scheduleEvent([=] { walk(direction, false); }, ticks);
  502. }
  503. return false;
  504. }
  505. }
  506. Position toPos = m_localPlayer->getPosition().translatedToDirection(direction);
  507. TilePtr toTile = g_map.getTile(toPos);
  508. // only do prewalks to walkable tiles (like grounds and not walls)
  509. if(toTile && toTile->isWalkable()) {
  510. m_localPlayer->preWalk(direction);
  511. // check walk to another floor (e.g: when above 3 parcels)
  512. } else {
  513. // check if can walk to a lower floor
  514. auto canChangeFloorDown = [&]() -> bool {
  515. Position pos = toPos;
  516. if(!pos.down())
  517. return false;
  518. TilePtr toTile = g_map.getTile(pos);
  519. if(toTile && toTile->hasElevation(3))
  520. return true;
  521. return false;
  522. };
  523. // check if can walk to a higher floor
  524. auto canChangeFloorUp = [&]() -> bool {
  525. TilePtr fromTile = m_localPlayer->getTile();
  526. if(!fromTile || !fromTile->hasElevation(3))
  527. return false;
  528. Position pos = toPos;
  529. if(!pos.up())
  530. return false;
  531. TilePtr toTile = g_map.getTile(pos);
  532. if(!toTile || !toTile->isWalkable())
  533. return false;
  534. return true;
  535. };
  536. if(canChangeFloorDown() || canChangeFloorUp() ||
  537. (!toTile || toTile->isEmpty())) {
  538. m_localPlayer->lockWalk();
  539. } else
  540. return false;
  541. }
  542. m_localPlayer->stopAutoWalk();
  543. g_lua.callGlobalField("g_game", "onWalk", direction, dash);
  544. forceWalk(direction);
  545. if(dash)
  546. m_dashTimer.restart();
  547. m_lastWalkDir = direction;
  548. return true;
  549. }
  550. bool Game::dashWalk(Otc::Direction direction)
  551. {
  552. return walk(direction, true);
  553. }
  554. void Game::autoWalk(std::vector<Otc::Direction> dirs)
  555. {
  556. if(!canPerformGameAction())
  557. return;
  558. // protocol limits walk path up to 255 directions
  559. if(dirs.size() > 127) {
  560. g_logger.error("Auto walk path too great, the maximum number of directions is 127");
  561. return;
  562. }
  563. if(dirs.size() == 0)
  564. return;
  565. // must cancel follow before any new walk
  566. if(isFollowing())
  567. cancelFollow();
  568. auto it = dirs.begin();
  569. Otc::Direction direction = *it;
  570. if(!m_localPlayer->canWalk(direction))
  571. return;
  572. TilePtr toTile = g_map.getTile(m_localPlayer->getPosition().translatedToDirection(direction));
  573. if(toTile && toTile->isWalkable() && !m_localPlayer->isServerWalking()) {
  574. m_localPlayer->preWalk(direction);
  575. if(getFeature(Otc::GameForceFirstAutoWalkStep)) {
  576. forceWalk(direction);
  577. dirs.erase(it);
  578. }
  579. }
  580. g_lua.callGlobalField("g_game", "onAutoWalk", dirs);
  581. m_protocolGame->sendAutoWalk(dirs);
  582. }
  583. void Game::forceWalk(Otc::Direction direction)
  584. {
  585. if(!canPerformGameAction())
  586. return;
  587. switch(direction) {
  588. case Otc::North:
  589. m_protocolGame->sendWalkNorth();
  590. break;
  591. case Otc::East:
  592. m_protocolGame->sendWalkEast();
  593. break;
  594. case Otc::South:
  595. m_protocolGame->sendWalkSouth();
  596. break;
  597. case Otc::West:
  598. m_protocolGame->sendWalkWest();
  599. break;
  600. case Otc::NorthEast:
  601. m_protocolGame->sendWalkNorthEast();
  602. break;
  603. case Otc::SouthEast:
  604. m_protocolGame->sendWalkSouthEast();
  605. break;
  606. case Otc::SouthWest:
  607. m_protocolGame->sendWalkSouthWest();
  608. break;
  609. case Otc::NorthWest:
  610. m_protocolGame->sendWalkNorthWest();
  611. break;
  612. default:
  613. break;
  614. }
  615. g_lua.callGlobalField("g_game", "onForceWalk", direction);
  616. }
  617. void Game::turn(Otc::Direction direction)
  618. {
  619. if(!canPerformGameAction())
  620. return;
  621. switch(direction) {
  622. case Otc::North:
  623. m_protocolGame->sendTurnNorth();
  624. break;
  625. case Otc::East:
  626. m_protocolGame->sendTurnEast();
  627. break;
  628. case Otc::South:
  629. m_protocolGame->sendTurnSouth();
  630. break;
  631. case Otc::West:
  632. m_protocolGame->sendTurnWest();
  633. break;
  634. default:
  635. break;
  636. }
  637. }
  638. void Game::stop()
  639. {
  640. if(!canPerformGameAction())
  641. return;
  642. if(isFollowing())
  643. cancelFollow();
  644. m_protocolGame->sendStop();
  645. }
  646. void Game::look(const ThingPtr& thing)
  647. {
  648. if(!canPerformGameAction() || !thing)
  649. return;
  650. if(thing->isCreature() && m_protocolVersion >= 961)
  651. m_protocolGame->sendLookCreature(thing->getId());
  652. else
  653. m_protocolGame->sendLook(thing->getPosition(), thing->getId(), thing->getStackPos());
  654. }
  655. void Game::move(const ThingPtr& thing, const Position& toPos, int count)
  656. {
  657. if(count <= 0)
  658. count = 1;
  659. if(!canPerformGameAction() || !thing || thing->getPosition() == toPos)
  660. return;
  661. uint id = thing->getId();
  662. if(thing->isCreature()) {
  663. CreaturePtr creature = thing->static_self_cast<Creature>();
  664. id = Proto::Creature;
  665. }
  666. m_protocolGame->sendMove(thing->getPosition(), id, thing->getStackPos(), toPos, count);
  667. }
  668. void Game::moveToParentContainer(const ThingPtr& thing, int count)
  669. {
  670. if(!canPerformGameAction() || !thing || count <= 0)
  671. return;
  672. Position position = thing->getPosition();
  673. move(thing, Position(position.x, position.y, 254), count);
  674. }
  675. void Game::rotate(const ThingPtr& thing)
  676. {
  677. if(!canPerformGameAction() || !thing)
  678. return;
  679. m_protocolGame->sendRotateItem(thing->getPosition(), thing->getId(), thing->getStackPos());
  680. }
  681. void Game::use(const ThingPtr& thing)
  682. {
  683. if(!canPerformGameAction() || !thing)
  684. return;
  685. Position pos = thing->getPosition();
  686. if(!pos.isValid()) // virtual item
  687. pos = Position(0xFFFF, 0, 0); // inventory item
  688. // some items, e.g. parcel, are not set as containers but they are.
  689. // always try to use these items in free container slots.
  690. m_protocolGame->sendUseItem(pos, thing->getId(), thing->getStackPos(), findEmptyContainerId());
  691. }
  692. void Game::useInventoryItem(int itemId)
  693. {
  694. if(!canPerformGameAction() || !g_things.isValidDatId(itemId, ThingCategoryItem))
  695. return;
  696. Position pos = Position(0xFFFF, 0, 0); // means that is a item in inventory
  697. m_protocolGame->sendUseItem(pos, itemId, 0, 0);
  698. }
  699. void Game::useWith(const ItemPtr& item, const ThingPtr& toThing)
  700. {
  701. if(!canPerformGameAction() || !item || !toThing)
  702. return;
  703. Position pos = item->getPosition();
  704. if(!pos.isValid()) // virtual item
  705. pos = Position(0xFFFF, 0, 0); // means that is an item in inventory
  706. m_protocolGame->sendUseItemWith(pos, item->getId(), item->getStackPos(), toThing->getPosition(), toThing->getId(), toThing->getStackPos());
  707. }
  708. void Game::useInventoryItemWith(int itemId, const ThingPtr& toThing)
  709. {
  710. if(!canPerformGameAction() || !toThing)
  711. return;
  712. Position pos = Position(0xFFFF, 0, 0); // means that is a item in inventory
  713. if(toThing->isCreature())
  714. m_protocolGame->sendUseOnCreature(pos, itemId, 0, toThing->getId());
  715. else
  716. m_protocolGame->sendUseItemWith(pos, itemId, 0, toThing->getPosition(), toThing->getId(), toThing->getStackPos());
  717. }
  718. ItemPtr Game::findItemInContainers(uint itemId, int subType)
  719. {
  720. for(auto& it : m_containers) {
  721. const ContainerPtr& container = it.second;
  722. if(container) {
  723. ItemPtr item = container->findItemById(itemId, subType);
  724. if(item != nullptr)
  725. return item;
  726. }
  727. }
  728. return nullptr;
  729. }
  730. int Game::open(const ItemPtr& item, const ContainerPtr& previousContainer)
  731. {
  732. if(!canPerformGameAction() || !item)
  733. return -1;
  734. int id = 0;
  735. if(!previousContainer)
  736. id = findEmptyContainerId();
  737. else
  738. id = previousContainer->getId();
  739. m_protocolGame->sendUseItem(item->getPosition(), item->getId(), item->getStackPos(), id);
  740. return id;
  741. }
  742. void Game::openParent(const ContainerPtr& container)
  743. {
  744. if(!canPerformGameAction() || !container)
  745. return;
  746. m_protocolGame->sendUpContainer(container->getId());
  747. }
  748. void Game::close(const ContainerPtr& container)
  749. {
  750. if(!canPerformGameAction() || !container)
  751. return;
  752. m_protocolGame->sendCloseContainer(container->getId());
  753. }
  754. void Game::refreshContainer(const ContainerPtr& container)
  755. {
  756. if(!canPerformGameAction())
  757. return;
  758. m_protocolGame->sendRefreshContainer(container->getId());
  759. }
  760. void Game::attack(CreaturePtr creature)
  761. {
  762. if(!canPerformGameAction() || creature == m_localPlayer)
  763. return;
  764. // cancel when attacking again
  765. if(creature && creature == m_attackingCreature)
  766. creature = nullptr;
  767. if(creature && isFollowing())
  768. cancelFollow();
  769. setAttackingCreature(creature);
  770. m_localPlayer->stopAutoWalk();
  771. if(m_protocolVersion >= 963) {
  772. if(creature)
  773. m_seq = creature->getId();
  774. } else
  775. m_seq++;
  776. m_protocolGame->sendAttack(creature ? creature->getId() : 0, m_seq);
  777. }
  778. void Game::follow(CreaturePtr creature)
  779. {
  780. if(!canPerformGameAction() || creature == m_localPlayer)
  781. return;
  782. // cancel when following again
  783. if(creature && creature == m_followingCreature)
  784. creature = nullptr;
  785. if(creature && isAttacking())
  786. cancelAttack();
  787. setFollowingCreature(creature);
  788. m_localPlayer->stopAutoWalk();
  789. if(m_protocolVersion >= 963) {
  790. if(creature)
  791. m_seq = creature->getId();
  792. } else
  793. m_seq++;
  794. m_protocolGame->sendFollow(creature ? creature->getId() : 0, m_seq);
  795. }
  796. void Game::cancelAttackAndFollow()
  797. {
  798. if(!canPerformGameAction())
  799. return;
  800. if(isFollowing())
  801. setFollowingCreature(nullptr);
  802. if(isAttacking())
  803. setAttackingCreature(nullptr);
  804. m_localPlayer->stopAutoWalk();
  805. m_protocolGame->sendCancelAttackAndFollow();
  806. g_lua.callGlobalField("g_game", "onCancelAttackAndFollow");
  807. }
  808. void Game::talk(const std::string& message)
  809. {
  810. if(!canPerformGameAction() || message.empty())
  811. return;
  812. talkChannel(Otc::MessageSay, 0, message);
  813. }
  814. void Game::talkChannel(Otc::MessageMode mode, int channelId, const std::string& message)
  815. {
  816. if(!canPerformGameAction() || message.empty())
  817. return;
  818. m_protocolGame->sendTalk(mode, channelId, "", message);
  819. }
  820. void Game::talkPrivate(Otc::MessageMode mode, const std::string& receiver, const std::string& message)
  821. {
  822. if(!canPerformGameAction() || receiver.empty() || message.empty())
  823. return;
  824. m_protocolGame->sendTalk(mode, 0, receiver, message);
  825. }
  826. void Game::openPrivateChannel(const std::string& receiver)
  827. {
  828. if(!canPerformGameAction() || receiver.empty())
  829. return;
  830. m_protocolGame->sendOpenPrivateChannel(receiver);
  831. }
  832. void Game::requestChannels()
  833. {
  834. if(!canPerformGameAction())
  835. return;
  836. m_protocolGame->sendRequestChannels();
  837. }
  838. void Game::joinChannel(int channelId)
  839. {
  840. if(!canPerformGameAction())
  841. return;
  842. m_protocolGame->sendJoinChannel(channelId);
  843. }
  844. void Game::leaveChannel(int channelId)
  845. {
  846. if(!canPerformGameAction())
  847. return;
  848. m_protocolGame->sendLeaveChannel(channelId);
  849. }
  850. void Game::closeNpcChannel()
  851. {
  852. if(!canPerformGameAction())
  853. return;
  854. m_protocolGame->sendCloseNpcChannel();
  855. }
  856. void Game::openOwnChannel()
  857. {
  858. if(!canPerformGameAction())
  859. return;
  860. m_protocolGame->sendOpenOwnChannel();
  861. }
  862. void Game::inviteToOwnChannel(const std::string& name)
  863. {
  864. if(!canPerformGameAction() || name.empty())
  865. return;
  866. m_protocolGame->sendInviteToOwnChannel(name);
  867. }
  868. void Game::excludeFromOwnChannel(const std::string& name)
  869. {
  870. if(!canPerformGameAction() || name.empty())
  871. return;
  872. m_protocolGame->sendExcludeFromOwnChannel(name);
  873. }
  874. void Game::partyInvite(int creatureId)
  875. {
  876. if(!canPerformGameAction())
  877. return;
  878. m_protocolGame->sendInviteToParty(creatureId);
  879. }
  880. void Game::partyJoin(int creatureId)
  881. {
  882. if(!canPerformGameAction())
  883. return;
  884. m_protocolGame->sendJoinParty(creatureId);
  885. }
  886. void Game::partyRevokeInvitation(int creatureId)
  887. {
  888. if(!canPerformGameAction())
  889. return;
  890. m_protocolGame->sendRevokeInvitation(creatureId);
  891. }
  892. void Game::partyPassLeadership(int creatureId)
  893. {
  894. if(!canPerformGameAction())
  895. return;
  896. m_protocolGame->sendPassLeadership(creatureId);
  897. }
  898. void Game::partyLeave()
  899. {
  900. if(!canPerformGameAction())
  901. return;
  902. m_protocolGame->sendLeaveParty();
  903. }
  904. void Game::partyShareExperience(bool active)
  905. {
  906. if(!canPerformGameAction())
  907. return;
  908. m_protocolGame->sendShareExperience(active);
  909. }
  910. void Game::requestOutfit()
  911. {
  912. if(!canPerformGameAction())
  913. return;
  914. m_protocolGame->sendRequestOutfit();
  915. }
  916. void Game::changeOutfit(const Outfit& outfit)
  917. {
  918. if(!canPerformGameAction())
  919. return;
  920. m_protocolGame->sendChangeOutfit(outfit);
  921. }
  922. void Game::addVip(const std::string& name)
  923. {
  924. if(!canPerformGameAction() || name.empty())
  925. return;
  926. m_protocolGame->sendAddVip(name);
  927. }
  928. void Game::removeVip(int playerId)
  929. {
  930. if(!canPerformGameAction())
  931. return;
  932. auto it = m_vips.find(playerId);
  933. if(it == m_vips.end())
  934. return;
  935. m_vips.erase(it);
  936. m_protocolGame->sendRemoveVip(playerId);
  937. }
  938. void Game::editVip(int playerId, const std::string& description, int iconId, bool notifyLogin)
  939. {
  940. if(!canPerformGameAction())
  941. return;
  942. auto it = m_vips.find(playerId);
  943. if(it == m_vips.end())
  944. return;
  945. std::get<2>(m_vips[playerId]) = description;
  946. std::get<3>(m_vips[playerId]) = iconId;
  947. std::get<4>(m_vips[playerId]) = notifyLogin;
  948. if(getFeature(Otc::GameAdditionalVipInfo))
  949. m_protocolGame->sendEditVip(playerId, description, iconId, notifyLogin);
  950. }
  951. void Game::setChaseMode(Otc::ChaseModes chaseMode)
  952. {
  953. if(!canPerformGameAction())
  954. return;
  955. if(m_chaseMode == chaseMode)
  956. return;
  957. m_chaseMode = chaseMode;
  958. m_protocolGame->sendChangeFightModes(m_fightMode, m_chaseMode, m_safeFight, m_pvpMode);
  959. g_lua.callGlobalField("g_game", "onChaseModeChange", chaseMode);
  960. }
  961. void Game::setFightMode(Otc::FightModes fightMode)
  962. {
  963. if(!canPerformGameAction())
  964. return;
  965. if(m_fightMode == fightMode)
  966. return;
  967. m_fightMode = fightMode;
  968. m_protocolGame->sendChangeFightModes(m_fightMode, m_chaseMode, m_safeFight, m_pvpMode);
  969. g_lua.callGlobalField("g_game", "onFightModeChange", fightMode);
  970. }
  971. void Game::setSafeFight(bool on)
  972. {
  973. if(!canPerformGameAction())
  974. return;
  975. if(m_safeFight == on)
  976. return;
  977. m_safeFight = on;
  978. m_protocolGame->sendChangeFightModes(m_fightMode, m_chaseMode, m_safeFight, m_pvpMode);
  979. g_lua.callGlobalField("g_game", "onSafeFightChange", on);
  980. }
  981. void Game::setPVPMode(Otc::PVPModes pvpMode)
  982. {
  983. if(!canPerformGameAction())
  984. return;
  985. if(!getFeature(Otc::GamePVPMode))
  986. return;
  987. if(m_pvpMode == pvpMode)
  988. return;
  989. m_pvpMode = pvpMode;
  990. m_protocolGame->sendChangeFightModes(m_fightMode, m_chaseMode, m_safeFight, m_pvpMode);
  991. g_lua.callGlobalField("g_game", "onPVPModeChange", pvpMode);
  992. }
  993. void Game::setUnjustifiedPoints(UnjustifiedPoints unjustifiedPoints)
  994. {
  995. if(!canPerformGameAction())
  996. return;
  997. if(!getFeature(Otc::GameUnjustifiedPoints))
  998. return;
  999. if(m_unjustifiedPoints == unjustifiedPoints)
  1000. return;
  1001. m_unjustifiedPoints = unjustifiedPoints;
  1002. g_lua.callGlobalField("g_game", "onUnjustifiedPointsChange", unjustifiedPoints);
  1003. }
  1004. void Game::setOpenPvpSituations(int openPvpSituations)
  1005. {
  1006. if(!canPerformGameAction())
  1007. return;
  1008. if(m_openPvpSituations == openPvpSituations)
  1009. return;
  1010. m_openPvpSituations = openPvpSituations;
  1011. g_lua.callGlobalField("g_game", "onOpenPvpSituationsChange", openPvpSituations);
  1012. }
  1013. void Game::inspectNpcTrade(const ItemPtr& item)
  1014. {
  1015. if(!canPerformGameAction() || !item)
  1016. return;
  1017. m_protocolGame->sendInspectNpcTrade(item->getId(), item->getCount());
  1018. }
  1019. void Game::buyItem(const ItemPtr& item, int amount, bool ignoreCapacity, bool buyWithBackpack)
  1020. {
  1021. if(!canPerformGameAction() || !item)
  1022. return;
  1023. m_protocolGame->sendBuyItem(item->getId(), item->getCountOrSubType(), amount, ignoreCapacity, buyWithBackpack);
  1024. }
  1025. void Game::sellItem(const ItemPtr& item, int amount, bool ignoreEquipped)
  1026. {
  1027. if(!canPerformGameAction() || !item)
  1028. return;
  1029. m_protocolGame->sendSellItem(item->getId(), item->getSubType(), amount, ignoreEquipped);
  1030. }
  1031. void Game::closeNpcTrade()
  1032. {
  1033. if(!canPerformGameAction())
  1034. return;
  1035. m_protocolGame->sendCloseNpcTrade();
  1036. }
  1037. void Game::requestTrade(const ItemPtr& item, const CreaturePtr& creature)
  1038. {
  1039. if(!canPerformGameAction() || !item || !creature)
  1040. return;
  1041. m_protocolGame->sendRequestTrade(item->getPosition(), item->getId(), item->getStackPos(), creature->getId());
  1042. }
  1043. void Game::inspectTrade(bool counterOffer, int index)
  1044. {
  1045. if(!canPerformGameAction())
  1046. return;
  1047. m_protocolGame->sendInspectTrade(counterOffer, index);
  1048. }
  1049. void Game::acceptTrade()
  1050. {
  1051. if(!canPerformGameAction())
  1052. return;
  1053. m_protocolGame->sendAcceptTrade();
  1054. }
  1055. void Game::rejectTrade()
  1056. {
  1057. if(!canPerformGameAction())
  1058. return;
  1059. m_protocolGame->sendRejectTrade();
  1060. }
  1061. void Game::editText(uint id, const std::string& text)
  1062. {
  1063. if(!canPerformGameAction())
  1064. return;
  1065. m_protocolGame->sendEditText(id, text);
  1066. }
  1067. void Game::editList(uint id, int doorId, const std::string& text)
  1068. {
  1069. if(!canPerformGameAction())
  1070. return;
  1071. m_protocolGame->sendEditList(id, doorId, text);
  1072. }
  1073. void Game::openRuleViolation(const std::string& reporter)
  1074. {
  1075. if(!canPerformGameAction())
  1076. return;
  1077. m_protocolGame->sendOpenRuleViolation(reporter);
  1078. }
  1079. void Game::closeRuleViolation(const std::string& reporter)
  1080. {
  1081. if(!canPerformGameAction())
  1082. return;
  1083. m_protocolGame->sendCloseRuleViolation(reporter);
  1084. }
  1085. void Game::cancelRuleViolation()
  1086. {
  1087. if(!canPerformGameAction())
  1088. return;
  1089. m_protocolGame->sendCancelRuleViolation();
  1090. }
  1091. void Game::reportBug(const std::string& comment)
  1092. {
  1093. if(!canPerformGameAction())
  1094. return;
  1095. m_protocolGame->sendBugReport(comment);
  1096. }
  1097. void Game::reportRuleViolation(const std::string& target, int reason, int action, const std::string& comment, const std::string& statement, int statementId, bool ipBanishment)
  1098. {
  1099. if(!canPerformGameAction())
  1100. return;
  1101. m_protocolGame->sendRuleViolation(target, reason, action, comment, statement, statementId, ipBanishment);
  1102. }
  1103. void Game::debugReport(const std::string& a, const std::string& b, const std::string& c, const std::string& d)
  1104. {
  1105. m_protocolGame->sendDebugReport(a, b, c, d);
  1106. }
  1107. void Game::requestQuestLog()
  1108. {
  1109. if(!canPerformGameAction())
  1110. return;
  1111. m_protocolGame->sendRequestQuestLog();
  1112. }
  1113. void Game::requestQuestLine(int questId)
  1114. {
  1115. if(!canPerformGameAction())
  1116. return;
  1117. m_protocolGame->sendRequestQuestLine(questId);
  1118. }
  1119. void Game::equipItem(const ItemPtr& item)
  1120. {
  1121. if(!canPerformGameAction())
  1122. return;
  1123. m_protocolGame->sendEquipItem(item->getId(), item->getCountOrSubType());
  1124. }
  1125. void Game::mount(bool mount)
  1126. {
  1127. if(!canPerformGameAction())
  1128. return;
  1129. m_protocolGame->sendMountStatus(mount);
  1130. }
  1131. void Game::requestItemInfo(const ItemPtr& item, int index)
  1132. {
  1133. if(!canPerformGameAction())
  1134. return;
  1135. m_protocolGame->sendRequestItemInfo(item->getId(), item->getSubType(), index);
  1136. }
  1137. void Game::answerModalDialog(int dialog, int button, int choice)
  1138. {
  1139. if(!canPerformGameAction())
  1140. return;
  1141. m_protocolGame->sendAnswerModalDialog(dialog, button, choice);
  1142. }
  1143. void Game::browseField(const Position& position)
  1144. {
  1145. if(!canPerformGameAction())
  1146. return;
  1147. m_protocolGame->sendBrowseField(position);
  1148. }
  1149. void Game::seekInContainer(int cid, int index)
  1150. {
  1151. if(!canPerformGameAction())
  1152. return;
  1153. m_protocolGame->sendSeekInContainer(cid, index);
  1154. }
  1155. void Game::ping()
  1156. {
  1157. if(!m_protocolGame || !m_protocolGame->isConnected())
  1158. return;
  1159. if(m_pingReceived != m_pingSent)
  1160. return;
  1161. m_denyBotCall = false;
  1162. m_protocolGame->sendPing();
  1163. m_denyBotCall = true;
  1164. m_pingSent++;
  1165. m_pingTimer.restart();
  1166. }
  1167. void Game::changeMapAwareRange(int xrange, int yrange)
  1168. {
  1169. if(!canPerformGameAction())
  1170. return;
  1171. m_protocolGame->sendChangeMapAwareRange(xrange, yrange);
  1172. }
  1173. bool Game::checkBotProtection()
  1174. {
  1175. #ifdef BOT_PROTECTION
  1176. // accepts calls comming from a stacktrace containing only C++ functions,
  1177. // if the stacktrace contains a lua function, then only accept if the engine is processing an input event
  1178. if(m_denyBotCall && g_lua.isInCppCallback() && !g_app.isOnInputEvent()) {
  1179. g_logger.error(g_lua.traceback("caught a lua call to a bot protected game function, the call was cancelled"));
  1180. return false;
  1181. }
  1182. #endif
  1183. return true;
  1184. }
  1185. bool Game::canPerformGameAction()
  1186. {
  1187. // we can only perform game actions if we meet these conditions:
  1188. // - the game is online
  1189. // - the local player exists
  1190. // - the local player is not dead
  1191. // - we have a game protocol
  1192. // - the game protocol is connected
  1193. // - its not a bot action
  1194. return m_online && m_localPlayer && !m_dead && m_protocolGame && m_protocolGame->isConnected() && checkBotProtection();
  1195. }
  1196. void Game::setProtocolVersion(int version)
  1197. {
  1198. if(m_protocolVersion == version)
  1199. return;
  1200. if(isOnline())
  1201. stdext::throw_exception("Unable to change protocol version while online");
  1202. if(version != 0 && (version < 740 || version > 1076))
  1203. stdext::throw_exception(stdext::format("Protocol version %d not supported", version));
  1204. m_protocolVersion = version;
  1205. Proto::buildMessageModesMap(version);
  1206. g_lua.callGlobalField("g_game", "onProtocolVersionChange", version);
  1207. }
  1208. void Game::setClientVersion(int version)
  1209. {
  1210. if(m_clientVersion == version)
  1211. return;
  1212. if(isOnline())
  1213. stdext::throw_exception("Unable to change client version while online");
  1214. if(version != 0 && (version < 740 || version > 1076))
  1215. stdext::throw_exception(stdext::format("Client version %d not supported", version));
  1216. m_features.reset();
  1217. enableFeature(Otc::GameFormatCreatureName);
  1218. if(version >= 770) {
  1219. enableFeature(Otc::GameLooktypeU16);
  1220. enableFeature(Otc::GameMessageStatements);
  1221. enableFeature(Otc::GameLoginPacketEncryption);
  1222. }
  1223. if(version >= 780) {
  1224. enableFeature(Otc::GamePlayerAddons);
  1225. enableFeature(Otc::GamePlayerStamina);
  1226. enableFeature(Otc::GameNewFluids);
  1227. enableFeature(Otc::GameMessageLevel);
  1228. enableFeature(Otc::GamePlayerStateU16);
  1229. enableFeature(Otc::GameNewOutfitProtocol);
  1230. }
  1231. if(version >= 790) {
  1232. enableFeature(Otc::GameWritableDate);
  1233. }
  1234. if(version >= 840) {
  1235. enableFeature(Otc::GameProtocolChecksum);
  1236. enableFeature(Otc::GameAccountNames);
  1237. enableFeature(Otc::GameDoubleFreeCapacity);
  1238. }
  1239. if(version >= 841) {
  1240. enableFeature(Otc::GameChallengeOnLogin);
  1241. enableFeature(Otc::GameMessageSizeCheck);
  1242. }
  1243. if(version >= 854) {
  1244. enableFeature(Otc::GameCreatureEmblems);
  1245. }
  1246. if(version >= 860) {
  1247. enableFeature(Otc::GameAttackSeq);
  1248. }
  1249. if(version >= 862) {
  1250. enableFeature(Otc::GamePenalityOnDeath);
  1251. }
  1252. if(version >= 870) {
  1253. enableFeature(Otc::GameDoubleExperience);
  1254. enableFeature(Otc::GamePlayerMounts);
  1255. enableFeature(Otc::GameSpellList);
  1256. }
  1257. if(version >= 910) {
  1258. enableFeature(Otc::GameNameOnNpcTrade);
  1259. enableFeature(Otc::GameTotalCapacity);
  1260. enableFeature(Otc::GameSkillsBase);
  1261. enableFeature(Otc::GamePlayerRegenerationTime);
  1262. enableFeature(Otc::GameChannelPlayerList);
  1263. enableFeature(Otc::GameEnvironmentEffect);
  1264. enableFeature(Otc::GameItemAnimationPhase);
  1265. }
  1266. if(version >= 940) {
  1267. enableFeature(Otc::GamePlayerMarket);
  1268. }
  1269. if(version >= 953) {
  1270. enableFeature(Otc::GamePurseSlot);
  1271. enableFeature(Otc::GameClientPing);
  1272. }
  1273. if(version >= 960) {
  1274. enableFeature(Otc::GameSpritesU32);
  1275. enableFeature(Otc::GameOfflineTrainingTime);
  1276. }
  1277. if(version >= 963) {
  1278. enableFeature(Otc::GameAdditionalVipInfo);
  1279. }
  1280. if(version >= 980) {
  1281. enableFeature(Otc::GamePreviewState);
  1282. enableFeature(Otc::GameClientVersion);
  1283. }
  1284. if(version >= 981) {
  1285. enableFeature(Otc::GameLoginPending);
  1286. enableFeature(Otc::GameNewSpeedLaw);
  1287. }
  1288. if(version >= 984) {
  1289. enableFeature(Otc::GameContainerPagination);
  1290. enableFeature(Otc::GameBrowseField);
  1291. }
  1292. if(version >= 1000) {
  1293. enableFeature(Otc::GameThingMarks);
  1294. enableFeature(Otc::GamePVPMode);
  1295. }
  1296. if(version >= 1035) {
  1297. enableFeature(Otc::GameDoubleSkills);
  1298. enableFeature(Otc::GameBaseSkillU16);
  1299. }
  1300. if(version >= 1036) {
  1301. enableFeature(Otc::GameCreatureIcons);
  1302. enableFeature(Otc::GameHideNpcNames);
  1303. }
  1304. if(version >= 1038) {
  1305. enableFeature(Otc::GamePremiumExpiration);
  1306. }
  1307. if(version >= 1050) {
  1308. enableFeature(Otc::GameEnhancedAnimations);
  1309. }
  1310. if(version >= 1053) {
  1311. enableFeature(Otc::GameUnjustifiedPoints);
  1312. }
  1313. if(version >= 1054) {
  1314. enableFeature(Otc::GameExperienceBonus);
  1315. }
  1316. if(version >= 1055) {
  1317. enableFeature(Otc::GameDeathType);
  1318. }
  1319. if(version >= 1061) {
  1320. enableFeature(Otc::GameOGLInformation);
  1321. }
  1322. if(version >= 1071) {
  1323. enableFeature(Otc::GameContentRevision);
  1324. }
  1325. if(version >= 1072) {
  1326. enableFeature(Otc::GameAuthenticator);
  1327. }
  1328. if(version >= 1074) {
  1329. enableFeature(Otc::GameSessionKey);
  1330. }
  1331. m_clientVersion = version;
  1332. g_lua.callGlobalField("g_game", "onClientVersionChange", version);
  1333. }
  1334. void Game::setAttackingCreature(const CreaturePtr& creature)
  1335. {
  1336. if(creature != m_attackingCreature) {
  1337. CreaturePtr oldCreature = m_attackingCreature;
  1338. m_attackingCreature = creature;
  1339. g_lua.callGlobalField("g_game", "onAttackingCreatureChange", creature, oldCreature);
  1340. }
  1341. }
  1342. void Game::setFollowingCreature(const CreaturePtr& creature)
  1343. {
  1344. CreaturePtr oldCreature = m_followingCreature;
  1345. m_followingCreature = creature;
  1346. g_lua.callGlobalField("g_game", "onFollowingCreatureChange", creature, oldCreature);
  1347. }
  1348. std::string Game::formatCreatureName(const std::string& name)
  1349. {
  1350. std::string formatedName = name;
  1351. if(getFeature(Otc::GameFormatCreatureName) && name.length() > 0) {
  1352. bool upnext = true;
  1353. for(uint i=0;i<formatedName.length();++i) {
  1354. char ch = formatedName[i];
  1355. if(upnext) {
  1356. formatedName[i] = stdext::upchar(ch);
  1357. upnext = false;
  1358. }
  1359. if(ch == ' ')
  1360. upnext = true;
  1361. }
  1362. }
  1363. return formatedName;
  1364. }
  1365. int Game::findEmptyContainerId()
  1366. {
  1367. int id = 0;
  1368. while(m_containers[id] != nullptr)
  1369. id++;
  1370. return id;
  1371. }
  1372. int Game::getOs()
  1373. {
  1374. if(m_clientCustomOs >= 0)
  1375. return m_clientCustomOs;
  1376. if(g_app.getOs() == "windows")
  1377. return 10;
  1378. else if(g_app.getOs() == "mac")
  1379. return 12;
  1380. else // linux
  1381. return 11;
  1382. }