From 52047805db8a67c85e38372a4f9d861a92c8a6fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Sun, 13 Feb 2022 01:48:36 +0200 Subject: [PATCH] Use correct type for EntityId and fix issues --- src/openrct2-ui/scripting/ScTitleSequence.hpp | 16 +++--- src/openrct2-ui/title/TitleSequencePlayer.cpp | 4 +- .../windows/TitleCommandEditor.cpp | 56 +++++++++---------- src/openrct2-ui/windows/TitleEditor.cpp | 8 +-- src/openrct2/Game.cpp | 2 +- src/openrct2/GameStateSnapshots.cpp | 2 +- src/openrct2/Identifiers.h | 2 +- src/openrct2/core/GroupVector.hpp | 13 +++-- src/openrct2/core/Identifier.hpp | 5 +- src/openrct2/entity/EntityRegistry.cpp | 2 +- src/openrct2/title/TitleSequence.cpp | 14 ++--- src/openrct2/title/TitleSequence.h | 4 +- 12 files changed, 67 insertions(+), 61 deletions(-) diff --git a/src/openrct2-ui/scripting/ScTitleSequence.hpp b/src/openrct2-ui/scripting/ScTitleSequence.hpp index 643a12606d..f04d942326 100644 --- a/src/openrct2-ui/scripting/ScTitleSequence.hpp +++ b/src/openrct2-ui/scripting/ScTitleSequence.hpp @@ -55,8 +55,8 @@ namespace OpenRCT2::Scripting obj.Set("index", value.SaveIndex); break; case TitleScript::Location: - obj.Set("x", value.X); - obj.Set("y", value.Y); + obj.Set("x", value.Location.X); + obj.Set("y", value.Location.Y); break; case TitleScript::Rotate: obj.Set("rotations", value.Rotations); @@ -65,10 +65,10 @@ namespace OpenRCT2::Scripting obj.Set("zoom", value.Zoom); break; case TitleScript::Follow: - if (value.SpriteIndex.IsNull()) + if (value.Follow.SpriteIndex.IsNull()) obj.Set("id", nullptr); else - obj.Set("id", value.SpriteIndex.ToUnderlying()); + obj.Set("id", value.Follow.SpriteIndex.ToUnderlying()); break; case TitleScript::Speed: obj.Set("speed", value.Speed); @@ -103,8 +103,8 @@ namespace OpenRCT2::Scripting command.SaveIndex = value["index"].as_int(); break; case TitleScript::Location: - command.X = value["x"].as_int(); - command.Y = value["y"].as_int(); + command.Location.X = value["x"].as_int(); + command.Location.Y = value["y"].as_int(); break; case TitleScript::Rotate: command.Rotations = value["rotations"].as_int(); @@ -117,11 +117,11 @@ namespace OpenRCT2::Scripting auto dukId = value["id"]; if (dukId.type() == DukValue::Type::NUMBER) { - command.SpriteIndex = EntityId::FromUnderlying(dukId.as_int()); + command.Follow.SpriteIndex = EntityId::FromUnderlying(dukId.as_int()); } else { - command.SpriteIndex = EntityId::GetNull(); + command.Follow.SpriteIndex = EntityId::GetNull(); } break; } diff --git a/src/openrct2-ui/title/TitleSequencePlayer.cpp b/src/openrct2-ui/title/TitleSequencePlayer.cpp index 53705453f6..4426219a59 100644 --- a/src/openrct2-ui/title/TitleSequencePlayer.cpp +++ b/src/openrct2-ui/title/TitleSequencePlayer.cpp @@ -254,7 +254,7 @@ private: break; case TitleScript::Location: { - auto loc = TileCoordsXY(command.X, command.Y).ToCoordsXY().ToTileCentre(); + auto loc = TileCoordsXY(command.Location.X, command.Location.Y).ToCoordsXY().ToTileCentre(); SetViewLocation(loc); break; } @@ -274,7 +274,7 @@ private: gGameSpeed = std::clamp(command.Speed, 1, 4); break; case TitleScript::Follow: - FollowSprite(command.SpriteIndex); + FollowSprite(command.Follow.SpriteIndex); break; case TitleScript::Restart: Reset(); diff --git a/src/openrct2-ui/windows/TitleCommandEditor.cpp b/src/openrct2-ui/windows/TitleCommandEditor.cpp index aecd027a80..e58f588862 100644 --- a/src/openrct2-ui/windows/TitleCommandEditor.cpp +++ b/src/openrct2-ui/windows/TitleCommandEditor.cpp @@ -243,8 +243,8 @@ void WindowTitleCommandEditorOpen(TitleSequence* sequence, int32_t index, bool i _command.SaveIndex = SAVE_INDEX_INVALID; break; case TitleScript::Location: - snprintf(textbox1Buffer, BUF_SIZE, "%d", _command.X); - snprintf(textbox2Buffer, BUF_SIZE, "%d", _command.Y); + snprintf(textbox1Buffer, BUF_SIZE, "%d", _command.Location.X); + snprintf(textbox2Buffer, BUF_SIZE, "%d", _command.Location.Y); break; case TitleScript::Rotate: case TitleScript::Zoom: @@ -254,9 +254,9 @@ void WindowTitleCommandEditorOpen(TitleSequence* sequence, int32_t index, bool i snprintf(textbox1Buffer, BUF_SIZE, "%d", _command.Milliseconds); break; case TitleScript::Follow: - if (!_command.SpriteIndex.IsNull()) + if (!_command.Follow.SpriteIndex.IsNull()) { - window_follow_sprite(window, _command.SpriteIndex); + window_follow_sprite(window, _command.Follow.SpriteIndex); } break; case TitleScript::Undefined: @@ -314,10 +314,10 @@ static void WindowTitleCommandEditorMouseup(rct_window* w, rct_widgetindex widge if (_command.Type == TitleScript::Location) { auto tileCoord = GetLocation(); - _command.X = static_cast(tileCoord.x); - _command.Y = static_cast(tileCoord.y); - snprintf(textbox1Buffer, BUF_SIZE, "%d", _command.X); - snprintf(textbox2Buffer, BUF_SIZE, "%d", _command.Y); + _command.Location.X = static_cast(tileCoord.x); + _command.Location.Y = static_cast(tileCoord.y); + snprintf(textbox1Buffer, BUF_SIZE, "%d", _command.Location.X); + snprintf(textbox2Buffer, BUF_SIZE, "%d", _command.Location.Y); } else if (_command.Type == TitleScript::Zoom) { @@ -432,7 +432,7 @@ static void WindowTitleCommandEditorDropdown(rct_window* w, rct_widgetindex widg switch (widgetIndex) { case WIDX_COMMAND_DROPDOWN: - if (!_command.SpriteIndex.IsNull()) + if (!_command.Follow.SpriteIndex.IsNull()) { window_unfollow_sprite(w); } @@ -446,10 +446,10 @@ static void WindowTitleCommandEditorDropdown(rct_window* w, rct_widgetindex widg case TitleScript::Location: { auto tileCoord = GetLocation(); - _command.X = static_cast(tileCoord.x); - _command.Y = static_cast(tileCoord.y); - snprintf(textbox1Buffer, BUF_SIZE, "%d", _command.X); - snprintf(textbox2Buffer, BUF_SIZE, "%d", _command.Y); + _command.Location.X = static_cast(tileCoord.x); + _command.Location.Y = static_cast(tileCoord.y); + snprintf(textbox1Buffer, BUF_SIZE, "%d", _command.Location.X); + snprintf(textbox2Buffer, BUF_SIZE, "%d", _command.Location.Y); break; } case TitleScript::Rotate: @@ -471,8 +471,8 @@ static void WindowTitleCommandEditorDropdown(rct_window* w, rct_widgetindex widg snprintf(textbox1Buffer, BUF_SIZE, "%d", _command.Zoom); break; case TitleScript::Follow: - _command.SpriteIndex = EntityId::GetNull(); - _command.SpriteName[0] = '\0'; + _command.Follow.SpriteIndex = EntityId::GetNull(); + _command.Follow.SpriteName[0] = '\0'; window_unfollow_sprite(w); // This is incorrect w->viewport->flags &= ~VIEWPORT_FLAG_GRIDLINES; @@ -588,9 +588,9 @@ static void WindowTitleCommandEditorTextinput(rct_window* w, rct_widgetindex wid { if (*end == '\0') { - _command.X = static_cast(value); + _command.Location.X = static_cast(value); } - snprintf(textbox1Buffer, BUF_SIZE, "%d", _command.X); + snprintf(textbox1Buffer, BUF_SIZE, "%d", _command.Location.X); w->Invalidate(); } else @@ -603,9 +603,9 @@ static void WindowTitleCommandEditorTextinput(rct_window* w, rct_widgetindex wid { if (*end == '\0') { - _command.Y = static_cast(value); + _command.Location.Y = static_cast(value); } - snprintf(textbox2Buffer, BUF_SIZE, "%d", _command.Y); + snprintf(textbox2Buffer, BUF_SIZE, "%d", _command.Location.Y); w->Invalidate(); } else @@ -643,7 +643,7 @@ static void WindowTitleCommandEditorToolDown(rct_window* w, rct_widgetindex widg validSprite = true; Formatter ft; peep->FormatNameTo(ft); - format_string(_command.SpriteName, USER_STRING_MAX_LENGTH, STR_STRINGID, &peep->Id); + format_string(_command.Follow.SpriteName, USER_STRING_MAX_LENGTH, STR_STRINGID, &peep->Id); } else if (vehicle != nullptr) { @@ -654,7 +654,7 @@ static void WindowTitleCommandEditorToolDown(rct_window* w, rct_widgetindex widg { Formatter ft; ride->FormatNameTo(ft); - format_string(_command.SpriteName, USER_STRING_MAX_LENGTH, STR_STRINGID, ft.Data()); + format_string(_command.Follow.SpriteName, USER_STRING_MAX_LENGTH, STR_STRINGID, ft.Data()); } } else if (litter != nullptr) @@ -663,24 +663,24 @@ static void WindowTitleCommandEditorToolDown(rct_window* w, rct_widgetindex widg if (name != STR_NONE) { validSprite = true; - format_string(_command.SpriteName, USER_STRING_MAX_LENGTH, name, nullptr); + format_string(_command.Follow.SpriteName, USER_STRING_MAX_LENGTH, name, nullptr); } } else if (balloon != nullptr) { validSprite = true; - format_string(_command.SpriteName, USER_STRING_MAX_LENGTH, STR_SHOP_ITEM_SINGULAR_BALLOON, nullptr); + format_string(_command.Follow.SpriteName, USER_STRING_MAX_LENGTH, STR_SHOP_ITEM_SINGULAR_BALLOON, nullptr); } else if (duck != nullptr) { validSprite = true; - format_string(_command.SpriteName, USER_STRING_MAX_LENGTH, STR_DUCK, nullptr); + format_string(_command.Follow.SpriteName, USER_STRING_MAX_LENGTH, STR_DUCK, nullptr); } if (validSprite) { - _command.SpriteIndex = entity->sprite_index; - window_follow_sprite(w, _command.SpriteIndex); + _command.Follow.SpriteIndex = entity->sprite_index; + window_follow_sprite(w, _command.Follow.SpriteIndex); tool_cancel(); w->Invalidate(); } @@ -790,10 +790,10 @@ static void WindowTitleCommandEditorPaint(rct_window* w, rct_drawpixelinfo* dpi) uint8_t colour = COLOUR_BLACK; rct_string_id spriteString = STR_TITLE_COMMAND_EDITOR_FORMAT_SPRITE_NAME; auto ft = Formatter(); - if (!_command.SpriteIndex.IsNull()) + if (!_command.Follow.SpriteIndex.IsNull()) { window_draw_viewport(dpi, w); - ft.Add(_command.SpriteName); + ft.Add(_command.Follow.SpriteName); } else { diff --git a/src/openrct2-ui/windows/TitleEditor.cpp b/src/openrct2-ui/windows/TitleEditor.cpp index a1c0ced3d3..e5dcdd1f5c 100644 --- a/src/openrct2-ui/windows/TitleEditor.cpp +++ b/src/openrct2-ui/windows/TitleEditor.cpp @@ -946,8 +946,8 @@ static void WindowTitleEditorScrollpaintCommands(rct_window* w, rct_drawpixelinf { auto commandName = STR_TITLE_EDITOR_COMMAND_LOCATION; ft.Add(commandName); - ft.Add(command.X); - ft.Add(command.Y); + ft.Add(command.Location.X); + ft.Add(command.Location.Y); break; } case TitleScript::Rotate: @@ -974,7 +974,7 @@ static void WindowTitleEditorScrollpaintCommands(rct_window* w, rct_drawpixelinf case TitleScript::Follow: { auto commandName = STR_TITLE_EDITOR_COMMAND_FOLLOW; - if (command.SpriteIndex.IsNull()) + if (command.Follow.SpriteIndex.IsNull()) { commandName = STR_TITLE_EDITOR_COMMAND_FOLLOW_NO_SPRITE; ft.Add(commandName); @@ -982,7 +982,7 @@ static void WindowTitleEditorScrollpaintCommands(rct_window* w, rct_drawpixelinf else { ft.Add(commandName); - ft.Add(command.SpriteName); + ft.Add(command.Follow.SpriteName); } break; } diff --git a/src/openrct2/Game.cpp b/src/openrct2/Game.cpp index 6b8edd06b6..434b6c9283 100644 --- a/src/openrct2/Game.cpp +++ b/src/openrct2/Game.cpp @@ -519,7 +519,7 @@ void game_unload_scripts() */ void reset_all_sprite_quadrant_placements() { - for (size_t i = 0; i < MAX_ENTITIES; i++) + for (EntityId::UnderlyingType i = 0; i < MAX_ENTITIES; i++) { auto* spr = GetEntity(EntityId::FromUnderlying(i)); if (spr != nullptr && spr->Type != EntityType::Null) diff --git a/src/openrct2/GameStateSnapshots.cpp b/src/openrct2/GameStateSnapshots.cpp index 76097c32b6..0b81153630 100644 --- a/src/openrct2/GameStateSnapshots.cpp +++ b/src/openrct2/GameStateSnapshots.cpp @@ -83,7 +83,7 @@ struct GameStateSnapshot_t if (saving) { - for (size_t i = 0; i < numSprites; i++) + for (EntityId::UnderlyingType i = 0; i < numSprites; i++) { auto entity = getEntity(EntityId::FromUnderlying(i)); if (entity == nullptr || entity->base.Type == EntityType::Null) diff --git a/src/openrct2/Identifiers.h b/src/openrct2/Identifiers.h index e33a8f38f8..d5201d0a0f 100644 --- a/src/openrct2/Identifiers.h +++ b/src/openrct2/Identifiers.h @@ -20,4 +20,4 @@ using BannerIndex = TIdentifier::max(), using RideId = TIdentifier::max(), struct RideIdTag>; -using EntityId = TIdentifier::max(), struct EntityIdTag>; +using EntityId = TIdentifier::max(), struct EntityIdTag>; diff --git a/src/openrct2/core/GroupVector.hpp b/src/openrct2/core/GroupVector.hpp index 89157a7dc3..099deda70a 100644 --- a/src/openrct2/core/GroupVector.hpp +++ b/src/openrct2/core/GroupVector.hpp @@ -8,7 +8,10 @@ *****************************************************************************/ #pragma once + #include +#include +#include #include template class GroupVector @@ -18,7 +21,7 @@ template class GroupVector public: bool Contains(Handle handle, V value) { - const auto index = static_cast(handle); + const auto index = static_cast(handle); if (index >= _data.size()) return false; @@ -28,7 +31,7 @@ public: void Add(Handle handle, V value) { - const auto index = static_cast(handle); + const auto index = static_cast(handle); if (index >= _data.size()) { _data.resize(index + 1); @@ -44,7 +47,7 @@ public: void Set(Handle handle, std::vector&& values) { - const auto index = static_cast(handle); + const auto index = static_cast(handle); if (index >= _data.size()) { _data.resize(index + 1); @@ -54,7 +57,7 @@ public: std::vector* GetAll(Handle handle) { - const auto index = static_cast(handle); + const auto index = static_cast(handle); if (index < _data.size()) { return &_data[index]; @@ -69,7 +72,7 @@ public: void RemoveHandle(Handle handle) { - const auto index = static_cast(handle); + const auto index = static_cast(handle); if (index < _data.size()) { _data[index].clear(); diff --git a/src/openrct2/core/Identifier.hpp b/src/openrct2/core/Identifier.hpp index 540ebe58c1..21a8fca916 100644 --- a/src/openrct2/core/Identifier.hpp +++ b/src/openrct2/core/Identifier.hpp @@ -9,6 +9,9 @@ #pragma once +#include +#include + template class TIdentifier { enum class ValueType : T @@ -48,7 +51,7 @@ public: } // Support for static_cast. - explicit operator std::size_t() const noexcept + explicit operator size_t() const noexcept { return static_cast(ToUnderlying()); } diff --git a/src/openrct2/entity/EntityRegistry.cpp b/src/openrct2/entity/EntityRegistry.cpp index f8666d714b..c2d30c6713 100644 --- a/src/openrct2/entity/EntityRegistry.cpp +++ b/src/openrct2/entity/EntityRegistry.cpp @@ -217,7 +217,7 @@ void ResetEntitySpatialIndices() { vec.clear(); } - for (size_t i = 0; i < MAX_ENTITIES; i++) + for (EntityId::UnderlyingType i = 0; i < MAX_ENTITIES; i++) { auto* spr = GetEntity(EntityId::FromUnderlying(i)); if (spr != nullptr && spr->Type != EntityType::Null) diff --git a/src/openrct2/title/TitleSequence.cpp b/src/openrct2/title/TitleSequence.cpp index 600f876d03..1eec8440a4 100644 --- a/src/openrct2/title/TitleSequence.cpp +++ b/src/openrct2/title/TitleSequence.cpp @@ -350,8 +350,8 @@ static std::vector LegacyScriptRead(const std::vector& sc else if (_stricmp(token, "LOCATION") == 0) { command.Type = TitleScript::Location; - command.X = atoi(part1) & 0xFF; - command.Y = atoi(part2) & 0xFF; + command.Location.X = atoi(part1) & 0xFF; + command.Location.Y = atoi(part2) & 0xFF; } else if (_stricmp(token, "ROTATE") == 0) { @@ -371,8 +371,8 @@ static std::vector LegacyScriptRead(const std::vector& sc else if (_stricmp(token, "FOLLOW") == 0) { command.Type = TitleScript::Follow; - command.SpriteIndex = EntityId::FromUnderlying(atoi(part1) & 0xFFFF); - safe_strcpy(command.SpriteName, part2, USER_STRING_MAX_LENGTH); + command.Follow.SpriteIndex = EntityId::FromUnderlying(atoi(part1) & 0xFFFF); + safe_strcpy(command.Follow.SpriteName, part2, USER_STRING_MAX_LENGTH); } else if (_stricmp(token, "WAIT") == 0) { @@ -526,7 +526,7 @@ static std::string LegacyScriptWrite(const TitleSequence& seq) case TitleScript::EndLoop: break; case TitleScript::Location: - String::Format(buffer, sizeof(buffer), "LOCATION %u %u", command.X, command.Y); + String::Format(buffer, sizeof(buffer), "LOCATION %u %u", command.Location.X, command.Location.Y); sb.Append(buffer); break; case TitleScript::Rotate: @@ -538,9 +538,9 @@ static std::string LegacyScriptWrite(const TitleSequence& seq) sb.Append(buffer); break; case TitleScript::Follow: - String::Format(buffer, sizeof(buffer), "FOLLOW %u ", command.SpriteIndex); + String::Format(buffer, sizeof(buffer), "FOLLOW %u ", command.Follow.SpriteIndex); sb.Append(buffer); - sb.Append(command.SpriteName); + sb.Append(command.Follow.SpriteName); break; case TitleScript::Speed: String::Format(buffer, sizeof(buffer), "SPEED %u", command.Speed); diff --git a/src/openrct2/title/TitleSequence.h b/src/openrct2/title/TitleSequence.h index 893f23e4cc..9dfe3d707e 100644 --- a/src/openrct2/title/TitleSequence.h +++ b/src/openrct2/title/TitleSequence.h @@ -27,14 +27,14 @@ struct TitleCommand { uint8_t X; uint8_t Y; - }; + } Location; uint8_t Rotations; // ROTATE (counter-clockwise) uint8_t Zoom; // ZOOM struct // FOLLOW { EntityId SpriteIndex; utf8 SpriteName[USER_STRING_MAX_LENGTH]; - }; + } Follow; uint8_t Speed; // SPEED uint16_t Milliseconds; // WAIT utf8 Scenario[TITLE_COMMAND_SCENARIO_LENGTH]; // LOADSC