Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix npc get only valid direction #688

Merged
merged 3 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions src/creatures/npcs/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,10 @@ void Npc::onThinkWalk(uint32_t interval)
return;
}

Direction dir = Position::getRandomDirection();
if (canWalkTo(getPosition(), dir)) {
listWalkDir.push_front(dir);
if (Direction newDirection;
getRandomStep(newDirection))
{
listWalkDir.push_front(newDirection);
addEventWalk();
}

Expand Down Expand Up @@ -562,6 +563,27 @@ bool Npc::getNextStep(Direction& nextDirection, uint32_t& flags) {
return Creature::getNextStep(nextDirection, flags);
}

bool Npc::getRandomStep(Direction& moveDirection) const
{
static std::vector<Direction> directionvector {
Direction::DIRECTION_NORTH,
Direction::DIRECTION_WEST,
Direction::DIRECTION_EAST,
Direction::DIRECTION_SOUTH
};
std::ranges::shuffle(directionvector, getRandomGenerator());

for (const Position& creaturePos = getPosition();
Direction direction : directionvector)
{
if (canWalkTo(creaturePos, direction)) {
moveDirection = direction;
return true;
}
}
return false;
}

void Npc::addShopPlayer(Player* player)
{
shopPlayerSet.insert(player);
Expand Down
3 changes: 2 additions & 1 deletion src/creatures/npcs/npc.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ class Npc final : public Creature
void onPlacedCreature() override;

bool canWalkTo(const Position& fromPos, Direction dir) const;
bool getNextStep(Direction& direction, uint32_t& flags) override;
bool getNextStep(Direction& nextDirection, uint32_t& flags) override;
bool getRandomStep(Direction& moveDirection) const;

void setNormalCreatureLight() override {
internalLight = npcType->info.light;
Expand Down