-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCAStar.cpp
56 lines (47 loc) · 1.35 KB
/
MCAStar.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "MCAStar.h"
#include <iostream>
static bool initialized = false;
static PathFindingWorld* world = nullptr;
MCAStarError init()
{
if (initialized)
return MC_ASTAR_ALREADY_INITIALIZED;
world = new PathFindingWorld();
/* e.g. OOM */
if (world == nullptr)
return MC_ASTAR_WORLD_INST_FAILED;
initialized = true;
return MC_ASTAR_NO_ERROR;
}
MCAStarError calculatePath(Path*& out, BlockPos start, BlockPos end)
{
if (!initialized)
return MCAStarError::MC_ASTAR_NOT_INITIALIZED;
if (&out == nullptr)
return MCAStarError::MC_ASTAR_NULLPTR;
uint32 closed, open;
out = AStar::calculatePath(closed, open, nullptr, world, start, end);
return MCAStarError::MC_ASTAR_NO_ERROR;
}
MCAStarError addChunk(ChunkPos pos, PathFindingChunk* chunk)
{
if (!initialized)
return MC_ASTAR_NOT_INITIALIZED;
if (chunk == nullptr)
return MCAStarError::MC_ASTAR_NULLPTR;
if (world == nullptr)
return MCAStarError::MC_ASTAR_NULLPTR;
if (world->isChunk(pos))
return MCAStarError::MC_ASTAR_INVALID_POSITION;
world->addChunk(pos, chunk);
return MCAStarError::MC_ASTAR_NO_ERROR;
}
MCAStarError setBlock(BlockPos pos, PathFindingBlockState state)
{
if (!initialized)
return MC_ASTAR_NOT_INITIALIZED;
if (!world->isChunk(ChunkPos(pos.x / 16, pos.y / 16, pos.z / 16)))
return MC_ASTAR_INVALID_POSITION;
world->setBlockState(pos, state);
return MC_ASTAR_NO_ERROR;
}