Skip to content

Commit

Permalink
Quest module and pathing, but dont use it because its fucked. plz sen…
Browse files Browse the repository at this point in the history
…d help.
  • Loading branch information
3vcloud committed Dec 8, 2023
1 parent 1b5d3d5 commit 768676a
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Dependencies/GWCA
Submodule GWCA updated from 654eef to c9c063
31 changes: 23 additions & 8 deletions GWToolboxdll/Modules/QuestModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace {
std::vector<CustomRenderer::CustomLine*> minimap_lines;
GW::GamePos original_quest_marker;
GW::GamePos calculated_from;
clock_t calculated_at = 0;
uint32_t current_waypoint = 0;
GW::Constants::QuestID quest_id;
bool calculating = false;
Expand Down Expand Up @@ -68,8 +69,7 @@ namespace {
if (original_quest_marker.x == INFINITY)
return;
calculated_from = from;
calculating = true;
PathfindingWindow::CalculatePath(calculated_from, original_quest_marker, OnQuestPathRecalculated, this);
calculating = PathfindingWindow::CalculatePath(calculated_from, original_quest_marker, OnQuestPathRecalculated, (void*)quest_id);
}
bool Update(const GW::GamePos& from) {
const auto quest = GetQuest();
Expand All @@ -80,14 +80,26 @@ namespace {
if (calculating) {
return false;
}
if (!calculated_at) {
Recalculate(from);
return false;
}
constexpr float dist_check = 1500.f * 1500.f;
if (GetSquareDistance(from, calculated_from) > 1500.f * 1500.f) {
Recalculate(from);
return false;
}
const auto wp = CurrentWaypoint();
if (wp && current_waypoint < waypoints.size() - 1 && GetSquareDistance(from, *wp) < dist_check) {
uint32_t original_waypoint = current_waypoint;

if (!waypoints.size())
return false;
while (current_waypoint < waypoints.size() - 1 && GetSquareDistance(from, waypoints[current_waypoint + 1]) < GetSquareDistance(from, waypoints[current_waypoint])) {
current_waypoint++;
}
while(current_waypoint < waypoints.size() && GetSquareDistance(from, waypoints[current_waypoint]) < dist_check) {
current_waypoint++;
}
if (original_waypoint != current_waypoint) {
calculated_from = from;
UpdateUI();
}
Expand Down Expand Up @@ -117,9 +129,11 @@ namespace {
delete found->second;
calculated_quest_paths.erase(found);
}
CalculatedQuestPath* GetCalculatedQuestPath(GW::Constants::QuestID quest_id) {
CalculatedQuestPath* GetCalculatedQuestPath(GW::Constants::QuestID quest_id, bool create_if_not_found = true) {
const auto found = calculated_quest_paths.find(quest_id);
if (found != calculated_quest_paths.end()) return found->second;
if (!create_if_not_found)
return nullptr;
auto cqp = new CalculatedQuestPath(quest_id);
calculated_quest_paths[quest_id] = cqp;
return cqp;
Expand Down Expand Up @@ -173,7 +187,8 @@ namespace {

// Called by PathfindingWindow when a path has been calculated. Should be on the main loop.
void OnQuestPathRecalculated(const std::vector<GW::GamePos>& waypoints, void* args) {
CalculatedQuestPath* cqp = (CalculatedQuestPath*)args;
auto cqp = GetCalculatedQuestPath(*(GW::Constants::QuestID*)&args, false);
if (!cqp) return;
ASSERT(cqp->calculating);

cqp->current_waypoint = 0;
Expand All @@ -183,7 +198,7 @@ namespace {
// Waypoint array is in descending distance, flip it
std::reverse(cqp->waypoints.begin(), cqp->waypoints.end());
}

cqp->calculated_at = TIMER_INIT();
cqp->calculating = false;
cqp->UpdateUI();
}
Expand Down Expand Up @@ -213,7 +228,7 @@ void QuestModule::Initialize()
for (auto ui_message : ui_messages) {
// Post callbacks, non blocking
(ui_message);
//GW::UI::RegisterUIMessageCallback(&ui_message_entry, ui_message, OnGWQuestMarkerUpdated, 0x4000);
GW::UI::RegisterUIMessageCallback(&ui_message_entry, ui_message, OnGWQuestMarkerUpdated, 0x4000);
}
}
void QuestModule::SignalTerminate() {
Expand Down
5 changes: 2 additions & 3 deletions GWToolboxdll/Modules/ToolboxSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ namespace {
MouseFix::Instance(),
KeyboardLanguageFix::Instance(),
ZrawDeepModule::Instance(),
GuildWarsSettingsModule::Instance(),
QuestModule::Instance()
GuildWarsSettingsModule::Instance()
//QuestModule::Instance()
};

std::vector<WidgetToggle> optional_widgets = {
Expand Down Expand Up @@ -214,7 +214,6 @@ void ToolboxSettings::LoadModules(ToolboxIni* ini)
GWToolbox::ToggleModule(SkillListingWindow::Instance());

#endif
GWToolbox::ToggleModule(PathfindingWindow::Instance());
for (const auto& m : optional_modules) {
GWToolbox::ToggleModule(*m.toolbox_module, m.enabled);
}
Expand Down
27 changes: 25 additions & 2 deletions GWToolboxdll/Windows/PathfindingWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ namespace {
size_t draw_pos = 0;
clock_t last_draw = 0;

volatile bool pending_terminate = false;
volatile bool pending_worker_task = false;

bool pending_redraw = false;
clock_t pending_undraw = 0;

Expand Down Expand Up @@ -119,6 +122,10 @@ namespace {

}
}
bool PathfindingWindow::ReadyForPathing() {
const auto m = GetMilepathForCurrentMap();
return m && m->ready();
}
void PathfindingWindow::Draw(IDirect3DDevice9*)
{
if (!visible) {
Expand Down Expand Up @@ -217,32 +224,46 @@ void PathfindingWindow::Draw(IDirect3DDevice9*)
void PathfindingWindow::SignalTerminate()
{
ToolboxWindow::SignalTerminate();
pending_terminate = true;
GW::UI::RemoveUIMessageCallback(&gw_ui_hookentry);
for (const auto m : mile_paths_by_map_file_id) {
m.second->stopProcessing();
}
}
bool PathfindingWindow::CanTerminate()
{
if (pending_worker_task)
return false;
for (const auto m : mile_paths_by_map_file_id) {
if (m.second->isProcessing())
return false;
}
return true;
}
void PathfindingWindow::CalculatePath(const GW::GamePos& from, const GW::GamePos& to, CalculatedCallback callback, void* args)
bool PathfindingWindow::CalculatePath(const GW::GamePos& from, const GW::GamePos& to, CalculatedCallback callback, void* args)
{
if (pending_terminate)
return false;

if (!ReadyForPathing())
return false;
GW::GamePos* from_cpy = new GW::GamePos();
memcpy(from_cpy, &from, sizeof(from));
GW::GamePos* to_cpy = new GW::GamePos();
memcpy(to_cpy, &to, sizeof(to));

pending_worker_task = true;

Resources::EnqueueWorkerTask([from_cpy, to_cpy, callback, args ]() {
const auto milepath = GetMilepathForCurrentMap();
Pathing::MilePath* milepath = nullptr;
Pathing::AStar* tmpAstar = nullptr;
Pathing::Error res = Pathing::Error::OK;
std::vector<GW::GamePos>* waypoints = new std::vector<GW::GamePos>();
if (pending_terminate) {
goto trigger_callback;
}

milepath = GetMilepathForCurrentMap();
if (!(milepath && milepath->ready())) {
goto trigger_callback;
}
Expand All @@ -269,7 +290,9 @@ void PathfindingWindow::CalculatePath(const GW::GamePos& from, const GW::GamePos
delete waypoints;

});
pending_worker_task = false;
});
return true;
}
void PathfindingWindow::Terminate()
{
Expand Down
6 changes: 4 additions & 2 deletions GWToolboxdll/Windows/PathfindingWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ class PathfindingWindow : public ToolboxWindow {
void SignalTerminate() override;
bool CanTerminate() override;
void Terminate() override;

static void CalculatePath(const GW::GamePos& from, const GW::GamePos& to, CalculatedCallback callback, void* args = nullptr);
// False if still calculating current map
static bool ReadyForPathing();
// False if still calculating current map
static bool CalculatePath(const GW::GamePos& from, const GW::GamePos& to, CalculatedCallback callback, void* args = nullptr);

private:
GW::GamePos m_saved_pos;
Expand Down
46 changes: 42 additions & 4 deletions GWToolboxdll/Windows/Pathing.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <thread>
#include <algorithm>
#include <GWCA/Managers/MapMgr.h>
#include <GWCA/Managers/GameThreadMgr.h>

#include <GWCA/Constants/Constants.h>
#include <GWCA/Constants/Maps.h>
#include <GWCA/Context/MapContext.h>
Expand All @@ -10,6 +12,39 @@
#include "MathUtility.h"
#include "Pathing.h"

namespace {



// Grab a copy of map_context->sub1->pathing_map_block for processing on a different thread - Blocks until copy is complete
Pathing::Error CopyPathingMapBlocks(std::vector<uint32_t>& block) {
auto block_pt = &block;
volatile Pathing::Error res = Pathing::Error::Unknown;
std::mutex mutex;
auto res_pt = &res;
// Enqueue
GW::GameThread::Enqueue([&block_pt, res_pt, &mutex]() {
const std::lock_guard<std::mutex> lock(mutex);
GW::MapContext* mapContext = GW::GetMapContext();
if (!mapContext) {
*res_pt = Pathing::Error::InvalidMapContext;
return;
}
GW::Array<uint32_t>& block = mapContext->sub1->pathing_map_block;
if(block.m_size)
block_pt->assign(block.m_buffer, block.m_buffer + block.m_size);
*res_pt = Pathing::Error::OK;
});
// Wait
do {
const std::lock_guard<std::mutex> lock(mutex);
if (res != Pathing::Error::Unknown)
break;
} while (true);
return res;
}
}

namespace Pathing {

using namespace GW;
Expand Down Expand Up @@ -766,6 +801,12 @@ namespace Pathing {
}

Error AStar::search(const GamePos &start_pos, const GamePos &goal_pos) {

std::vector<uint32_t> block;
Pathing::Error res = CopyPathingMapBlocks(block);

if (res != Pathing::Error::OK)
return res;
MilePath::point::Id point_id = m_mp->m_points.size();
MilePath::point start;
bool new_start = false;
Expand Down Expand Up @@ -794,10 +835,6 @@ namespace Pathing {
new_goal = true;
}

GW::MapContext* mapContext = GW::GetMapContext();
if (!mapContext) return Error::InvalidMapContext;
GW::Array<uint32_t>& block = mapContext->sub1->pathing_map_block;

{
std::vector<const AABB *> open;
std::vector<bool> visited;
Expand All @@ -815,6 +852,7 @@ namespace Pathing {

volatile clock_t start_timestamp = clock();

//@Cleanup: Maybe I'm not using milepath for its intended purpose, but this function will ALWAYS add more points to the graph!!
if (new_start) {
m_mp->m_points.push_back(start);
insertPointIntoVisGraph(start);
Expand Down
4 changes: 3 additions & 1 deletion GWToolboxdll/Windows/Pathing.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ namespace Pathing {

enum class Error : uint32_t {
OK,
Unknown,
FailedToFindGoalBox,
FailedToFindStartBox,
FailedToFinializePath,
InvalidMapContext,
BuildPathLengthExceeded
BuildPathLengthExceeded,
FailedToGetPathingMapBlock
};


Expand Down

0 comments on commit 768676a

Please sign in to comment.