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

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