Better annotate some magic numbers in for loops

This commit is contained in:
Hielke Morsink 2022-10-17 18:08:55 +02:00 committed by GitHub
parent fdbceae17c
commit 3bb8ea3668
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 62 additions and 61 deletions

View File

@ -915,8 +915,8 @@ public:
if (ridePage)
{
for (int32_t i = 0; i < 7; i++)
pressed_widgets &= ~(1 << (WIDX_FILTER_RIDE_TAB_ALL + i));
for (int32_t i = WIDX_FILTER_RIDE_TAB_ALL; i <= WIDX_FILTER_RIDE_TAB_STALL; i++)
pressed_widgets &= ~(1 << i);
if ((_filter_flags & FILTER_RIDES) == FILTER_RIDES)
pressed_widgets |= (1ULL << WIDX_FILTER_RIDE_TAB_ALL);

View File

@ -218,8 +218,8 @@ rct_window* WindowEditorObjectiveOptionsOpen()
static void WindowEditorObjectiveOptionsSetPressedTab(rct_window* w)
{
int32_t i;
for (i = 0; i < 2; i++)
w->pressed_widgets &= ~(1 << (WIDX_TAB_1 + i));
for (i = WIDX_TAB_1; i <= WIDX_TAB_2; i++)
w->pressed_widgets &= ~(1 << i);
w->pressed_widgets |= 1LL << (WIDX_TAB_1 + w->page);
}

View File

@ -904,7 +904,7 @@ public:
rct_widget* dropdownWidget = &widgets[widgetIndex - 1];
for (int32_t i = 0; i < 4; i++)
for (std::size_t i = 0; i < std::size(ResearchFundingLevelNames); i++)
{
gDropdownItems[i].Format = STR_DROPDOWN_MENU_LABEL;
gDropdownItems[i].Args = ResearchFundingLevelNames[i];

View File

@ -842,7 +842,8 @@ static void WindowLoadsavePopulateList(
{
// List Windows drives
w->disabled_widgets |= (1ULL << WIDX_NEW_FILE) | (1ULL << WIDX_NEW_FOLDER) | (1ULL << WIDX_UP);
for (int32_t x = 0; x < 26; x++)
static constexpr auto NumDriveLetters = 26;
for (int32_t x = 0; x < NumDriveLetters; x++)
{
if (drives & (1 << x))
{

View File

@ -804,11 +804,11 @@ public:
// Land tool mode (4 checkboxes)
int checkboxY = height - 55;
for (int32_t i = 0; i < 4; i++)
for (int32_t i = WIDX_LAND_OWNED_CHECKBOX; i <= WIDX_CONSTRUCTION_RIGHTS_SALE_CHECKBOX; i++)
{
widgets[WIDX_LAND_OWNED_CHECKBOX + i].top = checkboxY;
widgets[i].top = checkboxY;
checkboxY += 11;
widgets[WIDX_LAND_OWNED_CHECKBOX + i].bottom = checkboxY;
widgets[i].bottom = checkboxY;
checkboxY += 2;
}
@ -844,8 +844,8 @@ public:
widgets[WIDX_LAND_TOOL_SMALLER].type = WindowWidgetType::TrnBtn;
widgets[WIDX_LAND_TOOL_LARGER].type = WindowWidgetType::TrnBtn;
for (int32_t i = 0; i < 4; i++)
widgets[WIDX_LAND_OWNED_CHECKBOX + i].type = WindowWidgetType::Checkbox;
for (int32_t i = WIDX_LAND_OWNED_CHECKBOX; i <= WIDX_CONSTRUCTION_RIGHTS_SALE_CHECKBOX; i++)
widgets[i].type = WindowWidgetType::Checkbox;
widgets[WIDX_LAND_TOOL].image = LandTool::SizeToSpriteIndex(_landRightsToolSize);
}

View File

@ -277,9 +277,9 @@ static void WindowMultiplayerAnchorBorderWidgets(rct_window* w)
static void WindowMultiplayerSetPressedTab(rct_window* w)
{
for (int32_t i = 0; i < 2; i++)
for (int32_t i = WIDX_TAB1; i <= WIDX_TAB4; i++)
{
w->pressed_widgets &= ~(1 << (WIDX_TAB1 + i));
w->pressed_widgets &= ~(1 << i);
}
w->pressed_widgets |= 1LL << (WIDX_TAB1 + w->page);
}

View File

@ -1213,8 +1213,8 @@ private:
void SetPressedTab()
{
for (auto i = 0; i < 7; i++)
pressed_widgets &= ~(1 << (WIDX_TAB_1 + i));
for (int32_t i = WIDX_TAB_1; i <= WIDX_TAB_7; i++)
pressed_widgets &= ~(1 << i);
pressed_widgets |= 1LL << (WIDX_TAB_1 + page);
}

View File

@ -7,6 +7,7 @@
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include <iterator>
#include <openrct2-ui/interface/Dropdown.h>
#include <openrct2-ui/interface/Widget.h>
#include <openrct2-ui/windows/Window.h>
@ -425,15 +426,12 @@ static void WindowResearchFundingMouseup(rct_window* w, WidgetIndex widgetIndex)
*/
static void WindowResearchFundingMousedown(rct_window* w, WidgetIndex widgetIndex, rct_widget* widget)
{
rct_widget* dropdownWidget;
int32_t i;
if (widgetIndex != WIDX_RESEARCH_FUNDING_DROPDOWN_BUTTON)
return;
dropdownWidget = widget - 1;
rct_widget* dropdownWidget = widget - 1;
for (i = 0; i < 4; i++)
for (std::size_t i = 0; i < std::size(ResearchFundingLevelNames); i++)
{
gDropdownItems[i].Format = STR_DROPDOWN_MENU_LABEL;
gDropdownItems[i].Args = ResearchFundingLevelNames[i];

View File

@ -294,7 +294,7 @@ struct GameStateSnapshots final : public IGameStateSnapshots
COMPARE_FIELD(Peep, PathfindGoal.y);
COMPARE_FIELD(Peep, PathfindGoal.z);
COMPARE_FIELD(Peep, PathfindGoal.direction);
for (int i = 0; i < 4; i++)
for (std::size_t i = 0; i < spriteCmp.PathfindHistory.size(); i++)
{
COMPARE_FIELD(Peep, PathfindHistory[i].x);
COMPARE_FIELD(Peep, PathfindHistory[i].y);
@ -349,7 +349,7 @@ struct GameStateSnapshots final : public IGameStateSnapshots
COMPARE_FIELD(Guest, RejoinQueueTimeout);
COMPARE_FIELD(Guest, PreviousRide);
COMPARE_FIELD(Guest, PreviousRideTimeOut);
for (int i = 0; i < PEEP_MAX_THOUGHTS; i++)
for (std::size_t i = 0; i < PEEP_MAX_THOUGHTS; i++)
{
COMPARE_FIELD(Guest, Thoughts[i].type);
COMPARE_FIELD(Guest, Thoughts[i].item);
@ -412,11 +412,11 @@ struct GameStateSnapshots final : public IGameStateSnapshots
COMPARE_FIELD(Vehicle, SwingSpeed);
COMPARE_FIELD(Vehicle, status);
COMPARE_FIELD(Vehicle, sub_state);
for (int i = 0; i < 32; i++)
for (std::size_t i = 0; i < sizeof(Vehicle::peep) / sizeof(*Vehicle::peep); i++)
{
COMPARE_FIELD(Vehicle, peep[i]);
}
for (int i = 0; i < 32; i++)
for (std::size_t i = 0; i < sizeof(Vehicle::peep_tshirt_colours) / sizeof(*Vehicle::peep_tshirt_colours); i++)
{
COMPARE_FIELD(Vehicle, peep_tshirt_colours[i]);
}
@ -437,7 +437,7 @@ struct GameStateSnapshots final : public IGameStateSnapshots
COMPARE_FIELD(Vehicle, powered_acceleration);
COMPARE_FIELD(Vehicle, CollisionDetectionTimer);
COMPARE_FIELD(Vehicle, animation_frame);
for (int i = 0; i < 2; i++)
for (std::size_t i = 0; i < sizeof(Vehicle::pad_C6) / sizeof(*Vehicle::pad_C6); i++)
{
COMPARE_FIELD(Vehicle, pad_C6[i]);
}
@ -489,7 +489,7 @@ struct GameStateSnapshots final : public IGameStateSnapshots
{
COMPARE_FIELD(VehicleCrashParticle, frame);
COMPARE_FIELD(VehicleCrashParticle, time_to_live);
for (int i = 0; i < 2; i++)
for (std::size_t i = 0; i < sizeof(VehicleCrashParticle::colour) / sizeof(*VehicleCrashParticle::colour); i++)
{
COMPARE_FIELD(VehicleCrashParticle, colour[i]);
}

View File

@ -455,13 +455,13 @@ template<> struct DataSerializerTraits_t<TileElement>
stream->WriteValue(tileElement.base_height);
stream->WriteValue(tileElement.clearance_height);
stream->WriteValue(tileElement.owner);
for (int i = 0; i < 3; ++i)
for (auto v : tileElement.pad_05)
{
stream->WriteValue(tileElement.pad_05[i]);
stream->WriteValue(v);
}
for (int i = 0; i < 8; ++i)
for (auto v : tileElement.pad_08)
{
stream->WriteValue(tileElement.pad_08[i]);
stream->WriteValue(v);
}
}
static void decode(OpenRCT2::IStream* stream, TileElement& tileElement)
@ -471,13 +471,13 @@ template<> struct DataSerializerTraits_t<TileElement>
tileElement.base_height = stream->ReadValue<uint8_t>();
tileElement.clearance_height = stream->ReadValue<uint8_t>();
tileElement.owner = stream->ReadValue<uint8_t>();
for (int i = 0; i < 3; ++i)
for (auto& v : tileElement.pad_05)
{
tileElement.pad_05[i] = stream->ReadValue<uint8_t>();
v = stream->ReadValue<uint8_t>();
}
for (int i = 0; i < 8; ++i)
for (auto& v : tileElement.pad_08)
{
tileElement.pad_08[i] = stream->ReadValue<uint8_t>();
v = stream->ReadValue<uint8_t>();
}
}
static void log(OpenRCT2::IStream* stream, const TileElement& tileElement)

View File

@ -15,6 +15,7 @@
#include "../profiling/Profiling.h"
#include "../scenario/Scenario.h"
#include "../world/Footpath.h"
#include "../world/Location.hpp"
#include "../world/Map.h"
#include "../world/Scenery.h"
#include "EntityRegistry.h"
@ -93,7 +94,7 @@ void JumpingFountain::StartAnimation(const JumpingFountainType newType, const Co
{
case PATTERN::CYCLIC_SQUARES:
// 0, 1, 2, 3
for (int32_t i = 0; i < 4; i++)
for (int32_t i = 0; i < NumOrthogonalDirections; i++)
{
JumpingFountain::Create(
newType, { newLoc + _fountainDirectionsPositive[i], newZ }, _fountainDirections[i],
@ -103,7 +104,7 @@ void JumpingFountain::StartAnimation(const JumpingFountainType newType, const Co
case PATTERN::BOUNCING_PAIRS:
// random [0, 2 or 1, 3]
randomIndex = scenario_rand() & 1;
for (int32_t i = randomIndex; i < 4; i += 2)
for (int32_t i = randomIndex; i < NumOrthogonalDirections; i += 2)
{
JumpingFountain::Create(
newType, { newLoc + _fountainDirectionsPositive[i], newZ }, _fountainDirections[i],

View File

@ -46,6 +46,7 @@
#include "../world/Climate.h"
#include "../world/Footpath.h"
#include "../world/LargeScenery.h"
#include "../world/Location.hpp"
#include "../world/Map.h"
#include "../world/Park.h"
#include "../world/Scenery.h"
@ -4842,7 +4843,7 @@ void Guest::UpdateRideMazePathfinding()
uint8_t hedges[4]{ 0xFF, 0xFF, 0xFF, 0xFF };
uint8_t openCount = 0;
uint8_t mazeReverseLastEdge = DirectionReverse(MazeLastEdge);
for (uint8_t i = 0; i < 4; ++i)
for (uint8_t i = 0; i < NumOrthogonalDirections; ++i)
{
if (!(mazeEntry & (1 << _MazeCurrentDirectionToOpenHedge[Var37 / 4][i])) && i != mazeReverseLastEdge)
{

View File

@ -735,7 +735,6 @@ static int32_t cc_get(InteractiveConsole& console, const arguments_t& argv)
}
static int32_t cc_set(InteractiveConsole& console, const arguments_t& argv)
{
uint32_t i;
if (argv.size() > 1)
{
int32_t int_val[4];
@ -744,7 +743,7 @@ static int32_t cc_set(InteractiveConsole& console, const arguments_t& argv)
bool double_valid[4];
bool invalidArgs = false;
for (i = 0; i < 4; i++)
for (uint32_t i = 0; i < std::size(int_val); i++)
{
if (i + 1 < argv.size())
{
@ -1201,8 +1200,8 @@ static int32_t cc_load_object(InteractiveConsole& console, const arguments_t& ar
{
char name[9] = { 0 };
std::fill_n(name, 8, ' ');
int32_t i = 0;
for (const char* ch = argv[0].c_str(); *ch != '\0' && i < 8; ch++)
std::size_t i = 0;
for (const char* ch = argv[0].c_str(); *ch != '\0' && i < std::size(name) - 1; ch++)
{
name[i++] = *ch;
}

View File

@ -1306,9 +1306,8 @@ void window_resize(rct_window& w, int32_t dw, int32_t dh)
window_event_invalidate_call(&w);
// Update scroll widgets
for (int32_t i = 0; i < 3; i++)
for (auto& scroll : w.scrolls)
{
auto& scroll = w.scrolls[i];
scroll.h_right = WINDOW_SCROLL_UNDEFINED;
scroll.v_bottom = WINDOW_SCROLL_UNDEFINED;
}

View File

@ -1047,7 +1047,7 @@ void PaintSurface(PaintSession& session, uint8_t direction, uint16_t height, con
tile_descriptor tileDescriptors[5];
tileDescriptors[0] = selfDescriptor;
for (int32_t i = 0; i < 4; i++)
for (std::size_t i = 0; i < std::size(viewport_surface_paint_data); i++)
{
const CoordsXY& offset = viewport_surface_paint_data[i][rotation];
const CoordsXY position = base + offset;

View File

@ -310,9 +310,9 @@ static void PaintTileElementBase(PaintSession& session, const CoordsXY& origCoor
{ 1, 7, 3 },
};
for (int32_t sy = 0; sy < 3; sy++)
for (std::size_t sy = 0; sy < std::size(segmentPositions); sy++)
{
for (int32_t sx = 0; sx < 3; sx++)
for (std::size_t sx = 0; sx < std::size(segmentPositions[sy]); sx++)
{
uint16_t segmentHeight = session.SupportSegments[segmentPositions[sy][sx]].height;
auto imageColourFlats = ImageId(SPR_LAND_TOOL_SIZE_1).WithTransparency(FilterPaletteID::PaletteDarken3);
@ -328,8 +328,8 @@ static void PaintTileElementBase(PaintSession& session, const CoordsXY& origCoor
if ((session.ViewFlags & VIEWPORT_FLAG_CLIP_VIEW) && (segmentHeight > gClipHeight))
continue;
int32_t xOffset = sy * 10;
int32_t yOffset = -22 + sx * 10;
int32_t xOffset = static_cast<int32_t>(sy) * 10;
int32_t yOffset = -22 + static_cast<int32_t>(sx) * 10;
PaintStruct* ps = PaintAddImageAsParent(
session, imageColourFlats, { xOffset, yOffset, segmentHeight }, { 10, 10, 1 },
{ xOffset + 1, yOffset + 16, segmentHeight });
@ -389,7 +389,7 @@ const uint16_t segment_offsets[9] = {
void PaintUtilSetSegmentSupportHeight(PaintSession& session, int32_t segments, uint16_t height, uint8_t slope)
{
SupportHeight* supportSegments = session.SupportSegments;
for (int32_t s = 0; s < 9; s++)
for (std::size_t s = 0; s < std::size(segment_offsets); s++)
{
if (segments & segment_offsets[s])
{

View File

@ -1590,7 +1590,7 @@ Direction OriginalPathfinding::ChooseDirection(const TileCoordsXYZ& loc, Peep& p
if (isThin)
{
for (int32_t i = 0; i < 4; ++i)
for (std::size_t i = 0; i < peep.PathfindHistory.size(); ++i)
{
if (peep.PathfindHistory[i] == loc)
{
@ -1604,7 +1604,7 @@ Direction OriginalPathfinding::ChooseDirection(const TileCoordsXYZ& loc, Peep& p
if (_pathFindDebug)
{
log_verbose(
"Updating existing pf_history (in index: %d) for %d,%d,%d without entry edge %d & exit edge %d.", i,
"Updating existing pf_history (in index: %u) for %d,%d,%d without entry edge %d & exit edge %d.", i,
loc.x, loc.y, loc.z, DirectionReverse(peep.PeepDirection), chosen_edge);
}
#endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1

View File

@ -16,6 +16,8 @@
#include "../Track.h"
#include "../TrackPaint.h"
#include <iterator>
enum
{
SPR_CHAIRLIFT_CABLE_FLAT_SW_NE = 20500,
@ -79,7 +81,7 @@ static void chairlift_paint_util_draw_supports(PaintSession& session, int32_t se
{
bool success = false;
for (int32_t s = 0; s < 9; s++)
for (uint8_t s = 0; s < std::size(segment_offsets); s++)
{
if (!(segments & segment_offsets[s]))
{
@ -98,7 +100,7 @@ static void chairlift_paint_util_draw_supports(PaintSession& session, int32_t se
}
SupportHeight* supportSegments = session.SupportSegments;
for (int32_t s = 0; s < 9; s++)
for (uint8_t s = 0; s < std::size(segment_offsets); s++)
{
if (!(segments & segment_offsets[s]))
{

View File

@ -33,6 +33,7 @@
#include "../ride/Track.h"
#include "../ride/TrackData.h"
#include "../util/Util.h"
#include "Location.hpp"
#include "Map.h"
#include "MapAnimation.h"
#include "Park.h"
@ -509,7 +510,7 @@ static void FootpathConnectCorners(const CoordsXY& footpathPos, PathElement* ini
std::get<0>(tileElements) = { initialTileElement, footpathPos };
int32_t z = initialTileElement->GetBaseZ();
for (int32_t initialDirection = 0; initialDirection < 4; initialDirection++)
for (int32_t initialDirection = 0; initialDirection < NumOrthogonalDirections; initialDirection++)
{
int32_t direction = initialDirection;
auto currentPos = footpathPos + CoordsDirectionDelta[direction];
@ -1862,8 +1863,7 @@ void FootpathUpdatePathWideFlags(const CoordsXY& footpathPos)
// pathList is a list of elements, set by sub_6A8ACF adjacent to x,y
// Spanned from 0x00F3EFA8 to 0x00F3EFC7 (8 elements) in the original
std::array<TileElement*, 8> pathList;
for (int32_t direction = 0; direction < 8; ++direction)
for (std::size_t direction = 0; direction < pathList.size(); ++direction)
{
auto footpathLoc = CoordsXYZ(footpathPos + CoordsDirectionDelta[direction], height);
pathList[direction] = FootpathCanBeWide(footpathLoc);
@ -2030,7 +2030,7 @@ void FootpathUpdateQueueEntranceBanner(const CoordsXY& footpathPos, TileElement*
if (tileElement->AsPath()->IsQueue())
{
FootpathQueueChainPush(tileElement->AsPath()->GetRideIndex());
for (int32_t direction = 0; direction < 4; direction++)
for (int32_t direction = 0; direction < NumOrthogonalDirections; direction++)
{
if (tileElement->AsPath()->GetEdges() & (1 << direction))
{
@ -2284,7 +2284,7 @@ void FootpathRemoveEdgesAt(const CoordsXY& footpathPos, TileElement* tileElement
FootpathUpdateQueueEntranceBanner(footpathPos, tileElement);
bool fixCorners = false;
for (uint8_t direction = 0; direction < 4; direction++)
for (uint8_t direction = 0; direction < NumOrthogonalDirections; direction++)
{
int32_t z1 = tileElement->base_height;
if (tileElement->GetType() == TileElementType::Path)

View File

@ -25,7 +25,7 @@ static uint8_t GetBaseHeightOrZero(int32_t x, int32_t y)
*/
int32_t MapSmooth(int32_t l, int32_t t, int32_t r, int32_t b)
{
int32_t i, x, y, count, doubleCorner, raisedLand = 0;
int32_t x, y, count, doubleCorner, raisedLand = 0;
uint8_t highest, cornerHeights[4];
for (y = t; y < b; y++)
{
@ -55,14 +55,14 @@ int32_t MapSmooth(int32_t l, int32_t t, int32_t r, int32_t b)
cornerHeights[2] = GetBaseHeightOrZero(x + 1, y + 1);
cornerHeights[3] = GetBaseHeightOrZero(x - 1, y + 1);
highest = surfaceElement->base_height;
for (i = 0; i < 4; i++)
for (std::size_t i = 0; i < std::size(cornerHeights); i++)
highest = std::max(highest, cornerHeights[i]);
if (highest >= surfaceElement->base_height + 4)
{
count = 0;
int32_t canCompensate = 1;
for (i = 0; i < 4; i++)
for (std::size_t i = 0; i < std::size(cornerHeights); i++)
if (cornerHeights[i] == highest)
{
count++;