Skip to content

Commit 55c46d9

Browse files
committed
Replicate in Generals
1 parent 069978a commit 55c46d9

File tree

12 files changed

+185
-188
lines changed

12 files changed

+185
-188
lines changed

Generals/Code/GameEngine/Include/Common/Player.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ class Player : public Snapshot
608608
// add to the player's current selection this hotkey team.
609609
void processAddTeamGameMessage(Int hotkeyNum, GameMessage *msg);
610610

611-
// returns an AIGroup object that is the currently selected group.
611+
// fills an AIGroup object that is the currently selected group.
612612
void getCurrentSelectionAsAIGroup(AIGroup *group);
613613

614614
// sets the currently selected group to be the given AIGroup

Generals/Code/GameEngine/Include/GameLogic/AI.h

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
#include "Common/GameType.h"
3838
#include "GameLogic/Damage.h"
3939
#include "Common/STLTypedefs.h"
40+
#include "refcount.h"
41+
#include "ref_ptr.h"
4042

4143
class AIGroup;
4244
class AttackPriorityInfo;
@@ -265,7 +267,7 @@ class AI : public SubsystemInterface, public Snapshot
265267
void loadPostProcess( void );
266268

267269
// AI Groups -----------------------------------------------------------------------------------------------
268-
AIGroup *createGroup( void ); ///< instantiate a new AI Group
270+
RefCountPtr<AIGroup> createGroup( void ); ///< instantiate a new AI Group
269271
void destroyGroup( AIGroup *group ); ///< destroy the given AI Group
270272
AIGroup *findGroup( UnsignedInt id ); ///< return the AI Group with the given ID
271273

@@ -851,6 +853,10 @@ class AIGroup : public MemoryPoolObject, public Snapshot
851853
void xfer( Xfer *xfer );
852854
void loadPostProcess( void );
853855

856+
void Add_Ref() const { m_refCount.Add_Ref(); }
857+
void Release_Ref() const { m_refCount.Release_Ref(destroy, this); }
858+
void Num_Refs() const { m_refCount.Num_Refs(); }
859+
854860
void groupMoveToPosition( const Coord3D *pos, Bool addWaypoint, CommandSourceType cmdSource );
855861
void groupMoveToAndEvacuate( const Coord3D *pos, CommandSourceType cmdSource ); ///< move to given position(s)
856862
void groupMoveToAndEvacuateAndExit( const Coord3D *pos, CommandSourceType cmdSource ); ///< move to given position & unload transport.
@@ -938,13 +944,15 @@ class AIGroup : public MemoryPoolObject, public Snapshot
938944

939945
void add( Object *obj ); ///< add object to group
940946

941-
// returns true if remove destroyed the group for us.
947+
// Returns true if the group was emptied.
942948
Bool remove( Object *obj);
949+
950+
void removeAll( void );
943951

944952
// If the group contains any objects not owned by ownerPlayer, return TRUE.
945953
Bool containsAnyObjectsNotOwnedByPlayer( const Player *ownerPlayer );
946954

947-
// Remove any objects that aren't owned by the player, and return true if the group was destroyed due to emptiness
955+
// Removes any objects that aren't owned by the player, and returns true if the group was emptied.
948956
Bool removeAnyObjectsNotOwnedByPlayer( const Player *ownerPlayer );
949957

950958
UnsignedInt getID( void );
@@ -974,6 +982,11 @@ class AIGroup : public MemoryPoolObject, public Snapshot
974982
friend class AI;
975983
AIGroup( void );
976984

985+
static void destroy(AIGroup* self)
986+
{
987+
TheAI->destroyGroup(self);
988+
}
989+
977990
void recompute( void ); ///< recompute various group info, such as speed, leader, etc
978991

979992
ListObjectPtr m_memberList; ///< the list of member Objects
@@ -982,6 +995,8 @@ class AIGroup : public MemoryPoolObject, public Snapshot
982995
Real m_speed; ///< maximum speed of group (slowest member)
983996
Bool m_dirty; ///< "dirty bit" - if true then group speed, leader, needs recompute
984997

998+
RefCountValue<UnsignedShort> m_refCount; ///< the reference counter
999+
9851000
UnsignedInt m_id; ///< the unique ID of this group
9861001
Path *m_groundPath; ///< Group ground path.
9871002

Generals/Code/GameEngine/Include/GameLogic/Object.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#define _OBJECT_H_
3232

3333
#include "Lib/BaseType.h"
34+
#include "ref_ptr.h"
3435

3536
#include "Common/Geometry.h"
3637
#include "Common/Snapshot.h"
@@ -663,7 +664,7 @@ class Object : public Thing, public Snapshot
663664

664665
GeometryInfo m_geometryInfo;
665666

666-
AIGroup* m_group; ///< if non-NULL, we are part of this group of agents
667+
RefCountPtr<AIGroup> m_group; ///< if non-NULL, we are part of this group of agents
667668

668669
// These will last for my lifetime. I will reuse them and reset them. The truly dynamic ones are in PartitionManager
669670
SightingInfo *m_partitionLastLook; ///< Where and for whom I last looked, so I can undo its effects when I stop

Generals/Code/GameEngine/Source/GameLogic/AI/AI.cpp

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -330,18 +330,11 @@ void AI::reset( void )
330330
m_aiData = m_aiData->m_next;
331331
delete cur;
332332
}
333-
while (m_groupList.size())
334-
{
335-
AIGroup *groupToRemove = m_groupList.front();
336-
if (groupToRemove)
337-
{
338-
destroyGroup(groupToRemove);
339-
}
340-
else
341-
{
342-
m_groupList.pop_front(); // NULL group, just kill from list. Shouldn't really happen, but just in case.
343-
}
344-
}
333+
334+
DEBUG_ASSERTCRASH(m_groupList.empty(), ("AI::m_groupList is expected empty already\n"));
335+
336+
m_groupList.clear(); // Clear just in case...
337+
345338
m_nextGroupID = 0;
346339
m_nextFormationID = NO_FORMATION_ID;
347340
getNextFormationID(); // increment once past NO_FORMATION_ID. jba.
@@ -441,14 +434,14 @@ void AI::parseAiDataDefinition( INI* ini )
441434
/**
442435
* Create a new AI Group
443436
*/
444-
AIGroup *AI::createGroup( void )
437+
RefCountPtr<AIGroup> AI::createGroup( void )
445438
{
446439
// create a new instance
447-
AIGroup *group = newInstance(AIGroup);
440+
RefCountPtr<AIGroup> group = RefCountPtr<AIGroup>::Create_NoAddRef(newInstance(AIGroup));
448441

449442
// add it to the list
450443
// DEBUG_LOG(("***AIGROUP %x is being added to m_groupList.\n", group ));
451-
m_groupList.push_back( group );
444+
m_groupList.push_back( group.Peek() );
452445

453446
return group;
454447
}

Generals/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,8 @@ AIGroup::~AIGroup()
9292
{
9393
// DEBUG_LOG(("***AIGROUP %x is being destructed.\n", this));
9494
// disassociate each member from the group
95-
std::list<Object *>::iterator i;
96-
for( i = m_memberList.begin(); i != m_memberList.end(); /* empty */ )
97-
{
98-
Object *member = *i;
99-
if (member)
100-
{
101-
member->leaveGroup();
102-
i = m_memberList.begin(); // jump back to the beginning, cause ai->leaveGroup will remove this element.
103-
}
104-
else
105-
{
106-
i = m_memberList.erase(i);
107-
}
108-
}
95+
removeAll();
96+
10997
if (m_groundPath) {
11098
deleteInstance(m_groundPath);
11199
m_groundPath = NULL;
@@ -223,15 +211,33 @@ Bool AIGroup::remove( Object *obj )
223211
// list has changed, properties need recomputation
224212
m_dirty = true;
225213

226-
// if the group is empty, no-one is using it any longer, so destroy it
227214
if (isEmpty()) {
228-
TheAI->destroyGroup( this );
229215
return TRUE;
230216
}
231217

232218
return FALSE;
233219
}
234220

221+
/**
222+
* Remove all objects from group
223+
*/
224+
void AIGroup::removeAll( void )
225+
{
226+
std::list<Object *> memberList;
227+
memberList.swap(m_memberList);
228+
m_memberListSize = 0;
229+
230+
std::list<Object *>::iterator i;
231+
for ( i = memberList.begin(); i != memberList.end(); ++i )
232+
{
233+
Object *member = *i;
234+
if (member)
235+
member->leaveGroup();
236+
}
237+
238+
m_dirty = true;
239+
}
240+
235241
/**
236242
* If the group contains any objects not owned by ownerPlayer, return TRUE.
237243
*/

Generals/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -893,11 +893,11 @@ void AIPlayer::guardSupplyCenter( Team *team, Int minSupplies )
893893
}
894894
if (warehouse) {
895895

896-
AIGroup* theGroup = TheAI->createGroup();
896+
RefCountPtr<AIGroup> theGroup = TheAI->createGroup();
897897
if (!theGroup) {
898898
return;
899899
}
900-
team->getTeamAsAIGroup(theGroup);
900+
team->getTeamAsAIGroup(theGroup.Peek());
901901
Coord3D location = *warehouse->getPosition();
902902
// It's probably a defensive move - position towards the enemy.
903903
Region2D bounds;
@@ -2467,9 +2467,9 @@ void AIPlayer::checkReadyTeams( void )
24672467
/*
24682468
if (team->m_team->getPrototype()->getTemplateInfo()->m_hasHomeLocation &&
24692469
!team->m_reinforcement) {
2470-
AIGroup* theGroup = TheAI->createGroup();
2470+
RefCountPtr<AIGroup> theGroup = TheAI->createGroup();
24712471
if (theGroup) {
2472-
team->m_team->getTeamAsAIGroup(theGroup);
2472+
team->m_team->getTeamAsAIGroup(theGroup.Peek());
24732473
Coord3D destination = team->m_team->getPrototype()->getTemplateInfo()->m_homeLocation;
24742474
theGroup->groupTightenToPosition( &destination, false, CMD_FROM_AI );
24752475
team->m_frameStarted = TheGameLogic->getFrame();

Generals/Code/GameEngine/Source/GameLogic/AI/AIStates.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4050,8 +4050,8 @@ StateReturnType AIFollowWaypointPathState::update()
40504050
if (m_moveAsGroup) {
40514051
if (obj->getControllingPlayer()->isSkirmishAIPlayer()) {
40524052
Team *team = obj->getTeam();
4053-
AIGroup *group = TheAI->createGroup();
4054-
team->getTeamAsAIGroup(group);
4053+
RefCountPtr<AIGroup> group = TheAI->createGroup();
4054+
team->getTeamAsAIGroup(group.Peek());
40554055

40564056
Coord3D pos;
40574057
group->getCenter(&pos);

Generals/Code/GameEngine/Source/GameLogic/Object/Object.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -563,9 +563,8 @@ Object::~Object()
563563
ThePartitionManager->unRegisterObject( this );
564564

565565
// if we are in a group, remove us
566-
if (m_group)
567-
m_group->remove( this );
568-
566+
leaveGroup();
567+
569568
// note, do NOT free these, there are just a shadow copy!
570569
m_ai = NULL;
571570
m_physics = NULL;
@@ -3841,7 +3840,7 @@ void Object::xfer( Xfer *xfer )
38413840
}
38423841

38433842
// Doesn't need to be saved. These are created as needed. jba.
3844-
//AIGroup* m_group; ///< if non-NULL, we are part of this group of agents
3843+
//m_group;
38453844

38463845
// don't need to save m_partitionData.
38473846
DEBUG_ASSERTCRASH(!(xfer->getXferMode() == XFER_LOAD && m_partitionData == NULL), ("should not be in partitionmgr yet"));
@@ -5446,7 +5445,7 @@ RadarPriorityType Object::getRadarPriority( void ) const
54465445
// ------------------------------------------------------------------------------------------------
54475446
AIGroup *Object::getGroup(void)
54485447
{
5449-
return m_group;
5448+
return m_group.Peek();
54505449
}
54515450

54525451
//-------------------------------------------------------------------------------------------------
@@ -5456,7 +5455,7 @@ void Object::enterGroup( AIGroup *group )
54565455
// if we are in another group, remove ourselves from it first
54575456
leaveGroup();
54585457

5459-
m_group = group;
5458+
m_group = RefCountPtr<AIGroup>::Create_AddRef(group);
54605459
}
54615460

54625461
//-------------------------------------------------------------------------------------------------
@@ -5467,7 +5466,7 @@ void Object::leaveGroup( void )
54675466
if (m_group)
54685467
{
54695468
// to avoid recursion, set m_group to NULL before removing
5470-
AIGroup *group = m_group;
5469+
RefCountPtr<AIGroup> group = m_group;
54715470
m_group = NULL;
54725471
group->remove( this );
54735472
}

0 commit comments

Comments
 (0)