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

Additional wzapi sanity checks, assignObjectToGroup structure assignment tweak #3949

Merged
merged 2 commits into from
Jun 30, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions src/droid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1884,7 +1884,7 @@ void templateSetParts(const DROID *psDroid, DROID_TEMPLATE *psTemplate)
}

/* Make all the droids for a certain player a member of a specific group */
/* If a structure is selected, set its group to which droids will be automatically assigned */
/* If no droids are selected: If a structure is selected, set its group to which droids will be automatically assigned */
void assignObjectToGroup(UDWORD playerNumber, UDWORD groupNumber, bool clearGroup)
{
bool bAtLeastOne = false;
Expand All @@ -1894,24 +1894,6 @@ void assignObjectToGroup(UDWORD playerNumber, UDWORD groupNumber, bool clearGrou

if (groupNumber < UBYTE_MAX)
{
/* Run through all the structures */
for (STRUCTURE *psStruct : apsStructLists[playerNumber])
{
if (psStruct->selected && psStruct->isFactory())
{
if (psStruct->productToGroup != (UBYTE)groupNumber)
{
psStruct->productToGroup = (UBYTE)groupNumber;
}
else
{
// To make it possible to clear factory group assignment via "toggle" behavior of assigning a factory to the same group it's already assigned
psStruct->productToGroup = UBYTE_MAX;
}
return;
}
}

/* Run through all the droids */
for (DROID* psDroid : apsDroidLists[playerNumber])
{
Expand Down Expand Up @@ -1952,6 +1934,27 @@ void assignObjectToGroup(UDWORD playerNumber, UDWORD groupNumber, bool clearGrou
}
intGroupsChanged(newSelectedGroup);
}
if (!bAtLeastOne && groupNumber < UBYTE_MAX)
{
/* If no droids were selected to be added to the group, check if a factory is selected */
/* Run through all the structures */
for (STRUCTURE *psStruct : apsStructLists[playerNumber])
{
if (psStruct->selected && psStruct->isFactory())
{
if (psStruct->productToGroup != (UBYTE)groupNumber)
{
psStruct->productToGroup = (UBYTE)groupNumber;
}
else
{
// To make it possible to clear factory group assignment via "toggle" behavior of assigning a factory to the same group it's already assigned
psStruct->productToGroup = UBYTE_MAX;
}
return;
}
}
}
}


Expand Down
20 changes: 17 additions & 3 deletions src/wzapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,11 @@ wzapi::researchResults wzapi::enumResearch(WZAPI_NO_PARAMS)
//--
std::vector<const BASE_OBJECT *> wzapi::enumRange(WZAPI_PARAMS(int _x, int _y, int _range, optional<int> _playerFilter, optional<bool> _seen))
{
_x = std::max<int>(_x, 0);
_y = std::max<int>(_y, 0);
_x = std::min<int>(_x, mapWidth);
_y = std::min<int>(_y, mapHeight);

int player = context.player();
int x = world_coord(_x);
int y = world_coord(_y);
Expand Down Expand Up @@ -2117,6 +2122,11 @@ std::vector<scr_position> wzapi::getDroidPath(WZAPI_PARAMS(const DROID *psDroid)
//--
bool wzapi::addBeacon(WZAPI_PARAMS(int _x, int _y, int playerFilter, optional<std::string> _message))
{
SCRIPT_ASSERT(false, context, _x >= 0, "Beacon x value %d is less than zero", _x);
SCRIPT_ASSERT(false, context, _y >= 0, "Beacon y value %d is less than zero", _y);
SCRIPT_ASSERT(false, context, _x <= mapWidth, "Beacon x value %d is greater than mapWidth %d", _x, (int)mapWidth);
SCRIPT_ASSERT(false, context, _y <= mapHeight, "Beacon y value %d is greater than mapHeight %d", _y, (int)mapHeight);

int x = world_coord(_x);
int y = world_coord(_y);

Expand Down Expand Up @@ -2345,7 +2355,7 @@ bool wzapi::isSpectator(WZAPI_PARAMS(int player))
nlohmann::json wzapi::getWeaponInfo(WZAPI_PARAMS(std::string weaponName)) WZAPI_DEPRECATED
{
int weaponIndex = getCompFromName(COMP_WEAPON, WzString::fromUtf8(weaponName));
SCRIPT_ASSERT(nlohmann::json(), context, weaponIndex >= 0, "No such weapon: %s", weaponName.c_str());
SCRIPT_ASSERT(nlohmann::json(), context, weaponIndex >= 0 && weaponIndex < asWeaponStats.size(), "No such weapon: %s", weaponName.c_str());
WEAPON_STATS *psStats = &asWeaponStats[weaponIndex];
nlohmann::json result = nlohmann::json::object();
result["id"] = weaponName;
Expand All @@ -2365,6 +2375,10 @@ nlohmann::json wzapi::getWeaponInfo(WZAPI_PARAMS(std::string weaponName)) WZAPI_
//--
bool wzapi::centreView(WZAPI_PARAMS(int x, int y))
{
SCRIPT_ASSERT(false, context, x >= 0, "x value %d is less than zero", x);
SCRIPT_ASSERT(false, context, y >= 0, "y value %d is less than zero", y);
SCRIPT_ASSERT(false, context, x <= mapWidth, "x value %d is greater than mapWidth %d", x, (int)mapWidth);
SCRIPT_ASSERT(false, context, y <= mapHeight, "y value %d is greater than mapHeight %d", y, (int)mapHeight);
setViewPos(x, y, false);
return true;
}
Expand Down Expand Up @@ -3168,13 +3182,12 @@ int wzapi::countDroid(WZAPI_PARAMS(optional<int> _droidType, optional<int> _play
//--
wzapi::no_return_value wzapi::loadLevel(WZAPI_PARAMS(std::string levelName))
{
sstrcpy(aLevelName, levelName.c_str());

// Find the level dataset
LEVEL_DATASET *psNewLevel = levFindDataSet(levelName.c_str());
SCRIPT_ASSERT({}, context, psNewLevel, "Could not find level data for %s", levelName.c_str());

// Get the mission rolling...
sstrcpy(aLevelName, levelName.c_str());
prevMissionType = mission.type;
nextMissionType = psNewLevel->type;
loopMissionState = LMS_CLEAROBJECTS;
Expand Down Expand Up @@ -3249,6 +3262,7 @@ bool wzapi::donateObject(WZAPI_PARAMS(BASE_OBJECT *psObject, int player))
bool wzapi::donatePower(WZAPI_PARAMS(int amount, int player))
{
int from = context.player();
SCRIPT_ASSERT_PLAYER(false, context, player);
giftPower(from, player, amount, true);
return true;
}
Expand Down
Loading