Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Commit c0ed2f4

Browse files
committed
Cleaned up Attachable handling so all attachables sit in one list, added template function and unordered_set to clean up hardcoded attachable handling while still avoiding duplicates
Removed the m_DamageMultiplierRedefined flag and used -1 to define default instead Fixed various method comments and other minor stuff
1 parent 5361c54 commit c0ed2f4

File tree

3 files changed

+88
-136
lines changed

3 files changed

+88
-136
lines changed

Entities/MOSRotating.cpp

Lines changed: 44 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ void MOSRotating::Clear()
6262
m_RecoilOffset.Reset();
6363
m_Wounds.clear();
6464
m_Attachables.clear();
65-
m_AllAttachables.clear();
6665
m_Gibs.clear();
6766
m_GibImpulseLimit = 0;
6867
m_GibWoundLimit = 0;
@@ -73,8 +72,7 @@ void MOSRotating::Clear()
7372
m_pTempBitmap = 0;
7473
m_pTempBitmapS = 0;
7574
m_LoudnessOnGib = 1;
76-
m_DamageMultiplier = 1;
77-
m_DamageMultiplierRedefined = false;
75+
m_DamageMultiplier = -1;
7876
m_StringValueMap.clear();
7977
m_NumberValueMap.clear();
8078
m_ObjectValueMap.clear();
@@ -91,6 +89,8 @@ int MOSRotating::Create()
9189
if (MOSprite::Create() < 0)
9290
return -1;
9391

92+
if (m_DamageMultiplier < 0.0F) { m_DamageMultiplier = 1; }
93+
9494
if (m_pAtomGroup && m_pAtomGroup->AutoGenerate()/* && m_pAtomGroup->GetAtomCount() == 0*/)
9595
m_pAtomGroup->Create(this);
9696
else if (m_pAtomGroup)
@@ -197,7 +197,7 @@ int MOSRotating::Create(ContentFile spriteFile,
197197
const unsigned long lifetime)
198198
{
199199
MOSprite::Create(spriteFile, frameCount, mass, position, velocity, lifetime);
200-
200+
if (m_DamageMultiplier < 0.0F) { m_DamageMultiplier = 1; }
201201
return 0;
202202
}
203203

@@ -238,28 +238,20 @@ int MOSRotating::Create(const MOSRotating &reference)
238238
m_RecoilOffset = reference.m_RecoilOffset;
239239

240240
// Wound emitter copies
241-
AEmitter *pWound = 0;
242-
for (list<AEmitter *>::const_iterator itr = reference.m_Wounds.begin(); itr != reference.m_Wounds.end(); ++itr)
243-
{
244-
pWound = dynamic_cast<AEmitter *>((*itr)->Clone());
245-
AddWound(pWound, pWound->GetParentOffset());
246-
pWound = 0;
241+
for (AEmitter *wound : reference.m_Wounds) {
242+
AddWound(dynamic_cast<AEmitter *>(wound), wound->GetParentOffset());
247243
}
248244

249245
// Attachable copies
250-
m_AllAttachables.clear();
251-
Attachable *pAttachable = 0;
252-
for (list<Attachable *>::const_iterator aItr = reference.m_Attachables.begin(); aItr != reference.m_Attachables.end(); ++aItr)
253-
{
254-
pAttachable = dynamic_cast<Attachable *>((*aItr)->Clone());
255-
AddAttachable(pAttachable, pAttachable->GetParentOffset());
256-
pAttachable = 0;
246+
for (const Attachable *attachable : reference.m_Attachables) {
247+
if (m_AlreadyCopiedAttachableUniqueIDs.find(attachable->GetUniqueID()) != m_AlreadyCopiedAttachableUniqueIDs.end()) {
248+
AddAttachable(dynamic_cast<Attachable *>(attachable->Clone()));
249+
}
257250
}
258251

259252
// Gib copies
260-
for (list<Gib>::const_iterator gItr = reference.m_Gibs.begin(); gItr != reference.m_Gibs.end(); ++gItr)
261-
{
262-
m_Gibs.push_back(*gItr);
253+
for (const Gib &gib : reference.m_Gibs) {
254+
m_Gibs.push_back(gib);
263255
}
264256

265257
m_StringValueMap = reference.m_StringValueMap;
@@ -273,7 +265,6 @@ int MOSRotating::Create(const MOSRotating &reference)
273265
m_LoudnessOnGib = reference.m_LoudnessOnGib;
274266

275267
m_DamageMultiplier = reference.m_DamageMultiplier;
276-
m_DamageMultiplierRedefined = reference.m_DamageMultiplierRedefined;
277268

278269
/* Allocated in lazy fashion as needed when drawing flipped
279270
if (!m_pFlipBitmap && m_aSprite[0])
@@ -346,7 +337,6 @@ int MOSRotating::ReadProperty(std::string propName, Reader &reader)
346337
reader >> m_LoudnessOnGib;
347338
else if (propName == "DamageMultiplier") {
348339
reader >> m_DamageMultiplier;
349-
m_DamageMultiplierRedefined = true;
350340
} else if (propName == "AddCustomValue") {
351341
ReadCustomValueProperty(reader);
352342
} else
@@ -1050,7 +1040,6 @@ void MOSRotating::GibThis(Vector impactImpulse, float internalBlast, MovableObje
10501040
}
10511041
// Clear the attachables list, all the attachables ownership have been handed to the movableman
10521042
m_Attachables.clear();
1053-
m_AllAttachables.clear();
10541043

10551044
// Play the gib sound
10561045
m_GibSound.Play(m_Pos);
@@ -1618,122 +1607,68 @@ void MOSRotating::UpdateChildMOIDs(vector<MovableObject *> &MOIDIndex,
16181607
MOSprite::UpdateChildMOIDs(MOIDIndex, m_RootMOID, makeNewMOID);
16191608
}
16201609

1610+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16211611

1622-
/// <summary>
1623-
/// Attaches the passed in Attachable and adds it to the list of attachables, not changing its parent offset and not treating it as hardcoded.
1624-
/// </summary>
1625-
/// <param name="pAttachable">The Attachable to attach.</param>
1626-
void MOSRotating::AddAttachable(Attachable *pAttachable)
1627-
{
1628-
if (pAttachable)
1629-
{
1630-
AddAttachable(pAttachable, pAttachable->GetParentOffset());
1612+
void MOSRotating::AddAttachable(Attachable *attachable) {
1613+
if (attachable) {
1614+
AddAttachable(attachable, attachable->GetParentOffset());
16311615
}
16321616
}
16331617

1618+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16341619

1635-
/// <summary>
1636-
/// Attaches the passed in Attachable and adds it to the list of attachables, changing its parent offset to the passed in Vector but not treating it as hardcoded.
1637-
/// </summary>
1638-
/// <param name="pAttachable">The Attachable to add.</param>
1639-
/// <param name="parentOffsetToSet">The vector to set as the Attachable's parent offset.</param>
1640-
void MOSRotating::AddAttachable(Attachable *pAttachable, const Vector& parentOffsetToSet)
1641-
{
1642-
AddAttachable(pAttachable, parentOffsetToSet, false);
1643-
}
1644-
1645-
1646-
/// <summary>
1647-
/// Attaches the passed in Attachable and adds it to the list of attachables, not changing its parent offset but treating it as hardcoded depending on the passed in boolean.
1648-
/// </summary>
1649-
/// <param name="pAttachable">The Attachable to add.</param>
1650-
/// <param name="isHardcodedAttachable">Whether or not the Attachable should be treated as hardcoded.</param>
1651-
void MOSRotating::AddAttachable(Attachable *pAttachable, bool isHardcodedAttachable)
1652-
{
1653-
if (pAttachable)
1654-
{
1655-
AddAttachable(pAttachable, pAttachable->GetParentOffset(), isHardcodedAttachable);
1656-
}
1657-
}
1658-
1659-
1660-
/// <summary>
1661-
/// Attaches the passed in Attachable and adds it to the list of attachables, changing its parent offset to the passed in Vector and treating it as hardcoded depending on the passed in boolean.
1662-
/// </summary>
1663-
/// <param name="pAttachable">The Attachable to add.</param>
1664-
/// <param name="parentOffsetToSet">The vector to set as the Attachable's parent offset.</param>
1665-
/// <param name="isHardcodedAttachable">Whether or not the Attachable should be treated as hardcoded.</param>
1666-
void MOSRotating::AddAttachable(Attachable *pAttachable, const Vector & parentOffsetToSet, bool isHardcodedAttachable) {
1667-
if (pAttachable) {
1668-
pAttachable->SetParentOffset(parentOffsetToSet);
1669-
pAttachable->SetParent(this);
1620+
void MOSRotating::AddAttachable(Attachable *attachable, const Vector& parentOffsetToSet) {
1621+
if (attachable) {
1622+
attachable->SetParentOffset(parentOffsetToSet);
1623+
attachable->SetParent(this);
16701624

16711625
// Set the attachable's subgroup ID to it's Unique ID to avoid any possible conflicts when adding atoms to parent group.
1672-
pAttachable->SetAtomSubgroupID(pAttachable->GetUniqueID());
1673-
1674-
if (!isHardcodedAttachable) {
1675-
m_Attachables.push_back(pAttachable);
1676-
}
1677-
m_AllAttachables.push_back(pAttachable);
1626+
attachable->SetAtomSubgroupID(attachable->GetUniqueID());
1627+
m_Attachables.push_back(attachable);
16781628
}
16791629
}
16801630

1631+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16811632

1682-
/// <summary>
1683-
/// Detaches the Attachable corresponding to the passed in UniqueId, and removes it from the appropriate attachable lists
1684-
/// </summary>
1685-
/// <param name="attachableUniqueId">The UniqueId of the the attachable to remove</param>
1686-
/// <returns>False if the attachable is invalid, otherwise true</returns>
1687-
bool MOSRotating::RemoveAttachable(long attachableUniqueId)
1688-
{
1689-
MovableObject *attachableAsMovableObject = g_MovableMan.FindObjectByUniqueID(attachableUniqueId);
1690-
if (attachableAsMovableObject)
1691-
{
1633+
bool MOSRotating::RemoveAttachable(long attachableUniqueID) {
1634+
MovableObject *attachableAsMovableObject = g_MovableMan.FindObjectByUniqueID(attachableUniqueID);
1635+
if (attachableAsMovableObject) {
16921636
return RemoveAttachable((Attachable *)attachableAsMovableObject);
16931637
}
16941638
return false;
16951639
}
16961640

1641+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16971642

1698-
/// <summary>
1699-
/// Detaches the passed in Attachable and removes it from the appropriate attachable lists
1700-
/// </summary>
1701-
/// <param name="pAttachable">The attachable to remove</param>
1702-
/// <returns>False if the attachable is invalid, otherwise true</returns>
1703-
bool MOSRotating::RemoveAttachable(Attachable *pAttachable) {
1704-
if (pAttachable) {
1643+
bool MOSRotating::RemoveAttachable(Attachable *attachable) {
1644+
if (attachable) {
17051645
if (m_Attachables.size() > 0) {
1706-
m_Attachables.remove(pAttachable);
1646+
m_Attachables.remove(attachable);
17071647
}
1708-
if (m_AllAttachables.size() > 0) {
1709-
m_AllAttachables.remove(pAttachable);
1710-
}
1711-
if (pAttachable->ToDeleteWithParent()) {
1712-
pAttachable->SetToDelete();
1648+
if (attachable->ToDeleteWithParent()) {
1649+
attachable->SetToDelete();
17131650
} else {
1714-
pAttachable->SetParent(nullptr);
1651+
attachable->SetParent(nullptr);
17151652
}
17161653
return true;
17171654
}
17181655
return false;
17191656
}
17201657

1658+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
17211659

1722-
/// <summary>
1723-
/// Either detaches or deletes all of this MOSRotating's attachables
1724-
/// </summary>
1725-
/// <param name="destroy">Whether to detach or delete the attachables. Setting this to true deletes them, setting it to false detaches them</param>
17261660
void MOSRotating::DetachOrDestroyAll(bool destroy) {
1727-
for (list<Attachable *>::const_iterator aItr = m_AllAttachables.begin(); aItr != m_AllAttachables.end(); ++aItr) {
1728-
if (destroy)
1729-
delete (*aItr);
1730-
else
1731-
(*aItr)->SetParent(nullptr);
1732-
}
1733-
1734-
m_AllAttachables.clear();
1661+
for (Attachable *attachable : m_Attachables) {
1662+
if (destroy) {
1663+
delete attachable;
1664+
} else {
1665+
attachable->SetParent(nullptr);
1666+
}
1667+
}
1668+
m_Attachables.clear();
17351669
}
17361670

1671+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
17371672

17381673
//////////////////////////////////////////////////////////////////////////////////////////
17391674
// Virtual method: GetMOIDs

Entities/MOSRotating.h

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -425,19 +425,38 @@ ClassInfoGetters
425425

426426
virtual void ApplyImpulses();
427427

428+
/// <summary>
429+
/// Adds the passed in Attachable the list of attachables and sets its parent to this MOSRotating.
430+
/// </summary>
431+
/// <param name="attachable">The Attachable to add.</param>
432+
void AddAttachable(Attachable *attachable);
428433

429-
void AddAttachable(Attachable *pAttachable);
430-
431-
void AddAttachable(Attachable *pAttachable, const Vector& parentOffsetToSet);
432-
433-
void AddAttachable(Attachable *pAttachable, bool isHardcodedAttachable);
434-
435-
void AddAttachable(Attachable *pAttachable, const Vector& parentOffsetToSet, bool isHardcodedAttachable);
434+
/// <summary>
435+
/// Attaches the passed in Attachable and adds it to the list of attachables, changing its parent offset to the passed in Vector but not treating it as hardcoded.
436+
/// Adds the passed in Attachable the list of attachables, changes its parent offset to the passed in Vector, and sets its parent to this MOSRotating.
437+
/// </summary>
438+
/// <param name="attachable">The Attachable to add.</param>
439+
/// <param name="parentOffsetToSet">The Vector to set as the Attachable's parent offset.</param>
440+
void AddAttachable(Attachable *attachable, const Vector& parentOffsetToSet);
436441

437-
bool RemoveAttachable(long attachableUniqueId);
442+
/// <summary>
443+
/// Detaches the Attachable corresponding to the passed in UniqueID, and removes it from the appropriate attachable lists.
444+
/// </summary>
445+
/// <param name="attachableUniqueID">The UniqueID of the the attachable to remove.</param>
446+
/// <returns>False if the attachable is invalid, otherwise true.</returns>
447+
bool RemoveAttachable(long attachableUniqueID);
438448

439-
bool RemoveAttachable(Attachable *pAttachable);
449+
/// <summary>
450+
/// Detaches the passed in Attachable and removes it from the appropriate attachable lists.
451+
/// </summary>
452+
/// <param name="attachable">The attachable to remove.</param>
453+
/// <returns>False if the attachable is invalid, otherwise true.</returns>
454+
bool RemoveAttachable(Attachable *attachable);
440455

456+
/// <summary>
457+
/// Either detaches or deletes all of this MOSRotating's attachables.
458+
/// </summary>
459+
/// <param name="destroy">Whether to detach or delete the attachables. Setting this to true deletes them, setting it to false detaches them.</param>
441460
void DetachOrDestroyAll(bool destroy);
442461

443462

@@ -772,16 +791,6 @@ ClassInfoGetters
772791

773792
virtual float GetDamageMultiplier() const { return m_DamageMultiplier; }
774793

775-
//////////////////////////////////////////////////////////////////////////////////////////
776-
// Method: IsDamageMultiplierRedefined
777-
//////////////////////////////////////////////////////////////////////////////////////////
778-
// Description: Indicates whether the damage multiplier was altered in the .INI definition.
779-
// If not, CC will apply default values during actor construction.
780-
// Arguments: None.
781-
// Return value: Current multiplier value.
782-
783-
virtual bool IsDamageMultiplierRedefined() const { return m_DamageMultiplierRedefined; }
784-
785794

786795
//////////////////////////////////////////////////////////////////////////////////////////
787796
// Method: GetTravelImpulse
@@ -808,6 +817,17 @@ ClassInfoGetters
808817

809818
protected:
810819

820+
/// <summary>
821+
/// To be called during Create(reference):
822+
/// Clones a hardcoded attachable, setting the appropriate member variable, and calling AddAttachable.
823+
/// Also adds the reference attachable's UniqueID to the std::unordered_set of already copied attachable unique IDs so it doesn't get duplicated.
824+
/// </summary>
825+
/// <param name="referenceAttachable">The relevant hardcoded attachable belonging to the reference object being cloned.</param>
826+
/// <param name="hardcodedAttachableSetter">A std::function containing the setter for the given hardcoded attachable. Aside from setting the relevant member variable, this setter should call AddAttachable.</param>
827+
template<class T> void CloneHardcodedAttachable(Attachable *referenceAttachable, T hardcodedAttachableSetter) {
828+
m_AlreadyCopiedAttachableUniqueIDs.insert(referenceAttachable->GetUniqueID());
829+
hardcodedAttachableSetter(*this, dynamic_cast<Attachable *>(referenceAttachable->Clone()));
830+
}
811831

812832
//////////////////////////////////////////////////////////////////////////////////////////
813833
// Method: ApplyAttachableForces
@@ -869,12 +889,11 @@ ClassInfoGetters
869889
Vector m_RecoilForce;
870890
// The vector that the recoil offsets the sprite when m_Recoiled is true.
871891
Vector m_RecoilOffset;
872-
// The list of wound AEmitters currently attached to this MOSRotating, and owned here as well
892+
// The list of wound AEmitters currently attached to this MOSRotating, and owned here as well.
873893
std::list<AEmitter *> m_Wounds;
874-
// The list of general Attachables currently attached and Owned by this.
894+
// The list of Attachables currently attached and Owned by this.
875895
std::list<Attachable *> m_Attachables;
876-
// The list of all Attachables, including both hardcoded attachables and those added through ini or lua
877-
std::list<Attachable *> m_AllAttachables;
896+
std::unordered_set<unsigned long> m_AlreadyCopiedAttachableUniqueIDs; //<! An unordered set of Unique IDs, used to avoid duplicating hardcoded attachables (i.e. head, legs, etc.) when cloning.
878897
// The list of Gib:s this will create when gibbed
879898
std::list<Gib> m_Gibs;
880899
// The amount of impulse force required to gib this, in kg * (m/s). 0 means no limit
@@ -894,10 +913,8 @@ ClassInfoGetters
894913
// Map to store any object pointers
895914
std::map<std::string, Entity *> m_ObjectValueMap;
896915

897-
// Damage mutliplier for this attachable
916+
// Damage multiplier for this attachable
898917
float m_DamageMultiplier;
899-
// Whether damage multiplier for this attachable was redefined in .ini
900-
bool m_DamageMultiplierRedefined;
901918

902919
// Intermediary drawing bitmap used to flip rotating bitmaps. Owned!
903920
BITMAP *m_pFlipBitmap;

Managers/LuaMan.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ int LuaMan::Create()
775775
.def("AddEmitter", (void (MOSRotating::*)(Attachable *attachableToAdd, const Vector &parentOffset))&MOSRotating::AddAttachable, adopt(_2))
776776
.def("RemoveEmitter", (bool (MOSRotating::*)(Attachable *attachableToRemove))&MOSRotating::RemoveAttachable)
777777
.def("RemoveEmitter", (bool (MOSRotating::*)(long uniqueIDOfAttachableToRemove))&MOSRotating::RemoveAttachable)
778-
.def_readonly("Attachables", &MOSRotating::m_AllAttachables, return_stl_iterator)
778+
.def_readonly("Attachables", &MOSRotating::m_Attachables, return_stl_iterator)
779779
.def_readonly("Wounds", &MOSRotating::m_Wounds, return_stl_iterator),
780780

781781
CONCRETELUABINDING(Attachable, MOSRotating)

0 commit comments

Comments
 (0)