Skip to content

Commit

Permalink
Merge pull request #521 from Wargus/clean_up
Browse files Browse the repository at this point in the history
Clean up
  • Loading branch information
Jarod42 authored Oct 1, 2023
2 parents 5555720 + 585f3ea commit 19ecd27
Show file tree
Hide file tree
Showing 28 changed files with 159 additions and 192 deletions.
13 changes: 5 additions & 8 deletions src/ai/ai_force.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,23 +573,20 @@ unsigned int AiForceManager::FindFreeForce(AiForceRole role, int begin)
**
** @param unit Unit to search for.
**
** @return Force number, or -1 if not found
** @return Force number, or std::nullopt if not found
*/

int AiForceManager::GetForce(const CUnit &unit)
std::optional<int> AiForceManager::GetForce(const CUnit &unit)
{
for (unsigned int i = 0; i < forces.size(); ++i) {
AiForce &force = forces[i];

for (unsigned int j = 0; j < force.Units.size(); ++j) {
CUnit &aiunit = *force.Units[j];

if (UnitNumber(unit) == UnitNumber(aiunit)) {
for (CUnit *aiunit : force.Units) {
if (UnitNumber(unit) == UnitNumber(*aiunit)) {
return i;
}
}
}
return -1;
return std::nullopt;
}

/**
Expand Down
13 changes: 7 additions & 6 deletions src/ai/ai_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
----------------------------------------------------------------------------*/

#include <array>
#include <optional>
#include <vector>

#include "upgrade_structs.h" // MaxCost
Expand Down Expand Up @@ -157,7 +158,7 @@ class AiForce

void Attack(const Vec2i &pos);
void RemoveDeadUnit();
int PlanAttack();
bool PlanAttack();

void ReturnToHome();
bool NewRallyPoint(const Vec2i &startPos, Vec2i *resultPos);
Expand Down Expand Up @@ -207,14 +208,14 @@ class AiForceManager
const AiForce &operator[](unsigned int index) const { return forces[index]; }
AiForce &operator[](unsigned int index) { return forces[index]; }

int getIndex(AiForce *force) const
int getIndex(const AiForce &force) const
{
for (unsigned int i = 0; i < forces.size(); ++i) {
if (force == &forces[i]) {
if (&force == &forces[i]) {
return i;
}
}
return -1;
throw std::runtime_error("Invalid force");
}

unsigned int getScriptForce(unsigned int index)
Expand All @@ -225,7 +226,7 @@ class AiForceManager
return script[index];
}

int GetForce(const CUnit &unit);
std::optional<int> GetForce(const CUnit &unit);
void RemoveDeadUnit();
bool Assign(CUnit &unit, int force = -1);
void Update();
Expand Down Expand Up @@ -442,7 +443,7 @@ extern void AiForceManager();
// Plans
//
/// Find a wall to attack
extern int AiFindWall(AiForce *force);
extern bool AiFindWall(AiForce *force);
/// Plan the an attack
/// Send explorers around the map
extern void AiSendExplorers();
Expand Down
18 changes: 9 additions & 9 deletions src/ai/ai_plan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ static bool FindWall(const CUnit &unit, int range, Vec2i *wallPos)
**
** @return True if wall found.
*/
int AiFindWall(AiForce *force)
bool AiFindWall(AiForce *force)
{
// Find a unit to use. Best choice is a land unit with range 1.
// Next best choice is any land unit. Otherwise just use the first.
Expand All @@ -210,9 +210,9 @@ int AiFindWall(AiForce *force)
CommandMove(aiunit, wallPos, FlushCommands);
}
}
return 1;
return true;
}
return 0;
return false;
}

class ReachableTerrainMarker
Expand Down Expand Up @@ -342,7 +342,7 @@ int GetTotalBoardCapacity(ITERATOR begin, ITERATOR end)
** @todo transporter are more selective now (flag with unittypeland).
** We must manage it.
*/
int AiForce::PlanAttack()
bool AiForce::PlanAttack()
{
CPlayer &player = *AiPlayer->Player;
DebugPrint("%d: Planning for force #%lu of player #%d\n",
Expand All @@ -369,7 +369,7 @@ int AiForce::PlanAttack()
MarkReacheableTerrainType(*transporter, &transporterTerrainTraversal);
} else {
DebugPrint("%d: No transporter available\n", player.Index);
return 0;
return false;
}
}

Expand All @@ -378,15 +378,15 @@ int AiForce::PlanAttack()
CUnit *landUnit = nullptr;
if (auto it = ranges::find_if(Units, CUnitTypeFinder(UnitTypeLand)); it == Units.end()) {
DebugPrint("%d: No land unit in force\n", player.Index);
return 0;
return false;
} else {
landUnit = *it;
}

Vec2i pos = this->GoalPos;

if (AiFindTarget(*landUnit, transporterTerrainTraversal, &pos)) {
const unsigned int forceIndex = AiPlayer->Force.getIndex(this) + 1;
const unsigned int forceIndex = AiPlayer->Force.getIndex(*this) + 1;

if (transporter->GroupId != forceIndex) {
DebugPrint("%d: Assign any transporter #%d\n", player.Index, UnitNumber(*transporter));
Expand Down Expand Up @@ -427,9 +427,9 @@ int AiForce::PlanAttack()
DebugPrint("%d: Can attack\n", player.Index);
GoalPos = pos;
State = AiForceAttackingState::Boarding;
return 1;
return true;
}
return 0;
return false;
}

static bool ChooseRandomUnexploredPositionNear(const Vec2i &center, Vec2i *pos)
Expand Down
64 changes: 32 additions & 32 deletions src/ai/ai_resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
-- Functions
----------------------------------------------------------------------------*/

static int AiMakeUnit(CUnitType &type, const Vec2i &nearPos);
static bool AiMakeUnit(CUnitType &type, const Vec2i &nearPos);

/**
** Check if the costs are available for the AI.
Expand Down Expand Up @@ -122,7 +122,7 @@ static int AiCheckCosts(const int (&costs)[MaxCosts])
** @todo The number of food currently trained can be stored global
** for faster use.
*/
static int AiCheckSupply(const PlayerAi &pai, const CUnitType &type)
static bool AiCheckSupply(const PlayerAi &pai, const CUnitType &type)
{
// Count food supplies under construction.
int remaining = 0;
Expand All @@ -135,16 +135,16 @@ static int AiCheckSupply(const PlayerAi &pai, const CUnitType &type)
// We are already out of food.
remaining += pai.Player->Supply - pai.Player->Demand - type.Stats[pai.Player->Index].Variables[DEMAND_INDEX].Value;
if (remaining < 0) {
return 0;
return false;
}
// Count what we train.
for (const AiBuildQueue &queue : pai.UnitTypeBuilt) {
remaining -= queue.Made * queue.Type->Stats[pai.Player->Index].Variables[DEMAND_INDEX].Value;
if (remaining < 0) {
return 0;
return false;
}
}
return 1;
return true;
}

/**
Expand Down Expand Up @@ -209,13 +209,13 @@ bool AiEnemyUnitsInDistance(const CPlayer &player,

if (type == nullptr) {
std::vector<CUnit *> units = Select<1>(pos - offset, pos + offset, IsAEnemyUnitOf<true>(player));
return static_cast<int>(units.size());
return !units.empty();
} else {
const Vec2i typeSize(type->TileWidth - 1, type->TileHeight - 1);
const IsAEnemyUnitWhichCanCounterAttackOf<true> pred(player, *type);

std::vector<CUnit *> units = Select<1>(pos - offset, pos + typeSize + offset, pred);
return static_cast<int>(units.size());
return !units.empty();
}
}

Expand Down Expand Up @@ -262,7 +262,7 @@ static bool IsAlreadyWorking(const CUnit &unit)
**
** @note We must check if the dependencies are fulfilled.
*/
static int AiBuildBuilding(const CUnitType &type, CUnitType &building, const Vec2i &nearPos)
static bool AiBuildBuilding(const CUnitType &type, CUnitType &building, const Vec2i &nearPos)
{
std::vector<CUnit *> table = FindPlayerUnitsByType(*AiPlayer->Player, type, true);

Expand All @@ -278,7 +278,7 @@ static int AiBuildBuilding(const CUnitType &type, CUnitType &building, const Vec
}
if (num == 0) {
// No workers available to build
return 0;
return false;
}

CUnit &unit = (num == 1) ? *table[0] : *table[SyncRand() % num];
Expand All @@ -287,7 +287,7 @@ static int AiBuildBuilding(const CUnitType &type, CUnitType &building, const Vec
// Find a place to build.
if (AiFindBuildingPlace(unit, building, nearPos, &pos)) {
CommandBuildBuilding(unit, pos, building, FlushCommands);
return 1;
return true;
} else {
//when first worker can't build then rest also won't be able (save CPU)
if (Map.Info.IsPointOnMap(nearPos)) {
Expand All @@ -296,12 +296,12 @@ static int AiBuildBuilding(const CUnitType &type, CUnitType &building, const Vec
// Find a place to build.
if (AiFindBuildingPlace(*table[i], building, nearPos, &pos)) {
CommandBuildBuilding(*table[i], pos, building, FlushCommands);
return 1;
return true;
}
}
}
}
return 0;
return false;
}

static bool AiRequestedTypeAllowed(const CPlayer &player, const CUnitType &type)
Expand Down Expand Up @@ -631,7 +631,7 @@ static bool AiTrainUnit(const CUnitType &type, CUnitType &what)
**
** @note We must check if the dependencies are fulfilled.
*/
static int AiMakeUnit(CUnitType &typeToMake, const Vec2i &nearPos)
static bool AiMakeUnit(CUnitType &typeToMake, const Vec2i &nearPos)
{
// Find equivalents unittypes.
int usableTypes[UnitTypeMax + 1];
Expand Down Expand Up @@ -674,17 +674,17 @@ static int AiMakeUnit(CUnitType &typeToMake, const Vec2i &nearPos)
if (unit_count[table[i]->Slot]) {
if (type.Building) {
if (AiBuildBuilding(*table[i], type, nearPos)) {
return 1;
return true;
}
} else {
if (AiTrainUnit(*table[i], type)) {
return 1;
return true;
}
}
}
}
}
return 0;
return false;
}

/**
Expand Down Expand Up @@ -925,23 +925,23 @@ static void AiCheckingWork()
** @param unit pointer to the unit.
** @param resource resource identification.
**
** @return 1 if the worker was assigned, 0 otherwise.
** @return true if the worker was assigned, false otherwise.
*/
static int AiAssignHarvesterFromTerrain(CUnit &unit, int resource)
static bool AiAssignHarvesterFromTerrain(CUnit &unit, int resource)
{
// TODO : hardcoded forest
Vec2i forestPos;

// Code for terrain harvesters. Search for piece of terrain to mine.
if (FindTerrainType(unit.Type->MovementMask, MapFieldForest, 1000, *unit.Player, unit.tilePos, &forestPos)) {
CommandResourceLoc(unit, forestPos, FlushCommands);
return 1;
return true;
}
// Ask the AI to explore...
AiExplore(unit.tilePos, MapFieldLandUnit);

// Failed.
return 0;
return false;
}

/**
Expand All @@ -950,9 +950,9 @@ static int AiAssignHarvesterFromTerrain(CUnit &unit, int resource)
** @param unit pointer to the unit.
** @param resource resource identification.
**
** @return 1 if the worker was assigned, 0 otherwise.
** @return true if the worker was assigned, false otherwise.
*/
static int AiAssignHarvesterFromUnit(CUnit &unit, int resource)
static bool AiAssignHarvesterFromUnit(CUnit &unit, int resource)
{
// Try to find the nearest depot first.
CUnit *depot = FindDeposit(unit, 1000, resource);
Expand All @@ -961,7 +961,7 @@ static int AiAssignHarvesterFromUnit(CUnit &unit, int resource)

if (mine) {
CommandResource(unit, *mine, FlushCommands);
return 1;
return true;
}

int exploremask = 0;
Expand All @@ -988,21 +988,21 @@ static int AiAssignHarvesterFromUnit(CUnit &unit, int resource)
// Ask the AI to explore
AiExplore(unit.tilePos, exploremask);
// Failed.
return 0;
return false;
}
/**
** Assign worker to gather a certain resource.
**
** @param unit pointer to the unit.
** @param resource resource identification.
**
** @return 1 if the worker was assigned, 0 otherwise.
** @return true if the worker was assigned, false otherwise.
*/
static int AiAssignHarvester(CUnit &unit, int resource)
static bool AiAssignHarvester(CUnit &unit, int resource)
{
// It can't.
if (unit.Removed) {
return 0;
return false;
}

const ResourceInfo &resinfo = *unit.Type->ResInfo[resource];
Expand Down Expand Up @@ -1296,7 +1296,7 @@ static bool AiRepairBuilding(const CPlayer &player, const CUnitType &type, CUnit
**
** @return True if made, false if can't be made.
*/
static int AiRepairUnit(CUnit &unit)
static bool AiRepairUnit(CUnit &unit)
{
int n = AiHelpers.Repair().size();
std::vector<std::vector<CUnitType *> > &tablep = AiHelpers.Repair();
Expand All @@ -1305,14 +1305,14 @@ static int AiRepairUnit(CUnit &unit)
DebugPrint("%d: AiRepairUnit I: Nothing known about '%s'\n",
AiPlayer->Player->Index,
type.Ident.c_str());
return 0;
return false;
}
std::vector<CUnitType *> &table = tablep[type.Slot];
if (table.empty()) { // Oops not known.
DebugPrint("%d: AiRepairUnit II: Nothing known about '%s'\n",
AiPlayer->Player->Index,
type.Ident.c_str());
return 0;
return false;
}

const int *unit_count = AiPlayer->Player->UnitTypesAiActiveCount;
Expand All @@ -1322,11 +1322,11 @@ static int AiRepairUnit(CUnit &unit)
//
if (unit_count[table[i]->Slot]) {
if (AiRepairBuilding(*AiPlayer->Player, *table[i], unit)) {
return 1;
return true;
}
}
}
return 0;
return false;
}

/**
Expand Down
Loading

0 comments on commit 19ecd27

Please sign in to comment.