From 03378782ffc4f92edbcaf830fe34d8974f1ed607 Mon Sep 17 00:00:00 2001 From: Duncan Date: Thu, 3 Jun 2021 08:27:03 +0100 Subject: [PATCH] Split off litter code to litter.cpp (#14799) * Split off litter code to litter.cpp * Use PATH_CLEARANCE --- OpenRCT2.xcodeproj/project.pbxproj | 4 + src/openrct2/libopenrct2.vcxproj | 1 + src/openrct2/world/Litter.cpp | 127 +++++++++++++++++++++++++++++ src/openrct2/world/Sprite.cpp | 123 ---------------------------- 4 files changed, 132 insertions(+), 123 deletions(-) create mode 100644 src/openrct2/world/Litter.cpp diff --git a/OpenRCT2.xcodeproj/project.pbxproj b/OpenRCT2.xcodeproj/project.pbxproj index 850add3ef0..b6e6699cb4 100644 --- a/OpenRCT2.xcodeproj/project.pbxproj +++ b/OpenRCT2.xcodeproj/project.pbxproj @@ -766,6 +766,7 @@ F7D774AE1EC6741D00BE6EBC /* sequence in CopyFiles */ = {isa = PBXBuildFile; fileRef = D4EC48E51C2637710024B507 /* sequence */; }; E73A781EBC3C440A99D59821 /* EntityTweener.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0EB0D1DF6CC74E4190301D84 /* EntityTweener.cpp */; }; F42186C5840D4196981ADD16 /* EntityTweener.h in Headers */ = {isa = PBXBuildFile; fileRef = 091352A950004312BAB18717 /* EntityTweener.h */; }; + 0746674FA0794ABF86E406A1 /* Litter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9D3DD6CD73F5421880280D9D /* Litter.cpp */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -1834,6 +1835,7 @@ F7D774841EC66CD700BE6EBC /* OpenRCT2-cli */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "OpenRCT2-cli"; sourceTree = BUILT_PRODUCTS_DIR; }; 0EB0D1DF6CC74E4190301D84 /* EntityTweener.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = EntityTweener.cpp; path = src/openrct2/world/EntityTweener.cpp; sourceTree = SOURCE_ROOT; }; 091352A950004312BAB18717 /* EntityTweener.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = EntityTweener.h; path = src/openrct2/world/EntityTweener.h; sourceTree = SOURCE_ROOT; }; + 9D3DD6CD73F5421880280D9D /* Litter.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = Litter.cpp; path = src/openrct2/world/Litter.cpp; sourceTree = SOURCE_ROOT; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -3122,6 +3124,7 @@ 4C7B54422007646A00A52E21 /* Water.h */, 0EB0D1DF6CC74E4190301D84 /* EntityTweener.cpp */, 091352A950004312BAB18717 /* EntityTweener.h */, + 9D3DD6CD73F5421880280D9D /* Litter.cpp */, ); path = world; sourceTree = ""; @@ -4269,6 +4272,7 @@ 66A10FD1257F1E3000DD651A /* WaterRaiseAction.cpp in Sources */, C688791820289B9B0084B384 /* MonorailCycles.cpp in Sources */, E73A781EBC3C440A99D59821 /* EntityTweener.cpp in Sources */, + 0746674FA0794ABF86E406A1 /* Litter.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/src/openrct2/libopenrct2.vcxproj b/src/openrct2/libopenrct2.vcxproj index 709f91c421..375d579fa3 100644 --- a/src/openrct2/libopenrct2.vcxproj +++ b/src/openrct2/libopenrct2.vcxproj @@ -871,6 +871,7 @@ + diff --git a/src/openrct2/world/Litter.cpp b/src/openrct2/world/Litter.cpp new file mode 100644 index 0000000000..f2fa1d8108 --- /dev/null +++ b/src/openrct2/world/Litter.cpp @@ -0,0 +1,127 @@ +#include "Litter.h" + +#include "../Cheats.h" +#include "../localisation/StringIds.h" +#include "../scenario/Scenario.h" +#include "EntityList.h" +#include "Map.h" +#include "Sprite.h" + +static bool isLocationLitterable(const CoordsXYZ& mapPos) +{ + TileElement* tileElement; + + if (!map_is_location_owned(mapPos)) + return false; + + tileElement = map_get_first_element_at(mapPos); + if (tileElement == nullptr) + return false; + do + { + if (tileElement->GetType() != TILE_ELEMENT_TYPE_PATH) + continue; + + int32_t pathZ = tileElement->GetBaseZ(); + if (pathZ < mapPos.z || pathZ >= mapPos.z + PATH_CLEARANCE) + continue; + + return !tile_element_is_underground(tileElement); + } while (!(tileElement++)->IsLastForTile()); + return false; +} + +/** + * + * rct2: 0x0067375D + */ +void Litter::Create(const CoordsXYZD& litterPos, Type type) +{ + if (gCheatsDisableLittering) + return; + + auto offsetLitterPos = litterPos + + CoordsXY{ CoordsDirectionDelta[litterPos.direction >> 3].x / 8, + CoordsDirectionDelta[litterPos.direction >> 3].y / 8 }; + + if (!isLocationLitterable(offsetLitterPos)) + return; + + if (GetEntityListCount(EntityType::Litter) >= 500) + { + Litter* newestLitter = nullptr; + uint32_t newestLitterCreationTick = 0; + for (auto litter : EntityList()) + { + if (newestLitterCreationTick <= litter->creationTick) + { + newestLitterCreationTick = litter->creationTick; + newestLitter = litter; + } + } + + if (newestLitter != nullptr) + { + newestLitter->Invalidate(); + sprite_remove(newestLitter); + } + } + + Litter* litter = CreateEntity(); + if (litter == nullptr) + return; + + litter->sprite_direction = offsetLitterPos.direction; + litter->sprite_width = 6; + litter->sprite_height_negative = 6; + litter->sprite_height_positive = 3; + litter->SubType = type; + litter->MoveTo(offsetLitterPos); + litter->creationTick = gScenarioTicks; +} + +/** + * + * rct2: 0x006738E1 + */ +void Litter::RemoveAt(const CoordsXYZ& litterPos) +{ + std::vector removals; + for (auto litter : EntityTileList(litterPos)) + { + if (abs(litter->z - litterPos.z) <= 16) + { + if (abs(litter->x - litterPos.x) <= 8 && abs(litter->y - litterPos.y) <= 8) + { + removals.push_back(litter); + } + } + } + for (auto* litter : removals) + { + litter->Invalidate(); + sprite_remove(litter); + } +} + +static const rct_string_id litterNames[12] = { + STR_LITTER_VOMIT, + STR_LITTER_VOMIT, + STR_SHOP_ITEM_SINGULAR_EMPTY_CAN, + STR_SHOP_ITEM_SINGULAR_RUBBISH, + STR_SHOP_ITEM_SINGULAR_EMPTY_BURGER_BOX, + STR_SHOP_ITEM_SINGULAR_EMPTY_CUP, + STR_SHOP_ITEM_SINGULAR_EMPTY_BOX, + STR_SHOP_ITEM_SINGULAR_EMPTY_BOTTLE, + STR_SHOP_ITEM_SINGULAR_EMPTY_BOWL_RED, + STR_SHOP_ITEM_SINGULAR_EMPTY_DRINK_CARTON, + STR_SHOP_ITEM_SINGULAR_EMPTY_JUICE_CUP, + STR_SHOP_ITEM_SINGULAR_EMPTY_BOWL_BLUE, +}; + +rct_string_id Litter::GetName() const +{ + if (EnumValue(SubType) >= sizeof(litterNames)) + return STR_NONE; + return litterNames[EnumValue(SubType)]; +} diff --git a/src/openrct2/world/Sprite.cpp b/src/openrct2/world/Sprite.cpp index 4d021290f7..8c0a36a03f 100644 --- a/src/openrct2/world/Sprite.cpp +++ b/src/openrct2/world/Sprite.cpp @@ -9,18 +9,13 @@ #include "Sprite.h" -#include "../Cheats.h" #include "../Game.h" -#include "../OpenRCT2.h" -#include "../audio/audio.h" #include "../core/ChecksumStream.h" #include "../core/Crypt.h" #include "../core/DataSerialiser.h" #include "../core/Guard.hpp" #include "../core/MemoryStream.h" #include "../interface/Viewport.h" -#include "../localisation/Date.h" -#include "../localisation/Localisation.h" #include "../peep/Peep.h" #include "../ride/Vehicle.h" #include "../scenario/Scenario.h" @@ -562,124 +557,6 @@ void sprite_remove(SpriteBase* sprite) sprite_reset(sprite); } -static bool litter_can_be_at(const CoordsXYZ& mapPos) -{ - TileElement* tileElement; - - if (!map_is_location_owned(mapPos)) - return false; - - tileElement = map_get_first_element_at(mapPos); - if (tileElement == nullptr) - return false; - do - { - if (tileElement->GetType() != TILE_ELEMENT_TYPE_PATH) - continue; - - int32_t pathZ = tileElement->GetBaseZ(); - if (pathZ < mapPos.z || pathZ >= mapPos.z + 32) - continue; - - return !tile_element_is_underground(tileElement); - } while (!(tileElement++)->IsLastForTile()); - return false; -} - -/** - * - * rct2: 0x0067375D - */ -void Litter::Create(const CoordsXYZD& litterPos, Type type) -{ - if (gCheatsDisableLittering) - return; - - auto offsetLitterPos = litterPos - + CoordsXY{ CoordsDirectionDelta[litterPos.direction >> 3].x / 8, - CoordsDirectionDelta[litterPos.direction >> 3].y / 8 }; - - if (!litter_can_be_at(offsetLitterPos)) - return; - - if (GetEntityListCount(EntityType::Litter) >= 500) - { - Litter* newestLitter = nullptr; - uint32_t newestLitterCreationTick = 0; - for (auto litter : EntityList()) - { - if (newestLitterCreationTick <= litter->creationTick) - { - newestLitterCreationTick = litter->creationTick; - newestLitter = litter; - } - } - - if (newestLitter != nullptr) - { - newestLitter->Invalidate(); - sprite_remove(newestLitter); - } - } - - Litter* litter = CreateEntity(); - if (litter == nullptr) - return; - - litter->sprite_direction = offsetLitterPos.direction; - litter->sprite_width = 6; - litter->sprite_height_negative = 6; - litter->sprite_height_positive = 3; - litter->SubType = type; - litter->MoveTo(offsetLitterPos); - litter->creationTick = gScenarioTicks; -} - -/** - * - * rct2: 0x006738E1 - */ -void Litter::RemoveAt(const CoordsXYZ& litterPos) -{ - std::vector removals; - for (auto litter : EntityTileList(litterPos)) - { - if (abs(litter->z - litterPos.z) <= 16) - { - if (abs(litter->x - litterPos.x) <= 8 && abs(litter->y - litterPos.y) <= 8) - { - removals.push_back(litter); - } - } - } - for (auto* litter : removals) - { - litter->Invalidate(); - sprite_remove(litter); - } -} - -static const rct_string_id litterNames[12] = { - STR_LITTER_VOMIT, - STR_LITTER_VOMIT, - STR_SHOP_ITEM_SINGULAR_EMPTY_CAN, - STR_SHOP_ITEM_SINGULAR_RUBBISH, - STR_SHOP_ITEM_SINGULAR_EMPTY_BURGER_BOX, - STR_SHOP_ITEM_SINGULAR_EMPTY_CUP, - STR_SHOP_ITEM_SINGULAR_EMPTY_BOX, - STR_SHOP_ITEM_SINGULAR_EMPTY_BOTTLE, - STR_SHOP_ITEM_SINGULAR_EMPTY_BOWL_RED, - STR_SHOP_ITEM_SINGULAR_EMPTY_DRINK_CARTON, - STR_SHOP_ITEM_SINGULAR_EMPTY_JUICE_CUP, - STR_SHOP_ITEM_SINGULAR_EMPTY_BOWL_BLUE, -}; - -rct_string_id Litter::GetName() const -{ - if (EnumValue(SubType) >= sizeof(litterNames)) - return STR_NONE; - return litterNames[EnumValue(SubType)]; -} /** * Loops through all sprites, finds floating objects and removes them. * Returns the amount of removed objects as feedback.