Moved everything into a C++ GDNative class, and changed it to a floodfill algorithm.

This commit is contained in:
Francois Belair 2019-09-27 14:16:51 -04:00
parent 39b3c79a82
commit 44a833794b
16 changed files with 1087 additions and 25 deletions

353
Native/.gitignore vendored Normal file
View file

@ -0,0 +1,353 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
# User-specific files
*.rsuser
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Mono auto generated files
mono_crash.*
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
[Aa][Rr][Mm]/
[Aa][Rr][Mm]64/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
[Ll]ogs/
# Visual Studio 2015/2017 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# Visual Studio 2017 auto generated files
Generated\ Files/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUnit
*.VisualState.xml
TestResult.xml
nunit-*.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# Benchmark Results
BenchmarkDotNet.Artifacts/
# .NET Core
project.lock.json
project.fragment.lock.json
artifacts/
# StyleCop
StyleCopReport.xml
# Files built by Visual Studio
*_i.c
*_p.c
*_h.h
*.ilk
*.meta
*.obj
*.iobj
*.pch
*.pdb
*.ipdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*_wpftmp.csproj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# Visual Studio Trace Files
*.e2e
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# JustCode is a .NET coding add-in
.JustCode
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# AxoCover is a Code Coverage Tool
.axoCover/*
!.axoCover/settings.json
# Visual Studio code coverage results
*.coverage
*.coveragexml
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# Note: Comment the next line if you want to checkin your web deploy settings,
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# NuGet Symbol Packages
*.snupkg
# The packages folder can be ignored because of Package Restore
**/[Pp]ackages/*
# except build/, which is used as an MSBuild target.
!**/[Pp]ackages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/[Pp]ackages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
*.appx
*.appxbundle
*.appxupload
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!?*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
orleans.codegen.cs
# Including strong name files can present a security risk
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
#*.snk
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
ServiceFabricBackup/
*.rptproj.bak
# SQL Server files
*.mdf
*.ldf
*.ndf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
*.rptproj.rsuser
*- [Bb]ackup.rdl
*- [Bb]ackup ([0-9]).rdl
*- [Bb]ackup ([0-9][0-9]).rdl
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
node_modules/
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# CodeRush personal settings
.cr/personal
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
# Cake - Uncomment if you are using it
# tools/**
# !tools/packages.config
# Tabs Studio
*.tss
# Telerik's JustMock configuration file
*.jmconfig
# BizTalk build output
*.btp.cs
*.btm.cs
*.odx.cs
*.xsd.cs
# OpenCover UI analysis results
OpenCover/
# Azure Stream Analytics local run output
ASALocalRun/
# MSBuild Binary and Structured Log
*.binlog
# NVidia Nsight GPU debugger configuration file
*.nvuser
# MFractors (Xamarin productivity tool) working folder
.mfractor/
# Local History for Visual Studio
.localhistory/
# BeatPulse healthcheck temp database
healthchecksdb
# Backup folder for Package Reference Convert tool in Visual Studio 2017
MigrationBackup/
# Ionide (cross platform F# VS Code tools) working folder
.ionide/

291
Native/Heatmap.cpp Normal file
View file

@ -0,0 +1,291 @@
#include "Heatmap.h"
#include <TileMap.hpp>
#include <SceneTree.hpp>
#include <Viewport.hpp>
#include <deque>
#include <TileSet.hpp>
namespace godot {
inline float lerp(const float& a, const float& b, const float& t) {
return a + t * (b - a);
}
void Heatmap::_register_methods() {
//public
register_method("_ready", &Heatmap::_ready);
register_method("_draw", &Heatmap::_draw);
register_method("_process", &Heatmap::_process);
register_method("best_direction_for", &Heatmap::best_direction_for);
register_method("calculate_point_index", &Heatmap::calculate_point_index);
register_method("calculate_point_index_for_world_position", &Heatmap::calculate_point_index_for_world_position);
//semi-private
register_method("_on_Events_player_moved", &Heatmap::on_Events_player_moved);
//properties
register_property<Heatmap, NodePath>("pathfinding_tilemap", &Heatmap::m_pathfinding_tilemap, NodePath());
register_property<Heatmap, bool>("draw_debug", &Heatmap::m_draw_debug, false);
}
Heatmap::Heatmap() : m_draw_debug(false), m_grid(nullptr), m_max_heat(0), m_max_heat_cache(0),
m_x_max(0), m_y_max(0), m_x_min(0), m_y_min(0), m_updating(false) {
}
Heatmap::~Heatmap() {
}
void Heatmap::_init() {
}
void Heatmap::_ready() {
m_grid = (TileMap*)get_node(m_pathfinding_tilemap);
if (m_grid == nullptr) {
Godot::print_error("No tilemap found for Heatmap node.", __FUNCTION__, __FILE__, __LINE__ - 2);
return;
}
m_map_limits = m_grid->get_used_rect();
m_x_min = m_map_limits.position.x;
m_x_max = m_map_limits.size.x - m_map_limits.position.x;
m_y_min = m_map_limits.position.y;
m_y_max = m_map_limits.size.y - m_map_limits.position.y;
unsigned int highest_index = calculate_point_index(m_map_limits.size - m_map_limits.position);
m_cells_heat.resize(highest_index);
m_cells_heat_cache.resize(highest_index);
for (unsigned int i = 0; i < highest_index; ++i) {
m_cells_heat[i] = -1;
}
find_all_obstacles();
get_tree()->get_root()->find_node("Events", true, false)->connect("player_moved", this, "_on_Events_player_moved");
}
//For every cell in 2D array, check the heat and draw a rectangle colored according to its distance from the goal,
//get the direction it points to, and draw a simple vector line.
void Heatmap::_draw() {
if (!m_draw_debug) {
return;
}
Rect2 tile;
Vector2 cell_size = m_grid->get_cell_size();
tile.set_size(cell_size);
cell_size /= 2;
for (int y = int(m_y_min); y<int(m_y_max); ++y) {
for (int x = int(m_x_min); x<int(m_x_max); ++x) {
Vector2 point = Vector2(float(x), float(y));
Vector2 world_position = m_grid->map_to_world(point);
tile.set_position(world_position);
unsigned int cell_index = calculate_point_index(point);
int heat = m_cells_heat[cell_index];
if (heat == -1) {
continue;
}
float proportion = lerp(0.0f, 1.0f, float(heat) / float(m_max_heat));
draw_rect(tile, Color(1.0f - proportion, 0, proportion, 0.75f), true);
world_position += cell_size;
Vector2 direction = best_direction_for(point, false);
draw_rect(Rect2(world_position.x - 5, world_position.y - 5, 10, 10), Color(0, 1, 0), false);
draw_line(world_position, world_position + (direction * 20), Color(1, 1, 1));
}
}
}
void Heatmap::_process(float delta) {
if (!m_updating) {
return;
}
//Seeing if future::get is ready to deliver data or if the thread is still crunching numbers.
//_Is_Ready is not yet standardized, so we check with as immediate a timeout as we can.
if (m_future.wait_for(std::chrono::seconds(0)) == std::future_status::ready) {
Vector2 player_cell_position = m_future.get();
thread_done(player_cell_position);
}
}
//for the 3x3 grid surrounding the cell, find the one with the least heat that isn't -1 (invalid), and return a vector
//that points in its direction. Note that there could be multiple tiles with the same heat, which could cause
Vector2 Heatmap::best_direction_for(Vector2 t_location, bool t_is_world_location) {
Vector2 point = t_is_world_location ? m_grid->world_to_map(t_location) : t_location;
Vector2 world_location = t_is_world_location ? t_location : m_grid->map_to_world(point);
unsigned int cell_index = calculate_point_index(point);
if (cell_index < 0 || cell_index >= m_cells_heat.size()) {
return (m_last_player_cell_position - world_location).normalized();
}
Vector2 best_neighbor = point;
int best_heat = m_cells_heat[cell_index];
if (best_heat == -1) {
return (m_last_player_cell_position - world_location).normalized();
}
for (int y = 0; y < 3; ++y) {
for (int x = 0; x < 3; ++x) {
Vector2 point_relative = Vector2(point.x + float(x) - 1.0f, point.y + float(y) - 1.0f);
if (is_out_of_bounds(point_relative) || std::find(m_obstacles.begin(), m_obstacles.end(), point_relative) != m_obstacles.end()) {
continue;
}
unsigned int point_relative_index = calculate_point_index(point_relative);
if (point_relative_index == cell_index) {
continue;
}
if (point_relative_index >= 0 && point_relative_index < m_cells_heat.size()) {
int heat = m_cells_heat[point_relative_index];
if (heat == -1) {
continue;
}
if (heat <= best_heat) {
best_heat = heat;
best_neighbor = point_relative;
}
}
}
}
Vector2 world_neighbor = m_grid->map_to_world(best_neighbor);
Vector2 direction = (world_neighbor - world_location).normalized();
if (direction.length_squared() == 0) {
return (m_last_player_cell_position - world_location).normalized();
}
return direction;
}
unsigned int Heatmap::calculate_point_index(Vector2 t_point) {
return int((t_point.x - m_map_limits.position.x) + m_map_limits.size.x * (t_point.y - m_map_limits.position.y));
}
unsigned int Heatmap::calculate_point_index_for_world_position(Vector2 t_world_position) {
return calculate_point_index(m_grid->world_to_map(t_world_position));
}
//Runs through every cell and checks if the autotile bitmask of that cell covers the entire cell.
//If so, it is an obstacle. This does mean that thin corners can seem traversable, even though
//they wouldn't be.
void Heatmap::find_all_obstacles() {
Ref<TileSet> tileset = m_grid->get_tileset();
Array tile_ids = tileset->get_tiles_ids();
//TODO: Account for non-autotile tilesets.
for (int y = m_y_min; y < m_y_max; ++y) {
for (int x = m_x_min; x < m_x_max; ++x) {
int cell_id = m_grid->get_cell(x, y);
if (cell_id == -1 || !tile_ids.has(cell_id)) {
continue;
}
int cell_bitmask = tileset->autotile_get_bitmask(cell_id, m_grid->get_cell_autotile_coord(x, y));
TileSet::BitmaskMode mode = tileset->autotile_get_bitmask_mode(cell_id);
int all_covered = 325; //Bitmask is the sum of TileSet::BitmaskMode enum flags.
if (mode == TileSet::BITMASK_3X3 || mode == TileSet::BITMASK_3X3_MINIMAL) {
all_covered = 495;
}
if (cell_bitmask == all_covered) {
m_obstacles.push_back(Vector2(x, y));
}
}
}
}
//Breadth-first search using a queue.
Vector2 Heatmap::refresh_cells_heat(Vector2 t_cell_position) {
std::deque<HeatCell> queue;
//We begin with 4 cells of goals instead of 1 - this alleviates the problem of multiple cells
//having the same amount of heat.
queue.push_back(HeatCell(Vector2(t_cell_position.x, t_cell_position.y), 0));
queue.push_back(HeatCell(Vector2(t_cell_position.x - 1, t_cell_position.y), 0));
queue.push_back(HeatCell(Vector2(t_cell_position.x - 1, t_cell_position.y - 1), 0));
queue.push_back(HeatCell(Vector2(t_cell_position.x, t_cell_position.y - 1), 0));
m_max_heat_cache = 0;
while (!queue.empty()) {
HeatCell cell = queue.front();
queue.pop_front();
Vector2 position = cell.position;
int layer = cell.layer;
unsigned int index = calculate_point_index(position);
if (index < 0 || index >= m_cells_heat_cache.size()) {
continue;
}
m_cells_heat_cache[index] = layer;
if (layer > m_max_heat_cache) {
m_max_heat_cache = layer;
}
for (int y = 0; y < 3; ++y) {
for (int x = 0; x < 3; ++x) {
Vector2 point = Vector2(position.x + float(x) - 1.0f, position.y + float(y) - 1.0f);
unsigned int cell_index = calculate_point_index(point);
HeatCell new_cell = HeatCell(point, layer + 1);
if ( cell_index != index
&& cell_index >= 0 && cell_index < m_cells_heat_cache.size()
&& m_cells_heat_cache[cell_index] == -1
&& !is_out_of_bounds(point)
&& std::find(queue.begin(), queue.end(), new_cell) == queue.end()
&& std::find(m_obstacles.begin(), m_obstacles.end(), point) == m_obstacles.end()) {
queue.push_back(new_cell);
}
}
}
}
return t_cell_position;
}
//Copies the cached, thread specific versions that were used into those used by the main thread.
void Heatmap::thread_done(Vector2 t_cell_position) {
m_updating = false;
m_max_heat = m_max_heat_cache;
for (int i = 0; i < m_cells_heat.size(); ++i) {
m_cells_heat[i] = m_cells_heat_cache[i];
}
m_last_player_cell_position = t_cell_position;
update();
}
bool Heatmap::is_out_of_bounds(Vector2 t_position) {
return t_position.x < m_x_min || t_position.y < m_y_min
|| t_position.x > m_x_max || t_position.y > m_y_max;
}
void Heatmap::on_Events_player_moved(Node2D* t_player) {
if (m_updating) {
return;
}
Vector2 player_cell_position = m_grid->world_to_map(t_player->get_global_position());
bool out_of_bounds = is_out_of_bounds(player_cell_position);
Vector2 difference = player_cell_position - m_last_player_cell_position;
if (!out_of_bounds && (difference.x != 0 || difference.y != 0)) {
for (int i = 0; i < m_cells_heat_cache.size(); ++i) {
m_cells_heat_cache[i] = -1;
}
m_updating = true;
// Launch an immediate new thread with ::launch::async.
// Without that flag, it waits for a future.get()/wait() before starting work.
m_future = std::async(std::launch::async, &Heatmap::refresh_cells_heat, this, player_cell_position);
}
}
}

86
Native/Heatmap.h Normal file
View file

@ -0,0 +1,86 @@
// Heatmap
// Francois "@Razoric480" Belair
//
// GDNative Godot Class that uses a floodfill algorithm. Radiating out from the player position, it covers the whole tilemap.
// Each step removed adds one to the layer count. This can then be used by agents for quick
// lookup based pathfinding, instead of calculating A-star paths or other, potentially more expensive pathfinding routines.
#ifndef HEATMAP_H
#define HEATMAP_H
#include <Godot.hpp>
#include <Node2D.hpp>
#include <vector>
#include <future>
namespace godot {
//Forward declarations
class TileMap;
// HeatCell. Simple structure used by the `refresh_cells_heat` member function to contain both
// position and current layer in the flood fill search.
struct HeatCell {
HeatCell(Vector2 t_position, int t_layer) {
position = t_position;
layer = t_layer;
}
Vector2 position;
int layer;
bool operator ==(HeatCell const& other) {
return other.layer == layer && other.position == position;
}
bool operator !=(HeatCell const& other) {
return other.layer != layer || other.position != position;
}
};
class Heatmap : public Node2D {
GODOT_CLASS(Heatmap, Node2D)
public:
Heatmap();
~Heatmap();
static void _register_methods();
void _init();
void _ready();
void _draw();
void _process(float delta);
Vector2 best_direction_for(Vector2 t_location, bool t_is_world_location);
unsigned int calculate_point_index(Vector2 t_point);
unsigned int calculate_point_index_for_world_position(Vector2 t_world_position);
private:
void find_all_obstacles();
Vector2 refresh_cells_heat(Vector2 t_cell_position);
bool is_out_of_bounds(Vector2 t_position);
void on_Events_player_moved(Node2D* t_player);
void thread_done(Vector2 t_cell_position);
private:
NodePath m_pathfinding_tilemap;
bool m_draw_debug;
TileMap* m_grid;
Rect2 m_map_limits;
float m_x_min;
float m_x_max;
float m_y_min;
float m_y_max;
int m_max_heat;
int m_max_heat_cache;
bool m_updating;
std::vector<int> m_cells_heat;
std::vector<int> m_cells_heat_cache;
std::vector<Vector2> m_obstacles;
Vector2 m_last_player_cell_position;
std::future<Vector2> m_future;
};
}
#endif /* HEATMAP_H */

25
Native/Heatmap.sln Normal file
View file

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29230.47
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Heatmap", "Heatmap.vcxproj", "{95F50422-B0D5-44A4-8A83-3B56BFC6B7B0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{95F50422-B0D5-44A4-8A83-3B56BFC6B7B0}.Debug|x64.ActiveCfg = Debug|x64
{95F50422-B0D5-44A4-8A83-3B56BFC6B7B0}.Debug|x64.Build.0 = Debug|x64
{95F50422-B0D5-44A4-8A83-3B56BFC6B7B0}.Release|x64.ActiveCfg = Release|x64
{95F50422-B0D5-44A4-8A83-3B56BFC6B7B0}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6B60736E-491D-46DB-B927-593F9A8CEDF2}
EndGlobalSection
EndGlobal

110
Native/Heatmap.vcxproj Normal file
View file

@ -0,0 +1,110 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Heatmap.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="gdlibrary.cpp" />
<ClCompile Include="Heatmap.cpp" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<ProjectGuid>{95F50422-B0D5-44A4-8A83-3B56BFC6B7B0}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>Heatmap</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<IncludePath>E:\Projects\Games\GDQuest\GDNative\godot-cpp\include;E:\Projects\Games\GDQuest\GDNative\godot-cpp\include\core;E:\Projects\Games\GDQuest\GDNative\godot-cpp\include\gen;E:\Projects\Games\GDQuest\GDNative\godot-cpp\godot_headers;$(IncludePath)</IncludePath>
<TargetName>libheatmap</TargetName>
<LibraryPath>E:\Projects\Games\GDQuest\GDNative\godot-cpp\bin;$(LibraryPath)</LibraryPath>
<OutDir>..\game\assets\libraries\win64\</OutDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
<IncludePath>E:\Projects\Games\GDQuest\GDNative\godot-cpp\include;E:\Projects\Games\GDQuest\GDNative\godot-cpp\include\core;E:\Projects\Games\GDQuest\GDNative\godot-cpp\include\gen;E:\Projects\Games\GDQuest\GDNative\godot-cpp\godot_headers;$(IncludePath)</IncludePath>
<LibraryPath>E:\Projects\Games\GDQuest\GDNative\godot-cpp\bin;$(LibraryPath)</LibraryPath>
<OutDir>..\game\assets\libraries\win64\</OutDir>
<TargetName>libheatmap</TargetName>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;HEATMAP_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeaderFile>
</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
<AdditionalDependencies>libgodot-cpp.windows.debug.64.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;HEATMAP_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeaderFile>
</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
<AdditionalDependencies>libgodot-cpp.windows.release.64.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View file

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Heatmap.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="gdlibrary.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Heatmap.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

15
Native/gdlibrary.cpp Normal file
View file

@ -0,0 +1,15 @@
#include "Heatmap.h"
extern "C" void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *o) {
godot::Godot::gdnative_init(o);
}
extern "C" void GDN_EXPORT godot_gdnative_terminate(godot_gdnative_terminate_options *o) {
godot::Godot::gdnative_terminate(o);
}
extern "C" void GDN_EXPORT godot_nativescript_init(void *handle) {
godot::Godot::nativescript_init(handle);
godot::register_class<godot::Heatmap>();
}

View file

@ -0,0 +1,14 @@
[general]
singleton=false
load_once=true
symbol_prefix="godot_"
reloadable=true
[entry]
Windows.64="res://assets/libraries/win64/libheatmap.dll"
[dependencies]
Windows.64=[ ]

View file

@ -59,6 +59,11 @@ _global_script_classes=[ {
"language": "GDScript",
"path": "res://src/Player/FloorDetector.gd"
}, {
"base": "SteeringBehavior2D",
"class": "FollowHeatmapBehavior2D",
"language": "GDScript",
"path": "res://src/AI/Steering/Behaviors/Individual/FollowHeatmapBehavior2D.gd"
}, {
"base": "Reference",
"class": "Hit",
"language": "GDScript",
@ -140,6 +145,7 @@ _global_script_class_icons={
"EvadeBehavior2D": "",
"FleeBehavior2D": "",
"FloorDetector": "",
"FollowHeatmapBehavior2D": "",
"Hit": "",
"Hook": "res://assets/icons/icon_hook.svg",
"HookTarget": "",

8
game/src/AI/Heatmap.gdns Normal file
View file

@ -0,0 +1,8 @@
[gd_resource type="NativeScript" load_steps=2 format=2]
[ext_resource path="res://assets/libraries/heatmap.gdnlib" type="GDNativeLibrary" id=1]
[resource]
resource_name = "heatmap"
class_name = "Heatmap"
library = ExtResource( 1 )

View file

@ -0,0 +1,36 @@
extends SteeringBehavior2D
class_name FollowHeatmapBehavior2D
"""
A 2D steering behavior that expects a heatmap to be somewhere in the scene.
It uses that to figure out where it should be going - from large values in its cell
towards smaller values, with 0 being the goal.
"""
var _heatmap #should be able to do `var _heatmap :Heatmap` here because the GDNS file
# has `Heatmap` in class_name but it doesn't work and I get type not defined errors.
# Bug, or am I being dumb?
var _last_point_index: int = INF
var _last_velocity: = Vector2(0, 0)
func _ready() -> void:
_heatmap = get_tree().root.find_node("Heatmap", true, false)
assert _heatmap
func _calculate_steering_internal(steering: SteeringMotion2D) -> SteeringMotion2D:
var actor = get_actor()
var point_index: int = _heatmap.calculate_point_index_for_world_position(actor.global_position)
if point_index != _last_point_index:
_last_point_index = point_index
var to_target: Vector2 = _heatmap.best_direction_for(actor.global_position, true)
steering.velocity = to_target * controller.max_acceleration
_last_velocity = steering.velocity
else:
steering.velocity = _last_velocity
steering.angular_velocity = 0
return steering

View file

@ -0,0 +1,16 @@
extends Node2D
"""
Basic controller for a node2D - its behavior checks the heatmap and points
its directional vector towards the less heat (the 'goal')
"""
onready var behavior: FollowHeatmapBehavior2D = $BehaviorController2D/FollowHeatmapBehavior2D
var _motion: SteeringMotion2D = SteeringMotion2D.new()
var speed: float = 250
func _physics_process(delta: float) -> void:
var desired_velocity: = behavior.calculate_steering(_motion)
position += _motion.velocity.normalized() * speed * delta

View file

@ -0,0 +1,23 @@
[gd_scene load_steps=5 format=2]
[ext_resource path="res://src/AI/SwarmerEnemy.gd" type="Script" id=1]
[ext_resource path="res://src/Player/Rectangle.gd" type="Script" id=2]
[ext_resource path="res://src/AI/Steering/BehaviorController2D.gd" type="Script" id=3]
[ext_resource path="res://src/AI/Steering/Behaviors/Individual/FollowHeatmapBehavior2D.gd" type="Script" id=4]
[node name="SwarmerEnemy" type="Node2D"]
script = ExtResource( 1 )
[node name="Body" type="Node2D" parent="."]
script = ExtResource( 2 )
size = Vector2( 24, 24 )
outline = Vector2( 6, 6 )
color_fill = Color( 0.580392, 0.294118, 0.737255, 1 )
color_outline = Color( 0.27451, 0.12549, 0.807843, 1 )
[node name="BehaviorController2D" type="Node" parent="."]
script = ExtResource( 3 )
actor_path = NodePath("..")
[node name="FollowHeatmapBehavior2D" type="Node" parent="BehaviorController2D"]
script = ExtResource( 4 )

View file

@ -0,0 +1,30 @@
extends Node2D
"""
Spawns a number of swarmer enemies in the scene, who use the heatmap behavior
to chase the player for economical pathfinding.
"""
export var spawner_count: = 50
export var spawn_per_frame: = 10
export var swarmer: PackedScene
export var spawn_radius: = 200
export var minimum_speed: float = 200
export var maximum_speed: float = 300
func _ready():
randomize()
var r_squared: = spawn_radius*spawn_radius
for i in range(spawner_count):
if i % spawn_per_frame:
yield(get_tree(), "idle_frame")
var x: = rand_range(-spawn_radius, spawn_radius)
var y: = rand_range(-1, 1) * sqrt(r_squared-x*x)
var instance: = swarmer.instance()
instance.set_name("Swarmer%s"% i)
add_child(instance)
instance.speed = rand_range(minimum_speed, maximum_speed)
instance.global_position = global_position + Vector2(x,y)

View file

@ -0,0 +1,8 @@
[gd_scene load_steps=3 format=2]
[ext_resource path="res://src/AI/SwarmerSpawner.gd" type="Script" id=1]
[ext_resource path="res://src/AI/SwarmerEnemy.tscn" type="PackedScene" id=2]
[node name="SwarmerSpawner" type="Node2D"]
script = ExtResource( 1 )
swarmer = ExtResource( 2 )

File diff suppressed because one or more lines are too long