feat(model): ModelScene bows draw their string

A 1.12.1 bow's string is not part of its model. RefreshRangedWeaponModel
(0x00611E10) gives the ranged model of a bow (class 2, subclass 2) the
draw callback DrawBowstring (0x00611FF0) through SetDrawCallback
(0x00710DB0), with the unit as its user data, and sets a null callback
before it closes that model. The M2 draw runs the callback as one of the
model's elements. It draws two lines in the lighting record's colour
from the bow's $WTT event to its $WTB event: through the body's
attachment 35 while the bow is drawn (+0xD58 bit 0x4000, set by $BWP),
else through their midpoint.

The callback reads only the unit's model (+0xD8) and that bit, so an
actor keeps a block shaped like those two unit fields and hands it to
the engine's own DrawBowstring. Each update the actor finds the bow in
its ranged lane where the lane hangs it, in the hand or at the sheath
point the weapon trail's lookup gives (GetSheathAttachment 0x0047A070,
then FindChild), holds a reference on it, and strings it. A bow no
longer hung, or a released model, is unstrung first. The drawn bit
follows the actor's bow pull and release.
This commit is contained in:
Brues
2026-11-09 11:51:32 -06:00
parent 846aa44330
commit c6e76306ab
4 changed files with 81 additions and 0 deletions
+22
View File
@@ -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)
+7
View File
@@ -269,6 +269,13 @@ inline void AttachItemVisuals(void *model, uint32_t visualID) {
reinterpret_cast<Fn>(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<Fn>(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<Fn>(Offsets::FUN_MODEL_INSTANCE_SET_RENDER_CB)(model, callback, userData);
+8
View File
@@ -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<SpellCast> spellCasts;
std::vector<ImpactModel> impactModels;
std::vector<StuckMissile> stuckMissiles; // the missiles stuck in this actor
+44
View File
@@ -178,6 +178,7 @@
#include <cmath>
#include <cstdint>
#include <cstring>
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<SheathAttachment_t>(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<const void *>(Offsets::FUN_DRAW_BOWSTRING), stringUnit, true);
stringedBow = bow;
}
}
const uint32_t flags = bowDrawn ? static_cast<uint32_t>(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();