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.h 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. #ifndef GAME_H
  23. #define GAME_H
  24. #include "declarations.h"
  25. #include "item.h"
  26. #include "animatedtext.h"
  27. #include "effect.h"
  28. #include "creature.h"
  29. #include "container.h"
  30. #include "protocolgame.h"
  31. #include "localplayer.h"
  32. #include "outfit.h"
  33. #include <framework/core/timer.h>
  34. #include <bitset>
  35. struct UnjustifiedPoints {
  36. bool operator==(const UnjustifiedPoints& other) {
  37. return killsDay == other.killsDay &&
  38. killsDayRemaining == other.killsDayRemaining &&
  39. killsWeek == other.killsWeek &&
  40. killsWeekRemaining == other.killsWeekRemaining &&
  41. killsMonth == other.killsMonth &&
  42. killsMonthRemaining == other.killsMonthRemaining &&
  43. skullTime == other.skullTime;
  44. }
  45. uint8 killsDay;
  46. uint8 killsDayRemaining;
  47. uint8 killsWeek;
  48. uint8 killsWeekRemaining;
  49. uint8 killsMonth;
  50. uint8 killsMonthRemaining;
  51. uint8 skullTime;
  52. };
  53. typedef std::tuple<std::string, uint, std::string, int, bool> Vip;
  54. //@bindsingleton g_game
  55. class Game
  56. {
  57. public:
  58. Game();
  59. void init();
  60. void terminate();
  61. private:
  62. void resetGameStates();
  63. protected:
  64. void processConnectionError(const boost::system::error_code& error);
  65. void processDisconnect();
  66. void processPing();
  67. void processPingBack();
  68. void processUpdateNeeded(const std::string& signature);
  69. void processLoginError(const std::string& error);
  70. void processLoginAdvice(const std::string& message);
  71. void processLoginWait(const std::string& message, int time);
  72. void processLoginToken(bool unknown);
  73. void processLogin();
  74. void processPendingGame();
  75. void processEnterGame();
  76. void processGameStart();
  77. void processGameEnd();
  78. void processDeath(int deathType, int penality);
  79. void processGMActions(const std::vector<uint8>& actions);
  80. void processInventoryChange(int slot, const ItemPtr& item);
  81. void processAttackCancel(uint seq);
  82. void processWalkCancel(Otc::Direction direction);
  83. void processPlayerHelpers(int helpers);
  84. void processPlayerModes(Otc::FightModes fightMode, Otc::ChaseModes chaseMode, bool safeMode, Otc::PVPModes pvpMode);
  85. // message related
  86. void processTextMessage(Otc::MessageMode mode, const std::string& text);
  87. void processTalk(const std::string& name, int level, Otc::MessageMode mode, const std::string& text, int channelId, const Position& pos);
  88. // container related
  89. void 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);
  90. void processCloseContainer(int containerId);
  91. void processContainerAddItem(int containerId, const ItemPtr& item, int slot);
  92. void processContainerUpdateItem(int containerId, int slot, const ItemPtr& item);
  93. void processContainerRemoveItem(int containerId, int slot, const ItemPtr& lastItem);
  94. // channel related
  95. void processChannelList(const std::vector<std::tuple<int, std::string> >& channelList);
  96. void processOpenChannel(int channelId, const std::string& name);
  97. void processOpenPrivateChannel(const std::string& name);
  98. void processOpenOwnPrivateChannel(int channelId, const std::string& name);
  99. void processCloseChannel(int channelId);
  100. // rule violations
  101. void processRuleViolationChannel(int channelId);
  102. void processRuleViolationRemove(const std::string& name);
  103. void processRuleViolationCancel(const std::string& name);
  104. void processRuleViolationLock();
  105. // vip related
  106. void processVipAdd(uint id, const std::string& name, uint status, const std::string& description, int iconId, bool notifyLogin);
  107. void processVipStateChange(uint id, uint status);
  108. // tutorial hint
  109. void processTutorialHint(int id);
  110. void processAddAutomapFlag(const Position& pos, int icon, const std::string& message);
  111. void processRemoveAutomapFlag(const Position& pos, int icon, const std::string& message);
  112. // outfit
  113. void processOpenOutfitWindow(const Outfit& currentOutfit, const std::vector<std::tuple<int, std::string, int> >& outfitList,
  114. const std::vector<std::tuple<int, std::string> >& mountList);
  115. // npc trade
  116. void processOpenNpcTrade(const std::vector<std::tuple<ItemPtr, std::string, int, int, int> >& items);
  117. void processPlayerGoods(int money, const std::vector<std::tuple<ItemPtr, int> >& goods);
  118. void processCloseNpcTrade();
  119. // player trade
  120. void processOwnTrade(const std::string& name, const std::vector<ItemPtr>& items);
  121. void processCounterTrade(const std::string& name, const std::vector<ItemPtr>& items);
  122. void processCloseTrade();
  123. // edit text/list
  124. void processEditText(uint id, int itemId, int maxLength, const std::string& text, const std::string& writer, const std::string& date);
  125. void processEditList(uint id, int doorId, const std::string& text);
  126. // questlog
  127. void processQuestLog(const std::vector<std::tuple<int, std::string, bool> >& questList);
  128. void processQuestLine(int questId, const std::vector<std::tuple<std::string, std::string> >& questMissions);
  129. // modal dialogs >= 970
  130. void 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);
  131. friend class ProtocolGame;
  132. friend class Map;
  133. public:
  134. // login related
  135. void 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);
  136. void cancelLogin();
  137. void forceLogout();
  138. void safeLogout();
  139. // walk related
  140. bool walk(Otc::Direction direction, bool dash = false);
  141. bool dashWalk(Otc::Direction direction);
  142. void autoWalk(std::vector<Otc::Direction> dirs);
  143. void forceWalk(Otc::Direction direction);
  144. void turn(Otc::Direction direction);
  145. void stop();
  146. // item related
  147. void look(const ThingPtr& thing, bool isBattleList = false);
  148. void move(const ThingPtr &thing, const Position& toPos, int count);
  149. void moveToParentContainer(const ThingPtr& thing, int count);
  150. void rotate(const ThingPtr& thing);
  151. void use(const ThingPtr& thing);
  152. void useWith(const ItemPtr& fromThing, const ThingPtr& toThing);
  153. void useInventoryItem(int itemId);
  154. void useInventoryItemWith(int itemId, const ThingPtr& toThing);
  155. ItemPtr findItemInContainers(uint itemId, int subType);
  156. // container related
  157. int open(const ItemPtr& item, const ContainerPtr& previousContainer);
  158. void openParent(const ContainerPtr& container);
  159. void close(const ContainerPtr& container);
  160. void refreshContainer(const ContainerPtr& container);
  161. // attack/follow related
  162. void attack(CreaturePtr creature);
  163. void cancelAttack() { attack(nullptr); }
  164. void follow(CreaturePtr creature);
  165. void cancelFollow() { follow(nullptr); }
  166. void cancelAttackAndFollow();
  167. // talk related
  168. void talk(const std::string& message);
  169. void talkChannel(Otc::MessageMode mode, int channelId, const std::string& message);
  170. void talkPrivate(Otc::MessageMode mode, const std::string& receiver, const std::string& message);
  171. // channel related
  172. void openPrivateChannel(const std::string& receiver);
  173. void requestChannels();
  174. void joinChannel(int channelId);
  175. void leaveChannel(int channelId);
  176. void closeNpcChannel();
  177. void openOwnChannel();
  178. void inviteToOwnChannel(const std::string& name);
  179. void excludeFromOwnChannel(const std::string& name);
  180. // party related
  181. void partyInvite(int creatureId);
  182. void partyJoin(int creatureId);
  183. void partyRevokeInvitation(int creatureId);
  184. void partyPassLeadership(int creatureId);
  185. void partyLeave();
  186. void partyShareExperience(bool active);
  187. // outfit related
  188. void requestOutfit();
  189. void changeOutfit(const Outfit& outfit);
  190. // vip related
  191. void addVip(const std::string& name);
  192. void removeVip(int playerId);
  193. void editVip(int playerId, const std::string& description, int iconId, bool notifyLogin);
  194. // fight modes related
  195. void setChaseMode(Otc::ChaseModes chaseMode);
  196. void setFightMode(Otc::FightModes fightMode);
  197. void setSafeFight(bool on);
  198. void setPVPMode(Otc::PVPModes pvpMode);
  199. Otc::ChaseModes getChaseMode() { return m_chaseMode; }
  200. Otc::FightModes getFightMode() { return m_fightMode; }
  201. bool isSafeFight() { return m_safeFight; }
  202. Otc::PVPModes getPVPMode() { return m_pvpMode; }
  203. // pvp related
  204. void setUnjustifiedPoints(UnjustifiedPoints unjustifiedPoints);
  205. UnjustifiedPoints getUnjustifiedPoints() { return m_unjustifiedPoints; };
  206. void setOpenPvpSituations(int openPvpSitations);
  207. int getOpenPvpSituations() { return m_openPvpSituations; }
  208. // npc trade related
  209. void inspectNpcTrade(const ItemPtr& item);
  210. void buyItem(const ItemPtr& item, int amount, bool ignoreCapacity, bool buyWithBackpack);
  211. void sellItem(const ItemPtr& item, int amount, bool ignoreEquipped);
  212. void closeNpcTrade();
  213. // player trade related
  214. void requestTrade(const ItemPtr& item, const CreaturePtr& creature);
  215. void inspectTrade(bool counterOffer, int index);
  216. void acceptTrade();
  217. void rejectTrade();
  218. // house window and editable items related
  219. void editText(uint id, const std::string& text);
  220. void editList(uint id, int doorId, const std::string& text);
  221. // rule violations (only gms)
  222. void openRuleViolation(const std::string& reporter);
  223. void closeRuleViolation(const std::string& reporter);
  224. void cancelRuleViolation();
  225. // reports
  226. void reportBug(const std::string& comment);
  227. void reportRuleViolation(const std::string& target, int reason, int action, const std::string& comment, const std::string& statement, int statementId, bool ipBanishment);
  228. void debugReport(const std::string& a, const std::string& b, const std::string& c, const std::string& d);
  229. // questlog related
  230. void requestQuestLog();
  231. void requestQuestLine(int questId);
  232. // 870 only
  233. void equipItem(const ItemPtr& item);
  234. void mount(bool mount);
  235. // 910 only
  236. void requestItemInfo(const ItemPtr& item, int index);
  237. // >= 970 modal dialog
  238. void answerModalDialog(uint32 dialog, int button, int choice);
  239. // >= 984 browse field
  240. void browseField(const Position& position);
  241. void seekInContainer(int cid, int index);
  242. // >= 1080 ingame store
  243. void buyStoreOffer(int offerId, int productType, const std::string& name = "");
  244. void requestTransactionHistory(int page, int entriesPerPage);
  245. void requestStoreOffers(const std::string& categoryName, int serviceType = 0);
  246. void openStore(int serviceType = 0, const std::string& category = "");
  247. void transferCoins(const std::string& recipient, int amount);
  248. void openTransactionHistory(int entriesPerPage);
  249. //void reportRuleViolation2();
  250. void ping();
  251. void setPingDelay(int delay) { m_pingDelay = delay; }
  252. // otclient only
  253. void changeMapAwareRange(int xrange, int yrange);
  254. // dynamic support for game features
  255. void enableFeature(Otc::GameFeature feature) { m_features.set(feature, true); }
  256. void disableFeature(Otc::GameFeature feature) { m_features.set(feature, false); }
  257. void setFeature(Otc::GameFeature feature, bool enabled) { m_features.set(feature, enabled); }
  258. bool getFeature(Otc::GameFeature feature) { return m_features.test(feature); }
  259. void setProtocolVersion(int version);
  260. int getProtocolVersion() { return m_protocolVersion; }
  261. void setClientVersion(int version);
  262. int getClientVersion() { return m_clientVersion; }
  263. void setCustomOs(int os) { m_clientCustomOs = os; }
  264. int getOs();
  265. bool canPerformGameAction();
  266. bool checkBotProtection();
  267. bool isOnline() { return m_online; }
  268. bool isLogging() { return !m_online && m_protocolGame; }
  269. bool isDead() { return m_dead; }
  270. bool isAttacking() { return !!m_attackingCreature && !m_attackingCreature->isRemoved(); }
  271. bool isFollowing() { return !!m_followingCreature && !m_followingCreature->isRemoved(); }
  272. bool isConnectionOk() { return m_protocolGame && m_protocolGame->getElapsedTicksSinceLastRead() < 5000; }
  273. int getPing() { return m_ping >= 0 ? std::max<int>(m_ping, m_pingTimer.elapsed_millis()) : -1; }
  274. ContainerPtr getContainer(int index) { return m_containers[index]; }
  275. std::map<int, ContainerPtr> getContainers() { return m_containers; }
  276. std::map<int, Vip> getVips() { return m_vips; }
  277. CreaturePtr getAttackingCreature() { return m_attackingCreature; }
  278. CreaturePtr getFollowingCreature() { return m_followingCreature; }
  279. void setServerBeat(int beat) { m_serverBeat = beat; }
  280. int getServerBeat() { return m_serverBeat; }
  281. void setCanReportBugs(bool enable) { m_canReportBugs = enable; }
  282. bool canReportBugs() { return m_canReportBugs; }
  283. void setExpertPvpMode(bool enable) { m_expertPvpMode = enable; }
  284. bool getExpertPvpMode() { return m_expertPvpMode; }
  285. LocalPlayerPtr getLocalPlayer() { return m_localPlayer; }
  286. ProtocolGamePtr getProtocolGame() { return m_protocolGame; }
  287. std::string getCharacterName() { return m_characterName; }
  288. std::string getWorldName() { return m_worldName; }
  289. std::vector<uint8> getGMActions() { return m_gmActions; }
  290. bool isGM() { return m_gmActions.size() > 0; }
  291. Otc::Direction getLastWalkDir() { return m_lastWalkDir; }
  292. std::string formatCreatureName(const std::string &name);
  293. int findEmptyContainerId();
  294. protected:
  295. void enableBotCall() { m_denyBotCall = false; }
  296. void disableBotCall() { m_denyBotCall = true; }
  297. private:
  298. void setAttackingCreature(const CreaturePtr& creature);
  299. void setFollowingCreature(const CreaturePtr& creature);
  300. LocalPlayerPtr m_localPlayer;
  301. CreaturePtr m_attackingCreature;
  302. CreaturePtr m_followingCreature;
  303. ProtocolGamePtr m_protocolGame;
  304. std::map<int, ContainerPtr> m_containers;
  305. std::map<int, Vip> m_vips;
  306. bool m_online;
  307. bool m_denyBotCall;
  308. bool m_dead;
  309. bool m_expertPvpMode;
  310. int m_serverBeat;
  311. ticks_t m_ping;
  312. uint m_pingSent;
  313. uint m_pingReceived;
  314. stdext::timer m_pingTimer;
  315. Timer m_dashTimer;
  316. uint m_seq;
  317. int m_pingDelay;
  318. Otc::FightModes m_fightMode;
  319. Otc::ChaseModes m_chaseMode;
  320. Otc::PVPModes m_pvpMode;
  321. Otc::Direction m_lastWalkDir;
  322. UnjustifiedPoints m_unjustifiedPoints;
  323. int m_openPvpSituations;
  324. bool m_safeFight;
  325. bool m_canReportBugs;
  326. std::vector<uint8> m_gmActions;
  327. std::string m_characterName;
  328. std::string m_worldName;
  329. std::bitset<Otc::LastGameFeature> m_features;
  330. ScheduledEventPtr m_pingEvent;
  331. ScheduledEventPtr m_walkEvent;
  332. ScheduledEventPtr m_checkConnectionEvent;
  333. bool m_connectionFailWarned;
  334. int m_protocolVersion;
  335. int m_clientVersion;
  336. std::string m_clientSignature;
  337. int m_clientCustomOs;
  338. };
  339. extern Game g_game;
  340. #endif