Skip to content

Commit

Permalink
Add getWeaponStats() member function for DROID_TEMPLATE
Browse files Browse the repository at this point in the history
Replace direct uses of `asWeaponStats` for `DROID_TEMPLATE`
instances with calls to the corresponding member function.

Signed-off-by: Pavel Solodovnikov <[email protected]>
  • Loading branch information
ManManson committed Feb 2, 2024
1 parent 9879b4f commit fd65a72
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 20 deletions.
10 changes: 5 additions & 5 deletions src/design.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,7 @@ static void intSetDesignMode(DES_COMPMODE newCompMode, bool forceRefresh)
widgSetButtonState(psWScreen, IDDES_SYSTEMFORM, WBUT_LOCK);
widgSetButtonState(psWScreen, IDDES_SYSTEMBUTTON, WBUT_CLICKLOCK);
widgReveal(psWScreen, IDDES_SYSTEMFORM);
intSetSystemForm((COMPONENT_STATS *)(&asWeaponStats[sCurrDesign.asWeaps[0]])); // in case previous was a different slot
intSetSystemForm(sCurrDesign.getWeaponStats(0)); // in case previous was a different slot
break;
case IDES_BODY:
compList = intAddComponentForm();
Expand All @@ -1227,7 +1227,7 @@ static void intSetDesignMode(DES_COMPMODE newCompMode, bool forceRefresh)
widgSetButtonState(psWScreen, IDDES_SYSTEMFORM, WBUT_LOCK);
widgSetButtonState(psWScreen, IDDES_WPABUTTON, WBUT_CLICKLOCK);
widgReveal(psWScreen, IDDES_SYSTEMFORM);
intSetSystemForm((COMPONENT_STATS *)(&asWeaponStats[sCurrDesign.asWeaps[1]])); // in case previous was a different slot
intSetSystemForm(sCurrDesign.getWeaponStats(1)); // in case previous was a different slot
// Stop the button flashing
intSetButtonFlash(IDDES_WPABUTTON, false);
break;
Expand All @@ -1239,7 +1239,7 @@ static void intSetDesignMode(DES_COMPMODE newCompMode, bool forceRefresh)
widgSetButtonState(psWScreen, IDDES_SYSTEMFORM, WBUT_LOCK);
widgSetButtonState(psWScreen, IDDES_WPBBUTTON, WBUT_CLICKLOCK);
widgReveal(psWScreen, IDDES_SYSTEMFORM);
intSetSystemForm((COMPONENT_STATS *)(&asWeaponStats[sCurrDesign.asWeaps[2]])); // in case previous was a different slot
intSetSystemForm(sCurrDesign.getWeaponStats(2)); // in case previous was a different slot
// Stop the button flashing
intSetButtonFlash(IDDES_WPBBUTTON, false);
break;
Expand Down Expand Up @@ -2844,7 +2844,7 @@ bool intValidTemplate(DROID_TEMPLATE *psTempl, const char *newName, bool complai
{
ASSERT_OR_RETURN(false, psTempl->asWeaps[i] < asWeaponStats.size(), "Invalid range referenced for numWeaponStats, %d > %zu", psTempl->asWeaps[i], asWeaponStats.size());

int weaponSize = asWeaponStats[psTempl->asWeaps[i]].weaponSize;
int weaponSize = psTempl->getWeaponStats(i)->weaponSize;

if ((weaponSize == WEAPON_SIZE_LIGHT && bodysize != SIZE_LIGHT)
|| (weaponSize == WEAPON_SIZE_HEAVY && bodysize == SIZE_LIGHT)
Expand All @@ -2854,7 +2854,7 @@ bool intValidTemplate(DROID_TEMPLATE *psTempl, const char *newName, bool complai
return false;
}
if (checkTemplateIsVtol(psTempl)
&& asWeaponStats[psTempl->asWeaps[i]].vtolAttackRuns <= 0)
&& psTempl->getWeaponStats(i)->vtolAttackRuns <= 0)
{
debug(level, "VTOL with non-VTOL turret, not possible");
return false;
Expand Down
8 changes: 4 additions & 4 deletions src/droid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3392,24 +3392,24 @@ bool checkValidWeaponForProp(const DROID_TEMPLATE *psTemplate)
if (asPropulsionTypes[psPropStats->propulsionType].travel == AIR)
{
//check weapon stat for indirect
if (!proj_Direct(&asWeaponStats[psTemplate->asWeaps[0]])
|| !asWeaponStats[psTemplate->asWeaps[0]].vtolAttackRuns)
if (!proj_Direct(psTemplate->getWeaponStats(0))
|| !psTemplate->getWeaponStats(0)->vtolAttackRuns)
{
return false;
}
}
else
{
// VTOL weapons do not go on non-AIR units.
if (asWeaponStats[psTemplate->asWeaps[0]].vtolAttackRuns)
if (psTemplate->getWeaponStats(0)->vtolAttackRuns)
{
return false;
}
}

//also checks that there is no other system component
if (psTemplate->asParts[COMP_BRAIN] != 0
&& asWeaponStats[psTemplate->asWeaps[0]].weaponSubClass != WSC_COMMAND)
&& psTemplate->getWeaponStats(0)->weaponSubClass != WSC_COMMAND)
{
assert(false);
return false;
Expand Down
1 change: 1 addition & 0 deletions src/droiddef.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ struct DROID_TEMPLATE : public BASE_STATS
ECM_STATS* getECMStats() const;
REPAIR_STATS* getRepairStats() const;
CONSTRUCT_STATS* getConstructStats() const;
WEAPON_STATS* getWeaponStats(int weaponSlot) const;

/*!
* The droid components.
Expand Down
2 changes: 1 addition & 1 deletion src/quickjs_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ JSValue convTemplate(const DROID_TEMPLATE *psTempl, JSContext *ctx)
JSValue weaponlist = JS_NewArray(ctx);
for (int j = 0; j < psTempl->numWeaps; j++)
{
JS_DefinePropertyValueUint32(ctx, weaponlist, j, JS_NewString(ctx, asWeaponStats[psTempl->asWeaps[j]].id.toUtf8().c_str()), JS_PROP_ENUMERABLE);
JS_DefinePropertyValueUint32(ctx, weaponlist, j, JS_NewString(ctx, psTempl->getWeaponStats(j)->id.toUtf8().c_str()), JS_PROP_ENUMERABLE);
}
QuickJS_DefinePropertyValue(ctx, value, "weapons", weaponlist, JS_PROP_ENUMERABLE);
return value;
Expand Down
25 changes: 15 additions & 10 deletions src/template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,10 @@ bool designableTemplate(const DROID_TEMPLATE *psTempl, int player)
&& ((psTempl->asParts[COMP_ECM] == 0) || (!designablePart(*psTempl->getECMStats(), "ECM") && psTempl->getECMStats()->usageClass == UsageClass::Cyborg))
&& ((psTempl->asParts[COMP_SENSOR] == 0) || (!designablePart(*psTempl->getSensorStats(), "Sensor") && psTempl->getSensorStats()->usageClass == UsageClass::Cyborg))
&& ((psTempl->asParts[COMP_CONSTRUCT] == 0) || (!designablePart(*psTempl->getConstructStats(), "Construction part") && psTempl->getConstructStats()->usageClass == UsageClass::Cyborg))
&& ((psTempl->numWeaps <= 0) || (psTempl->getBrainStats()->psWeaponStat == &asWeaponStats[psTempl->asWeaps[0]])
|| (!designablePart(asWeaponStats[psTempl->asWeaps[0]], "Weapon 0") && ((!isSuperCyborg && asWeaponStats[psTempl->asWeaps[0]].usageClass == UsageClass::Cyborg) || (isSuperCyborg && asWeaponStats[psTempl->asWeaps[0]].usageClass == UsageClass::SuperCyborg))))
&& ((psTempl->numWeaps <= 1) || (!designablePart(asWeaponStats[psTempl->asWeaps[1]], "Weapon 1") && ((!isSuperCyborg && asWeaponStats[psTempl->asWeaps[1]].usageClass == UsageClass::Cyborg) || (isSuperCyborg && asWeaponStats[psTempl->asWeaps[1]].usageClass == UsageClass::SuperCyborg))))
&& ((psTempl->numWeaps <= 2) || (!designablePart(asWeaponStats[psTempl->asWeaps[2]], "Weapon 2") && ((!isSuperCyborg && asWeaponStats[psTempl->asWeaps[2]].usageClass == UsageClass::Cyborg) || (isSuperCyborg && asWeaponStats[psTempl->asWeaps[2]].usageClass == UsageClass::SuperCyborg))));
&& ((psTempl->numWeaps <= 0) || (psTempl->getBrainStats()->psWeaponStat == psTempl->getWeaponStats(0))
|| (!designablePart(*psTempl->getWeaponStats(0), "Weapon 0") && ((!isSuperCyborg && psTempl->getWeaponStats(0)->usageClass == UsageClass::Cyborg) || (isSuperCyborg && psTempl->getWeaponStats(0)->usageClass == UsageClass::SuperCyborg))))
&& ((psTempl->numWeaps <= 1) || (!designablePart(*psTempl->getWeaponStats(1), "Weapon 1") && ((!isSuperCyborg && psTempl->getWeaponStats(1)->usageClass == UsageClass::Cyborg) || (isSuperCyborg && psTempl->getWeaponStats(1)->usageClass == UsageClass::SuperCyborg))))
&& ((psTempl->numWeaps <= 2) || (!designablePart(*psTempl->getWeaponStats(2), "Weapon 2") && ((!isSuperCyborg && psTempl->getWeaponStats(2)->usageClass == UsageClass::Cyborg) || (isSuperCyborg && psTempl->getWeaponStats(2)->usageClass == UsageClass::SuperCyborg))));
}
else
{
Expand All @@ -288,10 +288,10 @@ bool designableTemplate(const DROID_TEMPLATE *psTempl, int player)
&& (psTempl->asParts[COMP_ECM] == 0 || designablePart(*psTempl->getECMStats(), "ECM"))
&& (psTempl->asParts[COMP_SENSOR] == 0 || designablePart(*psTempl->getSensorStats(), "Sensor"))
&& (psTempl->asParts[COMP_CONSTRUCT] == 0 || designablePart(*psTempl->getConstructStats(), "Construction part"))
&& (psTempl->numWeaps <= 0 || psTempl->getBrainStats()->psWeaponStat == &asWeaponStats[psTempl->asWeaps[0]]
|| designablePart(asWeaponStats[psTempl->asWeaps[0]], "Weapon 0"))
&& (psTempl->numWeaps <= 1 || designablePart(asWeaponStats[psTempl->asWeaps[1]], "Weapon 1"))
&& (psTempl->numWeaps <= 2 || designablePart(asWeaponStats[psTempl->asWeaps[2]], "Weapon 2"));
&& (psTempl->numWeaps <= 0 || psTempl->getBrainStats()->psWeaponStat == psTempl->getWeaponStats(0)
|| designablePart(*psTempl->getWeaponStats(0), "Weapon 0"))
&& (psTempl->numWeaps <= 1 || designablePart(*psTempl->getWeaponStats(1), "Weapon 1"))
&& (psTempl->numWeaps <= 2 || designablePart(*psTempl->getWeaponStats(2), "Weapon 2"));

if (transporter && designable)
{
Expand Down Expand Up @@ -437,7 +437,7 @@ nlohmann::json saveTemplateCommon(const DROID_TEMPLATE *psCurr)
for (int j = 0; j < psCurr->numWeaps; j++)
{
ASSERT(psCurr->asWeaps[j] < asWeaponStats.size(), "psCurr->asWeaps[%d] (%d) exceeds numWeaponStats (%zu)", j, (int)psCurr->asWeaps[j], asWeaponStats.size());
weapons.push_back(asWeaponStats[psCurr->asWeaps[j]].id);
weapons.push_back(psCurr->getWeaponStats(j)->id);
}
if (!weapons.empty())
{
Expand Down Expand Up @@ -527,6 +527,11 @@ CONSTRUCT_STATS* DROID_TEMPLATE::getConstructStats() const
return &asConstructStats[asParts[COMP_CONSTRUCT]];
}

WEAPON_STATS* DROID_TEMPLATE::getWeaponStats(int weaponSlot) const
{
return &asWeaponStats[asWeaps[weaponSlot]];
}

bool loadDroidTemplates(const char *filename)
{
WzConfig ini(filename, WzConfig::ReadOnlyAndRequired);
Expand Down Expand Up @@ -778,7 +783,7 @@ bool templateIsIDF(const DROID_TEMPLATE *psTemplate)
return false;
}

if (proj_Direct(&asWeaponStats[psTemplate->asWeaps[0]]))
if (proj_Direct(psTemplate->getWeaponStats(0)))
{
return false;
}
Expand Down

0 comments on commit fd65a72

Please sign in to comment.