Skip to content

Commit

Permalink
Replace some output variables as return type.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarod42 committed Aug 8, 2023
1 parent f48dd09 commit 8e9cdaf
Show file tree
Hide file tree
Showing 26 changed files with 123 additions and 183 deletions.
3 changes: 1 addition & 2 deletions src/action/action_still.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ static bool MoveRandomly(CUnit &unit)
auto w = unit.Type->PersonalSpaceWidth;
auto h = unit.Type->PersonalSpaceHeight;
if (w || h) {
std::vector<CUnit *> around;
SelectAroundUnit(unit, (w + h) / 2, around, IsSameMovementType(unit));
std::vector<CUnit *> around = SelectAroundUnit(unit, (w + h) / 2, IsSameMovementType(unit));
Vec2i vec(0, 0);
for (auto u : around) {
if (u != &unit) {
Expand Down
5 changes: 1 addition & 4 deletions src/ai/ai_building.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,7 @@ bool HallPlaceFinder::IsAUsableMine(const CUnit &mine) const
const Vec2i minpos = mine.tilePos - offset;
const Vec2i typeSize(mine.Type->TileWidth - 1, mine.Type->TileHeight - 1);
const Vec2i maxpos = mine.tilePos + typeSize + offset;
std::vector<CUnit *> units;

Select(minpos, maxpos, units);

std::vector<CUnit *> units = Select(minpos, maxpos);
const size_t nunits = units.size();
int buildings = 0;

Expand Down
9 changes: 4 additions & 5 deletions src/ai/ai_force.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,9 @@ VisitResult EnemyUnitFinder::Visit(TerrainTraversal &terrainTraversal, const Vec
return VisitResult_DeadEnd;
}

std::vector<CUnit *> table;
Vec2i minpos = pos - Vec2i(attackrange, attackrange);
Vec2i maxpos = pos + Vec2i(unit.Type->TileWidth - 1 + attackrange, unit.Type->TileHeight - 1 + attackrange);
Select(minpos, maxpos, table, HasNotSamePlayerAs(Players[PlayerNumNeutral]));
std::vector<CUnit *> table = Select(minpos, maxpos, HasNotSamePlayerAs(Players[PlayerNumNeutral]));
for (size_t i = 0; i != table.size(); ++i) {
CUnit *dest = table[i];
const CUnitType &dtype = *dest->Type;
Expand Down Expand Up @@ -1180,11 +1179,11 @@ void AiForceManager::Update()

// Find idle units and order them to defend
// Don't attack if there aren't our units near goal point
std::vector<CUnit *> nearGoal;
const Vec2i offset(15, 15);
maxPathing--;
Select(force.GoalPos - offset, force.GoalPos + offset, nearGoal,
IsAnAlliedUnitOf(*force.Units[0]->Player));
std::vector<CUnit *> nearGoal = Select(force.GoalPos - offset,
force.GoalPos + offset,
IsAnAlliedUnitOf(*force.Units[0]->Player));
if (nearGoal.empty()) {
force.ReturnToHome();
} else {
Expand Down
22 changes: 7 additions & 15 deletions src/ai/ai_resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,16 +214,15 @@ bool AiEnemyUnitsInDistance(const CPlayer &player,
const CUnitType *type, const Vec2i &pos, unsigned range)
{
const Vec2i offset(range, range);
std::vector<CUnit *> units;

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

Select<1>(pos - offset, pos + typeSize + offset, units, pred);
std::vector<CUnit *> units = Select<1>(pos - offset, pos + typeSize + offset, pred);
return static_cast<int>(units.size());
}
}
Expand Down Expand Up @@ -273,9 +272,7 @@ static bool IsAlreadyWorking(const CUnit &unit)
*/
static int AiBuildBuilding(const CUnitType &type, CUnitType &building, const Vec2i &nearPos)
{
std::vector<CUnit *> table;

FindPlayerUnitsByType(*AiPlayer->Player, type, table, true);
std::vector<CUnit *> table = FindPlayerUnitsByType(*AiPlayer->Player, type, true);

int num = 0;

Expand Down Expand Up @@ -629,9 +626,8 @@ static bool AiRequestSupply()
*/
static bool AiTrainUnit(const CUnitType &type, CUnitType &what)
{
std::vector<CUnit *> table;
std::vector<CUnit *> table = FindPlayerUnitsByType(*AiPlayer->Player, type, true);

FindPlayerUnitsByType(*AiPlayer->Player, type, table, true);
for (size_t i = 0; i != table.size(); ++i) {
CUnit &unit = *table[i];

Expand Down Expand Up @@ -718,9 +714,8 @@ static int AiMakeUnit(CUnitType &typeToMake, const Vec2i &nearPos)
*/
static bool AiResearchUpgrade(const CUnitType &type, CUpgrade &what)
{
std::vector<CUnit *> table;
std::vector<CUnit *> table = FindPlayerUnitsByType(*AiPlayer->Player, type, true);

FindPlayerUnitsByType(*AiPlayer->Player, type, table, true);
for (size_t i = 0; i != table.size(); ++i) {
CUnit &unit = *table[i];

Expand Down Expand Up @@ -806,16 +801,14 @@ void AiAddResearchRequest(CUpgrade *upgrade)
*/
static bool AiUpgradeTo(const CUnitType &type, CUnitType &what)
{
std::vector<CUnit *> table;

if (GameSettings.AiChecksDependencies) {
if (!CheckDependByType(*AiPlayer->Player, what)) {
return false;
}
}

// Remove all units already doing something.
FindPlayerUnitsByType(*AiPlayer->Player, type, table, true);
std::vector<CUnit *> table = FindPlayerUnitsByType(*AiPlayer->Player, type, true);
for (size_t i = 0; i != table.size(); ++i) {
CUnit &unit = *table[i];

Expand Down Expand Up @@ -1281,8 +1274,7 @@ static bool AiRepairBuilding(const CPlayer &player, const CUnitType &type, CUnit
// We need to send all nearby free workers to repair that building
// AI shouldn't send workers that are far away from repair point
// Selection of mining workers.
std::vector<CUnit *> table;
FindPlayerUnitsByType(*AiPlayer->Player, type, table, true);
std::vector<CUnit *> table = FindPlayerUnitsByType(*AiPlayer->Player, type, true);
int num = 0;
for (size_t i = 0; i != table.size(); ++i) {
CUnit &unit = *table[i];
Expand Down
3 changes: 1 addition & 2 deletions src/editor/editloop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1175,8 +1175,7 @@ static void EditorCallbackButtonUp(unsigned button)

const Vec2i t0 = Map.MapPixelPosToTilePos(pos0);
const Vec2i t1 = Map.MapPixelPosToTilePos(pos1);
std::vector<CUnit *> table;
Select(t0, t1, table);
std::vector<CUnit *> table = Select(t0, t1);

if (!(KeyModifiers & ModifierShift)) {
UnSelectAll();
Expand Down
19 changes: 5 additions & 14 deletions src/game/trigger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,7 @@ static int CclGetNumUnitsAt(lua_State *l)
std::swap(minPos.y, maxPos.y);
}

std::vector<CUnit *> units;

Select(minPos, maxPos, units);
std::vector<CUnit *> units = Select(minPos, maxPos);

int s = 0;
for (size_t i = 0; i != units.size(); ++i) {
Expand Down Expand Up @@ -249,14 +247,10 @@ static int CclIfNearUnit(lua_State *l)
// Get all unit types 'near'.
//

std::vector<CUnit *> unitsOfType;

FindUnitsByType(*ut2, unitsOfType);
std::vector<CUnit *> unitsOfType = FindUnitsByType(*ut2);
for (size_t i = 0; i != unitsOfType.size(); ++i) {
const CUnit &centerUnit = *unitsOfType[i];

std::vector<CUnit *> around;
SelectAroundUnit(centerUnit, 1, around);
std::vector<CUnit *> around = SelectAroundUnit(centerUnit, 1);

// Count the requested units
int s = 0;
Expand Down Expand Up @@ -316,13 +310,10 @@ static int CclIfRescuedNearUnit(lua_State *l)
}

// Get all unit types 'near'.
std::vector<CUnit *> table;
FindUnitsByType(*ut2, table);
std::vector<CUnit *> table = FindUnitsByType(*ut2);
for (size_t i = 0; i != table.size(); ++i) {
CUnit &centerUnit = *table[i];
std::vector<CUnit *> around;

SelectAroundUnit(centerUnit, 1, around);
std::vector<CUnit *> around = SelectAroundUnit(centerUnit, 1);
// Count the requested units
int s = 0;
for (size_t j = 0; j != around.size(); ++j) {
Expand Down
2 changes: 1 addition & 1 deletion src/include/missile.h
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ extern int CalculateDamage(const CUnit &attacker, const CUnit &goal, const Numbe
/// fire a missile
extern void FireMissile(CUnit &unit, CUnit *goal, const Vec2i &goalPos);

extern void FindAndSortMissiles(const CViewport &vp, std::vector<Missile *> &table);
extern std::vector<Missile *> FindAndSortMissiles(const CViewport &);

/// handle all missiles
extern void MissileActions();
Expand Down
2 changes: 1 addition & 1 deletion src/include/particle.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class CParticleManager
static void init();
static void exit();

void prepareToDraw(const CViewport &vp, std::vector<CParticle *> &table);
std::vector<CParticle *> prepareToDraw(const CViewport &);
void endDraw();

void update();
Expand Down
4 changes: 2 additions & 2 deletions src/include/unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -684,8 +684,8 @@ extern void CleanDecorations();

/// Draw unit's shadow
extern void DrawShadow(const CUnitType &type, int frame, const PixelPos &screenPos, char zDisplacement = 0);
/// Draw all units visible on map in viewport
extern int FindAndSortUnits(const CViewport &vp, std::vector<CUnit *> &table);
/// Collect all units visible on map in viewport
extern std::vector<CUnit *> FindAndSortUnits(const CViewport &);

/// Show a unit's orders.
extern void ShowOrder(const CUnit &unit);
Expand Down
30 changes: 16 additions & 14 deletions src/include/unit_find.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,17 @@ class UnitFinder
CUnit **unitP;
};

void Select(const Vec2i &ltPos, const Vec2i &rbPos, std::vector<CUnit *> &units);
void SelectFixed(const Vec2i &ltPos, const Vec2i &rbPos, std::vector<CUnit *> &units);
void SelectAroundUnit(const CUnit &unit, int range, std::vector<CUnit *> &around);
std::vector<CUnit *> Select(const Vec2i &ltPos, const Vec2i &rbPos);
std::vector<CUnit *> SelectFixed(const Vec2i &ltPos, const Vec2i &rbPos);
std::vector<CUnit *> SelectAroundUnit(const CUnit &unit, int range);

template <int selectMax = 0, typename Pred>
void SelectFixed(const Vec2i &ltPos, const Vec2i &rbPos, std::vector<CUnit *> &units, Pred pred)
std::vector<CUnit *> SelectFixed(const Vec2i &ltPos, const Vec2i &rbPos, Pred pred)
{
Assert(Map.Info.IsPointOnMap(ltPos));
Assert(Map.Info.IsPointOnMap(rbPos));
Assert(units.empty());

std::vector<CUnit *> units;
units.reserve(selectMax << 1);
int max = selectMax ? selectMax : INT_MAX;

Expand All @@ -250,7 +251,7 @@ void SelectFixed(const Vec2i &ltPos, const Vec2i &rbPos, std::vector<CUnit *> &u
if ((selectMax == 1 || unit.CacheLock == 0) && pred(&unit)) {
if (selectMax == 1) {
units.push_back(&unit);
return;
return units;
} else {
unit.CacheLock = 1;
units.push_back(&unit);
Expand All @@ -265,27 +266,28 @@ void SelectFixed(const Vec2i &ltPos, const Vec2i &rbPos, std::vector<CUnit *> &u
for (size_t i = 0; i != units.size(); ++i) {
units[i]->CacheLock = 0;
}
return units;
}

template <int selectMax = 0, typename Pred>
void Select(const Vec2i &ltPos, const Vec2i &rbPos, std::vector<CUnit *> &units, Pred pred)
std::vector<CUnit *> Select(const Vec2i &ltPos, const Vec2i &rbPos, Pred pred)
{
Vec2i minPos = ltPos;
Vec2i maxPos = rbPos;

Map.FixSelectionArea(minPos, maxPos);
SelectFixed<selectMax>(minPos, maxPos, units, pred);
return SelectFixed<selectMax>(minPos, maxPos, pred);
}

template <int selectMax = 0, typename Pred>
void SelectAroundUnit(const CUnit &unit, int range, std::vector<CUnit *> &around, Pred pred)
std::vector<CUnit *> SelectAroundUnit(const CUnit &unit, int range, Pred pred)
{
const Vec2i offset(range, range);
const Vec2i typeSize(unit.Type->TileWidth - 1, unit.Type->TileHeight - 1);

Select<selectMax>(unit.tilePos - offset,
unit.tilePos + typeSize + offset, around,
MakeAndPredicate(IsNotTheSameUnitAs(unit), pred));
return Select<selectMax>(unit.tilePos - offset,
unit.tilePos + typeSize + offset,
MakeAndPredicate(IsNotTheSameUnitAs(unit), pred));
}

template <typename Pred>
Expand Down Expand Up @@ -331,10 +333,10 @@ extern CUnit *FindIdleWorker(const CPlayer &player, const CUnit *last);
extern bool FindTerrainType(int movemask, int resmask, int range,
const CPlayer &player, const Vec2i &startPos, Vec2i *pos);

extern void FindUnitsByType(const CUnitType &type, std::vector<CUnit *> &units, bool everybody = false);
extern std::vector<CUnit *> FindUnitsByType(const CUnitType &type, bool everybody = false);

/// Find all units of this type of the player
extern void FindPlayerUnitsByType(const CPlayer &player, const CUnitType &type, std::vector<CUnit *> &units, bool ai_active = false);
extern std::vector<CUnit *> FindPlayerUnitsByType(const CPlayer &player, const CUnitType &type, bool ai_active = false);
/// Return any unit on that map tile
extern CUnit *UnitOnMapTile(const Vec2i &pos, unsigned int type);// = -1);
/// Return possible attack target on that map area
Expand Down
9 changes: 3 additions & 6 deletions src/map/map_draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,15 +433,12 @@ void CViewport::Draw(const fieldHighlightChecker highlightChecker /* = nullptr *
CurrentViewport = this;
{
// Now we need to sort units, missiles, particles by draw level and draw them
std::vector<CUnit *> unittable;
std::vector<Missile *> missiletable;
std::vector<CParticle *> particletable;
const std::vector<CUnit *> unittable = FindAndSortUnits(*this);
const std::vector<Missile *> missiletable = FindAndSortMissiles(*this);
const std::vector<CParticle *> particletable = ParticleManager.prepareToDraw(*this);

FindAndSortUnits(*this, unittable);
const size_t nunits = unittable.size();
FindAndSortMissiles(*this, missiletable);
const size_t nmissiles = missiletable.size();
ParticleManager.prepareToDraw(*this, particletable);
const size_t nparticles = particletable.size();

size_t i = 0;
Expand Down
15 changes: 7 additions & 8 deletions src/missile/missile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -572,10 +572,11 @@ static bool MissileDrawLevelCompare(const Missile *const l, const Missile *const
** Sort visible missiles on map for display.
**
** @param vp Viewport pointer.
** @param table OUT : array of missile to display sorted by DrawLevel.
** @return array of missile to display sorted by DrawLevel.
*/
void FindAndSortMissiles(const CViewport &vp, std::vector<Missile *> &table)
std::vector<Missile *> FindAndSortMissiles(const CViewport &vp)
{
std::vector<Missile *> table;
// Loop through global missiles, then through locals.
for (auto* missilePtr : GlobalMissiles) {
Missile &missile = *missilePtr;
Expand All @@ -597,6 +598,7 @@ void FindAndSortMissiles(const CViewport &vp, std::vector<Missile *> &table)
table.push_back(&missile);
}
ranges::sort(table, MissileDrawLevelCompare);
return table;
}

/**
Expand Down Expand Up @@ -667,8 +669,7 @@ void MissileHandlePierce(Missile &missile, const Vec2i &pos)
if (Map.Info.IsPointOnMap(pos) == false) {
return;
}
std::vector<CUnit *> units;
Select(pos, pos, units);
std::vector<CUnit *> units = Select(pos, pos);
for (CUnit *unitPtr : units) {
CUnit &unit = *unitPtr;

Expand All @@ -692,9 +693,8 @@ bool MissileHandleBlocking(Missile &missile, const PixelPos &position)
}
if (shouldHit) {
// search for blocking units
std::vector<CUnit *> blockingUnits;
const Vec2i missilePos = Map.MapPixelPosToTilePos(position);
Select(missilePos, missilePos, blockingUnits);
std::vector<CUnit *> blockingUnits = Select(missilePos, missilePos);
for (CUnit *unitPtr : blockingUnits) {
CUnit &unit = *unitPtr;
// If land unit shoots at land unit, missile can be blocked by Wall units
Expand Down Expand Up @@ -993,8 +993,7 @@ void Missile::MissileHit(CUnit *unit)
// Hits all units in range.
//
const Vec2i range(mtype.Range - 1, mtype.Range - 1);
std::vector<CUnit *> table;
Select(pos - range, pos + range, table);
std::vector<CUnit *> table = Select(pos - range, pos + range);
Assert(this->SourceUnit != nullptr);
for (size_t i = 0; i != table.size(); ++i) {
CUnit &goal = *table[i];
Expand Down
3 changes: 1 addition & 2 deletions src/missile/missile_flameshield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ void MissileFlameShield::Action()
return;
}

std::vector<CUnit *> table;
SelectAroundUnit(*unit, 1, table);
std::vector<CUnit *> table = SelectAroundUnit(*unit, 1);
for (size_t i = 0; i != table.size(); ++i) {
if (table[i]->CurrentAction() != UnitActionDie) {
HitUnit(this->SourceUnit, *table[i], this->Damage);
Expand Down
4 changes: 3 additions & 1 deletion src/particle/particlemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ static inline bool DrawLevelCompare(const CParticle *lhs, const CParticle *rhs)
return lhs->getDrawLevel() < rhs->getDrawLevel();
}

void CParticleManager::prepareToDraw(const CViewport &vp, std::vector<CParticle *> &table)
std::vector<CParticle *> CParticleManager::prepareToDraw(const CViewport &vp)
{
this->vp = &vp;
std::vector<CParticle *> table;

for (CParticle *p : particles) {
CParticle &particle = *p;
Expand All @@ -88,6 +89,7 @@ void CParticleManager::prepareToDraw(const CViewport &vp, std::vector<CParticle
}

ranges::sort(table, DrawLevelCompare);
return table;
}

void CParticleManager::endDraw()
Expand Down
Loading

0 comments on commit 8e9cdaf

Please sign in to comment.