Skip to content

Commit dcf0b09

Browse files
committed
Refactor MilUpgradeOptim
Clang (wrongly) detects `milBld` as (potentially) NULL. Restructure the method for less nesting and less conditions, possibly avoiding this.
1 parent 1d43ffc commit dcf0b09

1 file changed

Lines changed: 28 additions & 36 deletions

File tree

libs/s25main/ai/aijh/AIPlayerJH.cpp

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,48 +1383,42 @@ void AIPlayerJH::HandleLostLand(const MapPoint pt)
13831383

13841384
void AIPlayerJH::MilUpgradeOptim()
13851385
{
1386-
// do we have a upgrade building?
13871386
const auto* upgradeBld = UpdateUpgradeBuilding();
1388-
unsigned count = 0;
13891387
const std::list<nobMilitary*>& militaryBuildings = aii.GetMilitaryBuildings();
1388+
const auto numPlannedConnectedInlandMilitaryBlds = GetNumPlannedConnectedInlandMilitaryBlds();
1389+
unsigned count = 0;
13901390
for(const nobMilitary* milBld : militaryBuildings)
13911391
{
1392-
if(milBld != upgradeBld) // not upgrade building
1392+
if(!upgradeBld)
1393+
{
1394+
if(milBld->IsGoldDisabled() && milBld->GetFrontierDistance() != FrontierDistance::Far)
1395+
aii.SetCoinsAllowed(milBld->GetPos(), true);
1396+
} else if(milBld != upgradeBld) // not upgrade building
13931397
{
1394-
if(upgradeBld) // we do have an upgrade building
1398+
if(!milBld->IsGoldDisabled()) // deactivate gold for all other buildings
1399+
aii.SetCoinsAllowed(milBld->GetPos(), false);
1400+
// For dedicated inland buildings send out troops until 1 private is left, then cancel road
1401+
// Connect frontier buildings to road system.
1402+
if(milBld->GetFrontierDistance() != FrontierDistance::Far)
1403+
construction->AddConnectFlagJob(milBld->GetFlag());
1404+
else if(count + numPlannedConnectedInlandMilitaryBlds < militaryBuildings.size())
13951405
{
1396-
if(!milBld->IsGoldDisabled()) // deactivate gold for all other buildings
1397-
aii.SetCoinsAllowed(milBld->GetPos(), false);
1398-
if(milBld->GetFrontierDistance() == FrontierDistance::Far
1399-
&& (count + GetNumPlannedConnectedInlandMilitaryBlds()
1400-
< militaryBuildings.size())) // send out troops until 1 private is left, then cancel road
1406+
if(milBld->GetNumTroops() > 1) // more than 1 soldier remaining? -> send out order
14011407
{
1402-
if(milBld->GetNumTroops() > 1) // more than 1 soldier remaining? -> send out order
1403-
{
1404-
aii.SetTroopLimit(milBld->GetPos(), 0, 1);
1405-
for(unsigned rank = 1; rank < NUM_SOLDIER_RANKS; ++rank)
1406-
aii.SetTroopLimit(milBld->GetPos(), rank, 0);
1407-
1408-
// TODO: Currently the AI still manages soldiers by disconnecting roads, if in the future it
1409-
// uses only SetTroopLimit then this can be removed
1410-
for(unsigned rank = 0; rank < NUM_SOLDIER_RANKS; ++rank)
1411-
aii.SetTroopLimit(milBld->GetPos(), rank, milBld->GetMaxTroopsCt());
1412-
} else if(!milBld->IsNewBuilt()) // 0-1 soldier remains and the building has had at least 1 soldier
1413-
// at some point and the building is not new on the list-> cancel
1414-
// road (and fix roadsystem if necessary)
1415-
{
1416-
RemoveUnusedRoad(*milBld->GetFlag(), Direction::NorthWest, true, true, true);
1417-
}
1418-
} else if(milBld->GetFrontierDistance()
1419-
!= FrontierDistance::Far) // frontier building - connect to road system
1420-
{
1421-
construction->AddConnectFlagJob(milBld->GetFlag());
1422-
}
1423-
} else // no upgrade building? -> activate gold for frontier buildings
1424-
{
1425-
if(milBld->IsGoldDisabled() && milBld->GetFrontierDistance() != FrontierDistance::Far)
1408+
aii.SetTroopLimit(milBld->GetPos(), 0, 1);
1409+
for(unsigned rank = 1; rank < NUM_SOLDIER_RANKS; ++rank)
1410+
aii.SetTroopLimit(milBld->GetPos(), rank, 0);
1411+
1412+
// TODO: Currently the AI still manages soldiers by disconnecting roads, if in the future it
1413+
// uses only SetTroopLimit then this can be removed
1414+
for(unsigned rank = 0; rank < NUM_SOLDIER_RANKS; ++rank)
1415+
aii.SetTroopLimit(milBld->GetPos(), rank, milBld->GetMaxTroopsCt());
1416+
} else if(!milBld->IsNewBuilt())
14261417
{
1427-
aii.SetCoinsAllowed(milBld->GetPos(), true);
1418+
// 0-1 soldier remains and the building has had at least 1 soldier
1419+
// at some point and the building is not new on the list
1420+
// -> cancel road (and fix roadsystem if necessary)
1421+
RemoveUnusedRoad(*milBld->GetFlag(), Direction::NorthWest, true, true, true);
14281422
}
14291423
}
14301424
} else // upgrade building
@@ -1435,9 +1429,7 @@ void AIPlayerJH::MilUpgradeOptim()
14351429
continue;
14361430
}
14371431
if(milBld->IsGoldDisabled()) // activate gold
1438-
{
14391432
aii.SetCoinsAllowed(milBld->GetPos(), true);
1440-
}
14411433
// Keep 0 max rank soldiers, 1 of each other rank and fill the rest with privates
14421434
aii.SetTroopLimit(milBld->GetPos(), 0, milBld->GetMaxTroopsCt());
14431435
for(unsigned rank = 1; rank < ggs.GetMaxMilitaryRank(); ++rank)

0 commit comments

Comments
 (0)