Auto walker update must be done from C++ not lua due to bot protection.

* Moved the auto walk checker to the source.
* Implemented a temporary fix for findPath method (will now choose other tiles over null tiles).
This commit is contained in:
BeniS
2013-01-09 04:32:37 +13:00
parent 493202afcf
commit a1a8d28f5c
11 changed files with 84 additions and 105 deletions

View File

@@ -47,7 +47,8 @@ namespace Otc
ANIMATED_TEXT_DURATION = 1000,
STATIC_DURATION_PER_CHARACTER = 60,
MIN_STATIC_TEXT_DURATION = 3000,
MAX_STATIC_TEXT_WIDTH = 200
MAX_STATIC_TEXT_WIDTH = 200,
MAX_AUTOWALK_STEPS_RETRY = 10
};
enum DrawFlags {

View File

@@ -158,7 +158,7 @@ void Game::processGameStart()
m_protocolGame->sendPing();
disableBotCall();
}
}, 3000);
}, 2000);
}
}
@@ -564,6 +564,8 @@ bool Game::walk(Otc::Direction direction)
return false;
}
m_localPlayer->stopAutoWalkUpdate();
g_lua.callGlobalField("g_game", "onWalk", direction);
forceWalk(direction);
@@ -824,6 +826,7 @@ void Game::attack(CreaturePtr creature)
cancelFollow();
setAttackingCreature(creature);
m_localPlayer->stopAutoWalkUpdate();
if(m_protocolVersion >= 963) {
if(creature)
@@ -847,6 +850,7 @@ void Game::follow(CreaturePtr creature)
cancelAttack();
setFollowingCreature(creature);
m_localPlayer->stopAutoWalkUpdate();
if(m_protocolVersion >= 963) {
if(creature)
@@ -867,6 +871,8 @@ void Game::cancelAttackAndFollow()
if(isAttacking())
setAttackingCreature(nullptr);
m_localPlayer->stopAutoWalkUpdate();
m_protocolGame->sendCancelAttackAndFollow();
g_lua.callGlobalField("g_game", "onCancelAttackAndFollow");

View File

@@ -161,12 +161,54 @@ void LocalPlayer::cancelWalk(Otc::Direction direction)
if(direction != Otc::InvalidDirection)
setDirection(direction);
updateAutoWalkSteps(true);
callLuaField("onCancelWalk", direction);
}
void LocalPlayer::updateAutoWalkSteps(bool walkFailed)
{
if(!m_autoWalking) {
m_autoWalkSteps.clear();
return;
}
if(!m_lastAutoWalkDestination.isValid()) {
return;
}
// for now this cannot be done from lua, due to bot protection
m_autoWalkSteps.push_back(m_lastAutoWalkDestination);
if(m_autoWalkSteps.size() >= Otc::MAX_AUTOWALK_STEPS_RETRY || walkFailed) {
autoWalk(m_lastAutoWalkDestination);
}
}
bool LocalPlayer::autoWalk(const Position& destination)
{
m_autoWalkSteps.clear();
m_lastAutoWalkDestination = destination;
std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> result = g_map.findPath(m_position, destination, 127, Otc::PathFindAllowNullTiles);
std::vector<Otc::Direction> dirs = std::get<0>(result);
if(dirs.size() == 0)
return false;
g_game.autoWalk(dirs);
return true;
}
void LocalPlayer::stopAutoWalkUpdate()
{
m_lastAutoWalkDestination = Position();
m_autoWalkSteps.clear();
}
void LocalPlayer::stopWalk()
{
Creature::stopWalk(); // will call terminateWalk
m_lastPrewalkDone = true;
m_lastPrewalkDestionation = Position();
}
@@ -231,6 +273,13 @@ void LocalPlayer::onAppear()
lockWalk();
}
void LocalPlayer::onPositionChange(const Position& newPos, const Position& oldPos)
{
Creature::onPositionChange(newPos, oldPos);
updateAutoWalkSteps();
}
void LocalPlayer::setStates(int states)
{
if(m_states != states) {

View File

@@ -37,6 +37,9 @@ public:
void unlockWalk() { m_walkLockExpiration = 0; }
void lockWalk(int millis = 250);
void stopAutoWalkUpdate();
void updateAutoWalkSteps(bool walkFailed = false);
bool autoWalk(const Position& destination);
bool canWalk(Otc::Direction direction);
void setStates(int states);
@@ -87,6 +90,7 @@ public:
double getOfflineTrainingTime() { return m_offlineTrainingTime; }
std::vector<int> getSpells() { return m_spells; }
ItemPtr getInventoryItem(Otc::InventorySlot inventory) { return m_inventoryItems[inventory]; }
std::vector<Position> getAutoWalkSteps() { return m_autoWalkSteps; }
bool hasSight(const Position& pos);
bool isKnown() { return m_known; }
@@ -99,6 +103,7 @@ public:
bool isLocalPlayer() { return true; }
virtual void onAppear();
virtual void onPositionChange(const Position& newPos, const Position& oldPos);
protected:
void walk(const Position& oldPos, const Position& newPos);
@@ -117,6 +122,7 @@ private:
// walk related
Timer m_walkPingTimer;
Position m_lastPrewalkDestionation;
Position m_lastAutoWalkDestination;
ScheduledEventPtr m_autoWalkEndEvent;
ticks_t m_walkLockExpiration;
int m_lastWalkPing;
@@ -136,6 +142,7 @@ private:
std::array<int, Otc::LastSkill> m_skillsBaseLevel;
std::array<int, Otc::LastSkill> m_skillsLevelPercent;
std::vector<int> m_spells;
std::vector<Position> m_autoWalkSteps;
int m_states;
int m_vocation;

View File

@@ -459,6 +459,10 @@ void OTClient::registerLuaFunctions()
g_lua.bindClassMemberFunction<LocalPlayer>("isPreWalking", &LocalPlayer::isPreWalking);
g_lua.bindClassMemberFunction<LocalPlayer>("hasSight", &LocalPlayer::hasSight);
g_lua.bindClassMemberFunction<LocalPlayer>("isAutoWalking", &LocalPlayer::isAutoWalking);
g_lua.bindClassMemberFunction<LocalPlayer>("stopAutoWalkUpdate", &LocalPlayer::stopAutoWalkUpdate);
g_lua.bindClassMemberFunction<LocalPlayer>("updateAutoWalkSteps", &LocalPlayer::updateAutoWalkSteps);
g_lua.bindClassMemberFunction<LocalPlayer>("autoWalk", &LocalPlayer::autoWalk);
g_lua.bindClassMemberFunction<LocalPlayer>("getAutoWalkSteps", &LocalPlayer::getAutoWalkSteps);
g_lua.registerClass<Tile>();
g_lua.bindClassMemberFunction<Tile>("clean", &Tile::clean);

View File

@@ -506,6 +506,7 @@ std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> Map::findPath(const
Position neighborPos = currentNode->pos.translated(i, j);
const TilePtr& tile = getTile(neighborPos);
float walkFactor = 0;
if(neighborPos != goalPos) {
/*
Known Issue with Otc::PathFindAllowNullTiles flag:
@@ -514,7 +515,7 @@ std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> Map::findPath(const
but it is breaking normal path finding.
*/
if(!(flags & Otc::PathFindAllowNullTiles) && !tile)
continue;
walkFactor = 1.0f;
if(tile) {
if(!(flags & Otc::PathFindAllowCreatures) && tile->hasCreature())
continue;
@@ -525,12 +526,11 @@ std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> Map::findPath(const
}
}
float walkFactor;
Otc::Direction walkDir = currentNode->pos.getDirectionFromPosition(neighborPos);
if(walkDir >= Otc::NorthEast)
walkFactor = 3.0f;
walkFactor += 3.0f;
else
walkFactor = 1.0f;
walkFactor += 1.0f;
int groundSpeed = tile ? tile->getGroundSpeed() : 100;
float cost = currentNode->cost + (groundSpeed * walkFactor) / 100.0f;