Initial commit.

This commit is contained in:
2024-05-20 16:31:11 +02:00
commit 4b5d01c2a7
27 changed files with 1135 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
*.obj
*.exp
*.lib
.sconsign.dblite
*.dll

4
.gitmodules vendored Normal file
View File

@@ -0,0 +1,4 @@
[submodule "godot-cpp"]
path = godot-cpp
url = https://github.com/godotengine/godot-cpp
branch = 4.2

32
SConstruct Normal file
View File

@@ -0,0 +1,32 @@
#!/usr/bin/env python
import os
import sys
env = SConscript("godot-cpp/SConstruct")
# For reference:
# - CCFLAGS are compilation flags shared between C and C++
# - CFLAGS are for C-specific compilation flags
# - CXXFLAGS are for C++-specific compilation flags
# - CPPFLAGS are for pre-processor flags
# - CPPDEFINES are for pre-processor defines
# - LINKFLAGS are for linking flags
# tweak this if you want to use different folders, or more folders, to store your source code in.
env.Append(CPPPATH=["src/"])
sources = Glob("src/*.cpp")
if env["platform"] == "macos":
library = env.SharedLibrary(
"demo/bin/libMultilevelAStarExt.{}.{}.framework/libMultilevelAStarExt.{}.{}".format(
env["platform"], env["target"], env["platform"], env["target"]
),
source=sources,
)
else:
library = env.SharedLibrary(
"demo/bin/libMultilevelAStarExt{}{}".format(env["suffix"], env["SHLIBSUFFIX"]),
source=sources,
)
Default(library)

1
build.cmd Normal file
View File

@@ -0,0 +1 @@
scons platform=windows

2
demo/.gitattributes vendored Normal file
View File

@@ -0,0 +1,2 @@
# Normalize EOL for all files that Git considers text files.
* text=auto eol=lf

5
demo/.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
# Godot 4+ specific ignores
.godot/
# Misc
*.tmp

View File

@@ -0,0 +1,13 @@
[configuration]
entry_symbol = "my_astar_ext_init"
compatibility_minimum = "4.2"
[libraries]
macos.debug = "res://bin/libMultilevelAStarExt.macos.template_debug.framework"
macos.release = "res://bin/libMultilevelAStarExt.macos.template_release.framework"
windows.debug.x86_64 = "res://bin/libMultilevelAStarExt.windows.template_debug.x86_64.dll"
windows.release.x86_64 = "res://bin/libMultilevelAStarExt.windows.template_release.x86_64.dll"
linux.debug.x86_64 = "res://bin/libMultilevelAStarExt.linux.template_debug.x86_64.so"
linux.release.x86_64 = "res://bin/libMultilevelAStarExt.linux.template_release.x86_64.so"

View File

@@ -0,0 +1,63 @@
extends RefCounted
class_name MultilevelAStar
# tile custom data "type":
# 0 => blocked
# 1(+) => can move to same number
# -1(-) => can move to same, one larger or one smaller as long as they're both negative
# => can move to and from its absolute value and one larger than its absolute value
# private variables start with an _ as per the GDScript style guide
var _used_rect: Rect2i
var _astar: MultilevelAStarEx
func _init(map: TileMap):
_used_rect = map.get_used_rect()
# assert stuff here because the extension's assertions just crash without a message
assert(_used_rect.get_area() > 0)
_astar = MultilevelAStarEx.new()
_astar.init(_used_rect)
for layer in map.get_layers_count():
var cells := map.get_used_cells(layer)
for cell in cells:
var tile_data := map.get_cell_tile_data(layer, cell)
if tile_data != null:
var type = tile_data.get_custom_data("type")
assert(type is int)
_astar.set_terrain(cell, type)
func is_unit(point: Vector2i) -> bool:
assert(_used_rect.has_point(point))
return _astar.get_unit(point)
func set_unit(point: Vector2i, solid: bool = true) -> void:
assert(_used_rect.has_point(point))
_astar.set_unit(point, solid)
func get_terrain(point: Vector2i) -> MultilevelAStarEx.TerrainType:
assert(_used_rect.has_point(point))
return _astar.get_terrain(point)
func set_terrain(point: Vector2i, type: MultilevelAStarEx.TerrainType) -> void:
assert(_used_rect.has_point(point))
_astar.set_terrain(point, type)
func find_path(from: Vector2i, to: Vector2i, return_closest: bool = false) -> Array[Vector2i]:
assert(_used_rect.has_point(from))
assert(_used_rect.has_point(to))
var res = _astar.find_path(from, to, return_closest) # vrne Variant: null ali Array[Vector2i]
if res != null:
return res
else:
return []

1
demo/icon.svg Normal file
View File

@@ -0,0 +1 @@
<svg height="128" width="128" xmlns="http://www.w3.org/2000/svg"><rect x="2" y="2" width="124" height="124" rx="14" fill="#363d52" stroke="#212532" stroke-width="4"/><g transform="scale(.101) translate(122 122)"><g fill="#fff"><path d="M105 673v33q407 354 814 0v-33z"/><path d="m105 673 152 14q12 1 15 14l4 67 132 10 8-61q2-11 15-15h162q13 4 15 15l8 61 132-10 4-67q3-13 15-14l152-14V427q30-39 56-81-35-59-83-108-43 20-82 47-40-37-88-64 7-51 8-102-59-28-123-42-26 43-46 89-49-7-98 0-20-46-46-89-64 14-123 42 1 51 8 102-48 27-88 64-39-27-82-47-48 49-83 108 26 42 56 81zm0 33v39c0 276 813 276 814 0v-39l-134 12-5 69q-2 10-14 13l-162 11q-12 0-16-11l-10-65H446l-10 65q-4 11-16 11l-162-11q-12-3-14-13l-5-69z" fill="#478cbf"/><path d="M483 600c0 34 58 34 58 0v-86c0-34-58-34-58 0z"/><circle cx="725" cy="526" r="90"/><circle cx="299" cy="526" r="90"/></g><g fill="#414042"><circle cx="307" cy="532" r="60"/><circle cx="717" cy="532" r="60"/></g></g></svg>

After

Width:  |  Height:  |  Size: 949 B

37
demo/icon.svg.import Normal file
View File

@@ -0,0 +1,37 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://cpupav84hxj1u"
path="res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://icon.svg"
dest_files=["res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
svg/scale=1.0
editor/scale_with_editor_scale=false
editor/convert_colors_with_editor_theme=false

74
demo/main.gd Normal file
View File

@@ -0,0 +1,74 @@
extends Node2D
const CELL_SIZE: int = 32
@onready var map: TileMap = $TileMap
@onready var marker_from: Node2D = $MarkerFrom
@onready var marker_to: Node2D = $MarkerTo
@onready var path_line: Line2D = $PathLine
@onready var check_box: CheckBox = $CheckBox
var astar: MultilevelAStar
var used_rect: Rect2i
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
# mapa začne na -1, -1. tukaj premaknem da se vidi vse na mapi
# (saj verjetno obstaja boljši način ma ga ne poznam - mogoče kakšna kamera)
position = Vector2i.ONE * CELL_SIZE
used_rect = map.get_used_rect()
print(used_rect)
astar = MultilevelAStar.new(map)
mark_units()
marker_from.modulate = Color.html("#ff0000ff")
marker_to.modulate = Color.html("#00ff00ff")
marker_from.position = Vector2i(-1, -1) * CELL_SIZE
marker_to.position = (used_rect.position + used_rect.size - Vector2i.ONE) * CELL_SIZE
find_path()
func mark_units() -> void:
for unit in $Units.get_tree().get_nodes_in_group("unit"):
astar.set_unit(unit.position / CELL_SIZE)
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouseButton:
if event.pressed:
var cell := map.local_to_map(map.get_local_mouse_position())
if map.get_used_rect().has_point(cell):
if event.button_index == 1:
marker_to.position = Vector2(cell.x * CELL_SIZE, cell.y * CELL_SIZE)
find_path();
elif event.button_index == 2:
marker_from.position = Vector2(cell.x * CELL_SIZE, cell.y * CELL_SIZE)
find_path();
func find_path() -> void:
path_line.clear_points()
var from: Vector2i = Vector2i(marker_from.position / CELL_SIZE)
var to: Vector2i = Vector2i(marker_to.position / CELL_SIZE)
var arr = astar.find_path(from, to, check_box.button_pressed) # vrne Variant: null ali Array
#print(arr)
if arr.size() > 0:
# add new path
path_line.add_point(from * CELL_SIZE + Vector2i(16, 16))
for vec in arr:
#print("%s, %s" % [vec.x, vec.y])
path_line.add_point(vec * CELL_SIZE + Vector2i(16, 16))
func _on_check_box_pressed() -> void:
find_path()

84
demo/main.tscn Normal file

File diff suppressed because one or more lines are too long

BIN
demo/markers/marker.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 251 B

View File

@@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://ydnd8d7pbpct"
path="res://.godot/imported/marker.png-302bb6fffff2bb75ddac5872ea46d55a.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://markers/marker.png"
dest_files=["res://.godot/imported/marker.png-302bb6fffff2bb75ddac5872ea46d55a.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1

9
demo/markers/marker.tscn Normal file
View File

@@ -0,0 +1,9 @@
[gd_scene load_steps=2 format=3 uid="uid://cnrhrho63n4n2"]
[ext_resource type="Texture2D" uid="uid://ydnd8d7pbpct" path="res://markers/marker.png" id="1_mefdn"]
[node name="Marker" type="Node2D"]
[node name="Sprite2D" type="Sprite2D" parent="."]
texture = ExtResource("1_mefdn")
offset = Vector2(16, 16)

BIN
demo/markers/unit.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 156 B

View File

@@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://b6j8es5qgtv05"
path="res://.godot/imported/unit.png-1fe68df64273b07deeeaa2e1953638b5.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://markers/unit.png"
dest_files=["res://.godot/imported/unit.png-1fe68df64273b07deeeaa2e1953638b5.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1

9
demo/markers/unit.tscn Normal file
View File

@@ -0,0 +1,9 @@
[gd_scene load_steps=2 format=3 uid="uid://cj34xrleexqf6"]
[ext_resource type="Texture2D" uid="uid://b6j8es5qgtv05" path="res://markers/unit.png" id="1_qutcq"]
[node name="Unit" type="Node2D" groups=["unit"]]
[node name="Sprite2D" type="Sprite2D" parent="."]
position = Vector2(16, 16)
texture = ExtResource("1_qutcq")

20
demo/project.godot Normal file
View File

@@ -0,0 +1,20 @@
; Engine configuration file.
; It's best edited using the editor UI and not directly,
; since the parameters that go here are not all obvious.
;
; Format:
; [section] ; section goes between []
; param=value ; assign values to parameters
config_version=5
[application]
config/name="MultilevelAStarTest"
run/main_scene="res://main.tscn"
config/features=PackedStringArray("4.2", "Forward Plus")
config/icon="res://icon.svg"
[display]
window/stretch/mode="canvas_items"

BIN
demo/tiles/socerb.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

View File

@@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://dh20x6lnm6ly8"
path="res://.godot/imported/socerb.png-80780b522a349cb35f220b47d0cdac2c.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://tiles/socerb.png"
dest_files=["res://.godot/imported/socerb.png-80780b522a349cb35f220b47d0cdac2c.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1

204
demo/tiles/socerb.tres Normal file
View File

@@ -0,0 +1,204 @@
[gd_resource type="TileSet" load_steps=3 format=3 uid="uid://dsc2e3ohtmv01"]
[ext_resource type="Texture2D" uid="uid://dh20x6lnm6ly8" path="res://tiles/socerb.png" id="1_e53on"]
[sub_resource type="TileSetAtlasSource" id="TileSetAtlasSource_dokfe"]
texture = ExtResource("1_e53on")
texture_region_size = Vector2i(32, 32)
0:0/0 = 0
0:0/0/custom_data_0 = 1
1:0/0 = 0
1:0/0/custom_data_0 = -1
2:0/0 = 0
2:0/0/custom_data_0 = -1
3:0/0 = 0
3:0/0/custom_data_0 = -1
4:0/0 = 0
4:0/0/custom_data_0 = -2
8:0/0 = 0
9:0/0 = 0
10:0/0 = 0
11:0/0 = 0
12:0/0 = 0
13:0/0 = 0
0:1/0 = 0
0:1/0/custom_data_0 = 1
1:1/0 = 0
1:1/0/custom_data_0 = 1
2:1/0 = 0
3:1/0 = 0
4:1/0 = 0
5:1/0 = 0
6:1/0 = 0
7:1/0 = 0
8:1/0 = 0
9:1/0 = 0
9:1/0/custom_data_0 = 3
10:1/0 = 0
10:1/0/custom_data_0 = 3
11:1/0 = 0
11:1/0/custom_data_0 = 3
12:1/0 = 0
12:1/0/custom_data_0 = 3
13:1/0 = 0
14:1/0 = 0
0:2/0 = 0
0:2/0/custom_data_0 = 1
1:2/0 = 0
1:2/0/custom_data_0 = 1
2:2/0 = 0
3:2/0 = 0
4:2/0 = 0
5:2/0 = 0
6:2/0 = 0
7:2/0 = 0
8:2/0 = 0
8:2/0/custom_data_0 = 3
9:2/0 = 0
9:2/0/custom_data_0 = 3
10:2/0 = 0
10:2/0/custom_data_0 = 3
11:2/0 = 0
11:2/0/custom_data_0 = 3
12:2/0 = 0
12:2/0/custom_data_0 = 3
13:2/0 = 0
13:2/0/custom_data_0 = 3
14:2/0 = 0
0:3/0 = 0
0:3/0/custom_data_0 = 1
1:3/0 = 0
1:3/0/custom_data_0 = 1
2:3/0 = 0
3:3/0 = 0
3:3/0/custom_data_0 = 1
4:3/0 = 0
4:3/0/custom_data_0 = 1
5:3/0 = 0
5:3/0/custom_data_0 = 1
6:3/0 = 0
7:3/0 = 0
8:3/0 = 0
8:3/0/custom_data_0 = 3
9:3/0 = 0
9:3/0/custom_data_0 = 3
10:3/0 = 0
10:3/0/custom_data_0 = 3
11:3/0 = 0
11:3/0/custom_data_0 = 3
12:3/0 = 0
12:3/0/custom_data_0 = 3
13:3/0 = 0
13:3/0/custom_data_0 = 3
14:3/0 = 0
1:4/0 = 0
2:4/0 = 0
3:4/0 = 0
4:4/0 = 0
5:4/0 = 0
5:4/0/custom_data_0 = 1
6:4/0 = 0
7:4/0 = 0
8:4/0 = 0
8:4/0/custom_data_0 = 3
9:4/0 = 0
9:4/0/custom_data_0 = 3
10:4/0 = 0
10:4/0/custom_data_0 = 3
11:4/0 = 0
11:4/0/custom_data_0 = 3
12:4/0 = 0
12:4/0/custom_data_0 = 3
13:4/0 = 0
13:4/0/custom_data_0 = 3
14:4/0 = 0
2:5/0 = 0
3:5/0 = 0
4:5/0 = 0
5:5/0 = 0
6:5/0 = 0
7:5/0 = 0
8:5/0 = 0
8:5/0/custom_data_0 = 3
9:5/0 = 0
9:5/0/custom_data_0 = 3
10:5/0 = 0
10:5/0/custom_data_0 = 3
11:5/0 = 0
11:5/0/custom_data_0 = 3
12:5/0 = 0
12:5/0/custom_data_0 = 3
13:5/0 = 0
13:5/0/custom_data_0 = 3
14:5/0 = 0
2:6/0 = 0
3:6/0 = 0
4:6/0 = 0
5:6/0 = 0
6:6/0 = 0
7:6/0 = 0
8:6/0 = 0
9:6/0 = 0
9:6/0/custom_data_0 = 3
10:6/0 = 0
10:6/0/custom_data_0 = 3
11:6/0 = 0
11:6/0/custom_data_0 = 3
12:6/0 = 0
12:6/0/custom_data_0 = 3
13:6/0 = 0
14:6/0 = 0
2:7/0 = 0
3:7/0 = 0
3:7/0/custom_data_0 = 2
4:7/0 = 0
4:7/0/custom_data_0 = 2
5:7/0 = 0
5:7/0/custom_data_0 = 2
6:7/0 = 0
8:7/0 = 0
9:7/0 = 0
10:7/0 = 0
11:7/0 = 0
12:7/0 = 0
13:7/0 = 0
2:8/0 = 0
3:8/0 = 0
3:8/0/custom_data_0 = 2
4:8/0 = 0
4:8/0/custom_data_0 = 2
5:8/0 = 0
5:8/0/custom_data_0 = 2
6:8/0 = 0
2:9/0 = 0
3:9/0 = 0
3:9/0/custom_data_0 = 2
4:9/0 = 0
4:9/0/custom_data_0 = 2
5:9/0 = 0
5:9/0/custom_data_0 = 2
6:9/0 = 0
2:10/0 = 0
3:10/0 = 0
4:10/0 = 0
5:10/0 = 0
6:10/0 = 0
0:11/0 = 0
0:12/0 = 0
1:12/0 = 0
2:12/0 = 0
0:13/0 = 0
1:13/0 = 0
2:13/0 = 0
0:14/0 = 0
1:14/0 = 0
2:14/0 = 0
0:15/0 = 0
1:15/0 = 0
2:15/0 = 0
[resource]
tile_size = Vector2i(32, 32)
custom_data_layer_0/name = "type"
custom_data_layer_0/type = 2
sources/0 = SubResource("TileSetAtlasSource_dokfe")

1
godot-cpp Submodule

Submodule godot-cpp added at b28098e76b

346
src/MultilevelAStarEx.cpp Normal file
View File

@@ -0,0 +1,346 @@
#include "MultilevelAStarEx.h"
#include <godot_cpp/core/class_db.hpp>
#include <godot_cpp/core/error_macros.hpp>
#include <godot_cpp/variant/utility_functions.hpp>
#include <cstdlib>
#include <algorithm>
#include <cassert>
#define TERRAIN(x, y) (_terrain[(x) + (y) * _width])
#define UNITS(x, y) (_units[(x) + (y) * _width])
#define NODES(x, y) (_nodes[(x) + (y) * _width])
using namespace godot;
Node::Node() { }
Node::Node(int x, int y)
{
this->x = x;
this->y = y;
this->state = UNUSED;
}
void Node::init(Node* parent, int distanceFromStart, const Vector2i &end)
{
this->state = OPEN;
this->parent = parent;
this->distanceFromStart = distanceFromStart;
int dx = abs(x - end.x);
int dy = abs(y - end.y);
this->distanceToEnd = (dx + dy) * 10;
if ((dx == 0 || dx == 1) && (dy == 0 || dy == 1))
{
this->distanceToEndForClosest = 10;
}
else
{
this->distanceToEndForClosest = this->distanceToEnd;
}
}
int Node::total_cost() const
{
return distanceFromStart + distanceToEnd;
}
void MultilevelAStarEx::_bind_methods()
{
ClassDB::bind_method(D_METHOD("init", "region"), &MultilevelAStarEx::init);
ClassDB::bind_method(D_METHOD("get_region"), &MultilevelAStarEx::get_region);
ClassDB::bind_method(D_METHOD("set_terrain", "cell", "type"), &MultilevelAStarEx::set_terrain);
ClassDB::bind_method(D_METHOD("get_terrain", "cell"), &MultilevelAStarEx::get_terrain);
ClassDB::bind_method(D_METHOD("set_unit", "cell", "blocked"), &MultilevelAStarEx::set_unit);
ClassDB::bind_method(D_METHOD("get_unit", "cell"), &MultilevelAStarEx::get_unit);
ClassDB::bind_method(D_METHOD("find_path", "from", "to", "return_closest"), &MultilevelAStarEx::find_path);
BIND_ENUM_CONSTANT(STAIRS);
BIND_ENUM_CONSTANT(BLOCKED);
BIND_ENUM_CONSTANT(GROUND);
}
MultilevelAStarEx::MultilevelAStarEx()
{
//UtilityFunctions::print("Constructor.");
_init = false;
}
MultilevelAStarEx::~MultilevelAStarEx()
{
//UtilityFunctions::print("Destructor.");
}
void MultilevelAStarEx::init(const Rect2i &region)
{
DEV_ASSERT(!_init);
DEV_ASSERT(region.get_area() > 0);
_region = region;
_width = region.get_size().width;
_height = region.get_size().height;
_trans = region.get_position();
_terrain.resize(_width * _height, BLOCKED);
_units.resize(_width * _height, false);
_nodes.resize(_width * _height);
std::vector<Node>::iterator iter = _nodes.begin();
for (int y = 0; y < _height; y++)
{
for (int x = 0; x < _width; x++)
{
*iter++ = Node(x, y);
}
}
_init = true;
}
Rect2i MultilevelAStarEx::get_region() const
{
DEV_ASSERT(_init);
return _region;
}
void MultilevelAStarEx::set_terrain(const Vector2i &cell, TerrainType type)
{
DEV_ASSERT(_init);
DEV_ASSERT(_region.has_point(cell));
Vector2i cell2 = cell - _trans;
TERRAIN(cell2.x, cell2.y) = type;
}
MultilevelAStarEx::TerrainType MultilevelAStarEx::get_terrain(const Vector2i &cell) const
{
DEV_ASSERT(_init);
DEV_ASSERT(_region.has_point(cell));
Vector2i cell2 = cell - _trans;
return TERRAIN(cell2.x, cell2.y);
}
void MultilevelAStarEx::set_unit(const Vector2i &cell, bool blocked)
{
DEV_ASSERT(_init);
DEV_ASSERT(_region.has_point(cell));
Vector2i cell2 = cell - _trans;
UNITS(cell2.x, cell2.y) = blocked;
}
bool MultilevelAStarEx::get_unit(const Vector2i &cell) const
{
DEV_ASSERT(_init);
DEV_ASSERT(_region.has_point(cell));
Vector2i cell2 = cell - _trans;
return UNITS(cell2.x, cell2.y);
}
bool MultilevelAStarEx::can_move(const Node* current, int x, int y) const
{
if (UNITS(x, y)) return false;
TerrainType tc = TERRAIN(current->x, current->y);
TerrainType td = TERRAIN(x, y);
if (td == BLOCKED)
{
return false;
}
else if (td == tc)
{
return true;
}
else if (td < 0 && tc > 0) // td is stairs
{
if (-td == tc || -td == tc - 1) return true;
}
else if (td > 0 && tc < 0) // tc is stairs
{
if (-tc == td || -tc == td - 1) return true;
}
else if (td < 0 && tc < 0) // both are stairs
{
return abs(td - tc) < 2;
}
return false;
}
TypedArray<Vector2i> MultilevelAStarEx::generate_path(const Node* current) const
{
TypedArray<Vector2i> arr;
while (current->parent != nullptr)
{
arr.insert(0, Vector2i(current->x, current->y) + _trans);
current = current->parent;
}
return arr;
}
Variant MultilevelAStarEx::find_path(const Vector2i &from, const Vector2i &to, const bool return_closest)
{
DEV_ASSERT(_init);
DEV_ASSERT(_region.has_point(from));
DEV_ASSERT(_region.has_point(to));
Vector2i from2 = from - _trans;
Vector2i to2 = to - _trans;
Variant result;
std::vector<Node*> open;
std::vector<Node*> closed;
open.reserve(_width * _height);
closed.reserve(_width * _height);
Node* closest = &NODES(from2.x, from2.y);
closest->init(nullptr, 0, to2);
open.push_back(closest);
auto process = [this, &open, &closed, &to2, &closest](Node* current, int x, int y, int distance) {
Node* node = &NODES(x, y);
if (node->state == Node::UNUSED)
{
node->init(current, current->distanceFromStart + distance, to2);
open.push_back(node);
}
else if (node->state == Node::OPEN)
{
if (current->distanceFromStart < node->parent->distanceFromStart)
{
node->parent = current;
node->distanceFromStart = current->distanceFromStart + distance;
}
}
// find the closest cell
if (node->distanceToEndForClosest < closest->distanceToEndForClosest)
{
closest = node;
}
else if (node->distanceToEndForClosest == closest->distanceToEndForClosest)
{
if (node->distanceFromStart < closest->distanceFromStart)
{
closest = node;
}
}
};
while (open.size() > 0)
{
// find closest to destination
Node* current = open[0];
for (Node* n : open)
{
if (n->total_cost() < current->total_cost())
{
current = n;
}
}
if (current->distanceToEnd == 0)
{
// found the path
result = Variant(generate_path(current));
goto cleanup;
}
// close it
current->state = Node::CLOSED;
open.erase(std::remove(open.begin(), open.end(), current), open.end());
closed.push_back(current);
// expand it
if (current->x - 1 >= 0) // left
{
if (can_move(current, current->x - 1, current->y))
{
process(current, current->x - 1, current->y, 10);
}
}
if (current->x + 1 < _width) // right
{
if (can_move(current, current->x + 1, current->y))
{
process(current, current->x + 1, current->y, 10);
}
}
if (current->y - 1 >= 0) // up
{
if (can_move(current, current->x, current->y - 1))
{
process(current, current->x, current->y - 1, 10);
}
}
if (current->y + 1 < _height) // down
{
if (can_move(current, current->x, current->y + 1))
{
process(current, current->x, current->y + 1, 10);
}
}
if ((current->x - 1 >= 0) && (current->y - 1 >= 0)) // top left
{
if (can_move(current, current->x - 1, current->y - 1))
{
process(current, current->x - 1, current->y - 1, 14);
}
}
if ((current->x + 1 < _width) && (current->y - 1 >= 0)) // top right
{
if (can_move(current, current->x + 1, current->y - 1))
{
process(current, current->x + 1, current->y - 1, 14);
}
}
if ((current->x - 1 >= 0) && (current->y + 1 < _height)) // bottom left
{
if (can_move(current, current->x - 1, current->y + 1))
{
process(current, current->x - 1, current->y + 1, 14);
}
}
if ((current->x + 1 < _width) && (current->y + 1 < _height)) // bottom right
{
if (can_move(current, current->x + 1, current->y + 1))
{
process(current, current->x + 1, current->y + 1, 14);
}
}
}
// this is skipped if a path was found
if (return_closest)
{
// return path to closest
result = Variant(generate_path(closest));
}
cleanup:
// release the nodes
for (Node* node : open)
{
node->state = Node::UNUSED;
}
for (Node* node : closed)
{
node->state = Node::UNUSED;
}
return result;
}

85
src/MultilevelAStarEx.h Normal file
View File

@@ -0,0 +1,85 @@
#ifndef MultilevelAStarEx_H
#define MultilevelAStarEx_H
#include <gdextension_interface.h>
#include <godot_cpp/classes/ref.hpp>
#include <godot_cpp/variant/array.hpp>
#include <godot_cpp/variant/vector2i.hpp>
#include <vector>
namespace godot {
class Node {
friend class MultilevelAStarEx;
public:
enum NodeState
{
UNUSED, OPEN, CLOSED
};
private:
NodeState state;
int x, y;
Node* parent;
int distanceFromStart;
int distanceToEnd;
int distanceToEndForClosest;
Node(int x, int y);
void init(Node* parent, int distanceFromStart, const Vector2i &end);
int total_cost() const;
public:
Node();
};
class MultilevelAStarEx : public RefCounted
{
GDCLASS(MultilevelAStarEx, RefCounted)
public:
enum TerrainType
{
STAIRS = -1,
BLOCKED = 0,
GROUND = 1,
};
private:
bool _init;
std::vector<TerrainType> _terrain;
std::vector<bool> _units;
std::vector<Node> _nodes;
Rect2i _region;
Vector2i _trans;
int _width, _height;
bool can_move(const Node* current, int x, int y) const;
TypedArray<Vector2i> generate_path(const Node* current) const;
protected:
static void _bind_methods();
public:
MultilevelAStarEx();
~MultilevelAStarEx();
void init(const Rect2i &region);
Rect2i get_region() const;
void set_terrain(const Vector2i &cell, TerrainType type);
TerrainType get_terrain(const Vector2i &cell) const;
void set_unit(const Vector2i &cell, bool blocked);
bool get_unit(const Vector2i &cell) const;
Variant find_path(const Vector2i &from, const Vector2i &to, const bool return_closest);
};
}
VARIANT_ENUM_CAST(MultilevelAStarEx::TerrainType);
#endif

36
src/main.cpp Normal file
View File

@@ -0,0 +1,36 @@
#include "MultilevelAStarEx.h"
#include <gdextension_interface.h>
#include <godot_cpp/core/defs.hpp>
#include <godot_cpp/godot.hpp>
using namespace godot;
void initialize_module(ModuleInitializationLevel p_level)
{
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}
ClassDB::register_class<MultilevelAStarEx>();
}
void uninitialize_module(ModuleInitializationLevel p_level)
{
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}
}
extern "C" {
GDExtensionBool GDE_EXPORT my_astar_ext_init(GDExtensionInterfaceGetProcAddress p_get_proc_address, const GDExtensionClassLibraryPtr p_library, GDExtensionInitialization *r_initialization)
{
godot::GDExtensionBinding::InitObject init_obj(p_get_proc_address, p_library, r_initialization);
init_obj.register_initializer(initialize_module);
init_obj.register_terminator(uninitialize_module);
init_obj.set_minimum_library_initialization_level(MODULE_INITIALIZATION_LEVEL_SCENE);
return init_obj.init();
}
}

2
term.cmd Normal file
View File

@@ -0,0 +1,2 @@
@set PATH=%PATH%;D:\Programs\scons
@cmd