Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop a form of cheating with saved templates #378

Merged
merged 4 commits into from Jun 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions data/base/script/campaign/cam3-a.js
Expand Up @@ -232,6 +232,8 @@ function eventStartLevel()
setPower(PLAYER_POWER, CAM_HUMAN_PLAYER);
cam3Setup();

enableTemplate("ConstructionDroid");

camSetEnemyBases({
"NEXUS-WBase": {
cleanup: "westBaseCleanup",
Expand Down
3 changes: 3 additions & 0 deletions data/base/script/fastplay/fastdemo.js
Expand Up @@ -198,6 +198,9 @@ function eventStartLevel()
camPlayVideos("MBDEMO_MSG");
hackAddMessage("FAST_OBJ1", PROX_MSG, CAM_HUMAN_PLAYER, false);

enableTemplate("ConstructionDroid");
enableTemplate("ViperLtMGWheels");

queue("sendAttackGroup1", camSecondsToMilliseconds(10));
queue("sendAttackGroup2", camSecondsToMilliseconds(20));
queue("activateDefenders", camSecondsToMilliseconds(30));
Expand Down
1 change: 1 addition & 0 deletions data/base/script/rules.js
Expand Up @@ -156,6 +156,7 @@ function reticuleDesignCheck()
{
setReticuleButton(4, _("Design - construct HQ first"), "", "");
setMiniMap(false);
// Will enable templates that are researched whenever the reticule buttons update.
setDesign(false);
}
}
Expand Down
1 change: 0 additions & 1 deletion data/mp/multiplay/skirmish/rules.js
Expand Up @@ -108,7 +108,6 @@ function reticuleDesignCheck()
{
setReticuleButton(4, _("Design - construct HQ first"), "", "");
setMiniMap(false);
setDesign(false);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/qtscriptfuncs.cpp
Expand Up @@ -3251,13 +3251,13 @@ static QScriptValue js_setDesign(QScriptContext *context, QScriptEngine *engine)
// FIXME: This dual data structure for templates is just plain insane.
for (auto &keyvaluepair : droidTemplates[selectedPlayer])
{
bool researched = researchedTemplate(keyvaluepair.second, selectedPlayer);
bool researched = researchedTemplate(keyvaluepair.second, selectedPlayer, true);
keyvaluepair.second->enabled = (researched || allowDesign);
}
for (auto &localTemplate : localTemplates)
{
psCurr = &localTemplate;
bool researched = researchedTemplate(psCurr, selectedPlayer);
bool researched = researchedTemplate(psCurr, selectedPlayer, true);
psCurr->enabled = (researched || allowDesign);
}
return QScriptValue();
Expand Down
15 changes: 8 additions & 7 deletions src/structure.cpp
Expand Up @@ -2424,21 +2424,22 @@ static bool IsFactoryCommanderGroupFull(const FACTORY *psFactory)
return true;
}

// Check if a player has built a command relay.
bool hasBuiltCommandRelay(bool isMission, int player)
// Check if a player has a certain structure. Optionally, checks if there is
// at least one that is built.
bool structureExists(int player, STRUCTURE_TYPE type, bool built, bool isMission)
{
bool hasRelay = false;
bool found = false;

for (STRUCTURE *psCurr = isMission ? mission.apsStructLists[player] : apsStructLists[player]; psCurr; psCurr = psCurr->psNext)
{
if (psCurr->pStructureType->type == REF_COMMAND_CONTROL && psCurr->status == SS_BUILT)
if (psCurr->pStructureType->type == type && (!built || (built && psCurr->status == SS_BUILT)))
{
hasRelay = true;
found = true;
break;
}
}

return hasRelay;
return found;
}

// Disallow manufacture of units once these limits are reached,
Expand Down Expand Up @@ -2502,7 +2503,7 @@ static bool checkHaltOnMaxUnitsReached(STRUCTURE *psStructure, bool isMission)
else switch (droidTemplateType(templ))
{
case DROID_COMMAND:
if (!hasBuiltCommandRelay(isMission, player))
if (!structureExists(player, REF_COMMAND_CONTROL, true, isMission))
{
isLimit = true;
ssprintf(limitMsg, _("Can't build \"%s\" without a Command Relay Center — Production Halted"), templ->name.toUtf8().c_str());
Expand Down
2 changes: 1 addition & 1 deletion src/structure.h
Expand Up @@ -78,7 +78,7 @@ void setMaxDroids(int player, int value);
void setMaxCommanders(int player, int value);
void setMaxConstructors(int player, int value);

bool hasBuiltCommandRelay(bool isMission, int player);
bool structureExists(int player, STRUCTURE_TYPE type, bool built, bool isMission);

bool IsPlayerDroidLimitReached(int player);

Expand Down
12 changes: 10 additions & 2 deletions src/template.cpp
Expand Up @@ -44,6 +44,7 @@ std::map<int, DROID_TEMPLATE *> droidTemplates[MAX_PLAYERS];

bool allowDesign = true;
bool includeRedundantDesigns = false;
bool playerBuiltHQ = false;


static bool researchedItem(const DROID_TEMPLATE* /*psCurr*/, int player, COMPONENT_TYPE partIndex, int part, bool allowZero, bool allowRedundant)
Expand Down Expand Up @@ -645,6 +646,11 @@ void fillTemplateList(std::vector<DROID_TEMPLATE *> &pList, STRUCTURE *psFactory

BODY_SIZE iCapacity = (BODY_SIZE)psFactory->capacity;

if (!playerBuiltHQ)
{
playerBuiltHQ = structureExists(player, REF_HQ, true, false) || structureExists(player, REF_HQ, true, true);
}

/* Add the templates to the list*/
for (DROID_TEMPLATE &i : localTemplates)
{
Expand All @@ -661,8 +667,10 @@ void fillTemplateList(std::vector<DROID_TEMPLATE *> &pList, STRUCTURE *psFactory
}
}

if (!psCurr->enabled || !validTemplateForFactory(psCurr, psFactory, false)
|| !researchedTemplate(psCurr, player, includeRedundantDesigns))
if (!psCurr->enabled
|| (bMultiPlayer && !playerBuiltHQ)
|| !validTemplateForFactory(psCurr, psFactory, false)
|| !researchedTemplate(psCurr, player, includeRedundantDesigns))
{
continue;
}
Expand Down
1 change: 1 addition & 0 deletions src/template.h
Expand Up @@ -28,6 +28,7 @@ extern std::map<int, DROID_TEMPLATE *> droidTemplates[MAX_PLAYERS];

extern bool allowDesign;
extern bool includeRedundantDesigns;
extern bool playerBuiltHQ;


bool initTemplates();
Expand Down