* Level: Implement findPath with PathFinder now.

This commit is contained in:
iProgramInCpp
2023-12-06 14:13:10 +02:00
parent cd7acc8bd7
commit 8602de2428
6 changed files with 42 additions and 26 deletions

View File

@@ -61,9 +61,9 @@ public:
#else
#define LOG(...)
#define LOG_I(...)
#define LOG_W(...)
#define LOG_E(...)
#define LOG(...) ((void)0)
#define LOG_I(...) ((void)0)
#define LOG_W(...) ((void)0)
#define LOG_E(...) ((void)0)
#endif

View File

@@ -50,6 +50,8 @@ Level::Level(LevelStorage* pStor, const std::string& str, TLong seed, int x, Dim
m_pDimension->init(this);
m_pPathFinder = new PathFinder();
m_pChunkSource = createChunkSource();
updateSkyBrightness();
}
@@ -58,6 +60,7 @@ Level::~Level()
{
SAFE_DELETE(m_pChunkSource);
SAFE_DELETE(m_pDimension);
SAFE_DELETE(m_pPathFinder);
const size_t size = m_entities.size();
for (int i = 0; i < size; i++)
@@ -1785,24 +1788,30 @@ void Level::extinguishFire(int x, int y, int z, int dir)
setTile(x, y, z, TILE_AIR);
}
int Level::findPath(Entity* ent1, Entity* ent2, float f)
int Level::findPath(Path* path, Entity* ent, Entity* target, float f)
{
int fx = Mth::floor(ent1->m_pos.x);
int fy = Mth::floor(ent1->m_pos.y);
int fz = Mth::floor(ent1->m_pos.z);
int fx = Mth::floor(ent->m_pos.x);
int fy = Mth::floor(ent->m_pos.y);
int fz = Mth::floor(ent->m_pos.z);
Region reg(this, fx - int(f + 16), fy - int(f + 16), fz - int(f + 16), fx + int(f + 16), fy + int(f + 16), fz + int(f + 16));
// nothing more.
return 0;
m_pPathFinder->setLevel(&reg);
m_pPathFinder->findPath(*path, ent, target->m_pos.x, target->m_hitbox.min.y, target->m_pos.z, f);
return 1;
}
int Level::findPath(Entity* ent1, int dx, int dy, int dz, float f)
int Level::findPath(Path* path, Entity* ent, int dx, int dy, int dz, float f)
{
int fx = Mth::floor(ent1->m_pos.x);
int fy = Mth::floor(ent1->m_pos.y);
int fz = Mth::floor(ent1->m_pos.z);
Region reg(this, fx - int(f + 16), fy - int(f + 16), fz - int(f + 16), fx + int(f + 16), fy + int(f + 16), fz + int(f + 16));
// nothing more.
return 0;
int fx = Mth::floor(ent->m_pos.x);
int fy = Mth::floor(ent->m_pos.y);
int fz = Mth::floor(ent->m_pos.z);
Region reg(this, fx - int(f + 8), fy - int(f + 8), fz - int(f + 8), fx + int(f + 8), fy + int(f + 8), fz + int(f + 8));
m_pPathFinder->setLevel(&reg);
m_pPathFinder->findPath(*path, ent, float(dx) + 0.5f, float(dy) + 0.5f, float(dz) + 0.5f, f);
return 1;
}
int Level::getLightDepth(int x, int z)

View File

@@ -22,6 +22,7 @@
#include "world/level/storage/LevelStorageSource.hpp"
#include "world/level/storage/LevelSource.hpp"
#include "world/level/storage/LevelData.hpp"
#include "world/level/path/PathFinder.hpp"
#include "Dimension.hpp"
#include "LevelListener.hpp"
#include "TickNextTickData.hpp"
@@ -137,8 +138,8 @@ public:
void addEntities(const std::vector<Entity*>& entities);
void ensureAdded(Entity* entity);
void extinguishFire(int x, int y, int z, int dir);
int findPath(Entity* ent1, Entity* ent2, float f);
int findPath(Entity* ent1, int x, int y, int z, float f);
int findPath(Path* path, Entity* ent1, Entity* ent2, float f);
int findPath(Path* path, Entity* ent1, int x, int y, int z, float f);
int getLightDepth(int x, int z);
float getStarBrightness(float f);
float getSunAngle(float f);
@@ -195,5 +196,6 @@ public:
int field_B08;
uint8_t field_B0C;
int field_B10;
PathFinder* m_pPathFinder;
};

View File

@@ -10,8 +10,8 @@
float Node::distanceTo(Node* node)
{
float dx = node->m_x - m_x;
float dy = node->m_y - m_y;
float dz = node->m_z - m_z;
float dx = float(node->m_x) - float(m_x);
float dy = float(node->m_y) - float(m_y);
float dz = float(node->m_z) - float(m_z);
return Mth::sqrt(dx * dx + dy * dy + dz * dz);
}

View File

@@ -27,8 +27,8 @@ void Path::setNodes(Node** pNodes, int nodeCount)
for (int i = 0; i < nodeCount; i++)
{
// TODO: We are using the pNodes array for storage but duplicating the pNodes?
// This might cause a memory leak?
// This won't cause a memory leak since the pNodes before are managed
// by
Node* oldNode = pNodes[i];
m_pNodes[i] = new Node;
*m_pNodes[i] = *oldNode;

View File

@@ -12,7 +12,7 @@
#include "Path.hpp"
#include "BinaryHeap.hpp"
class Level;
class LevelSource;
class Entity;
#define MAX_NODE_COUNT (2048)
@@ -33,12 +33,17 @@ public:
bool findPath(Path&, Entity*, Node*, Node*, const Node*, float);
bool findPath(Path&, Entity*, float, float, float, float);
void setLevel(LevelSource* pLevel)
{
m_pLevel = pLevel;
}
private:
Node* new_Node(int x, int y, int z);
bool inlined_0(Path& path, Node* node2);
private:
Level* m_pLevel;
LevelSource* m_pLevel;
BinaryHeap m_binaryHeap;
NodeMap m_nodeMap;
Node m_nodeReserve[MAX_NODE_COUNT];