diff --git a/src/Offsets.h b/src/Offsets.h index 65b33191..2c4d9b7c 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -4401,6 +4401,13 @@ enum Offsets { FUN_CLOSE_HAND = 0x00479660, // TryOnItem — `__thiscall(pane, itemID)`: the worker behind TryOn. FUN_DRESSUPMODEL_TRY_ON_ITEM = 0x00504540, + // CanHoldBoth — `bool __fastcall(mainType, offType, bool dualWield)`, + // ret 4: TryOnItem keeps the other lane's item only when this is true. + // Main side 13 or 21 with off side 14 or 23, or 13 or 22 when dualWield. + FUN_DRESSUPMODEL_CAN_HOLD_BOTH = 0x00504BC0, + // `int __cdecl()`: the active player can dual wield (a non-zero + // 0x00C4D770). TryOnItem asks it only when the pane shows that player. + FUN_PLAYER_CAN_DUAL_WIELD = 0x005EAB70, // The worker's item-cache callback (0x00504BA0) re-runs it once the // record loads: `push itemID; mov ecx, pane; call` here. The only other // caller is the original Script_TryOn (0x00504E29), which ours replaces. @@ -10992,6 +10999,10 @@ enum Offsets { // form's while shapeshifted). Field 0x7D in the update-field name table // (0x0083A73C). OFF_UNIT_FIELD_DISPLAYID = 0x1F4, + // UNIT_FIELD_NATIVEDISPLAYID: the unit's own display (field 0x7E). + // DressUpModel's ready byte is DISPLAYID == NATIVEDISPLAYID (0x00504450, + // benilla ui/dressup): a shapeshifted unit takes no armor try-on. + OFF_UNIT_FIELD_NATIVEDISPLAYID = 0x1F8, // Local player movement-flags u32. Lives directly on the // CGPlayer object (not the broadcast descriptor) — this is diff --git a/src/model/DressUp.cpp b/src/model/DressUp.cpp index b2932564..98cc964b 100644 --- a/src/model/DressUp.cpp +++ b/src/model/DressUp.cpp @@ -23,17 +23,12 @@ // which the ModelScene actor shares. // // TryOn keeps vanilla's worker (TryOnItem, FUN_DRESSUPMODEL_TRY_ON_ITEM) and -// takes 4.3.4's argument forms in front of it: an item ID, an item link or -// `item:` string, a trade-skill `enchant:` link (the item its recipe makes), -// or the name of an item the player carries. Anything else shows +// takes the arguments Model::TryOn reads in front of it. No item shows // ERR_NOT_EQUIPPABLE, as 4.3.4 does. // -// Retail's hand argument is an inventory slot name, matched the way -// GetInventorySlotInfo matches it (SStrCmpI over the PaperDollItemFrame -// rows): MainHandSlot or SecondaryHandSlot. The worker picks a hand at one -// site (SITE_TRY_ON_HAND_CHOICE); a named hand sends a held item down that -// hand's path, so the worker's own eviction of an incompatible other lane -// runs, as 4.3.4's does for a forced hand. +// The worker picks a hand at one site (SITE_TRY_ON_HAND_CHOICE); a named hand +// sends a held item down that hand's path, so the worker's own eviction of an +// incompatible other lane runs, as 4.3.4's does for a forced hand. // // The enchant glow is the player's AttachEnchantVisual step on the item the // worker hangs: FindChild at the attachment AttachItemModel used, then @@ -43,11 +38,10 @@ // the link's enchant field, as 4.3.4 passes it. The worker's cache callback // re-runs with neither hand nor enchant, as 4.3.4's does. // -// 4.3.4's worker (`TryOnWithVisual`, 0x00A05010) tries on what a recipe makes: -// an item with no inventory type whose spell teaches a CREATE_ITEM spell is -// swapped for the made item. Vanilla's worker shows ERR_NOT_EQUIPPABLE -// instead. `TryOnItem` adds the swap in front of the worker, for our TryOn -// and for the worker's cache-load callback, so an uncached pattern works too. +// Vanilla's worker shows ERR_NOT_EQUIPPABLE for a recipe, where 4.3.4's +// tries on what it makes. `TryOnItem` adds that swap (Model::TryOn::WornItem) +// in front of the worker, for our TryOn and for the worker's cache-load +// callback, so an uncached pattern works too. // // TryOn's result (retail's ItemTryOnReason) is read from the same gates the // worker passes: an uncached record is DataPending (the worker has queued the @@ -68,21 +62,13 @@ #include "Game.h" #include "Offsets.h" -#include "dbc/Lookup.h" #include "Common.h" -#include "item/Arg.h" -#include "item/Dressable.h" -#include "item/ID.h" -#include "item/Location.h" #include "item/Record.h" -#include "model/ItemDisplay.h" #include "model/M2.h" #include "model/Outfit.h" +#include "model/TryOn.h" #include -#include -#include -#include #include namespace Model::DressUp { @@ -96,16 +82,6 @@ using AttachItemModel_t = int(__fastcall *)(void *model, const void *record, uin using TryOnItem_t = void(__thiscall *)(void *pane, uint32_t itemID); using SetModelInstance_t = void(__thiscall *)(void *pane, void *model); using ClearModel_t = void(__thiscall *)(void *pane); -using DisplayError_t = void(__cdecl *)(uint32_t id, ...); - -using Outfit::kBodySlotMainHand; -using Outfit::kBodySlotOffHand; - -// Retail's Enum.ItemTryOnReason. -constexpr int kTryOnSuccess = 0; -constexpr int kTryOnWrongRace = 1; -constexpr int kTryOnNotEquippable = 2; -constexpr int kTryOnDataPending = 3; static_assert(Offsets::OFF_DRESSUPMODEL_OFF_HAND_LANE == Offsets::OFF_DRESSUPMODEL_MAIN_HAND_LANE + sizeof(Outfit::HeldLane), @@ -132,19 +108,6 @@ Outfit::Lanes &LanesOf(void *pane) { return Game::Ref(pane, Offsets::OFF_DRESSUPMODEL_MAIN_HAND_LANE); } -// The glow `enchantID` gives an item shown with `display`: none when the -// display has its own. -uint32_t EnchantVisual(const void *display, uint32_t enchantID) { - if (display == nullptr || - Model::ItemDisplay::OwnVisual(static_cast(display)) != 0) - return 0; - const uint8_t *enchant = DBC::Record(Offsets::VAR_SPELLITEMENCHANT_RECORDS, - Offsets::VAR_SPELLITEMENCHANT_COUNT, enchantID); - return enchant != nullptr - ? Game::Read(enchant, Offsets::OFF_SPELLITEMENCHANT_ITEM_VISUAL) - : 0; -} - void ApplyWeapons(void *pane, const PaneState &s) { Outfit::HangWeapons(Pane::Model(pane), LanesOf(pane), s.weapons, s.sheathed, s.hideWeapons); } @@ -176,7 +139,7 @@ int __fastcall TryOnAttach(void *model, const void *record, uint32_t bodySlot, PaneState &s = g_panes[g_tryOnPane]; const int lane = Outfit::LaneIndex(bodySlot); s.weapons.display[lane] = record; - s.weapons.visual[lane] = EnchantVisual(record, g_tryOnEnchant); + s.weapons.visual[lane] = Outfit::EnchantVisual(record, g_tryOnEnchant); if (s.sheathSet) { if (s.hideWeapons) return -1; @@ -236,103 +199,32 @@ int __fastcall Script_UndressSlot(void *L) { return 0; } -// The item a trade-skill recipe makes, from an `enchant:` link: a -// TRADESPELL whose first effect makes an item (4.3.4's test). -uint32_t RecipeItem(const char *link) { - const char *enchant = std::strstr(link, "enchant:"); - if (enchant == nullptr) - return 0; - const long spellID = std::strtol(enchant + 8, nullptr, 10); - if (spellID <= 0) - return 0; - const uint8_t *spell = DBC::Record(Offsets::VAR_SPELL_RECORDS, Offsets::VAR_SPELL_RECORD_COUNT, - static_cast(spellID)); - if (spell == nullptr || - (Game::Read(spell, Offsets::OFF_SPELL_RECORD_ATTRIBUTES) & - Offsets::SPELL_ATTR_TRADESPELL) == 0) - return 0; - return Game::Read(spell, Offsets::OFF_SPELL_RECORD_EFFECT_ITEM_TYPE); -} - -// The item the worker is given: a cached recipe item becomes what it makes. -uint32_t WornItem(uint32_t itemID) { - const uint8_t *record = Item::PeekRecord(itemID); - if (record != nullptr && - Game::Read(record, Offsets::OFF_ITEMSTATS_INVENTORY_TYPE) == 0) { - const uint32_t made = Item::Dressable::RecipeItem(record); - if (made != 0) - return made; - } - return itemID; -} - // `__thiscall(pane, itemID)` as `__fastcall` with the EDX slot unused, so it // can stand at the worker's call sites. void __fastcall TryOnItem(void *pane, void *, uint32_t itemID) { g_tryOnPane = pane; - reinterpret_cast(Offsets::FUN_DRESSUPMODEL_TRY_ON_ITEM)(pane, WornItem(itemID)); + reinterpret_cast(Offsets::FUN_DRESSUPMODEL_TRY_ON_ITEM)(pane, TryOn::WornItem(itemID)); g_tryOnPane = nullptr; } -// The lane a hand name picks: the PaperDollItemFrame row it names -// (GetInventorySlotInfo's match), if that row is a held slot; else 0. -uint32_t NamedHand(const char *name) { - const auto *records = Game::Read(Offsets::VAR_PAPERDOLLITEMFRAME_RECORDS); - const int count = Game::Read(Offsets::VAR_PAPERDOLLITEMFRAME_COUNT); - if (records == nullptr) - return 0; - for (int i = 0; i < count; ++i) { - const uint8_t *row = records + i * Offsets::OFF_PAPERDOLLITEMFRAME_RECORD_STRIDE; - if (Game::StrCmpI(Game::Read(row, Offsets::OFF_PAPERDOLLITEMFRAME_SLOT_NAME), - name) != 0) - continue; - switch (Game::Read(row, Offsets::OFF_PAPERDOLLITEMFRAME_SLOT_NUMBER)) { - case Offsets::INVSLOT_MAINHAND: return kBodySlotMainHand; - case Offsets::INVSLOT_OFFHAND: return kBodySlotOffHand; - default: return 0; - } - } - return 0; -} - // What the worker did with `itemID`, from the gates it passes (0x00504540). int TryOnResult(void *pane, uint32_t itemID) { const uint8_t *record = Item::PeekRecord(itemID); if (record == nullptr) - return kTryOnDataPending; + return TryOn::kDataPending; if (Game::Read(record, Offsets::OFF_ITEMSTATS_DISPLAY_INFO_ID) == 0 || Pane::Model(pane) == nullptr) - return kTryOnNotEquippable; - switch (Game::Read(record, Offsets::OFF_ITEMSTATS_INVENTORY_TYPE)) { - case Offsets::INVTYPE_HEAD: - case Offsets::INVTYPE_SHOULDER: - case Offsets::INVTYPE_BODY: - case Offsets::INVTYPE_CHEST: - case Offsets::INVTYPE_WAIST: - case Offsets::INVTYPE_LEGS: - case Offsets::INVTYPE_FEET: - case Offsets::INVTYPE_WRIST: - case Offsets::INVTYPE_HAND: - case Offsets::INVTYPE_CLOAK: - case Offsets::INVTYPE_TABARD: - case Offsets::INVTYPE_ROBE: - // The armor arms: the component and the ready byte. + return TryOn::kNotEquippable; + switch (Outfit::ArmOf(Game::Read(record, Offsets::OFF_ITEMSTATS_INVENTORY_TYPE))) { + case Outfit::Arm::Armor: // the component and the ready byte return Game::Read(pane, Offsets::OFF_DRESSUPMODEL_COMPONENT) != nullptr && Game::Read(pane, Offsets::OFF_DRESSUPMODEL_READY) != 0 - ? kTryOnSuccess - : kTryOnNotEquippable; - case Offsets::INVTYPE_WEAPON: - case Offsets::INVTYPE_SHIELD: - case Offsets::INVTYPE_RANGED: - case Offsets::INVTYPE_2HWEAPON: - case Offsets::INVTYPE_WEAPONMAINHAND: - case Offsets::INVTYPE_WEAPONOFFHAND: - case Offsets::INVTYPE_HOLDABLE: - case Offsets::INVTYPE_THROWN: - case Offsets::INVTYPE_RANGEDRIGHT: - return kTryOnSuccess; // the held arm + ? TryOn::kSuccess + : TryOn::kNotEquippable; + case Outfit::Arm::Held: + return TryOn::kSuccess; default: - return kTryOnNotEquippable; + return TryOn::kNotEquippable; } } @@ -340,45 +232,17 @@ int __fastcall Script_TryOn(void *L) { void *pane = Game::Lua::ResolveDressUpModel(L); if (pane == nullptr) return 0; - // Read before the name search below, which stomps the Lua stack. - const uint32_t bodySlot = Game::Lua::Type(L, 3) == Game::Lua::TYPE_STRING - ? NamedHand(Game::Lua::ToString(L, 3)) - : 0; - const bool enchantGiven = Game::Lua::IsNumber(L, 4) != 0; - uint32_t enchant = enchantGiven ? static_cast(Game::Lua::ToNumber(L, 4)) : 0; - uint32_t itemID = 0; - if (Game::Lua::Type(L, 2) == Game::Lua::TYPE_NUMBER) { - itemID = static_cast(Game::Lua::ToNumber(L, 2)); - } else if (Game::Lua::Type(L, 2) == Game::Lua::TYPE_STRING) { - // Copied: the name search below stomps the Lua stack. - char s[256]; - std::snprintf(s, sizeof(s), "%s", Game::Lua::ToString(L, 2)); - if (std::strstr(s, "enchant:") != nullptr) { - itemID = RecipeItem(s); - } else { - // An item link, `item:` string or numeric string resolves to an ID; - // any other string is the name of an item the player carries. - const Item::Arg::Resolved arg = Item::Arg::ResolveString(s); - Item::Location::ByGUIDResult found{}; - if (arg.itemID != 0) { - itemID = static_cast(arg.itemID); - if (!enchantGiven) - enchant = static_cast(arg.enchant); - } else if (arg.name != nullptr && Item::Location::FindByArg(L, arg, &found)) - itemID = static_cast(Item::ID::FromCGItem(found.item)); - } - } - if (itemID == 0) { - reinterpret_cast(Offsets::FUN_GAMEUI_DISPLAY_ERROR)( - Offsets::GAME_ERROR_NOT_EQUIPPABLE); - Game::Lua::PushNumber(L, kTryOnNotEquippable); + const TryOn::Request request = TryOn::ReadArgs(L); + if (request.itemID == 0) { + TryOn::ShowNotEquippable(); + Game::Lua::PushNumber(L, TryOn::kNotEquippable); return 1; } - g_tryOnBodySlot = bodySlot; - g_tryOnEnchant = enchant; - TryOnItem(pane, nullptr, itemID); + g_tryOnBodySlot = request.bodySlot; + g_tryOnEnchant = request.enchant; + TryOnItem(pane, nullptr, request.itemID); g_tryOnBodySlot = g_tryOnEnchant = 0; - Game::Lua::PushNumber(L, TryOnResult(pane, WornItem(itemID))); + Game::Lua::PushNumber(L, TryOnResult(pane, TryOn::WornItem(request.itemID))); return 1; } @@ -499,10 +363,10 @@ const Game::Doc::Method g_methodDocs[] = { }; const Game::Lua::EnumIntegerEntry kItemTryOnReason[] = { - {"Success", kTryOnSuccess}, - {"WrongRace", kTryOnWrongRace}, - {"NotEquippable", kTryOnNotEquippable}, - {"DataPending", kTryOnDataPending}, + {"Success", TryOn::kSuccess}, + {"WrongRace", TryOn::kWrongRace}, + {"NotEquippable", TryOn::kNotEquippable}, + {"DataPending", TryOn::kDataPending}, }; void RegisterLuaFunctions() { diff --git a/src/model/Outfit.cpp b/src/model/Outfit.cpp index d7e19207..37c7169a 100644 --- a/src/model/Outfit.cpp +++ b/src/model/Outfit.cpp @@ -18,13 +18,24 @@ // // The slot map is 4.3.4's UndressSlot map, from inventory slot to component // slot. +// +// TryOn is DressUpModel::TryOnItem's arms (0x00504540, benilla ui/dressup) +// for a holder that is not a pane, the ModelScene actor: the pane runs the +// engine's worker itself (model/DressUp.cpp). Two differences: +// - An evicted lane is zeroed, as Undress zeroes one. The worker leaves its +// record, which only makes a later try-on detach it again. +// - A tabard keeps no guild emblem. The worker's emblem step reads the +// pane's player. #include "model/Outfit.h" #include "Game.h" #include "Offsets.h" +#include "dbc/Lookup.h" +#include "model/ItemDisplay.h" #include "model/M2.h" #include "object/Resolve.h" +#include "unit/Identity.h" namespace Model::Outfit { namespace { @@ -41,6 +52,8 @@ using GetVisibleItem_t = const void *(__thiscall *)(void *player, uint32_t slot) using GetDescVisual_t = int(__thiscall *)(const void *visibleItem); using CharCompCreate_t = void *(__cdecl *)(); using CharCompClone_t = bool(__thiscall *)(void *component, void *model, void *source); +using CharCompSetItem_t = void(__thiscall *)(void *component, uint32_t slot, uint32_t displayID); +using CanHoldBoth_t = bool(__fastcall *)(uint32_t mainType, uint32_t offType, bool dualWield); constexpr uint32_t kBodySlots[2] = {kBodySlotMainHand, kBodySlotOffHand}; @@ -84,6 +97,29 @@ int ComponentSlot(int slot0) { } } +// TryOnItem's hand for a held type: the named one, else a second one-hander +// in the off hand of a dual wielder, and each type's own hand. +int HandFor(uint32_t type, const Lanes &lanes, const HeldTryOn &held) { + if (held.bodySlot != 0) + return LaneIndex(held.bodySlot); + switch (type) { + case Offsets::INVTYPE_WEAPON: + return lanes[0].itemClass != 0 && + (lanes[0].inventoryType == Offsets::INVTYPE_WEAPON || + lanes[0].inventoryType == Offsets::INVTYPE_WEAPONMAINHAND) && + held.dualWield + ? 1 + : 0; + case Offsets::INVTYPE_SHIELD: + case Offsets::INVTYPE_RANGED: + case Offsets::INVTYPE_WEAPONOFFHAND: + case Offsets::INVTYPE_HOLDABLE: + return 1; + default: // two-hand, main-hand, thrown and right-hand ranged + return 0; + } +} + } // namespace void ReadLanes(void *unit, Lanes &lanes) { @@ -158,6 +194,112 @@ void HangWeapons(void *model, const Lanes &lanes, const Weapons &weapons, bool s } } +uint32_t EnchantVisual(const void *display, uint32_t enchantID) { + if (display == nullptr || + Model::ItemDisplay::OwnVisual(static_cast(display)) != 0) + return 0; + const uint8_t *enchant = DBC::Record(Offsets::VAR_SPELLITEMENCHANT_RECORDS, + Offsets::VAR_SPELLITEMENCHANT_COUNT, enchantID); + return enchant != nullptr + ? Game::Read(enchant, Offsets::OFF_SPELLITEMENCHANT_ITEM_VISUAL) + : 0; +} + +Arm ArmOf(uint32_t inventoryType, uint32_t *componentSlot) { + uint32_t slot = 0; + switch (inventoryType) { + case Offsets::INVTYPE_HEAD: slot = 0; break; + case Offsets::INVTYPE_SHOULDER: slot = 1; break; + case Offsets::INVTYPE_BODY: slot = 2; break; + case Offsets::INVTYPE_CHEST: + case Offsets::INVTYPE_ROBE: slot = 3; break; + case Offsets::INVTYPE_WAIST: slot = 4; break; + case Offsets::INVTYPE_LEGS: slot = 5; break; + case Offsets::INVTYPE_FEET: slot = 6; break; + case Offsets::INVTYPE_WRIST: slot = 7; break; + case Offsets::INVTYPE_HAND: slot = 8; break; + case Offsets::INVTYPE_TABARD: slot = 9; break; + case Offsets::INVTYPE_CLOAK: slot = 10; break; + case Offsets::INVTYPE_WEAPON: + case Offsets::INVTYPE_SHIELD: + case Offsets::INVTYPE_RANGED: + case Offsets::INVTYPE_2HWEAPON: + case Offsets::INVTYPE_WEAPONMAINHAND: + case Offsets::INVTYPE_WEAPONOFFHAND: + case Offsets::INVTYPE_HOLDABLE: + case Offsets::INVTYPE_THROWN: + case Offsets::INVTYPE_RANGEDRIGHT: + return Arm::Held; + case Offsets::INVTYPE_NECK: + case Offsets::INVTYPE_FINGER: + case Offsets::INVTYPE_TRINKET: + case Offsets::INVTYPE_RELIC: + return Arm::Nothing; + default: + return Arm::NotEquippable; + } + if (componentSlot != nullptr) + *componentSlot = slot; + return Arm::Armor; +} + +bool CanDualWield(uint64_t guid) { + return guid != 0 && guid == Unit::Identity::PlayerGuid() && + reinterpret_cast(Offsets::FUN_PLAYER_CAN_DUAL_WIELD)() != 0; +} + +bool TryOn(void *model, void *component, bool armorReady, Lanes &lanes, Weapons &weapons, + const uint8_t *record, const HeldTryOn &held) { + const auto displayID = Game::Read(record, Offsets::OFF_ITEMSTATS_DISPLAY_INFO_ID); + if (model == nullptr || displayID == 0) + return false; + const auto type = Game::Read(record, Offsets::OFF_ITEMSTATS_INVENTORY_TYPE); + uint32_t componentSlot = 0; + switch (ArmOf(type, &componentSlot)) { + case Arm::Armor: + if (component == nullptr || !armorReady) + return false; + reinterpret_cast(Offsets::FUN_CHARCOMP_UNEQUIP_SLOT)(component, + componentSlot); + reinterpret_cast(Offsets::FUN_CHARCOMP_SET_ITEM)(component, + componentSlot, displayID); + return true; + case Arm::Held: + break; + default: + return false; + } + const int lane = HandFor(type, lanes, held); + const int other = 1 - lane; + if (lanes[other].itemClass != 0) { + const uint32_t mainType = lane == 0 ? type : lanes[0].inventoryType; + const uint32_t offType = lane == 0 ? lanes[1].inventoryType : type; + if (!reinterpret_cast(Offsets::FUN_DRESSUPMODEL_CAN_HOLD_BOTH)( + mainType, offType, held.dualWield)) { + ClearLane(model, lanes, other); + weapons.display[other] = nullptr; + weapons.visual[other] = 0; + } + } + ClearLane(model, lanes, lane); + HeldLane &target = lanes[lane]; + target.itemClass = static_cast(Game::Read(record, Offsets::OFF_ITEMSTATS_CLASS)); + target.unknown1[0] = static_cast(Game::Read(record, Offsets::OFF_ITEMSTATS_SUBCLASS)); + target.inventoryType = static_cast(type); + target.sheatheType = static_cast(Game::Read(record, Offsets::OFF_ITEMSTATS_SHEATH)); + const uint8_t *display = DBC::Record(Offsets::VAR_ITEMDISPLAYINFO_RECORDS, + Offsets::VAR_ITEMDISPLAYINFO_COUNT, displayID); + weapons.display[lane] = display; + weapons.visual[lane] = EnchantVisual(display, held.enchant); + if (display == nullptr || held.hide) + return true; + const int attachId = reinterpret_cast(Offsets::FUN_ATTACH_ITEM_MODEL)( + model, display, kBodySlots[lane], target.sheatheType, held.sheathed, + type == Offsets::INVTYPE_SHIELD, type == Offsets::INVTYPE_RANGEDRIGHT); + HangVisual(model, attachId, weapons.visual[lane]); + return true; +} + void UndressSlot(void *model, void *component, Lanes &lanes, int invSlot) { if (model == nullptr || component == nullptr) return; diff --git a/src/model/Outfit.h b/src/model/Outfit.h index 431ac17e..4c06ebb2 100644 --- a/src/model/Outfit.h +++ b/src/model/Outfit.h @@ -70,6 +70,41 @@ void HangVisual(void *model, int attachId, uint32_t visual); void HangWeapons(void *model, const Lanes &lanes, const Weapons &weapons, bool sheathed, bool hide); +// The glow `enchantID` gives an item shown with `display` (an ItemDisplayInfo +// row): none when the display hangs its own (GetItemDescVisual's rule). +uint32_t EnchantVisual(const void *display, uint32_t enchantID); + +// TryOnItem's dispatch (0x00504540) on an item's inventory type. +enum class Arm { + Armor, // a component slot + Held, // a held-item lane + Nothing, // neck, finger, trinket, relic: nothing, and no error + NotEquippable, // anything else: the worker shows ERR_NOT_EQUIPPABLE +}; + +// The arm for `inventoryType`; `componentSlot` receives an armor arm's slot. +Arm ArmOf(uint32_t inventoryType, uint32_t *componentSlot = nullptr); + +// DressUpModel's dual-wield test: the model shows the active player, and the +// player can dual wield. +bool CanDualWield(uint64_t guid); + +// How a held try-on is hung. +struct HeldTryOn { + uint32_t bodySlot = 0; // the hand named (kBodySlot*), or 0 for the worker's choice + bool dualWield = false; + bool sheathed = false; + bool hide = false; + uint32_t enchant = 0; // its glow's SpellItemEnchantment, or 0 +}; + +// TryOnItem's arms on whatever holds the component and the lanes: an armor +// slot is cleared and set on the component, which `armorReady` (the pane's +// ready byte) must allow; a held item evicts an other-lane item it cannot be +// held with, then takes its lane and hangs. True when the item went on. +bool TryOn(void *model, void *component, bool armorReady, Lanes &lanes, Weapons &weapons, + const uint8_t *record, const HeldTryOn &held); + // UndressSlot: the item in one inventory slot (1-based) off the model. A // held lane is detached and zeroed, an armor slot cleared on the component. // Without a component it does nothing, held lanes included (4.3.4's diff --git a/src/model/Scene.h b/src/model/Scene.h index c3848e0f..294270d9 100644 --- a/src/model/Scene.h +++ b/src/model/Scene.h @@ -15,6 +15,7 @@ #include "animation/Object.h" #include "model/Outfit.h" +#include "model/TryOn.h" #include #include @@ -54,6 +55,9 @@ class Actor : public Animation::Object { bool SetUnitModel(uint64_t guid); // Hangs the held items per sheathed and hideWeapons. void ApplyWeapons(); + // Puts the item on the model (Outfit::TryOn), once the model has loaded + // and the item's record is cached; returns the ItemTryOnReason. + int TryOnItem(const Model::TryOn::Request &request); void ClearModel(); void PlayAnimation(); // Shows the kit (0 for none) on the model, replacing the one shown. @@ -75,6 +79,10 @@ class Actor : public Animation::Object { bool autoDress = true; bool sheathed = false; bool hideWeapons = false; + // DressUpModel's ready byte: the model may take armor (not a shapeshifted + // unit's). + bool armorReady = true; + std::vector pendingTryOns; // waiting for the model or the record bool loaded = false; // OnModelLoaded has run for the model bool animFinished = false; diff --git a/src/model/SceneActor.cpp b/src/model/SceneActor.cpp index a70a3e7d..14e5fd3f 100644 --- a/src/model/SceneActor.cpp +++ b/src/model/SceneActor.cpp @@ -76,6 +76,16 @@ // to copy), so usePlayerNativeForm, holdBowString and customRaceID change // nothing. // +// TryOn is DressUpModel's (Model::TryOn's arguments, then TryOnItem's arms, +// Outfit::TryOn) on the actor's component and lanes: a unit's copy or a +// dressed display takes armor and held items, any other model held items +// only. A shapeshifted unit takes no armor, as the pane's ready byte allows +// none. The held items hang per SetSheathed. The pane's worker waits for an +// uncached item record (its cache callback runs it again); the actor waits +// for the record and for its model to load (a display has no component until +// then), and returns DataPending for the first and Success for the second. +// OnModelLoaded runs after the waiting try-ons are on. +// // SetModelByUnitCreatureDisplayID shows the display the unit shows now // (UNIT_FIELD_DISPLAYID) the way SetModelByCreatureDisplayID shows one: a // player shows its race's bare display, a pet its creature's. @@ -122,6 +132,8 @@ #include "api/Documentation.h" #include "dbc/Lookup.h" #include "guid/Guid.h" +#include "item/Data.h" +#include "item/Record.h" #include "model/DisplayInfo.h" #include "model/M2.h" #include "model/VisualKit.h" @@ -191,6 +203,8 @@ void ReleaseModel(Actor &actor) { actor.unitGuid = 0; actor.lanes[0] = actor.lanes[1] = Outfit::HeldLane{}; actor.weapons = Outfit::Weapons{}; + actor.armorReady = true; + actor.pendingTryOns.clear(); actor.loaded = false; actor.animFinished = false; actor.heldPositionMs = -1; @@ -232,6 +246,9 @@ bool Actor::SetUnitModel(uint64_t guid) { if (copy == nullptr) return false; unitGuid = guid; + const auto *descriptor = Game::Read(unit, Offsets::OFF_UNIT_DESCRIPTOR); + armorReady = Game::Read(descriptor, Offsets::OFF_UNIT_FIELD_DISPLAYID) == + Game::Read(descriptor, Offsets::OFF_UNIT_FIELD_NATIVEDISPLAYID); dresser = Outfit::CloneComponent(copy, unit); Outfit::ReadLanes(unit, lanes); Outfit::ReadWeapons(guid, weapons); @@ -257,6 +274,84 @@ void Actor::ApplyWeapons() { Model::DisplayInfo::LightChildren(SceneFrame(), model); } +namespace { + +// The worker's answer for `record` on the actor: what TryOnItem's gates allow. +int TryOnReason(const Actor &actor, const uint8_t *record) { + if (actor.model == nullptr || + Game::Read(record, Offsets::OFF_ITEMSTATS_DISPLAY_INFO_ID) == 0) + return TryOn::kNotEquippable; + switch (Outfit::ArmOf(Game::Read(record, Offsets::OFF_ITEMSTATS_INVENTORY_TYPE))) { + case Outfit::Arm::Armor: // a component now, or the display's once it loads + return (actor.dresser != nullptr || actor.dressRecord != nullptr) && actor.armorReady + ? TryOn::kSuccess + : TryOn::kNotEquippable; + case Outfit::Arm::Held: + return TryOn::kSuccess; + default: + return TryOn::kNotEquippable; + } +} + +// The worker shows ERR_NOT_EQUIPPABLE for a type it has no arm for. +void ShowIfNotEquippable(const uint8_t *record) { + if (Outfit::ArmOf(Game::Read(record, Offsets::OFF_ITEMSTATS_INVENTORY_TYPE)) == + Outfit::Arm::NotEquippable) + TryOn::ShowNotEquippable(); +} + +// The worker on the actor's component and lanes. +void PutOn(Actor &actor, const TryOn::Request &request, const uint8_t *record) { + ShowIfNotEquippable(record); + Outfit::HeldTryOn held; + held.bodySlot = request.bodySlot; + held.dualWield = Outfit::CanDualWield(actor.unitGuid); + held.sheathed = actor.sheathed; + held.hide = actor.hideWeapons; + held.enchant = request.enchant; + if (Outfit::TryOn(actor.model, actor.dresser, actor.armorReady, actor.lanes, actor.weapons, + record, held)) + Model::DisplayInfo::LightChildren(actor.SceneFrame(), actor.model); +} + +// The waiting try-ons whose record is cached, in the order they came. +void PutOnWaiting(Actor &actor) { + auto &waiting = actor.pendingTryOns; + for (size_t i = 0; i < waiting.size();) { + const TryOn::Request request = waiting[i]; + const uint8_t *record = Item::PeekRecord(TryOn::WornItem(request.itemID)); + if (record == nullptr) { + ++i; + continue; + } + waiting.erase(waiting.begin() + static_cast(i)); + PutOn(actor, request, record); + } +} + +} // namespace + +int Actor::TryOnItem(const Model::TryOn::Request &request) { + if (model == nullptr) + return TryOn::kNotEquippable; + const uint8_t *record = Item::PeekRecord(TryOn::WornItem(request.itemID)); + if (record == nullptr) { + Item::Data::WarmCache(request.itemID); + pendingTryOns.push_back(request); + return TryOn::kDataPending; + } + if (loaded) { + PutOn(*this, request, record); + return TryOnReason(*this, record); + } + const int reason = TryOnReason(*this, record); + if (reason == TryOn::kSuccess) + pendingTryOns.push_back(request); + else + ShowIfNotEquippable(record); + return reason; +} + void Actor::ClearModel() { if (model == nullptr) return; @@ -348,11 +443,15 @@ void Actor::Update(float elapsedSec) { ready = false; if (ready && M2::IsLoaded(model, 0, 1)) { loaded = true; + PutOnWaiting(*this); ApplyKit(*this); RunScript(S_ONMODELLOADED, 0); } - } else if (dresser != nullptr) { - Model::DisplayInfo::PumpDresser(dresser); + } else { + if (!pendingTryOns.empty()) + PutOnWaiting(*this); + if (dresser != nullptr) + Model::DisplayInfo::PumpDresser(dresser); } if (model != nullptr && loaded && (paused || globalPaused)) { if (heldPositionMs < 0) @@ -544,6 +643,20 @@ int __fastcall Script_UndressSlot(void *L) { return 0; } +int __fastcall Script_TryOn(void *L) { + Actor *actor = Self(L); + if (actor == nullptr) + return 0; + const TryOn::Request request = TryOn::ReadArgs(L); + if (request.itemID == 0) { + TryOn::ShowNotEquippable(); + Game::Lua::PushNumber(L, TryOn::kNotEquippable); + return 1; + } + Game::Lua::PushNumber(L, actor->TryOnItem(request)); + return 1; +} + int __fastcall Script_SetSheathed(void *L) { Actor *actor = Self(L); if (actor == nullptr) @@ -916,6 +1029,7 @@ const Game::Lua::FrameMethodEntry kMethods[] = { {"Dress", &Script_Dress}, {"Undress", &Script_Undress}, {"UndressSlot", &Script_UndressSlot}, + {"TryOn", &Script_TryOn}, {"SetSheathed", &Script_SetSheathed}, {"GetSheathed", &Script_GetSheathed}, {"SetAutoDress", &Script_SetAutoDress}, @@ -1082,6 +1196,19 @@ const Game::Doc::Function kDress{"Dresses the actor in what its unit wears.", {} const Game::Doc::Function kUndress{"Takes everything off the actor.", kUndressArgs, {}}; const Game::Doc::Function kUndressSlot{ "Takes the item in one inventory slot off the actor.", kUndressSlotArgs, {}}; +const Game::Doc::Field kTryOnArgs[] = { + Req("linkOrItemModifiedAppearanceID", "IDOrLink", + "An item ID, item link, trade-skill recipe link, or the name of an item you carry."), + Opt("handSlotName", "cstring", nullptr, + "MAINHANDSLOT or SECONDARYHANDSLOT: the hand that holds the item."), + Opt("spellEnchantID", "number", nullptr, + "The enchant whose glow a weapon shows. Defaults to the item link's enchant."), +}; +const Game::Doc::Field kTryOnRet[] = {Req("result", "ItemTryOnReason")}; +const Game::Doc::Function kTryOn{ + "Puts an item on the actor. Armor needs a character model. The item goes on once its data " + "and the model have loaded.", + kTryOnArgs, kTryOnRet}; const Game::Doc::Function kSetSheathed{ "Puts the held items in the hands or the sheaths, or hides them.", kSheathedArgs, {}}; const Game::Doc::Function kGetSheathed{"Returns whether the held items are sheathed.", {}, @@ -1164,6 +1291,7 @@ const Game::Doc::Method kMethodDocs[] = { {"Dress", &kDress}, {"Undress", &kUndress}, {"UndressSlot", &kUndressSlot}, + {"TryOn", &kTryOn}, {"SetSheathed", &kSetSheathed}, {"GetSheathed", &kGetSheathed}, {"SetAutoDress", &kSetAutoDress}, diff --git a/src/model/TryOn.cpp b/src/model/TryOn.cpp new file mode 100644 index 00000000..a8d61342 --- /dev/null +++ b/src/model/TryOn.cpp @@ -0,0 +1,133 @@ +// This file is part of ClassicAPI. +// +// ClassicAPI is free software: you can redistribute it and/or modify it under the terms +// of the GNU General Public License as published by the Free Software Foundation, either +// version 3 of the License, or (at your option) any later version. +// +// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +// PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// ClassicAPI. If not, see . + +// TryOn's arguments. Retail's hand argument is an inventory slot name, +// matched the way GetInventorySlotInfo matches it (SStrCmpI over the +// PaperDollItemFrame rows): MainHandSlot or SecondaryHandSlot. Any other +// string names no hand. +// +// A recipe tries on what it makes, as 4.3.4's worker (`TryOnWithVisual`, +// 0x00A05010) does: an item with no inventory type whose spell teaches a +// CREATE_ITEM spell is swapped for the made item, and a trade-skill +// `enchant:` link names the item its TRADESPELL's first effect makes. + +#include "model/TryOn.h" + +#include "Game.h" +#include "Offsets.h" +#include "dbc/Lookup.h" +#include "item/Arg.h" +#include "item/Dressable.h" +#include "item/ID.h" +#include "item/Location.h" +#include "item/Record.h" +#include "model/Outfit.h" + +#include +#include +#include + +namespace Model::TryOn { +namespace { + +// The lane a hand name picks: the PaperDollItemFrame row it names +// (GetInventorySlotInfo's match), if that row is a held slot; else 0. +uint32_t NamedHand(const char *name) { + const auto *records = Game::Read(Offsets::VAR_PAPERDOLLITEMFRAME_RECORDS); + const int count = Game::Read(Offsets::VAR_PAPERDOLLITEMFRAME_COUNT); + if (records == nullptr) + return 0; + for (int i = 0; i < count; ++i) { + const uint8_t *row = records + i * Offsets::OFF_PAPERDOLLITEMFRAME_RECORD_STRIDE; + if (Game::StrCmpI(Game::Read(row, Offsets::OFF_PAPERDOLLITEMFRAME_SLOT_NAME), + name) != 0) + continue; + switch (Game::Read(row, Offsets::OFF_PAPERDOLLITEMFRAME_SLOT_NUMBER)) { + case Offsets::INVSLOT_MAINHAND: return Outfit::kBodySlotMainHand; + case Offsets::INVSLOT_OFFHAND: return Outfit::kBodySlotOffHand; + default: return 0; + } + } + return 0; +} + +// The item a trade-skill recipe makes, from an `enchant:` link: a +// TRADESPELL whose first effect makes an item (4.3.4's test). +uint32_t RecipeItem(const char *link) { + const char *enchant = std::strstr(link, "enchant:"); + if (enchant == nullptr) + return 0; + const long spellID = std::strtol(enchant + 8, nullptr, 10); + if (spellID <= 0) + return 0; + const uint8_t *spell = DBC::Record(Offsets::VAR_SPELL_RECORDS, Offsets::VAR_SPELL_RECORD_COUNT, + static_cast(spellID)); + if (spell == nullptr || + (Game::Read(spell, Offsets::OFF_SPELL_RECORD_ATTRIBUTES) & + Offsets::SPELL_ATTR_TRADESPELL) == 0) + return 0; + return Game::Read(spell, Offsets::OFF_SPELL_RECORD_EFFECT_ITEM_TYPE); +} + +} // namespace + +Request ReadArgs(void *L) { + Request request; + // Read before the name search below, which stomps the Lua stack. + if (Game::Lua::Type(L, 3) == Game::Lua::TYPE_STRING) + request.bodySlot = NamedHand(Game::Lua::ToString(L, 3)); + const bool enchantGiven = Game::Lua::IsNumber(L, 4) != 0; + if (enchantGiven) + request.enchant = static_cast(Game::Lua::ToNumber(L, 4)); + if (Game::Lua::Type(L, 2) == Game::Lua::TYPE_NUMBER) { + request.itemID = static_cast(Game::Lua::ToNumber(L, 2)); + } else if (Game::Lua::Type(L, 2) == Game::Lua::TYPE_STRING) { + // Copied: the name search below stomps the Lua stack. + char s[256]; + std::snprintf(s, sizeof(s), "%s", Game::Lua::ToString(L, 2)); + if (std::strstr(s, "enchant:") != nullptr) { + request.itemID = RecipeItem(s); + } else { + // An item link, `item:` string or numeric string resolves to an ID; + // any other string is the name of an item the player carries. + const Item::Arg::Resolved arg = Item::Arg::ResolveString(s); + Item::Location::ByGUIDResult found{}; + if (arg.itemID != 0) { + request.itemID = static_cast(arg.itemID); + if (!enchantGiven) + request.enchant = static_cast(arg.enchant); + } else if (arg.name != nullptr && Item::Location::FindByArg(L, arg, &found)) + request.itemID = static_cast(Item::ID::FromCGItem(found.item)); + } + } + return request; +} + +uint32_t WornItem(uint32_t itemID) { + const uint8_t *record = Item::PeekRecord(itemID); + if (record != nullptr && + Game::Read(record, Offsets::OFF_ITEMSTATS_INVENTORY_TYPE) == 0) { + const uint32_t made = Item::Dressable::RecipeItem(record); + if (made != 0) + return made; + } + return itemID; +} + +void ShowNotEquippable() { + using DisplayError_t = void(__cdecl *)(uint32_t id, ...); + reinterpret_cast(Offsets::FUN_GAMEUI_DISPLAY_ERROR)( + Offsets::GAME_ERROR_NOT_EQUIPPABLE); +} + +} // namespace Model::TryOn diff --git a/src/model/TryOn.h b/src/model/TryOn.h new file mode 100644 index 00000000..291407ef --- /dev/null +++ b/src/model/TryOn.h @@ -0,0 +1,50 @@ +// This file is part of ClassicAPI. +// +// ClassicAPI is free software: you can redistribute it and/or modify it under the terms +// of the GNU General Public License as published by the Free Software Foundation, either +// version 3 of the License, or (at your option) any later version. +// +// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +// PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// ClassicAPI. If not, see . + +#pragma once + +#include + +// What every `TryOn(item [, handSlotName, spellEnchantID])` takes in front of +// the step that dresses the model: DressUpModel's (model/DressUp.cpp, the +// engine's TryOnItem worker) and the ModelScene actor's (Outfit::TryOn). +namespace Model::TryOn { + +// Retail's Enum.ItemTryOnReason. +enum Reason : int { + kSuccess = 0, + kWrongRace = 1, + kNotEquippable = 2, + kDataPending = 3, +}; + +struct Request { + uint32_t itemID = 0; // 0: nothing to try on + uint32_t bodySlot = 0; // the hand the arguments name (Outfit::kBodySlot*), or 0 + uint32_t enchant = 0; // the SpellItemEnchantment whose glow a weapon shows, or 0 +}; + +// The arguments from stack index 2: 4.3.4's item forms (an item ID, an item +// link or `item:` string, a trade-skill `enchant:` link for the item its +// recipe makes, or the name of an item the player carries), retail's hand +// slot name and enchant. The enchant defaults to the link's. +Request ReadArgs(void *L); + +// The item the model wears for `itemID`: a cached recipe is what it makes +// (4.3.4's TryOnWithVisual swap). +uint32_t WornItem(uint32_t itemID); + +// The worker's answer to an item it cannot put on: ERR_NOT_EQUIPPABLE. +void ShowNotEquippable(); + +} // namespace Model::TryOn