diff --git a/src/Offsets.h b/src/Offsets.h index 8eb42cbc..24f22806 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -6398,6 +6398,28 @@ enum Offsets { // dead unit plays only death animations: CGUnit_C::PlayAnimation // (0x005FE2F0) returns for health below 1 unless the animation is one. FUN_IS_DEATH_ANIMATION = 0x005FDA90, + // The bowstring (Ghidra). RefreshRangedWeaponModel (0x00611E10) gives + // the ranged model of a bow (class 2, subclass 2; 0x00611EDF) the draw + // callback DrawBowstring (0x00611FF0, `__fastcall(model, lightingRecord, + // unit)`, ret 4) with the unit as its user data (0x00611EEE), and sets a + // null callback before it closes that model. SetDrawCallback + // (0x00710DB0, `__thiscall(model, callback, user, list0)`, ret 0xC) + // writes instance +0x1B8 / +0x1BC / +0x1C0; the M2 draw runs the + // callback as a custom element, in list 0 when list0 is set. The + // callback reads only the unit's model (+0xD8, OFF_CGOBJECT_MODEL) and + // its bow-drawn bit (+0xD58 bit 0x4000, set by $BWP at 0x00624B70): two + // lines in the lighting record's colour (+0x54..+0x5C) from the bow's + // $WTT to its $WTB through the body's attachment 35 while drawn, else + // through their midpoint. + FUN_DRAW_BOWSTRING = 0x00611FF0, + FUN_M2MODEL_SET_DRAW_CALLBACK = 0x00710DB0, + OFF_CGUNIT_STATE_FLAGS = 0xD58, + UNIT_STATE_BOW_DRAWN = 0x4000, + // GetSheathAttachment — `int __fastcall(sheatheType, bool mainHand)` + // (0x0047A070): a held item's sheath attachment, or -1 (the + // OFF_ITEMSTATS_SHEATH table). The weapon trail (0x00608D60) finds a + // held item's model with it and FindChild. + FUN_GET_SHEATH_ATTACHMENT = 0x0047A070, // Stuck missiles (Ghidra). The launcher (0x0060A509) flags a ranged // spell's missile 0x4 when the ranged item is a bow, crossbow or thrown // weapon (0x0060A710: class 2, subclass 2, 18 or 16). Launch (0x0061CEB0) diff --git a/src/model/M2.h b/src/model/M2.h index 67aa53e7..9551e0dd 100644 --- a/src/model/M2.h +++ b/src/model/M2.h @@ -269,6 +269,13 @@ inline void AttachItemVisuals(void *model, uint32_t visualID) { reinterpret_cast(Offsets::FUN_ATTACH_ITEM_VISUALS)(model, visualID); } +// CM2Model::SetDrawCallback: a callback the model's draw runs as one of its +// elements, in list 0 when `listZero`; a null callback removes it. +inline void SetDrawCallback(void *model, const void *callback, void *userData, bool listZero) { + using Fn = void(__thiscall *)(void *, const void *, void *, int); + reinterpret_cast(Offsets::FUN_M2MODEL_SET_DRAW_CALLBACK)(model, callback, userData, listZero ? 1 : 0); +} + inline void SetRenderCallback(void *model, const void *callback, void *userData) { using Fn = void(__thiscall *)(void *, const void *, void *); reinterpret_cast(Offsets::FUN_MODEL_INSTANCE_SET_RENDER_CB)(model, callback, userData); diff --git a/src/model/Scene.h b/src/model/Scene.h index e016e147..7b7d632a 100644 --- a/src/model/Scene.h +++ b/src/model/Scene.h @@ -217,6 +217,14 @@ class Actor : public Animation::Object { void *heldAmmo = nullptr; uint32_t heldAmmoDisplay = 0; bool bowDrawn = false; + // The bowstring, as RefreshRangedWeaponModel strings a unit's bow: the + // bow's model (one reference) draws DrawBowstring with `stringUnit`, the + // unit fields the callback reads (the model and the drawn bit). + void *stringedBow = nullptr; + alignas(4) uint8_t stringUnit[Offsets::OFF_CGUNIT_STATE_FLAGS + 4] = {}; + // Strings the bow in the ranged lane, wherever it hangs, and unstrings a + // bow no longer hung; each update, before the draw. + void StringBow(); std::vector spellCasts; std::vector impactModels; std::vector stuckMissiles; // the missiles stuck in this actor diff --git a/src/model/SceneActor.cpp b/src/model/SceneActor.cpp index a2b14cd3..24259fff 100644 --- a/src/model/SceneActor.cpp +++ b/src/model/SceneActor.cpp @@ -178,6 +178,7 @@ #include #include +#include namespace Model::Scene { @@ -225,9 +226,20 @@ void ApplyKit(Actor &actor) { } // Releases the model without scripts (a destroyed actor runs none). +// RefreshRangedWeaponModel's unstring (0x00611E10): a null draw callback +// before the bow's model goes. +void UnstringBow(Actor &actor) { + if (actor.stringedBow == nullptr) + return; + M2::SetDrawCallback(actor.stringedBow, nullptr, nullptr, true); + M2::Release(actor.stringedBow); + actor.stringedBow = nullptr; +} + void ReleaseModel(Actor &actor) { if (actor.model == nullptr) return; + UnstringBow(actor); Casts::Drop(actor); DropKitEffects(actor); M2::SetAnimDoneCallback(actor.model, nullptr, 0, 0); @@ -355,6 +367,36 @@ void Actor::DrawRanged() { bool Actor::LaneSheathed(int i) const { return rangedDrawn ? i != Casts::RangedLane(*this) : sheathed; } +// RefreshRangedWeaponModel's string (0x00611E10) on a bow (class 2, subclass +// 2), found where its lane hangs it as the weapon trail finds a held item +// (0x00608D60: GetSheathAttachment, FindChild). DrawBowstring reads the +// model and the drawn bit from `stringUnit` as from a unit. +void Actor::StringBow() { + using SheathAttachment_t = int(__fastcall *)(uint32_t sheatheType, bool mainHand); + void *bow = nullptr; + const int lane = model != nullptr ? Casts::RangedLane(*this) : -1; + if (lane >= 0 && lanes[lane].itemClass == Offsets::ITEM_CLASS_WEAPON && + lanes[lane].subclass == Offsets::ITEM_SUBCLASS_WEAPON_BOW) { + const int at = LaneSheathed(lane) + ? reinterpret_cast(Offsets::FUN_GET_SHEATH_ATTACHMENT)( + lanes[lane].sheatheType, lane == 0) + : (lane == 0 ? Offsets::ATTACH_HAND_RIGHT : Offsets::ATTACH_HAND_LEFT); + if (at >= 0) + bow = M2::FindChild(model, at); + } + if (bow != stringedBow) { + UnstringBow(*this); + if (bow != nullptr) { + M2::AddRef(bow); + M2::SetDrawCallback(bow, reinterpret_cast(Offsets::FUN_DRAW_BOWSTRING), stringUnit, true); + stringedBow = bow; + } + } + const uint32_t flags = bowDrawn ? static_cast(Offsets::UNIT_STATE_BOW_DRAWN) : 0u; + std::memcpy(stringUnit + Offsets::OFF_CGOBJECT_MODEL, &model, sizeof model); + std::memcpy(stringUnit + Offsets::OFF_CGUNIT_STATE_FLAGS, &flags, sizeof flags); +} + void Actor::TookLane(void *onModel, int i) { ownSheathe[i] = lanes[i].sheatheType; ApplySheatheCategory(onModel, i); @@ -642,6 +684,8 @@ void Actor::Update(float elapsedSec) { RunScript(S_ONANIMFINISHED, 0); } } + if (loaded) + StringBow(); Casts::Update(*this, elapsedSec); if (HasScript(S_ONUPDATE)) { void *L = Game::Lua::State();