localplayer.h

main 206 lines · 7.4 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
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
23#ifndef LOCALPLAYER_H
24#define LOCALPLAYER_H
25
26#include "player.h"
27#include "walkmatrix.h"
28
29// @bindclass
30class LocalPlayer : public Player
31{
32 enum {
33 PREWALK_TIMEOUT = 1000
34 };
35
36public:
37 LocalPlayer();
38
39 void draw(const Point& dest, bool animate = true, LightView* lightView = nullptr) override;
40
41 void unlockWalk() { m_walkLockExpiration = 0; }
42 void lockWalk(int millis = 200);
43 void stopAutoWalk();
44 bool autoWalk(Position destination, bool retry = false);
45 bool canWalk(Otc::Direction direction, bool ignoreLock = false);
46 bool isWalkLocked() {
47 return (m_walkLockExpiration != 0 && g_clock.millis() < m_walkLockExpiration);
48 }
49 void turn(Otc::Direction) override;
50
51 void setStates(int states);
52 void setSkill(uint8_t skill, int level, int levelPercent);
53 void setBaseSkill(uint8_t skill, int baseLevel);
54 void setHealth(double health, double maxHealth);
55 void setFreeCapacity(double freeCapacity);
56 void setTotalCapacity(double totalCapacity);
57 void setExperience(double experience);
58 void setLevel(double level, double levelPercent);
59 void setMana(double mana, double maxMana);
60 void setMagicLevel(double magicLevel, double magicLevelPercent);
61 void setBaseMagicLevel(double baseMagicLevel);
62 void setSoul(double soul);
63 void setStamina(double stamina);
64 void setKnown(bool known) { m_known = known; }
65 void setPendingGame(bool pending) { m_pending = pending; }
66 void setInventoryItem(Otc::InventorySlot inventory, const ItemPtr& item);
67 void setVocation(int vocation);
68 void setPremium(bool premium);
69 void setRegenerationTime(double regenerationTime);
70 void setOfflineTrainingTime(double offlineTrainingTime);
71 void setSpells(const std::vector<int>& spells);
72 void setBlessings(int blessings);
73
74 int getStates() { return m_states; }
75 int getSkillLevel(uint8_t skill) { return skill < m_skillsLevel.size() ? m_skillsLevel[skill] : 0; }
76 int getSkillBaseLevel(uint8_t skill) { return skill < m_skillsBaseLevel.size() ? m_skillsBaseLevel[skill] : 0; }
77 int getSkillLevelPercent(uint8_t skill) { return skill < m_skillsLevelPercent.size() ? m_skillsLevelPercent[skill] : 0; }
78 int getVocation() { return m_vocation; }
79 double getHealth() { return m_health; }
80 double getMaxHealth() { return m_maxHealth; }
81 double getFreeCapacity() { return m_freeCapacity; }
82 double getTotalCapacity() { return m_totalCapacity; }
83 double getExperience() { return m_experience; }
84 double getLevel() { return m_level; }
85 double getLevelPercent() { return m_levelPercent; }
86 double getMana() { return m_mana; }
87 double getMaxMana() { return std::max<double>(m_mana, m_maxMana); }
88 double getMagicLevel() { return m_magicLevel; }
89 double getMagicLevelPercent() { return m_magicLevelPercent; }
90 double getBaseMagicLevel() { return m_baseMagicLevel; }
91 double getSoul() { return m_soul; }
92 double getStamina() { return m_stamina; }
93 double getRegenerationTime() { return m_regenerationTime; }
94 double getOfflineTrainingTime() { return m_offlineTrainingTime; }
95 std::vector<int> getSpells() { return m_spells; }
96 ItemPtr getInventoryItem(Otc::InventorySlot inventory) { return m_inventoryItems[inventory]; }
97 int getBlessings() { return m_blessings; }
98
99 bool hasSight(const Position& pos);
100 bool isKnown() { return m_known; }
101 bool isAutoWalking() { return m_autoWalkDestination.isValid(); }
102 bool isServerWalking() override { return m_serverWalking; }
103 bool isPremium() { return m_premium; }
104 bool isPendingGame() { return m_pending; }
105
106 LocalPlayerPtr asLocalPlayer() { return static_self_cast<LocalPlayer>(); }
107 bool isLocalPlayer() override { return true; }
108
109 void onAppear() override;
110 void onPositionChange(const Position& newPos, const Position& oldPos) override;
111
112 // pre walking
113 void preWalk(Otc::Direction direction);
114 bool isPreWalking() override { return !m_preWalking.empty(); }
115 Position getPrewalkingPosition(bool beforePrewalk = false) override {
116 if(m_preWalking.empty())
117 return m_position;
118 else if (!beforePrewalk && m_preWalking.size() == 1)
119 return m_position;
120 auto ret = m_preWalking.rbegin();
121 if(!beforePrewalk)
122 ret++;
123 return *ret;
124 }
125
126 uint32_t getWalkPrediction(const Position& pos)
127 {
128 return m_walkMatrix.get(pos);
129 };
130
131 std::string dumpWalkMatrix()
132 {
133 return m_walkMatrix.dump();
134 }
135
136 void startServerWalking() { m_serverWalking = true; }
137 void finishServerWalking() { m_serverWalking = false; }
138
139protected:
140 void walk(const Position& oldPos, const Position& newPos) override;
141 void cancelWalk(Otc::Direction direction = Otc::InvalidDirection);
142
143 void cancelNewWalk(Otc::Direction dir);
144 bool predictiveCancelWalk(const Position& pos, uint32_t predictionId, Otc::Direction dir);
145
146 bool retryAutoWalk();
147 void stopWalk() override;
148
149 friend class Game;
150
151protected:
152 void updateWalkOffset(uint8 totalPixelsWalked, bool inNextFrame = false) override;
153 void updateWalk() override;
154 void terminateWalk() override;
155
156private:
157 // walk related
158 Position m_autoWalkDestination;
159 Position m_lastAutoWalkPosition;
160 int m_lastAutoWalkRetries = 0;
161 ScheduledEventPtr m_serverWalkEndEvent;
162 ScheduledEventPtr m_autoWalkContinueEvent;
163 ticks_t m_walkLockExpiration;
164
165 // walking and pre walking
166 std::list<Position> m_preWalking;
167 bool m_serverWalking = false;
168 bool m_lastPrewalkDone = false;
169 WalkMatrix m_walkMatrix;
170
171 bool m_premium = false;
172 bool m_known = false;
173 bool m_pending = false;
174
175 ItemPtr m_inventoryItems[Otc::LastInventorySlot];
176 Timer m_idleTimer;
177
178 std::vector<int> m_skillsLevel;
179 std::vector<int> m_skillsBaseLevel;
180 std::vector<int> m_skillsLevelPercent;
181 std::vector<int> m_spells;
182
183 int m_states;
184 int m_vocation;
185 int m_blessings;
186
187 double m_health;
188 double m_maxHealth;
189 double m_freeCapacity;
190 double m_totalCapacity;
191 double m_experience;
192 double m_level;
193 double m_levelPercent;
194 double m_mana;
195 double m_maxMana;
196 double m_magicLevel;
197 double m_magicLevelPercent;
198 double m_baseMagicLevel;
199 double m_soul;
200 double m_stamina;
201 double m_regenerationTime;
202 double m_offlineTrainingTime;
203};
204
205#endif
206
Top