collectible_catalog.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "collectible_catalog.h"
  2. #include <array>
  3. #include <shared_mutex>
  4. #include "../table.h"
  5. #include "core/threading/srw_lock.h"
  6. namespace sunrise::state::build_data::collectibles {
  7. namespace {
  8. core::threading::SrwLock g_lock;
  9. Table<Definition, kDefinitionCapacity> g_definitions;
  10. } // namespace
  11. /** Clears the table while no reader can observe a partial replacement. */
  12. void clear() noexcept {
  13. const std::lock_guard guard(g_lock);
  14. g_definitions.clear();
  15. }
  16. /** Checks that the native indices cover one complete dense range, in any input order. */
  17. bool valid(std::span<const Definition> definitions) noexcept {
  18. if (definitions.empty() || definitions.size() > kDefinitionCapacity) {
  19. return false;
  20. }
  21. std::array<bool, kDefinitionCapacity> occupied{};
  22. for (const Definition& definition : definitions) {
  23. if (definition.collectibleIndex >= definitions.size()
  24. || occupied[definition.collectibleIndex]
  25. || definition.materialRequirementCount > definition.materialRequirements.size()) {
  26. return false;
  27. }
  28. const bool hasRequirements = definition.materialRequirementCount != 0;
  29. if (hasRequirements
  30. != (definition.materialRequirementSetIndex
  31. != kUnavailableMaterialRequirementSetIndex)
  32. || hasRequirements != (definition.materialRequirementSetHash != 0)) {
  33. return false;
  34. }
  35. for (std::size_t index = 0; index < definition.materialRequirements.size(); ++index) {
  36. const MaterialRequirement& requirement = definition.materialRequirements[index];
  37. if (index < definition.materialRequirementCount) {
  38. if (requirement.itemDefinitionIndex == kUnavailableItemDefinitionIndex
  39. || requirement.quantity == 0) {
  40. return false;
  41. }
  42. for (std::size_t prior = 0; prior < index; ++prior) {
  43. if (definition.materialRequirements[prior].itemDefinitionIndex
  44. == requirement.itemDefinitionIndex) {
  45. return false;
  46. }
  47. }
  48. } else if (requirement.itemDefinitionIndex != kUnavailableItemDefinitionIndex
  49. || requirement.quantity != 0 || requirement.deleteOnAction
  50. || requirement.omitFromRequirements) {
  51. return false;
  52. }
  53. }
  54. occupied[definition.collectibleIndex] = true;
  55. }
  56. return true;
  57. }
  58. /** Places every validated row at the native index the request protocol uses. */
  59. bool replace(std::span<const Definition> definitions) noexcept {
  60. if (!valid(definitions)) {
  61. return false;
  62. }
  63. const std::lock_guard guard(g_lock);
  64. const std::span<Definition> storage = g_definitions.reset(definitions.size());
  65. if (storage.size() != definitions.size()) {
  66. return false;
  67. }
  68. for (const Definition& definition : definitions) {
  69. storage[definition.collectibleIndex] = definition;
  70. }
  71. return true;
  72. }
  73. /** Finds a row only when it is inside the published dense table. */
  74. bool find(std::uint16_t collectibleIndex, Definition& definition) noexcept {
  75. definition = {};
  76. definition.itemDefinitionIndex = kUnavailableItemDefinitionIndex;
  77. const std::shared_lock guard(g_lock);
  78. const std::span<const Definition> rows = g_definitions.rows();
  79. const bool found = static_cast<std::size_t>(collectibleIndex) < rows.size();
  80. if (found) {
  81. definition = rows[collectibleIndex];
  82. }
  83. return found;
  84. }
  85. /** Answers whether any published collectible grants one installed item row. */
  86. bool grants_item(std::uint16_t itemDefinitionIndex) noexcept {
  87. if (itemDefinitionIndex == kUnavailableItemDefinitionIndex) {
  88. return false;
  89. }
  90. const std::shared_lock guard(g_lock);
  91. for (const Definition& definition : g_definitions.rows()) {
  92. if (definition.itemDefinitionIndex == itemDefinitionIndex) {
  93. return true;
  94. }
  95. }
  96. return false;
  97. }
  98. /** Finds the collectible that grants one installed item row. */
  99. bool find_granting(std::uint16_t itemDefinitionIndex, std::uint16_t& collectibleIndex) noexcept {
  100. if (itemDefinitionIndex == kUnavailableItemDefinitionIndex) {
  101. return false;
  102. }
  103. const std::shared_lock guard(g_lock);
  104. for (const Definition& definition : g_definitions.rows()) {
  105. if (definition.itemDefinitionIndex == itemDefinitionIndex) {
  106. collectibleIndex = definition.collectibleIndex;
  107. return true;
  108. }
  109. }
  110. return false;
  111. }
  112. /** Copies the dense rows without exposing catalog storage. */
  113. bool snapshot(std::span<Definition> output, std::size_t& count) noexcept {
  114. const std::shared_lock guard(g_lock);
  115. return g_definitions.snapshot(output, count);
  116. }
  117. /** @return Number of published rows, read under the catalog lock. */
  118. std::size_t count() noexcept {
  119. const std::shared_lock guard(g_lock);
  120. return g_definitions.count();
  121. }
  122. } // namespace sunrise::state::build_data::collectibles