Skip to content

Commit

Permalink
refactroing, add castmovementray
Browse files Browse the repository at this point in the history
  • Loading branch information
Jnnshschl committed Aug 1, 2021
1 parent ba6af3d commit 816160e
Show file tree
Hide file tree
Showing 10 changed files with 287 additions and 285 deletions.
5 changes: 5 additions & 0 deletions AmeisenNavigation.Server/AmeisenNavigation.Server.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpplatest</LanguageStandard>
<LanguageStandard_C>stdc17</LanguageStandard_C>
<EnableEnhancedInstructionSet>StreamingSIMDExtensions2</EnableEnhancedInstructionSet>
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
<OmitFramePointers>true</OmitFramePointers>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -177,6 +181,7 @@
<ClInclude Include="resource.h" />
<ClInclude Include="src\Config\Config.hpp" />
<ClInclude Include="src\Enums\EMessageType.hpp" />
<ClInclude Include="src\Logging\AmeisenLogger.hpp" />
<ClInclude Include="src\Main.hpp" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
<ClInclude Include="resource.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="src\Logging\AmeisenLogger.hpp">
<Filter>Headerdateien</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="AmeisenNavigation.Server.rc">
Expand Down
53 changes: 53 additions & 0 deletions AmeisenNavigation.Server/src/Logging/AmeisenLogger.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once

#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>

#if defined WIN32 || defined WIN64
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <Windows.h>

constexpr auto CONSOLE_COLOR_BLUE = 3;
constexpr auto CONSOLE_COLOR_GRAY = 8;
constexpr auto CONSOLE_COLOR_GREEN = 10;
constexpr auto CONSOLE_COLOR_RED = 12;
constexpr auto CONSOLE_COLOR_WHITE = 15;
constexpr auto CONSOLE_COLOR_YELLOW = 14;
#endif

#define LogI(...) Log(__FUNCTION__, CONSOLE_COLOR_BLUE, CONSOLE_COLOR_WHITE, __VA_ARGS__)
#define LogD(...) Log(__FUNCTION__, CONSOLE_COLOR_GRAY, CONSOLE_COLOR_WHITE, __VA_ARGS__)
#define LogW(...) Log(__FUNCTION__, CONSOLE_COLOR_YELLOW, CONSOLE_COLOR_WHITE, __VA_ARGS__)
#define LogE(...) Log(__FUNCTION__, CONSOLE_COLOR_RED, CONSOLE_COLOR_WHITE, __VA_ARGS__)
#define LogS(...) Log(__FUNCTION__, CONSOLE_COLOR_GREEN, CONSOLE_COLOR_WHITE, __VA_ARGS__)

static bool ConsoleOpened;
static _iobuf* StdoutHandle;
static std::ofstream LogFileStream;

template<typename ...Args>
constexpr void Log(const std::string& tag, int color, int colorSecond, Args&& ...args)
{
static std::string Delimiter = " >> ";

#if defined WIN32 || defined WIN64
static void* ConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
#endif

std::cout << std::setw(8) << tag;

#if defined WIN32 || defined WIN64
SetConsoleTextAttribute(ConsoleHandle, color);
#endif

std::cout << Delimiter;

#if defined WIN32 || defined WIN64
SetConsoleTextAttribute(ConsoleHandle, colorSecond);
#endif

(std::cout << ... << args) << std::endl;
}
107 changes: 71 additions & 36 deletions AmeisenNavigation.Server/src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@
int main(int argc, const char* argv[])
{
#if defined(WIN32) || defined(WIN64)
SetConsoleTitle(L"AmeisenNavigation Server");
SetConsoleTitle(L"AmeisenNavigation Server");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
#endif

ChangeOutputColor(COLOR_WHITE);

std::cout << " ___ _ _ __ " << std::endl
<< " / | ____ ___ ___ (_)_______ ____ / | / /___ __ __" << std::endl
<< " / /| | / __ `__ \\/ _ \\/ / ___/ _ \\/ __ \\/ |/ / __ `/ | / /" << std::endl
<< " / ___ |/ / / / / / __/ (__ ) __/ / / / /| / /_/ /| |/ / " << std::endl
<< "/_/ |_/_/ /_/ /_/\\___/_/____/\\___/_/ /_/_/ |_/\\__,_/ |___/ " << std::endl
<< " Server " << AMEISENNAV_VERSION << std::endl << std::endl;
std::cout << " ___ _ _ __ " << std::endl
<< " / | ____ ___ ___ (_)_______ ____ / | / /___ __ __" << std::endl
<< " / /| | / __ `__ \\/ _ \\/ / ___/ _ \\/ __ \\/ |/ / __ `/ | / /" << std::endl
<< " / ___ |/ / / / / / __/ (__ ) __/ / / / /| / /_/ /| |/ / " << std::endl
<< " /_/ |_/_/ /_/ /_/\\___/_/____/\\___/_/ /_/_/ |_/\\__,_/ |___/ " << std::endl
<< " Server " << AMEISENNAV_VERSION << std::endl << std::endl;

std::filesystem::path configPath(std::filesystem::path(argv[0]).parent_path().string() + "\\config.cfg");

Config = new AmeisenNavConfig();

if (argc > 1)
Expand All @@ -25,66 +23,69 @@ int main(int argc, const char* argv[])

if (!std::filesystem::exists(configPath))
{
ChangeOutputColor(COLOR_RED);
std::cout << ">> Configfile does not exists: \"" << argv[1] << "\"" << std::endl;
LogE("Configfile does not exists: \"", argv[1], "\"");
std::cin.get();
return 1;
}
}

if (std::filesystem::exists(configPath))
{
ChangeOutputColor(COLOR_YELLOW);
std::cout << ">> Loaded Configfile: \"" << configPath.string() << "\"" << std::endl;
ChangeOutputColor(COLOR_WHITE);
Config->Load(configPath);
LogI("Loaded Configfile: \"", configPath.string(), "\"");

// directly save again to add new entries to it
Config->Save(configPath);
}
else
{
std::cout << ">> Created default Configfile: \"" << configPath.string() << "\"" << std::endl;
Config->Save(configPath);

LogI("Created default Configfile: \"", configPath.string(), "\"");
LogI("Edit it and restart the server, press any key to exit...");
std::cin.get();
return 1;
}

// validate config
if (!std::filesystem::exists(Config->mmapsPath))
{
ChangeOutputColor(COLOR_RED);
std::cout << ">> MMAPS folder does not exists: \"" << Config->mmapsPath << "\"" << std::endl;
LogE("MMAPS folder does not exists: \"", Config->mmapsPath, "\"");
std::cin.get();
return 1;
}

if (Config->maxPointPath <= 0)
{
ChangeOutputColor(COLOR_RED);
std::cout << ">> iMaxPointPath has to be a value > 0" << std::endl;
LogE("iMaxPointPath has to be a value > 0");
std::cin.get();
return 1;
}

if (Config->maxPolyPath <= 0)
{
ChangeOutputColor(COLOR_RED);
std::cout << ">> iMaxPolyPath has to be a value > 0" << std::endl;
LogE("iMaxPolyPath has to be a value > 0");
std::cin.get();
return 1;
}

if (Config->port <= 0 || Config->port > 65535)
{
ChangeOutputColor(COLOR_RED);
std::cout << ">> iMaxPolyPath has to be a value bewtween 1 and 65535" << std::endl;
LogE("iMaxPolyPath has to be a value bewtween 1 and 65535");
std::cin.get();
return 1;
}

// set ctrl+c handler to cleanup stuff when we exit
if (!SetConsoleCtrlHandler(SigIntHandler, 1))
{
ChangeOutputColor(COLOR_RED);
std::cout << ">> SetConsoleCtrlHandler() failed: " << GetLastError() << std::endl;
LogE("SetConsoleCtrlHandler() failed: ", GetLastError());
std::cin.get();
return 1;
}

PathBuffer = new Vector3[Config->maxPointPath];

Nav = new AmeisenNavigation(Config->mmapsPath, Config->maxPolyPath, Config->maxPointPath);
Server = new AnTcpServer(Config->ip, Config->port);

Expand All @@ -93,21 +94,22 @@ int main(int argc, const char* argv[])
Server->AddCallback(static_cast<char>(MessageType::RANDOM_POINT_AROUND), RandomPointAroundCallback);
Server->AddCallback(static_cast<char>(MessageType::MOVE_ALONG_SURFACE), MoveAlongSurfaceCallback);

std::cout << ">> Starting server on: " << Config->ip << ":" << std::to_string(Config->port) << std::endl;
LogS("Starting server on: ", Config->ip, ":", std::to_string(Config->port));
Server->Run();

std::cout << ">> Stopped server..." << std::endl;
LogI("Stopped server...");

delete Config;
delete Nav;
delete Server;
delete[] PathBuffer;
}

int __stdcall SigIntHandler(unsigned long signal)
{
if (signal == CTRL_C_EVENT || signal == CTRL_CLOSE_EVENT)
{
std::cout << ">> Received CTRL-C or CTRL-EXIT, stopping server..." << std::endl;
LogI("Received CTRL-C or CTRL-EXIT, stopping server...");
Server->Stop();
}

Expand All @@ -119,46 +121,52 @@ void PathCallback(ClientHandler* handler, char type, const void* data, int size)
const PathRequestData request = *reinterpret_cast<const PathRequestData*>(data);

int pathSize = 0;
Vector3* path = new Vector3[Config->maxPointPath];

if (Nav->GetPath(request.mapId, request.start, request.end, path, &pathSize))
QueryLock.lock();

if (Nav->GetPath(request.mapId, request.start, request.end, PathBuffer, &pathSize))
{
QueryLock.unlock();

if ((request.flags & static_cast<int>(PathRequestFlags::CATMULLROM)) && pathSize > 3)
{
std::vector<Vector3> output;
SmoothPathCatmullRom(path, pathSize, &output, Config->catmullRomSplinePoints, Config->catmullRomSplineAlpha);
SmoothPathCatmullRom(PathBuffer, pathSize, &output, Config->catmullRomSplinePoints, Config->catmullRomSplineAlpha);
handler->SendData(type, output.data(), sizeof(Vector3) * output.size());
}
else if ((request.flags & static_cast<int>(PathRequestFlags::CHAIKIN)) && pathSize > 2)
{
std::vector<Vector3> output;
SmoothPathChaikinCurve(path, pathSize, &output);
SmoothPathChaikinCurve(PathBuffer, pathSize, &output);
handler->SendData(type, output.data(), sizeof(Vector3) * output.size());
}
else
{
handler->SendData(type, path, sizeof(Vector3) * pathSize);
handler->SendData(type, PathBuffer, sizeof(Vector3) * pathSize);
}
}
else
{
QueryLock.unlock();
handler->SendDataPtr(type, &Vector3Zero);
}

delete[] path;
}

void RandomPointCallback(ClientHandler* handler, char type, const void* data, int size)
{
const int mapId = *reinterpret_cast<const int*>(data);
Vector3 point;

QueryLock.lock();

if (Nav->GetRandomPoint(mapId, &point))
{
QueryLock.unlock();
handler->SendDataPtr(type, &point);
}
else
{
QueryLock.unlock();
handler->SendDataPtr(type, &Vector3Zero);
}
}
Expand All @@ -168,12 +176,16 @@ void RandomPointAroundCallback(ClientHandler* handler, char type, const void* da
const RandomPointAroundData request = *reinterpret_cast<const RandomPointAroundData*>(data);
Vector3 position;

QueryLock.lock();

if (Nav->GetRandomPointAround(request.mapId, request.start, request.radius, &position))
{
QueryLock.unlock();
handler->SendDataPtr(type, &position);
}
else
{
QueryLock.unlock();
handler->SendDataPtr(type, &Vector3Zero);
}
}
Expand All @@ -183,12 +195,35 @@ void MoveAlongSurfaceCallback(ClientHandler* handler, char type, const void* dat
const MoveRequestData request = *reinterpret_cast<const MoveRequestData*>(data);
Vector3 position;

QueryLock.lock();

if (Nav->MoveAlongSurface(request.mapId, request.start, request.end, &position))
{
QueryLock.unlock();
handler->SendDataPtr(type, &position);
}
else
{
QueryLock.unlock();
handler->SendDataPtr(type, &Vector3Zero);
}
}

void CastRayCallback(ClientHandler* handler, char type, const void* data, int size)
{
const CastRayData request = *reinterpret_cast<const CastRayData*>(data);
dtRaycastHit hit;

QueryLock.lock();

if (Nav->CastMovementRay(request.mapId, request.start, request.end, &hit))
{
QueryLock.unlock();
handler->SendDataPtr(type, &request.end);
}
else
{
QueryLock.unlock();
handler->SendDataPtr(type, &Vector3Zero);
}
}
Loading

0 comments on commit 816160e

Please sign in to comment.