package_subclass_build.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include <array>
  2. #include "../../../../state/account/account_state.h"
  3. #include "../../../../state/build_data/runtime.h"
  4. #include "../../../../state/runtime/runtime.h"
  5. #include "internal.h"
  6. namespace sunrise::client::content::items::packages {
  7. namespace {
  8. namespace domain = state::build_data::abilities;
  9. /** The authored equipment slot that holds the subclass. */
  10. constexpr std::size_t kSubclassSlot =
  11. static_cast<std::size_t>(state::account::inventory::EquipmentSlot::subclass);
  12. /**
  13. * Finds the socket entry list that carries one character's subclass abilities.
  14. * @param character Authored character.
  15. * @param socketEntryListIndex Receives the subclass's socket-entry-list index.
  16. * @return True when the character equips a subclass whose detail is published.
  17. */
  18. [[nodiscard]] bool subclass_list(const state::CharacterState& character,
  19. std::uint16_t& socketEntryListIndex,
  20. const char*& reason) noexcept {
  21. const auto& slot = character.equipment.slots[kSubclassSlot];
  22. state::build_data::items::Definition item{};
  23. state::build_data::items::details::Definition detail{};
  24. reason = "subclass_slot";
  25. if (!slot.has_value()) {
  26. return false;
  27. }
  28. reason = "subclass_item";
  29. if (!state::build_data::find_item_definition_hash(slot->definitionHash, item)) {
  30. return false;
  31. }
  32. reason = "subclass_detail";
  33. if (!state::build_data::find_configured_item_detail(item.definitionIndex, detail)) {
  34. return false;
  35. }
  36. socketEntryListIndex = detail.socketEntryListIndex;
  37. return true;
  38. }
  39. /**
  40. * @param rows Rows built so far.
  41. * @param row Candidate whose key is tested against them.
  42. * @return True when the candidate's key is already held.
  43. */
  44. [[nodiscard]] bool held(std::span<const domain::Definition> rows,
  45. const domain::Definition& row) noexcept {
  46. for (const domain::Definition& existing : rows) {
  47. if (existing.socketEntryListIndex == row.socketEntryListIndex
  48. && existing.selection == row.selection) {
  49. return true;
  50. }
  51. }
  52. return false;
  53. }
  54. /** @param character Authored character. @return Its 5 selected socket entries. */
  55. [[nodiscard]] domain::Selection selection_of(const state::CharacterState& character) noexcept {
  56. return {character.movementAbilityEntry,
  57. character.grenadeAbilityEntry,
  58. character.superAbilityEntry,
  59. character.meleeAbilityEntry,
  60. character.classAbilityEntry};
  61. }
  62. } // namespace
  63. /** Builds one ability bucket row per distinct subclass and ability selection in use. */
  64. bool build_character_abilities(const reader::Source& source,
  65. reader::Scratch& scratch,
  66. std::span<const std::byte> root,
  67. std::vector<std::byte>& table,
  68. std::vector<std::byte>& definition,
  69. std::vector<std::byte>& blob,
  70. std::span<state::build_data::abilities::Definition> output,
  71. std::size_t& count) noexcept {
  72. count = 0;
  73. std::uint32_t tableTag = 0;
  74. tables::Array rows{};
  75. if (!tables::slot_tag(root, tables::kSocketEntryListTableSlot, tableTag) || tableTag == 0) {
  76. report_ability_failure("table_slot", 0, root.size(), tableTag);
  77. return false;
  78. }
  79. if (!reader::read_tag(source, scratch, tableTag, table)) {
  80. report_ability_failure("table_read", 0, tableTag, 0);
  81. return false;
  82. }
  83. if (!tables::find_array_at(
  84. std::span<const std::byte>{table}, tables::kTableArrayDescriptor, rows)) {
  85. report_ability_failure("table_array", 0, table.size(), 0);
  86. return false;
  87. }
  88. const state::AccountState account = state::account_snapshot();
  89. // First-option defaults every shipped subclass starts at (see account_state.h). Equipping a
  90. // subclass always resets its picks to these (state_account_runtime.cpp), so every owned
  91. // subclass publishes a row at this fixed selection regardless of which one is equipped right
  92. // now. That row has to exist synchronously, ahead of time: the equip response that needs it is
  93. // built inline with the commit, with no room to wait on a later refresh slice.
  94. const domain::Selection defaultSelection{state::kDefaultMovementAbilityEntry,
  95. state::kDefaultGrenadeAbilityEntry,
  96. state::kDefaultSuperAbilityEntry,
  97. state::kDefaultMeleeAbilityEntry,
  98. state::kDefaultClassAbilityEntry};
  99. // Builds and stores one row, skipping a key already held. Best-effort: a failure here still
  100. // lets the other rows in this pass publish.
  101. const auto publish = [&](std::size_t character,
  102. std::uint16_t socketEntryListIndex,
  103. const domain::Selection& selection) noexcept {
  104. if (count >= output.size()) {
  105. return;
  106. }
  107. domain::Definition row{};
  108. row.socketEntryListIndex = socketEntryListIndex;
  109. row.selection = selection;
  110. if (held(output.first(count), row)) {
  111. return;
  112. }
  113. tables::IndexRow indexRow{};
  114. if (!tables::index_row(std::span<const std::byte>{table}, rows, socketEntryListIndex, indexRow)
  115. || indexRow.targetTag == 0) {
  116. report_ability_failure("index_row", character, socketEntryListIndex, rows.count);
  117. return;
  118. }
  119. if (!reader::read_tag(source, scratch, indexRow.targetTag, definition)) {
  120. report_ability_failure("definition_read", character, socketEntryListIndex, indexRow.targetTag);
  121. return;
  122. }
  123. if (!build_ability_buckets(
  124. source, scratch, std::span<const std::byte>{definition}, blob, selection, row)) {
  125. const std::size_t packedSelection = selection.movementEntry
  126. | (selection.grenadeEntry << 8U)
  127. | (selection.superEntry << 16U)
  128. | (selection.meleeEntry << 24U);
  129. report_ability_failure("bucket_build", character, socketEntryListIndex, packedSelection);
  130. return;
  131. }
  132. output[count++] = row;
  133. };
  134. for (std::size_t character = 0; character < account.characterCount && count < output.size();
  135. ++character) {
  136. std::uint16_t equippedSocketEntryListIndex = 0;
  137. const char* subclassReason = "subclass";
  138. if (!subclass_list(
  139. account.characters[character], equippedSocketEntryListIndex, subclassReason)) {
  140. const auto& subclass = account.characters[character].equipment.slots[kSubclassSlot];
  141. report_ability_failure(
  142. subclassReason, character, subclass.has_value() ? subclass->definitionHash : 0, 0);
  143. continue;
  144. }
  145. const auto& equippedSlot = account.characters[character].equipment.slots[kSubclassSlot];
  146. state::build_data::items::Definition equippedItem{};
  147. if (!state::build_data::find_item_definition_hash(equippedSlot->definitionHash,
  148. equippedItem)) {
  149. continue;
  150. }
  151. // Every subclass the character owns publishes a row, not just the equipped one, so a
  152. // later equip swap always lands on an already-built row instead of racing the next
  153. // refresh slice. When the group cannot be resolved, at least the equipped one still
  154. // publishes, matching the prior single-row behaviour.
  155. std::array<std::uint16_t, state::build_data::kSubclassGroupSize> group{};
  156. std::array<std::uint16_t, state::build_data::kSubclassGroupSize> members{};
  157. std::size_t memberCount = 1;
  158. members[0] = equippedItem.definitionIndex;
  159. if (state::build_data::find_subclass_group(equippedItem.definitionIndex, group)) {
  160. members = group;
  161. memberCount = group.size();
  162. }
  163. const domain::Selection realSelection = selection_of(account.characters[character]);
  164. for (std::size_t member = 0; member < memberCount && count < output.size(); ++member) {
  165. const std::uint16_t memberDefinitionIndex = members[member];
  166. state::build_data::items::details::Definition memberDetail{};
  167. if (!state::build_data::find_configured_item_detail(memberDefinitionIndex,
  168. memberDetail)) {
  169. continue;
  170. }
  171. publish(character, memberDetail.socketEntryListIndex, defaultSelection);
  172. // The equipped member also publishes its real current picks, on top of the default
  173. // row every member gets: a fresh boot that never swapped needs its actual selection
  174. // to resolve, not the shared default.
  175. if (memberDefinitionIndex == equippedItem.definitionIndex
  176. && !(realSelection == defaultSelection)) {
  177. publish(character, memberDetail.socketEntryListIndex, realSelection);
  178. }
  179. }
  180. }
  181. if (count == 0) {
  182. report_ability_failure("empty", account.characterCount, rows.count, output.size());
  183. }
  184. return true;
  185. }
  186. } // namespace sunrise::client::content::items::packages