From 7f4b6b4f0cb276a7e0868c7e97d862b1feb468d3 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Tue, 14 May 2024 11:41:50 -0400 Subject: Hovered connections on subway map! --- CMakeLists.txt | 1 + src/ap_state.cpp | 21 ++++---- src/game_data.cpp | 25 +++++++++- src/game_data.h | 4 ++ src/network_set.cpp | 68 ++++++++++++++++++++++++++ src/network_set.h | 28 +++++++++++ src/subway_map.cpp | 129 +++++++++++++++++++++++++++++++++++++++++++++++--- src/subway_map.h | 12 +++++ src/tracker_frame.cpp | 13 +++++ src/tracker_frame.h | 3 ++ 10 files changed, 289 insertions(+), 15 deletions(-) create mode 100644 src/network_set.cpp create mode 100644 src/network_set.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d6170a1..6dc2f4c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,6 +45,7 @@ add_executable(lingo_ap_tracker "src/settings_dialog.cpp" "src/global.cpp" "src/subway_map.cpp" + "src/network_set.cpp" "vendor/whereami/whereami.c" ) set_property(TARGET lingo_ap_tracker PROPERTY CXX_STANDARD 20) diff --git a/src/ap_state.cpp b/src/ap_state.cpp index 8feb78b..b057beb 100644 --- a/src/ap_state.cpp +++ b/src/ap_state.cpp @@ -173,7 +173,7 @@ struct APState { TrackerLog("Location: " + std::to_string(location_id)); } - RefreshTracker(); + RefreshTracker(false); }); apclient->set_slot_disconnected_handler([this]() { @@ -197,7 +197,7 @@ struct APState { TrackerLog("Item: " + std::to_string(item.item)); } - RefreshTracker(); + RefreshTracker(false); }); apclient->set_retrieved_handler( @@ -206,14 +206,14 @@ struct APState { HandleDataStorage(key, value); } - RefreshTracker(); + RefreshTracker(false); }); apclient->set_set_reply_handler([this](const std::string& key, const nlohmann::json& value, const nlohmann::json&) { HandleDataStorage(key, value); - RefreshTracker(); + RefreshTracker(false); }); apclient->set_slot_connected_handler([this]( @@ -271,7 +271,7 @@ struct APState { connected = true; has_connection_result = true; - RefreshTracker(); + RefreshTracker(true); std::list corrected_keys; for (const std::string& key : tracked_data_storage_keys) { @@ -353,7 +353,7 @@ struct APState { } if (connected) { - RefreshTracker(); + RefreshTracker(false); } else { client_active = false; } @@ -407,11 +407,16 @@ struct APState { return data_storage.count(key) && std::any_cast(data_storage.at(key)); } - void RefreshTracker() { + void RefreshTracker(bool reset) { TrackerLog("Refreshing display..."); RecalculateReachability(); - tracker_frame->UpdateIndicators(); + + if (reset) { + tracker_frame->ResetIndicators(); + } else { + tracker_frame->UpdateIndicators(); + } } int64_t GetItemId(const std::string& item_name) { diff --git a/src/game_data.cpp b/src/game_data.cpp index 4348967..74f872c 100644 --- a/src/game_data.cpp +++ b/src/game_data.cpp @@ -62,6 +62,9 @@ struct GameData { std::vector sunwarp_doors_; + std::map subway_item_by_painting_; + std::map subway_item_by_sunwarp_; + bool loaded_area_data_ = false; std::set malconfigured_areas_; @@ -624,7 +627,10 @@ struct GameData { if (subway_it["paintings"]) { for (const auto &painting_it : subway_it["paintings"]) { - subway_item.paintings.push_back(painting_it.as()); + std::string painting_id = painting_it.as(); + + subway_item.paintings.push_back(painting_id); + subway_item_by_painting_[painting_id] = subway_item.id; } } @@ -649,6 +655,11 @@ struct GameData { } subway_item.sunwarp = sunwarp; + + subway_item_by_sunwarp_[sunwarp] = subway_item.id; + + subway_item.door = + AddOrGetDoor("Sunwarps", std::to_string(sunwarp.dots) + " Sunwarp"); } if (subway_it["special"]) { @@ -715,6 +726,10 @@ GameData &GetState() { } // namespace +bool SubwaySunwarp::operator<(const SubwaySunwarp& rhs) const { + return std::tie(dots, type) < std::tie(rhs.dots, rhs.type); +} + const std::vector &GD_GetMapAreas() { return GetState().map_areas_; } const MapArea &GD_GetMapArea(int id) { return GetState().map_areas_.at(id); } @@ -764,3 +779,11 @@ const std::vector &GD_GetSubwayItems() { const SubwayItem &GD_GetSubwayItem(int id) { return GetState().subway_items_.at(id); } + +int GD_GetSubwayItemForPainting(const std::string& painting_id) { + return GetState().subway_item_by_painting_.at(painting_id); +} + +int GD_GetSubwayItemForSunwarp(const SubwaySunwarp &sunwarp) { + return GetState().subway_item_by_sunwarp_.at(sunwarp); +} diff --git a/src/game_data.h b/src/game_data.h index 37d1eb3..3afaec3 100644 --- a/src/game_data.h +++ b/src/game_data.h @@ -128,6 +128,8 @@ enum class SubwaySunwarpType { struct SubwaySunwarp { int dots; SubwaySunwarpType type; + + bool operator<(const SubwaySunwarp& rhs) const; }; struct SubwayItem { @@ -156,5 +158,7 @@ const std::vector& GD_GetSunwarpDoors(); int GD_GetRoomForSunwarp(int index); const std::vector& GD_GetSubwayItems(); const SubwayItem& GD_GetSubwayItem(int id); +int GD_GetSubwayItemForPainting(const std::string& painting_id); +int GD_GetSubwayItemForSunwarp(const SubwaySunwarp& sunwarp); #endif /* end of include guard: GAME_DATA_H_9C42AC51 */ diff --git a/src/network_set.cpp b/src/network_set.cpp new file mode 100644 index 0000000..3238dcd --- /dev/null +++ b/src/network_set.cpp @@ -0,0 +1,68 @@ +#include "network_set.h" + +void NetworkSet::Clear() { + networks_.clear(); + network_by_item_.clear(); +} + +int NetworkSet::AddLink(int id1, int id2) { + if (id2 > id1) { + // Make sure id1 < id2 + std::swap(id1, id2); + } + + if (network_by_item_.count(id1)) { + if (network_by_item_.count(id2)) { + int network_id1 = network_by_item_[id1]; + int network_id2 = network_by_item_[id2]; + + networks_[network_id1].emplace(id1, id2); + + if (network_id1 != network_id2) { + for (const auto& [other_id1, other_id2] : networks_[network_id2]) { + network_by_item_[other_id1] = network_id1; + network_by_item_[other_id2] = network_id1; + } + + networks_[network_id1].merge(networks_[network_id2]); + networks_[network_id2].clear(); + } + + return network_id1; + } else { + int network_id = network_by_item_[id1]; + network_by_item_[id2] = network_id; + networks_[network_id].emplace(id1, id2); + + return network_id; + } + } else { + if (network_by_item_.count(id2)) { + int network_id = network_by_item_[id2]; + network_by_item_[id1] = network_id; + networks_[network_id].emplace(id1, id2); + + return network_id; + } else { + int network_id = networks_.size(); + network_by_item_[id1] = network_id; + network_by_item_[id2] = network_id; + networks_.emplace_back(); + networks_[network_id] = {{id1, id2}}; + + return network_id; + } + } +} + +bool NetworkSet::IsItemInNetwork(int id) const { + return network_by_item_.count(id); +} + +int NetworkSet::GetNetworkWithItem(int id) const { + return network_by_item_.at(id); +} + +const std::set>& NetworkSet::GetNetworkGraph(int id) const { + return networks_.at(id); +} diff --git a/src/network_set.h b/src/network_set.h new file mode 100644 index 0000000..e0ef043 --- /dev/null +++ b/src/network_set.h @@ -0,0 +1,28 @@ +#ifndef NETWORK_SET_H_3036B8E3 +#define NETWORK_SET_H_3036B8E3 + +#include +#include +#include +#include +#include + +class NetworkSet { + public: + void Clear(); + + int AddLink(int id1, int id2); + + bool IsItemInNetwork(int id) const; + + int GetNetworkWithItem(int id) const; + + const std::set>& GetNetworkGraph(int id) const; + + private: + + std::vector>> networks_; + std::map network_by_item_; +}; + +#endif /* end of include guard: NETWORK_SET_H_3036B8E3 */ diff --git a/src/subway_map.cpp b/src/subway_map.cpp index f857270..a03f0d8 100644 --- a/src/subway_map.cpp +++ b/src/subway_map.cpp @@ -2,6 +2,9 @@ #include +#include + +#include "ap_state.h" #include "game_data.h" #include "global.h" #include "tracker_state.h" @@ -42,10 +45,61 @@ SubwayMap::SubwayMap(wxWindow *parent) : wxPanel(parent, wxID_ANY) { Bind(wxEVT_MOTION, &SubwayMap::OnMouseMove, this); } +void SubwayMap::OnConnect() { + networks_.Clear(); + + std::map> tagged; + for (const SubwayItem &subway_item : GD_GetSubwayItems()) { + if (AP_IsPaintingShuffle() && !subway_item.paintings.empty()) { + continue; + } + + for (const std::string &tag : subway_item.tags) { + tagged[tag].push_back(subway_item.id); + } + + if (!AP_IsSunwarpShuffle() && subway_item.sunwarp && subway_item.sunwarp->type != SubwaySunwarpType::kFinal) { + std::ostringstream tag; + tag << "sunwarp" << subway_item.sunwarp->dots; + + tagged[tag.str()].push_back(subway_item.id); + } + } + + for (const auto &[tag, items] : tagged) { + // Pairwise connect all items with the same tag. + for (auto tag_it1 = items.begin(); std::next(tag_it1) != items.end(); + tag_it1++) { + for (auto tag_it2 = std::next(tag_it1); tag_it2 != items.end(); + tag_it2++) { + networks_.AddLink(*tag_it1, *tag_it2); + } + } + } + + checked_paintings_.clear(); +} + void SubwayMap::UpdateIndicators() { Redraw(); } +void SubwayMap::UpdatePainting(std::string from_painting_id, + std::optional to_painting_id) { + checked_paintings_.insert(from_painting_id); + + if (to_painting_id) { + networks_.AddLink(GD_GetSubwayItemForPainting(from_painting_id), + GD_GetSubwayItemForPainting(*to_painting_id)); + } +} + +void SubwayMap::UpdateSunwarp(SubwaySunwarp from_sunwarp, + SubwaySunwarp to_sunwarp) { + networks_.AddLink(GD_GetSubwayItemForSunwarp(from_sunwarp), + GD_GetSubwayItemForSunwarp(to_sunwarp)); +} + void SubwayMap::OnPaint(wxPaintEvent &event) { if (GetSize() != rendered_.GetSize()) { Redraw(); @@ -54,6 +108,73 @@ void SubwayMap::OnPaint(wxPaintEvent &event) { wxBufferedPaintDC dc(this); dc.DrawBitmap(rendered_, 0, 0); + if (hovered_item_ && networks_.IsItemInNetwork(*hovered_item_)) { + dc.SetBrush(*wxTRANSPARENT_BRUSH); + + int network_id = networks_.GetNetworkWithItem(*hovered_item_); + for (const auto &[item_id1, item_id2] : + networks_.GetNetworkGraph(network_id)) { + const SubwayItem &item1 = GD_GetSubwayItem(item_id1); + const SubwayItem &item2 = GD_GetSubwayItem(item_id2); + + int item1_x = (item1.x + AREA_ACTUAL_SIZE / 2) * render_width_ / map_image_.GetWidth() + render_x_; + int item1_y = (item1.y + AREA_ACTUAL_SIZE / 2) * render_width_ / map_image_.GetWidth() + render_y_; + + int item2_x = (item2.x + AREA_ACTUAL_SIZE / 2) * render_width_ / map_image_.GetWidth() + render_x_; + int item2_y = (item2.y + AREA_ACTUAL_SIZE / 2) * render_width_ / map_image_.GetWidth() + render_y_; + + int left = std::min(item1_x, item2_x); + int top = std::min(item1_y, item2_y); + int right = std::max(item1_x, item2_x); + int bottom = std::max(item1_y, item2_y); + + int halfwidth = right - left; + int halfheight = bottom - top; + + int ellipse_x; + int ellipse_y; + double start; + double end; + + if (item1_x > item2_x) { + ellipse_y = top; + + if (item1_y > item2_y) { + ellipse_x = left - halfwidth; + + start = 0; + end = 90; + } else { + ellipse_x = left; + + start = 90; + end = 180; + } + } else { + ellipse_y = top - halfheight; + + if (item1_y > item2_y) { + ellipse_x = left - halfwidth; + + start = 270; + end = 360; + } else { + ellipse_x = left; + + start = 180; + end = 270; + } + } + + dc.SetPen(*wxThePenList->FindOrCreatePen(*wxBLACK, 4)); + dc.DrawEllipticArc(ellipse_x, ellipse_y, halfwidth * 2, halfheight * 2, + start, end); + dc.SetPen(*wxThePenList->FindOrCreatePen(*wxCYAN, 2)); + dc.DrawEllipticArc(ellipse_x, ellipse_y, halfwidth * 2, halfheight * 2, + start, end); + } + } + event.Skip(); } @@ -73,13 +194,9 @@ void SubwayMap::OnMouseMove(wxMouseEvent &event) { } if (new_hovered_item != hovered_item_) { - if (new_hovered_item) { - wxLogVerbose("Hovered: %d", *new_hovered_item); - } else { - wxLogVerbose("Un-hovered: %d", *hovered_item_); - } - hovered_item_ = new_hovered_item; + + Refresh(); } event.Skip(); diff --git a/src/subway_map.h b/src/subway_map.h index 6cb5c63..1637125 100644 --- a/src/subway_map.h +++ b/src/subway_map.h @@ -9,15 +9,24 @@ #include #include +#include +#include #include #include +#include "game_data.h" +#include "network_set.h" + class SubwayMap : public wxPanel { public: SubwayMap(wxWindow *parent); + void OnConnect(); void UpdateIndicators(); + void UpdatePainting(std::string from_painting_id, + std::optional to_painting_id); + void UpdateSunwarp(SubwaySunwarp from_sunwarp, SubwaySunwarp to_sunwarp); private: void OnPaint(wxPaintEvent &event); @@ -40,6 +49,9 @@ class SubwayMap : public wxPanel { std::unique_ptr> tree_; std::optional hovered_item_; + + NetworkSet networks_; + std::set checked_paintings_; }; #endif /* end of include guard: SUBWAY_MAP_H_BD2D843E */ diff --git a/src/tracker_frame.cpp b/src/tracker_frame.cpp index 70fee2d..e944704 100644 --- a/src/tracker_frame.cpp +++ b/src/tracker_frame.cpp @@ -22,6 +22,7 @@ enum TrackerFrameIds { ID_SETTINGS = 3 }; +wxDEFINE_EVENT(STATE_RESET, wxCommandEvent); wxDEFINE_EVENT(STATE_CHANGED, wxCommandEvent); wxDEFINE_EVENT(STATUS_CHANGED, wxCommandEvent); @@ -56,6 +57,7 @@ TrackerFrame::TrackerFrame() Bind(wxEVT_MENU, &TrackerFrame::OnSettings, this, ID_SETTINGS); Bind(wxEVT_MENU, &TrackerFrame::OnCheckForUpdates, this, ID_CHECK_FOR_UPDATES); + Bind(STATE_RESET, &TrackerFrame::OnStateReset, this); Bind(STATE_CHANGED, &TrackerFrame::OnStateChanged, this); Bind(STATUS_CHANGED, &TrackerFrame::OnStatusChanged, this); @@ -103,6 +105,10 @@ void TrackerFrame::SetStatusMessage(std::string message) { QueueEvent(event); } +void TrackerFrame::ResetIndicators() { + QueueEvent(new wxCommandEvent(STATE_RESET)); +} + void TrackerFrame::UpdateIndicators() { QueueEvent(new wxCommandEvent(STATE_CHANGED)); } @@ -168,6 +174,13 @@ void TrackerFrame::OnCheckForUpdates(wxCommandEvent &event) { CheckForUpdates(/*manual=*/true); } +void TrackerFrame::OnStateReset(wxCommandEvent& event) { + tracker_panel_->UpdateIndicators(); + achievements_pane_->UpdateIndicators(); + subway_map_->OnConnect(); + Refresh(); +} + void TrackerFrame::OnStateChanged(wxCommandEvent &event) { tracker_panel_->UpdateIndicators(); achievements_pane_->UpdateIndicators(); diff --git a/src/tracker_frame.h b/src/tracker_frame.h index c7c6772..f1d7171 100644 --- a/src/tracker_frame.h +++ b/src/tracker_frame.h @@ -11,6 +11,7 @@ class AchievementsPane; class SubwayMap; class TrackerPanel; +wxDECLARE_EVENT(STATE_RESET, wxCommandEvent); wxDECLARE_EVENT(STATE_CHANGED, wxCommandEvent); wxDECLARE_EVENT(STATUS_CHANGED, wxCommandEvent); @@ -20,6 +21,7 @@ class TrackerFrame : public wxFrame { void SetStatusMessage(std::string message); + void ResetIndicators(); void UpdateIndicators(); private: @@ -29,6 +31,7 @@ class TrackerFrame : public wxFrame { void OnSettings(wxCommandEvent &event); void OnCheckForUpdates(wxCommandEvent &event); + void OnStateReset(wxCommandEvent &event); void OnStateChanged(wxCommandEvent &event); void OnStatusChanged(wxCommandEvent &event); -- cgit 1.4.1