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 46KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744
  1. /*
  2. * Copyright (c) 2010-2017 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, bool isBattleList)
  647. {
  648. if(!canPerformGameAction() || !thing)
  649. return;
  650. if(thing->isCreature() && isBattleList && 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. if(toThing->isCreature())
  707. m_protocolGame->sendUseOnCreature(pos, item->getId(), item->getStackPos(), toThing->getId());
  708. else
  709. m_protocolGame->sendUseItemWith(pos, item->getId(), item->getStackPos(), toThing->getPosition(), toThing->getId(), toThing->getStackPos());
  710. }
  711. void Game::useInventoryItemWith(int itemId, const ThingPtr& toThing)
  712. {
  713. if(!canPerformGameAction() || !toThing)
  714. return;
  715. Position pos = Position(0xFFFF, 0, 0); // means that is a item in inventory
  716. if(toThing->isCreature())
  717. m_protocolGame->sendUseOnCreature(pos, itemId, 0, toThing->getId());
  718. else
  719. m_protocolGame->sendUseItemWith(pos, itemId, 0, toThing->getPosition(), toThing->getId(), toThing->getStackPos());
  720. }
  721. ItemPtr Game::findItemInContainers(uint itemId, int subType)
  722. {
  723. for(auto& it : m_containers) {
  724. const ContainerPtr& container = it.second;
  725. if(container) {
  726. ItemPtr item = container->findItemById(itemId, subType);
  727. if(item != nullptr)
  728. return item;
  729. }
  730. }
  731. return nullptr;
  732. }
  733. int Game::open(const ItemPtr& item, const ContainerPtr& previousContainer)
  734. {
  735. if(!canPerformGameAction() || !item)
  736. return -1;
  737. int id = 0;
  738. if(!previousContainer)
  739. id = findEmptyContainerId();
  740. else
  741. id = previousContainer->getId();
  742. m_protocolGame->sendUseItem(item->getPosition(), item->getId(), item->getStackPos(), id);
  743. return id;
  744. }
  745. void Game::openParent(const ContainerPtr& container)
  746. {
  747. if(!canPerformGameAction() || !container)
  748. return;
  749. m_protocolGame->sendUpContainer(container->getId());
  750. }
  751. void Game::close(const ContainerPtr& container)
  752. {
  753. if(!canPerformGameAction() || !container)
  754. return;
  755. m_protocolGame->sendCloseContainer(container->getId());
  756. }
  757. void Game::refreshContainer(const ContainerPtr& container)
  758. {
  759. if(!canPerformGameAction())
  760. return;
  761. m_protocolGame->sendRefreshContainer(container->getId());
  762. }
  763. void Game::attack(CreaturePtr creature)
  764. {
  765. if(!canPerformGameAction() || creature == m_localPlayer)
  766. return;
  767. // cancel when attacking again
  768. if(creature && creature == m_attackingCreature)
  769. creature = nullptr;
  770. if(creature && isFollowing())
  771. cancelFollow();
  772. setAttackingCreature(creature);
  773. m_localPlayer->stopAutoWalk();
  774. if(m_protocolVersion >= 963) {
  775. if(creature)
  776. m_seq = creature->getId();
  777. } else
  778. m_seq++;
  779. m_protocolGame->sendAttack(creature ? creature->getId() : 0, m_seq);
  780. }
  781. void Game::follow(CreaturePtr creature)
  782. {
  783. if(!canPerformGameAction() || creature == m_localPlayer)
  784. return;
  785. // cancel when following again
  786. if(creature && creature == m_followingCreature)
  787. creature = nullptr;
  788. if(creature && isAttacking())
  789. cancelAttack();
  790. setFollowingCreature(creature);
  791. m_localPlayer->stopAutoWalk();
  792. if(m_protocolVersion >= 963) {
  793. if(creature)
  794. m_seq = creature->getId();
  795. } else
  796. m_seq++;
  797. m_protocolGame->sendFollow(creature ? creature->getId() : 0, m_seq);
  798. }
  799. void Game::cancelAttackAndFollow()
  800. {
  801. if(!canPerformGameAction())
  802. return;
  803. if(isFollowing())
  804. setFollowingCreature(nullptr);
  805. if(isAttacking())
  806. setAttackingCreature(nullptr);
  807. m_localPlayer->stopAutoWalk();
  808. m_protocolGame->sendCancelAttackAndFollow();
  809. g_lua.callGlobalField("g_game", "onCancelAttackAndFollow");
  810. }
  811. void Game::talk(const std::string& message)
  812. {
  813. if(!canPerformGameAction() || message.empty())
  814. return;
  815. talkChannel(Otc::MessageSay, 0, message);
  816. }
  817. void Game::talkChannel(Otc::MessageMode mode, int channelId, const std::string& message)
  818. {
  819. if(!canPerformGameAction() || message.empty())
  820. return;
  821. m_protocolGame->sendTalk(mode, channelId, "", message);
  822. }
  823. void Game::talkPrivate(Otc::MessageMode mode, const std::string& receiver, const std::string& message)
  824. {
  825. if(!canPerformGameAction() || receiver.empty() || message.empty())
  826. return;
  827. m_protocolGame->sendTalk(mode, 0, receiver, message);
  828. }
  829. void Game::openPrivateChannel(const std::string& receiver)
  830. {
  831. if(!canPerformGameAction() || receiver.empty())
  832. return;
  833. m_protocolGame->sendOpenPrivateChannel(receiver);
  834. }
  835. void Game::requestChannels()
  836. {
  837. if(!canPerformGameAction())
  838. return;
  839. m_protocolGame->sendRequestChannels();
  840. }
  841. void Game::joinChannel(int channelId)
  842. {
  843. if(!canPerformGameAction())
  844. return;
  845. m_protocolGame->sendJoinChannel(channelId);
  846. }
  847. void Game::leaveChannel(int channelId)
  848. {
  849. if(!canPerformGameAction())
  850. return;
  851. m_protocolGame->sendLeaveChannel(channelId);
  852. }
  853. void Game::closeNpcChannel()
  854. {
  855. if(!canPerformGameAction())
  856. return;
  857. m_protocolGame->sendCloseNpcChannel();
  858. }
  859. void Game::openOwnChannel()
  860. {
  861. if(!canPerformGameAction())
  862. return;
  863. m_protocolGame->sendOpenOwnChannel();
  864. }
  865. void Game::inviteToOwnChannel(const std::string& name)
  866. {
  867. if(!canPerformGameAction() || name.empty())
  868. return;
  869. m_protocolGame->sendInviteToOwnChannel(name);
  870. }
  871. void Game::excludeFromOwnChannel(const std::string& name)
  872. {
  873. if(!canPerformGameAction() || name.empty())
  874. return;
  875. m_protocolGame->sendExcludeFromOwnChannel(name);
  876. }
  877. void Game::partyInvite(int creatureId)
  878. {
  879. if(!canPerformGameAction())
  880. return;
  881. m_protocolGame->sendInviteToParty(creatureId);
  882. }
  883. void Game::partyJoin(int creatureId)
  884. {
  885. if(!canPerformGameAction())
  886. return;
  887. m_protocolGame->sendJoinParty(creatureId);
  888. }
  889. void Game::partyRevokeInvitation(int creatureId)
  890. {
  891. if(!canPerformGameAction())
  892. return;
  893. m_protocolGame->sendRevokeInvitation(creatureId);
  894. }
  895. void Game::partyPassLeadership(int creatureId)
  896. {
  897. if(!canPerformGameAction())
  898. return;
  899. m_protocolGame->sendPassLeadership(creatureId);
  900. }
  901. void Game::partyLeave()
  902. {
  903. if(!canPerformGameAction())
  904. return;
  905. m_protocolGame->sendLeaveParty();
  906. }
  907. void Game::partyShareExperience(bool active)
  908. {
  909. if(!canPerformGameAction())
  910. return;
  911. m_protocolGame->sendShareExperience(active);
  912. }
  913. void Game::requestOutfit()
  914. {
  915. if(!canPerformGameAction())
  916. return;
  917. m_protocolGame->sendRequestOutfit();
  918. }
  919. void Game::changeOutfit(const Outfit& outfit)
  920. {
  921. if(!canPerformGameAction())
  922. return;
  923. m_protocolGame->sendChangeOutfit(outfit);
  924. }
  925. void Game::addVip(const std::string& name)
  926. {
  927. if(!canPerformGameAction() || name.empty())
  928. return;
  929. m_protocolGame->sendAddVip(name);
  930. }
  931. void Game::removeVip(int playerId)
  932. {
  933. if(!canPerformGameAction())
  934. return;
  935. auto it = m_vips.find(playerId);
  936. if(it == m_vips.end())
  937. return;
  938. m_vips.erase(it);
  939. m_protocolGame->sendRemoveVip(playerId);
  940. }
  941. void Game::editVip(int playerId, const std::string& description, int iconId, bool notifyLogin)
  942. {
  943. if(!canPerformGameAction())
  944. return;
  945. auto it = m_vips.find(playerId);
  946. if(it == m_vips.end())
  947. return;
  948. std::get<2>(m_vips[playerId]) = description;
  949. std::get<3>(m_vips[playerId]) = iconId;
  950. std::get<4>(m_vips[playerId]) = notifyLogin;
  951. if(getFeature(Otc::GameAdditionalVipInfo))
  952. m_protocolGame->sendEditVip(playerId, description, iconId, notifyLogin);
  953. }
  954. void Game::setChaseMode(Otc::ChaseModes chaseMode)
  955. {
  956. if(!canPerformGameAction())
  957. return;
  958. if(m_chaseMode == chaseMode)
  959. return;
  960. m_chaseMode = chaseMode;
  961. m_protocolGame->sendChangeFightModes(m_fightMode, m_chaseMode, m_safeFight, m_pvpMode);
  962. g_lua.callGlobalField("g_game", "onChaseModeChange", chaseMode);
  963. }
  964. void Game::setFightMode(Otc::FightModes fightMode)
  965. {
  966. if(!canPerformGameAction())
  967. return;
  968. if(m_fightMode == fightMode)
  969. return;
  970. m_fightMode = fightMode;
  971. m_protocolGame->sendChangeFightModes(m_fightMode, m_chaseMode, m_safeFight, m_pvpMode);
  972. g_lua.callGlobalField("g_game", "onFightModeChange", fightMode);
  973. }
  974. void Game::setSafeFight(bool on)
  975. {
  976. if(!canPerformGameAction())
  977. return;
  978. if(m_safeFight == on)
  979. return;
  980. m_safeFight = on;
  981. m_protocolGame->sendChangeFightModes(m_fightMode, m_chaseMode, m_safeFight, m_pvpMode);
  982. g_lua.callGlobalField("g_game", "onSafeFightChange", on);
  983. }
  984. void Game::setPVPMode(Otc::PVPModes pvpMode)
  985. {
  986. if(!canPerformGameAction())
  987. return;
  988. if(!getFeature(Otc::GamePVPMode))
  989. return;
  990. if(m_pvpMode == pvpMode)
  991. return;
  992. m_pvpMode = pvpMode;
  993. m_protocolGame->sendChangeFightModes(m_fightMode, m_chaseMode, m_safeFight, m_pvpMode);
  994. g_lua.callGlobalField("g_game", "onPVPModeChange", pvpMode);
  995. }
  996. void Game::setUnjustifiedPoints(UnjustifiedPoints unjustifiedPoints)
  997. {
  998. if(!canPerformGameAction())
  999. return;
  1000. if(!getFeature(Otc::GameUnjustifiedPoints))
  1001. return;
  1002. if(m_unjustifiedPoints == unjustifiedPoints)
  1003. return;
  1004. m_unjustifiedPoints = unjustifiedPoints;
  1005. g_lua.callGlobalField("g_game", "onUnjustifiedPointsChange", unjustifiedPoints);
  1006. }
  1007. void Game::setOpenPvpSituations(int openPvpSituations)
  1008. {
  1009. if(!canPerformGameAction())
  1010. return;
  1011. if(m_openPvpSituations == openPvpSituations)
  1012. return;
  1013. m_openPvpSituations = openPvpSituations;
  1014. g_lua.callGlobalField("g_game", "onOpenPvpSituationsChange", openPvpSituations);
  1015. }
  1016. void Game::inspectNpcTrade(const ItemPtr& item)
  1017. {
  1018. if(!canPerformGameAction() || !item)
  1019. return;
  1020. m_protocolGame->sendInspectNpcTrade(item->getId(), item->getCount());
  1021. }
  1022. void Game::buyItem(const ItemPtr& item, int amount, bool ignoreCapacity, bool buyWithBackpack)
  1023. {
  1024. if(!canPerformGameAction() || !item)
  1025. return;
  1026. m_protocolGame->sendBuyItem(item->getId(), item->getCountOrSubType(), amount, ignoreCapacity, buyWithBackpack);
  1027. }
  1028. void Game::sellItem(const ItemPtr& item, int amount, bool ignoreEquipped)
  1029. {
  1030. if(!canPerformGameAction() || !item)
  1031. return;
  1032. m_protocolGame->sendSellItem(item->getId(), item->getSubType(), amount, ignoreEquipped);
  1033. }
  1034. void Game::closeNpcTrade()
  1035. {
  1036. if(!canPerformGameAction())
  1037. return;
  1038. m_protocolGame->sendCloseNpcTrade();
  1039. }
  1040. void Game::requestTrade(const ItemPtr& item, const CreaturePtr& creature)
  1041. {
  1042. if(!canPerformGameAction() || !item || !creature)
  1043. return;
  1044. m_protocolGame->sendRequestTrade(item->getPosition(), item->getId(), item->getStackPos(), creature->getId());
  1045. }
  1046. void Game::inspectTrade(bool counterOffer, int index)
  1047. {
  1048. if(!canPerformGameAction())
  1049. return;
  1050. m_protocolGame->sendInspectTrade(counterOffer, index);
  1051. }
  1052. void Game::acceptTrade()
  1053. {
  1054. if(!canPerformGameAction())
  1055. return;
  1056. m_protocolGame->sendAcceptTrade();
  1057. }
  1058. void Game::rejectTrade()
  1059. {
  1060. if(!canPerformGameAction())
  1061. return;
  1062. m_protocolGame->sendRejectTrade();
  1063. }
  1064. void Game::editText(uint id, const std::string& text)
  1065. {
  1066. if(!canPerformGameAction())
  1067. return;
  1068. m_protocolGame->sendEditText(id, text);
  1069. }
  1070. void Game::editList(uint id, int doorId, const std::string& text)
  1071. {
  1072. if(!canPerformGameAction())
  1073. return;
  1074. m_protocolGame->sendEditList(id, doorId, text);
  1075. }
  1076. void Game::openRuleViolation(const std::string& reporter)
  1077. {
  1078. if(!canPerformGameAction())
  1079. return;
  1080. m_protocolGame->sendOpenRuleViolation(reporter);
  1081. }
  1082. void Game::closeRuleViolation(const std::string& reporter)
  1083. {
  1084. if(!canPerformGameAction())
  1085. return;
  1086. m_protocolGame->sendCloseRuleViolation(reporter);
  1087. }
  1088. void Game::cancelRuleViolation()
  1089. {
  1090. if(!canPerformGameAction())
  1091. return;
  1092. m_protocolGame->sendCancelRuleViolation();
  1093. }
  1094. void Game::reportBug(const std::string& comment)
  1095. {
  1096. if(!canPerformGameAction())
  1097. return;
  1098. m_protocolGame->sendBugReport(comment);
  1099. }
  1100. void Game::reportRuleViolation(const std::string& target, int reason, int action, const std::string& comment, const std::string& statement, int statementId, bool ipBanishment)
  1101. {
  1102. if(!canPerformGameAction())
  1103. return;
  1104. m_protocolGame->sendRuleViolation(target, reason, action, comment, statement, statementId, ipBanishment);
  1105. }
  1106. void Game::debugReport(const std::string& a, const std::string& b, const std::string& c, const std::string& d)
  1107. {
  1108. m_protocolGame->sendDebugReport(a, b, c, d);
  1109. }
  1110. void Game::requestQuestLog()
  1111. {
  1112. if(!canPerformGameAction())
  1113. return;
  1114. m_protocolGame->sendRequestQuestLog();
  1115. }
  1116. void Game::requestQuestLine(int questId)
  1117. {
  1118. if(!canPerformGameAction())
  1119. return;
  1120. m_protocolGame->sendRequestQuestLine(questId);
  1121. }
  1122. void Game::equipItem(const ItemPtr& item)
  1123. {
  1124. if(!canPerformGameAction())
  1125. return;
  1126. m_protocolGame->sendEquipItem(item->getId(), item->getCountOrSubType());
  1127. }
  1128. void Game::mount(bool mount)
  1129. {
  1130. if(!canPerformGameAction())
  1131. return;
  1132. m_protocolGame->sendMountStatus(mount);
  1133. }
  1134. void Game::requestItemInfo(const ItemPtr& item, int index)
  1135. {
  1136. if(!canPerformGameAction())
  1137. return;
  1138. m_protocolGame->sendRequestItemInfo(item->getId(), item->getSubType(), index);
  1139. }
  1140. void Game::answerModalDialog(int dialog, int button, int choice)
  1141. {
  1142. if(!canPerformGameAction())
  1143. return;
  1144. m_protocolGame->sendAnswerModalDialog(dialog, button, choice);
  1145. }
  1146. void Game::browseField(const Position& position)
  1147. {
  1148. if(!canPerformGameAction())
  1149. return;
  1150. m_protocolGame->sendBrowseField(position);
  1151. }
  1152. void Game::seekInContainer(int cid, int index)
  1153. {
  1154. if(!canPerformGameAction())
  1155. return;
  1156. m_protocolGame->sendSeekInContainer(cid, index);
  1157. }
  1158. void Game::buyStoreOffer(int offerId, int productType, const std::string& name)
  1159. {
  1160. if(!canPerformGameAction())
  1161. return;
  1162. m_protocolGame->sendBuyStoreOffer(offerId, productType, name);
  1163. }
  1164. void Game::requestTransactionHistory(int page, int entriesPerPage)
  1165. {
  1166. if(!canPerformGameAction())
  1167. return;
  1168. m_protocolGame->sendRequestTransactionHistory(page, entriesPerPage);
  1169. }
  1170. void Game::requestStoreOffers(const std::string& categoryName, int serviceType)
  1171. {
  1172. if(!canPerformGameAction())
  1173. return;
  1174. m_protocolGame->sendRequestStoreOffers(categoryName, serviceType);
  1175. }
  1176. void Game::openStore(int serviceType, const std::string& category)
  1177. {
  1178. if(!canPerformGameAction())
  1179. return;
  1180. m_protocolGame->sendOpenStore(serviceType, category);
  1181. }
  1182. void Game::transferCoins(const std::string& recipient, int amount)
  1183. {
  1184. if(!canPerformGameAction())
  1185. return;
  1186. m_protocolGame->sendTransferCoins(recipient, amount);
  1187. }
  1188. void Game::openTransactionHistory(int entriesPerPage)
  1189. {
  1190. if(!canPerformGameAction())
  1191. return;
  1192. m_protocolGame->sendOpenTransactionHistory(entriesPerPage);
  1193. }
  1194. void Game::ping()
  1195. {
  1196. if(!m_protocolGame || !m_protocolGame->isConnected())
  1197. return;
  1198. if(m_pingReceived != m_pingSent)
  1199. return;
  1200. m_denyBotCall = false;
  1201. m_protocolGame->sendPing();
  1202. m_denyBotCall = true;
  1203. m_pingSent++;
  1204. m_pingTimer.restart();
  1205. }
  1206. void Game::changeMapAwareRange(int xrange, int yrange)
  1207. {
  1208. if(!canPerformGameAction())
  1209. return;
  1210. m_protocolGame->sendChangeMapAwareRange(xrange, yrange);
  1211. }
  1212. bool Game::checkBotProtection()
  1213. {
  1214. #ifdef BOT_PROTECTION
  1215. // accepts calls comming from a stacktrace containing only C++ functions,
  1216. // if the stacktrace contains a lua function, then only accept if the engine is processing an input event
  1217. if(m_denyBotCall && g_lua.isInCppCallback() && !g_app.isOnInputEvent()) {
  1218. g_logger.error(g_lua.traceback("caught a lua call to a bot protected game function, the call was cancelled"));
  1219. return false;
  1220. }
  1221. #endif
  1222. return true;
  1223. }
  1224. bool Game::canPerformGameAction()
  1225. {
  1226. // we can only perform game actions if we meet these conditions:
  1227. // - the game is online
  1228. // - the local player exists
  1229. // - the local player is not dead
  1230. // - we have a game protocol
  1231. // - the game protocol is connected
  1232. // - its not a bot action
  1233. return m_online && m_localPlayer && !m_localPlayer->isDead() && !m_dead && m_protocolGame && m_protocolGame->isConnected() && checkBotProtection();
  1234. }
  1235. void Game::setProtocolVersion(int version)
  1236. {
  1237. if(m_protocolVersion == version)
  1238. return;
  1239. if(isOnline())
  1240. stdext::throw_exception("Unable to change protocol version while online");
  1241. if(version != 0 && (version < 740 || version > 1099))
  1242. stdext::throw_exception(stdext::format("Protocol version %d not supported", version));
  1243. m_protocolVersion = version;
  1244. Proto::buildMessageModesMap(version);
  1245. g_lua.callGlobalField("g_game", "onProtocolVersionChange", version);
  1246. }
  1247. void Game::setClientVersion(int version)
  1248. {
  1249. if(m_clientVersion == version)
  1250. return;
  1251. if(isOnline())
  1252. stdext::throw_exception("Unable to change client version while online");
  1253. if(version != 0 && (version < 740 || version > 1099))
  1254. stdext::throw_exception(stdext::format("Client version %d not supported", version));
  1255. m_features.reset();
  1256. enableFeature(Otc::GameFormatCreatureName);
  1257. if(version >= 770) {
  1258. enableFeature(Otc::GameLooktypeU16);
  1259. enableFeature(Otc::GameMessageStatements);
  1260. enableFeature(Otc::GameLoginPacketEncryption);
  1261. }
  1262. if(version >= 780) {
  1263. enableFeature(Otc::GamePlayerAddons);
  1264. enableFeature(Otc::GamePlayerStamina);
  1265. enableFeature(Otc::GameNewFluids);
  1266. enableFeature(Otc::GameMessageLevel);
  1267. enableFeature(Otc::GamePlayerStateU16);
  1268. enableFeature(Otc::GameNewOutfitProtocol);
  1269. }
  1270. if(version >= 790) {
  1271. enableFeature(Otc::GameWritableDate);
  1272. }
  1273. if(version >= 840) {
  1274. enableFeature(Otc::GameProtocolChecksum);
  1275. enableFeature(Otc::GameAccountNames);
  1276. enableFeature(Otc::GameDoubleFreeCapacity);
  1277. }
  1278. if(version >= 841) {
  1279. enableFeature(Otc::GameChallengeOnLogin);
  1280. enableFeature(Otc::GameMessageSizeCheck);
  1281. }
  1282. if(version >= 854) {
  1283. enableFeature(Otc::GameCreatureEmblems);
  1284. }
  1285. if(version >= 860) {
  1286. enableFeature(Otc::GameAttackSeq);
  1287. }
  1288. if(version >= 862) {
  1289. enableFeature(Otc::GamePenalityOnDeath);
  1290. }
  1291. if(version >= 870) {
  1292. enableFeature(Otc::GameDoubleExperience);
  1293. enableFeature(Otc::GamePlayerMounts);
  1294. enableFeature(Otc::GameSpellList);
  1295. }
  1296. if(version >= 910) {
  1297. enableFeature(Otc::GameNameOnNpcTrade);
  1298. enableFeature(Otc::GameTotalCapacity);
  1299. enableFeature(Otc::GameSkillsBase);
  1300. enableFeature(Otc::GamePlayerRegenerationTime);
  1301. enableFeature(Otc::GameChannelPlayerList);
  1302. enableFeature(Otc::GameEnvironmentEffect);
  1303. enableFeature(Otc::GameItemAnimationPhase);
  1304. }
  1305. if(version >= 940) {
  1306. enableFeature(Otc::GamePlayerMarket);
  1307. }
  1308. if(version >= 953) {
  1309. enableFeature(Otc::GamePurseSlot);
  1310. enableFeature(Otc::GameClientPing);
  1311. }
  1312. if(version >= 960) {
  1313. enableFeature(Otc::GameSpritesU32);
  1314. enableFeature(Otc::GameOfflineTrainingTime);
  1315. }
  1316. if(version >= 963) {
  1317. enableFeature(Otc::GameAdditionalVipInfo);
  1318. }
  1319. if(version >= 980) {
  1320. enableFeature(Otc::GamePreviewState);
  1321. enableFeature(Otc::GameClientVersion);
  1322. }
  1323. if(version >= 981) {
  1324. enableFeature(Otc::GameLoginPending);
  1325. enableFeature(Otc::GameNewSpeedLaw);
  1326. }
  1327. if(version >= 984) {
  1328. enableFeature(Otc::GameContainerPagination);
  1329. enableFeature(Otc::GameBrowseField);
  1330. }
  1331. if(version >= 1000) {
  1332. enableFeature(Otc::GameThingMarks);
  1333. enableFeature(Otc::GamePVPMode);
  1334. }
  1335. if(version >= 1035) {
  1336. enableFeature(Otc::GameDoubleSkills);
  1337. enableFeature(Otc::GameBaseSkillU16);
  1338. }
  1339. if(version >= 1036) {
  1340. enableFeature(Otc::GameCreatureIcons);
  1341. enableFeature(Otc::GameHideNpcNames);
  1342. }
  1343. if(version >= 1038) {
  1344. enableFeature(Otc::GamePremiumExpiration);
  1345. }
  1346. if(version >= 1050) {
  1347. enableFeature(Otc::GameEnhancedAnimations);
  1348. }
  1349. if(version >= 1053) {
  1350. enableFeature(Otc::GameUnjustifiedPoints);
  1351. }
  1352. if(version >= 1054) {
  1353. enableFeature(Otc::GameExperienceBonus);
  1354. }
  1355. if(version >= 1055) {
  1356. enableFeature(Otc::GameDeathType);
  1357. }
  1358. if(version >= 1057) {
  1359. enableFeature(Otc::GameIdleAnimations);
  1360. }
  1361. if(version >= 1061) {
  1362. enableFeature(Otc::GameOGLInformation);
  1363. }
  1364. if(version >= 1071) {
  1365. enableFeature(Otc::GameContentRevision);
  1366. }
  1367. if(version >= 1072) {
  1368. enableFeature(Otc::GameAuthenticator);
  1369. }
  1370. if(version >= 1074) {
  1371. enableFeature(Otc::GameSessionKey);
  1372. }
  1373. if(version >= 1080) {
  1374. enableFeature(Otc::GameIngameStore);
  1375. }
  1376. if(version >= 1092) {
  1377. enableFeature(Otc::GameIngameStoreServiceType);
  1378. }
  1379. if(version >= 1093) {
  1380. enableFeature(Otc::GameIngameStoreHighlights);
  1381. }
  1382. if(version >= 1094) {
  1383. enableFeature(Otc::GameAdditionalSkills);
  1384. }
  1385. m_clientVersion = version;
  1386. g_lua.callGlobalField("g_game", "onClientVersionChange", version);
  1387. }
  1388. void Game::setAttackingCreature(const CreaturePtr& creature)
  1389. {
  1390. if(creature != m_attackingCreature) {
  1391. CreaturePtr oldCreature = m_attackingCreature;
  1392. m_attackingCreature = creature;
  1393. g_lua.callGlobalField("g_game", "onAttackingCreatureChange", creature, oldCreature);
  1394. }
  1395. }
  1396. void Game::setFollowingCreature(const CreaturePtr& creature)
  1397. {
  1398. CreaturePtr oldCreature = m_followingCreature;
  1399. m_followingCreature = creature;
  1400. g_lua.callGlobalField("g_game", "onFollowingCreatureChange", creature, oldCreature);
  1401. }
  1402. std::string Game::formatCreatureName(const std::string& name)
  1403. {
  1404. std::string formatedName = name;
  1405. if(getFeature(Otc::GameFormatCreatureName) && name.length() > 0) {
  1406. bool upnext = true;
  1407. for(uint i=0;i<formatedName.length();++i) {
  1408. char ch = formatedName[i];
  1409. if(upnext) {
  1410. formatedName[i] = stdext::upchar(ch);
  1411. upnext = false;
  1412. }
  1413. if(ch == ' ')
  1414. upnext = true;
  1415. }
  1416. }
  1417. return formatedName;
  1418. }
  1419. int Game::findEmptyContainerId()
  1420. {
  1421. int id = 0;
  1422. while(m_containers[id] != nullptr)
  1423. id++;
  1424. return id;
  1425. }
  1426. int Game::getOs()
  1427. {
  1428. if(m_clientCustomOs >= 0)
  1429. return m_clientCustomOs;
  1430. if(g_app.getOs() == "windows")
  1431. return 10;
  1432. else if(g_app.getOs() == "mac")
  1433. return 12;
  1434. else // linux
  1435. return 11;
  1436. }