Skip to content

Commit

Permalink
DROID: Make freestanding stat getters to be member functions of the `…
Browse files Browse the repository at this point in the history
…DROID` class

These are not `inline` anymore, since it would
introduce cyclic dependencies between `droiddef.h`
and `objectdef.h` headers.

More refactorings may be needed until this can be made
inline again. Although, it may not be even necessary,
if there is no clear performance penalty, AFAICS.

I don't really see any of these functions to
have a measurable impact on performance when I
did some CPU profiling after the changes.

Signed-off-by: Pavel Solodovnikov <[email protected]>
  • Loading branch information
ManManson committed Feb 2, 2024
1 parent fd65a72 commit 27fbc8c
Show file tree
Hide file tree
Showing 30 changed files with 200 additions and 193 deletions.
28 changes: 14 additions & 14 deletions src/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ static bool actionInsideMinRange(DROID *psDroid, BASE_OBJECT *psObj, WEAPON_STAT

if (!psStats)
{
psStats = getWeaponStats(psDroid, 0);
psStats = psDroid->getWeaponStats(0);
}

/* if I am a multi-turret droid */
Expand Down Expand Up @@ -605,7 +605,7 @@ static bool actionRemoveDroidsFromBuildPos(unsigned player, Vector2i pos, uint16
{
Vector2i dest = world_coord(b.map + Vector2i(x, y)) + Vector2i(TILE_UNITS, TILE_UNITS) / 2;
unsigned dist = iHypot(droid->pos.xy() - dest);
if (dist < bestDist && !fpathBlockingTile(map_coord(dest.x), map_coord(dest.y), getPropulsionStats(droid)->propulsionType))
if (dist < bestDist && !fpathBlockingTile(map_coord(dest.x), map_coord(dest.y), droid->getPropulsionStats()->propulsionType))
{
bestDest = dest;
bestDist = dist;
Expand Down Expand Up @@ -634,7 +634,7 @@ void actionSanity(DROID *psDroid)
// clear the target if it has died
for (int i = 0; i < MAX_WEAPONS; i++)
{
bDirect = proj_Direct(getWeaponStats(psDroid, i));
bDirect = proj_Direct(psDroid->getWeaponStats(i));
if (psDroid->psActionTarget[i] && (avoidOverkill ? aiObjectIsProbablyDoomed(psDroid->psActionTarget[i], bDirect) : psDroid->psActionTarget[i]->died))
{
syncDebugObject(psDroid->psActionTarget[i], '-');
Expand Down Expand Up @@ -681,7 +681,7 @@ void actionUpdateDroid(DROID *psDroid)

CHECK_DROID(psDroid);

PROPULSION_STATS *psPropStats = getPropulsionStats(psDroid);
PROPULSION_STATS *psPropStats = psDroid->getPropulsionStats();
ASSERT_OR_RETURN(, psPropStats != nullptr, "Invalid propulsion stats pointer");

bool secHoldActive = secondaryGetState(psDroid, DSO_HALTTYPE) == DSS_HALT_HOLD;
Expand Down Expand Up @@ -738,7 +738,7 @@ void actionUpdateDroid(DROID *psDroid)
{
BASE_OBJECT *psTemp = nullptr;

WEAPON_STATS *const psWeapStats = getWeaponStats(psDroid, i);
WEAPON_STATS *const psWeapStats = psDroid->getWeaponStats(i);
if (psDroid->asWeaps[i].nStat > 0
&& psWeapStats->rotate
&& IS_TIME_TO_CHECK_FOR_NEW_TARGET(psDroid)
Expand Down Expand Up @@ -838,7 +838,7 @@ void actionUpdateDroid(DROID *psDroid)
BASE_OBJECT *psTemp = nullptr;

//I moved psWeapStats flag update there
WEAPON_STATS *const psWeapStats = getWeaponStats(psDroid, i);
WEAPON_STATS *const psWeapStats = psDroid->getWeaponStats(i);
if (!psDroid->isVtol()
&& psDroid->asWeaps[i].nStat > 0
&& psWeapStats->rotate
Expand Down Expand Up @@ -876,7 +876,7 @@ void actionUpdateDroid(DROID *psDroid)
bHasTarget = false;
for (unsigned i = 0; i < psDroid->numWeaps; ++i)
{
bDirect = proj_Direct(getWeaponStats(psDroid, i));
bDirect = proj_Direct(psDroid->getWeaponStats(i));
blockingWall = nullptr;
// Does this weapon have a target?
if (psDroid->psActionTarget[i] != nullptr)
Expand Down Expand Up @@ -1071,7 +1071,7 @@ void actionUpdateDroid(DROID *psDroid)
&& actionVisibleTarget(psDroid, psActionTarget, i)
&& actionInRange(psDroid, psActionTarget, i))
{
WEAPON_STATS *const psWeapStats = getWeaponStats(psDroid, i);
WEAPON_STATS *const psWeapStats = psDroid->getWeaponStats(i);
WEAPON_EFFECT weapEffect = psWeapStats->weaponEffect;
blockingWall = visGetBlockingWall(psDroid, psActionTarget);

Expand Down Expand Up @@ -1226,7 +1226,7 @@ void actionUpdateDroid(DROID *psDroid)
&& validTarget(psDroid, psDroid->psActionTarget[0], i))
{
//I moved psWeapStats flag update there
psWeapStats = getWeaponStats(psDroid, i);
psWeapStats = psDroid->getWeaponStats(i);
if (actionVisibleTarget(psDroid, psDroid->psActionTarget[0], i))
{
if (actionInRange(psDroid, psDroid->psActionTarget[0], i))
Expand Down Expand Up @@ -1325,7 +1325,7 @@ void actionUpdateDroid(DROID *psDroid)
&& actionVisibleTarget(psDroid, psDroid->psActionTarget[0], i))
{
bool chaseBloke = false;
WEAPON_STATS *const psWeapStats = getWeaponStats(psDroid, i);
WEAPON_STATS *const psWeapStats = psDroid->getWeaponStats(i);
const bool actionIsInRange = actionInRange(psDroid, psDroid->psActionTarget[0], i);
if (psWeapStats->rotate)
{
Expand Down Expand Up @@ -1403,7 +1403,7 @@ void actionUpdateDroid(DROID *psDroid)
/* Stopped moving but haven't reached the target - possibly move again */

//'hack' to make the droid to check the primary turrent instead of all
WEAPON_STATS *const psWeapStats = getWeaponStats(psDroid, 0);
WEAPON_STATS *const psWeapStats = psDroid->getWeaponStats(0);

if (order->type == DORDER_ATTACKTARGET && secHoldActive)
{
Expand Down Expand Up @@ -1969,7 +1969,7 @@ void actionUpdateDroid(DROID *psDroid)
{
Vector2i diff = (psDroid->pos - order->psObj->pos).xy();
//Consider .shortRange here
int rangeSq = getWeaponStats(psDroid, 0)->upgrade[psDroid->player].maxRange / 2; // move close to sensor
int rangeSq = psDroid->getWeaponStats(0)->upgrade[psDroid->player].maxRange / 2; // move close to sensor
rangeSq = rangeSq * rangeSq;
if (dot(diff, diff) < rangeSq)
{
Expand Down Expand Up @@ -2063,7 +2063,7 @@ void actionUpdateDroid(DROID *psDroid)
{
BASE_OBJECT *psTemp = nullptr;

WEAPON_STATS *const psWeapStats = getWeaponStats(psDroid, i);
WEAPON_STATS *const psWeapStats = psDroid->getWeaponStats(i);
if (psDroid->asWeaps[i].nStat > 0 && psWeapStats->rotate
&& secondaryGetState(psDroid, DSO_ATTACK_LEVEL) == DSS_ALEV_ALWAYS
&& IS_TIME_TO_CHECK_FOR_NEW_TARGET(psDroid)
Expand Down Expand Up @@ -2231,7 +2231,7 @@ static void actionDroidBase(DROID *psDroid, DROID_ACTION_DATA *psAction)
{
ASSERT_OR_RETURN(, psAction->psObj == nullptr || !psAction->psObj->died, "Droid dead");

WEAPON_STATS *psWeapStats = getWeaponStats(psDroid, 0);
WEAPON_STATS *psWeapStats = psDroid->getWeaponStats(0);
Vector2i pos(0, 0);

CHECK_DROID(psDroid);
Expand Down
16 changes: 8 additions & 8 deletions src/ai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ static int aiDroidRange(DROID *psDroid, int weapon_slot)
}
else
{
WEAPON_STATS *psWStats = getWeaponStats(psDroid, weapon_slot);
WEAPON_STATS *psWStats = psDroid->getWeaponStats(weapon_slot);
longRange = proj_GetLongRange(*psWStats, psDroid->player);
}

Expand Down Expand Up @@ -197,7 +197,7 @@ static BASE_OBJECT *aiSearchSensorTargets(BASE_OBJECT *psObj, int weapon_slot, W
{
continue;
}
const auto sensorType = getSensorStats(psDroid)->type;
const auto sensorType = psDroid->getSensorStats()->type;
// Artillery should not fire at objects observed by VTOL CB/Strike sensors.
if (sensorType == VTOL_CB_SENSOR ||
sensorType == VTOL_INTERCEPT_SENSOR ||
Expand Down Expand Up @@ -293,7 +293,7 @@ static SDWORD targetAttackWeight(BASE_OBJECT *psTarget, BASE_OBJECT *psAttacker,
{
psAttackerDroid = (DROID *)psAttacker;

attackerWeapon = getWeaponStats(psAttackerDroid, weapon_slot);
attackerWeapon = psAttackerDroid->getWeaponStats(weapon_slot);

//check if this droid is assigned to a commander
bCmdAttached = hasCommander(psAttackerDroid);
Expand Down Expand Up @@ -420,8 +420,8 @@ static SDWORD targetAttackWeight(BASE_OBJECT *psTarget, BASE_OBJECT *psAttacker,
}

/* Now calculate the overall weight */
attackWeight = asWeaponModifier[weaponEffect][getPropulsionStats(targetDroid)->propulsionType] // Our weapon's effect against target
+ asWeaponModifierBody[weaponEffect][getBodyStats(targetDroid)->size]
attackWeight = asWeaponModifier[weaponEffect][targetDroid->getPropulsionStats()->propulsionType] // Our weapon's effect against target
+ asWeaponModifierBody[weaponEffect][targetDroid->getBodyStats()->size]
+ WEIGHT_DIST_TILE_DROID * objSensorRange(psAttacker) / TILE_UNITS
- WEIGHT_DIST_TILE_DROID * dist / TILE_UNITS // farther droids are less attractive
+ WEIGHT_HEALTH_DROID * damageRatio / 100 // we prefer damaged droids
Expand Down Expand Up @@ -583,7 +583,7 @@ int aiBestNearestTarget(DROID *psDroid, BASE_OBJECT **ppsObj, int weapon_slot, i
++numDroidNearestTargetChecksThisFrame;

// Check if we have a CB target to begin with
WEAPON_STATS* psWStats = getWeaponStats(psDroid, weapon_slot);
WEAPON_STATS* psWStats = psDroid->getWeaponStats(weapon_slot);
if (!proj_Direct(psWStats))
{
bestTarget = aiSearchSensorTargets((BASE_OBJECT *)psDroid, weapon_slot, psWStats, &tmpOrigin);
Expand Down Expand Up @@ -1267,7 +1267,7 @@ bool validTarget(BASE_OBJECT const *psObject, BASE_OBJECT const *psTarget, int w
switch (psTarget->type)
{
case OBJ_DROID:
if (asPropulsionTypes[getPropulsionStats((const DROID*)psTarget)->propulsionType].travel == AIR)
if (asPropulsionTypes[((const DROID*)psTarget)->getPropulsionStats()->propulsionType].travel == AIR)
{
if (((const DROID *)psTarget)->sMove.Status != MOVEINACTIVE)
{
Expand Down Expand Up @@ -1302,7 +1302,7 @@ bool validTarget(BASE_OBJECT const *psObject, BASE_OBJECT const *psTarget, int w
// Can't attack without a weapon
if (((const DROID *)psObject)->numWeaps != 0 && ((const DROID *)psObject)->asWeaps[weapon_slot].nStat != 0)
{
surfaceToAir = getWeaponStats((const DROID*)psObject, weapon_slot)->surfaceToAir;
surfaceToAir = ((const DROID*)psObject)->getWeaponStats(weapon_slot)->surfaceToAir;
if (((surfaceToAir & SHOOT_IN_AIR) && bTargetInAir) || ((surfaceToAir & SHOOT_ON_GROUND) && !bTargetInAir))
{
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/bucket3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ static SDWORD bucketCalculateZ(RENDER_TYPE objectType, void *pObject, const glm:
position.z = -(psSimpObj->pos.y);
position.y = psSimpObj->pos.z;

psBStats = getBodyStats(psDroid);
psBStats = psDroid->getBodyStats();
droidSize = psBStats->pIMD->radius;
z = pie_RotateProjectWithPerspective(&position, perspectiveViewMatrix, &pixel) - (droidSize * 2);

Expand Down
2 changes: 1 addition & 1 deletion src/cmddroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ SDWORD cmdDroidGetIndex(const DROID *psCommander)
/** This function returns the maximum group size of the command droid.*/
unsigned int cmdDroidMaxGroup(const DROID *psCommander)
{
const BRAIN_STATS *psStats = getBrainStats(psCommander);
const BRAIN_STATS *psStats = psCommander->getBrainStats();
return getDroidLevel(psCommander) * psStats->upgrade[psCommander->player].maxDroidsMult + psStats->upgrade[psCommander->player].maxDroids;
}

Expand Down
2 changes: 1 addition & 1 deletion src/combat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ int objArmour(const BASE_OBJECT *psObj, WEAPON_CLASS weaponClass)
int armour = 0;
if (psObj->type == OBJ_DROID)
{
armour = bodyArmour(*getBodyStats((const DROID*)psObj), psObj->player, weaponClass);
armour = bodyArmour(*((const DROID*)psObj)->getBodyStats(), psObj->player, weaponClass);
}
else if (psObj->type == OBJ_STRUCTURE && weaponClass == WC_KINETIC && ((const STRUCTURE *)psObj)->status != SS_BEING_BUILT)
{
Expand Down
4 changes: 2 additions & 2 deletions src/component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ static bool displayCompObj(DROID *psDroid, bool bButton, const glm::mat4& modelM
}

/* get propulsion stats */
psPropStats = getPropulsionStats(psDroid);
psPropStats = psDroid->getPropulsionStats();
ASSERT_OR_RETURN(didDrawSomething, psPropStats != nullptr, "invalid propulsion stats pointer");

//set pieflag for button object or ingame object
Expand Down Expand Up @@ -531,7 +531,7 @@ static bool displayCompObj(DROID *psDroid, bool bButton, const glm::mat4& modelM
}

/* Render animation effects based on movement or lack thereof, if any */
const auto* bodyStats = getBodyStats(psDroid);
const auto* bodyStats = psDroid->getBodyStats();
psMoveAnim = bodyStats->ppMoveIMDList[psDroid->asBits[COMP_PROPULSION]];
psStillAnim = bodyStats->ppStillIMDList[psDroid->asBits[COMP_PROPULSION]];
if (!bButton && psMoveAnim && psDroid->sMove.Status != MOVEINACTIVE)
Expand Down
26 changes: 13 additions & 13 deletions src/component.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,32 +83,32 @@ void drawMuzzleFlash(WEAPON sWeap, iIMDShape *weaponImd, iIMDShape *flashImd, PI
#define PART_IMD(STATS,DROID,COMPONENT,PLAYER) (STATS[DROID->asBits[COMPONENT]].pIMD)

/* Get the chassis imd */
#define BODY_IMD(DROID,PLAYER) (getBodyStats(DROID)->pIMD)
#define BODY_IMD(DROID,PLAYER) (DROID->getBodyStats()->pIMD)
/* Get the brain imd - NOTE: Unused!*/
#define BRAIN_IMD(DROID,PLAYER) (getBrainStats(DROID)->pIMD)
#define BRAIN_IMD(DROID,PLAYER) (DROID->getBrainStats()->pIMD)
/* Get the weapon imd */
#define WEAPON_IMD(DROID,WEAPON_NUM) (getWeaponStats(DROID, WEAPON_NUM)->pIMD)
#define WEAPON_IMD(DROID,WEAPON_NUM) (DROID->getWeaponStats(WEAPON_NUM)->pIMD)
/* Get the propulsion imd THIS IS A LITTLE MORE COMPLICATED NOW!*/
//#define PROPULSION_IMD(DROID,PLAYER) (asPropulsionStats[DROID->asBits[COMP_PROPULSION]].pIMD[PLAYER])
/* Get the sensor imd */
#define SENSOR_IMD(DROID,PLAYER) (getSensorStats(DROID)->pIMD)
#define SENSOR_IMD(DROID,PLAYER) (DROID->getSensorStats()->pIMD)
/* Get an ECM imd!?! */
#define ECM_IMD(DROID,PLAYER) (getECMStats(DROID)->pIMD)
#define ECM_IMD(DROID,PLAYER) (DROID->getECMStats()->pIMD)
/* Get an Repair imd!?! */
#define REPAIR_IMD(DROID,PLAYER) (getRepairStats(DROID)->pIMD)
#define REPAIR_IMD(DROID,PLAYER) (DROID->getRepairStats()->pIMD)
/* Get a construct imd */
#define CONSTRUCT_IMD(DROID,PLAYER) (getConstructStats(DROID)->pIMD)
#define CONSTRUCT_IMD(DROID,PLAYER) (DROID->getConstructStats()->pIMD)
/* Get a weapon mount imd*/
#define WEAPON_MOUNT_IMD(DROID,WEAPON_NUM) (getWeaponStats(DROID, WEAPON_NUM)->pMountGraphic)
#define WEAPON_MOUNT_IMD(DROID,WEAPON_NUM) (DROID->getWeaponStats(WEAPON_NUM)->pMountGraphic)
/* Get a sensor mount imd*/
#define SENSOR_MOUNT_IMD(DROID,PLAYER) (getSensorStats(DROID)->pMountGraphic)
#define SENSOR_MOUNT_IMD(DROID,PLAYER) (DROID->getSensorStats()->pMountGraphic)
/* Get a construct mount imd*/
#define CONSTRUCT_MOUNT_IMD(DROID,PLAYER) (getConstructStats(DROID)->pMountGraphic)
#define CONSTRUCT_MOUNT_IMD(DROID,PLAYER) (DROID->getConstructStats()->pMountGraphic)
/* Get a ecm mount imd*/
#define ECM_MOUNT_IMD(DROID,PLAYER) (getECMStats(DROID)->pMountGraphic)
#define ECM_MOUNT_IMD(DROID,PLAYER) (DROID->getECMStats()->pMountGraphic)
/* Get a repair mount imd*/
#define REPAIR_MOUNT_IMD(DROID,PLAYER) (getRepairStats(DROID)->pMountGraphic)
#define REPAIR_MOUNT_IMD(DROID,PLAYER) (DROID->getRepairStats()->pMountGraphic)
/* Get a muzzle flash pie*/
#define MUZZLE_FLASH_PIE(DROID,WEAPON_NUM) (getWeaponStats(DROID, WEAPON_NUM)->pMuzzleGraphic)
#define MUZZLE_FLASH_PIE(DROID,WEAPON_NUM) (DROID->getWeaponStats(WEAPON_NUM)->pMuzzleGraphic)

#endif // __INCLUDED_SRC_COMPONENT_H__
4 changes: 2 additions & 2 deletions src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1722,7 +1722,7 @@ static void dealWithLMBDroid(DROID *psDroid, SELECTION_TYPE selection)
if ((psCurr->droidType == DROID_WEAPON) &&
(psCurr->selected) &&
(psCurr->asWeaps[0].nStat > 0) &&
((!proj_Direct(getWeaponStats(psCurr, 0))) ||
((!proj_Direct(psCurr->getWeaponStats(0))) ||
psCurr->isVtol()) &&
droidSensorDroidWeapon((BASE_OBJECT *)psDroid, psCurr))
{
Expand Down Expand Up @@ -2608,7 +2608,7 @@ static SELECTION_TYPE establishSelection(UDWORD _selectedPlayer)
switch (psDominant->droidType)
{
case DROID_WEAPON:
if (proj_Direct(getWeaponStats(psDominant, 0)))
if (proj_Direct(psDominant->getWeaponStats(0)))
{
selectionClass = SC_DROID_DIRECT;
}
Expand Down
6 changes: 3 additions & 3 deletions src/display3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1771,7 +1771,7 @@ bool clipDroidOnScreen(DROID *psDroid, const glm::mat4 &perspectiveViewModelMatr
{
/* Get its absolute dimensions */
// NOTE: This only takes into account body, but is "good enough"
const BODY_STATS *psBStats = getBodyStats(psDroid);
const BODY_STATS *psBStats = psDroid->getBodyStats();
const iIMDShape * pIMD = (psBStats != nullptr) ? psBStats->pIMD->displayModel() : nullptr;

return clipShapeOnScreen(pIMD, perspectiveViewModelMatrix, overdrawScreenPoints);
Expand Down Expand Up @@ -3751,7 +3751,7 @@ static void drawDroidCmndNo(DROID *psDroid)
void calcScreenCoords(DROID *psDroid, const glm::mat4 &perspectiveViewMatrix)
{
/* Get it's absolute dimensions */
const BODY_STATS *psBStats = getBodyStats(psDroid);
const BODY_STATS *psBStats = psDroid->getBodyStats();
Vector3i origin;
Vector2i center(0, 0);
int wsRadius = 22; // World space radius, 22 = magic minimum
Expand Down Expand Up @@ -4412,7 +4412,7 @@ static void addConstructionLine(DROID *psDroid, STRUCTURE *psStructure, const gl
auto deltaPlayer = Vector3f(0,0,0);
auto pt0 = Vector3f(psDroid->pos.x, psDroid->pos.z + 24, -psDroid->pos.y) + deltaPlayer;

int constructPoints = constructorPoints(*getConstructStats(psDroid), psDroid->player);
int constructPoints = constructorPoints(*psDroid->getConstructStats(), psDroid->player);
int amount = 800 * constructPoints * (graphicsTime - psDroid->actionStarted) / GAME_TICKS_PER_SEC;

Vector3i each;
Expand Down
Loading

0 comments on commit 27fbc8c

Please sign in to comment.