Skip to content

Commit

Permalink
refactor(Core): Make more use of helpers. (azerothcore#19835)
Browse files Browse the repository at this point in the history
* Init.

* Reword.

* Update codestyle script.

Co-Authored-By: Kitzunu <[email protected]>

* Add gameobject type ID check, reorder checks.

* Add helper/codestyle check for unit type.

* `IsUnit()` -> `IsCreature()`

* Add `IsUnit()` method.

* Use type mask.

https: //github.com/TrinityCore/TrinityCore/commit/cc71da35b5dc74abf71f8691161525a23d870bb5
Co-Authored-By: Giacomo Pozzoni <[email protected]>
Co-Authored-By: Ovahlord <[email protected]>

* Replace instances of `isType` with `IsUnit`.

---------

Co-authored-by: Kitzunu <[email protected]>
Co-authored-by: Giacomo Pozzoni <[email protected]>
Co-authored-by: Ovahlord <[email protected]>
  • Loading branch information
4 people authored Sep 3, 2024
1 parent e3e4133 commit 1edac37
Show file tree
Hide file tree
Showing 165 changed files with 725 additions and 719 deletions.
18 changes: 12 additions & 6 deletions apps/codestyle/codestyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,20 @@ def get_typeid_check(file: io, file_path: str) -> None:
check_failed = False
# Parse all the file
for line_number, line in enumerate(file, start = 1):
if 'GetTypeId() == TYPEID_PLAYER' in line:
print(f"Please use IsPlayer() instead GetTypeId(): {file_path} at line {line_number}")
if 'GetTypeId() == TYPEID_ITEM' in line or 'GetTypeId() != TYPEID_ITEM' in line:
print(f"Please use IsItem() instead of GetTypeId(): {file_path} at line {line_number}")
check_failed = True
if 'GetTypeId() == TYPEID_ITEM' in line:
print(f"Please use IsItem() instead GetTypeId(): {file_path} at line {line_number}")
if 'GetTypeId() == TYPEID_UNIT' in line or 'GetTypeId() != TYPEID_UNIT' in line:
print(f"Please use IsCreature() instead of GetTypeId(): {file_path} at line {line_number}")
check_failed = True
if 'GetTypeId() == TYPEID_DYNOBJECT' in line:
print(f"Please use IsDynamicObject() instead GetTypeId(): {file_path} at line {line_number}")
if 'GetTypeId() == TYPEID_PLAYER' in line or 'GetTypeId() != TYPEID_PLAYER' in line:
print(f"Please use IsPlayer() instead of GetTypeId(): {file_path} at line {line_number}")
check_failed = True
if 'GetTypeId() == TYPEID_GAMEOBJECT' in line or 'GetTypeId() != TYPEID_GAMEOBJECT' in line:
print(f"Please use IsGameObject() instead of GetTypeId(): {file_path} at line {line_number}")
check_failed = True
if 'GetTypeId() == TYPEID_DYNOBJECT' in line or 'GetTypeId() != TYPEID_DYNOBJECT' in line:
print(f"Please use IsDynamicObject() instead of GetTypeId(): {file_path} at line {line_number}")
check_failed = True
# Handle the script error and update the result output
if check_failed:
Expand Down
2 changes: 1 addition & 1 deletion src/server/game/AI/CoreAI/PassiveAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void PossessedAI::JustDied(Unit* /*u*/)
void PossessedAI::KilledUnit(Unit* /*victim*/)
{
// We killed a creature, disable victim's loot
//if (victim->GetTypeId() == TYPEID_UNIT)
//if (victim->IsCreature())
// victim->RemoveDynamicFlag(UNIT_DYNFLAG_LOOTABLE);
}

Expand Down
2 changes: 1 addition & 1 deletion src/server/game/AI/CoreAI/PetAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ void PetAI::DoAttack(Unit* target, bool chase)

if (_canMeleeAttack())
{
float angle = combatRange == 0.f && target->GetTypeId() != TYPEID_PLAYER && !target->IsPet() ? float(M_PI) : 0.f;
float angle = combatRange == 0.f && !target->IsPlayer() && !target->IsPet() ? float(M_PI) : 0.f;
float tolerance = combatRange == 0.f ? float(M_PI_4) : float(M_PI * 2);
me->GetMotionMaster()->MoveChase(target, ChaseRange(0.f, combatRange), ChaseAngle(angle, tolerance));
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/game/AI/CoreAI/UnitAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ bool NonTankTargetSelector::operator()(Unit const* target) const
if (!target)
return false;

if (_playerOnly && target->GetTypeId() != TYPEID_PLAYER)
if (_playerOnly && !target->IsPlayer())
return false;

if (Unit* currentVictim = _source->GetThreatMgr().GetCurrentVictim())
Expand Down
6 changes: 3 additions & 3 deletions src/server/game/AI/CoreAI/UnitAI.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ struct DefaultTargetSelector : public Acore::unary_function<Unit*, bool>
if (target == except)
return false;

if (m_playerOnly && (target->GetTypeId() != TYPEID_PLAYER))
if (m_playerOnly && (!target->IsPlayer()))
return false;

if (m_dist > 0.0f && !me->IsWithinCombatRange(target, m_dist))
Expand Down Expand Up @@ -148,7 +148,7 @@ struct PowerUsersSelector : public Acore::unary_function<Unit*, bool>
if (target->getPowerType() != _power)
return false;

if (_playerOnly && target->GetTypeId() != TYPEID_PLAYER)
if (_playerOnly && !target->IsPlayer())
return false;

if (_dist > 0.0f && !_me->IsWithinCombatRange(target, _dist))
Expand All @@ -170,7 +170,7 @@ struct FarthestTargetSelector : public Acore::unary_function<Unit*, bool>
if (!_me || !target)
return false;

if (_playerOnly && target->GetTypeId() != TYPEID_PLAYER)
if (_playerOnly && !target->IsPlayer())
return false;

if (_maxDist > 0.0f && !_me->IsInRange(target, _minDist, _maxDist))
Expand Down
6 changes: 3 additions & 3 deletions src/server/game/AI/CreatureAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void CreatureAI::DoZoneInCombat(Creature* creature /*= nullptr*/, float maxRange
Map* map = creature->GetMap();
if (!map->IsDungeon()) //use IsDungeon instead of Instanceable, in case battlegrounds will be instantiated
{
LOG_ERROR("entities.unit.ai", "DoZoneInCombat call for map {} that isn't a dungeon (creature entry = {})", map->GetId(), creature->GetTypeId() == TYPEID_UNIT ? creature->ToCreature()->GetEntry() : 0);
LOG_ERROR("entities.unit.ai", "DoZoneInCombat call for map {} that isn't a dungeon (creature entry = {})", map->GetId(), creature->IsCreature() ? creature->ToCreature()->GetEntry() : 0);
return;
}

Expand Down Expand Up @@ -175,10 +175,10 @@ void CreatureAI::MoveInLineOfSight(Unit* who)
void CreatureAI::TriggerAlert(Unit const* who) const
{
// If there's no target, or target isn't a player do nothing
if (!who || who->GetTypeId() != TYPEID_PLAYER)
if (!who || !who->IsPlayer())
return;
// If this unit isn't an NPC, is already distracted, is in combat, is confused, stunned or fleeing, do nothing
if (me->GetTypeId() != TYPEID_UNIT || me->IsEngaged() || me->HasUnitState(UNIT_STATE_CONFUSED | UNIT_STATE_STUNNED | UNIT_STATE_FLEEING | UNIT_STATE_DISTRACTED))
if (!me->IsCreature() || me->IsEngaged() || me->HasUnitState(UNIT_STATE_CONFUSED | UNIT_STATE_STUNNED | UNIT_STATE_FLEEING | UNIT_STATE_DISTRACTED))
return;
// Only alert for hostiles!
if (me->IsCivilian() || me->HasReactState(REACT_PASSIVE) || !me->IsHostileTo(who) || !me->_IsTargetAcceptable(who))
Expand Down
2 changes: 1 addition & 1 deletion src/server/game/AI/ScriptedAI/ScriptedCreature.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class PlayerOrPetCheck
public:
bool operator() (WorldObject* unit) const
{
if (unit->GetTypeId() != TYPEID_PLAYER)
if (!unit->IsPlayer())
if (!unit->ToUnit()->GetOwnerGUID().IsPlayer())
return true;

Expand Down
2 changes: 1 addition & 1 deletion src/server/game/AI/ScriptedAI/ScriptedEscortAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ bool npc_escortAI::AssistPlayerInCombatAgainst(Unit* who)
}

// or if enemy is in evade mode
if (who->GetTypeId() == TYPEID_UNIT && who->ToCreature()->IsInEvadeMode())
if (who->IsCreature() && who->ToCreature()->IsInEvadeMode())
{
return false;
}
Expand Down
12 changes: 6 additions & 6 deletions src/server/game/AI/SmartScripts/SmartScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4217,7 +4217,7 @@ void SmartScript::ProcessEvent(SmartScriptHolder& e, Unit* unit, uint32 var0, ui
{
if (!me || !unit)
return;
if (e.event.kill.playerOnly && unit->GetTypeId() != TYPEID_PLAYER)
if (e.event.kill.playerOnly && !unit->IsPlayer())
return;
if (e.event.kill.creature && unit->GetEntry() != e.event.kill.creature)
return;
Expand Down Expand Up @@ -4254,7 +4254,7 @@ void SmartScript::ProcessEvent(SmartScriptHolder& e, Unit* unit, uint32 var0, ui
(hostilityMode == SmartEvent::LOSHostilityMode::NotHostile && !me->IsHostileTo(unit)) ||
(hostilityMode == SmartEvent::LOSHostilityMode::Hostile && me->IsHostileTo(unit)))
{
if (e.event.los.playerOnly && unit->GetTypeId() != TYPEID_PLAYER)
if (e.event.los.playerOnly && !unit->IsPlayer())
return;
RecalcTimer(e, e.event.los.cooldownMin, e.event.los.cooldownMax);
ProcessAction(e, unit);
Expand All @@ -4278,7 +4278,7 @@ void SmartScript::ProcessEvent(SmartScriptHolder& e, Unit* unit, uint32 var0, ui
(hostilityMode == SmartEvent::LOSHostilityMode::NotHostile && !me->IsHostileTo(unit)) ||
(hostilityMode == SmartEvent::LOSHostilityMode::Hostile && me->IsHostileTo(unit)))
{
if (e.event.los.playerOnly && unit->GetTypeId() != TYPEID_PLAYER)
if (e.event.los.playerOnly && !unit->IsPlayer())
return;
RecalcTimer(e, e.event.los.cooldownMin, e.event.los.cooldownMax);
ProcessAction(e, unit);
Expand Down Expand Up @@ -5269,7 +5269,7 @@ WorldObject* SmartScript::GetLastInvoker(WorldObject* invoker) const

bool SmartScript::IsUnit(WorldObject* obj)
{
return obj && (obj->GetTypeId() == TYPEID_UNIT || obj->IsPlayer());
return obj && (obj->IsCreature() || obj->IsPlayer());
}

bool SmartScript::IsPlayer(WorldObject* obj)
Expand All @@ -5279,7 +5279,7 @@ bool SmartScript::IsPlayer(WorldObject* obj)

bool SmartScript::IsCreature(WorldObject* obj)
{
return obj && obj->GetTypeId() == TYPEID_UNIT;
return obj && obj->IsCreature();
}

bool SmartScript::IsCharmedCreature(WorldObject* obj)
Expand All @@ -5295,7 +5295,7 @@ bool SmartScript::IsCharmedCreature(WorldObject* obj)

bool SmartScript::IsGameObject(WorldObject* obj)
{
return obj && obj->GetTypeId() == TYPEID_GAMEOBJECT;
return obj && obj->IsGameObject();
}

void SmartScript::IncPhase(uint32 p)
Expand Down
12 changes: 6 additions & 6 deletions src/server/game/Achievements/AchievementMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,27 +304,27 @@ bool AchievementCriteriaData::Meets(uint32 criteria_id, Player const* source, Un
case ACHIEVEMENT_CRITERIA_DATA_TYPE_NONE:
return true;
case ACHIEVEMENT_CRITERIA_DATA_TYPE_T_CREATURE:
if (!target || target->GetTypeId() != TYPEID_UNIT)
if (!target || !target->IsCreature())
return false;
return target->GetEntry() == creature.id;
case ACHIEVEMENT_CRITERIA_DATA_TYPE_T_PLAYER_CLASS_RACE:
if (!target || target->GetTypeId() != TYPEID_PLAYER)
if (!target || !target->IsPlayer())
return false;
if (classRace.class_id && classRace.class_id != target->ToPlayer()->getClass())
return false;
if (classRace.race_id && classRace.race_id != target->ToPlayer()->getRace())
return false;
return true;
case ACHIEVEMENT_CRITERIA_DATA_TYPE_S_PLAYER_CLASS_RACE:
if (!source || source->GetTypeId() != TYPEID_PLAYER)
if (!source || !source->IsPlayer())
return false;
if (classRace.class_id && classRace.class_id != source->ToPlayer()->getClass())
return false;
if (classRace.race_id && classRace.race_id != source->ToPlayer()->getRace())
return false;
return true;
case ACHIEVEMENT_CRITERIA_DATA_TYPE_T_PLAYER_LESS_HEALTH:
if (!target || target->GetTypeId() != TYPEID_PLAYER)
if (!target || !target->IsPlayer())
return false;
return !target->HealthAbovePct(health.percent);
case ACHIEVEMENT_CRITERIA_DATA_TYPE_T_PLAYER_DEAD:
Expand Down Expand Up @@ -371,7 +371,7 @@ bool AchievementCriteriaData::Meets(uint32 criteria_id, Player const* source, Un
return source->GetMap()->GetPlayersCountExceptGMs() <= map_players.maxcount;
case ACHIEVEMENT_CRITERIA_DATA_TYPE_T_TEAM:
{
if (!target || target->GetTypeId() != TYPEID_PLAYER)
if (!target || !target->IsPlayer())
return false;

// DB data compatibility...
Expand Down Expand Up @@ -1501,7 +1501,7 @@ void AchievementMgr::UpdateAchievementCriteria(AchievementCriteriaTypes type, ui
continue;

// map specific case (BG in fact) expected player targeted damage/heal
if (!unit || unit->GetTypeId() != TYPEID_PLAYER)
if (!unit || !unit->IsPlayer())
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions src/server/game/Combat/ThreatMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ bool ThreatCalcHelper::isValidProcess(Unit* hatedUnit, Unit* hatingUnit, SpellIn
if (threatSpell && threatSpell->HasAttribute(SPELL_ATTR1_NO_THREAT))
return false;

ASSERT(hatingUnit->GetTypeId() == TYPEID_UNIT);
ASSERT(hatingUnit->IsCreature());

return true;
}
Expand Down Expand Up @@ -190,7 +190,7 @@ void HostileReference::updateOnlineStatus()
// target is no player or not gamemaster
// target is not in flight
if (isValid()
&& (getTarget()->GetTypeId() != TYPEID_PLAYER || !getTarget()->ToPlayer()->IsGameMaster())
&& (!getTarget()->IsPlayer() || !getTarget()->ToPlayer()->IsGameMaster())
&& !getTarget()->IsInFlight()
&& getTarget()->IsInMap(GetSourceUnit())
&& getTarget()->InSamePhase(GetSourceUnit())
Expand Down
2 changes: 1 addition & 1 deletion src/server/game/Conditions/DisableMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ namespace DisableMgr
if (unit)
{
if ((spellFlags & SPELL_DISABLE_PLAYER && unit->IsPlayer()) ||
(unit->GetTypeId() == TYPEID_UNIT && ((unit->IsPet() && spellFlags & SPELL_DISABLE_PET) || spellFlags & SPELL_DISABLE_CREATURE)))
(unit->IsCreature() && ((unit->IsPet() && spellFlags & SPELL_DISABLE_PET) || spellFlags & SPELL_DISABLE_CREATURE)))
{
if (spellFlags & SPELL_DISABLE_MAP)
{
Expand Down
8 changes: 4 additions & 4 deletions src/server/game/Entities/Creature/Creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1904,7 +1904,7 @@ bool Creature::CanStartAttack(Unit const* who) const
return false;

// This set of checks is should be done only for creatures
if ((IsImmuneToNPC() && who->GetTypeId() != TYPEID_PLAYER) || // flag is valid only for non player characters
if ((IsImmuneToNPC() && !who->IsPlayer()) || // flag is valid only for non player characters
(IsImmuneToPC() && who->IsPlayer())) // immune to PC and target is a player, return false
{
return false;
Expand All @@ -1915,7 +1915,7 @@ bool Creature::CanStartAttack(Unit const* who) const
return false;

// Do not attack non-combat pets
if (who->GetTypeId() == TYPEID_UNIT && who->GetCreatureType() == CREATURE_TYPE_NON_COMBAT_PET)
if (who->IsCreature() && who->GetCreatureType() == CREATURE_TYPE_NON_COMBAT_PET)
return false;

if (!CanFly() && (GetDistanceZ(who) > CREATURE_Z_ATTACK_RANGE + m_CombatDistance)) // too much Z difference, skip very costy vmap calculations here
Expand Down Expand Up @@ -2498,7 +2498,7 @@ bool Creature::CanAssistTo(Unit const* u, Unit const* enemy, bool checkfaction /
return false;

// pussywizard: or if enemy is in evade mode
if (enemy && enemy->GetTypeId() == TYPEID_UNIT && enemy->ToCreature()->IsInEvadeMode())
if (enemy && enemy->IsCreature() && enemy->ToCreature()->IsInEvadeMode())
return false;

// we don't need help from non-combatant ;)
Expand Down Expand Up @@ -2636,7 +2636,7 @@ bool Creature::CanCreatureAttack(Unit const* victim, bool skipDistCheck) const
return false;

// pussywizard: or if enemy is in evade mode
if (victim->GetTypeId() == TYPEID_UNIT && victim->ToCreature()->IsInEvadeMode())
if (victim->IsCreature() && victim->ToCreature()->IsInEvadeMode())
return false;

// cannot attack if is during 5 second grace period, unless being attacked
Expand Down
8 changes: 4 additions & 4 deletions src/server/game/Entities/Creature/TemporarySummon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,14 @@ void TempSummon::InitSummon()
WorldObject* owner = GetSummoner();
if (owner)
{
if (owner->GetTypeId() == TYPEID_UNIT)
if (owner->IsCreature())
{
if (owner->ToCreature()->IsAIEnabled)
{
owner->ToCreature()->AI()->JustSummoned(this);
}
}
else if (owner->GetTypeId() == TYPEID_GAMEOBJECT)
else if (owner->IsGameObject())
{
if (owner->ToGameObject()->AI())
{
Expand Down Expand Up @@ -304,11 +304,11 @@ void TempSummon::UnSummon(uint32 msTime)

if (WorldObject* owner = GetSummoner())
{
if (owner->GetTypeId() == TYPEID_UNIT && owner->ToCreature()->IsAIEnabled)
if (owner->IsCreature() && owner->ToCreature()->IsAIEnabled)
{
owner->ToCreature()->AI()->SummonedCreatureDespawn(this);
}
else if (owner->GetTypeId() == TYPEID_GAMEOBJECT && owner->ToGameObject()->AI())
else if (owner->IsGameObject() && owner->ToGameObject()->AI())
{
owner->ToGameObject()->AI()->SummonedCreatureDespawn(this);
}
Expand Down
Loading

0 comments on commit 1edac37

Please sign in to comment.