about summary refs log tree commit diff stats
path: root/src/tracker_state.cpp
blob: 46bdbecd47ad099dcd488e9f888657aa0bc41719 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
#ifndef TRACKER_FRAME_H_86BD8DFB
#define TRACKER_FRAME_H_86BD8DFB

#include <wx/wxprec.h>

#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif

class AchievementsPane;
class TrackerPanel;

wxDECLARE_EVENT(STATE_CHANGED, wxCommandEvent);
wxDECLARE_EVENT(STATUS_CHANGED, wxCommandEvent);

class TrackerFrame : public wxFrame {
 public:
  TrackerFrame();

  void SetStatusMessage(std::string message);

  void UpdateIndicators();

 private:
  void OnExit(wxCommandEvent &event);
  void OnAbout(wxCommandEvent &event);
  void OnConnect(wxCommandEvent &event);
  void OnSettings(wxCommandEvent &event);
  void OnCheckForUpdates(wxCommandEvent &event);

  void OnStateChanged(wxCommandEvent &event);
  void OnStatusChanged(wxCommandEvent &event);

  void CheckForUpdates(bool manual);

  TrackerPanel *tracker_panel_;
  AchievementsPane *achievements_pane_;
};

#endif /* end of include guard: TRACKER_FRAME_H_86BD8DFB */
5'>365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
#include "tracker_state.h"

#include <list>
#include <map>
#include <mutex>
#include <set>
#include <sstream>
#include <tuple>

#include "ap_state.h"
#include "game_data.h"

namespace {

struct Requirements {
  bool disabled = false;

  std::set<int> doors;      // non-grouped, handles progressive
  std::set<int> items;      // all other items
  std::set<int> rooms;      // maybe
  bool mastery = false;     // maybe
  bool panel_hunt = false;  // maybe

  void Merge(const Requirements& rhs) {
    if (rhs.disabled) {
      return;
    }

    for (int id : rhs.doors) {
      doors.insert(id);
    }
    for (int id : rhs.items) {
      items.insert(id);
    }
    for (int id : rhs.rooms) {
      rooms.insert(id);
    }
    mastery = mastery || rhs.mastery;
    panel_hunt = panel_hunt || rhs.panel_hunt;
  }
};

class RequirementCalculator {
 public:
  void Reset() {
    doors_.clear();
    panels_.clear();
  }

  const Requirements& GetDoor(int door_id) {
    if (!doors_.count(door_id)) {
      Requirements requirements;
      const Door& door_obj = GD_GetDoor(door_id);

      if (!AP_IsPilgrimageEnabled() &&
          door_obj.type == DoorType::kSunPainting) {
        requirements.items.insert(door_obj.ap_item_id);
      } else if (door_obj.type == DoorType::kSunwarp) {
        switch (AP_GetSunwarpAccess()) {
          case kSUNWARP_ACCESS_NORMAL:
            // Do nothing.
            break;
          case kSUNWARP_ACCESS_DISABLED:
            requirements.disabled = true;
            break;
          case kSUNWARP_ACCESS_UNLOCK:
            requirements.items.insert(door_obj.group_ap_item_id);
            break;
          case kSUNWARP_ACCESS_INDIVIDUAL:
          case kSUNWARP_ACCESS_PROGRESSIVE:
            requirements.doors.insert(door_obj.id);
            break;
        }
      } else if (AP_GetDoorShuffleMode() == kNO_DOORS || door_obj.skip_item) {
        requirements.rooms.insert(door_obj.room);

        for (int panel_id : door_obj.panels) {
          const Requirements& panel_reqs = GetPanel(panel_id);
          requirements.Merge(panel_reqs);
        }
      } else if (AP_GetDoorShuffleMode() == kSIMPLE_DOORS &&
                 !door_obj.group_name.empty()) {
        requirements.items.insert(door_obj.group_ap_item_id);
      } else {
        requirements.doors.insert(door_obj.id);
      }

      doors_[door_id] = requirements;
    }

    return doors_[door_id];
  }

  const Requirements& GetPanel(int panel_id) {
    if (!panels_.count(panel_id)) {
      Requirements requirements;
      const Panel& panel_obj = GD_GetPanel(panel_id);

      requirements.rooms.insert(panel_obj.room);

      if (panel_obj.name == "THE MASTER") {
        requirements.mastery = true;
      }

      if ((panel_obj.name == "ANOTHER TRY" || panel_obj.name == "LEVEL 2") &&
          AP_GetLevel2Requirement() > 1) {
        requirements.panel_hunt = true;
      }

      for (int room_id : panel_obj.required_rooms) {
        requirements.rooms.insert(room_id);
      }

      for (int door_id : panel_obj.required_doors) {
        const Requirements& door_reqs = GetDoor(door_id);
        requirements.Merge(door_reqs);
      }

      for (int panel_id : panel_obj.required_panels) {
        const Requirements& panel_reqs = GetPanel(panel_id);
        requirements.Merge(panel_reqs);
      }

      if (AP_IsColorShuffle()) {
        for (LingoColor color : panel_obj.colors) {
          requirements.items.insert(GD_GetItemIdForColor(color));
        }
      }

      panels_[panel_id] = requirements;
    }

    return panels_[panel_id];
  }

 private:
  std::map<int, Requirements> doors_;
  std::map<int, Requirements> panels_;
};

struct TrackerState {
  std::map<int, bool> reachability;
  std::set<int> reachable_doors;
  std::set<int> reachable_paintings;
  std::mutex reachability_mutex;
  RequirementCalculator requirements;
  std::map<int, std::map<std::string, bool>> door_reports;
};

enum Decision { kYes, kNo, kMaybe };

TrackerState& GetState() {
  static TrackerState* instance = new TrackerState();
  return *instance;
}

class StateCalculator;

struct StateCalculatorOptions {
  int start;
  bool pilgrimage = false;
  StateCalculator* parent = nullptr;
};

class StateCalculator {
 public:
  StateCalculator() = default;

  explicit StateCalculator(StateCalculatorOptions options)
      : options_(options) {}

  void Calculate() {
    std::list<int> panel_boundary;
    std::list<int> painting_boundary;
    std::list<Exit> flood_boundary;
    flood_boundary.push_back({.destination_room = options_.start});

    bool reachable_changed = true;
    while (reachable_changed) {
      reachable_changed = false;

      std::list<Exit> new_boundary;

      std::list<int> new_panel_boundary;
      for (int panel_id : panel_boundary) {
        if (solveable_panels_.count(panel_id)) {
          continue;
        }

        Decision panel_reachable = IsPanelReachable(panel_id);
        if (panel_reachable == kYes) {
          solveable_panels_.insert(panel_id);
          reachable_changed = true;
        } else if (panel_reachable == kMaybe) {
          new_panel_boundary.push_back(panel_id);
        }
      }

      std::list<int> new_painting_boundary;
      for (int painting_id : painting_boundary) {
        if (reachable_paintings_.count(painting_id)) {
          continue;
        }

        Decision painting_reachable = IsPaintingReachable(painting_id);
        if (painting_reachable == kYes) {
          reachable_paintings_.insert(painting_id);
          reachable_changed = true;

          PaintingExit cur_painting = GD_GetPaintingExit(painting_id);
          if (AP_GetPaintingMapping().count(cur_painting.internal_id) &&
              AP_GetCheckedPaintings().count(cur_painting.internal_id)) {
            Exit painting_exit;
            PaintingExit target_painting =
                GD_GetPaintingExit(GD_GetPaintingByName(
                    AP_GetPaintingMapping().at(cur_painting.internal_id)));
            painting_exit.destination_room = target_painting.room;

            new_boundary.push_back(painting_exit);
          }
        } else if (painting_reachable == kMaybe) {
          new_painting_boundary.push_back(painting_id);
        }
      }

      for (const Exit& room_exit : flood_boundary) {
        if (reachable_rooms_.count(room_exit.destination_room)) {
          continue;
        }

        bool valid_transition = false;

        Decision exit_usable = IsExitUsable(room_exit);
        if (exit_usable == kYes) {
          valid_transition = true;
        } else if (exit_usable == kMaybe) {
          new_boundary.push_back(room_exit);
        }

        if (valid_transition) {
          reachable_rooms_.insert(room_exit.destination_room);
          reachable_changed = true;

          const Room& room_obj = GD_GetRoom(room_exit.destination_room);
          for (const Exit& out_edge : room_obj.exits) {
            if (out_edge.type == EntranceType::kPainting &&
                AP_IsPaintingShuffle()) {
              continue;
            }

            if (out_edge.type == EntranceType::kSunwarp &&
                AP_IsSunwarpShuffle()) {
              continue;
            }

            new_boundary.push_back(out_edge);
          }

          if (AP_IsPaintingShuffle()) {
            for (int out_edge : room_obj.paintings) {
              new_painting_boundary.push_back(out_edge);
            }
          }

          if (AP_IsSunwarpShuffle()) {
            for (int index : room_obj.sunwarps) {
              if (AP_GetSunwarpMapping().count(index)) {
                const SunwarpMapping& sm = AP_GetSunwarpMapping().at(index);

                Exit sunwarp_exit;
                sunwarp_exit.destination_room =
                    GD_GetRoomForSunwarp(sm.exit_index);
                sunwarp_exit.door = GD_GetSunwarpDoors().at(sm.dots - 1);

                new_boundary.push_back(sunwarp_exit);
              }
            }
          }

          if (AP_HasEarlyColorHallways() && room_obj.name == "Starting Room") {
            new_boundary.push_back(
                {.destination_room = GD_GetRoomByName("Outside The Undeterred"),
                 .type = EntranceType::kPainting});
          }

          if (AP_IsPilgrimageEnabled()) {
            if (room_obj.name == "Hub Room") {
              new_boundary.push_back(
                  {.destination_room = GD_GetRoomByName("Pilgrim Antechamber"),
                   .type = EntranceType::kPilgrimage});
            }
          } else {
            if (room_obj.name == "Starting Room") {
              new_boundary.push_back(
                  {.destination_room = GD_GetRoomByName("Pilgrim Antechamber"),
                   .door =
                       GD_GetDoorByName("Pilgrim Antechamber - Sun Painting"),
                   .type = EntranceType::kPainting});
            }
          }

          for (int panel_id : room_obj.panels) {
            new_panel_boundary.push_back(panel_id);
          }
        }
      }

      flood_boundary = new_boundary;
      panel_boundary = new_panel_boundary;
      painting_boundary = new_painting_boundary;
    }

    // Now that we know the full reachable area, let's make sure all doors are
    // evaluated.
    for (const Door& door : GD_GetDoors()) {
      int discard = IsDoorReachable(door.id);
    }
  }

  const std::set<int>& GetReachableRooms() const { return reachable_rooms_; }

  const std::map<int, Decision>& GetDoorDecisions() const {
    return door_decisions_;
  }

  const std::set<int>& GetSolveablePanels() const { return solveable_panels_; }

  const std::set<int>& GetReachablePaintings() const {
    return reachable_paintings_;
  }

  const std::map<int, std::map<std::string, bool>>& GetDoorReports() const {
    return door_report_;
  }

 private:
  Decision IsNonGroupedDoorReachable(const Door& door_obj) {
    bool has_item = AP_HasItem(door_obj.ap_item_id);

    if (!has_item) {
      for (const ProgressiveRequirement& prog_req : door_obj.progressives) {
        if (AP_HasItem(prog_req.ap_item_id, prog_req.quantity)) {
          has_item = true;
          break;
        }
      }
    }

    return has_item ? kYes : kNo;
  }

  Decision AreRequirementsSatisfied(
      const Requirements& reqs, std::map<std::string, bool>* report = nullptr) {
    if (reqs.disabled) {
      return kNo;
    }

    Decision final_decision = kYes;

    for (int door_id : reqs.doors) {
      const Door& door_obj = GD_GetDoor(door_id);
      Decision decision = IsNonGroupedDoorReachable(door_obj);

      if (report) {
        (*report)[door_obj.item_name] = (decision == kYes);
      }

      if (decision != kYes) {
        final_decision = decision;
      }
    }

    for (int item_id : reqs.items) {
      bool has_item = AP_HasItem(item_id);
      if (report) {
        (*report)[AP_GetItemName(item_id)] = has_item;
      }

      if (!has_item) {
        final_decision = kNo;
      }
    }

    for (int room_id : reqs.rooms) {
      bool reachable = reachable_rooms_.count(room_id);

      if (report) {
        std::string report_name = "Reach \"" + GD_GetRoom(room_id).name + "\"";
        (*report)[report_name] = reachable;
      }

      if (!reachable && final_decision != kNo) {
        final_decision = kMaybe;
      }
    }

    if (reqs.mastery) {
      int achievements_accessible = 0;

      for (int achieve_id : GD_GetAchievementPanels()) {
        if (solveable_panels_.count(achieve_id)) {
          achievements_accessible++;

          if (achievements_accessible >= AP_GetMasteryRequirement()) {
            break;
          }
        }
      }

      bool can_mastery =
          (achievements_accessible >= AP_GetMasteryRequirement());
      if (report) {
        (*report)["Mastery"] = can_mastery;
      }

      if (!can_mastery && final_decision != kNo) {
        final_decision = kMaybe;
      }
    }

    if (reqs.panel_hunt) {
      int counting_panels_accessible = 0;

      for (int solved_panel_id : solveable_panels_) {
        const Panel& solved_panel = GD_GetPanel(solved_panel_id);

        if (!solved_panel.non_counting) {
          counting_panels_accessible++;
        }
      }

      bool can_level2 =
          (counting_panels_accessible >= AP_GetLevel2Requirement() - 1);
      if (report) {
        std::string report_name =
            std::to_string(AP_GetLevel2Requirement()) + " Panels";
        (*report)[report_name] = can_level2;
      }

      if (!can_level2 && final_decision != kNo) {
        final_decision = kMaybe;
      }
    }

    return final_decision;
  }

  Decision IsDoorReachable_Helper(int door_id) {
    if (door_report_.count(door_id)) {
      door_report_[door_id].clear();
    } else {
      door_report_[door_id] = {};
    }

    return AreRequirementsSatisfied(GetState().requirements.GetDoor(door_id),
                                    &door_report_[door_id]);
  }

  Decision IsDoorReachable(int door_id) {
    if (options_.parent) {
      return options_.parent->IsDoorReachable(door_id);
    }

    if (door_decisions_.count(door_id)) {
      return door_decisions_.at(door_id);
    }

    Decision result = IsDoorReachable_Helper(door_id);
    if (result != kMaybe) {
      door_decisions_[door_id] = result;
    }

    return result;
  }

  Decision IsPanelReachable(int panel_id) {
    return AreRequirementsSatisfied(GetState().requirements.GetPanel(panel_id));
  }

  Decision IsPaintingReachable(int painting_id) {
    const PaintingExit& painting = GD_GetPaintingExit(painting_id);
    if (painting.door) {
      return IsDoorReachable(*painting.door);
    }

    return kYes;
  }

  Decision IsExitUsable(const Exit& room_exit) {
    if (room_exit.type == EntranceType::kPilgrimage) {
      if (options_.pilgrimage || !AP_IsPilgrimageEnabled()) {
        return kNo;
      }

      if (AP_GetSunwarpAccess() != kSUNWARP_ACCESS_NORMAL) {
        for (int door_id : GD_GetSunwarpDoors()) {
          Decision sub_decision = IsDoorReachable(door_id);
          if (sub_decision != kYes) {
            return sub_decision;
          }
        }
      }

      std::vector<std::tuple<int, int>> pilgrimage_pairs;
      if (AP_IsSunwarpShuffle()) {
        pilgrimage_pairs = std::vector<std::tuple<int, int>>(5);

        for (const auto& [start_index, mapping] : AP_GetSunwarpMapping()) {
          if (mapping.dots > 1) {
            std::get<1>(pilgrimage_pairs[mapping.dots - 2]) = start_index;
          }
          if (mapping.dots < 6) {
            std::get<0>(pilgrimage_pairs[mapping.dots - 1]) =
                mapping.exit_index;
          }
        }
      } else {
        pilgrimage_pairs = {{6, 1}, {8, 3}, {9, 4}, {10, 5}};
      }

      for (const auto& [from_sunwarp, to_sunwarp] : pilgrimage_pairs) {
        StateCalculator pilgrimage_calculator(
            {.start = GD_GetRoomForSunwarp(from_sunwarp),
             .pilgrimage = true,
             .parent = this});
        pilgrimage_calculator.Calculate();

        if (!pilgrimage_calculator.GetReachableRooms().count(
                GD_GetRoomForSunwarp(to_sunwarp))) {
          return kMaybe;
        }
      }

      return kYes;
    }

    if (options_.pilgrimage) {
      if (room_exit.type == EntranceType::kWarp ||
          room_exit.type == EntranceType::kSunwarp) {
        return kNo;
      }
      if (room_exit.type == EntranceType::kCrossroadsRoofAccess &&
          !AP_DoesPilgrimageAllowRoofAccess()) {
        return kNo;
      }
      if (room_exit.type == EntranceType::kPainting &&
          !AP_DoesPilgrimageAllowPaintings()) {
        return kNo;
      }
    }

    if (room_exit.type == EntranceType::kSunwarp) {
      if (AP_GetSunwarpAccess() == kSUNWARP_ACCESS_NORMAL) {
        return kYes;
      } else if (AP_GetSunwarpAccess() == kSUNWARP_ACCESS_DISABLED) {
        return kNo;
      }
    }

    if (room_exit.door.has_value()) {
      return IsDoorReachable(*room_exit.door);
    }

    return kYes;
  }

  StateCalculatorOptions options_;

  std::set<int> reachable_rooms_;
  std::map<int, Decision> door_decisions_;
  std::set<int> solveable_panels_;
  std::set<int> reachable_paintings_;
  std::map<int, std::map<std::string, bool>> door_report_;
};

}  // namespace

void ResetReachabilityRequirements() {
  std::lock_guard reachability_guard(GetState().reachability_mutex);
  GetState().requirements.Reset();
}

void RecalculateReachability() {
  StateCalculator state_calculator({.start = GD_GetRoomByName("Menu")});
  state_calculator.Calculate();

  const std::set<int>& reachable_rooms = state_calculator.GetReachableRooms();
  const std::set<int>& solveable_panels = state_calculator.GetSolveablePanels();

  std::map<int, bool> new_reachability;
  for (const MapArea& map_area : GD_GetMapAreas()) {
    for (size_t section_id = 0; section_id < map_area.locations.size();
         section_id++) {
      const Location& location_section = map_area.locations.at(section_id);
      bool reachable = reachable_rooms.count(location_section.room);
      if (reachable) {
        for (int panel_id : location_section.panels) {
          reachable &= (solveable_panels.count(panel_id) == 1);
        }
      }

      new_reachability[location_section.ap_location_id] = reachable;
    }
  }

  std::set<int> new_reachable_doors;
  for (const auto& [door_id, decision] : state_calculator.GetDoorDecisions()) {
    if (decision == kYes) {
      new_reachable_doors.insert(door_id);
    }
  }

  std::set<int> reachable_paintings = state_calculator.GetReachablePaintings();
  std::map<int, std::map<std::string, bool>> door_reports =
      state_calculator.GetDoorReports();

  {
    std::lock_guard reachability_guard(GetState().reachability_mutex);
    std::swap(GetState().reachability, new_reachability);
    std::swap(GetState().reachable_doors, new_reachable_doors);
    std::swap(GetState().reachable_paintings, reachable_paintings);
    std::swap(GetState().door_reports, door_reports);
  }
}

bool IsLocationReachable(int location_id) {
  std::lock_guard reachability_guard(GetState().reachability_mutex);

  if (GetState().reachability.count(location_id)) {
    return GetState().reachability.at(location_id);
  } else {
    return false;
  }
}

bool IsDoorOpen(int door_id) {
  std::lock_guard reachability_guard(GetState().reachability_mutex);

  return GetState().reachable_doors.count(door_id);
}

bool IsPaintingReachable(int painting_id) {
  std::lock_guard reachability_guard(GetState().reachability_mutex);

  return GetState().reachable_paintings.count(painting_id);
}

const std::map<std::string, bool>& GetDoorRequirements(int door_id) {
  std::lock_guard reachability_guard(GetState().reachability_mutex);

  return GetState().door_reports[door_id];
}