Skip to content

Commit 9ae0b92

Browse files
committed
refactor(module): Improve and simplify the implementation of HordeUpdate and related code (#1582)
1 parent 3c30024 commit 9ae0b92

File tree

6 files changed

+143
-157
lines changed

6 files changed

+143
-157
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@ class Player : public Snapshot
309309
/// return t iff the player has all sciences that are prereqs for knowing the given science
310310
Bool hasPrereqsForScience(ScienceType t) const;
311311

312-
Bool hasUpgradeComplete( const UpgradeTemplate *upgradeTemplate ); ///< does player have totally done and produced upgrade
313-
Bool hasUpgradeComplete( UpgradeMaskType testMask ); ///< does player have totally done and produced upgrade
312+
Bool hasUpgradeComplete( const UpgradeTemplate *upgradeTemplate ) const; ///< does player have totally done and produced upgrade
313+
Bool hasUpgradeComplete( UpgradeMaskType testMask ) const; ///< does player have totally done and produced upgrade
314314
UpgradeMaskType getCompletedUpgradeMask() const { return m_upgradesCompleted; } ///< get list of upgrades that are completed
315315
Bool hasUpgradeInProduction( const UpgradeTemplate *upgradeTemplate ); ///< does player have this upgrade in progress right now
316316
Upgrade *addUpgrade( const UpgradeTemplate *upgradeTemplate,

GeneralsMD/Code/GameEngine/Include/GameLogic/Module/AIUpdate.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class AssaultTransportAIInterface;
5757
class JetAIUpdate;
5858

5959
enum AIStateType CPP_11(: Int);
60+
enum HordeActionType CPP_11(: Int);
6061
enum ObjectID CPP_11(: Int);
6162

6263

@@ -560,7 +561,10 @@ class AIUpdateInterface : public UpdateModule, public AICommandInterface
560561
void setAttitude( AttitudeType tude ); ///< set the behavior modifier for this agent
561562

562563
// Common AI "status" effects -------------------------------------------------------------------
563-
void evaluateMoraleBonus( void );
564+
Bool hasNationalism() const;
565+
Bool hasFanaticism() const;
566+
void evaluateMoraleBonus( Bool inHorde, Bool allowNationalism, HordeActionType type );
567+
void evaluateNationalismBonusClassic( Bool inHorde, Bool allowNationalism );
564568

565569
#ifdef ALLOW_DEMORALIZE
566570
// demoralization ... what a nifty word to write.

GeneralsMD/Code/GameEngine/Include/GameLogic/Module/HordeUpdate.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ class HordeUpdateModuleData : public ModuleData
6262
{
6363
public:
6464
UnsignedInt m_updateRate; ///< how often to recheck our horde status
65-
KindOfMaskType m_kindof; ///< the kind(s) of units that count towards horde-ness
65+
KindOfMaskType m_kindof; ///< the kind(s) of units that count towards hordeness
6666
Int m_minCount; ///< min count to get "horde" status
67-
Real m_minDist; ///< min dist to contribute to horde-ness
67+
Real m_minDist; ///< min dist to contribute to hordeness
6868
Bool m_alliesOnly; ///< if true, only allied units count towards hordeness
6969
Bool m_exactMatch; ///< if true, only exact same type of units count towards hordeness
7070
Real m_rubOffRadius;///< If I am this close to another guy who is a true hordesman, it'll rub off on me
71-
HordeActionType m_action; ///< what to do if we get horde-ness
72-
Bool m_allowedNationalism; ///< Nationalism is hard ocded. Yeah! Add to the goodness with this flag instead of rewriting after Alpha.
73-
std::vector<AsciiString> m_flagSubObjNames; ///< name(s) of the flag subobj
71+
HordeActionType m_action; ///< what to do if we get hordeness
72+
Bool m_allowedNationalism; ///< Nationalism is hard coded. Yeah! Add to the goodness with this flag instead of rewriting after Alpha.
73+
std::vector<AsciiString> m_flagSubObjNames; ///< name(s) of the flag sub obj
7474

7575
HordeUpdateModuleData();
7676
static void buildFieldParse(MultiIniFieldParse& p);
@@ -87,7 +87,7 @@ class HordeUpdateInterface
8787
virtual Bool hasFlag() const = 0;
8888
virtual Bool isTrueHordeMember() const = 0;
8989
virtual Bool isAllowedNationalism() const = 0;
90-
90+
virtual HordeActionType getHordeActionType() const = 0;
9191
};
9292

9393
//-------------------------------------------------------------------------------------------------
@@ -104,11 +104,13 @@ class HordeUpdate : public UpdateModule, public HordeUpdateInterface
104104
HordeUpdateInterface *getHordeUpdateInterface() { return this; }
105105

106106
virtual void onDrawableBoundToObject();
107+
virtual UpdateSleepTime update(); ///< update this object's AI
108+
107109
virtual Bool isInHorde() const { return m_inHorde; }
110+
virtual Bool hasFlag() const { return m_hasFlag; }
108111
virtual Bool isTrueHordeMember() const { return m_trueHordeMember && m_inHorde; }
109112
virtual Bool isAllowedNationalism() const;
110-
virtual Bool hasFlag() const { return m_hasFlag; }
111-
virtual UpdateSleepTime update(); ///< update this object's AI
113+
virtual HordeActionType getHordeActionType() const { return getHordeUpdateModuleData()->m_action; };
112114

113115
protected:
114116

@@ -117,8 +119,8 @@ class HordeUpdate : public UpdateModule, public HordeUpdateInterface
117119

118120
private:
119121
UnsignedInt m_lastHordeRefreshFrame; //Just like it sounds
120-
Bool m_inHorde; //I amy be a trueMember, or I may merely inherit hordehood from a neighbor who is
121-
Bool m_trueHordeMember; //meaning, I have enough hordesman near me to qualify
122+
Bool m_inHorde; //I may be a true member, or I may merely inherit hordehood from a neighbor who is
123+
Bool m_trueHordeMember; //meaning, I have enough hordesmen near me to qualify
122124
Bool m_hasFlag;
123125

124126
};

GeneralsMD/Code/GameEngine/Source/Common/RTS/Player.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2990,7 +2990,7 @@ Upgrade *Player::findUpgrade( const UpgradeTemplate *upgradeTemplate )
29902990
//=================================================================================================
29912991
/** Does the player have this completed upgrade */
29922992
//=================================================================================================
2993-
Bool Player::hasUpgradeComplete( const UpgradeTemplate *upgradeTemplate )
2993+
Bool Player::hasUpgradeComplete( const UpgradeTemplate *upgradeTemplate ) const
29942994
{
29952995
UpgradeMaskType testMask = upgradeTemplate->getUpgradeMask();
29962996
return hasUpgradeComplete( testMask );
@@ -3001,7 +3001,7 @@ Bool Player::hasUpgradeComplete( const UpgradeTemplate *upgradeTemplate )
30013001
Does the player have this completed upgrade. This form is exposed so Objects can do quick lookups.
30023002
*/
30033003
//=================================================================================================
3004-
Bool Player::hasUpgradeComplete( UpgradeMaskType testMask )
3004+
Bool Player::hasUpgradeComplete( UpgradeMaskType testMask ) const
30053005
{
30063006
return m_upgradesCompleted.testForAll( testMask );
30073007
}

GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp

Lines changed: 92 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -4678,121 +4678,114 @@ setTmpValue(now);
46784678

46794679
// ------------------------------------------------------------------------------------------------
46804680
// ------------------------------------------------------------------------------------------------
4681-
void AIUpdateInterface::evaluateMoraleBonus( void )
4681+
Bool AIUpdateInterface::hasNationalism() const
46824682
{
4683-
Object *us = getObject();
4684-
#ifdef ALLOW_DEMORALIZE
4685-
Bool demoralized = isDemoralized();
4686-
#endif
4687-
Bool horde = FALSE;
4688-
Bool nationalism = FALSE;
4689-
Bool fanaticism = FALSE;
4690-
4691-
// do we have nationalism
4692-
///@todo Find a better way to represent nationalism without hardcoding here (CBD)
4693-
static const UpgradeTemplate *nationalismTemplate = TheUpgradeCenter->findUpgrade( "Upgrade_Nationalism" );
4694-
DEBUG_ASSERTCRASH( nationalismTemplate != NULL, ("AIUpdateInterface::evaluateMoraleBonus - Nationalism upgrade not found") );
4695-
Player *player = us->getControllingPlayer();
4696-
if( player && player->hasUpgradeComplete( nationalismTemplate ) )
4697-
nationalism = TRUE;
4698-
4699-
// do we have fanaticism
4700-
///@todo Find a better way to represent fanaticism without hardcoding here (MAL)
4701-
static const UpgradeTemplate *fanaticismTemplate = TheUpgradeCenter->findUpgrade( "Upgrade_Fanaticism" );
4702-
DEBUG_ASSERTCRASH( fanaticismTemplate != NULL, ("AIUpdateInterface::evaluateMoraleBonus - Fanaticism upgrade not found") );
4703-
if( player && player->hasUpgradeComplete( fanaticismTemplate ) )
4704-
fanaticism = TRUE;
4705-
4706-
// are we in a horde
4707-
HordeUpdateInterface *hui;
4708-
for( BehaviorModule** u = us->getBehaviorModules(); *u; ++u )
4709-
{
4710-
4711-
hui = (*u)->getHordeUpdateInterface();
4712-
if( hui && hui->isInHorde() )
4713-
{
4714-
horde = TRUE;
4715-
4716-
if( !hui->isAllowedNationalism() )
4717-
{
4718-
// Sorry CBD and MAL, but the cancer has spread to the lymph nodes. After Alpha, just pump full of painkillers.
4719-
nationalism = FALSE;
4720-
fanaticism = FALSE;
4721-
}
4722-
}
4683+
if (const Player *player = getObject()->getControllingPlayer())
4684+
{
4685+
///@todo Find a better way to represent nationalism without hard coding here (CBD)
4686+
static const UpgradeTemplate *nationalismTemplate = TheUpgradeCenter->findUpgrade( "Upgrade_Nationalism" );
4687+
DEBUG_ASSERTCRASH( nationalismTemplate != NULL, ("AIUpdateInterface::evaluateMoraleBonusImpl - Nationalism upgrade not found") );
4688+
return player->hasUpgradeComplete( nationalismTemplate );
4689+
}
4690+
return false;
4691+
}
47234692

4693+
// ------------------------------------------------------------------------------------------------
4694+
// ------------------------------------------------------------------------------------------------
4695+
Bool AIUpdateInterface::hasFanaticism() const
4696+
{
4697+
if (const Player *player = getObject()->getControllingPlayer())
4698+
{
4699+
///@todo Find a better way to represent fanaticism without hard coding here (MAL)
4700+
static const UpgradeTemplate *fanaticismTemplate = TheUpgradeCenter->findUpgrade( "Upgrade_Fanaticism" );
4701+
DEBUG_ASSERTCRASH( fanaticismTemplate != NULL, ("AIUpdateInterface::evaluateMoraleBonusImpl - Fanaticism upgrade not found") );
4702+
return player->hasUpgradeComplete( fanaticismTemplate );
47244703
}
4704+
return false;
4705+
}
47254706

4707+
// ------------------------------------------------------------------------------------------------
4708+
// TheSuperHackers @refactor The implementation has been improved and simplified.
4709+
// ------------------------------------------------------------------------------------------------
4710+
void AIUpdateInterface::evaluateMoraleBonus( Bool inHorde, Bool allowNationalism, HordeActionType type )
4711+
{
47264712
#ifdef ALLOW_DEMORALIZE
4727-
// if we are are not demoralized we can have horde and nationalism effects
4728-
if( demoralized == FALSE )
4729-
#endif
4713+
4714+
// if we are demoralized, then we can not have horde and nationalism effects
4715+
if( isDemoralized() )
47304716
{
4717+
Object *us = getObject();
47314718

4732-
#ifdef ALLOW_DEMORALIZE
47334719
// demoralized
4734-
us->clearWeaponBonusCondition( WEAPONBONUSCONDITION_DEMORALIZED );
4735-
#endif
4720+
us->setWeaponBonusCondition( WEAPONBONUSCONDITION_DEMORALIZED );
47364721

4737-
//Lorenzen temporarily disabled, since it fights with the horde buff
4738-
//Drawable *draw = us->getDrawable();
4739-
//if ( draw && !us->isKindOf( KINDOF_PORTABLE_STRUCTURE ) )
4740-
// draw->setTerrainDecal(TERRAIN_DECAL_NONE);
4722+
// we cannot have horde bonuses
4723+
us->clearWeaponBonusCondition( WEAPONBONUSCONDITION_HORDE );
4724+
us->clearWeaponBonusCondition( WEAPONBONUSCONDITION_NATIONALISM );
4725+
us->clearWeaponBonusCondition( WEAPONBONUSCONDITION_FANATICISM );
47414726

4742-
// horde
4743-
if( horde )
4727+
Drawable *draw = us->getDrawable();
4728+
if( draw && !us->isKindOf( KINDOF_PORTABLE_STRUCTURE ) )
47444729
{
4745-
us->setWeaponBonusCondition( WEAPONBONUSCONDITION_HORDE );
4746-
4730+
draw->setTerrainDecal(TERRAIN_DECAL_DEMORALIZED);
47474731
}
4748-
else
4749-
us->clearWeaponBonusCondition( WEAPONBONUSCONDITION_HORDE );
4750-
4751-
// nationalism
4752-
if( nationalism )
4753-
{
4754-
us->setWeaponBonusCondition( WEAPONBONUSCONDITION_NATIONALISM );
4755-
// fanaticism
4756-
if ( fanaticism )
4757-
us->setWeaponBonusCondition( WEAPONBONUSCONDITION_FANATICISM );// FOR THE NEW GC INFANTRY GENERAL
4758-
else
4759-
us->clearWeaponBonusCondition( WEAPONBONUSCONDITION_FANATICISM );
4760-
}
4761-
else
4762-
us->clearWeaponBonusCondition( WEAPONBONUSCONDITION_NATIONALISM );
47634732

4733+
return;
4734+
}
4735+
4736+
// demoralized
4737+
us->clearWeaponBonusCondition( WEAPONBONUSCONDITION_DEMORALIZED );
4738+
4739+
#endif // ALLOW_DEMORALIZE
4740+
4741+
//Lorenzen temporarily disabled, since it fights with the horde buff
4742+
//Object *us = getObject();
4743+
//Drawable *draw = us->getDrawable();
4744+
//if ( draw && !us->isKindOf( KINDOF_PORTABLE_STRUCTURE ) )
4745+
// draw->setTerrainDecal(TERRAIN_DECAL_NONE);
47644746

4747+
switch (type)
4748+
{
4749+
case HORDEACTION_HORDE:
4750+
evaluateNationalismBonusClassic(inHorde, allowNationalism);
4751+
break;
4752+
}
4753+
}
4754+
4755+
// ------------------------------------------------------------------------------------------------
4756+
// TheSuperHackers @info The classic Nationalism Bonus implementation.
4757+
// Is not great, because Nationalism and Fanaticism bonuses are not disabled when leaving the horde.
4758+
// ------------------------------------------------------------------------------------------------
4759+
void AIUpdateInterface::evaluateNationalismBonusClassic( Bool inHorde, Bool allowNationalism )
4760+
{
4761+
Object *us = getObject();
47654762

4763+
if( inHorde )
4764+
{
4765+
us->setWeaponBonusCondition( WEAPONBONUSCONDITION_HORDE );
47664766
}
4767-
#ifdef ALLOW_DEMORALIZE
47684767
else
47694768
{
4769+
us->clearWeaponBonusCondition( WEAPONBONUSCONDITION_HORDE );
4770+
}
47704771

4771-
// demoralized
4772-
us->setWeaponBonusCondition( WEAPONBONUSCONDITION_DEMORALIZED );
4772+
if( allowNationalism && hasNationalism() )
4773+
{
4774+
us->setWeaponBonusCondition( WEAPONBONUSCONDITION_NATIONALISM );
47734775

4774-
// we cannot have horde bonus condition
4775-
us->clearWeaponBonusCondition( WEAPONBONUSCONDITION_HORDE );
4776-
Drawable *draw = us->getDrawable();
4777-
if( draw && !us->isKindOf( KINDOF_PORTABLE_STRUCTURE ) )
4776+
if ( hasFanaticism() )
47784777
{
4779-
draw->setTerrainDecal(TERRAIN_DECAL_DEMORALIZED);
4778+
us->setWeaponBonusCondition( WEAPONBONUSCONDITION_FANATICISM );
47804779
}
4781-
4782-
// we cannot have nationalism bonus condition
4780+
else
4781+
{
4782+
us->clearWeaponBonusCondition( WEAPONBONUSCONDITION_FANATICISM );
4783+
}
4784+
}
4785+
else
4786+
{
47834787
us->clearWeaponBonusCondition( WEAPONBONUSCONDITION_NATIONALISM );
4784-
us->clearWeaponBonusCondition( WEAPONBONUSCONDITION_FANATICISM );
4785-
47864788
}
4787-
#endif
4788-
4789-
/*
4790-
UnicodeString msg;
4791-
msg.format( L"'%S' Horde=%d,Nationalism=%d,Demoralized=%d",
4792-
us->getTemplate()->getName().str(), horde, nationalism, demoralized );
4793-
TheInGameUI->message( msg );
4794-
*/
4795-
47964789
}
47974790

47984791
#ifdef ALLOW_DEMORALIZE
@@ -4809,12 +4802,16 @@ void AIUpdateInterface::setDemoralized( UnsignedInt durationInFrames )
48094802
if( (prevDemoralizedFrames == 0 && m_demoralizedFramesLeft > 0) ||
48104803
(prevDemoralizedFrames > 0 && m_demoralizedFramesLeft == 0) )
48114804
{
4812-
48134805
// evaluate demoralization, nationalism, and horde effect as they are all intertwined
4814-
evaluateMoraleBonus();
4815-
4806+
Object *us = getObject();
4807+
for( BehaviorModule** u = us->getBehaviorModules(); *u; ++u )
4808+
{
4809+
if ( HordeUpdateInterface *hui = (*u)->getHordeUpdateInterface() )
4810+
{
4811+
evaluateMoraleBonus( hui->isInHorde(), hui->isAllowedNationalism(), hui->getHordeActionType() );
4812+
}
4813+
}
48164814
}
4817-
48184815
}
48194816
#endif
48204817

0 commit comments

Comments
 (0)