Returning from point and returning empty array from C++ if no path found.
This commit is contained in:
@@ -56,13 +56,9 @@ func is_blocked(point: Vector2i) -> bool:
|
|||||||
return (_astar.get_terrain(point) == MultilevelAStarEx.BLOCKED) or _astar.get_unit(point)
|
return (_astar.get_terrain(point) == MultilevelAStarEx.BLOCKED) or _astar.get_unit(point)
|
||||||
|
|
||||||
|
|
||||||
|
# returns a list of points containing the path (including the starting point) or an empty array if no path was found
|
||||||
func find_path(from: Vector2i, to: Vector2i, return_closest: bool = false) -> Array[Vector2i]:
|
func find_path(from: Vector2i, to: Vector2i, return_closest: bool = false) -> Array[Vector2i]:
|
||||||
assert(_used_rect.has_point(from))
|
assert(_used_rect.has_point(from))
|
||||||
assert(_used_rect.has_point(to))
|
assert(_used_rect.has_point(to))
|
||||||
|
|
||||||
var res = _astar.find_path(from, to, return_closest) # returns Variant: null or Array[Vector2i]
|
return _astar.find_path(from, to, return_closest)
|
||||||
|
|
||||||
if res != null:
|
|
||||||
return res
|
|
||||||
else:
|
|
||||||
return []
|
|
||||||
|
|||||||
@@ -61,10 +61,10 @@ func find_path() -> void:
|
|||||||
|
|
||||||
var arr = astar.find_path(from, to, check_box.button_pressed) # vrne Variant: null ali Array
|
var arr = astar.find_path(from, to, check_box.button_pressed) # vrne Variant: null ali Array
|
||||||
#print(arr)
|
#print(arr)
|
||||||
|
print(arr.size())
|
||||||
|
|
||||||
if arr.size() > 0:
|
if arr.size() > 1: # if the start and the end are the same there is no line to draw
|
||||||
# add new path
|
# add new path
|
||||||
path_line.add_point(from * CELL_SIZE + Vector2i(16, 16))
|
|
||||||
for vec in arr:
|
for vec in arr:
|
||||||
#print("%s, %s" % [vec.x, vec.y])
|
#print("%s, %s" % [vec.x, vec.y])
|
||||||
path_line.add_point(vec * CELL_SIZE + Vector2i(16, 16))
|
path_line.add_point(vec * CELL_SIZE + Vector2i(16, 16))
|
||||||
|
|||||||
@@ -226,13 +226,11 @@ TypedArray<Vector2i> MultilevelAStarEx::generate_path(const Node *current) const
|
|||||||
int cnt = 0;
|
int cnt = 0;
|
||||||
for (const Node *n = current; n != nullptr; n = n->parent) cnt++;
|
for (const Node *n = current; n != nullptr; n = n->parent) cnt++;
|
||||||
|
|
||||||
cnt--; // because we're not adding the starting node
|
|
||||||
|
|
||||||
TypedArray<Vector2i> arr;
|
TypedArray<Vector2i> arr;
|
||||||
arr.resize(cnt);
|
arr.resize(cnt);
|
||||||
|
|
||||||
int i = cnt;
|
int i = cnt;
|
||||||
while (current->parent != nullptr)
|
while (current != nullptr)
|
||||||
{
|
{
|
||||||
arr[--cnt] = Vector2i(current->x, current->y) + _trans;
|
arr[--cnt] = Vector2i(current->x, current->y) + _trans;
|
||||||
current = current->parent;
|
current = current->parent;
|
||||||
@@ -241,7 +239,7 @@ TypedArray<Vector2i> MultilevelAStarEx::generate_path(const Node *current) const
|
|||||||
return arr;
|
return arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
Variant MultilevelAStarEx::find_path(const Vector2i &from, const Vector2i &to, bool return_closest)
|
TypedArray<Vector2i> MultilevelAStarEx::find_path(const Vector2i &from, const Vector2i &to, bool return_closest)
|
||||||
{
|
{
|
||||||
DEV_ASSERT(_init);
|
DEV_ASSERT(_init);
|
||||||
|
|
||||||
@@ -312,7 +310,7 @@ Variant MultilevelAStarEx::find_path(const Vector2i &from, const Vector2i &to, b
|
|||||||
if (current->distanceToEnd == 0)
|
if (current->distanceToEnd == 0)
|
||||||
{
|
{
|
||||||
// found the path
|
// found the path
|
||||||
return Variant(generate_path(current));
|
return generate_path(current);
|
||||||
}
|
}
|
||||||
|
|
||||||
// close it
|
// close it
|
||||||
@@ -381,8 +379,8 @@ Variant MultilevelAStarEx::find_path(const Vector2i &from, const Vector2i &to, b
|
|||||||
if (return_closest)
|
if (return_closest)
|
||||||
{
|
{
|
||||||
// return path to closest
|
// return path to closest
|
||||||
return Variant(generate_path(closest));
|
return generate_path(closest);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Variant();
|
return TypedArray<Vector2i>();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ public:
|
|||||||
TerrainType get_terrain(const Vector2i &cell) const;
|
TerrainType get_terrain(const Vector2i &cell) const;
|
||||||
void set_unit(const Vector2i &cell, bool blocked);
|
void set_unit(const Vector2i &cell, bool blocked);
|
||||||
bool get_unit(const Vector2i &cell) const;
|
bool get_unit(const Vector2i &cell) const;
|
||||||
Variant find_path(const Vector2i &from, const Vector2i &to, bool return_closest);
|
TypedArray<Vector2i> find_path(const Vector2i &from, const Vector2i &to, bool return_closest);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user