Skip to content

Commit

Permalink
Clean up Assert usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarod42 committed Oct 14, 2023
1 parent 68dd6e0 commit 1c90625
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/action/action_resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -989,9 +989,9 @@ int COrder_Resource::StopGathering(CUnit &unit)
int COrder_Resource::MoveToDepot(CUnit &unit)
{
const ResourceInfo &resinfo = *unit.Type->ResInfo[this->CurrentResource];
Assert(this->GetGoal());
CUnit &goal = *this->GetGoal();
CPlayer &player = *unit.Player;
Assert(&goal);

switch (DoActionMove(unit)) { // reached end-point?
case PF_UNREACHABLE:
Expand Down
29 changes: 14 additions & 15 deletions src/ai/ai_plan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,31 +430,31 @@ bool AiForce::PlanAttack()
return false;
}

static bool ChooseRandomUnexploredPositionNear(const Vec2i &center, Vec2i *pos)
static std::optional<Vec2i> ChooseRandomUnexploredPositionNear(const Vec2i &center)
{
Assert(pos != nullptr);

int ray = 3;
const int maxTryCount = 8;
for (int i = 0; i != maxTryCount; ++i) {
pos->x = center.x + SyncRand() % (2 * ray + 1) - ray;
pos->y = center.y + SyncRand() % (2 * ray + 1) - ray;
Vec2i pos;
pos.x = center.x + SyncRand() % (2 * ray + 1) - ray;
pos.y = center.y + SyncRand() % (2 * ray + 1) - ray;

if (Map.Info.IsPointOnMap(*pos)
&& Map.Field(*pos)->playerInfo.IsExplored(*AiPlayer->Player) == false) {
return true;
if (Map.Info.IsPointOnMap(pos)
&& Map.Field(pos)->playerInfo.IsExplored(*AiPlayer->Player) == false) {
return pos;
}
ray = 3 * ray / 2;
}
return false;
return std::nullopt;
}

static CUnit *GetBestExplorer(const AiExplorationRequest &request, Vec2i *pos)
static std::pair<CUnit *, Vec2i> GetBestExplorer(const AiExplorationRequest &request)
{
// Choose a target, "near"
const Vec2i &center = request.pos;
if (ChooseRandomUnexploredPositionNear(center, pos) == false) {
return nullptr;
auto pos = ChooseRandomUnexploredPositionNear(center);
if (!pos) {
return {nullptr, {-1, -1}};
}
// We have an unexplored tile in sight (pos)

Expand Down Expand Up @@ -495,7 +495,7 @@ static CUnit *GetBestExplorer(const AiExplorationRequest &request, Vec2i *pos)
bestunit = unit;
}
}
return bestunit;
return {bestunit, *pos};
}


Expand All @@ -517,8 +517,7 @@ void AiSendExplorers()
const int requestid = SyncRand() % requestcount;
const AiExplorationRequest &request = AiPlayer->FirstExplorationRequest[requestid];

Vec2i pos;
CUnit *bestunit = GetBestExplorer(request, &pos);
const auto& [bestunit, pos] = GetBestExplorer(request);
if (bestunit != nullptr) {
CommandMove(*bestunit, pos, FlushCommands);
AiPlayer->LastExplorationGameCycle = GameCycle;
Expand Down
2 changes: 1 addition & 1 deletion src/ai/ai_resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -997,8 +997,8 @@ static bool AiAssignHarvester(CUnit &unit, int resource)
return false;
}

Assert(unit.Type->ResInfo[resource]);
const ResourceInfo &resinfo = *unit.Type->ResInfo[resource];
Assert(&resinfo);

if (resinfo.TerrainHarvester) {
return AiAssignHarvesterFromTerrain(unit, resource);
Expand Down
2 changes: 1 addition & 1 deletion src/include/unit_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class CUnitManager
void Init();

CUnit *AllocUnit();
void ReleaseUnit(CUnit *unit);
void ReleaseUnit(CUnit &unit);
void Save(CFile &file) const;
void Load(lua_State *Lua);

Expand Down
8 changes: 4 additions & 4 deletions src/network/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ void ExecCommand(unsigned char msgnr, UnitRef unum,
CUnit *dest = nullptr;
if (dstnr != (unsigned short)0xFFFF) {
dest = &UnitManager->GetSlotUnit(dstnr);
Assert(dest && dest->Type);
Assert(dest->Type);
}
CommandLog("repair", &unit, status, pos.x, pos.y, dest, nullptr, -1);
CommandRepair(unit, pos, dest, status);
Expand All @@ -631,7 +631,7 @@ void ExecCommand(unsigned char msgnr, UnitRef unum,
CUnit *dest = nullptr;
if (dstnr != (unsigned short)0xFFFF) {
dest = &UnitManager->GetSlotUnit(dstnr);
Assert(dest && dest->Type);
Assert(dest->Type);
}
CommandLog("attack", &unit, status, pos.x, pos.y, dest, nullptr, -1);
CommandAttack(unit, pos, dest, status);
Expand All @@ -658,7 +658,7 @@ void ExecCommand(unsigned char msgnr, UnitRef unum,
CUnit *dest = nullptr;
if (dstnr != (unsigned short)0xFFFF) {
dest = &UnitManager->GetSlotUnit(dstnr);
Assert(dest && dest->Type);
Assert(dest->Type);
}
CommandLog("unload", &unit, status, pos.x, pos.y, dest, nullptr, -1);
CommandUnload(unit, pos, dest, status);
Expand Down Expand Up @@ -734,7 +734,7 @@ void ExecCommand(unsigned char msgnr, UnitRef unum,
CUnit *dest = nullptr;
if (dstnr != (unsigned short)0xFFFF) {
dest = &UnitManager->GetSlotUnit(dstnr);
Assert(dest && dest->Type);
Assert(dest->Type);
}
CommandLog("spell-cast", &unit, status, pos.x, pos.y, dest, nullptr, id);
CommandSpellCast(unit, pos, dest, *SpellTypeTable[id], status);
Expand Down
1 change: 0 additions & 1 deletion src/ui/botpanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ void AddButton(int pos, int level, const std::string &icon_ident,
{
char buf[2048];
ButtonAction *ba = new ButtonAction;
Assert(ba);

ba->Pos = pos;
ba->Level = level;
Expand Down
2 changes: 1 addition & 1 deletion src/ui/mainscr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,8 +544,8 @@ static void DrawUnitInfo(CUnit &unit)
}
}

Assert(unit.Type);
const CUnitType &type = *unit.Type;
Assert(&type);

// Draw IconUnit
DrawUnitInfo_portrait(unit);
Expand Down
11 changes: 3 additions & 8 deletions src/unit/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ void CUnit::Release(bool final)
CriticalOrder = nullptr;

// Remove the unit from the global units table.
UnitManager->ReleaseUnit(this);
UnitManager->ReleaseUnit(*this);
}

UnitAction CUnit::CurrentAction() const
Expand Down Expand Up @@ -786,9 +786,8 @@ CUnit *MakeUnit(const CUnitType &type, CPlayer *player)
** @param f2 Function to (un)mark for cloaking vision.
*/
static void MapMarkUnitSightRec(const CUnit &unit, const Vec2i &pos, int width, int height,
MapMarkerFunc *f, MapMarkerFunc *f2)
MapMarkerFunc &f, MapMarkerFunc *f2)
{
Assert(f);
MapSight(*unit.Player, unit, pos, width, height,
unit.Container ? unit.Container->CurrentSightRange : unit.CurrentSightRange, f);

Expand Down Expand Up @@ -1329,10 +1328,9 @@ void CUnit::Remove(CUnit *host)
*/
void UnitLost(CUnit &unit)
{
Assert(unit.Player); // Next code didn't support no player!
CPlayer &player = *unit.Player;

Assert(&player); // Next code didn't support no player!

// Call back to AI, for killed or lost units.
if (Editor.Running == EditorNotRunning) {
if (player.AiEnabled) {
Expand Down Expand Up @@ -2695,7 +2693,6 @@ int TargetPriorityCalculate(const CUnit &attacker, const CUnit &dest)
*/
bool InReactRange(const CUnit &unit, const CUnit &target)
{
Assert(&target != nullptr);
const int distance = unit.MapDistanceTo(target);
const int range = (unit.Player->Type == PlayerTypes::PlayerPerson)
? unit.Type->ReactRangePerson
Expand All @@ -2713,7 +2710,6 @@ bool InReactRange(const CUnit &unit, const CUnit &target)
*/
bool InAttackRange(const CUnit &unit, const CUnit &target)
{
Assert(&target != nullptr);
const int range = unit.Stats->Variables[ATTACKRANGE_INDEX].Max;
const int minRange = unit.Type->MinAttackRange;
const int distance = unit.Container ? unit.Container->MapDistanceTo(target)
Expand Down Expand Up @@ -2760,7 +2756,6 @@ bool InAttackRange(const CUnit &unit, const Vec2i &tilePos)
*/
Vec2i GetRndPosInDirection(const Vec2i &srcPos, const CUnit &dirUnit, const bool dirFrom, const int minRange, const int devRadius, const int rangeDev)
{
Assert(&dirUnit != nullptr);
const Vec2i dirPos = dirUnit.tilePos + dirUnit.Type->GetHalfTileSize();
return GetRndPosInDirection(srcPos, dirPos, dirFrom, minRange, devRadius, rangeDev);
}
Expand Down
24 changes: 11 additions & 13 deletions src/unit/unit_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +100,23 @@ CUnit *CUnitManager::AllocUnit()
**
** @param unit Unit to release
*/
void CUnitManager::ReleaseUnit(CUnit *unit)
void CUnitManager::ReleaseUnit(CUnit &unit)
{
Assert(unit);

if (lastCreated == unit) {
if (lastCreated == &unit) {
lastCreated = nullptr;
}
if (unit->UnitManagerData.unitSlot != -1) { // == -1 when loading.
Assert(units[unit->UnitManagerData.unitSlot] == unit);
if (unit.UnitManagerData.unitSlot != -1) { // == -1 when loading.
Assert(units[unit.UnitManagerData.unitSlot] == &unit);

CUnit *temp = units.back();
temp->UnitManagerData.unitSlot = unit->UnitManagerData.unitSlot;
units[unit->UnitManagerData.unitSlot] = temp;
unit->UnitManagerData.unitSlot = -1;
temp->UnitManagerData.unitSlot = unit.UnitManagerData.unitSlot;
units[unit.UnitManagerData.unitSlot] = temp;
unit.UnitManagerData.unitSlot = -1;
units.pop_back();
}
Assert(unit->PlayerSlot == -1);
releasedUnits.push_back(unit);
unit->ReleaseCycle = GameCycle + 500; // can be reused after this time
Assert(unit.PlayerSlot == -1);
releasedUnits.push_back(&unit);
unit.ReleaseCycle = GameCycle + 500; // can be reused after this time
//Refs = GameCycle + (NetworkMaxLag << 1); // could be reuse after this time
}

Expand Down Expand Up @@ -201,7 +199,7 @@ void CUnitManager::Load(lua_State *l)
}
}
Assert(unit_index != -1 && cycle != static_cast<unsigned long>(-1));
ReleaseUnit(unitSlots[unit_index]);
ReleaseUnit(*unitSlots[unit_index]);
unitSlots[unit_index]->ReleaseCycle = cycle;
lua_pop(l, 1);
}
Expand Down
6 changes: 1 addition & 5 deletions src/unit/unit_save.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,7 @@ std::string UnitReference(const CUnit &unit)
std::string UnitReference(const CUnitPtr &unit)
{
Assert(unit != nullptr);

std::ostringstream ss;
ss << "U" << std::setfill('0') << std::setw(4) << std::uppercase
<< std::hex << UnitNumber(*unit);
return ss.str();
return UnitReference(*unit);
}

void PathFinderInput::Save(CFile &file) const
Expand Down

0 comments on commit 1c90625

Please sign in to comment.